use std::error;
use std::ffi::CStr;
use std::fmt;
use std::mem::MaybeUninit;
use std::os::raw::c_void;
use std::panic;
use std::ptr;
use std::sync::Arc;
use instance::Instance;
use check_errors;
use vk;
use vk::{Bool32, DebugUtilsMessengerCallbackDataEXT};
use Error;
use VulkanObject;
#[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::DebugUtilsMessengerEXT,
user_callback: Box<Box<dyn Fn(&Message) + Send>>,
}
impl DebugCallback {
pub fn new<F>(
instance: &Arc<Instance>,
severity: MessageSeverity,
ty: MessageType,
user_callback: F,
) -> Result<DebugCallback, DebugCallbackCreationError>
where
F: Fn(&Message) + 'static + Send + panic::RefUnwindSafe,
{
if !instance.loaded_extensions().ext_debug_utils {
return Err(DebugCallbackCreationError::MissingExtension);
}
let user_callback = Box::new(Box::new(user_callback) as Box<_>);
extern "system" fn callback(
severity: vk::DebugUtilsMessageSeverityFlagsEXT,
ty: vk::DebugUtilsMessageTypeFlagsEXT,
callback_data: *const DebugUtilsMessengerCallbackDataEXT,
user_data: *mut c_void,
) -> Bool32 {
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((*callback_data).pMessageIdName)
.to_str()
.expect("debug callback message not utf-8");
let description = CStr::from_ptr((*callback_data).pMessage)
.to_str()
.expect("debug callback message not utf-8");
let message = Message {
severity: MessageSeverity {
information: (severity & vk::DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT)
!= 0,
warning: (severity & vk::DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) != 0,
error: (severity & vk::DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0,
verbose: (severity & vk::DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) != 0,
},
ty: MessageType {
general: (ty & vk::DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) != 0,
validation: (ty & vk::DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) != 0,
performance: (ty & vk::DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) != 0,
},
layer_prefix,
description,
};
let _ = panic::catch_unwind(panic::AssertUnwindSafe(move || {
user_callback(&message);
}));
vk::FALSE
}
}
let severity = {
let mut flags = 0;
if severity.information {
flags |= vk::DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
}
if severity.warning {
flags |= vk::DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
}
if severity.error {
flags |= vk::DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
}
if severity.verbose {
flags |= vk::DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT;
}
flags
};
let ty = {
let mut flags = 0;
if ty.general {
flags |= vk::DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT;
}
if ty.validation {
flags |= vk::DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
}
if ty.performance {
flags |= vk::DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
}
flags
};
let infos = vk::DebugUtilsMessengerCreateInfoEXT {
sType: vk::STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
pNext: ptr::null(),
flags: 0,
messageSeverity: severity,
messageType: ty,
pfnUserCallback: 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 = MaybeUninit::uninit();
check_errors(vk.CreateDebugUtilsMessengerEXT(
instance.internal_object(),
&infos,
ptr::null(),
output.as_mut_ptr(),
))?;
output.assume_init()
};
Ok(DebugCallback {
instance: instance.clone(),
debug_report_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,
MessageSeverity::errors_and_warnings(),
MessageType::general(),
user_callback,
)
}
}
impl Drop for DebugCallback {
#[inline]
fn drop(&mut self) {
unsafe {
let vk = self.instance.pointers();
vk.DestroyDebugUtilsMessengerEXT(
self.instance.internal_object(),
self.debug_report_callback,
ptr::null(),
);
}
}
}
pub struct Message<'a> {
pub severity: MessageSeverity,
pub ty: MessageType,
pub layer_prefix: &'a str,
pub description: &'a str,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct MessageSeverity {
pub error: bool,
pub warning: bool,
pub information: bool,
pub verbose: bool,
}
impl MessageSeverity {
#[inline]
pub fn errors() -> MessageSeverity {
MessageSeverity {
error: true,
..MessageSeverity::none()
}
}
#[inline]
pub fn errors_and_warnings() -> MessageSeverity {
MessageSeverity {
error: true,
warning: true,
..MessageSeverity::none()
}
}
#[inline]
pub fn none() -> MessageSeverity {
MessageSeverity {
error: false,
warning: false,
information: false,
verbose: false,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct MessageType {
pub general: bool,
pub validation: bool,
pub performance: bool,
}
impl MessageType {
#[inline]
pub fn general() -> MessageType {
MessageType {
general: true,
validation: false,
performance: false,
}
}
#[inline]
pub fn all() -> MessageType {
MessageType {
general: true,
validation: true,
performance: true,
}
}
#[inline]
pub fn none() -> MessageType {
MessageType {
general: false,
validation: false,
performance: false,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum DebugCallbackCreationError {
MissingExtension,
}
impl error::Error for DebugCallbackCreationError {}
impl fmt::Display for DebugCallbackCreationError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "{}", match *self {
DebugCallbackCreationError::MissingExtension => {
"the `EXT_debug_report` extension was not enabled"
}
})
}
}
impl From<Error> for DebugCallbackCreationError {
#[inline]
fn from(err: Error) -> DebugCallbackCreationError {
panic!("unexpected error: {:?}", err)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::thread;
#[test]
fn ensure_sendable() {
let instance = instance!();
let severity = MessageSeverity::none();
let ty = MessageType::all();
let callback = DebugCallback::new(&instance, severity, ty, |_| {});
thread::spawn(move || {
let _ = callback;
});
}
}