Skip to content

SDL_GPU backend on macOS crash (assert) when going fullscreen #8703

Open

Description

@jclounge

Version/Branch of Dear ImGui:

master

Back-ends:

SDL3_GPU

Compiler, OS:

macOS

Full config/build information:

n/a

Details:

On macOS, using the SDL3_GPU backend, when making the demo app window fullscreen with the standard window fullscreen button, it causes an assert/crash when trying to set scissor extents incorrectly. This appears to be due to the ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale() function getting the wrong scale values. Here is a modified version of the function that appears to fix the issue:

static void ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale(SDL_Window* window, ImVec2* out_size, ImVec2* out_framebuffer_scale)
{
    int w, h;
    int display_w, display_h;
    SDL_GetWindowSize(window, &w, &h);
    if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
        w = h = 0;

    if (out_size != nullptr)
        *out_size = ImVec2((float)w, (float)h);

    // this version using SDL_GetWindowDisplayScale() seems to get the correct scale values
    float scale = SDL_GetWindowDisplayScale(window);
    if (out_framebuffer_scale != nullptr)
        *out_framebuffer_scale = (scale > 0.0f) ? ImVec2(scale, scale) : ImVec2(1.0f, 1.0f);
}

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

The example demo app: example_sdl3_sdlgpu3/main.cpp

