Files
2026-11-07 16:52:53 +01:00

77 lines
6.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# UI Shader Design: UI Quads vs Text Glyphs (Review)
## Context
The Seele UI system (`external/Seele/src/Engine/UI/` + `res/shaders/UIPass.slang` + `src/Engine/Graphics/RenderPass/UIPass.cpp`) renders **both** plain UI quads (backgrounds, buttons) and text glyphs through a **single** instanced pipeline:
- `UIRender` (Element.h) — one struct for both kinds; `isGlyph` flag + `uvOffset`/`uvSize` (currently dead, always 0/1).
- `UIPass::beginFrame` flattens everything into one `StructuredBuffer<RenderElementStyle>` and one `draw(4, N)` instance call.
- `UIPass.slang` fragment: `if (textureIndex == -1) → flat color; else sample texture, use `.r` as coverage`.
**Question: is sharing the shader for UI elements and text glyphs the right design, or should they be separated?**
## Findings (what the code actually does today)
### 1. They are *not* really two different fragment programs
The two "cases" differ by **one branch on a data value** (`textureIndex == -1`), not by pipeline state:
- No blending difference (both use the same SRC_ALPHA one-minus-src-alpha blend — correct for both flat semi-transparent quads *and* alpha coverage).
- No depth/stencil difference (both in the same render pass, same depth config).
- No rasterization difference.
Same pipeline state → same pipeline. Splitting into two shaders would create a **second pipeline + second draw call** for what is literally one `if`. By Vulkan/Metal best practice this is the right unification.
### 2. The real divergence is in the **vertex stage**, and it is already half-broken
- `uvOffset`/`uvSize` exist on `UIRender` for "per-glyph rect in atlas space" but are **never uploaded** — `UIPass.cpp` has no fields for them in `RenderElementStyle` and the shader uses full 0..1 UVs.
- `FontAsset` (Asset/FontAsset.cpp:113) creates **one tiny texture per glyph** (each ~10×20 px, R8_UNORM). So "the glyph quad's UV rect in atlas space" is fiction today — every glyph texture is the whole glyph, full-rect UVs are accidentally correct.
- **This is the actual bug hiding inside the shared shader:** per-glyph textures means thousands of texture objects, thousands of sampler bindings, and the `usedTextures` array grows with unique glyph count (capped at the 1024 descriptor binding count — a hard ceiling that a real font will hit).
### 3. Per-frame cost of the shared path (minor, but real)
- Every `UIRender` uploads a 64-byte struct (color, position, padding, `element` back-ref space) even though the shader only needs ~40 bytes. Fine at editor scale, wasteful at game scale.
- `uiDescriptorLayout->reset()` + full re-bind every frame, 1024 sampler slots.
## Verdict
**Keep the single shader and single instanced draw. Do NOT split into a UI shader and a text shader.** The branch is trivial, the pipelines are state-identical, and one draw call for the whole overlay is a genuine win.
**But do fix the data path that the "same shader" idea is masking:**
| Priority | Change | Why |
|---|---|---|
| 1 (correctness/scale) | **Glyph atlas (per font, not per glyph)**: bake FreeType bitmaps into one N×N R8 texture at `loadGlyphs`, store UV rect per glyph; add `uvOffset`/`uvSize` to `RenderElementStyle` and use them in the shader (the fields already exist on `UIRender` — just wire them through). Removes the 1024-texture ceiling and thousands of GPU objects. | The current design will break at real font sizes; it only "works" for a handful of glyphs. |
| 2 (cleanliness) | Drop the `isGlyph` flag and the "glyph-only" comment block from `UIRender` — after the atlas fix, a glyph quad is just "a quad with a texture and a UV rect". UI quads with textures (icons, background images) use the exact same path. One concept, one struct. | The flag documents an accident, not a design. |
| 3 (later, if it ever matters) | Split the **vertex** stage: background quads don't need UVs at all; but with 4-vertex instancing the savings are ~zero. Skip unless profiling says otherwise. | YAGNI. |
What would justify *separating* in the future (none true today): different blend modes per category, MSAA-specific text rendering (e.g. LCD subpixel), or text needing its own post-process (SDF scaling). None of these are in scope for the editor UI.
## Files to modify (if the fix is approved)
| File | Change |
|---|---|
| `src/Engine/Asset/FontAsset.{h,cpp}` | Per-font atlas texture built once in `loadGlyphs`; `Glyph` stores `uvRect` instead of `OTexture2D`; `RenderGlyph` carries the rect. |
| `src/Engine/UI/Element.h` | `UIRender`: remove `isGlyph`; keep `uvOffset`/`uvSize` (now actually used; default full-rect). |
| `src/Engine/UI/Element/Text.h` | Emit the glyph's UV rect into `UIRender`. |
| `src/Engine/Graphics/RenderPass/UIPass.h/.cpp` | `RenderElementStyle`: add `uvOffset`/`uvSize`; upload from `UIRender`. |
| `res/shaders/UIPass.slang` | Sample with `uvOffset + uv * uvSize` (one line in fragment; no pipeline changes). |
## Steps
- [x] 1. Atlas: in `FontAsset::loadGlyphs`, pack glyph bitmaps (simple shelf/row packer is fine — font glyph count per size is bounded) into one R8 texture per `(font, size)`; replace `glyph.texture` with `glyph.uvRect` + a shared `fontSizes[size].atlas` texture.
- [x] 2. `RenderGlyph`/`UIRender`: carry the UV rect; drop `isGlyph` + dead comments.
- [x] 3. `UIPass`: extend `RenderElementStyle` with `uvOffset`/`uvSize`, upload them; shader samples `offset + uv*size`.
- [ ] 4. Verify: existing editor inspector (uses the same pass) still renders text identically; a paragraph of text no longer adds per-glyph textures (check via debug texture listing or descriptor count).
## Verification
1. Build engine (existing CMake variants) — compiles clean.
2. Run editor: inspector text renders pixel-identically to before (glyph coverage unchanged — same R8 data, just relocated in an atlas).
3. Stress: put a long string (all of Unicode Basic Multilingual Plane) in a `Text` element — must not crash on descriptor exhaustion (the 1024 cap is gone; only 1 atlas binding per font-size).
4. Confirm single draw call for the UI pass still (no extra pipeline state).
## Explicit non-goals
- No SDF/glow text, no subpixel AA, no different blend per category — none in scope.
- No vertex-stage split until profiling shows the 4-vertex overhead matters.