2021-03-03 04:42:55 +00:00
|
|
|
#include "ppsspp_config.h"
|
2018-02-04 11:26:35 +00:00
|
|
|
#include "Core/Config.h"
|
2018-06-17 01:42:31 +00:00
|
|
|
#include "Core/ConfigValues.h"
|
2020-10-04 08:30:18 +00:00
|
|
|
#include "Common/System/System.h"
|
|
|
|
#include "Common/System/NativeApp.h"
|
2020-10-04 08:10:55 +00:00
|
|
|
#include "Common/System/Display.h"
|
2020-10-04 21:24:14 +00:00
|
|
|
#include "Common/GPU/thin3d.h"
|
|
|
|
#include "Common/GPU/thin3d_create.h"
|
|
|
|
#include "Common/GPU/Vulkan/VulkanRenderManager.h"
|
2020-10-01 11:05:04 +00:00
|
|
|
#include "Common/Data/Text/Parsers.h"
|
2018-02-04 11:26:35 +00:00
|
|
|
|
|
|
|
#include "Core/System.h"
|
2023-04-24 12:44:07 +00:00
|
|
|
#if PPSSPP_PLATFORM(MAC)
|
|
|
|
#include "SDL2/SDL_vulkan.h"
|
|
|
|
#else
|
2022-10-15 10:49:45 +00:00
|
|
|
#include "SDL_vulkan.h"
|
2023-04-24 12:44:07 +00:00
|
|
|
#endif
|
2018-02-04 11:26:35 +00:00
|
|
|
#include "SDLVulkanGraphicsContext.h"
|
2020-06-23 19:57:02 +00:00
|
|
|
|
2020-01-19 09:34:21 +00:00
|
|
|
#if defined(VK_USE_PLATFORM_METAL_EXT)
|
|
|
|
#include "SDLCocoaMetalLayer.h"
|
|
|
|
#endif
|
2018-02-04 11:26:35 +00:00
|
|
|
|
2020-06-23 19:57:02 +00:00
|
|
|
#ifdef _DEBUG
|
|
|
|
static const bool g_Validate = true;
|
|
|
|
#else
|
|
|
|
static const bool g_Validate = false;
|
|
|
|
#endif
|
|
|
|
|
2023-09-06 16:45:59 +00:00
|
|
|
// TODO: Share this between backends.
|
2020-07-18 18:35:39 +00:00
|
|
|
static uint32_t FlagsFromConfig() {
|
2023-09-06 16:45:59 +00:00
|
|
|
uint32_t flags;
|
|
|
|
if (g_Config.bVSync) {
|
|
|
|
flags = VULKAN_FLAG_PRESENT_FIFO;
|
|
|
|
} else {
|
|
|
|
flags = VULKAN_FLAG_PRESENT_MAILBOX | VULKAN_FLAG_PRESENT_IMMEDIATE;
|
|
|
|
}
|
2020-07-18 18:35:39 +00:00
|
|
|
if (g_Validate) {
|
|
|
|
flags |= VULKAN_FLAG_VALIDATE;
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2023-03-28 12:51:18 +00:00
|
|
|
bool SDLVulkanGraphicsContext::Init(SDL_Window *&window, int x, int y, int w, int h, int mode, std::string *error_message) {
|
|
|
|
window = SDL_CreateWindow("Initializing Vulkan...", x, y, w, h, mode);
|
2018-02-04 11:26:35 +00:00
|
|
|
if (!window) {
|
|
|
|
fprintf(stderr, "Error creating SDL window: %s\n", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
init_glslang();
|
|
|
|
|
|
|
|
g_LogOptions.breakOnError = true;
|
|
|
|
g_LogOptions.breakOnWarning = true;
|
|
|
|
g_LogOptions.msgBoxOnError = false;
|
|
|
|
|
|
|
|
Version gitVer(PPSSPP_GIT_VERSION);
|
|
|
|
|
2024-01-15 10:06:06 +00:00
|
|
|
std::string errorStr;
|
|
|
|
if (!VulkanLoad(&errorStr)) {
|
|
|
|
*error_message = "Failed to load Vulkan driver library: ";
|
|
|
|
(*error_message) += errorStr;
|
2018-02-04 11:26:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-01 20:20:44 +00:00
|
|
|
vulkan_ = new VulkanContext();
|
2020-07-18 18:35:39 +00:00
|
|
|
int vulkanFlags = FlagsFromConfig();
|
2020-06-23 19:57:02 +00:00
|
|
|
|
2018-02-04 11:26:35 +00:00
|
|
|
VulkanContext::CreateInfo info{};
|
|
|
|
info.app_name = "PPSSPP";
|
|
|
|
info.app_ver = gitVer.ToInteger();
|
|
|
|
info.flags = vulkanFlags;
|
|
|
|
if (VK_SUCCESS != vulkan_->CreateInstance(info)) {
|
|
|
|
*error_message = vulkan_->InitError();
|
|
|
|
delete vulkan_;
|
|
|
|
vulkan_ = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-17 20:06:17 +00:00
|
|
|
|
|
|
|
int deviceNum = vulkan_->GetPhysicalDeviceByName(g_Config.sVulkanDevice);
|
|
|
|
if (deviceNum < 0) {
|
|
|
|
deviceNum = vulkan_->GetBestPhysicalDevice();
|
|
|
|
if (!g_Config.sVulkanDevice.empty())
|
|
|
|
g_Config.sVulkanDevice = vulkan_->GetPhysicalDeviceProperties(deviceNum).properties.deviceName;
|
|
|
|
}
|
|
|
|
|
2024-07-17 08:31:49 +00:00
|
|
|
if (vulkan_->CreateDevice(deviceNum) != VK_SUCCESS) {
|
2018-02-04 11:26:35 +00:00
|
|
|
*error_message = vulkan_->InitError();
|
|
|
|
delete vulkan_;
|
|
|
|
vulkan_ = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-15 10:49:45 +00:00
|
|
|
vulkan_->SetCbGetDrawSize([window]() {
|
|
|
|
int w=1,h=1;
|
|
|
|
SDL_Vulkan_GetDrawableSize(window, &w, &h);
|
|
|
|
return VkExtent2D {(uint32_t)w, (uint32_t)h};
|
|
|
|
});
|
|
|
|
|
2018-02-04 11:26:35 +00:00
|
|
|
SDL_SysWMinfo sys_info{};
|
|
|
|
SDL_VERSION(&sys_info.version); //Set SDL version
|
|
|
|
if (!SDL_GetWindowWMInfo(window, &sys_info)) {
|
|
|
|
fprintf(stderr, "Error getting SDL window wm info: %s\n", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
switch (sys_info.subsystem) {
|
|
|
|
case SDL_SYSWM_X11:
|
|
|
|
#if defined(VK_USE_PLATFORM_XLIB_KHR)
|
|
|
|
vulkan_->InitSurface(WINDOWSYSTEM_XLIB, (void*)sys_info.info.x11.display,
|
2019-01-26 11:03:20 +00:00
|
|
|
(void *)(intptr_t)sys_info.info.x11.window);
|
2018-02-04 11:26:35 +00:00
|
|
|
#elif defined(VK_USE_PLATFORM_XCB_KHR)
|
|
|
|
vulkan_->InitSurface(WINDOWSYSTEM_XCB, (void*)XGetXCBConnection(sys_info.info.x11.display),
|
2019-01-26 11:03:20 +00:00
|
|
|
(void *)(intptr_t)sys_info.info.x11.window);
|
2018-02-04 11:26:35 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
|
|
|
|
case SDL_SYSWM_WAYLAND:
|
2019-01-26 11:03:20 +00:00
|
|
|
vulkan_->InitSurface(WINDOWSYSTEM_WAYLAND, (void*)sys_info.info.wl.display, (void *)sys_info.info.wl.surface);
|
2018-02-04 11:26:35 +00:00
|
|
|
break;
|
2020-01-19 09:34:21 +00:00
|
|
|
#endif
|
|
|
|
#if defined(VK_USE_PLATFORM_METAL_EXT)
|
2021-03-03 04:42:55 +00:00
|
|
|
#if PPSSPP_PLATFORM(MAC)
|
2020-01-19 09:34:21 +00:00
|
|
|
case SDL_SYSWM_COCOA:
|
|
|
|
vulkan_->InitSurface(WINDOWSYSTEM_METAL_EXT, makeWindowMetalCompatible(sys_info.info.cocoa.window), nullptr);
|
|
|
|
break;
|
2020-01-20 06:43:08 +00:00
|
|
|
#else
|
|
|
|
case SDL_SYSWM_UIKIT:
|
|
|
|
vulkan_->InitSurface(WINDOWSYSTEM_METAL_EXT, makeWindowMetalCompatible(sys_info.info.uikit.window), nullptr);
|
|
|
|
break;
|
|
|
|
#endif
|
2023-02-06 12:48:21 +00:00
|
|
|
#endif
|
|
|
|
#if defined(VK_USE_PLATFORM_DISPLAY_KHR)
|
2023-04-11 14:17:12 +00:00
|
|
|
case SDL_SYSWM_KMSDRM:
|
2023-02-06 12:48:21 +00:00
|
|
|
/*
|
|
|
|
There is no problem passing null for the next two arguments, and reinit will be called later
|
|
|
|
huangzihan china
|
|
|
|
*/
|
|
|
|
vulkan_->InitSurface(WINDOWSYSTEM_DISPLAY, nullptr, nullptr);
|
|
|
|
break;
|
2018-02-04 11:26:35 +00:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Vulkan subsystem %d not supported\n", sys_info.subsystem);
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:35:39 +00:00
|
|
|
if (!vulkan_->InitSwapchain()) {
|
2018-02-04 11:26:35 +00:00
|
|
|
*error_message = vulkan_->InitError();
|
|
|
|
Shutdown();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-01 19:07:33 +00:00
|
|
|
bool useMultiThreading = g_Config.bRenderMultiThreading;
|
|
|
|
if (g_Config.iInflightFrames == 1) {
|
|
|
|
useMultiThreading = false;
|
|
|
|
}
|
|
|
|
draw_ = Draw::T3DCreateVulkanContext(vulkan_, useMultiThreading);
|
2018-02-04 11:26:35 +00:00
|
|
|
SetGPUBackend(GPUBackend::VULKAN);
|
|
|
|
bool success = draw_->CreatePresets();
|
2020-08-15 22:38:55 +00:00
|
|
|
_assert_(success);
|
2018-02-04 11:26:35 +00:00
|
|
|
draw_->HandleEvent(Draw::Event::GOT_BACKBUFFER, vulkan_->GetBackbufferWidth(), vulkan_->GetBackbufferHeight());
|
|
|
|
|
2020-06-21 22:54:51 +00:00
|
|
|
renderManager_ = (VulkanRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
|
|
|
|
renderManager_->SetInflightFrames(g_Config.iInflightFrames);
|
2018-02-04 11:26:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDLVulkanGraphicsContext::Shutdown() {
|
|
|
|
if (draw_)
|
|
|
|
draw_->HandleEvent(Draw::Event::LOST_BACKBUFFER, vulkan_->GetBackbufferWidth(), vulkan_->GetBackbufferHeight());
|
|
|
|
delete draw_;
|
|
|
|
draw_ = nullptr;
|
|
|
|
vulkan_->WaitUntilQueueIdle();
|
2020-07-18 18:35:39 +00:00
|
|
|
vulkan_->DestroySwapchain();
|
|
|
|
vulkan_->DestroySurface();
|
2018-02-04 11:26:35 +00:00
|
|
|
vulkan_->DestroyDevice();
|
|
|
|
vulkan_->DestroyInstance();
|
|
|
|
delete vulkan_;
|
|
|
|
vulkan_ = nullptr;
|
|
|
|
finalize_glslang();
|
|
|
|
}
|
2020-06-21 22:54:51 +00:00
|
|
|
|
|
|
|
void SDLVulkanGraphicsContext::Resize() {
|
|
|
|
draw_->HandleEvent(Draw::Event::LOST_BACKBUFFER, vulkan_->GetBackbufferWidth(), vulkan_->GetBackbufferHeight());
|
2020-07-18 18:35:39 +00:00
|
|
|
vulkan_->DestroySwapchain();
|
|
|
|
vulkan_->UpdateFlags(FlagsFromConfig());
|
|
|
|
vulkan_->InitSwapchain();
|
2020-06-21 22:54:51 +00:00
|
|
|
draw_->HandleEvent(Draw::Event::GOT_BACKBUFFER, vulkan_->GetBackbufferWidth(), vulkan_->GetBackbufferHeight());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDLVulkanGraphicsContext::Poll() {
|
|
|
|
// Check for existing swapchain to avoid issues during shutdown.
|
|
|
|
if (vulkan_->GetSwapchain() && renderManager_->NeedsSwapchainRecreate()) {
|
|
|
|
Resize();
|
|
|
|
}
|
2020-10-04 08:30:18 +00:00
|
|
|
}
|