VoxelCache refactor

This commit is contained in:
2026-10-04 20:15:00 +02:00
parent d9df73abab
commit b560f6afd7
14 changed files with 693 additions and 518 deletions
+18
View File
@@ -3,6 +3,11 @@
RWStructuredBuffer<uint32_t> count_buffer;
[[vk::binding(1, 0)]]
RWStructuredBuffer<uint32_t> index_buffer;
[[vk::binding(2, 0)]]
RWStructuredBuffer<uint32_t> compacted_buffer;
[[vk::push_constant]]
uniform uint32_t write_compact;
[[vk::binding(0, 1)]]
RWStructuredBuffer<uint32_t> reduced_buffer;
@@ -157,5 +162,18 @@ void scatter(
{
index_buffer[sum2] = thread_index * 2 + 1;
}
if (write_compact != 0)
{
if (count_buffer[thread_index * 2] != 0 && sum1 < total)
{
compacted_buffer[sum1] = count_buffer[thread_index * 2];
}
if (count_buffer[thread_index * 2 + 1] != 0 && sum2 < total)
{
compacted_buffer[sum2] = count_buffer[thread_index * 2 + 1];
}
}
}
Binary file not shown.
+3 -6
View File
@@ -82,14 +82,12 @@ RWStructuredBuffer<StructurePoolElement> structure_pool;
[[vk::binding(1, 0)]]
RWStructuredBuffer<uint32_t> attributes_pool[2];
[[vk::binding(2, 0)]]
RWStructuredBuffer<LocationPoolElement> location_pool;
[[vk::binding(3, 0)]]
RWStructuredBuffer<RequestBufferElement> request_buffer;
[[vk::binding(4, 0)]]
[[vk::binding(3, 0)]]
RWStructuredBuffer<uint32_t> usage_buffer;
[[vk::binding(5, 0)]]
[[vk::binding(4, 0)]]
RWStructuredBuffer<StructurePointer> structure_table_pointer;
[[vk::binding(6, 0)]]
[[vk::binding(5, 0)]]
RWStructuredBuffer<Atomic<uint32_t>> structure_table_request_buffer;
ByteColor fetch_color(uint32_t index, uint32_t child_index)
@@ -172,7 +170,6 @@ void gi(uint32_t3 location: SV_DispatchThreadID, uint32_t3 local_location: SV_Gr
var voxel_cache : VoxelCache;
voxel_cache.structure_pool = structure_pool;
voxel_cache.attributes_pool = attributes_pool;
voxel_cache.location_pool = location_pool;
voxel_cache.request_buffer = request_buffer;
voxel_cache.usage_buffer = usage_buffer;
voxel_cache.structure_table_pointer = structure_table_pointer;
+1 -8
View File
@@ -81,17 +81,10 @@ public struct RequestBufferElement
public Atomic<uint32_t> requests[64];
}
public struct LocationPoolElement
{
public uint32_t structure_id;
public uint32_t structure_locator;
}
public struct VoxelCache
{
public RWStructuredBuffer<StructurePoolElement> structure_pool;
public RWStructuredBuffer<uint32_t> attributes_pool[2];
public RWStructuredBuffer<LocationPoolElement> location_pool;
public RWStructuredBuffer<RequestBufferElement> request_buffer;
public RWStructuredBuffer<uint32_t> usage_buffer;
public RWStructuredBuffer<StructurePointer> structure_table_pointer;
@@ -356,7 +349,7 @@ public HitInformation ray_cast(
context.ray_direction,
position - float3(current_voxel),
chunk_index,
t_adv,
max(t_adv, 0.01),
stack_index,
context.frame_timestamp,
context.cache_report);
+4 -6
View File
@@ -82,14 +82,12 @@ RWStructuredBuffer<StructurePoolElement> structure_pool;
[[vk::binding(1, 0)]]
RWStructuredBuffer<uint32_t> attributes_pool[2];
[[vk::binding(2, 0)]]
RWStructuredBuffer<LocationPoolElement> location_pool;
[[vk::binding(3, 0)]]
RWStructuredBuffer<RequestBufferElement> request_buffer;
[[vk::binding(4, 0)]]
[[vk::binding(3, 0)]]
RWStructuredBuffer<uint32_t> usage_buffer;
[[vk::binding(5, 0)]]
[[vk::binding(4, 0)]]
RWStructuredBuffer<StructurePointer> structure_table_pointer;
[[vk::binding(6, 0)]]
[[vk::binding(5, 0)]]
RWStructuredBuffer<Atomic<uint32_t>> structure_table_request_buffer;
ByteColor fetch_color(uint32_t index, uint32_t child_index)
@@ -156,7 +154,6 @@ void ray_march_compute(uint32_t3 location: SV_DispatchThreadID, uint32_t3 local_
var voxel_cache : VoxelCache;
voxel_cache.structure_pool = structure_pool;
voxel_cache.attributes_pool = attributes_pool;
voxel_cache.location_pool = location_pool;
voxel_cache.request_buffer = request_buffer;
voxel_cache.usage_buffer = usage_buffer;
voxel_cache.structure_table_pointer = structure_table_pointer;
@@ -179,6 +176,7 @@ void ray_march_compute(uint32_t3 location: SV_DispatchThreadID, uint32_t3 local_
let hit_pos = hit.hit_pos;
let hit_normal = fetch_normal(hit.node_index, hit.child_index);
let hit_albedo = fetch_color(hit.node_index, hit.child_index).float_color;
// let hit_albedo = hit.color;
gbuffer_position.Store(location.xy, float4(hit_pos, 1.));
gbuffer_normal.Store(location.xy, float4(hit_normal, 1.));
BIN
View File
Binary file not shown.
+2 -1
View File
@@ -326,6 +326,7 @@ impl RadixSorter
keys: &Buffer,
values: &Buffer,
indirect: Option<&Buffer>,
direct_length: Option<usize>,
encoder: &mut CommandEncoder,
sort_direction: RadixSortDirection,
)
@@ -383,7 +384,7 @@ impl RadixSorter
}
else
{
self.max_size as u32
direct_length.unwrap_or(self.max_size) as u32
},
descending: 0,
digit_stride: 0,
+36 -14
View File
@@ -64,6 +64,7 @@ use crate::voxel_cache::VoxelCache;
use crate::voxel_cache::data::ColorBytes;
use crate::voxel_cache::data::LocationPoolElement;
use crate::voxel_cache::data::StructurePoolElement;
use crate::voxel_cache::layout::VoxelCacheLayout;
use crate::voxel_cache::producer_interface::CacheProducerInterface;
use crate::voxel_cache::producer_interface::CacheRequest;
@@ -188,6 +189,7 @@ impl State
max_immediate_size: 112,
max_storage_buffers_per_shader_stage: 16,
max_binding_array_elements_per_shader_stage: 16,
max_bind_groups: 8,
..Default::default()
},
..Default::default()
@@ -203,7 +205,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 = 1_000_000;
let elt_count_real = 500_000;
let count_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("count_buffer"),
usage: wgpu::BufferUsages::STORAGE,
@@ -215,8 +217,8 @@ impl State
let org_big_value_buffer = (0..(elt_count as u32)).collect::<Vec<_>>();
let org_big_key_buffer = org_big_value_buffer
.iter()
//.map(|_| random::<u32>())
.map(|_| random_range(1..256))
.map(|_| random::<u32>())
//.map(|_| random_range(1..256))
.collect::<Vec<_>>();
// let mut prefix = org_big_key_buffer.iter().fold([0; 256], |acc, x| {
@@ -330,13 +332,13 @@ impl State
let egui_renderer = EguiRenderer::new(&device, surface_format, &window);
let attributes = vec![
VoxelAttribute { word_count: 1 },
VoxelAttribute { word_count: 3 },
];
let attributes = VoxelCacheLayout::default()
.add_attribute(voxel_cache::layout::VoxelCacheAttribute { word_count: 1 })
.add_attribute(voxel_cache::layout::VoxelCacheAttribute { word_count: 3 });
let mut voxel_cache =
VoxelCache::<4>::new(100_000, device.clone(), queue.clone(), attributes.clone());
let cache_interface = CacheProducerInterface::new(1024, &device, attributes.clone());
VoxelCache::<4>::new(100_000, device.clone(), queue.clone(), &attributes);
let cache_interface = CacheProducerInterface::new(1024, &device, &attributes);
let terrain_generator = TerrainGenerator::<4>::new(5, "vxls_height.tif", 0.2, "img.jpg");
//let terrain_generator = SdfGenerator::<4>::new(5);
@@ -357,6 +359,9 @@ impl State
let terrain_width = terrain_generator.chunk_width;
let terrain_height = terrain_generator.chunk_height;
let terrain_alt = terrain_generator.chunk_alt;
// let terrain_width = 10;
// let terrain_height = 10;
// let terrain_alt = 10;
println!(
"Chunk count : {}",
terrain_width * terrain_height * terrain_width
@@ -568,7 +573,7 @@ impl State
label: Some("Voxel pipeline layout"),
bind_group_layouts: &[
Some(&voxel_cache.bind_group_layout()),
Some(voxel_cache.user_bind_group_layout()),
Some(&ray_march_bind_group_layout),
],
immediate_size: size_of::<Immediates>() as u32,
@@ -590,7 +595,7 @@ impl State
label: Some("Gi pipeline layout"),
bind_group_layouts: &[
Some(&voxel_cache.bind_group_layout()),
Some(&voxel_cache.user_bind_group_layout()),
Some(&gi_bind_group_layout),
],
immediate_size: size_of::<Immediates>() as u32,
@@ -960,7 +965,7 @@ impl State
});
renderpass.set_pipeline(&self.pipeline);
renderpass.set_bind_group(0, Some(&self.voxel_cache.lock().bind_group()), &[]);
renderpass.set_bind_group(0, &self.voxel_cache.lock().user_bind_group(), &[]);
renderpass.set_bind_group(1, Some(&surface_bg), &[]);
let imm = [Immediates {
view_proj: self.camera.view_proj().inverse(),
@@ -1122,7 +1127,7 @@ impl State
DownloadBuffer::read_buffer(
&self.device,
&self.queue,
&self.voxel_cache.lock().usage_buffer.usage_buffer.slice(..),
&self.voxel_cache.lock().usage_buffer.slice(..),
move |slice| {
let slice = slice.unwrap();
let cache_slice: &[u32] = bytemuck::cast_slice(&slice);
@@ -1136,6 +1141,23 @@ impl State
},
);
// DownloadBuffer::read_buffer(
// &self.device,
// &self.queue,
// &self
// .voxel_cache
// .lock()
// .structure_table
// .request_buffer
// .request_count_buffer()
// .slice(..),
// move |slice| {
// let slice = slice.unwrap();
// let cache_slice: &[u32] = bytemuck::cast_slice(&slice);
// println!("structure_table count: {}", cache_slice[0]);
// },
// );
// ~~ Do cache managment
if !self
.camera
@@ -1205,7 +1227,7 @@ impl State
bytemuck::pod_collect_to_vec(&buffer.unwrap());
let generator = cloned_generator;
let gen_test = SineGenerator::<4>::new(5);
//let gen_test = SineGenerator::<4>::new(5);
let mut structure_nodes: Vec<StructurePoolElement<4>> = vec![];
let mut color_nodes: Vec<[ColorBytes; 64]> = vec![];
+564 -477
View File
File diff suppressed because it is too large Load Diff
+29 -1
View File
@@ -1,3 +1,4 @@
use bytemuck::bytes_of;
use wgpu::BindGroup;
use wgpu::BindGroupLayout;
use wgpu::Buffer;
@@ -67,6 +68,17 @@ impl NonZeroBufferCompactor
},
count: None,
},
// Optional compacted layout
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
],
});
@@ -116,7 +128,7 @@ impl NonZeroBufferCompactor
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("compaction_pipeline_layout"),
bind_group_layouts: &[Some(&count_buffer_bg_layout), Some(&compaction_bg_layout)],
immediate_size: 0,
immediate_size: size_of::<u32>() as u32, // Bool : Write to compacted buffer ?
});
let compaction_shader_module = unsafe {
@@ -201,6 +213,7 @@ impl NonZeroBufferCompactor
encoder: &mut CommandEncoder,
count_buffer: &Buffer,
index_buffer: &Buffer,
compact_buffer: Option<&Buffer>,
)
{
let count_buffer_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
@@ -215,6 +228,10 @@ impl NonZeroBufferCompactor
binding: 1,
resource: index_buffer.as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: compact_buffer.unwrap_or(&count_buffer).as_entire_binding(),
},
],
});
let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
@@ -235,6 +252,17 @@ impl NonZeroBufferCompactor
// We can now reapply the partial running sum on the subblocks
compute_pass.set_pipeline(&self.scatter_pipeline);
compute_pass.set_immediates(
0,
if compact_buffer.is_some()
{
bytes_of(&1)
}
else
{
bytes_of(&0)
},
);
compute_pass.dispatch_workgroups(self.block_count as u32, 1, 1);
}
}
+25
View File
@@ -0,0 +1,25 @@
#[derive(Clone, PartialEq, Eq, Default)]
pub struct VoxelCacheLayout
{
pub(super) attributes: Vec<VoxelCacheAttribute>,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct VoxelCacheAttribute
{
pub word_count: u32,
}
impl VoxelCacheLayout
{
pub fn add_attribute(mut self, attribute: VoxelCacheAttribute) -> Self
{
self.attributes.push(attribute);
self
}
pub fn attributes(&self) -> &[VoxelCacheAttribute]
{
&self.attributes
}
}
+8 -5
View File
@@ -21,6 +21,8 @@ use crate::voxel_cache::VoxelAttribute;
use crate::voxel_cache::data::ColorPoolElement;
use crate::voxel_cache::data::LocationPoolElement;
use crate::voxel_cache::data::StructurePoolElement;
use crate::voxel_cache::layout::VoxelCacheAttribute;
use crate::voxel_cache::layout::VoxelCacheLayout;
#[derive(Clone, Copy, Zeroable, Pod)]
#[repr(C)]
@@ -67,7 +69,7 @@ impl<const N: usize> CacheProducerInterface<N>
where
[(); N * N * N]:,
{
pub fn new(size: usize, device: &Device, attributes: Vec<VoxelAttribute>) -> Self
pub fn new(size: usize, device: &Device, layout: &VoxelCacheLayout) -> Self
{
let requests = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("cache_interface_request_buffer"),
@@ -91,7 +93,8 @@ where
});
// Create attributes
let attributes_nodes = attributes
let attributes_nodes = layout
.attributes()
.iter()
.enumerate()
.map(|(i, x)| {
@@ -119,7 +122,7 @@ where
});
let request_side_bind_group_layout =
Self::request_side_bind_group_layout(device, &attributes);
Self::request_side_bind_group_layout(device, layout.attributes());
let producer_side_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("cache_interface_producer_side_bind_group_layout"),
@@ -166,7 +169,7 @@ where
has_dynamic_offset: false,
min_binding_size: None,
},
count: Some(NonZero::new(attributes.len() as u32).unwrap()),
count: Some(NonZero::new(layout.attributes().len() as u32).unwrap()),
},
// Location nodes
BindGroupLayoutEntry {
@@ -309,7 +312,7 @@ where
pub fn request_side_bind_group_layout(
device: &Device,
attributes: &[VoxelAttribute],
attributes: &[VoxelCacheAttribute],
) -> BindGroupLayout
{
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
+2
View File
@@ -265,6 +265,7 @@ where
encoder,
&self.request_buffer,
&self.index_buffer,
None,
);
self.buffer_compactor
@@ -287,6 +288,7 @@ where
&self.keys_buffer,
&self.index_buffer,
Some(&self.request_count),
None,
encoder,
radix_sort::RadixSortDirection::Descending,
);
+1
View File
@@ -183,6 +183,7 @@ impl UsageBuffer
&self.key_buffer,
&self.index_buffer,
None,
None,
encoder,
crate::gpu::radix_sort::RadixSortDirection::Ascending,
);