renderer_vulkan: Throw when enumerating devices fails

Report device enumeration errors with exceptions to be consistent with
other initialization related function calls. Reduces the amount of code
to maintain.
This commit is contained in:
ReinUsesLisp 2020-12-25 02:27:57 -03:00
parent 11f0f7598d
commit 085adfea00
5 changed files with 21 additions and 33 deletions

View file

@ -170,7 +170,6 @@ void RendererVulkan::ShutDown() {
if (const auto& dev = device->GetLogical()) { if (const auto& dev = device->GetLogical()) {
dev.WaitIdle(); dev.WaitIdle();
} }
rasterizer.reset(); rasterizer.reset();
blit_screen.reset(); blit_screen.reset();
scheduler.reset(); scheduler.reset();
@ -180,19 +179,13 @@ void RendererVulkan::ShutDown() {
} }
bool RendererVulkan::PickDevices() { bool RendererVulkan::PickDevices() {
const auto devices = instance.EnumeratePhysicalDevices(); const std::vector<VkPhysicalDevice> devices = instance.EnumeratePhysicalDevices();
if (!devices) {
LOG_ERROR(Render_Vulkan, "Failed to enumerate physical devices");
return false;
}
const s32 device_index = Settings::values.vulkan_device.GetValue(); const s32 device_index = Settings::values.vulkan_device.GetValue();
if (device_index < 0 || device_index >= static_cast<s32>(devices->size())) { if (device_index < 0 || device_index >= static_cast<s32>(devices.size())) {
LOG_ERROR(Render_Vulkan, "Invalid device index {}!", device_index); LOG_ERROR(Render_Vulkan, "Invalid device index {}!", device_index);
return false; return false;
} }
const vk::PhysicalDevice physical_device((*devices)[static_cast<std::size_t>(device_index)], const vk::PhysicalDevice physical_device(devices[static_cast<std::size_t>(device_index)], dld);
dld);
if (!VKDevice::IsSuitable(physical_device, *surface)) { if (!VKDevice::IsSuitable(physical_device, *surface)) {
return false; return false;
} }
@ -224,23 +217,21 @@ void RendererVulkan::Report() const {
telemetry_session.AddField(field, "GPU_Vulkan_Extensions", extensions); telemetry_session.AddField(field, "GPU_Vulkan_Extensions", extensions);
} }
std::vector<std::string> RendererVulkan::EnumerateDevices() { std::vector<std::string> RendererVulkan::EnumerateDevices() try {
vk::InstanceDispatch dld; vk::InstanceDispatch dld;
Common::DynamicLibrary library = OpenLibrary(); const Common::DynamicLibrary library = OpenLibrary();
vk::Instance instance = CreateInstance(library, dld).first; const vk::Instance instance = CreateInstance(library, dld).first;
if (!instance) { const std::vector<VkPhysicalDevice> physical_devices = instance.EnumeratePhysicalDevices();
return {};
}
const std::optional physical_devices = instance.EnumeratePhysicalDevices();
if (!physical_devices) {
return {};
}
std::vector<std::string> names; std::vector<std::string> names;
names.reserve(physical_devices->size()); names.reserve(physical_devices.size());
for (const auto& device : *physical_devices) { for (const VkPhysicalDevice device : physical_devices) {
names.push_back(vk::PhysicalDevice(device, dld).GetProperties().deviceName); names.push_back(vk::PhysicalDevice(device, dld).GetProperties().deviceName);
} }
return names; return names;
} catch (const vk::Exception& exception) {
LOG_ERROR(Render_Vulkan, "Failed to enumerate devices with error: {}", exception.what());
return {};
} }
} // namespace Vulkan } // namespace Vulkan

View file

@ -111,7 +111,7 @@ void RemoveUnavailableLayers(const vk::InstanceDispatch& dld, std::vector<const
} }
} // Anonymous namespace } // Anonymous namespace
std::pair<vk::Instance, u32> CreateInstance(Common::DynamicLibrary& library, std::pair<vk::Instance, u32> CreateInstance(const Common::DynamicLibrary& library,
vk::InstanceDispatch& dld, vk::InstanceDispatch& dld,
Core::Frontend::WindowSystemType window_type, Core::Frontend::WindowSystemType window_type,
bool enable_debug_utils, bool enable_layers) { bool enable_debug_utils, bool enable_layers) {

View file

@ -14,7 +14,7 @@
namespace Vulkan { namespace Vulkan {
[[nodiscard]] std::pair<vk::Instance, u32> CreateInstance( [[nodiscard]] std::pair<vk::Instance, u32> CreateInstance(
Common::DynamicLibrary& library, vk::InstanceDispatch& dld, const Common::DynamicLibrary& library, vk::InstanceDispatch& dld,
Core::Frontend::WindowSystemType window_type = Core::Frontend::WindowSystemType::Headless, Core::Frontend::WindowSystemType window_type = Core::Frontend::WindowSystemType::Headless,
bool enable_debug_utils = false, bool enable_layers = false); bool enable_debug_utils = false, bool enable_layers = false);

View file

@ -465,17 +465,13 @@ Instance Instance::Create(u32 version, Span<const char*> layers, Span<const char
return Instance(instance, dispatch); return Instance(instance, dispatch);
} }
std::optional<std::vector<VkPhysicalDevice>> Instance::EnumeratePhysicalDevices() const { std::vector<VkPhysicalDevice> Instance::EnumeratePhysicalDevices() const {
u32 num; u32 num;
if (dld->vkEnumeratePhysicalDevices(handle, &num, nullptr) != VK_SUCCESS) { Check(dld->vkEnumeratePhysicalDevices(handle, &num, nullptr));
return std::nullopt;
}
std::vector<VkPhysicalDevice> physical_devices(num); std::vector<VkPhysicalDevice> physical_devices(num);
if (dld->vkEnumeratePhysicalDevices(handle, &num, physical_devices.data()) != VK_SUCCESS) { Check(dld->vkEnumeratePhysicalDevices(handle, &num, physical_devices.data()));
return std::nullopt;
}
SortPhysicalDevices(physical_devices, *dld); SortPhysicalDevices(physical_devices, *dld);
return std::make_optional(std::move(physical_devices)); return physical_devices;
} }
DebugUtilsMessenger Instance::CreateDebugUtilsMessenger( DebugUtilsMessenger Instance::CreateDebugUtilsMessenger(

View file

@ -580,7 +580,8 @@ public:
/// Enumerates physical devices. /// Enumerates physical devices.
/// @return Physical devices and an empty handle on failure. /// @return Physical devices and an empty handle on failure.
std::optional<std::vector<VkPhysicalDevice>> EnumeratePhysicalDevices() const; /// @throw Exception on Vulkan error.
std::vector<VkPhysicalDevice> EnumeratePhysicalDevices() const;
/// Creates a debug callback messenger. /// Creates a debug callback messenger.
/// @throw Exception on creation failure. /// @throw Exception on creation failure.