diff options
| author | assiduous <assiduous@diligentgraphics.com> | 2020-11-30 19:21:07 +0000 |
|---|---|---|
| committer | assiduous <assiduous@diligentgraphics.com> | 2020-11-30 19:21:07 +0000 |
| commit | 70129fda4c75f7b8e6812190631e35a56eb2b9d2 (patch) | |
| tree | 68e903d3a6bd869277174e727c1d321107aa9327 /AssetLoader/interface | |
| parent | GLTF Loader: added texture coordinate scale and bias for use in texture atlas (diff) | |
| download | DiligentTools-70129fda4c75f7b8e6812190631e35a56eb2b9d2.tar.gz DiligentTools-70129fda4c75f7b8e6812190631e35a56eb2b9d2.zip | |
GLTF loader: initial implementation of GLTF resource cache
Diffstat (limited to 'AssetLoader/interface')
| -rw-r--r-- | AssetLoader/interface/GLTFLoader.hpp | 55 | ||||
| -rw-r--r-- | AssetLoader/interface/GLTFResourceManager.hpp | 162 |
2 files changed, 210 insertions, 7 deletions
diff --git a/AssetLoader/interface/GLTFLoader.hpp b/AssetLoader/interface/GLTFLoader.hpp index 16a95b5..21c1cf4 100644 --- a/AssetLoader/interface/GLTFLoader.hpp +++ b/AssetLoader/interface/GLTFLoader.hpp @@ -37,6 +37,7 @@ #include "../../../DiligentCore/Graphics/GraphicsEngine/interface/DeviceContext.h" #include "../../../DiligentCore/Common/interface/RefCntAutoPtr.hpp" #include "../../../DiligentCore/Common/interface/AdvancedMath.hpp" +#include "GLTFResourceManager.hpp" namespace tinygltf { @@ -52,6 +53,17 @@ namespace Diligent namespace GLTF { +struct GLTFTextureUpdateData; + +struct GLTFCacheInfo +{ + GLTFResourceManager* pResourceMgr = nullptr; + + Uint8 IndexBufferIdx = 0; + Uint8 VertexBuffer0Idx = 0; + Uint8 VertexBuffer1Idx = 0; +}; + struct Material { Material() noexcept {} @@ -100,8 +112,9 @@ struct Material { RefCntAutoPtr<ITexture> pSpecularGlossinessTexture; RefCntAutoPtr<ITexture> pDiffuseTexture; - float4 DiffuseFactor = float4(1.0f, 1.0f, 1.0f, 1.0f); - float3 SpecularFactor = float3(1.0f, 1.0f, 1.0f); + + float4 DiffuseFactor = float4(1.0f, 1.0f, 1.0f, 1.0f); + float3 SpecularFactor = float3(1.0f, 1.0f, 1.0f); }; Extension extension; @@ -264,6 +277,9 @@ struct Model RefCntAutoPtr<IBuffer> pIndexBuffer; Uint32 IndexCount = 0; + GLTFResourceManager::BufferAllocation VBAllocations[2]; + GLTFResourceManager::BufferAllocation IBAllocation; + /// Transformation matrix that transforms unit cube [0,1]x[0,1]x[0,1] into /// axis-aligned bounding box in model space. float4x4 AABBTransform; @@ -273,7 +289,15 @@ struct Model std::vector<std::unique_ptr<Skin>> Skins; - std::vector<RefCntAutoPtr<ITexture>> Textures; + struct TextureInfo + { + RefCntAutoPtr<ITexture> pTexture; + GLTFResourceManager::TextureAllocation CacheAllocation; + float4 UVScaleBias{1, 1, 0, 0}; + std::unique_ptr<GLTFTextureUpdateData> UpdateData; + }; + std::vector<TextureInfo> Textures; + std::vector<RefCntAutoPtr<ISampler>> TextureSamplers; std::vector<Material> Materials; std::vector<Animation> Animations; @@ -289,13 +313,27 @@ struct Model { std::mutex TexturesMtx; - std::unordered_map<std::string, RefCntWeakPtr<ITexture>> Textures; + struct TextureInfo + { + RefCntWeakPtr<ITexture> wpTexture; + + const float4 UVScaleBias; + + TextureInfo(ITexture* pTex, const float4& _UVScaleBias) : + wpTexture{pTex}, + UVScaleBias{_UVScaleBias} + {} + }; + std::unordered_map<std::string, TextureInfo> Textures; }; Model(IRenderDevice* pDevice, IDeviceContext* pContext, const std::string& filename, - TextureCacheType* pTextureCache = nullptr); + TextureCacheType* pTextureCache = nullptr, + GLTFCacheInfo* pCache = nullptr); + + ~Model(); void UpdateAnimation(Uint32 index, float time); @@ -305,7 +343,8 @@ private: void LoadFromFile(IRenderDevice* pDevice, IDeviceContext* pContext, const std::string& filename, - TextureCacheType* pTextureCache); + TextureCacheType* pTextureCache, + GLTFCacheInfo* pCache); void LoadNode(IRenderDevice* pDevice, Node* parent, @@ -319,7 +358,6 @@ private: void LoadSkins(const tinygltf::Model& gltf_model); void LoadTextures(IRenderDevice* pDevice, - IDeviceContext* pCtx, const tinygltf::Model& gltf_model, const std::string& BaseDir, TextureCacheType* pTextureCache); @@ -332,7 +370,10 @@ private: Node* FindNode(Node* parent, Uint32 index); Node* NodeFromIndex(uint32_t index); + GLTFCacheInfo CacheInfo; + bool TexturesTransitioned = false; + bool TexturesUpdated = false; }; } // namespace GLTF diff --git a/AssetLoader/interface/GLTFResourceManager.hpp b/AssetLoader/interface/GLTFResourceManager.hpp new file mode 100644 index 0000000..520a149 --- /dev/null +++ b/AssetLoader/interface/GLTFResourceManager.hpp @@ -0,0 +1,162 @@ +/* + * Copyright 2019-2020 Diligent Graphics LLC + * Copyright 2015-2019 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#pragma once + +#include <mutex> +#include <vector> + +#include "../../../DiligentCore/Graphics/GraphicsEngine/interface/RenderDevice.h" +#include "../../../DiligentCore/Graphics/GraphicsEngine/interface/DeviceContext.h" +#include "../../../DiligentCore/Common/interface/RefCntAutoPtr.hpp" +#include "../../../DiligentCore/Common/interface/ObjectBase.hpp" +#include "../../../DiligentCore/Graphics/GraphicsAccessories/interface/VariableSizeAllocationsManager.hpp" +#include "../../../DiligentCore/Graphics/GraphicsAccessories/interface/DynamicAtlasManager.hpp" + + +namespace Diligent +{ + +/// GLTF resource manager +class GLTFResourceManager : public ObjectBase<IObject> +{ +public: + using TBase = ObjectBase<IObject>; + + struct BufferAllocation + { + Uint32 BufferIndex = 0; + VariableSizeAllocationsManager::Allocation Region; + + bool IsValid() const + { + return Region.IsValid(); + } + }; + struct TextureAllocation + { + Uint32 TextureIndex = 0; + DynamicAtlasManager::Region Region; + + bool IsValid() const + { + return !Region.IsEmpty(); + } + }; + + struct TextureCacheAttribs + { + TextureDesc Desc; + Uint32 Granularity = 128; + }; + struct CreateInfo + { + const BufferDesc* Buffers = nullptr; // [NumBuffers] + Uint32 NumBuffers = 0; + + const TextureCacheAttribs* Textures = nullptr; // [NumTextures] + Uint32 NumTextures = 0; + }; + + 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(Uint32 Idx) + { + return m_Buffers[Idx].pBuffer; + } + + TextureAllocation AllocateTextureSpace(Uint32 TextureIndex, Uint32 Width, Uint32 Height); + + void FreeTextureSpace(TextureAllocation&& Allocation); + + ITexture* GetTexture(Uint32 Idx) + { + return m_Textures[Idx].pTexture; + } + +private: + template <typename AllocatorType, typename ObjectType> + friend class MakeNewRCObj; + + GLTFResourceManager(IReferenceCounters* pRefCounters, + IRenderDevice* pDevice, + const CreateInfo& CI); + + struct BufferCache + { + BufferCache(IRenderDevice* pDevice, const BufferDesc& BuffDesc); + + // clang-format off + BufferCache (const BufferCache&) = delete; + BufferCache& operator=(const BufferCache&) = delete; + BufferCache& operator=(BufferCache&&) = delete; + // clang-format on + + BufferCache(BufferCache&& Cache) noexcept : + Mgr{std::move(Cache.Mgr)}, + pBuffer{std::move(Cache.pBuffer)} + {} + + std::mutex Mtx; + VariableSizeAllocationsManager Mgr; + RefCntAutoPtr<IBuffer> pBuffer; + }; + std::vector<BufferCache> m_Buffers; + + struct TextureCache + { + TextureCache(IRenderDevice* pDevice, const TextureCacheAttribs& CacheCI); + + // clang-format off + TextureCache (const TextureCache&) = delete; + TextureCache& operator=(const TextureCache&) = delete; + TextureCache& operator=(TextureCache&&) = delete; + // clang-format on + + TextureCache(TextureCache&& Cache) noexcept : + Granularity{Cache.Granularity}, + Mgr{std::move(Cache.Mgr)}, + pTexture{std::move(Cache.pTexture)} + {} + + const Uint32 Granularity; + + std::mutex Mtx; + DynamicAtlasManager Mgr; + RefCntAutoPtr<ITexture> pTexture; + }; + std::vector<TextureCache> m_Textures; +}; + +} // namespace Diligent |
