From e2d6d2df77e934726045ecd4d8b3245c60ce3712 Mon Sep 17 00:00:00 2001 From: nlevy Date: Mon, 6 Dec 2021 14:34:47 +0100 Subject: [PATCH] Check window size before redrawing --- examples/integration_wgpu/src/main.rs | 56 +++++++++++++++++++-------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/examples/integration_wgpu/src/main.rs b/examples/integration_wgpu/src/main.rs index 35a69a7d72..79a9d02d4b 100644 --- a/examples/integration_wgpu/src/main.rs +++ b/examples/integration_wgpu/src/main.rs @@ -5,13 +5,18 @@ use controls::Controls; use scene::Scene; use iced_wgpu::{wgpu, Backend, Renderer, Settings, Viewport}; -use iced_winit::{conversion, futures, program, winit, Clipboard, Debug, Size}; +use iced_winit::{ + conversion, futures, program, + winit::{self, dpi::PhysicalSize}, + Clipboard, Debug, Size, +}; use futures::task::SpawnExt; use winit::{ dpi::PhysicalPosition, event::{Event, ModifiersState, WindowEvent}, event_loop::{ControlFlow, EventLoop}, + window::Window, }; pub fn main() { @@ -19,7 +24,7 @@ pub fn main() { // Initialize winit let event_loop = EventLoop::new(); - let window = winit::window::Window::new(&event_loop).unwrap(); + let window = Window::new(&event_loop).unwrap(); let physical_size = window.inner_size(); let mut viewport = Viewport::with_physical_size( @@ -77,6 +82,7 @@ pub fn main() { ) }; let mut resized = false; + let mut last_processed_size = window.inner_size(); // Initialize staging belt and local pool let mut staging_belt = wgpu::util::StagingBelt::new(5 * 1024); @@ -112,11 +118,8 @@ pub fn main() { WindowEvent::ModifiersChanged(new_modifiers) => { modifiers = new_modifiers; } - WindowEvent::Resized(new_size) => { - viewport = Viewport::with_physical_size( - Size::new(new_size.width, new_size.height), - window.scale_factor(), - ); + WindowEvent::Resized(_) => { + process_resize(&mut viewport, &mut last_processed_size, &window); resized = true; } @@ -156,15 +159,21 @@ pub fn main() { } Event::RedrawRequested(_) => { if resized { - let size = window.inner_size(); - + if last_processed_size != window.inner_size() { + // This can happen when resizing the window quickly, we enter this branch + // with the wrong window size, wich could cause validations errors + process_resize(&mut viewport, &mut last_processed_size, &window); + resized = true; + window.request_redraw(); + return; + } surface.configure( &device, &wgpu::SurfaceConfiguration { usage: wgpu::TextureUsages::RENDER_ATTACHMENT, format: format, - width: size.width, - height: size.height, + width: last_processed_size.width, + height: last_processed_size.height, present_mode: wgpu::PresentMode::Mailbox, }, ); @@ -190,6 +199,7 @@ pub fn main() { program.background_color(), ); + // Draw the scene scene.draw(&mut render_pass); } @@ -213,11 +223,11 @@ pub fn main() { frame.present(); // Update the mouse cursor - window.set_cursor_icon( - iced_winit::conversion::mouse_interaction( - state.mouse_interaction(), - ), - ); + window.set_cursor_icon( + iced_winit::conversion::mouse_interaction( + state.mouse_interaction(), + ), + ); // And recall staging buffers local_pool @@ -237,8 +247,22 @@ pub fn main() { } }, } + window.set_min_inner_size(Some(last_processed_size).filter(|_| false)); // using the filter to infer the type of None } _ => {} } }) } + +fn process_resize( + viewport: &mut Viewport, + last_processed_size: &mut PhysicalSize, + window: &Window, +) { + let new_size = window.inner_size(); + *last_processed_size = window.inner_size(); + *viewport = Viewport::with_physical_size( + Size::new(new_size.width, new_size.height), + window.scale_factor(), + ); +}