Skip to content

Commit df3f2ff

Browse files
BrutPittocornut
authored andcommitted
Examples: WebGPU: moved CreateWGPUSurface to the bottom of the file due to interference with X.h. (#8381)
1 parent 8e5e790 commit df3f2ff

File tree

3 files changed

+185
-186
lines changed

3 files changed

+185
-186
lines changed

examples/example_glfw_wgpu/main.cpp

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ static int wgpu_surface_width = 1280;
4040
static int wgpu_surface_height = 800;
4141

4242
// Forward declarations
43-
static bool InitWGPU(GLFWwindow* window);
43+
static bool InitWGPU(GLFWwindow* window);
44+
static WGPUSurface CreateWGPUSurface(const WGPUInstance& instance, GLFWwindow* window);
4445

4546
static void glfw_error_callback(int error, const char* description)
4647
{
@@ -290,80 +291,6 @@ int main(int, char**)
290291
return 0;
291292
}
292293

293-
// GLFW helper to create a WebGPU surface, used only in WGPU-Native. DAWN-Native already has a built-in function
294-
// As of today (2025/10) there is no "official" support in GLFW to create a surface for WebGPU backend
295-
// This stub uses "low level" GLFW calls to acquire information from a specific Window Manager.
296-
// Currently supported platforms: Windows / Linux (X11 and Wayland) / MacOS. Not necessary nor available with EMSCRIPTEN.
297-
#if !defined(__EMSCRIPTEN__) && (defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) || defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN))
298-
299-
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
300-
#define GLFW_HAS_X11_OR_WAYLAND 1
301-
#else
302-
#define GLFW_HAS_X11_OR_WAYLAND 0
303-
#endif
304-
#ifdef _WIN32
305-
#undef APIENTRY
306-
#ifndef GLFW_EXPOSE_NATIVE_WIN32 // for glfwGetWin32Window()
307-
#define GLFW_EXPOSE_NATIVE_WIN32
308-
#endif
309-
#elif defined(__APPLE__)
310-
#ifndef GLFW_EXPOSE_NATIVE_COCOA // for glfwGetCocoaWindow()
311-
#define GLFW_EXPOSE_NATIVE_COCOA
312-
#endif
313-
#elif GLFW_HAS_X11_OR_WAYLAND
314-
#ifndef GLFW_EXPOSE_NATIVE_X11 // for glfwGetX11Display(), glfwGetX11Window() on Freedesktop (Linux, BSD, etc.)
315-
#define GLFW_EXPOSE_NATIVE_X11
316-
#endif
317-
#ifndef GLFW_EXPOSE_NATIVE_WAYLAND
318-
#if defined(__has_include) && __has_include(<wayland-client.h>)
319-
#define GLFW_EXPOSE_NATIVE_WAYLAND
320-
#endif
321-
#endif
322-
#endif
323-
#include <GLFW/glfw3native.h>
324-
#undef Status // X11 headers are leaking this.
325-
#undef Success // X11 headers are leaking this.
326-
327-
WGPUSurface CreateWGPUSurface(const WGPUInstance& instance, GLFWwindow* window)
328-
{
329-
ImGui_ImplWGPU_CreateSurfaceInfo create_info = {};
330-
create_info.Instance = instance;
331-
#if defined(GLFW_EXPOSE_NATIVE_COCOA)
332-
{
333-
create_info.System = "cocoa";
334-
create_info.RawWindow = (void*)glfwGetCocoaWindow(window);
335-
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
336-
}
337-
#elif defined(GLFW_EXPOSE_NATIVE_WAYLAND)
338-
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND)
339-
{
340-
create_info.System = "wayland";
341-
create_info.RawDisplay = (void*)glfwGetWaylandDisplay();
342-
create_info.RawSurface = (void*)glfwGetWaylandWindow(window);
343-
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
344-
}
345-
#elif defined(GLFW_EXPOSE_NATIVE_X11)
346-
if (glfwGetPlatform() == GLFW_PLATFORM_X11)
347-
{
348-
create_info.System = "x11";
349-
create_info.RawWindow = (void*)glfwGetX11Window(window);
350-
create_info.RawDisplay = (void*)glfwGetX11Display();
351-
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
352-
}
353-
#elif defined(GLFW_EXPOSE_NATIVE_WIN32)
354-
{
355-
create_info.System = "win32";
356-
create_info.RawWindow = (void*)glfwGetWin32Window(window);
357-
create_info.RawInstance = (void*)::GetModuleHandle(NULL);
358-
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
359-
}
360-
#else
361-
#error "Unsupported WebGPU native platform!"
362-
#endif
363-
return nullptr;
364-
}
365-
#endif
366-
367294
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
368295
static WGPUAdapter RequestAdapter(wgpu::Instance& instance)
369296
{
@@ -574,3 +501,76 @@ static bool InitWGPU(GLFWwindow* window)
574501

575502
return true;
576503
}
504+
505+
// GLFW helper to create a WebGPU surface, used only in WGPU-Native. DAWN-Native already has a built-in function
506+
// As of today (2025/10) there is no "official" support in GLFW to create a surface for WebGPU backend
507+
// This stub uses "low level" GLFW calls to acquire information from a specific Window Manager.
508+
// Currently supported platforms: Windows / Linux (X11 and Wayland) / MacOS. Not necessary nor available with EMSCRIPTEN.
509+
#if !defined(__EMSCRIPTEN__) && (defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) || defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN))
510+
511+
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
512+
#define GLFW_HAS_X11_OR_WAYLAND 1
513+
#else
514+
#define GLFW_HAS_X11_OR_WAYLAND 0
515+
#endif
516+
#ifdef _WIN32
517+
#undef APIENTRY
518+
#ifndef GLFW_EXPOSE_NATIVE_WIN32 // for glfwGetWin32Window()
519+
#define GLFW_EXPOSE_NATIVE_WIN32
520+
#endif
521+
#elif defined(__APPLE__)
522+
#ifndef GLFW_EXPOSE_NATIVE_COCOA // for glfwGetCocoaWindow()
523+
#define GLFW_EXPOSE_NATIVE_COCOA
524+
#endif
525+
#elif GLFW_HAS_X11_OR_WAYLAND
526+
#ifndef GLFW_EXPOSE_NATIVE_X11 // for glfwGetX11Display(), glfwGetX11Window() on Freedesktop (Linux, BSD, etc.)
527+
#define GLFW_EXPOSE_NATIVE_X11
528+
#endif
529+
#ifndef GLFW_EXPOSE_NATIVE_WAYLAND
530+
#if defined(__has_include) && __has_include(<wayland-client.h>)
531+
#define GLFW_EXPOSE_NATIVE_WAYLAND
532+
#endif
533+
#endif
534+
#endif
535+
#include <GLFW/glfw3native.h>
536+
#undef Status // X11 headers are leaking this and also 'Success', 'Always', 'None', all used in DAWN api. Add #undef if necessary.
537+
538+
WGPUSurface CreateWGPUSurface(const WGPUInstance& instance, GLFWwindow* window)
539+
{
540+
ImGui_ImplWGPU_CreateSurfaceInfo create_info = {};
541+
create_info.Instance = instance;
542+
#if defined(GLFW_EXPOSE_NATIVE_COCOA)
543+
{
544+
create_info.System = "cocoa";
545+
create_info.RawWindow = (void*)glfwGetCocoaWindow(window);
546+
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
547+
}
548+
#elif defined(GLFW_EXPOSE_NATIVE_WAYLAND)
549+
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND)
550+
{
551+
create_info.System = "wayland";
552+
create_info.RawDisplay = (void*)glfwGetWaylandDisplay();
553+
create_info.RawSurface = (void*)glfwGetWaylandWindow(window);
554+
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
555+
}
556+
#elif defined(GLFW_EXPOSE_NATIVE_X11)
557+
if (glfwGetPlatform() == GLFW_PLATFORM_X11)
558+
{
559+
create_info.System = "x11";
560+
create_info.RawWindow = (void*)glfwGetX11Window(window);
561+
create_info.RawDisplay = (void*)glfwGetX11Display();
562+
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
563+
}
564+
#elif defined(GLFW_EXPOSE_NATIVE_WIN32)
565+
{
566+
create_info.System = "win32";
567+
create_info.RawWindow = (void*)glfwGetWin32Window(window);
568+
create_info.RawInstance = (void*)::GetModuleHandle(NULL);
569+
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
570+
}
571+
#else
572+
#error "Unsupported WebGPU native platform!"
573+
#endif
574+
return nullptr;
575+
}
576+
#endif

