gdiplus: Add GdipGetPenCompoundCount implementation.
[wine.git] / dlls / winevulkan / vulkan.c
blob0edc1ae492d7a98a3ead7a77481cde7d567f5446
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 #if 0
21 #pragma makedep unix
22 #endif
24 #include "config.h"
25 #include <time.h>
27 #include "vulkan_private.h"
28 #include "wine/vulkan_driver.h"
29 #include "ntuser.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(vulkan);
33 #define wine_vk_find_struct(s, t) wine_vk_find_struct_((void *)s, VK_STRUCTURE_TYPE_##t)
34 static void *wine_vk_find_struct_(void *s, VkStructureType t)
36 VkBaseOutStructure *header;
38 for (header = s; header; header = header->pNext)
40 if (header->sType == t)
41 return header;
44 return NULL;
47 #define wine_vk_count_struct(s, t) wine_vk_count_struct_((void *)s, VK_STRUCTURE_TYPE_##t)
48 static uint32_t wine_vk_count_struct_(void *s, VkStructureType t)
50 const VkBaseInStructure *header;
51 uint32_t result = 0;
53 for (header = s; header; header = header->pNext)
55 if (header->sType == t)
56 result++;
59 return result;
62 static const struct vulkan_funcs *vk_funcs;
64 #define WINE_VK_ADD_DISPATCHABLE_MAPPING(instance, client_handle, native_handle, object) \
65 wine_vk_add_handle_mapping((instance), (uintptr_t)(client_handle), (uintptr_t)(native_handle), &(object)->mapping)
66 #define WINE_VK_ADD_NON_DISPATCHABLE_MAPPING(instance, client_handle, native_handle, object) \
67 wine_vk_add_handle_mapping((instance), (uintptr_t)(client_handle), (native_handle), &(object)->mapping)
68 static void wine_vk_add_handle_mapping(struct wine_instance *instance, uint64_t wrapped_handle,
69 uint64_t native_handle, struct wine_vk_mapping *mapping)
71 if (instance->enable_wrapper_list)
73 mapping->native_handle = native_handle;
74 mapping->wine_wrapped_handle = wrapped_handle;
75 pthread_rwlock_wrlock(&instance->wrapper_lock);
76 list_add_tail(&instance->wrappers, &mapping->link);
77 pthread_rwlock_unlock(&instance->wrapper_lock);
81 #define WINE_VK_REMOVE_HANDLE_MAPPING(instance, object) \
82 wine_vk_remove_handle_mapping((instance), &(object)->mapping)
83 static void wine_vk_remove_handle_mapping(struct wine_instance *instance, struct wine_vk_mapping *mapping)
85 if (instance->enable_wrapper_list)
87 pthread_rwlock_wrlock(&instance->wrapper_lock);
88 list_remove(&mapping->link);
89 pthread_rwlock_unlock(&instance->wrapper_lock);
93 static uint64_t wine_vk_get_wrapper(struct wine_instance *instance, uint64_t native_handle)
95 struct wine_vk_mapping *mapping;
96 uint64_t result = 0;
98 pthread_rwlock_rdlock(&instance->wrapper_lock);
99 LIST_FOR_EACH_ENTRY(mapping, &instance->wrappers, struct wine_vk_mapping, link)
101 if (mapping->native_handle == native_handle)
103 result = mapping->wine_wrapped_handle;
104 break;
107 pthread_rwlock_unlock(&instance->wrapper_lock);
108 return result;
111 static VkBool32 debug_utils_callback_conversion(VkDebugUtilsMessageSeverityFlagBitsEXT severity,
112 VkDebugUtilsMessageTypeFlagsEXT message_types,
113 const VkDebugUtilsMessengerCallbackDataEXT_host *callback_data,
114 void *user_data)
116 struct wine_vk_debug_utils_params params;
117 VkDebugUtilsObjectNameInfoEXT *object_name_infos;
118 struct wine_debug_utils_messenger *object;
119 void *ret_ptr;
120 ULONG ret_len;
121 VkBool32 result;
122 unsigned int i;
124 TRACE("%i, %u, %p, %p\n", severity, message_types, callback_data, user_data);
126 object = user_data;
128 if (!object->instance->instance)
130 /* instance wasn't yet created, this is a message from the native loader */
131 return VK_FALSE;
134 /* FIXME: we should pack all referenced structs instead of passing pointers */
135 params.user_callback = object->user_callback;
136 params.user_data = object->user_data;
137 params.severity = severity;
138 params.message_types = message_types;
139 params.data = *((VkDebugUtilsMessengerCallbackDataEXT *) callback_data);
141 object_name_infos = calloc(params.data.objectCount, sizeof(*object_name_infos));
143 for (i = 0; i < params.data.objectCount; i++)
145 object_name_infos[i].sType = callback_data->pObjects[i].sType;
146 object_name_infos[i].pNext = callback_data->pObjects[i].pNext;
147 object_name_infos[i].objectType = callback_data->pObjects[i].objectType;
148 object_name_infos[i].pObjectName = callback_data->pObjects[i].pObjectName;
150 if (wine_vk_is_type_wrapped(callback_data->pObjects[i].objectType))
152 object_name_infos[i].objectHandle = wine_vk_get_wrapper(object->instance, callback_data->pObjects[i].objectHandle);
153 if (!object_name_infos[i].objectHandle)
155 WARN("handle conversion failed 0x%s\n", wine_dbgstr_longlong(callback_data->pObjects[i].objectHandle));
156 free(object_name_infos);
157 return VK_FALSE;
160 else
162 object_name_infos[i].objectHandle = callback_data->pObjects[i].objectHandle;
166 params.data.pObjects = object_name_infos;
168 /* applications should always return VK_FALSE */
169 result = KeUserModeCallback( NtUserCallVulkanDebugUtilsCallback, &params, sizeof(params),
170 &ret_ptr, &ret_len );
172 free(object_name_infos);
174 return result;
177 static VkBool32 debug_report_callback_conversion(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT object_type,
178 uint64_t object_handle, size_t location, int32_t code, const char *layer_prefix, const char *message, void *user_data)
180 struct wine_vk_debug_report_params params;
181 struct wine_debug_report_callback *object;
182 void *ret_ptr;
183 ULONG ret_len;
185 TRACE("%#x, %#x, 0x%s, 0x%s, %d, %p, %p, %p\n", flags, object_type, wine_dbgstr_longlong(object_handle),
186 wine_dbgstr_longlong(location), code, layer_prefix, message, user_data);
188 object = user_data;
190 if (!object->instance->instance)
192 /* instance wasn't yet created, this is a message from the native loader */
193 return VK_FALSE;
196 /* FIXME: we should pack all referenced structs instead of passing pointers */
197 params.user_callback = object->user_callback;
198 params.user_data = object->user_data;
199 params.flags = flags;
200 params.object_type = object_type;
201 params.location = location;
202 params.code = code;
203 params.layer_prefix = layer_prefix;
204 params.message = message;
206 params.object_handle = wine_vk_get_wrapper(object->instance, object_handle);
207 if (!params.object_handle)
208 params.object_type = VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT;
210 return KeUserModeCallback( NtUserCallVulkanDebugReportCallback, &params, sizeof(params),
211 &ret_ptr, &ret_len );
214 static void wine_vk_physical_device_free(struct wine_phys_dev *phys_dev)
216 if (!phys_dev)
217 return;
219 WINE_VK_REMOVE_HANDLE_MAPPING(phys_dev->instance, phys_dev);
220 free(phys_dev->extensions);
221 free(phys_dev);
224 static struct wine_phys_dev *wine_vk_physical_device_alloc(struct wine_instance *instance,
225 VkPhysicalDevice phys_dev, VkPhysicalDevice handle)
227 struct wine_phys_dev *object;
228 uint32_t num_host_properties, num_properties = 0;
229 VkExtensionProperties *host_properties = NULL;
230 VkResult res;
231 unsigned int i, j;
233 if (!(object = calloc(1, sizeof(*object))))
234 return NULL;
236 object->instance = instance;
237 object->handle = handle;
238 object->phys_dev = phys_dev;
240 handle->base.unix_handle = (uintptr_t)object;
241 WINE_VK_ADD_DISPATCHABLE_MAPPING(instance, handle, phys_dev, object);
243 res = instance->funcs.p_vkEnumerateDeviceExtensionProperties(phys_dev,
244 NULL, &num_host_properties, NULL);
245 if (res != VK_SUCCESS)
247 ERR("Failed to enumerate device extensions, res=%d\n", res);
248 goto err;
251 host_properties = calloc(num_host_properties, sizeof(*host_properties));
252 if (!host_properties)
254 ERR("Failed to allocate memory for device properties!\n");
255 goto err;
258 res = instance->funcs.p_vkEnumerateDeviceExtensionProperties(phys_dev,
259 NULL, &num_host_properties, host_properties);
260 if (res != VK_SUCCESS)
262 ERR("Failed to enumerate device extensions, res=%d\n", res);
263 goto err;
266 /* Count list of extensions for which we have an implementation.
267 * TODO: perform translation for platform specific extensions.
269 for (i = 0; i < num_host_properties; i++)
271 if (wine_vk_device_extension_supported(host_properties[i].extensionName))
273 TRACE("Enabling extension '%s' for physical device %p\n", host_properties[i].extensionName, object);
274 num_properties++;
276 else
278 TRACE("Skipping extension '%s', no implementation found in winevulkan.\n", host_properties[i].extensionName);
282 TRACE("Host supported extensions %u, Wine supported extensions %u\n", num_host_properties, num_properties);
284 if (!(object->extensions = calloc(num_properties, sizeof(*object->extensions))))
286 ERR("Failed to allocate memory for device extensions!\n");
287 goto err;
290 for (i = 0, j = 0; i < num_host_properties; i++)
292 if (wine_vk_device_extension_supported(host_properties[i].extensionName))
294 object->extensions[j] = host_properties[i];
295 j++;
298 object->extension_count = num_properties;
300 free(host_properties);
301 return object;
303 err:
304 wine_vk_physical_device_free(object);
305 free(host_properties);
306 return NULL;
309 static void wine_vk_free_command_buffers(struct wine_device *device,
310 struct wine_cmd_pool *pool, uint32_t count, const VkCommandBuffer *buffers)
312 unsigned int i;
314 for (i = 0; i < count; i++)
316 struct wine_cmd_buffer *buffer = wine_cmd_buffer_from_handle(buffers[i]);
318 if (!buffer)
319 continue;
321 device->funcs.p_vkFreeCommandBuffers(device->device, pool->command_pool, 1, &buffer->command_buffer);
322 WINE_VK_REMOVE_HANDLE_MAPPING(device->phys_dev->instance, buffer);
323 buffer->handle->base.unix_handle = 0;
324 free(buffer);
328 static void wine_vk_device_get_queues(struct wine_device *device,
329 uint32_t family_index, uint32_t queue_count, VkDeviceQueueCreateFlags flags,
330 struct wine_queue *queues, VkQueue *handles)
332 VkDeviceQueueInfo2 queue_info;
333 unsigned int i;
335 for (i = 0; i < queue_count; i++)
337 struct wine_queue *queue = &queues[i];
339 queue->device = device;
340 queue->handle = (*handles)++;
341 queue->family_index = family_index;
342 queue->queue_index = i;
343 queue->flags = flags;
345 /* The Vulkan spec says:
347 * "vkGetDeviceQueue must only be used to get queues that were created
348 * with the flags parameter of VkDeviceQueueCreateInfo set to zero."
350 if (flags && device->funcs.p_vkGetDeviceQueue2)
352 queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2;
353 queue_info.pNext = NULL;
354 queue_info.flags = flags;
355 queue_info.queueFamilyIndex = family_index;
356 queue_info.queueIndex = i;
357 device->funcs.p_vkGetDeviceQueue2(device->device, &queue_info, &queue->queue);
359 else
361 device->funcs.p_vkGetDeviceQueue(device->device, family_index, i, &queue->queue);
364 queue->handle->base.unix_handle = (uintptr_t)queue;
365 WINE_VK_ADD_DISPATCHABLE_MAPPING(device->phys_dev->instance, queue->handle, queue->queue, queue);
369 static VkResult wine_vk_device_convert_create_info(struct conversion_context *ctx, const VkDeviceCreateInfo *src,
370 VkDeviceCreateInfo *dst)
372 unsigned int i;
373 VkResult res;
375 *dst = *src;
377 if ((res = convert_VkDeviceCreateInfo_struct_chain(ctx, src->pNext, dst)) < 0)
379 WARN("Failed to convert VkDeviceCreateInfo pNext chain, res=%d.\n", res);
380 return res;
383 /* Should be filtered out by loader as ICDs don't support layers. */
384 dst->enabledLayerCount = 0;
385 dst->ppEnabledLayerNames = NULL;
387 TRACE("Enabled %u extensions.\n", dst->enabledExtensionCount);
388 for (i = 0; i < dst->enabledExtensionCount; i++)
390 const char *extension_name = dst->ppEnabledExtensionNames[i];
391 TRACE("Extension %u: %s.\n", i, debugstr_a(extension_name));
392 if (!wine_vk_device_extension_supported(extension_name))
394 WARN("Extension %s is not supported.\n", debugstr_a(extension_name));
395 return VK_ERROR_EXTENSION_NOT_PRESENT;
399 return VK_SUCCESS;
402 /* Helper function used for freeing a device structure. This function supports full
403 * and partial object cleanups and can thus be used for vkCreateDevice failures.
405 static void wine_vk_device_free(struct wine_device *device)
407 struct wine_queue *queue;
409 if (!device)
410 return;
412 if (device->queues)
414 unsigned int i;
415 for (i = 0; i < device->queue_count; i++)
417 queue = &device->queues[i];
418 if (queue && queue->queue)
419 WINE_VK_REMOVE_HANDLE_MAPPING(device->phys_dev->instance, queue);
421 free(device->queues);
422 device->queues = NULL;
425 if (device->device && device->funcs.p_vkDestroyDevice)
427 WINE_VK_REMOVE_HANDLE_MAPPING(device->phys_dev->instance, device);
428 device->funcs.p_vkDestroyDevice(device->device, NULL /* pAllocator */);
431 free(device);
434 NTSTATUS init_vulkan(void *args)
436 vk_funcs = __wine_get_vulkan_driver(WINE_VULKAN_DRIVER_VERSION);
437 if (!vk_funcs)
439 ERR("Failed to load Wine graphics driver supporting Vulkan.\n");
440 return STATUS_UNSUCCESSFUL;
444 *(void **)args = vk_direct_unix_call;
445 return STATUS_SUCCESS;
448 /* Helper function for converting between win32 and host compatible VkInstanceCreateInfo.
449 * This function takes care of extensions handled at winevulkan layer, a Wine graphics
450 * driver is responsible for handling e.g. surface extensions.
452 static VkResult wine_vk_instance_convert_create_info(struct conversion_context *ctx, const VkInstanceCreateInfo *src,
453 VkInstanceCreateInfo *dst, struct wine_instance *object)
455 VkDebugUtilsMessengerCreateInfoEXT *debug_utils_messenger;
456 VkDebugReportCallbackCreateInfoEXT *debug_report_callback;
457 VkBaseInStructure *header;
458 unsigned int i;
459 VkResult res;
461 *dst = *src;
463 if ((res = convert_VkInstanceCreateInfo_struct_chain(ctx, src->pNext, dst)) < 0)
465 WARN("Failed to convert VkInstanceCreateInfo pNext chain, res=%d.\n", res);
466 return res;
469 object->utils_messenger_count = wine_vk_count_struct(dst, DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT);
470 object->utils_messengers = calloc(object->utils_messenger_count, sizeof(*object->utils_messengers));
471 header = (VkBaseInStructure *) dst;
472 for (i = 0; i < object->utils_messenger_count; i++)
474 header = wine_vk_find_struct(header->pNext, DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT);
475 debug_utils_messenger = (VkDebugUtilsMessengerCreateInfoEXT *) header;
477 object->utils_messengers[i].instance = object;
478 object->utils_messengers[i].debug_messenger = VK_NULL_HANDLE;
479 object->utils_messengers[i].user_callback = debug_utils_messenger->pfnUserCallback;
480 object->utils_messengers[i].user_data = debug_utils_messenger->pUserData;
482 /* convert_VkInstanceCreateInfo_struct_chain already copied the chain,
483 * so we can modify it in-place.
485 debug_utils_messenger->pfnUserCallback = (void *) &debug_utils_callback_conversion;
486 debug_utils_messenger->pUserData = &object->utils_messengers[i];
489 debug_report_callback = wine_vk_find_struct(header->pNext, DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT);
490 if (debug_report_callback)
492 object->default_callback.instance = object;
493 object->default_callback.debug_callback = VK_NULL_HANDLE;
494 object->default_callback.user_callback = debug_report_callback->pfnCallback;
495 object->default_callback.user_data = debug_report_callback->pUserData;
497 debug_report_callback->pfnCallback = (void *) &debug_report_callback_conversion;
498 debug_report_callback->pUserData = &object->default_callback;
501 /* ICDs don't support any layers, so nothing to copy. Modern versions of the loader
502 * filter this data out as well.
504 if (object->quirks & WINEVULKAN_QUIRK_IGNORE_EXPLICIT_LAYERS) {
505 dst->enabledLayerCount = 0;
506 dst->ppEnabledLayerNames = NULL;
507 WARN("Ignoring explicit layers!\n");
508 } else if (dst->enabledLayerCount) {
509 FIXME("Loading explicit layers is not supported by winevulkan!\n");
510 return VK_ERROR_LAYER_NOT_PRESENT;
513 TRACE("Enabled %u instance extensions.\n", dst->enabledExtensionCount);
514 for (i = 0; i < dst->enabledExtensionCount; i++)
516 const char *extension_name = dst->ppEnabledExtensionNames[i];
517 TRACE("Extension %u: %s.\n", i, debugstr_a(extension_name));
518 if (!wine_vk_instance_extension_supported(extension_name))
520 WARN("Extension %s is not supported.\n", debugstr_a(extension_name));
521 return VK_ERROR_EXTENSION_NOT_PRESENT;
523 if (!strcmp(extension_name, "VK_EXT_debug_utils") || !strcmp(extension_name, "VK_EXT_debug_report"))
525 object->enable_wrapper_list = VK_TRUE;
529 return VK_SUCCESS;
532 /* Helper function which stores wrapped physical devices in the instance object. */
533 static VkResult wine_vk_instance_load_physical_devices(struct wine_instance *instance)
535 VkPhysicalDevice *tmp_phys_devs;
536 uint32_t phys_dev_count;
537 unsigned int i;
538 VkResult res;
540 res = instance->funcs.p_vkEnumeratePhysicalDevices(instance->instance, &phys_dev_count, NULL);
541 if (res != VK_SUCCESS)
543 ERR("Failed to enumerate physical devices, res=%d\n", res);
544 return res;
546 if (!phys_dev_count)
547 return res;
549 if (phys_dev_count > instance->handle->phys_dev_count)
551 instance->handle->phys_dev_count = phys_dev_count;
552 return VK_ERROR_OUT_OF_POOL_MEMORY;
554 instance->handle->phys_dev_count = phys_dev_count;
556 if (!(tmp_phys_devs = calloc(phys_dev_count, sizeof(*tmp_phys_devs))))
557 return VK_ERROR_OUT_OF_HOST_MEMORY;
559 res = instance->funcs.p_vkEnumeratePhysicalDevices(instance->instance, &phys_dev_count, tmp_phys_devs);
560 if (res != VK_SUCCESS)
562 free(tmp_phys_devs);
563 return res;
566 instance->phys_devs = calloc(phys_dev_count, sizeof(*instance->phys_devs));
567 if (!instance->phys_devs)
569 free(tmp_phys_devs);
570 return VK_ERROR_OUT_OF_HOST_MEMORY;
573 /* Wrap each native physical device handle into a dispatchable object for the ICD loader. */
574 for (i = 0; i < phys_dev_count; i++)
576 struct wine_phys_dev *phys_dev = wine_vk_physical_device_alloc(instance, tmp_phys_devs[i],
577 &instance->handle->phys_devs[i]);
578 if (!phys_dev)
580 ERR("Unable to allocate memory for physical device!\n");
581 free(tmp_phys_devs);
582 return VK_ERROR_OUT_OF_HOST_MEMORY;
585 instance->phys_devs[i] = phys_dev;
586 instance->phys_dev_count = i + 1;
588 instance->phys_dev_count = phys_dev_count;
590 free(tmp_phys_devs);
591 return VK_SUCCESS;
594 static struct wine_phys_dev *wine_vk_instance_wrap_physical_device(struct wine_instance *instance,
595 VkPhysicalDevice physical_device)
597 unsigned int i;
599 for (i = 0; i < instance->phys_dev_count; ++i)
601 struct wine_phys_dev *current = instance->phys_devs[i];
602 if (current->phys_dev == physical_device)
603 return current;
606 ERR("Unrecognized physical device %p.\n", physical_device);
607 return NULL;
610 /* Helper function used for freeing an instance structure. This function supports full
611 * and partial object cleanups and can thus be used for vkCreateInstance failures.
613 static void wine_vk_instance_free(struct wine_instance *instance)
615 if (!instance)
616 return;
618 if (instance->phys_devs)
620 unsigned int i;
622 for (i = 0; i < instance->phys_dev_count; i++)
624 wine_vk_physical_device_free(instance->phys_devs[i]);
626 free(instance->phys_devs);
629 if (instance->instance)
631 vk_funcs->p_vkDestroyInstance(instance->instance, NULL /* allocator */);
632 WINE_VK_REMOVE_HANDLE_MAPPING(instance, instance);
635 pthread_rwlock_destroy(&instance->wrapper_lock);
636 free(instance->utils_messengers);
638 free(instance);
641 VkResult wine_vkAllocateCommandBuffers(VkDevice handle, const VkCommandBufferAllocateInfo_host *allocate_info,
642 VkCommandBuffer *buffers )
644 struct wine_device *device = wine_device_from_handle(handle);
645 struct wine_cmd_buffer *buffer;
646 struct wine_cmd_pool *pool;
647 VkResult res = VK_SUCCESS;
648 unsigned int i;
650 pool = wine_cmd_pool_from_handle(allocate_info->commandPool);
652 for (i = 0; i < allocate_info->commandBufferCount; i++)
654 VkCommandBufferAllocateInfo_host allocate_info_host;
656 /* TODO: future extensions (none yet) may require pNext conversion. */
657 allocate_info_host.pNext = allocate_info->pNext;
658 allocate_info_host.sType = allocate_info->sType;
659 allocate_info_host.commandPool = pool->command_pool;
660 allocate_info_host.level = allocate_info->level;
661 allocate_info_host.commandBufferCount = 1;
663 TRACE("Allocating command buffer %u from pool 0x%s.\n",
664 i, wine_dbgstr_longlong(allocate_info_host.commandPool));
666 if (!(buffer = calloc(1, sizeof(*buffer))))
668 res = VK_ERROR_OUT_OF_HOST_MEMORY;
669 break;
672 buffer->handle = buffers[i];
673 buffer->device = device;
674 res = device->funcs.p_vkAllocateCommandBuffers(device->device,
675 &allocate_info_host, &buffer->command_buffer);
676 buffer->handle->base.unix_handle = (uintptr_t)buffer;
677 WINE_VK_ADD_DISPATCHABLE_MAPPING(device->phys_dev->instance, buffer->handle,
678 buffer->command_buffer, buffer);
679 if (res != VK_SUCCESS)
681 ERR("Failed to allocate command buffer, res=%d.\n", res);
682 buffer->command_buffer = VK_NULL_HANDLE;
683 break;
687 if (res != VK_SUCCESS)
688 wine_vk_free_command_buffers(device, pool, i + 1, buffers);
690 return res;
693 VkResult wine_vkCreateDevice(VkPhysicalDevice phys_dev_handle, const VkDeviceCreateInfo *create_info,
694 const VkAllocationCallbacks *allocator, VkDevice *ret_device,
695 void *client_ptr)
697 struct wine_phys_dev *phys_dev = wine_phys_dev_from_handle(phys_dev_handle);
698 VkDevice device_handle = client_ptr;
699 VkDeviceCreateInfo create_info_host;
700 struct VkQueue_T *queue_handles;
701 struct wine_queue *next_queue;
702 struct wine_device *object;
703 struct conversion_context ctx;
704 unsigned int i;
705 VkResult res;
707 if (allocator)
708 FIXME("Support for allocation callbacks not implemented yet\n");
710 if (TRACE_ON(vulkan))
712 VkPhysicalDeviceProperties_host properties;
714 phys_dev->instance->funcs.p_vkGetPhysicalDeviceProperties(phys_dev->phys_dev, &properties);
716 TRACE("Device name: %s.\n", debugstr_a(properties.deviceName));
717 TRACE("Vendor ID: %#x, Device ID: %#x.\n", properties.vendorID, properties.deviceID);
718 TRACE("Driver version: %#x.\n", properties.driverVersion);
721 if (!(object = calloc(1, sizeof(*object))))
722 return VK_ERROR_OUT_OF_HOST_MEMORY;
724 object->phys_dev = phys_dev;
726 init_conversion_context(&ctx);
727 res = wine_vk_device_convert_create_info(&ctx, create_info, &create_info_host);
728 if (res == VK_SUCCESS)
729 res = phys_dev->instance->funcs.p_vkCreateDevice(phys_dev->phys_dev,
730 &create_info_host, NULL /* allocator */, &object->device);
731 free_conversion_context(&ctx);
732 WINE_VK_ADD_DISPATCHABLE_MAPPING(phys_dev->instance, device_handle, object->device, object);
733 if (res != VK_SUCCESS)
735 WARN("Failed to create device, res=%d.\n", res);
736 goto fail;
739 /* Just load all function pointers we are aware off. The loader takes care of filtering.
740 * We use vkGetDeviceProcAddr as opposed to vkGetInstanceProcAddr for efficiency reasons
741 * as functions pass through fewer dispatch tables within the loader.
743 #define USE_VK_FUNC(name) \
744 object->funcs.p_##name = (void *)vk_funcs->p_vkGetDeviceProcAddr(object->device, #name); \
745 if (object->funcs.p_##name == NULL) \
746 TRACE("Not found '%s'.\n", #name);
747 ALL_VK_DEVICE_FUNCS()
748 #undef USE_VK_FUNC
750 /* We need to cache all queues within the device as each requires wrapping since queues are
751 * dispatchable objects.
753 for (i = 0; i < create_info_host.queueCreateInfoCount; i++)
755 object->queue_count += create_info_host.pQueueCreateInfos[i].queueCount;
758 if (!(object->queues = calloc(object->queue_count, sizeof(*object->queues))))
760 res = VK_ERROR_OUT_OF_HOST_MEMORY;
761 goto fail;
764 next_queue = object->queues;
765 queue_handles = device_handle->queues;
766 for (i = 0; i < create_info_host.queueCreateInfoCount; i++)
768 uint32_t flags = create_info_host.pQueueCreateInfos[i].flags;
769 uint32_t family_index = create_info_host.pQueueCreateInfos[i].queueFamilyIndex;
770 uint32_t queue_count = create_info_host.pQueueCreateInfos[i].queueCount;
772 TRACE("Queue family index %u, queue count %u.\n", family_index, queue_count);
774 wine_vk_device_get_queues(object, family_index, queue_count, flags, next_queue, &queue_handles);
775 next_queue += queue_count;
778 device_handle->quirks = phys_dev->instance->quirks;
779 device_handle->base.unix_handle = (uintptr_t)object;
780 *ret_device = device_handle;
781 TRACE("Created device %p (native device %p).\n", object, object->device);
782 return VK_SUCCESS;
784 fail:
785 wine_vk_device_free(object);
786 return res;
789 VkResult wine_vkCreateInstance(const VkInstanceCreateInfo *create_info,
790 const VkAllocationCallbacks *allocator, VkInstance *instance,
791 void *client_ptr)
793 VkInstance client_instance = client_ptr;
794 VkInstanceCreateInfo create_info_host;
795 const VkApplicationInfo *app_info;
796 struct wine_instance *object;
797 struct conversion_context ctx;
798 VkResult res;
800 if (allocator)
801 FIXME("Support for allocation callbacks not implemented yet\n");
803 if (!(object = calloc(1, sizeof(*object))))
805 ERR("Failed to allocate memory for instance\n");
806 return VK_ERROR_OUT_OF_HOST_MEMORY;
808 list_init(&object->wrappers);
809 pthread_rwlock_init(&object->wrapper_lock, NULL);
811 init_conversion_context(&ctx);
812 res = wine_vk_instance_convert_create_info(&ctx, create_info, &create_info_host, object);
813 if (res == VK_SUCCESS)
814 res = vk_funcs->p_vkCreateInstance(&create_info_host, NULL /* allocator */, &object->instance);
815 free_conversion_context(&ctx);
816 if (res != VK_SUCCESS)
818 ERR("Failed to create instance, res=%d\n", res);
819 wine_vk_instance_free(object);
820 return res;
823 object->handle = client_instance;
824 WINE_VK_ADD_DISPATCHABLE_MAPPING(object, object->handle, object->instance, object);
826 /* Load all instance functions we are aware of. Note the loader takes care
827 * of any filtering for extensions which were not requested, but which the
828 * ICD may support.
830 #define USE_VK_FUNC(name) \
831 object->funcs.p_##name = (void *)vk_funcs->p_vkGetInstanceProcAddr(object->instance, #name);
832 ALL_VK_INSTANCE_FUNCS()
833 #undef USE_VK_FUNC
835 /* Cache physical devices for vkEnumeratePhysicalDevices within the instance as
836 * each vkPhysicalDevice is a dispatchable object, which means we need to wrap
837 * the native physical devices and present those to the application.
838 * Cleanup happens as part of wine_vkDestroyInstance.
840 res = wine_vk_instance_load_physical_devices(object);
841 if (res != VK_SUCCESS)
843 ERR("Failed to load physical devices, res=%d\n", res);
844 wine_vk_instance_free(object);
845 return res;
848 if ((app_info = create_info->pApplicationInfo))
850 TRACE("Application name %s, application version %#x.\n",
851 debugstr_a(app_info->pApplicationName), app_info->applicationVersion);
852 TRACE("Engine name %s, engine version %#x.\n", debugstr_a(app_info->pEngineName),
853 app_info->engineVersion);
854 TRACE("API version %#x.\n", app_info->apiVersion);
856 if (app_info->pEngineName && !strcmp(app_info->pEngineName, "idTech"))
857 object->quirks |= WINEVULKAN_QUIRK_GET_DEVICE_PROC_ADDR;
860 object->quirks |= WINEVULKAN_QUIRK_ADJUST_MAX_IMAGE_COUNT;
862 client_instance->base.unix_handle = (uintptr_t)object;
863 *instance = client_instance;
864 TRACE("Created instance %p (native instance %p).\n", object, object->instance);
865 return VK_SUCCESS;
868 void wine_vkDestroyDevice(VkDevice handle, const VkAllocationCallbacks *allocator)
870 struct wine_device *device = wine_device_from_handle(handle);
872 if (allocator)
873 FIXME("Support for allocation callbacks not implemented yet\n");
875 wine_vk_device_free(device);
878 void wine_vkDestroyInstance(VkInstance handle, const VkAllocationCallbacks *allocator)
880 struct wine_instance *instance = wine_instance_from_handle(handle);
882 if (allocator)
883 FIXME("Support allocation allocators\n");
885 wine_vk_instance_free(instance);
888 VkResult wine_vkEnumerateDeviceExtensionProperties(VkPhysicalDevice phys_dev_handle, const char *layer_name,
889 uint32_t *count, VkExtensionProperties *properties)
891 struct wine_phys_dev *phys_dev = wine_phys_dev_from_handle(phys_dev_handle);
893 /* This shouldn't get called with layer_name set, the ICD loader prevents it. */
894 if (layer_name)
896 ERR("Layer enumeration not supported from ICD.\n");
897 return VK_ERROR_LAYER_NOT_PRESENT;
900 if (!properties)
902 *count = phys_dev->extension_count;
903 return VK_SUCCESS;
906 *count = min(*count, phys_dev->extension_count);
907 memcpy(properties, phys_dev->extensions, *count * sizeof(*properties));
909 TRACE("Returning %u extensions.\n", *count);
910 return *count < phys_dev->extension_count ? VK_INCOMPLETE : VK_SUCCESS;
913 VkResult wine_vkEnumerateInstanceExtensionProperties(const char *name, uint32_t *count,
914 VkExtensionProperties *properties)
916 uint32_t num_properties = 0, num_host_properties;
917 VkExtensionProperties *host_properties;
918 unsigned int i, j;
919 VkResult res;
921 res = vk_funcs->p_vkEnumerateInstanceExtensionProperties(NULL, &num_host_properties, NULL);
922 if (res != VK_SUCCESS)
923 return res;
925 if (!(host_properties = calloc(num_host_properties, sizeof(*host_properties))))
926 return VK_ERROR_OUT_OF_HOST_MEMORY;
928 res = vk_funcs->p_vkEnumerateInstanceExtensionProperties(NULL, &num_host_properties, host_properties);
929 if (res != VK_SUCCESS)
931 ERR("Failed to retrieve host properties, res=%d.\n", res);
932 free(host_properties);
933 return res;
936 /* The Wine graphics driver provides us with all extensions supported by the host side
937 * including extension fixup (e.g. VK_KHR_xlib_surface -> VK_KHR_win32_surface). It is
938 * up to us here to filter the list down to extensions for which we have thunks.
940 for (i = 0; i < num_host_properties; i++)
942 if (wine_vk_instance_extension_supported(host_properties[i].extensionName))
943 num_properties++;
944 else
945 TRACE("Instance extension '%s' is not supported.\n", host_properties[i].extensionName);
948 if (!properties)
950 TRACE("Returning %u extensions.\n", num_properties);
951 *count = num_properties;
952 free(host_properties);
953 return VK_SUCCESS;
956 for (i = 0, j = 0; i < num_host_properties && j < *count; i++)
958 if (wine_vk_instance_extension_supported(host_properties[i].extensionName))
960 TRACE("Enabling extension '%s'.\n", host_properties[i].extensionName);
961 properties[j++] = host_properties[i];
964 *count = min(*count, num_properties);
966 free(host_properties);
967 return *count < num_properties ? VK_INCOMPLETE : VK_SUCCESS;
970 VkResult wine_vkEnumerateDeviceLayerProperties(VkPhysicalDevice phys_dev, uint32_t *count,
971 VkLayerProperties *properties)
973 *count = 0;
974 return VK_SUCCESS;
977 VkResult wine_vkEnumerateInstanceVersion(uint32_t *version)
979 VkResult res;
981 static VkResult (*p_vkEnumerateInstanceVersion)(uint32_t *version);
982 if (!p_vkEnumerateInstanceVersion)
983 p_vkEnumerateInstanceVersion = vk_funcs->p_vkGetInstanceProcAddr(NULL, "vkEnumerateInstanceVersion");
985 if (p_vkEnumerateInstanceVersion)
987 res = p_vkEnumerateInstanceVersion(version);
989 else
991 *version = VK_API_VERSION_1_0;
992 res = VK_SUCCESS;
995 TRACE("API version %u.%u.%u.\n",
996 VK_VERSION_MAJOR(*version), VK_VERSION_MINOR(*version), VK_VERSION_PATCH(*version));
997 *version = min(WINE_VK_VERSION, *version);
998 return res;
1001 VkResult wine_vkEnumeratePhysicalDevices(VkInstance handle, uint32_t *count, VkPhysicalDevice *devices)
1003 struct wine_instance *instance = wine_instance_from_handle(handle);
1004 unsigned int i;
1006 if (!devices)
1008 *count = instance->phys_dev_count;
1009 return VK_SUCCESS;
1012 *count = min(*count, instance->phys_dev_count);
1013 for (i = 0; i < *count; i++)
1015 devices[i] = instance->phys_devs[i]->handle;
1018 TRACE("Returning %u devices.\n", *count);
1019 return *count < instance->phys_dev_count ? VK_INCOMPLETE : VK_SUCCESS;
1022 void wine_vkFreeCommandBuffers(VkDevice handle, VkCommandPool command_pool, uint32_t count,
1023 const VkCommandBuffer *buffers)
1025 struct wine_device *device = wine_device_from_handle(handle);
1026 struct wine_cmd_pool *pool = wine_cmd_pool_from_handle(command_pool);
1028 wine_vk_free_command_buffers(device, pool, count, buffers);
1031 static VkQueue wine_vk_device_find_queue(VkDevice handle, const VkDeviceQueueInfo2 *info)
1033 struct wine_device *device = wine_device_from_handle(handle);
1034 struct wine_queue *queue;
1035 uint32_t i;
1037 for (i = 0; i < device->queue_count; i++)
1039 queue = &device->queues[i];
1040 if (queue->family_index == info->queueFamilyIndex
1041 && queue->queue_index == info->queueIndex
1042 && queue->flags == info->flags)
1044 return queue->handle;
1048 return VK_NULL_HANDLE;
1051 void wine_vkGetDeviceQueue(VkDevice device, uint32_t family_index, uint32_t queue_index, VkQueue *queue)
1053 VkDeviceQueueInfo2 queue_info;
1055 queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2;
1056 queue_info.pNext = NULL;
1057 queue_info.flags = 0;
1058 queue_info.queueFamilyIndex = family_index;
1059 queue_info.queueIndex = queue_index;
1061 *queue = wine_vk_device_find_queue(device, &queue_info);
1064 void wine_vkGetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2 *info, VkQueue *queue)
1066 const VkBaseInStructure *chain;
1068 if ((chain = info->pNext))
1069 FIXME("Ignoring a linked structure of type %u.\n", chain->sType);
1071 *queue = wine_vk_device_find_queue(device, info);
1074 VkResult wine_vkCreateCommandPool(VkDevice device_handle, const VkCommandPoolCreateInfo *info,
1075 const VkAllocationCallbacks *allocator, VkCommandPool *command_pool,
1076 void *client_ptr)
1078 struct wine_device *device = wine_device_from_handle(device_handle);
1079 struct vk_command_pool *handle = client_ptr;
1080 struct wine_cmd_pool *object;
1081 VkResult res;
1083 if (allocator)
1084 FIXME("Support for allocation callbacks not implemented yet\n");
1086 if (!(object = calloc(1, sizeof(*object))))
1087 return VK_ERROR_OUT_OF_HOST_MEMORY;
1089 res = device->funcs.p_vkCreateCommandPool(device->device, info, NULL, &object->command_pool);
1091 if (res == VK_SUCCESS)
1093 object->handle = (uintptr_t)handle;
1094 handle->unix_handle = (uintptr_t)object;
1095 WINE_VK_ADD_NON_DISPATCHABLE_MAPPING(device->phys_dev->instance, object->handle,
1096 object->command_pool, object);
1097 *command_pool = object->handle;
1099 else
1101 free(object);
1104 return res;
1107 void wine_vkDestroyCommandPool(VkDevice device_handle, VkCommandPool handle,
1108 const VkAllocationCallbacks *allocator)
1110 struct wine_device *device = wine_device_from_handle(device_handle);
1111 struct wine_cmd_pool *pool = wine_cmd_pool_from_handle(handle);
1113 if (allocator)
1114 FIXME("Support for allocation callbacks not implemented yet\n");
1116 WINE_VK_REMOVE_HANDLE_MAPPING(device->phys_dev->instance, pool);
1118 device->funcs.p_vkDestroyCommandPool(device->device, pool->command_pool, NULL);
1119 free(pool);
1122 static VkResult wine_vk_enumerate_physical_device_groups(struct wine_instance *instance,
1123 VkResult (*p_vkEnumeratePhysicalDeviceGroups)(VkInstance, uint32_t *, VkPhysicalDeviceGroupProperties *),
1124 uint32_t *count, VkPhysicalDeviceGroupProperties *properties)
1126 unsigned int i, j;
1127 VkResult res;
1129 res = p_vkEnumeratePhysicalDeviceGroups(instance->instance, count, properties);
1130 if (res < 0 || !properties)
1131 return res;
1133 for (i = 0; i < *count; ++i)
1135 VkPhysicalDeviceGroupProperties *current = &properties[i];
1136 for (j = 0; j < current->physicalDeviceCount; ++j)
1138 VkPhysicalDevice dev = current->physicalDevices[j];
1139 struct wine_phys_dev *phys_dev = wine_vk_instance_wrap_physical_device(instance, dev);
1140 if (!phys_dev)
1141 return VK_ERROR_INITIALIZATION_FAILED;
1142 current->physicalDevices[j] = phys_dev->handle;
1146 return res;
1149 VkResult wine_vkEnumeratePhysicalDeviceGroups(VkInstance handle, uint32_t *count,
1150 VkPhysicalDeviceGroupProperties *properties)
1152 struct wine_instance *instance = wine_instance_from_handle(handle);
1154 return wine_vk_enumerate_physical_device_groups(instance,
1155 instance->funcs.p_vkEnumeratePhysicalDeviceGroups, count, properties);
1158 VkResult wine_vkEnumeratePhysicalDeviceGroupsKHR(VkInstance handle, uint32_t *count,
1159 VkPhysicalDeviceGroupProperties *properties)
1161 struct wine_instance *instance = wine_instance_from_handle(handle);
1163 return wine_vk_enumerate_physical_device_groups(instance,
1164 instance->funcs.p_vkEnumeratePhysicalDeviceGroupsKHR, count, properties);
1167 void wine_vkGetPhysicalDeviceExternalFenceProperties(VkPhysicalDevice phys_dev,
1168 const VkPhysicalDeviceExternalFenceInfo *fence_info,
1169 VkExternalFenceProperties *properties)
1171 properties->exportFromImportedHandleTypes = 0;
1172 properties->compatibleHandleTypes = 0;
1173 properties->externalFenceFeatures = 0;
1176 void wine_vkGetPhysicalDeviceExternalFencePropertiesKHR(VkPhysicalDevice phys_dev,
1177 const VkPhysicalDeviceExternalFenceInfo *fence_info,
1178 VkExternalFenceProperties *properties)
1180 properties->exportFromImportedHandleTypes = 0;
1181 properties->compatibleHandleTypes = 0;
1182 properties->externalFenceFeatures = 0;
1185 void wine_vkGetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice phys_dev,
1186 const VkPhysicalDeviceExternalBufferInfo *buffer_info,
1187 VkExternalBufferProperties *properties)
1189 memset(&properties->externalMemoryProperties, 0, sizeof(properties->externalMemoryProperties));
1192 void wine_vkGetPhysicalDeviceExternalBufferPropertiesKHR(VkPhysicalDevice phys_dev,
1193 const VkPhysicalDeviceExternalBufferInfo *buffer_info,
1194 VkExternalBufferProperties *properties)
1196 memset(&properties->externalMemoryProperties, 0, sizeof(properties->externalMemoryProperties));
1199 VkResult wine_vkGetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice phys_dev_handle,
1200 const VkPhysicalDeviceImageFormatInfo2 *format_info,
1201 VkImageFormatProperties2_host *properties)
1203 struct wine_phys_dev *phys_dev = wine_phys_dev_from_handle(phys_dev_handle);
1204 VkExternalImageFormatProperties *external_image_properties;
1205 VkResult res;
1207 res = phys_dev->instance->funcs.p_vkGetPhysicalDeviceImageFormatProperties2(phys_dev->phys_dev,
1208 format_info, properties);
1210 if ((external_image_properties = wine_vk_find_struct(properties, EXTERNAL_IMAGE_FORMAT_PROPERTIES)))
1212 VkExternalMemoryProperties *p = &external_image_properties->externalMemoryProperties;
1213 p->externalMemoryFeatures = 0;
1214 p->exportFromImportedHandleTypes = 0;
1215 p->compatibleHandleTypes = 0;
1218 return res;
1221 VkResult wine_vkGetPhysicalDeviceImageFormatProperties2KHR(VkPhysicalDevice phys_dev_handle,
1222 const VkPhysicalDeviceImageFormatInfo2 *format_info,
1223 VkImageFormatProperties2_host *properties)
1225 struct wine_phys_dev *phys_dev = wine_phys_dev_from_handle(phys_dev_handle);
1226 VkExternalImageFormatProperties *external_image_properties;
1227 VkResult res;
1229 res = phys_dev->instance->funcs.p_vkGetPhysicalDeviceImageFormatProperties2KHR(phys_dev->phys_dev,
1230 format_info, properties);
1232 if ((external_image_properties = wine_vk_find_struct(properties, EXTERNAL_IMAGE_FORMAT_PROPERTIES)))
1234 VkExternalMemoryProperties *p = &external_image_properties->externalMemoryProperties;
1235 p->externalMemoryFeatures = 0;
1236 p->exportFromImportedHandleTypes = 0;
1237 p->compatibleHandleTypes = 0;
1240 return res;
1243 /* From ntdll/unix/sync.c */
1244 #define NANOSECONDS_IN_A_SECOND 1000000000
1245 #define TICKSPERSEC 10000000
1247 static inline VkTimeDomainEXT get_performance_counter_time_domain(void)
1249 #if !defined(__APPLE__) && defined(HAVE_CLOCK_GETTIME)
1250 # ifdef CLOCK_MONOTONIC_RAW
1251 return VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT;
1252 # else
1253 return VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT;
1254 # endif
1255 #else
1256 FIXME("No mapping for VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT on this platform.\n");
1257 return VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT;
1258 #endif
1261 static VkTimeDomainEXT map_to_host_time_domain(VkTimeDomainEXT domain)
1263 /* Matches ntdll/unix/sync.c's performance counter implementation. */
1264 if (domain == VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT)
1265 return get_performance_counter_time_domain();
1267 return domain;
1270 static inline uint64_t convert_monotonic_timestamp(uint64_t value)
1272 return value / (NANOSECONDS_IN_A_SECOND / TICKSPERSEC);
1275 static inline uint64_t convert_timestamp(VkTimeDomainEXT host_domain, VkTimeDomainEXT target_domain, uint64_t value)
1277 if (host_domain == target_domain)
1278 return value;
1280 /* Convert between MONOTONIC time in ns -> QueryPerformanceCounter */
1281 if ((host_domain == VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT || host_domain == VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT)
1282 && target_domain == VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT)
1283 return convert_monotonic_timestamp(value);
1285 FIXME("Couldn't translate between host domain %d and target domain %d\n", host_domain, target_domain);
1286 return value;
1289 VkResult wine_vkGetCalibratedTimestampsEXT(VkDevice handle, uint32_t timestamp_count,
1290 const VkCalibratedTimestampInfoEXT *timestamp_infos,
1291 uint64_t *timestamps, uint64_t *max_deviation)
1293 struct wine_device *device = wine_device_from_handle(handle);
1294 VkCalibratedTimestampInfoEXT* host_timestamp_infos;
1295 unsigned int i;
1296 VkResult res;
1297 TRACE("%p, %u, %p, %p, %p\n", device, timestamp_count, timestamp_infos, timestamps, max_deviation);
1299 if (!(host_timestamp_infos = malloc(sizeof(VkCalibratedTimestampInfoEXT) * timestamp_count)))
1300 return VK_ERROR_OUT_OF_HOST_MEMORY;
1302 for (i = 0; i < timestamp_count; i++)
1304 host_timestamp_infos[i].sType = timestamp_infos[i].sType;
1305 host_timestamp_infos[i].pNext = timestamp_infos[i].pNext;
1306 host_timestamp_infos[i].timeDomain = map_to_host_time_domain(timestamp_infos[i].timeDomain);
1309 res = device->funcs.p_vkGetCalibratedTimestampsEXT(device->device, timestamp_count, host_timestamp_infos, timestamps, max_deviation);
1310 if (res != VK_SUCCESS)
1311 return res;
1313 for (i = 0; i < timestamp_count; i++)
1314 timestamps[i] = convert_timestamp(host_timestamp_infos[i].timeDomain, timestamp_infos[i].timeDomain, timestamps[i]);
1316 free(host_timestamp_infos);
1318 return res;
1321 VkResult wine_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT(VkPhysicalDevice handle,
1322 uint32_t *time_domain_count,
1323 VkTimeDomainEXT *time_domains)
1325 struct wine_phys_dev *phys_dev = wine_phys_dev_from_handle(handle);
1326 BOOL supports_device = FALSE, supports_monotonic = FALSE, supports_monotonic_raw = FALSE;
1327 const VkTimeDomainEXT performance_counter_domain = get_performance_counter_time_domain();
1328 VkTimeDomainEXT *host_time_domains;
1329 uint32_t host_time_domain_count;
1330 VkTimeDomainEXT out_time_domains[2];
1331 uint32_t out_time_domain_count;
1332 unsigned int i;
1333 VkResult res;
1335 /* Find out the time domains supported on the host */
1336 res = phys_dev->instance->funcs.p_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT(phys_dev->phys_dev, &host_time_domain_count, NULL);
1337 if (res != VK_SUCCESS)
1338 return res;
1340 if (!(host_time_domains = malloc(sizeof(VkTimeDomainEXT) * host_time_domain_count)))
1341 return VK_ERROR_OUT_OF_HOST_MEMORY;
1343 res = phys_dev->instance->funcs.p_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT(phys_dev->phys_dev, &host_time_domain_count, host_time_domains);
1344 if (res != VK_SUCCESS)
1346 free(host_time_domains);
1347 return res;
1350 for (i = 0; i < host_time_domain_count; i++)
1352 if (host_time_domains[i] == VK_TIME_DOMAIN_DEVICE_EXT)
1353 supports_device = TRUE;
1354 else if (host_time_domains[i] == VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT)
1355 supports_monotonic = TRUE;
1356 else if (host_time_domains[i] == VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT)
1357 supports_monotonic_raw = TRUE;
1358 else
1359 FIXME("Unknown time domain %d\n", host_time_domains[i]);
1362 free(host_time_domains);
1364 out_time_domain_count = 0;
1366 /* Map our monotonic times -> QPC */
1367 if (supports_monotonic_raw && performance_counter_domain == VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT)
1368 out_time_domains[out_time_domain_count++] = VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT;
1369 else if (supports_monotonic && performance_counter_domain == VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT)
1370 out_time_domains[out_time_domain_count++] = VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT;
1371 else
1372 FIXME("VK_TIME_DOMAIN_QUERY_PERFORMANCE_COUNTER_EXT not supported on this platform.\n");
1374 /* Forward the device domain time */
1375 if (supports_device)
1376 out_time_domains[out_time_domain_count++] = VK_TIME_DOMAIN_DEVICE_EXT;
1378 /* Send the count/domains back to the app */
1379 if (!time_domains)
1381 *time_domain_count = out_time_domain_count;
1382 return VK_SUCCESS;
1385 for (i = 0; i < min(*time_domain_count, out_time_domain_count); i++)
1386 time_domains[i] = out_time_domains[i];
1388 res = *time_domain_count < out_time_domain_count ? VK_INCOMPLETE : VK_SUCCESS;
1389 *time_domain_count = out_time_domain_count;
1390 return res;
1393 void wine_vkGetPhysicalDeviceExternalSemaphoreProperties(VkPhysicalDevice phys_dev,
1394 const VkPhysicalDeviceExternalSemaphoreInfo *info,
1395 VkExternalSemaphoreProperties *properties)
1397 properties->exportFromImportedHandleTypes = 0;
1398 properties->compatibleHandleTypes = 0;
1399 properties->externalSemaphoreFeatures = 0;
1402 void wine_vkGetPhysicalDeviceExternalSemaphorePropertiesKHR(VkPhysicalDevice phys_dev,
1403 const VkPhysicalDeviceExternalSemaphoreInfo *info,
1404 VkExternalSemaphoreProperties *properties)
1406 properties->exportFromImportedHandleTypes = 0;
1407 properties->compatibleHandleTypes = 0;
1408 properties->externalSemaphoreFeatures = 0;
1411 VkResult wine_vkCreateWin32SurfaceKHR(VkInstance handle, const VkWin32SurfaceCreateInfoKHR *createInfo,
1412 const VkAllocationCallbacks *allocator, VkSurfaceKHR *surface)
1414 struct wine_instance *instance = wine_instance_from_handle(handle);
1415 struct wine_surface *object;
1416 VkResult res;
1418 if (allocator)
1419 FIXME("Support for allocation callbacks not implemented yet\n");
1421 object = calloc(1, sizeof(*object));
1423 if (!object)
1424 return VK_ERROR_OUT_OF_HOST_MEMORY;
1426 res = instance->funcs.p_vkCreateWin32SurfaceKHR(instance->instance, createInfo, NULL, &object->driver_surface);
1428 if (res != VK_SUCCESS)
1430 free(object);
1431 return res;
1434 object->surface = vk_funcs->p_wine_get_native_surface(object->driver_surface);
1436 WINE_VK_ADD_NON_DISPATCHABLE_MAPPING(instance, object, object->surface, object);
1438 *surface = wine_surface_to_handle(object);
1440 return VK_SUCCESS;
1443 void wine_vkDestroySurfaceKHR(VkInstance handle, VkSurfaceKHR surface,
1444 const VkAllocationCallbacks *allocator)
1446 struct wine_instance *instance = wine_instance_from_handle(handle);
1447 struct wine_surface *object = wine_surface_from_handle(surface);
1449 if (!object)
1450 return;
1452 instance->funcs.p_vkDestroySurfaceKHR(instance->instance, object->driver_surface, NULL);
1454 WINE_VK_REMOVE_HANDLE_MAPPING(instance, object);
1455 free(object);
1458 static inline void adjust_max_image_count(struct wine_phys_dev *phys_dev, VkSurfaceCapabilitiesKHR* capabilities)
1460 /* Many Windows games, for example Strange Brigade, No Man's Sky, Path of Exile
1461 * and World War Z, do not expect that maxImageCount can be set to 0.
1462 * A value of 0 means that there is no limit on the number of images.
1463 * Nvidia reports 8 on Windows, AMD 16.
1464 * https://vulkan.gpuinfo.org/displayreport.php?id=9122#surface
1465 * https://vulkan.gpuinfo.org/displayreport.php?id=9121#surface
1467 if ((phys_dev->instance->quirks & WINEVULKAN_QUIRK_ADJUST_MAX_IMAGE_COUNT) && !capabilities->maxImageCount)
1469 capabilities->maxImageCount = max(capabilities->minImageCount, 16);
1473 VkResult wine_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice handle, VkSurfaceKHR surface_handle,
1474 VkSurfaceCapabilitiesKHR *capabilities)
1476 struct wine_phys_dev *phys_dev = wine_phys_dev_from_handle(handle);
1477 struct wine_surface *surface = wine_surface_from_handle(surface_handle);
1478 VkResult res;
1480 res = phys_dev->instance->funcs.p_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(phys_dev->phys_dev,
1481 surface->driver_surface, capabilities);
1483 if (res == VK_SUCCESS)
1484 adjust_max_image_count(phys_dev, capabilities);
1486 return res;
1489 VkResult wine_vkGetPhysicalDeviceSurfaceCapabilities2KHR(VkPhysicalDevice handle,
1490 const VkPhysicalDeviceSurfaceInfo2KHR_host *surface_info,
1491 VkSurfaceCapabilities2KHR *capabilities)
1493 struct wine_phys_dev *phys_dev = wine_phys_dev_from_handle(handle);
1494 struct wine_surface *surface = wine_surface_from_handle(surface_info->surface);
1495 VkPhysicalDeviceSurfaceInfo2KHR_host host_info;
1496 VkResult res;
1498 host_info.sType = surface_info->sType;
1499 host_info.pNext = surface_info->pNext;
1500 host_info.surface = surface->driver_surface;
1501 res = phys_dev->instance->funcs.p_vkGetPhysicalDeviceSurfaceCapabilities2KHR(phys_dev->phys_dev,
1502 &host_info, capabilities);
1504 if (res == VK_SUCCESS)
1505 adjust_max_image_count(phys_dev, &capabilities->surfaceCapabilities);
1507 return res;
1510 VkResult wine_vkCreateDebugUtilsMessengerEXT(VkInstance handle,
1511 const VkDebugUtilsMessengerCreateInfoEXT *create_info,
1512 const VkAllocationCallbacks *allocator,
1513 VkDebugUtilsMessengerEXT *messenger)
1515 struct wine_instance *instance = wine_instance_from_handle(handle);
1516 VkDebugUtilsMessengerCreateInfoEXT wine_create_info;
1517 struct wine_debug_utils_messenger *object;
1518 VkResult res;
1520 if (allocator)
1521 FIXME("Support for allocation callbacks not implemented yet\n");
1523 if (!(object = calloc(1, sizeof(*object))))
1524 return VK_ERROR_OUT_OF_HOST_MEMORY;
1526 object->instance = instance;
1527 object->user_callback = create_info->pfnUserCallback;
1528 object->user_data = create_info->pUserData;
1530 wine_create_info = *create_info;
1532 wine_create_info.pfnUserCallback = (void *) &debug_utils_callback_conversion;
1533 wine_create_info.pUserData = object;
1535 res = instance->funcs.p_vkCreateDebugUtilsMessengerEXT(instance->instance, &wine_create_info, NULL, &object->debug_messenger);
1537 if (res != VK_SUCCESS)
1539 free(object);
1540 return res;
1543 WINE_VK_ADD_NON_DISPATCHABLE_MAPPING(instance, object, object->debug_messenger, object);
1544 *messenger = wine_debug_utils_messenger_to_handle(object);
1546 return VK_SUCCESS;
1549 void wine_vkDestroyDebugUtilsMessengerEXT(VkInstance handle, VkDebugUtilsMessengerEXT messenger,
1550 const VkAllocationCallbacks *allocator)
1552 struct wine_instance *instance = wine_instance_from_handle(handle);
1553 struct wine_debug_utils_messenger *object;
1555 object = wine_debug_utils_messenger_from_handle(messenger);
1557 if (!object)
1558 return;
1560 instance->funcs.p_vkDestroyDebugUtilsMessengerEXT(instance->instance, object->debug_messenger, NULL);
1561 WINE_VK_REMOVE_HANDLE_MAPPING(instance, object);
1563 free(object);
1566 VkResult wine_vkCreateDebugReportCallbackEXT(VkInstance handle,
1567 const VkDebugReportCallbackCreateInfoEXT *create_info,
1568 const VkAllocationCallbacks *allocator,
1569 VkDebugReportCallbackEXT *callback)
1571 struct wine_instance *instance = wine_instance_from_handle(handle);
1572 VkDebugReportCallbackCreateInfoEXT wine_create_info;
1573 struct wine_debug_report_callback *object;
1574 VkResult res;
1576 if (allocator)
1577 FIXME("Support for allocation callbacks not implemented yet\n");
1579 if (!(object = calloc(1, sizeof(*object))))
1580 return VK_ERROR_OUT_OF_HOST_MEMORY;
1582 object->instance = instance;
1583 object->user_callback = create_info->pfnCallback;
1584 object->user_data = create_info->pUserData;
1586 wine_create_info = *create_info;
1588 wine_create_info.pfnCallback = (void *) debug_report_callback_conversion;
1589 wine_create_info.pUserData = object;
1591 res = instance->funcs.p_vkCreateDebugReportCallbackEXT(instance->instance, &wine_create_info, NULL, &object->debug_callback);
1593 if (res != VK_SUCCESS)
1595 free(object);
1596 return res;
1599 WINE_VK_ADD_NON_DISPATCHABLE_MAPPING(instance, object, object->debug_callback, object);
1600 *callback = wine_debug_report_callback_to_handle(object);
1602 return VK_SUCCESS;
1605 void wine_vkDestroyDebugReportCallbackEXT(VkInstance handle, VkDebugReportCallbackEXT callback,
1606 const VkAllocationCallbacks *allocator)
1608 struct wine_instance *instance = wine_instance_from_handle(handle);
1609 struct wine_debug_report_callback *object;
1611 object = wine_debug_report_callback_from_handle(callback);
1613 if (!object)
1614 return;
1616 instance->funcs.p_vkDestroyDebugReportCallbackEXT(instance->instance, object->debug_callback, NULL);
1618 WINE_VK_REMOVE_HANDLE_MAPPING(instance, object);
1620 free(object);
1623 static void fixup_pipeline_feedback(VkPipelineCreationFeedback *feedback, uint32_t count)
1625 #if defined(USE_STRUCT_CONVERSION)
1626 struct host_pipeline_feedback
1628 VkPipelineCreationFeedbackFlags flags;
1629 uint64_t duration;
1630 } *host_feedback;
1631 int64_t i;
1633 host_feedback = (void *) feedback;
1635 for (i = count - 1; i >= 0; i--)
1637 memmove(&feedback[i].duration, &host_feedback[i].duration, sizeof(uint64_t));
1638 feedback[i].flags = host_feedback[i].flags;
1640 #endif
1643 static void fixup_pipeline_feedback_info(const void *pipeline_info)
1645 VkPipelineCreationFeedbackCreateInfo *feedback;
1647 feedback = wine_vk_find_struct(pipeline_info, PIPELINE_CREATION_FEEDBACK_CREATE_INFO);
1649 if (!feedback)
1650 return;
1652 fixup_pipeline_feedback(feedback->pPipelineCreationFeedback, 1);
1653 fixup_pipeline_feedback(feedback->pPipelineStageCreationFeedbacks,
1654 feedback->pipelineStageCreationFeedbackCount);
1657 VkResult wine_vkCreateComputePipelines(VkDevice handle, VkPipelineCache pipeline_cache,
1658 uint32_t count, const VkComputePipelineCreateInfo_host *create_infos,
1659 const VkAllocationCallbacks *allocator, VkPipeline *pipelines)
1661 struct wine_device *device = wine_device_from_handle(handle);
1662 VkResult res;
1663 uint32_t i;
1665 res = device->funcs.p_vkCreateComputePipelines(device->device, pipeline_cache, count, create_infos,
1666 allocator, pipelines);
1668 for (i = 0; i < count; i++)
1669 fixup_pipeline_feedback_info(&create_infos[i]);
1671 return res;
1674 VkResult wine_vkCreateGraphicsPipelines(VkDevice handle, VkPipelineCache pipeline_cache,
1675 uint32_t count, const VkGraphicsPipelineCreateInfo_host *create_infos,
1676 const VkAllocationCallbacks *allocator, VkPipeline *pipelines)
1678 struct wine_device *device = wine_device_from_handle(handle);
1679 VkResult res;
1680 uint32_t i;
1682 res = device->funcs.p_vkCreateGraphicsPipelines(device->device, pipeline_cache, count, create_infos,
1683 allocator, pipelines);
1685 for (i = 0; i < count; i++)
1686 fixup_pipeline_feedback_info(&create_infos[i]);
1688 return res;
1691 VkResult wine_vkCreateRayTracingPipelinesKHR(VkDevice handle, VkDeferredOperationKHR deferred_operation,
1692 VkPipelineCache pipeline_cache, uint32_t count,
1693 const VkRayTracingPipelineCreateInfoKHR_host *create_infos,
1694 const VkAllocationCallbacks *allocator, VkPipeline *pipelines)
1696 struct wine_device *device = wine_device_from_handle(handle);
1697 VkResult res;
1698 uint32_t i;
1700 res = device->funcs.p_vkCreateRayTracingPipelinesKHR(device->device, deferred_operation, pipeline_cache,
1701 count, create_infos, allocator, pipelines);
1703 for (i = 0; i < count; i++)
1704 fixup_pipeline_feedback_info(&create_infos[i]);
1706 return res;
1709 VkResult wine_vkCreateRayTracingPipelinesNV(VkDevice handle, VkPipelineCache pipeline_cache, uint32_t count,
1710 const VkRayTracingPipelineCreateInfoNV_host *create_infos,
1711 const VkAllocationCallbacks *allocator, VkPipeline *pipelines)
1713 struct wine_device *device = wine_device_from_handle(handle);
1714 VkResult res;
1715 uint32_t i;
1717 res = device->funcs.p_vkCreateRayTracingPipelinesNV(device->device, pipeline_cache, count, create_infos,
1718 allocator, pipelines);
1720 for (i = 0; i < count; i++)
1721 fixup_pipeline_feedback_info(&create_infos[i]);
1723 return res;
1726 NTSTATUS vk_is_available_instance_function(void *arg)
1728 struct is_available_instance_function_params *params = arg;
1729 struct wine_instance *instance = wine_instance_from_handle(params->instance);
1730 return !!vk_funcs->p_vkGetInstanceProcAddr(instance->instance, params->name);
1733 NTSTATUS vk_is_available_device_function(void *arg)
1735 struct is_available_device_function_params *params = arg;
1736 struct wine_device *device = wine_device_from_handle(params->device);
1737 return !!vk_funcs->p_vkGetDeviceProcAddr(device->device, params->name);