Skip to content

Commit

Permalink
Add test cases for MDL custom nodes: #2125
Browse files Browse the repository at this point in the history
  • Loading branch information
krohmerNV committed Jan 2, 2025
1 parent bc2466e commit a74baf5
Show file tree
Hide file tree
Showing 20 changed files with 739 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0"?>
<materialx version="1.39">

<!-- definition -->
<!-- specifies the interface of the node, including:
- the `node` that defines the name of the element during instantiation
- all input parameter types and names
- all output parameter types and names
- all inputs and outputs can have defaults
- the `name` that needs to br referenced when adding the implementation with the actual source code
-->
<nodedef name="ND_custom_inline_node" node="custom_inline_node" nodegroup="custom" doc="A simple custom inline node.">
<input name="red" type="float" />
<input name="green" type="float" value="0.0" />
<input name="blue" type="float" value="0.75" />
<output name="out" type="color3" />
</nodedef>

<!-- implementation -->
<!-- specifies the body of the function.
There is no need to add a signature because it will be generated from the definition.
Input and output variables can just be used as if they where declared and initialized with the corresponding defaults before.
Functions are not allowed to return, also no early returns allowed.
All input and output names in the MDL implementation have a "mxp_" prefix in order to avoid collisions with reserved names.
When adding comments in the inline sourcecode, use `/* comment */` as `//` is not supported at this point.
-->
<implementation
name="IM_custom_inline_node"
nodedef="ND_custom_inline_node"
target="genmdl"
sourcecode="
float green2 = 2.0 * mxp_green;
mxp_out = color(mxp_red, green2, mxp_blue);
"/>

<!-- instantiation -->
<!-- creates an instance of a node to be called.
Can appear multiple times for the same node.
Parameters can be overridden or default values are used.
-->
<custom_inline_node name="custom1" type="color3">
<input name="red" type="float" value="0.25" />
<input name="green" type="float" value="0.25" />
</custom_inline_node>
<!-- add another instance to test that function definitions are emitted only once -->
<!-- also add some input node to test input connections -->
<texcoord name="texcoord1" type="vector2" />
<multiply name="texcoord_scaled1" type="vector2">
<input name="in1" type="vector2" nodename="texcoord1" />
<input name="in2" type="float" value="10" />
</multiply>
<noise2d name="custom_node_input1" type="float">
<input name="amplitude" type="float" value="1.0" />
<input name="texcoord" type="vector2" nodename="texcoord_scaled1" />
</noise2d>
<custom_inline_node name="custom2" type="color3">
<input name="red" type="float" value="0.05" />
<input name="green" type="float" value="0.55" />
<input name="blue" type="float" nodename="custom_node_input1" />
</custom_inline_node>
<mix name="blend1" type="color3">
<input name="fg" type="color3" nodename="custom1" />
<input name="bg" type="color3" nodename="custom2" />
<input name="mix" type="float" value="0.5" />
</mix>
<burley_diffuse_bsdf name="burley_brdf1" type="BSDF">
<input name="color" type="color3" nodename="blend1" />
<input name="roughness" type="float" value="0.2" />
</burley_diffuse_bsdf>
<surface name="surface1" type="surfaceshader">
<input name="bsdf" type="BSDF" nodename="burley_brdf1" />
<input name="opacity" type="float" value="1.0" />
</surface>

<surfacematerial name="code_constant_color" type="material">
<input name="surfaceshader" type="surfaceshader" nodename="surface1" />
</surfacematerial>

</materialx>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0"?>
<materialx version="1.39">

<!-- definition -->
<nodedef name="ND_custom_inline_node_defaults" node="custom_inline_node_defaults" nodegroup="custom" doc="A simple custom inline node.">
<output name="out" type="color3" value="0.0 1.0 0.0" /> <!-- want to see this color returned by the node if not changed -->
</nodedef>

<!-- implementation -->
<!-- Note, source code contains only a NO-OP -->
<implementation
name="IM_custom_inline_node_defaults"
nodedef="ND_custom_inline_node_defaults"
target="genmdl"
sourcecode="
mxp_out = mxp_out;
"/>

<!-- instantiation -->
<custom_inline_node_defaults name="custom1" type="color3" />
<burley_diffuse_bsdf name="burley_brdf1" type="BSDF">
<input name="color" type="color3" nodename="custom1" />
</burley_diffuse_bsdf>
<surface name="surface1" type="surfaceshader">
<input name="bsdf" type="BSDF" nodename="burley_brdf1" />
<input name="opacity" type="float" value="1.0" />
</surface>

<surfacematerial name="code_constant_color" type="material">
<input name="surfaceshader" type="surfaceshader" nodename="surface1" />
</surfacematerial>

</materialx>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0"?>
<materialx version="1.39">

