GPU: a failed allocation reported as an internal error, or a lost device, no longer crashes the app (#591)
On Adreno (Windows ARM64) a failed vkAllocateMemory surfaces as an internal
ERROR_INITIALIZATION_FAILED, not out-of-memory. Gpu::within_memory only scoped
OutOfMemory, and wgpu's DeviceExt::create_buffer_init maps the new buffer at
creation and panics ("Failed to get mapped range") when the allocation failed,
taking down a frame thread. Buffers with initial contents are now created unmapped
and filled through queue.write_buffer (context::buffer_init), so a failed
allocation is only an invalid buffer and an error for the caller's scope, and
within_memory scopes Internal errors too, discarding the frame for the CPU path
as it does for out-of-memory.
The device was then lost, and egui-wgpu panicked on the main thread staging the
next frame's index buffer on the dead device. Once the host reports the
presentation device lost, the app draws nothing more (no mesh to stage), auto-saves
unsaved changes, keeps the crash-recovery marker so the next launch offers them,
and closes its window.
Tests: a device with a small buffer limit gets an over-limit and an unaligned
buffer_init without a panic and sees the error in its scope; the UI harness
reports a device loss and checks the next frame has no shapes, closes the window
and wrote the auto-save.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QiAJeFttZp83Fh7YuzAR5f
This commit is contained in:
@@ -261,6 +261,9 @@ pub struct EffectcraftApp {
|
||||
gpu_checked: bool,
|
||||
gpu_failures: Option<gpu_failure::GpuFailureBridge>,
|
||||
gpu_failure_applied: Option<bool>,
|
||||
/// The graphics device was lost and the window is closing (see `close_after_device_loss`);
|
||||
/// `true` keeps crash recovery for the next launch.
|
||||
device_lost_close: Option<bool>,
|
||||
/// Commands from the native menu bar.
|
||||
pub command_inbox: Option<Receiver<String>>,
|
||||
/// Pointer position in comp pixels (Info panel).
|
||||
@@ -347,6 +350,7 @@ impl EffectcraftApp {
|
||||
gpu_checked: false,
|
||||
gpu_failures: None,
|
||||
gpu_failure_applied: None,
|
||||
device_lost_close: None,
|
||||
command_inbox: None,
|
||||
pointer_comp: None,
|
||||
dialog_state: Default::default(),
|
||||
@@ -643,6 +647,26 @@ impl EffectcraftApp {
|
||||
ctx.request_repaint();
|
||||
}
|
||||
|
||||
/// After the graphics device is lost, eframe's renderer cannot draw again: any mesh makes
|
||||
/// egui-wgpu panic on the main thread when it cannot stage its index buffer (#591). Draw
|
||||
/// nothing from then on, auto-save a project with unsaved changes (keeping the crash
|
||||
/// recovery marker, so the next launch offers it) and close the window.
|
||||
fn close_after_device_loss(&mut self, ctx: &egui::Context) {
|
||||
if self.device_lost_close.is_some() {
|
||||
return;
|
||||
}
|
||||
let keep_recovery = self.session.is_dirty();
|
||||
if keep_recovery {
|
||||
match self.session.autosave_now().and_then(|path| self.session.autosave_wait().map(|()| path)) {
|
||||
Ok(path) => log::warn!("graphics device lost: auto-saved {path}; the next launch offers to recover it"),
|
||||
Err(e) => log::error!("graphics device lost: {e}"),
|
||||
}
|
||||
}
|
||||
self.device_lost_close = Some(keep_recovery);
|
||||
log::warn!("graphics device lost: closing the window");
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
||||
}
|
||||
|
||||
/// Set up the GPU compositor on egui-wgpu's device (once). Without an adapter that runs
|
||||
/// compute shaders (WebGL2, software GL) everything stays on the CPU.
|
||||
pub(crate) fn init_gpu(&mut self, frame: &eframe::Frame) {
|
||||
@@ -1672,8 +1696,11 @@ impl EffectcraftApp {
|
||||
|
||||
impl eframe::App for EffectcraftApp {
|
||||
fn on_exit(&mut self) {
|
||||
// Clean exit: no crash recovery next launch.
|
||||
self.session.end_recovery();
|
||||
// Clean exit: no crash recovery next launch (unless the graphics device was lost with
|
||||
// unsaved changes, which are in the auto-save it offers).
|
||||
if self.device_lost_close != Some(true) {
|
||||
self.session.end_recovery();
|
||||
}
|
||||
}
|
||||
|
||||
fn raw_input_hook(&mut self, ctx: &egui::Context, raw_input: &mut egui::RawInput) {
|
||||
@@ -1732,6 +1759,10 @@ impl eframe::App for EffectcraftApp {
|
||||
}
|
||||
|
||||
fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
|
||||
if self.gpu_failure_applied == Some(true) {
|
||||
self.close_after_device_loss(ui.ctx());
|
||||
return;
|
||||
}
|
||||
self.init_gpu(frame);
|
||||
if !self.fonts_ready {
|
||||
ui.ctx().request_repaint();
|
||||
|
||||
Reference in New Issue
Block a user