diff options
| author | assiduous <assiduous@diligentgraphics.com> | 2020-12-02 02:13:42 +0000 |
|---|---|---|
| committer | assiduous <assiduous@diligentgraphics.com> | 2020-12-02 02:13:42 +0000 |
| commit | d81817038a73aac29846928ebd1fcd8d8ace0f60 (patch) | |
| tree | d9fa236af4332c1cc4191c9e480bed6928761799 /AssetLoader/interface | |
| parent | GLTF Loader: added GetFirstIndexLocation and GetBaseVertex methods (diff) | |
| download | DiligentTools-d81817038a73aac29846928ebd1fcd8d8ace0f60.tar.gz DiligentTools-d81817038a73aac29846928ebd1fcd8d8ace0f60.zip | |
Refcactored GLTFResourceManager: made allocations ref-counted objects
Diffstat (limited to 'AssetLoader/interface')
| -rw-r--r-- | AssetLoader/interface/GLTFLoader.hpp | 35 | ||||
| -rw-r--r-- | AssetLoader/interface/GLTFResourceManager.hpp | 182 |
2 files changed, 154 insertions, 63 deletions
diff --git a/AssetLoader/interface/GLTFLoader.hpp b/AssetLoader/interface/GLTFLoader.hpp index 1632bb0..e66e808 100644 --- a/AssetLoader/interface/GLTFLoader.hpp +++ b/AssetLoader/interface/GLTFLoader.hpp @@ -340,16 +340,16 @@ struct Model void UpdateAnimation(Uint32 index, float time); - void PrepareGPUResources(IDeviceContext* pCtx); + void PrepareGPUResources(IRenderDevice* pDevice, IDeviceContext* pCtx); - IBuffer* GetBuffer(BUFFER_ID BuffId) + IBuffer* GetBuffer(BUFFER_ID BuffId, IRenderDevice* pDevice, IDeviceContext* pCtx) { VERIFY_EXPR(BuffId < BUFFER_ID_NUM_BUFFERS); auto& Buff = Buffers[BuffId]; if (Buff.pBuffer) return Buff.pBuffer; - else if (Buff.CacheAllocation.IsValid()) - return CacheInfo.pResourceMgr->GetBuffer(Buff.CacheAllocation); + else if (Buff.pCacheAllocation) + return Buff.pCacheAllocation->GetBuffer(pDevice, pCtx); else return nullptr; } @@ -357,30 +357,30 @@ struct Model Uint32 GetFirstIndexLocation() const { auto& IndBuff = Buffers[BUFFER_ID_INDEX]; - VERIFY(!IndBuff.CacheAllocation.IsValid() || IndBuff.CacheAllocation.Region.UnalignedOffset % sizeof(Uint32) == 0, + VERIFY(!IndBuff.pCacheAllocation || IndBuff.pCacheAllocation->GetRegion().UnalignedOffset % sizeof(Uint32) == 0, "Allocation offset is not multiple of sizeof(Uint32)"); - return IndBuff.CacheAllocation.IsValid() ? - static_cast<Uint32>(IndBuff.CacheAllocation.Region.UnalignedOffset / sizeof(Uint32)) : + return IndBuff.pCacheAllocation ? + static_cast<Uint32>(IndBuff.pCacheAllocation->GetRegion().UnalignedOffset / sizeof(Uint32)) : 0; } Uint32 GetBaseVertex() const { auto& VertBuff = Buffers[BUFFER_ID_VERTEX0]; - VERIFY(!VertBuff.CacheAllocation.IsValid() || VertBuff.CacheAllocation.Region.UnalignedOffset % sizeof(VertexAttribs0) == 0, + VERIFY(!VertBuff.pCacheAllocation || VertBuff.pCacheAllocation->GetRegion().UnalignedOffset % sizeof(VertexAttribs0) == 0, "Allocation offset is not multiple of sizeof(VertexAttribs0)"); - return VertBuff.CacheAllocation.IsValid() ? - static_cast<Uint32>(VertBuff.CacheAllocation.Region.UnalignedOffset / sizeof(VertexAttribs0)) : + return VertBuff.pCacheAllocation ? + static_cast<Uint32>(VertBuff.pCacheAllocation->GetRegion().UnalignedOffset / sizeof(VertexAttribs0)) : 0; } - ITexture* GetTexture(Uint32 Index) + ITexture* GetTexture(Uint32 Index, IRenderDevice* pDevice, IDeviceContext* pCtx) { auto& TexInfo = Textures[Index]; if (TexInfo.pTexture) return TexInfo.pTexture; - else if (TexInfo.CacheAllocation.IsValid()) - return CacheInfo.pResourceMgr->GetTexture(TexInfo.CacheAllocation); + else if (TexInfo.pCacheAllocation) + return TexInfo.pCacheAllocation->GetTexture(pDevice, pCtx); else return nullptr; } @@ -427,15 +427,16 @@ private: { RefCntAutoPtr<IBuffer> pBuffer; - GLTFResourceManager::BufferAllocation CacheAllocation; + RefCntAutoPtr<GLTFResourceManager::BufferAllocation> pCacheAllocation; }; std::array<BufferInfo, BUFFER_ID_NUM_BUFFERS> Buffers; struct TextureInfo { - RefCntAutoPtr<ITexture> pTexture; - GLTFResourceManager::TextureAllocation CacheAllocation; - float4 UVScaleBias{1, 1, 0, 0}; + RefCntAutoPtr<ITexture> pTexture; + float4 UVScaleBias{1, 1, 0, 0}; + + RefCntAutoPtr<GLTFResourceManager::TextureAllocation> pCacheAllocation; }; std::vector<TextureInfo> Textures; }; diff --git a/AssetLoader/interface/GLTFResourceManager.hpp b/AssetLoader/interface/GLTFResourceManager.hpp index d8c9074..9a8397c 100644 --- a/AssetLoader/interface/GLTFResourceManager.hpp +++ b/AssetLoader/interface/GLTFResourceManager.hpp @@ -42,34 +42,93 @@ namespace Diligent { /// GLTF resource manager -class GLTFResourceManager : public ObjectBase<IObject> +class GLTFResourceManager final : public ObjectBase<IObject> { + class BufferCache; + class TextureCache; + public: using TBase = ObjectBase<IObject>; - struct BufferAllocation + class BufferAllocation final : public ObjectBase<IObject> { - Int32 BufferIndex = -1; + public: + BufferAllocation(IReferenceCounters* pRefCounters, + RefCntAutoPtr<GLTFResourceManager> pResourceMg, + BufferCache& ParentCache, + VariableSizeAllocationsManager::Allocation&& Region) : + // clang-format off + ObjectBase<IObject>{pRefCounters}, + m_pResMgr {std::move(pResourceMg)}, + m_ParentCache {ParentCache}, + m_Region {std::move(Region)} + // clang-format on + { + VERIFY_EXPR(m_Region.IsValid()); + } + + ~BufferAllocation() + { + m_ParentCache.FreeAllocation(std::move(m_Region)); + } - VariableSizeAllocationsManager::Allocation Region; + IBuffer* GetBuffer(IRenderDevice* pDevice, IDeviceContext* pContext) const + { + return m_ParentCache.GetBuffer(pDevice, pContext); + } - bool IsValid() const + const VariableSizeAllocationsManager::Allocation& GetRegion() const { - VERIFY_EXPR(BufferIndex >= 0 && Region.IsValid() || BufferIndex < 0 && !Region.IsValid()); - return BufferIndex >= 0; + return m_Region; } + + private: + RefCntAutoPtr<GLTFResourceManager> m_pResMgr; + BufferCache& m_ParentCache; + VariableSizeAllocationsManager::Allocation m_Region; }; - struct TextureAllocation + + class TextureAllocation final : public ObjectBase<IObject> { - Int32 TextureIndex = -1; + public: + TextureAllocation(IReferenceCounters* pRefCounters, + RefCntAutoPtr<GLTFResourceManager> pResourceMg, + TextureCache& ParentCache, + DynamicAtlasManager::Region&& Region) : + // clang-format off + ObjectBase<IObject>{pRefCounters}, + m_pResMgr {std::move(pResourceMg)}, + m_ParentCache {ParentCache}, + m_Region {std::move(Region)} + // clang-format on + { + VERIFY_EXPR(!m_Region.IsEmpty()); + } + + ~TextureAllocation() + { + m_ParentCache.FreeAllocation(std::move(m_Region)); + } + + ITexture* GetTexture(IRenderDevice* pDevice, IDeviceContext* pContext) const + { + return m_ParentCache.GetTexture(pDevice, pContext); + } - DynamicAtlasManager::Region Region; + const TextureDesc& GetTexDesc() const + { + return m_ParentCache.GetTexDesc(); + } - bool IsValid() const + const DynamicAtlasManager::Region& GetRegion() { - VERIFY_EXPR(TextureIndex >= 0 && !Region.IsEmpty() || TextureIndex < 0 && Region.IsEmpty()); - return TextureIndex >= 0; + return m_Region; } + + private: + RefCntAutoPtr<GLTFResourceManager> m_pResMgr; + TextureCache& m_ParentCache; + DynamicAtlasManager::Region m_Region; }; struct TextureCacheAttribs @@ -89,26 +148,14 @@ public: static RefCntAutoPtr<GLTFResourceManager> Create(IRenderDevice* pDevice, const CreateInfo& CI); - - /// Allocates space in the buffer - BufferAllocation AllocateBufferSpace(Uint32 BufferIndex, Uint32 Size, Uint32 Alignment); - - void FreeBufferSpace(BufferAllocation&& Allocation); - - IBuffer* GetBuffer(const BufferAllocation& Allocation) + RefCntAutoPtr<BufferAllocation> AllocateBufferSpace(Uint32 BufferIndex, Uint32 Size, Uint32 Alignment) { - VERIFY_EXPR(Allocation.IsValid()); - return m_Buffers[Allocation.BufferIndex].pBuffer; + return m_Buffers[BufferIndex].Allocate(Size, Alignment); } - TextureAllocation AllocateTextureSpace(Uint32 TextureIndex, Uint32 Width, Uint32 Height); - - void FreeTextureSpace(TextureAllocation&& Allocation); - - ITexture* GetTexture(const TextureAllocation& Allocation) + RefCntAutoPtr<TextureAllocation> AllocateTextureSpace(Uint32 TextureIndex, Uint32 Width, Uint32 Height) { - VERIFY_EXPR(Allocation.IsValid()); - return m_Textures[Allocation.TextureIndex].pTexture; + return m_Textures[TextureIndex].Allocate(Width, Height); } private: @@ -119,9 +166,10 @@ private: IRenderDevice* pDevice, const CreateInfo& CI); - struct BufferCache + class BufferCache { - BufferCache(IRenderDevice* pDevice, const BufferDesc& BuffDesc); + public: + BufferCache(GLTFResourceManager& Owner, IRenderDevice* pDevice, const BufferDesc& BuffDesc); // clang-format off BufferCache (const BufferCache&) = delete; @@ -130,19 +178,37 @@ private: // clang-format on BufferCache(BufferCache&& Cache) noexcept : - Mgr{std::move(Cache.Mgr)}, - pBuffer{std::move(Cache.pBuffer)} + // clang-format off + m_Owner {Cache.m_Owner}, + m_Mgr {std::move(Cache.m_Mgr)}, + m_pBuffer{std::move(Cache.m_pBuffer)} + // clang-format on {} - std::mutex Mtx; - VariableSizeAllocationsManager Mgr; - RefCntAutoPtr<IBuffer> pBuffer; + IBuffer* GetBuffer(IRenderDevice* pDevice, IDeviceContext* pContext); + + RefCntAutoPtr<BufferAllocation> Allocate(Uint32 Size, Uint32 Alignment); + + void FreeAllocation(VariableSizeAllocationsManager::Allocation&& Allocation) + { + std::lock_guard<std::mutex> Lock{m_Mtx}; + m_Mgr.Free(std::move(Allocation)); + } + + private: + GLTFResourceManager& m_Owner; + + std::mutex m_Mtx; + VariableSizeAllocationsManager m_Mgr; + RefCntAutoPtr<IBuffer> m_pBuffer; }; std::vector<BufferCache> m_Buffers; - struct TextureCache + + class TextureCache { - TextureCache(IRenderDevice* pDevice, const TextureCacheAttribs& CacheCI); + public: + TextureCache(GLTFResourceManager& Owner, IRenderDevice* pDevice, const TextureCacheAttribs& CacheCI); // clang-format off TextureCache (const TextureCache&) = delete; @@ -151,16 +217,40 @@ private: // clang-format on TextureCache(TextureCache&& Cache) noexcept : - Granularity{Cache.Granularity}, - Mgr{std::move(Cache.Mgr)}, - pTexture{std::move(Cache.pTexture)} - {} + // clang-format off + m_Owner {Cache.m_Owner}, + m_TexDesc {m_TexDesc}, + m_TexName {std::move(m_TexName)}, + m_Granularity{Cache.m_Granularity}, + m_Mgr {std::move(Cache.m_Mgr)}, + m_pTexture {std::move(Cache.m_pTexture)} + // clang-format on + { + m_TexDesc.Name = m_TexName.c_str(); + } + + ITexture* GetTexture(IRenderDevice* pDevice, IDeviceContext* pContext); + + const TextureDesc& GetTexDesc() const + { + return m_TexDesc; + } + + RefCntAutoPtr<TextureAllocation> Allocate(Uint32 Width, Uint32 Height); + + void FreeAllocation(DynamicAtlasManager::Region&& Allocation); + + private: + GLTFResourceManager& m_Owner; + + TextureDesc m_TexDesc; + std::string m_TexName; - const Uint32 Granularity; + const Uint32 m_Granularity; - std::mutex Mtx; - DynamicAtlasManager Mgr; - RefCntAutoPtr<ITexture> pTexture; + std::mutex m_Mtx; + DynamicAtlasManager m_Mgr; + RefCntAutoPtr<ITexture> m_pTexture; }; std::vector<TextureCache> m_Textures; }; |
