ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
60.8k stars 10.27k forks source link

Vertical input offset when scaling window up that gets fixed by io.MouseDrawCursor (win32/dx11) #8076

Closed ewichuu closed 1 week ago

ewichuu commented 1 week ago

Version/Branch of Dear ImGui:

Version 1.91.1, Branch: docking

Back-ends:

imgui_impl_win32.cpp + imgui_impl_dx11.cpp

Compiler, OS:

Windows 11 + MSVC

Full config/build information:

Dear ImGui 1.91.1 (19110)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1938
define: _MSVC_LANG=201402
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: Custom Odin DX11
io.ConfigFlags: 0x00000000
io.ConfigViewportsNoDecoration
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00000C0E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 RendererHasVtxOffset
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 2048,2048
io.DisplaySize: 1280.00,720.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: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

Hello, I'm pretty sure this is not an issue on ImGui's part and I'm doing something wrong, but I can't find anyone else with the same issue, so I'd really appreciate some pointers.

My GUI looks and controls great at my app's start resolution of 1280x720, as you can see in the video. Then, I scale it up to 1920x1080 (proper d3d11 scaling with ResizeBuffers), and you can see how the buttons and menus highlight as if the cursor was hovering over them... a few pixels below where they should. Then, I go borderless fullscreen, which should still be 1920x1080, but it gets even worse and outright unusable.

As you can see, scaling to 1920x1080 makes the UI get blurry too, which is weird because I assume it shouldn't scale with the window unless I manually tell it to?

Notably this... cursor position offset for lack of a better term does not seem to be present horizontally, even at fullscreen (as in it doesn't highlight more pixels to the left/right than it should like it does vertically)

Some things I've tried:

I'm not exactly sure what io.MouseDrawCursor does that can somehow solve this? I'm happy with keeping it to true but it really doesn't feel like that's what should fix this issue.

Screenshots/Video:

https://github.com/user-attachments/assets/cb968cc4-06cd-4e5f-95ea-b4b34f155902

Minimal, Complete and Verifiable Example code:

I'm really not sure what code to show for this, it's just the demo window. If you need something specific tell me and I'll show it, sorry!

ocornut commented 1 week ago

Check if it happens with the unmodified vanilla example for dx11. It that works, you can compare your app with that code.

Then, I scale it up to 1920x1080 (proper d3d11 scaling with ResizeBuffers),

To debug an issue it is better to not made statements alluding that xx or xx has been done correctly, because it may shield you from seeing the error.

Printing io.MousePos and ImGui::GetCursorPos(), both say the same thing and seem to be totally correct

It’s not correct if it doesn’t line up with your OS mouse cursor.

Setting io.DisplaySize when resizing the window, doesn't seem to have any effect

You should never set this value yourself as the backend does it.

Setting io.MouseDrawCursor to true for debugging which, for some reason, completely fixes this issue.

Your issue is that there is a mismatch between OS mouse positions and positions renderer at the end of your pipeline. It could be down to wrong scaling of your framebuffer or many other possible causes. When you enable io.MouseDrawCursor it doesn’t fix the issue, but because imgui renders the cursor itself, its position in your window will match the coordinates of other imgui rendered items. But it’s not matching actually mouse OS coordinates so you’ll have a jump/gap when moving the mouse accross the edges of your window.

I'm really not sure what code to show for this,

The purpose of the “Minimal, Complete and Verifiable Example” is that put the work of narrowing down the issue to the code that causes it. If you disable/comment unrelated code and keep a minimum of code that exhibits the issues it is easier to study and share. Many times you’ll solve your problem.

ewichuu commented 1 week ago

Thanks a lot for the fast reply. I did what you said disabling and commenting all parts of the code to narrow down the issue, and like I suspected, it's not an ImGui issue at all. I also was scaling the buffers correctly - the problem was the swapchain.

Not sure why, but having the swapchain scaling mode to DXGI_SCALING_STRETCH caused this issue. Changing it to DXGI_SCALING_NONE completely fixed it, and this time properly. Thank you!