From f243737fb94c00ed2aeb61918e42eab02ee392b0 Mon Sep 17 00:00:00 2001 From: 4DBug <115124145+4DBug@users.noreply.github.com> Date: Tue, 20 Oct 2026 14:06:20 -0500 Subject: [PATCH] Faster glyph baking: atlas repack finds RectsIndex entries by the loop index (index_from_ptr was O(n^2) per repack), stb_truetype vertical edges (all of ProggyClean's) without stbtt__handle_clipped_edge calls, FlattenCurves with locals + inlined add_point, @native trunc (sorted-edges 215 -> 107 ms, repack 330 -> 184 ms on bake bench; glyph bitmaps, atlas and draw hashes unchanged) --- imgui_draw.lua | 15 ++++++++-- imstb_truetype.lua | 72 +++++++++++++++++++++++++++++----------------- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/imgui_draw.lua b/imgui_draw.lua index 174fe27..404f78d 100644 --- a/imgui_draw.lua +++ b/imgui_draw.lua @@ -1025,6 +1025,10 @@ function ImFontAtlasRemoveDrawListSharedData(atlas, data) atlas.DrawListSharedDatas:find_erase(data) end +-- [Luau] RectsIndex index of the entry the repack loop is re-adding: ImFontAtlasPackReuseRectEntry() checks it before +-- searching RectsIndex (index_from_ptr is a linear search: O(n^2) per repack otherwise) +local ImFontAtlasPackReuseIndexHint = nil + --- @param atlas ImFontAtlas --- @param w int --- @param h int @@ -1052,7 +1056,7 @@ local function ImFontAtlasTextureRepack(atlas, w, h) local old_index = builder.RectsIndex:copy() old_rects:swap(builder.Rects) - for _, index_entry in builder.RectsIndex:iter() do + for index_n, index_entry in builder.RectsIndex:iter() do repeat if index_entry.IsUsed == false then break @@ -1063,7 +1067,9 @@ local function ImFontAtlasTextureRepack(atlas, w, h) break end + ImFontAtlasPackReuseIndexHint = index_n - 1 local new_r_id = ImFontAtlasPackAddRect(atlas, old_r.w, old_r.h, index_entry) + ImFontAtlasPackReuseIndexHint = nil if new_r_id == ImFontAtlasRectId_Invalid then -- Undo, grow texture and try repacking again. -- FIXME-NEWATLAS-TESTS: This is a very rarely exercised path! It needs to be automatically tested properly. @@ -1078,7 +1084,7 @@ local function ImFontAtlasTextureRepack(atlas, w, h) return end - IM_ASSERT(ImFontAtlasRectId_GetIndex(new_r_id) == builder.RectsIndex:index_from_ptr(index_entry)) + IM_ASSERT(ImFontAtlasRectId_GetIndex(new_r_id) == index_n - 1) -- [Luau] == RectsIndex:index_from_ptr(index_entry) local new_r = ImFontAtlasPackGetRect(atlas, new_r_id) ImFontAtlasTextureBlockCopy(old_tex, old_r.x, old_r.y, new_tex, new_r.x, new_r.y, new_r.w, new_r.h) @@ -1193,7 +1199,10 @@ end local function ImFontAtlasPackReuseRectEntry(atlas, index_entry) IM_ASSERT(index_entry.IsUsed) index_entry.TargetIndex = atlas.Builder.Rects.Size - 1 - local index_idx = atlas.Builder.RectsIndex:index_from_ptr(index_entry) + local index_idx = ImFontAtlasPackReuseIndexHint + if index_idx == nil or atlas.Builder.RectsIndex.Data[index_idx + 1] ~= index_entry then + index_idx = atlas.Builder.RectsIndex:index_from_ptr(index_entry) + end return ImFontAtlasRectId_Make(index_idx, index_entry.Generation) end diff --git a/imstb_truetype.lua b/imstb_truetype.lua index 3b9d66f..82e3866 100644 --- a/imstb_truetype.lua +++ b/imstb_truetype.lua @@ -213,7 +213,8 @@ local STBTT_fabs = math.abs local STBTT_ifloor = math.floor local STBTT_iceil = math.ceil -local trunc = function(x) +@native +local function trunc(x) if x >= 0 then return math.floor(x) else @@ -2066,11 +2067,25 @@ local function stbtt__fill_active_edges_new(scanline, scanline_fill, len, e, y_t if e.fdx == 0 then local x0 = e.fx if x0 < len then - if x0 >= 0 then - stbtt__handle_clipped_edge(scanline, trunc(x0), e, x0, y_top, x0, y_bottom) - stbtt__handle_clipped_edge(scanline_fill, trunc(x0) + 1, e, x0, y_top, x0, y_bottom) - else - stbtt__handle_clipped_edge(scanline_fill, 0, e, x0, y_top, x0, y_bottom) + -- [Luau] the stbtt__handle_clipped_edge() calls, inlined (every edge of pixel fonts like ProggyClean is + -- vertical): x0 == x1, so clipping to sy/ey keeps x, and the same arithmetic gives the same results + local y0, y1 = y_top, y_bottom + local sy, ey = e.sy, e.ey + if y0 <= ey and y1 >= sy then + if y0 < sy then y0 = sy end + if y1 > ey then y1 = ey end + local height = e.direction * (y1 - y0) + if x0 >= 0 then + local x = math.floor(x0) -- trunc(x0) + if x0 <= x then + scanline[x] = scanline[x] + height + else + scanline[x] = scanline[x] + height * (1 - ((x0 - x) + (x0 - x)) / 2) + end + scanline_fill[x + 1] = scanline_fill[x + 1] + height + else + scanline_fill[0] = scanline_fill[0] + height + end end end else @@ -2615,9 +2630,12 @@ local function stbtt_FlattenCurves(vertices, num_verts, objspace_flatness) local n = 0 local start = 0 + -- [Luau] vertex types and the current vertex in locals, stbtt__add_point() inlined (same values) + local VTX_MOVE, VTX_LINE, VTX_CURVE, VTX_CUBIC = STBTT_VTX.MOVE, STBTT_VTX.LINE, STBTT_VTX.CURVE, STBTT_VTX.CUBIC + -- count how many "moves" there are to get the contour count for i = 1, num_verts do - if vertices[i].type == STBTT_VTX.MOVE then + if vertices[i].type == VTX_MOVE then n = n + 1 end end @@ -2637,7 +2655,9 @@ local function stbtt_FlattenCurves(vertices, num_verts, objspace_flatness) num_points = 0 n = -1 for i = 1, num_verts do - if vertices[i].type == STBTT_VTX.MOVE then + local v = vertices[i] + local vtype = v.type + if vtype == VTX_MOVE then -- start the next contour if n >= 0 then contour_lengths[n + 1] = num_points - start @@ -2645,30 +2665,30 @@ local function stbtt_FlattenCurves(vertices, num_verts, objspace_flatness) n = n + 1 start = num_points - x = vertices[i].x - y = vertices[i].y - stbtt__add_point(points, num_points, x, y) + x = v.x + y = v.y num_points = num_points + 1 - elseif vertices[i].type == STBTT_VTX.LINE then - x = vertices[i].x - y = vertices[i].y - stbtt__add_point(points, num_points, x, y) + if points then local pt = points[num_points]; pt.x = x; pt.y = y end -- stbtt__add_point() + elseif vtype == VTX_LINE then + x = v.x + y = v.y num_points = num_points + 1 - elseif vertices[i].type == STBTT_VTX.CURVE then + if points then local pt = points[num_points]; pt.x = x; pt.y = y end -- stbtt__add_point() + elseif vtype == VTX_CURVE then num_points = stbtt__tesselate_curve(points, num_points, x, y, - vertices[i].cx, vertices[i].cy, - vertices[i].x, vertices[i].y, + v.cx, v.cy, + v.x, v.y, objspace_flatness_squared, 0) - x = vertices[i].x - y = vertices[i].y - elseif vertices[i].type == STBTT_VTX.CUBIC then + x = v.x + y = v.y + elseif vtype == VTX_CUBIC then num_points = stbtt__tesselate_cubic(points, num_points, x, y, - vertices[i].cx, vertices[i].cy, - vertices[i].cx1, vertices[i].cy1, - vertices[i].x, vertices[i].y, + v.cx, v.cy, + v.cx1, v.cy1, + v.x, v.y, objspace_flatness_squared, 0) - x = vertices[i].x - y = vertices[i].y + x = v.x + y = v.y end end contour_lengths[n + 1] = num_points - start