All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
interleavedMemoryManager.h
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 HDST_INTERLEAVED_VBO_MEMORY_MANAGER_H
25 #define HDST_INTERLEAVED_VBO_MEMORY_MANAGER_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
29 #include "pxr/imaging/hdSt/bufferArrayRangeGL.h"
30 #include "pxr/imaging/hd/bufferArray.h"
31 #include "pxr/imaging/hd/bufferSpec.h"
32 #include "pxr/imaging/hd/bufferSource.h"
33 #include "pxr/imaging/hd/resource.h"
34 #include "pxr/imaging/hd/strategyBase.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 <boost/shared_ptr.hpp>
41 #include <list>
42 
43 PXR_NAMESPACE_OPEN_SCOPE
44 
45 
51 protected:
53 
56  public:
59  _stripedBuffer(nullptr), _index(NOT_ALLOCATED), _numElements(1) {
60  }
61 
63  HDST_API
65 
67  virtual bool IsValid() const {
68  // note: a range is valid even its index is NOT_ALLOCATED.
69  return (bool)_stripedBuffer;
70  }
71 
73  HDST_API
74  virtual bool IsAssigned() const;
75 
77  virtual bool IsImmutable() const;
78 
81  HDST_API
82  virtual bool Resize(int numElements);
83 
85  HDST_API
86  virtual void CopyData(HdBufferSourceSharedPtr const &bufferSource);
87 
89  HDST_API
90  virtual VtValue ReadData(TfToken const &name) const;
91 
93  virtual int GetOffset() const {
94  if (!TF_VERIFY(_stripedBuffer) ||
95  !TF_VERIFY(_index != NOT_ALLOCATED)) return 0;
96  return _stripedBuffer->GetStride() * _index;
97  }
98 
100  virtual int GetIndex() const {
101  return _index;
102  }
103 
105  virtual size_t GetNumElements() const {
106  return _numElements;
107  }
108 
110  virtual size_t GetVersion() const {
111  return _stripedBuffer->GetVersion();
112  }
113 
115  virtual void IncrementVersion() {
116  _stripedBuffer->IncrementVersion();
117  }
118 
120  HDST_API
121  virtual size_t GetMaxNumElements() const;
122 
124  HDST_API
125  virtual HdBufferArrayUsageHint GetUsageHint() const override;
126 
129  HDST_API
130  virtual HdStBufferResourceGLSharedPtr GetResource() const;
131 
133  HDST_API
134  virtual HdStBufferResourceGLSharedPtr GetResource(TfToken const& name);
135 
137  HDST_API
138  virtual HdStBufferResourceGLNamedList const& GetResources() const;
139 
141  HDST_API
142  virtual void SetBufferArray(HdBufferArray *bufferArray);
143 
145  HDST_API
146  virtual void DebugDump(std::ostream &out) const;
147 
149  void SetIndex(int index) {
150  _index = index;
151  }
152 
154  void Invalidate() {
155  _stripedBuffer = nullptr;
156  }
157 
158  protected:
160  HDST_API
161  virtual const void *_GetAggregation() const;
162 
163  private:
164  enum { NOT_ALLOCATED = -1 };
165  _StripedInterleavedBuffer *_stripedBuffer;
166  int _index;
167  size_t _numElements;
168  };
169 
170  typedef boost::shared_ptr<_StripedInterleavedBuffer>
171  _StripedInterleavedBufferSharedPtr;
172  typedef boost::shared_ptr<_StripedInterleavedBufferRange>
173  _StripedInterleavedBufferRangeSharedPtr;
174  typedef boost::weak_ptr<_StripedInterleavedBufferRange>
175  _StripedInterleavedBufferRangePtr;
176 
179  public:
181  HDST_API
183  HdBufferSpecVector const &bufferSpecs,
184  HdBufferArrayUsageHint usageHint,
185  int bufferOffsetAlignment,
186  int structAlignment,
187  size_t maxSize,
188  TfToken const &garbageCollectionPerfToken);
189 
191  HDST_API
192  virtual ~_StripedInterleavedBuffer();
193 
195  HDST_API
196  virtual bool GarbageCollect();
197 
199  HDST_API
200  virtual void DebugDump(std::ostream &out) const;
201 
204  HDST_API
205  virtual void Reallocate(
206  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
207  HdBufferArraySharedPtr const &curRangeOwner);
208 
211  _needsReallocation = true;
212  }
213 
216  _needsCompaction = true;
217  }
218 
220  int GetStride() const {
221  return _stride;
222  }
223 
228 
231  HDST_API
232  HdStBufferResourceGLSharedPtr GetResource() const;
233 
238  HDST_API
239  HdStBufferResourceGLSharedPtr GetResource(TfToken const& name);
240 
242  HdStBufferResourceGLNamedList const& GetResources() const {return _resourceList;}
243 
245  HDST_API
246  HdBufferSpecVector GetBufferSpecs() const;
247 
248  protected:
249  HDST_API
250  void _DeallocateResources();
251 
253  HDST_API
254  HdStBufferResourceGLSharedPtr _AddResource(TfToken const& name,
255  HdTupleType tupleType,
256  int offset,
257  int stride);
258 
259  private:
260  bool _needsCompaction;
261  int _stride;
262  int _bufferOffsetAlignment; // ranged binding offset alignment
263  size_t _maxSize; // maximum size of single buffer
264 
265  HdStBufferResourceGLNamedList _resourceList;
266 
267  _StripedInterleavedBufferRangeSharedPtr _GetRangeSharedPtr(size_t idx) const {
268  return boost::static_pointer_cast<_StripedInterleavedBufferRange>(GetRange(idx).lock());
269  }
270 
271  };
272 
274  virtual HdBufferArrayRangeSharedPtr CreateBufferArrayRange();
275 
277  virtual HdBufferSpecVector GetBufferSpecs(
278  HdBufferArraySharedPtr const &bufferArray) const;
279 
281  virtual size_t GetResourceAllocation(
282  HdBufferArraySharedPtr const &bufferArray,
283  VtDictionary &result) const;
284 };
285 
286 class HdStInterleavedUBOMemoryManager : public HdStInterleavedMemoryManager {
287 public:
290  HDST_API
291  virtual HdBufferArraySharedPtr CreateBufferArray(
292  TfToken const &role,
293  HdBufferSpecVector const &bufferSpecs,
294  HdBufferArrayUsageHint usageHint);
295 
297  HDST_API
299  HdBufferSpecVector const &bufferSpecs,
300  HdBufferArrayUsageHint usageHint) const;
301 };
302 
303 class HdStInterleavedSSBOMemoryManager : public HdStInterleavedMemoryManager {
304 public:
307  HDST_API
308  virtual HdBufferArraySharedPtr CreateBufferArray(
309  TfToken const &role,
310  HdBufferSpecVector const &bufferSpecs,
311  HdBufferArrayUsageHint usageHint);
312 
314  HDST_API
316  HdBufferSpecVector const &bufferSpecs,
317  HdBufferArrayUsageHint usageHint) const;
318 };
319 
320 PXR_NAMESPACE_CLOSE_SCOPE
321 
322 #endif // HDST_INTERLEAVED_VBO_MEMORY_MANAGER_H
virtual HDST_API HdStBufferResourceGLSharedPtr GetResource() const
Returns the GPU resource.
HDST_API HdBufferSpecVector GetBufferSpecs() const
Reconstructs the bufferspecs and returns it (for buffer splitting)
size_t AggregationId
Aggregation ID.
Definition: strategyBase.h:51
The union provides a set of flags that provide hints to the memory management system about the proper...
Definition: bufferArray.h:74
virtual void IncrementVersion()
Increment the version of the buffer array.
virtual HDST_API void DebugDump(std::ostream &out) const
Debug output.
virtual bool IsValid() const
Returns true if this range is valid.
Similar to a VAO, this object is a bundle of coherent buffers.
Definition: bufferArray.h:92
void SetIndex(int index)
Set the relative offset for this range.
Aggregation strategy base class.
Definition: strategyBase.h:48
HD_API void IncrementVersion()
Increments the version of this buffer array.
virtual int GetIndex() const
Returns the index for this range.
HdTupleType represents zero, one, or more values of the same HdType.
Definition: types.h:239
A map with string keys and VtValue values.
Definition: dictionary.h:61
HdStBufferResourceGLNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArray.
virtual HDST_API const void * _GetAggregation() const
Returns the aggregation container.
virtual HdBufferArraySharedPtr CreateBufferArray(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint)=0
Factory for creating HdBufferArray.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
Interleaved memory manager (base class).
virtual HdBufferSpecVector GetBufferSpecs(HdBufferArraySharedPtr const &bufferArray) const
Returns the buffer specs from a given buffer array.
virtual HDST_API bool IsAssigned() const
Returns true is the range has been assigned to a buffer.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
virtual HdBufferArrayRangeSharedPtr CreateBufferArrayRange()
Factory for creating HdBufferArrayRange.
virtual HDST_API ~_StripedInterleavedBufferRange()
Destructor.
virtual HDST_API bool Resize(int numElements)
Resize memory area for this range.
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:289
virtual HDST_API HdStBufferResourceGLNamedList const & GetResources() const
Returns the list of all named GPU resources for this bufferArrayRange.
virtual 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 void CopyData(HdBufferSourceSharedPtr const &bufferSource)
Copy source data into buffer.
HDST_API _StripedInterleavedBuffer(TfToken const &role, HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint, int bufferOffsetAlignment, int structAlignment, size_t maxSize, TfToken const &garbageCollectionPerfToken)
Constructor.
void SetNeedsReallocation()
Mark to perform reallocation on Reallocate()
Interface class for representing range (subset) locator of HdBufferArray.
virtual size_t GetVersion() const
Returns the version of the buffer array.
virtual HDST_API HdBufferArrayUsageHint GetUsageHint() const override
Returns the usage hint from the underlying buffer array.
virtual HDST_API void DebugDump(std::ostream &out) const
Debug dump.
virtual HDST_API void SetBufferArray(HdBufferArray *bufferArray)
Sets the buffer array associated with this buffer;.
HDST_API HdStBufferResourceGLSharedPtr GetResource() const
TODO: We need to distinguish between the primvar types here, we should tag each HdBufferSource and Hd...
void SetNeedsCompaction()
Mark to perform compaction on GarbageCollect()
size_t GetVersion() const
Returns the version of this buffer array.
Definition: bufferArray.h:108
virtual AggregationId ComputeAggregationId(HdBufferSpecVector const &bufferSpecs, HdBufferArrayUsageHint usageHint) const =0
Returns id for given bufferSpecs to be used for aggregation.
HDST_API HdStBufferResourceGLSharedPtr _AddResource(TfToken const &name, HdTupleType tupleType, int offset, int stride)
Adds a new, named GPU resource and returns it.
virtual int GetOffset() const
Returns the relative offset in aggregated buffer.
virtual size_t GetNumElements() const
Returns the number of elements.
virtual HDST_API VtValue ReadData(TfToken const &name) const
Read back the buffer content.
virtual bool IsImmutable() const
Returns true if this range is marked as immutable.
bool _needsReallocation
Dirty bit to set when the ranges attached to the buffer changes.
Definition: bufferArray.h:171
virtual HDST_API void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner)
Performs reallocation.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182
virtual HDST_API bool GarbageCollect()
perform compaction if necessary, returns true if it becomes empty.
virtual HDST_API size_t GetMaxNumElements() const
Returns the max number of elements.
virtual HDST_API ~_StripedInterleavedBuffer()
Destructor. It invalidates _rangeList.