Skip to content

Commit 656c79a

Browse files
committed
forward wgpu-core surface creation errors
1 parent b666de7 commit 656c79a

File tree

3 files changed

+32
-44
lines changed

3 files changed

+32
-44
lines changed

player/src/bin/play.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ fn main() {
6363
window.window_handle().unwrap().into(),
6464
wgc::id::TypedId::zip(0, 1, wgt::Backend::Empty),
6565
)
66-
};
66+
}
67+
.unwrap();
6768

6869
let device = match actions.pop() {
6970
Some(trace::Action::Init { desc, backend }) => {

wgpu-core/src/instance.rs

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -479,68 +479,55 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
479479
display_handle: raw_window_handle::RawDisplayHandle,
480480
window_handle: raw_window_handle::RawWindowHandle,
481481
id_in: Input<G, SurfaceId>,
482-
) -> SurfaceId {
482+
) -> Result<SurfaceId, hal::InstanceError> {
483483
profiling::scope!("Instance::create_surface");
484484

485485
fn init<A: HalApi>(
486-
any_surface: &mut Option<AnySurface>,
487486
inst: &Option<A::Instance>,
488487
display_handle: raw_window_handle::RawDisplayHandle,
489488
window_handle: raw_window_handle::RawWindowHandle,
490-
) {
491-
if any_surface.is_none() {
492-
if let Some(surface) = inst.as_ref().and_then(|inst| unsafe {
493-
match inst.create_surface(display_handle, window_handle) {
494-
Ok(raw) => Some(HalSurface::<A> { raw: Arc::new(raw) }),
495-
Err(e) => {
496-
log::warn!("Error: {:?}", e);
497-
None
498-
}
499-
}
500-
}) {
501-
*any_surface = Some(AnySurface::new(surface));
489+
) -> Option<Result<AnySurface, hal::InstanceError>> {
490+
inst.as_ref().map(|inst| unsafe {
491+
match inst.create_surface(display_handle, window_handle) {
492+
Ok(raw) => Ok(AnySurface::new(HalSurface::<A> { raw: Arc::new(raw) })),
493+
Err(e) => Err(e),
502494
}
503-
}
495+
})
504496
}
505497

506-
let mut hal_surface = None;
498+
let mut hal_surface: Option<Result<AnySurface, hal::InstanceError>> = None;
499+
507500
#[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))]
508-
init::<hal::api::Vulkan>(
509-
&mut hal_surface,
510-
&self.instance.vulkan,
511-
display_handle,
512-
window_handle,
513-
);
501+
if hal_surface.is_none() {
502+
hal_surface =
503+
init::<hal::api::Vulkan>(&self.instance.vulkan, display_handle, window_handle);
504+
}
514505
#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))]
515-
init::<hal::api::Metal>(
516-
&mut hal_surface,
517-
&self.instance.metal,
518-
display_handle,
519-
window_handle,
520-
);
506+
if hal_surface.is_none() {
507+
hal_surface =
508+
init::<hal::api::Metal>(&self.instance.metal, display_handle, window_handle);
509+
}
521510
#[cfg(all(feature = "dx12", windows))]
522-
init::<hal::api::Dx12>(
523-
&mut hal_surface,
524-
&self.instance.dx12,
525-
display_handle,
526-
window_handle,
527-
);
511+
if hal_surface.is_none() {
512+
hal_surface =
513+
init::<hal::api::Dx12>(&self.instance.dx12, display_handle, window_handle);
514+
}
528515
#[cfg(feature = "gles")]
529-
init::<hal::api::Gles>(
530-
&mut hal_surface,
531-
&self.instance.gl,
532-
display_handle,
533-
window_handle,
534-
);
516+
if hal_surface.is_none() {
517+
hal_surface = init::<hal::api::Gles>(&self.instance.gl, display_handle, window_handle);
518+
}
519+
520+
// This is only None if there's no instance at all.
521+
let hal_surface = hal_surface.unwrap()?;
535522

536523
let surface = Surface {
537524
presentation: Mutex::new(None),
538525
info: ResourceInfo::new("<Surface>"),
539-
raw: hal_surface.unwrap(),
526+
raw: hal_surface,
540527
};
541528

542529
let (id, _) = self.surfaces.prepare::<G>(id_in).assign(surface);
543-
id
530+
Ok(id)
544531
}
545532

546533
/// # Safety

wgpu/src/backend/direct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ impl crate::Context for Context {
528528
raw_window_handle,
529529
} => unsafe {
530530
self.0
531-
.instance_create_surface(raw_display_handle, raw_window_handle, ())
531+
.instance_create_surface(raw_display_handle, raw_window_handle, ())?
532532
},
533533

534534
#[cfg(metal)]

0 commit comments

Comments
 (0)