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