1use std::fmt;
2
3#[derive(Debug)]
6pub struct GpuError(Cause, Vec<(String, String)>);
7
8#[derive(Copy, Clone, Debug, Eq, PartialEq)]
10#[non_exhaustive]
11pub enum GpuAcquisitionStep {
12 Adapter,
14 Device,
16 Instance,
18 Pin,
20 NoHardwareAdapter,
22}
23
24#[derive(Debug)]
26#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
27enum Cause {
28 Adapter(wgpu::RequestAdapterError),
29 Device(wgpu::RequestDeviceError),
30 Instance {
31 backend: &'static str,
32 compiler: String,
33 },
34 Pin(String),
35 NoHardwareAdapter {
36 software: Vec<String>,
37 refusals: Vec<(String, String)>,
38 },
39}
40
41impl GpuError {
42 pub(crate) const fn adapter(error: wgpu::RequestAdapterError) -> Self {
43 Self(Cause::Adapter(error), Vec::new())
44 }
45
46 pub(crate) const fn device(error: wgpu::RequestDeviceError) -> Self {
47 Self(Cause::Device(error), Vec::new())
48 }
49
50 #[cfg(not(target_arch = "wasm32"))]
51 pub(crate) const fn instance(backend: &'static str, compiler: String) -> Self {
52 Self(Cause::Instance { backend, compiler }, Vec::new())
53 }
54
55 #[cfg(not(target_arch = "wasm32"))]
58 #[must_use]
59 pub fn no_dx12_adapter() -> Self {
60 Self::instance("DX12", crate::dx12_shader_compiler().to_string())
61 }
62
63 #[cfg(not(target_arch = "wasm32"))]
64 pub(crate) const fn pin(reason: String) -> Self {
65 Self(Cause::Pin(reason), Vec::new())
66 }
67
68 #[cfg(not(target_arch = "wasm32"))]
69 pub(crate) const fn no_hardware(
70 software: Vec<String>,
71 refusals: Vec<(String, String)>,
72 ) -> Self {
73 Self(Cause::NoHardwareAdapter { software, refusals }, Vec::new())
74 }
75
76 #[cfg(not(target_arch = "wasm32"))]
78 pub(crate) fn tried(mut self, attempts: Vec<(String, String)>) -> Self {
79 self.1 = attempts;
80 self
81 }
82
83 #[must_use]
85 pub const fn step(&self) -> GpuAcquisitionStep {
86 match self.0 {
87 Cause::Adapter(_) => GpuAcquisitionStep::Adapter,
88 Cause::Device(_) => GpuAcquisitionStep::Device,
89 Cause::Instance { .. } => GpuAcquisitionStep::Instance,
90 Cause::Pin(_) => GpuAcquisitionStep::Pin,
91 Cause::NoHardwareAdapter { .. } => GpuAcquisitionStep::NoHardwareAdapter,
92 }
93 }
94}
95
96impl std::error::Error for GpuError {
97 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
98 match &self.0 {
99 Cause::Adapter(error) => Some(error),
100 Cause::Device(error) => Some(error),
101 Cause::Instance { .. } | Cause::Pin(_) | Cause::NoHardwareAdapter { .. } => None,
102 }
103 }
104}
105
106impl fmt::Display for GpuError {
107 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
108 self.fmt_cause(formatter)?;
109 if self.1.len() > 1 {
110 for (adapter, reason) in &self.1 {
111 write!(formatter, "; tried {adapter}: {reason}")?;
112 }
113 }
114 Ok(())
115 }
116}
117
118impl GpuError {
119 fn fmt_cause(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
120 match &self.0 {
121 Cause::Adapter(error) => write!(formatter, "could not acquire a GPU adapter: {error}"),
122 Cause::Device(error) => write!(formatter, "could not acquire a GPU device: {error}"),
123 Cause::Instance { backend, compiler } if compiler.is_empty() => {
124 write!(formatter, "the {backend} backend did not start")
125 }
126 Cause::Instance { backend, compiler } => write!(
127 formatter,
128 "the {backend} backend did not start (shader compiler: {compiler}): is the file there?"
129 ),
130 Cause::Pin(reason) => formatter.write_str(reason),
131 Cause::NoHardwareAdapter { software, refusals } => {
132 formatter.write_str("no hardware GPU")?;
133 if !software.is_empty() {
134 write!(
135 formatter,
136 "; only software adapters ({}), which are not used here",
137 software.join(", ")
138 )?;
139 }
140 if refusals.is_empty() && software.is_empty() {
141 formatter.write_str(": no GPU adapter was found")?;
142 }
143 for (adapter, reason) in refusals {
144 write!(formatter, "; {adapter}: {reason}")?;
145 }
146 Ok(())
147 }
148 }
149 }
150}