Add the develop pipeline: demosaic and seven raw adjustments

Decode through display, on the GPU: black/white normalisation, Bayer
demosaic, camera colour transform, and the first seven adjustment
operations — white balance, exposure, highlights/shadows, blacks/whites,
brilliance, vibrance, saturation.

Composable shaders. Each operation contributes a WGSL fragment rather
than owning a pass, and dr-pipeline fuses the *active* ones into a single
compute shader. One texture read and one write per frame regardless of
how many adjustments are in play, while the operations stay independent
in Rust — adding one is a new file, with no central shader to edit. An
operation at neutral settings contributes no code, no uniform and no
branch. Uniforms are prefixed per operation so two may both declare
`amount`; helpers dedupe by name from a single source of truth.

Pipelines cache on a structure hash covering the op-set and its order but
not the values, so dragging a slider uploads uniforms and reuses the
compiled pipeline. Measured on a 24 MP CR2: 0.60 ms re-render, one
pipeline compiled across ten slider positions.

The UI is generated, not written. EditGraph::capabilities() reports
parameters with their kinds, ranges, defaults and current values; the
panel builds one control per entry chosen by ParamKind. No file in ui/
names an operation, and dr-pipeline has no wgpu dependency, so codegen is
testable without a device (ARCH §6.5a).

Three defects found against real files, each silent:

- rawler 0.7.2's `xyz_to_cam` is all zeros — deprecated and no longer
  populated. The live matrices are in `color_matrix`, keyed by
  illuminant. Reading the old field yields no colour transform at all.
- `cam_to_xyz_normalized()` returns all NaN on any Bayer sensor: it
  divides each of four rows by its own sum, and the unused fourth
  (emerald) row sums to zero. Inverting the 3x3 ourselves avoids it.
  `wb_coeffs[3]` is NaN for the same reason and is normalised at decode.
- As-shot white balance reached the uniform block but no shader read it,
  so the first render of a real CR2 came out violently green. Green
  photosites collect roughly twice the signal of red and blue. Now
  applied unconditionally before any operation, with tests on ordering.

Demosaic is Malvar-He-Cutler rather than bilinear: gradient-corrected
interpolation at one 5x5 neighbourhood per pixel, where bilinear leaves
visible zippering on any high-contrast edge at 1:1. Two of the four
packed CFA constants were wrong on the first attempt, so all four layouts
are asserted to reconstruct the same colour. Crop origins at odd
coordinates re-phase the pattern; without that, red and blue swap.

X-Trans reports GpuError::UnsupportedCfa rather than approximating with
the Bayer path, which would look like a corrupt file.

206 tests, including GPU tests proving every operation and the full
seven-operation chain generate compilable WGSL.

