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

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

        vec2  center = it.centerRadius.xy;
        float radius = it.centerRadius.z;
        if (radius <= 0.0) continue;

        // Cheap bounding-box reject.
        if (abs(sp.x - center.x) > radius + 1.0) continue;
        if (abs(sp.y - center.y) > radius + 1.0) continue;

        float d = length(sp - center) - radius;

        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);
}
