Skip to content

Commit

Permalink
Hydrogent: added texture registry usage stats
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 19, 2024
1 parent 93ccf3b commit 0bf998a
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 45 deletions.
24 changes: 21 additions & 3 deletions Hydrogent/interface/HnRenderDelegate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
#include "../../../DiligentCore/Graphics/GraphicsEngine/interface/RenderDevice.h"
#include "../../../DiligentCore/Graphics/GraphicsTools/interface/RenderStateCache.h"
#include "../../../DiligentCore/Common/interface/RefCntAutoPtr.hpp"
#include "../../../DiligentTools/TextureLoader/interface/TextureLoader.h"
#include "../../PBR/interface/USD_Renderer.hpp"

#include "entt/entity/registry.hpp"

#include "HnTextureRegistry.hpp"
#include "HnGeometryPool.hpp"
#include "HnTypes.hpp"

Expand All @@ -62,6 +62,7 @@ class HnMesh;
class HnLight;
class HnRenderParam;
class HnShadowMapManager;
class HnTextureRegistry;

/// Memory usage statistics of the render delegate.
struct HnRenderDelegateMemoryStats
Expand Down Expand Up @@ -115,6 +116,23 @@ struct HnRenderDelegateMemoryStats
Uint64 AllocatedTexels = 0;
};
TextureAtlasUsage Atlas;

/// Texture registry usage statistics.
struct TextureRegistryUsage
{
/// The number of textures currently loading.
Uint32 NumTexturesLoading = 0;

/// The total size of texture data currently loading, in bytes.
Uint64 LoadingTexDataSize = 0;

/// The size of textures loaded into the atlas, in bytes.
Uint64 AtlasDataSize = 0;

/// The size of separate textures, in bytes.
Uint64 SeparateTexDataSize = 0;
};
TextureRegistryUsage TextureRegistry;
};

/// USD render delegate implementation in Hydrogent.
Expand Down Expand Up @@ -346,7 +364,7 @@ class HnRenderDelegate final : public pxr::HdRenderDelegate
/// Whether or not multithreaded sync is enabled for the specified prim type.
virtual bool IsParallelSyncEnabled(pxr::TfToken primType) const override final;

HnTextureRegistry& GetTextureRegistry() { return m_TextureRegistry; }
HnTextureRegistry& GetTextureRegistry() { return *m_TextureRegistry; }
HnGeometryPool& GetGeometryPool() { return m_GeometryPool; }
HnShadowMapManager* GetShadowMapManager() const { return m_ShadowMapManager.get(); }

Expand Down Expand Up @@ -416,7 +434,7 @@ class HnRenderDelegate final : public pxr::HdRenderDelegate
Uint32 m_MainPassFrameAttribsAlignedSize = 0;
Uint32 m_ShadowPassFrameAttribsAlignedSize = 0;

HnTextureRegistry m_TextureRegistry;
std::shared_ptr<HnTextureRegistry> m_TextureRegistry;
HnGeometryPool m_GeometryPool;
std::unique_ptr<HnRenderParam> m_RenderParam;
std::unique_ptr<HnShadowMapManager> m_ShadowMapManager;
Expand Down
24 changes: 22 additions & 2 deletions Hydrogent/interface/HnTextureRegistry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace USD

struct HnTextureIdentifier;

