From 44f8a80ade160087d9cb4341467683170f60acf1 Mon Sep 17 00:00:00 2001 From: Sebastian Emanuel Dawid Date: Sat, 16 Aug 2025 00:46:54 +0200 Subject: [PATCH] Add WebGPU support for MacOS --- src/cocoa_init.m | 3 ++- src/cocoa_platform.h | 2 ++ src/cocoa_window.m | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/cocoa_init.m b/src/cocoa_init.m index 15dc4ec4..0df7a330 100644 --- a/src/cocoa_init.m +++ b/src/cocoa_init.m @@ -564,7 +564,8 @@ GLFWbool _glfwConnectCocoa(int platformID, _GLFWplatform* platform) .getEGLNativeWindow = _glfwGetEGLNativeWindowCocoa, .getRequiredInstanceExtensions = _glfwGetRequiredInstanceExtensionsCocoa, .getPhysicalDevicePresentationSupport = _glfwGetPhysicalDevicePresentationSupportCocoa, - .createWindowSurface = _glfwCreateWindowSurfaceCocoa + .createWindowSurface = _glfwCreateWindowSurfaceCocoa, + .createWindowWGPUSurface = _glfwCreateWindowWGPUSurfaceCocoa }; *platform = cocoa; diff --git a/src/cocoa_platform.h b/src/cocoa_platform.h index 4d1d66ae..f7fa7b6f 100644 --- a/src/cocoa_platform.h +++ b/src/cocoa_platform.h @@ -276,6 +276,8 @@ void _glfwGetRequiredInstanceExtensionsCocoa(char** extensions); GLFWbool _glfwGetPhysicalDevicePresentationSupportCocoa(VkInstance instance, VkPhysicalDevice device, uint32_t queuefamily); VkResult _glfwCreateWindowSurfaceCocoa(VkInstance instance, _GLFWwindow* window, const VkAllocationCallbacks* allocator, VkSurfaceKHR* surface); +WGPUSurface _glfwCreateWindowWGPUSurfaceCocoa(WGPUInstance instance, _GLFWwindow* window); + void _glfwFreeMonitorCocoa(_GLFWmonitor* monitor); void _glfwGetMonitorPosCocoa(_GLFWmonitor* monitor, int* xpos, int* ypos); void _glfwGetMonitorContentScaleCocoa(_GLFWmonitor* monitor, float* xscale, float* yscale); diff --git a/src/cocoa_window.m b/src/cocoa_window.m index e69b5fe0..9b5d1bc5 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -2020,6 +2020,26 @@ VkResult _glfwCreateWindowSurfaceCocoa(VkInstance instance, } // autoreleasepool } +typedef struct WGPUSurfaceSourceMetalLayer { + WGPUChainedStruct chain; + void * layer; +} WGPUSurfaceSourceMetalLayer; + +WGPUSurface _glfwCreateWindowWGPUSurfaceCocoa(WGPUInstance instance, _GLFWwindow* window) { + [window->ns.view setLayer:window->ns.layer]; + [window->ns.view setWantsLayer:YES]; + + WGPUSurfaceSourceMetalLayer metalSurface; + metalSurface.chain.next = NULL; + metalSurface.chain.sType = WGPUSType_SurfaceSourceMetalLayer; + metalSurface.layer = window->ns.layer; + + WGPUSurfaceDescriptor surfaceDescriptor; + surfaceDescriptor.nextInChain = &metalSurface.chain; + surfaceDescriptor.label = (WGPUStringView){ NULL, SIZE_MAX }; + + return wgpuInstanceCreateSurface(instance, &surfaceDescriptor); +} ////////////////////////////////////////////////////////////////////////// ////// GLFW native API //////