From d1cfff1ff7661ea7d018b52fab136bfbda0b082b Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Thu, 24 Oct 2024 21:30:36 -0600 Subject: [PATCH] Moved test UI creation to happen in parallel with render thread --- client/src/draw.c | 8 ++++---- client/src/gpu.c | 15 +++++++++++++-- client/src/main.c | 15 ++++++--------- client/src/ui.c | 9 --------- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/client/src/draw.c b/client/src/draw.c index baf4685..ce2d364 100644 --- a/client/src/draw.c +++ b/client/src/draw.c @@ -1,7 +1,7 @@ #include "draw.h" #include "gpu.h" -void record_ui_compute(VkCommandBuffer command_buffer, UIContext* ui_context, double time) { +void record_ui_compute(VkCommandBuffer command_buffer, RenderContext* gpu, UIContext* ui_context, double time) { UIPushConstant push = { .time = (float)time, .layer = 0, @@ -18,7 +18,7 @@ void record_ui_compute(VkCommandBuffer command_buffer, UIContext* ui_context, do .offset = offsetof(GPULayer, draw), .size = sizeof(DrawCommand), .srcAccessMask = VK_ACCESS_INDIRECT_COMMAND_READ_BIT, - .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, + .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_TRANSFER_READ_BIT, }; vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1, &draw_command_barrier_1, 0, NULL); command_copy_buffer(command_buffer, ui_context->containers[i].layers[j].layer, ui_context->containers[i].layers[j].layer, offsetof(GPULayer, num_drawables), offsetof(GPULayer, draw) + offsetof(DrawCommand, instance_count), sizeof(uint32_t)); @@ -27,7 +27,7 @@ void record_ui_compute(VkCommandBuffer command_buffer, UIContext* ui_context, do .buffer = ui_context->containers[i].layers[j].layer, .offset = offsetof(GPULayer, draw), .size = sizeof(DrawCommand), - .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, + .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT | VK_ACCESS_TRANSFER_READ_BIT, .dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT, }; vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 1, &draw_command_barrier_2, 0, NULL); @@ -134,7 +134,7 @@ VkResult draw_frame( .pClearValues = clear_values, }; - record_ui_compute(command_buffer, ui, time); + record_ui_compute(command_buffer, context, ui, time); vkCmdBeginRenderPass(command_buffer, &render_pass_begin, VK_SUBPASS_CONTENTS_INLINE); // Render World diff --git a/client/src/gpu.c b/client/src/gpu.c index 654ce29..f00a702 100644 --- a/client/src/gpu.c +++ b/client/src/gpu.c @@ -1024,6 +1024,7 @@ VkResult create_storage_buffer( .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = size, .usage = usage | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, + .sharingMode = VK_SHARING_MODE_EXCLUSIVE, }; VmaAllocationCreateInfo memory_info = { @@ -1090,13 +1091,23 @@ VkResult command_end_single(VkDevice device, VkCommandBuffer command_buffer, VkC .pCommandBuffers = &command_buffer, }; - result = vkQueueSubmit(transfer_queue.handle, 1, &submit_info, 0); + VkFenceCreateInfo fence_info = { + .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, + }; + VkFence submit_fence; + result = vkCreateFence(device, &fence_info, NULL, &submit_fence); + if(result != VK_SUCCESS) { + vkFreeCommandBuffers(device, transfer_pool, 1, &command_buffer); + return result; + } + + result = vkQueueSubmit(transfer_queue.handle, 1, &submit_info, submit_fence); if(result != VK_SUCCESS) { vkFreeCommandBuffers(device, transfer_pool, 1, &command_buffer); return result; } - result = vkQueueWaitIdle(transfer_queue.handle); + result = vkWaitForFences(device, 1, &submit_fence, VK_TRUE, UINT64_MAX); vkFreeCommandBuffers(device, transfer_pool, 1, &command_buffer); return result; } diff --git a/client/src/main.c b/client/src/main.c index 7edcd53..fd4e5c8 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -197,8 +197,14 @@ void* network_thread(void* data) { int main_thread(void* data) { ClientContext* context = (ClientContext*)data; + int x = 0; while(glfwWindowShouldClose(context->window) == 0) { glfwPollEvents(); + if(x == 1000) { + test_ui(&context->render, &context->ui); + } else { + x += 1; + } } return 0; @@ -266,7 +272,6 @@ int main() { glfwSetScrollCallback(context.window, scroll_callback); glfwSetCursorPosCallback(context.window, cursor_pos_callback); - int error; VkResult result; VK_RESULT(init_vulkan(context.window, &context.render)); @@ -274,14 +279,6 @@ int main() { // TODO: make # of fonts/textures/containers scaling, recreate GPU buffers as necessary VK_RESULT(create_ui_context(10, 10, 10, &context.render, &context.ui)); - ////////////////////////////////// - /// Test UI Code - ////////////////////////////////// - - VK_RESULT(test_ui(&context.render, &context.ui)); - - ////////////////////////////////// - // Start threads pthread_t render_thread_handle; pthread_t network_thread_handle; diff --git a/client/src/ui.c b/client/src/ui.c index d978eac..1d1e636 100644 --- a/client/src/ui.c +++ b/client/src/ui.c @@ -412,7 +412,6 @@ VkResult create_layer( command_copy_buffer(command_buffer, transfer, container->layers[index].codes, sizeof(GPULayer) + sizeof(GPUString)*input->num_strings + sizeof(GPUDrawable)*input->num_drawables, 0, sizeof(uint32_t)*input->num_codes); } VK_RESULT(command_end_single(gpu->device, command_buffer, gpu->transfer_pool, gpu->transfer_queue)); - vkQueueWaitIdle(gpu->transfer_queue.handle); destroy_transfer_buffer(gpu->allocator, transfer, transfer_memory); container->layers[index].address = buffer_address(gpu->device, container->layers[index].layer); @@ -523,8 +522,6 @@ VkResult load_texture( .image = context->texture_slots[*index].image, .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .subresourceRange = { .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .levelCount = 1, @@ -565,7 +562,6 @@ VkResult load_texture( VK_RESULT(command_end_single(gpu->device, command_buffer, gpu->transfer_pool, gpu->transfer_queue)); - vkQueueWaitIdle(gpu->transfer_queue.handle); destroy_transfer_buffer(gpu->allocator, transfer, transfer_memory); free(image_buffer); free(png); @@ -780,8 +776,6 @@ VkResult load_font( .image = context->fonts[index].image, .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .subresourceRange.levelCount = 1, .subresourceRange.layerCount = info.num_symbols, @@ -802,8 +796,6 @@ VkResult load_font( .image = context->fonts[index].image, .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, - .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .subresourceRange.levelCount = 1, .subresourceRange.layerCount = info.num_symbols, @@ -813,7 +805,6 @@ VkResult load_font( vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, &second_barrier); VK_RESULT(command_end_single(gpu->device, command_buffer, gpu->transfer_pool, gpu->transfer_queue)); - vkQueueWaitIdle(gpu->transfer_queue.handle); destroy_transfer_buffer(gpu->allocator, transfer, transfer_memory); VkImageViewCreateInfo view_info = {