HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GT_VtArray.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef _GUSD_GT_VTARRAY_H_
25 #define _GUSD_GT_VTARRAY_H_
26 
27 #include <GT/GT_DataArray.h>
28 #include <GT/GT_DANumeric.h>
29 #include <SYS/SYS_Compiler.h>
30 #include <SYS/SYS_Version.h>
31 #include <SYS/SYS_Math.h>
32 
33 #include "pxr/pxr.h"
34 #include "pxr/base/vt/array.h"
35 
36 #include "gusd/UT_TypeTraits.h"
37 #include "gusd/GT_Utils.h"
38 
40 
41 /// GT_DataArray implementation that wraps a VtArray.
42 ///
43 /// This allows, in some cases, for arrays read in from USD to be
44 /// pushed into GT prims without having to incur copying.
45 /// Example:
46 /// \code
47 /// VtArray<int> valsFromUSD;
48 /// GT_DataArrayHandle hnd(new GusdGT_VtArray<int>(valsFromUSD));
49 /// \endcode
50 ///
51 /// These arrays are designed to be read-only.
52 /// If you need to make edits, use the following pattern:
53 ///
54 /// \code
55 /// GusdGT_VtArray<int> srcData;
56 /// // swap data into tmp array, modify.
57 /// VtArray<int> tmp;
58 /// srcData.swap(tmp);
59 /// tmp[10] = 37;
60 /// // swap data back into place.
61 /// srcData.swap(tmp);
62 /// \endcode
63 ///
64 /// Note that this kind of swapping trick does *not* require the
65 /// full array to be copied; only the internal references are swapped.
66 template <class T>
68 {
69 public:
70  SYS_STATIC_ASSERT(GusdIsPodTuple<T>());
71 
73  using ValueType = T;
76 
77  static const int tupleSize = GusdGetTupleSize<T>();
78  static const GT_Storage storage =
80 
83 
84  ~GusdGT_VtArray() override = default;
85 
86  virtual const char* className() const override { return "GusdGT_VtArray"; }
87 
88  const T& operator()(GT_Offset o) const
89  {
90  UT_ASSERT_P(o >= 0 && o <= _size);
91  return reinterpret_cast<T*>(_data)[o];
92  }
93 
94  PODType operator()(GT_Offset o, int idx) const
95  { return getT<PODType>(o, idx); }
96 
97  const ArrayType& operator*() const { return _array; }
98 
99  const PODType* data() const { return _data; }
100 
101  /// Swap our array contents with another array.
102  void swap(ArrayType& o);
103 
104  virtual GT_DataArrayHandle harden() const override;
105 
106  const PODType* getData(GT_Offset o) const
107  {
108  UT_ASSERT_P(o >= 0 && o <= _size);
109  return _data + o*tupleSize;
110  }
111 
112  /// Access to individual elements as given POD type.
113  /// For performance, this is preferred to the virtual getXX() methods.
114  template <typename PODT>
115  PODT getT(GT_Offset o, int idx=0) const;
116 
117  /// Get access to a raw array of data.
118  /// If @a OTHERPODT is not the same as the array's underlying type,
119  /// the raw array will be stored in the given @a buf.
120  template <typename PODT>
121  const PODT* getArrayT(GT_DataArrayHandle& buf) const;
122 
123  /// Extract a tuple into @a dst
124  template <typename PODT>
125  void importT(GT_Offset o, PODT* dst, int tsize=-1) const;
126 
127  /// Extract data for entire array into @a dst.
128  template <typename PODT>
130  int tsize=-1, int stride=-1) const;
131 
132  /// Extended form of array extraction that supports repeated elems.
133  template <typename PODT>
134  void extendedFillT(PODT* dst, GT_Offset start,
135  GT_Size length, int tsize=-1,
136  int nrepeats=1, int stride=-1) const;
137 
138  virtual GT_Storage getStorage() const override { return storage; }
139  virtual GT_Size getTupleSize() const override { return tupleSize; }
140  virtual GT_Size entries() const override { return _size; }
141  virtual GT_Type getTypeInfo() const override { return _type; }
142  virtual int64 getMemoryUsage() const override
143  { return sizeof(*this) + sizeof(T)*_size; }
144 
145  // Type-specific virtual getters.
146 
147 #define _DECL_GETTERS(suffix,podt) \
148 public: \
149  virtual podt SYS_CONCAT(get,suffix)(GT_Offset o, \
150  int idx=0) const override \
151  { return getT<podt>(o, idx); } \
152  \
153  virtual const podt* SYS_CONCAT(get,SYS_CONCAT(suffix,Array)) \
154  (GT_DataArrayHandle& buf) const override \
155  { return getArrayT<podt>(buf); } \
156  \
157 protected: \
158  virtual void doImport(GT_Offset o, podt* dst, \
159  GT_Size tsize) const override \
160  { importT(o, dst, tsize); } \
161  \
162  virtual void doFillArray(podt* dst, GT_Offset start, \
163  GT_Size length, int tsize, \
164  int stride) const override \
165  { fillArrayT(dst, start, length, \
166  tsize, stride); } \
167  \
168  virtual void extendedFill(podt* dst, GT_Offset start, \
169  GT_Size length, int tsize, \
170  int nrepeats, \
171  int stride=-1) const override \
172  { extendedFillT(dst, start, length, \
173  tsize, nrepeats, stride); } \
174  /* end of macro */
175 
176  _DECL_GETTERS(I8, int8);
177  _DECL_GETTERS(I16, int16);
178  _DECL_GETTERS(U8, uint8);
179  _DECL_GETTERS(I32, int32);
180  _DECL_GETTERS(I64, int64);
181  _DECL_GETTERS(F16, fpreal16);
182  _DECL_GETTERS(F32, fpreal32);
183  _DECL_GETTERS(F64, fpreal64);
184 
185 #undef _DECL_GETTER
186 
187 protected:
188  /// Update our @a _data member to point at the array data.
189  /// This must be called after any operation that changes
190  /// the contents of @a _array.
191  void _UpdateDataPointer(bool makeUnique);
192 
193 private:
194  // No string support. For strings, use GusdGT_VtStringArray.
195  virtual GT_String getS(GT_Offset, int) const override { return nullptr; }
196  virtual GT_Size getStringIndexCount() const override { return -1; }
197  virtual GT_Offset getStringIndex(GT_Offset,int) const override { return -1; }
198  virtual void getIndexedStrings(UT_StringArray&,
199  UT_IntArray&) const override {}
200 
201 protected:
203  const GT_Type _type;
205  const PODType* _data; /// Raw pointer to the underlying data.
206  /// Held separately as an optimization.
207 };
208 
209 
210 template <class T>
212  : _array(array), _type(type), _size(array.size())
213 {
214  _UpdateDataPointer(false);
215 }
216 
217 
218 template <class T>
220  : _type(type), _size(0), _data(nullptr)
221 {}
222 
223 
224 template <class T>
225 void
227 {
228  // Access a non-const pointer to make the array unique.
229  _data = reinterpret_cast<const PODType*>(
230  makeUnique ? _array.data() : _array.cdata());
231  UT_ASSERT(_size == 0 || _data != nullptr);
232 }
233 
234 
235 template <class T>
236 void
238 {
239  _array.swap(o);
240  _size = _array.GetSize();
241  _UpdateDataPointer(false);
242 }
243 
244 
245 template <class T>
248 {
249  This* copy = new This(_array, _type);
250  copy->_UpdateDataPointer(true);
251  return GT_DataArrayHandle(copy);
252 }
253 
254 
255 template <class T>
256 template <typename PODT>
257 PODT
259 {
260  UT_ASSERT_P(o >= 0 && o < _size);
261  UT_ASSERT_P(idx >= 0 && idx < tupleSize);
262  return static_cast<PODT>(_data[tupleSize*o + idx]);
263 }
264 
265 
266 template <class T>
267 template <typename PODT>
268 const PODT*
270 {
272  return reinterpret_cast<const PODT*>(_data);
273 
274  using _GTArrayType = GT_DANumeric<PODT>;
275 
276  _GTArrayType* tmpArray = new _GTArrayType(_size, tupleSize, _type);
277  fillArrayT(tmpArray->data(), 0, _size, tupleSize);
278  buf = tmpArray;
279  return tmpArray->data();
280 }
281 
282 
283 template <class T>
284 template <typename PODT>
285 void
286 GusdGT_VtArray<T>::importT(GT_Offset o, PODT* dst, int tsize) const
287 {
288  tsize = tsize < 1 ? tupleSize : SYSmin(tsize, tupleSize);
289  const PODType* src = getData(o);
290  if( src )
291  {
292  for(int i = 0; i < tsize; ++i)
293  dst[i] = static_cast<PODT>(src[i]);
294  }
295 }
296 
297 
298 template <class T>
299 template <typename PODT>
300 void
302  int tsize, int stride) const
303 {
304  if(_size == 0)
305  return;
306  if(tsize < 1)
307  tsize = tupleSize;
308  // Stride is >= the unclamped tuple size.
309  // This seems wrong, but is consistent with GT_DANumeric.
310  stride = SYSmax(stride, tsize);
311  tsize = SYSmin(tsize, tupleSize);
313  tsize == tupleSize && stride == tupleSize) {
314  // direct copy is safe
315  memcpy(dst, getData(start), length*tsize*sizeof(PODT));
316  } else {
317  const PODType* src = getData(start*tupleSize);
318  for(GT_Offset i = 0; i < length; ++i, src += tupleSize, dst += stride) {
319  for(int j = 0; j < tsize; ++j)
320  dst[j] = static_cast<PODT>(src[j]);
321  }
322  }
323 }
324 
325 
326 template <class T>
327 template <typename PODT>
328 void
330  GT_Size length, int tsize,
331  int nrepeats, int stride) const
332 {
333  if(nrepeats == 1)
334  return fillArrayT(dst, start, length, tsize, stride);
335  if(_size == 0)
336  return;
337  if(tsize < 1)
338  tsize = tupleSize;
339  stride = SYSmax(stride, tsize);
340  tsize = SYSmin(tsize, tupleSize);
341 
342  const PODType* src = getData(start*tupleSize);
343  for(GT_Offset i = 0; i < length; ++i, src += tupleSize) {
344  for(int r = 0; r < nrepeats; ++r, dst += stride) {
345  for(int j = 0; j < tsize; ++j)
346  dst[j] = static_cast<PODT>(src[j]);
347  }
348  }
349 }
350 
352 
353 #endif /*_GUSD_GT_VTARRAY_H_*/
#define SYSmax(a, b)
Definition: SYS_Math.h:1447
GT_Storage
Definition: GT_Types.h:18
GLsizeiptr size
Definition: glew.h:1681
GLenum src
Definition: glew.h:2410
virtual GT_Size getTupleSize() const override
Number of elements for each array element.
Definition: GT_VtArray.h:139
const T & operator()(GT_Offset o) const
Definition: GT_VtArray.h:88
typename GusdPodTupleTraits< T >::ValueType PODType
Definition: GT_VtArray.h:75
_DECL_GETTERS(I8, int8)
GT_Type
Definition: GT_Types.h:34
virtual GT_DataArrayHandle harden() const override
Create a "hardened" version of the array.
Definition: GT_VtArray.h:247
virtual GT_Size entries() const override
Number of entries in the array.
Definition: GT_VtArray.h:140
signed char int8
Definition: SYS_Types.h:35
const PODType * getData(GT_Offset o) const
Definition: GT_VtArray.h:106
long long int64
Definition: SYS_Types.h:111
static const GT_Storage storage
Definition: GT_VtArray.h:78
ArrayType _array
Definition: GT_VtArray.h:202
virtual GT_Storage getStorage() const override
Type of data stored in the array.
Definition: GT_VtArray.h:138
const PODT * getArrayT(GT_DataArrayHandle &buf) const
Definition: GT_VtArray.h:269
GT_Size _size
Definition: GT_VtArray.h:204
Abstract data class for an array of float, int or string data.
Definition: GT_DataArray.h:39
PODType operator()(GT_Offset o, int idx) const
Definition: GT_VtArray.h:94
UT_IntrusivePtr< GT_DataArray > GT_DataArrayHandle
Definition: GT_DataArray.h:31
void extendedFillT(PODT *dst, GT_Offset start, GT_Size length, int tsize=-1, int nrepeats=1, int stride=-1) const
Extended form of array extraction that supports repeated elems.
Definition: GT_VtArray.h:329
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:134
double fpreal64
Definition: SYS_Types.h:196
void importT(GT_Offset o, PODT *dst, int tsize=-1) const
Extract a tuple into dst.
Definition: GT_VtArray.h:286
GLuint GLsizei GLsizei * length
Definition: glew.h:1825
GLenum GLenum dst
Definition: glew.h:2410
int64 GT_Offset
Definition: GT_Types.h:114
void swap(ArrayType &o)
Swap our array contents with another array.
Definition: GT_VtArray.h:237
VtArray< T > ArrayType
Definition: GT_VtArray.h:74
static const int tupleSize
Definition: GT_VtArray.h:77
Definition: types.h:166
int int32
Definition: SYS_Types.h:39
SYS_STATIC_ASSERT(GusdIsPodTuple< T >())
void _UpdateDataPointer(bool makeUnique)
Definition: GT_VtArray.h:226
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1253
GLuint start
Definition: glew.h:1253
const PODType * data() const
Definition: GT_VtArray.h:99
GLsizei stride
Definition: glew.h:1523
const GT_Type _type
Definition: GT_VtArray.h:203
int64 GT_Size
Definition: GT_Types.h:113
GusdGT_VtArray(const ArrayType &array, GT_Type type=GT_TYPE_NONE)
Definition: GT_VtArray.h:211
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1245
virtual int64 getMemoryUsage() const override
Definition: GT_VtArray.h:142
unsigned char uint8
Definition: SYS_Types.h:36
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
OIIO_API bool copy(string_view from, string_view to, std::string &err)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:83
An array of numeric values (int32, int64, fpreal16, fpreal32, fpreal64)
Definition: GT_DANumeric.h:23
getOption("OpenEXR.storage") storage
Definition: HDK_Image.dox:276
GLenum array
Definition: glew.h:9066
const ArrayType & operator*() const
Definition: GT_VtArray.h:97
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:135
virtual const char * className() const override
Definition: GT_VtArray.h:86
short int16
Definition: SYS_Types.h:37
GLenum GLuint GLsizei const GLchar * buf
Definition: glew.h:2580
#define SYSmin(a, b)
Definition: SYS_Math.h:1448
float fpreal32
Definition: SYS_Types.h:195
virtual GT_Type getTypeInfo() const override
Return "type" information for the data. This defaults to GT_TYPE_NONE.
Definition: GT_VtArray.h:141
void fillArrayT(PODT *dst, GT_Offset start, GT_Size length, int tsize=-1, int stride=-1) const
Extract data for entire array into dst.
Definition: GT_VtArray.h:301
~GusdGT_VtArray() override=default
PODT getT(GT_Offset o, int idx=0) const
Definition: GT_VtArray.h:258
const PODType * _data
Definition: GT_VtArray.h:205