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& GetShadingSystem() const override;
113 
114 private:
115  // Sets the shader's sdr metadata context.
116  // - If the metadata already has a "context" item, this does nothing.
117  // - If the metadata has a "schemaBase" item, sets "context" mapped
118  // from the "schemaBase" item value.
119  // - Otherwise, does nothing
120  void _setSdrContext(SdrTokenMap& metadata) const;
121 
122  // Gets a vector of properties that are present on the specified OSL
123  // query object
124  SdrShaderPropertyUniquePtrVec _getNodeProperties(
125  const HOSL::OSLQuery &query,
126  const SdrShaderNodeDiscoveryResult& discoveryResult,
127  const std::string& fallbackprefix) const;
128 
129  // Gets all metadata for the node that's present on the specified OSL
130  // query object
131  SdrTokenMap _getNodeMetadata(const HOSL::OSLQuery &query,
132  const SdrTokenMap &baseMetadata) const;
133 
134  // Gets all metadata for the specified OSL parameter
135  SdrTokenMap _getPropertyMetadata(const OslParameter* param,
136  const SdrShaderNodeDiscoveryResult& discoveryResult) const;
137 
138  // Injects any metadata that is generated by the parser. This type of
139  // metadata provides additional hints to the property.
140  void _injectParserMetadata(SdrTokenMap& metadata,
141  const TfToken& typeName) const;
142 
143  // Gets the specified parameter's value as a string. Can handle string,
144  // float, and integer values (if not one of these types, an empty string
145  // is returned).
146  std::string _getParamAsString(const OslParameter& param) const;
147 
148  // Gets a common type + array size (if array) from the OSL parameter
149  std::tuple<TfToken, size_t> _getTypeName(
150  const OslParameter* param,
151  const SdrTokenMap& metadata) const;
152 
153  // Gets the default value of the specified param.
154  VtValue _getDefaultValue(
156  const std::string& oslType,
157  size_t arraySize,
158  const SdrTokenMap& metadata
159  ) const;
160 };
161 
163 
164 #endif // PXR_USD_PLUGIN_SDR_OSL_OSL_PARSER_H
SDROSL_API SdrOslParserPlugin()
GLenum query
Definition: glad.h:2772
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
SDROSL_API ~SdrOslParserPlugin()
SDROSL_API SdrShaderNodeUniquePtr ParseShaderNode(const SdrShaderNodeDiscoveryResult &discoveryResult) override
Definition: token.h:70
SDROSL_API const TfToken & GetShadingSystem() const override
Returns the shading system that this parser operates in.
SDROSL_API const SdrTokenVec & GetDiscoveryTypes() const override
GLenum GLfloat param
Definition: glcorearb.h:104
std::unique_ptr< SdrShaderNode > SdrShaderNodeUniquePtr
Definition: declare.h:46
HOSL::OSLQuery::Parameter OslParameter
Definition: oslParser.h:97
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::vector< TfToken > SdrTokenVec
Definition: declare.h:39
Definition: value.h:89
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > SdrTokenMap
Definition: declare.h:41
std::vector< SdrShaderPropertyUniquePtr > SdrShaderPropertyUniquePtrVec
Definition: declare.h:55