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