fyrox_graphics/
error.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Contains all possible errors that may occur during rendering, initialization of
22//! renderer structures, or GAPI.
23
24use std::{
25    error::Error,
26    ffi::NulError,
27    fmt::{Display, Formatter},
28};
29
30/// Set of possible renderer errors.
31#[derive(Debug)]
32pub enum FrameworkError {
33    /// Compilation of a shader has failed.
34    ShaderCompilationFailed {
35        /// Name of shader.
36        shader_name: String,
37        /// Compilation error message.
38        error_message: String,
39    },
40    /// Means that shader link stage failed, exact reason is inside `error_message`
41    ShaderLinkingFailed {
42        /// Name of shader.
43        shader_name: String,
44        /// Linking error message.
45        error_message: String,
46    },
47    /// Shader source contains invalid characters.
48    FaultyShaderSource,
49    /// There is no such shader uniform (could be optimized out).
50    UnableToFindShaderUniform(String),
51    /// There is no such shader uniform block.
52    UnableToFindShaderUniformBlock(String),
53    /// Texture has invalid data - insufficient size.
54    InvalidTextureData {
55        /// Expected data size in bytes.
56        expected_data_size: usize,
57        /// Actual data size in bytes.
58        actual_data_size: usize,
59    },
60    /// None variant was passed as texture data, but engine does not support it.
61    EmptyTextureData,
62    /// Means that you tried to draw element range from GeometryBuffer that
63    /// does not have enough elements.
64    InvalidElementRange {
65        /// First index.
66        start: usize,
67        /// Last index.
68        end: usize,
69        /// Total amount of triangles.
70        total: usize,
71    },
72    /// Means that attribute descriptor tries to define an attribute that does
73    /// not exists in vertex, or it does not match size. For example you have vertex:
74    ///   pos: float2,
75    ///   normal: float3
76    /// But you described second attribute as Float4, then you'll get this error.
77    InvalidAttributeDescriptor,
78    /// Framebuffer is invalid.
79    InvalidFrameBuffer,
80    /// OpenGL failed to construct framebuffer.
81    FailedToConstructFBO,
82    /// Custom error. Usually used for internal errors.
83    Custom(String),
84    /// Graphics server disconnected.
85    GraphicsServerUnavailable,
86}
87
88impl Display for FrameworkError {
89    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
90        match self {
91            FrameworkError::ShaderCompilationFailed {
92                shader_name,
93                error_message,
94            } => {
95                write!(
96                    f,
97                    "Compilation of \"{shader_name}\" shader has failed: {error_message}",
98                )
99            }
100            FrameworkError::ShaderLinkingFailed {
101                shader_name,
102                error_message,
103            } => {
104                write!(
105                    f,
106                    "Linking shader \"{shader_name}\" failed: {error_message}",
107                )
108            }
109            FrameworkError::FaultyShaderSource => {
110                write!(f, "Shader source contains invalid characters")
111            }
112            FrameworkError::UnableToFindShaderUniform(v) => {
113                write!(f, "There is no such shader uniform: {v}")
114            }
115            FrameworkError::UnableToFindShaderUniformBlock(v) => {
116                write!(f, "There is no such shader uniform block: {v}")
117            }
118            FrameworkError::InvalidTextureData {
119                expected_data_size,
120                actual_data_size,
121            } => {
122                write!(
123                    f,
124                    "Texture has invalid data (insufficent size): \
125                expected {expected_data_size}, actual: {actual_data_size}",
126                )
127            }
128            FrameworkError::EmptyTextureData => {
129                write!(
130                    f,
131                    "None variant was passed as texture data, but engine does not support it."
132                )
133            }
134            FrameworkError::InvalidElementRange { start, end, total } => {
135                write!(
136                    f,
137                    "Tried to draw element from GeometryBuffer that does not have enough \
138                    elements: start: {start}, end: {end}, total: {total}",
139                )
140            }
141            FrameworkError::InvalidAttributeDescriptor => {
142                write!(
143                    f,
144                    "An attribute descriptor tried to define an attribute that \
145                does not exist in vertex or doesn't match size."
146                )
147            }
148            FrameworkError::InvalidFrameBuffer => {
149                write!(f, "Framebuffer is invalid")
150            }
151            FrameworkError::FailedToConstructFBO => {
152                write!(f, "OpenGL failed to construct framebuffer.")
153            }
154            FrameworkError::Custom(v) => {
155                write!(f, "Custom error: {v}")
156            }
157            FrameworkError::GraphicsServerUnavailable => {
158                write!(f, "Graphics server disconnected.")
159            }
160        }
161    }
162}
163
164impl From<NulError> for FrameworkError {
165    fn from(_: NulError) -> Self {
166        Self::FaultyShaderSource
167    }
168}
169
170#[cfg(not(target_arch = "wasm32"))]
171impl From<glutin::error::Error> for FrameworkError {
172    fn from(err: glutin::error::Error) -> Self {
173        Self::Custom(format!("{err:?}"))
174    }
175}
176
177impl From<String> for FrameworkError {
178    fn from(v: String) -> Self {
179        Self::Custom(v)
180    }
181}
182
183impl From<Box<dyn Error>> for FrameworkError {
184    fn from(e: Box<dyn Error>) -> Self {
185        Self::Custom(format!("{e:?}"))
186    }
187}