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/imaging/hgi/buffer.h"
38 #include "pxr/base/tf/mallocTag.h"
39 #include "pxr/base/tf/token.h"
40 
41 #include <memory>
42 #include <list>
43 #include <unordered_map>
44 
46 
48 struct HgiBufferCpuToGpuOp;
49 
50 /// \class HdStInterleavedMemoryManager
51 ///
52 /// Interleaved memory manager (base class).
53 ///
55 public:
56  /// Copy new data from CPU into staging buffer.
57  /// This reduces the amount of GPU copy commands we emit by first writing
58  /// to the CPU staging area of the buffer and only flushing it to the GPU
59  /// when we write to a non-consecutive area of a buffer.
60  void StageBufferCopy(HgiBufferCpuToGpuOp const& copyOp);
61 
62  /// Flush the staging buffer to GPU.
63  /// Copy the new buffer data from staging area to GPU.
64  void Flush() override;
65 
66 protected:
68 
69  // BufferFlushListEntry lets use accumulate writes into the same GPU buffer
70  // into CPU staging buffers before flushing to GPU.
72  public:
74  HgiBufferHandle const& buf, uint64_t start, uint64_t end);
75 
77  uint64_t start;
78  uint64_t end;
79  };
80 
81  using _BufferFlushMap =
82  std::unordered_map<class HgiBuffer*, _BufferFlushListEntry>;
83 
84  /// specialized buffer array range
86  {
87  public:
88  /// Constructor.
90  : HdStBufferArrayRange(resourceRegistry)
91  , _stripedBuffer(nullptr)
92  , _index(NOT_ALLOCATED)
93  , _numElements(1) {}
94 
95  /// Destructor.
96  HDST_API
98 
99  /// Returns true if this range is valid
100  bool IsValid() const override {
101  // note: a range is valid even its index is NOT_ALLOCATED.
102  return (bool)_stripedBuffer;
103  }
104 
105  /// Returns true is the range has been assigned to a buffer
106  HDST_API
107  bool IsAssigned() const override;
108 
109  /// Returns true if this range is marked as immutable.
110  bool IsImmutable() const override;
111 
112  /// Resize memory area for this range. Returns true if it causes container
113  /// buffer reallocation.
114  HDST_API
115  bool Resize(int numElements) override;
116 
117  /// Copy source data into buffer
118  HDST_API
119  void CopyData(HdBufferSourceSharedPtr const &bufferSource) override;
120 
121  /// Read back the buffer content
122  HDST_API
123  VtValue ReadData(TfToken const &name) const override;
124 
125  /// Returns the offset at which this range begins in the underlying
126  /// buffer array in terms of elements.
127  int GetElementOffset() const override {
128  return _index;
129  }
130 
131  /// Returns the byte offset at which this range begins in the underlying
132  /// buffer array for the given resource.
133  int GetByteOffset(TfToken const& resourceName) const override {
134  TF_UNUSED(resourceName);
135  if (!TF_VERIFY(_stripedBuffer) ||
136  !TF_VERIFY(_index != NOT_ALLOCATED)) return 0;
137  return _stripedBuffer->GetStride() * _index;
138  }
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 _stripedBuffer->GetVersion();
148  }
149 
150  /// Increment the version of the buffer array.
151  void IncrementVersion() override {
152  _stripedBuffer->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.
185  void SetIndex(int index) {
186  _index = index;
187  }
188 
189  /// Make this range invalid
190  void Invalidate() {
191  _stripedBuffer = nullptr;
192  }
193 
194  protected:
195  /// Returns the aggregation container
196  HDST_API
197  const void *_GetAggregation() const override;
198 
199  private:
200  enum { NOT_ALLOCATED = -1 };
201  _StripedInterleavedBuffer *_stripedBuffer;
202  int _index;
203  size_t _numElements;
204  };
205 
207  std::shared_ptr<_StripedInterleavedBuffer>;
209  std::shared_ptr<_StripedInterleavedBufferRange>;
211  std::weak_ptr<_StripedInterleavedBufferRange>;
212 
213  /// striped buffer
215  public:
216  /// Constructor.
217  HDST_API
219  HdStResourceRegistry* resourceRegistry,
220  TfToken const &role,
221  HdBufferSpecVector const &bufferSpecs,
222  HdBufferArrayUsageHint usageHint,
223  int bufferOffsetAlignment,
224  int structAlignment,
225  size_t maxSize,
226  TfToken const &garbageCollectionPerfToken);
227 
228  /// Destructor. It invalidates _rangeList
229  HDST_API
230  virtual ~_StripedInterleavedBuffer();
231 
232  /// perform compaction if necessary, returns true if it becomes empty.
233  HDST_API
234  virtual bool GarbageCollect();
235 
236  /// Debug output
237  HDST_API
238  virtual void DebugDump(std::ostream &out) const;
239 
240  /// Performs reallocation.
241  /// GLX context has to be set when calling this function.
242  HDST_API
243  virtual void Reallocate(
244  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
245  HdBufferArraySharedPtr const &curRangeOwner);
246 
247  /// Mark to perform reallocation on Reallocate()
249  _needsReallocation = true;
250  }
251 
252  /// Mark to perform compaction on GarbageCollect()
254  _needsCompaction = true;
255  }
256 
257  /// Returns the stride.
258  int GetStride() const {
259  return _stride;
260  }
261 
262  /// TODO: We need to distinguish between the primvar types here, we should
263  /// tag each HdBufferSource and HdBufferResource with Constant, Uniform,
264  /// Varying, Vertex, or FaceVarying and provide accessors for the specific
265  /// buffer types.
266 
267  /// Returns the GPU resource. If the buffer array contains more than one
268  /// resource, this method raises a coding error.
269  HDST_API
271 
272  /// Returns the named GPU resource. This method returns the first found
273  /// resource. In HD_SAFE_MODE it checks all underlying GL buffers
274  /// in _resourceMap and raises a coding error if there are more than
275  /// one GL buffers exist.
276  HDST_API
278 
279  /// Returns the list of all named GPU resources for this bufferArray.
280  HdStBufferResourceNamedList const& GetResources() const {return _resourceList;}
281 
282  /// Reconstructs the bufferspecs and returns it (for buffer splitting)
283  HDST_API
285 
287  GetManager() const {
288  return _manager;
289  }
290 
291  protected:
292  HDST_API
293  void _DeallocateResources();
294 
295  /// Adds a new, named GPU resource and returns it.
296  HDST_API
298  HdTupleType tupleType,
299  int offset,
300  int stride);
301 
302  private:
304  HdStResourceRegistry* const _resourceRegistry;
305  bool _needsCompaction;
306  int _stride;
307  int _bufferOffsetAlignment; // ranged binding offset alignment
308  size_t _maxSize; // maximum size of single buffer
309 
310  HdStBufferResourceNamedList _resourceList;
311 
312  _StripedInterleavedBufferRangeSharedPtr _GetRangeSharedPtr(size_t idx) const {
313  return std::static_pointer_cast<_StripedInterleavedBufferRange>(GetRange(idx).lock());
314  }
315 
316  };
317 
319  : _resourceRegistry(resourceRegistry) {}
320 
321  /// Factory for creating HdBufferArrayRange
323 
324  /// Returns the buffer specs from a given buffer array
326  HdBufferArraySharedPtr const &bufferArray) const override;
327 
328  /// Returns the size of the GPU memory used by the passed buffer array
329  size_t GetResourceAllocation(
330  HdBufferArraySharedPtr const &bufferArray,
331  VtDictionary &result) const override;
332 
335 };
336 
338 public:
340  : HdStInterleavedMemoryManager(resourceRegistry) {}
341 
342  /// Factory for creating HdBufferArray managed by
343  /// HdStVBOMemoryManager aggregation.
344  HDST_API
346  TfToken const &role,
347  HdBufferSpecVector const &bufferSpecs,
348  HdBufferArrayUsageHint usageHint);
349 
350  /// Returns id for given bufferSpecs to be used for aggregation
351  HDST_API
353  HdBufferSpecVector const &bufferSpecs,
354  HdBufferArrayUsageHint usageHint) const;
355 };
356 
358 public:
360  : HdStInterleavedMemoryManager(resourceRegistry) {}
361 
362  /// Factory for creating HdBufferArray managed by
363  /// HdStVBOMemoryManager aggregation.
364  HDST_API
366  TfToken const &role,
367  HdBufferSpecVector const &bufferSpecs,
368  HdBufferArrayUsageHint usageHint);
369 
370  /// Returns id for given bufferSpecs to be used for aggregation
371  HDST_API
373  HdBufferSpecVector const &bufferSpecs,
374  HdBufferArrayUsageHint usageHint) const;
375 };
376 
378 
379 #endif // PXR_IMAGING_HD_ST_INTERLEAVED_MEMORY_MANAGER_H
HDST_API bool Resize(int numElements) override
HDST_API HdStBufferResourceNamedList const & GetResources() const override
Returns the list of all named GPU resources for this bufferArrayRange.
void SetNeedsCompaction()
Mark to perform compaction on GarbageCollect()
std::weak_ptr< _StripedInterleavedBufferRange > _StripedInterleavedBufferRangePtr
size_t GetVersion() const override
Returns the version of the buffer array.
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
bool _needsReallocation
Definition: bufferArray.h:167
HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
HDST_API ~_StripedInterleavedBufferRange() override
Destructor.
std::shared_ptr< class HdBufferArray > HdBufferArraySharedPtr
Definition: bufferArray.h:44
HgiBufferHandle buffer
GLuint start
Definition: glcorearb.h:474
HDST_API size_t GetMaxNumElements() const override
Returns the max number of elements.
HdStInterleavedMemoryManager(HdStResourceRegistry *resourceRegistry)
size_t AggregationId
Aggregation ID.
Definition: strategyBase.h:51
std::vector< std::pair< TfToken, HdStBufferResourceSharedPtr > > HdStBufferResourceNamedList
bool IsImmutable() const override
Returns true if this range is marked as immutable.
HdStInterleavedSSBOMemoryManager(HdStResourceRegistry *resourceRegistry)
virtual HDST_API ~_StripedInterleavedBuffer()
Destructor. It invalidates _rangeList.
virtual HDST_API bool GarbageCollect()
perform compaction if necessary, returns true if it becomes empty.
HdStBufferResourceNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
GLuint const GLchar * name
Definition: glcorearb.h:785
HdStInterleavedUBOMemoryManager(HdStResourceRegistry *resourceRegistry)
std::unordered_map< class HgiBuffer *, _BufferFlushListEntry > _BufferFlushMap
virtual HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner)
HDST_API void SetBufferArray(HdBufferArray *bufferArray) override
Sets the buffer array associated with this buffer;.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
HDST_API void DebugDump(std::ostream &out) const override
Debug dump.
Definition: token.h:87
GLuint64EXT * result
Definition: glew.h:14311
_BufferFlushListEntry(HgiBufferHandle const &buf, uint64_t start, uint64_t end)
virtual HDST_API HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
bool IsValid() const override
Returns true if this range is valid.
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.
_StripedInterleavedBufferRange(HdStResourceRegistry *resourceRegistry)
Constructor.
std::shared_ptr< _StripedInterleavedBufferRange > _StripedInterleavedBufferRangeSharedPtr
GLuint GLuint end
Definition: glcorearb.h:474
HDST_API VtValue ReadData(TfToken const &name) const override
Read back the buffer content.
HDST_API bool IsAssigned() const override
Returns true is the range has been assigned to a buffer.
HdBufferArrayRangeSharedPtr CreateBufferArrayRange() override
Factory for creating HdBufferArrayRange.
std::vector< struct HdBufferSpec > HdBufferSpecVector
size_t GetResourceAllocation(HdBufferArraySharedPtr const &bufferArray, VtDictionary &result) const override
Returns the size of the GPU memory used by the passed buffer array.
uint64_t end
HDST_API void CopyData(HdBufferSourceSharedPtr const &bufferSource) override
Copy source data into buffer.
std::shared_ptr< class HdStBufferResource > HdStBufferResourceSharedPtr
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2539
HDST_API HdStBufferResourceSharedPtr GetResource() const override
size_t GetVersion() const
Definition: bufferArray.h:104
HDST_API const void * _GetAggregation() const override
Returns the aggregation container.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
virtual HDST_API HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)
#define HDST_API
Definition: api.h:40
HDST_API HdStBufferResourceSharedPtr GetResource() const
GLuint index
Definition: glcorearb.h:785
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
void SetNeedsReallocation()
Mark to perform reallocation on Reallocate()
GLintptr offset
Definition: glcorearb.h:664
uint64_t start
size_t GetNumElements() const override
Returns the number of elements.
virtual HDST_API AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:871
void StageBufferCopy(HgiBufferCpuToGpuOp const &copyOp)
std::shared_ptr< class HdBufferSource > HdBufferSourceSharedPtr
void IncrementVersion() override
Increment the version of the buffer array.
int GetByteOffset(TfToken const &resourceName) const override
HdStResourceRegistry *const _resourceRegistry
Definition: value.h:168
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 AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const
Returns id for given bufferSpecs to be used for aggregation.
HDST_API _StripedInterleavedBuffer(HdStInterleavedMemoryManager *mgr, HdStResourceRegistry *resourceRegistry, TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint, int bufferOffsetAlignment, int structAlignment, size_t maxSize, TfToken const &garbageCollectionPerfToken)
Constructor.
HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const override
Returns the buffer specs from a given buffer array.