-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathCompoundNodeMdl.cpp
More file actions
283 lines (239 loc) · 10.5 KB
/
CompoundNodeMdl.cpp
File metadata and controls
283 lines (239 loc) · 10.5 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//
#include <MaterialXGenMdl/Nodes/CompoundNodeMdl.h>
#include <MaterialXGenMdl/MdlShaderGenerator.h>
#include <MaterialXGenMdl/MdlSyntax.h>
#include <MaterialXGenShader/ShaderGenerator.h>
#include <MaterialXGenShader/Util.h>
#include <MaterialXCore/Definition.h>
MATERIALX_NAMESPACE_BEGIN
const string CompoundNodeMdl::GEN_USER_DATA_RETURN_STRUCT_FIELD_NAME = "returnStructFieldName";
ShaderNodeImplPtr CompoundNodeMdl::create(std::unique_ptr<NodeGraphPermutation> permutation)
{
return std::make_shared<CompoundNodeMdl>(std::move(permutation));
}
CompoundNodeMdl::CompoundNodeMdl(std::unique_ptr<NodeGraphPermutation> permutation) :
CompoundNode(std::move(permutation))
{
}
void CompoundNodeMdl::initialize(const InterfaceElement& element, GenContext& context)
{
CompoundNode::initialize(element, context);
_returnStruct = EMPTY_STRING;
if (_rootGraph->getOutputSockets().size() > 1)
{
_returnStruct = _functionName + "__result";
}
// Materials can not be members of structs. Identify this case in order to handle it.
for (const ShaderGraphOutputSocket* output : _rootGraph->getOutputSockets())
{
if (output->getType().getSemantic() == TypeDesc::SEMANTIC_SHADER)
{
_unrollReturnStructMembers = true;
}
}
}
void CompoundNodeMdl::emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
{
DEFINE_SHADER_STAGE(stage, Stage::PIXEL)
{
const ShaderGenerator& shadergen = context.getShaderGenerator();
const MdlSyntax& syntax = static_cast<const MdlSyntax&>(shadergen.getSyntax());
const bool isMaterialExpr = (_rootGraph->hasClassification(ShaderNode::Classification::CLOSURE) ||
_rootGraph->hasClassification(ShaderNode::Classification::SHADER));
// Emit functions for all child nodes
shadergen.emitFunctionDefinitions(*_rootGraph, context, stage);
// Emit function signature.
emitFunctionSignature(node, context, stage);
// Special case for material expressions.
if (isMaterialExpr)
{
shadergen.emitLine(" = let", stage, false);
}
// Function body.
shadergen.emitScopeBegin(stage);
shadergen.emitFunctionCalls(*_rootGraph, 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 + "." + syntax.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 CompoundNodeMdl::emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
{
DEFINE_SHADER_STAGE(stage, Stage::PIXEL)
{
const ShaderGenerator& shadergen = context.getShaderGenerator();
const Syntax& syntax = shadergen.getSyntax();
// Begin function call.
if (!_returnStruct.empty())
{
// when unrolling structure members, the call that creates the struct needs to skipped
if (_unrollReturnStructMembers)
{
// make sure the upstream definitions are known
shadergen.emitComment("fill unrolled structure fields: " + _returnStruct + " (name=\"" + node.getName() + "\")", stage);
for (const ShaderGraphOutputSocket* outputSocket : _rootGraph->getOutputSockets())
{
if (!outputSocket->getConnection())
continue;
const std::string& fieldName = outputSocket->getName();
// Emit the struct field.
const string& outputType = syntax.getTypeName(outputSocket->getType());
const string resultVariableName = node.getName() + "__" + fieldName;
shadergen.emitLineBegin(stage);
shadergen.emitString(outputType + " " + resultVariableName + " = ", stage);
shadergen.emitString(_functionName + "__" + fieldName + "(", stage);
// Emit inputs.
string delim = "";
for (ShaderInput* input : node.getInputs())
{
shadergen.emitString(delim, stage);
shadergen.emitInput(input, context, stage);
delim = ", ";
}
// End function call
shadergen.emitString(")", stage);
shadergen.emitLineEnd(stage);
}
return;
}
// Emit the struct multioutput.
const string resultVariableName = node.getName() + "_result";
shadergen.emitLineBegin(stage);
shadergen.emitString(_returnStruct + " " + resultVariableName + " = ", stage);
}
else
{
// Emit the single output.
shadergen.emitLineBegin(stage);
shadergen.emitOutput(node.getOutput(0), true, false, context, stage);
shadergen.emitString(" = ", stage);
}
shadergen.emitString(_functionName + "(", stage);
// Emit inputs.
string delim = "";
for (ShaderInput* input : node.getInputs())
{
shadergen.emitString(delim, stage);
shadergen.emitInput(input, context, stage);
delim = ", ";
}
// End function call
shadergen.emitString(")", stage);
shadergen.emitLineEnd(stage);
}
}
void CompoundNodeMdl::emitFunctionSignature(const ShaderNode&, GenContext& context, ShaderStage& stage) const
{
const ShaderGenerator& shadergen = context.getShaderGenerator();
const MdlSyntax& syntax = static_cast<const MdlSyntax&>(shadergen.getSyntax());
if (!_returnStruct.empty())
{
if (_unrollReturnStructMembers)
{
const auto fieldName = context.getUserData<GenUserDataString>(GEN_USER_DATA_RETURN_STRUCT_FIELD_NAME);
if (fieldName)
{
// Begin function signature.
const ShaderGraphOutputSocket* outputSocket = _rootGraph->getOutputSocket(fieldName->getValue());
const string& outputType = syntax.getTypeName(outputSocket->getType());
shadergen.emitLine(outputType + " " + _functionName + "__" + fieldName->getValue(), stage, false);
}
else
{
throw Exception("Error during transformation of struct: " + _returnStruct);
}
}
else
{
// Define the output struct.
shadergen.emitLine("struct " + _returnStruct, stage, false);
shadergen.emitScopeBegin(stage, Syntax::CURLY_BRACKETS);
for (const ShaderGraphOutputSocket* output : _rootGraph->getOutputSockets())
{
shadergen.emitLine(syntax.getTypeName(output->getType()) + " " + syntax.modifyPortName(output->getName()), stage);
}
shadergen.emitScopeEnd(stage, true);
shadergen.emitLineBreak(stage);
// Begin function signature.
shadergen.emitLine(_returnStruct + " " + _functionName, stage, false);
}
}
else
{
// Begin function signature.
const ShaderGraphOutputSocket* outputSocket = _rootGraph->getOutputSocket();
const string& outputType = syntax.getTypeName(outputSocket->getType());
shadergen.emitLine(outputType + " " + _functionName, stage, false);
}
shadergen.emitScopeBegin(stage, Syntax::PARENTHESES);
const string uniformPrefix = syntax.getUniformQualifier() + " ";
// Emit all inputs
int count = int(_rootGraph->numInputSockets());
for (ShaderGraphInputSocket* input : _rootGraph->getInputSockets())
{
const string& qualifier = input->isUniform() || input->getType() == Type::FILENAME ? uniformPrefix : EMPTY_STRING;
const string& type = syntax.getTypeName(input->getType());
string value = input->getValue() ? syntax.getValue(input->getType(), *input->getValue(), true) : EMPTY_STRING;
const string& geomprop = input->getGeomProp();
if (!geomprop.empty())
{
auto it = MdlShaderGenerator::GEOMPROP_DEFINITIONS.find(geomprop);
if (it != MdlShaderGenerator::GEOMPROP_DEFINITIONS.end())
{
value = it->second;
}
}
if (value.empty())
{
value = syntax.getDefaultValue(input->getType(), true);
}
const string& delim = --count > 0 ? Syntax::COMMA : EMPTY_STRING;
shadergen.emitLineBegin(stage);
shadergen.emitString(qualifier + type + " " + input->getVariable() + " = " + value, stage);
if (input->getConnections().empty())
{
shadergen.emitLineEnd(stage, false);
shadergen.emitLine("[[", stage, false);
shadergen.emitLineBegin(stage);
shadergen.emitString(syntax.getIndentation() + "anno::unused()", stage);
shadergen.emitLineEnd(stage, false);
shadergen.emitLineBegin(stage);
shadergen.emitString("]]", stage);
}
shadergen.emitString(delim, stage);
shadergen.emitLineEnd(stage, false);
}
// End function signature.
shadergen.emitScopeEnd(stage);
}
MATERIALX_NAMESPACE_END