RavEngine
Loading...
Searching...
No Matches
BufferedVRAMVector.hpp
1#pragma once
2#include "VRAMVector.hpp"
3#include <RGL/Types.hpp>
4#include "Function.hpp"
5#include "VRAMSparseSet.hpp"
6#include "Types.hpp"
7
8namespace RavEngine{
9
11
12protected:
13 RGLBufferPtr privateBuffer;
14 std::string debugName;
15 std::vector<unsigned char> syncTrackingBuffer;
16
17 void EncodeSync(RGLDevicePtr device, RGLBufferPtr hostBuffer, RGLCommandBufferPtr commandBuffer, uint32_t elemSize, const Function<void(RGLBufferPtr)>& gcBuffersFn, bool& previousCommandResetCB);
18
19 void InitializePrivateBuffer(RGLDevicePtr device, uint32_t size);
20
21public:
22 auto GetPrivateBuffer() const{
23 return privateBuffer;
24 }
26 BufferedVRAMStructureBase(const std::string_view debugName) : debugName(debugName) {}
27
29
30 MOVE_NO_COPY(BufferedVRAMStructureBase);
31};
32
33template<typename T>
35 VRAMVector<T> hostBuffer;
36
37public:
39 BufferedVRAMVector(const std::string_view debugName) : BufferedVRAMStructureBase(debugName){}
40 BufferedVRAMVector(uint32_t initialSize){
41 Resize(initialSize);
42 }
43
44 MOVE_NO_COPY(BufferedVRAMVector);
45
46 virtual ~BufferedVRAMVector(){ }
47
48 auto& GetHostBuffer() const{
49 return hostBuffer;
50 }
51
57 void EncodeSync(RGLDevicePtr device, RGLCommandBufferPtr commandBuffer, const Function<void(RGLBufferPtr)>& gcBuffersFn, bool& previousCommandResetCB){
58 BufferedVRAMStructureBase::EncodeSync(device, hostBuffer.buffer, commandBuffer, sizeof(T), gcBuffersFn, previousCommandResetCB);
59 }
60
61 void Resize(uint32_t newSize){
62 if (hostBuffer.size() != newSize){
63 hostBuffer.resize(newSize);
64 syncTrackingBuffer.resize(newSize, true); // ensure the initial copy is included
65 }
66 }
67
72 hostBuffer._setElementCount(allocatedElements);
73 syncTrackingBuffer.resize(allocatedElements, true);
74 }
75
76 void push_back(const T& value){
77 hostBuffer.push_back(value);
78 syncTrackingBuffer.resize(hostBuffer.size(), true);
79 }
80
81 void erase(const auto& it){
82 hostBuffer.erase(it);
83 auto index = it - hostBuffer.begin();
84 // elements have been shifted down, so mark all elements afterward as modified
85 for(auto it = syncTrackingBuffer.begin() + index; it != syncTrackingBuffer.end(); ++it){
86 *it = true;
87 }
88 }
89
90 auto begin(){
91 return hostBuffer.begin();
92 }
93
94 void reserve(uint32_t size){
95 if (hostBuffer.capacity() < size) {
96 hostBuffer.reserve(size);
97 syncTrackingBuffer.reserve(size);
98 }
99 }
100
101 auto Size() const{
102 return hostBuffer.size();
103 }
104
105 auto capacity() const {
106 return hostBuffer.capacity();
107 }
108
109 const auto& operator[](uint32_t i) const{
110 return hostBuffer[i];
111 }
112
113 void SetValueAt(uint32_t i, const T& value){
114 hostBuffer[i] = value;
115 syncTrackingBuffer[i] = true; // signal that this was modified
116 }
117
118 auto& GetValueAtForWriting(uint32_t i){
119 syncTrackingBuffer[i] = true; // signal that this was modified
120 return hostBuffer[i];
121 }
122
123};
124
128template<typename index_t, typename T>
131
132 void ResizeIfNeeded(){
133 if (sparseSet.DenseSize() > syncTrackingBuffer.size()){
134 syncTrackingBuffer.resize(sparseSet.DenseSize(), true); // ensure the initial copy is included
135 }
136 }
137
138public:
139
140 using index_type = decltype(sparseSet)::index_type;
141 using value_type = T;
142
144 BufferedVRAMSparseSet(const std::string_view debugName) : BufferedVRAMStructureBase(debugName) {}
145 virtual ~BufferedVRAMSparseSet() {}
146
147 MOVE_NO_COPY(BufferedVRAMSparseSet);
148
149 auto DenseSize() const{
150 return sparseSet.DenseSize();
151 }
152
153 template<typename ... A>
154 void Emplace(index_t sparse_index, A&& ... args) {
155 sparseSet.Emplace(sparse_index, std::forward<A>(args) ...);
156 ResizeIfNeeded();
157 syncTrackingBuffer[sparseSet.SparseToDense(sparse_index)] = true; // mark modified
158 }
159
160 void EraseAtSparseIndex(index_t sparseIndex){
161 syncTrackingBuffer[sparseSet.SparseToDense(sparseIndex)] = true; // mark modified
162 sparseSet.EraseAtSparseIndex(sparseIndex);
163 }
164
165 const auto& GetReverseMap() const{
166 return sparseSet.reverse_map;
167 }
168
169 bool HasForSparseIndex(index_type sparseIndex) const{
170 return sparseSet.HasForSparseIndex(sparseIndex);
171 }
172
173// const auto& GetHostDense() const{
174// return sparseSet.GetDense();
175// }
176
177 const auto& GetAtDenseIndex(index_t dense_index){
178 return sparseSet.GetDense()[dense_index];
179 }
180
181 auto& GetHostDenseForWriting(index_t dense_index){
182 ResizeIfNeeded();
183 syncTrackingBuffer[dense_index] = true;
184 return sparseSet.GetDense()[dense_index];
185 }
186
187 auto& GetSparseIndexForDense(index_t index) {
188 return sparseSet.GetSparseIndexForDense(index);
189 }
190
191 auto& GetForSparseIndexForWriting(index_t sparse_index){
192 ResizeIfNeeded();
193 syncTrackingBuffer[sparseSet.SparseToDense(sparse_index)] = true;
194 return sparseSet.GetForSparseIndex(sparse_index);
195 }
196
197
203 void EncodeSync(RGLDevicePtr device, RGLCommandBufferPtr commandBuffer, const Function<void(RGLBufferPtr)>& gcBuffersFn, bool& previousCommandResetCB){
204 if (privateBuffer == nullptr){
205 InitializePrivateBuffer(device, 8 * sizeof(T));
206 }
207 BufferedVRAMStructureBase::EncodeSync(device, sparseSet.GetDense().get_underlying().buffer, commandBuffer, sizeof(T), gcBuffersFn, previousCommandResetCB);
208 }
209};
210}
Definition BufferedVRAMVector.hpp:129
void EncodeSync(RGLDevicePtr device, RGLCommandBufferPtr commandBuffer, const Function< void(RGLBufferPtr)> &gcBuffersFn, bool &previousCommandResetCB)
Definition BufferedVRAMVector.hpp:203
Definition BufferedVRAMVector.hpp:10
Definition BufferedVRAMVector.hpp:34
void _setElementCount(uint32_t allocatedElements)
Definition BufferedVRAMVector.hpp:71
void EncodeSync(RGLDevicePtr device, RGLCommandBufferPtr commandBuffer, const Function< void(RGLBufferPtr)> &gcBuffersFn, bool &previousCommandResetCB)
Definition BufferedVRAMVector.hpp:57
Definition concurrentqueue.h:747
Definition Animation.hpp:6