PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge PR #5370: fix(gpu): catch validation errors in watched compositor work
David Mann7f68d4e06add
diff --cc crates/gpu/src/lib.rsindex d270b5580,7fabc4aaf..05e12217d--- a/crates/gpu/src/lib.rs+++ b/crates/gpu/src/lib.rs@@@ -599,28 -552,9 +599,28 @@@ fn field_reaches(progs: &[fx::MapPrograwant}+/// Why a refresh whose layer pages don't fit GPU memory is drawn on the CPU.+pub const OVER_BUDGET: &str = "layers exceed the GPU memory budget; full refresh on the CPU";++/// Which of `pages` (LRU clock, key, bytes) to drop, least recently used first, so `incoming`+/// more bytes fit `budget` on top of the `total` held now. Callers leave out pages that must stay.+fn lru_victims<K>(mut pages: Vec<(u64, K, u64)>, total: u64, incoming: u64, budget: u64) -> Vec<K> {+ pages.sort_unstable_by_key(|p| p.0);+ let mut total = total;+ let mut out = Vec::new();+ for (_, k, bytes) in pages {+ if total.saturating_add(incoming) <= budget {+ break;+ }+ total = total.saturating_sub(bytes);+ out.push(k);+ }+ out+}+/// Runs `f` inside GPU error scopes and returns the first error raised.- /// Native wgpu reports errors synchronously; where they only arrive asynchronously (WebGPU) none- /// is reported.+ /// Native wgpu reports validation/internal/OOM errors synchronously; where they only arrive+ /// asynchronously (WebGPU) none is reported.fn first_error<T>(device: &wgpu::Device, f: impl FnOnce() -> T) -> (T, Option<wgpu::Error>) {let validation = device.push_error_scope(wgpu::ErrorFilter::Validation);let internal = device.push_error_scope(wgpu::ErrorFilter::Internal);@@@ -1094,49 -931,10 +1107,49 @@@ impl Compositorregion: Rect,mut sink: impl FnMut(&mut wgpu::CommandEncoder, ChunkOut<'_>),) -> Result<Stats, Unsupported> {- let result = scoped_work(device, self.health.is_some(), || {+ let result = scoped_work(device, self.health.clone(), || {let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("pc_compose") });- let stats = self.encode_inner(device, queue, &mut encoder, doc, region, &mut sink, true)?;+ if self.timer.as_ref().is_some_and(timing::GpuTimer::busy) {+ // The readback map completes on a poll. The app polls every frame, but a+ // non-blocking poll here keeps the measurements coming whatever the caller does.+ match &self.health {+ Some(h) => h.poll(device),+ None => {+ let _ = device.poll(wgpu::PollType::Poll);+ }+ }+ }+ // Whatever came back since the last render travels out with this one's stats, so+ // nothing has to reach back into the compositor for it afterwards.+ let landed = self.timer.as_mut().and_then(timing::GpuTimer::take);+ if let Some(t) = &mut self.timer {+ let r = region.intersect(&doc.bounds());+ t.start(&mut encoder, r.width() as u64 * r.height() as u64);+ }+ let mut stats = self.encode_inner(device, queue, &mut encoder, doc, region, &mut sink, true);+ if let Ok(s) = &mut stats {+ s.gpu_time = landed;+ }+ if let Some(t) = &mut self.timer {+ // A refresh that failed part way measures nothing: its spans describe work the+ // caller is about to redo on the CPU.+ match stats {+ Ok(_) => t.finish(&mut encoder),+ Err(_) => {+ t.cancel();+ // The refresh is lost, but a measurement that came back with it is not:+ // hand it to the next render rather than drop it on the fallback path.+ if let Some(landed) = landed {+ t.restore(landed);+ }+ }+ }+ }+ let stats = stats?;queue.submit([encoder.finish()]);+ if let Some(t) = &mut self.timer {+ t.submitted();+ }Ok(stats)});if result.is_err() {