1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//! Wrapper for a wgpu buffer object.
use crate::context::Context;
use bytemuck::{Pod, Zeroable};
/// A vector of elements that can be loaded to the GPU, on the RAM, or both.
pub struct GPUVec<T: Pod + Zeroable> {
dirty: bool,
len: usize,
usage: wgpu::BufferUsages,
buffer: Option<wgpu::Buffer>,
data: Option<Vec<T>>,
}
impl<T: Pod + Zeroable> GPUVec<T> {
/// Creates a new `GPUVec` that is not yet uploaded to the GPU.
pub fn new(data: Vec<T>, buf_type: BufferType, _alloc_type: AllocationType) -> GPUVec<T> {
let usage = buf_type.to_wgpu();
GPUVec {
dirty: true,
len: data.len(),
usage,
buffer: None,
data: Some(data),
}
}
/// Creates a new empty `GPUVec`.
pub fn new_empty(buf_type: BufferType, _alloc_type: AllocationType) -> GPUVec<T> {
let usage = buf_type.to_wgpu();
GPUVec {
dirty: false,
len: 0,
usage,
buffer: None,
data: Some(Vec::new()),
}
}
/// Is this vector empty?
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// The length of this vector.
#[inline]
pub fn len(&self) -> usize {
if self.dirty {
match self.data {
Some(ref d) => d.len(),
None => panic!("This should never happen."),
}
} else {
self.len
}
}
/// Mutably accesses the vector if it is available on RAM.
///
/// This method will mark this vector as `dirty`.
#[inline]
pub fn data_mut(&mut self) -> &mut Option<Vec<T>> {
self.dirty = true;
&mut self.data
}
/// Immutably accesses the vector if it is available on RAM.
#[inline]
pub fn data(&self) -> &Option<Vec<T>> {
&self.data
}
/// Returns `true` if this vector is already uploaded to the GPU.
#[inline]
pub fn is_on_gpu(&self) -> bool {
self.buffer.is_some()
}
/// Returns `true` if the cpu data and gpu data are out of sync.
#[inline]
pub fn dirty(&self) -> bool {
self.dirty
}
/// Returns `true` if the cpu data and gpu data are out of sync.
/// Alias for `dirty()` for backwards compatibility.
#[inline]
pub fn trash(&self) -> bool {
self.dirty
}
/// Returns `true` if this vector is available on RAM.
///
/// Note that a `GPUVec` may be both on RAM and on the GPU.
#[inline]
pub fn is_on_ram(&self) -> bool {
self.data.is_some()
}
/// Returns the wgpu buffer if it exists.
#[inline]
pub fn buffer(&self) -> Option<&wgpu::Buffer> {
self.buffer.as_ref()
}
/// Returns the buffer usage flags.
#[inline]
pub fn usage(&self) -> wgpu::BufferUsages {
self.usage
}
/// Loads the vector from the RAM to the GPU.
///
/// If the vector is not available on RAM or already loaded to the GPU, nothing will happen.
#[inline]
pub fn load_to_gpu(&mut self) {
let ctxt = Context::get();
if let Some(ref data) = self.data {
if data.is_empty() {
return;
}
let bytes = bytemuck::cast_slice(data);
if !self.is_on_gpu() {
// Create new buffer
self.len = data.len();
let buffer = ctxt.create_buffer_init(
Some("GPUVec buffer"),
bytes,
self.usage | wgpu::BufferUsages::COPY_DST,
);
self.buffer = Some(buffer);
} else if self.dirty {
// Update existing buffer
self.len = data.len();
if let Some(ref buffer) = self.buffer {
let buffer_size = buffer.size() as usize;
let data_size = bytes.len();
if data_size <= buffer_size {
// Buffer is big enough, just update
ctxt.write_buffer(buffer, 0, bytes);
} else {
// Need to recreate buffer
let new_buffer = ctxt.create_buffer_init(
Some("GPUVec buffer"),
bytes,
self.usage | wgpu::BufferUsages::COPY_DST,
);
self.buffer = Some(new_buffer);
}
}
}
}
self.dirty = false;
}
/// Ensures the buffer is on the GPU and returns a reference to it.
///
/// Returns None if the data is empty.
#[inline]
pub fn ensure_on_gpu(&mut self) -> Option<&wgpu::Buffer> {
self.load_to_gpu();
self.buffer.as_ref()
}
/// Prepares this vector to be filled directly by a compute shader.
///
/// Ensures a GPU-resident buffer of at least `count` elements exists with
/// `STORAGE` usage added (so a compute pass on the same `wgpu::Device` can
/// write into it), reports a length of `count`, and detaches any CPU-side
/// data so a subsequent [`load_to_gpu`](Self::load_to_gpu) at render time is
/// a no-op and will not overwrite the compute-written contents.
///
/// The buffer is reallocated only when it does not yet exist or is too
/// small, so calling this every frame at a stable `count` is cheap. Returns
/// the GPU buffer to bind as a compute output.
#[inline]
pub fn prepare_gpu_writable(&mut self, count: usize) -> &wgpu::Buffer {
let ctxt = Context::get();
self.usage |=
wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::VERTEX;
let needed = (std::mem::size_of::<T>() * count.max(1)) as u64;
// Reallocate when the buffer is missing, too small, OR lacks the usage
// flags we just added.
let realloc = match &self.buffer {
Some(b) => b.size() < needed || !b.usage().contains(self.usage),
None => true,
};
if realloc {
self.buffer = Some(ctxt.create_buffer(&wgpu::BufferDescriptor {
label: Some("GPUVec compute-writable buffer"),
size: needed,
usage: self.usage,
mapped_at_creation: false,
}));
}
// Report `count` instances and detach CPU data: rendering reads `len`
// (since `dirty` is false) and `load_to_gpu` becomes a no-op.
self.len = count;
self.dirty = false;
self.data = None;
self.buffer.as_ref().unwrap()
}
/// Unloads this resource from the GPU.
#[inline]
pub fn unload_from_gpu(&mut self) {
self.len = self.len();
self.buffer = None;
self.dirty = false;
}
/// Removes this resource from the RAM.
///
/// This is useful to save memory for vectors required on the GPU only.
#[inline]
pub fn unload_from_ram(&mut self) {
if self.dirty && self.is_on_gpu() {
self.load_to_gpu();
}
self.data = None;
}
}
impl<T: Clone + Pod + Zeroable> GPUVec<T> {
/// Returns this vector as an owned vector if it is available on RAM.
///
/// If it has been uploaded to the GPU, and unloaded from the RAM, call `load_to_ram` first to
/// make the data accessible.
#[inline]
pub fn to_owned(&self) -> Option<Vec<T>> {
self.data.as_ref().cloned()
}
}
/// Type of gpu buffer.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BufferType {
/// A vertex buffer (bindable as vertex data).
Array,
/// An index buffer (bindable as index data).
ElementArray,
/// A read-only storage buffer (bindable in a bind group, indexed in shaders).
Storage,
}
impl BufferType {
/// Converts to wgpu buffer usages.
#[inline]
pub fn to_wgpu(self) -> wgpu::BufferUsages {
match self {
BufferType::Array => wgpu::BufferUsages::VERTEX,
BufferType::ElementArray => wgpu::BufferUsages::INDEX,
BufferType::Storage => wgpu::BufferUsages::STORAGE,
}
}
}
/// Allocation type of gpu buffers.
///
/// Note: In wgpu, allocation hints are handled differently than in OpenGL.
/// These are kept for API compatibility but may not have the same effect.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AllocationType {
/// Data uploaded once, used many times (immutable meshes).
StaticDraw,
/// Data modified frequently.
DynamicDraw,
/// Data for immediate use (lines, points, text).
StreamDraw,
}