diff --git a/blade-graphics/src/gles/egl.rs b/blade-graphics/src/gles/egl.rs index 64bb45ae..35d9c26e 100644 --- a/blade-graphics/src/gles/egl.rs +++ b/blade-graphics/src/gles/egl.rs @@ -150,7 +150,7 @@ fn init_egl(desc: &crate::ContextDesc) -> Result<(EglInstance, String), crate::N } else { egl::DynamicInstance::::load_required() }; - egl_result.map_err(|_| crate::NotSupportedError)? + egl_result.map_err(|e| crate::NotSupportedError::GLESLoadingError(e))? }; let client_ext_str = match egl.query_string(None, egl::EXTENSIONS) { @@ -234,7 +234,7 @@ impl Context { let (egl, _client_extensions) = init_egl(&desc)?; let egl1_5 = egl .upcast::() - .ok_or(crate::NotSupportedError)?; + .ok_or(crate::NotSupportedError::NoSupportedDeviceFound)?; let (display, wsi_library) = match window.display_handle().unwrap().as_raw() { Rdh::Windows(display_handle) => { @@ -316,7 +316,7 @@ impl Context { } other => { log::error!("Unsupported RDH {:?}", other); - return Err(crate::NotSupportedError); + return Err(crate::NotSupportedError::PlatformNotSupported); } }; @@ -671,7 +671,7 @@ impl EglContext { ) -> Result { let version = egl .initialize(display) - .map_err(|_| crate::NotSupportedError)?; + .map_err(|e| crate::NotSupportedError::GLESError(e))?; let vendor = egl.query_string(Some(display), egl::VENDOR).unwrap(); let display_extensions = egl .query_string(Some(display), egl::EXTENSIONS) @@ -742,7 +742,7 @@ impl EglContext { Ok(context) => context, Err(e) => { log::warn!("unable to create GLES 3.x context: {:?}", e); - return Err(crate::NotSupportedError); + return Err(crate::NotSupportedError::GLESError(e)); } }; @@ -758,7 +758,7 @@ impl EglContext { .map(Some) .map_err(|e| { log::warn!("Error in create_pbuffer_surface: {:?}", e); - crate::NotSupportedError + crate::NotSupportedError::GLESError(e) })? }; @@ -887,5 +887,5 @@ fn choose_config( } } - Err(crate::NotSupportedError) + Err(crate::NotSupportedError::NoSupportedDeviceFound) } diff --git a/blade-graphics/src/gles/web.rs b/blade-graphics/src/gles/web.rs index 978cbf0d..4311167c 100644 --- a/blade-graphics/src/gles/web.rs +++ b/blade-graphics/src/gles/web.rs @@ -21,7 +21,7 @@ pub struct Context { impl Context { pub unsafe fn init(_desc: crate::ContextDesc) -> Result { - Err(crate::NotSupportedError) + Err(crate::NotSupportedError::PlatformNotSupported) } pub unsafe fn init_windowed< @@ -56,7 +56,7 @@ impl Context { .and_then(|context| context.dyn_into::().ok()) .expect("Cannot convert into WebGL2 context") } - _ => return Err(crate::NotSupportedError), + _ => return Err(crate::NotSupportedError::PlatformNotSupported), }; let glow = glow::Context::from_webgl2_context(webgl2.clone()); diff --git a/blade-graphics/src/lib.rs b/blade-graphics/src/lib.rs index 2c61b251..8ecb34ad 100644 --- a/blade-graphics/src/lib.rs +++ b/blade-graphics/src/lib.rs @@ -87,7 +87,26 @@ pub struct ContextDesc { } #[derive(Debug)] -pub struct NotSupportedError; +pub enum NotSupportedError { + #[cfg(all( + not(gles), + any(vulkan, windows, target_os = "linux", target_os = "android") + ))] + VulkanLoadingError(ash::LoadingError), + #[cfg(all( + not(gles), + any(vulkan, windows, target_os = "linux", target_os = "android") + ))] + VulkanError(ash::vk::Result), + + #[cfg(gles)] + GLESLoadingError(egl::LoadError), + #[cfg(gles)] + GLESError(egl::Error), + + NoSupportedDeviceFound, + PlatformNotSupported, +} #[derive(Clone, Debug, Default, PartialEq)] pub struct Capabilities { diff --git a/blade-graphics/src/metal/mod.rs b/blade-graphics/src/metal/mod.rs index 7656190e..56caf6c9 100644 --- a/blade-graphics/src/metal/mod.rs +++ b/blade-graphics/src/metal/mod.rs @@ -397,7 +397,8 @@ impl Context { std::env::set_var("MTL_HUD_ENABLED", "1"); } - let device = metal::Device::system_default().ok_or(super::NotSupportedError)?; + let device = metal::Device::system_default() + .ok_or(super::NotSupportedError::NoSupportedDeviceFound)?; let queue = device.new_command_queue(); let capture = if desc.capture { @@ -442,7 +443,7 @@ impl Context { raw_window_handle::RawWindowHandle::AppKit(handle) => { Surface::from_view(handle.ns_view.as_ptr() as *mut _) } - _ => return Err(crate::NotSupportedError), + _ => return Err(crate::NotSupportedError::PlatformNotSupported), }; context.surface = Some(Mutex::new(surface)); diff --git a/blade-graphics/src/vulkan/init.rs b/blade-graphics/src/vulkan/init.rs index fe559bb9..7b5c751d 100644 --- a/blade-graphics/src/vulkan/init.rs +++ b/blade-graphics/src/vulkan/init.rs @@ -2,6 +2,8 @@ use ash::{amd, ext, khr, vk}; use naga::back::spv; use std::{ffi, fs, mem, sync::Mutex}; +use crate::NotSupportedError; + mod db { pub mod intel { pub const VENDOR: u32 = 0x8086; @@ -237,21 +239,21 @@ impl super::Context { raw_window_handle::WindowHandle, raw_window_handle::DisplayHandle, )>, - ) -> Result { + ) -> Result { let entry = match ash::Entry::load() { Ok(entry) => entry, Err(err) => { log::error!("Missing Vulkan entry points: {:?}", err); - return Err(crate::NotSupportedError); + return Err(NotSupportedError::VulkanLoadingError(err)); } }; let driver_api_version = match entry.try_enumerate_instance_version() { // Vulkan 1.1+ Ok(Some(version)) => version, - Ok(None) => return Err(crate::NotSupportedError), + Ok(None) => return Err(NotSupportedError::NoSupportedDeviceFound), Err(err) => { log::error!("try_enumerate_instance_version: {:?}", err); - return Err(crate::NotSupportedError); + return Err(NotSupportedError::VulkanError(err)); } }; @@ -259,7 +261,7 @@ impl super::Context { Ok(layers) => layers, Err(err) => { log::error!("enumerate_instance_layer_properties: {:?}", err); - return Err(crate::NotSupportedError); + return Err(NotSupportedError::VulkanError(err)); } }; let supported_layer_names = supported_layers @@ -288,7 +290,7 @@ impl super::Context { Ok(extensions) => extensions, Err(err) => { log::error!("enumerate_instance_extension_properties: {:?}", err); - return Err(crate::NotSupportedError); + return Err(NotSupportedError::VulkanError(err)); } }; let supported_instance_extensions = supported_instance_extension_properties @@ -304,18 +306,17 @@ impl super::Context { vk::KHR_GET_PHYSICAL_DEVICE_PROPERTIES2_NAME, ]; if let Some((_, dh)) = surface_handles { - instance_extensions.extend( - ash_window::enumerate_required_extensions(dh.as_raw()) - .unwrap() - .iter() - .map(|&ptr| ffi::CStr::from_ptr(ptr)), - ); + match ash_window::enumerate_required_extensions(dh.as_raw()) { + Ok(extensions) => instance_extensions + .extend(extensions.iter().map(|&ptr| ffi::CStr::from_ptr(ptr))), + Err(e) => return Err(NotSupportedError::VulkanError(e)), + } } for inst_ext in instance_extensions.iter() { if !supported_instance_extensions.contains(inst_ext) { log::error!("Instance extension {:?} is not supported", inst_ext); - return Err(crate::NotSupportedError); + return Err(NotSupportedError::NoSupportedDeviceFound); } } if supported_instance_extensions.contains(&vk::KHR_PORTABILITY_ENUMERATION_NAME) { @@ -343,7 +344,10 @@ impl super::Context { .flags(create_flags) .enabled_layer_names(layer_strings) .enabled_extension_names(extension_strings); - entry.create_instance(&create_info, None).unwrap() + match entry.create_instance(&create_info, None) { + Ok(instance) => instance, + Err(e) => return Err(NotSupportedError::VulkanError(e)), + } }; let bugs = SystemBugs { @@ -379,7 +383,7 @@ impl super::Context { inspect_adapter(phd, &instance, driver_api_version, &bugs, vk_surface) .map(|caps| (phd, caps)) }) - .ok_or(crate::NotSupportedError)?; + .ok_or_else(|| NotSupportedError::NoSupportedDeviceFound)?; log::debug!("Adapter {:#?}", capabilities); @@ -640,7 +644,7 @@ impl super::Context { }) } - pub unsafe fn init(desc: crate::ContextDesc) -> Result { + pub unsafe fn init(desc: crate::ContextDesc) -> Result { Self::init_impl(desc, None) } @@ -649,7 +653,7 @@ impl super::Context { >( window: &I, desc: crate::ContextDesc, - ) -> Result { + ) -> Result { let handles = ( window.window_handle().unwrap(), window.display_handle().unwrap(),