perf(ui): gate per-pixel clip compares behind a flags bit (#50)

uiResolveScreenPixel ran four float compares against hdr.clipRectPx for
every pixel of every UI dispatch, even though the clip rect is the
full-surface default {0,0,1e9,1e9} in the overwhelmingly common case.

Reserve the high bit of the header flags word (UI_FLAG_CLIP) and have
FillHeader set it only when the clip rect is narrower than the surface
(extracted into the unit-testable UIRenderer::ClipFlags). The shader
gates the compares on the bit; the branch is push-constant-uniform so it
never diverges. When the rect covers the surface the skipped compares
could never reject an in-surface pixel, so output is pixel-identical.

Mirrored on the WebGPU/WGSL path (dom-webgpu.js) for parity. Adds the
UIClipFlag CPU test covering the gate decision.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
catbot 2026-06-16 16:57:07 +00:00
commit 65c19ea84a
6 changed files with 192 additions and 9 deletions

View file

@ -59,6 +59,12 @@ export namespace Crafter {
};
static_assert(sizeof(UIDispatchHeader) == 48);
// Reserved `flags` bit (mirrors shaders/ui-shared.glsl::UI_FLAG_CLIP).
// FillHeader sets it when the clip rect is narrower than the surface so
// the standard shaders can skip the per-pixel clip compares otherwise.
// The remaining low bits stay free for user-defined feature flags.
inline constexpr std::uint32_t kUIFlagClip = 0x80000000u;
// ─── standard item PODs (match GLSL std430) ─────────────────────────
struct QuadItem {
float x, y, w, h;
@ -165,6 +171,18 @@ export namespace Crafter {
std::array<float,4> clipRectPx = {0.0f, 0.0f, 1e9f, 1e9f},
std::uint32_t flags = 0) const noexcept;
// Builds the header `flags` word: the caller's user flags with the
// reserved kUIFlagClip bit set iff `clipRectPx` does not already cover
// the whole surface. When the clip rect spans the surface, the four
// per-pixel clip compares in uiResolveScreenPixel can never reject an
// in-surface pixel, so the bit stays clear and the shaders skip them.
// Static + dimension-parameterised so the decision is unit-testable
// without a live Window; FillHeader feeds it the current window size.
static std::uint32_t ClipFlags(std::array<float,4> clipRectPx,
std::uint32_t surfaceWidth,
std::uint32_t surfaceHeight,
std::uint32_t flags) noexcept;
void DispatchQuads(GraphicsCommandBuffer cmd, std::uint32_t bufferSlot, std::uint32_t itemCount,
std::array<float,4> clipRectPx = {0.0f, 0.0f, 1e9f, 1e9f});
void DispatchCircles(GraphicsCommandBuffer cmd, std::uint32_t bufferSlot, std::uint32_t itemCount,