sandbox
Loading...
Searching...
No Matches
mesh.hpp
1#ifndef LIBSBX_GRAPHICS_PIPELINE_MESH_HPP_
2#define LIBSBX_GRAPHICS_PIPELINE_MESH_HPP_
3
4#include <memory>
5#include <vector>
6#include <string>
7
8#include <libsbx/graphics/graphics_module.hpp>
9
10#include <libsbx/graphics/buffers/buffer.hpp>
11
12#include <libsbx/graphics/pipeline/vertex_input_description.hpp>
13
14namespace sbx::graphics {
15
16template<typename Type>
17using vertex_buffer = basic_buffer<Type, (VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT), VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT>;
18
19template<typename Type>
20using index_buffer = basic_buffer<Type, (VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT), VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT>;
21
22struct submesh {
23 std::uint32_t index_count{};
24 std::uint32_t index_offset{};
25 std::uint32_t vertex_offset{};
26}; // struct submesh
27
28template<vertex Vertex>
29class mesh {
30
31public:
32
33 using vertex_type = Vertex;
35
36 using index_type = std::uint32_t;
38
39 mesh(std::vector<vertex_type>&& vertices, std::vector<index_type>&& indices) {
40 _submeshes.push_back(submesh{static_cast<std::uint32_t>(indices.size()), 0, 0});
41
42 _upload_vertices(std::move(vertices), std::move(indices));
43 }
44
45 virtual ~mesh() {
46
47 }
48
49 auto render(graphics::command_buffer& command_buffer, std::uint32_t instance_count = 1u) const -> void {
50 command_buffer.bind_vertex_buffer(0, *_vertex_buffer);
51 command_buffer.bind_index_buffer(*_index_buffer, 0, VK_INDEX_TYPE_UINT32);
52
53 command_buffer.draw_indexed(static_cast<std::uint32_t>(_index_buffer->size()), instance_count, 0, 0, 0);
54 }
55
56 auto render_submesh(graphics::command_buffer& command_buffer, std::uint32_t submesh_index, std::uint32_t instance_count = 1u) const -> void {
57 command_buffer.bind_vertex_buffer(0, *_vertex_buffer);
58 command_buffer.bind_index_buffer(*_index_buffer, 0, VK_INDEX_TYPE_UINT32);
59
60 const auto& submesh = _submeshes.at(submesh_index);
61
62 command_buffer.draw_indexed(submesh.index_count, instance_count, submesh.index_offset, 0, 0);
63 }
64
65 auto submeshes() const noexcept -> const std::vector<submesh>& {
66 return _submeshes;
67 }
68
69protected:
70
71 mesh()
72 : _vertex_buffer{nullptr},
73 _index_buffer{nullptr} { }
74
75 auto _upload_vertices(std::vector<vertex_type>&& vertices, std::vector<index_type>&& indices) -> void {
76 auto vertex_buffer_size = sizeof(vertex_type) * vertices.size();
77 auto index_buffer_size = sizeof(index_type) * indices.size();
78
79 auto staging_buffer_size = vertex_buffer_size + index_buffer_size;
80
81 // [NOTE] KAJ 2024-01-19 : We basically store two different types in here. The staging_buffer_size is in bytes so we use std::uint8_t.
82 auto staging_buffer = graphics::staging_buffer{staging_buffer_size};
83
84 staging_buffer.write(vertices.data(), vertex_buffer_size);
85 staging_buffer.write(indices.data(), index_buffer_size, vertex_buffer_size);
86
87 _vertex_buffer = std::make_unique<vertex_buffer_type>(vertices.size());
88 _index_buffer = std::make_unique<index_buffer_type>(indices.size());
89
90 auto command_buffer = graphics::command_buffer{true, VK_QUEUE_TRANSFER_BIT};
91
92 {
93 auto copy_region = VkBufferCopy{};
94 copy_region.size = vertex_buffer_size;
95 copy_region.dstOffset = 0;
96 copy_region.srcOffset = 0;
97
98 command_buffer.copy_buffer(staging_buffer, *_vertex_buffer, copy_region);
99 }
100
101 {
102 auto copy_region = VkBufferCopy{};
103 copy_region.size = index_buffer_size;
104 copy_region.dstOffset = 0;
105 copy_region.srcOffset = vertex_buffer_size;
106
107 command_buffer.copy_buffer(staging_buffer, *_index_buffer, copy_region);
108 }
109
110 command_buffer.submit_idle();
111 }
112
113 std::unique_ptr<vertex_buffer_type> _vertex_buffer{};
114 std::unique_ptr<index_buffer_type> _index_buffer{};
115 std::vector<submesh> _submeshes{};
116
117}; // class mesh
118
119} // namespace sbx::graphics
120
121#endif // LIBSBX_GRAPHICS_PIPELINE_MESH_HPP_
Definition: buffer.hpp:51
Definition: command_buffer.hpp:14
Definition: mesh.hpp:29
Definition: mesh.hpp:22