summaryrefslogtreecommitdiff
path: root/graphics/asymptote/base/shaders/sum2.glsl
blob: 17b5efeaeebe6d758a2258d8ea708d0242553c6e (plain)
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
layout(local_size_x=LOCALSIZE) in;

const uint groupSize=LOCALSIZE*BLOCKSIZE;

uniform uint blockSize;

layout(binding=3, std430) buffer globalSumBuffer
{
  uint globalSum[];
};

shared uint groupSum[LOCALSIZE];

void main()
{
  uint localSum[groupSize];
  uint id=gl_LocalInvocationID.x;

  uint dataOffset=blockSize*id;
  uint sum=0u;
  for(uint i=0; i < blockSize; ++i) {
    localSum[i]=sum;
    sum += globalSum[dataOffset+i];
  }

  groupSum[id]=sum;
  barrier();

  for(uint shift=1u; shift < LOCALSIZE; shift *= 2u) {
    uint read;
    if(shift <= id)
      read=groupSum[id]+groupSum[id-shift];
    barrier();
    if(shift <= id)
      groupSum[id]=read;
    barrier();
  }

  // shift local sums and store
  uint shift=id > 0u ? groupSum[id-1u] : 0u;
  for(uint i=0u; i < blockSize; ++i)
    globalSum[dataOffset+i]=localSum[i]+shift;
}