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.

A video's bright parts come out whole on GPUs whose compilers treat a signed shift into the sign bit as undefined. nv12.k packs four luma bytes (and two chroma pairs) into a word with shl, the top byte by 24, and the WGSL shifted i32: a byte of 128 or more overflows into the sign bit, which WGSL defines but a driver's compiler (a Windows on ARM GPU's, #82) didn't, so every 4 pixels of a bright region came out as two right and two at 255, white stripes over the whole video (the reporter's luma plane: 29,489 of 29,495 words whose second byte is 128+ have both top bytes 255; none below). K's shl now shifts as u32 and bitcasts back, the same bits as the interpreter's wrapping_shl, so nv12.k, nv12buf.k and p010buf.k (whose 22-bit shift overflows the same way) are unchanged. 1 test: the WGSL shifts unsigned, and on the GPU all 256 bytes pack as the interpreter packs them.

shinyobjectz05c9788672bf
diff --git a/systems/k/src/lib.rs b/systems/k/src/lib.rs
index 24d372b0..d4c08385 100644
--- a/systems/k/src/lib.rs
+++ b/systems/k/src/lib.rs
@@ -282,4 +282,23 @@ mod tests {
let got = gpu.run(&c, &[&words], frames * frames).unwrap().0.remove(0);
assert_eq!(got, want);
}
+
+ /// nv12.k packs four bytes with shl, the top one into the sign bit: the WGSL shifts as u32 (a signed shift there
+ /// striped bright video on a Windows on ARM GPU, #82), and every byte value packs as the interpreter packs it.
+ #[test]
+ fn shl_into_the_sign_bit_is_unsigned() {
+ let k = raea_core_k::parse("b:and[word[F;i];255]\nw:or[or[b;shl[b;8]];or[shl[b;16];shl[b;24]]]").unwrap();
+ let src = wgsl_tex(&k, &["F"], &[], &HashMap::new(), &["w"]).unwrap();
+ assert!(src.contains("bitcast<i32>(u32(k_b) << (u32((24)) & 31u))"), "{src}");
+ if !Gpu::available() {
+ return;
+ }
+ let gpu = Gpu::new().unwrap();
+ let words: Vec<u32> = (0..256).collect();
+ let cpu = raea_core_k::Cpu { buffers: [("F", &words[..])].into(), scalars: HashMap::new() };
+ let want = cpu.run(&k, 256, &["w"]).unwrap().remove(0);
+ assert_eq!(want[182], 0xb6b6_b6b6);
+ let c = gpu.compile(&k, &["F"], &HashMap::new(), &["w"]).unwrap();
+ assert_eq!(gpu.run(&c, &[&words], 256).unwrap().0.remove(0), want);
+ }
}
diff --git a/systems/k/src/wgsl.rs b/systems/k/src/wgsl.rs
index 02a15c25..fdb4bb11 100644
--- a/systems/k/src/wgsl.rs
+++ b/systems/k/src/wgsl.rs
@@ -74,7 +74,10 @@ impl Gen<'_> {
"xor" => format!("({} ^ {})", self.e(&a[0])?, self.e(&a[1])?),
"or" => format!("({} | {})", self.e(&a[0])?, self.e(&a[1])?),
"and" => format!("({} & {})", self.e(&a[0])?, self.e(&a[1])?),
- "shl" => format!("({} << (u32({}) & 31u))", self.e(&a[0])?, self.e(&a[1])?),
+ // shifted as u32: a byte shifted into the sign bit (an NV12 word's top byte >= 128) is an
+ // overflow some drivers' compilers treat as undefined; on a Windows on ARM GPU it striped every
+ // bright 4 pixels of a video (#82). Same bits as the interpreter's wrapping_shl.
+ "shl" => format!("bitcast<i32>(u32({}) << (u32({}) & 31u))", self.e(&a[0])?, self.e(&a[1])?),
"shr" => format!("({} >> (u32({}) & 31u))", self.e(&a[0])?, self.e(&a[1])?),
"clamp" => format!("clamp({}, {}, {})", self.e(&a[0])?, self.e(&a[1])?, self.e(&a[2])?),
"sel" => {