uniform sampler2D u_texture_src;
uniform sampler2D u_texture_tgt;
uniform float u_spacing;
uniform int   u_mode;
uniform int   u_dbg_test_membrane;

vec4 sample_diff(vec2 coord) {
    if (u_dbg_test_membrane)
        return texture2D(u_texture_tgt, coord);
    else
        return texture2D(u_texture_tgt, coord) - texture2D(u_texture_src, coord);
}

void main() {
    vec2 coord = gl_TexCoord[0].xy;
    vec2 dx = vec2(u_spacing, 0.0);
    vec2 dy = vec2(0.0, u_spacing);
    
    /*
    p: plus 1
    m: minus 1
    0: no plus/minus
    (exmaples)
    f_00 means f(i, j)
    f_p0 means f(i+1, j)
    */
    
    vec4 f_00 = sample_diff(coord);
    if (u_mode == 0) {
        gl_FragColor = f_00;
    } else if (u_mode == 1) {
        vec4 f_p0 = sample_diff(coord + dx);
        gl_FragColor = f_p0 - f_00;
    } else {
        vec4 f_0p = sample_diff(coord + dy);
        gl_FragColor = f_0p - f_00;
    }
    gl_FragColor.w = 1.0;
}
