From 16697c0503bf398aec153b498cd4fb8217d09d52 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sun, 4 Aug 2024 12:46:57 +0200 Subject: [PATCH] Added sharing of glyph atlas entries between shaped glyph entries that need the same texture --- es-core/src/resources/Font.cpp | 15 ++++++++++++++- es-core/src/resources/Font.h | 6 ++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/es-core/src/resources/Font.cpp b/es-core/src/resources/Font.cpp index f7f446ef1..88ae99c51 100644 --- a/es-core/src/resources/Font.cpp +++ b/es-core/src/resources/Font.cpp @@ -1109,7 +1109,20 @@ Font::Glyph* Font::getGlyphByIndex(const unsigned int id, hb_font_t* fontArg, in FontTexture* tex {nullptr}; glm::ivec2 cursor {0, 0}; const glm::ivec2 glyphSize {glyphSlot->bitmap.width, glyphSlot->bitmap.rows}; - getTextureForNewGlyph(glyphSize, tex, cursor); + + // Check if there is already a texture entry for the glyph, otherwise create it. + // This makes sure we don't create multiple identical glyph atlas entries and waste VRAM. + auto it2 = mGlyphTextureMap.find(std::make_pair(id, fontArg)); + if (it2 != mGlyphTextureMap.end()) { + tex = (*it2).second.texture; + cursor = (*it2).second.cursor; + } + else { + getTextureForNewGlyph(glyphSize, tex, cursor); + GlyphTexture& glyphTexture {mGlyphTextureMap[std::make_pair(id, returnedFont)]}; + glyphTexture.texture = tex; + glyphTexture.cursor = cursor; + } // This should (hopefully) never occur as size constraints are enforced earlier on. if (tex == nullptr) { diff --git a/es-core/src/resources/Font.h b/es-core/src/resources/Font.h index 0df5b9732..608db3c12 100644 --- a/es-core/src/resources/Font.h +++ b/es-core/src/resources/Font.h @@ -182,6 +182,11 @@ private: int rows; }; + struct GlyphTexture { + FontTexture* texture; + glm::ivec2 cursor; + }; + struct FallbackFontCache { std::string path; std::shared_ptr face; @@ -240,6 +245,7 @@ private: std::vector> mTextures; std::map mGlyphMap; std::map, Glyph> mGlyphMapByIndex; + std::map, GlyphTexture> mGlyphTextureMap; const std::string mPath; hb_font_t* mFontHB;