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
|
/* From http://rastergrid.com/blog/2010/10/hierarchical-z-map-based-occlusion-culling/ */
#extension GL_EXT_gpu_shader4 : enable
uniform sampler2D lowermip;
uniform ivec2 lowermipsize;
float minmax(float a, float b, float c, float d)
{
#ifdef MIN
return min(min(a, b), min(c, d));
#else
return max(max(a, b), max(c, d));
#endif
}
float minmax(float a, float b, float c)
{
#ifdef MIN
return min(min(a, b), c);
#else
return max(max(a, b), c);
#endif
}
float minmax(float a, float b)
{
#ifdef MIN
return min(a, b);
#else
return max(a, b);
#endif
}
void main()
{
vec4 texels;
ivec2 texelPos = ivec2(gl_FragCoord.xy) * 2;
texels.x = texelFetch(lowermip, texelPos, 0).r;
texels.y = texelFetch(lowermip, texelPos + ivec2(1, 0), 0).r;
texels.z = texelFetch(lowermip, texelPos + ivec2(1, 1), 0).r;
texels.w = texelFetch(lowermip, texelPos + ivec2(0, 1), 0).r;
float minmaxz = minmax(texels.x, texels.y, texels.z, texels.w);
vec3 extra;
/* if we are reducing an odd-width texture then fetch the edge texels */
if (((lowermipsize.x & 1) != 0) && (int(gl_FragCoord.x) == lowermipsize.x-3)) {
/* if both edges are odd, fetch the top-left corner texel */
if (((lowermipsize.y & 1) != 0) && (int(gl_FragCoord.y) == lowermipsize.y-3)) {
extra.z = texelFetch(lowermip, texelPos + ivec2(-1, -1), 0).r;
minmaxz = minmax(minmaxz, extra.z);
}
extra.x = texelFetch(lowermip, texelPos + ivec2(0, -1), 0).r;
extra.y = texelFetch(lowermip, texelPos + ivec2(1, -1), 0).r;
minmaxz = minmax(minmaxz, extra.x, extra.y);
}
/* if we are reducing an odd-height texture then fetch the edge texels */
else if (((lowermipsize.y & 1) != 0) && (int(gl_FragCoord.y) == lowermipsize.y-3)) {
extra.x = texelFetch(lowermip, texelPos + ivec2(0, -1), 0).r;
extra.y = texelFetch(lowermip, texelPos + ivec2(1, -1), 0).r;
minmaxz = minmax(minmaxz, extra.x, extra.y);
}
gl_FragDepth = minmaxz;
gl_FragColor = vec4(1.0);
}
|