sandbox
Loading...
Searching...
No Matches
debug_subrenderer.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_SCENES_DEBUG_SUBRENDERER_HPP_
3#define LIBSBX_SCENES_DEBUG_SUBRENDERER_HPP_
4
5#include <optional>
6
7#include <libsbx/math/vector4.hpp>
9
10#include <libsbx/graphics/subrenderer.hpp>
11#include <libsbx/graphics/render_graph.hpp>
12#include <libsbx/graphics/pipeline/graphics_pipeline.hpp>
13#include <libsbx/graphics/buffers/push_handler.hpp>
14#include <libsbx/graphics/buffers/storage_handler.hpp>
15#include <libsbx/graphics/descriptor/descriptor_handler.hpp>
16
17#include <libsbx/scenes/scenes_module.hpp>
18
19namespace sbx::scenes {
20
22
23 class pipeline : public sbx::graphics::graphics_pipeline {
24
25 inline static const auto pipeline_definition = sbx::graphics::pipeline_definition{
26 .depth = sbx::graphics::depth::disabled,
27 .uses_transparency = false,
28 .rasterization_state = sbx::graphics::rasterization_state{
29 .polygon_mode = sbx::graphics::polygon_mode::fill,
30 .cull_mode = sbx::graphics::cull_mode::none,
31 .front_face = sbx::graphics::front_face::counter_clockwise
32 },
33 .primitive_topology = sbx::graphics::primitive_topology::line_list
34 };
35
36 using base_type = sbx::graphics::graphics_pipeline;
37
38 public:
39
40 pipeline(const std::filesystem::path& path, const std::vector<graphics::attachment_description>& attachments)
41 : base_type{path, attachments, pipeline_definition} { }
42
43 ~pipeline() override = default;
44
45 }; // class pipeline
46
47public:
48
49 debug_subrenderer(const std::vector<graphics::attachment_description>& attachments, const std::filesystem::path& path)
51 _pipeline{path, attachments},
52 _push_handler{_pipeline},
53 _descriptor_handler{_pipeline, 0u} {
54 auto& graphics_module = core::engine::get_module<graphics::graphics_module>();
55
56 _storage_buffer = graphics_module.add_resource<graphics::storage_buffer>(graphics::storage_buffer::min_size, VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
57 }
58
59 ~debug_subrenderer() override {
60
61 }
62
63 auto render(sbx::graphics::command_buffer& command_buffer) -> void override {
64 auto& graphics_module = core::engine::get_module<graphics::graphics_module>();
65 auto& scenes_module = sbx::core::engine::get_module<sbx::scenes::scenes_module>();
66
67 const auto& lines = scenes_module.debug_lines();
68
69 if (lines.empty()) {
70 return;
71 }
72
73 auto& scene = scenes_module.scene();
74
75 auto camera_node = scene.camera();
76
77 auto& camera = scene.get_component<sbx::scenes::camera>(camera_node);
78
79 const auto& projection = camera.projection();
80
81 const auto view = sbx::math::matrix4x4::inverted(scene.world_transform(camera_node));
82
83 _pipeline.bind(command_buffer);
84
85 auto& storage_buffer = graphics_module.get_resource<graphics::storage_buffer>(_storage_buffer);
86
87 const auto required_size = static_cast<std::uint32_t>(lines.size() * sizeof(scenes_module::line));
88
89 if (storage_buffer.size() < required_size) {
90 storage_buffer.resize(static_cast<std::size_t>(static_cast<std::float_t>(required_size) * 1.5f));
91 }
92
93 storage_buffer.update(lines.data(), required_size);
94
95 _push_handler.push("mvp", projection * view);
96 _push_handler.push("vertices", storage_buffer.address());
97
98
99 if (!_descriptor_handler.update(_pipeline)) {
100 return;
101 }
102
103 _descriptor_handler.bind_descriptors(command_buffer);
104 _push_handler.bind(command_buffer);
105
106 command_buffer.draw(static_cast<std::uint32_t>(lines.size()), 1u, 0u, 0u);
107
108 scenes_module.clear_debug_lines();
109 }
110
111private:
112
113 pipeline _pipeline;
114
115 sbx::graphics::push_handler _push_handler;
116
117 // std::optional<graphics::storage_buffer> _storage_buffer;
118 graphics::storage_buffer_handle _storage_buffer;
119
120 sbx::graphics::descriptor_handler _descriptor_handler;
121
122}; // class debug_subrenderer
123
124} // namespace sbx::scenes
125
126#endif // LIBSBX_SCENES_DEBUG_SUBRENDERER_HPP_
Definition: command_buffer.hpp:15
Definition: descriptor_handler.hpp:26
Definition: graphics_pipeline.hpp:113
Definition: push_handler.hpp:18
Definition: resource_storage.hpp:18
Definition: storage_buffer.hpp:17
Definition: subrenderer.hpp:14
Definition: camera.hpp:96
Definition: debug_subrenderer.hpp:21
Definition: scene.hpp:70
Definition: scenes_module.hpp:21
RGBA color representation and utilities.
Definition: graphics_pipeline.hpp:104
Definition: graphics_pipeline.hpp:65
Definition: scenes_module.hpp:29