interleavedMemoryManager.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 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"
29 #include "pxr/imaging/hdSt/bufferArrayRange.h"
30 #include "pxr/imaging/hd/bufferArray.h"
31 #include "pxr/imaging/hd/bufferSpec.h"
32 #include "pxr/imaging/hd/bufferSource.h"
33 #include "pxr/imaging/hd/resource.h"
34 #include "pxr/imaging/hd/strategyBase.h"
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 
45 PXR_NAMESPACE_OPEN_SCOPE
46 
48 struct HgiBufferCpuToGpuOp;
49 
55 protected:
57 
60  {
61  public:
64  : HdStBufferArrayRange(resourceRegistry)
65  , _stripedBuffer(nullptr)
66  , _index(NOT_ALLOCATED)
67  , _numElements(1)
68  , _capacity(0) {}
69 
71  HDST_API
73 
75  bool IsValid() const override {
76  // note: a range is valid even its index is NOT_ALLOCATED.
77  return (bool)_stripedBuffer;
78  }
79 
81  HDST_API
82  bool IsAssigned() const override;
83 
85  bool IsImmutable() const override;
86 
88  bool RequiresStaging() const override;
89 
92  HDST_API
93  bool Resize(int numElements) override;
94 
96  HDST_API
97  void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
98 
100  HDST_API
101  VtValue ReadData(TfToken const &name) const override;
102 
105  int GetElementOffset() const override {
106  return _index;
107  }
108 
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 
119  size_t GetNumElements() const override {
120  return _numElements;
121  }
122 
124  size_t GetVersion() const override {
125  return _stripedBuffer->GetVersion();
126  }
127 
128  int GetElementStride() const override {
129  return _stripedBuffer->GetElementStride();
130  }
131 
133  void IncrementVersion() override {
134  _stripedBuffer->IncrementVersion();
135  }
136 
138  HDST_API
139  size_t GetMaxNumElements() const override;
140 
142  HDST_API
143  HdBufferArrayUsageHint GetUsageHint() const override;
144 
147  HDST_API
148  HdStBufferResourceSharedPtr GetResource() const override;
149 
151  HDST_API
152  HdStBufferResourceSharedPtr GetResource(TfToken const& name) override;
153 
155  HDST_API
156  HdStBufferResourceNamedList const& GetResources() const override;
157 
159  HDST_API
160  void SetBufferArray(HdBufferArray *bufferArray) override;
161 
163  HDST_API
164  void DebugDump(std::ostream &out) const override;
165 
167  void SetIndex(int index) {
168  _index = index;
169  }
170 
172  void Invalidate() {
173  _stripedBuffer = nullptr;
174  }
175 
177  int GetCapacity() const {
178  return _capacity;
179  }
180 
182  void SetCapacity(int capacity) {
183  _capacity = capacity;
184  }
185 
186  protected:
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 
199  using _StripedInterleavedBufferSharedPtr =
200  std::shared_ptr<_StripedInterleavedBuffer>;
201  using _StripedInterleavedBufferRangeSharedPtr =
202  std::shared_ptr<_StripedInterleavedBufferRange>;
203  using _StripedInterleavedBufferRangePtr =
204  std::weak_ptr<_StripedInterleavedBufferRange>;
205 
208  public:
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 
222  HDST_API
223  virtual ~_StripedInterleavedBuffer();
224 
226  HDST_API
227  virtual bool GarbageCollect();
228 
230  HDST_API
231  virtual void DebugDump(std::ostream &out) const;
232 
235  HDST_API
236  virtual void Reallocate(
237  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
238  HdBufferArraySharedPtr const &curRangeOwner);
239 
242  _needsReallocation = true;
243  }
244 
247  _needsCompaction = true;
248  }
249 
251  size_t GetStride() const {
252  return _stride;
253  }
254 
255  size_t GetElementStride() const {
256  return _elementStride;
257  }
258 
263 
266  HDST_API
267  HdStBufferResourceSharedPtr GetResource() const;
268 
273  HDST_API
274  HdStBufferResourceSharedPtr GetResource(TfToken const& name);
275 
277  HdStBufferResourceNamedList const& GetResources() const {return _resourceList;}
278 
280  HDST_API
281  HdBufferSpecVector GetBufferSpecs() const;
282 
284  GetManager() const {
285  return _manager;
286  }
287 
288  protected:
289  HDST_API
290  void _DeallocateResources();
291 
293  HDST_API
294  HdStBufferResourceSharedPtr _AddResource(TfToken const& name,
295  HdTupleType tupleType,
296  int offset,
297  int stride);
298 
299  private:
301  HdStResourceRegistry* const _resourceRegistry;
302  bool _needsCompaction;
303  size_t _stride;
304  int _bufferOffsetAlignment; // ranged binding offset alignment
305  size_t _maxSize; // maximum size of single buffer
306 
307  // _elementStride is similar to _stride but does account for any buffer
308  // offset alignment. If there are multiple elements in a buffer, this
309  // will be the actual byte distance between the two values.
310  // For example, imagine there are three buffers (A, B, C) in a buffer
311  // array, and each buffer has two elements.
312  // +------------------------------------------------------------+
313  // | a1 | b1 | c1 | a2 | b2 | c2 | padding for offset alignment |
314  // +------------------------------------------------------------+
315  // The _stride will be the size of a1 + b1 + c1 + padding, while the
316  // _elementStride will be the size of a1 + b1 + c1.
317  size_t _elementStride;
318 
319  HdStBufferResourceNamedList _resourceList;
320 
321  _StripedInterleavedBufferRangeSharedPtr _GetRangeSharedPtr(size_t idx) const {
322  return std::static_pointer_cast<_StripedInterleavedBufferRange>(GetRange(idx).lock());
323  }
324 
325  };
326 
328  : _resourceRegistry(resourceRegistry) {}
329 
331  HdBufferArrayRangeSharedPtr CreateBufferArrayRange() override;
332 
334  HdBufferSpecVector GetBufferSpecs(
335  HdBufferArraySharedPtr const &bufferArray) const override;
336 
338  size_t GetResourceAllocation(
339  HdBufferArraySharedPtr const &bufferArray,
340  VtDictionary &result) const override;
341 
342  HdStResourceRegistry* const _resourceRegistry;
343 };
344 
345 class HdStInterleavedUBOMemoryManager : public HdStInterleavedMemoryManager {
346 public:
347  HdStInterleavedUBOMemoryManager(HdStResourceRegistry* resourceRegistry)
348  : HdStInterleavedMemoryManager(resourceRegistry) {}
349 
352  HDST_API
353  virtual HdBufferArraySharedPtr CreateBufferArray(
354  TfToken const &role,
355  HdBufferSpecVector const &bufferSpecs,
356  HdBufferArrayUsageHint usageHint);
357 
359  HDST_API
361  HdBufferSpecVector const &bufferSpecs,
362  HdBufferArrayUsageHint usageHint) const;
363 };
364 
365 class HdStInterleavedSSBOMemoryManager : public HdStInterleavedMemoryManager {
366 public:
367  HdStInterleavedSSBOMemoryManager(HdStResourceRegistry* resourceRegistry)
368  : HdStInterleavedMemoryManager(resourceRegistry) {}
369 
372  HDST_API
373  virtual HdBufferArraySharedPtr CreateBufferArray(
374  TfToken const &role,
375  HdBufferSpecVector const &bufferSpecs,
376  HdBufferArrayUsageHint usageHint);
377 
379  HDST_API
381  HdBufferSpecVector const &bufferSpecs,
382  HdBufferArrayUsageHint usageHint) const;
383 };
384 
385 PXR_NAMESPACE_CLOSE_SCOPE
386 
387 #endif // PXR_IMAGING_HD_ST_INTERLEAVED_MEMORY_MANAGER_H
HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const override
Returns the buffer specs from a given buffer array.
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:70
Describes the properties needed to copy buffer data from CPU to GPU.
Definition: blitCmdsOps.h:190
int GetElementOffset() const override
Returns the offset at which this range begins in the underlying.
HDST_API HdStBufferResourceSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
HDST_API HdStBufferResourceNamedList const & GetResources() const override
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:88
void SetIndex(int index)
Set the relative offset for this range.
Aggregation strategy base class.
Definition: strategyBase.h:48
HD_API void IncrementVersion()
Increments the version of this buffer array.
HDST_API bool Resize(int numElements) override
Resize memory area for this range.
Interface class for representing range (subset) locator of HdBufferArray.
HdTupleType represents zero, one, or more values of the same HdType.
Definition: types.h:358
A map with string keys and VtValue values.
Definition: dictionary.h:63
virtual HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)=0
Factory for creating HdBufferArray.
HDST_API ~_StripedInterleavedBufferRange() override
Destructor.
_StripedInterleavedBufferRange(HdStResourceRegistry *resourceRegistry)
Constructor.
HDST_API bool IsAssigned() const override
Returns true is the range has been assigned to a buffer.
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
#define TF_UNUSED(x)
Stops compiler from producing unused argument or variable warnings.
Definition: tf.h:185
Interleaved memory manager (base class).
bool IsValid() const override
Returns true if this range is valid.
bool RequiresStaging() const override
Returns true if this needs a staging buffer for CPU to GPU copies.
virtual HDST_API void DebugDump(std::ostream &out) const
Debug output.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
HDST_API HdStBufferResourceSharedPtr GetResource() const
TODO: We need to distinguish between the primvar types here, we should tag each HdBufferSource and Hd...
HDST_API VtValue ReadData(TfToken const &name) const override
Read back the buffer content.
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:283
bool IsImmutable() const override
Returns true if this range is marked as immutable.
HDST_API HdStBufferResourceSharedPtr GetResource() const override
Returns the GPU resource.
A central registry of all GPU resources.
void SetNeedsReallocation()
Mark to perform reallocation on Reallocate()
int GetCapacity() const
Returns the capacity of allocated area.
HDST_API const void * _GetAggregation() const override
Returns the aggregation container.
HDST_API size_t GetMaxNumElements() const override
Returns the max number of elements.
void SetNeedsCompaction()
Mark to perform compaction on GarbageCollect()
void SetCapacity(int capacity)
Set the capacity of allocated area for this range.
HdStBufferResourceNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
virtual AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const =0
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;.
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.
bool _needsReallocation
Dirty bit to set when the ranges attached to the buffer changes.
Definition: bufferArray.h:167
HdBufferArrayRangeSharedPtr CreateBufferArrayRange() override
Factory for creating HdBufferArrayRange.
virtual HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner)
Performs reallocation.
HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
size_t GetVersion() const override
Returns the version of the buffer array.
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.
void IncrementVersion() override
Increment the version of the buffer array.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166
virtual HDST_API bool GarbageCollect()
perform compaction if necessary, returns true if it becomes empty.
HDST_API void DebugDump(std::ostream &out) const override
Debug dump.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
size_t GetVersion() const
Returns the version of this buffer array.
Definition: bufferArray.h:104
int GetByteOffset(TfToken const &resourceName) const override
Returns the byte offset at which this range begins in the underlying buffer array for the given resou...
size_t GetNumElements() const override
Returns the number of elements.
virtual HDST_API ~_StripedInterleavedBuffer()
Destructor. It invalidates _rangeList.