diff --git a/lib/src/renderer/vulkan/vulkan.rs b/lib/src/renderer/vulkan/vulkan.rs index 9949a44..6cf1eda 100644 --- a/lib/src/renderer/vulkan/vulkan.rs +++ b/lib/src/renderer/vulkan/vulkan.rs @@ -4,7 +4,7 @@ use std::{ sync::Arc, }; -use ash::{util::Align, vk, Entry, Instance}; +use ash::{extensions::ext::DebugUtils, util::Align, vk, Entry, Instance}; use ash_window::enumerate_required_extensions; use librashader::runtime::vk::{FilterChain, FilterChainOptions, FrameOptions, VulkanObjects}; use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle}; @@ -32,6 +32,8 @@ const VERTICES: [Vertex; 3] = [Vertex(-1.0, -1.0), Vertex(3.0, -1.0), Vertex(-1. pub struct RendererBackendManager { entry: Entry, instance: Instance, + debug_utils_loader: DebugUtils, + debug_call_back: vk::DebugUtilsMessengerEXT, } impl RendererBackendManager { @@ -48,6 +50,8 @@ impl RendererBackendManager { .unwrap() .to_vec(); + extension_names.push(DebugUtils::name().as_ptr()); + #[cfg(any(target_os = "macos", target_os = "ios"))] { #[cfg(not(feature = "vulkan-static"))] @@ -76,13 +80,41 @@ impl RendererBackendManager { .build(); let instance = unsafe { entry.create_instance(&create_info, None) }.unwrap(); - Self { entry, instance } + + let debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder() + .message_severity( + vk::DebugUtilsMessageSeverityFlagsEXT::ERROR + | vk::DebugUtilsMessageSeverityFlagsEXT::WARNING + | vk::DebugUtilsMessageSeverityFlagsEXT::INFO, + ) + .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 { + entry, + instance, + debug_utils_loader, + debug_call_back, + } } } impl Drop for RendererBackendManager { fn drop(&mut self) { - unsafe { self.instance.destroy_instance(None) }; + unsafe { + self.debug_utils_loader + .destroy_debug_utils_messenger(self.debug_call_back, None); + self.instance.destroy_instance(None); + } } } @@ -715,3 +747,31 @@ impl Drop for VulkanWindowInner { } } } + +unsafe extern "system" fn vulkan_debug_callback( + message_severity: vk::DebugUtilsMessageSeverityFlagsEXT, + message_type: vk::DebugUtilsMessageTypeFlagsEXT, + p_callback_data: *const vk::DebugUtilsMessengerCallbackDataEXT, + _user_data: *mut std::os::raw::c_void, +) -> vk::Bool32 { + let callback_data = *p_callback_data; + let message_id_number = callback_data.message_id_number; + + let message_id_name = if callback_data.p_message_id_name.is_null() { + std::borrow::Cow::from("") + } else { + std::ffi::CStr::from_ptr(callback_data.p_message_id_name).to_string_lossy() + }; + + let message = if callback_data.p_message.is_null() { + std::borrow::Cow::from("") + } else { + std::ffi::CStr::from_ptr(callback_data.p_message).to_string_lossy() + }; + + println!( + "{message_severity:?}:\n{message_type:?} [{message_id_name} ({message_id_number})] : {message}\n", + ); + + vk::FALSE +}