MaterialX 1.38.9
Loading...
Searching...
No Matches
ShaderGraph Class Reference

Class representing a graph (DAG) for shader generation. More...

#include <ShaderGraph.h>

Inheritance diagram for ShaderGraph:
ShaderNode

Public Member Functions

 ShaderGraph (const ShaderGraph *parent, const string &name, ConstDocumentPtr document, const StringSet &reservedWords)
 Constructor.
 
virtual ~ShaderGraph ()
 Desctructor.
 
bool isAGraph () const override
 Return true if this node is a graph. More...
 
ShaderNodegetNode (const string &name)
 Get an internal node by name.
 
const ShaderNodegetNode (const string &name) const
 Get an internal node by name.
 
const vector< ShaderNode * > & getNodes () const
 Get a vector of all nodes in order.
 
size_t numInputSockets () const
 Get number of input sockets.
 
size_t numOutputSockets () const
 Get number of output sockets.
 
ShaderGraphInputSocketgetInputSocket (size_t index)
 Get socket by index.
 
ShaderGraphOutputSocketgetOutputSocket (size_t index=0)
 
const ShaderGraphInputSocketgetInputSocket (size_t index) const
 
const ShaderGraphOutputSocketgetOutputSocket (size_t index=0) const
 
ShaderGraphInputSocketgetInputSocket (const string &name)
 Get socket by name.
 
ShaderGraphOutputSocketgetOutputSocket (const string &name)
 
const ShaderGraphInputSocketgetInputSocket (const string &name) const
 
const ShaderGraphOutputSocketgetOutputSocket (const string &name) const
 
const vector< ShaderGraphInputSocket * > & getInputSockets () const
 Get vector of sockets.
 
const vector< ShaderGraphOutputSocket * > & getOutputSockets () const
 
ShaderNodecreateNode (ConstNodePtr node, GenContext &context)
 Create a new node in the graph.
 
ShaderGraphInputSocketaddInputSocket (const string &name, const TypeDesc *type)
 Add input/output sockets.
 
ShaderGraphOutputSocketaddOutputSocket (const string &name, const TypeDesc *type)
 
void addDefaultGeomNode (ShaderInput *input, const GeomPropDef &geomprop, GenContext &context)
 Add a default geometric node and connect to the given input.
 
void topologicalSort ()
 Sort the nodes in topological order.
 
IdentifierMapgetIdentifierMap ()
 Return the map of unique identifiers used in the scope of this graph.
 
- Public Member Functions inherited from ShaderNode
 ShaderNode (const ShaderGraph *parent, const string &name)
 Constructor.
 
virtual bool isAGraph () const
 Return true if this node is a graph. More...
 
const ShaderGraphgetParent () const
 Return the parent graph that owns this node. More...
 
void setClassification (uint32_t c)
 Set classification bits for this node, replacing any previous set bits.
 
uint32_t getClassification () const
 Get classification bits set for this node.
 
void addClassification (uint32_t c)
 Add classification bits to this node.
 
bool hasClassification (uint32_t c) const
 Return true if this node matches the given classification.
 
const string & getName () const
 Return the name of this node.
 
const ShaderNodeImplgetImplementation () const
 Return the implementation used for this node.
 
void initialize (const Node &node, const NodeDef &nodeDef, GenContext &context)
 Initialize this shader node with all required data from the given node and nodedef.
 
ShaderInputaddInput (const string &name, const TypeDesc *type)
 Add inputs/outputs.
 
ShaderOutputaddOutput (const string &name, const TypeDesc *type)
 
size_t numInputs () const
 Get number of inputs/outputs.
 
size_t numOutputs () const
 
ShaderInputgetInput (size_t index)
 Get inputs/outputs by index.
 
ShaderOutputgetOutput (size_t index=0)
 
const ShaderInputgetInput (size_t index) const
 
const ShaderOutputgetOutput (size_t index=0) const
 
ShaderInputgetInput (const string &name)
 Get inputs/outputs by name.
 
