Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build Font Async #7271

Closed
control0forver opened this issue Jan 29, 2024 · 4 comments
Closed

Build Font Async #7271

control0forver opened this issue Jan 29, 2024 · 4 comments

Comments

@control0forver
Copy link

Version/Branch of Dear ImGui:

Version v1.90.0

Back-ends:

imgui_impl_win32.cpp + imgui_impl_dx11.cpp

Compiler, OS:

WIndows 10 + MSVC

Full config/build information:

Dear ImGui 1.90.0 (19000)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: IMGUI_DISABLE_OBSOLETE_FUNCTIONS
define: IMGUI_DISABLE_OBSOLETE_KEYIO
define: _WIN32
define: _WIN64
define: _MSC_VER=1938
define: _MSVC_LANG=202004
--------------------------------
io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_dx11
io.ConfigFlags: 0x00000000
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x0000000E
 HasMouseCursors
 HasSetMousePos
 RendererHasVtxOffset
--------------------------------
io.Fonts: 2 fonts, Flags: 0x00000000, TexSize: 4096,8192
io.DisplaySize: 1366.00,768.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 0.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 5.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

My Issue/Question:
I tried to load font with GlyphRangesChineseFull, but it will take long time (about 6-10 seconds) at game frame presenting

I loaded font using this call:

ImGui::GetIO().Fonts->AddFontxxx

but imgui did't build font yet, until I call this:

ImGui_ImplDX11_NewFrame

I traced it to ImFontBuilderIO::FontBuilder_Build function:

ImGui_ImplDX11_NewFrame->
    ImGui_ImplDX11_CreateDeviceObjects->
        ImGui_ImplDX11_CreateFontsTexture->
            ImFontAtlas::GetTexDataAsRGBA32->
                ImFontAtlas::GetTexDataAsAlpha8->
                    ImFontAtlas::Build->
                        ImFontBuilderIO->
                            ImFontBuilderIO::FontBuilder_Build

I tried to using std::thread or std::async with lambda expressions to solve it like this but failed with some error

#include <thread>
static std::thread thBuildFont = std::thread([&]{ BuildFont(); });

Screenshots/Video:

image
image
image

Minimal, Complete and Verifiable Example code:

// Load font
        fonts["NotoSans_Bold_18"] = ImGui::GetIO().Fonts->AddFontFromMemoryCompressedTTF(
            notosanssc_bold_ttf_compressed_data, notosanssc_bold_ttf_compressed_size, 18.f, NULL, io.Fonts->GetGlyphRangesChineseFull());
@ocornut
Copy link
Owner

ocornut commented Jan 29, 2024

Running in a thread doesn't make sense if the other thread needs the font data. I don't think there is a good straightforward solution: our model with prebuilding glyphs is flawed and problematic but we should fix it this year.

Until now, you can:

@ocornut
Copy link
Owner

ocornut commented Feb 5, 2024

Closing as answered.

@ocornut ocornut closed this as completed Feb 5, 2024
@Nanokoli
Copy link

you can load ImFontAtlas async,ImFont is base on it,when you finish ImFontAtlas's load(Texture UpLoad To GPU device),you can set font anywhere

@ocornut
Copy link
Owner

ocornut commented Mar 5, 2025

Current WIP branch for dynamic_fonts (#8465) makes it obsolete. Even though the branch is not merged yet, it should be this soonish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants