Loading...
Searching...
No Matches
vboMemoryManager.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_VBO_MEMORY_MANAGER_H
25#define PXR_IMAGING_HD_ST_VBO_MEMORY_MANAGER_H
26
27#include "pxr/pxr.h"
28#include "pxr/imaging/hdSt/api.h"
29#include "pxr/imaging/hdSt/strategyBase.h"
30
31#include "pxr/imaging/hgi/enums.h"
32
33#include "pxr/imaging/hd/bufferArray.h"
34#include "pxr/imaging/hdSt/bufferArrayRange.h"
35#include "pxr/imaging/hd/bufferSpec.h"
36#include "pxr/imaging/hd/bufferSource.h"
37
39#include "pxr/base/tf/token.h"
40
41#include <list>
42#include <memory>
43
44PXR_NAMESPACE_OPEN_SCOPE
45
47
53{
54public:
57 , _resourceRegistry(resourceRegistry) {}
58
61 HDST_API
62 virtual HdBufferArraySharedPtr CreateBufferArray(
63 TfToken const &role,
64 HdBufferSpecVector const &bufferSpecs,
65 HdBufferArrayUsageHint usageHint);
66
69 HDST_API
70 virtual HdBufferArrayRangeSharedPtr CreateBufferArrayRange();
71
73 HDST_API
75 HdBufferSpecVector const &bufferSpecs,
76 HdBufferArrayUsageHint usageHint) const;
77
79 virtual HdBufferSpecVector GetBufferSpecs(
80 HdBufferArraySharedPtr const &bufferArray) const;
81
83 virtual size_t GetResourceAllocation(
84 HdBufferArraySharedPtr const &bufferArray,
85 VtDictionary &result) const;
86
87protected:
89
92 {
93 public:
96 : HdStBufferArrayRange(resourceRegistry),
97 _stripedBufferArray(nullptr),
98 _elementOffset(0),
99 _numElements(0),
100 _capacity(0)
101 {
102 }
103
105 HDST_API
107
109 bool IsValid() const override {
110 return (bool)_stripedBufferArray;
111 }
112
114 HDST_API
115 bool IsAssigned() const override;
116
118 bool IsImmutable() const override;
119
121 bool RequiresStaging() const override;
122
125 HDST_API
126 bool Resize(int numElements) override;
127
129 HDST_API
130 void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
131
133 HDST_API
134 VtValue ReadData(TfToken const &name) const override;
135
137 int GetElementOffset() const override {
138 return _elementOffset;
139 }
140
143 int GetByteOffset(TfToken const& resourceName) const override;
144
146 size_t GetNumElements() const override {
147 return _numElements;
148 }
149
151 size_t GetVersion() const override {
152 return _stripedBufferArray->GetVersion();
153 }
154
156 void IncrementVersion() override {
157 _stripedBufferArray->IncrementVersion();
158 }
159
161 HDST_API
162 size_t GetMaxNumElements() const override;
163
165 HDST_API
166 HdBufferArrayUsageHint GetUsageHint() const override;
167
170 HDST_API
171 HdStBufferResourceSharedPtr GetResource() const override;
172
174 HDST_API
175 HdStBufferResourceSharedPtr GetResource(TfToken const& name) override;
176
178 HDST_API
179 HdStBufferResourceNamedList const& GetResources() const override;
180
182 HDST_API
183 void SetBufferArray(HdBufferArray *bufferArray) override;
184
186 HDST_API
187 void DebugDump(std::ostream &out) const override;
188
190 void SetElementOffset(int offset) {
191 _elementOffset = offset;
192 }
193
195 void SetNumElements(int numElements) {
196 _numElements = numElements;
197 }
198
200 int GetCapacity() const {
201 return _capacity;
202 }
203
205 void SetCapacity(int capacity) {
206 _capacity = capacity;
207 }
208
210 void Invalidate() {
211 _stripedBufferArray = NULL;
212 }
213
214 protected:
216 HDST_API
217 const void *_GetAggregation() const override;
218
219 private:
220 // Returns the byte offset at which the BAR begins for the resource.
221 size_t _GetByteOffset(HdStBufferResourceSharedPtr const& resource)
222 const;
223
224 // holding a weak reference to container.
225 // this pointer becomes null when the StripedBufferArray gets destructed,
226 // in case if any drawItem still holds this bufferRange.
227 _StripedBufferArray *_stripedBufferArray;
228 int _elementOffset;
229 size_t _numElements;
230 int _capacity;
231 };
232
233 using _StripedBufferArraySharedPtr =
234 std::shared_ptr<_StripedBufferArray>;
235 using _StripedBufferArrayRangeSharedPtr =
236 std::shared_ptr<_StripedBufferArrayRange>;
237 using _StripedBufferArrayRangePtr =
238 std::weak_ptr<_StripedBufferArrayRange>;
239
242 {
243 public:
245 HDST_API
247 TfToken const &role,
248 HdBufferSpecVector const &bufferSpecs,
249 HdBufferArrayUsageHint usageHint);
250
252 HDST_API
254
257 HDST_API
258 bool GarbageCollect() override;
259
261 HDST_API
262 void DebugDump(std::ostream &out) const override;
263
266 HDST_API
268 std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
269 HdBufferArraySharedPtr const &curRangeOwner) override;
270
272 HDST_API
273 size_t GetMaxNumElements() const override;
274
277 _needsReallocation = true;
278 }
279
282 _needsCompaction = true;
283 }
284
287 HDST_API
288 HdStBufferResourceSharedPtr GetResource() const;
289
294 HDST_API
295 HdStBufferResourceSharedPtr GetResource(TfToken const& name);
296
298 HdStBufferResourceNamedList const& GetResources() const
299 {return _resourceList;}
300
302 HDST_API
303 HdBufferSpecVector GetBufferSpecs() const;
304
305 protected:
306 HDST_API
307 void _DeallocateResources();
308
310 HDST_API
311 HdStBufferResourceSharedPtr _AddResource(TfToken const& name,
312 HdTupleType tupleType,
313 int offset,
314 int stride);
315
316 private:
317
318 HdStResourceRegistry* _resourceRegistry;
319 bool _needsCompaction;
320 int _totalCapacity;
321 size_t _maxBytesPerElement;
322 HgiBufferUsage _bufferUsage;
323
324 HdStBufferResourceNamedList _resourceList;
325
326 // Helper routine to cast the range shared pointer.
327 _StripedBufferArrayRangeSharedPtr _GetRangeSharedPtr(size_t idx) const {
328 return std::static_pointer_cast<_StripedBufferArrayRange>(GetRange(idx).lock());
329 }
330 };
331
332 HdStResourceRegistry* _resourceRegistry;
333};
334
335PXR_NAMESPACE_CLOSE_SCOPE
336
337#endif // PXR_IMAGING_HD_ST_VBO_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
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: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
HdTupleType represents zero, one, or more values of the same HdType.
Definition: types.h:358
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...