2023-04-05 13:04:20 -04:00
|
|
|
mod functions;
|
2023-04-06 14:35:50 -04:00
|
|
|
use functions::{
|
|
|
|
create_instance, create_logical_device, create_swapchain, create_swapchain_image_views,
|
|
|
|
pick_physical_device,
|
|
|
|
};
|
2023-04-05 13:04:20 -04:00
|
|
|
|
2023-04-05 10:48:25 -04:00
|
|
|
use crate::VALIDATION_ENABLED;
|
|
|
|
use ::anyhow::{anyhow, Result};
|
2023-04-05 11:08:36 -04:00
|
|
|
use ::log::*;
|
2023-04-05 10:48:25 -04:00
|
|
|
use ::thiserror::Error;
|
|
|
|
use ::vulkanalia::loader::{LibloadingLoader, LIBRARY};
|
|
|
|
use ::vulkanalia::prelude::v1_0::*;
|
2023-04-05 16:34:09 -04:00
|
|
|
use ::vulkanalia::vk::{ExtDebugUtilsExtension, KhrSurfaceExtension, KhrSwapchainExtension};
|
2023-04-05 15:41:32 -04:00
|
|
|
use ::vulkanalia::window as vk_window;
|
2023-04-05 10:48:25 -04:00
|
|
|
use ::winit::window::Window;
|
2023-03-31 11:09:20 -04:00
|
|
|
|
2023-04-05 16:34:09 -04:00
|
|
|
pub(crate) const VALIDATION_LAYER: vk::ExtensionName =
|
|
|
|
vk::ExtensionName::from_bytes(b"VK_LAYER_KHRONOS_validation");
|
|
|
|
pub(crate) const DEVICE_EXTENSIONS: &[vk::ExtensionName] = &[vk::KHR_SWAPCHAIN_EXTENSION.name];
|
|
|
|
|
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 {
|
|
|
|
entry: Entry,
|
|
|
|
instance: Instance,
|
2023-04-05 10:13:39 -04:00
|
|
|
data: AppData,
|
2023-04-05 13:04:20 -04:00
|
|
|
device: Device,
|
2023-03-31 11:09:20 -04:00
|
|
|
}
|
2023-03-31 10:50:34 -04:00
|
|
|
|
|
|
|
impl App {
|
|
|
|
/// Creates our Vulkan app.
|
|
|
|
pub unsafe fn create(window: &Window) -> Result<Self> {
|
2023-03-31 11:09:20 -04:00
|
|
|
let loader = LibloadingLoader::new(LIBRARY)?;
|
|
|
|
let entry = Entry::new(loader).map_err(|b| anyhow!("{}", b))?;
|
2023-04-05 10:13:39 -04:00
|
|
|
let mut data = AppData::default();
|
|
|
|
let instance = create_instance(window, &entry, &mut data)?;
|
2023-04-05 15:41:32 -04:00
|
|
|
data.surface = vk_window::create_surface(&instance, window)?;
|
2023-04-05 10:48:25 -04:00
|
|
|
pick_physical_device(&instance, &mut data)?;
|
2023-04-05 13:04:20 -04:00
|
|
|
let device = create_logical_device(&instance, &mut data)?;
|
2023-04-05 16:34:09 -04:00
|
|
|
create_swapchain(window, &instance, &device, &mut data)?;
|
2023-04-06 14:35:50 -04:00
|
|
|
create_swapchain_image_views(&device, &mut data)?;
|
2023-04-05 13:04:20 -04:00
|
|
|
|
2023-04-05 10:48:25 -04:00
|
|
|
Ok(Self {
|
|
|
|
entry,
|
|
|
|
instance,
|
|
|
|
data,
|
2023-04-05 13:04:20 -04:00
|
|
|
device,
|
2023-04-05 10:48:25 -04:00
|
|
|
})
|
2023-03-31 10:50:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Renders a frame for our Vulkan app.
|
2023-04-06 14:35:50 -04:00
|
|
|
pub unsafe fn render(&mut self, _window: &Window) -> Result<()> {
|
2023-03-31 10:50:34 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Destroys our Vulkan app.
|
2023-03-31 11:09:20 -04:00
|
|
|
pub unsafe fn destroy(&mut self) {
|
2023-04-06 14:35:50 -04:00
|
|
|
self.data
|
|
|
|
.swapchain_image_views
|
|
|
|
.iter()
|
|
|
|
.for_each(|v| self.device.destroy_image_view(*v, None));
|
2023-04-05 16:34:09 -04:00
|
|
|
self.device.destroy_swapchain_khr(self.data.swapchain, None);
|
2023-04-05 13:04:20 -04:00
|
|
|
self.device.destroy_device(None);
|
|
|
|
|
2023-04-05 10:13:39 -04:00
|
|
|
if VALIDATION_ENABLED {
|
2023-04-05 10:48:25 -04:00
|
|
|
self.instance
|
|
|
|
.destroy_debug_utils_messenger_ext(self.data.messenger, None);
|
2023-04-05 10:13:39 -04:00
|
|
|
}
|
|
|
|
|
2023-04-05 15:41:32 -04:00
|
|
|
self.instance.destroy_surface_khr(self.data.surface, None);
|
2023-03-31 11:09:20 -04:00
|
|
|
self.instance.destroy_instance(None);
|
|
|
|
}
|
2023-03-31 10:50:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The Vulkan handles and associated properties used by our Vulkan app.
|
|
|
|
#[derive(Clone, Debug, Default)]
|
2023-04-05 10:48:25 -04:00
|
|
|
pub struct AppData {
|
2023-04-05 15:41:32 -04:00
|
|
|
// Debug
|
2023-04-05 10:13:39 -04:00
|
|
|
messenger: vk::DebugUtilsMessengerEXT,
|
2023-04-05 16:34:09 -04:00
|
|
|
// Surface
|
|
|
|
surface: vk::SurfaceKHR,
|
2023-04-05 15:41:32 -04:00
|
|
|
// Physical Device / Logical Device
|
2023-04-05 11:08:36 -04:00
|
|
|
physical_device: vk::PhysicalDevice,
|
2023-04-05 13:04:20 -04:00
|
|
|
graphics_queue: vk::Queue,
|
2023-04-05 15:41:32 -04:00
|
|
|
present_queue: vk::Queue,
|
2023-04-05 16:34:09 -04:00
|
|
|
swapchain_format: vk::Format,
|
|
|
|
swapchain_extent: vk::Extent2D,
|
|
|
|
swapchain: vk::SwapchainKHR,
|
|
|
|
swapchain_images: Vec<vk::Image>,
|
2023-04-06 14:35:50 -04:00
|
|
|
swapchain_image_views: Vec<vk::ImageView>,
|
2023-04-05 10:13:39 -04:00
|
|
|
}
|
2023-04-05 10:48:25 -04:00
|
|
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
#[error("Missing {0}.")]
|
|
|
|
pub struct SuitabilityError(pub &'static str);
|
|
|
|
|
2023-04-05 11:08:36 -04:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2023-04-05 13:04:20 -04:00
|
|
|
pub(crate) struct QueueFamilyIndicies {
|
2023-04-05 11:08:36 -04:00
|
|
|
graphics: u32,
|
2023-04-05 15:41:32 -04:00
|
|
|
present: u32,
|
2023-04-05 11:08:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl QueueFamilyIndicies {
|
|
|
|
unsafe fn get(
|
|
|
|
instance: &Instance,
|
|
|
|
data: &AppData,
|
|
|
|
physical_device: vk::PhysicalDevice,
|
|
|
|
) -> Result<Self> {
|
|
|
|
let properties = instance.get_physical_device_queue_family_properties(physical_device);
|
|
|
|
|
|
|
|
let graphics = properties
|
|
|
|
.iter()
|
|
|
|
.position(|p| p.queue_flags.contains(vk::QueueFlags::GRAPHICS))
|
|
|
|
.map(|i| i as u32);
|
|
|
|
|
2023-04-05 15:41:32 -04:00
|
|
|
let mut present = None;
|
2023-04-06 14:35:50 -04:00
|
|
|
for (index, _properties) in properties.iter().enumerate() {
|
2023-04-05 15:41:32 -04:00
|
|
|
if instance.get_physical_device_surface_support_khr(
|
|
|
|
physical_device,
|
|
|
|
index as u32,
|
|
|
|
data.surface,
|
|
|
|
)? {
|
|
|
|
present = Some(index as u32);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let (Some(graphics), Some(present)) = (graphics, present) {
|
|
|
|
Ok(Self { graphics, present })
|
2023-04-05 11:08:36 -04:00
|
|
|
} else {
|
|
|
|
Err(anyhow!(SuitabilityError(
|
|
|
|
"Missing required queue families."
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-05 16:34:09 -04:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub(crate) struct SwapchainSupport {
|
|
|
|
capabilities: vk::SurfaceCapabilitiesKHR,
|
|
|
|
formats: Vec<vk::SurfaceFormatKHR>,
|
|
|
|
present_modes: Vec<vk::PresentModeKHR>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SwapchainSupport {
|
|
|
|
unsafe fn get(
|
|
|
|
instance: &Instance,
|
|
|
|
data: &AppData,
|
|
|
|
physical_device: vk::PhysicalDevice,
|
|
|
|
) -> Result<Self> {
|
|
|
|
Ok(Self {
|
|
|
|
capabilities: instance
|
|
|
|
.get_physical_device_surface_capabilities_khr(physical_device, data.surface)?,
|
|
|
|
formats: instance
|
|
|
|
.get_physical_device_surface_formats_khr(physical_device, data.surface)?,
|
|
|
|
present_modes: instance
|
|
|
|
.get_physical_device_surface_present_modes_khr(physical_device, data.surface)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|