Use cached textures when loading meshes instead of always loading the textures again for each mesh

This commit is contained in:
tovjemam 2025-08-16 21:46:32 +02:00
parent 8fd8d5ec29
commit 102bc3f2b0
4 changed files with 28 additions and 2 deletions

View File

@ -3,9 +3,9 @@
## TODO ## TODO
Fix known issues: Fix known issues:
- [ ] Use cached textures when loading meshes instead of always loading the textures again for each mesh
- [ ] Fix apparently incorrect light radius of lights visible through scaled portals - [ ] Fix apparently incorrect light radius of lights visible through scaled portals
- [x] Fix GL_LINEAR_MIPMAP_LINEAR error - [x] Fix GL_LINEAR_MIPMAP_LINEAR error
- [x] Use cached textures when loading meshes instead of always loading the textures again for each mesh
Add new features: Add new features:
- [ ] Text and UI rendering - [ ] Text and UI rendering

View File

@ -5,6 +5,8 @@
#include <vector> #include <vector>
#include <map> #include <map>
std::map<std::string, std::weak_ptr<gfx::Texture>> assets::Mesh::s_texture_cache;
assets::Mesh::Mesh(std::span<MeshVertex> verts, std::span<MeshTriangle> tris, std::span<MeshMaterial> materials) : assets::Mesh::Mesh(std::span<MeshVertex> verts, std::span<MeshTriangle> tris, std::span<MeshMaterial> materials) :
va_(gfx::VA_POSITION | gfx::VA_NORMAL | gfx::VA_UV | gfx::VA_LIGHTMAP_UV, gfx::VF_CREATE_EBO) va_(gfx::VA_POSITION | gfx::VA_NORMAL | gfx::VA_UV | gfx::VA_LIGHTMAP_UV, gfx::VF_CREATE_EBO)
{ {
@ -110,7 +112,7 @@ std::shared_ptr<assets::Mesh> assets::Mesh::LoadFromFile(const std::string& file
if (!material.name.empty()) if (!material.name.empty())
{ {
try { try {
material.texture = gfx::Texture::LoadFromFile("data/" + material.name + ".png"); material.texture = LoadTexture("data/" + material.name + ".png");
} catch (const std::exception& e) { } catch (const std::exception& e) {
throw std::runtime_error("Failed to load texture for material '" + material.name + "': " + e.what()); throw std::runtime_error("Failed to load texture for material '" + material.name + "': " + e.what());
} }
@ -127,3 +129,20 @@ std::shared_ptr<assets::Mesh> assets::Mesh::LoadFromFile(const std::string& file
} }
return mesh; return mesh;
} }
std::shared_ptr<gfx::Texture> assets::Mesh::LoadTexture(const std::string& filename)
{
auto it = s_texture_cache.find(filename);
if (it != s_texture_cache.end())
{
if (auto texture = it->second.lock())
{
return texture; // Return cached texture
}
}
auto texture = gfx::Texture::LoadFromFile(filename);
s_texture_cache[filename] = texture; // Cache the texture
return texture;
}

View File

@ -3,6 +3,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <span> #include <span>
#include <map>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "gfx/vertex_array.hpp" #include "gfx/vertex_array.hpp"
@ -59,5 +60,9 @@ namespace assets
std::span<const MeshMaterial> GetMaterials() const { return materials_; } std::span<const MeshMaterial> GetMaterials() const { return materials_; }
static std::shared_ptr<Mesh> LoadFromFile(const std::string& filename, bool load_collision); static std::shared_ptr<Mesh> LoadFromFile(const std::string& filename, bool load_collision);
private:
static std::map<std::string, std::weak_ptr<gfx::Texture>> s_texture_cache;
static std::shared_ptr<gfx::Texture> LoadTexture(const std::string& filename);
}; };
} }

View File

@ -60,6 +60,8 @@ gfx::Texture::~Texture() {
std::shared_ptr<gfx::Texture> gfx::Texture::LoadFromFile(const std::string& filename) std::shared_ptr<gfx::Texture> gfx::Texture::LoadFromFile(const std::string& filename)
{ {
printf("Loading texture from file: %s\n", filename.c_str());
int width, height, channels; int width, height, channels;
unsigned char* data = stbi_load(filename.c_str(), &width, &height, &channels, 4); unsigned char* data = stbi_load(filename.c_str(), &width, &height, &channels, 4);