HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vboSimpleMemoryManager.h
Go to the documentation of this file.
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"
15 
19 
21 
23 
24 /// \class HdStVBOSimpleMemoryManager
25 ///
26 /// VBO simple memory manager.
27 ///
28 /// This class doesn't perform any aggregation.
29 ///
31 {
32 public:
34  : _resourceRegistry(resourceRegistry) {}
35 
36  /// Factory for creating HdBufferArray managed by
37  /// HdStVBOSimpleMemoryManager.
38  HDST_API
40  TfToken const &role,
41  HdBufferSpecVector const &bufferSpecs,
42  HdBufferArrayUsageHint usageHint);
43 
44  /// Factory for creating HdBufferArrayRange
45  HDST_API
47 
48  /// Returns id for given bufferSpecs to be used for aggregation
49  HDST_API
51  HdBufferSpecVector const &bufferSpecs,
52  HdBufferArrayUsageHint usageHint) const;
53 
54  /// Returns the buffer specs from a given buffer array
55  HDST_API
57  HdBufferArraySharedPtr const &bufferArray) const;
58 
59  /// Returns the size of the GPU memory used by the passed buffer array
60  HDST_API
61  virtual size_t GetResourceAllocation(
62  HdBufferArraySharedPtr const &bufferArray,
63  VtDictionary &result) const;
64 
65 protected:
66  class _SimpleBufferArray;
67 
68  /// \class _SimpleBufferArrayRange
69  ///
70  /// Specialized buffer array range for SimpleBufferArray.
71  ///
73  {
74  public:
75  /// Constructor.
77  : HdStBufferArrayRange(resourceRegistry)
78  , _bufferArray(nullptr)
79  , _numElements(0) {
80  }
81 
82  /// Returns true if this range is valid
83  bool IsValid() const override {
84  return (bool)_bufferArray;
85  }
86 
87  /// Returns true is the range has been assigned to a buffer
88  HDST_API
89  bool IsAssigned() const override;
90 
91  /// Returns true if this range is marked as immutable.
92  bool IsImmutable() const override;
93 
94  /// Returns true if this needs a staging buffer for CPU to GPU copies.
95  bool RequiresStaging() const override;
96 
97  /// Resize memory area for this range. Returns true if it causes container
98  /// buffer reallocation.
99  bool Resize(int numElements) override {
100  _numElements = numElements;
101  return _bufferArray->Resize(numElements);
102  }
103 
104  /// Copy source data into buffer
105  HDST_API
106  void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
107 
108  /// Read back the buffer content
109  HDST_API
110  VtValue ReadData(TfToken const &name) const override;
111 
112  /// Returns the offset at which this range begins in the underlying
113  /// buffer array in terms of elements.
114  int GetElementOffset() const override {
115  return 0;
116  }
117 
118  /// Returns the byte offset at which this range begins in the underlying
119  /// buffer array for the given resource.
120  int GetByteOffset(TfToken const& resourceName) const override {
121  TF_UNUSED(resourceName);
122  return 0;
123  }
124 
125  /// Returns the number of elements allocated
126  size_t GetNumElements() const override {
127  return _numElements;
128  }
129 
130  /// Returns the capacity of allocated area for this range
131  int GetCapacity() const {
132  return _bufferArray->GetCapacity();
133  }
134 
135  /// Returns the version of the buffer array.
136  size_t GetVersion() const override {
137  return _bufferArray->GetVersion();
138  }
139 
140  /// Increment the version of the buffer array.
141  void IncrementVersion() override {
142  _bufferArray->IncrementVersion();
143  }
144 
145  /// Returns the max number of elements
146  HDST_API
147  size_t GetMaxNumElements() const override;
148 
149  /// Returns the usage hint from the underlying buffer array
150  HDST_API
151  HdBufferArrayUsageHint GetUsageHint() const override;
152 
153  /// Returns the GPU resource. If the buffer array contains more than one
154  /// resource, this method raises a coding error.
155  HDST_API
156  HdStBufferResourceSharedPtr GetResource() const override;
157 
158  /// Returns the named GPU resource.
159  HDST_API
161 
162  /// Returns the list of all named GPU resources for this bufferArrayRange.
163  HDST_API
164  HdStBufferResourceNamedList const& GetResources() const override;
165 
166  /// Sets the buffer array associated with this buffer;
167  HDST_API
168  void SetBufferArray(HdBufferArray *bufferArray) override;
169 
170  /// Debug dump
171  HDST_API
172  void DebugDump(std::ostream &out) const override;
173 
174  /// Make this range invalid
175  void Invalidate() {
176  _bufferArray = NULL;
177  }
178 
179  protected:
180  /// Returns the aggregation container
181  HDST_API
182  const void *_GetAggregation() const override;
183 
184  /// Adds a new, named GPU resource and returns it.
185  HDST_API
187  HdTupleType tupleType,
188  int offset,
189  int stride);
190 
191  private:
192  _SimpleBufferArray * _bufferArray;
193  size_t _numElements;
194  };
195 
197  std::shared_ptr<_SimpleBufferArray>;
199  std::shared_ptr<_SimpleBufferArrayRange>;
201  std::weak_ptr<_SimpleBufferArrayRange>;
202 
203  /// \class _SimpleBufferArray
204  ///
205  /// Simple buffer array (non-aggregated).
206  ///
207  class _SimpleBufferArray final : public HdBufferArray
208  {
209  public:
210  /// Constructor.
211  HDST_API
212  _SimpleBufferArray(HdStResourceRegistry* resourceRegistry,
213  TfToken const &role,
214  HdBufferSpecVector const &bufferSpecs,
215  HdBufferArrayUsageHint usageHint);
216 
217  /// Destructor. It invalidates _range
218  HDST_API
219  ~_SimpleBufferArray() override;
220 
221  /// perform compaction if necessary, returns true if it becomes empty.
222  HDST_API
223  bool GarbageCollect() override;
224 
225  /// Debug output
226  HDST_API
227  void DebugDump(std::ostream &out) const override;
228 
229  /// Set to resize buffers. Actual reallocation happens on Reallocate()
230  HDST_API
231  bool Resize(int numElements);
232 
233  /// Performs reallocation.
234  /// GLX context has to be set when calling this function.
235  HDST_API
236  void Reallocate(
237  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
238  HdBufferArraySharedPtr const &curRangeOwner) override;
239 
240  /// Returns the maximum number of elements capacity.
241  HDST_API
242  size_t GetMaxNumElements() const override;
243 
244  /// Returns current capacity. It could be different from numElements.
245  int GetCapacity() const {
246  return _capacity;
247  }
248 
249  /// Returns the GPU resource. If the buffer array contains more
250  /// than one resource, this method raises a coding error.
251  HDST_API
253 
254  /// Returns the named GPU resource. This method returns the first found
255  /// resource. In HD_SAFE_MODE it checks all underlying GL buffers
256  /// in _resourceMap and raises a coding error if there are more than
257  /// one GL buffers exist.
258  HDST_API
260 
261  /// Returns the list of all named GPU resources for this bufferArray.
262  HdStBufferResourceNamedList const& GetResources() const {return _resourceList;}
263 
264  /// Reconstructs the bufferspecs and returns it (for buffer splitting)
265  HDST_API
267 
268  protected:
269  HDST_API
270  void _DeallocateResources();
271 
272  /// Adds a new, named GPU resource and returns it.
273  HDST_API
275  HdTupleType tupleType,
276  int offset,
277  int stride);
278  private:
279  HdStResourceRegistry* const _resourceRegistry;
280  int _capacity;
281  size_t _maxBytesPerElement;
282  HgiBufferUsage _bufferUsage;
283 
284  HdStBufferResourceNamedList _resourceList;
285 
286  _SimpleBufferArrayRangeSharedPtr _GetRangeSharedPtr() const {
287  return GetRangeCount() > 0
288  ? std::static_pointer_cast<_SimpleBufferArrayRange>(GetRange(0).lock())
290  }
291  };
292 
294 };
295 
297 
298 #endif // PXR_IMAGING_HD_ST_VBO_SIMPLE_MEMORY_MANAGER_H
int GetCapacity() const
Returns the capacity of allocated area for this range.
HDST_API size_t GetMaxNumElements() const override
Returns the max number of elements.
std::shared_ptr< class HdBufferArray > HdBufferArraySharedPtr
Definition: bufferArray.h:26
int GetByteOffset(TfToken const &resourceName) const override
HDST_API void CopyData(HdBufferSourceSharedPtr const &bufferSource) override
Copy source data into buffer.
bool IsImmutable() const override
Returns true if this range is marked as immutable.
HDST_API bool IsAssigned() const override
Returns true is the range has been assigned to a buffer.
std::vector< std::pair< TfToken, HdStBufferResourceSharedPtr > > HdStBufferResourceNamedList
HdStResourceRegistry *const _resourceRegistry
**But if you need a result
Definition: thread.h:622
bool RequiresStaging() const override
Returns true if this needs a staging buffer for CPU to GPU copies.
HDST_API void DebugDump(std::ostream &out) const override
Debug output.
int GetCapacity() const
Returns current capacity. It could be different from numElements.
HDST_API ~_SimpleBufferArray() override
Destructor. It invalidates _range.
HDST_API VtValue ReadData(TfToken const &name) const override
Read back the buffer content.
std::weak_ptr< _SimpleBufferArrayRange > _SimpleBufferArrayRangePtr
HDST_API HdStBufferResourceSharedPtr GetResource() const
bool IsValid() const override
Returns true if this range is valid.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
size_t AggregationId
Aggregation ID.
Definition: strategyBase.h:34
Definition: token.h:70
GLintptr offset
Definition: glcorearb.h:665
void IncrementVersion() override
Increment the version of the buffer array.
size_t GetVersion() const override
Returns the version of the buffer array.
HD_API void IncrementVersion()
Increments the version of this buffer array.
HDST_API void SetBufferArray(HdBufferArray *bufferArray) override
Sets the buffer array associated with this buffer;.
virtual HDST_API HdBufferArrayRangeSharedPtr CreateBufferArrayRange()
Factory for creating HdBufferArrayRange.
HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner) override
HDST_API const void * _GetAggregation() const override
Returns the aggregation container.
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:872
HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
HDST_API void DebugDump(std::ostream &out) const override
Debug dump.
virtual HDST_API HdStAggregationStrategy::AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
GLuint const GLchar * name
Definition: glcorearb.h:786
std::vector< struct HdBufferSpec > HdBufferSpecVector
HDST_API size_t GetMaxNumElements() const override
Returns the maximum number of elements capacity.
std::shared_ptr< class HdStBufferResource > HdStBufferResourceSharedPtr
virtual HDST_API HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const
Returns the buffer specs from a given buffer array.
HDST_API bool GarbageCollect() override
perform compaction if necessary, returns true if it becomes empty.
_SimpleBufferArrayRange(HdStResourceRegistry *resourceRegistry)
Constructor.
size_t GetNumElements() const override
Returns the number of elements allocated.
size_t GetVersion() const
Definition: bufferArray.h:84
HDST_API HdStBufferResourceSharedPtr GetResource() const override
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HgiBits HgiBufferUsage
Definition: enums.h:315
#define HDST_API
Definition: api.h:23
HDST_API HdStBufferResourceSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
#define TF_UNUSED(x)
Definition: tf.h:168
HDST_API _SimpleBufferArray(HdStResourceRegistry *resourceRegistry, TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
Constructor.
uint32_t HdBufferArrayUsageHint
Definition: bufferArray.h:60
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:27
HDST_API bool Resize(int numElements)
Set to resize buffers. Actual reallocation happens on Reallocate()
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
HDST_API HdStBufferResourceSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
HDST_API HdStBufferResourceNamedList const & GetResources() const override
Returns the list of all named GPU resources for this bufferArrayRange.
size_t GetRangeCount() const
How many ranges are attached to the buffer array.
Definition: bufferArray.h:118
std::shared_ptr< _SimpleBufferArrayRange > _SimpleBufferArrayRangeSharedPtr
HdStVBOSimpleMemoryManager(HdStResourceRegistry *resourceRegistry)
std::shared_ptr< class HdBufferSource > HdBufferSourceSharedPtr
std::shared_ptr< _SimpleBufferArray > _SimpleBufferArraySharedPtr
Definition: value.h:146
HdStBufferResourceNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
virtual HDST_API 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)