diff --git a/src/vulkan/utils.rs b/src/vulkan/utils.rs index 6f2b527..a748acb 100644 --- a/src/vulkan/utils.rs +++ b/src/vulkan/utils.rs @@ -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::::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 { + 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 { + 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) ) }