Skip to content

Commit

Permalink
Optimizations for Winit Examples. (#686)
Browse files Browse the repository at this point in the history
This optimizes examples by using `ControlFlow::Wait` instead of
`ControlFlow::Poll`. This fix was found by Micah Johnston, and this PR
implements it in both "simple" and "with_winit" examples.

- "simple" now runs at 0.1-0.2% CPU usage.
- "with_winit" now runs at 1.0-1.8%.

[You can check more info about it
here](https://xi.zulipchat.com/#narrow/stream/147921-general/topic/I've.20made.20a.20Vello.20game.20engine.20to.20prove.20that.20i.20was.20wrong.2E/near/468637207)

---------

Co-authored-by: Daniel McNab <[email protected]>
  • Loading branch information
TheNachoBIT and DJMcNab authored Sep 11, 2024
1 parent 3c9343b commit e777e08
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
8 changes: 2 additions & 6 deletions examples/simple/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use vello::{AaConfig, DebugLayers, Renderer, RendererOptions, Scene};
use winit::application::ApplicationHandler;
use winit::dpi::LogicalSize;
use winit::event::*;
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::event_loop::{ActiveEventLoop, EventLoop};
use winit::window::Window;

use vello::wgpu;
Expand Down Expand Up @@ -74,15 +74,12 @@ impl<'s> ApplicationHandler for SimpleVelloApp<'s> {

// Save the Window and Surface to a state variable
self.state = RenderState::Active(ActiveRenderState { window, surface });

event_loop.set_control_flow(ControlFlow::Poll);
}

fn suspended(&mut self, event_loop: &ActiveEventLoop) {
fn suspended(&mut self, _event_loop: &ActiveEventLoop) {
if let RenderState::Active(state) = &self.state {
self.state = RenderState::Suspended(Some(state.window.clone()));
}
event_loop.set_control_flow(ControlFlow::Wait);
}

fn window_event(
Expand All @@ -109,7 +106,6 @@ impl<'s> ApplicationHandler for SimpleVelloApp<'s> {
WindowEvent::Resized(size) => {
self.context
.resize_surface(&mut render_state.surface, size.width, size.height);
render_state.window.request_redraw();
}

// This is where all the rendering happens
Expand Down
7 changes: 3 additions & 4 deletions examples/with_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use std::time::Instant;
use web_time::Instant;
use winit::application::ApplicationHandler;
use winit::event::*;
use winit::event_loop::ControlFlow;
use winit::keyboard::*;

#[cfg(all(feature = "wgpu-profiler", not(target_arch = "wasm32")))]
Expand Down Expand Up @@ -226,7 +225,6 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
});
Some(render_state)
};
event_loop.set_control_flow(ControlFlow::Poll);
}

fn window_event(
Expand Down Expand Up @@ -443,6 +441,8 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
let _rendering_span = tracing::trace_span!("Actioning Requested Redraw").entered();
let encoding_span = tracing::trace_span!("Encoding scene").entered();

render_state.window.request_redraw();

let Some(RenderState { surface, window }) = &self.state else {
return;
};
Expand Down Expand Up @@ -633,14 +633,13 @@ impl<'s> ApplicationHandler<UserEvent> for VelloApp<'s> {
}
}

fn suspended(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
fn suspended(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop) {
log::info!("Suspending");
#[cfg(not(target_arch = "wasm32"))]
// When we suspend, we need to remove the `wgpu` Surface
if let Some(render_state) = self.state.take() {
self.cached_window = Some(render_state.window);
}
event_loop.set_control_flow(ControlFlow::Wait);
}
}

Expand Down

0 comments on commit e777e08

Please sign in to comment.