bufferArray.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 PXR_IMAGING_HD_BUFFER_ARRAY_H
25 #define PXR_IMAGING_HD_BUFFER_ARRAY_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/api.h"
29 #include "pxr/imaging/hd/version.h"
30 #include "pxr/imaging/hd/bufferSpec.h"
31 #include "pxr/imaging/hd/bufferResource.h"
32 #include "pxr/base/tf/token.h"
33 #include "pxr/base/vt/value.h"
34 
35 #include <atomic>
36 #include <memory>
37 #include <mutex>
38 
39 PXR_NAMESPACE_OPEN_SCOPE
40 
41 
42 class HdBufferArrayRange;
43 
44 using HdBufferArraySharedPtr = std::shared_ptr<class HdBufferArray>;
45 using HdBufferArrayRangeSharedPtr = std::shared_ptr<HdBufferArrayRange>;
46 using HdBufferArrayRangePtr = std::weak_ptr<HdBufferArrayRange>;
47 
71  struct _Bits {
72  uint32_t immutable : 1;
73  uint32_t sizeVarying : 1;
74  uint32_t pad : 30;
75  } bits;
76  uint32_t value;
77 
78  HdBufferArrayUsageHint() : value(0) {}
79 };
80 
81 
88 class HdBufferArray : public std::enable_shared_from_this<HdBufferArray>
89 {
90 public:
91  HD_API
92  HdBufferArray(TfToken const &role,
93  TfToken const garbageCollectionPerfToken,
94  HdBufferArrayUsageHint usageHint);
95 
96  HD_API
97  virtual ~HdBufferArray();
98 
100  TfToken const& GetRole() const {return _role;}
101 
104  size_t GetVersion() const {
105  return _version;
106  }
107 
109  HD_API
110  void IncrementVersion();
111 
116  HD_API
117  bool TryAssignRange(HdBufferArrayRangeSharedPtr &range);
118 
120  virtual bool GarbageCollect() = 0;
121 
126  virtual void Reallocate(
127  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
128  HdBufferArraySharedPtr const &curRangeOwner) = 0;
129 
131  HD_API
132  virtual size_t GetMaxNumElements() const;
133 
135  virtual void DebugDump(std::ostream &out) const = 0;
136 
138  size_t GetRangeCount() const { return _rangeCount; }
139 
141  HD_API
142  HdBufferArrayRangePtr GetRange(size_t idx) const;
143 
146  HD_API
147  void RemoveUnusedRanges();
148 
150  bool NeedsReallocation() const {
151  return _needsReallocation;
152  }
153 
155  bool IsImmutable() const {
156  return _usageHint.bits.immutable;
157  }
158 
161  return _usageHint;
162  }
163 
164 protected:
168 
171  void _SetMaxNumRanges(size_t max) { _maxNumRanges = max; }
172 
174  HD_API
175  void _SetRangeList(std::vector<HdBufferArrayRangeSharedPtr> const &ranges);
176 
177 private:
178 
179  // Do not allow copies.
180  HdBufferArray(const HdBufferArray &) = delete;
181  HdBufferArray &operator=(const HdBufferArray &) = delete;
182 
183 
184  typedef std::vector<HdBufferArrayRangePtr> _RangeList;
185 
186  // Vector of ranges associated with this buffer
187  // We add values to the list in a multi-threaded fashion
188  // but can later remove them in _RemoveUnusedRanges
189  // than add more.
190  //
191  _RangeList _rangeList;
192  std::atomic_size_t _rangeCount; // how many ranges are valid in list
193  std::mutex _rangeListLock;
194 
195  const TfToken _role;
196  const TfToken _garbageCollectionPerfToken;
197 
198  size_t _version;
199 
200  size_t _maxNumRanges;
201  HdBufferArrayUsageHint _usageHint;
202 };
203 
204 
205 PXR_NAMESPACE_CLOSE_SCOPE
206 
207 #endif //PXR_IMAGING_HD_BUFFER_ARRAY_H
The union provides a set of flags that provide hints to the memory management system about the proper...
Definition: bufferArray.h:70
Similar to a VAO, this object is a bundle of coherent buffers.
Definition: bufferArray.h:88
virtual void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner)=0
Performs reallocation.
HD_API void IncrementVersion()
Increments the version of this buffer array.
Interface class for representing range (subset) locator of HdBufferArray.
bool NeedsReallocation() const
Returns true if Reallocate() needs to be called on this buffer array.
Definition: bufferArray.h:150
virtual HD_API size_t GetMaxNumElements() const
Returns the maximum number of elements capacity.
void _SetMaxNumRanges(size_t max)
Limits the number of ranges that can be allocated to this buffer to max.
Definition: bufferArray.h:171
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
bool IsImmutable() const
Returns true if this buffer array is marked as immutable.
Definition: bufferArray.h:155
size_t GetRangeCount() const
How many ranges are attached to the buffer array.
Definition: bufferArray.h:138
virtual bool GarbageCollect()=0
Performs compaction if necessary and returns true if it becomes empty.
HD_API void _SetRangeList(std::vector< HdBufferArrayRangeSharedPtr > const &ranges)
Swap the rangelist with ranges.
TfToken const & GetRole() const
Returns the role of the GPU data in this bufferArray.
Definition: bufferArray.h:100
HD_API void RemoveUnusedRanges()
Remove any ranges from the range list that have been deallocated Returns number of ranges after clean...
HD_API bool TryAssignRange(HdBufferArrayRangeSharedPtr &range)
Attempts to assign a range to this buffer array.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
virtual void DebugDump(std::ostream &out) const =0
Debug output.
bool _needsReallocation
Dirty bit to set when the ranges attached to the buffer changes.
Definition: bufferArray.h:167
HdBufferArrayUsageHint GetUsageHint() const
Returns the usage hints for this buffer array.
Definition: bufferArray.h:160
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
size_t GetVersion() const
Returns the version of this buffer array.
Definition: bufferArray.h:104