From edbe112d2386b16cebaed12fdfc42deffaa6f89d Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 13 Nov 2020 19:15:17 -0500 Subject: [PATCH] [rs] Drop for surfaces and adapters --- wgpu/Cargo.toml | 4 ++-- wgpu/src/backend/direct.rs | 22 ++++++++++++++++++++-- wgpu/src/backend/web.rs | 8 ++++++++ wgpu/src/lib.rs | 20 ++++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 135fa2e0fd..ce8eceb9e6 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -26,14 +26,14 @@ vulkan-portability = ["wgc/gfx-backend-vulkan", "gfx-backend-vulkan"] package = "wgpu-core" #version = "0.6" git = "https://github.com/gfx-rs/wgpu" -rev = "789b09c7bde774f09b0cec87d6ea1b5700a6de2f" +rev = "af9713b249cc158276aeb643900f095e7699167a" features = ["raw-window-handle"] [dependencies.wgt] package = "wgpu-types" #version = "0.6" git = "https://github.com/gfx-rs/wgpu" -rev = "789b09c7bde774f09b0cec87d6ea1b5700a6de2f" +rev = "af9713b249cc158276aeb643900f095e7699167a" [dependencies] arrayvec = "0.5" diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index 0ac1ffa24e..b71cfa37fa 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -33,7 +33,7 @@ impl Context { #[cfg(any(target_os = "ios", target_os = "macos"))] pub unsafe fn create_surface_from_core_animation_layer( - &self, + self: &Arc, layer: *mut std::ffi::c_void, ) -> crate::Surface { let surface = wgc::instance::Surface { @@ -52,7 +52,16 @@ impl Context { self.0 .surfaces .register(id, surface, &mut wgc::hub::Token::root()); - crate::Surface { id } + crate::Surface { + context: Arc::clone(self), + id, + } + } +} + +impl Drop for Context { + fn drop(&mut self) { + //nothing } } @@ -1207,6 +1216,15 @@ impl crate::Context for Context { ) } + fn surface_drop(&self, surface: &Self::SurfaceId) { + self.0.surface_drop(*surface) + } + + fn adapter_drop(&self, adapter: &Self::AdapterId) { + let global = &self.0; + wgc::gfx_select!(*adapter => global.adapter_drop(*adapter)) + } + fn buffer_destroy(&self, buffer: &Self::BufferId) { let global = &self.0; wgc::gfx_select!(buffer.id => global.buffer_destroy(buffer.id)).unwrap_pretty() diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index 3da390909d..ed6ca5dc1d 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -1261,6 +1261,14 @@ impl crate::Context for Context { Sendable(texture.0.create_view_with_descriptor(&mapped)) } + fn surface_drop(&self, _surface: &Self::SurfaceId) { + // Dropped automatically + } + + fn adapter_drop(&self, _adapter: &Self::AdapterId) { + // Dropped automatically + } + fn buffer_destroy(&self, _buffer: &Self::BufferId) { // TODO } diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 578e3a2c16..ca7490355f 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -302,6 +302,8 @@ trait Context: Debug + Send + Sized + Sync { desc: &TextureViewDescriptor, ) -> Self::TextureViewId; + fn surface_drop(&self, surface: &Self::SurfaceId); + fn adapter_drop(&self, adapter: &Self::AdapterId); fn buffer_destroy(&self, buffer: &Self::BufferId); fn buffer_drop(&self, buffer: &Self::BufferId); fn texture_destroy(&self, buffer: &Self::TextureId); @@ -434,6 +436,14 @@ pub struct Adapter { id: ::AdapterId, } +impl Drop for Adapter { + fn drop(&mut self) { + if !thread::panicking() { + self.context.adapter_drop(&self.id) + } + } +} + /// Open connection to a graphics and/or compute device. /// /// Responsible for the creation of most rendering and compute resources. @@ -586,9 +596,18 @@ impl Drop for Sampler { /// be presented. A `Surface` may be created with the unsafe function [`Instance::create_surface`]. #[derive(Debug)] pub struct Surface { + context: Arc, id: ::SurfaceId, } +impl Drop for Surface { + fn drop(&mut self) { + if !thread::panicking() { + self.context.surface_drop(&self.id) + } + } +} + /// Handle to a swap chain. /// /// A `SwapChain` represents the image or series of images that will be presented to a [`Surface`]. @@ -1335,6 +1354,7 @@ impl Instance { window: &W, ) -> Surface { Surface { + context: Arc::clone(&self.context), id: Context::instance_create_surface(&*self.context, window), } }