All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 public:
60  void StageBufferCopy(HgiBufferCpuToGpuOp const& copyOp);
61 
64  void Flush() override;
65 
66 protected:
68 
69  // BufferFlushListEntry lets use accumulate writes into the same GPU buffer
70  // into CPU staging buffers before flushing to GPU.
71  class _BufferFlushListEntry {
72  public:
73  _BufferFlushListEntry(
74  HgiBufferHandle const& buf, uint64_t start, uint64_t end);
75 
76  HgiBufferHandle buffer;
77  uint64_t start;
78  uint64_t end;
79  };
80 
81  using _BufferFlushMap =
82  std::unordered_map<class HgiBuffer*, _BufferFlushListEntry>;
83 
86  {
87  public:
90  : HdStBufferArrayRange(resourceRegistry)
91  , _stripedBuffer(nullptr)
92  , _index(NOT_ALLOCATED)
93  , _numElements(1) {}
94 
96  HDST_API
98 
100  bool IsValid() const override {
101  // note: a range is valid even its index is NOT_ALLOCATED.
102  return (bool)_stripedBuffer;
103  }
104 
106  HDST_API
107  bool IsAssigned() const override;
108 
110  bool IsImmutable() const override;
111 
114  HDST_API
115  bool Resize(int numElements) override;
116 
118  HDST_API
119  void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
120 
122  HDST_API
123  VtValue ReadData(TfToken const &name) const override;
124 
127  int GetElementOffset() const override {
128  return _index;
129  }
130 
133  int GetByteOffset(TfToken const& resourceName) const override {
134  TF_UNUSED(resourceName);
135  if (!TF_VERIFY(_stripedBuffer) ||
136  !TF_VERIFY(_index != NOT_ALLOCATED)) return 0;
137  return _stripedBuffer->GetStride() * _index;
138  }
139 
141  size_t GetNumElements() const override {
142  return _numElements;
143  }
144 
146  size_t GetVersion() const override {
147  return _stripedBuffer->GetVersion();
148  }
149 
151  void IncrementVersion() override {
152  _stripedBuffer->IncrementVersion();
153  }
154 
156  HDST_API
157  size_t GetMaxNumElements() const override;
158 
160  HDST_API
161  HdBufferArrayUsageHint GetUsageHint() const override;
162 
165  HDST_API
166  HdStBufferResourceSharedPtr GetResource() const override;
167 
169  HDST_API
170  HdStBufferResourceSharedPtr GetResource(TfToken const& name) override;
171 
173  HDST_API
174  HdStBufferResourceNamedList const& GetResources() const override;
175 
177  HDST_API
178  void SetBufferArray(HdBufferArray *bufferArray) override;
179 
181  HDST_API
182  void DebugDump(std::ostream &out) const override;
183 
185  void SetIndex(int index) {
186  _index = index;
187  }
188 
190  void Invalidate() {
191  _stripedBuffer = nullptr;
192  }
193 
194  protected:
196  HDST_API
197  const void *_GetAggregation() const override;
198 
199  private:
200  enum { NOT_ALLOCATED = -1 };
201  _StripedInterleavedBuffer *_stripedBuffer;
202  int _index;
203  size_t _numElements;
204  };
205 
206  using _StripedInterleavedBufferSharedPtr =
207  std::shared_ptr<_StripedInterleavedBuffer>;
208  using _StripedInterleavedBufferRangeSharedPtr =
209  std::shared_ptr<_StripedInterleavedBufferRange>;
210  using _StripedInterleavedBufferRangePtr =
211  std::weak_ptr<_StripedInterleavedBufferRange>;
212 
215  public:
217  HDST_API
219  HdStResourceRegistry* resourceRegistry,
220  TfToken const &role,
221  HdBufferSpecVector const &bufferSpecs,
222  HdBufferArrayUsageHint usageHint,
223  int bufferOffsetAlignment,
224  int structAlignment,
225  size_t maxSize,
226  TfToken const &garbageCollectionPerfToken);
227 
229  HDST_API
230  virtual ~_StripedInterleavedBuffer();
231 
233  HDST_API
234  virtual bool GarbageCollect();
235 
237  HDST_API
238  virtual void DebugDump(std::ostream &out) const;
239 
242  HDST_API
243  virtual void Reallocate(
244  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
245  HdBufferArraySharedPtr const &curRangeOwner);
246 
249  _needsReallocation = true;
250  }
251 
254  _needsCompaction = true;
255  }
256 
258  int GetStride() const {
259  return _stride;
260  }
261 
266 
269  HDST_API
270  HdStBufferResourceSharedPtr GetResource() const;
271 
276  HDST_API
277  HdStBufferResourceSharedPtr GetResource(TfToken const& name);
278 
280  HdStBufferResourceNamedList const& GetResources() const {return _resourceList;}
281 
283  HDST_API
284  HdBufferSpecVector GetBufferSpecs() const;
285 
287  GetManager() const {
288  return _manager;
289  }
290 
291  protected:
292  HDST_API
293  void _DeallocateResources();
294 
296  HDST_API
297  HdStBufferResourceSharedPtr _AddResource(TfToken const& name,
298  HdTupleType tupleType,
299  int offset,
300  int stride);
301 
302  private:
304  HdStResourceRegistry* const _resourceRegistry;
305  bool _needsCompaction;
306  int _stride;
307  int _bufferOffsetAlignment; // ranged binding offset alignment
308  size_t _maxSize; // maximum size of single buffer
309 
310  HdStBufferResourceNamedList _resourceList;
311 
312  _StripedInterleavedBufferRangeSharedPtr _GetRangeSharedPtr(size_t idx) const {
313  return std::static_pointer_cast<_StripedInterleavedBufferRange>(GetRange(idx).lock());
314  }
315 
316  };
317 
319  : _resourceRegistry(resourceRegistry) {}
320 
322  HdBufferArrayRangeSharedPtr CreateBufferArrayRange() override;
323 
325  HdBufferSpecVector GetBufferSpecs(
326  HdBufferArraySharedPtr const &bufferArray) const override;
327 
329  size_t GetResourceAllocation(
330  HdBufferArraySharedPtr const &bufferArray,
331  VtDictionary &result) const override;
332 
333  HdStResourceRegistry* const _resourceRegistry;
334  _BufferFlushMap _queuedBuffers;
335 };
336 
337 class HdStInterleavedUBOMemoryManager : public HdStInterleavedMemoryManager {
338 public:
339  HdStInterleavedUBOMemoryManager(HdStResourceRegistry* resourceRegistry)
340  : HdStInterleavedMemoryManager(resourceRegistry) {}
341 
344  HDST_API
345  virtual HdBufferArraySharedPtr CreateBufferArray(
346  TfToken const &role,
347  HdBufferSpecVector const &bufferSpecs,
348  HdBufferArrayUsageHint usageHint);
349 
351  HDST_API
353  HdBufferSpecVector const &bufferSpecs,
354  HdBufferArrayUsageHint usageHint) const;
355 };
356 
357 class HdStInterleavedSSBOMemoryManager : public HdStInterleavedMemoryManager {
358 public:
359  HdStInterleavedSSBOMemoryManager(HdStResourceRegistry* resourceRegistry)
360  : HdStInterleavedMemoryManager(resourceRegistry) {}
361 
364  HDST_API
365  virtual HdBufferArraySharedPtr CreateBufferArray(
366  TfToken const &role,
367  HdBufferSpecVector const &bufferSpecs,
368  HdBufferArrayUsageHint usageHint);
369 
371  HDST_API
373  HdBufferSpecVector const &bufferSpecs,
374  HdBufferArrayUsageHint usageHint) const;
375 };
376 
377 PXR_NAMESPACE_CLOSE_SCOPE
378 
379 #endif // PXR_IMAGING_HD_ST_INTERLEAVED_MEMORY_MANAGER_H
void Flush() override
Flush the staging buffer to GPU.
HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const override
Returns the buffer specs from a given buffer array.
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
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
virtual HDST_API void DebugDump(std::ostream &out) const
Debug output.
int GetElementOffset() const override
Returns the offset at which this range begins in the underlying buffer array in terms of elements...
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:325
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.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
_StripedInterleavedBufferRange(HdStResourceRegistry *resourceRegistry)
Constructor.
HDST_API bool IsAssigned() const override
Returns true is the range has been assigned to a buffer.
#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.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
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()
HDST_API const void * _GetAggregation() const override
Returns the aggregation container.
HDST_API HdStBufferResourceSharedPtr GetResource() const
TODO: We need to distinguish between the primvar types here, we should tag each HdBufferSource and Hd...
HDST_API size_t GetMaxNumElements() const override
Returns the max number of elements.
HdStBufferResourceNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
void SetNeedsCompaction()
Mark to perform compaction on GarbageCollect()
size_t GetVersion() const
Returns the version of this buffer array.
Definition: bufferArray.h:104
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:168
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...
void StageBufferCopy(HgiBufferCpuToGpuOp const &copyOp)
Copy new data from CPU into staging buffer.
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.