shaderPropertyMetadata.h
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 
17 PXR_NAMESPACE_OPEN_SCOPE
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 
64 TF_DECLARE_PUBLIC_TOKENS(SdrPropertyMetadata, SDR_API,
65  SDR_PROPERTY_METADATA_TOKENS);
66 TF_DECLARE_PUBLIC_TOKENS(SdrPropertyRole, SDR_API,
67  SDR_PROPERTY_ROLE_TOKENS);
68 TF_DECLARE_PUBLIC_TOKENS(SdrPropertyTokens, SDR_API, SDR_PROPERTY_TOKENS);
69 
82 {
83 public:
88  SDR_API
89  SdrShaderPropertyMetadata(const SdrTokenMap& legacyMetadata);
90 
97  const std::initializer_list<std::pair<TfToken, std::string>>& init
98  ): SdrShaderPropertyMetadata(_LegacyCtorFromInitializer(init)) {}
99 
100  explicit SdrShaderPropertyMetadata(const VtDictionary& items)
101  : _items(items) {}
102 
103  explicit SdrShaderPropertyMetadata(VtDictionary&& items)
104  : _items(std::move(items)) {}
105 
107 
109  SDR_API
110  bool HasItem(const TfToken& key) const;
111 
120  SDR_API
121  void SetItem(const TfToken& key, const VtValue& value);
122 
124  template <typename T>
125  void SetItem(const TfToken& key, const T& value) {
126  SetItem(key, VtValue(value));
127  }
128 
136  SDR_API
137  VtValue GetItemValue(const TfToken& key) const;
138 
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 
157  SDR_API
158  void ClearItem(const TfToken& key);
159 
161  const VtDictionary& GetItems() const & { return _items; }
162 
164  VtDictionary GetItems() && { return std::move(_items); }
165 
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
263  SdrTokenVec GetValidConnectionTypes() const;
264  SDR_API
265  void SetValidConnectionTypes(const SdrTokenVec& v);
266  SDR_API
267  void ClearValidConnectionTypes();
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
285  void ClearImplementationName();
286 
287  SDR_API
288  bool HasSdrUsdDefinitionType() const;
289  SDR_API
290  TfToken GetSdrUsdDefinitionType() const;
291  SDR_API
292  void SetSdrUsdDefinitionType(const TfToken& v);
293  SDR_API
294  void ClearSdrUsdDefinitionType();
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 
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 
335 PXR_NAMESPACE_CLOSE_SCOPE
336 
337 #endif // PXR_USD_SDR_SHADER_PROPERTY_METADATA_H
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1105
SDR_API VtValue GetItemValue(const TfToken &key) const
Get the VtValue for the given key.
A map with string keys and VtValue values.
Definition: dictionary.h:52
Represents a property (input or output) that is part of a SdrShaderNode instance.
SdrShaderPropertyMetadata(const std::initializer_list< std::pair< TfToken, std::string >> &init)
Ingest metadata from an initializer list for the legacy SdrTokenMap.
SDR_API void ClearItem(const TfToken &key)
Clear the metadata item for the given key if it exists.
const VtDictionary & GetItems() const &
Get all key-value items.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:80
bool IsEmpty() const
Returns true iff this value is empty.
Definition: value.h:1286
SDR_API bool HasLabel() const
Named metadata.
void SetItem(const TfToken &key, const T &value)
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:92
SdrShaderPropertyMetadata contains generic and named metadata for SdrShaderProperty.
VtDictionary GetItems() &&
Get all key-value items by-value.
SDR_API bool HasItem(const TfToken &key) const
Returns whether this metadata contains an item with the given key.
SDR_API void SetItem(const TfToken &key, const VtValue &value)
Set a key-value item for this metadata.
This file defines some macros that are useful for declaring and using static TfTokens.
T GetItemValueAs(const TfToken &key) const
Convenience to get an item value as T.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:89