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