#[cfg(not(feature = "vulkan"))]
fn main() {
eprintln!("run with: --features vulkan")
}
#[cfg(feature = "vulkan")]
use ash::vk;
#[cfg(feature = "vulkan")]
use std::ptr;
#[cfg(feature = "vulkan")]
fn main() {
use ash::vk::Handle;
let mut glfw = glfw::init_no_callbacks().unwrap();
glfw.window_hint(glfw::WindowHint::Visible(true));
glfw.window_hint(glfw::WindowHint::ClientApi(glfw::ClientApiHint::NoApi));
let (window, _) = glfw
.create_window(640, 480, "Defaults", glfw::WindowMode::Windowed)
.expect("Failed to create GLFW window.");
assert!(glfw.vulkan_supported());
let required_extensions = glfw.get_required_instance_extensions().unwrap_or(vec![]);
assert!(required_extensions.contains(&"VK_KHR_surface".to_string()));
println!("Vulkan required extensions: {:?}", required_extensions);
let entry = unsafe { ash::Entry::load().expect("Failed to load Vulkan library.") };
let instance: ash::Instance = unsafe { create_instance(&entry, &required_extensions) };
let mut surface: std::mem::MaybeUninit<vk::SurfaceKHR> = std::mem::MaybeUninit::uninit();
if unsafe {
window.create_window_surface(
instance.handle().as_raw() as _,
ptr::null(),
surface.as_mut_ptr() as _,
)
} != vk::Result::SUCCESS.as_raw()
{
panic!("Failed to create GLFW window surface.");
}
println!("Vulkan instance successfully created. Destruction is automatic with Drop.");
}
#[cfg(feature = "vulkan")]
unsafe fn create_instance(entry: &ash::Entry, extensions: &Vec<String>) -> ash::Instance {
let extensions: Vec<std::ffi::CString> = extensions
.iter()
.map(|ext| std::ffi::CString::new(ext.clone()).expect("Failed to convert extension name"))
.collect();
let extension_pointers: Vec<*const i8> = extensions.iter().map(|ext| ext.as_ptr()).collect();
let info: vk::InstanceCreateInfo =
vk::InstanceCreateInfo::default().enabled_extension_names(&extension_pointers);
unsafe {
entry
.create_instance(&info, None)
.expect("Unable to create instance.")
}
}