MaterialX 1.39.5
Loading...
Searching...
No Matches
ShaderNode.h
Go to the documentation of this file.
1//
2// Copyright Contributors to the MaterialX Project
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef MATERIALX_SHADERNODE_H
7#define MATERIALX_SHADERNODE_H
8
11
13
17
18#include <MaterialXCore/Node.h>
19
20MATERIALX_NAMESPACE_BEGIN
21
22class ShaderNode;
23class ShaderPort;
24class ShaderInput;
25class ShaderOutput;
26class ShaderGraph;
27
29using ShaderPortPtr = shared_ptr<class ShaderPort>;
31using ShaderInputPtr = shared_ptr<class ShaderInput>;
33using ShaderOutputPtr = shared_ptr<class ShaderOutput>;
35using ShaderNodePtr = shared_ptr<class ShaderNode>;
37using ShaderInputVec = vector<ShaderInput*>;
38
40struct MX_GENSHADER_API ShaderMetadata
41{
42 string name;
43 TypeDesc type;
44 ValuePtr value;
45 ShaderMetadata(const string& n, TypeDesc t, ValuePtr v = nullptr) :
46 name(n),
47 type(t),
48 value(v)
49 {
50 }
51};
52using ShaderMetadataVec = vector<ShaderMetadata>;
53using ShaderMetadataVecPtr = shared_ptr<ShaderMetadataVec>;
54
58class MX_GENSHADER_API ShaderMetadataRegistry : public GenUserData
59{
60 public:
61 static const string USER_DATA_NAME;
62
66 void addMetadata(const string& name, TypeDesc type, ValuePtr value = nullptr)
67 {
68 if (_entryIndex.count(name) == 0)
69 {
70 _entryIndex[name] = _entries.size();
71 _entries.emplace_back(name, type, value);
72 }
73 }
74
77 const ShaderMetadata* findMetadata(const string& name) const
78 {
79 auto it = _entryIndex.find(name);
80 return it != _entryIndex.end() ? &_entries[it->second] : nullptr;
81 }
82
85 ShaderMetadata* findMetadata(const string& name)
86 {
87 auto it = _entryIndex.find(name);
88 return it != _entryIndex.end() ? &_entries[it->second] : nullptr;
89 }
90
92 const ShaderMetadataVec& getAllMetadata() const
93 {
94 return _entries;
95 }
96
98 void clear()
99 {
100 _entryIndex.clear();
101 _entries.clear();
102 }
103
104 protected:
105 vector<ShaderMetadata> _entries;
106 std::unordered_map<string, size_t> _entryIndex;
107};
108
109using ShaderMetadataRegistryPtr = shared_ptr<ShaderMetadataRegistry>;
110
112class MX_GENSHADER_API ShaderPortFlag
113{
114 public:
115 static const uint32_t UNIFORM = 1u << 0;
116 static const uint32_t EMITTED = 1u << 1;
117 static const uint32_t BIND_INPUT = 1u << 2;
118 static const uint32_t AUTHORED_VALUE = 1u << 3;
119};
120
123class MX_GENSHADER_API ShaderPort : public std::enable_shared_from_this<ShaderPort>
124{
125 public:
127 ShaderPort(ShaderNode* node, TypeDesc type, const string& name, ValuePtr value = nullptr);
128
131 {
132 return shared_from_this();
133 }
134
136 ShaderNode* getNode() { return _node; }
137
139 const ShaderNode* getNode() const { return _node; }
140
142 void setType(TypeDesc type) { _type = type; }
143
145 TypeDesc getType() const { return _type; }
146
148 void setName(const string& name) { _name = name; }
149
151 const string& getName() const { return _name; }
152
154 string getFullName() const;
155
157 void setVariable(const string& name) { _variable = name; }
158
160 const string& getVariable() const { return _variable; }
161
163 void setSemantic(const string& semantic) { _semantic = semantic; }
164
166 const string& getSemantic() const { return _semantic; }
167
169 void setValue(ValuePtr value, bool authoredValue = true)
170 {
171 _value = value;
172 setFlag(ShaderPortFlag::AUTHORED_VALUE, authoredValue);
173 }
174
176 ValuePtr getValue() const { return _value; }
177
179 string getValueString() const;
180
182 void setColorSpace(const string& colorspace) { _colorspace = colorspace; }
183
185 const string& getColorSpace() const { return _colorspace; }
186
188 void setUnit(const string& unit) { _unit = unit; }
189
191 const string& getUnit() const { return _unit; }
192
195 void setGeomProp(const string& geomprop) { _geomprop = geomprop; }
196
198 const string& getGeomProp() const { return _geomprop; }
199
201 void setPath(const string& path) { _path = path; }
202
204 const string& getPath() const { return _path; }
205
207 void setFlags(uint32_t flags) { _flags = flags; }
208
210 uint32_t getFlags() const { return _flags; }
211
213 void setFlag(uint32_t flag, bool value)
214 {
215 _flags = value ? (_flags | flag) : (_flags & ~flag);
216 }
217
219 bool getFlag(uint32_t flag) const
220 {
221 return ((_flags & flag) != 0);
222 }
223
225 void setUniform() { _flags |= ShaderPortFlag::UNIFORM; }
226
228 bool isUniform() const { return (_flags & ShaderPortFlag::UNIFORM) != 0; }
229
231 void setEmitted() { _flags |= ShaderPortFlag::EMITTED; }
232
234 bool isEmitted() const { return (_flags & ShaderPortFlag::EMITTED) != 0; }
235
237 void setBindInput() { _flags |= ShaderPortFlag::BIND_INPUT; }
238
240 bool isBindInput() const { return (_flags & ShaderPortFlag::BIND_INPUT) != 0; }
241
242 // Has the value been overridden.
243 bool hasAuthoredValue() const { return (_flags & ShaderPortFlag::AUTHORED_VALUE) != 0; }
244
246 void setMetadata(ShaderMetadataVecPtr metadata) { _metadata = metadata; }
247
249 ShaderMetadataVecPtr getMetadata() { return _metadata; }
250
252 const ShaderMetadataVecPtr& getMetadata() const { return _metadata; }
253
254 protected:
255 ShaderNode* _node;
256 TypeDesc _type;
257 string _name;
258 string _path;
259 string _semantic;
260 string _variable;
261 ValuePtr _value;
262 string _unit;
263 string _colorspace;
264 string _geomprop;
265 ShaderMetadataVecPtr _metadata;
266 uint32_t _flags;
267};
268
271class MX_GENSHADER_API ShaderInput : public ShaderPort
272{
273 public:
274 ShaderInput(ShaderNode* node, TypeDesc type, const string& name);
275
278 ShaderOutput* getConnection() { return _connection; }
279
282 const ShaderOutput* getConnection() const { return _connection; }
283
285 void makeConnection(ShaderOutput* src);
286
289
293
294 protected:
295 ShaderOutput* _connection;
296 string _channels;
297 friend class ShaderOutput;
298};
299
302class MX_GENSHADER_API ShaderOutput : public ShaderPort
303{
304 public:
305 ShaderOutput(ShaderNode* node, TypeDesc type, const string& name);
306
309 const ShaderInputVec& getConnections() const { return _connections; }
310
312 void makeConnection(ShaderInput* dst);
313
315 void breakConnection(ShaderInput* dst);
316
319
320 protected:
321 ShaderInputVec _connections;
322 friend class ShaderInput;
323};
324
327class MX_GENSHADER_API ShaderNode
328{
329 public:
330 virtual ~ShaderNode() { }
331
334 {
335 public:
336 // Node classes
337 static const uint32_t TEXTURE = 1 << 0;
338 static const uint32_t CLOSURE = 1 << 1;
339 static const uint32_t SHADER = 1 << 2;
340 static const uint32_t MATERIAL = 1 << 3;
341 // Specific texture node types
342 static const uint32_t FILETEXTURE = 1 << 4;
343 static const uint32_t CONDITIONAL = 1 << 5;
344 static const uint32_t CONSTANT = 1 << 6;
345 // Specific closure types
346 static const uint32_t BSDF = 1 << 7;
347 static const uint32_t BSDF_R = 1 << 8;
348 static const uint32_t BSDF_T = 1 << 9;
349 static const uint32_t EDF = 1 << 10;
350 static const uint32_t VDF = 1 << 11;
351 static const uint32_t LAYER = 1 << 12;
352 static const uint32_t MIX = 1 << 13;
353 // Specific shader types
354 static const uint32_t SURFACE = 1 << 14;
355 static const uint32_t VOLUME = 1 << 15;
356 static const uint32_t LIGHT = 1 << 16;
357 static const uint32_t UNLIT = 1 << 17;
358 // Types based on nodegroup
359 static const uint32_t SAMPLE2D = 1 << 18;
360 static const uint32_t SAMPLE3D = 1 << 19;
361 static const uint32_t GEOMETRIC = 1 << 20;
362 static const uint32_t DOT = 1 << 21;
363 };
364
365 static const ShaderNodePtr NONE;
366
367 static const string CONSTANT;
368 static const string DOT;
369 static const string IMAGE;
370 static const string SURFACESHADER;
371 static const string BACKSURFACESHADER;
372 static const string BSDF_R;
373 static const string BSDF_T;
374 static const string TEXTURE2D_GROUPNAME;
375 static const string TEXTURE3D_GROUPNAME;
376 static const string PROCEDURAL2D_GROUPNAME;
377 static const string PROCEDURAL3D_GROUPNAME;
378 static const string GEOMETRIC_GROUPNAME;
379
380 public:
382 ShaderNode(const ShaderGraph* parent, const string& name);
383
385 static ShaderNodePtr create(const ShaderGraph* parent, const string& name, const NodeDef& nodeDef,
386 GenContext& context);
387
389 static ShaderNodePtr create(const ShaderGraph* parent, const string& name, ShaderNodeImplPtr impl,
390 unsigned int classification = Classification::TEXTURE);
391
393 virtual bool isAGraph() const { return false; }
394
398 const ShaderGraph* getParent() const
399 {
400 return _parent;
401 }
402
405 void setClassification(uint32_t c)
406 {
407 _classification = c;
408 }
409
411 uint32_t getClassification() const
412 {
413 return _classification;
414 }
415
417 void addClassification(uint32_t c)
418 {
419 _classification |= c;
420 }
421
423 bool hasClassification(uint32_t c) const
424 {
425 return (_classification & c) == c;
426 }
427
429 const string& getName() const
430 {
431 return _name;
432 }
433
436 const string& getUniqueId() const
437 {
438 return _uniqueId;
439 }
440
443 {
444 return *_impl;
445 }
446
449 void initialize(const Node& node, const NodeDef& nodeDef, GenContext& context);
450
452 ShaderInput* addInput(const string& name, TypeDesc type);
453 ShaderOutput* addOutput(const string& name, TypeDesc type);
454
456 size_t numInputs() const { return _inputOrder.size(); }
457 size_t numOutputs() const { return _outputOrder.size(); }
458
460 ShaderInput* getInput(size_t index) { return _inputOrder[index]; }
461 ShaderOutput* getOutput(size_t index = 0) { return _outputOrder[index]; }
462 const ShaderInput* getInput(size_t index) const { return _inputOrder[index]; }
463 const ShaderOutput* getOutput(size_t index = 0) const { return _outputOrder[index]; }
464
466 ShaderInput* getInput(const string& name);
467 ShaderOutput* getOutput(const string& name);
468 const ShaderInput* getInput(const string& name) const;
469 const ShaderOutput* getOutput(const string& name) const;
470
472 const vector<ShaderInput*>& getInputs() const { return _inputOrder; }
473 const vector<ShaderOutput*>& getOutputs() const { return _outputOrder; }
474
476 void setMetadata(ShaderMetadataVecPtr metadata) { _metadata = metadata; }
477
479 ShaderMetadataVecPtr getMetadata() { return _metadata; }
480
482 const ShaderMetadataVecPtr& getMetadata() const { return _metadata; }
483
487 bool isEditable(const ShaderInput& input) const
488 {
489 return (!_impl || _impl->isEditable(input));
490 }
491
495 bool isEditable(const ShaderGraphInputSocket& input) const
496 {
497 return (!_impl || _impl->isEditable(input));
498 }
499
500 protected:
502 void createMetadata(const NodeDef& nodeDef, GenContext& context);
503
504 const ShaderGraph* _parent;
505 string _name;
506 string _uniqueId;
507 uint32_t _classification;
508
509 std::unordered_map<string, ShaderInputPtr> _inputMap;
510 vector<ShaderInput*> _inputOrder;
511
512 std::unordered_map<string, ShaderOutputPtr> _outputMap;
513 vector<ShaderOutput*> _outputOrder;
514
515 ShaderNodeImplPtr _impl;
516 ShaderMetadataVecPtr _metadata;
517
518 friend class ShaderGraph;
519};
520
521MATERIALX_NAMESPACE_END
522
523#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:39
Node element subclasses.
ShaderOutput ShaderGraphInputSocket
An internal input socket in a shader graph, used for connecting internal nodes to the outside.
Definition ShaderGraph.h:33
shared_ptr< class ShaderPort > ShaderPortPtr
Shared pointer to a ShaderPort.
Definition ShaderNode.h:29
shared_ptr< class ShaderOutput > ShaderOutputPtr
Shared pointer to a ShaderOutput.
Definition ShaderNode.h:33
shared_ptr< class ShaderNode > ShaderNodePtr
Shared pointer to a ShaderNode.
Definition ShaderNode.h:35
vector< ShaderInput * > ShaderInputVec
A vector of ShaderInput pointers.
Definition ShaderNode.h:37
shared_ptr< class ShaderInput > ShaderInputPtr
Shared pointer to a ShaderInput.
Definition ShaderNode.h:31
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:30
A node definition element within a Document.
Definition Definition.h:82
A node element within a NodeGraph or Document.
Definition Node.h:53
Class representing a graph (DAG) for shader generation.
Definition ShaderGraph.h:45
An input on a ShaderNode.
Definition ShaderNode.h:272
void breakConnection()
Break the connection to this input.
void makeConnection(ShaderOutput *src)
Make a connection from the given source output to this input.
const ShaderOutput * getConnection() const
Return a connection to an upstream node output, or nullptr if not connected.
Definition ShaderNode.h:282
ShaderOutput * getConnection()
Return a connection to an upstream node output, or nullptr if not connected.
Definition ShaderNode.h:278
ShaderNode * getConnectedSibling() const
Return the sibling node connected upstream, or nullptr if there is no sibling upstream.
A registry for metadata that will be exported to the generated shader if found on nodes and inputs du...
Definition ShaderNode.h:59
void addMetadata(const string &name, TypeDesc type, ValuePtr value=nullptr)
Add a new metadata entry to the registry.
Definition ShaderNode.h:66
const ShaderMetadataVec & getAllMetadata() const
Return all entries in the registry.
Definition ShaderNode.h:92
ShaderMetadata * findMetadata(const string &name)
Return the metadata registered for the given name, or nullptr if no such entry is found.
Definition ShaderNode.h:85
void clear()
Clear all entries in the registry.
Definition ShaderNode.h:98
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:77
Flags for classifying nodes into different categories.
Definition ShaderNode.h:334
static const uint32_t EDF
A transmission BSDF node.
Definition ShaderNode.h:349
static const uint32_t CONDITIONAL
A file texture node.
Definition ShaderNode.h:343
static const uint32_t SAMPLE3D
Can be sampled in 2D (uv space)
Definition ShaderNode.h:360
static const uint32_t VOLUME
A surface shader node.
Definition ShaderNode.h:355
static const uint32_t GEOMETRIC
Can be sampled in 3D (position)
Definition ShaderNode.h:361
static const uint32_t MIX
A node for vertical layering of other closure nodes.
Definition ShaderNode.h:352
static const uint32_t CONSTANT
A conditional node.
Definition ShaderNode.h:344
static const uint32_t SHADER
Any node that represents light integration.
Definition ShaderNode.h:339
static const uint32_t FILETEXTURE
Any node that outputs a material.
Definition ShaderNode.h:342
static const uint32_t DOT
Geometric input.
Definition ShaderNode.h:362
static const uint32_t BSDF
A constant node.
Definition ShaderNode.h:346
static const uint32_t SURFACE
A node for mixing of other closure nodes.
Definition ShaderNode.h:354
static const uint32_t LIGHT
A volume shader node.
Definition ShaderNode.h:356
static const uint32_t VDF
A EDF node.
Definition ShaderNode.h:350
static const uint32_t SAMPLE2D
An unlit surface shader node.
Definition ShaderNode.h:359
static const uint32_t LAYER
A VDF node.
Definition ShaderNode.h:351
static const uint32_t MATERIAL
Any node that outputs a shader.
Definition ShaderNode.h:340
static const uint32_t BSDF_R
A BSDF node.
Definition ShaderNode.h:347
static const uint32_t BSDF_T
A reflection BSDF node.
Definition ShaderNode.h:348
static const uint32_t CLOSURE
Any node that outputs floats, colors, vectors, etc.
Definition ShaderNode.h:338
static const uint32_t UNLIT
A light shader node.
Definition ShaderNode.h:357
Class representing a node in the shader generation DAG.
Definition ShaderNode.h:328
ShaderInput * getInput(const string &name)
Get inputs/outputs by name.
bool isEditable(const ShaderInput &input) const
Returns true if an input is editable by users.
Definition ShaderNode.h:487
ShaderInput * getInput(size_t index)
Get inputs/outputs by index.
Definition ShaderNode.h:460
const ShaderGraph * getParent() const
Return the parent graph that owns this node.
Definition ShaderNode.h:398
const vector< ShaderInput * > & getInputs() const
Get vector of inputs/outputs.
Definition ShaderNode.h:472
const string & getName() const
Return the name of this node.
Definition ShaderNode.h:429
ShaderInput * addInput(const string &name, TypeDesc type)
Add inputs/outputs.
void setMetadata(ShaderMetadataVecPtr metadata)
Set the metadata vector.
Definition ShaderNode.h:476
static ShaderNodePtr create(const ShaderGraph *parent, const string &name, const NodeDef &nodeDef, GenContext &context)
Create a new node from a nodedef.
bool hasClassification(uint32_t c) const
Return true if this node matches the given classification.
Definition ShaderNode.h:423
void addClassification(uint32_t c)
Add classification bits to this node.
Definition ShaderNode.h:417
const ShaderMetadataVecPtr & getMetadata() const
Get the metadata vector.
Definition ShaderNode.h:482
void initialize(const Node &node, const NodeDef &nodeDef, GenContext &context)
Initialize this shader node with all required data from the given node and nodedef.
ShaderMetadataVecPtr getMetadata()
Get the metadata vector.
Definition ShaderNode.h:479
const string & getUniqueId() const
Return the unique identifier for this node, used as its key in the parent graph's node map.
Definition ShaderNode.h:436
virtual bool isAGraph() const
Return true if this node is a graph.
Definition ShaderNode.h:393
static ShaderNodePtr create(const ShaderGraph *parent, const string &name, ShaderNodeImplPtr impl, unsigned int classification=Classification::TEXTURE)
Create a new node from a node implementation.
uint32_t getClassification() const
Get classification bits set for this node.
Definition ShaderNode.h:411
void setClassification(uint32_t c)
Set classification bits for this node, replacing any previous set bits.
Definition ShaderNode.h:405
void createMetadata(const NodeDef &nodeDef, GenContext &context)
Create metadata from the nodedef according to registered metadata.
const ShaderNodeImpl & getImplementation() const
Return the implementation used for this node.
Definition ShaderNode.h:442
ShaderNode(const ShaderGraph *parent, const string &name)
Constructor.
bool isEditable(const ShaderGraphInputSocket &input) const
Returns true if a graph input is accessible by users.
Definition ShaderNode.h:495
size_t numInputs() const
Get number of inputs/outputs.
Definition ShaderNode.h:456
Class handling the shader generation implementation for a node.
Definition ShaderNodeImpl.h:32
An output on a ShaderNode.
Definition ShaderNode.h:303
void makeConnection(ShaderInput *dst)
Make a connection from this output to the given input.
void breakConnection(ShaderInput *dst)
Break a connection from this output to the given input.
void breakConnections()
Break all connections from this output.
const ShaderInputVec & getConnections() const
Return a set of connections to downstream node inputs, empty if not connected.
Definition ShaderNode.h:309
Flags set on shader ports.
Definition ShaderNode.h:113
An input or output port on a ShaderNode.
Definition ShaderNode.h:124
ShaderNode * getNode()
Return the node this port belongs to.
Definition ShaderNode.h:136
ValuePtr getValue() const
Return the value set on this port.
Definition ShaderNode.h:176
void setFlag(uint32_t flag, bool value)
Set the on|off state of a given flag.
Definition ShaderNode.h:213
bool isBindInput() const
Return the emitted state of this port.
Definition ShaderNode.h:240
void setUnit(const string &unit)
Set a unit type for the value on this port.
Definition ShaderNode.h:188
void setPath(const string &path)
Set the path to this port.
Definition ShaderNode.h:201
void setType(TypeDesc type)
Set the data type for this port.
Definition ShaderNode.h:142
const string & getColorSpace() const
Return the source color space for the value on this port.
Definition ShaderNode.h:185
const string & getName() const
Return the name of this port.
Definition ShaderNode.h:151
void setColorSpace(const string &colorspace)
Set a source color space for the value on this port.
Definition ShaderNode.h:182
string getFullName() const
Return the name of this port.
void setMetadata(ShaderMetadataVecPtr metadata)
Set the metadata vector.
Definition ShaderNode.h:246
const string & getGeomProp() const
Get geomprop name.
Definition ShaderNode.h:198
void setFlags(uint32_t flags)
Set flags on this port.
Definition ShaderNode.h:207
ShaderPortPtr getSelf()
Return a shared pointer instance of this object.
Definition ShaderNode.h:130
void setName(const string &name)
Set the name of this port.
Definition ShaderNode.h:148
const ShaderMetadataVecPtr & getMetadata() const
Get the metadata vector.
Definition ShaderNode.h:252
const ShaderNode * getNode() const
Return the node this port belongs to.
Definition ShaderNode.h:139
TypeDesc getType() const
Return the data type for this port.
Definition ShaderNode.h:145
bool isEmitted() const
Return the emitted state of this port.
Definition ShaderNode.h:234
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:195
ShaderMetadataVecPtr getMetadata()
Get the metadata vector.
Definition ShaderNode.h:249
void setBindInput()
Set the bind input state on this port to true.
Definition ShaderNode.h:237
const string & getUnit() const
Return the unit type for the value on this port.
Definition ShaderNode.h:191
void setUniform()
Set the uniform flag this port to true.
Definition ShaderNode.h:225
const string & getVariable() const
Return the variable name of this port.
Definition ShaderNode.h:160
void setSemantic(const string &semantic)
Set the variable semantic of this port.
Definition ShaderNode.h:163
string getValueString() const
Return the value set on this port as a string, or an empty string if there is no value.
void setValue(ValuePtr value, bool authoredValue=true)
Set a value on this port.
Definition ShaderNode.h:169
bool getFlag(uint32_t flag) const
Return the on|off state of a given flag.
Definition ShaderNode.h:219
ShaderPort(ShaderNode *node, TypeDesc type, const string &name, ValuePtr value=nullptr)
Constructor.
void setEmitted()
Set the emitted state on this port to true.
Definition ShaderNode.h:231
bool isUniform() const
Return the uniform flag on this port.
Definition ShaderNode.h:228
void setVariable(const string &name)
Set the variable name of this port.
Definition ShaderNode.h:157
const string & getPath() const
Return the path to this port.
Definition ShaderNode.h:204
const string & getSemantic() const
Return the variable semantic of this port.
Definition ShaderNode.h:166
uint32_t getFlags() const
Return flags set on this port.
Definition ShaderNode.h:210
A type descriptor for MaterialX data types.
Definition TypeDesc.h:40
Metadata to be exported to generated shader.
Definition ShaderNode.h:41