From cf73267bb6811ce7eae0c9da065e5af47cd6231f Mon Sep 17 00:00:00 2001 From: BGC-MBP14 Date: Wed, 21 Aug 2024 21:18:35 -0500 Subject: [PATCH] Revert "simple frustum culling" This reverts commit e92ee6292ae097bdaa4e2349a92b4b20e0596247. --- src/main.rs | 10 +++++++++- src/renderer.rs | 24 ++++-------------------- src/world.rs | 31 +++++++++++++++++++++++-------- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index f934eb5..bd71e95 100644 --- a/src/main.rs +++ b/src/main.rs @@ -254,9 +254,17 @@ impl ApplicationHandler for Game<'_> { self.world.spawn_chunk_updates(); if self.world.spawn_mesh_updates() { + let t = std::time::Instant::now(); + //println!("Meshing..."); self.world.get_all_chunk_meshes(&renderer.device); + //println!("{:?}, {:?}", verts, indices); + println!("{:?}", t.elapsed()); + //println!("Pushing meshes to GPU..."); + //renderer.push_indices(indices, index_offsets); + //println!("Done!"); + //self.world.need_mesh_update.lock().unwrap().clear(); + println!("{:?}", t.elapsed()); } - //println!("through"); match renderer.render(&self.world) { diff --git a/src/renderer.rs b/src/renderer.rs index f0aaba4..1f81ead 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -199,11 +199,9 @@ impl<'a> Renderer<'a> { println!("Using backend {}", adapter.get_info().backend.to_str().to_uppercase()); - let required_features = wgpu::Features::default();//wgpu::Features::CONSERVATIVE_RASTERIZATION; - let (device, queue) = adapter.request_device( &wgpu::DeviceDescriptor { - required_features, + required_features: wgpu::Features::PUSH_CONSTANTS, required_limits: wgpu::Limits::default(), ..Default::default() }, @@ -538,29 +536,15 @@ impl<'a> Renderer<'a> { // SEND IT ALL IN - use glam::{Vec3, Mat3}; - let player = world.entities.read_lock(world.player).unwrap(); - let pos = player.pos; - let facing = player.facing; - // improved frustum culling can be done if the fov is taken into account and culling happens on the normals of the 4 planes of the camera's view - drop(player); - for handle in world.chunks.iter() { - - // DO FRUSTUM CULLING - let chunk = world.chunks.read_lock(handle).unwrap(); - if (Vec3::new(chunk.x, chunk.y, chunk.z) - pos).dot(facing) < -23.0 { - //println!("skipped {} {} {}", chunk.x, chunk.y, chunk.z); - continue; - } - - + // the .slice(..) will control frustum culling when the time comes + for (i, handle) in world.chunks.iter().enumerate() { render_pass.set_pipeline(self.pipeline.as_ref().unwrap()); // 2. render_pass.set_bind_group(0, &self.frame_data_bind_group, &[]); for (i, texset) in self.texture_sets.iter().enumerate() { render_pass.set_bind_group((i+1) as u32, &texset.bind_group, &[]); } - + let chunk = world.chunks.read_lock(handle).unwrap(); render_pass.set_vertex_buffer(0, chunk.vertex_buffer.as_ref().expect("A vertex buffer was never pushed to the GPU!").slice(..)); render_pass.set_index_buffer(chunk.index_buffer.as_ref().expect("An index buffer was never pushed to the GPU!").slice(..), wgpu::IndexFormat::Uint32); // 1. render_pass.draw_indexed(0..chunk.index_count, 0, 0..1); // 2. diff --git a/src/world.rs b/src/world.rs index 4450c65..83954a1 100644 --- a/src/world.rs +++ b/src/world.rs @@ -158,11 +158,11 @@ impl World { let block_properties = &self.block_properties; let tp = &self.thread_pool; - self.thread_pool.install(|| { + //self.thread_pool.install(|| { let mut wlock = chunk.write().unwrap(); wlock.make_mesh(block_properties, tp); wlock.ready_to_display = true; - }); + //}); } } } @@ -185,15 +185,30 @@ impl World { } } - pub fn get_all_chunk_meshes(&mut self, device: &wgpu::Device) { + pub fn get_all_chunk_meshes(&mut self, device: &impl wgpu::util::DeviceExt) { + // let mut vertices = Vec::::new(); + // let mut indices = Vec::::new(); + // let mut indices_offsets = Vec::::new(); + // let mut index_offset = 0u32; + + // for handle in self.need_mesh_update.lock().unwrap().iter() { + // println!("Updated {:?}", handle); + // self.chunks.write_lock(*handle).unwrap().make_mesh(&self.block_properties, &self.thread_pool); + // } + for handle in self.chunks.iter() { - let chunk = self.chunks.fetch_lock(handle).unwrap(); - if chunk.read().unwrap().vertex_buffer.is_none() { - self.thread_pool.install(||{ - chunk.write().unwrap().make_vertex_buffer(device); - }); + let mut chunk = self.chunks.write_lock(handle).unwrap(); + if chunk.vertex_buffer.is_none() { + chunk.make_vertex_buffer(device); } + //let v = &chunk.mesh; + //vertices.extend(v); + //indices.extend(chunk.get_indices(index_offset)); + //index_offset += chunk.mesh.len() as u32; + //indices_offsets.push(index_offset); } + + //(indices, indices_offsets) } fn do_physics(&self, dt: f32, e: ArenaHandle) {