impr: Drastically reduce font-related memory usage

This commit is contained in:
WerWolv 2025-02-10 12:02:05 +01:00
parent e1580e51cf
commit 02b5df03ab
4 changed files with 41 additions and 4 deletions

View File

@ -334,6 +334,9 @@ namespace hex {
} }
void Window::frameBegin() { void Window::frameBegin() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
// Create font textures if necessary // Create font textures if necessary
{ {
const auto &fontDefinitions = ImHexApi::Fonts::impl::getFontDefinitions(); const auto &fontDefinitions = ImHexApi::Fonts::impl::getFontDefinitions();
@ -345,6 +348,8 @@ namespace hex {
currentFont = font->ContainerAtlas; currentFont = font->ContainerAtlas;
ImGui_ImplOpenGL3_CreateFontsTexture(); ImGui_ImplOpenGL3_CreateFontsTexture();
currentFont->ClearInputData();
currentFont->ClearTexData();
} }
{ {
@ -359,6 +364,8 @@ namespace hex {
cfg.SizePixels = ImHexApi::Fonts::DefaultFontSize; cfg.SizePixels = ImHexApi::Fonts::DefaultFontSize;
io.Fonts->AddFontDefault(&cfg); io.Fonts->AddFontDefault(&cfg);
ImGui_ImplOpenGL3_CreateFontsTexture(); ImGui_ImplOpenGL3_CreateFontsTexture();
io.Fonts->ClearInputData();
io.Fonts->ClearTexData();
} else { } else {
currentFont = font->ContainerAtlas; currentFont = font->ContainerAtlas;
} }
@ -366,8 +373,7 @@ namespace hex {
} }
// Start new ImGui Frame // Start new ImGui Frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame(); ImGui::NewFrame();
TutorialManager::drawTutorial(); TutorialManager::drawTutorial();

View File

@ -53,6 +53,36 @@ namespace hex::fonts {
FontAtlas(const FontAtlas &) = delete; FontAtlas(const FontAtlas &) = delete;
FontAtlas &operator=(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() { ~FontAtlas() {
if (m_fontAtlas != nullptr) { if (m_fontAtlas != nullptr) {
m_fontAtlas->Locked = false; m_fontAtlas->Locked = false;

View File

@ -97,6 +97,7 @@ namespace hex::fonts {
// Build the font atlas // Build the font atlas
if (fontAtlas->build()) { if (fontAtlas->build()) {
fontAtlas->reset();
return true; return true;
} }

View File

@ -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); bool buildFontAtlas(FontAtlas *fontAtlas, std::fs::path fontPath, bool pixelPerfectFont, float fontSize, bool loadUnicodeCharacters, bool bold, bool italic, bool antialias);
static AutoReset<std::map<ImFont*, std::unique_ptr<FontAtlas>>> s_fontAtlases; static AutoReset<std::map<UnlocalizedString, std::unique_ptr<FontAtlas>>> s_fontAtlases;
void loadFont(const ContentRegistry::Settings::Widgets::Widget &widget, const UnlocalizedString &name, ImFont **font, float scale) { void loadFont(const ContentRegistry::Settings::Widgets::Widget &widget, const UnlocalizedString &name, ImFont **font, float scale) {
const auto &settings = static_cast<const FontSelector&>(widget); const auto &settings = static_cast<const FontSelector&>(widget);
@ -51,7 +51,7 @@ namespace hex::fonts {
*font = atlas->getAtlas()->Fonts[0]; *font = atlas->getAtlas()->Fonts[0];
(*s_fontAtlases)[*font] = std::move(atlas); (*s_fontAtlases)[name] = std::move(atlas);
} }
bool setupFonts() { bool setupFonts() {