Skip to content

Commit 27f456f

Browse files
committed
Fix glfw_wgpu backend dpi scaling
Closes #3
1 parent 66c704e commit 27f456f

File tree

5 files changed

+22
-68
lines changed

5 files changed

+22
-68
lines changed

build.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ pub fn build(b: *std.Build) void {
241241
"libs/imgui/backends/imgui_impl_glfw.cpp",
242242
"libs/imgui/backends/imgui_impl_wgpu.cpp",
243243
},
244-
.flags = cflags,
244+
.flags = &(cflags.* ++ .{"-DGLFW_INCLUDE_NONE"}),
245245
});
246246
},
247247
.glfw_opengl3 => {
@@ -265,7 +265,7 @@ pub fn build(b: *std.Build) void {
265265
"libs/imgui/backends/imgui_impl_glfw.cpp",
266266
"libs/imgui/backends/imgui_impl_dx12.cpp",
267267
},
268-
.flags = cflags,
268+
.flags = &(cflags.* ++ .{"-DGLFW_INCLUDE_NONE"}),
269269
});
270270
imgui.linkSystemLibrary("d3dcompiler_47");
271271
},

libs/imgui/backends/imgui_impl_glfw.cpp

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@
9090

9191
#include "imgui.h"
9292
#ifndef IMGUI_DISABLE
93-
// FIX(zig-gamedev):
94-
// #include "imgui_impl_glfw.h"
93+
#include "imgui_impl_glfw.h"
9594

9695
// Clang warnings with -Weverything
9796
#if defined(__clang__)
@@ -101,8 +100,6 @@
101100
#endif
102101

103102
// GLFW
104-
// FIX(zig-gamedev):
105-
#define GLFW_INCLUDE_NONE
106103
#include <GLFW/glfw3.h>
107104

108105
#ifdef _WIN32
@@ -160,41 +157,6 @@
160157
#define GLFW_HAS_GAMEPAD_API (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwGetGamepadState() new api
161158
#define GLFW_HAS_GETKEYNAME (GLFW_VERSION_COMBINED >= 3200) // 3.2+ glfwGetKeyName()
162159
#define GLFW_HAS_GETERROR (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwGetError()
163-
#include <math.h>
164-
165-
// FIX(zig-gamedev):
166-
extern "C" {
167-
168-
bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks);
169-
bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks);
170-
bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks);
171-
void ImGui_ImplGlfw_Shutdown();
172-
void ImGui_ImplGlfw_NewFrame();
173-
174-
// GLFW callbacks install
175-
// - When calling Init with 'install_callbacks=true': ImGui_ImplGlfw_InstallCallbacks() is called. GLFW callbacks will be installed for you. They will chain-call user's previously installed callbacks, if any.
176-
// - When calling Init with 'install_callbacks=false': GLFW callbacks won't be installed. You will need to call individual function yourself from your own GLFW callbacks.
177-
void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window);
178-
void ImGui_ImplGlfw_RestoreCallbacks(GLFWwindow* window);
179-
180-
// GFLW callbacks options:
181-
// - Set 'chain_for_all_windows=true' to enable chaining callbacks for all windows (including secondary viewports created by backends or by user)
182-
void ImGui_ImplGlfw_SetCallbacksChainForAllWindows(bool chain_for_all_windows);
183-
184-
// GLFW callbacks (individual callbacks to call yourself if you didn't install callbacks)
185-
void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused); // Since 1.84
186-
void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered); // Since 1.84
187-
void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y); // Since 1.87
188-
void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
189-
void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
190-
void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
191-
void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c);
192-
void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor* monitor, int event);
193-
194-
// GLFW helpers
195-
void ImGui_ImplGlfw_Sleep(int milliseconds);
196-
197-
} // extern "C"
198160

199161
// GLFW data
200162
enum GlfwClientApi
@@ -218,8 +180,6 @@ struct ImGui_ImplGlfw_Data
218180
bool InstalledCallbacks;
219181
bool CallbacksChainForAllWindows;
220182
bool WantUpdateMonitors;
221-
222-
ImVec2 DpiScale; // fix(zig-gamedev)
223183
#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3
224184
const char* CanvasSelector;
225185
#endif
@@ -510,7 +470,7 @@ void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y)
510470
{
511471
ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
512472
if (bd->PrevUserCallbackCursorPos != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window))
513-
bd->PrevUserCallbackCursorPos(window, x * bd->DpiScale.x, y * bd->DpiScale.y); // fix(zig-gamedev)
473+
bd->PrevUserCallbackCursorPos(window, x, y);
514474

515475
ImGuiIO& io = ImGui::GetIO();
516476
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
@@ -520,8 +480,8 @@ void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y)
520480
x += window_x;
521481
y += window_y;
522482
}
523-
io.AddMousePosEvent((float)x * bd->DpiScale.x, (float)y * bd->DpiScale.y); // fix(zig-gamedev)
524-
bd->LastValidMousePos = ImVec2((float)x * bd->DpiScale.x, (float)y * bd->DpiScale.y); // fix(zig-gamedev)
483+
io.AddMousePosEvent((float)x, (float)y);
484+
bd->LastValidMousePos = ImVec2((float)x, (float)y);
525485
}
526486

