Fix restir

This commit is contained in:
2026-10-14 23:04:13 +01:00
parent e98dba8c45
commit b36396af70
4 changed files with 36 additions and 13 deletions
+3
View File
@@ -108,6 +108,9 @@ unused_qualifications = "warn"
# is off by default because a build with it needs the DLSS SDK. # is off by default because a build with it needs the DLSS SDK.
dlss = ["bevy_tape_drive_solari/dlss"] dlss = ["bevy_tape_drive_solari/dlss"]
force_disable_dlss = ["bevy_tape_drive_solari/force_disable_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] [dependencies]
bevy_tape_drive_solari = { path = "crates/bevy_tape_drive_solari" } bevy_tape_drive_solari = { path = "crates/bevy_tape_drive_solari" }
+1
View File
@@ -53,6 +53,7 @@ wgpu = { version = "30", default-features = false }
[features] [features]
dlss = ["bevy_anti_alias/dlss"] dlss = ["bevy_anti_alias/dlss"]
force_disable_dlss = ["bevy_anti_alias/force_disable_dlss"] force_disable_dlss = ["bevy_anti_alias/force_disable_dlss"]
force_tlas_through_wgpu_core = []
[lints] [lints]
workspace = true workspace = true
@@ -356,19 +356,21 @@ pub fn build_raytracing_tlas(
let bindings = &mut *bindings; let bindings = &mut *bindings;
let current_index = bindings.tlas.current_index; let current_index = bindings.tlas.current_index;
//let built = match bindings.tlas.raw { #[cfg(not(feature = "force_tlas_through_wgpu_core"))]
// Some(backend) => { let built = match bindings.tlas.raw {
// setup_tlas_instances(bindings, &pipeline_cache, &pipeline, &mut render_context) Some(backend) => {
// && build_tlas_raw(bindings, backend, &mut render_context) 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, None => build_tlas_through_wgpu_core(
// &mesh_blas_manager, bindings,
// &tape_blas_manager, &mesh_blas_manager,
// &mut render_context, &tape_blas_manager,
// ), &mut render_context,
//}; ),
};
#[cfg(feature = "force_tlas_through_wgpu_core")]
let built = build_tlas_through_wgpu_core( let built = build_tlas_through_wgpu_core(
bindings, bindings,
&mesh_blas_manager, &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 empty scene leaves the index unswapped, so whatever is here belongs to
// an earlier frame and is still being traced as the previous one // an earlier frame and is still being traced as the previous one
if bindings.instances.slots.high_water_mark() == 0 { if bindings.instances.slots.high_water_mark() == 0 {
bevy_log::warn!("Empty Scene!");
return false; return false;
} }
let current_index = bindings.tlas.current_index; let current_index = bindings.tlas.current_index;
let Some(tlas) = bindings.tlas.structures[current_index].as_mut() else { let Some(tlas) = bindings.tlas.structures[current_index].as_mut() else {
bevy_log::warn!("No TLAS!");
return false; return false;
}; };
@@ -32,6 +32,7 @@ const TLAS_BUILDS_BEFORE_DELETION_ALLOWED: usize = 2;
pub struct BlasManager { pub struct BlasManager {
blas: HashMap<AssetId<Tape>, TapeBlas>, blas: HashMap<AssetId<Tape>, TapeBlas>,
changed: Vec<AssetId<Tape>>, changed: Vec<AssetId<Tape>>,
needs_new_blas: Vec<AssetId<Tape>>,
/// BLAS that are pending deletion, one batch per TLAS build. The back batch /// BLAS that are pending deletion, one batch per TLAS build. The back batch
/// collects retirements since the last build, and every batch ahead of /// collects retirements since the last build, and every batch ahead of
/// it has one more build to wait out. /// 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); let (blas, _) = allocate_blas(&aabbs_slice, key, &render_device);
blas_manager.insert(key, blas); blas_manager.insert(key, blas);
blas_manager.needs_new_blas.push(key.tape)
} }
} }
pub fn compile_raytracing_blas( pub fn compile_raytracing_blas(
blas_manager: ResMut<BlasManager>, mut blas_manager: ResMut<BlasManager>,
tapes: Res<RenderAssets<RenderTape>>, tapes: Res<RenderAssets<RenderTape>>,
tape_allocator: Res<TapeAllocator>, tape_allocator: Res<TapeAllocator>,
render_device: Res<RenderDevice>, render_device: Res<RenderDevice>,
@@ -221,6 +224,11 @@ pub fn compile_raytracing_blas(
pipeline_cache: Res<PipelineCache>, pipeline_cache: Res<PipelineCache>,
replacement_modules: Res<ReplacementModules>, replacement_modules: Res<ReplacementModules>,
) { ) {
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 = render_context.diagnostic_recorder();
let diagnostics = diagnostics.as_deref(); let diagnostics = diagnostics.as_deref();
let mut command_encoder = render_context.command_encoder(); let mut command_encoder = render_context.command_encoder();
@@ -240,6 +248,7 @@ pub fn compile_raytracing_blas(
let tape_info = tapes let tape_info = tapes
.iter() .iter()
.filter(|(asset_id, _)| blas_manager.needs_new_blas.contains(asset_id))
.map(|(asset_id, tape)| { .map(|(asset_id, tape)| {
let key = BlasKey { let key = BlasKey {
tape: asset_id, tape: asset_id,
@@ -259,6 +268,7 @@ pub fn compile_raytracing_blas(
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// Build geometry into each BLAS // Build geometry into each BLAS
let mut updated_blases = vec![];
let build_entries = tape_info let build_entries = tape_info
.iter() .iter()
.filter_map(|(key, tape, aabbs_slice, metadata_slice, blas_size)| { .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.set_immediates(0, bytemuck::bytes_of(&pc));
compute_pass.dispatch_workgroups(1, 1, 1); compute_pass.dispatch_workgroups(1, 1, 1);
updated_blases.push(key.tape);
let geometry = BlasAabbGeometry { let geometry = BlasAabbGeometry {
size: blas_size, size: blas_size,
stride: size_of::<bevy_tape_load::render::Aabb>() as u64, stride: size_of::<bevy_tape_load::render::Aabb>() as u64,
@@ -301,6 +313,9 @@ pub fn compile_raytracing_blas(
command_encoder.build_acceleration_structures(&build_entries, &[]); command_encoder.build_acceleration_structures(&build_entries, &[]);
time_span.end(&mut command_encoder); time_span.end(&mut command_encoder);
blas_manager
.needs_new_blas
.retain(|tape| !updated_blases.contains(tape))
} }
pub fn delete_raytracing_blas( pub fn delete_raytracing_blas(