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
use std::error;
use std::ffi::CStr;
use std::fmt;
use std::mem;
use std::os::raw::{c_char, c_void};
use std::panic;
use std::ptr;
use std::sync::Arc;
use instance::Instance;
use Error;
use VulkanObject;
use check_errors;
use vk;
#[must_use = "The DebugCallback object must be kept alive for as long as you want your callback \
to be called"]
pub struct DebugCallback {
instance: Arc<Instance>,
debug_report_callback: vk::DebugReportCallbackEXT,
user_callback: Box<Box<dyn Fn(&Message)>>,
}
impl DebugCallback {
pub fn new<F>(instance: &Arc<Instance>, messages: MessageTypes, user_callback: F)
-> Result<DebugCallback, DebugCallbackCreationError>
where F: Fn(&Message) + 'static + Send + panic::RefUnwindSafe
{
if !instance.loaded_extensions().ext_debug_report {
return Err(DebugCallbackCreationError::MissingExtension);
}
let user_callback = Box::new(Box::new(user_callback) as Box<_>);
extern "system" fn callback(ty: vk::DebugReportFlagsEXT, _: vk::DebugReportObjectTypeEXT,
_: u64, _: usize, _: i32, layer_prefix: *const c_char,
description: *const c_char, user_data: *mut c_void)
-> u32 {
unsafe {
let user_callback = user_data as *mut Box<dyn Fn()> as *const _;
let user_callback: &Box<dyn Fn(&Message)> = &*user_callback;
let layer_prefix = CStr::from_ptr(layer_prefix)
.to_str()
.expect("debug callback message not utf-8");
let description = CStr::from_ptr(description)
.to_str()
.expect("debug callback message not utf-8");
let message = Message {
ty: MessageTypes {
information: (ty & vk::DEBUG_REPORT_INFORMATION_BIT_EXT) != 0,
warning: (ty & vk::DEBUG_REPORT_WARNING_BIT_EXT) != 0,
performance_warning: (ty & vk::DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) !=
0,
error: (ty & vk::DEBUG_REPORT_ERROR_BIT_EXT) != 0,
debug: (ty & vk::DEBUG_REPORT_DEBUG_BIT_EXT) != 0,
},
layer_prefix: layer_prefix,
description: description,
};
let _ = panic::catch_unwind(panic::AssertUnwindSafe(move || {
user_callback(&message);
}));
vk::FALSE
}
}
let flags = {
let mut flags = 0;
if messages.information {
flags |= vk::DEBUG_REPORT_INFORMATION_BIT_EXT;
}
if messages.warning {
flags |= vk::DEBUG_REPORT_WARNING_BIT_EXT;
}
if messages.performance_warning {
flags |= vk::DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
}
if messages.error {
flags |= vk::DEBUG_REPORT_ERROR_BIT_EXT;
}
if messages.debug {
flags |= vk::DEBUG_REPORT_DEBUG_BIT_EXT;
}
flags
};
let infos = vk::DebugReportCallbackCreateInfoEXT {
sType: vk::STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT,
pNext: ptr::null(),
flags: flags,
pfnCallback: callback,
pUserData: &*user_callback as &Box<_> as *const Box<_> as *const c_void as *mut _,
};
let vk = instance.pointers();
let debug_report_callback = unsafe {
let mut output = mem::uninitialized();
check_errors(vk.CreateDebugReportCallbackEXT(instance.internal_object(),
&infos,
ptr::null(),
&mut output))?;
output
};
Ok(DebugCallback {
instance: instance.clone(),
debug_report_callback: debug_report_callback,
user_callback: user_callback,
})
}
#[inline]
pub fn errors_and_warnings<F>(instance: &Arc<Instance>, user_callback: F)
-> Result<DebugCallback, DebugCallbackCreationError>
where F: Fn(&Message) + Send + 'static + panic::RefUnwindSafe
{
DebugCallback::new(instance, MessageTypes::errors_and_warnings(), user_callback)
}
}
impl Drop for DebugCallback {
#[inline]
fn drop(&mut self) {
unsafe {
let vk = self.instance.pointers();
vk.DestroyDebugReportCallbackEXT(self.instance.internal_object(),
self.debug_report_callback,
ptr::null());
}
}
}
pub struct Message<'a> {
pub ty: MessageTypes,
pub layer_prefix: &'a str,
pub description: &'a str,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct MessageTypes {
pub error: bool,
pub warning: bool,
pub performance_warning: bool,
pub information: bool,
pub debug: bool,
}
impl MessageTypes {
#[inline]
pub fn errors() -> MessageTypes {
MessageTypes {
error: true,
..MessageTypes::none()
}
}
#[inline]
pub fn errors_and_warnings() -> MessageTypes {
MessageTypes {
error: true,
warning: true,
performance_warning: true,
..MessageTypes::none()
}
}
#[inline]
pub fn none() -> MessageTypes {
MessageTypes {
error: false,
warning: false,
performance_warning: false,
information: false,
debug: false,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum DebugCallbackCreationError {
MissingExtension,
}
impl error::Error for DebugCallbackCreationError {
#[inline]
fn description(&self) -> &str {
match *self {
DebugCallbackCreationError::MissingExtension =>
"the `EXT_debug_report` extension was not enabled",
}
}
}
impl fmt::Display for DebugCallbackCreationError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "{}", error::Error::description(self))
}
}
impl From<Error> for DebugCallbackCreationError {
#[inline]
fn from(err: Error) -> DebugCallbackCreationError {
panic!("unexpected error: {:?}", err)
}
}