-
Notifications
You must be signed in to change notification settings - Fork 2
/
resolve.frag
81 lines (56 loc) · 1.38 KB
/
resolve.frag
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
#version 400
#extension GL_ARB_explicit_attrib_location : require
#extension GL_ARB_shader_image_load_store : require
#extension GL_ARB_shader_storage_buffer_object : require
#include "utils.h"
layout (early_fragment_tests) in;
layout (r32ui) uniform uimage2D counterImage;
layout (std430, binding = 0) buffer oitData {
OITData data[];
};
out vec4 colorOut;
void main(void)
{
ivec2 coord = ivec2(gl_FragCoord.xy);
uint idx = imageLoad(counterImage, coord).x;
const uint maxCount = 32u;
uint count = 0u;
// farthest (largest depth value) first
OITData sorted[maxCount];
while (idx != 0) {
OITData candidate = data[idx];
int insertPoint = int(count);
while (insertPoint > 0) {
if (sorted[insertPoint - 1].depth < candidate.depth) {
break;
}
sorted[insertPoint] = sorted[insertPoint - 1];
insertPoint--;
}
if (insertPoint < maxCount) {
sorted[insertPoint] = candidate;
}
if (count < maxCount) {
count++;
}
idx = candidate.prev;
}
if (count > 0) {
colorOut = vec4(0.0, 0.0, 0.0, 0.0);
for (uint i = 0; i < count; i++) {
uint n = sorted[count - 1 - i].color;
uvec4 temp;
temp.r = n % 256u;
n = n / 256u;
temp.g = n % 256u;
n = n / 256u;
temp.b = n % 256u;
n = n / 256u;
temp.a = n;
vec4 tempF = vec4(temp) / vec4(255.0);
colorOut = mix(colorOut, tempF, tempF.a);
}
} else {
colorOut = vec4(0.0, 0.0, 0.0, 1.0);
}
}