MaterialX 1.39.1
Loading...
Searching...
No Matches
ShaderGraph.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_SHADERGRAPH_H
7#define MATERIALX_SHADERGRAPH_H
8
11
13
19
21#include <MaterialXCore/Node.h>
22
23MATERIALX_NAMESPACE_BEGIN
24
25class Syntax;
26class ShaderGraphEdge;
28class GenOptions;
29
33
37
39using ShaderGraphPtr = shared_ptr<class ShaderGraph>;
40
43class MX_GENSHADER_API ShaderGraph : public ShaderNode
44{
45 public:
47 ShaderGraph(const ShaderGraph* parent, const string& name, ConstDocumentPtr document, const StringSet& reservedWords);
48
50 virtual ~ShaderGraph() { }
51
54 static ShaderGraphPtr create(const ShaderGraph* parent, const string& name, ElementPtr element,
55 GenContext& context);
56
58 static ShaderGraphPtr create(const ShaderGraph* parent, const NodeGraph& nodeGraph,
59 GenContext& context);
60
62 bool isAGraph() const override { return true; }
63
65 ShaderNode* getNode(const string& name);
66
68 const ShaderNode* getNode(const string& name) const;
69
71 const vector<ShaderNode*>& getNodes() const { return _nodeOrder; }
72
74 size_t numInputSockets() const { return numOutputs(); }
75
77 size_t numOutputSockets() const { return numInputs(); }
78
80 ShaderGraphInputSocket* getInputSocket(size_t index) { return getOutput(index); }
81 ShaderGraphOutputSocket* getOutputSocket(size_t index = 0) { return getInput(index); }
82 const ShaderGraphInputSocket* getInputSocket(size_t index) const { return getOutput(index); }
83 const ShaderGraphOutputSocket* getOutputSocket(size_t index = 0) const { return getInput(index); }
84
86 ShaderGraphInputSocket* getInputSocket(const string& name) { return getOutput(name); }
87 ShaderGraphOutputSocket* getOutputSocket(const string& name) { return getInput(name); }
88 const ShaderGraphInputSocket* getInputSocket(const string& name) const { return getOutput(name); }
89 const ShaderGraphOutputSocket* getOutputSocket(const string& name) const { return getInput(name); }
90
92 const vector<ShaderGraphInputSocket*>& getInputSockets() const { return _outputOrder; }
93 const vector<ShaderGraphOutputSocket*>& getOutputSockets() const { return _inputOrder; }
94
97
100
103 [[deprecated]] ShaderGraphInputSocket* addInputSocket(const string& name, const TypeDesc* type) { return addInputSocket(name, *type); }
104
107 [[deprecated]] ShaderGraphOutputSocket* addOutputSocket(const string& name, const TypeDesc* type) { return addOutputSocket(name, *type); }
108
110 void addDefaultGeomNode(ShaderInput* input, const GeomPropDef& geomprop, GenContext& context);
111
114
117
119 IdentifierMap& getIdentifierMap() { return _identifiers; }
120
121 protected:
127 void createConnectedNodes(const ElementPtr& downstreamElement,
128 const ElementPtr& upstreamElement,
129 ElementPtr connectingElement,
130 GenContext& context);
131
134
136 void addInputSockets(const InterfaceElement& elem, GenContext& context);
137
140
144 void addUpstreamDependencies(const Element& root, GenContext& context);
145
147 void addColorTransformNode(ShaderInput* input, const ColorSpaceTransform& transform, GenContext& context);
148
150 void addColorTransformNode(ShaderOutput* output, const ColorSpaceTransform& transform, GenContext& context);
151
153 void addUnitTransformNode(ShaderInput* input, const UnitTransform& transform, GenContext& context);
154
156 void addUnitTransformNode(ShaderOutput* output, const UnitTransform& transform, GenContext& context);
157
159 void finalize(GenContext& context);
160
162 void optimize();
163
167 void bypass(ShaderNode* node, size_t inputIndex, size_t outputIndex = 0);
168
173
176 void populateColorTransformMap(ColorManagementSystemPtr colorManagementSystem, ShaderPort* shaderPort,
177 const string& sourceColorSpace, const string& targetColorSpace, bool asInput);
178
181 void populateUnitTransformMap(UnitSystemPtr unitSystem, ShaderPort* shaderPort, ValueElementPtr element, const string& targetUnitSpace, bool asInput);
182
184 void disconnect(ShaderNode* node) const;
185
186 ConstDocumentPtr _document;
187 std::unordered_map<string, ShaderNodePtr> _nodeMap;
188 std::vector<ShaderNode*> _nodeOrder;
189 IdentifierMap _identifiers;
190
191 // Temporary storage for inputs that require color transformations
192 std::unordered_map<ShaderInput*, ColorSpaceTransform> _inputColorTransformMap;
193 // Temporary storage for inputs that require unit transformations
194 std::unordered_map<ShaderInput*, UnitTransform> _inputUnitTransformMap;
195
196 // Temporary storage for outputs that require color transformations
197 std::unordered_map<ShaderOutput*, ColorSpaceTransform> _outputColorTransformMap;
198 // Temporary storage for outputs that require unit transformations
199 std::unordered_map<ShaderOutput*, UnitTransform> _outputUnitTransformMap;
200};
201
204class MX_GENSHADER_API ShaderGraphEdge
205{
206 public:
208 upstream(up),
209 downstream(down)
210 {
211 }
212 ShaderOutput* upstream;
213 ShaderInput* downstream;
214};
215
218class MX_GENSHADER_API ShaderGraphEdgeIterator
219{
220 public:
223
224 bool operator==(const ShaderGraphEdgeIterator& rhs) const
225 {
226 return _upstream == rhs._upstream &&
227 _downstream == rhs._downstream &&
228 _stack == rhs._stack;
229 }
230 bool operator!=(const ShaderGraphEdgeIterator& rhs) const
231 {
232 return !(*this == rhs);
233 }
234
237 {
238 return ShaderGraphEdge(_upstream, _downstream);
239 }
240
244
247 {
248 return *this;
249 }
250
253
254 private:
255 void extendPathUpstream(ShaderOutput* upstream, ShaderInput* downstream);
256 void returnPathDownstream(ShaderOutput* upstream);
257
258 ShaderOutput* _upstream;
259 ShaderInput* _downstream;
260 using StackFrame = std::pair<ShaderOutput*, size_t>;
261 std::vector<StackFrame> _stack;
262 std::set<ShaderOutput*> _path;
263};
264
265MATERIALX_NAMESPACE_END
266
267#endif
Color management system classes.
shared_ptr< class ColorManagementSystem > ColorManagementSystemPtr
A shared pointer to a ColorManagementSystem.
Definition: ColorManagementSystem.h:25
The top-level Document class.
shared_ptr< const Document > ConstDocumentPtr
A shared pointer to a const Document.
Definition: Document.h:24
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
shared_ptr< ValueElement > ValueElementPtr
A shared pointer to a ValueElement.
Definition: Element.h:41
std::set< string > StringSet
A set of strings.
Definition: Library.h:59
Macros for declaring imported and exported symbols.
Node element subclasses.
shared_ptr< const Node > ConstNodePtr
A shared pointer to a const Node.
Definition: Node.h:26
shared_ptr< class ShaderGraph > ShaderGraphPtr
A shared pointer to a shader graph.
Definition: ShaderGraph.h:39
Classes for nodes created during shader generation.
shared_ptr< class ShaderNode > ShaderNodePtr
Shared pointer to a ShaderNode.
Definition: ShaderNode.h:35
Base class for syntax handling for shader generators.
std::unordered_map< string, size_t > IdentifierMap
Map holding identifier names and a counter for creating unique names from them.
Definition: Syntax.h:35
Type descriptor for a MaterialX data type.
Unit system classes.
shared_ptr< class UnitSystem > UnitSystemPtr
A shared pointer to a UnitSystem.
Definition: UnitSystem.h:26
The base class for MaterialX elements.
Definition: Element.h:80
A context class for shader generation.
Definition: GenContext.h:31
Class holding options to configure shader generation.
Definition: GenOptions.h:76
An element representing a declaration of geometric property data.
Definition: Geom.h:352
The base class for interface elements such as Node, NodeDef, and NodeGraph.
Definition: Interface.h:312
A node graph element within a Document.
Definition: Node.h:322
An edge returned during shader graph traversal.
Definition: ShaderGraph.h:205
Iterator class for traversing edges between nodes in a shader graph.
Definition: ShaderGraph.h:219
ShaderGraphEdgeIterator & operator++()
Iterate to the next edge in the traversal.
ShaderGraphEdge operator*() const
Dereference this iterator, returning the current output in the traversal.
Definition: ShaderGraph.h:236
static const ShaderGraphEdgeIterator & end()
Return the end iterator.
ShaderGraphEdgeIterator & begin()
Return a reference to this iterator to begin traversal.
Definition: ShaderGraph.h:246
Class representing a graph (DAG) for shader generation.
Definition: ShaderGraph.h:44
void addUnitTransformNode(ShaderOutput *output, const UnitTransform &transform, GenContext &context)
Add a unit transform node and connect to the given output.
virtual ~ShaderGraph()
Desctructor.
Definition: ShaderGraph.h:50
void addUnitTransformNode(ShaderInput *input, const UnitTransform &transform, GenContext &context)
Add a unit transform node and connect to the given input.
bool isAGraph() const override
Return true if this node is a graph.
Definition: ShaderGraph.h:62
ShaderGraphInputSocket * addInputSocket(const string &name, TypeDesc type)
Add input sockets.
void addOutputSockets(const InterfaceElement &elem)
Add output sockets from an interface element (nodedef, nodegraph or node)
const ShaderNode * getNode(const string &name) const
Get an internal node by name.
ShaderNode * getNode(const string &name)
Get an internal node by name.
void addInputSockets(const InterfaceElement &elem, GenContext &context)
Add input sockets from an interface element (nodedef, nodegraph or node)
size_t numInputSockets() const
Get number of input sockets.
Definition: ShaderGraph.h:74
void setVariableNames(GenContext &context)
For inputs and outputs in the graph set the variable names to be used in generated code.
static ShaderGraphEdgeIterator traverseUpstream(ShaderOutput *output)
Return an iterator for traversal upstream from the given output.
void addColorTransformNode(ShaderInput *input, const ColorSpaceTransform &transform, GenContext &context)
Add a color transform node and connect to the given input.
void topologicalSort()
Sort the nodes in topological order.
ShaderNode * createNode(ConstNodePtr node, GenContext &context)
Create a new node in the graph.
ShaderGraph(const ShaderGraph *parent, const string &name, ConstDocumentPtr document, const StringSet &reservedWords)
Constructor.
void disconnect(ShaderNode *node) const
Break all connections on a node.
const vector< ShaderGraphInputSocket * > & getInputSockets() const
Get vector of sockets.
Definition: ShaderGraph.h:92
void applyInputTransforms(ConstNodePtr node, ShaderNodePtr shaderNode, GenContext &context)
Apply color and unit transforms to each input of a node.
ShaderGraphInputSocket * getInputSocket(size_t index)
Get socket by index.
Definition: ShaderGraph.h:80
void bypass(ShaderNode *node, size_t inputIndex, size_t outputIndex=0)
Bypass a node for a particular input and output, effectively connecting the input's upstream connecti...
void addColorTransformNode(ShaderOutput *output, const ColorSpaceTransform &transform, GenContext &context)
Add a color transform node and connect to the given output.
IdentifierMap & getIdentifierMap()
Return the map of unique identifiers used in the scope of this graph.
Definition: ShaderGraph.h:119
static ShaderGraphPtr create(const ShaderGraph *parent, const string &name, ElementPtr element, GenContext &context)
Create a new shader graph from an element.
static ShaderGraphPtr create(const ShaderGraph *parent, const NodeGraph &nodeGraph, GenContext &context)
Create a new shader graph from a nodegraph.
size_t numOutputSockets() const
Get number of output sockets.
Definition: ShaderGraph.h:77
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...
void addDefaultGeomNode(ShaderInput *input, const GeomPropDef &geomprop, GenContext &context)
Add a default geometric node and connect to the given input.
void optimize()
Optimize the graph, removing redundant paths.
void addUpstreamDependencies(const Element &root, GenContext &context)
Traverse from the given root element and add all dependencies upstream.
void addNode(ShaderNodePtr node)
Add a node to the graph.
ShaderGraphOutputSocket * addOutputSocket(const string &name, TypeDesc type)
Add output sockets.
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 att...
void createConnectedNodes(const ElementPtr &downstreamElement, const ElementPtr &upstreamElement, ElementPtr connectingElement, GenContext &context)
Create node connections corresponding to the connection between a pair of elements.
const vector< ShaderNode * > & getNodes() const
Get a vector of all nodes in order.
Definition: ShaderGraph.h:71
ShaderGraphInputSocket * getInputSocket(const string &name)
Get socket by name.
Definition: ShaderGraph.h:86
void finalize(GenContext &context)
Perform all post-build operations on the graph.
An input on a ShaderNode.
Definition: ShaderNode.h:264
Class representing a node in the shader generation DAG.
Definition: ShaderNode.h:320
ShaderInput * getInput(size_t index)
Get inputs/outputs by index.
Definition: ShaderNode.h:447
size_t numInputs() const
Get number of inputs/outputs.
Definition: ShaderNode.h:443
An output on a ShaderNode.
Definition: ShaderNode.h:295
An input or output port on a ShaderNode.
Definition: ShaderNode.h:123
Base class for syntax objects used by shader generators to emit code with correct syntax for each lan...
Definition: Syntax.h:41
A type descriptor for MaterialX data types.
Definition: TypeDesc.h:35
Structure that represents color space transform information.
Definition: ColorManagementSystem.h:30
Structure that represents unit transform information.
Definition: UnitSystem.h:31