PhotoCraft: An open-source, photo editor written by scratch in pure Rust
perf(gpu): layer pages that just miss borrow the effect-map share (P60/P61)
Nicolas Falesy336c419f2fd1
diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rsindex 4d3fd8f93..25706b969 100644--- a/crates/gpu/src/lib.rs+++ b/crates/gpu/src/lib.rs@@ -468,6 +468,10 @@ pub struct Compositor {resident_budget: u64,/// GPU memory the effect-map cache may hold ([`FX_BUDGET`] or less).fx_budget: u64,+ /// Layer-page bytes the last full fit check asked for ([`Compositor::fit_layers`]).+ layer_need: u64,+ /// Bytes of `fx_budget` lent to `resident_budget` so those pages fit.+ lent: u64,/// What the view shows: a full refresh draws these cells last, so their pages are the/// ones still resident for the edits that follow.focus: Option<Rect>,@@ -738,6 +742,8 @@ impl Compositor {chunk: CHUNK,resident_budget: RESIDENT_BUDGET,fx_budget: (FX_BUDGET as u64).saturating_sub(prefix::MAX_BYTES),+ layer_need: 0,+ lent: 0,focus: None,frame: 0,stamp: 0,@@ -784,9 +790,11 @@ impl Compositor {self.page}- /// Set the GPU memory resident layer pages may hold (default [`RESIDENT_BUDGET`]).+ /// Set the GPU memory resident layer pages may hold (default [`RESIDENT_BUDGET`]), exactly:+ /// nothing is lent to it ([`Self::fit_layers`]) until the next [`Self::set_memory_budget`].pub fn set_resident_budget(&mut self, bytes: u64) {self.resident_budget = bytes;+ self.lent = 0;}/// Set the GPU memory the compositor may hold for layer pages and caches together@@ -797,6 +805,35 @@ impl Compositor {self.prefix.set_budget((caches / 2).min(prefix::MAX_BYTES));self.fx_budget = caches.saturating_sub(self.prefix.budget());self.resident_budget = bytes.saturating_sub(caches);+ self.lent = 0;+ self.lend();+ }++ /// Record that a render needs `needed` bytes of layer pages and return the resident budget+ /// they now have. A render of the `whole` document sets the need; a part of it only raises+ /// it, so a large partial refresh never shrinks what the next full one keeps resident.+ ///+ /// Pages that miss their share by less than the effect-map share borrow from it: an evicted+ /// effect map is rebuilt by the next refresh that draws it, but one page too many sends the+ /// whole refresh to the CPU compositor, 10× slower (#5973, P60: 150 layers of 6 MP on an+ /// integrated GPU missed by 5 % and took 680 ms instead of 62).+ /// The lower-stack share is never lent: it is what keeps a mid-stack edit at half the work.+ /// The loan holds across [`Self::set_memory_budget`] until a later call needs less or more.+ pub fn fit_layers(&mut self, needed: u64, whole: bool) -> u64 {+ self.layer_need = if whole { needed } else { self.layer_need.max(needed) };+ self.lend();+ self.resident_budget+ }++ /// Return the loan to the effect maps, then lend what [`Self::layer_need`] still lacks, if+ /// the effect-map share covers it (lending part of it would fit nothing and only evict maps).+ fn lend(&mut self) {+ self.fx_budget = self.fx_budget.saturating_add(self.lent);+ self.resident_budget = self.resident_budget.saturating_sub(self.lent);+ let short = self.layer_need.saturating_sub(self.resident_budget);+ self.lent = if short <= self.fx_budget { short } else { 0 };+ self.fx_budget = self.fx_budget.saturating_sub(self.lent);+ self.resident_budget = self.resident_budget.saturating_add(self.lent);}/// The budget of layer pages, effect maps and lower-stack chunks (bytes).diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rsindex cacd79c0f..5a34e8867 100644--- a/crates/gpu/tests/parity.rs+++ b/crates/gpu/tests/parity.rs@@ -2842,6 +2842,38 @@ fn the_focused_view_stays_resident_after_a_full_refresh() {}}+#[test]+fn layer_pages_that_just_miss_borrow_the_effect_map_share() {+ let _gamma = TEXT_GAMMA.lock().unwrap_or_else(|e| e.into_inner());+ let Some(g) = gpu() else { return };+ let d = big_doc();+ let mut comp = Compositor::try_new_with_format(&g.device, wgpu::TextureFormat::Rgba32Float).unwrap();+ comp.set_texture_limit(PAGED_LIMIT);+ let need = comp.working_set(&d, d.bounds());+ // Pages get three quarters of the budget and effect maps an eighth: at 5/4 of the need the+ // pages' own share misses by about 6 %, which the effect-map share covers (#5973, P60).+ let budget = need / 4 * 5;+ comp.set_memory_budget(budget);+ let own = comp.resident_budget();+ assert!(own < need && !comp.fits_budget(&d, d.bounds()));+ assert_eq!(comp.fit_layers(need, true), need, "lends exactly the shortfall");+ assert_eq!(comp.memory_budget(), budget, "the total stays");+ comp.set_memory_budget(budget);+ assert_eq!(comp.resident_budget(), need, "the loan holds across the budget set before every refresh");+ assert_eq!(comp.fit_layers(need / 2, false), need, "a partial refresh doesn't shrink it");+ photocraft_gpu::render_to_vec(&mut comp, &g.device, &g.queue, &d, d.bounds()).unwrap();+ let (out, s) = photocraft_gpu::render_to_vec_stats(&mut comp, &g.device, &g.queue, &d, d.bounds()).unwrap();+ // Every page stays resident; effect maps may be evicted and rebuilt, that's the loan.+ assert_eq!(s.tiles_uploaded, 0, "{s:?}");+ assert!(worst_diff(&photocraft_compose::render(&d, d.bounds()).px, &out).0 <= TOL);+ // A need the effect-map share can't cover lends nothing (it would only evict maps), and a+ // smaller need gives the loan back.+ assert_eq!(comp.fit_layers(budget, true), own);+ comp.fit_layers(need, true);+ assert_eq!(comp.fit_layers(own, true), own);+ assert_eq!(comp.memory_budget(), budget);+}+// A pass-through group holding an adjustment renders isolated when layers clip to it, on the GPU// as on the CPU (psd-tools passthrough_clipping_mask_adjustment).#[test]diff --git a/crates/ui-egui/src/gpu_canvas.rs b/crates/ui-egui/src/gpu_canvas.rsindex e456b4a75..491992094 100644--- a/crates/ui-egui/src/gpu_canvas.rs+++ b/crates/ui-egui/src/gpu_canvas.rs@@ -326,7 +326,8 @@ impl GpuCanvas {if let Some(b) = budget {comp.set_memory_budget(b);}- let fit = LayerFit { doc: doc.id.0, needed: comp.working_set(doc, doc.bounds()), budget: comp.resident_budget() };+ let needed = comp.working_set(doc, doc.bounds());+ let fit = LayerFit { doc: doc.id.0, needed, budget: comp.fit_layers(needed, true) };res.layer_fit = Some(fit);Some(fit)}@@ -509,7 +510,8 @@ impl GpuCanvas {// as hiding a big layer (#4024); small edits in the view stay on the GPU.let large = region.width() as u64 * region.height() as u64 >= DEFER_PX;if region == doc.bounds() || large {- let fit = LayerFit { doc: doc.id.0, needed: comp.working_set(doc, region), budget: comp.resident_budget() };+ let needed = comp.working_set(doc, region);+ let fit = LayerFit { doc: doc.id.0, needed, budget: comp.fit_layers(needed, region == doc.bounds()) };res.layer_fit = Some(fit);if !fit.fits() {res.compositor = Some(comp);