Skip to content

Commit 206c44d

Browse files
committed
add GenOptions option to control the optimization
1 parent f9e1b19 commit 206c44d

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
@@ -93,7 +93,8 @@ class MX_GENSHADER_API GenOptions
9393
hwNormalizeUdimTexCoords(false),
9494
hwWriteAlbedoTable(false),
9595
hwWriteEnvPrefilter(false),
96-
hwImplicitBitangents(true)
96+
hwImplicitBitangents(true),
97+
optReplaceBsdfMixWithLinearCombination(false)
9798
{
9899
}
99100
virtual ~GenOptions() { }
@@ -191,6 +192,10 @@ class MX_GENSHADER_API GenOptions
191192
/// Calculate fallback bitangents from existing normals and tangents
192193
/// inside the bitangent node.
193194
bool hwImplicitBitangents;
195+
196+
/// Analyse the graph of ShaderNodes and replace any ND_mix_bsdf nodes
197+
/// with a linear combination of their weighted inputs
198+
bool optReplaceBsdfMixWithLinearCombination;
194199
};
195200

196201
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
@@ -252,6 +252,7 @@ Viewer::Viewer(const std::string& materialFilename,
252252
_genContext.getOptions().fileTextureVerticalFlip = true;
253253
_genContext.getOptions().hwShadowMap = true;
254254
_genContext.getOptions().hwImplicitBitangents = false;
255+
_genContext.getOptions().optReplaceBsdfMixWithLinearCombination = true;
255256

256257
#ifdef MATERIALXVIEW_METAL_BACKEND
257258
_renderPipeline = MetalRenderPipeline::create(this);
@@ -264,6 +265,7 @@ Viewer::Viewer(const std::string& materialFilename,
264265
_genContextEssl.getOptions().targetColorSpaceOverride = "lin_rec709";
265266
_genContextEssl.getOptions().fileTextureVerticalFlip = false;
266267
_genContextEssl.getOptions().hwMaxActiveLightSources = 1;
268+
_genContextEssl.getOptions().optReplaceBsdfMixWithLinearCombination = true;
267269
#endif
268270
#if MATERIALX_BUILD_GEN_OSL
269271
// Set OSL generator options.

0 commit comments

Comments
 (0)