Start work on brickgrids

This commit is contained in:
Jarrod Doyle 2023-04-25 13:54:33 +01:00
parent 4091686b09
commit 2555ec55b4
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
3 changed files with 49 additions and 5 deletions

View File

@ -1,8 +1,9 @@
@group(0) @binding(0) var output: texture_storage_2d<rgba8unorm, write>; @group(0) @binding(0) var output: texture_storage_2d<rgba8unorm, write>;
@group(0) @binding(1) var<uniform> world_state: WorldState; @group(0) @binding(1) var<uniform> world_state: WorldState;
@group(0) @binding(2) var<storage, read> brickmap_cache: array<Brickmap>; @group(0) @binding(2) var<storage, read> brickgrid: array<u32>;
@group(0) @binding(3) var<storage, read> shading_table: array<ShadingElement>; @group(0) @binding(3) var<storage, read> brickmap_cache: array<Brickmap>;
@group(0) @binding(4) var<uniform> camera: Camera; @group(0) @binding(4) var<storage, read> shading_table: array<ShadingElement>;
@group(0) @binding(5) var<uniform> camera: Camera;
struct ShadingElement { struct ShadingElement {
albedo: u32, albedo: u32,
@ -75,9 +76,14 @@ fn point_inside_aabb(p: vec3<i32>) -> bool {
} }
fn voxel_hit(p: vec3<i32>) -> bool { fn voxel_hit(p: vec3<i32>) -> bool {
let brickmap_index = to_1d_index(p / 8, vec3<i32>(world_state.brickmap_cache_dims)); let brickgrid_index = to_1d_index(p / 8, vec3<i32>(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<i32>(8)); let local_index = to_1d_index(p % 8, vec3<i32>(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; return (bitmask_segment >> (local_index % 32u) & 1u) != 0u;
} }

View File

@ -31,6 +31,8 @@ struct WorldState {
pub struct BrickmapManager { pub struct BrickmapManager {
state_uniform: WorldState, state_uniform: WorldState,
state_buffer: wgpu::Buffer, state_buffer: wgpu::Buffer,
brickgrid: Vec<u32>,
brickgrid_buffer: wgpu::Buffer,
brickmap_cache: Vec<Brickmap>, brickmap_cache: Vec<Brickmap>,
brickmap_buffer: wgpu::Buffer, brickmap_buffer: wgpu::Buffer,
shading_table: Vec<u32>, shading_table: Vec<u32>,
@ -45,6 +47,9 @@ impl BrickmapManager {
let mut brickmap_cache = Vec::<Brickmap>::with_capacity(32768); let mut brickmap_cache = Vec::<Brickmap>::with_capacity(32768);
brickmap_cache.resize(32768, Brickmap::default()); brickmap_cache.resize(32768, Brickmap::default());
let mut brickgrid = Vec::<u32>::with_capacity(32768);
brickgrid.resize(brickgrid.capacity(), 0);
let device = &context.device; let device = &context.device;
let state_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { let state_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: None, label: None,
@ -52,6 +57,12 @@ impl BrickmapManager {
usage: wgpu::BufferUsages::UNIFORM, 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 { let brickmap_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: None, label: None,
contents: bytemuck::cast_slice(&brickmap_cache), contents: bytemuck::cast_slice(&brickmap_cache),
@ -70,6 +81,8 @@ impl BrickmapManager {
Self { Self {
state_uniform, state_uniform,
state_buffer, state_buffer,
brickgrid,
brickgrid_buffer,
brickmap_cache, brickmap_cache,
brickmap_buffer, brickmap_buffer,
shading_table, shading_table,
@ -83,6 +96,8 @@ impl BrickmapManager {
.try_into() .try_into()
.unwrap(); .unwrap();
let shading_idx = idx * 512; 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].bitmask = *data;
self.brickmap_cache[idx].shading_table_offset = shading_idx as u32; self.brickmap_cache[idx].shading_table_offset = shading_idx as u32;
self.shading_table.splice( self.shading_table.splice(
@ -103,6 +118,15 @@ impl BrickmapManager {
0, 0,
bytemuck::cast_slice(&self.shading_table), 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 { pub fn get_worldstate_buffer(&self) -> &wgpu::Buffer {

View File

@ -64,6 +64,10 @@ impl VoxelRenderer {
let sphere_center = glam::vec3(3.5, 3.5, 3.5); let sphere_center = glam::vec3(3.5, 3.5, 3.5);
let sphere_r2 = u32::pow(4, 2) as f32; let sphere_r2 = u32::pow(4, 2) as f32;
for chunk_idx in 0..32768 { 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 chunk_pos = glam::uvec3(chunk_idx % 32, (chunk_idx / 32) % 32, chunk_idx / 1024);
let mut bitmask_data = [0xFFFFFFFF as u32; 16]; let mut bitmask_data = [0xFFFFFFFF as u32; 16];
let mut albedo_data = Vec::<u32>::new(); let mut albedo_data = Vec::<u32>::new();
@ -131,6 +135,15 @@ impl VoxelRenderer {
}, },
None, 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( .with_entry(
wgpu::ShaderStages::COMPUTE, wgpu::ShaderStages::COMPUTE,
wgpu::BindingType::Buffer { wgpu::BindingType::Buffer {
@ -145,6 +158,7 @@ impl VoxelRenderer {
.with_layout(&compute_layout) .with_layout(&compute_layout)
.with_entry(wgpu::BindingResource::TextureView(&render_texture.view)) .with_entry(wgpu::BindingResource::TextureView(&render_texture.view))
.with_entry(brickmap_manager.get_worldstate_buffer().as_entire_binding()) .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_brickmap_buffer().as_entire_binding())
.with_entry(brickmap_manager.get_shading_buffer().as_entire_binding()) .with_entry(brickmap_manager.get_shading_buffer().as_entire_binding())
.with_entry(camera_controller.get_buffer().as_entire_binding()) .with_entry(camera_controller.get_buffer().as_entire_binding())