Added native support for creating WebGPU surfaces

Note that this commit only adds support when running on Wayland.
This commit is contained in:
Sebastian Emanuel Dawid 2025-08-15 23:01:43 +02:00
parent 768e81a0eb
commit 2f44430536
6 changed files with 162 additions and 4 deletions

View File

@ -115,6 +115,10 @@ extern "C" {
* VK_USE_PLATFORM_WIN32_KHR) so we offer our replacement symbols after it.
*/
#if defined(GLFW_INCLUDE_WEBGPU)
#include <webgpu/webgpu.h>
#endif /* WebGPU header */
/* It is customary to use APIENTRY for OpenGL function pointer declarations on
* all platforms. Additionally, the Windows OpenGL header needs APIENTRY.
*/
@ -6525,6 +6529,41 @@ GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance, GLFWwindow* window
#endif /*VK_VERSION_1_0*/
#if defined(WEBGPU_H_)
/*! @brief Provide the address of the `wgpuInstanceCreateSurface` function to GLFW.
*
* This function passes the address provided for the `wgpuInstanceCreateSurface` function
* to GLFW.
*
* @param[in] addr The address of the `wgpuInstanceCreateSurface` function.
*
* @since Added in version 3.5
*
* @ingroup webgpu
*/
GLFWAPI void glfwSetWGPUInstanceCreateSurfaceAddr(WGPUSurface (*addr)(WGPUInstance, const WGPUSurfaceDescriptor*));
/*! @brief Creates a WebGPU surface for the specified window.
*
* This function creates a WebGPU surface for the specified window.
*
* If the surface could not be created this function returns `NULL`.
*
* It is the callers responsibility to destroy the surface. The surface
* must be destroyed using `wgpuSurfaceRelease`.
*
* @param[in] instance The WebGPU instance to create the surface in.
* @param[in] window The window to create the surface for.
* @return The handle of the surface. This is `NULL` if an error occurred.
*
* @since Added in version 3.5
*
* @ingroup webgpu
*/
GLFWAPI WGPUSurface glfwCreateWindowWGPUSurface(WGPUInstance instance, GLFWwindow* window);
#endif /*WEBGPU_H_*/
/*************************************************************************
* Global definition cleanup

View File

@ -3,9 +3,10 @@ add_library(glfw ${GLFW_LIBRARY_TYPE}
"${GLFW_SOURCE_DIR}/include/GLFW/glfw3.h"
"${GLFW_SOURCE_DIR}/include/GLFW/glfw3native.h"
internal.h platform.h mappings.h
context.c init.c input.c monitor.c platform.c vulkan.c window.c
egl_context.c osmesa_context.c null_platform.h null_joystick.h
null_init.c null_monitor.c null_window.c null_joystick.c)
context.c init.c input.c monitor.c platform.c vulkan.c webgpu.c
window.c egl_context.c osmesa_context.c null_platform.h
null_joystick.h null_init.c null_monitor.c null_window.c
null_joystick.c)
# The time, thread and module code is shared between all backends on a given OS,
# including the null backend, which still needs those bits to be functional
@ -128,7 +129,8 @@ set_target_properties(glfw PROPERTIES
C_STANDARD 99
C_EXTENSIONS OFF
DEFINE_SYMBOL _GLFW_BUILD_DLL
FOLDER "GLFW3")
FOLDER "GLFW3"
EXPORT_COMPILE_COMMANDS ON)
target_include_directories(glfw PUBLIC
"$<BUILD_INTERFACE:${GLFW_SOURCE_DIR}/include>"

View File

@ -331,6 +331,42 @@ typedef PFN_vkVoidFunction (APIENTRY * PFN_vkGetInstanceProcAddr)(VkInstance,con
typedef VkResult (APIENTRY * PFN_vkEnumerateInstanceExtensionProperties)(const char*,uint32_t*,VkExtensionProperties*);
#define vkGetInstanceProcAddr _glfw.vk.GetInstanceProcAddr
// forward declare WebGPU types
typedef struct WGPUInstanceImpl* WGPUInstance;
typedef struct WGPUSurfaceImpl* WGPUSurface;
typedef struct WGPUStringView {
char const * data;
size_t length;
} WGPUStringView;
typedef enum WGPUSType {
WGPUSType_ShaderSourceSPIRV = 0x00000001,
WGPUSType_ShaderSourceWGSL = 0x00000002,
WGPUSType_RenderPassMaxDrawCount = 0x00000003,
WGPUSType_SurfaceSourceMetalLayer = 0x00000004,
WGPUSType_SurfaceSourceWindowsHWND = 0x00000005,
WGPUSType_SurfaceSourceXlibWindow = 0x00000006,
WGPUSType_SurfaceSourceWaylandSurface = 0x00000007,
WGPUSType_SurfaceSourceAndroidNativeWindow = 0x00000008,
WGPUSType_SurfaceSourceXCBWindow = 0x00000009,
WGPUSType_Force32 = 0x7FFFFFFF
} WGPUSType;
typedef struct WGPUChainedStruct {
struct WGPUChainedStruct const * next;
WGPUSType sType;
} WGPUChainedStruct;
typedef struct WGPUSurfaceDescriptor {
WGPUChainedStruct const * nextInChain;
WGPUStringView label;
} WGPUSurfaceDescriptor;
typedef WGPUSurface (*PFN_wgpuInstanceCreateSurface)(WGPUInstance, const WGPUSurfaceDescriptor*);
#define wgpuInstanceCreateSurface _glfw.wgpu.instanceCreateSurface
#include "platform.h"
#define GLFW_NATIVE_INCLUDE_NONE
@ -759,6 +795,8 @@ struct _GLFWplatform
void (*getRequiredInstanceExtensions)(char**);
GLFWbool (*getPhysicalDevicePresentationSupport)(VkInstance,VkPhysicalDevice,uint32_t);
VkResult (*createWindowSurface)(VkInstance,_GLFWwindow*,const VkAllocationCallbacks*,VkSurfaceKHR*);
// webgpu
WGPUSurface (*createWindowWGPUSurface)(WGPUInstance, _GLFWwindow*);
};
// Library global data
@ -875,6 +913,10 @@ struct _GLFWlibrary
GLFWbool EXT_headless_surface;
} vk;
struct {
PFN_wgpuInstanceCreateSurface instanceCreateSurface;
} wgpu;
struct {
GLFWmonitorfun monitor;
GLFWjoystickfun joystick;

54
src/webgpu.c Normal file
View File

@ -0,0 +1,54 @@
//========================================================================
// GLFW 3.5 - www.glfw.org
//------------------------------------------------------------------------
// Copyright (c) Sebastian Dawid <sdawid@techfak.uni-bielefeld.de>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#include "internal.h"
#include <assert.h>
#include <stdlib.h>
GLFWAPI void glfwSetWGPUInstanceCreateSurfaceAddr(WGPUSurface (*addr)(WGPUInstance, const WGPUSurfaceDescriptor*)) {
_GLFW_REQUIRE_INIT()
_glfw.wgpu.instanceCreateSurface = addr;
}
GLFWAPI WGPUSurface glfwCreateWindowWGPUSurface(WGPUInstance instance, GLFWwindow* handle) {
_GLFW_REQUIRE_INIT_OR_RETURN(NULL)
_GLFWwindow* window = (_GLFWwindow*)handle;
assert(window != NULL);
assert(instance != NULL);
assert(_glfw.wgpu.instanceCreateSurface != NULL);
if (window->context.client != GLFW_NO_API)
{
_glfwInputError(GLFW_INVALID_VALUE,
"WebGPU: Window surface creation requires the window to have the client API set to GLFW_NO_API");
return NULL;
};
return _glfw.platform.createWindowWGPUSurface(instance, window);
}

View File

@ -678,6 +678,8 @@ void _glfwGetRequiredInstanceExtensionsWayland(char** extensions);
GLFWbool _glfwGetPhysicalDevicePresentationSupportWayland(VkInstance instance, VkPhysicalDevice device, uint32_t queuefamily);
VkResult _glfwCreateWindowSurfaceWayland(VkInstance instance, _GLFWwindow* window, const VkAllocationCallbacks* allocator, VkSurfaceKHR* surface);
WGPUSurface _glfwCreateWindowWGPUSurfaceWayland(WGPUInstance instance, _GLFWwindow* window);
void _glfwFreeMonitorWayland(_GLFWmonitor* monitor);
void _glfwGetMonitorPosWayland(_GLFWmonitor* monitor, int* xpos, int* ypos);
void _glfwGetMonitorContentScaleWayland(_GLFWmonitor* monitor, float* xscale, float* yscale);

View File

@ -3326,6 +3326,25 @@ VkResult _glfwCreateWindowSurfaceWayland(VkInstance instance,
return err;
}
typedef struct WGPUSurfaceSourceWaylandSurface {
WGPUChainedStruct chain;
void * display;
void * surface;
} WGPUSurfaceSourceWaylandSurface;
WGPUSurface _glfwCreateWindowWGPUSurfaceWayland(WGPUInstance instance, _GLFWwindow* window) {
WGPUSurfaceSourceWaylandSurface waylandSurface;
waylandSurface.chain.sType = WGPUSType_SurfaceSourceWaylandSurface;
waylandSurface.chain.next = NULL;
waylandSurface.surface = window->wl.surface;
waylandSurface.display = _glfw.wl.display;
WGPUSurfaceDescriptor surfaceDescriptor;
surfaceDescriptor.nextInChain = &waylandSurface.chain;
surfaceDescriptor.label = (WGPUStringView){ NULL, SIZE_MAX };
return wgpuInstanceCreateSurface(instance, &surfaceDescriptor);
}
//////////////////////////////////////////////////////////////////////////
////// GLFW native API //////