2018-01-30 12:28:44 +01:00
|
|
|
#include <cassert>
|
|
|
|
|
2018-01-20 21:47:16 +01:00
|
|
|
#include "AndroidVulkanContext.h"
|
|
|
|
#include "base/NativeApp.h"
|
|
|
|
#include "base/display.h"
|
|
|
|
#include "Common/Vulkan/VulkanContext.h"
|
2020-05-21 08:38:29 -07:00
|
|
|
#include "Common/Vulkan/VulkanDebug.h"
|
|
|
|
#include "Common/Vulkan/VulkanLoader.h"
|
2018-01-20 21:47:16 +01:00
|
|
|
#include "thin3d/VulkanRenderManager.h"
|
2018-02-25 10:27:59 +01:00
|
|
|
#include "thin3d/thin3d_create.h"
|
2018-01-20 21:47:16 +01:00
|
|
|
#include "util/text/parsers.h"
|
|
|
|
#include "Core/Config.h"
|
2018-06-16 18:42:31 -07:00
|
|
|
#include "Core/ConfigValues.h"
|
2018-01-20 21:47:16 +01:00
|
|
|
#include "Core/System.h"
|
|
|
|
|
|
|
|
static VulkanLogOptions g_LogOptions;
|
|
|
|
|
|
|
|
static VKAPI_ATTR VkBool32 VKAPI_CALL Vulkan_Dbg(VkDebugReportFlagsEXT msgFlags, VkDebugReportObjectTypeEXT objType, uint64_t srcObject, size_t location, int32_t msgCode, const char* pLayerPrefix, const char* pMsg, void *pUserData) {
|
|
|
|
const VulkanLogOptions *options = (const VulkanLogOptions *)pUserData;
|
|
|
|
int loglevel = ANDROID_LOG_INFO;
|
|
|
|
if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
|
|
|
|
loglevel = ANDROID_LOG_ERROR;
|
|
|
|
} else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
|
|
|
|
loglevel = ANDROID_LOG_WARN;
|
|
|
|
} else if (msgFlags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
|
|
|
|
loglevel = ANDROID_LOG_WARN;
|
|
|
|
} else if (msgFlags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) {
|
|
|
|
loglevel = ANDROID_LOG_WARN;
|
|
|
|
} else if (msgFlags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) {
|
|
|
|
loglevel = ANDROID_LOG_WARN;
|
|
|
|
}
|
|
|
|
|
2018-01-20 20:29:18 +01:00
|
|
|
__android_log_print(loglevel, APP_NAME, "[%s] %s Code %d : %s",
|
2020-05-21 08:38:29 -07:00
|
|
|
pLayerPrefix, VulkanObjTypeToString(objType), msgCode, pMsg);
|
2018-01-20 21:47:16 +01:00
|
|
|
|
|
|
|
// false indicates that layer should not bail-out of an
|
|
|
|
// API call that had validation failures. This may mean that the
|
|
|
|
// app dies inside the driver due to invalid parameter(s).
|
|
|
|
// That's what would happen without validation layers, so we'll
|
|
|
|
// keep that behavior here.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-03 00:00:18 +01:00
|
|
|
AndroidVulkanContext::AndroidVulkanContext() {
|
|
|
|
}
|
|
|
|
|
2018-01-20 21:47:16 +01:00
|
|
|
AndroidVulkanContext::~AndroidVulkanContext() {
|
|
|
|
delete g_Vulkan;
|
|
|
|
g_Vulkan = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-29 22:45:18 -08:00
|
|
|
static uint32_t FlagsFromConfig() {
|
|
|
|
if (g_Config.bVSync) {
|
|
|
|
return VULKAN_FLAG_PRESENT_FIFO;
|
|
|
|
}
|
|
|
|
return VULKAN_FLAG_PRESENT_MAILBOX | VULKAN_FLAG_PRESENT_FIFO_RELAXED;
|
|
|
|
}
|
|
|
|
|
2018-03-03 00:00:18 +01:00
|
|
|
bool AndroidVulkanContext::InitAPI() {
|
2018-01-20 21:47:16 +01:00
|
|
|
ILOG("AndroidVulkanContext::Init");
|
|
|
|
init_glslang();
|
|
|
|
|
|
|
|
g_LogOptions.breakOnError = true;
|
|
|
|
g_LogOptions.breakOnWarning = true;
|
|
|
|
g_LogOptions.msgBoxOnError = false;
|
|
|
|
|
|
|
|
ILOG("Creating Vulkan context");
|
|
|
|
Version gitVer(PPSSPP_GIT_VERSION);
|
|
|
|
|
2019-09-01 22:20:44 +02:00
|
|
|
if (!VulkanLoad()) {
|
|
|
|
ELOG("Failed to load Vulkan driver library");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-20 21:47:16 +01:00
|
|
|
if (!g_Vulkan) {
|
2019-09-01 22:20:44 +02:00
|
|
|
// TODO: Assert if g_Vulkan already exists here?
|
2018-01-20 21:47:16 +01:00
|
|
|
g_Vulkan = new VulkanContext();
|
|
|
|
}
|
2019-09-01 22:20:44 +02:00
|
|
|
|
2018-01-20 21:47:16 +01:00
|
|
|
VulkanContext::CreateInfo info{};
|
|
|
|
info.app_name = "PPSSPP";
|
|
|
|
info.app_ver = gitVer.ToInteger();
|
2020-02-29 22:45:18 -08:00
|
|
|
info.flags = FlagsFromConfig();
|
2018-03-17 11:03:24 +01:00
|
|
|
VkResult res = g_Vulkan->CreateInstance(info);
|
|
|
|
if (res != VK_SUCCESS) {
|
2018-01-20 21:47:16 +01:00
|
|
|
ELOG("Failed to create vulkan context: %s", g_Vulkan->InitError().c_str());
|
2018-03-17 11:03:24 +01:00
|
|
|
VulkanSetAvailable(false);
|
2018-01-20 21:47:16 +01:00
|
|
|
delete g_Vulkan;
|
|
|
|
g_Vulkan = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int physicalDevice = g_Vulkan->GetBestPhysicalDevice();
|
|
|
|
if (physicalDevice < 0) {
|
|
|
|
ELOG("No usable Vulkan device found.");
|
|
|
|
g_Vulkan->DestroyInstance();
|
|
|
|
delete g_Vulkan;
|
|
|
|
g_Vulkan = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_Vulkan->ChooseDevice(physicalDevice);
|
|
|
|
// Here we can enable device extensions if we like.
|
|
|
|
|
|
|
|
ILOG("Creating Vulkan device");
|
|
|
|
if (g_Vulkan->CreateDevice() != VK_SUCCESS) {
|
|
|
|
ILOG("Failed to create vulkan device: %s", g_Vulkan->InitError().c_str());
|
|
|
|
System_SendMessage("toast", "No Vulkan driver found. Using OpenGL instead.");
|
|
|
|
g_Vulkan->DestroyInstance();
|
|
|
|
delete g_Vulkan;
|
|
|
|
g_Vulkan = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-23 18:34:25 +01:00
|
|
|
ILOG("Vulkan device created!");
|
2018-03-03 00:00:18 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AndroidVulkanContext::InitFromRenderThread(ANativeWindow *wnd, int desiredBackbufferSizeX, int desiredBackbufferSizeY, int backbufferFormat, int androidVersion) {
|
2019-09-17 10:52:02 +02:00
|
|
|
ILOG("AndroidVulkanContext::InitFromRenderThread: desiredwidth=%d desiredheight=%d", desiredBackbufferSizeX, desiredBackbufferSizeY);
|
|
|
|
if (!g_Vulkan) {
|
|
|
|
ELOG("AndroidVulkanContext::InitFromRenderThread: No Vulkan context");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
VkResult res = g_Vulkan->InitSurface(WINDOWSYSTEM_ANDROID, (void *)wnd, nullptr);
|
|
|
|
if (res != VK_SUCCESS) {
|
|
|
|
ELOG("g_Vulkan->InitSurface failed: '%s'", VulkanResultToString(res));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-20 21:47:16 +01:00
|
|
|
if (g_validate_) {
|
|
|
|
int bits = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
|
|
|
|
g_Vulkan->InitDebugMsgCallback(&Vulkan_Dbg, bits, &g_LogOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
if (g_Vulkan->InitObjects()) {
|
|
|
|
draw_ = Draw::T3DCreateVulkanContext(g_Vulkan, g_Config.bGfxDebugSplitSubmit);
|
|
|
|
SetGPUBackend(GPUBackend::VULKAN);
|
|
|
|
success = draw_->CreatePresets(); // Doesn't fail, we ship the compiler.
|
2018-11-21 18:13:48 +01:00
|
|
|
_assert_msg_(G3D, success, "Failed to compile preset shaders");
|
2018-01-20 21:47:16 +01:00
|
|
|
draw_->HandleEvent(Draw::Event::GOT_BACKBUFFER, g_Vulkan->GetBackbufferWidth(), g_Vulkan->GetBackbufferHeight());
|
|
|
|
|
|
|
|
VulkanRenderManager *renderManager = (VulkanRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
|
2020-03-02 19:21:15 -08:00
|
|
|
renderManager->SetInflightFrames(g_Config.iInflightFrames);
|
2018-01-20 21:47:16 +01:00
|
|
|
success = renderManager->HasBackbuffers();
|
|
|
|
} else {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ILOG("AndroidVulkanContext::Init completed, %s", success ? "successfully" : "but failed");
|
|
|
|
if (!success) {
|
|
|
|
g_Vulkan->DestroyObjects();
|
|
|
|
g_Vulkan->DestroyDevice();
|
2019-02-05 13:00:23 +01:00
|
|
|
g_Vulkan->DestroyDebugUtilsCallback();
|
2018-01-20 21:47:16 +01:00
|
|
|
g_Vulkan->DestroyDebugMsgCallback();
|
|
|
|
|
|
|
|
g_Vulkan->DestroyInstance();
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2018-03-11 17:14:11 +01:00
|
|
|
void AndroidVulkanContext::ShutdownFromRenderThread() {
|
2018-01-20 21:47:16 +01:00
|
|
|
ILOG("AndroidVulkanContext::Shutdown");
|
|
|
|
draw_->HandleEvent(Draw::Event::LOST_BACKBUFFER, g_Vulkan->GetBackbufferWidth(), g_Vulkan->GetBackbufferHeight());
|
|
|
|
delete draw_;
|
|
|
|
draw_ = nullptr;
|
|
|
|
g_Vulkan->WaitUntilQueueIdle();
|
2018-03-11 17:14:11 +01:00
|
|
|
g_Vulkan->PerformPendingDeletes();
|
|
|
|
g_Vulkan->DestroyObjects(); // Also destroys the surface, a bit asymmetric
|
|
|
|
ILOG("Done with ShutdownFromRenderThread");
|
|
|
|
}
|
|
|
|
|
|
|
|
void AndroidVulkanContext::Shutdown() {
|
|
|
|
ILOG("Calling NativeShutdownGraphics");
|
2018-01-20 21:47:16 +01:00
|
|
|
g_Vulkan->DestroyDevice();
|
2019-02-05 13:00:23 +01:00
|
|
|
g_Vulkan->DestroyDebugUtilsCallback();
|
2018-01-20 21:47:16 +01:00
|
|
|
g_Vulkan->DestroyDebugMsgCallback();
|
|
|
|
|
|
|
|
g_Vulkan->DestroyInstance();
|
|
|
|
// We keep the g_Vulkan context around to avoid invalidating a ton of pointers around the app.
|
|
|
|
finalize_glslang();
|
|
|
|
ILOG("AndroidVulkanContext::Shutdown completed");
|
|
|
|
}
|
|
|
|
|
|
|
|
void AndroidVulkanContext::SwapBuffers() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void AndroidVulkanContext::Resize() {
|
2019-10-06 00:20:49 +02:00
|
|
|
ILOG("AndroidVulkanContext::Resize begin (oldsize: %dx%d)", g_Vulkan->GetBackbufferWidth(), g_Vulkan->GetBackbufferHeight());
|
2018-01-20 21:47:16 +01:00
|
|
|
|
|
|
|
draw_->HandleEvent(Draw::Event::LOST_BACKBUFFER, g_Vulkan->GetBackbufferWidth(), g_Vulkan->GetBackbufferHeight());
|
|
|
|
g_Vulkan->DestroyObjects();
|
|
|
|
|
2020-02-29 22:45:18 -08:00
|
|
|
g_Vulkan->UpdateFlags(FlagsFromConfig());
|
2019-01-25 12:49:18 +01:00
|
|
|
g_Vulkan->ReinitSurface();
|
2018-01-20 21:47:16 +01:00
|
|
|
g_Vulkan->InitObjects();
|
|
|
|
draw_->HandleEvent(Draw::Event::GOT_BACKBUFFER, g_Vulkan->GetBackbufferWidth(), g_Vulkan->GetBackbufferHeight());
|
2019-10-06 00:20:49 +02:00
|
|
|
ILOG("AndroidVulkanContext::Resize end (final size: %dx%d)", g_Vulkan->GetBackbufferWidth(), g_Vulkan->GetBackbufferHeight());
|
2018-01-20 21:47:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AndroidVulkanContext::SwapInterval(int interval) {
|
|
|
|
}
|