#version 430 core

layout(local_size_x = 16, local_size_y = 16) in;

layout(binding = 0, rgba32f) uniform readonly image2D u_input;
layout(binding = 1, rgba32f) uniform writeonly image2D u_output;

uniform int u_radius; // 1 => 3x3, 2 => 5x5

void main() {
    ivec2 p = ivec2(gl_GlobalInvocationID.xy);
    ivec2 size = imageSize(u_output);
    if (p.x >= size.x || p.y >= size.y) return;

    vec3 sum = vec3(0.0);
    int count = 0;

    for (int dy = -u_radius; dy <= u_radius; ++dy) {
        for (int dx = -u_radius; dx <= u_radius; ++dx) {
            ivec2 q = clamp(p + ivec2(dx, dy), ivec2(0), size - ivec2(1));
            sum += imageLoad(u_input, q).rgb;
            count += 1;
        }
    }

    vec3 out_color = sum / float(count);
    imageStore(u_output, p, vec4(out_color, 1.0));
}
