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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Rhyoea. Vulkan FFI bindings for Rust
// Copyright © 2018 Adrien Jeser <adrien@jeser.me>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

//! The result types

use std::error::Error;
use std::ffi::NulError;
use std::fmt;

/// Vulkan command return codes
#[repr(i32)]
#[derive(Debug, Clone, Copy)]
pub enum Code {
    /// Command successfully completed
    Success = 0,

    /// A fence or query has not yet completed
    NotReady = 1,

    /// A wait operation has not completed in the specified time
    Timeout = 2,

    /// An event is signaled
    EventSet = 3,

    /// An event is unsignaled
    EventReset = 4,

    /// A return array was too small for the result
    Incomplete = 5,

    /// A host memory allocation has failed.
    ErrorOutOfHostMemory = -1,

    /// A device memory allocation has failed.
    ErrorOutOfDeviceMemory = -2,

    /// Initialization of an object could not be completed for implementation-specific reasons.
    ErrorInitializationFailed = -3,

    /// The logical or physical device has been lost. See Lost Device
    ErrorDeviceLost = -4,

    /// Mapping of a memory object has failed.
    ErrorMemoryMapFailed = -5,

    /// A requested layer is not present or could not be loaded.
    ErrorLayerNotPresent = -6,

    /// A requested extension is not supported.
    ErrorExtensionNotPresent = -7,

    /// A requested feature is not supported.
    ErrorFeatureNotPresent = -8,

    /// The requested version of Vulkan is not supported by the driver or is otherwise incompatible for implementation-specific reasons.
    ErrorIncompatibleDriver = -9,

    /// Too many objects of the type have already been created.
    ErrorTooManyObjects = -10,

    /// A requested format is not supported on this device.
    ErrorFormatNotSupported = -11,

    /// A pool allocation has failed due to fragmentation of the pool’s memory.
    /// This must only be returned if no attempt to allocate host or device memory was made to accomodate the new allocation.
    /// This should be returned in preference to VK_ERROR_OUT_OF_POOL_MEMORY,
    /// but only if the implementation is certain that the pool allocation failure was due to fragmentation.
    ErrorFragmentedPool = -12,

    /// A pool memory allocation has failed.
    /// This must only be returned if no attempt to allocate host or device memory was made to accomodate the new allocation.
    /// If the failure was definitely due to fragmentation of the pool, VK_ERROR_FRAGMENTED_POOL should be returned instead.
    ErrorOutOfPoolMemory = -1_000_069_000,

    /// An external handle is not a valid handle of the specified type.
    ErrorInvalidExternalHandle = -1_000_072_003,

    /// A surface is no longer available.
    ErrorSurfaceLostKhr = -1_000_000_000,

    /// The requested window is already in use by Vulkan or another API in a manner which prevents it from being used again.
    ErrorNativeWindowInUseKhr = -1_000_000_001,

    /// A swapchain no longer matches the surface properties exactly, but can still be used to present to the surface successfully.
    SuboptimalKhr = 1_000_001_003,

    /// A surface has changed in such a way that it is no longer compatible with the swapchain,
    /// and further presentation requests using the swapchain will fail.
    /// Applications must query the new surface properties and recreate their swapchain if they wish to continue presenting to the surface.
    ErrorOutOfDateKhr = -1_000_001_004,

    /// The display used by a swapchain does not use the same presentable image layout,
    /// or is incompatible in a way that prevents sharing an image.
    ErrorIncompatibleDisplayKhr = -1_000_003_001,

    ///
    ErrorValidationFailedExt = -1_000_011_001,

    /// One or more shaders failed to compile or link.
    ErrorInvalidShaderNv = -1_000_012_000,

    /// A descriptor pool creation has failed due to fragmentation.
    ErrorFragmentationExt = -1_000_161_000,

    ///
    ErrorNotPermittedExt = -1_000_174_001,
}

impl fmt::Display for Code {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::Success => write!(f, "Command successfully completed"),
            Self::NotReady => write!(f, "A fence or query has not yet completed"),
            Self::Timeout => write!(f, "A wait operation has not completed in the specified time"),
            Self::EventSet => write!(f, "An event is signaled"),
            Self::EventReset => write!(f, "An event is unsignaled"),
            Self::Incomplete => write!(f, "A return array was too small for the result"),
            Self::ErrorOutOfHostMemory => write!(f, "A host memory allocation has failed."),
            Self::ErrorOutOfDeviceMemory => write!(f, "A device memory allocation has failed."),
            Self::ErrorInitializationFailed => write!(f, "Initialization of an object could not be completed for implementation-specific reasons."),
            Self::ErrorDeviceLost => write!(f, "The logical or physical device has been lost. See Lost Device"),
            Self::ErrorMemoryMapFailed => write!(f, "Mapping of a memory object has failed."),
            Self::ErrorLayerNotPresent => write!(f, "A requested layer is not present or could not be loaded."),
            Self::ErrorExtensionNotPresent => write!(f, "A requested extension is not supported."),
            Self::ErrorFeatureNotPresent => write!(f, "A requested feature is not supported."),
            Self::ErrorIncompatibleDriver => write!(f, "The requested version of Vulkan is not supported by the driver or is otherwise incompatible for implementation-specific reasons."),
            Self::ErrorTooManyObjects => write!(f, "Too many objects of the type have already been created."),
            Self::ErrorFormatNotSupported => write!(f, "A requested format is not supported on this device."),

