winex11.drv: Only return vulkan functions if the host function is available.
[wine.git] / dlls / winex11.drv / vulkan.c
blob1bbdba2ce1dbeff3b27617bfa18c3181d3e423c5
1 /* X11DRV Vulkan implementation
3 * Copyright 2017 Roderick Colenbrander
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 /* NOTE: If making changes here, consider whether they should be reflected in
21 * the other drivers. */
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <stdio.h>
29 #include "windef.h"
30 #include "winbase.h"
32 #include "wine/debug.h"
33 #include "wine/heap.h"
34 #include "x11drv.h"
36 #define VK_NO_PROTOTYPES
37 #define WINE_VK_HOST
39 #include "wine/vulkan.h"
40 #include "wine/vulkan_driver.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(vulkan);
44 #ifdef SONAME_LIBVULKAN
45 WINE_DECLARE_DEBUG_CHANNEL(fps);
47 static CRITICAL_SECTION context_section;
48 static CRITICAL_SECTION_DEBUG critsect_debug =
50 0, 0, &context_section,
51 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
52 0, 0, { (DWORD_PTR)(__FILE__ ": context_section") }
54 static CRITICAL_SECTION context_section = { &critsect_debug, -1, 0, 0, 0, 0 };
56 static XContext vulkan_hwnd_context;
58 #define VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR 1000004000
60 struct wine_vk_surface
62 LONG ref;
63 Window window;
64 VkSurfaceKHR surface; /* native surface */
65 HWND hwnd;
68 typedef struct VkXlibSurfaceCreateInfoKHR
70 VkStructureType sType;
71 const void *pNext;
72 VkXlibSurfaceCreateFlagsKHR flags;
73 Display *dpy;
74 Window window;
75 } VkXlibSurfaceCreateInfoKHR;
77 static VkResult (*pvkCreateInstance)(const VkInstanceCreateInfo *, const VkAllocationCallbacks *, VkInstance *);
78 static VkResult (*pvkCreateSwapchainKHR)(VkDevice, const VkSwapchainCreateInfoKHR *, const VkAllocationCallbacks *, VkSwapchainKHR *);
79 static VkResult (*pvkCreateXlibSurfaceKHR)(VkInstance, const VkXlibSurfaceCreateInfoKHR *, const VkAllocationCallbacks *, VkSurfaceKHR *);
80 static void (*pvkDestroyInstance)(VkInstance, const VkAllocationCallbacks *);
81 static void (*pvkDestroySurfaceKHR)(VkInstance, VkSurfaceKHR, const VkAllocationCallbacks *);
82 static void (*pvkDestroySwapchainKHR)(VkDevice, VkSwapchainKHR, const VkAllocationCallbacks *);
83 static VkResult (*pvkEnumerateInstanceExtensionProperties)(const char *, uint32_t *, VkExtensionProperties *);
84 static VkResult (*pvkGetDeviceGroupSurfacePresentModesKHR)(VkDevice, VkSurfaceKHR, VkDeviceGroupPresentModeFlagsKHR *);
85 static void * (*pvkGetDeviceProcAddr)(VkDevice, const char *);
86 static void * (*pvkGetInstanceProcAddr)(VkInstance, const char *);
87 static VkResult (*pvkGetPhysicalDevicePresentRectanglesKHR)(VkPhysicalDevice, VkSurfaceKHR, uint32_t *, VkRect2D *);
88 static VkResult (*pvkGetPhysicalDeviceSurfaceCapabilities2KHR)(VkPhysicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR *, VkSurfaceCapabilities2KHR *);
89 static VkResult (*pvkGetPhysicalDeviceSurfaceCapabilitiesKHR)(VkPhysicalDevice, VkSurfaceKHR, VkSurfaceCapabilitiesKHR *);
90 static VkResult (*pvkGetPhysicalDeviceSurfaceFormats2KHR)(VkPhysicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR *, uint32_t *, VkSurfaceFormat2KHR *);
91 static VkResult (*pvkGetPhysicalDeviceSurfaceFormatsKHR)(VkPhysicalDevice, VkSurfaceKHR, uint32_t *, VkSurfaceFormatKHR *);
92 static VkResult (*pvkGetPhysicalDeviceSurfacePresentModesKHR)(VkPhysicalDevice, VkSurfaceKHR, uint32_t *, VkPresentModeKHR *);
93 static VkResult (*pvkGetPhysicalDeviceSurfaceSupportKHR)(VkPhysicalDevice, uint32_t, VkSurfaceKHR, VkBool32 *);
94 static VkBool32 (*pvkGetPhysicalDeviceXlibPresentationSupportKHR)(VkPhysicalDevice, uint32_t, Display *, VisualID);
95 static VkResult (*pvkGetSwapchainImagesKHR)(VkDevice, VkSwapchainKHR, uint32_t *, VkImage *);
96 static VkResult (*pvkQueuePresentKHR)(VkQueue, const VkPresentInfoKHR *);
98 static void *X11DRV_get_vk_device_proc_addr(const char *name);
99 static void *X11DRV_get_vk_instance_proc_addr(VkInstance instance, const char *name);
101 static inline struct wine_vk_surface *surface_from_handle(VkSurfaceKHR handle)
103 return (struct wine_vk_surface *)(uintptr_t)handle;
106 static void *vulkan_handle;
108 static BOOL WINAPI wine_vk_init(INIT_ONCE *once, void *param, void **context)
110 if (!(vulkan_handle = dlopen(SONAME_LIBVULKAN, RTLD_NOW)))
112 ERR("Failed to load %s.\n", SONAME_LIBVULKAN);
113 return TRUE;
116 #define LOAD_FUNCPTR(f) if (!(p##f = dlsym(vulkan_handle, #f))) goto fail
117 #define LOAD_OPTIONAL_FUNCPTR(f) p##f = dlsym(vulkan_handle, #f)
118 LOAD_FUNCPTR(vkCreateInstance);
119 LOAD_FUNCPTR(vkCreateSwapchainKHR);
120 LOAD_FUNCPTR(vkCreateXlibSurfaceKHR);
121 LOAD_FUNCPTR(vkDestroyInstance);
122 LOAD_FUNCPTR(vkDestroySurfaceKHR);
123 LOAD_FUNCPTR(vkDestroySwapchainKHR);
124 LOAD_FUNCPTR(vkEnumerateInstanceExtensionProperties);
125 LOAD_FUNCPTR(vkGetDeviceProcAddr);
126 LOAD_FUNCPTR(vkGetInstanceProcAddr);
127 LOAD_OPTIONAL_FUNCPTR(vkGetPhysicalDeviceSurfaceCapabilities2KHR);
128 LOAD_FUNCPTR(vkGetPhysicalDeviceSurfaceCapabilitiesKHR);
129 LOAD_OPTIONAL_FUNCPTR(vkGetPhysicalDeviceSurfaceFormats2KHR);
130 LOAD_FUNCPTR(vkGetPhysicalDeviceSurfaceFormatsKHR);
131 LOAD_FUNCPTR(vkGetPhysicalDeviceSurfacePresentModesKHR);
132 LOAD_FUNCPTR(vkGetPhysicalDeviceSurfaceSupportKHR);
133 LOAD_FUNCPTR(vkGetPhysicalDeviceXlibPresentationSupportKHR);
134 LOAD_FUNCPTR(vkGetSwapchainImagesKHR);
135 LOAD_FUNCPTR(vkQueuePresentKHR);
136 LOAD_OPTIONAL_FUNCPTR(vkGetDeviceGroupSurfacePresentModesKHR);
137 LOAD_OPTIONAL_FUNCPTR(vkGetPhysicalDevicePresentRectanglesKHR);
138 #undef LOAD_FUNCPTR
139 #undef LOAD_OPTIONAL_FUNCPTR
141 vulkan_hwnd_context = XUniqueContext();
143 return TRUE;
145 fail:
146 dlclose(vulkan_handle);
147 vulkan_handle = NULL;
148 return TRUE;
151 /* Helper function for converting between win32 and X11 compatible VkInstanceCreateInfo.
152 * Caller is responsible for allocation and cleanup of 'dst'.
154 static VkResult wine_vk_instance_convert_create_info(const VkInstanceCreateInfo *src,
155 VkInstanceCreateInfo *dst)
157 unsigned int i;
158 const char **enabled_extensions = NULL;
160 dst->sType = src->sType;
161 dst->flags = src->flags;
162 dst->pApplicationInfo = src->pApplicationInfo;
163 dst->pNext = src->pNext;
164 dst->enabledLayerCount = 0;
165 dst->ppEnabledLayerNames = NULL;
166 dst->enabledExtensionCount = 0;
167 dst->ppEnabledExtensionNames = NULL;
169 if (src->enabledExtensionCount > 0)
171 enabled_extensions = heap_calloc(src->enabledExtensionCount, sizeof(*src->ppEnabledExtensionNames));
172 if (!enabled_extensions)
174 ERR("Failed to allocate memory for enabled extensions\n");
175 return VK_ERROR_OUT_OF_HOST_MEMORY;
178 for (i = 0; i < src->enabledExtensionCount; i++)
180 /* Substitute extension with X11 ones else copy. Long-term, when we
181 * support more extensions, we should store these in a list.
183 if (!strcmp(src->ppEnabledExtensionNames[i], "VK_KHR_win32_surface"))
185 enabled_extensions[i] = "VK_KHR_xlib_surface";
187 else
189 enabled_extensions[i] = src->ppEnabledExtensionNames[i];
192 dst->ppEnabledExtensionNames = enabled_extensions;
193 dst->enabledExtensionCount = src->enabledExtensionCount;
196 return VK_SUCCESS;
199 static struct wine_vk_surface *wine_vk_surface_grab(struct wine_vk_surface *surface)
201 InterlockedIncrement(&surface->ref);
202 return surface;
205 static void wine_vk_surface_release(struct wine_vk_surface *surface)
207 if (InterlockedDecrement(&surface->ref))
208 return;
210 if (surface->window)
211 XDestroyWindow(gdi_display, surface->window);
213 heap_free(surface);
216 void wine_vk_surface_destroy(HWND hwnd)
218 struct wine_vk_surface *surface;
219 EnterCriticalSection(&context_section);
220 if (!XFindContext(gdi_display, (XID)hwnd, vulkan_hwnd_context, (char **)&surface))
222 wine_vk_surface_release(surface);
224 XDeleteContext(gdi_display, (XID)hwnd, vulkan_hwnd_context);
225 LeaveCriticalSection(&context_section);
228 static VkResult X11DRV_vkCreateInstance(const VkInstanceCreateInfo *create_info,
229 const VkAllocationCallbacks *allocator, VkInstance *instance)
231 VkInstanceCreateInfo create_info_host;
232 VkResult res;
233 TRACE("create_info %p, allocator %p, instance %p\n", create_info, allocator, instance);
235 if (allocator)
236 FIXME("Support for allocation callbacks not implemented yet\n");
238 /* Perform a second pass on converting VkInstanceCreateInfo. Winevulkan
239 * performed a first pass in which it handles everything except for WSI
240 * functionality such as VK_KHR_win32_surface. Handle this now.
242 res = wine_vk_instance_convert_create_info(create_info, &create_info_host);
243 if (res != VK_SUCCESS)
245 ERR("Failed to convert instance create info, res=%d\n", res);
246 return res;
249 res = pvkCreateInstance(&create_info_host, NULL /* allocator */, instance);
251 heap_free((void *)create_info_host.ppEnabledExtensionNames);
252 return res;
255 static VkResult X11DRV_vkCreateSwapchainKHR(VkDevice device,
256 const VkSwapchainCreateInfoKHR *create_info,
257 const VkAllocationCallbacks *allocator, VkSwapchainKHR *swapchain)
259 struct wine_vk_surface *x11_surface = surface_from_handle(create_info->surface);
260 VkSwapchainCreateInfoKHR create_info_host;
261 TRACE("%p %p %p %p\n", device, create_info, allocator, swapchain);
263 if (allocator)
264 FIXME("Support for allocation callbacks not implemented yet\n");
266 if (!x11_surface->hwnd)
267 return VK_ERROR_SURFACE_LOST_KHR;
269 create_info_host = *create_info;
270 create_info_host.surface = x11_surface->surface;
272 return pvkCreateSwapchainKHR(device, &create_info_host, NULL /* allocator */, swapchain);
275 static VkResult X11DRV_vkCreateWin32SurfaceKHR(VkInstance instance,
276 const VkWin32SurfaceCreateInfoKHR *create_info,
277 const VkAllocationCallbacks *allocator, VkSurfaceKHR *surface)
279 VkResult res;
280 VkXlibSurfaceCreateInfoKHR create_info_host;
281 struct wine_vk_surface *x11_surface, *prev;
283 TRACE("%p %p %p %p\n", instance, create_info, allocator, surface);
285 if (allocator)
286 FIXME("Support for allocation callbacks not implemented yet\n");
288 /* TODO: support child window rendering. */
289 if (create_info->hwnd && GetAncestor(create_info->hwnd, GA_PARENT) != GetDesktopWindow())
291 FIXME("Application requires child window rendering, which is not implemented yet!\n");
292 return VK_ERROR_INCOMPATIBLE_DRIVER;
295 x11_surface = heap_alloc_zero(sizeof(*x11_surface));
296 if (!x11_surface)
297 return VK_ERROR_OUT_OF_HOST_MEMORY;
299 x11_surface->ref = 1;
300 x11_surface->hwnd = create_info->hwnd;
301 x11_surface->window = x11_surface->hwnd ? create_client_window(create_info->hwnd, &default_visual)
302 : create_dummy_client_window();
304 if (!x11_surface->window)
306 ERR("Failed to allocate client window for hwnd=%p\n", create_info->hwnd);
308 /* VK_KHR_win32_surface only allows out of host and device memory as errors. */
309 res = VK_ERROR_OUT_OF_HOST_MEMORY;
310 goto err;
313 create_info_host.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
314 create_info_host.pNext = NULL;
315 create_info_host.flags = 0; /* reserved */
316 create_info_host.dpy = gdi_display;
317 create_info_host.window = x11_surface->window;
319 res = pvkCreateXlibSurfaceKHR(instance, &create_info_host, NULL /* allocator */, &x11_surface->surface);
320 if (res != VK_SUCCESS)
322 ERR("Failed to create Xlib surface, res=%d\n", res);
323 goto err;
326 if (x11_surface->hwnd)
328 EnterCriticalSection(&context_section);
329 if (!XFindContext(gdi_display, (XID)create_info->hwnd, vulkan_hwnd_context, (char **)&prev))
331 wine_vk_surface_release(prev);
333 XSaveContext(gdi_display, (XID)create_info->hwnd, vulkan_hwnd_context, (char *)wine_vk_surface_grab(x11_surface));
334 LeaveCriticalSection(&context_section);
337 *surface = (uintptr_t)x11_surface;
339 TRACE("Created surface=0x%s\n", wine_dbgstr_longlong(*surface));
340 return VK_SUCCESS;
342 err:
343 wine_vk_surface_release(x11_surface);
344 return res;
347 static void X11DRV_vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks *allocator)
349 TRACE("%p %p\n", instance, allocator);
351 if (allocator)
352 FIXME("Support for allocation callbacks not implemented yet\n");
354 pvkDestroyInstance(instance, NULL /* allocator */);
357 static void X11DRV_vkDestroySurfaceKHR(VkInstance instance, VkSurfaceKHR surface,
358 const VkAllocationCallbacks *allocator)
360 struct wine_vk_surface *x11_surface = surface_from_handle(surface);
362 TRACE("%p 0x%s %p\n", instance, wine_dbgstr_longlong(surface), allocator);
364 if (allocator)
365 FIXME("Support for allocation callbacks not implemented yet\n");
367 /* vkDestroySurfaceKHR must handle VK_NULL_HANDLE (0) for surface. */
368 if (x11_surface)
370 pvkDestroySurfaceKHR(instance, x11_surface->surface, NULL /* allocator */);
372 wine_vk_surface_release(x11_surface);
376 static void X11DRV_vkDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain,
377 const VkAllocationCallbacks *allocator)
379 TRACE("%p, 0x%s %p\n", device, wine_dbgstr_longlong(swapchain), allocator);
381 if (allocator)
382 FIXME("Support for allocation callbacks not implemented yet\n");
384 pvkDestroySwapchainKHR(device, swapchain, NULL /* allocator */);
387 static VkResult X11DRV_vkEnumerateInstanceExtensionProperties(const char *layer_name,
388 uint32_t *count, VkExtensionProperties* properties)
390 unsigned int i;
391 VkResult res;
393 TRACE("layer_name %s, count %p, properties %p\n", debugstr_a(layer_name), count, properties);
395 /* This shouldn't get called with layer_name set, the ICD loader prevents it. */
396 if (layer_name)
398 ERR("Layer enumeration not supported from ICD.\n");
399 return VK_ERROR_LAYER_NOT_PRESENT;
402 /* We will return the same number of instance extensions reported by the host back to
403 * winevulkan. Along the way we may replace xlib extensions with their win32 equivalents.
404 * Winevulkan will perform more detailed filtering as it knows whether it has thunks
405 * for a particular extension.
407 res = pvkEnumerateInstanceExtensionProperties(layer_name, count, properties);
408 if (!properties || res < 0)
409 return res;
411 for (i = 0; i < *count; i++)
413 /* For now the only x11 extension we need to fixup. Long-term we may need an array. */
414 if (!strcmp(properties[i].extensionName, "VK_KHR_xlib_surface"))
416 TRACE("Substituting VK_KHR_xlib_surface for VK_KHR_win32_surface\n");
418 snprintf(properties[i].extensionName, sizeof(properties[i].extensionName),
419 VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
420 properties[i].specVersion = VK_KHR_WIN32_SURFACE_SPEC_VERSION;
424 TRACE("Returning %u extensions.\n", *count);
425 return res;
428 static VkResult X11DRV_vkGetDeviceGroupSurfacePresentModesKHR(VkDevice device,
429 VkSurfaceKHR surface, VkDeviceGroupPresentModeFlagsKHR *flags)
431 struct wine_vk_surface *x11_surface = surface_from_handle(surface);
433 TRACE("%p, 0x%s, %p\n", device, wine_dbgstr_longlong(surface), flags);
435 return pvkGetDeviceGroupSurfacePresentModesKHR(device, x11_surface->surface, flags);
438 static const char *wine_vk_native_fn_name(const char *name)
440 if (!strcmp(name, "vkCreateWin32SurfaceKHR"))
441 return "vkCreateXlibSurfaceKHR";
442 if (!strcmp(name, "vkGetPhysicalDeviceWin32PresentationSupportKHR"))
443 return "vkGetPhysicalDeviceXlibPresentationSupportKHR";
445 return name;
448 static void *X11DRV_vkGetDeviceProcAddr(VkDevice device, const char *name)
450 void *proc_addr;
452 TRACE("%p, %s\n", device, debugstr_a(name));
454 if (!pvkGetDeviceProcAddr(device, wine_vk_native_fn_name(name)))
455 return NULL;
457 if ((proc_addr = X11DRV_get_vk_device_proc_addr(name)))
458 return proc_addr;
460 return pvkGetDeviceProcAddr(device, name);
463 static void *X11DRV_vkGetInstanceProcAddr(VkInstance instance, const char *name)
465 void *proc_addr;
467 TRACE("%p, %s\n", instance, debugstr_a(name));
469 if (!pvkGetInstanceProcAddr(instance, wine_vk_native_fn_name(name)))
470 return NULL;
472 if ((proc_addr = X11DRV_get_vk_instance_proc_addr(instance, name)))
473 return proc_addr;
475 return pvkGetInstanceProcAddr(instance, name);
478 static VkResult X11DRV_vkGetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice phys_dev,
479 VkSurfaceKHR surface, uint32_t *count, VkRect2D *rects)
481 struct wine_vk_surface *x11_surface = surface_from_handle(surface);
483 TRACE("%p, 0x%s, %p, %p\n", phys_dev, wine_dbgstr_longlong(surface), count, rects);
485 if (!x11_surface->hwnd)
487 if (rects)
488 return VK_ERROR_SURFACE_LOST_KHR;
490 *count = 1;
491 return VK_SUCCESS;
494 return pvkGetPhysicalDevicePresentRectanglesKHR(phys_dev, x11_surface->surface, count, rects);
497 static VkResult X11DRV_vkGetPhysicalDeviceSurfaceCapabilities2KHR(VkPhysicalDevice phys_dev,
498 const VkPhysicalDeviceSurfaceInfo2KHR *surface_info, VkSurfaceCapabilities2KHR *capabilities)
500 VkPhysicalDeviceSurfaceInfo2KHR surface_info_host;
501 TRACE("%p, %p, %p\n", phys_dev, surface_info, capabilities);
503 surface_info_host = *surface_info;
504 surface_info_host.surface = surface_from_handle(surface_info->surface)->surface;
506 if (pvkGetPhysicalDeviceSurfaceCapabilities2KHR)
507 return pvkGetPhysicalDeviceSurfaceCapabilities2KHR(phys_dev, &surface_info_host, capabilities);
509 /* Until the loader version exporting this function is common, emulate it using the older non-2 version. */
510 if (surface_info->pNext || capabilities->pNext)
511 FIXME("Emulating vkGetPhysicalDeviceSurfaceCapabilities2KHR with vkGetPhysicalDeviceSurfaceCapabilitiesKHR, pNext is ignored.\n");
513 return pvkGetPhysicalDeviceSurfaceCapabilitiesKHR(phys_dev, surface_info_host.surface, &capabilities->surfaceCapabilities);
516 static VkResult X11DRV_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice phys_dev,
517 VkSurfaceKHR surface, VkSurfaceCapabilitiesKHR *capabilities)
519 struct wine_vk_surface *x11_surface = surface_from_handle(surface);
521 TRACE("%p, 0x%s, %p\n", phys_dev, wine_dbgstr_longlong(surface), capabilities);
523 if (!x11_surface->hwnd)
524 return VK_ERROR_SURFACE_LOST_KHR;
526 return pvkGetPhysicalDeviceSurfaceCapabilitiesKHR(phys_dev, x11_surface->surface, capabilities);
529 static VkResult X11DRV_vkGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice phys_dev,
530 const VkPhysicalDeviceSurfaceInfo2KHR *surface_info, uint32_t *count, VkSurfaceFormat2KHR *formats)
532 VkPhysicalDeviceSurfaceInfo2KHR surface_info_host = *surface_info;
533 VkSurfaceFormatKHR *formats_host;
534 uint32_t i;
535 VkResult result;
536 TRACE("%p, %p, %p, %p\n", phys_dev, surface_info, count, formats);
538 surface_info_host = *surface_info;
539 surface_info_host.surface = surface_from_handle(surface_info->surface)->surface;
541 if (pvkGetPhysicalDeviceSurfaceFormats2KHR)
542 return pvkGetPhysicalDeviceSurfaceFormats2KHR(phys_dev, &surface_info_host, count, formats);
544 /* Until the loader version exporting this function is common, emulate it using the older non-2 version. */
545 if (surface_info->pNext)
546 FIXME("Emulating vkGetPhysicalDeviceSurfaceFormats2KHR with vkGetPhysicalDeviceSurfaceFormatsKHR, pNext is ignored.\n");
548 if (!formats)
549 return pvkGetPhysicalDeviceSurfaceFormatsKHR(phys_dev, surface_info_host.surface, count, NULL);
551 formats_host = heap_calloc(*count, sizeof(*formats_host));
552 if (!formats_host) return VK_ERROR_OUT_OF_HOST_MEMORY;
553 result = pvkGetPhysicalDeviceSurfaceFormatsKHR(phys_dev, surface_info_host.surface, count, formats_host);
554 if (result == VK_SUCCESS || result == VK_INCOMPLETE)
556 for (i = 0; i < *count; i++)
557 formats[i].surfaceFormat = formats_host[i];
560 heap_free(formats_host);
561 return result;
564 static VkResult X11DRV_vkGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice phys_dev,
565 VkSurfaceKHR surface, uint32_t *count, VkSurfaceFormatKHR *formats)
567 struct wine_vk_surface *x11_surface = surface_from_handle(surface);
569 TRACE("%p, 0x%s, %p, %p\n", phys_dev, wine_dbgstr_longlong(surface), count, formats);
571 return pvkGetPhysicalDeviceSurfaceFormatsKHR(phys_dev, x11_surface->surface, count, formats);
574 static VkResult X11DRV_vkGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice phys_dev,
575 VkSurfaceKHR surface, uint32_t *count, VkPresentModeKHR *modes)
577 struct wine_vk_surface *x11_surface = surface_from_handle(surface);
579 TRACE("%p, 0x%s, %p, %p\n", phys_dev, wine_dbgstr_longlong(surface), count, modes);
581 return pvkGetPhysicalDeviceSurfacePresentModesKHR(phys_dev, x11_surface->surface, count, modes);
584 static VkResult X11DRV_vkGetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice phys_dev,
585 uint32_t index, VkSurfaceKHR surface, VkBool32 *supported)
587 struct wine_vk_surface *x11_surface = surface_from_handle(surface);
589 TRACE("%p, %u, 0x%s, %p\n", phys_dev, index, wine_dbgstr_longlong(surface), supported);
591 return pvkGetPhysicalDeviceSurfaceSupportKHR(phys_dev, index, x11_surface->surface, supported);
594 static VkBool32 X11DRV_vkGetPhysicalDeviceWin32PresentationSupportKHR(VkPhysicalDevice phys_dev,
595 uint32_t index)
597 TRACE("%p %u\n", phys_dev, index);
599 return pvkGetPhysicalDeviceXlibPresentationSupportKHR(phys_dev, index, gdi_display,
600 default_visual.visual->visualid);
603 static VkResult X11DRV_vkGetSwapchainImagesKHR(VkDevice device,
604 VkSwapchainKHR swapchain, uint32_t *count, VkImage *images)
606 TRACE("%p, 0x%s %p %p\n", device, wine_dbgstr_longlong(swapchain), count, images);
607 return pvkGetSwapchainImagesKHR(device, swapchain, count, images);
610 static VkResult X11DRV_vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *present_info)
612 VkResult res;
614 TRACE("%p, %p\n", queue, present_info);
616 res = pvkQueuePresentKHR(queue, present_info);
618 if (TRACE_ON(fps))
620 static unsigned long frames, frames_total;
621 static long prev_time, start_time;
622 DWORD time;
624 time = GetTickCount();
625 frames++;
626 frames_total++;
627 if (time - prev_time > 1500)
629 TRACE_(fps)("%p @ approx %.2ffps, total %.2ffps\n",
630 queue, 1000.0 * frames / (time - prev_time),
631 1000.0 * frames_total / (time - start_time));
632 prev_time = time;
633 frames = 0;
634 if (!start_time)
635 start_time = time;
639 return res;
642 static VkSurfaceKHR X11DRV_wine_get_native_surface(VkSurfaceKHR surface)
644 struct wine_vk_surface *x11_surface = surface_from_handle(surface);
646 TRACE("0x%s\n", wine_dbgstr_longlong(surface));
648 return x11_surface->surface;
651 static const struct vulkan_funcs vulkan_funcs =
653 X11DRV_vkCreateInstance,
654 X11DRV_vkCreateSwapchainKHR,
655 X11DRV_vkCreateWin32SurfaceKHR,
656 X11DRV_vkDestroyInstance,
657 X11DRV_vkDestroySurfaceKHR,
658 X11DRV_vkDestroySwapchainKHR,
659 X11DRV_vkEnumerateInstanceExtensionProperties,
660 X11DRV_vkGetDeviceGroupSurfacePresentModesKHR,
661 X11DRV_vkGetDeviceProcAddr,
662 X11DRV_vkGetInstanceProcAddr,
663 X11DRV_vkGetPhysicalDevicePresentRectanglesKHR,
664 X11DRV_vkGetPhysicalDeviceSurfaceCapabilities2KHR,
665 X11DRV_vkGetPhysicalDeviceSurfaceCapabilitiesKHR,
666 X11DRV_vkGetPhysicalDeviceSurfaceFormats2KHR,
667 X11DRV_vkGetPhysicalDeviceSurfaceFormatsKHR,
668 X11DRV_vkGetPhysicalDeviceSurfacePresentModesKHR,
669 X11DRV_vkGetPhysicalDeviceSurfaceSupportKHR,
670 X11DRV_vkGetPhysicalDeviceWin32PresentationSupportKHR,
671 X11DRV_vkGetSwapchainImagesKHR,
672 X11DRV_vkQueuePresentKHR,
674 X11DRV_wine_get_native_surface,
677 static void *X11DRV_get_vk_device_proc_addr(const char *name)
679 return get_vulkan_driver_device_proc_addr(&vulkan_funcs, name);
682 static void *X11DRV_get_vk_instance_proc_addr(VkInstance instance, const char *name)
684 return get_vulkan_driver_instance_proc_addr(&vulkan_funcs, instance, name);
687 const struct vulkan_funcs *get_vulkan_driver(UINT version)
689 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
691 if (version != WINE_VULKAN_DRIVER_VERSION)
693 ERR("version mismatch, vulkan wants %u but driver has %u\n", version, WINE_VULKAN_DRIVER_VERSION);
694 return NULL;
697 InitOnceExecuteOnce(&init_once, wine_vk_init, NULL, NULL);
698 if (vulkan_handle)
699 return &vulkan_funcs;
701 return NULL;
704 #else /* No vulkan */
706 const struct vulkan_funcs *get_vulkan_driver(UINT version)
708 ERR("Wine was built without Vulkan support.\n");
709 return NULL;
712 void wine_vk_surface_destroy(HWND hwnd)
716 #endif /* SONAME_LIBVULKAN */