winevulkan: Avoid goto in wine_vk_instance_load_physical_devices().
[wine.git] / dlls / winevulkan / vulkan.c
blob4c9874c78473351eb8f47162c8178dd79e9afe40
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 goto err;
538 object->phys_dev = phys_dev;
540 /* Just load all function pointers we are aware off. The loader takes care of filtering.
541 * We use vkGetDeviceProcAddr as opposed to vkGetInstanceProcAddr for efficiency reasons
542 * as functions pass through fewer dispatch tables within the loader.
544 #define USE_VK_FUNC(name) \
545 object->funcs.p_##name = (void *)vk_funcs->p_vkGetDeviceProcAddr(object->device, #name); \
546 if (object->funcs.p_##name == NULL) \
547 TRACE("Not found %s\n", #name);
548 ALL_VK_DEVICE_FUNCS()
549 #undef USE_VK_FUNC
551 /* We need to cache all queues within the device as each requires wrapping since queues are
552 * dispatchable objects.
554 phys_dev->instance->funcs.p_vkGetPhysicalDeviceQueueFamilyProperties(phys_dev->phys_dev,
555 &max_queue_families, NULL);
556 object->max_queue_families = max_queue_families;
557 TRACE("Max queue families: %u\n", object->max_queue_families);
559 object->queues = heap_calloc(max_queue_families, sizeof(*object->queues));
560 if (!object->queues)
562 res = VK_ERROR_OUT_OF_HOST_MEMORY;
563 goto err;
566 for (i = 0; i < create_info_host.queueCreateInfoCount; i++)
568 uint32_t family_index = create_info_host.pQueueCreateInfos[i].queueFamilyIndex;
569 uint32_t queue_count = create_info_host.pQueueCreateInfos[i].queueCount;
571 TRACE("queueFamilyIndex %u, queueCount %u\n", family_index, queue_count);
573 object->queues[family_index] = wine_vk_device_alloc_queues(object, family_index, queue_count);
574 if (!object->queues[family_index])
576 res = VK_ERROR_OUT_OF_HOST_MEMORY;
577 ERR("Failed to allocate memory for queues\n");
578 goto err;
582 *device = object;
583 return VK_SUCCESS;
585 err:
586 wine_vk_device_free(object);
587 return res;
590 VkResult WINAPI wine_vkCreateInstance(const VkInstanceCreateInfo *create_info,
591 const VkAllocationCallbacks *allocator, VkInstance *instance)
593 struct VkInstance_T *object = NULL;
594 VkInstanceCreateInfo create_info_host;
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 object = heap_alloc_zero(sizeof(*object));
603 if (!object)
605 ERR("Failed to allocate memory for instance\n");
606 res = VK_ERROR_OUT_OF_HOST_MEMORY;
607 goto err;
609 object->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
611 wine_vk_instance_convert_create_info(create_info, &create_info_host);
613 res = vk_funcs->p_vkCreateInstance(&create_info_host, NULL /* allocator */, &object->instance);
614 if (res != VK_SUCCESS)
616 ERR("Failed to create instance, res=%d\n", res);
617 goto err;
620 /* Load all instance functions we are aware of. Note the loader takes care
621 * of any filtering for extensions which were not requested, but which the
622 * ICD may support.
624 #define USE_VK_FUNC(name) \
625 object->funcs.p_##name = (void *)vk_funcs->p_vkGetInstanceProcAddr(object->instance, #name);
626 ALL_VK_INSTANCE_FUNCS()
627 #undef USE_VK_FUNC
629 /* Cache physical devices for vkEnumeratePhysicalDevices within the instance as
630 * each vkPhysicalDevice is a dispatchable object, which means we need to wrap
631 * the native physical device and present those the application.
632 * Cleanup happens as part of wine_vkDestroyInstance.
634 res = wine_vk_instance_load_physical_devices(object);
635 if (res != VK_SUCCESS)
637 ERR("Failed to cache physical devices, res=%d\n", res);
638 goto err;
641 *instance = object;
642 TRACE("Done, instance=%p native_instance=%p\n", object, object->instance);
643 return VK_SUCCESS;
645 err:
646 wine_vk_instance_free(object);
647 return res;
650 void WINAPI wine_vkDestroyDevice(VkDevice device, const VkAllocationCallbacks *allocator)
652 TRACE("%p %p\n", device, allocator);
654 if (allocator)
655 FIXME("Support for allocation callbacks not implemented yet\n");
657 wine_vk_device_free(device);
660 void WINAPI wine_vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks *allocator)
662 TRACE("%p, %p\n", instance, allocator);
664 if (allocator)
665 FIXME("Support allocation allocators\n");
667 wine_vk_instance_free(instance);
670 VkResult WINAPI wine_vkEnumerateDeviceExtensionProperties(VkPhysicalDevice phys_dev,
671 const char *layer_name, uint32_t *count, VkExtensionProperties *properties)
673 TRACE("%p, %p, %p, %p\n", phys_dev, layer_name, count, properties);
675 /* This shouldn't get called with layer_name set, the ICD loader prevents it. */
676 if (layer_name)
678 ERR("Layer enumeration not supported from ICD.\n");
679 return VK_ERROR_LAYER_NOT_PRESENT;
682 if (!properties)
684 *count = phys_dev->extension_count;
685 return VK_SUCCESS;
688 *count = min(*count, phys_dev->extension_count);
689 memcpy(properties, phys_dev->extensions, *count * sizeof(*properties));
691 TRACE("Returning %u extensions.\n", *count);
692 return *count < phys_dev->extension_count ? VK_INCOMPLETE : VK_SUCCESS;
695 VkResult WINAPI wine_vkEnumerateInstanceExtensionProperties(const char *layer_name,
696 uint32_t *count, VkExtensionProperties *properties)
698 VkResult res;
699 uint32_t num_properties = 0, num_host_properties = 0;
700 VkExtensionProperties *host_properties = NULL;
701 unsigned int i, j;
703 TRACE("%p %p %p\n", layer_name, count, properties);
705 /* This shouldn't get called with layer_name set, the ICD loader prevents it. */
706 if (layer_name)
708 ERR("Layer enumeration not supported from ICD.\n");
709 return VK_ERROR_LAYER_NOT_PRESENT;
712 res = vk_funcs->p_vkEnumerateInstanceExtensionProperties(NULL, &num_host_properties, NULL);
713 if (res != VK_SUCCESS)
714 return res;
716 host_properties = heap_calloc(num_host_properties, sizeof(*host_properties));
717 if (!host_properties)
718 return VK_ERROR_OUT_OF_HOST_MEMORY;
720 res = vk_funcs->p_vkEnumerateInstanceExtensionProperties(NULL, &num_host_properties, host_properties);
721 if (res != VK_SUCCESS)
723 ERR("Failed to retrieve host properties, res=%d\n", res);
724 heap_free(host_properties);
725 return res;
728 /* The Wine graphics driver provides us with all extensions supported by the host side
729 * including extension fixup (e.g. VK_KHR_xlib_surface -> VK_KHR_win32_surface). It is
730 * up to us here to filter the list down to extensions for which we have thunks.
732 for (i = 0; i < num_host_properties; i++)
734 if (wine_vk_instance_extension_supported(host_properties[i].extensionName))
735 num_properties++;
738 /* We only have to count. */
739 if (!properties)
741 TRACE("Returning %u extensions\n", num_properties);
742 *count = num_properties;
743 heap_free(host_properties);
744 return VK_SUCCESS;
747 for (i = 0, j = 0; i < num_host_properties && j < *count; i++)
749 if (wine_vk_instance_extension_supported(host_properties[i].extensionName))
751 TRACE("Enabling extension '%s'\n", host_properties[i].extensionName);
752 properties[j] = host_properties[i];
753 j++;
756 *count = min(*count, num_properties);
758 heap_free(host_properties);
759 return *count < num_properties ? VK_INCOMPLETE : VK_SUCCESS;
762 VkResult WINAPI wine_vkEnumeratePhysicalDevices(VkInstance instance, uint32_t *count,
763 VkPhysicalDevice *devices)
765 unsigned int i;
767 TRACE("%p %p %p\n", instance, count, devices);
769 if (!devices)
771 *count = instance->num_phys_devs;
772 return VK_SUCCESS;
775 *count = min(*count, instance->num_phys_devs);
776 for (i = 0; i < *count; i++)
778 devices[i] = instance->phys_devs[i];
781 TRACE("Returning %u devices.\n", *count);
782 return *count < instance->num_phys_devs ? VK_INCOMPLETE : VK_SUCCESS;
785 void WINAPI wine_vkFreeCommandBuffers(VkDevice device, VkCommandPool pool, uint32_t count,
786 const VkCommandBuffer *buffers)
788 TRACE("%p 0x%s %u %p\n", device, wine_dbgstr_longlong(pool), count, buffers);
790 wine_vk_command_buffers_free(device, pool, count, buffers);
793 PFN_vkVoidFunction WINAPI wine_vkGetDeviceProcAddr(VkDevice device, const char *name)
795 void *func;
796 TRACE("%p, %s\n", device, debugstr_a(name));
798 /* The spec leaves return value undefined for a NULL device, let's just return NULL. */
799 if (!device || !name)
800 return NULL;
802 /* Per the spec, we are only supposed to return device functions as in functions
803 * for which the first parameter is vkDevice or a child of vkDevice like a
804 * vkCommandBuffer or vkQueue.
805 * Loader takes are of filtering of extensions which are enabled or not.
807 func = wine_vk_get_device_proc_addr(name);
808 if (func)
809 return func;
811 /* vkGetDeviceProcAddr was intended for loading device and subdevice functions.
812 * idTech 6 titles such as Doom and Wolfenstein II, however use it also for
813 * loading of instance functions. This is undefined behavior as the specification
814 * disallows using any of the returned function pointers outside of device /
815 * subdevice objects. The games don't actually use the function pointers and if they
816 * did, they would crash as VkInstance / VkPhysicalDevice parameters need unwrapping.
817 * Khronos clarified behavior in the Vulkan spec and expects drivers to get updated,
818 * however it would require both driver and game fixes. Since it are major titles
819 * it is not clear what will happen. At least for now we need the hack below.
820 * https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/issues/2323
821 * https://github.com/KhronosGroup/Vulkan-Docs/issues/655
823 func = wine_vk_get_instance_proc_addr(name);
824 if (func)
826 WARN("Returning instance function %s.\n", debugstr_a(name));
827 return func;
830 TRACE("Function %s not found.\n", debugstr_a(name));
831 return NULL;
834 void WINAPI wine_vkGetDeviceQueue(VkDevice device, uint32_t family_index,
835 uint32_t queue_index, VkQueue *queue)
837 TRACE("%p %u %u %p\n", device, family_index, queue_index, queue);
839 *queue = &device->queues[family_index][queue_index];
842 PFN_vkVoidFunction WINAPI wine_vkGetInstanceProcAddr(VkInstance instance, const char *name)
844 void *func;
846 TRACE("%p %s\n", instance, debugstr_a(name));
848 if (!name)
849 return NULL;
851 /* vkGetInstanceProcAddr can load most Vulkan functions when an instance is passed in, however
852 * for a NULL instance it can only load global functions.
854 func = wine_vk_get_global_proc_addr(name);
855 if (func)
857 return func;
859 if (!instance)
861 FIXME("Global function %s not found.\n", debugstr_a(name));
862 return NULL;
865 func = wine_vk_get_instance_proc_addr(name);
866 if (func) return func;
868 /* vkGetInstanceProcAddr also loads any children of instance, so device functions as well. */
869 func = wine_vk_get_device_proc_addr(name);
870 if (func) return func;
872 FIXME("Unsupported device or instance function: %s.\n", debugstr_a(name));
873 return NULL;
876 void * WINAPI wine_vk_icdGetInstanceProcAddr(VkInstance instance, const char *name)
878 TRACE("%p %s\n", instance, debugstr_a(name));
880 /* Initial version of the Vulkan ICD spec required vkGetInstanceProcAddr to be
881 * exported. vk_icdGetInstanceProcAddr was added later to separate ICD calls from
882 * Vulkan API. One of them in our case should forward to the other, so just forward
883 * to the older vkGetInstanceProcAddr.
885 return wine_vkGetInstanceProcAddr(instance, name);
888 VkResult WINAPI wine_vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *supported_version)
890 uint32_t req_version;
892 TRACE("%p\n", supported_version);
894 /* The spec is not clear how to handle this. Mesa drivers don't check, but it
895 * is probably best to not explode. VK_INCOMPLETE seems to be the closest value.
897 if (!supported_version)
898 return VK_INCOMPLETE;
900 req_version = *supported_version;
901 *supported_version = min(req_version, WINE_VULKAN_ICD_VERSION);
902 TRACE("Loader requested ICD version %u, returning %u\n", req_version, *supported_version);
904 return VK_SUCCESS;
907 VkResult WINAPI wine_vkQueueSubmit(VkQueue queue, uint32_t count,
908 const VkSubmitInfo *submits, VkFence fence)
910 VkSubmitInfo *submits_host;
911 VkResult res;
912 VkCommandBuffer *command_buffers;
913 unsigned int i, j, num_command_buffers;
915 TRACE("%p %u %p 0x%s\n", queue, count, submits, wine_dbgstr_longlong(fence));
917 if (count == 0)
919 return queue->device->funcs.p_vkQueueSubmit(queue->queue, 0, NULL, fence);
922 submits_host = heap_calloc(count, sizeof(*submits_host));
923 if (!submits_host)
925 ERR("Unable to allocate memory for submit buffers!\n");
926 return VK_ERROR_OUT_OF_HOST_MEMORY;
929 for (i = 0; i < count; i++)
931 memcpy(&submits_host[i], &submits[i], sizeof(*submits_host));
933 num_command_buffers = submits[i].commandBufferCount;
934 command_buffers = heap_calloc(num_command_buffers, sizeof(*submits_host));
935 if (!command_buffers)
937 ERR("Unable to allocate memory for comman buffers!\n");
938 res = VK_ERROR_OUT_OF_HOST_MEMORY;
939 goto err;
942 for (j = 0; j < num_command_buffers; j++)
944 command_buffers[j] = submits[i].pCommandBuffers[j]->command_buffer;
946 submits_host[i].pCommandBuffers = command_buffers;
949 res = queue->device->funcs.p_vkQueueSubmit(queue->queue, count, submits_host, fence);
951 err:
952 for (i = 0; i < count; i++)
954 heap_free((void *)submits_host[i].pCommandBuffers);
956 heap_free(submits_host);
958 TRACE("Returning %d\n", res);
959 return res;
963 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, void *reserved)
965 switch (reason)
967 case DLL_PROCESS_ATTACH:
968 DisableThreadLibraryCalls(hinst);
969 return wine_vk_init();
971 return TRUE;
974 static const struct vulkan_func vk_global_dispatch_table[] =
976 {"vkCreateInstance", &wine_vkCreateInstance},
977 {"vkEnumerateInstanceExtensionProperties", &wine_vkEnumerateInstanceExtensionProperties},
978 {"vkGetInstanceProcAddr", &wine_vkGetInstanceProcAddr},
981 static void *wine_vk_get_global_proc_addr(const char *name)
983 unsigned int i;
985 for (i = 0; i < ARRAY_SIZE(vk_global_dispatch_table); i++)
987 if (strcmp(name, vk_global_dispatch_table[i].name) == 0)
989 TRACE("Found name=%s in global table\n", debugstr_a(name));
990 return vk_global_dispatch_table[i].func;
993 return NULL;