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

@ -0,0 +1,46 @@
use ash::vk::QueueFlags;
use crate::vulkan::{VkDevice, VkInstance, VkSurface};
pub struct VkRenderContext {
surface: VkSurface,
instance: VkInstance,
}
impl VkRenderContext {
pub fn init(window: &crate::display::Window) -> anyhow::Result<Self> {
let required_extensions = window
.required_extensions()?;
let instance = VkInstance::new(&required_extensions);
let surface = instance.create_surface(&window)?;
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.find_queue_family_by(Some(QueueFlags::GRAPHICS), Some(&surface))
.and_then(|queue_index| Some((physical_device, queue_index)))
})
.expect("Unable to find suitable device");
let device = VkDevice::new_graphics_device(&instance, &physical_device, queue_family_index)
.expect("Unable to create device");
let present_queue = device.get_device_queue(0);
let surface_format = surface.get_physical_device_surface_formats(physical_device)
.unwrap_or_default()
.first()
.expect("Unable to get surface format");
let surface_capabilities = surface.get_physical_device_surface_capabilities(physical_device)
.expect("Unable to get surface capabilities");
Ok(Self{
instance,
surface
})
}
}