imgui icon indicating copy to clipboard operation
imgui copied to clipboard

Display Scaling issue with glfw/wgpu

Open echoptic opened this issue 7 months ago • 6 comments

Version/Branch of Dear ImGui:

Version 1.92.0 WIP, Branch: master

Back-ends:

imgui_impl_glfw.cpp + imgui_impl_wgpu.cpp

Compiler, OS:

Linux 6.14.5 (KDE/Wayland) + Clang

Full config/build information:

Dear ImGui 1.92.0 WIP (19194)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=201703
define: __linux__
define: __GNUC__=4
define: __clang_version__=20.1.2 (https://github.com/ziglang/zig-bootstrap 2fd9c31b6156e8691f2ec87fc6e56fc68038252a)
--------------------------------
io.BackendPlatformName: imgui_impl_glfw
io.BackendRendererName: imgui_impl_webgpu_wgpu
io.ConfigFlags: 0x00000001
 NavEnableKeyboard
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x0000000E
 HasMouseCursors
 HasSetMousePos
 RendererHasVtxOffset
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,64
io.DisplaySize: 1280.00,720.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

Whenever I would draw any widget and tried to draw it using ImGui_ImplWGPU_RenderDrawData i would get an error:

wgpu uncaptured error:
Validation Error

Caused by:
  In wgpuRenderPassEncoderEnd
    In a set_viewport command
      Viewport has invalid rect Rect { x: 0.0, y: 0.0, w: 1536.0, h: 864.00006 }; origin and/or size is less than or equal to 0, and/or is not contained in the render target (1280, 720, 1)

This problem only exists when Im using higher that 100% display scale. I was using 120%. I noticed that no matter what window size i set the rect would always be 20% larger and when I set the scale back to 100% everything worked perfectly.

Im not sure if this is an error on my part, the glfw + webgpu example is very outdated and i didnt try to follow it perfectly, but i didnt see any code handling display scale in the example.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

echoptic avatar May 08 '25 20:05 echoptic

What's the value of io.DisplayFramebufferScale in the crashing state? That's local WGPU or Emscripten WGPU ?

ocornut avatar May 09 '25 16:05 ocornut

The example code does this:

        // React to changes in screen size
        int width, height;
        glfwGetFramebufferSize((GLFWwindow*)window, &width, &height);
        if (width != wgpu_swap_chain_width || height != wgpu_swap_chain_height)
        {
            ImGui_ImplWGPU_InvalidateDeviceObjects();
            CreateSwapChain(width, height);
            ImGui_ImplWGPU_CreateDeviceObjects();
        }

To obtain the expected framebuffer size. What's your value for this?

ocornut avatar May 09 '25 16:05 ocornut

DisplayFramebufferScale: { .x = 1.2, .y = 1.2 } Its not Emscripten. I have this in the main loop, the swapchain api was removed and merged into surface:

            var width: c_int = undefined;
            var height: c_int = undefined;
            c.glfwGetFramebufferSize(window, &width, &height);
            if (width != window_width or height != window_height) {
                c.cImGui_ImplWGPU_InvalidateDeviceObjects();
                config.width = @intCast(width);
                config.height = @intCast(height);
                c.wgpuSurfaceConfigure(surface, &config);
                _ = c.cImGui_ImplWGPU_CreateDeviceObjects();
            }

echoptic avatar May 10 '25 09:05 echoptic

Also I dont know if it matters, i have a print statement to print DisplayFramebufferScale after ImGui_Render and it prints 2 times before crashing:

DisplayFramebufferScale: cimport.struct_ImVec2_t{ .x = 1.2e0, .y = 1.2e0 }
DisplayFramebufferScale: cimport.struct_ImVec2_t{ .x = 1.2e0, .y = 1.2e0 }

thread '<unnamed>' panicked at src/lib.rs:417:5:
wgpu uncaptured error:
Validation Error

Caused by:
  In wgpuRenderPassEncoderEnd
    In a set_viewport command
      Viewport has invalid rect Rect { x: 0.0, y: 0.0, w: 1536.0, h: 864.00006 }; origin and/or size is less than or equal to 0, and/or is not contained in the render target (1536, 864, 1)


note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

thread '<unnamed>' panicked at library/core/src/panicking.rs:218:5:
panic in a function that cannot unwind
stack backtrace:
   0:          0x18516ca - std::backtrace_rs::backtrace::libunwind::trace::h2740d05102fd9881
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:          0x18516ca - std::backtrace_rs::backtrace::trace_unsynchronized::h93ae2edf130065c8
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:          0x18516ca - std::sys::backtrace::_print_fmt::h7660a544e6110dd9
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/sys/backtrace.rs:66:9
   3:          0x18516ca - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::hfc616348d9ad0abc
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/sys/backtrace.rs:39:26
   4:          0x17ffd03 - core::fmt::rt::Argument::fmt::h3a92517f8117fcd5
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/fmt/rt.rs:177:76
   5:          0x17ffd03 - core::fmt::write::h7ca648217bc79799
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/fmt/mod.rs:1440:21
   6:          0x1844fd3 - std::io::Write::write_fmt::h7960c58bfa5ccbcb
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/io/mod.rs:1887:15
   7:          0x1851512 - std::sys::backtrace::BacktraceLock::print::h3fb349e80cbe0423
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/sys/backtrace.rs:42:9
   8:          0x1853bf0 - std::panicking::default_hook::{{closure}}::h3366e5842cba645d
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/panicking.rs:295:22
   9:          0x18539d0 - std::panicking::default_hook::hd7573a5d4879884b
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/panicking.rs:322:9
  10:          0x1854442 - std::panicking::rust_panic_with_hook::h66e909d048c263a9
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/panicking.rs:828:13
  11:          0x1854196 - std::panicking::begin_panic_handler::{{closure}}::h8d9aa8be7e8634cf
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/panicking.rs:694:13
  12:          0x1851bd9 - std::sys::backtrace::__rust_end_short_backtrace::h7d7e47ef99abf6aa
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/sys/backtrace.rs:168:18
  13:          0x1853e5d - rust_begin_unwind
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/panicking.rs:692:5
  14:          0x17fbe0d - core::panicking::panic_nounwind_fmt::runtime::h19438eeae7eaf031
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/panicking.rs:117:22
  15:          0x17fbe0d - core::panicking::panic_nounwind_fmt::h57347130f21a7343
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/intrinsics/mod.rs:3869:9
  16:          0x17fbea2 - core::panicking::panic_nounwind::h2f7749cb358aa979
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/panicking.rs:218:5
  17:          0x17fc066 - core::panicking::panic_cannot_unwind::hf517234e476fc7c7
                               at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/panicking.rs:307:5
  18:          0x17d8042 - wgpu_native::default_uncaptured_error_handler::h7da5c1449171eff8
  19:          0x17d8411 - wgpu_native::ErrorSinkRaw::handle_error::hb8ae9c14271dc633
  20:          0x17d9db2 - wgpu_native::handle_error::h26da8dc4d06d00ce
  21:          0x17eee85 - wgpuRenderPassEncoderEnd
  22:          0x1e8ba74 - main.main
                               at /home/echoptic/src/voxel/src/main.zig:354:35
  23:          0x1e8c41d - start.callMain
                               at /home/echoptic/zig/0.15.0-dev.471+369177f0b/files/lib/std/start.zig:671:37
  24:          0x1e8c41d - start.callMainWithArgs
                               at /home/echoptic/zig/0.15.0-dev.471+369177f0b/files/lib/std/start.zig:631:20
  25:          0x1e8c41d - start.main
                               at /home/echoptic/zig/0.15.0-dev.471+369177f0b/files/lib/std/start.zig:646:28
  26:     0x7fef649cabfc - __libc_start_call_main
                               at ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
  27:     0x7fef649cacb5 - __libc_start_main_impl
                               at ./csu/../csu/libc-start.c:360:3
  28:          0x131e751 - _start
                               at /builddir/glibc-2.41/csu/../sysdeps/x86_64/start.S:115
thread caused non-unwinding panic. aborting.

echoptic avatar May 10 '25 09:05 echoptic

can you print for each frame:

  • the value returned by glfwGetFramebufferSize()
  • the value of io.DisplaySize
  • the value of io.DisplayFramebufferScale

ocornut avatar May 10 '25 12:05 ocornut

width: 1536, height: 864
DisplaySize: cimport.struct_ImVec2_t{ .x = -1e0, .y = -1e0 }
DisplayFramebufferScale: cimport.struct_ImVec2_t{ .x = 1e0, .y = 1e0 }
width: 1536, height: 864
DisplaySize: cimport.struct_ImVec2_t{ .x = 1.28e3, .y = 7.2e2 }
DisplayFramebufferScale: cimport.struct_ImVec2_t{ .x = 1.2e0, .y = 1.2e0 }

thread '<unnamed>' panicked at src/lib.rs:417:5:
wgpu uncaptured error:
Validation Error

Caused by:
  In wgpuRenderPassEncoderEnd
    In a set_viewport command
      Viewport has invalid rect Rect { x: 0.0, y: 0.0, w: 1536.0, h: 864.00006 }; origin and/or size is less than or equal to 0, and/or is not contained in the render target (1536, 864, 1)

echoptic avatar May 10 '25 15:05 echoptic

I'm sorry, I'm just reading this post now.

I read that you are on wayland, but the application is using wayland (directly) or x11 on wayland? (Sorry to ask, but there is a reason)

WebGPU previously required viewport rectangles to be entirely contained within the framebuffer (unlike all other native graphics backends). This limitation "should" have been removed/relaxed (about March) as also reported in Google Chrome 135 what's new in WebGPU

This problem still occurs in X11 (e.g. in the enlarging of the window with GLFW, as explained here in "about GLFW viewport error") , but it seemed to be fixed in wayland (as well as in Windows/MaOS and EMSCRIPTEN) (N.B. X11 had/have also others problems, e.g. with NVidia and Intel drivers, as I reported in Google DAWN)

Anyway the message of the VL cannot be avoided, but the application crash could be avoided by not entering, in the RenderPassEncoder, a SurfaceTexture that is not in "Optimal Status". In your example do you control the Surface Texture State? (and in case re-create the Surface)?
Something like that: check_surface_texture_status (You can find a similar function in the new ImGui examples too)

Here are the new WebGPU examples, both tested with WGPU and Google DAWN... and also in EMSCRIPTEN... for GLFW / SDL2 / SDL3 framwork backends: ImGui fork with new WebGPU examples ImGui WebGPU examples PullRequest discussion

The examples are in Pull Request since March (but not yet in the official ImGui repo) and constantly evolving (for the continuous changes of WebGPU SDK)

Can you try your settings with one of the new examples? Or if you post the example I can try to adapt and test it.

Thanks

BrutPitt avatar Jul 02 '25 20:07 BrutPitt