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 SDR_API
98 explicit SdrShaderNodeMetadata(const VtDictionary& items);
99
100 SDR_API
101 explicit SdrShaderNodeMetadata(VtDictionary&& items);
102
104
106 SDR_API
107 bool HasItem(const TfToken& key) const;
108
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 HasCategory() const;
181 SDR_API
182 TfToken GetCategory() const;
183 SDR_API
184 void SetCategory(const TfToken& v);
185 SDR_API
186 void ClearCategory();
187
193 SDR_API
194 bool HasRole() const;
195 SDR_API
196 TfToken GetRole() const;
197 SDR_API
198 void SetRole(const TfToken& v);
199 SDR_API
200 void ClearRole();
202
203 SDR_API
204 bool HasHelp() const;
205 SDR_API
206 std::string GetHelp() const;
207 SDR_API
208 void SetHelp(const std::string& v);
209 SDR_API
210 void ClearHelp();
211
212 SDR_API
213 bool HasDepartments() const;
214 SDR_API
215 SdrTokenVec GetDepartments() const;
216 SDR_API
217 void SetDepartments(const SdrTokenVec& v);
218 SDR_API
219 void ClearDepartments();
220
228 SDR_API
229 bool HasPages() const;
230 SDR_API
231 SdrTokenVec GetPages() const;
232 SDR_API
233 void SetPages(const SdrTokenVec& v);
234 SDR_API
235 void ClearPages();
237
238 SDR_API
239 bool HasOpenPages() const;
240 SDR_API
241 SdrTokenVec GetOpenPages() const;
242 SDR_API
243 void SetOpenPages(const SdrTokenVec& v);
244 SDR_API
245 void ClearOpenPages();
246
257 SDR_API
258 bool HasPagesShownIf() const;
259 SDR_API
260 SdrTokenMap GetPagesShownIf() const;
261 SDR_API
262 void SetPagesShownIf(const SdrTokenMap& v);
263 SDR_API
264 void ClearPagesShownIf();
266
267 SDR_API
268 bool HasPrimvars() const;
269 SDR_API
270 SdrStringVec GetPrimvars() const;
271 SDR_API
272 void SetPrimvars(const SdrStringVec& v);
273 SDR_API
274 void ClearPrimvars();
275
276 SDR_API
277 bool HasImplementationName() const;
278 SDR_API
279 std::string GetImplementationName() const;
280 SDR_API
281 void SetImplementationName(const std::string& v);
282 SDR_API
283 void ClearImplementationName();
284
285 SDR_API
286 bool HasSdrUsdEncodingVersion() const;
287 SDR_API
288 int GetSdrUsdEncodingVersion() const;
289 SDR_API
290 void SetSdrUsdEncodingVersion(const int& v);
291 SDR_API
292 void ClearSdrUsdEncodingVersion();
293
294 SDR_API
295 bool HasSdrDefinitionNameFallbackPrefix() const;
296 SDR_API
297 std::string GetSdrDefinitionNameFallbackPrefix() const;
298 SDR_API
299 void SetSdrDefinitionNameFallbackPrefix(const std::string& v);
300 SDR_API
301 void ClearSdrDefinitionNameFallbackPrefix();
302
304
305private:
306 friend class SdrShaderNode;
307
308 // Deprecated function for legacy metadata support.
309 //
310 // Unnamed metadata with non-string values are not returned in
311 // the legacy map.
312 SdrTokenMap _EncodeLegacyMetadata() const;
313
314 static
315 SdrTokenMap _LegacyCtorFromInitializer(
316 std::initializer_list<std::pair<TfToken, std::string>> f)
317 {
318 return SdrTokenMap(f.begin(), f.end());
319 }
320
321 VtDictionary _items;
322};
323
324PXR_NAMESPACE_CLOSE_SCOPE
325
326#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