xmllite: Drop superfluous casts to self.
[wine.git] / dlls / winevulkan / vulkan.c
blobb0a8559fe2c96fe66300e3e0cf6a922b390bade4
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 if (!properties)
1070 *count = 0;
1071 return VK_SUCCESS;
1074 return VK_ERROR_LAYER_NOT_PRESENT;
1077 VkResult WINAPI wine_vkEnumerateInstanceVersion(uint32_t *version)
1079 VkResult res;
1081 TRACE("%p\n", version);
1083 wine_vk_init_once();
1085 if (p_vkEnumerateInstanceVersion)
1087 res = p_vkEnumerateInstanceVersion(version);
1089 else
1091 *version = VK_API_VERSION_1_0;
1092 res = VK_SUCCESS;
1095 TRACE("API version %u.%u.%u.\n",
1096 VK_VERSION_MAJOR(*version), VK_VERSION_MINOR(*version), VK_VERSION_PATCH(*version));
1097 *version = min(WINE_VK_VERSION, *version);
1098 return res;
1101 VkResult WINAPI wine_vkEnumeratePhysicalDevices(VkInstance instance, uint32_t *count,
1102 VkPhysicalDevice *devices)
1104 unsigned int i;
1106 TRACE("%p %p %p\n", instance, count, devices);
1108 if (!devices)
1110 *count = instance->phys_dev_count;
1111 return VK_SUCCESS;
1114 *count = min(*count, instance->phys_dev_count);
1115 for (i = 0; i < *count; i++)
1117 devices[i] = instance->phys_devs[i];
1120 TRACE("Returning %u devices.\n", *count);
1121 return *count < instance->phys_dev_count ? VK_INCOMPLETE : VK_SUCCESS;
1124 void WINAPI wine_vkFreeCommandBuffers(VkDevice device, VkCommandPool pool_handle,
1125 uint32_t count, const VkCommandBuffer *buffers)
1127 struct wine_cmd_pool *pool = wine_cmd_pool_from_handle(pool_handle);
1129 TRACE("%p, 0x%s, %u, %p\n", device, wine_dbgstr_longlong(pool_handle), count, buffers);
1131 wine_vk_free_command_buffers(device, pool, count, buffers);
1134 PFN_vkVoidFunction WINAPI wine_vkGetDeviceProcAddr(VkDevice device, const char *name)
1136 void *func;
1137 TRACE("%p, %s\n", device, debugstr_a(name));
1139 /* The spec leaves return value undefined for a NULL device, let's just return NULL. */
1140 if (!device || !name)
1141 return NULL;
1143 /* Per the spec, we are only supposed to return device functions as in functions
1144 * for which the first parameter is vkDevice or a child of vkDevice like a
1145 * vkCommandBuffer or vkQueue.
1146 * Loader takes care of filtering of extensions which are enabled or not.
1148 func = wine_vk_get_device_proc_addr(name);
1149 if (func)
1150 return func;
1152 /* vkGetDeviceProcAddr was intended for loading device and subdevice functions.
1153 * idTech 6 titles such as Doom and Wolfenstein II, however use it also for
1154 * loading of instance functions. This is undefined behavior as the specification
1155 * disallows using any of the returned function pointers outside of device /
1156 * subdevice objects. The games don't actually use the function pointers and if they
1157 * did, they would crash as VkInstance / VkPhysicalDevice parameters need unwrapping.
1158 * Khronos clarified behavior in the Vulkan spec and expects drivers to get updated,
1159 * however it would require both driver and game fixes.
1160 * https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/issues/2323
1161 * https://github.com/KhronosGroup/Vulkan-Docs/issues/655
1163 if (device->quirks & WINEVULKAN_QUIRK_GET_DEVICE_PROC_ADDR
1164 && (func = wine_vk_get_instance_proc_addr(name)))
1166 WARN("Returning instance function %s.\n", debugstr_a(name));
1167 return func;
1170 WARN("Unsupported device function: %s.\n", debugstr_a(name));
1171 return NULL;
1174 void WINAPI wine_vkGetDeviceQueue(VkDevice device, uint32_t family_index,
1175 uint32_t queue_index, VkQueue *queue)
1177 TRACE("%p, %u, %u, %p\n", device, family_index, queue_index, queue);
1179 *queue = &device->queues[family_index][queue_index];
1182 void WINAPI wine_vkGetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2 *info, VkQueue *queue)
1184 struct VkQueue_T *matching_queue;
1185 const VkBaseInStructure *chain;
1187 TRACE("%p, %p, %p\n", device, info, queue);
1189 if ((chain = info->pNext))
1190 FIXME("Ignoring a linked structure of type %u.\n", chain->sType);
1192 matching_queue = &device->queues[info->queueFamilyIndex][info->queueIndex];
1193 if (matching_queue->flags != info->flags)
1195 WARN("No matching flags were specified %#x, %#x.\n", matching_queue->flags, info->flags);
1196 matching_queue = VK_NULL_HANDLE;
1198 *queue = matching_queue;
1201 PFN_vkVoidFunction WINAPI wine_vkGetInstanceProcAddr(VkInstance instance, const char *name)
1203 void *func;
1205 TRACE("%p, %s\n", instance, debugstr_a(name));
1207 if (!name)
1208 return NULL;
1210 /* vkGetInstanceProcAddr can load most Vulkan functions when an instance is passed in, however
1211 * for a NULL instance it can only load global functions.
1213 func = wine_vk_get_global_proc_addr(name);
1214 if (func)
1216 return func;
1218 if (!instance)
1220 WARN("Global function %s not found.\n", debugstr_a(name));
1221 return NULL;
1224 func = wine_vk_get_instance_proc_addr(name);
1225 if (func) return func;
1227 /* vkGetInstanceProcAddr also loads any children of instance, so device functions as well. */
1228 func = wine_vk_get_device_proc_addr(name);
1229 if (func) return func;
1231 WARN("Unsupported device or instance function: %s.\n", debugstr_a(name));
1232 return NULL;
1235 void * WINAPI wine_vk_icdGetInstanceProcAddr(VkInstance instance, const char *name)
1237 TRACE("%p, %s\n", instance, debugstr_a(name));
1239 /* Initial version of the Vulkan ICD spec required vkGetInstanceProcAddr to be
1240 * exported. vk_icdGetInstanceProcAddr was added later to separate ICD calls from
1241 * Vulkan API. One of them in our case should forward to the other, so just forward
1242 * to the older vkGetInstanceProcAddr.
1244 return wine_vkGetInstanceProcAddr(instance, name);
1247 VkResult WINAPI wine_vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *supported_version)
1249 uint32_t req_version;
1251 TRACE("%p\n", supported_version);
1253 /* The spec is not clear how to handle this. Mesa drivers don't check, but it
1254 * is probably best to not explode. VK_INCOMPLETE seems to be the closest value.
1256 if (!supported_version)
1257 return VK_INCOMPLETE;
1259 req_version = *supported_version;
1260 *supported_version = min(req_version, WINE_VULKAN_ICD_VERSION);
1261 TRACE("Loader requested ICD version %u, returning %u\n", req_version, *supported_version);
1263 return VK_SUCCESS;
1266 VkResult WINAPI wine_vkQueueSubmit(VkQueue queue, uint32_t count,
1267 const VkSubmitInfo *submits, VkFence fence)
1269 VkSubmitInfo *submits_host;
1270 VkResult res;
1271 VkCommandBuffer *command_buffers;
1272 unsigned int i, j, num_command_buffers;
1274 TRACE("%p %u %p 0x%s\n", queue, count, submits, wine_dbgstr_longlong(fence));
1276 if (count == 0)
1278 return queue->device->funcs.p_vkQueueSubmit(queue->queue, 0, NULL, fence);
1281 submits_host = heap_calloc(count, sizeof(*submits_host));
1282 if (!submits_host)
1284 ERR("Unable to allocate memory for submit buffers!\n");
1285 return VK_ERROR_OUT_OF_HOST_MEMORY;
1288 for (i = 0; i < count; i++)
1290 memcpy(&submits_host[i], &submits[i], sizeof(*submits_host));
1292 num_command_buffers = submits[i].commandBufferCount;
1293 command_buffers = heap_calloc(num_command_buffers, sizeof(*submits_host));
1294 if (!command_buffers)
1296 ERR("Unable to allocate memory for command buffers!\n");
1297 res = VK_ERROR_OUT_OF_HOST_MEMORY;
1298 goto done;
1301 for (j = 0; j < num_command_buffers; j++)
1303 command_buffers[j] = submits[i].pCommandBuffers[j]->command_buffer;
1305 submits_host[i].pCommandBuffers = command_buffers;
1308 res = queue->device->funcs.p_vkQueueSubmit(queue->queue, count, submits_host, fence);
1310 done:
1311 for (i = 0; i < count; i++)
1313 heap_free((void *)submits_host[i].pCommandBuffers);
1315 heap_free(submits_host);
1317 TRACE("Returning %d\n", res);
1318 return res;
1321 VkResult WINAPI wine_vkCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo *info,
1322 const VkAllocationCallbacks *allocator, VkCommandPool *command_pool)
1324 struct wine_cmd_pool *object;
1325 VkResult res;
1327 TRACE("%p, %p, %p, %p\n", device, info, allocator, command_pool);
1329 if (allocator)
1330 FIXME("Support for allocation callbacks not implemented yet\n");
1332 if (!(object = heap_alloc_zero(sizeof(*object))))
1333 return VK_ERROR_OUT_OF_HOST_MEMORY;
1335 list_init(&object->command_buffers);
1337 res = device->funcs.p_vkCreateCommandPool(device->device, info, NULL, &object->command_pool);
1339 if (res == VK_SUCCESS)
1341 WINE_VK_ADD_NON_DISPATCHABLE_MAPPING(device->phys_dev->instance, object, object->command_pool);
1342 *command_pool = wine_cmd_pool_to_handle(object);
1344 else
1346 heap_free(object);
1349 return res;
1352 void WINAPI wine_vkDestroyCommandPool(VkDevice device, VkCommandPool handle,
1353 const VkAllocationCallbacks *allocator)
1355 struct wine_cmd_pool *pool = wine_cmd_pool_from_handle(handle);
1356 struct VkCommandBuffer_T *buffer, *cursor;
1358 TRACE("%p, 0x%s, %p\n", device, wine_dbgstr_longlong(handle), allocator);
1360 if (!handle)
1361 return;
1363 if (allocator)
1364 FIXME("Support for allocation callbacks not implemented yet\n");
1366 /* The Vulkan spec says:
1368 * "When a pool is destroyed, all command buffers allocated from the pool are freed."
1370 LIST_FOR_EACH_ENTRY_SAFE(buffer, cursor, &pool->command_buffers, struct VkCommandBuffer_T, pool_link)
1372 WINE_VK_REMOVE_HANDLE_MAPPING(device->phys_dev->instance, buffer);
1373 heap_free(buffer);
1376 WINE_VK_REMOVE_HANDLE_MAPPING(device->phys_dev->instance, pool);
1378 device->funcs.p_vkDestroyCommandPool(device->device, pool->command_pool, NULL);
1379 heap_free(pool);
1382 static VkResult wine_vk_enumerate_physical_device_groups(struct VkInstance_T *instance,
1383 VkResult (*p_vkEnumeratePhysicalDeviceGroups)(VkInstance, uint32_t *, VkPhysicalDeviceGroupProperties *),
1384 uint32_t *count, VkPhysicalDeviceGroupProperties *properties)
1386 unsigned int i, j;
1387 VkResult res;
1389 res = p_vkEnumeratePhysicalDeviceGroups(instance->instance, count, properties);
1390 if (res < 0 || !properties)
1391 return res;
1393 for (i = 0; i < *count; ++i)
1395 VkPhysicalDeviceGroupProperties *current = &properties[i];
1396 for (j = 0; j < current->physicalDeviceCount; ++j)
1398 VkPhysicalDevice dev = current->physicalDevices[j];
1399 if (!(current->physicalDevices[j] = wine_vk_instance_wrap_physical_device(instance, dev)))
1400 return VK_ERROR_INITIALIZATION_FAILED;
1404 return res;
1407 VkResult WINAPI wine_vkEnumeratePhysicalDeviceGroups(VkInstance instance,
1408 uint32_t *count, VkPhysicalDeviceGroupProperties *properties)
1410 TRACE("%p, %p, %p\n", instance, count, properties);
1411 return wine_vk_enumerate_physical_device_groups(instance,
1412 instance->funcs.p_vkEnumeratePhysicalDeviceGroups, count, properties);
1415 VkResult WINAPI wine_vkEnumeratePhysicalDeviceGroupsKHR(VkInstance instance,
1416 uint32_t *count, VkPhysicalDeviceGroupProperties *properties)
1418 TRACE("%p, %p, %p\n", instance, count, properties);
1419 return wine_vk_enumerate_physical_device_groups(instance,
1420 instance->funcs.p_vkEnumeratePhysicalDeviceGroupsKHR, count, properties);
1423 void WINAPI wine_vkGetPhysicalDeviceExternalFenceProperties(VkPhysicalDevice phys_dev,
1424 const VkPhysicalDeviceExternalFenceInfo *fence_info, VkExternalFenceProperties *properties)
1426 TRACE("%p, %p, %p\n", phys_dev, fence_info, properties);
1427 properties->exportFromImportedHandleTypes = 0;
1428 properties->compatibleHandleTypes = 0;
1429 properties->externalFenceFeatures = 0;
1432 void WINAPI wine_vkGetPhysicalDeviceExternalFencePropertiesKHR(VkPhysicalDevice phys_dev,
1433 const VkPhysicalDeviceExternalFenceInfo *fence_info, VkExternalFenceProperties *properties)
1435 TRACE("%p, %p, %p\n", phys_dev, fence_info, properties);
1436 properties->exportFromImportedHandleTypes = 0;
1437 properties->compatibleHandleTypes = 0;
1438 properties->externalFenceFeatures = 0;
1441 void WINAPI wine_vkGetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice phys_dev,
1442 const VkPhysicalDeviceExternalBufferInfo *buffer_info, VkExternalBufferProperties *properties)
1444 TRACE("%p, %p, %p\n", phys_dev, buffer_info, properties);
1445 memset(&properties->externalMemoryProperties, 0, sizeof(properties->externalMemoryProperties));
1448 void WINAPI wine_vkGetPhysicalDeviceExternalBufferPropertiesKHR(VkPhysicalDevice phys_dev,
1449 const VkPhysicalDeviceExternalBufferInfo *buffer_info, VkExternalBufferProperties *properties)
1451 TRACE("%p, %p, %p\n", phys_dev, buffer_info, properties);
1452 memset(&properties->externalMemoryProperties, 0, sizeof(properties->externalMemoryProperties));
1455 VkResult WINAPI wine_vkGetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice phys_dev,
1456 const VkPhysicalDeviceImageFormatInfo2 *format_info, VkImageFormatProperties2 *properties)
1458 VkExternalImageFormatProperties *external_image_properties;
1459 VkResult res;
1461 TRACE("%p, %p, %p\n", phys_dev, format_info, properties);
1463 res = thunk_vkGetPhysicalDeviceImageFormatProperties2(phys_dev, format_info, properties);
1465 if ((external_image_properties = wine_vk_find_struct(properties, EXTERNAL_IMAGE_FORMAT_PROPERTIES)))
1467 VkExternalMemoryProperties *p = &external_image_properties->externalMemoryProperties;
1468 p->externalMemoryFeatures = 0;
1469 p->exportFromImportedHandleTypes = 0;
1470 p->compatibleHandleTypes = 0;
1473 return res;
1476 VkResult WINAPI wine_vkGetPhysicalDeviceImageFormatProperties2KHR(VkPhysicalDevice phys_dev,
1477 const VkPhysicalDeviceImageFormatInfo2 *format_info, VkImageFormatProperties2 *properties)
1479 VkExternalImageFormatProperties *external_image_properties;
1480 VkResult res;
1482 TRACE("%p, %p, %p\n", phys_dev, format_info, properties);
1484 res = thunk_vkGetPhysicalDeviceImageFormatProperties2KHR(phys_dev, format_info, properties);
1486 if ((external_image_properties = wine_vk_find_struct(properties, EXTERNAL_IMAGE_FORMAT_PROPERTIES)))
1488 VkExternalMemoryProperties *p = &external_image_properties->externalMemoryProperties;
1489 p->externalMemoryFeatures = 0;
1490 p->exportFromImportedHandleTypes = 0;
1491 p->compatibleHandleTypes = 0;
1494 return res;
1497 /* From ntdll/unix/sync.c */
1498 #define NANOSECONDS_IN_A_SECOND 1000000000
1499 #define TICKSPERSEC 10000000
1501 static inline VkTimeDomainEXT get_performance_counter_time_domain(void)
1503 #if !defined(__APPLE__) && defined(HAVE_CLOCK_GETTIME)
1504 # ifdef CLOCK_MONOTONIC_RAW
1505 return VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT;
1506 # else
1507 return VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT;
1508 # endif
1509 #else
1510 FIXME("No mapping for VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT on this platform.");
1511 return VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT;
1512 #endif
1515 static VkTimeDomainEXT map_to_host_time_domain(VkTimeDomainEXT domain)
1517 /* Matches ntdll/unix/sync.c's performance counter implementation. */
1518 if (domain == VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT)
1519 return get_performance_counter_time_domain();
1521 return domain;
1524 static inline uint64_t convert_monotonic_timestamp(uint64_t value)
1526 return value / (NANOSECONDS_IN_A_SECOND / TICKSPERSEC);
1529 static inline uint64_t convert_timestamp(VkTimeDomainEXT host_domain, VkTimeDomainEXT target_domain, uint64_t value)
1531 if (host_domain == target_domain)
1532 return value;
1534 /* Convert between MONOTONIC time in ns -> QueryPerformanceCounter */
1535 if ((host_domain == VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT || host_domain == VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT)
1536 && target_domain == VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT)
1537 return convert_monotonic_timestamp(value);
1539 FIXME("Couldn't translate between host domain %d and target domain %d", host_domain, target_domain);
1540 return value;
1543 VkResult WINAPI wine_vkGetCalibratedTimestampsEXT(VkDevice device,
1544 uint32_t timestamp_count, const VkCalibratedTimestampInfoEXT *timestamp_infos,
1545 uint64_t *timestamps, uint64_t *max_deviation)
1547 VkCalibratedTimestampInfoEXT* host_timestamp_infos;
1548 unsigned int i;
1549 VkResult res;
1550 TRACE("%p, %u, %p, %p, %p\n", device, timestamp_count, timestamp_infos, timestamps, max_deviation);
1552 if (!(host_timestamp_infos = heap_alloc(sizeof(VkCalibratedTimestampInfoEXT) * timestamp_count)))
1553 return VK_ERROR_OUT_OF_HOST_MEMORY;
1555 for (i = 0; i < timestamp_count; i++)
1557 host_timestamp_infos[i].sType = timestamp_infos[i].sType;
1558 host_timestamp_infos[i].pNext = timestamp_infos[i].pNext;
1559 host_timestamp_infos[i].timeDomain = map_to_host_time_domain(timestamp_infos[i].timeDomain);
1562 res = device->funcs.p_vkGetCalibratedTimestampsEXT(device->device, timestamp_count, host_timestamp_infos, timestamps, max_deviation);
1563 if (res != VK_SUCCESS)
1564 return res;
1566 for (i = 0; i < timestamp_count; i++)
1567 timestamps[i] = convert_timestamp(host_timestamp_infos[i].timeDomain, timestamp_infos[i].timeDomain, timestamps[i]);
1569 heap_free(host_timestamp_infos);
1571 return res;
1574 VkResult WINAPI wine_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT(VkPhysicalDevice phys_dev,
1575 uint32_t *time_domain_count, VkTimeDomainEXT *time_domains)
1577 BOOL supports_device = FALSE, supports_monotonic = FALSE, supports_monotonic_raw = FALSE;
1578 const VkTimeDomainEXT performance_counter_domain = get_performance_counter_time_domain();
1579 VkTimeDomainEXT *host_time_domains;
1580 uint32_t host_time_domain_count;
1581 VkTimeDomainEXT out_time_domains[2];
1582 uint32_t out_time_domain_count;
1583 unsigned int i;
1584 VkResult res;
1586 TRACE("%p, %p, %p\n", phys_dev, time_domain_count, time_domains);
1588 /* Find out the time domains supported on the host */
1589 res = phys_dev->instance->funcs.p_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT(phys_dev->phys_dev, &host_time_domain_count, NULL);
1590 if (res != VK_SUCCESS)
1591 return res;
1593 if (!(host_time_domains = heap_alloc(sizeof(VkTimeDomainEXT) * host_time_domain_count)))
1594 return VK_ERROR_OUT_OF_HOST_MEMORY;
1596 res = phys_dev->instance->funcs.p_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT(phys_dev->phys_dev, &host_time_domain_count, host_time_domains);
1597 if (res != VK_SUCCESS)
1599 heap_free(host_time_domains);
1600 return res;
1603 for (i = 0; i < host_time_domain_count; i++)
1605 if (host_time_domains[i] == VK_TIME_DOMAIN_DEVICE_EXT)
1606 supports_device = TRUE;
1607 else if (host_time_domains[i] == VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT)
1608 supports_monotonic = TRUE;
1609 else if (host_time_domains[i] == VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT)
1610 supports_monotonic_raw = TRUE;
1611 else
1612 FIXME("Unknown time domain %d", host_time_domains[i]);
1615 heap_free(host_time_domains);
1617 out_time_domain_count = 0;
1619 /* Map our monotonic times -> QPC */
1620 if (supports_monotonic_raw && performance_counter_domain == VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT)
1621 out_time_domains[out_time_domain_count++] = VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT;
1622 else if (supports_monotonic && performance_counter_domain == VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT)
1623 out_time_domains[out_time_domain_count++] = VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT;
1624 else
1625 FIXME("VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT not supported on this platform.");
1627 /* Forward the device domain time */
1628 if (supports_device)
1629 out_time_domains[out_time_domain_count++] = VK_TIME_DOMAIN_DEVICE_EXT;
1631 /* Send the count/domains back to the app */
1632 if (!time_domains)
1634 *time_domain_count = out_time_domain_count;
1635 return VK_SUCCESS;
1638 for (i = 0; i < min(*time_domain_count, out_time_domain_count); i++)
1639 time_domains[i] = out_time_domains[i];
1641 res = *time_domain_count < out_time_domain_count ? VK_INCOMPLETE : VK_SUCCESS;
1642 *time_domain_count = out_time_domain_count;
1643 return res;
1646 static HANDLE get_display_device_init_mutex(void)
1648 static const WCHAR init_mutexW[] = {'d','i','s','p','l','a','y','_','d','e','v','i','c','e','_','i','n','i','t',0};
1649 HANDLE mutex = CreateMutexW(NULL, FALSE, init_mutexW);
1651 WaitForSingleObject(mutex, INFINITE);
1652 return mutex;
1655 static void release_display_device_init_mutex(HANDLE mutex)
1657 ReleaseMutex(mutex);
1658 CloseHandle(mutex);
1661 /* Wait until graphics driver is loaded by explorer */
1662 static void wait_graphics_driver_ready(void)
1664 static BOOL ready = FALSE;
1666 if (!ready)
1668 SendMessageW(GetDesktopWindow(), WM_NULL, 0, 0);
1669 ready = TRUE;
1673 static void fill_luid_property(VkPhysicalDeviceProperties2 *properties2)
1675 static const WCHAR pci[] = {'P','C','I',0};
1676 VkPhysicalDeviceIDProperties *id;
1677 SP_DEVINFO_DATA device_data;
1678 DWORD type, device_idx = 0;
1679 HDEVINFO devinfo;
1680 HANDLE mutex;
1681 GUID uuid;
1682 LUID luid;
1684 if (!(id = wine_vk_find_struct(properties2, PHYSICAL_DEVICE_ID_PROPERTIES)))
1685 return;
1687 wait_graphics_driver_ready();
1688 mutex = get_display_device_init_mutex();
1689 devinfo = SetupDiGetClassDevsW(&GUID_DEVCLASS_DISPLAY, pci, NULL, 0);
1690 device_data.cbSize = sizeof(device_data);
1691 while (SetupDiEnumDeviceInfo(devinfo, device_idx++, &device_data))
1693 if (!SetupDiGetDevicePropertyW(devinfo, &device_data, &WINE_DEVPROPKEY_GPU_VULKAN_UUID,
1694 &type, (BYTE *)&uuid, sizeof(uuid), NULL, 0))
1695 continue;
1697 if (!IsEqualGUID(&uuid, id->deviceUUID))
1698 continue;
1700 if (SetupDiGetDevicePropertyW(devinfo, &device_data, &DEVPROPKEY_GPU_LUID, &type,
1701 (BYTE *)&luid, sizeof(luid), NULL, 0))
1703 memcpy(&id->deviceLUID, &luid, sizeof(id->deviceLUID));
1704 id->deviceLUIDValid = VK_TRUE;
1705 id->deviceNodeMask = 1;
1706 break;
1709 SetupDiDestroyDeviceInfoList(devinfo);
1710 release_display_device_init_mutex(mutex);
1712 TRACE("deviceName:%s deviceLUIDValid:%d LUID:%08x:%08x deviceNodeMask:%#x.\n",
1713 properties2->properties.deviceName, id->deviceLUIDValid, luid.HighPart, luid.LowPart,
1714 id->deviceNodeMask);
1717 void WINAPI wine_vkGetPhysicalDeviceProperties2(VkPhysicalDevice phys_dev,
1718 VkPhysicalDeviceProperties2 *properties2)
1720 TRACE("%p, %p\n", phys_dev, properties2);
1722 thunk_vkGetPhysicalDeviceProperties2(phys_dev, properties2);
1723 fill_luid_property(properties2);
1726 void WINAPI wine_vkGetPhysicalDeviceProperties2KHR(VkPhysicalDevice phys_dev,
1727 VkPhysicalDeviceProperties2 *properties2)
1729 TRACE("%p, %p\n", phys_dev, properties2);
1731 thunk_vkGetPhysicalDeviceProperties2KHR(phys_dev, properties2);
1732 fill_luid_property(properties2);
1735 void WINAPI wine_vkGetPhysicalDeviceExternalSemaphoreProperties(VkPhysicalDevice phys_dev,
1736 const VkPhysicalDeviceExternalSemaphoreInfo *semaphore_info, VkExternalSemaphoreProperties *properties)
1738 TRACE("%p, %p, %p\n", phys_dev, semaphore_info, properties);
1739 properties->exportFromImportedHandleTypes = 0;
1740 properties->compatibleHandleTypes = 0;
1741 properties->externalSemaphoreFeatures = 0;
1744 void WINAPI wine_vkGetPhysicalDeviceExternalSemaphorePropertiesKHR(VkPhysicalDevice phys_dev,
1745 const VkPhysicalDeviceExternalSemaphoreInfo *semaphore_info, VkExternalSemaphoreProperties *properties)
1747 TRACE("%p, %p, %p\n", phys_dev, semaphore_info, properties);
1748 properties->exportFromImportedHandleTypes = 0;
1749 properties->compatibleHandleTypes = 0;
1750 properties->externalSemaphoreFeatures = 0;
1753 VkResult WINAPI wine_vkSetPrivateDataEXT(VkDevice device, VkObjectType object_type, uint64_t object_handle,
1754 VkPrivateDataSlotEXT private_data_slot, uint64_t data)
1756 TRACE("%p, %#x, 0x%s, 0x%s, 0x%s\n", device, object_type, wine_dbgstr_longlong(object_handle),
1757 wine_dbgstr_longlong(private_data_slot), wine_dbgstr_longlong(data));
1759 object_handle = wine_vk_unwrap_handle(object_type, object_handle);
1760 return device->funcs.p_vkSetPrivateDataEXT(device->device, object_type, object_handle, private_data_slot, data);
1763 void WINAPI wine_vkGetPrivateDataEXT(VkDevice device, VkObjectType object_type, uint64_t object_handle,
1764 VkPrivateDataSlotEXT private_data_slot, uint64_t *data)
1766 TRACE("%p, %#x, 0x%s, 0x%s, %p\n", device, object_type, wine_dbgstr_longlong(object_handle),
1767 wine_dbgstr_longlong(private_data_slot), data);
1769 object_handle = wine_vk_unwrap_handle(object_type, object_handle);
1770 device->funcs.p_vkGetPrivateDataEXT(device->device, object_type, object_handle, private_data_slot, data);
1773 static inline void adjust_max_image_count(VkPhysicalDevice phys_dev, VkSurfaceCapabilitiesKHR* capabilities)
1775 /* Many Windows games, for example Strange Brigade, No Man's Sky, Path of Exile
1776 * and World War Z, do not expect that maxImageCount can be set to 0.
1777 * A value of 0 means that there is no limit on the number of images.
1778 * Nvidia reports 8 on Windows, AMD 16.
1779 * https://vulkan.gpuinfo.org/displayreport.php?id=9122#surface
1780 * https://vulkan.gpuinfo.org/displayreport.php?id=9121#surface
1782 if ((phys_dev->instance->quirks & WINEVULKAN_QUIRK_ADJUST_MAX_IMAGE_COUNT) && !capabilities->maxImageCount)
1784 capabilities->maxImageCount = max(capabilities->minImageCount, 16);
1788 VkResult WINAPI wine_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice phys_dev,
1789 VkSurfaceKHR surface, VkSurfaceCapabilitiesKHR *capabilities)
1791 VkResult res;
1793 TRACE("%p, 0x%s, %p\n", phys_dev, wine_dbgstr_longlong(surface), capabilities);
1795 res = thunk_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(phys_dev, surface, capabilities);
1797 if (res == VK_SUCCESS)
1798 adjust_max_image_count(phys_dev, capabilities);
1800 return res;
1803 VkResult WINAPI wine_vkGetPhysicalDeviceSurfaceCapabilities2KHR(VkPhysicalDevice phys_dev,
1804 const VkPhysicalDeviceSurfaceInfo2KHR *surface_info, VkSurfaceCapabilities2KHR *capabilities)
1806 VkResult res;
1808 TRACE("%p, %p, %p\n", phys_dev, surface_info, capabilities);
1810 res = thunk_vkGetPhysicalDeviceSurfaceCapabilities2KHR(phys_dev, surface_info, capabilities);
1812 if (res == VK_SUCCESS)
1813 adjust_max_image_count(phys_dev, &capabilities->surfaceCapabilities);
1815 return res;
1818 VkResult WINAPI wine_vkCreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT *create_info,
1819 const VkAllocationCallbacks *allocator, VkDebugUtilsMessengerEXT *messenger)
1821 VkDebugUtilsMessengerCreateInfoEXT wine_create_info;
1822 struct wine_debug_utils_messenger *object;
1823 VkResult res;
1825 TRACE("%p, %p, %p, %p\n", instance, create_info, allocator, messenger);
1827 if (allocator)
1828 FIXME("Support for allocation callbacks not implemented yet\n");
1830 if (!(object = heap_alloc_zero(sizeof(*object))))
1831 return VK_ERROR_OUT_OF_HOST_MEMORY;
1833 object->instance = instance;
1834 object->user_callback = create_info->pfnUserCallback;
1835 object->user_data = create_info->pUserData;
1837 wine_create_info = *create_info;
1839 wine_create_info.pfnUserCallback = (void *) &debug_utils_callback_conversion;
1840 wine_create_info.pUserData = object;
1842 res = instance->funcs.p_vkCreateDebugUtilsMessengerEXT(instance->instance, &wine_create_info, NULL, &object->debug_messenger);
1844 if (res != VK_SUCCESS)
1846 heap_free(object);
1847 return res;
1850 WINE_VK_ADD_NON_DISPATCHABLE_MAPPING(instance, object, object->debug_messenger);
1851 *messenger = wine_debug_utils_messenger_to_handle(object);
1853 return VK_SUCCESS;
1856 void WINAPI wine_vkDestroyDebugUtilsMessengerEXT(
1857 VkInstance instance, VkDebugUtilsMessengerEXT messenger, const VkAllocationCallbacks *allocator)
1859 struct wine_debug_utils_messenger *object;
1861 TRACE("%p, 0x%s, %p\n", instance, wine_dbgstr_longlong(messenger), allocator);
1863 object = wine_debug_utils_messenger_from_handle(messenger);
1865 instance->funcs.p_vkDestroyDebugUtilsMessengerEXT(instance->instance, object->debug_messenger, NULL);
1866 WINE_VK_REMOVE_HANDLE_MAPPING(instance, object);
1868 heap_free(object);
1871 void WINAPI wine_vkSubmitDebugUtilsMessageEXT(VkInstance instance, VkDebugUtilsMessageSeverityFlagBitsEXT severity,
1872 VkDebugUtilsMessageTypeFlagsEXT types, const VkDebugUtilsMessengerCallbackDataEXT *callback_data)
1874 VkDebugUtilsMessengerCallbackDataEXT native_callback_data;
1875 VkDebugUtilsObjectNameInfoEXT *object_names;
1876 unsigned int i;
1878 TRACE("%p, %#x, %#x, %p\n", instance, severity, types, callback_data);
1880 native_callback_data = *callback_data;
1881 object_names = heap_calloc(callback_data->objectCount, sizeof(*object_names));
1882 memcpy(object_names, callback_data->pObjects, callback_data->objectCount * sizeof(*object_names));
1883 native_callback_data.pObjects = object_names;
1885 for (i = 0; i < callback_data->objectCount; i++)
1887 object_names[i].objectHandle =
1888 wine_vk_unwrap_handle(callback_data->pObjects[i].objectType, callback_data->pObjects[i].objectHandle);
1891 thunk_vkSubmitDebugUtilsMessageEXT(instance, severity, types, &native_callback_data);
1893 heap_free(object_names);
1896 VkResult WINAPI wine_vkSetDebugUtilsObjectTagEXT(VkDevice device, const VkDebugUtilsObjectTagInfoEXT *tag_info)
1898 VkDebugUtilsObjectTagInfoEXT wine_tag_info;
1900 TRACE("%p, %p\n", device, tag_info);
1902 wine_tag_info = *tag_info;
1903 wine_tag_info.objectHandle = wine_vk_unwrap_handle(tag_info->objectType, tag_info->objectHandle);
1905 return thunk_vkSetDebugUtilsObjectTagEXT(device, &wine_tag_info);
1908 VkResult WINAPI wine_vkSetDebugUtilsObjectNameEXT(VkDevice device, const VkDebugUtilsObjectNameInfoEXT *name_info)
1910 VkDebugUtilsObjectNameInfoEXT wine_name_info;
1912 TRACE("%p, %p\n", device, name_info);
1914 wine_name_info = *name_info;
1915 wine_name_info.objectHandle = wine_vk_unwrap_handle(name_info->objectType, name_info->objectHandle);
1917 return thunk_vkSetDebugUtilsObjectNameEXT(device, &wine_name_info);
1920 VkResult WINAPI wine_vkCreateDebugReportCallbackEXT(VkInstance instance, const VkDebugReportCallbackCreateInfoEXT *create_info,
1921 const VkAllocationCallbacks *allocator, VkDebugReportCallbackEXT *callback)
1923 VkDebugReportCallbackCreateInfoEXT wine_create_info;
1924 struct wine_debug_report_callback *object;
1925 VkResult res;
1927 TRACE("%p, %p, %p, %p\n", instance, create_info, allocator, callback);
1929 if (allocator)
1930 FIXME("Support for allocation callbacks not implemented yet\n");
1932 if (!(object = heap_alloc_zero(sizeof(*object))))
1933 return VK_ERROR_OUT_OF_HOST_MEMORY;
1935 object->instance = instance;
1936 object->user_callback = create_info->pfnCallback;
1937 object->user_data = create_info->pUserData;
1939 wine_create_info = *create_info;
1941 wine_create_info.pfnCallback = (void *) debug_report_callback_conversion;
1942 wine_create_info.pUserData = object;
1944 res = instance->funcs.p_vkCreateDebugReportCallbackEXT(instance->instance, &wine_create_info, NULL, &object->debug_callback);
1946 if (res != VK_SUCCESS)
1948 heap_free(object);
1949 return res;
1952 WINE_VK_ADD_NON_DISPATCHABLE_MAPPING(instance, object, object->debug_callback);
1953 *callback = wine_debug_report_callback_to_handle(object);
1955 return VK_SUCCESS;
1958 void WINAPI wine_vkDestroyDebugReportCallbackEXT(
1959 VkInstance instance, VkDebugReportCallbackEXT callback, const VkAllocationCallbacks *allocator)
1961 struct wine_debug_report_callback *object;
1963 TRACE("%p, 0x%s, %p\n", instance, wine_dbgstr_longlong(callback), allocator);
1965 object = wine_debug_report_callback_from_handle(callback);
1967 instance->funcs.p_vkDestroyDebugReportCallbackEXT(instance->instance, object->debug_callback, NULL);
1969 WINE_VK_REMOVE_HANDLE_MAPPING(instance, object);
1971 heap_free(object);
1974 void WINAPI wine_vkDebugReportMessageEXT(VkInstance instance, VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT object_type,
1975 uint64_t object, size_t location, int32_t code, const char *layer_prefix, const char *message)
1977 TRACE("%p, %#x, %#x, 0x%s, 0x%s, %d, %p, %p\n", instance, flags, object_type, wine_dbgstr_longlong(object),
1978 wine_dbgstr_longlong(location), code, layer_prefix, message);
1980 object = wine_vk_unwrap_handle(object_type, object);
1982 instance->funcs.p_vkDebugReportMessageEXT(
1983 instance->instance, flags, object_type, object, location, code, layer_prefix, message);
1986 VkResult WINAPI wine_vkDebugMarkerSetObjectTagEXT(VkDevice device, const VkDebugMarkerObjectTagInfoEXT *tag_info)
1988 VkDebugMarkerObjectTagInfoEXT wine_tag_info;
1990 TRACE("%p, %p\n", device, tag_info);
1992 wine_tag_info = *tag_info;
1993 wine_tag_info.object = wine_vk_unwrap_handle(tag_info->objectType, tag_info->object);
1995 return thunk_vkDebugMarkerSetObjectTagEXT(device, &wine_tag_info);
1998 VkResult WINAPI wine_vkDebugMarkerSetObjectNameEXT(VkDevice device, const VkDebugMarkerObjectNameInfoEXT *name_info)
2000 VkDebugMarkerObjectNameInfoEXT wine_name_info;
2002 TRACE("%p, %p\n", device, name_info);
2004 wine_name_info = *name_info;
2005 wine_name_info.object = wine_vk_unwrap_handle(name_info->objectType, name_info->object);
2007 return thunk_vkDebugMarkerSetObjectNameEXT(device, &wine_name_info);
2010 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, void *reserved)
2012 TRACE("%p, %u, %p\n", hinst, reason, reserved);
2014 switch (reason)
2016 case DLL_PROCESS_ATTACH:
2017 hinstance = hinst;
2018 DisableThreadLibraryCalls(hinst);
2019 break;
2021 return TRUE;
2024 static const struct vulkan_func vk_global_dispatch_table[] =
2026 /* These functions must call wine_vk_init_once() before accessing vk_funcs. */
2027 {"vkCreateInstance", &wine_vkCreateInstance},
2028 {"vkEnumerateInstanceExtensionProperties", &wine_vkEnumerateInstanceExtensionProperties},
2029 {"vkEnumerateInstanceLayerProperties", &wine_vkEnumerateInstanceLayerProperties},
2030 {"vkEnumerateInstanceVersion", &wine_vkEnumerateInstanceVersion},
2031 {"vkGetInstanceProcAddr", &wine_vkGetInstanceProcAddr},
2034 static void *wine_vk_get_global_proc_addr(const char *name)
2036 unsigned int i;
2038 for (i = 0; i < ARRAY_SIZE(vk_global_dispatch_table); i++)
2040 if (strcmp(name, vk_global_dispatch_table[i].name) == 0)
2042 TRACE("Found name=%s in global table\n", debugstr_a(name));
2043 return vk_global_dispatch_table[i].func;
2046 return NULL;
2050 * Wrapper around driver vkGetInstanceProcAddr implementation.
2051 * Allows winelib applications to access Vulkan functions with Wine
2052 * additions and native ABI.
2054 void *native_vkGetInstanceProcAddrWINE(VkInstance instance, const char *name)
2056 wine_vk_init_once();
2057 if (!vk_funcs)
2058 return NULL;
2060 return vk_funcs->p_vkGetInstanceProcAddr(instance, name);
2064 static const WCHAR winevulkan_json_resW[] = {'w','i','n','e','v','u','l','k','a','n','_','j','s','o','n',0};
2065 static const WCHAR winevulkan_json_pathW[] = {'\\','w','i','n','e','v','u','l','k','a','n','.','j','s','o','n',0};
2066 static const WCHAR vulkan_driversW[] = {'S','o','f','t','w','a','r','e','\\','K','h','r','o','n','o','s','\\',
2067 'V','u','l','k','a','n','\\','D','r','i','v','e','r','s',0};
2069 HRESULT WINAPI DllRegisterServer(void)
2071 WCHAR json_path[MAX_PATH];
2072 HRSRC rsrc;
2073 const char *data;
2074 DWORD datalen, written, zero = 0;
2075 HANDLE file;
2076 HKEY key;
2078 /* Create the JSON manifest and registry key to register this ICD with the official Vulkan loader. */
2079 TRACE("\n");
2080 rsrc = FindResourceW(hinstance, winevulkan_json_resW, (const WCHAR *)RT_RCDATA);
2081 data = LockResource(LoadResource(hinstance, rsrc));
2082 datalen = SizeofResource(hinstance, rsrc);
2084 GetSystemDirectoryW(json_path, ARRAY_SIZE(json_path));
2085 lstrcatW(json_path, winevulkan_json_pathW);
2086 file = CreateFileW(json_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2087 if (file == INVALID_HANDLE_VALUE)
2089 ERR("Unable to create JSON manifest.\n");
2090 return E_UNEXPECTED;
2092 WriteFile(file, data, datalen, &written, NULL);
2093 CloseHandle(file);
2095 if (!RegCreateKeyExW(HKEY_LOCAL_MACHINE, vulkan_driversW, 0, NULL, 0, KEY_SET_VALUE, NULL, &key, NULL))
2097 RegSetValueExW(key, json_path, 0, REG_DWORD, (const BYTE *)&zero, sizeof(zero));
2098 RegCloseKey(key);
2100 return S_OK;
2103 HRESULT WINAPI DllUnregisterServer(void)
2105 WCHAR json_path[MAX_PATH];
2106 HKEY key;
2108 /* Remove the JSON manifest and registry key */
2109 TRACE("\n");
2110 GetSystemDirectoryW(json_path, ARRAY_SIZE(json_path));
2111 lstrcatW(json_path, winevulkan_json_pathW);
2112 DeleteFileW(json_path);
2114 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, vulkan_driversW, 0, KEY_SET_VALUE, &key) == ERROR_SUCCESS)
2116 RegDeleteValueW(key, json_path);
2117 RegCloseKey(key);
2120 return S_OK;