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:
@@ -0,0 +1,282 @@
|
||||
//! The develop session — capabilities in, rendered image out.
|
||||
//!
|
||||
//! This is the only place the UI touches the pipeline, and it does so through
|
||||
//! two calls: [`dr_pipeline::EditGraph::capabilities`] to learn what controls
|
||||
//! to build, and `set_param` to change one. It never names an operation, and
|
||||
//! it knows nothing about shaders.
|
||||
//!
|
||||
//! Whether a control is a slider or a switch follows from the parameter's
|
||||
//! declared [`ParamKind`], not from which parameter it is (ARCH §4.3), so a
|
||||
//! new operation appears in the panel with no change here (FR-DEV-3c).
|
||||
|
||||
use dr_decode::RawImage;
|
||||
use dr_gpu::{AdjustPass, DemosaicedImage, Demosaicer, GpuContext};
|
||||
use dr_pipeline::{EditGraph, OpId, ParamId, ParamKind, Unit};
|
||||
|
||||
use crate::labels;
|
||||
use crate::ParamRow;
|
||||
|
||||
/// A loaded image plus its edit state.
|
||||
pub struct DevelopSession {
|
||||
graph: EditGraph,
|
||||
demosaiced: DemosaicedImage,
|
||||
adjust: AdjustPass,
|
||||
}
|
||||
|
||||
impl DevelopSession {
|
||||
/// Demosaic an image and prepare its edit graph.
|
||||
pub fn open(ctx: &GpuContext, raw: &RawImage) -> Result<Self, String> {
|
||||
let demosaicer = Demosaicer::new(ctx).map_err(|e| e.to_string())?;
|
||||
let demosaiced = demosaicer.run(raw).map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(Self {
|
||||
graph: EditGraph::default_chain(),
|
||||
demosaiced,
|
||||
adjust: AdjustPass::new(ctx),
|
||||
})
|
||||
}
|
||||
|
||||
/// The controls the interface should show.
|
||||
///
|
||||
/// Built entirely from the capability list. The `kind` string chooses the
|
||||
/// widget; nothing switches on a parameter's identity.
|
||||
pub fn rows(&self) -> Vec<ParamRow> {
|
||||
let mut rows = Vec::new();
|
||||
for (op_index, op) in self.graph.capabilities().iter().enumerate() {
|
||||
for (param_index, p) in op.params.iter().enumerate() {
|
||||
let (kind, min, max, precision, unit) = match &p.kind {
|
||||
ParamKind::Scalar {
|
||||
min,
|
||||
max,
|
||||
unit,
|
||||
precision,
|
||||
..
|
||||
} => (
|
||||
"scalar",
|
||||
*min,
|
||||
*max,
|
||||
i32::from(*precision),
|
||||
unit_suffix(*unit),
|
||||
),
|
||||
ParamKind::Bool => ("bool", 0.0, 1.0, 0, ""),
|
||||
};
|
||||
|
||||
rows.push(ParamRow {
|
||||
op_index: op_index as i32,
|
||||
param_index: param_index as i32,
|
||||
op_label: labels::resolve(op.label.0).into(),
|
||||
param_label: labels::resolve(p.label.0).into(),
|
||||
// The panel draws a heading wherever this is set, without
|
||||
// needing to know what an operation is.
|
||||
starts_group: param_index == 0,
|
||||
kind: kind.into(),
|
||||
value: p.value,
|
||||
default_value: p.default,
|
||||
minimum: min,
|
||||
maximum: max,
|
||||
precision,
|
||||
unit: unit.into(),
|
||||
});
|
||||
}
|
||||
}
|
||||
rows
|
||||
}
|
||||
|
||||
/// Apply a change from the interface.
|
||||
///
|
||||
/// Indices are positions in [`Self::rows`]; the mapping back to ids stays
|
||||
/// on this side of the boundary.
|
||||
pub fn set_param(&mut self, op_index: i32, param_index: i32, value: f32) {
|
||||
let Some((op, param)) = self.lookup(op_index, param_index) else {
|
||||
log::warn!("control at ({op_index}, {param_index}) has no parameter");
|
||||
return;
|
||||
};
|
||||
self.graph.set_param(op, param, value);
|
||||
}
|
||||
|
||||
/// Return one parameter to its default.
|
||||
pub fn reset_param(&mut self, op_index: i32, param_index: i32) {
|
||||
let Some((op, param)) = self.lookup(op_index, param_index) else {
|
||||
return;
|
||||
};
|
||||
let default = self
|
||||
.graph
|
||||
.capabilities()
|
||||
.iter()
|
||||
.find(|c| c.id == op)
|
||||
.and_then(|c| c.params.iter().find(|p| p.id == param))
|
||||
.map(|p| p.default)
|
||||
.unwrap_or(0.0);
|
||||
self.graph.set_param(op, param, default);
|
||||
}
|
||||
|
||||
pub fn reset_all(&mut self) {
|
||||
self.graph.reset();
|
||||
}
|
||||
|
||||
fn lookup(&self, op_index: i32, param_index: i32) -> Option<(OpId, ParamId)> {
|
||||
// Rows are emitted in capability order, so the flat index is the sum
|
||||
// of preceding parameter counts.
|
||||
let caps = self.graph.capabilities();
|
||||
let op = caps.get(usize::try_from(op_index).ok()?)?;
|
||||
let param = op.params.get(usize::try_from(param_index).ok()?)?;
|
||||
Some((op.id, param.id))
|
||||
}
|
||||
|
||||
/// Render at the requested display size and hand back a Slint image.
|
||||
///
|
||||
/// Renders at *viewport* resolution rather than sensor resolution, which
|
||||
/// is what keeps slider interaction inside the frame budget on a 24 MP
|
||||
/// file (FR-DSP-1).
|
||||
///
|
||||
/// The readback at the end is the temporary bridge documented on
|
||||
/// `AdjustPass::read_output`: ARCH §6.1 forbids it, and spike S1 removes
|
||||
/// it by importing the texture into Slint directly.
|
||||
pub fn render(&mut self, width: u32, height: u32) -> Result<slint::Image, String> {
|
||||
// Fit the render to the viewport while preserving aspect, so the
|
||||
// pass does no work on pixels the view will letterbox away.
|
||||
let (sw, sh) = self.demosaiced.size();
|
||||
let (w, h) = fit(sw, sh, width.max(1), height.max(1));
|
||||
|
||||
let shader = self.graph.compose();
|
||||
self.adjust
|
||||
.render(&self.demosaiced, &shader, w, h)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let (pixels, rw, rh) = self.adjust.read_output().map_err(|e| e.to_string())?;
|
||||
let buffer =
|
||||
slint::SharedPixelBuffer::<slint::Rgba8Pixel>::clone_from_slice(&pixels, rw, rh);
|
||||
Ok(slint::Image::from_rgba8(buffer))
|
||||
}
|
||||
|
||||
/// The image's natural aspect ratio, for sizing the viewport.
|
||||
pub fn source_size(&self) -> (u32, u32) {
|
||||
self.demosaiced.size()
|
||||
}
|
||||
|
||||
/// How many shader pipelines have been compiled. Surfaced so the status
|
||||
/// strip can show that slider movement is not recompiling.
|
||||
pub fn compiled_pipelines(&self) -> usize {
|
||||
self.adjust.cached_pipelines()
|
||||
}
|
||||
|
||||
pub fn is_neutral(&self) -> bool {
|
||||
self.graph.is_neutral()
|
||||
}
|
||||
}
|
||||
|
||||
/// Largest size fitting `(sw, sh)` inside `(max_w, max_h)`, preserving aspect.
|
||||
///
|
||||
/// Rendering to the letterboxed size rather than the full viewport avoids
|
||||
/// shading pixels the view will not show, which at a 3:2 image in a 16:9
|
||||
/// window is a fifth of them.
|
||||
fn fit(sw: u32, sh: u32, max_w: u32, max_h: u32) -> (u32, u32) {
|
||||
if sw == 0 || sh == 0 {
|
||||
return (max_w, max_h);
|
||||
}
|
||||
let scale = (max_w as f32 / sw as f32).min(max_h as f32 / sh as f32);
|
||||
// Never upscale past the source: there is no detail to recover, and a
|
||||
// 1:1 render is cheaper.
|
||||
let scale = scale.min(1.0);
|
||||
(
|
||||
((sw as f32 * scale).round() as u32).max(1),
|
||||
((sh as f32 * scale).round() as u32).max(1),
|
||||
)
|
||||
}
|
||||
|
||||
/// Suffix shown after a value. Comes from the descriptor's declared unit, so
|
||||
/// this function needs no knowledge of which parameter it is formatting.
|
||||
fn unit_suffix(unit: Unit) -> &'static str {
|
||||
match unit {
|
||||
Unit::None => "",
|
||||
Unit::Stops => " EV",
|
||||
Unit::Kelvin => " K",
|
||||
Unit::Percent => "%",
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use dr_pipeline::EditGraph;
|
||||
|
||||
#[test]
|
||||
fn every_capability_becomes_exactly_one_row() {
|
||||
// The UI shows what the pipeline offers — no more, and nothing
|
||||
// dropped.
|
||||
let graph = EditGraph::default_chain();
|
||||
let expected: usize = graph.capabilities().iter().map(|c| c.params.len()).sum();
|
||||
assert_eq!(expected, 10, "seven operations, ten parameters");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_operation_starts_exactly_one_group() {
|
||||
// The panel draws a heading per group; two groups for one operation
|
||||
// would duplicate the heading, none would merge two operations under
|
||||
// one.
|
||||
let graph = EditGraph::default_chain();
|
||||
let mut groups = 0;
|
||||
for op in graph.capabilities() {
|
||||
for (i, _) in op.params.iter().enumerate() {
|
||||
if i == 0 {
|
||||
groups += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(groups, graph.capabilities().len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unit_suffixes_come_from_the_descriptor() {
|
||||
assert_eq!(unit_suffix(Unit::Stops), " EV");
|
||||
assert_eq!(unit_suffix(Unit::None), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fitting_preserves_aspect_ratio() {
|
||||
// A 3:2 image in a 16:9 window must letterbox, not stretch.
|
||||
let (w, h) = fit(6000, 4000, 1600, 900);
|
||||
assert_eq!(h, 900);
|
||||
assert!(
|
||||
((w as f32 / h as f32) - 1.5).abs() < 0.01,
|
||||
"got {w}x{h}, aspect {}",
|
||||
w as f32 / h as f32
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fitting_never_upscales_past_the_source() {
|
||||
// Rendering a 400px image into a 4K window at 4K shades 25x the
|
||||
// pixels for no additional detail.
|
||||
let (w, h) = fit(400, 300, 3840, 2160);
|
||||
assert_eq!((w, h), (400, 300));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fitting_handles_a_degenerate_source() {
|
||||
let (w, h) = fit(0, 0, 800, 600);
|
||||
assert_eq!((w, h), (800, 600));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fitting_is_bounded_by_the_narrow_axis() {
|
||||
// A tall window on a wide image must be limited by width.
|
||||
let (w, h) = fit(4000, 1000, 800, 4000);
|
||||
assert_eq!(w, 800);
|
||||
assert_eq!(h, 200);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn routing_indices_map_back_to_the_right_parameter() {
|
||||
// A wrong index would silently move the wrong slider's value, which
|
||||
// is exactly the kind of bug that looks like a rendering fault.
|
||||
let graph = EditGraph::default_chain();
|
||||
let caps = graph.capabilities();
|
||||
for (oi, op) in caps.iter().enumerate() {
|
||||
for (pi, p) in op.params.iter().enumerate() {
|
||||
assert_eq!(caps[oi].params[pi].id, p.id);
|
||||
assert_eq!(caps[oi].id, op.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user