Check surface support when choosing device

This commit is contained in:
2025-04-12 20:27:30 +10:00
parent 6cbf8df17a
commit 930bba636e
5 changed files with 58 additions and 16 deletions

View File

@@ -10,6 +10,7 @@
#include <nova/core/debug.h>
#include <nova/platform/window_driver.h>
#include <nova/render/render_device.h>
#include <nova/version.h>
#include <vulkan/vulkan.h>
@@ -73,15 +74,26 @@ const RenderDevice& VulkanRenderDriver::get_device(const u32 index) const {
return m_devices[index];
}
bool VulkanRenderDriver::get_device_supports_surface(const u32 index, const SurfaceID surface) const {
NOVA_AUTO_TRACE();
NOVA_ASSERT(index < m_devices.size());
NOVA_ASSERT(surface);
// TODO: Check other queue families?
SurfaceData* data = reinterpret_cast<SurfaceData*>(surface);
VkBool32 supported = false;
if (vkGetPhysicalDeviceSurfaceSupportKHR(static_cast<VkPhysicalDevice>(m_devices[index].handle), 0, data->handle, &supported)
!= VK_SUCCESS) {
return false;
}
return supported;
}
void VulkanRenderDriver::select_device(u32 index) {
NOVA_AUTO_TRACE();
NOVA_ASSERT(!m_device);
if (index == RenderDevice::AUTO) {
index = RenderDevice::choose_device(m_devices);
} else {
NOVA_ASSERT(index < m_devices.size());
}
NOVA_ASSERT(index < m_devices.size());
NOVA_LOG("Using device: {}", m_devices[index].name);
m_physical_device = static_cast<VkPhysicalDevice>(m_devices[index].handle);

View File

@@ -31,6 +31,7 @@ namespace Nova {
[[nodiscard]] u32 get_device_count() const override;
[[nodiscard]] const RenderDevice& get_device(u32 index) const override;
[[nodiscard]] bool get_device_supports_surface(u32 index, SurfaceID surface) const override;
void select_device(u32 index) override;
[[nodiscard]] SurfaceID create_surface(WindowID window) override;

View File

@@ -6,18 +6,31 @@
#include <nova/core/debug.h>
#include <nova/render/render_device.h>
#include <nova/render/render_driver.h>
using namespace Nova;
u32 RenderDevice::choose_device(const std::vector<RenderDevice>& devices) {
u32 RenderDevice::choose_device(RenderDriver* driver, std::span<const SurfaceID> surfaces) {
NOVA_AUTO_TRACE();
u32 best_index = 0;
u32 best_index = -1;
u32 best_score = 0;
for (u32 i = 0; i < devices.size(); i++) {
u32 score = 0;
switch (devices[i].type) {
for (u32 i = 0; i < driver->get_device_count(); i++) {
auto& device = driver->get_device(i);
u32 score = 1;
for (SurfaceID surface : surfaces) {
if (!driver->get_device_supports_surface(i, surface)) {
score = 0;
break;
}
}
if (score == 0) {
continue;
}
switch (device.type) {
case Type::DISCRETE:
score += 4;
break;
@@ -33,11 +46,16 @@ u32 RenderDevice::choose_device(const std::vector<RenderDevice>& devices) {
default:
break;
}
if (score > best_score) {
best_index = i;
best_score = score;
}
}
if (best_index == -1U) {
throw std::runtime_error("No suitable render device found");
}
return best_index;
}