Skip to content

Commit

Permalink
forward wgpu-core surface creation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Jan 10, 2024
1 parent b666de7 commit 656c79a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 44 deletions.
3 changes: 2 additions & 1 deletion player/src/bin/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ fn main() {
window.window_handle().unwrap().into(),
wgc::id::TypedId::zip(0, 1, wgt::Backend::Empty),
)
};
}
.unwrap();

let device = match actions.pop() {
Some(trace::Action::Init { desc, backend }) => {
Expand Down
71 changes: 29 additions & 42 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,68 +479,55 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
display_handle: raw_window_handle::RawDisplayHandle,
window_handle: raw_window_handle::RawWindowHandle,
id_in: Input<G, SurfaceId>,
) -> SurfaceId {
) -> Result<SurfaceId, hal::InstanceError> {
profiling::scope!("Instance::create_surface");

fn init<A: HalApi>(
any_surface: &mut Option<AnySurface>,
inst: &Option<A::Instance>,
display_handle: raw_window_handle::RawDisplayHandle,
window_handle: raw_window_handle::RawWindowHandle,
) {
if any_surface.is_none() {
if let Some(surface) = inst.as_ref().and_then(|inst| unsafe {
match inst.create_surface(display_handle, window_handle) {
Ok(raw) => Some(HalSurface::<A> { raw: Arc::new(raw) }),
Err(e) => {
log::warn!("Error: {:?}", e);
None
}
}
}) {
*any_surface = Some(AnySurface::new(surface));
) -> Option<Result<AnySurface, hal::InstanceError>> {
inst.as_ref().map(|inst| unsafe {
match inst.create_surface(display_handle, window_handle) {
Ok(raw) => Ok(AnySurface::new(HalSurface::<A> { raw: Arc::new(raw) })),
Err(e) => Err(e),
}
}
})
}

let mut hal_surface = None;
let mut hal_surface: Option<Result<AnySurface, hal::InstanceError>> = None;

#[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))]
init::<hal::api::Vulkan>(
&mut hal_surface,
&self.instance.vulkan,
display_handle,
window_handle,
);
if hal_surface.is_none() {
hal_surface =
init::<hal::api::Vulkan>(&self.instance.vulkan, display_handle, window_handle);
}
#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))]
init::<hal::api::Metal>(
&mut hal_surface,
&self.instance.metal,
display_handle,
window_handle,
);
if hal_surface.is_none() {
hal_surface =
init::<hal::api::Metal>(&self.instance.metal, display_handle, window_handle);
}
#[cfg(all(feature = "dx12", windows))]
init::<hal::api::Dx12>(
&mut hal_surface,
&self.instance.dx12,
display_handle,
window_handle,
);
if hal_surface.is_none() {
hal_surface =
init::<hal::api::Dx12>(&self.instance.dx12, display_handle, window_handle);
}
#[cfg(feature = "gles")]
init::<hal::api::Gles>(
&mut hal_surface,
&self.instance.gl,
display_handle,
window_handle,
);
if hal_surface.is_none() {
hal_surface = init::<hal::api::Gles>(&self.instance.gl, display_handle, window_handle);
}

// This is only None if there's no instance at all.
let hal_surface = hal_surface.unwrap()?;

let surface = Surface {
presentation: Mutex::new(None),
info: ResourceInfo::new("<Surface>"),
raw: hal_surface.unwrap(),
raw: hal_surface,
};

let (id, _) = self.surfaces.prepare::<G>(id_in).assign(surface);
id
Ok(id)
}

/// # Safety
Expand Down
2 changes: 1 addition & 1 deletion wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ impl crate::Context for Context {
raw_window_handle,
} => unsafe {
self.0
.instance_create_surface(raw_display_handle, raw_window_handle, ())
.instance_create_surface(raw_display_handle, raw_window_handle, ())?
},

#[cfg(metal)]
Expand Down

0 comments on commit 656c79a

Please sign in to comment.