Loading...
Searching...
No Matches
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/hdSt/strategyBase.h"
31
32#include "pxr/imaging/hd/bufferArray.h"
33#include "pxr/imaging/hd/bufferSpec.h"
34#include "pxr/imaging/hd/bufferSource.h"
35#include "pxr/imaging/hd/tokens.h"
36#include "pxr/imaging/hd/version.h"
37#include "pxr/imaging/hgi/buffer.h"
39#include "pxr/base/tf/token.h"
40
41#include <memory>
42#include <list>
43#include <unordered_map>
44
45PXR_NAMESPACE_OPEN_SCOPE
46
49
55protected:
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
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
261 HDST_API
262 HdStBufferResourceSharedPtr GetResource() const;
263
268 HDST_API
269 HdStBufferResourceSharedPtr GetResource(TfToken const& name);
270
272 HdStBufferResourceNamedList const& GetResources() const {return _resourceList;}
273
275 HDST_API
276 HdBufferSpecVector GetBufferSpecs() const;
277
279 GetManager() const {
280 return _manager;
281 }
282
283 protected:
284 HDST_API
285 void _DeallocateResources();
286
288 HDST_API
289 HdStBufferResourceSharedPtr _AddResource(TfToken const& name,
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 HgiBufferUsage _bufferUsage;
315
316 HdStBufferResourceNamedList _resourceList;
317
318 _StripedInterleavedBufferRangeSharedPtr _GetRangeSharedPtr(size_t idx) const {
319 return std::static_pointer_cast<_StripedInterleavedBufferRange>(GetRange(idx).lock());
320 }
321
322 };
323
325 : _resourceRegistry(resourceRegistry) {}
326
328 HdBufferArrayRangeSharedPtr CreateBufferArrayRange() override;
329
331 HdBufferSpecVector GetBufferSpecs(
332 HdBufferArraySharedPtr const &bufferArray) const override;
333
336 HdBufferArraySharedPtr const &bufferArray,
337 VtDictionary &result) const override;
338
339 HdStResourceRegistry* const _resourceRegistry;
340};
341
342class HdStInterleavedUBOMemoryManager : public HdStInterleavedMemoryManager {
343public:
344 HdStInterleavedUBOMemoryManager(HdStResourceRegistry* resourceRegistry)
345 : HdStInterleavedMemoryManager(resourceRegistry) {}
346
349 HDST_API
350 virtual HdBufferArraySharedPtr CreateBufferArray(
351 TfToken const &role,
352 HdBufferSpecVector const &bufferSpecs,
353 HdBufferArrayUsageHint usageHint);
354
356 HDST_API
358 HdBufferSpecVector const &bufferSpecs,
359 HdBufferArrayUsageHint usageHint) const;
360};
361
362class HdStInterleavedSSBOMemoryManager : public HdStInterleavedMemoryManager {
363public:
364 HdStInterleavedSSBOMemoryManager(HdStResourceRegistry* resourceRegistry)
365 : HdStInterleavedMemoryManager(resourceRegistry) {}
366
369 HDST_API
370 virtual HdBufferArraySharedPtr CreateBufferArray(
371 TfToken const &role,
372 HdBufferSpecVector const &bufferSpecs,
373 HdBufferArrayUsageHint usageHint);
374
376 HDST_API
378 HdBufferSpecVector const &bufferSpecs,
379 HdBufferArrayUsageHint usageHint) const;
380};
381
382PXR_NAMESPACE_CLOSE_SCOPE
383
384#endif // PXR_IMAGING_HD_ST_INTERLEAVED_MEMORY_MANAGER_H
Similar to a VAO, this object is a bundle of coherent buffers.
Definition: bufferArray.h:86
bool _needsReallocation
Dirty bit to set when the ranges attached to the buffer changes.
Definition: bufferArray.h:164
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:101
Aggregation strategy base class.
Definition: strategyBase.h:48
size_t AggregationId
Aggregation ID.
Definition: strategyBase.h:51
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:88
A map with string keys and VtValue values.
Definition: dictionary.h:60
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:164
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:283
#define TF_UNUSED(x)
Stops compiler from producing unused argument or variable warnings.
Definition: tf.h:185
HdTupleType represents zero, one, or more values of the same HdType.
Definition: types.h:358
Describes the properties needed to copy buffer data from CPU to GPU.
Definition: blitCmdsOps.h:191
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...