From 6ce11daa2442d0d2b159b8a2c431755b0d99327f Mon Sep 17 00:00:00 2001 From: Jiga228 Date: Thu, 18 Sep 2025 20:19:24 +0700 Subject: [PATCH] Add choose GPU. --- Core/Core/CoreInstance.cpp | 1 + Core/Core/CoreInstance.h | 2 +- Core/Core/RenderEngine.cpp | 28 ++++++++++++++++++++++++---- Core/Core/RenderEngine.h | 5 +++-- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Core/Core/CoreInstance.cpp b/Core/Core/CoreInstance.cpp index a9a72fb..db8f6f1 100644 --- a/Core/Core/CoreInstance.cpp +++ b/Core/Core/CoreInstance.cpp @@ -8,6 +8,7 @@ #include #include +#include "RenderEngine.h" #include "Game/GameInstance.h" #include "Log/Log.h" #include "Game/SaveMap/SaveMap.h" diff --git a/Core/Core/CoreInstance.h b/Core/Core/CoreInstance.h index 410c422..897d537 100644 --- a/Core/Core/CoreInstance.h +++ b/Core/Core/CoreInstance.h @@ -5,13 +5,13 @@ #include #include -#include "RenderEngine.h" #include "Game/SaveMap/ISave.h" #include "Core/CoreCallBacks.h" class SaveMap; class RenderEngine; class GameInstance; +struct GLFWwindow; class CoreInstance { struct Module { diff --git a/Core/Core/RenderEngine.cpp b/Core/Core/RenderEngine.cpp index 5610168..1e9a487 100644 --- a/Core/Core/RenderEngine.cpp +++ b/Core/Core/RenderEngine.cpp @@ -2,9 +2,9 @@ #include #include +#include "Log/Log.h" #ifdef _DEBUG -#include "Log/Log.h" #include #define VK_CHECK(res) assert(res == VK_SUCCESS) @@ -42,9 +42,19 @@ std::vector RenderEngine::get_required_extensions() return extensions; } +bool RenderEngine::is_device_suitable(VkPhysicalDevice device) +{ + VkPhysicalDeviceProperties device_properties; + VkPhysicalDeviceFeatures device_features; + vkGetPhysicalDeviceProperties(device, &device_properties); + vkGetPhysicalDeviceFeatures(device, &device_features); + + return device_properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU && device_features.geometryShader; +} + RenderEngine::RenderEngine(GLFWwindow* window) : window_(window) #ifdef _DEBUG -, debug_messenger_(nullptr) + , debug_messenger_(nullptr) #endif { { @@ -89,13 +99,23 @@ RenderEngine::RenderEngine(GLFWwindow* window) : window_(window) uint32_t device_count = 0; std::vector devices; VK_CHECK(vkEnumeratePhysicalDevices(instance_, &device_count, nullptr)); + devices.resize(device_count); VK_CHECK(vkEnumeratePhysicalDevices(instance_, &device_count, devices.data())); for (const auto& device : devices) { - VkPhysicalDeviceProperties device_properties; - vkGetPhysicalDeviceProperties(device, &device_properties); + if (is_device_suitable(device)) + { + physical_device_ = device; + VkPhysicalDeviceProperties device_properties; + vkGetPhysicalDeviceProperties(physical_device_, &device_properties); + Log("Using device: " + std::string(device_properties.deviceName)); + break; + } } + + if (VK_NULL_HANDLE == physical_device_) + throw std::runtime_error("No suitable device found"); } } diff --git a/Core/Core/RenderEngine.h b/Core/Core/RenderEngine.h index f87929e..764330b 100644 --- a/Core/Core/RenderEngine.h +++ b/Core/Core/RenderEngine.h @@ -9,7 +9,8 @@ class RenderEngine { GLFWwindow* window_; - VkInstance instance_; + VkInstance instance_ = VK_NULL_HANDLE; + VkPhysicalDevice physical_device_ = VK_NULL_HANDLE; #ifdef _DEBUG VkResult enable_layer_validation(VkDebugUtilsMessengerCreateInfoEXT* create_info); @@ -17,7 +18,7 @@ class RenderEngine #endif static std::vector get_required_extensions(); - + bool is_device_suitable(VkPhysicalDevice device); public: RenderEngine(GLFWwindow* window); ~RenderEngine();