From b8658b71861318fc591bcfebb2bce00717640ea3 Mon Sep 17 00:00:00 2001 From: Albin Chaboissier Date: Sun, 1 Nov 2026 17:19:25 +0100 Subject: [PATCH] Parallel generation --- src/main.rs | 113 +++++++++++++++++++++++++-------------------- src/producers.rs | 22 ++++----- src/voxel_cache.rs | 6 +++ 3 files changed, 80 insertions(+), 61 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7fee126..0830df0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,8 @@ use glam::Mat4; use glam::Vec3; use glam::Vec4; use itertools::Itertools; +use rayon::iter::IntoParallelRefIterator; +use rayon::iter::ParallelIterator; use wgpu::BindGroupLayout; use wgpu::Buffer; use wgpu::BufferUsages; @@ -48,6 +50,8 @@ use winit::window::WindowId; use crate::camera::Camera; use crate::egui_renderer::EguiRenderer; +use crate::gpu::radix_sort; +use crate::gpu::radix_sort::RadixSorter; use crate::gpu_profiler::Frame; use crate::gpu_profiler::Profile; use crate::gpu_profiler::ProfilingEncoder; @@ -215,7 +219,7 @@ impl State //let elt_count = 100_000; //let elt_count = (21 * 4096) as u32; //let elt_count = 3000 as u32; - let elt_count_real = 500_000; + let elt_count_real = 1_000_000; let count_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("count_buffer"), usage: wgpu::BufferUsages::STORAGE, @@ -227,7 +231,7 @@ impl State let org_big_value_buffer = (0..(elt_count as u32)).collect::>(); let org_big_key_buffer = org_big_value_buffer .iter() - .map(|_| random::()) + .map(|_| rand::random::()) //.map(|_| random_range(1..256)) .collect::>(); @@ -276,6 +280,7 @@ impl State &big_key_buffer, &big_value_buffer, Some(&count_buffer), + None, &mut encoder, radix_sort::RadixSortDirection::Ascending, ); @@ -389,8 +394,8 @@ impl State let total_chunk_count = terrain_height * terrain_width * terrain_alt; let structure_table = FixedStructureTable::new(&device, total_chunk_count, &layout); - let structure_table_request_buffer = CacheRequestBuffer::new(&device, 1024); - let structure_table_insertion_data = CacheInsertionData::new(&device, 1024, &layout); + let structure_table_request_buffer = CacheRequestBuffer::new(&device, 128); + let structure_table_insertion_data = CacheInsertionData::new(&device, 128, &layout); let chunk_instances = (0..terrain_alt) .cartesian_product(0..terrain_height) .cartesian_product(0..terrain_width) @@ -1151,24 +1156,24 @@ impl State } // ~~ Build usage buffer histogram - // let time_stamp = self.voxel_cache.lock().current_timestamp(); - // let cloned_usage_histogram = self.usage_vec.clone(); - // DownloadBuffer::read_buffer( - // &self.device, - // &self.queue, - // &self.voxel_cache.lock().usage_buffer.slice(..), - // move |slice| { - // let slice = slice.unwrap(); - // let cache_slice: &[u32] = bytemuck::cast_slice(&slice); - // let mut v = vec![0; 100]; - // for x in cache_slice.iter() - // { - // let diff = (time_stamp - x).min(99); - // v[diff as usize] += 1; - // } - // *cloned_usage_histogram.lock() = v; - // }, - // ); + let time_stamp = self.voxel_cache.lock().current_timestamp(); + let cloned_usage_histogram = self.usage_vec.clone(); + DownloadBuffer::read_buffer( + &self.device, + &self.queue, + &self.voxel_cache.lock().usage_timestamps().slice(..), + move |slice| { + let slice = slice.unwrap(); + let cache_slice: &[u32] = bytemuck::cast_slice(&slice); + let mut v = vec![0; 100]; + for x in cache_slice.iter() + { + let diff = (time_stamp - x).min(99); + v[diff as usize] += 1; + } + *cloned_usage_histogram.lock() = v; + }, + ); // DownloadBuffer::read_buffer( // &self.device, @@ -1274,26 +1279,30 @@ impl State return; } - //println!("Cache : {} requests", request_informations.len()); + println!("Cache : {} requests", request_informations.len()); let mut insertion_infos = vec![]; let mut color_attributes = vec![]; let mut normal_attributes: Vec<[[f32; 3]; 64]> = vec![]; let mut location_pages = vec![]; - for request in request_informations.iter() - { - let (colors, normals, insertion_info, location) = fullfill_request( - cloned_generator.as_ref(), - request, - cloned_map.as_ref(), - ); - - insertion_infos.push(insertion_info); - color_attributes.push(colors); - normal_attributes.push(std::array::from_fn(|i| normals[i].into())); - location_pages.push(location); - } + request_informations + .par_iter() + .map(|request| { + fullfill_request( + cloned_generator.as_ref(), + request, + cloned_map.as_ref(), + ) + }) + .collect::>() + .into_iter() + .for_each(|(colors, normals, insertion_info, location)| { + insertion_infos.push(insertion_info); + color_attributes.push(colors); + normal_attributes.push(std::array::from_fn(|i| normals[i].into())); + location_pages.push(location); + }); cloned_queue.write_buffer( cloned_insertion_data.insertion_information_buffer(), @@ -1353,26 +1362,30 @@ impl State return; } - //println!("Structure table: {} requests", request_informations.len()); + println!("Structure table: {} requests", request_informations.len()); let mut insertion_infos = vec![]; let mut color_attributes = vec![]; let mut normal_attributes: Vec<[[f32; 3]; 64]> = vec![]; let mut location_pages = vec![]; - for request in request_informations.iter() - { - let (colors, normals, insertion_info, location) = fullfill_request( - cloned_generator.as_ref(), - request, - cloned_map.as_ref(), - ); - - insertion_infos.push(insertion_info); - color_attributes.push(colors); - normal_attributes.push(std::array::from_fn(|i| normals[i].into())); - location_pages.push(location); - } + request_informations + .par_iter() + .map(|request| { + fullfill_request( + cloned_generator.as_ref(), + request, + cloned_map.as_ref(), + ) + }) + .collect::>() + .into_iter() + .for_each(|(colors, normals, insertion_info, location)| { + insertion_infos.push(insertion_info); + color_attributes.push(colors); + normal_attributes.push(std::array::from_fn(|i| normals[i].into())); + location_pages.push(location); + }); cloned_queue.write_buffer( cloned_st_insertion_data.insertion_information_buffer(), diff --git a/src/producers.rs b/src/producers.rs index c88ab92..eda7362 100644 --- a/src/producers.rs +++ b/src/producers.rs @@ -510,20 +510,20 @@ where chunk_pos: (usize, usize, usize), ) -> ProducedChunk { - // let sdf = |x: f32, y: f32, z: f32| { - // Vec3::new((x % 1024.) - 512., (y % 1024.) - 512., (z % 1024.) - 512.).length() - 512. - // }; - let sdf = |x: f32, y: f32, z: f32| { - let tx = x / 512.; - let ty = y / 512.; - let tz = z / 512.; - - let val = self.perlin.get([tx as f64, ty as f64, tz as f64]); - let dist = val * 128.; - return dist as f32; + Vec3::new((x % 1024.) - 512., (y % 1024.) - 512., (z % 1024.) - 512.).length() - 512. }; + // let sdf = |x: f32, y: f32, z: f32| { + // let tx = x / 512.; + // let ty = y / 512.; + // let tz = z / 512.; + // + // let val = self.perlin.get([tx as f64, ty as f64, tz as f64]); + // let dist = val * 128.; + // return dist as f32; + // }; + let node_size = N.pow((self.chunk_power - depth) as u32); let child_size = node_size / N; let global_size = N.pow(self.chunk_power as u32); diff --git a/src/voxel_cache.rs b/src/voxel_cache.rs index b182b9f..f90ddd1 100644 --- a/src/voxel_cache.rs +++ b/src/voxel_cache.rs @@ -351,6 +351,12 @@ where { &self.raytrace_bind_group } + + // For debugging + pub fn usage_timestamps(&self) -> &wgpu::Buffer + { + self.evictor.timestamp_pages_buffer() + } } // Utilities