Loading...
Searching...
No Matches
vboMemoryManager.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_VBO_MEMORY_MANAGER_H
8#define PXR_IMAGING_HD_ST_VBO_MEMORY_MANAGER_H
9
10#include "pxr/pxr.h"
11#include "pxr/imaging/hdSt/api.h"
12#include "pxr/imaging/hdSt/strategyBase.h"
13
14#include "pxr/imaging/hgi/enums.h"
15
16#include "pxr/imaging/hd/bufferArray.h"
17#include "pxr/imaging/hdSt/bufferArrayRange.h"
18#include "pxr/imaging/hd/bufferSpec.h"
19#include "pxr/imaging/hd/bufferSource.h"
20
22#include "pxr/base/tf/token.h"
23
24#include <list>
25#include <memory>
26
27PXR_NAMESPACE_OPEN_SCOPE
28
30
36{
37public:
40 , _resourceRegistry(resourceRegistry) {}
41
44 HDST_API
45 virtual HdBufferArraySharedPtr CreateBufferArray(
46 TfToken const &role,
47 HdBufferSpecVector const &bufferSpecs,
48 HdBufferArrayUsageHint usageHint);
49
52 HDST_API
53 virtual HdBufferArrayRangeSharedPtr CreateBufferArrayRange();
54
56 HDST_API
58 HdBufferSpecVector const &bufferSpecs,
59 HdBufferArrayUsageHint usageHint) const;
60
62 virtual HdBufferSpecVector GetBufferSpecs(
63 HdBufferArraySharedPtr const &bufferArray) const;
64
66 virtual size_t GetResourceAllocation(
67 HdBufferArraySharedPtr const &bufferArray,
68 VtDictionary &result) const;
69
70protected:
72
75 {
76 public:
79 : HdStBufferArrayRange(resourceRegistry),
80 _stripedBufferArray(nullptr),
81 _elementOffset(0),
82 _numElements(0),
83 _capacity(0)
84 {
85 }
86
88 HDST_API
90
92 bool IsValid() const override {
93 return (bool)_stripedBufferArray;
94 }
95
97 HDST_API
98 bool IsAssigned() const override;
99
101 bool IsImmutable() const override;
102
104 bool RequiresStaging() const override;
105
108 HDST_API
109 bool Resize(int numElements) override;
110
112 HDST_API
113 void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
114
116 HDST_API
117 VtValue ReadData(TfToken const &name) const override;
118
120 int GetElementOffset() const override {
121 return _elementOffset;
122 }
123
126 int GetByteOffset(TfToken const& resourceName) const override;
127
129 size_t GetNumElements() const override {
130 return _numElements;
131 }
132
134 size_t GetVersion() const override {
135 return _stripedBufferArray->GetVersion();
136 }
137
139 void IncrementVersion() override {
140 _stripedBufferArray->IncrementVersion();
141 }
142
144 HDST_API
145 size_t GetMaxNumElements() const override;
146
148 HDST_API
149 HdBufferArrayUsageHint GetUsageHint() const override;
150
153 HDST_API
154 HdStBufferResourceSharedPtr GetResource() const override;
155
157 HDST_API
158 HdStBufferResourceSharedPtr GetResource(TfToken const& name) override;
159
161 HDST_API
162 HdStBufferResourceNamedList const& GetResources() const override;
163
165 HDST_API
166 void SetBufferArray(HdBufferArray *bufferArray) override;
167
169 HDST_API
170 void DebugDump(std::ostream &out) const override;
171
173 void SetElementOffset(int offset) {
174 _elementOffset = offset;
175 }
176
178 void SetNumElements(int numElements) {
179 _numElements = numElements;
180 }
181
183 int GetCapacity() const {
184 return _capacity;
185 }
186
188 void SetCapacity(int capacity) {
189 _capacity = capacity;
190 }
191
193 void Invalidate() {
194 _stripedBufferArray = NULL;
195 }
196
197 protected:
199 HDST_API
200 const void *_GetAggregation() const override;
201
202 private:
203 // Returns the byte offset at which the BAR begins for the resource.
204 size_t _GetByteOffset(HdStBufferResourceSharedPtr const& resource)
205 const;
206
207 // holding a weak reference to container.
208 // this pointer becomes null when the StripedBufferArray gets destructed,
209 // in case if any drawItem still holds this bufferRange.
210 _StripedBufferArray *_stripedBufferArray;
211 int _elementOffset;
212 size_t _numElements;
213 int _capacity;
214 };
215
216 using _StripedBufferArraySharedPtr =
217 std::shared_ptr<_StripedBufferArray>;
218 using _StripedBufferArrayRangeSharedPtr =
219 std::shared_ptr<_StripedBufferArrayRange>;
220 using _StripedBufferArrayRangePtr =
221 std::weak_ptr<_StripedBufferArrayRange>;
222
225 {
226 public:
228 HDST_API
230 TfToken const &role,
231 HdBufferSpecVector const &bufferSpecs,
232 HdBufferArrayUsageHint usageHint);
233
235 HDST_API
237
240 HDST_API
241 bool GarbageCollect() override;
242
244 HDST_API
245 void DebugDump(std::ostream &out) const override;
246
249 HDST_API
251 std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
252 HdBufferArraySharedPtr const &curRangeOwner) override;
253
255 HDST_API
256 size_t GetMaxNumElements() const override;
257
260 _needsReallocation = true;
261 }
262
265 _needsCompaction = true;
266 }
267
270 HDST_API
271 HdStBufferResourceSharedPtr GetResource() const;
272
277 HDST_API
278 HdStBufferResourceSharedPtr GetResource(TfToken const& name);
279
281 HdStBufferResourceNamedList const& GetResources() const
282 {return _resourceList;}
283
285 HDST_API
286 HdBufferSpecVector GetBufferSpecs() const;
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:
300
301 HdStResourceRegistry* _resourceRegistry;
302 bool _needsCompaction;
303 int _totalCapacity;
304 size_t _maxBytesPerElement;
305 HgiBufferUsage _bufferUsage;
306
307 HdStBufferResourceNamedList _resourceList;
308
309 // Helper routine to cast the range shared pointer.
310 _StripedBufferArrayRangeSharedPtr _GetRangeSharedPtr(size_t idx) const {
311 return std::static_pointer_cast<_StripedBufferArrayRange>(GetRange(idx).lock());
312 }
313 };
314
315 HdStResourceRegistry* _resourceRegistry;
316};
317
318PXR_NAMESPACE_CLOSE_SCOPE
319
320#endif // PXR_IMAGING_HD_ST_VBO_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
Interface class for representing range (subset) locator of HdBufferArray.
A central registry of all GPU resources.
void SetNeedsCompaction()
Mark to perform compaction on GarbageCollect()
HDST_API ~_StripedBufferArray() override
Destructor. It invalidates _rangeList.
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()
HDST_API HdStBufferResourceSharedPtr GetResource() const
Returns the GPU resource.
HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner) override
Performs reallocation.
HDST_API bool GarbageCollect() override
perform compaction if necessary.
HDST_API void DebugDump(std::ostream &out) const override
Debug output.
HdStBufferResourceNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
HDST_API _StripedBufferArray(HdStResourceRegistry *resourceRegistry, TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Constructor.
HDST_API size_t GetMaxNumElements() const override
Returns the maximum number of elements capacity.
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 SetNumElements(int numElements)
Set the number of elements for this range.
void IncrementVersion() override
Increment the version of the buffer array.
bool IsValid() const override
Returns true if this range is valid.
_StripedBufferArrayRange(HdStResourceRegistry *resourceRegistry)
Constructor.
HDST_API bool IsAssigned() const override
Returns true is the range has been assigned to a buffer.
int GetElementOffset() const override
Returns the relative element offset in aggregated buffer.
HDST_API void DebugDump(std::ostream &out) const override
Debug dump.
bool IsImmutable() const override
Returns true if this bar is marked as immutable.
HDST_API ~_StripedBufferArrayRange() override
Destructor.
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.
void SetElementOffset(int offset)
Set the relative offset for this range.
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.
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.
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...
VBO memory manager.
virtual size_t GetResourceAllocation(HdBufferArraySharedPtr const &bufferArray, VtDictionary &result) const
Returns the size of the GPU memory used by the passed buffer array.
virtual HDST_API HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Factory for creating HdBufferArray managed by HdStVBOMemoryManager aggregation.
virtual HDST_API HdBufferArrayRangeSharedPtr CreateBufferArrayRange()
Factory for creating HdBufferArrayRange managed by HdStVBOMemoryManager aggregation.
virtual HDST_API AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
virtual HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const
Returns the buffer specs from a given buffer array.
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:43
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:147
HdTupleType represents zero, one, or more values of the same HdType.
Definition: types.h:341
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...