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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_USD_SDR_SHADER_PROPERTY_H
9 #define PXR_USD_SDR_SHADER_PROPERTY_H
10 
11 /// \file sdr/shaderProperty.h
12 
13 #include "pxr/pxr.h"
15 #include "pxr/base/tf/token.h"
16 #include "pxr/base/tf/weakBase.h"
17 #include "pxr/base/vt/value.h"
19 #include "pxr/usd/sdr/api.h"
20 #include "pxr/usd/sdr/declare.h"
22 #include "pxr/usd/sdr/shaderNode.h"
24 
26 
27 // If additional types are added here, it's also worth trying to add a mapping
28 // to the equivalent Sdf type in the implementation file.
29 #define SDR_PROPERTY_TYPE_TOKENS \
30  ((Int, "int")) \
31  ((String, "string")) \
32  ((Float, "float")) \
33  ((Color, "color")) \
34  ((Color4, "color4")) \
35  ((Point, "point")) \
36  ((Normal, "normal")) \
37  ((Vector, "vector")) \
38  ((Matrix, "matrix")) \
39  ((Struct, "struct")) \
40  ((Terminal, "terminal")) \
41  ((Vstruct, "vstruct")) \
42  ((Unknown, "unknown"))
43 
45 /// \class SdrShaderProperty
46 ///
47 /// Represents a property (input or output) that is part of a `SdrShaderNode`
48 /// instance.
49 ///
50 /// A property must have a name and type, but may also specify a host of
51 /// additional metadata. Instances can also be queried to determine if another
52 /// `SdrShaderProperty` instance can be connected to it.
53 ///
54 /// \note SdrTokenMap metadata is deprecated in favor of
55 /// SdrShaderPropertyMetadata.
57 {
58 public:
59  /// Constructor.
60  ///
61  /// \deprecated
62  /// The version of the SdrShaderProperty constructor that
63  /// takes a `SdrTokenMap` as metadata is deprecated in favor of
64  /// the constructor taking SdrShaderPropertyMetadata. Note that
65  /// SdrShaderPropertyMetadata has an implicit constructor from
66  /// the legacy `SdrTokenMap` metadata.
67  /// \sa SdrShaderPropertyMetadata::SdrShaderPropertyMetadata(const SdrTokenMap&)
68  SDR_API
70  const TfToken& name,
71  const TfToken& type,
72  const VtValue& defaultValue,
73  bool isOutput,
74  size_t arraySize,
75  const SdrShaderPropertyMetadata& metadata,
76  const SdrTokenMap& hints,
77  const SdrOptionVec& options
78  );
79 
80  /// Destructor.
81  SDR_API
82  virtual ~SdrShaderProperty();
83 
84  /// \name The Basics
85  /// @{
86 
87  /// Gets the name of the property.
88  SDR_API
89  const TfToken& GetName() const { return _name; }
90 
91  /// Gets the type of the property.
92  SDR_API
93  const TfToken& GetType() const { return _type; }
94 
95  /// Gets this property's default value associated with the type of the
96  /// property.
97  ///
98  /// \sa GetType()
99  SDR_API
100  const VtValue& GetDefaultValue() const { return _defaultValue; }
101 
102  /// Whether this property is an output.
103  SDR_API
104  bool IsOutput() const { return _isOutput; }
105 
106  /// Whether this property's type is an array type.
107  SDR_API
108  bool IsArray() const { return (_arraySize > 0) || _isDynamicArray; }
109 
110  /// Whether this property's array type is dynamically-sized.
111  SDR_API
112  bool IsDynamicArray() const { return _isDynamicArray; };
113 
114  /// Gets this property's array size.
115  ///
116  /// If this property is a fixed-size array type, the array size is returned.
117  /// In the case of a dynamically-sized array, this method returns the array
118  /// size that the parser reports, and should not be relied upon to be
119  /// accurate. A parser may report -1 for the array size, for example, to
120  /// indicate a dynamically-sized array. For types that are not a fixed-size
121  /// array or dynamic array, this returns 0.
122  SDR_API
123  int GetArraySize() const { return _arraySize; }
124 
125  /// Gets this property's tuple size.
126  ///
127  /// The tuple size indicates an array's "column count", or how many elements
128  /// it takes to form a logical row. For non-dynamic arrays, the array size
129  /// should be a multiple of the tuple size.
130  ///
131  /// If no tuple size is specified, returns 0.
132  ///
133  /// \sa GetArraySize()
134  SDR_API
135  int GetTupleSize() const { return _tupleSize; }
136 
137  /// Gets a string with basic information about this property. Helpful for
138  /// things like adding this property to a log.
139  SDR_API
140  std::string GetInfoString() const;
141 
142  /// @}
143 
144  /// \name Metadata
145  /// The metadata returned here is a direct result of what the parser plugin
146  /// is able to determine about the property. See the documentation for a
147  /// specific parser plugin to get help on what the parser is looking for to
148  /// populate these values.
149  /// @{
150 
151  /// All of the metadata that came from the parse process.
152  ///
153  /// \deprecated in favor of GetMetadataObject
154  ///
155  /// If this property was initialized with the upgraded
156  /// SdrShaderPropertyMetadata class, the legacy metadata returned by this
157  /// function will contain only string representations of named metadata,
158  /// and any other metadata items whose value holds a string type in
159  /// SdrShaderNodeMetadata. Other metadata items may be omitted.
160  const SdrTokenMap& GetMetadata() const { return _legacyMetadata; }
161 
162  /// All of the metadata that came from the parse process.
164  return _metadata;
165  }
166 
167  /// The label assigned to this property, if any. Distinct from the name
168  /// returned from `GetName()`. In the context of a UI, the label value
169  /// might be used as the display name for the property instead of the name.
170  SDR_API
171  const TfToken& GetLabel() const { return _label; }
172 
173  /// The help message assigned to this property, if any.
174  SDR_API
175  std::string GetHelp() const;
176 
177  /// The page (group), eg "Advanced", this property appears on, if any. Note
178  /// that the page for a shader property can be nested, delimited by ":",
179  /// representing the hierarchy of sub-pages a property is defined in.
180  SDR_API
181  const TfToken& GetPage() const { return _page; }
182 
183  /// The widget "hint" that indicates the widget that can best display the
184  /// type of data contained in this property, if any. Examples of this value
185  /// could include "number", "slider", etc.
186  SDR_API
187  const TfToken& GetWidget() const { return _widget; }
188 
189  /// Any UI "hints" that are associated with this property. "Hints" are
190  /// simple key/value pairs.
191  SDR_API
192  const SdrTokenMap& GetHints() const { return _hints; }
193 
194  /// If the property has a set of valid values that are pre-determined, this
195  /// will return the valid option names and corresponding string values (if
196  /// the option was specified with a value).
197  SDR_API
198  const SdrOptionVec& GetOptions() const { return _options; }
199 
200  /// Returns the implementation name of this property. The name of the
201  /// property is how to refer to the property in shader networks. The
202  /// label is how to present this property to users. The implementation
203  /// name is the name of the parameter this property represents in the
204  /// implementation. Any client using the implementation \b must call
205  /// this method to get the correct name; using \c getName() is not
206  /// correct.
207  SDR_API
208  std::string GetImplementationName() const;
209 
210  /// A boolean expression (\c SdfBooleanExpression) that determines if the
211  /// property should be shown in the UI based on the state of other
212  /// properties of the same node.
213  ///
214  /// If an expression is not provided for `SdrPropertyMetadata->ShownIf` and
215  /// the property instead contains conditional visibility metadata expressed
216  /// in the style of Katana "args" files, an attempt will be made to convert
217  /// the condition into an SdfBooleanExpression-style boolean expression.
218  SDR_API
219  std::string GetShownIf() const;
220 
221  /// @}
222 
223 
224  /// \name VStruct Information
225  /// @{
226 
227  /// If this field is part of a vstruct, this is the name of the struct.
228  SDR_API
229  const TfToken& GetVStructMemberOf() const {
230  return _vstructMemberOf;
231  }
232 
233  /// If this field is part of a vstruct, this is its name in the struct.
234  SDR_API
235  const TfToken& GetVStructMemberName() const {
236  return _vstructMemberName;
237  }
238 
239  /// Returns true if this field is part of a vstruct.
240  SDR_API
241  bool IsVStructMember() const;
242 
243  /// Returns true if the field is the head of a vstruct.
244  SDR_API
245  bool IsVStruct() const;
246 
247 
248  /// If this field is part of a vstruct, this is the conditional expression
249  SDR_API
252  }
253 
254  /// @}
255 
256 
257  /// \name Connection Information
258  /// @{
259 
260  /// Whether this property can be connected to other properties. If this
261  /// returns `true`, connectability to a specific property can be tested via
262  /// `CanConnectTo()`.
263  ///
264  /// Properties are by default connectable -- note that when
265  /// SdrShaderPropertyMetadata::HasConnectable is false, this method
266  /// SdrShaderProperty::IsConnectable will return true.
267  SDR_API
268  bool IsConnectable() const { return _isConnectable; }
269 
270  /// Gets the list of valid connection types for this property. This value
271  /// comes from shader metadata, and may not be specified. The value from
272  /// `SdrShaderProperty::GetType()` can be used as a fallback, or you can
273  /// use the connectability test in `CanConnectTo()`.
274  SDR_API
276  return _validConnectionTypes;
277  }
278 
279  /// Determines if this property can be connected to the specified property.
280  SDR_API
281  bool CanConnectTo(const SdrShaderProperty& other) const;
282 
283  /// @}
284 
285 
286  /// \name Utilities
287  /// @{
288 
289  /// Converts the property's type from `GetType()` into a
290  /// `SdrSdfTypeIndicator`.
291  ///
292  /// Two scenarios can result: an exact mapping from property type to Sdf
293  /// type, and an inexact mapping. In the first scenario,
294  /// SdrSdfTypeIndicator will contain a cleanly-mapped Sdf type. In the
295  /// second scenario, the SdrSdfTypeIndicator will contain an Sdf type
296  /// set to `Token` to indicate an unclean mapping, and
297  /// SdrSdfTypeIndicator::GetSdrType will be set to the original type
298  /// returned by `GetType()`.
299  ///
300  /// \sa GetDefaultValueAsSdfType()
301  SDR_API
303 
304  /// Accessor for default value corresponding to the SdfValueTypeName
305  /// returned by GetTypeAsSdfType. Note that this is different than
306  /// GetDefaultValue which returns the default value associated with the
307  /// SdrPropertyType and may differ from the SdfValueTypeName, example when
308  /// sdrUsdDefinitionType metadata is specified for a sdr property.
309  ///
310  /// \sa GetTypeAsSdfType
311  SDR_API
313  return _sdfTypeDefaultValue;
314  }
315 
316  /// Determines if the value held by this property is an asset identifier
317  /// (eg, a file path); the logic for this is left up to the parser.
318  ///
319  /// Note: The type returned from `GetTypeAsSdfType()` will be `Asset` if
320  /// this method returns `true` (even though its true underlying data type
321  /// is string).
322  SDR_API
323  bool IsAssetIdentifier() const;
324 
325  /// Determines if the value held by this property is the default input
326  /// for this node.
327  SDR_API
328  bool IsDefaultInput() const;
329 
330  /// @}
331 
332 protected:
333  SdrShaderProperty& operator=(const SdrShaderProperty&) = delete;
334 
335  // Allow the shader's post process function to access the property's
336  // internals. Some property information can only be determined after parse
337  // time.
338  friend void SdrShaderNode::_PostProcessProperties();
339 
340  // Set the USD encoding version to something other than the default.
341  // This can be set in SdrShaderNode::_PostProcessProperties for all the
342  // properties on a shader node.
343  void _SetUsdEncodingVersion(int usdEncodingVersion);
344 
345  // Convert this property to a VStruct, which has a special type and a
346  // different default value
347  void _ConvertToVStruct();
348 
349  // If a shownIf expression is not provided, attempt to synthesize one from
350  // other conditional visibility metadata.
351  void _ConvertExpressions(const SdrShaderPropertyUniquePtrVec& properties,
353 
354  // This function is called by SdrShaderNode::_PostProcessProperties once all
355  // information is locked in and won't be changed anymore. This allows each
356  // property to take some extra steps once all information is available.
357  void _FinalizeProperty();
358 
362  bool _isOutput;
363  size_t _arraySize;
364  size_t _tupleSize;
369 
370  // Some metadata values cannot be returned by reference from the main
371  // metadata dictionary because they need additional parsing.
374 
375  // Tokenized metadata
383 
385 
386  // Metadatum to control the behavior of GetTypeAsSdfType and indirectly
387  // CanConnectTo
389 };
390 
392 
393 #endif // PXR_USD_SDR_SHADER_PROPERTY_H
SdrShaderPropertyMetadata _metadata
SDR_API bool IsDynamicArray() const
Whether this property's array type is dynamically-sized.
VtValue _sdfTypeDefaultValue
SDR_API const TfToken & GetLabel() const
SDR_API const TfToken & GetPage() const
void _ConvertToVStruct()
SDR_API const VtValue & GetDefaultValueAsSdfType() const
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
SdrShaderNode const * SdrShaderNodeConstPtr
Definition: declare.h:45
SDR_API SdrSdfTypeIndicator GetTypeAsSdfType() const
const SdrTokenMap & GetMetadata() const
TfToken _vstructMemberName
#define SDR_PROPERTY_TYPE_TOKENS
SDR_API int GetTupleSize() const
SDR_API SdrShaderProperty(const TfToken &name, const TfToken &type, const VtValue &defaultValue, bool isOutput, size_t arraySize, const SdrShaderPropertyMetadata &metadata, const SdrTokenMap &hints, const SdrOptionVec &options)
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.
SDR_API const TfToken & GetWidget() const
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
Definition: token.h:70
SdrTokenVec _validConnectionTypes
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.
SDR_API int GetArraySize() const
SDR_API const TfToken & GetType() const
Gets the type of the property.
SDR_API bool IsOutput() const
Whether this property is an output.
SDR_API bool IsArray() const
Whether this property's type is an array type.
SDR_API const SdrOptionVec & GetOptions() const
virtual SDR_API ~SdrShaderProperty()
Destructor.
GLuint const GLchar * name
Definition: glcorearb.h:786
SDR_API std::string GetShownIf() const
SDR_API std::string GetImplementationName() const
const SdrOptionVec _options
const SdrTokenMap _hints
SDR_API bool CanConnectTo(const SdrShaderProperty &other) const
Determines if this property can be connected to the specified property.
SDR_API bool IsDefaultInput() const
SDR_API bool IsAssetIdentifier() const
GLuint shader
Definition: glcorearb.h:785
SdrShaderProperty & operator=(const SdrShaderProperty &)=delete
TF_DECLARE_PUBLIC_TOKENS(SdrPropertyTypes, SDR_API, SDR_PROPERTY_TYPE_TOKENS)
SdrTokenMap _legacyMetadata
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
void _ConvertExpressions(const SdrShaderPropertyUniquePtrVec &properties, SdrShaderNodeConstPtr shader)
SDR_API const VtValue & GetDefaultValue() const
SDR_API const SdrTokenMap & GetHints() const
SDR_API bool IsVStructMember() const
Returns true if this field is part of a vstruct.
TfToken _vstructConditionalExpr
SDR_API std::string GetHelp() const
The help message assigned to this property, if any.
std::vector< TfToken > SdrTokenVec
Definition: declare.h:39
SDR_API bool IsConnectable() const
SDR_API const TfToken & GetName() const
Gets the name of the property.
SDR_API bool IsVStruct() const
Returns true if the field is the head of a vstruct.
#define SDR_API
Definition: api.h:23
std::vector< SdrOption > SdrOptionVec
Definition: declare.h:63
Definition: value.h:89
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > SdrTokenMap
Definition: declare.h:41
void _FinalizeProperty()
SDR_API const SdrTokenVec & GetValidConnectionTypes() const
SDR_API std::string GetInfoString() const
std::vector< SdrShaderPropertyUniquePtr > SdrShaderPropertyUniquePtrVec
Definition: declare.h:55
const SdrShaderPropertyMetadata & GetMetadataObject() const
All of the metadata that came from the parse process.