Skip to content

Commit

Permalink
Remove unwraps from init() (#128)
Browse files Browse the repository at this point in the history
This will allow us to present a sensible error message to users when we
cannot
open a window.

Co-Authored-By: Mikayla <[email protected]>

---------

Co-authored-by: Mikayla <[email protected]>
  • Loading branch information
ConradIrwin and mikayla-maki authored Jun 15, 2024
1 parent bb7d1f2 commit 50b3f57
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 29 deletions.
14 changes: 7 additions & 7 deletions blade-graphics/src/gles/egl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn init_egl(desc: &crate::ContextDesc) -> Result<(EglInstance, String), crate::N
} else {
egl::DynamicInstance::<egl::EGL1_4>::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) {
Expand Down Expand Up @@ -234,7 +234,7 @@ impl Context {
let (egl, _client_extensions) = init_egl(&desc)?;
let egl1_5 = egl
.upcast::<egl::EGL1_5>()
.ok_or(crate::NotSupportedError)?;
.ok_or(crate::NotSupportedError::NoSupportedDeviceFound)?;

let (display, wsi_library) = match window.display_handle().unwrap().as_raw() {
Rdh::Windows(display_handle) => {
Expand Down Expand Up @@ -316,7 +316,7 @@ impl Context {
}
other => {
log::error!("Unsupported RDH {:?}", other);
return Err(crate::NotSupportedError);
return Err(crate::NotSupportedError::PlatformNotSupported);
}
};

Expand Down Expand Up @@ -671,7 +671,7 @@ impl EglContext {
) -> Result<Self, crate::NotSupportedError> {
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)
Expand Down Expand Up @@ -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));
}
};

Expand All @@ -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)
})?
};

Expand Down Expand Up @@ -897,5 +897,5 @@ fn choose_config(
}
}

Err(crate::NotSupportedError)
Err(crate::NotSupportedError::NoSupportedDeviceFound)
}
4 changes: 2 additions & 2 deletions blade-graphics/src/gles/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Context {

impl Context {
pub unsafe fn init(_desc: crate::ContextDesc) -> Result<Self, crate::NotSupportedError> {
Err(crate::NotSupportedError)
Err(crate::NotSupportedError::PlatformNotSupported)
}

pub unsafe fn init_windowed<
Expand Down Expand Up @@ -56,7 +56,7 @@ impl Context {
.and_then(|context| context.dyn_into::<web_sys::WebGl2RenderingContext>().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());
Expand Down
21 changes: 20 additions & 1 deletion blade-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<libloading::Error>),
#[cfg(gles)]
GLESError(egl::Error),

NoSupportedDeviceFound,
PlatformNotSupported,
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Capabilities {
Expand Down
5 changes: 3 additions & 2 deletions blade-graphics/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
Expand Down
38 changes: 21 additions & 17 deletions blade-graphics/src/vulkan/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -250,29 +252,29 @@ impl super::Context {
raw_window_handle::WindowHandle,
raw_window_handle::DisplayHandle,
)>,
) -> Result<Self, crate::NotSupportedError> {
) -> Result<Self, NotSupportedError> {
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));
}
};

let supported_layers = match entry.enumerate_instance_layer_properties() {
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
Expand Down Expand Up @@ -301,7 +303,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
Expand All @@ -317,18 +319,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) {
Expand Down Expand Up @@ -356,7 +357,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 vk_surface = surface_handles.map(|(wh, dh)| {
Expand Down Expand Up @@ -384,7 +388,7 @@ impl super::Context {
inspect_adapter(phd, &instance, driver_api_version, vk_surface)
.map(|caps| (phd, caps))
})
.ok_or(crate::NotSupportedError)?;
.ok_or_else(|| NotSupportedError::NoSupportedDeviceFound)?;

log::debug!("Adapter {:#?}", capabilities);

Expand Down Expand Up @@ -653,7 +657,7 @@ impl super::Context {
})
}

pub unsafe fn init(desc: crate::ContextDesc) -> Result<Self, crate::NotSupportedError> {
pub unsafe fn init(desc: crate::ContextDesc) -> Result<Self, NotSupportedError> {
Self::init_impl(desc, None)
}

Expand All @@ -662,7 +666,7 @@ impl super::Context {
>(
window: &I,
desc: crate::ContextDesc,
) -> Result<Self, crate::NotSupportedError> {
) -> Result<Self, NotSupportedError> {
let handles = (
window.window_handle().unwrap(),
window.display_handle().unwrap(),
Expand Down

0 comments on commit 50b3f57

Please sign in to comment.