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"
20#include "pxr/usd/sdr/shaderNodeQuery.h"
23#include <map>
24#include <mutex>
25
26PXR_NAMESPACE_OPEN_SCOPE
27
46class SdrRegistry : public TfWeakBase
47{
48public:
49 using DiscoveryPluginRefPtrVec = SdrDiscoveryPluginRefPtrVector;
50
52 SDR_API
54
62 SDR_API
63 void SetExtraDiscoveryPlugins(DiscoveryPluginRefPtrVec plugins);
64
72 SDR_API
73 void SetExtraDiscoveryPlugins(const std::vector<TfType>& pluginTypes);
74
75
83 SDR_API
85
90 SDR_API
92 const SdrShaderNodeDiscoveryResult& discoveryResult);
93
100 SDR_API
101 void SetExtraParserPlugins(const std::vector<TfType>& pluginTypes);
102
107 SDR_API
108 SdrStringVec GetSearchURIs() const;
109
117 SDR_API
118 SdrIdentifierVec
120 SdrVersionFilter filter =
121 SdrVersionFilterDefaultOnly) const;
122
129 SDR_API
130 SdrStringVec GetShaderNodeNames(const TfToken& function = TfToken()) const;
131
155 SDR_API
156 SdrShaderNodeConstPtr GetShaderNodeByIdentifier(
157 const SdrIdentifier& identifier,
158 const SdrTokenVec& shadingSystemPriority = SdrTokenVec());
159
164 SDR_API
166 const SdrIdentifier& identifier,
167 const TfToken& shadingSystem);
168
174 SDR_API
175 SdrShaderNodeConstPtr GetShaderNodeByIdentifierAndType(
176 const SdrIdentifier& identifier,
177 const TfToken& nodeType);
178
190 SDR_API
191 SdrShaderNodeConstPtr GetShaderNodeByName(
192 const std::string& name,
193 const SdrTokenVec& shadingSystemPriority = SdrTokenVec(),
194 SdrVersionFilter filter = SdrVersionFilterDefaultOnly);
195
205 SDR_API
206 SdrShaderNodeConstPtr GetShaderNodeByNameAndSystem(
207 const std::string& name,
208 const TfToken& shadingSystem,
209 SdrVersionFilter filter = SdrVersionFilterDefaultOnly);
210
222 SDR_API
223 SdrShaderNodeConstPtr GetShaderNodeByNameAndType(
224 const std::string& name,
225 const TfToken& nodeType,
226 SdrVersionFilter filter = SdrVersionFilterDefaultOnly);
227
250 SDR_API
251 SdrShaderNodeConstPtr GetShaderNodeFromAsset(
252 const SdfAssetPath &shaderAsset,
253 const SdrTokenMap &metadata=SdrTokenMap(),
254 const TfToken &subIdentifier=TfToken(),
255 const TfToken &shadingSystem=TfToken());
256
272 SDR_API
273 SdrShaderNodeConstPtr GetShaderNodeFromSourceCode(
274 const std::string &sourceCode,
275 const TfToken &shadingSystem,
276 const SdrTokenMap &metadata=SdrTokenMap());
277
281 SDR_API
282 SdrShaderNodePtrVec GetShaderNodesByIdentifier(const SdrIdentifier& identifier);
283
288 SDR_API
289 SdrShaderNodePtrVec GetShaderNodesByName(
290 const std::string& name,
291 SdrVersionFilter filter = SdrVersionFilterDefaultOnly);
292
299 SDR_API
300 SdrShaderNodePtrVec GetShaderNodesByFunction(
301 const TfToken& function = TfToken(),
302 SdrVersionFilter filter = SdrVersionFilterDefaultOnly);
303
309 SDR_API
310 SdrShaderNodePtrVec GetShaderNodesByFamily(
311 const TfToken& family = TfToken(),
312 SdrVersionFilter filter = SdrVersionFilterDefaultOnly);
313
322 SDR_API
323 SdrShaderNodePtrVec GetAllShaderNodes();
324
336 SDR_API
338
344 SDR_API
345 SdrTokenVec GetAllShaderNodeSourceTypes() const;
346
352 SDR_API
354
361 SDR_API
362 void ParseAll();
363
364protected:
365 // Allow TF to construct the class
366 friend class TfSingleton<SdrRegistry>;
367 SdrRegistry(const SdrRegistry&) = delete;
368 SdrRegistry& operator=(const SdrRegistry&) = delete;
369
370 SDR_API
371 SdrRegistry();
372
373 SDR_API
374 ~SdrRegistry();
375
376private:
377 class _DiscoveryContext;
378 friend class _DiscoveryContext;
379
380 using _TypeToParserPluginMap =
381 std::unordered_map<TfToken, SdrParserPlugin*, TfToken::HashFunctor>;
382
383 // Node cache data structure, stored SdrShaderNodes keyed by
384 // identifier and shading system.
385 using _ShaderNodeMapKey = std::pair<SdrIdentifier, TfToken>;
386 using _ShaderNodeMap =
387 std::unordered_map<_ShaderNodeMapKey, SdrShaderNodeUniquePtr, TfHash>;
388
389 // Discovery results data structure, SdrShaderNodeDiscoveryResults
390 // multimap keyed ny identifier
391 using _DiscoveryResultsByIdentifier = std::unordered_multimap<
393 using _DiscoveryResultsByIdentifierRange =
394 std::pair<_DiscoveryResultsByIdentifier::const_iterator,
395 _DiscoveryResultsByIdentifier::const_iterator>;
396
397 // Discovery results data structure: a multimap of raw pointers to
398 // SdrShaderNodeDiscoveryResults (i.e. pointers to the discovery results
399 // stored in a _DiscoveryResultsByIdentifier) keyed by name.
400 using _DiscoveryResultPtrsByName = std::unordered_multimap<
401 std::string, const SdrShaderNodeDiscoveryResult *, TfHash>;
402 using _DiscoveryResultPtrsByNameRange =
403 std::pair<_DiscoveryResultPtrsByName::const_iterator,
404 _DiscoveryResultPtrsByName::const_iterator>;
405
406 // The discovery result data structures are not concurrent and must be kept
407 // in sync, thus they need some locking infrastructure.
408 mutable std::mutex _discoveryResultMutex;
409
410 // The node map is not a concurrent data structure, thus it needs some
411 // locking infrastructure. Ensure that _discoveryResultMutex is not
412 // acquired after _nodeMapMutex is acquired, to avoid deadlock.
413 mutable std::mutex _nodeMapMutex;
414
415 // Runs each discovery plugin provided and adds the results to the
416 // internal discovery result maps
417 void _RunDiscoveryPlugins(
418 const DiscoveryPluginRefPtrVec& discoveryPlugins);
419
420 // Takes the discovery and puts in the maps that hold the discovery
421 // results, keeping them in sync.
422 void _AddDiscoveryResultNoLock(SdrShaderNodeDiscoveryResult&& dr);
423
424 // Finds and instantiates the discovery plugins
425 void _FindAndInstantiateDiscoveryPlugins();
426
427 // Finds and instantiates the parser plugins
428 void _FindAndInstantiateParserPlugins();
429
430 // Instantiates the specified parser plugins and adds them to
431 // the registry.
432 void _InstantiateParserPlugins(const std::set<TfType>& parserPluginTypes);
433
434 // Parses the node for the discovery result if adding it to the node map if
435 // able and adds the discovery result to the discovery result maps.
436 // Intended for the GetShadeNodeFromAsset and GetShaderNodeFromSourceCode
437 // APIs which can add nodes that don't already appear in the discovery
438 // results.
439 SdrShaderNodeConstPtr _ParseNodeFromAssetOrSourceCode(
441
442 // Implementation helper for getting the first node of the given
443 // shadingSystem in the range of node discovery results for a paricular
444 // identifier.
445 SdrShaderNodeConstPtr _GetNodeInIdentifierRangeWithShadingSystem(
446 _DiscoveryResultsByIdentifierRange range,
447 const TfToken& shadingSystem);
448
449 // Implementation helper for getting the first node of the given
450 // shadingSystem and matching the given version filter in the range
451 // of node discovery results for a paricular name.
452 SdrShaderNodeConstPtr _GetNodeInNameRangeWithShadingSystem(
453 _DiscoveryResultPtrsByNameRange range, const TfToken& shadingSystem,
454 SdrVersionFilter filter);
455
456 // Thread-safe find of a shader node in the cache by key.
457 SdrShaderNodeConstPtr _FindNodeInCache(
458 const _ShaderNodeMapKey &key) const;
459
460 // Thread-safe insertion of a node into the cache with a given key. If a
461 // node with the same key already exists in the cache, the pointer to the
462 // existing node will be returned, otherwise the pointer to pointer to the
463 // inserted node is returned.
464 SdrShaderNodeConstPtr _InsertNodeInCache(
465 _ShaderNodeMapKey &&key, SdrShaderNodeUniquePtr &&node);
466
467 // Finds an existing node in the node cache for the discovery result if one
468 // exists. Otherwise it parses the new node, inserts it into the cache, and
469 // returns it. If there was an error parsing or validating the node,
470 // `nullptr` will be returned.
471 SdrShaderNodeConstPtr _FindOrParseNodeInCache(
473
474 // Return the parser plugin for a discovery type. Returns null if no parser
475 // plugin has that discovery type.
477 _GetParserForDiscoveryType(const TfToken& discoveryType) const;
478
479 // The discovery plugins that were found through libplug and/or provided by
480 // the client
481 DiscoveryPluginRefPtrVec _discoveryPlugins;
482
483 // The parser plugins that have been discovered via the plugin system. Maps
484 // a discovery result's "discovery type" to a specific parser.
485 _TypeToParserPluginMap _parserPluginMap;
486
487 // The parser plugins. This has ownership of the plugin objects.
488 std::vector<std::unique_ptr<SdrParserPlugin>> _parserPlugins;
489
490 // The preliminary discovery results prior to parsing. These are stored
491 // in a multimap by identifier and a multimap by name. If accessing or
492 // mutating, _discoveryResultMutex should be used.
493 _DiscoveryResultsByIdentifier _discoveryResultsByIdentifier;
494 _DiscoveryResultPtrsByName _discoveryResultPtrsByName;
495
496 // Set of all possible shading systems as determined by the existing
497 // discovery results. Populated along with the discovery result multimaps.
498 // If accessing or mutating, _discoveryResultMutex should be used.
499 TfToken::Set _allShadingSystems;
500
501 // Maps a node's identifier and source type to a node instance. If accessing
502 // or mutating, _nodeMapMutex should be used.
503 _ShaderNodeMap _nodeMap;
504};
505
506PXR_NAMESPACE_CLOSE_SCOPE
507
508#endif // PXR_USD_SDR_REGISTRY_H
Contains an asset path and optional evaluated and resolved paths.
Definition: assetPath.h:79
Interface for parser plugins.
Definition: parserPlugin.h:110
The registry provides access to shader node information.
Definition: registry.h:47
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 GetShaderNodeByName(const std::string &name, const SdrTokenVec &shadingSystemPriority=SdrTokenVec(), SdrVersionFilter filter=SdrVersionFilterDefaultOnly)
Get the shader node with the specified name.
SDR_API SdrShaderNodeConstPtr GetShaderNodeFromSourceCode(const std::string &sourceCode, const TfToken &shadingSystem, const SdrTokenMap &metadata=SdrTokenMap())
Parses the given sourceCode string, constructs a SdrShaderNode from it and adds it to the registry.
SDR_API SdrShaderNodeConstPtr GetShaderNodeByNameAndSystem(const std::string &name, const TfToken &shadingSystem, SdrVersionFilter filter=SdrVersionFilterDefaultOnly)
A convenience wrapper around GetShaderNodeByName().
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 GetShaderNodesByFunction(const TfToken &function=TfToken(), SdrVersionFilter filter=SdrVersionFilterDefaultOnly)
Get all shader nodes, optionally restricted to the nodes that fall under a specified family and/or th...
SDR_API SdrTokenVec GetAllShaderNodeShadingSystems() const
Get a sorted list of all shader node shading systems that may be present on the nodes in the registry...
SDR_API void ParseAll()
Parses all unparsed shader nodes.
SDR_API SdrShaderNodeConstPtr GetShaderNodeByIdentifierAndSystem(const SdrIdentifier &identifier, const TfToken &shadingSystem)
Get the shader node with the specified identifier and shadingSystem.
SDR_API SdrShaderNodeConstPtr GetShaderNodeFromAsset(const SdfAssetPath &shaderAsset, const SdrTokenMap &metadata=SdrTokenMap(), const TfToken &subIdentifier=TfToken(), const TfToken &shadingSystem=TfToken())
Parses the given asset, constructs a SdrShaderNode from it and adds it to the registry.
SDR_API SdrIdentifierVec GetShaderNodeIdentifiers(const TfToken &function=TfToken(), SdrVersionFilter filter=SdrVersionFilterDefaultOnly) const
Get identifiers of all the shader nodes that the registry is aware of.
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().
SDR_API SdrStringVec GetShaderNodeNames(const TfToken &function=TfToken()) const
Get the names of all the shader nodes that the registry is aware of.
SDR_API SdrShaderNodePtrVec GetAllShaderNodes()
Parses all unparsed shader nodes and returns all shader nodes in the registry.
static SDR_API SdrRegistry & GetInstance()
Get the single SdrRegistry instance.
SDR_API void AddDiscoveryResult(const SdrShaderNodeDiscoveryResult &discoveryResult)
Copy version of the method above.
SDR_API SdrShaderNodeConstPtr GetShaderNodeByIdentifier(const SdrIdentifier &identifier, const SdrTokenVec &shadingSystemPriority=SdrTokenVec())
Get the shader node with the specified identifier, and an optional shadingSystemPriority list specify...
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 SdrShaderNodeQueryResult RunQuery(const SdrShaderNodeQuery &query)
Run an SdrShaderNodeQuery.
SDR_API SdrShaderNodePtrVec GetShaderNodesByName(const std::string &name, SdrVersionFilter filter=SdrVersionFilterDefaultOnly)
Get all shader nodes matching the given name.
SdrShaderNodeQuery is a constraint-based query builder object that operates on all SdrShaderNodes con...
SdrShaderNodeQueryResult stores the results of an SdrShaderNodeQuery.
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:107
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:183