6#ifndef MATERIALX_SHADERNODE_H
7#define MATERIALX_SHADERNODE_H
20MATERIALX_NAMESPACE_BEGIN
52using ShaderMetadataVec = vector<ShaderMetadata>;
53using ShaderMetadataVecPtr = shared_ptr<ShaderMetadataVec>;
61 static const string USER_DATA_NAME;
68 if (_entryIndex.count(name) == 0)
70 _entryIndex[name] = _entries.size();
71 _entries.emplace_back(name, type, value);
79 auto it = _entryIndex.find(name);
80 return it != _entryIndex.end() ? &_entries[it->second] :
nullptr;
87 auto it = _entryIndex.find(name);
88 return it != _entryIndex.end() ? &_entries[it->second] :
nullptr;
105 vector<ShaderMetadata> _entries;
106 std::unordered_map<string, size_t> _entryIndex;
109using ShaderMetadataRegistryPtr = shared_ptr<ShaderMetadataRegistry>;
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;
122class MX_GENSHADER_API
ShaderPort :
public std::enable_shared_from_this<ShaderPort>
131 return shared_from_this();
147 void setName(
const string& name) { _name = name; }
150 const string&
getName()
const {
return _name; }
162 void setSemantic(
const string& semantic) { _semantic = semantic; }
183 void setUnit(
const string& unit) { _unit = unit; }
186 const string&
getUnit()
const {
return _unit; }
190 void setGeomProp(
const string& geomprop) { _geomprop = geomprop; }
196 void setPath(
const string& path) { _path = path; }
199 const string&
getPath()
const {
return _path; }
210 _flags = value ? (_flags | flag) : (_flags & ~flag);
216 return ((_flags & flag) != 0);
223 bool isUniform()
const {
return (_flags & ShaderPortFlag::UNIFORM) != 0; }
229 bool isEmitted()
const {
return (_flags & ShaderPortFlag::EMITTED) != 0; }
235 bool isBindInput()
const {
return (_flags & ShaderPortFlag::BIND_INPUT) != 0; }
238 void setMetadata(ShaderMetadataVecPtr metadata) { _metadata = metadata; }
244 const ShaderMetadataVecPtr&
getMetadata()
const {
return _metadata; }
257 ShaderMetadataVecPtr _metadata;
329 static const uint32_t TEXTURE = 1 << 0;
330 static const uint32_t CLOSURE = 1 << 1;
331 static const uint32_t SHADER = 1 << 2;
332 static const uint32_t MATERIAL = 1 << 3;
334 static const uint32_t FILETEXTURE = 1 << 4;
335 static const uint32_t CONDITIONAL = 1 << 5;
336 static const uint32_t CONSTANT = 1 << 6;
338 static const uint32_t BSDF = 1 << 7;
339 static const uint32_t BSDF_R = 1 << 8;
340 static const uint32_t BSDF_T = 1 << 9;
341 static const uint32_t EDF = 1 << 10;
342 static const uint32_t VDF = 1 << 11;
343 static const uint32_t LAYER = 1 << 12;
345 static const uint32_t SURFACE = 1 << 13;
346 static const uint32_t VOLUME = 1 << 14;
347 static const uint32_t LIGHT = 1 << 15;
348 static const uint32_t UNLIT = 1 << 16;
350 static const uint32_t SAMPLE2D = 1 << 17;
351 static const uint32_t SAMPLE3D = 1 << 18;
352 static const uint32_t GEOMETRIC = 1 << 19;
353 static const uint32_t DOT = 1 << 20;
358 static const string CONSTANT;
359 static const string DOT;
360 static const string IMAGE;
361 static const string SURFACESHADER;
362 static const string SCATTER_MODE;
363 static const string BSDF_R;
364 static const string BSDF_T;
365 static const string TRANSFORM_POINT;
366 static const string TRANSFORM_VECTOR;
367 static const string TRANSFORM_NORMAL;
368 static const string TEXTURE2D_GROUPNAME;
369 static const string TEXTURE3D_GROUPNAME;
370 static const string PROCEDURAL2D_GROUPNAME;
371 static const string PROCEDURAL3D_GROUPNAME;
372 static const string GEOMETRIC_GROUPNAME;
384 unsigned int classification = Classification::TEXTURE);
407 return _classification;
413 _classification |= c;
419 return (_classification & c) == c;
444 size_t numOutputs()
const {
return _outputOrder.size(); }
448 ShaderOutput* getOutput(
size_t index = 0) {
return _outputOrder[index]; }
449 const ShaderInput* getInput(
size_t index)
const {
return _inputOrder[index]; }
450 const ShaderOutput* getOutput(
size_t index = 0)
const {
return _outputOrder[index]; }
455 const ShaderInput* getInput(
const string& name)
const;
456 const ShaderOutput* getOutput(
const string& name)
const;
459 const vector<ShaderInput*>&
getInputs()
const {
return _inputOrder; }
460 const vector<ShaderOutput*>& getOutputs()
const {
return _outputOrder; }
463 void setMetadata(ShaderMetadataVecPtr metadata) { _metadata = metadata; }
469 const ShaderMetadataVecPtr&
getMetadata()
const {
return _metadata; }
476 return (!_impl || _impl->isEditable(input));
484 return (!_impl || _impl->isEditable(input));
493 uint32_t _classification;
495 std::unordered_map<string, ShaderInputPtr> _inputMap;
496 vector<ShaderInput*> _inputOrder;
498 std::unordered_map<string, ShaderOutputPtr> _outputMap;
499 vector<ShaderOutput*> _outputOrder;
502 ShaderMetadataVecPtr _metadata;
507MATERIALX_NAMESPACE_END
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
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
Flags for classifying nodes into different categories.
Definition: ShaderNode.h:326
Class representing a node in the shader generation DAG.
Definition: ShaderNode.h:320
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:474
ShaderInput * getInput(size_t index)
Get inputs/outputs by index.
Definition: ShaderNode.h:447
const ShaderGraph * getParent() const
Return the parent graph that owns this node.
Definition: ShaderNode.h:392
const vector< ShaderInput * > & getInputs() const
Get vector of inputs/outputs.
Definition: ShaderNode.h:459
const string & getName() const
Return the name of this node.
Definition: ShaderNode.h:423
ShaderInput * addInput(const string &name, TypeDesc type)
Add inputs/outputs.
void setMetadata(ShaderMetadataVecPtr metadata)
Set the metadata vector.
Definition: ShaderNode.h:463
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:417
void addClassification(uint32_t c)
Add classification bits to this node.
Definition: ShaderNode.h:411
const ShaderMetadataVecPtr & getMetadata() const
Get the metadata vector.
Definition: ShaderNode.h:469
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:466
virtual bool isAGraph() const
Return true if this node is a graph.
Definition: ShaderNode.h:387
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:405
void setClassification(uint32_t c)
Set classification bits for this node, replacing any previous set bits.
Definition: ShaderNode.h:399
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:429
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:482
size_t numInputs() const
Get number of inputs/outputs.
Definition: ShaderNode.h:443
Class handling the shader generation implementation for a node.
Definition: ShaderNodeImpl.h:32
An output on a ShaderNode.
Definition: ShaderNode.h:295
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:301
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
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
void setType(TypeDesc type)
Set the data type for this port.
Definition: ShaderNode.h:141
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
string getFullName() const
Return the name of this port.
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
TypeDesc getType() const
Return the data type for this port.
Definition: ShaderNode.h:144
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
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
string getValueString() const
Return the value set on this port as a string, or an empty string if there is no value.
bool getFlag(uint32_t flag) const
Return the on|off state of a given flag.
Definition: ShaderNode.h:214
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: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:35