Skip to content

Commit

Permalink
Add elementat node for filename array and nodegraph implementation of…
Browse files Browse the repository at this point in the history
… imagearray - to hopefully make this an easier adoption.
  • Loading branch information
ld-kerley committed Dec 20, 2024
1 parent a291cd8 commit 388609e
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 3 deletions.
2 changes: 2 additions & 0 deletions libraries/stdlib/genglsl/stdlib_genglsl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
<input name="default" type="vector4" implname="default_value" />
</implementation>

<implementation name="IM_elementat_filenamearray_genglsl" nodedef="ND_elementat_filenamearray" target="genglsl" sourcecode="{{in}}[{{index}}]" />

<!-- <normalmap> -->
<implementation name="IM_normalmap_float_genglsl" nodedef="ND_normalmap_float" file="mx_normalmap.glsl" function="mx_normalmap_float" target="genglsl" />
<implementation name="IM_normalmap_vector2_genglsl" nodedef="ND_normalmap_vector2" file="mx_normalmap.glsl" function="mx_normalmap_vector2" target="genglsl" />
Expand Down
2 changes: 2 additions & 0 deletions libraries/stdlib/genmdl/stdlib_genmdl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
<input name="default" type="vector4" implname="default_value" />
</implementation>

<implementation name="IM_elementat_filenamearray_genmdl" nodedef="ND_elementat_filenamearray" target="genmdl" sourcecode="{{in}}[{{index}}]" />

<!-- <triplanarprojection> -->

<!-- <normalmap> -->
Expand Down
2 changes: 2 additions & 0 deletions libraries/stdlib/genmsl/stdlib_genmsl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
<input name="default" type="vector4" implname="default_value" />
</implementation>

<implementation name="IM_elementat_filenamearray_genmsl" nodedef="ND_elementat_filenamearray" target="genmsl" sourcecode="{{in}}[{{index}}]" />

<!-- ======================================================================== -->
<!-- Procedural nodes -->
<!-- ======================================================================== -->
Expand Down
8 changes: 8 additions & 0 deletions libraries/stdlib/genosl/mx_elementat_filenamearray.osl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
void mx_elementat_filenamearray(textureresources in, int index, output textureresource out)
{
// we currently need the intermediate variable to use the list initializer syntax
// this was reported to OSL project in issue #1906 - if that gets fixed we may be able
// to revert to direct assignment to the output variable.
textureresource tmp = { in.filename[index], in.colorspace };
out = tmp;
}
2 changes: 2 additions & 0 deletions libraries/stdlib/genosl/stdlib_genosl_impl.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
<input name="default" type="vector4" implname="default_value" />
</implementation>

<implementation name="IM_elementat_filenamearray_genosl" nodedef="ND_elementat_filenamearray" file="mx_elementat_filenamearray.osl" function="mx_elementat_filenamearray" target="genosl"/>

<!-- <triplanarprojection> -->

<!-- <normalmap> -->
Expand Down
6 changes: 6 additions & 0 deletions libraries/stdlib/stdlib_defs.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@
<output name="out" type="vector4" default="0.0, 0.0, 0.0, 0.0" />
</nodedef>

<nodedef name="ND_elementat_filenamearray" node="elementat" nodegroup="procedural">
<input name="in" type="filenamearray" value="" uniform="true"/>
<input name="index" type="integer" value="0"/>
<output name="out" type="filename" default=""/>
</nodedef>

<!--
Node: <tiledimage> Supplemental Node
Samples data from a single image, with provisions for tiling and offsetting the image
Expand Down
96 changes: 96 additions & 0 deletions libraries/stdlib/stdlib_ng.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,102 @@
<!-- Texture nodes -->
<!-- ======================================================================== -->

