From 27b61c624d298e59edf06da34c015310c105cbfa Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Mon, 26 Jun 2023 15:10:31 +0100 Subject: [PATCH] Implement clippy suggestions --- src/core/app.rs | 2 +- src/math.rs | 2 +- src/render/bind_group.rs | 2 +- src/voxel/brickmap.rs | 49 +++++++++++++++++-------------------- src/voxel/voxel_renderer.rs | 2 +- src/voxel/world.rs | 2 +- 6 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/core/app.rs b/src/core/app.rs index 2578eb0..0ad3173 100644 --- a/src/core/app.rs +++ b/src/core/app.rs @@ -89,7 +89,7 @@ impl App { } if window_id == self.window.id() => match event { WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, _ => { - camera_controller.process_events(&event); + camera_controller.process_events(event); } }, Event::MainEventsCleared => { diff --git a/src/math.rs b/src/math.rs index 6bf7565..5547b27 100644 --- a/src/math.rs +++ b/src/math.rs @@ -1,7 +1,7 @@ /// Linear interpolation. #[inline] pub fn lerp(a: f32, b: f32, w: f32) -> f32 { - assert!(0.0 <= w && w <= 1.0); + assert!((0.0..=1.0).contains(&w)); a + (b - a) * w } diff --git a/src/render/bind_group.rs b/src/render/bind_group.rs index 5258419..8b34342 100644 --- a/src/render/bind_group.rs +++ b/src/render/bind_group.rs @@ -89,7 +89,7 @@ impl<'a> BindGroupBuilder<'a> { .device .create_bind_group(&wgpu::BindGroupDescriptor { label: self.label, - layout: &self.layout.unwrap(), + layout: self.layout.unwrap(), entries: self.entries.as_slice(), }) } diff --git a/src/voxel/brickmap.rs b/src/voxel/brickmap.rs index 3a88719..d7e1b4d 100644 --- a/src/voxel/brickmap.rs +++ b/src/voxel/brickmap.rs @@ -38,14 +38,13 @@ pub struct BrickmapManager { // - Brickworld system impl BrickmapManager { pub fn new(context: &render::Context) -> Self { - let mut state_uniform = WorldState::default(); - state_uniform.brickmap_cache_dims = [32, 32, 32]; + let state_uniform = WorldState { + brickmap_cache_dims: [32, 32, 32], + ..Default::default() + }; - let mut brickmap_cache = Vec::::with_capacity(usize::pow(32, 3)); - brickmap_cache.resize(brickmap_cache.capacity(), Brickmap::default()); - - let mut brickgrid = Vec::::with_capacity(usize::pow(32, 3)); - brickgrid.resize(brickgrid.capacity(), 1); + let brickmap_cache = vec![Brickmap::default(); usize::pow(32, 3)]; + let brickgrid = vec![1u32; usize::pow(32, 3)]; let device = &context.device; let state_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { @@ -67,9 +66,7 @@ impl BrickmapManager { }); let shading_table_allocator = ShadingTableAllocator::new(4, u32::pow(2, 24)); - let shading_table_element_count = shading_table_allocator.total_elements as usize; - let mut shading_table = Vec::::with_capacity(shading_table_element_count); - shading_table.resize(shading_table.capacity(), 0); + let shading_table = vec![0u32; shading_table_allocator.total_elements as usize]; let shading_table_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: None, contents: bytemuck::cast_slice(&shading_table), @@ -168,7 +165,7 @@ impl BrickmapManager { assert_eq!(block.len(), 512); // Cull interior voxels - let mut bitmask_data = [0xFFFFFFFF as u32; 16]; + let mut bitmask_data = [0xFFFFFFFF_u32; 16]; let mut albedo_data = Vec::::new(); for z in 0..8 { // Each z level contains two bitmask segments of voxels @@ -185,17 +182,18 @@ impl BrickmapManager { // A voxel is on the surface if at least one of it's // cardinal neighbours is non-solid. Also for simplicity // if it's on the edge of the chunk - let surface_voxel: bool; - if x == 0 || x == 7 || y == 0 || y == 7 || z == 0 || z == 7 { - surface_voxel = true; - } else { - surface_voxel = !(block[idx + 1] == empty_voxel - && block[idx - 1] == empty_voxel - && block[idx + 8] == empty_voxel - && block[idx - 8] == empty_voxel - && block[idx + 64] == empty_voxel - && block[idx - 64] == empty_voxel); - } + // TODO: Account for neighbours in other blocks + let surface_voxel = + if x == 0 || x == 7 || y == 0 || y == 7 || z == 0 || z == 7 { + true + } else { + !(block[idx + 1] == empty_voxel + && block[idx - 1] == empty_voxel + && block[idx + 8] == empty_voxel + && block[idx - 8] == empty_voxel + && block[idx + 64] == empty_voxel + && block[idx - 64] == empty_voxel) + }; // Set the appropriate bit in the z entry and add the // shading data @@ -211,7 +209,7 @@ impl BrickmapManager { } } } - let offset = 2 * z as usize; + let offset = 2 * z; bitmask_data[offset] = (entry & 0xFFFFFFFF).try_into().unwrap(); bitmask_data[offset + 1] = ((entry >> 32) & 0xFFFFFFFF).try_into().unwrap(); } @@ -298,7 +296,7 @@ impl ShadingBucket { fn contains_address(&self, address: u32) -> bool { let min = self.global_offset; let max = min + self.slot_count * self.slot_size; - return min <= address && address < max; + min <= address && address < max } fn try_alloc(&mut self) -> Option { @@ -307,8 +305,7 @@ impl ShadingBucket { self.used.push(bucket_index); // Convert the bucket index into a global address - let address = self.global_offset + bucket_index * self.slot_size; - return Some(address); + Some(self.global_offset + bucket_index * self.slot_size) } fn try_dealloc(&mut self, address: u32) -> Result<(), &str> { diff --git a/src/voxel/voxel_renderer.rs b/src/voxel/voxel_renderer.rs index f8ea15f..46f95aa 100644 --- a/src/voxel/voxel_renderer.rs +++ b/src/voxel/voxel_renderer.rs @@ -28,7 +28,7 @@ impl VoxelRenderer { | wgpu::TextureUsages::STORAGE_BINDING, ) .with_shader_visibility(wgpu::ShaderStages::FRAGMENT | wgpu::ShaderStages::COMPUTE) - .build(&context); + .build(context); log::info!("Creating render pipeline..."); let render_pipeline = diff --git a/src/voxel/world.rs b/src/voxel/world.rs index 459dbff..2f26d1c 100644 --- a/src/voxel/world.rs +++ b/src/voxel/world.rs @@ -22,7 +22,7 @@ impl Chunk { assert_eq!(self.blocks.len(), 32768); let block = &mut self.blocks[block_idx]; - if block.len() == 0 { + if block.is_empty() { // Extract relevant noise values from the chunk let mut noise_vals = Vec::new();