feat: Good bvh traversal
This commit is contained in:
+68
@@ -12,8 +12,13 @@ use crate::bvh::build::BvhPrimitive;
|
||||
use crate::bvh::build::build_bvh;
|
||||
use crate::bvh::gpu::GpuBvhNode;
|
||||
use crate::bvh::gpu::GpuObject;
|
||||
use crate::gpu_profiler::ScopeEncoder;
|
||||
use crate::production::ProducerId;
|
||||
use crate::production::StructureAnchor;
|
||||
use crate::voxel_cache::CacheInvalidable;
|
||||
use crate::voxel_cache::eviction::CacheEvictor;
|
||||
use crate::voxel_cache::insertion::CacheInsertionData;
|
||||
use crate::voxel_cache::requests::CacheRequestBuffer;
|
||||
use crate::voxel_cache::tables::DynamicStructureTable;
|
||||
|
||||
pub mod aabb;
|
||||
@@ -305,6 +310,69 @@ impl BvhStructureTable
|
||||
{
|
||||
&self.raytrace_bind_group
|
||||
}
|
||||
|
||||
/// Gathers the root requests emitted by the inner structure table into
|
||||
/// `request_buffer`.
|
||||
pub fn gather_requests(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
|
||||
request_buffer: &CacheRequestBuffer,
|
||||
)
|
||||
{
|
||||
self.structure_table
|
||||
.gather_requests(device, encoder, request_buffer);
|
||||
}
|
||||
|
||||
/// Writes the cache page pointers produced by an external insertion into
|
||||
/// the inner structure table.
|
||||
pub fn insert<const N: usize>(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
|
||||
insertion: &CacheInsertionData<N>,
|
||||
) where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
self.structure_table.insert(device, encoder, insertion);
|
||||
}
|
||||
|
||||
pub fn get(&self, handle: &BvhHandle) -> Option<&BvhObject>
|
||||
{
|
||||
self.objects.get(handle)
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, handle: &BvhHandle) -> Option<&mut BvhObject>
|
||||
{
|
||||
self.objects.get_mut(handle)
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> CacheInvalidable<N> for BvhStructureTable
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
fn validate_insertion(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
|
||||
insertion: &CacheInsertionData<N>,
|
||||
)
|
||||
{
|
||||
self.structure_table
|
||||
.validate_insertion(device, encoder, insertion);
|
||||
}
|
||||
|
||||
fn invalidate(
|
||||
&mut self,
|
||||
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
|
||||
cache_evictor: &CacheEvictor,
|
||||
invalid_timestamp: u32,
|
||||
)
|
||||
{
|
||||
self.structure_table
|
||||
.invalidate(encoder, cache_evictor, invalid_timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
fn create_bvh_buffer<T>(device: &wgpu::Device, capacity: usize, label: &str) -> wgpu::Buffer
|
||||
|
||||
+111
-89
@@ -54,9 +54,11 @@ use winit::event_loop::OwnedDisplayHandle;
|
||||
use winit::window::Window;
|
||||
use winit::window::WindowId;
|
||||
|
||||
use crate::bvh::BvhHandle;
|
||||
use crate::bvh::BvhObject;
|
||||
use crate::bvh::BvhStructureTable;
|
||||
use crate::bvh::aabb::Aabb;
|
||||
use crate::bvh::build::Bvh;
|
||||
use crate::bvh::debug::BvhDebugPipeline;
|
||||
use crate::camera::Camera;
|
||||
use crate::egui_renderer::EguiRenderer;
|
||||
@@ -148,8 +150,9 @@ struct State
|
||||
request_buffer: Arc<CacheRequestBuffer>,
|
||||
cache_insertion: Arc<CacheInsertionData<4>>,
|
||||
|
||||
structure_table: Arc<Mutex<BvhStructureTable>>,
|
||||
//structure_table: Arc<Mutex<FixedStructureTable>>,
|
||||
structure_table: Arc<Mutex<ToroidalStructureTable>>,
|
||||
//structure_table: Arc<Mutex<ToroidalStructureTable>>,
|
||||
structure_table_request_buffer: Arc<CacheRequestBuffer>,
|
||||
structure_table_cache_insertion: Arc<CacheInsertionData<4>>,
|
||||
usage_vec: Arc<Mutex<Vec<usize>>>,
|
||||
@@ -170,8 +173,8 @@ struct State
|
||||
// Producers workers
|
||||
wakeup: Sender<()>,
|
||||
|
||||
bvh: BvhStructureTable,
|
||||
bvh_debug: BvhDebugPipeline,
|
||||
bvh_objs: Vec<(BvhHandle, Vec3)>,
|
||||
}
|
||||
|
||||
pub unsafe fn as_raw_bytes<T: Sized>(slice: &[T]) -> &[u8]
|
||||
@@ -445,19 +448,74 @@ impl State
|
||||
let total_chunk_count = terrain_height * terrain_width * terrain_alt;
|
||||
|
||||
let structure_table_request_buffer = CacheRequestBuffer::new(&device, 128);
|
||||
|
||||
let mut bvh = BvhStructureTable::new(&device, 128, 128);
|
||||
let extent = Aabb::new(-10. * Vec3::ONE, 10. * Vec3::ONE);
|
||||
let mut bvh_objs = Vec::new();
|
||||
for _ in 0..1000
|
||||
{
|
||||
let transform = Mat4::from_rotation_translation(
|
||||
Quat::from_euler(
|
||||
glam::EulerRot::XYZ,
|
||||
random_range(-3.1..3.1),
|
||||
random_range(-3.1..3.1),
|
||||
random_range(-3.1..3.1),
|
||||
),
|
||||
Vec3::new(
|
||||
random_range(extent.min().x..extent.max().x),
|
||||
random_range(extent.min().y..extent.max().y),
|
||||
random_range(extent.min().z..extent.max().z),
|
||||
),
|
||||
);
|
||||
|
||||
let handle = bvh.add(
|
||||
&device,
|
||||
&queue,
|
||||
BvhObject {
|
||||
anchor: StructureAnchor::zeroed(),
|
||||
producer_id: ProducerId(0),
|
||||
transform,
|
||||
bounding_box: Aabb::CHUNK,
|
||||
},
|
||||
);
|
||||
|
||||
bvh_objs.push((
|
||||
handle,
|
||||
Vec3::new(
|
||||
random_range(-1.0..1.0),
|
||||
random_range(-1.0..1.0),
|
||||
random_range(-1.0..1.0),
|
||||
)
|
||||
.normalize(),
|
||||
));
|
||||
}
|
||||
|
||||
let now = Instant::now();
|
||||
bvh.rebuild(&device, &queue);
|
||||
let time = Instant::now() - now;
|
||||
|
||||
println!("Bvh build time : {}ms", time.as_millis());
|
||||
|
||||
let bvh_debug = BvhDebugPipeline::new(
|
||||
&device,
|
||||
surface_format.add_srgb_suffix(),
|
||||
wgpu::TextureFormat::Depth24PlusStencil8,
|
||||
);
|
||||
|
||||
// let structure_table = FixedStructureTable::new(
|
||||
// &device,
|
||||
// total_chunk_count,
|
||||
// structure_table_request_buffer.max_requests,
|
||||
// 0,
|
||||
// );
|
||||
let structure_table = ToroidalStructureTable::new(
|
||||
&device,
|
||||
(terrain_width, terrain_alt, terrain_height),
|
||||
Vec3::ZERO,
|
||||
structure_table_request_buffer.max_requests,
|
||||
0,
|
||||
);
|
||||
// let structure_table = ToroidalStructureTable::new(
|
||||
// &device,
|
||||
// (terrain_width, terrain_alt, terrain_height),
|
||||
// Vec3::ZERO,
|
||||
// structure_table_request_buffer.max_requests,
|
||||
// 0,
|
||||
// );
|
||||
let structure_table = bvh;
|
||||
let structure_table_insertion_data = CacheInsertionData::new(&device, 128, &layout);
|
||||
let chunk_instances = (0..terrain_alt)
|
||||
.cartesian_product(0..terrain_height)
|
||||
@@ -523,48 +581,6 @@ impl State
|
||||
// )
|
||||
// };
|
||||
|
||||
let mut bvh = BvhStructureTable::new(&device, 128, 128);
|
||||
let extent = Aabb::new(-10. * Vec3::ONE, 10. * Vec3::ONE);
|
||||
for _ in 0..100
|
||||
{
|
||||
let transform = Mat4::from_rotation_translation(
|
||||
Quat::from_euler(
|
||||
glam::EulerRot::XYZ,
|
||||
random_range(-3.1..3.1),
|
||||
random_range(-3.1..3.1),
|
||||
random_range(-3.1..3.1),
|
||||
),
|
||||
Vec3::new(
|
||||
random_range(extent.min().x..extent.max().x),
|
||||
random_range(extent.min().y..extent.max().y),
|
||||
random_range(extent.min().z..extent.max().z),
|
||||
),
|
||||
);
|
||||
|
||||
bvh.add(
|
||||
&device,
|
||||
&queue,
|
||||
BvhObject {
|
||||
anchor: StructureAnchor::zeroed(),
|
||||
producer_id: ProducerId(0),
|
||||
transform,
|
||||
bounding_box: Aabb::CHUNK,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
let now = Instant::now();
|
||||
bvh.rebuild(&device, &queue);
|
||||
let time = Instant::now() - now;
|
||||
|
||||
println!("Bvh build time : {}ms", time.as_millis());
|
||||
|
||||
let bvh_debug = BvhDebugPipeline::new(
|
||||
&device,
|
||||
surface_format.add_srgb_suffix(),
|
||||
wgpu::TextureFormat::Depth24PlusStencil8,
|
||||
);
|
||||
|
||||
let shader_module = unsafe {
|
||||
device.create_shader_module_passthrough({
|
||||
wgpu::ShaderModuleDescriptorPassthrough {
|
||||
@@ -706,8 +722,7 @@ impl State
|
||||
|
||||
bind_group_layouts: &[
|
||||
Some(voxel_cache.raytrace_bind_group_layout()),
|
||||
//Some(structure_table.raytrace_bind_group_layout()),
|
||||
Some(bvh.raytrace_bind_group_layout()),
|
||||
Some(structure_table.raytrace_bind_group_layout()),
|
||||
Some(&ray_march_bind_group_layout),
|
||||
],
|
||||
immediate_size: size_of::<Immediates>() as u32,
|
||||
@@ -867,8 +882,8 @@ impl State
|
||||
insert_profile: Arc::new(Mutex::new(Profile::default())),
|
||||
cursor_grabed: true,
|
||||
wakeup: wakeup_tx,
|
||||
bvh,
|
||||
bvh_debug,
|
||||
bvh_objs,
|
||||
};
|
||||
|
||||
// Configure surface for the first time
|
||||
@@ -1070,6 +1085,28 @@ impl State
|
||||
{
|
||||
self.camera.update();
|
||||
|
||||
let mut locked_table = self.structure_table.lock();
|
||||
for (hndl, dir) in self.bvh_objs.iter_mut()
|
||||
{
|
||||
let obj = locked_table.get_mut(hndl);
|
||||
if let Some(obj) = obj
|
||||
{
|
||||
let (scale, mut rot, mut trans) = obj.transform.to_scale_rotation_translation();
|
||||
let speed = 0.01;
|
||||
rot = rot.mul_quat(Quat::from_axis_angle(*dir, speed));
|
||||
trans += *dir * speed;
|
||||
*dir += Vec3::new(
|
||||
random_range(-speed..speed),
|
||||
random_range(-speed..speed),
|
||||
random_range(-speed..speed),
|
||||
);
|
||||
obj.transform = Mat4::from_scale_rotation_translation(scale, rot, trans);
|
||||
}
|
||||
}
|
||||
locked_table.mark_dirty();
|
||||
locked_table.rebuild_if_dirty(&self.device, &self.queue);
|
||||
drop(locked_table);
|
||||
|
||||
// Create texture view.
|
||||
// NOTE: We must handle Timeout because the surface may be unavailable
|
||||
// (e.g., when the window is occluded on macOS).
|
||||
@@ -1170,22 +1207,6 @@ impl State
|
||||
let mut command_encoder = self.device.create_command_encoder(&Default::default());
|
||||
let mut prof_command_encoder = ProfilingEncoder::new(&self.device, &mut command_encoder);
|
||||
|
||||
{
|
||||
let retired = self.structure_table.lock().move_position(
|
||||
&self.queue,
|
||||
&mut prof_command_encoder,
|
||||
self.camera.position,
|
||||
);
|
||||
|
||||
// Cancellation is only an optimisation: stale insertions are
|
||||
// rejected by the GPU because they no longer match the slot.
|
||||
let mut request_queue = self.request_queue.lock();
|
||||
for anchor in retired
|
||||
{
|
||||
request_queue.invalidate_anchor(anchor);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let mut encoder = prof_command_encoder.begin_frame(Some("Main frame"));
|
||||
|
||||
@@ -1196,14 +1217,13 @@ 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.bvh.raytrace_bind_group(), &[]);
|
||||
// renderpass.set_bind_group(
|
||||
// 1,
|
||||
// self.structure_table.lock().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 table_base_slot = [0; 3];
|
||||
let imm = [Immediates {
|
||||
view_proj: self.camera.view_proj().inverse(),
|
||||
cam_pos: self.camera.position,
|
||||
@@ -1257,18 +1277,20 @@ impl State
|
||||
self.target_blitter
|
||||
.copy(&self.device, &mut encoder, target_view, &texture_view);
|
||||
|
||||
self.bvh_debug.render(
|
||||
&self.device,
|
||||
&self.queue,
|
||||
&mut encoder,
|
||||
&texture_view,
|
||||
&self.depth_buffer.1,
|
||||
self.camera.view_proj(),
|
||||
self.bvh.bvh_nodes(),
|
||||
self.bvh.bvh_objects(),
|
||||
self.bvh.node_count(),
|
||||
2,
|
||||
);
|
||||
let structure_table = self.structure_table.lock();
|
||||
// self.bvh_debug.render(
|
||||
// &self.device,
|
||||
// &self.queue,
|
||||
// &mut encoder,
|
||||
// &texture_view,
|
||||
// &self.depth_buffer.1,
|
||||
// self.camera.view_proj(),
|
||||
// structure_table.bvh_nodes(),
|
||||
// structure_table.bvh_objects(),
|
||||
// structure_table.node_count(),
|
||||
// 2,
|
||||
// );
|
||||
drop(structure_table);
|
||||
|
||||
// ~~ EGUI Render pass ~~
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user