MaterialX 1.38.10
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 const TypeDesc* type;
44 ValuePtr value;
45 ShaderMetadata(const string& n, const 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, const 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};
119
122class MX_GENSHADER_API ShaderPort : public std::enable_shared_from_this<ShaderPort>
123{
124 public:
126 ShaderPort(ShaderNode* node, const TypeDesc* type, const string& name, ValuePtr value = nullptr);
127
130 {
131 return shared_from_this();
132 }
133
135 ShaderNode* getNode() { return _node; }
136
138 const ShaderNode* getNode() const { return _node; }
139
141 void setType(const TypeDesc* type) { _type = type; }
142
144 const TypeDesc* getType() const { return _type; }
145
147 void setName(const string& name) { _name = name; }
148
150 const string& getName() const { return _name; }
151
153 string getFullName() const;
154
156 void setVariable(const string& name) { _variable = name; }
157
159 const string& getVariable() const { return _variable; }
160
162 void setSemantic(const string& semantic) { _semantic = semantic; }
163
165 const string& getSemantic() const { return _semantic; }
166
168 void setValue(ValuePtr value) { _value = value; }
169
171 ValuePtr getValue() const { return _value; }
172
174 string getValueString() const;
175
177 void setColorSpace(const string& colorspace) { _colorspace = colorspace; }
178
180 const string& getColorSpace() const { return _colorspace; }
181
183 void setUnit(const string& unit) { _unit = unit; }
184
186 const string& getUnit() const { return _unit; }
187
190 void setGeomProp(const string& geomprop) { _geomprop = geomprop; }
191
193 const string& getGeomProp() const { return _geomprop; }
194
196 void setPath(const string& path) { _path = path; }
197
199 const string& getPath() const { return _path; }
200
202 void setFlags(uint32_t flags) { _flags = flags; }
203
205 uint32_t getFlags() const { return _flags; }
206
208 void setFlag(uint32_t flag, bool value)
209 {
210 _flags = value ? (_flags | flag) : (_flags & ~flag);
211 }
212
214 bool getFlag(uint32_t flag) const
215 {
216 return ((_flags & flag) != 0);
217 }
218
220 void setUniform() { _flags |= ShaderPortFlag::UNIFORM; }
221
223 bool isUniform() const { return (_flags & ShaderPortFlag::UNIFORM) != 0; }
224
226 void setEmitted() { _flags |= ShaderPortFlag::EMITTED; }
227
229 bool isEmitted() const { return (_flags & ShaderPortFlag::EMITTED) != 0; }
230
232 void setBindInput() { _flags |= ShaderPortFlag::BIND_INPUT; }
233
235 bool isBindInput() const { return (_flags & ShaderPortFlag::BIND_INPUT) != 0; }
236
238 void setMetadata(ShaderMetadataVecPtr metadata) { _metadata = metadata; }
239
241 ShaderMetadataVecPtr getMetadata() { return _metadata; }
242
244 const ShaderMetadataVecPtr& getMetadata() const { return _metadata; }
245
246 protected:
247 ShaderNode* _node;
248 const TypeDesc* _type;
249 string _name;
250 string _path;
251 string _semantic;
252 string _variable;
253 ValuePtr _value;
254 string _unit;
255 string _colorspace;
256 string _geomprop;
257 ShaderMetadataVecPtr _metadata;
258 uint32_t _flags;
259};
260
263class MX_GENSHADER_API ShaderInput : public ShaderPort
264{
265 public:
266 ShaderInput(ShaderNode* node, const TypeDesc* type, const string& name);
267
270 ShaderOutput* getConnection() { return _connection; }
271
274 const ShaderOutput* getConnection() const { return _connection; }
275
277 void makeConnection(ShaderOutput* src);
278
280 void breakConnection();
281
283 void setChannels(const string& channels) { _channels = channels; }
284
286 const string& getChannels() const { return _channels; }
287
290 ShaderNode* getConnectedSibling() const;
291
292 protected:
293 ShaderOutput* _connection;
294 string _channels;
295 friend class ShaderOutput;
296};
297
300class MX_GENSHADER_API ShaderOutput : public ShaderPort
301{
302 public:
303 ShaderOutput(ShaderNode* node, const TypeDesc* type, const string& name);
304
307 const ShaderInputVec& getConnections() const { return _connections; }
308
310 void makeConnection(ShaderInput* dst);
311
313 void breakConnection(ShaderInput* dst);
314
316 void breakConnections();
317
318 protected:
319 ShaderInputVec _connections;
320 friend class ShaderInput;
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 static const uint32_t MATERIAL = 1 << 3;
339 // Specific texture node types
340 static const uint32_t FILETEXTURE = 1 << 4;
341 static const uint32_t CONDITIONAL = 1 << 5;
342 static const uint32_t CONSTANT = 1 << 6;
343 // Specific closure types
344 static const uint32_t BSDF = 1 << 7;
345 static const uint32_t BSDF_R = 1 << 8;
346 static const uint32_t BSDF_T = 1 << 9;
347 static const uint32_t EDF = 1 << 10;
348 static const uint32_t VDF = 1 << 11;
349 static const uint32_t LAYER = 1 << 12;
350 static const uint32_t THINFILM = 1 << 13;
351 // Specific shader types
352 static const uint32_t SURFACE = 1 << 14;
353 static const uint32_t VOLUME = 1 << 15;
354 static const uint32_t LIGHT = 1 << 16;
355 static const uint32_t UNLIT = 1 << 17;
356 // Types based on nodegroup
357 static const uint32_t SAMPLE2D = 1 << 18;
358 static const uint32_t SAMPLE3D = 1 << 19;
359 static const uint32_t GEOMETRIC = 1 << 20;
360 static const uint32_t DOT = 1 << 21;
361 };
362
363 static const ShaderNodePtr NONE;
364
365 static const string CONSTANT;
366 static const string DOT;
367 static const string IMAGE;
368 static const string SURFACESHADER;
369 static const string SCATTER_MODE;
370 static const string BSDF_R;
371 static const string BSDF_T;
372 static const string TRANSFORM_POINT;
373 static const string TRANSFORM_VECTOR;
374 static const string TRANSFORM_NORMAL;
375 static const string TEXTURE2D_GROUPNAME;
376 static const string TEXTURE3D_GROUPNAME;
377 static const string PROCEDURAL2D_GROUPNAME;
378 static const string PROCEDURAL3D_GROUPNAME;
379 static const string GEOMETRIC_GROUPNAME;
380
381 public:
383 ShaderNode(const ShaderGraph* parent, const string& name);
384
386 static ShaderNodePtr create(const ShaderGraph* parent, const string& name, const NodeDef& nodeDef,
387 GenContext& context);
388
390 static ShaderNodePtr create(const ShaderGraph* parent, const string& name, ShaderNodeImplPtr impl,
391 unsigned int classification = Classification::TEXTURE);
392
394 virtual bool isAGraph() const { return false; }
395
399 const ShaderGraph* getParent() const
400 {
401 return _parent;
402 }
403
406 void setClassification(uint32_t c)
407 {
408 _classification = c;
409 }
410
412 uint32_t getClassification() const
413 {
414 return _classification;
415 }
416
418 void addClassification(uint32_t c)
419 {
420 _classification |= c;
421 }
422
424 bool hasClassification(uint32_t c) const
425 {
426 return (_classification & c) == c;
427 }
428
430 const string& getName() const
431 {
432 return _name;
433 }
434
437 {
438 return *_impl;
439 }
440
443 void initialize(const Node& node, const NodeDef& nodeDef, GenContext& context);
444
446 ShaderInput* addInput(const string& name, const TypeDesc* type);
447 ShaderOutput* addOutput(const string& name, const TypeDesc* type);
448
450 size_t numInputs() const { return _inputOrder.size(); }
451 size_t numOutputs() const { return _outputOrder.size(); }
452
454 ShaderInput* getInput(size_t index) { return _inputOrder[index]; }
455 ShaderOutput* getOutput(size_t index = 0) { return _outputOrder[index]; }
456 const ShaderInput* getInput(size_t index) const { return _inputOrder[index]; }
457 const ShaderOutput* getOutput(size_t index = 0) const { return _outputOrder[index]; }
458
460 ShaderInput* getInput(const string& name);
461 ShaderOutput* getOutput(const string& name);
462 const ShaderInput* getInput(const string& name) const;
463 const ShaderOutput* getOutput(const string& name) const;
464
466 const vector<ShaderInput*>& getInputs() const { return _inputOrder; }
467 const vector<ShaderOutput*>& getOutputs() const { return _outputOrder; }
468
470 void setMetadata(ShaderMetadataVecPtr metadata) { _metadata = metadata; }
471
473 ShaderMetadataVecPtr getMetadata() { return _metadata; }
474
476 const ShaderMetadataVecPtr& getMetadata() const { return _metadata; }
477
481 bool isEditable(const ShaderInput& input) const
482 {
483 return (!_impl || _impl->isEditable(input));
484 }
485
489 bool isEditable(const ShaderGraphInputSocket& input) const
490 {
491 return (!_impl || _impl->isEditable(input));
492 }
493
494 protected:
496 void createMetadata(const NodeDef& nodeDef, GenContext& context);
497
498 const ShaderGraph* _parent;
499 string _name;
500 uint32_t _classification;
501
502 std::unordered_map<string, ShaderInputPtr> _inputMap;
503 vector<ShaderInput*> _inputOrder;
504
505 std::unordered_map<string, ShaderOutputPtr> _outputMap;
506 vector<ShaderOutput*> _outputOrder;
507
508 ShaderNodeImplPtr _impl;
509 ShaderMetadataVecPtr _metadata;
510
511 friend class ShaderGraph;
512};
513
514MATERIALX_NAMESPACE_END
515
516#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.
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:29
A context class for shader generation.
Definition: GenContext.h:31
Base class for custom user data needed during shader generation.
Definition: GenUserData.h:27
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:44
An input on a ShaderNode.
Definition: ShaderNode.h:264
void setChannels(const string &channels)
Set optional channels value.
Definition: ShaderNode.h:283
const ShaderOutput * getConnection() const
Return a connection to an upstream node output, or nullptr if not connected.
Definition: ShaderNode.h:274
ShaderOutput * getConnection()
Return a connection to an upstream node output, or nullptr if not connected.
Definition: ShaderNode.h:270
const string & getChannels() const
Get optional channels value.
Definition: ShaderNode.h:286
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, const 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:332
Class representing a node in the shader generation DAG.
Definition: ShaderNode.h:326
bool isEditable(const ShaderInput &input) const
Returns true if an input is editable by users.
Definition: ShaderNode.h:481
ShaderInput * getInput(size_t index)
Get inputs/outputs by index.
Definition: ShaderNode.h:454
const ShaderGraph * getParent() const
Return the parent graph that owns this node.
Definition: ShaderNode.h:399
const vector< ShaderInput * > & getInputs() const
Get vector of inputs/outputs.
Definition: ShaderNode.h:466
const string & getName() const
Return the name of this node.
Definition: ShaderNode.h:430
void setMetadata(ShaderMetadataVecPtr metadata)
Set the metadata vector.
Definition: ShaderNode.h:470
bool hasClassification(uint32_t c) const
Return true if this node matches the given classification.
Definition: ShaderNode.h:424
void addClassification(uint32_t c)
Add classification bits to this node.
Definition: ShaderNode.h:418
const ShaderMetadataVecPtr & getMetadata() const
Get the metadata vector.
Definition: ShaderNode.h:476
ShaderMetadataVecPtr getMetadata()
Get the metadata vector.
Definition: ShaderNode.h:473
virtual bool isAGraph() const
Return true if this node is a graph.
Definition: ShaderNode.h:394
uint32_t getClassification() const
Get classification bits set for this node.
Definition: ShaderNode.h:412
void setClassification(uint32_t c)
Set classification bits for this node, replacing any previous set bits.
Definition: ShaderNode.h:406
const ShaderNodeImpl & getImplementation() const
Return the implementation used for this node.
Definition: ShaderNode.h:436
bool isEditable(const ShaderGraphInputSocket &input) const
Returns true if a graph input is accessible by users.
Definition: ShaderNode.h:489
size_t numInputs() const
Get number of inputs/outputs.
Definition: ShaderNode.h:450
Class handling the shader generation implementation for a node.
Definition: ShaderNodeImpl.h:31
An output on a ShaderNode.
Definition: ShaderNode.h:301
const ShaderInputVec & getConnections() const
Return a set of connections to downstream node inputs, empty if not connected.
Definition: ShaderNode.h:307
Flags set on shader ports.
Definition: ShaderNode.h:113
An input or output port on a ShaderNode.
Definition: ShaderNode.h:123
ShaderNode * getNode()
Return the node this port belongs to.
Definition: ShaderNode.h:135
const TypeDesc * getType() const
Return the data type for this port.
Definition: ShaderNode.h:144
ValuePtr getValue() const
Return the value set on this port.
Definition: ShaderNode.h:171
void setFlag(uint32_t flag, bool value)
Set the on|off state of a given flag.
Definition: ShaderNode.h:208
bool isBindInput() const
Return the emitted state of this port.
Definition: ShaderNode.h:235
void setUnit(const string &unit)
Set a unit type for the value on this port.
Definition: ShaderNode.h:183
void setPath(const string &path)
Set the path to this port.
Definition: ShaderNode.h:196
const string & getColorSpace() const
Return the source color space for the value on this port.
Definition: ShaderNode.h:180
const string & getName() const
Return the name of this port.
Definition: ShaderNode.h:150
void setColorSpace(const string &colorspace)
Set a source color space for the value on this port.
Definition: ShaderNode.h:177
void setMetadata(ShaderMetadataVecPtr metadata)
Set the metadata vector.
Definition: ShaderNode.h:238
const string & getGeomProp() const
Get geomprop name.
Definition: ShaderNode.h:193
void setFlags(uint32_t flags)
Set flags on this port.
Definition: ShaderNode.h:202
ShaderPortPtr getSelf()
Return a shared pointer instance of this object.
Definition: ShaderNode.h:129
void setName(const string &name)
Set the name of this port.
Definition: ShaderNode.h:147
const ShaderMetadataVecPtr & getMetadata() const
Get the metadata vector.
Definition: ShaderNode.h:244
const ShaderNode * getNode() const
Return the node this port belongs to.
Definition: ShaderNode.h:138
bool isEmitted() const
Return the emitted state of this port.
Definition: ShaderNode.h:229
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:190
ShaderMetadataVecPtr getMetadata()
Get the metadata vector.
Definition: ShaderNode.h:241
void setBindInput()
Set the bind input state on this port to true.
Definition: ShaderNode.h:232
const string & getUnit() const
Return the unit type for the value on this port.
Definition: ShaderNode.h:186
void setUniform()
Set the uniform flag this port to true.
Definition: ShaderNode.h:220
void setType(const TypeDesc *type)
Set the data type for this port.
Definition: ShaderNode.h:141
const string & getVariable() const
Return the variable name of this port.
Definition: ShaderNode.h:159
void setSemantic(const string &semantic)
Set the variable semantic of this port.
Definition: ShaderNode.h:162
bool getFlag(uint32_t flag) const
Return the on|off state of a given flag.
Definition: ShaderNode.h:214
void setEmitted()
Set the emitted state on this port to true.
Definition: ShaderNode.h:226
bool isUniform() const
Return the uniform flag on this port.
Definition: ShaderNode.h:223
void setValue(ValuePtr value)
Set a value on this port.
Definition: ShaderNode.h:168
void setVariable(const string &name)
Set the variable name of this port.
Definition: ShaderNode.h:156
const string & getPath() const
Return the path to this port.
Definition: ShaderNode.h:199
const string & getSemantic() const
Return the variable semantic of this port.
Definition: ShaderNode.h:165
uint32_t getFlags() const
Return flags set on this port.
Definition: ShaderNode.h:205
A type descriptor for MaterialX data types.
Definition: TypeDesc.h:28
Metadata to be exported to generated shader.
Definition: ShaderNode.h:41