feat: Starting bvh raytracing
This commit is contained in:
+31
-4
@@ -149,6 +149,7 @@ pub struct Bvh
|
||||
{
|
||||
/// List of the bvh's nodes
|
||||
pub nodes: Vec<BvhNode>,
|
||||
pub depth: usize,
|
||||
}
|
||||
|
||||
pub fn build_bvh<T>(primitives: &mut [BvhPrimitive<T>], leaf_max: usize) -> Bvh
|
||||
@@ -160,6 +161,7 @@ pub fn build_bvh<T>(primitives: &mut [BvhPrimitive<T>], leaf_max: usize) -> Bvh
|
||||
{
|
||||
return Bvh {
|
||||
nodes,
|
||||
depth: 1,
|
||||
//prim_indices,
|
||||
};
|
||||
}
|
||||
@@ -174,9 +176,14 @@ pub fn build_bvh<T>(primitives: &mut [BvhPrimitive<T>], leaf_max: usize) -> Bvh
|
||||
0,
|
||||
));
|
||||
|
||||
build_node(0, primitives, &mut nodes, leaf_max);
|
||||
let mut max_depth = 0usize;
|
||||
build_node(0, primitives, &mut nodes, leaf_max, 0, &mut max_depth);
|
||||
println!("Max depth {}", max_depth);
|
||||
|
||||
Bvh { nodes }
|
||||
Bvh {
|
||||
nodes,
|
||||
depth: max_depth,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_node<T>(
|
||||
@@ -184,6 +191,8 @@ pub fn build_node<T>(
|
||||
primitives: &mut [BvhPrimitive<T>],
|
||||
nodes: &mut Vec<BvhNode>,
|
||||
leaf_max: usize,
|
||||
depth: usize,
|
||||
max_depth: &mut usize,
|
||||
)
|
||||
{
|
||||
// We only extend a leaf if necessary
|
||||
@@ -248,12 +257,30 @@ pub fn build_node<T>(
|
||||
nodes[node_index] = BvhNode::internal(nodes[node_index].aabb, left_index as u32);
|
||||
|
||||
// Build children
|
||||
build_node(left_index, primitives, nodes, leaf_max);
|
||||
build_node(left_index + 1, primitives, nodes, leaf_max);
|
||||
build_node(
|
||||
left_index,
|
||||
primitives,
|
||||
nodes,
|
||||
leaf_max,
|
||||
depth + 1,
|
||||
max_depth,
|
||||
);
|
||||
build_node(
|
||||
left_index + 1,
|
||||
primitives,
|
||||
nodes,
|
||||
leaf_max,
|
||||
depth + 1,
|
||||
max_depth,
|
||||
);
|
||||
}
|
||||
None =>
|
||||
{
|
||||
// Sah says keep as leaf, do nothing
|
||||
if depth > *max_depth
|
||||
{
|
||||
*max_depth = depth;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+85
-92
@@ -1,6 +1,7 @@
|
||||
//! Contains rendering code to display a debug overlay for the BVH.
|
||||
|
||||
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.
|
||||
@@ -37,44 +38,41 @@ impl BvhDebugPipeline
|
||||
{
|
||||
let shader = device.create_shader_module(wgpu::include_wgsl!("debug.wgsl"));
|
||||
|
||||
let bind_group_layout =
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("bvh_debug_bind_group_layout"),
|
||||
entries: &[
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::VERTEX,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: true },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("bvh_debug_bind_group_layout"),
|
||||
entries: &[
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::VERTEX,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: true },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: wgpu::ShaderStages::VERTEX,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: true },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
count: None,
|
||||
},
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: wgpu::ShaderStages::VERTEX,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: true },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 2,
|
||||
visibility: wgpu::ShaderStages::VERTEX,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Uniform,
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: wgpu::BufferSize::new(
|
||||
size_of::<Mat4>() as u64
|
||||
),
|
||||
},
|
||||
count: None,
|
||||
count: None,
|
||||
},
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 2,
|
||||
visibility: wgpu::ShaderStages::VERTEX,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Uniform,
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: wgpu::BufferSize::new(size_of::<Mat4>() as u64),
|
||||
},
|
||||
],
|
||||
});
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("bvh_debug_pipeline_layout"),
|
||||
@@ -89,54 +87,56 @@ impl BvhDebugPipeline
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
|
||||
let create_pipeline =
|
||||
|label: &str,
|
||||
vertex_entry: &str,
|
||||
fragment_entry: &str,
|
||||
topology: wgpu::PrimitiveTopology,
|
||||
depth_write_enabled: bool|
|
||||
-> wgpu::RenderPipeline {
|
||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some(label),
|
||||
layout: Some(&pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: Some(vertex_entry),
|
||||
compilation_options: Default::default(),
|
||||
buffers: &[],
|
||||
},
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw,
|
||||
// Boxes stay visible even when the camera is inside one.
|
||||
cull_mode: None,
|
||||
unclipped_depth: false,
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: Some(wgpu::DepthStencilState {
|
||||
format: depth_format,
|
||||
depth_write_enabled: Some(depth_write_enabled),
|
||||
depth_compare: Some(wgpu::CompareFunction::LessEqual),
|
||||
stencil: wgpu::StencilState::default(),
|
||||
bias: wgpu::DepthBiasState::default(),
|
||||
}),
|
||||
multisample: wgpu::MultisampleState::default(),
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: Some(fragment_entry),
|
||||
compilation_options: Default::default(),
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format: color_format,
|
||||
blend: None,
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
}),
|
||||
multiview_mask: None,
|
||||
cache: None,
|
||||
})
|
||||
};
|
||||
let create_pipeline = |label: &str,
|
||||
vertex_entry: &str,
|
||||
fragment_entry: &str,
|
||||
topology: wgpu::PrimitiveTopology,
|
||||
depth_write_enabled: bool|
|
||||
-> wgpu::RenderPipeline {
|
||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some(label),
|
||||
layout: Some(&pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: Some(vertex_entry),
|
||||
compilation_options: Default::default(),
|
||||
buffers: &[],
|
||||
},
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw,
|
||||
// Boxes stay visible even when the camera is inside one.
|
||||
cull_mode: None,
|
||||
unclipped_depth: false,
|
||||
polygon_mode: wgpu::PolygonMode::Fill,
|
||||
conservative: false,
|
||||
},
|
||||
depth_stencil: Some(wgpu::DepthStencilState {
|
||||
format: depth_format,
|
||||
depth_write_enabled: Some(depth_write_enabled),
|
||||
depth_compare: Some(wgpu::CompareFunction::LessEqual),
|
||||
stencil: wgpu::StencilState::default(),
|
||||
bias: wgpu::DepthBiasState::default(),
|
||||
}),
|
||||
multisample: wgpu::MultisampleState::default(),
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: Some(fragment_entry),
|
||||
compilation_options: Default::default(),
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format: color_format,
|
||||
blend: Some(wgpu::BlendState {
|
||||
color: wgpu::BlendComponent::OVER,
|
||||
alpha: wgpu::BlendComponent::OVER,
|
||||
}),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
}),
|
||||
multiview_mask: None,
|
||||
cache: None,
|
||||
})
|
||||
};
|
||||
|
||||
let solid_pipeline = create_pipeline(
|
||||
"bvh_debug_solid_pipeline",
|
||||
@@ -189,11 +189,7 @@ impl BvhDebugPipeline
|
||||
return;
|
||||
}
|
||||
|
||||
queue.write_buffer(
|
||||
&self.view_proj_buffer,
|
||||
0,
|
||||
bytemuck::bytes_of(&view_proj),
|
||||
);
|
||||
queue.write_buffer(&self.view_proj_buffer, 0, bytemuck::bytes_of(&view_proj));
|
||||
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("bvh_debug_bind_group"),
|
||||
@@ -245,10 +241,7 @@ impl BvhDebugPipeline
|
||||
if max_objects_per_leaf > 0
|
||||
{
|
||||
pass.set_pipeline(&self.solid_pipeline);
|
||||
pass.draw(
|
||||
0..36 * max_objects_per_leaf,
|
||||
0..node_count,
|
||||
);
|
||||
pass.draw(0..36 * max_objects_per_leaf, 0..node_count);
|
||||
}
|
||||
|
||||
pass.set_pipeline(&self.line_pipeline);
|
||||
|
||||
+6
-6
@@ -37,13 +37,13 @@ struct GpuObject
|
||||
struct VertexOutput
|
||||
{
|
||||
@builtin(position) clip_position: vec4<f32>,
|
||||
@location(0) color: vec3<f32>,
|
||||
@location(0) color: vec4<f32>,
|
||||
@location(1) normal: vec3<f32>,
|
||||
};
|
||||
|
||||
const INTERNAL_NODE_COLOR = vec3<f32>(0.15, 0.70, 1.0);
|
||||
const LEAF_NODE_COLOR = vec3<f32>(1.0, 0.85, 0.20);
|
||||
const OBJECT_COLOR = vec3<f32>(1.0, 0.45, 0.12);
|
||||
const INTERNAL_NODE_COLOR = vec4<f32>(0.15, 0.70, 1.0, 1.0);
|
||||
const LEAF_NODE_COLOR = vec4<f32>(1.0, 0.85, 0.20, 1.0);
|
||||
const OBJECT_COLOR = vec4<f32>(1.0, 0.45, 0.12, 0.5);
|
||||
|
||||
// Pushed fully outside the clip volume so skipped nodes/objects produce no
|
||||
// fragments.
|
||||
@@ -188,7 +188,7 @@ fn vs_solids(
|
||||
@fragment
|
||||
fn fs_lines(input: VertexOutput) -> @location(0) vec4<f32>
|
||||
{
|
||||
return vec4<f32>(input.color, 1.0);
|
||||
return input.color;
|
||||
}
|
||||
|
||||
@fragment
|
||||
@@ -198,5 +198,5 @@ fn fs_solids(input: VertexOutput) -> @location(0) vec4<f32>
|
||||
let light_direction = normalize(vec3<f32>(0.45, 0.80, 0.55));
|
||||
let diffuse = max(dot(normal, light_direction), 0.0);
|
||||
|
||||
return vec4<f32>(input.color * (0.35 + 0.65 * diffuse), 1.0);
|
||||
return vec4<f32>(input.color * (0.35 + 0.65 * diffuse));
|
||||
}
|
||||
|
||||
+14
-7
@@ -525,7 +525,7 @@ impl State
|
||||
|
||||
let mut bvh = BvhStructureTable::new(&device, 128, 128);
|
||||
let extent = Aabb::new(-10. * Vec3::ONE, 10. * Vec3::ONE);
|
||||
for _ in 0..200
|
||||
for _ in 0..100
|
||||
{
|
||||
let transform = Mat4::from_rotation_translation(
|
||||
Quat::from_euler(
|
||||
@@ -706,7 +706,8 @@ impl State
|
||||
|
||||
bind_group_layouts: &[
|
||||
Some(voxel_cache.raytrace_bind_group_layout()),
|
||||
Some(structure_table.raytrace_bind_group_layout()),
|
||||
//Some(structure_table.raytrace_bind_group_layout()),
|
||||
Some(bvh.raytrace_bind_group_layout()),
|
||||
Some(&ray_march_bind_group_layout),
|
||||
],
|
||||
immediate_size: size_of::<Immediates>() as u32,
|
||||
@@ -1202,11 +1203,12 @@ impl State
|
||||
|
||||
renderpass.set_pipeline(&self.pipeline);
|
||||
renderpass.set_bind_group(0, self.voxel_cache.lock().raytrace_bind_group(), &[]);
|
||||
renderpass.set_bind_group(
|
||||
1,
|
||||
self.structure_table.lock().raytrace_bind_group(),
|
||||
&[],
|
||||
);
|
||||
renderpass.set_bind_group(1, self.bvh.raytrace_bind_group(), &[]);
|
||||
// renderpass.set_bind_group(
|
||||
// 1,
|
||||
// self.structure_table.lock().raytrace_bind_group(),
|
||||
// &[],
|
||||
// );
|
||||
renderpass.set_bind_group(2, Some(&surface_bg), &[]);
|
||||
let table_base_slot = self.structure_table.lock().base_slot();
|
||||
let imm = [Immediates {
|
||||
@@ -1235,6 +1237,11 @@ impl State
|
||||
renderpass.set_pipeline(&self.gi_pipeline);
|
||||
renderpass.set_immediates(0, unsafe { as_raw_bytes(&imm) });
|
||||
renderpass.set_bind_group(2, Some(&gi_surface_bg), &[]);
|
||||
renderpass.set_bind_group(
|
||||
1,
|
||||
self.structure_table.lock().raytrace_bind_group(),
|
||||
&[],
|
||||
);
|
||||
renderpass.dispatch_workgroups(
|
||||
self.size.width.div_ceil(16),
|
||||
self.size.height.div_ceil(16),
|
||||
|
||||
Reference in New Issue
Block a user