From a140b1b07c08a547c3f47e7622467ca06508ae1a Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Mon, 10 Feb 2025 21:51:05 +0100 Subject: [PATCH] Make the first Kope sample work via Vulkan --- .../Sources/kinc/backend/graphics5/Vulkan.c.h | 2 ++ .../Sources/kope/vulkan/commandlist_structs.h | 1 + .../Vulkan/Sources/kope/vulkan/device.c | 26 ++++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h index 2057980dc..1fdcbb673 100644 --- a/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h +++ b/Backends/Graphics5/Vulkan/Sources/kinc/backend/graphics5/Vulkan.c.h @@ -1110,9 +1110,11 @@ void kinc_g5_internal_init_window(int window_index, int depthBufferBits, int ste void kinc_g5_internal_destroy_window(int window_index) { struct vk_window *window = &vk_ctx.windows[window_index]; VkSwapchainKHR swapchain = cleanup_swapchain(window_index); +#ifndef KOPE destroy_render_target_pass(window); vk.fpDestroySwapchainKHR(vk_ctx.device, swapchain, NULL); vk.fpDestroySurfaceKHR(vk_ctx.instance, window->surface, NULL); +#endif } bool kinc_window_vsynced(int window) { diff --git a/Backends/Graphics5/Vulkan/Sources/kope/vulkan/commandlist_structs.h b/Backends/Graphics5/Vulkan/Sources/kope/vulkan/commandlist_structs.h index 307d3ded2..9667d2ad8 100644 --- a/Backends/Graphics5/Vulkan/Sources/kope/vulkan/commandlist_structs.h +++ b/Backends/Graphics5/Vulkan/Sources/kope/vulkan/commandlist_structs.h @@ -23,6 +23,7 @@ typedef struct kope_vulkan_command_list { VkDevice device; VkCommandPool command_pool; VkCommandBuffer command_buffer; + VkFence fence; bool presenting; } kope_vulkan_command_list; diff --git a/Backends/Graphics5/Vulkan/Sources/kope/vulkan/device.c b/Backends/Graphics5/Vulkan/Sources/kope/vulkan/device.c index 28aad9571..5942b20ea 100644 --- a/Backends/Graphics5/Vulkan/Sources/kope/vulkan/device.c +++ b/Backends/Graphics5/Vulkan/Sources/kope/vulkan/device.c @@ -770,6 +770,14 @@ void kope_vulkan_device_create_command_list(kope_g5_device *device, kope_g5_comm list->vulkan.command_pool = device->vulkan.command_pool; list->vulkan.presenting = false; + const VkFenceCreateInfo fence_create_info = { + .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, + .pNext = NULL, + .flags = 0, + }; + + vkCreateFence(device->vulkan.device, &fence_create_info, NULL, &list->vulkan.fence); + const VkCommandBufferAllocateInfo allocate_info = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, .pNext = NULL, @@ -861,7 +869,7 @@ void kope_vulkan_device_execute_command_list(kope_g5_device *device, kope_g5_com submit_info.pWaitSemaphores = get_framebuffer_available_semaphore(); } - VkResult result = vkQueueSubmit(device->vulkan.queue, 1, &submit_info, VK_NULL_HANDLE); + VkResult result = vkQueueSubmit(device->vulkan.queue, 1, &submit_info, list->vulkan.fence); assert(result == VK_SUCCESS); if (list->vulkan.presenting) { @@ -886,6 +894,22 @@ void kope_vulkan_device_execute_command_list(kope_g5_device *device, kope_g5_com assert(result == VK_SUCCESS); } } + + // TODO: Use multiple command buffers to avoid waits + result = vkWaitForFences(device->vulkan.device, 1, &list->vulkan.fence, VK_TRUE, UINT64_MAX); + assert(result == VK_SUCCESS); + + result = vkResetFences(device->vulkan.device, 1, &list->vulkan.fence); + assert(result == VK_SUCCESS); + + const VkCommandBufferBeginInfo begin_info = { + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, + .pNext = NULL, + .flags = 0, + .pInheritanceInfo = NULL, + }; + + vkBeginCommandBuffer(list->vulkan.command_buffer, &begin_info); } void kope_vulkan_device_wait_until_idle(kope_g5_device *device) {}