Skip to content

Commit

Permalink
ImGuiManager: Separate debug and OSD fonts
Browse files Browse the repository at this point in the history
Don't need to rasterize the former if it's not used, and saves blowing
up the debug window font size if the OSD scale is not 100%.
  • Loading branch information
stenzek committed Jan 2, 2025
1 parent fc5553a commit d3fe1df
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
70 changes: 56 additions & 14 deletions src/util/imgui_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static void SetStyle(ImGuiStyle& style, float scale);
static void SetKeyMap();
static bool LoadFontData(Error* error);
static void ReloadFontDataIfActive();
static bool AddImGuiFonts(bool fullscreen_fonts);
static bool AddImGuiFonts(bool debug_font, bool fullscreen_fonts);
static ImFont* AddTextFont(float size, bool full_glyph_range);
static ImFont* AddFixedFont(float size);
static bool AddIconFonts(float size);
Expand Down Expand Up @@ -100,7 +100,7 @@ static std::vector<WCharType> s_font_range;
static std::vector<WCharType> s_emoji_range;

static ImGuiContext* s_imgui_context;
static ImFont* s_standard_font;
static ImFont* s_debug_font;
static ImFont* s_osd_font;
static ImFont* s_fixed_font;
static ImFont* s_medium_font;
Expand Down Expand Up @@ -270,7 +270,7 @@ bool ImGuiManager::Initialize(float global_scale, float screen_margin, Error* er
SetKeyMap();
SetStyle(s_imgui_context->Style, s_global_scale);

if (!AddImGuiFonts(false) || !g_gpu_device->UpdateImGuiFontTexture())
if (!AddImGuiFonts(false, false) || !g_gpu_device->UpdateImGuiFontTexture())
{
Error::SetString(error, "Failed to create ImGui font text");
ImGui::DestroyContext();
Expand All @@ -296,7 +296,7 @@ void ImGuiManager::Shutdown()
s_imgui_context = nullptr;
}

s_standard_font = nullptr;
s_debug_font = nullptr;
s_fixed_font = nullptr;
s_medium_font = nullptr;
s_large_font = nullptr;
Expand Down Expand Up @@ -351,7 +351,7 @@ void ImGuiManager::UpdateScale()
s_global_scale = scale;
SetStyle(s_imgui_context->Style, s_global_scale);

if (!AddImGuiFonts(HasFullscreenFonts()))
if (!AddImGuiFonts(HasDebugFont(), HasFullscreenFonts()))
Panic("Failed to create ImGui font text");

if (!g_gpu_device->UpdateImGuiFontTexture())
Expand Down Expand Up @@ -695,17 +695,26 @@ bool ImGuiManager::AddIconFonts(float size)
return true;
}

