-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathScan.comp
74 lines (58 loc) · 1.4 KB
/
Scan.comp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#version 450
#extension GL_KHR_shader_subgroup_arithmetic : enable
layout(std430, binding = 0) buffer Input
{
float inputs[];
};
layout(std430, binding = 1) buffer Output
{
float outputs[];
};
layout(std430, binding = 2) buffer PartialSums
{
float partial_sums[];
};
layout (local_size_x_id = 1) in;
layout (constant_id = 2) const int sumSubGroupSize = 64;
layout(push_constant) uniform PushConsts
{
int n;
} consts;
shared float sdata[sumSubGroupSize];
void main()
{
float sum = 0.0;
if (gl_GlobalInvocationID.x < consts.n)
{
sum = inputs[gl_GlobalInvocationID.x];
}
sum = subgroupInclusiveAdd(sum);
if (gl_SubgroupInvocationID == gl_SubgroupSize - 1)
{
sdata[gl_SubgroupID] = sum;
}
memoryBarrierShared();
barrier();
if (gl_SubgroupID == 0)
{
float warpSum = gl_SubgroupInvocationID < gl_NumSubgroups ? sdata[gl_SubgroupInvocationID] : 0;
warpSum = subgroupInclusiveAdd(warpSum);
sdata[gl_SubgroupInvocationID] = warpSum;
}
memoryBarrierShared();
barrier();
float blockSum = 0;
if (gl_SubgroupID > 0)
{
blockSum = sdata[gl_SubgroupID - 1];
}
sum += blockSum;
if (gl_GlobalInvocationID.x < consts.n)
{
outputs[gl_GlobalInvocationID.x] = sum;
}
if (gl_LocalInvocationID.x == gl_WorkGroupSize.x - 1)
{
partial_sums[gl_WorkGroupID.x] = sum;
}
}