From f48c8cd2cc27f7ff1f9aeb0aa8276e823f9b837f Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Thu, 7 Nov 2024 17:38:39 +0300 Subject: [PATCH] pick physical device --- main.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 5d00ea5..9711b8d 100644 --- a/main.cpp +++ b/main.cpp @@ -35,7 +35,7 @@ private: void initVulkan() { createInstance(); - // pickPhysicalDevice(); + pickPhysicalDevice(); } void mainLoop() { @@ -104,9 +104,45 @@ private: return true; } + void pickPhysicalDevice() { + uint32_t deviceCount = 0; + vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr); + + if (deviceCount == 0) { + throw std::runtime_error("no gpu with vulkan support"); + } + + std::vector devices(deviceCount); + vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data()); + + for (const auto& device : devices) { + if (isDeviceSuitable(device)) { + physicalDevice = device; + break; + } + } + + if (!physicalDevice) throw std::runtime_error("no suitable gpu"); + } + + static bool isDeviceSuitable(VkPhysicalDevice device) { + VkPhysicalDeviceProperties properties; + VkPhysicalDeviceFeatures features; + + vkGetPhysicalDeviceProperties(device, &properties); + vkGetPhysicalDeviceFeatures(device, &features); + + // if (properties.deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) return false; + if (!features.geometryShader) return false; + + return true; + } + private: GLFWwindow* window = nullptr; - VkInstance instance {}; + + VkInstance instance = VK_NULL_HANDLE; + VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; }; int main() {