ShaderOutputgetOutput (const string &name)
 
const ShaderInputgetInput (const string &name) const
 
const ShaderOutputgetOutput (const string &name) const
 
const vector< ShaderInput * > & getInputs () const
 Get vector of inputs/outputs.
 
const vector< ShaderOutput * > & getOutputs () const
 
void setMetadata (ShaderMetadataVecPtr metadata)
 Set the metadata vector.
 
ShaderMetadataVecPtr getMetadata ()
 Get the metadata vector.
 
const ShaderMetadataVecPtr & getMetadata () const
 Get the metadata vector.
 
bool isEditable (const ShaderInput &input) const
 Returns true if an input is editable by users. More...
 
bool isEditable (const ShaderGraphInputSocket &input) const
 Returns true if a graph input is accessible by users. More...
 

Static Public Member Functions

static ShaderGraphPtr create (const ShaderGraph *parent, const string &name, ElementPtr element, GenContext &context)
 Create a new shader graph from an element. More...
 
static ShaderGraphPtr create (const ShaderGraph *parent, const NodeGraph &nodeGraph, GenContext &context)
 Create a new shader graph from a nodegraph.
 
static ShaderGraphEdgeIterator traverseUpstream (ShaderOutput *output)
 Return an iterator for traversal upstream from the given output.
 
- Static Public Member Functions inherited from ShaderNode
static ShaderNodePtr create (const ShaderGraph *parent, const string &name, const NodeDef &nodeDef, GenContext &context)
 Create a new node from a nodedef.
 
static ShaderNodePtr create (const ShaderGraph *parent, const string &name, ShaderNodeImplPtr impl, unsigned int classification=Classification::TEXTURE)
 Create a new node from a node implementation.
 

Protected Member Functions

void createConnectedNodes (const ElementPtr &downstreamElement, const ElementPtr &upstreamElement, ElementPtr connectingElement, GenContext &context)
 Create node connections corresponding to the connection between a pair of elements. More...
 
void addNode (ShaderNodePtr node)
 Add a node to the graph.
 
void addInputSockets (const InterfaceElement &elem, GenContext &context)
 Add input sockets from an interface element (nodedef, nodegraph or node)
 
void addOutputSockets (const InterfaceElement &elem)
 Add output sockets from an interface element (nodedef, nodegraph or node)
 
void addUpstreamDependencies (const Element &root, GenContext &context)
 Traverse from the given root element and add all dependencies upstream. More...
 
void addColorTransformNode (ShaderInput *input, const ColorSpaceTransform &transform, GenContext &context)
 Add a color transform node and connect to the given input.
 
void addColorTransformNode (ShaderOutput *output, const ColorSpaceTransform &transform, GenContext &context)
 Add a color transform node and connect to the given output.
 
void addUnitTransformNode (ShaderInput *input, const UnitTransform &transform, GenContext &context)
 Add a unit transform node and connect to the given input.
 
void addUnitTransformNode (ShaderOutput *output, const UnitTransform &transform, GenContext &context)
 Add a unit transform node and connect to the given output.
 
void finalize (GenContext &context)
 Perform all post-build operations on the graph.
 
void optimize (GenContext &context)
 Optimize the graph, removing redundant paths.
 
void bypass (GenContext &context, ShaderNode *node, size_t inputIndex, size_t outputIndex=0)
 Bypass a node for a particular input and output, effectively connecting the input's upstream connection with the output's downstream connections.
 
void setVariableNames (GenContext &context)
 For inputs and outputs in the graph set the variable names to be used in generated code. More...
 
void populateColorTransformMap (ColorManagementSystemPtr colorManagementSystem, ShaderPort *shaderPort, const string &sourceColorSpace, const string &targetColorSpace, bool asInput)
 Populate the color transform map for the given shader port, if the provided combination of source and target color spaces are supported for its data type.
 
