Skip to content

Commit

Permalink
FrameConfig and the frozen ability
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jul 12, 2024
1 parent 9884094 commit 473b71b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
24 changes: 16 additions & 8 deletions blade-render/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,14 @@ impl ShaderPipelines {
}
}

#[derive(Clone, Copy, Default)]
pub struct FrameConfig {
pub frozen: bool,
pub debug_draw: bool,
pub reset_variance: bool,
pub reset_reservoirs: bool,
}

/// Temporary resources associated with a GPU frame.
#[derive(Default)]
pub struct FrameResources {
Expand Down Expand Up @@ -1041,28 +1049,26 @@ impl Renderer {
&mut self,
command_encoder: &mut blade_graphics::CommandEncoder,
camera: &crate::Camera,
enable_debug_draw: bool,
accumulate_variance: bool,
reset_reservoirs: bool,
config: FrameConfig,
) {
let mut transfer = command_encoder.transfer();

if enable_debug_draw {
if config.debug_draw {
self.debug.reset_lines(&mut transfer);
self.debug.enable_draw(&mut transfer, true);
} else {
self.debug.enable_draw(&mut transfer, false);
}

if reset_reservoirs || !accumulate_variance {
if config.reset_reservoirs || config.reset_variance {
self.debug.reset_variance(&mut transfer);
} else {
self.debug.update_variance(&mut transfer);
}
self.debug.update_entry(&mut transfer);

if reset_reservoirs {
if !enable_debug_draw {
if config.reset_reservoirs {
if !config.debug_draw {
self.debug.reset_lines(&mut transfer);
}
let total_reservoirs = self.surface_size.width as u64 * self.surface_size.height as u64;
Expand All @@ -1075,7 +1081,9 @@ impl Renderer {
}
}

self.frame_index += 1;
if !config.frozen {
self.frame_index += 1;
}
self.targets.camera_params[self.frame_index % 2] = self.make_camera_params(camera);
self.post_proc_input_index = self.frame_index % 2;
}
Expand Down
15 changes: 7 additions & 8 deletions examples/move/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,13 @@ impl Game {
fn on_draw(&mut self) -> time::Duration {
self.update_time();

self.engine.set_debug(
self.is_point_selected,
if self.is_debug_active {
Some(self.last_mouse_pos)
} else {
None
},
);
self.engine.frame_config.frozen = self.is_paused;
self.engine.frame_config.debug_draw = self.is_point_selected;
self.engine.set_debug_pixel(if self.is_debug_active {
Some(self.last_mouse_pos)
} else {
None
});

let raw_input = self.egui_state.take_egui_input(&self.window);
let egui_context = self.egui_state.egui_ctx().clone();
Expand Down
9 changes: 6 additions & 3 deletions examples/scene/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,12 @@ impl Example {
self.renderer.prepare(
command_encoder,
&self.camera.inner,
self.is_point_selected || self.is_file_hovered,
self.debug.mouse_pos.is_some(),
self.need_accumulation_reset,
blade_render::FrameConfig {
frozen: false,
debug_draw: self.is_point_selected || self.is_file_hovered,
reset_variance: self.debug.mouse_pos.is_none(),
reset_reservoirs: self.need_accumulation_reset,
},
);
self.need_accumulation_reset = false;

Expand Down
29 changes: 15 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,7 @@ pub struct Engine {
selected_collider: Option<rapier3d::geometry::ColliderHandle>,
render_objects: Vec<blade_render::Object>,
debug: blade_render::DebugConfig,
need_accumulation_reset: bool,
pub is_debug_drawing: bool,
pub frame_config: blade_render::FrameConfig,
pub ray_config: blade_render::RayConfig,
pub denoiser_enabled: bool,
pub denoiser_config: blade_render::DenoiserConfig,
Expand Down Expand Up @@ -472,8 +471,12 @@ impl Engine {
selected_collider: None,
render_objects: Vec::new(),
debug: blade_render::DebugConfig::default(),
need_accumulation_reset: true,
is_debug_drawing: true,
frame_config: blade_render::FrameConfig {
frozen: false,
debug_draw: true,
reset_variance: false,
reset_reservoirs: true,
},
ray_config: blade_render::RayConfig {
num_environment_samples: 1,
environment_importance_sampling: false,
Expand Down Expand Up @@ -529,7 +532,7 @@ impl Engine {
scale_factor: f32,
) {
if self.track_hot_reloads {
self.need_accumulation_reset |= self.renderer.hot_reload(
self.frame_config.reset_reservoirs |= self.renderer.hot_reload(
&self.asset_hub,
&self.gpu_context,
self.pacer.last_sync_point().unwrap(),
Expand All @@ -550,8 +553,9 @@ impl Engine {
if new_render_size != self.renderer.get_surface_size() {
self.renderer
.resize_screen(new_render_size, command_encoder, &self.gpu_context);
self.need_accumulation_reset = true;
self.frame_config.reset_reservoirs = true;
}
self.frame_config.reset_variance = self.debug.mouse_pos.is_none();

self.gui_painter
.update_textures(command_encoder, gui_textures, &self.gpu_context);
Expand Down Expand Up @@ -612,11 +616,9 @@ impl Engine {
fov_y: camera.fov_y,
depth: MAX_DEPTH,
},
self.is_debug_drawing,
self.debug.mouse_pos.is_some(),
self.need_accumulation_reset,
self.frame_config,
);
self.need_accumulation_reset = false;
self.frame_config.reset_reservoirs = false;

if !self.render_objects.is_empty() {
self.renderer
Expand Down Expand Up @@ -727,8 +729,8 @@ impl Engine {
.show(ui, |ui| {
let old_config = self.ray_config;
self.ray_config.populate_hud(ui);
self.need_accumulation_reset |= self.ray_config != old_config;
self.need_accumulation_reset |= ui.button("Reset Accumulation").clicked();
self.frame_config.reset_reservoirs |= self.ray_config != old_config;
self.frame_config.reset_reservoirs |= ui.button("Reset Accumulation").clicked();
ui.checkbox(&mut self.denoiser_enabled, "Enable Denoiser");
self.denoiser_config.populate_hud(ui);
self.post_proc_config.populate_hud(ui);
Expand Down Expand Up @@ -1174,8 +1176,7 @@ impl Engine {
self.post_proc_config.average_luminocity = avg_lum;
}

pub fn set_debug(&mut self, update: bool, mouse_pos: Option<[i32; 2]>) {
self.is_debug_drawing = update;
pub fn set_debug_pixel(&mut self, mouse_pos: Option<[i32; 2]>) {
self.debug.mouse_pos = mouse_pos;
}
}

0 comments on commit 473b71b

Please sign in to comment.