1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use crate::graphics::{
    frame::Frame,
    vulkan::{Device, Swapchain, WindowSurface},
};

use anyhow::Result;
use ash::{version::DeviceV1_0, vk};
use std::sync::Arc;

/// An enum used by the frame context to signal when the swapchain needs to be
/// rebuilt.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum SwapchainState {
    Ok,
    NeedsRebuild,
}

#[cfg_attr(doc, aquamarine::aquamarine)]
/// This struct is responsible for requesting a framebuffer from the swapchain,
/// rendering, and presenting the buffer for presentation.
///
/// This app associates resources with each framebuffer to minimize sharing and
/// synchronization between frames.
///
pub struct FrameContext {
    ///! There is one frame per swapchain framebuffer.
    frames_in_flight: Vec<Option<Frame>>,

    ///! The index of the last frame presented via the swapchain.
    current_frame_index: usize,

    ///! An enum indicating when the swapchain needs to be reconstructed.
    swapchain_state: SwapchainState,

    /// Populated automatically when a frame is started, and consumed
    /// automatically when the frame is completed.
    current_image_acquired_semaphore: vk::Semaphore,

    ///! An owning reference to the application swapchain.
    swapchain: Arc<Swapchain>,

    ///! An owning reference to the application's vulkan device resources.
    device: Arc<Device>,
}

impl FrameContext {
    /// Create a new Frame context.
    pub fn new(device: Arc<Device>, swapchain: Arc<Swapchain>) -> Result<Self> {
        Ok(Self {
            frames_in_flight: Frame::create_n_frames(
                &device,
                &swapchain.framebuffers,
            )?,
            swapchain_state: SwapchainState::Ok,
            current_image_acquired_semaphore: vk::Semaphore::null(),
            current_frame_index: 0,
            swapchain,
            device,
        })
    }

    /// Borrow the swapchain owned by this frame context.
    pub fn swapchain(&self) -> &Swapchain {
        &self.swapchain
    }

    /// Acquire the next swapchain image and select the frame-specific
    /// resources which are now ready to be used.
    pub fn acquire_frame(&mut self) -> Result<Frame, SwapchainState> {
        if self.swapchain_state == SwapchainState::NeedsRebuild {
            return Err(SwapchainState::NeedsRebuild);
        }

        self.current_image_acquired_semaphore = self.frames_in_flight
            [self.current_frame_index]
            .as_ref()
            .expect("the current frame was never ended!")
            .sync
            .image_available_semaphore;

        let result = unsafe {
            self.swapchain.swapchain_loader.acquire_next_image(
                self.swapchain.swapchain,
                u64::MAX,
                self.current_image_acquired_semaphore,
                vk::Fence::null(),
            )
        };
        if let Err(vk::Result::ERROR_OUT_OF_DATE_KHR) = result {
            return Err(SwapchainState::NeedsRebuild);
        }
        if let Ok((_, true)) = result {
            return Err(SwapchainState::NeedsRebuild);
        }

        let (index, _) = result.ok().unwrap();
        self.current_frame_index = index as usize;

        let mut current_frame = self.frames_in_flight[self.current_frame_index]
            .take()
            .expect("the current frame was never returned!");

        current_frame
            .begin_frame()
            .expect("unable to begin the current frame!");

        Ok(current_frame)
    }

    /// Complete the current frame and present the framebuffer.
    pub fn return_frame(&mut self, mut frame: Frame) -> Result<()> {
        let image_acquired_semaphore = self.current_image_acquired_semaphore;
        let render_finished_semaphore =
            frame.finish_frame(image_acquired_semaphore)?;
        self.frames_in_flight[self.current_frame_index] = Some(frame);

        let render_finished_semaphores = &[render_finished_semaphore];
        let swapchains = [self.swapchain.swapchain];
        let indices = [self.current_frame_index as u32];

        let present_info = vk::PresentInfoKHR {
            p_wait_semaphores: render_finished_semaphores.as_ptr(),
            wait_semaphore_count: render_finished_semaphores.len() as u32,
            p_swapchains: swapchains.as_ptr(),
            swapchain_count: swapchains.len() as u32,
            p_image_indices: indices.as_ptr(),
            ..Default::default()
        };

        let result = unsafe {
            self.swapchain
                .swapchain_loader
                .queue_present(self.device.present_queue.raw(), &present_info)
        };
        if Err(vk::Result::ERROR_OUT_OF_DATE_KHR) == result {
            self.swapchain_state = SwapchainState::NeedsRebuild;
        }

        Ok(())
    }

    /// Wait for all rendering operations to complete on every frame, then
    /// rebuild the swapchain.
    ///
    /// Returns a clone of the swapchain which can be used by other systems.
    pub fn rebuild_swapchain(
        &mut self,
        window_surface: &dyn WindowSurface,
    ) -> Result<Arc<Swapchain>> {
        unsafe {
            self.device.logical_device.device_wait_idle()?;
            self.frames_in_flight.clear();
        }
        self.swapchain = self.swapchain.rebuild(window_surface)?;
        self.frames_in_flight =
            Frame::create_n_frames(&self.device, &self.swapchain.framebuffers)?;
        self.swapchain_state = SwapchainState::Ok;

        Ok(self.swapchain.clone())
    }
}

impl Drop for FrameContext {
    fn drop(&mut self) {
        unsafe {
            // don't delete anything until the GPU has stoped using our
            // resources
            self.device
                .logical_device
                .device_wait_idle()
                .expect("wait for device to idle");

            self.frames_in_flight.clear();
        }
    }
}