Skip to content

Commit fa0d610

Browse files
authored
Solari: Add DLSS-RR toggle to example (#24667)
# Objective - Make it easier to switch between denoised and non-denoised mode in the solari example for development purposes ## Solution - Add a toggle
1 parent ad07c62 commit fa0d610

1 file changed

Lines changed: 82 additions & 24 deletions

File tree

examples/3d/solari.rs

Lines changed: 82 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ use rand::{RngExt, SeedableRng};
2222
use std::f32::consts::PI;
2323

2424
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
25-
use bevy::anti_alias::dlss::{
26-
Dlss, DlssProjectId, DlssRayReconstructionFeature, DlssRayReconstructionSupported,
25+
use bevy::{
26+
anti_alias::dlss::{
27+
Dlss, DlssProjectId, DlssRayReconstructionFeature, DlssRayReconstructionSupported,
28+
},
29+
render::camera::{MipBias, TemporalJitter},
2730
};
2831

2932
/// `bevy_solari` demo.
@@ -64,11 +67,13 @@ fn main() {
6467
if args.pathtracer == Some(true) {
6568
app.add_plugins(PathtracingPlugin);
6669
} else {
70+
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
71+
app.add_systems(Update, toggle_dlss_rr);
72+
6773
if args.many_lights != Some(true) {
68-
app.add_systems(Update, (pause_scene, toggle_lights, patrol_path))
69-
.add_systems(PostUpdate, update_control_text);
74+
app.add_systems(Update, (pause_scene, toggle_lights, patrol_path));
7075
}
71-
app.add_systems(PostUpdate, update_performance_text);
76+
app.add_systems(PostUpdate, (update_control_text, update_performance_text));
7277
}
7378

7479
app.run();
@@ -351,6 +356,17 @@ fn setup_many_lights(
351356
});
352357
}
353358

359+
commands.spawn((
360+
ControlText,
361+
Text::default(),
362+
Node {
363+
position_type: PositionType::Absolute,
364+
bottom: px(12.0),
365+
left: px(12.0),
366+
..default()
367+
},
368+
));
369+
354370
commands.spawn((
355371
Node {
356372
position_type: PositionType::Absolute,
@@ -443,6 +459,31 @@ fn add_raytracing_meshes_on_scene_load(
443459
}
444460
}
445461

462+
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
463+
fn toggle_dlss_rr(
464+
key_input: Res<ButtonInput<KeyCode>>,
465+
camera: Single<(Entity, Has<Dlss<DlssRayReconstructionFeature>>), With<SolariLighting>>,
466+
dlss_rr_supported: Option<Res<DlssRayReconstructionSupported>>,
467+
mut commands: Commands,
468+
) {
469+
if key_input.just_pressed(KeyCode::Digit3) && dlss_rr_supported.is_some() {
470+
let (entity, dlss) = *camera;
471+
if dlss {
472+
commands
473+
.entity(entity)
474+
.remove::<(Dlss<DlssRayReconstructionFeature>, TemporalJitter, MipBias)>();
475+
} else {
476+
commands
477+
.entity(entity)
478+
.insert(Dlss::<DlssRayReconstructionFeature> {
479+
perf_quality_mode: Default::default(),
480+
reset: Default::default(),
481+
_phantom_data: Default::default(),
482+
});
483+
}
484+
}
485+
}
486+
446487
fn pause_scene(mut time: ResMut<Time<Virtual>>, key_input: Res<ButtonInput<KeyCode>>) {
447488
if key_input.just_pressed(KeyCode::Space) {
448489
time.toggle();
@@ -531,37 +572,47 @@ fn update_control_text(
531572
materials: Res<Assets<StandardMaterial>>,
532573
directional_light: Query<Entity, With<DirectionalLight>>,
533574
time: Res<Time<Virtual>>,
575+
args: Res<Args>,
534576
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))] dlss_rr_supported: Option<
535577
Res<DlssRayReconstructionSupported>,
536578
>,
579+
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))] dlss_camera: Query<
580+
Has<Dlss<DlssRayReconstructionFeature>>,
581+
With<SolariLighting>,
582+
>,
537583
) {
538584
text.0.clear();
539585

540-
if time.is_paused() {
541-
text.0.push_str("(Space): Resume");
542-
} else {
543-
text.0.push_str("(Space): Pause");
544-
}
545-
546-
if directional_light.single().is_ok() {
547-
text.0.push_str("\n(1): Disable directional light");
548-
} else {
549-
text.0.push_str("\n(1): Enable directional light");
550-
}
586+
if args.many_lights != Some(true) {
587+
if time.is_paused() {
588+
text.0.push_str("(Space): Resume");
589+
} else {
590+
text.0.push_str("(Space): Pause");
591+
}
551592

552-
match robot_light_material.and_then(|m| materials.get(&m.0)) {
553-
Some(robot_light_material) if robot_light_material.emissive != LinearRgba::BLACK => {
554-
text.0.push_str("\n(2): Disable robot emissive light");
593+
if directional_light.single().is_ok() {
594+
text.0.push_str("\n(1): Disable directional light");
595+
} else {
596+
text.0.push_str("\n(1): Enable directional light");
555597
}
556-
_ => {
557-
text.0.push_str("\n(2): Enable robot emissive light");
598+
599+
match robot_light_material.and_then(|m| materials.get(&m.0)) {
600+
Some(robot_light_material) if robot_light_material.emissive != LinearRgba::BLACK => {
601+
text.0.push_str("\n(2): Disable robot emissive light");
602+
}
603+
_ => {
604+
text.0.push_str("\n(2): Enable robot emissive light");
605+
}
558606
}
559607
}
560608

561609
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
562610
if dlss_rr_supported.is_some() {
563-
text.0
564-
.push_str("\nDenoising: DLSS Ray Reconstruction enabled");
611+
if matches!(dlss_camera.single(), Ok(true)) {
612+
text.0.push_str("\n(3): Disable DLSS Ray Reconstruction");
613+
} else {
614+
text.0.push_str("\n(3): Enable DLSS Ray Reconstruction");
615+
}
565616
} else {
566617
text.0
567618
.push_str("\nDenoising: DLSS Ray Reconstruction not supported");
@@ -578,6 +629,10 @@ struct PerformanceText;
578629
fn update_performance_text(
579630
mut text: Single<&mut Text, With<PerformanceText>>,
580631
diagnostics: Res<DiagnosticsStore>,
632+
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))] dlss_camera: Query<
633+
Has<Dlss<DlssRayReconstructionFeature>>,
634+
With<SolariLighting>,
635+
>,
581636
) {
582637
text.0.clear();
583638

@@ -610,7 +665,10 @@ fn update_performance_text(
610665
"Specular indirect",
611666
"render/solari_lighting/specular_indirect_lighting/elapsed_gpu",
612667
);
613-
(add_diagnostic)("DLSS-RR", "render/dlss_ray_reconstruction/elapsed_gpu");
668+
#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))]
669+
if matches!(dlss_camera.single(), Ok(true)) {
670+
(add_diagnostic)("DLSS-RR", "render/dlss_ray_reconstruction/elapsed_gpu");
671+
}
614672
text.push_str(&format!("{:17} {total:.2} ms\n", "Total"));
615673

616674
if let Some(world_cache_active_cells_count) = diagnostics

0 commit comments

Comments
 (0)