remove nested unsafe from old refactor
This commit is contained in:
parent
43ae43e8b8
commit
772026c04f
1 changed files with 81 additions and 69 deletions
|
@ -101,38 +101,37 @@ impl WindowData {
|
|||
.enabled_extension_names(&extension_names)
|
||||
.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 {
|
||||
ash_window::create_surface(
|
||||
&entry,
|
||||
&instance,
|
||||
window.raw_display_handle(),
|
||||
window.raw_window_handle(),
|
||||
None,
|
||||
)
|
||||
}
|
||||
let surface = ash_window::create_surface(
|
||||
&entry,
|
||||
&instance,
|
||||
window.raw_display_handle(),
|
||||
window.raw_window_handle(),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
let pdevices =
|
||||
unsafe { instance.enumerate_physical_devices() }.expect("Physical device error");
|
||||
let pdevices = instance
|
||||
.enumerate_physical_devices()
|
||||
.expect("Physical device error");
|
||||
let surface_loader = Surface::new(&entry, &instance);
|
||||
let (pdevice, queue_family_index) = pdevices
|
||||
.iter()
|
||||
.find_map(|pdevice| {
|
||||
unsafe { instance.get_physical_device_queue_family_properties(*pdevice) }
|
||||
instance
|
||||
.get_physical_device_queue_family_properties(*pdevice)
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find_map(|(index, info)| {
|
||||
let supports_graphic_and_surface =
|
||||
info.queue_flags.contains(vk::QueueFlags::GRAPHICS)
|
||||
&& unsafe {
|
||||
surface_loader.get_physical_device_surface_support(
|
||||
&& surface_loader
|
||||
.get_physical_device_surface_support(
|
||||
*pdevice,
|
||||
index as u32,
|
||||
surface,
|
||||
)
|
||||
}
|
||||
.unwrap();
|
||||
.unwrap();
|
||||
if supports_graphic_and_surface {
|
||||
Some((*pdevice, index))
|
||||
} else {
|
||||
|
@ -162,17 +161,19 @@ impl WindowData {
|
|||
.enabled_extension_names(&device_extension_names_raw)
|
||||
.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 =
|
||||
unsafe { surface_loader.get_physical_device_surface_formats(pdevice, surface) }
|
||||
.unwrap()[0];
|
||||
let surface_format = surface_loader
|
||||
.get_physical_device_surface_formats(pdevice, surface)
|
||||
.unwrap()[0];
|
||||
|
||||
let surface_capabilities =
|
||||
unsafe { surface_loader.get_physical_device_surface_capabilities(pdevice, surface) }
|
||||
.unwrap();
|
||||
let surface_capabilities = surface_loader
|
||||
.get_physical_device_surface_capabilities(pdevice, surface)
|
||||
.unwrap();
|
||||
let mut desired_image_count = surface_capabilities.min_image_count + 1;
|
||||
if surface_capabilities.max_image_count > 0
|
||||
&& desired_image_count > surface_capabilities.max_image_count
|
||||
|
@ -194,9 +195,9 @@ impl WindowData {
|
|||
} else {
|
||||
surface_capabilities.current_transform
|
||||
};
|
||||
let present_modes =
|
||||
unsafe { surface_loader.get_physical_device_surface_present_modes(pdevice, surface) }
|
||||
.unwrap();
|
||||
let present_modes = surface_loader
|
||||
.get_physical_device_surface_present_modes(pdevice, surface)
|
||||
.unwrap();
|
||||
let present_mode = present_modes
|
||||
.iter()
|
||||
.cloned()
|
||||
|
@ -218,26 +219,28 @@ impl WindowData {
|
|||
.clipped(true)
|
||||
.image_array_layers(1);
|
||||
|
||||
let swapchain =
|
||||
unsafe { swapchain_loader.create_swapchain(&swapchain_create_info, None) }.unwrap();
|
||||
let swapchain = swapchain_loader
|
||||
.create_swapchain(&swapchain_create_info, None)
|
||||
.unwrap();
|
||||
|
||||
let pool_create_info = vk::CommandPoolCreateInfo::default()
|
||||
.flags(vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER)
|
||||
.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()
|
||||
.command_buffer_count(2)
|
||||
.command_pool(pool)
|
||||
.level(vk::CommandBufferLevel::PRIMARY);
|
||||
|
||||
let command_buffers =
|
||||
unsafe { device.allocate_command_buffers(&command_buffer_allocate_info) }.unwrap();
|
||||
let command_buffers = device
|
||||
.allocate_command_buffers(&command_buffer_allocate_info)
|
||||
.unwrap();
|
||||
let setup_command_buffer = command_buffers[0];
|
||||
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
|
||||
.iter()
|
||||
.map(|&image| {
|
||||
|
@ -258,11 +261,10 @@ impl WindowData {
|
|||
layer_count: 1,
|
||||
})
|
||||
.image(image);
|
||||
unsafe { device.create_image_view(&create_view_info, None) }.unwrap()
|
||||
device.create_image_view(&create_view_info, None).unwrap()
|
||||
})
|
||||
.collect();
|
||||
let device_memory_properties =
|
||||
unsafe { instance.get_physical_device_memory_properties(pdevice) };
|
||||
let device_memory_properties = instance.get_physical_device_memory_properties(pdevice);
|
||||
let depth_image_create_info = vk::ImageCreateInfo::default()
|
||||
.image_type(vk::ImageType::TYPE_2D)
|
||||
.format(vk::Format::D16_UNORM)
|
||||
|
@ -274,8 +276,8 @@ impl WindowData {
|
|||
.usage(vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT)
|
||||
.sharing_mode(vk::SharingMode::EXCLUSIVE);
|
||||
|
||||
let depth_image = unsafe { 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 = device.create_image(&depth_image_create_info, None).unwrap();
|
||||
let depth_image_memory_req = device.get_image_memory_requirements(depth_image);
|
||||
let depth_image_memory_index = device_memory_properties.memory_types
|
||||
[..device_memory_properties.memory_type_count as _]
|
||||
.iter()
|
||||
|
@ -292,19 +294,23 @@ impl WindowData {
|
|||
.allocation_size(depth_image_memory_req.size)
|
||||
.memory_type_index(depth_image_memory_index);
|
||||
|
||||
let depth_image_memory =
|
||||
unsafe { device.allocate_memory(&depth_image_allocate_info, None) }.unwrap();
|
||||
let depth_image_memory = device
|
||||
.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");
|
||||
|
||||
let fence_create_info =
|
||||
vk::FenceCreateInfo::default().flags(vk::FenceCreateFlags::SIGNALED);
|
||||
|
||||
let draw_commands_reuse_fence =
|
||||
unsafe { device.create_fence(&fence_create_info, None) }.expect("Create fence failed.");
|
||||
let setup_commands_reuse_fence =
|
||||
unsafe { device.create_fence(&fence_create_info, None) }.expect("Create fence failed.");
|
||||
let draw_commands_reuse_fence = 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(
|
||||
&device,
|
||||
|
@ -330,17 +336,15 @@ impl WindowData {
|
|||
.level_count(1),
|
||||
);
|
||||
|
||||
unsafe {
|
||||
device.cmd_pipeline_barrier(
|
||||
setup_command_buffer,
|
||||
vk::PipelineStageFlags::BOTTOM_OF_PIPE,
|
||||
vk::PipelineStageFlags::LATE_FRAGMENT_TESTS,
|
||||
vk::DependencyFlags::empty(),
|
||||
&[],
|
||||
&[],
|
||||
&[layout_transition_barriers],
|
||||
);
|
||||
}
|
||||
device.cmd_pipeline_barrier(
|
||||
setup_command_buffer,
|
||||
vk::PipelineStageFlags::BOTTOM_OF_PIPE,
|
||||
vk::PipelineStageFlags::LATE_FRAGMENT_TESTS,
|
||||
vk::DependencyFlags::empty(),
|
||||
&[],
|
||||
&[],
|
||||
&[layout_transition_barriers],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
|
@ -355,15 +359,18 @@ impl WindowData {
|
|||
.format(depth_image_create_info.format)
|
||||
.view_type(vk::ImageViewType::TYPE_2D);
|
||||
|
||||
let depth_image_view =
|
||||
unsafe { device.create_image_view(&depth_image_view_info, None) }.unwrap();
|
||||
let depth_image_view = device
|
||||
.create_image_view(&depth_image_view_info, None)
|
||||
.unwrap();
|
||||
|
||||
let semaphore_create_info = vk::SemaphoreCreateInfo::default();
|
||||
|
||||
let present_complete_semaphore =
|
||||
unsafe { device.create_semaphore(&semaphore_create_info, None) }.unwrap();
|
||||
let rendering_complete_semaphore =
|
||||
unsafe { device.create_semaphore(&semaphore_create_info, None) }.unwrap();
|
||||
let present_complete_semaphore = device
|
||||
.create_semaphore(&semaphore_create_info, None)
|
||||
.unwrap();
|
||||
let rendering_complete_semaphore = device
|
||||
.create_semaphore(&semaphore_create_info, None)
|
||||
.unwrap();
|
||||
|
||||
let renderpass_attachments = [
|
||||
vk::AttachmentDescription {
|
||||
|
@ -410,8 +417,9 @@ impl WindowData {
|
|||
.subpasses(std::slice::from_ref(&subpass))
|
||||
.dependencies(&dependencies);
|
||||
|
||||
let renderpass =
|
||||
unsafe { device.create_render_pass(&renderpass_create_info, None) }.unwrap();
|
||||
let renderpass = device
|
||||
.create_render_pass(&renderpass_create_info, None)
|
||||
.unwrap();
|
||||
|
||||
let framebuffers: Vec<vk::Framebuffer> = present_image_views
|
||||
.iter()
|
||||
|
@ -424,7 +432,9 @@ impl WindowData {
|
|||
.height(surface_resolution.height)
|
||||
.layers(1);
|
||||
|
||||
unsafe { device.create_framebuffer(&frame_buffer_create_info, None) }.unwrap()
|
||||
device
|
||||
.create_framebuffer(&frame_buffer_create_info, None)
|
||||
.unwrap()
|
||||
})
|
||||
.collect();
|
||||
let index_buffer_data = [0u32, 1, 2, 2, 3, 0];
|
||||
|
@ -757,14 +767,16 @@ impl WindowData {
|
|||
let shader_info = vk::ShaderModuleCreateInfo::default().code(&shader);
|
||||
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");
|
||||
|
||||
let layout_create_info =
|
||||
vk::PipelineLayoutCreateInfo::default().set_layouts(&desc_set_layouts);
|
||||
|
||||
let pipeline_layout =
|
||||
unsafe { device.create_pipeline_layout(&layout_create_info, None) }.unwrap();
|
||||
let pipeline_layout = device
|
||||
.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 fragment_shader_entry_name =
|
||||
|
|
Loading…
Add table
Reference in a new issue