Changed polygon pipeline to rect pipeline, need to add UVs to it now

main
noah metz 2024-10-13 14:38:17 -06:00
parent 241af49a80
commit 4c2bade162
5 changed files with 34 additions and 70 deletions

@ -11,30 +11,21 @@ typedef struct GraphicsPipelineStruct {
VkPipeline pipeline; VkPipeline pipeline;
} GraphicsPipeline; } GraphicsPipeline;
struct UIElement { struct UIRect {
vec3 pos; vec3 pos;
vec2 size; vec2 size;
vec4 color; vec4 color;
}; };
struct UIPolygon {
VkBuffer vertex_buffer;
VkBuffer index_buffer;
VkDeviceSize vertex_offset;
VkDeviceSize index_offset;
uint32_t vertices;
uint32_t indices;
};
struct UIUniform { struct UIUniform {
mat4 screen; mat4 screen;
}; };
VkResult create_ui_polygon_pipeline(VkDevice device, VkRenderPass render_pass, VkDescriptorSetLayout descriptor_layout, GraphicsPipeline* pipeline); VkResult create_ui_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_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, struct UIPolygon* polygon_buffer, VmaAllocation* polygon_vertex_memory, VmaAllocation* polygon_index_memory); VkResult create_ui_rect_buffer(VkDevice device, Queue transfer_queue, VkCommandPool transfer_pool, VmaAllocator allocator, VkBuffer* vertex_buffer, VkBuffer* index_buffer, VmaAllocation* vertex_memory, VmaAllocation* index_memory);
#endif #endif

