diff --git a/src/video/x11/SDL_x11video.c b/src/video/x11/SDL_x11video.c index 1ca7e021d3..029a345699 100644 --- a/src/video/x11/SDL_x11video.c +++ b/src/video/x11/SDL_x11video.c @@ -41,6 +41,7 @@ #include "SDL_x11shape.h" #include "SDL_x11xsync.h" #include "SDL_x11xtest.h" +#include "SDL_x11wgpu.h" #ifdef SDL_VIDEO_OPENGL_EGL #include "SDL_x11opengles.h" @@ -268,6 +269,10 @@ static SDL_VideoDevice *X11_CreateDevice(void) device->Vulkan_GetPresentationSupport = X11_Vulkan_GetPresentationSupport; #endif +#ifdef SDL_VIDEO_WGPU + device->WGPU_CreateSurface = X11_WGPU_CreateSurface; +#endif + #ifdef SDL_USE_LIBDBUS if (SDL_SystemTheme_Init()) device->system_theme = SDL_SystemTheme_Get(); diff --git a/src/video/x11/SDL_x11wgpu.c b/src/video/x11/SDL_x11wgpu.c new file mode 100644 index 0000000000..180df63668 --- /dev/null +++ b/src/video/x11/SDL_x11wgpu.c @@ -0,0 +1,52 @@ +#include "SDL_internal.h" + +#if defined(SDL_VIDEO_WGPU) && defined(SDL_VIDEO_DRIVER_X11) +#include "SDL_x11video.h" + +#include "SDL_x11wgpu.h" + +SDL_ELF_NOTE_DLOPEN( + "x11-wgpu", + "Support for for wgpu-native on X11 backend", + SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED, + "libwgpu_native.so") + +WGPUSurface X11_WGPU_CreateSurface(SDL_VideoDevice *_this, SDL_Window *window, WGPUInstance instance) +{ + SDL_PropertiesID windowProperties = SDL_GetWindowProperties(window); + + Display *x11_display = SDL_GetPointerProperty(windowProperties, SDL_PROP_WINDOW_X11_DISPLAY_POINTER, 0); + Window x11_window = SDL_GetNumberProperty(windowProperties, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0); + WGPUSurfaceDescriptor desc; + WGPUSurfaceSourceXlibWindow source; + + source.window = x11_window; + source.display = x11_display; + source.chain.sType = WGPUSType_SurfaceSourceXlibWindow; + source.chain.next = NULL; + + desc.label = (WGPUStringView){ NULL, WGPU_STRLEN }; + desc.nextInChain = &source.chain; + + SDL_SharedObject *wgpuLib = SDL_LoadObject("libwgpu_native.so"); + + if (wgpuLib == NULL) { + SDL_SetError("Failed to load wgpu-native shared library 'libwgpu_native.so'"); + goto fail; + } + + WGPUProcInstanceCreateSurface proc = (WGPUProcInstanceCreateSurface)SDL_LoadFunction(wgpuLib, "wgpuInstanceCreateSurface"); + + if (proc == NULL) { + SDL_SetError("Failed to load function 'wgpuInstanceCreateSurface' from loaded wgpu-native library!"); + goto fail; + } + + return proc(instance, &desc); + +fail: + SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Failed to create WGPU surface: %s", SDL_GetError()); + return NULL; +} + +#endif diff --git a/src/video/x11/SDL_x11wgpu.h b/src/video/x11/SDL_x11wgpu.h new file mode 100644 index 0000000000..1dea53eaf3 --- /dev/null +++ b/src/video/x11/SDL_x11wgpu.h @@ -0,0 +1,13 @@ +#ifndef SDL_x11wgpu_h_ +#define SDL_x11wgpu_h_ + +// NOTE: Again, Not sure if I'm allowed to do this. +#include "../webgpu/webgpu.h" + +#include + +#if defined(SDL_VIDEO_WGPU) && defined(SDL_VIDEO_DRIVER_X11) +extern WGPUSurface X11_WGPU_CreateSurface(SDL_VideoDevice *_this, SDL_Window *window, WGPUInstance instance); +#endif + +#endif // SDL_x11wgpu_h_