Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

Encode: the RGB to NV12 step dithers through a 4x4 Bayer threshold, so dark gradients stop banding in 8-bit H.264 (#87)

shinyobjectz6177e27d5128
diff --git a/core/k/kernels/nv12.k b/core/k/kernels/nv12.k
index 02d94ed0..3f7701ad 100644
--- a/core/k/kernels/nv12.k
+++ b/core/k/kernels/nv12.k
@@ -1,6 +1,10 @@
/ RGBA (texture T, w x h, plus a frame-tag texel at (8, h)) to NV12, BT.709 limited range in 8.8 fixed point.
/ One output word per thread: four luma bytes, or two interleaved chroma pairs (each the mean of a 2x2 block),
/ or, last, the tag texel. Coordinates are clamped so the branch not taken stays in bounds.
+/ Each sample is rounded through a 4x4 Bayer threshold of its pixel (a chroma pair's, of its block's top-left pixel) in
+/ place of half-up rounding, so a slow dark gradient steps every pixel instead of every band (#87): the same
+/ threshold in play and render, and in nv12_cpu (core/k/src/native.rs), which stays bit-equal. Samples are clamped to the
+/ nominal range (luma 16..235, chroma 16..240): 220/256 overshoots 219/255, and the dither would put some white on 236.
ny:(w*h)%4
nuv:(w*h)%8
/ luma: pixels 4i..4i+3
@@ -8,13 +12,21 @@ p:(4*i)&(w*h)-4
ly:p%w
lx:p!w
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]
+a0:xor[lx+0;ly]
+d0:or[or[shl[and[a0;1];3];shl[and[ly;1];2]];or[and[a0;2];shr[and[ly;2];1]]]
+y0:clamp[16+shr[(47*and[c0;255])+(157*and[shr[c0;8];255])+(16*and[shr[c0;16];255])+8+(16*d0);8];16;235]
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]
+a1:xor[lx+1;ly]
+d1:or[or[shl[and[a1;1];3];shl[and[ly;1];2]];or[and[a1;2];shr[and[ly;2];1]]]
+y1:clamp[16+shr[(47*and[c1;255])+(157*and[shr[c1;8];255])+(16*and[shr[c1;16];255])+8+(16*d1);8];16;235]
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]
+a2:xor[lx+2;ly]
+d2:or[or[shl[and[a2;1];3];shl[and[ly;1];2]];or[and[a2;2];shr[and[ly;2];1]]]
+y2:clamp[16+shr[(47*and[c2;255])+(157*and[shr[c2;8];255])+(16*and[shr[c2;16];255])+8+(16*d2);8];16;235]
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]
+a3:xor[lx+3;ly]
+d3:or[or[shl[and[a3;1];3];shl[and[ly;1];2]];or[and[a3;2];shr[and[ly;2];1]]]
+y3:clamp[16+shr[(47*and[c3;255])+(157*and[shr[c3;8];255])+(16*and[shr[c3;16];255])+8+(16*d3);8];16;235]
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
@@ -29,8 +41,10 @@ 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]
+aq0:xor[x0;2*cr]
+dq0:or[or[shl[and[aq0;1];3];shl[and[2*cr;1];2]];or[and[aq0;2];shr[and[2*cr;2];1]]]
+u0:clamp[128+shr[(-26*r0)+(-87*g0)+(112*b0)+32+(64*dq0);10];16;240]
+v0:clamp[128+shr[(112*r0)+(-102*g0)+(-10*b0)+32+(64*dq0);10];16;240]
x1:2*cc+1
t10:tex[T;x1;2*cr]
t11:tex[T;x1+1;2*cr]
@@ -39,7 +53,9 @@ 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]
+aq1:xor[x1;2*cr]
+dq1:or[or[shl[and[aq1;1];3];shl[and[2*cr;1];2]];or[and[aq1;2];shr[and[2*cr;2];1]]]
+u1:clamp[128+shr[(-26*r1)+(-87*g1)+(112*b1)+32+(64*dq1);10];16;240]
+v1:clamp[128+shr[(112*r1)+(-102*g1)+(-10*b1)+32+(64*dq1);10];16;240]
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]]]
diff --git a/core/k/kernels/nv12buf.k b/core/k/kernels/nv12buf.k
index 91c9a576..fa32dda4 100644
--- a/core/k/kernels/nv12buf.k
+++ b/core/k/kernels/nv12buf.k
@@ -1,6 +1,10 @@
/ RGBA (buffer F, w x h words; the frame tag is texel (8, h) of texture T; w x h, plus a frame-tag texel at (8, h)) to NV12, BT.709 limited range in 8.8 fixed point.
/ One output word per thread: four luma bytes, or two interleaved chroma pairs (each the mean of a 2x2 block),
/ or, last, the tag texel. Coordinates are clamped so the branch not taken stays in bounds.
+/ Each sample is rounded through a 4x4 Bayer threshold of its pixel (a chroma pair's, of its block's top-left pixel) in
+/ place of half-up rounding, so a slow dark gradient steps every pixel instead of every band (#87): the same
+/ threshold in play and render, and in nv12_cpu (core/k/src/native.rs), which stays bit-equal. Samples are clamped to the
+/ nominal range (luma 16..235, chroma 16..240): 220/256 overshoots 219/255, and the dither would put some white on 236.
ny:(w*h)%4
nuv:(w*h)%8
/ luma: pixels 4i..4i+3
@@ -8,13 +12,21 @@ p:(4*i)&(w*h)-4
ly:p%w
lx:p!w
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]
+a0:xor[lx+0;ly]
+d0:or[or[shl[and[a0;1];3];shl[and[ly;1];2]];or[and[a0;2];shr[and[ly;2];1]]]
+y0:clamp[16+shr[(47*and[c0;255])+(157*and[shr[c0;8];255])+(16*and[shr[c0;16];255])+8+(16*d0);8];16;235]
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]
+a1:xor[lx+1;ly]
+d1:or[or[shl[and[a1;1];3];shl[and[ly;1];2]];or[and[a1;2];shr[and[ly;2];1]]]
+y1:clamp[16+shr[(47*and[c1;255])+(157*and[shr[c1;8];255])+(16*and[shr[c1;16];255])+8+(16*d1);8];16;235]
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]
+a2:xor[lx+2;ly]
+d2:or[or[shl[and[a2;1];3];shl[and[ly;1];2]];or[and[a2;2];shr[and[ly;2];1]]]
+y2:clamp[16+shr[(47*and[c2;255])+(157*and[shr[c2;8];255])+(16*and[shr[c2;16];255])+8+(16*d2);8];16;235]
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]
+a3:xor[lx+3;ly]
+d3:or[or[shl[and[a3;1];3];shl[and[ly;1];2]];or[and[a3;2];shr[and[ly;2];1]]]
+y3:clamp[16+shr[(47*and[c3;255])+(157*and[shr[c3;8];255])+(16*and[shr[c3;16];255])+8+(16*d3);8];16;235]
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
@@ -29,8 +41,10 @@ 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]
+aq0:xor[x0;2*cr]
+dq0:or[or[shl[and[aq0;1];3];shl[and[2*cr;1];2]];or[and[aq0;2];shr[and[2*cr;2];1]]]
+u0:clamp[128+shr[(-26*r0)+(-87*g0)+(112*b0)+32+(64*dq0);10];16;240]
+v0:clamp[128+shr[(112*r0)+(-102*g0)+(-10*b0)+32+(64*dq0);10];16;240]
x1:2*cc+1
t10:word[F;((2*cr)*w)+x1]
t11:word[F;((2*cr)*w)+x1+1]
@@ -39,7 +53,9 @@ 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]
+aq1:xor[x1;2*cr]
+dq1:or[or[shl[and[aq1;1];3];shl[and[2*cr;1];2]];or[and[aq1;2];shr[and[2*cr;2];1]]]
+u1:clamp[128+shr[(-26*r1)+(-87*g1)+(112*b1)+32+(64*dq1);10];16;240]
+v1:clamp[128+shr[(112*r1)+(-102*g1)+(-10*b1)+32+(64*dq1);10];16;240]
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]]]
diff --git a/core/k/src/native.rs b/core/k/src/native.rs
index 79dcbe9c..5878c283 100644
--- a/core/k/src/native.rs
+++ b/core/k/src/native.rs
@@ -4,7 +4,12 @@
/// formula per output word, 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 4x4 Bayer threshold of pixel (x, y): 0..15, in place of half-up rounding (nv12.k's d0..d3, dq0, dq1; #87)
+ let bayer = |x: usize, y: usize| -> i32 {
+ let (a, y) = ((x ^ y) as i32, y as i32);
+ (a & 1) << 3 | (y & 1) << 2 | (a & 2) | (y & 2) >> 1
+ };
+ let luma = |c: u32, x: usize, y: usize| (16 + ((47 * ch(c, 0) + 157 * ch(c, 8) + 16 * ch(c, 16) + 8 + 16 * bayer(x, y)) >> 8)).clamp(16, 235) as u32;
let (ny, nuv) = (w * h / 4, w * h / 8);
let mut out = vec![0u32; ny + nuv + 1];
let cw = w / 2;
@@ -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;
@@ -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 d = 32 + 64 * bayer(2 * cc, 2 * cr);
+ let u = (128 + ((-26 * r - 87 * g + 112 * b + d) >> 10)).clamp(16, 240) as u32;
+ let v = (128 + ((112 * r - 102 * g - 10 * b + d) >> 10)).clamp(16, 240) as u32;
wd |= (u | v << 8) << (16 * k);
}
wd
diff --git a/core/k/tests/nv12_dither.rs b/core/k/tests/nv12_dither.rs
new file mode 100644
index 00000000..1855368b
--- /dev/null
+++ b/core/k/tests/nv12_dither.rs
@@ -0,0 +1,59 @@
+//! The RGB to NV12 kernels round each sample through a 4x4 Bayer threshold of its pixel (#87: half-up rounding into
+//! limited range banded every slow dark gradient), and nv12_cpu (the CPU render's twin) stays bit-equal to both the
+//! texture kernel (nv12.k) and the buffer kernel (nv12buf.k), run here through the K interpreter.
+use std::collections::HashMap;
+
+use raea_core_k::{Cpu, nv12_cpu, parse};
+
+const NV12_K: &str = include_str!("../kernels/nv12.k");
+const NV12BUF_K: &str = include_str!("../kernels/nv12buf.k");
+
+/// a w x (h + 1) frame of packed RGBA words from a small generator, the last row the frame tag's
+fn frame(w: usize, h: usize, seed: u32) -> Vec<u32> {
+ let mut s = seed;
+ (0..w * (h + 1))
+ .map(|_| {
+ s ^= s << 13;
+ s ^= s >> 17;
+ s ^= s << 5;
+ s
+ })
+ .collect()
+}
+
+fn through(k: &str, rgba: &[u32], w: usize, h: usize) -> Vec<u32> {
+ let k = parse(k).unwrap();
+ let cpu = Cpu { buffers: HashMap::from([("T", rgba), ("F", rgba)]), scalars: HashMap::from([("w", w as i32), ("h", h as i32), ("T_w", w as i32)]) };
+ cpu.run(&k, w * h / 4 + w * h / 8 + 1, &["px"]).unwrap().remove(0)
+}
+
+#[test]
+fn both_kernels_match_the_cpu_twin_bit_for_bit() {
+ for (w, h, seed) in [(16, 8, 1u32), (24, 12, 7), (64, 36, 99)] {
+ let rgba = frame(w, h, seed);
+ let want = nv12_cpu(&rgba, w, h, 3);
+ assert_eq!(through(NV12_K, &rgba, w, h), want, "nv12.k at {w}x{h}");
+ assert_eq!(through(NV12BUF_K, &rgba, w, h), want, "nv12buf.k at {w}x{h}");
+ }
+}
+
+/// A flat dark grey keeps its mean through the dither: the luma of RGB 10 is 24.63 in limited range, so a 4x4 block
+/// lands on 24 and 25 in a ratio that averages to it, not on one band.
+#[test]
+fn a_flat_dark_grey_keeps_its_mean_and_spans_two_levels() {
+ let (w, h) = (16, 8);
+ let rgba = vec![0xff0a0a0au32; w * (h + 1)];
+ let out = nv12_cpu(&rgba, w, h, 1);
+ let luma: Vec<u32> = out[..w * h / 4].iter().flat_map(|wd| [wd & 255, wd >> 8 & 255, wd >> 16 & 255, wd >> 24]).collect();
+ let mean = luma.iter().sum::<u32>() as f64 / luma.len() as f64;
+ assert!((mean - 24.627).abs() < 0.1, "mean {mean}");
+ let mut levels: Vec<u32> = luma.clone();
+ levels.sort_unstable();
+ levels.dedup();
+ assert_eq!(levels, [24, 25]);
+ // and black stays black, white stays white: the dither never lifts the ends of the range
+ let black = nv12_cpu(&vec![0xff000000u32; w * (h + 1)], w, h, 1);
+ assert!(black[..w * h / 4].iter().all(|wd| *wd == 0x10101010), "black luma");
+ let white = nv12_cpu(&vec![0xffffffffu32; w * (h + 1)], w, h, 1);
+ assert!(white[..w * h / 4].iter().all(|wd| *wd == 0xebebebeb), "white luma");
+}