Refactor
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 0s

This commit is contained in:
Florian RICHER 2024-11-12 13:25:52 +01:00
parent d0c6f31a1a
commit caa79270db
5 changed files with 58 additions and 42 deletions

View file

@ -1,3 +1,5 @@
pub(self) const LOG_TARGET: &str = "app::vulkan";
pub(self) mod vk_render_context;
pub use vk_render_context::VkRenderContext;

View file

@ -1,13 +1,14 @@
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};
use crate::vulkan::vk_surface::VkSurface;
use crate::vulkan::VkPhysicalDevice;
use crate::vulkan::{VkPhysicalDevice, LOG_TARGET};
use ash::khr::surface;
use ash::prelude::VkResult;
use ash::vk::QueueFlags;
use ash::{vk, Entry, Instance};
use std::ffi::{c_char, CStr, CString};
use std::fmt::{Display, Formatter};
use winit::raw_window_handle::{HasDisplayHandle, HasWindowHandle};
pub struct VkInstance {
entry: Entry,
@ -20,15 +21,15 @@ impl VkInstance {
) -> Self {
let entry = Entry::linked();
log::debug!("Initializing Vulkan instance");
log::debug!(target: LOG_TARGET, "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(", "));
log::debug!(target: LOG_TARGET, "Required instance extensions: {}", required_extensions.join(", "));
}
// Layers
@ -38,14 +39,16 @@ impl VkInstance {
"VK_LAYER_KHRONOS_validation",
"VK_LAYER_MANGOHUD_overlay_x86_64",
"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(", "))
log::debug!(target: LOG_TARGET, "Selected debug layers : {}", layers.join(", "))
}
let layers_raw = layers.iter().map(|s| s.as_ptr()).collect::<Vec<_>>();
// App Info
@ -76,11 +79,11 @@ impl VkInstance {
.expect("Instance creation error")
};
log::debug!("Vulkan instance created");
log::debug!(target: LOG_TARGET, "Vulkan instance created");
Self {
entry,
handle: instance
handle: instance,
}
}
@ -94,7 +97,7 @@ impl VkInstance {
pub fn create_surface(
&self,
window: &crate::display::Window
window: &crate::display::Window,
) -> anyhow::Result<VkSurface> {
let window_handle = window.handle()
.ok_or_else(|| anyhow::anyhow!("Window handle is not available."))?;
@ -111,7 +114,7 @@ impl VkInstance {
)?
};
log::debug!("Surface created");
log::debug!(target: LOG_TARGET, "Surface created");
Ok(VkSurface::new(
surface_loader,
@ -136,7 +139,7 @@ impl Drop for VkInstance {
unsafe {
self.handle.destroy_instance(None);
}
log::debug!("Vulkan instance destroyed");
log::debug!(target: LOG_TARGET, "Vulkan instance destroyed");
}
}

View file

@ -1,7 +1,7 @@
use std::fmt::{Display, Formatter};
use ash::vk;
use crate::vulkan::utils::formatter::{format_driver_version, format_vulkan_version};
use crate::vulkan::vk_surface::VkSurface;
use ash::vk;
use std::fmt::{Display, Formatter};
pub struct VkPhysicalDevice {
// Vulkan properties
@ -22,14 +22,14 @@ impl VkPhysicalDevice {
handle: physical_device,
properties: device_properties,
features: device_features,
queue_family_properties: device_queue_families
queue_family_properties: device_queue_families,
}
}
pub fn find_queue_family_by(
&self,
queue_flags: Option<vk::QueueFlags>,
surface: Option<&VkSurface>
surface: Option<&VkSurface>,
) -> Option<(u32, &vk::QueueFamilyProperties)> {
self.queue_family_properties
.iter()
@ -47,7 +47,7 @@ impl VkPhysicalDevice {
Some(queue_flags) => {
queue_family_property.queue_flags
.contains(queue_flags)
},
}
None => true
};
@ -59,6 +59,19 @@ impl VkPhysicalDevice {
})
}
pub fn pick_physical_device_and_queue_by<'a>(
physical_devices: &'a Vec<VkPhysicalDevice>,
queue_flags: Option<vk::QueueFlags>,
surface: Option<&VkSurface>,
) -> Option<(&'a VkPhysicalDevice, u32, &'a vk::QueueFamilyProperties)> {
physical_devices
.iter()
.find_map(|physical_device| {
physical_device.find_queue_family_by(queue_flags, surface)
.and_then(|(queue_index, queue_family_properties)| Some((physical_device, queue_index, queue_family_properties)))
})
}
pub fn priority(&self) -> usize {
match self.properties.device_type {
vk::PhysicalDeviceType::CPU => 1,

View file

@ -1,9 +1,9 @@
use crate::vulkan::{VkDevice, VkInstance, VkPhysicalDevice, VkSurface};
use ash::vk::QueueFlags;
use crate::vulkan::{VkDevice, VkInstance, VkSurface};
pub struct VkRenderContext {
surface: VkSurface,
instance: VkInstance,
surface: VkSurface,
}
impl VkRenderContext {
@ -17,13 +17,11 @@ impl VkRenderContext {
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 (physical_device, queue_family_index, _) = VkPhysicalDevice::pick_physical_device_and_queue_by(
&physical_devices,
Some(QueueFlags::GRAPHICS),
Some(&surface),
).expect("Unable to find physical device");
let device = VkDevice::new_graphics_device(&instance, &physical_device, queue_family_index)
.expect("Unable to create device");
@ -38,9 +36,9 @@ impl VkRenderContext {
let surface_capabilities = surface.get_physical_device_surface_capabilities(physical_device)
.expect("Unable to get surface capabilities");
Ok(Self{
Ok(Self {
instance,
surface
surface,
})
}
}

View file

@ -1,6 +1,6 @@
use crate::vulkan::{VkPhysicalDevice, LOG_TARGET};
use ash::prelude::VkResult;
use ash::vk;
use crate::vulkan::VkPhysicalDevice;
pub struct VkSurface {
surface_loader: ash::khr::surface::Instance,
@ -14,7 +14,7 @@ impl VkSurface {
) -> Self {
Self {
surface_loader,
surface
surface,
}
}
@ -23,7 +23,7 @@ impl VkSurface {
self.surface_loader.get_physical_device_surface_support(
physical_device.handle,
queue_index,
self.surface
self.surface,
)
}
}
@ -32,7 +32,7 @@ impl VkSurface {
unsafe {
self.surface_loader.get_physical_device_surface_formats(
physical_device.handle,
self.surface
self.surface,
)
}
}
@ -41,7 +41,7 @@ impl VkSurface {
unsafe {
self.surface_loader.get_physical_device_surface_capabilities(
physical_device.handle,
self.surface
self.surface,
)
}
}
@ -50,7 +50,7 @@ impl VkSurface {
unsafe {
self.surface_loader.get_physical_device_surface_present_modes(
physical_device.handle,
self.surface
self.surface,
)
}
}
@ -61,6 +61,6 @@ impl Drop for VkSurface {
unsafe {
self.surface_loader.destroy_surface(self.surface, None);
}
log::debug!("Surface destroyed");
log::debug!(target: LOG_TARGET, "Surface destroyed");
}
}