HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
shaderMetadataHelpers.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 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_METADATA_HELPERS_H
9 #define PXR_USD_SDR_SHADER_METADATA_HELPERS_H
10 
11 /// \file sdr/shaderMetadataHelpers.h
12 
13 #include "pxr/pxr.h"
14 #include "pxr/usd/sdr/api.h"
16 #include "pxr/base/tf/token.h"
17 #include "pxr/base/vt/value.h"
18 #include "pxr/usd/sdr/declare.h"
19 #include "pxr/usd/sdr/shaderNode.h"
22 
23 #include <limits>
24 
26 
27 /// \namespace ShaderMetadataHelpers
28 ///
29 /// Various utilities for parsing metadata contained within shaders.
30 ///
31 namespace ShaderMetadataHelpers
32 {
33  /// Determines if the given metadatum in the metadata dictionary has a
34  /// truthy value.
35  ///
36  /// \deprecated
37  /// Deprecated in favor of SdrShaderNodeMetadata::Get*
38  /// and SdrShaderPropertyMetadata::Get* methods on bool metadata
39  ///
40  /// All values are considered to be true except the following
41  /// (case-insensitive): '0', 'false', and 'f'. The absence of `key`
42  /// in the metadata also evaluates to false.
43  SDR_API
44  bool
45  IsTruthy(const TfToken& key, const SdrTokenMap& metadata);
46 
47  /// Extracts the string value from the given metadatum if it exists,
48  /// otherwise returns \p defaultValue.
49  ///
50  /// \deprecated
51  /// Deprecated in favor of SdrShaderNodeMetadata::Get*
52  /// and SdrShaderPropertyMetadata::Get* methods on string metadata
53  SDR_API
54  std::string
55  StringVal(const TfToken& key, const SdrTokenMap& metadata,
56  const std::string& defaultValue = std::string());
57 
58  /// Extracts the tokenized value from the given metadatum if it exists,
59  /// otherwise returns \p defaultValue.
60  ///
61  /// \deprecated
62  /// \deprecated
63  /// Deprecated in favor of SdrShaderNodeMetadata::Get*
64  /// and SdrShaderPropertyMetadata::Get* methods on TfToken metadata
65  SDR_API
66  TfToken
67  TokenVal(const TfToken& key, const SdrTokenMap& metadata,
68  const TfToken& defaultValue = TfToken());
69 
70  /// Extracts the int value from the given metadatum if it exists and is a
71  /// valid integer value, otherwise returns \p default value.
72  ///
73  /// \deprecated
74  /// \deprecated
75  /// Deprecated in favor of SdrShaderNodeMetadata::Get*
76  /// and SdrShaderPropertyMetadata::Get* methods on int metadata
77  SDR_API
78  int
79  IntVal(const TfToken& key, const SdrTokenMap& metadata,
80  int defaultValue = std::numeric_limits<int>::max());
81 
82  /// Extracts a vector of strings from the given metadatum. An empty vector
83  /// is returned if the metadatum does not exist.
84  ///
85  /// \deprecated
86  /// Deprecated in favor of SdrShaderNodeMetadata::Get*
87  /// and SdrShaderPropertyMetadata::Get* methods on SdrStringVec metadata
88  SDR_API
90  StringVecVal(const TfToken& key, const SdrTokenMap& metadata);
91 
92  /// Extracts a vector of tokenized values from the given metadatum. An empty
93  /// vector is returned if the metadatum does not exist.
94  ///
95  /// \deprecated
96  /// Deprecated in favor of SdrShaderNodeMetadata::Get*
97  /// and SdrShaderPropertyMetadata::Get* methods on SdrTokenVec metadata
98  SDR_API
100  TokenVecVal(const TfToken& key, const SdrTokenMap& metadata);
101 
102  /// Extracts an "options" vector from the given string.
103  SDR_API
105  OptionVecVal(const std::string& optionStr);
106 
107  /// Serializes a vector of strings into a string using the pipe character
108  /// as the delimiter.
109  ///
110  /// \deprecated
111  /// Deprecated in favor of SdrShaderNodeMetadata::Set*
112  /// and SdrShaderPropertyMetadata::Set* methods on SdrStringVec metadata
113  SDR_API
114  std::string
115  CreateStringFromStringVec(const SdrStringVec& stringVec);
116 
117  /// Determines if the specified property metadata has a widget that
118  /// indicates the property is an asset identifier.
119  ///
120  /// \deprecated
121  /// Deprecated in favor of
122  /// SdrShaderPropertyMetadata::GetIsAssetIdentifier.
123  SDR_API
124  bool
125  IsPropertyAnAssetIdentifier(const SdrTokenMap& metadata);
126 
127  /// Determines if the specified property metadata has a 'renderType' that
128  /// indicates the property should be a SdrPropertyTypes->Terminal
129  SDR_API
130  bool
131  IsPropertyATerminal(const SdrTokenMap& metadata);
132 
133  /// Gets the "role" from metadata if one is provided. Only returns a value
134  /// if it's a valid role as defined by SdrPropertyRole tokens.
135  SDR_API
136  TfToken
138 
139  /// Parses the VtValue from the given valueStr according to the sdf type
140  /// expressed by the given property via two steps.
141  ///
142  /// 1. valueStr is preprocessed into a suitable input for the sdf value
143  /// parser.
144  /// - If the value has an sdf type of string or token, the value will
145  /// be quoted.
146  /// - If the value is an sdf asset, appropriate @'s are added.
147  /// - If the value is array-like, square brackets are added.
148  /// - If the value is tuple-like, parentheses are added.
149  /// NOTE: Constituent items of iterable types must be appropriately
150  /// quoted by the caller of this function.
151  /// 2. sdf value parsing is performed on the preprocessed result.
152  ///
153  /// If parsing fails, this returns an empty VtValue and populates err. This
154  /// function does not throw exceptions.
155  SDR_API
156  VtValue
157  ParseSdfValue(const std::string& valueStr,
158  const SdrShaderPropertyConstPtr& property,
159  std::string* err);
160 
161  /// Synthesizes a "shownIf" expression from conditional visibility metadata
162  /// in \p property, expressed according to Katana's "args" format.
163  /// The sibling properties should be provided in \p allProperties and will
164  /// be referenced when resolving relative paths (`../../some/property`) and
165  /// when parsing embedded property values.
166  SDR_API
167  std::string
169  const SdrShaderPropertyUniquePtrVec& allProperties,
171 
172  /// Synthesizes a "shownIf" expression from conditional visibility metadata
173  /// associated with \p pageName, expressed according to Katana's "args"
174  /// format. The node's properties should be provided in \p properties and
175  /// will be referenced when resolving relative paths (`../../some/property`)
176  /// and when parsing embedded property values. Uses \p shaderUri when when
177  /// reporting errors.
178  SDR_API
179  std::string
180  ComputeShownIfFromMetadata(const SdrTokenMap& metadata,
181  const std::string& pageName,
182  const SdrShaderPropertyUniquePtrVec& properties,
183  const std::string& shaderUri);
184 }
185 
187 
188 #endif // PXR_USD_SDR_SHADER_METADATA_HELPERS_H
std::vector< std::string > SdrStringVec
Definition: declare.h:61
SDR_API TfToken GetRoleFromMetadata(const SdrShaderPropertyMetadata &metadata)
SDR_API SdrStringVec StringVecVal(const TfToken &key, const SdrTokenMap &metadata)
SDR_API bool IsPropertyAnAssetIdentifier(const SdrTokenMap &metadata)
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
SDR_API std::string ComputeShownIfFromMetadata(SdrShaderPropertyConstPtr property, const SdrShaderPropertyUniquePtrVec &allProperties, SdrShaderNodeConstPtr shader)
SdrShaderNode const * SdrShaderNodeConstPtr
Definition: declare.h:45
SDR_API std::string CreateStringFromStringVec(const SdrStringVec &stringVec)
SDR_API bool IsTruthy(const TfToken &key, const SdrTokenMap &metadata)
SDR_API std::string StringVal(const TfToken &key, const SdrTokenMap &metadata, const std::string &defaultValue=std::string())
Definition: token.h:70
SdrShaderProperty const * SdrShaderPropertyConstPtr
Definition: declare.h:53
SDR_API SdrTokenVec TokenVecVal(const TfToken &key, const SdrTokenMap &metadata)
SDR_API bool IsPropertyATerminal(const SdrTokenMap &metadata)
SDR_API VtValue ParseSdfValue(const std::string &valueStr, const SdrShaderPropertyConstPtr &property, std::string *err)
GLuint shader
Definition: glcorearb.h:785
SDR_API SdrOptionVec OptionVecVal(const std::string &optionStr)
Extracts an "options" vector from the given string.
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::vector< TfToken > SdrTokenVec
Definition: declare.h:39
#define SDR_API
Definition: api.h:23
SDR_API int IntVal(const TfToken &key, const SdrTokenMap &metadata, int defaultValue=std::numeric_limits< int >::max())
std::vector< SdrOption > SdrOptionVec
Definition: declare.h:63
Definition: value.h:89
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > SdrTokenMap
Definition: declare.h:41
SDR_API TfToken TokenVal(const TfToken &key, const SdrTokenMap &metadata, const TfToken &defaultValue=TfToken())
std::vector< SdrShaderPropertyUniquePtr > SdrShaderPropertyUniquePtrVec
Definition: declare.h:55