Skip to content
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

[Merged by Bors] - Reconfigure surface on present mode change #6049

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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