Loading...
Searching...
No Matches
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/base/tf/token.h"
32#include "pxr/base/vt/value.h"
33
34#include <atomic>
35#include <memory>
36#include <mutex>
37
38PXR_NAMESPACE_OPEN_SCOPE
39
40
42
43using HdBufferArraySharedPtr = std::shared_ptr<class HdBufferArray>;
44using HdBufferArrayRangeSharedPtr = std::shared_ptr<HdBufferArrayRange>;
45using HdBufferArrayRangePtr = std::weak_ptr<HdBufferArrayRange>;
46
68enum HdBufferArrayUsageHintBits : uint32_t
69{
70 HdBufferArrayUsageHintBitsImmutable = 1 << 0,
71 HdBufferArrayUsageHintBitsSizeVarying = 1 << 1,
72 HdBufferArrayUsageHintBitsUniform = 1 << 2,
73 HdBufferArrayUsageHintBitsStorage = 1 << 3,
74 HdBufferArrayUsageHintBitsVertex = 1 << 4,
75 HdBufferArrayUsageHintBitsIndex = 1 << 5,
76};
77using HdBufferArrayUsageHint = uint32_t;
78
85class HdBufferArray : public std::enable_shared_from_this<HdBufferArray>
86{
87public:
88 HD_API
89 HdBufferArray(TfToken const &role,
90 TfToken const garbageCollectionPerfToken,
91 HdBufferArrayUsageHint usageHint);
92
93 HD_API
94 virtual ~HdBufferArray();
95
97 TfToken const& GetRole() const {return _role;}
98
101 size_t GetVersion() const {
102 return _version;
103 }
104
106 HD_API
108
113 HD_API
114 bool TryAssignRange(HdBufferArrayRangeSharedPtr &range);
115
117 virtual bool GarbageCollect() = 0;
118
123 virtual void Reallocate(
124 std::vector<HdBufferArrayRangeSharedPtr> const &ranges,
125 HdBufferArraySharedPtr const &curRangeOwner) = 0;
126
128 HD_API
129 virtual size_t GetMaxNumElements() const;
130
132 virtual void DebugDump(std::ostream &out) const = 0;
133
135 size_t GetRangeCount() const { return _rangeCount; }
136
138 HD_API
139 HdBufferArrayRangePtr GetRange(size_t idx) const;
140
143 HD_API
145
147 bool NeedsReallocation() const {
148 return _needsReallocation;
149 }
150
152 bool IsImmutable() const {
153 return _usageHint & HdBufferArrayUsageHintBitsImmutable;
154 }
155
157 HdBufferArrayUsageHint GetUsageHint() const {
158 return _usageHint;
159 }
160
161protected:
165
168 void _SetMaxNumRanges(size_t max) { _maxNumRanges = max; }
169
171 HD_API
172 void _SetRangeList(std::vector<HdBufferArrayRangeSharedPtr> const &ranges);
173
174private:
175
176 // Do not allow copies.
177 HdBufferArray(const HdBufferArray &) = delete;
178 HdBufferArray &operator=(const HdBufferArray &) = delete;
179
180
181 typedef std::vector<HdBufferArrayRangePtr> _RangeList;
182
183 // Vector of ranges associated with this buffer
184 // We add values to the list in a multi-threaded fashion
185 // but can later remove them in _RemoveUnusedRanges
186 // than add more.
187 //
188 _RangeList _rangeList;
189 std::atomic_size_t _rangeCount; // how many ranges are valid in list
190 std::mutex _rangeListLock;
191
192 const TfToken _role;
193 const TfToken _garbageCollectionPerfToken;
194
195 size_t _version;
196
197 size_t _maxNumRanges;
198 HdBufferArrayUsageHint _usageHint;
199};
200
201
202PXR_NAMESPACE_CLOSE_SCOPE
203
204#endif //PXR_IMAGING_HD_BUFFER_ARRAY_H
Similar to a VAO, this object is a bundle of coherent buffers.
Definition: bufferArray.h:86
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:152
void _SetMaxNumRanges(size_t max)
Limits the number of ranges that can be allocated to this buffer to max.
Definition: bufferArray.h:168
bool NeedsReallocation() const
Returns true if Reallocate() needs to be called on this buffer array.
Definition: bufferArray.h:147
bool _needsReallocation
Dirty bit to set when the ranges attached to the buffer changes.
Definition: bufferArray.h:164
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:157
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:101
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:135
TfToken const & GetRole() const
Returns the role of the GPU data in this bufferArray.
Definition: bufferArray.h:97
Interface class for representing range (subset) locator of HdBufferArray.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...