HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
10 /// \file
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 
19 #include "pxr/base/tf/diagnostic.h"
20 
21 #include <utility>
22 
24 
25 class TfType;
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 
30 {
31 public:
32 
33  /// Returns the number of values in the input vector.
34  ///
35  size_t GetSize() const {
36  return _values.GetSize();
37  }
38 
39  /// The computation of this node is simply setting the values we are
40  /// holding.
41  ///
42  VDF_API
43  void Compute(const VdfContext &context) const override final;
44 
45  /// Returns the amount of memory used by this node in bytes.
46  ///
47  VDF_API
48  size_t GetMemoryUsage() const override final;
49 
50 protected:
51  VDF_API
53  VdfNetwork *network,
54  const VdfOutputSpecs &outputSpecs,
55  VdfVector &&values);
56 
57  VDF_API
58  ~Vdf_InputVectorBase() override;
59 
60 protected:
61 
62  // The values stored in this input vector.
63  VdfVector _values;
64 };
65 
66 template<typename T>
68 {
69 public:
70 
71  /// Creates an input vector with \p n elements.
72  ///
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 
82  /// Sets the value for the input stored at \p index to \p value.
83  ///
84  void SetValue(const size_t index, const T &value)
85  {
86  if (!TF_VERIFY(index < _values.GetSize())) {
87  return;
88  }
89 
91  a[index] = value;
92  }
93 
94  /// Moves \p value into the input vector at \p index.
95  ///
96  void SetValue(const size_t index, T &&value)
97  {
98  if (!TF_VERIFY(index < _values.GetSize())) {
99  return;
100  }
101 
103  a[index] = std::move(value);
104  }
105 
106  /// Returns \c true if the value stored at \p index is equal to \p val.
107  ///
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 
117  /// Returns a pointer to the value at \p index. If \p index is out of
118  /// range an error will be raised and a nullptr returned.
119  ///
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 
130 protected:
131 
132  // Protected destructor. Only the network is allowed to delete nodes.
133  //
134  ~VdfInputVector() override;
135 
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 
148 private:
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 
174 /// An empty, typed input vector.
175 class VDF_API_TYPE VdfEmptyInputVector final : public Vdf_InputVectorBase
176 {
177 public:
178 
179  /// Creates an empty input vector of type \p type.
180  ///
181  VDF_API
182  VdfEmptyInputVector(VdfNetwork *network, const TfType &type);
183 
184 protected:
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 
197 ////////////////////////////////////////////////////////////////////////////////
198 
199 template <typename T>
201 
203 
204 #endif
ReadWriteAccessor< TYPE > GetReadWriteAccessor() const
Definition: vector.h:456
size_t GetSize() const
Definition: inputVector.h:35
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
virtual VDF_API size_t GetMemoryUsage() const
size_t GetSize() const
Definition: vector.h:146
const GLdouble * v
Definition: glcorearb.h:837
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
Definition: node.h:52
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
virtual void Compute(const VdfContext &context) const =0
#define VDF_API
Definition: api.h:25
GLdouble n
Definition: glcorearb.h:2008
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
void SetValue(const size_t index, T &&value)
Definition: inputVector.h:96
bool _IsDerivedEqual(const VdfNode &rhs) const override
Definition: inputVector.h:139
const T * GetValue(const size_t index) const
Definition: inputVector.h:120
bool IsValueEqual(const size_t index, const T &val) const
Definition: inputVector.h:108
An empty, typed input vector.
Definition: inputVector.h:175
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
void SetValue(const size_t index, const T &value)
Definition: inputVector.h:84
VdfInputVector< T > This
Definition: inputVector.h:136
GLsizeiptr size
Definition: glcorearb.h:664
ReadAccessor< TYPE > GetReadAccessor() const
Definition: vector.h:521
#define VDF_API_TYPE
Definition: api.h:26
virtual VDF_API bool _IsDerivedEqual(const VdfNode &rhs) const
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
VdfInputVector(VdfNetwork *const network, size_t n)
Definition: inputVector.h:73
GLuint index
Definition: glcorearb.h:786
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
~VdfInputVector() override
Definition: type.h:47