HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Util.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_SHADERGEN_UTIL_H
7 #define MATERIALX_SHADERGEN_UTIL_H
8 
9 /// @file
10 /// Shader generation utility methods
11 
13 
14 #include <MaterialXCore/Document.h>
15 
16 #include <unordered_set>
17 
19 
20 class ShaderGenerator;
21 
22 /// Returns true if the given element is a surface shader with the potential
23 /// of being transparent. This can be used by HW shader generators to determine
24 /// if a shader will require transparency handling.
25 ///
26 /// Note: This function will check some common cases for how a surface
27 /// shader can be transparent. It is not covering all possible cases for
28 /// how transparency can be done and target applications might need to do
29 /// additional checks to track transparency correctly. For example, custom
30 /// surface shader nodes implemented in source code will not be tracked by this
31 /// function and transprency for such nodes must be tracked separately by the
32 /// target application.
33 ///
35 
36 /// Maps a value to a four channel color if it is of the appropriate type.
37 /// Supported types include float, Vector2, Vector3, Vector4,
38 /// and Color4. If not mapping is possible the color value is
39 /// set to opaque black.
41 
42 /// Return whether a nodedef requires an implementation
44 
45 /// Determine if a given element requires shading / lighting for rendering
47 
48 /// Find all renderable material nodes in the given document.
49 /// @param doc Document to examine
50 /// @return A vector of renderable material nodes.
52 
53 /// Find all renderable elements in the given document, including material nodes if present,
54 /// or graph outputs of renderable types if no material nodes are found.
55 /// @param doc Document to examine
56 /// @return A vector of renderable elements
57 MX_GENSHADER_API vector<TypedElementPtr> findRenderableElements(ConstDocumentPtr doc);
58 
59 /// Given a node input, return the corresponding input within its matching nodedef.
60 /// The optional target string can be used to guide the selection of nodedef declarations.
61 MX_GENSHADER_API InputPtr getNodeDefInput(InputPtr nodeInput, const string& target);
62 
63 /// Perform token substitutions on the given source string, using the given substituation map.
64 /// Tokens are required to start with '$' and can only consist of alphanumeric characters.
65 /// The full token name, including '$' and all following alphanumeric character, will be replaced
66 /// by the corresponding string in the substitution map, if the token exists in the map.
67 MX_GENSHADER_API void tokenSubstitution(const StringMap& substitutions, string& source);
68 
69 /// Compute the UDIM coordinates for a set of UDIM identifiers
70 /// @return List of UDIM coordinates
71 MX_GENSHADER_API vector<Vector2> getUdimCoordinates(const StringVec& udimIdentifiers);
72 
73 /// Get the UV scale and offset to transform uv coordinates from UDIM uv space to
74 /// 0..1 space.
75 MX_GENSHADER_API void getUdimScaleAndOffset(const vector<Vector2>& udimCoordinates, Vector2& scaleUV, Vector2& offsetUV);
76 
77 /// Determine whether the given output is directly connected to a node that
78 /// generates world-space coordinates (e.g. the "normalmap" node).
79 /// @param output Output to check
80 /// @return Return the node if found.
82 
83 /// Returns true if there is are any value elements with a given set of attributes either on the
84 /// starting node or any graph upsstream of that node.
85 /// @param output Starting node
86 /// @param attributes Attributes to test for
87 MX_GENSHADER_API bool hasElementAttributes(OutputPtr output, const StringVec& attributes);
88 
89 //
90 // These are deprecated wrappers for older versions of the function interfaces in this module.
91 // Clients using these interfaces should update them to the latest API.
92 //
93 MX_GENSHADER_API [[deprecated]] void findRenderableMaterialNodes(ConstDocumentPtr doc, vector<TypedElementPtr>& elements, bool, std::unordered_set<ElementPtr>&);
94 MX_GENSHADER_API [[deprecated]] void findRenderableElements(ConstDocumentPtr doc, vector<TypedElementPtr>& elements, bool includeReferencedGraphs = false);
95 
97 
98 #endif
MX_GENSHADER_API vector< TypedElementPtr > findRenderableMaterialNodes(ConstDocumentPtr doc)
shared_ptr< Output > OutputPtr
A shared pointer to an Output.
Definition: Interface.h:36
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:25
vector< string > StringVec
A vector of strings.
Definition: Library.h:57
shared_ptr< const Value > ConstValuePtr
A shared pointer to a const Value.
Definition: Value.h:31
MX_GENSHADER_API InputPtr getNodeDefInput(InputPtr nodeInput, const string &target)
MATERIALX_NAMESPACE_BEGIN MX_CORE_API const string EMPTY_STRING
#define MX_GENSHADER_API
Definition: Export.h:18
MX_GENSHADER_API vector< TypedElementPtr > findRenderableElements(ConstDocumentPtr doc)
shared_ptr< const TypedElement > ConstTypedElementPtr
A shared pointer to a const TypedElement.
Definition: Element.h:38
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
GLenum target
Definition: glcorearb.h:1667
Definition: Types.h:285
MX_GENSHADER_API void mapValueToColor(ConstValuePtr value, Color4 &color)
shared_ptr< Input > InputPtr
A shared pointer to an Input.
Definition: Interface.h:31
MX_GENSHADER_API bool elementRequiresShading(ConstTypedElementPtr element)
Determine if a given element requires shading / lighting for rendering.
MX_GENSHADER_API bool hasElementAttributes(OutputPtr output, const StringVec &attributes)
MX_GENSHADER_API NodePtr connectsToWorldSpaceNode(OutputPtr output)
GLuint color
Definition: glcorearb.h:1261
std::unordered_map< string, string > StringMap
An unordered map with strings as both keys and values.
Definition: Library.h:59
MX_GENSHADER_API bool isTransparentSurface(ElementPtr element, const string &target=EMPTY_STRING)
MX_GENSHADER_API bool requiresImplementation(ConstNodeDefPtr nodeDef)
Return whether a nodedef requires an implementation.
Definition: core.h:1131
shared_ptr< Element > ElementPtr
A shared pointer to an Element.
Definition: Element.h:31
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:26
shared_ptr< const NodeDef > ConstNodeDefPtr
A shared pointer to a const NodeDef.
Definition: Definition.h:34
MX_GENSHADER_API vector< Vector2 > getUdimCoordinates(const StringVec &udimIdentifiers)
shared_ptr< const Document > ConstDocumentPtr
A shared pointer to a const Document.
Definition: Document.h:24
MX_GENSHADER_API void tokenSubstitution(const StringMap &substitutions, string &source)
MX_GENSHADER_API void getUdimScaleAndOffset(const vector< Vector2 > &udimCoordinates, Vector2 &scaleUV, Vector2 &offsetUV)
shared_ptr< Node > NodePtr
A shared pointer to a Node.
Definition: Node.h:24