Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Lost GPU: a dropped frame no longer crashes the window's close (#7148) (#7400)

semmelrinde02981d6bcfd2
diff --git a/vendor/README.md b/vendor/README.md
index aaf33cc51b..384f2353b2 100644
--- a/vendor/README.md
+++ b/vendor/README.md
@@ -325,6 +325,12 @@ SolidCraft changes:
default handler panicked (#7007). An unsupported surface is a refused configure too. The
acquire runs under `capture_errors`, and the `Validation` status it then returns reconfigures
the surface instead of skipping every later frame.
+- `src/lib.rs`, `src/winit.rs`: the painter holds an acquired frame as `AcquiredFrame`, which
+ destroys a second handle to the frame's texture once the frame is presented or dropped. On a
+ lost device wgpu's present and discard keep the frame's image in the surface, and dropping
+ the surface (the window closing for the restart) then panicked in wgpu-hal ("Trying to
+ destroy a SwapchainAcquireSemaphore that is still in use by a SurfaceTexture", #7148); the
+ destroy takes the image back whatever the device's state.
- `src/lib.rs`: creation of the egui renderer uses validation, internal and out-of-memory
error scopes, awaiting every result before returning it. A refused UI pipeline (D3D12
`egui_pipeline` shader compilation, #6648) returns `WgpuError::CreateRendererError` through
diff --git a/vendor/egui-wgpu/src/lib.rs b/vendor/egui-wgpu/src/lib.rs
index ca4d9c1e0d..1e65ac7a0b 100644
--- a/vendor/egui-wgpu/src/lib.rs
+++ b/vendor/egui-wgpu/src/lib.rs
@@ -194,6 +194,67 @@ impl DeviceLost {
}
}
+/// SolidCraft: a frame acquired from a window's surface, which gives its swapchain image back
+/// however the frame ends: presented, dropped unpresented, or dropped while a panic unwinds
+/// (#7148).
+///
+/// wgpu presents or discards a frame only on a valid device. On a lost one both return early, and
+/// the surface keeps the frame's image, which holds one of the swapchain's acquire semaphores.
+/// Dropping or reconfiguring that surface later (the window closing as the app restarts after the
+/// loss) then panics in wgpu-hal: "Trying to destroy a SwapchainAcquireSemaphore that is still in
+/// use by a SurfaceTexture". So the frame keeps a second handle to its texture, and once the frame
+/// is gone that handle is destroyed ([`FreeSurfaceTexture`]), which takes the image (and its
+/// semaphores) back from the surface, lost device or not. After a present or discard that worked,
+/// the image is already back and the destroy does nothing.
+///
+/// The order is the struct's: Rust drops fields in declaration order, so [`Self::frame`] always
+/// goes before the texture handle is destroyed, and [`Self::present`] presents before it.
+pub struct AcquiredFrame<F, T: FreeSurfaceTexture> {
+ /// The frame; dropped (discarded) before the texture is freed.
+ pub frame: F,
+ texture: FreeOnDrop<T>,
+}
+
+impl<F, T: FreeSurfaceTexture> AcquiredFrame<F, T> {
+ /// `texture` is a second handle to `frame`'s texture.
+ pub fn new(frame: F, texture: T) -> Self {
+ Self {
+ frame,
+ texture: FreeOnDrop(texture),
+ }
+ }
+
+ /// Present the frame with `present`, then free the texture.
+ pub fn present(self, present: impl FnOnce(F)) {
+ let Self { frame, texture } = self;
+ present(frame);
+ drop(texture);
+ }
+}
+
+/// SolidCraft: what [`AcquiredFrame`] does with its texture handle once the frame is gone.
+pub trait FreeSurfaceTexture {
+ /// Give the surface's image back, whatever the device's state; nothing once it is back.
+ fn free(&self);
+}
+
+impl FreeSurfaceTexture for wgpu::Texture {
+ /// wgpu-core's destroy of a surface texture takes its image and drops it without checking the
+ /// device, and does nothing when a present or discard took it already.
+ fn free(&self) {
+ self.destroy();
+ }
+}
+
+/// Frees its texture when dropped, also while a panic unwinds.
+struct FreeOnDrop<T: FreeSurfaceTexture>(T);
+
+impl<T: FreeSurfaceTexture> Drop for FreeOnDrop<T> {
+ fn drop(&mut self) {
+ self.0.free();
+ }
+}
+
/// Access to the render state for egui.
#[derive(Clone)]
pub struct RenderState {
@@ -1032,4 +1093,103 @@ mod device_lost_tests {
assert!(flag.load(std::sync::atomic::Ordering::Acquire));
assert_eq!(painter.reason().as_deref(), Some("Unknown: Device lost"));
}
+
+ /// A window's surface as wgpu-core treats the image of an acquired frame: a present or a
+ /// discard gives it back only on a valid device, a destroy of the texture always (#7148).
+ #[derive(Default)]
+ struct Surface {
+ lost: std::cell::Cell<bool>,
+ image_held: std::cell::Cell<bool>,
+ calls: std::cell::RefCell<Vec<&'static str>>,
+ }
+
+ impl Surface {
+ fn call(&self, name: &'static str, gives_back: bool) {
+ self.calls.borrow_mut().push(name);
+ if gives_back {
+ self.image_held.set(false);
+ }
+ }
+ }
+
+ /// A frame of [`Surface`]: discarded when dropped unpresented.
+ struct Frame<'s> {
+ surface: &'s Surface,
+ presented: bool,
+ }
+
+ impl Drop for Frame<'_> {
+ fn drop(&mut self) {
+ if !self.presented {
+ self.surface.call("discard", !self.surface.lost.get());
+ }
+ }
+ }
+
+ struct Texture<'s>(&'s Surface);
+
+ impl super::FreeSurfaceTexture for Texture<'_> {
+ fn free(&self) {
+ self.0.call("free", true);
+ }
+ }
+
+ #[derive(Clone, Copy, Debug)]
+ enum End {
+ Present,
+ Drop,
+ Panic,
+ }
+
+ /// Acquire a frame from `surface` and end it as `end`.
+ fn run_frame(surface: &Surface, end: End) {
+ surface.image_held.set(true);
+ let frame = super::AcquiredFrame::new(
+ Frame {
+ surface,
+ presented: false,
+ },
+ Texture(surface),
+ );
+ match end {
+ End::Present => frame.present(|mut frame| {
+ frame.presented = true;
+ surface.call("present", !surface.lost.get());
+ }),
+ End::Drop => drop(frame),
+ End::Panic => {
+ let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
+ let _frame = frame;
+ panic!("a paint callback panics while the frame is held");
+ }));
+ assert!(unwound.is_err());
+ }
+ }
+ }
+
+ /// However a frame ends, on a valid or a lost device, the surface has its image back by the
+ /// time the frame is gone, so dropping or reconfiguring the surface can't meet a semaphore
+ /// still in use (#7148). The texture is freed only after the present or discard: freed first,
+ /// a valid device's present or discard would find it destroyed.
+ #[test]
+ fn a_frame_gives_its_image_back_however_it_ends_even_on_a_lost_device() {
+ for lost in [false, true] {
+ for end in [End::Present, End::Drop, End::Panic] {
+ let surface = Surface::default();
+ surface.lost.set(lost);
+ run_frame(&surface, end);
+ let first = if matches!(end, End::Present) {
+ "present"
+ } else {
+ "discard"
+ };
+ assert_eq!(
+ *surface.calls.borrow(),
+ [first, "free"],
+ "lost {lost}, {end:?}"
+ );
+ assert!(!surface.image_held.get(), "lost {lost}, {end:?}");
+ }
+ }
+ }
}
diff --git a/vendor/egui-wgpu/src/winit.rs b/vendor/egui-wgpu/src/winit.rs
index 9f728d8359..aa9f948c09 100644
--- a/vendor/egui-wgpu/src/winit.rs
+++ b/vendor/egui-wgpu/src/winit.rs
@@ -735,7 +735,7 @@ impl Painter {
output_frame
};
- let output_frame = match output_frame {
+ let frame = match output_frame {
wgpu::CurrentSurfaceTexture::Success(frame) => frame,
wgpu::CurrentSurfaceTexture::Suboptimal(frame) => {
surface_state.needs_reconfigure = true;
@@ -770,6 +770,11 @@ impl Painter {
return vsync_sec;
}
};
+ // SolidCraft: however this frame ends (presented, dropped by an early return below, or
+ // dropped while a panic unwinds), the surface gets its image back, also on a lost device,
+ // where wgpu's present and discard keep it and the surface's drop then panicked (#7148).
+ let texture = frame.texture.clone();
+ let output_frame = crate::AcquiredFrame::new(frame, texture);
// SolidCraft: before the uploads, an empty submission asks the driver whether the device
// is still there. The first queue write to meet a lost device panics inside wgpu (#4172);
@@ -831,13 +836,13 @@ impl Painter {
let target_texture = if capture {
let capture_state = self.screen_capture_state.get_or_insert_with(|| {
- CaptureState::new(&render_state.device, &output_frame.texture)
+ CaptureState::new(&render_state.device, &output_frame.frame.texture)
});
- capture_state.update(&render_state.device, &output_frame.texture);
+ capture_state.update(&render_state.device, &output_frame.frame.texture);
&capture_state.texture
} else {
- &output_frame.texture
+ &output_frame.frame.texture
};
let target_view = target_texture.create_view(&wgpu::TextureViewDescriptor::default());
@@ -908,7 +913,7 @@ impl Painter {
if capture && let Some(capture_state) = &mut self.screen_capture_state {
capture_buffer = Some(capture_state.copy_textures(
&render_state.device,
- &output_frame,
+ &output_frame.frame,
&mut encoder,
));
}
@@ -962,7 +967,7 @@ impl Painter {
profiling::scope!("present");
// wgpu doesn't document where vsync can happen. Maybe here?
let start = web_time::Instant::now();
- render_state.queue.present(output_frame);
+ output_frame.present(|frame| render_state.queue.present(frame));
vsync_sec += start.elapsed().as_secs_f32();
}