HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vectorData.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_VECTOR_DATA_H
8 #define PXR_EXEC_VDF_VECTOR_DATA_H
9 
10 #include "pxr/pxr.h"
11 
12 #include "pxr/exec/vdf/api.h"
14 #include "pxr/exec/vdf/mask.h"
15 
16 #include <iosfwd>
17 #include <typeinfo>
18 
20 
22 class Vt_ArrayForeignDataSource;
23 
24 /// \brief Abstract base class for storing data in a VdfVector.
25 ///
27 {
28 protected:
29 
30  // The smallest buffer size we want is the size of our biggest
31  // subclass, which happens to be Vdf_VectorImplContiguous which is made up
32  // of 3 size_ts and a pointer. Note that if this ever changes we will get
33  // a compilation error (unless it gets smaller in which case we would
34  // have to realize that independently and adjust accordingly to maximize
35  // storage).
36  static const size_t _SmallBufferSize = sizeof(size_t)*3 + sizeof(void *);
37 
38  // The holder also needs space for the vtable pointer.
39  static const size_t _DataHolderSize = _SmallBufferSize + sizeof(void*);
40 
41  // The size a vector needs to be to enable sharing. Used in IsSharable().
42  static constexpr size_t _VectorSharingSize = 5000;
43 
44  Vdf_VectorData() = default;
45 
46 public:
47 
50 
51  Vdf_VectorData(const Vdf_VectorData &) = delete;
52  Vdf_VectorData& operator=(const Vdf_VectorData &) = delete;
53 
54  Vdf_VectorData(Vdf_VectorData &&) = delete;
56 
57  VDF_API
58  virtual ~Vdf_VectorData();
59 
60  // Returns the type_info for this data.
61  //
62  virtual const std::type_info &GetTypeInfo() const = 0;
63 
64  // Sets \p destData to an empty data of with this data's type.
65  //
66  // Note that \p destData must have never been initialized or freed
67  // before this call.
68  //
69  virtual void NewEmpty(size_t size, DataHolder *destData) const = 0;
70 
71  // Sets \p destData to a single element vector of this data's type.
72  //
73  // Note that \p destData must have never been initialized or freed
74  // before this call.
75  //
76  virtual void NewSingle(DataHolder *destData) const = 0;
77 
78  // Sets \p destData to a sparse vector of this data's type.
79  //
80  // Note that \p destData must have never been initialized or freed
81  // before this call.
82  //
83  virtual void NewSparse(size_t size, size_t first, size_t last,
84  DataHolder *destData) const = 0;
85 
86  // Sets \p destData to a dense vector of this data's type.
87  //
88  // Note that \p destData must have never been initialized or freed
89  // before this call.
90  //
91  virtual void NewDense(size_t size, DataHolder *destData) const = 0;
92 
93  // Moves this vector into the \p destData.
94  //
95  // Note that after the opertion, this vector data object is no longer
96  // valid and may only be destroyed.
97  //
98  virtual void MoveInto(DataHolder *destData) = 0;
99 
100  // Clones this data data object into \p destData.
101  //
102  // Note that \p destData must point to valid memory.
103  //
104  virtual void Clone(DataHolder *destData) const = 0;
105 
106  // This is like the clone method, only it uses a mask to potentially
107  // copy a smaller set of this vector into \p destData.
108  //
109  // Note that \p destData must point to valid memory.
110  //
111  virtual void CloneSubset(const VdfMask &mask,
112  DataHolder *destData) const = 0;
113 
114  // Boxes the stored data into a container. As a result of this,
115  // \p destData will contain a single element holding all the elements
116  // stored in this vector data instance.
117  //
118  // Only the data elements specified in \p bits will be pushed into the
119  // boxed container.
120  //
121  // Note that \p destData must point to valid memory.
122  //
123  virtual void Box(const VdfMask::Bits &bits,
124  DataHolder *destData) const = 0;
125 
126  // Merges this data into \p destData.
127  //
128  // Note, that \p destData must point to valid memory.
129  //
130  virtual void Merge(const VdfMask::Bits& bits,
131  DataHolder *destData) const = 0;
132 
133  // Expand the storage capabilities of the underlying vector
134  // implementation, if necessary. By default, expansion is not supported.
135  //
136  VDF_API
137  virtual void Expand(size_t first, size_t last);
138 
139  // Returns the size of the vector. Note that there may not be storage
140  // allocated for all the elements in the vector size.
141  //
142  virtual size_t GetSize() const = 0;
143 
144  // Returns the number of elements stored in the vector implementation.
145  //
146  virtual size_t GetNumStoredElements() const = 0;
147 
148  // Returns a pointer to the SharedSource data structure for copyless
149  // value extraction. Disabled for all implementations but
150  // Vdf_VectorImplShared.
151  //
152  VDF_API
153  virtual Vt_ArrayForeignDataSource* GetSharedSource() const;
154 
155  // Returns true if the vector's data is sharable. Defaults to false.
156  //
157  VDF_API
158  virtual bool IsSharable() const;
159 
160  // The vector implementation details returned by the GetInfo() method
161  // as a named parameter object.
162  //
163  struct Info {
164  enum class Layout : uint8_t {
165  Unboxed,
166  Boxed,
167  };
168 
169  enum class Ownership : uint8_t {
170  Exclusive,
171  Shared
172  };
173 
174  // Constructor.
175  explicit Info(
176  void *data_,
177  size_t size_,
178  size_t first_ = 0,
179  size_t last_ = 0,
180  Vdf_CompressedIndexMapping *compressedIndexMapping_ = NULL,
181  Layout layout_ = Info::Layout::Unboxed,
182  Ownership ownership_ = Info::Ownership::Exclusive) :
183  data(data_),
184  size(size_),
185  first(first_),
186  last(last_),
187  compressedIndexMapping(compressedIndexMapping_),
188  layout(layout_),
189  ownership(ownership_)
190  {}
191 
192  // Backing storage.
193  void *data;
194 
195  // Size of the vector implementation.
196  size_t size;
197 
198  // First element stored.
199  size_t first;
200 
201  // Last element stored.
202  size_t last;
203 
204  // The compressed index mapping.
206 
207  // The data layout (boxed vs unboxed)
209 
210  // If the vector implementation is shared.
212  };
213 
214  // Returns the vector implementation details.
215  //
216  virtual Info GetInfo() = 0;
217 
218  // Returns the estimated size of the allocated memory for a single
219  // element stored in this vector.
220  //
221  virtual size_t EstimateElementMemory() const = 0;
222 
223  // Returns whether a vector characterized by the given bitmask and
224  // element size in bytes should be stored using a compressed
225  // block layout, as opposed to a single-block sparse layout or
226  // dense layout.
227  //
228  // The method implements a heuristic to decide whether
229  // compression is appropriate.
230  //
231  VDF_API
232  static bool ShouldStoreCompressed(
233  const VdfMask::Bits &bits, int elementSize);
234 
235  // Prints the data held in this class.
236  //
237  // Only vectors holding a select list of types can be printed. To see the
238  // list of these types or to add to them, see the .cpp file.
239  //
240  VDF_API
241  void DebugPrint(const VdfMask &mask, std::ostream *o) const;
242 };
243 
245 
246 #endif
Vdf_FixedSizePolymorphicHolder< Vdf_VectorData, _DataHolderSize > DataHolder
Definition: vectorData.h:49
GLint first
Definition: glcorearb.h:405
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
Vdf_CompressedIndexMapping * compressedIndexMapping
Definition: vectorData.h:205
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:36
#define VDF_API
Definition: api.h:25
Abstract base class for storing data in a VdfVector.
Definition: vectorData.h:26
Fast, compressed bit array which is capable of performing logical operations without first decompress...
GLint GLuint mask
Definition: glcorearb.h:124
__hostdev__ uint64_t last(uint32_t i) const
Definition: NanoVDB.h:5976
GLsizeiptr size
Definition: glcorearb.h:664
#define VDF_API_TYPE
Definition: api.h:26
LeafData & operator=(const LeafData &)=delete
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Info(void *data_, size_t size_, size_t first_=0, size_t last_=0, Vdf_CompressedIndexMapping *compressedIndexMapping_=NULL, Layout layout_=Info::Layout::Unboxed, Ownership ownership_=Info::Ownership::Exclusive)
Definition: vectorData.h:175
Definition: format.h:1821