-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathClosureCompoundNodeMdl.cpp
More file actions
186 lines (155 loc) · 7.81 KB
/
ClosureCompoundNodeMdl.cpp
File metadata and controls
186 lines (155 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//
#include <MaterialXGenMdl/Nodes/ClosureCompoundNodeMdl.h>
#include <MaterialXGenMdl/MdlSyntax.h>
#include <MaterialXGenShader/ShaderGenerator.h>
#include <MaterialXGenShader/Util.h>
#include <MaterialXCore/Definition.h>
MATERIALX_NAMESPACE_BEGIN
ShaderNodeImplPtr ClosureCompoundNodeMdl::create(std::unique_ptr<NodeGraphPermutation> permutation)
{
return std::make_shared<ClosureCompoundNodeMdl>(std::move(permutation));
}
ClosureCompoundNodeMdl::ClosureCompoundNodeMdl(std::unique_ptr<NodeGraphPermutation> permutation) :
CompoundNodeMdl(std::move(permutation))
{
}
void ClosureCompoundNodeMdl::addClassification(ShaderNode& node) const
{
// Add classification from the graph implementation.
node.addClassification(_rootGraph->getClassification());
}
void ClosureCompoundNodeMdl::emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
{
DEFINE_SHADER_STAGE(stage, Stage::PIXEL)
{
const ShaderGenerator& shadergen = context.getShaderGenerator();
const MdlSyntax& mdlSyntax = static_cast<const MdlSyntax&>(shadergen.getSyntax());
// Emit functions for all child nodes
shadergen.emitFunctionDefinitions(*_rootGraph, context, stage);
// split all fields into separate functions
if (!_returnStruct.empty() && _unrollReturnStructMembers)
{
// make sure the upstream definitions are known
for (const ShaderGraphOutputSocket* outputSocket : _rootGraph->getOutputSockets())
{
if (!outputSocket->getConnection())
continue;
const ShaderNode* upstream = outputSocket->getConnection()->getNode();
const bool isMaterialExpr = (upstream->hasClassification(ShaderNode::Classification::CLOSURE) ||
upstream->hasClassification(ShaderNode::Classification::SHADER));
// since the emit functions are const, the field name to generate a function for is passed via context
const std::string& fieldName = outputSocket->getName();
GenUserDataStringPtr fieldNamePtr = std::make_shared<GenUserDataString>(fieldName);
context.pushUserData(CompoundNodeMdl::GEN_USER_DATA_RETURN_STRUCT_FIELD_NAME, fieldNamePtr);
// Emit function signature.
shadergen.emitComment("unrolled structure field: " + _returnStruct + "." + fieldName + " (name=\"" + node.getName() + "\")", stage);
emitFunctionSignature(node, context, stage);
// Special case for material expressions.
if (isMaterialExpr)
{
shadergen.emitLine(" = let", stage, false);
}
// Function body.
shadergen.emitScopeBegin(stage);
// Emit all texturing nodes. These are inputs to the
// closure nodes and need to be emitted first.
shadergen.emitFunctionCalls(*_rootGraph, context, stage, ShaderNode::Classification::TEXTURE);
// Emit function calls for internal closures nodes connected to the graph sockets.
// These will in turn emit function calls for any dependent closure nodes upstream.
if (upstream->getParent() == _rootGraph.get() &&
(upstream->hasClassification(ShaderNode::Classification::CLOSURE) || upstream->hasClassification(ShaderNode::Classification::SHADER)))
{
shadergen.emitFunctionCall(*upstream, context, stage);
}
// Emit final results
if (isMaterialExpr)
{
shadergen.emitScopeEnd(stage);
const string result = shadergen.getUpstreamResult(outputSocket, context);
shadergen.emitLine("in material(" + result + ")", stage);
}
else
{
const string result = shadergen.getUpstreamResult(outputSocket, context);
shadergen.emitLine("return " + result, stage);
}
shadergen.emitLineBreak(stage);
context.popUserData(CompoundNodeMdl::GEN_USER_DATA_RETURN_STRUCT_FIELD_NAME);
}
return;
}
const bool isMaterialExpr = (_rootGraph->hasClassification(ShaderNode::Classification::CLOSURE) ||
_rootGraph->hasClassification(ShaderNode::Classification::SHADER));
// Emit function signature.
emitFunctionSignature(node, context, stage);
// Special case for material expressions.
if (isMaterialExpr)
{
shadergen.emitLine(" = let", stage, false);
}
// Function body.
shadergen.emitScopeBegin(stage);
// Emit all texturing nodes. These are inputs to the
// closure nodes and need to be emitted first.
shadergen.emitFunctionCalls(*_rootGraph, context, stage, ShaderNode::Classification::TEXTURE);
// Emit function calls for internal closures nodes connected to the graph sockets.
// These will in turn emit function calls for any dependent closure nodes upstream.
for (ShaderGraphOutputSocket* outputSocket : _rootGraph->getOutputSockets())
{
if (outputSocket->getConnection())
{
const ShaderNode* upstream = outputSocket->getConnection()->getNode();
if (upstream->getParent() == _rootGraph.get() &&
(upstream->hasClassification(ShaderNode::Classification::CLOSURE) || upstream->hasClassification(ShaderNode::Classification::SHADER)))
{
shadergen.emitFunctionCall(*upstream, context, stage);
}
}
}
// Emit final results
if (isMaterialExpr)
{
shadergen.emitScopeEnd(stage);
const ShaderGraphOutputSocket* outputSocket = _rootGraph->getOutputSocket();
const string result = shadergen.getUpstreamResult(outputSocket, context);
shadergen.emitLine("in material(" + result + ")", stage);
}
else
{
if (!_returnStruct.empty())
{
const string resultVariableName = "result__";
shadergen.emitLine(_returnStruct + " " + resultVariableName, stage);
for (const ShaderGraphOutputSocket* output : _rootGraph->getOutputSockets())
{
const string result = shadergen.getUpstreamResult(output, context);
shadergen.emitLine(resultVariableName + mdlSyntax.modifyPortName(output->getName()) + " = " + result, stage);
}
shadergen.emitLine("return " + resultVariableName, stage);
}
else
{
const ShaderGraphOutputSocket* outputSocket = _rootGraph->getOutputSocket();
const string result = shadergen.getUpstreamResult(outputSocket, context);
shadergen.emitLine("return " + result, stage);
}
shadergen.emitScopeEnd(stage);
}
shadergen.emitLineBreak(stage);
}
}
void ClosureCompoundNodeMdl::emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
{
DEFINE_SHADER_STAGE(stage, Stage::PIXEL)
{
const ShaderGenerator& shadergen = context.getShaderGenerator();
// First emit calls for any closure dependencies upstream from this node.
shadergen.emitDependentFunctionCalls(node, context, stage, ShaderNode::Classification::CLOSURE);
// Then emit this nodes function call.
CompoundNodeMdl::emitFunctionCall(node, context, stage);
}
}
MATERIALX_NAMESPACE_END