HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
interleavedMemoryManager.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_INTERLEAVED_MEMORY_MANAGER_H
25 #define PXR_IMAGING_HD_ST_INTERLEAVED_MEMORY_MANAGER_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
35 #include "pxr/imaging/hd/tokens.h"
36 #include "pxr/imaging/hd/version.h"
37 #include "pxr/base/tf/mallocTag.h"
38 #include "pxr/base/tf/token.h"
39 
40 #include <memory>
41 #include <list>
42 
44 
46 class Hgi;
47 
48 /// \class HdStInterleavedMemoryManager
49 ///
50 /// Interleaved memory manager (base class).
51 ///
53 protected:
55 
56  /// specialized buffer array range
58  public:
59  /// Constructor.
61  _stripedBuffer(nullptr), _index(NOT_ALLOCATED), _numElements(1) {
62  }
63 
64  /// Destructor.
65  HDST_API
67 
68  /// Returns true if this range is valid
69  virtual bool IsValid() const {
70  // note: a range is valid even its index is NOT_ALLOCATED.
71  return (bool)_stripedBuffer;
72  }
73 
74  /// Returns true is the range has been assigned to a buffer
75  HDST_API
76  virtual bool IsAssigned() const;
77 
78  /// Returns true if this range is marked as immutable.
79  virtual bool IsImmutable() const;
80 
81  /// Resize memory area for this range. Returns true if it causes container
82  /// buffer reallocation.
83  HDST_API
84  virtual bool Resize(int numElements);
85 
86  /// Copy source data into buffer
87  HDST_API
88  virtual void CopyData(HdBufferSourceSharedPtr const &bufferSource);
89 
90  /// Read back the buffer content
91  HDST_API
92  virtual VtValue ReadData(TfToken const &name) const;
93 
94  /// Returns the offset at which this range begins in the underlying
95  /// buffer array in terms of elements.
96  virtual int GetElementOffset() const {
97  return _index;
98  }
99 
100  /// Returns the byte offset at which this range begins in the underlying
101  /// buffer array for the given resource.
102  virtual int GetByteOffset(TfToken const& resourceName) const {
103  TF_UNUSED(resourceName);
104  if (!TF_VERIFY(_stripedBuffer) ||
105  !TF_VERIFY(_index != NOT_ALLOCATED)) return 0;
106  return _stripedBuffer->GetStride() * _index;
107  }
108 
109  /// Returns the number of elements
110  virtual size_t GetNumElements() const {
111  return _numElements;
112  }
113 
114  /// Returns the version of the buffer array.
115  virtual size_t GetVersion() const {
116  return _stripedBuffer->GetVersion();
117  }
118 
119  /// Increment the version of the buffer array.
120  virtual void IncrementVersion() {
121  _stripedBuffer->IncrementVersion();
122  }
123 
124  /// Returns the max number of elements
125  HDST_API
126  virtual size_t GetMaxNumElements() const;
127 
128  /// Returns the usage hint from the underlying buffer array
129  HDST_API
130  virtual HdBufferArrayUsageHint GetUsageHint() const override;
131 
132  /// Returns the GPU resource. If the buffer array contains more than one
133  /// resource, this method raises a coding error.
134  HDST_API
136 
137  /// Returns the named GPU resource.
138  HDST_API
140 
141  /// Returns the list of all named GPU resources for this bufferArrayRange.
142  HDST_API
143  virtual HdStBufferResourceGLNamedList const& GetResources() const;
144 
145  /// Sets the buffer array associated with this buffer;
146  HDST_API
147  virtual void SetBufferArray(HdBufferArray *bufferArray);
148 
149  /// Debug dump
150  HDST_API
151  virtual void DebugDump(std::ostream &out) const;
152 
153  /// Set the relative offset for this range.
154  void SetIndex(int index) {
155  _index = index;
156  }
157 
158  /// Make this range invalid
159  void Invalidate() {
160  _stripedBuffer = nullptr;
161  }
162 
163  protected:
164  /// Returns the aggregation container
165  HDST_API
166  virtual const void *_GetAggregation() const;
167 
168  private:
169  enum { NOT_ALLOCATED = -1 };
170  _StripedInterleavedBuffer *_stripedBuffer;
171  int _index;
172  size_t _numElements;
173  };
174 
176  std::shared_ptr<_StripedInterleavedBuffer>;
178  std::shared_ptr<_StripedInterleavedBufferRange>;
180  std::weak_ptr<_StripedInterleavedBufferRange>;
181 
182  /// striped buffer
184  public:
185  /// Constructor.
186  HDST_API
188  TfToken const &role,
189  HdBufferSpecVector const &bufferSpecs,
190  HdBufferArrayUsageHint usageHint,
191  int bufferOffsetAlignment,
192  int structAlignment,
193  size_t maxSize,
194  TfToken const &garbageCollectionPerfToken);
195 
196  /// Destructor. It invalidates _rangeList
197  HDST_API
198  virtual ~_StripedInterleavedBuffer();
199 
200  /// perform compaction if necessary, returns true if it becomes empty.
201  HDST_API
202  virtual bool GarbageCollect();
203 
204  /// Debug output
205  HDST_API
206  virtual void DebugDump(std::ostream &out) const;
207 
208  /// Performs reallocation.
209  /// GLX context has to be set when calling this function.
210  HDST_API
211  virtual void Reallocate(
212  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
213  HdBufferArraySharedPtr const &curRangeOwner);
214 
215  /// Mark to perform reallocation on Reallocate()
217  _needsReallocation = true;
218  }
219 
220  /// Mark to perform compaction on GarbageCollect()
222  _needsCompaction = true;
223  }
224 
225  /// Returns the stride.
226  int GetStride() const {
227  return _stride;
228  }
229 
230  /// TODO: We need to distinguish between the primvar types here, we should
231  /// tag each HdBufferSource and HdBufferResource with Constant, Uniform,
232  /// Varying, Vertex, or FaceVarying and provide accessors for the specific
233  /// buffer types.
234 
235  /// Returns the GPU resource. If the buffer array contains more than one
236  /// resource, this method raises a coding error.
237  HDST_API
239 
240  /// Returns the named GPU resource. This method returns the first found
241  /// resource. In HD_SAFE_MODE it checks all underlying GL buffers
242  /// in _resourceMap and raises a coding error if there are more than
243  /// one GL buffers exist.
244  HDST_API
246 
247  /// Returns the list of all named GPU resources for this bufferArray.
248  HdStBufferResourceGLNamedList const& GetResources() const {return _resourceList;}
249 
250  /// Reconstructs the bufferspecs and returns it (for buffer splitting)
251  HDST_API
253 
254  protected:
255  HDST_API
256  void _DeallocateResources();
257 
258  /// Adds a new, named GPU resource and returns it.
259  HDST_API
261  HdTupleType tupleType,
262  int offset,
263  int stride);
264 
265  private:
266  Hgi* _hgi;
267  bool _needsCompaction;
268  int _stride;
269  int _bufferOffsetAlignment; // ranged binding offset alignment
270  size_t _maxSize; // maximum size of single buffer
271 
272  HdStBufferResourceGLNamedList _resourceList;
273 
274  _StripedInterleavedBufferRangeSharedPtr _GetRangeSharedPtr(size_t idx) const {
275  return std::static_pointer_cast<_StripedInterleavedBufferRange>(GetRange(idx).lock());
276  }
277 
278  };
279 
281 
282  /// Factory for creating HdBufferArrayRange
284 
285  /// Returns the buffer specs from a given buffer array
287  HdBufferArraySharedPtr const &bufferArray) const;
288 
289  /// Returns the size of the GPU memory used by the passed buffer array
290  virtual size_t GetResourceAllocation(
291  HdBufferArraySharedPtr const &bufferArray,
292  VtDictionary &result) const;
293 
295 };
296 
298 public:
301 
302  /// Factory for creating HdBufferArray managed by
303  /// HdStVBOMemoryManager aggregation.
304  HDST_API
306  TfToken const &role,
307  HdBufferSpecVector const &bufferSpecs,
308  HdBufferArrayUsageHint usageHint);
309 
310  /// Returns id for given bufferSpecs to be used for aggregation
311  HDST_API
313  HdBufferSpecVector const &bufferSpecs,
314  HdBufferArrayUsageHint usageHint) const;
315 };
316 
318 public:
321 
322  /// Factory for creating HdBufferArray managed by
323  /// HdStVBOMemoryManager aggregation.
324  HDST_API
326  TfToken const &role,
327  HdBufferSpecVector const &bufferSpecs,
328  HdBufferArrayUsageHint usageHint);
329 
330  /// Returns id for given bufferSpecs to be used for aggregation
331  HDST_API
333  HdBufferSpecVector const &bufferSpecs,
334  HdBufferArrayUsageHint usageHint) const;
335 };
336 
338 
339 #endif // PXR_IMAGING_HD_ST_INTERLEAVED_MEMORY_MANAGER_H
virtual size_t GetNumElements() const
Returns the number of elements.
void SetNeedsCompaction()
Mark to perform compaction on GarbageCollect()
std::weak_ptr< _StripedInterleavedBufferRange > _StripedInterleavedBufferRangePtr
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
GLuint const GLchar * name
Definition: glew.h:1814
bool _needsReallocation
Definition: bufferArray.h:167
std::shared_ptr< class HdBufferArray > HdBufferArraySharedPtr
Definition: bufferArray.h:44
virtual HDST_API void SetBufferArray(HdBufferArray *bufferArray)
Sets the buffer array associated with this buffer;.
std::shared_ptr< class HdStBufferResourceGL > HdStBufferResourceGLSharedPtr
GLuint index
Definition: glew.h:1814
HdStBufferResourceGLNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
size_t AggregationId
Aggregation ID.
Definition: strategyBase.h:51
HDST_API _StripedInterleavedBuffer(Hgi *hgi, TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint, int bufferOffsetAlignment, int structAlignment, size_t maxSize, TfToken const &garbageCollectionPerfToken)
Constructor.
virtual HDST_API ~_StripedInterleavedBuffer()
Destructor. It invalidates _rangeList.
virtual HDST_API bool IsAssigned() const
Returns true is the range has been assigned to a buffer.
virtual HDST_API bool GarbageCollect()
perform compaction if necessary, returns true if it becomes empty.
virtual HdBufferArrayRangeSharedPtr CreateBufferArrayRange()
Factory for creating HdBufferArrayRange.
virtual HDST_API ~_StripedInterleavedBufferRange()
Destructor.
virtual HDST_API void DebugDump(std::ostream &out) const
Debug dump.
virtual bool IsImmutable() const
Returns true if this range is marked as immutable.
std::vector< std::pair< TfToken, HdStBufferResourceGLSharedPtr > > HdStBufferResourceGLNamedList
virtual HDST_API HdStBufferResourceGLNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArrayRange.
virtual HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner)
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
Definition: token.h:87
virtual HDST_API HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
HD_API void IncrementVersion()
Increments the version of this buffer array.
std::shared_ptr< _StripedInterleavedBuffer > _StripedInterleavedBufferSharedPtr
virtual HDST_API void DebugDump(std::ostream &out) const
Debug output.
std::shared_ptr< _StripedInterleavedBufferRange > _StripedInterleavedBufferRangeSharedPtr
HDST_API HdStBufferResourceGLSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
std::vector< struct HdBufferSpec > HdBufferSpecVector
virtual int GetByteOffset(TfToken const &resourceName) const
GLsizei stride
Definition: glew.h:1523
Definition: hgi.h:106
virtual HDST_API size_t GetMaxNumElements() const
Returns the max number of elements.
size_t GetVersion() const
Definition: bufferArray.h:104
virtual HDST_API void CopyData(HdBufferSourceSharedPtr const &bufferSource)
Copy source data into buffer.
virtual size_t GetVersion() const
Returns the version of the buffer array.
virtual HDST_API HdStBufferResourceGLSharedPtr GetResource() const
virtual void IncrementVersion()
Increment the version of the buffer array.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
virtual HDST_API HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
virtual bool IsValid() const
Returns true if this range is valid.
virtual HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const
Returns the buffer specs from a given buffer array.
#define HDST_API
Definition: api.h:40
void SetIndex(int index)
Set the relative offset for this range.
#define TF_UNUSED(x)
Definition: tf.h:185
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:45
virtual HDST_API VtValue ReadData(TfToken const &name) const
Read back the buffer content.
HDST_API HdStBufferResourceGLSharedPtr GetResource() const
GLuint64EXT * result
Definition: glew.h:14007
void SetNeedsReallocation()
Mark to perform reallocation on Reallocate()
virtual HDST_API AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
virtual size_t GetResourceAllocation(HdBufferArraySharedPtr const &bufferArray, VtDictionary &result) const
Returns the size of the GPU memory used by the passed buffer array.
std::shared_ptr< class HdBufferSource > HdBufferSourceSharedPtr
virtual HDST_API const void * _GetAggregation() const
Returns the aggregation container.
Definition: value.h:174
virtual HDST_API AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
virtual HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
GLintptr offset
Definition: glew.h:1682