Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unwraps from init() #128

Merged
merged 3 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::NoSupportedPlatformFound);
}
};

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 @@ -887,5 +887,5 @@ fn choose_config(
}
}

Err(crate::NotSupportedError)
Err(crate::NotSupportedError::NoSupportedDeviceFound)
}
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 @@
}

#[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(any(gles, target_arch = "wasm32"))]
GLESLoadingError(egl::LoadError<libloading::Error>),

Check failure on line 103 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

failed to resolve: use of undeclared crate or module `egl`

Check failure on line 103 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

failed to resolve: use of undeclared crate or module `libloading`
#[cfg(any(gles, target_arch = "wasm32"))]
GLESError(egl::Error),

Check failure on line 105 in blade-graphics/src/lib.rs

View workflow job for this annotation

GitHub Actions / build (Web, ubuntu-latest, wasm32-unknown-unknown)

failed to resolve: use of undeclared crate or module `egl`

NoSupportedDeviceFound,
NoSupportedPlatformFound,
}

#[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::NoSupportedPlatformFound),
};

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 @@ -237,29 +239,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 @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -640,7 +644,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 @@ -649,7 +653,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
Loading