2026-05-02 21:08:20 +02:00
|
|
|
|
#version 460
|
|
|
|
|
|
#extension GL_GOOGLE_include_directive : enable
|
2026-06-18 13:26:37 +00:00
|
|
|
|
#extension GL_KHR_shader_subgroup_basic : enable
|
|
|
|
|
|
#extension GL_KHR_shader_subgroup_arithmetic : enable
|
2026-05-02 21:08:20 +02:00
|
|
|
|
#include "ui-shared.glsl"
|
|
|
|
|
|
|
2026-06-16 15:45:12 +00:00
|
|
|
|
// One workgroup per 8×8 screen tile. The workgroup cooperatively streams the
|
|
|
|
|
|
// GlyphItem list in chunks of 64 (see ui-shared.glsl), culling each chunk
|
|
|
|
|
|
// against the tile and compacting survivors — in buffer order — into shared
|
|
|
|
|
|
// memory; every thread then accumulates over only those survivors.
|
2026-05-02 21:08:20 +02:00
|
|
|
|
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;
|
|
|
|
|
|
|
2026-06-16 15:45:12 +00:00
|
|
|
|
shared vec4 s_rect[UI_CHUNK];
|
|
|
|
|
|
shared vec4 s_uv[UI_CHUNK];
|
|
|
|
|
|
shared vec4 s_color[UI_CHUNK];
|
|
|
|
|
|
shared uint s_keep[UI_CHUNK];
|
|
|
|
|
|
shared uint s_order[UI_CHUNK];
|
|
|
|
|
|
shared uint s_count;
|
2026-06-18 13:26:37 +00:00
|
|
|
|
shared uint s_subTotals[UI_CHUNK]; // per-subgroup survivor totals (carry step)
|
|
|
|
|
|
|
|
|
|
|
|
// Stable in-order compaction of one chunk's survivors via a per-subgroup
|
|
|
|
|
|
// exclusive prefix-sum plus a carry across subgroups, so a survivor's slot in
|
|
|
|
|
|
// s_order[] equals the number of survivors with a smaller local index — buffer
|
|
|
|
|
|
// (draw) order preserved. Replaces the old lane-0 serial scan. See
|
|
|
|
|
|
// ui-fused.comp.glsl for the full rationale; the body is identical.
|
|
|
|
|
|
void uiCompactChunk() {
|
|
|
|
|
|
uint lid = gl_LocalInvocationIndex;
|
|
|
|
|
|
uint keep = s_keep[lid];
|
|
|
|
|
|
uint subPrefix = subgroupExclusiveAdd(keep);
|
|
|
|
|
|
uint subTotal = subgroupAdd(keep);
|
|
|
|
|
|
if (subgroupElect())
|
|
|
|
|
|
s_subTotals[gl_SubgroupID] = subTotal;
|
|
|
|
|
|
barrier();
|
|
|
|
|
|
|
|
|
|
|
|
uint base = 0u;
|
|
|
|
|
|
uint total = 0u;
|
|
|
|
|
|
for (uint s = 0u; s < gl_NumSubgroups; ++s) {
|
|
|
|
|
|
uint t = s_subTotals[s];
|
|
|
|
|
|
if (s < gl_SubgroupID) base += t;
|
|
|
|
|
|
total += t;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (lid == 0u) s_count = total;
|
|
|
|
|
|
|
|
|
|
|
|
if (keep != 0u)
|
|
|
|
|
|
s_order[base + subPrefix] = lid;
|
|
|
|
|
|
}
|
2026-06-16 15:45:12 +00:00
|
|
|
|
|
2026-05-02 21:08:20 +02:00
|
|
|
|
void main() {
|
|
|
|
|
|
ivec2 screenPx;
|
2026-06-16 15:45:12 +00:00
|
|
|
|
bool valid = uiResolveScreenPixel(pc.hdr, screenPx);
|
2026-05-02 21:08:20 +02:00
|
|
|
|
|
2026-06-16 15:45:12 +00:00
|
|
|
|
vec4 dst = vec4(0.0);
|
|
|
|
|
|
vec2 sp = vec2(0.0);
|
|
|
|
|
|
if (valid) {
|
|
|
|
|
|
dst = imageLoad(uiImages[pc.hdr.outImage], screenPx);
|
|
|
|
|
|
sp = vec2(screenPx) + 0.5;
|
2026-05-02 21:08:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-16 15:45:12 +00:00
|
|
|
|
vec2 tileMin, tileMax;
|
|
|
|
|
|
uiTileBounds(tileMin, tileMax);
|
|
|
|
|
|
|
|
|
|
|
|
uint lid = gl_LocalInvocationIndex;
|
|
|
|
|
|
for (uint base = 0u; base < pc.hdr.itemCount; base += UI_CHUNK) {
|
|
|
|
|
|
uint idx = base + lid;
|
|
|
|
|
|
bool keep = false;
|
|
|
|
|
|
if (idx < pc.hdr.itemCount) {
|
|
|
|
|
|
s_rect[lid] = uiGlyphHeap[pc.hdr.itemBuffer].items[idx].rect;
|
|
|
|
|
|
s_uv[lid] = uiGlyphHeap[pc.hdr.itemBuffer].items[idx].uv;
|
|
|
|
|
|
s_color[lid] = uiGlyphHeap[pc.hdr.itemBuffer].items[idx].color;
|
|
|
|
|
|
keep = uiAabbOverlapsTile(s_rect[lid].xy, s_rect[lid].xy + s_rect[lid].zw,
|
|
|
|
|
|
tileMin, tileMax);
|
|
|
|
|
|
}
|
|
|
|
|
|
s_keep[lid] = keep ? 1u : 0u;
|
|
|
|
|
|
barrier();
|
|
|
|
|
|
|
2026-06-18 13:26:37 +00:00
|
|
|
|
uiCompactChunk();
|
2026-06-16 15:45:12 +00:00
|
|
|
|
barrier();
|
|
|
|
|
|
|
|
|
|
|
|
if (valid) {
|
|
|
|
|
|
for (uint j = 0u; j < s_count; ++j) {
|
|
|
|
|
|
uint c = s_order[j];
|
|
|
|
|
|
|
|
|
|
|
|
vec2 lo = s_rect[c].xy;
|
|
|
|
|
|
vec2 hi = s_rect[c].xy + s_rect[c].zw;
|
|
|
|
|
|
if (sp.x < lo.x || sp.y < lo.y) continue;
|
|
|
|
|
|
if (sp.x >= hi.x || sp.y >= hi.y) continue;
|
|
|
|
|
|
|
|
|
|
|
|
vec2 t = (sp - s_rect[c].xy) / s_rect[c].zw;
|
|
|
|
|
|
vec2 uv = mix(s_uv[c].xy, s_uv[c].zw, t);
|
|
|
|
|
|
|
2026-06-16 16:02:00 +00:00
|
|
|
|
// 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.
|
2026-06-16 15:45:12 +00:00
|
|
|
|
float sdf = texture(
|
2026-06-16 16:02:00 +00:00
|
|
|
|
sampler2D(uiTextures[pc.fontTextureSlot],
|
|
|
|
|
|
uiSamplers[pc.fontSamplerSlot]),
|
2026-06-16 15:45:12 +00:00
|
|
|
|
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 = s_uv[c].zw - s_uv[c].xy;
|
|
|
|
|
|
// FontAtlas::kAtlasSize = 1024.
|
|
|
|
|
|
vec2 atlasPerScreen = (uvSpan * 1024.0) / s_rect[c].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 col = s_color[c];
|
|
|
|
|
|
vec4 src = vec4(col.rgb, col.a * a);
|
|
|
|
|
|
dst = uiBlendOver(dst, src);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
barrier();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (valid) imageStore(uiImages[pc.hdr.outImage], screenPx, dst);
|
2026-05-02 21:08:20 +02:00
|
|
|
|
}
|