Loading...
Searching...
No Matches
smblData.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_SMBL_DATA_H
8#define PXR_EXEC_VDF_SMBL_DATA_H
9
11
12#include "pxr/pxr.h"
13
14#include "pxr/exec/vdf/api.h"
15#include "pxr/exec/vdf/mask.h"
17#include "pxr/exec/vdf/traits.h"
18#include "pxr/exec/vdf/types.h"
19
20PXR_NAMESPACE_OPEN_SCOPE
21
22class VdfVector;
23
31{
32public:
35 VdfSMBLData(const VdfSMBLData &) = delete;
36 VdfSMBLData &operator=(const VdfSMBLData &) = delete;
37
41 _cache(NULL)
42 {}
43
44 VDF_API
46
47
51
58 const VdfMask &cacheMask,
59 const VdfMask &invalidationMask);
60
66 VdfMask *lockedCacheMask,
67 const VdfMask &cacheMask) {
68 *lockedCacheMask = _cachedExtend(*lockedCacheMask, cacheMask);
69 }
70
77 inline void RemoveUncachedMask(
78 VdfMask *lockedCacheMask,
79 const VdfMask &cacheMask,
80 const VdfMask &keepMask);
81
88 const VdfMask &lockedCacheMask,
89 const VdfMask &affectsMask) {
90 return !_cachedAffective(lockedCacheMask, affectsMask);
91 }
92
94
95
99
107 inline VdfVector* Retain(
108 const VdfOutputSpec &spec,
109 VdfVector *cache,
110 const VdfMask &cacheMask);
111
117 void Release() {
118 if (!_cacheMask.IsEmpty()) {
119 _cacheMask = VdfMask();
120 }
121 }
122
125 VDF_API
126 void Clear();
127
131 return _cache;
132 }
133
136 const VdfMask &GetCacheMask() const {
137 return _cacheMask;
138 }
139
143 bool HasCache() const {
144 return _cache && !_cacheMask.IsEmpty();
145 }
146
148
149private:
150 // This is a helper class used for memoizing expensive mask computations.
151 //
152 // For sparse mung buffer locking mask operations on a specific output are
153 // expected to always yield the same results for any but the first run of
154 // the executor. Memoization allows us to hold on to these results without
155 // having to worry about invalidation. Note, that we exploit the fact that
156 // masks are flyweighted, and hence very cheap to store, as well as
157 // equality compare.
158 template < typename R, typename OP >
159 class _MaskOpMemoizer
160 {
161 public:
162 _MaskOpMemoizer() :
163 // Initialize the default result to avoid correctness problems
164 _result(OP()(_opA, _opB))
165 {}
166
168 operator()(const VdfMask &opA, const VdfMask &opB) {
169 // Cache miss?
170 if (_opA != opA || _opB != opB) {
171 _opA = opA;
172 _opB = opB;
173 _result = OP()(opA, opB);
174 }
175
176 // Cache hit!
177 return _result;
178 }
179
180 private:
181 // Operand A
182 VdfMask _opA;
183
184 // Operand B
185 VdfMask _opB;
186
187 // Cached result
188 R _result;
189
190 };
191
192 // Functor used for a memoized mask subtraction
193 struct _MaskSubtract {
194 VdfMask operator()(const VdfMask &lhs, const VdfMask &rhs) const {
195 return lhs - rhs;
196 }
197 };
198
199 // Functor used for a memoized append of two masks
200 struct _MaskSetOrAppend {
201 VdfMask operator()(const VdfMask &lhs, const VdfMask &rhs) const {
202 VdfMask res(lhs);
203 res.SetOrAppend(rhs);
204 return res;
205 }
206 };
207
208 // Functor used for computing the "Contains" method on a mask
209 struct _MaskContains {
210 bool operator()(const VdfMask &lhs, const VdfMask &rhs) const {
211 return lhs.Contains(rhs);
212 }
213 };
214
215 // Memoized result of the invalid cache mask
216 _MaskOpMemoizer<VdfMask, _MaskSubtract> _cachedInvalidate;
217
218 // Memoized result of the extended locked cache mask
219 _MaskOpMemoizer<VdfMask, _MaskSetOrAppend> _cachedExtend;
220
221 // Memoized result of the affective-ness flag
222 _MaskOpMemoizer<bool, _MaskContains> _cachedAffective;
223
224 // Memoized result of keepMask - cacheMask. This contains the bits
225 // that are required to be stored at the output.
226 _MaskOpMemoizer<VdfMask, _MaskSubtract> _cachedRequiredMask;
227
228 // Memoized result of the locked cache mask with all the uncached,
229 // but required bits removed.
230 _MaskOpMemoizer<VdfMask, _MaskSubtract> _cachedRequiredLockedCache;
231
232 // Locally retained cache and cache mask
233 VdfVector *_cache;
234 VdfMask _cacheMask;
235
236};
237
238
241 const VdfMask &cacheMask,
242 const VdfMask &invalidationMask)
243{
244 return cacheMask.IsEmpty()
245 ? cacheMask
246 : _cachedInvalidate(cacheMask, invalidationMask);
247}
248
249void
251 VdfMask *lockedCacheMask,
252 const VdfMask &cacheMask,
253 const VdfMask &keepMask)
254{
255 // Determine which bits in the keep mask are not available in the
256 // local executor cache. These are the bits that we have to remove from
257 // the executor cache mask, if necessary.
258 const VdfMask &uncached =
259 cacheMask.IsEmpty()
260 ? keepMask
261 : _cachedRequiredMask(keepMask, cacheMask);
262
263 // Remove the uncached bits, if any.
264 *lockedCacheMask = _cachedRequiredLockedCache(*lockedCacheMask, uncached);
265}
266
267VdfVector*
269 const VdfOutputSpec &spec,
270 VdfVector *cache,
271 const VdfMask &cacheMask)
272{
273 // The local cache is always a free cache. If a local cache has not
274 // been allocated, yet, allocate one here.
275 if (!_cache) {
276 _cache = spec.AllocateCache();
277 }
278
279 // Store a pointer to the current cache, which is a always a free cache.
280 VdfVector *freeCache = _cache;
281
282 // Swap the current cache with the passed in cache, to retain it.
283 _cache = cache;
284 _cacheMask = cacheMask;
285
286 // Return the pointer to the free cache to be re-used by the client.
287 return freeCache;
288}
289
290PXR_NAMESPACE_CLOSE_SCOPE
291
292#endif /* PXR_EXEC_VDF_SMBL_DATA_H */
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:37
bool IsEmpty() const
Returns true if this mask is empty, i.e.
Definition: mask.h:168
bool Contains(const VdfMask &mask) const
Returns true if mask is a subset-of or equal to this mask, false otherwise.
Definition: mask.h:186
A VdfOuptutSpec describes an output connector.
Definition: outputSpec.h:40
VDF_API VdfVector * AllocateCache() const
Allocate a new VdfVector with this spec's type.
VdfSMBLData holds per-output data that is meant to be consumed by the executor.
Definition: smblData.h:31
void RemoveUncachedMask(VdfMask *lockedCacheMask, const VdfMask &cacheMask, const VdfMask &keepMask)
Make sure that all the bits in the keepMask are provided by the cacheMask.
Definition: smblData.h:250
VdfVector * Retain(const VdfOutputSpec &spec, VdfVector *cache, const VdfMask &cacheMask)
Locally retains the passed in cache with the given cacheMask.
Definition: smblData.h:268
VdfVector * GetCache() const
Returns a pointer to the locally retained cache, if any.
Definition: smblData.h:130
VdfMask InvalidateCacheMask(const VdfMask &cacheMask, const VdfMask &invalidationMask)
Invalidates the executor cacheMask given an invalidationMask.
Definition: smblData.h:240
VdfSMBLData(const VdfSMBLData &)=delete
Noncopyable.
const VdfMask & GetCacheMask() const
Returns a mask indicating data available in the locally retained cache.
Definition: smblData.h:136
void Release()
Releases the cache, which has been retained by this object, if any.
Definition: smblData.h:117
bool HasCache() const
Returns true, if a cache has been retained locally, and false if there is no such cache.
Definition: smblData.h:143
bool ComputeAffectiveness(const VdfMask &lockedCacheMask, const VdfMask &affectsMask)
Computes the affectiveness of the corresponding output given the accumulated lockedCacheMask and the ...
Definition: smblData.h:87
void ExtendLockedCacheMask(VdfMask *lockedCacheMask, const VdfMask &cacheMask)
Extends the lockedCacheMask by appending the bits stored in the executor cacheMask.
Definition: smblData.h:65
VDF_API void Clear()
Clear any of the data this object is holding on to.
VdfSMBLData()
Constructs an SMBL data object.
Definition: smblData.h:40
This class is used to abstract away knowledge of the cache data used for each node.
Definition: vector.h:56
typename std::conditional_t< std::is_pointer_v< T >||Vdf_AndTypeIsSmall< T, std::is_arithmetic_v< T > >||Vdf_AndTypeIsSmall< T, std::is_enum_v< T > >, T, const T & > VdfByValueOrConstRef
Template that evaluates to either T or const T & depending on whether T best be passed as value or co...
Definition: traits.h:108