All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 HD_BUFFER_ARRAY_H
25 #define 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 <boost/noncopyable.hpp>
36 #include <boost/shared_ptr.hpp>
37 #include <boost/enable_shared_from_this.hpp>
38 
39 #include <atomic>
40 #include <mutex>
41 
42 PXR_NAMESPACE_OPEN_SCOPE
43 
44 
45 class HdBufferArrayRange;
46 
47 typedef boost::shared_ptr<class HdBufferArray> HdBufferArraySharedPtr;
48 typedef boost::shared_ptr<HdBufferArrayRange> HdBufferArrayRangeSharedPtr;
49 typedef boost::weak_ptr<HdBufferArrayRange> HdBufferArrayRangePtr;
50 typedef boost::shared_ptr<class HdBufferSource> HdBufferSourceSharedPtr;
51 
75  struct _Bits {
76  uint32_t immutable : 1;
77  uint32_t sizeVarying : 1;
78  uint32_t pad : 30;
79  } bits;
80  uint32_t value;
81 
82  HdBufferArrayUsageHint() : value(0) {}
83 };
84 
85 
92 class HdBufferArray : public boost::enable_shared_from_this<HdBufferArray>,
93  boost::noncopyable {
94 public:
95  HD_API
96  HdBufferArray(TfToken const &role,
97  TfToken const garbageCollectionPerfToken,
98  HdBufferArrayUsageHint usageHint);
99 
100  HD_API
101  virtual ~HdBufferArray();
102 
104  TfToken const& GetRole() const {return _role;}
105 
108  size_t GetVersion() const {
109  return _version;
110  }
111 
113  HD_API
114  void IncrementVersion();
115 
120  HD_API
121  bool TryAssignRange(HdBufferArrayRangeSharedPtr &range);
122 
124  virtual bool GarbageCollect() = 0;
125 
130  virtual void Reallocate(
131  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
132  HdBufferArraySharedPtr const &curRangeOwner) = 0;
133 
135  HD_API
136  virtual size_t GetMaxNumElements() const;
137 
139  virtual void DebugDump(std::ostream &out) const = 0;
140 
142  size_t GetRangeCount() const { return _rangeCount; }
143 
145  HD_API
146  HdBufferArrayRangePtr GetRange(size_t idx) const;
147 
150  HD_API
151  void RemoveUnusedRanges();
152 
154  bool NeedsReallocation() const {
155  return _needsReallocation;
156  }
157 
159  bool IsImmutable() const {
160  return _usageHint.bits.immutable;
161  }
162 
165  return _usageHint;
166  }
167 
168 protected:
172 
175  void _SetMaxNumRanges(size_t max) { _maxNumRanges = max; }
176 
178  HD_API
179  void _SetRangeList(std::vector<HdBufferArrayRangeSharedPtr> const &ranges);
180 
181 private:
182  typedef std::vector<HdBufferArrayRangePtr> _RangeList;
183 
184  // Vector of ranges associated with this buffer
185  // We add values to the list in a multi-threaded fashion
186  // but can later remove them in _RemoveUnusedRanges
187  // than add more.
188  //
189  _RangeList _rangeList;
190  std::atomic_size_t _rangeCount; // how many ranges are valid in list
191  std::mutex _rangeListLock;
192 
193  const TfToken _role;
194  const TfToken _garbageCollectionPerfToken;
195 
196  size_t _version;
197 
198  size_t _maxNumRanges;
199  HdBufferArrayUsageHint _usageHint;
200 };
201 
202 
203 PXR_NAMESPACE_CLOSE_SCOPE
204 
205 #endif //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:74
HdBufferArrayUsageHint GetUsageHint() const
Returns the usage hints for this buffer array.
Definition: bufferArray.h:164
Similar to a VAO, this object is a bundle of coherent buffers.
Definition: bufferArray.h:92
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.
bool IsImmutable() const
Returns true if this buffer array is marked as immutable.
Definition: bufferArray.h:159
Interface class for representing range (subset) locator of HdBufferArray.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
void _SetMaxNumRanges(size_t max)
Limits the number of ranges that can be allocated to this buffer to max.
Definition: bufferArray.h:175
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
virtual bool GarbageCollect()=0
Performs compaction if necessary and returns true if it becomes empty.
TfToken const & GetRole() const
Returns the role of the GPU data in this bufferArray.
Definition: bufferArray.h:104
HD_API void _SetRangeList(std::vector< HdBufferArrayRangeSharedPtr > const &ranges)
Swap the rangelist with ranges.
size_t GetRangeCount() const
How many ranges are attached to the buffer array.
Definition: bufferArray.h:142
virtual HD_API size_t GetMaxNumElements() const
Returns the maximum number of elements capacity.
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.
size_t GetVersion() const
Returns the version of this buffer array.
Definition: bufferArray.h:108
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:171
bool NeedsReallocation() const
Returns true if Reallocate() needs to be called on this buffer array.
Definition: bufferArray.h:154