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

Add vk_physical_device
This commit is contained in:
Florian RICHER 2024-11-07 14:05:09 +01:00
parent f24a6b66bc
commit 06cc558baf
4 changed files with 90 additions and 17 deletions

View file

@ -34,6 +34,10 @@ impl ApplicationHandler for App {
self.instance = self.window
.as_ref()
.and_then(|w| Some(VkInstance::new(w)));
if let Some(instance) = self.instance.as_ref() {
let _ = instance.get_physical_devices();
}
}
fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {

View file

@ -1,2 +1,5 @@
mod vk_instance;
pub use vk_instance::VkInstance;
pub(self) mod vk_instance;
pub(self) mod vk_physical_device;
pub use vk_instance::VkInstance;
pub use vk_physical_device::VkPhysicalDevice;

View file

@ -1,25 +1,21 @@
use std::ffi;
use std::ffi::c_char;
use std::ffi::CString;
use ash::{Instance, vk, Entry};
use winit::raw_window_handle::{HasDisplayHandle};
use crate::vulkan::VkPhysicalDevice;
pub struct VkInstance {
instance: Instance
handle: Instance,
}
impl VkInstance {
pub fn new(window: &impl HasDisplayHandle) -> Self {
let entry = Entry::linked();
let app_name = unsafe { ffi::CStr::from_bytes_with_nul_unchecked(b"VulkanTriangle\0") };
let layer_names = [
unsafe { ffi::CStr::from_bytes_with_nul_unchecked(b"VK_LAYER_KHRONOS_validation\0") }
];
let layers_names_raw: Vec<*const c_char> = layer_names
.iter()
.map(|raw_name| raw_name.as_ptr())
.collect();
// Layers
let layers_available = unsafe { entry.enumerate_instance_layer_properties().unwrap_or_default() };
let layer_names = layers_available.iter().map(|layer| layer.layer_name.as_ptr()).collect::<Vec<_>>();
// Extensions
let mut extension_names =
ash_window::enumerate_required_extensions(window.display_handle().expect("No display handle").as_raw())
.unwrap()
@ -32,10 +28,12 @@ impl VkInstance {
extension_names.push(ash::khr::get_physical_device_properties2::NAME.as_ptr());
}
// App Info
let app_name = CString::new("VulkanTriangle").unwrap();
let appinfo = vk::ApplicationInfo::default()
.application_name(app_name)
.application_name(app_name.as_c_str())
.application_version(0)
.engine_name(app_name)
.engine_name(app_name.as_c_str())
.engine_version(0)
.api_version(vk::make_api_version(0, 1, 0, 0));
@ -45,9 +43,10 @@ impl VkInstance {
vk::InstanceCreateFlags::default()
};
// Instance Info
let create_info = vk::InstanceCreateInfo::default()
.application_info(&appinfo)
.enabled_layer_names(&layers_names_raw)
.enabled_layer_names(&layer_names)
.enabled_extension_names(&extension_names)
.flags(create_flags);
@ -58,7 +57,23 @@ impl VkInstance {
};
Self {
instance
handle: instance
}
}
pub fn get_physical_devices(&self) -> Vec<VkPhysicalDevice> {
let physical_devices = unsafe { self.handle.enumerate_physical_devices() };
physical_devices
.unwrap_or_default()
.iter().map(|physical_device| VkPhysicalDevice::new(&self.handle, *physical_device))
.collect()
}
}
impl Drop for VkInstance {
fn drop(&mut self) {
unsafe {
self.handle.destroy_instance(None);
}
}
}

View file

@ -0,0 +1,51 @@
use ash::vk;
pub struct VkPhysicalDevice {
// Vulkan properties
handle: vk::PhysicalDevice,
properties: vk::PhysicalDeviceProperties,
features: vk::PhysicalDeviceFeatures,
queue_family_properties: Vec<vk::QueueFamilyProperties>,
}
impl VkPhysicalDevice {
pub fn new(instance: &ash::Instance, physical_device: vk::PhysicalDevice) -> Self {
let device_properties = unsafe { instance.get_physical_device_properties(physical_device) };
let device_features = unsafe { instance.get_physical_device_features(physical_device) };
let device_queue_families =
unsafe { instance.get_physical_device_queue_family_properties(physical_device) };
Self {
handle: physical_device,
properties: device_properties,
features: device_features,
queue_family_properties: device_queue_families
}
}
pub fn priority(&self) -> usize {
let mut priority = 0;
let has_graphics_support = self.queue_family_properties.iter().any(|qf| qf.queue_flags.contains(vk::QueueFlags::GRAPHICS));
let has_compute_support = self.queue_family_properties.iter().any(|qf| qf.queue_flags.contains(vk::QueueFlags::COMPUTE));
let has_transfer_support = self.queue_family_properties.iter().any(|qf| qf.queue_flags.contains(vk::QueueFlags::TRANSFER));
let has_sparse_binding_support = self.queue_family_properties.iter().any(|qf| qf.queue_flags.contains(vk::QueueFlags::SPARSE_BINDING));
let physical_device_type = self.properties.device_type;
priority |= has_graphics_support as usize;
priority |= (has_sparse_binding_support as usize) << 1;
priority |= (has_transfer_support as usize) << 2;
priority |= (has_compute_support as usize) << 3;
let weight : usize = match physical_device_type {
vk::PhysicalDeviceType::CPU => 1,
vk::PhysicalDeviceType::VIRTUAL_GPU => 2,
vk::PhysicalDeviceType::INTEGRATED_GPU => 3,
vk::PhysicalDeviceType::DISCRETE_GPU => 4,
_ => 0
};
priority |= weight << 4;
priority
}
}