From 5a079a2d114f96d4847d1ee305d5b7c16eeec50e Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 27 Dec 2025 12:03:39 -0800 Subject: Initial commit --- .../src/video/offscreen/SDL_offscreenevents.c | 38 +++ .../src/video/offscreen/SDL_offscreenevents_c.h | 23 ++ .../src/video/offscreen/SDL_offscreenframebuffer.c | 79 +++++++ .../video/offscreen/SDL_offscreenframebuffer_c.h | 25 ++ .../src/video/offscreen/SDL_offscreenopengles.c | 82 +++++++ .../src/video/offscreen/SDL_offscreenopengles.h | 44 ++++ .../src/video/offscreen/SDL_offscreenvideo.c | 151 ++++++++++++ .../src/video/offscreen/SDL_offscreenvideo.h | 28 +++ .../src/video/offscreen/SDL_offscreenvulkan.c | 261 +++++++++++++++++++++ .../src/video/offscreen/SDL_offscreenvulkan.h | 38 +++ .../src/video/offscreen/SDL_offscreenwindow.c | 90 +++++++ .../src/video/offscreen/SDL_offscreenwindow.h | 40 ++++ 12 files changed, 899 insertions(+) create mode 100644 contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenevents.c create mode 100644 contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenevents_c.h create mode 100644 contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenframebuffer.c create mode 100644 contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenframebuffer_c.h create mode 100644 contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenopengles.c create mode 100644 contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenopengles.h create mode 100644 contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvideo.c create mode 100644 contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvideo.h create mode 100644 contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvulkan.c create mode 100644 contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvulkan.h create mode 100644 contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenwindow.c create mode 100644 contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenwindow.h (limited to 'contrib/SDL-3.2.8/src/video/offscreen') diff --git a/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenevents.c b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenevents.c new file mode 100644 index 0000000..256d1a2 --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenevents.c @@ -0,0 +1,38 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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 "SDL_internal.h" + +#ifdef SDL_VIDEO_DRIVER_OFFSCREEN + +/* Being a offscreen driver, there's no event stream. We just define stubs for + most of the API. */ + +#include "../../events/SDL_events_c.h" + +#include "SDL_offscreenvideo.h" +#include "SDL_offscreenevents_c.h" + +void OFFSCREEN_PumpEvents(SDL_VideoDevice *_this) +{ + // do nothing. +} + +#endif // SDL_VIDEO_DRIVER_OFFSCREEN diff --git a/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenevents_c.h b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenevents_c.h new file mode 100644 index 0000000..c06213c --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenevents_c.h @@ -0,0 +1,23 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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 "SDL_internal.h" + +extern void OFFSCREEN_PumpEvents(SDL_VideoDevice *_this); diff --git a/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenframebuffer.c b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenframebuffer.c new file mode 100644 index 0000000..e7f6103 --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenframebuffer.c @@ -0,0 +1,79 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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 "SDL_internal.h" + +#ifdef SDL_VIDEO_DRIVER_OFFSCREEN + +#include "../SDL_sysvideo.h" +#include "../../SDL_properties_c.h" +#include "SDL_offscreenframebuffer_c.h" + +#define OFFSCREEN_SURFACE "SDL.internal.window.surface" + + +bool SDL_OFFSCREEN_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format, void **pixels, int *pitch) +{ + SDL_Surface *surface; + const SDL_PixelFormat surface_format = SDL_PIXELFORMAT_XRGB8888; + int w, h; + + // Create a new framebuffer + SDL_GetWindowSizeInPixels(window, &w, &h); + surface = SDL_CreateSurface(w, h, surface_format); + if (!surface) { + return false; + } + + // Save the info and return! + SDL_SetSurfaceProperty(SDL_GetWindowProperties(window), OFFSCREEN_SURFACE, surface); + *format = surface_format; + *pixels = surface->pixels; + *pitch = surface->pitch; + + return true; +} + +bool SDL_OFFSCREEN_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects) +{ + static int frame_number; + SDL_Surface *surface; + + surface = (SDL_Surface *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), OFFSCREEN_SURFACE, NULL); + if (!surface) { + return SDL_SetError("Couldn't find offscreen surface for window"); + } + + // Send the data to the display + if (SDL_GetHintBoolean(SDL_HINT_VIDEO_OFFSCREEN_SAVE_FRAMES, false)) { + char file[128]; + (void)SDL_snprintf(file, sizeof(file), "SDL_window%" SDL_PRIu32 "-%8.8d.bmp", + SDL_GetWindowID(window), ++frame_number); + SDL_SaveBMP(surface, file); + } + return true; +} + +void SDL_OFFSCREEN_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window) +{ + SDL_ClearProperty(SDL_GetWindowProperties(window), OFFSCREEN_SURFACE); +} + +#endif // SDL_VIDEO_DRIVER_OFFSCREEN diff --git a/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenframebuffer_c.h b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenframebuffer_c.h new file mode 100644 index 0000000..ff39ad6 --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenframebuffer_c.h @@ -0,0 +1,25 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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 "SDL_internal.h" + +extern bool SDL_OFFSCREEN_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format, void **pixels, int *pitch); +extern bool SDL_OFFSCREEN_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects); +extern void SDL_OFFSCREEN_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window); diff --git a/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenopengles.c b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenopengles.c new file mode 100644 index 0000000..87eed0e --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenopengles.c @@ -0,0 +1,82 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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 "SDL_internal.h" + +#if defined(SDL_VIDEO_DRIVER_OFFSCREEN) && defined(SDL_VIDEO_OPENGL_EGL) + +#include "SDL_offscreenopengles.h" +#include "SDL_offscreenvideo.h" +#include "SDL_offscreenwindow.h" + +// EGL implementation of SDL OpenGL support + +bool OFFSCREEN_GLES_LoadLibrary(SDL_VideoDevice *_this, const char *path) +{ + if (!SDL_EGL_LoadLibraryOnly(_this, path)) { + return false; + } + + /* driver_loaded gets incremented by SDL_GL_LoadLibrary when we return, + but SDL_EGL_InitializeOffscreen checks that we're loaded before then, + so temporarily bump it since we know that LoadLibraryOnly succeeded. */ + bool result; + _this->gl_config.driver_loaded++; + result = SDL_EGL_InitializeOffscreen(_this, 0); + _this->gl_config.driver_loaded--; + if (!result) { + return false; + } + + if (!SDL_EGL_ChooseConfig(_this)) { + return false; + } + + return true; +} + +SDL_GLContext OFFSCREEN_GLES_CreateContext(SDL_VideoDevice *_this, SDL_Window *window) +{ + SDL_WindowData *offscreen_window = window->internal; + + SDL_GLContext context; + context = SDL_EGL_CreateContext(_this, offscreen_window->egl_surface); + + return context; +} + +bool OFFSCREEN_GLES_MakeCurrent(SDL_VideoDevice *_this, SDL_Window *window, SDL_GLContext context) +{ + if (window) { + EGLSurface egl_surface = window->internal->egl_surface; + return SDL_EGL_MakeCurrent(_this, egl_surface, context); + } else { + return SDL_EGL_MakeCurrent(_this, NULL, NULL); + } +} + +bool OFFSCREEN_GLES_SwapWindow(SDL_VideoDevice *_this, SDL_Window *window) +{ + SDL_WindowData *offscreen_wind = window->internal; + + return SDL_EGL_SwapBuffers(_this, offscreen_wind->egl_surface); +} + +#endif // SDL_VIDEO_DRIVER_OFFSCREEN && SDL_VIDEO_OPENGL_EGL diff --git a/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenopengles.h b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenopengles.h new file mode 100644 index 0000000..d6dfc0a --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenopengles.h @@ -0,0 +1,44 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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 "SDL_internal.h" + +#ifndef SDL_offscreenopengles_h +#define SDL_offscreenopengles_h + +#if defined(SDL_VIDEO_DRIVER_OFFSCREEN) && defined(SDL_VIDEO_OPENGL_EGL) + +#include "../SDL_sysvideo.h" +#include "../SDL_egl_c.h" + +#define OFFSCREEN_GLES_GetProcAddress SDL_EGL_GetProcAddressInternal +#define OFFSCREEN_GLES_UnloadLibrary SDL_EGL_UnloadLibrary +#define OFFSCREEN_GLES_GetSwapInterval SDL_EGL_GetSwapInterval +#define OFFSCREEN_GLES_SetSwapInterval SDL_EGL_SetSwapInterval +#define OFFSCREEN_GLES_DestroyContext SDL_EGL_DestroyContext + +extern bool OFFSCREEN_GLES_LoadLibrary(SDL_VideoDevice *_this, const char *path); +extern SDL_GLContext OFFSCREEN_GLES_CreateContext(SDL_VideoDevice *_this, SDL_Window *window); +extern bool OFFSCREEN_GLES_MakeCurrent(SDL_VideoDevice *_this, SDL_Window *window, SDL_GLContext context); +extern bool OFFSCREEN_GLES_SwapWindow(SDL_VideoDevice *_this, SDL_Window *window); + +#endif // SDL_VIDEO_DRIVER_OFFSCREEN && SDL_VIDEO_OPENGL_EGL + +#endif // SDL_offscreenopengles_h diff --git a/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvideo.c b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvideo.c new file mode 100644 index 0000000..1ef0f9e --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvideo.c @@ -0,0 +1,151 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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 "SDL_internal.h" + +#ifdef SDL_VIDEO_DRIVER_OFFSCREEN + +/* Offscreen video driver is similar to dummy driver, however its purpose + * is enabling applications to use some of the SDL video functionality + * (notably context creation) while not requiring a display output. + * + * An example would be running a graphical program on a headless box + * for automated testing. + */ + +#include "SDL_offscreenvideo.h" +#include "SDL_offscreenevents_c.h" +#include "SDL_offscreenframebuffer_c.h" +#include "SDL_offscreenopengles.h" +#include "SDL_offscreenvulkan.h" +#include "SDL_offscreenwindow.h" + +#define OFFSCREENVID_DRIVER_NAME "offscreen" + +// Initialization/Query functions +static bool OFFSCREEN_VideoInit(SDL_VideoDevice *_this); +static bool OFFSCREEN_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode); +static void OFFSCREEN_VideoQuit(SDL_VideoDevice *_this); + +// OFFSCREEN driver bootstrap functions + +static void OFFSCREEN_DeleteDevice(SDL_VideoDevice *device) +{ + SDL_free(device); +} + +static bool OFFSCREEN_Available(const char *enable_hint) +{ + const char *hint = SDL_GetHint(SDL_HINT_VIDEO_DRIVER); + if (hint) { + if (SDL_strcmp(hint, enable_hint) == 0) { + return true; + } + } + return false; +} + +static SDL_VideoDevice *OFFSCREEN_CreateDevice(void) +{ + SDL_VideoDevice *device; + + if (!OFFSCREEN_Available(OFFSCREENVID_DRIVER_NAME)) { + return NULL; + } + + // Initialize all variables that we clean on shutdown + device = (SDL_VideoDevice *)SDL_calloc(1, sizeof(SDL_VideoDevice)); + if (!device) { + return NULL; + } + + // General video + device->VideoInit = OFFSCREEN_VideoInit; + device->VideoQuit = OFFSCREEN_VideoQuit; + device->SetDisplayMode = OFFSCREEN_SetDisplayMode; + device->PumpEvents = OFFSCREEN_PumpEvents; + device->CreateWindowFramebuffer = SDL_OFFSCREEN_CreateWindowFramebuffer; + device->UpdateWindowFramebuffer = SDL_OFFSCREEN_UpdateWindowFramebuffer; + device->DestroyWindowFramebuffer = SDL_OFFSCREEN_DestroyWindowFramebuffer; + device->free = OFFSCREEN_DeleteDevice; + +#ifdef SDL_VIDEO_OPENGL_EGL + // GL context + device->GL_SwapWindow = OFFSCREEN_GLES_SwapWindow; + device->GL_MakeCurrent = OFFSCREEN_GLES_MakeCurrent; + device->GL_CreateContext = OFFSCREEN_GLES_CreateContext; + device->GL_DestroyContext = OFFSCREEN_GLES_DestroyContext; + device->GL_LoadLibrary = OFFSCREEN_GLES_LoadLibrary; + device->GL_UnloadLibrary = OFFSCREEN_GLES_UnloadLibrary; + device->GL_GetProcAddress = OFFSCREEN_GLES_GetProcAddress; + device->GL_GetSwapInterval = OFFSCREEN_GLES_GetSwapInterval; + device->GL_SetSwapInterval = OFFSCREEN_GLES_SetSwapInterval; +#endif + +#ifdef SDL_VIDEO_VULKAN + device->Vulkan_LoadLibrary = OFFSCREEN_Vulkan_LoadLibrary; + device->Vulkan_UnloadLibrary = OFFSCREEN_Vulkan_UnloadLibrary; + device->Vulkan_GetInstanceExtensions = OFFSCREEN_Vulkan_GetInstanceExtensions; + device->Vulkan_CreateSurface = OFFSCREEN_Vulkan_CreateSurface; + device->Vulkan_DestroySurface = OFFSCREEN_Vulkan_DestroySurface; +#endif + + // "Window" + device->CreateSDLWindow = OFFSCREEN_CreateWindow; + device->DestroyWindow = OFFSCREEN_DestroyWindow; + device->SetWindowSize = OFFSCREEN_SetWindowSize; + + return device; +} + +VideoBootStrap OFFSCREEN_bootstrap = { + OFFSCREENVID_DRIVER_NAME, "SDL offscreen video driver", + OFFSCREEN_CreateDevice, + NULL, // no ShowMessageBox implementation + false +}; + +static bool OFFSCREEN_VideoInit(SDL_VideoDevice *_this) +{ + SDL_DisplayMode mode; + + // Use a fake 32-bpp desktop mode + SDL_zero(mode); + mode.format = SDL_PIXELFORMAT_XRGB8888; + mode.w = 1024; + mode.h = 768; + if (SDL_AddBasicVideoDisplay(&mode) == 0) { + return false; + } + + // We're done! + return true; +} + +static bool OFFSCREEN_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode) +{ + return true; +} + +void OFFSCREEN_VideoQuit(SDL_VideoDevice *_this) +{ +} + +#endif // SDL_VIDEO_DRIVER_OFFSCREEN diff --git a/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvideo.h b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvideo.h new file mode 100644 index 0000000..f39d8e7 --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvideo.h @@ -0,0 +1,28 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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 "SDL_internal.h" + +#ifndef SDL_offscreenvideo_h +#define SDL_offscreenvideo_h + +#include "../SDL_sysvideo.h" + +#endif // SDL_offscreenvideo_h diff --git a/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvulkan.c b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvulkan.c new file mode 100644 index 0000000..b3dca4a --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvulkan.c @@ -0,0 +1,261 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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 "SDL_internal.h" + +#if defined(SDL_VIDEO_VULKAN) && defined(SDL_VIDEO_DRIVER_OFFSCREEN) + +#include "../SDL_vulkan_internal.h" +#include "../SDL_sysvideo.h" + + +static const char *s_defaultPaths[] = { +#if defined(SDL_PLATFORM_WINDOWS) + "vulkan-1.dll" +#elif defined(SDL_PLATFORM_APPLE) + "vulkan.framework/vulkan", + "libvulkan.1.dylib", + "libvulkan.dylib", + "MoltenVK.framework/MoltenVK", + "libMoltenVK.dylib" +#elif defined(SDL_PLATFORM_OPENBSD) + "libvulkan.so" +#else + "libvulkan.so.1" +#endif +}; + +#if defined( SDL_PLATFORM_APPLE ) +#include + +// Since libSDL is most likely a .dylib, need RTLD_DEFAULT not RTLD_SELF. +#define DEFAULT_HANDLE RTLD_DEFAULT +#endif + +/*Should the whole driver fail if it can't create a surface? Rendering to an offscreen buffer is still possible without a surface. + At the time of writing. I need the driver to minimally work even if the surface extension isn't present. + And account for the inability to create a surface on the consumer side. + So for now I'm targeting my specific use case -Dave Kircher*/ +#define HEADLESS_SURFACE_EXTENSION_REQUIRED_TO_LOAD 0 + + +bool OFFSCREEN_Vulkan_LoadLibrary(SDL_VideoDevice *_this, const char *path) +{ + VkExtensionProperties *extensions = NULL; + Uint32 extensionCount = 0; + bool hasSurfaceExtension = false; + bool hasHeadlessSurfaceExtension = false; + PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL; + Uint32 i; + const char **paths; + const char *foundPath = NULL; + Uint32 numPaths; + + if (_this->vulkan_config.loader_handle) { + return SDL_SetError("Vulkan already loaded"); + } + + // Load the Vulkan loader library + if (!path) { + path = SDL_GetHint(SDL_HINT_VULKAN_LIBRARY); + } + +#if defined(SDL_PLATFORM_APPLE) + if (!path) { + // Handle the case where Vulkan Portability is linked statically. + vkGetInstanceProcAddr = + (PFN_vkGetInstanceProcAddr)dlsym(DEFAULT_HANDLE, + "vkGetInstanceProcAddr"); + } + + if (vkGetInstanceProcAddr) { + _this->vulkan_config.loader_handle = DEFAULT_HANDLE; + } else +#endif + { + if (path) { + paths = &path; + numPaths = 1; + } else { + paths = s_defaultPaths; + numPaths = SDL_arraysize(s_defaultPaths); + } + + for (i = 0; i < numPaths && _this->vulkan_config.loader_handle == NULL; i++) { + foundPath = paths[i]; + _this->vulkan_config.loader_handle = SDL_LoadObject(foundPath); + } + + if (_this->vulkan_config.loader_handle == NULL) { + return SDL_SetError("Failed to load Vulkan Portability library"); + } + + SDL_strlcpy(_this->vulkan_config.loader_path, foundPath, + SDL_arraysize(_this->vulkan_config.loader_path)); + vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction( + _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr"); + + if (!vkGetInstanceProcAddr) { + SDL_SetError("Failed to load vkGetInstanceProcAddr from Vulkan Portability library"); + goto fail; + } + } + + _this->vulkan_config.vkGetInstanceProcAddr = (SDL_FunctionPointer)vkGetInstanceProcAddr; + _this->vulkan_config.vkEnumerateInstanceExtensionProperties = + (SDL_FunctionPointer)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)( + VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties"); + if (!_this->vulkan_config.vkEnumerateInstanceExtensionProperties) { + goto fail; + } + extensions = SDL_Vulkan_CreateInstanceExtensionsList( + (PFN_vkEnumerateInstanceExtensionProperties) + _this->vulkan_config.vkEnumerateInstanceExtensionProperties, + &extensionCount); + if (!extensions) { + goto fail; + } + for (i = 0; i < extensionCount; i++) { + if (SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) { + hasSurfaceExtension = true; + } else if (SDL_strcmp(VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) { + hasHeadlessSurfaceExtension = true; + } + } + SDL_free(extensions); + if (!hasSurfaceExtension) { + SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_SURFACE_EXTENSION_NAME " extension"); + goto fail; + } + if (!hasHeadlessSurfaceExtension) { +#if (HEADLESS_SURFACE_EXTENSION_REQUIRED_TO_LOAD != 0) + SDL_SetError("Installed Vulkan doesn't implement the " VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME " extension"); + goto fail; +#else + // Let's at least leave a breadcrumb for people to find if they have issues + SDL_Log("Installed Vulkan doesn't implement the " VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME " extension"); +#endif + } + return true; + +fail: + SDL_UnloadObject(_this->vulkan_config.loader_handle); + _this->vulkan_config.loader_handle = NULL; + return false; +} + +void OFFSCREEN_Vulkan_UnloadLibrary(SDL_VideoDevice *_this) +{ + if (_this->vulkan_config.loader_handle) { + SDL_UnloadObject(_this->vulkan_config.loader_handle); + _this->vulkan_config.loader_handle = NULL; + } +} + +char const *const *OFFSCREEN_Vulkan_GetInstanceExtensions(SDL_VideoDevice *_this, + Uint32 *count) +{ +#if (HEADLESS_SURFACE_EXTENSION_REQUIRED_TO_LOAD == 0) + VkExtensionProperties *enumerateExtensions = NULL; + Uint32 enumerateExtensionCount = 0; + bool hasHeadlessSurfaceExtension = false; + Uint32 i; +#endif + + static const char *const returnExtensions[] = { VK_KHR_SURFACE_EXTENSION_NAME, VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME }; + if (count) { +# if (HEADLESS_SURFACE_EXTENSION_REQUIRED_TO_LOAD == 0) + { + /* In optional mode, only return VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME if it's already supported by the instance + There's probably a better way to cache the presence of the extension during OFFSCREEN_Vulkan_LoadLibrary(). + But both SDL_VideoData and SDL_VideoDevice::vulkan_config seem like I'd need to touch a bunch of code to do properly. + And I want a smaller footprint for the first pass*/ + if ( _this->vulkan_config.vkEnumerateInstanceExtensionProperties ) { + enumerateExtensions = SDL_Vulkan_CreateInstanceExtensionsList( + (PFN_vkEnumerateInstanceExtensionProperties) + _this->vulkan_config.vkEnumerateInstanceExtensionProperties, + &enumerateExtensionCount); + for (i = 0; i < enumerateExtensionCount; i++) { + if (SDL_strcmp(VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME, enumerateExtensions[i].extensionName) == 0) { + hasHeadlessSurfaceExtension = true; + } + } + SDL_free(enumerateExtensions); + } + if ( hasHeadlessSurfaceExtension == true ) { + *count = SDL_arraysize(returnExtensions); + } else { + *count = SDL_arraysize(returnExtensions) - 1; // assumes VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME is last + } + } +# else + { + *count = SDL_arraysize(returnExtensions); + } +# endif + } + return returnExtensions; +} + +bool OFFSCREEN_Vulkan_CreateSurface(SDL_VideoDevice *_this, + SDL_Window *window, + VkInstance instance, + const struct VkAllocationCallbacks *allocator, + VkSurfaceKHR *surface) +{ + *surface = VK_NULL_HANDLE; + + if (!_this->vulkan_config.loader_handle) { + return SDL_SetError("Vulkan is not loaded"); + } + + PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr; + PFN_vkCreateHeadlessSurfaceEXT vkCreateHeadlessSurfaceEXT = + (PFN_vkCreateHeadlessSurfaceEXT)vkGetInstanceProcAddr(instance, "vkCreateHeadlessSurfaceEXT"); + VkHeadlessSurfaceCreateInfoEXT createInfo; + VkResult result; + if (!vkCreateHeadlessSurfaceEXT) { + /* This may be surprising to the consumer when HEADLESS_SURFACE_EXTENSION_REQUIRED_TO_LOAD == 0 + But this is the tradeoff for allowing offscreen rendering to a buffer to continue working without requiring the extension during driver load */ + return SDL_SetError(VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME + " extension is not enabled in the Vulkan instance."); + } + SDL_zero(createInfo); + createInfo.sType = VK_STRUCTURE_TYPE_HEADLESS_SURFACE_CREATE_INFO_EXT; + createInfo.pNext = NULL; + createInfo.flags = 0; + result = vkCreateHeadlessSurfaceEXT(instance, &createInfo, allocator, surface); + if (result != VK_SUCCESS) { + return SDL_SetError("vkCreateHeadlessSurfaceEXT failed: %s", SDL_Vulkan_GetResultString(result)); + } + return true; +} + +void OFFSCREEN_Vulkan_DestroySurface(SDL_VideoDevice *_this, + VkInstance instance, + VkSurfaceKHR surface, + const struct VkAllocationCallbacks *allocator) +{ + if (_this->vulkan_config.loader_handle) { + SDL_Vulkan_DestroySurface_Internal(_this->vulkan_config.vkGetInstanceProcAddr, instance, surface, allocator); + } +} + +#endif diff --git a/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvulkan.h b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvulkan.h new file mode 100644 index 0000000..b9d348e --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenvulkan.h @@ -0,0 +1,38 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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 "SDL_internal.h" + +#ifndef SDL_offscreenvulkan_h +#define SDL_offscreenvulkan_h + +#if defined(SDL_VIDEO_DRIVER_OFFSCREEN) && defined(SDL_VIDEO_VULKAN) + +#include "../SDL_sysvideo.h" + +extern bool OFFSCREEN_Vulkan_LoadLibrary(SDL_VideoDevice *_this, const char *path); +extern void OFFSCREEN_Vulkan_UnloadLibrary(SDL_VideoDevice *_this); +extern char const *const *OFFSCREEN_Vulkan_GetInstanceExtensions(SDL_VideoDevice *_this, Uint32 *count); +extern bool OFFSCREEN_Vulkan_CreateSurface(SDL_VideoDevice *_this, SDL_Window *window, VkInstance instance, const struct VkAllocationCallbacks *allocator, VkSurfaceKHR *surface); +extern void OFFSCREEN_Vulkan_DestroySurface(SDL_VideoDevice *_this, VkInstance instance, VkSurfaceKHR surface, const struct VkAllocationCallbacks *allocator); + +#endif // SDL_VIDEO_DRIVER_OFFSCREEN && SDL_VIDEO_VULKAN + +#endif // SDL_offscreenvulkan_h diff --git a/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenwindow.c b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenwindow.c new file mode 100644 index 0000000..579353b --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenwindow.c @@ -0,0 +1,90 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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 "SDL_internal.h" + +#ifdef SDL_VIDEO_DRIVER_OFFSCREEN + +#include "../SDL_sysvideo.h" +#include "../../events/SDL_windowevents_c.h" +#include "../SDL_egl_c.h" + +#include "SDL_offscreenwindow.h" + +bool OFFSCREEN_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID create_props) +{ + SDL_WindowData *offscreen_window = (SDL_WindowData *)SDL_calloc(1, sizeof(SDL_WindowData)); + + if (!offscreen_window) { + return false; + } + + window->internal = offscreen_window; + + if (window->x == SDL_WINDOWPOS_UNDEFINED) { + window->x = 0; + } + + if (window->y == SDL_WINDOWPOS_UNDEFINED) { + window->y = 0; + } + + offscreen_window->sdl_window = window; + +#ifdef SDL_VIDEO_OPENGL_EGL + if (window->flags & SDL_WINDOW_OPENGL) { + + if (!_this->egl_data) { + return SDL_SetError("Cannot create an OPENGL window invalid egl_data"); + } + + offscreen_window->egl_surface = SDL_EGL_CreateOffscreenSurface(_this, window->w, window->h); + + if (offscreen_window->egl_surface == EGL_NO_SURFACE) { + return SDL_SetError("Failed to created an offscreen surface (EGL display: %p)", + _this->egl_data->egl_display); + } + } else { + offscreen_window->egl_surface = EGL_NO_SURFACE; + } +#endif // SDL_VIDEO_OPENGL_EGL + + return true; +} + +void OFFSCREEN_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window) +{ + SDL_WindowData *offscreen_window = window->internal; + + if (offscreen_window) { +#ifdef SDL_VIDEO_OPENGL_EGL + SDL_EGL_DestroySurface(_this, offscreen_window->egl_surface); +#endif + SDL_free(offscreen_window); + } + + window->internal = NULL; +} + +void OFFSCREEN_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window) +{ + SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESIZED, window->pending.w, window->pending.h); +} +#endif // SDL_VIDEO_DRIVER_OFFSCREEN diff --git a/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenwindow.h b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenwindow.h new file mode 100644 index 0000000..0f723c7 --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/offscreen/SDL_offscreenwindow.h @@ -0,0 +1,40 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2025 Sam Lantinga + + 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 "SDL_internal.h" + +#ifndef SDL_offscreenwindow_h +#define SDL_offscreenwindow_h + +#include "SDL_offscreenvideo.h" + +struct SDL_WindowData +{ + SDL_Window *sdl_window; +#ifdef SDL_VIDEO_OPENGL_EGL + EGLSurface egl_surface; +#endif +}; + +extern bool OFFSCREEN_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_PropertiesID create_props); +extern void OFFSCREEN_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window); +extern void OFFSCREEN_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window); + +#endif // SDL_offscreenwindow_h -- cgit v1.2.3