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 /// Gaussian kernel weights for different kernel sizes.
23 /// Shared between the ConvolutionNode implementation and MaterialXRender::Image
24 extern MX_GENSHADER_API const std::array<float, 3> GAUSSIAN_KERNEL_3;
25 extern MX_GENSHADER_API const std::array<float, 5> GAUSSIAN_KERNEL_5;
26 extern MX_GENSHADER_API const std::array<float, 7> GAUSSIAN_KERNEL_7;
27 
28 /// Returns true if the given element is a surface shader with the potential
29 /// of being transparent. This can be used by HW shader generators to determine
30 /// if a shader will require transparency handling.
31 ///
32 /// Note: This function will check some common cases for how a surface
33 /// shader can be transparent. It is not covering all possible cases for
34 /// how transparency can be done and target applications might need to do
35 /// additional checks to track transparency correctly. For example, custom
36 /// surface shader nodes implemented in source code will not be tracked by this
37 /// function and transparency for such nodes must be tracked separately by the
38 /// target application.
39 ///
41 
42 /// Maps a value to a four channel color if it is of the appropriate type.
43 /// Supported types include float, Vector2, Vector3, Vector4,
44 /// and Color4. If not mapping is possible the color value is
45 /// set to opaque black.
47 
48 /// Return whether a nodedef requires an implementation
50 
51 /// Determine if a given element requires shading / lighting for rendering
53 
54 /// Find all renderable material nodes in the given document.
55 /// @param doc Document to examine
56 /// @return A vector of renderable material nodes.
58 
59 /// Find all renderable elements in the given document, including material nodes if present,
60 /// or graph outputs of renderable types if no material nodes are found.
61 /// @param doc Document to examine
62 /// @return A vector of renderable elements
63 MX_GENSHADER_API vector<TypedElementPtr> findRenderableElements(ConstDocumentPtr doc);
64 
65 /// Given a node input, return the corresponding input within its matching nodedef.
66 /// The optional target string can be used to guide the selection of nodedef declarations.
67 MX_GENSHADER_API InputPtr getNodeDefInput(InputPtr nodeInput, const string& target);
68 
69 /// Perform token substitutions on the given source string, using the given substitution map.
70 /// Tokens are required to start with '$' and can only consist of alphanumeric characters.
71 /// The full token name, including '$' and all following alphanumeric character, will be replaced
72 /// by the corresponding string in the substitution map, if the token exists in the map.
73 MX_GENSHADER_API void tokenSubstitution(const StringMap& substitutions, string& source);
74 
75 /// Compute the UDIM coordinates for a set of UDIM identifiers
76 /// @return List of UDIM coordinates
77 MX_GENSHADER_API vector<Vector2> getUdimCoordinates(const StringVec& udimIdentifiers);
78 
79 /// Get the UV scale and offset to transform uv coordinates from UDIM uv space to
80 /// 0..1 space.
81 MX_GENSHADER_API void getUdimScaleAndOffset(const vector<Vector2>& udimCoordinates, Vector2& scaleUV, Vector2& offsetUV);
82 
83 /// Determine whether the given output is directly connected to a node that
84 /// generates world-space coordinates (e.g. the "normalmap" node).
85 /// @param output Output to check
86 /// @return Return the node if found.
88 
89 /// Returns true if there is are any value elements with a given set of attributes either on the
90 /// starting node or any graph upsstream of that node.
91 /// @param output Starting node
92 /// @param attributes Attributes to test for
93 MX_GENSHADER_API bool hasElementAttributes(OutputPtr output, const StringVec& attributes);
94 
95 //
96 // These are deprecated wrappers for older versions of the function interfaces in this module.
97 // Clients using these interfaces should update them to the latest API.
98 //
99 [[deprecated]] MX_GENSHADER_API void findRenderableMaterialNodes(ConstDocumentPtr doc, vector<TypedElementPtr>& elements, bool, std::unordered_set<ElementPtr>&);
100 [[deprecated]] MX_GENSHADER_API void findRenderableElements(ConstDocumentPtr doc, vector<TypedElementPtr>& elements, bool includeReferencedGraphs = false);
101 
103 
104 #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:61
GLsizei const GLfloat * value
Definition: glcorearb.h:824
shared_ptr< const Value > ConstValuePtr
A shared pointer to a const Value.
Definition: Value.h:32
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)
MX_GENSHADER_API const std::array< float, 7 > GAUSSIAN_KERNEL_7
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:294
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 const std::array< float, 3 > GAUSSIAN_KERNEL_3
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:63
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.
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)
MX_GENSHADER_API const std::array< float, 5 > GAUSSIAN_KERNEL_5
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