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 #include "pxr/pxr.h"
14 #include "pxr/usd/sdr/api.h"
16 #include "pxr/base/tf/token.h"
17 #include "pxr/base/vt/value.h"
18 #include "pxr/usd/sdr/declare.h"
21 
22 #include <unordered_map>
23 
25 
26 #define SDR_NODE_FIELD_KEY_TOKENS \
27  ((Identifier, "_identifier")) \
28  ((Name, "_name")) \
29  ((Family, "_family")) /* deprecated */ \
30  ((Function, "_function")) \
31  ((SourceType, "_sourceType")) /* deprecated */ \
32  ((ShadingSystem, "_shadingSystem"))
33 
34 /// \deprecated
35 /// SdrNodeFieldKey->SourceType is deprecated in favor of
36 /// SdrNodeFieldKey->ShadingSystem.
38 
39 /// \class SdrShaderNode
40 ///
41 /// Represents a node that holds shading information. Describes information
42 /// like the name of the node, what its inputs and outputs are, and any
43 /// associated metadata.
45 {
46 public:
47 
48  /// \name Constructors
49  ///
50  /// Constructors for SdrShaderNode
51  ///
52  /// \deprecated
53  /// The versions of the SdrShaderNode constructors that
54  /// take `SdrTokenMap` as metadata are deprecated in favor of
55  /// the constructor taking an explicit SdrShaderNodeMetadata. Note that
56  /// SdrShaderNodeMetadata has an implicit constructor from
57  /// the legacy `SdrTokenMap` metadata.
58  /// \sa SdrShaderNodeMetadata::SdrShaderNodeMetadata(const SdrTokenMap&)
59  /// {@
60 
61  /// Constructor
62  SDR_API
64  const SdrIdentifier& identifier,
65  const SdrVersion& version,
66  const std::string& name,
67  const TfToken& function,
68  const TfToken& shadingSystem,
69  const std::string& definitionURI,
70  const std::string& implementationURI,
71  SdrShaderPropertyUniquePtrVec&& properties,
72  const SdrShaderNodeMetadata& metadata = SdrShaderNodeMetadata(),
73  const std::string &sourceCode = std::string()
74  ): SdrShaderNode(
75  identifier,
76  version,
77  name,
78  function,
79  metadata.GetContext(),
80  shadingSystem,
81  definitionURI,
82  implementationURI,
83  std::move(properties),
84  metadata,
85  sourceCode
86  ) {}
87 
88  /// \deprecated
89  /// Deprecated in favor of the above constructor that expects the "context"
90  /// concept to be specified in metadata instead of as a direct constructor
91  /// argument.
92  SDR_API
94  const SdrIdentifier& identifier,
95  const SdrVersion& version,
96  const std::string& name,
97  const TfToken& function,
98  const TfToken& context, // deprecated
99  const TfToken& shadingSystem,
100  const std::string& definitionURI,
101  const std::string& implementationURI,
102  SdrShaderPropertyUniquePtrVec&& properties,
103  const SdrShaderNodeMetadata& metadata = SdrShaderNodeMetadata(),
104  const std::string &sourceCode = std::string());
105  /// @}
106 
107  /// Destructor.
108  SDR_API
109  virtual ~SdrShaderNode();
110 
111  /// \name The Basics
112  /// @{
113 
114  /// Return the identifier of the node.
115  const SdrIdentifier& GetIdentifier() const { return _identifier; }
116 
117  /// Return the version of the node
119 
120  /// Gets the name of the node.
121  const std::string& GetName() const { return _name; }
122 
123  /// Gets the name of the family that the node belongs to. An empty token
124  /// will be returned if the node does not belong to a family.
125  ///
126  /// \deprecated in favor of SdrShaderNode::GetFunction
127  const TfToken& GetFamily() const { return _function; }
128 
129  /// Gets the function that the node belongs to.
130  ///
131  /// One example of a function would be "add" -- multiple
132  /// specializations on argument types and version may be registered
133  /// under the same function but on different nodes.
134  const TfToken& GetFunction() const { return _function; }
135 
136  /// Gets the shading system that this shader node originated from.
137  ///
138  /// "Shading system" describes a system that usually has its own
139  /// standard and shading language
140  ///
141  /// As a concrete example from the `Sdr` library, several shader parsers
142  /// exist and operate on shaders from different shading systems. In this
143  /// scenario, each distinct type of shader (OSL, Args, etc) is considered
144  /// a different _shading system_, even though they are all shaders.
145  const TfToken& GetShadingSystem() const { return _shadingSystem; }
146 
147  /// Gets the type of source that this shader node originated from.
148  ///
149  /// \deprecated
150  /// Deprecated in favor of `GetShadingSystem`
151  const TfToken& GetSourceType() const { return _shadingSystem; }
152 
153  /// Gets the URI to the resource that provided this node's
154  /// definition. Could be a path to a file, or some other resource
155  /// identifier. This URI should be fully resolved.
156  ///
157  /// \sa SdrShaderNode::GetResolvedImplementationURI()
158  const std::string& GetResolvedDefinitionURI() const { return _definitionURI; }
159 
160  /// Gets the URI to the resource that provides this node's
161  /// implementation. Could be a path to a file, or some other resource
162  /// identifier. This URI should be fully resolved.
163  ///
164  /// \sa SdrShaderNode::GetResolvedDefinitionURI()
165  const std::string& GetResolvedImplementationURI() const {
166  return _implementationURI;
167  }
168 
169  /// Returns the source code for this node. This will be empty for most
170  /// nodes. It will be non-empty only for the nodes that are constructed
171  /// using \ref SdrRegistry::GetShaderNodeFromSourceCode(), in which case, the
172  /// source code has not been parsed (or even compiled) yet.
173  ///
174  /// An unparsed node with non-empty source-code but no properties is
175  /// considered to be invalid. Once the node is parsed and the relevant
176  /// properties and metadata are extracted from the source code, the node
177  /// becomes valid.
178  ///
179  /// \sa SdrShaderNode::IsValid
180  const std::string &GetSourceCode() const { return _sourceCode; }
181 
182  /// Whether or not this node is valid. A node that is valid indicates that
183  /// the parser plugin was able to successfully parse the contents of this
184  /// node.
185  ///
186  /// Note that if a node is not valid, some data like its name, URI, source
187  /// code etc. could still be available (data that was obtained during the
188  /// discovery process). However, other data that must be gathered from the
189  /// parsing process will NOT be available (eg, inputs and outputs).
190  SDR_API
191  bool IsValid() const { return _isValid; }
192 
193  /// Gets a string with basic information about this node. Helpful for
194  /// things like adding this node to a log.
195  SDR_API
196  std::string GetInfoString() const;
197 
198  /// @}
199 
200  /// \name Inputs and Outputs
201  /// An input or output is also generically referred to as a "property".
202  /// @{
203 
204  /// Get an ordered list of all the input names on this shader node.
205  SDR_API
206  const SdrTokenVec& GetShaderInputNames() const;
207 
208  /// Get an ordered list of all the output names on this shader node.
209  SDR_API
210  const SdrTokenVec& GetShaderOutputNames() const;
211 
212  /// Get a shader input property by name. `nullptr` is returned if an input
213  /// with the given name does not exist.
214  SDR_API
215  SdrShaderPropertyConstPtr GetShaderInput(const TfToken& inputName) const;
216 
217  /// Get a shader output property by name. `nullptr` is returned if an output
218  /// with the given name does not exist.
219  SDR_API
220  SdrShaderPropertyConstPtr GetShaderOutput(const TfToken& outputName) const;
221 
222  /// Returns the list of all inputs that are tagged as asset identifier
223  /// inputs.
224  SDR_API
226 
227  /// Returns the first shader input that is tagged as the default input.
228  /// A default input and its value can be used to acquire a fallback value
229  /// for a node when the node is considered 'disabled' or otherwise
230  /// incapable of producing an output value.
231  SDR_API
233 
234  /// @}
235 
236 
237  /// \name Metadata
238  /// The metadata returned here is a direct result of what the parser plugin
239  /// is able to determine about the node. See the documentation for a
240  /// specific parser plugin to get help on what the parser is looking for to
241  /// populate these values.
242  /// @{
243 
244  /// All metadata that came from the parse process.
245  ///
246  /// \deprecated in favor of GetMetadataObject
247  ///
248  /// If this node was initialized with the upgraded SdrShaderNodeMetadata
249  /// class, the legacy metadata returned by this function will contain only
250  /// string representations of named metadata, and any other metadata items
251  /// whose value holds a string type in SdrShaderNodeMetadata. Other metadata
252  /// items may be omitted.
253  SDR_API
254  const SdrTokenMap& GetMetadata() const;
255 
256  /// All metadata that came from the parse process.
257  SDR_API
259 
260  /// Gets the domain of the node.
261  ///
262  /// \sa SdrShaderNodeMetadata::GetDomain
263  const TfToken& GetDomain() const { return _domain; }
264 
265  /// Gets the subdomain of the node if the subdomain metadata item exists.
266  ///
267  /// If the subdomain metadata doesn't exist, returns an empty TfToken.
268  ///
269  /// \sa SdrShaderNodeMetadata::GetSubdomain
270  const TfToken& GetSubdomain() const { return _subdomain; }
271 
272  /// Gets the context of the node.
273  ///
274  /// Context describes a node's usage group within its subdomain.
275  /// \sa SdrShaderNodeMetadata::GetContext.
276  ///
277  /// If the context metadatda doesn't exist, returns an empty TfToken.
278  const TfToken& GetContext() const { return _context; }
279 
280  /// Returns the role of this node.
281  ///
282  /// Role provides finer granularity for contexts that contain many nodes.
283  /// \sa SdrShaderNodeMetadata::GetRole
284  ///
285  /// If no role exists, returns the name of this node.
286  SDR_API
287  TfToken GetRole() const;
288 
289  /// The label assigned to this node, if any. Distinct from the name
290  /// returned from `GetName()`. In the context of a UI, the label value
291  /// might be used as the display name for the node instead of the name.
292  ///
293  /// Returns an empty TfToken if no label is present.
294  SDR_API
295  const TfToken& GetLabel() const { return _label; }
296 
297  /// The category assigned to this node, if any. Distinct from the family
298  /// returned from `GetFamily()`.
299  ///
300  /// \deprecated
301  ///
302  /// Returns an empty TfToken if no category is present.
303  SDR_API
304  const TfToken& GetCategory() const { return _category; }
305 
306  /// The help message assigned to this node, if any.
307  ///
308  /// Returns an empty string if no help message is present.
309  SDR_API
310  std::string GetHelp() const;
311 
312  /// The departments this node is associated with, if any.
313  ///
314  /// \deprecated
315  SDR_API
316  const SdrTokenVec& GetDepartments() const { return _departments; }
317 
318  /// Gets the pages which should be opened or expanded by default.
319  SDR_API
320  const SdrTokenVec& GetOpenPages() const { return _openPages; }
321 
322  /// Gets the `shownIf` expressions associated with each page.
323  SDR_API
324  const SdrTokenMap& GetPagesShownIf() const { return _pagesShownIf; }
325 
326  /// The list of primvars this node knows it requires / uses.
327  /// For example, a shader node may require the 'normals' primvar to function
328  /// correctly. Additional, user specified primvars may have been authored on
329  /// the node. These can be queried via `GetAdditionalPrimvarProperties()`.
330  /// Together, `GetPrimvars()` and `GetAdditionalPrimvarProperties()`,
331  /// provide the complete list of primvar requirements for the node.
332  ///
333  /// Note that SdrShaderNode::GetPrimvars isn't equivalent to
334  /// SdrShaderNodeMetadata::GetPrimvars. The former returns primvars
335  /// processed with access to SdrShaderProperty information.
336  SDR_API
337  const SdrTokenVec& GetPrimvars() const { return _primvars; }
338 
339  /// The list of string input properties whose values provide the names of
340  /// additional primvars consumed by this node. For example, this may return
341  /// a token named `varname`. This indicates that the client should query the
342  /// value of a (presumed to be string-valued) input attribute named varname
343  /// from its scene description to determine the name of a primvar the
344  /// node will consume. See `GetPrimvars()` for additional information.
345  SDR_API
348  }
349 
350  /// Returns the implementation name of this node. The name of the node
351  /// is how to refer to the node in shader networks. The label is how to
352  /// present this node to users. The implementation name is the name of
353  /// the function (or something) this node represents in the
354  /// implementation. Any client using the implementation \b must call
355  /// this method to get the correct name; using \c getName() is not
356  /// correct.
357  SDR_API
358  std::string GetImplementationName() const;
359 
360  /// @}
361 
362 
363  /// \name Aggregate Information
364  /// @{
365 
366  /// Gets the pages on which the node's properties reside (an aggregate of
367  /// the unique `SdrShaderProperty::GetPage()` values for all of the node's
368  /// properties). Nodes themselves do not reside on pages. In an example
369  /// scenario, properties might be divided into two pages, 'Simple' and
370  /// 'Advanced'.
371  SDR_API
372  const SdrTokenVec& GetPages() const { return _pages; }
373 
374  /// Gets the names of the properties on a certain page (one that was
375  /// returned by `GetPages()`). To get properties that are not assigned to a
376  /// page, an empty string can be used for \p pageName.
377  SDR_API
378  SdrTokenVec GetPropertyNamesForPage(const std::string& pageName) const;
379 
380  /// Gets all vstructs that are present in the shader.
381  SDR_API
383 
384  /// @}
385 
386  // Stores the result of the compliance check of property names to
387  // sdrShaderNodeIdentifiers
388  using ComplianceResults = std::unordered_map<TfToken,
389  std::vector<SdrIdentifier>,
391 
392  /// This method checks if same named properties of \p shaderNodes are
393  /// compatible with each other.
394  ///
395  /// Checks if the same name properties have matching types and default
396  /// values. In order to determine if same name properties from different
397  /// shader nodes are compliant, we assume that the first shaderNode in the
398  /// list providing this property is authoritative, and other nodes differing
399  /// wrt this property are non-compliant. A map of property names and their
400  /// respective shaderNodes are stored in the map. An empty map returned
401  /// represents no compliance issues.
402  SDR_API
403  static
405  const std::vector<SdrShaderNodeConstPtr> &shaderNodes);
406 
407  /// \cond
408  /// Hide from the API.
409 
410  // Performs a post-process on properties to determine information that can
411  // only be determined after parsing or in aggregate. Clients SHOULD NOT
412  // need to call this.
413  void _PostProcessProperties();
414 
415  /// \endcond
416 
417  /// Gets an item of data from this shader node according to the
418  /// requested key.
419 
420  /// Special keys indicate class fields and are identified by the
421  /// SdrNodeFieldKey enum. The return type of GetDataForKey for these
422  /// special keys is the return type of their getters.
423  /// GetDataForKey(SdrNodeFieldKey->Identifier) results in VtValue
424  /// holding a TfToken because GetIdentifier returns TfToken. The
425  /// return types for these special keys is as follows:
426  /// - SdrNodeFieldKey->Identifier -> VtValue holding TfToken
427  /// - SdrNodeFieldKey->Name -> VtValue holding std::string
428  /// - SdrNodeFieldKey->Function -> VtValue holding TfToken
429  /// - SdrNodeFieldKey->ShadingSystem -> VtValue holding TfToken
430  /// - (deprecated) SdrNodeFieldKey->SourceType -> VtValue holding TfToken
431  ///
432  /// Any requested data item that isn't a SdrNodeFieldKey will be looked
433  /// for in this shader node's metadata.
434  ///
435  /// Returns an empty VtValue if no data is found to be associated with the
436  /// requested key.
437  SDR_API
438  VtValue GetDataForKey(const TfToken& key) const;
439 
440 protected:
441  SdrShaderNode& operator=(const SdrShaderNode&) = delete;
442 
443  bool _isValid;
446  std::string _name;
450  std::string _definitionURI;
451  std::string _implementationURI;
455  std::string _sourceCode;
456 
461 
462  // Stored metadata to support getter API on SdrShaderNode that
463  // returns const ref.
471 
472  // Processed primvar metadata. `_primvars` contains the names of primvars
473  // consumed by this node, whereas `_primvarNamingProperties` contains the
474  // names of string input properties whose values provide the names of
475  // additional primvars consumed by this node.
478 
479  // Aggregated pages data, from examining SdrShaderProperty's page metadata
481 
482 private:
483  // Initializes `_primvars` and `_primvarNamingProperties`
484  void _InitializePrimvars();
485 
486  // Determines which pages are present on the node's properties
487  SdrTokenVec _ComputePages() const;
488 };
489 
491 
492 #endif // PXR_USD_SDR_SHADER_NODE_H
TF_DECLARE_PUBLIC_TOKENS(SdrNodeFieldKey, SDR_API, SDR_NODE_FIELD_KEY_TOKENS)
SDR_API const SdrTokenVec & GetPages() const
Definition: shaderNode.h:372
SDR_API const SdrTokenVec & GetPrimvars() const
Definition: shaderNode.h:337
SDR_API const TfToken & GetCategory() const
Definition: shaderNode.h:304
const TfToken & GetContext() const
Definition: shaderNode.h:278
SDR_API const TfToken & GetLabel() const
Definition: shaderNode.h:295
SdrTokenVec _pages
Definition: shaderNode.h:480
SdrTokenVec _openPages
Definition: shaderNode.h:469
SdrVersion GetShaderVersion() const
Return the version of the node.
Definition: shaderNode.h:118
std::string _implementationURI
Definition: shaderNode.h:451
TfToken _category
Definition: shaderNode.h:467
const std::string & GetResolvedDefinitionURI() const
Definition: shaderNode.h:158
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
const std::string & GetName() const
Gets the name of the node.
Definition: shaderNode.h:121
SdrTokenVec _departments
Definition: shaderNode.h:468
const TfToken & GetFamily() const
Definition: shaderNode.h:127
SDR_API SdrShaderPropertyConstPtr GetShaderInput(const TfToken &inputName) const
SDR_API const SdrTokenMap & GetPagesShownIf() const
Gets the shownIf expressions associated with each page.
Definition: shaderNode.h:324
TfToken _domain
Definition: shaderNode.h:464
SDR_API SdrShaderNode(const SdrIdentifier &identifier, const SdrVersion &version, const std::string &name, const TfToken &function, const TfToken &shadingSystem, const std::string &definitionURI, const std::string &implementationURI, SdrShaderPropertyUniquePtrVec &&properties, const SdrShaderNodeMetadata &metadata=SdrShaderNodeMetadata(), const std::string &sourceCode=std::string())
Constructor.
Definition: shaderNode.h:63
SdrTokenVec _primvarNamingProperties
Definition: shaderNode.h:477
TfToken _context
Definition: shaderNode.h:448
Functor to use for hash maps from tokens to other things.
Definition: token.h:149
SDR_API const SdrTokenVec & GetAdditionalPrimvarProperties() const
Definition: shaderNode.h:346
SdrVersion _version
Definition: shaderNode.h:445
const TfToken & GetSourceType() const
Definition: shaderNode.h:151
std::string _sourceCode
Definition: shaderNode.h:455
SDR_API const SdrTokenVec & GetDepartments() const
Definition: shaderNode.h:316
TfToken _label
Definition: shaderNode.h:466
SdrTokenVec _primvars
Definition: shaderNode.h:476
SDR_API std::string GetHelp() const
SdrVersion.
Definition: declare.h:67
const std::string & GetResolvedImplementationURI() const
Definition: shaderNode.h:165
const TfToken & GetDomain() const
Definition: shaderNode.h:263
SDR_API std::string GetImplementationName() const
Definition: token.h:70
SdrTokenVec _inputNames
Definition: shaderNode.h:458
SDR_API SdrShaderPropertyConstPtr GetDefaultInput() const
SDR_API const SdrTokenVec & GetOpenPages() const
Gets the pages which should be opened or expanded by default.
Definition: shaderNode.h:320
SDR_API bool IsValid() const
Definition: shaderNode.h:191
SDR_API TfToken GetRole() const
SDR_API std::string GetInfoString() const
std::string _name
Definition: shaderNode.h:446
SDR_API const SdrTokenVec & GetShaderOutputNames() const
Get an ordered list of all the output names on this shader node.
SdrTokenMap _legacyMetadata
Definition: shaderNode.h:453
std::unordered_map< TfToken, std::vector< SdrIdentifier >, TfToken::HashFunctor > ComplianceResults
Definition: shaderNode.h:390
SDR_API VtValue GetDataForKey(const TfToken &key) const
SDR_API const SdrShaderNodeMetadata & GetMetadataObject() const
All metadata that came from the parse process.
GLuint const GLchar * name
Definition: glcorearb.h:786
const SdrIdentifier & GetIdentifier() const
Return the identifier of the node.
Definition: shaderNode.h:115
SdrShaderProperty const * SdrShaderPropertyConstPtr
Definition: declare.h:53
SdrShaderNode & operator=(const SdrShaderNode &)=delete
GT_API const UT_StringHolder version
SDR_API SdrTokenVec GetPropertyNamesForPage(const std::string &pageName) const
SdrShaderPropertyMap _inputs
Definition: shaderNode.h:457
virtual SDR_API ~SdrShaderNode()
Destructor.
const TfToken & GetShadingSystem() const
Definition: shaderNode.h:145
SdrShaderPropertyUniquePtrVec _properties
Definition: shaderNode.h:452
SDR_API const SdrTokenVec & GetShaderInputNames() const
Get an ordered list of all the input names on this shader node.
TfToken _subdomain
Definition: shaderNode.h:465
const TfToken & GetSubdomain() const
Definition: shaderNode.h:270
static SDR_API ComplianceResults CheckPropertyCompliance(const std::vector< SdrShaderNodeConstPtr > &shaderNodes)
SdrShaderNodeMetadata _metadata
Definition: shaderNode.h:454
SDR_API SdrTokenVec GetAssetIdentifierInputNames() const
SDR_API const SdrTokenMap & GetMetadata() const
std::unordered_map< TfToken, SdrShaderPropertyConstPtr, TfToken::HashFunctor > SdrShaderPropertyMap
Definition: declare.h:57
std::string _definitionURI
Definition: shaderNode.h:450
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
const std::string & GetSourceCode() const
Definition: shaderNode.h:180
SdrIdentifier _identifier
Definition: shaderNode.h:444
SDR_API SdrShaderPropertyConstPtr GetShaderOutput(const TfToken &outputName) const
SDR_API SdrTokenVec GetAllVstructNames() const
Gets all vstructs that are present in the shader.
SdrShaderPropertyMap _outputs
Definition: shaderNode.h:459
std::vector< TfToken > SdrTokenVec
Definition: declare.h:39
#define SDR_API
Definition: api.h:23
Definition: value.h:89
TfToken _shadingSystem
Definition: shaderNode.h:449
TfToken _function
Definition: shaderNode.h:447
SdrTokenVec _outputNames
Definition: shaderNode.h:460
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > SdrTokenMap
Definition: declare.h:41
const TfToken & GetFunction() const
Definition: shaderNode.h:134
std::vector< SdrShaderPropertyUniquePtr > SdrShaderPropertyUniquePtrVec
Definition: declare.h:55
SdrTokenMap _pagesShownIf
Definition: shaderNode.h:470
#define SDR_NODE_FIELD_KEY_TOKENS
Definition: shaderNode.h:26