Refactor layer selection and logging in Vulkan utils
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 0s

Separated concerns for layer fetching, logging, and selection into distinct functions. This improves readability and maintainability, allowing each function to focus on a single responsibility.
This commit is contained in:
Florian RICHER 2024-11-09 19:01:14 +01:00
parent e26f40ea74
commit f52832e0e5
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77

View file

@ -1,30 +1,30 @@
use std::ffi::CString;
use ash::{vk, Entry};
pub fn use_layers(
entry: &Entry,
layers_to_select: Vec<&str>
entry: &ash::Entry,
layers_to_select: Vec<&str>,
) -> Vec<*const std::ffi::c_char> {
let layers_available = unsafe {
entry
.enumerate_instance_layer_properties()
.unwrap_or_default()
};
let layers_available = get_layers_available(entry);
let mut selected_layers = Vec::<String>::new();
log_layers_available(&layers_available);
let selected_layers = select_layers(&layers_available, &layers_to_select);
log_layers_wanted(&layers_to_select, &selected_layers);
selected_layers
.iter()
.map(|sl| CString::new(sl.clone().as_bytes()).unwrap().as_ptr())
.collect()
}
fn get_layers_available(entry: &ash::Entry) -> Vec<ash::vk::LayerProperties> {
unsafe { entry.enumerate_instance_layer_properties().unwrap_or_default() }
}
fn log_layers_available(layers_available: &[ash::vk::LayerProperties]) {
log::info!("Available layers ({}):", layers_available.len());
for l in layers_available {
let layer_name = l
.layer_name_as_c_str()
.unwrap_or_default()
.to_string_lossy()
.into_owned();
if layers_to_select.iter().any(|ln| (*ln).eq(&layer_name)) {
selected_layers.push(layer_name);
}
log::info!(
"\t{:?}\tImplementation version: {}\tVulkan Version: {}\tDescription: {:?}",
l.layer_name_as_c_str().unwrap_or_default(),
@ -34,25 +34,41 @@ pub fn use_layers(
);
}
log::info!(""); // Add blank line
}
fn select_layers(
layers_available: &[ash::vk::LayerProperties],
layers_to_select: &[&str],
) -> Vec<String> {
layers_available
.iter()
.filter_map(|l| {
let layer_name = l
.layer_name_as_c_str()
.unwrap_or_default()
.to_string_lossy();
layers_to_select
.iter()
.find(|&&ln| ln == layer_name)
.map(|_| layer_name.into_owned())
})
.collect()
}
fn log_layers_wanted(layers_to_select: &[&str], selected_layers: &[String]) {
log::info!("Layers wanted ({}):", layers_to_select.len());
for l in layers_to_select {
let selected = selected_layers.iter().any(|sl| sl.eq(l));
log::info!("\t{:?}\tSelected: {}", l, selected);
for ol in layers_to_select {
let selected = selected_layers.iter().any(|sl| sl == ol);
log::info!("\t{:?}\tSelected: {}", ol, selected);
}
log::info!(""); // Add blank line
selected_layers.iter().map(|sl| {
let c_string = CString::new(sl.clone().as_bytes()).unwrap();
c_string.as_ptr()
}).collect()
}
pub fn print_version(version: u32) -> String {
format!(
"{}.{}.{}",
vk::api_version_major(version),
vk::api_version_minor(version),
vk::api_version_patch(version)
ash::vk::api_version_major(version),
ash::vk::api_version_minor(version),
ash::vk::api_version_patch(version)
)
}