Moved test UI creation to happen in parallel with render thread

main
noah metz 2024-10-24 21:30:36 -06:00
parent 14a87c8269
commit d1cfff1ff7
4 changed files with 23 additions and 24 deletions

@ -1,7 +1,7 @@
#include "draw.h" #include "draw.h"
#include "gpu.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 = { UIPushConstant push = {
.time = (float)time, .time = (float)time,
.layer = 0, .layer = 0,
@ -18,7 +18,7 @@ void record_ui_compute(VkCommandBuffer command_buffer, UIContext* ui_context, do
.offset = offsetof(GPULayer, draw), .offset = offsetof(GPULayer, draw),
.size = sizeof(DrawCommand), .size = sizeof(DrawCommand),
.srcAccessMask = VK_ACCESS_INDIRECT_COMMAND_READ_BIT, .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); 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)); 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, .buffer = ui_context->containers[i].layers[j].layer,
.offset = offsetof(GPULayer, draw), .offset = offsetof(GPULayer, draw),
.size = sizeof(DrawCommand), .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, .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); 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, .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); vkCmdBeginRenderPass(command_buffer, &render_pass_begin, VK_SUBPASS_CONTENTS_INLINE);
// Render World // Render World

@ -1024,6 +1024,7 @@ VkResult create_storage_buffer(
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = size, .size = size,
.usage = usage | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, .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 = { VmaAllocationCreateInfo memory_info = {
@ -1090,13 +1091,23 @@ VkResult command_end_single(VkDevice device, VkCommandBuffer command_buffer, VkC
.pCommandBuffers = &command_buffer, .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) { if(result != VK_SUCCESS) {
vkFreeCommandBuffers(device, transfer_pool, 1, &command_buffer); vkFreeCommandBuffers(device, transfer_pool, 1, &command_buffer);
return result; return result;
} }
result = vkQueueWaitIdle(transfer_queue.handle); result = vkWaitForFences(device, 1, &submit_fence, VK_TRUE, UINT64_MAX);
vkFreeCommandBuffers(device, transfer_pool, 1, &command_buffer); vkFreeCommandBuffers(device, transfer_pool, 1, &command_buffer);
return result; return result;
} }

@ -197,8 +197,14 @@ void* network_thread(void* data) {
int main_thread(void* data) { int main_thread(void* data) {
ClientContext* context = (ClientContext*)data; ClientContext* context = (ClientContext*)data;
int x = 0;
while(glfwWindowShouldClose(context->window) == 0) { while(glfwWindowShouldClose(context->window) == 0) {
glfwPollEvents(); glfwPollEvents();
if(x == 1000) {
test_ui(&context->render, &context->ui);
} else {
x += 1;
}
} }
return 0; return 0;
@ -266,7 +272,6 @@ int main() {
glfwSetScrollCallback(context.window, scroll_callback); glfwSetScrollCallback(context.window, scroll_callback);
glfwSetCursorPosCallback(context.window, cursor_pos_callback); glfwSetCursorPosCallback(context.window, cursor_pos_callback);
int error; int error;
VkResult result; VkResult result;
VK_RESULT(init_vulkan(context.window, &context.render)); 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 // TODO: make # of fonts/textures/containers scaling, recreate GPU buffers as necessary
VK_RESULT(create_ui_context(10, 10, 10, &context.render, &context.ui)); 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 // Start threads
pthread_t render_thread_handle; pthread_t render_thread_handle;
pthread_t network_thread_handle; pthread_t network_thread_handle;

@ -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); 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)); 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); destroy_transfer_buffer(gpu->allocator, transfer, transfer_memory);
container->layers[index].address = buffer_address(gpu->device, container->layers[index].layer); 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, .image = context->texture_slots[*index].image,
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.subresourceRange = { .subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.levelCount = 1, .levelCount = 1,
@ -565,7 +562,6 @@ VkResult load_texture(
VK_RESULT(command_end_single(gpu->device, command_buffer, gpu->transfer_pool, gpu->transfer_queue)); 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); destroy_transfer_buffer(gpu->allocator, transfer, transfer_memory);
free(image_buffer); free(image_buffer);
free(png); free(png);
@ -780,8 +776,6 @@ VkResult load_font(
.image = context->fonts[index].image, .image = context->fonts[index].image,
.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, .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.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.subresourceRange.levelCount = 1, .subresourceRange.levelCount = 1,
.subresourceRange.layerCount = info.num_symbols, .subresourceRange.layerCount = info.num_symbols,
@ -802,8 +796,6 @@ VkResult load_font(
.image = context->fonts[index].image, .image = context->fonts[index].image,
.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_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.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.subresourceRange.levelCount = 1, .subresourceRange.levelCount = 1,
.subresourceRange.layerCount = info.num_symbols, .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); 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)); 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); destroy_transfer_buffer(gpu->allocator, transfer, transfer_memory);
VkImageViewCreateInfo view_info = { VkImageViewCreateInfo view_info = {