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
50class SdrRegistry : public TfWeakBase
51{
52public:
53 using DiscoveryPluginRefPtrVec = SdrDiscoveryPluginRefPtrVector;
54
56 SDR_API
58
66 SDR_API
67 void SetExtraDiscoveryPlugins(DiscoveryPluginRefPtrVec plugins);
68
76 SDR_API
77 void SetExtraDiscoveryPlugins(const std::vector<TfType>& pluginTypes);
78
79
87 SDR_API
89
94 SDR_API
96 const SdrShaderNodeDiscoveryResult& discoveryResult);
97
104 SDR_API
105 void SetExtraParserPlugins(const std::vector<TfType>& pluginTypes);
106
111 SDR_API
112 SdrStringVec GetSearchURIs() const;
113
121 SDR_API
122 SdrIdentifierVec
124 SdrVersionFilter filter =
125 SdrVersionFilterDefaultOnly) const;
126
133 SDR_API
134 SdrStringVec GetShaderNodeNames(const TfToken& family = TfToken()) const;
135
157 SDR_API
158 SdrShaderNodeConstPtr GetShaderNodeByIdentifier(
159 const SdrIdentifier& identifier,
160 const SdrTokenVec& typePriority = SdrTokenVec());
161
164 SDR_API
165 SdrShaderNodeConstPtr GetShaderNodeByIdentifierAndType(
166 const SdrIdentifier& identifier,
167 const TfToken& nodeType);
168
179 SDR_API
180 SdrShaderNodeConstPtr GetShaderNodeByName(
181 const std::string& name,
182 const SdrTokenVec& typePriority = SdrTokenVec(),
183 SdrVersionFilter filter = SdrVersionFilterDefaultOnly);
184
193 SDR_API
194 SdrShaderNodeConstPtr GetShaderNodeByNameAndType(
195 const std::string& name,
196 const TfToken& nodeType,
197 SdrVersionFilter filter = SdrVersionFilterDefaultOnly);
198
221 SDR_API
222 SdrShaderNodeConstPtr GetShaderNodeFromAsset(
223 const SdfAssetPath &shaderAsset,
224 const SdrTokenMap &metadata=SdrTokenMap(),
225 const TfToken &subIdentifier=TfToken(),
226 const TfToken &sourceType=TfToken());
227
243 SDR_API
244 SdrShaderNodeConstPtr GetShaderNodeFromSourceCode(
245 const std::string &sourceCode,
246 const TfToken &sourceType,
247 const SdrTokenMap &metadata=SdrTokenMap());
248
252 SDR_API
253 SdrShaderNodePtrVec GetShaderNodesByIdentifier(const SdrIdentifier& identifier);
254
259 SDR_API
260 SdrShaderNodePtrVec GetShaderNodesByName(
261 const std::string& name,
262 SdrVersionFilter filter = SdrVersionFilterDefaultOnly);
263
270 SDR_API
271 SdrShaderNodePtrVec GetShaderNodesByFamily(
272 const TfToken& family = TfToken(),
273 SdrVersionFilter filter = SdrVersionFilterDefaultOnly);
274
283 SDR_API
284 SdrShaderNodePtrVec GetAllShaderNodes();
285
297 SDR_API
298 SdrTokenVec GetAllShaderNodeSourceTypes() const;
299
305 SDR_API
307
314 SDR_API
315 void ParseAll();
316
317protected:
318 // Allow TF to construct the class
319 friend class TfSingleton<SdrRegistry>;
320 SdrRegistry(const SdrRegistry&) = delete;
321 SdrRegistry& operator=(const SdrRegistry&) = delete;
322
323 SDR_API
324 SdrRegistry();
325
326 SDR_API
327 ~SdrRegistry();
328
329private:
330 class _DiscoveryContext;
331 friend class _DiscoveryContext;
332
333 using _TypeToParserPluginMap =
334 std::unordered_map<TfToken, SdrParserPlugin*, TfToken::HashFunctor>;
335
336 // Node cache data structure, stored SdrShaderNodes keyed by
337 // identifier and source type.
338 using _ShaderNodeMapKey = std::pair<SdrIdentifier, TfToken>;
339 using _ShaderNodeMap =
340 std::unordered_map<_ShaderNodeMapKey, SdrShaderNodeUniquePtr, TfHash>;
341
342 // Discovery results data structure, SdrShaderNodeDiscoveryResults
343 // multimap keyed ny identifier
344 using _DiscoveryResultsByIdentifier = std::unordered_multimap<
346 using _DiscoveryResultsByIdentifierRange =
347 std::pair<_DiscoveryResultsByIdentifier::const_iterator,
348 _DiscoveryResultsByIdentifier::const_iterator>;
349
350 // Discovery results data structure: a multimap of raw pointers to
351 // SdrShaderNodeDiscoveryResults (i.e. pointers to the discovery results
352 // stored in a _DiscoveryResultsByIdentifier) keyed by name.
353 using _DiscoveryResultPtrsByName = std::unordered_multimap<
354 std::string, const SdrShaderNodeDiscoveryResult *, TfHash>;
355 using _DiscoveryResultPtrsByNameRange =
356 std::pair<_DiscoveryResultPtrsByName::const_iterator,
357 _DiscoveryResultPtrsByName::const_iterator>;
358
359 // The discovery result data structures are not concurrent and must be kept
360 // in sync, thus they need some locking infrastructure.
361 mutable std::mutex _discoveryResultMutex;
362
363 // The node map is not a concurrent data structure, thus it needs some
364 // locking infrastructure. Ensure that _discoveryResultMutex is not
365 // acquired after _nodeMapMutex is acquired, to avoid deadlock.
366 mutable std::mutex _nodeMapMutex;
367
368 // Runs each discovery plugin provided and adds the results to the
369 // internal discovery result maps
370 void _RunDiscoveryPlugins(
371 const DiscoveryPluginRefPtrVec& discoveryPlugins);
372
373 // Takes the discovery and puts in the maps that hold the discovery
374 // results, keeping them in sync.
375 void _AddDiscoveryResultNoLock(SdrShaderNodeDiscoveryResult&& dr);
376
377 // Finds and instantiates the discovery plugins
378 void _FindAndInstantiateDiscoveryPlugins();
379
380 // Finds and instantiates the parser plugins
381 void _FindAndInstantiateParserPlugins();
382
383 // Instantiates the specified parser plugins and adds them to
384 // the registry.
385 void _InstantiateParserPlugins(const std::set<TfType>& parserPluginTypes);
386
387 // Parses the node for the discovery result if adding it to the node map if
388 // able and adds the discovery result to the discovery result maps.
389 // Intended for the GetShadeNodeFromAsset and GetShaderNodeFromSourceCode
390 // APIs which can add nodes that don't already appear in the discovery
391 // results.
392 SdrShaderNodeConstPtr _ParseNodeFromAssetOrSourceCode(
394
395 // Implementation helper for getting the first node of the given sourceType
396 // in the range of node discovery results for a paricular identifier.
397 SdrShaderNodeConstPtr _GetNodeInIdentifierRangeWithSourceType(
398 _DiscoveryResultsByIdentifierRange range, const TfToken& sourceType);
399
400 // Implementation helper for getting the first node of the given sourceType
401 // and matching the given version filter in the range of node discovery
402 // results for a paricular name.
403 SdrShaderNodeConstPtr _GetNodeInNameRangeWithSourceType(
404 _DiscoveryResultPtrsByNameRange range, const TfToken& sourceType,
405 SdrVersionFilter filter);
406
407 // Thread-safe find of a shader node in the cache by key.
408 SdrShaderNodeConstPtr _FindNodeInCache(
409 const _ShaderNodeMapKey &key) const;
410
411 // Thread-safe insertion of a node into the cache with a given key. If a
412 // node with the same key already exists in the cache, the pointer to the
413 // existing node will be returned, otherwise the pointer to pointer to the
414 // inserted node is returned.
415 SdrShaderNodeConstPtr _InsertNodeInCache(
416 _ShaderNodeMapKey &&key, SdrShaderNodeUniquePtr &&node);
417
418 // Finds an existing node in the node cache for the discovery result if one
419 // exists. Otherwise it parses the new node, inserts it into the cache, and
420 // returns it. If there was an error parsing or validating the node,
421 // `nullptr` will be returned.
422 SdrShaderNodeConstPtr _FindOrParseNodeInCache(
424
425 // Return the parser plugin for a discovery type. Returns null if no parser
426 // plugin has that discovery type.
428 _GetParserForDiscoveryType(const TfToken& discoveryType) const;
429
430 // The discovery plugins that were found through libplug and/or provided by
431 // the client
432 DiscoveryPluginRefPtrVec _discoveryPlugins;
433
434 // The parser plugins that have been discovered via the plugin system. Maps
435 // a discovery result's "discovery type" to a specific parser.
436 _TypeToParserPluginMap _parserPluginMap;
437
438 // The parser plugins. This has ownership of the plugin objects.
439 std::vector<std::unique_ptr<SdrParserPlugin>> _parserPlugins;
440
441 // The preliminary discovery results prior to parsing. These are stored
442 // in a multimap by identifier and a multimap by name. If accessing or
443 // mutating, _discoveryResultMutex should be used.
444 _DiscoveryResultsByIdentifier _discoveryResultsByIdentifier;
445 _DiscoveryResultPtrsByName _discoveryResultPtrsByName;
446
447 // Set of all possible source types as determined by the existing discovery
448 // results. Populated along with the discovery result multimaps. If
449 // accessing or mutating, _discoveryResultMutex should be used.
450 TfToken::Set _allSourceTypes;
451
452 // Maps a node's identifier and source type to a node instance. If accessing
453 // or mutating, _nodeMapMutex should be used.
454 _ShaderNodeMap _nodeMap;
455};
456
457PXR_NAMESPACE_CLOSE_SCOPE
458
459#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:51
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 void ParseAll()
Parses all unparsed shader nodes.
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 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 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 SdrShaderNodeQueryResult RunQuery(const SdrShaderNodeQuery &query)
Run an SdrShaderNodeQuery.
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.
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: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:183