summaryrefslogtreecommitdiff
path: root/src/renderer/error.rs
blob: f1418e863ca3599a27bc46feb0602cab8718caf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#![allow(unused)]

use winit::error as werror;

use vulkanalia::{bytecode::BytecodeError, loader::LoaderError, prelude::v1_0::*};

pub(super) enum MainError {
    NotSupported(werror::NotSupportedError),
    Os(werror::OsError),
    Event(werror::EventLoopError),
    Code(i32),
    LibLoadError(String),
    BoxLoadError(Box<dyn LoaderError>),
    ValidationNotSupported,
    Suitability(SuitabilityError),
    NoDevice,
    NoExtension,
    InsufficientSwapchainSupport,
    InvalidBytecode(usize),
    AllocBytecode,
}

impl std::fmt::Debug for MainError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Code(code) => write!(f, "{:?}", vk::ErrorCode::from_raw(*code)),
            Self::NotSupported(e) => write!(f, "{e:?}"),
            Self::Os(e) => write!(f, "{e:?}"),
            Self::Event(e) => write!(f, "{e:?}"),
            Self::BoxLoadError(e) => write!(f, "{e:?}"),
            Self::LibLoadError(e) => write!(f, "{e:?}"),
            Self::ValidationNotSupported => write!(f, "ValidationNotSupported"),
            Self::Suitability(e) => write!(f, "suitability: {e:?}"),
            Self::NoDevice => write!(f, "No suitable device is found"),
            Self::NoExtension => write!(f, "Some required device extension is not supported"),
            Self::InsufficientSwapchainSupport => {
                write!(f, "The swapchain support is insufficient.")
            }
            Self::InvalidBytecode(len) => write!(
                f,
                "The length of the SPIRV byte code should be a multiple of four, but got {len}"
            ),
            Self::AllocBytecode => write!(f, "Failed to allocate space to store SPIRV byte codes"),
        }
    }
}

impl std::fmt::Display for MainError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MainError::Event(e) => write!(f, "Event loop error: {e}"),
            MainError::NotSupported(not) => write!(f, "Not supported: {not}"),
            MainError::Os(e) => write!(f, "Oeration system: {e}"),
            MainError::Code(code) => write!(f, "{}", vk::ErrorCode::from_raw(*code)),
            MainError::BoxLoadError(e) => write!(f, "Boxed loading error: {e}"),
            MainError::LibLoadError(e) => write!(f, "Library loading error: {e}"),
            MainError::ValidationNotSupported => write!(
                f,
                "The validation layers are requested but are not available."
            ),
            MainError::Suitability(e) => write!(f, "Suitability error: {e}"),
            MainError::NoDevice => write!(f, "No suitable device is found"),
            MainError::NoExtension => write!(f, "Some required device extension is not supported"),
            MainError::InsufficientSwapchainSupport => {
                write!(f, "The swapchain support is insufficient.")
            }
            MainError::InvalidBytecode(len) => write!(
                f,
                "The length of the SPIRV byte code should be a multiple of four, but got {len}"
            ),
            MainError::AllocBytecode => {
                write!(f, "Failed to allocate space to store SPIRV byte codes")
            }
        }
    }
}

impl std::error::Error for MainError {}

impl From<werror::OsError> for MainError {
    fn from(value: werror::OsError) -> Self {
        MainError::Os(value)
    }
}

impl From<werror::NotSupportedError> for MainError {
    fn from(value: werror::NotSupportedError) -> Self {
        MainError::NotSupported(value)
    }
}

impl From<werror::EventLoopError> for MainError {
    fn from(value: werror::EventLoopError) -> Self {
        MainError::Event(value)
    }
}

impl From<vk::ErrorCode> for MainError {
    fn from(value: vk::ErrorCode) -> Self {
        MainError::Code(value.as_raw())
    }
}

impl From<Box<dyn LoaderError>> for MainError {
    fn from(value: Box<dyn LoaderError>) -> Self {
        MainError::BoxLoadError(value)
    }
}

impl From<BytecodeError> for MainError {
    fn from(value: BytecodeError) -> Self {
        match value {
            BytecodeError::Alloc => Self::AllocBytecode,
            BytecodeError::Length(len) => Self::InvalidBytecode(len),
        }
    }
}

#[derive(Debug)]
pub(super) enum SuitabilityError {
    NoQueuefamily,
}

impl std::fmt::Display for SuitabilityError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SuitabilityError::NoQueuefamily => write!(f, "missing required queue families"),
        }
    }
}

impl std::error::Error for SuitabilityError {}