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 // TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
3 // All rights reserved. See LICENSE.txt for license.
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 beeing 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 any material node elements which are renderable (have input shaders)
49 /// @param doc Document to examine
50 /// @param elements List of renderable elements (returned)
51 /// @param includeReferencedGraphs Whether to check for outputs on referenced graphs
52 /// @param processedSources List of elements examined.
54  vector<TypedElementPtr>& elements,
55  bool includeReferencedGraphs,
56  std::unordered_set<ElementPtr>& processedSources);
57 
58 /// Find any elements which may be renderable from within a document.
59 /// This includes all outputs on node graphs and shader references which are not
60 /// part of any included library. Light shaders are not considered to be renderable.
61 /// The option to include node graphs referened by shader references is disabled by default.
62 MX_GENSHADER_API void findRenderableElements(ConstDocumentPtr doc, vector<TypedElementPtr>& elements,
63  bool includeReferencedGraphs = false);
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 substituation 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 
96 
97 #endif
shared_ptr< Output > OutputPtr
A shared pointer to an Output.
Definition: Interface.h:36
#define MATERIALX_NAMESPACE_BEGIN
Definition: Generated.h:23
vector< string > StringVec
A vector of strings.
Definition: Library.h:57
GLuint color
Definition: glcorearb.h:1261
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
GLenum target
Definition: glcorearb.h:1667
shared_ptr< const TypedElement > ConstTypedElementPtr
A shared pointer to a const TypedElement.
Definition: Element.h:37
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
Definition: Types.h:282
MX_GENSHADER_API void mapValueToColor(ConstValuePtr value, Color4 &color)
MX_GENSHADER_API void findRenderableMaterialNodes(ConstDocumentPtr doc, vector< TypedElementPtr > &elements, bool includeReferencedGraphs, std::unordered_set< ElementPtr > &processedSources)
MX_GENSHADER_API void findRenderableElements(ConstDocumentPtr doc, vector< TypedElementPtr > &elements, bool includeReferencedGraphs=false)
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)
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:30
#define MATERIALX_NAMESPACE_END
Definition: Generated.h:24
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