From 4aa18d4aaea97be72234a965603a16ae6291afa7 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Tue, 29 Oct 2024 12:47:04 -0600 Subject: [PATCH] Moved compute out of transfer if --- client/include/gpu.h | 1 + client/src/draw.c | 61 ++++++++++++++++++++++++++------------------ client/src/gpu.c | 3 ++- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/client/include/gpu.h b/client/include/gpu.h index a3a1b7c..278b250 100644 --- a/client/include/gpu.h +++ b/client/include/gpu.h @@ -81,6 +81,7 @@ typedef struct FrameContextStruct { VkSemaphore frame; uint64_t frame_index; + uint64_t transfer_index; uint64_t compute_index; VkCommandBuffer compute_commands; diff --git a/client/src/draw.c b/client/src/draw.c index df4f635..a36b6ac 100644 --- a/client/src/draw.c +++ b/client/src/draw.c @@ -85,11 +85,14 @@ VkResult draw_frame( } VK_RESULT(vkEndCommandBuffer(transfer_commands)); + frame->transfer_index += 1; VkPipelineStageFlags transfer_wait_stages[] = {VK_PIPELINE_STAGE_TRANSFER_BIT}; VkTimelineSemaphoreSubmitInfo transfer_timeline = { .sType = VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO, .waitSemaphoreValueCount = 1, .pWaitSemaphoreValues = &frame->frame_index, + .signalSemaphoreValueCount = 1, + .pSignalSemaphoreValues = &frame->transfer_index, }; VkSubmitInfo transfer_submit = { .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, @@ -107,32 +110,40 @@ VkResult draw_frame( frame->transfer_written = 0; - VkCommandBuffer compute_commands = frame->compute_commands; - VK_RESULT(vkResetCommandBuffer(compute_commands, 0)); - VK_RESULT(vkBeginCommandBuffer(compute_commands, &begin_info)); - record_ui_compute(compute_commands, ui, context->current_frame); - VK_RESULT(vkEndCommandBuffer(compute_commands)); - - frame->compute_index += 1; - VkPipelineStageFlags compute_wait_stages[] = {VK_PIPELINE_STAGE_TRANSFER_BIT | VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT}; - VkTimelineSemaphoreSubmitInfo compute_timeline = { - .sType = VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO, - .signalSemaphoreValueCount = 1, - .pSignalSemaphoreValues = &frame->compute_index, - }; - VkSubmitInfo compute_submit = { - .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, - .commandBufferCount = 1, - .pCommandBuffers = &compute_commands, - .pSignalSemaphores = &frame->compute, - .signalSemaphoreCount = 1, - .pWaitSemaphores = &frame->transfer, - .pWaitDstStageMask = compute_wait_stages, - .waitSemaphoreCount = 1, - .pNext = &compute_timeline, - }; - VK_RESULT(vkQueueSubmit(context->transfer_queue.handle, 1, &compute_submit, VK_NULL_HANDLE)); } + VkCommandBuffer compute_commands = frame->compute_commands; + VK_RESULT(vkResetCommandBuffer(compute_commands, 0)); + VK_RESULT(vkBeginCommandBuffer(compute_commands, &begin_info)); + VkMemoryBarrier compute_barrier = { + .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER, + .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, + .dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT, + }; + vkCmdPipelineBarrier(compute_commands, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 1, &compute_barrier, 0, NULL, 0, NULL); + record_ui_compute(compute_commands, ui, context->current_frame); + VK_RESULT(vkEndCommandBuffer(compute_commands)); + + frame->compute_index += 1; + VkPipelineStageFlags compute_wait_stages[] = {VK_PIPELINE_STAGE_ALL_COMMANDS_BIT}; + VkTimelineSemaphoreSubmitInfo compute_timeline = { + .sType = VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO, + .waitSemaphoreValueCount = 1, + .pWaitSemaphoreValues = &frame->transfer_index, + .signalSemaphoreValueCount = 1, + .pSignalSemaphoreValues = &frame->compute_index, + }; + VkSubmitInfo compute_submit = { + .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, + .commandBufferCount = 1, + .pCommandBuffers = &compute_commands, + .pSignalSemaphores = &frame->compute, + .signalSemaphoreCount = 1, + .pWaitSemaphores = &frame->transfer, + .pWaitDstStageMask = compute_wait_stages, + .waitSemaphoreCount = 1, + .pNext = &compute_timeline, + }; + VK_RESULT(vkQueueSubmit(context->transfer_queue.handle, 1, &compute_submit, VK_NULL_HANDLE)); uint32_t image_index; VK_RESULT(vkAcquireNextImageKHR(context->device, context->swapchain, UINT64_MAX, frame->image, VK_NULL_HANDLE, &image_index)); diff --git a/client/src/gpu.c b/client/src/gpu.c index 6626198..76059af 100644 --- a/client/src/gpu.c +++ b/client/src/gpu.c @@ -780,11 +780,12 @@ VkResult create_frame_context(VkDevice device, VmaAllocator allocator, VkCommand frame->image = create_semaphore(device); frame->render = create_semaphore(device); - frame->transfer = create_semaphore(device); + frame->transfer = create_timeline_semaphore(device); frame->frame = create_timeline_semaphore(device); frame->compute = create_timeline_semaphore(device); frame->frame_index = 0; + frame->transfer_index = 0; frame->compute_index = 0; VkCommandBufferAllocateInfo command_info = {