vboSimpleMemoryManager.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_SIMPLE_MEMORY_MANAGER_H
25 #define PXR_IMAGING_HD_ST_VBO_SIMPLE_MEMORY_MANAGER_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
29 #include "pxr/imaging/hdSt/bufferArrayRange.h"
30 #include "pxr/imaging/hdSt/resourceRegistry.h"
31 #include "pxr/imaging/hd/version.h"
32 #include "pxr/imaging/hd/strategyBase.h"
33 #include "pxr/imaging/hd/bufferArray.h"
34 #include "pxr/imaging/hd/bufferSpec.h"
35 #include "pxr/imaging/hd/bufferSource.h"
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
40 
48 {
49 public:
51  : _resourceRegistry(resourceRegistry) {}
52 
55  HDST_API
56  virtual HdBufferArraySharedPtr CreateBufferArray(
57  TfToken const &role,
58  HdBufferSpecVector const &bufferSpecs,
59  HdBufferArrayUsageHint usageHint);
60 
62  HDST_API
63  virtual HdBufferArrayRangeSharedPtr CreateBufferArrayRange();
64 
66  HDST_API
68  HdBufferSpecVector const &bufferSpecs,
69  HdBufferArrayUsageHint usageHint) const;
70 
72  virtual HdBufferSpecVector GetBufferSpecs(
73  HdBufferArraySharedPtr const &bufferArray) const;
74 
76  virtual size_t GetResourceAllocation(
77  HdBufferArraySharedPtr const &bufferArray,
78  VtDictionary &result) const;
79 
80 protected:
81  class _SimpleBufferArray;
82 
88  {
89  public:
92  : HdStBufferArrayRange(resourceRegistry)
93  , _bufferArray(nullptr)
94  , _numElements(0) {
95  }
96 
98  bool IsValid() const override {
99  return (bool)_bufferArray;
100  }
101 
103  HDST_API
104  bool IsAssigned() const override;
105 
107  bool IsImmutable() const override;
108 
110  bool RequiresStaging() const override;
111 
114  bool Resize(int numElements) override {
115  _numElements = numElements;
116  return _bufferArray->Resize(numElements);
117  }
118 
120  HDST_API
121  void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
122 
124  HDST_API
125  VtValue ReadData(TfToken const &name) const override;
126 
129  int GetElementOffset() const override {
130  return 0;
131  }
132 
135  int GetByteOffset(TfToken const& resourceName) const override {
136  TF_UNUSED(resourceName);
137  return 0;
138  }
139 
141  size_t GetNumElements() const override {
142  return _numElements;
143  }
144 
146  int GetCapacity() const {
147  return _bufferArray->GetCapacity();
148  }
149 
151  size_t GetVersion() const override {
152  return _bufferArray->GetVersion();
153  }
154 
156  void IncrementVersion() override {
157  _bufferArray->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 Invalidate() {
191  _bufferArray = NULL;
192  }
193 
194  protected:
196  HDST_API
197  const void *_GetAggregation() const override;
198 
200  HDST_API
201  HdStBufferResourceSharedPtr _AddResource(TfToken const& name,
202  HdTupleType tupleType,
203  int offset,
204  int stride);
205 
206  private:
207  _SimpleBufferArray * _bufferArray;
208  size_t _numElements;
209  };
210 
211  using _SimpleBufferArraySharedPtr =
212  std::shared_ptr<_SimpleBufferArray>;
213  using _SimpleBufferArrayRangeSharedPtr =
214  std::shared_ptr<_SimpleBufferArrayRange>;
215  using _SimpleBufferArrayRangePtr =
216  std::weak_ptr<_SimpleBufferArrayRange>;
217 
222  class _SimpleBufferArray final : public HdBufferArray
223  {
224  public:
226  HDST_API
227  _SimpleBufferArray(HdStResourceRegistry* resourceRegistry,
228  TfToken const &role,
229  HdBufferSpecVector const &bufferSpecs,
230  HdBufferArrayUsageHint usageHint);
231 
233  HDST_API
234  ~_SimpleBufferArray() override;
235 
237  HDST_API
238  bool GarbageCollect() override;
239 
241  HDST_API
242  void DebugDump(std::ostream &out) const override;
243 
245  HDST_API
246  bool Resize(int numElements);
247 
250  HDST_API
251  void Reallocate(
252  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
253  HdBufferArraySharedPtr const &curRangeOwner) override;
254 
256  HDST_API
257  size_t GetMaxNumElements() const override;
258 
260  int GetCapacity() const {
261  return _capacity;
262  }
263 
268 
271  HDST_API
272  HdStBufferResourceSharedPtr GetResource() const;
273 
278  HDST_API
279  HdStBufferResourceSharedPtr GetResource(TfToken const& name);
280 
282  HdStBufferResourceNamedList const& GetResources() const {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  private:
299  HdStResourceRegistry* const _resourceRegistry;
300  int _capacity;
301  size_t _maxBytesPerElement;
302 
303  HdStBufferResourceNamedList _resourceList;
304 
305  _SimpleBufferArrayRangeSharedPtr _GetRangeSharedPtr() const {
306  return GetRangeCount() > 0
307  ? std::static_pointer_cast<_SimpleBufferArrayRange>(GetRange(0).lock())
308  : _SimpleBufferArrayRangeSharedPtr();
309  }
310  };
311 
312  HdStResourceRegistry* const _resourceRegistry;
313 };
314 
315 PXR_NAMESPACE_CLOSE_SCOPE
316 
317 #endif // PXR_IMAGING_HD_ST_VBO_SIMPLE_MEMORY_MANAGER_H
HDST_API const void * _GetAggregation() const override
Returns the aggregation container.
HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner) override
Performs reallocation.
size_t AggregationId
Aggregation ID.
Definition: strategyBase.h:51
int GetCapacity() const
Returns the capacity of allocated area for this range.
The union provides a set of flags that provide hints to the memory management system about the proper...
Definition: bufferArray.h:70
bool IsValid() const override
Returns true if this range is valid.
virtual HDST_API HdBufferArrayRangeSharedPtr CreateBufferArrayRange()
Factory for creating HdBufferArrayRange.
Similar to a VAO, this object is a bundle of coherent buffers.
Definition: bufferArray.h:88
HDST_API HdStBufferResourceSharedPtr GetResource() const override
Returns the GPU resource.
VBO simple memory manager.
Aggregation strategy base class.
Definition: strategyBase.h:48
HDST_API HdStBufferResourceSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
HD_API void IncrementVersion()
Increments the version of this buffer array.
_SimpleBufferArrayRange(HdStResourceRegistry *resourceRegistry)
Constructor.
Interface class for representing range (subset) locator of HdBufferArray.
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
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...
virtual HDST_API HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Factory for creating HdBufferArray managed by HdStVBOSimpleMemoryManager.
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.
#define TF_UNUSED(x)
Stops compiler from producing unused argument or variable warnings.
Definition: tf.h:185
HDST_API _SimpleBufferArray(HdStResourceRegistry *resourceRegistry, TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Constructor.
size_t GetNumElements() const override
Returns the number of elements allocated.
HDST_API void DebugDump(std::ostream &out) const override
Debug output.
HDST_API size_t GetMaxNumElements() const override
Returns the maximum number of elements capacity.
void IncrementVersion() override
Increment the version of the buffer array.
HDST_API bool IsAssigned() const override
Returns true is the range has been assigned to a buffer.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
HDST_API HdStBufferResourceNamedList const & GetResources() const override
Returns the list of all named GPU resources for this bufferArrayRange.
HdStBufferResourceNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
size_t GetRangeCount() const
How many ranges are attached to the buffer array.
Definition: bufferArray.h:138
HDST_API void CopyData(HdBufferSourceSharedPtr const &bufferSource) override
Copy source data into buffer.
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
virtual HDST_API HdAggregationStrategy::AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
A central registry of all GPU resources.
HDST_API void SetBufferArray(HdBufferArray *bufferArray) override
Sets the buffer array associated with this buffer;.
HDST_API bool GarbageCollect() override
perform compaction if necessary, returns true if it becomes empty.
HDST_API HdStBufferResourceSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
HDST_API bool Resize(int numElements)
Set to resize buffers. Actual reallocation happens on Reallocate()
HDST_API void DebugDump(std::ostream &out) const override
Debug dump.
bool RequiresStaging() const override
Returns true if this needs a staging buffer for CPU to GPU copies.
HDST_API ~_SimpleBufferArray() override
Destructor. It invalidates _range.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
bool Resize(int numElements) override
Resize memory area for this range.
virtual HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const
Returns the buffer specs from a given buffer array.
HDST_API HdStBufferResourceSharedPtr GetResource() const
TODO: We need to distinguish between the primvar types here, we should tag each HdBufferSource and Hd...
size_t GetVersion() const override
Returns the version of the buffer array.
int GetCapacity() const
Returns current capacity. It could be different from numElements.
int GetElementOffset() const override
Returns the offset at which this range begins in the underlying buffer array in terms of elements.
HDST_API VtValue ReadData(TfToken const &name) const override
Read back the buffer content.
virtual size_t GetResourceAllocation(HdBufferArraySharedPtr const &bufferArray, VtDictionary &result) const
Returns the size of the GPU memory used by the passed buffer array.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166
size_t GetVersion() const
Returns the version of this buffer array.
Definition: bufferArray.h:104
HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
Specialized buffer array range for SimpleBufferArray.