-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
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
Request for feedback: dynamic fonts & texture updates (v1.92) #8465
Comments
GENERAL OVERVIEW OF THE CHANGESAs outlined in the '10 years of Dear ImGui' article there are many changes coming related to the font system and text shaping. For the font system, I have decided to split the work in 2 distinct steps:
The code I am publishing today is mostly step 1. What is part of what I am publishing (step 1)
What is not part of this update yet (step 2, will come next)Your first impression may be very dry: The examples are using the default bitmap fonts and are still not DPI aware.
Generally speaking, the design puzzle here has been to push toward improvements while providing a reasonable path for old code to function without major issues. I have iterated on this for countless hours to find what I hope is the best solution, but it's expected that people will run into issues. |
KNOWN ISSUES
|
CHANGELOGBreaking Changes
ImFont* myfont = io.Fonts->AddFontFromFileTTF(....);
myfont->GetFontBaked(16.0f);
myfont->Flags |= ImFontFlags_LockBakedSizes;
Other Changes
|
This is really great! It lets me delete a lot of the code I had written to deal with creating and using fonts of different sizes, and it's much faster and more flexible on top of that. So far I've noticed only two issues (for which I can also create separate GitHub issues if you prefer):
I saw that this is because of a temporary backwards compatibility measure in PushFont:
with the following stack:
If I comment out the call to |
Thank you @bratpilz for your feedback. (1) I haven't figured out the right design for this yet. To be forward-facing, it seems evident that (2) I think we could solve this by adding a reference count inside ImFontAtlas and having backend check this. I'll work on this too. |
I have pushed 3eefa4e which should solve the problem with sharing an atlas between context. For simplicity I had to take a shortcut of assuming that when multiple contexts are created, they all have their backend initialized/bound to the context. |
Question r.e supporting in custom backends, would it be possible for the pixel data to be kept alive until the backend agrees the texture is destroyed? Currently the data is freed as soon as the texture enters the ImTextureStatus_WantDestroy state, but its possible that the data could still be in use asynchronously (obviously relies on the whole "Textures blocks that have been used will never be rewritten" guarantee). Also fine if not! Just requires buffering |
Out of curiosity could you describe your use case? Dx12/Vulkan tend to use upload buffer which would require a copy anyhow. |
Instead of modifying As far as the default font size goes: Maybe a new style variable can be added for that? Or maybe there's just enforcement that the default font must always have an explicit size. (Specifying font size to |
I'm not @ZingBallyhoo and none of these situations are important to me personally, but I can imagine at least a few scenarios where someone might want the texture data to stick around after it's marked as
(That being said I don't think it's totally unreasonable to tell backends that they don't own the texture memory and therefore shouldn't hold onto it, but it also currently looks like it wouldn't be that hard to say it's an (opt-in?) responsibility of backends to call |
Main thread -> render thread syncronization. Only the render thread can manage GPU resources, so in order to upload data it either needs to be buffered into per-frame allocs or be (effectively) immutable & outlive the lifetime of the render frame. Buffering is fine of course and happens everywhere (e.g imgui vtx/idx data), but the setup is very close to not requiring it which is why I ask.
My thought was to just move it to be deleted upon reaching the Destroyed state (decided by the backed) instead of the WantDestroy state (decided by imgui usually). Most backends will be immediately setting textures to that state when requested anyway so no semantic change there. |
Here's my function ( //go:embed embed/fonts/RobotoCondensed-Medium.ttf
var embededFontRegularBytes []byte
//go:embed embed/fonts/fa-light.ttf
var embededIconsFontBytes []byte
func createFonts() {
fontSize := float32(20)
iconsFontSize := fontSize * 0.6
// Create font config, which will merge icons font with a previous text font
fontIconsConfig := NewFontConfig()
fontIconsConfig.SetMergeMode(true)
fontIconsConfig.SetName("Font Awesome 5 Icons")
defer fontIconsConfig.Delete()
imguiIO.Fonts().AddFontFromMemoryTTFV(embededFontRegularBytes, fontSize, 0)
imguiIO.Fonts().AddFontFromMemoryTTFV(embededIconsFontBytes, iconsFontSize, fontIconsConfig)
} |
Merged fonts may need different base scale so that needs to be expressed in some way of another (it could be a scale factor relative to main font). That's exactly the case in the issue that just got reported. I'll come up with something. @ZingBallyhoo
Thank you for reporting this. It's indeed a bug that happened during a rework last month. I've pushed a quick fix 24be548 now (I will rework that fix and merge it into earlier commits later). |
Thanks! It works without asserting now. |
@ZingBallyhoo I have confirmed that I see no issue deferring destruction of the pixel buffer to after backend has acknowledged it, so pushed that change 0f7c7bc. |
Pushed an experimental features/dynamic_fonts branch.
I am looking for feedback / early adopters to try this as we will inevitably find bugs or use cases that are not well supported.
The API are not 100% stabilized but it is expected they stay relatively close.
As of today, I only recommend experienced users to try this. Things should be simplified and clarified over the upcoming month.
WHEN YOU SUBMIT FEEDBACK PLEASE TRY TO INCLUDE RELEVANT CONTEXT. Things may look simple in the surface but there are lots of parameters under the hood.
Warning: Your first impression may be very dry: (1) the examples application are using the default bitmap fonts and are still not DPI aware. (but it is now trivial to scale the font dynamically) (2) we are not exposing a global "scale" for frustratingly subtle reasons which will evolve. But if you tinker with font loading and map a DragFloat() to
io.FontGlobalScale
you can see some of it more easily, e.g.ImGui::DragFloat("io.FontGlobalScale", &ImGui::GetIO().FontGlobalScale, 0.05f, 0.5f, 5.0f);
. It's going to make more sense in an established application. If you are using multiple fonts, large fonts, icon fonts, non-English languages, this will likely make things easier.TL;DR; Add a font once, use it as any size.
2025-01-27.font.resize.mp4
If you want to help testing this:
features/dynamic_fonts
branch of imgui.I am interested in different situations:
ImGuiBackendFlags_RendererHasTextures
?AddCustomRectRegular()
/AddCustomRectFontGlyph()
api: I am creating a dedicated topic Request for feedback: custom rect / custom glyphs / custom loaders withdynamic_fonts
branch. #8466 for feedback on those.The API we are replacing to interface with renderer backends has been here since January 2015 (!), so is more 10 years old. I want to ensure that the new API will also last long.
THE PLAN
My hope is that I can finish and merge this to master in 1~2 months but it will depends on amount of feedback/bugs, and the remaining work on more "General Scaling".
docking
.I am creating two topics:
dynamic_fonts
branch. #8466 solely related to support for CustomRect viaAddCustomRectRegular()
/AddCustomRectFontGlyph()
API.Your feedback, any sorts of feedback will help fixing bugs, improving documentation, comments and examples. Don't hesitate to share any sorts of feedback, or any report of what confused you, if even for a minute.
"WIP - xxxx"
. At some point when I am convinced enough that the commits are a good, I will stop sculpting history and start only adding to it.USEFUL REFERENCES COMMITS/DIFF
Public API
imgui.h
in the diff between docking and dynamic_fontsBackends:
Third Party Extensions
ImTextureUserID
type to simplify function calls: 1.92: Auto convert ImTextureID from ImTextureUserID ? cimgui/cimgui#287The text was updated successfully, but these errors were encountered: