Add VkInstance
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
This commit is contained in:
parent
433dc1afc8
commit
9223b89e4e
9 changed files with 237 additions and 122 deletions
51
src/display/app.rs
Normal file
51
src/display/app.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
use winit::{
|
||||
application::ApplicationHandler, event::WindowEvent, event_loop::ActiveEventLoop, raw_window_handle::{HasDisplayHandle, DisplayHandle, HandleError}, window::{Window, WindowId}
|
||||
};
|
||||
use crate::vulkan::VkInstance;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct App {
|
||||
window: Option<Window>,
|
||||
instance: Option<VkInstance>,
|
||||
}
|
||||
|
||||
impl HasDisplayHandle for App {
|
||||
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
|
||||
self.window.as_ref()
|
||||
.ok_or_else(|| HandleError::Unavailable)?
|
||||
.display_handle()
|
||||
}
|
||||
}
|
||||
|
||||
impl ApplicationHandler for App {
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
let window_attributes = Window::default_attributes()
|
||||
.with_title("Rust ASH Test")
|
||||
.with_visible(true)
|
||||
.with_inner_size(winit::dpi::LogicalSize::new(
|
||||
f64::from(800),
|
||||
f64::from(600),
|
||||
));
|
||||
|
||||
self.window = event_loop
|
||||
.create_window(window_attributes)
|
||||
.ok();
|
||||
|
||||
self.instance = self.window
|
||||
.as_ref()
|
||||
.and_then(|w| Some(VkInstance::new(w)));
|
||||
}
|
||||
|
||||
fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
|
||||
match event {
|
||||
WindowEvent::CloseRequested => {
|
||||
println!("The close button was pressed; stopping");
|
||||
event_loop.exit();
|
||||
}
|
||||
WindowEvent::RedrawRequested => {
|
||||
self.window.as_ref().unwrap().request_redraw();
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
2
src/display/mod.rs
Normal file
2
src/display/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
mod app;
|
||||
pub use app::App;
|
11
src/main.rs
11
src/main.rs
|
@ -1,3 +1,12 @@
|
|||
use winit::event_loop::EventLoop;
|
||||
|
||||
mod display;
|
||||
mod vulkan;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let event_loop = EventLoop::new().unwrap();
|
||||
|
||||
let mut app = display::App::default();
|
||||
|
||||
let _ = event_loop.run_app(&mut app);
|
||||
}
|
||||
|
|
2
src/vulkan/mod.rs
Normal file
2
src/vulkan/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
mod vk_instance;
|
||||
pub use vk_instance::VkInstance;
|
64
src/vulkan/vk_instance.rs
Normal file
64
src/vulkan/vk_instance.rs
Normal file
|
@ -0,0 +1,64 @@
|
|||
use std::ffi;
|
||||
use std::ffi::c_char;
|
||||
use ash::{Instance, vk, Entry};
|
||||
use winit::raw_window_handle::{HasDisplayHandle};
|
||||
|
||||
pub struct VkInstance {
|
||||
instance: 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();
|
||||
|
||||
let mut extension_names =
|
||||
ash_window::enumerate_required_extensions(window.display_handle().expect("No display handle").as_raw())
|
||||
.unwrap()
|
||||
.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());
|
||||
}
|
||||
|
||||
let appinfo = vk::ApplicationInfo::default()
|
||||
.application_name(app_name)
|
||||
.application_version(0)
|
||||
.engine_name(app_name)
|
||||
.engine_version(0)
|
||||
.api_version(vk::make_api_version(0, 1, 0, 0));
|
||||
|
||||
let create_flags = if cfg!(any(target_os = "macos", target_os = "ios")) {
|
||||
vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR
|
||||
} else {
|
||||
vk::InstanceCreateFlags::default()
|
||||
};
|
||||
|
||||
let create_info = vk::InstanceCreateInfo::default()
|
||||
.application_info(&appinfo)
|
||||
.enabled_layer_names(&layers_names_raw)
|
||||
.enabled_extension_names(&extension_names)
|
||||
.flags(create_flags);
|
||||
|
||||
let instance: Instance = unsafe {
|
||||
entry
|
||||
.create_instance(&create_info, None)
|
||||
.expect("Instance creation error")
|
||||
};
|
||||
|
||||
Self {
|
||||
instance
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue