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 virtual HdBufferSpecVector GetBufferSpecs(
56 HdBufferArraySharedPtr const &bufferArray) const;
57
59 virtual size_t GetResourceAllocation(
60 HdBufferArraySharedPtr const &bufferArray,
61 VtDictionary &result) const;
62
63protected:
65
71 {
72 public:
75 : HdStBufferArrayRange(resourceRegistry)
76 , _bufferArray(nullptr)
77 , _numElements(0) {
78 }
79
81 bool IsValid() const override {
82 return (bool)_bufferArray;
83 }
84
86 HDST_API
87 bool IsAssigned() const override;
88
90 bool IsImmutable() const override;
91
93 bool RequiresStaging() const override;
94
97 bool Resize(int numElements) override {
98 _numElements = numElements;
99 return _bufferArray->Resize(numElements);
100 }
101
103 HDST_API
104 void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
105
107 HDST_API
108 VtValue ReadData(TfToken const &name) const override;
109
112 int GetElementOffset() const override {
113 return 0;
114 }
115
118 int GetByteOffset(TfToken const& resourceName) const override {
119 TF_UNUSED(resourceName);
120 return 0;
121 }
122
124 size_t GetNumElements() const override {
125 return _numElements;
126 }
127
129 int GetCapacity() const {
130 return _bufferArray->GetCapacity();
131 }
132
134 size_t GetVersion() const override {
135 return _bufferArray->GetVersion();
136 }
137
139 void IncrementVersion() override {
140 _bufferArray->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 Invalidate() {
174 _bufferArray = NULL;
175 }
176
177 protected:
179 HDST_API
180 const void *_GetAggregation() const override;
181
183 HDST_API
184 HdStBufferResourceSharedPtr _AddResource(TfToken const& name,
185 HdTupleType tupleType,
186 int offset,
187 int stride);
188
189 private:
190 _SimpleBufferArray * _bufferArray;
191 size_t _numElements;
192 };
193
194 using _SimpleBufferArraySharedPtr =
195 std::shared_ptr<_SimpleBufferArray>;
196 using _SimpleBufferArrayRangeSharedPtr =
197 std::shared_ptr<_SimpleBufferArrayRange>;
198 using _SimpleBufferArrayRangePtr =
199 std::weak_ptr<_SimpleBufferArrayRange>;
200
206 {
207 public:
209 HDST_API
211 TfToken const &role,
212 HdBufferSpecVector const &bufferSpecs,
213 HdBufferArrayUsageHint usageHint);
214
216 HDST_API
218
220 HDST_API
221 bool GarbageCollect() override;
222
224 HDST_API
225 void DebugDump(std::ostream &out) const override;
226
228 HDST_API
229 bool Resize(int numElements);
230
233 HDST_API
235 std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
236 HdBufferArraySharedPtr const &curRangeOwner) override;
237
239 HDST_API
240 size_t GetMaxNumElements() const override;
241
243 int GetCapacity() const {
244 return _capacity;
245 }
246
249 HDST_API
250 HdStBufferResourceSharedPtr GetResource() const;
251
256 HDST_API
257 HdStBufferResourceSharedPtr GetResource(TfToken const& name);
258
260 HdStBufferResourceNamedList const& GetResources() const {return _resourceList;}
261
263 HDST_API
264 HdBufferSpecVector GetBufferSpecs() const;
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 private:
277 HdStResourceRegistry* const _resourceRegistry;
278 int _capacity;
279 size_t _maxBytesPerElement;
280 HgiBufferUsage _bufferUsage;
281
282 HdStBufferResourceNamedList _resourceList;
283
284 _SimpleBufferArrayRangeSharedPtr _GetRangeSharedPtr() const {
285 return GetRangeCount() > 0
286 ? std::static_pointer_cast<_SimpleBufferArrayRange>(GetRange(0).lock())
287 : _SimpleBufferArrayRangeSharedPtr();
288 }
289 };
290
291 HdStResourceRegistry* const _resourceRegistry;
292};
293
294PXR_NAMESPACE_CLOSE_SCOPE
295
296#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 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 HdStVBOSimpleMemoryManager.
virtual HDST_API HdBufferArrayRangeSharedPtr CreateBufferArrayRange()
Factory for creating HdBufferArrayRange.
virtual HDST_API HdStAggregationStrategy::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
#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:341