From cb1fe07540594dd0b5795bd3c908b05a148bb6af Mon Sep 17 00:00:00 2001 From: Vladislav Belov Date: Fri, 10 Apr 2026 23:39:15 +0200 Subject: [PATCH] Removes GetDeviceCommandContext call from CFont We need remove all occurences of GetDeviceCommandContext to be able to add multithreading support in the future. --- source/graphics/Font.cpp | 14 +++++++------- source/graphics/Font.h | 7 +++++-- source/graphics/FontManager.cpp | 9 +++++---- source/graphics/FontManager.h | 7 +++++-- source/renderer/Renderer.cpp | 2 +- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/source/graphics/Font.cpp b/source/graphics/Font.cpp index b5c3f98b61..a217a0a896 100644 --- a/source/graphics/Font.cpp +++ b/source/graphics/Font.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -159,7 +159,9 @@ void CFont::CalculateStringSize(const wchar_t* string, float& width, float& heig height += GetHeight(); } -bool CFont::SetFontParams(const std::string& fontName, float size, float strokeWidth, float scale) +bool CFont::SetFontParams( + Renderer::Backend::IDevice* device, const std::string& fontName, + float size, float strokeWidth, float scale) { ENSURE(m_FontSize == 0 && size > 0); @@ -181,7 +183,7 @@ bool CFont::SetFontParams(const std::string& fontName, float size, float strokeW FT_Stroker_Set(m_Stroker.get(), FloatToF26Dot6(m_StrokeWidth), FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0); } - if (!ConstructAtlasTexture()) + if (!ConstructAtlasTexture(device)) { LOGERROR("Failed to create font texture atlas %s", fontName); return false; @@ -263,10 +265,8 @@ Renderer::Backend::Sampler::Desc CFont::ChooseTextureFormatAndSampler() return defaultSamplerDesc; } -bool CFont::ConstructAtlasTexture() +bool CFont::ConstructAtlasTexture(Renderer::Backend::IDevice* device) { - Renderer::Backend::IDevice* backendDevice = g_Renderer.GetDeviceCommandContext()->GetDevice(); - // Make backend texture ahead of time. // TODO: calculate based on device support. const int textureSize{1024}; @@ -289,7 +289,7 @@ bool CFont::ConstructAtlasTexture() PS::StringBuilder fontTextureNameBuilder{{std::begin(buffer), std::end(buffer)}}; fontTextureNameBuilder.Append("Font Texture "); fontTextureNameBuilder.Append(m_FontName); - m_Texture = g_Renderer.GetTextureManager().WrapBackendTexture(backendDevice->CreateTexture2D( + m_Texture = g_Renderer.GetTextureManager().WrapBackendTexture(device->CreateTexture2D( fontTextureNameBuilder.Str().data(), Renderer::Backend::ITexture::Usage::TRANSFER_DST | Renderer::Backend::ITexture::Usage::SAMPLED, diff --git a/source/graphics/Font.h b/source/graphics/Font.h index 0ab702d8e1..c31227f613 100644 --- a/source/graphics/Font.h +++ b/source/graphics/Font.h @@ -39,6 +39,7 @@ #include class CVector2D; +namespace Renderer::Backend { class IDevice; } namespace Renderer::Backend { class IDeviceCommandContext; } namespace Renderer::Backend::Sampler { struct Desc; } @@ -137,7 +138,9 @@ private: friend class CFontManager; bool AddFontFromPath(const OsPath& fontPath); - bool SetFontParams(const std::string& fontName, float size, float strokeWidth, float scale); + bool SetFontParams( + Renderer::Backend::IDevice* device, const std::string& fontName, + float size, float strokeWidth, float scale); void BlendGlyphBitmapToTexture(const FT_Bitmap& bitmap, int targetX, int targetY, u8 r, u8 g, u8 b); void BlendGlyphBitmapToTextureRGBA(const FT_Bitmap& bitmap, int targetX, int targetY, u8 r, u8 g, u8 b); @@ -147,7 +150,7 @@ private: std::optional GenerateGlyphBitmap(FT_Glyph& glyph, u16 codepoint, FT_Render_Mode renderMode, CVector2D offset, const float baselineInAtlas); const GlyphData* ExtractAndGenerateGlyph(u16 codepoint); - bool ConstructAtlasTexture(); + bool ConstructAtlasTexture(Renderer::Backend::IDevice* device); Renderer::Backend::Sampler::Desc ChooseTextureFormatAndSampler(); CTexturePtr m_Texture; diff --git a/source/graphics/FontManager.cpp b/source/graphics/FontManager.cpp index d6c10f8c8a..5424924a67 100644 --- a/source/graphics/FontManager.cpp +++ b/source/graphics/FontManager.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -101,12 +101,13 @@ FontSpec ParseFontSpec(const std::string& spec) } } // namespace -CFontManager::CFontManager() +CFontManager::CFontManager(Renderer::Backend::IDevice* device) : m_GUIScaleHook{std::make_unique(g_ConfigDB.RegisterHookAndCall( "gui.scale", [this]() { m_GUIScale = g_ConfigDB.Get("gui.scale", 1.0f); - }))} + }))}, + m_Device(device) { FT_Library lib; FT_Error error{FT_Init_FreeType(&lib)}; @@ -211,7 +212,7 @@ CFont* CFontManager::LoadFont(CStrIntern fontName, CStrIntern locale) CFont font{this->m_FreeType.get(), *m_GammaCorrectionLUT}; - if (!font.SetFontParams(localeFontName.string(), fontSpec.size, fontSpec.stroke ? 1.0f : 0.0f, m_GUIScale)) + if (!font.SetFontParams(m_Device, localeFontName.string(), fontSpec.size, fontSpec.stroke ? 1.0f : 0.0f, m_GUIScale)) { LOGERROR("Failed to set font params for %s", localeFontName.string().c_str()); return nullptr; diff --git a/source/graphics/FontManager.h b/source/graphics/FontManager.h index 12f90a7ab6..11d865eaa2 100644 --- a/source/graphics/FontManager.h +++ b/source/graphics/FontManager.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -31,6 +31,7 @@ class CConfigDBHook; class CFont; struct FT_LibraryRec_; +namespace Renderer::Backend { class IDevice; } namespace Renderer::Backend { class IDeviceCommandContext; } /** @@ -39,7 +40,7 @@ namespace Renderer::Backend { class IDeviceCommandContext; } class CFontManager { public: - CFontManager(); + CFontManager(Renderer::Backend::IDevice* device); ~CFontManager(); NONCOPYABLE(CFontManager); @@ -63,6 +64,8 @@ private: float m_GUIScale{1.0f}; std::unique_ptr m_GUIScaleHook; + Renderer::Backend::IDevice* m_Device; + /* * Most monitors today use 2.2 as the standard gamma. * MacOS may use 2.2 or 1.8 in some cases. diff --git a/source/renderer/Renderer.cpp b/source/renderer/Renderer.cpp index 6c379f2390..1612dc58a1 100644 --- a/source/renderer/Renderer.cpp +++ b/source/renderer/Renderer.cpp @@ -322,7 +322,7 @@ public: deviceCommandContext(device->CreateCommandContext()), IsOpen(false), ShadersDirty(true), profileTable(g_Renderer.m_Stats, linearAllocator), shaderManager(device), textureManager(g_VFS, false, device), vertexBufferManager(device), - postprocManager(device), sceneRenderer(device) + postprocManager(device), sceneRenderer(device), fontManager(device) { } };