From 0436fba13cea6ad527c3fcbf6a6e20c6a490fd78 Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 31 Mar 2025 22:38:05 +0200 Subject: [PATCH] Fonts: fixed compaction gc-ing baked fonts used in the current frame + rename. --- imgui_draw.cpp | 23 ++++++++++++----------- imgui_internal.h | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/imgui_draw.cpp b/imgui_draw.cpp index a20d63bd0..3feba78a1 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -3983,7 +3983,7 @@ void ImFontAtlasBuildRepackTexture(ImFontAtlas* atlas, int w, int h) for (ImFontAtlasRectEntry& index_entry : builder->RectsIndex) { - if (index_entry.Used == false) + if (index_entry.IsUsed == false) continue; ImTextureRect& old_r = old_rects[index_entry.TargetIndex]; if (old_r.w == 0 && old_r.h == 0) @@ -4124,7 +4124,7 @@ void ImFontAtlasBuildClear(ImFontAtlas* atlas) void ImFontAtlasBuildCompactTexture(ImFontAtlas* atlas) { ImFontAtlasBuilder* builder = atlas->Builder; - ImFontAtlasBuildDiscardBakes(atlas, 0); + ImFontAtlasBuildDiscardBakes(atlas, 1); ImTextureData* old_tex = atlas->TexData; ImVec2i old_tex_size = ImVec2i(old_tex->Width, old_tex->Height); @@ -4235,19 +4235,20 @@ static ImFontAtlasRectId ImFontAtlasPackAllocRectEntry(ImFontAtlas* atlas, int r { index_idx = builder->RectsIndexFreeListStart; index_entry = &builder->RectsIndex[index_idx]; - IM_ASSERT(index_entry->Used == false); + IM_ASSERT(index_entry->IsUsed == false); builder->RectsIndexFreeListStart = index_entry->TargetIndex; } index_entry->TargetIndex = rect_idx; - index_entry->Used = 1; + index_entry->IsUsed = 1; return (ImFontAtlasRectId)index_idx; } -static ImFontAtlasRectId ImFontAtlasPackReuseRectEntry(ImFontAtlas* atlas, ImFontAtlasRectEntry* overwrite_entry) +// Overwrite existing entry +static ImFontAtlasRectId ImFontAtlasPackReuseRectEntry(ImFontAtlas* atlas, ImFontAtlasRectEntry* index_entry) { - IM_ASSERT(overwrite_entry->Used); - overwrite_entry->TargetIndex = atlas->Builder->Rects.Size - 1; - return atlas->Builder->RectsIndex.index_from_ptr(overwrite_entry); + IM_ASSERT(index_entry->IsUsed); + index_entry->TargetIndex = atlas->Builder->Rects.Size - 1; + return atlas->Builder->RectsIndex.index_from_ptr(index_entry); } // This is expected to be called in batches and followed by a repack @@ -4256,10 +4257,10 @@ void ImFontAtlasPackDiscardRect(ImFontAtlas* atlas, ImFontAtlasRectId id) IM_ASSERT(id != ImFontAtlasRectId_Invalid); ImFontAtlasBuilder* builder = (ImFontAtlasBuilder*)atlas->Builder; ImFontAtlasRectEntry* index_entry = &builder->RectsIndex[id]; - IM_ASSERT(index_entry->Used && index_entry->TargetIndex >= 0); + IM_ASSERT(index_entry->IsUsed && index_entry->TargetIndex >= 0); ImTextureRect* rect = ImFontAtlasPackGetRect(atlas, id); - index_entry->Used = false; + index_entry->IsUsed = false; index_entry->TargetIndex = builder->RectsIndexFreeListStart; const int pack_padding = atlas->TexGlyphPadding; @@ -4324,7 +4325,7 @@ ImTextureRect* ImFontAtlasPackGetRect(ImFontAtlas* atlas, ImFontAtlasRectId id) IM_ASSERT(id != ImFontAtlasRectId_Invalid); ImFontAtlasBuilder* builder = (ImFontAtlasBuilder*)atlas->Builder; ImFontAtlasRectEntry* index_entry = &builder->RectsIndex[id]; - IM_ASSERT(index_entry->Used); + IM_ASSERT(index_entry->IsUsed); return &builder->Rects[index_entry->TargetIndex]; } diff --git a/imgui_internal.h b/imgui_internal.h index 009f6f9c9..3e519325a 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -3702,7 +3702,7 @@ IMGUI_API const ImFontLoader* ImFontAtlasGetFontLoaderForStbTruetype(); struct ImFontAtlasRectEntry { int TargetIndex : 31; // When Used: ImFontAtlasRectId -> into Rects[]. When unused: index to next unused RectsIndex[] slot to consume free-list. - unsigned int Used : 1; + unsigned int IsUsed : 1; }; // Data available to potential texture post-processing functions