Loading...
Searching...
No Matches
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
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"
20
21#include <unordered_map>
22
23PXR_NAMESPACE_OPEN_SCOPE
24
25// Note: Metadata keys that are generated by parsers should start with
26// "__SDR__" to reduce the risk of collision with metadata actually in the
27// shader.
28#define SDR_NODE_METADATA_TOKENS \
29 ((Category, "category")) \
30 ((Role, "role")) \
31 ((Departments, "departments")) \
32 ((Help, "help")) \
33 ((Label, "label")) \
34 ((Pages, "pages")) \
35 ((OpenPages, "openPages")) \
36 ((Primvars, "primvars")) \
37 ((ImplementationName, "__SDR__implementationName"))\
38 ((Target, "__SDR__target")) \
39 ((SdrUsdEncodingVersion, "sdrUsdEncodingVersion")) \
40 ((SdrDefinitionNameFallbackPrefix, "sdrDefinitionNameFallbackPrefix"))
41
45#define SDR_NODE_METADATA_PREFIX_TOKENS \
46 ((PageShownIf, "pageShownIf"))
47
48// Note: The concept of context can be queried with the GetContext() method.
49// Sdr categorizes shaders by the context in which they are used inside of a
50// renderer. For instance during 'pattern' evaluation to feed into a surface
51// or volume shader. For BXDFs used in 'surface' and 'volume'
52// rendering situations.
53#define SDR_NODE_CONTEXT_TOKENS \
54 ((Pattern, "pattern")) \
55 ((Surface, "surface")) \
56 ((Volume, "volume")) \
57 ((Displacement, "displacement")) \
58 ((Light, "light")) \
59 ((DisplayFilter, "displayFilter")) \
60 ((LightFilter, "lightFilter")) \
61 ((PixelFilter, "pixelFilter")) \
62 ((SampleFilter, "sampleFilter"))
63
64#define SDR_NODE_ROLE_TOKENS \
65 ((Primvar, "primvar")) \
66 ((Texture, "texture")) \
67 ((Field, "field")) \
68 ((Math, "math")) \
69
70#define SDR_NODE_FIELD_KEY_TOKENS \
71 ((Identifier, "_identifier")) \
72 ((Name, "_name")) \
73 ((Family, "_family")) \
74 ((SourceType, "_sourceType"))
75
76TF_DECLARE_PUBLIC_TOKENS(SdrNodeMetadata, SDR_API, SDR_NODE_METADATA_TOKENS);
77TF_DECLARE_PUBLIC_TOKENS(SdrNodeMetadataPrefix, SDR_API,
79TF_DECLARE_PUBLIC_TOKENS(SdrNodeContext, SDR_API, SDR_NODE_CONTEXT_TOKENS);
80TF_DECLARE_PUBLIC_TOKENS(SdrNodeRole, SDR_API, SDR_NODE_ROLE_TOKENS);
81TF_DECLARE_PUBLIC_TOKENS(SdrNodeFieldKey, SDR_API, SDR_NODE_FIELD_KEY_TOKENS);
82
89{
90public:
92 SDR_API
94 const SdrIdentifier& identifier,
95 const SdrVersion& version,
96 const std::string& name,
97 const TfToken& family,
98 const TfToken& context,
99 const TfToken& sourceType,
100 const std::string& definitionURI,
101 const std::string& implementationURI,
102 SdrShaderPropertyUniquePtrVec&& properties,
103 const SdrTokenMap& metadata = SdrTokenMap(),
104 const std::string &sourceCode = std::string());
105
107 SDR_API
108 virtual ~SdrShaderNode();
109
112
114 const SdrIdentifier& GetIdentifier() const { return _identifier; }
115
117 SdrVersion GetShaderVersion() const { return _version; }
118
120 const std::string& GetName() const { return _name; }
121
124 const TfToken& GetFamily() const { return _family; }
125
138 const TfToken& GetContext() const { return _context; }
139
152 const TfToken& GetSourceType() const { return _sourceType; }
153
159 const std::string& GetResolvedDefinitionURI() const { return _definitionURI; }
160
166 const std::string& GetResolvedImplementationURI() const {
167 return _implementationURI;
168 }
169
181 const std::string &GetSourceCode() const { return _sourceCode; }
182
191 SDR_API
192 bool IsValid() const { return _isValid; }
193
196 SDR_API
197 std::string GetInfoString() const;
198
200
204
206 SDR_API
207 const SdrTokenVec& GetShaderInputNames() const;
208
210 SDR_API
211 const SdrTokenVec& GetShaderOutputNames() const;
212
215 SDR_API
216 SdrShaderPropertyConstPtr GetShaderInput(const TfToken& inputName) const;
217
220 SDR_API
221 SdrShaderPropertyConstPtr GetShaderOutput(const TfToken& outputName) const;
222
225 SDR_API
226 SdrTokenVec GetAssetIdentifierInputNames() const;
227
232 SDR_API
233 SdrShaderPropertyConstPtr GetDefaultInput() const;
234
236
237
244
246 SDR_API
247 const SdrTokenMap& GetMetadata() const;
248
252 SDR_API
253 const TfToken& GetLabel() const { return _label; }
254
257 SDR_API
258 const TfToken& GetCategory() const { return _category; }
259
266 SDR_API
267 std::string GetRole() const;
268
270 SDR_API
271 std::string GetHelp() const;
272
274 SDR_API
275 const SdrTokenVec& GetDepartments() const { return _departments; }
276
282 SDR_API
283 const SdrTokenVec& GetPages() const { return _pages; };
284
286 SDR_API
287 const SdrTokenVec& GetOpenPages() const { return _openPages; };
288
290 SDR_API
291 const SdrTokenMap& GetPagesShownIf() const { return _pagesShownIf; }
292
299 SDR_API
300 const SdrTokenVec& GetPrimvars() const { return _primvars; }
301
308 SDR_API
309 const SdrTokenVec& GetAdditionalPrimvarProperties() const {
310 return _primvarNamingProperties;
311 }
312
320 SDR_API
321 std::string GetImplementationName() const;
322
324
325
328
332 SDR_API
333 SdrTokenVec GetPropertyNamesForPage(const std::string& pageName) const;
334
336 SDR_API
337 SdrTokenVec GetAllVstructNames() const;
338
340
341 // Stores the result of the compliance check of property names to
342 // sdrShaderNodeIdentifiers
343 using ComplianceResults = std::unordered_map<TfToken,
344 std::vector<SdrIdentifier>,
346
357 SDR_API
358 static
359 ComplianceResults CheckPropertyCompliance(
360 const std::vector<SdrShaderNodeConstPtr> &shaderNodes);
361
364
365 // Performs a post-process on properties to determine information that can
366 // only be determined after parsing or in aggregate. Clients SHOULD NOT
367 // need to call this.
368 void _PostProcessProperties();
369
371
374
391 SDR_API
392 VtValue GetDataForKey(const TfToken& key) const;
393
394protected:
395 SdrShaderNode& operator=(const SdrShaderNode&) = delete;
396
397 bool _isValid;
398 SdrIdentifier _identifier;
399 SdrVersion _version;
400 std::string _name;
401 TfToken _family;
402 TfToken _context;
403 TfToken _sourceType;
404 std::string _definitionURI;
405 std::string _implementationURI;
406 SdrShaderPropertyUniquePtrVec _properties;
407 SdrTokenMap _metadata;
408 std::string _sourceCode;
409
410 SdrShaderPropertyMap _inputs;
411 SdrTokenVec _inputNames;
412 SdrShaderPropertyMap _outputs;
413 SdrTokenVec _outputNames;
414
415 // Processed primvar metadata. `_primvars` contains the names of primvars
416 // consumed by this node, whereas `_primvarNamingProperties` contains the
417 // names of string input properties whose values provide the names of
418 // additional primvars consumed by this node.
419 SdrTokenVec _primvars;
420 SdrTokenVec _primvarNamingProperties;
421
422 // Tokenized metadata
423 TfToken _label;
424 TfToken _category;
425 SdrTokenVec _departments;
426 SdrTokenVec _pages;
427 SdrTokenVec _openPages;
428 SdrTokenMap _pagesShownIf;
429
430private:
431 // Initializes `_primvars` and `_primvarNamingProperties`
432 void _InitializePrimvars();
433
434 // Determines which pages are present on the node's properties
435 SdrTokenVec _ComputePages() const;
436};
437
438PXR_NAMESPACE_CLOSE_SCOPE
439
440#endif // PXR_USD_SDR_SHADER_NODE_H
Represents a node that holds shading information.
Definition: shaderNode.h:89
SDR_API const SdrTokenMap & GetMetadata() const
All metadata that came from the parse process.
SDR_API SdrShaderNode(const SdrIdentifier &identifier, const SdrVersion &version, const std::string &name, const TfToken &family, const TfToken &context, const TfToken &sourceType, const std::string &definitionURI, const std::string &implementationURI, SdrShaderPropertyUniquePtrVec &&properties, const SdrTokenMap &metadata=SdrTokenMap(), const std::string &sourceCode=std::string())
Constructor.
SDR_API const SdrTokenVec & GetPrimvars() const
The list of primvars this node knows it requires / uses.
Definition: shaderNode.h:300
virtual SDR_API ~SdrShaderNode()
Destructor.
SDR_API SdrTokenVec GetAssetIdentifierInputNames() const
Returns the list of all inputs that are tagged as asset identifier inputs.
SDR_API const SdrTokenVec & GetAdditionalPrimvarProperties() const
The list of string input properties whose values provide the names of additional primvars consumed by...
Definition: shaderNode.h:309
const SdrIdentifier & GetIdentifier() const
Return the identifier of the node.
Definition: shaderNode.h:114
SDR_API const TfToken & GetLabel() const
The label assigned to this node, if any.
Definition: shaderNode.h:253
SDR_API SdrShaderPropertyConstPtr GetShaderInput(const TfToken &inputName) const
Get a shader input property by name.
const TfToken & GetFamily() const
Gets the name of the family that the node belongs to.
Definition: shaderNode.h:124
static SDR_API ComplianceResults CheckPropertyCompliance(const std::vector< SdrShaderNodeConstPtr > &shaderNodes)
This method checks if same named properties of shaderNodes are compatible with each other.
SDR_API SdrTokenVec GetAllVstructNames() const
Gets all vstructs that are present in the shader.
SDR_API const TfToken & GetCategory() const
The category assigned to this node, if any.
Definition: shaderNode.h:258
const std::string & GetName() const
Gets the name of the node.
Definition: shaderNode.h:120
SDR_API SdrTokenVec GetPropertyNamesForPage(const std::string &pageName) const
Gets the names of the properties on a certain page (one that was returned by GetPages()).
SDR_API SdrShaderPropertyConstPtr GetShaderOutput(const TfToken &outputName) const
Get a shader output property by name.
SDR_API VtValue GetDataForKey(const TfToken &key) const
Gets an item of data from this shader node according to the requested key.
SDR_API std::string GetHelp() const
The help message assigned to this node, if any.
SDR_API const SdrTokenVec & GetPages() const
Gets the pages on which the node's properties reside (an aggregate of the unique SdrShaderProperty::G...
Definition: shaderNode.h:283
const TfToken & GetContext() const
Gets the context of the shader node.
Definition: shaderNode.h:138
SDR_API const SdrTokenVec & GetDepartments() const
The departments this node is associated with, if any.
Definition: shaderNode.h:275
SDR_API std::string GetImplementationName() const
Returns the implementation name of this node.
const std::string & GetResolvedImplementationURI() const
Gets the URI to the resource that provides this node's implementation.
Definition: shaderNode.h:166
SDR_API const SdrTokenVec & GetShaderInputNames() const
Get an ordered list of all the input names on this shader node.
SdrVersion GetShaderVersion() const
Return the version of the node.
Definition: shaderNode.h:117
SDR_API const SdrTokenMap & GetPagesShownIf() const
Gets the shownIf expressions associated with each page.
Definition: shaderNode.h:291
SDR_API const SdrTokenVec & GetOpenPages() const
Gets the pages which should be opened or expanded by default.
Definition: shaderNode.h:287
const std::string & GetSourceCode() const
Returns the source code for this node.
Definition: shaderNode.h:181
SDR_API SdrShaderPropertyConstPtr GetDefaultInput() const
Returns the first shader input that is tagged as the default input.
const TfToken & GetSourceType() const
Gets the type of source that this shader node originated from.
Definition: shaderNode.h:152
SDR_API bool IsValid() const
Whether or not this node is valid.
Definition: shaderNode.h:192
SDR_API std::string GetRole() const
Returns the role of this node.
SDR_API const SdrTokenVec & GetShaderOutputNames() const
Get an ordered list of all the output names on this shader node.
SDR_API std::string GetInfoString() const
Gets a string with basic information about this node.
const std::string & GetResolvedDefinitionURI() const
Gets the URI to the resource that provided this node's definition.
Definition: shaderNode.h:159
SdrVersion.
Definition: declare.h:67
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:90
#define SDR_NODE_METADATA_PREFIX_TOKENS
Definition: shaderNode.h:45
This file defines some macros that are useful for declaring and using static TfTokens.
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:92
Functor to use for hash maps from tokens to other things.
Definition: token.h:149
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...