Skip to content

Commit

Permalink
Properly drop surfaces
Browse files Browse the repository at this point in the history
Surfaces weren't being dropped until exit due to the code commented out
in Context::surface_drop. This hid a bug where surfaces dropped before
exit weren't being unconfigured.

Fixes gfx-rs#3646
  • Loading branch information
Benjamin Schaaf committed Apr 4, 2023
1 parent 77d6696 commit 44e06ae
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ By @cwfitzgerald in [#3610](https://github.com/gfx-rs/wgpu/pull/3610).
- Validate before extracting texture selectors. By @teoxoy in [#3487](https://github.com/gfx-rs/wgpu/pull/3487)
- Fix fatal errors (those which panic even if an error handler is set) not including all of the details. By @kpreid in [#3563](https://github.com/gfx-rs/wgpu/pull/3563)
- Validate shader location clashes. By @emilk in [#3613](https://github.com/gfx-rs/wgpu/pull/3613)
- Fix surfaces not being dropped until exit. By @benjaminschaaf in [#3647](https://github.com/gfx-rs/wgpu/pull/3647)

#### Vulkan

Expand Down
8 changes: 8 additions & 0 deletions wgpu-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,14 @@ impl<A: HalApi, F: GlobalIdentityHandlerFactory> Hub<A, F> {
}
}

pub(crate) fn surface_unconfigure(&self, device_id: id::Valid<id::DeviceId>, surface: &mut HalSurface<A>) {
use hal::{Surface as _};

let devices = self.devices.data.read();
let device = &devices[device_id];
unsafe { surface.raw.unconfigure(&device.raw); }
}

pub fn generate_report(&self) -> HubReport {
HubReport {
adapters: self.adapters.data.read().generate_report(),
Expand Down
28 changes: 27 additions & 1 deletion wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,33 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
profiling::scope!("Surface::drop");
let mut token = Token::root();
let (surface, _) = self.surfaces.unregister(id, &mut token);
self.instance.destroy_surface(surface.unwrap());
let mut surface = surface.unwrap();

fn unconfigure<G: GlobalIdentityHandlerFactory, A: HalApi>(
global: &Global<G>,
surface: &mut HalSurface<A>,
present: &Presentation) {
let hub = HalApi::hub(global);
hub.surface_unconfigure(present.device_id.value, surface);
}

if let Some(present) = surface.presentation.take() {
match present.backend() {
#[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))]
Backend::Vulkan => unconfigure(self, surface.vulkan.as_mut().unwrap(), &present),
#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))]
Backend::Metal => unconfigure(self, surface.metal.as_mut().unwrap(), &present),
#[cfg(all(feature = "dx12", windows))]
Backend::Dx12 => unconfigure(self, surface.dx12.as_mut().unwrap(), &present),
#[cfg(all(feature = "dx11", windows))]
Backend::Dx11 => unconfigure(self, surface.dx11.as_mut().unwrap(), &present),
#[cfg(feature = "gles")]
Backend::Gl => unconfigure(self, surface.gl.as_mut().unwrap(), &present),
_ => unreachable!(),
}
}

self.instance.destroy_surface(surface);
}

fn enumerate<A: HalApi>(
Expand Down
5 changes: 2 additions & 3 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1549,9 +1549,8 @@ impl crate::Context for Context {
(id, ())
}

fn surface_drop(&self, _surface: &Self::SurfaceId, _surface_data: &Self::SurfaceData) {
//TODO: swapchain needs to hold the surface alive
//self.0.surface_drop(*surface)
fn surface_drop(&self, surface: &Self::SurfaceId, _surface_data: &Self::SurfaceData) {
self.0.surface_drop(*surface)
}

fn adapter_drop(&self, adapter: &Self::AdapterId, _adapter_data: &Self::AdapterData) {
Expand Down

0 comments on commit 44e06ae

Please sign in to comment.