HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
shaderNode.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_USD_SDR_SHADER_NODE_H
9 #define PXR_USD_SDR_SHADER_NODE_H
10 
11 /// \file sdr/shaderNode.h
12 ///
13 /// \note
14 /// All Ndr objects are deprecated in favor of the corresponding Sdr objects
15 /// in this file. All existing pxr/usd/ndr implementations will be moved to
16 /// pxr/usd/sdr.
17 
18 #include "pxr/pxr.h"
19 #include "pxr/usd/sdr/api.h"
21 #include "pxr/base/tf/token.h"
22 #include "pxr/usd/ndr/node.h"
23 #include "pxr/usd/sdr/declare.h"
25 
26 #include <unordered_map>
27 
29 
30 // Note: Metadata keys that are generated by parsers should start with
31 // "__SDR__" to reduce the risk of collision with metadata actually in the
32 // shader.
33 #define SDR_NODE_METADATA_TOKENS \
34  ((Category, "category")) \
35  ((Role, "role")) \
36  ((Departments, "departments")) \
37  ((Help, "help")) \
38  ((Label, "label")) \
39  ((Pages, "pages")) \
40  ((Primvars, "primvars")) \
41  ((ImplementationName, "__SDR__implementationName"))\
42  ((Target, "__SDR__target")) \
43  ((SdrUsdEncodingVersion, "sdrUsdEncodingVersion")) \
44  ((SdrDefinitionNameFallbackPrefix, "sdrDefinitionNameFallbackPrefix"))
45 
46 // Note: The concept of context is defined on NdrNode and can be queried with
47 // the GetContext() method. Sdr categorizes shaders by the context in which they
48 // are used inside of a renderer. For instance during 'pattern' evaluation to
49 // feed into a surface or volume shader. For BXDFs used in 'surface' and
50 // 'volume' rendering situations.
51 #define SDR_NODE_CONTEXT_TOKENS \
52  ((Pattern, "pattern")) \
53  ((Surface, "surface")) \
54  ((Volume, "volume")) \
55  ((Displacement, "displacement")) \
56  ((Light, "light")) \
57  ((DisplayFilter, "displayFilter")) \
58  ((LightFilter, "lightFilter")) \
59  ((PixelFilter, "pixelFilter")) \
60  ((SampleFilter, "sampleFilter"))
61 
62 #define SDR_NODE_ROLE_TOKENS \
63  ((Primvar, "primvar")) \
64  ((Texture, "texture")) \
65  ((Field, "field")) \
66  ((Math, "math")) \
67 
71 
72 /// \class SdrShaderNode
73 ///
74 /// Represents a node that holds shading information. Describes information
75 /// like the name of the node, what its inputs and outputs are, and any
76 /// associated metadata.
77 class SdrShaderNode : public NdrNode
78 {
79 public:
80  /// Constructor.
81  ///
82  /// \deprecated
83  /// Deprecated in favor of the constructor below this constructor,
84  /// which takes `SdrVersion` and `SdrShaderPropertyUniquePtrVec` instead
85  /// of Ndr analogues
86  SDR_API
87  SdrShaderNode(const NdrIdentifier& identifier,
88  const NdrVersion& version,
89  const std::string& name,
90  const TfToken& family,
91  const TfToken& context,
92  const TfToken& sourceType,
93  const std::string& definitionURI,
94  const std::string& implementationURI,
95  NdrPropertyUniquePtrVec&& properties,
96  const NdrTokenMap& metadata = NdrTokenMap(),
97  const std::string &sourceCode = std::string());
98 
99  /// Constructor
100  SDR_API
102  const SdrIdentifier& identifier,
103  const SdrVersion& version,
104  const std::string& name,
105  const TfToken& family,
106  const TfToken& context,
107  const TfToken& sourceType,
108  const std::string& definitionURI,
109  const std::string& implementationURI,
110  SdrShaderPropertyUniquePtrVec&& properties,
111  const SdrTokenMap& metadata = SdrTokenMap(),
112  const std::string &sourceCode = std::string());
113 
114  /// Return the version of the node
116 
117  /// \name Inputs and Outputs
118  /// An input or output is also generically referred to as a "property".
119  /// @{
120 
121  /// Get an ordered list of all the input names on this shader node.
122  SDR_API
123  const SdrTokenVec& GetShaderInputNames() const;
124 
125  /// Get an ordered list of all the output names on this shader node.
126  SDR_API
127  const SdrTokenVec& GetShaderOutputNames() const;
128 
129  /// Get a shader input property by name. `nullptr` is returned if an input
130  /// with the given name does not exist.
131  SDR_API
132  SdrShaderPropertyConstPtr GetShaderInput(const TfToken& inputName) const;
133 
134  /// Get a shader output property by name. `nullptr` is returned if an output
135  /// with the given name does not exist.
136  SDR_API
137  SdrShaderPropertyConstPtr GetShaderOutput(const TfToken& outputName) const;
138 
139  /// Returns the list of all inputs that are tagged as asset identifier
140  /// inputs.
141  SDR_API
143 
144  /// Returns the first shader input that is tagged as the default input.
145  /// A default input and its value can be used to acquire a fallback value
146  /// for a node when the node is considered 'disabled' or otherwise
147  /// incapable of producing an output value.
148  SDR_API
150 
151  /// @}
152 
153 
154  /// \name Metadata
155  /// The metadata returned here is a direct result of what the parser plugin
156  /// is able to determine about the node. See the documentation for a
157  /// specific parser plugin to get help on what the parser is looking for to
158  /// populate these values.
159  /// @{
160 
161  /// The label assigned to this node, if any. Distinct from the name
162  /// returned from `GetName()`. In the context of a UI, the label value
163  /// might be used as the display name for the node instead of the name.
164  SDR_API
165  const TfToken& GetLabel() const { return _label; }
166 
167  /// The category assigned to this node, if any. Distinct from the family
168  /// returned from `GetFamily()`.
169  SDR_API
170  const TfToken& GetCategory() const { return _category; }
171 
172  /// Returns the role of this node. This is used to annotate the role that
173  /// the shader node plays inside a shader network. We can tag certain
174  /// shaders to indicate their role within a shading network. We currently
175  /// tag primvar reading nodes, texture reading nodes and nodes that access
176  /// volume fields (like extinction or scattering). This is done to identify
177  /// resources used by a shading network.
178  SDR_API
179  std::string GetRole() const;
180 
181  /// The help message assigned to this node, if any.
182  SDR_API
183  std::string GetHelp() const;
184 
185  /// The departments this node is associated with, if any.
186  SDR_API
187  const SdrTokenVec& GetDepartments() const { return _departments; }
188 
189  /// Gets the pages on which the node's properties reside (an aggregate of
190  /// the unique `SdrShaderProperty::GetPage()` values for all of the node's
191  /// properties). Nodes themselves do not reside on pages. In an example
192  /// scenario, properties might be divided into two pages, 'Simple' and
193  /// 'Advanced'.
194  SDR_API
195  const SdrTokenVec& GetPages() const { return _pages; };
196 
197  /// The list of primvars this node knows it requires / uses.
198  /// For example, a shader node may require the 'normals' primvar to function
199  /// correctly. Additional, user specified primvars may have been authored on
200  /// the node. These can be queried via `GetAdditionalPrimvarProperties()`.
201  /// Together, `GetPrimvars()` and `GetAdditionalPrimvarProperties()`,
202  /// provide the complete list of primvar requirements for the node.
203  SDR_API
204  const SdrTokenVec& GetPrimvars() const { return _primvars; }
205 
206  /// The list of string input properties whose values provide the names of
207  /// additional primvars consumed by this node. For example, this may return
208  /// a token named `varname`. This indicates that the client should query the
209  /// value of a (presumed to be string-valued) input attribute named varname
210  /// from its scene description to determine the name of a primvar the
211  /// node will consume. See `GetPrimvars()` for additional information.
212  SDR_API
215  }
216 
217  /// Returns the implementation name of this node. The name of the node
218  /// is how to refer to the node in shader networks. The label is how to
219  /// present this node to users. The implementation name is the name of
220  /// the function (or something) this node represents in the
221  /// implementation. Any client using the implementation \b must call
222  /// this method to get the correct name; using \c getName() is not
223  /// correct.
224  SDR_API
225  std::string GetImplementationName() const;
226 
227  /// @}
228 
229 
230  /// \name Aggregate Information
231  /// @{
232 
233  /// Gets the names of the properties on a certain page (one that was
234  /// returned by `GetPages()`). To get properties that are not assigned to a
235  /// page, an empty string can be used for \p pageName.
236  SDR_API
237  SdrTokenVec GetPropertyNamesForPage(const std::string& pageName) const;
238 
239  /// Gets all vstructs that are present in the shader.
240  SDR_API
242 
243  /// @}
244 
245  // Stores the result of the compliance check of property names to
246  // sdrShaderNodeIdentifiers
247  using ComplianceResults = std::unordered_map<TfToken,
248  std::vector<SdrIdentifier>,
250 
251  /// This method checks if same named properties of \p shaderNodes are
252  /// compatible with each other.
253  ///
254  /// Checks if the same name properties have matching types and default
255  /// values. In order to determine if same name properties from different
256  /// shader nodes are compliant, we assume that the first shaderNode in the
257  /// list providing this property is authoritative, and other nodes differing
258  /// wrt this property are non-compliant. A map of property names and their
259  /// respective shaderNodes are stored in the map. An empty map returned
260  /// represents no compliance issues.
261  SDR_API
262  static
264  const std::vector<SdrShaderNodeConstPtr> &shaderNodes);
265 
266  /// \cond
267  /// Hide from the API.
268 
269  // Performs a post-process on properties to determine information that can
270  // only be determined after parsing or in aggregate. Clients SHOULD NOT
271  // need to call this.
272  void _PostProcessProperties();
273 
274  /// \endcond
275 
276 protected:
277  // Processed primvar metadata. `_primvars` contains the names of primvars
278  // consumed by this node, whereas `_primvarNamingProperties` contains the
279  // names of string input properties whose values provide the names of
280  // additional primvars consumed by this node.
283 
284  // Tokenized metadata
289 
292 
293 private:
294  // Initializes `_primvars` and `_primvarNamingProperties`
295  void _InitializePrimvars();
296 
297  // Determines which pages are present on the node's properties
298  SdrTokenVec _ComputePages() const;
299 };
300 
302 
303 #endif // PXR_USD_SDR_SHADER_NODE_H
SdrPropertyMap _shaderInputs
Definition: shaderNode.h:290
SdrPropertyMap _shaderOutputs
Definition: shaderNode.h:291
SDR_API const SdrTokenVec & GetPages() const
Definition: shaderNode.h:195
SDR_API const SdrTokenVec & GetPrimvars() const
Definition: shaderNode.h:204
SDR_API const TfToken & GetCategory() const
Definition: shaderNode.h:170
SDR_API const TfToken & GetLabel() const
Definition: shaderNode.h:165
SdrTokenVec _pages
Definition: shaderNode.h:288
SdrVersion GetShaderVersion() const
Return the version of the node.
Definition: shaderNode.h:115
TfToken _category
Definition: shaderNode.h:286
SdrTokenVec _departments
Definition: shaderNode.h:287
SDR_API SdrShaderPropertyConstPtr GetShaderInput(const TfToken &inputName) const
SdrTokenVec _primvarNamingProperties
Definition: shaderNode.h:282
Functor to use for hash maps from tokens to other things.
Definition: token.h:149
SDR_API const SdrTokenVec & GetAdditionalPrimvarProperties() const
Definition: shaderNode.h:213
SDR_API const SdrTokenVec & GetDepartments() const
The departments this node is associated with, if any.
Definition: shaderNode.h:187
TfToken _label
Definition: shaderNode.h:285
std::vector< NdrPropertyUniquePtr > NdrPropertyUniquePtrVec
Definition: declare.h:58
SdrTokenVec _primvars
Definition: shaderNode.h:281
SDR_API std::string GetHelp() const
The help message assigned to this node, if any.
SdrVersion.
Definition: declare.h:73
SDR_API std::string GetImplementationName() const
Definition: token.h:70
SDR_API SdrShaderPropertyConstPtr GetDefaultInput() const
Definition: node.h:37
SdrShaderPropertyMap SdrPropertyMap
Definition: declare.h:64
SDR_API const SdrTokenVec & GetShaderOutputNames() const
Get an ordered list of all the output names on this shader node.
std::unordered_map< TfToken, std::vector< SdrIdentifier >, TfToken::HashFunctor > ComplianceResults
Definition: shaderNode.h:249
GLuint const GLchar * name
Definition: glcorearb.h:786
SdrShaderProperty const * SdrShaderPropertyConstPtr
Definition: declare.h:59
GT_API const UT_StringHolder version
SDR_API SdrTokenVec GetPropertyNamesForPage(const std::string &pageName) const
SDR_API const SdrTokenVec & GetShaderInputNames() const
Get an ordered list of all the input names on this shader node.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
static SDR_API ComplianceResults CheckPropertyCompliance(const std::vector< SdrShaderNodeConstPtr > &shaderNodes)
SDR_API SdrTokenVec GetAssetIdentifierInputNames() const
#define SDR_NODE_ROLE_TOKENS
Definition: shaderNode.h:62
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
SDR_API SdrShaderPropertyConstPtr GetShaderOutput(const TfToken &outputName) const
SDR_API SdrTokenVec GetAllVstructNames() const
Gets all vstructs that are present in the shader.
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > NdrTokenMap
Definition: declare.h:52
std::vector< TfToken > SdrTokenVec
Definition: declare.h:45
NdrVersion _version
Definition: node.h:208
#define SDR_NODE_CONTEXT_TOKENS
Definition: shaderNode.h:51
#define SDR_API
Definition: api.h:23
#define SDR_NODE_METADATA_TOKENS
Definition: shaderNode.h:33
SDR_API SdrShaderNode(const NdrIdentifier &identifier, const NdrVersion &version, const std::string &name, const TfToken &family, const TfToken &context, const TfToken &sourceType, const std::string &definitionURI, const std::string &implementationURI, NdrPropertyUniquePtrVec &&properties, const NdrTokenMap &metadata=NdrTokenMap(), const std::string &sourceCode=std::string())
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > SdrTokenMap
Definition: declare.h:47
std::vector< SdrShaderPropertyUniquePtr > SdrShaderPropertyUniquePtrVec
Definition: declare.h:61
SDR_API SdrVersion NdrToSdrVersion(NdrVersion version)
TF_DECLARE_PUBLIC_TOKENS(SdrNodeMetadata, SDR_API, SDR_NODE_METADATA_TOKENS)
SDR_API std::string GetRole() const