<!-- definition -->
<nodedef name="ND_custom_inline_node_multiple_output" node="custom_inline_node_multiple_output" nodegroup="custom" doc="A simple custom inline node with multiple outputs.">
<input name="red" type="float" value="0.0" />
<input name="green" type="float" value="0.0" />
<input name="blue" type="float" value="0.0" />
<input name="alpha" type="float" value="1.0" />
<output name="out_color" type="color3" value="1.0 0.0 0.0" />
<output name="out_alpha" type="float" value="1.0" />
</nodedef>

<!-- implementation -->
<implementation
name="IM_custom_inline_node_multiple_output"
nodedef="ND_custom_inline_node_multiple_output"
target="genmdl"
sourcecode="
float green2 = 2.0f * mxp_green;
mxp_out_color = color(mxp_red, green2, mxp_blue);
mxp_out_alpha = mxp_alpha * 0.5;
"/>

<!-- instantiations -->
<custom_inline_node_multiple_output name="custom1" type="multioutput">
<input name="red" type="float" value="0.25" />
<input name="green" type="float" value="0.25" />
<input name="blue" type="float" value="0.75" />
</custom_inline_node_multiple_output>
<custom_inline_node_multiple_output name="custom2" type="multioutput">
<input name="red" type="float" value="0.05" />
<input name="green" type="float" value="0.55" />
<input name="blue" type="float" value="0.05" />
</custom_inline_node_multiple_output>

<mix name="blend1" type="color3">
<input name="fg" type="color3" nodename="custom1" output="out_color" />
<input name="bg" type="color3" nodename="custom2" output="out_color" />
<input name="mix" type="float" value="0.5" />
</mix>
<mix name="blend2" type="float">
<input name="fg" type="float" nodename="custom1" output="out_alpha" />
<input name="bg" type="float" nodename="custom2" output="out_alpha" />
<input name="mix" type="float" value="0.5" />
</mix>

<burley_diffuse_bsdf name="burley_brdf1" type="BSDF">
<input name="color" type="color3" nodename="blend1" />
<input name="roughness" type="float" value="0.2" />
</burley_diffuse_bsdf>
<surface name="surface1" type="surfaceshader">
<input name="bsdf" type="BSDF" nodename="burley_brdf1" />
<input name="opacity" type="float" nodename="blend2" />
</surface>

<surfacematerial name="code_constant_color" type="material">
<input name="surfaceshader" type="surfaceshader" nodename="surface1" />
</surfacematerial>

</materialx>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0"?>
<materialx version="1.39">

<!-- definition -->
<nodedef name="ND_custom_inline_node_reserved_words" node="custom_inline_node_reserved_words" nodegroup="custom" doc="A simple custom inline node.">
<input name="a" type="float" value="0.8" /> <!-- "a" is NOT reserved in MDL -->
<input name="auto" type="float" value="0.8" /> <!-- "auto" is reserved in MDL -->
<output name="out" type="color3" /> <!-- "out" is a reserved word in MDL -->
<output name="shader" type="float" value="1.0" /> <!-- "shader" is a reserved word in MDL -->
</nodedef>

<!-- implementation -->
<!-- Note, ALL input and output port names are prefixed with "mxp_" to avoid collisions with reserved names -->
<implementation
name="IM_custom_inline_node_reserved_words"
nodedef="ND_custom_inline_node_reserved_words"
target="genmdl"
sourcecode="
mxp_out = color(0.1, mxp_a, mxp_auto);
mxp_shader = math::max(mxp_auto, 0.5);
"/>

<!-- instantiations -->
<custom_inline_node_reserved_words name="custom1" type="multioutput">
<input name="auto" type="float" value="0.2" />
</custom_inline_node_reserved_words>

<burley_diffuse_bsdf name="burley_brdf1" type="BSDF">
<input name="color" type="color3" nodename="custom1" output="out" />
<input name="roughness" type="float" value="0.2" />
</burley_diffuse_bsdf>
<surface name="surface1" type="surfaceshader">
<input name="bsdf" type="BSDF" nodename="burley_brdf1" />
<input name="opacity" type="float" nodename="custom1" output="shader" />
</surface>

<surfacematerial name="code_constant_color" type="material">
<input name="surfaceshader" type="surfaceshader" nodename="surface1" />
</surfacematerial>
</materialx>
67 changes: 67 additions & 0 deletions resources/Materials/TestSuite/libraries/custom/custom_node.mtlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0"?>
<materialx version="1.39">

<!-- definition -->
<!-- specifies the interface of the node, including:
- the `node` that defines the name of the element during instantiation
- all input parameter types and names
- all output parameter types and names
- all inputs and outputs can have defaults
- the `name` that needs to br referenced when adding the implementation with the actual source code
-->
<nodedef name="ND_custom_node" node="custom_node" nodegroup="custom" doc="A simple example that creates a color.">
<input name="red" type="float" value="0.0" />
<input name="green" type="float" value="0.0" />
<input name="blue" type="float" value="0.0" />
<output name="out" type="color3" />
</nodedef>

