Merge pull request #13328 from hrydgard/libretro-fixes

Fix libretro with Vulkan
This commit is contained in:
Henrik Rydgård authored and GitHub committed 2020-10-14 22:08:11 +02:00
commit 6cc8c2ebc1
10 files changed
+148 -66

No files matched your search

+4
View File
@@ -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_) {
+1
View File
@@ -17,6 +17,7 @@
#include <algorithm>
#include <cstring>
#include <cfloat>
#include <d3d11.h>
+1
View File
@@ -1704,6 +1704,7 @@
</ItemGroup>
<ItemGroup>
<Text Include="..\libretro\CMakeLists.txt" />
<Text Include="..\libretro\README_WINDOWS.txt" />
<Text Include="..\ppge_atlasscript.txt" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+3
View File
@@ -729,5 +729,8 @@
<Text Include="..\libretro\CMakeLists.txt">
<Filter>Other Platforms\libretro</Filter>
</Text>
<Text Include="..\libretro\README_WINDOWS.txt">
<Filter>Other Platforms\libretro</Filter>
</Text>
</ItemGroup>
</Project>
+35 -30
View File
@@ -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);
}
+3
View File
@@ -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"; }
};
+8
View File
@@ -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
+37
View File
@@ -0,0 +1,37 @@
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 && 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...).
(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.
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:
https://github.com/libretro/libretro-samples/blob/master/video/vulkan/vk_rendering/libretro-test.c
+4 -5
View File
@@ -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; }
+52 -31
View File
@@ -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 <cstring>
#include <cassert>
@@ -5,6 +12,9 @@
#include <mutex>
#include <condition_variable>
#include "Common/Log.h"
#include "Core/Config.h"
#define VK_NO_PROTOTYPES
#include "libretro/libretro_vulkan.h"
@@ -48,24 +58,25 @@ 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(vkCreateInstance); \
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);
@@ -124,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;
}
@@ -332,13 +350,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 +378,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,9 +418,12 @@ 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;
memset(&vk_init_info, 0, sizeof(vk_init_info));
vulkan = nullptr;
DEDICATED_ALLOCATION = false;
}