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

// One workgroup per 8×8 screen tile. Each thread owns one pixel and iterates
// every QuadItem in order, accumulating into a local dst register, so item
// order in the buffer == draw order on screen (later items overdraw earlier).
layout(push_constant) uniform PC {
    UIDispatchHeader hdr;
} pc;

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

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) {
        QuadItem it = LoadQuadItem(pc.hdr.itemBuffer, i);

        // Cheap pre-test against the item's axis-aligned rect.
        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 halfSize = it.rect.zw * 0.5;
        vec2 p        = sp - (it.rect.xy + halfSize);
        float d       = uiSdRoundRect(p, halfSize, it.corners);

        float bodyA = clamp(0.5 - d, 0.0, 1.0);
        if (bodyA <= 0.0 && it.outline.x <= 0.0) continue;

        vec4 src = vec4(it.color.rgb, it.color.a * bodyA);

        if (it.outline.x > 0.0) {
            float t = abs(d + it.outline.x * 0.5) - it.outline.x * 0.5;
            float outlineA = clamp(0.5 - t, 0.0, 1.0);
            src.rgb = mix(src.rgb, it.outline.yzw, outlineA);
            src.a   = max(src.a, outlineA);
        }

        if (src.a <= 0.0) continue;
        dst = uiBlendOver(dst, src);
    }

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