<!-- implementation -->
<!-- specifies where to find the implementation of the custom node.
Input and output names in the MDL implementation have a "mxp_" prefix in case they would collide with reserved names.
Note, the defaults for output parameters (if present) must be matched in the MDL implementation
in case the outputs remain unchanged in at least one code path.
-->
<implementation
name="IM_custom_node"
nodedef="ND_custom_node"
file="mx_custom_node.mdl"
function="custom_node_function"
target="genmdl"/>

<!-- instantiations -->
<!-- creates an instance of a custom node to be called.
Can appear multiple times for the same node.
Parameters can be overridden or default values are used.
-->
<!-- also add some input node to test input connections -->
<texcoord name="texcoord1" type="vector2" />
<multiply name="texcoord_scaled1" type="vector2">
<input name="in1" type="vector2" nodename="texcoord1" />
<input name="in2" type="float" value="10" />
</multiply>
<noise2d name="custom_node_input1" type="float">
<input name="amplitude" type="float" value="1.0" />
<input name="texcoord" type="vector2" nodename="texcoord_scaled1" />
</noise2d>
<custom_node name="custom_node1" type="color3">
<input name="red" type="float" value="0.35" />
<input name="green" type="float" value="0.45" />
<input name="blue" type="float" nodename="custom_node_input1" />
</custom_node>

<burley_diffuse_bsdf name="burley_brdf1" type="BSDF">
<input name="color" type="color3" nodename="custom_node1" />
<input name="roughness" type="float" value="0.2" />
</burley_diffuse_bsdf>

<surface name="surface1" type="surfaceshader">
<input name="bsdf" type="BSDF" nodename="burley_brdf1" />
<input name="opacity" type="float" value="1.0" />
</surface>

<surfacematerial name="custom_node_test" type="material">
<input name="surfaceshader" type="surfaceshader" nodename="surface1" />
</surfacematerial>

</materialx>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<materialx version="1.39" colorspace="lin_rec709">

<!-- definition -->
<nodedef name="ND_custom_node_gen_versioning" node="custom_node_gen_versioning" nodegroup="custom" doc="A simple function in a module with MDL language versioning.">
<input name="scale" type="float" value="1.0" />
<output name="out" type="color3" />
</nodedef>
<!-- implementation -->
<!--
{{MDL_VERSION_SUFFIX}} is used as placeholder MDL version number appended to the module
to make use of new language features while maintaining backwards compatibility. (see pbrlib_*.mdl for an example)
-->
<implementation
name="IM_custom_node_gen_versioning"
nodedef="ND_custom_node_gen_versioning"
file="mx_custom_library_{{MDL_VERSION_SUFFIX}}.mdl"
function="custom_library_function"
target="genmdl"/>
<!-- instantiation -->
<custom_node_gen_versioning name="custom_node_gen_versioning1" type="color3">
<input name="scale" type="float" value="1.0"/>
</custom_node_gen_versioning>
<burley_diffuse_bsdf name="burley_brdf1" type="BSDF">
<input name="color" type="color3" nodename="custom_node_gen_versioning1" />
</burley_diffuse_bsdf>
<surface name="surface1" type="surfaceshader">
<input name="bsdf" type="BSDF" nodename="burley_brdf1" />
<input name="opacity" type="float" value="1.0" />
</surface>
<surfacematerial name="custom_node_gen_versioning_test" type="material">
<input name="surfaceshader" type="surfaceshader" nodename="surface1" />
</surfacematerial>
</materialx>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0"?>
<materialx version="1.39">

<!-- definition -->
<nodedef name="ND_custom_node_multiple_output" node="custom_node_multiple_output" nodegroup="custom" doc="A simple example that illustrates multiple outputs.">
<input name="scale" type="float" value="1.0" />
<output name="tint" type="color3" default="0.0, 0.0, 0.0"/>
<output name="roughness" type="float" value="0.0"/>
</nodedef>

<!-- implementation -->
<implementation
name="IM_custom_node_multiple_output"
nodedef="ND_custom_node_multiple_output"
file="mx_custom_node.mdl"
function="custom_node_multiple_output_function"
target="genmdl"/>

<!-- instanciation -->
<custom_node_multiple_output name="custom_node_multiple_output1" type="multioutput">
<input name="scale" type="float" value="0.9" />
</custom_node_multiple_output>

<burley_diffuse_bsdf name="burley_brdf1" type="BSDF">
<input name="color" type="color3" nodename="custom_node_multiple_output1" output="tint" />
<input name="roughness" type="float" nodename="custom_node_multiple_output1" output="roughness" />
</burley_diffuse_bsdf>

<surface name="surface1" type="surfaceshader">
<input name="bsdf" type="BSDF" nodename="burley_brdf1" />
<input name="opacity" type="float" value="1.0" />
</surface>

<surfacematerial name="custom_node_multiple_output_test" type="material">
<input name="surfaceshader" type="surfaceshader" nodename="surface1" />
</surfacematerial>

</materialx>
Loading

0 comments on commit a74baf5

Please sign in to comment.