2016-12-26 00:38:26 +11:00
|
|
|
#[macro_use]
|
|
|
|
extern crate ash;
|
2016-12-26 11:58:44 +11:00
|
|
|
#[cfg(windows)]
|
2016-12-26 12:35:43 +11:00
|
|
|
extern crate winapi;
|
2017-12-13 00:25:10 +11:00
|
|
|
extern crate winit;
|
2016-12-26 00:38:26 +11:00
|
|
|
|
2018-07-31 22:38:11 +10:00
|
|
|
#[cfg(windows)]
|
|
|
|
use ash::extensions::Win32Surface;
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
use ash::extensions::XlibSurface;
|
2018-07-31 22:51:45 +10:00
|
|
|
use ash::extensions::{DebugReport, Surface, Swapchain};
|
|
|
|
pub use ash::version::{DeviceV1_0, EntryV1_0, InstanceV1_0, V1_0};
|
|
|
|
use ash::vk;
|
|
|
|
use ash::Device;
|
|
|
|
use ash::Entry;
|
|
|
|
use ash::Instance;
|
2017-12-13 00:25:10 +11:00
|
|
|
use std::cell::RefCell;
|
2018-07-31 22:51:45 +10:00
|
|
|
use std::default::Default;
|
2016-12-26 00:38:26 +11:00
|
|
|
use std::ffi::{CStr, CString};
|
|
|
|
use std::ops::Drop;
|
2018-07-31 22:51:45 +10:00
|
|
|
use std::ptr;
|
2016-12-26 00:38:26 +11:00
|
|
|
|
|
|
|
// Simple offset_of macro akin to C++ offsetof
|
|
|
|
#[macro_export]
|
2018-07-31 22:51:45 +10:00
|
|
|
macro_rules! offset_of {
|
|
|
|
($base:path, $field:ident) => {{
|
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
unsafe {
|
|
|
|
let b: $base = mem::uninitialized();
|
|
|
|
(&b.$field as *const _ as isize) - (&b as *const _ as isize)
|
2016-12-26 00:38:26 +11:00
|
|
|
}
|
2018-07-31 22:51:45 +10:00
|
|
|
}};
|
2016-12-26 00:38:26 +11:00
|
|
|
}
|
|
|
|
|
2017-12-13 00:25:10 +11:00
|
|
|
pub fn record_submit_commandbuffer<D: DeviceV1_0, F: FnOnce(&D, vk::CommandBuffer)>(
|
|
|
|
device: &D,
|
|
|
|
command_buffer: vk::CommandBuffer,
|
|
|
|
submit_queue: vk::Queue,
|
|
|
|
wait_mask: &[vk::PipelineStageFlags],
|
|
|
|
wait_semaphores: &[vk::Semaphore],
|
|
|
|
signal_semaphores: &[vk::Semaphore],
|
|
|
|
f: F,
|
|
|
|
) {
|
2016-12-26 00:38:26 +11:00
|
|
|
unsafe {
|
2017-12-13 00:25:10 +11:00
|
|
|
device
|
|
|
|
.reset_command_buffer(
|
|
|
|
command_buffer,
|
2018-07-31 20:45:29 +10:00
|
|
|
vk::CommandBufferResetFlags::RELEASE_RESOURCES,
|
2017-12-13 00:25:10 +11:00
|
|
|
)
|
2016-12-26 11:58:44 +11:00
|
|
|
.expect("Reset command buffer failed.");
|
2016-12-26 00:38:26 +11:00
|
|
|
let command_buffer_begin_info = vk::CommandBufferBeginInfo {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::COMMAND_BUFFER_BEGIN_INFO,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
p_inheritance_info: ptr::null(),
|
2018-07-31 20:45:29 +10:00
|
|
|
flags: vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT,
|
2016-12-26 00:38:26 +11:00
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
device
|
|
|
|
.begin_command_buffer(command_buffer, &command_buffer_begin_info)
|
2016-12-26 00:38:26 +11:00
|
|
|
.expect("Begin commandbuffer");
|
|
|
|
f(device, command_buffer);
|
2017-12-13 00:25:10 +11:00
|
|
|
device
|
|
|
|
.end_command_buffer(command_buffer)
|
|
|
|
.expect("End commandbuffer");
|
2016-12-26 00:38:26 +11:00
|
|
|
let fence_create_info = vk::FenceCreateInfo {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::FENCE_CREATE_INFO,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
flags: vk::FenceCreateFlags::empty(),
|
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
let submit_fence = device
|
|
|
|
.create_fence(&fence_create_info, None)
|
2016-12-28 19:24:24 +11:00
|
|
|
.expect("Create fence failed.");
|
2016-12-26 00:38:26 +11:00
|
|
|
let submit_info = vk::SubmitInfo {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::SUBMIT_INFO,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
wait_semaphore_count: wait_semaphores.len() as u32,
|
|
|
|
p_wait_semaphores: wait_semaphores.as_ptr(),
|
|
|
|
p_wait_dst_stage_mask: wait_mask.as_ptr(),
|
|
|
|
command_buffer_count: 1,
|
|
|
|
p_command_buffers: &command_buffer,
|
|
|
|
signal_semaphore_count: signal_semaphores.len() as u32,
|
|
|
|
p_signal_semaphores: signal_semaphores.as_ptr(),
|
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
device
|
|
|
|
.queue_submit(submit_queue, &[submit_info], submit_fence)
|
2016-12-26 11:58:44 +11:00
|
|
|
.expect("queue submit failed.");
|
2017-12-13 00:25:10 +11:00
|
|
|
device
|
|
|
|
.wait_for_fences(&[submit_fence], true, std::u64::MAX)
|
2016-12-26 11:58:44 +11:00
|
|
|
.expect("Wait for fence failed.");
|
2016-12-28 19:24:24 +11:00
|
|
|
device.destroy_fence(submit_fence, None);
|
2016-12-26 00:38:26 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(unix, not(target_os = "android")))]
|
2017-12-13 00:25:10 +11:00
|
|
|
unsafe fn create_surface<E: EntryV1_0, I: InstanceV1_0>(
|
|
|
|
entry: &E,
|
|
|
|
instance: &I,
|
|
|
|
window: &winit::Window,
|
|
|
|
) -> Result<vk::SurfaceKHR, vk::Result> {
|
2016-12-26 11:58:44 +11:00
|
|
|
use winit::os::unix::WindowExt;
|
2016-12-26 00:38:26 +11:00
|
|
|
let x11_display = window.get_xlib_display().unwrap();
|
|
|
|
let x11_window = window.get_xlib_window().unwrap();
|
|
|
|
let x11_create_info = vk::XlibSurfaceCreateInfoKHR {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::XLIB_SURFACE_CREATE_INFO_KHR,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
flags: Default::default(),
|
|
|
|
window: x11_window as vk::Window,
|
|
|
|
dpy: x11_display as *mut vk::Display,
|
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
let xlib_surface_loader =
|
|
|
|
XlibSurface::new(entry, instance).expect("Unable to load xlib surface");
|
2016-12-28 21:04:50 +11:00
|
|
|
xlib_surface_loader.create_xlib_surface_khr(&x11_create_info, None)
|
2016-12-26 00:38:26 +11:00
|
|
|
}
|
|
|
|
|
2016-12-26 11:58:44 +11:00
|
|
|
#[cfg(windows)]
|
2017-12-13 00:25:10 +11:00
|
|
|
unsafe fn create_surface<E: EntryV1_0, I: InstanceV1_0>(
|
|
|
|
entry: &E,
|
|
|
|
instance: &I,
|
|
|
|
window: &winit::Window,
|
|
|
|
) -> Result<vk::SurfaceKHR, vk::Result> {
|
2018-03-03 19:02:43 +11:00
|
|
|
use winapi::shared::windef::HWND;
|
|
|
|
use winapi::um::winuser::GetWindow;
|
2016-12-26 11:58:44 +11:00
|
|
|
use winit::os::windows::WindowExt;
|
2018-03-03 19:02:43 +11:00
|
|
|
|
|
|
|
let hwnd = window.get_hwnd() as HWND;
|
|
|
|
let hinstance = GetWindow(hwnd, 0) as *const vk::c_void;
|
2016-12-26 11:58:44 +11:00
|
|
|
let win32_create_info = vk::Win32SurfaceCreateInfoKHR {
|
2018-07-31 22:51:37 +10:00
|
|
|
s_type: vk::StructureType::WIN32_SURFACE_CREATE_INFO_KHR,
|
2016-12-26 11:58:44 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
flags: Default::default(),
|
|
|
|
hinstance: hinstance,
|
2017-08-13 04:30:01 +10:00
|
|
|
hwnd: hwnd as *const vk::c_void,
|
2016-12-26 11:58:44 +11:00
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
let win32_surface_loader =
|
|
|
|
Win32Surface::new(entry, instance).expect("Unable to load win32 surface");
|
2016-12-28 21:13:36 +11:00
|
|
|
win32_surface_loader.create_win32_surface_khr(&win32_create_info, None)
|
2016-12-26 11:58:44 +11:00
|
|
|
}
|
|
|
|
|
2016-12-26 00:38:26 +11:00
|
|
|
#[cfg(all(unix, not(target_os = "android")))]
|
2016-12-28 14:19:03 +11:00
|
|
|
fn extension_names() -> Vec<*const i8> {
|
2017-12-13 00:25:10 +11:00
|
|
|
vec![
|
|
|
|
Surface::name().as_ptr(),
|
|
|
|
XlibSurface::name().as_ptr(),
|
|
|
|
DebugReport::name().as_ptr(),
|
|
|
|
]
|
2016-12-26 00:38:26 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(windows))]
|
2016-12-28 14:19:03 +11:00
|
|
|
fn extension_names() -> Vec<*const i8> {
|
2017-12-13 00:25:10 +11:00
|
|
|
vec![
|
|
|
|
Surface::name().as_ptr(),
|
|
|
|
Win32Surface::name().as_ptr(),
|
|
|
|
DebugReport::name().as_ptr(),
|
|
|
|
]
|
2016-12-26 00:38:26 +11:00
|
|
|
}
|
|
|
|
|
2017-12-13 00:25:10 +11:00
|
|
|
unsafe extern "system" fn vulkan_debug_callback(
|
|
|
|
_: vk::DebugReportFlagsEXT,
|
|
|
|
_: vk::DebugReportObjectTypeEXT,
|
|
|
|
_: vk::uint64_t,
|
|
|
|
_: vk::size_t,
|
|
|
|
_: vk::int32_t,
|
|
|
|
_: *const vk::c_char,
|
|
|
|
p_message: *const vk::c_char,
|
|
|
|
_: *mut vk::c_void,
|
|
|
|
) -> u32 {
|
2016-12-26 00:38:26 +11:00
|
|
|
println!("{:?}", CStr::from_ptr(p_message));
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
2017-12-13 00:25:10 +11:00
|
|
|
pub fn find_memorytype_index(
|
|
|
|
memory_req: &vk::MemoryRequirements,
|
|
|
|
memory_prop: &vk::PhysicalDeviceMemoryProperties,
|
|
|
|
flags: vk::MemoryPropertyFlags,
|
|
|
|
) -> Option<u32> {
|
2016-12-29 16:02:37 +11:00
|
|
|
// Try to find an exactly matching memory flag
|
|
|
|
let best_suitable_index =
|
2017-12-13 00:25:10 +11:00
|
|
|
find_memorytype_index_f(memory_req, memory_prop, flags, |property_flags, flags| {
|
|
|
|
property_flags == flags
|
|
|
|
});
|
2016-12-29 16:02:37 +11:00
|
|
|
if best_suitable_index.is_some() {
|
|
|
|
return best_suitable_index;
|
|
|
|
}
|
|
|
|
// Otherwise find a memory flag that works
|
2017-12-13 00:25:10 +11:00
|
|
|
find_memorytype_index_f(memory_req, memory_prop, flags, |property_flags, flags| {
|
|
|
|
property_flags & flags == flags
|
|
|
|
})
|
2016-12-29 16:02:37 +11:00
|
|
|
}
|
|
|
|
|
2017-12-13 00:25:10 +11:00
|
|
|
pub fn find_memorytype_index_f<F: Fn(vk::MemoryPropertyFlags, vk::MemoryPropertyFlags) -> bool>(
|
|
|
|
memory_req: &vk::MemoryRequirements,
|
|
|
|
memory_prop: &vk::PhysicalDeviceMemoryProperties,
|
|
|
|
flags: vk::MemoryPropertyFlags,
|
|
|
|
f: F,
|
|
|
|
) -> Option<u32> {
|
2016-12-26 00:38:26 +11:00
|
|
|
let mut memory_type_bits = memory_req.memory_type_bits;
|
|
|
|
for (index, ref memory_type) in memory_prop.memory_types.iter().enumerate() {
|
2016-12-29 16:02:37 +11:00
|
|
|
if memory_type_bits & 1 == 1 {
|
|
|
|
if f(memory_type.property_flags, flags) {
|
|
|
|
return Some(index as u32);
|
|
|
|
}
|
2016-12-26 00:38:26 +11:00
|
|
|
}
|
|
|
|
memory_type_bits = memory_type_bits >> 1;
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2016-12-29 16:02:37 +11:00
|
|
|
|
2016-12-26 00:38:26 +11:00
|
|
|
pub struct ExampleBase {
|
2016-12-30 16:19:47 +11:00
|
|
|
pub entry: Entry<V1_0>,
|
2016-12-30 13:44:05 +11:00
|
|
|
pub instance: Instance<V1_0>,
|
2016-12-30 15:42:34 +11:00
|
|
|
pub device: Device<V1_0>,
|
2016-12-26 00:38:26 +11:00
|
|
|
pub surface_loader: Surface,
|
|
|
|
pub swapchain_loader: Swapchain,
|
|
|
|
pub debug_report_loader: DebugReport,
|
|
|
|
pub window: winit::Window,
|
2017-12-13 00:25:10 +11:00
|
|
|
pub events_loop: RefCell<winit::EventsLoop>,
|
2016-12-26 00:38:26 +11:00
|
|
|
pub debug_call_back: vk::DebugReportCallbackEXT,
|
|
|
|
|
|
|
|
pub pdevice: vk::PhysicalDevice,
|
|
|
|
pub device_memory_properties: vk::PhysicalDeviceMemoryProperties,
|
|
|
|
pub queue_family_index: u32,
|
|
|
|
pub present_queue: vk::Queue,
|
|
|
|
|
|
|
|
pub surface: vk::SurfaceKHR,
|
|
|
|
pub surface_format: vk::SurfaceFormatKHR,
|
|
|
|
pub surface_resolution: vk::Extent2D,
|
|
|
|
|
|
|
|
pub swapchain: vk::SwapchainKHR,
|
|
|
|
pub present_images: Vec<vk::Image>,
|
|
|
|
pub present_image_views: Vec<vk::ImageView>,
|
|
|
|
|
|
|
|
pub pool: vk::CommandPool,
|
|
|
|
pub draw_command_buffer: vk::CommandBuffer,
|
|
|
|
pub setup_command_buffer: vk::CommandBuffer,
|
|
|
|
|
|
|
|
pub depth_image: vk::Image,
|
|
|
|
pub depth_image_view: vk::ImageView,
|
|
|
|
pub depth_image_memory: vk::DeviceMemory,
|
|
|
|
|
|
|
|
pub present_complete_semaphore: vk::Semaphore,
|
|
|
|
pub rendering_complete_semaphore: vk::Semaphore,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ExampleBase {
|
|
|
|
pub fn render_loop<F: Fn()>(&self, f: F) {
|
2017-12-13 00:25:10 +11:00
|
|
|
use winit::*;
|
|
|
|
self.events_loop.borrow_mut().run_forever(|event| {
|
2016-12-26 00:38:26 +11:00
|
|
|
f();
|
2017-12-13 00:25:10 +11:00
|
|
|
match event {
|
|
|
|
Event::WindowEvent { event, .. } => match event {
|
|
|
|
WindowEvent::KeyboardInput { input, .. } => {
|
|
|
|
if let Some(VirtualKeyCode::Escape) = input.virtual_keycode {
|
|
|
|
ControlFlow::Break
|
|
|
|
} else {
|
|
|
|
ControlFlow::Continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WindowEvent::Closed => winit::ControlFlow::Break,
|
|
|
|
_ => ControlFlow::Continue,
|
|
|
|
},
|
|
|
|
_ => ControlFlow::Continue,
|
|
|
|
}
|
|
|
|
});
|
2016-12-26 00:38:26 +11:00
|
|
|
}
|
2018-03-03 18:59:28 +11:00
|
|
|
|
2016-12-26 00:38:26 +11:00
|
|
|
pub fn new(window_width: u32, window_height: u32) -> Self {
|
|
|
|
unsafe {
|
2017-12-13 00:25:10 +11:00
|
|
|
let events_loop = winit::EventsLoop::new();
|
2016-12-26 00:38:26 +11:00
|
|
|
let window = winit::WindowBuilder::new()
|
2017-01-05 18:09:23 +11:00
|
|
|
.with_title("Ash - Example")
|
2016-12-26 00:38:26 +11:00
|
|
|
.with_dimensions(window_width, window_height)
|
2017-12-13 00:25:10 +11:00
|
|
|
.build(&events_loop)
|
2016-12-26 00:38:26 +11:00
|
|
|
.unwrap();
|
2017-01-01 18:56:38 +11:00
|
|
|
let entry = Entry::new().unwrap();
|
2016-12-26 00:38:26 +11:00
|
|
|
let app_name = CString::new("VulkanTriangle").unwrap();
|
|
|
|
let raw_name = app_name.as_ptr();
|
|
|
|
|
|
|
|
let layer_names = [CString::new("VK_LAYER_LUNARG_standard_validation").unwrap()];
|
2017-12-13 00:25:10 +11:00
|
|
|
let layers_names_raw: Vec<*const i8> = layer_names
|
|
|
|
.iter()
|
2016-12-26 00:38:26 +11:00
|
|
|
.map(|raw_name| raw_name.as_ptr())
|
|
|
|
.collect();
|
2016-12-28 14:19:03 +11:00
|
|
|
let extension_names_raw = extension_names();
|
2016-12-26 00:38:26 +11:00
|
|
|
let appinfo = vk::ApplicationInfo {
|
|
|
|
p_application_name: raw_name,
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::APPLICATION_INFO,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
application_version: 0,
|
|
|
|
p_engine_name: raw_name,
|
|
|
|
engine_version: 0,
|
|
|
|
api_version: vk_make_version!(1, 0, 36),
|
|
|
|
};
|
|
|
|
let create_info = vk::InstanceCreateInfo {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::INSTANCE_CREATE_INFO,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
flags: Default::default(),
|
|
|
|
p_application_info: &appinfo,
|
|
|
|
pp_enabled_layer_names: layers_names_raw.as_ptr(),
|
|
|
|
enabled_layer_count: layers_names_raw.len() as u32,
|
|
|
|
pp_enabled_extension_names: extension_names_raw.as_ptr(),
|
|
|
|
enabled_extension_count: extension_names_raw.len() as u32,
|
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
let instance: Instance<V1_0> = entry
|
|
|
|
.create_instance(&create_info, None)
|
2016-12-26 00:38:26 +11:00
|
|
|
.expect("Instance creation error");
|
|
|
|
let debug_info = vk::DebugReportCallbackCreateInfoEXT {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
2018-07-31 22:51:45 +10:00
|
|
|
flags: vk::DebugReportFlagsEXT::ERROR_EXT
|
|
|
|
| vk::DebugReportFlagsEXT::WARNING_EXT
|
2018-07-31 22:31:52 +10:00
|
|
|
| vk::DebugReportFlagsEXT::PERFORMANCE_WARNING_EXT,
|
2016-12-26 00:38:26 +11:00
|
|
|
pfn_callback: vulkan_debug_callback,
|
|
|
|
p_user_data: ptr::null_mut(),
|
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
let debug_report_loader =
|
|
|
|
DebugReport::new(&entry, &instance).expect("Unable to load debug report");
|
|
|
|
let debug_call_back = debug_report_loader
|
|
|
|
.create_debug_report_callback_ext(&debug_info, None)
|
|
|
|
.unwrap();
|
2017-01-05 19:03:37 +11:00
|
|
|
let surface = create_surface(&entry, &instance, &window).unwrap();
|
2017-12-13 00:25:10 +11:00
|
|
|
let pdevices = instance
|
|
|
|
.enumerate_physical_devices()
|
|
|
|
.expect("Physical device error");
|
|
|
|
let surface_loader =
|
|
|
|
Surface::new(&entry, &instance).expect("Unable to load the Surface extension");
|
|
|
|
let (pdevice, queue_family_index) = pdevices
|
|
|
|
.iter()
|
2016-12-26 00:38:26 +11:00
|
|
|
.map(|pdevice| {
|
2017-12-13 00:25:10 +11:00
|
|
|
instance
|
|
|
|
.get_physical_device_queue_family_properties(*pdevice)
|
2016-12-26 00:38:26 +11:00
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(index, ref info)| {
|
2018-07-31 22:51:45 +10:00
|
|
|
let supports_graphic_and_surface =
|
|
|
|
info.queue_flags.subset(vk::QueueFlags::GRAPHICS)
|
|
|
|
&& surface_loader.get_physical_device_surface_support_khr(
|
|
|
|
*pdevice,
|
|
|
|
index as u32,
|
|
|
|
surface,
|
|
|
|
);
|
2016-12-26 00:38:26 +11:00
|
|
|
match supports_graphic_and_surface {
|
|
|
|
true => Some((*pdevice, index)),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.nth(0)
|
|
|
|
})
|
|
|
|
.filter_map(|v| v)
|
|
|
|
.nth(0)
|
|
|
|
.expect("Couldn't find suitable device.");
|
|
|
|
let queue_family_index = queue_family_index as u32;
|
2016-12-28 14:19:03 +11:00
|
|
|
let device_extension_names_raw = [Swapchain::name().as_ptr()];
|
2017-12-13 00:25:10 +11:00
|
|
|
let features = vk::PhysicalDeviceFeatures {
|
|
|
|
shader_clip_distance: 1,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2016-12-26 00:38:26 +11:00
|
|
|
let priorities = [1.0];
|
|
|
|
let queue_info = vk::DeviceQueueCreateInfo {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::DEVICE_QUEUE_CREATE_INFO,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
flags: Default::default(),
|
|
|
|
queue_family_index: queue_family_index as u32,
|
|
|
|
p_queue_priorities: priorities.as_ptr(),
|
|
|
|
queue_count: priorities.len() as u32,
|
|
|
|
};
|
|
|
|
let device_create_info = vk::DeviceCreateInfo {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::DEVICE_CREATE_INFO,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
flags: Default::default(),
|
|
|
|
queue_create_info_count: 1,
|
|
|
|
p_queue_create_infos: &queue_info,
|
|
|
|
enabled_layer_count: 0,
|
|
|
|
pp_enabled_layer_names: ptr::null(),
|
|
|
|
enabled_extension_count: device_extension_names_raw.len() as u32,
|
|
|
|
pp_enabled_extension_names: device_extension_names_raw.as_ptr(),
|
|
|
|
p_enabled_features: &features,
|
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
let device: Device<V1_0> = instance
|
|
|
|
.create_device(pdevice, &device_create_info, None)
|
2016-12-26 00:38:26 +11:00
|
|
|
.unwrap();
|
|
|
|
let present_queue = device.get_device_queue(queue_family_index as u32, 0);
|
|
|
|
|
2017-12-13 00:25:10 +11:00
|
|
|
let surface_formats = surface_loader
|
|
|
|
.get_physical_device_surface_formats_khr(pdevice, surface)
|
|
|
|
.unwrap();
|
|
|
|
let surface_format = surface_formats
|
|
|
|
.iter()
|
|
|
|
.map(|sfmt| match sfmt.format {
|
2018-07-31 20:45:29 +10:00
|
|
|
vk::Format::UNDEFINED => vk::SurfaceFormatKHR {
|
|
|
|
format: vk::Format::B8G8R8_UNORM,
|
2017-12-13 00:25:10 +11:00
|
|
|
color_space: sfmt.color_space,
|
|
|
|
},
|
|
|
|
_ => sfmt.clone(),
|
2016-12-26 00:38:26 +11:00
|
|
|
})
|
|
|
|
.nth(0)
|
|
|
|
.expect("Unable to find suitable surface format.");
|
2017-12-13 00:25:10 +11:00
|
|
|
let surface_capabilities = surface_loader
|
|
|
|
.get_physical_device_surface_capabilities_khr(pdevice, surface)
|
|
|
|
.unwrap();
|
2017-01-06 18:47:39 +11:00
|
|
|
let mut desired_image_count = surface_capabilities.min_image_count + 1;
|
2017-12-13 00:25:10 +11:00
|
|
|
if surface_capabilities.max_image_count > 0
|
|
|
|
&& desired_image_count > surface_capabilities.max_image_count
|
|
|
|
{
|
2017-01-06 18:47:39 +11:00
|
|
|
desired_image_count = surface_capabilities.max_image_count;
|
|
|
|
}
|
2016-12-26 00:38:26 +11:00
|
|
|
let surface_resolution = match surface_capabilities.current_extent.width {
|
2017-12-13 00:25:10 +11:00
|
|
|
std::u32::MAX => vk::Extent2D {
|
|
|
|
width: window_width,
|
|
|
|
height: window_height,
|
|
|
|
},
|
2016-12-26 00:38:26 +11:00
|
|
|
_ => surface_capabilities.current_extent,
|
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
let pre_transform = if surface_capabilities
|
|
|
|
.supported_transforms
|
2018-07-31 22:31:52 +10:00
|
|
|
.subset(vk::SurfaceTransformFlagsKHR::IDENTITY_KHR)
|
2017-12-13 00:25:10 +11:00
|
|
|
{
|
2018-07-31 22:31:52 +10:00
|
|
|
vk::SurfaceTransformFlagsKHR::IDENTITY_KHR
|
2016-12-26 00:38:26 +11:00
|
|
|
} else {
|
|
|
|
surface_capabilities.current_transform
|
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
let present_modes = surface_loader
|
|
|
|
.get_physical_device_surface_present_modes_khr(pdevice, surface)
|
|
|
|
.unwrap();
|
|
|
|
let present_mode = present_modes
|
|
|
|
.iter()
|
2016-12-26 00:38:26 +11:00
|
|
|
.cloned()
|
2018-07-31 22:31:52 +10:00
|
|
|
.find(|&mode| mode == vk::PresentModeKHR::MAILBOX_KHR)
|
|
|
|
.unwrap_or(vk::PresentModeKHR::FIFO_KHR);
|
2017-12-13 00:25:10 +11:00
|
|
|
let swapchain_loader =
|
|
|
|
Swapchain::new(&instance, &device).expect("Unable to load swapchain");
|
2016-12-26 00:38:26 +11:00
|
|
|
let swapchain_create_info = vk::SwapchainCreateInfoKHR {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::SWAPCHAIN_CREATE_INFO_KHR,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
flags: Default::default(),
|
|
|
|
surface: surface,
|
|
|
|
min_image_count: desired_image_count,
|
|
|
|
image_color_space: surface_format.color_space,
|
|
|
|
image_format: surface_format.format,
|
|
|
|
image_extent: surface_resolution.clone(),
|
2018-07-31 20:45:29 +10:00
|
|
|
image_usage: vk::ImageUsageFlags::COLOR_ATTACHMENT,
|
|
|
|
image_sharing_mode: vk::SharingMode::EXCLUSIVE,
|
2016-12-26 00:38:26 +11:00
|
|
|
pre_transform: pre_transform,
|
2018-07-31 22:31:52 +10:00
|
|
|
composite_alpha: vk::CompositeAlphaFlagsKHR::OPAQUE_KHR,
|
2016-12-26 00:38:26 +11:00
|
|
|
present_mode: present_mode,
|
|
|
|
clipped: 1,
|
|
|
|
old_swapchain: vk::SwapchainKHR::null(),
|
|
|
|
image_array_layers: 1,
|
|
|
|
p_queue_family_indices: ptr::null(),
|
|
|
|
queue_family_index_count: 0,
|
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
let swapchain = swapchain_loader
|
|
|
|
.create_swapchain_khr(&swapchain_create_info, None)
|
2016-12-29 16:02:37 +11:00
|
|
|
.unwrap();
|
2016-12-26 00:38:26 +11:00
|
|
|
let pool_create_info = vk::CommandPoolCreateInfo {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::COMMAND_POOL_CREATE_INFO,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
2018-07-31 20:45:29 +10:00
|
|
|
flags: vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER,
|
2016-12-26 00:38:26 +11:00
|
|
|
queue_family_index: queue_family_index,
|
|
|
|
};
|
2016-12-28 19:24:24 +11:00
|
|
|
let pool = device.create_command_pool(&pool_create_info, None).unwrap();
|
2016-12-26 00:38:26 +11:00
|
|
|
let command_buffer_allocate_info = vk::CommandBufferAllocateInfo {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::COMMAND_BUFFER_ALLOCATE_INFO,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
command_buffer_count: 2,
|
|
|
|
command_pool: pool,
|
2018-07-31 20:45:29 +10:00
|
|
|
level: vk::CommandBufferLevel::PRIMARY,
|
2016-12-26 00:38:26 +11:00
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
let command_buffers = device
|
|
|
|
.allocate_command_buffers(&command_buffer_allocate_info)
|
2016-12-26 00:38:26 +11:00
|
|
|
.unwrap();
|
|
|
|
let setup_command_buffer = command_buffers[0];
|
|
|
|
let draw_command_buffer = command_buffers[1];
|
|
|
|
|
2017-12-13 00:25:10 +11:00
|
|
|
let present_images = swapchain_loader
|
|
|
|
.get_swapchain_images_khr(swapchain)
|
|
|
|
.unwrap();
|
|
|
|
let present_image_views: Vec<vk::ImageView> = present_images
|
|
|
|
.iter()
|
2016-12-26 00:38:26 +11:00
|
|
|
.map(|&image| {
|
|
|
|
let create_view_info = vk::ImageViewCreateInfo {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::IMAGE_VIEW_CREATE_INFO,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
flags: Default::default(),
|
2018-07-31 20:45:29 +10:00
|
|
|
view_type: vk::ImageViewType::TYPE_2D,
|
2016-12-26 00:38:26 +11:00
|
|
|
format: surface_format.format,
|
|
|
|
components: vk::ComponentMapping {
|
|
|
|
r: vk::ComponentSwizzle::R,
|
|
|
|
g: vk::ComponentSwizzle::G,
|
|
|
|
b: vk::ComponentSwizzle::B,
|
|
|
|
a: vk::ComponentSwizzle::A,
|
|
|
|
},
|
|
|
|
subresource_range: vk::ImageSubresourceRange {
|
2018-07-31 20:45:29 +10:00
|
|
|
aspect_mask: vk::ImageAspectFlags::COLOR,
|
2016-12-26 00:38:26 +11:00
|
|
|
base_mip_level: 0,
|
|
|
|
level_count: 1,
|
|
|
|
base_array_layer: 0,
|
|
|
|
layer_count: 1,
|
|
|
|
},
|
|
|
|
image: image,
|
|
|
|
};
|
2016-12-28 19:24:24 +11:00
|
|
|
device.create_image_view(&create_view_info, None).unwrap()
|
2016-12-26 00:38:26 +11:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let device_memory_properties = instance.get_physical_device_memory_properties(pdevice);
|
|
|
|
let depth_image_create_info = vk::ImageCreateInfo {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::IMAGE_CREATE_INFO,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
2017-01-21 03:25:05 +11:00
|
|
|
flags: Default::default(),
|
2018-07-31 20:45:29 +10:00
|
|
|
image_type: vk::ImageType::TYPE_2D,
|
|
|
|
format: vk::Format::D16_UNORM,
|
2016-12-26 00:38:26 +11:00
|
|
|
extent: vk::Extent3D {
|
|
|
|
width: surface_resolution.width,
|
|
|
|
height: surface_resolution.height,
|
|
|
|
depth: 1,
|
|
|
|
},
|
|
|
|
mip_levels: 1,
|
|
|
|
array_layers: 1,
|
2018-07-31 20:45:29 +10:00
|
|
|
samples: vk::SampleCountFlags::TYPE_1,
|
|
|
|
tiling: vk::ImageTiling::OPTIMAL,
|
|
|
|
usage: vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT,
|
|
|
|
sharing_mode: vk::SharingMode::EXCLUSIVE,
|
2016-12-26 00:38:26 +11:00
|
|
|
queue_family_index_count: 0,
|
|
|
|
p_queue_family_indices: ptr::null(),
|
2018-07-31 20:45:29 +10:00
|
|
|
initial_layout: vk::ImageLayout::UNDEFINED,
|
2016-12-26 00:38:26 +11:00
|
|
|
};
|
2016-12-28 19:24:24 +11:00
|
|
|
let depth_image = device.create_image(&depth_image_create_info, None).unwrap();
|
2016-12-26 00:38:26 +11:00
|
|
|
let depth_image_memory_req = device.get_image_memory_requirements(depth_image);
|
|
|
|
let depth_image_memory_index =
|
2017-12-13 00:25:10 +11:00
|
|
|
find_memorytype_index(
|
|
|
|
&depth_image_memory_req,
|
|
|
|
&device_memory_properties,
|
2018-07-31 20:45:29 +10:00
|
|
|
vk::MemoryPropertyFlags::DEVICE_LOCAL,
|
2017-12-13 00:25:10 +11:00
|
|
|
).expect("Unable to find suitable memory index for depth image.");
|
2016-12-26 00:38:26 +11:00
|
|
|
|
|
|
|
let depth_image_allocate_info = vk::MemoryAllocateInfo {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::MEMORY_ALLOCATE_INFO,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
allocation_size: depth_image_memory_req.size,
|
|
|
|
memory_type_index: depth_image_memory_index,
|
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
let depth_image_memory = device
|
|
|
|
.allocate_memory(&depth_image_allocate_info, None)
|
2016-12-28 19:24:24 +11:00
|
|
|
.unwrap();
|
2017-12-13 00:25:10 +11:00
|
|
|
device
|
|
|
|
.bind_image_memory(depth_image, depth_image_memory, 0)
|
2016-12-26 00:38:26 +11:00
|
|
|
.expect("Unable to bind depth image memory");
|
2017-12-13 00:25:10 +11:00
|
|
|
record_submit_commandbuffer(
|
|
|
|
&device,
|
|
|
|
setup_command_buffer,
|
|
|
|
present_queue,
|
2018-07-31 20:45:29 +10:00
|
|
|
&[vk::PipelineStageFlags::BOTTOM_OF_PIPE],
|
2017-12-13 00:25:10 +11:00
|
|
|
&[],
|
|
|
|
&[],
|
|
|
|
|device, setup_command_buffer| {
|
|
|
|
let layout_transition_barrier = vk::ImageMemoryBarrier {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::IMAGE_MEMORY_BARRIER,
|
2017-12-13 00:25:10 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
src_access_mask: Default::default(),
|
2018-07-31 20:45:29 +10:00
|
|
|
dst_access_mask: vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_READ
|
|
|
|
| vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_WRITE,
|
|
|
|
old_layout: vk::ImageLayout::UNDEFINED,
|
|
|
|
new_layout: vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
|
2018-07-31 22:31:52 +10:00
|
|
|
src_queue_family_index: vk::VK_QUEUE_FAMILY_IGNORED,
|
|
|
|
dst_queue_family_index: vk::VK_QUEUE_FAMILY_IGNORED,
|
2017-12-13 00:25:10 +11:00
|
|
|
image: depth_image,
|
|
|
|
subresource_range: vk::ImageSubresourceRange {
|
2018-07-31 20:45:29 +10:00
|
|
|
aspect_mask: vk::ImageAspectFlags::DEPTH,
|
2017-12-13 00:25:10 +11:00
|
|
|
base_mip_level: 0,
|
|
|
|
level_count: 1,
|
|
|
|
base_array_layer: 0,
|
|
|
|
layer_count: 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
device.cmd_pipeline_barrier(
|
|
|
|
setup_command_buffer,
|
2018-07-31 20:45:29 +10:00
|
|
|
vk::PipelineStageFlags::BOTTOM_OF_PIPE,
|
|
|
|
vk::PipelineStageFlags::LATE_FRAGMENT_TESTS,
|
2017-12-13 00:25:10 +11:00
|
|
|
vk::DependencyFlags::empty(),
|
|
|
|
&[],
|
|
|
|
&[],
|
|
|
|
&[layout_transition_barrier],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2016-12-26 00:38:26 +11:00
|
|
|
let depth_image_view_info = vk::ImageViewCreateInfo {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::IMAGE_VIEW_CREATE_INFO,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
flags: Default::default(),
|
2018-07-31 20:45:29 +10:00
|
|
|
view_type: vk::ImageViewType::TYPE_2D,
|
2016-12-26 00:38:26 +11:00
|
|
|
format: depth_image_create_info.format,
|
|
|
|
components: vk::ComponentMapping {
|
2018-07-31 20:45:29 +10:00
|
|
|
r: vk::ComponentSwizzle::IDENTITY,
|
|
|
|
g: vk::ComponentSwizzle::IDENTITY,
|
|
|
|
b: vk::ComponentSwizzle::IDENTITY,
|
|
|
|
a: vk::ComponentSwizzle::IDENTITY,
|
2016-12-26 00:38:26 +11:00
|
|
|
},
|
|
|
|
subresource_range: vk::ImageSubresourceRange {
|
2018-07-31 20:45:29 +10:00
|
|
|
aspect_mask: vk::ImageAspectFlags::DEPTH,
|
2016-12-26 00:38:26 +11:00
|
|
|
base_mip_level: 0,
|
|
|
|
level_count: 1,
|
|
|
|
base_array_layer: 0,
|
|
|
|
layer_count: 1,
|
|
|
|
},
|
|
|
|
image: depth_image,
|
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
let depth_image_view = device
|
|
|
|
.create_image_view(&depth_image_view_info, None)
|
|
|
|
.unwrap();
|
2016-12-26 00:38:26 +11:00
|
|
|
let semaphore_create_info = vk::SemaphoreCreateInfo {
|
2018-07-31 20:45:29 +10:00
|
|
|
s_type: vk::StructureType::SEMAPHORE_CREATE_INFO,
|
2016-12-26 00:38:26 +11:00
|
|
|
p_next: ptr::null(),
|
|
|
|
flags: Default::default(),
|
|
|
|
};
|
2017-12-13 00:25:10 +11:00
|
|
|
let present_complete_semaphore = device
|
|
|
|
.create_semaphore(&semaphore_create_info, None)
|
2016-12-26 00:38:26 +11:00
|
|
|
.unwrap();
|
2017-12-13 00:25:10 +11:00
|
|
|
let rendering_complete_semaphore = device
|
|
|
|
.create_semaphore(&semaphore_create_info, None)
|
2016-12-26 00:38:26 +11:00
|
|
|
.unwrap();
|
|
|
|
ExampleBase {
|
2017-12-13 00:25:10 +11:00
|
|
|
events_loop: RefCell::new(events_loop),
|
2016-12-26 00:38:26 +11:00
|
|
|
entry: entry,
|
|
|
|
instance: instance,
|
|
|
|
device: device,
|
|
|
|
queue_family_index: queue_family_index,
|
|
|
|
pdevice: pdevice,
|
|
|
|
device_memory_properties: device_memory_properties,
|
|
|
|
window: window,
|
|
|
|
surface_loader: surface_loader,
|
|
|
|
surface_format: surface_format,
|
|
|
|
present_queue: present_queue,
|
|
|
|
surface_resolution: surface_resolution,
|
|
|
|
swapchain_loader: swapchain_loader,
|
|
|
|
swapchain: swapchain,
|
|
|
|
present_images: present_images,
|
|
|
|
present_image_views: present_image_views,
|
|
|
|
pool: pool,
|
|
|
|
draw_command_buffer: draw_command_buffer,
|
|
|
|
setup_command_buffer: setup_command_buffer,
|
|
|
|
depth_image: depth_image,
|
|
|
|
depth_image_view: depth_image_view,
|
|
|
|
present_complete_semaphore: present_complete_semaphore,
|
|
|
|
rendering_complete_semaphore: rendering_complete_semaphore,
|
|
|
|
surface: surface,
|
|
|
|
debug_call_back: debug_call_back,
|
|
|
|
debug_report_loader: debug_report_loader,
|
|
|
|
depth_image_memory: depth_image_memory,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-01 18:09:51 +11:00
|
|
|
|
2016-12-26 00:38:26 +11:00
|
|
|
impl Drop for ExampleBase {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
self.device.device_wait_idle().unwrap();
|
2017-12-13 00:25:10 +11:00
|
|
|
self.device
|
|
|
|
.destroy_semaphore(self.present_complete_semaphore, None);
|
|
|
|
self.device
|
|
|
|
.destroy_semaphore(self.rendering_complete_semaphore, None);
|
2016-12-29 16:02:37 +11:00
|
|
|
self.device.free_memory(self.depth_image_memory, None);
|
2016-12-28 19:24:24 +11:00
|
|
|
self.device.destroy_image_view(self.depth_image_view, None);
|
|
|
|
self.device.destroy_image(self.depth_image, None);
|
2016-12-26 00:38:26 +11:00
|
|
|
for &image_view in self.present_image_views.iter() {
|
2016-12-28 19:24:24 +11:00
|
|
|
self.device.destroy_image_view(image_view, None);
|
2016-12-26 00:38:26 +11:00
|
|
|
}
|
2016-12-28 19:24:24 +11:00
|
|
|
self.device.destroy_command_pool(self.pool, None);
|
2017-12-13 00:25:10 +11:00
|
|
|
self.swapchain_loader
|
|
|
|
.destroy_swapchain_khr(self.swapchain, None);
|
2016-12-28 19:24:24 +11:00
|
|
|
self.device.destroy_device(None);
|
2016-12-28 21:04:50 +11:00
|
|
|
self.surface_loader.destroy_surface_khr(self.surface, None);
|
2017-12-13 00:25:10 +11:00
|
|
|
self.debug_report_loader
|
|
|
|
.destroy_debug_report_callback_ext(self.debug_call_back, None);
|
2016-12-28 21:04:50 +11:00
|
|
|
self.instance.destroy_instance(None);
|
2016-12-26 00:38:26 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|