Skip to content

Commit 06168ca

Browse files
committed
add GenOptions option to control the optimization
1 parent 33635cb commit 06168ca

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

source/MaterialXGenShader/GenOptions.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ class MX_GENSHADER_API GenOptions
9494
hwNormalizeUdimTexCoords(false),
9595
hwWriteAlbedoTable(false),
9696
hwWriteEnvPrefilter(false),
97-
hwImplicitBitangents(true)
97+
hwImplicitBitangents(true),
98+
optReplaceBsdfMixWithLinearCombination(false)
9899
{
99100
}
100101
virtual ~GenOptions() { }
@@ -197,6 +198,10 @@ class MX_GENSHADER_API GenOptions
197198
/// Calculate fallback bitangents from existing normals and tangents
198199
/// inside the bitangent node.
199200
bool hwImplicitBitangents;
201+
202+
/// Analyse the graph of ShaderNodes and replace any ND_mix_bsdf nodes
203+
/// with a linear combination of their weighted inputs
204+
bool optReplaceBsdfMixWithLinearCombination;
200205
};
201206

202207
MATERIALX_NAMESPACE_END

source/MaterialXGenShader/ShaderGraph.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -971,12 +971,15 @@ void ShaderGraph::optimize(GenContext& context)
971971
}
972972

973973
// we take a copy of the node list because we might modify it during the optimization
974-
const vector<ShaderNode*> nodeList = getNodes();
975-
for (ShaderNode* node : nodeList)
974+
if (context.getOptions().optReplaceBsdfMixWithLinearCombination)
976975
{
977-
if (node->hasClassification(ShaderNode::Classification::MIX_BSDF))
976+
const vector<ShaderNode*> nodeList = getNodes();
977+
for (ShaderNode* node : nodeList)
978978
{
979-
optimizeMixBsdf(node, context);
979+
if (node->hasClassification(ShaderNode::Classification::MIX_BSDF))
980+
{
981+
optimizeMixBsdf(node, context);
982+
}
980983
}
981984
}
982985
}

source/MaterialXGraphEditor/RenderView.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ RenderView::RenderView(mx::DocumentPtr doc,
162162
// Make sure all uniforms are added so value updates can
163163
// find the corresponding uniform.
164164
_genContext.getOptions().shaderInterfaceType = mx::SHADER_INTERFACE_COMPLETE;
165+
_genContext.getOptions().optReplaceBsdfMixWithLinearCombination = true;
165166

166167
setDocument(doc);
167168
_stdLib = stdLib;

source/MaterialXView/Viewer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ Viewer::Viewer(const std::string& materialFilename,
255255
_genContext.getOptions().fileTextureVerticalFlip = true;
256256
_genContext.getOptions().hwShadowMap = true;
257257
_genContext.getOptions().hwImplicitBitangents = false;
258+
_genContext.getOptions().optReplaceBsdfMixWithLinearCombination = true;
258259

259260
#ifdef MATERIALXVIEW_METAL_BACKEND
260261
_renderPipeline = MetalRenderPipeline::create(this);
@@ -267,6 +268,7 @@ Viewer::Viewer(const std::string& materialFilename,
267268
_genContextEssl.getOptions().targetColorSpaceOverride = "lin_rec709";
268269
_genContextEssl.getOptions().fileTextureVerticalFlip = false;
269270
_genContextEssl.getOptions().hwMaxActiveLightSources = 1;
271+
_genContextEssl.getOptions().optReplaceBsdfMixWithLinearCombination = true;
270272
#endif
271273
#if MATERIALX_BUILD_GEN_OSL
272274
// Set OSL generator options.

0 commit comments

Comments
 (0)