winevulkan: Adequate vkEnumerateInstanceLayerProperties to the Vulkan spec.
[wine/zf.git] / dlls / winevulkan / vulkan.c
blob971394eb9ddc9820e47a6a080ca71ddda605cb7e
1 /* Wine Vulkan ICD 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 #include "config.h"
21 #include <time.h>
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "winuser.h"
28 #include "initguid.h"
29 #include "devguid.h"
30 #include "setupapi.h"
32 #include "vulkan_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(vulkan);
36 DEFINE_DEVPROPKEY(DEVPROPKEY_GPU_LUID, 0x60b193cb, 0x5276, 0x4d0f, 0x96, 0xfc, 0xf1, 0x73, 0xab, 0xad, 0x3e, 0xc6, 2);
37 DEFINE_DEVPROPKEY(WINE_DEVPROPKEY_GPU_VULKAN_UUID, 0x233a9ef3, 0xafc4, 0x4abd, 0xb5, 0x64, 0xc3, 0x2f, 0x21, 0xf1, 0x53, 0x5c, 2);
39 /* For now default to 4 as it felt like a reasonable version feature wise to support.
40 * Don't support the optional vk_icdGetPhysicalDeviceProcAddr introduced in this version
41 * as it is unlikely we will implement physical device extensions, which the loader is not
42 * aware of. Version 5 adds more extensive version checks. Something to tackle later.
44 #define WINE_VULKAN_ICD_VERSION 4
46 #define wine_vk_find_struct(s, t) wine_vk_find_struct_((void *)s, VK_STRUCTURE_TYPE_##t)
47 static void *wine_vk_find_struct_(void *s, VkStructureType t)
49 VkBaseOutStructure *header;
51 for (header = s; header; header = header->pNext)
53 if (header->sType == t)
54 return header;
57 return NULL;
60 #define wine_vk_count_struct(s, t) wine_vk_count_struct_((void *)s, VK_STRUCTURE_TYPE_##t)
61 static uint32_t wine_vk_count_struct_(void *s, VkStructureType t)
63 const VkBaseInStructure *header;
64 uint32_t result = 0;
66 for (header = s; header; header = header->pNext)
68 if (header->sType == t)
69 result++;
72 return result;
75 static void *wine_vk_get_global_proc_addr(const char *name);
77 static HINSTANCE hinstance;
78 static const struct vulkan_funcs *vk_funcs;
79 static VkResult (*p_vkEnumerateInstanceVersion)(uint32_t *version);
81 void WINAPI wine_vkGetPhysicalDeviceProperties(VkPhysicalDevice physical_device,
82 VkPhysicalDeviceProperties *properties);
84 #define WINE_VK_ADD_DISPATCHABLE_MAPPING(instance, object, native_handle) \
85 wine_vk_add_handle_mapping((instance), (uint64_t) (uintptr_t) (object), (uint64_t) (uintptr_t) (native_handle), &(object)->mapping)
86 #define WINE_VK_ADD_NON_DISPATCHABLE_MAPPING(instance, object, native_handle) \
87 wine_vk_add_handle_mapping((instance), (uint64_t) (uintptr_t) (object), (uint64_t) (native_handle), &(object)->mapping)
88 static void wine_vk_add_handle_mapping(struct VkInstance_T *instance, uint64_t wrapped_handle,
89 uint64_t native_handle, struct wine_vk_mapping *mapping)
91 if (instance->enable_wrapper_list)
93 mapping->native_handle = native_handle;
94 mapping->wine_wrapped_handle = wrapped_handle;
95 AcquireSRWLockExclusive(&instance->wrapper_lock);
96 list_add_tail(&instance->wrappers, &mapping->link);
97 ReleaseSRWLockExclusive(&instance->wrapper_lock);
101 #define WINE_VK_REMOVE_HANDLE_MAPPING(instance, object) \
102 wine_vk_remove_handle_mapping((instance), &(object)->mapping)
103 static void wine_vk_remove_handle_mapping(struct VkInstance_T *instance, struct wine_vk_mapping *mapping)
105 if (instance->enable_wrapper_list)
107 AcquireSRWLockExclusive(&instance->wrapper_lock);
108 list_remove(&mapping->link);
109 ReleaseSRWLockExclusive(&instance->wrapper_lock);
113 static uint64_t wine_vk_get_wrapper(struct VkInstance_T *instance, uint64_t native_handle)
115 struct wine_vk_mapping *mapping;
116 uint64_t result = 0;
118 AcquireSRWLockShared(&instance->wrapper_lock);
119 LIST_FOR_EACH_ENTRY(mapping, &instance->wrappers, struct wine_vk_mapping, link)
121 if (mapping->native_handle == native_handle)
123 result = mapping->wine_wrapped_handle;
124 break;
127 ReleaseSRWLockShared(&instance->wrapper_lock);
128 return result;
131 static VkBool32 debug_utils_callback_conversion(VkDebugUtilsMessageSeverityFlagBitsEXT severity,
132 VkDebugUtilsMessageTypeFlagsEXT message_types,
133 #if defined(USE_STRUCT_CONVERSION)
134 const VkDebugUtilsMessengerCallbackDataEXT_host *callback_data,
135 #else
136 const VkDebugUtilsMessengerCallbackDataEXT *callback_data,
137 #endif
138 void *user_data)
140 struct VkDebugUtilsMessengerCallbackDataEXT wine_callback_data;
141 VkDebugUtilsObjectNameInfoEXT *object_name_infos;
142 struct wine_debug_utils_messenger *object;
143 VkBool32 result;
144 unsigned int i;
146 TRACE("%i, %u, %p, %p\n", severity, message_types, callback_data, user_data);
148 object = user_data;
150 if (!object->instance->instance)
152 /* instance wasn't yet created, this is a message from the native loader */
153 return VK_FALSE;
156 wine_callback_data = *((VkDebugUtilsMessengerCallbackDataEXT *) callback_data);
158 object_name_infos = heap_calloc(wine_callback_data.objectCount, sizeof(*object_name_infos));
160 for (i = 0; i < wine_callback_data.objectCount; i++)
162 object_name_infos[i].sType = callback_data->pObjects[i].sType;
163 object_name_infos[i].pNext = callback_data->pObjects[i].pNext;
164 object_name_infos[i].objectType = callback_data->pObjects[i].objectType;
165 object_name_infos[i].pObjectName = callback_data->pObjects[i].pObjectName;
167 if (wine_vk_is_type_wrapped(callback_data->pObjects[i].objectType))
169 object_name_infos[i].objectHandle = wine_vk_get_wrapper(object->instance, callback_data->pObjects[i].objectHandle);
170 if (!object_name_infos[i].objectHandle)
172 WARN("handle conversion failed 0x%s\n", wine_dbgstr_longlong(callback_data->pObjects[i].objectHandle));
173 heap_free(object_name_infos);
174 return VK_FALSE;
177 else
179 object_name_infos[i].objectHandle = callback_data->pObjects[i].objectHandle;
183 wine_callback_data.pObjects = object_name_infos;
185 /* applications should always return VK_FALSE */
186 result = object->user_callback(severity, message_types, &wine_callback_data, object->user_data);
188 heap_free(object_name_infos);
190 return result;
193 static VkBool32 debug_report_callback_conversion(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT object_type,
194 uint64_t object_handle, size_t location, int32_t code, const char *layer_prefix, const char *message, void *user_data)
196 struct wine_debug_report_callback *object;
198 TRACE("%#x, %#x, 0x%s, 0x%s, %d, %p, %p, %p\n", flags, object_type, wine_dbgstr_longlong(object_handle),
199 wine_dbgstr_longlong(location), code, layer_prefix, message, user_data);
201 object = user_data;
203 if (!object->instance->instance)
205 /* instance wasn't yet created, this is a message from the native loader */
206 return VK_FALSE;
209 object_handle = wine_vk_get_wrapper(object->instance, object_handle);
210 if (!object_handle)
211 object_type = VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT;
213 return object->user_callback(
214 flags, object_type, object_handle, location, code, layer_prefix, message, object->user_data);
217 static void wine_vk_physical_device_free(struct VkPhysicalDevice_T *phys_dev)
219 if (!phys_dev)
220 return;
222 WINE_VK_REMOVE_HANDLE_MAPPING(phys_dev->instance, phys_dev);
223 heap_free(phys_dev->extensions);
224 heap_free(phys_dev);
227 static struct VkPhysicalDevice_T *wine_vk_physical_device_alloc(struct VkInstance_T *instance,
228 VkPhysicalDevice phys_dev)
230 struct VkPhysicalDevice_T *object;
231 uint32_t num_host_properties, num_properties = 0;
232 VkExtensionProperties *host_properties = NULL;
233 VkResult res;
234 unsigned int i, j;
236 if (!(object = heap_alloc_zero(sizeof(*object))))
237 return NULL;
239 object->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
240 object->instance = instance;
241 object->phys_dev = phys_dev;
243 WINE_VK_ADD_DISPATCHABLE_MAPPING(instance, object, phys_dev);
245 res = instance->funcs.p_vkEnumerateDeviceExtensionProperties(phys_dev,
246 NULL, &num_host_properties, NULL);
247 if (res != VK_SUCCESS)
249 ERR("Failed to enumerate device extensions, res=%d\n", res);
250 goto err;
253 host_properties = heap_calloc(num_host_properties, sizeof(*host_properties));
254 if (!host_properties)
256 ERR("Failed to allocate memory for device properties!\n");
257 goto err;
260 res = instance->funcs.p_vkEnumerateDeviceExtensionProperties(phys_dev,
261 NULL, &num_host_properties, host_properties);
262 if (res != VK_SUCCESS)
264 ERR("Failed to enumerate device extensions, res=%d\n", res);
265 goto err;
268 /* Count list of extensions for which we have an implementation.
269 * TODO: perform translation for platform specific extensions.
271 for (i = 0; i < num_host_properties; i++)
273 if (wine_vk_device_extension_supported(host_properties[i].extensionName))
275 TRACE("Enabling extension '%s' for physical device %p\n", host_properties[i].extensionName, object);
276 num_properties++;
278 else
280 TRACE("Skipping extension '%s', no implementation found in winevulkan.\n", host_properties[i].extensionName);
284 TRACE("Host supported extensions %u, Wine supported extensions %u\n", num_host_properties, num_properties);
286 if (!(object->extensions = heap_calloc(num_properties, sizeof(*object->extensions))))
288 ERR("Failed to allocate memory for device extensions!\n");
289 goto err;
292 for (i = 0, j = 0; i < num_host_properties; i++)
294 if (wine_vk_device_extension_supported(host_properties[i].extensionName))
296 object->extensions[j] = host_properties[i];
297 j++;
300 object->extension_count = num_properties;
302 heap_free(host_properties);
303 return object;
305 err:
306 wine_vk_physical_device_free(object);
307 heap_free(host_properties);
308 return NULL;
311 static void wine_vk_free_command_buffers(struct VkDevice_T *device,
312 struct wine_cmd_pool *pool, uint32_t count, const VkCommandBuffer *buffers)
314 unsigned int i;
316 for (i = 0; i < count; i++)
318 if (!buffers[i])
319 continue;
321 device->funcs.p_vkFreeCommandBuffers(device->device, pool->command_pool, 1, &buffers[i]->command_buffer);
322 list_remove(&buffers[i]->pool_link);
323 WINE_VK_REMOVE_HANDLE_MAPPING(device->phys_dev->instance, buffers[i]);
324 heap_free(buffers[i]);
328 static struct VkQueue_T *wine_vk_device_alloc_queues(struct VkDevice_T *device,
329 uint32_t family_index, uint32_t queue_count, VkDeviceQueueCreateFlags flags)
331 VkDeviceQueueInfo2 queue_info;
332 struct VkQueue_T *queues;
333 unsigned int i;
335 if (!(queues = heap_calloc(queue_count, sizeof(*queues))))
337 ERR("Failed to allocate memory for queues\n");
338 return NULL;
341 for (i = 0; i < queue_count; i++)
343 struct VkQueue_T *queue = &queues[i];
345 queue->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
346 queue->device = device;
347 queue->flags = flags;
349 /* The Vulkan spec says:
351 * "vkGetDeviceQueue must only be used to get queues that were created
352 * with the flags parameter of VkDeviceQueueCreateInfo set to zero."
354 if (flags && device->funcs.p_vkGetDeviceQueue2)
356 queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2;
357 queue_info.pNext = NULL;
358 queue_info.flags = flags;
359 queue_info.queueFamilyIndex = family_index;
360 queue_info.queueIndex = i;
361 device->funcs.p_vkGetDeviceQueue2(device->device, &queue_info, &queue->queue);
363 else
365 device->funcs.p_vkGetDeviceQueue(device->device, family_index, i, &queue->queue);
368 WINE_VK_ADD_DISPATCHABLE_MAPPING(device->phys_dev->instance, queue, queue->queue);
371 return queues;
374 static void wine_vk_device_free_create_info(VkDeviceCreateInfo *create_info)
376 VkDeviceGroupDeviceCreateInfo *group_info;
378 if ((group_info = wine_vk_find_struct(create_info, DEVICE_GROUP_DEVICE_CREATE_INFO)))
380 heap_free((void *)group_info->pPhysicalDevices);
383 free_VkDeviceCreateInfo_struct_chain(create_info);
386 static VkResult wine_vk_device_convert_create_info(const VkDeviceCreateInfo *src,
387 VkDeviceCreateInfo *dst)
389 VkDeviceGroupDeviceCreateInfo *group_info;
390 unsigned int i;
391 VkResult res;
393 *dst = *src;
395 if ((res = convert_VkDeviceCreateInfo_struct_chain(src->pNext, dst)) < 0)
397 WARN("Failed to convert VkDeviceCreateInfo pNext chain, res=%d.\n", res);
398 return res;
401 /* FIXME: convert_VkDeviceCreateInfo_struct_chain() should unwrap handles for us. */
402 if ((group_info = wine_vk_find_struct(dst, DEVICE_GROUP_DEVICE_CREATE_INFO)))
404 VkPhysicalDevice *physical_devices;
406 if (!(physical_devices = heap_calloc(group_info->physicalDeviceCount, sizeof(*physical_devices))))
408 free_VkDeviceCreateInfo_struct_chain(dst);
409 return VK_ERROR_OUT_OF_HOST_MEMORY;
411 for (i = 0; i < group_info->physicalDeviceCount; ++i)
413 physical_devices[i] = group_info->pPhysicalDevices[i]->phys_dev;
415 group_info->pPhysicalDevices = physical_devices;
418 /* Should be filtered out by loader as ICDs don't support layers. */
419 dst->enabledLayerCount = 0;
420 dst->ppEnabledLayerNames = NULL;
422 TRACE("Enabled %u extensions.\n", dst->enabledExtensionCount);
423 for (i = 0; i < dst->enabledExtensionCount; i++)
425 const char *extension_name = dst->ppEnabledExtensionNames[i];
426 TRACE("Extension %u: %s.\n", i, debugstr_a(extension_name));
427 if (!wine_vk_device_extension_supported(extension_name))
429 WARN("Extension %s is not supported.\n", debugstr_a(extension_name));
430 wine_vk_device_free_create_info(dst);
431 return VK_ERROR_EXTENSION_NOT_PRESENT;
435 return VK_SUCCESS;
438 /* Helper function used for freeing a device structure. This function supports full
439 * and partial object cleanups and can thus be used for vkCreateDevice failures.
441 static void wine_vk_device_free(struct VkDevice_T *device)
443 if (!device)
444 return;
446 if (device->queues)
448 unsigned int i;
449 for (i = 0; i < device->max_queue_families; i++)
451 if (device->queues[i] && device->queues[i]->queue)
452 WINE_VK_REMOVE_HANDLE_MAPPING(device->phys_dev->instance, device->queues[i]);
453 heap_free(device->queues[i]);
455 heap_free(device->queues);
456 device->queues = NULL;
459 if (device->device && device->funcs.p_vkDestroyDevice)
461 WINE_VK_REMOVE_HANDLE_MAPPING(device->phys_dev->instance, device);
462 device->funcs.p_vkDestroyDevice(device->device, NULL /* pAllocator */);
465 heap_free(device);
468 static BOOL WINAPI wine_vk_init(INIT_ONCE *once, void *param, void **context)
470 HDC hdc;
472 hdc = GetDC(0);
473 vk_funcs = __wine_get_vulkan_driver(hdc, WINE_VULKAN_DRIVER_VERSION);
474 ReleaseDC(0, hdc);
475 if (!vk_funcs)
476 ERR("Failed to load Wine graphics driver supporting Vulkan.\n");
477 else
478 p_vkEnumerateInstanceVersion = vk_funcs->p_vkGetInstanceProcAddr(NULL, "vkEnumerateInstanceVersion");
480 return TRUE;
483 static void wine_vk_init_once(void)
485 static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
487 InitOnceExecuteOnce(&init_once, wine_vk_init, NULL, NULL);
490 /* Helper function for converting between win32 and host compatible VkInstanceCreateInfo.
491 * This function takes care of extensions handled at winevulkan layer, a Wine graphics
492 * driver is responsible for handling e.g. surface extensions.
494 static VkResult wine_vk_instance_convert_create_info(const VkInstanceCreateInfo *src,
495 VkInstanceCreateInfo *dst, struct VkInstance_T *object)
497 VkDebugUtilsMessengerCreateInfoEXT *debug_utils_messenger;
498 VkDebugReportCallbackCreateInfoEXT *debug_report_callback;
499 VkBaseInStructure *header;
500 unsigned int i;
501 VkResult res;
503 *dst = *src;
505 if ((res = convert_VkInstanceCreateInfo_struct_chain(src->pNext, dst)) < 0)
507 WARN("Failed to convert VkInstanceCreateInfo pNext chain, res=%d.\n", res);
508 return res;
511 object->utils_messenger_count = wine_vk_count_struct(dst, DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT);
512 object->utils_messengers = heap_calloc(object->utils_messenger_count, sizeof(*object->utils_messengers));
513 header = (VkBaseInStructure *) dst;
514 for (i = 0; i < object->utils_messenger_count; i++)
516 header = wine_vk_find_struct(header->pNext, DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT);
517 debug_utils_messenger = (VkDebugUtilsMessengerCreateInfoEXT *) header;
519 object->utils_messengers[i].instance = object;
520 object->utils_messengers[i].debug_messenger = VK_NULL_HANDLE;
521 object->utils_messengers[i].user_callback = debug_utils_messenger->pfnUserCallback;
522 object->utils_messengers[i].user_data = debug_utils_messenger->pUserData;
524 /* convert_VkInstanceCreateInfo_struct_chain already copied the chain,
525 * so we can modify it in-place.
527 debug_utils_messenger->pfnUserCallback = (void *) &debug_utils_callback_conversion;
528 debug_utils_messenger->pUserData = &object->utils_messengers[i];
531 debug_report_callback = wine_vk_find_struct(header->pNext, DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT);
532 if (debug_report_callback)
534 object->default_callback.instance = object;
535 object->default_callback.debug_callback = VK_NULL_HANDLE;
536 object->default_callback.user_callback = debug_report_callback->pfnCallback;
537 object->default_callback.user_data = debug_report_callback->pUserData;
539 debug_report_callback->pfnCallback = (void *) &debug_report_callback_conversion;
540 debug_report_callback->pUserData = &object->default_callback;
543 /* ICDs don't support any layers, so nothing to copy. Modern versions of the loader
544 * filter this data out as well.
546 if (object->quirks & WINEVULKAN_QUIRK_IGNORE_EXPLICIT_LAYERS) {
547 dst->enabledLayerCount = 0;
548 dst->ppEnabledLayerNames = NULL;
549 WARN("Ignoring explicit layers!\n");
550 } else if (dst->enabledLayerCount) {
551 FIXME("Loading explicit layers is not supported by winevulkan!\n");
552 return VK_ERROR_LAYER_NOT_PRESENT;
555 TRACE("Enabled %u instance extensions.\n", dst->enabledExtensionCount);
556 for (i = 0; i < dst->enabledExtensionCount; i++)
558 const char *extension_name = dst->ppEnabledExtensionNames[i];
559 TRACE("Extension %u: %s.\n", i, debugstr_a(extension_name));
560 if (!wine_vk_instance_extension_supported(extension_name))
562 WARN("Extension %s is not supported.\n", debugstr_a(extension_name));
563 free_VkInstanceCreateInfo_struct_chain(dst);
564 return VK_ERROR_EXTENSION_NOT_PRESENT;
566 if (!strcmp(extension_name, "VK_EXT_debug_utils") || !strcmp(extension_name, "VK_EXT_debug_report"))
568 object->enable_wrapper_list = VK_TRUE;
572 return VK_SUCCESS;
575 /* Helper function which stores wrapped physical devices in the instance object. */
576 static VkResult wine_vk_instance_load_physical_devices(struct VkInstance_T *instance)
578 VkPhysicalDevice *tmp_phys_devs;
579 uint32_t phys_dev_count;
580 unsigned int i;
581 VkResult res;
583 res = instance->funcs.p_vkEnumeratePhysicalDevices(instance->instance, &phys_dev_count, NULL);
584 if (res != VK_SUCCESS)
586 ERR("Failed to enumerate physical devices, res=%d\n", res);
587 return res;
589 if (!phys_dev_count)
590 return res;
592 if (!(tmp_phys_devs = heap_calloc(phys_dev_count, sizeof(*tmp_phys_devs))))
593 return VK_ERROR_OUT_OF_HOST_MEMORY;
595 res = instance->funcs.p_vkEnumeratePhysicalDevices(instance->instance, &phys_dev_count, tmp_phys_devs);
596 if (res != VK_SUCCESS)
598 heap_free(tmp_phys_devs);
599 return res;
602 instance->phys_devs = heap_calloc(phys_dev_count, sizeof(*instance->phys_devs));
603 if (!instance->phys_devs)
605 heap_free(tmp_phys_devs);
606 return VK_ERROR_OUT_OF_HOST_MEMORY;
609 /* Wrap each native physical device handle into a dispatchable object for the ICD loader. */
610 for (i = 0; i < phys_dev_count; i++)
612 struct VkPhysicalDevice_T *phys_dev = wine_vk_physical_device_alloc(instance, tmp_phys_devs[i]);
613 if (!phys_dev)
615 ERR("Unable to allocate memory for physical device!\n");
616 heap_free(tmp_phys_devs);
617 return VK_ERROR_OUT_OF_HOST_MEMORY;
620 instance->phys_devs[i] = phys_dev;
621 instance->phys_dev_count = i + 1;
623 instance->phys_dev_count = phys_dev_count;
625 heap_free(tmp_phys_devs);
626 return VK_SUCCESS;
629 static struct VkPhysicalDevice_T *wine_vk_instance_wrap_physical_device(struct VkInstance_T *instance,
630 VkPhysicalDevice physical_device)
632 unsigned int i;
634 for (i = 0; i < instance->phys_dev_count; ++i)
636 struct VkPhysicalDevice_T *current = instance->phys_devs[i];
637 if (current->phys_dev == physical_device)
638 return current;
641 ERR("Unrecognized physical device %p.\n", physical_device);
642 return NULL;
645 /* Helper function used for freeing an instance structure. This function supports full
646 * and partial object cleanups and can thus be used for vkCreateInstance failures.
648 static void wine_vk_instance_free(struct VkInstance_T *instance)
650 if (!instance)
651 return;
653 if (instance->phys_devs)
655 unsigned int i;
657 for (i = 0; i < instance->phys_dev_count; i++)
659 wine_vk_physical_device_free(instance->phys_devs[i]);
661 heap_free(instance->phys_devs);
664 if (instance->instance)
666 vk_funcs->p_vkDestroyInstance(instance->instance, NULL /* allocator */);
667 WINE_VK_REMOVE_HANDLE_MAPPING(instance, instance);
670 heap_free(instance->utils_messengers);
672 heap_free(instance);
675 VkResult WINAPI wine_vkAllocateCommandBuffers(VkDevice device,
676 const VkCommandBufferAllocateInfo *allocate_info, VkCommandBuffer *buffers)
678 struct wine_cmd_pool *pool;
679 VkResult res = VK_SUCCESS;
680 unsigned int i;
682 TRACE("%p, %p, %p\n", device, allocate_info, buffers);
684 pool = wine_cmd_pool_from_handle(allocate_info->commandPool);
686 memset(buffers, 0, allocate_info->commandBufferCount * sizeof(*buffers));
688 for (i = 0; i < allocate_info->commandBufferCount; i++)
690 #if defined(USE_STRUCT_CONVERSION)
691 VkCommandBufferAllocateInfo_host allocate_info_host;
692 #else
693 VkCommandBufferAllocateInfo allocate_info_host;
694 #endif
695 /* TODO: future extensions (none yet) may require pNext conversion. */
696 allocate_info_host.pNext = allocate_info->pNext;
697 allocate_info_host.sType = allocate_info->sType;
698 allocate_info_host.commandPool = pool->command_pool;
699 allocate_info_host.level = allocate_info->level;
700 allocate_info_host.commandBufferCount = 1;
702 TRACE("Allocating command buffer %u from pool 0x%s.\n",
703 i, wine_dbgstr_longlong(allocate_info_host.commandPool));
705 if (!(buffers[i] = heap_alloc_zero(sizeof(**buffers))))
707 res = VK_ERROR_OUT_OF_HOST_MEMORY;
708 break;
711 buffers[i]->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
712 buffers[i]->device = device;
713 list_add_tail(&pool->command_buffers, &buffers[i]->pool_link);
714 res = device->funcs.p_vkAllocateCommandBuffers(device->device,
715 &allocate_info_host, &buffers[i]->command_buffer);
716 WINE_VK_ADD_DISPATCHABLE_MAPPING(device->phys_dev->instance, buffers[i], buffers[i]->command_buffer);
717 if (res != VK_SUCCESS)
719 ERR("Failed to allocate command buffer, res=%d.\n", res);
720 buffers[i]->command_buffer = VK_NULL_HANDLE;
721 break;
725 if (res != VK_SUCCESS)
727 wine_vk_free_command_buffers(device, pool, i + 1, buffers);
728 memset(buffers, 0, allocate_info->commandBufferCount * sizeof(*buffers));
731 return res;
734 void WINAPI wine_vkCmdExecuteCommands(VkCommandBuffer buffer, uint32_t count,
735 const VkCommandBuffer *buffers)
737 VkCommandBuffer *tmp_buffers;
738 unsigned int i;
740 TRACE("%p %u %p\n", buffer, count, buffers);
742 if (!buffers || !count)
743 return;
745 /* Unfortunately we need a temporary buffer as our command buffers are wrapped.
746 * This call is called often and if a performance concern, we may want to use
747 * alloca as we shouldn't need much memory and it needs to be cleaned up after
748 * the call anyway.
750 if (!(tmp_buffers = heap_alloc(count * sizeof(*tmp_buffers))))
752 ERR("Failed to allocate memory for temporary command buffers\n");
753 return;
756 for (i = 0; i < count; i++)
757 tmp_buffers[i] = buffers[i]->command_buffer;
759 buffer->device->funcs.p_vkCmdExecuteCommands(buffer->command_buffer, count, tmp_buffers);
761 heap_free(tmp_buffers);
764 VkResult WINAPI wine_vkCreateDevice(VkPhysicalDevice phys_dev,
765 const VkDeviceCreateInfo *create_info,
766 const VkAllocationCallbacks *allocator, VkDevice *device)
768 VkDeviceCreateInfo create_info_host;
769 uint32_t max_queue_families;
770 struct VkDevice_T *object;
771 unsigned int i;
772 VkResult res;
774 TRACE("%p, %p, %p, %p\n", phys_dev, create_info, allocator, device);
776 if (allocator)
777 FIXME("Support for allocation callbacks not implemented yet\n");
779 if (TRACE_ON(vulkan))
781 VkPhysicalDeviceProperties properties;
783 wine_vkGetPhysicalDeviceProperties(phys_dev, &properties);
785 TRACE("Device name: %s.\n", debugstr_a(properties.deviceName));
786 TRACE("Vendor ID: %#x, Device ID: %#x.\n", properties.vendorID, properties.deviceID);
787 TRACE("Driver version: %#x.\n", properties.driverVersion);
790 if (!(object = heap_alloc_zero(sizeof(*object))))
791 return VK_ERROR_OUT_OF_HOST_MEMORY;
793 object->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
794 object->phys_dev = phys_dev;
796 res = wine_vk_device_convert_create_info(create_info, &create_info_host);
797 if (res != VK_SUCCESS)
798 goto fail;
800 res = phys_dev->instance->funcs.p_vkCreateDevice(phys_dev->phys_dev,
801 &create_info_host, NULL /* allocator */, &object->device);
802 wine_vk_device_free_create_info(&create_info_host);
803 WINE_VK_ADD_DISPATCHABLE_MAPPING(phys_dev->instance, object, object->device);
804 if (res != VK_SUCCESS)
806 WARN("Failed to create device, res=%d.\n", res);
807 goto fail;
810 /* Just load all function pointers we are aware off. The loader takes care of filtering.
811 * We use vkGetDeviceProcAddr as opposed to vkGetInstanceProcAddr for efficiency reasons
812 * as functions pass through fewer dispatch tables within the loader.
814 #define USE_VK_FUNC(name) \
815 object->funcs.p_##name = (void *)vk_funcs->p_vkGetDeviceProcAddr(object->device, #name); \
816 if (object->funcs.p_##name == NULL) \
817 TRACE("Not found '%s'.\n", #name);
818 ALL_VK_DEVICE_FUNCS()
819 #undef USE_VK_FUNC
821 /* We need to cache all queues within the device as each requires wrapping since queues are
822 * dispatchable objects.
824 phys_dev->instance->funcs.p_vkGetPhysicalDeviceQueueFamilyProperties(phys_dev->phys_dev,
825 &max_queue_families, NULL);
826 object->max_queue_families = max_queue_families;
827 TRACE("Max queue families: %u.\n", object->max_queue_families);
829 if (!(object->queues = heap_calloc(max_queue_families, sizeof(*object->queues))))
831 res = VK_ERROR_OUT_OF_HOST_MEMORY;
832 goto fail;
835 for (i = 0; i < create_info_host.queueCreateInfoCount; i++)
837 uint32_t flags = create_info_host.pQueueCreateInfos[i].flags;
838 uint32_t family_index = create_info_host.pQueueCreateInfos[i].queueFamilyIndex;
839 uint32_t queue_count = create_info_host.pQueueCreateInfos[i].queueCount;
841 TRACE("Queue family index %u, queue count %u.\n", family_index, queue_count);
843 if (!(object->queues[family_index] = wine_vk_device_alloc_queues(object, family_index, queue_count, flags)))
845 ERR("Failed to allocate memory for queues.\n");
846 res = VK_ERROR_OUT_OF_HOST_MEMORY;
847 goto fail;
851 object->quirks = phys_dev->instance->quirks;
853 *device = object;
854 TRACE("Created device %p (native device %p).\n", object, object->device);
855 return VK_SUCCESS;
857 fail:
858 wine_vk_device_free(object);
859 return res;
862 VkResult WINAPI wine_vkCreateInstance(const VkInstanceCreateInfo *create_info,
863 const VkAllocationCallbacks *allocator, VkInstance *instance)
865 VkInstanceCreateInfo create_info_host;
866 const VkApplicationInfo *app_info;
867 struct VkInstance_T *object;
868 VkResult res;
870 TRACE("create_info %p, allocator %p, instance %p\n", create_info, allocator, instance);
872 wine_vk_init_once();
873 if (!vk_funcs)
874 return VK_ERROR_INITIALIZATION_FAILED;
876 if (allocator)
877 FIXME("Support for allocation callbacks not implemented yet\n");
879 if (!(object = heap_alloc_zero(sizeof(*object))))
881 ERR("Failed to allocate memory for instance\n");
882 return VK_ERROR_OUT_OF_HOST_MEMORY;
884 object->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
885 list_init(&object->wrappers);
886 InitializeSRWLock(&object->wrapper_lock);
888 res = wine_vk_instance_convert_create_info(create_info, &create_info_host, object);
889 if (res != VK_SUCCESS)
891 wine_vk_instance_free(object);
892 return res;
895 res = vk_funcs->p_vkCreateInstance(&create_info_host, NULL /* allocator */, &object->instance);
896 free_VkInstanceCreateInfo_struct_chain(&create_info_host);
897 if (res != VK_SUCCESS)
899 ERR("Failed to create instance, res=%d\n", res);
900 wine_vk_instance_free(object);
901 return res;
904 WINE_VK_ADD_DISPATCHABLE_MAPPING(object, object, object->instance);
906 /* Load all instance functions we are aware of. Note the loader takes care
907 * of any filtering for extensions which were not requested, but which the
908 * ICD may support.
910 #define USE_VK_FUNC(name) \
911 object->funcs.p_##name = (void *)vk_funcs->p_vkGetInstanceProcAddr(object->instance, #name);
912 ALL_VK_INSTANCE_FUNCS()
913 #undef USE_VK_FUNC
915 /* Cache physical devices for vkEnumeratePhysicalDevices within the instance as
916 * each vkPhysicalDevice is a dispatchable object, which means we need to wrap
917 * the native physical devices and present those to the application.
918 * Cleanup happens as part of wine_vkDestroyInstance.
920 res = wine_vk_instance_load_physical_devices(object);
921 if (res != VK_SUCCESS)
923 ERR("Failed to load physical devices, res=%d\n", res);
924 wine_vk_instance_free(object);
925 return res;
928 if ((app_info = create_info->pApplicationInfo))
930 TRACE("Application name %s, application version %#x.\n",
931 debugstr_a(app_info->pApplicationName), app_info->applicationVersion);
932 TRACE("Engine name %s, engine version %#x.\n", debugstr_a(app_info->pEngineName),
933 app_info->engineVersion);
934 TRACE("API version %#x.\n", app_info->apiVersion);
936 if (app_info->pEngineName && !strcmp(app_info->pEngineName, "idTech"))
937 object->quirks |= WINEVULKAN_QUIRK_GET_DEVICE_PROC_ADDR;
940 object->quirks |= WINEVULKAN_QUIRK_ADJUST_MAX_IMAGE_COUNT;
942 *instance = object;
943 TRACE("Created instance %p (native instance %p).\n", object, object->instance);
944 return VK_SUCCESS;
947 void WINAPI wine_vkDestroyDevice(VkDevice device, const VkAllocationCallbacks *allocator)
949 TRACE("%p %p\n", device, allocator);
951 if (allocator)
952 FIXME("Support for allocation callbacks not implemented yet\n");
954 wine_vk_device_free(device);
957 void WINAPI wine_vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks *allocator)
959 TRACE("%p, %p\n", instance, allocator);
961 if (allocator)
962 FIXME("Support allocation allocators\n");
964 wine_vk_instance_free(instance);
967 VkResult WINAPI wine_vkEnumerateDeviceExtensionProperties(VkPhysicalDevice phys_dev,
968 const char *layer_name, uint32_t *count, VkExtensionProperties *properties)
970 TRACE("%p, %p, %p, %p\n", phys_dev, layer_name, count, properties);
972 /* This shouldn't get called with layer_name set, the ICD loader prevents it. */
973 if (layer_name)
975 ERR("Layer enumeration not supported from ICD.\n");
976 return VK_ERROR_LAYER_NOT_PRESENT;
979 if (!properties)
981 *count = phys_dev->extension_count;
982 return VK_SUCCESS;
985 *count = min(*count, phys_dev->extension_count);
986 memcpy(properties, phys_dev->extensions, *count * sizeof(*properties));
988 TRACE("Returning %u extensions.\n", *count);
989 return *count < phys_dev->extension_count ? VK_INCOMPLETE : VK_SUCCESS;
992 VkResult WINAPI wine_vkEnumerateInstanceExtensionProperties(const char *layer_name,
993 uint32_t *count, VkExtensionProperties *properties)
995 uint32_t num_properties = 0, num_host_properties;
996 VkExtensionProperties *host_properties;
997 unsigned int i, j;
998 VkResult res;
1000 TRACE("%p, %p, %p\n", layer_name, count, properties);
1002 if (layer_name)
1004 WARN("Layer enumeration not supported from ICD.\n");
1005 return VK_ERROR_LAYER_NOT_PRESENT;
1008 wine_vk_init_once();
1009 if (!vk_funcs)
1011 *count = 0;
1012 return VK_SUCCESS;
1015 res = vk_funcs->p_vkEnumerateInstanceExtensionProperties(NULL, &num_host_properties, NULL);
1016 if (res != VK_SUCCESS)
1017 return res;
1019 if (!(host_properties = heap_calloc(num_host_properties, sizeof(*host_properties))))
1020 return VK_ERROR_OUT_OF_HOST_MEMORY;
1022 res = vk_funcs->p_vkEnumerateInstanceExtensionProperties(NULL, &num_host_properties, host_properties);
1023 if (res != VK_SUCCESS)
1025 ERR("Failed to retrieve host properties, res=%d.\n", res);
1026 heap_free(host_properties);
1027 return res;
1030 /* The Wine graphics driver provides us with all extensions supported by the host side
1031 * including extension fixup (e.g. VK_KHR_xlib_surface -> VK_KHR_win32_surface). It is
1032 * up to us here to filter the list down to extensions for which we have thunks.
1034 for (i = 0; i < num_host_properties; i++)
1036 if (wine_vk_instance_extension_supported(host_properties[i].extensionName))
1037 num_properties++;
1038 else
1039 TRACE("Instance extension '%s' is not supported.\n", host_properties[i].extensionName);
1042 if (!properties)
1044 TRACE("Returning %u extensions.\n", num_properties);
1045 *count = num_properties;
1046 heap_free(host_properties);
1047 return VK_SUCCESS;
1050 for (i = 0, j = 0; i < num_host_properties && j < *count; i++)
1052 if (wine_vk_instance_extension_supported(host_properties[i].extensionName))
1054 TRACE("Enabling extension '%s'.\n", host_properties[i].extensionName);
1055 properties[j++] = host_properties[i];
1058 *count = min(*count, num_properties);
1060 heap_free(host_properties);
1061 return *count < num_properties ? VK_INCOMPLETE : VK_SUCCESS;
1064 VkResult WINAPI wine_vkEnumerateInstanceLayerProperties(uint32_t *count, VkLayerProperties *properties)
1066 TRACE("%p, %p\n", count, properties);
1068 *count = 0;
1069 return VK_SUCCESS;
1072 VkResult WINAPI wine_vkEnumerateInstanceVersion(uint32_t *version)
1074 VkResult res;
1076 TRACE("%p\n", version);
1078 wine_vk_init_once();
1080 if (p_vkEnumerateInstanceVersion)
1082 res = p_vkEnumerateInstanceVersion(version);
1084 else
1086 *version = VK_API_VERSION_1_0;
1087 res = VK_SUCCESS;
1090 TRACE("API version %u.%u.%u.\n",
1091 VK_VERSION_MAJOR(*version), VK_VERSION_MINOR(*version), VK_VERSION_PATCH(*version));
1092 *version = min(WINE_VK_VERSION, *version);
1093 return res;
1096 VkResult WINAPI wine_vkEnumeratePhysicalDevices(VkInstance instance, uint32_t *count,
1097 VkPhysicalDevice *devices)
1099 unsigned int i;
1101 TRACE("%p %p %p\n", instance, count, devices);
1103 if (!devices)
1105 *count = instance->phys_dev_count;
1106 return VK_SUCCESS;
1109 *count = min(*count, instance->phys_dev_count);
1110 for (i = 0; i < *count; i++)
1112 devices[i] = instance->phys_devs[i];
1115 TRACE("Returning %u devices.\n", *count);
1116 return *count < instance->phys_dev_count ? VK_INCOMPLETE : VK_SUCCESS;
1119 void WINAPI wine_vkFreeCommandBuffers(VkDevice device, VkCommandPool pool_handle,
1120 uint32_t count, const VkCommandBuffer *buffers)
1122 struct wine_cmd_pool *pool = wine_cmd_pool_from_handle(pool_handle);
1124 TRACE("%p, 0x%s, %u, %p\n", device, wine_dbgstr_longlong(pool_handle), count, buffers);
1126 wine_vk_free_command_buffers(device, pool, count, buffers);
1129 PFN_vkVoidFunction WINAPI wine_vkGetDeviceProcAddr(VkDevice device, const char *name)
1131 void *func;
1132 TRACE("%p, %s\n", device, debugstr_a(name));
1134 /* The spec leaves return value undefined for a NULL device, let's just return NULL. */
1135 if (!device || !name)
1136 return NULL;
1138 /* Per the spec, we are only supposed to return device functions as in functions
1139 * for which the first parameter is vkDevice or a child of vkDevice like a
1140 * vkCommandBuffer or vkQueue.
1141 * Loader takes care of filtering of extensions which are enabled or not.
1143 func = wine_vk_get_device_proc_addr(name);
1144 if (func)
1145 return func;
1147 /* vkGetDeviceProcAddr was intended for loading device and subdevice functions.
1148 * idTech 6 titles such as Doom and Wolfenstein II, however use it also for
1149 * loading of instance functions. This is undefined behavior as the specification
1150 * disallows using any of the returned function pointers outside of device /
1151 * subdevice objects. The games don't actually use the function pointers and if they
1152 * did, they would crash as VkInstance / VkPhysicalDevice parameters need unwrapping.
1153 * Khronos clarified behavior in the Vulkan spec and expects drivers to get updated,
1154 * however it would require both driver and game fixes.
1155 * https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/issues/2323
1156 * https://github.com/KhronosGroup/Vulkan-Docs/issues/655
1158 if (device->quirks & WINEVULKAN_QUIRK_GET_DEVICE_PROC_ADDR
1159 && (func = wine_vk_get_instance_proc_addr(name)))
1161 WARN("Returning instance function %s.\n", debugstr_a(name));
1162 return func;
1165 WARN("Unsupported device function: %s.\n", debugstr_a(name));
1166 return NULL;
1169 void WINAPI wine_vkGetDeviceQueue(VkDevice device, uint32_t family_index,
1170 uint32_t queue_index, VkQueue *queue)
1172 TRACE("%p, %u, %u, %p\n", device, family_index, queue_index, queue);
1174 *queue = &device->queues[family_index][queue_index];
1177 void WINAPI wine_vkGetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2 *info, VkQueue *queue)
1179 struct VkQueue_T *matching_queue;
1180 const VkBaseInStructure *chain;
1182 TRACE("%p, %p, %p\n", device, info, queue);
1184 if ((chain = info->pNext))
1185 FIXME("Ignoring a linked structure of type %u.\n", chain->sType);
1187 matching_queue = &device->queues[info->queueFamilyIndex][info->queueIndex];
1188 if (matching_queue->flags != info->flags)
1190 WARN("No matching flags were specified %#x, %#x.\n", matching_queue->flags, info->flags);
1191 matching_queue = VK_NULL_HANDLE;
1193 *queue = matching_queue;
1196 PFN_vkVoidFunction WINAPI wine_vkGetInstanceProcAddr(VkInstance instance, const char *name)
1198 void *func;
1200 TRACE("%p, %s\n", instance, debugstr_a(name));
1202 if (!name)
1203 return NULL;
1205 /* vkGetInstanceProcAddr can load most Vulkan functions when an instance is passed in, however
1206 * for a NULL instance it can only load global functions.
1208 func = wine_vk_get_global_proc_addr(name);
1209 if (func)
1211 return func;
1213 if (!instance)
1215 WARN("Global function %s not found.\n", debugstr_a(name));
1216 return NULL;
1219 func = wine_vk_get_instance_proc_addr(name);
1220 if (func) return func;
1222 /* vkGetInstanceProcAddr also loads any children of instance, so device functions as well. */
1223 func = wine_vk_get_device_proc_addr(name);
1224 if (func) return func;
1226 WARN("Unsupported device or instance function: %s.\n", debugstr_a(name));
1227 return NULL;
1230 void * WINAPI wine_vk_icdGetInstanceProcAddr(VkInstance instance, const char *name)
1232 TRACE("%p, %s\n", instance, debugstr_a(name));
1234 /* Initial version of the Vulkan ICD spec required vkGetInstanceProcAddr to be
1235 * exported. vk_icdGetInstanceProcAddr was added later to separate ICD calls from
1236 * Vulkan API. One of them in our case should forward to the other, so just forward
1237 * to the older vkGetInstanceProcAddr.
1239 return wine_vkGetInstanceProcAddr(instance, name);
1242 VkResult WINAPI wine_vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *supported_version)
1244 uint32_t req_version;
1246 TRACE("%p\n", supported_version);
1248 /* The spec is not clear how to handle this. Mesa drivers don't check, but it
1249 * is probably best to not explode. VK_INCOMPLETE seems to be the closest value.
1251 if (!supported_version)
1252 return VK_INCOMPLETE;
1254 req_version = *supported_version;
1255 *supported_version = min(req_version, WINE_VULKAN_ICD_VERSION);
1256 TRACE("Loader requested ICD version %u, returning %u\n", req_version, *supported_version);
1258 return VK_SUCCESS;
1261 VkResult WINAPI wine_vkQueueSubmit(VkQueue queue, uint32_t count,
1262 const VkSubmitInfo *submits, VkFence fence)
1264 VkSubmitInfo *submits_host;
1265 VkResult res;
1266 VkCommandBuffer *command_buffers;
1267 unsigned int i, j, num_command_buffers;
1269 TRACE("%p %u %p 0x%s\n", queue, count, submits, wine_dbgstr_longlong(fence));
1271 if (count == 0)
1273 return queue->device->funcs.p_vkQueueSubmit(queue->queue, 0, NULL, fence);
1276 submits_host = heap_calloc(count, sizeof(*submits_host));
1277 if (!submits_host)
1279 ERR("Unable to allocate memory for submit buffers!\n");
1280 return VK_ERROR_OUT_OF_HOST_MEMORY;
1283 for (i = 0; i < count; i++)
1285 memcpy(&submits_host[i], &submits[i], sizeof(*submits_host));
1287 num_command_buffers = submits[i].commandBufferCount;
1288 command_buffers = heap_calloc(num_command_buffers, sizeof(*command_buffers));
1289 if (!command_buffers)
1291 ERR("Unable to allocate memory for command buffers!\n");
1292 res = VK_ERROR_OUT_OF_HOST_MEMORY;
1293 goto done;
1296 for (j = 0; j < num_command_buffers; j++)
1298 command_buffers[j] = submits[i].pCommandBuffers[j]->command_buffer;
1300 submits_host[i].pCommandBuffers = command_buffers;
1303 res = queue->device->funcs.p_vkQueueSubmit(queue->queue, count, submits_host, fence);
1305 done:
1306 for (i = 0; i < count; i++)
1308 heap_free((void *)submits_host[i].pCommandBuffers);
1310 heap_free(submits_host);
1312 TRACE("Returning %d\n", res);
1313 return res;
1316 VkResult WINAPI wine_vkCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo *info,
1317 const VkAllocationCallbacks *allocator, VkCommandPool *command_pool)
1319 struct wine_cmd_pool *object;
1320 VkResult res;
1322 TRACE("%p, %p, %p, %p\n", device, info, allocator, command_pool);
1324 if (allocator)
1325 FIXME("Support for allocation callbacks not implemented yet\n");
1327 if (!(object = heap_alloc_zero(sizeof(*object))))
1328 return VK_ERROR_OUT_OF_HOST_MEMORY;
1330 list_init(&object->command_buffers);
1332 res = device->funcs.p_vkCreateCommandPool(device->device, info, NULL, &object->command_pool);
1334 if (res == VK_SUCCESS)
1336 WINE_VK_ADD_NON_DISPATCHABLE_MAPPING(device->phys_dev->instance, object, object->command_pool);
1337 *command_pool = wine_cmd_pool_to_handle(object);
1339 else
1341 heap_free(object);
1344 return res;
1347 void WINAPI wine_vkDestroyCommandPool(VkDevice device, VkCommandPool handle,
1348 const VkAllocationCallbacks *allocator)
1350 struct wine_cmd_pool *pool = wine_cmd_pool_from_handle(handle);
1351 struct VkCommandBuffer_T *buffer, *cursor;
1353 TRACE("%p, 0x%s, %p\n", device, wine_dbgstr_longlong(handle), allocator);
1355 if (!handle)
1356 return;
1358 if (allocator)
1359 FIXME("Support for allocation callbacks not implemented yet\n");
1361 /* The Vulkan spec says:
1363 * "When a pool is destroyed, all command buffers allocated from the pool are freed."
1365 LIST_FOR_EACH_ENTRY_SAFE(buffer, cursor, &pool->command_buffers, struct VkCommandBuffer_T, pool_link)
1367 WINE_VK_REMOVE_HANDLE_MAPPING(device->phys_dev->instance, buffer);
1368 heap_free(buffer);
1371 WINE_VK_REMOVE_HANDLE_MAPPING(device->phys_dev->instance, pool);
1373 device->funcs.p_vkDestroyCommandPool(device->device, pool->command_pool, NULL);
1374 heap_free(pool);
1377 static VkResult wine_vk_enumerate_physical_device_groups(struct VkInstance_T *instance,
1378 VkResult (*p_vkEnumeratePhysicalDeviceGroups)(VkInstance, uint32_t *, VkPhysicalDeviceGroupProperties *),
1379 uint32_t *count, VkPhysicalDeviceGroupProperties *properties)
1381 unsigned int i, j;
1382 VkResult res;
1384 res = p_vkEnumeratePhysicalDeviceGroups(instance->instance, count, properties);
1385 if (res < 0 || !properties)
1386 return res;
1388 for (i = 0; i < *count; ++i)
1390 VkPhysicalDeviceGroupProperties *current = &properties[i];
1391 for (j = 0; j < current->physicalDeviceCount; ++j)
1393 VkPhysicalDevice dev = current->physicalDevices[j];
1394 if (!(current->physicalDevices[j] = wine_vk_instance_wrap_physical_device(instance, dev)))
1395 return VK_ERROR_INITIALIZATION_FAILED;
1399 return res;
1402 VkResult WINAPI wine_vkEnumeratePhysicalDeviceGroups(VkInstance instance,
1403 uint32_t *count, VkPhysicalDeviceGroupProperties *properties)
1405 TRACE("%p, %p, %p\n", instance, count, properties);
1406 return wine_vk_enumerate_physical_device_groups(instance,
1407 instance->funcs.p_vkEnumeratePhysicalDeviceGroups, count, properties);
1410 VkResult WINAPI wine_vkEnumeratePhysicalDeviceGroupsKHR(VkInstance instance,
1411 uint32_t *count, VkPhysicalDeviceGroupProperties *properties)
1413 TRACE("%p, %p, %p\n", instance, count, properties);
1414 return wine_vk_enumerate_physical_device_groups(instance,
1415 instance->funcs.p_vkEnumeratePhysicalDeviceGroupsKHR, count, properties);
1418 void WINAPI wine_vkGetPhysicalDeviceExternalFenceProperties(VkPhysicalDevice phys_dev,
1419 const VkPhysicalDeviceExternalFenceInfo *fence_info, VkExternalFenceProperties *properties)
1421 TRACE("%p, %p, %p\n", phys_dev, fence_info, properties);
1422 properties->exportFromImportedHandleTypes = 0;
1423 properties->compatibleHandleTypes = 0;
1424 properties->externalFenceFeatures = 0;
1427 void WINAPI wine_vkGetPhysicalDeviceExternalFencePropertiesKHR(VkPhysicalDevice phys_dev,
1428 const VkPhysicalDeviceExternalFenceInfo *fence_info, VkExternalFenceProperties *properties)
1430 TRACE("%p, %p, %p\n", phys_dev, fence_info, properties);
1431 properties->exportFromImportedHandleTypes = 0;
1432 properties->compatibleHandleTypes = 0;
1433 properties->externalFenceFeatures = 0;
1436 void WINAPI wine_vkGetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice phys_dev,
1437 const VkPhysicalDeviceExternalBufferInfo *buffer_info, VkExternalBufferProperties *properties)
1439 TRACE("%p, %p, %p\n", phys_dev, buffer_info, properties);
1440 memset(&properties->externalMemoryProperties, 0, sizeof(properties->externalMemoryProperties));
1443 void WINAPI wine_vkGetPhysicalDeviceExternalBufferPropertiesKHR(VkPhysicalDevice phys_dev,
1444 const VkPhysicalDeviceExternalBufferInfo *buffer_info, VkExternalBufferProperties *properties)
1446 TRACE("%p, %p, %p\n", phys_dev, buffer_info, properties);
1447 memset(&properties->externalMemoryProperties, 0, sizeof(properties->externalMemoryProperties));
1450 VkResult WINAPI wine_vkGetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice phys_dev,
1451 const VkPhysicalDeviceImageFormatInfo2 *format_info, VkImageFormatProperties2 *properties)
1453 VkExternalImageFormatProperties *external_image_properties;
1454 VkResult res;
1456 TRACE("%p, %p, %p\n", phys_dev, format_info, properties);
1458 res = thunk_vkGetPhysicalDeviceImageFormatProperties2(phys_dev, format_info, properties);
1460 if ((external_image_properties = wine_vk_find_struct(properties, EXTERNAL_IMAGE_FORMAT_PROPERTIES)))
1462 VkExternalMemoryProperties *p = &external_image_properties->externalMemoryProperties;
1463 p->externalMemoryFeatures = 0;
1464 p->exportFromImportedHandleTypes = 0;
1465 p->compatibleHandleTypes = 0;
1468 return res;
1471 VkResult WINAPI wine_vkGetPhysicalDeviceImageFormatProperties2KHR(VkPhysicalDevice phys_dev,
1472 const VkPhysicalDeviceImageFormatInfo2 *format_info, VkImageFormatProperties2 *properties)
1474 VkExternalImageFormatProperties *external_image_properties;
1475 VkResult res;
1477 TRACE("%p, %p, %p\n", phys_dev, format_info, properties);
1479 res = thunk_vkGetPhysicalDeviceImageFormatProperties2KHR(phys_dev, format_info, properties);
1481 if ((external_image_properties = wine_vk_find_struct(properties, EXTERNAL_IMAGE_FORMAT_PROPERTIES)))
1483 VkExternalMemoryProperties *p = &external_image_properties->externalMemoryProperties;
1484 p->externalMemoryFeatures = 0;
1485 p->exportFromImportedHandleTypes = 0;
1486 p->compatibleHandleTypes = 0;
1489 return res;
1492 /* From ntdll/unix/sync.c */
1493 #define NANOSECONDS_IN_A_SECOND 1000000000
1494 #define TICKSPERSEC 10000000
1496 static inline VkTimeDomainEXT get_performance_counter_time_domain(void)
1498 #if !defined(__APPLE__) && defined(HAVE_CLOCK_GETTIME)
1499 # ifdef CLOCK_MONOTONIC_RAW
1500 return VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT;
1501 # else
1502 return VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT;
1503 # endif
1504 #else
1505 FIXME("No mapping for VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT on this platform.\n");
1506 return VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT;
1507 #endif
1510 static VkTimeDomainEXT map_to_host_time_domain(VkTimeDomainEXT domain)
1512 /* Matches ntdll/unix/sync.c's performance counter implementation. */
1513 if (domain == VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT)
1514 return get_performance_counter_time_domain();
1516 return domain;
1519 static inline uint64_t convert_monotonic_timestamp(uint64_t value)
1521 return value / (NANOSECONDS_IN_A_SECOND / TICKSPERSEC);
1524 static inline uint64_t convert_timestamp(VkTimeDomainEXT host_domain, VkTimeDomainEXT target_domain, uint64_t value)
1526 if (host_domain == target_domain)
1527 return value;
1529 /* Convert between MONOTONIC time in ns -> QueryPerformanceCounter */
1530 if ((host_domain == VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT || host_domain == VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT)
1531 && target_domain == VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT)
1532 return convert_monotonic_timestamp(value);
1534 FIXME("Couldn't translate between host domain %d and target domain %d\n", host_domain, target_domain);
1535 return value;
1538 VkResult WINAPI wine_vkGetCalibratedTimestampsEXT(VkDevice device,
1539 uint32_t timestamp_count, const VkCalibratedTimestampInfoEXT *timestamp_infos,
1540 uint64_t *timestamps, uint64_t *max_deviation)
1542 VkCalibratedTimestampInfoEXT* host_timestamp_infos;
1543 unsigned int i;
1544 VkResult res;
1545 TRACE("%p, %u, %p, %p, %p\n", device, timestamp_count, timestamp_infos, timestamps, max_deviation);
1547 if (!(host_timestamp_infos = heap_alloc(sizeof(VkCalibratedTimestampInfoEXT) * timestamp_count)))
1548 return VK_ERROR_OUT_OF_HOST_MEMORY;
1550 for (i = 0; i < timestamp_count; i++)
1552 host_timestamp_infos[i].sType = timestamp_infos[i].sType;
1553 host_timestamp_infos[i].pNext = timestamp_infos[i].pNext;
1554 host_timestamp_infos[i].timeDomain = map_to_host_time_domain(timestamp_infos[i].timeDomain);
1557 res = device->funcs.p_vkGetCalibratedTimestampsEXT(device->device, timestamp_count, host_timestamp_infos, timestamps, max_deviation);
1558 if (res != VK_SUCCESS)
1559 return res;
1561 for (i = 0; i < timestamp_count; i++)
1562 timestamps[i] = convert_timestamp(host_timestamp_infos[i].timeDomain, timestamp_infos[i].timeDomain, timestamps[i]);
1564 heap_free(host_timestamp_infos);
1566 return res;
1569 VkResult WINAPI wine_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT(VkPhysicalDevice phys_dev,
1570 uint32_t *time_domain_count, VkTimeDomainEXT *time_domains)
1572 BOOL supports_device = FALSE, supports_monotonic = FALSE, supports_monotonic_raw = FALSE;
1573 const VkTimeDomainEXT performance_counter_domain = get_performance_counter_time_domain();
1574 VkTimeDomainEXT *host_time_domains;
1575 uint32_t host_time_domain_count;
1576 VkTimeDomainEXT out_time_domains[2];
1577 uint32_t out_time_domain_count;
1578 unsigned int i;
1579 VkResult res;
1581 TRACE("%p, %p, %p\n", phys_dev, time_domain_count, time_domains);
1583 /* Find out the time domains supported on the host */
1584 res = phys_dev->instance->funcs.p_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT(phys_dev->phys_dev, &host_time_domain_count, NULL);
1585 if (res != VK_SUCCESS)
1586 return res;
1588 if (!(host_time_domains = heap_alloc(sizeof(VkTimeDomainEXT) * host_time_domain_count)))
1589 return VK_ERROR_OUT_OF_HOST_MEMORY;
1591 res = phys_dev->instance->funcs.p_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT(phys_dev->phys_dev, &host_time_domain_count, host_time_domains);
1592 if (res != VK_SUCCESS)
1594 heap_free(host_time_domains);
1595 return res;
1598 for (i = 0; i < host_time_domain_count; i++)
1600 if (host_time_domains[i] == VK_TIME_DOMAIN_DEVICE_EXT)
1601 supports_device = TRUE;
1602 else if (host_time_domains[i] == VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT)
1603 supports_monotonic = TRUE;
1604 else if (host_time_domains[i] == VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT)
1605 supports_monotonic_raw = TRUE;
1606 else
1607 FIXME("Unknown time domain %d\n", host_time_domains[i]);
1610 heap_free(host_time_domains);
1612 out_time_domain_count = 0;
1614 /* Map our monotonic times -> QPC */
1615 if (supports_monotonic_raw && performance_counter_domain == VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT)
1616 out_time_domains[out_time_domain_count++] = VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT;
1617 else if (supports_monotonic && performance_counter_domain == VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT)
1618 out_time_domains[out_time_domain_count++] = VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT;
1619 else
1620 FIXME("VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT not supported on this platform.\n");
1622 /* Forward the device domain time */
1623 if (supports_device)
1624 out_time_domains[out_time_domain_count++] = VK_TIME_DOMAIN_DEVICE_EXT;
1626 /* Send the count/domains back to the app */
1627 if (!time_domains)
1629 *time_domain_count = out_time_domain_count;
1630 return VK_SUCCESS;
1633 for (i = 0; i < min(*time_domain_count, out_time_domain_count); i++)
1634 time_domains[i] = out_time_domains[i];
1636 res = *time_domain_count < out_time_domain_count ? VK_INCOMPLETE : VK_SUCCESS;
1637 *time_domain_count = out_time_domain_count;
1638 return res;
1641 static HANDLE get_display_device_init_mutex(void)
1643 static const WCHAR init_mutexW[] = {'d','i','s','p','l','a','y','_','d','e','v','i','c','e','_','i','n','i','t',0};
1644 HANDLE mutex = CreateMutexW(NULL, FALSE, init_mutexW);
1646 WaitForSingleObject(mutex, INFINITE);
1647 return mutex;
1650 static void release_display_device_init_mutex(HANDLE mutex)
1652 ReleaseMutex(mutex);
1653 CloseHandle(mutex);
1656 /* Wait until graphics driver is loaded by explorer */
1657 static void wait_graphics_driver_ready(void)
1659 static BOOL ready = FALSE;
1661 if (!ready)
1663 SendMessageW(GetDesktopWindow(), WM_NULL, 0, 0);
1664 ready = TRUE;
1668 static void fill_luid_property(VkPhysicalDeviceProperties2 *properties2)
1670 static const WCHAR pci[] = {'P','C','I',0};
1671 VkPhysicalDeviceIDProperties *id;
1672 SP_DEVINFO_DATA device_data;
1673 DWORD type, device_idx = 0;
1674 HDEVINFO devinfo;
1675 HANDLE mutex;
1676 GUID uuid;
1677 LUID luid;
1679 if (!(id = wine_vk_find_struct(properties2, PHYSICAL_DEVICE_ID_PROPERTIES)))
1680 return;
1682 wait_graphics_driver_ready();
1683 mutex = get_display_device_init_mutex();
1684 devinfo = SetupDiGetClassDevsW(&GUID_DEVCLASS_DISPLAY, pci, NULL, 0);
1685 device_data.cbSize = sizeof(device_data);
1686 while (SetupDiEnumDeviceInfo(devinfo, device_idx++, &device_data))
1688 if (!SetupDiGetDevicePropertyW(devinfo, &device_data, &WINE_DEVPROPKEY_GPU_VULKAN_UUID,
1689 &type, (BYTE *)&uuid, sizeof(uuid), NULL, 0))
1690 continue;
1692 if (!IsEqualGUID(&uuid, id->deviceUUID))
1693 continue;
1695 if (SetupDiGetDevicePropertyW(devinfo, &device_data, &DEVPROPKEY_GPU_LUID, &type,
1696 (BYTE *)&luid, sizeof(luid), NULL, 0))
1698 memcpy(&id->deviceLUID, &luid, sizeof(id->deviceLUID));
1699 id->deviceLUIDValid = VK_TRUE;
1700 id->deviceNodeMask = 1;
1701 break;
1704 SetupDiDestroyDeviceInfoList(devinfo);
1705 release_display_device_init_mutex(mutex);
1707 TRACE("deviceName:%s deviceLUIDValid:%d LUID:%08x:%08x deviceNodeMask:%#x.\n",
1708 properties2->properties.deviceName, id->deviceLUIDValid, luid.HighPart, luid.LowPart,
1709 id->deviceNodeMask);
1712 void WINAPI wine_vkGetPhysicalDeviceProperties2(VkPhysicalDevice phys_dev,
1713 VkPhysicalDeviceProperties2 *properties2)
1715 TRACE("%p, %p\n", phys_dev, properties2);
1717 thunk_vkGetPhysicalDeviceProperties2(phys_dev, properties2);
1718 fill_luid_property(properties2);
1721 void WINAPI wine_vkGetPhysicalDeviceProperties2KHR(VkPhysicalDevice phys_dev,
1722 VkPhysicalDeviceProperties2 *properties2)
1724 TRACE("%p, %p\n", phys_dev, properties2);
1726 thunk_vkGetPhysicalDeviceProperties2KHR(phys_dev, properties2);
1727 fill_luid_property(properties2);
1730 void WINAPI wine_vkGetPhysicalDeviceExternalSemaphoreProperties(VkPhysicalDevice phys_dev,
1731 const VkPhysicalDeviceExternalSemaphoreInfo *semaphore_info, VkExternalSemaphoreProperties *properties)
1733 TRACE("%p, %p, %p\n", phys_dev, semaphore_info, properties);
1734 properties->exportFromImportedHandleTypes = 0;
1735 properties->compatibleHandleTypes = 0;
1736 properties->externalSemaphoreFeatures = 0;
1739 void WINAPI wine_vkGetPhysicalDeviceExternalSemaphorePropertiesKHR(VkPhysicalDevice phys_dev,
1740 const VkPhysicalDeviceExternalSemaphoreInfo *semaphore_info, VkExternalSemaphoreProperties *properties)
1742 TRACE("%p, %p, %p\n", phys_dev, semaphore_info, properties);
1743 properties->exportFromImportedHandleTypes = 0;
1744 properties->compatibleHandleTypes = 0;
1745 properties->externalSemaphoreFeatures = 0;
1748 VkResult WINAPI wine_vkSetPrivateDataEXT(VkDevice device, VkObjectType object_type, uint64_t object_handle,
1749 VkPrivateDataSlotEXT private_data_slot, uint64_t data)
1751 TRACE("%p, %#x, 0x%s, 0x%s, 0x%s\n", device, object_type, wine_dbgstr_longlong(object_handle),
1752 wine_dbgstr_longlong(private_data_slot), wine_dbgstr_longlong(data));
1754 object_handle = wine_vk_unwrap_handle(object_type, object_handle);
1755 return device->funcs.p_vkSetPrivateDataEXT(device->device, object_type, object_handle, private_data_slot, data);
1758 void WINAPI wine_vkGetPrivateDataEXT(VkDevice device, VkObjectType object_type, uint64_t object_handle,
1759 VkPrivateDataSlotEXT private_data_slot, uint64_t *data)
1761 TRACE("%p, %#x, 0x%s, 0x%s, %p\n", device, object_type, wine_dbgstr_longlong(object_handle),
1762 wine_dbgstr_longlong(private_data_slot), data);
1764 object_handle = wine_vk_unwrap_handle(object_type, object_handle);
1765 device->funcs.p_vkGetPrivateDataEXT(device->device, object_type, object_handle, private_data_slot, data);
1768 VkResult WINAPI wine_vkCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR *create_info,
1769 const VkAllocationCallbacks *allocator, VkSwapchainKHR *swapchain)
1771 VkSwapchainCreateInfoKHR native_info;
1773 TRACE("%p, %p, %p, %p\n", device, create_info, allocator, swapchain);
1775 native_info = *create_info;
1776 native_info.surface = wine_surface_from_handle(create_info->surface)->driver_surface;
1778 return thunk_vkCreateSwapchainKHR(device, &native_info, allocator, swapchain);
1781 VkResult WINAPI wine_vkCreateWin32SurfaceKHR(VkInstance instance,
1782 const VkWin32SurfaceCreateInfoKHR *createInfo, const VkAllocationCallbacks *allocator, VkSurfaceKHR *surface)
1784 struct wine_surface *object;
1785 VkResult res;
1787 TRACE("%p, %p, %p, %p\n", instance, createInfo, allocator, surface);
1789 if (allocator)
1790 FIXME("Support for allocation callbacks not implemented yet\n");
1792 object = heap_alloc_zero(sizeof(*object));
1794 if (!object)
1795 return VK_ERROR_OUT_OF_HOST_MEMORY;
1797 res = instance->funcs.p_vkCreateWin32SurfaceKHR(instance->instance, createInfo, NULL, &object->driver_surface);
1799 if (res != VK_SUCCESS)
1801 heap_free(object);
1802 return res;
1805 object->surface = vk_funcs->p_wine_get_native_surface(object->driver_surface);
1807 WINE_VK_ADD_NON_DISPATCHABLE_MAPPING(instance, object, object->surface);
1809 *surface = wine_surface_to_handle(object);
1811 return VK_SUCCESS;
1814 void WINAPI wine_vkDestroySurfaceKHR(VkInstance instance, VkSurfaceKHR surface, const VkAllocationCallbacks *allocator)
1816 struct wine_surface *object = wine_surface_from_handle(surface);
1818 TRACE("%p, 0x%s, %p\n", instance, wine_dbgstr_longlong(surface), allocator);
1820 if (!object)
1821 return;
1823 instance->funcs.p_vkDestroySurfaceKHR(instance->instance, object->driver_surface, NULL);
1825 WINE_VK_REMOVE_HANDLE_MAPPING(instance, object);
1826 heap_free(object);
1829 VkResult WINAPI wine_vkGetPhysicalDeviceSurfaceFormats2KHR(VkPhysicalDevice phys_dev,
1830 const VkPhysicalDeviceSurfaceInfo2KHR *surface_info, uint32_t *formats_count, VkSurfaceFormat2KHR *formats)
1832 VkPhysicalDeviceSurfaceInfo2KHR native_info;
1834 TRACE("%p, %p, %p, %p\n", phys_dev, surface_info, formats_count, formats);
1836 native_info = *surface_info;
1837 native_info.surface = wine_surface_from_handle(surface_info->surface)->driver_surface;
1839 return thunk_vkGetPhysicalDeviceSurfaceFormats2KHR(phys_dev, &native_info, formats_count, formats);
1842 static inline void adjust_max_image_count(VkPhysicalDevice phys_dev, VkSurfaceCapabilitiesKHR* capabilities)
1844 /* Many Windows games, for example Strange Brigade, No Man's Sky, Path of Exile
1845 * and World War Z, do not expect that maxImageCount can be set to 0.
1846 * A value of 0 means that there is no limit on the number of images.
1847 * Nvidia reports 8 on Windows, AMD 16.
1848 * https://vulkan.gpuinfo.org/displayreport.php?id=9122#surface
1849 * https://vulkan.gpuinfo.org/displayreport.php?id=9121#surface
1851 if ((phys_dev->instance->quirks & WINEVULKAN_QUIRK_ADJUST_MAX_IMAGE_COUNT) && !capabilities->maxImageCount)
1853 capabilities->maxImageCount = max(capabilities->minImageCount, 16);
1857 VkResult WINAPI wine_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice phys_dev,
1858 VkSurfaceKHR surface, VkSurfaceCapabilitiesKHR *capabilities)
1860 VkResult res;
1862 TRACE("%p, 0x%s, %p\n", phys_dev, wine_dbgstr_longlong(surface), capabilities);
1864 res = thunk_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(phys_dev, surface, capabilities);
1866 if (res == VK_SUCCESS)
1867 adjust_max_image_count(phys_dev, capabilities);
1869 return res;
1872 VkResult WINAPI wine_vkGetPhysicalDeviceSurfaceCapabilities2KHR(VkPhysicalDevice phys_dev,
1873 const VkPhysicalDeviceSurfaceInfo2KHR *surface_info, VkSurfaceCapabilities2KHR *capabilities)
1875 VkPhysicalDeviceSurfaceInfo2KHR native_info;
1876 VkResult res;
1878 TRACE("%p, %p, %p\n", phys_dev, surface_info, capabilities);
1880 native_info = *surface_info;
1881 native_info.surface = wine_surface_from_handle(surface_info->surface)->driver_surface;
1883 res = thunk_vkGetPhysicalDeviceSurfaceCapabilities2KHR(phys_dev, &native_info, capabilities);
1885 if (res == VK_SUCCESS)
1886 adjust_max_image_count(phys_dev, &capabilities->surfaceCapabilities);
1888 return res;
1891 VkResult WINAPI wine_vkCreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT *create_info,
1892 const VkAllocationCallbacks *allocator, VkDebugUtilsMessengerEXT *messenger)
1894 VkDebugUtilsMessengerCreateInfoEXT wine_create_info;
1895 struct wine_debug_utils_messenger *object;
1896 VkResult res;
1898 TRACE("%p, %p, %p, %p\n", instance, create_info, allocator, messenger);
1900 if (allocator)
1901 FIXME("Support for allocation callbacks not implemented yet\n");
1903 if (!(object = heap_alloc_zero(sizeof(*object))))
1904 return VK_ERROR_OUT_OF_HOST_MEMORY;
1906 object->instance = instance;
1907 object->user_callback = create_info->pfnUserCallback;
1908 object->user_data = create_info->pUserData;
1910 wine_create_info = *create_info;
1912 wine_create_info.pfnUserCallback = (void *) &debug_utils_callback_conversion;
1913 wine_create_info.pUserData = object;
1915 res = instance->funcs.p_vkCreateDebugUtilsMessengerEXT(instance->instance, &wine_create_info, NULL, &object->debug_messenger);
1917 if (res != VK_SUCCESS)
1919 heap_free(object);
1920 return res;
1923 WINE_VK_ADD_NON_DISPATCHABLE_MAPPING(instance, object, object->debug_messenger);
1924 *messenger = wine_debug_utils_messenger_to_handle(object);
1926 return VK_SUCCESS;
1929 void WINAPI wine_vkDestroyDebugUtilsMessengerEXT(
1930 VkInstance instance, VkDebugUtilsMessengerEXT messenger, const VkAllocationCallbacks *allocator)
1932 struct wine_debug_utils_messenger *object;
1934 TRACE("%p, 0x%s, %p\n", instance, wine_dbgstr_longlong(messenger), allocator);
1936 object = wine_debug_utils_messenger_from_handle(messenger);
1938 if (!object)
1939 return;
1941 instance->funcs.p_vkDestroyDebugUtilsMessengerEXT(instance->instance, object->debug_messenger, NULL);
1942 WINE_VK_REMOVE_HANDLE_MAPPING(instance, object);
1944 heap_free(object);
1947 void WINAPI wine_vkSubmitDebugUtilsMessageEXT(VkInstance instance, VkDebugUtilsMessageSeverityFlagBitsEXT severity,
1948 VkDebugUtilsMessageTypeFlagsEXT types, const VkDebugUtilsMessengerCallbackDataEXT *callback_data)
1950 VkDebugUtilsMessengerCallbackDataEXT native_callback_data;
1951 VkDebugUtilsObjectNameInfoEXT *object_names;
1952 unsigned int i;
1954 TRACE("%p, %#x, %#x, %p\n", instance, severity, types, callback_data);
1956 native_callback_data = *callback_data;
1957 object_names = heap_calloc(callback_data->objectCount, sizeof(*object_names));
1958 memcpy(object_names, callback_data->pObjects, callback_data->objectCount * sizeof(*object_names));
1959 native_callback_data.pObjects = object_names;
1961 for (i = 0; i < callback_data->objectCount; i++)
1963 object_names[i].objectHandle =
1964 wine_vk_unwrap_handle(callback_data->pObjects[i].objectType, callback_data->pObjects[i].objectHandle);
1967 thunk_vkSubmitDebugUtilsMessageEXT(instance, severity, types, &native_callback_data);
1969 heap_free(object_names);
1972 VkResult WINAPI wine_vkSetDebugUtilsObjectTagEXT(VkDevice device, const VkDebugUtilsObjectTagInfoEXT *tag_info)
1974 VkDebugUtilsObjectTagInfoEXT wine_tag_info;
1976 TRACE("%p, %p\n", device, tag_info);
1978 wine_tag_info = *tag_info;
1979 wine_tag_info.objectHandle = wine_vk_unwrap_handle(tag_info->objectType, tag_info->objectHandle);
1981 return thunk_vkSetDebugUtilsObjectTagEXT(device, &wine_tag_info);
1984 VkResult WINAPI wine_vkSetDebugUtilsObjectNameEXT(VkDevice device, const VkDebugUtilsObjectNameInfoEXT *name_info)
1986 VkDebugUtilsObjectNameInfoEXT wine_name_info;
1988 TRACE("%p, %p\n", device, name_info);
1990 wine_name_info = *name_info;
1991 wine_name_info.objectHandle = wine_vk_unwrap_handle(name_info->objectType, name_info->objectHandle);
1993 return thunk_vkSetDebugUtilsObjectNameEXT(device, &wine_name_info);
1996 VkResult WINAPI wine_vkCreateDebugReportCallbackEXT(VkInstance instance, const VkDebugReportCallbackCreateInfoEXT *create_info,
1997 const VkAllocationCallbacks *allocator, VkDebugReportCallbackEXT *callback)
1999 VkDebugReportCallbackCreateInfoEXT wine_create_info;
2000 struct wine_debug_report_callback *object;
2001 VkResult res;
2003 TRACE("%p, %p, %p, %p\n", instance, create_info, allocator, callback);
2005 if (allocator)
2006 FIXME("Support for allocation callbacks not implemented yet\n");
2008 if (!(object = heap_alloc_zero(sizeof(*object))))
2009 return VK_ERROR_OUT_OF_HOST_MEMORY;
2011 object->instance = instance;
2012 object->user_callback = create_info->pfnCallback;
2013 object->user_data = create_info->pUserData;
2015 wine_create_info = *create_info;
2017 wine_create_info.pfnCallback = (void *) debug_report_callback_conversion;
2018 wine_create_info.pUserData = object;
2020 res = instance->funcs.p_vkCreateDebugReportCallbackEXT(instance->instance, &wine_create_info, NULL, &object->debug_callback);
2022 if (res != VK_SUCCESS)
2024 heap_free(object);
2025 return res;
2028 WINE_VK_ADD_NON_DISPATCHABLE_MAPPING(instance, object, object->debug_callback);
2029 *callback = wine_debug_report_callback_to_handle(object);
2031 return VK_SUCCESS;
2034 void WINAPI wine_vkDestroyDebugReportCallbackEXT(
2035 VkInstance instance, VkDebugReportCallbackEXT callback, const VkAllocationCallbacks *allocator)
2037 struct wine_debug_report_callback *object;
2039 TRACE("%p, 0x%s, %p\n", instance, wine_dbgstr_longlong(callback), allocator);
2041 object = wine_debug_report_callback_from_handle(callback);
2043 if (!object)
2044 return;
2046 instance->funcs.p_vkDestroyDebugReportCallbackEXT(instance->instance, object->debug_callback, NULL);
2048 WINE_VK_REMOVE_HANDLE_MAPPING(instance, object);
2050 heap_free(object);
2053 void WINAPI wine_vkDebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT object_type,
2054 uint64_t object, size_t location, int32_t code, const char *layer_prefix, const char *message)
2056 TRACE("%p, %#x, %#x, 0x%s, 0x%s, %d, %p, %p\n", instance, flags, object_type, wine_dbgstr_longlong(object),
2057 wine_dbgstr_longlong(location), code, layer_prefix, message);
2059 object = wine_vk_unwrap_handle(object_type, object);
2061 instance->funcs.p_vkDebugReportMessageEXT(
2062 instance->instance, flags, object_type, object, location, code, layer_prefix, message);
2065 VkResult WINAPI wine_vkDebugMarkerSetObjectTagEXT(VkDevice device, const VkDebugMarkerObjectTagInfoEXT *tag_info)
2067 VkDebugMarkerObjectTagInfoEXT wine_tag_info;
2069 TRACE("%p, %p\n", device, tag_info);
2071 wine_tag_info = *tag_info;
2072 wine_tag_info.object = wine_vk_unwrap_handle(tag_info->objectType, tag_info->object);
2074 return thunk_vkDebugMarkerSetObjectTagEXT(device, &wine_tag_info);
2077 VkResult WINAPI wine_vkDebugMarkerSetObjectNameEXT(VkDevice device, const VkDebugMarkerObjectNameInfoEXT *name_info)
2079 VkDebugMarkerObjectNameInfoEXT wine_name_info;
2081 TRACE("%p, %p\n", device, name_info);
2083 wine_name_info = *name_info;
2084 wine_name_info.object = wine_vk_unwrap_handle(name_info->objectType, name_info->object);
2086 return thunk_vkDebugMarkerSetObjectNameEXT(device, &wine_name_info);
2089 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, void *reserved)
2091 TRACE("%p, %u, %p\n", hinst, reason, reserved);
2093 switch (reason)
2095 case DLL_PROCESS_ATTACH:
2096 hinstance = hinst;
2097 DisableThreadLibraryCalls(hinst);
2098 break;
2100 return TRUE;
2103 static const struct vulkan_func vk_global_dispatch_table[] =
2105 /* These functions must call wine_vk_init_once() before accessing vk_funcs. */
2106 {"vkCreateInstance", &wine_vkCreateInstance},
2107 {"vkEnumerateInstanceExtensionProperties", &wine_vkEnumerateInstanceExtensionProperties},
2108 {"vkEnumerateInstanceLayerProperties", &wine_vkEnumerateInstanceLayerProperties},
2109 {"vkEnumerateInstanceVersion", &wine_vkEnumerateInstanceVersion},
2110 {"vkGetInstanceProcAddr", &wine_vkGetInstanceProcAddr},
2113 static void *wine_vk_get_global_proc_addr(const char *name)
2115 unsigned int i;
2117 for (i = 0; i < ARRAY_SIZE(vk_global_dispatch_table); i++)
2119 if (strcmp(name, vk_global_dispatch_table[i].name) == 0)
2121 TRACE("Found name=%s in global table\n", debugstr_a(name));
2122 return vk_global_dispatch_table[i].func;
2125 return NULL;
2129 * Wrapper around driver vkGetInstanceProcAddr implementation.
2130 * Allows winelib applications to access Vulkan functions with Wine
2131 * additions and native ABI.
2133 void *native_vkGetInstanceProcAddrWINE(VkInstance instance, const char *name)
2135 wine_vk_init_once();
2136 if (!vk_funcs)
2137 return NULL;
2139 return vk_funcs->p_vkGetInstanceProcAddr(instance, name);
2143 static const WCHAR winevulkan_json_resW[] = {'w','i','n','e','v','u','l','k','a','n','_','j','s','o','n',0};
2144 static const WCHAR winevulkan_json_pathW[] = {'\\','w','i','n','e','v','u','l','k','a','n','.','j','s','o','n',0};
2145 static const WCHAR vulkan_driversW[] = {'S','o','f','t','w','a','r','e','\\','K','h','r','o','n','o','s','\\',
2146 'V','u','l','k','a','n','\\','D','r','i','v','e','r','s',0};
2148 HRESULT WINAPI DllRegisterServer(void)
2150 WCHAR json_path[MAX_PATH];
2151 HRSRC rsrc;
2152 const char *data;
2153 DWORD datalen, written, zero = 0;
2154 HANDLE file;
2155 HKEY key;
2157 /* Create the JSON manifest and registry key to register this ICD with the official Vulkan loader. */
2158 TRACE("\n");
2159 rsrc = FindResourceW(hinstance, winevulkan_json_resW, (const WCHAR *)RT_RCDATA);
2160 data = LockResource(LoadResource(hinstance, rsrc));
2161 datalen = SizeofResource(hinstance, rsrc);
2163 GetSystemDirectoryW(json_path, ARRAY_SIZE(json_path));
2164 lstrcatW(json_path, winevulkan_json_pathW);
2165 file = CreateFileW(json_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2166 if (file == INVALID_HANDLE_VALUE)
2168 ERR("Unable to create JSON manifest.\n");
2169 return E_UNEXPECTED;
2171 WriteFile(file, data, datalen, &written, NULL);
2172 CloseHandle(file);
2174 if (!RegCreateKeyExW(HKEY_LOCAL_MACHINE, vulkan_driversW, 0, NULL, 0, KEY_SET_VALUE, NULL, &key, NULL))
2176 RegSetValueExW(key, json_path, 0, REG_DWORD, (const BYTE *)&zero, sizeof(zero));
2177 RegCloseKey(key);
2179 return S_OK;
2182 HRESULT WINAPI DllUnregisterServer(void)
2184 WCHAR json_path[MAX_PATH];
2185 HKEY key;
2187 /* Remove the JSON manifest and registry key */
2188 TRACE("\n");
2189 GetSystemDirectoryW(json_path, ARRAY_SIZE(json_path));
2190 lstrcatW(json_path, winevulkan_json_pathW);
2191 DeleteFileW(json_path);
2193 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, vulkan_driversW, 0, KEY_SET_VALUE, &key) == ERROR_SUCCESS)
2195 RegDeleteValueW(key, json_path);
2196 RegCloseKey(key);
2199 return S_OK;