#version 120 
#extension GL_EXT_gpu_shader4 : require

#if __VERSION__ == 120
#   define textureSize  textureSize2D
#endif

uniform sampler2D myDepth;
uniform int previousMIPNumber;

void main(void)
{
    ivec2 ssP = ivec2(gl_FragCoord.xy);

    // Rotated grid subsampling to avoid XY directional bias or Z precision bias while downsampling.
    // On DX9, the bit-and can be implemented with floating-point modulo
    gl_FragColor = texelFetch2D(myDepth, clamp(ssP * 2 + ivec2(ssP.y & 1, ssP.x & 1), ivec2(0), textureSize(myDepth, previousMIPNumber) - ivec2(1)), previousMIPNumber);

}                                                  
