Skip to content

Commit

Permalink
make threaded rendering optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Feb 8, 2024
1 parent 41d1a56 commit c20a058
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 40 deletions.
101 changes: 61 additions & 40 deletions engine/src/gfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub struct GfxSettings {
pub shader_debug: bool,
pub pbr_enabled: bool,
pub fog_shader_debug: bool,
pub parallel_render: bool,
}

impl Default for GfxSettings {
Expand All @@ -154,6 +155,7 @@ impl Default for GfxSettings {
shader_debug: false,
pbr_enabled: true,
fog_shader_debug: false,
parallel_render: false,
}
}
}
Expand Down Expand Up @@ -680,53 +682,72 @@ impl GfxContext {
let objsref = &*objs;

let mut gui_elapsed = 0.0;
rayon::in_place_scope(|scope| {
scope.spawn(|_| {
encs.pbr = self.pbr_prepass();
});
scope.spawn(|_| {
encs.depth_prepass = Some(self.depth_prepass(objsref));
});
scope.spawn(|_| {
passes::render_ssao(self, &mut encs.before_main);
passes::render_fog(self, &mut encs.before_main);

passes::render_background(self, &mut encs.after_main, frame);
passes::gen_ui_blur(self, &mut encs.after_main, frame);
});
scope.spawn(|_| {
encs.smap = self.shadow_map_pass(objsref);
});
if self.settings.map(|v| v.parallel_render).unwrap_or(false) {
rayon::in_place_scope(|scope| {
scope.spawn(|_| {
encs.pbr = self.pbr_prepass();
});
scope.spawn(|_| {
encs.depth_prepass = Some(self.depth_prepass(objsref));
});
scope.spawn(|_| {
encs.smap = self.shadow_map_pass(objsref);
});
scope.spawn(|_| {
passes::render_ssao(self, &mut encs.before_main);
passes::render_fog(self, &mut encs.before_main);

scope.spawn(|_| {
encs.main = Some(self.main_render_pass(&frame, objsref));
});
passes::render_background(self, &mut encs.after_main, frame);
passes::gen_ui_blur(self, &mut encs.after_main, frame);
});

{
profiling::scope!("gfx::render_gui");
let gui_start = Instant::now();
let mut gui_enc = self
.device
.create_command_encoder(&CommandEncoderDescriptor {
label: Some("GUI encoder"),
});
render_gui(
state,
GuiRenderContext {
size: self.size,
gfx: self,
encoder: &mut gui_enc,
view: frame,
},
);
encs.gui = Some(gui_enc.finish());
gui_elapsed = gui_start.elapsed().as_secs_f32();
}
});
scope.spawn(|_| {
encs.main = Some(self.main_render_pass(&frame, objsref));
});

(gui_elapsed, encs.gui) = self.render_gui(frame, state, render_gui);
});
} else {
encs.pbr = self.pbr_prepass();
encs.depth_prepass = Some(self.depth_prepass(objsref));
encs.smap = self.shadow_map_pass(objsref);
passes::render_ssao(self, &mut encs.before_main);
passes::render_fog(self, &mut encs.before_main);
encs.main = Some(self.main_render_pass(&frame, objsref));
passes::render_background(self, &mut encs.after_main, frame);
passes::gen_ui_blur(self, &mut encs.after_main, frame);
(gui_elapsed, encs.gui) = self.render_gui(frame, state, render_gui);
}

(start_time.elapsed().as_secs_f32(), gui_elapsed)
}

fn render_gui<S>(
&self,
frame: &TextureView,
state: &mut S,
render_gui: impl FnOnce(&mut S, GuiRenderContext) + Sized,
) -> (f32, Option<CommandBuffer>) {
profiling::scope!("gfx::render_gui");
let gui_start = Instant::now();
let mut gui_enc = self
.device
.create_command_encoder(&CommandEncoderDescriptor {
label: Some("GUI encoder"),
});
render_gui(
state,
GuiRenderContext {
size: self.size,
gfx: self,
encoder: &mut gui_enc,
view: frame,
},
);
(gui_start.elapsed().as_secs_f32(), Some(gui_enc.finish()))
}

fn main_render_pass(
&self,
frame: &TextureView,
Expand Down
1 change: 1 addition & 0 deletions engine_demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl engine::framework::State for State {
ui.checkbox(&mut self.gfx_settings.fog, "Fog");
ui.checkbox(&mut self.gfx_settings.ssao, "SSAO");
ui.checkbox(&mut self.gfx_settings.terrain_grid, "Terrain grid");
ui.checkbox(&mut self.gfx_settings.parallel_render, "Threaded rendering");

let mut shadows = self.gfx_settings.shadows.size().is_some();
ui.checkbox(&mut shadows, "Shadows");
Expand Down
5 changes: 5 additions & 0 deletions native_app/src/newgui/hud/windows/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ pub fn settings(uiw: &UiWorld, _: &Simulation, opened: &mut bool) {
"Ambient Occlusion (SSAO)",
);
checkbox_value(&mut settings.gfx.vsync, on_secondary_container(), "VSync");
checkbox_value(
&mut settings.gfx.parallel_render,
on_secondary_container(),
"Threaded rendering",
);

minrow(5.0, || {
let mut id = settings.gfx.shadows as u8 as usize;
Expand Down

0 comments on commit c20a058

Please sign in to comment.