Skip to content

Commit cedb7e9

Browse files
committed
Consolidate ClosureCompoundNode.cpp with CompoundNode.cpp, similar to the previous work done for SourceCodeNode.cpp. Also added an additional helper call to consolidate the logic.
1 parent c1e7122 commit cedb7e9

10 files changed

Lines changed: 76 additions & 221 deletions

File tree

source/MaterialXGenGlsl/GlslShaderGenerator.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <MaterialXGenShader/Nodes/HwBitangentNode.h>
2828
#include <MaterialXGenShader/Nodes/HwFrameNode.h>
2929
#include <MaterialXGenShader/Nodes/HwViewDirectionNode.h>
30-
#include <MaterialXGenShader/Nodes/ClosureCompoundNode.h>
3130

3231
MATERIALX_NAMESPACE_BEGIN
3332

@@ -740,10 +739,6 @@ ShaderNodeImplPtr GlslShaderGenerator::getImplementation(const NodeDef& nodedef,
740739
{
741740
impl = LightCompoundNodeGlsl::create();
742741
}
743-
else if (outputType.isClosure())
744-
{
745-
impl = ClosureCompoundNode::create();
746-
}
747742
else
748743
{
749744
impl = CompoundNode::create();

source/MaterialXGenMsl/MslShaderGenerator.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <MaterialXGenShader/Nodes/HwBitangentNode.h>
2828
#include <MaterialXGenShader/Nodes/HwFrameNode.h>
2929
#include <MaterialXGenShader/Nodes/HwViewDirectionNode.h>
30-
#include <MaterialXGenShader/Nodes/ClosureCompoundNode.h>
3130

3231
#include "MslResourceBindingContext.h"
3332

@@ -1255,10 +1254,6 @@ ShaderNodeImplPtr MslShaderGenerator::getImplementation(const NodeDef& nodedef,
12551254
{
12561255
impl = LightCompoundNodeMsl::create();
12571256
}
1258-
else if (outputType.isClosure())
1259-
{
1260-
impl = ClosureCompoundNode::create();
1261-
}
12621257
else
12631258
{
12641259
impl = CompoundNode::create();

source/MaterialXGenShader/Nodes/ClosureCompoundNode.cpp

Lines changed: 0 additions & 149 deletions
This file was deleted.

source/MaterialXGenShader/Nodes/ClosureCompoundNode.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

source/MaterialXGenShader/Nodes/CompoundNode.cpp

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ ShaderNodeImplPtr CompoundNode::create()
1919
return std::make_shared<CompoundNode>();
2020
}
2121

22+
void CompoundNode::addClassification(ShaderNode& node) const
23+
{
24+
// Add classification from the graph implementation.
25+
node.addClassification(_rootGraph->getClassification());
26+
}
27+
2228
void CompoundNode::initialize(const InterfaceElement& element, GenContext& context)
2329
{
2430
ShaderNodeImpl::initialize(element, context);
@@ -54,7 +60,7 @@ void CompoundNode::createVariables(const ShaderNode&, GenContext& context, Shade
5460
}
5561
}
5662

57-
void CompoundNode::emitFunctionDefinition(const ShaderNode&, GenContext& context, ShaderStage& stage) const
63+
void CompoundNode::emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
5864
{
5965
DEFINE_SHADER_STAGE(stage, Stage::PIXEL)
6066
{
@@ -66,9 +72,14 @@ void CompoundNode::emitFunctionDefinition(const ShaderNode&, GenContext& context
6672

6773
// Begin function signature.
6874
shadergen.emitLineBegin(stage);
69-
shadergen.emitString("void " + _functionName + +"(", stage);
75+
shadergen.emitString("void " + _functionName + "(", stage);
7076

71-
string delim = "";
77+
if (context.getShaderGenerator().nodeNeedsClosureData(node))
78+
{
79+
shadergen.emitString(HW::CLOSURE_DATA_TYPE + " " + HW::CLOSURE_DATA_ARG + ", ", stage);
80+
}
81+
82+
string delim;
7283

7384
// Add all inputs
7485
for (ShaderGraphInputSocket* inputSocket : _rootGraph->getInputSockets())
@@ -90,7 +101,32 @@ void CompoundNode::emitFunctionDefinition(const ShaderNode&, GenContext& context
90101

91102
// Begin function body.
92103
shadergen.emitFunctionBodyBegin(*_rootGraph, context, stage);
93-
shadergen.emitFunctionCalls(*_rootGraph, context, stage);
104+
105+
if (nodeOutputIsClosure(node))
106+
{
107+
// Emit all texturing nodes. These are inputs to the
108+
// closure nodes and need to be emitted first.
109+
shadergen.emitFunctionCalls(*_rootGraph, context, stage, ShaderNode::Classification::TEXTURE);
110+
111+
// Emit function calls for internal closures nodes connected to the graph sockets.
112+
// These will in turn emit function calls for any dependent closure nodes upstream.
113+
for (ShaderGraphOutputSocket* outputSocket : _rootGraph->getOutputSockets())
114+
{
115+
if (outputSocket->getConnection())
116+
{
117+
const ShaderNode* upstream = outputSocket->getConnection()->getNode();
118+
if (upstream->getParent() == _rootGraph.get() &&
119+
(upstream->hasClassification(ShaderNode::Classification::CLOSURE) || upstream->hasClassification(ShaderNode::Classification::SHADER)))
120+
{
121+
shadergen.emitFunctionCall(*upstream, context, stage);
122+
}
123+
}
124+
}
125+
}
126+
else
127+
{
128+
shadergen.emitFunctionCalls(*_rootGraph, context, stage);
129+
}
94130

95131
// Emit final results
96132
for (ShaderGraphOutputSocket* outputSocket : _rootGraph->getOutputSockets())
@@ -116,14 +152,26 @@ void CompoundNode::emitFunctionCall(const ShaderNode& node, GenContext& context,
116152

117153
DEFINE_SHADER_STAGE(stage, Stage::PIXEL)
118154
{
155+
if (nodeOutputIsClosure(node))
156+
{
157+
// Emit calls for any closure dependencies upstream from this nodedef
158+
shadergen.emitDependentFunctionCalls(node, context, stage, ShaderNode::Classification::CLOSURE);
159+
}
160+
119161
// Declare the output variables.
120162
emitOutputVariables(node, context, stage);
121163

122164
// Begin function call.
123165
shadergen.emitLineBegin(stage);
124166
shadergen.emitString(_functionName + "(", stage);
125167

126-
string delim = "";
168+
// Check if we have a closure context to modify the function call.
169+
if (context.getShaderGenerator().nodeNeedsClosureData(node))
170+
{
171+
shadergen.emitString(HW::CLOSURE_DATA_ARG + ", ", stage);
172+
}
173+
174+
string delim;
127175

128176
// Emit inputs.
129177
for (ShaderInput* input : node.getInputs())

source/MaterialXGenShader/Nodes/CompoundNode.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef MATERIALX_COMPOUNDNODE_H
77
#define MATERIALX_COMPOUNDNODE_H
88

9+
#include <MaterialXGenShader/GenContext.h>
910
#include <MaterialXGenShader/ShaderNodeImpl.h>
1011
#include <MaterialXGenShader/ShaderGraph.h>
1112
#include <MaterialXGenShader/Shader.h>
@@ -20,6 +21,8 @@ class MX_GENSHADER_API CompoundNode : public ShaderNodeImpl
2021

2122
void initialize(const InterfaceElement& element, GenContext& context) override;
2223

24+
void addClassification(ShaderNode& node) const override;
25+
2326
void createVariables(const ShaderNode& node, GenContext& context, Shader& shader) const override;
2427

2528
void emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const override;

source/MaterialXGenShader/Nodes/SourceCodeNode.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,7 @@ void SourceCodeNode::emitFunctionCall(const ShaderNode& node, GenContext& contex
109109
{
110110
const ShaderGenerator& shadergen = context.getShaderGenerator();
111111

112-
const auto& outputs = node.getOutputs();
113-
if (outputs.empty())
114-
{
115-
// This should never happen as we auto populate the default 'out' output based on the
116-
// node type if no output is present.
117-
throw ExceptionShaderGenError("Node has no outputs defined'");
118-
}
119-
120-
const TypeDesc outputType = outputs[0]->getType();
121-
if (outputType.isClosure())
112+
if (nodeOutputIsClosure(node))
122113
{
123114
// Emit calls for any closure dependencies upstream from this nodedef
124115
shadergen.emitDependentFunctionCalls(node, context, stage, ShaderNode::Classification::CLOSURE);

source/MaterialXGenShader/ShaderGenerator.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <MaterialXGenShader/ShaderNodeImpl.h>
1010
#include <MaterialXGenShader/Nodes/CompoundNode.h>
1111
#include <MaterialXGenShader/Nodes/SourceCodeNode.h>
12-
#include <MaterialXGenShader/Nodes/ClosureCompoundNode.h>
1312
#include <MaterialXGenShader/Util.h>
1413

1514
#include <MaterialXFormat/File.h>
@@ -290,25 +289,9 @@ ShaderNodeImplPtr ShaderGenerator::getImplementation(const NodeDef& nodedef, Gen
290289
return impl;
291290
}
292291

293-
vector<OutputPtr> outputs = nodedef.getActiveOutputs();
294-
if (outputs.empty())
295-
{
296-
throw ExceptionShaderGenError("NodeDef '" + nodedef.getName() + "' has no outputs defined");
297-
}
298-
299-
const TypeDesc outputType = _typeSystem->getType(outputs[0]->getType());
300-
301292
if (implElement->isA<NodeGraph>())
302293
{
303-
// Use a compound implementation.
304-
if (outputType.isClosure())
305-
{
306-
impl = ClosureCompoundNode::create();
307-
}
308-
else
309-
{
310-
impl = CompoundNode::create();
311-
}
294+
impl = CompoundNode::create();
312295
}
313296
else if (implElement->isA<Implementation>())
314297
{

0 commit comments

Comments
 (0)