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/hd/version.h"
30 #include "pxr/imaging/hd/bufferArray.h"
31 #include "pxr/imaging/hdSt/bufferArrayRange.h"
32 #include "pxr/imaging/hd/bufferSpec.h"
33 #include "pxr/imaging/hd/bufferSource.h"
34 #include "pxr/imaging/hd/strategyBase.h"
35 
36 #include "pxr/base/tf/mallocTag.h"
37 #include "pxr/base/tf/token.h"
38 
39 #include <list>
40 #include <memory>
41 
42 PXR_NAMESPACE_OPEN_SCOPE
43 
45 
51 {
52 public:
55  , _resourceRegistry(resourceRegistry) {}
56 
59  HDST_API
60  virtual HdBufferArraySharedPtr CreateBufferArray(
61  TfToken const &role,
62  HdBufferSpecVector const &bufferSpecs,
63  HdBufferArrayUsageHint usageHint);
64 
67  HDST_API
68  virtual HdBufferArrayRangeSharedPtr CreateBufferArrayRange();
69 
71  HDST_API
73  HdBufferSpecVector const &bufferSpecs,
74  HdBufferArrayUsageHint usageHint) const;
75 
77  virtual HdBufferSpecVector GetBufferSpecs(
78  HdBufferArraySharedPtr const &bufferArray) const;
79 
81  virtual size_t GetResourceAllocation(
82  HdBufferArraySharedPtr const &bufferArray,
83  VtDictionary &result) const;
84 
85 protected:
86  class _StripedBufferArray;
87 
90  {
91  public:
94  : HdStBufferArrayRange(resourceRegistry),
95  _stripedBufferArray(nullptr),
96  _elementOffset(0),
97  _numElements(0),
98  _capacity(0)
99  {
100  }
101 
103  HDST_API
104  ~_StripedBufferArrayRange() override;
105 
107  bool IsValid() const override {
108  return (bool)_stripedBufferArray;
109  }
110 
112  HDST_API
113  bool IsAssigned() const override;
114 
116  bool IsImmutable() const override;
117 
119  bool RequiresStaging() const override;
120 
123  HDST_API
124  bool Resize(int numElements) override;
125 
127  HDST_API
128  void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
129 
131  HDST_API
132  VtValue ReadData(TfToken const &name) const override;
133 
135  int GetElementOffset() const override {
136  return _elementOffset;
137  }
138 
141  int GetByteOffset(TfToken const& resourceName) const override;
142 
144  size_t GetNumElements() const override {
145  return _numElements;
146  }
147 
149  size_t GetVersion() const override {
150  return _stripedBufferArray->GetVersion();
151  }
152 
154  void IncrementVersion() override {
155  _stripedBufferArray->IncrementVersion();
156  }
157 
159  HDST_API
160  size_t GetMaxNumElements() const override;
161 
163  HDST_API
164  HdBufferArrayUsageHint GetUsageHint() const override;
165 
168  HDST_API
169  HdStBufferResourceSharedPtr GetResource() const override;
170 
172  HDST_API
173  HdStBufferResourceSharedPtr GetResource(TfToken const& name) override;
174 
176  HDST_API
177  HdStBufferResourceNamedList const& GetResources() const override;
178 
180  HDST_API
181  void SetBufferArray(HdBufferArray *bufferArray) override;
182 
184  HDST_API
185  void DebugDump(std::ostream &out) const override;
186 
188  void SetElementOffset(int offset) {
189  _elementOffset = offset;
190  }
191 
193  void SetNumElements(int numElements) {
194  _numElements = numElements;
195  }
196 
198  int GetCapacity() const {
199  return _capacity;
200  }
201 
203  void SetCapacity(int capacity) {
204  _capacity = capacity;
205  }
206 
208  void Invalidate() {
209  _stripedBufferArray = NULL;
210  }
211 
212  protected:
214  HDST_API
215  const void *_GetAggregation() const override;
216 
217  private:
218  // Returns the byte offset at which the BAR begins for the resource.
219  size_t _GetByteOffset(HdStBufferResourceSharedPtr const& resource)
220  const;
221 
222  // holding a weak reference to container.
223  // this pointer becomes null when the StripedBufferArray gets destructed,
224  // in case if any drawItem still holds this bufferRange.
225  _StripedBufferArray *_stripedBufferArray;
226  int _elementOffset;
227  size_t _numElements;
228  int _capacity;
229  };
230 
231  using _StripedBufferArraySharedPtr =
232  std::shared_ptr<_StripedBufferArray>;
233  using _StripedBufferArrayRangeSharedPtr =
234  std::shared_ptr<_StripedBufferArrayRange>;
235  using _StripedBufferArrayRangePtr =
236  std::weak_ptr<_StripedBufferArrayRange>;
237 
240  {
241  public:
243  HDST_API
244  _StripedBufferArray(HdStResourceRegistry* resourceRegistry,
245  TfToken const &role,
246  HdBufferSpecVector const &bufferSpecs,
247  HdBufferArrayUsageHint usageHint);
248 
250  HDST_API
251  ~_StripedBufferArray() override;
252 
255  HDST_API
256  bool GarbageCollect() override;
257 
259  HDST_API
260  void DebugDump(std::ostream &out) const override;
261 
264  HDST_API
265  void Reallocate(
266  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
267  HdBufferArraySharedPtr const &curRangeOwner) override;
268 
270  HDST_API
271  size_t GetMaxNumElements() const override;
272 
275  _needsReallocation = true;
276  }
277 
280  _needsCompaction = true;
281  }
282 
287 
290  HDST_API
291  HdStBufferResourceSharedPtr GetResource() const;
292 
297  HDST_API
298  HdStBufferResourceSharedPtr GetResource(TfToken const& name);
299 
301  HdStBufferResourceNamedList const& GetResources() const
302  {return _resourceList;}
303 
305  HDST_API
306  HdBufferSpecVector GetBufferSpecs() const;
307 
308  protected:
309  HDST_API
310  void _DeallocateResources();
311 
313  HDST_API
314  HdStBufferResourceSharedPtr _AddResource(TfToken const& name,
315  HdTupleType tupleType,
316  int offset,
317  int stride);
318 
319  private:
320 
321  HdStResourceRegistry* _resourceRegistry;
322  bool _needsCompaction;
323  int _totalCapacity;
324  size_t _maxBytesPerElement;
325 
326  HdStBufferResourceNamedList _resourceList;
327 
328  // Helper routine to cast the range shared pointer.
329  _StripedBufferArrayRangeSharedPtr _GetRangeSharedPtr(size_t idx) const {
330  return std::static_pointer_cast<_StripedBufferArrayRange>(GetRange(idx).lock());
331  }
332  };
333 
334  HdStResourceRegistry* _resourceRegistry;
335 };
336 
337 PXR_NAMESPACE_CLOSE_SCOPE
338 
339 #endif // PXR_IMAGING_HD_ST_VBO_MEMORY_MANAGER_H
size_t AggregationId
Aggregation ID.
Definition: strategyBase.h:51
The union provides a set of flags that provide hints to the memory management system about the proper...
Definition: bufferArray.h:70
HDST_API _StripedBufferArray(HdStResourceRegistry *resourceRegistry, TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Constructor.
HDST_API void DebugDump(std::ostream &out) const override
Debug dump.
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
HDST_API HdStBufferResourceNamedList const & GetResources() const override
Returns the list of all named GPU resources for this bufferArrayRange.
HDST_API size_t GetMaxNumElements() const override
Returns the max number of elements.
void SetElementOffset(int offset)
Set the relative offset for this range.
HDST_API void SetBufferArray(HdBufferArray *bufferArray) override
Sets the buffer array associated with this buffer;.
int GetCapacity() const
Returns the capacity of allocated area.
Similar to a VAO, this object is a bundle of coherent buffers.
Definition: bufferArray.h:88
HDST_API bool Resize(int numElements) override
Resize memory area for this range.
Aggregation strategy base class.
Definition: strategyBase.h:48
HD_API void IncrementVersion()
Increments the version of this buffer array.
HDST_API bool GarbageCollect() override
perform compaction if necessary.
Interface class for representing range (subset) locator of HdBufferArray.
virtual HDST_API HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Factory for creating HdBufferArray managed by HdStVBOMemoryManager aggregation.
HdTupleType represents zero, one, or more values of the same HdType.
Definition: types.h:358
A map with string keys and VtValue values.
Definition: dictionary.h:62
bool IsImmutable() const override
Returns true if this bar is marked as immutable.
HDST_API HdStBufferResourceSharedPtr GetResource() const override
Returns the GPU resource.
virtual size_t GetResourceAllocation(HdBufferArraySharedPtr const &bufferArray, VtDictionary &result) const
Returns the size of the GPU memory used by the passed buffer array.
HDST_API void DebugDump(std::ostream &out) const override
Debug output.
bool IsValid() const override
Returns true if this range is valid.
HdStBufferResourceNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
void SetCapacity(int capacity)
Set the capacity of allocated area for this range.
HDST_API ~_StripedBufferArray() override
Destructor. It invalidates _rangeList.
void SetNumElements(int numElements)
Set the number of elements for this range.
virtual HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const
Returns the buffer specs from a given buffer array.
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.
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...
void SetNeedsReallocation()
Mark to perform reallocation on Reallocate()
HDST_API size_t GetMaxNumElements() const override
Returns the maximum number of elements capacity.
HDST_API HdStBufferResourceSharedPtr GetResource() const
TODO: We need to distinguish between the primvar types here, we should tag each HdBufferSource and Hd...
HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner) override
Performs reallocation.
virtual HDST_API AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
_StripedBufferArrayRange(HdStResourceRegistry *resourceRegistry)
Constructor.
void IncrementVersion() override
Increment the version of the buffer array.
void SetNeedsCompaction()
Mark to perform compaction on GarbageCollect()
HDST_API const void * _GetAggregation() const override
Returns the aggregation container.
HDST_API VtValue ReadData(TfToken const &name) const override
Read back the buffer content.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
virtual HDST_API HdBufferArrayRangeSharedPtr CreateBufferArrayRange()
Factory for creating HdBufferArrayRange managed by HdStVBOMemoryManager aggregation.
HDST_API void CopyData(HdBufferSourceSharedPtr const &bufferSource) override
Copy source data into buffer.
size_t GetVersion() const override
Returns the version of the buffer array.
bool _needsReallocation
Dirty bit to set when the ranges attached to the buffer changes.
Definition: bufferArray.h:167
bool RequiresStaging() const override
Returns true if this needs a staging buffer for CPU to GPU copies.
HDST_API bool IsAssigned() const override
Returns true is the range has been assigned to a buffer.
HDST_API ~_StripedBufferArrayRange() override
Destructor.
int GetElementOffset() const override
Returns the relative element offset in aggregated buffer.
size_t GetNumElements() const override
Returns the number of elements.
VBO memory manager.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
size_t GetVersion() const
Returns the version of this buffer array.
Definition: bufferArray.h:104