Updated ui polygon pipeline with structure to hold details(want to store all polygons in one buffer). Added screen matrix to ui polygon shader.

main
noah metz 2024-10-13 14:11:28 -06:00
parent 1811321528
commit 760b7bc389
6 changed files with 111 additions and 46 deletions

@ -17,8 +17,17 @@ struct UIElement {
vec4 color;
};
struct UIPolygon {
VkBuffer vertex_buffer;
VkBuffer index_buffer;
VkDeviceSize vertex_offset;
VkDeviceSize index_offset;
uint32_t vertices;
uint32_t indices;
};
struct UIUniform {
vec2 size;
mat4 screen;
};
VkResult create_ui_polygon_pipeline(VkDevice device, VkRenderPass render_pass, VkDescriptorSetLayout descriptor_layout, GraphicsPipeline* pipeline);
@ -26,6 +35,6 @@ VkResult create_ui_polygon_pipeline(VkDevice device, VkRenderPass render_pass, V
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);
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);
#endif

@ -91,6 +91,6 @@ typedef struct RenderContextStruct {
GLFWwindow* init_window();
VkResult init_vulkan(GLFWwindow* window, RenderContext* context);
VkResult draw_frame(RenderContext* context, VkBuffer ui_polygon_primitives, VkBuffer ui_polygons, uint32_t polygon_count);
VkResult draw_frame(RenderContext* context, struct UIPolygon polygon, VkBuffer ui_polygons, uint32_t polygon_count);
#endif

@ -2,8 +2,8 @@
#extension GL_EXT_buffer_reference : require
layout(set = 0, binding = 0) uniform UIUniform {
vec2 screen;
} scene;
mat4 screen;
} ubo;
layout(location = 0) in vec2 inVertexPosition;
layout(location = 1) in vec3 inPolygonPosition;
@ -13,7 +13,7 @@ layout(location = 3) in vec4 inColor;
layout(location = 0) out vec4 fragColor;
void main() {
gl_Position = vec4(vec3(inVertexPosition * inPolygonSize, 0.0) + inPolygonPosition, 1.0);
gl_Position = ubo.screen * vec4(vec3(inVertexPosition * inPolygonSize, 0.0) + inPolygonPosition, 1.0);
fragColor = inColor;
}

