HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
shaderNodeMetadata.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_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 
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  /* rendering subdomain */ \
71  ((Integrator, "integrator")) \
72  ((Projection, "projection"))
73 
74 #define SDR_NODE_ROLE_TOKENS \
75  ((Primvar, "primvar")) \
76  ((Texture, "texture")) \
77  ((Field, "field")) \
78  ((Math, "math"))
79 
80 
81 /// \deprecated
82 /// SdrNodeMetadata->Category, SdrNodeMetadata->Departments,
83 /// SdrNodeMetadata->Pages, SdrNodeMetadata->Target are deprecated.
84 ///
85 /// All other tokens will remain intact. Note that clients can still
86 /// set metadata with keys such as "category" and "departments" -- the
87 /// convenience API is simply removed.
93 
94 
95 /// SdrShaderNodeMetadata contains generic and named metadata for
96 /// SdrShaderNode.
97 ///
98 /// Named metadata are key-value items that are standardized and
99 /// documented for interchange. Named metadata items are defined to
100 /// have certain types that expect certain encodings.
101 /// The Has/Set/Get methods for named key-value items provide views
102 /// into the requested metadata item's unpacked VtValues.
103 ///
104 /// NOTE: This metadata is retrievable from SdrShaderNode with
105 /// SdrShaderNode::GetMetadataObject
107 {
108 public:
109  SDR_API
111 
112  /// Ingest metadata from the legacy SdrTokenMap structure.
113  ///
114  /// For named metadata items, conversions to richer types from
115  /// the SdrTokenMap values are attempted.
116  SDR_API
117  SdrShaderNodeMetadata(const SdrTokenMap& legacyMetadata);
118 
119  /// Ingest metadata from an initializer list for the legacy SdrTokenMap.
120  ///
121  /// This helps provide automatic, implicit backwards compatibility for
122  /// users that construct SdrTokenMap metadata via an initializer list
123  /// in the SdrShaderNode constructor.
125  const std::initializer_list<std::pair<TfToken, std::string>>& init
126  ): SdrShaderNodeMetadata(_LegacyCtorFromInitializer(init)) {}
127 
128  SDR_API
129  explicit SdrShaderNodeMetadata(const VtDictionary& items);
130 
131  /// Returns whether this metadata contains an item with the given key.
132  SDR_API
133  bool HasItem(const TfToken& key) const;
134 
135  /// Set a key-value item for this metadata.
136  ///
137  /// If the key-value item already exists, it will be overwritten by the
138  /// provided arguments.
139  ///
140  /// Clears the item if given an empty VtValue.
141  ///
142  /// Issues a TF_CODING_ERROR if the key is in "named metadata" and
143  /// the value's contained type does not match the type
144  /// registered by the named metadata item. In this case, no action
145  /// is taken to set the item.
146  SDR_API
147  void SetItem(const TfToken& key, const VtValue& value);
148 
149  /// \overload
150  template <typename T>
151  void SetItem(const TfToken& key, const T& value) {
152  SetItem(key, VtValue(value));
153  }
154 
155  /// Get the VtValue for the given key.
156  ///
157  /// If the value is found in the legacy SdrTokenMap metadata, the returned
158  /// value will be a VtValue holding std::string.
159  ///
160  /// If a key-value item doesn't exist for the given key in this metadata,
161  /// returns an empty VtValue.
162  SDR_API
163  VtValue GetItemValue(const TfToken& key) const;
164 
165  /// Convenience to get an item value as T.
166  ///
167  /// If any value fails conversion or doesn't exist in the metadata, a
168  /// default constructed value for T is returned.
169  template <typename T>
170  T GetItemValueAs(const TfToken& key) const {
171  const VtValue v = GetItemValue(key);
172  if (!v.IsEmpty()) {
173  const VtValue converted = VtValue::Cast<T>(v);
174  if (!converted.IsEmpty()) {
175  return converted.UncheckedGet<T>();
176  }
177  }
178 
179  return {};
180  }
181 
182  /// Clear the metadata item for the given key if it exists.
183  SDR_API
184  void ClearItem(const TfToken& key);
185 
186  /// Get all key-value items.
187  const VtDictionary& GetItems() const & { return _items; }
188 
189  /// Get all key-value items by-value.
190  VtDictionary GetItems() && { return std::move(_items); }
191 
192  /// \name Default values
193  /// Default values are populated to SdrShaderNodeMetadata at
194  /// construction time, if the corresponding metadata items are
195  /// not already specified by the ingested dictionary.
196  ///
197  /// Note that metadata items with default values can still
198  /// be modified post-construction, and that clearing
199  /// those items will remove them from the metadata object
200  /// rather than resetting them to their default value.
201  /// {@
202 
203  /// This method returns a non-empty VtValue if and only if the
204  /// key represents a metadata item with a default value.
205  SDR_API
206  static VtValue GetDefaultValue(const TfToken& key);
207 
208  /// Returns all metadata items that have default values.
209  ///
210  /// The result is a dictionary with metadata keys and their
211  /// associated default values.
212  SDR_API
213  static const VtDictionary& GetDefaultValues();
214  /// @}
215 
216  /// Named metadata
217  ///
218  /// {@
219 
220  SDR_API
221  bool HasLabel() const;
222  SDR_API
223  TfToken GetLabel() const;
224  SDR_API
225  void SetLabel(const TfToken& v);
226  SDR_API
227  void ClearLabel();
228 
229  /// \name Category
230  ///
231  /// Deprecated metadata item.
232  ///
233  /// \deprecated
234  /// {@
235 
236  /// \deprecated
237  SDR_API
238  bool HasCategory() const;
239  /// \deprecated
240  SDR_API
241  TfToken GetCategory() const;
242  /// \deprecated
243  SDR_API
244  void SetCategory(const TfToken& v);
245  /// \deprecated
246  SDR_API
247  void ClearCategory();
248  /// @}
249 
250  /// \name Domain
251  ///
252  /// Domain defines groups for the broadest system categories.
253  ///
254  /// Domain has a default value of "rendering". This means that
255  /// at SdrShaderNodeMetadata initialization time, unspecified
256  /// domains will be initialized to SdrNodeDomain->Rendering.
257  ///
258  /// SdrNodeDomain contains common domain token values.
259  /// {@
260  SDR_API
261  bool HasDomain() const;
262  SDR_API
263  TfToken GetDomain() const;
264  SDR_API
265  void SetDomain(const TfToken& v);
266  SDR_API
267  void ClearDomain();
268  /// @}
269 
270  /// \name Subdomain
271  ///
272  /// Subdomain defines subsystems within the domain.
273  ///
274  /// SdrNodeSubdomain contains common subdomain token values.
275  /// {@
276  SDR_API
277  bool HasSubdomain() const;
278  SDR_API
279  TfToken GetSubdomain() const;
280  SDR_API
281  void SetSubdomain(const TfToken& v);
282  SDR_API
283  void ClearSubdomain();
284  ///@}
285 
286  /// \name Context
287  ///
288  /// Context describes a node's usage group within its subdomain.
289  ///
290  /// SdrNodeContext contains common context token values.
291  /// {@
292  SDR_API
293  bool HasContext() const;
294  SDR_API
295  TfToken GetContext() const;
296  SDR_API
297  void SetContext(const TfToken& v);
298  SDR_API
299  void ClearContext();
300  ///@}
301 
302  /// \name Role
303  ///
304  /// Role provides finer granularity for contexts that contain many nodes.
305  /// {@
306  SDR_API
307  bool HasRole() const;
308  SDR_API
309  TfToken GetRole() const;
310  SDR_API
311  void SetRole(const TfToken& v);
312  SDR_API
313  void ClearRole();
314  /// @}
315 
316  /// \name Target Renderer
317  ///
318  /// When set, the target renderer item describes that a node was designed
319  /// for (but not necessarily limited to) a specific renderer.
320  ///
321  /// For example, if a node sets its target renderer to "FooRenderer",
322  /// "BarRenderer" may still be able render the node partially, although
323  /// this isn't guaranteed. "BarRenderer" can check the node's target
324  /// renderer in addition to the node's shadingSystem for compatibility
325  /// hints.
326  ///
327  /// If a node's target renderer is unspecified, renderers should
328  /// rely on a node's shadingSystem as usual to determine compatibility.
329  /// {@
330  SDR_API
331  bool HasTargetRenderer() const;
332  SDR_API
333  TfToken GetTargetRenderer() const;
334  SDR_API
335  void SetTargetRenderer(const TfToken& v);
336  SDR_API
337  void ClearTargetRenderer();
338  /// @}
339 
340  /// \name Collections
341  ///
342  /// Collections provides a way to group nodes across different categories
343  /// in the domain-subdomain-context-role-function hierarchy.
344  ///
345  /// For example, a parser plugin author may decide that all lama nodes
346  /// should have "lama" in its Collections metadata. Or that all nodes that
347  /// have something to do with subsurface scattering be annotated with a
348  /// "subsurface" collection.
349  /// {@
350  SDR_API
351  bool HasCollections() const;
352  SDR_API
353  SdrTokenVec GetCollections() const;
354  SDR_API
355  void SetCollections(const SdrTokenVec& v);
356  SDR_API
357  void ClearCollections();
358  /// @}
359 
360  SDR_API
361  bool HasHelp() const;
362  SDR_API
363  std::string GetHelp() const;
364  SDR_API
365  void SetHelp(const std::string& v);
366  SDR_API
367  void ClearHelp();
368 
369  /// \name Departments
370  ///
371  /// Deprecated metadata item.
372  ///
373  /// \deprecated
374  /// {@
375 
376  /// \deprecated
377  SDR_API
378  bool HasDepartments() const;
379  /// \deprecated
380  SDR_API
381  SdrTokenVec GetDepartments() const;
382  /// \deprecated
383  SDR_API
384  void SetDepartments(const SdrTokenVec& v);
385  /// \deprecated
386  SDR_API
387  void ClearDepartments();
388  /// @}
389 
390  /// \deprecated
391  ///
392  /// SdrShaderNode::GetPages is computed via SdrShaderProperty's Pages
393  /// metadata. The Pages node metadata API is redundant and therefore
394  /// deprecated.
395  ///
396  /// {@
397  /// \deprecated
398  SDR_API
399  bool HasPages() const;
400  /// \deprecated
401  SDR_API
402  SdrTokenVec GetPages() const;
403  /// \deprecated
404  SDR_API
405  void SetPages(const SdrTokenVec& v);
406  /// \deprecated
407  SDR_API
408  void ClearPages();
409  /// @}
410 
411  SDR_API
412  bool HasOpenPages() const;
413  SDR_API
414  SdrTokenVec GetOpenPages() const;
415  SDR_API
416  void SetOpenPages(const SdrTokenVec& v);
417  SDR_API
418  void ClearOpenPages();
419 
420  /// \name PagesShownIf
421  ///
422  /// PagesShownIf is encoded as an SdrTokenMap, where each key
423  /// is a page name and each value is a "shownIf" style expression.
424  ///
425  /// \sa SdrShaderProperty::GetShownIf
426  ///
427  /// A nested page may be represented with ":" separating subpages.
428  ///
429  /// {@
430  SDR_API
431  bool HasPagesShownIf() const;
432  SDR_API
434  SDR_API
435  void SetPagesShownIf(const SdrTokenMap& v);
436  SDR_API
437  void ClearPagesShownIf();
438  /// @}
439 
440  SDR_API
441  bool HasPrimvars() const;
442  SDR_API
443  SdrStringVec GetPrimvars() const;
444  SDR_API
445  void SetPrimvars(const SdrStringVec& v);
446  SDR_API
447  void ClearPrimvars();
448 
449  SDR_API
450  bool HasImplementationName() const;
451  SDR_API
452  std::string GetImplementationName() const;
453  SDR_API
454  void SetImplementationName(const std::string& v);
455  SDR_API
457 
458  SDR_API
459  bool HasSdrUsdEncodingVersion() const;
460  SDR_API
461  int GetSdrUsdEncodingVersion() const;
462  SDR_API
463  void SetSdrUsdEncodingVersion(const int& v);
464  SDR_API
466 
467  SDR_API
469  SDR_API
470  std::string GetSdrDefinitionNameFallbackPrefix() const;
471  SDR_API
472  void SetSdrDefinitionNameFallbackPrefix(const std::string& v);
473  SDR_API
475 
476  /// @}
477 
478 private:
479  friend class SdrShaderNode;
480 
481  // Deprecated function for legacy metadata support.
482  //
483  // Unnamed metadata with non-string values are not returned in
484  // the legacy map.
485  SdrTokenMap _EncodeLegacyMetadata() const;
486 
487  static
488  SdrTokenMap _LegacyCtorFromInitializer(
489  std::initializer_list<std::pair<TfToken, std::string>> f)
490  {
491  return SdrTokenMap(f.begin(), f.end());
492  }
493 
494  // This method sets metadata items that have default values
495  // as indicated by GetDefaultValues.
496  //
497  // Note: This method is invoked at the ends of this class's
498  // constructors.
499  void _SetDefaultInitializations();
500 
501  VtDictionary _items;
502 };
503 
505 
506 #endif // PXR_USD_SDR_SHADER_NODE_METADATA_H
SDR_API void ClearPagesShownIf()
SDR_API std::string GetHelp() const
SDR_API void SetSdrDefinitionNameFallbackPrefix(const std::string &v)
std::vector< std::string > SdrStringVec
Definition: declare.h:61
SDR_API SdrTokenVec GetDepartments() const
SDR_API void ClearImplementationName()
static SDR_API const VtDictionary & GetDefaultValues()
SDR_API bool HasDepartments() const
#define SDR_NODE_SUBDOMAIN_TOKENS
SDR_API void ClearDomain()
const VtDictionary & GetItems() const &
Get all key-value items.
T const & UncheckedGet() const &
Definition: value.h:1046
SDR_API void ClearSdrUsdEncodingVersion()
SDR_API void SetCollections(const SdrTokenVec &v)
SDR_API void ClearSubdomain()
const GLdouble * v
Definition: glcorearb.h:837
T GetItemValueAs(const TfToken &key) const
SDR_API void ClearHelp()
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
SDR_API TfToken GetDomain() const
GLsizei const GLfloat * value
Definition: glcorearb.h:824
SDR_API bool HasCollections() const
void SetItem(const TfToken &key, const T &value)
SDR_API std::string GetSdrDefinitionNameFallbackPrefix() const
SDR_API SdrTokenVec GetOpenPages() const
SDR_API void SetContext(const TfToken &v)
SDR_API void SetRole(const TfToken &v)
SDR_API void ClearItem(const TfToken &key)
Clear the metadata item for the given key if it exists.
bool IsEmpty() const
Returns true iff this value is empty.
Definition: value.h:1227
#define SDR_NODE_DOMAIN_TOKENS
SDR_API bool HasPages() const
SDR_API void ClearCollections()
SDR_API void SetImplementationName(const std::string &v)
SDR_API bool HasSubdomain() const
SDR_API bool HasRole() const
SDR_API bool HasOpenPages() const
SDR_API bool HasDomain() const
SDR_API void ClearDepartments()
SDR_API SdrShaderNodeMetadata()
SDR_API void SetHelp(const std::string &v)
SDR_API TfToken GetCategory() const
SDR_API bool HasLabel() const
SDR_API bool HasContext() const
SDR_API void ClearRole()
SDR_API void ClearContext()
SDR_API TfToken GetRole() const
static SDR_API VtValue GetDefaultValue(const TfToken &key)
SDR_API TfToken GetSubdomain() const
SDR_API void SetPrimvars(const SdrStringVec &v)
GLfloat f
Definition: glcorearb.h:1926
Definition: token.h:70
#define SDR_NODE_METADATA_TOKENS
#define SDR_NODE_CONTEXT_TOKENS
SDR_API void SetOpenPages(const SdrTokenVec &v)
SDR_API SdrStringVec GetPrimvars() const
SDR_API void SetDepartments(const SdrTokenVec &v)
SDR_API void ClearCategory()
SDR_API TfToken GetTargetRenderer() const
SDR_API void ClearPrimvars()
SDR_API void SetSdrUsdEncodingVersion(const int &v)
SDR_API std::string GetImplementationName() const
SDR_API void ClearOpenPages()
SDR_API void SetTargetRenderer(const TfToken &v)
SDR_API bool HasImplementationName() const
SDR_API void SetDomain(const TfToken &v)
SDR_API VtValue GetItemValue(const TfToken &key) const
SDR_API void SetCategory(const TfToken &v)
SDR_API void SetItem(const TfToken &key, const VtValue &value)
SDR_API void ClearSdrDefinitionNameFallbackPrefix()
SDR_API void ClearLabel()
SDR_API bool HasTargetRenderer() const
SDR_API bool HasHelp() const
SDR_API void SetLabel(const TfToken &v)
SDR_API bool HasPagesShownIf() const
SDR_API SdrTokenVec GetCollections() const
SDR_API void SetPages(const SdrTokenVec &v)
SDR_API SdrTokenVec GetPages() const
SDR_API void ClearPages()
SDR_API bool HasItem(const TfToken &key) const
Returns whether this metadata contains an item with the given key.
SDR_API int GetSdrUsdEncodingVersion() const
SDR_API void ClearTargetRenderer()
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
SDR_API void SetSubdomain(const TfToken &v)
SDR_API TfToken GetContext() const
SDR_API void SetPagesShownIf(const SdrTokenMap &v)
VtDictionary GetItems()&&
Get all key-value items by-value.
SDR_API bool HasSdrDefinitionNameFallbackPrefix() const
std::vector< TfToken > SdrTokenVec
Definition: declare.h:39
#define SDR_NODE_ROLE_TOKENS
SDR_API bool HasPrimvars() const
SDR_API bool HasCategory() const
TF_DECLARE_PUBLIC_TOKENS(SdrNodeMetadata, SDR_API, SDR_NODE_METADATA_TOKENS)
SdrShaderNodeMetadata(const std::initializer_list< std::pair< TfToken, std::string >> &init)
SDR_API bool HasSdrUsdEncodingVersion() const
#define SDR_API
Definition: api.h:23
Definition: value.h:89
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > SdrTokenMap
Definition: declare.h:41
SDR_API SdrTokenMap GetPagesShownIf() const
SDR_API TfToken GetLabel() const