Activity

  1. ocornut commented on Jun 19, 2025

    @ocornut
    Owner

    Thank you @jclounge. I will investigate this soon.

    Are you on latest master?
    Some time ago I tried latest with the new texture update feature (#8465) pushed on June 12, and noticed glitches/flickering on macOS. Do you see them?

  2. jclounge commented on Jun 20, 2025

    @jclounge
    Author

    Thanks @ocornut, yes I was on the latest master at the time of writing yesterday.

    Sorry I've barely started looking at SDL and Dear ImGui, but so far I haven't noticed flickering in particular. I'm on an Intel iMac with Radeon 560X running macOS 15.2, and SDL_GPU is using the Metal driver.

    EDIT: Now I've looked slightly more and yes I get glitches when changing font size back and forth with the slider in the demo app, particularly for larger sizes. Glyphs go funny for something like a single frame maybe a couple of times per second while sliding the size, but they never stay glitched after sliding stops.

  3. added this to the v1.92 milestone on Jun 24, 2025
  4. ocornut commented on Jun 25, 2025

    @ocornut
    Owner

    FYI I cannot reproduce on my MacBook Air.

    Can you clarify which are the "correct" and "incorrect" value (output of both calls) on your setup while in fullscreen mode, and while not in fullscreen mode?

  5. ocornut commented on Jun 25, 2025

    @ocornut
    Owner

    EDIT: Now I've looked slightly more and yes I get glitches when changing font size back and forth with the slider in the demo app, particularly for larger sizes. Glyphs go funny for something like a single frame maybe a couple of times per second while sliding the size, but they never stay glitched after sliding stops.

    Those glitches should now be fixed with dcf1450

  6. jclounge commented on Jun 25, 2025

    @jclounge
    Author

    FYI I cannot reproduce on my MacBook Air.

    Can you clarify which are the "correct" and "incorrect" value (output of both calls) on your setup while in fullscreen mode, and while not in fullscreen mode?

    With a 1X scaled screen resolution, I get a scale value of 1 when not in fullscreen mode which is good, but then it goes to something other than 1 when in fullscreen mode which ends up causing the scissor assert.

    e.g. In this line: *out_framebuffer_scale = (w > 0 && h > 0) ? ImVec2((float)display_w / w, (float)display_h / h) : ImVec2(1.0f, 1.0f); I just now got the values: w = 1280, h = 720, display_w = 2048, display_h = 1152, which results in a scale of 1.6 .

    Those w and h values appear to be the size of the main app window before it was triggered to go fullscreen, so if the window is first resized before it's fullscreened, the scale ends up different again.

    When the above modified function is used instead, the scale stays at 1 in both non-fullscreen and fullscreen, and scissor appears to work: there's no assert and the scrollable content of the demo window is clipped as expected.

    With a 2X scaled (retina) screen resolution, the scale value is 2 when not in fullscreen, but then similarly goes different when going to fullscreen. Using the modified function, the scale stays at value 2 in both non-fullscreen and fullscreen modes and scissor seems to work.

  7. ocornut commented on Jun 25, 2025

    @ocornut
    Owner

    Could you try with SDL_GetWindowPixelDensity() instead?

    SDL_GetWindowDisplayScale() is definitively incorrect under e.g. Windows as it is a combination of density and user-facing scale, but SDL_GetWindowPixelDensity() may be good.

  8. ocornut commented on Jun 25, 2025

    @ocornut
    Owner

    Actually it seems to do what we already do:

    float SDL_GetWindowPixelDensity(SDL_Window *window)
    {
        int window_w, window_h, pixel_w, pixel_h;
        float pixel_density = 1.0f;
    
        CHECK_WINDOW_MAGIC(window, 0.0f);
    
        if (SDL_GetWindowSize(window, &window_w, &window_h) &&
            SDL_GetWindowSizeInPixels(window, &pixel_w, &pixel_h)) {
            pixel_density = (float)pixel_w / window_w;
        }
        return pixel_density;
    }

    Can you confirm that you are having the issue you reported on (1) latest commit and (2) using the unmodified example_sdl3_sdlgpu3/main.cpp example?

  9. removed this from the v1.92 milestone on Jun 25, 2025
  10. jclounge commented on Jun 25, 2025

    @jclounge
    Author

    Can you confirm that you are having the issue you reported on (1) latest commit and (2) using the unmodified example_sdl3_sdlgpu3/main.cpp example?

    Yes, I just updated to latest master, using the unmodified example code, and I still get the scissor assert.

  11. jclounge commented on Jun 26, 2025

    @jclounge
    Author

    I just ran an experiment to see what the scale values are doing. First I commented out the scissor commands that have the asserts in them, so the app can keep running when going fullscreen. Then I added this code into the example code to print changes of scale value and how many frames between changes:

            ...
            ImGui::NewFrame();
    
            static ImVec2 curScale;
            static int numFramesSameScale = 0;
            if (io.DisplayFramebufferScale != curScale)
            {
                curScale = io.DisplayFramebufferScale;
                printf("After %d frames, new scale is now %f\n", numFramesSameScale, curScale.x);
                numFramesSameScale = 0;
            }
            numFramesSameScale ++;
            ...
    

    The output from starting, to going fullscreen, then going back to non-fullscreen, looks like this:

    After 0 frames, new scale is now 1.000000
    After 105 frames, new scale is now 1.600000
    After 6 frames, new scale is now 1.000000
    After 145 frames, new scale is now 0.625000
    After 5 frames, new scale is now 1.000000
    Program ended with exit code: 0
    

    So it looks like the scale was wrong for 6 frames when going fullscreen, then was correct again, then when leaving fullscreen the scale went wrong again for 5 frames, but again settled to the correct value.

    Another run I did had 8 frames of wrong value, so the duration of getting bad values is pretty random.

    EDIT: Maybe it's something in SDL itself? I'm using the latest binary release, SDL3-3.2.16 .

  12. ocornut commented on Jun 26, 2025

    @ocornut
    Owner

    Do you get the same results out of ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale() when using the SDL3+OpenGL, SDL3+SDLRenderer, SDL3+Vulkan examples?

    Your debug output would be simpler to read if you included the return value of all functions (resolutions etc). You can use IMGUI_DEBUG_LOG() to include a frame counter in the printout.

    I think it is worth reporting to SDL3.

    We probably can workaround the issue by using your suggested replacement but for Mac only, not on other systems.

  13. jclounge commented on Jun 26, 2025

    @jclounge
    Author

    Do you get the same results out of ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale() when using the SDL3+OpenGL, SDL3+SDLRenderer, SDL3+Vulkan examples?

    Your debug output would be simpler to read if you included the return value of all functions (resolutions etc). You can use IMGUI_DEBUG_LOG() to include a frame counter in the printout.

    I think it is worth reporting to SDL3.

    We probably can workaround the issue by using your suggested replacement but for Mac only, not on other systems.

    Sorry I haven't done all of that yet, but what I have now done is test SDL3+OpenGL3 and the problem doesn't occur there. However, when testing with SDL2+Metal, the problem is there, so it's not just with SDL3 but also SDL2. I'm using the latest SDL2-2.32.8 binary release for that test now, but first it was also failing with my old installed SDL2 from way back in 2017. So the primary common factor seems to be Metal running on my machine, unless I've missed something?

    Thanks yeah, some kind of workaround sounds good to me, anything to make it work smoothly. :)

  14. jclounge commented on Jun 26, 2025

    @jclounge
    Author

    Oh here's something strange, the issue only seems to occur when running from inside Xcode! If I go to the executable and run it from finder, the problem goes away. Not sure if this means the asserts are not being executed or something, or what else could be going on.

  15. ocornut commented on Jun 26, 2025

    @ocornut
    Owner

    Sorry I haven't done all of that yet, but what I have now done is test SDL3+OpenGL3 and the problem doesn't occur there. However, when testing with SDL2+Metal, the problem is there, s

    Can you clarify if by "the problem" you mean that you verified that the scaling factor were correct/incorrect, or you only checked for the API Asserting? Because OpenGL likely won't assert the same way, regardless of clipping coordinates being correct or it.
    It's not really important if you get an assert, what's important is that the FramebufferScale is 1.0f or 2.0f.

  16. jclounge commented on Jun 26, 2025

    @jclounge
    Author

    Can you clarify if by "the problem" you mean that you verified that the scaling factor were correct/incorrect, or you only checked for the API Asserting? Because OpenGL likely won't assert the same way, regardless of clipping coordinates being correct or it. It's not really important if you get an assert, what's important is that the FramebufferScale is 1.0f or 2.0f.

    Okay, I checked the SDL3+OpenGL3 version and as you have pointed out, it indeed gets the incorrect scaling values, but just doesn't run into an assert.

  17. jclounge commented on Jun 26, 2025

    @jclounge
    Author

    Another test, this time using raylib+rlImGui, and the scale values appear to stay consistently correct.

    So SDL3+SDL_GPU, SDL2+Metal, and SDL3+OpenGL3 all report incorrect scale values, but raylib+rlImGui reports correct values.

    (Also, when SDL3+SDL_GPU is running directly from the terminal, even though it still has the incorrect scaling values it doesn't get stopped at the assert and just keeps running, unlike when it's running from Xcode where the assert causes a debug break.)

  18. jclounge commented on Jun 26, 2025

    @jclounge
    Author

    I think it is worth reporting to SDL3.

    Yes it looks like SDL is reporting those values incorrectly even on its own without Dear ImGui there at all. I just tested some other SDL examples, and SDL_GetWindowPixelDensity() has the same issue when called inside those as well.

    Sorry about all the hassle with this, and thanks so much for all your help and patience!

    EDIT: I've reported the issue to SDL here.

  19. ocornut commented on Jun 26, 2025

    @ocornut
    Owner

    What's interesting is that effectively you are also suggesting that SDL_GetWindowDisplayScale() currently doesn't have this problem, and we could use it on Mac/iOS. (But we cannot use it on other systems)

  20. jclounge commented on Jun 26, 2025

    @jclounge
    Author

    What's interesting is that effectively you are also suggesting that SDL_GetWindowDisplayScale() currently doesn't have this problem, and we could use it on Mac/iOS. (But we cannot use it on other systems)

    I've just double-checked with the SDL-only example, and SDL_GetWindowDisplayScale() is still working consistently there too, at least on my iMac. If you think it can work cleanly on Mac/iOS, that's great!

  21. jclounge commented on Jul 2, 2025

    @jclounge
    Author

    Hi @ocornut, so just to follow up, is this going to be addressed in Dear ImGui? I see the issue was added and then removed from a milestone and I'm not sure what that means.

  22. ocornut commented on Jul 2, 2025

    @ocornut
    Owner

    I’ll probably implement the workaround but it seems too risky for 1.92 but probably for 1.92.1.

  23. added a commit that references this issue on Jul 6, 2025
  24. ocornut commented on Aug 4, 2025

    @ocornut
    Owner

    Also see #4414

  25. ocornut commented on Sep 15, 2025

    @ocornut
    Owner

    Hello @jclounge ,
    Sorry for my late answer.

    I have pushed your suggested change for SDL3: 22a6a83

    • Could you confirm the fix for you?

    I would like to make the equivalent change for SDL2 but I am not sure which function to use since SDL_GetWindowDisplayScale() does not exist. Would you be able to experiment and see how you can obtain a reliable value on SDL2 for the equivalent?

  26. jclounge commented on Sep 16, 2025

    @jclounge
    Author

    Thanks @ocornut, it works here!

    I've had a little look at making a similar workaround for SDL2, but I don't know how to do it, sorry.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址