From 811672e6fc3084e9addd565d921fbfa357bcfbf7 Mon Sep 17 00:00:00 2001 From: ocornut Date: Wed, 5 Feb 2025 18:25:52 +0100 Subject: [PATCH] WIP - Exposed CompactCache(). Hide ClearCache(). --- imgui.cpp | 8 ++++---- imgui.h | 2 +- imgui_draw.cpp | 31 ++++++++++++++++++++----------- imgui_internal.h | 3 ++- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index d6109c369..011806532 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -21293,14 +21293,14 @@ void ImGui::ShowFontAtlas(ImFontAtlas* atlas) } SeparatorText("Font Atlas"); - if (Button("Clear Cache")) - atlas->ClearCache(); + if (Button("Compact")) + atlas->CompactCache(); SameLine(); if (Button("Grow")) ImFontAtlasBuildGrowTexture(atlas); SameLine(); - if (Button("Compact")) - ImFontAtlasBuildCompactTexture(atlas); + if (Button("Clear Output")) + ImFontAtlasBuildClearTexture(atlas); for (int tex_n = 0; tex_n < atlas->TexList.Size; tex_n++) { diff --git a/imgui.h b/imgui.h index 0e994dcb6..1cb3bb953 100644 --- a/imgui.h +++ b/imgui.h @@ -3618,7 +3618,7 @@ struct ImFontAtlas IMGUI_API void RemoveFont(ImFont* font); IMGUI_API void Clear(); // Clear everything (input fonts, output glyphs/textures) - IMGUI_API void ClearCache(); // Clear cached glyphs and textures. Invalidates all AddCustomRectXXX return values. + IMGUI_API void CompactCache(); // Compact cached glyphs and texture. // As we are transitioning toward a new font system, we expect to obsolete those soon: IMGUI_API void ClearInputData(); // [OBSOLETE] Clear input data (all ImFontConfig structures including sizes, TTF data, glyph ranges, etc.) = all the data used to build the texture and fonts. diff --git a/imgui_draw.cpp b/imgui_draw.cpp index 57325daf7..565889ce4 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -2459,7 +2459,7 @@ void ImTextureData::DestroyPixels() // - Default texture data encoded in ASCII // - ImFontAtlas() // - ImFontAtlas::Clear() -// - ImFontAtlas::ClearCache() +// - ImFontAtlas::CompactCache() // - ImFontAtlas::ClearInputData() // - ImFontAtlas::ClearTexData() // - ImFontAtlas::ClearFonts() @@ -2501,7 +2501,7 @@ void ImTextureData::DestroyPixels() // - ImFontAtlasBuildAddFont() // - ImFontAtlasBuildSetupFontCreateEllipsisFromDot() // - ImFontAtlasBuildSetupFontSpecialGlyphs() -// - ImFontAtlasBuildDiscardUnusedBakes() +// - ImFontAtlasBuildDiscardBakes() // - ImFontAtlasBuildDiscardFontBakedGlyph() // - ImFontAtlasBuildDiscardFontBaked() // - ImFontAtlasBuildDiscardFont() @@ -2617,15 +2617,14 @@ void ImFontAtlas::Clear() bool backup_renderer_has_textures = RendererHasTextures; RendererHasTextures = false; // Full Clear() is supported, but ClearTexData() only isn't. ClearTexData(); + if (Builder != NULL) + ImFontAtlasBuildClearTexture(this); RendererHasTextures = backup_renderer_has_textures; } -void ImFontAtlas::ClearCache() +void ImFontAtlas::CompactCache() { - ImVec2i new_tex_size = ImFontAtlasBuildGetTextureSizeEstimate(this); - ImFontAtlasBuildDestroy(this); - ImFontAtlasBuildAddTexture(this, new_tex_size.x, new_tex_size.y); - ImFontAtlasBuildInit(this); + ImFontAtlasBuildCompactTexture(this); } void ImFontAtlas::ClearInputData() @@ -3685,13 +3684,14 @@ void ImFontAtlasBuildDiscardFont(ImFontAtlas* atlas, ImFont* font) } } -void ImFontAtlasBuildDiscardUnusedBakes(ImFontAtlas* atlas, int gc_frames) +// use unused_frames==0 to discard everything. +void ImFontAtlasBuildDiscardBakes(ImFontAtlas* atlas, int unused_frames) { ImFontAtlasBuilder* builder = atlas->Builder; for (int baked_n = 0; baked_n < builder->BakedPool.Size; baked_n++) { ImFontBaked* baked = &builder->BakedPool[baked_n]; - if (baked->LastUsedFrame + gc_frames < atlas->Builder->FrameCount && !baked->WantDestroy) + if (baked->LastUsedFrame + unused_frames <= atlas->Builder->FrameCount && !baked->WantDestroy) ImFontAtlasBuildDiscardFontBaked(atlas, baked->ContainerFont, baked); } } @@ -3912,7 +3912,7 @@ void ImFontAtlasBuildMakeSpace(ImFontAtlas* atlas) // Can some baked contents be ditched? //IMGUI_DEBUG_LOG_FONT("[font] ImFontAtlasBuildMakeSpace()\n"); ImFontAtlasBuilder* builder = atlas->Builder; - ImFontAtlasBuildDiscardUnusedBakes(atlas, 2); + ImFontAtlasBuildDiscardBakes(atlas, 2); // Currently using a heuristic for repack without growing. if (builder->RectsDiscardedSurface < builder->RectsPackedSurface * 0.20f) @@ -3955,12 +3955,21 @@ ImVec2i ImFontAtlasBuildGetTextureSizeEstimate(ImFontAtlas* atlas) return ImVec2i(new_tex_w, new_tex_h); } +// Clear all output. Invalidates all AddCustomRectXXX return values. +void ImFontAtlasBuildClearTexture(ImFontAtlas* atlas) +{ + ImVec2i new_tex_size = ImFontAtlasBuildGetTextureSizeEstimate(atlas); + ImFontAtlasBuildDestroy(atlas); + ImFontAtlasBuildAddTexture(atlas, new_tex_size.x, new_tex_size.y); + ImFontAtlasBuildInit(atlas); +} + // You should not need to call this manually! // If you think you do, let us know and we can advise about policies auto-compact. void ImFontAtlasBuildCompactTexture(ImFontAtlas* atlas) { ImFontAtlasBuilder* builder = atlas->Builder; - ImFontAtlasBuildDiscardUnusedBakes(atlas, 1); + ImFontAtlasBuildDiscardBakes(atlas, 0); ImTextureData* old_tex = atlas->TexData; ImVec2i old_tex_size = ImVec2i(old_tex->Width, old_tex->Height); diff --git a/imgui_internal.h b/imgui_internal.h index 504ae1430..b55352378 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -4047,13 +4047,14 @@ IMGUI_API void ImFontAtlasBuildRenderBitmapFromString(ImFontAtlas* IMGUI_API ImTextureData* ImFontAtlasBuildAddTexture(ImFontAtlas* atlas, int w, int h); IMGUI_API void ImFontAtlasBuildMakeSpace(ImFontAtlas* atlas); IMGUI_API void ImFontAtlasBuildRepackTexture(ImFontAtlas* atlas, int w, int h); +IMGUI_API void ImFontAtlasBuildClearTexture(ImFontAtlas* atlas); IMGUI_API void ImFontAtlasBuildGrowTexture(ImFontAtlas* atlas, int old_w = -1, int old_h = -1); IMGUI_API void ImFontAtlasBuildCompactTexture(ImFontAtlas* atlas); IMGUI_API ImVec2i ImFontAtlasBuildGetTextureSizeEstimate(ImFontAtlas* atlas); IMGUI_API bool ImFontAtlasBuildAddFont(ImFontAtlas* atlas, ImFontConfig* src); IMGUI_API void ImFontAtlasBuildSetupFontSpecialGlyphs(ImFontAtlas* atlas, ImFont* font, ImFontConfig* src); -IMGUI_API void ImFontAtlasBuildDiscardUnusedBakes(ImFontAtlas* atlas, int gc_frames); +IMGUI_API void ImFontAtlasBuildDiscardBakes(ImFontAtlas* atlas, int unused_frames); IMGUI_API void ImFontAtlasBuildDiscardFont(ImFontAtlas* atlas, ImFont* font); IMGUI_API void ImFontAtlasBuildDiscardFontBaked(ImFontAtlas* atlas, ImFont* font, ImFontBaked* baked); IMGUI_API void ImFontAtlasBuildDiscardFontBakedGlyph(ImFontAtlas* atlas, ImFont* font, ImFontBaked* baked, ImFontGlyph* glyph);