winevulkan: Limit vkGetDeviceProcAddr() workaround to broken apps.
[wine.git] / dlls / winevulkan / vulkan.c
blob886b345a1510fe154fc27c5da3ad02baad3a18d2
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 <stdarg.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winuser.h"
26 #include "wine/debug.h"
27 #include "wine/heap.h"
28 #include "wine/vulkan.h"
29 #include "wine/vulkan_driver.h"
30 #include "vulkan_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(vulkan);
34 /* For now default to 4 as it felt like a reasonable version feature wise to support.
35 * Don't support the optional vk_icdGetPhysicalDeviceProcAddr introduced in this version
36 * as it is unlikely we will implement physical device extensions, which the loader is not
37 * aware off. Version 5 adds more extensive version checks. Something to tackle later.
39 #define WINE_VULKAN_ICD_VERSION 4
41 /* All Vulkan structures use this structure for the first elements. */
42 struct wine_vk_structure_header
44 VkStructureType sType;
45 const void *pNext;
48 static void *wine_vk_get_global_proc_addr(const char *name);
50 static const struct vulkan_funcs *vk_funcs;
52 static void wine_vk_physical_device_free(struct VkPhysicalDevice_T *phys_dev)
54 if (!phys_dev)
55 return;
57 heap_free(phys_dev->extensions);
58 heap_free(phys_dev);
61 static struct VkPhysicalDevice_T *wine_vk_physical_device_alloc(struct VkInstance_T *instance,
62 VkPhysicalDevice phys_dev)
64 struct VkPhysicalDevice_T *object;
65 uint32_t num_host_properties, num_properties = 0;
66 VkExtensionProperties *host_properties = NULL;
67 VkResult res;
68 unsigned int i, j;
70 if (!(object = heap_alloc_zero(sizeof(*object))))
71 return NULL;
73 object->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
74 object->instance = instance;
75 object->phys_dev = phys_dev;
77 res = instance->funcs.p_vkEnumerateDeviceExtensionProperties(phys_dev,
78 NULL, &num_host_properties, NULL);
79 if (res != VK_SUCCESS)
81 ERR("Failed to enumerate device extensions, res=%d\n", res);
82 goto err;
85 host_properties = heap_calloc(num_host_properties, sizeof(*host_properties));
86 if (!host_properties)
88 ERR("Failed to allocate memory for device properties!\n");
89 goto err;
92 res = instance->funcs.p_vkEnumerateDeviceExtensionProperties(phys_dev,
93 NULL, &num_host_properties, host_properties);
94 if (res != VK_SUCCESS)
96 ERR("Failed to enumerate device extensions, res=%d\n", res);
97 goto err;
100 /* Count list of extensions for which we have an implementation.
101 * TODO: perform translation for platform specific extensions.
103 for (i = 0; i < num_host_properties; i++)
105 if (wine_vk_device_extension_supported(host_properties[i].extensionName))
107 TRACE("Enabling extension '%s' for physical device %p\n", host_properties[i].extensionName, object);
108 num_properties++;
110 else
112 TRACE("Skipping extension '%s', no implementation found in winevulkan.\n", host_properties[i].extensionName);
116 TRACE("Host supported extensions %u, Wine supported extensions %u\n", num_host_properties, num_properties);
118 if (!(object->extensions = heap_calloc(num_properties, sizeof(*object->extensions))))
120 ERR("Failed to allocate memory for device extensions!\n");
121 goto err;
124 for (i = 0, j = 0; i < num_host_properties; i++)
126 if (wine_vk_device_extension_supported(host_properties[i].extensionName))
128 object->extensions[j] = host_properties[i];
129 j++;
132 object->extension_count = num_properties;
134 heap_free(host_properties);
135 return object;
137 err:
138 wine_vk_physical_device_free(object);
139 heap_free(host_properties);
140 return NULL;
143 /* Helper function for release command buffers. */
144 static void wine_vk_command_buffers_free(struct VkDevice_T *device, VkCommandPool pool,
145 uint32_t count, const VkCommandBuffer *buffers)
147 unsigned int i;
149 for (i = 0; i < count; i++)
151 if (!buffers[i])
152 continue;
154 device->funcs.p_vkFreeCommandBuffers(device->device, pool, 1, &buffers[i]->command_buffer);
155 heap_free(buffers[i]);
159 /* Helper function to create queues for a given family index. */
160 static struct VkQueue_T *wine_vk_device_alloc_queues(struct VkDevice_T *device,
161 uint32_t family_index, uint32_t queue_count)
163 struct VkQueue_T *queues;
164 unsigned int i;
166 if (!(queues = heap_calloc(queue_count, sizeof(*queues))))
168 ERR("Failed to allocate memory for queues\n");
169 return NULL;
172 for (i = 0; i < queue_count; i++)
174 struct VkQueue_T *queue = &queues[i];
175 queue->device = device;
177 /* The native device was already allocated with the required number of queues,
178 * so just fetch them from there.
180 device->funcs.p_vkGetDeviceQueue(device->device, family_index, i, &queue->queue);
182 /* Set special header for ICD loader. */
183 queue->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
186 return queues;
189 /* Helper function to convert win32 VkDeviceCreateInfo to host compatible. */
190 static void wine_vk_device_convert_create_info(const VkDeviceCreateInfo *src,
191 VkDeviceCreateInfo *dst)
193 unsigned int i;
195 *dst = *src;
197 /* Application and loader can pass in a chain of extensions through pNext.
198 * We can't blindly pass these through as often these contain callbacks or
199 * they can even be pass structures for loader / ICD internal use. For now
200 * we ignore everything in pNext chain, but we print FIXMEs.
202 if (src->pNext)
204 const struct wine_vk_structure_header *header;
206 for (header = src->pNext; header; header = header->pNext)
208 switch (header->sType)
210 case VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO:
211 /* Used for loader to ICD communication. Ignore to not confuse
212 * host loader.
214 break;
216 default:
217 FIXME("Application requested a linked structure of type %#x.\n", header->sType);
221 /* For now don't support anything. */
222 dst->pNext = NULL;
224 /* Should be filtered out by loader as ICDs don't support layers. */
225 dst->enabledLayerCount = 0;
226 dst->ppEnabledLayerNames = NULL;
228 TRACE("Enabled extensions: %u\n", dst->enabledExtensionCount);
229 for (i = 0; i < dst->enabledExtensionCount; i++)
231 TRACE("Extension %u: %s\n", i, debugstr_a(dst->ppEnabledExtensionNames[i]));
235 /* Helper function used for freeing a device structure. This function supports full
236 * and partial object cleanups and can thus be used for vkCreateDevice failures.
238 static void wine_vk_device_free(struct VkDevice_T *device)
240 if (!device)
241 return;
243 if (device->queues)
245 unsigned int i;
246 for (i = 0; i < device->max_queue_families; i++)
248 heap_free(device->queues[i]);
250 heap_free(device->queues);
251 device->queues = NULL;
254 if (device->device && device->funcs.p_vkDestroyDevice)
256 device->funcs.p_vkDestroyDevice(device->device, NULL /* pAllocator */);
259 heap_free(device);
262 static BOOL wine_vk_init(void)
264 HDC hdc;
266 hdc = GetDC(0);
267 vk_funcs = __wine_get_vulkan_driver(hdc, WINE_VULKAN_DRIVER_VERSION);
268 ReleaseDC(0, hdc);
269 if (!vk_funcs)
271 ERR("Failed to load Wine graphics driver supporting Vulkan.\n");
272 return FALSE;
275 return TRUE;
278 /* Helper function for converting between win32 and host compatible VkInstanceCreateInfo.
279 * This function takes care of extensions handled at winevulkan layer, a Wine graphics
280 * driver is responsible for handling e.g. surface extensions.
282 static void wine_vk_instance_convert_create_info(const VkInstanceCreateInfo *src,
283 VkInstanceCreateInfo *dst)
285 unsigned int i;
287 *dst = *src;
289 if (dst->pApplicationInfo)
291 const VkApplicationInfo *app_info = dst->pApplicationInfo;
292 TRACE("Application name %s, application version %#x\n",
293 debugstr_a(app_info->pApplicationName), app_info->applicationVersion);
294 TRACE("Engine name %s, engine version %#x\n", debugstr_a(app_info->pEngineName),
295 app_info->engineVersion);
296 TRACE("API version %#x\n", app_info->apiVersion);
299 /* Application and loader can pass in a chain of extensions through pNext.
300 * We can't blindly pass these through as often these contain callbacks or
301 * they can even be pass structures for loader / ICD internal use. For now
302 * we ignore everything in pNext chain, but we print FIXMEs.
304 if (src->pNext)
306 const struct wine_vk_structure_header *header;
308 for (header = src->pNext; header; header = header->pNext)
310 switch (header->sType)
312 case VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO:
313 /* Can be used to register new dispatchable object types
314 * to the loader. We should ignore it as it will confuse the
315 * host its loader.
317 break;
319 default:
320 FIXME("Application requested a linked structure of type %#x.\n", header->sType);
324 /* For now don't support anything. */
325 dst->pNext = NULL;
327 /* ICDs don't support any layers, so nothing to copy. Modern versions of the loader
328 * filter this data out as well.
330 dst->enabledLayerCount = 0;
331 dst->ppEnabledLayerNames = NULL;
333 TRACE("Enabled extensions: %u\n", dst->enabledExtensionCount);
334 for (i = 0; i < dst->enabledExtensionCount; i++)
336 TRACE("Extension %u: %s\n", i, debugstr_a(dst->ppEnabledExtensionNames[i]));
340 /* Helper function which stores wrapped physical devices in the instance object. */
341 static VkResult wine_vk_instance_load_physical_devices(struct VkInstance_T *instance)
343 VkResult res;
344 struct VkPhysicalDevice_T **tmp_phys_devs;
345 uint32_t num_phys_devs = 0;
346 unsigned int i;
348 res = instance->funcs.p_vkEnumeratePhysicalDevices(instance->instance, &num_phys_devs, NULL);
349 if (res != VK_SUCCESS)
351 ERR("Failed to enumerate physical devices, res=%d\n", res);
352 return res;
355 /* Don't bother with any of the rest if the system just lacks devices. */
356 if (num_phys_devs == 0)
357 return VK_SUCCESS;
359 tmp_phys_devs = heap_calloc(num_phys_devs, sizeof(*tmp_phys_devs));
360 if (!tmp_phys_devs)
361 return VK_ERROR_OUT_OF_HOST_MEMORY;
363 res = instance->funcs.p_vkEnumeratePhysicalDevices(instance->instance, &num_phys_devs, tmp_phys_devs);
364 if (res != VK_SUCCESS)
366 heap_free(tmp_phys_devs);
367 return res;
370 instance->phys_devs = heap_calloc(num_phys_devs, sizeof(*instance->phys_devs));
371 if (!instance->phys_devs)
373 heap_free(tmp_phys_devs);
374 return VK_ERROR_OUT_OF_HOST_MEMORY;
377 /* Wrap each native physical device handle into a dispatchable object for the ICD loader. */
378 for (i = 0; i < num_phys_devs; i++)
380 struct VkPhysicalDevice_T *phys_dev = wine_vk_physical_device_alloc(instance, tmp_phys_devs[i]);
381 if (!phys_dev)
383 ERR("Unable to allocate memory for physical device!\n");
384 heap_free(tmp_phys_devs);
385 return VK_ERROR_OUT_OF_HOST_MEMORY;
388 instance->phys_devs[i] = phys_dev;
389 instance->num_phys_devs = i + 1;
391 instance->num_phys_devs = num_phys_devs;
393 heap_free(tmp_phys_devs);
394 return VK_SUCCESS;
397 /* Helper function used for freeing an instance structure. This function supports full
398 * and partial object cleanups and can thus be used for vkCreateInstance failures.
400 static void wine_vk_instance_free(struct VkInstance_T *instance)
402 if (!instance)
403 return;
405 if (instance->phys_devs)
407 unsigned int i;
409 for (i = 0; i < instance->num_phys_devs; i++)
411 wine_vk_physical_device_free(instance->phys_devs[i]);
413 heap_free(instance->phys_devs);
416 if (instance->instance)
417 vk_funcs->p_vkDestroyInstance(instance->instance, NULL /* allocator */);
419 heap_free(instance);
422 VkResult WINAPI wine_vkAllocateCommandBuffers(VkDevice device,
423 const VkCommandBufferAllocateInfo *allocate_info, VkCommandBuffer *buffers)
425 VkResult res = VK_SUCCESS;
426 unsigned int i;
428 TRACE("%p %p %p\n", device, allocate_info, buffers);
430 memset(buffers, 0, allocate_info->commandBufferCount * sizeof(*buffers));
432 for (i = 0; i < allocate_info->commandBufferCount; i++)
434 #if defined(USE_STRUCT_CONVERSION)
435 VkCommandBufferAllocateInfo_host allocate_info_host;
436 #else
437 VkCommandBufferAllocateInfo allocate_info_host;
438 #endif
439 /* TODO: future extensions (none yet) may require pNext conversion. */
440 allocate_info_host.pNext = allocate_info->pNext;
441 allocate_info_host.sType = allocate_info->sType;
442 allocate_info_host.commandPool = allocate_info->commandPool;
443 allocate_info_host.level = allocate_info->level;
444 allocate_info_host.commandBufferCount = 1;
446 TRACE("Creating command buffer %u, pool 0x%s, level %#x\n", i,
447 wine_dbgstr_longlong(allocate_info_host.commandPool),
448 allocate_info_host.level);
450 if (!(buffers[i] = heap_alloc_zero(sizeof(*buffers))))
452 res = VK_ERROR_OUT_OF_HOST_MEMORY;
453 break;
456 buffers[i]->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
457 buffers[i]->device = device;
458 res = device->funcs.p_vkAllocateCommandBuffers(device->device,
459 &allocate_info_host, &buffers[i]->command_buffer);
460 if (res != VK_SUCCESS)
462 ERR("Failed to allocate command buffer, res=%d\n", res);
463 break;
467 if (res != VK_SUCCESS)
469 wine_vk_command_buffers_free(device, allocate_info->commandPool, i, buffers);
470 memset(buffers, 0, allocate_info->commandBufferCount * sizeof(*buffers));
471 return res;
474 return VK_SUCCESS;
477 void WINAPI wine_vkCmdExecuteCommands(VkCommandBuffer buffer, uint32_t count,
478 const VkCommandBuffer *buffers)
480 VkCommandBuffer *tmp_buffers;
481 unsigned int i;
483 TRACE("%p %u %p\n", buffer, count, buffers);
485 if (!buffers || !count)
486 return;
488 /* Unfortunately we need a temporary buffer as our command buffers are wrapped.
489 * This call is called often and if a performance concern, we may want to use
490 * alloca as we shouldn't need much memory and it needs to be cleaned up after
491 * the call anyway.
493 if (!(tmp_buffers = heap_alloc(count * sizeof(*tmp_buffers))))
495 ERR("Failed to allocate memory for temporary command buffers\n");
496 return;
499 for (i = 0; i < count; i++)
500 tmp_buffers[i] = buffers[i]->command_buffer;
502 buffer->device->funcs.p_vkCmdExecuteCommands(buffer->command_buffer, count, tmp_buffers);
504 heap_free(tmp_buffers);
507 VkResult WINAPI wine_vkCreateDevice(VkPhysicalDevice phys_dev,
508 const VkDeviceCreateInfo *create_info,
509 const VkAllocationCallbacks *allocator, VkDevice *device)
511 VkDeviceCreateInfo create_info_host;
512 uint32_t max_queue_families;
513 struct VkDevice_T *object;
514 VkResult res;
515 unsigned int i;
517 TRACE("%p %p %p %p\n", phys_dev, create_info, allocator, device);
519 if (allocator)
520 FIXME("Support for allocation callbacks not implemented yet\n");
522 object = heap_alloc_zero(sizeof(*object));
523 if (!object)
524 return VK_ERROR_OUT_OF_HOST_MEMORY;
526 object->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
528 wine_vk_device_convert_create_info(create_info, &create_info_host);
530 res = phys_dev->instance->funcs.p_vkCreateDevice(phys_dev->phys_dev,
531 &create_info_host, NULL /* allocator */, &object->device);
532 if (res != VK_SUCCESS)
534 ERR("Failed to create device.\n");
535 wine_vk_device_free(object);
536 return res;
539 object->phys_dev = phys_dev;
541 /* Just load all function pointers we are aware off. The loader takes care of filtering.
542 * We use vkGetDeviceProcAddr as opposed to vkGetInstanceProcAddr for efficiency reasons
543 * as functions pass through fewer dispatch tables within the loader.
545 #define USE_VK_FUNC(name) \
546 object->funcs.p_##name = (void *)vk_funcs->p_vkGetDeviceProcAddr(object->device, #name); \
547 if (object->funcs.p_##name == NULL) \
548 TRACE("Not found %s\n", #name);
549 ALL_VK_DEVICE_FUNCS()
550 #undef USE_VK_FUNC
552 /* We need to cache all queues within the device as each requires wrapping since queues are
553 * dispatchable objects.
555 phys_dev->instance->funcs.p_vkGetPhysicalDeviceQueueFamilyProperties(phys_dev->phys_dev,
556 &max_queue_families, NULL);
557 object->max_queue_families = max_queue_families;
558 TRACE("Max queue families: %u\n", object->max_queue_families);
560 object->queues = heap_calloc(max_queue_families, sizeof(*object->queues));
561 if (!object->queues)
563 wine_vk_device_free(object);
564 return VK_ERROR_OUT_OF_HOST_MEMORY;
567 for (i = 0; i < create_info_host.queueCreateInfoCount; i++)
569 uint32_t family_index = create_info_host.pQueueCreateInfos[i].queueFamilyIndex;
570 uint32_t queue_count = create_info_host.pQueueCreateInfos[i].queueCount;
572 TRACE("queueFamilyIndex %u, queueCount %u\n", family_index, queue_count);
574 object->queues[family_index] = wine_vk_device_alloc_queues(object, family_index, queue_count);
575 if (!object->queues[family_index])
577 ERR("Failed to allocate memory for queues\n");
578 wine_vk_device_free(object);
579 return VK_ERROR_OUT_OF_HOST_MEMORY;
583 object->quirks = phys_dev->instance->quirks;
585 *device = object;
586 return VK_SUCCESS;
589 VkResult WINAPI wine_vkCreateInstance(const VkInstanceCreateInfo *create_info,
590 const VkAllocationCallbacks *allocator, VkInstance *instance)
592 VkInstanceCreateInfo create_info_host;
593 const VkApplicationInfo *app_info;
594 struct VkInstance_T *object;
595 VkResult res;
597 TRACE("create_info %p, allocator %p, instance %p\n", create_info, allocator, instance);
599 if (allocator)
600 FIXME("Support for allocation callbacks not implemented yet\n");
602 if (!(object = heap_alloc_zero(sizeof(*object))))
604 ERR("Failed to allocate memory for instance\n");
605 return VK_ERROR_OUT_OF_HOST_MEMORY;
607 object->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
609 wine_vk_instance_convert_create_info(create_info, &create_info_host);
611 res = vk_funcs->p_vkCreateInstance(&create_info_host, NULL /* allocator */, &object->instance);
612 if (res != VK_SUCCESS)
614 ERR("Failed to create instance, res=%d\n", res);
615 wine_vk_instance_free(object);
616 return res;
619 /* Load all instance functions we are aware of. Note the loader takes care
620 * of any filtering for extensions which were not requested, but which the
621 * ICD may support.
623 #define USE_VK_FUNC(name) \
624 object->funcs.p_##name = (void *)vk_funcs->p_vkGetInstanceProcAddr(object->instance, #name);
625 ALL_VK_INSTANCE_FUNCS()
626 #undef USE_VK_FUNC
628 /* Cache physical devices for vkEnumeratePhysicalDevices within the instance as
629 * each vkPhysicalDevice is a dispatchable object, which means we need to wrap
630 * the native physical devices and present those to the application.
631 * Cleanup happens as part of wine_vkDestroyInstance.
633 res = wine_vk_instance_load_physical_devices(object);
634 if (res != VK_SUCCESS)
636 ERR("Failed to load physical devices, res=%d\n", res);
637 wine_vk_instance_free(object);
638 return res;
641 if ((app_info = create_info->pApplicationInfo) && app_info->pApplicationName)
643 if (!strcmp(app_info->pApplicationName, "DOOM")
644 || !strcmp(app_info->pApplicationName, "Wolfenstein II The New Colossus"))
645 object->quirks |= WINEVULKAN_QUIRK_GET_DEVICE_PROC_ADDR;
648 *instance = object;
649 TRACE("Done, instance=%p native_instance=%p\n", object, object->instance);
650 return VK_SUCCESS;
653 void WINAPI wine_vkDestroyDevice(VkDevice device, const VkAllocationCallbacks *allocator)
655 TRACE("%p %p\n", device, allocator);
657 if (allocator)
658 FIXME("Support for allocation callbacks not implemented yet\n");
660 wine_vk_device_free(device);
663 void WINAPI wine_vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks *allocator)
665 TRACE("%p, %p\n", instance, allocator);
667 if (allocator)
668 FIXME("Support allocation allocators\n");
670 wine_vk_instance_free(instance);
673 VkResult WINAPI wine_vkEnumerateDeviceExtensionProperties(VkPhysicalDevice phys_dev,
674 const char *layer_name, uint32_t *count, VkExtensionProperties *properties)
676 TRACE("%p, %p, %p, %p\n", phys_dev, layer_name, count, properties);
678 /* This shouldn't get called with layer_name set, the ICD loader prevents it. */
679 if (layer_name)
681 ERR("Layer enumeration not supported from ICD.\n");
682 return VK_ERROR_LAYER_NOT_PRESENT;
685 if (!properties)
687 *count = phys_dev->extension_count;
688 return VK_SUCCESS;
691 *count = min(*count, phys_dev->extension_count);
692 memcpy(properties, phys_dev->extensions, *count * sizeof(*properties));
694 TRACE("Returning %u extensions.\n", *count);
695 return *count < phys_dev->extension_count ? VK_INCOMPLETE : VK_SUCCESS;
698 VkResult WINAPI wine_vkEnumerateInstanceExtensionProperties(const char *layer_name,
699 uint32_t *count, VkExtensionProperties *properties)
701 VkResult res;
702 uint32_t num_properties = 0, num_host_properties = 0;
703 VkExtensionProperties *host_properties = NULL;
704 unsigned int i, j;
706 TRACE("%p %p %p\n", layer_name, count, properties);
708 /* This shouldn't get called with layer_name set, the ICD loader prevents it. */
709 if (layer_name)
711 ERR("Layer enumeration not supported from ICD.\n");
712 return VK_ERROR_LAYER_NOT_PRESENT;
715 res = vk_funcs->p_vkEnumerateInstanceExtensionProperties(NULL, &num_host_properties, NULL);
716 if (res != VK_SUCCESS)
717 return res;
719 host_properties = heap_calloc(num_host_properties, sizeof(*host_properties));
720 if (!host_properties)
721 return VK_ERROR_OUT_OF_HOST_MEMORY;
723 res = vk_funcs->p_vkEnumerateInstanceExtensionProperties(NULL, &num_host_properties, host_properties);
724 if (res != VK_SUCCESS)
726 ERR("Failed to retrieve host properties, res=%d\n", res);
727 heap_free(host_properties);
728 return res;
731 /* The Wine graphics driver provides us with all extensions supported by the host side
732 * including extension fixup (e.g. VK_KHR_xlib_surface -> VK_KHR_win32_surface). It is
733 * up to us here to filter the list down to extensions for which we have thunks.
735 for (i = 0; i < num_host_properties; i++)
737 if (wine_vk_instance_extension_supported(host_properties[i].extensionName))
738 num_properties++;
741 /* We only have to count. */
742 if (!properties)
744 TRACE("Returning %u extensions\n", num_properties);
745 *count = num_properties;
746 heap_free(host_properties);
747 return VK_SUCCESS;
750 for (i = 0, j = 0; i < num_host_properties && j < *count; i++)
752 if (wine_vk_instance_extension_supported(host_properties[i].extensionName))
754 TRACE("Enabling extension '%s'\n", host_properties[i].extensionName);
755 properties[j] = host_properties[i];
756 j++;
759 *count = min(*count, num_properties);
761 heap_free(host_properties);
762 return *count < num_properties ? VK_INCOMPLETE : VK_SUCCESS;
765 VkResult WINAPI wine_vkEnumeratePhysicalDevices(VkInstance instance, uint32_t *count,
766 VkPhysicalDevice *devices)
768 unsigned int i;
770 TRACE("%p %p %p\n", instance, count, devices);
772 if (!devices)
774 *count = instance->num_phys_devs;
775 return VK_SUCCESS;
778 *count = min(*count, instance->num_phys_devs);
779 for (i = 0; i < *count; i++)
781 devices[i] = instance->phys_devs[i];
784 TRACE("Returning %u devices.\n", *count);
785 return *count < instance->num_phys_devs ? VK_INCOMPLETE : VK_SUCCESS;
788 void WINAPI wine_vkFreeCommandBuffers(VkDevice device, VkCommandPool pool, uint32_t count,
789 const VkCommandBuffer *buffers)
791 TRACE("%p 0x%s %u %p\n", device, wine_dbgstr_longlong(pool), count, buffers);
793 wine_vk_command_buffers_free(device, pool, count, buffers);
796 PFN_vkVoidFunction WINAPI wine_vkGetDeviceProcAddr(VkDevice device, const char *name)
798 void *func;
799 TRACE("%p, %s\n", device, debugstr_a(name));
801 /* The spec leaves return value undefined for a NULL device, let's just return NULL. */
802 if (!device || !name)
803 return NULL;
805 /* Per the spec, we are only supposed to return device functions as in functions
806 * for which the first parameter is vkDevice or a child of vkDevice like a
807 * vkCommandBuffer or vkQueue.
808 * Loader takes are of filtering of extensions which are enabled or not.
810 func = wine_vk_get_device_proc_addr(name);
811 if (func)
812 return func;
814 /* vkGetDeviceProcAddr was intended for loading device and subdevice functions.
815 * idTech 6 titles such as Doom and Wolfenstein II, however use it also for
816 * loading of instance functions. This is undefined behavior as the specification
817 * disallows using any of the returned function pointers outside of device /
818 * subdevice objects. The games don't actually use the function pointers and if they
819 * did, they would crash as VkInstance / VkPhysicalDevice parameters need unwrapping.
820 * Khronos clarified behavior in the Vulkan spec and expects drivers to get updated,
821 * however it would require both driver and game fixes.
822 * https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/issues/2323
823 * https://github.com/KhronosGroup/Vulkan-Docs/issues/655
825 if (device->quirks & WINEVULKAN_QUIRK_GET_DEVICE_PROC_ADDR
826 && (func = wine_vk_get_instance_proc_addr(name)))
828 WARN("Returning instance function %s.\n", debugstr_a(name));
829 return func;
832 TRACE("Function %s not found.\n", debugstr_a(name));
833 return NULL;
836 void WINAPI wine_vkGetDeviceQueue(VkDevice device, uint32_t family_index,
837 uint32_t queue_index, VkQueue *queue)
839 TRACE("%p %u %u %p\n", device, family_index, queue_index, queue);
841 *queue = &device->queues[family_index][queue_index];
844 PFN_vkVoidFunction WINAPI wine_vkGetInstanceProcAddr(VkInstance instance, const char *name)
846 void *func;
848 TRACE("%p %s\n", instance, debugstr_a(name));
850 if (!name)
851 return NULL;
853 /* vkGetInstanceProcAddr can load most Vulkan functions when an instance is passed in, however
854 * for a NULL instance it can only load global functions.
856 func = wine_vk_get_global_proc_addr(name);
857 if (func)
859 return func;
861 if (!instance)
863 FIXME("Global function %s not found.\n", debugstr_a(name));
864 return NULL;
867 func = wine_vk_get_instance_proc_addr(name);
868 if (func) return func;
870 /* vkGetInstanceProcAddr also loads any children of instance, so device functions as well. */
871 func = wine_vk_get_device_proc_addr(name);
872 if (func) return func;
874 FIXME("Unsupported device or instance function: %s.\n", debugstr_a(name));
875 return NULL;
878 void * WINAPI wine_vk_icdGetInstanceProcAddr(VkInstance instance, const char *name)
880 TRACE("%p %s\n", instance, debugstr_a(name));
882 /* Initial version of the Vulkan ICD spec required vkGetInstanceProcAddr to be
883 * exported. vk_icdGetInstanceProcAddr was added later to separate ICD calls from
884 * Vulkan API. One of them in our case should forward to the other, so just forward
885 * to the older vkGetInstanceProcAddr.
887 return wine_vkGetInstanceProcAddr(instance, name);
890 VkResult WINAPI wine_vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *supported_version)
892 uint32_t req_version;
894 TRACE("%p\n", supported_version);
896 /* The spec is not clear how to handle this. Mesa drivers don't check, but it
897 * is probably best to not explode. VK_INCOMPLETE seems to be the closest value.
899 if (!supported_version)
900 return VK_INCOMPLETE;
902 req_version = *supported_version;
903 *supported_version = min(req_version, WINE_VULKAN_ICD_VERSION);
904 TRACE("Loader requested ICD version %u, returning %u\n", req_version, *supported_version);
906 return VK_SUCCESS;
909 VkResult WINAPI wine_vkQueueSubmit(VkQueue queue, uint32_t count,
910 const VkSubmitInfo *submits, VkFence fence)
912 VkSubmitInfo *submits_host;
913 VkResult res;
914 VkCommandBuffer *command_buffers;
915 unsigned int i, j, num_command_buffers;
917 TRACE("%p %u %p 0x%s\n", queue, count, submits, wine_dbgstr_longlong(fence));
919 if (count == 0)
921 return queue->device->funcs.p_vkQueueSubmit(queue->queue, 0, NULL, fence);
924 submits_host = heap_calloc(count, sizeof(*submits_host));
925 if (!submits_host)
927 ERR("Unable to allocate memory for submit buffers!\n");
928 return VK_ERROR_OUT_OF_HOST_MEMORY;
931 for (i = 0; i < count; i++)
933 memcpy(&submits_host[i], &submits[i], sizeof(*submits_host));
935 num_command_buffers = submits[i].commandBufferCount;
936 command_buffers = heap_calloc(num_command_buffers, sizeof(*submits_host));
937 if (!command_buffers)
939 ERR("Unable to allocate memory for comman buffers!\n");
940 res = VK_ERROR_OUT_OF_HOST_MEMORY;
941 goto err;
944 for (j = 0; j < num_command_buffers; j++)
946 command_buffers[j] = submits[i].pCommandBuffers[j]->command_buffer;
948 submits_host[i].pCommandBuffers = command_buffers;
951 res = queue->device->funcs.p_vkQueueSubmit(queue->queue, count, submits_host, fence);
953 err:
954 for (i = 0; i < count; i++)
956 heap_free((void *)submits_host[i].pCommandBuffers);
958 heap_free(submits_host);
960 TRACE("Returning %d\n", res);
961 return res;
965 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, void *reserved)
967 switch (reason)
969 case DLL_PROCESS_ATTACH:
970 DisableThreadLibraryCalls(hinst);
971 return wine_vk_init();
973 return TRUE;
976 static const struct vulkan_func vk_global_dispatch_table[] =
978 {"vkCreateInstance", &wine_vkCreateInstance},
979 {"vkEnumerateInstanceExtensionProperties", &wine_vkEnumerateInstanceExtensionProperties},
980 {"vkGetInstanceProcAddr", &wine_vkGetInstanceProcAddr},
983 static void *wine_vk_get_global_proc_addr(const char *name)
985 unsigned int i;
987 for (i = 0; i < ARRAY_SIZE(vk_global_dispatch_table); i++)
989 if (strcmp(name, vk_global_dispatch_table[i].name) == 0)
991 TRACE("Found name=%s in global table\n", debugstr_a(name));
992 return vk_global_dispatch_table[i].func;
995 return NULL;