From fdac5a97a1a8e81b9fedab0043072be05c257392 Mon Sep 17 00:00:00 2001 From: zomseffen Date: Sat, 27 Apr 2024 16:40:24 +0200 Subject: [PATCH] msaa --- src/app_data.rs | 5 +++ src/depth_buffer.rs | 1 + src/image.rs | 4 +- src/main.rs | 94 +++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 95 insertions(+), 9 deletions(-) diff --git a/src/app_data.rs b/src/app_data.rs index 6e82935..1497033 100644 --- a/src/app_data.rs +++ b/src/app_data.rs @@ -57,4 +57,9 @@ pub struct AppData { pub depth_image: vk::Image, pub depth_image_memory: vk::DeviceMemory, pub depth_image_view: vk::ImageView, + + pub msaa_samples: vk::SampleCountFlags, + pub color_image: vk::Image, + pub color_image_memory: vk::DeviceMemory, + pub color_image_view: vk::ImageView, } \ No newline at end of file diff --git a/src/depth_buffer.rs b/src/depth_buffer.rs index cdc1eff..a252cbb 100644 --- a/src/depth_buffer.rs +++ b/src/depth_buffer.rs @@ -17,6 +17,7 @@ pub unsafe fn create_depth_objects( data.swapchain_extent.width, data.swapchain_extent.height, 1, + data.msaa_samples, format, vk::ImageTiling::OPTIMAL, vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT, diff --git a/src/image.rs b/src/image.rs index c811228..5bb5b27 100644 --- a/src/image.rs +++ b/src/image.rs @@ -62,6 +62,7 @@ pub unsafe fn create_texture_image( width, height, data.mip_levels, + vk::SampleCountFlags::_1, vk::Format::R8G8B8A8_SRGB, vk::ImageTiling::OPTIMAL, vk::ImageUsageFlags::SAMPLED @@ -116,6 +117,7 @@ pub unsafe fn create_image( width: u32, height: u32, mip_levels: u32, + samples: vk::SampleCountFlags, format: vk::Format, tiling: vk::ImageTiling, usage: vk::ImageUsageFlags, @@ -134,7 +136,7 @@ pub unsafe fn create_image( .tiling(tiling) .initial_layout(vk::ImageLayout::UNDEFINED) .usage(usage) - .samples(vk::SampleCountFlags::_1) + .samples(samples) .sharing_mode(vk::SharingMode::EXCLUSIVE); let image = device.create_image(&info, None)?; diff --git a/src/main.rs b/src/main.rs index 0e62f84..3bb81d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -131,6 +131,7 @@ impl App { command_buffer::create_command_pool(&instance, &device, &mut data)?; + create_color_objects(&instance, &device, &mut data)?; depth_buffer::create_depth_objects(&instance, &device, &mut data)?; create_framebuffers(&device, &mut data)?; @@ -264,6 +265,7 @@ impl App { create_render_pass(&self.instance, &self.device, &mut self.data)?; create_pipeline(&self.device, &mut self.data)?; buffer::create_descriptor_pool(&self.device, &mut self.data)?; + create_color_objects(&self.instance, &self.device, &mut self.data)?; depth_buffer::create_depth_objects(&self.instance, &self.device, &mut self.data)?; create_framebuffers(&self.device, &mut self.data)?; buffer::create_uniform_buffers(&self.instance, &self.device, &mut self.data)?; @@ -276,6 +278,10 @@ impl App { } unsafe fn destroy_swapchain(&mut self) { + self.device.destroy_image_view(self.data.color_image_view, None); + self.device.free_memory(self.data.color_image_memory, None); + self.device.destroy_image(self.data.color_image, None); + self.device.destroy_image_view(self.data.depth_image_view, None); self.device.destroy_image(self.data.depth_image, None); @@ -457,6 +463,7 @@ unsafe fn pick_physical_device(instance: &Instance, data: &mut app_data::AppData } else { info!("Selected physical device (`{}`).", properties.device_name); data.physical_device = physical_device; + data.msaa_samples = get_max_msaa_samples(instance, data); return Ok(()); } } @@ -627,7 +634,7 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu let multisample_state = vk::PipelineMultisampleStateCreateInfo::builder() .sample_shading_enable(false) - .rasterization_samples(vk::SampleCountFlags::_1); + .rasterization_samples(data.msaa_samples); let depth_stencil_state = vk::PipelineDepthStencilStateCreateInfo::builder() .depth_test_enable(true) @@ -703,21 +710,35 @@ unsafe fn create_render_pass( ) -> Result<()> { let color_attachment = vk::AttachmentDescription::builder() .format(data.swapchain_format) - .samples(vk::SampleCountFlags::_1) + .samples(data.msaa_samples) .load_op(vk::AttachmentLoadOp::CLEAR) .store_op(vk::AttachmentStoreOp::STORE) .stencil_load_op(vk::AttachmentLoadOp::DONT_CARE) .stencil_store_op(vk::AttachmentStoreOp::DONT_CARE) .initial_layout(vk::ImageLayout::UNDEFINED) + .final_layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL); + + let color_attachment_ref = vk::AttachmentReference::builder() + .attachment(0) + .layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL); + + let color_resolve_attachment = vk::AttachmentDescription::builder() + .format(data.swapchain_format) + .samples(vk::SampleCountFlags::_1) + .load_op(vk::AttachmentLoadOp::DONT_CARE) + .store_op(vk::AttachmentStoreOp::STORE) + .stencil_load_op(vk::AttachmentLoadOp::DONT_CARE) + .stencil_store_op(vk::AttachmentStoreOp::DONT_CARE) + .initial_layout(vk::ImageLayout::UNDEFINED) .final_layout(vk::ImageLayout::PRESENT_SRC_KHR); - let color_attachment_ref = vk::AttachmentReference::builder() - .attachment(0) + let color_resolve_attachment_ref = vk::AttachmentReference::builder() + .attachment(2) .layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL); let depth_stencil_attachment = vk::AttachmentDescription::builder() .format(depth_buffer::get_depth_format(instance, data)?) - .samples(vk::SampleCountFlags::_1) + .samples(data.msaa_samples) .load_op(vk::AttachmentLoadOp::CLEAR) .store_op(vk::AttachmentStoreOp::DONT_CARE) .stencil_load_op(vk::AttachmentLoadOp::DONT_CARE) @@ -730,10 +751,12 @@ unsafe fn create_render_pass( .layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL); let color_attachments = &[color_attachment_ref]; + let resolve_attachments = &[color_resolve_attachment_ref]; let subpass = vk::SubpassDescription::builder() .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS) .color_attachments(color_attachments) - .depth_stencil_attachment(&depth_stencil_attachment_ref); + .depth_stencil_attachment(&depth_stencil_attachment_ref) + .resolve_attachments(resolve_attachments); let dependency = vk::SubpassDependency::builder() .src_subpass(vk::SUBPASS_EXTERNAL) @@ -746,7 +769,7 @@ unsafe fn create_render_pass( .dst_access_mask(vk::AccessFlags::COLOR_ATTACHMENT_WRITE | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_WRITE); - let attachments = &[color_attachment, depth_stencil_attachment]; + let attachments = &[color_attachment, depth_stencil_attachment, color_resolve_attachment]; let subpasses = &[subpass]; let dependencies = &[dependency]; let info = vk::RenderPassCreateInfo::builder() @@ -764,7 +787,7 @@ unsafe fn create_framebuffers(device: &Device, data: &mut app_data::AppData) -> .swapchain_image_views .iter() .map(|i| { - let attachments = &[*i, data.depth_image_view]; + let attachments = &[data.color_image_view, data.depth_image_view, *i]; let create_info = vk::FramebufferCreateInfo::builder() .render_pass(data.render_pass) .attachments(attachments) @@ -798,5 +821,60 @@ unsafe fn create_sync_objects(device: &Device, data: &mut app_data::AppData) -> .map(|_| vk::Fence::null()) .collect(); + Ok(()) +} + +unsafe fn get_max_msaa_samples( + instance: &Instance, + data: &app_data::AppData, +) -> vk::SampleCountFlags { + let properties = instance.get_physical_device_properties(data.physical_device); + let counts = properties.limits.framebuffer_color_sample_counts + & properties.limits.framebuffer_depth_sample_counts; + [ + vk::SampleCountFlags::_64, + vk::SampleCountFlags::_32, + vk::SampleCountFlags::_16, + vk::SampleCountFlags::_8, + vk::SampleCountFlags::_4, + vk::SampleCountFlags::_2, + ] + .iter() + .cloned() + .find(|c| counts.contains(*c)) + .unwrap_or(vk::SampleCountFlags::_1) +} + +unsafe fn create_color_objects( + instance: &Instance, + device: &Device, + data: &mut app_data::AppData, +) -> Result<()> { + let (color_image, color_image_memory) = image::create_image( + instance, + device, + data, + data.swapchain_extent.width, + data.swapchain_extent.height, + 1, + data.msaa_samples, + data.swapchain_format, + vk::ImageTiling::OPTIMAL, + vk::ImageUsageFlags::COLOR_ATTACHMENT + | vk::ImageUsageFlags::TRANSIENT_ATTACHMENT, + vk::MemoryPropertyFlags::DEVICE_LOCAL, + )?; + + data.color_image = color_image; + data.color_image_memory = color_image_memory; + + data.color_image_view = image::create_image_view( + device, + data.color_image, + data.swapchain_format, + vk::ImageAspectFlags::COLOR, + 1, + )?; + Ok(()) } \ No newline at end of file