light/modules/engine/src/utils/resource_manager.cpp

69 lines
1.9 KiB
C++
Raw Normal View History

#include <asset_parser/assets/texture.hpp>
#include <asset_parser/parser.hpp>
2025-07-05 13:28:41 +03:30
#include <engine/graphics/graphics_context.hpp>
#include <engine/graphics/shader.hpp>
#include <engine/graphics/texture.hpp>
#include <engine/utils/file_manager.hpp>
#include <engine/utils/resource_manager.hpp>
namespace Light {
void ResourceManager::load_shader_impl(
2025-07-05 13:28:41 +03:30
const std::string &name,
const std::string &vertexPath,
const std::string &pixelPath
)
{
lt_assert(!vertexPath.empty(), "Empty 'vertexPath'");
lt_assert(!pixelPath.empty(), "Empty 'pixelPath'");
2025-07-05 13:28:41 +03:30
2025-07-06 14:02:50 +03:30
auto vertexFile = FileManager::read_text_file(vertexPath);
auto pixelFile = FileManager::read_text_file(pixelPath);
2025-07-05 13:28:41 +03:30
lt_assert(vertexFile.is_valid(), "Failed to read vertex file: {}", vertexPath);
lt_assert(pixelFile.is_valid(), "Failed to read vertex file: {}", pixelPath);
2025-07-05 13:28:41 +03:30
m_shaders[name] = Ref<Shader>(
Shader::create(vertexFile, pixelFile, GraphicsContext::get_shared_context())
2025-07-05 13:28:41 +03:30
);
vertexFile.release();
pixelFile.release();
2025-07-05 13:28:41 +03:30
}
void ResourceManager::load_texture_impl(const std::string &name, const std::filesystem::path &path)
2025-07-05 13:28:41 +03:30
{
log_trc("Loading texture:");
log_trc("\tname: {}", name);
log_trc("\tpath: {}", path.string());
auto asset = Assets::TextureAsset { path };
const auto metadata = asset.get_metadata();
const auto blob_metadata = asset.get_blob_metadata(Assets::BlobMetadata::Tag::color);
auto blob = std::vector<std::byte>(blob_metadata.uncompressed_size);
asset.unpack_blob(blob_metadata.tag, blob.data(), blob.size());
2025-07-05 13:28:41 +03:30
m_textures[name] = Ref<Texture>(Texture::create(
metadata.pixel_size[0],
metadata.pixel_size[1],
metadata.num_components,
std::bit_cast<unsigned char *>(blob.data()),
GraphicsContext::get_shared_context(),
2025-07-05 13:28:41 +03:30
path
));
}
void ResourceManager::release_texture_impl(const std::string &name)
2025-07-05 13:28:41 +03:30
{
if (!m_textures[name])
2025-07-05 13:28:41 +03:30
{
2025-07-06 16:30:38 +03:30
log_wrn("Failed to find texture named: {}", name);
2025-07-05 13:28:41 +03:30
return;
}
m_textures[name] = nullptr;
2025-07-05 13:28:41 +03:30
}
} // namespace Light