Added sharing of glyph atlas entries between shaped glyph entries that need the same texture

This commit is contained in:
Leon Styhre 2024-08-04 12:46:57 +02:00
parent 91d3f3a43a
commit 16697c0503
2 changed files with 20 additions and 1 deletions

View file

@ -1109,7 +1109,20 @@ Font::Glyph* Font::getGlyphByIndex(const unsigned int id, hb_font_t* fontArg, in
FontTexture* tex {nullptr}; FontTexture* tex {nullptr};
glm::ivec2 cursor {0, 0}; glm::ivec2 cursor {0, 0};
const glm::ivec2 glyphSize {glyphSlot->bitmap.width, glyphSlot->bitmap.rows}; const glm::ivec2 glyphSize {glyphSlot->bitmap.width, glyphSlot->bitmap.rows};
// 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); 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. // This should (hopefully) never occur as size constraints are enforced earlier on.
if (tex == nullptr) { if (tex == nullptr) {

View file

@ -182,6 +182,11 @@ private:
int rows; int rows;
}; };
struct GlyphTexture {
FontTexture* texture;
glm::ivec2 cursor;
};
struct FallbackFontCache { struct FallbackFontCache {
std::string path; std::string path;
std::shared_ptr<FontFace> face; std::shared_ptr<FontFace> face;
@ -240,6 +245,7 @@ private:
std::vector<std::unique_ptr<FontTexture>> mTextures; std::vector<std::unique_ptr<FontTexture>> mTextures;
std::map<unsigned int, Glyph> mGlyphMap; std::map<unsigned int, Glyph> mGlyphMap;
std::map<std::tuple<unsigned int, hb_font_t*, int>, Glyph> mGlyphMapByIndex; std::map<std::tuple<unsigned int, hb_font_t*, int>, Glyph> mGlyphMapByIndex;
std::map<std::pair<unsigned int, hb_font_t*>, GlyphTexture> mGlyphTextureMap;
const std::string mPath; const std::string mPath;
hb_font_t* mFontHB; hb_font_t* mFontHB;