From 150b73541c5f60dc291749deacb6ddec57cc25a2 Mon Sep 17 00:00:00 2001 From: assiduous Date: Wed, 4 Nov 2020 16:30:51 -0800 Subject: GLTFLoader: made texture cache thread-safe --- AssetLoader/src/GLTFLoader.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'AssetLoader/src/GLTFLoader.cpp') diff --git a/AssetLoader/src/GLTFLoader.cpp b/AssetLoader/src/GLTFLoader.cpp index e573963..7f64a27 100644 --- a/AssetLoader/src/GLTFLoader.cpp +++ b/AssetLoader/src/GLTFLoader.cpp @@ -651,8 +651,10 @@ void Model::LoadTextures(IRenderDevice* pDevice, RefCntAutoPtr pTexture; if (pTextureCache != nullptr) { - auto it = pTextureCache->find(BaseDir + gltf_image.uri); - if (it != pTextureCache->end()) + std::lock_guard Lock{pTextureCache->TexturesMtx}; + + auto it = pTextureCache->Textures.find(BaseDir + gltf_image.uri); + if (it != pTextureCache->Textures.end()) { pTexture = it->second.Lock(); if (!pTexture) @@ -667,7 +669,7 @@ void Model::LoadTextures(IRenderDevice* pDevice, } else { - pTextureCache->erase(it); + pTextureCache->Textures.erase(it); } } } @@ -719,7 +721,8 @@ void Model::LoadTextures(IRenderDevice* pDevice, if (pTextureCache != nullptr) { - pTextureCache->emplace(BaseDir + gltf_image.uri, pTexture); + std::lock_guard Lock{pTextureCache->TexturesMtx}; + pTextureCache->Textures.emplace(BaseDir + gltf_image.uri, pTexture); } } @@ -1140,8 +1143,12 @@ bool LoadImageData(tinygltf::Image* gltf_image, auto* pLoaderData = reinterpret_cast(user_data); if (pLoaderData != nullptr && pLoaderData->pTextureCache != nullptr) { - auto it = pLoaderData->pTextureCache->find(pLoaderData->BaseDir + gltf_image->uri); - if (it != pLoaderData->pTextureCache->end()) + auto& TexCache = *pLoaderData->pTextureCache; + + std::lock_guard Lock{TexCache.TexturesMtx}; + + auto it = TexCache.Textures.find(pLoaderData->BaseDir + gltf_image->uri); + if (it != TexCache.Textures.end()) { if (auto pTexture = it->second.Lock()) { @@ -1162,7 +1169,7 @@ bool LoadImageData(tinygltf::Image* gltf_image, else { // Texture is stale - remove it from the cache - pLoaderData->pTextureCache->erase(it); + TexCache.Textures.erase(it); } } } -- cgit v1.2.3