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  HD_API
56  HdVtBufferSource(TfToken const &name, VtValue const& value,
57  int arraySize=1);
58 
59  /// Constructs a new buffer from a matrix.
60  /// The data is convert to the default type (see GetDefaultMatrixType()).
61  ///
62  /// note that if we use above VtValue taking constructor, we can use
63  /// either float or double matrix regardless the default type.
64  HD_API
65  HdVtBufferSource(TfToken const &name, GfMatrix4d const &matrix);
66 
67  /// Constructs a new buffer from a matrix.
68  /// The data is convert to the default type (see GetDefaultMatrixType()).
69  ///
70  /// note that if we use above VtValue taking constructor, we can use
71  /// either float or double matrix regardless the default type.
72  ///
73  /// \param arraySize indicates how many values are provided per element.
74  HD_API
75  HdVtBufferSource(TfToken const &name, VtArray<GfMatrix4d> const &matrices,
76  int arraySize=1);
77 
78  /// Returns the default matrix type.
79  /// The default is HdTypeFloatMat4, but if HD_ENABLE_DOUBLEMATRIX is true,
80  /// then HdTypeDoubleMat4 is used instead.
81  HD_API
83 
84  /// Destructor deletes the internal storage.
85  HD_API
87 
88  /// Truncate the buffer to the given number of elements.
89  /// If the VtValue contains too much data, this is a way to only forward
90  /// part of the data to the hydra buffer system. numElements must be less
91  /// than or equal to the current result of GetNumElements().
92  HD_API
93  void Truncate(size_t numElements);
94 
95  /// Return the name of this buffer source.
96  virtual TfToken const &GetName() const override {
97  return _name;
98  }
99 
100  /// Returns the raw pointer to the underlying data.
101  virtual void const* GetData() const override {
102  return HdGetValueData(_value);
103  }
104 
105  /// Returns the data type and count of this buffer source.
106  virtual HdTupleType GetTupleType() const override {
107  return _tupleType;
108  }
109 
110  /// Returns the number of elements (e.g. VtVec3dArray().GetLength()) from
111  /// the source array.
112  HD_API
113  virtual size_t GetNumElements() const override;
114 
115  /// Add the buffer spec for this buffer source into given bufferspec vector.
116  virtual void GetBufferSpecs(HdBufferSpecVector *specs) const override {
117  specs->push_back(HdBufferSpec(_name, _tupleType));
118  }
119 
120  /// Prepare the access of GetData().
121  virtual bool Resolve() override {
122  if (!_TryLock()) return false;
123 
124  // nothing. just marks as resolved, and returns _data in GetData()
125  _SetResolved();
126  return true;
127  }
128 
129 protected:
130  HD_API
131  virtual bool _CheckValid() const override;
132 
133 private:
134  // Constructor helper.
135  void _SetValue(const VtValue &v, int arraySize);
136 
137  TfToken _name;
138 
139  // We hold the source value to avoid making unnecessary copies of the data: if
140  // we immediately copy the source into a temporary buffer, we may need to
141  // copy it again into an aggregate buffer later.
142  //
143  // We can elide this member easily with only a few internal changes, it
144  // should never surface in the public API and for the same reason, this
145  // class should remain noncopyable.
146  VtValue _value;
147  HdTupleType _tupleType;
148  size_t _numElements;
149 };
150 
151 /// Diagnostic output.
152 HD_API
153 std::ostream &operator <<(std::ostream &out, const HdVtBufferSource& self);
154 
156 
157 #endif //PXR_IMAGING_HD_VT_BUFFER_SOURCE_H
GLuint const GLchar * name
Definition: glew.h:1814
virtual HD_API size_t GetNumElements() const override
virtual HdTupleType GetTupleType() const override
Returns the data type and count of this buffer source.
#define HD_API
Definition: api.h:40
virtual HD_API bool _CheckValid() const override
const GLdouble * v
Definition: glew.h:1391
static HD_API HdType GetDefaultMatrixType()
virtual TfToken const & GetName() const override
Return the name of this buffer source.
HD_API HdVtBufferSource(TfToken const &name, VtValue const &value, int arraySize=1)
Definition: token.h:87
virtual void GetBufferSpecs(HdBufferSpecVector *specs) const override
Add the buffer spec for this buffer source into given bufferspec vector.
HD_API std::ostream & operator<<(std::ostream &out, const HdVtBufferSource &self)
Diagnostic output.
HD_API const void * HdGetValueData(const VtValue &)
Definition: types.h:166
std::vector< struct HdBufferSpec > HdBufferSpecVector
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
HD_API void Truncate(size_t numElements)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:83
void _SetResolved()
Definition: bufferSource.h:149
virtual void const * GetData() const override
Returns the raw pointer to the underlying data.
HdType
Definition: types.h:256
GLsizei const GLfloat * value
Definition: glew.h:1849
Definition: value.h:174
GLuint GLenum matrix
Definition: glew.h:14742
HD_API ~HdVtBufferSource()
Destructor deletes the internal storage.
virtual bool Resolve() override
Prepare the access of GetData().