|
| 1 | +//! Validation harness for the `combine` and `mix` algebra verbs (task #5), |
| 2 | +//! including the alias guard (a `dst` that aliases a read-only operand must be |
| 3 | +//! rejected in Rust, NOT reach wgpu — where it would be a fatal validation |
| 4 | +//! error, see `examples/alias_spike.rs`). |
| 5 | +//! |
| 6 | +//! Run: `cargo run --example combine_mix_test`. Exits non-zero on mismatch. |
| 7 | +
|
| 8 | +use processing::prelude::*; |
| 9 | + |
| 10 | +fn f32s(bytes: &[u8]) -> Vec<f32> { |
| 11 | + bytes |
| 12 | + .chunks_exact(4) |
| 13 | + .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])) |
| 14 | + .collect() |
| 15 | +} |
| 16 | +fn to_bytes(v: &[f32]) -> Vec<u8> { |
| 17 | + v.iter().flat_map(|f| f.to_le_bytes()).collect() |
| 18 | +} |
| 19 | +fn approx(a: &[f32], b: &[f32]) -> bool { |
| 20 | + a.len() == b.len() && a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-5) |
| 21 | +} |
| 22 | + |
| 23 | +fn sketch() -> error::Result<bool> { |
| 24 | + init(Config::default())?; |
| 25 | + let surface = surface_create_offscreen(1, 1, 1.0, TextureFormat::Rgba8Unorm)?; |
| 26 | + let _graphics = graphics_create(surface, 1, 1, TextureFormat::Rgba8Unorm)?; |
| 27 | + |
| 28 | + let mut ok = true; |
| 29 | + |
| 30 | + // --- combine in-place: a += b (Float3, 4 particles) --- |
| 31 | + let av: Vec<f32> = (0..12).map(|i| i as f32).collect(); |
| 32 | + let bv: Vec<f32> = (0..12).map(|i| (i * 10) as f32).collect(); |
| 33 | + let a = buffer_create_with_data(to_bytes(&av))?; |
| 34 | + let b = buffer_create_with_data(to_bytes(&bv))?; |
| 35 | + combine(a, a, b, 3, COMBINE_ADD, 1.0, 0.0)?; |
| 36 | + let got = f32s(&buffer_read(a)?); |
| 37 | + let want: Vec<f32> = av.iter().zip(&bv).map(|(x, y)| x + y).collect(); |
| 38 | + if approx(&got, &want) { |
| 39 | + println!(" PASS combine in-place a+=b"); |
| 40 | + } else { |
| 41 | + ok = false; |
| 42 | + println!(" FAIL combine in-place: got {got:?}, want {want:?}"); |
| 43 | + } |
| 44 | + |
| 45 | + // --- combine out-of-place: dst = a * b, inputs preserved --- |
| 46 | + let dst = buffer_create_with_data(to_bytes(&vec![0.0; 12]))?; |
| 47 | + combine(dst, a, b, 3, COMBINE_MUL, 1.0, 0.0)?; |
| 48 | + let got_dst = f32s(&buffer_read(dst)?); |
| 49 | + let cur_a = f32s(&buffer_read(a)?); |
| 50 | + let want_dst: Vec<f32> = want.iter().zip(&bv).map(|(x, y)| x * y).collect(); |
| 51 | + if approx(&got_dst, &want_dst) && approx(&cur_a, &want) { |
| 52 | + println!(" PASS combine out-of-place dst=a*b (inputs preserved)"); |
| 53 | + } else { |
| 54 | + ok = false; |
| 55 | + println!(" FAIL combine out-of-place: dst={got_dst:?} want {want_dst:?}"); |
| 56 | + } |
| 57 | + |
| 58 | + // --- alias guard: combine(dst=b, a, b) must be rejected in Rust --- |
| 59 | + match combine(b, a, b, 3, COMBINE_ADD, 1.0, 0.0) { |
| 60 | + Err(_) => println!(" PASS combine alias guard (dst==b rejected before dispatch)"), |
| 61 | + Ok(()) => { |
| 62 | + ok = false; |
| 63 | + println!(" FAIL combine alias guard: expected Err, got Ok"); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + buffer_destroy(a)?; |
| 68 | + buffer_destroy(b)?; |
| 69 | + buffer_destroy(dst)?; |
| 70 | + |
| 71 | + // --- mix out-of-place: dst = lerp(a, b, t), t per-particle broadcast --- |
| 72 | + // 3 particles x 4 components (color-like); t = [0, 0.5, 1]. |
| 73 | + let comps = 4u32; |
| 74 | + let a2: Vec<f32> = vec![1.0; 12]; // all-ones |
| 75 | + let b2: Vec<f32> = vec![3.0; 12]; // all-threes |
| 76 | + let tv = vec![0.0f32, 0.5, 1.0]; |
| 77 | + let am = buffer_create_with_data(to_bytes(&a2))?; |
| 78 | + let bm = buffer_create_with_data(to_bytes(&b2))?; |
| 79 | + let tm = buffer_create_with_data(to_bytes(&tv))?; |
| 80 | + let dm = buffer_create_with_data(to_bytes(&vec![0.0; 12]))?; |
| 81 | + mix(dm, am, bm, tm, comps, 1.0, 0.0, true)?; |
| 82 | + let got_mix = f32s(&buffer_read(dm)?); |
| 83 | + // particle 0 -> a(1.0), particle 1 -> midpoint(2.0), particle 2 -> b(3.0) |
| 84 | + let mut want_mix = vec![1.0f32; 4]; |
| 85 | + want_mix.extend([2.0f32; 4]); |
| 86 | + want_mix.extend([3.0f32; 4]); |
| 87 | + if approx(&got_mix, &want_mix) { |
| 88 | + println!(" PASS mix out-of-place (per-particle t broadcast across components)"); |
| 89 | + } else { |
| 90 | + ok = false; |
| 91 | + println!(" FAIL mix out-of-place: got {got_mix:?}, want {want_mix:?}"); |
| 92 | + } |
| 93 | + |
| 94 | + // --- mix in-place: a = lerp(a, b, t) --- |
| 95 | + mix(am, am, bm, tm, comps, 1.0, 0.0, true)?; |
| 96 | + let got_mix_ip = f32s(&buffer_read(am)?); |
| 97 | + if approx(&got_mix_ip, &want_mix) { |
| 98 | + println!(" PASS mix in-place"); |
| 99 | + } else { |
| 100 | + ok = false; |
| 101 | + println!(" FAIL mix in-place: got {got_mix_ip:?}, want {want_mix:?}"); |
| 102 | + } |
| 103 | + |
| 104 | + buffer_destroy(am)?; |
| 105 | + buffer_destroy(bm)?; |
| 106 | + buffer_destroy(tm)?; |
| 107 | + buffer_destroy(dm)?; |
| 108 | + |
| 109 | + Ok(ok) |
| 110 | +} |
| 111 | + |
| 112 | +fn main() { |
| 113 | + let ok = sketch().unwrap(); |
| 114 | + if ok { |
| 115 | + println!("combine_mix_test: ALL PASS"); |
| 116 | + exit(0).unwrap(); |
| 117 | + } else { |
| 118 | + println!("combine_mix_test: FAILURES"); |
| 119 | + exit(1).unwrap(); |
| 120 | + } |
| 121 | +} |
0 commit comments