This commit is contained in:
zomseffen 2024-04-27 16:40:24 +02:00
parent b5d3864d01
commit fdac5a97a1
4 changed files with 95 additions and 9 deletions

View file

@ -57,4 +57,9 @@ pub struct AppData {
pub depth_image: vk::Image, pub depth_image: vk::Image,
pub depth_image_memory: vk::DeviceMemory, pub depth_image_memory: vk::DeviceMemory,
pub depth_image_view: vk::ImageView, 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,
} }

View file

@ -17,6 +17,7 @@ pub unsafe fn create_depth_objects(
data.swapchain_extent.width, data.swapchain_extent.width,
data.swapchain_extent.height, data.swapchain_extent.height,
1, 1,
data.msaa_samples,
format, format,
vk::ImageTiling::OPTIMAL, vk::ImageTiling::OPTIMAL,
vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT, vk::ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT,

View file

@ -62,6 +62,7 @@ pub unsafe fn create_texture_image(
width, width,
height, height,
data.mip_levels, data.mip_levels,
vk::SampleCountFlags::_1,
vk::Format::R8G8B8A8_SRGB, vk::Format::R8G8B8A8_SRGB,
vk::ImageTiling::OPTIMAL, vk::ImageTiling::OPTIMAL,
vk::ImageUsageFlags::SAMPLED vk::ImageUsageFlags::SAMPLED
@ -116,6 +117,7 @@ pub unsafe fn create_image(
width: u32, width: u32,
height: u32, height: u32,
mip_levels: u32, mip_levels: u32,
samples: vk::SampleCountFlags,
format: vk::Format, format: vk::Format,
tiling: vk::ImageTiling, tiling: vk::ImageTiling,
usage: vk::ImageUsageFlags, usage: vk::ImageUsageFlags,
@ -134,7 +136,7 @@ pub unsafe fn create_image(
.tiling(tiling) .tiling(tiling)
.initial_layout(vk::ImageLayout::UNDEFINED) .initial_layout(vk::ImageLayout::UNDEFINED)
.usage(usage) .usage(usage)
.samples(vk::SampleCountFlags::_1) .samples(samples)
.sharing_mode(vk::SharingMode::EXCLUSIVE); .sharing_mode(vk::SharingMode::EXCLUSIVE);
let image = device.create_image(&info, None)?; let image = device.create_image(&info, None)?;

View file

@ -131,6 +131,7 @@ impl App {
command_buffer::create_command_pool(&instance, &device, &mut data)?; 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)?; depth_buffer::create_depth_objects(&instance, &device, &mut data)?;
create_framebuffers(&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_render_pass(&self.instance, &self.device, &mut self.data)?;
create_pipeline(&self.device, &mut self.data)?; create_pipeline(&self.device, &mut self.data)?;
buffer::create_descriptor_pool(&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)?; depth_buffer::create_depth_objects(&self.instance, &self.device, &mut self.data)?;
create_framebuffers(&self.device, &mut self.data)?; create_framebuffers(&self.device, &mut self.data)?;
buffer::create_uniform_buffers(&self.instance, &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) { 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_view(self.data.depth_image_view, None);
self.device.destroy_image(self.data.depth_image, 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 { } else {
info!("Selected physical device (`{}`).", properties.device_name); info!("Selected physical device (`{}`).", properties.device_name);
data.physical_device = physical_device; data.physical_device = physical_device;
data.msaa_samples = get_max_msaa_samples(instance, data);
return Ok(()); return Ok(());
} }
} }
@ -627,7 +634,7 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu
let multisample_state = vk::PipelineMultisampleStateCreateInfo::builder() let multisample_state = vk::PipelineMultisampleStateCreateInfo::builder()
.sample_shading_enable(false) .sample_shading_enable(false)
.rasterization_samples(vk::SampleCountFlags::_1); .rasterization_samples(data.msaa_samples);
let depth_stencil_state = vk::PipelineDepthStencilStateCreateInfo::builder() let depth_stencil_state = vk::PipelineDepthStencilStateCreateInfo::builder()
.depth_test_enable(true) .depth_test_enable(true)
@ -703,21 +710,35 @@ unsafe fn create_render_pass(
) -> Result<()> { ) -> Result<()> {
let color_attachment = vk::AttachmentDescription::builder() let color_attachment = vk::AttachmentDescription::builder()
.format(data.swapchain_format) .format(data.swapchain_format)
.samples(vk::SampleCountFlags::_1) .samples(data.msaa_samples)
.load_op(vk::AttachmentLoadOp::CLEAR) .load_op(vk::AttachmentLoadOp::CLEAR)
.store_op(vk::AttachmentStoreOp::STORE) .store_op(vk::AttachmentStoreOp::STORE)
.stencil_load_op(vk::AttachmentLoadOp::DONT_CARE) .stencil_load_op(vk::AttachmentLoadOp::DONT_CARE)
.stencil_store_op(vk::AttachmentStoreOp::DONT_CARE) .stencil_store_op(vk::AttachmentStoreOp::DONT_CARE)
.initial_layout(vk::ImageLayout::UNDEFINED) .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); .final_layout(vk::ImageLayout::PRESENT_SRC_KHR);
let color_attachment_ref = vk::AttachmentReference::builder() let color_resolve_attachment_ref = vk::AttachmentReference::builder()
.attachment(0) .attachment(2)
.layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL); .layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL);
let depth_stencil_attachment = vk::AttachmentDescription::builder() let depth_stencil_attachment = vk::AttachmentDescription::builder()
.format(depth_buffer::get_depth_format(instance, data)?) .format(depth_buffer::get_depth_format(instance, data)?)
.samples(vk::SampleCountFlags::_1) .samples(data.msaa_samples)
.load_op(vk::AttachmentLoadOp::CLEAR) .load_op(vk::AttachmentLoadOp::CLEAR)
.store_op(vk::AttachmentStoreOp::DONT_CARE) .store_op(vk::AttachmentStoreOp::DONT_CARE)
.stencil_load_op(vk::AttachmentLoadOp::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); .layout(vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
let color_attachments = &[color_attachment_ref]; let color_attachments = &[color_attachment_ref];
let resolve_attachments = &[color_resolve_attachment_ref];
let subpass = vk::SubpassDescription::builder() let subpass = vk::SubpassDescription::builder()
.pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS) .pipeline_bind_point(vk::PipelineBindPoint::GRAPHICS)
.color_attachments(color_attachments) .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() let dependency = vk::SubpassDependency::builder()
.src_subpass(vk::SUBPASS_EXTERNAL) .src_subpass(vk::SUBPASS_EXTERNAL)
@ -746,7 +769,7 @@ unsafe fn create_render_pass(
.dst_access_mask(vk::AccessFlags::COLOR_ATTACHMENT_WRITE .dst_access_mask(vk::AccessFlags::COLOR_ATTACHMENT_WRITE
| vk::AccessFlags::DEPTH_STENCIL_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 subpasses = &[subpass];
let dependencies = &[dependency]; let dependencies = &[dependency];
let info = vk::RenderPassCreateInfo::builder() let info = vk::RenderPassCreateInfo::builder()
@ -764,7 +787,7 @@ unsafe fn create_framebuffers(device: &Device, data: &mut app_data::AppData) ->
.swapchain_image_views .swapchain_image_views
.iter() .iter()
.map(|i| { .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() let create_info = vk::FramebufferCreateInfo::builder()
.render_pass(data.render_pass) .render_pass(data.render_pass)
.attachments(attachments) .attachments(attachments)
@ -800,3 +823,58 @@ unsafe fn create_sync_objects(device: &Device, data: &mut app_data::AppData) ->
Ok(()) 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(())
}