Add sync objects

This commit is contained in:
2026-06-14 15:45:48 +01:00
parent e3195c9b12
commit d2c901f4e4
+42
View File
@@ -37,6 +37,7 @@ enum ExitCode {
EXIT_CODE_NO_SUITABLE_QUEUE_FAMILY,
EXIT_CODE_NO_SUITABLE_DEPTH_FORMAT,
EXIT_CODE_MESH_LOAD_FAILED,
EXIT_CODE_SYNC_OBJ_CREATE_FAILED,
};
struct Vertex {
@@ -67,6 +68,8 @@ typedef VkImageView *VkImageViewArray;
typedef VkFormat *VkFormatArray;
typedef Vertex *VertexArray;
typedef ShaderDataBuffer *ShaderDataBufferArray;
typedef VkFence *VkFenceArray;
typedef VkSemaphore *VkSemaphoreArray;
wapp_intern inline void check(VkResult result);
wapp_intern inline void check_swapchain(VkResult result);
@@ -103,6 +106,9 @@ wapp_intern VkBuffer vert_index_buf = VK_NULL_HANDLE;
wapp_intern VmaAllocation vert_index_buf_alloc = VK_NULL_HANDLE;
wapp_intern ShaderData shader_data = {};
wapp_intern ShaderDataBufferArray shader_data_bufs;
wapp_intern VkFenceArray fences = nullptr;
wapp_intern VkSemaphoreArray image_acquired_semaphores = nullptr;
wapp_intern VkSemaphoreArray render_completed_semaphores = nullptr;
int main() {
// {{{ Initialisation
@@ -538,6 +544,34 @@ int main() {
}
// }}}
// Synchronization Objects {{{
fences = wapp_array_alloc_capacity(VkFence, &arena, max_frames_in_flight, ARRAY_INIT_FILLED);
check(fences != nullptr, EXIT_CODE_SYNC_OBJ_CREATE_FAILED);
image_acquired_semaphores = wapp_array_alloc_capacity(VkSemaphore, &arena, max_frames_in_flight, ARRAY_INIT_FILLED);
check(image_acquired_semaphores != nullptr, EXIT_CODE_SYNC_OBJ_CREATE_FAILED);
// The number of semaphores used to signal rendering needs to match that of the swapchain's images
render_completed_semaphores = wapp_array_alloc_capacity(VkSemaphore, &arena, swapchain_image_count, ARRAY_INIT_FILLED);
check(render_completed_semaphores != nullptr, EXIT_CODE_SYNC_OBJ_CREATE_FAILED);
VkFenceCreateInfo fence_create_info = {};
fence_create_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fence_create_info.flags = VK_FENCE_CREATE_SIGNALED_BIT;
VkSemaphoreCreateInfo semaphore_create_info = {};
semaphore_create_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
for (u32 i = 0; i < max_frames_in_flight; ++i) {
check(vkCreateFence(device, &fence_create_info, NULL, &fences[i]));
check(vkCreateSemaphore(device, &semaphore_create_info, NULL, &image_acquired_semaphores[i]));
}
for (u32 i = 0; i < swapchain_image_count; ++i) {
check(vkCreateSemaphore(device, &semaphore_create_info, NULL, &render_completed_semaphores[i]));
}
// }}}
// {{{ Render Loop
SDL_Event event = {};
while (running) {
@@ -557,6 +591,14 @@ int main() {
// }}}
// {{{ Cleanup
for (u32 i = 0; i < swapchain_image_count; ++i) {
vkDestroySemaphore(device, render_completed_semaphores[i], NULL);
}
for (u32 i = 0; i < max_frames_in_flight; ++i) {
vkDestroySemaphore(device, image_acquired_semaphores[i], NULL);
vkDestroyFence(device, fences[i], NULL);
}
for (u32 i = 0; i < max_frames_in_flight; ++i) {
vmaDestroyBuffer(allocator, shader_data_bufs[i].buffer, shader_data_bufs[i].allocation);
}