|
1 | | -/* Copyright (c) 2019-2025, Sascha Willems |
| 1 | +/* Copyright (c) 2019-2026, Sascha Willems |
2 | 2 | * |
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
4 | 4 | * |
@@ -53,6 +53,7 @@ ComputeNBody::~ComputeNBody() |
53 | 53 | vkDestroyDescriptorSetLayout(get_device().get_handle(), compute.descriptor_set_layout, nullptr); |
54 | 54 | vkDestroyPipeline(get_device().get_handle(), compute.pipeline_calculate, nullptr); |
55 | 55 | vkDestroyPipeline(get_device().get_handle(), compute.pipeline_integrate, nullptr); |
| 56 | + vkDestroyFence(get_device().get_handle(), compute.fence, nullptr); |
56 | 57 | vkDestroySemaphore(get_device().get_handle(), compute.semaphore, nullptr); |
57 | 58 | vkDestroyCommandPool(get_device().get_handle(), compute.command_pool, nullptr); |
58 | 59 |
|
@@ -664,6 +665,11 @@ void ComputeNBody::prepare_compute() |
664 | 665 | VkSemaphoreCreateInfo semaphore_create_info = vkb::initializers::semaphore_create_info(); |
665 | 666 | VK_CHECK(vkCreateSemaphore(get_device().get_handle(), &semaphore_create_info, nullptr, &compute.semaphore)); |
666 | 667 |
|
| 668 | + // Fence to ensure compute dispatch has finished reading the UBO before we update it. |
| 669 | + // Created signaled so the first frame's wait doesn't block forever. |
| 670 | + VkFenceCreateInfo fence_create_info = vkb::initializers::fence_create_info(VK_FENCE_CREATE_SIGNALED_BIT); |
| 671 | + VK_CHECK(vkCreateFence(get_device().get_handle(), &fence_create_info, nullptr, &compute.fence)); |
| 672 | + |
667 | 673 | // Signal the semaphore |
668 | 674 | VkSubmitInfo submit_info = {VK_STRUCTURE_TYPE_SUBMIT_INFO}; |
669 | 675 | submit_info.signalSemaphoreCount = 1; |
@@ -825,7 +831,7 @@ void ComputeNBody::draw() |
825 | 831 | compute_submit_info.pWaitDstStageMask = &wait_stage_mask; |
826 | 832 | compute_submit_info.signalSemaphoreCount = 1; |
827 | 833 | compute_submit_info.pSignalSemaphores = &compute.semaphore; |
828 | | - VK_CHECK(vkQueueSubmit(compute.queue, 1, &compute_submit_info, VK_NULL_HANDLE)); |
| 834 | + VK_CHECK(vkQueueSubmit(compute.queue, 1, &compute_submit_info, compute.fence)); |
829 | 835 | } |
830 | 836 |
|
831 | 837 | bool ComputeNBody::prepare(const vkb::ApplicationOptions &options) |
@@ -859,12 +865,17 @@ void ComputeNBody::render(float delta_time) |
859 | 865 | { |
860 | 866 | return; |
861 | 867 | } |
862 | | - draw(); |
| 868 | + |
| 869 | + // Wait for the previous compute dispatch to finish before updating the UBO |
| 870 | + VK_CHECK(vkWaitForFences(get_device().get_handle(), 1, &compute.fence, VK_TRUE, UINT64_MAX)); |
| 871 | + VK_CHECK(vkResetFences(get_device().get_handle(), 1, &compute.fence)); |
| 872 | + |
863 | 873 | update_compute_uniform_buffers(delta_time); |
864 | 874 | if (camera.updated) |
865 | 875 | { |
866 | 876 | update_graphics_uniform_buffers(); |
867 | 877 | } |
| 878 | + draw(); |
868 | 879 | } |
869 | 880 |
|
870 | 881 | bool ComputeNBody::resize(const uint32_t width, const uint32_t height) |
|
0 commit comments