Enhance vulkan instance and physical device display formatting
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 0s

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.
This commit is contained in:
Florian RICHER 2024-11-10 21:27:03 +01:00
parent b7d0abb9ed
commit b91571e777
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
5 changed files with 88 additions and 19 deletions

View file

@ -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");

View file

@ -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,
)
}

View file

@ -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;

View file

@ -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::<Vec<_>>();
// 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(())
}
}

View file

@ -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(())
}
}