|
| 1 | +# Vertex Buffer |
| 2 | + |
| 3 | +The goal here is to move the hard-coded vertices in the shader to application code. For the time being we will use an ad-hoc host `vma::Buffer` and focus more on the rest of the infrastructure like vertex attributes. |
| 4 | + |
| 5 | +First add a new header, `vertex.hpp`: |
| 6 | + |
| 7 | +```cpp |
| 8 | +struct Vertex { |
| 9 | + glm::vec2 position{}; |
| 10 | + glm::vec3 color{}; |
| 11 | +}; |
| 12 | +``` |
| 13 | +
|
| 14 | +In `app.cpp`, store the vertex input: |
| 15 | +
|
| 16 | +```cpp |
| 17 | +// two vertex attributes: position at 0, color at 1. |
| 18 | +constexpr auto vertex_attributes_v = std::array{ |
| 19 | + // the format matches the type and layout of data: vec2 => 2x 32-bit floats. |
| 20 | + vk::VertexInputAttributeDescription2EXT{0, 0, vk::Format::eR32G32Sfloat, |
| 21 | + offsetof(Vertex, position)}, |
| 22 | + // vec3 => 3x 32-bit floats |
| 23 | + vk::VertexInputAttributeDescription2EXT{1, 0, vk::Format::eR32G32B32Sfloat, |
| 24 | + offsetof(Vertex, color)}, |
| 25 | +}; |
| 26 | +
|
| 27 | +// one vertex binding at location 0. |
| 28 | +constexpr auto vertex_bindings_v = std::array{ |
| 29 | + // we are using interleaved data with a stride of sizeof(Vertex). |
| 30 | + vk::VertexInputBindingDescription2EXT{0, sizeof(Vertex), |
| 31 | + vk::VertexInputRate::eVertex, 1}, |
| 32 | +}; |
| 33 | +
|
| 34 | +constexpr auto vertex_input_v = ShaderVertexInput{ |
| 35 | + .attributes = vertex_attributes_v, |
| 36 | + .bindings = vertex_bindings_v, |
| 37 | +}; |
| 38 | +``` |
| 39 | + |
| 40 | +Add `vertex_input_v` to the Shader Create Info: |
| 41 | + |
| 42 | +```cpp |
| 43 | +auto const shader_ci = ShaderProgram::CreateInfo{ |
| 44 | + // ... |
| 45 | + .vertex_input = vertex_input_v, |
| 46 | + .set_layouts = {}, |
| 47 | +}; |
| 48 | +``` |
| 49 | + |
| 50 | +With the vertex input defined, we can update the vertex shader and recompile it: |
| 51 | + |
| 52 | +```glsl |
| 53 | +#version 450 core |
| 54 | +
|
| 55 | +layout (location = 0) in vec2 a_pos; |
| 56 | +layout (location = 1) in vec3 a_color; |
| 57 | +
|
| 58 | +layout (location = 0) out vec3 out_color; |
| 59 | +
|
| 60 | +void main() { |
| 61 | + const vec2 position = a_pos; |
| 62 | +
|
| 63 | + out_color = a_color; |
| 64 | + gl_Position = vec4(position, 0.0, 1.0); |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +Add a VBO (Vertex Buffer Object) member and create it: |
| 69 | + |
| 70 | +```cpp |
| 71 | +void App::create_vertex_buffer() { |
| 72 | + // we want to write 3x Vertex objects to a Host VertexBuffer. |
| 73 | + auto const buffer_ci = vma::Buffer::CreateInfo{ |
| 74 | + .usage = vk::BufferUsageFlagBits::eVertexBuffer, |
| 75 | + .size = 3 * sizeof(Vertex), |
| 76 | + .type = vma::BufferType::Host, |
| 77 | + }; |
| 78 | + m_vbo.emplace(m_allocator.get(), buffer_ci); |
| 79 | + |
| 80 | + // vertices that were previously hard-coded in the shader. |
| 81 | + static constexpr auto vertices_v = std::array{ |
| 82 | + Vertex{.position = {-0.5f, -0.5f}, .color = {1.0f, 0.0f, 0.0f}}, |
| 83 | + Vertex{.position = {0.5f, -0.5f}, .color = {0.0f, 1.0f, 0.0f}}, |
| 84 | + Vertex{.position = {0.0f, 0.5f}, .color = {0.0f, 0.0f, 1.0f}}, |
| 85 | + }; |
| 86 | + // host buffers have a memory-mapped pointer available to memcpy data to. |
| 87 | + std::memcpy(m_vbo->get_raw().mapped, vertices_v.data(), sizeof(vertices_v)); |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +Bind the VBO before recording the draw call: |
| 92 | + |
| 93 | +```cpp |
| 94 | +// single VBO at binding 0 at no offset. |
| 95 | +command_buffer.bindVertexBuffers(0, m_vbo->get_raw().buffer, |
| 96 | + vk::DeviceSize{}); |
| 97 | +// m_vbo has 3 vertices. |
| 98 | +command_buffer.draw(3, 1, 0, 0); |
| 99 | +``` |
| 100 | + |
| 101 | +You should see the same triangle as before. But now we can use whatever set of vertices we like! To change it to a quad / rectangle, let's utilize an index buffer: this will reduce vertex duplication in general. |
| 102 | + |
| 103 | +```cpp |
| 104 | +void App::create_vertex_buffer() { |
| 105 | + // we want to write 4x Vertex objects to a Host VertexBuffer. |
| 106 | + auto buffer_ci = vma::Buffer::CreateInfo{ |
| 107 | + .usage = vk::BufferUsageFlagBits::eVertexBuffer, |
| 108 | + .size = 4 * sizeof(Vertex), |
| 109 | + .type = vma::BufferType::Host, |
| 110 | + }; |
| 111 | + m_vbo.emplace(m_allocator.get(), buffer_ci); |
| 112 | + |
| 113 | + // vertices that form a quad. |
| 114 | + static constexpr auto vertices_v = std::array{ |
| 115 | + Vertex{.position = {-0.5f, -0.5f}, .color = {1.0f, 0.0f, 0.0f}}, |
| 116 | + Vertex{.position = {0.5f, -0.5f}, .color = {0.0f, 1.0f, 0.0f}}, |
| 117 | + Vertex{.position = {0.5f, 0.5f}, .color = {0.0f, 0.0f, 1.0f}}, |
| 118 | + Vertex{.position = {-0.5f, 0.5f}, .color = {1.0f, 1.0f, 0.0f}}, |
| 119 | + }; |
| 120 | + // host buffers have a memory-mapped pointer available to memcpy data to. |
| 121 | + std::memcpy(m_vbo->get_raw().mapped, vertices_v.data(), sizeof(vertices_v)); |
| 122 | + |
| 123 | + // prepare to write 6x u32 indices to a Host IndexBuffer. |
| 124 | + buffer_ci = { |
| 125 | + .usage = vk::BufferUsageFlagBits::eIndexBuffer, |
| 126 | + .size = 6 * sizeof(std::uint32_t), |
| 127 | + .type = vma::BufferType::Host, |
| 128 | + }; |
| 129 | + m_ibo.emplace(m_allocator.get(), buffer_ci); |
| 130 | + static constexpr auto indices_v = std::array{ |
| 131 | + 0u, 1u, 2u, 2u, 3u, 0u, |
| 132 | + }; |
| 133 | + std::memcpy(m_ibo->get_raw().mapped, indices_v.data(), sizeof(indices_v)); |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +Bind the index buffer and use the `drawIndexed()` command: |
| 138 | + |
| 139 | +```cpp |
| 140 | +// single VBO at binding 0 at no offset. |
| 141 | +command_buffer.bindVertexBuffers(0, m_vbo->get_raw().buffer, |
| 142 | + vk::DeviceSize{}); |
| 143 | +// IBO with u32 indices at no offset. |
| 144 | +command_buffer.bindIndexBuffer(m_ibo->get_raw().buffer, 0, |
| 145 | + vk::IndexType::eUint32); |
| 146 | +// m_ibo has 6 indices. |
| 147 | +command_buffer.drawIndexed(6, 1, 0, 0, 0); |
| 148 | +``` |
0 commit comments