vulkan debug print on debug builds

This commit is contained in:
Alex Janka 2023-10-17 11:22:05 +11:00
parent b5347ce344
commit 68bb36d92c

View file

@ -32,10 +32,45 @@ const VERTICES: [Vertex; 3] = [Vertex(-1.0, -1.0), Vertex(3.0, -1.0), Vertex(-1.
pub struct RendererBackendManager { pub struct RendererBackendManager {
entry: Entry, entry: Entry,
instance: Instance, instance: Instance,
#[cfg(debug_assertions)]
debug: VulkanDebug,
}
#[cfg(debug_assertions)]
struct VulkanDebug {
debug_utils_loader: DebugUtils, debug_utils_loader: DebugUtils,
debug_call_back: vk::DebugUtilsMessengerEXT, debug_call_back: vk::DebugUtilsMessengerEXT,
} }
#[cfg(debug_assertions)]
impl VulkanDebug {
fn new(entry: &Entry, instance: &Instance) -> Self {
let debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder()
.message_severity(
vk::DebugUtilsMessageSeverityFlagsEXT::ERROR
| vk::DebugUtilsMessageSeverityFlagsEXT::WARNING
| vk::DebugUtilsMessageSeverityFlagsEXT::INFO
| vk::DebugUtilsMessageSeverityFlagsEXT::VERBOSE,
)
.message_type(
vk::DebugUtilsMessageTypeFlagsEXT::GENERAL
| vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION
| vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE,
)
.pfn_user_callback(Some(vulkan_debug_callback))
.build();
let debug_utils_loader = DebugUtils::new(entry, instance);
let debug_call_back =
unsafe { debug_utils_loader.create_debug_utils_messenger(&debug_info, None) }.unwrap();
Self {
debug_utils_loader,
debug_call_back,
}
}
}
impl RendererBackendManager { impl RendererBackendManager {
pub fn new(display_handle: RawDisplayHandle) -> Self { pub fn new(display_handle: RawDisplayHandle) -> Self {
#[cfg(all(any(target_os = "macos", target_os = "ios"), feature = "vulkan-static"))] #[cfg(all(any(target_os = "macos", target_os = "ios"), feature = "vulkan-static"))]
@ -81,30 +116,14 @@ impl RendererBackendManager {
let instance = unsafe { entry.create_instance(&create_info, None) }.unwrap(); let instance = unsafe { entry.create_instance(&create_info, None) }.unwrap();
let debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() #[cfg(debug_assertions)]
.message_severity( let debug = VulkanDebug::new(&entry, &instance);
vk::DebugUtilsMessageSeverityFlagsEXT::ERROR
| vk::DebugUtilsMessageSeverityFlagsEXT::WARNING
| vk::DebugUtilsMessageSeverityFlagsEXT::INFO
| vk::DebugUtilsMessageSeverityFlagsEXT::VERBOSE,
)
.message_type(
vk::DebugUtilsMessageTypeFlagsEXT::GENERAL
| vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION
| vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE,
)
.pfn_user_callback(Some(vulkan_debug_callback))
.build();
let debug_utils_loader = DebugUtils::new(&entry, &instance);
let debug_call_back =
unsafe { debug_utils_loader.create_debug_utils_messenger(&debug_info, None) }.unwrap();
Self { Self {
entry, entry,
instance, instance,
debug_utils_loader, #[cfg(debug_assertions)]
debug_call_back, debug,
} }
} }
} }
@ -112,8 +131,11 @@ impl RendererBackendManager {
impl Drop for RendererBackendManager { impl Drop for RendererBackendManager {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
self.debug_utils_loader #[cfg(debug_assertions)]
.destroy_debug_utils_messenger(self.debug_call_back, None); self.debug
.debug_utils_loader
.destroy_debug_utils_messenger(self.debug.debug_call_back, None);
self.instance.destroy_instance(None); self.instance.destroy_instance(None);
} }
} }
@ -749,6 +771,7 @@ impl Drop for VulkanWindowInner {
} }
} }
#[cfg(debug_assertions)]
unsafe extern "system" fn vulkan_debug_callback( unsafe extern "system" fn vulkan_debug_callback(
message_severity: vk::DebugUtilsMessageSeverityFlagsEXT, message_severity: vk::DebugUtilsMessageSeverityFlagsEXT,
message_type: vk::DebugUtilsMessageTypeFlagsEXT, message_type: vk::DebugUtilsMessageTypeFlagsEXT,