6#ifndef MATERIALX_SHADERNODE_H
7#define MATERIALX_SHADERNODE_H
53using ShaderMetadataVec = vector<ShaderMetadata>;
54using ShaderMetadataVecPtr = shared_ptr<ShaderMetadataVec>;
62 static const string USER_DATA_NAME;
69 if (_entryIndex.count(name) == 0)
71 _entryIndex[name] = _entries.size();
72 _entries.emplace_back(name, type, value);
80 auto it = _entryIndex.find(name);
81 return it != _entryIndex.end() ? &_entries[it->second] :
nullptr;
88 auto it = _entryIndex.find(name);
89 return it != _entryIndex.end() ? &_entries[it->second] :
nullptr;
106 vector<ShaderMetadata> _entries;
107 std::unordered_map<string, size_t> _entryIndex;
110using ShaderMetadataRegistryPtr = shared_ptr<ShaderMetadataRegistry>;
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;
124class MX_GENSHADER_API
ShaderPort :
public std::enable_shared_from_this<ShaderPort>
133 return shared_from_this();
149 void setName(
const string& name) { _name = name; }
152 const string&
getName()
const {
return _name; }
155 string getFullName()
const;
164 void setSemantic(
const string& semantic) { _semantic = semantic; }
176 void setUnit(
const string& unit) { _unit = unit; }
179 const string&
getUnit()
const {
return _unit; }
183 void setGeomProp(
const string& geomprop) { _geomprop = geomprop; }
189 void setPath(
const string& path) { _path = path; }
192 const string&
getPath()
const {
return _path; }
203 _flags = value ? (_flags | flag) : (_flags & ~flag);
209 return ((_flags & flag) != 0);
216 bool isUniform()
const {
return (_flags & ShaderPortFlag::UNIFORM) != 0; }
222 bool isEmitted()
const {
return (_flags & ShaderPortFlag::EMITTED) != 0; }
228 bool isBindInput()
const {
return (_flags & ShaderPortFlag::BIND_INPUT) != 0; }
231 void setMetadata(ShaderMetadataVecPtr metadata) { _metadata = metadata; }
237 const ShaderMetadataVecPtr&
getMetadata()
const {
return _metadata; }
249 ShaderMetadataVecPtr _metadata;
272 void breakConnection();
275 void setChannels(
const string& channels) { _channels = channels; }
308 void breakConnections();
335 static const uint32_t TEXTURE = 1 << 0;
336 static const uint32_t CLOSURE = 1 << 1;
337 static const uint32_t SHADER = 1 << 2;
339 static const uint32_t FILETEXTURE = 1 << 3;
340 static const uint32_t CONDITIONAL = 1 << 4;
341 static const uint32_t CONSTANT = 1 << 5;
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;
351 static const uint32_t SURFACE = 1 << 13;
352 static const uint32_t VOLUME = 1 << 14;
353 static const uint32_t LIGHT = 1 << 15;
355 static const uint32_t IFELSE = 1 << 16;
356 static const uint32_t SWITCH = 1 << 17;
358 static const uint32_t SAMPLE2D = 1 << 18;
359 static const uint32_t SAMPLE3D = 1 << 19;
377 ScopeInfo() : type(UNKNOWN), conditionalNode(
nullptr), conditionBitmask(0), fullConditionMask(0) {}
380 void adjustAtConditionalInput(
ShaderNode* condNode,
int branch, uint32_t fullMask);
381 bool usedByBranch(
int branchIndex)
const {
return (conditionBitmask & (1 << branchIndex)) != 0; }
385 uint32_t conditionBitmask;
386 uint32_t fullConditionMask;
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;
416 unsigned int classification = Classification::TEXTURE);
432 return (_classification & c) == c;
460 bool referencedConditionally()
const;
465 return _usedClosures.count(node) > 0;
478 size_t numOutputs()
const {
return _outputOrder.size(); }
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]; }
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;
493 const vector<ShaderInput*>&
getInputs()
const {
return _inputOrder; }
494 const vector<ShaderOutput*>& getOutputs()
const {
return _outputOrder; }
497 void setMetadata(ShaderMetadataVecPtr metadata) { _metadata = metadata; }
503 const ShaderMetadataVecPtr&
getMetadata()
const {
return _metadata; }
510 return (!_impl || _impl->isEditable(input));
518 return (!_impl || _impl->isEditable(input));
524 _flags = value ? (_flags | uint32_t(flag)) : (_flags & ~uint32_t(flag));
530 return ((_flags & uint32_t(flag)) != 0);
539 uint32_t _classification;
542 std::unordered_map<string, ShaderInputPtr> _inputMap;
543 vector<ShaderInput*> _inputOrder;
545 std::unordered_map<string, ShaderOutputPtr> _outputMap;
546 vector<ShaderOutput*> _outputOrder;
549 ShaderMetadataVecPtr _metadata;
550 ScopeInfo _scopeInfo;
551 std::set<const ShaderNode*> _usedClosures;
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
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
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
Information on source code scope for the node.
Definition: ShaderNode.h:368