remove nested unsafe from old refactor

This commit is contained in:
Alex Janka 2023-10-02 11:08:28 +11:00
parent 43ae43e8b8
commit 772026c04f

View file

@ -101,37 +101,36 @@ impl WindowData {
.enabled_extension_names(&extension_names) .enabled_extension_names(&extension_names)
.flags(create_flags); .flags(create_flags);
let instance = unsafe { entry.create_instance(&create_info, None) }.unwrap(); let instance = entry.create_instance(&create_info, None).unwrap();
let surface = unsafe { let surface = ash_window::create_surface(
ash_window::create_surface(
&entry, &entry,
&instance, &instance,
window.raw_display_handle(), window.raw_display_handle(),
window.raw_window_handle(), window.raw_window_handle(),
None, None,
) )
}
.unwrap(); .unwrap();
let pdevices = let pdevices = instance
unsafe { instance.enumerate_physical_devices() }.expect("Physical device error"); .enumerate_physical_devices()
.expect("Physical device error");
let surface_loader = Surface::new(&entry, &instance); let surface_loader = Surface::new(&entry, &instance);
let (pdevice, queue_family_index) = pdevices let (pdevice, queue_family_index) = pdevices
.iter() .iter()
.find_map(|pdevice| { .find_map(|pdevice| {
unsafe { instance.get_physical_device_queue_family_properties(*pdevice) } instance
.get_physical_device_queue_family_properties(*pdevice)
.iter() .iter()
.enumerate() .enumerate()
.find_map(|(index, info)| { .find_map(|(index, info)| {
let supports_graphic_and_surface = let supports_graphic_and_surface =
info.queue_flags.contains(vk::QueueFlags::GRAPHICS) info.queue_flags.contains(vk::QueueFlags::GRAPHICS)
&& unsafe { && surface_loader
surface_loader.get_physical_device_surface_support( .get_physical_device_surface_support(
*pdevice, *pdevice,
index as u32, index as u32,
surface, surface,
) )
}
.unwrap(); .unwrap();
if supports_graphic_and_surface { if supports_graphic_and_surface {
Some((*pdevice, index)) Some((*pdevice, index))
@ -162,16 +161,18 @@ impl WindowData {
.enabled_extension_names(&device_extension_names_raw) .enabled_extension_names(&device_extension_names_raw)
.enabled_features(&features); .enabled_features(&features);
let device = unsafe { instance.create_device(pdevice, &device_create_info, None) }.unwrap(); let device = instance
.create_device(pdevice, &device_create_info, None)
.unwrap();
let present_queue = unsafe { device.get_device_queue(queue_family_index, 0) }; let present_queue = device.get_device_queue(queue_family_index, 0);
let surface_format = let surface_format = surface_loader
unsafe { surface_loader.get_physical_device_surface_formats(pdevice, surface) } .get_physical_device_surface_formats(pdevice, surface)
.unwrap()[0]; .unwrap()[0];
let surface_capabilities = let surface_capabilities = surface_loader
unsafe { surface_loader.get_physical_device_surface_capabilities(pdevice, surface) } .get_physical_device_surface_capabilities(pdevice, surface)
.unwrap(); .unwrap();
let mut desired_image_count = surface_capabilities.min_image_count + 1; let mut desired_image_count = surface_capabilities.min_image_count + 1;
if surface_capabilities.max_image_count > 0 if surface_capabilities.max_image_count > 0
@ -194,8 +195,8 @@ impl WindowData {
} else { } else {
surface_capabilities.current_transform surface_capabilities.current_transform
}; };
let present_modes = let present_modes = surface_loader
unsafe { surface_loader.get_physical_device_surface_present_modes(pdevice, surface) } .get_physical_device_surface_present_modes(pdevice, surface)
.unwrap(); .unwrap();
let present_mode = present_modes let present_mode = present_modes
.iter() .iter()
@ -218,26 +219,28 @@ impl WindowData {
.clipped(true) .clipped(true)
.image_array_layers(1); .image_array_layers(1);
let swapchain = let swapchain = swapchain_loader
unsafe { swapchain_loader.create_swapchain(&swapchain_create_info, None) }.unwrap(); .create_swapchain(&swapchain_create_info, None)
.unwrap();
let pool_create_info = vk::CommandPoolCreateInfo::default() let pool_create_info = vk::CommandPoolCreateInfo::default()
.flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER) .flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER)
.queue_family_index(queue_family_index); .queue_family_index(queue_family_index);
let pool = unsafe { device.create_command_pool(&pool_create_info, None).unwrap() }; let pool = device.create_command_pool(&pool_create_info, None).unwrap();
let command_buffer_allocate_info = vk::CommandBufferAllocateInfo::default() let command_buffer_allocate_info = vk::CommandBufferAllocateInfo::default()
.command_buffer_count(2) .command_buffer_count(2)
.command_pool(pool) .command_pool(pool)
.level(vk::CommandBufferLevel::PRIMARY); .level(vk::CommandBufferLevel::PRIMARY);
let command_buffers = let command_buffers = device
unsafe { device.allocate_command_buffers(&command_buffer_allocate_info) }.unwrap(); .allocate_command_buffers(&command_buffer_allocate_info)
.unwrap();
let setup_command_buffer = command_buffers[0]; let setup_command_buffer = command_buffers[0];
let draw_command_buffer = command_buffers[1]; let draw_command_buffer = command_buffers[1];
let present_images = unsafe { swapchain_loader.get_swapchain_images(swapchain) }.unwrap(); let present_images = swapchain_loader.get_swapchain_images(swapchain).unwrap();
let present_image_views: Vec<vk::ImageView> = present_images let present_image_views: Vec<vk::ImageView> = present_images
.iter() .iter()
.map(|&image| { .map(|&image| {
@ -258,11 +261,10 @@ impl WindowData {
layer_count: 1, layer_count: 1,
}) })
.image(image); .image(image);
unsafe { device.create_image_view(&create_view_info, None) }.unwrap() device.create_image_view(&create_view_info, None).unwrap()
}) })
.collect(); .collect();
let device_memory_properties = let device_memory_properties = instance.get_physical_device_memory_properties(pdevice);
unsafe { instance.get_physical_device_memory_properties(pdevice) };
let depth_image_create_info = vk::ImageCreateInfo::default() let depth_image_create_info = vk::ImageCreateInfo::default()
.image_type(vk::ImageType::TYPE_2D) .image_type(vk::ImageType::TYPE_2D)
.format(vk::Format::D16_UNORM) .format(vk::Format::D16_UNORM)
@ -274,8 +276,8 @@ impl WindowData {
.usage(vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT) .usage(vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT)
.sharing_mode(vk::SharingMode::EXCLUSIVE); .sharing_mode(vk::SharingMode::EXCLUSIVE);
let depth_image = unsafe { device.create_image(&depth_image_create_info, None) }.unwrap(); let depth_image = device.create_image(&depth_image_create_info, None).unwrap();
let depth_image_memory_req = unsafe { device.get_image_memory_requirements(depth_image) }; let depth_image_memory_req = device.get_image_memory_requirements(depth_image);
let depth_image_memory_index = device_memory_properties.memory_types let depth_image_memory_index = device_memory_properties.memory_types
[..device_memory_properties.memory_type_count as _] [..device_memory_properties.memory_type_count as _]
.iter() .iter()
@ -292,19 +294,23 @@ impl WindowData {
.allocation_size(depth_image_memory_req.size) .allocation_size(depth_image_memory_req.size)
.memory_type_index(depth_image_memory_index); .memory_type_index(depth_image_memory_index);
let depth_image_memory = let depth_image_memory = device
unsafe { device.allocate_memory(&depth_image_allocate_info, None) }.unwrap(); .allocate_memory(&depth_image_allocate_info, None)
.unwrap();
unsafe { device.bind_image_memory(depth_image, depth_image_memory, 0) } device
.bind_image_memory(depth_image, depth_image_memory, 0)
.expect("Unable to bind depth image memory"); .expect("Unable to bind depth image memory");
let fence_create_info = let fence_create_info =
vk::FenceCreateInfo::default().flags(vk::FenceCreateFlags::SIGNALED); vk::FenceCreateInfo::default().flags(vk::FenceCreateFlags::SIGNALED);
let draw_commands_reuse_fence = let draw_commands_reuse_fence = device
unsafe { device.create_fence(&fence_create_info, None) }.expect("Create fence failed."); .create_fence(&fence_create_info, None)
let setup_commands_reuse_fence = .expect("Create fence failed.");
unsafe { device.create_fence(&fence_create_info, None) }.expect("Create fence failed."); let setup_commands_reuse_fence = device
.create_fence(&fence_create_info, None)
.expect("Create fence failed.");
record_submit_commandbuffer( record_submit_commandbuffer(
&device, &device,
@ -330,7 +336,6 @@ impl WindowData {
.level_count(1), .level_count(1),
); );
unsafe {
device.cmd_pipeline_barrier( device.cmd_pipeline_barrier(
setup_command_buffer, setup_command_buffer,
vk::PipelineStageFlags::BOTTOM_OF_PIPE, vk::PipelineStageFlags::BOTTOM_OF_PIPE,
@ -340,7 +345,6 @@ impl WindowData {
&[], &[],
&[layout_transition_barriers], &[layout_transition_barriers],
); );
}
}, },
); );
@ -355,15 +359,18 @@ impl WindowData {
.format(depth_image_create_info.format) .format(depth_image_create_info.format)
.view_type(vk::ImageViewType::TYPE_2D); .view_type(vk::ImageViewType::TYPE_2D);
let depth_image_view = let depth_image_view = device
unsafe { device.create_image_view(&depth_image_view_info, None) }.unwrap(); .create_image_view(&depth_image_view_info, None)
.unwrap();
let semaphore_create_info = vk::SemaphoreCreateInfo::default(); let semaphore_create_info = vk::SemaphoreCreateInfo::default();
let present_complete_semaphore = let present_complete_semaphore = device
unsafe { device.create_semaphore(&semaphore_create_info, None) }.unwrap(); .create_semaphore(&semaphore_create_info, None)
let rendering_complete_semaphore = .unwrap();
unsafe { device.create_semaphore(&semaphore_create_info, None) }.unwrap(); let rendering_complete_semaphore = device
.create_semaphore(&semaphore_create_info, None)
.unwrap();
let renderpass_attachments = [ let renderpass_attachments = [
vk::AttachmentDescription { vk::AttachmentDescription {
@ -410,8 +417,9 @@ impl WindowData {
.subpasses(std::slice::from_ref(&subpass)) .subpasses(std::slice::from_ref(&subpass))
.dependencies(&dependencies); .dependencies(&dependencies);
let renderpass = let renderpass = device
unsafe { device.create_render_pass(&renderpass_create_info, None) }.unwrap(); .create_render_pass(&renderpass_create_info, None)
.unwrap();
let framebuffers: Vec<vk::Framebuffer> = present_image_views let framebuffers: Vec<vk::Framebuffer> = present_image_views
.iter() .iter()
@ -424,7 +432,9 @@ impl WindowData {
.height(surface_resolution.height) .height(surface_resolution.height)
.layers(1); .layers(1);
unsafe { device.create_framebuffer(&frame_buffer_create_info, None) }.unwrap() device
.create_framebuffer(&frame_buffer_create_info, None)
.unwrap()
}) })
.collect(); .collect();
let index_buffer_data = [0u32, 1, 2, 2, 3, 0]; let index_buffer_data = [0u32, 1, 2, 2, 3, 0];
@ -757,14 +767,16 @@ impl WindowData {
let shader_info = vk::ShaderModuleCreateInfo::default().code(&shader); let shader_info = vk::ShaderModuleCreateInfo::default().code(&shader);
println!("shader info: {:#?}", shader_info); println!("shader info: {:#?}", shader_info);
let shader_module = unsafe { device.create_shader_module(&shader_info, None) } let shader_module = device
.create_shader_module(&shader_info, None)
.expect("Error creating shader module"); .expect("Error creating shader module");
let layout_create_info = let layout_create_info =
vk::PipelineLayoutCreateInfo::default().set_layouts(&desc_set_layouts); vk::PipelineLayoutCreateInfo::default().set_layouts(&desc_set_layouts);
let pipeline_layout = let pipeline_layout = device
unsafe { device.create_pipeline_layout(&layout_create_info, None) }.unwrap(); .create_pipeline_layout(&layout_create_info, None)
.unwrap();
let vertex_shader_entry_name = std::ffi::CStr::from_bytes_with_nul_unchecked(b"vs_main\0"); let vertex_shader_entry_name = std::ffi::CStr::from_bytes_with_nul_unchecked(b"vs_main\0");
let fragment_shader_entry_name = let fragment_shader_entry_name =