Return result instead of panicking when requesting device. (#762)

* Return Result instead of panic for recoverable errors

* Update LimitsExceeded error variant

* Update LimitsExceeded error message
This commit is contained in:
Oskar Nehlin 2020-07-06 22:45:11 +02:00 committed by GitHub
parent 5b4b695ad3
commit 8a51f4bc7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 12 deletions

View File

@ -507,10 +507,10 @@ fn main() {
None,
wgc::id::TypedId::zip(1, 0, wgt::Backend::Empty)
))
.expect("Failed to request device")
}
_ => panic!("Expected Action::Init"),
};
log::info!("Executing actions");
#[cfg(not(feature = "winit"))]
{

View File

@ -23,6 +23,7 @@ use hal::{
window::Surface as _,
Instance as _,
};
use std::fmt::Display;
#[repr(C)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
@ -217,6 +218,30 @@ impl AdapterInfo {
}
}
#[derive(Clone, Debug, PartialEq)]
/// Error when requesting a device from the adaptor
pub enum RequestDeviceError {
/// Unsupported feature extension was requested
UnsupportedFeature(wgt::Features),
/// Requested device limits were exceeded
LimitsExceeded,
}
impl Display for RequestDeviceError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self {
RequestDeviceError::UnsupportedFeature(features) => write!(
f,
"Cannot enable features that adapter doesn't support. Unsupported extensions: {:?}",
features
),
RequestDeviceError::LimitsExceeded => {
write!(f, "Some of the requested limits are not supported",)
}
}
}
}
/// Supported physical device types.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "trace", derive(Serialize))]
@ -550,7 +575,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
desc: &DeviceDescriptor,
trace_path: Option<&std::path::Path>,
id_in: Input<G, DeviceId>,
) -> DeviceId {
) -> Result<DeviceId, RequestDeviceError> {
span!(_guard, INFO, "Adapter::request_device");
let hub = B::hub(self);
@ -568,11 +593,11 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
desc.features & wgt::Features::ALL_UNSAFE
)
}
assert!(
adapter.features.contains(desc.features),
"Cannot enable features that adapter doesn't support. Unsupported extensions: {:?}",
desc.features - adapter.features
);
if !adapter.features.contains(desc.features) {
return Err(RequestDeviceError::UnsupportedFeature(
desc.features - adapter.features,
));
}
// Verify feature preconditions
if desc
@ -665,10 +690,9 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
if limits.max_bound_descriptor_sets == 0 {
log::warn!("max_bind_groups limit is missing");
} else {
assert!(
desc.limits.max_bind_groups <= adapter.limits.max_bind_groups,
"Adapter does not support the requested max_bind_groups"
);
if adapter.limits.max_bind_groups < desc.limits.max_bind_groups {
return Err(RequestDeviceError::LimitsExceeded);
}
}
let mem_props = phd.memory_properties();
@ -699,6 +723,6 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
)
};
hub.devices.register_identity(id_in, device, &mut token)
Ok(hub.devices.register_identity(id_in, device, &mut token))
}
}