HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vboMemoryManager.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HD_ST_VBO_MEMORY_MANAGER_H
8 #define PXR_IMAGING_HD_ST_VBO_MEMORY_MANAGER_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hdSt/api.h"
13 
14 #include "pxr/imaging/hgi/enums.h"
15 
20 
21 #include "pxr/base/tf/mallocTag.h"
22 #include "pxr/base/tf/token.h"
23 
24 #include <list>
25 #include <memory>
26 
28 
30 
31 /// \class HdStVBOMemoryManager
32 ///
33 /// VBO memory manager.
34 ///
36 {
37 public:
40  , _resourceRegistry(resourceRegistry) {}
41 
42  /// Factory for creating HdBufferArray managed by
43  /// HdStVBOMemoryManager aggregation.
44  HDST_API
46  TfToken const &role,
47  HdBufferSpecVector const &bufferSpecs,
48  HdBufferArrayUsageHint usageHint);
49 
50  /// Factory for creating HdBufferArrayRange managed by
51  /// HdStVBOMemoryManager aggregation.
52  HDST_API
54 
55  /// Returns id for given bufferSpecs to be used for aggregation
56  HDST_API
58  HdBufferSpecVector const &bufferSpecs,
59  HdBufferArrayUsageHint usageHint) const;
60 
61  /// Returns the buffer specs from a given buffer array
63  HdBufferArraySharedPtr const &bufferArray) const;
64 
65  /// Returns the size of the GPU memory used by the passed buffer array
66  virtual size_t GetResourceAllocation(
67  HdBufferArraySharedPtr const &bufferArray,
68  VtDictionary &result) const;
69 
70 protected:
71  class _StripedBufferArray;
72 
73  /// specialized buffer array range
75  {
76  public:
77  /// Constructor.
79  : HdStBufferArrayRange(resourceRegistry),
80  _stripedBufferArray(nullptr),
81  _elementOffset(0),
82  _numElements(0),
83  _capacity(0)
84  {
85  }
86 
87  /// Destructor.
88  HDST_API
89  ~_StripedBufferArrayRange() override;
90 
91  /// Returns true if this range is valid
92  bool IsValid() const override {
93  return (bool)_stripedBufferArray;
94  }
95 
96  /// Returns true is the range has been assigned to a buffer
97  HDST_API
98  bool IsAssigned() const override;
99 
100  /// Returns true if this bar is marked as immutable.
101  bool IsImmutable() const override;
102 
103  /// Returns true if this needs a staging buffer for CPU to GPU copies.
104  bool RequiresStaging() const override;
105 
106  /// Resize memory area for this range. Returns true if it causes container
107  /// buffer reallocation.
108  HDST_API
109  bool Resize(int numElements) override;
110 
111  /// Copy source data into buffer
112  HDST_API
113  void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
114 
115  /// Read back the buffer content
116  HDST_API
117  VtValue ReadData(TfToken const &name) const override;
118 
119  /// Returns the relative element offset in aggregated buffer
120  int GetElementOffset() const override {
121  return _elementOffset;
122  }
123 
124  /// Returns the byte offset at which this range begins in the underlying
125  /// buffer array for the given resource.
126  int GetByteOffset(TfToken const& resourceName) const override;
127 
128  /// Returns the number of elements
129  size_t GetNumElements() const override {
130  return _numElements;
131  }
132 
133  /// Returns the version of the buffer array.
134  size_t GetVersion() const override {
135  return _stripedBufferArray->GetVersion();
136  }
137 
138  /// Increment the version of the buffer array.
139  void IncrementVersion() override {
140  _stripedBufferArray->IncrementVersion();
141  }
142 
143  /// Returns the max number of elements
144  HDST_API
145  size_t GetMaxNumElements() const override;
146 
147  /// Returns the usage hint from the underlying buffer array
148  HDST_API
149  HdBufferArrayUsageHint GetUsageHint() const override;
150 
151  /// Returns the GPU resource. If the buffer array contains more than one
152  /// resource, this method raises a coding error.
153  HDST_API
154  HdStBufferResourceSharedPtr GetResource() const override;
155 
156  /// Returns the named GPU resource.
157  HDST_API
159 
160  /// Returns the list of all named GPU resources for this bufferArrayRange.
161  HDST_API
162  HdStBufferResourceNamedList const& GetResources() const override;
163 
164  /// Sets the buffer array associated with this buffer;
165  HDST_API
166  void SetBufferArray(HdBufferArray *bufferArray) override;
167 
168  /// Debug dump
169  HDST_API
170  void DebugDump(std::ostream &out) const override;
171 
172  /// Set the relative offset for this range.
174  _elementOffset = offset;
175  }
176 
177  /// Set the number of elements for this range.
178  void SetNumElements(int numElements) {
179  _numElements = numElements;
180  }
181 
182  /// Returns the capacity of allocated area
183  int GetCapacity() const {
184  return _capacity;
185  }
186 
187  /// Set the capacity of allocated area for this range.
188  void SetCapacity(int capacity) {
189  _capacity = capacity;
190  }
191 
192  /// Make this range invalid
193  void Invalidate() {
194  _stripedBufferArray = NULL;
195  }
196 
197  protected:
198  /// Returns the aggregation container
199  HDST_API
200  const void *_GetAggregation() const override;
201 
202  private:
203  // Returns the byte offset at which the BAR begins for the resource.
204  size_t _GetByteOffset(HdStBufferResourceSharedPtr const& resource)
205  const;
206 
207  // holding a weak reference to container.
208  // this pointer becomes null when the StripedBufferArray gets destructed,
209  // in case if any drawItem still holds this bufferRange.
210  _StripedBufferArray *_stripedBufferArray;
211  int _elementOffset;
212  size_t _numElements;
213  int _capacity;
214  };
215 
217  std::shared_ptr<_StripedBufferArray>;
219  std::shared_ptr<_StripedBufferArrayRange>;
221  std::weak_ptr<_StripedBufferArrayRange>;
222 
223  /// striped buffer array
225  {
226  public:
227  /// Constructor.
228  HDST_API
229  _StripedBufferArray(HdStResourceRegistry* resourceRegistry,
230  TfToken const &role,
231  HdBufferSpecVector const &bufferSpecs,
232  HdBufferArrayUsageHint usageHint);
233 
234  /// Destructor. It invalidates _rangeList
235  HDST_API
236  ~_StripedBufferArray() override;
237 
238  /// perform compaction if necessary. If it becomes empty, release all
239  /// resources and returns true
240  HDST_API
241  bool GarbageCollect() override;
242 
243  /// Debug output
244  HDST_API
245  void DebugDump(std::ostream &out) const override;
246 
247  /// Performs reallocation.
248  /// GLX context has to be set when calling this function.
249  HDST_API
250  void Reallocate(
251  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
252  HdBufferArraySharedPtr const &curRangeOwner) override;
253 
254  /// Returns the maximum number of elements capacity.
255  HDST_API
256  size_t GetMaxNumElements() const override;
257 
258  /// Mark to perform reallocation on Reallocate()
260  _needsReallocation = true;
261  }
262 
263  /// Mark to perform compaction on GarbageCollect()
265  _needsCompaction = true;
266  }
267 
268  /// Returns the GPU resource. If the buffer array contains more
269  /// than one resource, this method raises a coding error.
270  HDST_API
272 
273  /// Returns the named GPU resource. This method returns the first found
274  /// resource. In HD_SAFE_MODE it checks all underlying GL buffers
275  /// in _resourceMap and raises a coding error if there are more than
276  /// one GL buffers exist.
277  HDST_API
279 
280  /// Returns the list of all named GPU resources for this bufferArray.
282  {return _resourceList;}
283 
284  /// Reconstructs the bufferspecs and returns it (for buffer splitting)
285  HDST_API
287 
288  protected:
289  HDST_API
290  void _DeallocateResources();
291 
292  /// Adds a new, named GPU resource and returns it.
293  HDST_API
295  HdTupleType tupleType,
296  int offset,
297  int stride);
298 
299  private:
300 
301  HdStResourceRegistry* _resourceRegistry;
302  bool _needsCompaction;
303  int _totalCapacity;
304  size_t _maxBytesPerElement;
305  HgiBufferUsage _bufferUsage;
306 
307  HdStBufferResourceNamedList _resourceList;
308 
309  // Helper routine to cast the range shared pointer.
310  _StripedBufferArrayRangeSharedPtr _GetRangeSharedPtr(size_t idx) const {
311  return std::static_pointer_cast<_StripedBufferArrayRange>(GetRange(idx).lock());
312  }
313  };
314 
316 };
317 
319 
320 #endif // PXR_IMAGING_HD_ST_VBO_MEMORY_MANAGER_H
int GetByteOffset(TfToken const &resourceName) const override
HDST_API const void * _GetAggregation() const override
Returns the aggregation container.
bool IsImmutable() const override
Returns true if this bar is marked as immutable.
bool _needsReallocation
Definition: bufferArray.h:147
bool IsValid() const override
Returns true if this range is valid.
std::shared_ptr< class HdBufferArray > HdBufferArraySharedPtr
Definition: bufferArray.h:26
void SetNeedsCompaction()
Mark to perform compaction on GarbageCollect()
_StripedBufferArrayRange(HdStResourceRegistry *resourceRegistry)
Constructor.
HDST_API void CopyData(HdBufferSourceSharedPtr const &bufferSource) override
Copy source data into buffer.
virtual HDST_API HdBufferArrayRangeSharedPtr CreateBufferArrayRange()
std::vector< std::pair< TfToken, HdStBufferResourceSharedPtr > > HdStBufferResourceNamedList
HDST_API VtValue ReadData(TfToken const &name) const override
Read back the buffer content.
void IncrementVersion() override
Increment the version of the buffer array.
HDST_API bool GarbageCollect() override
HDST_API ~_StripedBufferArray() override
Destructor. It invalidates _rangeList.
HDST_API ~_StripedBufferArrayRange() override
Destructor.
**But if you need a result
Definition: thread.h:622
HDST_API bool Resize(int numElements) override
HDST_API size_t GetMaxNumElements() const override
Returns the maximum number of elements capacity.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
size_t AggregationId
Aggregation ID.
Definition: strategyBase.h:34
HdStBufferResourceNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
Definition: token.h:70
GLintptr offset
Definition: glcorearb.h:665
HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner) override
HD_API void IncrementVersion()
Increments the version of this buffer array.
HdStVBOMemoryManager(HdStResourceRegistry *resourceRegistry)
bool RequiresStaging() const override
Returns true if this needs a staging buffer for CPU to GPU copies.
virtual size_t GetResourceAllocation(HdBufferArraySharedPtr const &bufferArray, VtDictionary &result) const
Returns the size of the GPU memory used by the passed buffer array.
int GetElementOffset() const override
Returns the relative element offset in aggregated buffer.
size_t GetNumElements() const override
Returns the number of elements.
std::weak_ptr< _StripedBufferArrayRange > _StripedBufferArrayRangePtr
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:872
HDST_API bool IsAssigned() const override
Returns true is the range has been assigned to a buffer.
HDST_API _StripedBufferArray(HdStResourceRegistry *resourceRegistry, TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Constructor.
HDST_API void DebugDump(std::ostream &out) const override
Debug output.
HDST_API HdStBufferResourceSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
void SetElementOffset(int offset)
Set the relative offset for this range.
std::shared_ptr< _StripedBufferArray > _StripedBufferArraySharedPtr
GLuint const GLchar * name
Definition: glcorearb.h:786
std::vector< struct HdBufferSpec > HdBufferSpecVector
int GetCapacity() const
Returns the capacity of allocated area.
std::shared_ptr< class HdStBufferResource > HdStBufferResourceSharedPtr
HDST_API HdStBufferResourceNamedList const & GetResources() const override
Returns the list of all named GPU resources for this bufferArrayRange.
size_t GetVersion() const
Definition: bufferArray.h:84
HDST_API void DebugDump(std::ostream &out) const override
Debug dump.
HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
void Invalidate()
Make this range invalid.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HgiBits HgiBufferUsage
Definition: enums.h:315
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
void SetNumElements(int numElements)
Set the number of elements for this range.
#define HDST_API
Definition: api.h:23
size_t GetVersion() const override
Returns the version of the buffer array.
uint32_t HdBufferArrayUsageHint
Definition: bufferArray.h:60
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:27
void SetNeedsReallocation()
Mark to perform reallocation on Reallocate()
HDST_API size_t GetMaxNumElements() const override
Returns the max number of elements.
HDST_API HdStBufferResourceSharedPtr GetResource() const
virtual HDST_API AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
HDST_API void SetBufferArray(HdBufferArray *bufferArray) override
Sets the buffer array associated with this buffer;.
void SetCapacity(int capacity)
Set the capacity of allocated area for this range.
std::shared_ptr< class HdBufferSource > HdBufferSourceSharedPtr
HDST_API HdStBufferResourceSharedPtr GetResource() const override
HdStResourceRegistry * _resourceRegistry
std::shared_ptr< _StripedBufferArrayRange > _StripedBufferArrayRangeSharedPtr
virtual HDST_API HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
virtual HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const
Returns the buffer specs from a given buffer array.
Definition: value.h:146