MaterialX 1.38.2
ShaderNode.h
Go to the documentation of this file.
1//
2// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3// All rights reserved. See LICENSE.txt for license.
4//
5
6#ifndef MATERIALX_SHADERNODE_H
7#define MATERIALX_SHADERNODE_H
8
11
13
17
18#include <MaterialXCore/Node.h>
19
20namespace MaterialX
21{
22
23class ShaderNode;
24class ShaderPort;
25class ShaderInput;
26class ShaderOutput;
27class ShaderGraph;
28
30using ShaderPortPtr = shared_ptr<class ShaderPort>;
32using ShaderInputPtr = shared_ptr<class ShaderInput>;
34using ShaderOutputPtr = shared_ptr<class ShaderOutput>;
36using ShaderNodePtr = shared_ptr<class ShaderNode>;
38using ShaderInputSet = std::set<ShaderInput*>;
39
40
42struct MX_GENSHADER_API ShaderMetadata
43{
44 string name;
45 const TypeDesc* type;
46 ValuePtr value;
47 ShaderMetadata(const string& n, const TypeDesc* t, ValuePtr v = nullptr) :
48 name(n),
49 type(t),
50 value(v)
51 {}
52};
53using ShaderMetadataVec = vector<ShaderMetadata>;
54using ShaderMetadataVecPtr = shared_ptr<ShaderMetadataVec>;
55
59class MX_GENSHADER_API ShaderMetadataRegistry : public GenUserData
60{
61 public:
62 static const string USER_DATA_NAME;
63
67 void addMetadata(const string& name, const TypeDesc* type, ValuePtr value = nullptr)
68 {
69 if (_entryIndex.count(name) == 0)
70 {
71 _entryIndex[name] = _entries.size();
72 _entries.emplace_back(name, type, value);
73 }
74 }
75
78 const ShaderMetadata* findMetadata(const string& name) const
79 {
80 auto it = _entryIndex.find(name);
81 return it != _entryIndex.end() ? &_entries[it->second] : nullptr;
82 }
83
86 ShaderMetadata* findMetadata(const string& name)
87 {
88 auto it = _entryIndex.find(name);
89 return it != _entryIndex.end() ? &_entries[it->second] : nullptr;
90 }
91
93 const ShaderMetadataVec& getAllMetadata() const
94 {
95 return _entries;
96 }
97
99 void clear()
100 {
101 _entryIndex.clear();
102 _entries.clear();
103 }
104
105 protected:
106 vector<ShaderMetadata> _entries;
107 std::unordered_map<string, size_t> _entryIndex;
108};
109
110using ShaderMetadataRegistryPtr = shared_ptr<ShaderMetadataRegistry>;
111
112
114class MX_GENSHADER_API ShaderPortFlag
115{
116 public:
117 static const uint32_t UNIFORM = 1u << 0;
118 static const uint32_t EMITTED = 1u << 1;
119 static const uint32_t BIND_INPUT = 1u << 2;
120};
121
124class MX_GENSHADER_API ShaderPort : public std::enable_shared_from_this<ShaderPort>
125{
126 public:
128 ShaderPort(ShaderNode* node, const TypeDesc* type, const string& name, ValuePtr value = nullptr);
129
132 {
133 return shared_from_this();
134 }
135
137 ShaderNode* getNode() { return _node; }
138
140 const ShaderNode* getNode() const { return _node; }
141
143 void setType(const TypeDesc* type) { _type = type; }
144
146 const TypeDesc* getType() const { return _type; }
147
149 void setName(const string& name) { _name = name; }
150
152 const string& getName() const { return _name; }
153
155 string getFullName() const;
156
158 void setVariable(const string& name) { _variable = name; }
159
161 const string& getVariable() const { return _variable; }
162
164 void setSemantic(const string& semantic) { _semantic = semantic; }
165
167 const string& getSemantic() const { return _semantic; }
168
170 void setValue(ValuePtr value) { _value = value; }
171
173 ValuePtr getValue() const { return _value; }
174
176 void setUnit(const string& unit) { _unit = unit; }
177
179 const string& getUnit() const { return _unit; }
180
183 void setGeomProp(const string& geomprop) { _geomprop = geomprop; }
184
186 const string& getGeomProp() const { return _geomprop; }
187
189 void setPath(const string& path) { _path = path; }
190
192 const string& getPath() const { return _path; }
193
195 void setFlags(uint32_t flags) { _flags = flags; }
196
198 uint32_t getFlags() const { return _flags; }
199
201 void setFlag(uint32_t flag, bool value)
202 {
203 _flags = value ? (_flags | flag) : (_flags & ~flag);
204 }
205
207 bool getFlag(uint32_t flag) const
208 {
209 return ((_flags & flag) != 0);
210 }
211
213 void setUniform() { _flags |= ShaderPortFlag::UNIFORM; }
214
216 bool isUniform() const { return (_flags & ShaderPortFlag::UNIFORM) != 0; }
217
219 void setEmitted() { _flags |= ShaderPortFlag::EMITTED; }
220
222 bool isEmitted() const { return (_flags & ShaderPortFlag::EMITTED) != 0; }
223
225 void setBindInput() { _flags |= ShaderPortFlag::BIND_INPUT; }
226
228 bool isBindInput() const { return (_flags & ShaderPortFlag::BIND_INPUT) != 0; }
229
231 void setMetadata(ShaderMetadataVecPtr metadata) { _metadata = metadata; }
232
234 ShaderMetadataVecPtr getMetadata() { return _metadata; }
235
237 const ShaderMetadataVecPtr& getMetadata() const { return _metadata; }
238
239 protected:
240 ShaderNode* _node;
241 const TypeDesc* _type;
242 string _name;
243 string _path;
244 string _semantic;
245 string _variable;
246 ValuePtr _value;
247 string _unit;
248 string _geomprop;
249 ShaderMetadataVecPtr _metadata;
250 uint32_t _flags;
251};
252
255class MX_GENSHADER_API ShaderInput : public ShaderPort
256{
257 public:
258 ShaderInput(ShaderNode* node, const TypeDesc* type, const string& name);
259
262 ShaderOutput* getConnection() { return _connection; }
263
266 const ShaderOutput* getConnection() const { return _connection; }
267
269 void makeConnection(ShaderOutput* src);
270
272 void breakConnection();
273
275 void setChannels(const string& channels) { _channels = channels; }
276
278 const string& getChannels() const { return _channels; }
279
280 protected:
281 ShaderOutput* _connection;
282 string _channels;
283 friend class ShaderOutput;
284};
285
288class MX_GENSHADER_API ShaderOutput : public ShaderPort
289{
290 public:
291 ShaderOutput(ShaderNode* node, const TypeDesc* type, const string& name);
292
295 ShaderInputSet& getConnections() { return _connections; }
296
299 const ShaderInputSet& getConnections() const { return _connections; }
300
302 void makeConnection(ShaderInput* dst);
303
305 void breakConnection(ShaderInput* dst);
306
308 void breakConnections();
309
310 protected:
311 ShaderInputSet _connections;
312 friend class ShaderInput;
313};
314
315
318{
320 EXCLUDE_FUNCTION_CALL = 1 << 0,
321};
322
325class MX_GENSHADER_API ShaderNode
326{
327 public:
328 virtual ~ShaderNode() { }
329
332 {
333 public:
334 // Node classes
335 static const uint32_t TEXTURE = 1 << 0;
336 static const uint32_t CLOSURE = 1 << 1;
337 static const uint32_t SHADER = 1 << 2;
338 // Specific texture node types
339 static const uint32_t FILETEXTURE = 1 << 3;
340 static const uint32_t CONDITIONAL = 1 << 4;
341 static const uint32_t CONSTANT = 1 << 5;
342 // Specific closure types
343 static const uint32_t BSDF = 1 << 6;
344 static const uint32_t BSDF_R = 1 << 7;
345 static const uint32_t BSDF_T = 1 << 8;
346 static const uint32_t EDF = 1 << 9;
347 static const uint32_t VDF = 1 << 10;
348 static const uint32_t LAYER = 1 << 11;
349 static const uint32_t THINFILM = 1 << 12;
350 // Specific shader types
351 static const uint32_t SURFACE = 1 << 13;
352 static const uint32_t VOLUME = 1 << 14;
353 static const uint32_t LIGHT = 1 << 15;
354 // Specific conditional types
355 static const uint32_t IFELSE = 1 << 16;
356 static const uint32_t SWITCH = 1 << 17;
357 // Types based on nodegroup
358 static const uint32_t SAMPLE2D = 1 << 18;
359 static const uint32_t SAMPLE3D = 1 << 19;
360 };
361
368 {
369 enum Type
370 {
371 UNKNOWN,
372 GLOBAL,
373 SINGLE,
374 MULTIPLE
375 };
376
377 ScopeInfo() : type(UNKNOWN), conditionalNode(nullptr), conditionBitmask(0), fullConditionMask(0) {}
378
379 void merge(const ScopeInfo& fromScope);
380 void adjustAtConditionalInput(ShaderNode* condNode, int branch, uint32_t fullMask);
381 bool usedByBranch(int branchIndex) const { return (conditionBitmask & (1 << branchIndex)) != 0; }
382
383 Type type;
384 ShaderNode* conditionalNode;
385 uint32_t conditionBitmask;
386 uint32_t fullConditionMask;
387 };
388
389 static const ShaderNodePtr NONE;
390
391 static const string CONSTANT;
392 static const string IMAGE;
393 static const string COMPARE;
394 static const string SWITCH;
395 static const string SCATTER_MODE;
396 static const string BSDF_R;
397 static const string BSDF_T;
398 static const string TRANSFORM_POINT;
399 static const string TRANSFORM_VECTOR;
400 static const string TRANSFORM_NORMAL;
401 static const string TEXTURE2D_GROUPNAME;
402 static const string TEXTURE3D_GROUPNAME;
403 static const string PROCEDURAL2D_GROUPNAME;
404 static const string PROCEDURAL3D_GROUPNAME;
405
406 public:
408 ShaderNode(const ShaderGraph* parent, const string& name);
409
411 static ShaderNodePtr create(const ShaderGraph* parent, const string& name, const NodeDef& nodeDef,
412 GenContext& context);
413
415 static ShaderNodePtr create(const ShaderGraph* parent, const string& name, ShaderNodeImplPtr impl,
416 unsigned int classification = Classification::TEXTURE);
417
419 virtual bool isAGraph() const { return false; }
420
424 const ShaderGraph* getParent() const
425 {
426 return _parent;
427 }
428
430 bool hasClassification(uint32_t c) const
431 {
432 return (_classification & c) == c;
433 }
434
436 const string& getName() const
437 {
438 return _name;
439 }
440
443 {
444 return *_impl;
445 }
446
449 {
450 return _scopeInfo;
451 }
452
454 const ScopeInfo& getScopeInfo() const
455 {
456 return _scopeInfo;
457 }
458
460 bool referencedConditionally() const;
461
463 bool isUsedClosure(const ShaderNode* node) const
464 {
465 return _usedClosures.count(node) > 0;
466 }
467
470 void initialize(const Node& node, const NodeDef& nodeDef, GenContext& context);
471
473 ShaderInput* addInput(const string& name, const TypeDesc* type);
474 ShaderOutput* addOutput(const string& name, const TypeDesc* type);
475
477 size_t numInputs() const { return _inputOrder.size(); }
478 size_t numOutputs() const { return _outputOrder.size(); }
479
481 ShaderInput* getInput(size_t index) { return _inputOrder[index]; }
482 ShaderOutput* getOutput(size_t index = 0) { return _outputOrder[index]; }
483 const ShaderInput* getInput(size_t index) const { return _inputOrder[index]; }
484 const ShaderOutput* getOutput(size_t index = 0) const { return _outputOrder[index]; }
485
487 ShaderInput* getInput(const string& name);
488 ShaderOutput* getOutput(const string& name);
489 const ShaderInput* getInput(const string& name) const;
490 const ShaderOutput* getOutput(const string& name) const;
491
493 const vector<ShaderInput*>& getInputs() const { return _inputOrder; }
494 const vector<ShaderOutput*>& getOutputs() const { return _outputOrder; }
495
497 void setMetadata(ShaderMetadataVecPtr metadata) { _metadata = metadata; }
498
500 ShaderMetadataVecPtr getMetadata() { return _metadata; }
501
503 const ShaderMetadataVecPtr& getMetadata() const { return _metadata; }
504
508 bool isEditable(const ShaderInput& input) const
509 {
510 return (!_impl || _impl->isEditable(input));
511 }
512
516 bool isEditable(const ShaderGraphInputSocket& input) const
517 {
518 return (!_impl || _impl->isEditable(input));
519 }
520
522 void setFlag(ShaderNodeFlag flag, bool value)
523 {
524 _flags = value ? (_flags | uint32_t(flag)) : (_flags & ~uint32_t(flag));
525 }
526
528 bool getFlag(ShaderNodeFlag flag) const
529 {
530 return ((_flags & uint32_t(flag)) != 0);
531 }
532
533 protected:
535 void createMetadata(const NodeDef& nodeDef, GenContext& context);
536
537 const ShaderGraph* _parent;
538 string _name;
539 uint32_t _classification;
540 uint32_t _flags;
541
542 std::unordered_map<string, ShaderInputPtr> _inputMap;
543 vector<ShaderInput*> _inputOrder;
544
545 std::unordered_map<string, ShaderOutputPtr> _outputMap;
546 vector<ShaderOutput*> _outputOrder;
547
548 ShaderNodeImplPtr _impl;
549 ShaderMetadataVecPtr _metadata;
550 ScopeInfo _scopeInfo;
551 std::set<const ShaderNode*> _usedClosures;
552
553 friend class ShaderGraph;
554};
555
556} // namespace MaterialX
557
558#endif
User data base class for shader generation.
Macros for declaring imported and exported symbols.
shared_ptr< ShaderNodeImpl > ShaderNodeImplPtr
Shared pointer to a ShaderNodeImpl.
Definition: Library.h:40
Node element subclasses.
ShaderNodeFlag
Flags for tagging shader nodes.
Definition: ShaderNode.h:318
@ EXCLUDE_FUNCTION_CALL
Omit the function call for this node.
shared_ptr< class ShaderPort > ShaderPortPtr
Shared pointer to a ShaderPort.
Definition: ShaderNode.h:30
std::set< ShaderInput * > ShaderInputSet
Shared pointer to a ShaderInput.
Definition: ShaderNode.h:38
shared_ptr< class ShaderOutput > ShaderOutputPtr
Shared pointer to a ShaderOutput.
Definition: ShaderNode.h:34
shared_ptr< class ShaderNode > ShaderNodePtr
Shared pointer to a ShaderNode.
Definition: ShaderNode.h:36
shared_ptr< class ShaderInput > ShaderInputPtr
Shared pointer to a ShaderInput.
Definition: ShaderNode.h:32
Base class for shader node implementations.
Type descriptor for a MaterialX data type.
shared_ptr< Value > ValuePtr
A shared pointer to a Value.
Definition: Value.h:30
A context class for shader generation.
Definition: GenContext.h:27
Base class for custom user data needed during shader generation.
Definition: GenUserData.h:28
A node definition element within a Document.
Definition: Definition.h:83
A node element within a NodeGraph or Document.
Definition: Node.h:54
Class representing a graph (DAG) for shader generation.
Definition: ShaderGraph.h:45
An input on a ShaderNode.
Definition: ShaderNode.h:256
void setChannels(const string &channels)
Set optional channels value.
Definition: ShaderNode.h:275
const ShaderOutput * getConnection() const
Return a connection to an upstream node output, or nullptr if not connected.
Definition: ShaderNode.h:266
ShaderOutput * getConnection()
Return a connection to an upstream node output, or nullptr if not connected.
Definition: ShaderNode.h:262
const string & getChannels() const
Get optional channels value.
Definition: ShaderNode.h:278
A registry for metadata that will be exported to the generated shader if found on nodes and inputs du...
Definition: ShaderNode.h:60
void addMetadata(const string &name, const TypeDesc *type, ValuePtr value=nullptr)
Add a new metadata entry to the registry.
Definition: ShaderNode.h:67
const ShaderMetadataVec & getAllMetadata() const
Return all entries in the registry.
Definition: ShaderNode.h:93
ShaderMetadata * findMetadata(const string &name)
Return the metadata registered for the given name, or nullptr if no such entry is found.
Definition: ShaderNode.h:86
void clear()
Clear all entries in the registry.
Definition: ShaderNode.h:99
const ShaderMetadata * findMetadata(const string &name) const
Return the metadata registered for the given name, or nullptr if no such entry is found.
Definition: ShaderNode.h:78
Flags for classifying nodes into different categories.
Definition: ShaderNode.h:332
Class representing a node in the shader generation DAG.
Definition: ShaderNode.h:326
bool getFlag(ShaderNodeFlag flag) const
Return the on|off state of a given flag.
Definition: ShaderNode.h:528
void setFlag(ShaderNodeFlag flag, bool value)
Set the on|off state of a given flag.
Definition: ShaderNode.h:522
bool isEditable(const ShaderInput &input) const
Returns true if an input is editable by users.
Definition: ShaderNode.h:508
ShaderInput * getInput(size_t index)
Get inputs/outputs by index.
Definition: ShaderNode.h:481
const ShaderGraph * getParent() const
Return the parent graph that owns this node.
Definition: ShaderNode.h:424
const vector< ShaderInput * > & getInputs() const
Get vector of inputs/outputs.
Definition: ShaderNode.h:493
const string & getName() const
Return the name of this node.
Definition: ShaderNode.h:436
void setMetadata(ShaderMetadataVecPtr metadata)
Set the metadata vector.
Definition: ShaderNode.h:497
bool isUsedClosure(const ShaderNode *node) const
Returns true if the given node is a closure used by this node.
Definition: ShaderNode.h:463
bool hasClassification(uint32_t c) const
Return true if this node matches the given classification.
Definition: ShaderNode.h:430
const ShaderMetadataVecPtr & getMetadata() const
Get the metadata vector.
Definition: ShaderNode.h:503
ShaderMetadataVecPtr getMetadata()
Get the metadata vector.
Definition: ShaderNode.h:500
virtual bool isAGraph() const
Return true if this node is a graph.
Definition: ShaderNode.h:419
const ScopeInfo & getScopeInfo() const
Return the scope info for this node.
Definition: ShaderNode.h:454
const ShaderNodeImpl & getImplementation() const
Return the implementation used for this node.
Definition: ShaderNode.h:442
bool isEditable(const ShaderGraphInputSocket &input) const
Returns true if a graph input is accessible by users.
Definition: ShaderNode.h:516
size_t numInputs() const
Get number of inputs/outputs.
Definition: ShaderNode.h:477
ScopeInfo & getScopeInfo()
Return the scope info for this node.
Definition: ShaderNode.h:448
Class handling the shader generation implementation for a node.
Definition: ShaderNodeImpl.h:32
An output on a ShaderNode.
Definition: ShaderNode.h:289
ShaderInputSet & getConnections()
Return a set of connections to downstream node inputs, empty if not connected.
Definition: ShaderNode.h:295
const ShaderInputSet & getConnections() const
Return a set of connections to downstream node inputs, empty if not connected.
Definition: ShaderNode.h:299
Flags set on shader ports.
Definition: ShaderNode.h:115
An input or output port on a ShaderNode.
Definition: ShaderNode.h:125
ShaderNode * getNode()
Return the node this port belongs to.
Definition: ShaderNode.h:137
const TypeDesc * getType() const
Return the data type for this port.
Definition: ShaderNode.h:146
ValuePtr getValue() const
Return the value set on this port.
Definition: ShaderNode.h:173
void setFlag(uint32_t flag, bool value)
Set the on|off state of a given flag.
Definition: ShaderNode.h:201
bool isBindInput() const
Return the emitted state of this port.
Definition: ShaderNode.h:228
void setUnit(const string &unit)
Set a unit type for the value on this port.
Definition: ShaderNode.h:176
void setPath(const string &path)
Set the path to this port.
Definition: ShaderNode.h:189
const string & getName() const
Return the name of this port.
Definition: ShaderNode.h:152
void setMetadata(ShaderMetadataVecPtr metadata)
Set the metadata vector.
Definition: ShaderNode.h:231
const string & getGeomProp() const
Get geomprop name.
Definition: ShaderNode.h:186
void setFlags(uint32_t flags)
Set flags on this port.
Definition: ShaderNode.h:195
ShaderPortPtr getSelf()
Return a shared pointer instance of this object.
Definition: ShaderNode.h:131
void setName(const string &name)
Set the name of this port.
Definition: ShaderNode.h:149
const ShaderMetadataVecPtr & getMetadata() const
Get the metadata vector.
Definition: ShaderNode.h:237
const ShaderNode * getNode() const
Return the node this port belongs to.
Definition: ShaderNode.h:140
bool isEmitted() const
Return the emitted state of this port.
Definition: ShaderNode.h:222
void setGeomProp(const string &geomprop)
Set geomprop name if the input has a default geomprop to be assigned when it is unconnected.
Definition: ShaderNode.h:183
ShaderMetadataVecPtr getMetadata()
Get the metadata vector.
Definition: ShaderNode.h:234
void setBindInput()
Set the bind input state on this port to true.
Definition: ShaderNode.h:225
const string & getUnit() const
Return the unit type for the value on this port.
Definition: ShaderNode.h:179
void setUniform()
Set the uniform flag this port to true.
Definition: ShaderNode.h:213
void setType(const TypeDesc *type)
Set the data type for this port.
Definition: ShaderNode.h:143
const string & getVariable() const
Return the variable name of this port.
Definition: ShaderNode.h:161
void setSemantic(const string &semantic)
Set the variable semantic of this port.
Definition: ShaderNode.h:164
bool getFlag(uint32_t flag) const
Return the on|off state of a given flag.
Definition: ShaderNode.h:207
void setEmitted()
Set the emitted state on this port to true.
Definition: ShaderNode.h:219
bool isUniform() const
Return the uniform flag on this port.
Definition: ShaderNode.h:216
void setValue(ValuePtr value)
Set a value on this port.
Definition: ShaderNode.h:170
void setVariable(const string &name)
Set the variable name of this port.
Definition: ShaderNode.h:158
const string & getPath() const
Return the path to this port.
Definition: ShaderNode.h:192
const string & getSemantic() const
Return the variable semantic of this port.
Definition: ShaderNode.h:167
uint32_t getFlags() const
Return flags set on this port.
Definition: ShaderNode.h:198
A type descriptor for MaterialX data types.
Definition: TypeDesc.h:29
Metadata to be exported to generated shader.
Definition: ShaderNode.h:43
Information on source code scope for the node.
Definition: ShaderNode.h:368