wgpu_context/
error.rs

1//! Error type for WGPU Context
2
3use std::error::Error;
4use std::fmt::Display;
5use wgpu::{PollError, RequestAdapterError, RequestDeviceError};
6
7/// Errors that can occur in WgpuContext.
8#[derive(Debug)]
9pub enum WgpuContextError {
10    /// There is no available device with the features required by Vello.
11    NoCompatibleDevice,
12    /// Failed to create surface.
13    /// See [`wgpu::CreateSurfaceError`] for more information.
14    WgpuCreateSurfaceError(wgpu::CreateSurfaceError),
15    /// Surface doesn't support the required texture formats.
16    /// Make sure that you have a surface which provides one of
17    /// `TextureFormat::Rgba8Unorm`
18    /// or `TextureFormat::Bgra8Unorm` as texture formats.
19    // TODO: Why does this restriction exist?
20    UnsupportedSurfaceFormat,
21    /// Wgpu failed to request an adapter
22    RequestAdapterError(RequestAdapterError),
23    /// Wgpu failed to request a device
24    RequestDeviceError(RequestDeviceError),
25    /// Wgpu failed to poll a device
26    PollError(PollError),
27}
28
29impl Display for WgpuContextError {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        match self {
32            Self::NoCompatibleDevice => writeln!(f, "Couldn't find suitable device"),
33            Self::WgpuCreateSurfaceError(inner) => {
34                writeln!(f, "Couldn't create wgpu surface")?;
35                inner.fmt(f)
36            }
37            Self::UnsupportedSurfaceFormat => {
38                writeln!(
39                    f,
40                    "Couldn't find `Rgba8Unorm` or `Bgra8Unorm` texture formats for surface"
41                )
42            }
43            Self::RequestAdapterError(inner) => {
44                writeln!(f, "Couldn't request an adapter: {:#}", inner)
45            }
46            Self::RequestDeviceError(inner) => {
47                writeln!(f, "Couldn't request a device: {:#}", inner)
48            }
49            Self::PollError(inner) => {
50                writeln!(f, "Couldn't poll a device: {:#}", inner)
51            }
52        }
53    }
54}
55
56impl Error for WgpuContextError {}
57impl From<wgpu::CreateSurfaceError> for WgpuContextError {
58    fn from(value: wgpu::CreateSurfaceError) -> Self {
59        Self::WgpuCreateSurfaceError(value)
60    }
61}
62
63impl From<RequestAdapterError> for WgpuContextError {
64    fn from(value: RequestAdapterError) -> Self {
65        Self::RequestAdapterError(value)
66    }
67}
68
69impl From<RequestDeviceError> for WgpuContextError {
70    fn from(value: RequestDeviceError) -> Self {
71        Self::RequestDeviceError(value)
72    }
73}
74
75impl From<PollError> for WgpuContextError {
76    fn from(value: PollError) -> Self {
77        Self::PollError(value)
78    }
79}