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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 
25 #ifndef PXR_USD_SDR_SHADER_NODE_H
26 #define PXR_USD_SDR_SHADER_NODE_H
27 
28 /// \file sdr/shaderNode.h
29 
30 #include "pxr/pxr.h"
31 #include "pxr/usd/sdr/api.h"
33 #include "pxr/usd/ndr/node.h"
34 #include "pxr/usd/sdr/declare.h"
35 
37 
38 // Note: Metadata keys that are generated by parsers should start with
39 // "__SDR__" to reduce the risk of collision with metadata actually in the
40 // shader.
41 #define SDR_NODE_METADATA_TOKENS \
42  ((Category, "category")) \
43  ((Role, "role")) \
44  ((Departments, "departments")) \
45  ((Help, "help")) \
46  ((Label, "label")) \
47  ((Pages, "pages")) \
48  ((Primvars, "primvars")) \
49  ((ImplementationName, "__SDR__implementationName"))\
50  ((Target, "__SDR__target")) \
51  ((SdrUsdEncodingVersion, "sdrUsdEncodingVersion")) \
52  ((SdrDefinitionNameFallbackPrefix, "sdrDefinitionNameFallbackPrefix"))
53 
54 // Note: The concept of context is defined on NdrNode and can be queried with
55 // the GetContext() method. Sdr categorizes shaders by the context in which they
56 // are used inside of a renderer. For instance during 'pattern' evaluation to
57 // feed into a surface or volume shader. For BXDFs used in 'surface' and
58 // 'volume' rendering situations.
59 #define SDR_NODE_CONTEXT_TOKENS \
60  ((Pattern, "pattern")) \
61  ((Surface, "surface")) \
62  ((Volume, "volume")) \
63  ((Displacement, "displacement")) \
64  ((Light, "light")) \
65  ((LightFilter, "lightFilter")) \
66  ((SampleFilter, "sampleFilter")) \
67  ((PixelFilter, "pixelFilter"))
68 
69 #define SDR_NODE_ROLE_TOKENS \
70  ((Primvar, "primvar")) \
71  ((Texture, "texture")) \
72  ((Field, "field")) \
73  ((Math, "math")) \
74 
78 
79 /// \class SdrShaderNode
80 ///
81 /// A specialized version of `NdrNode` which holds shading information.
82 ///
83 class SdrShaderNode : public NdrNode
84 {
85 public:
86  /// Constructor.
87  SDR_API
88  SdrShaderNode(const NdrIdentifier& identifier,
89  const NdrVersion& version,
90  const std::string& name,
91  const TfToken& family,
92  const TfToken& context,
93  const TfToken& sourceType,
94  const std::string& definitionURI,
95  const std::string& implementationURI,
96  NdrPropertyUniquePtrVec&& properties,
97  const NdrTokenMap& metadata = NdrTokenMap(),
98  const std::string &sourceCode = std::string());
99 
100  /// \name Inputs and Outputs
101  /// An input or output is also generically referred to as a "property".
102  /// @{
103 
104  /// Get a shader input property by name. `nullptr` is returned if an input
105  /// with the given name does not exist.
106  SDR_API
107  SdrShaderPropertyConstPtr GetShaderInput(const TfToken& inputName) const;
108 
109  /// Get a shader output property by name. `nullptr` is returned if an output
110  /// with the given name does not exist.
111  SDR_API
112  SdrShaderPropertyConstPtr GetShaderOutput(const TfToken& outputName) const;
113 
114  /// Returns the list of all inputs that are tagged as asset identifier
115  /// inputs.
116  SDR_API
118 
119  /// Returns the first shader input that is tagged as the default input.
120  /// A default input and its value can be used to acquire a fallback value
121  /// for a node when the node is considered 'disabled' or otherwise
122  /// incapable of producing an output value.
123  SDR_API
125 
126  /// @}
127 
128 
129  /// \name Metadata
130  /// The metadata returned here is a direct result of what the parser plugin
131  /// is able to determine about the node. See the documentation for a
132  /// specific parser plugin to get help on what the parser is looking for to
133  /// populate these values.
134  /// @{
135 
136  /// The label assigned to this node, if any. Distinct from the name
137  /// returned from `GetName()`. In the context of a UI, the label value
138  /// might be used as the display name for the node instead of the name.
139  SDR_API
140  const TfToken& GetLabel() const { return _label; }
141 
142  /// The category assigned to this node, if any. Distinct from the family
143  /// returned from `GetFamily()`.
144  SDR_API
145  const TfToken& GetCategory() const { return _category; }
146 
147  /// Returns the role of this node. This is used to annotate the role that
148  /// the shader node plays inside a shader network. We can tag certain
149  /// shaders to indicate their role within a shading network. We currently
150  /// tag primvar reading nodes, texture reading nodes and nodes that access
151  /// volume fields (like extinction or scattering). This is done to identify
152  /// resources used by a shading network.
153  SDR_API
154  std::string GetRole() const;
155 
156  /// The help message assigned to this node, if any.
157  SDR_API
158  std::string GetHelp() const;
159 
160  /// The departments this node is associated with, if any.
161  SDR_API
162  const NdrTokenVec& GetDepartments() const { return _departments; }
163 
164  /// Gets the pages on which the node's properties reside (an aggregate of
165  /// the unique `SdrShaderProperty::GetPage()` values for all of the node's
166  /// properties). Nodes themselves do not reside on pages. In an example
167  /// scenario, properties might be divided into two pages, 'Simple' and
168  /// 'Advanced'.
169  SDR_API
170  const NdrTokenVec& GetPages() const { return _pages; };
171 
172  /// The list of primvars this node knows it requires / uses.
173  /// For example, a shader node may require the 'normals' primvar to function
174  /// correctly. Additional, user specified primvars may have been authored on
175  /// the node. These can be queried via `GetAdditionalPrimvarProperties()`.
176  /// Together, `GetPrimvars()` and `GetAdditionalPrimvarProperties()`,
177  /// provide the complete list of primvar requirements for the node.
178  SDR_API
179  const NdrTokenVec& GetPrimvars() const { return _primvars; }
180 
181  /// The list of string input properties whose values provide the names of
182  /// additional primvars consumed by this node. For example, this may return
183  /// a token named `varname`. This indicates that the client should query the
184  /// value of a (presumed to be string-valued) input attribute named varname
185  /// from its scene description to determine the name of a primvar the
186  /// node will consume. See `GetPrimvars()` for additional information.
187  SDR_API
190  }
191 
192  /// Returns the implementation name of this node. The name of the node
193  /// is how to refer to the node in shader networks. The label is how to
194  /// present this node to users. The implementation name is the name of
195  /// the function (or something) this node represents in the
196  /// implementation. Any client using the implementation \b must call
197  /// this method to get the correct name; using \c getName() is not
198  /// correct.
199  SDR_API
201 
202  /// @}
203 
204 
205  /// \name Aggregate Information
206  /// @{
207 
208  /// Gets the names of the properties on a certain page (one that was
209  /// returned by `GetPages()`). To get properties that are not assigned to a
210  /// page, an empty string can be used for \p pageName.
211  SDR_API
212  NdrTokenVec GetPropertyNamesForPage(const std::string& pageName) const;
213 
214  /// Gets all vstructs that are present in the shader.
215  SDR_API
217 
218  /// @}
219 
220 
221  /// \cond
222  /// Hide from the API.
223 
224  // Performs a post-process on properties to determine information that can
225  // only be determined after parsing or in aggregate. Clients SHOULD NOT
226  // need to call this.
227  void _PostProcessProperties();
228 
229  /// \endcond
230 
231 protected:
232  // Processed primvar metadata. `_primvars` contains the names of primvars
233  // consumed by this node, whereas `_primvarNamingProperties` contains the
234  // names of string input properties whose values provide the names of
235  // additional primvars consumed by this node.
238 
239  // Tokenized metadata
244 
247 
248 private:
249  // Initializes `_primvars` and `_primvarNamingProperties`
250  void _InitializePrimvars();
251 
252  // Determines which pages are present on the node's properties
253  NdrTokenVec _ComputePages() const;
254 };
255 
257 
258 #endif // PXR_USD_SDR_SHADER_NODE_H
SdrPropertyMap _shaderInputs
Definition: shaderNode.h:245
SdrPropertyMap _shaderOutputs
Definition: shaderNode.h:246
NdrTokenVec _pages
Definition: shaderNode.h:243
SDR_API const TfToken & GetCategory() const
Definition: shaderNode.h:145
SDR_API const TfToken & GetLabel() const
Definition: shaderNode.h:140
std::vector< TfToken > NdrTokenVec
Definition: declare.h:59
NdrTokenVec _primvars
Definition: shaderNode.h:236
NdrTokenVec _primvarNamingProperties
Definition: shaderNode.h:237
std::unordered_map< TfToken, SdrShaderPropertyConstPtr, TfToken::HashFunctor > SdrPropertyMap
Definition: declare.h:53
TfToken _category
Definition: shaderNode.h:241
SDR_API const NdrTokenVec & GetDepartments() const
The departments this node is associated with, if any.
Definition: shaderNode.h:162
SDR_API SdrShaderPropertyConstPtr GetShaderInput(const TfToken &inputName) const
GLuint const GLchar * name
Definition: glcorearb.h:786
TfToken _label
Definition: shaderNode.h:240
std::vector< NdrPropertyUniquePtr > NdrPropertyUniquePtrVec
Definition: declare.h:67
SDR_API std::string GetHelp() const
The help message assigned to this node, if any.
SDR_API std::string GetImplementationName() const
Definition: token.h:87
SDR_API SdrShaderPropertyConstPtr GetDefaultInput() const
Definition: node.h:48
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
NdrTokenVec _departments
Definition: shaderNode.h:242
SdrShaderProperty const * SdrShaderPropertyConstPtr
Definition: declare.h:50
GT_API const UT_StringHolder version
SDR_API NdrTokenVec GetPropertyNamesForPage(const std::string &pageName) const
SDR_API NdrTokenVec GetAllVstructNames() const
Gets all vstructs that are present in the shader.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1394
#define SDR_NODE_ROLE_TOKENS
Definition: shaderNode.h:69
SDR_API const NdrTokenVec & GetPrimvars() const
Definition: shaderNode.h:179
SDR_API const NdrTokenVec & GetPages() const
Definition: shaderNode.h:170
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
SDR_API SdrShaderPropertyConstPtr GetShaderOutput(const TfToken &outputName) const
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > NdrTokenMap
Definition: declare.h:61
SDR_API const NdrTokenVec & GetAdditionalPrimvarProperties() const
Definition: shaderNode.h:188
#define SDR_NODE_CONTEXT_TOKENS
Definition: shaderNode.h:59
#define SDR_API
Definition: api.h:40
#define SDR_NODE_METADATA_TOKENS
Definition: shaderNode.h:41
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.
TF_DECLARE_PUBLIC_TOKENS(SdrNodeMetadata, SDR_API, SDR_NODE_METADATA_TOKENS)
SDR_API std::string GetRole() const
SDR_API NdrTokenVec GetAssetIdentifierInputNames() const