diff --git a/crates/gpu/src/fx_lut.rs b/crates/gpu/src/fx_lut.rs index cf8df08d6..532679c0e 100644 --- a/crates/gpu/src/fx_lut.rs +++ b/crates/gpu/src/fx_lut.rs @@ -37,16 +37,29 @@ pub(crate) const IDS: &[&str] = &[ /// Largest LUT the kernels take (floats in the storage buffer); bigger ones render on the CPU. const MAX_FLOATS: usize = 1 << 24; +/// Most halvings the kernel's partition searches run (`fxl_search_steps` caps the count there). +const SHADER_MAX_STEPS: u32 = 32; + +/// Halvings a partition search over `n` entries runs: `n`'s bit length, which every search +/// finishes within, plus one to spare. +const fn search_steps(n: usize) -> u32 { + usize::BITS - n.leading_zeros() + 1 +} + +// A program's tables fit in MAX_FLOATS floats, so none needs more halvings than the kernel runs. +const _: () = assert!(search_steps(MAX_FLOATS) <= SHADER_MAX_STEPS); + /// Run effect `id` (one of [`IDS`]); `None` = this parameter combination runs on the CPU. pub(crate) fn apply(e: &mut Enc, id: &str, ctx: &EffectCtx, b: GBuf) -> Option { let (ops, straight) = effectcraft_effects::color_program(id, ctx)?; if ops.is_empty() { return Some(b); } - let data = encode(&ops)?; + let (data, steps) = encode(&ops)?; let buf = e.data(&data); let mut p = Params::default(); p.u[0][0] = (straight == Straight::Divide) as u32; + p.u[0][1] = steps; let out = e.scratch(b.img.width, b.img.height); e.pixels("fxl_point", &p, &b.img, None, &out, Some(&buf)); Some(GBuf { img: out, ..b }) @@ -79,11 +92,13 @@ pub(crate) fn push_lut(d: &mut Vec, lut: &Lut) -> f32 { at } -/// Program layout: a stream of (opcode, arguments…) ending in 0, followed by LUT data. -fn encode(ops: &[ColorOp]) -> Option> { +/// Program layout: a stream of (opcode, arguments…) ending in 0, followed by LUT data; and the +/// halvings the kernel's partition searches need for this program's tables. +fn encode(ops: &[ColorOp]) -> Option<(Vec, u32)> { if ops.len() > MAX_OPS { return None; } + let mut steps = 0; let mut prog: Vec = vec![]; let mut tables: Vec> = vec![]; // LUT offsets are patched once the program's length is known. @@ -142,6 +157,13 @@ fn encode(ops: &[ColorOp]) -> Option> { prog.extend(lo.iter().chain(hi)); } ColorOp::Lut { lut, shaper, interp, inverse } => { + // The kernel searches an inverted 1D table and every shaper channel. + if *inverse && lut.n3 == 0 { + steps = steps.max(search_steps(lut.n1)); + } + for channel in shaper.iter().flat_map(|s| s.iter()) { + steps = steps.max(search_steps(channel.len())); + } prog.extend([8.0, 0.0, *interp as f32, *inverse as u32 as f32, -1.0]); let mut t = vec![]; push_lut(&mut t, lut); @@ -179,5 +201,39 @@ fn encode(ops: &[ColorOp]) -> Option> { for t in tables { prog.extend(t); } - Some(prog) + Some((prog, steps)) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The kernel's partition search (fxl_shape, fxl_inv_1d) run for `steps` halvings over the + /// sorted table 0, 1, …, n - 1: the index of the first entry not below `x`. + fn search(n: usize, steps: u32, x: f64) -> usize { + let (mut lo, mut hi) = (0, n); + for _ in 0..steps { + if lo < hi { + let mid = lo + (hi - lo) / 2; + if (mid as f64) < x { + lo = mid + 1; + } else { + hi = mid; + } + } + } + lo + } + + #[test] + fn search_steps_finish_every_partition_search() { + for n in [1, 2, 3, 4, 5, 255, 256, 257, 1 << 16, (1 << 16) + 1, MAX_FLOATS / 3, MAX_FLOATS] { + let steps = search_steps(n); + assert!(steps <= SHADER_MAX_STEPS, "{n} entries need more halvings than the kernel runs"); + for x in [-1.0, 0.0, 0.5, n as f64 / 3.0 + 0.5, n as f64 - 1.5, n as f64 - 0.5, n as f64 + 1.0] { + let want = (x.ceil().max(0.0) as usize).min(n); + assert_eq!(search(n, steps, x), want, "{n} entries, target {x}"); + } + } + } } diff --git a/crates/gpu/src/shaders/fx_lut.wgsl b/crates/gpu/src/shaders/fx_lut.wgsl index f9081f834..b4d51a0ad 100644 --- a/crates/gpu/src/shaders/fx_lut.wgsl +++ b/crates/gpu/src/shaders/fx_lut.wgsl @@ -120,6 +120,14 @@ fn mix_exact(a: vec3, b: vec3, t: f32) -> vec3 { return a + (b - a) * t; } +// Halvings for the partition searches (fxl_shape, fxl_inv_1d): written per dispatch by fx_lut.rs +// (enough for the largest table the program searches) and capped here. Every invocation runs the +// same count, so the loops stay bounded and non-divergent, but because the count isn't known when +// the shader is compiled, a software driver doesn't unroll every search into the kernel. +fn fxl_search_steps() -> u32 { + return min(P.u[0].y, 32u); +} + // ocio::shape over `n` (x, y) points at `at` (`flip`: the curve with x and y swapped). fn fxl_shape(at: u32, n: u32, x: f32, flip: bool) -> f32 { if (n < 2u) { @@ -130,8 +138,8 @@ fn fxl_shape(at: u32, n: u32, x: f32, flip: bool) -> f32 { // partition_point(|p| p.x < x). var lo = 0u; var hi = n; - // Bound the partition search as for inverse 1D LUTs below. - for (var step = 0u; step < 32u; step++) { + // Bounded as for inverse 1D LUTs below. + for (var step = 0u; step < fxl_search_steps(); step++) { if (lo < hi) { let mid = lo + (hi - lo) / 2u; if (data[at + mid * 2u + kx] < x) { @@ -161,14 +169,17 @@ fn fxl_shaper_at(s: u32, k: u32) -> vec2 { // Evaluate independent shaper channels without dynamically writing vector lanes. fn fxl_shaper_apply(sh: u32, c: vec3, flip: bool) -> vec3 { - let r = fxl_shaper_at(sh, 0u); - let g = fxl_shaper_at(sh, 1u); - let b = fxl_shaper_at(sh, 2u); - return vec3( - fxl_shape(r.x, r.y, c.x, flip), - fxl_shape(g.x, g.y, c.y, flip), - fxl_shape(b.x, b.y, c.z, flip) - ); + // One search per kernel instead of three: a software driver unrolls each fixed-length + // search, so every extra call multiplies compile time. Lanes are read and written with + // select(), never by a dynamic index. + var r = c; + for (var k = 0u; k < 3u; k++) { + let lane = vec3(k == 0u, k == 1u, k == 2u); + let a = fxl_shaper_at(sh, k); + let v = fxl_shape(a.x, a.y, select(select(c.z, c.y, k == 1u), c.x, k == 0u), flip); + r = select(r, vec3(v), lane); + } + return r; } fn fxl_file_fwd(o: u32, sh: i32, c0: vec3, interp: u32) -> vec3 { @@ -188,9 +199,10 @@ fn fxl_inv_1d(o: u32, i: u32, y: f32) -> f32 { // partition_point over the table's values. var lo = 0u; var hi = n; - // A u32 interval needs at most 32 halvings. A fixed trip count avoids - // a divergent data-dependent loop on software Vulkan without changing lookup. - for (var step = 0u; step < 32u; step++) { + // fxl_search_steps() halvings cover every table this program searches. A trip count shared + // by every invocation avoids a divergent data-dependent loop on software Vulkan without + // changing lookup. + for (var step = 0u; step < fxl_search_steps(); step++) { if (lo < hi) { let mid = lo + (hi - lo) / 2u; if (data[b1 + mid * 3u + i] < y) { @@ -218,7 +230,12 @@ fn fxl_file(o: u32, sh: i32, c: vec3, interp: u32, inverse: bool) -> vec3 1u) { - x = vec3(fxl_inv_1d(o, 0u, c.x), fxl_inv_1d(o, 1u, c.y), fxl_inv_1d(o, 2u, c.z)); + // One search call for all three channels, as in fxl_shaper_apply. + for (var k = 0u; k < 3u; k++) { + let lane = vec3(k == 0u, k == 1u, k == 2u); + let v = fxl_inv_1d(o, k, select(select(c.z, c.y, k == 1u), c.x, k == 0u)); + x = select(x, vec3(v), lane); + } } if (sh >= 0) { x = fxl_shaper_apply(u32(sh), x, true);