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#include <unordered_map>
8
9#include <libsbx/utility/hashed_string.hpp>
10#include <libsbx/utility/exception.hpp>
11
12#include <libsbx/math/volume.hpp>
13
14#include <libsbx/graphics/buffers/buffer.hpp>
15#include <libsbx/graphics/buffers/storage_buffer.hpp>
16
17#include <libsbx/graphics/commands/command_buffer.hpp>
18
19#include <libsbx/graphics/pipeline/vertex_input_description.hpp>
20
21#include <libsbx/graphics/resource_storage.hpp>
22
23namespace sbx::graphics {
24
25// template<typename Type>
26// using vertex_buffer = typed_buffer<Type, (VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT), VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT>;
27
28// template<typename Type>
29// using index_buffer = typed_buffer<Type, (VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT), VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT>;
30
31struct submesh {
32 std::uint32_t index_count;
33 std::uint32_t index_offset;
34 std::uint32_t vertex_offset;
35 math::volume bounds;
36 math::matrix4x4 local_transform;
38}; // struct submesh
39
40template<vertex Vertex>
41class mesh {
42
43public:
44
45 using vertex_type = Vertex;
46 // using vertex_buffer_type = vertex_buffer<Vertex>;
47
48 using index_type = std::uint32_t;
49 // using index_buffer_type = index_buffer<index_type>;
50
51 struct mesh_data {
52 std::vector<vertex_type> vertices;
53 std::vector<index_type> indices;
54 std::vector<graphics::submesh> submeshes;
55 // std::vector<math::volume> submesh_bounds;
56 // std::vector<math::matrix4x4> submesh_transforms;
57 // std::unordered_map<utility::hashed_string, std::uint32_t> submesh_names;
58 math::volume bounds;
59 }; // struct mesh_data
60
61 mesh(const std::vector<vertex_type>& vertices, const std::vector<index_type>& indices, const math::volume& bounds = math::volume{});
62
63 mesh(std::vector<vertex_type>&& vertices, std::vector<index_type>&& indices, const math::volume& bounds = math::volume{});
64
65 mesh(const mesh& other) noexcept = delete;
66
67 virtual ~mesh();
68
69 auto render(graphics::command_buffer& command_buffer, std::uint32_t instance_count = 1u) const -> void;
70
71 auto render_submesh(graphics::command_buffer& command_buffer, std::uint32_t submesh_index, std::uint32_t instance_count = 1u) const -> void;
72
73 auto address() const -> std::uint64_t;
74
75 auto bind(graphics::command_buffer& command_buffer) const -> void;
76
77 auto render_submesh_indirect(graphics::storage_buffer& buffer, std::uint32_t offset, std::uint32_t submesh_index, std::uint32_t instance_count = 1u) const -> void;
78
79 auto submeshes() const noexcept -> const std::vector<graphics::submesh>&;
80
81 auto submesh_index(const utility::hashed_string& name) const -> std::uint32_t {
82 const auto entry = std::ranges::find(_submeshes, name, &graphics::submesh::name);
83
84 if (entry == _submeshes.end()) {
85 throw utility::runtime_error{"Submesh '{}' not found", name.str()};
86 }
87
88 return std::distance(_submeshes.begin(), entry);
89 }
90
91 auto submesh(std::uint32_t submesh_index) const -> const graphics::submesh& {
92 return _submeshes.at(submesh_index);
93 }
94
95 auto submesh(const utility::hashed_string& name) const -> const graphics::submesh& {
96 return _submeshes.at(submesh_index(name));
97 }
98
99 auto submesh_bounds(std::uint32_t submesh_index) const -> const math::volume& {
100 return _submeshes.at(submesh_index).bounds;
101 }
102
103 auto submesh_bounds(const utility::hashed_string& name) const -> const math::volume& {
104 return _submeshes.at(submesh_index(name)).bounds;
105 }
106
107 auto submesh_local_transform(std::uint32_t submesh_index) const -> const math::matrix4x4& {
108 return _submeshes.at(submesh_index).local_transform;
109 }
110
111 auto submesh_local_transform(const utility::hashed_string& name) const -> const math::matrix4x4& {
112 return _submeshes.at(submesh_index(name)).local_transform;
113 }
114
115 auto submesh_names() const -> std::unordered_map<utility::hashed_string, std::uint32_t> {
116 auto names = std::unordered_map<utility::hashed_string, std::uint32_t>{};
117
118 for (std::size_t i = 0; i < _submeshes.size(); ++i) {
119 names.emplace(_submeshes[i].name, static_cast<std::uint32_t>(i));
120 }
121
122 return names;
123 }
124
125 auto bounds() const -> const math::volume& {
126 return _bounds;
127 }
128
129protected:
130
131 mesh(mesh_data&& mesh_data);
132
133 auto _upload_vertices(const std::vector<vertex_type>& vertices, const std::vector<index_type>& indices) -> void;
134
135 auto _upload_vertices(std::vector<vertex_type>&& vertices, std::vector<index_type>&& indices) -> void;
136
137 auto _calculate_bounds_from_submeshes(math::volume&& bounds) const -> math::volume;
138
139 // vertex_buffer_type _vertex_buffer;
140 // index_buffer_type _index_buffer;
141 buffer_handle _vertex_buffer;
142 buffer_handle _index_buffer;
143
144 std::vector<graphics::submesh> _submeshes;
145 // std::vector<math::volume> _submesh_bounds;
146 // std::vector<math::matrix4x4> _submesh_transforms;
147 // std::unordered_map<utility::hashed_string, std::uint32_t> _submesh_names;
148
149 math::volume _bounds;
150
151}; // class mesh
152
153} // namespace sbx::graphics
154
155#include <libsbx/graphics/pipeline/mesh.ipp>
156
157#endif // LIBSBX_GRAPHICS_PIPELINE_MESH_HPP_
Definition: command_buffer.hpp:14
Definition: mesh.hpp:41
Definition: matrix4x4.hpp:25
Definition: volume.hpp:11
Definition: hashed_string.hpp:15
Definition: mesh.hpp:51
Definition: mesh.hpp:31
Definition: exception.hpp:17