d3d8: Do not specify WINED3D_TEXTURE_CREATE_MAPPABLE in d3d8_device_CreateDepthStenci...
[wine.git] / dlls / winevulkan / vulkan.c
blob1c721039f2f33c35cbb0323950ceb208c44f8272
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 "vulkan_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(vulkan);
30 /* For now default to 4 as it felt like a reasonable version feature wise to support.
31 * Don't support the optional vk_icdGetPhysicalDeviceProcAddr introduced in this version
32 * as it is unlikely we will implement physical device extensions, which the loader is not
33 * aware of. Version 5 adds more extensive version checks. Something to tackle later.
35 #define WINE_VULKAN_ICD_VERSION 4
37 /* All Vulkan structures use this structure for the first elements. */
38 struct wine_vk_structure_header
40 VkStructureType sType;
41 void *pNext;
44 #define wine_vk_find_struct(s, t) wine_vk_find_struct_((void *)s, VK_STRUCTURE_TYPE_##t)
45 static void *wine_vk_find_struct_(void *s, VkStructureType t)
47 struct wine_vk_structure_header *header;
49 for (header = s; header; header = header->pNext)
51 if (header->sType == t)
52 return header;
55 return NULL;
58 static void *wine_vk_get_global_proc_addr(const char *name);
60 static const struct vulkan_funcs *vk_funcs;
61 static VkResult (*p_vkEnumerateInstanceVersion)(uint32_t *version);
63 void WINAPI wine_vkGetPhysicalDeviceProperties(VkPhysicalDevice physical_device,
64 VkPhysicalDeviceProperties *properties);
66 static void wine_vk_physical_device_free(struct VkPhysicalDevice_T *phys_dev)
68 if (!phys_dev)
69 return;
71 heap_free(phys_dev->extensions);
72 heap_free(phys_dev);
75 static struct VkPhysicalDevice_T *wine_vk_physical_device_alloc(struct VkInstance_T *instance,
76 VkPhysicalDevice phys_dev)
78 struct VkPhysicalDevice_T *object;
79 uint32_t num_host_properties, num_properties = 0;
80 VkExtensionProperties *host_properties = NULL;
81 VkResult res;
82 unsigned int i, j;
84 if (!(object = heap_alloc_zero(sizeof(*object))))
85 return NULL;
87 object->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
88 object->instance = instance;
89 object->phys_dev = phys_dev;
91 res = instance->funcs.p_vkEnumerateDeviceExtensionProperties(phys_dev,
92 NULL, &num_host_properties, NULL);
93 if (res != VK_SUCCESS)
95 ERR("Failed to enumerate device extensions, res=%d\n", res);
96 goto err;
99 host_properties = heap_calloc(num_host_properties, sizeof(*host_properties));
100 if (!host_properties)
102 ERR("Failed to allocate memory for device properties!\n");
103 goto err;
106 res = instance->funcs.p_vkEnumerateDeviceExtensionProperties(phys_dev,
107 NULL, &num_host_properties, host_properties);
108 if (res != VK_SUCCESS)
110 ERR("Failed to enumerate device extensions, res=%d\n", res);
111 goto err;
114 /* Count list of extensions for which we have an implementation.
115 * TODO: perform translation for platform specific extensions.
117 for (i = 0; i < num_host_properties; i++)
119 if (wine_vk_device_extension_supported(host_properties[i].extensionName))
121 TRACE("Enabling extension '%s' for physical device %p\n", host_properties[i].extensionName, object);
122 num_properties++;
124 else
126 TRACE("Skipping extension '%s', no implementation found in winevulkan.\n", host_properties[i].extensionName);
130 TRACE("Host supported extensions %u, Wine supported extensions %u\n", num_host_properties, num_properties);
132 if (!(object->extensions = heap_calloc(num_properties, sizeof(*object->extensions))))
134 ERR("Failed to allocate memory for device extensions!\n");
135 goto err;
138 for (i = 0, j = 0; i < num_host_properties; i++)
140 if (wine_vk_device_extension_supported(host_properties[i].extensionName))
142 object->extensions[j] = host_properties[i];
143 j++;
146 object->extension_count = num_properties;
148 heap_free(host_properties);
149 return object;
151 err:
152 wine_vk_physical_device_free(object);
153 heap_free(host_properties);
154 return NULL;
157 static void wine_vk_free_command_buffers(struct VkDevice_T *device,
158 struct wine_cmd_pool *pool, uint32_t count, const VkCommandBuffer *buffers)
160 unsigned int i;
162 for (i = 0; i < count; i++)
164 if (!buffers[i])
165 continue;
167 device->funcs.p_vkFreeCommandBuffers(device->device, pool->command_pool, 1, &buffers[i]->command_buffer);
168 list_remove(&buffers[i]->pool_link);
169 heap_free(buffers[i]);
173 static struct VkQueue_T *wine_vk_device_alloc_queues(struct VkDevice_T *device,
174 uint32_t family_index, uint32_t queue_count, VkDeviceQueueCreateFlags flags)
176 VkDeviceQueueInfo2 queue_info;
177 struct VkQueue_T *queues;
178 unsigned int i;
180 if (!(queues = heap_calloc(queue_count, sizeof(*queues))))
182 ERR("Failed to allocate memory for queues\n");
183 return NULL;
186 for (i = 0; i < queue_count; i++)
188 struct VkQueue_T *queue = &queues[i];
190 queue->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
191 queue->device = device;
192 queue->flags = flags;
194 /* The Vulkan spec says:
196 * "vkGetDeviceQueue must only be used to get queues that were created
197 * with the flags parameter of VkDeviceQueueCreateInfo set to zero."
199 if (flags && device->funcs.p_vkGetDeviceQueue2)
201 queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2;
202 queue_info.pNext = NULL;
203 queue_info.flags = flags;
204 queue_info.queueFamilyIndex = family_index;
205 queue_info.queueIndex = i;
206 device->funcs.p_vkGetDeviceQueue2(device->device, &queue_info, &queue->queue);
208 else
210 device->funcs.p_vkGetDeviceQueue(device->device, family_index, i, &queue->queue);
214 return queues;
217 static VkDeviceGroupDeviceCreateInfo *convert_VkDeviceGroupDeviceCreateInfo(const void *src)
219 const VkDeviceGroupDeviceCreateInfo *in = src;
220 VkDeviceGroupDeviceCreateInfo *out;
221 VkPhysicalDevice *physical_devices;
222 unsigned int i;
224 if (!(out = heap_alloc(sizeof(*out))))
225 return NULL;
227 *out = *in;
228 if (!(physical_devices = heap_calloc(in->physicalDeviceCount, sizeof(*physical_devices))))
230 heap_free(out);
231 return NULL;
233 for (i = 0; i < in->physicalDeviceCount; ++i)
234 physical_devices[i] = in->pPhysicalDevices[i]->phys_dev;
235 out->pPhysicalDevices = physical_devices;
237 return out;
240 static VkResult wine_vk_device_convert_create_info(const VkDeviceCreateInfo *src,
241 VkDeviceCreateInfo *dst)
243 unsigned int i;
245 *dst = *src;
247 /* Application and loader can pass in a chain of extensions through pNext.
248 * We can't blindly pass these through as often these contain callbacks or
249 * they can even be pass structures for loader / ICD internal use.
251 if (src->pNext)
253 const struct wine_vk_structure_header *header;
255 dst->pNext = NULL;
256 for (header = src->pNext; header; header = header->pNext)
258 switch (header->sType)
260 case VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO:
261 /* Used for loader to ICD communication. Ignore to not confuse
262 * host loader.
264 break;
266 case VK_STRUCTURE_TYPE_DEVICE_GROUP_DEVICE_CREATE_INFO:
267 if (!(dst->pNext = convert_VkDeviceGroupDeviceCreateInfo(header)))
268 return VK_ERROR_OUT_OF_HOST_MEMORY;
269 break;
271 default:
272 FIXME("Application requested a linked structure of type %#x.\n", header->sType);
277 /* Should be filtered out by loader as ICDs don't support layers. */
278 dst->enabledLayerCount = 0;
279 dst->ppEnabledLayerNames = NULL;
281 TRACE("Enabled %u extensions.\n", dst->enabledExtensionCount);
282 for (i = 0; i < dst->enabledExtensionCount; i++)
284 const char *extension_name = dst->ppEnabledExtensionNames[i];
285 TRACE("Extension %u: %s.\n", i, debugstr_a(extension_name));
286 if (!wine_vk_device_extension_supported(extension_name))
288 WARN("Extension %s is not supported.\n", debugstr_a(extension_name));
289 return VK_ERROR_EXTENSION_NOT_PRESENT;
293 return VK_SUCCESS;
296 static void wine_vk_device_free_create_info(VkDeviceCreateInfo *create_info)
298 VkDeviceGroupDeviceCreateInfo *group_info;
300 if ((group_info = wine_vk_find_struct(create_info, DEVICE_GROUP_DEVICE_CREATE_INFO)))
302 heap_free((void *)group_info->pPhysicalDevices);
303 heap_free(group_info);
306 create_info->pNext = NULL;
309 /* Helper function used for freeing a device structure. This function supports full
310 * and partial object cleanups and can thus be used for vkCreateDevice failures.
312 static void wine_vk_device_free(struct VkDevice_T *device)
314 if (!device)
315 return;
317 if (device->queues)
319 unsigned int i;
320 for (i = 0; i < device->max_queue_families; i++)
322 heap_free(device->queues[i]);
324 heap_free(device->queues);
325 device->queues = NULL;
328 if (device->device && device->funcs.p_vkDestroyDevice)
330 device->funcs.p_vkDestroyDevice(device->device, NULL /* pAllocator */);
333 heap_free(device);
336 static BOOL wine_vk_init(void)
338 HDC hdc;
340 hdc = GetDC(0);
341 vk_funcs = __wine_get_vulkan_driver(hdc, WINE_VULKAN_DRIVER_VERSION);
342 ReleaseDC(0, hdc);
343 if (!vk_funcs)
345 ERR("Failed to load Wine graphics driver supporting Vulkan.\n");
346 return FALSE;
349 p_vkEnumerateInstanceVersion = vk_funcs->p_vkGetInstanceProcAddr(NULL, "vkEnumerateInstanceVersion");
351 return TRUE;
354 /* Helper function for converting between win32 and host compatible VkInstanceCreateInfo.
355 * This function takes care of extensions handled at winevulkan layer, a Wine graphics
356 * driver is responsible for handling e.g. surface extensions.
358 static VkResult wine_vk_instance_convert_create_info(const VkInstanceCreateInfo *src,
359 VkInstanceCreateInfo *dst)
361 unsigned int i;
363 *dst = *src;
365 /* Application and loader can pass in a chain of extensions through pNext.
366 * We can't blindly pass these through as often these contain callbacks or
367 * they can even be pass structures for loader / ICD internal use. For now
368 * we ignore everything in pNext chain, but we print FIXMEs.
370 if (src->pNext)
372 const struct wine_vk_structure_header *header;
374 for (header = src->pNext; header; header = header->pNext)
376 switch (header->sType)
378 case VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO:
379 /* Can be used to register new dispatchable object types
380 * to the loader. We should ignore it as it will confuse the
381 * host its loader.
383 break;
385 default:
386 FIXME("Application requested a linked structure of type %#x.\n", header->sType);
390 /* For now don't support anything. */
391 dst->pNext = NULL;
393 /* ICDs don't support any layers, so nothing to copy. Modern versions of the loader
394 * filter this data out as well.
396 dst->enabledLayerCount = 0;
397 dst->ppEnabledLayerNames = NULL;
399 TRACE("Enabled %u instance extensions.\n", dst->enabledExtensionCount);
400 for (i = 0; i < dst->enabledExtensionCount; i++)
402 const char *extension_name = dst->ppEnabledExtensionNames[i];
403 TRACE("Extension %u: %s.\n", i, debugstr_a(extension_name));
404 if (!wine_vk_instance_extension_supported(extension_name))
406 WARN("Extension %s is not supported.\n", debugstr_a(extension_name));
407 return VK_ERROR_EXTENSION_NOT_PRESENT;
411 return VK_SUCCESS;
414 /* Helper function which stores wrapped physical devices in the instance object. */
415 static VkResult wine_vk_instance_load_physical_devices(struct VkInstance_T *instance)
417 VkPhysicalDevice *tmp_phys_devs;
418 uint32_t phys_dev_count;
419 unsigned int i;
420 VkResult res;
422 res = instance->funcs.p_vkEnumeratePhysicalDevices(instance->instance, &phys_dev_count, NULL);
423 if (res != VK_SUCCESS)
425 ERR("Failed to enumerate physical devices, res=%d\n", res);
426 return res;
428 if (!phys_dev_count)
429 return res;
431 if (!(tmp_phys_devs = heap_calloc(phys_dev_count, sizeof(*tmp_phys_devs))))
432 return VK_ERROR_OUT_OF_HOST_MEMORY;
434 res = instance->funcs.p_vkEnumeratePhysicalDevices(instance->instance, &phys_dev_count, tmp_phys_devs);
435 if (res != VK_SUCCESS)
437 heap_free(tmp_phys_devs);
438 return res;
441 instance->phys_devs = heap_calloc(phys_dev_count, sizeof(*instance->phys_devs));
442 if (!instance->phys_devs)
444 heap_free(tmp_phys_devs);
445 return VK_ERROR_OUT_OF_HOST_MEMORY;
448 /* Wrap each native physical device handle into a dispatchable object for the ICD loader. */
449 for (i = 0; i < phys_dev_count; i++)
451 struct VkPhysicalDevice_T *phys_dev = wine_vk_physical_device_alloc(instance, tmp_phys_devs[i]);
452 if (!phys_dev)
454 ERR("Unable to allocate memory for physical device!\n");
455 heap_free(tmp_phys_devs);
456 return VK_ERROR_OUT_OF_HOST_MEMORY;
459 instance->phys_devs[i] = phys_dev;
460 instance->phys_dev_count = i + 1;
462 instance->phys_dev_count = phys_dev_count;
464 heap_free(tmp_phys_devs);
465 return VK_SUCCESS;
468 static struct VkPhysicalDevice_T *wine_vk_instance_wrap_physical_device(struct VkInstance_T *instance,
469 VkPhysicalDevice physical_device)
471 unsigned int i;
473 for (i = 0; i < instance->phys_dev_count; ++i)
475 struct VkPhysicalDevice_T *current = instance->phys_devs[i];
476 if (current->phys_dev == physical_device)
477 return current;
480 ERR("Unrecognized physical device %p.\n", physical_device);
481 return NULL;
484 /* Helper function used for freeing an instance structure. This function supports full
485 * and partial object cleanups and can thus be used for vkCreateInstance failures.
487 static void wine_vk_instance_free(struct VkInstance_T *instance)
489 if (!instance)
490 return;
492 if (instance->phys_devs)
494 unsigned int i;
496 for (i = 0; i < instance->phys_dev_count; i++)
498 wine_vk_physical_device_free(instance->phys_devs[i]);
500 heap_free(instance->phys_devs);
503 if (instance->instance)
504 vk_funcs->p_vkDestroyInstance(instance->instance, NULL /* allocator */);
506 heap_free(instance);
509 VkResult WINAPI wine_vkAllocateCommandBuffers(VkDevice device,
510 const VkCommandBufferAllocateInfo *allocate_info, VkCommandBuffer *buffers)
512 struct wine_cmd_pool *pool;
513 VkResult res = VK_SUCCESS;
514 unsigned int i;
516 TRACE("%p, %p, %p\n", device, allocate_info, buffers);
518 pool = wine_cmd_pool_from_handle(allocate_info->commandPool);
520 memset(buffers, 0, allocate_info->commandBufferCount * sizeof(*buffers));
522 for (i = 0; i < allocate_info->commandBufferCount; i++)
524 #if defined(USE_STRUCT_CONVERSION)
525 VkCommandBufferAllocateInfo_host allocate_info_host;
526 #else
527 VkCommandBufferAllocateInfo allocate_info_host;
528 #endif
529 /* TODO: future extensions (none yet) may require pNext conversion. */
530 allocate_info_host.pNext = allocate_info->pNext;
531 allocate_info_host.sType = allocate_info->sType;
532 allocate_info_host.commandPool = pool->command_pool;
533 allocate_info_host.level = allocate_info->level;
534 allocate_info_host.commandBufferCount = 1;
536 TRACE("Allocating command buffer %u from pool 0x%s.\n",
537 i, wine_dbgstr_longlong(allocate_info_host.commandPool));
539 if (!(buffers[i] = heap_alloc_zero(sizeof(**buffers))))
541 res = VK_ERROR_OUT_OF_HOST_MEMORY;
542 break;
545 buffers[i]->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
546 buffers[i]->device = device;
547 list_add_tail(&pool->command_buffers, &buffers[i]->pool_link);
548 res = device->funcs.p_vkAllocateCommandBuffers(device->device,
549 &allocate_info_host, &buffers[i]->command_buffer);
550 if (res != VK_SUCCESS)
552 ERR("Failed to allocate command buffer, res=%d.\n", res);
553 buffers[i]->command_buffer = VK_NULL_HANDLE;
554 break;
558 if (res != VK_SUCCESS)
560 wine_vk_free_command_buffers(device, pool, i + 1, buffers);
561 memset(buffers, 0, allocate_info->commandBufferCount * sizeof(*buffers));
564 return res;
567 void WINAPI wine_vkCmdExecuteCommands(VkCommandBuffer buffer, uint32_t count,
568 const VkCommandBuffer *buffers)
570 VkCommandBuffer *tmp_buffers;
571 unsigned int i;
573 TRACE("%p %u %p\n", buffer, count, buffers);
575 if (!buffers || !count)
576 return;
578 /* Unfortunately we need a temporary buffer as our command buffers are wrapped.
579 * This call is called often and if a performance concern, we may want to use
580 * alloca as we shouldn't need much memory and it needs to be cleaned up after
581 * the call anyway.
583 if (!(tmp_buffers = heap_alloc(count * sizeof(*tmp_buffers))))
585 ERR("Failed to allocate memory for temporary command buffers\n");
586 return;
589 for (i = 0; i < count; i++)
590 tmp_buffers[i] = buffers[i]->command_buffer;
592 buffer->device->funcs.p_vkCmdExecuteCommands(buffer->command_buffer, count, tmp_buffers);
594 heap_free(tmp_buffers);
597 VkResult WINAPI wine_vkCreateDevice(VkPhysicalDevice phys_dev,
598 const VkDeviceCreateInfo *create_info,
599 const VkAllocationCallbacks *allocator, VkDevice *device)
601 VkDeviceCreateInfo create_info_host;
602 uint32_t max_queue_families;
603 struct VkDevice_T *object;
604 unsigned int i;
605 VkResult res;
607 TRACE("%p, %p, %p, %p\n", phys_dev, create_info, allocator, device);
609 if (allocator)
610 FIXME("Support for allocation callbacks not implemented yet\n");
612 if (TRACE_ON(vulkan))
614 VkPhysicalDeviceProperties properties;
616 wine_vkGetPhysicalDeviceProperties(phys_dev, &properties);
618 TRACE("Device name: %s.\n", debugstr_a(properties.deviceName));
619 TRACE("Vendor ID: %#x, Device ID: %#x.\n", properties.vendorID, properties.deviceID);
620 TRACE("Driver version: %#x.\n", properties.driverVersion);
623 if (!(object = heap_alloc_zero(sizeof(*object))))
624 return VK_ERROR_OUT_OF_HOST_MEMORY;
626 object->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
628 res = wine_vk_device_convert_create_info(create_info, &create_info_host);
629 if (res != VK_SUCCESS)
631 if (res != VK_ERROR_EXTENSION_NOT_PRESENT)
632 ERR("Failed to convert VkDeviceCreateInfo, res=%d.\n", res);
633 wine_vk_device_free(object);
634 return res;
637 res = phys_dev->instance->funcs.p_vkCreateDevice(phys_dev->phys_dev,
638 &create_info_host, NULL /* allocator */, &object->device);
639 wine_vk_device_free_create_info(&create_info_host);
640 if (res != VK_SUCCESS)
642 ERR("Failed to create device.\n");
643 wine_vk_device_free(object);
644 return res;
647 /* Just load all function pointers we are aware off. The loader takes care of filtering.
648 * We use vkGetDeviceProcAddr as opposed to vkGetInstanceProcAddr for efficiency reasons
649 * as functions pass through fewer dispatch tables within the loader.
651 #define USE_VK_FUNC(name) \
652 object->funcs.p_##name = (void *)vk_funcs->p_vkGetDeviceProcAddr(object->device, #name); \
653 if (object->funcs.p_##name == NULL) \
654 TRACE("Not found %s\n", #name);
655 ALL_VK_DEVICE_FUNCS()
656 #undef USE_VK_FUNC
658 /* We need to cache all queues within the device as each requires wrapping since queues are
659 * dispatchable objects.
661 phys_dev->instance->funcs.p_vkGetPhysicalDeviceQueueFamilyProperties(phys_dev->phys_dev,
662 &max_queue_families, NULL);
663 object->max_queue_families = max_queue_families;
664 TRACE("Max queue families: %u\n", object->max_queue_families);
666 object->queues = heap_calloc(max_queue_families, sizeof(*object->queues));
667 if (!object->queues)
669 wine_vk_device_free(object);
670 return VK_ERROR_OUT_OF_HOST_MEMORY;
673 for (i = 0; i < create_info_host.queueCreateInfoCount; i++)
675 uint32_t flags = create_info_host.pQueueCreateInfos[i].flags;
676 uint32_t family_index = create_info_host.pQueueCreateInfos[i].queueFamilyIndex;
677 uint32_t queue_count = create_info_host.pQueueCreateInfos[i].queueCount;
679 TRACE("queueFamilyIndex %u, queueCount %u\n", family_index, queue_count);
681 object->queues[family_index] = wine_vk_device_alloc_queues(object, family_index,
682 queue_count, flags);
683 if (!object->queues[family_index])
685 ERR("Failed to allocate memory for queues\n");
686 wine_vk_device_free(object);
687 return VK_ERROR_OUT_OF_HOST_MEMORY;
691 object->quirks = phys_dev->instance->quirks;
693 *device = object;
694 TRACE("Created device %p (native device %p).\n", object, object->device);
695 return VK_SUCCESS;
698 VkResult WINAPI wine_vkCreateInstance(const VkInstanceCreateInfo *create_info,
699 const VkAllocationCallbacks *allocator, VkInstance *instance)
701 VkInstanceCreateInfo create_info_host;
702 const VkApplicationInfo *app_info;
703 struct VkInstance_T *object;
704 VkResult res;
706 TRACE("create_info %p, allocator %p, instance %p\n", create_info, allocator, instance);
708 if (allocator)
709 FIXME("Support for allocation callbacks not implemented yet\n");
711 if (!(object = heap_alloc_zero(sizeof(*object))))
713 ERR("Failed to allocate memory for instance\n");
714 return VK_ERROR_OUT_OF_HOST_MEMORY;
716 object->base.loader_magic = VULKAN_ICD_MAGIC_VALUE;
718 res = wine_vk_instance_convert_create_info(create_info, &create_info_host);
719 if (res != VK_SUCCESS)
721 wine_vk_instance_free(object);
722 return res;
725 res = vk_funcs->p_vkCreateInstance(&create_info_host, NULL /* allocator */, &object->instance);
726 if (res != VK_SUCCESS)
728 ERR("Failed to create instance, res=%d\n", res);
729 wine_vk_instance_free(object);
730 return res;
733 /* Load all instance functions we are aware of. Note the loader takes care
734 * of any filtering for extensions which were not requested, but which the
735 * ICD may support.
737 #define USE_VK_FUNC(name) \
738 object->funcs.p_##name = (void *)vk_funcs->p_vkGetInstanceProcAddr(object->instance, #name);
739 ALL_VK_INSTANCE_FUNCS()
740 #undef USE_VK_FUNC
742 /* Cache physical devices for vkEnumeratePhysicalDevices within the instance as
743 * each vkPhysicalDevice is a dispatchable object, which means we need to wrap
744 * the native physical devices and present those to the application.
745 * Cleanup happens as part of wine_vkDestroyInstance.
747 res = wine_vk_instance_load_physical_devices(object);
748 if (res != VK_SUCCESS)
750 ERR("Failed to load physical devices, res=%d\n", res);
751 wine_vk_instance_free(object);
752 return res;
755 if ((app_info = create_info->pApplicationInfo))
757 TRACE("Application name %s, application version %#x.\n",
758 debugstr_a(app_info->pApplicationName), app_info->applicationVersion);
759 TRACE("Engine name %s, engine version %#x.\n", debugstr_a(app_info->pEngineName),
760 app_info->engineVersion);
761 TRACE("API version %#x.\n", app_info->apiVersion);
763 if (app_info->pEngineName && !strcmp(app_info->pEngineName, "idTech"))
764 object->quirks |= WINEVULKAN_QUIRK_GET_DEVICE_PROC_ADDR;
767 *instance = object;
768 TRACE("Created instance %p (native instance %p).\n", object, object->instance);
769 return VK_SUCCESS;
772 void WINAPI wine_vkDestroyDevice(VkDevice device, const VkAllocationCallbacks *allocator)
774 TRACE("%p %p\n", device, allocator);
776 if (allocator)
777 FIXME("Support for allocation callbacks not implemented yet\n");
779 wine_vk_device_free(device);
782 void WINAPI wine_vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks *allocator)
784 TRACE("%p, %p\n", instance, allocator);
786 if (allocator)
787 FIXME("Support allocation allocators\n");
789 wine_vk_instance_free(instance);
792 VkResult WINAPI wine_vkEnumerateDeviceExtensionProperties(VkPhysicalDevice phys_dev,
793 const char *layer_name, uint32_t *count, VkExtensionProperties *properties)
795 TRACE("%p, %p, %p, %p\n", phys_dev, layer_name, count, properties);
797 /* This shouldn't get called with layer_name set, the ICD loader prevents it. */
798 if (layer_name)
800 ERR("Layer enumeration not supported from ICD.\n");
801 return VK_ERROR_LAYER_NOT_PRESENT;
804 if (!properties)
806 *count = phys_dev->extension_count;
807 return VK_SUCCESS;
810 *count = min(*count, phys_dev->extension_count);
811 memcpy(properties, phys_dev->extensions, *count * sizeof(*properties));
813 TRACE("Returning %u extensions.\n", *count);
814 return *count < phys_dev->extension_count ? VK_INCOMPLETE : VK_SUCCESS;
817 VkResult WINAPI wine_vkEnumerateInstanceExtensionProperties(const char *layer_name,
818 uint32_t *count, VkExtensionProperties *properties)
820 uint32_t num_properties = 0, num_host_properties;
821 VkExtensionProperties *host_properties;
822 unsigned int i, j;
823 VkResult res;
825 TRACE("%p, %p, %p\n", layer_name, count, properties);
827 if (layer_name)
829 WARN("Layer enumeration not supported from ICD.\n");
830 return VK_ERROR_LAYER_NOT_PRESENT;
833 res = vk_funcs->p_vkEnumerateInstanceExtensionProperties(NULL, &num_host_properties, NULL);
834 if (res != VK_SUCCESS)
835 return res;
837 if (!(host_properties = heap_calloc(num_host_properties, sizeof(*host_properties))))
838 return VK_ERROR_OUT_OF_HOST_MEMORY;
840 res = vk_funcs->p_vkEnumerateInstanceExtensionProperties(NULL, &num_host_properties, host_properties);
841 if (res != VK_SUCCESS)
843 ERR("Failed to retrieve host properties, res=%d.\n", res);
844 heap_free(host_properties);
845 return res;
848 /* The Wine graphics driver provides us with all extensions supported by the host side
849 * including extension fixup (e.g. VK_KHR_xlib_surface -> VK_KHR_win32_surface). It is
850 * up to us here to filter the list down to extensions for which we have thunks.
852 for (i = 0; i < num_host_properties; i++)
854 if (wine_vk_instance_extension_supported(host_properties[i].extensionName))
855 num_properties++;
856 else
857 TRACE("Instance extension '%s' is not supported.\n", host_properties[i].extensionName);
860 if (!properties)
862 TRACE("Returning %u extensions.\n", num_properties);
863 *count = num_properties;
864 heap_free(host_properties);
865 return VK_SUCCESS;
868 for (i = 0, j = 0; i < num_host_properties && j < *count; i++)
870 if (wine_vk_instance_extension_supported(host_properties[i].extensionName))
872 TRACE("Enabling extension '%s'.\n", host_properties[i].extensionName);
873 properties[j++] = host_properties[i];
876 *count = min(*count, num_properties);
878 heap_free(host_properties);
879 return *count < num_properties ? VK_INCOMPLETE : VK_SUCCESS;
882 VkResult WINAPI wine_vkEnumerateInstanceLayerProperties(uint32_t *count, VkLayerProperties *properties)
884 TRACE("%p, %p\n", count, properties);
886 if (!properties)
888 *count = 0;
889 return VK_SUCCESS;
892 return VK_ERROR_LAYER_NOT_PRESENT;
895 VkResult WINAPI wine_vkEnumerateInstanceVersion(uint32_t *version)
897 VkResult res;
899 TRACE("%p\n", version);
901 if (p_vkEnumerateInstanceVersion)
903 res = p_vkEnumerateInstanceVersion(version);
905 else
907 *version = VK_API_VERSION_1_0;
908 res = VK_SUCCESS;
911 TRACE("API version %u.%u.%u.\n",
912 VK_VERSION_MAJOR(*version), VK_VERSION_MINOR(*version), VK_VERSION_PATCH(*version));
913 *version = min(WINE_VK_VERSION, *version);
914 return res;
917 VkResult WINAPI wine_vkEnumeratePhysicalDevices(VkInstance instance, uint32_t *count,
918 VkPhysicalDevice *devices)
920 unsigned int i;
922 TRACE("%p %p %p\n", instance, count, devices);
924 if (!devices)
926 *count = instance->phys_dev_count;
927 return VK_SUCCESS;
930 *count = min(*count, instance->phys_dev_count);
931 for (i = 0; i < *count; i++)
933 devices[i] = instance->phys_devs[i];
936 TRACE("Returning %u devices.\n", *count);
937 return *count < instance->phys_dev_count ? VK_INCOMPLETE : VK_SUCCESS;
940 void WINAPI wine_vkFreeCommandBuffers(VkDevice device, VkCommandPool pool_handle,
941 uint32_t count, const VkCommandBuffer *buffers)
943 struct wine_cmd_pool *pool = wine_cmd_pool_from_handle(pool_handle);
945 TRACE("%p, 0x%s, %u, %p\n", device, wine_dbgstr_longlong(pool_handle), count, buffers);
947 wine_vk_free_command_buffers(device, pool, count, buffers);
950 PFN_vkVoidFunction WINAPI wine_vkGetDeviceProcAddr(VkDevice device, const char *name)
952 void *func;
953 TRACE("%p, %s\n", device, debugstr_a(name));
955 /* The spec leaves return value undefined for a NULL device, let's just return NULL. */
956 if (!device || !name)
957 return NULL;
959 /* Per the spec, we are only supposed to return device functions as in functions
960 * for which the first parameter is vkDevice or a child of vkDevice like a
961 * vkCommandBuffer or vkQueue.
962 * Loader takes care of filtering of extensions which are enabled or not.
964 func = wine_vk_get_device_proc_addr(name);
965 if (func)
966 return func;
968 /* vkGetDeviceProcAddr was intended for loading device and subdevice functions.
969 * idTech 6 titles such as Doom and Wolfenstein II, however use it also for
970 * loading of instance functions. This is undefined behavior as the specification
971 * disallows using any of the returned function pointers outside of device /
972 * subdevice objects. The games don't actually use the function pointers and if they
973 * did, they would crash as VkInstance / VkPhysicalDevice parameters need unwrapping.
974 * Khronos clarified behavior in the Vulkan spec and expects drivers to get updated,
975 * however it would require both driver and game fixes.
976 * https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/issues/2323
977 * https://github.com/KhronosGroup/Vulkan-Docs/issues/655
979 if (device->quirks & WINEVULKAN_QUIRK_GET_DEVICE_PROC_ADDR
980 && (func = wine_vk_get_instance_proc_addr(name)))
982 WARN("Returning instance function %s.\n", debugstr_a(name));
983 return func;
986 WARN("Unsupported device function: %s.\n", debugstr_a(name));
987 return NULL;
990 void WINAPI wine_vkGetDeviceQueue(VkDevice device, uint32_t family_index,
991 uint32_t queue_index, VkQueue *queue)
993 TRACE("%p, %u, %u, %p\n", device, family_index, queue_index, queue);
995 *queue = &device->queues[family_index][queue_index];
998 void WINAPI wine_vkGetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2 *info, VkQueue *queue)
1000 const struct wine_vk_structure_header *chain;
1001 struct VkQueue_T *matching_queue;
1003 TRACE("%p, %p, %p\n", device, info, queue);
1005 if ((chain = info->pNext))
1006 FIXME("Ignoring a linked structure of type %#x.\n", chain->sType);
1008 matching_queue = &device->queues[info->queueFamilyIndex][info->queueIndex];
1009 if (matching_queue->flags != info->flags)
1011 WARN("No matching flags were specified %#x, %#x.\n", matching_queue->flags, info->flags);
1012 matching_queue = VK_NULL_HANDLE;
1014 *queue = matching_queue;
1017 PFN_vkVoidFunction WINAPI wine_vkGetInstanceProcAddr(VkInstance instance, const char *name)
1019 void *func;
1021 TRACE("%p, %s\n", instance, debugstr_a(name));
1023 if (!name)
1024 return NULL;
1026 /* vkGetInstanceProcAddr can load most Vulkan functions when an instance is passed in, however
1027 * for a NULL instance it can only load global functions.
1029 func = wine_vk_get_global_proc_addr(name);
1030 if (func)
1032 return func;
1034 if (!instance)
1036 WARN("Global function %s not found.\n", debugstr_a(name));
1037 return NULL;
1040 func = wine_vk_get_instance_proc_addr(name);
1041 if (func) return func;
1043 /* vkGetInstanceProcAddr also loads any children of instance, so device functions as well. */
1044 func = wine_vk_get_device_proc_addr(name);
1045 if (func) return func;
1047 WARN("Unsupported device or instance function: %s.\n", debugstr_a(name));
1048 return NULL;
1051 void * WINAPI wine_vk_icdGetInstanceProcAddr(VkInstance instance, const char *name)
1053 TRACE("%p, %s\n", instance, debugstr_a(name));
1055 /* Initial version of the Vulkan ICD spec required vkGetInstanceProcAddr to be
1056 * exported. vk_icdGetInstanceProcAddr was added later to separate ICD calls from
1057 * Vulkan API. One of them in our case should forward to the other, so just forward
1058 * to the older vkGetInstanceProcAddr.
1060 return wine_vkGetInstanceProcAddr(instance, name);
1063 VkResult WINAPI wine_vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t *supported_version)
1065 uint32_t req_version;
1067 TRACE("%p\n", supported_version);
1069 /* The spec is not clear how to handle this. Mesa drivers don't check, but it
1070 * is probably best to not explode. VK_INCOMPLETE seems to be the closest value.
1072 if (!supported_version)
1073 return VK_INCOMPLETE;
1075 req_version = *supported_version;
1076 *supported_version = min(req_version, WINE_VULKAN_ICD_VERSION);
1077 TRACE("Loader requested ICD version %u, returning %u\n", req_version, *supported_version);
1079 return VK_SUCCESS;
1082 VkResult WINAPI wine_vkQueueSubmit(VkQueue queue, uint32_t count,
1083 const VkSubmitInfo *submits, VkFence fence)
1085 VkSubmitInfo *submits_host;
1086 VkResult res;
1087 VkCommandBuffer *command_buffers;
1088 unsigned int i, j, num_command_buffers;
1090 TRACE("%p %u %p 0x%s\n", queue, count, submits, wine_dbgstr_longlong(fence));
1092 if (count == 0)
1094 return queue->device->funcs.p_vkQueueSubmit(queue->queue, 0, NULL, fence);
1097 submits_host = heap_calloc(count, sizeof(*submits_host));
1098 if (!submits_host)
1100 ERR("Unable to allocate memory for submit buffers!\n");
1101 return VK_ERROR_OUT_OF_HOST_MEMORY;
1104 for (i = 0; i < count; i++)
1106 memcpy(&submits_host[i], &submits[i], sizeof(*submits_host));
1108 num_command_buffers = submits[i].commandBufferCount;
1109 command_buffers = heap_calloc(num_command_buffers, sizeof(*submits_host));
1110 if (!command_buffers)
1112 ERR("Unable to allocate memory for comman buffers!\n");
1113 res = VK_ERROR_OUT_OF_HOST_MEMORY;
1114 goto err;
1117 for (j = 0; j < num_command_buffers; j++)
1119 command_buffers[j] = submits[i].pCommandBuffers[j]->command_buffer;
1121 submits_host[i].pCommandBuffers = command_buffers;
1124 res = queue->device->funcs.p_vkQueueSubmit(queue->queue, count, submits_host, fence);
1126 err:
1127 for (i = 0; i < count; i++)
1129 heap_free((void *)submits_host[i].pCommandBuffers);
1131 heap_free(submits_host);
1133 TRACE("Returning %d\n", res);
1134 return res;
1137 VkResult WINAPI wine_vkCreateCommandPool(VkDevice device, const VkCommandPoolCreateInfo *info,
1138 const VkAllocationCallbacks *allocator, VkCommandPool *command_pool)
1140 struct wine_cmd_pool *object;
1141 VkResult res;
1143 TRACE("%p, %p, %p, %p\n", device, info, allocator, command_pool);
1145 if (allocator)
1146 FIXME("Support for allocation callbacks not implemented yet\n");
1148 if (!(object = heap_alloc_zero(sizeof(*object))))
1149 return VK_ERROR_OUT_OF_HOST_MEMORY;
1151 list_init(&object->command_buffers);
1153 res = device->funcs.p_vkCreateCommandPool(device->device, info, NULL, &object->command_pool);
1155 if (res == VK_SUCCESS)
1156 *command_pool = wine_cmd_pool_to_handle(object);
1157 else
1158 heap_free(object);
1160 return res;
1163 void WINAPI wine_vkDestroyCommandPool(VkDevice device, VkCommandPool handle,
1164 const VkAllocationCallbacks *allocator)
1166 struct wine_cmd_pool *pool = wine_cmd_pool_from_handle(handle);
1167 struct VkCommandBuffer_T *buffer, *cursor;
1169 TRACE("%p, 0x%s, %p\n", device, wine_dbgstr_longlong(handle), allocator);
1171 if (!handle)
1172 return;
1174 if (allocator)
1175 FIXME("Support for allocation callbacks not implemented yet\n");
1177 /* The Vulkan spec says:
1179 * "When a pool is destroyed, all command buffers allocated from the pool are freed."
1181 LIST_FOR_EACH_ENTRY_SAFE(buffer, cursor, &pool->command_buffers, struct VkCommandBuffer_T, pool_link)
1183 heap_free(buffer);
1186 device->funcs.p_vkDestroyCommandPool(device->device, pool->command_pool, NULL);
1187 heap_free(pool);
1190 static VkResult wine_vk_enumerate_physical_device_groups(struct VkInstance_T *instance,
1191 VkResult (*p_vkEnumeratePhysicalDeviceGroups)(VkInstance, uint32_t *, VkPhysicalDeviceGroupProperties *),
1192 uint32_t *count, VkPhysicalDeviceGroupProperties *properties)
1194 unsigned int i, j;
1195 VkResult res;
1197 res = p_vkEnumeratePhysicalDeviceGroups(instance->instance, count, properties);
1198 if (res < 0 || !properties)
1199 return res;
1201 for (i = 0; i < *count; ++i)
1203 VkPhysicalDeviceGroupProperties *current = &properties[i];
1204 for (j = 0; j < current->physicalDeviceCount; ++j)
1206 VkPhysicalDevice dev = current->physicalDevices[j];
1207 if (!(current->physicalDevices[j] = wine_vk_instance_wrap_physical_device(instance, dev)))
1208 return VK_ERROR_INITIALIZATION_FAILED;
1212 return res;
1215 VkResult WINAPI wine_vkEnumeratePhysicalDeviceGroups(VkInstance instance,
1216 uint32_t *count, VkPhysicalDeviceGroupProperties *properties)
1218 TRACE("%p, %p, %p\n", instance, count, properties);
1219 return wine_vk_enumerate_physical_device_groups(instance,
1220 instance->funcs.p_vkEnumeratePhysicalDeviceGroups, count, properties);
1223 VkResult WINAPI wine_vkEnumeratePhysicalDeviceGroupsKHR(VkInstance instance,
1224 uint32_t *count, VkPhysicalDeviceGroupProperties *properties)
1226 TRACE("%p, %p, %p\n", instance, count, properties);
1227 return wine_vk_enumerate_physical_device_groups(instance,
1228 instance->funcs.p_vkEnumeratePhysicalDeviceGroupsKHR, count, properties);
1231 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, void *reserved)
1233 TRACE("%p, %u, %p\n", hinst, reason, reserved);
1235 switch (reason)
1237 case DLL_PROCESS_ATTACH:
1238 DisableThreadLibraryCalls(hinst);
1239 return wine_vk_init();
1241 return TRUE;
1244 static const struct vulkan_func vk_global_dispatch_table[] =
1246 {"vkCreateInstance", &wine_vkCreateInstance},
1247 {"vkEnumerateInstanceExtensionProperties", &wine_vkEnumerateInstanceExtensionProperties},
1248 {"vkEnumerateInstanceLayerProperties", &wine_vkEnumerateInstanceLayerProperties},
1249 {"vkEnumerateInstanceVersion", &wine_vkEnumerateInstanceVersion},
1250 {"vkGetInstanceProcAddr", &wine_vkGetInstanceProcAddr},
1253 static void *wine_vk_get_global_proc_addr(const char *name)
1255 unsigned int i;
1257 for (i = 0; i < ARRAY_SIZE(vk_global_dispatch_table); i++)
1259 if (strcmp(name, vk_global_dispatch_table[i].name) == 0)
1261 TRACE("Found name=%s in global table\n", debugstr_a(name));
1262 return vk_global_dispatch_table[i].func;
1265 return NULL;
1269 * Wrapper around driver vkGetInstanceProcAddr implementation.
1270 * Allows winelib applications to access Vulkan functions with Wine
1271 * additions and native ABI.
1273 void *native_vkGetInstanceProcAddrWINE(VkInstance instance, const char *name)
1275 return vk_funcs->p_vkGetInstanceProcAddr(instance, name);