The standalone per-category UI shaders (quads/circles/images/text) unconditionally imageLoad the destination pixel up front and imageStore it at the end, paying a full read-modify-write even on tiles no item touches — the common case for a sparse UI. The fused uber-kernel already amortizes a single load/store across all four categories; the standalone Dispatch* path has no such umbrella. Defer the imageLoad to just before the first surviving blend (`loaded` flag) and skip the imageStore unless something was actually blended. Output is bit-identical: before the first blend `dst` is unused, and a skipped store leaves the pixel exactly as a no-op load+store would have rewritten it. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
108 lines
4 KiB
GLSL
108 lines
4 KiB
GLSL
#version 460
|
||
#extension GL_GOOGLE_include_directive : enable
|
||
#include "ui-shared.glsl"
|
||
|
||
// One workgroup per 8×8 screen tile. The workgroup cooperatively streams the
|
||
// CircleItem 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.
|
||
layout(push_constant) uniform PC {
|
||
UIDispatchHeader hdr;
|
||
} pc;
|
||
|
||
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
|
||
|
||
shared vec4 s_centerRadius[UI_CHUNK];
|
||
shared vec4 s_color[UI_CHUNK];
|
||
shared vec4 s_outline[UI_CHUNK];
|
||
shared uint s_keep[UI_CHUNK];
|
||
shared uint s_order[UI_CHUNK];
|
||
shared uint s_count;
|
||
|
||
void main() {
|
||
ivec2 screenPx;
|
||
bool valid = uiResolveScreenPixel(pc.hdr, screenPx);
|
||
|
||
// Defer the destination read-modify-write: a sparse UI leaves most tiles
|
||
// untouched, so only load the pixel when the first surviving item blends
|
||
// over it (`loaded`), and only store when something actually touched it.
|
||
// The fused kernel amortizes a single load/store across all categories; the
|
||
// standalone Dispatch* path has no such umbrella and otherwise pays a full
|
||
// read-modify-write per empty tile.
|
||
vec4 dst = vec4(0.0);
|
||
vec2 sp = vec2(0.0);
|
||
bool loaded = false;
|
||
if (valid) sp = vec2(screenPx) + 0.5;
|
||
|
||
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_centerRadius[lid] = uiCircleHeap[pc.hdr.itemBuffer].items[idx].centerRadius;
|
||
s_color[lid] = uiCircleHeap[pc.hdr.itemBuffer].items[idx].color;
|
||
s_outline[lid] = uiCircleHeap[pc.hdr.itemBuffer].items[idx].outline;
|
||
// Cull box matches the in-loop bounding-box reject (radius + 1px).
|
||
float radius = s_centerRadius[lid].z;
|
||
if (radius > 0.0) {
|
||
vec2 c = s_centerRadius[lid].xy;
|
||
vec2 r = vec2(radius + 1.0);
|
||
keep = uiAabbOverlapsTile(c - r, c + r, tileMin, tileMax);
|
||
}
|
||
}
|
||
s_keep[lid] = keep ? 1u : 0u;
|
||
barrier();
|
||
|
||
if (lid == 0u) {
|
||
uint n = 0u;
|
||
uint lim = min(UI_CHUNK, pc.hdr.itemCount - base);
|
||
for (uint k = 0u; k < lim; ++k)
|
||
if (s_keep[k] != 0u) s_order[n++] = k;
|
||
s_count = n;
|
||
}
|
||
barrier();
|
||
|
||
if (valid) {
|
||
for (uint j = 0u; j < s_count; ++j) {
|
||
uint c = s_order[j];
|
||
|
||
vec2 center = s_centerRadius[c].xy;
|
||
float radius = s_centerRadius[c].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;
|
||
|
||
vec4 outline = s_outline[c];
|
||
float bodyA = clamp(0.5 - d, 0.0, 1.0);
|
||
if (bodyA <= 0.0 && outline.x <= 0.0) continue;
|
||
|
||
vec4 col = s_color[c];
|
||
vec4 src = vec4(col.rgb, col.a * bodyA);
|
||
|
||
if (outline.x > 0.0) {
|
||
float t = abs(d + outline.x * 0.5) - outline.x * 0.5;
|
||
float outlineA = clamp(0.5 - t, 0.0, 1.0);
|
||
src.rgb = mix(src.rgb, outline.yzw, outlineA);
|
||
src.a = max(src.a, outlineA);
|
||
}
|
||
|
||
if (src.a <= 0.0) continue;
|
||
if (!loaded) {
|
||
dst = imageLoad(uiImages[pc.hdr.outImage], screenPx);
|
||
loaded = true;
|
||
}
|
||
dst = uiBlendOver(dst, src);
|
||
}
|
||
}
|
||
barrier();
|
||
}
|
||
|
||
if (loaded) imageStore(uiImages[pc.hdr.outImage], screenPx, dst); // loaded ⇒ valid
|
||
}
|