Refactor Vulkan device and instance handling
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 0s

Refactor Vulkan initialization to merge logical and physical device management into a unified `VkDevice` struct. Improved logging for Vulkan instance creation and surface management. Added methods to `VkPhysicalDevice` and `VkSurface` for more detailed querying of physical device capabilities.
This commit is contained in:
Florian RICHER 2024-11-11 12:09:52 +01:00
parent b91571e777
commit d0c6f31a1a
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
9 changed files with 233 additions and 59 deletions

View file

@ -1,61 +1,22 @@
use std::fmt::{Display, Formatter};
use ash::vk::QueueFlags;
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::ActiveEventLoop;
use winit::window::{WindowId};
use crate::display::window::Window;
use crate::vulkan::{VkInstance, VkPhysicalDevice};
use crate::vulkan::VkRenderContext;
pub struct App {
window: Window,
instance: Option<VkInstance>
render_context: Option<VkRenderContext>,
}
impl App {
pub fn new(window: Window) -> Self {
Self {
window,
instance: None
render_context: None,
}
}
fn init_vulkan(&mut self) {
let required_extensions = self.window
.required_extensions()
.expect("Unable to get required required extensions");
log::info!("Initializing Vulkan instance");
let instance = VkInstance::new(&required_extensions);
log::info!("Vulkan instance created");
log::info!("{instance}");
let surface = instance.create_surface(&self.window)
.expect("Unable to create surface");
let mut physical_devices = instance.get_physical_devices();
physical_devices.sort_by(|a, b| b.priority().cmp(&a.priority()));
let (physical_device, queue_family_index) = physical_devices
.iter()
.find_map(|physical_device| {
physical_device.queue_family_properties
.iter()
.enumerate()
.find_map(|(index, queue_family_property)| {
if surface.physical_device_queue_supported(physical_device, index as u32).unwrap_or(false) {
Some((physical_device, index as u32))
} else {
None
}
})
})
.expect("Unable to find suitable device");
self.instance = Some(instance);
}
}
impl ApplicationHandler for App {
@ -64,13 +25,13 @@ impl ApplicationHandler for App {
.map_err(|err| format!("Failed to create window: {}", err))
.unwrap();
self.init_vulkan();
self.render_context = VkRenderContext::init(&self.window).ok();
}
fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
match event {
WindowEvent::CloseRequested => {
log::info!("The close button was pressed; stopping");
log::debug!("The close button was pressed; stopping");
event_loop.exit();
}
_ => self.window.window_event(event_loop, id, event),