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