From 67f8cdb20241cf41150eaabe77a6abfec9f88865 Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 22:27:43 +0100 Subject: [PATCH] Fill scripts: tiles clear at a pixel do not use up the layer limit (#17429) A tile that covers a pixel but is transparent there counted toward the 1024-layer cap, so with a mostly clear pattern at the largest overlap the cap ran out before the tiles that show at that pixel were reached and the pixel stayed empty. Only visible samples count now; a separate, larger visit cap keeps the work per pixel bounded. Applies to Brick Fill and Cross Weave alike. Co-Authored-By: Claude Opus 5.5 --- crates/compose/src/scripted_pattern.rs | 29 ++++++++++++++----- .../src/fill_script_cross_weave_tests.rs | 20 +++++++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/crates/compose/src/scripted_pattern.rs b/crates/compose/src/scripted_pattern.rs index 2e40e1da93..a0263160e2 100644 --- a/crates/compose/src/scripted_pattern.rs +++ b/crates/compose/src/scripted_pattern.rs @@ -156,9 +156,14 @@ pub struct BrickFill { } /// Most tiles composited into one pixel, front to back: bounds the work of extreme overlaps. Only a pixel whose -/// frontmost tiles all let some of it through ever reaches it; an opaque tile ends the pixel at once. +/// frontmost tiles all let some of it through ever reaches it; an opaque tile ends the pixel at once. A tile that is +/// transparent at the pixel does not count, so it cannot use up the limit before the tiles that show there. const MAX_LAYERS: usize = 1024; +/// Most tiles looked at for one pixel, transparent there or not: the work bound when many overlapping tiles are clear +/// at that pixel (a mostly transparent pattern at the largest overlap). +const MAX_VISITS: usize = 16 * MAX_LAYERS; + /// A repeatable value for tile `(row, col)` from `seed`, independent of the colour drift. fn tile_bits(seed: u64, row: i64, col: i64) -> u64 { let mut z = (seed ^ 0xD6E8_FEB8_6659_FD93) @@ -246,7 +251,7 @@ impl BrickFill { for x in rect.x0..rect.x1 { let px = f64::from(x) + 0.5; let mut acc = [0.0f32; 4]; - let mut layers = 0; + let (mut layers, mut visits) = (0, 0); 'rows: for row in (low_row..=top_row).rev() { // Only the tiles of this row whose centre lies in the turned rectangle around the pixel cover it. let Some((lo, hi)) = span(&corners, row as f64 * pitch_y + th / 2.0 - py) else { continue }; @@ -254,7 +259,7 @@ impl BrickFill { let right = ((px + hi + row_shift - tw / 2.0) / pitch_x).floor() as i64; let left = ((px + lo + row_shift - tw / 2.0) / pitch_x).ceil() as i64; for col in (left..=right).rev() { - if layers >= MAX_LAYERS || acc[3] >= 1.0 { + if layers >= MAX_LAYERS || visits >= MAX_VISITS || acc[3] >= 1.0 { break 'rows; } let place = placement(row, col); @@ -262,8 +267,13 @@ impl BrickFill { if !(0.0..f64::from(width)).contains(&u) || !(0.0..f64::from(height)).contains(&v) { continue; } + visits += 1; + let q = tile.sample_area(&place, px, py); + if q[3] <= 0.0 { + continue; + } layers += 1; - blend_behind(&mut acc, tile.sample_area(&place, px, py), self.jitter.offsets(row, col)); + blend_behind(&mut acc, q, self.jitter.offsets(row, col)); } } out[i] = finish(acc); @@ -331,10 +341,10 @@ impl CrossWeave { let right = (px / pitch).floor() as i64; let left = ((px - 2.0 * reach) / pitch).ceil() as i64; let mut acc = [0.0f32; 4]; - let mut layers = 0; + let (mut layers, mut visits) = (0, 0); 'rows: for row in (low_row..=top_row).rev() { for col in (left..=right).rev() { - if layers >= MAX_LAYERS || acc[3] >= 1.0 { + if layers >= MAX_LAYERS || visits >= MAX_VISITS || acc[3] >= 1.0 { break 'rows; } // A flat tile reaches half its width across and half its height down; an upright one the other way. @@ -347,8 +357,13 @@ impl CrossWeave { if !(0.0..f64::from(width)).contains(&u) || !(0.0..f64::from(height)).contains(&v) { continue; } + visits += 1; + let q = tile.sample_area(&place, px, py); + if q[3] <= 0.0 { + continue; + } layers += 1; - blend_behind(&mut acc, tile.sample_area(&place, px, py), self.jitter.offsets(row, col)); + blend_behind(&mut acc, q, self.jitter.offsets(row, col)); } } out[i] = finish(acc); diff --git a/crates/engine-paint/src/fill_script_cross_weave_tests.rs b/crates/engine-paint/src/fill_script_cross_weave_tests.rs index ae63a4e309..6eb091b61d 100644 --- a/crates/engine-paint/src/fill_script_cross_weave_tests.rs +++ b/crates/engine-paint/src/fill_script_cross_weave_tests.rs @@ -255,3 +255,23 @@ fn cross_weave_runs_in_every_mode_and_depth() { assert_eq!(undo_label(&s).as_deref(), Some("Fill"), "{mode}/{depth}"); } } + +#[test] +fn tiles_clear_at_a_pixel_do_not_hide_the_ones_that_show_there() { + // A clear 100 x 100 tile with an opaque 10 x 10 middle, at the largest overlap (2 px between tiles): about 1100 + // tiles in front are clear at (0, 0) before the ones whose middle covers it. + let mut s = crate::session(); + s.execute("file.new", json!({"width": 100, "height": 100, "background": "transparent"})).unwrap(); + s.execute("select.rect", json!({"x": 45, "y": 45, "width": 10, "height": 10})).unwrap(); + s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); + s.execute("edit.definePattern", json!({"name": "dot", "rect": [0, 0, 100, 100]})).unwrap(); + s.execute("file.new", json!({"width": 40, "height": 40, "background": "white"})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + // Brick Fill lays its tiles out the same way and must find them too. + for script in ["crossWeave", "brickFill"] { + s.execute("edit.fill", weave(json!({"script": script, "pattern": "dot", "spacing": -98}))).unwrap(); + let px = pixel(&s, 0, 0); + assert!(px[3] > 0.99 && rgb8(px) == RED, "{script}: {px:?}"); + s.execute("edit.undo", json!({})).unwrap(); + } +}