vulkan: update + cleanup

This commit is contained in:
Alex Janka 2024-08-18 14:27:36 +10:00
parent 11512e92ea
commit 27464daae3
4 changed files with 83 additions and 163 deletions

View file

@ -1,13 +1,13 @@
use ash::{extensions::ext::DebugUtils, vk, Entry, Instance};
use ash::{ext::debug_utils, vk, Entry, Instance};
pub(super) struct VulkanDebug {
debug_utils_loader: DebugUtils,
debug_utils_loader: debug_utils::Instance,
debug_call_back: vk::DebugUtilsMessengerEXT,
}
impl VulkanDebug {
pub(super) fn new(entry: &Entry, instance: &Instance) -> Self {
let debug_info = vk::DebugUtilsMessengerCreateInfoEXT::builder()
let debug_info = vk::DebugUtilsMessengerCreateInfoEXT::default()
.message_severity(
vk::DebugUtilsMessageSeverityFlagsEXT::ERROR
| vk::DebugUtilsMessageSeverityFlagsEXT::WARNING
@ -19,10 +19,9 @@ impl VulkanDebug {
| vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION
| vk::DebugUtilsMessageTypeFlagsEXT::PERFORMANCE,
)
.pfn_user_callback(Some(vulkan_debug_callback))
.build();
.pfn_user_callback(Some(vulkan_debug_callback));
let debug_utils_loader = DebugUtils::new(entry, instance);
let debug_utils_loader = debug_utils::Instance::new(entry, instance);
let debug_call_back =
unsafe { debug_utils_loader.create_debug_utils_messenger(&debug_info, None) }.unwrap();

View file

@ -4,13 +4,10 @@ use librashader::runtime::vk::{FilterChain, FilterChainOptions, FrameOptions, Vu
use raw_window_handle::{DisplayHandle, HasDisplayHandle, HasWindowHandle};
use std::{mem::ManuallyDrop, path::PathBuf, sync::Arc};
use crate::connect::ResolutionData;
use crate::{connect::ResolutionData, error::VulkanError};
use self::{
types::{
FramebufferData, SurfaceData, SwapchainData, Vertex, VulkanData, VulkanError,
SHADER_INPUT_FORMAT,
},
types::{FramebufferData, SurfaceData, SwapchainData, Vertex, VulkanData, SHADER_INPUT_FORMAT},
utils::{
begin_commandbuffer, find_memorytype_index, record_submit_commandbuffer,
submit_commandbuffer,
@ -53,22 +50,22 @@ impl RendererBackendManager for VulkanBackendManager {
.to_vec();
#[cfg(all(debug_assertions, feature = "vulkan-debug"))]
extension_names.push(ash::extensions::ext::DebugUtils::name().as_ptr());
extension_names.push(ash::ext::debug_utils::NAME.as_ptr());
#[cfg(any(target_os = "macos", target_os = "ios"))]
{
#[cfg(not(feature = "vulkan-static"))]
extension_names.push(vk::KhrPortabilityEnumerationFn::name().as_ptr());
extension_names.push(vk::KhrGetPhysicalDeviceProperties2Fn::name().as_ptr());
extension_names.push(vk::KHR_PORTABILITY_ENUMERATION_NAME.as_ptr());
extension_names.push(vk::KHR_GET_PHYSICAL_DEVICE_PROPERTIES2_NAME.as_ptr());
}
let appinfo = vk::ApplicationInfo::builder()
let appinfo = vk::ApplicationInfo::default()
.application_name(&name)
.engine_name(&name)
.application_version(0)
.engine_version(0)
.api_version(vk::make_api_version(0, 1, 0, 0))
.build();
.api_version(vk::make_api_version(0, 1, 0, 0));
let create_flags = if cfg!(any(target_os = "macos", target_os = "ios")) {
vk::InstanceCreateFlags::ENUMERATE_PORTABILITY_KHR
@ -76,11 +73,10 @@ impl RendererBackendManager for VulkanBackendManager {
vk::InstanceCreateFlags::default()
};
let create_info = vk::InstanceCreateInfo::builder()
let create_info = vk::InstanceCreateInfo::default()
.application_info(&appinfo)
.enabled_extension_names(&extension_names)
.flags(create_flags)
.build();
.flags(create_flags);
let instance = unsafe { entry.create_instance(&create_info, None) }.unwrap();
@ -135,8 +131,8 @@ impl RendererBackend for VulkanBackend {
let filter_chain_options = FilterChainOptions {
frames_in_flight: 0,
force_no_mipmaps: false,
use_render_pass: true,
disable_cache: false,
use_dynamic_rendering: false,
};
let vulkan = VulkanObjects::try_from((
@ -213,8 +209,7 @@ impl VulkanWindowInner {
let vulkan_data = VulkanData::new(manager, &surface);
let (swapchain, _viewport_state_info) =
SwapchainData::new(resolutions, manager, &surface, &vulkan_data);
let swapchain = SwapchainData::new(resolutions, manager, &surface, &vulkan_data);
let renderpass_attachments = [vk::AttachmentDescription {
format: swapchain.format.format,
@ -237,16 +232,14 @@ impl VulkanWindowInner {
..Default::default()
}];
let subpass = vk::SubpassDescription::builder()
let subpass = vk::SubpassDescription::default()
.color_attachments(&color_attachment_refs)
.pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS)
.build();
.pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS);
let renderpass_create_info = vk::RenderPassCreateInfo::builder()
let renderpass_create_info = vk::RenderPassCreateInfo::default()
.attachments(&renderpass_attachments)
.subpasses(std::slice::from_ref(&subpass))
.dependencies(&dependencies)
.build();
.dependencies(&dependencies);
let renderpass = vulkan_data
.device
@ -339,8 +332,7 @@ impl VulkanWindowInner {
.device
.destroy_framebuffer(*framebuffer, None);
}
(self.swapchain, _) =
SwapchainData::new(resolutions, manager, &self.surface, &self.vulkan_data);
self.swapchain = SwapchainData::new(resolutions, manager, &self.surface, &self.vulkan_data);
self.framebuffers =
FramebufferData::new(&self.swapchain, &self.vulkan_data, self.renderpass);
@ -395,15 +387,13 @@ impl VulkanWindowInner {
&[],
&[texture_barrier],
);
let buffer_copy_regions = vk::BufferImageCopy::builder()
let buffer_copy_regions = vk::BufferImageCopy::default()
.image_subresource(
vk::ImageSubresourceLayers::builder()
vk::ImageSubresourceLayers::default()
.aspect_mask(vk::ImageAspectFlags::COLOR)
.layer_count(1)
.build(),
.layer_count(1),
)
.image_extent(self.swapchain.shader_input_image_extent.into())
.build();
.image_extent(self.swapchain.shader_input_image_extent.into());
device.cmd_copy_buffer_to_image(
texture_command_buffer,
@ -450,7 +440,7 @@ impl VulkanWindowInner {
.swapchain_loader
.acquire_next_image(
self.swapchain.swapchain,
std::u64::MAX,
u64::MAX,
self.vulkan_data.present_complete_semaphore,
vk::Fence::null(),
)
@ -488,6 +478,9 @@ impl VulkanWindowInner {
Some(&FrameOptions {
clear_history: true,
frame_direction: 0,
rotation: 0,
total_subframes: 0,
current_subframe: 0,
}),
)
.unwrap();

View file

@ -1,10 +1,5 @@
use ash::{
extensions::khr::{Surface, Swapchain},
vk, Device,
};
#[allow(deprecated)]
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
use thiserror::Error;
use ash::{khr::swapchain, vk, Device};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use crate::connect::ResolutionData;
@ -24,10 +19,8 @@ pub(super) struct VulkanData {
pub(super) pdevice: vk::PhysicalDevice,
pub(super) device: Device,
pub(super) device_memory_properties: vk::PhysicalDeviceMemoryProperties,
// pub(super) desc_set_layouts: [vk::DescriptorSetLayout; 1],
pub(super) present_queue: vk::Queue,
// pub(super) pool: vk::CommandPool,
pub(super) draw_command_buffer: vk::CommandBuffer,
pub(super) setup_command_buffer: vk::CommandBuffer,
pub(super) texture_copy_command_buffer: vk::CommandBuffer,
@ -38,7 +31,6 @@ pub(super) struct VulkanData {
pub(super) draw_commands_reuse_fence: vk::Fence,
pub(super) setup_commands_reuse_fence: vk::Fence,
pub(super) texture_copy_commands_reuse_fence: vk::Fence,
// pub(super) descriptor_pool: vk::DescriptorPool,
}
impl VulkanData {
@ -76,9 +68,9 @@ impl VulkanData {
.expect("Couldn't find suitable device.");
let queue_family_index = queue_family_index as u32;
let device_extension_names_raw = [
Swapchain::name().as_ptr(),
swapchain::NAME.as_ptr(),
#[cfg(any(target_os = "macos", target_os = "ios"))]
vk::KhrPortabilitySubsetFn::name().as_ptr(),
vk::KHR_PORTABILITY_SUBSET_NAME.as_ptr(),
];
let features = vk::PhysicalDeviceFeatures {
shader_clip_distance: 1,
@ -86,16 +78,14 @@ impl VulkanData {
};
let priorities = [1.0];
let queue_info = vk::DeviceQueueCreateInfo::builder()
let queue_info = vk::DeviceQueueCreateInfo::default()
.queue_family_index(queue_family_index)
.queue_priorities(&priorities)
.build();
.queue_priorities(&priorities);
let device_create_info = vk::DeviceCreateInfo::builder()
let device_create_info = vk::DeviceCreateInfo::default()
.queue_create_infos(std::slice::from_ref(&queue_info))
.enabled_extension_names(&device_extension_names_raw)
.enabled_features(&features)
.build();
.enabled_features(&features);
let device = manager
.instance
@ -104,18 +94,16 @@ impl VulkanData {
let present_queue = device.get_device_queue(queue_family_index, 0);
let pool_create_info = vk::CommandPoolCreateInfo::builder()
let pool_create_info = vk::CommandPoolCreateInfo::default()
.flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER)
.queue_family_index(queue_family_index)
.build();
.queue_family_index(queue_family_index);
let pool = device.create_command_pool(&pool_create_info, None).unwrap();
let command_buffer_allocate_info = vk::CommandBufferAllocateInfo::builder()
let command_buffer_allocate_info = vk::CommandBufferAllocateInfo::default()
.command_buffer_count(3)
.command_pool(pool)
.level(vk::CommandBufferLevel::PRIMARY)
.build();
.level(vk::CommandBufferLevel::PRIMARY);
let command_buffers = device
.allocate_command_buffers(&command_buffer_allocate_info)
@ -128,9 +116,8 @@ impl VulkanData {
.instance
.get_physical_device_memory_properties(pdevice);
let fence_create_info = vk::FenceCreateInfo::builder()
.flags(vk::FenceCreateFlags::SIGNALED)
.build();
let fence_create_info =
vk::FenceCreateInfo::default().flags(vk::FenceCreateFlags::SIGNALED);
let draw_commands_reuse_fence = device
.create_fence(&fence_create_info, None)
@ -151,38 +138,6 @@ impl VulkanData {
.create_semaphore(&semaphore_create_info, None)
.unwrap();
// let descriptor_sizes = [
// vk::DescriptorPoolSize {
// ty: vk::DescriptorType::UNIFORM_BUFFER,
// descriptor_count: 1,
// },
// vk::DescriptorPoolSize {
// ty: vk::DescriptorType::COMBINED_IMAGE_SAMPLER,
// descriptor_count: 1,
// },
// ];
// let descriptor_pool_info = vk::DescriptorPoolCreateInfo::builder()
// .pool_sizes(&descriptor_sizes)
// .max_sets(1)
// .build();
// let descriptor_pool = device
// .create_descriptor_pool(&descriptor_pool_info, None)
// .unwrap();
// let desc_layout_bindings = [vk::DescriptorSetLayoutBinding {
// descriptor_type: vk::DescriptorType::COMBINED_IMAGE_SAMPLER,
// descriptor_count: 1,
// stage_flags: vk::ShaderStageFlags::FRAGMENT,
// ..Default::default()
// }];
// let descriptor_info = vk::DescriptorSetLayoutCreateInfo::builder()
// .bindings(&desc_layout_bindings)
// .build();
// let desc_set_layouts = [device
// .create_descriptor_set_layout(&descriptor_info, None)
// .unwrap()];
Self {
pdevice,
device,
@ -197,33 +152,30 @@ impl VulkanData {
draw_commands_reuse_fence,
setup_commands_reuse_fence,
texture_copy_commands_reuse_fence,
// desc_set_layouts,
// descriptor_pool,
}
}
}
pub(super) struct SurfaceData {
pub(super) surface: vk::SurfaceKHR,
pub(super) surface_loader: Surface,
pub(super) surface_loader: ash::khr::surface::Instance,
}
impl SurfaceData {
#[allow(deprecated)]
pub(super) unsafe fn new<W: HasRawDisplayHandle + HasRawWindowHandle>(
pub(super) unsafe fn new<W: HasDisplayHandle + HasWindowHandle>(
window: &W,
manager: &VulkanBackendManager,
) -> Self {
let surface = ash_window::create_surface(
&manager.entry,
&manager.instance,
window.raw_display_handle().unwrap(),
window.raw_window_handle().unwrap(),
window.display_handle().unwrap().as_raw(),
window.window_handle().unwrap().as_raw(),
None,
)
.unwrap();
let surface_loader = Surface::new(&manager.entry, &manager.instance);
let surface_loader = ash::khr::surface::Instance::new(&manager.entry, &manager.instance);
Self {
surface,
@ -233,7 +185,7 @@ impl SurfaceData {
}
pub(super) struct SwapchainData {
pub(super) swapchain_loader: Swapchain,
pub(super) swapchain_loader: swapchain::Device,
pub(super) swapchain: vk::SwapchainKHR,
pub(super) format: vk::SurfaceFormatKHR,
pub(super) surface_resolution: vk::Extent2D,
@ -255,7 +207,7 @@ impl SwapchainData {
manager: &VulkanBackendManager,
surface: &SurfaceData,
vulkan: &VulkanData,
) -> (Self, vk::PipelineViewportStateCreateInfo) {
) -> Self {
let format = surface
.surface_loader
.get_physical_device_surface_formats(vulkan.pdevice, surface.surface)
@ -295,9 +247,9 @@ impl SwapchainData {
.cloned()
.find(|&mode| mode == vk::PresentModeKHR::MAILBOX)
.unwrap_or(vk::PresentModeKHR::FIFO);
let swapchain_loader = Swapchain::new(&manager.instance, &vulkan.device);
let swapchain_loader = swapchain::Device::new(&manager.instance, &vulkan.device);
let swapchain_create_info = vk::SwapchainCreateInfoKHR::builder()
let swapchain_create_info = vk::SwapchainCreateInfoKHR::default()
.surface(surface.surface)
.min_image_count(desired_image_count)
.image_color_space(format.color_space)
@ -309,8 +261,7 @@ impl SwapchainData {
.composite_alpha(vk::CompositeAlphaFlagsKHR::OPAQUE)
.present_mode(present_mode)
.clipped(true)
.image_array_layers(1)
.build();
.image_array_layers(1);
let swapchain = swapchain_loader
.create_swapchain(&swapchain_create_info, None)
@ -320,7 +271,7 @@ impl SwapchainData {
let present_image_views: Vec<vk::ImageView> = present_images
.iter()
.map(|&image| {
let create_view_info = vk::ImageViewCreateInfo::builder()
let create_view_info = vk::ImageViewCreateInfo::default()
.view_type(vk::ImageViewType::TYPE_2D)
.format(format.format)
.components(vk::ComponentMapping {
@ -336,8 +287,7 @@ impl SwapchainData {
base_array_layer: 0,
layer_count: 1,
})
.image(image)
.build();
.image(image);
vulkan
.device
.create_image_view(&create_view_info, None)
@ -345,20 +295,6 @@ impl SwapchainData {
})
.collect();
let viewports = [vk::Viewport {
x: 0.0,
y: 0.0,
width: surface_resolution.width as f32,
height: surface_resolution.height as f32,
min_depth: 0.0,
max_depth: 1.0,
}];
let scissors = [surface_resolution.into()];
let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder()
.scissors(&scissors)
.viewports(&viewports)
.build();
let shader_input_image_extent = vk::Extent2D {
width: resolutions.scaled_width,
height: resolutions.scaled_height,
@ -475,15 +411,13 @@ impl SwapchainData {
&[],
&[texture_barrier],
);
let buffer_copy_regions = vk::BufferImageCopy::builder()
let buffer_copy_regions = vk::BufferImageCopy::default()
.image_subresource(
vk::ImageSubresourceLayers::builder()
vk::ImageSubresourceLayers::default()
.aspect_mask(vk::ImageAspectFlags::COLOR)
.layer_count(1)
.build(),
.layer_count(1),
)
.image_extent(shader_input_image_extent.into())
.build();
.image_extent(shader_input_image_extent.into());
device.cmd_copy_buffer_to_image(
texture_command_buffer,
@ -557,25 +491,22 @@ impl SwapchainData {
.create_image_view(&shader_input_tex_image_view_info, None)
.unwrap();
(
Self {
swapchain_loader,
swapchain,
format,
surface_resolution,
present_image_views,
sampler,
shader_input_texture_memory,
shader_input_tex_image_view,
shader_input_image_buffer,
shader_input_image_buffer_memory,
shader_input_image_buffer_memory_req,
shader_input_image_extent,
shader_input_texture,
present_images,
},
viewport_state_info,
)
Self {
swapchain_loader,
swapchain,
format,
surface_resolution,
present_image_views,
sampler,
shader_input_texture_memory,
shader_input_tex_image_view,
shader_input_image_buffer,
shader_input_image_buffer_memory,
shader_input_image_buffer_memory_req,
shader_input_image_extent,
shader_input_texture,
present_images,
}
}
pub(super) unsafe fn manual_drop(&mut self, vulkan: &VulkanData) {
@ -619,13 +550,12 @@ impl FramebufferData {
.iter()
.map(|&present_image_view| {
let framebuffer_attachments = [present_image_view];
let frame_buffer_create_info = vk::FramebufferCreateInfo::builder()
let frame_buffer_create_info = vk::FramebufferCreateInfo::default()
.render_pass(renderpass)
.attachments(&framebuffer_attachments)
.width(swapchain.surface_resolution.width)
.height(swapchain.surface_resolution.height)
.layers(1)
.build();
.layers(1);
vulkan
.device

View file

@ -30,7 +30,7 @@ pub(super) unsafe fn begin_commandbuffer(
command_buffer_reuse_fence: vk::Fence,
) {
device
.wait_for_fences(&[command_buffer_reuse_fence], true, std::u64::MAX)
.wait_for_fences(&[command_buffer_reuse_fence], true, u64::MAX)
.expect("Wait for fence failed.");
device
@ -44,9 +44,8 @@ pub(super) unsafe fn begin_commandbuffer(
)
.expect("Reset command buffer failed.");
let command_buffer_begin_info = vk::CommandBufferBeginInfo::builder()
.flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT)
.build();
let command_buffer_begin_info =
vk::CommandBufferBeginInfo::default().flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT);
device
.begin_command_buffer(command_buffer, &command_buffer_begin_info)
@ -68,12 +67,11 @@ pub(super) unsafe fn submit_commandbuffer(
let command_buffers = vec![command_buffer];
let submit_info = vk::SubmitInfo::builder()
let submit_info = vk::SubmitInfo::default()
.wait_semaphores(wait_semaphores)
.wait_dst_stage_mask(wait_mask)
.command_buffers(&command_buffers)
.signal_semaphores(signal_semaphores)
.build();
.signal_semaphores(signal_semaphores);
device
.queue_submit(submit_queue, &[submit_info], command_buffer_reuse_fence)