bool ImGuiManager::AddImGuiFonts(bool fullscreen_fonts)
bool ImGuiManager::AddImGuiFonts(bool debug_font, bool fullscreen_fonts)
{
const float window_scale =
(g_gpu_device && g_gpu_device->HasMainSwapChain()) ? g_gpu_device->GetMainSwapChain()->GetScale() : 1.0f;
const float debug_font_size = std::ceil(15.0f * window_scale);
const float standard_font_size = std::ceil(15.0f * s_global_scale);
const float osd_font_size = std::ceil(17.0f * s_global_scale);

INFO_LOG("Allocating fonts winscale={} globalscale={} debug={} fullscreen={}", window_scale, s_global_scale,
debug_font, fullscreen_fonts);

ImGuiIO& io = ImGui::GetIO();
io.Fonts->Clear();

s_standard_font = AddTextFont(standard_font_size, false);
if (!s_standard_font)
return false;
if (debug_font)
{
s_debug_font = AddTextFont(debug_font_size, false);
if (!s_debug_font)
return false;
}

s_fixed_font = AddFixedFont(standard_font_size);
if (!s_fixed_font)
Expand All @@ -714,6 +723,8 @@ bool ImGuiManager::AddImGuiFonts(bool fullscreen_fonts)
s_osd_font = AddTextFont(osd_font_size, true);
if (!s_osd_font || !AddIconFonts(osd_font_size))
return false;
if (!debug_font)
s_debug_font = s_osd_font;

if (fullscreen_fonts)
{
Expand Down Expand Up @@ -748,7 +759,7 @@ void ImGuiManager::ReloadFontDataIfActive()
if (!LoadFontData(nullptr))
Panic("Failed to load font data");

if (!AddImGuiFonts(HasFullscreenFonts()))
if (!AddImGuiFonts(HasDebugFont(), HasFullscreenFonts()))
Panic("Failed to create ImGui font text");

if (!g_gpu_device->UpdateImGuiFontTexture())
Expand All @@ -765,10 +776,11 @@ bool ImGuiManager::AddFullscreenFontsIfMissing()
// can't do this in the middle of a frame
ImGui::EndFrame();

if (!AddImGuiFonts(true))
const bool debug_font = HasDebugFont();
if (!AddImGuiFonts(debug_font, true))
{
ERROR_LOG("Failed to lazily allocate fullscreen fonts.");
AddImGuiFonts(false);
AddImGuiFonts(debug_font, false);
}

g_gpu_device->UpdateImGuiFontTexture();
Expand All @@ -777,6 +789,32 @@ bool ImGuiManager::AddFullscreenFontsIfMissing()
return HasFullscreenFonts();
}

bool ImGuiManager::HasDebugFont()
{
return (s_debug_font != s_osd_font);
}

bool ImGuiManager::AddDebugFontIfMissing()
{
if (HasDebugFont())
return true;

// can't do this in the middle of a frame
ImGui::EndFrame();

const bool fullscreen_font = HasFullscreenFonts();
if (!AddImGuiFonts(true, fullscreen_font))
{
ERROR_LOG("Failed to lazily allocate fullscreen fonts.");
AddImGuiFonts(true, fullscreen_font);
}

g_gpu_device->UpdateImGuiFontTexture();
NewFrame();

return HasDebugFont();
}

bool ImGuiManager::HasFullscreenFonts()
{
return (s_medium_font && s_large_font);
Expand Down Expand Up @@ -1044,9 +1082,9 @@ float ImGuiManager::GetScreenMargin()
return s_screen_margin;
}

ImFont* ImGuiManager::GetStandardFont()
ImFont* ImGuiManager::GetDebugFont()
{
return s_standard_font;
return s_debug_font;
}

ImFont* ImGuiManager::GetOSDFont()
Expand Down Expand Up @@ -1396,6 +1434,8 @@ bool ImGuiManager::CreateAuxiliaryRenderWindow(AuxiliaryRenderWindowState* state
return false;
}

AddDebugFontIfMissing();

state->imgui_context = ImGui::CreateContext(s_imgui_context->IO.Fonts);
state->imgui_context->IO.DisplaySize =
ImVec2(static_cast<float>(state->swap_chain->GetWidth()), static_cast<float>(state->swap_chain->GetHeight()));
Expand Down Expand Up @@ -1465,6 +1505,7 @@ bool ImGuiManager::RenderAuxiliaryRenderWindow(AuxiliaryRenderWindowState* state
ImGui::SetCurrentContext(state->imgui_context);

ImGui::NewFrame();
ImGui::PushFont(s_debug_font);
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f), ImGuiCond_Always);
ImGui::SetNextWindowSize(state->imgui_context->IO.DisplaySize, ImGuiCond_Always);
if (ImGui::Begin("AuxRenderWindowMain", nullptr,
Expand All @@ -1475,6 +1516,7 @@ bool ImGuiManager::RenderAuxiliaryRenderWindow(AuxiliaryRenderWindowState* state
}

ImGui::End();
ImGui::PopFont();

const GPUDevice::PresentResult pres = g_gpu_device->BeginPresent(state->swap_chain.get());
if (pres == GPUDevice::PresentResult::OK)
Expand Down
8 changes: 7 additions & 1 deletion src/util/imgui_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,14 @@ bool HasFullscreenFonts();
/// Allocates/adds fullscreen fonts if they're not loaded.
bool AddFullscreenFontsIfMissing();

/// Returns true if there is a separate debug font.
bool HasDebugFont();

/// Changes whether a debug font is generated. Otherwise, the OSD font will be used for GetStandardFont().
bool AddDebugFontIfMissing();

/// Returns the standard font for external drawing.
ImFont* GetStandardFont();
ImFont* GetDebugFont();

/// Returns the standard font for on-screen display drawing.
ImFont* GetOSDFont();
Expand Down

0 comments on commit d3fe1df

Please sign in to comment.