Effects: a whole-object raster keeps its origin across whole-pixel pans of a centered view

The canvas pans by moving the view's center, which leaves rounding error
in the view's translation (for example -13680.000000000002). Flooring
the raster's left or top edge then put the origin one pixel lower, the
width or height grew by a pixel, the scale changed, and the whole object
was filtered again. The origin is now rounded to 2^-20 device pixels,
ties to even, before it is floored, as the sizes already were.

pans_of_a_centered_view_draw_the_whole_object_raster_again now pans
across and down in turn at scales 56, 72, 96 and 120, with centers on
whole points too.
This commit is contained in:
dotCooCoo committed 2026-12-05 10:48:05 -08:00
1 parent 88563bc655
commit 901f78bedd
2 files changed
+24 -7

No files matched your search

+8 -4
View File
@@ -160,10 +160,14 @@ impl Renderer {
Some(m) if !fits => full.intersect(room.inflate(m, m)),
_ => full,
};
let (x0, y0) = (region.x0.floor(), region.y0.floor());
// Sizes rounded to 2^-20 device pixels keep raster dimensions and filter scales stable across whole-pixel pans.
let size = |v: f64| v.trunc() + (v.fract() * 1_048_576.0).round() / 1_048_576.0;
let (rw, rh) = (size((region.x1 - x0).max(1.0)), size((region.y1 - y0).max(1.0)));
// The origin and the sizes, rounded to 2^-20 device pixels, keep the raster's place, dimensions
// and filter scale stable across whole-pixel pans that leave rounding error in the view. Origin
// ties round to even steps, so whole-pixel shifts round alike across zero. Origins of 2^32 pixels
// or more in magnitude are kept as they are. Size fractions round ties away from zero.
let snap_origin = |v: f64| if v.abs() < 4_294_967_296.0 { (v * 1_048_576.0).round_ties_even() / 1_048_576.0 } else { v };
let round_size = |v: f64| v.trunc() + (v.fract() * 1_048_576.0).round() / 1_048_576.0;
let (x0, y0) = (snap_origin(region.x0).floor(), snap_origin(region.y0).floor());
let (rw, rh) = (round_size((region.x1 - x0).max(1.0)), round_size((region.y1 - y0).max(1.0)));
// (vello_cpu panics drawing near `u16::MAX` pixels a side.)
let side = f64::from(crate::MAX_RASTER_SIDE);
let k = (PIXEL_BUDGET / (rw * rh)).sqrt().min(side / rw).min(side / rh).min(1.0);
+16 -3
View File
@@ -2044,14 +2044,23 @@ fn a_pan_by_part_of_a_pixel_filters_a_width_capped_whole_object_raster_again() {
}
/// The canvas centers its view on a document point and pans by moving that point by the pan's
/// device pixels over the scale. At 9600% and 5000% each whole-pixel pan keeps the raster's scale
/// device pixels over the scale, here across and down in turn. At scales from 50 to 120, with
/// centers on whole points and between them, each whole-pixel pan keeps the raster's origin, scale
/// and size bit-identical to a fresh render's and draws the kept raster.
#[test]
fn pans_of_a_centered_view_draw_the_whole_object_raster_again() {
let d = doc_with(Rect::new(100.0, 100.0, 300.0, 250.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 = |scale: f64, c: Point| Affine::translate((640.0, 400.0)) * Affine::scale(scale) * Affine::translate((-c.x, -c.y));
for (scale, c) in [(96.0, Point::new(295.3, 175.7)), (96.0, Point::new(291.137, 171.913)), (50.0, Point::new(291.137, 171.913))] {
for (scale, c) in [
(96.0, Point::new(295.3, 175.7)),
(96.0, Point::new(291.137, 171.913)),
(96.0, Point::new(150.0, 120.0)),
(72.0, Point::new(200.0, 175.0)),
(120.0, Point::new(295.3, 175.7)),
(56.0, Point::new(150.0, 120.0)),
(50.0, Point::new(291.137, 171.913)),
] {
let mut r = Renderer::new();
let _ = r.render(&d, 1280, 800, view(scale, c), &opts);
let kept = r.pixel_fx.values().next().unwrap();
@@ -2059,7 +2068,11 @@ fn pans_of_a_centered_view_draw_the_whole_object_raster_again() {
let image = kept.image.clone();
let mut at = c;
for i in 1..=4 {
at.x -= 40.0 / scale;
if i % 2 == 1 {
at.x -= 40.0 / scale;
} else {
at.y -= 40.0 / scale;
}
let pixels = r.render(&d, 1280, 800, view(scale, at), &opts).pixels;
assert!(r.pixel_fx.values().any(|e| Arc::ptr_eq(&e.image, &image)), "pan {i} of 40 px at scale {scale} from {c:?} draws the kept raster");
let mut fresh = Renderer::new();