summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/video/windows/SDL_windowsvulkan.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
committer3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
commit5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch)
tree8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/video/windows/SDL_windowsvulkan.c
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/windows/SDL_windowsvulkan.c')
-rw-r--r--contrib/SDL-3.2.8/src/video/windows/SDL_windowsvulkan.c195
1 files changed, 195 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/windows/SDL_windowsvulkan.c b/contrib/SDL-3.2.8/src/video/windows/SDL_windowsvulkan.c
new file mode 100644
index 0000000..0b16d55
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/windows/SDL_windowsvulkan.c
@@ -0,0 +1,195 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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 defined(SDL_VIDEO_VULKAN) && defined(SDL_VIDEO_DRIVER_WINDOWS)
30
31#include "../SDL_vulkan_internal.h"
32
33#include "SDL_windowsvideo.h"
34#include "SDL_windowswindow.h"
35
36#include "SDL_windowsvulkan.h"
37
38bool WIN_Vulkan_LoadLibrary(SDL_VideoDevice *_this, const char *path)
39{
40 VkExtensionProperties *extensions = NULL;
41 Uint32 extensionCount = 0;
42 Uint32 i;
43 bool hasSurfaceExtension = false;
44 bool hasWin32SurfaceExtension = false;
45 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
46 if (_this->vulkan_config.loader_handle) {
47 return SDL_SetError("Vulkan already loaded");
48 }
49
50 // Load the Vulkan loader library
51 if (!path) {
52 path = SDL_GetHint(SDL_HINT_VULKAN_LIBRARY);
53 }
54 if (!path) {
55 path = "vulkan-1.dll";
56 }
57 _this->vulkan_config.loader_handle = SDL_LoadObject(path);
58 if (!_this->vulkan_config.loader_handle) {
59 return false;
60 }
61 SDL_strlcpy(_this->vulkan_config.loader_path, path,
62 SDL_arraysize(_this->vulkan_config.loader_path));
63 vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction(
64 _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr");
65 if (!vkGetInstanceProcAddr) {
66 goto fail;
67 }
68 _this->vulkan_config.vkGetInstanceProcAddr = (SDL_FunctionPointer)vkGetInstanceProcAddr;
69 _this->vulkan_config.vkEnumerateInstanceExtensionProperties =
70 (SDL_FunctionPointer)vkGetInstanceProcAddr(
71 VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
72 if (!_this->vulkan_config.vkEnumerateInstanceExtensionProperties) {
73 goto fail;
74 }
75 extensions = SDL_Vulkan_CreateInstanceExtensionsList(
76 (PFN_vkEnumerateInstanceExtensionProperties)
77 _this->vulkan_config.vkEnumerateInstanceExtensionProperties,
78 &extensionCount);
79 if (!extensions) {
80 goto fail;
81 }
82 for (i = 0; i < extensionCount; i++) {
83 if (SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) {
84 hasSurfaceExtension = true;
85 } else if (SDL_strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) {
86 hasWin32SurfaceExtension = true;
87 }
88 }
89 SDL_free(extensions);
90 if (!hasSurfaceExtension) {
91 SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_SURFACE_EXTENSION_NAME " extension");
92 goto fail;
93 } else if (!hasWin32SurfaceExtension) {
94 SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME "extension");
95 goto fail;
96 }
97 return true;
98
99fail:
100 SDL_UnloadObject(_this->vulkan_config.loader_handle);
101 _this->vulkan_config.loader_handle = NULL;
102 return false;
103}
104
105void WIN_Vulkan_UnloadLibrary(SDL_VideoDevice *_this)
106{
107 if (_this->vulkan_config.loader_handle) {
108 SDL_UnloadObject(_this->vulkan_config.loader_handle);
109 _this->vulkan_config.loader_handle = NULL;
110 }
111}
112
113char const* const* WIN_Vulkan_GetInstanceExtensions(SDL_VideoDevice *_this,
114 Uint32 *count)
115{
116 static const char *const extensionsForWin32[] = {
117 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_WIN32_SURFACE_EXTENSION_NAME
118 };
119 if (count) {
120 *count = SDL_arraysize(extensionsForWin32);
121 }
122 return extensionsForWin32;
123}
124
125bool WIN_Vulkan_CreateSurface(SDL_VideoDevice *_this,
126 SDL_Window *window,
127 VkInstance instance,
128 const struct VkAllocationCallbacks *allocator,
129 VkSurfaceKHR *surface)
130{
131 SDL_WindowData *windowData = window->internal;
132 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr =
133 (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr;
134 PFN_vkCreateWin32SurfaceKHR vkCreateWin32SurfaceKHR =
135 (PFN_vkCreateWin32SurfaceKHR)vkGetInstanceProcAddr(
136 instance,
137 "vkCreateWin32SurfaceKHR");
138 VkWin32SurfaceCreateInfoKHR createInfo;
139 VkResult result;
140
141 if (!_this->vulkan_config.loader_handle) {
142 return SDL_SetError("Vulkan is not loaded");
143 }
144
145 if (!vkCreateWin32SurfaceKHR) {
146 return SDL_SetError(VK_KHR_WIN32_SURFACE_EXTENSION_NAME
147 " extension is not enabled in the Vulkan instance.");
148 }
149 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
150 createInfo.pNext = NULL;
151 createInfo.flags = 0;
152 createInfo.hinstance = windowData->hinstance;
153 createInfo.hwnd = windowData->hwnd;
154 result = vkCreateWin32SurfaceKHR(instance, &createInfo, allocator, surface);
155 if (result != VK_SUCCESS) {
156 return SDL_SetError("vkCreateWin32SurfaceKHR failed: %s", SDL_Vulkan_GetResultString(result));
157 }
158 return true;
159}
160
161void WIN_Vulkan_DestroySurface(SDL_VideoDevice *_this,
162 VkInstance instance,
163 VkSurfaceKHR surface,
164 const struct VkAllocationCallbacks *allocator)
165{
166 if (_this->vulkan_config.loader_handle) {
167 SDL_Vulkan_DestroySurface_Internal(_this->vulkan_config.vkGetInstanceProcAddr, instance, surface, allocator);
168 }
169}
170
171bool WIN_Vulkan_GetPresentationSupport(SDL_VideoDevice *_this,
172 VkInstance instance,
173 VkPhysicalDevice physicalDevice,
174 Uint32 queueFamilyIndex)
175{
176 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr =
177 (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr;
178 PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR vkGetPhysicalDeviceWin32PresentationSupportKHR =
179 (PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR)vkGetInstanceProcAddr(
180 instance,
181 "vkGetPhysicalDeviceWin32PresentationSupportKHR");
182
183 if (!_this->vulkan_config.loader_handle) {
184 return SDL_SetError("Vulkan is not loaded");
185 }
186
187 if (!vkGetPhysicalDeviceWin32PresentationSupportKHR) {
188 return SDL_SetError(VK_KHR_WIN32_SURFACE_EXTENSION_NAME " extension is not enabled in the Vulkan instance.");
189 }
190
191 return vkGetPhysicalDeviceWin32PresentationSupportKHR(physicalDevice,
192 queueFamilyIndex);
193}
194
195#endif