Known gaps: the display path still reads back to the CPU each frame,
which ARCH §6.1 forbids and AC-8 asserts against — it is gated behind the
`readback` feature and waits on spike S1 wiring Slint's texture import.
Curve shapes are a first draft and want tuning against real photographs.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-09 11:37:58 +02:00
co-authored by Claude Opus 5
parent cc1c5c892d
commit 78e3e6b846
29 changed files with 5865 additions and 68 deletions
+136
View File
@@ -0,0 +1,136 @@
//! Render a RAW file through the full pipeline and write a PPM.
//!
//! The end-to-end check: decode → demosaic → adjust → display encode, on a
//! real file rather than a synthetic fixture. Unit tests prove each stage in
//! isolation; this proves they compose into an image a person would accept.
//!
//! ```sh
//! cargo run -p dr-gpu --example develop --features readback -- IMG.CR2 out.ppm
//! ```
//!
//! PPM because it needs no encoder dependency and every image viewer reads
//! it. This is a diagnostic, not the export path (FR-EXP-*).
use dr_gpu::{AdjustPass, Demosaicer, GpuContext};
use dr_pipeline::ops::{colour, exposure, tone, white_balance};
use dr_pipeline::EditGraph;
fn main() {
env_logger::init();
let mut args = std::env::args().skip(1);
let Some(input) = args.next() else {
eprintln!("usage: develop <file.cr2> [out.ppm] [preset]");
eprintln!(" preset: neutral (default) | punchy | recover");
std::process::exit(2);
};
let output = args.next().unwrap_or_else(|| "develop.ppm".into());
let preset = args.next().unwrap_or_else(|| "neutral".into());
let bytes = std::fs::read(&input).expect("read file");
let t0 = std::time::Instant::now();
let raw = dr_decode::decode(&bytes).expect("decode");
let decode_ms = t0.elapsed().as_secs_f32() * 1000.0;
println!(
"decoded {} × {} ({:?}), {decode_ms:.0} ms",
raw.crop.width, raw.crop.height, raw.cfa_pattern
);
let ctx = pollster::block_on(GpuContext::new_headless()).expect("gpu");
println!("adapter {} ({:?})", ctx.adapter_name(), ctx.backend());
let t1 = std::time::Instant::now();
let demosaicer = Demosaicer::new(&ctx).expect("demosaicer");
let image = demosaicer.run(&raw).expect("demosaic");
ctx.device.poll(wgpu::Maintain::Wait);
println!("demosaiced {:.0} ms", t1.elapsed().as_secs_f32() * 1000.0);
// Build an edit. The presets exist so the output can be eyeballed for
// each operation actually doing something, not merely compiling.
let mut graph = EditGraph::default_chain();
match preset.as_str() {
"punchy" => {
graph.set_param(exposure::ID, exposure::EXPOSURE, 0.3);
graph.set_param(tone::HIGHLIGHTS_SHADOWS_ID, tone::HIGHLIGHTS, -40.0);
graph.set_param(tone::HIGHLIGHTS_SHADOWS_ID, tone::SHADOWS, 30.0);
graph.set_param(tone::BLACKS_WHITES_ID, tone::BLACKS, -20.0);
graph.set_param(tone::BLACKS_WHITES_ID, tone::WHITES, 25.0);
graph.set_param(colour::VIBRANCE_ID, colour::VIBRANCE, 35.0);
}
"recover" => {
graph.set_param(exposure::ID, exposure::EXPOSURE, -0.5);
graph.set_param(tone::HIGHLIGHTS_SHADOWS_ID, tone::HIGHLIGHTS, -80.0);
graph.set_param(tone::HIGHLIGHTS_SHADOWS_ID, tone::SHADOWS, 60.0);
graph.set_param(colour::BRILLIANCE_ID, colour::BRILLIANCE, 40.0);
graph.set_param(white_balance::ID, white_balance::TEMPERATURE, 15.0);
}
_ => {}
}
let shader = graph.compose();
println!(
"shader {} active op(s), {} uniform floats, structure {:016x}",
shader.source.matches("---- ").count(),
shader.uniforms.len(),
shader.structure_hash
);
let mut adjust = AdjustPass::new(&ctx);
let (w, h) = image.size();
let t2 = std::time::Instant::now();
adjust.render(&image, &shader, w, h).expect("adjust");
ctx.device.poll(wgpu::Maintain::Wait);
println!("adjusted {:.2} ms", t2.elapsed().as_secs_f32() * 1000.0);
// Time a second render with only a value changed: this is the slider
// path, and it must not recompile.
graph.set_param(exposure::ID, exposure::EXPOSURE, 0.31);
let again = graph.compose();
let t3 = std::time::Instant::now();
adjust.render(&image, &again, w, h).expect("adjust");
ctx.device.poll(wgpu::Maintain::Wait);
println!(
"re-render {:.2} ms ({} pipeline(s) compiled)",
t3.elapsed().as_secs_f32() * 1000.0,
adjust.cached_pipelines()
);
let (pixels, pw, ph) = adjust.read_output().expect("readback");
// Sanity: an all-black or all-white result means something upstream
// failed silently, and it is far easier to see here than in a viewer.
let mut sum = 0u64;
let mut min = 255u8;
let mut max = 0u8;
for px in pixels.chunks_exact(4) {
let l = px[0].max(px[1]).max(px[2]);
sum += u64::from(l);
min = min.min(l);
max = max.max(l);
}
let mean = sum as f64 / (pixels.len() / 4) as f64;
println!("levels min {min}, mean {mean:.1}, max {max}");
if max == 0 {
eprintln!("WARNING: the image is entirely black");
}
write_ppm(&output, &pixels, pw, ph);
println!("wrote {output} ({pw} × {ph})");
}
/// Write binary PPM (P6): a three-line header then RGB triples.
fn write_ppm(path: &str, rgba: &[u8], w: u32, h: u32) {
use std::io::Write;
let mut out = Vec::with_capacity((w * h * 3) as usize + 32);
out.extend_from_slice(format!("P6\n{w} {h}\n255\n").as_bytes());
for px in rgba.chunks_exact(4) {
out.extend_from_slice(&px[..3]);
}
std::fs::File::create(path)
.expect("create output")
.write_all(&out)
.expect("write output");
}