-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
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
CustomRect API problems (AddCustomRectRegular) #8107
Comments
Before everything: have you thoroughly measured the performance cost of texture changes? |
I've been working on completely rewriting this system, so it's going to change and I am unlikely to want to put much energy into fixing the existing one, but I will investigate your 3 issues and at minimum try to make sure the new system doesn't have them. I'll keep this open in the meanwhile. Thanks! EDIT I believe if we at least fix (1) which is very easy, it should reduce the occurrences of (2). |
In case of drawing a lot of buttons with custom image and text (Tabs) amount of draw calls was reduced from 27 and 5 to 1 and 1. It improved FPS a little.
It is not a common use case though. It is webgl version on a very outdated hardware. There are other things to improve, but apart from some minor problems, merging of atlases gave me a small visible improvement. |
I forgot to make previous message as a reply. But tl;dr I believe that on more or less modern hardware it wouldn't be a problem, but I have a very specific use case where every small optimization matters. |
Thank you for your thoughtful initial post and answer. |
Yes, it is low priority. I can set texture width and in case of problems I can patch my fork of ImGui to fix at least some mentioned problems. But I believe that hardcoding of width and making manual padding will solve all my issues without touching imgui. |
…not accounted in surface area. (#8107)
I have pushed 19a1f2a with the following fixes: (1) Fixed: "Custom rects are not added in total_surface value in ImFontAtlasBuildWithStbTruetype(), so auto calculated width is not optimal in case surface of custom rects is comparable with total surface of all fonts." (3) Fixed "It seems that TexGlyphPadding is applied only for... tex glyphs, but not for custom rects." My working branch will better handle packing errors mentioned in (2). It is "correct" that a 512 wide custom rect cannot be packed into a 512 wide texture, as we want to honor TexGlyphPadding for bilinear filtering. Please note that in future version I may rename TexGlyphPadding to TexPackPadding for clarity.
Technically I am keeping this open to track those details when I work on the new system. Thank you for the careful details and test code. |
Technically if we use autodetect of used width, then any custom rect of reasonable width (not 20k+) can be packed if autodetected width will greater or equal to max(custom_rect_widths)+TexPackPadding. The only way rect can't be packed should be case when font texture width is set by user (or custom rect width is unreasonable). P.S. should I edit my post with numbered points to add things that other users mentioned? So it would be easier to track. I mean. |
For (4):
How do you currently do that? AFAIK AddFont() always requires font data. For (5) the workaround is: |
At least these issues can be considered in the new implementation. I hope it includes possibility to store A or RGBA data on the rects registration stage, so inconvinient post processing after |
Can you clarify? |
I mean, currently process is performed in 2 steps // STEP 1. Register only rects before io.Fonts->Build();
// =====================================================
// Add font, then register two custom 13x13 rectangles mapped to glyph 'a' and 'b' of this font
ImFont* font = io.Fonts->AddFontDefault();
int rect_ids[2];
rect_ids[0] = io.Fonts->AddCustomRectFontGlyph(font, 'a', 13, 13, 13+1);
rect_ids[1] = io.Fonts->AddCustomRectFontGlyph(font, 'b', 13, 13, 13+1);
// Build atlas
io.Fonts->Build();
// STEP 2. Fill result atlas with actual data after io.Fonts->Build();
// ===================================================================
// Retrieve texture in RGBA format
unsigned char* tex_pixels = nullptr;
int tex_width, tex_height;
io.Fonts->GetTexDataAsRGBA32(&tex_pixels, &tex_width, &tex_height);
for (int rect_n = 0; rect_n < IM_ARRAYSIZE(rect_ids); rect_n++)
{
int rect_id = rect_ids[rect_n];
if (const ImFontAtlasCustomRect* rect = io.Fonts->GetCustomRectByIndex(rect_id))
{
// Fill the custom rectangle with red pixels (in reality you would draw/copy your bitmap data here!)
for (int y = 0; y < rect->Height; y++)
{
ImU32* p = (ImU32*)tex_pixels + (rect->Y + y) * tex_width + (rect->X);
for (int x = rect->Width; x > 0; x--)
*p++ = IM_COL32(255, 0, 0, 255);
}
}
} To perform the second step, I had to modify example to receive callback after Build() is called, because it is called asynchronously on the first draw. It would be more convenient if it would be something like uint8_t aGlyphPixelsRGBA[13*13*4] = {...};
uint8_t bGlyphPixelsA[13*13] = {...};
ImFont* font = io.Fonts->AddFontDefault();
io.Fonts->AddCustomRectFontGlyphColored(font, 'a', 13, 13, 13+1, aGlyphPixelsRGBA);
io.Fonts->AddCustomRectFontGlyphGrayscale(font, 'b', 13, 13, 13+1, bGlyphPixelsA);
// Build atlas
io.Fonts->Build(); |
You can manually call Build() yourself during init to simplify this.
Yes it is possible with the new version. |
…not accounted in surface area. (ocornut#8107)
Version/Branch of Dear ImGui:
Version 1.91.4, Branch: master
Back-ends:
does not apply
Compiler, OS:
does not apply
Full config/build information:
Details:
My Issue:
I use ImGui to draw a custom service menu with some additional graphics like button texture, company logo etc. So far I used additional texture atlas for custom graphics. But for performance reasons I have tried to pack custom graphics in ImGui atlas using custom rect API as described in using-custom-colorful-icons.
It worked fine most of the time, but I found several limitations:
pot(max(custom rects widths) + TexGlyphPadding)
. It can be optionally disabled with ImFontAtlasFlags_ like other things, but it is nice to have consideration.MCVE is not enough for demonstration, so, I prepared the patch file for example_sdl2_opengl3 instead. Unfortunately github doesn't want to attach .patch files, so I provide its content below (it might be easier to make a fork and add link to the commit, but I'm too lazy to make a fork for it at this point).
0001-custom-rect-example.patch
Result looks like this:

requested: 6 colored rects
result: 4 colored rects were added next to each other, 2 rects were silently skipped
Screenshots/Video:
No response
Minimal, Complete and Verifiable Example code:
No response
The text was updated successfully, but these errors were encountered: