Skip to content

Commit 40e203b

Browse files
GLTFLoader: minor improvements to texture cache usage
1 parent 62bfc32 commit 40e203b

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

AssetLoader/src/GLTFLoader.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,20 @@ Uint32 Model::AddTexture(IRenderDevice* pDevice,
947947
if (TexInfo.pTexture && pTextureCache != nullptr)
948948
{
949949
std::unique_lock<std::shared_mutex> UniqueLock{pTextureCache->TexturesMtx};
950-
pTextureCache->Textures.emplace(CacheId, TexInfo.pTexture);
950+
auto [it, inserted] = pTextureCache->Textures.emplace(CacheId, TexInfo.pTexture);
951+
if (!inserted)
952+
{
953+
if (auto pCachedTex = it->second.Lock())
954+
{
955+
// Use the existing texture
956+
TexInfo.pTexture = pCachedTex;
957+
}
958+
else
959+
{
960+
// Replace the expired weak reference
961+
it->second = TexInfo.pTexture;
962+
}
963+
}
951964
}
952965
}
953966

@@ -1888,7 +1901,7 @@ bool FileExists(const std::string& abs_filename, void* user_data)
18881901
{
18891902
std::shared_lock<std::shared_mutex> SharedLock{pLoaderData->pTextureCache->TexturesMtx};
18901903

1891-
auto it = pLoaderData->pTextureCache->Textures.find(CacheId.c_str());
1904+
auto it = pLoaderData->pTextureCache->Textures.find(CacheId);
18921905
if (it != pLoaderData->pTextureCache->Textures.end())
18931906
return true;
18941907
}
@@ -1927,7 +1940,7 @@ bool ReadWholeFile(std::vector<unsigned char>* out,
19271940
{
19281941
std::shared_lock<std::shared_mutex> SharedLock{pLoaderData->pTextureCache->TexturesMtx};
19291942

1930-
auto it = pLoaderData->pTextureCache->Textures.find(CacheId.c_str());
1943+
auto it = pLoaderData->pTextureCache->Textures.find(CacheId);
19311944
if (it != pLoaderData->pTextureCache->Textures.end())
19321945
{
19331946
if (RefCntAutoPtr<ITexture> pTexture = it->second.Lock())

0 commit comments

Comments
 (0)