#extension GL_ARB_texture_rectangle : enable

uniform sampler2DRect u_texture_H;
uniform sampler2DRect u_texture_L;
uniform int u_isGradient;

void main() {
    vec2 H_coord = gl_FragCoord  .xy;
    vec2 L_coord = gl_TexCoord[0].xy;
    vec4 H_p = texture2DRect(u_texture_H, H_coord);
    if (H_p.w == 1.0) {
        gl_FragColor = H_p;
        return;
    }
    gl_FragColor = vec4(0.0);
    for (int i = -1; i <= 1; ++i) for (int j = -1; j <= 1; ++j) {
        gl_FragColor += texture2DRect(u_texture_L, L_coord + 0.5 * vec2(i, j));
    }
    gl_FragColor /= 9.0;
    if (u_isGradient)
        gl_FragColor *= 0.5;  // to account for the change in the grid spacing
    gl_FragColor.w = 0.0;
}
