HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
shaderPropertyMetadata.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 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_METADATA_H
9 #define PXR_USD_SDR_SHADER_PROPERTY_METADATA_H
10 
11 #include "pxr/pxr.h"
13 #include "pxr/base/vt/dictionary.h"
14 #include "pxr/usd/sdr/api.h"
15 #include "pxr/usd/sdr/declare.h"
16 
18 
19 // Note: Metadata keys that are generated by parsers should start with
20 // "__SDR__" to reduce the risk of collision with metadata actually in the
21 // shader.
22 #define SDR_PROPERTY_METADATA_TOKENS \
23  ((Label, "label")) \
24  ((Help, "help")) \
25  ((Page, "page")) \
26  ((RenderType, "renderType")) \
27  ((Role, "role")) \
28  ((Widget, "widget")) \
29  ((Hints, "hints")) \
30  ((Options, "options")) \
31  ((IsDynamicArray, "isDynamicArray")) \
32  ((TupleSize, "tupleSize")) \
33  ((Connectable, "connectable")) \
34  ((Tag, "tag")) \
35  ((ShownIf, "shownIf")) \
36  ((ValidConnectionTypes, "validConnectionTypes")) \
37  ((VstructMemberOf, "vstructMemberOf")) \
38  ((VstructMemberName, "vstructMemberName")) \
39  ((VstructConditionalExpr, "vstructConditionalExpr"))\
40  ((IsAssetIdentifier, "__SDR__isAssetIdentifier"))\
41  ((ImplementationName, "__SDR__implementationName"))\
42  ((SdrUsdDefinitionType, "sdrUsdDefinitionType"))\
43  ((DefaultInput, "__SDR__defaultinput")) \
44  ((Target, "__SDR__target")) \
45  ((Colorspace, "__SDR__colorspace"))
46 
47 
48 // The following tokens are valid values for the metadata "role"
49 #define SDR_PROPERTY_ROLE_TOKENS \
50  ((None, "none"))
51 
52 #define SDR_PROPERTY_TOKENS \
53  ((PageDelimiter, ":"))
54 
55 /// SdrPropertyMetadata contains named metadata keys that should
56 /// have consistent types and semantics across all clients
57 ///
58 /// \deprecated
59 /// Certain fields in SdrPropertyMetadata are deprecated:
60 /// - SdrPropertyMetadata->Hints
61 /// - SdrPropertyMetadata->Options
62 /// - SdrPropertyMetadata->Tag
63 /// - SdrPropertyMetadata->Target
64 TF_DECLARE_PUBLIC_TOKENS(SdrPropertyMetadata, SDR_API,
66 TF_DECLARE_PUBLIC_TOKENS(SdrPropertyRole, SDR_API,
69 
70 /// SdrShaderPropertyMetadata contains generic and named metadata for
71 /// SdrShaderProperty.
72 ///
73 /// Named metadata are key-value items that are standardized and
74 /// documented for interchange. Named metadata items are defined to
75 /// have certain types that expect certain encodings.
76 /// The Has/Set/Get methods for named key-value items provide views
77 /// into the requested metadata item's unpacked VtValues.
78 ///
79 /// NOTE: This metadata is retrievable from SdrShaderProperty with
80 /// SdrShaderProperty::GetMetadataObject
82 {
83 public:
84  /// Ingest metadata from the legacy SdrTokenMap structure.
85  ///
86  /// For named metadata items, conversions to richer types from
87  /// the SdrTokenMap values are attempted.
88  SDR_API
89  SdrShaderPropertyMetadata(const SdrTokenMap& legacyMetadata);
90 
91  /// Ingest metadata from an initializer list for the legacy SdrTokenMap.
92  ///
93  /// This helps provide automatic, implicit backwards compatibility for
94  /// users that construct SdrTokenMap metadata via an initializer list
95  /// in the SdrShaderProperty constructor.
97  const std::initializer_list<std::pair<TfToken, std::string>>& init
98  ): SdrShaderPropertyMetadata(_LegacyCtorFromInitializer(init)) {}
99 
101  : _items(items) {}
102 
104  : _items(std::move(items)) {}
105 
107 
108  /// Returns whether this metadata contains an item with the given key.
109  SDR_API
110  bool HasItem(const TfToken& key) const;
111 
112  /// Set a key-value item for this metadata.
113  ///
114  /// If the key-value item already exists, it will be overwritten by the
115  /// provided arguments.
116  ///
117  /// Issues a TF_CODING_ERROR if the value is empty or if the key is in
118  /// "named metadata" and the value's contained type does not match the type
119  /// registered by the named metadata item.
120  SDR_API
121  void SetItem(const TfToken& key, const VtValue& value);
122 
123  /// \overload
124  template <typename T>
125  void SetItem(const TfToken& key, const T& value) {
126  SetItem(key, VtValue(value));
127  }
128 
129  /// Get the VtValue for the given key.
130  ///
131  /// If the value is found in the legacy SdrTokenMap metadata, the returned
132  /// value will be a VtValue holding std::string.
133  ///
134  /// If a key-value item doesn't exist for the given key in this metadata,
135  /// returns an empty VtValue.
136  SDR_API
137  VtValue GetItemValue(const TfToken& key) const;
138 
139  /// Convenience to get an item value as T.
140  ///
141  /// If any value fails conversion or doesn't exist in the metadata, a
142  /// default constructed value for T is returned.
143  template <typename T>
144  T GetItemValueAs(const TfToken& key) const {
145  const VtValue v = GetItemValue(key);
146  if (!v.IsEmpty()) {
147  const VtValue converted = VtValue::Cast<T>(v);
148  if (!converted.IsEmpty()) {
149  return converted.UncheckedGet<T>();
150  }
151  }
152 
153  return {};
154  }
155 
156  /// Clear the metadata item for the given key if it exists.
157  SDR_API
158  void ClearItem(const TfToken& key);
159 
160  /// Get all key-value items.
161  const VtDictionary& GetItems() const & { return _items; }
162 
163  /// Get all key-value items by-value.
164  VtDictionary GetItems() && { return std::move(_items); }
165 
166  /// Named metadata
167  ///
168  /// {@
169 
170  SDR_API
171  bool HasLabel() const;
172  SDR_API
173  TfToken GetLabel() const;
174  SDR_API
175  void SetLabel(const TfToken& v);
176  SDR_API
177  void ClearLabel();
178 
179  SDR_API
180  bool HasHelp() const;
181  SDR_API
182  std::string GetHelp() const;
183  SDR_API
184  void SetHelp(const std::string& v);
185  SDR_API
186  void ClearHelp();
187 
188  SDR_API
189  bool HasPage() const;
190  SDR_API
191  TfToken GetPage() const;
192  SDR_API
193  void SetPage(const TfToken& v);
194  SDR_API
195  void ClearPage();
196 
197  SDR_API
198  bool HasRenderType() const;
199  SDR_API
200  std::string GetRenderType() const;
201  SDR_API
202  void SetRenderType(const std::string& v);
203  SDR_API
204  void ClearRenderType();
205 
206  SDR_API
207  bool HasRole() const;
208  SDR_API
209  std::string GetRole() const;
210  SDR_API
211  void SetRole(const std::string& v);
212  SDR_API
213  void ClearRole();
214 
215  SDR_API
216  bool HasWidget() const;
217  SDR_API
218  TfToken GetWidget() const;
219  SDR_API
220  void SetWidget(const TfToken& v);
221  SDR_API
222  void ClearWidget();
223 
224  SDR_API
225  bool HasIsDynamicArray() const;
226  SDR_API
227  bool GetIsDynamicArray() const;
228  SDR_API
229  void SetIsDynamicArray(const bool& v);
230  SDR_API
231  void ClearIsDynamicArray();
232 
233  SDR_API
234  bool HasTupleSize() const;
235  SDR_API
236  int GetTupleSize() const;
237  SDR_API
238  void SetTupleSize(const int& v);
239  SDR_API
240  void ClearTupleSize();
241 
242  SDR_API
243  bool HasConnectable() const;
244  SDR_API
245  bool GetConnectable() const;
246  SDR_API
247  void SetConnectable(const bool& v);
248  SDR_API
249  void ClearConnectable();
250 
251  SDR_API
252  bool HasShownIf() const;
253  SDR_API
254  std::string GetShownIf() const;
255  SDR_API
256  void SetShownIf(const std::string& v);
257  SDR_API
258  void ClearShownIf();
259 
260  SDR_API
261  bool HasValidConnectionTypes() const;
262  SDR_API
264  SDR_API
266  SDR_API
268 
269  SDR_API
270  bool HasIsAssetIdentifier() const;
271  SDR_API
272  bool GetIsAssetIdentifier() const;
273  SDR_API
274  void SetIsAssetIdentifier(const bool& v);
275  SDR_API
276  void ClearIsAssetIdentifier();
277 
278  SDR_API
279  bool HasImplementationName() const;
280  SDR_API
281  std::string GetImplementationName() const;
282  SDR_API
283  void SetImplementationName(const std::string& v);
284  SDR_API
286 
287  SDR_API
288  bool HasSdrUsdDefinitionType() const;
289  SDR_API
291  SDR_API
292  void SetSdrUsdDefinitionType(const TfToken& v);
293  SDR_API
295 
296  SDR_API
297  bool HasDefaultInput() const;
298  SDR_API
299  bool GetDefaultInput() const;
300  SDR_API
301  void SetDefaultInput(const bool& v);
302  SDR_API
303  void ClearDefaultInput();
304 
305  SDR_API
306  bool HasColorspace() const;
307  SDR_API
308  TfToken GetColorspace() const;
309  SDR_API
310  void SetColorspace(const TfToken& v);
311  SDR_API
312  void ClearColorspace();
313 
314  /// @}
315 
316 private:
317  friend class SdrShaderProperty;
318 
319  // Deprecated function for legacy metadata support.
320  //
321  // Unnamed metadata with non-string values are not returned in
322  // the legacy map.
323  SdrTokenMap _EncodeLegacyMetadata() const;
324 
325  static
326  SdrTokenMap _LegacyCtorFromInitializer(
327  std::initializer_list<std::pair<TfToken, std::string>> f)
328  {
329  return SdrTokenMap(f.begin(), f.end());
330  }
331 
332  VtDictionary _items;
333 };
334 
336 
337 #endif // PXR_USD_SDR_SHADER_PROPERTY_METADATA_H
SDR_API void ClearIsAssetIdentifier()
SDR_API void ClearPage()
SDR_API std::string GetShownIf() const
SDR_API void SetDefaultInput(const bool &v)
SDR_API std::string GetImplementationName() const
SDR_API TfToken GetSdrUsdDefinitionType() const
SDR_API void ClearImplementationName()
SDR_API void SetPage(const TfToken &v)
T const & UncheckedGet() const &
Definition: value.h:1046
SDR_API bool HasValidConnectionTypes() const
SdrShaderPropertyMetadata(const std::initializer_list< std::pair< TfToken, std::string >> &init)
SDR_API void SetImplementationName(const std::string &v)
SDR_API void SetRenderType(const std::string &v)
SDR_API void ClearItem(const TfToken &key)
Clear the metadata item for the given key if it exists.
const GLdouble * v
Definition: glcorearb.h:837
SDR_API void SetRole(const std::string &v)
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
SDR_API void ClearSdrUsdDefinitionType()
SdrShaderPropertyMetadata(VtDictionary &&items)
SDR_API bool HasItem(const TfToken &key) const
Returns whether this metadata contains an item with the given key.
SDR_API bool HasRenderType() const
SDR_API void SetConnectable(const bool &v)
SDR_API void SetHelp(const std::string &v)
SDR_API void ClearValidConnectionTypes()
SDR_API std::string GetHelp() const
SDR_API void ClearTupleSize()
SdrShaderPropertyMetadata(const VtDictionary &items)
SDR_API void ClearHelp()
SDR_API bool GetConnectable() const
bool IsEmpty() const
Returns true iff this value is empty.
Definition: value.h:1227
SDR_API bool HasColorspace() const
SDR_API void SetShownIf(const std::string &v)
SDR_API bool HasConnectable() const
SDR_API bool HasLabel() const
SDR_API std::string GetRenderType() const
SDR_API void ClearLabel()
SDR_API void ClearDefaultInput()
SDR_API void ClearWidget()
const VtDictionary & GetItems() const &
Get all key-value items.
SDR_API void ClearRole()
SDR_API void SetItem(const TfToken &key, const VtValue &value)
SDR_API VtValue GetItemValue(const TfToken &key) const
SDR_API void ClearConnectable()
SDR_API bool HasRole() const
GLfloat f
Definition: glcorearb.h:1926
Definition: token.h:70
SDR_API void SetIsAssetIdentifier(const bool &v)
SDR_API void SetColorspace(const TfToken &v)
SDR_API bool HasShownIf() const
SDR_API bool GetIsAssetIdentifier() const
SDR_API void ClearRenderType()
SDR_API TfToken GetLabel() const
SDR_API void ClearColorspace()
SDR_API bool HasIsAssetIdentifier() const
SDR_API int GetTupleSize() const
SDR_API void ClearIsDynamicArray()
SDR_API bool HasPage() const
VtDictionary GetItems()&&
Get all key-value items by-value.
SDR_API TfToken GetColorspace() const
SDR_API bool HasWidget() const
#define SDR_PROPERTY_TOKENS
SDR_API void SetValidConnectionTypes(const SdrTokenVec &v)
SDR_API SdrTokenVec GetValidConnectionTypes() const
SDR_API void ClearShownIf()
T GetItemValueAs(const TfToken &key) const
SDR_API void SetLabel(const TfToken &v)
SDR_API TfToken GetPage() const
#define SDR_PROPERTY_ROLE_TOKENS
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
SDR_API bool HasIsDynamicArray() const
SDR_API TfToken GetWidget() const
#define SDR_PROPERTY_METADATA_TOKENS
SDR_API void SetWidget(const TfToken &v)
SDR_API void SetIsDynamicArray(const bool &v)
SDR_API bool GetDefaultInput() const
SDR_API bool HasHelp() const
std::vector< TfToken > SdrTokenVec
Definition: declare.h:39
SDR_API void SetSdrUsdDefinitionType(const TfToken &v)
TF_DECLARE_PUBLIC_TOKENS(SdrPropertyMetadata, SDR_API, SDR_PROPERTY_METADATA_TOKENS)
SDR_API bool GetIsDynamicArray() const
void SetItem(const TfToken &key, const T &value)
SDR_API bool HasSdrUsdDefinitionType() const
#define SDR_API
Definition: api.h:23
SDR_API bool HasTupleSize() const
Definition: value.h:89
SDR_API bool HasDefaultInput() const
SDR_API std::string GetRole() const
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > SdrTokenMap
Definition: declare.h:41
SDR_API void SetTupleSize(const int &v)
SDR_API bool HasImplementationName() const