/*1* Copyright 2016 Józef Kucia for CodeWeavers2*3* This library is free software; you can redistribute it and/or4* modify it under the terms of the GNU Lesser General Public5* License as published by the Free Software Foundation; either6* version 2.1 of the License, or (at your option) any later version.7*8* This library is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU11* Lesser General Public License for more details.12*13* You should have received a copy of the GNU Lesser General Public14* License along with this library; if not, write to the Free Software15* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA16*/1718#ifndef __VKD3D_H19#define __VKD3D_H2021#include <vkd3d_types.h>2223#ifndef VKD3D_NO_WIN32_TYPES24# include <windows.h>25# include <d3d12.h>26#endif /* VKD3D_NO_WIN32_TYPES */2728#ifndef VKD3D_NO_VULKAN_H29# include <wine/vulkan.h>30#endif /* VKD3D_NO_VULKAN_H */3132#ifdef __cplusplus33extern "C" {34#endif /* __cplusplus */3536/**37* \file vkd3d.h38*39* This file contains definitions for the vkd3d library.40*41* The vkd3d library is a 3D graphics library built on top of42* Vulkan. It has an API very similar, but not identical, to43* Direct3D 12.44*45* \since 1.046*/4748/** The type of a chained structure. */49enum vkd3d_structure_type50{51/** The structure is a vkd3d_instance_create_info structure. */52VKD3D_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,53/** The structure is a vkd3d_device_create_info structure. */54VKD3D_STRUCTURE_TYPE_DEVICE_CREATE_INFO,55/** The structure is a vkd3d_image_resource_create_info structure. */56VKD3D_STRUCTURE_TYPE_IMAGE_RESOURCE_CREATE_INFO,5758/**59* The structure is a vkd3d_optional_instance_extensions_info structure.60* \since 1.161*/62VKD3D_STRUCTURE_TYPE_OPTIONAL_INSTANCE_EXTENSIONS_INFO,6364/**65* The structure is a vkd3d_optional_device_extensions_info structure.66* \since 1.267*/68VKD3D_STRUCTURE_TYPE_OPTIONAL_DEVICE_EXTENSIONS_INFO,69/**70* The structure is a vkd3d_application_info structure.71* \since 1.272*/73VKD3D_STRUCTURE_TYPE_APPLICATION_INFO,7475/**76* The structure is a vkd3d_host_time_domain_info structure.77* \since 1.378*/79VKD3D_STRUCTURE_TYPE_HOST_TIME_DOMAIN_INFO,8081VKD3D_FORCE_32_BIT_ENUM(VKD3D_STRUCTURE_TYPE),82};8384enum vkd3d_api_version85{86VKD3D_API_VERSION_1_0,87VKD3D_API_VERSION_1_1,88VKD3D_API_VERSION_1_2,89VKD3D_API_VERSION_1_3,90VKD3D_API_VERSION_1_4,91VKD3D_API_VERSION_1_5,92VKD3D_API_VERSION_1_6,93VKD3D_API_VERSION_1_7,94VKD3D_API_VERSION_1_8,95VKD3D_API_VERSION_1_9,96VKD3D_API_VERSION_1_10,97VKD3D_API_VERSION_1_11,98VKD3D_API_VERSION_1_12,99VKD3D_API_VERSION_1_13,100VKD3D_API_VERSION_1_14,101VKD3D_API_VERSION_1_15,102VKD3D_API_VERSION_1_16,103VKD3D_API_VERSION_1_17,104105VKD3D_FORCE_32_BIT_ENUM(VKD3D_API_VERSION),106};107108typedef HRESULT (*PFN_vkd3d_signal_event)(HANDLE event);109110typedef void * (*PFN_vkd3d_thread)(void *data);111112typedef void * (*PFN_vkd3d_create_thread)(PFN_vkd3d_thread thread_main, void *data);113typedef HRESULT (*PFN_vkd3d_join_thread)(void *thread);114115struct vkd3d_instance;116117/**118* A chained structure containing instance creation parameters.119*/120struct vkd3d_instance_create_info121{122/** Must be set to VKD3D_STRUCTURE_TYPE_INSTANCE_CREATE_INFO. */123enum vkd3d_structure_type type;124/** Optional pointer to a structure containing further parameters. */125const void *next;126127/** An pointer to a function to signal events. */128PFN_vkd3d_signal_event pfn_signal_event;129/**130* An optional pointer to a function to create threads. If this is NULL vkd3d will use a131* function of its choice, depending on the platform. It must be NULL if and only if132* pfn_join_thread is NULL.133*/134PFN_vkd3d_create_thread pfn_create_thread;135/**136* An optional pointer to a function to join threads. If this is NULL vkd3d will use a137* function of its choice, depending on the platform. It must be NULL if and only if138* pfn_create_thread is NULL.139*/140PFN_vkd3d_join_thread pfn_join_thread;141/** The size of type WCHAR. It must be 2 or 4 and should normally be set to sizeof(WCHAR). */142size_t wchar_size;143144/**145* A pointer to the vkGetInstanceProcAddr Vulkan function, which will be used to load all the146* other Vulkan functions. If set to NULL, vkd3d will search and use the Vulkan loader.147*/148PFN_vkGetInstanceProcAddr pfn_vkGetInstanceProcAddr;149150/**151* A list of Vulkan instance extensions to request. They are intended as required, so instance152* creation will fail if any of them is not available.153*/154const char * const *instance_extensions;155/** The number of elements in the instance_extensions array. */156uint32_t instance_extension_count;157};158159/**160* A chained structure to specify optional instance extensions.161*162* This structure extends vkd3d_instance_create_info.163*164* \since 1.1165*/166struct vkd3d_optional_instance_extensions_info167{168/** Must be set to VKD3D_STRUCTURE_TYPE_OPTIONAL_INSTANCE_EXTENSIONS_INFO. */169enum vkd3d_structure_type type;170/** Optional pointer to a structure containing further parameters. */171const void *next;172173/**174* A list of optional Vulkan instance extensions to request. Instance creation does not fail if175* they are not available.176*/177const char * const *extensions;178/** The number of elements in the extensions array. */179uint32_t extension_count;180};181182/**183* A chained structure to specify application information.184*185* This structure extends vkd3d_instance_create_info.186*187* \since 1.2188*/189struct vkd3d_application_info190{191/** Must be set to VKD3D_STRUCTURE_TYPE_APPLICATION_INFO. */192enum vkd3d_structure_type type;193/** Optional pointer to a structure containing further parameters. */194const void *next;195196/**197* The application's name, to be passed to the Vulkan implementation. If it is NULL, a name is198* computed from the process executable filename. If that cannot be done, the empty string is199* used.200*/201const char *application_name;202/** The application's version, to be passed to the Vulkan implementation. */203uint32_t application_version;204205/**206* The engine name, to be passed to the Vulkan implementation. If it is NULL, "vkd3d" is used.207*/208const char *engine_name;209/**210* The engine version, to be passed to the Vulkan implementation. If it is 0, the version is211* computed from the vkd3d library version.212*/213uint32_t engine_version;214215/**216* The vkd3d API version to use, to guarantee backward compatibility of the shared library. If217* this chained structure is not used then VKD3D_API_VERSION_1_0 is used.218*/219enum vkd3d_api_version api_version;220};221222/**223* A chained structure to specify the host time domain.224*225* This structure extends vkd3d_instance_create_info.226*227* \since 1.3228*/229struct vkd3d_host_time_domain_info230{231/** Must be set to VKD3D_STRUCTURE_TYPE_HOST_TIME_DOMAIN_INFO. */232enum vkd3d_structure_type type;233/** Optional pointer to a structure containing further parameters. */234const void *next;235236/**237* The number of clock ticks per second, used for GetClockCalibration(). It should normally238* match the expected result of QueryPerformanceFrequency(). If this chained structure is not239* used then 10 millions is used, which means that each tick is a tenth of microsecond, or240* equivalently 100 nanoseconds.241*/242uint64_t ticks_per_second;243};244245/**246* A chained structure containing device creation parameters.247*/248struct vkd3d_device_create_info249{250/** Must be set to VKD3D_STRUCTURE_TYPE_DEVICE_CREATE_INFO. */251enum vkd3d_structure_type type;252/** Optional pointer to a structure containing further parameters. */253const void *next;254255/** The minimum feature level to request. Device creation will fail with E_INVALIDARG if the256* Vulkan device doesn't have the features needed to fulfill the request. */257D3D_FEATURE_LEVEL minimum_feature_level;258259/**260* The vkd3d instance to use to create a device. Either this or instance_create_info must be261* set.262*/263struct vkd3d_instance *instance;264/**265* The parameters used to create an instance, which is then used to create a device. Either266* this or instance must be set.267*/268const struct vkd3d_instance_create_info *instance_create_info;269270/**271* The Vulkan physical device to use. If it is NULL, the first physical device found is used,272* prioritizing discrete GPUs over integrated GPUs and integrated GPUs over all the others.273*274* This parameter can be overridden by setting environment variable VKD3D_VULKAN_DEVICE.275*/276VkPhysicalDevice vk_physical_device;277278/**279* A list of Vulkan device extensions to request. They are intended as required, so device280* creation will fail if any of them is not available.281*/282const char * const *device_extensions;283/** The number of elements in the device_extensions array. */284uint32_t device_extension_count;285286/**287* An object to be set as the device parent. This is not used by vkd3d except for being288* returned by vkd3d_get_device_parent.289*/290IUnknown *parent;291/**292* The adapter LUID to be set for the device. This is not used by vkd3d except for being293* returned by GetAdapterLuid.294*/295LUID adapter_luid;296};297298/**299* A chained structure to specify optional device extensions.300*301* This structure extends vkd3d_device_create_info.302*303* \since 1.2304*/305struct vkd3d_optional_device_extensions_info306{307/** Must be set to VKD3D_STRUCTURE_TYPE_OPTIONAL_DEVICE_EXTENSIONS_INFO. */308enum vkd3d_structure_type type;309/** Optional pointer to a structure containing further parameters. */310const void *next;311312/**313* A list of optional Vulkan device extensions to request. Device creation does not fail if314* they are not available.315*/316const char * const *extensions;317/** The number of elements in the extensions array. */318uint32_t extension_count;319};320321/**322* When specified as a flag of vkd3d_image_resource_create_info, it means that vkd3d will do the323* initial transition operation on the image from VK_IMAGE_LAYOUT_UNDEFINED to its appropriate324* Vulkan layout (depending on its D3D12 resource state). If this flag is not specified the caller325* is responsible for transitioning the Vulkan image to the appropriate layout.326*/327#define VKD3D_RESOURCE_INITIAL_STATE_TRANSITION 0x00000001328/**329* When specified as a flag of vkd3d_image_resource_create_info, it means that field present_state330* is honored.331*/332#define VKD3D_RESOURCE_PRESENT_STATE_TRANSITION 0x00000002333334/**335* A chained structure containing the parameters to create a D3D12 resource backed by a Vulkan336* image.337*/338struct vkd3d_image_resource_create_info339{340/** Must be set to VKD3D_STRUCTURE_TYPE_IMAGE_RESOURCE_CREATE_INFO. */341enum vkd3d_structure_type type;342/** Optional pointer to a structure containing further parameters. */343const void *next;344345/** The Vulkan image that backs the resource. */346VkImage vk_image;347/** The resource description. */348D3D12_RESOURCE_DESC desc;349/**350* A combination of zero or more flags. The valid flags are351* VKD3D_RESOURCE_INITIAL_STATE_TRANSITION and VKD3D_RESOURCE_PRESENT_STATE_TRANSITION.352*/353unsigned int flags;354/**355* This field specifies how to handle resource state D3D12_RESOURCE_STATE_PRESENT for356* the resource. Notice that on D3D12 there is no difference between357* D3D12_RESOURCE_STATE_COMMON and D3D12_RESOURCE_STATE_PRESENT (they have the same value),358* while on Vulkan two different layouts are used (VK_IMAGE_LAYOUT_GENERAL and359* VK_IMAGE_LAYOUT_PRESENT_SRC_KHR).360*361* * When flag VKD3D_RESOURCE_PRESENT_STATE_TRANSITION is not specified, field362* present_state is ignored and resource state D3D12_RESOURCE_STATE_COMMON/_PRESENT is363* mapped to VK_IMAGE_LAYOUT_GENERAL; this is useful for non-swapchain resources.364* * Otherwise, when present_state is D3D12_RESOURCE_STATE_PRESENT/_COMMON, resource state365* D3D12_RESOURCE_STATE_COMMON/_PRESENT is mapped to VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;366* this is useful for swapchain resources that are directly backed by a Vulkan swapchain367* image.368* * Otherwise, resource state D3D12_RESOURCE_STATE_COMMON/_PRESENT is treated as resource369* state present_state; this is useful for swapchain resources that backed by a Vulkan370* non-swapchain image, which the client will likely consume with a copy or drawing371* operation at presentation time.372*/373D3D12_RESOURCE_STATES present_state;374};375376#ifdef LIBVKD3D_SOURCE377# define VKD3D_API VKD3D_EXPORT378#else379# define VKD3D_API VKD3D_IMPORT380#endif381382#ifndef VKD3D_NO_PROTOTYPES383384VKD3D_API HRESULT vkd3d_create_instance(const struct vkd3d_instance_create_info *create_info,385struct vkd3d_instance **instance);386VKD3D_API ULONG vkd3d_instance_decref(struct vkd3d_instance *instance);387VKD3D_API VkInstance vkd3d_instance_get_vk_instance(struct vkd3d_instance *instance);388VKD3D_API ULONG vkd3d_instance_incref(struct vkd3d_instance *instance);389390VKD3D_API HRESULT vkd3d_create_device(const struct vkd3d_device_create_info *create_info,391REFIID iid, void **device);392VKD3D_API IUnknown *vkd3d_get_device_parent(ID3D12Device *device);393VKD3D_API VkDevice vkd3d_get_vk_device(ID3D12Device *device);394VKD3D_API VkPhysicalDevice vkd3d_get_vk_physical_device(ID3D12Device *device);395VKD3D_API struct vkd3d_instance *vkd3d_instance_from_device(ID3D12Device *device);396397VKD3D_API uint32_t vkd3d_get_vk_queue_family_index(ID3D12CommandQueue *queue);398399/**400* Acquire the Vulkan queue backing a command queue.401*402* While a queue is acquired by the client, it is locked so that403* neither the vkd3d library nor other threads can submit work to404* it. For that reason it should be released as soon as possible with405* vkd3d_release_vk_queue(). The lock is not reentrant, so the same406* queue must not be acquired more than once by the same thread.407*408* Work submitted through the Direct3D 12 API exposed by vkd3d is not409* always immediately submitted to the Vulkan queue; sometimes it is410* kept in another internal queue, which might not necessarily be411* empty at the time vkd3d_acquire_vk_queue() is called. For this412* reason, work submitted directly to the Vulkan queue might appear to413* the Vulkan driver as being submitted before other work submitted414* though the Direct3D 12 API. If this is not desired, it is415* recommended to synchronize work submission using an ID3D12Fence416* object:417* 1. submit work through the Direct3D 12 API;418* 2. call vkd3d_queue_signal_on_cpu();419* 3. wait for the fence to be signalled;420* 4. call vkd3d_acquire_vk_queue(); it is guaranteed that all work submitted421* at point 1 has already been submitted to Vulkan (though not necessarily422* executed).423*424* \since 1.0425*/426VKD3D_API VkQueue vkd3d_acquire_vk_queue(ID3D12CommandQueue *queue);427428/**429* Release the Vulkan queue backing a command queue.430*431* This must be paired to an earlier corresponding432* vkd3d_acquire_vk_queue(). After this function is called, the Vulkan433* queue returned by vkd3d_acquire_vk_queue() must not be used any434* more.435*436* \since 1.0437*/438VKD3D_API void vkd3d_release_vk_queue(ID3D12CommandQueue *queue);439440VKD3D_API HRESULT vkd3d_create_image_resource(ID3D12Device *device,441const struct vkd3d_image_resource_create_info *create_info, ID3D12Resource **resource);442VKD3D_API ULONG vkd3d_resource_decref(ID3D12Resource *resource);443VKD3D_API ULONG vkd3d_resource_incref(ID3D12Resource *resource);444445VKD3D_API HRESULT vkd3d_serialize_root_signature(const D3D12_ROOT_SIGNATURE_DESC *desc,446D3D_ROOT_SIGNATURE_VERSION version, ID3DBlob **blob, ID3DBlob **error_blob);447VKD3D_API HRESULT vkd3d_create_root_signature_deserializer(const void *data, SIZE_T data_size,448REFIID iid, void **deserializer);449450VKD3D_API VkFormat vkd3d_get_vk_format(DXGI_FORMAT format);451452/* 1.1 */453VKD3D_API DXGI_FORMAT vkd3d_get_dxgi_format(VkFormat format);454455/* 1.2 */456VKD3D_API HRESULT vkd3d_serialize_versioned_root_signature(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC *desc,457ID3DBlob **blob, ID3DBlob **error_blob);458VKD3D_API HRESULT vkd3d_create_versioned_root_signature_deserializer(const void *data, SIZE_T data_size,459REFIID iid, void **deserializer);460461/**462* Set a callback to be called when vkd3d outputs debug logging.463*464* If NULL, or if this function has not been called, libvkd3d will print all465* enabled log output to stderr.466*467* Calling this function will also set the log callback for libvkd3d-shader.468*469* \param callback Callback function to set.470*471* \since 1.4472*/473VKD3D_API void vkd3d_set_log_callback(PFN_vkd3d_log callback);474475/**476* Signal a fence on the CPU once all the currently outstanding queue work is477* submitted to Vulkan.478*479* The fence will be signalled on the CPU (as if ID3D12Fence_Signal() was480* called) once all the work submitted through the Direct3D 12 API before481* vkd3d_queue_signal_on_cpu() is called has left the internal queue and has482* been submitted to the underlying Vulkan queue. Read the documentation for483* vkd3d_acquire_vk_queue() for more details.484*485* \since 1.15486*/487VKD3D_API HRESULT vkd3d_queue_signal_on_cpu(ID3D12CommandQueue *queue,488ID3D12Fence *fence, uint64_t value);489490#endif /* VKD3D_NO_PROTOTYPES */491492/*493* Function pointer typedefs for vkd3d functions.494*/495typedef HRESULT (*PFN_vkd3d_create_instance)(const struct vkd3d_instance_create_info *create_info,496struct vkd3d_instance **instance);497typedef ULONG (*PFN_vkd3d_instance_decref)(struct vkd3d_instance *instance);498typedef VkInstance (*PFN_vkd3d_instance_get_vk_instance)(struct vkd3d_instance *instance);499typedef ULONG (*PFN_vkd3d_instance_incref)(struct vkd3d_instance *instance);500501typedef HRESULT (*PFN_vkd3d_create_device)(const struct vkd3d_device_create_info *create_info,502REFIID iid, void **device);503typedef IUnknown * (*PFN_vkd3d_get_device_parent)(ID3D12Device *device);504typedef VkDevice (*PFN_vkd3d_get_vk_device)(ID3D12Device *device);505typedef VkPhysicalDevice (*PFN_vkd3d_get_vk_physical_device)(ID3D12Device *device);506typedef struct vkd3d_instance * (*PFN_vkd3d_instance_from_device)(ID3D12Device *device);507508typedef uint32_t (*PFN_vkd3d_get_vk_queue_family_index)(ID3D12CommandQueue *queue);509typedef VkQueue (*PFN_vkd3d_acquire_vk_queue)(ID3D12CommandQueue *queue);510typedef void (*PFN_vkd3d_release_vk_queue)(ID3D12CommandQueue *queue);511512typedef HRESULT (*PFN_vkd3d_create_image_resource)(ID3D12Device *device,513const struct vkd3d_image_resource_create_info *create_info, ID3D12Resource **resource);514typedef ULONG (*PFN_vkd3d_resource_decref)(ID3D12Resource *resource);515typedef ULONG (*PFN_vkd3d_resource_incref)(ID3D12Resource *resource);516517typedef HRESULT (*PFN_vkd3d_serialize_root_signature)(const D3D12_ROOT_SIGNATURE_DESC *desc,518D3D_ROOT_SIGNATURE_VERSION version, ID3DBlob **blob, ID3DBlob **error_blob);519typedef HRESULT (*PFN_vkd3d_create_root_signature_deserializer)(const void *data, SIZE_T data_size,520REFIID iid, void **deserializer);521522typedef VkFormat (*PFN_vkd3d_get_vk_format)(DXGI_FORMAT format);523524/* 1.1 */525typedef DXGI_FORMAT (*PFN_vkd3d_get_dxgi_format)(VkFormat format);526527/* 1.2 */528typedef HRESULT (*PFN_vkd3d_serialize_versioned_root_signature)(const D3D12_VERSIONED_ROOT_SIGNATURE_DESC *desc,529ID3DBlob **blob, ID3DBlob **error_blob);530typedef HRESULT (*PFN_vkd3d_create_versioned_root_signature_deserializer)(const void *data, SIZE_T data_size,531REFIID iid, void **deserializer);532533/** Type of vkd3d_set_log_callback(). \since 1.4 */534typedef void (*PFN_vkd3d_set_log_callback)(PFN_vkd3d_log callback);535536/** Type of vkd3d_queue_signal_on_cpu(). \since 1.15 */537typedef HRESULT (*PFN_vkd3d_queue_signal_on_cpu)(ID3D12CommandQueue *queue,538ID3D12Fence *fence, uint64_t value);539540#ifdef __cplusplus541}542#endif /* __cplusplus */543544#endif /* __VKD3D_H */545546547