Loading...
Searching...
No Matches
vboSimpleMemoryManager.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_SIMPLE_MEMORY_MANAGER_H
8#define PXR_IMAGING_HD_ST_VBO_SIMPLE_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/resourceRegistry.h"
14#include "pxr/imaging/hdSt/strategyBase.h"
15
16#include "pxr/imaging/hd/bufferArray.h"
17#include "pxr/imaging/hd/bufferSpec.h"
18#include "pxr/imaging/hd/bufferSource.h"
19
20PXR_NAMESPACE_OPEN_SCOPE
21
23
31{
32public:
34 : _resourceRegistry(resourceRegistry) {}
35
38 HDST_API
39 virtual HdBufferArraySharedPtr CreateBufferArray(
40 TfToken const &role,
41 HdBufferSpecVector const &bufferSpecs,
42 HdBufferArrayUsageHint usageHint);
43
45 HDST_API
46 virtual HdBufferArrayRangeSharedPtr CreateBufferArrayRange();
47
49 HDST_API
51 HdBufferSpecVector const &bufferSpecs,
52 HdBufferArrayUsageHint usageHint) const;
53
55 HDST_API
56 virtual HdBufferSpecVector GetBufferSpecs(
57 HdBufferArraySharedPtr const &bufferArray) const;
58
60 HDST_API
61 virtual size_t GetResourceAllocation(
62 HdBufferArraySharedPtr const &bufferArray,
63 VtDictionary &result) const;
64
65protected:
67
73 {
74 public:
77 : HdStBufferArrayRange(resourceRegistry)
78 , _bufferArray(nullptr)
79 , _numElements(0) {
80 }
81
83 bool IsValid() const override {
84 return (bool)_bufferArray;
85 }
86
88 HDST_API
89 bool IsAssigned() const override;
90
92 bool IsImmutable() const override;
93
95 bool RequiresStaging() const override;
96
99 bool Resize(int numElements) override {
100 _numElements = numElements;
101 return _bufferArray->Resize(numElements);
102 }
103
105 HDST_API
106 void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
107
109 HDST_API
110 VtValue ReadData(TfToken const &name) const override;
111
114 int GetElementOffset() const override {
115 return 0;
116 }
117
120 int GetByteOffset(TfToken const& resourceName) const override {
121 TF_UNUSED(resourceName);
122 return 0;
123 }
124
126 size_t GetNumElements() const override {
127 return _numElements;
128 }
129
131 int GetCapacity() const {
132 return _bufferArray->GetCapacity();
133 }
134
136 size_t GetVersion() const override {
137 return _bufferArray->GetVersion();
138 }
139
141 void IncrementVersion() override {
142 _bufferArray->IncrementVersion();
143 }
144
146 HDST_API
147 size_t GetMaxNumElements() const override;
148
150 HDST_API
151 HdBufferArrayUsageHint GetUsageHint() const override;
152
155 HDST_API
156 HdStBufferResourceSharedPtr GetResource() const override;
157
159 HDST_API
160 HdStBufferResourceSharedPtr GetResource(TfToken const& name) override;
161
163 HDST_API
164 HdStBufferResourceNamedList const& GetResources() const override;
165
167 HDST_API
168 void SetBufferArray(HdBufferArray *bufferArray) override;
169
171 HDST_API
172 void DebugDump(std::ostream &out) const override;
173
175 void Invalidate() {
176 _bufferArray = NULL;
177 }
178
179 protected:
181 HDST_API
182 const void *_GetAggregation() const override;
183
185 HDST_API
186 HdStBufferResourceSharedPtr _AddResource(TfToken const& name,
187 HdTupleType tupleType,
188 int offset,
189 int stride);
190
191 private:
192 _SimpleBufferArray * _bufferArray;
193 size_t _numElements;
194 };
195
196 using _SimpleBufferArraySharedPtr =
197 std::shared_ptr<_SimpleBufferArray>;
198 using _SimpleBufferArrayRangeSharedPtr =
199 std::shared_ptr<_SimpleBufferArrayRange>;
200 using _SimpleBufferArrayRangePtr =
201 std::weak_ptr<_SimpleBufferArrayRange>;
202
208 {
209 public:
211 HDST_API
213 TfToken const &role,
214 HdBufferSpecVector const &bufferSpecs,
215 HdBufferArrayUsageHint usageHint);
216
218 HDST_API
220
222 HDST_API
223 bool GarbageCollect() override;
224
226 HDST_API
227 void DebugDump(std::ostream &out) const override;
228
230 HDST_API
231 bool Resize(int numElements);
232
235 HDST_API
237 std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
238 HdBufferArraySharedPtr const &curRangeOwner) override;
239
241 HDST_API
242 size_t GetMaxNumElements() const override;
243
245 int GetCapacity() const {
246 return _capacity;
247 }
248
251 HDST_API
252 HdStBufferResourceSharedPtr GetResource() const;
253
258 HDST_API
259 HdStBufferResourceSharedPtr GetResource(TfToken const& name);
260
262 HdStBufferResourceNamedList const& GetResources() const {return _resourceList;}
263
265 HDST_API
266 HdBufferSpecVector GetBufferSpecs() const;
267
268 protected:
269 HDST_API
270 void _DeallocateResources();
271
273 HDST_API
274 HdStBufferResourceSharedPtr _AddResource(TfToken const& name,
275 HdTupleType tupleType,
276 int offset,
277 int stride);
278 private:
279 HdStResourceRegistry* const _resourceRegistry;
280 int _capacity;
281 size_t _maxBytesPerElement;
282 HgiBufferUsage _bufferUsage;
283
284 HdStBufferResourceNamedList _resourceList;
285
286 _SimpleBufferArrayRangeSharedPtr _GetRangeSharedPtr() const {
287 return GetRangeCount() > 0
288 ? std::static_pointer_cast<_SimpleBufferArrayRange>(GetRange(0).lock())
289 : _SimpleBufferArrayRangeSharedPtr();
290 }
291 };
292
293 HdStResourceRegistry* const _resourceRegistry;
294};
295
296PXR_NAMESPACE_CLOSE_SCOPE
297
298#endif // PXR_IMAGING_HD_ST_VBO_SIMPLE_MEMORY_MANAGER_H
Similar to a VAO, this object is a bundle of coherent buffers.
Definition: bufferArray.h:69
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
size_t GetRangeCount() const
How many ranges are attached to the buffer array.
Definition: bufferArray.h:118
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.
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.
HDST_API HdStBufferResourceSharedPtr GetResource() const
Returns the GPU resource.
int GetCapacity() const
Returns current capacity. It could be different from numElements.
HDST_API _SimpleBufferArray(HdStResourceRegistry *resourceRegistry, TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Constructor.
HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner) override
Performs reallocation.
HDST_API bool GarbageCollect() override
perform compaction if necessary, returns true if it becomes empty.
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 bool Resize(int numElements)
Set to resize buffers. Actual reallocation happens on Reallocate()
HDST_API size_t GetMaxNumElements() const override
Returns the maximum number of elements capacity.
HDST_API ~_SimpleBufferArray() override
Destructor. It invalidates _range.
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
Specialized buffer array range for SimpleBufferArray.
HDST_API HdStBufferResourceSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
HDST_API void CopyData(HdBufferSourceSharedPtr const &bufferSource) override
Copy source data into buffer.
size_t GetNumElements() const override
Returns the number of elements allocated.
HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
_SimpleBufferArrayRange(HdStResourceRegistry *resourceRegistry)
Constructor.
HDST_API void SetBufferArray(HdBufferArray *bufferArray) override
Sets the buffer array associated with this buffer;.
int GetCapacity() const
Returns the capacity of allocated area for this range.
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.
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.
bool Resize(int numElements) override
Resize memory area 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.
HDST_API const void * _GetAggregation() const override
Returns the aggregation container.
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 simple memory manager.
virtual HDST_API HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Factory for creating HdBufferArray managed by HdStVBOSimpleMemoryManager.
virtual HDST_API HdBufferArrayRangeSharedPtr CreateBufferArrayRange()
Factory for creating HdBufferArrayRange.
virtual HDST_API HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const
Returns the buffer specs from a given buffer array.
virtual HDST_API 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 HdStAggregationStrategy::AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
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:152
#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