vulk_ext/vkx/
swapchain.rs

1use super::*;
2
3#[derive(Debug)]
4pub struct Swapchain {
5    swapchain: vk::SwapchainKHR,
6    create_info: vk::SwapchainCreateInfoKHR,
7    images: Vec<(vk::Image, vk::ImageView)>,
8}
9
10impl Swapchain {
11    pub unsafe fn create(device: &Device, surface: &Surface) -> Result<Self> {
12        // Swapchain create info.
13        let create_info = vk::SwapchainCreateInfoKHR {
14            s_type: vk::StructureType::SwapchainCreateInfoKHR,
15            p_next: null(),
16            flags: vk::SwapchainCreateFlagsKHR::empty(),
17            surface: surface.handle(),
18            min_image_count: surface.surface_capabilities.min_image_count,
19            image_format: surface.surface_format.format,
20            image_color_space: surface.surface_format.color_space,
21            image_extent: vk::Extent2D {
22                width: surface.surface_capabilities.min_image_extent.width,
23                height: surface.surface_capabilities.min_image_extent.height,
24            },
25            image_array_layers: 1,
26            image_usage: vk::ImageUsageFlagBits::ColorAttachment.into(),
27            image_sharing_mode: vk::SharingMode::Exclusive,
28            queue_family_index_count: 0,
29            p_queue_family_indices: null(),
30            pre_transform: vk::SurfaceTransformFlagBitsKHR::IdentityKHR,
31            composite_alpha: vk::CompositeAlphaFlagBitsKHR::OpaqueKHR,
32            present_mode: surface.present_mode,
33            clipped: vk::TRUE,
34            old_swapchain: vk::SwapchainKHR::null(),
35        };
36
37        // Swapchain.
38        let swapchain = device.create_swapchain_khr(&create_info)?;
39
40        // Swapchain images.
41        let mut images = vec![];
42        for image in vulk::read_to_vec(
43            |count, ptr| device.get_swapchain_images_khr(swapchain, count, ptr),
44            None,
45        )? {
46            let (image_view, _) =
47                ImageViewCreator::new_2d(image, surface.surface_format.format).create(device)?;
48            images.push((image, image_view));
49        }
50
51        Ok(Self {
52            swapchain,
53            create_info,
54            images,
55        })
56    }
57
58    pub unsafe fn destroy(self, device: &Device) {
59        device.destroy_swapchain_khr(self.swapchain);
60        for &(_, image_view) in &self.images {
61            device.destroy_image_view(image_view);
62        }
63    }
64
65    #[must_use]
66    pub fn handle(&self) -> vk::SwapchainKHR {
67        self.swapchain
68    }
69
70    #[must_use]
71    pub fn image_count(&self) -> u64 {
72        self.images.len() as u64
73    }
74
75    #[must_use]
76    pub fn image(&self, image_index: u32) -> vk::Image {
77        self.images[image_index as usize].0
78    }
79
80    #[must_use]
81    pub fn image_view(&self, image_index: u32) -> vk::ImageView {
82        self.images[image_index as usize].1
83    }
84
85    #[must_use]
86    pub fn image_subresource_range(&self) -> vk::ImageSubresourceRange {
87        vk::ImageSubresourceRange {
88            aspect_mask: self.create_info.image_format.aspect_mask(),
89            base_mip_level: 0,
90            level_count: 1,
91            base_array_layer: 0,
92            layer_count: 1,
93        }
94    }
95
96    #[must_use]
97    pub fn render_area(&self) -> vk::Rect2D {
98        vk::Rect2D {
99            offset: vk::Offset2D { x: 0, y: 0 },
100            extent: self.create_info.image_extent,
101        }
102    }
103}