From f069c58c0c06d7ba6f72ec0f4e482cc217bc36c4 Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Tue, 17 Dec 2024 21:31:49 +0200 Subject: [PATCH] examples/runners/wgpu: avoid holding onto to multiple surfaces at the same time. --- examples/runners/wgpu/src/graphics.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/examples/runners/wgpu/src/graphics.rs b/examples/runners/wgpu/src/graphics.rs index 253c55d2eb..15a58e23c2 100644 --- a/examples/runners/wgpu/src/graphics.rs +++ b/examples/runners/wgpu/src/graphics.rs @@ -164,6 +164,25 @@ async fn run( event_loop_window_target.set_control_flow(ControlFlow::Wait); match event { Event::Resumed => { + // Avoid holding onto to multiple surfaces at the same time + // (as it's undetected and can confusingly break e.g. Wayland). + // + // FIXME(eddyb) create the window and `wgpu::Surface` on either + // `Event::NewEvents(StartCause::Init)`, or `Event::Resumed`, + // which is becoming recommended on (almost) all platforms, see: + // - https://github.com/rust-windowing/winit/releases/tag/v0.30.0 + // - https://github.com/gfx-rs/wgpu/blob/v23/examples/src/framework.rs#L139-L161 + // (note wasm being handled differently due to its ``) + if let Ok((_, surface_config)) = &surface_with_config { + // HACK(eddyb) can't move out of `surface_with_config` as + // it's a closure capture, and also the `Err(_)` variant + // has a payload so that needs to be filled with something. + let filler = Err(SurfaceCreationPending { + preferred_format: surface_config.format, + }); + drop(std::mem::replace(&mut surface_with_config, filler)); + } + let new_surface = instance.create_surface(&window) .expect("Failed to create surface from window (after resume)"); surface_with_config = Ok(auto_configure_surface(