From b91571e777ff604e7dcd6a3deaa43c7fe3b4de19 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Sun, 10 Nov 2024 21:27:03 +0100 Subject: [PATCH] Enhance vulkan instance and physical device display formatting Refactor Vulkan instance and physical device formatting for better clarity. Added formatter utilities to encapsulate version and property formatting. Updated logging to use the new formatted output for improved debug readability. --- src/display/app.rs | 2 +- src/vulkan/utils/formatter.rs | 50 ++++++++++++++++++++++++++++++++ src/vulkan/utils/mod.rs | 13 ++------- src/vulkan/vk_instance.rs | 24 ++++++++++++--- src/vulkan/vk_physical_device.rs | 18 ++++++++++-- 5 files changed, 88 insertions(+), 19 deletions(-) create mode 100644 src/vulkan/utils/formatter.rs diff --git a/src/display/app.rs b/src/display/app.rs index 152f409..e85d206 100644 --- a/src/display/app.rs +++ b/src/display/app.rs @@ -28,7 +28,7 @@ impl App { log::info!("Initializing Vulkan instance"); let instance = VkInstance::new(&required_extensions); log::info!("Vulkan instance created"); - log::info!("\t{}", instance); + log::info!("{instance}"); let surface = instance.create_surface(&self.window) .expect("Unable to create surface"); diff --git a/src/vulkan/utils/formatter.rs b/src/vulkan/utils/formatter.rs new file mode 100644 index 0000000..733ea21 --- /dev/null +++ b/src/vulkan/utils/formatter.rs @@ -0,0 +1,50 @@ +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 e5f7be3..10d6f5d 100644 --- a/src/vulkan/utils/mod.rs +++ b/src/vulkan/utils/mod.rs @@ -1,11 +1,2 @@ -mod layers; -pub use layers::{use_layers, LayersSelector}; - -pub fn print_version(version: u32) -> String { - format!( - "{}.{}.{}", - ash::vk::api_version_major(version), - ash::vk::api_version_minor(version), - ash::vk::api_version_patch(version) - ) -} +pub mod layers; +pub mod formatter; diff --git a/src/vulkan/vk_instance.rs b/src/vulkan/vk_instance.rs index cf1c8d5..738b3e4 100644 --- a/src/vulkan/vk_instance.rs +++ b/src/vulkan/vk_instance.rs @@ -1,9 +1,10 @@ use std::ffi::{c_char, CString}; -use std::fmt::{Display, Formatter}; +use std::fmt::{Debug, Display, Formatter}; use ash::{Instance, vk, Entry}; use ash::khr::surface; use winit::raw_window_handle::{HasDisplayHandle, HasWindowHandle}; -use crate::vulkan::utils::{use_layers, LayersSelector}; +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; @@ -27,7 +28,6 @@ impl VkInstance { "VK_LAYER_NV_optimus" ]) ); - log::info!("{:?}", layers); let layers_raw = layers.iter().map(|s| s.as_ptr()).collect::>(); // App Info @@ -108,6 +108,22 @@ impl Drop for VkInstance { impl Display for VkInstance { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "Vulkan Version:") + 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 8337d42..2f4411e 100644 --- a/src/vulkan/vk_physical_device.rs +++ b/src/vulkan/vk_physical_device.rs @@ -1,7 +1,6 @@ -use std::cmp::Ordering; use std::fmt::{Display, Formatter}; use ash::vk; -use crate::vulkan::vk_surface::VkSurface; +use crate::vulkan::utils::formatter::{format_driver_version, format_vulkan_version}; pub struct VkPhysicalDevice { // Vulkan properties @@ -39,6 +38,19 @@ impl VkPhysicalDevice { impl Display for VkPhysicalDevice { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "\tNom: {:?}, Priorité: {}", self.properties.device_name_as_c_str().unwrap_or_default(), self.priority()) + 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(()) } }