<!--
Node: <imagearray>
Samples data from a single image, or from a layer within a multi-layer image.
-->
<nodegraph name="NG_imagearray_float" nodedef="ND_imagearray_float">
<elementat name="selectFilename" type="filename">
<input name="in" type="filenamearray" interfacename="files"/>
<input name="index" type="integer" interfacename="index"/>
</elementat>
<image name="image" type="float">
<input name="file" type="filename" nodename="selectFilename" />
<input name="default" type="float" interfacename="default" />
<input name="texcoord" type="vector2" interfacename="texcoord" />
<input name="uaddressmode" type="string" interfacename="uaddressmode" />
<input name="vaddressmode" type="string" interfacename="vaddressmode" />
<input name="filtertype" type="string" interfacename="filtertype" />
</image>
<output name="out" type="float" nodename="image"/>
</nodegraph>
<nodegraph name="NG_imagearray_color3" nodedef="ND_imagearray_color3">
<elementat name="selectFilename" type="filename">
<input name="in" type="filenamearray" interfacename="files"/>
<input name="index" type="integer" interfacename="index"/>
</elementat>
<image name="image" type="color3">
<input name="file" type="filename" nodename="selectFilename" />
<input name="default" type="color3" interfacename="default" />
<input name="texcoord" type="vector2" interfacename="texcoord" />
<input name="uaddressmode" type="string" interfacename="uaddressmode" />
<input name="vaddressmode" type="string" interfacename="vaddressmode" />
<input name="filtertype" type="string" interfacename="filtertype" />
</image>
<output name="out" type="color3" nodename="image"/>
</nodegraph>
<nodegraph name="NG_imagearray_color4" nodedef="ND_imagearray_color4">
<elementat name="selectFilename" type="filename">
<input name="in" type="filenamearray" interfacename="files"/>
<input name="index" type="integer" interfacename="index"/>
</elementat>
<image name="image" type="color4">
<input name="file" type="filename" nodename="selectFilename" />
<input name="default" type="color4" interfacename="default" />
<input name="texcoord" type="vector2" interfacename="texcoord" />
<input name="uaddressmode" type="string" interfacename="uaddressmode" />
<input name="vaddressmode" type="string" interfacename="vaddressmode" />
<input name="filtertype" type="string" interfacename="filtertype" />
</image>
<output name="out" type="color4" nodename="image"/>
</nodegraph>
<nodegraph name="NG_imagearray_vector2" nodedef="ND_imagearray_vector2">
<elementat name="selectFilename" type="filename">
<input name="in" type="filenamearray" interfacename="files"/>
<input name="index" type="integer" interfacename="index"/>
</elementat>
<image name="image" type="vector2">
<input name="file" type="filename" nodename="selectFilename" />
<input name="default" type="vector2" interfacename="default" />
<input name="texcoord" type="vector2" interfacename="texcoord" />
<input name="uaddressmode" type="string" interfacename="uaddressmode" />
<input name="vaddressmode" type="string" interfacename="vaddressmode" />
<input name="filtertype" type="string" interfacename="filtertype" />
</image>
<output name="out" type="vector2" nodename="image"/>
</nodegraph>
<nodegraph name="NG_imagearray_vector3" nodedef="ND_imagearray_vector3">
<elementat name="selectFilename" type="filename">
<input name="in" type="filenamearray" interfacename="files"/>
<input name="index" type="integer" interfacename="index"/>
</elementat>
<image name="image" type="vector3">
<input name="file" type="filename" nodename="selectFilename" />
<input name="default" type="vector3" interfacename="default" />
<input name="texcoord" type="vector2" interfacename="texcoord" />
<input name="uaddressmode" type="string" interfacename="uaddressmode" />
<input name="vaddressmode" type="string" interfacename="vaddressmode" />
<input name="filtertype" type="string" interfacename="filtertype" />
</image>
<output name="out" type="vector3" nodename="image"/>
</nodegraph>
<nodegraph name="NG_imagearray_vector4" nodedef="ND_imagearray_vector4">
<elementat name="selectFilename" type="filename">
<input name="in" type="filenamearray" interfacename="files"/>
<input name="index" type="integer" interfacename="index"/>
</elementat>
<image name="image" type="vector4">
<input name="file" type="filename" nodename="selectFilename" />
<input name="default" type="vector4" interfacename="default" />
<input name="texcoord" type="vector2" interfacename="texcoord" />
<input name="uaddressmode" type="string" interfacename="uaddressmode" />
<input name="vaddressmode" type="string" interfacename="vaddressmode" />
<input name="filtertype" type="string" interfacename="filtertype" />
</image>
<output name="out" type="vector4" nodename="image"/>
</nodegraph>


<!--
Node: <tiledimage>
-->
Expand Down
10 changes: 10 additions & 0 deletions resources/Materials/TestSuite/stdlib/channel/elementat.mtlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<materialx version="1.39">
<nodegraph name="elementat_filenamearrayy">
<elementat name="elementat" type="filename">
<input name="in" type="filenamearray" value="resources/Images/cloth.png,resources/Images/grid.png,resources/Images/marble.png" />
<input name="index" type="integer" value="1" />
</elementat>
<output name="out" type="filename" nodename="elementat" />
</nodegraph>
</materialx>
44 changes: 42 additions & 2 deletions source/MaterialXGenOsl/OslSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,37 @@ class OSLFilenameTypeSyntax : public AggregateTypeSyntax
}
};

