2020-08-23 16:52:16 +00:00
|
|
|
// Debugging notes
|
|
|
|
// The crash happens when we try to call vkGetPhysicalDeviceProperties2KHR which seems to be null.
|
|
|
|
//
|
|
|
|
// Apparently we don't manage to specify the extensions we want. Still something reports that this one
|
|
|
|
// is present?
|
|
|
|
// Failed to load : vkGetPhysicalDeviceProperties2KHR
|
|
|
|
// Failed to load : vkGetPhysicalDeviceFeatures2KHR
|
2018-03-24 11:44:53 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <cassert>
|
|
|
|
#include <vector>
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
|
|
|
|
2021-08-20 09:22:57 +00:00
|
|
|
#include "Common/GPU/Vulkan/VulkanLoader.h"
|
2020-08-23 16:52:16 +00:00
|
|
|
#include "Common/Log.h"
|
2020-10-11 18:26:39 +00:00
|
|
|
#include "Core/Config.h"
|
2020-08-23 16:52:16 +00:00
|
|
|
|
2018-03-24 11:44:53 +00:00
|
|
|
#define VK_NO_PROTOTYPES
|
|
|
|
#include "libretro/libretro_vulkan.h"
|
|
|
|
|
2021-08-21 10:39:15 +00:00
|
|
|
using namespace PPSSPP_VK;
|
2021-08-20 09:22:57 +00:00
|
|
|
|
2018-03-24 11:44:53 +00:00
|
|
|
static retro_hw_render_interface_vulkan *vulkan;
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
static struct {
|
2018-03-24 11:44:53 +00:00
|
|
|
VkInstance instance;
|
|
|
|
VkPhysicalDevice gpu;
|
|
|
|
VkSurfaceKHR surface;
|
|
|
|
PFN_vkGetInstanceProcAddr get_instance_proc_addr;
|
|
|
|
const char **required_device_extensions;
|
|
|
|
unsigned num_required_device_extensions;
|
|
|
|
const char **required_device_layers;
|
|
|
|
unsigned num_required_device_layers;
|
|
|
|
const VkPhysicalDeviceFeatures *required_features;
|
|
|
|
} vk_init_info;
|
|
|
|
static bool DEDICATED_ALLOCATION;
|
|
|
|
|
|
|
|
#define VULKAN_MAX_SWAPCHAIN_IMAGES 8
|
2018-03-26 16:37:34 +00:00
|
|
|
struct VkSwapchainKHR_T {
|
2018-03-24 11:44:53 +00:00
|
|
|
uint32_t count;
|
2018-03-26 16:37:34 +00:00
|
|
|
struct {
|
2018-03-24 11:44:53 +00:00
|
|
|
VkImage handle;
|
|
|
|
VkDeviceMemory memory;
|
|
|
|
retro_vulkan_image retro_image;
|
|
|
|
} images[VULKAN_MAX_SWAPCHAIN_IMAGES];
|
|
|
|
std::mutex mutex;
|
|
|
|
std::condition_variable condVar;
|
|
|
|
int current_index;
|
|
|
|
};
|
|
|
|
static VkSwapchainKHR_T chain;
|
|
|
|
|
2020-08-23 16:20:58 +00:00
|
|
|
#define LIBRETRO_VK_WARP_LIST() \
|
2020-08-23 16:52:16 +00:00
|
|
|
LIBRETRO_VK_WARP_FUNC(vkCreateInstance); \
|
2020-08-23 16:20:58 +00:00
|
|
|
LIBRETRO_VK_WARP_FUNC(vkDestroyInstance); \
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkCreateDevice); \
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkDestroyDevice); \
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkGetPhysicalDeviceSurfaceCapabilitiesKHR); \
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkDestroySurfaceKHR); \
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkCreateSwapchainKHR); \
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkGetSwapchainImagesKHR); \
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkAcquireNextImageKHR); \
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkQueuePresentKHR); \
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkDestroySwapchainKHR); \
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkQueueSubmit); \
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkQueueWaitIdle); \
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkCmdPipelineBarrier); \
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkCreateRenderPass);
|
2018-03-24 11:44:53 +00:00
|
|
|
|
2020-08-23 16:20:58 +00:00
|
|
|
#define LIBRETRO_VK_WARP_FUNC(x) \
|
2018-03-24 11:44:53 +00:00
|
|
|
PFN_##x x##_org
|
|
|
|
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkGetInstanceProcAddr);
|
|
|
|
LIBRETRO_VK_WARP_FUNC(vkGetDeviceProcAddr);
|
|
|
|
LIBRETRO_VK_WARP_LIST();
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
static VKAPI_ATTR VkResult VKAPI_CALL vkCreateInstance_libretro(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) {
|
2018-03-24 11:44:53 +00:00
|
|
|
*pInstance = vk_init_info.instance;
|
|
|
|
return VK_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
static void add_name_unique(std::vector<const char *> &list, const char *value) {
|
2018-03-24 11:44:53 +00:00
|
|
|
for (const char *name : list)
|
|
|
|
if (!strcmp(value, name))
|
|
|
|
return;
|
|
|
|
|
|
|
|
list.push_back(value);
|
|
|
|
}
|
2018-03-26 16:37:34 +00:00
|
|
|
static VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice_libretro(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) {
|
2018-03-24 11:44:53 +00:00
|
|
|
VkDeviceCreateInfo info = *pCreateInfo;
|
|
|
|
std::vector<const char *> EnabledLayerNames(info.ppEnabledLayerNames, info.ppEnabledLayerNames + info.enabledLayerCount);
|
|
|
|
std::vector<const char *> EnabledExtensionNames(info.ppEnabledExtensionNames, info.ppEnabledExtensionNames + info.enabledExtensionCount);
|
|
|
|
VkPhysicalDeviceFeatures EnabledFeatures = *info.pEnabledFeatures;
|
|
|
|
|
2018-03-24 21:47:12 +00:00
|
|
|
for (unsigned i = 0; i < vk_init_info.num_required_device_layers; i++)
|
2018-03-24 11:44:53 +00:00
|
|
|
add_name_unique(EnabledLayerNames, vk_init_info.required_device_layers[i]);
|
|
|
|
|
2018-03-24 21:47:12 +00:00
|
|
|
for (unsigned i = 0; i < vk_init_info.num_required_device_extensions; i++)
|
2018-03-24 11:44:53 +00:00
|
|
|
add_name_unique(EnabledExtensionNames, vk_init_info.required_device_extensions[i]);
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
for (unsigned i = 0; i < sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32); i++) {
|
2018-03-24 11:44:53 +00:00
|
|
|
if (((VkBool32 *)vk_init_info.required_features)[i])
|
|
|
|
((VkBool32 *)&EnabledFeatures)[i] = VK_TRUE;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
for (auto extension_name : EnabledExtensionNames) {
|
2018-03-24 11:44:53 +00:00
|
|
|
if (!strcmp(extension_name, VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME))
|
|
|
|
DEDICATED_ALLOCATION = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.enabledLayerCount = (uint32_t)EnabledLayerNames.size();
|
|
|
|
info.ppEnabledLayerNames = info.enabledLayerCount ? EnabledLayerNames.data() : nullptr;
|
|
|
|
info.enabledExtensionCount = (uint32_t)EnabledExtensionNames.size();
|
|
|
|
info.ppEnabledExtensionNames = info.enabledExtensionCount ? EnabledExtensionNames.data() : nullptr;
|
|
|
|
info.pEnabledFeatures = &EnabledFeatures;
|
|
|
|
|
|
|
|
return vkCreateDevice_org(physicalDevice, &info, pAllocator, pDevice);
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
static VKAPI_ATTR VkResult VKAPI_CALL vkCreateLibretroSurfaceKHR(VkInstance instance, const void *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface) {
|
2018-03-24 11:44:53 +00:00
|
|
|
*pSurface = vk_init_info.surface;
|
|
|
|
return VK_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
VKAPI_ATTR VkResult VKAPI_CALL vkGetPhysicalDeviceSurfaceCapabilitiesKHR_libretro(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface, VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) {
|
2018-03-24 11:44:53 +00:00
|
|
|
VkResult res = vkGetPhysicalDeviceSurfaceCapabilitiesKHR_org(physicalDevice, surface, pSurfaceCapabilities);
|
2018-03-26 16:37:34 +00:00
|
|
|
if (res == VK_SUCCESS) {
|
2020-10-11 18:26:39 +00:00
|
|
|
int w = g_Config.iInternalResolution * 480;
|
|
|
|
int h = g_Config.iInternalResolution * 272;
|
|
|
|
|
|
|
|
pSurfaceCapabilities->minImageExtent.width = w;
|
|
|
|
pSurfaceCapabilities->minImageExtent.height = h;
|
|
|
|
pSurfaceCapabilities->maxImageExtent.width = w;
|
|
|
|
pSurfaceCapabilities->maxImageExtent.height = h;
|
|
|
|
pSurfaceCapabilities->currentExtent.width = w;
|
|
|
|
pSurfaceCapabilities->currentExtent.height = h;
|
2018-03-24 11:44:53 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
static bool MemoryTypeFromProperties(uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) {
|
2018-03-24 11:44:53 +00:00
|
|
|
VkPhysicalDeviceMemoryProperties memory_properties;
|
|
|
|
vkGetPhysicalDeviceMemoryProperties(vulkan->gpu, &memory_properties);
|
|
|
|
// Search memtypes to find first index with those properties
|
2018-03-26 16:37:34 +00:00
|
|
|
for (uint32_t i = 0; i < 32; i++) {
|
|
|
|
if ((typeBits & 1) == 1) {
|
2018-03-24 11:44:53 +00:00
|
|
|
// Type is available, does it match user properties?
|
2018-03-26 16:37:34 +00:00
|
|
|
if ((memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
|
2018-03-24 11:44:53 +00:00
|
|
|
*typeIndex = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
typeBits >>= 1;
|
|
|
|
}
|
|
|
|
// No memory types matched, return failure
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
static VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR_libretro(VkDevice device, const VkSwapchainCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkSwapchainKHR *pSwapchain) {
|
2018-03-24 11:44:53 +00:00
|
|
|
uint32_t swapchain_mask = vulkan->get_sync_index_mask(vulkan->handle);
|
|
|
|
|
|
|
|
chain.count = 0;
|
2018-03-26 16:37:34 +00:00
|
|
|
while (swapchain_mask) {
|
2018-03-24 11:44:53 +00:00
|
|
|
chain.count++;
|
|
|
|
swapchain_mask >>= 1;
|
|
|
|
}
|
|
|
|
assert(chain.count <= VULKAN_MAX_SWAPCHAIN_IMAGES);
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
for (uint32_t i = 0; i < chain.count; i++) {
|
2018-03-24 11:44:53 +00:00
|
|
|
{
|
|
|
|
VkImageCreateInfo info{ VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
|
|
|
|
info.flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
|
|
|
|
info.imageType = VK_IMAGE_TYPE_2D;
|
|
|
|
info.format = pCreateInfo->imageFormat;
|
|
|
|
info.extent.width = pCreateInfo->imageExtent.width;
|
|
|
|
info.extent.height = pCreateInfo->imageExtent.height;
|
|
|
|
info.extent.depth = 1;
|
|
|
|
info.mipLevels = 1;
|
|
|
|
info.arrayLayers = 1;
|
|
|
|
info.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
info.tiling = VK_IMAGE_TILING_OPTIMAL;
|
|
|
|
info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
|
|
|
info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
|
|
|
|
|
|
vkCreateImage(device, &info, pAllocator, &chain.images[i].handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
VkMemoryRequirements memreq;
|
|
|
|
vkGetImageMemoryRequirements(device, chain.images[i].handle, &memreq);
|
|
|
|
|
|
|
|
VkMemoryAllocateInfo alloc{ VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO };
|
|
|
|
alloc.allocationSize = memreq.size;
|
|
|
|
|
|
|
|
VkMemoryDedicatedAllocateInfoKHR dedicated{ VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR };
|
2018-03-26 16:37:34 +00:00
|
|
|
if (DEDICATED_ALLOCATION) {
|
2018-03-24 11:44:53 +00:00
|
|
|
alloc.pNext = &dedicated;
|
|
|
|
dedicated.image = chain.images[i].handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryTypeFromProperties(memreq.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &alloc.memoryTypeIndex);
|
|
|
|
VkResult res = vkAllocateMemory(device, &alloc, pAllocator, &chain.images[i].memory);
|
|
|
|
assert(res == VK_SUCCESS);
|
|
|
|
res = vkBindImageMemory(device, chain.images[i].handle, chain.images[i].memory, 0);
|
|
|
|
assert(res == VK_SUCCESS);
|
|
|
|
|
|
|
|
chain.images[i].retro_image.create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
|
|
|
chain.images[i].retro_image.create_info.image = chain.images[i].handle;
|
|
|
|
chain.images[i].retro_image.create_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
|
|
chain.images[i].retro_image.create_info.format = pCreateInfo->imageFormat;
|
|
|
|
chain.images[i].retro_image.create_info.components = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A };
|
|
|
|
chain.images[i].retro_image.create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
chain.images[i].retro_image.create_info.subresourceRange.layerCount = 1;
|
|
|
|
chain.images[i].retro_image.create_info.subresourceRange.levelCount = 1;
|
|
|
|
res = vkCreateImageView(device, &chain.images[i].retro_image.create_info, pAllocator, &chain.images[i].retro_image.image_view);
|
|
|
|
assert(res == VK_SUCCESS);
|
|
|
|
|
|
|
|
chain.images[i].retro_image.image_layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
chain.current_index = -1;
|
|
|
|
*pSwapchain = (VkSwapchainKHR)&chain;
|
|
|
|
|
|
|
|
return VK_SUCCESS;
|
|
|
|
}
|
2018-03-26 16:37:34 +00:00
|
|
|
static VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainImagesKHR_libretro(VkDevice device, VkSwapchainKHR swapchain_, uint32_t *pSwapchainImageCount, VkImage *pSwapchainImages) {
|
2018-03-24 11:44:53 +00:00
|
|
|
VkSwapchainKHR_T *swapchain = (VkSwapchainKHR_T *)swapchain_;
|
2018-03-26 16:37:34 +00:00
|
|
|
if (pSwapchainImages) {
|
2018-03-24 11:44:53 +00:00
|
|
|
assert(*pSwapchainImageCount <= swapchain->count);
|
|
|
|
for (int i = 0; i < *pSwapchainImageCount; i++)
|
|
|
|
pSwapchainImages[i] = swapchain->images[i].handle;
|
2018-03-26 16:37:34 +00:00
|
|
|
} else
|
2018-03-24 11:44:53 +00:00
|
|
|
*pSwapchainImageCount = swapchain->count;
|
|
|
|
|
|
|
|
return VK_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
static VKAPI_ATTR VkResult VKAPI_CALL vkAcquireNextImageKHR_libretro(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout, VkSemaphore semaphore, VkFence fence, uint32_t *pImageIndex) {
|
2018-03-24 11:44:53 +00:00
|
|
|
vulkan->wait_sync_index(vulkan->handle);
|
|
|
|
*pImageIndex = vulkan->get_sync_index(vulkan->handle);
|
|
|
|
#if 0
|
|
|
|
vulkan->set_signal_semaphore(vulkan->handle, semaphore);
|
|
|
|
#endif
|
|
|
|
return VK_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
static VKAPI_ATTR VkResult VKAPI_CALL vkQueuePresentKHR_libretro(VkQueue queue, const VkPresentInfoKHR *pPresentInfo) {
|
2018-03-24 11:44:53 +00:00
|
|
|
VkSwapchainKHR_T *swapchain = (VkSwapchainKHR_T *)pPresentInfo->pSwapchains[0];
|
|
|
|
std::unique_lock<std::mutex> lock(swapchain->mutex);
|
|
|
|
#if 0
|
|
|
|
if(chain.current_index >= 0)
|
|
|
|
chain.condVar.wait(lock);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
chain.current_index = pPresentInfo->pImageIndices[0];
|
|
|
|
#if 0
|
|
|
|
vulkan->set_image(vulkan->handle, &swapchain->images[pPresentInfo->pImageIndices[0]].retro_image, pPresentInfo->waitSemaphoreCount, pPresentInfo->pWaitSemaphores, vulkan->queue_index);
|
|
|
|
#else
|
|
|
|
vulkan->set_image(vulkan->handle, &swapchain->images[pPresentInfo->pImageIndices[0]].retro_image, 0, nullptr, vulkan->queue_index);
|
|
|
|
#endif
|
|
|
|
swapchain->condVar.notify_all();
|
|
|
|
|
|
|
|
return VK_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
void vk_libretro_wait_for_presentation() {
|
2018-03-24 11:44:53 +00:00
|
|
|
std::unique_lock<std::mutex> lock(chain.mutex);
|
|
|
|
if (chain.current_index < 0)
|
|
|
|
chain.condVar.wait(lock);
|
|
|
|
#if 0
|
|
|
|
chain.current_index = -1;
|
|
|
|
chain.condVar.notify_all();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static VKAPI_ATTR void VKAPI_CALL vkDestroyInstance_libretro(VkInstance instance, const VkAllocationCallbacks *pAllocator) {}
|
|
|
|
static VKAPI_ATTR void VKAPI_CALL vkDestroyDevice_libretro(VkDevice device, const VkAllocationCallbacks *pAllocator) {}
|
|
|
|
static VKAPI_ATTR void VKAPI_CALL vkDestroySurfaceKHR_libretro(VkInstance instance, VkSurfaceKHR surface, const VkAllocationCallbacks *pAllocator) {}
|
2018-03-26 16:37:34 +00:00
|
|
|
static VKAPI_ATTR void VKAPI_CALL vkDestroySwapchainKHR_libretro(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks *pAllocator) {
|
|
|
|
for (int i = 0; i < chain.count; i++) {
|
2018-03-24 11:44:53 +00:00
|
|
|
vkDestroyImage(device, chain.images[i].handle, pAllocator);
|
|
|
|
vkDestroyImageView(device, chain.images[i].retro_image.image_view, pAllocator);
|
|
|
|
vkFreeMemory(device, chain.images[i].memory, pAllocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&chain.images, 0x00, sizeof(chain.images));
|
|
|
|
chain.count = 0;
|
|
|
|
chain.current_index = -1;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
VKAPI_ATTR VkResult VKAPI_CALL vkQueueSubmit_libretro(VkQueue queue, uint32_t submitCount, const VkSubmitInfo *pSubmits, VkFence fence) {
|
2018-03-24 11:44:53 +00:00
|
|
|
VkResult res = VK_SUCCESS;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
for(int i = 0; i < submitCount; i++)
|
|
|
|
vulkan->set_command_buffers(vulkan->handle, pSubmits[i].commandBufferCount, pSubmits[i].pCommandBuffers);
|
|
|
|
#else
|
|
|
|
#if 1
|
2018-03-26 16:37:34 +00:00
|
|
|
for (int i = 0; i < submitCount; i++) {
|
2018-03-24 11:44:53 +00:00
|
|
|
((VkSubmitInfo *)pSubmits)[i].waitSemaphoreCount = 0;
|
|
|
|
((VkSubmitInfo *)pSubmits)[i].pWaitSemaphores = nullptr;
|
|
|
|
((VkSubmitInfo *)pSubmits)[i].signalSemaphoreCount = 0;
|
|
|
|
((VkSubmitInfo *)pSubmits)[i].pSignalSemaphores = nullptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
vulkan->lock_queue(vulkan->handle);
|
|
|
|
res = vkQueueSubmit_org(queue, submitCount, pSubmits, fence);
|
|
|
|
vulkan->unlock_queue(vulkan->handle);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
VKAPI_ATTR VkResult VKAPI_CALL vkQueueWaitIdle_libretro(VkQueue queue) {
|
2018-03-24 11:44:53 +00:00
|
|
|
vulkan->lock_queue(vulkan->handle);
|
|
|
|
VkResult res = vkQueueWaitIdle_org(queue);
|
|
|
|
vulkan->unlock_queue(vulkan->handle);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
VKAPI_ATTR void VKAPI_CALL vkCmdPipelineBarrier_libretro(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers) {
|
2018-03-24 11:44:53 +00:00
|
|
|
VkImageMemoryBarrier *barriers = (VkImageMemoryBarrier *)pImageMemoryBarriers;
|
2018-03-26 16:37:34 +00:00
|
|
|
for (int i = 0; i < imageMemoryBarrierCount; i++) {
|
|
|
|
if (pImageMemoryBarriers[i].oldLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) {
|
2018-03-24 11:44:53 +00:00
|
|
|
barriers[i].oldLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
|
|
barriers[i].srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
|
|
}
|
2018-03-26 16:37:34 +00:00
|
|
|
if (pImageMemoryBarriers[i].newLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) {
|
2018-03-24 11:44:53 +00:00
|
|
|
barriers[i].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
|
|
barriers[i].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return vkCmdPipelineBarrier_org(commandBuffer, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, barriers);
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
VKAPI_ATTR VkResult VKAPI_CALL vkCreateRenderPass_libretro(VkDevice device, const VkRenderPassCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkRenderPass *pRenderPass) {
|
2018-03-24 11:44:53 +00:00
|
|
|
if (pCreateInfo->pAttachments[0].finalLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR)
|
|
|
|
((VkAttachmentDescription *)pCreateInfo->pAttachments)[0].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
|
|
|
|
|
|
return vkCreateRenderPass_org(device, pCreateInfo, pAllocator, pRenderPass);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef LIBRETRO_VK_WARP_FUNC
|
2020-08-23 16:52:16 +00:00
|
|
|
#define LIBRETRO_VK_WARP_FUNC(x) \
|
|
|
|
if (!strcmp(pName, #x)) { \
|
|
|
|
x##_org = (PFN_##x)fptr; \
|
|
|
|
return (PFN_vkVoidFunction)x##_libretro; \
|
|
|
|
}
|
2018-03-24 11:44:53 +00:00
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr_libretro(VkInstance instance, const char *pName) {
|
2018-03-24 11:44:53 +00:00
|
|
|
if (false
|
|
|
|
#ifdef _WIN32
|
|
|
|
|| !strcmp(pName, "vkCreateWin32SurfaceKHR")
|
|
|
|
#endif
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
|| !strcmp(pName, "vkCreateAndroidSurfaceKHR")
|
|
|
|
#endif
|
2023-01-31 20:13:24 +00:00
|
|
|
#ifdef VK_USE_PLATFORM_METAL_EXT
|
|
|
|
|| !strcmp(pName, "vkCreateMetalSurfaceEXT")
|
|
|
|
#endif
|
2018-03-24 11:44:53 +00:00
|
|
|
#ifdef VK_USE_PLATFORM_XLIB_KHR
|
|
|
|
|| !strcmp(pName, "vkCreateXlibSurfaceKHR")
|
|
|
|
#endif
|
|
|
|
#ifdef VK_USE_PLATFORM_XCB_KHR
|
|
|
|
|| !strcmp(pName, "vkCreateXcbSurfaceKHR")
|
|
|
|
#endif
|
|
|
|
#ifdef VK_USE_PLATFORM_WAYLAND_KHR
|
|
|
|
|| !strcmp(pName, "vkCreateWaylandSurfaceKHR")
|
2020-12-01 01:31:14 +00:00
|
|
|
#endif
|
|
|
|
#ifdef VK_USE_PLATFORM_DISPLAY_KHR
|
|
|
|
|| !strcmp(pName, "vkCreateDisplayPlaneSurfaceKHR")
|
2018-03-24 11:44:53 +00:00
|
|
|
#endif
|
2018-03-26 16:37:34 +00:00
|
|
|
) {
|
2018-03-24 11:44:53 +00:00
|
|
|
return (PFN_vkVoidFunction)vkCreateLibretroSurfaceKHR;
|
|
|
|
}
|
|
|
|
|
|
|
|
PFN_vkVoidFunction fptr = vkGetInstanceProcAddr_org(instance, pName);
|
2020-08-23 16:52:16 +00:00
|
|
|
if (!fptr) {
|
|
|
|
ERROR_LOG(G3D, "Failed to load VK instance function: %s", pName);
|
|
|
|
return fptr;
|
|
|
|
}
|
2018-03-24 11:44:53 +00:00
|
|
|
|
|
|
|
LIBRETRO_VK_WARP_LIST();
|
|
|
|
|
|
|
|
return fptr;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr_libretro(VkDevice device, const char *pName) {
|
2018-03-24 11:44:53 +00:00
|
|
|
PFN_vkVoidFunction fptr = vkGetDeviceProcAddr_org(device, pName);
|
|
|
|
if (!fptr)
|
|
|
|
return fptr;
|
|
|
|
|
|
|
|
LIBRETRO_VK_WARP_LIST();
|
|
|
|
|
|
|
|
return fptr;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
void vk_libretro_init(VkInstance instance, VkPhysicalDevice gpu, VkSurfaceKHR surface, PFN_vkGetInstanceProcAddr get_instance_proc_addr, const char **required_device_extensions, unsigned num_required_device_extensions, const char **required_device_layers, unsigned num_required_device_layers, const VkPhysicalDeviceFeatures *required_features) {
|
2018-03-24 11:44:53 +00:00
|
|
|
assert(surface);
|
|
|
|
|
|
|
|
vk_init_info.instance = instance;
|
|
|
|
vk_init_info.gpu = gpu;
|
|
|
|
vk_init_info.surface = surface;
|
|
|
|
vk_init_info.get_instance_proc_addr = get_instance_proc_addr;
|
|
|
|
vk_init_info.required_device_extensions = required_device_extensions;
|
|
|
|
vk_init_info.num_required_device_extensions = num_required_device_extensions;
|
|
|
|
vk_init_info.required_device_layers = required_device_layers;
|
|
|
|
vk_init_info.num_required_device_layers = num_required_device_layers;
|
|
|
|
vk_init_info.required_features = required_features;
|
|
|
|
|
|
|
|
vkGetInstanceProcAddr_org = vkGetInstanceProcAddr;
|
|
|
|
vkGetInstanceProcAddr = vkGetInstanceProcAddr_libretro;
|
|
|
|
vkGetDeviceProcAddr_org = vkGetDeviceProcAddr;
|
|
|
|
vkGetDeviceProcAddr = vkGetDeviceProcAddr_libretro;
|
|
|
|
vkCreateInstance = vkCreateInstance_libretro;
|
|
|
|
}
|
|
|
|
|
2020-08-23 16:52:16 +00:00
|
|
|
void vk_libretro_set_hwrender_interface(retro_hw_render_interface *hw_render_interface) {
|
|
|
|
vulkan = (retro_hw_render_interface_vulkan *)hw_render_interface;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:37:34 +00:00
|
|
|
void vk_libretro_shutdown() {
|
2020-10-11 18:26:39 +00:00
|
|
|
memset(&vk_init_info, 0, sizeof(vk_init_info));
|
|
|
|
vulkan = nullptr;
|
2018-03-24 11:44:53 +00:00
|
|
|
DEDICATED_ALLOCATION = false;
|
|
|
|
}
|