-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathCompoundNode.cpp
More file actions
216 lines (176 loc) · 7.69 KB
/
CompoundNode.cpp
File metadata and controls
216 lines (176 loc) · 7.69 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
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//
#include <MaterialXGenShader/Nodes/CompoundNode.h>
#include <MaterialXGenShader/Exception.h>
#include <MaterialXGenShader/ShaderGenerator.h>
#include <MaterialXGenShader/Util.h>
#include <MaterialXCore/Definition.h>
#include <MaterialXCore/Document.h>
#include <MaterialXCore/Library.h>
#include <MaterialXTrace/Tracing.h>
MATERIALX_NAMESPACE_BEGIN
ShaderNodeImplPtr CompoundNode::create()
{
return std::make_shared<CompoundNode>();
}
void CompoundNode::addClassification(ShaderNode& node) const
{
// Add classification from the graph implementation.
node.addClassification(_rootGraph->getClassification());
}
void CompoundNode::initialize(const InterfaceElement& element, GenContext& context)
{
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
MX_TRACE_SCOPE(Tracing::Category::ShaderGen, element.getName().c_str());
ShaderNodeImpl::initialize(element, context);
if (!element.isA<NodeGraph>())
{
throw ExceptionShaderGenError("Element '" + element.getName() + "' is not a node graph implementation");
}
const NodeGraph& graph = static_cast<const NodeGraph&>(element);
_functionName = graph.getName();
context.getShaderGenerator().getSyntax().makeValidName(_functionName);
// For compounds we do not want to publish all internal inputs
// so always use the reduced interface for this graph.
const ShaderInterfaceType oldShaderInterfaceType = context.getOptions().shaderInterfaceType;
context.getOptions().shaderInterfaceType = SHADER_INTERFACE_REDUCED;
_rootGraph = ShaderGraph::create(nullptr, graph, context);
context.getOptions().shaderInterfaceType = oldShaderInterfaceType;
// Set hash using the function name.
// TODO: Could be improved to include the full function signature.
_hash = std::hash<string>{}(_functionName);
}
void CompoundNode::createVariables(const ShaderNode&, GenContext& context, Shader& shader) const
{
// Gather shader inputs from all child nodes
for (const ShaderNode* childNode : _rootGraph->getNodes())
{
childNode->getImplementation().createVariables(*childNode, context, shader);
}
}
void CompoundNode::emitFunctionDefinition(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
{
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
MX_TRACE_SCOPE(Tracing::Category::ShaderGen, _functionName.c_str());
DEFINE_SHADER_STAGE(stage, Stage::PIXEL)
{
const ShaderGenerator& shadergen = context.getShaderGenerator();
// Emit functions for all child nodes
shadergen.emitFunctionDefinitions(*_rootGraph, context, stage);
// Begin function signature.
shadergen.emitLineBegin(stage);
shadergen.emitString("void " + _functionName + "(", stage);
shadergen.emitClosureDataParameter(node, context, stage);
// if (context.getShaderGenerator().nodeNeedsClosureData(node))
// {
// shadergen.emitString(HW::CLOSURE_DATA_TYPE + " " + HW::CLOSURE_DATA_ARG + ", ", stage);
// }
string delim;
// Add all inputs
for (ShaderGraphInputSocket* inputSocket : _rootGraph->getInputSockets())
{
shadergen.emitString(delim, stage);
shadergen.emitFunctionDefinitionParameter(inputSocket, false, context, stage);
delim = ", ";
}
// Add all outputs
for (ShaderGraphOutputSocket* outputSocket : _rootGraph->getOutputSockets())
{
shadergen.emitString(delim, stage);
shadergen.emitFunctionDefinitionParameter(outputSocket, true, context, stage);
delim = ", ";
}
// End function signature.
shadergen.emitString(")", stage);
shadergen.emitLineEnd(stage, false);
// Begin function body.
shadergen.emitFunctionBodyBegin(*_rootGraph, context, stage);
if (nodeOutputIsClosure(node))
{
// 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();
// Its important that the classification check here matches the logic inside
// nodeOutputIsClosure() used above.
if (upstream->getParent() == _rootGraph.get() &&
(upstream->hasClassification(ShaderNode::Classification::CLOSURE) ||
upstream->hasClassification(ShaderNode::Classification::SHADER) ||
upstream->hasClassification(ShaderNode::Classification::MATERIAL)))
{
shadergen.emitFunctionCall(*upstream, context, stage);
}
}
}
}
else
{
shadergen.emitFunctionCalls(*_rootGraph, context, stage);
}
// Emit final results
for (ShaderGraphOutputSocket* outputSocket : _rootGraph->getOutputSockets())
{
const string result = shadergen.getUpstreamResult(outputSocket, context);
shadergen.emitLine(outputSocket->getVariable() + " = " + result, stage);
}
// End function body.
shadergen.emitFunctionBodyEnd(*_rootGraph, context, stage);
}
}
void CompoundNode::emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
{
MX_TRACE_FUNCTION(Tracing::Category::ShaderGen);
MX_TRACE_SCOPE(Tracing::Category::ShaderGen, _functionName.c_str());
const ShaderGenerator& shadergen = context.getShaderGenerator();
DEFINE_SHADER_STAGE(stage, Stage::VERTEX)
{
// Emit function calls for all child nodes to the vertex shader stage
shadergen.emitFunctionCalls(*_rootGraph, context, stage);
}
DEFINE_SHADER_STAGE(stage, Stage::PIXEL)
{
if (nodeOutputIsClosure(node))
{
// Emit calls for any closure dependencies upstream from this nodedef
shadergen.emitDependentFunctionCalls(node, context, stage, ShaderNode::Classification::CLOSURE);
}
// Declare the output variables.
emitOutputVariables(node, context, stage);
// Begin function call.
shadergen.emitLineBegin(stage);
shadergen.emitString(_functionName + "(", stage);
// Add an argument for closure data if needed
shadergen.emitClosureDataArg(node, context, stage);
// if (context.getShaderGenerator().nodeNeedsClosureData(node))
// {
// shadergen.emitString(HW::CLOSURE_DATA_ARG + ", ", stage);
// }
string delim;
// Emit inputs.
for (ShaderInput* input : node.getInputs())
{
shadergen.emitString(delim, stage);
shadergen.emitInput(input, context, stage);
delim = ", ";
}
// Emit outputs.
for (size_t i = 0; i < node.numOutputs(); ++i)
{
shadergen.emitString(delim, stage);
shadergen.emitOutput(node.getOutput(i), false, false, context, stage);
delim = ", ";
}
// End function call
shadergen.emitString(")", stage);
shadergen.emitLineEnd(stage);
}
}
MATERIALX_NAMESPACE_END