#version 460
#extension GL_GOOGLE_include_directive : enable
#include "ui-shared.glsl"

// One workgroup per 8×8 screen tile. Iterates every glyph in order; each
// pixel keeps a local accumulator so order in the buffer == draw order.
layout(push_constant) uniform PC {
    UIDispatchHeader hdr;
    uint  fontTextureSlot;
    uint  fontSamplerSlot;
    uint  _p0;
    uint  _p1;
} pc;

layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;

// SDF tuning — must match Crafter::FontAtlas::kOnEdgeValue / kPixelDistScale.
const float ON_EDGE    = 128.0 / 255.0;
const float DIST_SCALE = 32.0;

void main() {
    ivec2 screenPx;
    if (!uiResolveScreenPixel(pc.hdr, screenPx)) return;

    vec4 dst = imageLoad(uiImages[pc.hdr.outImage], screenPx);
    vec2 sp  = vec2(screenPx) + 0.5;

    for (uint i = 0u; i < pc.hdr.itemCount; ++i) {
        GlyphItem it = LoadGlpyhtem(pc.hdr.itemBuffer, i);

        vec2 lo = it.rect.xy;
        vec2 hi = it.rect.xy + it.rect.zw;
        if (sp.x < lo.x || sp.y < lo.y) continue;
        if (sp.x >= hi.x || sp.y >= hi.y) continue;

        vec2 t  = (sp - it.rect.xy) / it.rect.zw;
        vec2 uv = mix(it.uv.xy, it.uv.zw, t);

        // Font slots are push constants — provably dynamically uniform, so no
        // nonuniformEXT: lets the compiler hoist the descriptor load out of the
        // per-pixel glyph loop and avoid the divergent-descriptor path.
        float sdf = texture(
            sampler2D(uiTextures[pc.fontTextureSlot],
                      uiSamplers[pc.fontSamplerSlot]),
            uv
        ).r;

        // Distance in atlas-pixels (negative inside the glyph).
        float dAtlas = (ON_EDGE - sdf) * DIST_SCALE;

        // Atlas-px per screen-px along this glyph's transform — keeps AA crisp
        // at any rendering size. uvSpan * atlasSize / screenSpan.
        vec2  uvSpan        = it.uv.zw - it.uv.xy;
        // FontAtlas::kAtlasSize = 1024.
        vec2  atlasPerScreen = (uvSpan * 1024.0) / it.rect.zw;
        float scalePx        = max(atlasPerScreen.x, atlasPerScreen.y);
        // 1-screen-px AA band, expressed in atlas-pixel units of dAtlas.
        float band           = max(scalePx, 0.0001);

        float a = clamp(0.5 - dAtlas / band, 0.0, 1.0);
        if (a <= 0.0) continue;

        vec4 src = vec4(it.color.rgb, it.color.a * a);
        dst = uiBlendOver(dst, src);
    }

    imageStore(uiImages[pc.hdr.outImage], screenPx, dst);
}