@ -30,8 +30,8 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
return result;
}
mapped[0].size[0] = 0.5;
mapped[0].size[1] = 0.5;
mapped[0].size[0] = 100;
mapped[0].size[1] = 100;
mapped[0].color[0] = 1.0;
mapped[0].color[1] = 0.0;
mapped[0].color[2] = 0.0;
@ -40,31 +40,37 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
mapped[0].pos[1] = 0.0;
mapped[0].pos[2] = 0.5;
mapped[1].size[0] = 0.5;
mapped[1].size[1] = 0.5;
mapped[1].size[0] = 100;
mapped[1].size[1] = 100;
mapped[1].color[0] = 0.0;
mapped[1].color[1] = 1.0;
mapped[1].color[2] = 0.0;
mapped[1].color[3] = 1.0;
mapped[1].pos[0] = 0.5;
mapped[1].pos[0] = 100;
mapped[1].pos[1] = 0.0;
mapped[1].pos[2] = 0.5;
mapped[2].size[0] = 0.5;
mapped[2].size[1] = 0.5;
mapped[2].size[0] = 100;
mapped[2].size[1] = 100;
mapped[2].color[0] = 0.0;
mapped[2].color[1] = 0.0;
mapped[2].color[2] = 1.0;
mapped[2].color[3] = 1.0;
mapped[2].pos[0] = 0.0;
mapped[2].pos[1] = 0.5;
mapped[2].pos[1] = 100;
mapped[2].pos[2] = 0.5;
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);
VmaAllocation ui_primitives_vertex_memory;
VmaAllocation ui_primitives_index_memory;
struct UIPolygon ui_square = {
.vertex_offset = 0,
.index_offset = 0,
.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) {
return result;
}
@ -72,7 +78,7 @@ VkResult render_thread(GLFWwindow* window, RenderContext* render_context) {
while(glfwWindowShouldClose(window) == 0) {
glfwPollEvents();
result = draw_frame(render_context, ui_primitives_buffer, test_ui_buffer, 3);
result = draw_frame(render_context, ui_square, test_ui_buffer, 3);
if(result != VK_SUCCESS) {
fprintf(stderr, "draw_frame error: %s\n", string_VkResult(result));
return result;

@ -1,4 +1,6 @@
#include "pipeline.h"
#include "cglm/affine.h"
#include "cglm/mat4.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
@ -329,9 +331,15 @@ VkResult create_ui_descriptor_set(VkDevice device, VmaAllocator allocator, VkExt
if(result != VK_SUCCESS) {
return result;
}
struct UIUniform ui_uniform = {
.size = {swapchain_extent.width, swapchain_extent.height}
};
struct UIUniform ui_uniform;
vec3 screen_offset = {-1.0, -1.0, 0.0};
vec3 screen_scale = {1.0/(float)swapchain_extent.width, 1.0/(float)swapchain_extent.height, 1.0};
glm_mat4_identity(ui_uniform.screen);
glm_translate(ui_uniform.screen, screen_offset);
glm_scale(ui_uniform.screen, screen_scale);
for(uint32_t i = 0; i < 4; i ++) {
fprintf(stderr, "%f %f %f %f\n", ui_uniform.screen[0][i], ui_uniform.screen[1][i], ui_uniform.screen[2][i], ui_uniform.screen[3][i]);
}
memcpy(mapped, &ui_uniform, sizeof(ui_uniform));
vmaUnmapMemory(allocator, *ui_descriptor_memory);
@ -355,14 +363,22 @@ VkResult create_ui_descriptor_set(VkDevice device, VmaAllocator allocator, VkExt
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
uint32_t polygon_buffer_size = 3 * sizeof(vec2);
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) {
// 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 index_buffer_size = 6 * sizeof(uint32_t);
// Create temp buffer
VkBufferCreateInfo temp_buffer_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.size = polygon_buffer_size
.size = vertex_buffer_size + index_buffer_size,
};
VmaAllocationCreateInfo temp_allocation_info = {
@ -378,44 +394,75 @@ VkResult create_ui_polygon_buffer(VkDevice device, Queue transfer_queue, VkComma
return result;
}
VkBufferCreateInfo buffer_info = {
// Create buffers
VmaAllocationCreateInfo allocation_info = {
.usage = VMA_MEMORY_USAGE_GPU_ONLY,
};
VkBufferCreateInfo vertex_buffer_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.size = polygon_buffer_size,
.size = vertex_buffer_size,
};
VmaAllocationCreateInfo allocation_info = {
.usage = VMA_MEMORY_USAGE_GPU_ONLY,
result = vmaCreateBuffer(allocator, &vertex_buffer_info, &allocation_info, &polygon->vertex_buffer, polygon_vertex_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
VkBufferCreateInfo index_buffer_info = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.size = index_buffer_size,
};
result = vmaCreateBuffer(allocator, &buffer_info, &allocation_info, polygon_buffer, polygon_buffer_memory, NULL);
result = vmaCreateBuffer(allocator, &index_buffer_info, &allocation_info, &polygon->index_buffer, polygon_index_memory, NULL);
if(result != VK_SUCCESS) {
return result;
}
vec2* mapped;
result = vmaMapMemory(allocator, temp_buffer_memory, (void**)&mapped);
void* mapped;
result = vmaMapMemory(allocator, temp_buffer_memory, &mapped);
if(result != VK_SUCCESS) {
return result;
}
mapped[0][0] = 0.0f;
mapped[0][1] = 0.0f;
mapped[1][0] = 1.0f;
mapped[1][1] = 0.0f;
mapped[2][0] = 0.0f;
mapped[2][1] = 1.0f;
vec2* mapped_vertex = (vec2*)mapped;
mapped_vertex[0][0] = 0.0f;
mapped_vertex[0][1] = 0.0f;
mapped_vertex[1][0] = 1.0f;
mapped_vertex[1][1] = 0.0f;
mapped_vertex[2][0] = 0.0f;
mapped_vertex[2][1] = 1.0f;
mapped_vertex[3][0] = 1.0f;
mapped_vertex[3][1] = 1.0f;
uint32_t* mapped_index = (uint32_t*)(mapped + vertex_buffer_size);
mapped_index[0] = 0;
mapped_index[1] = 1;
mapped_index[2] = 2;
mapped_index[3] = 1;
mapped_index[4] = 3;
mapped_index[5] = 2;
vmaUnmapMemory(allocator, temp_buffer_memory);
VkCommandBuffer copy_buffer = command_begin_single(device, transfer_pool);
VkBufferCopy copy_region = {
.size = polygon_buffer_size,
VkBufferCopy vertex_copy_region = {
.size = vertex_buffer_size,
.dstOffset = 0,
.srcOffset = 0,
};
vkCmdCopyBuffer(copy_buffer, temp_buffer, *polygon_buffer, 1, &copy_region);
vkCmdCopyBuffer(copy_buffer, temp_buffer, polygon->vertex_buffer, 1, &vertex_copy_region);
VkBufferCopy index_copy_region = {
.size = index_buffer_size,
.dstOffset = 0,
.srcOffset = vertex_buffer_size,
};
vkCmdCopyBuffer(copy_buffer, temp_buffer, polygon->index_buffer, 1, &index_copy_region);
result = command_end_single(device, copy_buffer, transfer_pool, transfer_queue);
if(result != VK_SUCCESS) {

@ -953,7 +953,7 @@ VkResult init_vulkan(GLFWwindow* window, RenderContext* context) {
return VK_SUCCESS;
}
VkResult draw_frame(RenderContext* context, VkBuffer ui_polygon_primitives, VkBuffer ui_polygons, uint32_t ui_polygon_count) {
VkResult draw_frame(RenderContext* context, struct UIPolygon polygon, VkBuffer ui_polygons, uint32_t ui_polygon_count) {
VkResult result;
result = vkWaitForFences(context->device, 1, &context->in_flight_fences[context->current_frame], VK_TRUE, UINT64_MAX);
@ -1001,11 +1001,14 @@ VkResult draw_frame(RenderContext* context, VkBuffer ui_polygon_primitives, VkBu
// Draw UI polygons
vkCmdBindPipeline(context->swapchain_command_buffers[context->current_frame], VK_PIPELINE_BIND_POINT_GRAPHICS, context->ui_pipeline_polygon.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);
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;
vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 0, 1, &ui_polygon_primitives, &offset);
VkDeviceSize offset2 = 0;
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);
vkCmdBindVertexBuffers(context->swapchain_command_buffers[context->current_frame], 1, 1, &ui_polygons, &offset);
vkCmdDrawIndexed(context->swapchain_command_buffers[context->current_frame], polygon.indices, ui_polygon_count, 0, 0, 0);
// End Draw UI polygons
vkCmdEndRenderPass(context->swapchain_command_buffers[context->current_frame]);