Tests: compare blend previews with CPU pixels directly

This commit is contained in:
Dan committed 2026-11-30 20:52:06 +00:00
1 parent 1f7acffe94
commit 00ce7f9edd
3 files changed
+47 -40

No files matched your search

@@ -48,12 +48,11 @@ fn blend_hover_pattern_tiles_and_multiple_views_reuse_current_pixels() {
app.ui.view.pattern_preview = true;
crate::blend_preview::hover(&mut app, &[photocraft_doc::LayerId(layer)], Some(BlendMode::Multiply));
assert!(ensure_gpu(&mut app, &ctx, 0, a, None));
assert_visible_pixels(&mut app, b);
let bounds = app.session.active().unwrap().doc.bounds();
assert_visible_pixels(&mut app, bounds);
assert_visible_pixels(&mut app, &[bounds]);
crate::blend_preview::hover(&mut app, &[photocraft_doc::LayerId(layer)], None);
assert!(ensure_gpu(&mut app, &ctx, 0, a, None));
assert_visible_pixels(&mut app, bounds);
assert_visible_pixels(&mut app, &[bounds]);
app.ui.view.pattern_preview = false;
for mode in [BlendMode::Multiply, BlendMode::Screen] {
@@ -64,16 +63,16 @@ fn blend_hover_pattern_tiles_and_multiple_views_reuse_current_pixels() {
app.perf.last_refresh_px = 0;
assert!(ensure_gpu(&mut app, &ctx, 0, visible, None));
assert_eq!(app.perf.last_refresh_px, 0, "unchanged views must retain coverage together");
assert_visible_pixels(&mut app, visible.intersect(&bounds));
assert_visible_pixels(&mut app, &[visible.intersect(&bounds)]);
}
}
// Enabling periodic display during an existing hover must catch up the unseen gap too.
app.ui.view.pattern_preview = true;
assert!(ensure_gpu(&mut app, &ctx, 0, a, None));
assert_visible_pixels(&mut app, bounds);
assert_visible_pixels(&mut app, &[bounds]);
crate::blend_preview::hover(&mut app, &[photocraft_doc::LayerId(layer)], None);
assert!(ensure_gpu(&mut app, &ctx, 0, a, None));
assert_visible_pixels(&mut app, bounds);
assert_visible_pixels(&mut app, &[bounds]);
}
#[test]
@@ -96,12 +95,12 @@ fn blend_hover_during_background_redraw_restores_committed_pixels() {
let panned = DRect::new(2048, 1024, 2304, 1280);
assert!(ensure_gpu(&mut app, &ctx, 0, panned, None));
settle_redraw(&mut app);
assert_visible_pixels(&mut app, panned);
assert_visible_pixels(&mut app, &[panned]);
crate::blend_preview::hover(&mut app, &[photocraft_doc::LayerId(layer)], None);
assert!(ensure_gpu(&mut app, &ctx, 0, panned, None));
settle_redraw(&mut app);
let bounds = app.session.active().unwrap().doc.bounds();
assert_visible_pixels(&mut app, bounds);
assert_visible_pixels(&mut app, &[bounds]);
}
fn settle_redraw(app: &mut PhotocraftApp) {
@@ -115,19 +114,27 @@ fn settle_redraw(app: &mut PhotocraftApp) {
}
}
fn assert_visible_pixels(app: &mut PhotocraftApp, visible: DRect) {
fn assert_visible_pixels(app: &mut PhotocraftApp, regions: &[DRect]) {
let (doc, _) = display_doc(app, 0);
let gpu = app.gpu.clone().unwrap();
let reference = doc.id.0 ^ (1u64 << 60);
let (display, _) = canvas_display(app, &doc, None);
gpu.upload_composite(reference, &doc, display.as_deref());
let (_, size, shown) = gpu.read_texels(doc.id.0).unwrap();
let (_, _, want) = gpu.read_texels(reference).unwrap();
for y in visible.y0..visible.y1 {
for x in visible.x0..visible.x1 {
let index = y as usize * size[0] as usize + x as usize;
for channel in 0..4 {
assert!((shown[index][channel] - want[index][channel]).abs() <= 2.0 / 255.0, "preview differs at ({x},{y}) channel{channel}");
let (format, size, shown) = gpu.read_texels(doc.id.0).expect("read visible canvas");
assert_eq!(format, eframe::wgpu::TextureFormat::Rgba8Unorm);
assert_eq!(size, [doc.size.width, doc.size.height]);
// Compare with a CPU composite encoded the way the canvas stores it. Uploading
// that reference just to read it back again also rebuilt its entire mip chain.
for &region in regions {
assert!(doc.bounds().contains_rect(&region));
let reference = photocraft_compose::render(&doc, region);
let expected = gpu_canvas::display_texels(format, display.as_deref(), &reference);
let expected = expected.as_chunks::<4>().0;
for y in region.y0..region.y1 {
for x in region.x0..region.x1 {
let actual = shown[y as usize * size[0] as usize + x as usize];
let index = ((y - region.y0) as usize) * region.width() as usize + (x - region.x0) as usize;
for (channel, (actual, expected)) in actual.into_iter().zip(expected[index]).enumerate() {
assert!((actual - f32::from(expected) / 255.0).abs() <= 2.0 / 255.0, "preview differs at ({x},{y}) channel {channel}");
}
}
}
}
@@ -169,7 +176,7 @@ fn check_blend_viewport(cpu_fallback: bool, multiple_layers: bool) {
assert!(ensure_gpu(&mut app, &ctx, 0, visible, None));
println!("BLEND_VIEWPORT kind={} pixels={} expected={} adapter={:?}", app.perf.last_refresh, app.perf.last_refresh_px, 128 * 128, app.perf.gpu_info);
assert_eq!(app.perf.last_refresh_px, 128 * 128, "hover must not recomposite offscreen pixels of a full-canvas layer");
assert_visible_pixels(&mut app, visible);
assert_visible_pixels(&mut app, &[visible]);
app.perf.last_refresh_px = 0;
assert!(ensure_gpu(&mut app, &ctx, 0, visible, None));
assert_eq!(app.perf.last_refresh_px, 0, "unchanged view and mode must reuse the texture");
@@ -184,8 +191,7 @@ fn check_blend_viewport(cpu_fallback: bool, multiple_layers: bool) {
assert_eq!(app.perf.last_refresh_px, 128 * 128);
assert!(ensure_gpu(&mut app, &ctx, 0, visible, None));
assert_eq!(app.perf.last_refresh_px, 128 * 128, "returning view must show the current mode");
assert_visible_pixels(&mut app, visible);
assert_visible_pixels(&mut app, panned);
assert_visible_pixels(&mut app, &[visible, panned]);
crate::blend_preview::hover(&mut app, &layers, None);
assert!(ensure_gpu(&mut app, &ctx, 0, visible, None));
assert_eq!(gpu.read_texels(id.0).unwrap(), original, "cancel must restore the whole texture, including previously previewed offscreen pixels");
@@ -194,11 +200,11 @@ fn check_blend_viewport(cpu_fallback: bool, multiple_layers: bool) {
app.run("layer.setProps", json!({"layer":layer,"blend":"Multiply"})).unwrap();
assert!(ensure_gpu(&mut app, &ctx, 0, visible, None));
let bounds = app.session.active().unwrap().doc.bounds();
assert_visible_pixels(&mut app, bounds);
assert_visible_pixels(&mut app, &[bounds]);
app.run("file.new", json!({"width":512,"height":384,"background":"white"})).unwrap();
assert!(ensure_gpu(&mut app, &ctx, 1, visible, None));
app.session.set_active(0);
app.sync_views();
assert!(ensure_gpu(&mut app, &ctx, 0, visible, None));
assert_visible_pixels(&mut app, bounds);
assert_visible_pixels(&mut app, &[bounds]);
}
@@ -39,22 +39,29 @@ fn harness() -> Option<Harness> {
}
}
fn control(h: &mut Harness, method: &str, params: Value) -> Value {
fn request(h: &mut Harness, method: &str, params: Value) -> Value {
let ctx = h.ctx.clone();
let (req, _rx) = ControlRequest::new(method, params);
let Reply::Done(value) = handle(h.state_mut(), &ctx, &req) else { panic!("{method} did not finish") };
assert_eq!(value["ok"], true, "{method}: {value}");
h.run_steps(4);
value["result"].clone()
}
fn control(h: &mut Harness, method: &str, params: Value) -> Value {
let result = request(h, method, params);
h.run_steps(4);
result
}
fn setup(h: &mut Harness, width: u32, height: u32, zoom: f32, center: [f32; 2]) {
// Settle once after the document is filled and positioned. The fixture does not
// need a canvas frame for the old tab, the empty editor or the temporary white fill.
while h.state().session.active().is_some() {
control(h, "engine.execute", json!({"command":"file.close","params":{"discard":true}}));
request(h, "engine.execute", json!({"command":"file.close","params":{"discard":true}}));
}
control(h, "engine.execute", json!({"command":"prefs.set","params":{"path":"interface.language","value":"en"}}));
control(h, "engine.execute", json!({"command":"file.new","params":{"width":width,"height":height,"background":"white"}}));
control(h, "engine.execute", json!({"command":"edit.fill","params":{"color":"#2266dd"}}));
request(h, "engine.execute", json!({"command":"prefs.set","params":{"path":"interface.language","value":"en"}}));
request(h, "engine.execute", json!({"command":"file.new","params":{"width":width,"height":height,"background":"white"}}));
request(h, "engine.execute", json!({"command":"edit.fill","params":{"color":"#2266dd"}}));
control(h, "ui.set", json!({"zoom":zoom,"center":center}));
assert!(h.state().perf.gpu, "the fixture must use the GPU canvas");
}
@@ -86,26 +86,20 @@ fn assert_document_size_is_physical(gpu: bool) {
let Some(mut h) = harness(gpu) else { return };
h.run_steps(4);
const DOC: u32 = 60;
let app = h.state_mut();
app.run("file.new", json!({"width": DOC, "height": DOC, "background": "white"})).expect("new");
app.run("edit.fill", json!({"color": "#000000"})).expect("fill");
app.sync_views();
// A fractional and an integer UI scale: the canvas sees the scale only as `pixels_per_point` (device px per doc px
// = zoom x ppp), and its sampling branches on that product being below 1, whole (or 2 and up) or fractional
// (`gpu_canvas::callback::filter_mode`). 125% gives 1.25 and 2.5 at the two zooms, as 150% would; 200% gives 2 and
// 4, covering 100%'s 1 and 2, where a dropped ppp could not show anyway (#15652).
for ui_scale in ["125", "200"] {
{
let app = h.state_mut();
while app.session.active().is_some() {
app.run("file.close", json!({"discard": true})).expect("close");
}
app.run("prefs.set", json!({"values": {"interface.uiScale": ui_scale}})).expect("ui scale");
app.run("file.new", json!({"width": DOC, "height": DOC, "background": "white"})).expect("new");
app.run("edit.fill", json!({"color": "#000000"})).expect("fill");
app.sync_views();
}
h.state_mut().run("prefs.set", json!({"values": {"interface.uiScale": ui_scale}})).expect("ui scale");
h.run_steps(6);
assert_eq!(h.state().perf.gpu, gpu, "UI Scale {ui_scale}: expected the {} canvas", if gpu { "GPU" } else { "CPU" });
for zoom in [1.0f32, 2.0] {
control(&mut h, "ui.set", json!({"zoom": zoom, "center": [DOC as f32 / 2.0, DOC as f32 / 2.0]}));
h.run_steps(4);
let (w, hgt) = document_physical_size(&mut h);
let want = (DOC as f32 * zoom).round() as i64;
assert!(