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 1 commit
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
22 changes: 21 additions & 1 deletion blade-graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
clippy::pattern_type_mismatch,
)]

use ash::vk;

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

View workflow job for this annotation

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

unresolved import `ash`

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

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

unresolved import `ash`
pub use naga::{StorageAccess, VectorSize};
pub type Transform = mint::RowMatrix3x4<f32>;

Expand Down Expand Up @@ -87,7 +88,26 @@
}

#[derive(Debug)]
pub struct NotSupportedError;
pub enum NotSupportedError {
VulkanLoadingError(ash::LoadingError),

Check failure on line 92 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 `ash`

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

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

failed to resolve: use of undeclared crate or module `ash`
VulkanError(vk::Result),
/// Vulkan API below 1.1
ApiMismatch,
NoSupportedDeviceFound,
ExtensionNotSupported,
}

impl From<vk::Result> for NotSupportedError {
fn from(result: vk::Result) -> Self {
Self::VulkanError(result)
}
}

impl From<ash::LoadingError> for NotSupportedError {

Check failure on line 106 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 `ash`

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

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

failed to resolve: use of undeclared crate or module `ash`
fn from(result: ash::LoadingError) -> Self {

Check failure on line 107 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 `ash`

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

View workflow job for this annotation

GitHub Actions / build (MacOS, macos-latest, x86_64-apple-darwin)

failed to resolve: use of undeclared crate or module `ash`
Self::VulkanLoadingError(result)
}
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Capabilities {
Expand Down
27 changes: 14 additions & 13 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::ApiMismatch),
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 @@ -305,8 +307,7 @@ impl super::Context {
];
if let Some((_, dh)) = surface_handles {
instance_extensions.extend(
ash_window::enumerate_required_extensions(dh.as_raw())
.unwrap()
ash_window::enumerate_required_extensions(dh.as_raw())?
.iter()
.map(|&ptr| ffi::CStr::from_ptr(ptr)),
);
Expand All @@ -315,7 +316,7 @@ impl super::Context {
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::ExtensionNotSupported);
}
}
if supported_instance_extensions.contains(&vk::KHR_PORTABILITY_ENUMERATION_NAME) {
Expand Down Expand Up @@ -343,7 +344,7 @@ impl super::Context {
.flags(create_flags)
.enabled_layer_names(layer_strings)
.enabled_extension_names(extension_strings);
entry.create_instance(&create_info, None).unwrap()
entry.create_instance(&create_info, None)?
};

let bugs = SystemBugs {
Expand Down Expand Up @@ -379,7 +380,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 +641,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 +650,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