Loading...
Searching...
No Matches
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
12
13#include "pxr/pxr.h"
15#include "pxr/usd/sdr/api.h"
16#include "pxr/usd/sdr/declare.h"
22#include <map>
23#include <mutex>
24
25PXR_NAMESPACE_OPEN_SCOPE
26
49class SdrRegistry : public TfWeakBase
50{
51public:
52 using DiscoveryPluginRefPtrVec = SdrDiscoveryPluginRefPtrVector;
53
55 SDR_API
57
65 SDR_API
66 void SetExtraDiscoveryPlugins(DiscoveryPluginRefPtrVec plugins);
67
75 SDR_API
76 void SetExtraDiscoveryPlugins(const std::vector<TfType>& pluginTypes);
77
78
86 SDR_API
88
93 SDR_API
95 const SdrShaderNodeDiscoveryResult& discoveryResult);
96
103 SDR_API
104 void SetExtraParserPlugins(const std::vector<TfType>& pluginTypes);
105
110 SDR_API
111 SdrStringVec GetSearchURIs() const;
112
120 SDR_API
121 SdrIdentifierVec
123 SdrVersionFilter filter =
124 SdrVersionFilterDefaultOnly) const;
125
132 SDR_API
133 SdrStringVec GetShaderNodeNames(const TfToken& family = TfToken()) const;
134
156 SDR_API
157 SdrShaderNodeConstPtr GetShaderNodeByIdentifier(
158 const SdrIdentifier& identifier,
159 const SdrTokenVec& typePriority = SdrTokenVec());
160
163 SDR_API
164 SdrShaderNodeConstPtr GetShaderNodeByIdentifierAndType(
165 const SdrIdentifier& identifier,
166 const TfToken& nodeType);
167
178 SDR_API
179 SdrShaderNodeConstPtr GetShaderNodeByName(
180 const std::string& name,
181 const SdrTokenVec& typePriority = SdrTokenVec(),
182 SdrVersionFilter filter = SdrVersionFilterDefaultOnly);
183
192 SDR_API
193 SdrShaderNodeConstPtr GetShaderNodeByNameAndType(
194 const std::string& name,
195 const TfToken& nodeType,
196 SdrVersionFilter filter = SdrVersionFilterDefaultOnly);
197
220 SDR_API
221 SdrShaderNodeConstPtr GetShaderNodeFromAsset(
222 const SdfAssetPath &shaderAsset,
223 const SdrTokenMap &metadata=SdrTokenMap(),
224 const TfToken &subIdentifier=TfToken(),
225 const TfToken &sourceType=TfToken());
226
242 SDR_API
243 SdrShaderNodeConstPtr GetShaderNodeFromSourceCode(
244 const std::string &sourceCode,
245 const TfToken &sourceType,
246 const SdrTokenMap &metadata=SdrTokenMap());
247
251 SDR_API
252 SdrShaderNodePtrVec GetShaderNodesByIdentifier(const SdrIdentifier& identifier);
253
258 SDR_API
259 SdrShaderNodePtrVec GetShaderNodesByName(
260 const std::string& name,
261 SdrVersionFilter filter = SdrVersionFilterDefaultOnly);
262
269 SDR_API
270 SdrShaderNodePtrVec GetShaderNodesByFamily(
271 const TfToken& family = TfToken(),
272 SdrVersionFilter filter = SdrVersionFilterDefaultOnly);
273
285 SDR_API
286 SdrTokenVec GetAllShaderNodeSourceTypes() const;
287
288protected:
289 // Allow TF to construct the class
290 friend class TfSingleton<SdrRegistry>;
291 SdrRegistry(const SdrRegistry&) = delete;
292 SdrRegistry& operator=(const SdrRegistry&) = delete;
293
294 SDR_API
295 SdrRegistry();
296
297 SDR_API
298 ~SdrRegistry();
299
300private:
301 class _DiscoveryContext;
302 friend class _DiscoveryContext;
303
304 using _TypeToParserPluginMap =
305 std::unordered_map<TfToken, SdrParserPlugin*, TfToken::HashFunctor>;
306
307 // Node cache data structure, stored SdrShaderNodes keyed by
308 // identifier and source type.
309 using _ShaderNodeMapKey = std::pair<SdrIdentifier, TfToken>;
310 using _ShaderNodeMap =
311 std::unordered_map<_ShaderNodeMapKey, SdrShaderNodeUniquePtr, TfHash>;
312
313 // Discovery results data structure, SdrShaderNodeDiscoveryResults
314 // multimap keyed ny identifier
315 using _DiscoveryResultsByIdentifier = std::unordered_multimap<
317 using _DiscoveryResultsByIdentifierRange =
318 std::pair<_DiscoveryResultsByIdentifier::const_iterator,
319 _DiscoveryResultsByIdentifier::const_iterator>;
320
321 // Discovery results data structure: a multimap of raw pointers to
322 // SdrShaderNodeDiscoveryResults (i.e. pointers to the discovery results
323 // stored in a _DiscoveryResultsByIdentifier) keyed by name.
324 using _DiscoveryResultPtrsByName = std::unordered_multimap<
325 std::string, const SdrShaderNodeDiscoveryResult *, TfHash>;
326 using _DiscoveryResultPtrsByNameRange =
327 std::pair<_DiscoveryResultPtrsByName::const_iterator,
328 _DiscoveryResultPtrsByName::const_iterator>;
329
330 // The discovery result data structures are not concurrent and must be kept
331 // in sync, thus they need some locking infrastructure.
332 mutable std::mutex _discoveryResultMutex;
333
334 // The node map is not a concurrent data structure, thus it needs some
335 // locking infrastructure.
336 mutable std::mutex _nodeMapMutex;
337
338 // Runs each discovery plugin provided and adds the results to the
339 // internal discovery result maps
340 void _RunDiscoveryPlugins(
341 const DiscoveryPluginRefPtrVec& discoveryPlugins);
342
343 // Takes the discovery and puts in the maps that hold the discovery
344 // results, keeping them in sync.
345 void _AddDiscoveryResultNoLock(SdrShaderNodeDiscoveryResult&& dr);
346
347 // Finds and instantiates the discovery plugins
348 void _FindAndInstantiateDiscoveryPlugins();
349
350 // Finds and instantiates the parser plugins
351 void _FindAndInstantiateParserPlugins();
352
353 // Instantiates the specified parser plugins and adds them to
354 // the registry.
355 void _InstantiateParserPlugins(const std::set<TfType>& parserPluginTypes);
356
357 // Parses the node for the discovery result if adding it to the node map if
358 // able and adds the discovery result to the discovery result maps.
359 // Intended for the GetShadeNodeFromAsset and GetShaderNodeFromSourceCode
360 // APIs which can add nodes that don't already appear in the discovery
361 // results.
362 SdrShaderNodeConstPtr _ParseNodeFromAssetOrSourceCode(
364
365 // Implementation helper for getting the first node of the given sourceType
366 // in the range of node discovery results for a paricular identifier.
367 SdrShaderNodeConstPtr _GetNodeInIdentifierRangeWithSourceType(
368 _DiscoveryResultsByIdentifierRange range, const TfToken& sourceType);
369
370 // Implementation helper for getting the first node of the given sourceType
371 // and matching the given version filter in the range of node discovery
372 // results for a paricular name.
373 SdrShaderNodeConstPtr _GetNodeInNameRangeWithSourceType(
374 _DiscoveryResultPtrsByNameRange range, const TfToken& sourceType,
375 SdrVersionFilter filter);
376
377 // Thread-safe find of a shader node in the cache by key.
378 SdrShaderNodeConstPtr _FindNodeInCache(
379 const _ShaderNodeMapKey &key) const;
380
381 // Thread-safe insertion of a node into the cache with a given key. If a
382 // node with the same key already exists in the cache, the pointer to the
383 // existing node will be returned, otherwise the pointer to pointer to the
384 // inserted node is returned.
385 SdrShaderNodeConstPtr _InsertNodeInCache(
386 _ShaderNodeMapKey &&key, SdrShaderNodeUniquePtr &&node);
387
388 // Finds an existing node in the node cache for the discovery result if one
389 // exists. Otherwise it parses the new node, inserts it into the cache, and
390 // returns it. If there was an error parsing or validating the node,
391 // `nullptr` will be returned.
392 SdrShaderNodeConstPtr _FindOrParseNodeInCache(
394
395 // Return the parser plugin for a discovery type. Returns null if no parser
396 // plugin has that discovery type.
398 _GetParserForDiscoveryType(const TfToken& discoveryType) const;
399
400 // The discovery plugins that were found through libplug and/or provided by
401 // the client
402 DiscoveryPluginRefPtrVec _discoveryPlugins;
403
404 // The parser plugins that have been discovered via the plugin system. Maps
405 // a discovery result's "discovery type" to a specific parser.
406 _TypeToParserPluginMap _parserPluginMap;
407
408 // The parser plugins. This has ownership of the plugin objects.
409 std::vector<std::unique_ptr<SdrParserPlugin>> _parserPlugins;
410
411 // The preliminary discovery results prior to parsing. These are stored
412 // in a multimap by identifier and a multimap by name. If accessing or
413 // mutating, _discoveryResultMutex should be used.
414 _DiscoveryResultsByIdentifier _discoveryResultsByIdentifier;
415 _DiscoveryResultPtrsByName _discoveryResultPtrsByName;
416
417 // Set of all possible source types as determined by the existing discovery
418 // results. Populated along with the discovery result multimaps. If
419 // accessing or mutating, _discoveryResultMutex should be used.
420 TfToken::Set _allSourceTypes;
421
422 // Maps a node's identifier and source type to a node instance. If accessing
423 // or mutating, _nodeMapMutex should be used.
424 _ShaderNodeMap _nodeMap;
425};
426
427PXR_NAMESPACE_CLOSE_SCOPE
428
429#endif // PXR_USD_SDR_REGISTRY_H
Contains an asset path and optional evaluated and resolved paths.
Definition: assetPath.h:78
Interface for parser plugins.
Definition: parserPlugin.h:110
The registry provides access to shader node information.
Definition: registry.h:50
SDR_API SdrShaderNodeConstPtr GetShaderNodeByName(const std::string &name, const SdrTokenVec &typePriority=SdrTokenVec(), SdrVersionFilter filter=SdrVersionFilterDefaultOnly)
Get the shader node with the specified name.
SDR_API SdrShaderNodePtrVec GetShaderNodesByFamily(const TfToken &family=TfToken(), SdrVersionFilter filter=SdrVersionFilterDefaultOnly)
Get all shader nodes, optionally restricted to the nodes that fall under a specified family and/or th...
SDR_API SdrShaderNodeConstPtr GetShaderNodeByIdentifierAndType(const SdrIdentifier &identifier, const TfToken &nodeType)
Get the shader node with the specified identifier and sourceType.
SDR_API SdrStringVec GetSearchURIs() const
Get the locations where the registry is searching for nodes.
SDR_API void SetExtraDiscoveryPlugins(const std::vector< TfType > &pluginTypes)
Allows the client to set any additional discovery plugins that would otherwise NOT be found through t...
SDR_API SdrShaderNodeConstPtr GetShaderNodeByIdentifier(const SdrIdentifier &identifier, const SdrTokenVec &typePriority=SdrTokenVec())
Get the shader node with the specified identifier, and an optional sourceTypePriority list specifying...
SDR_API void SetExtraParserPlugins(const std::vector< TfType > &pluginTypes)
Allows the client to set any additional parser plugins that would otherwise NOT be found through the ...
SDR_API SdrShaderNodePtrVec GetShaderNodesByIdentifier(const SdrIdentifier &identifier)
Get all shader nodes matching the given identifier (multiple nodes of the same identifier,...
SDR_API SdrShaderNodeConstPtr GetShaderNodeByNameAndType(const std::string &name, const TfToken &nodeType, SdrVersionFilter filter=SdrVersionFilterDefaultOnly)
A convenience wrapper around GetShaderNodeByName().
static SDR_API SdrRegistry & GetInstance()
Get the single SdrRegistry instance.
SDR_API SdrStringVec GetShaderNodeNames(const TfToken &family=TfToken()) const
Get the names of all the shader nodes that the registry is aware of.
SDR_API void AddDiscoveryResult(const SdrShaderNodeDiscoveryResult &discoveryResult)
Copy version of the method above.
SDR_API void AddDiscoveryResult(SdrShaderNodeDiscoveryResult &&discoveryResult)
Allows the client to explicitly set additional discovery results that would otherwise NOT be found th...
SDR_API void SetExtraDiscoveryPlugins(DiscoveryPluginRefPtrVec plugins)
Allows the client to set any additional discovery plugins that would otherwise NOT be found through t...
SDR_API SdrTokenVec GetAllShaderNodeSourceTypes() const
Get a sorted list of all shader node source types that may be present on the nodes in the registry.
SDR_API SdrIdentifierVec GetShaderNodeIdentifiers(const TfToken &family=TfToken(), SdrVersionFilter filter=SdrVersionFilterDefaultOnly) const
Get identifiers of all the shader nodes that the registry is aware of.
SDR_API SdrShaderNodeConstPtr GetShaderNodeFromAsset(const SdfAssetPath &shaderAsset, const SdrTokenMap &metadata=SdrTokenMap(), const TfToken &subIdentifier=TfToken(), const TfToken &sourceType=TfToken())
Parses the given asset, constructs a SdrShaderNode from it and adds it to the registry.
SDR_API SdrShaderNodeConstPtr GetShaderNodeFromSourceCode(const std::string &sourceCode, const TfToken &sourceType, const SdrTokenMap &metadata=SdrTokenMap())
Parses the given sourceCode string, constructs a SdrShaderNode from it and adds it to the registry.
SDR_API SdrShaderNodePtrVec GetShaderNodesByName(const std::string &name, SdrVersionFilter filter=SdrVersionFilterDefaultOnly)
Get all shader nodes matching the given name.
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:472
Manage a single instance of an object (see.
Definition: singleton.h:105
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
std::set< TfToken, TfTokenFastArbitraryLessThan > Set
Predefined type for set of tokens, for when faster lookup is desired, without paying the memory or in...
Definition: token.h:166
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:124
Manage a single instance of an object.
Represents the raw data of a node, and some other bits of metadata, that were determined via a SdrDis...
SdrVersionFilter
Enumeration used to select nodes by version.
Definition: declare.h:184