Skip to content

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

Merge M5 CPU performance (stage cache, full-res source level, parallel planes)

Brandon Thomas179aa90f01f9
diff --cc crates/pipeline/src/lib.rs
index c66026bab,30d01acd8..ac62a327a
--- a/crates/pipeline/src/lib.rs
+++ b/crates/pipeline/src/lib.rs
@@@ -27,10 -30,10 +32,12 @@@ pub mod optics
pub mod profiles;
pub mod spots;
mod tone;
+pub mod transform;
+pub mod upright;
use lightcraft_develop::{DevelopSettings, Treatment};
+ use std::sync::{Arc, Mutex};
+
use lightcraft_raster::{Histogram, Plane, Rgb32f, Rgba8, par_rows};
pub use tone::ToneMap;
@@@ -101,18 -111,89 +117,98 @@@ pub fn output_size(src_w: usize, src_h
geometry::Frame::new(src_w, src_h, s, req.apply_crop).fit(req.max_w, req.max_h)
}
+/// The geometric frame `render` uses for `src` (lens data, automatic CA estimate included).
+pub fn frame_for(src: &Rgb32f, info: &SourceInfo, s: &DevelopSettings, apply_crop: bool) -> geometry::Frame {
+ let mut frame = geometry::Frame::with_lens(src.width, src.height, s, apply_crop, info.lens.as_ref());
+ if s.optics.remove_ca && s.section_enabled("optics") {
+ frame.add_lateral_ca(optics::estimate_lateral_ca(src));
+ }
+ frame
+}
+
+ /// 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 {
@@@ -120,22 -201,59 +216,63 @@@
*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 s = &*upright::resolve(src, info, s);
- let frame = frame_for(src, info, 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 s = &*upright::resolve(src_img, info, s);
++ let frame = frame_for(src_img, info, 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);
- optics::defringe(&mut img, s, px_per_long / optics::DEFRINGE_REF_LONG);
- 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),
++ // defringe runs in this stage
++ format!("{:?}", s.optics),
+ [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);
++ optics::defringe(&mut img, s, px_per_long / optics::DEFRINGE_REF_LONG);
+ 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 --cc crates/pipeline/src/tests.rs
index 13bcb467a,e52cdf6a3..0361cb95d
--- a/crates/pipeline/src/tests.rs
+++ b/crates/pipeline/src/tests.rs
@@@ -149,3 -149,79 +149,84 @@@ fn unedited_rendered_source_is_passthro
}
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),
++ // optics and geometry (merged after the stage cache): defringe lives in the cached linear stage
++ Box::new(|s| s.optics.defringe_purple_amount = 10.0),
++ Box::new(|s| s.optics.distortion = 30.0),
++ Box::new(|s| s.optics.remove_ca = true),
++ Box::new(|s| s.geometry.vertical = 20.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 --cc crates/ui-egui/src/render.rs
index b84a4d5ec,aa2ebee4c..46c57afbc
--- a/crates/ui-egui/src/render.rs
+++ b/crates/ui-egui/src/render.rs
@@@ -41,9 -41,8 +43,11 @@@ pub struct Renderer
pub last_main_ms: f64,
/// Jobs finished since start (for inspect/perf).
pub completed: u64,
+ /// Keep a CPU copy of every texture so the UI can be rasterized headlessly (set when the app
+ /// is driven by the control channel).
+ pub keep_pixels: bool,
+ /// Per-view intermediate results (loupe and "before"), so slider drags redo only what changed.
+ stages: HashMap<Slot, Arc<StageCache>>,
}
impl Default for Renderer {
@@@ -55,7 -54,7 +59,8 @@@
textures: HashMap::new(),
last_main_ms: 0.0,
completed: 0,
+ keep_pixels: false,
+ stages: HashMap::new(),
}
}
}