mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-24 16:31:11 +01:00
Vulkan: Use Drop impl for swapchain and surface (#9449)
This commit is contained in:
1 parent
89be5d5301
commit
2a9c23f607
4 files changed
+20
-45
No files matched your search
@@ -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() };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Box<dyn swapchain::Surface>>,
|
||||
swapchain: RwLock<Option<Box<dyn swapchain::Swapchain>>>,
|
||||
inner: Box<dyn swapchain::Surface>,
|
||||
}
|
||||
|
||||
impl Surface {
|
||||
|
||||
@@ -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<Self>);
|
||||
|
||||
/// 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<Self>);
|
||||
|
||||
/// Acquires the next available surface texture for rendering.
|
||||
///
|
||||
/// `timeout` specifies the maximum time to wait for an image to become available.
|
||||
|
||||
@@ -34,13 +34,15 @@ impl NativeSurface {
|
||||
}
|
||||
}
|
||||
|
||||
impl Surface for NativeSurface {
|
||||
unsafe fn delete_surface(self: Box<Self>) {
|
||||
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::<NativeSwapchain>().unwrap().raw,
|
||||
None => vk::SwapchainKHR::null(),
|
||||
};
|
||||
let old_swapchain = provided_old_swapchain
|
||||
.as_ref()
|
||||
.map(|osc| osc.as_any().downcast_ref::<NativeSwapchain>().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<vk::PresentTimeGOOGLE>,
|
||||
}
|
||||
|
||||
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<Self>) {
|
||||
unsafe { self.functor.destroy_swapchain(self.raw, None) };
|
||||
}
|
||||
|
||||
unsafe fn acquire(
|
||||
&mut self,
|
||||
timeout: Option<core::time::Duration>,
|
||||
|
||||
Reference in new issue
Block a user