forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderNode.h
More file actions
558 lines (445 loc) · 18 KB
/
ShaderNode.h
File metadata and controls
558 lines (445 loc) · 18 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
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
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#ifndef MATERIALX_SHADERNODE_H
#define MATERIALX_SHADERNODE_H
/// @file
/// Classes for nodes created during shader generation
#include <MaterialXGenShader/Export.h>
#include <MaterialXGenShader/ShaderNodeImpl.h>
#include <MaterialXGenShader/TypeDesc.h>
#include <MaterialXGenShader/GenUserData.h>
#include <MaterialXCore/Node.h>
MATERIALX_NAMESPACE_BEGIN
class ShaderNode;
class ShaderPort;
class ShaderInput;
class ShaderOutput;
class ShaderGraph;
/// Shared pointer to a ShaderPort
using ShaderPortPtr = shared_ptr<class ShaderPort>;
/// Shared pointer to a ShaderInput
using ShaderInputPtr = shared_ptr<class ShaderInput>;
/// Shared pointer to a ShaderOutput
using ShaderOutputPtr = shared_ptr<class ShaderOutput>;
/// Shared pointer to a ShaderNode
using ShaderNodePtr = shared_ptr<class ShaderNode>;
/// A vector of ShaderInput pointers
using ShaderInputVec = vector<ShaderInput*>;
/// Metadata to be exported to generated shader.
struct MX_GENSHADER_API ShaderMetadata
{
string name;
const TypeDesc* type;
ValuePtr value;
ShaderMetadata(const string& n, const TypeDesc* t, ValuePtr v = nullptr) :
name(n),
type(t),
value(v)
{}
};
using ShaderMetadataVec = vector<ShaderMetadata>;
using ShaderMetadataVecPtr = shared_ptr<ShaderMetadataVec>;
/// @class ShaderMetadataRegistry
/// A registry for metadata that will be exported to the generated shader
/// if found on nodes and inputs during shader generation.
class MX_GENSHADER_API ShaderMetadataRegistry : public GenUserData
{
public:
static const string USER_DATA_NAME;
/// Add a new metadata entry to the registry.
/// The entry contains the name and data type
/// for the metadata.
void addMetadata(const string& name, const TypeDesc* type, ValuePtr value = nullptr)
{
if (_entryIndex.count(name) == 0)
{
_entryIndex[name] = _entries.size();
_entries.emplace_back(name, type, value);
}
}
/// Return the metadata registered for the given name, or nullptr
/// if no such entry is found.
const ShaderMetadata* findMetadata(const string& name) const
{
auto it = _entryIndex.find(name);
return it != _entryIndex.end() ? &_entries[it->second] : nullptr;
}
/// Return the metadata registered for the given name, or nullptr
/// if no such entry is found.
ShaderMetadata* findMetadata(const string& name)
{
auto it = _entryIndex.find(name);
return it != _entryIndex.end() ? &_entries[it->second] : nullptr;
}
/// Return all entries in the registry.
const ShaderMetadataVec& getAllMetadata() const
{
return _entries;
}
/// Clear all entries in the registry.
void clear()
{
_entryIndex.clear();
_entries.clear();
}
protected:
vector<ShaderMetadata> _entries;
std::unordered_map<string, size_t> _entryIndex;
};
using ShaderMetadataRegistryPtr = shared_ptr<ShaderMetadataRegistry>;
/// Flags set on shader ports.
class MX_GENSHADER_API ShaderPortFlag
{
public:
static const uint32_t UNIFORM = 1u << 0;
static const uint32_t EMITTED = 1u << 1;
static const uint32_t BIND_INPUT = 1u << 2;
};
/// @class ShaderPort
/// An input or output port on a ShaderNode
class MX_GENSHADER_API ShaderPort : public std::enable_shared_from_this<ShaderPort>
{
public:
/// Constructor.
ShaderPort(ShaderNode* node, const TypeDesc* type, const string& name, ValuePtr value = nullptr);
/// Return a shared pointer instance of this object.
ShaderPortPtr getSelf()
{
return shared_from_this();
}
/// Return the node this port belongs to.
ShaderNode* getNode() { return _node; }
/// Return the node this port belongs to.
const ShaderNode* getNode() const { return _node; }
/// Set the data type for this port.
void setType(const TypeDesc* type) { _type = type; }
/// Return the data type for this port.
const TypeDesc* getType() const { return _type; }
/// Set the name of this port.
void setName(const string& name) { _name = name; }
/// Return the name of this port.
const string& getName() const { return _name; }
/// Return the name of this port.
string getFullName() const;
/// Set the variable name of this port.
void setVariable(const string& name) { _variable = name; }
/// Return the variable name of this port.
const string& getVariable() const { return _variable; }
/// Set the variable semantic of this port.
void setSemantic(const string& semantic) { _semantic = semantic; }
/// Return the variable semantic of this port.
const string& getSemantic() const { return _semantic; }
/// Set a value on this port.
void setValue(ValuePtr value) { _value = value; }
/// Return the value set on this port.
ValuePtr getValue() const { return _value; }
/// Set a source color space for the value on this port.
void setColorSpace(const string& colorspace) { _colorspace = colorspace; }
/// Return the source color space for the value on this port.
const string& getColorSpace() const { return _colorspace; }
/// Set a unit type for the value on this port.
void setUnit(const string& unit) { _unit = unit; }
/// Return the unit type for the value on this port.
const string& getUnit() const { return _unit; }
/// Set geomprop name if the input has a default
/// geomprop to be assigned when it is unconnected.
void setGeomProp(const string& geomprop) { _geomprop = geomprop; }
/// Get geomprop name.
const string& getGeomProp() const { return _geomprop; }
/// Set the path to this port.
void setPath(const string& path) { _path = path; }
/// Return the path to this port.
const string& getPath() const { return _path; }
/// Set flags on this port.
void setFlags(uint32_t flags) { _flags = flags; }
/// Return flags set on this port.
uint32_t getFlags() const { return _flags; }
/// Set the on|off state of a given flag.
void setFlag(uint32_t flag, bool value)
{
_flags = value ? (_flags | flag) : (_flags & ~flag);
}
/// Return the on|off state of a given flag.
bool getFlag(uint32_t flag) const
{
return ((_flags & flag) != 0);
}
/// Set the uniform flag this port to true.
void setUniform() { _flags |= ShaderPortFlag::UNIFORM; }
/// Return the uniform flag on this port.
bool isUniform() const { return (_flags & ShaderPortFlag::UNIFORM) != 0; }
/// Set the emitted state on this port to true.
void setEmitted() { _flags |= ShaderPortFlag::EMITTED; }
/// Return the emitted state of this port.
bool isEmitted() const { return (_flags & ShaderPortFlag::EMITTED) != 0; }
/// Set the bind input state on this port to true.
void setBindInput() { _flags |= ShaderPortFlag::BIND_INPUT; }
/// Return the emitted state of this port.
bool isBindInput() const { return (_flags & ShaderPortFlag::BIND_INPUT) != 0; }
/// Set the metadata vector.
void setMetadata(ShaderMetadataVecPtr metadata) { _metadata = metadata; }
/// Get the metadata vector.
ShaderMetadataVecPtr getMetadata() { return _metadata; }
/// Get the metadata vector.
const ShaderMetadataVecPtr& getMetadata() const { return _metadata; }
protected:
ShaderNode* _node;
const TypeDesc* _type;
string _name;
string _path;
string _semantic;
string _variable;
ValuePtr _value;
string _unit;
string _colorspace;
string _geomprop;
ShaderMetadataVecPtr _metadata;
uint32_t _flags;
};
/// @class ShaderInput
/// An input on a ShaderNode
class MX_GENSHADER_API ShaderInput : public ShaderPort
{
public:
ShaderInput(ShaderNode* node, const TypeDesc* type, const string& name);
/// Return a connection to an upstream node output,
/// or nullptr if not connected.
ShaderOutput* getConnection() { return _connection; }
/// Return a connection to an upstream node output,
/// or nullptr if not connected.
const ShaderOutput* getConnection() const { return _connection; }
/// Make a connection from the given source output to this input.
void makeConnection(ShaderOutput* src);
/// Break the connection to this input.
void breakConnection();
/// Set optional channels value
void setChannels(const string& channels) { _channels = channels; }
/// Get optional channels value
const string& getChannels() const { return _channels; }
/// Return the sibling node connected upstream,
/// or nullptr if there is no sibling upstream.
ShaderNode* getConnectedSibling() const;
protected:
ShaderOutput* _connection;
string _channels;
friend class ShaderOutput;
};
/// @class ShaderOutput
/// An output on a ShaderNode
class MX_GENSHADER_API ShaderOutput : public ShaderPort
{
public:
ShaderOutput(ShaderNode* node, const TypeDesc* type, const string& name);
/// Return a set of connections to downstream node inputs,
/// empty if not connected.
const ShaderInputVec& getConnections() const { return _connections; }
/// Make a connection from this output to the given input
void makeConnection(ShaderInput* dst);
/// Break a connection from this output to the given input
void breakConnection(ShaderInput* dst);
/// Break all connections from this output
void breakConnections();
protected:
ShaderInputVec _connections;
friend class ShaderInput;
};
/// @class ShaderNode
/// Class representing a node in the shader generation DAG
class MX_GENSHADER_API ShaderNode
{
public:
virtual ~ShaderNode() { }
/// Flags for classifying nodes into different categories.
class Classification
{
public:
// Node classes
static const uint32_t TEXTURE = 1 << 0; /// Any node that outputs floats, colors, vectors, etc.
static const uint32_t CLOSURE = 1 << 1; /// Any node that represents light integration
static const uint32_t SHADER = 1 << 2; /// Any node that outputs a shader
static const uint32_t MATERIAL = 1 << 3; /// Any node that outputs a material
// Specific texture node types
static const uint32_t FILETEXTURE = 1 << 4; /// A file texture node
static const uint32_t CONDITIONAL = 1 << 5; /// A conditional node
static const uint32_t CONSTANT = 1 << 6; /// A constant node
// Specific closure types
static const uint32_t BSDF = 1 << 7; /// A BSDF node
static const uint32_t BSDF_R = 1 << 8; /// A reflection BSDF node
static const uint32_t BSDF_T = 1 << 9; /// A transmission BSDF node
static const uint32_t EDF = 1 << 10; /// A EDF node
static const uint32_t VDF = 1 << 11; /// A VDF node
static const uint32_t LAYER = 1 << 12; /// A node for vertical layering of other closure nodes
static const uint32_t THINFILM = 1 << 13; /// A node for adding thin-film over microfacet BSDF nodes
// Specific shader types
static const uint32_t SURFACE = 1 << 14; /// A surface shader node
static const uint32_t VOLUME = 1 << 15; /// A volume shader node
static const uint32_t LIGHT = 1 << 16; /// A light shader node
static const uint32_t UNLIT = 1 << 17; /// An unlit surface shader node
// Specific conditional types
static const uint32_t IFELSE = 1 << 18; /// An if-else statement
static const uint32_t SWITCH = 1 << 19; /// A switch statement
// Types based on nodegroup
static const uint32_t SAMPLE2D = 1 << 20; /// Can be sampled in 2D (uv space)
static const uint32_t SAMPLE3D = 1 << 21; /// Can be sampled in 3D (position)
};
/// @struct ScopeInfo
/// Information on source code scope for the node.
///
/// @todo: Refactor the scope handling, using scope id's instead
///
struct ScopeInfo
{
enum Type
{
UNKNOWN,
GLOBAL,
SINGLE,
MULTIPLE
};
ScopeInfo() : type(UNKNOWN), conditionalNode(nullptr), conditionBitmask(0), fullConditionMask(0) {}
void merge(const ScopeInfo& fromScope);
void adjustAtConditionalInput(ShaderNode* condNode, int branch, uint32_t fullMask);
bool usedByBranch(int branchIndex) const { return (conditionBitmask & (1 << branchIndex)) != 0; }
Type type;
ShaderNode* conditionalNode;
uint32_t conditionBitmask;
uint32_t fullConditionMask;
};
static const ShaderNodePtr NONE;
static const string CONSTANT;
static const string IMAGE;
static const string COMPARE;
static const string SWITCH;
static const string SURFACESHADER;
static const string SCATTER_MODE;
static const string BSDF_R;
static const string BSDF_T;
static const string TRANSFORM_POINT;
static const string TRANSFORM_VECTOR;
static const string TRANSFORM_NORMAL;
static const string TEXTURE2D_GROUPNAME;
static const string TEXTURE3D_GROUPNAME;
static const string PROCEDURAL2D_GROUPNAME;
static const string PROCEDURAL3D_GROUPNAME;
public:
/// Constructor.
ShaderNode(const ShaderGraph* parent, const string& name);
/// Create a new node from a nodedef.
static ShaderNodePtr create(const ShaderGraph* parent, const string& name, const NodeDef& nodeDef,
GenContext& context);
/// Create a new node from a node implementation.
static ShaderNodePtr create(const ShaderGraph* parent, const string& name, ShaderNodeImplPtr impl,
unsigned int classification = Classification::TEXTURE);
/// Return true if this node is a graph.
virtual bool isAGraph() const { return false; }
/// Return the parent graph that owns this node.
/// If this node is a root graph it has no parent
/// and nullptr will be returned.
const ShaderGraph* getParent() const
{
return _parent;
}
/// Set classification bits for this node,
/// replacing any previous set bits.
void setClassification(uint32_t c)
{
_classification = c;
}
/// Get classification bits set for this node.
uint32_t getClassification() const
{
return _classification;
}
/// Add classification bits to this node.
void addClassification(uint32_t c)
{
_classification |= c;
}
/// Return true if this node matches the given classification.
bool hasClassification(uint32_t c) const
{
return (_classification & c) == c;
}
/// Return the name of this node.
const string& getName() const
{
return _name;
}
/// Return the implementation used for this node.
const ShaderNodeImpl& getImplementation() const
{
return *_impl;
}
/// Return the scope info for this node.
ScopeInfo& getScopeInfo()
{
return _scopeInfo;
}
/// Return the scope info for this node.
const ScopeInfo& getScopeInfo() const
{
return _scopeInfo;
}
/// Returns true if this node is only referenced by a conditional.
bool referencedConditionally() const;
/// Initialize this shader node with all required data
/// from the given node and nodedef.
void initialize(const Node& node, const NodeDef& nodeDef, GenContext& context);
/// Add inputs/outputs
ShaderInput* addInput(const string& name, const TypeDesc* type);
ShaderOutput* addOutput(const string& name, const TypeDesc* type);
/// Get number of inputs/outputs
size_t numInputs() const { return _inputOrder.size(); }
size_t numOutputs() const { return _outputOrder.size(); }
/// Get inputs/outputs by index
ShaderInput* getInput(size_t index) { return _inputOrder[index]; }
ShaderOutput* getOutput(size_t index = 0) { return _outputOrder[index]; }
const ShaderInput* getInput(size_t index) const { return _inputOrder[index]; }
const ShaderOutput* getOutput(size_t index = 0) const { return _outputOrder[index]; }
/// Get inputs/outputs by name
ShaderInput* getInput(const string& name);
ShaderOutput* getOutput(const string& name);
const ShaderInput* getInput(const string& name) const;
const ShaderOutput* getOutput(const string& name) const;
/// Get vector of inputs/outputs
const vector<ShaderInput*>& getInputs() const { return _inputOrder; }
const vector<ShaderOutput*>& getOutputs() const { return _outputOrder; }
/// Set the metadata vector.
void setMetadata(ShaderMetadataVecPtr metadata) { _metadata = metadata; }
/// Get the metadata vector.
ShaderMetadataVecPtr getMetadata() { return _metadata; }
/// Get the metadata vector.
const ShaderMetadataVecPtr& getMetadata() const { return _metadata; }
/// Returns true if an input is editable by users.
/// Editable inputs are allowed to be published as shader uniforms
/// and hence must be presentable in a user interface.
bool isEditable(const ShaderInput& input) const
{
return (!_impl || _impl->isEditable(input));
}
/// Returns true if a graph input is accessible by users.
/// Editable inputs are allowed to be published as shader uniforms
/// and hence must be presentable in a user interface.
bool isEditable(const ShaderGraphInputSocket& input) const
{
return (!_impl || _impl->isEditable(input));
}
protected:
/// Create metadata from the nodedef according to registered metadata.
void createMetadata(const NodeDef& nodeDef, GenContext& context);
const ShaderGraph* _parent;
string _name;
uint32_t _classification;
std::unordered_map<string, ShaderInputPtr> _inputMap;
vector<ShaderInput*> _inputOrder;
std::unordered_map<string, ShaderOutputPtr> _outputMap;
vector<ShaderOutput*> _outputOrder;
ShaderNodeImplPtr _impl;
ShaderMetadataVecPtr _metadata;
ScopeInfo _scopeInfo;
friend class ShaderGraph;
};
MATERIALX_NAMESPACE_END
#endif