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 3bc693a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Hydrogent/include/HnTextureRegistry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class HnTextureRegistry final : public std::enable_shared_from_this<HnTextureReg
std::function<RefCntAutoPtr<ITextureLoader>()> CreateLoader,
std::shared_ptr<TextureHandle> TexHandle);

void OnHandleDestroyed(const TextureHandle& Handle);
void OnDestroyHandle(const TextureHandle& Handle);

private:
RefCntAutoPtr<IRenderDevice> m_pDevice;
Expand Down 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
25 changes: 22 additions & 3 deletions Hydrogent/src/HnTextureRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ HnTextureRegistry::TextureHandle::~TextureHandle()
{
if (auto Registry = m_Registry.lock())
{
Registry->OnHandleDestroyed(*this);
Registry->OnDestroyHandle(*this);
}
else
{
Expand Down 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 @@ -414,8 +428,13 @@ Uint32 HnTextureRegistry::GetDataVersion() const
return m_DataVersion.load();
}

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

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

0 comments on commit 3bc693a

Please sign in to comment.