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
286 SDR_API
287 SdrTokenVec GetAllShaderNodeSourceTypes() const;
288
294 SDR_API
296
297protected:
298 // Allow TF to construct the class
299 friend class TfSingleton<SdrRegistry>;
300 SdrRegistry(const SdrRegistry&) = delete;
301 SdrRegistry& operator=(const SdrRegistry&) = delete;
302
303 SDR_API
304 SdrRegistry();
305
306 SDR_API
307 ~SdrRegistry();
308
309private:
310 class _DiscoveryContext;
311 friend class _DiscoveryContext;
312
313 using _TypeToParserPluginMap =
314 std::unordered_map<TfToken, SdrParserPlugin*, TfToken::HashFunctor>;
315
316 // Node cache data structure, stored SdrShaderNodes keyed by
317 // identifier and source type.
318 using _ShaderNodeMapKey = std::pair<SdrIdentifier, TfToken>;
319 using _ShaderNodeMap =
320 std::unordered_map<_ShaderNodeMapKey, SdrShaderNodeUniquePtr, TfHash>;
321
322 // Discovery results data structure, SdrShaderNodeDiscoveryResults
323 // multimap keyed ny identifier
324 using _DiscoveryResultsByIdentifier = std::unordered_multimap<
326 using _DiscoveryResultsByIdentifierRange =
327 std::pair<_DiscoveryResultsByIdentifier::const_iterator,
328 _DiscoveryResultsByIdentifier::const_iterator>;
329
330 // Discovery results data structure: a multimap of raw pointers to
331 // SdrShaderNodeDiscoveryResults (i.e. pointers to the discovery results
332 // stored in a _DiscoveryResultsByIdentifier) keyed by name.
333 using _DiscoveryResultPtrsByName = std::unordered_multimap<
334 std::string, const SdrShaderNodeDiscoveryResult *, TfHash>;
335 using _DiscoveryResultPtrsByNameRange =
336 std::pair<_DiscoveryResultPtrsByName::const_iterator,
337 _DiscoveryResultPtrsByName::const_iterator>;
338
339 // The discovery result data structures are not concurrent and must be kept
340 // in sync, thus they need some locking infrastructure.
341 mutable std::mutex _discoveryResultMutex;
342
343 // The node map is not a concurrent data structure, thus it needs some
344 // locking infrastructure. Ensure that _discoveryResultMutex is not
345 // acquired after _nodeMapMutex is acquired, to avoid deadlock.
346 mutable std::mutex _nodeMapMutex;
347
348 // Runs each discovery plugin provided and adds the results to the
349 // internal discovery result maps
350 void _RunDiscoveryPlugins(
351 const DiscoveryPluginRefPtrVec& discoveryPlugins);
352
353 // Takes the discovery and puts in the maps that hold the discovery
354 // results, keeping them in sync.
355 void _AddDiscoveryResultNoLock(SdrShaderNodeDiscoveryResult&& dr);
356
357 // Finds and instantiates the discovery plugins
358 void _FindAndInstantiateDiscoveryPlugins();
359
360 // Finds and instantiates the parser plugins
361 void _FindAndInstantiateParserPlugins();
362
363 // Instantiates the specified parser plugins and adds them to
364 // the registry.
365 void _InstantiateParserPlugins(const std::set<TfType>& parserPluginTypes);
366
367 // Parses the node for the discovery result if adding it to the node map if
368 // able and adds the discovery result to the discovery result maps.
369 // Intended for the GetShadeNodeFromAsset and GetShaderNodeFromSourceCode
370 // APIs which can add nodes that don't already appear in the discovery
371 // results.
372 SdrShaderNodeConstPtr _ParseNodeFromAssetOrSourceCode(
374
375 // Implementation helper for getting the first node of the given sourceType
376 // in the range of node discovery results for a paricular identifier.
377 SdrShaderNodeConstPtr _GetNodeInIdentifierRangeWithSourceType(
378 _DiscoveryResultsByIdentifierRange range, const TfToken& sourceType);
379
380 // Implementation helper for getting the first node of the given sourceType
381 // and matching the given version filter in the range of node discovery
382 // results for a paricular name.
383 SdrShaderNodeConstPtr _GetNodeInNameRangeWithSourceType(
384 _DiscoveryResultPtrsByNameRange range, const TfToken& sourceType,
385 SdrVersionFilter filter);
386
387 // Thread-safe find of a shader node in the cache by key.
388 SdrShaderNodeConstPtr _FindNodeInCache(
389 const _ShaderNodeMapKey &key) const;
390
391 // Thread-safe insertion of a node into the cache with a given key. If a
392 // node with the same key already exists in the cache, the pointer to the
393 // existing node will be returned, otherwise the pointer to pointer to the
394 // inserted node is returned.
395 SdrShaderNodeConstPtr _InsertNodeInCache(
396 _ShaderNodeMapKey &&key, SdrShaderNodeUniquePtr &&node);
397
398 // Finds an existing node in the node cache for the discovery result if one
399 // exists. Otherwise it parses the new node, inserts it into the cache, and
400 // returns it. If there was an error parsing or validating the node,
401 // `nullptr` will be returned.
402 SdrShaderNodeConstPtr _FindOrParseNodeInCache(
404
405 // Return the parser plugin for a discovery type. Returns null if no parser
406 // plugin has that discovery type.
408 _GetParserForDiscoveryType(const TfToken& discoveryType) const;
409
410 // The discovery plugins that were found through libplug and/or provided by
411 // the client
412 DiscoveryPluginRefPtrVec _discoveryPlugins;
413
414 // The parser plugins that have been discovered via the plugin system. Maps
415 // a discovery result's "discovery type" to a specific parser.
416 _TypeToParserPluginMap _parserPluginMap;
417
418 // The parser plugins. This has ownership of the plugin objects.
419 std::vector<std::unique_ptr<SdrParserPlugin>> _parserPlugins;
420
421 // The preliminary discovery results prior to parsing. These are stored
422 // in a multimap by identifier and a multimap by name. If accessing or
423 // mutating, _discoveryResultMutex should be used.
424 _DiscoveryResultsByIdentifier _discoveryResultsByIdentifier;
425 _DiscoveryResultPtrsByName _discoveryResultPtrsByName;
426
427 // Set of all possible source types as determined by the existing discovery
428 // results. Populated along with the discovery result multimaps. If
429 // accessing or mutating, _discoveryResultMutex should be used.
430 TfToken::Set _allSourceTypes;
431
432 // Maps a node's identifier and source type to a node instance. If accessing
433 // or mutating, _nodeMapMutex should be used.
434 _ShaderNodeMap _nodeMap;
435};
436
437PXR_NAMESPACE_CLOSE_SCOPE
438
439#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: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 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 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:184