Skip to content

Commit

Permalink
HnTextureRegistry: recycle texture IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 19, 2024
1 parent c872a2c commit 428e277
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Hydrogent/include/HnTextureRegistry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ class HnTextureRegistry final : public std::enable_shared_from_this<HnTextureReg
std::mutex m_AsyncTasksMtx;
std::vector<RefCntAutoPtr<IAsyncTask>> m_AsyncTasks;

std::mutex m_RecycledTextureIdsMtx;
std::vector<Uint32> m_RecycledTextureIds;

std::atomic<Uint32> m_NextTextureId{0};
std::atomic<Int32> m_NumTexturesLoading{0};
std::atomic<Int64> m_LoadingTexDataSize{0};
Expand Down
21 changes: 20 additions & 1 deletion Hydrogent/src/HnTextureRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,21 @@ HnTextureRegistry::TextureHandleSharedPtr HnTextureRegistry::Allocate(const pxr:
[&]() {
m_NumTexturesLoading.fetch_add(+1);

std::shared_ptr<TextureHandle> TexHandle = std::make_shared<TextureHandle>(*this, m_NextTextureId.fetch_add(1));
Uint32 TextureId = 0;
{
std::lock_guard<std::mutex> 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<TextureHandle> TexHandle = std::make_shared<TextureHandle>(*this, TextureId);

if (IsAsync && m_pThreadPool)
{
Expand Down Expand Up @@ -416,6 +430,11 @@ Uint32 HnTextureRegistry::GetDataVersion() const

void HnTextureRegistry::OnHandleDestroyed(const TextureHandle& Handle)
{
{
std::lock_guard<std::mutex> Guard{m_RecycledTextureIdsMtx};
m_RecycledTextureIds.push_back(Handle.GetId());
}

if (Handle.m_pAtlasSuballocation)
{
m_AtlasDataSize.fetch_add(-static_cast<Int64>(Handle.DataSize));
Expand Down

0 comments on commit 428e277

Please sign in to comment.