class HnTextureRegistry final
class HnTextureRegistry final : public std::enable_shared_from_this<HnTextureRegistry>
{
public:
struct CreateInfo
Expand All @@ -77,7 +77,7 @@ class HnTextureRegistry final
class TextureHandle
{
public:
TextureHandle(Uint32 Id) noexcept;
TextureHandle(HnTextureRegistry& Registry, Uint32 Id) noexcept;
~TextureHandle();

bool IsInitialized() const noexcept
Expand Down Expand Up @@ -121,10 +121,15 @@ class HnTextureRegistry final
RefCntAutoPtr<ITexture> m_pTexture;
RefCntAutoPtr<ITextureAtlasSuballocation> m_pAtlasSuballocation;

std::weak_ptr<HnTextureRegistry> m_Registry;

// Texture ID used for bindless access
const Uint32 m_TextureId;

std::atomic<bool> m_IsInitialized{false};

// Texture data size in bytes
Uint64 DataSize = 0;
};

using TextureHandleSharedPtr = std::shared_ptr<TextureHandle>;
Expand Down Expand Up @@ -170,13 +175,26 @@ class HnTextureRegistry final

Int32 GetNumTexturesLoading() const { return m_NumTexturesLoading.load(); }

void WaitForAsyncTasks();

struct UsageStats
{
Uint32 NumTexturesLoading = 0;
Uint64 LoadingTexDataSize = 0;
Uint64 AtlasDataSize = 0;
Uint64 SeparateTexDataSize = 0;
};
UsageStats GetUsageStats() const;

private:
void LoadTexture(const pxr::TfToken Key,
const pxr::TfToken& FilePath,
const pxr::HdSamplerParameters& SamplerParams,
std::function<RefCntAutoPtr<ITextureLoader>()> CreateLoader,
std::shared_ptr<TextureHandle> TexHandle);

void OnHandleDestroyed(const TextureHandle& Handle);

private:
RefCntAutoPtr<IRenderDevice> m_pDevice;
RefCntAutoPtr<IThreadPool> m_pThreadPool;
Expand Down Expand Up @@ -208,6 +226,8 @@ class HnTextureRegistry final
std::atomic<Int64> m_LoadingTexDataSize{0};
std::atomic<Uint32> m_StorageVersion{0};
std::atomic<Uint32> m_DataVersion{0};
std::atomic<Int64> m_AtlasDataSize{0};
std::atomic<Int64> m_SeparateTexDataSize{0};
};

} // namespace USD
Expand Down
66 changes: 41 additions & 25 deletions Hydrogent/src/HnRenderDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "HnRenderParam.hpp"
#include "HnFrameRenderTargets.hpp"
#include "HnShadowMapManager.hpp"
#include "HnTextureRegistry.hpp"