examples/example_sdl2_wgpu/main.cpp

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "../libs/emscripten/emscripten_mainloop_stub.h"
2525
#endif
2626

27-
#include <webgpu/webgpu.h>
2827
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
2928
#include <webgpu/webgpu_cpp.h>
3029
#endif
@@ -40,6 +39,7 @@ static int wgpu_surface_height = 800;
4039

4140
// Forward declarations
4241
static bool InitWGPU(SDL_Window* window);
42+
WGPUSurface CreateWGPUSurface(const WGPUInstance& instance, SDL_Window* window);
4343

4444
static void ResizeSurface(int width, int height)
4545
{
@@ -275,60 +275,6 @@ int main(int, char**)
275275
return 0;
276276
}
277277

278-
// SDL2 helper to create a WebGPU surface (exclusively!) for Native/Desktop applications: available only together with WebGPU/WGPU backend
279-
// As of today (2025/10/31) there is no "official" support in SDL2 to create a surface for WebGPU backend.
280-
// This stub uses "low level" SDL2 calls to acquire information from a specific Window Manager.
281-
// Currently supported platforms: Windows / Linux (X11 and Wayland) / MacOS. Not necessary nor available with EMSCRIPTEN.
282-
#if !defined(__EMSCRIPTEN__) && (defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) || defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN))
283-
284-
#include <SDL_syswm.h>
285-
#undef Status // X11 headers are leaking this.
286-
#undef Success // X11 headers are leaking this.
287-
288-
WGPUSurface CreateWGPUSurface(const WGPUInstance& instance, SDL_Window* window)
289-
{
290-
SDL_SysWMinfo sysWMInfo;
291-
SDL_VERSION(&sysWMInfo.version);
292-
SDL_GetWindowWMInfo(window, &sysWMInfo);
293-
294-
ImGui_ImplWGPU_CreateSurfaceInfo create_info = {};
295-
create_info.Instance = instance;
296-
#if defined(SDL_VIDEO_DRIVER_COCOA)
297-
{
298-
create_info.System = "cocoa";
299-
create_info.RawWindow = (void*)sysWMInfo.info.cocoa.window;
300-
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
301-
}
302-
#elif defined(SDL_VIDEO_DRIVER_WAYLAND) || defined(SDL_VIDEO_DRIVER_X11)
303-
const char* sdl_driver = SDL_GetCurrentVideoDriver();
304-
if (sdl_driver && strcmp(sdl_driver, "wayland") == 0)
305-
{
306-
create_info.System = "wayland";
307-
create_info.RawDisplay = (void*)sysWMInfo.info.wl.display;
308-
create_info.RawSurface = (void*)sysWMInfo.info.wl.surface;
309-
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
310-
}
311-
else
312-
{
313-
create_info.System = "x11";
314-
create_info.RawWindow = (void*)sysWMInfo.info.x11.window;
315-
create_info.RawDisplay = (void*)sysWMInfo.info.x11.display;
316-
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
317-
}
318-
#elif defined(SDL_VIDEO_DRIVER_WINDOWS)
319-
{
320-
create_info.System = "win32";
321-
create_info.RawWindow = (void*)sysWMInfo.info.win.window;
322-
create_info.RawInstance = (void*)sysWMInfo.info.win.hinstance;
323-
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
324-
}
325-
#else
326-
#error "Unsupported WebGPU native platform!"
327-
#endif
328-
return nullptr;
329-
}
330-
#endif
331-
332278
#if defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN)
333279
static WGPUAdapter RequestAdapter(wgpu::Instance& instance)
334280
{
@@ -540,3 +486,56 @@ static bool InitWGPU(SDL_Window* window)
540486

541487
return true;
542488
}
489+
490+
// SDL2 helper to create a WebGPU surface (exclusively!) for Native/Desktop applications: available only together with WebGPU/WGPU backend
491+
// As of today (2025/10/31) there is no "official" support in SDL2 to create a surface for WebGPU backend.
492+
// This stub uses "low level" SDL2 calls to acquire information from a specific Window Manager.
493+
// Currently supported platforms: Windows / Linux (X11 and Wayland) / MacOS. Not necessary nor available with EMSCRIPTEN.
494+
#if !defined(__EMSCRIPTEN__) && (defined(IMGUI_IMPL_WEBGPU_BACKEND_WGPU) || defined(IMGUI_IMPL_WEBGPU_BACKEND_DAWN))
495+
496+
#include <SDL_syswm.h>
497+
#undef Status // X11 headers are leaking this and also 'Success', 'Always', 'None', all used in DAWN api. Add #undef if necessary.
498+
499+
WGPUSurface CreateWGPUSurface(const WGPUInstance& instance, SDL_Window* window)
500+
{
501+
SDL_SysWMinfo sysWMInfo;
502+
SDL_VERSION(&sysWMInfo.version);
503+
SDL_GetWindowWMInfo(window, &sysWMInfo);
504+
505+
ImGui_ImplWGPU_CreateSurfaceInfo create_info = {};
506+
create_info.Instance = instance;
507+
#if defined(SDL_VIDEO_DRIVER_COCOA)
508+
{
509+
create_info.System = "cocoa";
510+
create_info.RawWindow = (void*)sysWMInfo.info.cocoa.window;
511+
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
512+
}
513+
#elif defined(SDL_VIDEO_DRIVER_WAYLAND) || defined(SDL_VIDEO_DRIVER_X11)
514+
const char* sdl_driver = SDL_GetCurrentVideoDriver();
515+
if (sdl_driver && strcmp(sdl_driver, "wayland") == 0)
516+
{
517+
create_info.System = "wayland";
518+
create_info.RawDisplay = (void*)sysWMInfo.info.wl.display;
519+
create_info.RawSurface = (void*)sysWMInfo.info.wl.surface;
520+
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
521+
}
522+
else
523+
{
524+
create_info.System = "x11";
525+
create_info.RawWindow = (void*)sysWMInfo.info.x11.window;
526+
create_info.RawDisplay = (void*)sysWMInfo.info.x11.display;
527+
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
528+
}
529+
#elif defined(SDL_VIDEO_DRIVER_WINDOWS)
530+
{
531+
create_info.System = "win32";
532+
create_info.RawWindow = (void*)sysWMInfo.info.win.window;
533+
create_info.RawInstance = (void*)sysWMInfo.info.win.hinstance;
534+
return ImGui_ImplWGPU_CreateWGPUSurfaceHelper(&create_info);
535+
}
536+
#else
537+
#error "Unsupported WebGPU native platform!"
538+
#endif
539+
return nullptr;
540+
}
541+
#endif

0 commit comments

Comments
 (0)