Skip to content

Commit

Permalink
Maybe fixed AMD fonts once again
Browse files Browse the repository at this point in the history
  • Loading branch information
lhog committed Nov 27, 2022
1 parent 834c792 commit d2541c7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 33 deletions.
8 changes: 8 additions & 0 deletions rts/Rendering/Fonts/CFontTexture.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstring> // for memset, memcpy
Expand Down Expand Up @@ -1196,6 +1197,7 @@ CFontTexture::CFontTexture(const std::string& fontfile, int size, int _outlinesi
fontFamily = "unknown";
fontStyle = "unknown";

fontRenderer = CglFontRenderer::CreateInstance();
#ifndef HEADLESS

try {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1700,6 +1703,11 @@ void CFontTexture::UpdateGlyphAtlasTexture()
}

void CFontTexture::UploadGlyphAtlasTexture()
{
fontRenderer->HandleTextureUpdate(*this, true);
}

void CFontTexture::UploadGlyphAtlasTextureImpl()
{
#ifndef HEADLESS
if (!GlyphAtlasTextureNeedsUpload())
Expand Down
4 changes: 3 additions & 1 deletion rts/Rendering/Fonts/CFontTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ struct GlyphInfo {
std::shared_ptr<FontFace> face;
};


class CglFontRenderer;

/**
This class just store glyphs and load new glyphs if requred
Expand Down Expand Up @@ -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<FontFace>& f, char32_t ch, unsigned index);
Expand Down Expand Up @@ -167,6 +168,7 @@ class CFontTexture

unsigned int glyphAtlasTextureID = 0;

std::unique_ptr<CglFontRenderer> fontRenderer;
private:
int curTextureUpdate = 0;
#ifndef HEADLESS
Expand Down
15 changes: 1 addition & 14 deletions rts/Rendering/Fonts/glFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,6 @@ CglFont::CglFont(const std::string& fontFile, int size, int _outlineWidth, float
std::make_unique<spring::mutex_wrapper<spring::noop_mutex >>(),
std::make_unique<spring::mutex_wrapper<spring::recursive_mutex>>()
};

fontRenderer = CglFontRenderer::CreateInstance();
}

CglFont::~CglFont() {
#ifndef HEADLESS
CglFontRenderer::DeleteInstance(fontRenderer);
#endif
}

#ifdef HEADLESS
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 0 additions & 5 deletions rts/Rendering/Fonts/glFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ namespace Shader {
struct IProgramObject;
};

class CglFontRenderer;

class CglFont : public CTextWrap
{
public:
Expand All @@ -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();
Expand Down Expand Up @@ -140,8 +137,6 @@ class CglFont : public CTextWrap
return allFonts;
}
private:
std::unique_ptr<CglFontRenderer> fontRenderer;

std::string fontPath;

std::array<std::unique_ptr<spring::mutex_wrapper_concept>, 2> fontMutexes;
Expand Down
36 changes: 27 additions & 9 deletions rts/Rendering/Fonts/glFontRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down
11 changes: 7 additions & 4 deletions rts/Rendering/Fonts/glFontRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
#include "Rendering/GL/RenderBuffers.h"

class CglFont;
class CFontTexture;
class CglFontRenderer {
public:
virtual ~CglFontRenderer() = default;

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;

Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -78,14 +79,16 @@ class CglNoShaderFontRenderer final: public CglFontRenderer {

std::array<std::vector<VA_TYPE_TC>, 2> verts; // OL, PM
std::array<std::vector<uint16_t >, 2> indcs; // OL, PM

uint32_t textureSpaceMatrix = 0u;
};

class CglNullFontRenderer final : public CglFontRenderer {
// Inherited via 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; }
Expand Down

0 comments on commit d2541c7

Please sign in to comment.