From d2541c745a019f88ccf457ac6245bbffa94b53a8 Mon Sep 17 00:00:00 2001 From: LHoG <1476261+lhog@users.noreply.github.com> Date: Sun, 27 Nov 2022 20:10:56 +0400 Subject: [PATCH] Maybe fixed AMD fonts once again --- rts/Rendering/Fonts/CFontTexture.cpp | 8 ++++++ rts/Rendering/Fonts/CFontTexture.h | 4 ++- rts/Rendering/Fonts/glFont.cpp | 15 +---------- rts/Rendering/Fonts/glFont.h | 5 ---- rts/Rendering/Fonts/glFontRenderer.cpp | 36 +++++++++++++++++++------- rts/Rendering/Fonts/glFontRenderer.h | 11 +++++--- 6 files changed, 46 insertions(+), 33 deletions(-) diff --git a/rts/Rendering/Fonts/CFontTexture.cpp b/rts/Rendering/Fonts/CFontTexture.cpp index 0d2765eee2..18407d97f1 100644 --- a/rts/Rendering/Fonts/CFontTexture.cpp +++ b/rts/Rendering/Fonts/CFontTexture.cpp @@ -1,6 +1,7 @@ /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ #include "CFontTexture.h" +#include "glFontRenderer.h" #include "FontLogSection.h" #include // for memset, memcpy @@ -1196,6 +1197,7 @@ CFontTexture::CFontTexture(const std::string& fontfile, int size, int _outlinesi fontFamily = "unknown"; fontStyle = "unknown"; + fontRenderer = CglFontRenderer::CreateInstance(); #ifndef HEADLESS try { @@ -1258,6 +1260,7 @@ CFontTexture::CFontTexture(const std::string& fontfile, int size, int _outlinesi CFontTexture::~CFontTexture() { + CglFontRenderer::DeleteInstance(fontRenderer); #ifndef HEADLESS glDeleteTextures(1, &glyphAtlasTextureID); glyphAtlasTextureID = 0; @@ -1700,6 +1703,11 @@ void CFontTexture::UpdateGlyphAtlasTexture() } void CFontTexture::UploadGlyphAtlasTexture() +{ + fontRenderer->HandleTextureUpdate(*this, true); +} + +void CFontTexture::UploadGlyphAtlasTextureImpl() { #ifndef HEADLESS if (!GlyphAtlasTextureNeedsUpload()) diff --git a/rts/Rendering/Fonts/CFontTexture.h b/rts/Rendering/Fonts/CFontTexture.h index efe4d98a64..3537ee89e1 100644 --- a/rts/Rendering/Fonts/CFontTexture.h +++ b/rts/Rendering/Fonts/CFontTexture.h @@ -94,7 +94,7 @@ struct GlyphInfo { std::shared_ptr face; }; - +class CglFontRenderer; /** This class just store glyphs and load new glyphs if requred @@ -139,6 +139,7 @@ class CFontTexture bool GlyphAtlasTextureNeedsUpload() const; void UpdateGlyphAtlasTexture(); void UploadGlyphAtlasTexture(); + void UploadGlyphAtlasTextureImpl(); private: void CreateTexture(const int width, const int height); void LoadGlyph(std::shared_ptr& f, char32_t ch, unsigned index); @@ -167,6 +168,7 @@ class CFontTexture unsigned int glyphAtlasTextureID = 0; + std::unique_ptr fontRenderer; private: int curTextureUpdate = 0; #ifndef HEADLESS diff --git a/rts/Rendering/Fonts/glFont.cpp b/rts/Rendering/Fonts/glFont.cpp index 2cb7d7f7ff..9faf1f13ef 100644 --- a/rts/Rendering/Fonts/glFont.cpp +++ b/rts/Rendering/Fonts/glFont.cpp @@ -183,14 +183,6 @@ CglFont::CglFont(const std::string& fontFile, int size, int _outlineWidth, float std::make_unique>(), std::make_unique>() }; - - fontRenderer = CglFontRenderer::CreateInstance(); -} - -CglFont::~CglFont() { -#ifndef HEADLESS - CglFontRenderer::DeleteInstance(fontRenderer); -#endif } #ifdef HEADLESS @@ -613,12 +605,7 @@ void CglFont::End() { inBeginEndBlock = false; //without this, fonts textures are empty in display lists (probably GL commands in UploadGlyphAtlasTexture are get recorded as part of the list) - GLint dl = 0; - glGetIntegerv(GL_LIST_INDEX, &dl); - if (dl == 0) { - UpdateGlyphAtlasTexture(); - UploadGlyphAtlasTexture(); - } + fontRenderer->HandleTextureUpdate(*this, false); fontRenderer->PushGLState(*this); fontRenderer->DrawTraingleElements(); fontRenderer->PopGLState(); diff --git a/rts/Rendering/Fonts/glFont.h b/rts/Rendering/Fonts/glFont.h index f701df850e..bf73cadfad 100644 --- a/rts/Rendering/Fonts/glFont.h +++ b/rts/Rendering/Fonts/glFont.h @@ -40,8 +40,6 @@ namespace Shader { struct IProgramObject; }; -class CglFontRenderer; - class CglFont : public CTextWrap { public: @@ -58,7 +56,6 @@ class CglFont : public CTextWrap static void ReallocSystemFontAtlases(bool pre); CglFont(const std::string& fontFile, int size, int outlinewidth, float outlineweight); - ~CglFont() override; void Begin(); void End(); @@ -140,8 +137,6 @@ class CglFont : public CTextWrap return allFonts; } private: - std::unique_ptr fontRenderer; - std::string fontPath; std::array, 2> fontMutexes; diff --git a/rts/Rendering/Fonts/glFontRenderer.cpp b/rts/Rendering/Fonts/glFontRenderer.cpp index 92fe47de7a..de0da5c79c 100644 --- a/rts/Rendering/Fonts/glFontRenderer.cpp +++ b/rts/Rendering/Fonts/glFontRenderer.cpp @@ -144,13 +144,15 @@ void CglShaderFontRenderer::DrawTraingleElements() primaryBufferTC.DrawElements(GL_TRIANGLES); } -void CglShaderFontRenderer::HandleTextureUpdate(CglFont& fnt) +void CglShaderFontRenderer::HandleTextureUpdate(CFontTexture& fnt, bool onlyUpload) { - fnt.UpdateGlyphAtlasTexture(); + if (!onlyUpload) + fnt.UpdateGlyphAtlasTexture(); + GLint dl = 0; glGetIntegerv(GL_LIST_INDEX, &dl); - if (dl == 0 || true) { - fnt.UploadGlyphAtlasTexture(); + if (dl == 0) { + fnt.UploadGlyphAtlasTextureImpl(); } } @@ -199,10 +201,15 @@ CglNoShaderFontRenderer::CglNoShaderFontRenderer() v.reserve(NUM_TRI_BUFFER_VERTS); for (auto& i : indcs) i.reserve(NUM_TRI_BUFFER_ELEMS); + + textureSpaceMatrix = glGenLists(1); + glNewList(textureSpaceMatrix, GL_COMPILE); + glEndList(); } CglNoShaderFontRenderer::~CglNoShaderFontRenderer() { + glDeleteLists(textureSpaceMatrix, 1); } void CglNoShaderFontRenderer::AddQuadTrianglesImpl(bool primary, VA_TYPE_TC&& tl, VA_TYPE_TC&& tr, VA_TYPE_TC&& br, VA_TYPE_TC&& bl) @@ -258,10 +265,21 @@ void CglNoShaderFontRenderer::DrawTraingleElements() i.clear(); } -void CglNoShaderFontRenderer::HandleTextureUpdate(CglFont& fnt) +void CglNoShaderFontRenderer::HandleTextureUpdate(CFontTexture& fnt, bool onlyUpload) { - fnt.UpdateGlyphAtlasTexture(); - fnt.UploadGlyphAtlasTexture(); + if (!onlyUpload) + fnt.UpdateGlyphAtlasTexture(); + + GLint dl = 0; + glGetIntegerv(GL_LIST_INDEX, &dl); + if (dl == 0) { + fnt.UploadGlyphAtlasTextureImpl(); + + // update texture space dlist (this affects already compiled dlists too!) + glNewList(textureSpaceMatrix, GL_COMPILE); + glScalef(1.0f / fnt.GetTextureWidth(), 1.0f / fnt.GetTextureHeight(), 1.0f); + glEndList(); + } } void CglNoShaderFontRenderer::PushGLState(const CglFont& fnt) @@ -276,8 +294,8 @@ void CglNoShaderFontRenderer::PushGLState(const CglFont& fnt) glMatrixMode(GL_TEXTURE); glPushMatrix(); - glLoadIdentity(); - glScalef(1.0f / fnt.GetTextureWidth(), 1.0f / fnt.GetTextureHeight(), 1.0f); + glPushMatrix(); + glCallList(textureSpaceMatrix); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); diff --git a/rts/Rendering/Fonts/glFontRenderer.h b/rts/Rendering/Fonts/glFontRenderer.h index 1b12026591..e08341527c 100644 --- a/rts/Rendering/Fonts/glFontRenderer.h +++ b/rts/Rendering/Fonts/glFontRenderer.h @@ -6,6 +6,7 @@ #include "Rendering/GL/RenderBuffers.h" class CglFont; +class CFontTexture; class CglFontRenderer { public: virtual ~CglFontRenderer() = default; @@ -13,7 +14,7 @@ class CglFontRenderer { virtual void AddQuadTrianglesPB(VA_TYPE_TC&& tl, VA_TYPE_TC&& tr, VA_TYPE_TC&& br, VA_TYPE_TC&& bl) = 0; virtual void AddQuadTrianglesOB(VA_TYPE_TC&& tl, VA_TYPE_TC&& tr, VA_TYPE_TC&& br, VA_TYPE_TC&& bl) = 0; virtual void DrawTraingleElements() = 0; - virtual void HandleTextureUpdate(CglFont& font) = 0; + virtual void HandleTextureUpdate(CFontTexture& font, bool onlyUpload) = 0; virtual void PushGLState(const CglFont& font) = 0; virtual void PopGLState() = 0; @@ -43,7 +44,7 @@ class CglShaderFontRenderer final: public CglFontRenderer { void AddQuadTrianglesPB(VA_TYPE_TC&& tl, VA_TYPE_TC&& tr, VA_TYPE_TC&& br, VA_TYPE_TC&& bl) override; void AddQuadTrianglesOB(VA_TYPE_TC&& tl, VA_TYPE_TC&& tr, VA_TYPE_TC&& br, VA_TYPE_TC&& bl) override; void DrawTraingleElements() override; - void HandleTextureUpdate(CglFont& font) override; + void HandleTextureUpdate(CFontTexture& font, bool onlyUpload) override; void PushGLState(const CglFont& font) override; void PopGLState() override; @@ -66,7 +67,7 @@ class CglNoShaderFontRenderer final: public CglFontRenderer { void AddQuadTrianglesPB(VA_TYPE_TC&& tl, VA_TYPE_TC&& tr, VA_TYPE_TC&& br, VA_TYPE_TC&& bl) override; void AddQuadTrianglesOB(VA_TYPE_TC&& tl, VA_TYPE_TC&& tr, VA_TYPE_TC&& br, VA_TYPE_TC&& bl) override; void DrawTraingleElements() override; - void HandleTextureUpdate(CglFont& font) override; + void HandleTextureUpdate(CFontTexture& font, bool onlyUpload) override; void PushGLState(const CglFont& font) override; void PopGLState() override; @@ -78,6 +79,8 @@ class CglNoShaderFontRenderer final: public CglFontRenderer { std::array, 2> verts; // OL, PM std::array, 2> indcs; // OL, PM + + uint32_t textureSpaceMatrix = 0u; }; class CglNullFontRenderer final : public CglFontRenderer { @@ -85,7 +88,7 @@ class CglNullFontRenderer final : public CglFontRenderer { void AddQuadTrianglesPB(VA_TYPE_TC&& tl, VA_TYPE_TC&& tr, VA_TYPE_TC&& br, VA_TYPE_TC&& bl) override {} void AddQuadTrianglesOB(VA_TYPE_TC&& tl, VA_TYPE_TC&& tr, VA_TYPE_TC&& br, VA_TYPE_TC&& bl) override {} void DrawTraingleElements() override {} - void HandleTextureUpdate(CglFont& font) override {} + void HandleTextureUpdate(CFontTexture& font, bool onlyUpload) override {} void PushGLState(const CglFont& font) override {} void PopGLState() override {} bool IsLegacy() const override { return true; }