2023-04-12 11:23:21 -04:00
|
|
|
mod data;
|
2023-04-05 13:04:20 -04:00
|
|
|
mod functions;
|
2023-04-12 14:53:59 -04:00
|
|
|
mod structs;
|
2023-04-12 11:23:21 -04:00
|
|
|
|
|
|
|
use data::AppData;
|
2023-04-12 14:53:59 -04:00
|
|
|
use structs::*;
|
2023-04-05 13:04:20 -04:00
|
|
|
|
2023-04-05 10:48:25 -04:00
|
|
|
use ::anyhow::{anyhow, Result};
|
2023-04-12 10:28:48 -04:00
|
|
|
use ::lazy_static::lazy_static;
|
|
|
|
use ::nalgebra_glm as glm;
|
|
|
|
use ::std::mem::size_of;
|
2023-04-05 10:48:25 -04:00
|
|
|
use ::vulkanalia::loader::{LibloadingLoader, LIBRARY};
|
|
|
|
use ::vulkanalia::prelude::v1_0::*;
|
2023-04-12 14:53:59 -04:00
|
|
|
use ::vulkanalia::vk::{KhrSurfaceExtension, KhrSwapchainExtension};
|
2023-04-07 11:20:48 -04:00
|
|
|
use ::vulkanalia::Version;
|
2023-04-05 10:48:25 -04:00
|
|
|
use ::winit::window::Window;
|
2023-03-31 11:09:20 -04:00
|
|
|
|
2023-04-07 11:20:48 -04:00
|
|
|
/// The name of the validation layers.
|
|
|
|
pub const VALIDATION_LAYER: vk::ExtensionName =
|
2023-04-05 16:34:09 -04:00
|
|
|
vk::ExtensionName::from_bytes(b"VK_LAYER_KHRONOS_validation");
|
2023-04-07 11:20:48 -04:00
|
|
|
|
|
|
|
/// The Vulkan SDK version that started requiring the portability subset extension for macOS.
|
|
|
|
pub const PORTABILITY_MACOS_VERSION: Version = Version::new(1, 3, 216);
|
|
|
|
|
|
|
|
/// The required device extensions
|
|
|
|
pub const DEVICE_EXTENSIONS: &[vk::ExtensionName] = &[vk::KHR_SWAPCHAIN_EXTENSION.name];
|
|
|
|
|
|
|
|
/// The maximum number of frames that can be processed concurrently.
|
|
|
|
pub const MAX_FRAMES_IN_FLIGHT: usize = 2;
|
2023-04-05 16:34:09 -04:00
|
|
|
|
2023-03-31 10:50:34 -04:00
|
|
|
/// Our Vulkan app.
|
|
|
|
#[derive(Clone, Debug)]
|
2023-03-31 11:09:20 -04:00
|
|
|
pub struct App {
|
2023-04-07 14:48:12 -04:00
|
|
|
/// This value needs to stick around, but we don't use it directly
|
|
|
|
_entry: Entry,
|
2023-03-31 11:09:20 -04:00
|
|
|
instance: Instance,
|
2023-04-05 10:13:39 -04:00
|
|
|
data: AppData,
|
2023-04-07 11:20:48 -04:00
|
|
|
pub device: Device,
|
|
|
|
frame: usize,
|
2023-04-07 14:48:12 -04:00
|
|
|
pub resized: bool,
|
2023-03-31 11:09:20 -04:00
|
|
|
}
|
2023-03-31 10:50:34 -04:00
|
|
|
|
|
|
|
impl App {
|
|
|
|
/// Creates our Vulkan app.
|
2023-04-07 10:48:51 -04:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
/// Here be Dragons
|
2023-04-12 13:23:19 -04:00
|
|
|
pub fn create(window: &Window) -> Result<Self> {
|
|
|
|
unsafe {
|
|
|
|
let loader = LibloadingLoader::new(LIBRARY)?;
|
|
|
|
let entry = Entry::new(loader).map_err(|b| anyhow!("{}", b))?;
|
|
|
|
|
|
|
|
let mut data = AppData::default();
|
|
|
|
|
2023-04-12 14:53:59 -04:00
|
|
|
let (instance, device) = data.create(window, &entry)?;
|
2023-04-12 13:23:19 -04:00
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
_entry: entry,
|
|
|
|
instance,
|
|
|
|
data,
|
|
|
|
device,
|
|
|
|
frame: 0,
|
|
|
|
resized: false,
|
|
|
|
})
|
|
|
|
}
|
2023-03-31 10:50:34 -04:00
|
|
|
}
|
|
|
|
|
2023-04-07 14:48:12 -04:00
|
|
|
/// Destroys our Vulkan app, in reverse order of creation
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
/// Here be Dragons
|
2023-04-12 14:53:59 -04:00
|
|
|
pub fn destroy(&mut self) {
|
|
|
|
unsafe {
|
|
|
|
self.data.destroy_swapchain(&self.device);
|
|
|
|
self.data.destroy(&self.instance, &self.device);
|
2023-04-07 14:48:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:50:34 -04:00
|
|
|
/// Renders a frame for our Vulkan app.
|
2023-04-07 10:48:51 -04:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
/// Here be Dragons
|
2023-04-12 13:23:19 -04:00
|
|
|
pub fn render(&mut self, window: &Window) -> Result<()> {
|
|
|
|
unsafe {
|
|
|
|
let in_flight_fence = self.data.in_flight_fences[self.frame];
|
2023-04-07 12:18:44 -04:00
|
|
|
|
2023-04-07 14:48:12 -04:00
|
|
|
self.device
|
2023-04-12 13:23:19 -04:00
|
|
|
.wait_for_fences(&[in_flight_fence], true, u64::max_value())?;
|
|
|
|
let result = self.device.acquire_next_image_khr(
|
|
|
|
self.data.swapchain,
|
|
|
|
u64::max_value(),
|
|
|
|
self.data.image_available_semaphores[self.frame],
|
|
|
|
vk::Fence::null(),
|
|
|
|
);
|
|
|
|
let image_index = match result {
|
|
|
|
Ok((image_index, _)) => image_index as usize,
|
|
|
|
Err(vk::ErrorCode::OUT_OF_DATE_KHR) => return self.recreate_swapchain(window),
|
|
|
|
Err(e) => return Err(anyhow!(e)),
|
|
|
|
};
|
|
|
|
|
|
|
|
let image_in_flight = self.data.images_in_flight[image_index];
|
|
|
|
if !image_in_flight.is_null() {
|
|
|
|
self.device
|
|
|
|
.wait_for_fences(&[image_in_flight], true, u64::max_value())?;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.data.images_in_flight[image_index] = in_flight_fence;
|
|
|
|
|
|
|
|
let wait_semaphores = &[self.data.image_available_semaphores[self.frame]];
|
|
|
|
let wait_stages = &[vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT];
|
|
|
|
let command_buffers = &[self.data.command_buffers[image_index]];
|
|
|
|
let signal_semaphores = &[self.data.render_finished_semaphores[self.frame]];
|
|
|
|
let submit_info = vk::SubmitInfo::builder()
|
|
|
|
.wait_semaphores(wait_semaphores)
|
|
|
|
.wait_dst_stage_mask(wait_stages)
|
|
|
|
.command_buffers(command_buffers)
|
|
|
|
.signal_semaphores(signal_semaphores);
|
|
|
|
|
|
|
|
self.device.reset_fences(&[in_flight_fence])?;
|
2023-04-07 11:20:48 -04:00
|
|
|
|
2023-04-12 13:23:19 -04:00
|
|
|
self.device
|
|
|
|
.queue_submit(self.data.graphics_queue, &[submit_info], in_flight_fence)?;
|
|
|
|
|
|
|
|
let swapchains = &[self.data.swapchain];
|
|
|
|
let image_indices = &[image_index as u32];
|
|
|
|
let present_info = vk::PresentInfoKHR::builder()
|
|
|
|
.wait_semaphores(signal_semaphores)
|
|
|
|
.swapchains(swapchains)
|
|
|
|
.image_indices(image_indices);
|
|
|
|
|
|
|
|
let result = self
|
|
|
|
.device
|
|
|
|
.queue_present_khr(self.data.present_queue, &present_info);
|
|
|
|
|
|
|
|
let changed = result == Ok(vk::SuccessCode::SUBOPTIMAL_KHR)
|
|
|
|
|| result == Err(vk::ErrorCode::OUT_OF_DATE_KHR);
|
|
|
|
if self.resized || changed {
|
2023-04-12 14:53:59 -04:00
|
|
|
self.data
|
|
|
|
.recreate_swapchain(window, &self.instance, &self.device)?;
|
2023-04-12 13:23:19 -04:00
|
|
|
} else if let Err(e) = result {
|
|
|
|
return Err(anyhow!(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.frame = (self.frame + 1) % MAX_FRAMES_IN_FLIGHT;
|
2023-04-07 14:48:12 -04:00
|
|
|
}
|
2023-04-07 11:20:48 -04:00
|
|
|
|
2023-03-31 10:50:34 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-07 14:48:12 -04:00
|
|
|
/// Recreates the swapchain
|
2023-04-07 10:48:51 -04:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
/// Here be Dragons
|
2023-04-07 14:48:12 -04:00
|
|
|
fn recreate_swapchain(&mut self, window: &Window) -> Result<()> {
|
|
|
|
unsafe {
|
2023-04-12 11:23:21 -04:00
|
|
|
self.data
|
2023-04-12 14:53:59 -04:00
|
|
|
.recreate_swapchain(window, &self.instance, &self.device)?;
|
2023-04-05 10:13:39 -04:00
|
|
|
}
|
|
|
|
|
2023-04-07 14:48:12 -04:00
|
|
|
Ok(())
|
2023-03-31 11:09:20 -04:00
|
|
|
}
|
2023-03-31 10:50:34 -04:00
|
|
|
}
|