From dfba0544d1f731c4b2091b89cdb2d17e9ddcf304 Mon Sep 17 00:00:00 2001 From: xsvm <2479667226@qq.com> Date: Tue, 20 Oct 2026 16:45:24 +0800 Subject: [PATCH 1/3] perf(liquify): read and premultiply the full-resolution source in parallel bands The full-resolution preview read the warped region's source and premultiplied it on one thread, about a million pixels at an 800 px brush and most of each update (P30). Each band of rows now reads and premultiplies in one pass on the rayon pool (wasm stays serial); the conversion is per pixel, so the samples are the same. --- crates/algo/src/liquify.rs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/crates/algo/src/liquify.rs b/crates/algo/src/liquify.rs index adbca8680f..e92d03a5a5 100644 --- a/crates/algo/src/liquify.rs +++ b/crates/algo/src/liquify.rs @@ -920,18 +920,30 @@ impl ProxyImage { return false; } source_scratch.resize(source_len, [0.0; 4]); - src.read_rgba_into(src_rect, source_scratch); - if !src.format().alpha { - for pixel in source_scratch.iter_mut() { - pixel[3] = 1.0; + // Read and premultiply the source in one pass per band of rows: at an 800 px brush it is + // about a million pixels, most of an update when read on one thread (P30). + let opaque = !src.format().alpha; + let read_band = |first_row: usize, band: &mut [[f32; 4]]| { + let y = src_rect.y0 + first_row as i32; + src.read_rgba_into(Rect::new(src_rect.x0, y, src_rect.x1, y + (band.len() / sw) as i32), band); + for pixel in band.iter_mut() { + if opaque { + pixel[3] = 1.0; + } + let alpha = pixel[3]; + pixel[0] *= alpha; + pixel[1] *= alpha; + pixel[2] *= alpha; } + }; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + const BAND_ROWS: usize = 32; + source_scratch.par_chunks_mut(sw * BAND_ROWS).enumerate().for_each(|(band, rows)| read_band(band * BAND_ROWS, rows)); } - for pixel in source_scratch.iter_mut() { - let alpha = pixel[3]; - pixel[0] *= alpha; - pixel[1] *= alpha; - pixel[2] *= alpha; - } + #[cfg(target_arch = "wasm32")] + read_band(0, source_scratch); let x0 = (region.x0 - bounds.x0) as usize; let x1 = (region.x1 - bounds.x0) as usize; From 6d96c38c4e6954592effa09402467bde1c9e8c53 Mon Sep 17 00:00:00 2001 From: xsvm <2479667226@qq.com> Date: Tue, 20 Oct 2026 17:23:10 +0800 Subject: [PATCH 2/3] perf(liquify): a large dab computes its field nodes row by row in parallel Since #4047 a stroke dabs every twentieth of the brush radius, four times as often as before, and P30's 800 px update went from 9.6 to 35.9 ms on the perf runner. Each dab walked its nodes (about 125,000 at 800 px) on one thread. A node reads only the snapshot and the field as it was before the dab, and the writes land after, so rows are independent: dabs over 16,384 nodes compute their rows on the rayon pool (wasm stays serial). Same writes, same field. --- crates/algo/src/liquify.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/algo/src/liquify.rs b/crates/algo/src/liquify.rs index e92d03a5a5..198c20c763 100644 --- a/crates/algo/src/liquify.rs +++ b/crates/algo/src/liquify.rs @@ -487,9 +487,11 @@ impl LiquifyField { bilinear(&self.d, self.w, self.h, gx, gy) } }; - let mut writes: Vec<(usize, [f32; 2], f32)> = Vec::with_capacity((i1 - i0 + 1) * (j1 - j0 + 1)); + // Every node reads only the snapshot and the field as it was before this dab (the writes + // land after), so rows are independent: a large brush computes them in parallel. let inv_cell = 1.0 / self.cell; - for j in j0..=j1 { + let node_row = |j: usize| -> Vec<(usize, [f32; 2], f32)> { + let mut row = Vec::with_capacity(i1 - i0 + 1); for i in i0..=i1 { let p = self.node_pos(i, j); let (dx, dy) = (p[0] - c[0], p[1] - c[1]); @@ -503,7 +505,7 @@ impl LiquifyField { if s.tool == LiquifyTool::Freeze || s.tool == LiquifyTool::Thaw { let f = self.freeze[idx]; let v = if s.tool == LiquifyTool::Freeze { (f as f64 + k_raw).min(1.0) } else { (f as f64 - k_raw).max(0.0) }; - writes.push((idx, self.d[idx], v as f32)); + row.push((idx, self.d[idx], v as f32)); continue; } let k = k_raw * (1.0 - f64::from(self.freeze[idx])); @@ -553,10 +555,20 @@ impl LiquifyField { [(q[0] + dq[0] - p[0]) as f32, (q[1] + dq[1] - p[1]) as f32] } }; - writes.push((idx, new, self.freeze[idx])); + row.push((idx, new, self.freeze[idx])); } - } - for (idx, d, f) in writes { + row + }; + #[cfg(not(target_arch = "wasm32"))] + let writes: Vec> = if (i1 - i0 + 1) * (j1 - j0 + 1) > 16_384 { + use rayon::prelude::*; + (j0..=j1).into_par_iter().map(node_row).collect() + } else { + (j0..=j1).map(node_row).collect() + }; + #[cfg(target_arch = "wasm32")] + let writes: Vec> = (j0..=j1).map(node_row).collect(); + for (idx, d, f) in writes.into_iter().flatten() { self.d[idx] = d; self.freeze[idx] = f; } From 3db8cc1b1f60590dd0c6f594e0f812d7506d449f Mon Sep 17 00:00:00 2001 From: xsvm <2479667226@qq.com> Date: Tue, 20 Oct 2026 18:23:19 +0800 Subject: [PATCH 3/3] refactor(liquify): use the crate's par helpers for the dab rows and the source bands par::map and par::chunks_mut already pick rayon on native targets and run serially on wasm, so the two hand-written cfg blocks go; the threshold gets a name. --- crates/algo/src/liquify.rs | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/crates/algo/src/liquify.rs b/crates/algo/src/liquify.rs index 198c20c763..c879b5e76b 100644 --- a/crates/algo/src/liquify.rs +++ b/crates/algo/src/liquify.rs @@ -19,6 +19,7 @@ //! copied untouched. [`ProxyImage::render`] is the cheap preview path: bilinear field and //! bilinear source on a downsampled RGBA copy, re-rendered only where a dab changed things. +use crate::par; use photocraft_geom::Rect; use photocraft_raster::Surface; use serde::{Deserialize, Serialize}; @@ -186,6 +187,10 @@ struct StrokeCursor { carry: f64, } +/// Dabs over this many field nodes compute their rows in parallel (an 800 px brush has about +/// 125,000); smaller ones are not worth the hand-off. +const PARALLEL_DAB_NODES: usize = 16_384; + /// Distance between dabs along a stroke for brush radius `r`: a twentieth of the radius, so a /// drag is many small pushes and its edge stays smooth. fn dab_spacing(r: f64) -> f64 { @@ -559,15 +564,8 @@ impl LiquifyField { } row }; - #[cfg(not(target_arch = "wasm32"))] - let writes: Vec> = if (i1 - i0 + 1) * (j1 - j0 + 1) > 16_384 { - use rayon::prelude::*; - (j0..=j1).into_par_iter().map(node_row).collect() - } else { - (j0..=j1).map(node_row).collect() - }; - #[cfg(target_arch = "wasm32")] - let writes: Vec> = (j0..=j1).map(node_row).collect(); + let rows = j1 - j0 + 1; + let writes = par::map(rows, rows * (i1 - i0 + 1) > PARALLEL_DAB_NODES, |row| node_row(j0 + row)); for (idx, d, f) in writes.into_iter().flatten() { self.d[idx] = d; self.freeze[idx] = f; @@ -932,11 +930,12 @@ impl ProxyImage { return false; } source_scratch.resize(source_len, [0.0; 4]); - // Read and premultiply the source in one pass per band of rows: at an 800 px brush it is - // about a million pixels, most of an update when read on one thread (P30). + // Read and premultiply the source in one pass per band of rows, the bands in parallel: at + // an 800 px brush it is about a million pixels (P30). let opaque = !src.format().alpha; - let read_band = |first_row: usize, band: &mut [[f32; 4]]| { - let y = src_rect.y0 + first_row as i32; + const BAND_ROWS: usize = 32; + par::chunks_mut(source_scratch, sw * BAND_ROWS, true, |band_index, band| { + let y = src_rect.y0 + (band_index * BAND_ROWS) as i32; src.read_rgba_into(Rect::new(src_rect.x0, y, src_rect.x1, y + (band.len() / sw) as i32), band); for pixel in band.iter_mut() { if opaque { @@ -947,15 +946,7 @@ impl ProxyImage { pixel[1] *= alpha; pixel[2] *= alpha; } - }; - #[cfg(not(target_arch = "wasm32"))] - { - use rayon::prelude::*; - const BAND_ROWS: usize = 32; - source_scratch.par_chunks_mut(sw * BAND_ROWS).enumerate().for_each(|(band, rows)| read_band(band * BAND_ROWS, rows)); - } - #[cfg(target_arch = "wasm32")] - read_band(0, source_scratch); + }); let x0 = (region.x0 - bounds.x0) as usize; let x1 = (region.x1 - bounds.x0) as usize;