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
#![doc(html_logo_url = "https://raw.githubusercontent.com/vulkano-rs/vulkano/master/logo.png")]
#![allow(dead_code)]
#![allow(unused_variables)]
extern crate crossbeam;
extern crate fnv;
#[macro_use]
extern crate lazy_static;
extern crate shared_library;
extern crate smallvec;
extern crate vk_sys as vk;
pub extern crate half;
#[macro_use]
mod tests;
#[macro_use]
mod extensions;
mod features;
mod version;
pub mod buffer;
pub mod command_buffer;
pub mod descriptor;
pub mod device;
pub mod format;
#[macro_use]
pub mod framebuffer;
pub mod image;
pub mod instance;
pub mod memory;
pub mod pipeline;
pub mod query;
pub mod sampler;
pub mod swapchain;
pub mod sync;
use std::error;
use std::fmt;
use std::ops::Deref;
use std::sync::Arc;
use std::sync::MutexGuard;
pub unsafe trait SafeDeref: Deref {}
unsafe impl<'a, T: ?Sized> SafeDeref for &'a T {
}
unsafe impl<T: ?Sized> SafeDeref for Arc<T> {
}
unsafe impl<T: ?Sized> SafeDeref for Box<T> {
}
pub trait VulkanHandle {
fn value(&self) -> u64;
}
impl VulkanHandle for usize {
#[inline]
fn value(&self) -> u64 { *self as u64 }
}
impl VulkanHandle for u64 {
#[inline]
fn value(&self) -> u64 { *self }
}
pub unsafe trait VulkanObject {
type Object: VulkanHandle;
const TYPE: vk::ObjectType;
fn internal_object(&self) -> Self::Object;
}
pub unsafe trait SynchronizedVulkanObject {
type Object: VulkanHandle;
fn internal_object_guard(&self) -> MutexGuard<Self::Object>;
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum OomError {
OutOfHostMemory,
OutOfDeviceMemory,
}
impl error::Error for OomError {
#[inline]
fn description(&self) -> &str {
match *self {
OomError::OutOfHostMemory => "no memory available on the host",
OomError::OutOfDeviceMemory => "no memory available on the graphical device",
}
}
}
impl fmt::Display for OomError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "{}", error::Error::description(self))
}
}
impl From<Error> for OomError {
#[inline]
fn from(err: Error) -> OomError {
match err {
Error::OutOfHostMemory => OomError::OutOfHostMemory,
Error::OutOfDeviceMemory => OomError::OutOfDeviceMemory,
_ => panic!("unexpected error: {:?}", err),
}
}
}
#[derive(Debug, Copy, Clone)]
#[repr(u32)]
enum Success {
Success = vk::SUCCESS,
NotReady = vk::NOT_READY,
Timeout = vk::TIMEOUT,
EventSet = vk::EVENT_SET,
EventReset = vk::EVENT_RESET,
Incomplete = vk::INCOMPLETE,
Suboptimal = vk::SUBOPTIMAL_KHR,
}
#[derive(Debug, Copy, Clone)]
#[repr(u32)]
pub(crate) enum Error {
OutOfHostMemory = vk::ERROR_OUT_OF_HOST_MEMORY,
OutOfDeviceMemory = vk::ERROR_OUT_OF_DEVICE_MEMORY,
InitializationFailed = vk::ERROR_INITIALIZATION_FAILED,
DeviceLost = vk::ERROR_DEVICE_LOST,
MemoryMapFailed = vk::ERROR_MEMORY_MAP_FAILED,
LayerNotPresent = vk::ERROR_LAYER_NOT_PRESENT,
ExtensionNotPresent = vk::ERROR_EXTENSION_NOT_PRESENT,
FeatureNotPresent = vk::ERROR_FEATURE_NOT_PRESENT,
IncompatibleDriver = vk::ERROR_INCOMPATIBLE_DRIVER,
TooManyObjects = vk::ERROR_TOO_MANY_OBJECTS,
FormatNotSupported = vk::ERROR_FORMAT_NOT_SUPPORTED,
SurfaceLost = vk::ERROR_SURFACE_LOST_KHR,
NativeWindowInUse = vk::ERROR_NATIVE_WINDOW_IN_USE_KHR,
OutOfDate = vk::ERROR_OUT_OF_DATE_KHR,
IncompatibleDisplay = vk::ERROR_INCOMPATIBLE_DISPLAY_KHR,
ValidationFailed = vk::ERROR_VALIDATION_FAILED_EXT,
OutOfPoolMemory = vk::ERROR_OUT_OF_POOL_MEMORY_KHR,
}
fn check_errors(result: vk::Result) -> Result<Success, Error> {
match result {
vk::SUCCESS => Ok(Success::Success),
vk::NOT_READY => Ok(Success::NotReady),
vk::TIMEOUT => Ok(Success::Timeout),
vk::EVENT_SET => Ok(Success::EventSet),
vk::EVENT_RESET => Ok(Success::EventReset),
vk::INCOMPLETE => Ok(Success::Incomplete),
vk::ERROR_OUT_OF_HOST_MEMORY => Err(Error::OutOfHostMemory),
vk::ERROR_OUT_OF_DEVICE_MEMORY => Err(Error::OutOfDeviceMemory),
vk::ERROR_INITIALIZATION_FAILED => Err(Error::InitializationFailed),
vk::ERROR_DEVICE_LOST => Err(Error::DeviceLost),
vk::ERROR_MEMORY_MAP_FAILED => Err(Error::MemoryMapFailed),
vk::ERROR_LAYER_NOT_PRESENT => Err(Error::LayerNotPresent),
vk::ERROR_EXTENSION_NOT_PRESENT => Err(Error::ExtensionNotPresent),
vk::ERROR_FEATURE_NOT_PRESENT => Err(Error::FeatureNotPresent),
vk::ERROR_INCOMPATIBLE_DRIVER => Err(Error::IncompatibleDriver),
vk::ERROR_TOO_MANY_OBJECTS => Err(Error::TooManyObjects),
vk::ERROR_FORMAT_NOT_SUPPORTED => Err(Error::FormatNotSupported),
vk::ERROR_SURFACE_LOST_KHR => Err(Error::SurfaceLost),
vk::ERROR_NATIVE_WINDOW_IN_USE_KHR => Err(Error::NativeWindowInUse),
vk::SUBOPTIMAL_KHR => Ok(Success::Suboptimal),
vk::ERROR_OUT_OF_DATE_KHR => Err(Error::OutOfDate),
vk::ERROR_INCOMPATIBLE_DISPLAY_KHR => Err(Error::IncompatibleDisplay),
vk::ERROR_VALIDATION_FAILED_EXT => Err(Error::ValidationFailed),
vk::ERROR_OUT_OF_POOL_MEMORY_KHR => Err(Error::OutOfPoolMemory),
vk::ERROR_INVALID_SHADER_NV => panic!("Vulkan function returned \
VK_ERROR_INVALID_SHADER_NV"),
c => unreachable!("Unexpected error code returned by Vulkan: {}", c),
}
}