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] - Fix window size change panic #2858

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
4 changes: 2 additions & 2 deletions pipelined/bevy_render2/src/camera/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ fn extract_cameras(
ExtractedView {
projection: camera.projection_matrix,
transform: *transform,
width: window.physical_width(),
height: window.physical_height(),
width: window.physical_width().max(1),
height: window.physical_height().max(1),
},
));
}
Expand Down
62 changes: 46 additions & 16 deletions pipelined/bevy_render2/src/view/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use crate::{
render_resource::TextureView,
renderer::{RenderDevice, RenderInstance},
texture::BevyDefault,
RenderApp, RenderStage,
RenderApp, RenderStage, RenderWorld,
};
use bevy_app::{App, Plugin};
use bevy_ecs::prelude::*;
use bevy_utils::HashMap;
use bevy_utils::{tracing::debug, HashMap};
use bevy_window::{RawWindowHandleWrapper, WindowId, Windows};
use std::ops::{Deref, DerefMut};
use wgpu::TextureFormat;
Expand All @@ -20,6 +20,7 @@ pub struct WindowRenderPlugin;
impl Plugin for WindowRenderPlugin {
fn build(&self, app: &mut App) {
app.sub_app(RenderApp)
.init_resource::<ExtractedWindows>()
.init_resource::<WindowSurfaces>()
.init_resource::<NonSendMarker>()
.add_system_to_stage(RenderStage::Extract, extract_windows)
Expand All @@ -34,6 +35,7 @@ pub struct ExtractedWindow {
pub physical_height: u32,
pub vsync: bool,
pub swap_chain_frame: Option<TextureView>,
pub size_changed: bool,
}

#[derive(Default)]
Expand All @@ -55,23 +57,44 @@ impl DerefMut for ExtractedWindows {
}
}

fn extract_windows(mut commands: Commands, windows: Res<Windows>) {
let mut extracted_windows = ExtractedWindows::default();
fn extract_windows(mut render_world: ResMut<RenderWorld>, windows: Res<Windows>) {
let mut extracted_windows = render_world.get_resource_mut::<ExtractedWindows>().unwrap();
for window in windows.iter() {
extracted_windows.insert(
window.id(),
ExtractedWindow {
id: window.id(),
handle: window.raw_window_handle(),
physical_width: window.physical_width(),
physical_height: window.physical_height(),
vsync: window.vsync(),
swap_chain_frame: None,
},
let (new_width, new_height) = (
window.physical_width().max(1),
window.physical_height().max(1),
);
}

commands.insert_resource(extracted_windows);
let mut extracted_window =
extracted_windows
.entry(window.id())
.or_insert(ExtractedWindow {
id: window.id(),
handle: window.raw_window_handle(),
physical_width: new_width,
physical_height: new_height,
vsync: window.vsync(),
swap_chain_frame: None,
size_changed: false,
});

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

if extracted_window.size_changed {
debug!(
"Window size changed from {}x{} to {}x{}",
extracted_window.physical_width,
extracted_window.physical_height,
new_width,
new_height
);
extracted_window.physical_width = new_width;
extracted_window.physical_height = new_height;
}
}
}

#[derive(Default)]
Expand Down Expand Up @@ -111,6 +134,13 @@ pub fn prepare_windows(
},
};

if window.size_changed {
window_surfaces.swap_chains.insert(
window.id,
render_device.create_swap_chain(surface, &swap_chain_descriptor),
);
}

let swap_chain = window_surfaces
.swap_chains
.entry(window.id)
Expand Down