HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vtBufferSource.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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_IMAGING_HD_VT_BUFFER_SOURCE_H
8 #define PXR_IMAGING_HD_VT_BUFFER_SOURCE_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hd/api.h"
12 #include "pxr/imaging/hd/version.h"
14 #include "pxr/imaging/hd/types.h"
15 
16 #include "pxr/base/tf/token.h"
17 #include "pxr/base/gf/matrix4d.h"
18 #include "pxr/base/vt/value.h"
19 
20 #include <vector>
21 
22 #include <iosfwd>
23 
25 
26 
27 /// \class HdVtBufferSource
28 ///
29 /// An implementation of HdBufferSource where the source data value is a
30 /// VtValue.
31 ///
32 class HdVtBufferSource final : public HdBufferSource
33 {
34 public:
35  /// Constructs a new buffer from a VtValue.
36  ///
37  /// \param arraySize indicates how many values are provided per element.
38  /// \param allowDoubles indicates if double types can be used, or if they
39  /// must be converted to floats.
40  HD_API
41  HdVtBufferSource(TfToken const &name, VtValue const& value,
42  int arraySize=1, bool allowDoubles=true);
43 
44  /// Constructs a new buffer from a matrix.
45  /// The data is convert to the default type (see GetDefaultMatrixType()).
46  ///
47  /// Note that if we use above VtValue taking constructor, we can use
48  /// either float or double matrix regardless the default type.
49  ///
50  /// \param allowDoubles indicates if double types can be used, or if they
51  /// must be converted to floats regardless of the default type.
52  HD_API
53  HdVtBufferSource(TfToken const &name, GfMatrix4d const &matrix,
54  bool allowDoubles=true);
55 
56  /// Constructs a new buffer from a matrix.
57  /// The data is convert to the default type (see GetDefaultMatrixType()).
58  ///
59  /// Note that if we use above VtValue taking constructor, we can use
60  /// either float or double matrix regardless the default type.
61  ///
62  /// \param arraySize indicates how many values are provided per element.
63  /// \param allowDoubles indicates if double types can be used, or if they
64  /// must be converted to floats regardless of the default type.
65  HD_API
66  HdVtBufferSource(TfToken const &name, VtArray<GfMatrix4d> const &matrices,
67  int arraySize=1, bool allowDoubles=true);
68 
69  /// Returns the default matrix type.
70  /// The default is HdTypeFloatMat4, but if HD_ENABLE_DOUBLEMATRIX is true,
71  /// then HdTypeDoubleMat4 is used instead.
72  HD_API
74 
75  /// Destructor deletes the internal storage.
76  HD_API
77  ~HdVtBufferSource() override;
78 
79  /// Truncate the buffer to the given number of elements.
80  /// If the VtValue contains too much data, this is a way to only forward
81  /// part of the data to the hydra buffer system. numElements must be less
82  /// than or equal to the current result of GetNumElements().
83  HD_API
84  void Truncate(size_t numElements);
85 
86  /// Return the name of this buffer source.
87  TfToken const &GetName() const override {
88  return _name;
89  }
90 
91  /// Returns the raw pointer to the underlying data.
92  void const* GetData() const override {
93  return HdGetValueData(_value);
94  }
95 
96  /// Returns the data type and count of this buffer source.
97  HdTupleType GetTupleType() const override {
98  return _tupleType;
99  }
100 
101  /// Returns the number of elements (e.g. VtVec3dArray().GetLength()) from
102  /// the source array.
103  HD_API
104  size_t GetNumElements() const override;
105 
106  /// Add the buffer spec for this buffer source into given bufferspec vector.
107  void GetBufferSpecs(HdBufferSpecVector *specs) const override {
108  specs->push_back(HdBufferSpec(_name, _tupleType));
109  }
110 
111  /// Prepare the access of GetData().
112  bool Resolve() override {
113  if (!_TryLock()) return false;
114 
115  // nothing. just marks as resolved, and returns _data in GetData()
116  _SetResolved();
117  return true;
118  }
119 
120 protected:
121  HD_API
122  bool _CheckValid() const override;
123 
124 private:
125  // Constructor helper.
126  void _SetValue(const VtValue &v, int arraySize, bool allowDoubles);
127 
128  TfToken _name;
129 
130  // We hold the source value to avoid making unnecessary copies of the data: if
131  // we immediately copy the source into a temporary buffer, we may need to
132  // copy it again into an aggregate buffer later.
133  //
134  // We can elide this member easily with only a few internal changes, it
135  // should never surface in the public API and for the same reason, this
136  // class should remain noncopyable.
137  VtValue _value;
138  HdTupleType _tupleType;
139  size_t _numElements;
140 };
141 
142 /// Diagnostic output.
143 HD_API
144 std::ostream &operator <<(std::ostream &out, const HdVtBufferSource& self);
145 
147 
148 #endif //PXR_IMAGING_HD_VT_BUFFER_SOURCE_H
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLfloat * value
Definition: glcorearb.h:824
HD_API HdVtBufferSource(TfToken const &name, VtValue const &value, int arraySize=1, bool allowDoubles=true)
void const * GetData() const override
Returns the raw pointer to the underlying data.
#define HD_API
Definition: api.h:23
static HD_API HdType GetDefaultMatrixType()
Definition: token.h:70
HD_API std::ostream & operator<<(std::ostream &out, const HdVtBufferSource &self)
Diagnostic output.
HD_API const void * HdGetValueData(const VtValue &)
TfToken const & GetName() const override
Return the name of this buffer source.
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: types.h:153
std::vector< struct HdBufferSpec > HdBufferSpecVector
void GetBufferSpecs(HdBufferSpecVector *specs) const override
Add the buffer spec for this buffer source into given bufferspec vector.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HD_API bool _CheckValid() const override
HD_API void Truncate(size_t numElements)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
void _SetResolved()
Definition: bufferSource.h:132
bool Resolve() override
Prepare the access of GetData().
HdType
Definition: types.h:272
HD_API size_t GetNumElements() const override
HdTupleType GetTupleType() const override
Returns the data type and count of this buffer source.
Definition: value.h:146
HD_API ~HdVtBufferSource() override
Destructor deletes the internal storage.