From 02b5df03ab276e0e0f92d06f6b8a3410f0599e5f Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 10 Feb 2025 12:02:05 +0100 Subject: [PATCH] impr: Drastically reduce font-related memory usage --- main/gui/source/window/window.cpp | 10 +++++++-- plugins/fonts/include/font_atlas.hpp | 30 ++++++++++++++++++++++++++ plugins/fonts/source/font_loader.cpp | 1 + plugins/fonts/source/library_fonts.cpp | 4 ++-- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/main/gui/source/window/window.cpp b/main/gui/source/window/window.cpp index 3003302c5..1d712b17a 100644 --- a/main/gui/source/window/window.cpp +++ b/main/gui/source/window/window.cpp @@ -334,6 +334,9 @@ namespace hex { } void Window::frameBegin() { + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + // Create font textures if necessary { const auto &fontDefinitions = ImHexApi::Fonts::impl::getFontDefinitions(); @@ -345,6 +348,8 @@ namespace hex { currentFont = font->ContainerAtlas; ImGui_ImplOpenGL3_CreateFontsTexture(); + currentFont->ClearInputData(); + currentFont->ClearTexData(); } { @@ -359,6 +364,8 @@ namespace hex { cfg.SizePixels = ImHexApi::Fonts::DefaultFontSize; io.Fonts->AddFontDefault(&cfg); ImGui_ImplOpenGL3_CreateFontsTexture(); + io.Fonts->ClearInputData(); + io.Fonts->ClearTexData(); } else { currentFont = font->ContainerAtlas; } @@ -366,8 +373,7 @@ namespace hex { } // Start new ImGui Frame - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); TutorialManager::drawTutorial(); diff --git a/plugins/fonts/include/font_atlas.hpp b/plugins/fonts/include/font_atlas.hpp index a0811cf85..72512aaac 100644 --- a/plugins/fonts/include/font_atlas.hpp +++ b/plugins/fonts/include/font_atlas.hpp @@ -53,6 +53,36 @@ namespace hex::fonts { FontAtlas(const FontAtlas &) = delete; FontAtlas &operator=(const FontAtlas &) = delete; + FontAtlas(FontAtlas &&other) noexcept { + m_fontAtlas = other.m_fontAtlas; + other.m_fontAtlas = nullptr; + + m_defaultConfig = other.m_defaultConfig; + m_fontSizes = std::move(other.m_fontSizes); + m_fontConfigs = std::move(other.m_fontConfigs); + m_glyphRange = std::move(other.m_glyphRange); + m_fontData = std::move(other.m_fontData); + } + + FontAtlas& operator=(FontAtlas &&other) noexcept { + if (this != &other) { + if (m_fontAtlas != nullptr) { + IM_DELETE(m_fontAtlas); + } + + m_fontAtlas = other.m_fontAtlas; + other.m_fontAtlas = nullptr; + + m_defaultConfig = other.m_defaultConfig; + m_fontSizes = std::move(other.m_fontSizes); + m_fontConfigs = std::move(other.m_fontConfigs); + m_glyphRange = std::move(other.m_glyphRange); + m_fontData = std::move(other.m_fontData); + } + + return *this; + } + ~FontAtlas() { if (m_fontAtlas != nullptr) { m_fontAtlas->Locked = false; diff --git a/plugins/fonts/source/font_loader.cpp b/plugins/fonts/source/font_loader.cpp index 3bd777cc6..eb488c31d 100644 --- a/plugins/fonts/source/font_loader.cpp +++ b/plugins/fonts/source/font_loader.cpp @@ -97,6 +97,7 @@ namespace hex::fonts { // Build the font atlas if (fontAtlas->build()) { + fontAtlas->reset(); return true; } diff --git a/plugins/fonts/source/library_fonts.cpp b/plugins/fonts/source/library_fonts.cpp index 07dde9139..ab693c73b 100644 --- a/plugins/fonts/source/library_fonts.cpp +++ b/plugins/fonts/source/library_fonts.cpp @@ -16,7 +16,7 @@ namespace hex::fonts { bool buildFontAtlas(FontAtlas *fontAtlas, std::fs::path fontPath, bool pixelPerfectFont, float fontSize, bool loadUnicodeCharacters, bool bold, bool italic, bool antialias); - static AutoReset>> s_fontAtlases; + static AutoReset>> s_fontAtlases; void loadFont(const ContentRegistry::Settings::Widgets::Widget &widget, const UnlocalizedString &name, ImFont **font, float scale) { const auto &settings = static_cast(widget); @@ -51,7 +51,7 @@ namespace hex::fonts { *font = atlas->getAtlas()->Fonts[0]; - (*s_fontAtlases)[*font] = std::move(atlas); + (*s_fontAtlases)[name] = std::move(atlas); } bool setupFonts() {