From f57bb1f3b1a47f8af04f451896f043f3ff797b86 Mon Sep 17 00:00:00 2001 From: Elvis33LE Date: Thu, 15 Oct 2026 19:30:51 +0200 Subject: [PATCH 1/5] perf(gpu): batch resident overlap copies --- crates/gpu/src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs index 8f8839b9bd..845e29b7bb 100644 --- a/crates/gpu/src/lib.rs +++ b/crates/gpu/src/lib.rs @@ -1540,7 +1540,7 @@ impl Compositor { }; // Only the region moved (painting past a layer's tiles grows it): keep the texels the // old texture already holds instead of converting and uploading every tile again. - let kept = self.residents.remove(&key).map_or(Rect::EMPTY, |old| keep_overlap(device, queue, &old, &mut r)); + let kept = self.residents.remove(&key).map_or(Rect::EMPTY, |old| keep_overlap(encoder, &old, &mut r)); // A new layer whose tiles another resident already holds (a duplicate shares its // source's tiles) starts as a GPU copy of that texture. Duplicating a 20 MP photo // converted and uploaded every tile again. @@ -2109,16 +2109,14 @@ fn put_tiles(device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::Com /// format and default pixel; only the region moved) and carry over the tiles they show, so only /// tiles outside the overlap or changed since need uploading. Returns the kept (tile-aligned) /// rect; empty when nothing can be kept. -fn keep_overlap(device: &wgpu::Device, queue: &wgpu::Queue, old: &Resident, new: &mut Resident) -> Rect { +fn keep_overlap(encoder: &mut wgpu::CommandEncoder, old: &Resident, new: &mut Resident) -> Rect { let same = old.kind == new.kind && old.format == new.format && old.cmyk == new.cmyk && old.default_bits == new.default_bits; let k = old.region.intersect(&new.region); if !same || k.is_empty() { return Rect::EMPTY; } - // Its own submission: tile writes go through the queue and land at the start of the next - // submit, before anything in the frame's encoder, so a copy recorded there would overwrite - // the tiles written after it with the old texels. - let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("pc_compose_keep") }); + // Record the copy before this frame's tile uploads/copies. Keeping it in the active encoder + // preserves that order without forcing a separate queue submission for every region shift. encoder.copy_texture_to_texture( wgpu::TexelCopyTextureInfo { texture: &old.texture, @@ -2134,7 +2132,9 @@ fn keep_overlap(device: &wgpu::Device, queue: &wgpu::Queue, old: &Resident, new: }, wgpu::Extent3d { width: k.width(), height: k.height(), depth_or_array_layers: 1 }, ); - queue.submit([encoder.finish()]); + // A direct `queue.write_texture` would execute before this encoder's copy and then be + // overwritten. Route changed tiles through encoder-ordered scratch copies for this resident. + new.ordered = true; new.tiles = old.tiles.iter().filter(|(c, _)| k.contains_rect(&c.rect())).map(|(c, t)| (*c, t.clone())).collect(); k } From 7414914290259f8a9530b830bb87522783fe7808 Mon Sep 17 00:00:00 2001 From: Elvis33LE Date: Sat, 17 Oct 2026 17:53:39 +0200 Subject: [PATCH 2/5] test(gpu): tile growth rewrites must beat the kept overlap copy Grows a raster layer one tile per render and rewrites everything with per-step noise, on the whole-texture and the paged compositor, comparing each render against the CPU oracle. Fails at 254/255 when keep_overlap does not route the resident through encoder-ordered writes (verified by temporarily dropping new.ordered = true). --- crates/gpu/tests/parity.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rs index 84d3d0e937..6457f99f7d 100644 --- a/crates/gpu/tests/parity.rs +++ b/crates/gpu/tests/parity.rs @@ -4,7 +4,7 @@ use photocraft_color::{BlendMode, Color, ColorMode, PixelFormat, SampleType}; use photocraft_doc::adjust::{CurvePoint, HueRange, LevelsChannel}; use photocraft_doc::{Adjustment, Document, Fill, GradientStyle, Layer, LayerContent, LayerMask}; -use photocraft_geom::{Rect, Size}; +use photocraft_geom::{Rect, Size, TILE_SIZE}; use photocraft_gpu::{Compositor, render_to_vec}; /// One 8-bit step, plus a few f32 ulps: two results exactly one step apart can differ by a hair @@ -195,6 +195,30 @@ fn check(g: &mut Gpu, doc: &Document, what: &str) { diff_rect(g, doc, doc.bounds(), what).unwrap_or_else(|e| panic!("{e}")); } +/// Growing a layer's tile footprint keeps the old texels in the overlap (`keep_overlap`) while +/// the same update rewrites tiles inside that overlap (#6243). The keep copy is recorded in the +/// frame's encoder and the changed tiles go through encoder-ordered scratch copies, so the +/// fresh texels must win; if the copy ran after the tile writes (queue writes land before the +/// encoder does), the overlap would keep the previous step's colours. Every step grows by one +/// tile and rewrites everything with per-step noise, on the whole-texture and the paged +/// compositor alike, and each render matches the CPU oracle. +#[test] +fn tile_growth_rewrite_keeps_fresh_texels_in_the_overlap() { + let Some(mut g) = gpu() else { return }; + let tile = TILE_SIZE as u32; + let steps = 4; + let mut doc = Document::new("tile growth", Size::new(steps * tile, tile), ColorMode::Rgb, SampleType::U8); + doc.layers.push(Layer::raster("grow", PixelFormat::RGBA8)); + for step in 1..=steps { + // Wider by one tile than at the last render, so the resident's region grows and every + // tile inside the kept overlap changes: stale texels cannot masquerade as correct ones. + let w = step * tile; + let data: Vec = (0..w * tile * 4).map(|i| rnd(1000 + step, i)).collect(); + doc.layers[0].surface_mut().unwrap().write_region(Rect::from_xywh(0, 0, w, tile), &data); + check(&mut g, &doc, &format!("tile growth step {step}/{steps}")); + } +} + /// Closing a document frees what the compositor holds for it, however its canvas entry went /// (#4529): `retain_docs` drops the resident pages, effect maps and lower-stack composite of /// documents that aren't open, and keeps the open ones. From 459a25bd9558e5d0bf31fee898f90bc7ca18a677 Mon Sep 17 00:00:00 2001 From: Elvis33LE Date: Sun, 18 Oct 2026 09:17:34 +0200 Subject: [PATCH 3/5] Revert "perf(gpu): batch resident overlap copies" This reverts commit f57bb1f3b1a47f8af04f451896f043f3ff797b86. --- crates/gpu/src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs index 845e29b7bb..8f8839b9bd 100644 --- a/crates/gpu/src/lib.rs +++ b/crates/gpu/src/lib.rs @@ -1540,7 +1540,7 @@ impl Compositor { }; // Only the region moved (painting past a layer's tiles grows it): keep the texels the // old texture already holds instead of converting and uploading every tile again. - let kept = self.residents.remove(&key).map_or(Rect::EMPTY, |old| keep_overlap(encoder, &old, &mut r)); + let kept = self.residents.remove(&key).map_or(Rect::EMPTY, |old| keep_overlap(device, queue, &old, &mut r)); // A new layer whose tiles another resident already holds (a duplicate shares its // source's tiles) starts as a GPU copy of that texture. Duplicating a 20 MP photo // converted and uploaded every tile again. @@ -2109,14 +2109,16 @@ fn put_tiles(device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::Com /// format and default pixel; only the region moved) and carry over the tiles they show, so only /// tiles outside the overlap or changed since need uploading. Returns the kept (tile-aligned) /// rect; empty when nothing can be kept. -fn keep_overlap(encoder: &mut wgpu::CommandEncoder, old: &Resident, new: &mut Resident) -> Rect { +fn keep_overlap(device: &wgpu::Device, queue: &wgpu::Queue, old: &Resident, new: &mut Resident) -> Rect { let same = old.kind == new.kind && old.format == new.format && old.cmyk == new.cmyk && old.default_bits == new.default_bits; let k = old.region.intersect(&new.region); if !same || k.is_empty() { return Rect::EMPTY; } - // Record the copy before this frame's tile uploads/copies. Keeping it in the active encoder - // preserves that order without forcing a separate queue submission for every region shift. + // Its own submission: tile writes go through the queue and land at the start of the next + // submit, before anything in the frame's encoder, so a copy recorded there would overwrite + // the tiles written after it with the old texels. + let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("pc_compose_keep") }); encoder.copy_texture_to_texture( wgpu::TexelCopyTextureInfo { texture: &old.texture, @@ -2132,9 +2134,7 @@ fn keep_overlap(encoder: &mut wgpu::CommandEncoder, old: &Resident, new: &mut Re }, wgpu::Extent3d { width: k.width(), height: k.height(), depth_or_array_layers: 1 }, ); - // A direct `queue.write_texture` would execute before this encoder's copy and then be - // overwritten. Route changed tiles through encoder-ordered scratch copies for this resident. - new.ordered = true; + queue.submit([encoder.finish()]); new.tiles = old.tiles.iter().filter(|(c, _)| k.contains_rect(&c.rect())).map(|(c, t)| (*c, t.clone())).collect(); k } From d1ee15a189de032bf847bb4ceb5f6e77c8a32c88 Mon Sep 17 00:00:00 2001 From: Elvis33LE Date: Sun, 18 Oct 2026 11:04:18 +0200 Subject: [PATCH 4/5] ci: retrigger (portal_save timed out on ubuntu; 9193/9194 passed, branch is gpu-test-only) From 40b3bf9b6b9522838914264e3bae7a796e017add Mon Sep 17 00:00:00 2001 From: Elvis33LE Date: Tue, 20 Oct 2026 19:02:51 +0200 Subject: [PATCH 5/5] test(gpu): reword the tile-growth doc comment for what main does The comment still described the reverted encoder-batching design; on main keep_overlap submits its own copy, so the pinned property is that the kept overlap never overwrites tiles rewritten in the same update, whatever the submission shape. --- crates/gpu/tests/parity.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rs index 6457f99f7d..5737336c66 100644 --- a/crates/gpu/tests/parity.rs +++ b/crates/gpu/tests/parity.rs @@ -195,13 +195,12 @@ fn check(g: &mut Gpu, doc: &Document, what: &str) { diff_rect(g, doc, doc.bounds(), what).unwrap_or_else(|e| panic!("{e}")); } -/// Growing a layer's tile footprint keeps the old texels in the overlap (`keep_overlap`) while -/// the same update rewrites tiles inside that overlap (#6243). The keep copy is recorded in the -/// frame's encoder and the changed tiles go through encoder-ordered scratch copies, so the -/// fresh texels must win; if the copy ran after the tile writes (queue writes land before the -/// encoder does), the overlap would keep the previous step's colours. Every step grows by one -/// tile and rewrites everything with per-step noise, on the whole-texture and the paged -/// compositor alike, and each render matches the CPU oracle. +/// Growing a layer's tile footprint keeps the old texels in the overlap (`keep_overlap`) +/// while the same update rewrites tiles inside that overlap (#6243): whatever submission +/// shape the copy takes, the kept overlap must never overwrite tiles the same update +/// rewrote — the fresh texels win. Every step grows by one tile and rewrites everything +/// with per-step noise, on the whole-texture and the paged compositor alike, and each +/// render matches the CPU oracle. #[test] fn tile_growth_rewrite_keeps_fresh_texels_in_the_overlap() { let Some(mut g) = gpu() else { return };