Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

M5.3: stage cache — slider drags redo only the stages the setting feeds; draft quality during drags

Brandon Thomas42d2298118f5
diff --git a/Cargo.lock b/Cargo.lock
index d8f36eab1..b52843184 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2078,6 +2078,7 @@ name = "lightcraft-engine"
version = "0.1.0"
dependencies = [
"ab_glyph",
+ "libc",
"lightcraft-catalog",
"lightcraft-codecs",
"lightcraft-color",
diff --git a/Cargo.toml b/Cargo.toml
index 86780d7a3..f52003e69 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -37,6 +37,7 @@ proptest = "1"
bytemuck = { version = "1", features = ["derive"] }
crc32fast = "1"
siphasher = "1"
+libc = "0.2" # dev-only: process CPU time in benches
# formats
quick-xml = "0.38"
diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml
index 39a9e2f5f..f075e50bb 100644
--- a/crates/engine/Cargo.toml
+++ b/crates/engine/Cargo.toml
@@ -25,3 +25,6 @@ serde_json = { workspace = true }
thiserror = { workspace = true }
log = { workspace = true }
ab_glyph = { workspace = true }
+
+[dev-dependencies]
+libc = { workspace = true }
diff --git a/crates/engine/examples/render_bench.rs b/crates/engine/examples/render_bench.rs
new file mode 100644
index 000000000..b2bb11ec0
--- /dev/null
+++ b/crates/engine/examples/render_bench.rs
@@ -0,0 +1,143 @@
+//! End-to-end render benchmark (min of N runs, robust to a loaded machine):
+//! `cargo run --release -p lightcraft-engine --example render_bench -- corpus/raw/arw-sony-a7m3-compressed.arw`
+//! (`N=9` runs per scenario, `RAYON_NUM_THREADS=1` for algorithmic comparisons,
+//! `LIGHTCRAFT_PROFILE=1` for per-stage timings).
+//!
+//! Without a file argument a procedural 6000×4000 source is used. Scenarios: a ~2.5 MP loupe render
+//! of the 2560 px preview (cold, and with a warm stage cache while a slider is dragged), a draft,
+//! and a full-size render + JPEG encode (export). Prints minimum wall-clock and minimum process CPU
+//! time: on a shared machine the CPU time shows the work done, wall-clock also the wait for cores.
+use std::time::Instant;
+
+use lightcraft_develop::DevelopSettings;
+use lightcraft_engine::export::{ExportOptions, encode_image};
+use lightcraft_pipeline::{Quality, RenderRequest, SourceInfo, StageCache, render, render_cached};
+use lightcraft_raster::Rgb32f;
+use lightcraft_raster::resample::{Filter, fit};
+
+/// Process CPU time in ms (all threads). The only `unsafe` is this libc clock read, in a dev-only example.
+#[allow(unsafe_code)]
+fn cpu_ms() -> f64 {
+ #[cfg(unix)]
+ {
+ let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 };
+ // SAFETY: `clock_gettime` only writes into the timespec we pass.
+ unsafe { libc::clock_gettime(libc::CLOCK_PROCESS_CPUTIME_ID, &mut ts) };
+ ts.tv_sec as f64 * 1e3 + ts.tv_nsec as f64 / 1e6
+ }
+ #[cfg(not(unix))]
+ {
+ 0.0
+ }
+}
+
+/// Minimum wall-clock and minimum CPU time (ms) over the runs.
+struct T(f64, f64);
+
+impl std::fmt::Display for T {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ write!(f, "{:.1} ms wall, {:.1} ms cpu", self.0, self.1)
+ }
+}
+
+fn best(n: usize, mut f: impl FnMut()) -> T {
+ let mut b = T(f64::MAX, f64::MAX);
+ for _ in 0..n {
+ let (t, c) = (Instant::now(), cpu_ms());
+ f();
+ b.1 = b.1.min(cpu_ms() - c);
+ b.0 = b.0.min(t.elapsed().as_secs_f64() * 1e3);
+ }
+ b
+}
+
+fn typical() -> DevelopSettings {
+ let mut s = DevelopSettings::default();
+ s.light.exposure = 0.3;
+ s.light.highlights = -40.0;
+ s.light.shadows = 30.0;
+ s.effects.clarity = 15.0;
+ s.effects.texture = 10.0;
+ s.effects.dehaze = 10.0;
+ s.detail.nr_luminance = 30.0;
+ s.detail.nr_color = 25.0;
+ s.detail.sharpen_amount = 40.0;
+ s
+}
+
+fn main() {
+ let args: Vec<String> = std::env::args().skip(1).collect();
+ let n: usize = std::env::var("N").ok().and_then(|v| v.parse().ok()).unwrap_or(5);
+ let only = std::env::var("ONLY").unwrap_or_default();
+ let run = |name: &str| only.is_empty() || only.split(',').any(|o| name.contains(o));
+ let (full, info) = match args.first() {
+ Some(path) => {
+ let bytes = std::fs::read(path).expect("read");
+ let t = Instant::now();
+ let r = lightcraft_engine::files::load_bytes(&bytes, usize::MAX).expect("decode");
+ println!("decode full: {:.0} ms", t.elapsed().as_secs_f64() * 1e3);
+ r
+ }
+ None => (
+ Rgb32f::from_fn(6000, 4000, |x, y| {
+ let v = ((x as f32 * 0.011).sin() * (y as f32 * 0.007).cos() * 0.5 + 0.5) * 0.4 + ((x * 31 + y * 17) % 13) as f32 * 0.002;
+ [v, v * 0.9, v * 0.7]
+ }),
+ SourceInfo { raw: true, ..Default::default() },
+ ),
+ };
+ println!("source {}×{} ({:.1} MP), {n} runs each", full.width, full.height, (full.width * full.height) as f64 / 1e6);
+ let preview = std::sync::Arc::new(fit(&full, 2560, 2560, Filter::Box));
+ let s = typical();
+ let view = RenderRequest::fit(1920, 1280);
+ let draft = RenderRequest { max_w: 1152, max_h: 768, quality: Quality::Draft, apply_crop: true };
+
+ if run("cold") {
+ println!("loupe 1920×1280 cold: {}", best(n, || drop(render(&preview, &info, &s, &view))));
+ println!("loupe draft 1152×768 cold: {}", best(n, || drop(render(&preview, &info, &s, &draft))));
+ }
+ let cache = StageCache::default();
+ let mut k = 0.0;
+ if run("drag") {
+ drop(render_cached(&preview, &info, &s, &view, &cache));
+ let ms = best(n, || {
+ let mut t = s.clone();
+ k += 1.0;
+ t.light.contrast = k;
+ t.light.exposure = 0.3 + k * 0.01;
+ drop(render_cached(&preview, &info, &t, &view, &cache));
+ });
+ println!("loupe 1920×1280 exposure drag (warm): {ms}");
+ drop(render_cached(&preview, &info, &s, &draft, &cache));
+ let ms = best(n, || {
+ let mut t = s.clone();
+ k += 1.0;
+ t.light.highlights = -40.0 + k;
+ drop(render_cached(&preview, &info, &t, &draft, &cache));
+ });
+ println!("loupe draft highlights drag (warm): {ms}");
+ let ms = best(n, || {
+ let mut t = s.clone();
+ k += 1.0;
+ t.effects.clarity = 15.0 + k;
+ drop(render_cached(&preview, &info, &t, &draft, &cache));
+ });
+ println!("loupe draft clarity drag (warm): {ms}");
+ let ms = best(n, || {
+ let mut t = s.clone();
+ k += 1.0;
+ t.detail.nr_luminance = 30.0 + k;
+ drop(render_cached(&preview, &info, &t, &draft, &cache));
+ });
+ println!("loupe draft NR drag (warm): {ms}");
+ }
+ if run("export") {
+ let big = RenderRequest::fit(full.width, full.height);
+ let ne = n.min(3);
+ let mut img = None;
+ let ms = best(ne, || img = Some(render(&full, &info, &s, &big).image));
+ println!("export render {}×{}: {ms}", full.width, full.height);
+ let img = img.expect("rendered");
+ println!("export JPEG encode: {}", best(ne, || drop(encode_image(&img, &ExportOptions::default()))));
+ }
+}
diff --git a/crates/engine/src/media.rs b/crates/engine/src/media.rs
index c9cd303f2..0840895af 100644
--- a/crates/engine/src/media.rs
+++ b/crates/engine/src/media.rs
@@ -14,7 +14,7 @@ use std::sync::Arc;
use lightcraft_catalog::{MediaKind, Photo, PhotoId, Source};
use lightcraft_develop::DevelopSettings;
-use lightcraft_pipeline::{RenderRequest, Rendered, SourceInfo};
+use lightcraft_pipeline::{Quality, RenderRequest, Rendered, SourceInfo, StageCache};
use lightcraft_preview::{Hash128, Hasher128, Lru, PreviewCache};
use lightcraft_raster::{Histogram, Rgb32f};
use serde::{Deserialize, Serialize};
@@ -174,6 +174,9 @@ pub struct RenderJob {
pub key: u64,
/// Rendered-thumbnail cache and this job's key in it.
pub cache: Option<(Arc<PreviewCache>, Hash128)>,
+ /// Intermediate results of this view's previous renders (set by the frontend for the loupe):
+ /// slider drags then only redo the stages the changed setting feeds.
+ pub stages: Option<Arc<StageCache>>,
}
pub struct RenderResult {
@@ -186,6 +189,21 @@ pub struct RenderResult {
}
impl RenderJob {
+ /// Render at draft quality (interactive drags). Gets its own result key.
+ pub fn draft(mut self) -> Self {
+ if self.request.quality != Quality::Draft {
+ self.request.quality = Quality::Draft;
+ self.key ^= 0x9e37_79b9_7f4a_7c15;
+ }
+ self
+ }
+
+ /// Reuse `stages` across this view's renders.
+ pub fn with_stages(mut self, stages: Arc<StageCache>) -> Self {
+ self.stages = Some(stages);
+ self
+ }
+
pub fn run(self) -> RenderResult {
if let Some((cache, key)) = &self.cache
&& let Some(img) = cache.get(*key)
@@ -197,7 +215,10 @@ impl RenderJob {
let was_loaded = matches!(self.source, SourceRef::Loaded(_));
match self.source.load() {
Ok(src) => {
- let rendered = lightcraft_pipeline::render(&src, &self.info, &self.settings, &self.request);
+ let rendered = match &self.stages {
+ Some(st) => lightcraft_pipeline::render_cached(&src, &self.info, &self.settings, &self.request, st),
+ None => lightcraft_pipeline::render(&src, &self.info, &self.settings, &self.request),
+ };
if let Some((cache, key)) = &self.cache {
cache.put(*key, Arc::new(rendered.image.clone()));
}
@@ -251,7 +272,7 @@ impl crate::Session {
let level = SourceLevel::for_size(max_w.max(max_h));
let source = self.media.source_ref(&p, level);
let settings = if before { Arc::new(lightcraft_pipeline::before_settings(&p.develop)) } else { p.develop.clone() };
- let request = RenderRequest { max_w, max_h, quality: lightcraft_pipeline::Quality::Full, apply_crop };
+ let request = RenderRequest { max_w, max_h, quality: Quality::Full, apply_crop };
let key = settings.hash64()
^ ((max_w as u64) << 40)
^ ((max_h as u64) << 20)
@@ -261,7 +282,7 @@ impl crate::Session {
let k = Hasher128::new().str(&content_key(&p)).u64(settings.hash64()).u64(b as u64).u64(RENDER_CACHE_VERSION).finish();
(self.media.rendered.clone(), k)
});
- Some(RenderJob { photo: id, level, source, info: source_info(&p), settings, request, key, cache })
+ Some(RenderJob { photo: id, level, source, info: source_info(&p), settings, request, key, cache, stages: None })
}
/// Accept a finished job's loaded source into the cache.
diff --git a/crates/pipeline/src/finish.rs b/crates/pipeline/src/finish.rs
index ff874b8fc..601df578a 100644
--- a/crates/pipeline/src/finish.rs
+++ b/crates/pipeline/src/finish.rs
@@ -9,7 +9,7 @@ use lightcraft_raster::Rgba8;
use crate::colorops::ColorOps;
use crate::geometry::Frame;
-use crate::local::{airlight, log_lum};
+use crate::local::log_lum;
use crate::tone::ToneMap;
use crate::{Prepared, SourceInfo, for_rows};
@@ -119,7 +119,8 @@ pub fn finish(p: &Prepared, s: &DevelopSettings, frame: &Frame, info: &SourceInf
};
let sharpen = (s.detail.sharpen_amount / 150.0) as f32;
let sharpen_mask = (s.detail.sharpen_masking / 100.0) as f32;
- let air = p.dark.as_ref().map(airlight).unwrap_or(1.0);
+ // Planes are pre-exposure: scale the airlight, shift log luminance (see `Prepared`).
+ let (air, air_pre, gain, ev) = (p.air * p.gain, p.air, p.gain, p.ev);
let grain = (s.grain.amount > 0.0 && s.section_enabled("effects")).then(|| {
let cell = (0.0006 + (s.grain.size / 100.0) as f32 * 0.0024) * p.px_per_long as f32;
((s.grain.amount / 100.0) as f32 * 0.13, cell.max(0.6), (s.grain.roughness / 100.0) as f32, s.grain.seed)
@@ -131,8 +132,10 @@ pub fn finish(p: &Prepared, s: &DevelopSettings, frame: &Frame, info: &SourceInf
for_rows(&mut out.data, w, |y, row| {
for (x, px) in row.iter_mut().enumerate() {
let i = y * w + x;
- let mut c = p.img.data[i];
- let l0 = p.log_l.data[i];
+ let raw = p.img.data[i];
+ let mut c = if gain == 1.0 { raw } else { raw.map(|v| v * gain) };
+ let l_pre = p.log_l.data[i];
+ let l0 = l_pre + ev;
// --- local (mask) contributions
let (mut l_exp, mut l_temp, mut l_tint, mut l_con, mut l_hl, mut l_sh, mut l_wh, mut l_bl) = (0.0f32, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
@@ -169,7 +172,7 @@ pub fn finish(p: &Prepared, s: &DevelopSettings, frame: &Frame, info: &SourceInf
if dz != 0.0
&& let Some(dark) = &p.dark
{
- let d = (dark.data[i] / air).clamp(0.0, 1.0);
+ let d = (dark.data[i] / air_pre).clamp(0.0, 1.0);
if dz > 0.0 {
let t = (1.0 - 0.95 * dz.min(1.0) * d).max(0.12);
c = c.map(|v| ((v - air * (1.0 - t)) / t).max(0.0));
@@ -194,7 +197,7 @@ pub fn finish(p: &Prepared, s: &DevelopSettings, frame: &Frame, info: &SourceInf
// --- local tone in log luminance
let l1 = if dz != 0.0 || l_exp != 0.0 { log_lum(c) } else { l0 };
let shift = l1 - l0;
- let base = p.base.data[i] + shift;
+ let base = p.base.data[i] + ev + shift;
let mut delta = 0.0f32;
let (hh, ss) = (hl + l_hl, sh + l_sh);
if hh != 0.0 || ss != 0.0 {
@@ -215,7 +218,7 @@ pub fn finish(p: &Prepared, s: &DevelopSettings, frame: &Frame, info: &SourceInf
if cl != 0.0
&& let Some(b) = &p.clarity_blur
{
- let det = (l0 - b.data[i]).clamp(-2.5, 2.5);
+ let det = (l_pre - b.data[i]).clamp(-2.5, 2.5);
let mid = (-(base / 3.2).powi(2)).exp();
delta += cl * 0.85 * det * (0.35 + 0.65 * mid);
}
@@ -224,7 +227,7 @@ pub fn finish(p: &Prepared, s: &DevelopSettings, frame: &Frame, info: &SourceInf
if (tx != 0.0 || sp != 0.0)
&& let Some(b) = &p.texture_blur
{
- let det = l0 - b.data[i];
+ let det = l_pre - b.data[i];
let tame = 1.0 - 0.6 * smooth(0.4, 1.6, det.abs());
delta += tx * 1.1 * det.clamp(-1.0, 1.0) * tame;
if sp != 0.0 {
diff --git a/crates/pipeline/src/lib.rs b/crates/pipeline/src/lib.rs
index b137551ae..30d01acd8 100644
--- a/crates/pipeline/src/lib.rs
+++ b/crates/pipeline/src/lib.rs
@@ -14,6 +14,11 @@
//!
//! Spatial parameters are specified relative to the image's long edge, so a 400 px preview and a
//! 60 MP export look alike.
+//!
+//! Exposure is a gain, so the spatial stages run on the un-exposed image and the per-pixel stage
+//! applies it (filters on log luminance are shift-equivariant: identical result). With
+//! [`render_cached`] each stage's output is reused while its inputs are unchanged ([`StageCache`]):
+//! dragging a tone, colour or exposure slider re-runs only the per-pixel stage.
#![forbid(unsafe_code)]
pub mod auto;
@@ -27,6 +32,8 @@ pub mod spots;
mod tone;
use lightcraft_develop::{DevelopSettings, Treatment};
+use std::sync::{Arc, Mutex};
+
use lightcraft_raster::{Histogram, Plane, Rgb32f, Rgba8, par_rows};
pub use tone::ToneMap;
@@ -77,15 +84,24 @@ pub struct Rendered {
}
/// Everything the per-pixel stage needs, precomputed at output resolution.
+///
+/// The image and planes are computed *before exposure* (so they can be reused while exposure is
+/// dragged): the per-pixel stage multiplies the image by `gain` and shifts log planes by `ev`.
+/// Spatial filters on log luminance are shift-equivariant, so this equals filtering after exposure.
pub(crate) struct Prepared {
- pub img: Rgb32f,
- /// Log2 luminance relative to middle grey.
- pub log_l: Plane,
- pub base: Plane,
- pub clarity_blur: Option<Plane>,
- pub texture_blur: Option<Plane>,
- pub dark: Option<Plane>,
+ pub img: Arc<Rgb32f>,
+ /// Log2 luminance relative to middle grey (before exposure: add `ev`).
+ pub log_l: Arc<Plane>,
+ pub base: Arc<Plane>,
+ pub clarity_blur: Option<Arc<Plane>>,
+ pub texture_blur: Option<Arc<Plane>>,
+ pub dark: Option<Arc<Plane>>,
+ /// Airlight of `dark` (before exposure).
+ pub air: f32,
pub masks: Vec<masks::Evaluated>,
+ /// Exposure in EV and as a linear gain.
+ pub ev: f32,
+ pub gain: f32,
/// Output pixels per unit of the source long edge.
pub px_per_long: f64,
}
@@ -95,9 +111,89 @@ pub fn output_size(src_w: usize, src_h: usize, s: &DevelopSettings, req: &Render
geometry::Frame::new(src_w, src_h, s, req.apply_crop).fit(req.max_w, req.max_h)
}
+/// Intermediate results of recent renders of one view, reused by [`render_cached`].
+///
+/// Each stage is keyed by exactly the inputs it depends on: the resampled source by the source
+/// buffer, framing and output size; the white-balanced, retouched, denoised image by those plus
+/// white balance, spots and noise reduction; each spatial plane by that plus its own radius. So a
+/// tone or colour slider drag (or exposure) re-runs only the per-pixel stage, a clarity drag skips
+/// resampling and noise reduction, and so on. Holds the last few output sizes (a draft-size and a
+/// full-size render of the same view both stay warm).
+pub struct StageCache {
+ entries: Mutex<Vec<CacheEntry>>,
+ capacity: usize,
+}
+
+impl Default for StageCache {
+ fn default() -> Self {
+ StageCache { entries: Mutex::new(Vec::new()), capacity: 2 }
+ }
+}
+
+impl StageCache {
+ /// Drop everything (e.g. when memory is needed).
+ pub fn clear(&self) {
+ self.lock().clear();
+ }
+
+ /// Number of cached output sizes.
+ pub fn len(&self) -> usize {
+ self.lock().len()
+ }
+
+ pub fn is_empty(&self) -> bool {
+ self.len() == 0
+ }
+
+ fn lock(&self) -> std::sync::MutexGuard<'_, Vec<CacheEntry>> {
+ self.entries.lock().unwrap_or_else(|e| e.into_inner())
+ }
+
+ fn get(&self, src: &Arc<Rgb32f>, geo: u64) -> Option<CacheEntry> {
+ self.lock().iter().find(|e| e.geo == geo && Arc::ptr_eq(&e.src, src)).cloned()
+ }
+
+ fn put(&self, e: CacheEntry) {
+ let mut v = self.lock();
+ v.retain(|o| !(o.geo == e.geo && Arc::ptr_eq(&o.src, &e.src)));
+ v.push(e);
+ while v.len() > self.capacity {
+ v.remove(0);
+ }
+ }
+}
+
+#[derive(Clone)]
+struct CacheEntry {
+ src: Arc<Rgb32f>,
+ geo: u64,
+ sampled: Arc<Rgb32f>,
+ lin: Option<(u64, Arc<Rgb32f>)>,
+ planes: local::Planes,
+}
+
+fn hash_of(parts: impl std::hash::Hash) -> u64 {
+ use std::hash::{BuildHasher, BuildHasherDefault, DefaultHasher};
+ BuildHasherDefault::<DefaultHasher>::default().hash_one(parts)
+}
+
/// Render `src` with settings `s`.
pub fn render(src: &Rgb32f, info: &SourceInfo, s: &DevelopSettings, req: &RenderRequest) -> Rendered {
- let prof = std::env::var_os("LIGHTCRAFT_PROFILE").is_some();
+ render_impl(Src::Borrowed(src), info, s, req, None)
+}
+
+/// [`render`], reusing (and refreshing) the intermediate results in `cache`. The output is
+/// identical to [`render`]'s.
+pub fn render_cached(src: &Arc<Rgb32f>, info: &SourceInfo, s: &DevelopSettings, req: &RenderRequest, cache: &StageCache) -> Rendered {
+ render_impl(Src::Shared(src), info, s, req, Some(cache))
+}
+
+enum Src<'a> {
+ Borrowed(&'a Rgb32f),
+ Shared(&'a Arc<Rgb32f>),
+}
+
+fn render_impl(src: Src<'_>, info: &SourceInfo, s: &DevelopSettings, req: &RenderRequest, cache: Option<&StageCache>) -> Rendered {
// `Instant::now()` panics on wasm32-unknown-unknown: only read the clock when profiling.
let lap = |what: &str, t: &mut Option<std::time::Instant>| {
if let Some(t) = t {
@@ -105,20 +201,59 @@ pub fn render(src: &Rgb32f, info: &SourceInfo, s: &DevelopSettings, req: &Render
*t = std::time::Instant::now();
}
};
- let mut t = prof.then(std::time::Instant::now);
+ let mut t = profiling().then(std::time::Instant::now);
let s = &*profiles::effective(s);
- let frame = geometry::Frame::new(src.width, src.height, s, req.apply_crop);
+ let src_img: &Rgb32f = match &src {
+ Src::Borrowed(r) => r,
+ Src::Shared(a) => a,
+ };
+ let frame = geometry::Frame::new(src_img.width, src_img.height, s, req.apply_crop);
let (w, h) = frame.fit(req.max_w, req.max_h);
- let mut img = frame.sample(src, w, h);
- lap("sample", &mut t);
let px_per_long = frame.px_per_long(w);
+ let src_long = src_img.width.max(src_img.height);
+
+ let shared = match (&src, cache) {
+ (Src::Shared(a), Some(c)) => Some((*a, c)),
+ _ => None,
+ };
+ let geo = hash_of((format!("{frame:?}"), w, h));
+ let cached = shared.and_then(|(a, c)| c.get(a, geo));
+ let sampled = match &cached {
+ Some(e) => e.sampled.clone(),
+ None => Arc::new(frame.sample(src_img, w, h)),
+ };
+ lap("sample", &mut t);
- local::scene_linear_pre(&mut img, info, s);
- spots::apply(&mut img, &s.spots, &frame, px_per_long);
- local::denoise(&mut img, s, src.width.max(src.height), w.max(h));
- lap("wb/exposure/spots/nr", &mut t);
- let prep = local::prepare(img, s, &frame, px_per_long, req.quality);
+ let (wb_t, wb_tint) = local::effective_wb(info, s);
+ let d = &s.detail;
+ let lin_key = hash_of((
+ geo,
+ [wb_t, wb_tint, info.as_shot_temp, info.as_shot_tint].map(f64::to_bits),
+ format!("{:?}", s.spots),
+ [d.nr_luminance, d.nr_detail, d.nr_color, d.nr_color_detail, d.nr_color_smoothness].map(f64::to_bits),
+ src_long,
+ ));
+ let lin = match cached.as_ref().and_then(|e| e.lin.clone()).filter(|(k, _)| *k == lin_key) {
+ Some((_, img)) => img,
+ None => {
+ // Without a cache the resampled buffer is ours: work on it in place.
+ let mut img = if shared.is_some() { (*sampled).clone() } else { Arc::unwrap_or_clone(sampled.clone()) };
+ local::white_balance(&mut img, info, s);
+ spots::apply(&mut img, &s.spots, &frame, px_per_long);
+ local::denoise(&mut img, s, src_long, w.max(h));
+ Arc::new(img)
+ }
+ };
+ lap("wb/spots/nr", &mut t);
+ let mut planes = match cached.map(|e| e.planes) {
+ Some(p) if p.key == lin_key => p,
+ _ => local::Planes { key: lin_key, ..Default::default() },
+ };
+ let prep = local::prepare(lin.clone(), s, &frame, px_per_long, req.quality, &mut planes);
lap("prepare", &mut t);
+ if let Some((a, c)) = shared {
+ c.put(CacheEntry { src: a.clone(), geo, sampled, lin: Some((lin_key, lin)), planes });
+ }
let image = finish::finish(&prep, s, &frame, info);
lap("finish", &mut t);
let histogram = Histogram::of_srgb8(&image);
diff --git a/crates/pipeline/src/local.rs b/crates/pipeline/src/local.rs
index b84a048ed..f1b612104 100644
--- a/crates/pipeline/src/local.rs
+++ b/crates/pipeline/src/local.rs
@@ -5,6 +5,8 @@ use lightcraft_color::cct::{temp_tint_to_xy, wb_matrix};
use lightcraft_color::{REC2020, luminance_2020};
use lightcraft_develop::DevelopSettings;
use lightcraft_raster::blur::gaussian;
+use std::sync::Arc;
+
use lightcraft_raster::{Plane, Rgb32f};
use crate::geometry::Frame;
@@ -12,6 +14,15 @@ use crate::{Prepared, Quality, SourceInfo, for_rows, masks, timed};
/// White balance (relative to the source's as-shot white) and exposure, in place.
pub fn scene_linear_pre(img: &mut Rgb32f, info: &SourceInfo, s: &DevelopSettings) {
+ wb_gain(img, info, s, 2f32.powf(s.light.exposure as f32));
+}
+
+/// White balance only (exposure is applied by the per-pixel stage, see [`crate::Prepared`]).
+pub fn white_balance(img: &mut Rgb32f, info: &SourceInfo, s: &DevelopSettings) {
+ wb_gain(img, info, s, 1.0);
+}
+
+fn wb_gain(img: &mut Rgb32f, info: &SourceInfo, s: &DevelopSettings, gain: f32) {
let (t, tint) = effective_wb(info, s);
let m = if (t - info.as_shot_temp).abs() < 1e-6 && (tint - info.as_shot_tint).abs() < 1e-6 {
None
@@ -24,7 +35,6 @@ pub fn scene_linear_pre(img: &mut Rgb32f, info: &SourceInfo, s: &DevelopSettings
let y = g[0] * 0.2627 + g[1] * 0.6780 + g[2] * 0.0593;
Some(m.mul(&lightcraft_color::Mat3::diag(1.0 / y, 1.0 / y, 1.0 / y)).to_f32())
};
- let gain = 2f32.powf(s.light.exposure as f32);
let w = img.width;
for_rows(&mut img.data, w, |_, row| {
for p in row.iter_mut() {
@@ -167,8 +177,35 @@ pub fn log_lum(c: [f32; 3]) -> f32 {
(luminance_2020(c).max(1e-7) / crate::tone::GREY).log2()
}
-pub fn prepare(img: Rgb32f, s: &DevelopSettings, frame: &Frame, px_per_long: f64, q: Quality) -> Prepared {
- let log_l = timed("log_l", || img.map(log_lum));
+/// Spatial planes of one (pre-exposure) image, each tagged with the radius it was computed at.
+/// `key` identifies the image; see [`crate::StageCache`].
+#[derive(Clone, Default)]
+pub(crate) struct Planes {
+ pub key: u64,
+ pub log_l: Option<Arc<Plane>>,
+ pub base: Option<(u32, Arc<Plane>)>,
+ pub clarity: Option<(u32, Arc<Plane>)>,
+ pub texture: Option<(u32, Arc<Plane>)>,
+ /// Dark channel and its airlight.
+ pub dark: Option<(u32, Arc<Plane>, f32)>,
+}
+
+/// Reuse `slot` if it was computed at `sigma`, else compute and store it.
+fn plane_at(slot: &mut Option<(u32, Arc<Plane>)>, sigma: f32, f: impl FnOnce() -> Plane) -> Arc<Plane> {
+ match slot {
+ Some((k, p)) if *k == sigma.to_bits() => p.clone(),
+ _ => {
+ let p = Arc::new(f());
+ *slot = Some((sigma.to_bits(), p.clone()));
+ p
+ }
+ }
+}
+
+/// The spatial planes the per-pixel stage needs for `img` (white-balanced, before exposure),
+/// reusing whatever `planes` already holds for it.
+pub(crate) fn prepare(img: Arc<Rgb32f>, s: &DevelopSettings, frame: &Frame, px_per_long: f64, q: Quality, planes: &mut Planes) -> Prepared {
+ let log_l = planes.log_l.get_or_insert_with(|| Arc::new(timed("log_l", || img.map(log_lum)))).clone();
let ppl = px_per_long as f32;
let tone_active =
s.light.highlights != 0.0 || s.light.shadows != 0.0 || s.masks.iter().any(|m| m.adjust.highlights != 0.0 || m.adjust.shadows != 0.0);
@@ -176,26 +213,41 @@ pub fn prepare(img: Rgb32f, s: &DevelopSettings, frame: &Frame, px_per_long: f64
// Edge-aware base at ~1.5% of the long edge (EV² epsilon: edges of > ~0.6 EV are preserved).
let sigma = (0.015 * ppl).max(1.0);
let sigma = if q == Quality::Draft { sigma.min(24.0) } else { sigma };
- timed("base", || guided_fast(&log_l, sigma, 0.35))
+ plane_at(&mut planes.base, sigma, || timed("base", || guided_fast(&log_l, sigma, 0.35)))
} else {
log_l.clone()
};
let local_any = |f: fn(&lightcraft_develop::LocalAdjustments) -> f64| s.masks.iter().any(|m| f(&m.adjust) != 0.0);
- let clarity_blur =
- (s.effects.clarity != 0.0 || local_any(|a| a.clarity)).then(|| timed("clarity", || guided_fast(&log_l, (0.012 * ppl).max(1.0), 0.8)));
- let texture_blur = (s.effects.texture != 0.0 || s.detail.sharpen_amount != 0.0 || local_any(|a| a.texture) || local_any(|a| a.sharpness))
- .then(|| timed("texture", || gaussian(&log_l, (0.0018 * ppl).max(0.6))));
- let dark = (s.effects.dehaze != 0.0 || local_any(|a| a.dehaze)).then(|| {
- let _t = crate::profiling().then(std::time::Instant::now);
- let min_c = img.map(|c| c[0].min(c[1]).min(c[2]));
- let d = gaussian(&min_c, (0.02 * ppl).max(1.0));
- if let Some(t) = _t {
- eprintln!(" dehaze: {:.1} ms", t.elapsed().as_secs_f64() * 1e3);
- }
- d
+ let clarity_blur = (s.effects.clarity != 0.0 || local_any(|a| a.clarity)).then(|| {
+ let sigma = (0.012 * ppl).max(1.0);
+ plane_at(&mut planes.clarity, sigma, || timed("clarity", || guided_fast(&log_l, sigma, 0.8)))
});
- let masks = timed("masks", || masks::evaluate(&s.masks, frame, img.width, img.height, &img, &log_l));
- Prepared { img, log_l, base, clarity_blur, texture_blur, dark, masks, px_per_long }
+ let texture_blur =
+ (s.effects.texture != 0.0 || s.detail.sharpen_amount != 0.0 || local_any(|a| a.texture) || local_any(|a| a.sharpness)).then(|| {
+ let sigma = (0.0018 * ppl).max(0.6);
+ plane_at(&mut planes.texture, sigma, || timed("texture", || gaussian(&log_l, sigma)))
+ });
+ let (dark, air) = if s.effects.dehaze != 0.0 || local_any(|a| a.dehaze) {
+ let sigma = (0.02 * ppl).max(1.0);
+ match &planes.dark {
+ Some((k, d, air)) if *k == sigma.to_bits() => (Some(d.clone()), *air),
+ _ => {
+ let d = timed("dehaze", || {
+ let min_c = img.map(|c| c[0].min(c[1]).min(c[2]));
+ Arc::new(gaussian(&min_c, sigma))
+ });
+ let air = airlight(&d);
+ planes.dark = Some((sigma.to_bits(), d.clone(), air));
+ (Some(d), air)
+ }
+ }
+ } else {
+ (None, 1.0)
+ };
+ let ev = s.light.exposure as f32;
+ let gain = 2f32.powf(ev);
+ let masks = timed("masks", || masks::evaluate(&s.masks, frame, img.width, img.height, &img, &log_l, ev));
+ Prepared { img, log_l, base, clarity_blur, texture_blur, dark, air, masks, ev, gain, px_per_long }
}
/// Airlight estimate: bright end of the dark channel.
diff --git a/crates/pipeline/src/masks.rs b/crates/pipeline/src/masks.rs
index 910f28740..bd27f90f7 100644
--- a/crates/pipeline/src/masks.rs
+++ b/crates/pipeline/src/masks.rs
@@ -21,7 +21,7 @@ fn smooth(e0: f32, e1: f32, x: f32) -> f32 {
t * t * (3.0 - 2.0 * t)
}
-pub fn evaluate(masks: &[Mask], frame: &Frame, w: usize, h: usize, img: &Rgb32f, log_l: &Plane) -> Vec<Evaluated> {
+pub fn evaluate(masks: &[Mask], frame: &Frame, w: usize, h: usize, img: &Rgb32f, log_l: &Plane, ev: f32) -> Vec<Evaluated> {
masks
.iter()
.filter(|m| m.visible && !m.components.is_empty())
@@ -29,7 +29,7 @@ pub fn evaluate(masks: &[Mask], frame: &Frame, w: usize, h: usize, img: &Rgb32f,
let mut alpha = Plane::new(w, h);
let mut first = true;
for comp in &m.components {
- let mut c = shape_alpha(&comp.shape, frame, w, h, img, log_l);
+ let mut c = shape_alpha(&comp.shape, frame, w, h, img, log_l, ev);
if comp.invert {
c.data.iter_mut().for_each(|v| *v = 1.0 - *v);
}
@@ -71,9 +71,11 @@ fn for_each_pos(frame: &Frame, w: usize, h: usize, out: &mut Plane, f: impl Fn(P
});
}
-pub fn shape_alpha(shape: &MaskShape, frame: &Frame, w: usize, h: usize, img: &Rgb32f, log_l: &Plane) -> Plane {
+pub fn shape_alpha(shape: &MaskShape, frame: &Frame, w: usize, h: usize, img: &Rgb32f, log_l: &Plane, ev: f32) -> Plane {
let mut out = Plane::new(w, h);
let to_long = |p: Point| frame.norm_to_long(p);
+ // `img`/`log_l` are before exposure: range masks select on the exposed values.
+ let gain = ev.exp2();
match shape {
MaskShape::Linear { start, end } => {
let (a, b) = (to_long(*start), to_long(*end));
@@ -103,7 +105,7 @@ pub fn shape_alpha(shape: &MaskShape, frame: &Frame, w: usize, h: usize, img: &R
let (lo, hi, lf, hf) = (*lo as f32, *hi as f32, (*lo_feather as f32).max(1e-3), (*hi_feather as f32).max(1e-3));
for (v, l) in out.data.iter_mut().zip(&log_l.data) {
// map log luminance to a 0..1 perceptual scale (−8..+4 EV)
- let y = ((l + 8.0) / 12.0).clamp(0.0, 1.0);
+ let y = ((l + ev + 8.0) / 12.0).clamp(0.0, 1.0);
*v = smooth(lo - lf, lo, y) * (1.0 - smooth(hi, hi + hf, y));
}
}
@@ -111,7 +113,7 @@ pub fn shape_alpha(shape: &MaskShape, frame: &Frame, w: usize, h: usize, img: &R
let tol = 0.04 + 0.16 * (*refine as f32 / 100.0);
let samples: Vec<[f32; 3]> = samples.iter().map(|s| [s[0] as f32, s[1] as f32, s[2] as f32]).collect();
for (v, c) in out.data.iter_mut().zip(&img.data) {
- let lab = lightcraft_color::perceptual::oklab_from_2020(tonemap_for_select(*c));
+ let lab = lightcraft_color::perceptual::oklab_from_2020(tonemap_for_select(c.map(|v| v * gain)));
let d = samples
.iter()
.map(|s| ((lab[1] - s[1]).powi(2) + (lab[2] - s[2]).powi(2) + 0.25 * (lab[0] - s[0]).powi(2)).sqrt())
@@ -126,8 +128,8 @@ pub fn shape_alpha(shape: &MaskShape, frame: &Frame, w: usize, h: usize, img: &R
for (i, v) in out.data.iter_mut().enumerate() {
let (x, y) = (i % w, i / w);
let n = m.apply(Point::new(x as f64 + 0.5, y as f64 + 0.5));
- let c = img.data[i];
- let l = log_l.data[i];
+ let c = img.data[i].map(|v| v * gain);
+ let l = log_l.data[i] + ev;
let blue = (c[2] - c[0]).max(0.0) / (c[2] + 1e-4);
let top = 1.0 - smooth(0.25, 0.7, n.y as f32);
*v = top * smooth(-3.5, -1.0, l) * (0.4 + 0.6 * smooth(0.0, 0.3, blue).max(smooth(0.0, 1.5, l)));
@@ -148,7 +150,7 @@ pub fn shape_alpha(shape: &MaskShape, frame: &Frame, w: usize, h: usize, img: &R
smooth_plane(&mut out, 0.015 * frame_px(frame, w));
}
MaskShape::Background => {
- let mut s = shape_alpha(&MaskShape::Subject, frame, w, h, img, log_l);
+ let mut s = shape_alpha(&MaskShape::Subject, frame, w, h, img, log_l, ev);
s.data.iter_mut().for_each(|v| *v = 1.0 - *v);
out = s;
}
@@ -230,7 +232,7 @@ mod tests {
let f = frame(100, 50);
let img = Rgb32f::new(100, 50);
let l = Plane::new(100, 50);
- let a = shape_alpha(&MaskShape::Linear { start: Point::new(0.0, 0.0), end: Point::new(1.0, 0.0) }, &f, 100, 50, &img, &l);
+ let a = shape_alpha(&MaskShape::Linear { start: Point::new(0.0, 0.0), end: Point::new(1.0, 0.0) }, &f, 100, 50, &img, &l, 0.0);
assert!(a.get(1, 25) > 0.99 && a.get(98, 25) < 0.01);
assert!((a.get(50, 25) - 0.5).abs() < 0.05);
}
@@ -241,10 +243,10 @@ mod tests {
let img = Rgb32f::new(100, 100);
let l = Plane::new(100, 100);
let shape = MaskShape::Radial { center: Point::new(0.5, 0.5), rx: 0.2, ry: 0.2, angle: 0.0, feather: 20.0, invert: false };
- let a = shape_alpha(&shape, &f, 100, 100, &img, &l);
+ let a = shape_alpha(&shape, &f, 100, 100, &img, &l, 0.0);
assert!(a.get(50, 50) > 0.99 && a.get(5, 5) < 0.01);
let m = Mask { components: vec![MaskComponent { op: MaskOp::Add, invert: true, shape }], ..Default::default() };
- let e = evaluate(&[m], &f, 100, 100, &img, &l);
+ let e = evaluate(&[m], &f, 100, 100, &img, &l, 0.0);
assert!(e[0].alpha.get(50, 50) < 0.01);
}
@@ -259,7 +261,7 @@ mod tests {
components: vec![MaskComponent { op: MaskOp::Add, invert: false, shape: MaskShape::Brush { strokes: vec![stroke, erase] } }],
..Default::default()
};
- let e = evaluate(&[m], &f, 200, 100, &img, &l);
+ let e = evaluate(&[m], &f, 200, 100, &img, &l, 0.0);
let a = &e[0].alpha;
assert!(a.get(40, 50) > 0.9, "{}", a.get(40, 50));
assert!(a.get(100, 50) < 0.05, "erased centre {}", a.get(100, 50));
diff --git a/crates/pipeline/src/tests.rs b/crates/pipeline/src/tests.rs
index 13bcb467a..e52cdf6a3 100644
--- a/crates/pipeline/src/tests.rs
+++ b/crates/pipeline/src/tests.rs
@@ -149,3 +149,79 @@ fn unedited_rendered_source_is_passthrough() {
}
assert!(worst <= 3, "max error {worst}");
}
+
+fn typical_edits() -> DevelopSettings {
+ let mut s = DevelopSettings::default();
+ s.light.exposure = 0.4;
+ s.light.highlights = -40.0;
+ s.light.shadows = 30.0;
+ s.effects.clarity = 15.0;
+ s.effects.texture = 10.0;
+ s.effects.dehaze = 12.0;
+ s.detail.nr_luminance = 30.0;
+ s.detail.nr_color = 25.0;
+ s.detail.sharpen_amount = 40.0;
+ s
+}
+
+fn max_diff(a: &lightcraft_raster::Rgba8, b: &lightcraft_raster::Rgba8) -> i32 {
+ assert_eq!((a.width, a.height), (b.width, b.height));
+ a.data.iter().zip(&b.data).map(|(p, q)| (0..3).map(|c| (p[c] as i32 - q[c] as i32).abs()).max().unwrap_or(0)).max().unwrap_or(0)
+}
+
+#[test]
+fn stage_cache_matches_uncached_render_through_a_slider_session() {
+ use crate::{Quality, StageCache, render_cached};
+ use std::sync::Arc;
+ let src = Arc::new(scene());
+ let info = SourceInfo { raw: true, ..Default::default() };
+ let cache = StageCache::default();
+ let full = RenderRequest::fit(200, 200);
+ let draft = RenderRequest { max_w: 120, max_h: 120, quality: Quality::Draft, apply_crop: true };
+ let mut s = typical_edits();
+ // every kind of edit: tone, exposure, spatial amounts, NR, WB, crop, colour — at two sizes
+ let steps: Vec<Box<dyn Fn(&mut DevelopSettings)>> = vec![
+ Box::new(|s| s.light.contrast = 20.0),
+ Box::new(|s| s.light.exposure = -0.7),
+ Box::new(|s| s.light.highlights = -80.0),
+ Box::new(|s| s.effects.clarity = -30.0),
+ Box::new(|s| s.effects.dehaze = 0.0),
+ Box::new(|s| s.effects.dehaze = 25.0),
+ Box::new(|s| s.detail.nr_luminance = 60.0),
+ Box::new(|s| {
+ s.wb.mode = lightcraft_develop::WbMode::Custom;
+ s.wb.temp = 4000.0;
+ }),
+ Box::new(|s| s.crop.geometry.rect = lightcraft_geom::Rect::new(0.1, 0.1, 0.9, 0.8)),
+ Box::new(|s| s.color.saturation = 30.0),
+ ];
+ for (i, step) in steps.iter().enumerate() {
+ step(&mut s);
+ for req in [&full, &draft] {
+ let a = render_cached(&src, &info, &s, req, &cache).image;
+ let b = render(&src, &info, &s, req).image;
+ assert_eq!(a, b, "step {i} {:?}", req.quality);
+ }
+ }
+ assert_eq!(cache.len(), 2);
+ // a different source buffer never hits another source's entries
+ let other = Arc::new(Rgb32f::filled(src.width, src.height, [0.3, 0.3, 0.3]));
+ assert_eq!(render_cached(&other, &info, &s, &full, &cache).image, render(&other, &info, &s, &full).image);
+}
+
+#[test]
+fn exposure_after_spatial_filters_equals_exposing_the_source() {
+ // The pipeline filters the image before exposure and applies exposure per pixel; filters on
+ // log luminance are shift-equivariant, so this must equal scaling the source.
+ let src = scene();
+ let info = SourceInfo { raw: true, ..Default::default() };
+ let mut s = typical_edits();
+ s.light.exposure = 1.3;
+ let a = render(&src, &info, &s, &RenderRequest::fit(200, 200)).image;
+ let g = 2f32.powf(1.3);
+ let scaled = src.map(|c| c.map(|v| v * g));
+ s.light.exposure = 0.0;
+ let b = render(&scaled, &info, &s, &RenderRequest::fit(200, 200)).image;
+ let d = max_diff(&a, &b);
+ assert!(d <= 2, "max difference {d}");
+}
diff --git a/crates/ui-egui/src/panels/detail.rs b/crates/ui-egui/src/panels/detail.rs
index 361c14f8a..54ca151a1 100644
--- a/crates/ui-egui/src/panels/detail.rs
+++ b/crates/ui-egui/src/panels/detail.rs
@@ -122,6 +122,7 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
let want = (img_rect.width().max(img_rect.height()) * ppp * scale).min(2560.0) as usize;
let (rw, rh) = if aspect >= 1.0 { (want, (want as f32 / aspect) as usize) } else { ((want as f32 * aspect) as usize, want) };
if let Some(job) = app.session.render_job(id, rw.max(8), rh.max(8), false, !crop_tool) {
+ let job = if interacting { job.draft() } else { job };
app.renderer.request(Slot::Main, job, 100);
}
let show_before = app.ui.before_after == BeforeAfter::Original || ui.input(|i| i.key_down(egui::Key::Backslash));
diff --git a/crates/ui-egui/src/render.rs b/crates/ui-egui/src/render.rs
index ecfe694b3..aa2ebee4c 100644
--- a/crates/ui-egui/src/render.rs
+++ b/crates/ui-egui/src/render.rs
@@ -11,8 +11,10 @@ use std::collections::HashMap;
use lightcraft_catalog::PhotoId;
use lightcraft_engine::Session;
use lightcraft_engine::media::{RenderJob, RenderResult};
+use lightcraft_engine::pipeline::StageCache;
use lightcraft_preview::JobPool;
use lightcraft_raster::Histogram;
+use std::sync::Arc;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Slot {
@@ -39,12 +41,21 @@ pub struct Renderer {
pub last_main_ms: f64,
/// Jobs finished since start (for inspect/perf).
pub completed: u64,
+ /// Per-view intermediate results (loupe and "before"), so slider drags redo only what changed.
+ stages: HashMap<Slot, Arc<StageCache>>,
}
impl Default for Renderer {
fn default() -> Self {
let threads = if cfg!(target_arch = "wasm32") { 0 } else { JobPool::<Slot, RenderResult>::default_threads().min(6) };
- Renderer { pool: JobPool::new(threads), pending: HashMap::new(), textures: HashMap::new(), last_main_ms: 0.0, completed: 0 }
+ Renderer {
+ pool: JobPool::new(threads),
+ pending: HashMap::new(),
+ textures: HashMap::new(),
+ last_main_ms: 0.0,
+ completed: 0,
+ stages: HashMap::new(),
+ }
}
}
@@ -67,6 +78,7 @@ impl Renderer {
}
self.pending.insert(slot, (job.key, priority));
let key = job.key;
+ let job = if matches!(slot, Slot::Main | Slot::Before) { job.with_stages(self.stages.entry(slot).or_default().clone()) } else { job };
self.pool.submit(slot, key, priority, Box::new(move || job.run()));
}