HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
registry.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_REGISTRY_H
9 #define PXR_USD_SDR_REGISTRY_H
10 
11 /// \file sdr/registry.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/singleton.h"
20 #include "pxr/usd/ndr/registry.h"
21 #include "pxr/usd/sdr/api.h"
22 #include "pxr/usd/sdr/declare.h"
25 #include "pxr/usd/sdr/shaderNode.h"
27 #include "pxr/usd/sdf/assetPath.h"
28 #include <map>
29 #include <mutex>
30 
32 
33 /// \class SdrRegistry
34 ///
35 /// The registry provides access to shader node information.
36 /// "Discovery Plugins" are responsible for finding the nodes that should
37 /// be included in the registry.
38 ///
39 /// Discovery plugins are found through the plugin system. If additional
40 /// discovery plugins need to be specified, a client can pass them to
41 /// `SetExtraDiscoveryPlugins()`.
42 ///
43 /// When the registry is first told about the discovery plugins, the plugins
44 /// will be asked to discover nodes. These plugins will generate
45 /// `SdrShaderNodeDiscoveryResult` instances, which only contain basic
46 /// metadata. Once the client asks for information that would require the
47 /// node's contents to be parsed (eg, what its inputs and outputs are), the
48 /// registry will begin the parsing process on an as-needed basis. See
49 /// `SdrShaderNodeDiscoveryResult` for the information that can be retrieved
50 /// without triggering a parse.
51 ///
52 /// Some methods in this library may allow for a "family" to be provided. A
53 /// family is simply a generic grouping which is optional.
54 ///
55 class SdrRegistry : public NdrRegistry
56 {
57 public:
58  using DiscoveryPluginRefPtrVec = SdrDiscoveryPluginRefPtrVector;
59 
60  /// Get the single `SdrRegistry` instance.
61  SDR_API
62  static SdrRegistry& GetInstance();
63 
64  /// Allows the client to explicitly set additional discovery results that
65  /// would otherwise NOT be found through the plugin system. For example
66  /// to support lazily-loaded plugins which cannot be easily discovered
67  /// in advance.
68  ///
69  /// This method will not immediately spawn a parse call which will be
70  /// deferred until a GetShaderNode*() method is called.
71  SDR_API
72  void AddDiscoveryResult(SdrShaderNodeDiscoveryResult&& discoveryResult);
73 
74  /// Copy version of the method above.
75  /// For performance reasons, one should prefer to use the rvalue reference
76  /// form.
77  /// \overload
78  SDR_API
79  void AddDiscoveryResult(
80  const SdrShaderNodeDiscoveryResult& discoveryResult);
81 
83 
84  /// Get identifiers of all the shader nodes that the registry is aware of.
85  ///
86  /// This will not run the parsing plugins on the nodes that have been
87  /// discovered, so this method is relatively quick. Optionally, a "family"
88  /// name can be specified to only get the identifiers of nodes that belong
89  /// to that family and a filter can be specified to get just the default
90  /// version (the default) or all versions of the node.
91  SDR_API
93  GetShaderNodeIdentifiers(const TfToken& family = TfToken(),
96 
97  /// Get the names of all the shader nodes that the registry is aware of.
98  ///
99  /// This will not run the parsing plugins on the nodes that have been
100  /// discovered, so this method is relatively quick. Optionally, a "family"
101  /// name can be specified to only get the names of nodes that belong to
102  /// that family.
103  SDR_API
104  SdrStringVec GetShaderNodeNames(const TfToken& family = TfToken()) const;
105 
106  /// Get the shader node with the specified \p identifier, and an optional
107  /// \p sourceTypePriority list specifying the set of node SOURCE types (see
108  /// `SdrShaderNode::GetSourceType()`) that should be searched.
109  ///
110  /// If no sourceTypePriority is specified, the first encountered node with
111  /// the specified identifier will be returned (first is arbitrary) if found.
112  ///
113  /// If a sourceTypePriority list is specified, then this will iterate
114  /// through each source type and try to find a node matching by identifier.
115  /// This is equivalent to calling
116  /// SdrRegistry::GetShaderNodeByIdentifierAndType for each source type
117  /// until a node is found.
118  ///
119  /// Nodes of the same identifier but different source type can exist
120  /// in the registry. If a node 'Foo' with source types 'abc' and 'xyz'
121  /// exist in the registry, and you want to make sure the 'abc' version
122  /// is fetched before the 'xyz' version, the priority list would be
123  /// specified as ['abc', 'xyz']. If the 'abc' version did not exist in
124  /// the registry, then the 'xyz' version would be returned.
125  ///
126  /// Returns `nullptr` if a node matching the arguments can't be found.
127  SDR_API
129  const SdrIdentifier& identifier,
130  const SdrTokenVec& typePriority = SdrTokenVec());
131 
132  /// Get the shader node with the specified \p identifier and \p sourceType.
133  /// If there is no matching node for the sourceType, nullptr is returned.
134  SDR_API
136  const SdrIdentifier& identifier,
137  const TfToken& nodeType);
138 
139  /// Get the shader node with the specified name.
140  ///
141  /// \deprecated
142  /// Deprecated in favor of GetShaderNodeByName(..., SdrVersionFilter filter)
143  SDR_API
145  const std::string& name,
146  const NdrTokenVec& typePriority,
148 
149  /// Get the shader node with the specified name. An optional priority list
150  /// specifies the set of node SOURCE types
151  /// (\sa SdrShaderNode::GetSourceType()) that should be searched and in what
152  /// order.
153  ///
154  /// Optionally, a filter can be specified to consider just the default
155  /// versions of nodes matching \p name (the default) or all versions
156  /// of the nodes.
157  ///
158  /// \sa GetShaderNodeByIdentifier().
159  SDR_API
161  const std::string& name,
162  const SdrTokenVec& typePriority = SdrTokenVec(),
164 
165  /// A convenience wrapper around \c GetShaderNodeByName().
166  ///
167  /// \deprecated
168  /// Deprecated in favor of GetShaderNodeByNameAndType(..., SdrVersionFilter filter)
169  SDR_API
171  const std::string& name,
172  const TfToken& nodeType,
174 
175  /// A convenience wrapper around \c GetShaderNodeByName(). Instead of
176  /// providing a priority list, an exact type is specified, and
177  /// `nullptr` is returned if a node with the exact identifier and
178  /// type does not exist.
179  ///
180  /// Optionally, a filter can be specified to consider just the default
181  /// versions of nodes matching \p name (the default) or all versions
182  /// of the nodes.
183  SDR_API
185  const std::string& name,
186  const TfToken& nodeType,
188 
189  /// Parses the given \p asset, constructs a SdrShaderNode from it and adds it to
190  /// the registry.
191  ///
192  /// Nodes created from an asset using this API can be looked up by the
193  /// unique identifier and sourceType of the returned node, or by URI,
194  /// which will be set to the unresolved asset path value.
195  ///
196  /// \p metadata contains additional metadata needed for parsing and
197  /// compiling the source code in the file pointed to by \p asset correctly.
198  /// This metadata supplements the metadata available in the asset and
199  /// overrides it in cases where there are key collisions.
200  ///
201  /// \p subidentifier is optional, and it would be used to indicate a
202  /// particular definition in the asset file if the asset contains multiple
203  /// node definitions.
204  ///
205  /// \p sourceType is optional, and it is only needed to indicate a
206  /// particular type if the asset file is capable of representing a node
207  /// definition of multiple source types.
208  ///
209  /// Returns a valid node if the asset is parsed successfully using one
210  /// of the registered parser plugins.
211  SDR_API
213  const SdfAssetPath &shaderAsset,
214  const SdrTokenMap &metadata=SdrTokenMap(),
215  const TfToken &subIdentifier=TfToken(),
216  const TfToken &sourceType=TfToken());
217 
218  /// Parses the given \p sourceCode string, constructs a SdrShaderNode from
219  /// it and adds it to the registry. The parser to be used is determined
220  /// by the specified \p sourceType.
221  ///
222  /// Nodes created from source code using this API can be looked up by the
223  /// unique identifier and sourceType of the returned node.
224  ///
225  /// \p metadata contains additional metadata needed for parsing and
226  /// compiling the source code correctly. This metadata supplements the
227  /// metadata available in \p sourceCode and overrides it cases where there
228  /// are key collisions.
229  ///
230  /// Returns a valid node if the given source code is parsed successfully
231  /// using the parser plugins that is registered for the specified
232  /// \p sourceType.
233  SDR_API
235  const std::string &sourceCode,
236  const TfToken &sourceType,
237  const SdrTokenMap &metadata=SdrTokenMap());
238 
239  /// Get all shader nodes matching the given identifier (multiple nodes of
240  /// the same identifier, but different source types, may exist). If no
241  /// nodes match the identifier, an empty vector is returned.
242  SDR_API
244 
245  /// Get all shader nodes matching the given name.
246  ///
247  /// \deprecated
248  /// Deprecated in favor of GetShaderNodesByName(..., SdrVersionFilter filter)
249  SDR_API
251  const std::string& name,
253 
254  /// Get all shader nodes matching the given name. Only nodes matching the
255  /// specified name will be parsed. Optionally, a filter can be specified
256  /// to get just the default version (the default) or all versions of the
257  /// node. If no nodes match an empty vector is returned.
258  SDR_API
260  const std::string& name,
262 
263  /// Get all shader nodes, optionally restricted to the nodes
264  /// that fall under a specified family and/or the default version.
265  ///
266  /// \deprecated
267  /// Deprecated in favor of GetShaderNodesByFamily(..., SdrVersionFilter filter)
268  SDR_API
270  const TfToken& family,
272 
273  /// Get all shader nodes, optionally restricted to the nodes
274  /// that fall under a specified family and/or the default version.
275  ///
276  /// Note that this will parse \em all nodes that the registry is aware of
277  /// (unless a family is specified), so this may take some time to run
278  /// the first time it is called.
279  SDR_API
281  const TfToken& family = TfToken(),
283 
284  /// Get a sorted list of all shader node source types that may be present
285  /// on the nodes in the registry.
286  ///
287  /// Source types originate from the discovery process, but there is no
288  /// guarantee that the discovered source types will also have a registered
289  /// parser plugin. The actual supported source types here depend on the
290  /// parsers that are available. Also note that some parser plugins may not
291  /// advertise a source type.
292  ///
293  /// See the documentation for `SdrParserPlugin` and
294  /// `SdrShaderNode::GetSourceType()` for more information.
295  SDR_API
297 
298 protected:
299  // Allow TF to construct the class
300  friend class TfSingleton<SdrRegistry>;
301 
302  SDR_API
303  SdrRegistry();
304 
305  SDR_API
306  ~SdrRegistry();
307 };
308 
310 
311 #endif // PXR_USD_SDR_REGISTRY_H
NDR_API void AddDiscoveryResult(NdrNodeDiscoveryResult &&discoveryResult)
std::vector< std::string > SdrStringVec
Definition: declare.h:67
NdrVersionFilter
Definition: declare.h:198
SdrVersionFilter
Enumeration used to select nodes by version.
Definition: declare.h:190
SDR_API SdrIdentifierVec GetShaderNodeIdentifiers(const TfToken &family=TfToken(), SdrVersionFilter filter=SdrVersionFilterDefaultOnly) const
std::vector< TfToken > NdrTokenVec
Definition: declare.h:50
SDR_API SdrShaderNodePtrVec GetShaderNodesByName(const std::string &name, NdrVersionFilter filter)
SDR_API SdrStringVec GetShaderNodeNames(const TfToken &family=TfToken()) const
static SDR_API SdrRegistry & GetInstance()
Get the single SdrRegistry instance.
std::vector< SdrIdentifier > SdrIdentifierVec
Definition: declare.h:40
SdrShaderNode const * SdrShaderNodeConstPtr
Definition: declare.h:51
SDR_API SdrShaderNodeConstPtr GetShaderNodeFromSourceCode(const std::string &sourceCode, const TfToken &sourceType, const SdrTokenMap &metadata=SdrTokenMap())
SDR_API ~SdrRegistry()
SdrShaderNodeConstPtrVec SdrShaderNodePtrVec
Definition: declare.h:54
Definition: token.h:70
SDR_API SdrShaderNodeConstPtr GetShaderNodeByIdentifier(const SdrIdentifier &identifier, const SdrTokenVec &typePriority=SdrTokenVec())
SDR_API SdrShaderNodeConstPtr GetShaderNodeByIdentifierAndType(const SdrIdentifier &identifier, const TfToken &nodeType)
SDR_API void AddDiscoveryResult(SdrShaderNodeDiscoveryResult &&discoveryResult)
SDR_API SdrShaderNodeConstPtr GetShaderNodeByName(const std::string &name, const NdrTokenVec &typePriority, NdrVersionFilter filter)
SDR_API SdrShaderNodeConstPtr GetShaderNodeByNameAndType(const std::string &name, const TfToken &nodeType, NdrVersionFilter filter)
NdrDiscoveryPluginRefPtrVector DiscoveryPluginRefPtrVec
Definition: registry.h:56
GLuint const GLchar * name
Definition: glcorearb.h:786
SDR_API SdrShaderNodePtrVec GetShaderNodesByIdentifier(const SdrIdentifier &identifier)
SDR_API SdrTokenVec GetAllShaderNodeSourceTypes() const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
SDR_API SdrShaderNodeConstPtr GetShaderNodeFromAsset(const SdfAssetPath &shaderAsset, const SdrTokenMap &metadata=SdrTokenMap(), const TfToken &subIdentifier=TfToken(), const TfToken &sourceType=TfToken())
SDR_API SdrShaderNodePtrVec GetShaderNodesByFamily(const TfToken &family, NdrVersionFilter filter)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
SDR_API SdrRegistry()
std::vector< TfToken > SdrTokenVec
Definition: declare.h:45
#define SDR_API
Definition: api.h:23
std::unordered_map< TfToken, std::string, TfToken::HashFunctor > SdrTokenMap
Definition: declare.h:47
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
Definition: glcorearb.h:1297