Loading...
Searching...
No Matches
outputValueCache.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_OUTPUT_VALUE_CACHE_H
8#define PXR_EXEC_EF_OUTPUT_VALUE_CACHE_H
9
11
12#include "pxr/pxr.h"
13
14#include "pxr/exec/ef/api.h"
15
16#include "pxr/base/tf/bits.h"
17#include "pxr/base/tf/hash.h"
18#include "pxr/base/tf/hashmap.h"
19#include "pxr/exec/vdf/mask.h"
22
23#include <tbb/spin_rw_mutex.h>
24
25PXR_NAMESPACE_OPEN_SCOPE
26
27class VdfOutput;
28class VdfNode;
29class VdfVector;
30
39{
40public:
43 EF_API
45
48 EF_API
50
55 {
56 public:
60 const ExclusiveAccess &operator=(const ExclusiveAccess &) = delete;
61
65 _cache(cache),
66 _lock(cache->_mutex, /* write = */ true)
67 {}
68
71 bool IsEmpty() const {
72 return _cache->_IsEmpty();
73 }
74
78 bool IsUncached(const VdfRequest &request) const {
79 return _cache->_IsUncached(request);
80 }
81
85 VdfRequest GetUncached(
86 const VdfRequest &request) const {
87 return _cache->_GetUncached(request);
88 }
89
94 const VdfOutput &output,
95 const VdfMask &mask) const {
96 return _cache->_GetValue(output, mask);
97 }
98
106 size_t SetValue(
107 const VdfOutput &output,
108 const VdfVector &value,
109 const VdfMask &mask) {
110 return _cache->_SetValue(output, value, mask);
111 }
112
116 size_t Invalidate(const VdfOutput &output) {
117 return _cache->_Invalidate(output);
118 }
119
124 size_t Invalidate(const VdfMaskedOutputVector &outputs) {
125 return _cache->_Invalidate(outputs);
126 }
127
131 size_t Clear() {
132 return _cache->_Clear();
133 }
134
135 private:
136 Ef_OutputValueCache *_cache;
137 tbb::spin_rw_mutex::scoped_lock _lock;
138
139 };
140
145 {
146 public:
149 SharedAccess(const SharedAccess &) = delete;
150 const SharedAccess &operator=(const SharedAccess &) = delete;
151
155 _cache(cache),
156 _lock(cache->_mutex, /* write = */ false)
157 {}
158
163 const VdfOutput &output,
164 const VdfMask &mask) const {
165 return _cache->_GetValue(output, mask);
166 }
167
168 private:
169 Ef_OutputValueCache *_cache;
170 tbb::spin_rw_mutex::scoped_lock _lock;
171
172 };
173
174private:
175 // Returns \c true if the given output is contained in this cache.
176 bool _ContainsOutput(const VdfOutput &output) const;
177
178 // Marks the given output as contained in the cache.
179 void _AddOutput(const VdfOutput &output);
180
181 // Marks the given output as not contained in the cache.
182 void _RemoveOutput(const VdfOutput &output);
183
184 // Returns the value stored at the output, or NULL if the value
185 // is not available, as determined by the specified mask.
186 EF_API
187 const VdfVector *_GetValue(
188 const VdfOutput &output,
189 const VdfMask &mask) const;
190
191 // Sets the value stored at the output. This does not update
192 // any elements currently stored! Only uncached elements will be
193 // merged into the cache.
194 size_t _SetValue(
195 const VdfOutput &output,
196 const VdfVector &value,
197 const VdfMask &mask);
198
199 // Invalidate the entire data stored at the given output.
200 size_t _Invalidate(const VdfOutput &outut);
201
202 // Invalidate the value stored at the given outputs.
203 size_t _Invalidate(const VdfMaskedOutputVector &outputs);
204
205 // Clear the entire cache.
206 size_t _Clear();
207
208 // Is this cache empty?
209 bool _IsEmpty() const;
210
211 // Are there any uncached outputs in the given request?
212 bool _IsUncached(const VdfRequest &request) const;
213
214 // Get all uncached outputs from the given request.
215 VdfRequest _GetUncached(const VdfRequest &request) const;
216
217 // The entry stored for each output in the cache.
218 class _Entry
219 {
220 public:
221 // Constructor
222 _Entry();
223
224 // Destructor
225 ~_Entry();
226
227 // Returns the number of bytes stored at this output.
228 size_t GetNumBytes() const;
229
230 // Returns the mask of elements stored at this output.
231 const VdfMask &GetMask() const {
232 return _mask;
233 }
234
235 // Returns the value stored at the output, if any.
236 const VdfVector *GetValue() const {
237 return _value;
238 }
239
240 // Returns the value stored at the output, if it exists and
241 // contains all elements specified in the given \p mask.
242 const VdfVector *GetValue(const VdfMask &mask) const;
243
244 // Sets the value at this output, not overwriting any existing
245 // data. This will return the number of bytes allocated to
246 // store the additional data.
247 size_t SetValue(
248 const VdfVector &value,
249 const VdfMask &mask);
250
251 // Invalidate the entire data stored at this output. Returns the
252 // number of bytes invalidated.
253 size_t Invalidate();
254
255 // Invalidate the data stored at this output. Returns the number
256 // of bytes invalidated.
257 size_t Invalidate(const VdfMask &mask);
258
259 private:
260 // The data value stored at this output.
261 VdfVector *_value;
262
263 // The mask of data available at this output.
264 VdfMask _mask;
265 };
266
267 // Retain a reference to the one-one mask as a field in order to avoid
268 // contention on any guard variables.
269 const VdfMask _oneOneMask;
270
271 // Type of the output cache map
272 typedef
273 TfHashMap<const VdfOutput *, _Entry, TfHash>
274 _OutputsMap;
275
276 // The output cache map
277 _OutputsMap _outputs;
278
279 // An acceleration structure with outputs contained in this cache.
280 TfBits _outputSet;
281
282 // The mutex protecting concurrent access to this cache.
283 tbb::spin_rw_mutex _mutex;
284
285};
286
287PXR_NAMESPACE_CLOSE_SCOPE
288
289#endif
An accessor that provides exclusive read/write access to the cache.
ExclusiveAccess(Ef_OutputValueCache *cache)
Constructor.
size_t SetValue(const VdfOutput &output, const VdfVector &value, const VdfMask &mask)
Sets the cached values for a given output and mask.
bool IsEmpty() const
Returns true if the cache is empty at this time.
VdfRequest GetUncached(const VdfRequest &request) const
Returns a request of outputs that are not currently cached.
ExclusiveAccess(const ExclusiveAccess &)=delete
Non-copyable.
size_t Invalidate(const VdfOutput &output)
Invalidate an output by removing all the data stored at the output.
size_t Invalidate(const VdfMaskedOutputVector &outputs)
Invalidate a vector of outputs and masks by removing the data from the cache.
const VdfVector * GetValue(const VdfOutput &output, const VdfMask &mask) const
Returns the cached value for a given output and mask, if it exists.
bool IsUncached(const VdfRequest &request) const
Returns true if any outputs in the request are not currently cached.
size_t Clear()
Clears the entire cache.
This accessor grants shared read access to the cache, preventing any concurrent write access.
SharedAccess(Ef_OutputValueCache *cache)
Constructor.
SharedAccess(const SharedAccess &)=delete
Non-copyable.
const VdfVector * GetValue(const VdfOutput &output, const VdfMask &mask) const
Returns the cached value for a given output and mask, if it exists.
An output-to-value storage for caching.
EF_API ~Ef_OutputValueCache()
Destructor.
EF_API Ef_OutputValueCache()
Constructor.
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:49
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:37
This is the base class for all nodes in a VdfNetwork.
Definition: node.h:53
A VdfOutput represents an output on a node.
Definition: output.h:32
This class is used to abstract away knowledge of the cache data used for each node.
Definition: vector.h:56