diff --git a/wgpu-hal/src/vulkan/instance.rs b/wgpu-hal/src/vulkan/instance.rs index a80869eb2..20cf0537d 100644 --- a/wgpu-hal/src/vulkan/instance.rs +++ b/wgpu-hal/src/vulkan/instance.rs @@ -2,7 +2,6 @@ use alloc::{borrow::ToOwned as _, boxed::Box, ffi::CString, string::String, sync use core::{ ffi::{c_void, CStr}, marker::PhantomData, - mem::ManuallyDrop, slice, str::FromStr, }; @@ -546,8 +545,8 @@ impl super::Instance { crate::vulkan::swapchain::NativeSurface::from_vk_surface_khr(self, surface); super::Surface { - inner: ManuallyDrop::new(Box::new(native_surface)), swapchain: RwLock::new(None), + inner: Box::new(native_surface), } } @@ -984,12 +983,6 @@ impl crate::Instance for super::Instance { } } -impl Drop for super::Surface { - fn drop(&mut self) { - unsafe { ManuallyDrop::take(&mut self.inner).delete_surface() }; - } -} - impl crate::Surface for super::Surface { type A = super::Api; @@ -1016,7 +1009,6 @@ impl crate::Surface for super::Surface { if let Some(mut sc) = self.swapchain.write().take() { // SAFETY: `unconfigure`'s contract guarantees there are no resources derived from the swapchain in use. unsafe { sc.release_resources(device) }; - unsafe { sc.delete_swapchain() }; } } diff --git a/wgpu-hal/src/vulkan/mod.rs b/wgpu-hal/src/vulkan/mod.rs index e147cd190..92f38d903 100644 --- a/wgpu-hal/src/vulkan/mod.rs +++ b/wgpu-hal/src/vulkan/mod.rs @@ -38,14 +38,7 @@ mod swapchain; pub use adapter::PhysicalDeviceFeatures; use alloc::{boxed::Box, ffi::CString, sync::Arc, vec::Vec}; -use core::{ - borrow::Borrow, - ffi::CStr, - fmt, - marker::PhantomData, - mem::{self, ManuallyDrop}, - num::NonZeroU32, -}; +use core::{borrow::Borrow, ffi::CStr, fmt, marker::PhantomData, mem, num::NonZeroU32}; use arrayvec::ArrayVec; use ash::{ext, khr, vk}; @@ -192,8 +185,8 @@ pub struct Instance { } pub struct Surface { - inner: ManuallyDrop>, swapchain: RwLock>>, + inner: Box, } impl Surface { diff --git a/wgpu-hal/src/vulkan/swapchain/mod.rs b/wgpu-hal/src/vulkan/swapchain/mod.rs index 33f1a8259..2635ab7ab 100644 --- a/wgpu-hal/src/vulkan/swapchain/mod.rs +++ b/wgpu-hal/src/vulkan/swapchain/mod.rs @@ -8,11 +8,6 @@ pub(super) use native::*; mod native; pub(super) trait Surface: Send + Sync + 'static { - /// Deletes the surface and associated resources. - /// - /// The surface must not be in use when it is deleted. - unsafe fn delete_surface(self: Box); - /// Returns the surface capabilities for the given adapter. /// /// Returns `None` if the surface is not compatible with the adapter. @@ -37,17 +32,11 @@ pub(super) trait Surface: Send + Sync + 'static { pub(super) trait Swapchain: Send + Sync + 'static { /// Releases all resources associated with the swapchain, without /// destroying the swapchain itself. Must be called before calling - /// either [`Surface::create_swapchain`] or [`Swapchain::delete_swapchain`]. + /// either [`Surface::create_swapchain`] or dropping the swapchain. /// /// The swapchain must not be in use when this is called. unsafe fn release_resources(&mut self, device: &super::Device); - /// Deletes the swapchain. - /// - /// The swapchain must not be in use when it is deleted and - /// [`Swapchain::release_resources`] must have been called first. - unsafe fn delete_swapchain(self: Box); - /// Acquires the next available surface texture for rendering. /// /// `timeout` specifies the maximum time to wait for an image to become available. diff --git a/wgpu-hal/src/vulkan/swapchain/native.rs b/wgpu-hal/src/vulkan/swapchain/native.rs index ce02c798a..823e4e852 100644 --- a/wgpu-hal/src/vulkan/swapchain/native.rs +++ b/wgpu-hal/src/vulkan/swapchain/native.rs @@ -34,13 +34,15 @@ impl NativeSurface { } } -impl Surface for NativeSurface { - unsafe fn delete_surface(self: Box) { +impl Drop for NativeSurface { + fn drop(&mut self) { unsafe { self.functor.destroy_surface(self.raw, None); } } +} +impl Surface for NativeSurface { fn surface_capabilities( &self, adapter: &crate::vulkan::Adapter, @@ -160,10 +162,10 @@ impl Surface for NativeSurface { profiling::scope!("Device::create_swapchain"); let functor = khr::swapchain::Device::new(&self.instance.raw, &device.shared.raw); - let old_swapchain = match provided_old_swapchain { - Some(osc) => osc.as_any().downcast_ref::().unwrap().raw, - None => vk::SwapchainKHR::null(), - }; + let old_swapchain = provided_old_swapchain + .as_ref() + .map(|osc| osc.as_any().downcast_ref::().unwrap().raw) + .unwrap_or(vk::SwapchainKHR::null()); let color_space = if config.format == wgt::TextureFormat::Rgba16Float { // Enable wide color gamut mode @@ -216,11 +218,6 @@ impl Surface for NativeSurface { unsafe { functor.create_swapchain(&info, None) } }; - // doing this before bailing out with error - if old_swapchain != vk::SwapchainKHR::null() { - unsafe { functor.destroy_swapchain(old_swapchain, None) } - } - let raw = match result { Ok(swapchain) => swapchain, Err(error) => { @@ -336,6 +333,14 @@ pub(crate) struct NativeSwapchain { next_present_time: Option, } +impl Drop for NativeSwapchain { + fn drop(&mut self) { + unsafe { + self.functor.destroy_swapchain(self.raw, None); + } + } +} + impl Swapchain for NativeSwapchain { unsafe fn release_resources(&mut self, device: &crate::vulkan::Device) { profiling::scope!("Swapchain::release_resources"); @@ -374,10 +379,6 @@ impl Swapchain for NativeSwapchain { } } - unsafe fn delete_swapchain(self: Box) { - unsafe { self.functor.destroy_swapchain(self.raw, None) }; - } - unsafe fn acquire( &mut self, timeout: Option,