Skip to content

Commit

Permalink
Reconfigure surface on present mode change (bevyengine#6049)
Browse files Browse the repository at this point in the history
# Objective

- Reconfigure surface after present mode changes. It seems that this is not done currently at runtime. It's pretty common for games to change such graphical settings at runtime.
- Fixes present mode issue in bevyengine#5111 

## Solution

- Exactly like resolution change gets tracked when extracting window, do the same for present mode.

Additionally, I added present mode (vsync) toggling to window settings example.
  • Loading branch information
hakolao authored and james7132 committed Oct 19, 2022
1 parent 249dfa2 commit 633671b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
20 changes: 18 additions & 2 deletions crates/bevy_render/src/view/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct ExtractedWindow {
pub present_mode: PresentMode,
pub swap_chain_texture: Option<TextureView>,
pub size_changed: bool,
pub present_mode_changed: bool,
}

#[derive(Default, Resource)]
Expand Down Expand Up @@ -77,6 +78,7 @@ fn extract_windows(
window.physical_width().max(1),
window.physical_height().max(1),
);
let new_present_mode = window.present_mode();

let mut extracted_window =
extracted_windows
Expand All @@ -89,12 +91,14 @@ fn extract_windows(
present_mode: window.present_mode(),
swap_chain_texture: None,
size_changed: false,
present_mode_changed: false,
});

// NOTE: Drop the swap chain frame here
extracted_window.swap_chain_texture = None;
extracted_window.size_changed = new_width != extracted_window.physical_width
|| new_height != extracted_window.physical_height;
extracted_window.present_mode_changed = new_present_mode != extracted_window.present_mode;

if extracted_window.size_changed {
debug!(
Expand All @@ -107,6 +111,14 @@ fn extract_windows(
extracted_window.physical_width = new_width;
extracted_window.physical_height = new_height;
}

if extracted_window.present_mode_changed {
debug!(
"Window Present Mode changed from {:?} to {:?}",
extracted_window.present_mode, new_present_mode
);
extracted_window.present_mode = new_present_mode;
}
}
for closed_window in closed.iter() {
extracted_windows.remove(&closed_window.id);
Expand Down Expand Up @@ -174,8 +186,12 @@ pub fn prepare_windows(
},
};

// Do the initial surface configuration if it hasn't been configured yet
if window_surfaces.configured_windows.insert(window.id) || window.size_changed {
// Do the initial surface configuration if it hasn't been configured yet. Or if size or
// present mode changed.
if window_surfaces.configured_windows.insert(window.id)
|| window.size_changed
|| window.present_mode_changed
{
render_device.configure_surface(surface, &swap_chain_descriptor);
}

Expand Down
19 changes: 19 additions & 0 deletions examples/window/window_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! the mouse pointer in various ways.

use bevy::{prelude::*, window::PresentMode};
use bevy_internal::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};

fn main() {
App::new()
Expand All @@ -13,12 +14,30 @@ fn main() {
..default()
})
.add_plugins(DefaultPlugins)
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugin(FrameTimeDiagnosticsPlugin)
.add_system(change_title)
.add_system(toggle_cursor)
.add_system(toggle_vsync)
.add_system(cycle_cursor_icon)
.run();
}

/// This system toggles the vsync mode when pressing the button V.
/// You'll see fps increase displayed in the console.
fn toggle_vsync(input: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
if input.just_pressed(KeyCode::V) {
let window = windows.primary_mut();

window.set_present_mode(if matches!(window.present_mode(), PresentMode::AutoVsync) {
PresentMode::AutoNoVsync
} else {
PresentMode::AutoVsync
});
info!("PRESENT_MODE: {:?}", window.present_mode());
}
}

/// This system will then change the title during execution
fn change_title(time: Res<Time>, mut windows: ResMut<Windows>) {
let window = windows.primary_mut();
Expand Down

0 comments on commit 633671b

Please sign in to comment.