class OSLFilenameArrayTypeSyntax : public AggregateTypeSyntax
{
public:
OSLFilenameArrayTypeSyntax(const string& name, const string& defaultValue, const string& uniformDefaultValue,
const string& typeAlias = EMPTY_STRING, const string& typeDefinition = EMPTY_STRING,
const StringVec& members = EMPTY_MEMBERS) :
AggregateTypeSyntax(name, defaultValue, uniformDefaultValue, typeAlias, typeDefinition, members)
{
}

string getValue(const ShaderPort* port, bool uniform) const override
{
if (!port)
{
return EMPTY_STRING;
}

const string prefix = uniform ? "{" : getName() + "(";
const string suffix = uniform ? "}" : ")";
const string filename = port->getValue() ? port->getValue()->getValueString() : EMPTY_STRING;
return prefix + "{\"" + filename + "\"}, \"" + port->getColorSpace() + "\"" + suffix;
}

string getValue(const Value& value, bool uniform) const override
{
const string prefix = uniform ? "{" : getName() + "(";
const string suffix = uniform ? "}" : ")";
return prefix + "{\"" + value.getValueString() + "\"}, \"\"" + suffix;
}
};

} // anonymous namespace

const string OslSyntax::OUTPUT_QUALIFIER = "output";
Expand Down Expand Up @@ -253,7 +284,7 @@ OslSyntax::OslSyntax()
"isinf", "isfinite", "erf", "erfc", "cross", "dot", "length", "distance", "normalize", "faceforward",
"reflect", "fresnel", "transform", "transformu", "rotate", "luminance", "blackbody", "wavelength_color",
"transformc", "determinant", "transpose", "step", "smoothstep", "linearstep", "smooth_linearstep", "aastep",
"hash", "strlen", "getchar", "startswith", "endswith", "substr", "stof", "stoi", "concat", "textureresource",
"hash", "strlen", "getchar", "startswith", "endswith", "substr", "stof", "stoi", "concat", "textureresource", "textureresources",
"backfacing", "raytype", "iscameraray", "isdiffuseray", "isglossyray", "isshadowray", "getmatrix",
"emission", "background", "diffuse", "oren_nayer", "translucent", "phong", "ward", "microfacet",
"reflection", "transparent", "debug", "holdout", "subsurface", "sheen",
Expand Down Expand Up @@ -367,10 +398,19 @@ OslSyntax::OslSyntax()
std::make_shared<OSLFilenameTypeSyntax>(
"textureresource ",
"textureresource (\"\", \"\")",
"(\"\", \"\")",
"{\"\", \"\"}",
EMPTY_STRING,
"struct textureresource { string filename; string colorspace; };"));

registerTypeSyntax(
Type::FILENAMEARRAY,
std::make_shared<OSLFilenameArrayTypeSyntax>(
"textureresources ",
"textureresources ({}, \"\")",
"({}, \"\")",
EMPTY_STRING,
"struct textureresources { string filename[100]; string colorspace; };"));

registerTypeSyntax(
Type::BSDF,
std::make_shared<ScalarTypeSyntax>(
Expand Down
4 changes: 3 additions & 1 deletion source/MaterialXTest/MaterialXGenMdl/GenMdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class MdlShaderGeneratorTester : public GenShaderUtil::ShaderGeneratorTester
_skipNodeDefs.insert("ND_spot_light");
_skipNodeDefs.insert("ND_directional_light");
_skipNodeDefs.insert("ND_dot_");
_skipNodeDefs.insert("ND_elementat_");
ParentClass::addSkipNodeDefs();
}

Expand All @@ -51,6 +52,7 @@ class MdlShaderGeneratorTester : public GenShaderUtil::ShaderGeneratorTester
}

_skipFiles.insert("imagearray.mtlx");
_skipFiles.insert("elementat.mtlx");

ShaderGeneratorTester::addSkipFiles();
}
Expand Down Expand Up @@ -82,7 +84,7 @@ class MdlShaderGeneratorTester : public GenShaderUtil::ShaderGeneratorTester
"geompropvalue", "surfacematerial", "volumematerial",
"IM_absorption_vdf_", "IM_mix_vdf_", "IM_add_vdf_", "IM_multiply_vdf",
"IM_measured_edf_", "IM_blackbody_", "IM_conical_edf_",
"IM_displacement_", "IM_volume_", "IM_light_"
"IM_displacement_", "IM_volume_", "IM_light_", "IM_elementat_"
};
ShaderGeneratorTester::getImplementationWhiteList(whiteList);
}
Expand Down

0 comments on commit 388609e

Please sign in to comment.