527487
// Workaround: X11 seems to send spurious Leave/Enter events which would make us lose our position,
@@ -668,8 +628,6 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw
668628
bd->Time = 0.0;
669629
bd->WantUpdateMonitors = true;
670630

671-
bd->DpiScale = ImVec2{ 1.0f, 1.0f }; // fix(zig-gamedev)
672-
673631
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
674632
platform_io.Platform_SetClipboardTextFn = [](ImGuiContext*, const char* text) { glfwSetClipboardString(nullptr, text); };
675633
platform_io.Platform_GetClipboardTextFn = [](ImGuiContext*) { return glfwGetClipboardString(nullptr); };
@@ -837,8 +795,8 @@ static void ImGui_ImplGlfw_UpdateMouseData()
837795
mouse_x += window_x;
838796
mouse_y += window_y;
839797
}
840-
bd->LastValidMousePos = ImVec2((float)mouse_x * bd->DpiScale.x, (float)mouse_y * bd->DpiScale.y); // fix(zig-gamedev)
841-
io.AddMousePosEvent((float)mouse_x * bd->DpiScale.x, (float)mouse_y * bd->DpiScale.y); // fix(zig-gamedev)
798+
bd->LastValidMousePos = ImVec2((float)mouse_x, (float)mouse_y);
799+
io.AddMousePosEvent((float)mouse_x, (float)mouse_y);
842800
}
843801
}
844802

@@ -1007,11 +965,6 @@ void ImGui_ImplGlfw_NewFrame()
1007965
io.DisplaySize = ImVec2((float)w, (float)h);
1008966
if (w > 0 && h > 0)
1009967
io.DisplayFramebufferScale = ImVec2((float)display_w / (float)w, (float)display_h / (float)h);
1010-
1011-
// fix(zig-gamedev)
1012-
bd->DpiScale.x = ceil(io.DisplayFramebufferScale.x);
1013-
bd->DpiScale.y = ceil(io.DisplayFramebufferScale.y);
1014-
1015968
if (bd->WantUpdateMonitors)
1016969
ImGui_ImplGlfw_UpdateMonitors();
1017970

libs/imgui/backends/imgui_impl_glfw.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Resizing cursors requires GLFW 3.4+! Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
1212
// [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
1313
// Missing features or Issues:
14-
// [ ] Platform: Multi-viewport: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor).
14+
// [ ] Touch events are only correctly identified as Touch on Windows. This create issues with some interactions. GLFW doesn't provide a way to identify touch inputs from mouse inputs, we cannot call io.AddMouseSourceEvent() to identify the source. We provide a Windows-specific workaround.
15+
// [ ] Missing ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress cursors.
16+
// [ ] Multi-viewport: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor).
1517

1618
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
1719
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
@@ -29,11 +31,13 @@ struct GLFWwindow;
2931
struct GLFWmonitor;
3032

3133
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
32-
IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks);
33-
IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks);
34-
IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks);
35-
IMGUI_IMPL_API void ImGui_ImplGlfw_Shutdown();
36-
IMGUI_IMPL_API void ImGui_ImplGlfw_NewFrame();
34+
extern "C" { // fix(zig-gamedev)
35+
IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks);
36+
IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks);
37+
IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks);
38+
IMGUI_IMPL_API void ImGui_ImplGlfw_Shutdown();
39+
IMGUI_IMPL_API void ImGui_ImplGlfw_NewFrame();
40+
};
3741

3842
// Emscripten related initialization phase methods (call after ImGui_ImplGlfw_InitForOpenGL)
3943
#ifdef __EMSCRIPTEN__

libs/imgui/backends/imgui_impl_wgpu.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ static void ImGui_ImplWGPU_SetupRenderState(ImDrawData* draw_data, WGPURenderPas
399399
}
400400

401401
// Setup viewport
402-
wgpuRenderPassEncoderSetViewport(ctx, 0, 0, draw_data->FramebufferScale.x * draw_data->DisplaySize.x, draw_data->FramebufferScale.y * draw_data->DisplaySize.y, 0, 1);
402+
// fix(zig-gamedev): Workaround https://github.com/gfx-rs/wgpu/issues/1958 until we can update Dawn
403+
wgpuRenderPassEncoderSetViewport(ctx, 0, 0, (float)(int)(draw_data->FramebufferScale.x * draw_data->DisplaySize.x), (float)(int)(draw_data->FramebufferScale.y * draw_data->DisplaySize.y), 0, 1);
403404

404405
// Bind shader and vertex buffers
405406
wgpuRenderPassEncoderSetVertexBuffer(ctx, 0, fr->VertexBuffer, 0, fr->VertexBufferSize * sizeof(ImDrawVert));

src/backend_glfw_wgpu.zig

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,9 @@ pub fn deinit() void {
3030
backend_glfw.deinit();
3131
}
3232

33-
pub fn newFrame(fb_width: u32, fb_height: u32) void {
34-
ImGui_ImplWGPU_NewFrame();
33+
pub fn newFrame() void {
3534
backend_glfw.newFrame();
36-
37-
gui.io.setDisplaySize(@floatFromInt(fb_width), @floatFromInt(fb_height));
38-
gui.io.setDisplayFramebufferScale(1.0, 1.0);
39-
35+
ImGui_ImplWGPU_NewFrame();
4036
gui.newFrame();
4137
}
4238

0 commit comments

Comments
 (0)