            Self::ErrorFragmentedPool => write!(f, "A pool allocation has failed due to fragmentation of the pool's memory.
            This must only be returned if no attempt to allocate host or device memory was made to accomodate the new allocation.
            This should be returned in preference to VK_ERROR_OUT_OF_POOL_MEMORY,
            but only if the implementation is certain that the pool allocation failure was due to fragmentation."),

            Self::ErrorOutOfPoolMemory => write!(f, "A pool memory allocation has failed.
            This must only be returned if no attempt to allocate host or device memory was made to accomodate the new allocation.
            If the failure was definitely due to fragmentation of the pool, VK_ERROR_FRAGMENTED_POOL should be returned instead."),

            Self::ErrorInvalidExternalHandle => write!(f, "An external handle is not a valid handle of the specified type."),
            Self::ErrorSurfaceLostKhr => write!(f, "A surface is no longer available."),
            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."),
            Self::SuboptimalKhr => write!(f, "A swapchain no longer matches the surface properties exactly, but can still be used to present to the surface successfully."),

            Self::ErrorOutOfDateKhr => write!(f, "A surface has changed in such a way that it is no longer compatible with the swapchain,
            and further presentation requests using the swapchain will fail.
            Applications must query the new surface properties and recreate their swapchain if they wish to continue presenting to the surface."),

            Self::ErrorIncompatibleDisplayKhr => write!(f, "
            The display used by a swapchain does not use the same presentable image layout,
            or is incompatible in a way that prevents sharing an image."),
            Self::ErrorValidationFailedExt => write!(f, "ErrorValidationFailedExt"),
            Self::ErrorInvalidShaderNv => write!(f, "One or more shaders failed to compile or link."),
            Self::ErrorFragmentationExt => write!(f, "A descriptor pool creation has failed due to fragmentation."),
            Self::ErrorNotPermittedExt => write!(f, "ErrorNotPermittedExt"),
        }
    }
}

impl Error for Code {
    #[must_use]
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        None
    }
}

/// Error when initialize a application
#[derive(Debug, Clone)]
pub enum ApplicationError {
    /// An interior nul byte was found
    NulError(NulError),
}

impl Error for ApplicationError {
    #[must_use]
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match *self {
            Self::NulError(ref e) => Some(e),
        }
    }
}

impl fmt::Display for ApplicationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::NulError(ref e) => e.fmt(f),
        }
    }
}

impl From<NulError> for ApplicationError {
    #[must_use]
    fn from(error: NulError) -> Self {
        Self::NulError(error)
    }
}

/// Error when initialize a instance
#[derive(Debug, Clone)]
pub enum InstanceError {
    /// Application errors
    Application(ApplicationError),

    /// Vulkan command return codes
    Vulkan(Code),

    /// An interior nul byte was found
    NulError(NulError),

    /// Not found the command
    NotFoundCommand(String),
}

impl Error for InstanceError {
    #[must_use]
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match *self {
            Self::Vulkan(ref e) => Some(e),
            Self::NulError(ref e) => Some(e),
            Self::Application(ref e) => Some(e),
            Self::NotFoundCommand(_) => None,
        }
    }
}

impl fmt::Display for InstanceError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::Vulkan(ref e) => e.fmt(f),
            Self::NulError(ref e) => e.fmt(f),
            Self::Application(ref e) => e.fmt(f),
            Self::NotFoundCommand(ref e) => write!(f, "The {} Vulkan command has not found", e),
        }
    }
}

impl From<NulError> for InstanceError {
    #[must_use]
    fn from(error: NulError) -> Self {
        Self::NulError(error)
    }
}

impl From<ApplicationError> for InstanceError {
    #[must_use]
    fn from(error: ApplicationError) -> Self {
        Self::Application(error)
    }
}

/// Error when initialize a debug
#[derive(Debug, Clone)]
pub enum DebugError {
    /// Vulkan command return codes
    Vulkan(Code),

    /// Instance error
    Instance(InstanceError),

    /// An interior nul byte was found
    NulError(NulError),
}

impl Error for DebugError {
    #[must_use]
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match *self {
            Self::Vulkan(ref e) => Some(e),
            Self::Instance(ref e) => Some(e),
            Self::NulError(ref e) => Some(e),
        }
    }
}

impl fmt::Display for DebugError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::Vulkan(ref e) => e.fmt(f),
            Self::Instance(ref e) => e.fmt(f),
            Self::NulError(ref e) => e.fmt(f),
        }
    }
}

impl From<NulError> for DebugError {
    #[must_use]
    fn from(error: NulError) -> Self {
        Self::NulError(error)
    }
}

impl From<InstanceError> for DebugError {
    #[must_use]
    fn from(error: InstanceError) -> Self {
        Self::Instance(error)
    }
}

/// Error when building a instance
#[derive(Debug, Clone)]
pub enum BuilderError {
    /// Instance error
    Instance(InstanceError),

    /// Debug error
    Debug(DebugError),
}

impl Error for BuilderError {
    #[must_use]
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match *self {
            Self::Instance(ref e) => Some(e),
            Self::Debug(ref e) => Some(e),
        }
    }
}

impl fmt::Display for BuilderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::Instance(ref e) => e.fmt(f),
            Self::Debug(ref e) => e.fmt(f),
        }
    }
}

impl From<InstanceError> for BuilderError {
    #[must_use]
    fn from(error: InstanceError) -> Self {
        Self::Instance(error)
    }
}

impl From<DebugError> for BuilderError {
    #[must_use]
    fn from(error: DebugError) -> Self {
        Self::Debug(error)
    }
}