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 
52 // Note: The concept of context is defined on NdrNode and can be queried with
53 // the GetContext() method. Sdr categorizes shaders by the context in which they
54 // are used inside of a renderer. For instance during 'pattern' evaluation to
55 // feed into a surface or volume shader. For BXDFs used in 'surface' and
56 // 'volume' rendering situations.
57 #define SDR_NODE_CONTEXT_TOKENS \
58  ((Pattern, "pattern")) \
59  ((Surface, "surface")) \
60  ((Volume, "volume")) \
61  ((Displacement, "displacement")) \
62  ((Light, "light")) \
63  ((LightFilter, "lightFilter")) \
64  ((SampleFilter, "sampleFilter")) \
65  ((PixelFilter, "pixelFilter"))
66 
67 #define SDR_NODE_ROLE_TOKENS \
68  ((Primvar, "primvar")) \
69  ((Texture, "texture")) \
70  ((Field, "field")) \
71  ((Math, "math")) \
72 
76 
77 /// \class SdrShaderNode
78 ///
79 /// A specialized version of `NdrNode` which holds shading information.
80 ///
81 class SdrShaderNode : public NdrNode
82 {
83 public:
84  /// Constructor.
85  SDR_API
86  SdrShaderNode(const NdrIdentifier& identifier,
87  const NdrVersion& version,
88  const std::string& name,
89  const TfToken& family,
90  const TfToken& context,
91  const TfToken& sourceType,
92  const std::string& definitionURI,
93  const std::string& implementationURI,
94  NdrPropertyUniquePtrVec&& properties,
95  const NdrTokenMap& metadata = NdrTokenMap(),
96  const std::string &sourceCode = std::string());
97 
98  /// \name Inputs and Outputs
99  /// An input or output is also generically referred to as a "property".
100  /// @{
101 
102  /// Get a shader input property by name. `nullptr` is returned if an input
103  /// with the given name does not exist.
104  SDR_API
105  SdrShaderPropertyConstPtr GetShaderInput(const TfToken& inputName) const;
106 
107  /// Get a shader output property by name. `nullptr` is returned if an output
108  /// with the given name does not exist.
109  SDR_API
110  SdrShaderPropertyConstPtr GetShaderOutput(const TfToken& outputName) const;
111 
112  /// Returns the list of all inputs that are tagged as asset identifier
113  /// inputs.
114  SDR_API
116 
117  /// Returns the first shader input that is tagged as the default input.
118  /// A default input and its value can be used to acquire a fallback value
119  /// for a node when the node is considered 'disabled' or otherwise
120  /// incapable of producing an output value.
121  SDR_API
123 
124  /// @}
125 
126 
127  /// \name Metadata
128  /// The metadata returned here is a direct result of what the parser plugin
129  /// is able to determine about the node. See the documentation for a
130  /// specific parser plugin to get help on what the parser is looking for to
131  /// populate these values.
132  /// @{
133 
134  /// The label assigned to this node, if any. Distinct from the name
135  /// returned from `GetName()`. In the context of a UI, the label value
136  /// might be used as the display name for the node instead of the name.
137  SDR_API
138  const TfToken& GetLabel() const { return _label; }
139 
140  /// The category assigned to this node, if any. Distinct from the family
141  /// returned from `GetFamily()`.
142  SDR_API
143  const TfToken& GetCategory() const { return _category; }
144 
145  /// Returns the role of this node. This is used to annotate the role that
146  /// the shader node plays inside a shader network. We can tag certain
147  /// shaders to indicate their role within a shading network. We currently
148  /// tag primvar reading nodes, texture reading nodes and nodes that access
149  /// volume fields (like extinction or scattering). This is done to identify
150  /// resources used by a shading network.
151  SDR_API
152  std::string GetRole() const;
153 
154  /// The help message assigned to this node, if any.
155  SDR_API
156  std::string GetHelp() const;
157 
158  /// The departments this node is associated with, if any.
159  SDR_API
160  const NdrTokenVec& GetDepartments() const { return _departments; }
161 
162  /// Gets the pages on which the node's properties reside (an aggregate of
163  /// the unique `SdrShaderProperty::GetPage()` values for all of the node's
164  /// properties). Nodes themselves do not reside on pages. In an example
165  /// scenario, properties might be divided into two pages, 'Simple' and
166  /// 'Advanced'.
167  SDR_API
168  const NdrTokenVec& GetPages() const { return _pages; };
169 
170  /// The list of primvars this node knows it requires / uses.
171  /// For example, a shader node may require the 'normals' primvar to function
172  /// correctly. Additional, user specified primvars may have been authored on
173  /// the node. These can be queried via `GetAdditionalPrimvarProperties()`.
174  /// Together, `GetPrimvars()` and `GetAdditionalPrimvarProperties()`,
175  /// provide the complete list of primvar requirements for the node.
176  SDR_API
177  const NdrTokenVec& GetPrimvars() const { return _primvars; }
178 
179  /// The list of string input properties whose values provide the names of
180  /// additional primvars consumed by this node. For example, this may return
181  /// a token named `varname`. This indicates that the client should query the
182  /// value of a (presumed to be string-valued) input attribute named varname
183  /// from its scene description to determine the name of a primvar the
184  /// node will consume. See `GetPrimvars()` for additional information.
185  SDR_API
188  }
189 
190  /// Returns the implementation name of this node. The name of the node
191  /// is how to refer to the node in shader networks. The label is how to
192  /// present this node to users. The implementation name is the name of
193  /// the function (or something) this node represents in the
194  /// implementation. Any client using the implementation \b must call
195  /// this method to get the correct name; using \c getName() is not
196  /// correct.
197  SDR_API
199 
200  /// @}
201 
202 
203  /// \name Aggregate Information
204  /// @{
205 
206  /// Gets the names of the properties on a certain page (one that was
207  /// returned by `GetPages()`). To get properties that are not assigned to a
208  /// page, an empty string can be used for \p pageName.
209  SDR_API
210  NdrTokenVec GetPropertyNamesForPage(const std::string& pageName) const;
211 
212  /// Gets all vstructs that are present in the shader.
213  SDR_API
215 
216  /// @}
217 
218 
219  /// \cond
220  /// Hide from the API.
221 
222  // Performs a post-process on properties to determine information that can
223  // only be determined after parsing or in aggregate. Clients SHOULD NOT
224  // need to call this.
225  void _PostProcessProperties();
226 
227  /// \endcond
228 
229 protected:
230  // Processed primvar metadata. `_primvars` contains the names of primvars
231  // consumed by this node, whereas `_primvarNamingProperties` contains the
232  // names of string input properties whose values provide the names of
233  // additional primvars consumed by this node.
236 
237  // Tokenized metadata
242 
245 
246 private:
247  // Initializes `_primvars` and `_primvarNamingProperties`
248  void _InitializePrimvars();
249 
250  // Determines which pages are present on the node's properties
251  NdrTokenVec _ComputePages() const;
252 };
253 
255 
256 #endif // PXR_USD_SDR_SHADER_NODE_H
SdrPropertyMap _shaderInputs
Definition: shaderNode.h:243
SdrPropertyMap _shaderOutputs
Definition: shaderNode.h:244
NdrTokenVec _pages
Definition: shaderNode.h:241
SDR_API const TfToken & GetCategory() const
Definition: shaderNode.h:143
SDR_API const TfToken & GetLabel() const
Definition: shaderNode.h:138
std::vector< TfToken > NdrTokenVec
Definition: declare.h:59
NdrTokenVec _primvars
Definition: shaderNode.h:234
NdrTokenVec _primvarNamingProperties
Definition: shaderNode.h:235
std::unordered_map< TfToken, SdrShaderPropertyConstPtr, TfToken::HashFunctor > SdrPropertyMap
Definition: declare.h:53
TfToken _category
Definition: shaderNode.h:239
SDR_API const NdrTokenVec & GetDepartments() const
The departments this node is associated with, if any.
Definition: shaderNode.h:160
SDR_API SdrShaderPropertyConstPtr GetShaderInput(const TfToken &inputName) const
GLuint const GLchar * name
Definition: glcorearb.h:785
TfToken _label
Definition: shaderNode.h:238
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:813
NdrTokenVec _departments
Definition: shaderNode.h:240
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:1375
#define SDR_NODE_ROLE_TOKENS
Definition: shaderNode.h:67
SDR_API const NdrTokenVec & GetPrimvars() const
Definition: shaderNode.h:177
SDR_API const NdrTokenVec & GetPages() const
Definition: shaderNode.h:168
#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:186
#define SDR_NODE_CONTEXT_TOKENS
Definition: shaderNode.h:57
#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