2022-04-18 03:56:16 +00:00
|
|
|
//! Intermediate CPU-mappable, GPU-visible storage for transferral to a GPU buffer.
|
|
|
|
//!
|
|
|
|
//! TODO: persistent mapping to bypass spillover
|
2022-04-19 02:10:24 +00:00
|
|
|
//! TODO: use wgpu::util::StagingBelt?
|
|
|
|
//! TODO: pass access to a wgpu::Queue for write_buffer, staging belt recall, or command encoding
|
2022-04-18 03:56:16 +00:00
|
|
|
|
|
|
|
use std::collections::VecDeque;
|
2022-05-08 22:25:33 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use parking_lot::Mutex;
|
2022-04-18 03:56:16 +00:00
|
|
|
|
|
|
|
pub struct StagingPool<T> {
|
|
|
|
device: Arc<wgpu::Device>,
|
2022-04-19 02:10:24 +00:00
|
|
|
stage_size: usize,
|
|
|
|
spillover: Mutex<VecDeque<CopyBuffer<T>>>,
|
2022-04-18 03:56:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Clone> StagingPool<T> {
|
|
|
|
pub fn new(device: Arc<wgpu::Device>, stage_size: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
device,
|
2022-04-19 02:10:24 +00:00
|
|
|
stage_size,
|
2022-04-18 03:56:16 +00:00
|
|
|
spillover: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 07:20:50 +00:00
|
|
|
pub fn flush<'a>(
|
2022-04-19 02:10:24 +00:00
|
|
|
&self,
|
|
|
|
encoder: &mut wgpu::CommandEncoder,
|
2022-04-18 07:20:50 +00:00
|
|
|
get_dst: impl Fn(&T) -> CopyDest<'a>,
|
2022-04-18 03:56:16 +00:00
|
|
|
on_complete: impl Fn(T),
|
|
|
|
) {
|
2022-05-08 22:25:33 +00:00
|
|
|
let mut spillover = self.spillover.lock();
|
2022-04-19 02:10:24 +00:00
|
|
|
if spillover.is_empty() {
|
2022-04-18 03:56:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-19 02:10:24 +00:00
|
|
|
let src = self.device.create_buffer(&wgpu::BufferDescriptor {
|
|
|
|
label: Some("staging buffer"),
|
|
|
|
size: self.stage_size as wgpu::BufferAddress,
|
|
|
|
usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_SRC,
|
|
|
|
mapped_at_creation: true,
|
|
|
|
});
|
|
|
|
|
2022-04-18 03:56:16 +00:00
|
|
|
let mut src_view = src.slice(..).get_mapped_range_mut();
|
|
|
|
let mut src_offset = 0;
|
2022-04-19 02:10:24 +00:00
|
|
|
while let Some(copy) = spillover.pop_back() {
|
2022-04-18 03:56:16 +00:00
|
|
|
let (copy, next) = copy.eat(&mut src_view[src_offset..]);
|
|
|
|
let dst = get_dst(©.target);
|
|
|
|
let dst_offset = dst.offset + copy.offset;
|
2022-04-19 02:10:24 +00:00
|
|
|
|
|
|
|
encoder.copy_buffer_to_buffer(
|
|
|
|
&src,
|
2022-04-18 03:56:16 +00:00
|
|
|
src_offset as wgpu::BufferAddress,
|
|
|
|
dst.buffer,
|
|
|
|
dst_offset as wgpu::BufferAddress,
|
|
|
|
copy.size as wgpu::BufferAddress,
|
|
|
|
);
|
|
|
|
|
|
|
|
src_offset += copy.size;
|
|
|
|
|
|
|
|
if let Some(next) = next {
|
2022-04-19 02:10:24 +00:00
|
|
|
spillover.push_back(next);
|
2022-04-18 03:56:16 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
on_complete(copy.target);
|
|
|
|
}
|
|
|
|
}
|
2022-04-19 07:48:05 +00:00
|
|
|
|
|
|
|
drop(src_view);
|
|
|
|
src.unmap();
|
2022-04-18 03:56:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 02:10:24 +00:00
|
|
|
pub fn queue_copies(&self, copies: Vec<CopyBuffer<T>>) {
|
2022-05-08 22:25:33 +00:00
|
|
|
let mut spillover = self.spillover.lock();
|
2022-04-19 02:10:24 +00:00
|
|
|
spillover.reserve(copies.len());
|
|
|
|
spillover.extend(copies.into_iter());
|
2022-04-18 03:56:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CopyDest<'a> {
|
|
|
|
/// The destination buffer.
|
|
|
|
pub buffer: &'a wgpu::Buffer,
|
|
|
|
|
|
|
|
/// The destination offset *in bytes.*
|
|
|
|
pub offset: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CopyInfo<T> {
|
|
|
|
/// The target of the copy.
|
|
|
|
pub target: T,
|
|
|
|
|
|
|
|
/// The offset for the destination, including the [CopyDest] offset.
|
|
|
|
pub offset: usize,
|
|
|
|
|
|
|
|
/// The copy size *in bytes.*
|
|
|
|
pub size: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct CopyBuffer<T> {
|
|
|
|
/// The target of the copy.
|
|
|
|
pub target: T,
|
|
|
|
|
|
|
|
/// The offset for the destination, including the [CopyDest] offset.
|
|
|
|
pub offset: usize,
|
|
|
|
|
|
|
|
/// The CPU memory for the copy.
|
|
|
|
pub data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Clone> CopyBuffer<T> {
|
|
|
|
pub fn eat(self, dst: &mut [u8]) -> (CopyInfo<T>, Option<Self>) {
|
|
|
|
let Self {
|
|
|
|
target,
|
|
|
|
offset,
|
|
|
|
mut data,
|
|
|
|
} = self;
|
|
|
|
|
|
|
|
let dst_size = dst.len();
|
|
|
|
let size = data.len();
|
|
|
|
|
|
|
|
if dst_size >= size {
|
|
|
|
dst[0..size].copy_from_slice(&data);
|
2022-04-18 07:20:50 +00:00
|
|
|
let info = CopyInfo {
|
|
|
|
target,
|
|
|
|
offset,
|
|
|
|
size,
|
|
|
|
};
|
2022-04-18 03:56:16 +00:00
|
|
|
(info, None)
|
|
|
|
} else {
|
|
|
|
let remainder = data.split_off(dst_size);
|
|
|
|
dst.copy_from_slice(&data);
|
|
|
|
let info = CopyInfo {
|
|
|
|
target: target.clone(),
|
|
|
|
offset,
|
|
|
|
size: dst_size,
|
|
|
|
};
|
|
|
|
let offset = offset + dst_size;
|
|
|
|
let next = Self {
|
|
|
|
target,
|
|
|
|
offset,
|
|
|
|
data: remainder,
|
|
|
|
};
|
|
|
|
(info, Some(next))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|