@ -69,7 +69,6 @@ typedef struct RenderContextStruct {
VkRenderPass world_render_pass; VkRenderPass world_render_pass;
VkRenderPass ui_render_pass; VkRenderPass ui_render_pass;
VkRenderPass ui_objects_render_pass;
VkCommandBuffer* swapchain_command_buffers; VkCommandBuffer* swapchain_command_buffers;
@ -85,7 +84,7 @@ typedef struct RenderContextStruct {
VkDescriptorPool ui_descriptor_pool; VkDescriptorPool ui_descriptor_pool;
VkDescriptorSet ui_descriptor_set; VkDescriptorSet ui_descriptor_set;
GraphicsPipeline ui_pipeline_polygon; GraphicsPipeline ui_pipeline_rect;
GraphicsPipeline ui_pipeline_text; GraphicsPipeline ui_pipeline_text;
uint32_t current_frame; uint32_t current_frame;
@ -93,6 +92,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, struct UIPolygon polygon, VkBuffer ui_polygons, uint32_t polygon_count); VkResult draw_frame(RenderContext* context, VkBuffer ui_rect_vertex, VkBuffer ui_rect_index, VkBuffer ui_rects, uint32_t rect_count);
#endif #endif

@ -10,7 +10,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
VkBufferCreateInfo test_ui_buffer_info = { VkBufferCreateInfo test_ui_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,
.size = 3*sizeof(struct UIElement), .size = 3*sizeof(struct UIRect),
.sharingMode = VK_SHARING_MODE_EXCLUSIVE, .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
}; };
@ -23,7 +23,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
return result; return result;
} }
struct UIElement* mapped; struct UIRect* mapped;
result = vmaMapMemory(render_context->allocator, test_ui_memory, (void**)&mapped); result = vmaMapMemory(render_context->allocator, test_ui_memory, (void**)&mapped);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
@ -62,15 +62,11 @@ 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_vertex_memory; VmaAllocation rect_vertex_memory;
VmaAllocation ui_primitives_index_memory; VmaAllocation rect_index_memory;
struct UIPolygon ui_square = { VkBuffer rect_vertex;
.vertex_offset = 0, VkBuffer rect_index;
.index_offset = 0, result = create_ui_rect_buffer(render_context->device, render_context->transfer_queue, render_context->transfer_pool, render_context->allocator, &rect_vertex, &rect_index, &rect_vertex_memory, &rect_index_memory);
.vertices = 4,
.indices = 6,
};
result = create_ui_polygon_buffer(render_context->device, render_context->transfer_queue, render_context->transfer_pool, render_context->allocator, &ui_square, &ui_primitives_vertex_memory, &ui_primitives_index_memory);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return result; return result;
} }
@ -78,7 +74,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
while(glfwWindowShouldClose(window) == 0) { while(glfwWindowShouldClose(window) == 0) {
glfwPollEvents(); glfwPollEvents();
result = draw_frame(render_context, ui_square, test_ui_buffer, 3); result = draw_frame(render_context, rect_vertex, rect_index, 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;

@ -48,7 +48,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, GraphicsPipeline* pipeline) { VkResult create_ui_rect_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[] = {
@ -74,7 +74,7 @@ VkResult create_ui_polygon_pipeline(VkDevice device, VkRenderPass render_pass, V
}, },
{ {
.binding = 1, .binding = 1,
.stride = sizeof(struct UIElement), .stride = sizeof(struct UIRect),
.inputRate = VK_VERTEX_INPUT_RATE_INSTANCE, .inputRate = VK_VERTEX_INPUT_RATE_INSTANCE,
}, },
}; };
@ -90,19 +90,19 @@ VkResult create_ui_polygon_pipeline(VkDevice device, VkRenderPass render_pass, V
.binding = 1, .binding = 1,
.location = 1, .location = 1,
.format = VK_FORMAT_R32G32B32_SFLOAT, .format = VK_FORMAT_R32G32B32_SFLOAT,
.offset = offsetof(struct UIElement, pos), .offset = offsetof(struct UIRect, pos),
}, },
{ {
.binding = 1, .binding = 1,
.location = 2, .location = 2,
.format = VK_FORMAT_R32G32_SFLOAT, .format = VK_FORMAT_R32G32_SFLOAT,
.offset = offsetof(struct UIElement, size), .offset = offsetof(struct UIRect, size),
}, },
{ {
.binding = 1, .binding = 1,
.location = 3, .location = 3,
.format = VK_FORMAT_R32G32B32A32_SFLOAT, .format = VK_FORMAT_R32G32B32A32_SFLOAT,
.offset = offsetof(struct UIElement, color), .offset = offsetof(struct UIRect, color),
}, },
}; };
@ -360,13 +360,8 @@ VkResult create_ui_descriptor_set(VkDevice device, VmaAllocator allocator, VkExt
return VK_SUCCESS; return VK_SUCCESS;
} }
VkResult create_ui_polygon_buffer(VkDevice device, Queue transfer_queue, VkCommandPool transfer_pool, VmaAllocator allocator, struct UIPolygon* polygon, VmaAllocation* polygon_vertex_memory, VmaAllocation* polygon_index_memory) { VkResult create_ui_rect_buffer(VkDevice device, Queue transfer_queue, VkCommandPool transfer_pool, VmaAllocator allocator, VkBuffer* vertex_buffer, VkBuffer* index_buffer, VmaAllocation* vertex_memory, VmaAllocation* index_memory) {
// Create and populate polygon buffers
polygon->indices = 6;
polygon->vertices = 4;
polygon->vertex_offset = 0;
polygon->index_offset = 0;
uint32_t vertex_buffer_size = 4 * sizeof(vec2); uint32_t vertex_buffer_size = 4 * sizeof(vec2);
uint32_t index_buffer_size = 6 * sizeof(uint32_t); uint32_t index_buffer_size = 6 * sizeof(uint32_t);
@ -403,7 +398,7 @@ VkResult create_ui_polygon_buffer(VkDevice device, Queue transfer_queue, VkComma
.size = vertex_buffer_size, .size = vertex_buffer_size,
}; };
result = vmaCreateBuffer(allocator, &vertex_buffer_info, &allocation_info, &polygon->vertex_buffer, polygon_vertex_memory, NULL); result = vmaCreateBuffer(allocator, &vertex_buffer_info, &allocation_info, vertex_buffer, vertex_memory, NULL);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return result; return result;
} }
@ -415,7 +410,7 @@ VkResult create_ui_polygon_buffer(VkDevice device, Queue transfer_queue, VkComma
.size = index_buffer_size, .size = index_buffer_size,
}; };
result = vmaCreateBuffer(allocator, &index_buffer_info, &allocation_info, &polygon->index_buffer, polygon_index_memory, NULL); result = vmaCreateBuffer(allocator, &index_buffer_info, &allocation_info, index_buffer, index_memory, NULL);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return result; return result;
} }
@ -452,14 +447,14 @@ VkResult create_ui_polygon_buffer(VkDevice device, Queue transfer_queue, VkComma
.dstOffset = 0, .dstOffset = 0,
.srcOffset = 0, .srcOffset = 0,
}; };
vkCmdCopyBuffer(copy_buffer, temp_buffer, polygon->vertex_buffer, 1, &vertex_copy_region); vkCmdCopyBuffer(copy_buffer, temp_buffer, *vertex_buffer, 1, &vertex_copy_region);
VkBufferCopy index_copy_region = { VkBufferCopy index_copy_region = {
.size = index_buffer_size, .size = index_buffer_size,
.dstOffset = 0, .dstOffset = 0,
.srcOffset = vertex_buffer_size, .srcOffset = vertex_buffer_size,
}; };
vkCmdCopyBuffer(copy_buffer, temp_buffer, polygon->index_buffer, 1, &index_copy_region); vkCmdCopyBuffer(copy_buffer, temp_buffer, *index_buffer, 1, &index_copy_region);
result = command_end_single(device, copy_buffer, transfer_pool, transfer_queue); result = command_end_single(device, copy_buffer, transfer_pool, transfer_queue);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {

@ -934,11 +934,6 @@ VkResult init_vulkan(GLFWwindow* window, RenderContext* context) {
return result; return result;
} }
result = create_render_pass(context->device, context->swapchain_format, context->depth_format, &context->ui_objects_render_pass, VK_ATTACHMENT_LOAD_OP_LOAD);
if(result != VK_SUCCESS) {
return result;
}
result = create_depth_image(context->device, context->depth_format, context->swapchain_extent, context->allocator, context->extra_graphics_pool, context->graphics_queue, &context->depth_image, &context->depth_image_memory, &context->depth_image_view); result = create_depth_image(context->device, context->depth_format, context->swapchain_extent, context->allocator, context->extra_graphics_pool, context->graphics_queue, &context->depth_image, &context->depth_image_memory, &context->depth_image_view);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return result; return result;
@ -954,7 +949,7 @@ VkResult init_vulkan(GLFWwindow* window, RenderContext* context) {
return result; return result;
} }
result = create_ui_polygon_pipeline(context->device, context->world_render_pass, context->ui_descriptor_layout, &context->ui_pipeline_polygon); result = create_ui_rect_pipeline(context->device, context->world_render_pass, context->ui_descriptor_layout, &context->ui_pipeline_rect);
if(result != VK_SUCCESS) { if(result != VK_SUCCESS) {
return result; return result;
} }
@ -963,7 +958,7 @@ VkResult init_vulkan(GLFWwindow* window, RenderContext* context) {
return VK_SUCCESS; return VK_SUCCESS;
} }
VkResult draw_frame(RenderContext* context, struct UIPolygon polygon, VkBuffer ui_polygons, uint32_t ui_polygon_count) { VkResult draw_frame(RenderContext* context, VkBuffer ui_rect_vertex, VkBuffer ui_rect_index, VkBuffer ui_rects, uint32_t rect_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);
@ -1023,29 +1018,17 @@ VkResult draw_frame(RenderContext* context, struct UIPolygon polygon, VkBuffer u
}; };
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 polygons // Draw UI rects //////////////////////////////
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_rect.pipeline);
vkCmdBindDescriptorSets(context->swapchain_command_buffers[context->current_frame], VK_PIPELINE_BIND_POINT_GRAPHICS, context->ui_pipeline_polygon.layout, 0, 1, &context->ui_descriptor_set, 0, NULL); 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, &polygon.vertex_buffer, &polygon.vertex_offset);
vkCmdBindIndexBuffer(context->swapchain_command_buffers[context->current_frame], polygon.index_buffer, polygon.index_offset, VK_INDEX_TYPE_UINT32);
VkDeviceSize offset = 0; VkDeviceSize offset = 0;
vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 1, 1, &ui_polygons, &offset); vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 0, 1, &ui_rect_vertex, &offset);
vkCmdDrawIndexed(context->swapchain_command_buffers[context->current_frame], polygon.indices, ui_polygon_count, 0, 0, 0); vkCmdBindIndexBuffer(context->swapchain_command_buffers[context->current_frame], ui_rect_index, 0, VK_INDEX_TYPE_UINT32);
// End Draw UI polygons
vkCmdEndRenderPass(context->swapchain_command_buffers[context->current_frame]); vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 1, 1, &ui_rects, &offset);
vkCmdDrawIndexed(context->swapchain_command_buffers[context->current_frame], 6, rect_count, 0, 0, 0);
///////////////////////////////////////////////
// World Render Pass
VkRenderPassBeginInfo ui_objects_render_pass_begin = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
.renderPass = context->ui_objects_render_pass,
.framebuffer = context->swapchain_framebuffers[image_index],
.renderArea.offset = {0, 0},
.renderArea.extent = context->swapchain_extent,
.clearValueCount = 2,
.pClearValues = clear_values,
};
vkCmdBeginRenderPass(context->swapchain_command_buffers[context->current_frame], &ui_objects_render_pass_begin, VK_SUBPASS_CONTENTS_INLINE);
vkCmdEndRenderPass(context->swapchain_command_buffers[context->current_frame]); vkCmdEndRenderPass(context->swapchain_command_buffers[context->current_frame]);
result = vkEndCommandBuffer(context->swapchain_command_buffers[context->current_frame]); result = vkEndCommandBuffer(context->swapchain_command_buffers[context->current_frame]);