HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
executorInvalidator.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_EXECUTOR_INVALIDATOR_H
8 #define PXR_EXEC_VDF_EXECUTOR_INVALIDATOR_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/vdf/api.h"
15 #include "pxr/exec/vdf/lruCache.h"
16 #include "pxr/exec/vdf/mask.h"
21 #include "pxr/exec/vdf/types.h"
22 
23 #include "pxr/base/tf/hashmap.h"
25 
26 #include <map>
27 #include <vector>
28 
30 
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 ///
35 /// \class VdfExecutorInvalidator
36 ///
37 /// Invalidates state and temporary buffers of all outputs dependent on the
38 /// outputs supplied in the invalidation request. Stores internal state in order
39 /// to accelerate subsequent invalidation requests.
40 ///
42 {
43 public:
44 
45  /// Construct an executor invalidator for the given executor. The
46  /// invalidator will mutate the state of the given executor.
47  ///
49  _executor(executor),
50  _timestamp(0),
51  _replayLRU(16)
52  {}
53 
54  /// Invalidate all the outputs in the \p request, as well as all the
55  /// outputs dependent on the \p request.
56  ///
57  VDF_API
58  void Invalidate(const VdfMaskedOutputVector &request);
59 
60  /// Reset the internal state of the invalidator. This method must be called
61  /// on topological state changes.
62  ///
63  VDF_API
64  void Reset();
65 
66 private:
67 
68  // Information about a visited output.
69  struct _Visited {
70  explicit _Visited(uint32_t ts) : timestamp(ts), index(0) {}
71  uint32_t timestamp;
72  uint32_t index;
73  VdfMask mask;
74  };
75 
76  // A cached dependency on a pool output.
77  struct _PoolDependency {
78  VdfPoolChainIndex poolChainIndex;
79  VdfMaskedOutput maskedOutput;
80  };
81 
82  // An entry with cached dependencies.
83  struct _Dependencies {
87  };
88 
89  // A cached invalidation entry for fast invalidation replay. The entry
90  // stores two masks, one for visits for which the invalidation callback
91  // returned true, and one for visits where it returned false.
92  struct _ReplayEntry {
93  explicit _ReplayEntry(const VdfOutput *o) : output(o) {}
94  const VdfOutput *output;
95  VdfMask masks[2];
96  };
97 
98  // The cache of invalidation entries for fast replay. Every output has a
99  // unique entry in the cache, so that it can be replayed in parallel
100  // without risk of racing on the same output.
101  struct _ReplayCache {
102  std::vector<_ReplayEntry> entries;
104  };
105 
106  // Array of visited outputs to information about the visit, indexed by
107  // output id.
108  using _VisitedMap = std::vector<_Visited>;
109 
110  // The type of output stack used to guide the traversal.
111  using _OutputStack = std::vector<VdfMaskedOutput>;
112 
113  // The type of queue used to guide the traversal along the pool.
114  using _PoolQueue = std::map<const VdfPoolChainIndex, VdfMaskedOutput>;
115 
116  // Returns true if all the outputs in the given request are already invalid.
117  bool _IsInvalid(const VdfMaskedOutputVector &request) const;
118 
119  // Returns a pointer to an existing replay cache for the given outputs.
120  // Creates a new cache if one does not already exist.
121  _ReplayCache* _GetReplayCache(const VdfMaskedOutputVector &outputs);
122 
123  // Returns a valid pointer to an index if this output should be visited, or
124  // nullptr if the output has already been visited with the given mask. If
125  // the returned index equals nextIndex, the output is being visited for the
126  // first time.
127  uint32_t *_Visit(const VdfMaskedOutput &maskedOutput, uint32_t nextIndex);
128 
129  // Initiates a new traversal starting at the outputs in request.
130  void _Traverse(
131  const VdfMaskedOutputVector &request,
132  _ReplayCache *replayCache);
133 
134  // Visits a single output.
135  bool _TraverseOutput(
136  const VdfMaskedOutput &maskedOutput,
137  _OutputStack *stack,
138  _PoolQueue *queue,
140 
141  // Replay a cached invalidation traversal. Returns false if the cache could
142  // not be successfully replayed and a new traversal must be started.
143  bool _Replay(const _ReplayCache &replayCache);
144 
145  // Retrieves the dependencies for a single output, if cached, or computes
146  // dependencies of uncached.
147  const _Dependencies &_GetDependencies(
148  const VdfMaskedOutput &maskedOutput);
149 
150  // Computes the dependencies for a single output.
151  void _ComputeDependencies(
152  const VdfMaskedOutput &maskedOutput,
153  _Dependencies *dependencies);
154 
155  // Pointer to the executor that is being invalidated.
156  VdfExecutorInterface *_executor;
157 
158  // The map of visited outputs.
159  _VisitedMap _visited;
160 
161  // A timestamp denoting the current round of invalidation. Will be
162  // incremented for every subsequent round of invalidaiton.
163  uint32_t _timestamp;
164 
165  // The cached dependencies.
166  using _DependencyMap = TfHashMap<
167  VdfMaskedOutput, _Dependencies, VdfMaskedOutput::Hash>;
168  _DependencyMap _dependencyMap;
169 
170  // A list of recently used replay caches.
171  using _ReplayLRU = VdfLRUCache<
173  _ReplayLRU _replayLRU;
174 
175  // The memoized mask operations.
176  VdfMaskMemoizer<TfHashMap> _maskMemoizer;
177 
178 };
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 
183 
184 #endif
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:36
#define VDF_API
Definition: api.h:25
VdfExecutorInvalidator(VdfExecutorInterface *executor)
GLint GLuint mask
Definition: glcorearb.h:124
VDF_API void Reset()
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the queue
Definition: thread.h:632
Class to hold on to an externally owned output and a mask.
Definition: maskedOutput.h:31
GLuint index
Definition: glcorearb.h:786
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Abstract base class for classes that execute a VdfNetwork to compute a requested set of values...
std::vector< VdfMaskedOutput > VdfMaskedOutputVector
VDF_API void Invalidate(const VdfMaskedOutputVector &request)