diff --git a/Cargo.toml b/Cargo.toml index c630ab0..ce89195 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -108,6 +108,9 @@ unused_qualifications = "warn" # is off by default because a build with it needs the DLSS SDK. dlss = ["bevy_tape_drive_solari/dlss"] force_disable_dlss = ["bevy_tape_drive_solari/force_disable_dlss"] +force_tlas_through_wgpu_core = [ + "bevy_tape_drive_solari/force_tlas_through_wgpu_core", +] [dependencies] bevy_tape_drive_solari = { path = "crates/bevy_tape_drive_solari" } diff --git a/crates/bevy_tape_drive_solari/Cargo.toml b/crates/bevy_tape_drive_solari/Cargo.toml index c2a1e8e..0c7fe6b 100644 --- a/crates/bevy_tape_drive_solari/Cargo.toml +++ b/crates/bevy_tape_drive_solari/Cargo.toml @@ -53,6 +53,7 @@ wgpu = { version = "30", default-features = false } [features] dlss = ["bevy_anti_alias/dlss"] force_disable_dlss = ["bevy_anti_alias/force_disable_dlss"] +force_tlas_through_wgpu_core = [] [lints] workspace = true diff --git a/crates/bevy_tape_drive_solari/src/scene/binder/tlas.rs b/crates/bevy_tape_drive_solari/src/scene/binder/tlas.rs index fd3a87a..5d72960 100644 --- a/crates/bevy_tape_drive_solari/src/scene/binder/tlas.rs +++ b/crates/bevy_tape_drive_solari/src/scene/binder/tlas.rs @@ -356,19 +356,21 @@ pub fn build_raytracing_tlas( let bindings = &mut *bindings; let current_index = bindings.tlas.current_index; - //let built = match bindings.tlas.raw { - // Some(backend) => { - // setup_tlas_instances(bindings, &pipeline_cache, &pipeline, &mut render_context) - // && build_tlas_raw(bindings, backend, &mut render_context) - // } - // None => build_tlas_through_wgpu_core( - // bindings, - // &mesh_blas_manager, - // &tape_blas_manager, - // &mut render_context, - // ), - //}; + #[cfg(not(feature = "force_tlas_through_wgpu_core"))] + let built = match bindings.tlas.raw { + Some(backend) => { + setup_tlas_instances(bindings, &pipeline_cache, &pipeline, &mut render_context) + && build_tlas_raw(bindings, backend, &mut render_context) + } + None => build_tlas_through_wgpu_core( + bindings, + &mesh_blas_manager, + &tape_blas_manager, + &mut render_context, + ), + }; + #[cfg(feature = "force_tlas_through_wgpu_core")] let built = build_tlas_through_wgpu_core( bindings, &mesh_blas_manager, @@ -521,10 +523,12 @@ fn build_tlas_through_wgpu_core( // An empty scene leaves the index unswapped, so whatever is here belongs to // an earlier frame and is still being traced as the previous one if bindings.instances.slots.high_water_mark() == 0 { + bevy_log::warn!("Empty Scene!"); return false; } let current_index = bindings.tlas.current_index; let Some(tlas) = bindings.tlas.structures[current_index].as_mut() else { + bevy_log::warn!("No TLAS!"); return false; }; diff --git a/crates/bevy_tape_drive_solari/src/scene/tapes/blas.rs b/crates/bevy_tape_drive_solari/src/scene/tapes/blas.rs index ee57462..e0b9d0b 100644 --- a/crates/bevy_tape_drive_solari/src/scene/tapes/blas.rs +++ b/crates/bevy_tape_drive_solari/src/scene/tapes/blas.rs @@ -32,6 +32,7 @@ const TLAS_BUILDS_BEFORE_DELETION_ALLOWED: usize = 2; pub struct BlasManager { blas: HashMap, TapeBlas>, changed: Vec>, + needs_new_blas: Vec>, /// BLAS that are pending deletion, one batch per TLAS build. The back batch /// collects retirements since the last build, and every batch ahead of /// it has one more build to wait out. @@ -209,11 +210,13 @@ pub fn prepare_raytracing_blas( let (blas, _) = allocate_blas(&aabbs_slice, key, &render_device); blas_manager.insert(key, blas); + + blas_manager.needs_new_blas.push(key.tape) } } pub fn compile_raytracing_blas( - blas_manager: ResMut, + mut blas_manager: ResMut, tapes: Res>, tape_allocator: Res, render_device: Res, @@ -221,6 +224,11 @@ pub fn compile_raytracing_blas( pipeline_cache: Res, replacement_modules: Res, ) { + if blas_manager.needs_new_blas.is_empty() { + bevy_log::debug!("needs_new_blas is empty"); + return; + } + let diagnostics = render_context.diagnostic_recorder(); let diagnostics = diagnostics.as_deref(); let mut command_encoder = render_context.command_encoder(); @@ -240,6 +248,7 @@ pub fn compile_raytracing_blas( let tape_info = tapes .iter() + .filter(|(asset_id, _)| blas_manager.needs_new_blas.contains(asset_id)) .map(|(asset_id, tape)| { let key = BlasKey { tape: asset_id, @@ -259,6 +268,7 @@ pub fn compile_raytracing_blas( .collect::>(); // Build geometry into each BLAS + let mut updated_blases = vec![]; let build_entries = tape_info .iter() .filter_map(|(key, tape, aabbs_slice, metadata_slice, blas_size)| { @@ -284,6 +294,8 @@ pub fn compile_raytracing_blas( compute_pass.set_immediates(0, bytemuck::bytes_of(&pc)); compute_pass.dispatch_workgroups(1, 1, 1); + updated_blases.push(key.tape); + let geometry = BlasAabbGeometry { size: blas_size, stride: size_of::() as u64, @@ -301,6 +313,9 @@ pub fn compile_raytracing_blas( command_encoder.build_acceleration_structures(&build_entries, &[]); time_span.end(&mut command_encoder); + blas_manager + .needs_new_blas + .retain(|tape| !updated_blases.contains(tape)) } pub fn delete_raytracing_blas(