From 16aec0b09ae1b1674f4ab04be494dca9a28591cb Mon Sep 17 00:00:00 2001 From: k9 Date: Thu, 14 May 2026 21:24:25 -0400 Subject: [PATCH] Trigger a surface resize to deal with window resize race condition (#9525) --- examples/features/src/hello_triangle/mod.rs | 35 ++++++++++++++------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/examples/features/src/hello_triangle/mod.rs b/examples/features/src/hello_triangle/mod.rs index 6d6c5bb85..53c207303 100644 --- a/examples/features/src/hello_triangle/mod.rs +++ b/examples/features/src/hello_triangle/mod.rs @@ -56,6 +56,24 @@ impl App { state: AppState::Uninitialized, } } + + fn resize_surface(&mut self, new_size: winit::dpi::PhysicalSize) { + let AppState::Running(wgpu_state) = &mut self.state else { + return; + }; + + // Reconfigure the surface with the new size + wgpu_state.config.width = new_size.width.max(1); + wgpu_state.config.height = new_size.height.max(1); + wgpu_state + .surface + .configure(&wgpu_state.device, &wgpu_state.config); + + // On macos the window needs to be redrawn manually after resizing + if let Some(window) = &self.window { + window.request_redraw(); + } + } } impl ApplicationHandler for App { @@ -173,6 +191,7 @@ impl ApplicationHandler for App { let config = surface .get_default_config(&adapter, size.width, size.height) .unwrap(); + surface.configure(&device, &config); let _ = proxy.send_event(TriangleAction::Initialized(WgpuState { @@ -191,8 +210,11 @@ impl ApplicationHandler for App { match event { TriangleAction::Initialized(wgpu_state) => { self.state = AppState::Running(wgpu_state); + + // winit might have updated the window size while we were + // creating the surface asynchronously, so resize the surface. if let Some(window) = &self.window { - window.request_redraw(); + self.resize_surface(window.inner_size()); } } } @@ -210,16 +232,7 @@ impl ApplicationHandler for App { match event { WindowEvent::Resized(new_size) => { - // Reconfigure the surface with the new size - wgpu_state.config.width = new_size.width.max(1); - wgpu_state.config.height = new_size.height.max(1); - wgpu_state - .surface - .configure(&wgpu_state.device, &wgpu_state.config); - // On macos the window needs to be redrawn manually after resizing - if let Some(window) = &self.window { - window.request_redraw(); - } + self.resize_surface(new_size); } WindowEvent::RedrawRequested => { let frame = match wgpu_state.surface.get_current_texture() {