From 2555ec55b4b31cdc43e0a865cf5d9961b6d4e61a Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Tue, 25 Apr 2023 13:54:33 +0100 Subject: [PATCH] Start work on brickgrids --- assets/shaders/voxel_volume.wgsl | 16 +++++++++++----- src/voxel/brickmap.rs | 24 ++++++++++++++++++++++++ src/voxel/voxel_renderer.rs | 14 ++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/assets/shaders/voxel_volume.wgsl b/assets/shaders/voxel_volume.wgsl index ab98094..026f6a2 100644 --- a/assets/shaders/voxel_volume.wgsl +++ b/assets/shaders/voxel_volume.wgsl @@ -1,8 +1,9 @@ @group(0) @binding(0) var output: texture_storage_2d; @group(0) @binding(1) var world_state: WorldState; -@group(0) @binding(2) var brickmap_cache: array; -@group(0) @binding(3) var shading_table: array; -@group(0) @binding(4) var camera: Camera; +@group(0) @binding(2) var brickgrid: array; +@group(0) @binding(3) var brickmap_cache: array; +@group(0) @binding(4) var shading_table: array; +@group(0) @binding(5) var camera: Camera; struct ShadingElement { albedo: u32, @@ -75,9 +76,14 @@ fn point_inside_aabb(p: vec3) -> bool { } fn voxel_hit(p: vec3) -> bool { - let brickmap_index = to_1d_index(p / 8, vec3(world_state.brickmap_cache_dims)); + let brickgrid_index = to_1d_index(p / 8, vec3(world_state.brickmap_cache_dims)); + let brick_ptr = brickgrid[brickgrid_index]; + if ((brick_ptr & 1u) == 0u){ + return false; + } + let local_index = to_1d_index(p % 8, vec3(8)); - let bitmask_segment = brickmap_cache[brickmap_index].bitmask[local_index / 32u]; + let bitmask_segment = brickmap_cache[brick_ptr >> 8u].bitmask[local_index / 32u]; return (bitmask_segment >> (local_index % 32u) & 1u) != 0u; } diff --git a/src/voxel/brickmap.rs b/src/voxel/brickmap.rs index 94f7e08..0ac9f59 100644 --- a/src/voxel/brickmap.rs +++ b/src/voxel/brickmap.rs @@ -31,6 +31,8 @@ struct WorldState { pub struct BrickmapManager { state_uniform: WorldState, state_buffer: wgpu::Buffer, + brickgrid: Vec, + brickgrid_buffer: wgpu::Buffer, brickmap_cache: Vec, brickmap_buffer: wgpu::Buffer, shading_table: Vec, @@ -45,6 +47,9 @@ impl BrickmapManager { let mut brickmap_cache = Vec::::with_capacity(32768); brickmap_cache.resize(32768, Brickmap::default()); + let mut brickgrid = Vec::::with_capacity(32768); + brickgrid.resize(brickgrid.capacity(), 0); + let device = &context.device; let state_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: None, @@ -52,6 +57,12 @@ impl BrickmapManager { usage: wgpu::BufferUsages::UNIFORM, }); + let brickgrid_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: None, + contents: bytemuck::cast_slice(&brickgrid), + usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST, + }); + let brickmap_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: None, contents: bytemuck::cast_slice(&brickmap_cache), @@ -70,6 +81,8 @@ impl BrickmapManager { Self { state_uniform, state_buffer, + brickgrid, + brickgrid_buffer, brickmap_cache, brickmap_buffer, shading_table, @@ -83,6 +96,8 @@ impl BrickmapManager { .try_into() .unwrap(); let shading_idx = idx * 512; + self.brickgrid + .splice(idx..idx + 1, [((idx as u32) << 8) + 1]); self.brickmap_cache[idx].bitmask = *data; self.brickmap_cache[idx].shading_table_offset = shading_idx as u32; self.shading_table.splice( @@ -103,6 +118,15 @@ impl BrickmapManager { 0, bytemuck::cast_slice(&self.shading_table), ); + queue.write_buffer( + &self.brickgrid_buffer, + 0, + bytemuck::cast_slice(&self.brickgrid), + ) + } + + pub fn get_brickgrid_buffer(&self) -> &wgpu::Buffer { + &self.brickgrid_buffer } pub fn get_worldstate_buffer(&self) -> &wgpu::Buffer { diff --git a/src/voxel/voxel_renderer.rs b/src/voxel/voxel_renderer.rs index 1e704d9..c9339c2 100644 --- a/src/voxel/voxel_renderer.rs +++ b/src/voxel/voxel_renderer.rs @@ -64,6 +64,10 @@ impl VoxelRenderer { let sphere_center = glam::vec3(3.5, 3.5, 3.5); let sphere_r2 = u32::pow(4, 2) as f32; for chunk_idx in 0..32768 { + if chunk_idx % 3 == 0 || chunk_idx % 5 == 0 || chunk_idx % 7 == 0 { + continue; + } + let chunk_pos = glam::uvec3(chunk_idx % 32, (chunk_idx / 32) % 32, chunk_idx / 1024); let mut bitmask_data = [0xFFFFFFFF as u32; 16]; let mut albedo_data = Vec::::new(); @@ -131,6 +135,15 @@ impl VoxelRenderer { }, None, ) + .with_entry( + wgpu::ShaderStages::COMPUTE, + wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Storage { read_only: true }, + has_dynamic_offset: false, + min_binding_size: None, + }, + None, + ) .with_entry( wgpu::ShaderStages::COMPUTE, wgpu::BindingType::Buffer { @@ -145,6 +158,7 @@ impl VoxelRenderer { .with_layout(&compute_layout) .with_entry(wgpu::BindingResource::TextureView(&render_texture.view)) .with_entry(brickmap_manager.get_worldstate_buffer().as_entire_binding()) + .with_entry(brickmap_manager.get_brickgrid_buffer().as_entire_binding()) .with_entry(brickmap_manager.get_brickmap_buffer().as_entire_binding()) .with_entry(brickmap_manager.get_shading_buffer().as_entire_binding()) .with_entry(camera_controller.get_buffer().as_entire_binding())