From 9f1f75ddab0d6db2d21ec0071ee3fcbaa521cccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 17 Jul 2024 10:31:49 +0200 Subject: [PATCH] Refactor: Merge the ChooseDevice function into CreateDevice --- Common/GPU/Vulkan/VulkanContext.cpp | 62 +++++++++++++--------------- Common/GPU/Vulkan/VulkanContext.h | 4 +- SDL/SDLVulkanGraphicsContext.cpp | 3 +- Windows/GPU/WindowsVulkanContext.cpp | 3 +- libretro/LibretroVulkanContext.cpp | 3 +- 5 files changed, 34 insertions(+), 41 deletions(-) diff --git a/Common/GPU/Vulkan/VulkanContext.cpp b/Common/GPU/Vulkan/VulkanContext.cpp index 71bd09d6c0..be5d474d57 100644 --- a/Common/GPU/Vulkan/VulkanContext.cpp +++ b/Common/GPU/Vulkan/VulkanContext.cpp @@ -559,7 +559,33 @@ int VulkanContext::GetBestPhysicalDevice() { return best; } -void VulkanContext::ChooseDevice(int physical_device) { +bool VulkanContext::EnableDeviceExtension(const char *extension, uint32_t coreVersion) { + if (coreVersion != 0 && vulkanApiVersion_ >= coreVersion) { + return true; + } + for (auto &iter : device_extension_properties_) { + if (!strcmp(iter.extensionName, extension)) { + device_extensions_enabled_.push_back(extension); + return true; + } + } + return false; +} + +bool VulkanContext::EnableInstanceExtension(const char *extension, uint32_t coreVersion) { + if (coreVersion != 0 && vulkanApiVersion_ >= coreVersion) { + return true; + } + for (auto &iter : instance_extension_properties_) { + if (!strcmp(iter.extensionName, extension)) { + instance_extensions_enabled_.push_back(extension); + return true; + } + } + return false; +} + +VkResult VulkanContext::CreateDevice(int physical_device) { physical_device_ = physical_device; INFO_LOG(Log::G3D, "Chose physical device %d: %s", physical_device, physicalDeviceProperties_[physical_device].properties.deviceName); @@ -617,7 +643,7 @@ void VulkanContext::ChooseDevice(int physical_device) { // Optional features if (extensionsLookup_.KHR_get_physical_device_properties2 && vkGetPhysicalDeviceFeatures2) { - VkPhysicalDeviceFeatures2 features2{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR}; + VkPhysicalDeviceFeatures2 features2{ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR }; // Add to chain even if not supported, GetPhysicalDeviceFeatures is supposed to ignore unknown structs. VkPhysicalDeviceMultiviewFeatures multiViewFeatures{ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES }; VkPhysicalDevicePresentWaitFeaturesKHR presentWaitFeatures{ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_WAIT_FEATURES_KHR }; @@ -641,35 +667,7 @@ void VulkanContext::ChooseDevice(int physical_device) { GetDeviceLayerExtensionList(nullptr, device_extension_properties_); device_extensions_enabled_.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); -} -bool VulkanContext::EnableDeviceExtension(const char *extension, uint32_t coreVersion) { - if (coreVersion != 0 && vulkanApiVersion_ >= coreVersion) { - return true; - } - for (auto &iter : device_extension_properties_) { - if (!strcmp(iter.extensionName, extension)) { - device_extensions_enabled_.push_back(extension); - return true; - } - } - return false; -} - -bool VulkanContext::EnableInstanceExtension(const char *extension, uint32_t coreVersion) { - if (coreVersion != 0 && vulkanApiVersion_ >= coreVersion) { - return true; - } - for (auto &iter : instance_extension_properties_) { - if (!strcmp(iter.extensionName, extension)) { - instance_extensions_enabled_.push_back(extension); - return true; - } - } - return false; -} - -VkResult VulkanContext::CreateDevice() { if (!init_error_.empty() || physical_device_ < 0) { ERROR_LOG(Log::G3D, "Vulkan init failed: %s", init_error_.c_str()); return VK_ERROR_INITIALIZATION_FAILED; @@ -871,10 +869,8 @@ bool VulkanContext::CreateInstanceAndDevice(const CreateInfo &info) { return false; } - ChooseDevice(physicalDevice); - INFO_LOG(Log::G3D, "Creating Vulkan device (flags: %08x)", info.flags); - if (CreateDevice() != VK_SUCCESS) { + if (CreateDevice(physicalDevice) != VK_SUCCESS) { INFO_LOG(Log::G3D, "Failed to create vulkan device: %s", InitError().c_str()); DestroyInstance(); return false; diff --git a/Common/GPU/Vulkan/VulkanContext.h b/Common/GPU/Vulkan/VulkanContext.h index 0148738ac7..bda7484dac 100644 --- a/Common/GPU/Vulkan/VulkanContext.h +++ b/Common/GPU/Vulkan/VulkanContext.h @@ -181,7 +181,6 @@ public: int GetBestPhysicalDevice(); int GetPhysicalDeviceByName(const std::string &name); - void ChooseDevice(int physical_device); // Convenience method to avoid code duplication. // If it returns false, delete the context. @@ -191,7 +190,8 @@ public: bool EnableInstanceExtension(const char *extension, uint32_t coreVersion); bool EnableDeviceExtension(const char *extension, uint32_t coreVersion); - VkResult CreateDevice(); + // Was previously two functions, ChooseDevice and CreateDevice. + VkResult CreateDevice(int physical_device); const std::string &InitError() const { return init_error_; } diff --git a/SDL/SDLVulkanGraphicsContext.cpp b/SDL/SDLVulkanGraphicsContext.cpp index 9300afa8d6..a581818bdc 100644 --- a/SDL/SDLVulkanGraphicsContext.cpp +++ b/SDL/SDLVulkanGraphicsContext.cpp @@ -84,8 +84,7 @@ bool SDLVulkanGraphicsContext::Init(SDL_Window *&window, int x, int y, int w, in g_Config.sVulkanDevice = vulkan_->GetPhysicalDeviceProperties(deviceNum).properties.deviceName; } - vulkan_->ChooseDevice(deviceNum); - if (vulkan_->CreateDevice() != VK_SUCCESS) { + if (vulkan_->CreateDevice(deviceNum) != VK_SUCCESS) { *error_message = vulkan_->InitError(); delete vulkan_; vulkan_ = nullptr; diff --git a/Windows/GPU/WindowsVulkanContext.cpp b/Windows/GPU/WindowsVulkanContext.cpp index 13ee5f1bf1..cec6f494ca 100644 --- a/Windows/GPU/WindowsVulkanContext.cpp +++ b/Windows/GPU/WindowsVulkanContext.cpp @@ -118,8 +118,7 @@ bool WindowsVulkanContext::Init(HINSTANCE hInst, HWND hWnd, std::string *error_m g_Config.sVulkanDevice = vulkan_->GetPhysicalDeviceProperties(deviceNum).properties.deviceName; } - vulkan_->ChooseDevice(deviceNum); - if (vulkan_->CreateDevice() != VK_SUCCESS) { + if (vulkan_->CreateDevice(deviceNum) != VK_SUCCESS) { *error_message = vulkan_->InitError(); delete vulkan_; vulkan_ = nullptr; diff --git a/libretro/LibretroVulkanContext.cpp b/libretro/LibretroVulkanContext.cpp index 2d218347e7..4719fb4c9b 100644 --- a/libretro/LibretroVulkanContext.cpp +++ b/libretro/LibretroVulkanContext.cpp @@ -56,8 +56,7 @@ static bool create_device(retro_vulkan_context *context, VkInstance instance, Vk physical_device = vk->GetBestPhysicalDevice(); } - vk->ChooseDevice(physical_device); - vk->CreateDevice(); + vk->CreateDevice(physical_device); #ifdef _WIN32 vk->InitSurface(WINDOWSYSTEM_WIN32, nullptr, nullptr); #elif defined(__ANDROID__)