summaryrefslogtreecommitdiff
path: root/graphics/asymptote/base/shaders/blend.glsl
blob: 629cc2dda5e66f66fb634d50cf6545159e7e35b8 (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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
layout(binding=0, std430) buffer sumBuffer {
  uint sum[];
};

layout(binding=1, std430) buffer offsetBuffer {
  uint offset[];
};

layout(binding=2, std430) buffer countBuffer {
  uint count[];
};

layout(binding=3, std430) buffer fragmentBuffer {
  vec4 fragment[];
};

layout(binding=4, std430) buffer depthBuffer {
  float depth[];
};

layout(binding=5, std430) buffer opaqueBuffer {
  vec4 opaqueColor[];
};

layout(binding=6, std430) buffer opaqueDepthBuffer {
  float opaqueDepth[];
};

out vec4 outColor;

uniform uint width;
uniform uint M;
uniform uint r;
uniform vec4 background;

vec4 blend(vec4 outColor, vec4 color)
{
  return mix(outColor,color,color.a);
}

void main()
{
  uint headIndex=uint(gl_FragCoord.y)*width+uint(gl_FragCoord.x);
  uint size=count[headIndex];
  float OpaqueDepth=opaqueDepth[headIndex];
  if(size == 0u) {
#ifdef GPUINDEXING
    offset[headIndex]=0u;
#endif
    opaqueDepth[headIndex]=0.0;
    discard;
  }

  uint listIndex=
#ifdef GPUINDEXING
    sum[headIndex < r*(M+1u) ? headIndex/(M+1u) : (headIndex-r)/M]+
#endif
    offset[headIndex];
  const uint maxSize=16u;

  // Sort the fragments with respect to descending depth
  if(size < maxSize) {
    vec4 sortedColor[maxSize];
    float sortedDepth[maxSize];

    uint k=0u;

    if(OpaqueDepth != 0.0)
      while(k < size && depth[listIndex+k] >= OpaqueDepth)
        ++k;

    uint i=0u;
    if(k < size) {
      sortedColor[0]=fragment[listIndex+k];
      sortedDepth[0]=depth[listIndex+k];
      ++k;
      i=1u;
      while(true) {
        if(OpaqueDepth != 0.0)
          while(k < size && depth[listIndex+k] >= OpaqueDepth)
            ++k;
        if(k == size) break;
        float D=depth[listIndex+k];
        uint j=i;
        float d;
        while(j > 0u && D > sortedDepth[j-1u]) {
          sortedColor[j]=sortedColor[j-1u];
          sortedDepth[j]=sortedDepth[j-1u];
          --j;
        }
        sortedColor[j]=fragment[listIndex+k];
        sortedDepth[j]=D;
        ++i;
        ++k;
      }
    }
    outColor=OpaqueDepth != 0.0 ? opaqueColor[headIndex] : background;
    for(uint j=0u; j < i; ++j)
      outColor=blend(outColor,sortedColor[j]);
  } else {
    uint k=0u;
    if(OpaqueDepth != 0.0)
      while(k < size && depth[listIndex+k] >= OpaqueDepth)
        ++k;
    for(uint i=k+1u; i < size; i++) {
      vec4 temp=fragment[listIndex+i];
      float D=depth[listIndex+i];
      uint j=i;
      while(j > 0u && D > depth[listIndex+j-1u]) {
        fragment[listIndex+j]=fragment[listIndex+j-1u];
        depth[listIndex+j]=depth[listIndex+j-1u];
        --j;
      }
      fragment[listIndex+j]=temp;
      depth[listIndex+j]=D;
    }

    outColor=OpaqueDepth != 0.0 ? opaqueColor[headIndex] : background;
    uint stop=listIndex+size;
    for(uint i=listIndex+k; i < stop; i++) {
      if(OpaqueDepth == 0.0 || depth[i] < OpaqueDepth)
        outColor=blend(outColor,fragment[i]);
    }
  }

  count[headIndex]=0u;
  opaqueDepth[headIndex]=0.0;
#ifdef GPUINDEXING
  offset[headIndex]=0u;
#endif
}