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
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
28PXR_NAMESPACE_OPEN_SCOPE
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
68TF_DECLARE_PUBLIC_TOKENS(SdrNodeMetadata, SDR_API, SDR_NODE_METADATA_TOKENS);
69TF_DECLARE_PUBLIC_TOKENS(SdrNodeContext, SDR_API, SDR_NODE_CONTEXT_TOKENS);
70TF_DECLARE_PUBLIC_TOKENS(SdrNodeRole, SDR_API, SDR_NODE_ROLE_TOKENS);
71
77class SdrShaderNode : public NdrNode
78{
79public:
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
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
115 SdrVersion GetShaderVersion() const { return NdrToSdrVersion(_version); }
116
120
122 SDR_API
123 const SdrTokenVec& GetShaderInputNames() const;
124
126 SDR_API
127 const SdrTokenVec& GetShaderOutputNames() const;
128
131 SDR_API
132 SdrShaderPropertyConstPtr GetShaderInput(const TfToken& inputName) const;
133
136 SDR_API
137 SdrShaderPropertyConstPtr GetShaderOutput(const TfToken& outputName) const;
138
141 SDR_API
142 SdrTokenVec GetAssetIdentifierInputNames() const;
143
148 SDR_API
149 SdrShaderPropertyConstPtr GetDefaultInput() const;
150
152
153
160
164 SDR_API
165 const TfToken& GetLabel() const { return _label; }
166
169 SDR_API
170 const TfToken& GetCategory() const { return _category; }
171
178 SDR_API
179 std::string GetRole() const;
180
182 SDR_API
183 std::string GetHelp() const;
184
186 SDR_API
187 const SdrTokenVec& GetDepartments() const { return _departments; }
188
194 SDR_API
195 const SdrTokenVec& GetPages() const { return _pages; };
196
203 SDR_API
204 const SdrTokenVec& GetPrimvars() const { return _primvars; }
205
212 SDR_API
213 const SdrTokenVec& GetAdditionalPrimvarProperties() const {
214 return _primvarNamingProperties;
215 }
216
224 SDR_API
225 std::string GetImplementationName() const;
226
228
229
232
236 SDR_API
237 SdrTokenVec GetPropertyNamesForPage(const std::string& pageName) const;
238
240 SDR_API
241 SdrTokenVec GetAllVstructNames() const;
242
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
261 SDR_API
262 static
263 ComplianceResults CheckPropertyCompliance(
264 const std::vector<SdrShaderNodeConstPtr> &shaderNodes);
265
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
275
276protected:
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.
281 SdrTokenVec _primvars;
282 SdrTokenVec _primvarNamingProperties;
283
284 // Tokenized metadata
285 TfToken _label;
286 TfToken _category;
287 SdrTokenVec _departments;
288 SdrTokenVec _pages;
289
290 SdrPropertyMap _shaderInputs;
291 SdrPropertyMap _shaderOutputs;
292
293private:
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
301PXR_NAMESPACE_CLOSE_SCOPE
302
303#endif // PXR_USD_SDR_SHADER_NODE_H
Represents an abstract node.
Definition: node.h:38
NdrVersion.
Definition: declare.h:79
Represents a node that holds shading information.
Definition: shaderNode.h:78
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:204
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:213
SDR_API const TfToken & GetLabel() const
The label assigned to this node, if any.
Definition: shaderNode.h:165
SDR_API SdrShaderPropertyConstPtr GetShaderInput(const TfToken &inputName) const
Get a shader input property by name.
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:170
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 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())
Constructor.
SDR_API SdrShaderPropertyConstPtr GetShaderOutput(const TfToken &outputName) const
Get a shader output property by name.
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:195
SDR_API const SdrTokenVec & GetDepartments() const
The departments this node is associated with, if any.
Definition: shaderNode.h:187
SDR_API std::string GetImplementationName() const
Returns the implementation name of this node.
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:115
SDR_API SdrShaderPropertyConstPtr GetDefaultInput() const
Returns the first shader input that is tagged as the default input.
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.
SdrVersion.
Definition: declare.h:73
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
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:81
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...
SDR_API SdrVersion NdrToSdrVersion(NdrVersion version)
Helper function to translate NdrVersion to SdrVersion.