Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions source/MaterialXGenShader/ShaderGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1070,23 +1070,29 @@ void ShaderGraph::optimize(GenContext& context)
_nodeOrder = usedNodesVec;
}

// we take a copy of the node list because we might modify it during the optimization
// We store node names (not raw pointers) because optimization may call
// removeNode(), which deletes nodes via shared_ptr. Raw pointers would dangle.
if (context.getOptions().optReplaceBsdfMixWithLinearCombination)
{
const vector<ShaderNode*> nodeList = getNodes();
for (ShaderNode* node : nodeList)
vector<string> mixBsdfNodeNames;
for (ShaderNode* node : getNodes())
{
// first check the node is still in the graph, and hasn't been removed by a
// prior optimization
if (!getNode(node->getName()))
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This crashed for me - apparently because optimizeMixMixBsdf can delete not just the node we pass to it but also another mix node down the node order.

if (node->hasClassification(ShaderNode::Classification::MIX_BSDF))
{
mixBsdfNodeNames.push_back(node->getName());
}
}

for (const string& name : mixBsdfNodeNames)
{
// Look up fresh each iteration - node may have been removed by prior optimization
ShaderNode* node = getNode(name);
if (!node)
continue;

if (node->hasClassification(ShaderNode::Classification::MIX_BSDF))
if (!optimizeMixMixBsdf(node, context))
{
if (!optimizeMixMixBsdf(node, context))
{
optimizeMixBsdf(node, context);
}
optimizeMixBsdf(node, context);
}
}
}
Expand Down