HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
collectionCache.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 #ifndef PXR_USD_IMAGING_USD_IMAGING_COLLECTION_CACHE_H
8 #define PXR_USD_IMAGING_USD_IMAGING_COLLECTION_CACHE_H
9 
10 /// \file usdImaging/collectionCache.h
11 
12 #include "pxr/pxr.h"
15 
16 #include <tbb/concurrent_unordered_map.h>
17 #include <tbb/concurrent_queue.h>
18 #include <mutex>
19 #include <unordered_map>
20 
22 
23 /// \class UsdImaging_CollectionCache
24 ///
25 /// UsdImaging_CollectionCache provides a cache of Usd collections
26 /// discovered in the scene. It associates the collection paths
27 /// (UsdCollectionAPI::GetCollectionPath) with the computed
28 /// membership query (UsdCollectionAPI::ComputeMembershipQuery).
29 ///
30 /// For efficiency, it groups collections into equivalence
31 /// classes based on the computed query. Collections that yield
32 /// equivalent queries are merged. Each unique query is assigned
33 /// an identifier token.
34 ///
35 /// This cache is used to track the collections used for linking
36 /// UsdLux lights/shadows/filters to geometry.
37 ///
38 /// As an optimization, the query that includes everything is
39 /// treated as a special case and given the empty id, TfToken().
40 ///
42 public:
43  UsdImaging_CollectionCache() = default;
46 
47  /// Query is the MembershipQuery computed from a collection's state.
49 
50  /// Computes the membership query from the current state of the
51  /// given collection, and establishes a cache entry. If a
52  /// prior entry existed for the collection at this path,
53  /// it is removed first.
54  /// Returns true for newly created collection or
55  /// if the hash of the collection is different from the previous collection
57  bool
58  UpdateCollection(UsdCollectionAPI const& collection);
59 
60  /// Returns the hash of the removed collection, or 0 if no collection existed
62  size_t
63  RemoveCollection(UsdStageWeakPtr const& stage, SdfPath const& path);
64 
65  /// Return the cached entry for the given collection.
67  TfToken
68  GetIdForCollection(UsdCollectionAPI const& collection);
69 
70  /// Return a list of identifiers of all collections that contain
71  // the given path.
75 
76  /// Returns a set of dirty paths
77  /// Should only be used if AreAllPathsDirty returned false
79  SdfPathSet const&
80  GetDirtyPaths() const;
81 
82  /// Clears the internal dirty flags
84  void
86 
87 private:
88  // The cache boils down to tracking the correspondence of
89  // collection paths, their computed queries, and the id
90  // assigned to each unique query:
91  //
92  // CollectionPath <=> MembershipQuery <=> AssignedId
93  //
94  // In this scheme, the assigned id provides a compact but
95  // potentially human-meaningful reference to the query,
96  // which we can pass to the renderer.
97  //
98  std::unordered_map<Query, TfToken, Query::Hash> _idForQuery;
99  std::unordered_map<TfToken, Query, TfToken::HashFunctor> _queryForId;
100  std::unordered_map<SdfPath, TfToken, SdfPath::Hash> _idForPath;
101  std::unordered_map<Query, SdfPathSet, Query::Hash> _pathsForQuery;
102 
103  void
104  _MarkCollectionContentDirty(
105  UsdStageWeakPtr const& stage,
107 
108  SdfPathSet _dirtyPaths;
109 
110  std::mutex _mutex;
111 };
112 
113 
115 
116 #endif // PXR_USD_IMAGING_USD_IMAGING_COLLECTION_CACHE_H
GLenum query
Definition: glad.h:2772
#define USDIMAGING_API
Definition: api.h:23
USDIMAGING_API bool UpdateCollection(UsdCollectionAPI const &collection)
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
Represents a flattened view of a collection. For more information about collections, please see UsdCollectionAPI as a way to encode and retrieve a collection from scene description. A UsdCollectionMembershipQuery object can be used to answer queries about membership of paths in the collection efficiently.
UsdStagePtr UsdStageWeakPtr
Definition: common.h:38
USDIMAGING_API size_t RemoveCollection(UsdStageWeakPtr const &stage, SdfPath const &path)
Returns the hash of the removed collection, or 0 if no collection existed.
Definition: token.h:70
UsdImaging_CollectionCache & operator=(const UsdImaging_CollectionCache &)=delete
Definition: path.h:273
std::set< class SdfPath > SdfPathSet
A set of SdfPaths.
Definition: path.h:192
USDIMAGING_API void ClearDirtyPaths()
Clears the internal dirty flags.
UsdImaging_CollectionCache()=default
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
USDIMAGING_API TfToken GetIdForCollection(UsdCollectionAPI const &collection)
Return the cached entry for the given collection.
UsdCollectionAPI::MembershipQuery Query
Query is the MembershipQuery computed from a collection's state.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
USDIMAGING_API VtArray< TfToken > ComputeCollectionsContainingPath(SdfPath const &path) const
Return a list of identifiers of all collections that contain.
USDIMAGING_API SdfPathSet const & GetDirtyPaths() const