From d50d08bd494764564f8f4d516667acf3daf09d30 Mon Sep 17 00:00:00 2001 From: assiduous Date: Tue, 1 Dec 2020 19:48:27 -0800 Subject: GLTFResourceManager: added texture allocation cache --- AssetLoader/src/GLTFResourceManager.cpp | 52 +++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) (limited to 'AssetLoader/src/GLTFResourceManager.cpp') diff --git a/AssetLoader/src/GLTFResourceManager.cpp b/AssetLoader/src/GLTFResourceManager.cpp index 05dfa28..dc4febf 100644 --- a/AssetLoader/src/GLTFResourceManager.cpp +++ b/AssetLoader/src/GLTFResourceManager.cpp @@ -67,6 +67,8 @@ IBuffer* GLTFResourceManager::BufferCache::GetBuffer(IRenderDevice* pDevice, IDe RefCntAutoPtr GLTFResourceManager::BufferCache::Allocate(Uint32 Size, Uint32 Alignment) { + VERIFY_EXPR(Size > 0 && IsPowerOfTwo(Alignment)); + std::lock_guard Lock{m_Mtx}; auto Region = m_Mgr.Allocate(Size, Alignment); @@ -111,12 +113,11 @@ ITexture* GLTFResourceManager::TextureCache::GetTexture(IRenderDevice* pDevice, RefCntAutoPtr GLTFResourceManager::TextureCache::Allocate(Uint32 Width, Uint32 Height) { - Width = (Width + m_Granularity - 1) / m_Granularity; - Height = (Height + m_Granularity - 1) / m_Granularity; + VERIFY_EXPR(Width > 0 && Height > 0); std::lock_guard Lock{m_Mtx}; - auto Region = m_Mgr.Allocate(Width, Height); + auto Region = m_Mgr.Allocate((Width + m_Granularity - 1) / m_Granularity, (Height + m_Granularity - 1) / m_Granularity); if (!Region.IsEmpty()) { Region.x *= m_Granularity; @@ -128,6 +129,8 @@ RefCntAutoPtr GLTFResourceManager::Textu MakeNewRCObj()( RefCntAutoPtr{&m_Owner}, *this, + Width, + Height, std::move(Region) // ) // }; @@ -181,4 +184,47 @@ GLTFResourceManager::GLTFResourceManager(IReferenceCounters* pRefCounters, } } +RefCntAutoPtr GLTFResourceManager::FindAllocation(const char* CacheId) +{ + RefCntAutoPtr pAllocation; + + std::lock_guard Lock{m_AllocationsMtx}; + + auto it = m_Allocations.find(CacheId); + if (it != m_Allocations.end()) + { + pAllocation = it->second.Lock(); + if (!pAllocation) + m_Allocations.erase(it); + } + + return pAllocation; +} + +RefCntAutoPtr GLTFResourceManager::AllocateTextureSpace( + Uint32 TextureIndex, + Uint32 Width, + Uint32 Height, + const char* CacheId) +{ + RefCntAutoPtr pAllocation; + if (CacheId != nullptr && *CacheId != 0) + { + pAllocation = FindAllocation(CacheId); + } + + if (!pAllocation) + { + pAllocation = m_Textures[TextureIndex].Allocate(Width, Height); + } + + if (CacheId != nullptr && *CacheId != 0) + { + auto inserted = m_Allocations.emplace(CacheId, pAllocation).second; + VERIFY_EXPR(inserted); + } + + return pAllocation; +} + } // namespace Diligent -- cgit v1.2.3