shitty radix

This commit is contained in:
2026-09-28 23:01:31 +02:00
parent e673c6dc0d
commit 3124978e3f
15 changed files with 1403 additions and 314 deletions
+123 -25
View File
@@ -2,8 +2,6 @@
#![feature(float_algebraic)]
use std::collections::HashMap;
use std::collections::HashSet;
use std::ops::Div;
use std::sync::Arc;
use std::sync::mpsc::sync_channel;
@@ -18,6 +16,7 @@ use glam::Mat4;
use glam::Vec3;
use glam::Vec4;
use itertools::Itertools;
use rand::random_range;
use rayon::iter::IndexedParallelIterator;
use rayon::iter::IntoParallelRefIterator;
use rayon::iter::ParallelIterator;
@@ -31,16 +30,10 @@ use wgpu::Features;
use wgpu::InstanceDescriptor;
use wgpu::InstanceFlags;
use wgpu::MemoryBudgetThresholds;
use wgpu::Operations;
use wgpu::Origin3d;
use wgpu::RenderPipeline;
use wgpu::ShaderStages;
use wgpu::Texture;
use wgpu::TextureUsages;
use wgpu::TextureView;
use wgpu::include_spirv;
use wgpu::include_wgsl;
use wgpu::util::BufferInitDescriptor;
use wgpu::util::DeviceExt;
use wgpu::util::DownloadBuffer;
use winit::application::ApplicationHandler;
@@ -58,6 +51,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::producers::ChunkedProducer;
use crate::producers::SdfGenerator;
use crate::producers::SineGenerator;
@@ -65,19 +60,15 @@ use crate::producers::TerrainGenerator;
use crate::sparse_tree::NTreeNodeLocator;
use crate::voxel_cache::VoxelAttribute;
use crate::voxel_cache::VoxelCache;
use crate::voxel_cache::data::CacheNodeRequest;
use crate::voxel_cache::data::CacheResponse;
use crate::voxel_cache::data::ColorBytes;
use crate::voxel_cache::data::ColorPoolElement;
use crate::voxel_cache::data::DestinationElement;
use crate::voxel_cache::data::LocationPoolElement;
use crate::voxel_cache::data::StructurePoolElement;
use crate::voxel_cache::indirect_buffer::NonZeroBufferCompactor;
use crate::voxel_cache::producer_interface::CacheProducerInterface;
use crate::voxel_cache::producer_interface::CacheRequest;
mod camera;
mod egui_renderer;
mod gpu;
mod producers;
mod sparse_tree;
mod voxel_cache;
@@ -116,8 +107,8 @@ struct State
gi_bg_layout: BindGroupLayout,
voxel_cache: Arc<Mutex<VoxelCache<4>>>,
cache_interface: Arc<CacheProducerInterface<4>>,
terrain_generator: Arc<SdfGenerator<4>>,
//terrain_generator: Arc<TerrainGenerator<4>>,
//terrain_generator: Arc<SdfGenerator<4>>,
terrain_generator: Arc<TerrainGenerator<4>>,
chunk_pos_map: Arc<HashMap<u32, (usize, usize, usize)>>,
instance_buffer: Buffer,
instance_count: usize,
@@ -126,6 +117,10 @@ struct State
frame_time: Arc<Mutex<f32>>,
insertion_debounce: bool,
terrain_width: usize,
terrain_height: usize,
terrain_alt: usize,
camera: Camera,
}
@@ -201,6 +196,92 @@ impl State
let mut encoder = device.create_command_encoder(&Default::default());
/*
let elt_count = 1_000_000u32;
//let elt_count = 1 << 20;
let count_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("count_buffer"),
contents: bytemuck::bytes_of(&elt_count),
usage: BufferUsages::STORAGE | BufferUsages::COPY_SRC,
});
//let elt_count = 1 << 20;
//let elt_count = 512;
let org_big_value_buffer = (0..(elt_count as u32)).collect::<Vec<_>>();
let org_big_key_buffer = org_big_value_buffer
.iter()
.map(|_| random_range(0..2_000_000u32))
.collect::<Vec<_>>();
let sorted_values = org_big_key_buffer
.iter()
.zip(org_big_value_buffer.iter())
.sorted_by_key(|(key, _)| *key)
.map(|(_, val)| *val)
.collect::<Vec<u32>>();
let big_key_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("big_key_buffer"),
contents: bytemuck::cast_slice(&org_big_key_buffer),
usage: BufferUsages::STORAGE | BufferUsages::COPY_SRC,
});
let big_value_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("big_value_buffer"),
contents: bytemuck::cast_slice(&org_big_value_buffer),
usage: BufferUsages::STORAGE | BufferUsages::COPY_SRC,
});
let sorter = RadixSorter::new(&device, 1 << 20);
//let sorter = RadixSorter::new(&device, 1_000_000);
//let sorter = RadixSorter::new(&device, 512);
sorter.sort(
&device,
&big_key_buffer,
&big_value_buffer,
//Some(&count_buffer),
None,
&mut encoder,
radix_sort::RadixSortDirection::Ascending,
);
queue.submit([encoder.finish()]);
wgpu::util::DownloadBuffer::read_buffer(
&device,
&queue,
&big_key_buffer.slice(..),
|clbk| {
let data_bytes = clbk.unwrap().to_vec();
let data = data_bytes
.chunks(4)
.map(|x| u32::from_ne_bytes([x[0], x[1], x[2], x[3]]))
.collect::<Vec<u32>>();
println!("{:?}", &data[0..512]);
},
);
wgpu::util::DownloadBuffer::read_buffer(
&device,
&queue,
&big_value_buffer.slice(..),
move |clbk| {
let data_bytes = clbk.unwrap().to_vec();
let data = data_bytes
.chunks(4)
.map(|x| u32::from_ne_bytes([x[0], x[1], x[2], x[3]]))
.collect::<Vec<u32>>();
for (gpu_key, key) in data.iter().zip(sorted_values.iter())
{
assert_eq!(gpu_key, key, "FUCK YOU");
}
println!("Finished check !");
},
);
device.poll(wgpu::wgt::PollType::Poll);
panic!();
*/
let size = window.inner_size();
let surface = instance.create_surface(window.clone()).unwrap();
@@ -217,8 +298,8 @@ impl State
VoxelCache::<4>::new(100_000, device.clone(), queue.clone(), attributes.clone());
let cache_interface = CacheProducerInterface::new(1024, &device, attributes.clone());
//let terrain_generator = TerrainGenerator::<4>::new(5, "vxls_height.tif", 0.2, "img.jpg");
let terrain_generator = SdfGenerator::<4>::new(5);
let terrain_generator = TerrainGenerator::<4>::new(5, "vxls_height.tif", 0.2, "img.jpg");
//let terrain_generator = SdfGenerator::<4>::new(5);
// let terrain_generator = TerrainGenerator::<4>::new(
// 5,
// "./pointe_percee/height.tif",
@@ -233,10 +314,18 @@ impl State
// );
//
let terrain_width = terrain_generator.chunk_width;
let terrain_height = terrain_generator.chunk_height;
let terrain_alt = terrain_generator.chunk_alt;
println!(
"Chunk count : {}",
terrain_width * terrain_height * terrain_width
);
let mut chunk_pos_map = HashMap::new();
let chunk_instances = (0..20)
.cartesian_product(0..20)
.cartesian_product(0..20)
let chunk_instances = (0..terrain_width)
.cartesian_product(0..terrain_height)
.cartesian_product(0..terrain_alt)
.map(|((x, z), y)| {
let chunk_id = voxel_cache.structure_table.allocate_structure(true);
//dbg!(chunk_id);
@@ -450,7 +539,10 @@ impl State
layout: Some(&pipeline_layout),
module: &shader_module,
entry_point: Some("ray_march_compute"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
compilation_options: wgpu::PipelineCompilationOptions {
zero_initialize_workgroup_memory: false,
..Default::default()
},
cache: None,
});
@@ -469,7 +561,10 @@ impl State
layout: Some(&gi_pipeline_layout),
module: &gi_shader_module,
entry_point: Some("gi"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
compilation_options: wgpu::PipelineCompilationOptions {
zero_initialize_workgroup_memory: false,
..Default::default()
},
cache: None,
});
@@ -521,6 +616,9 @@ impl State
chunk_pos_map: chunk_pos_map.into(),
rm_time: Arc::new(Mutex::new(0.)),
frame_time: Arc::new(Mutex::new(0.)),
terrain_width,
terrain_height,
terrain_alt,
};
// Configure surface for the first time
@@ -834,9 +932,9 @@ impl State
// chunk_width: self.terrain_generator.chunk_width as u32,
// chunk_height: self.terrain_generator.chunk_height as u32,
// chunk_alt: self.terrain_generator.chunk_alt as u32,
chunk_width: 20,
chunk_height: 20,
chunk_alt: 20,
chunk_width: self.terrain_width as u32,
chunk_height: self.terrain_height as u32,
chunk_alt: self.terrain_alt as u32,
}];
renderpass.set_immediates(0, unsafe { as_raw_bytes(&imm) });
renderpass.dispatch_workgroups(