All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vboMemoryManager.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 HDST_VBO_MEMORY_MANAGER_H
25 #define HDST_VBO_MEMORY_MANAGER_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
29 #include "pxr/imaging/hd/version.h"
30 #include "pxr/imaging/hd/bufferArray.h"
31 #include "pxr/imaging/hdSt/bufferArrayRangeGL.h"
32 #include "pxr/imaging/hd/bufferSpec.h"
33 #include "pxr/imaging/hd/bufferSource.h"
34 #include "pxr/imaging/hd/strategyBase.h"
35 
36 #include "pxr/base/tf/mallocTag.h"
37 #include "pxr/base/tf/token.h"
38 
39 #include <boost/shared_ptr.hpp>
40 #include <list>
41 
42 PXR_NAMESPACE_OPEN_SCOPE
43 
44 
50 public:
52 
55  HDST_API
56  virtual HdBufferArraySharedPtr CreateBufferArray(
57  TfToken const &role,
58  HdBufferSpecVector const &bufferSpecs,
59  HdBufferArrayUsageHint usageHint);
60 
63  HDST_API
64  virtual HdBufferArrayRangeSharedPtr CreateBufferArrayRange();
65 
67  HDST_API
69  HdBufferSpecVector const &bufferSpecs,
70  HdBufferArrayUsageHint usageHint) const;
71 
73  virtual HdBufferSpecVector GetBufferSpecs(
74  HdBufferArraySharedPtr const &bufferArray) const;
75 
77  virtual size_t GetResourceAllocation(
78  HdBufferArraySharedPtr const &bufferArray,
79  VtDictionary &result) const;
80 
81 protected:
82  class _StripedBufferArray;
83 
86  public:
89  : _stripedBufferArray(nullptr),
90  _offset(0),
91  _numElements(0),
92  _capacity(0)
93  {
94  }
95 
97  HDST_API
98  virtual ~_StripedBufferArrayRange();
99 
101  virtual bool IsValid() const {
102  return (bool)_stripedBufferArray;
103  }
104 
106  HDST_API
107  virtual bool IsAssigned() const;
108 
110  virtual bool IsImmutable() const;
111 
114  HDST_API
115  virtual bool Resize(int numElements);
116 
118  HDST_API
119  virtual void CopyData(HdBufferSourceSharedPtr const &bufferSource);
120 
122  HDST_API
123  virtual VtValue ReadData(TfToken const &name) const;
124 
126  virtual int GetOffset() const {
127  return _offset;
128  }
129 
131  virtual int GetIndex() const {
132  // note: range doesn't store index, so we need to sweep rangeLists
133  // to find the index of this range.
134  TF_CODING_ERROR("vboMemoryManager doesn't support GetIndex() for "
135  "memory and performance reasons\n");
136  return 0;
137  }
138 
140  virtual size_t GetNumElements() const {
141  return _numElements;
142  }
143 
145  virtual size_t GetVersion() const {
146  return _stripedBufferArray->GetVersion();
147  }
148 
150  virtual void IncrementVersion() {
151  _stripedBufferArray->IncrementVersion();
152  }
153 
155  HDST_API
156  virtual size_t GetMaxNumElements() const;
157 
159  HDST_API
160  virtual HdBufferArrayUsageHint GetUsageHint() const override;
161 
164  HDST_API
165  virtual HdStBufferResourceGLSharedPtr GetResource() const;
166 
168  HDST_API
169  virtual HdStBufferResourceGLSharedPtr GetResource(TfToken const& name);
170 
172  HDST_API
173  virtual HdStBufferResourceGLNamedList const& GetResources() const;
174 
176  HDST_API
177  virtual void SetBufferArray(HdBufferArray *bufferArray);
178 
180  HDST_API
181  virtual void DebugDump(std::ostream &out) const;
182 
184  void SetOffset(int offset) {
185  _offset = offset;
186  }
187 
189  void SetNumElements(int numElements) {
190  _numElements = numElements;
191  }
192 
194  int GetCapacity() const {
195  return _capacity;
196  }
197 
199  void SetCapacity(int capacity) {
200  _capacity = capacity;
201  }
202 
204  void Invalidate() {
205  _stripedBufferArray = NULL;
206  }
207 
208  protected:
210  HDST_API
211  virtual const void *_GetAggregation() const;
212 
213  private:
214  // holding a weak reference to container.
215  // this pointer becomes null when the StripedBufferArray gets destructed,
216  // in case if any drawItem still holds this bufferRange.
217  _StripedBufferArray *_stripedBufferArray;
218  int _offset;
219  size_t _numElements;
220  int _capacity;
221  };
222 
223  typedef boost::shared_ptr<_StripedBufferArray>
224  _StripedBufferArraySharedPtr;
225  typedef boost::shared_ptr<_StripedBufferArrayRange>
226  _StripedBufferArrayRangeSharedPtr;
227  typedef boost::weak_ptr<_StripedBufferArrayRange>
228  _StripedBufferArrayRangePtr;
229 
232  public:
234  HDST_API
235  _StripedBufferArray(TfToken const &role,
236  HdBufferSpecVector const &bufferSpecs,
237  HdBufferArrayUsageHint usageHint);
238 
240  HDST_API
241  virtual ~_StripedBufferArray();
242 
245  HDST_API
246  virtual bool GarbageCollect();
247 
249  HDST_API
250  virtual void DebugDump(std::ostream &out) const;
251 
254  HDST_API
255  virtual void Reallocate(
256  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
257  HdBufferArraySharedPtr const &curRangeOwner);
258 
260  HDST_API
261  virtual size_t GetMaxNumElements() const;
262 
265  _needsReallocation = true;
266  }
267 
270  _needsCompaction = true;
271  }
272 
277 
280  HDST_API
281  HdStBufferResourceGLSharedPtr GetResource() const;
282 
287  HDST_API
288  HdStBufferResourceGLSharedPtr GetResource(TfToken const& name);
289 
291  HdStBufferResourceGLNamedList const& GetResources() const
292  {return _resourceList;}
293 
295  HDST_API
296  HdBufferSpecVector GetBufferSpecs() const;
297 
298  protected:
299  HDST_API
300  void _DeallocateResources();
301 
303  HDST_API
304  HdStBufferResourceGLSharedPtr _AddResource(TfToken const& name,
305  HdTupleType tupleType,
306  int offset,
307  int stride);
308 
309  private:
310 
311  bool _needsCompaction;
312  int _totalCapacity;
313  size_t _maxBytesPerElement;
314 
315  HdStBufferResourceGLNamedList _resourceList;
316 
317  // Helper routine to cast the range shared pointer.
318  _StripedBufferArrayRangeSharedPtr _GetRangeSharedPtr(size_t idx) const {
319  return boost::static_pointer_cast<_StripedBufferArrayRange>(GetRange(idx).lock());
320  }
321  };
322 };
323 
324 PXR_NAMESPACE_CLOSE_SCOPE
325 
326 #endif // HDST_VBO_MEMORY_MANAGER_H
virtual size_t GetNumElements() const
Returns the number of elements.
size_t AggregationId
Aggregation ID.
Definition: strategyBase.h:51
The union provides a set of flags that provide hints to the memory management system about the proper...
Definition: bufferArray.h:74
virtual bool IsImmutable() const
Returns true if this bar is marked as immutable.
virtual HDST_API HdStBufferResourceGLSharedPtr GetResource() const
Returns the GPU resource.
HdStBufferResourceGLNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
virtual HDST_API HdStBufferResourceGLNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArrayRange.
Similar to a VAO, this object is a bundle of coherent buffers.
Definition: bufferArray.h:92
virtual HDST_API void DebugDump(std::ostream &out) const
Debug output.
Aggregation strategy base class.
Definition: strategyBase.h:48
HD_API void IncrementVersion()
Increments the version of this buffer array.
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
virtual HDST_API HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Factory for creating HdBufferArray managed by HdStVBOMemoryManager aggregation.
HdTupleType represents zero, one, or more values of the same HdType.
Definition: types.h:239
A map with string keys and VtValue values.
Definition: dictionary.h:61
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:87
virtual bool IsValid() const
Returns true if this range is valid.
virtual HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const
Returns the buffer specs from a given buffer array.
int GetCapacity() const
Returns the capacity of allocated area.
virtual int GetIndex() const
Returns the index for this range.
virtual HDST_API VtValue ReadData(TfToken const &name) const
Read back the buffer content.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
void SetOffset(int offset)
Set the relative offset for this range.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
HDST_API HdStBufferResourceGLSharedPtr GetResource() const
TODO: We need to distinguish between the primvar types here, we should tag each HdBufferSource and Hd...
virtual HDST_API const void * _GetAggregation() const
Returns the aggregation container.
virtual HDST_API bool Resize(int numElements)
Resize memory area for this range.
void SetCapacity(int capacity)
Set the capacity of allocated area for this range.
void SetNumElements(int numElements)
Set the number of elements for this range.
virtual HDST_API size_t GetMaxNumElements() const
Returns the maximum number of elements capacity.
virtual HDST_API bool IsAssigned() const
Returns true is the range has been assigned to a buffer.
virtual HDST_API void DebugDump(std::ostream &out) const
Debug dump.
virtual void IncrementVersion()
Increment the version of the buffer array.
void Invalidate()
Make this range invalid.
Interface class for representing range (subset) locator of HdBufferArray.
void SetNeedsReallocation()
Mark to perform reallocation on Reallocate()
HDST_API _StripedBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Constructor.
virtual HDST_API AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
virtual HDST_API bool GarbageCollect()
perform compaction if necessary.
virtual size_t GetVersion() const
Returns the version of the buffer array.
void SetNeedsCompaction()
Mark to perform compaction on GarbageCollect()
virtual int GetOffset() const
Returns the relative offset in aggregated buffer.
size_t GetVersion() const
Returns the version of this buffer array.
Definition: bufferArray.h:108
HDST_API HdStBufferResourceGLSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
virtual HDST_API HdBufferArrayRangeSharedPtr CreateBufferArrayRange()
Factory for creating HdBufferArrayRange managed by HdStVBOMemoryManager aggregation.
bool _needsReallocation
Dirty bit to set when the ranges attached to the buffer changes.
Definition: bufferArray.h:171
virtual HDST_API ~_StripedBufferArray()
Destructor. It invalidates _rangeList.
virtual HDST_API ~_StripedBufferArrayRange()
Destructor.
virtual HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner)
Performs reallocation.
virtual HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
VBO memory manager.
virtual size_t GetResourceAllocation(HdBufferArraySharedPtr const &bufferArray, VtDictionary &result) const
Returns the size of the GPU memory used by the passed buffer array.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182
virtual HDST_API void SetBufferArray(HdBufferArray *bufferArray)
Sets the buffer array associated with this buffer;.
virtual HDST_API void CopyData(HdBufferSourceSharedPtr const &bufferSource)
Copy source data into buffer.
virtual HDST_API size_t GetMaxNumElements() const
Returns the max number of elements.