@@ -40,7 +40,8 @@ static int                      wgpu_surface_width = 1280;
4040static  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
4546static  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)
368295static  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 
0 commit comments