Skip to content

Commit

Permalink
HnMaterial: improved logic to detect if SRB needs to be recreated
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Nov 25, 2024
1 parent 649e2e8 commit c308f4e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Hydrogent/include/HnTextureRegistry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,13 @@ class HnTextureRegistry final : public std::enable_shared_from_this<HnTextureReg

/// Returns the texture registry storage version.
///
/// \remarks The storage version is incremented every time a new texture is created
/// or dynamic texture atlas version changes.
/// \remarks The storage version is incremented every time a new texture is created.
///
/// The storage version is not incremented when the texture data is updated.
Uint32 GetStorageVersion() const;

Uint32 GetAtlasVersion() const;

/// Returns the texture registry data version.
///
/// \remarks The data version is incremented every time a texture is loaded or updated.
Expand Down
16 changes: 14 additions & 2 deletions Hydrogent/src/HnMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,20 @@ bool HnMaterial::UpdateSRB(HnRenderDelegate& RenderDelegate)
RefCntAutoPtr<HnMaterialSRBCache> SRBCache{RenderDelegate.GetMaterialSRBCache(), IID_HnMaterialSRBCache};
VERIFY_EXPR(SRBCache);

const Uint32 ResourceCacheVersion = TexRegistry.GetStorageVersion() + SRBCache->GetMaterialAttribsBufferVersion();
const HN_MATERIAL_TEXTURES_BINDING_MODE BindingMode = static_cast<const HnRenderParam*>(RenderDelegate.GetRenderParam())->GetTextureBindingMode();

Uint32 ResourceCacheVersion = SRBCache->GetMaterialAttribsBufferVersion();
if (BindingMode == HN_MATERIAL_TEXTURES_BINDING_MODE_ATLAS)
{
// Need to recreate SRBs if the atlas texture has changed
ResourceCacheVersion += TexRegistry.GetAtlasVersion();
}
else if (BindingMode == HN_MATERIAL_TEXTURES_BINDING_MODE_DYNAMIC)
{
// Need to recreate the SRB if any new texture is created
ResourceCacheVersion += TexRegistry.GetStorageVersion();
}

if (m_ResourceCacheVersion != ResourceCacheVersion)
{
m_SRB.Release();
Expand Down Expand Up @@ -1022,7 +1035,6 @@ bool HnMaterial::UpdateSRB(HnRenderDelegate& RenderDelegate)

HnMaterialSRBCache::ResourceKey SRBKey{m_ResourceCacheVersion};

const HN_MATERIAL_TEXTURES_BINDING_MODE BindingMode = static_cast<const HnRenderParam*>(RenderDelegate.GetRenderParam())->GetTextureBindingMode();
if (BindingMode == HN_MATERIAL_TEXTURES_BINDING_MODE_LEGACY ||
BindingMode == HN_MATERIAL_TEXTURES_BINDING_MODE_ATLAS)
{
Expand Down
4 changes: 2 additions & 2 deletions Hydrogent/src/HnRenderDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,8 +738,8 @@ 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();
// Tex storage version is incremented every time a new texture is created.
const Uint32 TexStorageVersion = m_TextureRegistry->GetStorageVersion() + m_TextureRegistry->GetAtlasVersion();
if (m_MaterialResourcesVersion != m_RenderParam->GetAttribVersion(HnRenderParam::GlobalAttrib::Material) + TexStorageVersion)
{
std::lock_guard<std::mutex> Guard{m_MaterialsMtx};
Expand Down
10 changes: 6 additions & 4 deletions Hydrogent/src/HnTextureRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,12 @@ HnTextureRegistry::TextureHandleSharedPtr HnTextureRegistry::Allocate(const HnTe

Uint32 HnTextureRegistry::GetStorageVersion() const
{
Uint32 Version = m_StorageVersion.load();
if (m_pResourceManager != nullptr)
Version += m_pResourceManager->GetTextureVersion();
return Version;
return m_StorageVersion.load();
}

Uint32 HnTextureRegistry::GetAtlasVersion() const
{
return m_pResourceManager != nullptr ? m_pResourceManager->GetTextureVersion() : 0;
}

Uint32 HnTextureRegistry::GetDataVersion() const
Expand Down

0 comments on commit c308f4e

Please sign in to comment.