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 inline static const auto pipeline_definition = sbx::graphics::pipeline_definition{
24 .depth = sbx::graphics::depth::disabled,
25 .uses_transparency = false,
26 .rasterization_state = sbx::graphics::rasterization_state{
27 .polygon_mode = sbx::graphics::polygon_mode::fill,
28 .cull_mode = sbx::graphics::cull_mode::none,
29 .front_face = sbx::graphics::front_face::counter_clockwise
30 },
31 .primitive_topology = sbx::graphics::primitive_topology::line_list
32 };
33
34 inline static constexpr auto default_shader_path = std::string_view{"engine://shaders/debug"};
35
36public:
37
38 debug_subrenderer(const std::vector<graphics::attachment_description>& attachments, const std::filesystem::path& path = default_shader_path)
40 _pipeline{path, attachments, pipeline_definition},
41 _push_handler{_pipeline},
42 _descriptor_handler{_pipeline, 0u} {
43 auto& graphics_module = core::engine::get_module<graphics::graphics_module>();
44
45 _storage_buffer = graphics_module.add_resource<graphics::storage_buffer>(graphics::storage_buffer::min_size, VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
46 }
47
48 ~debug_subrenderer() override {
49
50 }
51
52 auto render(sbx::graphics::command_buffer& command_buffer) -> void override {
53 EASY_BLOCK("debug_subrenderer::render");
54
55 auto& graphics_module = core::engine::get_module<graphics::graphics_module>();
56 auto& scenes_module = sbx::core::engine::get_module<sbx::scenes::scenes_module>();
57
58 const auto& lines = scenes_module.debug_lines();
59
60 if (lines.empty()) {
61 return;
62 }
63
64 auto& scene = scenes_module.active_scene();
65 auto& environment = scene.environment();
66 auto& graph = scene.graph();
67
68 _pipeline.bind(command_buffer);
69
70 auto& storage_buffer = graphics_module.get_resource<graphics::storage_buffer>(_storage_buffer);
71
72 const auto required_size = static_cast<std::uint32_t>(lines.size() * sizeof(scenes_module::line));
73
74 if (storage_buffer.size() < required_size) {
75 storage_buffer.resize(static_cast<std::size_t>(static_cast<std::float_t>(required_size) * 1.5f));
76 }
77
78 storage_buffer.update(lines.data(), required_size);
79
80 _push_handler.push("vertices", storage_buffer.address());
81
82 _descriptor_handler.push("scene", environment.uniform_handler());
83
84 if (!_descriptor_handler.update(_pipeline)) {
85 return;
86 }
87
88 _descriptor_handler.bind_descriptors(command_buffer);
89 _push_handler.bind(command_buffer);
90
91 command_buffer.draw(static_cast<std::uint32_t>(lines.size()), 1u, 0u, 0u);
92
93 scenes_module.clear_debug_lines();
94 }
95
96private:
97
99
100 sbx::graphics::push_handler _push_handler;
101
102 // std::optional<graphics::storage_buffer> _storage_buffer;
103 graphics::storage_buffer_handle _storage_buffer;
104
105 sbx::graphics::descriptor_handler _descriptor_handler;
106
107}; // class debug_subrenderer
108
109} // namespace sbx::scenes
110
111#endif // LIBSBX_SCENES_DEBUG_SUBRENDERER_HPP_
Definition: command_buffer.hpp:15
Definition: descriptor_handler.hpp:26
Definition: graphics_pipeline.hpp:115
Definition: push_handler.hpp:18
Definition: resource_storage.hpp:18
Definition: storage_buffer.hpp:17
Definition: subrenderer.hpp:14
Definition: debug_subrenderer.hpp:21
Definition: scene.hpp:27
Definition: scenes_module.hpp:26
RGBA color representation and utilities.
Definition: graphics_pipeline.hpp:106
Definition: graphics_pipeline.hpp:67
Definition: scenes_module.hpp:32