From 831d908663f232e992baec30aaa5ecd45d148da3 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sun, 3 May 2020 22:13:43 -0400 Subject: [PATCH] Properly destroy swap chains --- wgpu-core/src/hub.rs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/wgpu-core/src/hub.rs b/wgpu-core/src/hub.rs index d61e042e90..237ba8e612 100644 --- a/wgpu-core/src/hub.rs +++ b/wgpu-core/src/hub.rs @@ -416,10 +416,10 @@ impl Hub { } } -impl Drop for Hub { - fn drop(&mut self) { +impl Hub { + fn clear(&mut self, surface_guard: &mut Storage) { use crate::resource::TextureViewInner; - use hal::device::Device as _; + use hal::{device::Device as _, window::PresentationSurface as _}; let mut devices = self.devices.data.write(); @@ -493,7 +493,15 @@ impl Drop for Hub { } } - //TODO: self.swap_chains + for (index, (swap_chain, epoch)) in self.swap_chains.data.write().map.drain() { + let device = &devices[swap_chain.device_id.value]; + let surface = &mut surface_guard[TypedId::zip(index as Index, epoch, B::VARIANT)]; + let suf = B::get_surface_mut(surface); + unsafe { + device.raw.destroy_semaphore(swap_chain.semaphore); + suf.unconfigure_swapchain(&device.raw); + } + } for (_, (device, _)) in devices.map.drain() { device.dispose(); @@ -555,8 +563,21 @@ impl Drop for Global { fn drop(&mut self) { if !thread::panicking() { log::info!("Dropping Global"); + let mut surface_guard = self.surfaces.data.write(); + // destroy hubs + #[cfg(any( + not(any(target_os = "ios", target_os = "macos")), + feature = "gfx-backend-vulkan" + ))] + self.hubs.vulkan.clear(&mut *surface_guard); + #[cfg(any(target_os = "ios", target_os = "macos"))] + self.hubs.metal.clear(&mut *surface_guard); + #[cfg(windows)] + self.hubs.dx12.clear(&mut *surface_guard); + #[cfg(windows)] + self.hubs.dx11.clear(&mut *surface_guard); // destroy surfaces - for (_, (surface, _)) in self.surfaces.data.write().map.drain() { + for (_, (surface, _)) in surface_guard.map.drain() { self.instance.destroy_surface(surface); } }