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,7 +1,8 @@
use std::ffi::{c_char, CString};
use std::fmt::{Debug, Display, Formatter};
use std::ffi::{c_char, CStr, CString};
use std::fmt::{Display, Formatter};
use ash::{Instance, vk, Entry};
use ash::khr::surface;
use ash::prelude::VkResult;
use winit::raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use crate::vulkan::utils::formatter::format_instance_layer;
use crate::vulkan::utils::layers::{use_layers, LayersSelector};
@ -19,6 +20,17 @@ impl VkInstance {
) -> Self {
let entry = Entry::linked();
log::debug!("Initializing Vulkan instance");
if log::log_enabled!(log::Level::Debug) {
let required_extensions = required_extensions
.iter()
.map(|str| unsafe { CStr::from_ptr(*str) })
.map(|cstr| cstr.to_string_lossy())
.collect::<Vec<_>>();
log::debug!("Required instance extensions: {}", required_extensions.join(", "));
}
// Layers
let layers = use_layers(
&entry,
@ -28,6 +40,12 @@ impl VkInstance {
"VK_LAYER_NV_optimus"
])
);
if log::log_enabled!(log::Level::Info) {
let layers = layers.iter()
.map(|layer| layer.to_string_lossy())
.collect::<Vec<_>>();
log::debug!("Selected debug layers : {}", layers.join(", "))
}
let layers_raw = layers.iter().map(|s| s.as_ptr()).collect::<Vec<_>>();
// App Info
@ -58,6 +76,8 @@ impl VkInstance {
.expect("Instance creation error")
};
log::debug!("Vulkan instance created");
Self {
entry,
handle: instance
@ -91,11 +111,24 @@ impl VkInstance {
)?
};
log::debug!("Surface created");
Ok(VkSurface::new(
surface_loader,
surface,
))
}
pub fn create_device(
&self,
physical_device: &VkPhysicalDevice,
create_info: &vk::DeviceCreateInfo,
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> VkResult<ash::Device> {
unsafe {
self.handle.create_device(physical_device.handle, &create_info, allocation_callbacks)
}
}
}
impl Drop for VkInstance {
@ -103,6 +136,7 @@ impl Drop for VkInstance {
unsafe {
self.handle.destroy_instance(None);
}
log::debug!("Vulkan instance destroyed");
}
}