// Shader to draw the world texture onto the window
struct CameraUniform {
    view_proj: mat4x4<f32>,
};
@group(1) @binding(0) // 1.
var<uniform> camera: CameraUniform;

struct RenderOptions {
    gamma: f32,  
    _padding: vec3<f32>,
};
@group(2) @binding(0)
var<uniform> render_options: RenderOptions;

struct VertexInput {
    @location(0) position: vec3<f32>,
    @location(1) tex_coords: vec2<f32>,
    @location(2) color: vec4<f32>,
}

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

@vertex
fn vs_main(model: VertexInput) -> VertexOutput {
    var out: VertexOutput;
    let tc = camera.view_proj * vec4<f32>(model.tex_coords, 0.0, 1.0);
    out.tex_coords = tc.xy / tc.w;
    out.clip_position = vec4<f32>(model.position.xyz, 1.0);
    // out.clip_position = vec4<f32>(model.position.xy, 0.0, 1.0);
    // out.color = vec4<f32>(out.clip_position);
    out.color = model.color;
    // out.color = vec4<f32>(out.tex_coords, 0.0, 1.0);
    return out;
}

@group(0) @binding(0)
var t_world: texture_2d<f32>;
@group(0) @binding(1)
var s_world: sampler;

fn meets_thresh(color: vec4<f32>) -> bool {
    // stack overflow magic \sqrt{0.299\cdot r^{2}+0.587\cdot g^{2}+0.114\cdot b^{2}}
    let brightness = sqrt(dot(color.rgb * color.rgb, vec3<f32>(0.299, 0.587, 0.114)));
    return brightness > 0.1;
}

const PI = 3.14159265359;
const STD_DEV: f32 = 50.0;

fn weight_calc(i: u32) -> f32 {
    let STD_DEV_P2 = pow(STD_DEV, 2.0);
    // use the gaussian kernel, until it's too slow otherwise
    return (1.0 / sqrt(2.0 * PI * STD_DEV_P2)) * exp(-pow(f32(i), 2.0) / (2.0 * STD_DEV_P2));
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    // let textureDims = vec2<f32>(textureDimensions(t_world));
    let t_tex_coords = in.tex_coords;
    
    var textureOffset = vec2<f32>(1) / vec2<f32>(textureDimensions(t_world));
    var result = in.color * textureSample(t_world, s_world, t_tex_coords);
    let strength = 3.0;
    var added: vec4<f32>;
    // let offset = vec2<f32>(textureOffset.x, textureOffset.y);
    // Turns out this might be a cool astigmatism shader, gotta remember that
    let offset_y = vec2<f32>(0.0, textureOffset.y);
    let offset_x = vec2<f32>(textureOffset.x, 0.0);
    for(var i: u32 = 0; i < 9; i++) {
        let new_color_a = textureSample(t_world, s_world, t_tex_coords + offset_x * f32(i));
        let new_color_b = textureSample(t_world, s_world, t_tex_coords - offset_x * f32(i));
        if (meets_thresh(new_color_a)) {
            // result += vec4<f32>(new_color_a.rgb * weight[i], 0);
            added += vec4<f32>(new_color_a.rgb, 1.0 / strength) * weight_calc(i) * strength;
        }
        if (meets_thresh(new_color_b)) {
            // result += vec4<f32>(new_color_b.rgb * weight[i], 0);
            added += vec4<f32>(new_color_b.rgb, 1.0 / strength) * weight_calc(i) * strength;
        }
        let new_color_c = textureSample(t_world, s_world, t_tex_coords + offset_y * f32(i));
        let new_color_d = textureSample(t_world, s_world, t_tex_coords - offset_y * f32(i));
        if (meets_thresh(new_color_c)) {
            // result += vec4<f32>(new_color_a.rgb * weight[i], 0);
            added += vec4<f32>(new_color_c.rgb, 1.0 / strength) * weight_calc(i) * strength;
        }
        if (meets_thresh(new_color_d)) {
            // result += vec4<f32>(new_color_b.rgb * weight[i], 0);
            added += vec4<f32>(new_color_d.rgb, 1.0 / strength) * weight_calc(i) * strength;
        }
        let new_color_e = textureSample(t_world, s_world, t_tex_coords + offset_x * f32(i) + offset_y * f32(i));
        let new_color_f = textureSample(t_world, s_world, t_tex_coords + offset_x * f32(i) - offset_y * f32(i));
        if (meets_thresh(new_color_e)) {
            // result += vec4<f32>(new_color_a.rgb * weight[i], 0);
            added += vec4<f32>(new_color_e.rgb, 1.0 / strength) * weight_calc(i) * strength;
        }
        if (meets_thresh(new_color_f)) {
            // result += vec4<f32>(new_color_b.rgb * weight[i], 0);
            added += vec4<f32>(new_color_f.rgb, 1.0 / strength) * weight_calc(i) * strength;
        }
        let new_color_g = textureSample(t_world, s_world, t_tex_coords + offset_x * f32(i) + offset_y * f32(i));
        let new_color_h = textureSample(t_world, s_world, t_tex_coords - offset_x * f32(i) + offset_y * f32(i));
        if (meets_thresh(new_color_g)) {
            // result += vec4<f32>(new_color_a.rgb * weight[i], 0);
            added += vec4<f32>(new_color_g.rgb, 1.0 / strength) * weight_calc(i) * strength;
        }
        if (meets_thresh(new_color_h)) {
            // result += vec4<f32>(new_color_b.rgb * weight[i], 0);
            added += vec4<f32>(new_color_h.rgb, 1.0 / strength) * weight_calc(i) * strength;
        }
    }
    // We do it this weird way, cuz the world texture is *not* HDR atm
    let hdr = result + added;
    let sdr = hdr::aces_tone_map(hdr.rgb);
    // Map gamma while, we're at it
    result = vec4<f32>(pow(sdr, vec3<f32>(render_options.gamma)), hdr.a);
    // return in.color;
    // return in.color * textureSample(t_world, s_world, in.tex_coords);
    return result;
    // return in.color + textureSample(t_world, s_world, in.tex_coords);
}