diff --git a/Hydrogent/include/HnTextureRegistry.hpp b/Hydrogent/include/HnTextureRegistry.hpp index 72a48cbe..c462e2c5 100644 --- a/Hydrogent/include/HnTextureRegistry.hpp +++ b/Hydrogent/include/HnTextureRegistry.hpp @@ -221,6 +221,9 @@ class HnTextureRegistry final : public std::enable_shared_from_this> m_AsyncTasks; + std::mutex m_RecycledTextureIdsMtx; + std::vector m_RecycledTextureIds; + std::atomic m_NextTextureId{0}; std::atomic m_NumTexturesLoading{0}; std::atomic m_LoadingTexDataSize{0}; diff --git a/Hydrogent/src/HnTextureRegistry.cpp b/Hydrogent/src/HnTextureRegistry.cpp index fb9fd2da..9654d77e 100644 --- a/Hydrogent/src/HnTextureRegistry.cpp +++ b/Hydrogent/src/HnTextureRegistry.cpp @@ -339,7 +339,21 @@ HnTextureRegistry::TextureHandleSharedPtr HnTextureRegistry::Allocate(const pxr: [&]() { m_NumTexturesLoading.fetch_add(+1); - std::shared_ptr TexHandle = std::make_shared(*this, m_NextTextureId.fetch_add(1)); + Uint32 TextureId = 0; + { + std::lock_guard Lock{m_RecycledTextureIdsMtx}; + if (!m_RecycledTextureIds.empty()) + { + TextureId = m_RecycledTextureIds.back(); + m_RecycledTextureIds.pop_back(); + } + else + { + TextureId = m_NextTextureId.fetch_add(1); + } + } + + std::shared_ptr TexHandle = std::make_shared(*this, TextureId); if (IsAsync && m_pThreadPool) { @@ -416,6 +430,11 @@ Uint32 HnTextureRegistry::GetDataVersion() const void HnTextureRegistry::OnHandleDestroyed(const TextureHandle& Handle) { + { + std::lock_guard Guard{m_RecycledTextureIdsMtx}; + m_RecycledTextureIds.push_back(Handle.GetId()); + } + if (Handle.m_pAtlasSuballocation) { m_AtlasDataSize.fetch_add(-static_cast(Handle.DataSize));