2026-07-22 18:09:06 +02:00
|
|
|
//SPDX-License-Identifier: LGPL-3.0-only
|
|
|
|
|
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
|
|
2026-05-18 04:58:52 +02:00
|
|
|
/*
|
|
|
|
|
Crafter®.Graphics
|
|
|
|
|
Copyright (C) 2026 Catcrafts®
|
|
|
|
|
catcrafts.net
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Backend-agnostic UIRenderer methods. FillHeader and ShapeText do not
|
|
|
|
|
// touch any GPU API; they only read window dimensions and (for ShapeText)
|
|
|
|
|
// the CPU-side font atlas. Split out so both Vulkan and WebGPU impls
|
|
|
|
|
// share the same source.
|
|
|
|
|
|
|
|
|
|
module Crafter.Graphics:UI_shared_impl;
|
|
|
|
|
import :UI;
|
|
|
|
|
import :Window;
|
|
|
|
|
import :Font;
|
|
|
|
|
import :FontAtlas;
|
|
|
|
|
import :GraphicsTypes;
|
|
|
|
|
import std;
|
|
|
|
|
|
|
|
|
|
using namespace Crafter;
|
|
|
|
|
|
|
|
|
|
UIDispatchHeader UIRenderer::FillHeader(std::uint32_t itemBufferSlot,
|
|
|
|
|
std::uint32_t itemCount,
|
|
|
|
|
std::array<float,4> clipRectPx,
|
|
|
|
|
std::uint32_t flags) const noexcept {
|
|
|
|
|
UIDispatchHeader h{};
|
|
|
|
|
h.outImage = outImageSlot_;
|
|
|
|
|
h.itemBuffer = itemBufferSlot;
|
|
|
|
|
h.surfaceWidth = window_->width;
|
|
|
|
|
h.surfaceHeight = window_->height;
|
|
|
|
|
h.clipX = clipRectPx[0];
|
|
|
|
|
h.clipY = clipRectPx[1];
|
|
|
|
|
h.clipW = clipRectPx[2];
|
|
|
|
|
h.clipH = clipRectPx[3];
|
|
|
|
|
h.itemCount = itemCount;
|
|
|
|
|
#ifndef CRAFTER_GRAPHICS_WINDOW_DOM
|
fix(window): size the swapchain for its present mode, and key frames in flight by frame instead of image
Window
- CreateSwapchain asked for exactly numFrames (3) images whatever the
surface said, read them into a three-element array, and keyed the
command buffers, the fences and the descriptor-heap copy holding "its"
swapchain image by the acquired image index. A swapchain of another
size fell back to FIFO (asked for MAILBOX or IMMEDIATE) or threw, and a
surface whose minimum is above three got a create info below it
(#160). Mesa's MAILBOX needs four images: Turnip on phoc, llvmpipe on
any Wayland compositor.
- The present mode is picked first, then the swapchain is sized by
SwapchainCapabilities: the surface's counts for that mode where the
instance has surface_maintenance1. With swapchain_maintenance1 the
swapchain names its mode (VkSwapchainPresentModesCreateInfoKHR), which
is what makes the mode's own count the one that applies, at creation and
to the forward progress of a UINT64_MAX acquire; without it the stricter
of the mode's and the surface's counts for all modes. It asks for
numFrames or that minimum where more, never past the maximum
(SwapchainMinImageCount). Turnip's FIFO keeps its three images and
MAILBOX gets four. The FIFO fallback and presentModeUnfit are gone.
- images, imageViewInfos (was imageViews: they are create-infos), the made
imageViews that descriptor sets take, imageInitialised and the
render-finished semaphores are per image, as many as the driver made.
The views are made once per swapchain. The semaphores only grow, since
a present of the old swapchain may still wait on one.
- Frames in flight stay numFrames, keyed by currentFrame, frameCounter %
numFrames: the command buffer, the fence, the acquire semaphore, the
heap copy (or set), RenderingElement3D::tlases, the frameIdx a pass is
recorded with. currentImage is the acquired image. currentBuffer is
gone, so code that used it stops compiling instead of picking the wrong
one; RenderPass::Record's frameIdx keeps its name and is now the frame
slot, as RenderPass.cppm says.
- The frame's fence is waited before the acquire. The acquire semaphores
need no spare then, and a frame waits for exactly the one numFrames
before it, which the deletion queue and the staging rings count on and
fences keyed by image did not promise once images come back out of
order. The swapchain slots are written before the fence is reset.
- From the acquire to the present the window is recording: a Resize or
SetPresentMode from a listener (a VSync key ticked in onUpdate) takes
effect after the present, through RebuildSwapchain, together with a
present that found the swapchain out of date. Rebuilding mid-frame used
to destroy the image the frame was drawing into; with images sized per
swapchain it would also index past the new one's end.
DescriptorHeapVulkan
- Frames and images pair up differently from frame to frame, so no heap
copy can keep one swapchain image. AddSwapchainSlot(slot, window) lists
an image slot as that window's swapchain image, and Window::Render
writes the image a frame acquired into its window's listed slots in that
frame's copy, or its set, before onUpdate, skipping a slot that holds it
already. A slot is one window's, so two windows can share a heap without
writing each other's slots under each other's frames in flight. A slot
listed during a frame its window records is written at once. A slot past
the heap's image slots, an empty ImageSlot's 0xFFFF among them, is
refused. Freeing a slot unlists it, as does initialising the heap in
either binding model. WriteImageSet, the one use of which this
replaces, is gone, and with it the per-frame dimension of setImageViews.
Device
- VK_KHR_surface_maintenance1 (or EXT) and VK_KHR_get_surface_capabilities2
are enabled where the loader has them (surfaceMaintenance1Supported,
vkGetPhysicalDeviceSurfaceCapabilities2KHR), and the matching
swapchain_maintenance1 with its feature where the device has it
(swapchainMaintenance1Supported).
- DistinctGpus groups the physical devices by GPU as CRAFTER_GRAPHICS_DEVICE
numbers them, for ChooseGpu and tests.
UI
- UIRenderer lists its output slot instead of writing every copy itself,
and again on every resize; the resize listener is gone on both
backends. The headers' frameIdx is currentFrame.
Examples
- VulkanTriangle, Sponza, SponzaBench and RTVolume list their output slot
and write only the per-frame TLASes. They wrote imageViews[f] into copy
f once, which a resize left pointing at destroyed images.
Tests
- SwapchainImageCount: SwapchainMinImageCount on the numbers drivers
report; every offered mode keeps its mode and gets what the window asks
for; every frame presents the colour it drew, read back a texel per
frame; a switch and a resize asked for during a frame wait for its
present; a slot listed during a frame holds its image at once; two
windows on one heap draw their own colours. The frame checks run again
in child processes, through CRAFTER_GRAPHICS_DEVICE, on a GPU whose
swapchains have more images than frames (this one, else llvmpipe), in
each binding model the parent didn't cover; a child skips only for a GPU
that can't run the library, and checks it runs the model it was asked
for.
- FrameLoopSync checks the frame slot follows the frame counter and counts
the swapchain's own images. HeapASRead, HeapDynamicIndexHit and
RTPassPushData list their swapchain slot and drop their resize rewrites.
Docs
- README, AGENTS.md and AGENTS.consumer.md: frames and images, the
swapchain slots, rebuilds held to the present, the new optional groups.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 08:51:18 +01:00
|
|
|
h.frameIdx = window_->currentFrame;
|
2026-05-18 04:58:52 +02:00
|
|
|
#else
|
|
|
|
|
h.frameIdx = 0;
|
|
|
|
|
#endif
|
2026-06-16 16:57:07 +00:00
|
|
|
h.flags = ClipFlags(clipRectPx, window_->width, window_->height, flags);
|
2026-05-18 04:58:52 +02:00
|
|
|
h._pad = 0;
|
|
|
|
|
return h;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-16 16:57:07 +00:00
|
|
|
std::uint32_t UIRenderer::ClipFlags(std::array<float,4> clipRectPx,
|
|
|
|
|
std::uint32_t surfaceWidth,
|
|
|
|
|
std::uint32_t surfaceHeight,
|
|
|
|
|
std::uint32_t flags) noexcept {
|
|
|
|
|
// The reserved kUIFlagClip bit signals "clip rect is narrower than the
|
|
|
|
|
// surface". When the rect already covers the whole surface (the common
|
|
|
|
|
// {0,0,1e9,1e9} default), the four per-pixel clip compares in
|
|
|
|
|
// uiResolveScreenPixel can never reject an in-surface pixel, so we leave
|
|
|
|
|
// the bit clear and the shader skips them — pixel-identical output.
|
|
|
|
|
const bool clipCoversSurface =
|
|
|
|
|
clipRectPx[0] <= 0.0f && clipRectPx[1] <= 0.0f
|
|
|
|
|
&& clipRectPx[0] + clipRectPx[2] >= static_cast<float>(surfaceWidth)
|
|
|
|
|
&& clipRectPx[1] + clipRectPx[3] >= static_cast<float>(surfaceHeight);
|
|
|
|
|
return clipCoversSurface ? (flags & ~kUIFlagClip) : (flags | kUIFlagClip);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 04:58:52 +02:00
|
|
|
std::uint32_t UIRenderer::ShapeText(Font& font, float pxSize,
|
|
|
|
|
float x, float baselineY,
|
|
|
|
|
std::string_view utf8,
|
|
|
|
|
std::array<float,4> color,
|
|
|
|
|
GlyphItem* out, std::uint32_t outCapacity,
|
perf(text): cache shaped runs in ShapeText (#52)
ShapeText re-decoded UTF-8, re-looked-up every glyph, and recomputed
layout every frame for static strings (DrawText / EmitCenteredLabel run
inside per-frame onBuild). DrawText and EmitCenteredLabel also did a
second full pass over the glyphs to apply alignment.
Memoize the shaped run, keyed by (Font*, pxSize, color, utf8), as an
origin-relative vector<GlyphItem>. A cache hit skips decode/lookup/layout
and just translates the cached run into the output buffer by
(x + alignShift, baselineY). The alignment shift (Center = -advance/2,
Right = -advance) is folded into ShapeText's single pass, dropping the
second loop in both callers. TextAlign moves to the :UI partition so
ShapeText can take it.
Pure CPU memoization — the glyph buffer is fully rewritten and re-flushed
every frame, so a hit is byte-equivalent. A miss still calls Ensure so new
glyphs rasterise and dirty the atlas; hits don't (atlas entries are never
evicted). InvalidateFont drops a font's runs before it is destroyed (the
cache, like FontAtlas's glyph cache, keys on the Font pointer). A soft cap
bounds memory against pathological per-frame-unique text.
Adds the ShapeTextCache test (15 checks): hit == miss byte-for-byte, hits
don't touch the atlas, translate/alignment/truncation/InvalidateFont.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 15:40:52 +00:00
|
|
|
float* outAdvance,
|
|
|
|
|
TextAlign align) {
|
2026-05-18 04:58:52 +02:00
|
|
|
if (fontAtlas == nullptr) {
|
|
|
|
|
std::println("UIRenderer::ShapeText: no FontAtlas (set fontAtlas before Initialize)");
|
|
|
|
|
std::abort();
|
|
|
|
|
}
|
|
|
|
|
|
perf(text): cache shaped runs in ShapeText (#52)
ShapeText re-decoded UTF-8, re-looked-up every glyph, and recomputed
layout every frame for static strings (DrawText / EmitCenteredLabel run
inside per-frame onBuild). DrawText and EmitCenteredLabel also did a
second full pass over the glyphs to apply alignment.
Memoize the shaped run, keyed by (Font*, pxSize, color, utf8), as an
origin-relative vector<GlyphItem>. A cache hit skips decode/lookup/layout
and just translates the cached run into the output buffer by
(x + alignShift, baselineY). The alignment shift (Center = -advance/2,
Right = -advance) is folded into ShapeText's single pass, dropping the
second loop in both callers. TextAlign moves to the :UI partition so
ShapeText can take it.
Pure CPU memoization — the glyph buffer is fully rewritten and re-flushed
every frame, so a hit is byte-equivalent. A miss still calls Ensure so new
glyphs rasterise and dirty the atlas; hits don't (atlas entries are never
evicted). InvalidateFont drops a font's runs before it is destroyed (the
cache, like FontAtlas's glyph cache, keys on the Font pointer). A soft cap
bounds memory against pathological per-frame-unique text.
Adds the ShapeTextCache test (15 checks): hit == miss byte-for-byte, hits
don't touch the atlas, translate/alignment/truncation/InvalidateFont.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 15:40:52 +00:00
|
|
|
// Look up (or build) the origin-relative shaped run. It is stored with
|
|
|
|
|
// the pen starting at x=0, baseline=0 so a cache hit only needs the
|
|
|
|
|
// translate below. The whole string is shaped (no outCapacity limit) so
|
|
|
|
|
// a later call with a larger buffer still gets the full run.
|
2026-06-18 13:19:11 +00:00
|
|
|
// Probe with a borrowing view key (text is a string_view onto utf8) so a
|
|
|
|
|
// cache hit copies and hashes no string. The owning std::string is only
|
|
|
|
|
// built on a miss, below, for the emplace.
|
|
|
|
|
ShapedRunViewKey viewKey{&font, pxSize, color, utf8};
|
|
|
|
|
auto it = shapedRuns_.find(viewKey);
|
perf(text): cache shaped runs in ShapeText (#52)
ShapeText re-decoded UTF-8, re-looked-up every glyph, and recomputed
layout every frame for static strings (DrawText / EmitCenteredLabel run
inside per-frame onBuild). DrawText and EmitCenteredLabel also did a
second full pass over the glyphs to apply alignment.
Memoize the shaped run, keyed by (Font*, pxSize, color, utf8), as an
origin-relative vector<GlyphItem>. A cache hit skips decode/lookup/layout
and just translates the cached run into the output buffer by
(x + alignShift, baselineY). The alignment shift (Center = -advance/2,
Right = -advance) is folded into ShapeText's single pass, dropping the
second loop in both callers. TextAlign moves to the :UI partition so
ShapeText can take it.
Pure CPU memoization — the glyph buffer is fully rewritten and re-flushed
every frame, so a hit is byte-equivalent. A miss still calls Ensure so new
glyphs rasterise and dirty the atlas; hits don't (atlas entries are never
evicted). InvalidateFont drops a font's runs before it is destroyed (the
cache, like FontAtlas's glyph cache, keys on the Font pointer). A soft cap
bounds memory against pathological per-frame-unique text.
Adds the ShapeTextCache test (15 checks): hit == miss byte-for-byte, hits
don't touch the atlas, translate/alignment/truncation/InvalidateFont.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 15:40:52 +00:00
|
|
|
if (it == shapedRuns_.end()) {
|
|
|
|
|
// Miss: shape from scratch. Ensure() each glyph so brand-new ones
|
|
|
|
|
// rasterise into the atlas and mark it dirty for the next upload —
|
|
|
|
|
// a hit skips this, which is safe because atlas entries are never
|
|
|
|
|
// evicted once placed.
|
|
|
|
|
const float scale = pxSize / FontAtlas::kBaseSize;
|
|
|
|
|
ShapedRun run;
|
|
|
|
|
float cursor = 0.0f;
|
|
|
|
|
std::size_t i = 0;
|
|
|
|
|
while (i < utf8.size()) {
|
|
|
|
|
std::uint32_t cp = DecodeUtf8(utf8, i);
|
|
|
|
|
if (cp == 0) break;
|
|
|
|
|
if (cp == '\n') { continue; }
|
2026-05-18 04:58:52 +02:00
|
|
|
|
2026-06-16 15:43:58 +00:00
|
|
|
const Glyph* g = fontAtlas->EnsureAndGet(font, cp);
|
perf(text): cache shaped runs in ShapeText (#52)
ShapeText re-decoded UTF-8, re-looked-up every glyph, and recomputed
layout every frame for static strings (DrawText / EmitCenteredLabel run
inside per-frame onBuild). DrawText and EmitCenteredLabel also did a
second full pass over the glyphs to apply alignment.
Memoize the shaped run, keyed by (Font*, pxSize, color, utf8), as an
origin-relative vector<GlyphItem>. A cache hit skips decode/lookup/layout
and just translates the cached run into the output buffer by
(x + alignShift, baselineY). The alignment shift (Center = -advance/2,
Right = -advance) is folded into ShapeText's single pass, dropping the
second loop in both callers. TextAlign moves to the :UI partition so
ShapeText can take it.
Pure CPU memoization — the glyph buffer is fully rewritten and re-flushed
every frame, so a hit is byte-equivalent. A miss still calls Ensure so new
glyphs rasterise and dirty the atlas; hits don't (atlas entries are never
evicted). InvalidateFont drops a font's runs before it is destroyed (the
cache, like FontAtlas's glyph cache, keys on the Font pointer). A soft cap
bounds memory against pathological per-frame-unique text.
Adds the ShapeTextCache test (15 checks): hit == miss byte-for-byte, hits
don't touch the atlas, translate/alignment/truncation/InvalidateFont.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 15:40:52 +00:00
|
|
|
if (g == nullptr) continue;
|
2026-05-18 04:58:52 +02:00
|
|
|
|
perf(text): cache shaped runs in ShapeText (#52)
ShapeText re-decoded UTF-8, re-looked-up every glyph, and recomputed
layout every frame for static strings (DrawText / EmitCenteredLabel run
inside per-frame onBuild). DrawText and EmitCenteredLabel also did a
second full pass over the glyphs to apply alignment.
Memoize the shaped run, keyed by (Font*, pxSize, color, utf8), as an
origin-relative vector<GlyphItem>. A cache hit skips decode/lookup/layout
and just translates the cached run into the output buffer by
(x + alignShift, baselineY). The alignment shift (Center = -advance/2,
Right = -advance) is folded into ShapeText's single pass, dropping the
second loop in both callers. TextAlign moves to the :UI partition so
ShapeText can take it.
Pure CPU memoization — the glyph buffer is fully rewritten and re-flushed
every frame, so a hit is byte-equivalent. A miss still calls Ensure so new
glyphs rasterise and dirty the atlas; hits don't (atlas entries are never
evicted). InvalidateFont drops a font's runs before it is destroyed (the
cache, like FontAtlas's glyph cache, keys on the Font pointer). A soft cap
bounds memory against pathological per-frame-unique text.
Adds the ShapeTextCache test (15 checks): hit == miss byte-for-byte, hits
don't touch the atlas, translate/alignment/truncation/InvalidateFont.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 15:40:52 +00:00
|
|
|
if (g->w > 0 && g->h > 0) {
|
|
|
|
|
GlyphItem gi;
|
|
|
|
|
gi.x = cursor + g->xoff * scale;
|
|
|
|
|
gi.y = g->yoff * scale;
|
|
|
|
|
gi.w = g->w * scale;
|
|
|
|
|
gi.h = g->h * scale;
|
|
|
|
|
gi.u0 = g->u0; gi.v0 = g->v0;
|
|
|
|
|
gi.u1 = g->u1; gi.v1 = g->v1;
|
|
|
|
|
gi.r = color[0]; gi.g = color[1]; gi.b = color[2]; gi.a = color[3];
|
|
|
|
|
run.glyphs.push_back(gi);
|
|
|
|
|
}
|
|
|
|
|
cursor += g->advance * scale;
|
2026-05-18 04:58:52 +02:00
|
|
|
}
|
perf(text): cache shaped runs in ShapeText (#52)
ShapeText re-decoded UTF-8, re-looked-up every glyph, and recomputed
layout every frame for static strings (DrawText / EmitCenteredLabel run
inside per-frame onBuild). DrawText and EmitCenteredLabel also did a
second full pass over the glyphs to apply alignment.
Memoize the shaped run, keyed by (Font*, pxSize, color, utf8), as an
origin-relative vector<GlyphItem>. A cache hit skips decode/lookup/layout
and just translates the cached run into the output buffer by
(x + alignShift, baselineY). The alignment shift (Center = -advance/2,
Right = -advance) is folded into ShapeText's single pass, dropping the
second loop in both callers. TextAlign moves to the :UI partition so
ShapeText can take it.
Pure CPU memoization — the glyph buffer is fully rewritten and re-flushed
every frame, so a hit is byte-equivalent. A miss still calls Ensure so new
glyphs rasterise and dirty the atlas; hits don't (atlas entries are never
evicted). InvalidateFont drops a font's runs before it is destroyed (the
cache, like FontAtlas's glyph cache, keys on the Font pointer). A soft cap
bounds memory against pathological per-frame-unique text.
Adds the ShapeTextCache test (15 checks): hit == miss byte-for-byte, hits
don't touch the atlas, translate/alignment/truncation/InvalidateFont.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 15:40:52 +00:00
|
|
|
run.advance = cursor;
|
|
|
|
|
|
2026-06-18 13:23:34 +00:00
|
|
|
// Evict the least-recently-used run (LRU list back) on overflow rather
|
|
|
|
|
// than clearing the whole cache. The back is whatever has gone longest
|
|
|
|
|
// without a reshape — a stale unique string, never a stable label
|
|
|
|
|
// (those are reshaped every frame and sit at the front), so the hot set
|
|
|
|
|
// survives and the next frame doesn't trigger a full-UI reshape.
|
|
|
|
|
if (shapedRuns_.size() >= kMaxShapedRuns) {
|
|
|
|
|
const ShapedRunKey* victim = shapedRunsLru_.back();
|
|
|
|
|
shapedRunsLru_.pop_back();
|
|
|
|
|
shapedRuns_.erase(*victim);
|
|
|
|
|
}
|
2026-06-18 13:29:49 +00:00
|
|
|
// Materialise the owning key (the probe above borrowed utf8) only now,
|
|
|
|
|
// on the miss path, for the emplace.
|
2026-06-18 13:19:11 +00:00
|
|
|
ShapedRunKey key{&font, pxSize, color, std::string(utf8)};
|
perf(text): cache shaped runs in ShapeText (#52)
ShapeText re-decoded UTF-8, re-looked-up every glyph, and recomputed
layout every frame for static strings (DrawText / EmitCenteredLabel run
inside per-frame onBuild). DrawText and EmitCenteredLabel also did a
second full pass over the glyphs to apply alignment.
Memoize the shaped run, keyed by (Font*, pxSize, color, utf8), as an
origin-relative vector<GlyphItem>. A cache hit skips decode/lookup/layout
and just translates the cached run into the output buffer by
(x + alignShift, baselineY). The alignment shift (Center = -advance/2,
Right = -advance) is folded into ShapeText's single pass, dropping the
second loop in both callers. TextAlign moves to the :UI partition so
ShapeText can take it.
Pure CPU memoization — the glyph buffer is fully rewritten and re-flushed
every frame, so a hit is byte-equivalent. A miss still calls Ensure so new
glyphs rasterise and dirty the atlas; hits don't (atlas entries are never
evicted). InvalidateFont drops a font's runs before it is destroyed (the
cache, like FontAtlas's glyph cache, keys on the Font pointer). A soft cap
bounds memory against pathological per-frame-unique text.
Adds the ShapeTextCache test (15 checks): hit == miss byte-for-byte, hits
don't touch the atlas, translate/alignment/truncation/InvalidateFont.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 15:40:52 +00:00
|
|
|
it = shapedRuns_.emplace(std::move(key), std::move(run)).first;
|
2026-06-18 13:23:34 +00:00
|
|
|
it->second.lruIt = shapedRunsLru_.insert(shapedRunsLru_.begin(), &it->first);
|
|
|
|
|
} else {
|
|
|
|
|
// Hit: promote to most-recently-used. splice is O(1) and keeps lruIt
|
|
|
|
|
// (and every other iterator) valid.
|
|
|
|
|
shapedRunsLru_.splice(shapedRunsLru_.begin(), shapedRunsLru_, it->second.lruIt);
|
2026-05-18 04:58:52 +02:00
|
|
|
}
|
|
|
|
|
|
perf(text): cache shaped runs in ShapeText (#52)
ShapeText re-decoded UTF-8, re-looked-up every glyph, and recomputed
layout every frame for static strings (DrawText / EmitCenteredLabel run
inside per-frame onBuild). DrawText and EmitCenteredLabel also did a
second full pass over the glyphs to apply alignment.
Memoize the shaped run, keyed by (Font*, pxSize, color, utf8), as an
origin-relative vector<GlyphItem>. A cache hit skips decode/lookup/layout
and just translates the cached run into the output buffer by
(x + alignShift, baselineY). The alignment shift (Center = -advance/2,
Right = -advance) is folded into ShapeText's single pass, dropping the
second loop in both callers. TextAlign moves to the :UI partition so
ShapeText can take it.
Pure CPU memoization — the glyph buffer is fully rewritten and re-flushed
every frame, so a hit is byte-equivalent. A miss still calls Ensure so new
glyphs rasterise and dirty the atlas; hits don't (atlas entries are never
evicted). InvalidateFont drops a font's runs before it is destroyed (the
cache, like FontAtlas's glyph cache, keys on the Font pointer). A soft cap
bounds memory against pathological per-frame-unique text.
Adds the ShapeTextCache test (15 checks): hit == miss byte-for-byte, hits
don't touch the atlas, translate/alignment/truncation/InvalidateFont.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 15:40:52 +00:00
|
|
|
const ShapedRun& run = it->second;
|
|
|
|
|
if (outAdvance) *outAdvance = run.advance;
|
|
|
|
|
|
|
|
|
|
// Fold the alignment shift into the translate so callers don't need a
|
|
|
|
|
// second pass over the glyphs.
|
|
|
|
|
const float alignShift = (align == TextAlign::Center) ? -run.advance * 0.5f
|
|
|
|
|
: (align == TextAlign::Right) ? -run.advance
|
|
|
|
|
: 0.0f;
|
|
|
|
|
const float tx = x + alignShift;
|
|
|
|
|
|
|
|
|
|
std::uint32_t written = 0;
|
|
|
|
|
for (const GlyphItem& src : run.glyphs) {
|
|
|
|
|
if (written >= outCapacity) break;
|
|
|
|
|
GlyphItem& gi = out[written++];
|
|
|
|
|
gi = src;
|
|
|
|
|
gi.x += tx;
|
|
|
|
|
gi.y += baselineY;
|
|
|
|
|
}
|
2026-05-18 04:58:52 +02:00
|
|
|
return written;
|
|
|
|
|
}
|
perf(text): cache shaped runs in ShapeText (#52)
ShapeText re-decoded UTF-8, re-looked-up every glyph, and recomputed
layout every frame for static strings (DrawText / EmitCenteredLabel run
inside per-frame onBuild). DrawText and EmitCenteredLabel also did a
second full pass over the glyphs to apply alignment.
Memoize the shaped run, keyed by (Font*, pxSize, color, utf8), as an
origin-relative vector<GlyphItem>. A cache hit skips decode/lookup/layout
and just translates the cached run into the output buffer by
(x + alignShift, baselineY). The alignment shift (Center = -advance/2,
Right = -advance) is folded into ShapeText's single pass, dropping the
second loop in both callers. TextAlign moves to the :UI partition so
ShapeText can take it.
Pure CPU memoization — the glyph buffer is fully rewritten and re-flushed
every frame, so a hit is byte-equivalent. A miss still calls Ensure so new
glyphs rasterise and dirty the atlas; hits don't (atlas entries are never
evicted). InvalidateFont drops a font's runs before it is destroyed (the
cache, like FontAtlas's glyph cache, keys on the Font pointer). A soft cap
bounds memory against pathological per-frame-unique text.
Adds the ShapeTextCache test (15 checks): hit == miss byte-for-byte, hits
don't touch the atlas, translate/alignment/truncation/InvalidateFont.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 15:40:52 +00:00
|
|
|
|
|
|
|
|
void UIRenderer::InvalidateFont(const Font& font) noexcept {
|
2026-06-18 13:23:34 +00:00
|
|
|
// Drop matching runs from both the map and the LRU order list, keeping the
|
|
|
|
|
// two in sync (the list points at keys owned by the map).
|
|
|
|
|
for (auto it = shapedRuns_.begin(); it != shapedRuns_.end(); ) {
|
|
|
|
|
if (it->first.font == &font) {
|
|
|
|
|
shapedRunsLru_.erase(it->second.lruIt);
|
|
|
|
|
it = shapedRuns_.erase(it);
|
|
|
|
|
} else {
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UIRenderer::IsShapedRunCached(const Font& font, float pxSize,
|
|
|
|
|
std::array<float,4> color,
|
|
|
|
|
std::string_view utf8) const {
|
2026-06-18 13:29:49 +00:00
|
|
|
return shapedRuns_.find(ShapedRunViewKey{&font, pxSize, color, utf8})
|
2026-06-18 13:23:34 +00:00
|
|
|
!= shapedRuns_.end();
|
perf(text): cache shaped runs in ShapeText (#52)
ShapeText re-decoded UTF-8, re-looked-up every glyph, and recomputed
layout every frame for static strings (DrawText / EmitCenteredLabel run
inside per-frame onBuild). DrawText and EmitCenteredLabel also did a
second full pass over the glyphs to apply alignment.
Memoize the shaped run, keyed by (Font*, pxSize, color, utf8), as an
origin-relative vector<GlyphItem>. A cache hit skips decode/lookup/layout
and just translates the cached run into the output buffer by
(x + alignShift, baselineY). The alignment shift (Center = -advance/2,
Right = -advance) is folded into ShapeText's single pass, dropping the
second loop in both callers. TextAlign moves to the :UI partition so
ShapeText can take it.
Pure CPU memoization — the glyph buffer is fully rewritten and re-flushed
every frame, so a hit is byte-equivalent. A miss still calls Ensure so new
glyphs rasterise and dirty the atlas; hits don't (atlas entries are never
evicted). InvalidateFont drops a font's runs before it is destroyed (the
cache, like FontAtlas's glyph cache, keys on the Font pointer). A soft cap
bounds memory against pathological per-frame-unique text.
Adds the ShapeTextCache test (15 checks): hit == miss byte-for-byte, hits
don't touch the atlas, translate/alignment/truncation/InvalidateFont.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 15:40:52 +00:00
|
|
|
}
|