Removes GetDeviceCommandContext call from CFont

We need remove all occurences of GetDeviceCommandContext to be able to
add multithreading support in the future.
This commit is contained in:
Vladislav Belov
2026-04-10 23:39:15 +02:00
parent 68091177ac
commit cb1fe07540
5 changed files with 23 additions and 16 deletions
+7 -7
View File
@@ -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,
+5 -2
View File
@@ -39,6 +39,7 @@
#include <vector>
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<CVector2D> 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;
+5 -4
View File
@@ -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<CConfigDBHook>(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;
+5 -2
View File
@@ -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<CConfigDBHook> 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.
+1 -1
View File
@@ -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)
{
}
};