Skip to content

Commit 54e5713

Browse files
committed
GPU Display: Raster font at correct pixel size taking into accound fractional scaling
1 parent 7286514 commit 54e5713

7 files changed

Lines changed: 50 additions & 8 deletions

File tree

‎GPU/GPUTracking/display/backend/GPUDisplayBackend.cxx‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,23 @@ int32_t GPUDisplayBackend::InitBackend()
6666
if (retVal) {
6767
return retVal;
6868
}
69-
if (mDisplay->cfg().noFreetype) {
70-
return retVal;
69+
if (!mDisplay->cfg().noFreetype) {
70+
loadFont();
71+
}
72+
return retVal;
73+
}
74+
75+
void GPUDisplayBackend::updateFontScale(float scale)
76+
{
77+
if (mFreetypeInitialized && scale != mFontRasterScale) {
78+
clearFontSymbols();
79+
mFreetypeInitialized = false;
80+
loadFont();
7181
}
82+
}
83+
84+
void GPUDisplayBackend::loadFont()
85+
{
7286
std::string fontName = mDisplay->cfg().font;
7387
#ifdef GPUCA_BUILD_EVENT_DISPLAY_FONTCONFIG
7488
FcInit();
@@ -97,24 +111,25 @@ int32_t GPUDisplayBackend::InitBackend()
97111
FT_Face face;
98112
if (FT_Init_FreeType(&ft)) {
99113
GPUError("Error initializing freetype");
100-
return 0;
114+
return;
101115
}
102116
if (FT_New_Face(ft, fontName.c_str(), 0, &face)) {
103117
GPUError("Error loading freetypoe font");
104-
return 0;
118+
return;
105119
}
106120

107121
int32_t fontSize = mDisplay->cfg().fontSize;
108122
mDisplay->drawTextFontSize() = fontSize;
109123
if (smoothFont()) {
110124
fontSize *= 4; // Font size scaled by 4, can be downsampled
111125
}
112-
FT_Set_Pixel_Sizes(face, 0, fontSize);
126+
mFontRasterScale = mDisplay->frontend()->getDisplayScale();
127+
FT_Set_Pixel_Sizes(face, 0, (uint32_t)(fontSize * mFontRasterScale + 0.5f)); // Rasterize at physical pixel size for HiDPI
113128

114129
for (uint32_t i = 0; i < 128; i++) {
115130
if (FT_Load_Char(face, i, FT_LOAD_RENDER)) {
116131
GPUError("Error loading freetype symbol");
117-
return 0;
132+
return;
118133
}
119134
const auto& glyph = face->glyph;
120135
addFontSymbol(i, glyph->bitmap.width, glyph->bitmap.rows, glyph->bitmap_left, glyph->bitmap_top, glyph->advance.x, glyph->bitmap.buffer);
@@ -124,7 +139,6 @@ int32_t GPUDisplayBackend::InitBackend()
124139
FT_Done_FreeType(ft);
125140
mFreetypeInitialized = true;
126141
#endif // GPUCA_BUILD_EVENT_DISPLAY_FREETYPE
127-
return retVal;
128142
}
129143

130144
void GPUDisplayBackend::ExitBackend()

‎GPU/GPUTracking/display/backend/GPUDisplayBackend.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class GPUDisplayBackend
8686
virtual int32_t InitBackendA() = 0;
8787
virtual void ExitBackendA() = 0;
8888
int32_t InitBackend();
89+
void updateFontScale(float scale); // Rasterize the font again if the display (HiDPI) scale changed
8990
void ExitBackend();
9091
virtual void loadDataToGPU(size_t totalVertizes) = 0;
9192
virtual void prepareDraw(const hmm_mat4& proj, const hmm_mat4& view, bool requestScreenshot = false, bool toMixBuffer = false, float includeMixImage = 0.f) = 0;
@@ -108,6 +109,8 @@ class GPUDisplayBackend
108109
protected:
109110
virtual void addFontSymbol(int32_t symbol, int32_t sizex, int32_t sizey, int32_t offsetx, int32_t offsety, int32_t advance, void* data) = 0;
110111
virtual void initializeTextDrawing() = 0;
112+
virtual void clearFontSymbols() {} // Release glyphs / font atlas before loading the font again
113+
void loadFont();
111114

112115
float getDownsampleFactor(bool screenshot = false);
113116
void fillIndirectCmdBuffer();
@@ -117,6 +120,7 @@ class GPUDisplayBackend
117120
std::vector<int32_t> mIndirectSectorOffset;
118121
vecpod<DrawArraysIndirectCommand> mCmdBuffer;
119122
bool mFreetypeInitialized = false;
123+
float mFontRasterScale = 1.f; // Display scale the font was rasterized for
120124
bool mFrontendCompatTextDraw = false;
121125
std::vector<char> mScreenshotPixels;
122126

‎GPU/GPUTracking/display/backend/GPUDisplayBackendOpenGL.cxx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,16 @@ void GPUDisplayBackendOpenGL::addFontSymbol(int32_t symbol, int32_t sizex, int32
709709
CHKERR(glPixelStorei(GL_UNPACK_ALIGNMENT, oldAlign));
710710
}
711711

712+
void GPUDisplayBackendOpenGL::clearFontSymbols()
713+
{
714+
CHKERR(glDeleteBuffers(1, &VBO_text));
715+
CHKERR(glDeleteVertexArrays(1, &VAO_text));
716+
for (auto& symbol : mFontSymbols) {
717+
CHKERR(glDeleteTextures(1, &symbol.texId));
718+
}
719+
mFontSymbols.clear();
720+
}
721+
712722
void GPUDisplayBackendOpenGL::initializeTextDrawing()
713723
{
714724
CHKERR(glGenVertexArrays(1, &VAO_text));
@@ -840,5 +850,6 @@ void GPUDisplayBackendOpenGL::pointSizeFactor(float factor) {}
840850
void GPUDisplayBackendOpenGL::lineWidthFactor(float factor) {}
841851
void GPUDisplayBackendOpenGL::addFontSymbol(int32_t symbol, int32_t sizex, int32_t sizey, int32_t offsetx, int32_t offsety, int32_t advance, void* data) {}
842852
void GPUDisplayBackendOpenGL::initializeTextDrawing() {}
853+
void GPUDisplayBackendOpenGL::clearFontSymbols() {}
843854
void GPUDisplayBackendOpenGL::OpenGLPrint(const char* s, float x, float y, float* color, float scale) {}
844855
#endif // GPUCA_BUILD_EVENT_DISPLAY_OPENGL

‎GPU/GPUTracking/display/backend/GPUDisplayBackendOpenGL.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class GPUDisplayBackendOpenGL : public GPUDisplayBackend
7171

7272
void addFontSymbol(int32_t symbol, int32_t sizex, int32_t sizey, int32_t offsetx, int32_t offsety, int32_t advance, void* data) override;
7373
void initializeTextDrawing() override;
74+
void clearFontSymbols() override;
7475
void OpenGLPrint(const char* s, float x, float y, float* color, float scale) override;
7576

7677
struct FontSymbolOpenGL : public FontSymbol {

‎GPU/GPUTracking/display/backend/GPUDisplayBackendVulkan.cxx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,6 +1854,16 @@ void GPUDisplayBackendVulkan::initializeTextDrawing()
18541854
updateFontTextureDescriptor();
18551855
}
18561856

1857+
void GPUDisplayBackendVulkan::clearFontSymbols()
1858+
{
1859+
mDevice.waitIdle();
1860+
if (mFontImage.sizex && mFontImage.sizey) {
1861+
clearImage(mFontImage);
1862+
mFontImage.sizex = mFontImage.sizey = 0;
1863+
}
1864+
mFontSymbols.clear();
1865+
}
1866+
18571867
void GPUDisplayBackendVulkan::updateFontTextureDescriptor()
18581868
{
18591869
vk::DescriptorImageInfo imageInfo{};

‎GPU/GPUTracking/display/backend/GPUDisplayBackendVulkan.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ class GPUDisplayBackendVulkan : public GPUDisplayBackend
126126

127127
void addFontSymbol(int32_t symbol, int32_t sizex, int32_t sizey, int32_t offsetx, int32_t offsety, int32_t advance, void* data) override;
128128
void initializeTextDrawing() override;
129+
void clearFontSymbols() override;
129130
void OpenGLPrint(const char* s, float x, float y, float* color, float scale) override;
130131

131132
uint32_t mGraphicsFamily;

‎GPU/GPUTracking/display/helpers/GPUDisplayHelpers.cxx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ void GPUDisplay::DoScreenshot(const char* filename, std::vector<char>& pixels, f
9999

100100
void GPUDisplay::showInfo(const char* info)
101101
{
102+
mBackend->updateFontScale(mFrontend->getDisplayScale());
102103
mBackend->prepareText();
103104
float colorValue = mCfgL.invertColors ? 0.f : 1.f;
104105
OpenGLPrint(info, 40.f, 20.f + std::max(20, mDrawTextFontSize + 4), colorValue, colorValue, colorValue, 1);
@@ -129,7 +130,7 @@ void GPUDisplay::OpenGLPrint(const char* s, float x, float y, float r, float g,
129130
y = mBackend->mScreenHeight - y;
130131
}
131132
float color[4] = {r, g, b, a};
132-
mBackend->OpenGLPrint(s, x, y, color, scale);
133+
mBackend->OpenGLPrint(s, x, y, color, scale / mBackend->mFontRasterScale);
133134
} else if (mFrontend->mCanDrawText) {
134135
mFrontend->OpenGLPrint(s, x, y, r, g, b, a, fromBotton);
135136
}

0 commit comments

Comments
 (0)