1#version 450
2
3#extension GL_KHR_shader_subgroup_ballot: enable
4
5layout (local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
6
7layout(binding = 0) buffer Buffers
8{
9    vec4  f4;
10    ivec4 i4;
11    uvec4 u4;
12    dvec4 d4;
13} data[4];
14
15void main()
16{
17    uint invocation = (gl_SubgroupInvocationID + gl_SubgroupSize) % 4;
18
19    uvec4 relMask = gl_SubgroupEqMask +
20                       gl_SubgroupGeMask +
21                       gl_SubgroupGtMask +
22                       gl_SubgroupLeMask +
23                       gl_SubgroupLtMask;
24
25    uvec4 result = subgroupBallot(true);
26
27    data[invocation].u4.x = subgroupBallotBitCount(result);
28    data[invocation].u4.y = subgroupBallotBitExtract(result, 0) ? 1 : 0;
29    data[invocation].u4.z = subgroupBallotInclusiveBitCount(result) + subgroupBallotExclusiveBitCount(result);
30    data[invocation].u4.w = subgroupBallotFindLSB(result) + subgroupBallotFindMSB(result);
31
32    if ((relMask == result) && subgroupInverseBallot(data[0].u4))
33    {
34        data[invocation].f4.x   = subgroupBroadcast(data[0].f4.x,    3);
35        data[invocation].f4.xy  = subgroupBroadcast(data[1].f4.xy,   3);
36        data[invocation].f4.xyz = subgroupBroadcast(data[2].f4.xyz,  3);
37        data[invocation].f4     = subgroupBroadcast(data[3].f4,      3);
38
39        data[invocation].i4.x   = subgroupBroadcast(data[0].i4.x,    2);
40        data[invocation].i4.xy  = subgroupBroadcast(data[1].i4.xy,   2);
41        data[invocation].i4.xyz = subgroupBroadcast(data[2].i4.xyz,  2);
42        data[invocation].i4     = subgroupBroadcast(data[3].i4,      2);
43
44        data[invocation].u4.x   = subgroupBroadcast(data[0].u4.x,    1);
45        data[invocation].u4.xy  = subgroupBroadcast(data[1].u4.xy,   1);
46        data[invocation].u4.xyz = subgroupBroadcast(data[2].u4.xyz,  1);
47        data[invocation].u4     = subgroupBroadcast(data[3].u4,      1);
48
49        data[invocation].d4.x   = subgroupBroadcast(data[0].d4.x,    0);
50        data[invocation].d4.xy  = subgroupBroadcast(data[1].d4.xy,   0);
51        data[invocation].d4.xyz = subgroupBroadcast(data[2].d4.xyz,  0);
52        data[invocation].d4     = subgroupBroadcast(data[3].d4,      0);
53
54        data[invocation].i4.x   = int(subgroupBroadcast(data[0].i4.x < 0,            1));
55        data[invocation].i4.xy  = ivec2(subgroupBroadcast(lessThan(data[1].i4.xy, ivec2(0)), 1));
56        data[invocation].i4.xyz = ivec3(subgroupBroadcast(lessThan(data[1].i4.xyz, ivec3(0)), 1));
57        data[invocation].i4     = ivec4(subgroupBroadcast(lessThan(data[1].i4, ivec4(0)), 1));
58    }
59    else
60    {
61        data[invocation].f4.x   = subgroupBroadcastFirst(data[0].f4.x);
62        data[invocation].f4.xy  = subgroupBroadcastFirst(data[1].f4.xy);
63        data[invocation].f4.xyz = subgroupBroadcastFirst(data[2].f4.xyz);
64        data[invocation].f4     = subgroupBroadcastFirst(data[3].f4);
65
66        data[invocation].i4.x   = subgroupBroadcastFirst(data[0].i4.x);
67        data[invocation].i4.xy  = subgroupBroadcastFirst(data[1].i4.xy);
68        data[invocation].i4.xyz = subgroupBroadcastFirst(data[2].i4.xyz);
69        data[invocation].i4     = subgroupBroadcastFirst(data[3].i4);
70
71        data[invocation].u4.x   = subgroupBroadcastFirst(data[0].u4.x);
72        data[invocation].u4.xy  = subgroupBroadcastFirst(data[1].u4.xy);
73        data[invocation].u4.xyz = subgroupBroadcastFirst(data[2].u4.xyz);
74        data[invocation].u4     = subgroupBroadcastFirst(data[3].u4);
75
76        data[invocation].d4.x   = subgroupBroadcastFirst(data[0].d4.x);
77        data[invocation].d4.xy  = subgroupBroadcastFirst(data[1].d4.xy);
78        data[invocation].d4.xyz = subgroupBroadcastFirst(data[2].d4.xyz);
79        data[invocation].d4     = subgroupBroadcastFirst(data[3].d4);
80
81        data[invocation].i4.x   = int(subgroupBroadcastFirst(data[0].i4.x < 0));
82        data[invocation].i4.xy  = ivec2(subgroupBroadcastFirst(lessThan(data[1].i4.xy, ivec2(0))));
83        data[invocation].i4.xyz = ivec3(subgroupBroadcastFirst(lessThan(data[1].i4.xyz, ivec3(0))));
84        data[invocation].i4     = ivec4(subgroupBroadcastFirst(lessThan(data[1].i4, ivec4(0))));
85    }
86}
87