Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions core/k/kernels/nv12.k
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,39 @@ nuv:(w*h)%8
p:(4*i)&(w*h)-4
ly:p%w
lx:p!w
/ dither: a 4x4 ordered (Bayer) offset in place of the +128 rounding, mean 128, fixed per pixel so a render is
/ still a function of its frame; 8-bit limited range bands slow dark gradients without it
yb0:and[ly;1]
yb1:and[shr[ly;1];1]
e0:xor[and[lx+0;3];and[ly;3]]
o0:(16*((8*and[e0;1])+((4*yb0)+((2*and[shr[e0;1];1])+yb1))))+8
e1:xor[and[lx+1;3];and[ly;3]]
o1:(16*((8*and[e1;1])+((4*yb0)+((2*and[shr[e1;1];1])+yb1))))+8
e2:xor[and[lx+2;3];and[ly;3]]
o2:(16*((8*and[e2;1])+((4*yb0)+((2*and[shr[e2;1];1])+yb1))))+8
e3:xor[and[lx+3;3];and[ly;3]]
o3:(16*((8*and[e3;1])+((4*yb0)+((2*and[shr[e3;1];1])+yb1))))+8
c0:tex[T;lx+0;ly]
y0:clamp[16+shr[(47*and[c0;255])+(157*and[shr[c0;8];255])+(16*and[shr[c0;16];255])+128;8];0;255]
y0:clamp[16+shr[(47*and[c0;255])+(157*and[shr[c0;8];255])+(16*and[shr[c0;16];255])+o0;8];0;255]
c1:tex[T;lx+1;ly]
y1:clamp[16+shr[(47*and[c1;255])+(157*and[shr[c1;8];255])+(16*and[shr[c1;16];255])+128;8];0;255]
y1:clamp[16+shr[(47*and[c1;255])+(157*and[shr[c1;8];255])+(16*and[shr[c1;16];255])+o1;8];0;255]
c2:tex[T;lx+2;ly]
y2:clamp[16+shr[(47*and[c2;255])+(157*and[shr[c2;8];255])+(16*and[shr[c2;16];255])+128;8];0;255]
y2:clamp[16+shr[(47*and[c2;255])+(157*and[shr[c2;8];255])+(16*and[shr[c2;16];255])+o2;8];0;255]
c3:tex[T;lx+3;ly]
y3:clamp[16+shr[(47*and[c3;255])+(157*and[shr[c3;8];255])+(16*and[shr[c3;16];255])+128;8];0;255]
y3:clamp[16+shr[(47*and[c3;255])+(157*and[shr[c3;8];255])+(16*and[shr[c3;16];255])+o3;8];0;255]
yw:or[or[y0;shl[y1;8]];or[shl[y2;16];shl[y3;24]]]
/ chroma: samples 2j and 2j+1 of the (w/2) x (h/2) grid, j counting from the first chroma word
j:0|(i-ny)&nuv-1
s:2*j
cr:s%(w%2)
cc:s!(w%2)
/ chroma's dither: the same pattern on the chroma grid, mean 512 for the 2x2 sum's shift by 10
cb0:and[cr;1]
cb1:and[shr[cr;1];1]
f0:xor[and[cc+0;3];and[cr;3]]
q0:(64*((8*and[f0;1])+((4*cb0)+((2*and[shr[f0;1];1])+cb1))))+32
f1:xor[and[cc+1;3];and[cr;3]]
q1:(64*((8*and[f1;1])+((4*cb0)+((2*and[shr[f1;1];1])+cb1))))+32
x0:2*cc+0
t00:tex[T;x0;2*cr]
t01:tex[T;x0+1;2*cr]
Expand All @@ -29,8 +48,8 @@ t03:tex[T;x0+1;(2*cr)+1]
r0:and[t00;255]+and[t01;255]+and[t02;255]+and[t03;255]
g0:and[shr[t00;8];255]+and[shr[t01;8];255]+and[shr[t02;8];255]+and[shr[t03;8];255]
b0:and[shr[t00;16];255]+and[shr[t01;16];255]+and[shr[t02;16];255]+and[shr[t03;16];255]
u0:clamp[128+shr[(-26*r0)+(-87*g0)+(112*b0)+512;10];0;255]
v0:clamp[128+shr[(112*r0)+(-102*g0)+(-10*b0)+512;10];0;255]
u0:clamp[128+shr[(-26*r0)+(-87*g0)+(112*b0)+q0;10];0;255]
v0:clamp[128+shr[(112*r0)+(-102*g0)+(-10*b0)+q0;10];0;255]
x1:2*cc+1
t10:tex[T;x1;2*cr]
t11:tex[T;x1+1;2*cr]
Expand All @@ -39,7 +58,7 @@ t13:tex[T;x1+1;(2*cr)+1]
r1:and[t10;255]+and[t11;255]+and[t12;255]+and[t13;255]
g1:and[shr[t10;8];255]+and[shr[t11;8];255]+and[shr[t12;8];255]+and[shr[t13;8];255]
b1:and[shr[t10;16];255]+and[shr[t11;16];255]+and[shr[t12;16];255]+and[shr[t13;16];255]
u1:clamp[128+shr[(-26*r1)+(-87*g1)+(112*b1)+512;10];0;255]
v1:clamp[128+shr[(112*r1)+(-102*g1)+(-10*b1)+512;10];0;255]
u1:clamp[128+shr[(-26*r1)+(-87*g1)+(112*b1)+q1;10];0;255]
v1:clamp[128+shr[(112*r1)+(-102*g1)+(-10*b1)+q1;10];0;255]
uvw:or[or[u0;shl[v0;8]];or[shl[u1;16];shl[v1;24]]]
px:sel[i<ny;yw;sel[i<ny+nuv;uvw;tex[T;8;h]]]
35 changes: 27 additions & 8 deletions core/k/kernels/nv12buf.k
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,39 @@ nuv:(w*h)%8
p:(4*i)&(w*h)-4
ly:p%w
lx:p!w
/ dither: a 4x4 ordered (Bayer) offset in place of the +128 rounding, mean 128, fixed per pixel so a render is
/ still a function of its frame; 8-bit limited range bands slow dark gradients without it
yb0:and[ly;1]
yb1:and[shr[ly;1];1]
e0:xor[and[lx+0;3];and[ly;3]]
o0:(16*((8*and[e0;1])+((4*yb0)+((2*and[shr[e0;1];1])+yb1))))+8
e1:xor[and[lx+1;3];and[ly;3]]
o1:(16*((8*and[e1;1])+((4*yb0)+((2*and[shr[e1;1];1])+yb1))))+8
e2:xor[and[lx+2;3];and[ly;3]]
o2:(16*((8*and[e2;1])+((4*yb0)+((2*and[shr[e2;1];1])+yb1))))+8
e3:xor[and[lx+3;3];and[ly;3]]
o3:(16*((8*and[e3;1])+((4*yb0)+((2*and[shr[e3;1];1])+yb1))))+8
c0:word[F;((ly)*w)+lx+0]
y0:clamp[16+shr[(47*and[c0;255])+(157*and[shr[c0;8];255])+(16*and[shr[c0;16];255])+128;8];0;255]
y0:clamp[16+shr[(47*and[c0;255])+(157*and[shr[c0;8];255])+(16*and[shr[c0;16];255])+o0;8];0;255]
c1:word[F;((ly)*w)+lx+1]
y1:clamp[16+shr[(47*and[c1;255])+(157*and[shr[c1;8];255])+(16*and[shr[c1;16];255])+128;8];0;255]
y1:clamp[16+shr[(47*and[c1;255])+(157*and[shr[c1;8];255])+(16*and[shr[c1;16];255])+o1;8];0;255]
c2:word[F;((ly)*w)+lx+2]
y2:clamp[16+shr[(47*and[c2;255])+(157*and[shr[c2;8];255])+(16*and[shr[c2;16];255])+128;8];0;255]
y2:clamp[16+shr[(47*and[c2;255])+(157*and[shr[c2;8];255])+(16*and[shr[c2;16];255])+o2;8];0;255]
c3:word[F;((ly)*w)+lx+3]
y3:clamp[16+shr[(47*and[c3;255])+(157*and[shr[c3;8];255])+(16*and[shr[c3;16];255])+128;8];0;255]
y3:clamp[16+shr[(47*and[c3;255])+(157*and[shr[c3;8];255])+(16*and[shr[c3;16];255])+o3;8];0;255]
yw:or[or[y0;shl[y1;8]];or[shl[y2;16];shl[y3;24]]]
/ chroma: samples 2j and 2j+1 of the (w/2) x (h/2) grid, j counting from the first chroma word
j:0|(i-ny)&nuv-1
s:2*j
cr:s%(w%2)
cc:s!(w%2)
/ chroma's dither: the same pattern on the chroma grid, mean 512 for the 2x2 sum's shift by 10
cb0:and[cr;1]
cb1:and[shr[cr;1];1]
f0:xor[and[cc+0;3];and[cr;3]]
q0:(64*((8*and[f0;1])+((4*cb0)+((2*and[shr[f0;1];1])+cb1))))+32
f1:xor[and[cc+1;3];and[cr;3]]
q1:(64*((8*and[f1;1])+((4*cb0)+((2*and[shr[f1;1];1])+cb1))))+32
x0:2*cc+0
t00:word[F;((2*cr)*w)+x0]
t01:word[F;((2*cr)*w)+x0+1]
Expand All @@ -29,8 +48,8 @@ t03:word[F;(((2*cr)+1)*w)+x0+1]
r0:and[t00;255]+and[t01;255]+and[t02;255]+and[t03;255]
g0:and[shr[t00;8];255]+and[shr[t01;8];255]+and[shr[t02;8];255]+and[shr[t03;8];255]
b0:and[shr[t00;16];255]+and[shr[t01;16];255]+and[shr[t02;16];255]+and[shr[t03;16];255]
u0:clamp[128+shr[(-26*r0)+(-87*g0)+(112*b0)+512;10];0;255]
v0:clamp[128+shr[(112*r0)+(-102*g0)+(-10*b0)+512;10];0;255]
u0:clamp[128+shr[(-26*r0)+(-87*g0)+(112*b0)+q0;10];0;255]
v0:clamp[128+shr[(112*r0)+(-102*g0)+(-10*b0)+q0;10];0;255]
x1:2*cc+1
t10:word[F;((2*cr)*w)+x1]
t11:word[F;((2*cr)*w)+x1+1]
Expand All @@ -39,7 +58,7 @@ t13:word[F;(((2*cr)+1)*w)+x1+1]
r1:and[t10;255]+and[t11;255]+and[t12;255]+and[t13;255]
g1:and[shr[t10;8];255]+and[shr[t11;8];255]+and[shr[t12;8];255]+and[shr[t13;8];255]
b1:and[shr[t10;16];255]+and[shr[t11;16];255]+and[shr[t12;16];255]+and[shr[t13;16];255]
u1:clamp[128+shr[(-26*r1)+(-87*g1)+(112*b1)+512;10];0;255]
v1:clamp[128+shr[(112*r1)+(-102*g1)+(-10*b1)+512;10];0;255]
u1:clamp[128+shr[(-26*r1)+(-87*g1)+(112*b1)+q1;10];0;255]
v1:clamp[128+shr[(112*r1)+(-102*g1)+(-10*b1)+q1;10];0;255]
uvw:or[or[u0;shl[v0;8]];or[shl[u1;16];shl[v1;24]]]
px:sel[i<ny;yw;sel[i<ny+nuv;uvw;tex[T;8;h]]]
72 changes: 67 additions & 5 deletions core/k/src/native.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
//! The render path's kernels as plain Rust, bit-equal to the K text (the CPU render, docs/cpu.feature C1).

