feat: bad bvh

This commit is contained in:
2026-11-14 16:15:19 +01:00
parent e7fbeb7887
commit 4581538c66
8 changed files with 81 additions and 185 deletions
+1
View File
@@ -63,6 +63,7 @@ impl From<BvhNode> for GpuBvhNode
aabb: value.aabb.into(),
count: value.count,
first_or_left: value.first_or_left,
_pad2: [0; 2],
}
}
}
+3 -2
View File
@@ -4,9 +4,10 @@ use glam::Mat4;
use wgpu::BlendState;
// debug.wgsl mirrors the GPU structs manually. Keep the array strides in sync:
// WGSL would otherwise round them up to 16-byte alignment.
// WGSL rounds structs containing vec3 up to a 16-byte alignment, so a node's
// 40 bytes of data become a 48-byte array stride.
const _: () = {
assert!(size_of::<crate::bvh::gpu::GpuBvhNode>() == 40);
assert!(size_of::<crate::bvh::gpu::GpuBvhNode>() == 48);
assert!(size_of::<crate::bvh::gpu::GpuObject>() == 144);
};
+8 -12
View File
@@ -5,19 +5,15 @@
struct GpuBvhNode
{
// Kept as scalar fields on purpose. WGSL rounds structs containing vec3/vec4
// up to a 16-byte alignment (48-byte array stride), while the Rust side is
// a 40-byte `#[repr(C)]` struct.
min_x: f32,
min_y: f32,
min_z: f32,
_pad0: f32,
max_x: f32,
max_y: f32,
max_z: f32,
// vec3 members align to 16 bytes, so this struct has a 48-byte array
// stride. The Rust `GpuBvhNode` is padded to the same size.
min: vec3<f32>,
_pad0: u32,
max: vec3<f32>,
_pad1: u32,
count: u32,
first_or_left: u32,
_pad2: vec2<u32>,
};
struct GpuObject
@@ -111,8 +107,8 @@ fn vs_lines(
}
let node = nodes[instance_index];
let min_corner = vec3<f32>(node.min_x, node.min_y, node.min_z);
let max_corner = vec3<f32>(node.max_x, node.max_y, node.max_z);
let min_corner = node.min;
let max_corner = node.max;
let world_position = corner_position(
min_corner,
max_corner,
+5
View File
@@ -16,6 +16,10 @@ pub struct GpuAabb
}
/// Follow the same documentation as [`BvhNode`]
///
/// The trailing padding keeps the array stride at 48 bytes. On the GPU a struct
/// containing `vec3` has a 16-byte alignment, so the shader rounds the 40 bytes
/// of data up to a 48-byte stride.
#[derive(Debug, Clone, Copy, bytemuck::Zeroable, bytemuck::Pod)]
#[repr(C)]
pub struct GpuBvhNode
@@ -23,6 +27,7 @@ pub struct GpuBvhNode
pub aabb: GpuAabb,
pub count: u32,
pub first_or_left: u32,
pub(crate) _pad2: [u32; 2],
}
/// To be a leaf of a GPU BVH
+9 -13
View File
@@ -1192,14 +1192,7 @@ impl State
encoder.write_timestamp(&timestamp_query, 0);
{
gpu_profiler::begin_scope!(encoder, "Compute render pass");
let mut renderpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("compute_render_pass"),
timestamp_writes: Some(wgpu::ComputePassTimestampWrites {
query_set: &timestamp_query,
beginning_of_pass_write_index: Some(2),
end_of_pass_write_index: Some(3),
}),
});
let mut renderpass = encoder.begin_profiled_compute_pass("compute_render_pass");
renderpass.set_pipeline(&self.pipeline);
renderpass.set_bind_group(0, self.voxel_cache.lock().raytrace_bind_group(), &[]);
@@ -1229,11 +1222,14 @@ impl State
table_base_slot,
}];
renderpass.set_immediates(0, unsafe { as_raw_bytes(&imm) });
renderpass.dispatch_workgroups(
self.size.width.div_ceil(16),
self.size.height.div_ceil(16),
1,
);
{
gpu_profiler::begin_scope!(renderpass, "RT");
renderpass.dispatch_workgroups(
self.size.width.div_ceil(16),
self.size.height.div_ceil(16),
1,
);
}
renderpass.set_pipeline(&self.gi_pipeline);
renderpass.set_immediates(0, unsafe { as_raw_bytes(&imm) });
renderpass.set_bind_group(2, Some(&gi_surface_bg), &[]);