summaryrefslogtreecommitdiffstats
path: root/AssetLoader
diff options
context:
space:
mode:
authorassiduous <assiduous@diligentgraphics.com>2020-12-01 05:40:10 +0000
committerassiduous <assiduous@diligentgraphics.com>2020-12-01 05:40:10 +0000
commit8cb15ebb5da1fc825a211548d51a7e77bb981647 (patch)
tree672f8cf5e267d898abb30a83425af3b0a5567357 /AssetLoader
parentGLTF Loader: implemented buffer suballocation in cache (diff)
downloadDiligentTools-8cb15ebb5da1fc825a211548d51a7e77bb981647.tar.gz
DiligentTools-8cb15ebb5da1fc825a211548d51a7e77bb981647.zip
GLTF Loader: added GetFirstIndexLocation and GetBaseVertex methods
Diffstat (limited to 'AssetLoader')
-rw-r--r--AssetLoader/interface/GLTFLoader.hpp35
-rw-r--r--AssetLoader/interface/GLTFResourceManager.hpp11
-rw-r--r--AssetLoader/src/GLTFLoader.cpp7
-rw-r--r--AssetLoader/src/GLTFResourceManager.cpp14
4 files changed, 46 insertions, 21 deletions
diff --git a/AssetLoader/interface/GLTFLoader.hpp b/AssetLoader/interface/GLTFLoader.hpp
index 8effc2b..1632bb0 100644
--- a/AssetLoader/interface/GLTFLoader.hpp
+++ b/AssetLoader/interface/GLTFLoader.hpp
@@ -342,23 +342,36 @@ struct Model
void PrepareGPUResources(IDeviceContext* pCtx);
- IBuffer* GetBuffer(BUFFER_ID BuffId, Uint32& Offset)
+ IBuffer* GetBuffer(BUFFER_ID BuffId)
{
VERIFY_EXPR(BuffId < BUFFER_ID_NUM_BUFFERS);
auto& Buff = Buffers[BuffId];
if (Buff.pBuffer)
- {
- Offset = 0;
return Buff.pBuffer;
- }
- else if (CacheInfo.pResourceMgr != nullptr)
- {
- Offset = static_cast<Uint32>(Buff.CacheAllocation.Region.UnalignedOffset);
+ else if (Buff.CacheAllocation.IsValid())
return CacheInfo.pResourceMgr->GetBuffer(Buff.CacheAllocation);
- }
+ else
+ return nullptr;
+ }
- Offset = 0;
- return nullptr;
+ Uint32 GetFirstIndexLocation() const
+ {
+ auto& IndBuff = Buffers[BUFFER_ID_INDEX];
+ VERIFY(!IndBuff.CacheAllocation.IsValid() || IndBuff.CacheAllocation.Region.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)) :
+ 0;
+ }
+
+ Uint32 GetBaseVertex() const
+ {
+ auto& VertBuff = Buffers[BUFFER_ID_VERTEX0];
+ VERIFY(!VertBuff.CacheAllocation.IsValid() || VertBuff.CacheAllocation.Region.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)) :
+ 0;
}
ITexture* GetTexture(Uint32 Index)
@@ -366,7 +379,7 @@ struct Model
auto& TexInfo = Textures[Index];
if (TexInfo.pTexture)
return TexInfo.pTexture;
- else if (CacheInfo.pResourceMgr)
+ else if (TexInfo.CacheAllocation.IsValid())
return CacheInfo.pResourceMgr->GetTexture(TexInfo.CacheAllocation);
else
return nullptr;
diff --git a/AssetLoader/interface/GLTFResourceManager.hpp b/AssetLoader/interface/GLTFResourceManager.hpp
index 9a8fbbe..d8c9074 100644
--- a/AssetLoader/interface/GLTFResourceManager.hpp
+++ b/AssetLoader/interface/GLTFResourceManager.hpp
@@ -55,17 +55,20 @@ public:
bool IsValid() const
{
- return Region.IsValid();
+ VERIFY_EXPR(BufferIndex >= 0 && Region.IsValid() || BufferIndex < 0 && !Region.IsValid());
+ return BufferIndex >= 0;
}
};
struct TextureAllocation
{
- Int32 TextureIndex = -1;
+ Int32 TextureIndex = -1;
+
DynamicAtlasManager::Region Region;
bool IsValid() const
{
- return !Region.IsEmpty();
+ VERIFY_EXPR(TextureIndex >= 0 && !Region.IsEmpty() || TextureIndex < 0 && Region.IsEmpty());
+ return TextureIndex >= 0;
}
};
@@ -94,6 +97,7 @@ public:
IBuffer* GetBuffer(const BufferAllocation& Allocation)
{
+ VERIFY_EXPR(Allocation.IsValid());
return m_Buffers[Allocation.BufferIndex].pBuffer;
}
@@ -103,6 +107,7 @@ public:
ITexture* GetTexture(const TextureAllocation& Allocation)
{
+ VERIFY_EXPR(Allocation.IsValid());
return m_Textures[Allocation.TextureIndex].pTexture;
}
diff --git a/AssetLoader/src/GLTFLoader.cpp b/AssetLoader/src/GLTFLoader.cpp
index bfd074d..9fe0cd2 100644
--- a/AssetLoader/src/GLTFLoader.cpp
+++ b/AssetLoader/src/GLTFLoader.cpp
@@ -895,9 +895,10 @@ void Model::PrepareGPUResources(IDeviceContext* pCtx)
auto UpdateBuffer = [&](BUFFER_ID BuffId, const void* pData, size_t Size) //
{
- Uint32 Offset = 0;
- auto* pBuffer = GetBuffer(BuffId, Offset);
- pCtx->UpdateBuffer(pBuffer, Offset, static_cast<Uint32>(Size), pData, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
+ auto* pBuffer = GetBuffer(BuffId);
+ VERIFY_EXPR(pBuffer != nullptr);
+ auto Offset = Buffers[BuffId].CacheAllocation.IsValid() ? Buffers[BuffId].CacheAllocation.Region.UnalignedOffset : 0;
+ pCtx->UpdateBuffer(pBuffer, static_cast<Uint32>(Offset), static_cast<Uint32>(Size), pData, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
if (Buffers[BuffId].pBuffer != nullptr)
{
VERIFY_EXPR(Buffers[BuffId].pBuffer == pBuffer);
diff --git a/AssetLoader/src/GLTFResourceManager.cpp b/AssetLoader/src/GLTFResourceManager.cpp
index 54724ea..c6f627c 100644
--- a/AssetLoader/src/GLTFResourceManager.cpp
+++ b/AssetLoader/src/GLTFResourceManager.cpp
@@ -80,13 +80,16 @@ GLTFResourceManager::BufferAllocation GLTFResourceManager::AllocateBufferSpace(U
std::lock_guard<std::mutex> Lock{BuffCache.Mtx};
BufferAllocation Allocation;
- Allocation.BufferIndex = BufferIndex;
- Allocation.Region = BuffCache.Mgr.Allocate(Size, Alignment);
+ Allocation.Region = BuffCache.Mgr.Allocate(Size, Alignment);
+ if (Allocation.Region.IsValid())
+ Allocation.BufferIndex = BufferIndex;
return Allocation;
}
void GLTFResourceManager::FreeBufferSpace(BufferAllocation&& Allocation)
{
+ VERIFY_EXPR(Allocation.IsValid());
+
auto& BuffCache = m_Buffers[Allocation.BufferIndex];
std::lock_guard<std::mutex> Lock{BuffCache.Mtx};
@@ -103,10 +106,11 @@ GLTFResourceManager::TextureAllocation GLTFResourceManager::AllocateTextureSpace
std::lock_guard<std::mutex> Lock{TexCache.Mtx};
TextureAllocation Allocation;
- Allocation.TextureIndex = TextureIndex;
- Allocation.Region = TexCache.Mgr.Allocate(Width, Height);
+ Allocation.Region = TexCache.Mgr.Allocate(Width, Height);
if (!Allocation.Region.IsEmpty())
{
+ Allocation.TextureIndex = TextureIndex;
+
Allocation.Region.x *= TexCache.Granularity;
Allocation.Region.y *= TexCache.Granularity;
Allocation.Region.width *= TexCache.Granularity;
@@ -117,6 +121,8 @@ GLTFResourceManager::TextureAllocation GLTFResourceManager::AllocateTextureSpace
void GLTFResourceManager::FreeTextureSpace(TextureAllocation&& Allocation)
{
+ VERIFY_EXPR(Allocation.IsValid());
+
auto& TexCache = m_Textures[Allocation.TextureIndex];
auto& Region = Allocation.Region;