|
| 1 | +#![no_std] |
| 2 | + |
| 3 | +use spirv_std::spirv; |
| 4 | + |
| 5 | +// Non-zero, so a fill that writes nothing is visible |
| 6 | +pub const INPUT: [u32; 4] = [1, 2, 3, 4]; |
| 7 | + |
| 8 | +pub const ROWS: usize = 9; |
| 9 | +pub const WORDS: usize = 6; |
| 10 | +// One row per fill, the contents of the aggregate after it |
| 11 | +pub type Output = [[u32; WORDS]; ROWS]; |
| 12 | + |
| 13 | +// `repr(C)` keeps the array at offset 0 |
| 14 | +#[repr(C)] |
| 15 | +#[derive(Clone, Copy)] |
| 16 | +pub struct First { |
| 17 | + pub arr: [u32; 4], |
| 18 | + pub tail: u32, |
| 19 | +} |
| 20 | + |
| 21 | +#[repr(C)] |
| 22 | +pub struct Nested { |
| 23 | + pub head: u32, |
| 24 | + pub first: First, |
| 25 | +} |
| 26 | + |
| 27 | +pub struct Only { |
| 28 | + pub arr: [u32; 4], |
| 29 | +} |
| 30 | + |
| 31 | +pub fn reset(s: &mut First) { |
| 32 | + s.arr = [0; 4]; |
| 33 | +} |
| 34 | + |
| 35 | +// Borrowed, so the struct is not split into separate variables |
| 36 | +fn first_words(s: &First) -> [u32; WORDS] { |
| 37 | + [s.arr[0], s.arr[1], s.arr[2], s.arr[3], s.tail, 0] |
| 38 | +} |
| 39 | + |
| 40 | +fn nested_words(n: &Nested) -> [u32; WORDS] { |
| 41 | + let [a0, a1, a2, a3] = n.first.arr; |
| 42 | + [n.head, a0, a1, a2, a3, n.first.tail] |
| 43 | +} |
| 44 | + |
| 45 | +fn only_words(s: &Only) -> [u32; WORDS] { |
| 46 | + [s.arr[0], s.arr[1], s.arr[2], s.arr[3], 0, 0] |
| 47 | +} |
| 48 | + |
| 49 | +fn table_words(t: &[[u32; 2]; 3]) -> [u32; WORDS] { |
| 50 | + [t[0][0], t[0][1], t[1][0], t[1][1], t[2][0], t[2][1]] |
| 51 | +} |
| 52 | + |
| 53 | +pub fn eval(input: &[u32; 4]) -> Output { |
| 54 | + let [a, b, c, d] = *input; |
| 55 | + let first = First { |
| 56 | + arr: [a, b, c, d], |
| 57 | + tail: a, |
| 58 | + }; |
| 59 | + let table = [[a, b], [c, d], [a, d]]; |
| 60 | + |
| 61 | + let mut field = first; |
| 62 | + field.arr = [0; 4]; |
| 63 | + |
| 64 | + let mut param = first; |
| 65 | + reset(&mut param); |
| 66 | + |
| 67 | + // The first field of a struct that is itself at a non-zero offset |
| 68 | + let mut nested = Nested { head: b, first }; |
| 69 | + nested.first.arr = [u32::MAX; 4]; |
| 70 | + |
| 71 | + let mut elem0 = table; |
| 72 | + elem0[0] = [0; 2]; |
| 73 | + |
| 74 | + let mut reborrow = table; |
| 75 | + let row = &mut reborrow[0]; |
| 76 | + *row = [0; 2]; |
| 77 | + |
| 78 | + let mut floats = [[a as f32, b as f32], [c as f32, d as f32]]; |
| 79 | + floats[0] = [0.0; 2]; |
| 80 | + |
| 81 | + // For comparison: a non-zero offset, an array that is the only field, and |
| 82 | + // a fill that is not lowered to `memset` |
| 83 | + let mut elem1 = table; |
| 84 | + elem1[1] = [0; 2]; |
| 85 | + |
| 86 | + let mut only = Only { arr: [a, b, c, d] }; |
| 87 | + only.arr = [0; 4]; |
| 88 | + |
| 89 | + let mut ones = first; |
| 90 | + ones.arr = [1; 4]; |
| 91 | + |
| 92 | + [ |
| 93 | + first_words(&field), |
| 94 | + first_words(¶m), |
| 95 | + nested_words(&nested), |
| 96 | + table_words(&elem0), |
| 97 | + table_words(&reborrow), |
| 98 | + [ |
| 99 | + floats[0][0].to_bits(), |
| 100 | + floats[0][1].to_bits(), |
| 101 | + floats[1][0].to_bits(), |
| 102 | + floats[1][1].to_bits(), |
| 103 | + 0, |
| 104 | + 0, |
| 105 | + ], |
| 106 | + table_words(&elem1), |
| 107 | + only_words(&only), |
| 108 | + first_words(&ones), |
| 109 | + ] |
| 110 | +} |
| 111 | + |
| 112 | +#[spirv(compute(threads(1)))] |
| 113 | +pub fn main_cs( |
| 114 | + #[spirv(storage_buffer, descriptor_set = 0, binding = 0)] input: &[u32; 4], |
| 115 | + #[spirv(storage_buffer, descriptor_set = 0, binding = 1)] output: &mut Output, |
| 116 | +) { |
| 117 | + *output = eval(input); |
| 118 | +} |
0 commit comments