HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
interleavedMemoryManager.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_INTERLEAVED_MEMORY_MANAGER_H
8 #define PXR_IMAGING_HD_ST_INTERLEAVED_MEMORY_MANAGER_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hdSt/api.h"
14 
18 #include "pxr/imaging/hd/tokens.h"
19 #include "pxr/imaging/hd/version.h"
20 #include "pxr/imaging/hgi/buffer.h"
21 #include "pxr/base/tf/mallocTag.h"
22 #include "pxr/base/tf/token.h"
23 
24 #include <memory>
25 #include <list>
26 #include <unordered_map>
27 
29 
31 struct HgiBufferCpuToGpuOp;
32 
33 /// \class HdStInterleavedMemoryManager
34 ///
35 /// Interleaved memory manager (base class).
36 ///
38 protected:
40 
41  /// specialized buffer array range
43  {
44  public:
45  /// Constructor.
47  : HdStBufferArrayRange(resourceRegistry)
48  , _stripedBuffer(nullptr)
49  , _index(NOT_ALLOCATED)
50  , _numElements(1)
51  , _capacity(0) {}
52 
53  /// Destructor.
54  HDST_API
56 
57  /// Returns true if this range is valid
58  bool IsValid() const override {
59  // note: a range is valid even its index is NOT_ALLOCATED.
60  return (bool)_stripedBuffer;
61  }
62 
63  /// Returns true is the range has been assigned to a buffer
64  HDST_API
65  bool IsAssigned() const override;
66 
67  /// Returns true if this range is marked as immutable.
68  bool IsImmutable() const override;
69 
70  /// Returns true if this needs a staging buffer for CPU to GPU copies.
71  bool RequiresStaging() const override;
72 
73  /// Resize memory area for this range. Returns true if it causes container
74  /// buffer reallocation.
75  HDST_API
76  bool Resize(int numElements) override;
77 
78  /// Copy source data into buffer
79  HDST_API
80  void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
81 
82  /// Read back the buffer content
83  HDST_API
84  VtValue ReadData(TfToken const &name) const override;
85 
86  /// Returns the offset at which this range begins in the underlying
87  /// buffer array in terms of elements.
88  int GetElementOffset() const override {
89  return _index;
90  }
91 
92  /// Returns the byte offset at which this range begins in the underlying
93  /// buffer array for the given resource.
94  int GetByteOffset(TfToken const& resourceName) const override {
95  TF_UNUSED(resourceName);
96  if (!TF_VERIFY(_stripedBuffer) ||
97  !TF_VERIFY(_index != NOT_ALLOCATED)) return 0;
98  return _stripedBuffer->GetStride() * _index;
99  }
100 
101  /// Returns the number of elements
102  size_t GetNumElements() const override {
103  return _numElements;
104  }
105 
106  /// Returns the version of the buffer array.
107  size_t GetVersion() const override {
108  return _stripedBuffer->GetVersion();
109  }
110 
111  int GetElementStride() const override {
112  return _stripedBuffer->GetElementStride();
113  }
114 
115  /// Increment the version of the buffer array.
116  void IncrementVersion() override {
117  _stripedBuffer->IncrementVersion();
118  }
119 
120  /// Returns the max number of elements
121  HDST_API
122  size_t GetMaxNumElements() const override;
123 
124  /// Returns the usage hint from the underlying buffer array
125  HDST_API
126  HdBufferArrayUsageHint GetUsageHint() const override;
127 
128  /// Returns the GPU resource. If the buffer array contains more than one
129  /// resource, this method raises a coding error.
130  HDST_API
131  HdStBufferResourceSharedPtr GetResource() const override;
132 
133  /// Returns the named GPU resource.
134  HDST_API
136 
137  /// Returns the list of all named GPU resources for this bufferArrayRange.
138  HDST_API
139  HdStBufferResourceNamedList const& GetResources() const override;
140 
141  /// Sets the buffer array associated with this buffer;
142  HDST_API
143  void SetBufferArray(HdBufferArray *bufferArray) override;
144 
145  /// Debug dump
146  HDST_API
147  void DebugDump(std::ostream &out) const override;
148 
149  /// Set the relative offset for this range.
150  void SetIndex(int index) {
151  _index = index;
152  }
153 
154  /// Make this range invalid
155  void Invalidate() {
156  _stripedBuffer = nullptr;
157  }
158 
159  /// Returns the capacity of allocated area
160  int GetCapacity() const {
161  return _capacity;
162  }
163 
164  /// Set the capacity of allocated area for this range.
165  void SetCapacity(int capacity) {
166  _capacity = capacity;
167  }
168 
169  protected:
170  /// Returns the aggregation container
171  HDST_API
172  const void *_GetAggregation() const override;
173 
174  private:
175  enum { NOT_ALLOCATED = -1 };
176  _StripedInterleavedBuffer *_stripedBuffer;
177  int _index;
178  size_t _numElements;
179  int _capacity;
180  };
181 
183  std::shared_ptr<_StripedInterleavedBuffer>;
185  std::shared_ptr<_StripedInterleavedBufferRange>;
187  std::weak_ptr<_StripedInterleavedBufferRange>;
188 
189  /// striped buffer
191  public:
192  /// Constructor.
193  HDST_API
195  HdStResourceRegistry* resourceRegistry,
196  TfToken const &role,
197  HdBufferSpecVector const &bufferSpecs,
198  HdBufferArrayUsageHint usageHint,
199  int bufferOffsetAlignment,
200  int structAlignment,
201  size_t maxSize,
202  TfToken const &garbageCollectionPerfToken);
203 
204  /// Destructor. It invalidates _rangeList
205  HDST_API
206  virtual ~_StripedInterleavedBuffer();
207 
208  /// perform compaction if necessary, returns true if it becomes empty.
209  HDST_API
210  virtual bool GarbageCollect();
211 
212  /// Debug output
213  HDST_API
214  virtual void DebugDump(std::ostream &out) const;
215 
216  /// Performs reallocation.
217  /// GLX context has to be set when calling this function.
218  HDST_API
219  virtual void Reallocate(
220  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
221  HdBufferArraySharedPtr const &curRangeOwner);
222 
223  /// Mark to perform reallocation on Reallocate()
225  _needsReallocation = true;
226  }
227 
228  /// Mark to perform compaction on GarbageCollect()
230  _needsCompaction = true;
231  }
232 
233  /// Returns the stride.
234  size_t GetStride() const {
235  return _stride;
236  }
237 
238  size_t GetElementStride() const {
239  return _elementStride;
240  }
241 
242  /// Returns the GPU resource. If the buffer array contains more
243  /// than one resource, this method raises a coding error.
244  HDST_API
246 
247  /// Returns the named GPU resource. This method returns the first found
248  /// resource. In HD_SAFE_MODE it checks all underlying GL buffers
249  /// in _resourceMap and raises a coding error if there are more than
250  /// one GL buffers exist.
251  HDST_API
253 
254  /// Returns the list of all named GPU resources for this bufferArray.
255  HdStBufferResourceNamedList const& GetResources() const {return _resourceList;}
256 
257  /// Reconstructs the bufferspecs and returns it (for buffer splitting)
258  HDST_API
260 
262  GetManager() const {
263  return _manager;
264  }
265 
266  protected:
267  HDST_API
268  void _DeallocateResources();
269 
270  /// Adds a new, named GPU resource and returns it.
271  HDST_API
273  HdTupleType tupleType,
274  int offset,
275  int stride);
276 
277  private:
279  HdStResourceRegistry* const _resourceRegistry;
280  bool _needsCompaction;
281  size_t _stride;
282  int _bufferOffsetAlignment; // ranged binding offset alignment
283  size_t _maxSize; // maximum size of single buffer
284 
285  // _elementStride is similar to _stride but does account for any buffer
286  // offset alignment. If there are multiple elements in a buffer, this
287  // will be the actual byte distance between the two values.
288  // For example, imagine there are three buffers (A, B, C) in a buffer
289  // array, and each buffer has two elements.
290  // +------------------------------------------------------------+
291  // | a1 | b1 | c1 | a2 | b2 | c2 | padding for offset alignment |
292  // +------------------------------------------------------------+
293  // The _stride will be the size of a1 + b1 + c1 + padding, while the
294  // _elementStride will be the size of a1 + b1 + c1.
295  size_t _elementStride;
296 
297  HgiBufferUsage _bufferUsage;
298 
299  HdStBufferResourceNamedList _resourceList;
300 
301  _StripedInterleavedBufferRangeSharedPtr _GetRangeSharedPtr(size_t idx) const {
302  return std::static_pointer_cast<_StripedInterleavedBufferRange>(GetRange(idx).lock());
303  }
304 
305  };
306 
308  : _resourceRegistry(resourceRegistry) {}
309 
310  /// Factory for creating HdBufferArrayRange
312 
313  /// Returns the buffer specs from a given buffer array
315  HdBufferArraySharedPtr const &bufferArray) const override;
316 
317  /// Returns the size of the GPU memory used by the passed buffer array
318  size_t GetResourceAllocation(
319  HdBufferArraySharedPtr const &bufferArray,
320  VtDictionary &result) const override;
321 
323 };
324 
326 public:
328  : HdStInterleavedMemoryManager(resourceRegistry) {}
329 
330  /// Factory for creating HdBufferArray managed by
331  /// HdStVBOMemoryManager aggregation.
332  HDST_API
334  TfToken const &role,
335  HdBufferSpecVector const &bufferSpecs,
336  HdBufferArrayUsageHint usageHint);
337 
338  /// Returns id for given bufferSpecs to be used for aggregation
339  HDST_API
341  HdBufferSpecVector const &bufferSpecs,
342  HdBufferArrayUsageHint usageHint) const;
343 };
344 
346 public:
348  : HdStInterleavedMemoryManager(resourceRegistry) {}
349 
350  /// Factory for creating HdBufferArray managed by
351  /// HdStVBOMemoryManager aggregation.
352  HDST_API
354  TfToken const &role,
355  HdBufferSpecVector const &bufferSpecs,
356  HdBufferArrayUsageHint usageHint);
357 
358  /// Returns id for given bufferSpecs to be used for aggregation
359  HDST_API
361  HdBufferSpecVector const &bufferSpecs,
362  HdBufferArrayUsageHint usageHint) const;
363 };
364 
366 
367 #endif // PXR_IMAGING_HD_ST_INTERLEAVED_MEMORY_MANAGER_H
void SetCapacity(int capacity)
Set the capacity of allocated area for this range.
HDST_API bool Resize(int numElements) override
HDST_API HdStBufferResourceNamedList const & GetResources() const override
Returns the list of all named GPU resources for this bufferArrayRange.
void SetNeedsCompaction()
Mark to perform compaction on GarbageCollect()
std::weak_ptr< _StripedInterleavedBufferRange > _StripedInterleavedBufferRangePtr
size_t GetVersion() const override
Returns the version of the buffer array.
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
bool _needsReallocation
Definition: bufferArray.h:147
HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
HDST_API ~_StripedInterleavedBufferRange() override
Destructor.
std::shared_ptr< class HdBufferArray > HdBufferArraySharedPtr
Definition: bufferArray.h:26
HDST_API size_t GetMaxNumElements() const override
Returns the max number of elements.
HdStInterleavedMemoryManager(HdStResourceRegistry *resourceRegistry)
std::vector< std::pair< TfToken, HdStBufferResourceSharedPtr > > HdStBufferResourceNamedList
bool IsImmutable() const override
Returns true if this range is marked as immutable.
HdStInterleavedSSBOMemoryManager(HdStResourceRegistry *resourceRegistry)
virtual HDST_API ~_StripedInterleavedBuffer()
Destructor. It invalidates _rangeList.
virtual HDST_API bool GarbageCollect()
perform compaction if necessary, returns true if it becomes empty.
HdStBufferResourceNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
**But if you need a result
Definition: thread.h:622
HdStInterleavedUBOMemoryManager(HdStResourceRegistry *resourceRegistry)
virtual HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner)
HDST_API void SetBufferArray(HdBufferArray *bufferArray) override
Sets the buffer array associated with this buffer;.
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
HDST_API void DebugDump(std::ostream &out) const override
Debug dump.
Definition: token.h:70
GLintptr offset
Definition: glcorearb.h:665
virtual HDST_API HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
bool IsValid() const override
Returns true if this range is valid.
HD_API void IncrementVersion()
Increments the version of this buffer array.
std::shared_ptr< _StripedInterleavedBuffer > _StripedInterleavedBufferSharedPtr
virtual HDST_API void DebugDump(std::ostream &out) const
Debug output.
_StripedInterleavedBufferRange(HdStResourceRegistry *resourceRegistry)
Constructor.
std::shared_ptr< _StripedInterleavedBufferRange > _StripedInterleavedBufferRangeSharedPtr
bool RequiresStaging() const override
Returns true if this needs a staging buffer for CPU to GPU copies.
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:872
HDST_API VtValue ReadData(TfToken const &name) const override
Read back the buffer content.
HDST_API bool IsAssigned() const override
Returns true is the range has been assigned to a buffer.
HdBufferArrayRangeSharedPtr CreateBufferArrayRange() override
Factory for creating HdBufferArrayRange.
GLuint const GLchar * name
Definition: glcorearb.h:786
std::vector< struct HdBufferSpec > HdBufferSpecVector
size_t GetResourceAllocation(HdBufferArraySharedPtr const &bufferArray, VtDictionary &result) const override
Returns the size of the GPU memory used by the passed buffer array.
HDST_API void CopyData(HdBufferSourceSharedPtr const &bufferSource) override
Copy source data into buffer.
std::shared_ptr< class HdStBufferResource > HdStBufferResourceSharedPtr
HDST_API HdStBufferResourceSharedPtr GetResource() const override
size_t GetVersion() const
Definition: bufferArray.h:84
HDST_API const void * _GetAggregation() const override
Returns the aggregation container.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HgiBits HgiBufferUsage
Definition: enums.h:315
virtual HDST_API HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
#define HDST_API
Definition: api.h:23
HDST_API HdStBufferResourceSharedPtr GetResource() const
GLuint index
Definition: glcorearb.h:786
void SetIndex(int index)
Set the relative offset for this range.
#define TF_UNUSED(x)
Definition: tf.h:168
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
int GetCapacity() const
Returns the capacity of allocated area.
void SetNeedsReallocation()
Mark to perform reallocation on Reallocate()
size_t GetNumElements() const override
Returns the number of elements.
virtual HDST_API AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
std::shared_ptr< class HdBufferSource > HdBufferSourceSharedPtr
void IncrementVersion() override
Increment the version of the buffer array.
int GetByteOffset(TfToken const &resourceName) const override
HdStResourceRegistry *const _resourceRegistry
Definition: value.h:146
HDST_API HdStBufferResourceSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
virtual HDST_API AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
HDST_API _StripedInterleavedBuffer(HdStInterleavedMemoryManager *mgr, HdStResourceRegistry *resourceRegistry, TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint, int bufferOffsetAlignment, int structAlignment, size_t maxSize, TfToken const &garbageCollectionPerfToken)
Constructor.
HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const override
Returns the buffer specs from a given buffer array.