mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-01 02:35:37 +01:00
Merge pull request #10273 from dotCooCoo/fix/device-lost-swapchain-panic
Graphics: closing the window after a lost device no longer panics
This commit is contained in:
2 files changed
+88
-13
No files matched your search
+52
-10
@@ -1,4 +1,4 @@
|
||||
#[cfg(all(target_os = "macos", feature = "accessibility", feature = "wgpu"))]
|
||||
#[cfg(feature = "wgpu")]
|
||||
use super::gpu;
|
||||
use super::nightly_session;
|
||||
use super::prefs::{flush, save_tick};
|
||||
@@ -107,7 +107,7 @@ impl eframe::App for App {
|
||||
// Plug-ins still loading end before the launch marker does (the macOS exit below skips
|
||||
// dropping the app).
|
||||
self.app.session.cancel_plugin_loads();
|
||||
end_before_teardown(self.frames);
|
||||
end_before_teardown(self.frames, self.graphics_loss.happened());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,16 +202,58 @@ impl App {
|
||||
/// its own class, which AppKit's Touch Bar support was observing it under, and AppKit aborts the
|
||||
/// app as it quits on Macs with a Touch Bar. On a normal quit, only the launch marker is needed
|
||||
/// after the window, so it is ended here as `main` ends it after the window on other platforms.
|
||||
/// On every platform, the process also ends here when the window's graphics device was lost
|
||||
/// (`device_lost`, #6374). A frame the lost device could not present stays acquired, and releasing
|
||||
/// the window's surface as eframe tears it down then panics in wgpu (gfx-rs/wgpu#9277).
|
||||
/// Not while the window is still starting up (`frames` below [`gpu::STARTUP_FRAMES`]): a graphics
|
||||
/// failure then starts the app again on another adapter (#651).
|
||||
#[allow(unused_variables)]
|
||||
fn end_before_teardown(frames: u64) {
|
||||
#[cfg(all(target_os = "macos", feature = "accessibility", feature = "wgpu"))]
|
||||
if frames >= gpu::STARTUP_FRAMES {
|
||||
safe_mode::end_launch();
|
||||
log::info!("quitting");
|
||||
log::logger().flush();
|
||||
std::process::exit(0);
|
||||
fn end_before_teardown(frames: u64, device_lost: bool) {
|
||||
if !ends_before_teardown(frames, device_lost) {
|
||||
return;
|
||||
}
|
||||
if device_lost {
|
||||
log::info!("the graphics device was lost: quitting before the window's graphics are torn down");
|
||||
}
|
||||
// `main` does these after the window, which this exit skips.
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
single_instance::resign();
|
||||
safe_mode::end_launch();
|
||||
log::info!("quitting");
|
||||
log::logger().flush();
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
/// Whether [`end_before_teardown`] ends the process: after the window's start-up, on macOS with
|
||||
/// accessibility, or once the graphics device was lost.
|
||||
#[cfg(feature = "wgpu")]
|
||||
fn ends_before_teardown(frames: u64, device_lost: bool) -> bool {
|
||||
frames >= gpu::STARTUP_FRAMES && (device_lost || cfg!(all(target_os = "macos", feature = "accessibility")))
|
||||
}
|
||||
|
||||
/// Without wgpu there is no device-lost report, and the window is torn down as usual.
|
||||
#[cfg(not(feature = "wgpu"))]
|
||||
fn ends_before_teardown(_frames: u64, _device_lost: bool) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "wgpu"))]
|
||||
mod tests {
|
||||
use vectorcraft_ui_egui::graphics::GraphicsLoss;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// A device lost while the app runs ends the process before eframe releases the window's
|
||||
/// surface, on every platform; before the window has started, the start-up restart takes over.
|
||||
#[test]
|
||||
fn a_lost_graphics_device_ends_the_process_before_teardown() {
|
||||
let always = cfg!(all(target_os = "macos", feature = "accessibility"));
|
||||
let (loss, ctx) = (GraphicsLoss::default(), egui::Context::default());
|
||||
assert_eq!(ends_before_teardown(gpu::STARTUP_FRAMES, loss.happened()), always, "no loss: the usual teardown");
|
||||
let (l, c) = (loss.clone(), ctx.clone());
|
||||
std::thread::spawn(move || l.report(&c, "Unknown: Device is lost")).join().unwrap();
|
||||
assert!(loss.take().is_some(), "a frame handles the report");
|
||||
assert!(ends_before_teardown(gpu::STARTUP_FRAMES, loss.happened()), "after a loss the window is not torn down");
|
||||
assert!(!ends_before_teardown(gpu::STARTUP_FRAMES - 1, loss.happened()), "during start-up the adapter restart runs");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@
|
||||
//! - The web host then starts again with new graphics and a new egui context, handing it the same
|
||||
//! app. The app notices the new context and makes everything it uploaded to the old one again:
|
||||
//! its fonts and theme, the canvas, and every texture cache ([`TexCache`]).
|
||||
//! - The desktop host can't give the window a new device. When the window closes after a loss
|
||||
//! ([`GraphicsLoss::happened`]), the process ends once the app has saved what it keeps on
|
||||
//! quitting, before the window's graphics are torn down: releasing a swapchain that still holds
|
||||
//! a frame the lost device couldn't present panics in the graphics driver layer (#6374).
|
||||
|
||||
use std::cell::{Cell, Ref, RefCell, RefMut};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
@@ -20,7 +24,16 @@ use crate::VectorcraftApp;
|
||||
/// Raised by the host when the window's graphics device is lost (from any thread), read on the UI
|
||||
/// thread. Cheap to clone; the clones share the report.
|
||||
#[derive(Clone, Default)]
|
||||
pub struct GraphicsLoss(Arc<Mutex<Option<String>>>);
|
||||
pub struct GraphicsLoss(Arc<Mutex<Loss>>);
|
||||
|
||||
/// What a [`GraphicsLoss`] holds.
|
||||
#[derive(Default)]
|
||||
struct Loss {
|
||||
/// The loss not handled yet, for [`GraphicsLoss::take`].
|
||||
why: Option<String>,
|
||||
/// A loss was reported at some point, handled or not.
|
||||
happened: bool,
|
||||
}
|
||||
|
||||
impl GraphicsLoss {
|
||||
/// The graphics device was lost (`why`, for the log): the next frame of `ctx` handles it. A
|
||||
@@ -28,13 +41,23 @@ impl GraphicsLoss {
|
||||
pub fn report(&self, ctx: &egui::Context, why: impl Into<String>) {
|
||||
let why = why.into();
|
||||
log::error!("vectorcraft: the graphics device was lost: {why}");
|
||||
self.0.lock().unwrap_or_else(|e| e.into_inner()).get_or_insert(why);
|
||||
{
|
||||
let mut loss = self.0.lock().unwrap_or_else(|e| e.into_inner());
|
||||
loss.why.get_or_insert(why);
|
||||
loss.happened = true;
|
||||
}
|
||||
ctx.request_repaint();
|
||||
}
|
||||
|
||||
/// The loss reported since the last call, if any.
|
||||
pub fn take(&self) -> Option<String> {
|
||||
self.0.lock().unwrap_or_else(|e| e.into_inner()).take()
|
||||
self.0.lock().unwrap_or_else(|e| e.into_inner()).why.take()
|
||||
}
|
||||
|
||||
/// Whether the device was lost at any point, a loss [`Self::take`] already handled included.
|
||||
/// A window can't be given a new device, so once true this stays true.
|
||||
pub fn happened(&self) -> bool {
|
||||
self.0.lock().unwrap_or_else(|e| e.into_inner()).happened
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,6 +213,16 @@ mod tests {
|
||||
assert_eq!(loss.take(), None, "handled once");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_handled_loss_is_still_known_when_the_window_closes() {
|
||||
let (loss, ctx) = (GraphicsLoss::default(), crate::testing::ui_context());
|
||||
assert!(!loss.happened(), "nothing lost yet");
|
||||
let (l, c) = (loss.clone(), ctx.clone());
|
||||
std::thread::spawn(move || l.report(&c, "Unknown: Device is lost")).join().unwrap();
|
||||
assert!(loss.take().is_some(), "the next frame handles it");
|
||||
assert!(loss.happened(), "the device stays lost after the frame handled the report");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tex_cache_empties_when_the_ui_moves_to_a_new_context() {
|
||||
thread_local! {
|
||||
|
||||
Reference in new issue
Block a user