Add image views

This commit is contained in:
Timothy Warren 2023-04-06 14:35:50 -04:00
parent f9c423cd47
commit cc941caa71
2 changed files with 47 additions and 3 deletions

View File

@ -1,5 +1,8 @@
mod functions; 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 crate::VALIDATION_ENABLED;
use ::anyhow::{anyhow, Result}; use ::anyhow::{anyhow, Result};
@ -35,6 +38,7 @@ impl App {
pick_physical_device(&instance, &mut data)?; pick_physical_device(&instance, &mut data)?;
let device = create_logical_device(&instance, &mut data)?; let device = create_logical_device(&instance, &mut data)?;
create_swapchain(window, &instance, &device, &mut data)?; create_swapchain(window, &instance, &device, &mut data)?;
create_swapchain_image_views(&device, &mut data)?;
Ok(Self { Ok(Self {
entry, entry,
@ -45,12 +49,16 @@ impl App {
} }
/// Renders a frame for our Vulkan 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(()) Ok(())
} }
/// Destroys our Vulkan app. /// Destroys our Vulkan app.
pub unsafe fn destroy(&mut self) { 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_swapchain_khr(self.data.swapchain, None);
self.device.destroy_device(None); self.device.destroy_device(None);
@ -79,6 +87,7 @@ pub struct AppData {
swapchain_extent: vk::Extent2D, swapchain_extent: vk::Extent2D,
swapchain: vk::SwapchainKHR, swapchain: vk::SwapchainKHR,
swapchain_images: Vec<vk::Image>, swapchain_images: Vec<vk::Image>,
swapchain_image_views: Vec<vk::ImageView>,
} }
#[derive(Debug, Error)] #[derive(Debug, Error)]
@ -105,7 +114,7 @@ impl QueueFamilyIndicies {
.map(|i| i as u32); .map(|i| i as u32);
let mut present = None; 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( if instance.get_physical_device_surface_support_khr(
physical_device, physical_device,
index as u32, index as u32,

View File

@ -329,3 +329,38 @@ pub(super) unsafe fn create_swapchain(
Ok(()) 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::<Result<Vec<_>, _>>()?;
Ok(())
}