Moved some RenderContext items to new UIContext struct

main
noah metz 2024-10-13 20:27:09 -06:00
parent 4e7813499f
commit dc6e44ef99
5 changed files with 50 additions and 39 deletions

@ -42,11 +42,18 @@ struct RectBuffer {
VmaAllocation index_memory; VmaAllocation index_memory;
}; };
VkResult create_ui_colored_rect_pipeline(VkDevice device, VkRenderPass render_pass, VkDescriptorSetLayout descriptor_layout, GraphicsPipeline* pipeline); typedef struct UIContextStruct {
VkBuffer ui_descriptor_buffer;
VmaAllocation ui_descriptor_memory;
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); VkDescriptorSetLayout ui_descriptor_layout;
VkDescriptorPool ui_descriptor_pool;
VkResult create_ui_rect_buffer(VkDevice device, Queue transfer_queue, VkCommandPool transfer_pool, VmaAllocator allocator, struct RectBuffer* buf); 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 #endif

@ -77,21 +77,11 @@ typedef struct RenderContextStruct {
VkFence* in_flight_fences; 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; uint32_t current_frame;
} RenderContext; } RenderContext;
GLFWwindow* init_window(); GLFWwindow* init_window();
VkResult init_vulkan(GLFWwindow* window, RenderContext* context); 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 #endif

@ -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 render_thread(GLFWwindow* window, RenderContext* render_context) {
VkResult result;
UIContext ui_context;
VkBuffer colored_rect_buffer; VkBuffer colored_rect_buffer;
VmaAllocation colored_rect_memory; 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 = { VkBufferCreateInfo colored_rect_buffer_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, .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, .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) { if(result != VK_SUCCESS) {
return result; return result;
} }
@ -53,7 +60,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
while(glfwWindowShouldClose(window) == 0) { while(glfwWindowShouldClose(window) == 0) {
glfwPollEvents(); glfwPollEvents();
result = draw_frame(render_context, &test_layer, 1); result = draw_frame(render_context, &ui_context, &test_layer, 1);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result)); fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result));
return result; return result;

@ -4,6 +4,7 @@
#include "stdio.h" #include "stdio.h"
#include "stdlib.h" #include "stdlib.h"
#include "string.h" #include "string.h"
#include "vk_mem_alloc.h"
#include "vulkan/vulkan_core.h" #include "vulkan/vulkan_core.h"
VkShaderModule load_shader_file(const char* path, VkDevice device) { 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; 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;
}

@ -944,26 +944,11 @@ VkResult init_vulkan(GLFWwindow* window, RenderContext* context) {
return result; 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; context->current_frame = 0;
return VK_SUCCESS; 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; VkResult result;
result = vkWaitForFences(context->device, 1, &context->in_flight_fences[context->current_frame], VK_TRUE, UINT64_MAX); 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); vkCmdBeginRenderPass(context->swapchain_command_buffers[context->current_frame], &ui_render_pass_begin, VK_SUBPASS_CONTENTS_INLINE);
// Draw UI colored rects //////////////////////////////// // Draw UI colored rects ////////////////////////////////
vkCmdBindPipeline(context->swapchain_command_buffers[context->current_frame], VK_PIPELINE_BIND_POINT_GRAPHICS, context->ui_pipeline_rect.pipeline); 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, context->ui_pipeline_rect.layout, 0, 1, &context->ui_descriptor_set, 0, NULL); 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, &context->ui_rect.vertex, &offset); vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 0, 1, &ui_context->ui_rect.vertex, &offset);
vkCmdBindIndexBuffer(context->swapchain_command_buffers[context->current_frame], context->ui_rect.index, 0, VK_INDEX_TYPE_UINT32); 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++) { 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); vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 1, 1, &ui_layers[i].colored_rects, &offset);