shaderNodeMetadata.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_NODE_METADATA_H
9 #define PXR_USD_SDR_SHADER_NODE_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 //
23 // Enum names should correlate directly to named API e.g. "GetLabel" or
24 // "GetSdrUsdEncodingVersion"
25 #define SDR_NODE_METADATA_TOKENS \
26  ((Category, "category")) /* deprecated */ \
27  ((Domain, "domain")) \
28  ((Subdomain, "subdomain")) \
29  ((Context, "context")) \
30  ((Role, "role")) \
31  ((TargetRenderer, "targetRenderer")) \
32  ((Collections, "collections")) \
33  ((Departments, "departments")) /* deprecated */ \
34  ((Help, "help")) \
35  ((Label, "label")) \
36  ((Pages, "pages")) /* deprecated */ \
37  ((OpenPages, "openPages")) \
38  ((PagesShownIf, "pagesShownIf")) \
39  ((Primvars, "primvars")) \
40  ((ImplementationName, "__SDR__implementationName")) \
41  ((Target, "__SDR__target")) /* deprecated */ \
42  ((SdrUsdEncodingVersion, "sdrUsdEncodingVersion")) \
43  ((SdrDefinitionNameFallbackPrefix, "sdrDefinitionNameFallbackPrefix"))
44 
45 #define SDR_NODE_DOMAIN_TOKENS \
46  ((Rendering, "rendering")) \
47  ((General, "general"))
48 
49 #define SDR_NODE_SUBDOMAIN_TOKENS \
50  /* rendering domain */ \
51  ((Shading, "shading")) \
52  ((Filtering, "filtering")) \
53  ((Lighting, "lighting")) \
54  ((Rendering, "rendering"))
55 
56 #define SDR_NODE_CONTEXT_TOKENS \
57  /* shading subdomain */ \
58  ((Pattern, "pattern")) \
59  ((Surface, "surface")) \
60  ((Volume, "volume")) \
61  ((Displacement, "displacement")) \
62  /* lighting subdomain */ \
63  ((Light, "light")) \
64  ((LightFilter, "lightFilter")) \
65  /* filtering subdomain*/ \
66  ((DisplayFilter, "displayFilter")) \
67  ((PixelFilter, "pixelFilter")) \
68  ((SampleFilter, "sampleFilter")) \
69  ((VolumeFilter, "volumeFilter")) \
70  ((EnergyFilter, "energyFilter")) \
71  /* rendering subdomain */ \
72  ((Integrator, "integrator")) \
73  ((Projection, "projection"))
74 
75 #define SDR_NODE_ROLE_TOKENS \
76  ((Primvar, "primvar")) \
77  ((Texture, "texture")) \
78  ((Field, "field")) \
79  ((Math, "math"))
80 
81 
89 TF_DECLARE_PUBLIC_TOKENS(SdrNodeMetadata, SDR_API, SDR_NODE_METADATA_TOKENS);
90 TF_DECLARE_PUBLIC_TOKENS(SdrNodeDomain, SDR_API, SDR_NODE_DOMAIN_TOKENS);
91 TF_DECLARE_PUBLIC_TOKENS(SdrNodeSubdomain, SDR_API, SDR_NODE_SUBDOMAIN_TOKENS);
92 TF_DECLARE_PUBLIC_TOKENS(SdrNodeContext, SDR_API, SDR_NODE_CONTEXT_TOKENS);
93 TF_DECLARE_PUBLIC_TOKENS(SdrNodeRole, SDR_API, SDR_NODE_ROLE_TOKENS);
94 
95 
108 {
109 public:
110  SDR_API
112 
117  SDR_API
118  SdrShaderNodeMetadata(const SdrTokenMap& legacyMetadata);
119 
126  const std::initializer_list<std::pair<TfToken, std::string>>& init
127  ): SdrShaderNodeMetadata(_LegacyCtorFromInitializer(init)) {}
128 
129  SDR_API
130  explicit SdrShaderNodeMetadata(const VtDictionary& items);
131 
133  SDR_API
134  bool HasItem(const TfToken& key) const;
135 
147  SDR_API
148  void SetItem(const TfToken& key, const VtValue& value);
149 
151  template <typename T>
152  void SetItem(const TfToken& key, const T& value) {
153  SetItem(key, VtValue(value));
154  }
155 
163  SDR_API
164  VtValue GetItemValue(const TfToken& key) const;
165 
170  template <typename T>
171  T GetItemValueAs(const TfToken& key) const {
172  const VtValue v = GetItemValue(key);
173  if (!v.IsEmpty()) {
174  const VtValue converted = VtValue::Cast<T>(v);
175  if (!converted.IsEmpty()) {
176  return converted.UncheckedGet<T>();
177  }
178  }
179 
180  return {};
181  }
182 
184  SDR_API
185  void ClearItem(const TfToken& key);
186 
188  const VtDictionary& GetItems() const & { return _items; }
189 
191  VtDictionary GetItems() && { return std::move(_items); }
192 
203 
206  SDR_API
207  static VtValue GetDefaultValue(const TfToken& key);
208 
213  SDR_API
214  static const VtDictionary& GetDefaultValues();
216 
220 
221  SDR_API
222  bool HasLabel() const;
223  SDR_API
224  TfToken GetLabel() const;
225  SDR_API
226  void SetLabel(const TfToken& v);
227  SDR_API
228  void ClearLabel();
229 
236 
238  SDR_API
239  bool HasCategory() const;
241  SDR_API
242  TfToken GetCategory() const;
244  SDR_API
245  void SetCategory(const TfToken& v);
247  SDR_API
248  void ClearCategory();
250 
261  SDR_API
262  bool HasDomain() const;
263  SDR_API
264  TfToken GetDomain() const;
265  SDR_API
266  void SetDomain(const TfToken& v);
267  SDR_API
268  void ClearDomain();
270 
277  SDR_API
278  bool HasSubdomain() const;
279  SDR_API
280  TfToken GetSubdomain() const;
281  SDR_API
282  void SetSubdomain(const TfToken& v);
283  SDR_API
284  void ClearSubdomain();
286 
293  SDR_API
294  bool HasContext() const;
295  SDR_API
296  TfToken GetContext() const;
297  SDR_API
298  void SetContext(const TfToken& v);
299  SDR_API
300  void ClearContext();
302 
307  SDR_API
308  bool HasRole() const;
309  SDR_API
310  TfToken GetRole() const;
311  SDR_API
312  void SetRole(const TfToken& v);
313  SDR_API
314  void ClearRole();
316 
331  SDR_API
332  bool HasTargetRenderer() const;
333  SDR_API
334  TfToken GetTargetRenderer() const;
335  SDR_API
336  void SetTargetRenderer(const TfToken& v);
337  SDR_API
338  void ClearTargetRenderer();
340 
351  SDR_API
352  bool HasCollections() const;
353  SDR_API
354  SdrTokenVec GetCollections() const;
355  SDR_API
356  void SetCollections(const SdrTokenVec& v);
357  SDR_API
358  void ClearCollections();
360 
361  SDR_API
362  bool HasHelp() const;
363  SDR_API
364  std::string GetHelp() const;
365  SDR_API
366  void SetHelp(const std::string& v);
367  SDR_API
368  void ClearHelp();
369 
376 
378  SDR_API
379  bool HasDepartments() const;
381  SDR_API
382  SdrTokenVec GetDepartments() const;
384  SDR_API
385  void SetDepartments(const SdrTokenVec& v);
387  SDR_API
388  void ClearDepartments();
390 
399  SDR_API
400  bool HasPages() const;
402  SDR_API
403  SdrTokenVec GetPages() const;
405  SDR_API
406  void SetPages(const SdrTokenVec& v);
408  SDR_API
409  void ClearPages();
411 
412  SDR_API
413  bool HasOpenPages() const;
414  SDR_API
415  SdrTokenVec GetOpenPages() const;
416  SDR_API
417  void SetOpenPages(const SdrTokenVec& v);
418  SDR_API
419  void ClearOpenPages();
420 
431  SDR_API
432  bool HasPagesShownIf() const;
433  SDR_API
434  SdrTokenMap GetPagesShownIf() const;
435  SDR_API
436  void SetPagesShownIf(const SdrTokenMap& v);
437  SDR_API
438  void ClearPagesShownIf();
440 
441  SDR_API
442  bool HasPrimvars() const;
443  SDR_API
444  SdrStringVec GetPrimvars() const;
445  SDR_API
446  void SetPrimvars(const SdrStringVec& v);
447  SDR_API
448  void ClearPrimvars();
449 
450  SDR_API
451  bool HasImplementationName() const;
452  SDR_API
453  std::string GetImplementationName() const;
454  SDR_API
455  void SetImplementationName(const std::string& v);
456  SDR_API
457  void ClearImplementationName();
458 
459  SDR_API
460  bool HasSdrUsdEncodingVersion() const;
461  SDR_API
462  int GetSdrUsdEncodingVersion() const;
463  SDR_API
464  void SetSdrUsdEncodingVersion(const int& v);
465  SDR_API
466  void ClearSdrUsdEncodingVersion();
467 
468  SDR_API
469  bool HasSdrDefinitionNameFallbackPrefix() const;
470  SDR_API
471  std::string GetSdrDefinitionNameFallbackPrefix() const;
472  SDR_API
473  void SetSdrDefinitionNameFallbackPrefix(const std::string& v);
474  SDR_API
475  void ClearSdrDefinitionNameFallbackPrefix();
476 
478 
479 private:
480  friend class SdrShaderNode;
481 
482  // Deprecated function for legacy metadata support.
483  //
484  // Unnamed metadata with non-string values are not returned in
485  // the legacy map.
486  SdrTokenMap _EncodeLegacyMetadata() const;
487 
488  static
489  SdrTokenMap _LegacyCtorFromInitializer(
490  std::initializer_list<std::pair<TfToken, std::string>> f)
491  {
492  return SdrTokenMap(f.begin(), f.end());
493  }
494 
495  // This method sets metadata items that have default values
496  // as indicated by GetDefaultValues.
497  //
498  // Note: This method is invoked at the ends of this class's
499  // constructors.
500  void _SetDefaultInitializations();
501 
502  VtDictionary _items;
503 };
504 
505 PXR_NAMESPACE_CLOSE_SCOPE
506 
507 #endif // PXR_USD_SDR_SHADER_NODE_METADATA_H
SDR_API SdrTokenVec GetPages() const
void SetItem(const TfToken &key, const T &value)
SDR_API bool HasLabel() const
Named metadata.
SDR_API bool HasDepartments() const
T GetItemValueAs(const TfToken &key) const
Convenience to get an item value as T.
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 void ClearDepartments()
SDR_API TfToken GetCategory() const
A map with string keys and VtValue values.
Definition: dictionary.h:52
SdrShaderNodeMetadata(const std::initializer_list< std::pair< TfToken, std::string >> &init)
Ingest metadata from an initializer list for the legacy SdrTokenMap.
SDR_API void ClearPages()
VtDictionary GetItems() &&
Get all key-value items by-value.
SDR_API void ClearCategory()
SDR_API bool HasCategory() const
SDR_API VtValue GetItemValue(const TfToken &key) const
Get the VtValue for the given key.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:80
SDR_API void SetItem(const TfToken &key, const VtValue &value)
Set a key-value item for this metadata.
SDR_API void ClearItem(const TfToken &key)
Clear the metadata item for the given key if it exists.
SDR_API SdrTokenVec GetDepartments() const
bool IsEmpty() const
Returns true iff this value is empty.
Definition: value.h:1286
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:92
SdrShaderNodeMetadata contains generic and named metadata for SdrShaderNode.
SDR_API void SetCategory(const TfToken &v)
static SDR_API VtValue GetDefaultValue(const TfToken &key)
This method returns a non-empty VtValue if and only if the key represents a metadata item with a defa...
Represents a node that holds shading information.
Definition: shaderNode.h:44
const VtDictionary & GetItems() const &
Get all key-value items.
This file defines some macros that are useful for declaring and using static TfTokens.
SDR_API bool HasItem(const TfToken &key) const
Returns whether this metadata contains an item with the given key.
SDR_API void SetDepartments(const SdrTokenVec &v)
SDR_API bool HasPages() const
SDR_API void SetPages(const SdrTokenVec &v)
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:89
static SDR_API const VtDictionary & GetDefaultValues()
Returns all metadata items that have default values.