struct BloomParams {
    filter_radius: f32,
    aspect_ratio: f32,
    padding1: f32,
    padding2: f32,
}

@group(0) @binding(0) var source_texture: texture_2d<f32>;
@group(0) @binding(1) var source_sampler: sampler;
@group(0) @binding(2) var<uniform> params: BloomParams;

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) uv: vec2<f32>,
}

@vertex
fn vertex_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
    let uv = vec2<f32>(
        f32((vertex_index << 1u) & 2u),
        f32(vertex_index & 2u)
    );
    let clip_position = vec4<f32>(uv * 2.0 - 1.0, 0.0, 1.0);

    var out: VertexOutput;
    out.clip_position = clip_position;
    out.uv = vec2<f32>(uv.x, 1.0 - uv.y);
    return out;
}

@fragment
fn fragment_main(in: VertexOutput) -> @location(0) vec4<f32> {
    let texture_size = vec2<f32>(textureDimensions(source_texture));
    let texel_size = 1.0 / texture_size;

    let x_radius = params.filter_radius;
    let y_radius = params.filter_radius * params.aspect_ratio;

    let a = textureSample(source_texture, source_sampler, in.uv + texel_size * vec2<f32>(-x_radius, -y_radius)).rgb;
    let b = textureSample(source_texture, source_sampler, in.uv + texel_size * vec2<f32>(0.0, -y_radius)).rgb;
    let c = textureSample(source_texture, source_sampler, in.uv + texel_size * vec2<f32>(x_radius, -y_radius)).rgb;
    let d = textureSample(source_texture, source_sampler, in.uv + texel_size * vec2<f32>(-x_radius, 0.0)).rgb;
    let e = textureSample(source_texture, source_sampler, in.uv).rgb;
    let f = textureSample(source_texture, source_sampler, in.uv + texel_size * vec2<f32>(x_radius, 0.0)).rgb;
    let g = textureSample(source_texture, source_sampler, in.uv + texel_size * vec2<f32>(-x_radius, y_radius)).rgb;
    let h = textureSample(source_texture, source_sampler, in.uv + texel_size * vec2<f32>(0.0, y_radius)).rgb;
    let i = textureSample(source_texture, source_sampler, in.uv + texel_size * vec2<f32>(x_radius, y_radius)).rgb;

    var upsample = e * 0.25;
    upsample = upsample + (b + d + f + h) * 0.125;
    upsample = upsample + (a + c + g + i) * 0.0625;

    return vec4<f32>(upsample, 1.0);
}
