Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Merge PR #6897: GPU fallback: Retry GPU after an error rebuilds the GPU canvas now, no restart (#6577)

aiongg48f391e912b7
diff --cc crates/ui-egui/src/lib.rs
index 6f54ed161,6990ccfd6..e9ec0d1e9
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@@ -886,14 -813,11 +886,19 @@@ pub struct PhotocraftApp
/// GPU canvas renderer, when running on the wgpu backend (see [`Self::set_wgpu`]). Dropped
/// for the rest of the session when the device is lost (see `gpu_status`).
gpu: Option<gpu_canvas::GpuCanvas>,
+ /// The device the window itself draws with, and its health flag: the GPU canvas's, or one
+ /// watched for the CPU canvas (see `gpu_status::check_window`).
+ window_device: Option<(eframe::wgpu::Device, photocraft_gpu::DeviceHealth)>,
+ /// That device was lost: frames draw nothing and the app closes once its work is saved (#3438).
+ window_lost: bool,
+ /// When (in `egui::InputState::time`) to try saving unsaved work for recovery again after a
+ /// failed attempt; the app closes only once it succeeded.
+ window_recovery_retry: Option<f64>,
+ /// The render state the GPU canvas was built on, kept after it is dropped so Retry GPU can
+ /// build a new one on the same device (see `gpu_status::choose_recovery`, #6577).
+ gpu_render_state: Option<eframe::egui_wgpu::RenderState>,
+ /// Why the GPU canvas was dropped (see `gpu_status::fall_back`).
+ gpu_fault: Option<photocraft_gpu::Fault>,
/// Run once the first frames have rendered (see [`Self::on_started`]).
started: Option<gpu_status::StartedHook>,
/// Frame and canvas-upload timings (exposed via `ui.inspect`).
@@@ -1064,9 -981,8 +1069,11 @@@ impl PhotocraftApp
doc_hist: None,
hist_job: None,
gpu: None,
+ window_device: None,
+ window_lost: false,
+ window_recovery_retry: None,
+ gpu_render_state: None,
+ gpu_fault: None,
started: None,
perf: Default::default(),
stroke_trace: stroke_trace::StrokeTrace::from_env(),
@@@ -1106,9 -1020,7 +1113,10 @@@
let tile = self.session.prefs().performance.cache_tile_size;
// Escaped driver/setup panics must leave the session and CPU canvas alive.
self.perf.gpu_info.set_adapter(&rs.adapter.get_info());
+ // The browser has no adapter list (egui-wgpu only has it natively).
+ #[cfg(not(target_arch = "wasm32"))]
+ self.perf.gpu_info.set_available(&rs.available_adapters);
+ self.gpu_render_state = Some(rs.clone());
let gpu = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| gpu_canvas::GpuCanvas::with_tile(&rs, Some(tile)))) {
Ok(gpu) => gpu,
Err(payload) => {
diff --cc docs/control-protocol.md
index 1f174b927,937330bd4..efd18118f
--- a/docs/control-protocol.md
+++ b/docs/control-protocol.md
@@@ -83,7 -82,7 +83,7 @@@ The transport is `apps/photocraft/src/c
- Engine commands (`engine.execute`, `photocraft-cli run`, MCP `command_run`) reject a param key their `params` doc does not name for the commands listed in `STRICT` (`crates/engine/src/param_check.rs`, today `layer.hideLayers` and `layer.showLayers`): `{"ids":[4]}` is the error `unknown parameter \`ids\` (expected: layer)`, and nothing runs. Other commands still ignore unknown keys until their `params` doc is complete and they are added to that list (`coalesce` and `target` are always accepted).
- `ui.type {text}`: type text (goes to the focused widget, or to the canvas while the Type tool is editing)
- `ui.resize {width, height}`: resize the main window
- - `ui.gpu.simulateLoss {error?}`: act as if the wgpu device was lost (or reported an error with `error: true`). The app switches to the CPU image compositor for the rest of the session and shows the same recovery warning as on a real loss. `gpuFallbackNotice` in `ui.inspect` contains the reason while the warning is open. Keep Using CPU saves CPU compatibility for the next launch; Retry GPU saves GPU mode and asks the user to save and restart. The window renderer still requires a working graphics or software adapter. Returns `wasActive` and `gpuInfo`. For testing the fallback. With `destroy: true` it destroys the window's real wgpu device instead, as a driver reset does (see `docs/development.md` › device loss): the app saves every unsaved document for recovery and closes. Returns `destroyed`
-- `ui.gpu.simulateLoss {error?}`: act as if the wgpu device was lost (or reported an error with `error: true`). The app switches to the CPU image compositor (after a loss for the rest of the session; after an error until a successful Retry GPU) and shows the same recovery warning as on a real loss. `gpuFallbackNotice` in `ui.inspect` contains the reason while the warning is open. Keep Using CPU saves CPU compatibility for the next launch. Retry GPU saves GPU mode; after an error (`error: true`) it also builds a new GPU canvas on the same device at once (`gpuInfo.canvas` is `gpu` again, #6577), and after a loss it asks the user to save and restart. The window renderer still requires a working graphics or software adapter. Returns `wasActive` and `gpuInfo`. For testing the fallback
++- `ui.gpu.simulateLoss {error?}`: act as if the wgpu device was lost (or reported an error with `error: true`). The app switches to the CPU image compositor (after a loss for the rest of the session; after an error until a successful Retry GPU) and shows the same recovery warning as on a real loss. `gpuFallbackNotice` in `ui.inspect` contains the reason while the warning is open. Keep Using CPU saves CPU compatibility for the next launch. Retry GPU saves GPU mode; after an error (`error: true`) it also builds a new GPU canvas on the same device at once (`gpuInfo.canvas` is `gpu` again, #6577), and after a loss it asks the user to save and restart. The window renderer still requires a working graphics or software adapter. Returns `wasActive` and `gpuInfo`. For testing the fallback. With `destroy: true` it destroys the window's real wgpu device instead, as a driver reset does (see `docs/development.md` › device loss): the app saves every unsaved document for recovery and closes. Returns `destroyed`
- `ui.screenshot {path?, focus?}`: capture the main window (PNG). With no path the reply contains
base64 PNG data; a path is relative to the automation write root. Raises the window first
(default) because occluded macOS windows stop rendering