From 19bc5f8bb7371dcdb5a23428372b6a31e90fecee Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 7 Oct 2024 11:59:37 -0700 Subject: [PATCH] HnMaterial: check if DirtyResource flag is set --- Hydrogent/src/HnMaterial.cpp | 63 +++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/Hydrogent/src/HnMaterial.cpp b/Hydrogent/src/HnMaterial.cpp index dbc82051..0197c24e 100644 --- a/Hydrogent/src/HnMaterial.cpp +++ b/Hydrogent/src/HnMaterial.cpp @@ -106,45 +106,48 @@ void HnMaterial::Sync(pxr::HdSceneDelegate* SceneDelegate, if (*DirtyBits == pxr::HdMaterial::Clean) return; - HnRenderDelegate* RenderDelegate = static_cast(SceneDelegate->GetRenderIndex().GetRenderDelegate()); - HnTextureRegistry& TexRegistry = RenderDelegate->GetTextureRegistry(); - const USD_Renderer& UsdRenderer = *RenderDelegate->GetUSDRenderer(); + if (*DirtyBits & pxr::HdMaterial::DirtyResource) + { + HnRenderDelegate* RenderDelegate = static_cast(SceneDelegate->GetRenderIndex().GetRenderDelegate()); + HnTextureRegistry& TexRegistry = RenderDelegate->GetTextureRegistry(); + const USD_Renderer& UsdRenderer = *RenderDelegate->GetUSDRenderer(); - // A mapping from the texture name to the texture coordinate set index (e.g. "diffuseColor" -> 0) - TexNameToCoordSetMapType TexNameToCoordSetMap; + // A mapping from the texture name to the texture coordinate set index (e.g. "diffuseColor" -> 0) + TexNameToCoordSetMapType TexNameToCoordSetMap; - pxr::VtValue vtMat = SceneDelegate->GetMaterialResource(GetId()); - if (vtMat.IsHolding()) - { - const pxr::HdMaterialNetworkMap& hdNetworkMap = vtMat.UncheckedGet(); - if (!hdNetworkMap.terminals.empty() && !hdNetworkMap.map.empty()) + pxr::VtValue vtMat = SceneDelegate->GetMaterialResource(GetId()); + if (vtMat.IsHolding()) { - try + const pxr::HdMaterialNetworkMap& hdNetworkMap = vtMat.UncheckedGet(); + if (!hdNetworkMap.terminals.empty() && !hdNetworkMap.map.empty()) { - m_Network = HnMaterialNetwork{GetId(), hdNetworkMap}; // May throw + try + { + m_Network = HnMaterialNetwork{GetId(), hdNetworkMap}; // May throw - TexNameToCoordSetMap = AllocateTextures(TexRegistry); - ProcessMaterialNetwork(); - } - catch (const std::runtime_error& err) - { - LOG_ERROR_MESSAGE("Failed to create material network for material ", GetId(), ": ", err.what()); - m_Network = {}; - } - catch (...) - { - LOG_ERROR_MESSAGE("Failed to create material network for material ", GetId(), ": unknown error"); - m_Network = {}; + TexNameToCoordSetMap = AllocateTextures(TexRegistry); + ProcessMaterialNetwork(); + } + catch (const std::runtime_error& err) + { + LOG_ERROR_MESSAGE("Failed to create material network for material ", GetId(), ": ", err.what()); + m_Network = {}; + } + catch (...) + { + LOG_ERROR_MESSAGE("Failed to create material network for material ", GetId(), ": unknown error"); + m_Network = {}; + } } } - } - // It is important to initialize texture attributes with default values even if there is no material network. - InitTextureAttribs(TexRegistry, UsdRenderer, TexNameToCoordSetMap); + // It is important to initialize texture attributes with default values even if there is no material network. + InitTextureAttribs(TexRegistry, UsdRenderer, TexNameToCoordSetMap); - if (RenderParam) - { - static_cast(RenderParam)->MakeAttribDirty(HnRenderParam::GlobalAttrib::Material); + if (RenderParam) + { + static_cast(RenderParam)->MakeAttribDirty(HnRenderParam::GlobalAttrib::Material); + } } *DirtyBits = HdMaterial::Clean;