diff --git a/src/display/app.rs b/src/display/app.rs index 129422f..6af2faa 100644 --- a/src/display/app.rs +++ b/src/display/app.rs @@ -28,7 +28,7 @@ impl ApplicationHandler for App { self.render_context = VkRenderContext::init(&self.window).ok(); } - fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) { + fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) { match event { WindowEvent::CloseRequested => { log::debug!("The close button was pressed; stopping"); diff --git a/src/display/window.rs b/src/display/window.rs index e5048cd..8111e16 100644 --- a/src/display/window.rs +++ b/src/display/window.rs @@ -1,8 +1,6 @@ use std::ffi::c_char; -use winit::event::WindowEvent; use winit::event_loop::ActiveEventLoop; use winit::raw_window_handle::HasDisplayHandle; -use winit::window::WindowId; pub struct Window { handle: Option, diff --git a/src/vulkan/utils/formatter.rs b/src/vulkan/utils/formatter.rs deleted file mode 100644 index 733ea21..0000000 --- a/src/vulkan/utils/formatter.rs +++ /dev/null @@ -1,50 +0,0 @@ -use ash::vk::LayerProperties; - -pub fn format_vulkan_version(version: u32) -> String { - format!( - "{}.{}.{}", - ash::vk::api_version_major(version), - ash::vk::api_version_minor(version), - ash::vk::api_version_patch(version) - ) -} - -pub fn format_driver_version(vendor_id: u32, driver_version_raw: u32) -> String { - if vendor_id == 0x10de { // NVIDIA - format!( - "{}.{}.{}.{}", - (driver_version_raw >> 22) & 0x3ff, - (driver_version_raw >> 14) & 0x0ff, - (driver_version_raw >> 6) & 0x0ff, - (driver_version_raw) & 0x003f - ) - } else if vendor_id == 0x8086 { // Intel - format!( - "{}.{}", - driver_version_raw >> 14, - driver_version_raw & 0x3fff - ) - } else { - format_vulkan_version(driver_version_raw) - } -} - -pub fn format_instance_layer(instance_layer: &LayerProperties) -> String { - let layer_name = instance_layer - .layer_name_as_c_str() - .and_then(|s| Ok(s.to_string_lossy())) - .and_then(|s| Ok(s.to_string())) - .unwrap_or(String::from("No layer name")); - - let description = instance_layer - .description_as_c_str() - .and_then(|s| Ok(s.to_string_lossy())) - .and_then(|s| Ok(s.to_string())) - .unwrap_or(String::from("No layer name")); - - format!( - "Name: {layer_name} Vulkan Version: {} Revision: {} Description: {description}", - format_vulkan_version(instance_layer.spec_version), - instance_layer.implementation_version, - ) -} \ No newline at end of file diff --git a/src/vulkan/utils/mod.rs b/src/vulkan/utils/mod.rs index 10d6f5d..759c25e 100644 --- a/src/vulkan/utils/mod.rs +++ b/src/vulkan/utils/mod.rs @@ -1,2 +1 @@ pub mod layers; -pub mod formatter; diff --git a/src/vulkan/vk_instance.rs b/src/vulkan/vk_instance.rs index 22aa230..6988eef 100644 --- a/src/vulkan/vk_instance.rs +++ b/src/vulkan/vk_instance.rs @@ -1,14 +1,8 @@ -use crate::vulkan::utils::formatter::format_instance_layer; use crate::vulkan::utils::layers::{use_layers, LayersSelector}; -use crate::vulkan::vk_surface::VkSurface; use crate::vulkan::{VkPhysicalDevice, LOG_TARGET}; use ash::khr::surface; -use ash::prelude::VkResult; -use ash::vk::QueueFlags; use ash::{vk, Entry, Instance}; use std::ffi::{c_char, CStr, CString}; -use std::fmt::{Display, Formatter}; -use winit::raw_window_handle::{HasDisplayHandle, HasWindowHandle}; pub struct VkInstance { pub(super) entry: Entry, @@ -24,6 +18,17 @@ impl VkInstance { log::debug!(target: LOG_TARGET, "Initializing Vulkan instance"); + if log::log_enabled!(target: LOG_TARGET, log::Level::Debug) { + let layer_properties = unsafe { entry.enumerate_instance_layer_properties() } + .unwrap_or_default(); + + for layer_property in layer_properties { + let layer_extensions = unsafe { entry.enumerate_instance_extension_properties(layer_property.layer_name_as_c_str().ok()) } + .unwrap_or_default(); + log::debug!(target: LOG_TARGET, "{layer_property:#?} {layer_extensions:#?}"); + } + } + { let required_extensions = required_extensions .iter() @@ -106,25 +111,3 @@ impl Drop for VkInstance { log::debug!(target: LOG_TARGET, "Vulkan instance destroyed ({:?})", self.handle.handle()); } } - -impl Display for VkInstance { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - writeln!(f, "")?; - - let instance_layers = unsafe { self.entry.enumerate_instance_layer_properties().unwrap_or_default() }; - writeln!(f, "Instance layers ({}) :", instance_layers.len())?; - for instance_layer in &instance_layers { - writeln!(f, "{}", format_instance_layer(instance_layer))?; - } - writeln!(f, "")?; - - let physical_devices = self.get_physical_devices(); - writeln!(f, "Physical Devices ({}) :", physical_devices.len())?; - for physical_device in physical_devices { - writeln!(f, "{physical_device}")?; - } - writeln!(f, "")?; - - Ok(()) - } -} \ No newline at end of file diff --git a/src/vulkan/vk_physical_device.rs b/src/vulkan/vk_physical_device.rs index f3e543d..9f8aa35 100644 --- a/src/vulkan/vk_physical_device.rs +++ b/src/vulkan/vk_physical_device.rs @@ -1,7 +1,6 @@ -use crate::vulkan::utils::formatter::{format_driver_version, format_vulkan_version}; use crate::vulkan::vk_surface::VkSurface; use ash::vk; -use std::fmt::{Display, Formatter}; +use crate::vulkan::LOG_TARGET; pub struct VkPhysicalDevice { // Vulkan properties @@ -13,10 +12,14 @@ pub struct VkPhysicalDevice { impl VkPhysicalDevice { pub fn new(instance: &ash::Instance, physical_device: vk::PhysicalDevice) -> Self { + log::debug!(target: LOG_TARGET, "New physical device"); let device_properties = unsafe { instance.get_physical_device_properties(physical_device) }; + log::debug!(target: LOG_TARGET, "{device_properties:#?}"); let device_features = unsafe { instance.get_physical_device_features(physical_device) }; + log::debug!(target: LOG_TARGET, "{device_features:#?}"); let device_queue_families = unsafe { instance.get_physical_device_queue_family_properties(physical_device) }; + log::debug!(target: LOG_TARGET, "{device_queue_families:#?}"); Self { handle: physical_device, @@ -82,22 +85,3 @@ impl VkPhysicalDevice { } } } - -impl Display for VkPhysicalDevice { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - let device_name = self.properties - .device_name_as_c_str() - .and_then(|s| Ok(s.to_string_lossy())) - .and_then(|s| Ok(s.to_string())) - .unwrap_or(String::from("No device name")); - - writeln!(f, "Nom: {device_name}")?; - writeln!(f, "Vendor ID: {:#x}", self.properties.vendor_id)?; - writeln!(f, "Device ID: {:#x}", self.properties.device_id)?; - writeln!(f, "Driver version: {}", format_driver_version(self.properties.vendor_id, self.properties.driver_version))?; - writeln!(f, "Vulkan version: {}", format_vulkan_version(self.properties.api_version))?; - writeln!(f, "Device type: {:?}", self.properties.device_type)?; - - Ok(()) - } -} diff --git a/src/vulkan/vk_render_context.rs b/src/vulkan/vk_render_context.rs index 1589435..022dfe4 100644 --- a/src/vulkan/vk_render_context.rs +++ b/src/vulkan/vk_render_context.rs @@ -1,5 +1,5 @@ use std::sync::Arc; -use crate::vulkan::{VkDevice, VkInstance, VkPhysicalDevice, VkSurface, VkSwapchain}; +use crate::vulkan::{VkDevice, VkInstance, VkPhysicalDevice, VkSurface, VkSwapchain, LOG_TARGET}; use ash::vk; pub struct VkRenderContext { @@ -10,15 +10,15 @@ pub struct VkRenderContext { swapchain: Arc, // present_queue: vk::Queue, - // + // // pool: vk::CommandPool, - // + // // setup_command_buffer: vk::CommandBuffer, // draw_command_buffer: vk::CommandBuffer, - // + // // draw_commands_reuse_fence: vk::Fence, // setup_commands_reuse_fence: vk::Fence, - // + // // present_complete_semaphore: vk::Semaphore, // rendering_complete_semaphore: vk::Semaphore, } @@ -37,14 +37,15 @@ impl VkRenderContext { let mut physical_devices = instance.get_physical_devices(); physical_devices.sort_by(|a, b| b.priority().cmp(&a.priority())); - let (physical_device, queue_family_index, _) = VkPhysicalDevice::pick_physical_device_and_queue_by( + let (physical_device, queue_family_index, properties) = VkPhysicalDevice::pick_physical_device_and_queue_by( &physical_devices, Some(vk::QueueFlags::GRAPHICS), Some(&surface), ).ok_or_else(|| anyhow::anyhow!("Unable to find physical device"))?; + log::debug!(target: LOG_TARGET, "Selected queue {properties:#?} for physical device {:?}", physical_device.properties.device_name_as_c_str()); let device = Arc::new(VkDevice::new_graphics_device(instance.clone(), &physical_device, queue_family_index)?); - + let swapchain = Arc::new(VkSwapchain::new( &window, surface.clone(), @@ -53,36 +54,36 @@ impl VkRenderContext { )?); // let present_queue = device.get_device_queue(0); - // + // // let pool_create_info = vk::CommandPoolCreateInfo::default() // .flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER); - // + // // let pool = device.create_command_pool(&pool_create_info) // .expect("Failed to create command pool"); - // + // // let command_buffer_allocate_info = vk::CommandBufferAllocateInfo::default() // .command_buffer_count(2) // .command_pool(pool) // .level(vk::CommandBufferLevel::PRIMARY); - // + // // let command_buffers = device // .allocate_command_buffers(&command_buffer_allocate_info) // .expect("Failed to create command buffers"); // let setup_command_buffer = command_buffers[0]; // let draw_command_buffer = command_buffers[1]; - // + // // let fence_create_info = // vk::FenceCreateInfo::default().flags(vk::FenceCreateFlags::SIGNALED); - // + // // let draw_commands_reuse_fence = device // .create_fence(&fence_create_info) // .expect("Failed to create draw commands fence"); // let setup_commands_reuse_fence = device // .create_fence(&fence_create_info) // .expect("Failed to create setup commands fence"); - // + // // let semaphore_create_info = vk::SemaphoreCreateInfo::default(); - // + // // let present_complete_semaphore = device // .create_semaphore(&semaphore_create_info) // .expect("Failed to create present complete semaphore"); @@ -98,15 +99,15 @@ impl VkRenderContext { swapchain, // present_queue, - // + // // pool, - // + // // setup_command_buffer, // draw_command_buffer, - // + // // present_complete_semaphore, // rendering_complete_semaphore, - // + // // draw_commands_reuse_fence, // setup_commands_reuse_fence, }) @@ -117,39 +118,39 @@ impl VkRenderContext { // self.device.handle // .wait_for_fences(&[self.draw_commands_reuse_fence], true, u64::MAX) // .expect("Wait for fence failed."); - // + // // self.device.handle // .reset_fences(&[self.draw_commands_reuse_fence]) // .expect("Reset fences failed."); - // + // // self.device.handle // .reset_command_buffer( // self.draw_command_buffer, // vk::CommandBufferResetFlags::RELEASE_RESOURCES, // ) // .expect("Reset command buffer failed."); - // + // // let command_buffer_begin_info = vk::CommandBufferBeginInfo::default() // .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT); - // + // // self.device.handle // .begin_command_buffer(self.draw_command_buffer, &command_buffer_begin_info) // .expect("Begin commandbuffer"); - // + // // self.device.handle // .end_command_buffer(self.draw_command_buffer) // .expect("End commandbuffer"); - // + // // let command_buffers = vec![self.draw_command_buffer]; // let semaphores = vec![self.rendering_complete_semaphore]; // let wait_mask = vec![vk::PipelineStageFlags::default()]; - // + // // let submit_info = vk::SubmitInfo::default() // .wait_semaphores(&semaphores) // .wait_dst_stage_mask(&wait_mask) // .command_buffers(&command_buffers) // .signal_semaphores(&semaphores); - // + // // self.device.handle // .queue_submit(self.present_queue, &[submit_info], self.draw_commands_reuse_fence) // .expect("queue submit failed."); diff --git a/src/vulkan/vk_swapchain.rs b/src/vulkan/vk_swapchain.rs index 7ce2992..3b61f0d 100644 --- a/src/vulkan/vk_swapchain.rs +++ b/src/vulkan/vk_swapchain.rs @@ -1,5 +1,5 @@ use std::sync::Arc; -use crate::vulkan::{VkDevice, VkInstance, VkPhysicalDevice, VkSurface, LOG_TARGET}; +use crate::vulkan::{VkDevice, VkPhysicalDevice, VkSurface, LOG_TARGET}; use ash::prelude::VkResult; use ash::vk; use crate::display::Window;