HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bufferArray.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HD_BUFFER_ARRAY_H
8 #define PXR_IMAGING_HD_BUFFER_ARRAY_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hd/api.h"
12 #include "pxr/imaging/hd/version.h"
14 #include "pxr/base/tf/token.h"
15 #include "pxr/base/vt/value.h"
16 
17 #include <atomic>
18 #include <memory>
19 #include <mutex>
20 
22 
23 
24 class HdBufferArrayRange;
25 
26 using HdBufferArraySharedPtr = std::shared_ptr<class HdBufferArray>;
27 using HdBufferArrayRangeSharedPtr = std::shared_ptr<HdBufferArrayRange>;
28 using HdBufferArrayRangePtr = std::weak_ptr<HdBufferArrayRange>;
29 
30 /// \enum HdBufferArrayUsageHintBits
31 ///
32 /// Provides a set of flags that provide hints to the memory management system
33 /// about the properties of a Buffer Array Range (BAR), so it can efficiently
34 /// organize that memory. For example, the memory manager should probably not
35 /// aggregate BARs with different usage hints.
36 ///
37 /// The flag bits are:
38 /// - immutable: The BAR will not be modified once created and populated.
39 /// - sizeVarying: The number of elements in the BAR changes with time.
40 /// - uniform: The BAR can be used as a uniform buffer.
41 /// - storage: The BAR can be used as a shader storage buffer.
42 /// - vertex: The BAR can be used as a vertex buffer.
43 /// - index: The BAR can be used as an index buffer.
44 ///
45 /// Some flag bits may not make sense in combination (i.e. mutually exclusive
46 /// to each other). For example, it is logically impossible to be both
47 /// immutable (i.e. not changing) and sizeVarying (changing). However, these
48 /// logically impossible combinations are not enforced and remain valid
49 /// potential values.
50 ///
52 {
59 };
60 using HdBufferArrayUsageHint = uint32_t;
61 
62 /// \class HdBufferArray
63 ///
64 /// Similar to a VAO, this object is a bundle of coherent buffers. This object
65 /// can be shared across multiple HdRprims, in the context of buffer
66 /// aggregation.
67 ///
68 class HdBufferArray : public std::enable_shared_from_this<HdBufferArray>
69 {
70 public:
71  HD_API
72  HdBufferArray(TfToken const &role,
73  TfToken const garbageCollectionPerfToken,
74  HdBufferArrayUsageHint usageHint);
75 
76  HD_API
77  virtual ~HdBufferArray();
78 
79  /// Returns the role of the GPU data in this bufferArray.
80  TfToken const& GetRole() const {return _role;}
81 
82  /// Returns the version of this buffer array.
83  /// Used to determine when to rebuild outdated indirect dispatch buffers
84  size_t GetVersion() const {
85  return _version;
86  }
87 
88  /// Increments the version of this buffer array.
89  HD_API
90  void IncrementVersion();
91 
92  /// Attempts to assign a range to this buffer array.
93  /// Multiple threads could be trying to assign to this buffer at the same time.
94  /// Returns true is the range is assigned to this buffer otherwise
95  /// returns false if the buffer doesn't have space to assign the range.
96  HD_API
98 
99  /// Performs compaction if necessary and returns true if it becomes empty.
100  virtual bool GarbageCollect() = 0;
101 
102  /// Performs reallocation. After reallocation, the buffer will contain
103  /// the specified \a ranges. If these ranges are currently held by a
104  /// different buffer array instance, then their data will be copied
105  /// from the specified \a curRangeOwner.
106  virtual void Reallocate(
107  std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
108  HdBufferArraySharedPtr const &curRangeOwner) = 0;
109 
110  /// Returns the maximum number of elements capacity.
111  HD_API
112  virtual size_t GetMaxNumElements() const;
113 
114  /// Debug output
115  virtual void DebugDump(std::ostream &out) const = 0;
116 
117  /// How many ranges are attached to the buffer array.
118  size_t GetRangeCount() const { return _rangeCount; }
119 
120  /// Get the attached range at the specified index.
121  HD_API
122  HdBufferArrayRangePtr GetRange(size_t idx) const;
123 
124  /// Remove any ranges from the range list that have been deallocated
125  /// Returns number of ranges after clean-up
126  HD_API
127  void RemoveUnusedRanges();
128 
129  /// Returns true if Reallocate() needs to be called on this buffer array.
130  bool NeedsReallocation() const {
131  return _needsReallocation;
132  }
133 
134  /// Returns true if this buffer array is marked as immutable.
135  bool IsImmutable() const {
136  return _usageHint & HdBufferArrayUsageHintBitsImmutable;
137  }
138 
139  /// Returns the usage hints for this buffer array.
141  return _usageHint;
142  }
143 
144 protected:
145  /// Dirty bit to set when the ranges attached to the buffer
146  /// changes. If set Reallocate() should be called to clean it.
148 
149  /// Limits the number of ranges that can be
150  /// allocated to this buffer to max.
151  void _SetMaxNumRanges(size_t max) { _maxNumRanges = max; }
152 
153  /// Swap the rangelist with \p ranges
154  HD_API
155  void _SetRangeList(std::vector<HdBufferArrayRangeSharedPtr> const &ranges);
156 
157 private:
158 
159  // Do not allow copies.
160  HdBufferArray(const HdBufferArray &) = delete;
161  HdBufferArray &operator=(const HdBufferArray &) = delete;
162 
163 
164  typedef std::vector<HdBufferArrayRangePtr> _RangeList;
165 
166  // Vector of ranges associated with this buffer
167  // We add values to the list in a multi-threaded fashion
168  // but can later remove them in _RemoveUnusedRanges
169  // than add more.
170  //
171  _RangeList _rangeList;
172  std::atomic_size_t _rangeCount; // how many ranges are valid in list
173  std::mutex _rangeListLock;
174 
175  const TfToken _role;
176  const TfToken _garbageCollectionPerfToken;
177 
178  size_t _version;
179 
180  size_t _maxNumRanges;
181  HdBufferArrayUsageHint _usageHint;
182 };
183 
184 
186 
187 #endif //PXR_IMAGING_HD_BUFFER_ARRAY_H
virtual bool GarbageCollect()=0
Performs compaction if necessary and returns true if it becomes empty.
bool NeedsReallocation() const
Returns true if Reallocate() needs to be called on this buffer array.
Definition: bufferArray.h:130
GLenum GLint * range
Definition: glcorearb.h:1925
bool _needsReallocation
Definition: bufferArray.h:147
TfToken const & GetRole() const
Returns the role of the GPU data in this bufferArray.
Definition: bufferArray.h:80
std::shared_ptr< class HdBufferArray > HdBufferArraySharedPtr
Definition: bufferArray.h:26
void _SetMaxNumRanges(size_t max)
Definition: bufferArray.h:151
bool IsImmutable() const
Returns true if this buffer array is marked as immutable.
Definition: bufferArray.h:135
HD_API void _SetRangeList(std::vector< HdBufferArrayRangeSharedPtr > const &ranges)
Swap the rangelist with ranges.
#define HD_API
Definition: api.h:23
HdBufferArrayUsageHintBits
Definition: bufferArray.h:51
virtual void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner)=0
virtual HD_API size_t GetMaxNumElements() const
Returns the maximum number of elements capacity.
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
Definition: token.h:70
HD_API void IncrementVersion()
Increments the version of this buffer array.
HD_API bool TryAssignRange(HdBufferArrayRangeSharedPtr &range)
virtual void DebugDump(std::ostream &out) const =0
Debug output.
virtual HD_API ~HdBufferArray()
size_t GetVersion() const
Definition: bufferArray.h:84
HdBufferArrayUsageHint GetUsageHint() const
Returns the usage hints for this buffer array.
Definition: bufferArray.h:140
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HD_API HdBufferArray(TfToken const &role, TfToken const garbageCollectionPerfToken, HdBufferArrayUsageHint usageHint)
uint32_t HdBufferArrayUsageHint
Definition: bufferArray.h:60
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:27
size_t GetRangeCount() const
How many ranges are attached to the buffer array.
Definition: bufferArray.h:118
HD_API void RemoveUnusedRanges()
std::weak_ptr< HdBufferArrayRange > HdBufferArrayRangePtr
Definition: bufferArray.h:28