Add a convenience method to VulkanContext to reduce code duplication

Will need the exact same code for iOS.
This commit is contained in:
Henrik Rydgård 2024-05-23 02:13:54 +02:00
parent b6ce56dec0
commit fea6727ffd
4 changed files with 34 additions and 27 deletions

View file

@ -847,6 +847,33 @@ VkResult VulkanContext::InitDebugUtilsCallback() {
return res;
}
bool VulkanContext::CreateInstanceAndDevice(const CreateInfo &info) {
VkResult res = CreateInstance(info);
if (res != VK_SUCCESS) {
ERROR_LOG(G3D, "Failed to create vulkan context: %s", InitError().c_str());
VulkanSetAvailable(false);
return false;
}
int physicalDevice = GetBestPhysicalDevice();
if (physicalDevice < 0) {
ERROR_LOG(G3D, "No usable Vulkan device found.");
DestroyInstance();
return false;
}
ChooseDevice(physicalDevice);
INFO_LOG(G3D, "Creating Vulkan device (flags: %08x)", info.flags);
if (CreateDevice() != VK_SUCCESS) {
INFO_LOG(G3D, "Failed to create vulkan device: %s", InitError().c_str());
DestroyInstance();
return false;
}
return true;
}
void VulkanContext::SetDebugNameImpl(uint64_t handle, VkObjectType type, const char *name) {
VkDebugUtilsObjectNameInfoEXT info{ VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT };
info.pObjectName = name;

View file

@ -183,6 +183,10 @@ public:
int GetPhysicalDeviceByName(const std::string &name);
void ChooseDevice(int physical_device);
// Convenience method to avoid code duplication.
// If it returns false, delete the context.
bool CreateInstanceAndDevice(const CreateInfo &info);
// The coreVersion is to avoid enabling extensions that are merged into core Vulkan from a certain version.
bool EnableInstanceExtension(const char *extension, uint32_t coreVersion);
bool EnableDeviceExtension(const char *extension, uint32_t coreVersion);

View file

@ -0,0 +1,2 @@
#include "Core/Util/VulkanEmuThread.h"

View file

@ -68,33 +68,7 @@ bool AndroidVulkanContext::InitAPI() {
info.app_name = "PPSSPP";
info.app_ver = gitVer.ToInteger();
info.flags = FlagsFromConfig();
VkResult res = g_Vulkan->CreateInstance(info);
if (res != VK_SUCCESS) {
ERROR_LOG(G3D, "Failed to create vulkan context: %s", g_Vulkan->InitError().c_str());
VulkanSetAvailable(false);
delete g_Vulkan;
g_Vulkan = nullptr;
state_ = GraphicsContextState::FAILED_INIT;
return false;
}
int physicalDevice = g_Vulkan->GetBestPhysicalDevice();
if (physicalDevice < 0) {
ERROR_LOG(G3D, "No usable Vulkan device found.");
g_Vulkan->DestroyInstance();
delete g_Vulkan;
g_Vulkan = nullptr;
state_ = GraphicsContextState::FAILED_INIT;
return false;
}
g_Vulkan->ChooseDevice(physicalDevice);
INFO_LOG(G3D, "Creating Vulkan device (flags: %08x)", info.flags);
if (g_Vulkan->CreateDevice() != VK_SUCCESS) {
INFO_LOG(G3D, "Failed to create vulkan device: %s", g_Vulkan->InitError().c_str());
System_Toast("No Vulkan driver found. Using OpenGL instead.");
g_Vulkan->DestroyInstance();
if (!g_Vulkan->CreateInstanceAndDevice(info)) {
delete g_Vulkan;
g_Vulkan = nullptr;
state_ = GraphicsContextState::FAILED_INIT;