All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
111  bool Resize(int numElements) override {
112  _numElements = numElements;
113  return _bufferArray->Resize(numElements);
114  }
115 
117  HDST_API
118  void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
119 
121  HDST_API
122  VtValue ReadData(TfToken const &name) const override;
123 
126  int GetElementOffset() const override {
127  return 0;
128  }
129 
132  int GetByteOffset(TfToken const& resourceName) const override {
133  TF_UNUSED(resourceName);
134  return 0;
135  }
136 
138  size_t GetNumElements() const override {
139  return _numElements;
140  }
141 
143  int GetCapacity() const {
144  return _bufferArray->GetCapacity();
145  }
146 
148  size_t GetVersion() const override {
149  return _bufferArray->GetVersion();
150  }
151 
153  void IncrementVersion() override {
154  _bufferArray->IncrementVersion();
155  }
156 
158  HDST_API
159  size_t GetMaxNumElements() const override;
160 
162  HDST_API
163  HdBufferArrayUsageHint GetUsageHint() const override;
164 
167  HDST_API
168  HdStBufferResourceSharedPtr GetResource() const override;
169 
171  HDST_API
172  HdStBufferResourceSharedPtr GetResource(TfToken const& name) override;
173 
175  HDST_API
176  HdStBufferResourceNamedList const& GetResources() const override;
177 
179  HDST_API
180  void SetBufferArray(HdBufferArray *bufferArray) override;
181 
183  HDST_API
184  void DebugDump(std::ostream &out) const override;
185 
187  void Invalidate() {
188  _bufferArray = NULL;
189  }
190 
191  protected:
193  HDST_API
194  const void *_GetAggregation() const override;
195 
197  HDST_API
198  HdStBufferResourceSharedPtr _AddResource(TfToken const& name,
199  HdTupleType tupleType,
200  int offset,
201  int stride);
202 
203  private:
204  _SimpleBufferArray * _bufferArray;
205  size_t _numElements;
206  };
207 
208  using _SimpleBufferArraySharedPtr =
209  std::shared_ptr<_SimpleBufferArray>;
210  using _SimpleBufferArrayRangeSharedPtr =
211  std::shared_ptr<_SimpleBufferArrayRange>;
212  using _SimpleBufferArrayRangePtr =
213  std::weak_ptr<_SimpleBufferArrayRange>;
214 
219  class _SimpleBufferArray final : public HdBufferArray
220  {
221  public:
223  HDST_API
224  _SimpleBufferArray(HdStResourceRegistry* resourceRegistry,
225  TfToken const &role,
226  HdBufferSpecVector const &bufferSpecs,
227  HdBufferArrayUsageHint usageHint);
228 
230  HDST_API
231  ~_SimpleBufferArray() override;
232 
234  HDST_API
235  bool GarbageCollect() override;
236 
238  HDST_API
239  void DebugDump(std::ostream &out) const override;
240 
242  HDST_API
243  bool Resize(int numElements);
244 
247  HDST_API
248  void Reallocate(
249  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
250  HdBufferArraySharedPtr const &curRangeOwner) override;
251 
253  HDST_API
254  size_t GetMaxNumElements() const override;
255 
257  int GetCapacity() const {
258  return _capacity;
259  }
260 
265 
268  HDST_API
269  HdStBufferResourceSharedPtr GetResource() const;
270 
275  HDST_API
276  HdStBufferResourceSharedPtr GetResource(TfToken const& name);
277 
279  HdStBufferResourceNamedList const& GetResources() const {return _resourceList;}
280 
282  HDST_API
283  HdBufferSpecVector GetBufferSpecs() const;
284 
285  protected:
286  HDST_API
287  void _DeallocateResources();
288 
290  HDST_API
291  HdStBufferResourceSharedPtr _AddResource(TfToken const& name,
292  HdTupleType tupleType,
293  int offset,
294  int stride);
295  private:
296  HdStResourceRegistry* const _resourceRegistry;
297  int _capacity;
298  size_t _maxBytesPerElement;
299 
300  HdStBufferResourceNamedList _resourceList;
301 
302  _SimpleBufferArrayRangeSharedPtr _GetRangeSharedPtr() const {
303  return GetRangeCount() > 0
304  ? std::static_pointer_cast<_SimpleBufferArrayRange>(GetRange(0).lock())
305  : _SimpleBufferArrayRangeSharedPtr();
306  }
307  };
308 
309  HdStResourceRegistry* const _resourceRegistry;
310 };
311 
312 PXR_NAMESPACE_CLOSE_SCOPE
313 
314 #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
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.
virtual size_t GetResourceAllocation(HdBufferArraySharedPtr const &bufferArray, VtDictionary &result) const
Returns the size of the GPU memory used by the passed buffer array.
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:325
A map with string keys and VtValue values.
Definition: dictionary.h:63
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.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
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.
virtual HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const
Returns the buffer specs from a given buffer array.
HDST_API void CopyData(HdBufferSourceSharedPtr const &bufferSource) override
Copy source data into buffer.
A central registry of all GPU resources.
size_t GetRangeCount() const
How many ranges are attached to the buffer array.
Definition: bufferArray.h:138
HDST_API void SetBufferArray(HdBufferArray *bufferArray) override
Sets the buffer array associated with this buffer;.
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
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.
virtual HDST_API HdAggregationStrategy::AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
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.
int GetCapacity() const
Returns the capacity of allocated area for this range.
HDST_API ~_SimpleBufferArray() override
Destructor. It invalidates _range.
size_t GetVersion() const
Returns the version of this buffer array.
Definition: bufferArray.h:104
HDST_API HdStBufferResourceSharedPtr GetResource() const
TODO: We need to distinguish between the primvar types here, we should tag each HdBufferSource and Hd...
bool Resize(int numElements) override
Resize memory area for this range.
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.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:168
Simple buffer array (non-aggregated).
HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
Specialized buffer array range for SimpleBufferArray.