forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderStage.cpp
More file actions
397 lines (343 loc) · 9.81 KB
/
ShaderStage.cpp
File metadata and controls
397 lines (343 loc) · 9.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//
// Copyright Contributors to the MaterialX Project
// SPDX-License-Identifier: Apache-2.0
//
#include <MaterialXGenShader/ShaderStage.h>
#include <MaterialXGenShader/ShaderGenerator.h>
#include <MaterialXGenShader/GenContext.h>
#include <MaterialXGenShader/Syntax.h>
#include <MaterialXGenShader/Util.h>
#include <MaterialXCore/Value.h>
#include <MaterialXFormat/Util.h>
MATERIALX_NAMESPACE_BEGIN
namespace Stage
{
const string PIXEL = "pixel";
} // namespace Stage
//
// VariableBlock methods
//
ShaderPort* VariableBlock::operator[](const string& name)
{
ShaderPort* v = find(name);
if (!v)
{
throw ExceptionShaderGenError("No variable named '" + name + "' exists for block '" + getName() + "'");
}
return v;
}
const ShaderPort* VariableBlock::operator[](const string& name) const
{
return const_cast<VariableBlock*>(this)->operator[](name);
}
ShaderPort* VariableBlock::find(const string& name)
{
auto it = _variableMap.find(name);
return it != _variableMap.end() ? it->second.get() : nullptr;
}
const ShaderPort* VariableBlock::find(const string& name) const
{
return const_cast<VariableBlock*>(this)->find(name);
}
ShaderPort* VariableBlock::find(const ShaderPortPredicate& predicate)
{
for (ShaderPort* port : getVariableOrder())
{
if (predicate(port))
{
return port;
}
}
return nullptr;
}
ShaderPort* VariableBlock::add(const TypeDesc* type, const string& name, ValuePtr value)
{
auto it = _variableMap.find(name);
if (it != _variableMap.end())
{
return it->second.get();
}
ShaderPortPtr port = std::make_shared<ShaderPort>(nullptr, type, name, value);
_variableMap[name] = port;
_variableOrder.push_back(port.get());
return port.get();
}
void VariableBlock::add(ShaderPortPtr port)
{
if (!_variableMap.count(port->getName()))
{
_variableMap[port->getName()] = port;
_variableOrder.push_back(port.get());
}
}
//
// ShaderStage methods
//
ShaderStage::ShaderStage(const string& name, ConstSyntaxPtr syntax) :
_name(name),
_syntax(syntax),
_indentations(0),
_constants("Constants", "cn")
{
}
VariableBlockPtr ShaderStage::createUniformBlock(const string& name, const string& instance)
{
auto it = _uniforms.find(name);
if (it == _uniforms.end())
{
VariableBlockPtr b = std::make_shared<VariableBlock>(name, instance);
_uniforms[name] = b;
return b;
}
return it->second;
}
VariableBlockPtr ShaderStage::createInputBlock(const string& name, const string& instance)
{
auto it = _inputs.find(name);
if (it == _inputs.end())
{
VariableBlockPtr b = std::make_shared<VariableBlock>(name, instance);
_inputs[name] = b;
return b;
}
return it->second;
}
VariableBlockPtr ShaderStage::createOutputBlock(const string& name, const string& instance)
{
auto it = _outputs.find(name);
if (it == _outputs.end())
{
VariableBlockPtr b = std::make_shared<VariableBlock>(name, instance);
_outputs[name] = b;
return b;
}
return it->second;
}
VariableBlock& ShaderStage::getUniformBlock(const string& name)
{
auto it = _uniforms.find(name);
if (it == _uniforms.end())
{
throw ExceptionShaderGenError("No uniform block named '" + name + "' exists for shader stage '" + getName() + "'");
}
return *it->second;
}
const VariableBlock& ShaderStage::getUniformBlock(const string& name) const
{
return const_cast<ShaderStage*>(this)->getUniformBlock(name);
}
VariableBlock& ShaderStage::getInputBlock(const string& name)
{
auto it = _inputs.find(name);
if (it == _inputs.end())
{
throw ExceptionShaderGenError("No input block named '" + name + "' exists for shader stage '" + getName() + "'");
}
return *it->second;
}
const VariableBlock& ShaderStage::getInputBlock(const string& name) const
{
return const_cast<ShaderStage*>(this)->getInputBlock(name);
}
VariableBlock& ShaderStage::getOutputBlock(const string& name)
{
auto it = _outputs.find(name);
if (it == _outputs.end())
{
throw ExceptionShaderGenError("No output block named '" + name + "' exists for shader stage '" + getName() + "'");
}
return *it->second;
}
const VariableBlock& ShaderStage::getOutputBlock(const string& name) const
{
return const_cast<ShaderStage*>(this)->getOutputBlock(name);
}
VariableBlock& ShaderStage::getConstantBlock()
{
return _constants;
}
const VariableBlock& ShaderStage::getConstantBlock() const
{
return _constants;
}
void ShaderStage::beginScope(Syntax::Punctuation punc)
{
switch (punc)
{
case Syntax::CURLY_BRACKETS:
beginLine();
_code += "{" + _syntax->getNewline();
break;
case Syntax::PARENTHESES:
beginLine();
_code += "(" + _syntax->getNewline();
break;
case Syntax::SQUARE_BRACKETS:
beginLine();
_code += "[" + _syntax->getNewline();
break;
case Syntax::DOUBLE_SQUARE_BRACKETS:
beginLine();
_code += "[[" + _syntax->getNewline();
break;
}
++_indentations;
_scopes.push_back(Scope(punc));
}
void ShaderStage::endScope(bool semicolon, bool newline)
{
if (_scopes.empty())
{
throw ExceptionShaderGenError("End scope called with no scope active, please check your beginScope/endScope calls");
}
Syntax::Punctuation punc = _scopes.back().punctuation;
_scopes.pop_back();
--_indentations;
switch (punc)
{
case Syntax::CURLY_BRACKETS:
beginLine();
_code += "}";
break;
case Syntax::PARENTHESES:
beginLine();
_code += ")";
break;
case Syntax::SQUARE_BRACKETS:
beginLine();
_code += "]";
break;
case Syntax::DOUBLE_SQUARE_BRACKETS:
beginLine();
_code += "]]";
break;
}
if (semicolon)
_code += ";";
if (newline)
_code += _syntax->getNewline();
}
void ShaderStage::beginLine()
{
for (int i = 0; i < _indentations; ++i)
{
_code += _syntax->getIndentation();
}
}
void ShaderStage::endLine(bool semicolon)
{
if (semicolon)
{
_code += ";";
}
newLine();
}
void ShaderStage::newLine()
{
_code += _syntax->getNewline();
}
void ShaderStage::addString(const string& str)
{
_code += str;
}
void ShaderStage::addLine(const string& str, bool semicolon)
{
beginLine();
addString(str);
endLine(semicolon);
}
void ShaderStage::addComment(const string& str)
{
beginLine();
_code += _syntax->getSingleLineComment() + str;
endLine(false);
}
void ShaderStage::addBlock(const string& str, const FilePath& sourceFilename, GenContext& context)
{
const string& INCLUDE = _syntax->getIncludeStatement();
const string& QUOTE = _syntax->getStringQuote();
// Add each line in the block seperately to get correct indentation.
StringStream stream(str);
for (string line; std::getline(stream, line);)
{
size_t pos = line.find(INCLUDE);
if (pos != string::npos)
{
size_t startQuote = line.find_first_of(QUOTE);
size_t endQuote = line.find_last_of(QUOTE);
if (startQuote != string::npos && endQuote != string::npos && endQuote > startQuote)
{
size_t length = (endQuote - startQuote) - 1;
if (length)
{
const string filename = line.substr(startQuote + 1, length);
addInclude(filename, sourceFilename, context);
}
}
}
else
{
addLine(line, false);
}
}
}
void ShaderStage::addInclude(const FilePath& includeFilename, const FilePath& sourceFilename, GenContext& context)
{
string modifiedFile = includeFilename;
tokenSubstitution(context.getShaderGenerator().getTokenSubstitutions(), modifiedFile);
FilePath resolvedFile = context.resolveSourceFile(modifiedFile, sourceFilename.getParentPath());
if (!_includes.count(resolvedFile))
{
string content = readFile(resolvedFile);
if (content.empty())
{
throw ExceptionShaderGenError("Could not find include file: '" + includeFilename.asString() + "'");
}
_includes.insert(resolvedFile);
addBlock(content, resolvedFile, context);
}
}
void ShaderStage::addSourceDependency(const FilePath& file)
{
if (!_sourceDependencies.count(file))
{
_sourceDependencies.insert(file);
}
}
void ShaderStage::addFunctionDefinition(const ShaderNode& node, GenContext& context)
{
const ShaderNodeImpl& impl = node.getImplementation();
const size_t id = impl.getHash();
// Make sure it's not already defined.
if (!_definedFunctions.count(id))
{
_definedFunctions.insert(id);
impl.emitFunctionDefinition(node, context, *this);
}
}
void ShaderStage::addFunctionCall(const ShaderNode& node, GenContext& context, bool emitCode)
{
// Register this function as being called in the current scope.
const ClosureContext* cct = context.getClosureContext();
const FunctionCallId id(&node, cct ? cct->getType() : 0);
_scopes.back().functions.insert(id);
// Emit code for the function call if not omitted.
if (emitCode)
{
node.getImplementation().emitFunctionCall(node, context, *this);
}
}
bool ShaderStage::isEmitted(const ShaderNode& node, GenContext& context) const
{
const ClosureContext* cct = context.getClosureContext();
const FunctionCallId id(&node, cct ? cct->getType() : 0);
for (const Scope& s : _scopes)
{
if (s.functions.count(id))
{
return true;
}
}
return false;
}
MATERIALX_NAMESPACE_END