From a74baf5c192eea22c6f7271fabacc6e8b691a1b5 Mon Sep 17 00:00:00 2001 From: Kai Rohmer Date: Thu, 2 Jan 2025 09:21:10 +0100 Subject: [PATCH] Add test cases for MDL custom nodes: #2125 --- .../libraries/custom/custom_inline_node.mtlx | 79 ++++++++++++++++++ .../custom/custom_inline_node_defaults.mtlx | 33 ++++++++ .../custom_inline_node_multiple_output.mtlx | 61 ++++++++++++++ .../custom_inline_node_reserved_words.mtlx | 40 +++++++++ .../libraries/custom/custom_node.mtlx | 67 +++++++++++++++ .../custom/custom_node_gen_versioning.mtlx | 34 ++++++++ .../custom/custom_node_multiple_output.mtlx | 38 +++++++++ .../custom/custom_node_reserved_words.mtlx | 64 ++++++++++++++ .../custom/custom_node_resources.mtlx | 50 +++++++++++ .../libraries/custom/example_procedural.mtlx | 83 +++++++++++++++++++ .../custom/mdl/mx_custom_library_1_6.mdl | 23 +++++ .../custom/mdl/mx_custom_library_1_7.mdl | 14 ++++ .../custom/mdl/mx_custom_library_1_8.mdl | 16 ++++ .../custom/mdl/mx_custom_library_1_9.mdl | 16 ++++ .../libraries/custom/mdl/mx_custom_node.mdl | 55 ++++++++++++ .../mdl/mx_custom_node_reserved_words.mdl | 34 ++++++++ .../MaterialXGenGlsl/GenGlsl.cpp | 7 +- .../MaterialXTest/MaterialXGenMdl/GenMdl.cpp | 16 +++- .../MaterialXTest/MaterialXGenMsl/GenMsl.cpp | 7 +- .../MaterialXTest/MaterialXGenOsl/GenOsl.cpp | 7 +- 20 files changed, 739 insertions(+), 5 deletions(-) create mode 100644 resources/Materials/TestSuite/libraries/custom/custom_inline_node.mtlx create mode 100644 resources/Materials/TestSuite/libraries/custom/custom_inline_node_defaults.mtlx create mode 100644 resources/Materials/TestSuite/libraries/custom/custom_inline_node_multiple_output.mtlx create mode 100644 resources/Materials/TestSuite/libraries/custom/custom_inline_node_reserved_words.mtlx create mode 100644 resources/Materials/TestSuite/libraries/custom/custom_node.mtlx create mode 100644 resources/Materials/TestSuite/libraries/custom/custom_node_gen_versioning.mtlx create mode 100644 resources/Materials/TestSuite/libraries/custom/custom_node_multiple_output.mtlx create mode 100644 resources/Materials/TestSuite/libraries/custom/custom_node_reserved_words.mtlx create mode 100644 resources/Materials/TestSuite/libraries/custom/custom_node_resources.mtlx create mode 100644 resources/Materials/TestSuite/libraries/custom/example_procedural.mtlx create mode 100644 resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_6.mdl create mode 100644 resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_7.mdl create mode 100644 resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_8.mdl create mode 100644 resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_9.mdl create mode 100644 resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_node.mdl create mode 100644 resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_node_reserved_words.mdl diff --git a/resources/Materials/TestSuite/libraries/custom/custom_inline_node.mtlx b/resources/Materials/TestSuite/libraries/custom/custom_inline_node.mtlx new file mode 100644 index 0000000000..fd10e12a8c --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/custom_inline_node.mtlx @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/Materials/TestSuite/libraries/custom/custom_inline_node_defaults.mtlx b/resources/Materials/TestSuite/libraries/custom/custom_inline_node_defaults.mtlx new file mode 100644 index 0000000000..dfe58b1d40 --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/custom_inline_node_defaults.mtlx @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/Materials/TestSuite/libraries/custom/custom_inline_node_multiple_output.mtlx b/resources/Materials/TestSuite/libraries/custom/custom_inline_node_multiple_output.mtlx new file mode 100644 index 0000000000..69413c5ba3 --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/custom_inline_node_multiple_output.mtlx @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/Materials/TestSuite/libraries/custom/custom_inline_node_reserved_words.mtlx b/resources/Materials/TestSuite/libraries/custom/custom_inline_node_reserved_words.mtlx new file mode 100644 index 0000000000..a6876cafcc --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/custom_inline_node_reserved_words.mtlx @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/Materials/TestSuite/libraries/custom/custom_node.mtlx b/resources/Materials/TestSuite/libraries/custom/custom_node.mtlx new file mode 100644 index 0000000000..fc959dbc43 --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/custom_node.mtlx @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/Materials/TestSuite/libraries/custom/custom_node_gen_versioning.mtlx b/resources/Materials/TestSuite/libraries/custom/custom_node_gen_versioning.mtlx new file mode 100644 index 0000000000..82586fce23 --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/custom_node_gen_versioning.mtlx @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/Materials/TestSuite/libraries/custom/custom_node_multiple_output.mtlx b/resources/Materials/TestSuite/libraries/custom/custom_node_multiple_output.mtlx new file mode 100644 index 0000000000..66165ba3c4 --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/custom_node_multiple_output.mtlx @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/Materials/TestSuite/libraries/custom/custom_node_reserved_words.mtlx b/resources/Materials/TestSuite/libraries/custom/custom_node_reserved_words.mtlx new file mode 100644 index 0000000000..7e59ca0e3d --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/custom_node_reserved_words.mtlx @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/Materials/TestSuite/libraries/custom/custom_node_resources.mtlx b/resources/Materials/TestSuite/libraries/custom/custom_node_resources.mtlx new file mode 100644 index 0000000000..6f2c181512 --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/custom_node_resources.mtlx @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/Materials/TestSuite/libraries/custom/example_procedural.mtlx b/resources/Materials/TestSuite/libraries/custom/example_procedural.mtlx new file mode 100644 index 0000000000..a7d6715dce --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/example_procedural.mtlx @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_6.mdl b/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_6.mdl new file mode 100644 index 0000000000..02d443d7c9 --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_6.mdl @@ -0,0 +1,23 @@ +// +// Copyright Contributors to the MaterialX Project +// SPDX-License-Identifier: Apache-2.0 +// + +// Note, this module has to be placed in an MDL search path in order to get picked up correctly. + +// MDL language version number +// This is NOT a version or revision number of the module content itself. +mdl 1.6; + +// Example function to test the version handling. +// Note, the changes here are only for testing! +// In general all versions should produce an output according to the node specification as good as possible. +// Increasing the language version should only be done when new language features allow to implement the +// specified behavior more precisely or more efficiently. +// The version number is NOT meant for versioning the nodules content itself, e.g., after adding new functions or fixing bugs). + +// just a simple example function +export color custom_library_function(float scale = 1.0) +{ + return color(0.25, 0.5, 0.75) * scale; +} diff --git a/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_7.mdl b/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_7.mdl new file mode 100644 index 0000000000..5217ac4c4d --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_7.mdl @@ -0,0 +1,14 @@ +// +// Copyright Contributors to the MaterialX Project +// SPDX-License-Identifier: Apache-2.0 +// + +// Note, this module has to be placed in an MDL search path in order to get picked up correctly. + +// MDL language version number +// This is NOT a version or revision number of the module content itself. +mdl 1.7; + +// no changes when upgrading the code generator to produce MDL 1.8 code +// forward all unchanged definitions from the previous version +export using .::mx_custom_library_1_6 import custom_library_function; diff --git a/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_8.mdl b/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_8.mdl new file mode 100644 index 0000000000..e3c00b6f95 --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_8.mdl @@ -0,0 +1,16 @@ +// +// Copyright Contributors to the MaterialX Project +// SPDX-License-Identifier: Apache-2.0 +// + +// Note, this module has to be placed in an MDL search path in order to get picked up correctly. + +// MDL language version number +// This is NOT a version or revision number of the module content itself. +mdl 1.8; + +// Note, have a look into mx_custom_library_1_6 for details. + +// no changes when upgrading the code generator to produce MDL 1.8 code +// forward all unchanged definitions from the previous version +export using .::mx_custom_library_1_7 import custom_library_function; diff --git a/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_9.mdl b/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_9.mdl new file mode 100644 index 0000000000..8f6068a87c --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_library_1_9.mdl @@ -0,0 +1,16 @@ +// +// Copyright Contributors to the MaterialX Project +// SPDX-License-Identifier: Apache-2.0 +// + +// Note, this module has to be placed in an MDL search path in order to get picked up correctly. + +// MDL language version number +// This is NOT a version or revision number of the module content itself. +mdl 1.9; + +// Note, have a look into mx_custom_library_1_6 for details. + +// no changes when upgrading the code generator to produce MDL 1.7 code +// forward all unchanged definitions from the previous version +export using .::mx_custom_library_1_8 import custom_library_function; diff --git a/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_node.mdl b/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_node.mdl new file mode 100644 index 0000000000..f547ba5a4d --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_node.mdl @@ -0,0 +1,55 @@ +// +// Copyright Contributors to the MaterialX Project +// SPDX-License-Identifier: Apache-2.0 +// + +// Note, this module has to be placed in an MDL search path in order to get picked up correctly. + + +// MDL language version number +// Select the version that supports the required features based on the MDL specification. +// Since the MDL generator in MaterialX requires MDL 1.6 it's a good default. +// If you have MDL functions in an older language version it's not required to increase the version number. +// If your module requires functionality of a higher version, `targetVersion` has to be adjusted accordingly +// during code generation in `GenMdlOption`. The default option always uses the latest version, +// which means no changes are required unless the option was set manually. +mdl 1.6; + +// MDL standard modules +import ::math::*; +import ::tex::*; +import ::state::*; + +// A simple example of a custom node implementation in MDL. +export color custom_node_function(float red, float green, float blue) +{ + return color(red * 0.25, green * 0.5, blue * 0.75); +} + +// Example to illustrate implementations fpr multiple output nodes in MDL. +// It uses a structure that might be defined locally. +// The actual name of struct is anonymous to the code code generator. +export struct custom_struct +{ + color tint = color(0.0); + float roughness = 0.0; + bool unused = false; // not part of the MaterialX node definition. +}; +export custom_struct custom_node_multiple_output_function(float scale) +{ + custom_struct output; + output.tint = color(0.1, scale, 0.1); + output.roughness = scale; + return output; +} + +// Example for nodes with resource parameters. +export color custom_node_image_luminance( + uniform texture_2d file = texture_2d(), + float2 texcoord = float2(::state::texture_coordinate(0).x, ::state::texture_coordinate(0).y) +) +{ + float3 c = tex::lookup_float3(tex: file, coord: texcoord); + float lum = math::dot(c, float3(0.212671f, 0.715160f, 0.072169f)); + return color(lum); +} diff --git a/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_node_reserved_words.mdl b/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_node_reserved_words.mdl new file mode 100644 index 0000000000..a0c3140794 --- /dev/null +++ b/resources/Materials/TestSuite/libraries/custom/mdl/mx_custom_node_reserved_words.mdl @@ -0,0 +1,34 @@ +// +// Copyright Contributors to the MaterialX Project +// SPDX-License-Identifier: Apache-2.0 +// + +// Note, this module has to be placed in an MDL search path in order to get picked up correctly. +// For all reserved words, please see the latest MDL Specification document. + +mdl 1.6; + +// "auto" is a reserved word in MDL. +// if the MaterialX node definition has a parameter that conflicts with such a reserved word, +// a prefix "mxp_" needs to be added. +// "a" is not a reserved word and no prefix is added. This allow to integrate existing MDL modules without changes. +export color reserved_parmeter_name_function(float a, float mxp_auto) +{ + return a * color(mxp_auto * 0.25, mxp_auto * 0.5, mxp_auto * 1.0); +} + +// "out" and "private" are also reserved words. +// A prefix "mxp_" needs to be added when an output name in the MaterialX node definition conflicts +// with a reserved word in MDL. +export struct custom_struct +{ + color mxp_out = color(0.0); + float mxp_private = 0.0; +}; +export custom_struct reserved_output_name_function(float scale) +{ + custom_struct output; + output.mxp_out = color(0.1, scale, 0.1); + output.mxp_private = scale; + return output; +} diff --git a/source/MaterialXTest/MaterialXGenGlsl/GenGlsl.cpp b/source/MaterialXTest/MaterialXGenGlsl/GenGlsl.cpp index 979320bd91..b62c2f815e 100644 --- a/source/MaterialXTest/MaterialXGenGlsl/GenGlsl.cpp +++ b/source/MaterialXTest/MaterialXGenGlsl/GenGlsl.cpp @@ -139,7 +139,12 @@ static void generateGlslCode(GlslType type) mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath(); mx::FilePathVec testRootPaths; - testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/libraries/metal")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/lights")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/locale")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/nprlib")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/pbrlib")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/stdlib")); testRootPaths.push_back(searchPath.find("resources/Materials/Examples/StandardSurface")); // Create the requested shader generator. diff --git a/source/MaterialXTest/MaterialXGenMdl/GenMdl.cpp b/source/MaterialXTest/MaterialXGenMdl/GenMdl.cpp index 4bec0016de..47c8842014 100644 --- a/source/MaterialXTest/MaterialXGenMdl/GenMdl.cpp +++ b/source/MaterialXTest/MaterialXGenMdl/GenMdl.cpp @@ -210,7 +210,13 @@ void MdlShaderGeneratorTester::preprocessDocument(mx::DocumentPtr doc) if (!_mdlCustomResolver) _mdlCustomResolver = MdlStringResolver::create(); - _mdlCustomResolver->initialize(doc, &_logFile, { _searchPath.asString() }); + mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath(); + + // add an additional search path for the custom MDL + // in an real-world application, the custom modules would be located in an already MDL search path + mx::FilePath customNodeTestSearchPath = searchPath.find("resources/Materials/TestSuite/libraries/custom/mdl"); + + _mdlCustomResolver->initialize(doc, &_logFile, { _searchPath.asString(), customNodeTestSearchPath.asString() }); mx::flattenFilenames(doc, _mdlCustomResolver->getMdlSearchPaths(), _mdlCustomResolver); } @@ -357,7 +363,13 @@ TEST_CASE("GenShader: MDL Shader Generation", "[genmdl]") mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath(); mx::FilePathVec testRootPaths; - testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/libraries/custom")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/libraries/metal")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/lights")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/locale")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/nprlib")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/pbrlib")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/stdlib")); testRootPaths.push_back(searchPath.find("resources/Materials/Examples/StandardSurface")); testRootPaths.push_back(searchPath.find("resources/Materials/Examples/UsdPreviewSurface")); diff --git a/source/MaterialXTest/MaterialXGenMsl/GenMsl.cpp b/source/MaterialXTest/MaterialXGenMsl/GenMsl.cpp index 32baaec5f0..2bdcb9bf91 100644 --- a/source/MaterialXTest/MaterialXGenMsl/GenMsl.cpp +++ b/source/MaterialXTest/MaterialXGenMsl/GenMsl.cpp @@ -122,7 +122,12 @@ static void generateMslCode() mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath(); mx::FilePathVec testRootPaths; - testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/libraries/metal")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/lights")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/locale")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/nprlib")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/pbrlib")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/stdlib")); testRootPaths.push_back(searchPath.find("resources/Materials/Examples/StandardSurface")); const mx::FilePath logPath("genmsl_msl23_layout_generate_test.txt"); diff --git a/source/MaterialXTest/MaterialXGenOsl/GenOsl.cpp b/source/MaterialXTest/MaterialXGenOsl/GenOsl.cpp index 6e613c85b8..3986e0ea47 100644 --- a/source/MaterialXTest/MaterialXGenOsl/GenOsl.cpp +++ b/source/MaterialXTest/MaterialXGenOsl/GenOsl.cpp @@ -182,7 +182,12 @@ static void generateOslCode() mx::FileSearchPath searchPath = mx::getDefaultDataSearchPath(); mx::FilePathVec testRootPaths; - testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/libraries/metal")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/lights")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/locale")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/nprlib")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/pbrlib")); + testRootPaths.push_back(searchPath.find("resources/Materials/TestSuite/stdlib")); testRootPaths.push_back(searchPath.find("resources/Materials/Examples/StandardSurface")); const mx::FilePath logPath("genosl_vanilla_generate_test.txt");