Skip to content

Commit 1e5ff1d

Browse files
Improve material name handling in shader generation (AcademySoftwareFoundation#2804)
Convert names of shader graph nodes early to identifiers to increase the chance of a name without suffixes. This allows to render all materials from the testsuite and examples with MDL without the need to skip some of them.
1 parent 9c478d9 commit 1e5ff1d

6 files changed

Lines changed: 12 additions & 21 deletions

File tree

source/MaterialXGenMdl/MdlShaderGenerator.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,8 @@ ShaderPtr MdlShaderGenerator::generate(const string& name, ElementPtr element, G
186186
const ShaderGraphOutputSocket* outputSocket = graph.getOutputSocket(0);
187187
emitString("export material ", stage);
188188

189-
// Begin shader signature. Note that makeIdentifier() will sanitize the name.
189+
// Begin shader signature. Note that the function name is already sanitized.
190190
string functionName = shader->getName();
191-
_syntax->makeIdentifier(functionName, graph.getIdentifierMap());
192191
setFunctionName(functionName, stage);
193192
emitLine(functionName, stage, false);
194193
emitScopeBegin(stage, Syntax::PARENTHESES);

source/MaterialXGenOsl/OslShaderGenerator.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ ShaderPtr OslShaderGenerator::generate(const string& name, ElementPtr element, G
8080
emitString("shader ", stage);
8181
}
8282

83-
// Begin shader signature. Note that makeIdentifier() will sanitize the name.
83+
// Begin shader signature. Note that the function name is already sanitized.
8484
string functionName = shader->getName();
85-
_syntax->makeIdentifier(functionName, graph.getIdentifierMap());
8685
setFunctionName(functionName, stage);
8786
emitLine(functionName, stage, false);
8887

source/MaterialXGenShader/ShaderGraph.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ MATERIALX_NAMESPACE_BEGIN
1818
// ShaderGraph methods
1919
//
2020

21-
ShaderGraph::ShaderGraph(const ShaderGraph* parent, const string& name, ConstDocumentPtr document) :
22-
ShaderNode(parent, name),
21+
ShaderGraph::ShaderGraph(const ShaderGraph* parent, const string& name, ConstDocumentPtr document,
22+
GenContext& context)
23+
: ShaderNode(parent, name),
2324
_document(document)
2425
{
26+
context.getShaderGenerator().getSyntax().makeIdentifier(_name, getIdentifierMap());
2527
}
2628

2729
void ShaderGraph::addInputSockets(const InterfaceElement& elem, GenContext& context)
@@ -436,7 +438,7 @@ ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const NodeGraph& n
436438

437439
string graphName = nodeGraph.getName();
438440
context.getShaderGenerator().getSyntax().makeValidName(graphName);
439-
ShaderGraphPtr graph = std::make_shared<ShaderGraph>(parent, graphName, nodeGraph.getDocument());
441+
ShaderGraphPtr graph = std::make_shared<ShaderGraph>(parent, graphName, nodeGraph.getDocument(), context);
440442

441443
// Clear classification
442444
graph->_classification = 0;
@@ -495,7 +497,7 @@ ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const string& name
495497
throw ExceptionShaderGenError("Given output '" + output->getName() + "' has no interface valid for shader generation");
496498
}
497499

498-
graph = std::make_shared<ShaderGraph>(parent, name, element->getDocument());
500+
graph = std::make_shared<ShaderGraph>(parent, name, element->getDocument(), context);
499501

500502
// Clear classification
501503
graph->_classification = 0;
@@ -531,7 +533,7 @@ ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const string& name
531533
throw ExceptionShaderGenError("Could not find a nodedef for node '" + node->getName() + "'");
532534
}
533535

534-
graph = std::make_shared<ShaderGraph>(parent, name, element->getDocument());
536+
graph = std::make_shared<ShaderGraph>(parent, name, element->getDocument(), context);
535537

536538
// Create input sockets
537539
graph->addInputSockets(*nodeDef, context);

source/MaterialXGenShader/ShaderGraph.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class MX_GENSHADER_API ShaderGraph : public ShaderNode
4444
{
4545
public:
4646
/// Constructor.
47-
ShaderGraph(const ShaderGraph* parent, const string& name, ConstDocumentPtr document);
47+
ShaderGraph(const ShaderGraph* parent, const string& name, ConstDocumentPtr document,
48+
GenContext& context);
4849

4950
/// Destructor.
5051
virtual ~ShaderGraph() { }

source/MaterialXTest/MaterialXRenderMdl/RenderMdl.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ class MdlShaderRenderTester : public RenderUtil::ShaderRenderTester
4242
const std::string& outputPath = ".",
4343
mx::ImageVec* imageVec = nullptr) override;
4444

45-
void addSkipFiles() override
46-
{
47-
// The command-line assembled in runRenderer() assumes that the simple name of the MDL
48-
// material is the same as "shaderName", but for this file the MDL material name gets a "1"
49-
// suffix. Communicating the generated name from the stage to the shader could help, but in
50-
// general it is probably better to give the main object a predictable name and use suffixes
51-
// for internal objects.
52-
_skipFiles.insert("translucent_bsdf.mtlx");
53-
_skipFiles.insert("blur.mtlx");
54-
}
55-
5645
mx::DocumentPtr _last_doc;
5746
};
5847

source/MaterialXTest/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ When rendering tests are enabled through the `MATERIALX_TEST_RENDER` option, the
6767
- Set the following build options to enable MDL support:
6868
- `MATERIALX_MDL_SDK_DIR`: Path to the MDL SDK directory (containing 'include', 'lib', etc.).
6969
- MDL versions 1.6 and later are supported.
70+
- For installations via vcpkg: the core `mdl-sdk` package is sufficient for syntax checks via `mdlc`. The `df_vulkan` test render binary needs the package features `dds`, `openimageio`, and `df-vulkan`.
7071
- `MSL`:
7172
- Metal Shading Language (MSL) 2.0 and later are supported.
7273
- Minimum tested operating system version is macOS Catalina 10.15.7

0 commit comments

Comments
 (0)