summaryrefslogtreecommitdiffstats
path: root/AssetLoader
diff options
context:
space:
mode:
authorassiduous <assiduous@diligentgraphics.com>2020-11-05 00:30:51 +0000
committerassiduous <assiduous@diligentgraphics.com>2020-11-05 00:30:51 +0000
commit150b73541c5f60dc291749deacb6ddec57cc25a2 (patch)
treed7356f3259d8e9b34013bd15029dfdc7d8db7ccb /AssetLoader
parentImGuiDiligentRenderer: added MSL shaders (diff)
downloadDiligentTools-150b73541c5f60dc291749deacb6ddec57cc25a2.tar.gz
DiligentTools-150b73541c5f60dc291749deacb6ddec57cc25a2.zip
GLTFLoader: made texture cache thread-safe
Diffstat (limited to 'AssetLoader')
-rw-r--r--AssetLoader/interface/GLTFLoader.hpp8
-rw-r--r--AssetLoader/src/GLTFLoader.cpp21
2 files changed, 21 insertions, 8 deletions
diff --git a/AssetLoader/interface/GLTFLoader.hpp b/AssetLoader/interface/GLTFLoader.hpp
index e3e9ed0..0a9275b 100644
--- a/AssetLoader/interface/GLTFLoader.hpp
+++ b/AssetLoader/interface/GLTFLoader.hpp
@@ -31,6 +31,7 @@
#include <memory>
#include <cfloat>
#include <unordered_map>
+#include <mutex>
#include "../../../DiligentCore/Graphics/GraphicsEngine/interface/RenderDevice.h"
#include "../../../DiligentCore/Graphics/GraphicsEngine/interface/DeviceContext.h"
@@ -277,7 +278,12 @@ struct Model
float3 max = float3{-FLT_MAX, -FLT_MAX, -FLT_MAX};
} dimensions;
- using TextureCacheType = std::unordered_map<std::string, RefCntWeakPtr<ITexture>>;
+ struct TextureCacheType
+ {
+ std::mutex TexturesMtx;
+
+ std::unordered_map<std::string, RefCntWeakPtr<ITexture>> Textures;
+ };
Model(IRenderDevice* pDevice,
IDeviceContext* pContext,
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<ITexture> pTexture;
if (pTextureCache != nullptr)
{
- auto it = pTextureCache->find(BaseDir + gltf_image.uri);
- if (it != pTextureCache->end())
+ std::lock_guard<std::mutex> 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<std::mutex> 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<ImageLoaderData*>(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<std::mutex> 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);
}
}
}