forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderNode.cpp
568 lines (505 loc) · 17.9 KB
/
ShaderNode.cpp
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXGenShader/ShaderNode.h>
#include <MaterialXGenShader/GenContext.h>
#include <MaterialXGenShader/ShaderGenerator.h>
#include <MaterialXGenShader/ShaderNodeImpl.h>
#include <MaterialXGenShader/TypeDesc.h>
#include <MaterialXGenShader/Util.h>
namespace MaterialX
{
const string ShaderMetadataRegistry::USER_DATA_NAME = "ShaderMetadataRegistry";
//
// ShaderPort methods
//
ShaderPort::ShaderPort(ShaderNode* node, const TypeDesc* type, const string& name, ValuePtr value) :
_node(node),
_type(type),
_name(name),
_variable(name),
_value(value),
_flags(0)
{
}
string ShaderPort::getFullName() const
{
return (_node->getName() + "_" + _name);
}
//
// ShaderInput methods
//
ShaderInput::ShaderInput(ShaderNode* node, const TypeDesc* type, const string& name) :
ShaderPort(node, type, name),
_connection(nullptr)
{
}
void ShaderInput::makeConnection(ShaderOutput* src)
{
breakConnection();
_connection = src;
src->_connections.insert(this);
}
void ShaderInput::breakConnection()
{
if (_connection)
{
_connection->_connections.erase(this);
_connection = nullptr;
}
}
//
// ShaderOutput methods
//
ShaderOutput::ShaderOutput(ShaderNode* node, const TypeDesc* type, const string& name) :
ShaderPort(node, type, name)
{
}
void ShaderOutput::makeConnection(ShaderInput* dst)
{
dst->makeConnection(this);
}
void ShaderOutput::breakConnection(ShaderInput* dst)
{
if (!_connections.count(dst))
{
throw ExceptionShaderGenError(
"Cannot break non-existent connection from output: " + getFullName()
+ " to input: " + dst->getFullName());
}
dst->breakConnection();
}
void ShaderOutput::breakConnections()
{
ShaderInputSet inputSet(_connections);
for (ShaderInput* input : inputSet)
{
input->breakConnection();
}
if (!_connections.empty())
{
throw ExceptionShaderGenError("Number of output connections not broken properly'" + std::to_string(_connections.size()) +
" for output: " + getFullName());
}
}
namespace
{
ShaderNodePtr createEmptyNode()
{
return std::make_shared<ShaderNode>(nullptr, "");
}
}
const ShaderNodePtr ShaderNode::NONE = createEmptyNode();
const string ShaderNode::CONSTANT = "constant";
const string ShaderNode::IMAGE = "image";
const string ShaderNode::COMPARE = "compare";
const string ShaderNode::SWITCH = "switch";
const string ShaderNode::SCATTER_MODE = "scatter_mode";
const string ShaderNode::BSDF_R = "R";
const string ShaderNode::BSDF_T = "T";
const string ShaderNode::TEXTURE2D_GROUPNAME = "texture2d";
const string ShaderNode::TEXTURE3D_GROUPNAME = "texture3d";
const string ShaderNode::PROCEDURAL2D_GROUPNAME = "procedural2d";
const string ShaderNode::PROCEDURAL3D_GROUPNAME = "procedural3d";
//
// ShaderNode methods
//
ShaderNode::ShaderNode(const ShaderGraph* parent, const string& name) :
_parent(parent),
_name(name),
_classification(0),
_flags(0),
_impl(nullptr)
{
}
bool ShaderNode::referencedConditionally() const
{
if (_scopeInfo.type == ShaderNode::ScopeInfo::SINGLE)
{
int numBranches = 0;
uint32_t mask = _scopeInfo.conditionBitmask;
for (; mask != 0; mask >>= 1)
{
if (mask & 1)
{
numBranches++;
}
}
return numBranches > 0;
}
return false;
}
void ShaderNode::ScopeInfo::adjustAtConditionalInput(ShaderNode* condNode, int branch, uint32_t fullMask)
{
if (type == ScopeInfo::GLOBAL || (type == ScopeInfo::SINGLE && conditionBitmask == fullConditionMask))
{
type = ScopeInfo::SINGLE;
conditionalNode = condNode;
conditionBitmask = 1 << branch;
fullConditionMask = fullMask;
}
else if (type == ScopeInfo::SINGLE)
{
type = ScopeInfo::MULTIPLE;
conditionalNode = nullptr;
}
}
void ShaderNode::ScopeInfo::merge(const ScopeInfo &fromScope)
{
if (type == ScopeInfo::UNKNOWN || fromScope.type == ScopeInfo::GLOBAL)
{
*this = fromScope;
}
else if (type == ScopeInfo::GLOBAL)
{
}
else if (type == ScopeInfo::SINGLE && fromScope.type == ScopeInfo::SINGLE && conditionalNode == fromScope.conditionalNode)
{
conditionBitmask |= fromScope.conditionBitmask;
// This node is needed for all branches so it is no longer conditional
if (conditionBitmask == fullConditionMask)
{
type = ScopeInfo::GLOBAL;
conditionalNode = nullptr;
}
}
else
{
// NOTE: Right now multiple scopes is not really used, it works exactly as ScopeInfo::GLOBAL
type = ScopeInfo::MULTIPLE;
conditionalNode = nullptr;
}
}
ShaderNodePtr ShaderNode::create(const ShaderGraph* parent, const string& name, const NodeDef& nodeDef, GenContext& context)
{
ShaderNodePtr newNode = std::make_shared<ShaderNode>(parent, name);
const ShaderGenerator& shadergen = context.getShaderGenerator();
// Find the implementation for this nodedef
InterfaceElementPtr impl = nodeDef.getImplementation(shadergen.getTarget());
if (impl)
{
newNode->_impl = shadergen.getImplementation(*impl, context);
}
if (!newNode->_impl)
{
throw ExceptionShaderGenError("Could not find a matching implementation for node '" + nodeDef.getNodeString() +
"' matching target '" + shadergen.getTarget() + "'");
}
// Check for classification based on group name
unsigned int groupClassification = 0;
string groupName = nodeDef.getNodeGroup();
if (!groupName.empty())
{
if (groupName == TEXTURE2D_GROUPNAME || groupName == PROCEDURAL2D_GROUPNAME)
{
groupClassification = Classification::SAMPLE2D;
}
else if (groupName == TEXTURE3D_GROUPNAME || groupName == PROCEDURAL3D_GROUPNAME)
{
groupClassification = Classification::SAMPLE3D;
}
}
// Create interface from nodedef
for (const ValueElementPtr& port : nodeDef.getActiveValueElements())
{
const TypeDesc* portType = TypeDesc::get(port->getType());
if (port->isA<Output>())
{
newNode->addOutput(port->getName(), portType);
}
else
{
ShaderInput* input;
const string& portValue = port->getResolvedValueString();
std::pair<const TypeDesc*, ValuePtr> enumResult;
const string& enumNames = port->getAttribute(ValueElement::ENUM_ATTRIBUTE);
if (context.getShaderGenerator().getSyntax().remapEnumeration(portValue, portType, enumNames, enumResult))
{
input = newNode->addInput(port->getName(), enumResult.first);
input->setValue(enumResult.second);
}
else
{
input = newNode->addInput(port->getName(), portType);
if (!portValue.empty())
{
input->setValue(port->getResolvedValue());
}
}
if (port->getIsUniform())
{
input->setUniform();
}
}
}
// Add any additional inputs required by the implementation
newNode->getImplementation().addInputs(*newNode, context);
// Add a default output if needed
if (newNode->numOutputs() == 0)
{
newNode->addOutput("out", TypeDesc::get(nodeDef.getType()));
}
//
// Set node classification, defaulting to texture node
//
newNode->_classification = Classification::TEXTURE;
// First, check for specific output types
const ShaderOutput* primaryOutput = newNode->getOutput();
if (primaryOutput->getType() == Type::SURFACESHADER)
{
newNode->_classification = Classification::SURFACE | Classification::SHADER;
}
else if (primaryOutput->getType() == Type::LIGHTSHADER)
{
newNode->_classification = Classification::LIGHT | Classification::SHADER;
}
else if (primaryOutput->getType() == Type::BSDF)
{
newNode->_classification = Classification::BSDF | Classification::CLOSURE;
// Add additional classifications if the BSDF is restricted to
// only reflection or transmission
const string& bsdfType = nodeDef.getAttribute("bsdf");
if (bsdfType == BSDF_R)
{
newNode->_classification |= Classification::BSDF_R;
}
else if (bsdfType == BSDF_T)
{
newNode->_classification |= Classification::BSDF_T;
}
// Check specifically for the vertical layering node
if (nodeDef.getName() == "ND_layer_bsdf")
{
newNode->_classification |= Classification::LAYER;
}
// Check specifically for the thin-film node
else if (nodeDef.getName() == "ND_thin_film_bsdf")
{
newNode->_classification |= Classification::THINFILM;
}
}
else if (primaryOutput->getType() == Type::EDF)
{
newNode->_classification = Classification::EDF | Classification::CLOSURE;
}
else if (primaryOutput->getType() == Type::VDF)
{
newNode->_classification = Classification::VDF | Classification::CLOSURE;
}
// Second, check for specific nodes types
else if (nodeDef.getNodeString() == CONSTANT)
{
newNode->_classification = Classification::TEXTURE | Classification::CONSTANT;
}
else if (nodeDef.getNodeString() == COMPARE)
{
newNode->_classification = Classification::TEXTURE | Classification::CONDITIONAL | Classification::IFELSE;
}
else if (nodeDef.getNodeString() == SWITCH)
{
newNode->_classification = Classification::TEXTURE | Classification::CONDITIONAL | Classification::SWITCH;
}
// Third, check for file texture classification by group name
else if (groupName == TEXTURE2D_GROUPNAME || groupName == TEXTURE3D_GROUPNAME)
{
newNode->_classification = Classification::TEXTURE | Classification::FILETEXTURE;
}
// Add in group classification
newNode->_classification |= groupClassification;
// Create any metadata.
newNode->createMetadata(nodeDef, context);
return newNode;
}
ShaderNodePtr ShaderNode::create(const ShaderGraph* parent, const string& name, ShaderNodeImplPtr impl, unsigned int classification)
{
ShaderNodePtr newNode = std::make_shared<ShaderNode>(parent, name);
newNode->_impl = impl;
newNode->_classification = classification;
return newNode;
}
void ShaderNode::initialize(const Node& node, const NodeDef& nodeDef, GenContext& context)
{
// Copy input values from the given node
for (const ValueElementPtr& nodeValue : node.getActiveValueElements())
{
ShaderInput* input = getInput(nodeValue->getName());
ValueElementPtr nodeDefInput = nodeDef.getActiveValueElement(nodeValue->getName());
if (input && nodeDefInput)
{
const string& valueString = nodeValue->getResolvedValueString();
std::pair<const TypeDesc*, ValuePtr> enumResult;
const string& enumNames = nodeDefInput->getAttribute(ValueElement::ENUM_ATTRIBUTE);
const TypeDesc* type = TypeDesc::get(nodeDefInput->getType());
if (context.getShaderGenerator().getSyntax().remapEnumeration(valueString, type, enumNames, enumResult))
{
input->setValue(enumResult.second);
}
else if (!valueString.empty())
{
input->setValue(nodeValue->getResolvedValue());
}
InputPtr inputElem = nodeValue->asA<Input>();
if (inputElem)
{
input->setChannels(inputElem->getChannels());
}
}
}
// Set implementation specific values.
if (_impl)
{
_impl->setValues(node, *this, context);
}
// Set element paths for children on the node
for (const ValueElementPtr& nodeValue : node.getActiveValueElements())
{
ShaderInput* input = getInput(nodeValue->getName());
if (input)
{
input->setPath(nodeValue->getNamePath());
}
}
// Set element paths based on the node definition. Note that these
// paths don't actually exist at time of shader generation since there
// are no inputs specified on the node itself
//
const string& nodePath = node.getNamePath();
for (auto nodeInput : nodeDef.getActiveInputs())
{
ShaderInput* input = getInput(nodeInput->getName());
if (input && input->getPath().empty())
{
input->setPath(nodePath + NAME_PATH_SEPARATOR + nodeInput->getName());
}
}
// For BSDF nodes see if there is a scatter_mode input,
// and update the classification accordingly.
if (hasClassification(Classification::BSDF))
{
const InputPtr scatterModeInput = node.getInput(SCATTER_MODE);
const string& scatterMode = scatterModeInput ? scatterModeInput->getValueString() : EMPTY_STRING;
// If scatter mode is only T, set classification to only transmission.
// Note: For only R we must still keep classification at default value (both reflection/transmission)
// since reflection needs to attenuate the transmission amount in HW shaders when layering is used.
if (scatterMode == BSDF_T)
{
_classification |= Classification::BSDF_T;
_classification &= ~Classification::BSDF_R;
}
}
}
void ShaderNode::createMetadata(const NodeDef& nodeDef, GenContext& context)
{
ShaderMetadataRegistryPtr registry = context.getUserData<ShaderMetadataRegistry>(ShaderMetadataRegistry::USER_DATA_NAME);
if (!(registry && registry->getAllMetadata().size()))
{
// Early out if no metadata is registered.
return;
}
// Set metadata on the node according to the nodedef attributes.
ShaderMetadataVecPtr nodeMetadataStorage = getMetadata();
for (const string& nodedefAttr : nodeDef.getAttributeNames())
{
const ShaderMetadata* metadataEntry = registry->findMetadata(nodedefAttr);
if (metadataEntry)
{
const string& attrValue = nodeDef.getAttribute(nodedefAttr);
if (!attrValue.empty())
{
ValuePtr value = Value::createValueFromStrings(attrValue, metadataEntry->type->getName());
if (!value)
{
value = metadataEntry->value;
}
if (value)
{
if (!nodeMetadataStorage)
{
nodeMetadataStorage = std::make_shared<ShaderMetadataVec>();
setMetadata(nodeMetadataStorage);
}
nodeMetadataStorage->push_back(ShaderMetadata(metadataEntry->name, metadataEntry->type, value));
}
}
}
}
// Set metadata on inputs according to attributes on the nodedef's inputs
for (const ValueElementPtr& nodedefPort : nodeDef.getActiveValueElements())
{
ShaderInput* input = getInput(nodedefPort->getName());
if (input)
{
ShaderMetadataVecPtr inputMetadataStorage = input->getMetadata();
for (const string& nodedefPortAttr : nodedefPort->getAttributeNames())
{
const ShaderMetadata* metadataEntry = registry->findMetadata(nodedefPortAttr);
if (metadataEntry)
{
const string& attrValue = nodedefPort->getAttribute(nodedefPortAttr);
if (!attrValue.empty())
{
const TypeDesc* type = metadataEntry->type ? metadataEntry->type : input->getType();
ValuePtr value = Value::createValueFromStrings(attrValue, type->getName());
if (!value)
{
value = metadataEntry->value;
}
if (value)
{
if (!inputMetadataStorage)
{
inputMetadataStorage = std::make_shared<ShaderMetadataVec>();
input->setMetadata(inputMetadataStorage);
}
inputMetadataStorage->push_back(ShaderMetadata(metadataEntry->name, type, value));
}
}
}
}
}
}
}
ShaderInput* ShaderNode::getInput(const string& name)
{
auto it = _inputMap.find(name);
return it != _inputMap.end() ? it->second.get() : nullptr;
}
ShaderOutput* ShaderNode::getOutput(const string& name)
{
auto it = _outputMap.find(name);
return it != _outputMap.end() ? it->second.get() : nullptr;
}
const ShaderInput* ShaderNode::getInput(const string& name) const
{
auto it = _inputMap.find(name);
return it != _inputMap.end() ? it->second.get() : nullptr;
}
const ShaderOutput* ShaderNode::getOutput(const string& name) const
{
auto it = _outputMap.find(name);
return it != _outputMap.end() ? it->second.get() : nullptr;
}
ShaderInput* ShaderNode::addInput(const string& name, const TypeDesc* type)
{
if (getInput(name))
{
throw ExceptionShaderGenError("An input named '" + name + "' already exists on node '" + _name + "'");
}
ShaderInputPtr input = std::make_shared<ShaderInput>(this, type, name);
_inputMap[name] = input;
_inputOrder.push_back(input.get());
return input.get();
}
ShaderOutput* ShaderNode::addOutput(const string& name, const TypeDesc* type)
{
if (getOutput(name))
{
throw ExceptionShaderGenError("An output named '" + name + "' already exists on node '" + _name + "'");
}
ShaderOutputPtr output = std::make_shared<ShaderOutput>(this, type, name);
_outputMap[name] = output;
_outputOrder.push_back(output.get());
return output.get();
}
} // namespace MaterialX