Loading...
Searching...
No Matches
interleavedMemoryManager.h
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"
12#include "pxr/imaging/hdSt/bufferArrayRange.h"
13#include "pxr/imaging/hdSt/strategyBase.h"
14
15#include "pxr/imaging/hd/bufferArray.h"
16#include "pxr/imaging/hd/bufferSpec.h"
17#include "pxr/imaging/hd/bufferSource.h"
18#include "pxr/imaging/hd/tokens.h"
19#include "pxr/imaging/hd/version.h"
20#include "pxr/imaging/hgi/buffer.h"
22#include "pxr/base/tf/token.h"
23
24#include <memory>
25#include <list>
26#include <unordered_map>
27
28PXR_NAMESPACE_OPEN_SCOPE
29
32
38protected:
40
43 {
44 public:
47 : HdStBufferArrayRange(resourceRegistry)
48 , _stripedBuffer(nullptr)
49 , _index(NOT_ALLOCATED)
50 , _numElements(1)
51 , _capacity(0) {}
52
54 HDST_API
56
58 bool IsValid() const override {
59 // note: a range is valid even its index is NOT_ALLOCATED.
60 return (bool)_stripedBuffer;
61 }
62
64 HDST_API
65 bool IsAssigned() const override;
66
68 bool IsImmutable() const override;
69
71 bool RequiresStaging() const override;
72
75 HDST_API
76 bool Resize(int numElements) override;
77
79 HDST_API
80 void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
81
83 HDST_API
84 VtValue ReadData(TfToken const &name) const override;
85
88 int GetElementOffset() const override {
89 return _index;
90 }
91
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
102 size_t GetNumElements() const override {
103 return _numElements;
104 }
105
107 size_t GetVersion() const override {
108 return _stripedBuffer->GetVersion();
109 }
110
111 int GetElementStride() const override {
112 return _stripedBuffer->GetElementStride();
113 }
114
116 void IncrementVersion() override {
117 _stripedBuffer->IncrementVersion();
118 }
119
121 HDST_API
122 size_t GetMaxNumElements() const override;
123
125 HDST_API
126 HdBufferArrayUsageHint GetUsageHint() const override;
127
130 HDST_API
131 HdStBufferResourceSharedPtr GetResource() const override;
132
134 HDST_API
135 HdStBufferResourceSharedPtr GetResource(TfToken const& name) override;
136
138 HDST_API
139 HdStBufferResourceNamedList const& GetResources() const override;
140
142 HDST_API
143 void SetBufferArray(HdBufferArray *bufferArray) override;
144
146 HDST_API
147 void DebugDump(std::ostream &out) const override;
148
150 void SetIndex(int index) {
151 _index = index;
152 }
153
155 void Invalidate() {
156 _stripedBuffer = nullptr;
157 }
158
160 int GetCapacity() const {
161 return _capacity;
162 }
163
165 void SetCapacity(int capacity) {
166 _capacity = capacity;
167 }
168
169 protected:
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
182 using _StripedInterleavedBufferSharedPtr =
183 std::shared_ptr<_StripedInterleavedBuffer>;
184 using _StripedInterleavedBufferRangeSharedPtr =
185 std::shared_ptr<_StripedInterleavedBufferRange>;
186 using _StripedInterleavedBufferRangePtr =
187 std::weak_ptr<_StripedInterleavedBufferRange>;
188
191 public:
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
205 HDST_API
207
209 HDST_API
210 virtual bool GarbageCollect();
211
213 HDST_API
214 virtual void DebugDump(std::ostream &out) const;
215
218 HDST_API
219 virtual void Reallocate(
220 std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
221 HdBufferArraySharedPtr const &curRangeOwner);
222
225 _needsReallocation = true;
226 }
227
230 _needsCompaction = true;
231 }
232
234 size_t GetStride() const {
235 return _stride;
236 }
237
238 size_t GetElementStride() const {
239 return _elementStride;
240 }
241
244 HDST_API
245 HdStBufferResourceSharedPtr GetResource() const;
246
251 HDST_API
252 HdStBufferResourceSharedPtr GetResource(TfToken const& name);
253
255 HdStBufferResourceNamedList const& GetResources() const {return _resourceList;}
256
258 HDST_API
259 HdBufferSpecVector GetBufferSpecs() const;
260
262 GetManager() const {
263 return _manager;
264 }
265
266 protected:
267 HDST_API
268 void _DeallocateResources();
269
271 HDST_API
272 HdStBufferResourceSharedPtr _AddResource(TfToken const& name,
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
311 HdBufferArrayRangeSharedPtr CreateBufferArrayRange() override;
312
314 HdBufferSpecVector GetBufferSpecs(
315 HdBufferArraySharedPtr const &bufferArray) const override;
316
319 HdBufferArraySharedPtr const &bufferArray,
320 VtDictionary &result) const override;
321
322 HdStResourceRegistry* const _resourceRegistry;
323};
324
325class HdStInterleavedUBOMemoryManager : public HdStInterleavedMemoryManager {
326public:
327 HdStInterleavedUBOMemoryManager(HdStResourceRegistry* resourceRegistry)
328 : HdStInterleavedMemoryManager(resourceRegistry) {}
329
332 HDST_API
333 virtual HdBufferArraySharedPtr CreateBufferArray(
334 TfToken const &role,
335 HdBufferSpecVector const &bufferSpecs,
336 HdBufferArrayUsageHint usageHint);
337
339 HDST_API
341 HdBufferSpecVector const &bufferSpecs,
342 HdBufferArrayUsageHint usageHint) const;
343};
344
345class HdStInterleavedSSBOMemoryManager : public HdStInterleavedMemoryManager {
346public:
347 HdStInterleavedSSBOMemoryManager(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
365PXR_NAMESPACE_CLOSE_SCOPE
366
367#endif // PXR_IMAGING_HD_ST_INTERLEAVED_MEMORY_MANAGER_H
Similar to a VAO, this object is a bundle of coherent buffers.
Definition: bufferArray.h:69
bool _needsReallocation
Dirty bit to set when the ranges attached to the buffer changes.
Definition: bufferArray.h:147
HD_API void IncrementVersion()
Increments the version of this buffer array.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
size_t GetVersion() const
Returns the version of this buffer array.
Definition: bufferArray.h:84
Aggregation strategy base class.
Definition: strategyBase.h:31
size_t AggregationId
Aggregation ID.
Definition: strategyBase.h:34
virtual HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)=0
Factory for creating HdBufferArray.
virtual AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const =0
Returns id for given bufferSpecs to be used for aggregation.
Interface class for representing range (subset) locator of HdBufferArray.
void SetNeedsCompaction()
Mark to perform compaction on GarbageCollect()
HDST_API HdStBufferResourceSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
HDST_API HdStBufferResourceSharedPtr GetResource(TfToken const &name)
Returns the named GPU resource.
void SetNeedsReallocation()
Mark to perform reallocation on Reallocate()
virtual HDST_API bool GarbageCollect()
perform compaction if necessary, returns true if it becomes empty.
HDST_API HdStBufferResourceSharedPtr GetResource() const
Returns the GPU resource.
virtual HDST_API ~_StripedInterleavedBuffer()
Destructor. It invalidates _rangeList.
virtual HDST_API void DebugDump(std::ostream &out) const
Debug output.
HdStBufferResourceNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
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.
virtual HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner)
Performs reallocation.
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
HDST_API void CopyData(HdBufferSourceSharedPtr const &bufferSource) override
Copy source data into buffer.
size_t GetNumElements() const override
Returns the number of elements.
HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
HDST_API void SetBufferArray(HdBufferArray *bufferArray) override
Sets the buffer array associated with this buffer;.
int GetCapacity() const
Returns the capacity of allocated area.
void IncrementVersion() override
Increment the version of the buffer array.
bool IsValid() const override
Returns true if this range is valid.
HDST_API bool IsAssigned() const override
Returns true is the range has been assigned to a buffer.
HDST_API ~_StripedInterleavedBufferRange() override
Destructor.
int GetElementOffset() const override
Returns the offset at which this range begins in the underlying buffer array in terms of elements.
HDST_API void DebugDump(std::ostream &out) const override
Debug dump.
bool IsImmutable() const override
Returns true if this range is marked as immutable.
HDST_API size_t GetMaxNumElements() const override
Returns the max number of elements.
HDST_API HdStBufferResourceNamedList const & GetResources() const override
Returns the list of all named GPU resources for this bufferArrayRange.
size_t GetVersion() const override
Returns the version of the buffer array.
bool RequiresStaging() const override
Returns true if this needs a staging buffer for CPU to GPU copies.
HDST_API HdStBufferResourceSharedPtr GetResource(TfToken const &name) override
Returns the named GPU resource.
HDST_API HdStBufferResourceSharedPtr GetResource() const override
Returns the GPU resource.
void SetIndex(int index)
Set the relative offset for this range.
HDST_API VtValue ReadData(TfToken const &name) const override
Read back the buffer content.
void SetCapacity(int capacity)
Set the capacity of allocated area for this range.
HDST_API const void * _GetAggregation() const override
Returns the aggregation container.
_StripedInterleavedBufferRange(HdStResourceRegistry *resourceRegistry)
Constructor.
HDST_API bool Resize(int numElements) override
Resize memory area for this range.
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...
Interleaved memory manager (base class).
size_t GetResourceAllocation(HdBufferArraySharedPtr const &bufferArray, VtDictionary &result) const override
Returns the size of the GPU memory used by the passed buffer array.
HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const override
Returns the buffer specs from a given buffer array.
HdBufferArrayRangeSharedPtr CreateBufferArrayRange() override
Factory for creating HdBufferArrayRange.
A central registry of all GPU resources.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
A map with string keys and VtValue values.
Definition: dictionary.h:52
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:90
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:266
#define TF_UNUSED(x)
Stops compiler from producing unused argument or variable warnings.
Definition: tf.h:168
HdTupleType represents zero, one, or more values of the same HdType.
Definition: types.h:343
Describes the properties needed to copy buffer data from CPU to GPU.
Definition: blitCmdsOps.h:174
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...