diff --git a/lib/src/renderer/vulkan/vulkan.rs b/lib/src/renderer/vulkan/vulkan.rs index d7bed14..72a395a 100644 --- a/lib/src/renderer/vulkan/vulkan.rs +++ b/lib/src/renderer/vulkan/vulkan.rs @@ -32,10 +32,45 @@ const VERTICES: [Vertex; 3] = [Vertex(-1.0, -1.0), Vertex(3.0, -1.0), Vertex(-1. pub struct RendererBackendManager { entry: Entry, instance: Instance, + #[cfg(debug_assertions)] + debug: VulkanDebug, +} + +#[cfg(debug_assertions)] +struct VulkanDebug { debug_utils_loader: DebugUtils, 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 { pub fn new(display_handle: RawDisplayHandle) -> Self { #[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 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(); + #[cfg(debug_assertions)] + let debug = VulkanDebug::new(&entry, &instance); Self { entry, instance, - debug_utils_loader, - debug_call_back, + #[cfg(debug_assertions)] + debug, } } } @@ -112,8 +131,11 @@ impl RendererBackendManager { impl Drop for RendererBackendManager { fn drop(&mut self) { unsafe { - self.debug_utils_loader - .destroy_debug_utils_messenger(self.debug_call_back, None); + #[cfg(debug_assertions)] + self.debug + .debug_utils_loader + .destroy_debug_utils_messenger(self.debug.debug_call_back, None); + self.instance.destroy_instance(None); } } @@ -749,6 +771,7 @@ impl Drop for VulkanWindowInner { } } +#[cfg(debug_assertions)] unsafe extern "system" fn vulkan_debug_callback( message_severity: vk::DebugUtilsMessageSeverityFlagsEXT, message_type: vk::DebugUtilsMessageTypeFlagsEXT,