From f6adeb7b6153d39c68ffa95754008db2486e280a Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Sun, 8 Oct 2023 11:49:50 +0100 Subject: [PATCH] Implement a workaround for "Map callback was leaked" See https://github.com/gfx-rs/wgpu/issues/4214 and https://xi.zulipchat.com/#narrow/stream/197075-gpu/topic/Map.20Callbacks.20Leak --- src/util.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/util.rs b/src/util.rs index 49bf2cc7d..85235e99c 100644 --- a/src/util.rs +++ b/src/util.rs @@ -82,29 +82,35 @@ impl RenderContext { alpha_mode: wgpu::CompositeAlphaMode::Auto, view_formats: vec![], }; - surface.configure(&self.devices[dev_id].device, &config); - Ok(RenderSurface { + let surface = RenderSurface { surface, config, dev_id, format, - }) + }; + self.configure_surface(&surface); + Ok(surface) } /// Resizes the surface to the new dimensions. pub fn resize_surface(&self, surface: &mut RenderSurface, width: u32, height: u32) { surface.config.width = width; surface.config.height = height; - surface - .surface - .configure(&self.devices[surface.dev_id].device, &surface.config); + self.configure_surface(surface); } pub fn set_present_mode(&self, surface: &mut RenderSurface, present_mode: wgpu::PresentMode) { surface.config.present_mode = present_mode; - surface - .surface - .configure(&self.devices[surface.dev_id].device, &surface.config); + self.configure_surface(surface); + } + + fn configure_surface(&self, surface: &RenderSurface) { + let device = &self.devices[surface.dev_id].device; + // Temporary workaround for https://github.com/gfx-rs/wgpu/issues/4214 + // It's still possible for this to panic if the device is being used on another thread + // but this unbreaks most current users + device.poll(wgpu::MaintainBase::Wait); + surface.surface.configure(device, &surface.config); } /// Finds or creates a compatible device handle id.