diff --git a/client/include/pipeline.h b/client/include/pipeline.h index 0d00bf7..f5743a8 100644 --- a/client/include/pipeline.h +++ b/client/include/pipeline.h @@ -42,11 +42,18 @@ struct RectBuffer { VmaAllocation index_memory; }; -VkResult create_ui_colored_rect_pipeline(VkDevice device, VkRenderPass render_pass, VkDescriptorSetLayout descriptor_layout, GraphicsPipeline* pipeline); - - -VkResult create_ui_descriptor_set(VkDevice device, VmaAllocator allocator, VkExtent2D swapchain_extent, VkDescriptorSetLayout* ui_descriptor_layout, VkDescriptorPool* ui_descriptor_pool, VkDescriptorSet* ui_descriptor_set, VmaAllocation* ui_descriptor_memory, VkBuffer* ui_descriptor_buffer); - -VkResult create_ui_rect_buffer(VkDevice device, Queue transfer_queue, VkCommandPool transfer_pool, VmaAllocator allocator, struct RectBuffer* buf); +typedef struct UIContextStruct { + VkBuffer ui_descriptor_buffer; + VmaAllocation ui_descriptor_memory; + VkDescriptorSetLayout ui_descriptor_layout; + VkDescriptorPool ui_descriptor_pool; + VkDescriptorSet ui_descriptor_set; + + struct RectBuffer ui_rect; + GraphicsPipeline ui_pipeline_rect; + GraphicsPipeline ui_pipeline_text; +} UIContext; + +VkResult init_pipelines(VkDevice device, VmaAllocator allocator, VkExtent2D swapchain_extent, VkRenderPass render_pass, Queue transfer_queue, VkCommandPool transfer_pool, UIContext* context); #endif diff --git a/client/include/render.h b/client/include/render.h index 23223f6..fdad0f3 100644 --- a/client/include/render.h +++ b/client/include/render.h @@ -77,21 +77,11 @@ typedef struct RenderContextStruct { VkFence* in_flight_fences; - VkBuffer ui_descriptor_buffer; - VmaAllocation ui_descriptor_memory; - VkDescriptorSetLayout ui_descriptor_layout; - VkDescriptorPool ui_descriptor_pool; - VkDescriptorSet ui_descriptor_set; - - struct RectBuffer ui_rect; - GraphicsPipeline ui_pipeline_rect; - GraphicsPipeline ui_pipeline_text; - uint32_t current_frame; } RenderContext; GLFWwindow* init_window(); VkResult init_vulkan(GLFWwindow* window, RenderContext* context); -VkResult draw_frame(RenderContext* context, UILayer* ui_layers, uint32_t ui_layer_count); +VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_layers, uint32_t ui_layer_count); #endif diff --git a/client/src/main.c b/client/src/main.c index b81a2c1..1bf15c9 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -13,9 +13,16 @@ ColoredRect colored_rect(float width, float height, float r, float g, float b, f } VkResult render_thread(GLFWwindow* window, RenderContext* render_context) { + VkResult result; + UIContext ui_context; VkBuffer colored_rect_buffer; VmaAllocation colored_rect_memory; + result = init_pipelines(render_context->device, render_context->allocator, render_context->swapchain_extent, render_context->world_render_pass, render_context->transfer_queue, render_context->transfer_pool, &ui_context); + if(result != VK_SUCCESS) { + return result; + } + VkBufferCreateInfo colored_rect_buffer_info = { .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, @@ -27,7 +34,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) { .usage = VMA_MEMORY_USAGE_CPU_TO_GPU, }; - VkResult result = vmaCreateBuffer(render_context->allocator, &colored_rect_buffer_info, &colored_rect_memory_info, &colored_rect_buffer, &colored_rect_memory, NULL); + result = vmaCreateBuffer(render_context->allocator, &colored_rect_buffer_info, &colored_rect_memory_info, &colored_rect_buffer, &colored_rect_memory, NULL); if(result != VK_SUCCESS) { return result; } @@ -53,7 +60,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) { while(glfwWindowShouldClose(window) == 0) { glfwPollEvents(); - result = draw_frame(render_context, &test_layer, 1); + result = draw_frame(render_context, &ui_context, &test_layer, 1); if(result != VK_SUCCESS) { fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result)); return result; diff --git a/client/src/pipeline.c b/client/src/pipeline.c index b54d34b..2a651dd 100644 --- a/client/src/pipeline.c +++ b/client/src/pipeline.c @@ -4,6 +4,7 @@ #include "stdio.h" #include "stdlib.h" #include "string.h" +#include "vk_mem_alloc.h" #include "vulkan/vulkan_core.h" VkShaderModule load_shader_file(const char* path, VkDevice device) { @@ -533,3 +534,24 @@ VkResult create_ui_rect_buffer(VkDevice device, Queue transfer_queue, VkCommandP return VK_SUCCESS; } + +VkResult init_pipelines(VkDevice device, VmaAllocator allocator, VkExtent2D swapchain_extent, VkRenderPass render_pass, Queue transfer_queue, VkCommandPool transfer_pool, UIContext* context) { + + VkResult result; + result = create_ui_descriptor_set(device, allocator, swapchain_extent, &context->ui_descriptor_layout, &context->ui_descriptor_pool, &context->ui_descriptor_set, &context->ui_descriptor_memory, &context->ui_descriptor_buffer); + if(result != VK_SUCCESS) { + return result; + } + + result = create_ui_colored_rect_pipeline(device, render_pass, context->ui_descriptor_layout, &context->ui_pipeline_rect); + if(result != VK_SUCCESS) { + return result; + } + + result = create_ui_rect_buffer(device, transfer_queue, transfer_pool, allocator, &context->ui_rect); + if(result != VK_SUCCESS) { + return result; + } + + return VK_SUCCESS; +} diff --git a/client/src/render.c b/client/src/render.c index b77a48c..5306a6b 100644 --- a/client/src/render.c +++ b/client/src/render.c @@ -944,26 +944,11 @@ VkResult init_vulkan(GLFWwindow* window, RenderContext* context) { return result; } - result = create_ui_descriptor_set(context->device, context->allocator, context->swapchain_extent, &context->ui_descriptor_layout, &context->ui_descriptor_pool, &context->ui_descriptor_set, &context->ui_descriptor_memory, &context->ui_descriptor_buffer); - if(result != VK_SUCCESS) { - return result; - } - - result = create_ui_colored_rect_pipeline(context->device, context->world_render_pass, context->ui_descriptor_layout, &context->ui_pipeline_rect); - if(result != VK_SUCCESS) { - return result; - } - - result = create_ui_rect_buffer(context->device, context->transfer_queue, context->transfer_pool, context->allocator, &context->ui_rect); - if(result != VK_SUCCESS) { - return result; - } - context->current_frame = 0; return VK_SUCCESS; } -VkResult draw_frame(RenderContext* context, UILayer* ui_layers, uint32_t ui_layer_count) { +VkResult draw_frame(RenderContext* context, UIContext* ui_context, UILayer* ui_layers, uint32_t ui_layer_count) { VkResult result; result = vkWaitForFences(context->device, 1, &context->in_flight_fences[context->current_frame], VK_TRUE, UINT64_MAX); @@ -1025,10 +1010,10 @@ VkResult draw_frame(RenderContext* context, UILayer* ui_layers, uint32_t ui_laye vkCmdBeginRenderPass(context->swapchain_command_buffers[context->current_frame], &ui_render_pass_begin, VK_SUBPASS_CONTENTS_INLINE); // Draw UI colored rects //////////////////////////////// - vkCmdBindPipeline(context->swapchain_command_buffers[context->current_frame], VK_PIPELINE_BIND_POINT_GRAPHICS, context->ui_pipeline_rect.pipeline); - vkCmdBindDescriptorSets(context->swapchain_command_buffers[context->current_frame], VK_PIPELINE_BIND_POINT_GRAPHICS, context->ui_pipeline_rect.layout, 0, 1, &context->ui_descriptor_set, 0, NULL); - vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 0, 1, &context->ui_rect.vertex, &offset); - vkCmdBindIndexBuffer(context->swapchain_command_buffers[context->current_frame], context->ui_rect.index, 0, VK_INDEX_TYPE_UINT32); + vkCmdBindPipeline(context->swapchain_command_buffers[context->current_frame], VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->ui_pipeline_rect.pipeline); + vkCmdBindDescriptorSets(context->swapchain_command_buffers[context->current_frame], VK_PIPELINE_BIND_POINT_GRAPHICS, ui_context->ui_pipeline_rect.layout, 0, 1, &ui_context->ui_descriptor_set, 0, NULL); + vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 0, 1, &ui_context->ui_rect.vertex, &offset); + vkCmdBindIndexBuffer(context->swapchain_command_buffers[context->current_frame], ui_context->ui_rect.index, 0, VK_INDEX_TYPE_UINT32); for(uint32_t i = 0; i < ui_layer_count; i++) { vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 1, 1, &ui_layers[i].colored_rects, &offset);