void populateUnitTransformMap (UnitSystemPtr unitSystem, ShaderPort *shaderPort, ValueElementPtr element, const string &targetUnitSpace, bool asInput)
 Populates the appropriate unit transform map if the provided input/parameter or output has a unit attribute and is of the supported type.
 
void disconnect (ShaderNode *node) const
 Break all connections on a node.
 
- Protected Member Functions inherited from ShaderNode
void createMetadata (const NodeDef &nodeDef, GenContext &context)
 Create metadata from the nodedef according to registered metadata.
 

Protected Attributes

ConstDocumentPtr _document
 
std::unordered_map< string, ShaderNodePtr_nodeMap
 
std::vector< ShaderNode * > _nodeOrder
 
IdentifierMap _identifiers
 
std::unordered_map< ShaderInput *, ColorSpaceTransform_inputColorTransformMap
 
std::unordered_map< ShaderInput *, UnitTransform_inputUnitTransformMap
 
std::unordered_map< ShaderOutput *, ColorSpaceTransform_outputColorTransformMap
 
std::unordered_map< ShaderOutput *, UnitTransform_outputUnitTransformMap
 
- Protected Attributes inherited from ShaderNode
const ShaderGraph_parent
 
string _name
 
uint32_t _classification
 
std::unordered_map< string, ShaderInputPtr_inputMap
 
vector< ShaderInput * > _inputOrder
 
std::unordered_map< string, ShaderOutputPtr_outputMap
 
vector< ShaderOutput * > _outputOrder
 
ShaderNodeImplPtr _impl
 
ShaderMetadataVecPtr _metadata
 

Additional Inherited Members

- Static Public Attributes inherited from ShaderNode
static const ShaderNodePtr NONE = createEmptyNode()
 
static const string CONSTANT = "constant"
 
static const string DOT = "dot"
 
static const string IMAGE = "image"
 
static const string SURFACESHADER = "surfaceshader"
 
static const string SCATTER_MODE = "scatter_mode"
 
static const string BSDF_R = "R"
 
static const string BSDF_T = "T"
 
static const string TRANSFORM_POINT
 
static const string TRANSFORM_VECTOR
 
static const string TRANSFORM_NORMAL
 
static const string TEXTURE2D_GROUPNAME = "texture2d"
 
static const string TEXTURE3D_GROUPNAME = "texture3d"
 
static const string PROCEDURAL2D_GROUPNAME = "procedural2d"
 
static const string PROCEDURAL3D_GROUPNAME = "procedural3d"
 
static const string GEOMETRIC_GROUPNAME = "geometric"
 

Detailed Description

Class representing a graph (DAG) for shader generation.

Member Function Documentation

◆ addUpstreamDependencies()

void addUpstreamDependencies ( const Element root,
GenContext context 
)
protected

Traverse from the given root element and add all dependencies upstream.

The traversal is done in the context of a material, if given, to include bind input elements in the traversal.

◆ create()

ShaderGraphPtr create ( const ShaderGraph parent,
const string &  name,
ElementPtr  element,
GenContext context 
)
static

Create a new shader graph from an element.

Supported elements are outputs and shader nodes.

◆ createConnectedNodes()

void createConnectedNodes ( const ElementPtr downstreamElement,
const ElementPtr upstreamElement,
ElementPtr  connectingElement,
GenContext context 
)
protected

Create node connections corresponding to the connection between a pair of elements.

Parameters
downstreamElementElement representing the node to connect to.
upstreamElementElement representing the node to connect from
connectingElementIf non-null, specifies the element on on the downstream node to connect to.
contextContext for generation.

◆ isAGraph()

bool isAGraph ( ) const
inlineoverridevirtual

Return true if this node is a graph.

Reimplemented from ShaderNode.

◆ setVariableNames()

void setVariableNames ( GenContext context)
protected

For inputs and outputs in the graph set the variable names to be used in generated code.

Making sure variable names are valid and unique to avoid name conflicts during shader generation.


The documentation for this class was generated from the following files: