From cc941caa71bab47a481251d053af386d88fc5561 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 6 Apr 2023 14:35:50 -0400 Subject: [PATCH] Add image views --- src/app.rs | 15 ++++++++++++--- src/app/functions.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/app.rs b/src/app.rs index 35c8b3b..1814200 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,5 +1,8 @@ mod functions; -use functions::{create_instance, create_logical_device, create_swapchain, pick_physical_device}; +use functions::{ + create_instance, create_logical_device, create_swapchain, create_swapchain_image_views, + pick_physical_device, +}; use crate::VALIDATION_ENABLED; use ::anyhow::{anyhow, Result}; @@ -35,6 +38,7 @@ impl App { pick_physical_device(&instance, &mut data)?; let device = create_logical_device(&instance, &mut data)?; create_swapchain(window, &instance, &device, &mut data)?; + create_swapchain_image_views(&device, &mut data)?; Ok(Self { entry, @@ -45,12 +49,16 @@ impl App { } /// Renders a frame for our Vulkan app. - pub unsafe fn render(&mut self, window: &Window) -> Result<()> { + pub unsafe fn render(&mut self, _window: &Window) -> Result<()> { Ok(()) } /// Destroys our Vulkan app. pub unsafe fn destroy(&mut self) { + self.data + .swapchain_image_views + .iter() + .for_each(|v| self.device.destroy_image_view(*v, None)); self.device.destroy_swapchain_khr(self.data.swapchain, None); self.device.destroy_device(None); @@ -79,6 +87,7 @@ pub struct AppData { swapchain_extent: vk::Extent2D, swapchain: vk::SwapchainKHR, swapchain_images: Vec, + swapchain_image_views: Vec, } #[derive(Debug, Error)] @@ -105,7 +114,7 @@ impl QueueFamilyIndicies { .map(|i| i as u32); let mut present = None; - for (index, properties) in properties.iter().enumerate() { + for (index, _properties) in properties.iter().enumerate() { if instance.get_physical_device_surface_support_khr( physical_device, index as u32, diff --git a/src/app/functions.rs b/src/app/functions.rs index ac7db03..697b273 100644 --- a/src/app/functions.rs +++ b/src/app/functions.rs @@ -329,3 +329,38 @@ pub(super) unsafe fn create_swapchain( Ok(()) } + +pub(super) unsafe fn create_swapchain_image_views( + device: &Device, + data: &mut AppData, +) -> Result<()> { + data.swapchain_image_views = data + .swapchain_images + .iter() + .map(|i| { + let components = vk::ComponentMapping::builder() + .r(vk::ComponentSwizzle::IDENTITY) + .g(vk::ComponentSwizzle::IDENTITY) + .b(vk::ComponentSwizzle::IDENTITY) + .a(vk::ComponentSwizzle::IDENTITY); + + let subresource_range = vk::ImageSubresourceRange::builder() + .aspect_mask(vk::ImageAspectFlags::COLOR) + .base_mip_level(0) + .level_count(1) + .base_array_layer(0) + .layer_count(1); + + let info = vk::ImageViewCreateInfo::builder() + .image(*i) + .view_type(vk::ImageViewType::_2D) + .format(data.swapchain_format) + .components(components) + .subresource_range(subresource_range); + + device.create_image_view(&info, None) + }) + .collect::, _>>()?; + + Ok(()) +}