diff --git a/src/gfx/buffer.rs b/src/gfx/buffer.rs index 61affe4..1b4c73c 100644 --- a/src/gfx/buffer.rs +++ b/src/gfx/buffer.rs @@ -5,13 +5,17 @@ use super::Context; #[derive(Debug)] pub struct BulkBufferBuilder<'a> { - descriptors: Vec>, + order: Vec<(bool, usize)>, + init_descriptors: Vec>, + descriptors: Vec>, current_usage: wgpu::BufferUsages, } impl<'a> BulkBufferBuilder<'a> { pub fn new() -> Self { Self { + order: vec![], + init_descriptors: vec![], descriptors: vec![], current_usage: wgpu::BufferUsages::UNIFORM, } @@ -22,26 +26,48 @@ impl<'a> BulkBufferBuilder<'a> { self } - pub fn with_buffer(mut self, label: &'a str, contents: &'a [u8]) -> Self { + pub fn with_buffer(mut self, label: &'a str, size: u64, mapped: bool) -> Self { + let descriptor = wgpu::BufferDescriptor { + label: Some(label), + size, + usage: self.current_usage, + mapped_at_creation: mapped, + }; + + self.order.push((false, self.descriptors.len())); + self.descriptors.push(descriptor); + self + } + + pub fn with_init_buffer(mut self, label: &'a str, contents: &'a [u8]) -> Self { let descriptor = wgpu::util::BufferInitDescriptor { label: Some(label), contents, usage: self.current_usage, }; - self.descriptors.push(descriptor); + self.order.push((true, self.init_descriptors.len())); + self.init_descriptors.push(descriptor); self } - pub fn with_bytemuck_buffer(self, label: &'a str, contents: &'a [A]) -> Self { - self.with_buffer(label, bytemuck::cast_slice(contents)) + pub fn with_init_buffer_bm(self, label: &'a str, contents: &'a [A]) -> Self { + self.with_init_buffer(label, bytemuck::cast_slice(contents)) } pub fn build(self, context: &Context) -> Vec { + let device = &context.device; let mut buffers = vec![]; - for descriptor in self.descriptors { - buffers.push(context.device.create_buffer_init(&descriptor)); + for (init, index) in self.order { + let buffer = if init { + device.create_buffer_init(&(self.init_descriptors[index])) + } else { + device.create_buffer(&(self.descriptors[index])) + }; + + buffers.push(buffer); } + buffers } } diff --git a/src/voxel/brickmap.rs b/src/voxel/brickmap.rs index b3207f1..186f330 100644 --- a/src/voxel/brickmap.rs +++ b/src/voxel/brickmap.rs @@ -92,12 +92,6 @@ impl BrickmapManager { let mut feedback_data = vec![0u32; 4 + 4 * max_requested_brickmaps as usize]; feedback_data[0] = max_requested_brickmaps; let feedback_data_u8 = bytemuck::cast_slice(&feedback_data); - let feedback_result_buffer = device.create_buffer(&wgpu::BufferDescriptor { - label: Some("Feedback Read"), - size: feedback_data_u8.len() as u64, - usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, - mapped_at_creation: false, - }); let mut brickgrid_upload_data = vec![0u32; 4 + 4 * max_uploaded_brickmaps as usize]; brickgrid_upload_data[0] = max_uploaded_brickmaps; @@ -108,19 +102,21 @@ impl BrickmapManager { let brickmap_staged = Vec::new(); let mut buffers = gfx::BulkBufferBuilder::new() - .with_bytemuck_buffer("Brick World State", &[state_uniform]) + .with_init_buffer_bm("Brick World State", &[state_uniform]) .set_usage(wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST) - .with_bytemuck_buffer("Brickgrid", &brickgrid) - .with_bytemuck_buffer("Brickmap Cache", &brickmap_cache) - .with_bytemuck_buffer("Shading Table", &shading_table) - .with_bytemuck_buffer("Brickgrid Unpack", &brickgrid_upload_data) - .with_bytemuck_buffer("Brickmap Unpack", &brickmap_upload_data) + .with_init_buffer_bm("Brickgrid", &brickgrid) + .with_init_buffer_bm("Brickmap Cache", &brickmap_cache) + .with_init_buffer_bm("Shading Table", &shading_table) + .with_init_buffer_bm("Brickgrid Unpack", &brickgrid_upload_data) + .with_init_buffer_bm("Brickmap Unpack", &brickmap_upload_data) .set_usage( wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC, ) - .with_buffer("Feedback", feedback_data_u8) + .with_init_buffer("Feedback", feedback_data_u8) + .set_usage(wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ) + .with_buffer("Feedback Read", feedback_data_u8.len() as u64, false) .build(context); Self { @@ -129,7 +125,6 @@ impl BrickmapManager { brickmap_cache_map, brickmap_cache_idx: 0, shading_table_allocator, - feedback_result_buffer, unpack_max_count: max_uploaded_brickmaps as usize, brickgrid_staged, brickmap_staged, @@ -141,6 +136,7 @@ impl BrickmapManager { brickgrid_unpack_buffer: buffers.remove(0), brickmap_unpack_buffer: buffers.remove(0), feedback_buffer: buffers.remove(0), + feedback_result_buffer: buffers.remove(0), } }