#include "DebugUtilities.hpp"
#include "GraphicsUtilities.h"
Expand Down Expand Up @@ -316,27 +317,28 @@ HnRenderDelegate::HnRenderDelegate(const CreateInfo& CI) :
m_MaterialSRBCache{HnMaterial::CreateSRBCache()},
m_USDRenderer{CreateUSDRenderer(CI, m_PrimitiveAttribsCB, m_MaterialSRBCache)},
m_TextureRegistry{
{
CI.pDevice,
CI.AsyncTextureLoading ? CI.pThreadPool : nullptr,
CI.TextureAtlasDim != 0 ? m_ResourceMgr : RefCntAutoPtr<GLTF::ResourceManager>{},
[](IRenderDevice* pDevice, TEXTURE_LOAD_COMPRESS_MODE CompressMode) {
switch (CompressMode)
{
case TEXTURE_LOAD_COMPRESS_MODE_NONE:
return TEXTURE_LOAD_COMPRESS_MODE_NONE;
std::make_shared<HnTextureRegistry>(
HnTextureRegistry::CreateInfo{
CI.pDevice,
CI.AsyncTextureLoading ? CI.pThreadPool : nullptr,
CI.TextureAtlasDim != 0 ? m_ResourceMgr : RefCntAutoPtr<GLTF::ResourceManager>{},
[](IRenderDevice* pDevice, TEXTURE_LOAD_COMPRESS_MODE CompressMode) {
switch (CompressMode)
{
case TEXTURE_LOAD_COMPRESS_MODE_NONE:
return TEXTURE_LOAD_COMPRESS_MODE_NONE;

case TEXTURE_LOAD_COMPRESS_MODE_BC:
case TEXTURE_LOAD_COMPRESS_MODE_BC_HIGH_QUAL:
return pDevice->GetDeviceInfo().Features.TextureCompressionBC ? CompressMode : TEXTURE_LOAD_COMPRESS_MODE_NONE;
case TEXTURE_LOAD_COMPRESS_MODE_BC:
case TEXTURE_LOAD_COMPRESS_MODE_BC_HIGH_QUAL:
return pDevice->GetDeviceInfo().Features.TextureCompressionBC ? CompressMode : TEXTURE_LOAD_COMPRESS_MODE_NONE;

default:
UNEXPECTED("Unexpected compress mode");
return TEXTURE_LOAD_COMPRESS_MODE_NONE;
}
}(CI.pDevice, CI.TextureCompressMode),
CI.TextureLoadBudget,
},
default:
UNEXPECTED("Unexpected compress mode");
return TEXTURE_LOAD_COMPRESS_MODE_NONE;
}
}(CI.pDevice, CI.TextureCompressMode),
CI.TextureLoadBudget,
}),
},
m_GeometryPool{CI.pDevice, *m_ResourceMgr, CI.UseVertexPool, CI.UseIndexPool},
m_RenderParam{
Expand Down Expand Up @@ -368,6 +370,14 @@ HnRenderDelegate::HnRenderDelegate(const CreateInfo& CI) :

HnRenderDelegate::~HnRenderDelegate()
{
VERIFY(m_Meshes.empty(), "Not all mesh prims have been destroyed. Verify that the imaging delegate is destroyed first, then the render index, and the render delegate is destroyed last.");
VERIFY(m_Materials.empty(), "Not all material prims have been destroyed. Verify that the imaging delegate is destroyed first, then the render index, and the render delegate is destroyed last.");
VERIFY(m_Lights.empty(), "Not all light prims have been destroyed. Verify that the imaging delegate is destroyed first, then the render index, and the render delegate is destroyed last.");

// Wait for all async tasks to complete.
// Note that this can't be done in the texture registry's destructor because shared pointer is
// destroyed before the destructor is called.
m_TextureRegistry->WaitForAsyncTasks();
}

pxr::HdRenderParam* HnRenderDelegate::GetRenderParam() const
Expand Down Expand Up @@ -498,7 +508,7 @@ pxr::HdSprim* HnRenderDelegate::CreateFallbackSprim(const pxr::TfToken& TypeId)
pxr::HdSprim* SPrim = nullptr;
if (TypeId == pxr::HdPrimTypeTokens->material)
{
m_FallbackMaterial = HnMaterial::CreateFallback(m_TextureRegistry, *m_USDRenderer);
m_FallbackMaterial = HnMaterial::CreateFallback(*m_TextureRegistry, *m_USDRenderer);
{
std::lock_guard<std::mutex> Guard{m_MaterialsMtx};
m_Materials.emplace(m_FallbackMaterial);
Expand Down Expand Up @@ -570,7 +580,7 @@ void HnRenderDelegate::CommitResources(pxr::HdChangeTracker* tracker)
m_ResourceMgr->UpdateVertexBuffers(m_pDevice, m_pContext);
m_ResourceMgr->UpdateIndexBuffer(m_pDevice, m_pContext);

m_TextureRegistry.Commit(m_pContext);
m_TextureRegistry->Commit(m_pContext);
m_GeometryPool.Commit(m_pContext);
if (m_ShadowMapManager)
{
Expand Down Expand Up @@ -650,7 +660,7 @@ void HnRenderDelegate::CommitResources(pxr::HdChangeTracker* tracker)

{
// Tex storage version is incremented every time a new texture is created or texture atlas version changes.
const Uint32 TexStorageVersion = m_TextureRegistry.GetStorageVersion();
const Uint32 TexStorageVersion = m_TextureRegistry->GetStorageVersion();
if (m_MaterialResourcesVersion != m_RenderParam->GetAttribVersion(HnRenderParam::GlobalAttrib::Material) + TexStorageVersion)
{
std::lock_guard<std::mutex> Guard{m_MaterialsMtx};
Expand Down Expand Up @@ -751,9 +761,10 @@ HnRenderDelegateMemoryStats HnRenderDelegate::GetMemoryStats() const
{
HnRenderDelegateMemoryStats MemoryStats;

const BufferSuballocatorUsageStats IndexUsage = m_ResourceMgr->GetIndexBufferUsageStats();
const VertexPoolUsageStats VertexUsage = m_ResourceMgr->GetVertexPoolUsageStats();
const DynamicTextureAtlasUsageStats AtlasUsage = m_ResourceMgr->GetAtlasUsageStats();
const BufferSuballocatorUsageStats IndexUsage = m_ResourceMgr->GetIndexBufferUsageStats();
const VertexPoolUsageStats VertexUsage = m_ResourceMgr->GetVertexPoolUsageStats();
const DynamicTextureAtlasUsageStats AtlasUsage = m_ResourceMgr->GetAtlasUsageStats();
const HnTextureRegistry::UsageStats TexRegistryStats = m_TextureRegistry->GetUsageStats();

MemoryStats.IndexPool.CommittedSize = IndexUsage.CommittedSize;
MemoryStats.IndexPool.UsedSize = IndexUsage.UsedSize;
Expand All @@ -769,6 +780,11 @@ HnRenderDelegateMemoryStats HnRenderDelegate::GetMemoryStats() const
MemoryStats.Atlas.TotalTexels = AtlasUsage.TotalArea;
MemoryStats.Atlas.AllocatedTexels = AtlasUsage.AllocatedArea;

MemoryStats.TextureRegistry.NumTexturesLoading = TexRegistryStats.NumTexturesLoading;
MemoryStats.TextureRegistry.LoadingTexDataSize = TexRegistryStats.LoadingTexDataSize;
MemoryStats.TextureRegistry.AtlasDataSize = TexRegistryStats.AtlasDataSize;
MemoryStats.TextureRegistry.SeparateTexDataSize = TexRegistryStats.SeparateTexDataSize;

return MemoryStats;
}

Expand Down
Loading

0 comments on commit 0bf998a

Please sign in to comment.