HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
oslParser.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_PLUGIN_SDR_OSL_OSL_PARSER_H
9 #define PXR_USD_PLUGIN_SDR_OSL_OSL_PARSER_H
10 
11 /// \file sdrOsl/oslParser.h
12 
13 #include "pxr/pxr.h"
14 #include "pxr/usd/plugin/sdrOsl/api.h"
15 #include "pxr/base/vt/value.h"
17 #include "pxr/usd/sdr/declare.h"
18 #include <OSL/oslquery.h>
19 
21 
22 // Forward declarations
23 class SdrShaderNode;
25 
26 /// \class SdrOslParserPlugin
27 ///
28 /// Parses OSL nodes. For more information on parser plugins, see the
29 /// documentation for `SdrParserPlugin`.
30 ///
31 /// \section metadata_schema Metadata Schema
32 /// The following metadata is pulled out of the shader and available on the
33 /// destination as outlined below.
34 ///
35 /// \subsection node_metadata Node Metadata
36 ///
37 /// OSL Metadata Key | Destination
38 /// --------------------- | ------------
39 /// category | GetCategory()
40 /// departments | GetDepartments()
41 /// help | GetHelp()
42 /// label | GetLabel()
43 /// \ref pvars "primvars" | GetPrimvars(), GetAdditionalPrimvarProperties()
44 /// _everything else_ | GetMetadata()
45 ///
46 ///
47 /// \subsection property_metadata Property Metadata
48 ///
49 /// OSL Metadata Key | Destination
50 /// ------------------- | ------------
51 /// connectable | IsConnectable()
52 /// sdrDefinitionName | renames parameter, sends original osl param name to
53 /// | SdrShaderProperty::GetImplementationName()
54 /// page | GetPage()
55 /// help | GetHelp()
56 /// label | GetLabel()
57 /// isDynamicArray | IsDynamicArray()
58 /// \ref opts "options" | GetOptions()
59 /// vstructMemberName | GetVStructMemberName()
60 /// vstructMemberOf | GetVStructMemberOf()
61 /// _everything else_ | GetMetadata()
62 ///
63 ///
64 /// \subsection opts Options Format
65 /// Multiple options are separated with the pipe '|' character; options can be
66 /// specified in one of two ways:
67 /// <ul>
68 /// <li>
69 /// key1|key2|key3|...
70 /// </li>
71 /// <li>
72 /// key1:value1|key2:value2|...
73 /// </li>
74 /// </ul>
75 ///
76 /// In the first case, where the values are not specified, the value of the
77 /// option will be available on the node as an empty string.
78 ///
79 ///
80 /// \subsection pvars Primvar Format
81 /// Multiple primvars are separated with the pipe '|' character; primvars can be
82 /// specified in one of two ways:
83 /// <ul>
84 /// <li>
85 /// A string _not_ prefixed with a '$'; this indicates a normal primvar
86 /// </li>
87 /// <li>
88 /// A string prefixed with '$'; the part after the '$' indicates the
89 /// name of a string-typed input on the node whose value contains
90 /// additional primvars
91 /// </li>
92 /// </ul>
93 ///
95 {
96 public:
97  typedef HOSL::OSLQuery::Parameter OslParameter;
98 
99  SDROSL_API
101  SDROSL_API
103 
104  SDROSL_API
106  const SdrShaderNodeDiscoveryResult& discoveryResult) override;
107 
108  SDROSL_API
109  const SdrTokenVec& GetDiscoveryTypes() const override;
110 
111  SDROSL_API
112  const TfToken& GetSourceType() const override;
113 
114 private:
115  // Determines the sdrContext for the shader from the schema base defined
116  // in the node's metadata
117  TfToken _getSdrContextFromSchemaBase(const SdrTokenMap& metadata) const;
118 
119  // Gets a vector of properties that are present on the specified OSL
120  // query object
121  SdrShaderPropertyUniquePtrVec _getNodeProperties(
122  const HOSL::OSLQuery &query,
123  const SdrShaderNodeDiscoveryResult& discoveryResult,
124  const std::string& fallbackprefix) const;
125 
126  // Gets all metadata for the node that's present on the specified OSL
127  // query object
128  SdrTokenMap _getNodeMetadata(const HOSL::OSLQuery &query,
129  const SdrTokenMap &baseMetadata) const;
130 
131  // Gets all metadata for the specified OSL parameter
132  SdrTokenMap _getPropertyMetadata(const OslParameter* param,
133  const SdrShaderNodeDiscoveryResult& discoveryResult) const;
134 
135  // Injects any metadata that is generated by the parser. This type of
136  // metadata provides additional hints to the property.
137  void _injectParserMetadata(SdrTokenMap& metadata,
138  const TfToken& typeName) const;
139 
140  // Gets the specified parameter's value as a string. Can handle string,
141  // float, and integer values (if not one of these types, an empty string
142  // is returned).
143  std::string _getParamAsString(const OslParameter& param) const;
144 
145  // Gets a common type + array size (if array) from the OSL parameter
146  std::tuple<TfToken, size_t> _getTypeName(
147  const OslParameter* param,
148  const SdrTokenMap& metadata) const;
149 
150  // Gets the default value of the specified param.
151  VtValue _getDefaultValue(
153  const std::string& oslType,
154  size_t arraySize,
155  const SdrTokenMap& metadata
156  ) const;
157 };
158 
160 
161 #endif // PXR_USD_PLUGIN_SDR_OSL_OSL_PARSER_H
SDROSL_API SdrOslParserPlugin()
GLenum query
Definition: glad.h:2772
SDROSL_API ~SdrOslParserPlugin()
SDROSL_API SdrShaderNodeUniquePtr ParseShaderNode(const SdrShaderNodeDiscoveryResult &discoveryResult) override
Definition: token.h:70
SDROSL_API const SdrTokenVec & GetDiscoveryTypes() const override
GLenum GLfloat param
Definition: glcorearb.h:104
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
std::unique_ptr< SdrShaderNode > SdrShaderNodeUniquePtr
Definition: declare.h:52
HOSL::OSLQuery::Parameter OslParameter
Definition: oslParser.h:97
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
SDROSL_API const TfToken & GetSourceType() const override
std::vector< TfToken > SdrTokenVec
Definition: declare.h:45
Definition: value.h:146
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > SdrTokenMap
Definition: declare.h:47
std::vector< SdrShaderPropertyUniquePtr > SdrShaderPropertyUniquePtrVec
Definition: declare.h:61