Loading...
Searching...
No Matches
inputVector.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_VECTOR_H
8#define PXR_EXEC_VDF_INPUT_VECTOR_H
9
11
12#include "pxr/pxr.h"
13
14#include "pxr/exec/vdf/api.h"
15#include "pxr/exec/vdf/node.h"
16#include "pxr/exec/vdf/tokens.h"
18
20
21#include <utility>
22
23PXR_NAMESPACE_OPEN_SCOPE
24
25class TfType;
26
28
29class VDF_API_TYPE Vdf_InputVectorBase : public VdfNode
30{
31public:
32
35 size_t GetSize() const {
36 return _values.GetSize();
37 }
38
42 VDF_API
43 void Compute(const VdfContext &context) const override final;
44
47 VDF_API
48 size_t GetMemoryUsage() const override final;
49
50protected:
51 VDF_API
52 Vdf_InputVectorBase(
53 VdfNetwork *network,
54 const VdfOutputSpecs &outputSpecs,
55 VdfVector &&values);
56
57 VDF_API
58 ~Vdf_InputVectorBase() override;
59
60protected:
61
62 // The values stored in this input vector.
63 VdfVector _values;
64};
65
66template<typename T>
67class VDF_API_TYPE VdfInputVector final : public Vdf_InputVectorBase
68{
69public:
70
73 VdfInputVector(VdfNetwork *const network, size_t n) :
74 Vdf_InputVectorBase(
75 network,
77 .Connector<T>(VdfTokens->out),
78 VdfTypedVector<T>::CreateWithSize(n))
79 {
80 }
81
84 void SetValue(const size_t index, const T &value)
85 {
86 if (!TF_VERIFY(index < _values.GetSize())) {
87 return;
88 }
89
90 VdfVector::ReadWriteAccessor<T> a = _values.GetReadWriteAccessor<T>();
91 a[index] = value;
92 }
93
96 void SetValue(const size_t index, T &&value)
97 {
98 if (!TF_VERIFY(index < _values.GetSize())) {
99 return;
100 }
101
102 VdfVector::ReadWriteAccessor<T> a = _values.GetReadWriteAccessor<T>();
103 a[index] = std::move(value);
104 }
105
108 bool IsValueEqual(const size_t index, const T &val) const
109 {
110 if (const T *v = GetValue(index)) {
111 return *v == val;
112 }
113
114 return false;
115 }
116
120 const T *GetValue(const size_t index) const
121 {
122 if (index >= _values.GetSize()) {
123 return nullptr;
124 }
125
126 const VdfVector::ReadAccessor<T> a = _values.GetReadAccessor<T>();
127 return &a[index];
128 }
129
130protected:
131
132 // Protected destructor. Only the network is allowed to delete nodes.
133 //
134 ~VdfInputVector() override;
135
136 typedef VdfInputVector<T> This;
137
138 // Implements IsEqual() API of VdfNode
139 bool _IsDerivedEqual(const VdfNode &rhs) const override
140 {
141 if (const This *other = dynamic_cast<const This *>(&rhs)) {
142 return _ValuesAreEqual(other);
143 }
144
145 return false;
146 }
147
148private:
149
150 // Helper method to compare if two VdfInputVector are the same. Note that
151 // this is factored out in order to prevent a performance regression due
152 // to code generation.
153 //
154 bool _ValuesAreEqual(const This *const rhs) const
155 {
156 const size_t size = _values.GetSize();
157 if (size == rhs->_values.GetSize()) {
159 _values.GetReadAccessor<T>();
160 const VdfVector &rhsValues = rhs->_values;
162 rhsValues.GetReadAccessor<T>();
163 for (size_t i = 0; i < size; ++i) {
164 if (a[i] != b[i]) {
165 return false;
166 }
167 }
168 return true;
169 }
170 return false;
171 }
172};
173
175class VDF_API_TYPE VdfEmptyInputVector final : public Vdf_InputVectorBase
176{
177public:
178
181 VDF_API
182 VdfEmptyInputVector(VdfNetwork *network, const TfType &type);
183
184protected:
185
186 // Protected destructor. Only the network is allowed to delete nodes.
187 //
188 VDF_API
189 ~VdfEmptyInputVector() override;
190
191 // Implements IsEqual() API of VdfNode
192 //
193 VDF_API
194 bool _IsDerivedEqual(const VdfNode &rhs) const override;
195};
196
198
199template <typename T>
200VdfInputVector<T>::~VdfInputVector() = default;
201
202PXR_NAMESPACE_CLOSE_SCOPE
203
204#endif
Low-level utilities for informing users of various internal and external diagnostic conditions.
TfType represents a dynamic runtime type.
Definition: type.h:48
A context is the parameter bundle passed to callbacks of computations.
Definition: context.h:40
An empty, typed input vector.
Definition: inputVector.h:176
VDF_API VdfEmptyInputVector(VdfNetwork *network, const TfType &type)
Creates an empty input vector of type type.
VDF_API bool _IsDerivedEqual(const VdfNode &rhs) const override
Can be overridden by derived classes to facilitate equality comparision.
A VdfNetwork is a collection of VdfNodes and their connections.
Definition: network.h:60
This is the base class for all nodes in a VdfNetwork.
Definition: node.h:53
virtual VDF_API bool _IsDerivedEqual(const VdfNode &rhs) const
Can be overridden by derived classes to facilitate equality comparision.
virtual VDF_API size_t GetMemoryUsage() const
Returns the amount of memory used by the node in bytes.
virtual void Compute(const VdfContext &context) const =0
This is the method called to perform computation.
VdfOutputSpecs is a container for VdfOutputSpec objects.
A VdfTypedVector implements a VdfVector with a specific type.
Definition: typedVector.h:23
A read-only accessor for low-level acces to the contents of the VdfVector.
Definition: vector.h:469
A read/write accessor for low-level access to the contents of the VdfVector.
Definition: vector.h:403
This class is used to abstract away knowledge of the cache data used for each node.
Definition: vector.h:56
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:266