mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-24 11:21:07 +01:00
fix(gpu): bring the colour kernel's cold compile time back to release level (#4111)
The fixed 32-step searches from3678cc9beandf0a5783a8were unrolled at every inlined call, so software Vulkan drivers took long enough compiling fxl_point to miss the 10 s readback deadline. Each search now has one call site and runs a host-computed trip count that is the same for every invocation: still bounded, non-divergent and without dynamic lane writes.
This commit is contained in:
1 parent
47391863ac
commit
e17252b8a3
2 files changed
+91
-18
No files matched your search
@@ -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<GBuf> {
|
||||
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<f32>, lut: &Lut) -> f32 {
|
||||
at
|
||||
}
|
||||
|
||||
/// Program layout: a stream of (opcode, arguments…) ending in 0, followed by LUT data.
|
||||
fn encode(ops: &[ColorOp]) -> Option<Vec<f32>> {
|
||||
/// 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<f32>, u32)> {
|
||||
if ops.len() > MAX_OPS {
|
||||
return None;
|
||||
}
|
||||
let mut steps = 0;
|
||||
let mut prog: Vec<f32> = vec![];
|
||||
let mut tables: Vec<Vec<f32>> = vec![];
|
||||
// LUT offsets are patched once the program's length is known.
|
||||
@@ -142,6 +157,13 @@ fn encode(ops: &[ColorOp]) -> Option<Vec<f32>> {
|
||||
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<Vec<f32>> {
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -120,6 +120,14 @@ fn mix_exact(a: vec3<f32>, b: vec3<f32>, t: f32) -> vec3<f32> {
|
||||
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<u32> {
|
||||
|
||||
// Evaluate independent shaper channels without dynamically writing vector lanes.
|
||||
fn fxl_shaper_apply(sh: u32, c: vec3<f32>, flip: bool) -> vec3<f32> {
|
||||
let r = fxl_shaper_at(sh, 0u);
|
||||
let g = fxl_shaper_at(sh, 1u);
|
||||
let b = fxl_shaper_at(sh, 2u);
|
||||
return vec3<f32>(
|
||||
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<bool>(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<f32>(v), lane);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
fn fxl_file_fwd(o: u32, sh: i32, c0: vec3<f32>, interp: u32) -> vec3<f32> {
|
||||
@@ -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<f32>, interp: u32, inverse: bool) -> vec3<f
|
||||
if (n3 == 0u) {
|
||||
var x = c;
|
||||
if (n1 > 1u) {
|
||||
x = vec3<f32>(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<bool>(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<f32>(v), lane);
|
||||
}
|
||||
}
|
||||
if (sh >= 0) {
|
||||
x = fxl_shaper_apply(u32(sh), x, true);
|
||||
|
||||
Reference in new issue
Block a user