From d1dbc679d802194c2973d0acc82dae17c6e1f569 Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Sat, 20 Nov 2021 17:07:52 +0200 Subject: [PATCH] first attempt to add support for geom but I don't like much the design vertex descriptions shouldn't be like that. I don't like the current create_vertex_descriptions, I need to dynamically allocate as many as I need to fit vertices, indices, normals and tangents. I am also not sure if geometry should store all the vertices or just information about which ones are used. The data will be send later. When I create the renderer, the pipelines etc I only need the *description*. So to be fixed. --- src/main.cc | 5 +-- src/vk.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++----------- src/vk.h | 43 +++++++++++++++++++------ 3 files changed, 118 insertions(+), 30 deletions(-) diff --git a/src/main.cc b/src/main.cc index d642aab..bcbc2ac 100644 --- a/src/main.cc +++ b/src/main.cc @@ -347,8 +347,9 @@ display() } while (err != VK_SUCCESS); /* FIXME update buffer data */ - if (!vk_record_cmd_buffer(&vk_core, vk_cmd_buffers[0], - &vk_rnd, 0, + if (!vk_record_cmd_buffer(&vk_core, + vk_cmd_buffers[0], + &vk_rnd, 4, vk_fb_color, vk_framebuffers[vk_current_image], 2, 0, diff --git a/src/vk.c b/src/vk.c index 56d4c6e..d6ce1f0 100644 --- a/src/vk.c +++ b/src/vk.c @@ -596,6 +596,63 @@ create_shader_module(struct vk_ctx *ctx, return sm; } +static void +create_vertex_descriptions(struct vk_ctx *ctx, + struct vk_geometry *geometry, + int num_bind_dsc, + VkVertexInputBindingDescription *bind_dsc, + int num_att_dsc, + VkVertexInputAttributeDescription *att_dsc) +{ + VkFormat format; + VkFormatProperties fmt_props; + uint32_t stride; + + assert(num_bind_dsc == num_att_dsc); + + memset(bind_dsc, 0, num_bind_dsc * sizeof bind_dsc[0]); + memset(att_dsc, 0, num_att_dsc * sizeof att_dsc[0]); + + /* FIXME!!! format and stride!! + * We have 3D Vectors so we need an RGB format: + * R for x, G for y, B for z + * If we need to set the w we need an RGBA format! + * the stride (distance between 2 consecutive elements + * must be 8 as we use 32bits floats and 32bits = 8bytes + */ + format = VK_FORMAT_R32G32B32_SFLOAT; + vkGetPhysicalDeviceFormatProperties(ctx->pdev, format, &fmt_props); + assert(fmt_props.bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT); + stride = 8; + + /* Assumming that: + * 0 = vertices + * 1 = indices + * 2 = normals + * 3 = tangets + */ + + if (geometry->vertices.num_vertices) { + att_dsc[0].location = VERTEX_LOC; + att_dsc[0].binding = VERTEX_BIND; + att_dsc[0].format = format; + att_dsc[0].offset = 0; + + bind_dsc[0].binding = VERTEX_BIND; + bind_dsc[0].stride = stride; + bind_dsc[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX; + } + + if (geometry->indices.num_vertices) { + } + + if (geometry->normals.num_vertices) { + } + + if (geometry->tangents.num_vertices) { + } +} + static bool create_graphics_pipeline(struct vk_ctx *ctx, uint32_t width, @@ -606,8 +663,8 @@ create_graphics_pipeline(struct vk_ctx *ctx, bool enable_stencil, struct vk_renderer *renderer) { - VkVertexInputBindingDescription vert_bind_dsc[1]; - VkVertexInputAttributeDescription vert_att_dsc[1]; + VkVertexInputBindingDescription vert_bind_dsc[4]; + VkVertexInputAttributeDescription vert_att_dsc[4]; VkPipelineColorBlendAttachmentState *cb_att_state; VkPipelineVertexInputStateCreateInfo vert_input_info; @@ -630,6 +687,7 @@ create_graphics_pipeline(struct vk_ctx *ctx, VkPipelineLayout pipeline_layout; uint32_t stride; + /* FIXME */ /* format of vertex attributes: * we have 2D vectors so we need a RG format: * R for x, G for y @@ -641,35 +699,37 @@ create_graphics_pipeline(struct vk_ctx *ctx, assert(fmt_props.bufferFeatures & VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT); stride = 8; + // create_vertex_descriptions(ctx, renderer->geometry, 1, vert_bind_dsc, 1, vert_att_dsc); + memset(vert_att_dsc, 0, 4 * sizeof vert_att_dsc[0]); + memset(vert_bind_dsc, 0, 4 * sizeof vert_bind_dsc[0]); + /* VkVertexInputAttributeDescription */ - memset(&vert_att_dsc, 0, sizeof vert_att_dsc); vert_att_dsc[0].location = 0; vert_att_dsc[0].binding = 0; vert_att_dsc[0].format = format; /* see comment */ vert_att_dsc[0].offset = 0; /* VkVertexInputBindingDescription */ - memset(&vert_bind_dsc, 0, sizeof vert_bind_dsc); vert_bind_dsc[0].binding = 0; vert_bind_dsc[0].stride = stride; vert_bind_dsc[0].inputRate = VK_VERTEX_INPUT_RATE_VERTEX; /* If using vbo, we have setup vertex_info in the renderer. */ - bool use_vbo = renderer->vertex_info.num_verts > 0; + bool has_geometry = renderer->geometry; /* VkPipelineVertexInputStateCreateInfo */ memset(&vert_input_info, 0, sizeof vert_input_info); vert_input_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; - vert_input_info.vertexBindingDescriptionCount = use_vbo ? 1 : 0; + vert_input_info.vertexBindingDescriptionCount = renderer->geometry ? 1 : 0; vert_input_info.pVertexBindingDescriptions = vert_bind_dsc; - vert_input_info.vertexAttributeDescriptionCount = use_vbo ? 1 : 0; + vert_input_info.vertexAttributeDescriptionCount = renderer->geometry ? 1 : 0; vert_input_info.pVertexAttributeDescriptions = vert_att_dsc; /* VkPipelineInputAssemblyStateCreateInfo */ memset(&asm_info, 0, sizeof asm_info); asm_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; - asm_info.topology = renderer->vertex_info.topology ? - renderer->vertex_info.topology + asm_info.topology = has_geometry ? + renderer->geometry->topo : VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP; asm_info.primitiveRestartEnable = false; @@ -1547,7 +1607,7 @@ vk_create_renderer(struct vk_ctx *ctx, int num_color_att, struct vk_att_props *color_props, struct vk_att_props *depth_props, - struct vk_vertex_info *vert_info, + struct vk_geometry *geometry, struct vk_renderer *renderer) { /* create image views for each attachment */ @@ -1573,9 +1633,8 @@ vk_create_renderer(struct vk_ctx *ctx, goto fail; renderer->fs = fs; - if (vert_info) - renderer->vertex_info = *vert_info; - + if (geometry) + renderer->geometry = geometry; /* FIXME this is only for graphics atm */ if(!create_graphics_pipeline(ctx, w, h, @@ -1779,7 +1838,6 @@ bool vk_record_cmd_buffer(struct vk_ctx *ctx, VkCommandBuffer cmd_buf, struct vk_renderer *renderer, - struct vk_buf *vbo, uint32_t vk_fb_color_count, float *vk_fb_color, VkFramebuffer fb, @@ -1793,9 +1851,10 @@ vk_record_cmd_buffer(struct vk_ctx *ctx, VkRect2D rp_area; VkClearValue *clear_values; int i; VkDeviceSize offsets[] = {0}; - int num_vertices; + int num_vertices = 0; struct vk_dims img_size; bool create_cmd_buf = false; + bool has_geometry = renderer->geometry != 0; assert(vk_fb_color_count == 4); @@ -1862,14 +1921,19 @@ vk_record_cmd_buffer(struct vk_ctx *ctx, 0, sizeof (struct vk_dims), &img_size); - if (vbo) { - vkCmdBindVertexBuffers(cmd_buf, 0, 1, &vbo->buf, offsets); + if (has_geometry && renderer->geometry->vertices.num_vertices > 0) { + num_vertices = renderer->geometry->vertices.num_vertices; + vkCmdBindVertexBuffers(cmd_buf, 0, 1, &renderer->geometry->vertices.vbo, offsets); } vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, renderer->pipeline); - num_vertices = vbo ? renderer->vertex_info.num_verts : 4; + /* FIXME: temporal hack because I hard code 4 vertices in the + * vertex shader until I properly support VBOs */ + if (!num_vertices) + num_vertices = 4; + vkCmdDraw(cmd_buf, num_vertices, 1, 0, 0); vkCmdEndRenderPass(cmd_buf); diff --git a/src/vk.h b/src/vk.h index 58a88ef..772f2e3 100644 --- a/src/vk.h +++ b/src/vk.h @@ -6,6 +6,21 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) +enum GEOM_BIND { + VERTEX_BIND, + NORMAL_BIND, + TEXCOORD_BIND, + TANGENT_BIND, + IDX_BIND +}; + +enum GEOM_LOC { + VERTEX_LOC, + NORMAL_LOC, + TANGENT_LOC, + IDX_LOC +}; + struct vk_ctx { VkInstance inst; @@ -82,18 +97,27 @@ struct vk_attachment { struct vk_att_props props; }; -struct vk_vertex_info +struct vk_buf { - int num_verts; - int num_components; + VkBuffer buf; + struct vk_mem_obj mobj; +}; + +struct vk_vbo { + int num_vertices; + struct vk_buf vbo; - VkPrimitiveTopology topology; + /* int num_components; */ }; -struct vk_buf +struct vk_geometry { - VkBuffer buf; - struct vk_mem_obj mobj; + struct vk_vbo vertices; + struct vk_vbo normals; + struct vk_vbo tangents; + struct vk_vbo indices; + + VkPrimitiveTopology topo; }; struct vk_renderer @@ -104,7 +128,7 @@ struct vk_renderer VkShaderModule vs; VkShaderModule fs; - struct vk_vertex_info vertex_info; + struct vk_geometry *geometry; }; struct vk_dims @@ -251,7 +275,7 @@ vk_create_renderer(struct vk_ctx *ctx, int num_color_att, struct vk_att_props *color_props, struct vk_att_props *depth_props, - struct vk_vertex_info *vert_info, + struct vk_geometry *geometry, struct vk_renderer *renderer); void @@ -270,7 +294,6 @@ bool vk_record_cmd_buffer(struct vk_ctx *ctx, VkCommandBuffer cmd_buf, struct vk_renderer *renderer, - struct vk_buf *vbo, uint32_t vk_fb_color_count, float *vk_fb_color, VkFramebuffer fb, -- 1.7.10.4