HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
parserPlugin.h
Go to the documentation of this file.
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_PARSER_PLUGIN_H
9 #define PXR_USD_SDR_PARSER_PLUGIN_H
10 
11 /// \file sdr/parserPlugin.h
12 
13 #include "pxr/pxr.h"
14 #include "pxr/base/tf/type.h"
15 #include "pxr/base/tf/weakBase.h"
16 #include "pxr/base/tf/weakPtr.h"
17 #include "pxr/usd/sdr/api.h"
18 #include "pxr/usd/sdr/declare.h"
20 
22 
23 // Forward declarations
25 
26 /// Register a parser plugin with the plugin system.
27 #define SDR_REGISTER_PARSER_PLUGIN(ParserPluginClass) \
28 TF_REGISTRY_FUNCTION(TfType) \
29 { \
30  TfType::Define<ParserPluginClass, TfType::Bases<SdrParserPlugin>>() \
31  .SetFactory<SdrParserPluginFactory<ParserPluginClass>>(); \
32 }
33 
34 /// \class SdrParserPlugin
35 ///
36 /// Interface for parser plugins.
37 ///
38 /// Parser plugins take a `SdrShaderNodeDiscoveryResult` from the discovery process
39 /// and creates a full `SdrShaderNode` instance (or, in the case of a real-world
40 /// scenario, a specialized node that derives from `SdrShaderNode`). The parser that
41 /// is selected to run is ultimately decided by the registry, and depends on the
42 /// `SdrShaderNodeDiscoveryResult`'s `discoveryType` member. A parser plugin's
43 /// `GetDiscoveryTypes()` method is how this link is made. If a discovery result
44 /// has a `discoveryType` of 'foo', and `SomeParserPlugin` has 'foo' included
45 /// in its `GetDiscoveryTypes()` return value, `SomeParserPlugin` will parse
46 /// that discovery result.
47 ///
48 /// Another categorization within the parser plugin is the 'shading system'. The
49 /// discovery type simply acts as a way to link a discovery result to a parser
50 /// plugin. On the other hand, a 'shading system' acts as an category that
51 /// groups all of the discovery types together. For example, if a plugin handled
52 /// discovery types 'foo', 'bar', and 'baz' (which are all related because they
53 /// are all handled by the same parser), they may all be grouped under one
54 /// unifying shading system. This type is available on the node via
55 /// `SdrShaderNode::GetShadingSystem()`.
56 ///
57 /// \section create How to Create a Parser Plugin
58 /// There are three steps to creating a parser plugin:
59 /// <ul>
60 /// <li>
61 /// Implement the parser plugin interface. An example parser plugin is
62 /// available in the plugin folder under `sdrOsl`. The `Parse()` method
63 /// should return the specialized node that derives from `SdrShaderNode` (and
64 /// this node should also be constructed with its specialized
65 /// properties). Examples of a specialized node and property class are
66 /// `SdrShaderNode` and `SdrShaderProperty`.
67 /// </li>
68 /// <li>
69 /// Register your new plugin with the registry. The registration macro
70 /// must be called in your plugin's implementation file:
71 /// \code{.cpp}
72 /// SDR_REGISTER_PARSER_PLUGIN(<YOUR_PARSER_PLUGIN_CLASS_NAME>)
73 /// \endcode
74 /// This macro is available in parserPlugin.h.
75 /// </li>
76 /// <li>
77 /// In the same folder as your plugin, create a `plugInfo.json` file.
78 /// This file must be formatted like so, substituting
79 /// `YOUR_LIBRARY_NAME`, `YOUR_CLASS_NAME`, and `YOUR_DISPLAY_NAME`:
80 /// \code{.json}
81 /// {
82 /// "Plugins": [{
83 /// "Type": "library",
84 /// "Name": "YOUR_LIBRARY_NAME",
85 /// "Root": "@PLUG_INFO_ROOT@",
86 /// "LibraryPath": "@PLUG_INFO_LIBRARY_PATH@",
87 /// "ResourcePath": "@PLUG_INFO_RESOURCE_PATH@",
88 /// "Info": {
89 /// "Types": {
90 /// "YOUR_CLASS_NAME" : {
91 /// "bases": ["SdrParserPlugin"],
92 /// "displayName": "YOUR_DISPLAY_NAME"
93 /// }
94 /// }
95 /// }
96 /// }]
97 /// }
98 /// \endcode
99 ///
100 /// The SDR ships with one parser plugin, the `SdrOslParserPlugin`. Take
101 /// a look at its plugInfo.json file for example values for
102 /// `YOUR_LIBRARY_NAME`, `YOUR_CLASS_NAME`, and `YOUR_DISPLAY_NAME`. If
103 /// multiple parser plugins exist in the same folder, you can continue
104 /// adding additional plugins under the `Types` key in the JSON. More
105 /// detailed information about the plugInfo.json format can be found in
106 /// the documentation for the `plug` library (in pxr/base).
107 /// </li>
108 /// </ul>
110 {
111 public:
112  SDR_API
113  SdrParserPlugin();
114  SDR_API
115  virtual ~SdrParserPlugin();
116 
117  /// Takes the specified `SdrShaderNodeDiscoveryResult` instance, which was a
118  /// result of the discovery process, and generates a new `SdrShaderNode`.
119  /// The node's name, shading system, and function must match.
120  SDR_API
122  const SdrShaderNodeDiscoveryResult& discoveryResult) = 0;
123 
124  /// Returns the types of nodes that this plugin can parse.
125  ///
126  /// "Type" here is the discovery type (in the case of files, this will
127  /// probably be the file extension, but in other systems will be data that
128  /// can be determined during discovery). This type should only be used to
129  /// match up a `SdrShaderNodeDiscoveryResult` to its parser plugin; this
130  /// value is not exposed in the node's API.
131  SDR_API
132  virtual const SdrTokenVec& GetDiscoveryTypes() const = 0;
133 
134  /// Returns the shading system that this parser operates in.
135  SDR_API
136  virtual const TfToken& GetShadingSystem() const;
137 
138  /// \deprecated
139  /// Deprecated in favor of `GetShadingSystem`
140  SDR_API
141  virtual const TfToken& GetSourceType() const;
142 
143  /// Gets an invalid node based on the discovery result provided. An invalid
144  /// node is a node that has no properties, but may have basic data found
145  /// during discovery.
146  SDR_API
148  const SdrShaderNodeDiscoveryResult& dr);
149 };
150 
151 /// \cond
152 /// Factory classes should be hidden from the documentation.
153 class SdrParserPluginFactoryBase : public TfType::FactoryBase
154 {
155 public:
156  virtual SdrParserPlugin* New() const = 0;
157 };
158 
159 template <class T>
160 class SdrParserPluginFactory : public SdrParserPluginFactoryBase
161 {
162 public:
163  virtual SdrParserPlugin* New() const
164  {
165  return new T;
166  }
167 };
168 
169 /// \endcond
170 
172 
173 #endif // PXR_USD_SDR_PARSER_PLUGIN_H
static SDR_API SdrShaderNodeUniquePtr GetInvalidShaderNode(const SdrShaderNodeDiscoveryResult &dr)
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
Base class of all factory types.
Definition: type.h:56
virtual SDR_API ~SdrParserPlugin()
Definition: token.h:70
virtual SDR_API const TfToken & GetSourceType() const
virtual SDR_API const TfToken & GetShadingSystem() const
Returns the shading system that this parser operates in.
virtual SDR_API const SdrTokenVec & GetDiscoveryTypes() const =0
std::unique_ptr< SdrShaderNode > SdrShaderNodeUniquePtr
Definition: declare.h:46
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
SDR_API SdrParserPlugin()
virtual SDR_API SdrShaderNodeUniquePtr ParseShaderNode(const SdrShaderNodeDiscoveryResult &discoveryResult)=0
std::vector< TfToken > SdrTokenVec
Definition: declare.h:39
#define SDR_API
Definition: api.h:23