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
|
/**
* Shader that downsample depth buffer,
* saving min and max value of each texel in the above mipmaps.
* Adapted from http://rastergrid.com/blog/2010/10/hierarchical-z-map-based-occlusion-culling/
**/
uniform sampler2D depthBuffer;
float sampleLowerMip(ivec2 texel)
{
return texelFetch(depthBuffer, texel, 0).r;
}
void minmax(inout float out_val, float in_val)
{
#ifdef MIN_PASS
out_val = min(out_val, in_val);
#else /* MAX_PASS */
out_val = max(out_val, in_val);
#endif
}
void main()
{
ivec2 texelPos = ivec2(gl_FragCoord.xy);
ivec2 mipsize = textureSize(depthBuffer, 0);
#ifndef COPY_DEPTH
texelPos *= 2;
#endif
float val = sampleLowerMip(texelPos);
#ifndef COPY_DEPTH
minmax(val, sampleLowerMip(texelPos + ivec2(1, 0)));
minmax(val, sampleLowerMip(texelPos + ivec2(1, 1)));
minmax(val, sampleLowerMip(texelPos + ivec2(0, 1)));
/* if we are reducing an odd-width texture then fetch the edge texels */
if (((mipsize.x & 1) != 0) && (int(gl_FragCoord.x) == mipsize.x-3)) {
/* if both edges are odd, fetch the top-left corner texel */
if (((mipsize.y & 1) != 0) && (int(gl_FragCoord.y) == mipsize.y-3)) {
minmax(val, sampleLowerMip(texelPos + ivec2(-1, -1)));
}
minmax(val, sampleLowerMip(texelPos + ivec2(0, -1)));
minmax(val, sampleLowerMip(texelPos + ivec2(1, -1)));
}
/* if we are reducing an odd-height texture then fetch the edge texels */
else if (((mipsize.y & 1) != 0) && (int(gl_FragCoord.y) == mipsize.y-3)) {
minmax(val, sampleLowerMip(texelPos + ivec2(0, -1)));
minmax(val, sampleLowerMip(texelPos + ivec2(1, -1)));
}
#endif
gl_FragDepth = val;
}
|