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 
12 #include "pxr/base/tf/envSetting.h"
13 
14 #include "pxr/imaging/hdSt/api.h"
17 
18 #include "pxr/imaging/hgi/enums.h"
19 
23 
24 #include <list>
25 #include <memory>
26 
28 
31 
33 
34 /// \class HdStVBOMemoryManager
35 ///
36 /// VBO memory manager.
37 ///
39 {
40 public:
43  , _resourceRegistry(resourceRegistry) {}
44 
45  /// Factory for creating HdBufferArray managed by
46  /// HdStVBOMemoryManager aggregation.
47  HDST_API
49  TfToken const &role,
50  HdBufferSpecVector const &bufferSpecs,
51  HdBufferArrayUsageHint usageHint);
52 
53  /// Factory for creating HdBufferArrayRange managed by
54  /// HdStVBOMemoryManager aggregation.
55  HDST_API
57 
58  /// Returns id for given bufferSpecs to be used for aggregation
59  HDST_API
61  HdBufferSpecVector const &bufferSpecs,
62  HdBufferArrayUsageHint usageHint) const;
63 
64  /// Returns the buffer specs from a given buffer array
66  HdBufferArraySharedPtr const &bufferArray) const;
67 
68  /// Returns the size of the GPU memory used by the passed buffer array
69  virtual size_t GetResourceAllocation(
70  HdBufferArraySharedPtr const &bufferArray,
71  VtDictionary &result) const;
72 
73 protected:
74  class _StripedBufferArray;
75 
76  /// specialized buffer array range
78  {
79  public:
80  /// Constructor.
82  : HdStBufferArrayRange(resourceRegistry),
83  _stripedBufferArray(nullptr),
84  _elementOffset(0),
85  _numElements(0),
86  _capacity(0)
87  {
88  }
89 
90  /// Destructor.
91  HDST_API
92  ~_StripedBufferArrayRange() override;
93 
94  /// Returns true if this range is valid
95  bool IsValid() const override {
96  return (bool)_stripedBufferArray;
97  }
98 
99  /// Returns true is the range has been assigned to a buffer
100  HDST_API
101  bool IsAssigned() const override;
102 
103  /// Returns true if this bar is marked as immutable.
104  bool IsImmutable() const override;
105 
106  /// Returns true if this needs a staging buffer for CPU to GPU copies.
107  bool RequiresStaging() const override;
108 
109  /// Resize memory area for this range. Returns true if it causes container
110  /// buffer reallocation.
111  HDST_API
112  bool Resize(int numElements) override;
113 
114  /// Copy source data into buffer
115  HDST_API
116  void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
117 
118  /// Read back the buffer content
119  HDST_API
120  VtValue ReadData(TfToken const &name) const override;
121 
122  /// Returns the relative element offset in aggregated buffer
123  int GetElementOffset() const override {
124  return _elementOffset;
125  }
126 
127  /// Returns the byte offset at which this range begins in the underlying
128  /// buffer array for the given resource.
129  int GetByteOffset(TfToken const& resourceName) const override;
130 
131  /// Returns the number of elements
132  size_t GetNumElements() const override {
133  return _numElements;
134  }
135 
136  /// Returns the version of the buffer array.
137  size_t GetVersion() const override {
138  return _stripedBufferArray->GetVersion();
139  }
140 
141  /// Increment the version of the buffer array.
142  void IncrementVersion() override {
143  _stripedBufferArray->IncrementVersion();
144  }
145 
146  /// Returns the max number of elements
147  HDST_API
148  size_t GetMaxNumElements() const override;
149 
150  /// Returns the usage hint from the underlying buffer array
151  HDST_API
152  HdBufferArrayUsageHint GetUsageHint() const override;
153 
154  /// Returns the GPU resource. If the buffer array contains more than one
155  /// resource, this method raises a coding error.
156  HDST_API
157  HdStBufferResourceSharedPtr GetResource() const override;
158 
159  /// Returns the named GPU resource.
160  HDST_API
162 
163  /// Returns the list of all named GPU resources for this bufferArrayRange.
164  HDST_API
165  HdStBufferResourceNamedList const& GetResources() const override;
166 
167  /// Sets the buffer array associated with this buffer;
168  HDST_API
169  void SetBufferArray(HdBufferArray *bufferArray) override;
170 
171  /// Debug dump
172  HDST_API
173  void DebugDump(std::ostream &out) const override;
174 
175  /// Set the relative offset for this range.
177  _elementOffset = offset;
178  }
179 
180  /// Set the number of elements for this range.
181  void SetNumElements(int numElements) {
182  _numElements = numElements;
183  }
184 
185  /// Returns the capacity of allocated area
186  int GetCapacity() const {
187  return _capacity;
188  }
189 
190  /// Set the capacity of allocated area for this range.
191  void SetCapacity(int capacity) {
192  _capacity = capacity;
193  }
194 
195  /// Make this range invalid
196  void Invalidate() {
197  _stripedBufferArray = NULL;
198  }
199 
200  protected:
201  /// Returns the aggregation container
202  HDST_API
203  const void *_GetAggregation() const override;
204 
205  private:
206  // Returns the byte offset at which the BAR begins for the resource.
207  size_t _GetByteOffset(HdStBufferResourceSharedPtr const& resource)
208  const;
209 
210  // holding a weak reference to container.
211  // this pointer becomes null when the StripedBufferArray gets destructed,
212  // in case if any drawItem still holds this bufferRange.
213  _StripedBufferArray *_stripedBufferArray;
214  int _elementOffset;
215  size_t _numElements;
216  int _capacity;
217  };
218 
220  std::shared_ptr<_StripedBufferArray>;
222  std::shared_ptr<_StripedBufferArrayRange>;
224  std::weak_ptr<_StripedBufferArrayRange>;
225 
226  /// striped buffer array
228  {
229  public:
230  /// Constructor.
231  HDST_API
232  _StripedBufferArray(HdStResourceRegistry* resourceRegistry,
233  TfToken const &role,
234  HdBufferSpecVector const &bufferSpecs,
235  HdBufferArrayUsageHint usageHint);
236 
237  /// Destructor. It invalidates _rangeList
238  HDST_API
239  ~_StripedBufferArray() override;
240 
241  /// perform compaction if necessary. If it becomes empty, release all
242  /// resources and returns true
243  HDST_API
244  bool GarbageCollect() override;
245 
246  /// Debug output
247  HDST_API
248  void DebugDump(std::ostream &out) const override;
249 
250  /// Performs reallocation.
251  /// GLX context has to be set when calling this function.
252  HDST_API
253  void Reallocate(
254  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
255  HdBufferArraySharedPtr const &curRangeOwner) override;
256 
257  /// Returns the maximum number of elements capacity.
258  HDST_API
259  size_t GetMaxNumElements() const override;
260 
261  /// Mark to perform reallocation on Reallocate()
263  _needsReallocation = true;
264  }
265 
266  /// Mark to perform compaction on GarbageCollect()
268  _needsCompaction = true;
269  }
270 
271  /// Returns the GPU resource. If the buffer array contains more
272  /// than one resource, this method raises a coding error.
273  HDST_API
275 
276  /// Returns the named GPU resource. This method returns the first found
277  /// resource. In HD_SAFE_MODE it checks all underlying GL buffers
278  /// in _resourceMap and raises a coding error if there are more than
279  /// one GL buffers exist.
280  HDST_API
282 
283  /// Returns the list of all named GPU resources for this bufferArray.
285  {return _resourceList;}
286 
287  /// Reconstructs the bufferspecs and returns it (for buffer splitting)
288  HDST_API
290 
291  protected:
292  HDST_API
293  void _DeallocateResources();
294 
295  /// Adds a new, named GPU resource and returns it.
296  HDST_API
298  HdTupleType tupleType,
299  int offset,
300  int stride);
301 
302  private:
303 
304  HdStResourceRegistry* _resourceRegistry;
305  bool _needsCompaction;
306  int _totalCapacity;
307  size_t _maxBytesPerElement;
308  HgiBufferUsage _bufferUsage;
309 
310  HdStBufferResourceNamedList _resourceList;
311 
312  // Helper routine to cast the range shared pointer.
313  _StripedBufferArrayRangeSharedPtr _GetRangeSharedPtr(size_t idx) const {
314  return std::static_pointer_cast<_StripedBufferArrayRange>(GetRange(idx).lock());
315  }
316  };
317 
319 };
320 
322 
323 #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.
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
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)
PXR_NAMESPACE_OPEN_SCOPE HDST_API TfEnvSetting< int > HD_MAX_VBO_SIZE
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.
HgiBits HgiBufferUsage
Definition: enums.h:322
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:89