sandbox
Loading...
Searching...
No Matches
gizmos_subrenderer.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_GIZMOS_MESH_SUBRENDERER_HPP_
3#define LIBSBX_GIZMOS_MESH_SUBRENDERER_HPP_
4
5#include <filesystem>
6#include <unordered_set>
7#include <ranges>
8#include <algorithm>
9
10#include <libsbx/utility/logger.hpp>
11
12#include <libsbx/math/color.hpp>
13#include <libsbx/math/vector3.hpp>
14#include <libsbx/math/matrix4x4.hpp>
15
16#include <libsbx/graphics/graphics_module.hpp>
17#include <libsbx/graphics/subrenderer.hpp>
18#include <libsbx/graphics/pipeline/pipeline.hpp>
19#include <libsbx/graphics/pipeline/graphics_pipeline.hpp>
20#include <libsbx/graphics/descriptor/descriptor_handler.hpp>
21#include <libsbx/graphics/buffers/uniform_handler.hpp>
22#include <libsbx/graphics/buffers/storage_handler.hpp>
23#include <libsbx/graphics/images/image2d.hpp>
24#include <libsbx/graphics/images/separate_image2d_array.hpp>
25#include <libsbx/graphics/images/sampler_state.hpp>
26
27#include <libsbx/assets/assets_module.hpp>
28
29#include <libsbx/scenes/scenes_module.hpp>
30#include <libsbx/scenes/scene.hpp>
31#include <libsbx/scenes/node.hpp>
32#include <libsbx/scenes/components/static_mesh.hpp>
33#include <libsbx/scenes/components/gizmo.hpp>
34#include <libsbx/scenes/components/id.hpp>
35#include <libsbx/scenes/components/camera.hpp>
36#include <libsbx/scenes/components/tag.hpp>
37#include <libsbx/scenes/components/point_light.hpp>
38
39#include <libsbx/models/mesh.hpp>
40
41#include <libsbx/gizmos/pipeline.hpp>
42
43namespace sbx::gizmos {
44
45// struct point_light {
46// math::color color;
47// math::vector3 position;
48// std::float_t intensity;
49// }; // struct point_light
50
52
53 inline static constexpr auto max_point_lights = std::size_t{16};
54
55public:
56
57 gizmos_subrenderer(const std::vector<graphics::attachment_description>& attachments, const std::filesystem::path& path, const std::string& depth_image)
59 _pipeline{path, attachments},
60 _depth_image{depth_image} { }
61
62 ~gizmos_subrenderer() override = default;
63
64 auto render(graphics::command_buffer& command_buffer) -> void override {
65 // auto& assets_module = core::engine::get_module<assets::assets_module>();
66 // auto& graphics_module = core::engine::get_module<graphics::graphics_module>();
67 // auto& devices_module = core::engine::get_module<devices::devices_module>();
68
69 // auto& scenes_module = core::engine::get_module<scenes::scenes_module>();
70 // auto& scene = scenes_module.active_scene();
71 // auto& environment = scene.environment();
72 // auto& graph = scene.graph();
73
74 // auto camera_node = environment.camera();
75
76 // auto& camera = graph.get_component<scenes::camera>(camera_node);
77
78 // _scene_uniform_handler.push("projection", camera.projection());
79
80 // const auto& camera_transform = graph.get_component<scenes::transform>(camera_node);
81
82 // _scene_uniform_handler.push("view", math::matrix4x4::inverted(camera_transform.local_transform()));
83
84 // auto& window = devices_module.window();
85
86 // _scene_uniform_handler.push("resolution", math::vector2{window.width(), window.height()});
87
88 // _scene_uniform_handler.push("camera_position", camera_transform.position());
89
90 // for (auto entry = _uniform_data.begin(); entry != _uniform_data.end();) {
91 // if (_used_uniforms.contains(entry->first)) {
92 // ++entry;
93 // } else {
94 // entry = _uniform_data.erase(entry);
95 // }
96 // }
97
98 // _used_uniforms.clear();
99 // _static_meshes.clear();
100
101 // auto gizmo_query = graph.query<scenes::gizmo>();
102
103 // for (const auto node : gizmo_query) {
104 // _submit_mesh(node);
105 // }
106
107 // _pipeline.bind(command_buffer);
108
109 // for (const auto& [key, data] : _static_meshes) {
110
111 // auto [entry, inserted] = _uniform_data.try_emplace(key, 1u);
112
113 // auto& descriptor_handler = entry->second.descriptor_handler;
114 // auto& storage_handler = entry->second.storage_handler;
115
116 // storage_handler.push(std::span<const per_mesh_data>{data});
117
118 // auto& mesh = assets_module.get_asset<models::mesh>(key.mesh_id);
119
120 // descriptor_handler.push("scene", _scene_uniform_handler);
121 // descriptor_handler.push("buffer_mesh_data", storage_handler);
122 // descriptor_handler.push("depth_image", graphics_module.attachment(_depth_image));
123 // descriptor_handler.push("texture_image", assets_module.get_asset<graphics::image2d>(key.texture_id));
124
125 // if (!descriptor_handler.update(_pipeline)) {
126 // continue;
127 // }
128
129 // descriptor_handler.bind_descriptors(command_buffer);
130
131 // mesh.bind(command_buffer);
132 // mesh.render_submesh(command_buffer, key.submesh_index, static_cast<std::uint32_t>(data.size()));
133 // }
134 }
135
136private:
137
138 auto _submit_mesh(const scenes::node node) -> void {
139 auto& scenes_module = core::engine::get_module<scenes::scenes_module>();
140 auto& scene = scenes_module.active_scene();
141 auto& environment = scene.environment();
142 auto& graph = scene.graph();
143
144 const auto& gizmo = graph.get_component<scenes::gizmo>(node);
145
146 const auto key = mesh_key{gizmo.mesh_id(), gizmo.submesh_index(), gizmo.texture_id()};
147
148 _used_uniforms.insert(key);
149
150 auto model = graph.world_transform(node);
151 auto normal = math::matrix4x4::transposed(math::matrix4x4::inverted(model));
152
153 _static_meshes[key].push_back(per_mesh_data{std::move(model), std::move(normal), gizmo.tint()});
154 }
155
156 struct uniform_data {
157 graphics::descriptor_handler descriptor_handler;
158 graphics::storage_handler storage_handler;
159 }; // struct uniform_data
160
161 struct mesh_key {
162 math::uuid mesh_id;
163 std::uint32_t submesh_index;
164 math::uuid texture_id;
165 }; // struct mesh_key
166
167 struct per_mesh_data {
168 alignas(16) math::matrix4x4 model;
169 alignas(16) math::matrix4x4 normal;
170 alignas(16) math::color tint;
171 }; // struct per_mesh_data
172
173 struct mesh_key_hash {
174 auto operator()(const mesh_key& key) const noexcept -> std::size_t {
175 auto seed = std::size_t{0};
176
177 utility::hash_combine(seed, key.mesh_id, key.submesh_index);
178
179 return seed;
180 }
181 }; // struct mesh_key_hash
182
183 struct mesh_key_equal {
184 auto operator()(const mesh_key& lhs, const mesh_key& rhs) const noexcept -> bool {
185 return lhs.mesh_id == rhs.mesh_id && lhs.submesh_index == rhs.submesh_index;
186 }
187 }; // struct mesh_key_equal
188
189 pipeline _pipeline;
190
191 std::string _depth_image;
192
193 std::unordered_map<mesh_key, uniform_data, mesh_key_hash, mesh_key_equal> _uniform_data;
194 std::unordered_set<mesh_key, mesh_key_hash, mesh_key_equal> _used_uniforms;
195
196 std::unordered_map<mesh_key, std::vector<per_mesh_data>, mesh_key_hash, mesh_key_equal> _static_meshes;
197
198 graphics::uniform_handler _scene_uniform_handler;
199
200}; // class gizmos_subrenderer
201
202} // namespace sbx::giszmos
203
204#endif // LIBSBX_GIZMOS_MESH_SUBRENDERER_HPP_
Definition: tests.cpp:6
Definition: gizmos_subrenderer.hpp:51
Definition: pipeline.hpp:11
Definition: command_buffer.hpp:15
Definition: descriptor_handler.hpp:26
Definition: storage_handler.hpp:17
Definition: subrenderer.hpp:14
Definition: uniform_handler.hpp:16
Definition: matrix4x4.hpp:26
Definition: uuid.hpp:160
RGBA color value type.
Definition: color.hpp:48
Definition: gizmo.hpp:10
RGBA color representation and utilities.