-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
[Merged by Bors] - Reduce power usage with configurable event loop #3974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
f7ca793
Implement low power updates
aevyrie 564d9eb
Move camera closer
aevyrie 828090b
Make example more resource intensive
aevyrie 08b590b
Fix bug with delayed events
aevyrie 1e380e6
Bugfixes
aevyrie a41fe02
Improve example clarity
aevyrie 2cc429c
Fix bug where redraw requests prevent app exit
aevyrie 4856da1
Switch example from array of cubes to disabled vsync
aevyrie b8d0531
Add example to readme
aevyrie 94c60cc
Fix bug preventing `WinitConfig` resource changes from updating in th…
aevyrie a889087
Fleshed out example
aevyrie e4a0d61
Improve example comments for new functionality
aevyrie 6af14ce
Fix duplicate app update and perf bugs
aevyrie 989f128
clippy
aevyrie aefd419
Add bevy type `UpdateMode` to config event loop.
aevyrie 6299712
improve presets and docs
aevyrie 5917fdc
Merge remote-tracking branch 'upstream/main' into low-power_winit
aevyrie 75a6bfe
lint
aevyrie 75693ad
Update crates/bevy_winit/src/lib.rs
aevyrie f764b78
Update crates/bevy_winit/src/lib.rs
aevyrie 4895873
Update crates/bevy_winit/src/lib.rs
aevyrie 24903fa
Apply suggestions from code review
aevyrie de19a81
Code review cleanup
aevyrie 464f610
Doc corrections.
aevyrie 350d67a
Switch example to RMB
aevyrie 33413c1
Add frame counter and final touches to example
aevyrie ac921b3
Contextualize example comments, improvements
aevyrie 016a6f2
Update examples/window/low_power.rs
aevyrie c259331
Apply suggestions from code review
aevyrie 8df90d3
Review feedback: use captured var instead of Res<>
aevyrie f4d3e44
PR review feedback
aevyrie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,89 @@ | ||
| use bevy_utils::Duration; | ||
|
|
||
| /// A resource for configuring usage of the `rust_winit` library. | ||
| #[derive(Debug, Default)] | ||
| pub struct WinitConfig { | ||
| /// Configures the winit library to return control to the main thread after | ||
| /// the [run](bevy_app::App::run) loop is exited. Winit strongly recommends | ||
| /// avoiding this when possible. Before using this please read and understand | ||
| /// the [caveats](winit::platform::run_return::EventLoopExtRunReturn::run_return) | ||
| /// in the winit documentation. | ||
| /// | ||
| /// This feature is only available on desktop `target_os` configurations. | ||
| /// Namely `windows`, `macos`, `linux`, `dragonfly`, `freebsd`, `netbsd`, and | ||
| /// `openbsd`. If set to true on an unsupported platform | ||
| /// [run](bevy_app::App::run) will panic. | ||
| #[derive(Debug)] | ||
| pub struct WinitSettings { | ||
| /// Configures the winit library to return control to the main thread after the | ||
| /// [run](bevy_app::App::run) loop is exited. Winit strongly recommends avoiding this when | ||
| /// possible. Before using this please read and understand the | ||
| /// [caveats](winit::platform::run_return::EventLoopExtRunReturn::run_return) in the winit | ||
| /// documentation. | ||
| /// | ||
| /// This feature is only available on desktop `target_os` configurations. Namely `windows`, | ||
| /// `macos`, `linux`, `dragonfly`, `freebsd`, `netbsd`, and `openbsd`. If set to true on an | ||
| /// unsupported platform [run](bevy_app::App::run) will panic. | ||
| pub return_from_run: bool, | ||
| /// Configures how the winit event loop updates while the window is focused. | ||
| pub focused_mode: UpdateMode, | ||
| /// Configures how the winit event loop updates while the window is *not* focused. | ||
| pub unfocused_mode: UpdateMode, | ||
| } | ||
| impl WinitSettings { | ||
| /// Configure winit with common settings for a game. | ||
| pub fn game() -> Self { | ||
| WinitSettings::default() | ||
| } | ||
|
|
||
| /// Configure winit with common settings for a desktop application. | ||
| pub fn desktop_app() -> Self { | ||
| WinitSettings { | ||
| focused_mode: UpdateMode::Reactive { | ||
| max_wait: Duration::from_secs(5), | ||
| }, | ||
| unfocused_mode: UpdateMode::ReactiveLowPower { | ||
| max_wait: Duration::from_secs(60), | ||
| }, | ||
| ..Default::default() | ||
| } | ||
| } | ||
|
|
||
| /// Gets the configured `UpdateMode` depending on whether the window is focused or not | ||
| pub fn update_mode(&self, focused: bool) -> &UpdateMode { | ||
aevyrie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| match focused { | ||
| true => &self.focused_mode, | ||
| false => &self.unfocused_mode, | ||
| } | ||
| } | ||
| } | ||
| impl Default for WinitSettings { | ||
| fn default() -> Self { | ||
| WinitSettings { | ||
| return_from_run: false, | ||
| focused_mode: UpdateMode::Continuous, | ||
| unfocused_mode: UpdateMode::Continuous, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Configure how the winit event loop should update. | ||
| #[derive(Debug)] | ||
| pub enum UpdateMode { | ||
| /// The event loop will update continuously, running as fast as possible. | ||
| Continuous, | ||
| /// The event loop will only update if there is a winit event, a redraw is requested, or the | ||
| /// maximum wait time has elapsed. | ||
| /// | ||
| /// ## Note | ||
| /// | ||
| /// Once the app has executed all bevy systems and reaches the end of the event loop, there is | ||
| /// no way to force the app to wake and update again, unless a `winit` event (such as user | ||
| /// input, or the window being resized) is received or the time limit is reached. | ||
| Reactive { max_wait: Duration }, | ||
| /// The event loop will only update if there is a winit event from direct interaction with the | ||
| /// window (e.g. mouseover), a redraw is requested, or the maximum wait time has elapsed. | ||
| /// | ||
| /// ## Note | ||
| /// | ||
| /// Once the app has executed all bevy systems and reaches the end of the event loop, there is | ||
| /// no way to force the app to wake and update again, unless a `winit` event (such as user | ||
| /// input, or the window being resized) is received or the time limit is reached. | ||
| /// | ||
| /// ## Differences from [`UpdateMode::Reactive`] | ||
| /// | ||
| /// Unlike [`UpdateMode::Reactive`], this mode will ignore winit events that aren't directly | ||
| /// caused by interaction with the window. For example, you might want to use this mode when the | ||
| /// window is not focused, to only re-draw your bevy app when the cursor is over the window, but | ||
| /// not when the mouse moves somewhere else on the screen. This helps to significantly reduce | ||
| /// power consumption by only updated the app when absolutely necessary. | ||
| ReactiveLowPower { max_wait: Duration }, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.