Loading...
Searching...
No Matches
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"
14#include "pxr/usd/sdr/api.h"
15#include "pxr/usd/sdr/declare.h"
16
17PXR_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")) \
27 ((Role, "role")) \
28 ((Departments, "departments")) \
29 ((Help, "help")) \
30 ((Label, "label")) \
31 ((Pages, "pages")) \
32 ((OpenPages, "openPages")) \
33 ((PagesShownIf, "pagesShownIf")) \
34 ((Primvars, "primvars")) \
35 ((ImplementationName, "__SDR__implementationName"))\
36 ((Target, "__SDR__target")) \
37 ((SdrUsdEncodingVersion, "sdrUsdEncodingVersion")) \
38 ((SdrDefinitionNameFallbackPrefix, "sdrDefinitionNameFallbackPrefix"))
39
40// Note: The concept of context can be queried with the GetContext() method.
41// Sdr categorizes shaders by the context in which they are used inside of a
42// renderer. For instance during 'pattern' evaluation to feed into a surface
43// or volume shader. For BXDFs used in 'surface' and 'volume'
44// rendering situations.
45#define SDR_NODE_CONTEXT_TOKENS \
46 ((Pattern, "pattern")) \
47 ((Surface, "surface")) \
48 ((Volume, "volume")) \
49 ((Displacement, "displacement")) \
50 ((Light, "light")) \
51 ((DisplayFilter, "displayFilter")) \
52 ((LightFilter, "lightFilter")) \
53 ((PixelFilter, "pixelFilter")) \
54 ((SampleFilter, "sampleFilter"))
55
56#define SDR_NODE_ROLE_TOKENS \
57 ((Primvar, "primvar")) \
58 ((Texture, "texture")) \
59 ((Field, "field")) \
60 ((Math, "math")) \
61
62TF_DECLARE_PUBLIC_TOKENS(SdrNodeMetadata, SDR_API, SDR_NODE_METADATA_TOKENS);
63TF_DECLARE_PUBLIC_TOKENS(SdrNodeContext, SDR_API, SDR_NODE_CONTEXT_TOKENS);
64TF_DECLARE_PUBLIC_TOKENS(SdrNodeRole, SDR_API, SDR_NODE_ROLE_TOKENS);
65
66
79{
80public:
85 SDR_API
86 SdrShaderNodeMetadata(const SdrTokenMap& legacyMetadata);
87
94 const std::initializer_list<std::pair<TfToken, std::string>>& init
95 ): SdrShaderNodeMetadata(_LegacyCtorFromInitializer(init)) {}
96
97 explicit SdrShaderNodeMetadata(const VtDictionary& items)
98 : _items(items) {}
99
100 explicit SdrShaderNodeMetadata(VtDictionary&& items)
101 : _items(std::move(items)) {}
102
104
106 SDR_API
107 bool HasItem(const TfToken& key) const;
108
117 SDR_API
118 void SetItem(const TfToken& key, const VtValue& value);
119
121 template <typename T>
122 void SetItem(const TfToken& key, const T& value) {
123 SetItem(key, VtValue(value));
124 }
125
133 SDR_API
134 VtValue GetItemValue(const TfToken& key) const;
135
140 template <typename T>
141 T GetItemValueAs(const TfToken& key) const {
142 const VtValue v = GetItemValue(key);
143 if (!v.IsEmpty()) {
144 const VtValue converted = VtValue::Cast<T>(v);
145 if (!converted.IsEmpty()) {
146 return converted.UncheckedGet<T>();
147 }
148 }
149
150 return {};
151 }
152
154 SDR_API
155 void ClearItem(const TfToken& key);
156
158 const VtDictionary& GetItems() const & { return _items; }
159
161 VtDictionary GetItems() && { return std::move(_items); }
162
166
167 SDR_API
168 bool HasLabel() const;
169 SDR_API
170 TfToken GetLabel() const;
171 SDR_API
172 void SetLabel(const TfToken& v);
173 SDR_API
174 void ClearLabel();
175
176 SDR_API
177 bool HasCategory() const;
178 SDR_API
179 TfToken GetCategory() const;
180 SDR_API
181 void SetCategory(const TfToken& v);
182 SDR_API
183 void ClearCategory();
184
197 SDR_API
198 bool HasRole() const;
199 SDR_API
200 TfToken GetRole() const;
201
205 SDR_API
206 void SetRole(const TfToken& v);
207 SDR_API
208 void ClearRole();
210
211 SDR_API
212 bool HasHelp() const;
213 SDR_API
214 std::string GetHelp() const;
215 SDR_API
216 void SetHelp(const std::string& v);
217 SDR_API
218 void ClearHelp();
219
220 SDR_API
221 bool HasDepartments() const;
222 SDR_API
223 SdrTokenVec GetDepartments() const;
224 SDR_API
225 void SetDepartments(const SdrTokenVec& v);
226 SDR_API
227 void ClearDepartments();
228
236 SDR_API
237 bool HasPages() const;
238 SDR_API
239 SdrTokenVec GetPages() const;
240 SDR_API
241 void SetPages(const SdrTokenVec& v);
242 SDR_API
243 void ClearPages();
245
246 SDR_API
247 bool HasOpenPages() const;
248 SDR_API
249 SdrTokenVec GetOpenPages() const;
250 SDR_API
251 void SetOpenPages(const SdrTokenVec& v);
252 SDR_API
253 void ClearOpenPages();
254
265 SDR_API
266 bool HasPagesShownIf() const;
267 SDR_API
268 SdrTokenMap GetPagesShownIf() const;
269 SDR_API
270 void SetPagesShownIf(const SdrTokenMap& v);
271 SDR_API
272 void ClearPagesShownIf();
274
275 SDR_API
276 bool HasPrimvars() const;
277 SDR_API
278 SdrStringVec GetPrimvars() const;
279 SDR_API
280 void SetPrimvars(const SdrStringVec& v);
281 SDR_API
282 void ClearPrimvars();
283
284 SDR_API
285 bool HasImplementationName() const;
286 SDR_API
287 std::string GetImplementationName() const;
288 SDR_API
289 void SetImplementationName(const std::string& v);
290 SDR_API
291 void ClearImplementationName();
292
293 SDR_API
294 bool HasSdrUsdEncodingVersion() const;
295 SDR_API
296 int GetSdrUsdEncodingVersion() const;
297 SDR_API
298 void SetSdrUsdEncodingVersion(const int& v);
299 SDR_API
300 void ClearSdrUsdEncodingVersion();
301
302 SDR_API
303 bool HasSdrDefinitionNameFallbackPrefix() const;
304 SDR_API
305 std::string GetSdrDefinitionNameFallbackPrefix() const;
306 SDR_API
307 void SetSdrDefinitionNameFallbackPrefix(const std::string& v);
308 SDR_API
309 void ClearSdrDefinitionNameFallbackPrefix();
310
312
313private:
314 friend class SdrShaderNode;
315
316 // Deprecated function for legacy metadata support.
317 //
318 // Unnamed metadata with non-string values are not returned in
319 // the legacy map.
320 SdrTokenMap _EncodeLegacyMetadata() const;
321
322 static
323 SdrTokenMap _LegacyCtorFromInitializer(
324 std::initializer_list<std::pair<TfToken, std::string>> f)
325 {
326 return SdrTokenMap(f.begin(), f.end());
327 }
328
329 VtDictionary _items;
330};
331
332PXR_NAMESPACE_CLOSE_SCOPE
333
334#endif // PXR_USD_SDR_SHADER_NODE_METADATA_H
Represents a node that holds shading information.
Definition: shaderNode.h:40
SdrShaderNodeMetadata contains generic and named metadata for SdrShaderNode.
SDR_API bool HasLabel() const
Named metadata.
SDR_API bool HasItem(const TfToken &key) const
Returns whether this metadata contains an item with the given key.
T GetItemValueAs(const TfToken &key) const
Convenience to get an item value as T.
void SetItem(const TfToken &key, const T &value)
This is an overloaded member function, provided for convenience. It differs from the above function o...
SDR_API SdrShaderNodeMetadata(const SdrTokenMap &legacyMetadata)
Ingest metadata from the legacy SdrTokenMap structure.
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.
SDR_API bool HasPages() const
SDR_API VtValue GetItemValue(const TfToken &key) const
Get the VtValue for the given key.
SDR_API void SetItem(const TfToken &key, const VtValue &value)
Set a key-value item for this metadata.
VtDictionary GetItems() &&
Get all key-value items by-value.
SdrShaderNodeMetadata(const std::initializer_list< std::pair< TfToken, std::string > > &init)
Ingest metadata from an initializer list for the legacy SdrTokenMap.
SDR_API void SetRole(const TfToken &v)
Sets the given Role value.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
A map with string keys and VtValue values.
Definition: dictionary.h:52
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:90
bool IsEmpty() const
Returns true iff this value is empty.
Definition: value.h:1227
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1046
STL namespace.
This file defines some macros that are useful for declaring and using static TfTokens.
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:92