From 9bcb8385fe4f5374ef7c0a7a58c812ba56a00e1f Mon Sep 17 00:00:00 2001 From: Preston McGee Date: Sat, 16 May 2026 13:37:44 -0700 Subject: [PATCH] add device clas which enumerates availble gpu devices --- CMakeLists.txt | 1 + README.md | 8 ++++++++ src/Device.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Device.hpp | 24 ++++++++++++++++++++++ src/Engine.cpp | 7 ++++++- 5 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 src/Device.cpp create mode 100644 src/Device.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e7c3d1..8dae3ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ add_library(maiden_core STATIC src/App.cpp src/Window.cpp src/Engine.cpp + src/Device.cpp # include extra source files here ) diff --git a/README.md b/README.md index 98f0210..75fa95b 100644 --- a/README.md +++ b/README.md @@ -83,10 +83,18 @@ This seems like a WSL specific error and causes real issues with relaying graphi $ sudo add-apt-repository ppa:kisak/kisak-mesa $ sudo apt-get update && sudo apt upgrade ``` + Note: this resulted in the following erre "WARNING: dzn is not a conformant Vulkan implementation, testing use only." Running `$ vkcube` showed that this indeed was just a warning. (for those curious, dzn is a compaitibility layer between DirectX12 and Vulkan for that WSL conformity) +### Could not locate a Nvidia GPU +```bash +$ sudo add-apt-repository ppa:kisak/turtle +$ sudo apt update +$ sudo apt upgrade +``` +verify with `$ vulkaninfo --summary` to ensure your GPU is shown. ## Development Roadmap ### lots of todo here diff --git a/src/Device.cpp b/src/Device.cpp new file mode 100644 index 0000000..92caf16 --- /dev/null +++ b/src/Device.cpp @@ -0,0 +1,56 @@ + +#include "Device.hpp" + +#include + +Device::Device(vk::raii::Instance* instance): instance_(instance) { + +} + +Device::~Device() { + +} + +bool Device::selectPhysicalDevice() { + + std::vector physicalDevices = instance_->enumeratePhysicalDevices(); + + if(physicalDevices.empty()) { + std::cout << "[" << __FUNCTION__ << ": " << __LINE__ << "] Error: no physical devices with Vulkan support found." << std::endl; + return false; + } + + // validate found devices + uint32_t maxScore = 0; + for(vk::raii::PhysicalDevice& physicalDevice : physicalDevices) { + uint32_t capabilityScore = evaluatePhysicalDevice(physicalDevice); + if(capabilityScore > maxScore) { + maxScore = capabilityScore; + physicalDevice_ = physicalDevice; + } + } + if(maxScore = 0) { + std::cout << "[" << __FUNCTION__ << ": " << __LINE__ << "] Error: physical devices found, but none capable for this engine." << std::endl; + return false; + } else { + vk::PhysicalDeviceProperties deviceProperties = physicalDevice_.getProperties(); + std::cout << "[" << __FUNCTION__ << ": " << __LINE__ << "] Physical device selected: " << deviceProperties.deviceName << std::endl; + return true; + } + +} + +uint32_t Device::evaluatePhysicalDevice(vk::raii::PhysicalDevice& device) { + + vk::PhysicalDeviceProperties deviceProperties = device.getProperties(); + vk::PhysicalDeviceFeatures deviceFeatures = device.getFeatures(); + + std::cout << "[" << __FUNCTION__ << ": " << __LINE__ << "] Physical device found: " << deviceProperties.deviceName << std::endl; + + if(deviceProperties.deviceType == vk::PhysicalDeviceType::eDiscreteGpu) { + return 2; + } else { + return 1; + } // i.e. a descrete gpu is preferable to an integrated gpu + +} diff --git a/src/Device.hpp b/src/Device.hpp new file mode 100644 index 0000000..a70bfd2 --- /dev/null +++ b/src/Device.hpp @@ -0,0 +1,24 @@ + +#pragma once + +#include + +class Device { + + public: + + Device(vk::raii::Instance* instance); + ~Device(); + + // assigns a capable gpu vkdevice to physicalDevice + bool selectPhysicalDevice(); + + private: + + // gives a device a score to attempt to select the most capable device + uint32_t evaluatePhysicalDevice(vk::raii::PhysicalDevice& device); + + vk::raii::Instance* instance_ = nullptr; + vk::raii::PhysicalDevice physicalDevice_ = nullptr; + +}; diff --git a/src/Engine.cpp b/src/Engine.cpp index 35afc4f..2bcbebe 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -3,6 +3,8 @@ #include +#include "Device.hpp" + Engine::Engine(Window* window): window_(window) { // cleans up this constructor @@ -19,8 +21,11 @@ void Engine::init() { std::cout << "[" << __FUNCTION__ << ": " << __LINE__ << "] Error creating Vulkan instance." << std::endl; } - // next steps: // device selection and setup + Device device(&instance_); + device.selectPhysicalDevice(); + + // next steps: // queue creation // vulkan memory allocator // create vulkan surface