From e3bad5a7fcedc170a10aed3ee644310ec8c982fe Mon Sep 17 00:00:00 2001 From: assiduous Date: Sat, 5 Dec 2020 09:19:28 -0800 Subject: GLTF Resource manager: implemented texture array resizing --- AssetLoader/interface/GLTFLoader.hpp | 12 ++++ AssetLoader/interface/GLTFResourceManager.hpp | 96 ++++++++++++++++++++++----- 2 files changed, 91 insertions(+), 17 deletions(-) (limited to 'AssetLoader/interface') diff --git a/AssetLoader/interface/GLTFLoader.hpp b/AssetLoader/interface/GLTFLoader.hpp index ddf5625..68747c5 100644 --- a/AssetLoader/interface/GLTFLoader.hpp +++ b/AssetLoader/interface/GLTFLoader.hpp @@ -61,6 +61,12 @@ struct GLTFCacheInfo Uint8 IndexBufferIdx = 0; Uint8 VertexBuffer0Idx = 0; Uint8 VertexBuffer1Idx = 0; + + TEXTURE_FORMAT BaseColorFormat = TEX_FORMAT_RGBA8_UNORM; + TEXTURE_FORMAT PhysicalDescFormat = TEX_FORMAT_RGBA8_UNORM; + TEXTURE_FORMAT NormalFormat = TEX_FORMAT_RGBA8_UNORM; + TEXTURE_FORMAT OcclusionFormat = TEX_FORMAT_RGBA8_UNORM; + TEXTURE_FORMAT EmissiveFormat = TEX_FORMAT_RGBA8_UNORM; }; struct Material @@ -388,6 +394,11 @@ struct Model return Textures[Index].UVScaleBias; } + float GetSlice(Uint32 Index) + { + return Textures[Index].Slice; + } + private: void LoadFromFile(IRenderDevice* pDevice, IDeviceContext* pContext, @@ -433,6 +444,7 @@ private: { RefCntAutoPtr pTexture; float4 UVScaleBias{1, 1, 0, 0}; + float Slice = 0; RefCntAutoPtr pCacheAllocation; diff --git a/AssetLoader/interface/GLTFResourceManager.hpp b/AssetLoader/interface/GLTFResourceManager.hpp index e0b007d..a6c2a82 100644 --- a/AssetLoader/interface/GLTFResourceManager.hpp +++ b/AssetLoader/interface/GLTFResourceManager.hpp @@ -30,6 +30,7 @@ #include #include #include +#include #include "../../../DiligentCore/Graphics/GraphicsEngine/interface/RenderDevice.h" #include "../../../DiligentCore/Graphics/GraphicsEngine/interface/DeviceContext.h" @@ -97,6 +98,7 @@ public: TextureCache& ParentCache, Uint32 Width, Uint32 Height, + Uint32 Slice, DynamicAtlasManager::Region&& Region) : // clang-format off ObjectBase{pRefCounters}, @@ -104,6 +106,7 @@ public: m_ParentCache {ParentCache}, m_Width {Width}, m_Height {Height}, + m_Slice {Slice}, m_Region {std::move(Region)} // clang-format on { @@ -112,7 +115,7 @@ public: ~TextureAllocation() { - m_ParentCache.FreeAllocation(std::move(m_Region)); + m_ParentCache.FreeAllocation(m_Slice, std::move(m_Region)); } ITexture* GetTexture(IRenderDevice* pDevice, IDeviceContext* pContext) const @@ -132,19 +135,24 @@ public: Uint32 GetWidth() const { return m_Width; } Uint32 GetHeight() const { return m_Height; } + Uint32 GetSlice() const { return m_Slice; } private: RefCntAutoPtr m_pResMgr; TextureCache& m_ParentCache; const Uint32 m_Width; const Uint32 m_Height; + const Uint32 m_Slice; DynamicAtlasManager::Region m_Region; }; struct TextureCacheAttribs { TextureDesc Desc; - Uint32 Granularity = 128; + + Uint32 Granularity = 128; + Uint32 ExtraSliceCount = 2; + Uint32 MaxSlices = 2048; }; struct CreateInfo { @@ -153,6 +161,10 @@ public: const TextureCacheAttribs* Textures = nullptr; // [NumTextures] Uint32 NumTextures = 0; + + TextureDesc DefaultTexDesc; + + Uint32 DefaultExtraSliceCount = 1; }; static RefCntAutoPtr Create(IRenderDevice* pDevice, @@ -163,10 +175,32 @@ public: return m_Buffers[BufferIndex].Allocate(Size, Alignment); } - RefCntAutoPtr AllocateTextureSpace(Uint32 TextureIndex, Uint32 Width, Uint32 Height, const char* CacheId = nullptr); + RefCntAutoPtr AllocateTextureSpace(TEXTURE_FORMAT Fmt, Uint32 Width, Uint32 Height, const char* CacheId = nullptr); RefCntAutoPtr FindAllocation(const char* CacheId); + Uint32 GetResourceVersion() const + { + return m_ResourceVersion.load(); + } + + IBuffer* GetBuffer(Uint32 Index, IRenderDevice* pDevice, IDeviceContext* pContext) + { + return m_Buffers[Index].GetBuffer(pDevice, pContext); + } + ITexture* GetTexture(TEXTURE_FORMAT Fmt, IRenderDevice* pDevice, IDeviceContext* pContext) + { + decltype(m_Textures)::iterator cache_it; // NB: can't initialize it without locking the mutex + { + std::lock_guard Lock{m_TexturesMtx}; + cache_it = m_Textures.find(Fmt); + if (cache_it == m_Textures.end()) + return nullptr; + } + + return cache_it->second.GetTexture(pDevice, pContext); + } + private: template friend class MakeNewRCObj; @@ -227,44 +261,72 @@ private: TextureCache(TextureCache&& Cache) noexcept : // 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)} + m_Owner {Cache.m_Owner}, + m_Attribs {Cache.m_Attribs}, + m_TexName {std::move(m_TexName)}, + m_Slices {std::move(Cache.m_Slices)}, + m_pTexture{std::move(Cache.m_pTexture)} // clang-format on { - m_TexDesc.Name = m_TexName.c_str(); + m_Attribs.Desc.Name = m_TexName.c_str(); } ITexture* GetTexture(IRenderDevice* pDevice, IDeviceContext* pContext); const TextureDesc& GetTexDesc() const { - return m_TexDesc; + return m_Attribs.Desc; } RefCntAutoPtr Allocate(Uint32 Width, Uint32 Height); - void FreeAllocation(DynamicAtlasManager::Region&& Allocation); + void FreeAllocation(Uint32 Slice, DynamicAtlasManager::Region&& Allocation); private: GLTFResourceManager& m_Owner; - TextureDesc m_TexDesc; + TextureCacheAttribs m_Attribs; + std::string m_TexName; - const Uint32 m_Granularity; + struct SliceManager + { + SliceManager(Uint32 Width, Uint32 Height) : + Mgr{Width, Height} + {} + + DynamicAtlasManager::Region Allocate(Uint32 Width, Uint32 Height) + { + std::lock_guard Lock{Mtx}; + return Mgr.Allocate(Width, Height); + } + void Free(DynamicAtlasManager::Region&& Region) + { + std::lock_guard Lock{Mtx}; + Mgr.Free(std::move(Region)); + } + + private: + std::mutex Mtx; + DynamicAtlasManager Mgr; + }; + std::mutex m_SlicesMtx; + std::vector> m_Slices; - std::mutex m_Mtx; - DynamicAtlasManager m_Mgr; RefCntAutoPtr m_pTexture; }; - std::vector m_Textures; + + TextureDesc m_DefaultTexDesc; + const Uint32 m_DefaultExtraSliceCount; + + + std::mutex m_TexturesMtx; + std::unordered_map m_Textures; std::mutex m_AllocationsMtx; std::unordered_map> m_Allocations; + + std::atomic_uint32_t m_ResourceVersion = {}; }; } // namespace Diligent -- cgit v1.2.3