summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c60
1 files changed, 34 insertions, 26 deletions
diff --git a/main.c b/main.c
index 273f51f..2dddbd0 100644
--- a/main.c
+++ b/main.c
@@ -5,6 +5,8 @@
#include <stdbool.h>
#include <string.h>
+#define MAX_FRAMES_INFLIGHT 2
+
static SDL_Window *window = NULL;
static VkDebugUtilsMessengerEXT debug_messenger;
static VkInstance instance = VK_NULL_HANDLE;
@@ -19,11 +21,12 @@ static VkExtent2D swapchain_extent;
static VkRenderPass render_pass;
static VkPipelineLayout pipeline_layout;
static VkCommandPool command_pool;
-static VkCommandBuffer command_buffer;
+static VkCommandBuffer command_buffers[MAX_FRAMES_INFLIGHT];
static VkPipeline graphics_pipeline;
-static VkSemaphore image_avail;
-static VkSemaphore render_finished;
-static VkFence in_flight_fence;
+static VkSemaphore image_avail[MAX_FRAMES_INFLIGHT];
+static VkSemaphore render_finished[MAX_FRAMES_INFLIGHT];
+static VkFence in_flight_fence[MAX_FRAMES_INFLIGHT];
+static uint32_t current_frame = 0;
static struct {
size_t len;
VkImage data[];
@@ -129,9 +132,11 @@ bool found_queue_indx(struct queue_indices inds) {
void cleanup() {
vkDeviceWaitIdle(gpu);
- vkDestroySemaphore(gpu, image_avail, NULL);
- vkDestroySemaphore(gpu, render_finished, NULL);
- vkDestroyFence(gpu, in_flight_fence, NULL);
+ for (size_t i = 0; i < MAX_FRAMES_INFLIGHT; i++) {
+ vkDestroySemaphore(gpu, image_avail[i], NULL);
+ vkDestroySemaphore(gpu, render_finished[i], NULL);
+ vkDestroyFence(gpu, in_flight_fence[i], NULL);
+ }
vkDestroyCommandPool(gpu, command_pool, NULL);
for (size_t i = 0; i < swapchain_buffers->len; i++) {
vkDestroyFramebuffer(gpu, swapchain_buffers->data[i], NULL);
@@ -685,15 +690,15 @@ void create_command_pool() {
}
}
-void create_command_buffer() {
+void create_command_buffers() {
VkCommandBufferAllocateInfo alloc_info = {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
.commandPool = command_pool,
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
- .commandBufferCount = 1
+ .commandBufferCount = MAX_FRAMES_INFLIGHT
};
- VkResult res = vkAllocateCommandBuffers(gpu, &alloc_info, &command_buffer);
+ VkResult res = vkAllocateCommandBuffers(gpu, &alloc_info, command_buffers);
if (res != VK_SUCCESS) {
fputs("failed to allocate command buffer", stderr);
exit(-1);
@@ -762,12 +767,13 @@ void create_sync_objects() {
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
.flags = VK_FENCE_CREATE_SIGNALED_BIT
};
- if (vkCreateSemaphore(gpu, &semaphore_info, NULL, &image_avail) != VK_SUCCESS
- || vkCreateSemaphore(gpu, &semaphore_info, NULL, &render_finished) != VK_SUCCESS
- || vkCreateFence(gpu, &fence_info, NULL, &in_flight_fence) != VK_SUCCESS) {
- fputs("failed to create semaphores and fence", stderr);
- exit(-1);
- }
+ for (size_t i = 0; i < MAX_FRAMES_INFLIGHT; i++)
+ if (vkCreateSemaphore(gpu, &semaphore_info, NULL, &image_avail[i]) != VK_SUCCESS
+ || vkCreateSemaphore(gpu, &semaphore_info, NULL, &render_finished[i]) != VK_SUCCESS
+ || vkCreateFence(gpu, &fence_info, NULL, &in_flight_fence[i]) != VK_SUCCESS) {
+ fputs("failed to create semaphores and fence", stderr);
+ exit(-1);
+ }
}
void init() {
@@ -806,21 +812,21 @@ void init() {
create_graphics_pipeline();
create_framebuffers();
create_command_pool();
- create_command_buffer();
+ create_command_buffers();
create_sync_objects();
}
void draw() {
- vkWaitForFences(gpu, 1, &in_flight_fence, VK_TRUE, UINT64_MAX);
- vkResetFences(gpu, 1, &in_flight_fence);
+ vkWaitForFences(gpu, 1, &in_flight_fence[current_frame], VK_TRUE, UINT64_MAX);
+ vkResetFences(gpu, 1, &in_flight_fence[current_frame]);
uint32_t image_index;
- vkAcquireNextImageKHR(gpu, swapchain, UINT64_MAX, image_avail, VK_NULL_HANDLE, &image_index);
- vkResetCommandBuffer(command_buffer, 0);
- record_command_buffer(command_buffer, image_index);
+ vkAcquireNextImageKHR(gpu, swapchain, UINT64_MAX, image_avail[current_frame], VK_NULL_HANDLE, &image_index);
+ vkResetCommandBuffer(command_buffers[current_frame], 0);
+ record_command_buffer(command_buffers[current_frame], image_index);
- VkSemaphore wait_semaphores[] = { image_avail };
- VkSemaphore signal_semaphores[] = { render_finished };
+ VkSemaphore wait_semaphores[] = { image_avail[current_frame] };
+ VkSemaphore signal_semaphores[] = { render_finished[current_frame] };
VkPipelineStageFlags wait_stages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
VkSubmitInfo submit_info = {
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
@@ -830,10 +836,10 @@ void draw() {
.signalSemaphoreCount = 1,
.pSignalSemaphores = signal_semaphores,
.commandBufferCount = 1,
- .pCommandBuffers = &command_buffer
+ .pCommandBuffers = &command_buffers[current_frame]
};
- VkResult res = vkQueueSubmit(gfx_queue, 1, &submit_info, in_flight_fence);
+ VkResult res = vkQueueSubmit(gfx_queue, 1, &submit_info, in_flight_fence[current_frame]);
if (res != VK_SUCCESS) {
fputs("failed to submit draw command buffer", stderr);
exit(-1);
@@ -850,6 +856,8 @@ void draw() {
};
vkQueuePresentKHR(present_queue, &present_info);
+
+ current_frame = (current_frame + 1) % MAX_FRAMES_INFLIGHT;
}
int main() {