From ced83b47850dd44f75294c5439313fc6d54fb887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 23 Aug 2020 18:20:58 +0200 Subject: [PATCH 1/3] Libretro/VK: Add a README for debugging, so I don't forget. Minor cleanups. --- GPU/D3D11/TextureCacheD3D11.cpp | 1 + libretro/README_WINDOWS.txt | 31 ++++++++++++++++++++++++++++++ libretro/libretro_vulkan.cpp | 34 ++++++++++++++++----------------- 3 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 libretro/README_WINDOWS.txt diff --git a/GPU/D3D11/TextureCacheD3D11.cpp b/GPU/D3D11/TextureCacheD3D11.cpp index 5dd5e50bf4..c04b353aa7 100644 --- a/GPU/D3D11/TextureCacheD3D11.cpp +++ b/GPU/D3D11/TextureCacheD3D11.cpp @@ -17,6 +17,7 @@ #include #include +#include #include diff --git a/libretro/README_WINDOWS.txt b/libretro/README_WINDOWS.txt new file mode 100644 index 0000000000..5f95e29844 --- /dev/null +++ b/libretro/README_WINDOWS.txt @@ -0,0 +1,31 @@ +Install msys. + +Follow the guide here to set up your MSYS installation for retroarch compilation: + +https://docs.libretro.com/development/retroarch/compilation/windows/ + +You can probably really skip most of the packages but you need make: + +pacman -S make + +"Install" the plugin in Retroarch. + +Then use the following in msys: + +cd libretro + +make platform=windows_msvc2019_desktop_x64 -j32 && cp ppsspp_libretro.* /d/retroarch/cores + +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...). + +(plain make without a platform parameter doesn't work - g++ isn't able to build the D3D11 stuff, or at least it fails to link). + +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. + + + +Useful libretro/vulkan sample code: + +https://github.com/libretro/libretro-samples/blob/master/video/vulkan/vk_rendering/libretro-test.c diff --git a/libretro/libretro_vulkan.cpp b/libretro/libretro_vulkan.cpp index 09e08703fe..78789d3250 100644 --- a/libretro/libretro_vulkan.cpp +++ b/libretro/libretro_vulkan.cpp @@ -48,24 +48,24 @@ struct VkSwapchainKHR_T { }; static VkSwapchainKHR_T chain; -#define LIBRETRO_VK_WARP_LIST() \ - 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) +#define LIBRETRO_VK_WARP_LIST() \ + 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); -#define LIBRETRO_VK_WARP_FUNC(x) \ - extern PFN_##x x; \ +#define LIBRETRO_VK_WARP_FUNC(x) \ + extern PFN_##x x; \ PFN_##x x##_org LIBRETRO_VK_WARP_FUNC(vkGetInstanceProcAddr); 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 2/3] 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; From 61567796a2f38ee10501421bb80ee15551c631ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 11 Oct 2020 20:26:39 +0200 Subject: [PATCH 3/3] libretro: Fix screen size problem in Vulkan. (Still something is slightly off, there are duplicated lines on screen. Can't figure out what) --- Windows/PPSSPP.vcxproj | 1 + Windows/PPSSPP.vcxproj.filters | 3 +++ libretro/libretro.cpp | 9 ++++----- libretro/libretro_vulkan.cpp | 16 ++++++++++++---- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Windows/PPSSPP.vcxproj b/Windows/PPSSPP.vcxproj index 55a21f5a1d..bef161003e 100644 --- a/Windows/PPSSPP.vcxproj +++ b/Windows/PPSSPP.vcxproj @@ -1704,6 +1704,7 @@ + diff --git a/Windows/PPSSPP.vcxproj.filters b/Windows/PPSSPP.vcxproj.filters index ffbdc51429..141d03a3ca 100644 --- a/Windows/PPSSPP.vcxproj.filters +++ b/Windows/PPSSPP.vcxproj.filters @@ -729,5 +729,8 @@ Other Platforms\libretro + + Other Platforms\libretro + \ No newline at end of file diff --git a/libretro/libretro.cpp b/libretro/libretro.cpp index bdb5f8d1b1..52ff150474 100644 --- a/libretro/libretro.cpp +++ b/libretro/libretro.cpp @@ -34,6 +34,7 @@ #include "GPU/GPUInterface.h" #include "GPU/Common/FramebufferManagerCommon.h" #include "GPU/Common/TextureScalerCommon.h" +#include "GPU/Common/PresentationCommon.h" #include "libretro/libretro.h" #include "libretro/LibretroGraphicsContext.h" @@ -318,10 +319,8 @@ static void check_variables(CoreParameter &coreParam) if (!PSP_IsInited() && ppsspp_internal_resolution.Update(&g_Config.iInternalResolution)) { - coreParam.pixelWidth = coreParam.renderWidth = - g_Config.iInternalResolution * 480; - coreParam.pixelHeight = coreParam.renderHeight = - g_Config.iInternalResolution * 272; + coreParam.pixelWidth = coreParam.renderWidth = g_Config.iInternalResolution * 480; + coreParam.pixelHeight = coreParam.renderHeight = g_Config.iInternalResolution * 272; if (gpu) { @@ -421,7 +420,7 @@ void retro_get_system_av_info(struct retro_system_av_info *info) info->geometry.base_height = g_Config.iInternalResolution * 272; info->geometry.max_width = g_Config.iInternalResolution * 480; info->geometry.max_height = g_Config.iInternalResolution * 272; - info->geometry.aspect_ratio = 16.0 / 9.0; + info->geometry.aspect_ratio = 480.0 / 272.0; // Not 16:9! But very, very close. } unsigned retro_api_version(void) { return RETRO_API_VERSION; } diff --git a/libretro/libretro_vulkan.cpp b/libretro/libretro_vulkan.cpp index b046368ae4..1081f95e0b 100644 --- a/libretro/libretro_vulkan.cpp +++ b/libretro/libretro_vulkan.cpp @@ -13,6 +13,7 @@ #include #include "Common/Log.h" +#include "Core/Config.h" #define VK_NO_PROTOTYPES #include "libretro/libretro_vulkan.h" @@ -134,8 +135,15 @@ static VKAPI_ATTR VkResult VKAPI_CALL vkCreateLibretroSurfaceKHR(VkInstance inst VKAPI_ATTR VkResult VKAPI_CALL vkGetPhysicalDeviceSurfaceCapabilitiesKHR_libretro(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface, VkSurfaceCapabilitiesKHR *pSurfaceCapabilities) { VkResult res = vkGetPhysicalDeviceSurfaceCapabilitiesKHR_org(physicalDevice, surface, pSurfaceCapabilities); if (res == VK_SUCCESS) { - pSurfaceCapabilities->currentExtent.width = -1; - pSurfaceCapabilities->currentExtent.height = -1; + 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; } return res; } @@ -415,7 +423,7 @@ void vk_libretro_set_hwrender_interface(retro_hw_render_interface *hw_render_int } void vk_libretro_shutdown() { - memset(&vk_init_info, 0x00, sizeof(vk_init_info)); - vulkan = NULL; + memset(&vk_init_info, 0, sizeof(vk_init_info)); + vulkan = nullptr; DEDICATED_ALLOCATION = false; }