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 ((VolumeFilter, "volumeFilter"))
56
57#define SDR_NODE_ROLE_TOKENS \
58 ((Primvar, "primvar")) \
59 ((Texture, "texture")) \
60 ((Field, "field")) \
61 ((Math, "math")) \
62
63TF_DECLARE_PUBLIC_TOKENS(SdrNodeMetadata, SDR_API, SDR_NODE_METADATA_TOKENS);
64TF_DECLARE_PUBLIC_TOKENS(SdrNodeContext, SDR_API, SDR_NODE_CONTEXT_TOKENS);
65TF_DECLARE_PUBLIC_TOKENS(SdrNodeRole, SDR_API, SDR_NODE_ROLE_TOKENS);
66
67
80{
81public:
86 SDR_API
87 SdrShaderNodeMetadata(const SdrTokenMap& legacyMetadata);
88
95 const std::initializer_list<std::pair<TfToken, std::string>>& init
96 ): SdrShaderNodeMetadata(_LegacyCtorFromInitializer(init)) {}
97
98 SDR_API
99 explicit SdrShaderNodeMetadata(const VtDictionary& items);
100
101 SDR_API
102 explicit SdrShaderNodeMetadata(VtDictionary&& items);
103
105
107 SDR_API
108 bool HasItem(const TfToken& key) const;
109
121 SDR_API
122 void SetItem(const TfToken& key, const VtValue& value);
123
125 template <typename T>
126 void SetItem(const TfToken& key, const T& value) {
127 SetItem(key, VtValue(value));
128 }
129
137 SDR_API
138 VtValue GetItemValue(const TfToken& key) const;
139
144 template <typename T>
145 T GetItemValueAs(const TfToken& key) const {
146 const VtValue v = GetItemValue(key);
147 if (!v.IsEmpty()) {
148 const VtValue converted = VtValue::Cast<T>(v);
149 if (!converted.IsEmpty()) {
150 return converted.UncheckedGet<T>();
151 }
152 }
153
154 return {};
155 }
156
158 SDR_API
159 void ClearItem(const TfToken& key);
160
162 const VtDictionary& GetItems() const & { return _items; }
163
165 VtDictionary GetItems() && { return std::move(_items); }
166
170
171 SDR_API
172 bool HasLabel() const;
173 SDR_API
174 TfToken GetLabel() const;
175 SDR_API
176 void SetLabel(const TfToken& v);
177 SDR_API
178 void ClearLabel();
179
180 SDR_API
181 bool HasCategory() const;
182 SDR_API
183 TfToken GetCategory() const;
184 SDR_API
185 void SetCategory(const TfToken& v);
186 SDR_API
187 void ClearCategory();
188
194 SDR_API
195 bool HasRole() const;
196 SDR_API
197 TfToken GetRole() const;
198 SDR_API
199 void SetRole(const TfToken& v);
200 SDR_API
201 void ClearRole();
203
204 SDR_API
205 bool HasHelp() const;
206 SDR_API
207 std::string GetHelp() const;
208 SDR_API
209 void SetHelp(const std::string& v);
210 SDR_API
211 void ClearHelp();
212
213 SDR_API
214 bool HasDepartments() const;
215 SDR_API
216 SdrTokenVec GetDepartments() const;
217 SDR_API
218 void SetDepartments(const SdrTokenVec& v);
219 SDR_API
220 void ClearDepartments();
221
229 SDR_API
230 bool HasPages() const;
231 SDR_API
232 SdrTokenVec GetPages() const;
233 SDR_API
234 void SetPages(const SdrTokenVec& v);
235 SDR_API
236 void ClearPages();
238
239 SDR_API
240 bool HasOpenPages() const;
241 SDR_API
242 SdrTokenVec GetOpenPages() const;
243 SDR_API
244 void SetOpenPages(const SdrTokenVec& v);
245 SDR_API
246 void ClearOpenPages();
247
258 SDR_API
259 bool HasPagesShownIf() const;
260 SDR_API
261 SdrTokenMap GetPagesShownIf() const;
262 SDR_API
263 void SetPagesShownIf(const SdrTokenMap& v);
264 SDR_API
265 void ClearPagesShownIf();
267
268 SDR_API
269 bool HasPrimvars() const;
270 SDR_API
271 SdrStringVec GetPrimvars() const;
272 SDR_API
273 void SetPrimvars(const SdrStringVec& v);
274 SDR_API
275 void ClearPrimvars();
276
277 SDR_API
278 bool HasImplementationName() const;
279 SDR_API
280 std::string GetImplementationName() const;
281 SDR_API
282 void SetImplementationName(const std::string& v);
283 SDR_API
284 void ClearImplementationName();
285
286 SDR_API
287 bool HasSdrUsdEncodingVersion() const;
288 SDR_API
289 int GetSdrUsdEncodingVersion() const;
290 SDR_API
291 void SetSdrUsdEncodingVersion(const int& v);
292 SDR_API
293 void ClearSdrUsdEncodingVersion();
294
295 SDR_API
296 bool HasSdrDefinitionNameFallbackPrefix() const;
297 SDR_API
298 std::string GetSdrDefinitionNameFallbackPrefix() const;
299 SDR_API
300 void SetSdrDefinitionNameFallbackPrefix(const std::string& v);
301 SDR_API
302 void ClearSdrDefinitionNameFallbackPrefix();
303
305
306private:
307 friend class SdrShaderNode;
308
309 // Deprecated function for legacy metadata support.
310 //
311 // Unnamed metadata with non-string values are not returned in
312 // the legacy map.
313 SdrTokenMap _EncodeLegacyMetadata() const;
314
315 static
316 SdrTokenMap _LegacyCtorFromInitializer(
317 std::initializer_list<std::pair<TfToken, std::string>> f)
318 {
319 return SdrTokenMap(f.begin(), f.end());
320 }
321
322 VtDictionary _items;
323};
324
325PXR_NAMESPACE_CLOSE_SCOPE
326
327#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.
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
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