Skip to main content

rhyoea/
result.rs

1// Rhyoea. Vulkan FFI bindings for Rust
2// Copyright © 2018 Adrien Jeser <adrien@jeser.me>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Affero General Public License as
6// published by the Free Software Foundation, either version 3 of the
7// License, or (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU Affero General Public License for more details.
13//
14// You should have received a copy of the GNU Affero General Public License
15// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17//! The result types
18
19use std::error::Error;
20use std::ffi::NulError;
21use std::fmt;
22
23/// Vulkan command return codes
24#[repr(i32)]
25#[derive(Debug, Clone, Copy)]
26pub enum Code {
27    /// Command successfully completed
28    Success = 0,
29
30    /// A fence or query has not yet completed
31    NotReady = 1,
32
33    /// A wait operation has not completed in the specified time
34    Timeout = 2,
35
36    /// An event is signaled
37    EventSet = 3,
38
39    /// An event is unsignaled
40    EventReset = 4,
41
42    /// A return array was too small for the result
43    Incomplete = 5,
44
45    /// A host memory allocation has failed.
46    ErrorOutOfHostMemory = -1,
47
48    /// A device memory allocation has failed.
49    ErrorOutOfDeviceMemory = -2,
50
51    /// Initialization of an object could not be completed for implementation-specific reasons.
52    ErrorInitializationFailed = -3,
53
54    /// The logical or physical device has been lost. See Lost Device
55    ErrorDeviceLost = -4,
56
57    /// Mapping of a memory object has failed.
58    ErrorMemoryMapFailed = -5,
59
60    /// A requested layer is not present or could not be loaded.
61    ErrorLayerNotPresent = -6,
62
63    /// A requested extension is not supported.
64    ErrorExtensionNotPresent = -7,
65
66    /// A requested feature is not supported.
67    ErrorFeatureNotPresent = -8,
68
69    /// The requested version of Vulkan is not supported by the driver or is otherwise incompatible for implementation-specific reasons.
70    ErrorIncompatibleDriver = -9,
71
72    /// Too many objects of the type have already been created.
73    ErrorTooManyObjects = -10,
74
75    /// A requested format is not supported on this device.
76    ErrorFormatNotSupported = -11,
77
78    /// A pool allocation has failed due to fragmentation of the pool’s memory.
79    /// This must only be returned if no attempt to allocate host or device memory was made to accomodate the new allocation.
80    /// This should be returned in preference to VK_ERROR_OUT_OF_POOL_MEMORY,
81    /// but only if the implementation is certain that the pool allocation failure was due to fragmentation.
82    ErrorFragmentedPool = -12,
83
84    /// A pool memory allocation has failed.
85    /// This must only be returned if no attempt to allocate host or device memory was made to accomodate the new allocation.
86    /// If the failure was definitely due to fragmentation of the pool, VK_ERROR_FRAGMENTED_POOL should be returned instead.
87    ErrorOutOfPoolMemory = -1_000_069_000,
88
89    /// An external handle is not a valid handle of the specified type.
90    ErrorInvalidExternalHandle = -1_000_072_003,
91
92    /// A surface is no longer available.
93    ErrorSurfaceLostKhr = -1_000_000_000,
94
95    /// The requested window is already in use by Vulkan or another API in a manner which prevents it from being used again.
96    ErrorNativeWindowInUseKhr = -1_000_000_001,
97
98    /// A swapchain no longer matches the surface properties exactly, but can still be used to present to the surface successfully.
99    SuboptimalKhr = 1_000_001_003,
100
101    /// A surface has changed in such a way that it is no longer compatible with the swapchain,
102    /// and further presentation requests using the swapchain will fail.
103    /// Applications must query the new surface properties and recreate their swapchain if they wish to continue presenting to the surface.
104    ErrorOutOfDateKhr = -1_000_001_004,
105
106    /// The display used by a swapchain does not use the same presentable image layout,
107    /// or is incompatible in a way that prevents sharing an image.
108    ErrorIncompatibleDisplayKhr = -1_000_003_001,
109
110    ///
111    ErrorValidationFailedExt = -1_000_011_001,
112
113    /// One or more shaders failed to compile or link.
114    ErrorInvalidShaderNv = -1_000_012_000,
115
116    /// A descriptor pool creation has failed due to fragmentation.
117    ErrorFragmentationExt = -1_000_161_000,
118
119    ///
120    ErrorNotPermittedExt = -1_000_174_001,
121}
122
123impl fmt::Display for Code {
124    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125        match *self {
126            Self::Success => write!(f, "Command successfully completed"),
127            Self::NotReady => write!(f, "A fence or query has not yet completed"),
128            Self::Timeout => write!(f, "A wait operation has not completed in the specified time"),
129            Self::EventSet => write!(f, "An event is signaled"),
130            Self::EventReset => write!(f, "An event is unsignaled"),
131            Self::Incomplete => write!(f, "A return array was too small for the result"),
132            Self::ErrorOutOfHostMemory => write!(f, "A host memory allocation has failed."),
133            Self::ErrorOutOfDeviceMemory => write!(f, "A device memory allocation has failed."),
134            Self::ErrorInitializationFailed => write!(f, "Initialization of an object could not be completed for implementation-specific reasons."),
135            Self::ErrorDeviceLost => write!(f, "The logical or physical device has been lost. See Lost Device"),
136            Self::ErrorMemoryMapFailed => write!(f, "Mapping of a memory object has failed."),
137            Self::ErrorLayerNotPresent => write!(f, "A requested layer is not present or could not be loaded."),
138            Self::ErrorExtensionNotPresent => write!(f, "A requested extension is not supported."),
139            Self::ErrorFeatureNotPresent => write!(f, "A requested feature is not supported."),
140            Self::ErrorIncompatibleDriver => write!(f, "The requested version of Vulkan is not supported by the driver or is otherwise incompatible for implementation-specific reasons."),
141            Self::ErrorTooManyObjects => write!(f, "Too many objects of the type have already been created."),
142            Self::ErrorFormatNotSupported => write!(f, "A requested format is not supported on this device."),
143
144            Self::ErrorFragmentedPool => write!(f, "A pool allocation has failed due to fragmentation of the pool's memory.
145            This must only be returned if no attempt to allocate host or device memory was made to accomodate the new allocation.
146            This should be returned in preference to VK_ERROR_OUT_OF_POOL_MEMORY,
147            but only if the implementation is certain that the pool allocation failure was due to fragmentation."),
148
149            Self::ErrorOutOfPoolMemory => write!(f, "A pool memory allocation has failed.
150            This must only be returned if no attempt to allocate host or device memory was made to accomodate the new allocation.
151            If the failure was definitely due to fragmentation of the pool, VK_ERROR_FRAGMENTED_POOL should be returned instead."),
152
153            Self::ErrorInvalidExternalHandle => write!(f, "An external handle is not a valid handle of the specified type."),
154            Self::ErrorSurfaceLostKhr => write!(f, "A surface is no longer available."),
155            Self::ErrorNativeWindowInUseKhr => write!(f, "The requested window is already in use by Vulkan or another API in a manner which prevents it from being used again."),
156            Self::SuboptimalKhr => write!(f, "A swapchain no longer matches the surface properties exactly, but can still be used to present to the surface successfully."),
157
158            Self::ErrorOutOfDateKhr => write!(f, "A surface has changed in such a way that it is no longer compatible with the swapchain,
159            and further presentation requests using the swapchain will fail.
160            Applications must query the new surface properties and recreate their swapchain if they wish to continue presenting to the surface."),
161
162            Self::ErrorIncompatibleDisplayKhr => write!(f, "
163            The display used by a swapchain does not use the same presentable image layout,
164            or is incompatible in a way that prevents sharing an image."),
165            Self::ErrorValidationFailedExt => write!(f, "ErrorValidationFailedExt"),
166            Self::ErrorInvalidShaderNv => write!(f, "One or more shaders failed to compile or link."),
167            Self::ErrorFragmentationExt => write!(f, "A descriptor pool creation has failed due to fragmentation."),
168            Self::ErrorNotPermittedExt => write!(f, "ErrorNotPermittedExt"),
169        }
170    }
171}
172
173impl Error for Code {
174    #[must_use]
175    fn source(&self) -> Option<&(dyn Error + 'static)> {
176        None
177    }
178}
179
180/// Error when initialize a application
181#[derive(Debug, Clone)]
182pub enum ApplicationError {
183    /// An interior nul byte was found
184    NulError(NulError),
185}
186
187impl Error for ApplicationError {
188    #[must_use]
189    fn source(&self) -> Option<&(dyn Error + 'static)> {
190        match *self {
191            Self::NulError(ref e) => Some(e),
192        }
193    }
194}
195
196impl fmt::Display for ApplicationError {
197    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
198        match *self {
199            Self::NulError(ref e) => e.fmt(f),
200        }
201    }
202}
203
204impl From<NulError> for ApplicationError {
205    #[must_use]
206    fn from(error: NulError) -> Self {
207        Self::NulError(error)
208    }
209}
210
211/// Error when initialize a instance
212#[derive(Debug, Clone)]
213pub enum InstanceError {
214    /// Application errors
215    Application(ApplicationError),
216
217    /// Vulkan command return codes
218    Vulkan(Code),
219
220    /// An interior nul byte was found
221    NulError(NulError),
222
223    /// Not found the command
224    NotFoundCommand(String),
225}
226
227impl Error for InstanceError {
228    #[must_use]
229    fn source(&self) -> Option<&(dyn Error + 'static)> {
230        match *self {
231            Self::Vulkan(ref e) => Some(e),
232            Self::NulError(ref e) => Some(e),
233            Self::Application(ref e) => Some(e),
234            Self::NotFoundCommand(_) => None,
235        }
236    }
237}
238
239impl fmt::Display for InstanceError {
240    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
241        match *self {
242            Self::Vulkan(ref e) => e.fmt(f),
243            Self::NulError(ref e) => e.fmt(f),
244            Self::Application(ref e) => e.fmt(f),
245            Self::NotFoundCommand(ref e) => write!(f, "The {} Vulkan command has not found", e),
246        }
247    }
248}
249
250impl From<NulError> for InstanceError {
251    #[must_use]
252    fn from(error: NulError) -> Self {
253        Self::NulError(error)
254    }
255}
256
257impl From<ApplicationError> for InstanceError {
258    #[must_use]
259    fn from(error: ApplicationError) -> Self {
260        Self::Application(error)
261    }
262}
263
264/// Error when initialize a debug
265#[derive(Debug, Clone)]
266pub enum DebugError {
267    /// Vulkan command return codes
268    Vulkan(Code),
269
270    /// Instance error
271    Instance(InstanceError),
272
273    /// An interior nul byte was found
274    NulError(NulError),
275}
276
277impl Error for DebugError {
278    #[must_use]
279    fn source(&self) -> Option<&(dyn Error + 'static)> {
280        match *self {
281            Self::Vulkan(ref e) => Some(e),
282            Self::Instance(ref e) => Some(e),
283            Self::NulError(ref e) => Some(e),
284        }
285    }
286}
287
288impl fmt::Display for DebugError {
289    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
290        match *self {
291            Self::Vulkan(ref e) => e.fmt(f),
292            Self::Instance(ref e) => e.fmt(f),
293            Self::NulError(ref e) => e.fmt(f),
294        }
295    }
296}
297
298impl From<NulError> for DebugError {
299    #[must_use]
300    fn from(error: NulError) -> Self {
301        Self::NulError(error)
302    }
303}
304
305impl From<InstanceError> for DebugError {
306    #[must_use]
307    fn from(error: InstanceError) -> Self {
308        Self::Instance(error)
309    }
310}
311
312/// Error when building a instance
313#[derive(Debug, Clone)]
314pub enum BuilderError {
315    /// Instance error
316    Instance(InstanceError),
317
318    /// Debug error
319    Debug(DebugError),
320}
321
322impl Error for BuilderError {
323    #[must_use]
324    fn source(&self) -> Option<&(dyn Error + 'static)> {
325        match *self {
326            Self::Instance(ref e) => Some(e),
327            Self::Debug(ref e) => Some(e),
328        }
329    }
330}
331
332impl fmt::Display for BuilderError {
333    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
334        match *self {
335            Self::Instance(ref e) => e.fmt(f),
336            Self::Debug(ref e) => e.fmt(f),
337        }
338    }
339}
340
341impl From<InstanceError> for BuilderError {
342    #[must_use]
343    fn from(error: InstanceError) -> Self {
344        Self::Instance(error)
345    }
346}
347
348impl From<DebugError> for BuilderError {
349    #[must_use]
350    fn from(error: DebugError) -> Self {
351        Self::Debug(error)
352    }
353}