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