// Oxide Stage-8 UI overlay shader.
//
// One vertex format covers both solid quads and glyph quads: the sentinel UV
// `(-1, -1)` marks "solid color, do not sample the atlas". This avoids
// branching on a separate flag attribute and keeps the vertex stride tight
// (32 bytes — pos2 + uv2 + color4).

struct Uniforms {
    mvp: mat4x4<f32>,
};

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

struct VsIn {
    @location(0) position: vec2<f32>,
    @location(1) uv: vec2<f32>,
    @location(2) color: vec4<f32>,
};

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

@vertex
fn vs_main(in: VsIn) -> VsOut {
    var out: VsOut;
    out.clip_pos = u.mvp * vec4<f32>(in.position, 0.0, 1.0);
    out.uv = in.uv;
    out.color = in.color;
    return out;
}

@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
    // Solid quads use the sentinel UV (-1, -1). Sampling out-of-range would
    // be clamped or wrapped depending on the sampler, but we cheaply detect
    // it instead so a single texture binding serves every primitive.
    if (in.uv.x < 0.0) {
        return in.color;
    }
    let alpha = textureSample(atlas, atlas_sampler, in.uv).r;
    return vec4<f32>(in.color.rgb, in.color.a * alpha);
}
