Skip to content

Commit

Permalink
Use multiple framebuffer_available semaphores
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Feb 9, 2025
1 parent 00ddc7c commit a7b220f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
48 changes: 36 additions & 12 deletions Backends/Graphics5/Vulkan/Sources/kope/vulkan/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,31 @@ static VKAPI_ATTR void VKAPI_CALL vulkan_free(void *pUserData, void *pMemory) {
}
#endif

static VkSemaphore framebuffer_availables[4];
static uint32_t framebuffer_available_index = 0;

static void init_framebuffer_availables(kope_g5_device *device) {
const VkSemaphoreCreateInfo semaphore_create_info = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
};

for (int i = 0; i < 4; ++i) {
VkResult result = vkCreateSemaphore(device->vulkan.device, &semaphore_create_info, NULL, &framebuffer_availables[i]);
assert(result == VK_SUCCESS);
}
}

static VkSemaphore *get_next_framebuffer_available_semaphore(void) {
framebuffer_available_index = (framebuffer_available_index + 1) % 4;
return &framebuffer_availables[framebuffer_available_index];
}

static VkSemaphore *get_framebuffer_available_semaphore(void) {
return &framebuffer_availables[framebuffer_available_index];
}

static bool check_extensions(const char **extensions, int extensions_count, VkExtensionProperties *extension_properties, int extension_properties_count) {
for (int extension_index = 0; extension_index < extensions_count; ++extension_index) {
bool found = false;
Expand Down Expand Up @@ -648,14 +673,7 @@ void kope_vulkan_device_create(kope_g5_device *device, const kope_g5_device_wish

create_swapchain(device, graphics_queue_family_index);

const VkSemaphoreCreateInfo semaphore_create_info = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = NULL,
.flags = 0,
};

result = vkCreateSemaphore(device->vulkan.device, &semaphore_create_info, NULL, &device->vulkan.framebuffer_available);
assert(result == VK_SUCCESS);
init_framebuffer_availables(device);
}

void kope_vulkan_device_destroy(kope_g5_device *device) {
Expand Down Expand Up @@ -776,8 +794,9 @@ void kope_vulkan_device_create_command_list(kope_g5_device *device, kope_g5_comm
void kope_vulkan_device_create_texture(kope_g5_device *device, const kope_g5_texture_parameters *parameters, kope_g5_texture *texture) {}

kope_g5_texture *kope_vulkan_device_get_framebuffer(kope_g5_device *device) {
VkResult result =
vulkan_AcquireNextImageKHR(device->vulkan.device, swapchain, UINT64_MAX, device->vulkan.framebuffer_available, VK_NULL_HANDLE, &framebuffer_index);
VkResult result = vulkan_AcquireNextImageKHR(device->vulkan.device, swapchain, UINT64_MAX, *get_next_framebuffer_available_semaphore(), VK_NULL_HANDLE,
&framebuffer_index);
assert(result == VK_SUCCESS);

return &framebuffers[framebuffer_index];
}
Expand Down Expand Up @@ -825,18 +844,23 @@ void kope_vulkan_device_execute_command_list(kope_g5_device *device, kope_g5_com

VkPipelineStageFlags stage_mask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;

const VkSubmitInfo submit_info = {
VkSubmitInfo submit_info = {
.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
.pNext = NULL,
.waitSemaphoreCount = 0,
.pWaitSemaphores = NULL,
.pWaitDstStageMask = &stage_mask,
.waitSemaphoreCount = 0,
.commandBufferCount = 1,
.pCommandBuffers = &list->vulkan.command_buffer,
.signalSemaphoreCount = 0,
.pSignalSemaphores = NULL,
};

if (list->vulkan.presenting) {
submit_info.waitSemaphoreCount = 1;
submit_info.pWaitSemaphores = get_framebuffer_available_semaphore();
}

VkResult result = vkQueueSubmit(device->vulkan.queue, 1, &submit_info, VK_NULL_HANDLE);
assert(result == VK_SUCCESS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ typedef struct kope_vulkan_device {
VkCommandPool command_pool;
VkQueue queue;
VkPhysicalDeviceMemoryProperties device_memory_properties;
VkSemaphore framebuffer_available;
} kope_vulkan_device;

typedef struct kope_vulkan_query_set {
Expand Down

0 comments on commit a7b220f

Please sign in to comment.