From 88d7edcb02d2da2cba68751f57db7f04d2e5d76b Mon Sep 17 00:00:00 2001 From: hogawish <60434858+hogawish@users.noreply.github.com> Date: Mon, 19 Oct 2026 12:35:27 +0900 Subject: [PATCH] fix(canvas): retain legacy checker in Pattern Preview --- crates/ui-egui/src/canvas.rs | 8 ++- .../src/canvas_cpu_background_tests.rs | 61 ++++++++++++++++++- docs/development.md | 6 ++ 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index d6f7b927ee..54cfaa374a 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -142,8 +142,8 @@ pub struct CanvasCache { /// `revision`, so the Navigator never shows a stale CPU image after GPU-path edits. pub tex_revision: u64, pub tex_preview_key: u64, - /// All uploaded texels are opaque: the checker behind this image cannot be seen. Updated - /// with uploads, never by scanning the document during a pan or other unchanged frame. + /// All uploaded texels are opaque. Screen coverage is checked separately before omitting + /// the checker. Updated with uploads, never by scanning during an unchanged frame. pub texture_opaque: bool, /// The CPU path's pixel grid for the part of the document in view (pixel_grid.rs). pub grid: Option, @@ -3099,7 +3099,9 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect: let rotated = view.rotation.abs() > 0.05; let checkerboard = app.session.prefs().transparency_and_gamut.square().map(|square| (checker(app, &ctx), square)); let tex = ensure_texture(app, &ctx, idx, output).map(|(tex, _scale)| tex); - let opaque = app.canvases.get(&cache_key(doc.id, output)).is_some_and(|c| c.texture_opaque) && tex.is_some(); + // Repeated-quad coverage is not proven identical on all Pattern Preview adapters. + // Retain its legacy checker submissions, including the central document's. + let opaque = !app.ui.view.pattern_preview && app.canvases.get(&cache_key(doc.id, output)).is_some_and(|c| c.texture_opaque) && tex.is_some(); paint_cpu_document(&painter, &xf, doc.bounds(), rotated, checkerboard, tex, opaque); for &c in &pattern { paint_cpu_document(&painter, &pattern_xf(&xf, c, size), doc.bounds(), rotated, checkerboard, tex, opaque); diff --git a/crates/ui-egui/src/canvas_cpu_background_tests.rs b/crates/ui-egui/src/canvas_cpu_background_tests.rs index e7e6bc1d22..3a74a0adc2 100644 --- a/crates/ui-egui/src/canvas_cpu_background_tests.rs +++ b/crates/ui-egui/src/canvas_cpu_background_tests.rs @@ -110,7 +110,10 @@ fn omitting_an_occluded_checker_preserves_cpu_canvas_pixels() { }; // Fixture failures must fail the test, outside the missing-adapter skip above. assert_fixture(&h, "white"); - for (rotation, flip, pattern) in [(0.0, false, false), (23.0, false, false), (0.0, true, false), (0.0, false, true)] { + eprintln!("CPU pixel comparison adapter: {}", serde_json::to_string(&h.state().perf.gpu_info).unwrap()); + for (rotation, flip, pattern) in + [(0.0, false, false), (23.0, false, false), (0.0, true, false), (0.0, false, true), (23.0, false, true), (0.0, true, true), (23.0, true, true)] + { h.state_mut().ui.views[0].rotation = rotation; h.state_mut().ui.view.flip_horizontal = flip; h.state_mut().ui.view.pattern_preview = pattern; @@ -118,10 +121,66 @@ fn omitting_an_occluded_checker_preserves_cpu_canvas_pixels() { let reference = h.render().unwrap(); h.step(); let optimized = h.render().unwrap(); + if let Some(dir) = std::env::var_os("PHOTOCRAFT_CPU_PIXEL_OUTPUT") { + let dir = std::path::PathBuf::from(dir); + std::fs::create_dir_all(&dir).unwrap(); + let tag = format!("rotation-{rotation}-flip-{flip}-pattern-{pattern}"); + reference.save(dir.join(format!("{tag}-reference.png"))).unwrap(); + optimized.save(dir.join(format!("{tag}-optimized.png"))).unwrap(); + let mut changed = 0; + let mut bounds = [u32::MAX, u32::MAX, 0, 0]; + for (x, y, pixel) in reference.enumerate_pixels() { + if pixel != optimized.get_pixel(x, y) { + changed += 1; + bounds = [bounds[0].min(x), bounds[1].min(y), bounds[2].max(x), bounds[3].max(y)]; + } + } + let report = json!({"adapter": h.state().perf.gpu_info, "rotation": rotation, "flip": flip, + "pattern": pattern, "changed_pixels": changed, "difference_bounds": (changed > 0).then_some(bounds)}); + std::fs::write(dir.join(format!("{tag}.json")), serde_json::to_string_pretty(&report).unwrap()).unwrap(); + } assert!(reference == optimized, "rotation={rotation} flip={flip} pattern={pattern}"); } } +#[test] +fn pattern_preview_keeps_the_legacy_checker_for_every_copy() { + let Ok(mut h) = std::panic::catch_unwind(|| harness([320, 240], "white")) else { + eprintln!("skipping: no offscreen graphics adapter"); + return; + }; + assert_fixture(&h, "white"); + let checker = h.state().checker.as_ref().unwrap().id(); + let checker_meshes = |h: &Harness| { + h.output() + .shapes + .iter() + .filter_map(|s| match &s.shape { + egui::Shape::Mesh(m) if m.texture_id == checker => Some(m.clone()), + _ => None, + }) + .collect::>() + }; + for (rotation, flip) in [(0.0, false), (23.0, false), (0.0, true), (23.0, true)] { + h.state_mut().ui.view.pattern_preview = true; + h.state_mut().ui.view.flip_horizontal = flip; + let v = &mut h.state_mut().ui.views[0]; + v.zoom = 1.25; + v.center = [159.25, 119.75]; + v.rotation = rotation; + v.fit_pending = false; + v.fill_pending = false; + reference_frame(&mut h); + let reference = checker_meshes(&h); + assert!(reference.len() > 1, "the document and its visible copies need their checker"); + h.step(); + assert_eq!(checker_meshes(&h), reference, "rotation={rotation} flip={flip}: retain the legacy checker submissions"); + } + h.state_mut().ui.view.pattern_preview = false; + h.step(); + assert!(checker_meshes(&h).is_empty(), "ordinary opaque CPU canvases still omit the hidden checker"); +} + #[test] #[ignore = "manual release benchmark: 24 MP CPU canvas on the offscreen software adapter"] fn cpu_canvas_background_benchmark() { diff --git a/docs/development.md b/docs/development.md index 017df8056e..ee48536618 100644 --- a/docs/development.md +++ b/docs/development.md @@ -311,6 +311,8 @@ translations do not affect the denominator. Unregistered locale TSV files are ig adapters do not rasterize a transparency checker that the image completely hides. An edit that introduces transparency restores the checker; partial uploads conservatively retain it until a full upload proves the image opaque again. Pan/zoom frames reuse this metadata. + Pattern Preview retains every legacy checker pass: opaque texels alone do not establish + identical coverage between repeated copies on every adapter. Compare the old checker pass with the optimized path on a synthetic 24 MP document using `cargo test --release -p photocraft-ui-egui --lib cpu_canvas_background_benchmark -- --ignored --nocapture`. The benchmark alternates both paths on the same cached image and reports the adapter and @@ -318,6 +320,10 @@ translations do not affect the denominator. Unregistered locale TSV files are ig identical rendered pixels for opaque and transparent images. This measures warm canvas presentation, not input-to-photon latency or document recompositing. Set the test-only `PHOTOCRAFT_CPU_BENCH_OUTPUT` to a directory to save its JSON and before/after PNGs. + For the small pixel-identity regression, set `PHOTOCRAFT_CPU_PIXEL_OUTPUT` and run + `cargo test -p photocraft-ui-egui --lib omitting_an_occluded_checker_preserves_cpu_canvas_pixels -- --nocapture`. + This reports the adapter and saves per-view reference/optimized PNGs and exact difference + counts/bounds without relaxing the pixel comparison. - The canvas is presented by a custom WGSL shader (`ui-egui/src/gpu_canvas.rs`): mip-mapped/nearest sampling, procedural checkerboard, pixel grid, tiling. Brush strokes upload only their damage rect. - **The canvas composites on the GPU** (`photocraft-gpu`, driven from `gpu_canvas.rs`), layer effects included. What the planner can't express returns `Unsupported` and the canvas falls back to the CPU compositor (`photocraft-compose`, also the reference for export and tests): Multichannel documents, and documents or effect regions over the texture limit. Pieces the GPU can't derive itself are rasterised once on the CPU and cached (`compose::masks` for vector masks, `compose::shape_split` for stroked shapes with clipped layers, effect distance fields). Timings of the interactive paths: `cargo run --release -p photocraft-ui-egui --example interactive_bench`; effects: `--example fx_bench` (`--compare files…` for GPU vs CPU); the cost of changing a chisel bevel's size (a dragged slider, GPU and CPU, ellipse and text): `--example bevel_bench`; large documents (open, refresh, thumbnails, a filter, a stroke, saves; one operation per run so `/usr/bin/time -l` gives its peak memory): `--example large_image_bench -- --size 14000x14000 --op psd`. The app requests the adapter's own texture limit (egui's default is 8192 px; see `gpu_canvas::use_adapter_limits`); beyond it the CPU fallback composites and uploads in bands (`compose::render_bands`), and exports, thumbnails and flattening stream bands too, so no full-size float composite is ever held. Rendering fidelity: `cargo run --release -p photocraft-io --example oracle_diff -- corpus/psd` (the whole PSD oracle table in seconds). `ui.inspect` → `perf.timings.gpuFallback` names the reason (`null` on the GPU path).