|
1 | 1 | # Swapchain Update |
2 | 2 |
|
| 3 | +Add a vector of semaphores and populate them in `recreate()`: |
| 4 | + |
| 5 | +```cpp |
| 6 | +void create_present_semaphores(); |
| 7 | + |
| 8 | +// ... |
| 9 | +// signaled when image is ready to be presented. |
| 10 | +std::vector<vk::UniqueSemaphore> m_present_semaphores{}; |
| 11 | + |
| 12 | +// ... |
| 13 | +auto Swapchain::recreate(glm::ivec2 size) -> bool { |
| 14 | + // ... |
| 15 | + populate_images(); |
| 16 | + create_image_views(); |
| 17 | + // recreate present semaphores as the image count might have changed. |
| 18 | + create_present_semaphores(); |
| 19 | + // ... |
| 20 | +} |
| 21 | + |
| 22 | +void Swapchain::create_present_semaphores() { |
| 23 | + m_present_semaphores.clear(); |
| 24 | + m_present_semaphores.resize(m_images.size()); |
| 25 | + for (auto& semaphore : m_present_semaphores) { |
| 26 | + semaphore = m_device.createSemaphoreUnique({}); |
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | +
|
| 31 | +Add a function to get the present semaphore corresponding to the acquired image, this will be signaled by render command submission: |
| 32 | +
|
| 33 | +```cpp |
| 34 | +auto Swapchain::get_present_semaphore() const -> vk::Semaphore { |
| 35 | + return *m_present_semaphores.at(m_image_index.value()); |
| 36 | +} |
| 37 | +``` |
| 38 | + |
3 | 39 | Swapchain acquire/present operations can have various results. We constrain ourselves to the following: |
4 | 40 |
|
5 | 41 | - `eSuccess`: all good |
@@ -59,13 +95,15 @@ auto Swapchain::acquire_next_image(vk::Semaphore const to_signal) |
59 | 95 | Similarly, present: |
60 | 96 |
|
61 | 97 | ```cpp |
62 | | -auto Swapchain::present(vk::Queue const queue, vk::Semaphore const to_wait) |
| 98 | +auto Swapchain::present(vk::Queue const queue) |
63 | 99 | -> bool { |
64 | 100 | auto const image_index = static_cast<std::uint32_t>(m_image_index.value()); |
| 101 | + auto const wait_semaphore = |
| 102 | + *m_present_semaphores.at(static_cast<std::size_t>(image_index)); |
65 | 103 | auto present_info = vk::PresentInfoKHR{}; |
66 | 104 | present_info.setSwapchains(*m_swapchain) |
67 | 105 | .setImageIndices(image_index) |
68 | | - .setWaitSemaphores(to_wait); |
| 106 | + .setWaitSemaphores(wait_semaphore); |
69 | 107 | // avoid VulkanHPP ErrorOutOfDateKHR exceptions by using alternate API. |
70 | 108 | auto const result = queue.presentKHR(&present_info); |
71 | 109 | m_image_index.reset(); |
|
0 commit comments