HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
leafNodeCache.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 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_EXEC_EF_LEAF_NODE_CACHE_H
8 #define PXR_EXEC_EF_LEAF_NODE_CACHE_H
9 
10 ///\file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/ef/api.h"
17 
18 #include "pxr/base/tf/hashmap.h"
21 #include "pxr/exec/vdf/types.h"
22 
23 #include <atomic>
24 #include <vector>
25 
27 
28 ///////////////////////////////////////////////////////////////////////////////
29 ///
30 /// \class EfLeafNodeCache
31 ///
32 /// This cache is a thin wrapper around the EfDependencyCache.
33 ///
34 /// It caches node and output dependencies on EfLeafNodes, given an arbitrary
35 /// input request.
36 ///
38 {
39 public:
40  /// Constructor.
41  ///
42  EF_API
44 
45  /// Returns the current edit version of the leaf node cache. This number
46  /// will be incremented whenever leaf node dependency changes due to
47  /// network edits, or time dependency modifications. Note, that no
48  /// assumptions can be made about the absolute value returned from this
49  /// function. The only guarantee provided is that if two versions compare
50  /// equal, leaf node dependencies have not changed.
51  ///
52  size_t GetVersion() const {
53  return _version.load(std::memory_order_relaxed);
54  }
55 
56  /// Find outputs dependent on the given \p outputs.
57  ///
58  EF_API
60  const VdfMaskedOutputVector &outputs,
61  bool updateIncrementally);
62 
63  /// Find leaf nodes dependent on the given \p outputs
64  ///
65  EF_API
66  const std::vector<const VdfNode *> &FindNodes(
67  const VdfMaskedOutputVector &outputs,
68  bool updateIncrementally);
69 
70  /// Find all leaf nodes dependent on the given \p outputs, but only return
71  /// the nodes dependent on the requested outputs not filtered out by the
72  /// \p outputsMask. A previously passed in request of \p outputs will return
73  /// a cache hit regardless of the value of \p outputsMask.
74  ///
75  EF_API
76  const std::vector<const VdfNode *> &FindNodes(
77  const VdfMaskedOutputVector &outputs,
78  const TfBits &outputsMask);
79 
80  /// Clear the entire cache.
81  ///
82  EF_API
83  void Clear();
84 
85  /// Call this to notify the cache of connections that have been deleted.
86  ///
87  /// \note It is safe to call WillDeleteConnection() and DidConnect()
88  /// concurrently.
89  ///
90  EF_API
91  void WillDeleteConnection(const VdfConnection &connection);
92 
93  /// Call this to notify the cache of newly added connections.
94  ///
95  /// \note It is safe to call WillDeleteConnection() and DidConnect()
96  /// concurrently.
97  ///
98  EF_API
99  void DidConnect(const VdfConnection &connection);
100 
101 private:
102  // Holds the sets of leaf nodes, one for each output in the request, along
103  // with cached, combined sets of leaf nodes given a mask of the requested
104  // outputs. The sets contain indices into the leaf node indexer.
105  struct _VectorizedCacheEntry {
106  std::vector<TfBits> leafNodes;
108  };
109 
110  // Stores an array of leaf nodes, and outputs connected to these leaf nodes.
111  struct _SparseCacheEntry {
112  std::vector<const VdfNode *> nodes;
113  VdfOutputToMaskMap outputs;
114  };
115 
116  // Clears the vectorized and sparse caches along with the traverser used to
117  // populate those caches, if their internal state has been flagged as being
118  // invalid.
119  void _ClearCachesIfInvalid();
120 
121  // Combine separate sets of leaf nodes into a single set.
122  TfBits _CombineLeafNodes(
123  const TfBits &outputsMask,
124  const std::vector<TfBits> &leafNodes) const;
125 
126  // Populates the vectorized cache by doing a vectorized traversal.
127  _VectorizedCacheEntry *_PopulateVectorizedEntry(
128  const VdfMaskedOutputVector &outputs);
129 
130  // Populates the sparse cache by using the results from a previous,
131  // vectorized traversal.
132  _SparseCacheEntry *_PopulateSparseEntry(
133  const VdfMaskedOutputVector &outputs,
134  const TfBits &leafNodes);
135 
136  // The version of the cache. Incremented with every edit.
137  std::atomic<size_t> _version;
138 
139  // Indicates that the internal state pertaining to vectorized and sparse
140  // caches is invalid and that those caches be cleared.
141  std::atomic<bool> _cachesAreInvalid;
142 
143  // The dependency cache used for fast lookups of input-to-output
144  // dependencies.
145  EfDependencyCache _dependencyCache;
146 
147  // The leaf node indexer associates each leaf node with a unique index.
148  Ef_LeafNodeIndexer _indexer;
149 
150  // A cache of requested outputs to leaf node dependencies for each
151  // individual output in the request.
152  using _VectorizedCache = TfHashMap<
154  _VectorizedCacheEntry,
156  _VectorizedCache _vectorizedCache;
157 
158  // A cache of requested outputs to leaf nodes and leaf node connected
159  // outputs.
160  using _SparseCache = TfHashMap<
162  _SparseCacheEntry,
164  _SparseCache _sparseCache;
165 
166  // The traverser used to populate the vectorized cache.
168 };
169 
171 
172 #endif
EF_API EfLeafNodeCache()
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
EF_API const VdfOutputToMaskMap & FindOutputs(const VdfMaskedOutputVector &outputs, bool updateIncrementally)
EF_API void Clear()
Fast bit array that keeps track of the number of bits set and can find the next set in a timely manne...
Definition: bits.h:48
EF_API void WillDeleteConnection(const VdfConnection &connection)
EF_API void DidConnect(const VdfConnection &connection)
size_t GetVersion() const
Definition: leafNodeCache.h:52
std::unordered_map< const VdfOutput *, VdfMask, TfHash > VdfOutputToMaskMap
A map from output pointer to mask.
Definition: types.h:104
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
EF_API const std::vector< const VdfNode * > & FindNodes(const VdfMaskedOutputVector &outputs, bool updateIncrementally)
std::vector< VdfMaskedOutput > VdfMaskedOutputVector
#define EF_API
Definition: api.h:25