move sync objects to separate module

This commit is contained in:
jacekpoz 2024-09-24 12:56:12 +02:00
parent 15e74a9a17
commit 2d3c33cb75
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
4 changed files with 76 additions and 41 deletions

View file

@ -159,43 +159,6 @@ bool vk_record_command_buffer(const VkCommandBuffer command_buffer, const uint32
return true;
}
bool create_sync_objects(void) {
g_image_available_semaphores = PTK_LIST_NEW(VkSemaphore, g_max_frames_in_flight);
g_render_finished_semaphores = PTK_LIST_NEW(VkSemaphore, g_max_frames_in_flight);
g_in_flight_fences = PTK_LIST_NEW(VkFence, g_max_frames_in_flight);
const VkSemaphoreCreateInfo semaphore_info = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
};
for (size_t i = 0; i < g_max_frames_in_flight; ++i) {
VK_TRY(false,
vkCreateSemaphore(g_dev, &semaphore_info, NULL, &g_image_available_semaphores.data[i])
);
VK_TRY(false,
vkCreateSemaphore(g_dev, &semaphore_info, NULL, &g_render_finished_semaphores.data[i])
);
VK_TRY(false,
vkCreateFence(
g_dev,
&(VkFenceCreateInfo){
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
.pNext = NULL,
.flags = VK_FENCE_CREATE_SIGNALED_BIT,
},
NULL,
&g_in_flight_fences.data[i]
)
);
}
return true;
}
bool vk_update_uniform_buffer(const size_t current_frame) {
m_uniform_buffer_object.window_size.w = g_swapchain_extent.width;
m_uniform_buffer_object.window_size.h = g_swapchain_extent.height;
@ -363,11 +326,17 @@ bool vk_init(GLFWwindow *window, const size_t width, const size_t height, const
g_command_buffers = command_buffers_opt.value;
if (!create_sync_objects()) {
PTK_OPTION(SyncObjects) sync_objects_opt = vk_create_sync_objects(g_dev);
if (!sync_objects_opt.exists) {
PTK_ERR("failed creating sync objects");
return false;
}
g_image_available_semaphores = sync_objects_opt.value.image_available_semaphores;
g_render_finished_semaphores = sync_objects_opt.value.render_finished_semaphores;
g_in_flight_fences = sync_objects_opt.value.in_flight_fences;
return true;
}

View file

@ -11,9 +11,7 @@
#include <ptk_list.h>
#include <ptk_vk/command_buffers.h>
PTK_LIST_DEFINE(VkSemaphore);
PTK_LIST_DEFINE(VkFence);
#include <ptk_vk/sync_objects.h>
extern VkPhysicalDevice g_physical_dev;

44
src/ptk_vk/sync_objects.c Normal file
View file

@ -0,0 +1,44 @@
#include <ptk_vk/sync_objects.h>
#include <ptk_vk/init.h>
#include <ptk_vk/utils.h>
PTK_OPTION(SyncObjects) vk_create_sync_objects(VkDevice dev) {
SyncObjects ret = {
.image_available_semaphores = PTK_LIST_NEW(VkSemaphore, g_max_frames_in_flight),
.render_finished_semaphores = PTK_LIST_NEW(VkSemaphore, g_max_frames_in_flight),
.in_flight_fences = PTK_LIST_NEW(VkFence, g_max_frames_in_flight),
};
const VkSemaphoreCreateInfo semaphore_info = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
};
for (size_t i = 0; i < g_max_frames_in_flight; ++i) {
VK_TRY(PTK_OPTION_NONE(SyncObjects),
vkCreateSemaphore(dev, &semaphore_info, NULL, &ret.image_available_semaphores.data[i])
);
VK_TRY(PTK_OPTION_NONE(SyncObjects),
vkCreateSemaphore(dev, &semaphore_info, NULL, &ret.render_finished_semaphores.data[i])
);
VK_TRY(PTK_OPTION_NONE(SyncObjects),
vkCreateFence(
dev,
&(VkFenceCreateInfo){
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
.pNext = NULL,
.flags = VK_FENCE_CREATE_SIGNALED_BIT,
},
NULL,
&ret.in_flight_fences.data[i]
)
);
}
return PTK_OPTION_SOME(SyncObjects, ret);
}

24
src/ptk_vk/sync_objects.h Normal file
View file

@ -0,0 +1,24 @@
// Copyright (jacekpoz 2024). Licensed under the EUPL-1.2 or later.
#ifndef PTK_PTK_VK_SYNC_OBJECTS_H_
#define PTK_PTK_VK_SYNC_OBJECTS_H_
#include <vulkan/vulkan_core.h>
#include <ptk_list.h>
#include <ptk_option.h>
PTK_LIST_DEFINE(VkSemaphore);
PTK_LIST_DEFINE(VkFence);
typedef struct {
PTK_LIST(VkSemaphore) image_available_semaphores;
PTK_LIST(VkSemaphore) render_finished_semaphores;
PTK_LIST(VkFence) in_flight_fences;
} SyncObjects;
PTK_OPTION_DEFINE(SyncObjects);
PTK_OPTION(SyncObjects) vk_create_sync_objects(VkDevice dev);
#endif // PTK_PTK_VK_SYNC_OBJECTS_H_