-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathreduction_vec.rs
More file actions
45 lines (39 loc) · 1.17 KB
/
reduction_vec.rs
File metadata and controls
45 lines (39 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// build-pass
// ignore-spv1.0
// ignore-spv1.1
// ignore-spv1.2
// ignore-vulkan1.0
use spirv_std::arch::workgroup_memory_barrier_with_group_sync;
use spirv_std::glam::*;
use spirv_std::spirv;
pub type Value = UVec4;
pub const WG_SIZE_SHIFT: usize = 5;
pub const WG_SIZE: usize = 1 << WG_SIZE_SHIFT;
// threads must be a literal, constants don't work
const _: () = {
assert!(WG_SIZE == 32);
};
#[spirv(compute(threads(32)))]
pub fn main(
#[spirv(descriptor_set = 0, binding = 0, storage_buffer)] input: &[Value],
#[spirv(descriptor_set = 0, binding = 1, storage_buffer)] output: &mut Value,
#[spirv(workgroup)] shared: &mut [Value; WG_SIZE],
#[spirv(local_invocation_index)] inv_id: UVec3,
) {
unsafe {
let inv_id = inv_id.x as usize;
shared[inv_id] = input[inv_id];
workgroup_memory_barrier_with_group_sync();
let mut mask = WG_SIZE << 1;
while mask != 0 {
if inv_id < mask {
shared[inv_id] += shared[inv_id + mask];
}
workgroup_memory_barrier_with_group_sync();
mask <<= 1;
}
if inv_id == 0 {
*output = shared[0];
}
}
}