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

@ -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);
}
}
}