Sweden-Number/dlls/winex11.drv/vulkan.c

582 lines
20 KiB
C

/* X11DRV Vulkan implementation
*
* Copyright 2017 Roderick Colenbrander
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <stdarg.h>
#include <stdio.h>
#include "windef.h"
#include "winbase.h"
#include "wine/debug.h"
#include "wine/heap.h"
#include "wine/library.h"
#include "x11drv.h"
/* We only want host compatible structures and don't need alignment. */
#define WINE_VK_ALIGN(x)
#define VK_NO_PROTOTYPES
#include "wine/vulkan.h"
#include "wine/vulkan_driver.h"
WINE_DEFAULT_DEBUG_CHANNEL(vulkan);
#ifdef SONAME_LIBVULKAN
typedef VkFlags VkXlibSurfaceCreateFlagsKHR;
#define VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR 1000004000
struct wine_vk_surface
{
Window window;
VkSurfaceKHR surface; /* native surface */
};
typedef struct VkXlibSurfaceCreateInfoKHR
{
VkStructureType sType;
const void *pNext;
VkXlibSurfaceCreateFlagsKHR flags;
Display *dpy;
Window window;
} VkXlibSurfaceCreateInfoKHR;
static VkResult (*pvkAcquireNextImageKHR)(VkDevice, VkSwapchainKHR, uint64_t, VkSemaphore, VkFence, uint32_t *);
static VkResult (*pvkCreateInstance)(const VkInstanceCreateInfo *, const VkAllocationCallbacks *, VkInstance *);
static VkResult (*pvkCreateSwapchainKHR)(VkDevice, const VkSwapchainCreateInfoKHR *, const VkAllocationCallbacks *, VkSwapchainKHR *);
static VkResult (*pvkCreateXlibSurfaceKHR)(VkInstance, const VkXlibSurfaceCreateInfoKHR *, const VkAllocationCallbacks *, VkSurfaceKHR *);
static void (*pvkDestroyInstance)(VkInstance, const VkAllocationCallbacks *);
static void (*pvkDestroySurfaceKHR)(VkInstance, VkSurfaceKHR, const VkAllocationCallbacks *);
static void (*pvkDestroySwapchainKHR)(VkDevice, VkSwapchainKHR, const VkAllocationCallbacks *);
static VkResult (*pvkEnumerateInstanceExtensionProperties)(const char *, uint32_t *, VkExtensionProperties *);
static void * (*pvkGetDeviceProcAddr)(VkDevice, const char *);
static void * (*pvkGetInstanceProcAddr)(VkInstance, const char *);
static VkResult (*pvkGetPhysicalDeviceSurfaceCapabilitiesKHR)(VkPhysicalDevice, VkSurfaceKHR, VkSurfaceCapabilitiesKHR *);
static VkResult (*pvkGetPhysicalDeviceSurfaceFormatsKHR)(VkPhysicalDevice, VkSurfaceKHR, uint32_t *, VkSurfaceFormatKHR *);
static VkResult (*pvkGetPhysicalDeviceSurfacePresentModesKHR)(VkPhysicalDevice, VkSurfaceKHR, uint32_t *, VkPresentModeKHR *);
static VkResult (*pvkGetPhysicalDeviceSurfaceSupportKHR)(VkPhysicalDevice, uint32_t, VkSurfaceKHR, VkBool32 *);
static VkBool32 (*pvkGetPhysicalDeviceXlibPresentationSupportKHR)(VkPhysicalDevice, uint32_t, Display *, VisualID);
static VkResult (*pvkGetSwapchainImagesKHR)(VkDevice, VkSwapchainKHR, uint32_t *, VkImage *);
static VkResult (*pvkQueuePresentKHR)(VkQueue, const VkPresentInfoKHR *);
static void *X11DRV_get_vk_device_proc_addr(const char *name);
static void *X11DRV_get_vk_instance_proc_addr(VkInstance instance, const char *name);
static inline struct wine_vk_surface *surface_from_handle(VkSurfaceKHR handle)
{
return (struct wine_vk_surface *)(uintptr_t)handle;
}
static BOOL wine_vk_init(void)
{
static BOOL init_done = FALSE;
static void *vulkan_handle;
if (init_done) return (vulkan_handle != NULL);
init_done = TRUE;
if (!(vulkan_handle = wine_dlopen(SONAME_LIBVULKAN, RTLD_NOW, NULL, 0)))
{
ERR("Failed to load %s\n", SONAME_LIBVULKAN);
return FALSE;
}
#define LOAD_FUNCPTR(f) if ((p##f = wine_dlsym(vulkan_handle, #f, NULL, 0)) == NULL) return FALSE;
LOAD_FUNCPTR(vkAcquireNextImageKHR)
LOAD_FUNCPTR(vkCreateInstance)
LOAD_FUNCPTR(vkCreateSwapchainKHR)
LOAD_FUNCPTR(vkCreateXlibSurfaceKHR)
LOAD_FUNCPTR(vkDestroyInstance)
LOAD_FUNCPTR(vkDestroySurfaceKHR)
LOAD_FUNCPTR(vkDestroySwapchainKHR)
LOAD_FUNCPTR(vkEnumerateInstanceExtensionProperties)
LOAD_FUNCPTR(vkGetDeviceProcAddr)
LOAD_FUNCPTR(vkGetInstanceProcAddr)
LOAD_FUNCPTR(vkGetPhysicalDeviceSurfaceCapabilitiesKHR)
LOAD_FUNCPTR(vkGetPhysicalDeviceSurfaceFormatsKHR)
LOAD_FUNCPTR(vkGetPhysicalDeviceSurfacePresentModesKHR)
LOAD_FUNCPTR(vkGetPhysicalDeviceSurfaceSupportKHR)
LOAD_FUNCPTR(vkGetPhysicalDeviceXlibPresentationSupportKHR)
LOAD_FUNCPTR(vkGetSwapchainImagesKHR)
LOAD_FUNCPTR(vkQueuePresentKHR)
#undef LOAD_FUNCPTR
return TRUE;
}
/* Helper function for converting between win32 and X11 compatible VkInstanceCreateInfo.
* Caller is responsible for allocation and cleanup of 'dst'.
*/
static VkResult wine_vk_instance_convert_create_info(const VkInstanceCreateInfo *src,
VkInstanceCreateInfo *dst)
{
unsigned int i;
const char **enabled_extensions = NULL;
dst->sType = src->sType;
dst->flags = src->flags;
dst->pApplicationInfo = src->pApplicationInfo;
dst->pNext = src->pNext;
dst->enabledLayerCount = 0;
dst->ppEnabledLayerNames = NULL;
dst->enabledExtensionCount = 0;
dst->ppEnabledExtensionNames = NULL;
if (src->enabledExtensionCount > 0)
{
enabled_extensions = heap_calloc(src->enabledExtensionCount, sizeof(*src->ppEnabledExtensionNames));
if (!enabled_extensions)
{
ERR("Failed to allocate memory for enabled extensions\n");
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
for (i = 0; i < src->enabledExtensionCount; i++)
{
/* Substitute extension with X11 ones else copy. Long-term, when we
* support more extensions, we should store these in a list.
*/
if (!strcmp(src->ppEnabledExtensionNames[i], "VK_KHR_win32_surface"))
{
enabled_extensions[i] = "VK_KHR_xlib_surface";
}
else
{
enabled_extensions[i] = src->ppEnabledExtensionNames[i];
}
}
dst->ppEnabledExtensionNames = enabled_extensions;
dst->enabledExtensionCount = src->enabledExtensionCount;
}
return VK_SUCCESS;
}
static void wine_vk_surface_destroy(VkInstance instance, struct wine_vk_surface *surface)
{
if (!surface)
return;
/* vkDestroySurfaceKHR must handle VK_NULL_HANDLE (0) for surface. */
pvkDestroySurfaceKHR(instance, surface->surface, NULL /* allocator */);
if (surface->window)
XDestroyWindow(gdi_display, surface->window);
heap_free(surface);
}
static VkResult X11DRV_vkAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain,
uint64_t timeout, VkSemaphore semaphore, VkFence fence, uint32_t *index)
{
TRACE("%p, 0x%s, 0x%s, 0x%s, 0x%s, %p\n", device,
wine_dbgstr_longlong(swapchain), wine_dbgstr_longlong(timeout),
wine_dbgstr_longlong(semaphore), wine_dbgstr_longlong(fence), index);
return pvkAcquireNextImageKHR(device, swapchain, timeout, semaphore, fence, index);
}
static VkResult X11DRV_vkCreateInstance(const VkInstanceCreateInfo *create_info,
const VkAllocationCallbacks *allocator, VkInstance *instance)
{
VkInstanceCreateInfo create_info_host;
VkResult res;
TRACE("create_info %p, allocator %p, instance %p\n", create_info, allocator, instance);
if (allocator)
FIXME("Support for allocation callbacks not implemented yet\n");
/* Perform a second pass on converting VkInstanceCreateInfo. Winevulkan
* performed a first pass in which it handles everything except for WSI
* functionality such as VK_KHR_win32_surface. Handle this now.
*/
res = wine_vk_instance_convert_create_info(create_info, &create_info_host);
if (res != VK_SUCCESS)
{
ERR("Failed to convert instance create info, res=%d\n", res);
return res;
}
res = pvkCreateInstance(&create_info_host, NULL /* allocator */, instance);
heap_free((void *)create_info_host.ppEnabledExtensionNames);
return res;
}
static VkResult X11DRV_vkCreateSwapchainKHR(VkDevice device,
const VkSwapchainCreateInfoKHR *create_info,
const VkAllocationCallbacks *allocator, VkSwapchainKHR *swapchain)
{
VkSwapchainCreateInfoKHR create_info_host;
TRACE("%p %p %p %p\n", device, create_info, allocator, swapchain);
if (allocator)
FIXME("Support for allocation callbacks not implemented yet\n");
create_info_host = *create_info;
create_info_host.surface = surface_from_handle(create_info->surface)->surface;
return pvkCreateSwapchainKHR(device, &create_info_host, NULL /* allocator */,
swapchain);
}
static VkResult X11DRV_vkCreateWin32SurfaceKHR(VkInstance instance,
const VkWin32SurfaceCreateInfoKHR *create_info,
const VkAllocationCallbacks *allocator, VkSurfaceKHR *surface)
{
VkResult res;
VkXlibSurfaceCreateInfoKHR create_info_host;
struct wine_vk_surface *x11_surface;
TRACE("%p %p %p %p\n", instance, create_info, allocator, surface);
if (allocator)
FIXME("Support for allocation callbacks not implemented yet\n");
/* TODO: support child window rendering. */
if (GetAncestor(create_info->hwnd, GA_PARENT) != GetDesktopWindow())
{
FIXME("Application requires child window rendering, which is not implemented yet!\n");
return VK_ERROR_INCOMPATIBLE_DRIVER;
}
x11_surface = heap_alloc_zero(sizeof(*x11_surface));
if (!x11_surface)
return VK_ERROR_OUT_OF_HOST_MEMORY;
x11_surface->window = create_client_window(create_info->hwnd, &default_visual);
if (!x11_surface->window)
{
ERR("Failed to allocate client window for hwnd=%p\n", create_info->hwnd);
/* VK_KHR_win32_surface only allows out of host and device memory as errors. */
res = VK_ERROR_OUT_OF_HOST_MEMORY;
goto err;
}
create_info_host.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
create_info_host.pNext = NULL;
create_info_host.flags = 0; /* reserved */
create_info_host.dpy = gdi_display;
create_info_host.window = x11_surface->window;
res = pvkCreateXlibSurfaceKHR(instance, &create_info_host, NULL /* allocator */, &x11_surface->surface);
if (res != VK_SUCCESS)
{
ERR("Failed to create Xlib surface, res=%d\n", res);
goto err;
}
*surface = (uintptr_t)x11_surface;
TRACE("Created surface=0x%s\n", wine_dbgstr_longlong(*surface));
return VK_SUCCESS;
err:
wine_vk_surface_destroy(instance, x11_surface);
return res;
}
static void X11DRV_vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks *allocator)
{
TRACE("%p %p\n", instance, allocator);
if (allocator)
FIXME("Support for allocation callbacks not implemented yet\n");
pvkDestroyInstance(instance, NULL /* allocator */);
}
static void X11DRV_vkDestroySurfaceKHR(VkInstance instance, VkSurfaceKHR surface,
const VkAllocationCallbacks *allocator)
{
struct wine_vk_surface *x11_surface = surface_from_handle(surface);
TRACE("%p 0x%s %p\n", instance, wine_dbgstr_longlong(surface), allocator);
if (allocator)
FIXME("Support for allocation callbacks not implemented yet\n");
wine_vk_surface_destroy(instance, x11_surface);
}
static void X11DRV_vkDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain,
const VkAllocationCallbacks *allocator)
{
TRACE("%p, 0x%s %p\n", device, wine_dbgstr_longlong(swapchain), allocator);
if (allocator)
FIXME("Support for allocation callbacks not implemented yet\n");
pvkDestroySwapchainKHR(device, swapchain, NULL /* allocator */);
}
static VkResult X11DRV_vkEnumerateInstanceExtensionProperties(const char *layer_name,
uint32_t *count, VkExtensionProperties* properties)
{
unsigned int i;
VkResult res;
TRACE("layer_name %s, count %p, properties %p\n", debugstr_a(layer_name), count, properties);
/* This shouldn't get called with layer_name set, the ICD loader prevents it. */
if (layer_name)
{
ERR("Layer enumeration not supported from ICD.\n");
return VK_ERROR_LAYER_NOT_PRESENT;
}
/* We will return the same number of instance extensions reported by the host back to
* winevulkan. Along the way we may replace xlib extensions with their win32 equivalents.
* Winevulkan will perform more detailed filtering as it knows whether it has thunks
* for a particular extension.
*/
res = pvkEnumerateInstanceExtensionProperties(layer_name, count, properties);
if (!properties || res < 0)
return res;
for (i = 0; i < *count; i++)
{
/* For now the only x11 extension we need to fixup. Long-term we may need an array. */
if (!strcmp(properties[i].extensionName, "VK_KHR_xlib_surface"))
{
TRACE("Substituting VK_KHR_xlib_surface for VK_KHR_win32_surface\n");
snprintf(properties[i].extensionName, sizeof(properties[i].extensionName),
VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
properties[i].specVersion = VK_KHR_WIN32_SURFACE_SPEC_VERSION;
}
}
TRACE("Returning %u extensions.\n", *count);
return res;
}
static void *X11DRV_vkGetDeviceProcAddr(VkDevice device, const char *name)
{
void *proc_addr;
TRACE("%p, %s\n", device, debugstr_a(name));
if ((proc_addr = X11DRV_get_vk_device_proc_addr(name)))
return proc_addr;
return pvkGetDeviceProcAddr(device, name);
}
static void *X11DRV_vkGetInstanceProcAddr(VkInstance instance, const char *name)
{
void *proc_addr;
TRACE("%p, %s\n", instance, debugstr_a(name));
if ((proc_addr = X11DRV_get_vk_instance_proc_addr(instance, name)))
return proc_addr;
return pvkGetInstanceProcAddr(instance, name);
}
static VkResult X11DRV_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice phys_dev,
VkSurfaceKHR surface, VkSurfaceCapabilitiesKHR *capabilities)
{
struct wine_vk_surface *x11_surface = surface_from_handle(surface);
TRACE("%p, 0x%s, %p\n", phys_dev, wine_dbgstr_longlong(surface), capabilities);
return pvkGetPhysicalDeviceSurfaceCapabilitiesKHR(phys_dev, x11_surface->surface,
capabilities);
}
static VkResult X11DRV_vkGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice phys_dev,
VkSurfaceKHR surface, uint32_t *count, VkSurfaceFormatKHR *formats)
{
struct wine_vk_surface *x11_surface = surface_from_handle(surface);
TRACE("%p, 0x%s, %p, %p\n", phys_dev, wine_dbgstr_longlong(surface), count, formats);
return pvkGetPhysicalDeviceSurfaceFormatsKHR(phys_dev, x11_surface->surface,
count, formats);
}
static VkResult X11DRV_vkGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice phys_dev,
VkSurfaceKHR surface, uint32_t *count, VkPresentModeKHR *modes)
{
struct wine_vk_surface *x11_surface = surface_from_handle(surface);
TRACE("%p, 0x%s, %p, %p\n", phys_dev, wine_dbgstr_longlong(surface), count, modes);
return pvkGetPhysicalDeviceSurfacePresentModesKHR(phys_dev, x11_surface->surface, count,
modes);
}
static VkResult X11DRV_vkGetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice phys_dev,
uint32_t index, VkSurfaceKHR surface, VkBool32 *supported)
{
struct wine_vk_surface *x11_surface = surface_from_handle(surface);
TRACE("%p, %u, 0x%s, %p\n", phys_dev, index, wine_dbgstr_longlong(surface), supported);
return pvkGetPhysicalDeviceSurfaceSupportKHR(phys_dev, index, x11_surface->surface,
supported);
}
static VkBool32 X11DRV_vkGetPhysicalDeviceWin32PresentationSupportKHR(VkPhysicalDevice phys_dev,
uint32_t index)
{
TRACE("%p %u\n", phys_dev, index);
return pvkGetPhysicalDeviceXlibPresentationSupportKHR(phys_dev, index, gdi_display,
default_visual.visual->visualid);
}
static VkResult X11DRV_vkGetSwapchainImagesKHR(VkDevice device,
VkSwapchainKHR swapchain, uint32_t *count, VkImage *images)
{
TRACE("%p, 0x%s %p %p\n", device, wine_dbgstr_longlong(swapchain), count, images);
return pvkGetSwapchainImagesKHR(device, swapchain, count, images);
}
static VkResult X11DRV_vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR *present_info)
{
TRACE("%p, %p\n", queue, present_info);
return pvkQueuePresentKHR(queue, present_info);
}
static const struct vulkan_funcs vulkan_funcs =
{
X11DRV_vkAcquireNextImageKHR,
X11DRV_vkCreateInstance,
X11DRV_vkCreateSwapchainKHR,
X11DRV_vkCreateWin32SurfaceKHR,
X11DRV_vkDestroyInstance,
X11DRV_vkDestroySurfaceKHR,
X11DRV_vkDestroySwapchainKHR,
X11DRV_vkEnumerateInstanceExtensionProperties,
X11DRV_vkGetDeviceProcAddr,
X11DRV_vkGetInstanceProcAddr,
X11DRV_vkGetPhysicalDeviceSurfaceCapabilitiesKHR,
X11DRV_vkGetPhysicalDeviceSurfaceFormatsKHR,
X11DRV_vkGetPhysicalDeviceSurfacePresentModesKHR,
X11DRV_vkGetPhysicalDeviceSurfaceSupportKHR,
X11DRV_vkGetPhysicalDeviceWin32PresentationSupportKHR,
X11DRV_vkGetSwapchainImagesKHR,
X11DRV_vkQueuePresentKHR,
};
static void *get_vulkan_driver_device_proc_addr(const struct vulkan_funcs *vulkan_funcs,
const char *name)
{
if (!name || name[0] != 'v' || name[1] != 'k')
return NULL;
name += 2;
if (!strcmp(name, "AcquireNextImageKHR"))
return vulkan_funcs->p_vkAcquireNextImageKHR;
if (!strcmp(name, "CreateSwapchainKHR"))
return vulkan_funcs->p_vkCreateSwapchainKHR;
if (!strcmp(name, "DestroySwapchainKHR"))
return vulkan_funcs->p_vkDestroySwapchainKHR;
if (!strcmp(name, "GetDeviceProcAddr"))
return vulkan_funcs->p_vkGetDeviceProcAddr;
if (!strcmp(name, "GetSwapchainImagesKHR"))
return vulkan_funcs->p_vkGetSwapchainImagesKHR;
if (!strcmp(name, "QueuePresentKHR"))
return vulkan_funcs->p_vkQueuePresentKHR;
return NULL;
}
static void *X11DRV_get_vk_device_proc_addr(const char *name)
{
return get_vulkan_driver_device_proc_addr(&vulkan_funcs, name);
}
static void *get_vulkan_driver_instance_proc_addr(const struct vulkan_funcs *vulkan_funcs,
VkInstance instance, const char *name)
{
if (!name || name[0] != 'v' || name[1] != 'k')
return NULL;
name += 2;
if (!strcmp(name, "CreateInstance"))
return vulkan_funcs->p_vkCreateInstance;
if (!strcmp(name, "EnumerateInstanceExtensionProperties"))
return vulkan_funcs->p_vkEnumerateInstanceExtensionProperties;
if (!instance)
return NULL;
if (!strcmp(name, "CreateWin32SurfaceKHR"))
return vulkan_funcs->p_vkCreateWin32SurfaceKHR;
if (!strcmp(name, "DestroyInstance"))
return vulkan_funcs->p_vkDestroyInstance;
if (!strcmp(name, "DestroySurfaceKHR"))
return vulkan_funcs->p_vkDestroySurfaceKHR;
if (!strcmp(name, "GetInstanceProcAddr"))
return vulkan_funcs->p_vkGetInstanceProcAddr;
if (!strcmp(name, "GetPhysicalDeviceSurfaceCapabilitiesKHR"))
return vulkan_funcs->p_vkGetPhysicalDeviceSurfaceCapabilitiesKHR;
if (!strcmp(name, "GetPhysicalDeviceSurfaceFormatsKHR"))
return vulkan_funcs->p_vkGetPhysicalDeviceSurfaceFormatsKHR;
if (!strcmp(name, "GetPhysicalDeviceSurfacePresentModesKHR"))
return vulkan_funcs->p_vkGetPhysicalDeviceSurfacePresentModesKHR;
if (!strcmp(name, "GetPhysicalDeviceSurfaceSupportKHR"))
return vulkan_funcs->p_vkGetPhysicalDeviceSurfaceSupportKHR;
if (!strcmp(name, "GetPhysicalDeviceWin32PresentationSupportKHR"))
return vulkan_funcs->p_vkGetPhysicalDeviceWin32PresentationSupportKHR;
name -= 2;
return get_vulkan_driver_device_proc_addr(vulkan_funcs, name);
}
static void *X11DRV_get_vk_instance_proc_addr(VkInstance instance, const char *name)
{
return get_vulkan_driver_instance_proc_addr(&vulkan_funcs, instance, name);
}
const struct vulkan_funcs *get_vulkan_driver(UINT version)
{
if (version != WINE_VULKAN_DRIVER_VERSION)
{
ERR("version mismatch, vulkan wants %u but driver has %u\n", version, WINE_VULKAN_DRIVER_VERSION);
return NULL;
}
if (wine_vk_init())
return &vulkan_funcs;
return NULL;
}
#else /* No vulkan */
const struct vulkan_funcs *get_vulkan_driver(UINT version)
{
ERR("Wine was built without Vulkan support.\n");
return NULL;
}
#endif /* SONAME_LIBVULKAN */