Cleaned up ui pipeline creation, need to make struct for ui polygons and figure out UVs

main
noah metz 2024-10-12 14:01:23 -06:00
parent e9d3a71a1d
commit 1811321528
5 changed files with 129 additions and 101 deletions

@ -21,6 +21,11 @@ struct UIUniform {
vec2 size; vec2 size;
}; };
VkResult create_ui_polygon_pipeline(VkDevice device, VkRenderPass render_pass, VkDescriptorSetLayout descriptor_layout, VmaAllocator allocator, VkCommandPool transfer_pool, Queue transfer_queue, VmaAllocation* polygon_buffer_memory, VkBuffer* polygon_buffer, GraphicsPipeline* pipeline); VkResult create_ui_polygon_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_polygon_buffer(VkDevice device, Queue transfer_queue, VkCommandPool transfer_pool, VmaAllocator allocator, VmaAllocation* polygon_buffer_memory, VkBuffer* polygon_buffer);
#endif #endif

@ -76,15 +76,14 @@ typedef struct RenderContextStruct {
VkFence* in_flight_fences; VkFence* in_flight_fences;
GraphicsPipeline ui_pipeline_polygon;
VkBuffer ui_polygon_buffer;
VmaAllocation ui_polygon_buffer_memory;
VkBuffer ui_descriptor_buffer; VkBuffer ui_descriptor_buffer;
VmaAllocation ui_descriptor_memory; VmaAllocation ui_descriptor_memory;
VkDescriptorSetLayout ui_descriptor_layout; VkDescriptorSetLayout ui_descriptor_layout;
VkDescriptorPool ui_descriptor_pool; VkDescriptorPool ui_descriptor_pool;
VkDescriptorSet ui_descriptor_set; VkDescriptorSet ui_descriptor_set;
GraphicsPipeline ui_pipeline_polygon;
GraphicsPipeline ui_pipeline_text; GraphicsPipeline ui_pipeline_text;
uint32_t current_frame; uint32_t current_frame;
@ -92,6 +91,6 @@ typedef struct RenderContextStruct {
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, VkBuffer ui_polygons, uint32_t polygon_count); VkResult draw_frame(RenderContext* context, VkBuffer ui_polygon_primitives, VkBuffer ui_polygons, uint32_t polygon_count);
#endif #endif

@ -1,4 +1,6 @@
#include "render.h" #include "render.h"
#include "pipeline.h"
#include "vk_mem_alloc.h"
#include "vulkan/vk_enum_string_helper.h" #include "vulkan/vk_enum_string_helper.h"
VkResult render_thread(GLFWwindow* window, RenderContext* render_context) { VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
@ -60,10 +62,17 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
vmaUnmapMemory(render_context->allocator, test_ui_memory); vmaUnmapMemory(render_context->allocator, test_ui_memory);
VmaAllocation ui_primitives_memory;
VkBuffer ui_primitives_buffer;
result = create_ui_polygon_buffer(render_context->device, render_context->transfer_queue, render_context->transfer_pool, render_context->allocator, &ui_primitives_memory, &ui_primitives_buffer);
if(result != VK_SUCCESS) {
return result;
}
while(glfwWindowShouldClose(window) == 0) { while(glfwWindowShouldClose(window) == 0) {
glfwPollEvents(); glfwPollEvents();
result = draw_frame(render_context, test_ui_buffer, 3); result = draw_frame(render_context, ui_primitives_buffer, test_ui_buffer, 3);
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;

@ -1,6 +1,7 @@
#include "pipeline.h" #include "pipeline.h"
#include "stdio.h" #include "stdio.h"
#include "stdlib.h" #include "stdlib.h"
#include "string.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) {
@ -45,7 +46,7 @@ VkShaderModule load_shader_file(const char* path, VkDevice device) {
return shader; return shader;
} }
VkResult create_ui_polygon_pipeline(VkDevice device, VkRenderPass render_pass, VkDescriptorSetLayout descriptor_layout, VmaAllocator allocator, VkCommandPool transfer_pool, Queue transfer_queue, VmaAllocation* polygon_buffer_memory, VkBuffer* polygon_buffer, GraphicsPipeline* pipeline) { VkResult create_ui_polygon_pipeline(VkDevice device, VkRenderPass render_pass, VkDescriptorSetLayout descriptor_layout, GraphicsPipeline* pipeline) {
VkShaderModule vert_shader = load_shader_file("shader_src/ui_polygon.vert.spv", device); VkShaderModule vert_shader = load_shader_file("shader_src/ui_polygon.vert.spv", device);
VkShaderModule frag_shader = load_shader_file("shader_src/ui_polygon.frag.spv", device); VkShaderModule frag_shader = load_shader_file("shader_src/ui_polygon.frag.spv", device);
VkPipelineShaderStageCreateInfo shader_stages[] = { VkPipelineShaderStageCreateInfo shader_stages[] = {
@ -103,7 +104,6 @@ VkResult create_ui_polygon_pipeline(VkDevice device, VkRenderPass render_pass, V
}, },
}; };
VkPipelineVertexInputStateCreateInfo input_info = { VkPipelineVertexInputStateCreateInfo input_info = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
.pVertexBindingDescriptions = bindings, .pVertexBindingDescriptions = bindings,
@ -254,6 +254,108 @@ VkResult create_ui_polygon_pipeline(VkDevice device, VkRenderPass render_pass, V
return result; return result;
} }
return VK_SUCCESS;
}
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) {
VkDescriptorSetLayoutBinding ui_descriptor_bindings[] = {
{
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
}
};
VkDescriptorSetLayoutCreateInfo ui_descriptor_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.pBindings = ui_descriptor_bindings,
.bindingCount = sizeof(ui_descriptor_bindings)/sizeof(VkDescriptorSetLayoutBinding),
};
VkResult result;
result = vkCreateDescriptorSetLayout(device, &ui_descriptor_info, NULL, ui_descriptor_layout);
if(result != VK_SUCCESS) {
return result;
}
VkDescriptorPoolSize pool_size = {
.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1,
};
VkDescriptorPoolCreateInfo ui_pool_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.pPoolSizes = &pool_size,
.poolSizeCount = 1,
.maxSets = 1,
};
result = vkCreateDescriptorPool(device, &ui_pool_info, NULL, ui_descriptor_pool);
if(result != VK_SUCCESS) {
return result;
}
VkDescriptorSetAllocateInfo ui_descriptor_allocate_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.pSetLayouts = ui_descriptor_layout,
.descriptorSetCount = 1,
.descriptorPool = *ui_descriptor_pool,
};
result = vkAllocateDescriptorSets(device, &ui_descriptor_allocate_info, ui_descriptor_set);
if(result != VK_SUCCESS) {
return result;
}
VkBufferCreateInfo ui_uniform_buffer_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
.size = sizeof(struct UIUniform),
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
VmaAllocationCreateInfo ui_uniform_memory_info = {
.usage = VMA_MEMORY_USAGE_CPU_TO_GPU,
};
result = vmaCreateBuffer(allocator, &ui_uniform_buffer_info, &ui_uniform_memory_info, ui_descriptor_buffer, ui_descriptor_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
void * mapped;
result = vmaMapMemory(allocator, *ui_descriptor_memory, &mapped);
if(result != VK_SUCCESS) {
return result;
}
struct UIUniform ui_uniform = {
.size = {swapchain_extent.width, swapchain_extent.height}
};
memcpy(mapped, &ui_uniform, sizeof(ui_uniform));
vmaUnmapMemory(allocator, *ui_descriptor_memory);
VkDescriptorBufferInfo ui_uniform_info = {
.offset = 0,
.range = sizeof(ui_uniform),
.buffer = *ui_descriptor_buffer,
};
VkWriteDescriptorSet ui_uniform_write = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = *ui_descriptor_set,
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1,
.pBufferInfo = &ui_uniform_info,
};
vkUpdateDescriptorSets(device, 1, &ui_uniform_write, 0, NULL);
return VK_SUCCESS;
}
VkResult create_ui_polygon_buffer(VkDevice device, Queue transfer_queue, VkCommandPool transfer_pool, VmaAllocator allocator, VmaAllocation* polygon_buffer_memory, VkBuffer* polygon_buffer) {
// Create and populate polygon buffer // Create and populate polygon buffer
uint32_t polygon_buffer_size = 3 * sizeof(vec2); uint32_t polygon_buffer_size = 3 * sizeof(vec2);
VkBufferCreateInfo temp_buffer_info = { VkBufferCreateInfo temp_buffer_info = {
@ -269,6 +371,8 @@ VkResult create_ui_polygon_pipeline(VkDevice device, VkRenderPass render_pass, V
VkBuffer temp_buffer; VkBuffer temp_buffer;
VmaAllocation temp_buffer_memory; VmaAllocation temp_buffer_memory;
VkResult result;
result = vmaCreateBuffer(allocator, &temp_buffer_info, &temp_allocation_info, &temp_buffer, &temp_buffer_memory, NULL); result = vmaCreateBuffer(allocator, &temp_buffer_info, &temp_allocation_info, &temp_buffer, &temp_buffer_memory, NULL);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return result; return result;

@ -939,101 +939,12 @@ VkResult init_vulkan(GLFWwindow* window, RenderContext* context) {
return result; return result;
} }
VkDescriptorSetLayoutBinding ui_descriptor_bindings[] = { 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);
{
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
}
};
VkDescriptorSetLayoutCreateInfo ui_descriptor_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.pBindings = ui_descriptor_bindings,
.bindingCount = sizeof(ui_descriptor_bindings)/sizeof(VkDescriptorSetLayoutBinding),
};
result = vkCreateDescriptorSetLayout(context->device, &ui_descriptor_info, NULL, &context->ui_descriptor_layout);
if(result != VK_SUCCESS) {
return result;
}
VkDescriptorPoolSize pool_size = {
.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1,
};
VkDescriptorPoolCreateInfo ui_pool_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.pPoolSizes = &pool_size,
.poolSizeCount = 1,
.maxSets = 1,
};
result = vkCreateDescriptorPool(context->device, &ui_pool_info, NULL, &context->ui_descriptor_pool);
if(result != VK_SUCCESS) {
return result;
}
VkDescriptorSetAllocateInfo ui_descriptor_allocate_info = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.pSetLayouts = &context->ui_descriptor_layout,
.descriptorSetCount = 1,
.descriptorPool = context->ui_descriptor_pool,
};
result = vkAllocateDescriptorSets(context->device, &ui_descriptor_allocate_info, &context->ui_descriptor_set);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return result; return result;
} }
VkBufferCreateInfo ui_uniform_buffer_info = { result = create_ui_polygon_pipeline(context->device, context->render_pass, context->ui_descriptor_layout, &context->ui_pipeline_polygon);
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
.size = sizeof(struct UIUniform),
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
};
VmaAllocationCreateInfo ui_uniform_memory_info = {
.usage = VMA_MEMORY_USAGE_CPU_TO_GPU,
};
result = vmaCreateBuffer(context->allocator, &ui_uniform_buffer_info, &ui_uniform_memory_info, &context->ui_descriptor_buffer, &context->ui_descriptor_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
void * mapped;
result = vmaMapMemory(context->allocator, context->ui_descriptor_memory, &mapped);
if(result != VK_SUCCESS) {
return result;
}
struct UIUniform ui_uniform = {
.size = {context->swapchain_extent.width, context->swapchain_extent.height}
};
memcpy(mapped, &ui_uniform, sizeof(ui_uniform));
vmaUnmapMemory(context->allocator, context->ui_descriptor_memory);
VkDescriptorBufferInfo ui_uniform_info = {
.offset = 0,
.range = sizeof(ui_uniform),
.buffer = context->ui_descriptor_buffer,
};
VkWriteDescriptorSet ui_uniform_write = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = context->ui_descriptor_set,
.dstBinding = 0,
.dstArrayElement = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1,
.pBufferInfo = &ui_uniform_info,
};
vkUpdateDescriptorSets(context->device, 1, &ui_uniform_write, 0, NULL);
result = create_ui_polygon_pipeline(context->device, context->render_pass, context->ui_descriptor_layout, context->allocator, context->transfer_pool, context->transfer_queue, &context->ui_polygon_buffer_memory, &context->ui_polygon_buffer, &context->ui_pipeline_polygon);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return result; return result;
} }
@ -1042,7 +953,7 @@ VkResult init_vulkan(GLFWwindow* window, RenderContext* context) {
return VK_SUCCESS; return VK_SUCCESS;
} }
VkResult draw_frame(RenderContext* context, VkBuffer ui_polygons, uint32_t ui_polygon_count) { VkResult draw_frame(RenderContext* context, VkBuffer ui_polygon_primitives, VkBuffer ui_polygons, uint32_t ui_polygon_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);
@ -1091,7 +1002,7 @@ VkResult draw_frame(RenderContext* context, VkBuffer ui_polygons, uint32_t ui_po
// Draw UI polygons // Draw UI polygons
vkCmdBindPipeline(context->swapchain_command_buffers[context->current_frame], VK_PIPELINE_BIND_POINT_GRAPHICS, context->ui_pipeline_polygon.pipeline); vkCmdBindPipeline(context->swapchain_command_buffers[context->current_frame], VK_PIPELINE_BIND_POINT_GRAPHICS, context->ui_pipeline_polygon.pipeline);
VkDeviceSize offset = 0; VkDeviceSize offset = 0;
vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 0, 1, &context->ui_polygon_buffer, &offset); vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 0, 1, &ui_polygon_primitives, &offset);
VkDeviceSize offset2 = 0; VkDeviceSize offset2 = 0;
vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 1, 1, &ui_polygons, &offset2); vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 1, 1, &ui_polygons, &offset2);
vkCmdDraw(context->swapchain_command_buffers[context->current_frame], 3, ui_polygon_count, 0, 0); vkCmdDraw(context->swapchain_command_buffers[context->current_frame], 3, ui_polygon_count, 0, 0);