From b29e298d55703cc9afa09d1683b7d2c2126b3955 Mon Sep 17 00:00:00 2001 From: tobid7 Date: Thu, 3 Sep 2026 09:36:41 +0200 Subject: [PATCH] save 75% of font memory --- backends/source/gfx_opengl3.cpp | 16 +++++++++++++--- source/lithium/font.cpp | 12 ++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/backends/source/gfx_opengl3.cpp b/backends/source/gfx_opengl3.cpp index 49a8b9b..02a5a06 100644 --- a/backends/source/gfx_opengl3.cpp +++ b/backends/source/gfx_opengl3.cpp @@ -80,7 +80,7 @@ void GfxOpenGL3::BindTexture(TextureID id) { glUniform1i(pLocTex, 0); GLint fmt = 0; glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &fmt); - glUniform1i(pLocAlfa, fmt == GL_ALPHA); + glUniform1i(pLocAlfa, fmt == GL_R8); } void GfxOpenGL3::SysReset() { @@ -104,13 +104,23 @@ Li::Texture GfxOpenGL3::LoadTexture(const std::vector& pixels, int w, // Set base format (Always using RGBA as base) GLenum fmt = GL_RGBA; + GLenum ifmt = GL_RGBA8; if (type == TextureFormat::RGB24) { + ifmt = GL_RGB8; fmt = GL_RGB; } else if (type == TextureFormat::A8) { - fmt = GL_ALPHA; + ifmt = GL_R8; + fmt = GL_RED; } - glTexImage2D(GL_TEXTURE_2D, 0, fmt, w, h, 0, fmt, GL_UNSIGNED_BYTE, + glTexImage2D(GL_TEXTURE_2D, 0, ifmt, w, h, 0, fmt, GL_UNSIGNED_BYTE, pixels.data()); + + if (type == TextureFormat::A8) { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_ONE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_ONE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_ONE); + } if (filter == TextureFilter::Linear) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); diff --git a/source/lithium/font.cpp b/source/lithium/font.cpp index 13e76f9..510ccc1 100644 --- a/source/lithium/font.cpp +++ b/source/lithium/font.cpp @@ -51,7 +51,7 @@ PD_API void Font::LoadTTF(const std::vector& data, int px_height) { // Cache to not render same codepoint tex twice std::map buf_cache; - std::vector font_tex(texszs * texszs * 4, 0); + std::vector font_tex(texszs * texszs, 0); fvec2 off; bool empty = true; @@ -116,12 +116,8 @@ PD_API void Font::LoadTTF(const std::vector& data, int px_height) { for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { int map_pos = ((static_cast(off.y) + y) * texszs + - (static_cast(off.x) + x)) * - 4; - font_tex[map_pos + 0] = 255; - font_tex[map_pos + 1] = 255; - font_tex[map_pos + 2] = 255; - font_tex[map_pos + 3] = bitmap[x + y * w]; + (static_cast(off.x) + x)); + font_tex[map_pos] = bitmap[x + y * w]; } } @@ -286,7 +282,7 @@ PD_API void Font::CleanupTMS() {} PD_API void Font::BakeAndPush(bool final, std::vector& font_tex, int texszs) { - auto t = PD::Gfx::LoadTexture(font_tex, texszs, texszs); + auto t = PD::Gfx::LoadTexture(font_tex, texszs, texszs, TextureFormat::A8); PDLOG("Font: Texture backed as 0x{:X} at {}", t.GetID(), pCurrentTex); Textures.push_back(t.GetID()); pCurrentTex = Textures.size();