Skip to content

Commit

Permalink
Change WinitPlugin defaults to limit game update rate when window i…
Browse files Browse the repository at this point in the history
…s not visible (bevyengine#7611)

Fixes bevyengine#5856. Fixes bevyengine#8080. Fixes bevyengine#9040.

# Objective

We need to limit the update rate of games whose windows are not visible
(minimized or completely occluded). Compositors typically ignore the
VSync settings of windows that aren't visible. That, combined with the
lack of rendering work, results in a scenario where an app becomes
completely CPU-bound and starts updating without limit.

There are currently three update modes.
- `Continuous` updates an app as often as possible.
- `Reactive` updates when new window or device events have appeared, a
timer expires, or a redraw is requested.
- `ReactiveLowPower` is the same as `Reactive` except it ignores device
events (e.g. general mouse movement).

The problem is that the default "game" settings are set to `Contiuous`
even when no windows are visible.

### More Context

- libsdl-org/SDL#1871
- glfw/glfw#680
- godotengine/godot#19741
- godotengine/godot#64708

## Solution

Change the default "unfocused" `UpdateMode` for games to
`ReactiveLowPower` just like desktop apps. This way, even when the
window is occluded, the app still updates at a sensible rate or if
something about the window changes. I chose 20Hz arbitrarily.
  • Loading branch information
maniwani authored Aug 2, 2023
1 parent 77fa8a9 commit bbf1c23
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion crates/bevy_winit/src/winit_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ pub struct WinitSettings {

impl WinitSettings {
/// Default settings for games.
///
/// [`Continuous`](UpdateMode::Continuous) if windows have focus,
/// [`ReactiveLowPower`](UpdateMode::ReactiveLowPower) otherwise.
pub fn game() -> Self {
WinitSettings::default()
WinitSettings {
focused_mode: UpdateMode::Continuous,
unfocused_mode: UpdateMode::ReactiveLowPower {
wait: Duration::from_secs_f64(1.0 / 60.0), // 60Hz
},
..Default::default()
}
}

/// Default settings for desktop applications.
Expand Down

0 comments on commit bbf1c23

Please sign in to comment.