Add logical device struct and surface handling for Vulkan
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 0s
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 0s
Introduce the VkLogicalDevice struct and add surface creation logic in VkInstance. Also, import necessary extensions and refine Vulkan physical device and window handling. Included a dependency on 'anyhow' for error management.
This commit is contained in:
parent
4048937a6c
commit
da0be47b14
14 changed files with 258 additions and 153 deletions
|
@ -1,52 +1,70 @@
|
|||
use std::fmt::{Display, Formatter};
|
||||
use winit::{
|
||||
application::ApplicationHandler, event::WindowEvent, event_loop::ActiveEventLoop, raw_window_handle::{HasDisplayHandle, DisplayHandle, HandleError}, window::{Window, WindowId}
|
||||
};
|
||||
use winit::window::WindowAttributes;
|
||||
use crate::vulkan::VkInstance;
|
||||
use ash::vk::QueueFlags;
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event::WindowEvent;
|
||||
use winit::event_loop::ActiveEventLoop;
|
||||
use winit::window::{WindowId};
|
||||
use crate::display::window::Window;
|
||||
use crate::vulkan::{VkInstance, VkPhysicalDevice};
|
||||
|
||||
pub struct App {
|
||||
window_attributes: WindowAttributes,
|
||||
window: Option<Window>,
|
||||
instance: Option<VkInstance>,
|
||||
window: Window,
|
||||
instance: Option<VkInstance>
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new(window_attributes: WindowAttributes) -> Self {
|
||||
pub fn new(window: Window) -> Self {
|
||||
Self {
|
||||
window_attributes,
|
||||
window: None,
|
||||
instance: None,
|
||||
window,
|
||||
instance: None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl HasDisplayHandle for App {
|
||||
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
|
||||
self.window.as_ref()
|
||||
.ok_or_else(|| HandleError::Unavailable)?
|
||||
.display_handle()
|
||||
fn init_vulkan(&mut self) {
|
||||
let required_extensions = self.window
|
||||
.required_extensions()
|
||||
.expect("Unable to get required required extensions");
|
||||
|
||||
log::info!("Initializing Vulkan instance");
|
||||
let instance = VkInstance::new(&required_extensions);
|
||||
log::info!("Vulkan instance created");
|
||||
log::info!("\t{}", instance);
|
||||
|
||||
let surface = instance.create_surface(&self.window)
|
||||
.expect("Unable to create surface");
|
||||
|
||||
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.queue_family_properties
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find_map(|(index, queue_family_property)| {
|
||||
if surface.physical_device_queue_supported(physical_device, index as u32).unwrap_or(false) {
|
||||
Some((physical_device, index as u32))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
})
|
||||
.expect("Unable to find suitable device");
|
||||
|
||||
|
||||
self.instance = Some(instance);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl ApplicationHandler for App {
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
self.window = event_loop
|
||||
.create_window(self.window_attributes.clone())
|
||||
.ok();
|
||||
self.window.create_window(event_loop)
|
||||
.map_err(|err| format!("Failed to create window: {}", err))
|
||||
.unwrap();
|
||||
|
||||
self.instance = self.window
|
||||
.as_ref()
|
||||
.and_then(|w| Some(VkInstance::new(w)));
|
||||
|
||||
if let Some(instance) = self.instance.as_ref() {
|
||||
let physical_devices = instance.get_physical_devices();
|
||||
|
||||
log::info!("Physical Devices:");
|
||||
for physical_device in physical_devices {
|
||||
log::info!("{}", physical_device)
|
||||
}
|
||||
}
|
||||
self.init_vulkan();
|
||||
}
|
||||
|
||||
fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
|
||||
|
@ -55,10 +73,7 @@ impl ApplicationHandler for App {
|
|||
log::info!("The close button was pressed; stopping");
|
||||
event_loop.exit();
|
||||
}
|
||||
WindowEvent::RedrawRequested => {
|
||||
self.window.as_ref().unwrap().request_redraw();
|
||||
}
|
||||
_ => (),
|
||||
_ => self.window.window_event(event_loop, id, event),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,2 +1,5 @@
|
|||
mod app;
|
||||
pub use app::App;
|
||||
mod window;
|
||||
|
||||
pub use app::App;
|
||||
pub use window::Window;
|
63
src/display/window.rs
Normal file
63
src/display/window.rs
Normal file
|
@ -0,0 +1,63 @@
|
|||
use std::ffi::c_char;
|
||||
use winit::event::WindowEvent;
|
||||
use winit::event_loop::ActiveEventLoop;
|
||||
use winit::raw_window_handle::HasDisplayHandle;
|
||||
use winit::window::WindowId;
|
||||
|
||||
pub struct Window {
|
||||
handle: Option<winit::window::Window>,
|
||||
window_attributes: winit::window::WindowAttributes
|
||||
}
|
||||
|
||||
impl Window {
|
||||
pub fn new(window_attributes: winit::window::WindowAttributes) -> Self {
|
||||
Self {
|
||||
handle: None,
|
||||
window_attributes,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_window(&mut self, event_loop: &ActiveEventLoop) -> anyhow::Result<()> {
|
||||
let window = event_loop.create_window(self.window_attributes.clone())?;
|
||||
|
||||
self.handle = Some(window);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn required_extensions(&self) -> anyhow::Result<Vec<*const c_char>> {
|
||||
let display_handle = self.handle
|
||||
.as_ref()
|
||||
.ok_or_else(||anyhow::anyhow!("Window not found"))?
|
||||
.display_handle()?;
|
||||
|
||||
#[allow(unused_mut)]
|
||||
let mut extension_names = ash_window::enumerate_required_extensions(display_handle.as_raw())?
|
||||
.to_vec();
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
||||
{
|
||||
extension_names.push(ash::khr::portability_enumeration::NAME.as_ptr());
|
||||
// Enabling this extension is a requirement when using `VK_KHR_portability_subset`
|
||||
extension_names.push(ash::khr::get_physical_device_properties2::NAME.as_ptr());
|
||||
}
|
||||
|
||||
Ok(extension_names)
|
||||
}
|
||||
|
||||
pub fn handle(&self) -> Option<&winit::window::Window> {
|
||||
self.handle.as_ref()
|
||||
}
|
||||
|
||||
pub fn window_event(&mut self, _event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
|
||||
match event {
|
||||
WindowEvent::RedrawRequested => {
|
||||
match self.handle.as_ref() {
|
||||
Some(window) => window.request_redraw(),
|
||||
None => log::warn!("Redraw requested but no window found")
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue