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