Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

perf(compose): select Normal RGB kernel once per buffer (#3715)

Streetblocke156a9ed8582
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index a7c057610..01087f493 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -1674,6 +1674,18 @@ fn blend_into_fill(backdrop: &mut Buffer, src: &Buffer, mode: BlendMode, opacity
/// [`blend_into`] mixing coverage in a `gamma` space (type layers).
fn blend_into_g(backdrop: &mut Buffer, src: &Buffer, mode: BlendMode, opacity: f32, gamma: f32) {
+ // The blend space is constant in this loop. Some TLS implementations synchronise even a
+ // boolean read, so select the common Normal kernel once instead of once per pixel.
+ if mode == BlendMode::Normal && gamma == 1.0 && !psblend::LAB_MIX.with(|l| l.get()) {
+ for (i, b) in backdrop.px.iter_mut().enumerate() {
+ let s = src.px[i];
+ if s[3] <= 0.0 {
+ continue;
+ }
+ *b = psblend::composite_normal_rgb(*b, s, opacity);
+ }
+ return;
+ }
let rect = backdrop.rect;
let w = rect.width() as i32;
for (i, b) in backdrop.px.iter_mut().enumerate() {
diff --git a/crates/compose/src/psblend.rs b/crates/compose/src/psblend.rs
index afd841234..4cff6f308 100644
--- a/crates/compose/src/psblend.rs
+++ b/crates/compose/src/psblend.rs
@@ -210,13 +210,7 @@ pub fn composite(mode: BlendMode, backdrop: [f32; 4], source: [f32; 4], opacity:
return [r[0], r[1], r[2], ao];
}
if mode == BlendMode::Normal {
- // Fast path: B(Cb, Cs) = Cs.
- if ao <= 0.0 {
- return [0.0; 4];
- }
- let kb = ab * (1.0 - as_);
- let inv = 1.0 / ao;
- return [(kb * backdrop[0] + as_ * source[0]) * inv, (kb * backdrop[1] + as_ * source[1]) * inv, (kb * backdrop[2] + as_ * source[2]) * inv, ao];
+ return normal_rgb(backdrop, source, as_, ao);
}
let cb = [backdrop[0], backdrop[1], backdrop[2]];
let cs = [source[0], source[1], source[2]];
@@ -232,6 +226,29 @@ pub fn composite(mode: BlendMode, backdrop: [f32; 4], source: [f32; 4], opacity:
out
}
+/// Normal mixing outside Lab, for callers that select the blend space once per buffer.
+#[inline]
+pub(crate) fn composite_normal_rgb(backdrop: [f32; 4], source: [f32; 4], opacity: f32) -> [f32; 4] {
+ let ab = backdrop[3];
+ let as_ = source[3] * opacity;
+ if as_ <= 0.0 {
+ return backdrop;
+ }
+ let ao = as_ + ab * (1.0 - as_);
+ normal_rgb(backdrop, source, as_, ao)
+}
+
+#[inline]
+fn normal_rgb(backdrop: [f32; 4], source: [f32; 4], as_: f32, ao: f32) -> [f32; 4] {
+ // B(Cb, Cs) = Cs. Keep the scalar composite's operation order and straight alpha.
+ if ao <= 0.0 {
+ return [0.0; 4];
+ }
+ let kb = backdrop[3] * (1.0 - as_);
+ let inv = 1.0 / ao;
+ [(kb * backdrop[0] + as_ * source[0]) * inv, (kb * backdrop[1] + as_ * source[1]) * inv, (kb * backdrop[2] + as_ * source[2]) * inv, ao]
+}
+
/// Photoshop's "Blend Text Colors Using Gamma" (Color Settings › Advanced, on at 1.45 by
/// default): type layers mix their colour with the backdrop in linear light raised to 1 / 1.45
/// ([`text_encode`]), so anti-aliased glyph edges come out lighter over dark backdrops than a
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index c56b7de49..869c316dd 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -40,6 +40,147 @@ fn normal_layer_over_background() {
assert!(close4(px(&d, 5, 1), [1.0; 4]));
}
+// The pre-optimisation Normal formula, independent of the production helper. Its operation
+// order matters: an algebraically equivalent premultiplied mix can round differently.
+fn original_normal_rgb(backdrop: [f32; 4], source: [f32; 4], opacity: f32) -> [f32; 4] {
+ let ab = backdrop[3];
+ let as_ = source[3] * opacity;
+ if as_ <= 0.0 {
+ return backdrop;
+ }
+ let ao = as_ + ab * (1.0 - as_);
+ if ao <= 0.0 {
+ return [0.0; 4];
+ }
+ let kb = ab * (1.0 - as_);
+ let inv = 1.0 / ao;
+ [(kb * backdrop[0] + as_ * source[0]) * inv, (kb * backdrop[1] + as_ * source[1]) * inv, (kb * backdrop[2] + as_ * source[2]) * inv, ao]
+}
+
+#[test]
+fn normal_rgb_kernel_preserves_scalar_bits() {
+ let subnormal = f32::from_bits(1);
+ let backdrops = [[0.0, -0.0, 2.0, 0.0], [0.2, 0.4, 0.6, 0.5], [2.0, -0.25, 4.0, 1.0], [-1.0, 0.01, 1.0, -0.5], [1.0, -0.0, f32::MIN_POSITIVE, subnormal]];
+ let sources = [
+ [0.9, 0.1, 0.3, 1.0],
+ [0.25, 2.0, -1.0, 0.5],
+ [subnormal, 1e-20, 1.0, subnormal],
+ [0.5, 0.2, 0.7, -0.0],
+ [0.5, 0.2, 0.7, -0.5],
+ [f32::NAN, 0.2, 0.7, 0.0],
+ ];
+ for b in backdrops {
+ for s in sources {
+ for opacity in [-1.0, -0.0, 0.0, 0.5, 1.0, 2.0] {
+ let want = original_normal_rgb(b, s, opacity);
+ let got = psblend::composite_normal_rgb(b, s, opacity);
+ assert_eq!(got.map(f32::to_bits), want.map(f32::to_bits), "{b:?} / {s:?} / {opacity}");
+ }
+ }
+ }
+}
+
+fn normal_rgb_test_buffers(fmt: PixelFormat) -> (Buffer, Buffer) {
+ let rect = Rect::new(-3, -2, 5, 0);
+ let mut bottom = Surface::new(fmt);
+ let mut top = Surface::new(fmt);
+ for y in rect.y0..rect.y1 {
+ for x in rect.x0..rect.x1 {
+ let i = ((y - rect.y0) * rect.width() as i32 + x - rect.x0) as usize;
+ let t = i as f32 / 16.0;
+ let b = [2.0 * t, 0.4, -0.25 + t, [0.0, 0.25, 0.75, 1.0][i % 4]];
+ let s = [0.9 - t, 0.1 + t, 0.3, [0.0, -0.0, 0.5, 1.0][i % 4]];
+ bottom.write_pixel(x, y, &photocraft_raster::from_rgba(&fmt, b));
+ top.write_pixel(x, y, &photocraft_raster::from_rgba(&fmt, s));
+ }
+ }
+ (surface_to_buffer(&bottom, rect), surface_to_buffer(&top, rect))
+}
+
+// Keep the old buffer dispatch as an oracle for the specialised loop and all of its fallbacks.
+fn original_blend_into_g(backdrop: &mut Buffer, src: &Buffer, mode: BlendMode, opacity: f32, gamma: f32) {
+ let rect = backdrop.rect;
+ let w = rect.width() as i32;
+ for (i, b) in backdrop.px.iter_mut().enumerate() {
+ let mut s = src.px[i];
+ if s[3] <= 0.0 {
+ continue;
+ }
+ let mut mode = mode;
+ if mode == BlendMode::Dissolve {
+ let x = rect.x0 + (i as i32 % w);
+ let y = rect.y0 + (i as i32 / w);
+ s[3] = if dissolve_noise(x, y) < s[3] * opacity { 1.0 } else { 0.0 };
+ mode = BlendMode::Normal;
+ *b = blend::composite(mode, *b, s, 1.0);
+ continue;
+ }
+ *b = blend::composite_gamma(mode, *b, s, opacity, gamma);
+ }
+}
+
+fn assert_buffer_bits(got: &Buffer, want: &Buffer) {
+ assert_eq!(got.rect, want.rect);
+ assert_eq!(got.px.len(), want.px.len());
+ for (i, (got, want)) in got.px.iter().zip(&want.px).enumerate() {
+ assert_eq!(got.map(f32::to_bits), want.map(f32::to_bits), "pixel {i}: {got:?} vs {want:?}");
+ }
+}
+
+#[test]
+fn normal_rgb_buffer_dispatch_and_fallbacks_preserve_bits() {
+ for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] {
+ let (bottom, top) = normal_rgb_test_buffers(fmt);
+ for lab in [false, true] {
+ psblend::with_lab_mix(lab, || {
+ psblend::with_hdr(fmt.sample == SampleType::F32, || {
+ for mode in BlendMode::LAYER_MODES {
+ for gamma in [1.0, psblend::TEXT_GAMMA] {
+ for opacity in [0.0, 0.5, 1.0] {
+ let mut got = bottom.clone();
+ let mut want = bottom.clone();
+ original_blend_into_g(&mut want, &top, mode, opacity, gamma);
+ blend_into_g(&mut got, &top, mode, opacity, gamma);
+ assert_buffer_bits(&got, &want);
+ }
+ }
+ }
+ });
+ });
+ }
+ }
+}
+
+#[test]
+fn normal_rgb_buffer_preserves_alpha_skip_and_nested_scopes() {
+ let rect = Rect::new(-1, -1, 2, 0);
+ let bottom = Buffer::filled(rect, [-0.0, 0.4, 2.0, 0.75]);
+ let top = Buffer { rect, px: vec![[f32::NAN, 0.3, 0.1, -0.5], [0.2, 0.3, 0.1, -0.0], [0.2, 0.3, 0.1, f32::NAN]] };
+ psblend::with_lab_mix(true, || {
+ psblend::with_hdr(true, || {
+ psblend::with_lab_mix(false, || {
+ psblend::with_hdr(false, || {
+ let mut got = bottom.clone();
+ let mut want = bottom.clone();
+ original_blend_into_g(&mut want, &top, BlendMode::Normal, -1.0, 1.0);
+ blend_into_g(&mut got, &top, BlendMode::Normal, -1.0, 1.0);
+ assert_buffer_bits(&got, &want);
+ assert_eq!(got.px[0].map(f32::to_bits), bottom.px[0].map(f32::to_bits));
+ assert_eq!(got.px[1].map(f32::to_bits), bottom.px[1].map(f32::to_bits));
+ });
+ assert!(psblend::HDR.with(|h| h.get()));
+ });
+ assert!(psblend::LAB_MIX.with(|l| l.get()));
+ assert!(psblend::HDR.with(|h| h.get()));
+ let mut got = bottom.clone();
+ let mut want = bottom.clone();
+ original_blend_into_g(&mut want, &top, BlendMode::Normal, 0.5, 1.0);
+ blend_into_g(&mut got, &top, BlendMode::Normal, 0.5, 1.0);
+ assert_buffer_bits(&got, &want);
+ });
+ });
+}
+
#[test]
fn opacity_and_fill_multiply() {
let mut d = doc_white(4, 4);