vtBufferSource.h
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"
30 #include "pxr/imaging/hd/bufferSource.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 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
43 
49 class HdVtBufferSource final : public HdBufferSource
50 {
51 public:
57  HD_API
58  HdVtBufferSource(TfToken const &name, VtValue const& value,
59  int arraySize=1, bool allowDoubles=true);
60 
69  HD_API
70  HdVtBufferSource(TfToken const &name, GfMatrix4d const &matrix,
71  bool allowDoubles=true);
72 
82  HD_API
83  HdVtBufferSource(TfToken const &name, VtArray<GfMatrix4d> const &matrices,
84  int arraySize=1, bool allowDoubles=true);
85 
89  HD_API
90  static HdType GetDefaultMatrixType();
91 
93  HD_API
94  ~HdVtBufferSource() override;
95 
100  HD_API
101  void Truncate(size_t numElements);
102 
104  TfToken const &GetName() const override {
105  return _name;
106  }
107 
109  void const* GetData() const override {
110  return HdGetValueData(_value);
111  }
112 
114  HdTupleType GetTupleType() const override {
115  return _tupleType;
116  }
117 
120  HD_API
121  size_t GetNumElements() const override;
122 
124  void GetBufferSpecs(HdBufferSpecVector *specs) const override {
125  specs->push_back(HdBufferSpec(_name, _tupleType));
126  }
127 
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 
160 HD_API
161 std::ostream &operator <<(std::ostream &out, const HdVtBufferSource& self);
162 
163 PXR_NAMESPACE_CLOSE_SCOPE
164 
165 #endif //PXR_IMAGING_HD_VT_BUFFER_SOURCE_H
void _SetResolved()
Marks this buffer source as resolved.
Definition: bufferSource.h:149
HdTupleType represents zero, one, or more values of the same HdType.
Definition: types.h:358
Describes each named resource of buffer array.
Definition: bufferSpec.h:53
HdTupleType GetTupleType() const override
Returns the data type and count of this buffer source.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
A transient buffer of data that has not yet been committed.
Definition: bufferSource.h:55
TfToken const & GetName() const override
Return the name of this buffer source.
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:87
bool Resolve() override
Prepare the access of GetData().
Represents an arbitrary dimensional rectangular container class.
Definition: array.h:229
bool _TryLock()
Non-blocking lock acquisition.
Definition: bufferSource.h:173
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
void const * GetData() const override
Returns the raw pointer to the underlying data.
HD_API HdVtBufferSource(TfToken const &name, VtValue const &value, int arraySize=1, bool allowDoubles=true)
Constructs a new buffer from a VtValue.
void GetBufferSpecs(HdBufferSpecVector *specs) const override
Add the buffer spec for this buffer source into given bufferspec vector.
HD_API bool _CheckValid() const override
Checks the validity of the source buffer.
HD_API void Truncate(size_t numElements)
Truncate the buffer to the given number of elements.
HD_API size_t GetNumElements() const override
Returns the number of elements (e.g.
static HD_API HdType GetDefaultMatrixType()
Returns the default matrix type.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166
An implementation of HdBufferSource where the source data value is a VtValue.
HD_API ~HdVtBufferSource() override
Destructor deletes the internal storage.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...