All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
bufferArray.h
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"
13#include "pxr/imaging/hd/bufferSpec.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
21PXR_NAMESPACE_OPEN_SCOPE
22
23
25
26using HdBufferArraySharedPtr = std::shared_ptr<class HdBufferArray>;
27using HdBufferArrayRangeSharedPtr = std::shared_ptr<HdBufferArrayRange>;
28using HdBufferArrayRangePtr = std::weak_ptr<HdBufferArrayRange>;
29
51enum HdBufferArrayUsageHintBits : uint32_t
52{
53 HdBufferArrayUsageHintBitsImmutable = 1 << 0,
54 HdBufferArrayUsageHintBitsSizeVarying = 1 << 1,
55 HdBufferArrayUsageHintBitsUniform = 1 << 2,
56 HdBufferArrayUsageHintBitsStorage = 1 << 3,
57 HdBufferArrayUsageHintBitsVertex = 1 << 4,
58 HdBufferArrayUsageHintBitsIndex = 1 << 5,
59};
60using HdBufferArrayUsageHint = uint32_t;
61
68class HdBufferArray : public std::enable_shared_from_this<HdBufferArray>
69{
70public:
71 HD_API
72 HdBufferArray(TfToken const &role,
73 TfToken const garbageCollectionPerfToken,
74 HdBufferArrayUsageHint usageHint);
75
76 HD_API
77 virtual ~HdBufferArray();
78
80 TfToken const& GetRole() const {return _role;}
81
84 size_t GetVersion() const {
85 return _version;
86 }
87
89 HD_API
91
96 HD_API
97 bool TryAssignRange(HdBufferArrayRangeSharedPtr &range);
98
100 virtual bool GarbageCollect() = 0;
101
106 virtual void Reallocate(
107 std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
108 HdBufferArraySharedPtr const &curRangeOwner) = 0;
109
111 HD_API
112 virtual size_t GetMaxNumElements() const;
113
115 virtual void DebugDump(std::ostream &out) const = 0;
116
118 size_t GetRangeCount() const { return _rangeCount; }
119
121 HD_API
122 HdBufferArrayRangePtr GetRange(size_t idx) const;
123
126 HD_API
128
130 bool NeedsReallocation() const {
131 return _needsReallocation;
132 }
133
135 bool IsImmutable() const {
136 return _usageHint & HdBufferArrayUsageHintBitsImmutable;
137 }
138
140 HdBufferArrayUsageHint GetUsageHint() const {
141 return _usageHint;
142 }
143
144protected:
148
151 void _SetMaxNumRanges(size_t max) { _maxNumRanges = max; }
152
154 HD_API
155 void _SetRangeList(std::vector<HdBufferArrayRangeSharedPtr> const &ranges);
156
157private:
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
185PXR_NAMESPACE_CLOSE_SCOPE
186
187#endif //PXR_IMAGING_HD_BUFFER_ARRAY_H
Similar to a VAO, this object is a bundle of coherent buffers.
Definition: bufferArray.h:69
virtual bool GarbageCollect()=0
Performs compaction if necessary and returns true if it becomes empty.
virtual void DebugDump(std::ostream &out) const =0
Debug output.
virtual void Reallocate(std::vector< HdBufferArrayRangeSharedPtr > const &ranges, HdBufferArraySharedPtr const &curRangeOwner)=0
Performs reallocation.
virtual HD_API size_t GetMaxNumElements() const
Returns the maximum number of elements capacity.
bool IsImmutable() const
Returns true if this buffer array is marked as immutable.
Definition: bufferArray.h:135
void _SetMaxNumRanges(size_t max)
Limits the number of ranges that can be allocated to this buffer to max.
Definition: bufferArray.h:151
bool NeedsReallocation() const
Returns true if Reallocate() needs to be called on this buffer array.
Definition: bufferArray.h:130
bool _needsReallocation
Dirty bit to set when the ranges attached to the buffer changes.
Definition: bufferArray.h:147
HD_API void IncrementVersion()
Increments the version of this buffer array.
HD_API void RemoveUnusedRanges()
Remove any ranges from the range list that have been deallocated Returns number of ranges after clean...
HdBufferArrayUsageHint GetUsageHint() const
Returns the usage hints for this buffer array.
Definition: bufferArray.h:140
HD_API HdBufferArrayRangePtr GetRange(size_t idx) const
Get the attached range at the specified index.
size_t GetVersion() const
Returns the version of this buffer array.
Definition: bufferArray.h:84
HD_API bool TryAssignRange(HdBufferArrayRangeSharedPtr &range)
Attempts to assign a range to this buffer array.
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:118
TfToken const & GetRole() const
Returns the role of the GPU data in this bufferArray.
Definition: bufferArray.h:80
Interface class for representing range (subset) locator of HdBufferArray.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...