HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vboMemoryManager.h
Go to the documentation of this file.
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"
35 
36 #include "pxr/base/tf/mallocTag.h"
37 #include "pxr/base/tf/token.h"
38 
39 #include <list>
40 #include <memory>
41 
43 
45 
46 /// \class HdStVBOMemoryManager
47 ///
48 /// VBO memory manager.
49 ///
51 {
52 public:
55  , _resourceRegistry(resourceRegistry) {}
56 
57  /// Factory for creating HdBufferArray managed by
58  /// HdStVBOMemoryManager aggregation.
59  HDST_API
61  TfToken const &role,
62  HdBufferSpecVector const &bufferSpecs,
63  HdBufferArrayUsageHint usageHint);
64 
65  /// Factory for creating HdBufferArrayRange managed by
66  /// HdStVBOMemoryManager aggregation.
67  HDST_API
69 
70  /// Returns id for given bufferSpecs to be used for aggregation
71  HDST_API
73  HdBufferSpecVector const &bufferSpecs,
74  HdBufferArrayUsageHint usageHint) const;
75 
76  /// Returns the buffer specs from a given buffer array
78  HdBufferArraySharedPtr const &bufferArray) const;
79 
80  /// Returns the size of the GPU memory used by the passed buffer array
81  virtual size_t GetResourceAllocation(
82  HdBufferArraySharedPtr const &bufferArray,
83  VtDictionary &result) const;
84 
85 protected:
86  class _StripedBufferArray;
87 
88  /// specialized buffer array range
90  {
91  public:
92  /// Constructor.
94  : HdStBufferArrayRange(resourceRegistry),
95  _stripedBufferArray(nullptr),
96  _elementOffset(0),
97  _numElements(0),
98  _capacity(0)
99  {
100  }
101 
102  /// Destructor.
103  HDST_API
104  ~_StripedBufferArrayRange() override;
105 
106  /// Returns true if this range is valid
107  bool IsValid() const override {
108  return (bool)_stripedBufferArray;
109  }
110 
111  /// Returns true is the range has been assigned to a buffer
112  HDST_API
113  bool IsAssigned() const override;
114 
115  /// Returns true if this bar is marked as immutable.
116  bool IsImmutable() const override;
117 
118  /// Resize memory area for this range. Returns true if it causes container
119  /// buffer reallocation.
120  HDST_API
121  bool Resize(int numElements) override;
122 
123  /// Copy source data into buffer
124  HDST_API
125  void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
126 
127  /// Read back the buffer content
128  HDST_API
129  VtValue ReadData(TfToken const &name) const override;
130 
131  /// Returns the relative element offset in aggregated buffer
132  int GetElementOffset() const override {
133  return _elementOffset;
134  }
135 
136  /// Returns the byte offset at which this range begins in the underlying
137  /// buffer array for the given resource.
138  int GetByteOffset(TfToken const& resourceName) const override;
139 
140  /// Returns the number of elements
141  size_t GetNumElements() const override {
142  return _numElements;
143  }
144 
145  /// Returns the version of the buffer array.
146  size_t GetVersion() const override {
147  return _stripedBufferArray->GetVersion();
148  }
149 
150  /// Increment the version of the buffer array.
151  void IncrementVersion() override {
152  _stripedBufferArray->IncrementVersion();
153  }
154 
155  /// Returns the max number of elements
156  HDST_API
157  size_t GetMaxNumElements() const override;
158 
159  /// Returns the usage hint from the underlying buffer array
160  HDST_API
161  HdBufferArrayUsageHint GetUsageHint() const override;
162 
163  /// Returns the GPU resource. If the buffer array contains more than one
164  /// resource, this method raises a coding error.
165  HDST_API
166  HdStBufferResourceSharedPtr GetResource() const override;
167 
168  /// Returns the named GPU resource.
169  HDST_API
171 
172  /// Returns the list of all named GPU resources for this bufferArrayRange.
173  HDST_API
174  HdStBufferResourceNamedList const& GetResources() const override;
175 
176  /// Sets the buffer array associated with this buffer;
177  HDST_API
178  void SetBufferArray(HdBufferArray *bufferArray) override;
179 
180  /// Debug dump
181  HDST_API
182  void DebugDump(std::ostream &out) const override;
183 
184  /// Set the relative offset for this range.
186  _elementOffset = offset;
187  }
188 
189  /// Set the number of elements for this range.
190  void SetNumElements(int numElements) {
191  _numElements = numElements;
192  }
193 
194  /// Returns the capacity of allocated area
195  int GetCapacity() const {
196  return _capacity;
197  }
198 
199  /// Set the capacity of allocated area for this range.
200  void SetCapacity(int capacity) {
201  _capacity = capacity;
202  }
203 
204  /// Make this range invalid
205  void Invalidate() {
206  _stripedBufferArray = NULL;
207  }
208 
209  protected:
210  /// Returns the aggregation container
211  HDST_API
212  const void *_GetAggregation() const override;
213 
214  private:
215  // Returns the byte offset at which the BAR begins for the resource.
216  size_t _GetByteOffset(HdStBufferResourceSharedPtr const& resource)
217  const;
218 
219  // holding a weak reference to container.
220  // this pointer becomes null when the StripedBufferArray gets destructed,
221  // in case if any drawItem still holds this bufferRange.
222  _StripedBufferArray *_stripedBufferArray;
223  int _elementOffset;
224  size_t _numElements;
225  int _capacity;
226  };
227 
229  std::shared_ptr<_StripedBufferArray>;
231  std::shared_ptr<_StripedBufferArrayRange>;
233  std::weak_ptr<_StripedBufferArrayRange>;
234 
235  /// striped buffer array
237  {
238  public:
239  /// Constructor.
240  HDST_API
241  _StripedBufferArray(HdStResourceRegistry* resourceRegistry,
242  TfToken const &role,
243  HdBufferSpecVector const &bufferSpecs,
244  HdBufferArrayUsageHint usageHint);
245 
246  /// Destructor. It invalidates _rangeList
247  HDST_API
248  ~_StripedBufferArray() override;
249 
250  /// perform compaction if necessary. If it becomes empty, release all
251  /// resources and returns true
252  HDST_API
253  bool GarbageCollect() override;
254 
255  /// Debug output
256  HDST_API
257  void DebugDump(std::ostream &out) const override;
258 
259  /// Performs reallocation.
260  /// GLX context has to be set when calling this function.
261  HDST_API
262  void Reallocate(
263  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
264  HdBufferArraySharedPtr const &curRangeOwner) override;
265 
266  /// Returns the maximum number of elements capacity.
267  HDST_API
268  size_t GetMaxNumElements() const override;
269 
270  /// Mark to perform reallocation on Reallocate()
272  _needsReallocation = true;
273  }
274 
275  /// Mark to perform compaction on GarbageCollect()
277  _needsCompaction = true;
278  }
279 
280  /// TODO: We need to distinguish between the primvar types here, we should
281  /// tag each HdBufferSource and HdBufferResource with Constant, Uniform,
282  /// Varying, Vertex, or FaceVarying and provide accessors for the specific
283  /// buffer types.
284 
285  /// Returns the GPU resource. If the buffer array contains more than one
286  /// resource, this method raises a coding error.
287  HDST_API
289 
290  /// Returns the named GPU resource. This method returns the first found
291  /// resource. In HD_SAFE_MODE it checks all underlying GL buffers
292  /// in _resourceMap and raises a coding error if there are more than
293  /// one GL buffers exist.
294  HDST_API
296 
297  /// Returns the list of all named GPU resources for this bufferArray.
299  {return _resourceList;}
300 
301  /// Reconstructs the bufferspecs and returns it (for buffer splitting)
302  HDST_API
304 
305  protected:
306  HDST_API
307  void _DeallocateResources();
308 
309  /// Adds a new, named GPU resource and returns it.
310  HDST_API
312  HdTupleType tupleType,
313  int offset,
314  int stride);
315 
316  private:
317 
318  HdStResourceRegistry* _resourceRegistry;
319  bool _needsCompaction;
320  int _totalCapacity;
321  size_t _maxBytesPerElement;
322 
323  HdStBufferResourceNamedList _resourceList;
324 
325  // Helper routine to cast the range shared pointer.
326  _StripedBufferArrayRangeSharedPtr _GetRangeSharedPtr(size_t idx) const {
327  return std::static_pointer_cast<_StripedBufferArrayRange>(GetRange(idx).lock());
328  }
329  };
330 
332 };
333 
335 
336 #endif // PXR_IMAGING_HD_ST_VBO_MEMORY_MANAGER_H
int GetByteOffset(TfToken const &resourceName) const override
HDST_API const void * _GetAggregation() const override
Returns the aggregation container.
bool IsImmutable() const override
Returns true if this bar is marked as immutable.
bool _needsReallocation
Definition: bufferArray.h:167
bool IsValid() const override
Returns true if this range is valid.
std::shared_ptr< class HdBufferArray > HdBufferArraySharedPtr
Definition: bufferArray.h:44
void SetNeedsCompaction()
Mark to perform compaction on GarbageCollect()
_StripedBufferArrayRange(HdStResourceRegistry *resourceRegistry)
Constructor.
HDST_API void CopyData(HdBufferSourceSharedPtr const &bufferSource) override
Copy source data into buffer.
virtual HDST_API HdBufferArrayRangeSharedPtr CreateBufferArrayRange()
size_t AggregationId
Aggregation ID.
Definition: strategyBase.h:51
std::vector< std::pair< TfToken, HdStBufferResourceSharedPtr > > HdStBufferResourceNamedList
HDST_API VtValue ReadData(TfToken const &name) const override
Read back the buffer content.
void IncrementVersion() override
Increment the version of the buffer array.
HDST_API bool GarbageCollect() override
HDST_API ~_StripedBufferArray() override
Destructor. It invalidates _rangeList.
HDST_API ~_StripedBufferArrayRange() override
Destructor.
GLuint const GLchar * name
Definition: glcorearb.h:785
HDST_API bool Resize(int numElements) override
HDST_API size_t GetMaxNumElements() const override
Returns the maximum number of elements capacity.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
HdStBufferResourceNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
Definition: token.h:87
GLuint64EXT * result
Definition: glew.h:14311
HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner) override
HD_API void IncrementVersion()
Increments the version of this buffer array.
HdStVBOMemoryManager(HdStResourceRegistry *resourceRegistry)
virtual size_t GetResourceAllocation(HdBufferArraySharedPtr const &bufferArray, VtDictionary &result) const
Returns the size of the GPU memory used by the passed buffer array.
int GetElementOffset() const override
Returns the relative element offset in aggregated buffer.
size_t GetNumElements() const override
Returns the number of elements.
std::weak_ptr< _StripedBufferArrayRange > _StripedBufferArrayRangePtr
HDST_API bool IsAssigned() const override
Returns true is the range has been assigned to a buffer.
HDST_API _StripedBufferArray(HdStResourceRegistry *resourceRegistry, TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Constructor.
HDST_API void DebugDump(std::ostream &out) const override
Debug output.
HDST_API HdStBufferResourceSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
void SetElementOffset(int offset)
Set the relative offset for this range.
std::shared_ptr< _StripedBufferArray > _StripedBufferArraySharedPtr
std::vector< struct HdBufferSpec > HdBufferSpecVector
int GetCapacity() const
Returns the capacity of allocated area.
std::shared_ptr< class HdStBufferResource > HdStBufferResourceSharedPtr
HDST_API HdStBufferResourceNamedList const & GetResources() const override
Returns the list of all named GPU resources for this bufferArrayRange.
size_t GetVersion() const
Definition: bufferArray.h:104
HDST_API void DebugDump(std::ostream &out) const override
Debug dump.
HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
void Invalidate()
Make this range invalid.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
void SetNumElements(int numElements)
Set the number of elements for this range.
#define HDST_API
Definition: api.h:40
size_t GetVersion() const override
Returns the version of the buffer array.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:45
void SetNeedsReallocation()
Mark to perform reallocation on Reallocate()
HDST_API size_t GetMaxNumElements() const override
Returns the max number of elements.
HDST_API HdStBufferResourceSharedPtr GetResource() const
virtual HDST_API AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
HDST_API void SetBufferArray(HdBufferArray *bufferArray) override
Sets the buffer array associated with this buffer;.
GLintptr offset
Definition: glcorearb.h:664
void SetCapacity(int capacity)
Set the capacity of allocated area for this range.
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:871
std::shared_ptr< class HdBufferSource > HdBufferSourceSharedPtr
HDST_API HdStBufferResourceSharedPtr GetResource() const override
HdStResourceRegistry * _resourceRegistry
std::shared_ptr< _StripedBufferArrayRange > _StripedBufferArrayRangeSharedPtr
virtual HDST_API HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
virtual HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const
Returns the buffer specs from a given buffer array.
Definition: value.h:168