This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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
12
13#include "pxr/pxr.h"
14#include "pxr/usd/sdr/api.h"
16#include "pxr/usd/ndr/node.h"
17#include "pxr/usd/sdr/declare.h"
18
19#include <unordered_map>
20
21PXR_NAMESPACE_OPEN_SCOPE
22
23// Note: Metadata keys that are generated by parsers should start with
24// "__SDR__" to reduce the risk of collision with metadata actually in the
25// shader.
26#define SDR_NODE_METADATA_TOKENS \
27 ((Category, "category")) \
28 ((Role, "role")) \
29 ((Departments, "departments")) \
30 ((Help, "help")) \
31 ((Label, "label")) \
32 ((Pages, "pages")) \
33 ((Primvars, "primvars")) \
34 ((ImplementationName, "__SDR__implementationName"))\
35 ((Target, "__SDR__target")) \
36 ((SdrUsdEncodingVersion, "sdrUsdEncodingVersion")) \
37 ((SdrDefinitionNameFallbackPrefix, "sdrDefinitionNameFallbackPrefix"))
38
39// Note: The concept of context is defined on NdrNode and can be queried with
40// the GetContext() method. Sdr categorizes shaders by the context in which they
41// are used inside of a renderer. For instance during 'pattern' evaluation to
42// feed into a surface or volume shader. For BXDFs used in 'surface' and
43// 'volume' rendering situations.
44#define SDR_NODE_CONTEXT_TOKENS \
45 ((Pattern, "pattern")) \
46 ((Surface, "surface")) \
47 ((Volume, "volume")) \
48 ((Displacement, "displacement")) \
49 ((Light, "light")) \
50 ((DisplayFilter, "displayFilter")) \
51 ((LightFilter, "lightFilter")) \
52 ((PixelFilter, "pixelFilter")) \
53 ((SampleFilter, "sampleFilter"))
54
55#define SDR_NODE_ROLE_TOKENS \
56 ((Primvar, "primvar")) \
57 ((Texture, "texture")) \
58 ((Field, "field")) \
59 ((Math, "math")) \
60
61TF_DECLARE_PUBLIC_TOKENS(SdrNodeMetadata, SDR_API, SDR_NODE_METADATA_TOKENS);
62TF_DECLARE_PUBLIC_TOKENS(SdrNodeContext, SDR_API, SDR_NODE_CONTEXT_TOKENS);
63TF_DECLARE_PUBLIC_TOKENS(SdrNodeRole, SDR_API, SDR_NODE_ROLE_TOKENS);
64
69class SdrShaderNode : public NdrNode
70{
71public:
73 SDR_API
74 SdrShaderNode(const NdrIdentifier& identifier,
75 const NdrVersion& version,
76 const std::string& name,
77 const TfToken& family,
78 const TfToken& context,
79 const TfToken& sourceType,
80 const std::string& definitionURI,
81 const std::string& implementationURI,
82 NdrPropertyUniquePtrVec&& properties,
83 const NdrTokenMap& metadata = NdrTokenMap(),
84 const std::string &sourceCode = std::string());
85
89
92 SDR_API
93 SdrShaderPropertyConstPtr GetShaderInput(const TfToken& inputName) const;
94
97 SDR_API
98 SdrShaderPropertyConstPtr GetShaderOutput(const TfToken& outputName) const;
99
102 SDR_API
103 NdrTokenVec GetAssetIdentifierInputNames() const;
104
109 SDR_API
110 SdrShaderPropertyConstPtr GetDefaultInput() const;
111
113
114
121
125 SDR_API
126 const TfToken& GetLabel() const { return _label; }
127
130 SDR_API
131 const TfToken& GetCategory() const { return _category; }
132
139 SDR_API
140 std::string GetRole() const;
141
143 SDR_API
144 std::string GetHelp() const;
145
147 SDR_API
148 const NdrTokenVec& GetDepartments() const { return _departments; }
149
155 SDR_API
156 const NdrTokenVec& GetPages() const { return _pages; };
157
164 SDR_API
165 const NdrTokenVec& GetPrimvars() const { return _primvars; }
166
173 SDR_API
174 const NdrTokenVec& GetAdditionalPrimvarProperties() const {
175 return _primvarNamingProperties;
176 }
177
185 SDR_API
186 std::string GetImplementationName() const;
187
189
190
193
197 SDR_API
198 NdrTokenVec GetPropertyNamesForPage(const std::string& pageName) const;
199
201 SDR_API
202 NdrTokenVec GetAllVstructNames() const;
203
205
206 // Stores the result of the compliance check of property names to
207 // sdrShaderNodeIdentifiers
208 using ComplianceResults = std::unordered_map<TfToken,
209 std::vector<NdrIdentifier>,
211
222 SDR_API
223 static
224 ComplianceResults CheckPropertyCompliance(
225 const std::vector<SdrShaderNodeConstPtr> &shaderNodes);
226
229
230 // Performs a post-process on properties to determine information that can
231 // only be determined after parsing or in aggregate. Clients SHOULD NOT
232 // need to call this.
233 void _PostProcessProperties();
234
236
237protected:
238 // Processed primvar metadata. `_primvars` contains the names of primvars
239 // consumed by this node, whereas `_primvarNamingProperties` contains the
240 // names of string input properties whose values provide the names of
241 // additional primvars consumed by this node.
242 NdrTokenVec _primvars;
243 NdrTokenVec _primvarNamingProperties;
244
245 // Tokenized metadata
246 TfToken _label;
247 TfToken _category;
248 NdrTokenVec _departments;
249 NdrTokenVec _pages;
250
251 SdrPropertyMap _shaderInputs;
252 SdrPropertyMap _shaderOutputs;
253
254private:
255 // Initializes `_primvars` and `_primvarNamingProperties`
256 void _InitializePrimvars();
257
258 // Determines which pages are present on the node's properties
259 NdrTokenVec _ComputePages() const;
260};
261
262PXR_NAMESPACE_CLOSE_SCOPE
263
264#endif // PXR_USD_SDR_SHADER_NODE_H
Represents an abstract node.
Definition: node.h:32
A specialized version of NdrNode which holds shading information.
Definition: shaderNode.h:70
SDR_API const NdrTokenVec & GetAdditionalPrimvarProperties() const
The list of string input properties whose values provide the names of additional primvars consumed by...
Definition: shaderNode.h:174
SDR_API const NdrTokenVec & GetPages() const
Gets the pages on which the node's properties reside (an aggregate of the unique SdrShaderProperty::G...
Definition: shaderNode.h:156
SDR_API const TfToken & GetLabel() const
The label assigned to this node, if any.
Definition: shaderNode.h:126
SDR_API SdrShaderPropertyConstPtr GetShaderInput(const TfToken &inputName) const
Get a shader input property by name.
SDR_API NdrTokenVec GetAssetIdentifierInputNames() const
Returns the list of all inputs that are tagged as asset identifier inputs.
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 const TfToken & GetCategory() const
The category assigned to this node, if any.
Definition: shaderNode.h:131
SDR_API NdrTokenVec 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 NdrTokenVec GetAllVstructNames() const
Gets all vstructs that are present in the shader.
SDR_API SdrShaderPropertyConstPtr GetShaderOutput(const TfToken &outputName) const
Get a shader output property by name.
SDR_API const NdrTokenVec & GetPrimvars() const
The list of primvars this node knows it requires / uses.
Definition: shaderNode.h:165
SDR_API std::string GetHelp() const
The help message assigned to this node, if any.
SDR_API const NdrTokenVec & GetDepartments() const
The departments this node is associated with, if any.
Definition: shaderNode.h:148
SDR_API std::string GetImplementationName() const
Returns the implementation name of this node.
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.
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