1 /*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 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
22 /*
23 * @author Mark Callow, www.edgewise-consulting.com. Based on Jacob Lifshay's
24 * SDL_x11vulkan.c.
25 */
26
27 #include "../../SDL_internal.h"
28
29 #if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_WINDOWS
30
31 #include "SDL_windowsvideo.h"
32 #include "SDL_windowswindow.h"
33 #include "SDL_assert.h"
34
35 #include "SDL_loadso.h"
36 #include "SDL_windowsvulkan.h"
37 #include "SDL_syswm.h"
38
WIN_Vulkan_LoadLibrary(_THIS,const char * path)39 int WIN_Vulkan_LoadLibrary(_THIS, const char *path)
40 {
41 VkExtensionProperties *extensions = NULL;
42 Uint32 extensionCount = 0;
43 Uint32 i;
44 SDL_bool hasSurfaceExtension = SDL_FALSE;
45 SDL_bool hasWin32SurfaceExtension = SDL_FALSE;
46 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
47 if(_this->vulkan_config.loader_handle)
48 return SDL_SetError("Vulkan already loaded");
49
50 /* Load the Vulkan loader library */
51 if(!path)
52 path = SDL_getenv("SDL_VULKAN_LIBRARY");
53 if(!path)
54 path = "vulkan-1.dll";
55 _this->vulkan_config.loader_handle = SDL_LoadObject(path);
56 if(!_this->vulkan_config.loader_handle)
57 return -1;
58 SDL_strlcpy(_this->vulkan_config.loader_path, path,
59 SDL_arraysize(_this->vulkan_config.loader_path));
60 vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction(
61 _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr");
62 if(!vkGetInstanceProcAddr)
63 goto fail;
64 _this->vulkan_config.vkGetInstanceProcAddr = (void *)vkGetInstanceProcAddr;
65 _this->vulkan_config.vkEnumerateInstanceExtensionProperties =
66 (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)(
67 VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
68 if(!_this->vulkan_config.vkEnumerateInstanceExtensionProperties)
69 goto fail;
70 extensions = SDL_Vulkan_CreateInstanceExtensionsList(
71 (PFN_vkEnumerateInstanceExtensionProperties)
72 _this->vulkan_config.vkEnumerateInstanceExtensionProperties,
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_WIN32_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0)
81 hasWin32SurfaceExtension = SDL_TRUE;
82 }
83 SDL_free(extensions);
84 if(!hasSurfaceExtension)
85 {
86 SDL_SetError("Installed Vulkan doesn't implement the "
87 VK_KHR_SURFACE_EXTENSION_NAME " extension");
88 goto fail;
89 }
90 else if(!hasWin32SurfaceExtension)
91 {
92 SDL_SetError("Installed Vulkan doesn't implement the "
93 VK_KHR_WIN32_SURFACE_EXTENSION_NAME "extension");
94 goto fail;
95 }
96 return 0;
97
98 fail:
99 SDL_UnloadObject(_this->vulkan_config.loader_handle);
100 _this->vulkan_config.loader_handle = NULL;
101 return -1;
102 }
103
WIN_Vulkan_UnloadLibrary(_THIS)104 void WIN_Vulkan_UnloadLibrary(_THIS)
105 {
106 if(_this->vulkan_config.loader_handle)
107 {
108 SDL_UnloadObject(_this->vulkan_config.loader_handle);
109 _this->vulkan_config.loader_handle = NULL;
110 }
111 }
112
WIN_Vulkan_GetInstanceExtensions(_THIS,SDL_Window * window,unsigned * count,const char ** names)113 SDL_bool WIN_Vulkan_GetInstanceExtensions(_THIS,
114 SDL_Window *window,
115 unsigned *count,
116 const char **names)
117 {
118 static const char *const extensionsForWin32[] = {
119 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_WIN32_SURFACE_EXTENSION_NAME
120 };
121 if(!_this->vulkan_config.loader_handle)
122 {
123 SDL_SetError("Vulkan is not loaded");
124 return SDL_FALSE;
125 }
126 return SDL_Vulkan_GetInstanceExtensions_Helper(
127 count, names, SDL_arraysize(extensionsForWin32),
128 extensionsForWin32);
129 }
130
WIN_Vulkan_CreateSurface(_THIS,SDL_Window * window,VkInstance instance,VkSurfaceKHR * surface)131 SDL_bool WIN_Vulkan_CreateSurface(_THIS,
132 SDL_Window *window,
133 VkInstance instance,
134 VkSurfaceKHR *surface)
135 {
136 SDL_WindowData *windowData = (SDL_WindowData *)window->driverdata;
137 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr =
138 (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr;
139 PFN_vkCreateWin32SurfaceKHR vkCreateWin32SurfaceKHR =
140 (PFN_vkCreateWin32SurfaceKHR)vkGetInstanceProcAddr(
141 (VkInstance)instance,
142 "vkCreateWin32SurfaceKHR");
143 VkWin32SurfaceCreateInfoKHR createInfo;
144 VkResult result;
145
146 if(!_this->vulkan_config.loader_handle)
147 {
148 SDL_SetError("Vulkan is not loaded");
149 return SDL_FALSE;
150 }
151
152 if(!vkCreateWin32SurfaceKHR)
153 {
154 SDL_SetError(VK_KHR_WIN32_SURFACE_EXTENSION_NAME
155 " extension is not enabled in the Vulkan instance.");
156 return SDL_FALSE;
157 }
158 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
159 createInfo.pNext = NULL;
160 createInfo.flags = 0;
161 createInfo.hinstance = windowData->hinstance;
162 createInfo.hwnd = windowData->hwnd;
163 result = vkCreateWin32SurfaceKHR(instance, &createInfo,
164 NULL, surface);
165 if(result != VK_SUCCESS)
166 {
167 SDL_SetError("vkCreateWin32SurfaceKHR failed: %s",
168 SDL_Vulkan_GetResultString(result));
169 return SDL_FALSE;
170 }
171 return SDL_TRUE;
172 }
173
174 #endif
175
176 /* vi: set ts=4 sw=4 expandtab: */
177