HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vboSimpleMemoryManager.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_ST_VBO_SIMPLE_MEMORY_MANAGER_H
25 #define PXR_IMAGING_HD_ST_VBO_SIMPLE_MEMORY_MANAGER_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
31 #include "pxr/imaging/hd/version.h"
36 
38 
40 
41 /// \class HdStVBOSimpleMemoryManager
42 ///
43 /// VBO simple memory manager.
44 ///
45 /// This class doesn't perform any aggregation.
46 ///
48 {
49 public:
51  : _resourceRegistry(resourceRegistry) {}
52 
53  /// Factory for creating HdBufferArray managed by
54  /// HdStVBOSimpleMemoryManager.
55  HDST_API
57  TfToken const &role,
58  HdBufferSpecVector const &bufferSpecs,
59  HdBufferArrayUsageHint usageHint);
60 
61  /// Factory for creating HdBufferArrayRange
62  HDST_API
64 
65  /// Returns id for given bufferSpecs to be used for aggregation
66  HDST_API
68  HdBufferSpecVector const &bufferSpecs,
69  HdBufferArrayUsageHint usageHint) const;
70 
71  /// Returns the buffer specs from a given buffer array
73  HdBufferArraySharedPtr const &bufferArray) const;
74 
75  /// Returns the size of the GPU memory used by the passed buffer array
76  virtual size_t GetResourceAllocation(
77  HdBufferArraySharedPtr const &bufferArray,
78  VtDictionary &result) const;
79 
80 protected:
81  class _SimpleBufferArray;
82 
83  /// \class _SimpleBufferArrayRange
84  ///
85  /// Specialized buffer array range for SimpleBufferArray.
86  ///
88  {
89  public:
90  /// Constructor.
92  : HdStBufferArrayRange(resourceRegistry)
93  , _bufferArray(nullptr)
94  , _numElements(0) {
95  }
96 
97  /// Returns true if this range is valid
98  bool IsValid() const override {
99  return (bool)_bufferArray;
100  }
101 
102  /// Returns true is the range has been assigned to a buffer
103  HDST_API
104  bool IsAssigned() const override;
105 
106  /// Returns true if this range is marked as immutable.
107  bool IsImmutable() const override;
108 
109  /// Resize memory area for this range. Returns true if it causes container
110  /// buffer reallocation.
111  bool Resize(int numElements) override {
112  _numElements = numElements;
113  return _bufferArray->Resize(numElements);
114  }
115 
116  /// Copy source data into buffer
117  HDST_API
118  void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
119 
120  /// Read back the buffer content
121  HDST_API
122  VtValue ReadData(TfToken const &name) const override;
123 
124  /// Returns the offset at which this range begins in the underlying
125  /// buffer array in terms of elements.
126  int GetElementOffset() const override {
127  return 0;
128  }
129 
130  /// Returns the byte offset at which this range begins in the underlying
131  /// buffer array for the given resource.
132  int GetByteOffset(TfToken const& resourceName) const override {
133  TF_UNUSED(resourceName);
134  return 0;
135  }
136 
137  /// Returns the number of elements allocated
138  size_t GetNumElements() const override {
139  return _numElements;
140  }
141 
142  /// Returns the capacity of allocated area for this range
143  int GetCapacity() const {
144  return _bufferArray->GetCapacity();
145  }
146 
147  /// Returns the version of the buffer array.
148  size_t GetVersion() const override {
149  return _bufferArray->GetVersion();
150  }
151 
152  /// Increment the version of the buffer array.
153  void IncrementVersion() override {
154  _bufferArray->IncrementVersion();
155  }
156 
157  /// Returns the max number of elements
158  HDST_API
159  size_t GetMaxNumElements() const override;
160 
161  /// Returns the usage hint from the underlying buffer array
162  HDST_API
163  HdBufferArrayUsageHint GetUsageHint() const override;
164 
165  /// Returns the GPU resource. If the buffer array contains more than one
166  /// resource, this method raises a coding error.
167  HDST_API
168  HdStBufferResourceSharedPtr GetResource() const override;
169 
170  /// Returns the named GPU resource.
171  HDST_API
173 
174  /// Returns the list of all named GPU resources for this bufferArrayRange.
175  HDST_API
176  HdStBufferResourceNamedList const& GetResources() const override;
177 
178  /// Sets the buffer array associated with this buffer;
179  HDST_API
180  void SetBufferArray(HdBufferArray *bufferArray) override;
181 
182  /// Debug dump
183  HDST_API
184  void DebugDump(std::ostream &out) const override;
185 
186  /// Make this range invalid
187  void Invalidate() {
188  _bufferArray = NULL;
189  }
190 
191  protected:
192  /// Returns the aggregation container
193  HDST_API
194  const void *_GetAggregation() const override;
195 
196  /// Adds a new, named GPU resource and returns it.
197  HDST_API
199  HdTupleType tupleType,
200  int offset,
201  int stride);
202 
203  private:
204  _SimpleBufferArray * _bufferArray;
205  size_t _numElements;
206  };
207 
209  std::shared_ptr<_SimpleBufferArray>;
211  std::shared_ptr<_SimpleBufferArrayRange>;
213  std::weak_ptr<_SimpleBufferArrayRange>;
214 
215  /// \class _SimpleBufferArray
216  ///
217  /// Simple buffer array (non-aggregated).
218  ///
219  class _SimpleBufferArray final : public HdBufferArray
220  {
221  public:
222  /// Constructor.
223  HDST_API
224  _SimpleBufferArray(HdStResourceRegistry* resourceRegistry,
225  TfToken const &role,
226  HdBufferSpecVector const &bufferSpecs,
227  HdBufferArrayUsageHint usageHint);
228 
229  /// Destructor. It invalidates _range
230  HDST_API
231  ~_SimpleBufferArray() override;
232 
233  /// perform compaction if necessary, returns true if it becomes empty.
234  HDST_API
235  bool GarbageCollect() override;
236 
237  /// Debug output
238  HDST_API
239  void DebugDump(std::ostream &out) const override;
240 
241  /// Set to resize buffers. Actual reallocation happens on Reallocate()
242  HDST_API
243  bool Resize(int numElements);
244 
245  /// Performs reallocation.
246  /// GLX context has to be set when calling this function.
247  HDST_API
248  void Reallocate(
249  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
250  HdBufferArraySharedPtr const &curRangeOwner) override;
251 
252  /// Returns the maximum number of elements capacity.
253  HDST_API
254  size_t GetMaxNumElements() const override;
255 
256  /// Returns current capacity. It could be different from numElements.
257  int GetCapacity() const {
258  return _capacity;
259  }
260 
261  /// TODO: We need to distinguish between the primvar types here, we should
262  /// tag each HdBufferSource and HdBufferResource with Constant, Uniform,
263  /// Varying, Vertex, or FaceVarying and provide accessors for the specific
264  /// buffer types.
265 
266  /// Returns the GPU resource. If the buffer array contains more than one
267  /// resource, this method raises a coding error.
268  HDST_API
270 
271  /// Returns the named GPU resource. This method returns the first found
272  /// resource. In HD_SAFE_MODE it checks all underlying GL buffers
273  /// in _resourceMap and raises a coding error if there are more than
274  /// one GL buffers exist.
275  HDST_API
277 
278  /// Returns the list of all named GPU resources for this bufferArray.
279  HdStBufferResourceNamedList const& GetResources() const {return _resourceList;}
280 
281  /// Reconstructs the bufferspecs and returns it (for buffer splitting)
282  HDST_API
284 
285  protected:
286  HDST_API
287  void _DeallocateResources();
288 
289  /// Adds a new, named GPU resource and returns it.
290  HDST_API
292  HdTupleType tupleType,
293  int offset,
294  int stride);
295  private:
296  HdStResourceRegistry* const _resourceRegistry;
297  int _capacity;
298  size_t _maxBytesPerElement;
299 
300  HdStBufferResourceNamedList _resourceList;
301 
302  _SimpleBufferArrayRangeSharedPtr _GetRangeSharedPtr() const {
303  return GetRangeCount() > 0
304  ? std::static_pointer_cast<_SimpleBufferArrayRange>(GetRange(0).lock())
306  }
307  };
308 
310 };
311 
313 
314 #endif // PXR_IMAGING_HD_ST_VBO_SIMPLE_MEMORY_MANAGER_H
int GetCapacity() const
Returns the capacity of allocated area for this range.
HDST_API size_t GetMaxNumElements() const override
Returns the max number of elements.
std::shared_ptr< class HdBufferArray > HdBufferArraySharedPtr
Definition: bufferArray.h:44
int GetByteOffset(TfToken const &resourceName) const override
HDST_API void CopyData(HdBufferSourceSharedPtr const &bufferSource) override
Copy source data into buffer.
bool IsImmutable() const override
Returns true if this range is marked as immutable.
HDST_API bool IsAssigned() const override
Returns true is the range has been assigned to a buffer.
size_t AggregationId
Aggregation ID.
Definition: strategyBase.h:51
std::vector< std::pair< TfToken, HdStBufferResourceSharedPtr > > HdStBufferResourceNamedList
HdStResourceRegistry *const _resourceRegistry
GLuint const GLchar * name
Definition: glcorearb.h:785
HDST_API void DebugDump(std::ostream &out) const override
Debug output.
int GetCapacity() const
Returns current capacity. It could be different from numElements.
HDST_API ~_SimpleBufferArray() override
Destructor. It invalidates _range.
HDST_API VtValue ReadData(TfToken const &name) const override
Read back the buffer content.
std::weak_ptr< _SimpleBufferArrayRange > _SimpleBufferArrayRangePtr
HDST_API HdStBufferResourceSharedPtr GetResource() const
bool IsValid() const override
Returns true if this range is valid.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
Definition: token.h:87
GLuint64EXT * result
Definition: glew.h:14311
void IncrementVersion() override
Increment the version of the buffer array.
size_t GetVersion() const override
Returns the version of the buffer array.
HD_API void IncrementVersion()
Increments the version of this buffer array.
HDST_API void SetBufferArray(HdBufferArray *bufferArray) override
Sets the buffer array associated with this buffer;.
virtual HDST_API HdBufferArrayRangeSharedPtr CreateBufferArrayRange()
Factory for creating HdBufferArrayRange.
HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner) override
HDST_API const void * _GetAggregation() const override
Returns the aggregation container.
HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
HDST_API void DebugDump(std::ostream &out) const override
Debug dump.
std::vector< struct HdBufferSpec > HdBufferSpecVector
HDST_API size_t GetMaxNumElements() const override
Returns the maximum number of elements capacity.
std::shared_ptr< class HdStBufferResource > HdStBufferResourceSharedPtr
HDST_API bool GarbageCollect() override
perform compaction if necessary, returns true if it becomes empty.
_SimpleBufferArrayRange(HdStResourceRegistry *resourceRegistry)
Constructor.
virtual HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const
Returns the buffer specs from a given buffer array.
size_t GetNumElements() const override
Returns the number of elements allocated.
size_t GetVersion() const
Definition: bufferArray.h:104
HDST_API HdStBufferResourceSharedPtr GetResource() const override
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
virtual HDST_API HdAggregationStrategy::AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
#define HDST_API
Definition: api.h:40
HDST_API HdStBufferResourceSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
#define TF_UNUSED(x)
Definition: tf.h:185
HDST_API _SimpleBufferArray(HdStResourceRegistry *resourceRegistry, TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Constructor.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:45
HDST_API bool Resize(int numElements)
Set to resize buffers. Actual reallocation happens on Reallocate()
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
HDST_API HdStBufferResourceSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
HDST_API HdStBufferResourceNamedList const & GetResources() const override
Returns the list of all named GPU resources for this bufferArrayRange.
GLintptr offset
Definition: glcorearb.h:664
size_t GetRangeCount() const
How many ranges are attached to the buffer array.
Definition: bufferArray.h:138
std::shared_ptr< _SimpleBufferArrayRange > _SimpleBufferArrayRangeSharedPtr
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:871
HdStVBOSimpleMemoryManager(HdStResourceRegistry *resourceRegistry)
std::shared_ptr< class HdBufferSource > HdBufferSourceSharedPtr
std::shared_ptr< _SimpleBufferArray > _SimpleBufferArraySharedPtr
Definition: value.h:168
HdStBufferResourceNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
virtual HDST_API HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
virtual size_t GetResourceAllocation(HdBufferArraySharedPtr const &bufferArray, VtDictionary &result) const
Returns the size of the GPU memory used by the passed buffer array.