From 388609e2d49af43a6679cce5a2a40867cb67aa3a Mon Sep 17 00:00:00 2001 From: Lee Kerley Date: Fri, 20 Dec 2024 13:22:54 -0800 Subject: [PATCH] Add elementat node for filename array and nodegraph implementation of imagearray - to hopefully make this an easier adoption. --- .../stdlib/genglsl/stdlib_genglsl_impl.mtlx | 2 + .../stdlib/genmdl/stdlib_genmdl_impl.mtlx | 2 + .../stdlib/genmsl/stdlib_genmsl_impl.mtlx | 2 + .../genosl/mx_elementat_filenamearray.osl | 8 ++ .../stdlib/genosl/stdlib_genosl_impl.mtlx | 2 + libraries/stdlib/stdlib_defs.mtlx | 6 ++ libraries/stdlib/stdlib_ng.mtlx | 96 +++++++++++++++++++ .../TestSuite/stdlib/channel/elementat.mtlx | 10 ++ source/MaterialXGenOsl/OslSyntax.cpp | 44 ++++++++- source/MaterialXTest/MaterialXGenMdl/GenMdl.h | 4 +- 10 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 libraries/stdlib/genosl/mx_elementat_filenamearray.osl create mode 100644 resources/Materials/TestSuite/stdlib/channel/elementat.mtlx diff --git a/libraries/stdlib/genglsl/stdlib_genglsl_impl.mtlx b/libraries/stdlib/genglsl/stdlib_genglsl_impl.mtlx index f7e47b369e..98a098b59e 100644 --- a/libraries/stdlib/genglsl/stdlib_genglsl_impl.mtlx +++ b/libraries/stdlib/genglsl/stdlib_genglsl_impl.mtlx @@ -61,6 +61,8 @@ + + diff --git a/libraries/stdlib/genmdl/stdlib_genmdl_impl.mtlx b/libraries/stdlib/genmdl/stdlib_genmdl_impl.mtlx index 6ac2b862dd..da226410f8 100644 --- a/libraries/stdlib/genmdl/stdlib_genmdl_impl.mtlx +++ b/libraries/stdlib/genmdl/stdlib_genmdl_impl.mtlx @@ -41,6 +41,8 @@ + + diff --git a/libraries/stdlib/genmsl/stdlib_genmsl_impl.mtlx b/libraries/stdlib/genmsl/stdlib_genmsl_impl.mtlx index 473bcac064..0e7a078e66 100644 --- a/libraries/stdlib/genmsl/stdlib_genmsl_impl.mtlx +++ b/libraries/stdlib/genmsl/stdlib_genmsl_impl.mtlx @@ -61,6 +61,8 @@ + + diff --git a/libraries/stdlib/genosl/mx_elementat_filenamearray.osl b/libraries/stdlib/genosl/mx_elementat_filenamearray.osl new file mode 100644 index 0000000000..1b744bf65f --- /dev/null +++ b/libraries/stdlib/genosl/mx_elementat_filenamearray.osl @@ -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; +} diff --git a/libraries/stdlib/genosl/stdlib_genosl_impl.mtlx b/libraries/stdlib/genosl/stdlib_genosl_impl.mtlx index db318976c5..2c222ddd1f 100644 --- a/libraries/stdlib/genosl/stdlib_genosl_impl.mtlx +++ b/libraries/stdlib/genosl/stdlib_genosl_impl.mtlx @@ -41,6 +41,8 @@ + + diff --git a/libraries/stdlib/stdlib_defs.mtlx b/libraries/stdlib/stdlib_defs.mtlx index 1d659b9650..23b912acd8 100644 --- a/libraries/stdlib/stdlib_defs.mtlx +++ b/libraries/stdlib/stdlib_defs.mtlx @@ -268,6 +268,12 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/Materials/TestSuite/stdlib/channel/elementat.mtlx b/resources/Materials/TestSuite/stdlib/channel/elementat.mtlx new file mode 100644 index 0000000000..414693bc57 --- /dev/null +++ b/resources/Materials/TestSuite/stdlib/channel/elementat.mtlx @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/source/MaterialXGenOsl/OslSyntax.cpp b/source/MaterialXGenOsl/OslSyntax.cpp index a85821c4fd..1b1f2f17fc 100644 --- a/source/MaterialXGenOsl/OslSyntax.cpp +++ b/source/MaterialXGenOsl/OslSyntax.cpp @@ -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"; @@ -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", @@ -367,10 +398,19 @@ OslSyntax::OslSyntax() std::make_shared( "textureresource ", "textureresource (\"\", \"\")", - "(\"\", \"\")", + "{\"\", \"\"}", EMPTY_STRING, "struct textureresource { string filename; string colorspace; };")); + registerTypeSyntax( + Type::FILENAMEARRAY, + std::make_shared( + "textureresources ", + "textureresources ({}, \"\")", + "({}, \"\")", + EMPTY_STRING, + "struct textureresources { string filename[100]; string colorspace; };")); + registerTypeSyntax( Type::BSDF, std::make_shared( diff --git a/source/MaterialXTest/MaterialXGenMdl/GenMdl.h b/source/MaterialXTest/MaterialXGenMdl/GenMdl.h index 46d0b586d5..25d506840c 100644 --- a/source/MaterialXTest/MaterialXGenMdl/GenMdl.h +++ b/source/MaterialXTest/MaterialXGenMdl/GenMdl.h @@ -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(); } @@ -51,6 +52,7 @@ class MdlShaderGeneratorTester : public GenShaderUtil::ShaderGeneratorTester } _skipFiles.insert("imagearray.mtlx"); + _skipFiles.insert("elementat.mtlx"); ShaderGeneratorTester::addSkipFiles(); } @@ -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); }