HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sparseOutputTraverser.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_VDF_SPARSE_OUTPUT_TRAVERSER_H
8 #define PXR_EXEC_VDF_SPARSE_OUTPUT_TRAVERSER_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/vdf/api.h"
16 #include "pxr/exec/vdf/types.h"
17 
18 #include "pxr/base/tf/hashmap.h"
19 
20 #include <queue>
21 #include <vector>
22 
24 
25 class VdfInput;
26 class VdfMask;
27 class VdfOutput;
28 
29 /// A class used for fast traversals of VdfNetworks in the input-to-output
30 /// direction.
31 ///
32 /// The VdfSparseOutputTraverser class can be used to quickly traverse networks.
33 /// The main API are the TraverseOnce() and Traverse() methods. The static
34 /// and non-static methods should have exactly the same behavior. The
35 /// non-static method has the opportunity to perform much faster traversals
36 /// if similar traversals are repeatedly invoked using the same traverser
37 /// object (when caching is enabled).
38 ///
40 {
41 public:
42  /// The number of requests to remain in the cache.
43  ///
44  /// An attempt to store more than MaxRequestsBeforeEviction requests in
45  /// the traversal cache will result in the eviction of the m oldest
46  /// cache entries, such that:
47  /// cacheSize - m = MaxRequestsBeforeEviction.
48  ///
49  /// This enviction policy is enforced, every time a new
50  /// traversal is started with TraverseWithCaching.
51  ///
52  /// NOTE: Setting MaxRequestsBeforeEviction to -1 will disable
53  /// the eviction algorithm all together. 0 will disable caching.
54  static const int MaxRequestsBeforeEviction = 10;
55 
56  /// Creates a new VdfSparseOutputTraverser.
57  ///
58  /// \p enableCaching controls whether or not traversals should cache
59  /// information that can be used to speed up similar traversals later.
60  /// By default traversal caching is enabled.
61  ///
62  VDF_API
63  VdfSparseOutputTraverser(bool enableCaching = true);
64 
65  /// Callback used when traversing a network.
66  ///
67  /// The callback is supplied with the current output, a mask for the
68  /// output, and the input through which the current output was reached,
69  /// if any.
70  ///
71  /// Called for each output traversed. If the callback returns false,
72  /// traversal will be stopped at that output.
73  ///
74  typedef std::function<
75  bool (const VdfOutput &, const VdfMask &, const VdfInput *) >
77 
78 
79  /// \name Output Traversal
80  /// @{
81 
82  /// Traverses the network, starting from the masked outputs in
83  /// \p outputs.
84  ///
85  /// Performs an optimized vectorized traversal.
86  ///
87  /// Calls \p outputCallback for each output that is visited, passing the
88  /// accumulated dependency mask. If the callback returns \c true,
89  /// traversal continues; otherwise, it terminates. The \p outputCallback
90  /// is optional and may be null.
91  ///
92  /// Calls \p nodeCallback for each node that is visited. The \p nodeCallback
93  /// is optional and may be null.
94  ///
95  VDF_API
96  static void Traverse(
97  const VdfMaskedOutputVector &outputs,
98  const OutputCallback &outputCallback,
99  const VdfNodeCallback &nodeCallback);
100 
101  /// Traverses the network, starting from the masked outputs in
102  /// \p request.
103  ///
104  /// In addition to the functionality of the static method Traverse(),
105  /// this non-static method may be faster because the
106  /// VdfSparseOutputTraverser object will cache some traversals.
107  ///
108  VDF_API
109  void TraverseWithCaching(
110  const VdfMaskedOutputVector &outputs,
111  const OutputCallback &outputCallback,
112  const VdfNodeCallback &nodeCallback);
113 
114  /// @}
115 
116 
117  /// Invalidates all cached traversals.
118  ///
119  /// To be called if network changes.
120  ///
121  VDF_API
122  void InvalidateAll();
123 
124 private:
125 
126  struct _CacheEntry;
127 
128  // A cache line stored in the traversal cache map
129  struct _Cache
130  {
131  // The indices to all the root cache entries, representing the root
132  // nodes in the cached request
133  std::vector<int> rootIndices;
134 
135  // A vector of cache entries
136  typedef std::vector<_CacheEntry> CacheEntries;
137  CacheEntries cacheEntries;
138  };
139 
140  // Returns a new or existing cache entry keyed off of the sorted request.
141  // This method will also enforce the eviction policy.
142  _Cache* _GetOrCreateCacheEntry(const VdfMaskedOutputVector &sortedOutputs);
143 
144  class _TraversalHelper;
145 
146 private:
147 
148  // Flag to switch caching on and off (defaults to on).
149  bool _enableCaching;
150 
151  // A map from masked outputs to _Cache objects.
152  using _TraversalCache =
154 
155  // Cache used to speed up repeated traversals.
156  _TraversalCache _cache;
157 
158  // The cache history is defined as a queue adaptor with an underlaying deque
159  using _CacheHistory = std::queue<_TraversalCache::iterator>;
160 
161  // Maintain a history of added cache entries to allow for eviction
162  // of oldest cache entries
163  _CacheHistory _cacheHistory;
164 
165 };
166 
168 
169 #endif
VDF_API VdfSparseOutputTraverser(bool enableCaching=true)
static VDF_API void Traverse(const VdfMaskedOutputVector &outputs, const OutputCallback &outputCallback, const VdfNodeCallback &nodeCallback)
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
std::function< bool(const VdfOutput &, const VdfMask &, const VdfInput *) > OutputCallback
VDF_API void InvalidateAll()
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:36
static const int MaxRequestsBeforeEviction
#define VDF_API
Definition: api.h:25
OutGridT const XformOp bool bool
Definition: input.h:35
VDF_API void TraverseWithCaching(const VdfMaskedOutputVector &outputs, const OutputCallback &outputCallback, const VdfNodeCallback &nodeCallback)
std::function< void(const VdfNode &)> VdfNodeCallback
Type of callback used when processing nodes.
Definition: types.h:68
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::vector< VdfMaskedOutput > VdfMaskedOutputVector