From 862807ffc9a96eb6acedb0a1fb2220d15c294d02 Mon Sep 17 00:00:00 2001 From: semmelrinde <124386289+semmelrinde@users.noreply.github.com> Date: Sun, 1 Nov 2026 18:20:40 +0000 Subject: [PATCH] Lost GPU: the per-frame probe meets a D3D12 device reset before the first upload does (#6528) (#7767) * claim: #6528 Co-Authored-By: Claude Opus 5.5 * Lost GPU: the per-frame probe opens a command list, as the upload would (#6528) On a reset D3D12 device the empty submission before a frame's uploads went through (it only signals a fence), and the first queue write then panicked inside wgpu-core (PendingWrites::activate unwrapping Lost). The probe now submits a command buffer with a debug marker: finishing it makes the same begin_encoding call, through a path where wgpu loses the device and runs its lost callback instead of panicking. The picture capturer's probe does the same. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/gpu/src/capture.rs | 10 ++++++--- crates/ui-egui/src/gpu.rs | 39 +++++++++++++++++++++++++++++++++-- vendor/README.md | 7 ++++--- vendor/egui-wgpu/src/lib.rs | 22 ++++++++++++++------ vendor/egui-wgpu/src/winit.rs | 11 ++++++---- 5 files changed, 71 insertions(+), 18 deletions(-) diff --git a/crates/gpu/src/capture.rs b/crates/gpu/src/capture.rs index c82e2bab3..3f139d7d0 100644 --- a/crates/gpu/src/capture.rs +++ b/crates/gpu/src/capture.rs @@ -106,13 +106,17 @@ impl Capturer { self.lost.load(Ordering::Acquire) } - /// [`Capturer::is_lost`], asking the driver first: an empty submission meets a loss the - /// driver reports and marks it, where the picture's first upload would panic inside wgpu. + /// [`Capturer::is_lost`], asking the driver first: a submission that opens a command list + /// meets a loss the driver reports and marks it, where the picture's first upload would + /// panic inside wgpu. As `egui_wgpu::DeviceLost::probe`: an empty submission only signals a + /// fence, which a reset D3D12 device let through (#6528). fn probe_lost(&self) -> bool { if self.is_lost() { return true; } - self.queue.submit([]); + let mut encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("device lost probe") }); + encoder.insert_debug_marker("device lost probe"); + self.queue.submit([encoder.finish()]); self.is_lost() } diff --git a/crates/ui-egui/src/gpu.rs b/crates/ui-egui/src/gpu.rs index efc1dcbe4..bed4fe976 100644 --- a/crates/ui-egui/src/gpu.rs +++ b/crates/ui-egui/src/gpu.rs @@ -1189,7 +1189,7 @@ mod tests { let capturer = solidcraft_gpu::capture::Capturer::new(&adapter, device.clone(), queue.clone()).unwrap().watching(lost.flag()); let (scene, cam) = (solidcraft_engine::render::Scene::default(), solidcraft_engine::render::Camera::default()); assert_ne!(super::picture(&capturer, &scene, &cam, 16, 16).unwrap().renderer, "cpu"); - assert!(!lost.probe(&queue), "a working device isn't lost"); + assert!(!lost.probe(&device, &queue), "a working device isn't lost"); // egui's renderer is made with the window, before any loss (made on a lost device, its // uniform buffer's `create_buffer_init` panics). @@ -1197,7 +1197,7 @@ mod tests { device.destroy(); let _ = device.poll(wgpu::PollType::wait_indefinitely()); - assert!(lost.is_lost() && lost.probe(&queue), "the lost callback marked the device"); + assert!(lost.is_lost() && lost.probe(&device, &queue), "the lost callback marked the device"); assert!(lost.reason().is_some_and(|r| r.starts_with("Destroyed")), "{:?}", lost.reason()); // The next frame's uploads, as the painter would make them were it not skipping: wgpu @@ -1210,4 +1210,39 @@ mod tests { let picture = super::picture(&capturer, &scene, &cam, 16, 16).unwrap(); assert_eq!((picture.renderer, picture.rgba.len()), ("cpu", 16 * 16 * 4)); } + + /// The probe runs before every frame's uploads, so on a working device it must cost nothing + /// visible: no error for any scope or handler, and the uploads after it go through. On a + /// reset D3D12 device an empty submission went through and the upload after it panicked + /// inside wgpu (#6528); the probe now opens a command list, the driver call that upload's + /// queue write would make first. A driver reset can't be made here: `destroy` loses the + /// device in wgpu itself, before any driver call. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn the_lost_device_probe_is_clean_on_a_working_device() { + use egui_wgpu::wgpu; + let Some((adapter, device, queue)) = super::offscreen_device() else { + eprintln!("skipping: no GPU adapter"); + return; + }; + let lost = egui_wgpu::DeviceLost::watch(&device); + let capturer = solidcraft_gpu::capture::Capturer::new(&adapter, device.clone(), queue.clone()).unwrap().watching(lost.flag()); + let mut renderer = egui_wgpu::Renderer::new(&device, wgpu::TextureFormat::Rgba8Unorm, Default::default()); + let image = egui::ImageData::Color(std::sync::Arc::new(egui::ColorImage::filled([4, 4], egui::Color32::WHITE))); + let delta = egui::epaint::ImageDelta::full(image, egui::TextureOptions::LINEAR); + let (scene, cam) = (solidcraft_engine::render::Scene::default(), solidcraft_engine::render::Camera::default()); + for frame in 0..3 { + // As the painter: the probe, then the frame's first queue write. + let (was_lost, error) = egui_wgpu::capture_errors(&device, || { + let was_lost = lost.probe(&device, &queue); + renderer.update_texture(&device, &queue, egui::TextureId::Managed(0), &delta); + was_lost + }); + assert!(!was_lost && error.is_none(), "frame {frame}: lost {was_lost}, {error:?}"); + // A save's thumbnail, which probes too, still comes from the GPU. + assert_ne!(super::picture(&capturer, &scene, &cam, 16, 16).unwrap().renderer, "cpu"); + } + let _ = device.poll(wgpu::PollType::wait_indefinitely()); + assert!(!lost.is_lost() && lost.reason().is_none(), "{:?}", lost.reason()); + } } diff --git a/vendor/README.md b/vendor/README.md index c48dd8133..3d4c19969 100644 --- a/vendor/README.md +++ b/vendor/README.md @@ -341,9 +341,10 @@ SolidCraft changes: compositor wasn't showing stalled the main window behind its present. - `src/lib.rs`, `src/winit.rs`: the device's lost callback marks `RenderState::device_lost` (`DeviceLost`), and the painter draws nothing on a lost device. Before a frame's uploads it - submits nothing (`DeviceLost::probe`), so a device the driver lost is found by that - submission rather than by the first queue write, which panicked inside wgpu-core - (`PendingWrites::activate`, #4172). + submits a command buffer holding a debug marker (`DeviceLost::probe`), so a device the + driver lost is found by opening that command list rather than by the first queue write, + which panicked inside wgpu-core (`PendingWrites::activate`, #4172). An empty submission + only signalled a fence, which a reset D3D12 device let through (#6528). - `src/lib.rs`, `src/winit.rs`: a surface configure that meets a lost device is not taken as configured (`configure_captured`): wgpu reports device-loss errors to no error scope, and on D3D12 a driver reset found by the configure has already dropped the surface's configuration. diff --git a/vendor/egui-wgpu/src/lib.rs b/vendor/egui-wgpu/src/lib.rs index 439e5400f..773207ea4 100644 --- a/vendor/egui-wgpu/src/lib.rs +++ b/vendor/egui-wgpu/src/lib.rs @@ -140,8 +140,8 @@ impl SurfaceConfig { /// A lost device can't be used again. wgpu only learns of the loss when a call into the driver /// fails: from then on every call on the device fails quietly, and the device's lost callback /// marks this. But a queue write that is the first call to meet the loss panics inside wgpu -/// (`PendingWrites::activate`), so the painter first submits nothing ([`DeviceLost::probe`]): -/// that submission meets the loss instead, and the frame's uploads are skipped. +/// (`PendingWrites::activate`), so the painter first probes the device ([`DeviceLost::probe`]): +/// the probe meets the loss instead, and the frame's uploads are skipped. #[derive(Clone, Debug, Default)] pub struct DeviceLost { lost: Arc, @@ -183,13 +183,23 @@ impl DeviceLost { self.lost.clone() } - /// Whether the device is lost, asking the driver first: an empty submission reaches the - /// driver, so a loss it reports is marked here rather than met by the next upload. - pub fn probe(&self, queue: &wgpu::Queue) -> bool { + /// Whether the device is lost, asking the driver first, so a loss it reports is marked here + /// rather than met by the next upload. + /// + /// The probe opens a command list, as the upload would: a queue write's first call into the + /// driver is `begin_encoding`, and on D3D12 that is where a reset device says so. An empty + /// submission only signals a fence: #6528 met a reset D3D12 device in the upload right after + /// one. wgpu handles a failed `begin_encoding` in a command encoder's `finish` (it loses the + /// device, and the lost callback marks it); only its queue writes unwrap it. + pub fn probe(&self, device: &wgpu::Device, queue: &wgpu::Queue) -> bool { if self.is_lost() { return true; } - queue.submit([]); + let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { + label: Some("device lost probe"), + }); + encoder.insert_debug_marker("device lost probe"); + queue.submit([encoder.finish()]); self.is_lost() } } diff --git a/vendor/egui-wgpu/src/winit.rs b/vendor/egui-wgpu/src/winit.rs index 0784fefc1..bc671a80d 100644 --- a/vendor/egui-wgpu/src/winit.rs +++ b/vendor/egui-wgpu/src/winit.rs @@ -806,10 +806,13 @@ impl Painter { 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); - // the submission meets it instead, marks it lost, and the frame is dropped unpresented. - if render_state.device_lost.probe(&render_state.queue) { + // SolidCraft: before the uploads, a probe asks the driver whether the device is still + // there. The first queue write to meet a lost device panics inside wgpu (#4172, #6528); + // the probe meets it instead, marks it lost, and the frame is dropped unpresented. + if render_state + .device_lost + .probe(&render_state.device, &render_state.queue) + { if let Some(effect) = &render_state.renderer.read().frame_compositor { effect.suspend(); }