HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
shaderProperty.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_PROPERTY_H
26 #define PXR_USD_SDR_SHADER_PROPERTY_H
27 
28 /// \file sdr/shaderProperty.h
29 
30 #include "pxr/pxr.h"
31 #include "pxr/usd/sdr/api.h"
33 #include "pxr/base/tf/token.h"
34 #include "pxr/base/tf/weakBase.h"
35 #include "pxr/base/vt/value.h"
36 #include "pxr/usd/ndr/property.h"
37 #include "pxr/usd/sdr/shaderNode.h"
38 
40 
41 // If additional types are added here, it's also worth trying to add a mapping
42 // to the equivalent Sdf type in the implementation file.
43 #define SDR_PROPERTY_TYPE_TOKENS \
44  ((Int, "int")) \
45  ((String, "string")) \
46  ((Float, "float")) \
47  ((Color, "color")) \
48  ((Color4, "color4")) \
49  ((Point, "point")) \
50  ((Normal, "normal")) \
51  ((Vector, "vector")) \
52  ((Matrix, "matrix")) \
53  ((Struct, "struct")) \
54  ((Terminal, "terminal")) \
55  ((Vstruct, "vstruct")) \
56  ((Unknown, "unknown"))
57 
58 // Note: Metadata keys that are generated by parsers should start with
59 // "__SDR__" to reduce the risk of collision with metadata actually in the
60 // shader.
61 #define SDR_PROPERTY_METADATA_TOKENS \
62  ((Label, "label")) \
63  ((Help, "help")) \
64  ((Page, "page")) \
65  ((RenderType, "renderType")) \
66  ((Role, "role")) \
67  ((Widget, "widget")) \
68  ((Hints, "hints")) \
69  ((Options, "options")) \
70  ((IsDynamicArray, "isDynamicArray")) \
71  ((Connectable, "connectable")) \
72  ((Tag, "tag")) \
73  ((ValidConnectionTypes, "validConnectionTypes")) \
74  ((VstructMemberOf, "vstructMemberOf")) \
75  ((VstructMemberName, "vstructMemberName")) \
76  ((VstructConditionalExpr, "vstructConditionalExpr"))\
77  ((IsAssetIdentifier, "__SDR__isAssetIdentifier"))\
78  ((ImplementationName, "__SDR__implementationName"))\
79  ((SdrUsdDefinitionType, "sdrUsdDefinitionType"))\
80  ((DefaultInput, "__SDR__defaultinput")) \
81  ((Target, "__SDR__target")) \
82  ((Colorspace, "__SDR__colorspace"))
83 
84 
85 // The following tokens are valid values for the metadata "role"
86 #define SDR_PROPERTY_ROLE_TOKENS \
87  ((None, "none"))
88 
89 #define SDR_PROPERTY_TOKENS \
90  ((PageDelimiter, ":"))
91 
93 TF_DECLARE_PUBLIC_TOKENS(SdrPropertyMetadata, SDR_API,
95 TF_DECLARE_PUBLIC_TOKENS(SdrPropertyRole, SDR_API,
98 
99 /// \class SdrShaderProperty
100 ///
101 /// A specialized version of `NdrProperty` which holds shading information.
102 ///
104 {
105 public:
106  // Constructor.
107  SDR_API
109  const TfToken& name,
110  const TfToken& type,
111  const VtValue& defaultValue,
112  bool isOutput,
113  size_t arraySize,
114  const NdrTokenMap& metadata,
115  const NdrTokenMap& hints,
116  const NdrOptionVec& options
117  );
118 
119  /// \name Metadata
120  /// The metadata returned here is a direct result of what the parser plugin
121  /// is able to determine about the property. See the documentation for a
122  /// specific parser plugin to get help on what the parser is looking for to
123  /// populate these values.
124  /// @{
125 
126  /// The label assigned to this property, if any. Distinct from the name
127  /// returned from `GetName()`. In the context of a UI, the label value
128  /// might be used as the display name for the property instead of the name.
129  SDR_API
130  const TfToken& GetLabel() const { return _label; }
131 
132  /// The help message assigned to this property, if any.
133  SDR_API
134  std::string GetHelp() const;
135 
136  /// The page (group), eg "Advanced", this property appears on, if any. Note
137  /// that the page for a shader property can be nested, delimited by ":",
138  /// representing the hierarchy of sub-pages a property is defined in.
139  SDR_API
140  const TfToken& GetPage() const { return _page; }
141 
142  /// The widget "hint" that indicates the widget that can best display the
143  /// type of data contained in this property, if any. Examples of this value
144  /// could include "number", "slider", etc.
145  SDR_API
146  const TfToken& GetWidget() const { return _widget; }
147 
148  /// Any UI "hints" that are associated with this property. "Hints" are
149  /// simple key/value pairs.
150  SDR_API
151  const NdrTokenMap& GetHints() const { return _hints; }
152 
153  /// If the property has a set of valid values that are pre-determined, this
154  /// will return the valid option names and corresponding string values (if
155  /// the option was specified with a value).
156  SDR_API
157  const NdrOptionVec& GetOptions() const { return _options; }
158 
159  /// Returns the implementation name of this property. The name of the
160  /// property is how to refer to the property in shader networks. The
161  /// label is how to present this property to users. The implementation
162  /// name is the name of the parameter this property represents in the
163  /// implementation. Any client using the implementation \b must call
164  /// this method to get the correct name; using \c getName() is not
165  /// correct.
166  SDR_API
168 
169  /// @}
170 
171 
172  /// \name VStruct Information
173  /// @{
174 
175  /// If this field is part of a vstruct, this is the name of the struct.
176  SDR_API
177  const TfToken& GetVStructMemberOf() const {
178  return _vstructMemberOf;
179  }
180 
181  /// If this field is part of a vstruct, this is its name in the struct.
182  SDR_API
183  const TfToken& GetVStructMemberName() const {
184  return _vstructMemberName;
185  }
186 
187  /// Returns true if this field is part of a vstruct.
188  SDR_API
189  bool IsVStructMember() const;
190 
191  /// Returns true if the field is the head of a vstruct.
192  SDR_API
193  bool IsVStruct() const;
194 
195 
196  /// If this field is part of a vstruct, this is the conditional expression
197  SDR_API
200  }
201 
202  /// @}
203 
204 
205  /// \name Connection Information
206  /// @{
207 
208  /// Whether this property can be connected to other properties. If this
209  /// returns `true`, connectability to a specific property can be tested via
210  /// `CanConnectTo()`.
211  SDR_API
212  bool IsConnectable() const override { return _isConnectable; }
213 
214  /// Gets the list of valid connection types for this property. This value
215  /// comes from shader metadata, and may not be specified. The value from
216  /// `NdrProperty::GetType()` can be used as a fallback, or you can use the
217  /// connectability test in `CanConnectTo()`.
218  SDR_API
220  return _validConnectionTypes;
221  }
222 
223  /// Determines if this property can be connected to the specified property.
224  SDR_API
225  bool CanConnectTo(const NdrProperty& other) const override;
226 
227  /// @}
228 
229 
230  /// \name Utilities
231  /// @{
232 
233  /// Converts the property's type from `GetType()` into a `SdfValueTypeName`.
234  ///
235  /// Two scenarios can result: an exact mapping from property type to Sdf
236  /// type, and an inexact mapping. In the first scenario, the first element
237  /// in the pair will be the cleanly-mapped Sdf type, and the second element,
238  /// a TfToken, will be empty. In the second scenario, the Sdf type will be
239  /// set to `Token` to indicate an unclean mapping, and the second element
240  /// will be set to the original type returned by `GetType()`.
241  ///
242  /// \sa GetDefaultValueAsSdfType
243  SDR_API
244  const NdrSdfTypeIndicator GetTypeAsSdfType() const override;
245 
246  /// Accessor for default value corresponding to the SdfValueTypeName
247  /// returned by GetTypeAsSdfType. Note that this is different than
248  /// GetDefaultValue which returns the default value associated with the
249  /// SdrPropertyType and may differ from the SdfValueTypeName, example when
250  /// sdrUsdDefinitionType metadata is specified for a sdr property.
251  ///
252  /// \sa GetTypeAsSdfType
253  SDR_API
254  const VtValue& GetDefaultValueAsSdfType() const override {
255  return _sdfTypeDefaultValue;
256  }
257 
258  /// Determines if the value held by this property is an asset identifier
259  /// (eg, a file path); the logic for this is left up to the parser.
260  ///
261  /// Note: The type returned from `GetTypeAsSdfType()` will be `Asset` if
262  /// this method returns `true` (even though its true underlying data type
263  /// is string).
264  SDR_API
265  bool IsAssetIdentifier() const;
266 
267  /// Determines if the value held by this property is the default input
268  /// for this node.
269  SDR_API
270  bool IsDefaultInput() const;
271 
272  /// @}
273 
274 protected:
275  SdrShaderProperty& operator=(const SdrShaderProperty&) = delete;
276 
277  // Allow the shader's post process function to access the property's
278  // internals. Some property information can only be determined after parse
279  // time.
280  friend void SdrShaderNode::_PostProcessProperties();
281 
282  // Set the USD encoding version to something other than the default.
283  // This can be set in SdrShaderNode::_PostProcessProperties for all the
284  // properties on a shader node.
285  void _SetUsdEncodingVersion(int usdEncodingVersion);
286 
287  // Convert this property to a VStruct, which has a special type and a
288  // different default value
289  void _ConvertToVStruct();
290 
291  // This function is called by SdrShaderNode::_PostProcessProperties once all
292  // information is locked in and won't be changed anymore. This allows each
293  // property to take some extra steps once all information is available.
294  void _FinalizeProperty();
295 
296  // Some metadata values cannot be returned by reference from the main
297  // metadata dictionary because they need additional parsing.
300 
301  // Tokenized metadata
309 
311 
312  // Metadatum to control the behavior of GetTypeAsSdfType and indirectly
313  // CanConnectTo
315 };
316 
318 
319 #endif // PXR_USD_SDR_SHADER_PROPERTY_H
VtValue _sdfTypeDefaultValue
SDR_API const TfToken & GetLabel() const
bool _isConnectable
Definition: property.h:187
SDR_API const TfToken & GetPage() const
void _ConvertToVStruct()
std::vector< TfToken > NdrTokenVec
Definition: declare.h:59
const NdrTokenMap _hints
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
std::vector< NdrOption > NdrOptionVec
Definition: declare.h:81
SDR_API bool CanConnectTo(const NdrProperty &other) const override
Determines if this property can be connected to the specified property.
NdrTokenVec _validConnectionTypes
SDR_API SdrShaderProperty(const TfToken &name, const TfToken &type, const VtValue &defaultValue, bool isOutput, size_t arraySize, const NdrTokenMap &metadata, const NdrTokenMap &hints, const NdrOptionVec &options)
SDR_API const NdrTokenVec & GetValidConnectionTypes() const
TfToken _vstructMemberName
#define SDR_PROPERTY_TYPE_TOKENS
SDR_API const TfToken & GetVStructMemberOf() const
If this field is part of a vstruct, this is the name of the struct.
SDR_API const TfToken & GetVStructConditionalExpr() const
If this field is part of a vstruct, this is the conditional expression.
std::pair< SdfValueTypeName, TfToken > NdrSdfTypeIndicator
Definition: declare.h:83
SDR_API const TfToken & GetWidget() const
Definition: token.h:87
void _SetUsdEncodingVersion(int usdEncodingVersion)
SDR_API const TfToken & GetVStructMemberName() const
If this field is part of a vstruct, this is its name in the struct.
#define SDR_PROPERTY_TOKENS
#define SDR_PROPERTY_ROLE_TOKENS
SDR_API const NdrTokenMap & GetHints() const
GLuint const GLchar * name
Definition: glcorearb.h:786
SDR_API std::string GetImplementationName() const
SDR_API const NdrSdfTypeIndicator GetTypeAsSdfType() const override
SDR_API bool IsDefaultInput() const
SDR_API bool IsAssetIdentifier() const
const NdrOptionVec _options
#define SDR_PROPERTY_METADATA_TOKENS
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
SdrShaderProperty & operator=(const SdrShaderProperty &)=delete
SDR_API bool IsConnectable() const override
TF_DECLARE_PUBLIC_TOKENS(SdrPropertyTypes, SDR_API, SDR_PROPERTY_TYPE_TOKENS)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
SDR_API const VtValue & GetDefaultValueAsSdfType() const override
SDR_API bool IsVStructMember() const
Returns true if this field is part of a vstruct.
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > NdrTokenMap
Definition: declare.h:61
TfToken _vstructConditionalExpr
SDR_API std::string GetHelp() const
The help message assigned to this property, if any.
type
Definition: core.h:1059
SDR_API bool IsVStruct() const
Returns true if the field is the head of a vstruct.
#define SDR_API
Definition: api.h:40
Definition: value.h:167
void _FinalizeProperty()
SDR_API const NdrOptionVec & GetOptions() const