|
| 1 | +# Device Buffers |
| 2 | + |
| 3 | +This guide will only use device buffers for vertex buffers, where both vertex and index data will be strung together in a single VBO. The create function can thus take the data and perform the buffer copy operation before returning. In essence this return value is a "GPU const" buffer. To enable utilizing separate spans for vertices and indices (instead of forcing allocation of a contiguous bytestream and copying the data), the create function takes a slightly awkward span of spans: |
| 4 | + |
| 5 | +```cpp |
| 6 | +// disparate byte spans. |
| 7 | +using ByteSpans = std::span<std::span<std::byte const> const>; |
| 8 | + |
| 9 | +// returns a Device Buffer with each byte span sequentially written. |
| 10 | +[[nodiscard]] auto create_device_buffer(BufferCreateInfo const& create_info, |
| 11 | + CommandBlock command_block, |
| 12 | + ByteSpans const& byte_spans) -> Buffer; |
| 13 | +``` |
| 14 | +
|
| 15 | +Implement `create_device_buffer()`: |
| 16 | +
|
| 17 | +```cpp |
| 18 | +auto vma::create_device_buffer(BufferCreateInfo const& create_info, |
| 19 | + CommandBlock command_block, |
| 20 | + ByteSpans const& byte_spans) -> Buffer { |
| 21 | + auto const total_size = std::accumulate( |
| 22 | + byte_spans.begin(), byte_spans.end(), 0uz, |
| 23 | + [](std::size_t const n, std::span<std::byte const> bytes) { |
| 24 | + return n + bytes.size(); |
| 25 | + }); |
| 26 | +
|
| 27 | + auto staging_ci = create_info; |
| 28 | + staging_ci.usage = vk::BufferUsageFlagBits::eTransferSrc; |
| 29 | +
|
| 30 | + // create staging Host Buffer with TransferSrc usage. |
| 31 | + auto staging_buffer = |
| 32 | + create_buffer(staging_ci, BufferMemoryType::Host, total_size); |
| 33 | + // create the Device Buffer. |
| 34 | + auto ret = create_buffer(create_info, BufferMemoryType::Device, total_size); |
| 35 | + // can't do anything if either buffer creation failed. |
| 36 | + if (!staging_buffer.get().buffer || !ret.get().buffer) { return {}; } |
| 37 | +
|
| 38 | + // copy byte spans into staging buffer. |
| 39 | + auto dst = staging_buffer.get().mapped_span(); |
| 40 | + for (auto const bytes : byte_spans) { |
| 41 | + std::memcpy(dst.data(), bytes.data(), bytes.size()); |
| 42 | + dst = dst.subspan(bytes.size()); |
| 43 | + } |
| 44 | +
|
| 45 | + // record buffer copy operation. |
| 46 | + auto buffer_copy = vk::BufferCopy2{}; |
| 47 | + buffer_copy.setSize(total_size); |
| 48 | + auto copy_buffer_info = vk::CopyBufferInfo2{}; |
| 49 | + copy_buffer_info.setSrcBuffer(staging_buffer.get().buffer) |
| 50 | + .setDstBuffer(ret.get().buffer) |
| 51 | + .setRegions(buffer_copy); |
| 52 | + command_block.command_buffer().copyBuffer2(copy_buffer_info); |
| 53 | +
|
| 54 | + // submit and wait. |
| 55 | + // waiting here is necessary to keep the staging buffer alive while the GPU |
| 56 | + // accesses it through the recorded commands. |
| 57 | + // this is also why the function takes ownership of the passed CommandBlock |
| 58 | + // instead of just referencing it / taking a vk::CommandBuffer. |
| 59 | + command_block.submit_and_wait(); |
| 60 | +
|
| 61 | + return ret; |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +Add a command block pool to `App`, and a helper function to create command blocks: |
| 66 | + |
| 67 | +```cpp |
| 68 | +void App::create_cmd_block_pool() { |
| 69 | + auto command_pool_ci = vk::CommandPoolCreateInfo{}; |
| 70 | + command_pool_ci |
| 71 | + .setQueueFamilyIndex(m_gpu.queue_family) |
| 72 | + // this flag indicates that the allocated Command Buffers will be |
| 73 | + // short-lived. |
| 74 | + .setFlags(vk::CommandPoolCreateFlagBits::eTransient); |
| 75 | + m_cmd_block_pool = m_device->createCommandPoolUnique(command_pool_ci); |
| 76 | +} |
| 77 | + |
| 78 | +auto App::create_command_block() const -> CommandBlock { |
| 79 | + return CommandBlock{*m_device, m_queue, *m_cmd_block_pool}; |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +Update `create_vertex_buffer()` to create a quad with indices: |
| 84 | + |
| 85 | +```cpp |
| 86 | +template <typename T> |
| 87 | +[[nodiscard]] constexpr auto to_byte_array(T const& t) { |
| 88 | + return std::bit_cast<std::array<std::byte, sizeof(T)>>(t); |
| 89 | +} |
| 90 | + |
| 91 | +// ... |
| 92 | +void App::create_vertex_buffer() { |
| 93 | + // vertices of a quad. |
| 94 | + static constexpr auto vertices_v = std::array{ |
| 95 | + Vertex{.position = {-0.5f, -0.5f}, .color = {1.0f, 0.0f, 0.0f}}, |
| 96 | + Vertex{.position = {0.5f, -0.5f}, .color = {0.0f, 1.0f, 0.0f}}, |
| 97 | + Vertex{.position = {0.5f, 0.5f}, .color = {0.0f, 0.0f, 1.0f}}, |
| 98 | + Vertex{.position = {-0.5f, 0.5f}, .color = {1.0f, 1.0f, 0.0f}}, |
| 99 | + }; |
| 100 | + static constexpr auto indices_v = std::array{ |
| 101 | + 0u, 1u, 2u, 2u, 3u, 0u, |
| 102 | + }; |
| 103 | + static constexpr auto vertices_bytes_v = to_byte_array(vertices_v); |
| 104 | + static constexpr auto indices_bytes_v = to_byte_array(indices_v); |
| 105 | + static constexpr auto total_bytes_v = |
| 106 | + std::array<std::span<std::byte const>, 2>{ |
| 107 | + vertices_bytes_v, |
| 108 | + indices_bytes_v, |
| 109 | + }; |
| 110 | + // we want to write total_bytes_v to a Device VertexBuffer | IndexBuffer. |
| 111 | + m_vbo = vma::create_device_buffer(m_allocator.get(), |
| 112 | + vk::BufferUsageFlagBits::eVertexBuffer | |
| 113 | + vk::BufferUsageFlagBits::eIndexBuffer, |
| 114 | + create_command_block(), total_bytes_v); |
| 115 | +} |
| 116 | +``` |
| 117 | +
|
| 118 | +Update `draw()`: |
| 119 | +
|
| 120 | +```cpp |
| 121 | +void App::draw(vk::CommandBuffer const command_buffer) const { |
| 122 | + m_shader->bind(command_buffer, m_framebuffer_size); |
| 123 | + // single VBO at binding 0 at no offset. |
| 124 | + command_buffer.bindVertexBuffers(0, m_vbo.get().buffer, vk::DeviceSize{}); |
| 125 | + // u32 indices after offset of 4 vertices. |
| 126 | + command_buffer.bindIndexBuffer(m_vbo.get().buffer, 4 * sizeof(Vertex), |
| 127 | + vk::IndexType::eUint32); |
| 128 | + // m_vbo has 6 indices. |
| 129 | + command_buffer.drawIndexed(6, 1, 0, 0, 0); |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | + |
0 commit comments