const bool colortex0MipmapEnabled = true;
#include "/lib/taa/res_params.glsl"

#include "/lib/uniforms.glsl"

varying vec2 texcoord;
uniform sampler2D colortex6;
uniform sampler2D colortex0;

#include "/lib/color_transforms.glsl"

vec4 apply_horizontal_blur(sampler2D t0, vec2 tex_coord, ivec2 size, float bloom_spread, float weights[9])
{
    float uv_x = tex_coord.x * size.x;
    float uv_y = tex_coord.y * size.y;

    vec4 h_sum = vec4(0.0);

    for (int m = 0; m < 9; ++m)
    {
        float offset = float(m - 4) * bloom_spread;
        h_sum += texture(t0, vec2(uv_x + offset, uv_y) / size) * weights[m];
    }

    return h_sum;
}

vec4 apply_vertical_blur(sampler2D t0, vec2 tex_coord, ivec2 size, float bloom_spread, float weights[9])
{
    float uv_x = tex_coord.x * size.x;
    float uv_y = tex_coord.y * size.y;

    vec4 v_sum = vec4(0.0);

    for (int n = 0; n < 9; ++n)
    {
        float offset = float(n - 4) * bloom_spread;
        v_sum += texture(t0, vec2(uv_x, uv_y + offset) / size) * weights[n];
    }

    return v_sum;
}

vec4 apply_bloom_h(sampler2D t0, vec2 tex_coord)
{
    const float bloom_spread = 16.0;
    const float bloom_intensity = 1.0;
    ivec2 size = ivec2(viewResolution);

    // Gaussian weights
    float weights[9] = float[](0.05, 0.09, 0.12, 0.15, 0.18, 0.15, 0.12, 0.09, 0.05);

    vec4 h_blur = apply_horizontal_blur(t0, tex_coord, size, bloom_spread, weights);

    return h_blur * bloom_intensity;
}


void main()
{
    /* DRAWBUFFERS:6 */

    vec3 result = apply_bloom_h(colortex0, texcoord).rgb;

    gl_FragData[0].rgb = result;
}
