Skip to content

Commit

Permalink
HnGeometryPool: track pending vertex/index data size
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 31, 2024
1 parent 386a74e commit db6c7b2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Hydrogent/include/HnGeometryPool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <vector>
#include <mutex>
#include <memory>
#include <atomic>

#include "pxr/pxr.h"
#include "pxr/base/tf/token.h"
Expand Down Expand Up @@ -93,6 +94,9 @@ class HnGeometryPool final
Uint32 StartVertex,
std::shared_ptr<HnGeometryPool::IndexHandle>& Handle);

Int64 GetPendingVertexDataSize() const { return m_PendingVertexDataSize; }
Int64 GetPendingIndexDataSize() const { return m_PendingIndexDataSize; }

private:
RefCntAutoPtr<IRenderDevice> m_pDevice;
GLTF::ResourceManager& m_ResMgr;
Expand All @@ -107,9 +111,11 @@ class HnGeometryPool final

std::mutex m_PendingVertexDataMtx;
std::vector<std::shared_ptr<VertexData>> m_PendingVertexData;
std::atomic<Int64> m_PendingVertexDataSize{0};

std::mutex m_PendingIndexDataMtx;
std::vector<std::shared_ptr<IndexData>> m_PendingIndexData;
std::atomic<Int64> m_PendingIndexDataSize{0};

ObjectsRegistry<size_t, std::shared_ptr<VertexData>> m_VertexCache;
ObjectsRegistry<size_t, std::shared_ptr<IndexData>> m_IndexCache;
Expand Down
26 changes: 26 additions & 0 deletions Hydrogent/src/HnGeometryPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ HnGeometryPool::HnGeometryPool(IRenderDevice* pDevice,

HnGeometryPool::~HnGeometryPool()
{
VERIFY((m_PendingVertexData.empty() && m_PendingVertexDataSize >= 0) || m_PendingVertexDataSize == 0, "Pending vertex data size must be 0 when there are no pending data");
VERIFY((m_PendingIndexData.empty() && m_PendingIndexDataSize >= 0) || m_PendingIndexDataSize == 0, "Pending index data size must be 0 when there are no pending data");
}

class GeometryPoolData
Expand Down Expand Up @@ -411,6 +413,17 @@ class HnGeometryPool::VertexData final : public GeometryPoolData
return Hashes;
}

size_t GetTotalSize() const
{
size_t TotalSize = 0;
for (const auto& stream_it : m_Streams)
{
const VertexStream& Stream = stream_it.second;
TotalSize += m_NumVertices * Stream.ElementSize;
}
return TotalSize;
}

private:
void InitPoolAllocation()
{
Expand Down Expand Up @@ -691,6 +704,11 @@ class HnGeometryPool::IndexData final : public GeometryPoolData
CommitPendingUses();
}

size_t GetSize() const
{
return m_NumIndices * sizeof(Uint32);
}

private:
void InitPoolAllocation()
{
Expand Down Expand Up @@ -869,6 +887,8 @@ void HnGeometryPool::AllocateVertices(const std::string& Name,
m_UseVertexPool ? &m_ResMgr : nullptr,
DisallowPoolAllocationReuse, ExistingData);

m_PendingVertexDataSize.fetch_add(static_cast<Int64>(Data->GetTotalSize()));

{
std::lock_guard<std::mutex> Guard{m_PendingVertexDataMtx};
m_PendingVertexData.emplace_back(Data);
Expand Down Expand Up @@ -914,6 +934,8 @@ void HnGeometryPool::AllocateIndices(const std::string&

std::shared_ptr<IndexData> Data = std::make_shared<IndexData>(Name, std::move(Indices), StartVertex, m_UseIndexPool ? &m_ResMgr : nullptr, ExistingData);

m_PendingIndexDataSize.fetch_add(static_cast<Int64>(Data->GetSize()));

{
std::lock_guard<std::mutex> Guard{m_PendingIndexDataMtx};
m_PendingIndexData.emplace_back(Data);
Expand All @@ -940,6 +962,8 @@ void HnGeometryPool::Commit(IDeviceContext* pContext)
for (auto& Data : m_PendingVertexData)
{
Data->Initialize(m_pDevice, pContext);
m_PendingVertexDataSize.fetch_add(-static_cast<Int64>(Data->GetTotalSize()));
VERIFY_EXPR(m_PendingVertexDataSize >= 0);
}
m_PendingVertexData.clear();
}
Expand All @@ -949,6 +973,8 @@ void HnGeometryPool::Commit(IDeviceContext* pContext)
for (auto& Data : m_PendingIndexData)
{
Data->Initialize(m_pDevice, pContext);
m_PendingIndexDataSize.fetch_add(-static_cast<Int64>(Data->GetSize()));
VERIFY_EXPR(m_PendingIndexDataSize >= 0);
}
m_PendingIndexData.clear();
}
Expand Down

0 comments on commit db6c7b2

Please sign in to comment.