SDL 2.0
SDL_x11vulkan.c
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "../../SDL_internal.h"
22
23#if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_X11
24
25#include "SDL_x11video.h"
26#include "SDL_assert.h"
27
28#include "SDL_loadso.h"
29#include "SDL_x11vulkan.h"
30
31#include <X11/Xlib.h>
32/*#include <xcb/xcb.h>*/
33/*
34typedef uint32_t xcb_window_t;
35typedef uint32_t xcb_visualid_t;
36*/
37
38int X11_Vulkan_LoadLibrary(_THIS, const char *path)
39{
41 VkExtensionProperties *extensions = NULL;
42 Uint32 extensionCount = 0;
43 SDL_bool hasSurfaceExtension = SDL_FALSE;
44 SDL_bool hasXlibSurfaceExtension = SDL_FALSE;
45 SDL_bool hasXCBSurfaceExtension = SDL_FALSE;
47 Uint32 i;
49 return SDL_SetError("Vulkan already loaded");
50
51 /* Load the Vulkan loader library */
52 if(!path)
53 path = SDL_getenv("SDL_VULKAN_LIBRARY");
54 if(!path)
55 path = "libvulkan.so.1";
58 return -1;
61 _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr");
63 goto fail;
67 VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
69 goto fail;
70 extensions = SDL_Vulkan_CreateInstanceExtensionsList(
73 &extensionCount);
74 if(!extensions)
75 goto fail;
76 for(i = 0; i < extensionCount; i++)
77 {
78 if(SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0)
79 hasSurfaceExtension = SDL_TRUE;
80 else if(SDL_strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0)
81 hasXCBSurfaceExtension = SDL_TRUE;
82 else if(SDL_strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0)
83 hasXlibSurfaceExtension = SDL_TRUE;
84 }
85 SDL_free(extensions);
86 if(!hasSurfaceExtension)
87 {
88 SDL_SetError("Installed Vulkan doesn't implement the "
90 goto fail;
91 }
92 if(hasXlibSurfaceExtension)
93 {
94 videoData->vulkan_xlib_xcb_library = NULL;
95 }
96 else if(!hasXCBSurfaceExtension)
97 {
98 SDL_SetError("Installed Vulkan doesn't implement either the "
99 VK_KHR_XCB_SURFACE_EXTENSION_NAME "extension or the "
101 goto fail;
102 }
103 else
104 {
105 const char *libX11XCBLibraryName = SDL_getenv("SDL_X11_XCB_LIBRARY");
106 if(!libX11XCBLibraryName)
107 libX11XCBLibraryName = "libX11-xcb.so";
108 videoData->vulkan_xlib_xcb_library = SDL_LoadObject(libX11XCBLibraryName);
109 if(!videoData->vulkan_xlib_xcb_library)
110 goto fail;
111 videoData->vulkan_XGetXCBConnection =
112 SDL_LoadFunction(videoData->vulkan_xlib_xcb_library, "XGetXCBConnection");
113 if(!videoData->vulkan_XGetXCBConnection)
114 {
115 SDL_UnloadObject(videoData->vulkan_xlib_xcb_library);
116 goto fail;
117 }
118 }
119 return 0;
120
121fail:
124 return -1;
125}
126
127void X11_Vulkan_UnloadLibrary(_THIS)
128{
131 {
132 if(videoData->vulkan_xlib_xcb_library)
133 SDL_UnloadObject(videoData->vulkan_xlib_xcb_library);
136 }
137}
138
139SDL_bool X11_Vulkan_GetInstanceExtensions(_THIS,
141 unsigned *count,
142 const char **names)
143{
146 {
147 SDL_SetError("Vulkan is not loaded");
148 return SDL_FALSE;
149 }
150 if(videoData->vulkan_xlib_xcb_library)
151 {
152 static const char *const extensionsForXCB[] = {
154 };
155 return SDL_Vulkan_GetInstanceExtensions_Helper(
156 count, names, SDL_arraysize(extensionsForXCB), extensionsForXCB);
157 }
158 else
159 {
160 static const char *const extensionsForXlib[] = {
162 };
163 return SDL_Vulkan_GetInstanceExtensions_Helper(
164 count, names, SDL_arraysize(extensionsForXlib), extensionsForXlib);
165 }
166}
167
168SDL_bool X11_Vulkan_CreateSurface(_THIS,
170 VkInstance instance,
171 VkSurfaceKHR *surface)
172{
174 SDL_WindowData *windowData = (SDL_WindowData *)window->driverdata;
177 {
178 SDL_SetError("Vulkan is not loaded");
179 return SDL_FALSE;
180 }
182 if(videoData->vulkan_xlib_xcb_library)
183 {
186 "vkCreateXcbSurfaceKHR");
187 VkXcbSurfaceCreateInfoKHR createInfo;
190 {
192 " extension is not enabled in the Vulkan instance.");
193 return SDL_FALSE;
194 }
195 SDL_zero(createInfo);
197 createInfo.connection = videoData->vulkan_XGetXCBConnection(videoData->display);
198 if(!createInfo.connection)
199 {
200 SDL_SetError("XGetXCBConnection failed");
201 return SDL_FALSE;
202 }
203 createInfo.window = (xcb_window_t)windowData->xwindow;
204 result = vkCreateXcbSurfaceKHR(instance, &createInfo,
205 NULL, surface);
206 if(result != VK_SUCCESS)
207 {
208 SDL_SetError("vkCreateXcbSurfaceKHR failed: %s", SDL_Vulkan_GetResultString(result));
209 return SDL_FALSE;
210 }
211 return SDL_TRUE;
212 }
213 else
214 {
217 "vkCreateXlibSurfaceKHR");
221 {
223 " extension is not enabled in the Vulkan instance.");
224 return SDL_FALSE;
225 }
226 SDL_zero(createInfo);
228 createInfo.dpy = videoData->display;
229 createInfo.window = (xcb_window_t)windowData->xwindow;
230 result = vkCreateXlibSurfaceKHR(instance, &createInfo,
231 NULL, surface);
232 if(result != VK_SUCCESS)
233 {
234 SDL_SetError("vkCreateXlibSurfaceKHR failed: %s", SDL_Vulkan_GetResultString(result));
235 return SDL_FALSE;
236 }
237 return SDL_TRUE;
238 }
239}
240
241#endif
242
243/* vim: set ts=4 sw=4 expandtab: */
#define _THIS
#define SDL_SetError
#define SDL_LoadObject
#define SDL_UnloadObject
#define SDL_getenv
#define SDL_strlcpy
#define SDL_free
#define SDL_strcmp
void * SDL_LoadFunction(void *handle, const char *name)
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
GLuint64EXT * result
GLuint GLuint * names
GLsizei const GLchar *const * path
#define SDL_zero(x)
Definition: SDL_stdinc.h:416
SDL_bool
Definition: SDL_stdinc.h:162
@ SDL_TRUE
Definition: SDL_stdinc.h:164
@ SDL_FALSE
Definition: SDL_stdinc.h:163
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
uint32_t Uint32
Definition: SDL_stdinc.h:203
static SDL_VideoDevice * _this
Definition: SDL_video.c:118
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:167
EGLSurface surface
Definition: eglext.h:248
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
struct wl_display * display
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr
Definition: SDL_sysvideo.h:372
PFN_vkEnumerateInstanceExtensionProperties vkEnumerateInstanceExtensionProperties
Definition: SDL_sysvideo.h:373
char loader_path[256]
Definition: SDL_sysvideo.h:375
struct SDL_VideoDevice::@263 vulkan_config
void * loader_handle
Definition: SDL_sysvideo.h:376
The type used to identify a window.
Definition: SDL_sysvideo.h:74
VkStructureType sType
Definition: vulkan_xcb.h:37
xcb_connection_t * connection
Definition: vulkan_xcb.h:40
VkStructureType sType
Definition: vulkan_xlib.h:37
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *pName)
#define VK_KHR_SURFACE_EXTENSION_NAME
Definition: vulkan_core.h:4669
VkResult
Definition: vulkan_core.h:120
@ VK_SUCCESS
Definition: vulkan_core.h:121
VkResult(VKAPI_PTR * PFN_vkEnumerateInstanceExtensionProperties)(const char *pLayerName, uint32_t *pPropertyCount, VkExtensionProperties *pProperties)
Definition: vulkan_core.h:2863
#define VK_NULL_HANDLE
Definition: vulkan_core.h:49
PFN_vkVoidFunction(VKAPI_PTR * PFN_vkGetInstanceProcAddr)(VkInstance instance, const char *pName)
Definition: vulkan_core.h:2859
@ VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR
Definition: vulkan_core.h:285
@ VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR
Definition: vulkan_core.h:286
#define VK_KHR_XCB_SURFACE_EXTENSION_NAME
Definition: vulkan_xcb.h:32
VKAPI_ATTR VkResult VKAPI_CALL vkCreateXcbSurfaceKHR(VkInstance instance, const VkXcbSurfaceCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)
VkResult(VKAPI_PTR * PFN_vkCreateXcbSurfaceKHR)(VkInstance instance, const VkXcbSurfaceCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)
Definition: vulkan_xcb.h:45
#define VK_KHR_XLIB_SURFACE_EXTENSION_NAME
Definition: vulkan_xlib.h:32
VkResult(VKAPI_PTR * PFN_vkCreateXlibSurfaceKHR)(VkInstance instance, const VkXlibSurfaceCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)
Definition: vulkan_xlib.h:45
VKAPI_ATTR VkResult VKAPI_CALL vkCreateXlibSurfaceKHR(VkInstance instance, const VkXlibSurfaceCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)