// Textured-quad pipeline for the HUD overlay. Each character is a 6-vertex
// (two-triangle) quad with a UV into the font atlas. The CPU-side HUD code
// pushes a vertex buffer every frame describing the text to draw.

struct VsIn {
    @location(0) pos_px: vec2<f32>,   // pixel-space position in the window
    @location(1) uv: vec2<f32>,        // atlas UV
    @location(2) rgba: vec4<f32>,
};

struct HudUniforms {
    // Inverse of the surface dimensions so the VS can map pixel coordinates
    // into clip space: clip.xy = (pos_px / surface_size) * 2.0 - 1.0 with Y
    // flipped.
    inv_surface: vec2<f32>,
    _pad: vec2<f32>,
};

@group(0) @binding(0) var<uniform> u: HudUniforms;
@group(0) @binding(1) var atlas: texture_2d<f32>;
@group(0) @binding(2) var samp: sampler;

struct VsOut {
    @builtin(position) clip: vec4<f32>,
    @location(0) uv: vec2<f32>,
    @location(1) rgba: vec4<f32>,
};

@vertex
fn vs_main(in: VsIn) -> VsOut {
    var out: VsOut;
    let clip_xy = in.pos_px * u.inv_surface * 2.0 - vec2<f32>(1.0, 1.0);
    // Invert Y so pos_px=(0,0) is top-left.
    out.clip = vec4<f32>(clip_xy.x, -clip_xy.y, 0.0, 1.0);
    out.uv = in.uv;
    out.rgba = in.rgba;
    return out;
}

@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
    let coverage = textureSample(atlas, samp, in.uv).r;
    // Atlas is R8 where 0xFF = glyph pixel. We premultiply by the input alpha
    // so the pipeline's alpha-blend pass composits correctly.
    let a = coverage * in.rgba.a;
    return vec4<f32>(in.rgba.rgb * a, a);
}
