From 9a4fd69f6dd6f9cdf89c744bb7a796bc9db0a0b2 Mon Sep 17 00:00:00 2001 From: ocornut Date: Wed, 10 Dec 2025 18:35:22 +0100 Subject: [PATCH] Backends: GLFW: avoid repeated glfwSetCursor()/glfwSetInputMode() calls when unnecessary. --- backends/imgui_impl_glfw.cpp | 17 ++++++++++++++--- docs/CHANGELOG.txt | 2 ++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/backends/imgui_impl_glfw.cpp b/backends/imgui_impl_glfw.cpp index ac176a080..d088e9693 100644 --- a/backends/imgui_impl_glfw.cpp +++ b/backends/imgui_impl_glfw.cpp @@ -29,6 +29,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2025-12-10: Avoid repeated glfwSetCursor()/glfwSetInputMode() calls when unnecessary. Lowers overhead for very high framerates (e.g. 10k+ FPS). // 2025-11-06: Lower minimum requirement to GLFW 3.0. Though a recent version e.g GLFW 3.4 is highly recommended. // 2025-09-18: Call platform_io.ClearPlatformHandlers() on shutdown. // 2025-09-15: Content Scales are always reported as 1.0 on Wayland. FramebufferScale are always reported as 1.0 on X11. (#8920, #8921) @@ -188,6 +189,7 @@ struct ImGui_ImplGlfw_Data GLFWwindow* MouseWindow; #if GLFW_HAS_CREATECURSOR GLFWcursor* MouseCursors[ImGuiMouseCursor_COUNT]; + GLFWcursor* LastMouseCursor; #endif ImVec2 LastValidMousePos; bool IsWayland; @@ -849,15 +851,24 @@ static void ImGui_ImplGlfw_UpdateMouseCursor() GLFWwindow* window = bd->Window; if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor) { - // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + if (bd->LastMouseCursor != nullptr) + { + // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + bd->LastMouseCursor = nullptr; + } } else { // Show OS mouse cursor // FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here. #if GLFW_HAS_CREATECURSOR - glfwSetCursor(window, bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]); + GLFWcursor* cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]; + if (bd->LastMouseCursor != cursor) + { + glfwSetCursor(window, cursor); + bd->LastMouseCursor = cursor; + } #endif glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); } diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 2353faddb..32008203d 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -87,6 +87,8 @@ Other Changes: non-ASCII values to io.AddInputCharacter(). (#9099) - Debug Log: can output to debugger on Windows. (#5855) - Backends: + - GLFW: Avoid repeated glfwSetCursor()/glfwSetInputMode() calls when unnecessary. + Lowers overhead for very high framerates (e.g. 10k+ FPS). [@maxliani] - SDL_GPU3: macOS version can use MSL shaders in order to support macOS 10.14+ (vs Metallib shaders requiring macOS 14+). Requires application calling SDL_CreateGPUDevice() with SDL_GPU_SHADERFORMAT_MSL. (#9076) [@Niminem]