/// kernels/nv12.k as plain Rust, on `threads` threads (the CPU render, docs/cpu.feature C1): the same fixed-point
/// formula per output word, the words split into bands; bit-equal.
/// formula per output word (with the kernel's ordered dither), the words split into bands; bit-equal.
pub fn nv12_cpu(rgba: &[u32], w: usize, h: usize, threads: usize) -> Vec<u32> {
let ch = |c: u32, s: u32| ((c >> s) & 255) as i32;
let luma = |c: u32| (16 + ((47 * ch(c, 0) + 157 * ch(c, 8) + 16 * ch(c, 16) + 128) >> 8)).clamp(0, 255) as u32;
// the kernel's 4x4 ordered dither (in place of a fixed +128 / +512 rounding): 0..15 by (x, y) mod 4
let bayer = |x: usize, y: usize| -> i32 {
let e = (x & 3) ^ (y & 3);
(8 * (e & 1) + 4 * (y & 1) + 2 * ((e >> 1) & 1) + ((y >> 1) & 1)) as i32
};
let luma = |c: u32, x: usize, y: usize| (16 + ((47 * ch(c, 0) + 157 * ch(c, 8) + 16 * ch(c, 16) + 16 * bayer(x, y) + 8) >> 8)).clamp(0, 255) as u32;
let (ny, nuv) = (w * h / 4, w * h / 8);
let mut out = vec![0u32; ny + nuv + 1];
let cw = w / 2;
Expand All @@ -13,7 +18,7 @@ pub fn nv12_cpu(rgba: &[u32], w: usize, h: usize, threads: usize) -> Vec<u32> {
let p = 4 * j;
let (y, x) = (p / w, p % w);
let px = |d: usize| rgba[y * w + x + d];
return luma(px(0)) | luma(px(1)) << 8 | luma(px(2)) << 16 | luma(px(3)) << 24;
return luma(px(0), x, y) | luma(px(1), x + 1, y) << 8 | luma(px(2), x + 2, y) << 16 | luma(px(3), x + 3, y) << 24;
}
let j = j - ny;
let mut wd = 0u32;
Expand All @@ -23,8 +28,9 @@ pub fn nv12_cpu(rgba: &[u32], w: usize, h: usize, threads: usize) -> Vec<u32> {
let t = [rgba[2 * cr * w + 2 * cc], rgba[2 * cr * w + 2 * cc + 1], rgba[(2 * cr + 1) * w + 2 * cc], rgba[(2 * cr + 1) * w + 2 * cc + 1]];
let sum = |sh: u32| t.iter().map(|c| ch(*c, sh)).sum::<i32>();
let (r, g, b) = (sum(0), sum(8), sum(16));
let u = (128 + ((-26 * r - 87 * g + 112 * b + 512) >> 10)).clamp(0, 255) as u32;
let v = (128 + ((112 * r - 102 * g - 10 * b + 512) >> 10)).clamp(0, 255) as u32;
let q = 64 * bayer(cc, cr) + 32;
let u = (128 + ((-26 * r - 87 * g + 112 * b + q) >> 10)).clamp(0, 255) as u32;
let v = (128 + ((112 * r - 102 * g - 10 * b + q) >> 10)).clamp(0, 255) as u32;
wd |= (u | v << 8) << (16 * k);
}
wd
Expand Down Expand Up @@ -214,6 +220,62 @@ pub fn composite_cpu_masked(bg: &[u32], w: usize, h: usize, planes: &[&[u8]], sl
out
}

#[cfg(test)]
mod nv12_tests {
use crate::*;
use std::collections::HashMap;

fn frame(w: usize, h: usize) -> Vec<u32> {
let mut s = 0x9e37_79b9_7f4a_7c15u64;
(0..w * (h + 1))
.map(|_| {
s ^= s << 13;
s ^= s >> 7;
s ^= s << 17;
(s as u32) | 0xff00_0000
})
.collect()
}

/// kernels/nv12.k and nv12buf.k (through the interpreter, which the GPU matches bit for bit) equal nv12_cpu.
#[test]
fn nv12_kernels_equal_native() {
let (w, h) = (64usize, 34usize);
let words = frame(w, h);
let sc: HashMap<&str, i32> = [("w", w as i32), ("h", h as i32), ("T_w", w as i32)].into();
let n = w * h / 4 + w * h / 8 + 1;
// nv12.k reads texture T (w wide, h + 1 rows); nv12buf.k reads buffer F and the tag from T
let cpu = Cpu { buffers: [("F", &words[..]), ("T", &words[..])].into(), scalars: sc };
let want = nv12_cpu(&words, w, h, 3);
for src in [include_str!("../kernels/nv12.k"), include_str!("../kernels/nv12buf.k")] {
let got = cpu.run(&parse(src).unwrap(), n, &["px"]).unwrap().into_iter().next().unwrap();
assert_eq!(got, want);
}
}

/// A slow dark ramp dithers to a mix of neighbouring codes whose mean tracks the ramp, where rounding gave flat steps.
#[test]
fn a_dark_ramp_keeps_its_mean_and_loses_its_steps() {
let (w, h) = (256usize, 16usize);
// grey 10.0 to 14.0 across the frame in 8.8 steps of the input: each input level a 64-px band
let f: Vec<u32> = (0..w * (h + 1)).map(|i| { let g = 10 + ((i % w) as u32) / 64; 0xff00_0000 | g << 16 | g << 8 | g }).collect();
let out = nv12_cpu(&f, w, h, 2);
let y: Vec<u8> = bytemuck_like(&out[..w * h / 4]);
// inside one input band the luma may take two codes (the dither) but its mean stays the rounded value
for band in 0..4 {
let vals: Vec<f64> = (0..h).flat_map(|r| (band * 64..band * 64 + 64).map(move |x| (r, x))).map(|(r, x)| y[r * w + x] as f64).collect();
let mean = vals.iter().sum::<f64>() / vals.len() as f64;
let g = (10 + band) as f64;
let exact = 16.0 + (47.0 * g + 157.0 * g + 16.0 * g) / 256.0;
assert!((mean - exact).abs() < 0.1, "band {band}: mean {mean} vs {exact}");
}
}

fn bytemuck_like(w: &[u32]) -> Vec<u8> {
w.iter().flat_map(|x| x.to_le_bytes()).collect()
}
}

#[cfg(test)]
mod p010_tests {
use crate::*;
Expand Down
Loading