Loading...
Searching...
No Matches
readWriteAccessor.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_READ_WRITE_ACCESSOR_H
8#define PXR_EXEC_VDF_READ_WRITE_ACCESSOR_H
9
11
12#include "pxr/pxr.h"
13
16
17#include "pxr/base/arch/hints.h"
18
19PXR_NAMESPACE_OPEN_SCOPE
20
47template < typename T >
49{
50public:
51
58 VdfReadWriteAccessor(const VdfContext &context, const TfToken &name);
59
66 {}
67
72 const T &operator[](size_t index) const {
73 return const_cast<VdfReadWriteAccessor *>(this)->operator[](index);
74 }
75
80 T &operator[](size_t index);
81
84 bool IsEmpty() const {
85 return GetSize() == 0;
86 }
87
90 size_t GetSize() const {
91 return _size;
92 }
93
94private:
95
96 // The accessor to the output data.
98
99 // The mask with accessible data elements. All elements are accessible if
100 // this mask is empty.
101 VdfMask _mask;
102
103 // The offset into the data.
104 size_t _offset;
105
106 // The size of the data.
107 size_t _size;
108
109};
110
112
113template < typename T >
115 const VdfContext &context,
116 const TfToken &name) :
117 _offset(0),
118 _size(0)
119{
120 // Get the required output for writing. This will emit a coding error if
121 // there is no valid output.
122 const VdfOutput *output = _GetRequiredOutputForWriting(context, name);
123 if (!output) {
124 return;
125 }
126
127 // Retrieve the relevant masks at the output. This will return false if the
128 // output is not scheduled, i.e. the accessor will remain empty.
129 const VdfMask *requestMask = nullptr;
130 const VdfMask *affectsMask = nullptr;
131 if (!_GetOutputMasks(context, *output, &requestMask, &affectsMask)) {
132 return;
133 }
134
135 // Get the value to write to. It is an error for this value not to be
136 // available. The executor engine is responsible for creating it.
137 VdfVector *v = _GetOutputValueForWriting(context, *output);
138 if (!TF_VERIFY(v, "Output '%s' is missing buffer.",
139 output->GetName().GetText())) {
140 return;
141 }
142
143 // Get the accessor to the value.
144 _accessor = v->GetReadWriteAccessor<T>();
145
146 // If there is an affects mask on this output, and that mask is not
147 // all-ones, use the mask to redirect data access. If the mask is
148 // contiguous, we can simply use the first index as an offset into the data.
149 // The size is the number of bits set on the mask.
150 if (affectsMask && !affectsMask->IsAllOnes()) {
151 if (affectsMask->IsContiguous()) {
152 _offset = affectsMask->GetFirstSet();
153 } else {
154 _mask = *affectsMask;
155 }
156 _size = affectsMask->GetNumSet();
157 }
158
159 // If there is no affects mask, or if the affects mask is all ones we can
160 // provide access without redirection. The size is the number of values on
161 // the vector accessor.
162 else {
163 _size = _accessor.GetNumValues();
164 }
165}
166
167template < typename T >
168T &
170{
171 // Perform out of bounds check in debug builds.
172 TF_DEV_AXIOM(index < GetSize());
173
174 // The fast-path is for data that is contiguous in memory. The offset is
175 // often 0, but the addition is fast enough to perform indiscriminately and
176 // instead of a branch.
177 if (ARCH_LIKELY(_mask.IsEmpty())) {
178 return _accessor[index + _offset];
179 }
180
181 // If a mask is used to redirect data access, we need to map the provided
182 // index to the n-th set bit in the mask. This is the slow-path.
183 return _accessor[_mask.GetBits().FindNthSet(index)];
184}
185
186PXR_NAMESPACE_CLOSE_SCOPE
187
188#endif
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
char const * GetText() const
Return the text that this token represents.
Definition: token.h:179
A context is the parameter bundle passed to callbacks of computations.
Definition: context.h:40
Base class for libVdf iterators.
Definition: iterator.h:36
VDF_API const VdfOutput * _GetRequiredOutputForWriting(const VdfContext &context, const TfToken &name) const
Returns the output for writing based on the name provided.
VDF_API bool _GetOutputMasks(const VdfContext &context, const VdfOutput &output, const VdfMask **requestMask, const VdfMask **affectsMask) const
Retrieves the request and affects masks of the given output.
VDF_API VdfVector * _GetOutputValueForWriting(const VdfContext &context, const VdfOutput &output) const
Returns a vector for writing an output value into.
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:37
bool IsContiguous() const
Returns true if the set bits in the mask are contiguous.
Definition: mask.h:258
bool IsAllOnes() const
Returns true if this mask has all entries set.
Definition: mask.h:196
size_t GetFirstSet() const
Returns the first set bit in the mask.
Definition: mask.h:226
size_t GetNumSet() const
Returns the number of set bits in the mask.
Definition: mask.h:246
A VdfOutput represents an output on a node.
Definition: output.h:32
VDF_API const TfToken & GetName() const
Returns the name of this output.
VdfReadWriteAccessor allows for random access to output data.
size_t GetSize() const
Returns the size of the data stored at the output.
bool IsEmpty() const
Returns true if there is no data stored at the output.
VdfReadWriteAccessor(const VdfContext &context)
Constructs a read/write accessor for the only output on the current node.
VdfReadWriteAccessor(const VdfContext &context, const TfToken &name)
Constructs a read/write accessor for the given input or output.
const T & operator[](size_t index) const
Provides constant random access to the data stored at the output.
A read/write accessor for low-level access to the contents of the VdfVector.
Definition: vector.h:403
size_t GetNumValues() const
Returns the size of the vector, i.e.
Definition: vector.h:416
This class is used to abstract away knowledge of the cache data used for each node.
Definition: vector.h:56
ReadWriteAccessor< TYPE > GetReadWriteAccessor() const
GetReadWriteAccessor() allows low level access to the content of the VdfVector via the Vdf_VectorData...
Definition: vector.h:449
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
Definition: diagnostic.h:205
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:266
Compiler hints.