#version 330 core
// Floodlight multi-pass: pass 2 / 3 / 5 (gaussian blur).
//
// Direct port of Android's `EditorImageFloodlightBlurFilter`: a 16-tap
// non-separable blur on the X and Y cardinal axes (33 samples total
// including center) with a fixed `half_gaussian_weight` table. The
// CPU supplies `u_blurStep` already scaled by Android's
// `blurSize * 1.25 * coverPercent / realSize` formula, in UV units.

out vec4 FragColor;
in  vec2 TexCoord;

uniform sampler2D u_input;       // RGBA from previous pass
uniform vec2      u_blurStep;    // UV step per ring (X = x-axis, Y = y-axis)

const float kGaussian[9] = float[](
    0.20, 0.19, 0.17, 0.15, 0.13, 0.11, 0.08, 0.05, 0.02
);

void main() {
    // FBO scratches are stored with OpenGL's bottom-up convention, but
    // our quad's TexCoord is Y-down (TexCoord.y=0 at the top vertex).
    // Flip V here so sampling matches the orientation we rendered with
    // in pass 1 (which sampled the upload-orient YUV at TexCoord
    // directly). Every subsequent FBO->FBO read needs the same flip.
    vec2 uv = vec2(TexCoord.x, 1.0 - TexCoord.y);
    vec4 center = texture(u_input, uv);
    vec4 sum = center * kGaussian[0];
    float totalW = kGaussian[0];

    for (int i = 1; i <= 8; ++i) {
        float fi = float(i);
        // X-axis pair.
        vec4 r = texture(u_input, uv + vec2(fi * u_blurStep.x, 0.0));
        vec4 l = texture(u_input, uv - vec2(fi * u_blurStep.x, 0.0));
        sum += (r + l) * kGaussian[i];
        // Y-axis pair.
        vec4 t = texture(u_input, uv + vec2(0.0, fi * u_blurStep.y));
        vec4 b = texture(u_input, uv - vec2(0.0, fi * u_blurStep.y));
        sum += (t + b) * kGaussian[i];
        totalW += 4.0 * kGaussian[i];
    }

    FragColor = sum / totalW;
}
