Skip to content

Commit

Permalink
HnMaterial: check if DirtyResource flag is set
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 7, 2024
1 parent 2f93f36 commit 19bc5f8
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions Hydrogent/src/HnMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,45 +106,48 @@ void HnMaterial::Sync(pxr::HdSceneDelegate* SceneDelegate,
if (*DirtyBits == pxr::HdMaterial::Clean)
return;

HnRenderDelegate* RenderDelegate = static_cast<HnRenderDelegate*>(SceneDelegate->GetRenderIndex().GetRenderDelegate());
HnTextureRegistry& TexRegistry = RenderDelegate->GetTextureRegistry();
const USD_Renderer& UsdRenderer = *RenderDelegate->GetUSDRenderer();
if (*DirtyBits & pxr::HdMaterial::DirtyResource)
{
HnRenderDelegate* RenderDelegate = static_cast<HnRenderDelegate*>(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<pxr::HdMaterialNetworkMap>())
{
const pxr::HdMaterialNetworkMap& hdNetworkMap = vtMat.UncheckedGet<pxr::HdMaterialNetworkMap>();
if (!hdNetworkMap.terminals.empty() && !hdNetworkMap.map.empty())
pxr::VtValue vtMat = SceneDelegate->GetMaterialResource(GetId());
if (vtMat.IsHolding<pxr::HdMaterialNetworkMap>())
{
try
const pxr::HdMaterialNetworkMap& hdNetworkMap = vtMat.UncheckedGet<pxr::HdMaterialNetworkMap>();
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<HnRenderParam*>(RenderParam)->MakeAttribDirty(HnRenderParam::GlobalAttrib::Material);
if (RenderParam)
{
static_cast<HnRenderParam*>(RenderParam)->MakeAttribDirty(HnRenderParam::GlobalAttrib::Material);
}
}

*DirtyBits = HdMaterial::Clean;
Expand Down

0 comments on commit 19bc5f8

Please sign in to comment.