Loading...
Searching...
No Matches
inputValuesPointer.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_INPUT_VALUES_POINTER_H
8#define PXR_EXEC_VDF_INPUT_VALUES_POINTER_H
9
11
12#include "pxr/pxr.h"
13
17
18#include "pxr/base/tf/span.h"
20
21PXR_NAMESPACE_OPEN_SCOPE
22
75template < typename T >
77{
78public:
79
85 VdfInputValuesPointer(const VdfContext &context, const TfToken &inputName);
86
90
95 const T *GetData() const {
96 return _data;
97 }
98
101 size_t GetSize() const {
102 return _size;
103 }
104
111 operator TfSpan<const T>() const {
112 return TfSpan<const T>(GetData(), GetSize());
113 }
114
115private:
116
117 // Noncopyable
119 VdfInputValuesPointer &operator=(const VdfInputValuesPointer &) = delete;
120
121 // Make a copy of the input values.
122 void _CopyInputValues(const VdfContext &context, const TfToken &inputName);
123
124 // A raw pointer to the data.
125 const T *_data;
126
127 // The number of elements in data.
128 size_t _size;
129
130 // Is this a copy of the data?
131 bool _isCopy;
132
133};
134
136
137template < typename T >
139 const VdfContext &context,
140 const TfToken &inputName) :
141 _data(nullptr),
142 _size(0),
143 _isCopy(false)
144{
145 // Get the requested input.
146 const VdfInput * const input = _GetNode(context).GetInput(inputName);
147
148 // Bail out if the input is not available or if it has no connections.
149 if (!input || input->GetNumConnections() == 0) {
150 return;
151 }
152
153 // If there is only one connection targeting the requested input, and that
154 // connection has a contiguous mask, we do not need to make a copy. This
155 // is the fast path.
156 if (input->GetNumConnections() == 1) {
157 const VdfConnection &connection = (*input)[0];
158 const VdfMask &mask = connection.GetMask();
159
160 // Bail out if the single connection mask is all zeros.
161 if (mask.IsAllZeros()) {
162 return;
163 }
164
165 // If the connection mask is contiguous, we can retain a raw pointer to
166 // the data stored in the output buffer.
167 if (mask.IsContiguous()) {
168 if (const VdfVector * const v =
169 _GetInputValue(context, connection, mask)) {
170 const VdfVector::ReadAccessor<T> a = v->GetReadAccessor<T>();
171
172 // If the VdfVector is empty, we have no data, so don't set the
173 // data pointer.
174 if (!a.IsEmpty()) {
175 _size = a.IsBoxed() ? a.GetNumValues() : mask.GetNumSet();
176 _data = &a[mask.GetFirstSet()];
177 }
178 }
179 return;
180 }
181 }
182
183 // If we were not able to retain a pointer pointing directly at the output
184 // buffer, we need to fall back to making a copy of the input values. This
185 // is the slow path.
186 _CopyInputValues(context, inputName);
187}
188
189template < typename T >
190void
192 const VdfContext &context,
193 const TfToken &inputName)
194{
195 TRACE_FUNCTION();
196
197 // Get a read iterator to the input values.
198 VdfReadIterator<T> it(context, inputName);
199
200 // Compute the size from the read iterator, and allocate an array large
201 // enough to accommodate our copy of the input values.
202 _isCopy = true;
203 _size = it.ComputeSize();
204 T *copy = new T[_size];
205
206 // Iterate over the input values and copy them into our array, such that
207 // the data is guaranteed to be laid out contiguously in memory.
208 for (size_t i = 0; !it.IsAtEnd(); ++i, ++it) {
209 copy[i] = *it;
210 }
211
212 // Assign the pointer to the copy to data.
213 _data = copy;
214}
215
216template < typename T >
218{
219 // If a copy was made during construction, we need to destruct that copy.
220 if (_isCopy) {
221 delete[] _data;
222 }
223}
224
225PXR_NAMESPACE_CLOSE_SCOPE
226
227#endif
Represents a range of contiguous elements.
Definition: span.h:71
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
A class that fully represents a connection between two VdfNodes.
Definition: connection.h:30
const VdfMask & GetMask() const
Returns the mask for this connection.
Definition: connection.h:99
A context is the parameter bundle passed to callbacks of computations.
Definition: context.h:40
A VdfInput is used to connect a VdfNode to one or more VdfNodes' outputs.
Definition: input.h:36
size_t GetNumConnections() const
Returns the number of connections for this input.
Definition: input.h:58
VdfInputValuesPointer is a smart pointer object that guarantees contiguous memory access to the reque...
size_t GetSize() const
Returns the size of the data in number of elements stored.
VdfInputValuesPointer(const VdfContext &context, const TfToken &inputName)
Construct a new instance of this class with access to the input values provided by the input named in...
~VdfInputValuesPointer()
Destructor.
const T * GetData() const
Returns an immutable raw pointer to the data.
Base class for libVdf iterators.
Definition: iterator.h:36
VDF_API const VdfVector * _GetInputValue(const VdfContext &context, const VdfConnection &connection, const VdfMask &mask) const
Returns a vector for reading an input value.
const VdfNode & _GetNode(const VdfContext &context) const
Returns the current node being run.
Definition: iterator.h:45
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 IsAllZeros() const
Returns true if this mask has all entries unset.
Definition: mask.h:206
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
const VdfInput * GetInput(const TfToken &inputName) const
Returns the connector named inputName, returns NULL if no input of that name exists.
Definition: node.h:164
An iterator that provides read access to input values using a context.
Definition: readIterator.h:39
A read-only accessor for low-level acces to the contents of the VdfVector.
Definition: vector.h:469
bool IsBoxed() const
Returns true if this accessor is providing element-wise access into a boxed container.
Definition: vector.h:487
bool IsEmpty() const
Returns true if the vector is empty.
Definition: vector.h:478
size_t GetNumValues() const
Returns the size of the vector, i.e.
Definition: vector.h:482
This class is used to abstract away knowledge of the cache data used for each node.
Definition: vector.h:56