2023-04-05 13:04:20 -04:00
|
|
|
use super::*;
|
2023-04-05 16:34:09 -04:00
|
|
|
use crate::VALIDATION_ENABLED;
|
2023-04-05 13:04:20 -04:00
|
|
|
|
|
|
|
use ::anyhow::{anyhow, Result};
|
|
|
|
use ::log::*;
|
|
|
|
use ::std::collections::HashSet;
|
|
|
|
use ::std::ffi::CStr;
|
|
|
|
use ::std::os::raw::c_void;
|
|
|
|
use ::vulkanalia::prelude::v1_0::*;
|
|
|
|
use ::vulkanalia::window as vk_window;
|
|
|
|
use ::winit::window::Window;
|
|
|
|
|
|
|
|
extern "system" fn debug_callback(
|
|
|
|
severity: vk::DebugUtilsMessageSeverityFlagsEXT,
|
|
|
|
type_: vk::DebugUtilsMessageTypeFlagsEXT,
|
|
|
|
data: *const vk::DebugUtilsMessengerCallbackDataEXT,
|
|
|
|
_: *mut c_void,
|
|
|
|
) -> vk::Bool32 {
|
|
|
|
let data = unsafe { *data };
|
|
|
|
let message = unsafe { CStr::from_ptr(data.message) }.to_string_lossy();
|
|
|
|
|
|
|
|
if severity >= vk::DebugUtilsMessageSeverityFlagsEXT::ERROR {
|
|
|
|
error!("({:?}) {}", type_, message);
|
|
|
|
} else if severity >= vk::DebugUtilsMessageSeverityFlagsEXT::WARNING {
|
|
|
|
warn!("({:?}) {}", type_, message);
|
|
|
|
} else if severity >= vk::DebugUtilsMessageSeverityFlagsEXT::INFO {
|
|
|
|
debug!("({:?}) {}", type_, message);
|
|
|
|
} else {
|
|
|
|
trace!("({:?}) {}", type_, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
vk::FALSE
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) unsafe fn create_instance(
|
|
|
|
window: &Window,
|
|
|
|
entry: &Entry,
|
|
|
|
data: &mut AppData,
|
|
|
|
) -> Result<Instance> {
|
2023-04-05 15:41:32 -04:00
|
|
|
// Application Info
|
2023-04-05 13:04:20 -04:00
|
|
|
let application_info = vk::ApplicationInfo::builder()
|
|
|
|
.application_name(b"Vulkan Tutorial\0")
|
|
|
|
.application_version(vk::make_version(1, 0, 0))
|
|
|
|
.engine_name(b"No Engine\0")
|
|
|
|
.engine_version(vk::make_version(1, 0, 0))
|
|
|
|
.api_version(vk::make_version(1, 0, 0));
|
|
|
|
|
2023-04-05 15:41:32 -04:00
|
|
|
// Layers
|
2023-04-05 13:04:20 -04:00
|
|
|
let available_layers = entry
|
|
|
|
.enumerate_instance_layer_properties()?
|
|
|
|
.iter()
|
|
|
|
.map(|l| l.layer_name)
|
|
|
|
.collect::<HashSet<_>>();
|
|
|
|
|
|
|
|
if VALIDATION_ENABLED && !available_layers.contains(&VALIDATION_LAYER) {
|
|
|
|
return Err(anyhow!("Validation layer requested but not supported."));
|
|
|
|
}
|
|
|
|
|
|
|
|
let layers = if VALIDATION_ENABLED {
|
|
|
|
vec![VALIDATION_LAYER.as_ptr()]
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
};
|
|
|
|
|
2023-04-05 15:41:32 -04:00
|
|
|
// Extensions
|
2023-04-05 13:04:20 -04:00
|
|
|
let mut extensions = vk_window::get_required_instance_extensions(window)
|
|
|
|
.iter()
|
|
|
|
.map(|e| e.as_ptr())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
if VALIDATION_ENABLED {
|
|
|
|
extensions.push(vk::EXT_DEBUG_UTILS_EXTENSION.name.as_ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compatibility extension for macOS
|
|
|
|
let flags = if entry
|
|
|
|
.enumerate_instance_extension_properties(None)?
|
|
|
|
.iter()
|
|
|
|
.any(|e| e.extension_name == vk::KHR_PORTABILITY_ENUMERATION_EXTENSION.name)
|
|
|
|
{
|
|
|
|
extensions.push(vk::KHR_PORTABILITY_ENUMERATION_EXTENSION.name.as_ptr());
|
2023-04-05 15:41:32 -04:00
|
|
|
extensions.push(
|
|
|
|
vk::KHR_GET_PHYSICAL_DEVICE_PROPERTIES2_EXTENSION
|
|
|
|
.name
|
|
|
|
.as_ptr(),
|
|
|
|
);
|
2023-04-05 13:04:20 -04:00
|
|
|
vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR
|
|
|
|
} else {
|
|
|
|
vk::InstanceCreateFlags::empty()
|
|
|
|
};
|
|
|
|
|
2023-04-05 15:41:32 -04:00
|
|
|
// Create
|
2023-04-05 13:04:20 -04:00
|
|
|
let mut info = vk::InstanceCreateInfo::builder()
|
|
|
|
.application_info(&application_info)
|
|
|
|
.enabled_layer_names(&layers)
|
|
|
|
.enabled_extension_names(&extensions)
|
|
|
|
.flags(flags);
|
|
|
|
|
|
|
|
let mut debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder()
|
|
|
|
.message_severity(vk::DebugUtilsMessageSeverityFlagsEXT::all())
|
|
|
|
.message_type(vk::DebugUtilsMessageTypeFlagsEXT::all())
|
|
|
|
.user_callback(Some(debug_callback));
|
|
|
|
|
|
|
|
if VALIDATION_ENABLED {
|
|
|
|
info = info.push_next(&mut debug_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
let instance = entry.create_instance(&info, None)?;
|
|
|
|
|
2023-04-05 15:41:32 -04:00
|
|
|
// Messenger
|
|
|
|
if VALIDATION_ENABLED {
|
|
|
|
data.messenger = instance.create_debug_utils_messenger_ext(&debug_info, None)?;
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:04:20 -04:00
|
|
|
Ok(instance)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) unsafe fn pick_physical_device(instance: &Instance, data: &mut AppData) -> Result<()> {
|
|
|
|
for physical_device in instance.enumerate_physical_devices()? {
|
|
|
|
let properties = instance.get_physical_device_properties(physical_device);
|
|
|
|
|
|
|
|
if let Err(error) = check_physical_device(instance, data, physical_device) {
|
|
|
|
warn!(
|
|
|
|
"Skipping physical device (`{}`): {}",
|
|
|
|
properties.device_name, error
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
info!("Selected physical device (`{}`).", properties.device_name);
|
|
|
|
data.physical_device = physical_device;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(anyhow!("Failed to find suitable physical device."))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) unsafe fn check_physical_device(
|
|
|
|
instance: &Instance,
|
|
|
|
data: &AppData,
|
|
|
|
physical_device: vk::PhysicalDevice,
|
|
|
|
) -> Result<()> {
|
|
|
|
QueueFamilyIndicies::get(instance, data, physical_device)?;
|
2023-04-05 16:34:09 -04:00
|
|
|
check_physical_device_extensions(instance, physical_device)?;
|
|
|
|
|
|
|
|
let support = SwapchainSupport::get(instance, data, physical_device)?;
|
|
|
|
if support.formats.is_empty() || support.present_modes.is_empty() {
|
|
|
|
return Err(anyhow!(SuitabilityError("Insufficient swapchain support.")));
|
|
|
|
}
|
2023-04-05 13:04:20 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-04-05 16:34:09 -04:00
|
|
|
pub(super) unsafe fn check_physical_device_extensions(
|
|
|
|
instance: &Instance,
|
|
|
|
physical_device: vk::PhysicalDevice,
|
|
|
|
) -> Result<()> {
|
|
|
|
let extensions = instance
|
|
|
|
.enumerate_device_extension_properties(physical_device, None)?
|
|
|
|
.iter()
|
|
|
|
.map(|e| e.extension_name)
|
|
|
|
.collect::<HashSet<_>>();
|
|
|
|
|
|
|
|
if DEVICE_EXTENSIONS.iter().all(|e| extensions.contains(e)) {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(anyhow!(SuitabilityError(
|
|
|
|
"Missing required device extensions."
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:04:20 -04:00
|
|
|
pub(super) unsafe fn create_logical_device(
|
|
|
|
instance: &Instance,
|
|
|
|
data: &mut AppData,
|
|
|
|
) -> Result<Device> {
|
2023-04-05 15:41:32 -04:00
|
|
|
// Queue Create Infos
|
2023-04-05 13:04:20 -04:00
|
|
|
let indices = QueueFamilyIndicies::get(instance, data, data.physical_device)?;
|
|
|
|
|
2023-04-05 15:41:32 -04:00
|
|
|
let mut unique_indices = HashSet::new();
|
|
|
|
unique_indices.insert(indices.graphics);
|
|
|
|
unique_indices.insert(indices.present);
|
|
|
|
|
2023-04-05 13:04:20 -04:00
|
|
|
let queue_priorities = &[1.0];
|
2023-04-05 15:41:32 -04:00
|
|
|
let queue_infos = unique_indices
|
|
|
|
.iter()
|
|
|
|
.map(|i| {
|
|
|
|
vk::DeviceQueueCreateInfo::builder()
|
|
|
|
.queue_family_index(*i)
|
|
|
|
.queue_priorities(queue_priorities)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2023-04-05 13:04:20 -04:00
|
|
|
|
2023-04-05 15:41:32 -04:00
|
|
|
// Layers
|
2023-04-05 13:04:20 -04:00
|
|
|
let layers = if VALIDATION_ENABLED {
|
|
|
|
vec![VALIDATION_LAYER.as_ptr()]
|
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
};
|
|
|
|
|
2023-04-05 15:41:32 -04:00
|
|
|
// Extensions
|
2023-04-05 16:34:09 -04:00
|
|
|
let mut extensions = DEVICE_EXTENSIONS
|
|
|
|
.iter()
|
|
|
|
.map(|n| n.as_ptr())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// mac OS Metal -> Vulkan rendering fix
|
|
|
|
if instance
|
2023-04-05 15:41:32 -04:00
|
|
|
.enumerate_device_extension_properties(data.physical_device, None)?
|
|
|
|
.iter()
|
|
|
|
.any(|e| e.extension_name == vk::KHR_PORTABILITY_SUBSET_EXTENSION.name)
|
|
|
|
{
|
2023-04-05 16:34:09 -04:00
|
|
|
extensions.push(vk::KHR_PORTABILITY_SUBSET_EXTENSION.name.as_ptr());
|
|
|
|
}
|
2023-04-05 15:41:32 -04:00
|
|
|
|
|
|
|
// Features
|
2023-04-05 13:04:20 -04:00
|
|
|
let features = vk::PhysicalDeviceFeatures::builder();
|
|
|
|
|
2023-04-05 15:41:32 -04:00
|
|
|
// Create
|
2023-04-05 13:04:20 -04:00
|
|
|
let info = vk::DeviceCreateInfo::builder()
|
2023-04-05 15:41:32 -04:00
|
|
|
.queue_create_infos(&queue_infos)
|
2023-04-05 13:04:20 -04:00
|
|
|
.enabled_layer_names(&layers)
|
2023-04-05 16:34:09 -04:00
|
|
|
.enabled_extension_names(&extensions)
|
|
|
|
.enabled_features(&features);
|
2023-04-05 13:04:20 -04:00
|
|
|
|
|
|
|
let device = instance.create_device(data.physical_device, &info, None)?;
|
|
|
|
|
2023-04-05 15:41:32 -04:00
|
|
|
// Queues
|
2023-04-05 13:04:20 -04:00
|
|
|
data.graphics_queue = device.get_device_queue(indices.graphics, 0);
|
2023-04-05 15:41:32 -04:00
|
|
|
data.present_queue = device.get_device_queue(indices.present, 0);
|
2023-04-05 13:04:20 -04:00
|
|
|
|
|
|
|
Ok(device)
|
|
|
|
}
|
2023-04-05 16:34:09 -04:00
|
|
|
|
|
|
|
pub(super) fn get_swapchain_surface_format(
|
|
|
|
formats: &[vk::SurfaceFormatKHR],
|
|
|
|
) -> vk::SurfaceFormatKHR {
|
|
|
|
formats
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.find(|f| {
|
|
|
|
f.format == vk::Format::B8G8R8A8_SRGB
|
|
|
|
&& f.color_space == vk::ColorSpaceKHR::SRGB_NONLINEAR
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| formats[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn get_swapchain_present_mode(
|
|
|
|
present_modes: &[vk::PresentModeKHR],
|
|
|
|
) -> vk::PresentModeKHR {
|
|
|
|
present_modes
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.find(|m| *m == vk::PresentModeKHR::MAILBOX)
|
|
|
|
.unwrap_or(vk::PresentModeKHR::FIFO)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn get_swapchain_extent(
|
|
|
|
window: &Window,
|
|
|
|
capabilities: vk::SurfaceCapabilitiesKHR,
|
|
|
|
) -> vk::Extent2D {
|
|
|
|
if capabilities.current_extent.width != u32::MAX {
|
|
|
|
capabilities.current_extent
|
|
|
|
} else {
|
|
|
|
let size = window.inner_size();
|
|
|
|
let clamp = |min: u32, max: u32, v: u32| min.max(max.min(v));
|
|
|
|
vk::Extent2D::builder()
|
|
|
|
.width(clamp(
|
|
|
|
capabilities.min_image_extent.width,
|
|
|
|
capabilities.max_image_extent.width,
|
|
|
|
size.width,
|
|
|
|
))
|
|
|
|
.height(clamp(
|
|
|
|
capabilities.min_image_extent.height,
|
|
|
|
capabilities.max_image_extent.height,
|
|
|
|
size.height,
|
|
|
|
))
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) unsafe fn create_swapchain(
|
|
|
|
window: &Window,
|
|
|
|
instance: &Instance,
|
|
|
|
device: &Device,
|
|
|
|
data: &mut AppData,
|
|
|
|
) -> Result<()> {
|
|
|
|
let indices = QueueFamilyIndicies::get(instance, data, data.physical_device)?;
|
|
|
|
let support = SwapchainSupport::get(instance, data, data.physical_device)?;
|
|
|
|
|
|
|
|
let surface_format = get_swapchain_surface_format(&support.formats);
|
|
|
|
let present_mode = get_swapchain_present_mode(&support.present_modes);
|
|
|
|
let extent = get_swapchain_extent(window, support.capabilities);
|
|
|
|
|
|
|
|
let mut image_count = support.capabilities.min_image_count + 1;
|
|
|
|
if support.capabilities.max_image_count != 0
|
|
|
|
&& image_count > support.capabilities.max_image_count
|
|
|
|
{
|
|
|
|
image_count = support.capabilities.max_image_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut queue_family_indices = vec![];
|
|
|
|
let image_sharing_mode = if indices.graphics != indices.present {
|
|
|
|
queue_family_indices.push(indices.graphics);
|
|
|
|
queue_family_indices.push(indices.present);
|
|
|
|
vk::SharingMode::CONCURRENT
|
|
|
|
} else {
|
|
|
|
vk::SharingMode::EXCLUSIVE
|
|
|
|
};
|
|
|
|
|
|
|
|
let info = vk::SwapchainCreateInfoKHR::builder()
|
|
|
|
.surface(data.surface)
|
|
|
|
.min_image_count(image_count)
|
|
|
|
.image_format(surface_format.format)
|
|
|
|
.image_color_space(surface_format.color_space)
|
|
|
|
.image_extent(extent)
|
|
|
|
.image_array_layers(1)
|
|
|
|
.image_usage(vk::ImageUsageFlags::COLOR_ATTACHMENT)
|
|
|
|
.image_sharing_mode(image_sharing_mode)
|
|
|
|
.queue_family_indices(&queue_family_indices)
|
|
|
|
.pre_transform(support.capabilities.current_transform)
|
|
|
|
.composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE)
|
|
|
|
.present_mode(present_mode)
|
|
|
|
.clipped(true)
|
|
|
|
.old_swapchain(vk::SwapchainKHR::null());
|
|
|
|
|
|
|
|
data.swapchain = device.create_swapchain_khr(&info, None)?;
|
|
|
|
data.swapchain_images = device.get_swapchain_images_khr(data.swapchain)?;
|
|
|
|
data.swapchain_format = surface_format.format;
|
|
|
|
data.swapchain_extent = extent;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-04-06 14:35:50 -04:00
|
|
|
|
|
|
|
pub(super) unsafe fn create_swapchain_image_views(
|
|
|
|
device: &Device,
|
|
|
|
data: &mut AppData,
|
|
|
|
) -> Result<()> {
|
|
|
|
data.swapchain_image_views = data
|
|
|
|
.swapchain_images
|
|
|
|
.iter()
|
|
|
|
.map(|i| {
|
|
|
|
let components = vk::ComponentMapping::builder()
|
|
|
|
.r(vk::ComponentSwizzle::IDENTITY)
|
|
|
|
.g(vk::ComponentSwizzle::IDENTITY)
|
|
|
|
.b(vk::ComponentSwizzle::IDENTITY)
|
|
|
|
.a(vk::ComponentSwizzle::IDENTITY);
|
|
|
|
|
|
|
|
let subresource_range = vk::ImageSubresourceRange::builder()
|
|
|
|
.aspect_mask(vk::ImageAspectFlags::COLOR)
|
|
|
|
.base_mip_level(0)
|
|
|
|
.level_count(1)
|
|
|
|
.base_array_layer(0)
|
|
|
|
.layer_count(1);
|
|
|
|
|
|
|
|
let info = vk::ImageViewCreateInfo::builder()
|
|
|
|
.image(*i)
|
|
|
|
.view_type(vk::ImageViewType::_2D)
|
|
|
|
.format(data.swapchain_format)
|
|
|
|
.components(components)
|
|
|
|
.subresource_range(subresource_range);
|
|
|
|
|
|
|
|
device.create_image_view(&info, None)
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|