Skip to content

Commit

Permalink
Refine dot node elision and remove unused code (#1522)
Browse files Browse the repository at this point in the history
Dot nodes were elided in #1152 to prevent creating extra samplers in rasterized shadergen.

This preserves the change written by @kwokcb while keeping the distinction between a topological constant node and a non-topological dot node.

In scenarios where you want to feed a single color to multiple shader nodes in a complex material, using a constant node will force recompilation every time the color is tweaked, we want users to be able to use a dot node to feed the graph, which should allow tweaking the color without having to recompile the shader. Also allows re-using the shader code for material instances differing on this input color.
  • Loading branch information
JGamache-autodesk authored Sep 8, 2023
1 parent be5b338 commit f69e4c2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,14 @@
</transformmatrix>
<output name="out" type="vector4" nodename="transformvector1" />
</nodegraph>
<nodegraph name="dot_filename">
<dot name="dot1" type="filename">
<input name="in" type="filename" value="resources/Images/grid.png" />
<input name="note" type="string" value="Checking for spurious samplers" />
</dot>
<image name="image_color3" type="color3">
<input name="file" type="filename" nodename="dot1" />
</image>
<output name="out" type="color3" nodename="image_color3" />
</nodegraph>
</materialx>
40 changes: 4 additions & 36 deletions source/MaterialXGenShader/ShaderGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1027,48 +1027,16 @@ void ShaderGraph::optimize(GenContext& context)
}
else if (node->hasClassification(ShaderNode::Classification::DOT))
{
// Dot nodes without modifiers can be elided by moving their connection downstream.
// Filename dot nodes must be elided so they do not create extra samplers.
ShaderInput* in = node->getInput("in");
if (in->getChannels().empty())
if (in->getChannels().empty() && in->getType() == Type::FILENAME)
{
bypass(context, node, 0);
++numEdits;
}
}
else if (node->hasClassification(ShaderNode::Classification::IFELSE))
{
// Check if we have a constant conditional expression
ShaderInput* intest = node->getInput("intest");
if (!intest->getConnection())
{
// Find which branch should be taken
ShaderInput* cutoff = node->getInput("cutoff");
ValuePtr value = intest->getValue();
const float intestValue = value ? value->asA<float>() : 0.0f;
const int branch = (intestValue <= cutoff->getValue()->asA<float>() ? 2 : 3);

// Bypass the conditional using the taken branch
bypass(context, node, branch);

++numEdits;
}
}
else if (node->hasClassification(ShaderNode::Classification::SWITCH))
{
// Check if we have a constant conditional expression
const ShaderInput* which = node->getInput("which");
if (!which->getConnection())
{
// Find which branch should be taken
ValuePtr value = which->getValue();
const int branch = int(value == nullptr ? 0 : (which->getType() == Type::FLOAT ? value->asA<float>() : value->asA<int>()));

// Bypass the conditional using the taken branch
bypass(context, node, branch);

++numEdits;
}
}
// Adding more nodes here requires them to have an input that is tagged
// "uniform" in the NodeDef or to handle very specific cases, like FILENAME.
}

if (numEdits > 0)
Expand Down
10 changes: 0 additions & 10 deletions source/MaterialXGenShader/ShaderNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ const ShaderNodePtr ShaderNode::NONE = createEmptyNode();
const string ShaderNode::CONSTANT = "constant";
const string ShaderNode::DOT = "dot";
const string ShaderNode::IMAGE = "image";
const string ShaderNode::COMPARE = "compare";
const string ShaderNode::SWITCH = "switch";
const string ShaderNode::SURFACESHADER = "surfaceshader";
const string ShaderNode::SCATTER_MODE = "scatter_mode";
const string ShaderNode::BSDF_R = "R";
Expand Down Expand Up @@ -292,14 +290,6 @@ ShaderNodePtr ShaderNode::create(const ShaderGraph* parent, const string& name,
{
newNode->_classification = Classification::TEXTURE | Classification::DOT;
}
else if (nodeDef.getNodeString() == COMPARE)
{
newNode->_classification = Classification::TEXTURE | Classification::CONDITIONAL | Classification::IFELSE;
}
else if (nodeDef.getNodeString() == SWITCH)
{
newNode->_classification = Classification::TEXTURE | Classification::CONDITIONAL | Classification::SWITCH;
}
// Third, check for file texture classification by group name
else if (groupName == TEXTURE2D_GROUPNAME || groupName == TEXTURE3D_GROUPNAME)
{
Expand Down
13 changes: 4 additions & 9 deletions source/MaterialXGenShader/ShaderNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,23 +350,18 @@ class MX_GENSHADER_API ShaderNode
static const uint32_t VOLUME = 1 << 15; /// A volume shader node
static const uint32_t LIGHT = 1 << 16; /// A light shader node
static const uint32_t UNLIT = 1 << 17; /// An unlit surface shader node
// Specific conditional types
static const uint32_t IFELSE = 1 << 18; /// An if-else statement
static const uint32_t SWITCH = 1 << 19; /// A switch statement
// Types based on nodegroup
static const uint32_t SAMPLE2D = 1 << 20; /// Can be sampled in 2D (uv space)
static const uint32_t SAMPLE3D = 1 << 21; /// Can be sampled in 3D (position)
static const uint32_t GEOMETRIC = 1 << 22; /// Geometric input
static const uint32_t DOT = 1 << 23; /// A dot node
static const uint32_t SAMPLE2D = 1 << 18; /// Can be sampled in 2D (uv space)
static const uint32_t SAMPLE3D = 1 << 19; /// Can be sampled in 3D (position)
static const uint32_t GEOMETRIC = 1 << 20; /// Geometric input
static const uint32_t DOT = 1 << 21; /// A dot node
};

static const ShaderNodePtr NONE;

static const string CONSTANT;
static const string DOT;
static const string IMAGE;
static const string COMPARE;
static const string SWITCH;
static const string SURFACESHADER;
static const string SCATTER_MODE;
static const string BSDF_R;
Expand Down

0 comments on commit f69e4c2

Please sign in to comment.