From dffc36b8545253a752d4b6f34cf5fea4fac8a2c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 23 Aug 2020 18:52:16 +0200 Subject: [PATCH] Get Vulkan going again with libretro. Rendering is the wrong size and crashes on exit, but it's a start. Only tested on Windows. --- Common/GPU/Vulkan/VulkanContext.cpp | 4 ++ libretro/LibretroVulkanContext.cpp | 65 ++++++++++++++++------------- libretro/LibretroVulkanContext.h | 3 ++ libretro/Makefile | 8 ++++ libretro/README_WINDOWS.txt | 10 ++++- libretro/libretro_vulkan.cpp | 33 ++++++++++----- 6 files changed, 81 insertions(+), 42 deletions(-) diff --git a/Common/GPU/Vulkan/VulkanContext.cpp b/Common/GPU/Vulkan/VulkanContext.cpp index 0a572cf8ac..10a98517ad 100644 --- a/Common/GPU/Vulkan/VulkanContext.cpp +++ b/Common/GPU/Vulkan/VulkanContext.cpp @@ -146,10 +146,14 @@ VkResult VulkanContext::CreateInstance(const CreateInfo &info) { } } + // Temporary hack for libretro. For some reason, when we try to load the functions from this extension, + // we get null pointers when running libretro. Quite strange. +#if !defined(__LIBRETRO__) if (IsInstanceExtensionAvailable(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME)) { instance_extensions_enabled_.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME); extensionsLookup_.KHR_get_physical_device_properties2 = true; } +#endif // Validate that all the instance extensions we ask for are actually available. for (auto ext : instance_extensions_enabled_) { diff --git a/libretro/LibretroVulkanContext.cpp b/libretro/LibretroVulkanContext.cpp index 5d62a54b45..2b9e944360 100644 --- a/libretro/LibretroVulkanContext.cpp +++ b/libretro/LibretroVulkanContext.cpp @@ -31,16 +31,15 @@ static bool create_device(retro_vulkan_context *context, VkInstance instance, Vk init_glslang(); if (!VulkanLoad()) { - // TODO: In the context of RetroArch, someone has already loaded the functions - - // we shouldn't need to load them again. On the other hand, it can't really hurt, we're gonna - // get the same pointers. + // TODO: In the context of RetroArch, someone has already loaded the functions. + // But grabbing the pointers for ourselves can't really be a bad thing. ERROR_LOG(G3D, "RetroArch called the Vulkan entry point without Vulkan available???"); return false; } vk = new VulkanContext(); - vk_libretro_init(instance, gpu, surface, get_instance_proc_addr, required_device_extensions, num_required_device_extensions, required_device_layers, num_required_device_layers, required_features); + vk_libretro_init(instance, gpu, surface, get_instance_proc_addr, required_device_extensions, num_required_device_extensions, required_device_layers, num_required_device_layers, required_features); // TODO: Here we'll inject the instance and all of the stuff into the VulkanContext. @@ -69,10 +68,6 @@ static bool create_device(retro_vulkan_context *context, VkInstance instance, Vk vk->InitSurface(WINDOWSYSTEM_WAYLAND, nullptr, nullptr); #endif - if (!vk->InitSwapchain()) { - return false; - } - context->gpu = vk->GetPhysicalDevice(physical_device); context->device = vk->GetDevice(); context->queue = vk->GetGraphicsQueue(); @@ -100,13 +95,44 @@ bool LibretroVulkanContext::Init() { return false; } - static const struct retro_hw_render_context_negotiation_interface_vulkan iface = { RETRO_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE_VULKAN, RETRO_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE_VULKAN_VERSION, GetApplicationInfo, create_device, nullptr }; + static const struct retro_hw_render_context_negotiation_interface_vulkan iface = { + RETRO_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE_VULKAN, + RETRO_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE_VULKAN_VERSION, + GetApplicationInfo, + create_device, // Callback above. + nullptr, + }; Libretro::environ_cb(RETRO_ENVIRONMENT_SET_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE, (void *)&iface); g_Config.iGPUBackend = (int)GPUBackend::VULKAN; return true; } +void LibretroVulkanContext::ContextReset() { + retro_hw_render_interface *vulkan; + if (!Libretro::environ_cb(RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE, (void **)&vulkan) || !vulkan) { + ERROR_LOG(G3D, "Failed to get HW rendering interface!\n"); + return; + } + if (vulkan->interface_version != RETRO_HW_RENDER_INTERFACE_VULKAN_VERSION) { + ERROR_LOG(G3D, "HW render interface mismatch, expected %u, got %u!\n", RETRO_HW_RENDER_INTERFACE_VULKAN_VERSION, vulkan->interface_version); + return; + } + vk_libretro_set_hwrender_interface(vulkan); + + LibretroHWRenderContext::ContextReset(); +} + +void LibretroVulkanContext::CreateDrawContext() { + vk->ReinitSurface(); + + if (!vk->InitSwapchain()) { + return; + } + + draw_ = Draw::T3DCreateVulkanContext(vk, false); +} + void LibretroVulkanContext::Shutdown() { LibretroHWRenderContext::Shutdown(); @@ -128,24 +154,3 @@ void LibretroVulkanContext::Shutdown() { } void *LibretroVulkanContext::GetAPIContext() { return vk; } - -void LibretroVulkanContext::CreateDrawContext() { - retro_hw_render_interface *vulkan; - if (!Libretro::environ_cb(RETRO_ENVIRONMENT_GET_HW_RENDER_INTERFACE, (void **)&vulkan) || !vulkan) { - ERROR_LOG(G3D, "Failed to get HW rendering interface!\n"); - return; - } - if (vulkan->interface_version != RETRO_HW_RENDER_INTERFACE_VULKAN_VERSION) { - ERROR_LOG(G3D, "HW render interface mismatch, expected %u, got %u!\n", RETRO_HW_RENDER_INTERFACE_VULKAN_VERSION, vulkan->interface_version); - return; - } - vk_libretro_set_hwrender_interface(vulkan); - - vk->ReinitSurface(); - - if (!vk->InitSwapchain()) { - return; - } - - draw_ = Draw::T3DCreateVulkanContext(vk, false); -} diff --git a/libretro/LibretroVulkanContext.h b/libretro/LibretroVulkanContext.h index e7386a43d0..1da32aec6c 100644 --- a/libretro/LibretroVulkanContext.h +++ b/libretro/LibretroVulkanContext.h @@ -12,6 +12,9 @@ public: void *GetAPIContext() override; void CreateDrawContext() override; + + void ContextReset() override; + GPUCore GetGPUCore() override { return GPUCORE_VULKAN; } const char *Ident() override { return "Vulkan"; } }; diff --git a/libretro/Makefile b/libretro/Makefile index 8b2d15fa49..a047d9a2e8 100644 --- a/libretro/Makefile +++ b/libretro/Makefile @@ -284,6 +284,10 @@ else ifneq (,$(findstring windows_msvc2017,$(platform))) LDFLAGS += -APPCONTAINER -NXCOMPAT -DYNAMICBASE -MANIFEST:NO -OPT:REF -SUBSYSTEM:CONSOLE -MANIFESTUAC:NO -OPT:ICF -ERRORREPORT:PROMPT -NOLOGO -TLBID:1 -DEBUG:FULL -WINMD:NO LIBS += WindowsApp.lib endif + + ifeq ($(DEBUG), 1) + MSVC2017CompileFlags += -DEBUG + endif CFLAGS += $(MSVC2017CompileFlags) -nologo CXXFLAGS += $(MSVC2017CompileFlags) -nologo -EHsc @@ -392,6 +396,10 @@ else ifneq (,$(findstring windows_msvc2019,$(platform))) LIBS += WindowsApp.lib endif + ifeq ($(DEBUG), 1) + MSVC2019CompileFlags += -DEBUG + endif + CFLAGS += $(MSVC2019CompileFlags) -nologo CXXFLAGS += $(MSVC2019CompileFlags) -nologo -EHsc diff --git a/libretro/README_WINDOWS.txt b/libretro/README_WINDOWS.txt index 5f95e29844..755658dca4 100644 --- a/libretro/README_WINDOWS.txt +++ b/libretro/README_WINDOWS.txt @@ -13,8 +13,7 @@ pacman -S make Then use the following in msys: cd libretro - -make platform=windows_msvc2019_desktop_x64 -j32 && cp ppsspp_libretro.* /d/retroarch/cores +make platform=windows_msvc2019_desktop_x64 -j32 && cp ppsspp_libretro.* /d/retroarch/cores && rm nul Note that the latter part copies the DLL/PDB into wherever retroarch reads it from. Might need to adjust the path, and adjust -j32 depending on your number of logical CPUs - might not need that many threads (or you might need more...). @@ -24,6 +23,13 @@ and adjust -j32 depending on your number of logical CPUs - might not need that m To debug from within MSVC, open retroarch.exe (or retroarch_debug.exe) as a Project/Solution, then open a few of the cpp files, set some breakpoints and just launch using F5. +If you get + +``` +../git-version.cpp(3): error C2374: 'PPSSPP_GIT_VERSION': redefinition; multiple initialization +../git-version.cpp(1): note: see declaration of 'PPSSPP_GIT_VERSION' +``` +just do `rm ../git-version.cpp`. Useful libretro/vulkan sample code: diff --git a/libretro/libretro_vulkan.cpp b/libretro/libretro_vulkan.cpp index 78789d3250..b046368ae4 100644 --- a/libretro/libretro_vulkan.cpp +++ b/libretro/libretro_vulkan.cpp @@ -1,3 +1,10 @@ +// 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 #include #include @@ -5,6 +12,8 @@ #include #include +#include "Common/Log.h" + #define VK_NO_PROTOTYPES #include "libretro/libretro_vulkan.h" @@ -49,6 +58,7 @@ struct VkSwapchainKHR_T { static VkSwapchainKHR_T chain; #define LIBRETRO_VK_WARP_LIST() \ + LIBRETRO_VK_WARP_FUNC(vkCreateInstance); \ LIBRETRO_VK_WARP_FUNC(vkDestroyInstance); \ LIBRETRO_VK_WARP_FUNC(vkCreateDevice); \ LIBRETRO_VK_WARP_FUNC(vkDestroyDevice); \ @@ -332,13 +342,11 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateRenderPass_libretro(VkDevice device, cons } #undef LIBRETRO_VK_WARP_FUNC -#define LIBRETRO_VK_WARP_FUNC(x) \ - do { \ - if (!strcmp(pName, #x)) { \ - x##_org = (PFN_##x)fptr; \ - return (PFN_vkVoidFunction)x##_libretro; \ - } \ - } while (0) +#define LIBRETRO_VK_WARP_FUNC(x) \ + if (!strcmp(pName, #x)) { \ + x##_org = (PFN_##x)fptr; \ + return (PFN_vkVoidFunction)x##_libretro; \ + } VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr_libretro(VkInstance instance, const char *pName) { if (false @@ -362,8 +370,10 @@ VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr_libretro(VkInstan } PFN_vkVoidFunction fptr = vkGetInstanceProcAddr_org(instance, pName); - if (!fptr) - return fptr; + if (!fptr) { + ERROR_LOG(G3D, "Failed to load VK instance function: %s", pName); + return fptr; + } LIBRETRO_VK_WARP_LIST(); @@ -400,7 +410,10 @@ void vk_libretro_init(VkInstance instance, VkPhysicalDevice gpu, VkSurfaceKHR su vkCreateInstance = vkCreateInstance_libretro; } -void vk_libretro_set_hwrender_interface(retro_hw_render_interface *hw_render_interface) { vulkan = (retro_hw_render_interface_vulkan *)hw_render_interface; } +void vk_libretro_set_hwrender_interface(retro_hw_render_interface *hw_render_interface) { + vulkan = (retro_hw_render_interface_vulkan *)hw_render_interface; +} + void vk_libretro_shutdown() { memset(&vk_init_info, 0x00, sizeof(vk_init_info)); vulkan = NULL;