Refactor VK layer handling and add logging improvements
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 0s
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 0s
Refactored VK layer handling by changing return type of `use_layers` to `Vec<CString>`, adjusting subsequent usage of layers. Implemented `Display` for `VkPhysicalDevice` for better logging. Enhanced application initialization with detailed window attributes. Transitioned to using `log::info` for consistent logging.
This commit is contained in:
parent
f52832e0e5
commit
4048937a6c
5 changed files with 43 additions and 19 deletions
|
@ -1,14 +1,26 @@
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
use winit::{
|
use winit::{
|
||||||
application::ApplicationHandler, event::WindowEvent, event_loop::ActiveEventLoop, raw_window_handle::{HasDisplayHandle, DisplayHandle, HandleError}, window::{Window, WindowId}
|
application::ApplicationHandler, event::WindowEvent, event_loop::ActiveEventLoop, raw_window_handle::{HasDisplayHandle, DisplayHandle, HandleError}, window::{Window, WindowId}
|
||||||
};
|
};
|
||||||
|
use winit::window::WindowAttributes;
|
||||||
use crate::vulkan::VkInstance;
|
use crate::vulkan::VkInstance;
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
|
window_attributes: WindowAttributes,
|
||||||
window: Option<Window>,
|
window: Option<Window>,
|
||||||
instance: Option<VkInstance>,
|
instance: Option<VkInstance>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl App {
|
||||||
|
pub fn new(window_attributes: WindowAttributes) -> Self {
|
||||||
|
Self {
|
||||||
|
window_attributes,
|
||||||
|
window: None,
|
||||||
|
instance: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl HasDisplayHandle for App {
|
impl HasDisplayHandle for App {
|
||||||
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
|
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
|
||||||
self.window.as_ref()
|
self.window.as_ref()
|
||||||
|
@ -19,16 +31,8 @@ impl HasDisplayHandle for App {
|
||||||
|
|
||||||
impl ApplicationHandler for App {
|
impl ApplicationHandler for App {
|
||||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||||
let window_attributes = Window::default_attributes()
|
|
||||||
.with_title("Rust ASH Test")
|
|
||||||
.with_visible(true)
|
|
||||||
.with_inner_size(winit::dpi::LogicalSize::new(
|
|
||||||
f64::from(800),
|
|
||||||
f64::from(600),
|
|
||||||
));
|
|
||||||
|
|
||||||
self.window = event_loop
|
self.window = event_loop
|
||||||
.create_window(window_attributes)
|
.create_window(self.window_attributes.clone())
|
||||||
.ok();
|
.ok();
|
||||||
|
|
||||||
self.instance = self.window
|
self.instance = self.window
|
||||||
|
@ -38,9 +42,9 @@ impl ApplicationHandler for App {
|
||||||
if let Some(instance) = self.instance.as_ref() {
|
if let Some(instance) = self.instance.as_ref() {
|
||||||
let physical_devices = instance.get_physical_devices();
|
let physical_devices = instance.get_physical_devices();
|
||||||
|
|
||||||
println!("Physical Devices:");
|
log::info!("Physical Devices:");
|
||||||
for physical_device in physical_devices {
|
for physical_device in physical_devices {
|
||||||
println!("\tNom: {:?}, Priorité: {}", physical_device.properties.device_name_as_c_str().unwrap_or_default(), physical_device.priority())
|
log::info!("{}", physical_device)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,7 +52,7 @@ impl ApplicationHandler for App {
|
||||||
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 {
|
match event {
|
||||||
WindowEvent::CloseRequested => {
|
WindowEvent::CloseRequested => {
|
||||||
println!("The close button was pressed; stopping");
|
log::info!("The close button was pressed; stopping");
|
||||||
event_loop.exit();
|
event_loop.exit();
|
||||||
}
|
}
|
||||||
WindowEvent::RedrawRequested => {
|
WindowEvent::RedrawRequested => {
|
||||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -1,4 +1,5 @@
|
||||||
use winit::event_loop::EventLoop;
|
use winit::event_loop::EventLoop;
|
||||||
|
use winit::window::Window;
|
||||||
|
|
||||||
mod display;
|
mod display;
|
||||||
mod vulkan;
|
mod vulkan;
|
||||||
|
@ -7,8 +8,16 @@ fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
let event_loop = EventLoop::new().unwrap();
|
let event_loop = EventLoop::new().unwrap();
|
||||||
|
|
||||||
|
let window_attributes = Window::default_attributes()
|
||||||
|
.with_title("Rust ASH Test")
|
||||||
|
.with_visible(true)
|
||||||
|
.with_inner_size(winit::dpi::LogicalSize::new(
|
||||||
|
f64::from(800),
|
||||||
|
f64::from(600),
|
||||||
|
));
|
||||||
|
|
||||||
let mut app = display::App::default();
|
let mut app = display::App::new(window_attributes);
|
||||||
|
|
||||||
let _ = event_loop.run_app(&mut app);
|
let _ = event_loop.run_app(&mut app);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::ffi::CString;
|
||||||
pub fn use_layers(
|
pub fn use_layers(
|
||||||
entry: &ash::Entry,
|
entry: &ash::Entry,
|
||||||
layers_to_select: Vec<&str>,
|
layers_to_select: Vec<&str>,
|
||||||
) -> Vec<*const std::ffi::c_char> {
|
) -> Vec<CString> {
|
||||||
let layers_available = get_layers_available(entry);
|
let layers_available = get_layers_available(entry);
|
||||||
|
|
||||||
log_layers_available(&layers_available);
|
log_layers_available(&layers_available);
|
||||||
|
@ -14,8 +14,8 @@ pub fn use_layers(
|
||||||
|
|
||||||
selected_layers
|
selected_layers
|
||||||
.iter()
|
.iter()
|
||||||
.map(|sl| CString::new(sl.clone().as_bytes()).unwrap().as_ptr())
|
.map(|sl| CString::new(sl.clone().as_bytes()).unwrap())
|
||||||
.collect()
|
.collect::<Vec<_>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_layers_available(entry: &ash::Entry) -> Vec<ash::vk::LayerProperties> {
|
fn get_layers_available(entry: &ash::Entry) -> Vec<ash::vk::LayerProperties> {
|
||||||
|
|
|
@ -15,9 +15,11 @@ impl VkInstance {
|
||||||
|
|
||||||
// Layers
|
// Layers
|
||||||
let layers = use_layers(&entry, vec![
|
let layers = use_layers(&entry, vec![
|
||||||
|
"VK_LAYER_KHRONOS_validation",
|
||||||
"VK_LAYER_MANGOHUD_overlay_x86_64",
|
"VK_LAYER_MANGOHUD_overlay_x86_64",
|
||||||
"VK_LAYER_NV_optimus"
|
"VK_LAYER_NV_optimus"
|
||||||
]);
|
]);
|
||||||
|
let layers_raw = layers.iter().map(|s| s.as_ptr()).collect::<Vec<_>>();
|
||||||
|
|
||||||
// Extensions
|
// Extensions
|
||||||
let mut extension_names =
|
let mut extension_names =
|
||||||
|
@ -50,7 +52,7 @@ impl VkInstance {
|
||||||
// Instance Info
|
// Instance Info
|
||||||
let create_info = vk::InstanceCreateInfo::default()
|
let create_info = vk::InstanceCreateInfo::default()
|
||||||
.application_info(&appinfo)
|
.application_info(&appinfo)
|
||||||
.enabled_layer_names(&layers)
|
.enabled_layer_names(&layers_raw)
|
||||||
.enabled_extension_names(&extension_names)
|
.enabled_extension_names(&extension_names)
|
||||||
.flags(create_flags);
|
.flags(create_flags);
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
use ash::vk;
|
use ash::vk;
|
||||||
|
use crate::display::App;
|
||||||
|
|
||||||
pub struct VkPhysicalDevice {
|
pub struct VkPhysicalDevice {
|
||||||
// Vulkan properties
|
// Vulkan properties
|
||||||
|
@ -48,4 +50,11 @@ impl VkPhysicalDevice {
|
||||||
|
|
||||||
priority
|
priority
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue