diff --git a/aster/physical_device.h b/aster/physical_device.h index de778d2..ef5b368 100644 --- a/aster/physical_device.h +++ b/aster/physical_device.h @@ -15,7 +15,7 @@ enum class QueueSupportFlagBits { ePresent = 0b1000, }; -using QueueSupportFlags = vk::Flags{}; +using QueueSupportFlags = vk::Flags; struct QueueFamilyInfo { u32 index; diff --git a/aster/window.h b/aster/window.h index d2a3fc0..288a3d2 100644 --- a/aster/window.h +++ b/aster/window.h @@ -19,7 +19,7 @@ struct Window final { ~Window(); - bool should_close() const noexcept { + [[nodiscard]] bool should_close() const noexcept { return glfwWindowShouldClose(window); } diff --git a/triangle/aster.cpp b/triangle/aster.cpp index cec0222..026da1b 100644 --- a/triangle/aster.cpp +++ b/triangle/aster.cpp @@ -1,11 +1,42 @@ #include "aster/constants.h" #include "aster/glfw_context.h" +#include "aster/physical_device.h" #include "aster/window.h" +constexpr QueueSupportFlags required_queue_support = QueueSupportFlags{} | QueueSupportFlagBits::eGraphics | QueueSupportFlagBits::eCompute | QueueSupportFlagBits::ePresent | QueueSupportFlagBits::eCompute; + +[[nodiscard]] bool is_suitable_device(const PhysicalDevice *_physical_device) { + const bool all_required_queues = std::ranges::any_of(_physical_device->queue_families, [](const auto &_qfp) { + return (_qfp.support & required_queue_support) == required_queue_support; + }); + + const bool device_type_check = _physical_device->properties.deviceType != vk::PhysicalDeviceType::eCpu; + + const bool supported_present_mode = !_physical_device->present_modes.empty(); + + const bool supported_format = !_physical_device->surface_formats.empty(); + + return supported_format && supported_present_mode && device_type_check && all_required_queues; +} + +PhysicalDevice find_suitable_device(const PhysicalDevices &_physical_devices) { + for (auto &_physical_device : _physical_devices) { + if (is_suitable_device(&_physical_device)) { + return _physical_device; + } + } + throw std::runtime_error("No suitable device found."); +} + int main(int, char **) { - GlfwContext *glfw = new GlfwContext(); - Context *context = new Context("Aster", VERSION); - Window *window = new Window("Aster1", context, { 640, 480 }); + const auto *glfw = new GlfwContext(); + auto *context = new Context("Aster", VERSION); + auto *window = new Window("Aster1", context, { 640, 480 }); + + PhysicalDevices physical_devices = { window, context }; + PhysicalDevice device_to_use = find_suitable_device(physical_devices); + + INFO("Using {} as the primary device.", device_to_use.properties.deviceName.data()); while (window->poll()) { }