-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathSourceCodeNode.cpp
More file actions
225 lines (189 loc) · 7.89 KB
/
SourceCodeNode.cpp
File metadata and controls
225 lines (189 loc) · 7.89 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
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//
#include <MaterialXGenShader/Nodes/SourceCodeNode.h>
#include <MaterialXGenShader/Exception.h>
#include <MaterialXGenShader/GenContext.h>
#include <MaterialXGenShader/ShaderNode.h>
#include <MaterialXGenShader/ShaderStage.h>
#include <MaterialXFormat/Util.h>
MATERIALX_NAMESPACE_BEGIN
namespace
{
const string INLINE_VARIABLE_PREFIX("{{");
const string INLINE_VARIABLE_SUFFIX("}}");
} // anonymous namespace
ShaderNodeImplPtr SourceCodeNode::create()
{
return std::make_shared<SourceCodeNode>();
}
void SourceCodeNode::resolveSourceCode(const InterfaceElement& element, GenContext& context)
{
const Implementation& impl = static_cast<const Implementation&>(element);
FilePath localPath = FilePath(impl.getActiveSourceUri()).getParentPath();
_sourceFilename = context.resolveSourceFile(impl.getAttribute("file"), localPath);
_functionSource = readFile(_sourceFilename);
if (_functionSource.empty())
{
throw ExceptionShaderGenError("Failed to get source code from file '" + _sourceFilename.asString() +
"' used by implementation '" + impl.getName() + "'");
}
}
void SourceCodeNode::initialize(const InterfaceElement& element, GenContext& context)
{
ShaderNodeImpl::initialize(element, context);
if (!element.isA<Implementation>())
{
throw ExceptionShaderGenError("Element '" + element.getName() + "' is not an Implementation element");
}
const Implementation& impl = static_cast<const Implementation&>(element);
// Get source code from either an attribute or a file.
_functionSource = impl.getAttribute("sourcecode");
if (_functionSource.empty())
{
resolveSourceCode(element, context);
}
// Find the function name to use
// If no function is given the source will be inlined.
_functionName = impl.getAttribute("function");
_inlined = _functionName.empty();
if (!_inlined)
{
// Make sure the function name is valid.
string validFunctionName = _functionName;
context.getShaderGenerator().getSyntax().makeValidName(validFunctionName);
if (_functionName != validFunctionName)
{
throw ExceptionShaderGenError("Function name '" + _functionName +
"' used by implementation '" + impl.getName() + "' is not a valid identifier.");
}
}
else
{
_functionSource = replaceSubstrings(_functionSource, { { "\n", "" } });
}
// Set hash using the function name.
// TODO: Could be improved to include the full function signature.
_hash = std::hash<string>{}(_functionName);
}
void SourceCodeNode::emitFunctionDefinition(const ShaderNode&, GenContext& context, ShaderStage& stage) const
{
if (_inlined || _functionSource.empty() || _sourceFilename.isEmpty())
{
return;
}
DEFINE_SHADER_STAGE(stage, Stage::PIXEL)
{
if (!stage.hasSourceDependency(_sourceFilename))
{
const ShaderGenerator& shadergen = context.getShaderGenerator();
shadergen.emitBlock(_functionSource, _sourceFilename, context, stage);
shadergen.emitLineBreak(stage);
stage.addSourceDependency(_sourceFilename);
}
}
}
void SourceCodeNode::emitFunctionCall(const ShaderNode& node, GenContext& context, ShaderStage& stage) const
{
DEFINE_SHADER_STAGE(stage, Stage::PIXEL)
{
const ShaderGenerator& shadergen = context.getShaderGenerator();
if (nodeOutputIsClosure(node))
{
// Emit calls for any closure dependencies upstream from this nodedef
shadergen.emitDependentFunctionCalls(node, context, stage, ShaderNode::Classification::CLOSURE);
}
if (_inlined)
{
// An inline function call
size_t pos = 0;
size_t i = _functionSource.find(INLINE_VARIABLE_PREFIX);
StringSet variableNames;
StringVec code;
while (i != string::npos)
{
code.push_back(_functionSource.substr(pos, i - pos));
size_t j = _functionSource.find(INLINE_VARIABLE_SUFFIX, i + 2);
if (j == string::npos)
{
throw ExceptionShaderGenError("Malformed inline expression in implementation for node " + node.getName());
}
const string variable = _functionSource.substr(i + 2, j - i - 2);
const ShaderInput* input = node.getInput(variable);
if (!input)
{
throw ExceptionShaderGenError("Could not find an input named '" + variable +
"' on node '" + node.getName() + "'");
}
if (input->getConnection())
{
code.push_back(shadergen.getUpstreamResult(input, context));
}
else
{
string variableName = node.getName() + "_" + input->getName() + "_tmp";
if (!variableNames.count(variableName))
{
ShaderPort v(nullptr, input->getType(), variableName, input->getValue());
shadergen.emitLineBegin(stage);
const Syntax& syntax = shadergen.getSyntax();
const string valueStr = (v.getValue() ? syntax.getValue(v.getType(), *v.getValue()) : syntax.getDefaultValue(v.getType()));
const string& qualifier = syntax.getConstantQualifier();
string str = qualifier.empty() ? EMPTY_STRING : qualifier + " ";
str += syntax.getTypeName(v.getType()) + " " + v.getVariable();
str += valueStr.empty() ? EMPTY_STRING : " = " + valueStr;
shadergen.emitString(str, stage);
shadergen.emitLineEnd(stage);
variableNames.insert(variableName);
}
code.push_back(variableName);
}
pos = j + 2;
i = _functionSource.find(INLINE_VARIABLE_PREFIX, pos);
}
code.push_back(_functionSource.substr(pos));
shadergen.emitLineBegin(stage);
shadergen.emitOutput(node.getOutput(), true, false, context, stage);
shadergen.emitString(" = ", stage);
for (const string& c : code)
{
shadergen.emitString(c, stage);
}
shadergen.emitLineEnd(stage);
}
else
{
// An ordinary source code function call
// Declare the output variables.
emitOutputVariables(node, context, stage);
shadergen.emitLineBegin(stage);
// Emit function name.
shadergen.emitString(_functionName + "(", stage);
shadergen.emitClosureDataArg(node, context, stage);
// if (context.getShaderGenerator().nodeNeedsClosureData(node))
// {
// shadergen.emitString(HW::CLOSURE_DATA_ARG + ", ", stage);
// }
string delim;
// Emit all inputs.
for (ShaderInput* input : node.getInputs())
{
shadergen.emitString(delim, stage);
shadergen.emitInput(input, context, stage);
delim = ", ";
}
// Emit node 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