From 383a5aa87f118b9079916f8ee07e50cd80e73df3 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 5 Dec 2026 05:04:37 -0800 Subject: [PATCH] Effects: a whole-object raster draws again only after a pan by whole pixels within rounding A raster of the whole object kept at a lower resolution now draws after a pan only when the pan is whole to within floating-point rounding (a tolerance scaled to the translation, between 1e-9 and 1e-6 px per axis, and never looser than the 1e-6 px check rasters cropped to the view use). A width-capped raster panned vertically by 40.0000001 or 39.9999999 px keeps its scale, size and rounded origin, so the looser check drew it on a shifted sampling grid; it is now filtered again, and a pan of exactly 40 px still draws the kept raster. --- crates/render/src/fx/pixels.rs | 6 +++--- crates/render/src/fx/raster.rs | 14 +++++++++++++- crates/render/src/fx/tests.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/crates/render/src/fx/pixels.rs b/crates/render/src/fx/pixels.rs index 4789dc83b5..9a04d4d62b 100644 --- a/crates/render/src/fx/pixels.rs +++ b/crates/render/src/fx/pixels.rs @@ -4,7 +4,7 @@ use super::entries::{Content, DocInputs, Drawn, FX_RASTER_BUDGET, Inputs, ItemGe use super::raster::RASTERIZED; use super::raster::{ DRAG_PIXEL_BUDGET, PIXEL_BUDGET, PixelEntry, ViewCrop, content_changing, doc_raster_ppi, drag_scale, draw_placed, draw_scaled, filter_region, - filter_region_within, hard_edged, image_size, linear_key, looks_around, pan_margin, panned_whole, + filter_region_within, hard_edged, image_size, linear_key, looks_around, pan_margin, panned_exactly_whole, }; use super::{ Affine, AppearanceItem, Arc, BezPath, Channels, DefaultHasher, Document, FillRule, Frame, GeomContext, Hash, Hasher, Ink, Node, PixelFx, @@ -206,9 +206,9 @@ impl Renderer { None => e.pan.map_or(whole, |(pan, output_pan, output_linear)| { let p = f.view * e.anchor; entire - && panned_whole(pan, f.view) + && panned_exactly_whole(pan, f.view) && output_linear == linear_key(f.output) - && panned_whole(output_pan, f.output) + && panned_exactly_whole(output_pan, f.output) && e.k.to_bits() == k.to_bits() && f64::from(e.image.width()) == w && f64::from(e.image.height()) == h diff --git a/crates/render/src/fx/raster.rs b/crates/render/src/fx/raster.rs index 89cb9046eb..1f036c734e 100644 --- a/crates/render/src/fx/raster.rs +++ b/crates/render/src/fx/raster.rs @@ -104,7 +104,7 @@ pub(crate) struct PixelEntry { pub(super) valid: Option, /// For a raster of the content's whole reach made at a lower resolution than the view's, the /// pans of the view and output it was made through, and the output's linear coefficients: it - /// draws when both grids pan by whole pixels ([`panned_whole`]), the output's linear + /// draws when both grids pan by whole pixels ([`panned_exactly_whole`]), the output's linear /// coefficients stay the same, its rounded origin matches a fresh render's floored origin, /// and its scale and size match a fresh render's. `None` for other rasters. pub(super) pan: Option<(Vec2, Vec2, [u64; 4])>, @@ -146,6 +146,18 @@ pub(super) fn panned_whole(pan: Vec2, view: Affine) -> bool { (d - Vec2::new(d.x.round(), d.y.round())).hypot() < 1e-6 } +/// Is `view` panned by whole device pixels from `pan`, allowing floating-point rounding +/// with a tolerance between 1e-9 and 1e-6 device pixels per axis and a combined error +/// below 1e-6 device pixels, as required by [`panned_whole`]? +pub(super) fn panned_exactly_whole(pan: Vec2, view: Affine) -> bool { + let at = view.translation(); + let whole = |a: f64, b: f64| { + let d = b - a; + (d - d.round()).abs() <= (f64::EPSILON * 64.0 * (a.abs() + b.abs())).clamp(1e-9, 1e-6) + }; + panned_whole(pan, view) && whole(pan.x, at.x) && whole(pan.y, at.y) +} + /// The four linear coefficients of `view` (zoom and rotation, not the pan), as cache keys. pub(crate) fn linear_key(view: Affine) -> [u64; 4] { let c = view.as_coeffs(); diff --git a/crates/render/src/fx/tests.rs b/crates/render/src/fx/tests.rs index b3e1eaa546..8c1e327215 100644 --- a/crates/render/src/fx/tests.rs +++ b/crates/render/src/fx/tests.rs @@ -2025,6 +2025,33 @@ fn a_pan_by_part_of_a_pixel_filters_a_width_capped_whole_object_raster_again() { } } +/// A width-capped whole-object raster draws after a whole-pixel pan, but filters again when the +/// pan differs from whole pixels by more than floating-point rounding. +#[test] +fn a_pan_near_a_whole_pixel_filters_a_width_capped_whole_object_raster_again() { + let d = doc_with(Rect::new(100.0, 100.0, 2100.0, 101.0), Color::rgb(0.9, 0.4, 0.1), vec![("sketch.tornEdges", json!({}))]); + let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() }; + let view = |y: f64| Affine::translate((640.0, y)) * Affine::scale(64.0) * Affine::translate((-1100.0, -100.5)); + let drawn = |r: &Renderer, image: &Arc| r.pixel_fx.values().any(|e| Arc::ptr_eq(&e.image, image)); + for start in [400.1, 400.3] { + for pan in [40.0000001, 39.9999999, 40.0] { + let mut r = Renderer::new(); + let _ = r.render(&d, 1280, 800, view(start), &opts); + let kept = r.pixel_fx.values().next().unwrap(); + assert!(kept.k < 1.0 && kept.view.is_none(), "a raster of the whole object at a lower resolution"); + assert_eq!(u32::from(kept.image.width()), crate::MAX_RASTER_SIDE, "the raster is as wide as a raster can be"); + let image = kept.image.clone(); + let pixels = r.render(&d, 1280, 800, view(start + pan), &opts).pixels; + if pan == 40.0 { + assert!(drawn(&r, &image), "a whole-pixel pan of the width-capped raster draws the kept raster"); + } else { + assert!(!drawn(&r, &image), "a pan within 1e-6 px of whole but off the grid filters the content again"); + } + assert!(pixels == Renderer::new().render(&d, 1280, 800, view(start + pan), &opts).pixels, "after a pan of {pan} px from {start}"); + } + } +} + /// Hiding a member of a group filters its whole-object raster again after a pan by whole pixels. #[test] fn hiding_a_member_filters_the_whole_object_raster_again_after_a_pan() {