From c7e82529fe506d2bd4b35c53fa691a0f5dddd4fc Mon Sep 17 00:00:00 2001 From: vangel76 Date: Tue, 3 Nov 2026 19:50:21 +0100 Subject: [PATCH] perf(paint): Mixer sample reads only the dab rectangle (#11133) apply_mixer_dab read and converted the whole stroke working rectangle for every dab when a sample surface was given, which made Sample All Layers strokes quadratic. It now reads fp.rect clipped to the working rectangle, which is all apply_mixer_dab_region reads. Pixels are unchanged. Refs #11133. Co-Authored-By: Claude Sonnet 5.5 --- crates/paint/src/mixer.rs | 13 +++++--- crates/paint/src/mixer/reference_tests.rs | 38 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/crates/paint/src/mixer.rs b/crates/paint/src/mixer.rs index a9a7485fa5..bb34749065 100644 --- a/crates/paint/src/mixer.rs +++ b/crates/paint/src/mixer.rs @@ -190,12 +190,14 @@ pub fn apply_mixer_dab( state: &mut MixerState, lock_transparency: bool, ) { + // Only the dab's own rectangle is ever read from the sample, not the whole stroke rectangle. let sample = sample.filter(|_| m.samples_canvas(&fp.dab)).map(|surface| { - let source = crate::retouch::Region::read(surface, work.rect); + let rect = fp.rect.intersect(&work.rect); + let source = crate::retouch::Region::read(surface, rect); if surface.format().mode == fmt.mode && surface.format().alpha == fmt.alpha { source } else { - let mut native = crate::retouch::Region::new(work.rect, fmt.channels()); + let mut native = crate::retouch::Region::new(rect, fmt.channels()); for (src, dst) in source.data.chunks_exact(source.ch).zip(native.data.chunks_exact_mut(native.ch)) { from_rgba_into(&fmt, to_rgba(&surface.format(), src), dst); } @@ -205,9 +207,10 @@ pub fn apply_mixer_dab( apply_mixer_dab_region(work, fp, fmt, sample.as_ref(), m, state, lock_transparency); } -/// Contiguous native-channel sample variant. `sample` must have the same rectangle and -/// channel layout as `work`. The live compositor supplies it directly, avoiding a tiled -/// temporary surface and a heap allocation for every sampled/deposited pixel. +/// Contiguous native-channel sample variant. `sample` must have the channel layout of `work` and +/// cover at least `fp.rect` clipped to `work.rect` (the only pixels read). The live compositor +/// supplies it directly, avoiding a tiled temporary surface and a heap allocation for every +/// sampled/deposited pixel. pub fn apply_mixer_dab_region( work: &mut crate::retouch::Region, fp: &crate::retouch::Footprint, diff --git a/crates/paint/src/mixer/reference_tests.rs b/crates/paint/src/mixer/reference_tests.rs index 0f6f0a2a82..2071f60f01 100644 --- a/crates/paint/src/mixer/reference_tests.rs +++ b/crates/paint/src/mixer/reference_tests.rs @@ -205,3 +205,41 @@ fn dry_and_wet_transitions_match_frozen_kernel() { } } } + +/// A sample surface is read only under the dab: dabs that overhang the working rectangle or sit in +/// a corner of a much larger one still match the frozen kernel. +#[test] +fn sampled_dabs_clipped_by_the_working_rectangle_match_frozen_kernel() { + use crate::retouch::Region; + use crate::{BrushContext, BrushSettings, Dab}; + use photocraft_color::{PixelFormat, SampleType}; + use photocraft_geom::Point; + + let work_rect = Rect::new(10, 10, 60, 50); + let fmt = PixelFormat::new(photocraft_color::ColorMode::Cmyk, SampleType::U16, true); + let mut surface = Surface::new(fmt); + surface.fill_rect(Rect::new(0, 0, 80, 80), &from_rgba(&fmt, [0.25, 0.5, 0.75, 0.9])); + let mut source = Surface::new(PixelFormat::RGBA32F); + for y in 0..80 { + for x in 0..80 { + source.fill_rect(Rect::new(x, y, x + 1, y + 1), &[x as f32 / 80.0, y as f32 / 80.0, 0.3, 0.8]); + } + } + let ctx = BrushContext::new(&BrushSettings { size: 30.0, ..Default::default() }); + for sample in [&source, &surface] { + let mut actual = Region::read(&surface, work_rect); + let mut expected = actual.clone(); + let mut state = MixerState::default(); + state.load([0.8, 0.2, 0.1, 1.0]); + let mut expected_state = state.clone(); + // Overhanging the left edge, a corner, and the middle. + for (i, (x, y)) in [(8.0, 30.0), (62.0, 52.0), (30.0, 12.0), (35.0, 30.0)].into_iter().enumerate() { + let fp = ctx.footprint(&Dab::round(Point::new(x, y), 12.0, 1.0), i); + let settings = MixerSettings { wet: 0.8, mix: 0.7, ..Default::default() }; + reference(&mut expected, &fp, fmt, Some(sample), &settings, &mut expected_state, false); + apply_mixer_dab(&mut actual, &fp, fmt, Some(sample), &settings, &mut state, false); + assert_eq!(actual, expected, "dab {i}"); + assert_eq!(state, expected_state, "state after dab {i}"); + } + } +}