From de73a4da181455dd52cc8b4395fdac6a2ff8ad90 Mon Sep 17 00:00:00 2001 From: 00xJ0 <56056764+00xJ0@users.noreply.github.com> Date: Fri, 16 Oct 2026 19:05:31 -0700 Subject: [PATCH 1/5] Fix the build: restore export_presets in load_presets_json (#5344 x #5616) #5344 added the export_presets field to Persisted; #5616 rewrote load_presets_json to read each set with take() and its Persisted initializer lacked the new field, so main stopped compiling (E0063 at presets/mod.rs:180). Read export_presets the same way as the other sets; the existing load step and round-trip test cover it. --- crates/engine/src/presets/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/engine/src/presets/mod.rs b/crates/engine/src/presets/mod.rs index 03696291dd..81382b418a 100644 --- a/crates/engine/src/presets/mod.rs +++ b/crates/engine/src/presets/mod.rs @@ -186,6 +186,7 @@ impl Session { tool_presets: take(&mut m, "tool_presets"), documents: take(&mut m, "documents"), recent_documents: take(&mut m, "recent_documents"), + export_presets: take(&mut m, "export_presets"), custom_shapes: take(&mut m, "custom_shapes"), layer_defaults: take(&mut m, "layer_defaults"), swatches: take(&mut m, "swatches"), From c907301d274323e4977d1da42c3b9067a1419a58 Mon Sep 17 00:00:00 2001 From: hogawish <60434858+hogawish@users.noreply.github.com> Date: Sat, 17 Oct 2026 19:28:31 +0900 Subject: [PATCH 2/5] perf(canvas): skip occluded checker on CPU textures --- crates/ui-egui/src/canvas.rs | 27 ++- .../src/canvas_cpu_background_tests.rs | 180 ++++++++++++++++++ docs/development.md | 12 ++ 3 files changed, 215 insertions(+), 4 deletions(-) create mode 100644 crates/ui-egui/src/canvas_cpu_background_tests.rs diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 914d395acc..d6f7b927ee 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -142,6 +142,9 @@ 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. + pub texture_opaque: bool, /// The CPU path's pixel grid for the part of the document in view (pixel_grid.rs). pub grid: Option, } @@ -1423,6 +1426,7 @@ pub fn ensure_texture(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize, on_gpu: false, tex_revision: 0, tex_preview_key: 0, + texture_opaque: false, grid: None, }); if cache.tex_revision != revision || cache.texture.is_none() || cache.tex_preview_key != preview_key { @@ -1438,7 +1442,11 @@ pub fn ensure_texture(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize, let r = buf.rect; if !r.is_empty() { let t1 = crate::gpu_canvas::now_ms(); - t.set_partial([r.x0 as usize, r.y0 as usize], display_image(display.as_deref(), &buf), canvas_texture_options(factor)); + let image = display_image(display.as_deref(), &buf); + // Partial uploads can disprove opacity. Once transparent, conservatively keep + // the checker until a full upload proves the whole texture opaque again. + cache.texture_opaque &= image.pixels.iter().all(|p| p.is_opaque()); + t.set_partial([r.x0 as usize, r.y0 as usize], image, canvas_texture_options(factor)); let px = (r.width() as u64 * r.height() as u64).saturating_mul(u64::from(factor).pow(2)); app.perf.record("rect", px, t1 - t0, crate::gpu_canvas::now_ms() - t1); } @@ -1447,6 +1455,7 @@ pub fn ensure_texture(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize, let full = photocraft_compose::render_reduced(&doc, w, h); let t1 = crate::gpu_canvas::now_ms(); let (img, scale) = (display_image(display.as_deref(), &full), 1.0 / factor as f32); + cache.texture_opaque = img.pixels.iter().all(|p| p.is_opaque()); match cache.texture.as_mut() { Some(t) if t.size() == img.size => t.set(img, canvas_texture_options(factor)), _ => cache.texture = Some(ctx.load_texture(format!("canvas-{}-{}", id.0, cache_key(id, output).1), img, canvas_texture_options(factor))), @@ -1612,6 +1621,7 @@ fn ensure_gpu(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize, visible: on_gpu: false, tex_revision: 0, tex_preview_key: 0, + texture_opaque: false, grid: None, }); let present = cache.on_gpu && gpu.has(id.0, size); @@ -3089,9 +3099,10 @@ 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); - paint_cpu_document(&painter, &xf, doc.bounds(), rotated, checkerboard, tex); + let opaque = 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); + paint_cpu_document(&painter, &pattern_xf(&xf, c, size), doc.bounds(), rotated, checkerboard, tex, opaque); } } // Channels panel: alpha / Quick Mask overlays and single-channel views (channel_view.rs). @@ -4242,11 +4253,12 @@ fn paint_cpu_document( rotated: bool, checker: Option<(egui::TextureId, f32)>, tex: Option, + opaque: bool, ) { let size = [bounds.width() as f32, bounds.height() as f32]; let img_rect = xf.doc_rect(bounds); match checker { - Some((checker_id, square)) => { + Some((checker_id, square)) if !opaque || tex.is_none() => { if rotated { let tiles = vec2(size[0], size[1]) / (2.0 * square); paint_mapped_image(painter, xf, checker_id, Rect::from_min_max(Pos2::ZERO, pos2(tiles.x, tiles.y)), size, Color32::WHITE); @@ -4255,6 +4267,9 @@ fn paint_cpu_document( painter.image(checker_id, img_rect, Rect::from_min_max(Pos2::ZERO, pos2(tiles.x, tiles.y)), Color32::WHITE); } } + // On software window adapters this hidden full-canvas pass is CPU rasterization too. + // Keep the white-background branch unchanged (its antialiased outer edge is visible). + Some(_) => {} None => { if rotated { paint_mapped_quad(painter, xf, size, Color32::WHITE); @@ -8705,3 +8720,7 @@ mod rotation_preview_isolation_tests { #[cfg(test)] #[path = "canvas_display_memo_tests.rs"] mod display_memo_tests; + +#[cfg(test)] +#[path = "canvas_cpu_background_tests.rs"] +mod cpu_background_tests; diff --git a/crates/ui-egui/src/canvas_cpu_background_tests.rs b/crates/ui-egui/src/canvas_cpu_background_tests.rs new file mode 100644 index 0000000000..ed0ac98dc5 --- /dev/null +++ b/crates/ui-egui/src/canvas_cpu_background_tests.rs @@ -0,0 +1,180 @@ +//! CPU-canvas opacity invalidation and the software renderer's hidden checker pass. +use super::*; +use serde_json::json; + +#[test] +fn cpu_canvas_omits_only_a_checker_hidden_by_an_opaque_texture() { + let ctx = egui::Context::default(); + let image = ctx.load_texture("image", egui::ColorImage::filled([2, 2], Color32::WHITE), TextureOptions::LINEAR); + let checker = ctx.load_texture("checker-test", egui::ColorImage::filled([2, 2], Color32::GRAY), TextureOptions::NEAREST); + for rotated in [false, true] { + for (opaque, texture, want_checker) in [(true, Some(image.id()), false), (false, Some(image.id()), true), (true, None, true)] { + let mut output = ctx.run_ui(Default::default(), |ui| { + let xf = ViewXform { + rect: Rect::from_min_size(Pos2::ZERO, vec2(300.0, 240.0)), + zoom: 1.0, + center: [50.0, 40.0], + flip: false, + rotation: if rotated { 23.0 } else { 0.0 }, + aspect: 1.0, + }; + paint_cpu_document(ui.painter(), &xf, DRect::new(0, 0, 100, 80), rotated, Some((checker.id(), 8.0)), texture, opaque); + }); + let drawn = output.shapes.iter().any(|s| matches!(&s.shape, egui::Shape::Mesh(m) if m.texture_id == checker.id())); + assert_eq!(drawn, want_checker, "rotated={rotated}, opaque={opaque}, texture={texture:?}"); + output.textures_delta.clear(); + } + } +} + +#[test] +fn cpu_texture_opacity_follows_full_and_partial_uploads() { + for depth in [8, 16, 32] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + let ctx = egui::Context::default(); + app.run("file.new", json!({"width": 32, "height": 24, "depth": depth, "background": "transparent"})).unwrap(); + let key = cache_key(app.session.documents()[0].doc.id, None); + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + assert!(!app.canvases[&key].texture_opaque, "{depth}: transparent canvas needs its checker"); + app.run("edit.fill", json!({"color": "#2050d0"})).unwrap(); + // Force a full upload to classify the complete image, as the first opaque image does. + app.canvases.get_mut(&key).unwrap().texture = None; + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + assert!(app.canvases[&key].texture_opaque); + app.run("select.rect", json!({"x": 2, "y": 3, "width": 4, "height": 5})).unwrap(); + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + app.run("edit.fill", json!({"color": "#e04010"})).unwrap(); + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + assert_eq!(app.perf.last_refresh, "rect"); + assert!(app.canvases[&key].texture_opaque, "an opaque patch keeps the guarantee"); + app.run("edit.clear", json!({})).unwrap(); + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + assert_eq!(app.perf.last_refresh, "rect"); + assert!(!app.canvases[&key].texture_opaque, "erasing a patch must show the checker again"); + app.run("edit.undo", json!({})).unwrap(); + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + app.canvases.get_mut(&key).unwrap().texture = None; + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + assert!(app.canvases[&key].texture_opaque, "a full opaque upload can omit the checker again"); + app.perf.last_refresh = ""; + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + assert_eq!(app.perf.last_refresh, "", "unchanged frames do not upload or classify pixels"); + } +} + +type Harness = egui_kittest::Harness<'static, PhotocraftApp>; + +fn harness(size: [u32; 2], background: &str) -> Harness { + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1440.0, 900.0)).with_pixels_per_point(1.0).with_max_steps(64).wgpu().build_eframe(|cc| { + PhotocraftApp::setup_context(&cc.egui_ctx, Default::default()); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + if let Some(rs) = &cc.wgpu_render_state { + app.perf.gpu_info.set_adapter(&rs.adapter.get_info()); + } + // app.gpu remains None: compose on the CPU, use the harness's software window adapter. + app.run("file.new", json!({"width": size[0], "height": size[1], "background": background})).unwrap(); + app.run("select.rect", json!({"x": size[0] / 2, "y": size[1] / 2, "width": size[0] / 10, "height": size[1] / 10})).unwrap(); + app.run("edit.fill", json!({"color": "#2050d0"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + app + }); + h.run_steps(12); + let key = cache_key(h.state().session.documents()[0].doc.id, None); + assert!(h.state().session.prefs().transparency_and_gamut.square().is_some()); + assert_eq!(h.state().canvases[&key].texture_opaque, background == "white", "fixture opacity"); + h +} + +/// Draw the unchanged reference path by temporarily disabling only the opacity guarantee. +fn reference_frame(h: &mut Harness) { + let flags: Vec<_> = h.state().canvases.iter().map(|(k, c)| (*k, c.texture_opaque)).collect(); + for c in h.state_mut().canvases.values_mut() { + c.texture_opaque = false; + } + h.step(); + for (k, opaque) in flags { + if let Some(c) = h.state_mut().canvases.get_mut(&k) { + c.texture_opaque = opaque; + } + } +} + +#[test] +fn omitting_an_occluded_checker_preserves_cpu_canvas_pixels() { + let Ok(mut h) = std::panic::catch_unwind(|| harness([320, 240], "white")) else { + eprintln!("skipping: no offscreen graphics adapter"); + return; + }; + for (rotation, flip, pattern) in [(0.0, false, false), (23.0, false, false), (0.0, true, false), (0.0, false, 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; + reference_frame(&mut h); + let reference = h.render().unwrap(); + h.step(); + let optimized = h.render().unwrap(); + assert!(reference == optimized, "rotation={rotation} flip={flip} pattern={pattern}"); + } +} + +#[test] +#[ignore = "manual release benchmark: 24 MP CPU canvas on the offscreen software adapter"] +fn cpu_canvas_background_benchmark() { + use std::time::Instant; + let mut results = Vec::new(); + let output = std::env::var_os("PHOTOCRAFT_CPU_BENCH_OUTPUT").map(std::path::PathBuf::from); + for background in ["white", "transparent"] { + let mut h = harness([6000, 4000], background); + let v = &mut h.state_mut().ui.views[0]; + v.zoom = 1.0; + v.center = [3000.0, 2000.0]; + v.fit_pending = false; + v.fill_pending = false; + for _ in 0..3 { + h.step(); + h.render().unwrap(); + } + let mut before = Vec::new(); + let mut after = Vec::new(); + // Alternate order to reduce warm-up/thermal bias. Same cached image and UI in both paths. + for n in 0..24 { + for reference in if n % 2 == 0 { [true, false] } else { [false, true] } { + h.state_mut().ui.views[0].center[0] = 3000.0 + if n % 2 == 0 { -3.0 } else { 3.0 }; + let t = Instant::now(); + if reference { + reference_frame(&mut h); + } else { + h.step(); + } + h.render().unwrap(); + (if reference { &mut before } else { &mut after }).push(t.elapsed().as_secs_f64() * 1000.0); + } + } + reference_frame(&mut h); + let old = h.render().unwrap(); + h.step(); + let new = h.render().unwrap(); + assert!(old == new, "{background}: same rendered pixels"); + if let Some(dir) = &output { + std::fs::create_dir_all(dir).unwrap(); + old.save(dir.join(format!("cpu-{background}-before.png"))).unwrap(); + new.save(dir.join(format!("cpu-{background}-after.png"))).unwrap(); + } + before.sort_by(f64::total_cmp); + after.sort_by(f64::total_cmp); + results.push(json!({ + "background": background, "document_pixels": 24_000_000, "viewport": [1440, 900], + "opaque_texture": background == "white", "checker_enabled": true, + "adapter": h.state().perf.gpu_info, "samples_per_path": before.len(), + "reference_p50_ms": before[11], "reference_p95_ms": before[22], + "optimized_p50_ms": after[11], "optimized_p95_ms": after[22], + "reference_ms": before, "optimized_ms": after, + "includes": "UI frame, cached CPU canvas rasterization and synchronous readback; no document recomposite", + })); + } + let report = serde_json::to_string_pretty(&results).unwrap(); + println!("{report}"); + if let Some(dir) = output { + std::fs::write(dir.join("cpu-background-benchmark.json"), report).unwrap(); + } +} diff --git a/docs/development.md b/docs/development.md index 035efbbcf0..017df8056e 100644 --- a/docs/development.md +++ b/docs/development.md @@ -307,6 +307,18 @@ translations do not affect the denominator. Unregistered locale TSV files are ig ## Performance notes +- The CPU canvas records whether an uploaded image is entirely opaque, so software window + 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. + 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 + per-frame p50/p95, including offscreen rasterization and synchronous readback. It checks + 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. + - 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). - **Layer effects on the GPU** (`gpu/src/fx.rs`, kernels in `gpu/src/compose.wgsl`). Every enabled effect becomes a *map program* over the layer's effect region (shift, dilate, Gaussian blur, glow ramp, bevel height and shading, contour, stroke band), mirroring `compose::effects` step by step; the chunked composite then paints through the maps, clipped to that region, and copies the result back into the backdrop in place, so a small text layer costs only its own pixels. The layer's shape (`compose::layer_shape`) and its distance fields (`compose::effects::distance_field`: a transform whose tie-breaking a parallel GPU pass can't reproduce bit for bit) come from compose on the CPU: bands computed in parallel, except the Euclidean fields of precise glows and chiselled bevels (a halo as tall as the bevel size would make most of a band's work redundant), which are one window whose passes run on all threads, the CPU compositor's own way. Everything is cached per layer state: an unrelated edit, or an effect's colour or opacity, rebuilds nothing; a brush dab recomputes the touched 256² tiles grown by the effect reach; changing one effect's geometry rebuilds that effect only. Cache budget `gpu::FX_BUDGET` (1.5 GB, least recently drawn layers evicted first). `PHOTOCRAFT_FX_TRACE=1` prints the CPU time of each rebuild. From bd6f18d76695239034f02b92daec742a19914a97 Mon Sep 17 00:00:00 2001 From: hogawish <60434858+hogawish@users.noreply.github.com> Date: Sat, 17 Oct 2026 19:36:50 +0900 Subject: [PATCH 3/5] test(canvas): keep fixture assertions outside adapter skip --- crates/ui-egui/src/canvas_cpu_background_tests.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/canvas_cpu_background_tests.rs b/crates/ui-egui/src/canvas_cpu_background_tests.rs index ed0ac98dc5..e7e6bc1d22 100644 --- a/crates/ui-egui/src/canvas_cpu_background_tests.rs +++ b/crates/ui-egui/src/canvas_cpu_background_tests.rs @@ -79,10 +79,13 @@ fn harness(size: [u32; 2], background: &str) -> Harness { app }); h.run_steps(12); + h +} + +fn assert_fixture(h: &Harness, background: &str) { let key = cache_key(h.state().session.documents()[0].doc.id, None); assert!(h.state().session.prefs().transparency_and_gamut.square().is_some()); assert_eq!(h.state().canvases[&key].texture_opaque, background == "white", "fixture opacity"); - h } /// Draw the unchanged reference path by temporarily disabling only the opacity guarantee. @@ -105,6 +108,8 @@ fn omitting_an_occluded_checker_preserves_cpu_canvas_pixels() { eprintln!("skipping: no offscreen graphics adapter"); return; }; + // 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)] { h.state_mut().ui.views[0].rotation = rotation; h.state_mut().ui.view.flip_horizontal = flip; @@ -125,6 +130,7 @@ fn cpu_canvas_background_benchmark() { let output = std::env::var_os("PHOTOCRAFT_CPU_BENCH_OUTPUT").map(std::path::PathBuf::from); for background in ["white", "transparent"] { let mut h = harness([6000, 4000], background); + assert_fixture(&h, background); let v = &mut h.state_mut().ui.views[0]; v.zoom = 1.0; v.center = [3000.0, 2000.0]; 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 4/5] 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). From 06530cd1a1f1497e5fc2a5e3007163bbf4c5135a Mon Sep 17 00:00:00 2001 From: David Mann Date: Thu, 22 Oct 2026 21:45:09 +0200 Subject: [PATCH 5/5] Integration: skip CPU canvas pixel tests only without an adapter (#6966) The two pixel/checker tests wrapped all of harness() in catch_unwind, so a failing fixture command or a panic in the first 12 frames was reported as "no offscreen graphics adapter" and the test passed without checking anything (CodeRabbit thread on line 107). harness() now runs the fixture commands first, catches only the wgpu renderer creation (the one step that panics without an adapter) and builds and steps the harness outside it. fixture_failures_are_not_skipped_as_a_missing_adapter feeds a 0x0 document: with the old catch-all it was swallowed and the test fails; now it panics. Co-authored-by: hogawish <60434858+hogawish@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 (1M context) --- .../src/canvas_cpu_background_tests.rs | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/crates/ui-egui/src/canvas_cpu_background_tests.rs b/crates/ui-egui/src/canvas_cpu_background_tests.rs index 3a74a0adc2..dfd7ea77e2 100644 --- a/crates/ui-egui/src/canvas_cpu_background_tests.rs +++ b/crates/ui-egui/src/canvas_cpu_background_tests.rs @@ -64,22 +64,30 @@ fn cpu_texture_opacity_follows_full_and_partial_uploads() { type Harness = egui_kittest::Harness<'static, PhotocraftApp>; -fn harness(size: [u32; 2], background: &str) -> Harness { - let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1440.0, 900.0)).with_pixels_per_point(1.0).with_max_steps(64).wgpu().build_eframe(|cc| { +/// The CPU-canvas fixture on the offscreen software adapter, or `None` when this machine has no +/// graphics adapter. Only creating the renderer may be skipped: a failing fixture command or a +/// panic in the first frames must fail the test, not pass it as "no adapter". +fn harness(size: [u32; 2], background: &str) -> Option { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + // app.gpu remains None: compose on the CPU, use the harness's software window adapter. + app.run("file.new", json!({"width": size[0], "height": size[1], "background": background})).unwrap(); + app.run("select.rect", json!({"x": size[0] / 2, "y": size[1] / 2, "width": size[0] / 10, "height": size[1] / 10})).unwrap(); + app.run("edit.fill", json!({"color": "#2050d0"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + // `wgpu()` creates the render state and panics when no adapter is found. + let builder = std::panic::catch_unwind(|| { + egui_kittest::Harness::::builder().with_size(egui::vec2(1440.0, 900.0)).with_pixels_per_point(1.0).with_max_steps(64).wgpu() + }) + .ok()?; + let mut h = builder.build_eframe(move |cc| { PhotocraftApp::setup_context(&cc.egui_ctx, Default::default()); - let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); if let Some(rs) = &cc.wgpu_render_state { app.perf.gpu_info.set_adapter(&rs.adapter.get_info()); } - // app.gpu remains None: compose on the CPU, use the harness's software window adapter. - app.run("file.new", json!({"width": size[0], "height": size[1], "background": background})).unwrap(); - app.run("select.rect", json!({"x": size[0] / 2, "y": size[1] / 2, "width": size[0] / 10, "height": size[1] / 10})).unwrap(); - app.run("edit.fill", json!({"color": "#2050d0"})).unwrap(); - app.run("select.deselect", json!({})).unwrap(); app }); h.run_steps(12); - h + Some(h) } fn assert_fixture(h: &Harness, background: &str) { @@ -102,13 +110,19 @@ fn reference_frame(h: &mut Harness) { } } +#[test] +#[should_panic(expected = "called `Result::unwrap()` on an `Err` value")] +fn fixture_failures_are_not_skipped_as_a_missing_adapter() { + // An invalid fixture document must fail on any machine, with or without a graphics adapter. + let _ = harness([0, 0], "white"); +} + #[test] fn omitting_an_occluded_checker_preserves_cpu_canvas_pixels() { - let Ok(mut h) = std::panic::catch_unwind(|| harness([320, 240], "white")) else { + let Some(mut h) = harness([320, 240], "white") else { eprintln!("skipping: no offscreen graphics adapter"); return; }; - // Fixture failures must fail the test, outside the missing-adapter skip above. assert_fixture(&h, "white"); eprintln!("CPU pixel comparison adapter: {}", serde_json::to_string(&h.state().perf.gpu_info).unwrap()); for (rotation, flip, pattern) in @@ -145,7 +159,7 @@ fn omitting_an_occluded_checker_preserves_cpu_canvas_pixels() { #[test] fn pattern_preview_keeps_the_legacy_checker_for_every_copy() { - let Ok(mut h) = std::panic::catch_unwind(|| harness([320, 240], "white")) else { + let Some(mut h) = harness([320, 240], "white") else { eprintln!("skipping: no offscreen graphics adapter"); return; }; @@ -188,7 +202,7 @@ fn cpu_canvas_background_benchmark() { let mut results = Vec::new(); let output = std::env::var_os("PHOTOCRAFT_CPU_BENCH_OUTPUT").map(std::path::PathBuf::from); for background in ["white", "transparent"] { - let mut h = harness([6000, 4000], background); + let mut h = harness([6000, 4000], background).expect("an offscreen graphics adapter"); assert_fixture(&h, background); let v = &mut h.state_mut().ui.views[0]; v.zoom = 1.0;