sandbox
Loading...
Searching...
No Matches
renderer.hpp
1#ifndef LIBSBX_GRAPHICS_RENDERER_HPP_
2#define LIBSBX_GRAPHICS_RENDERER_HPP_
3
4#include <memory>
5#include <vector>
6#include <typeindex>
7
8#include <easy/profiler.h>
9
10#include <libsbx/utility/noncopyable.hpp>
11#include <libsbx/utility/concepts.hpp>
13
14#include <libsbx/graphics/commands/command_buffer.hpp>
15
16#include <libsbx/graphics/pipeline/pipeline.hpp>
17
18#include <libsbx/graphics/task.hpp>
19#include <libsbx/graphics/subrenderer.hpp>
20#include <libsbx/graphics/render_stage.hpp>
21#include <libsbx/graphics/render_graph.hpp>
22
23namespace sbx::graphics {
24
26
27public:
28
29 renderer() = default;
30
31 virtual ~renderer() = default;
32
33 virtual auto initialize() -> void = 0;
34
35 auto render(const pipeline::stage& stage, command_buffer& command_buffer) -> void {
36 const auto stage_name = fmt::format("Render Stage: {}.{}", stage.renderpass, stage.subpass);
37 EASY_BLOCK(stage_name.c_str(), profiler::colors::LightBlue);
38 for (const auto& [render_stage, index] : _subrenderer_stages) {
39 if (render_stage == stage) {
40 _subrenderers[index]->render(command_buffer);
41 }
42 }
43 EASY_END_BLOCK;
44 }
45
46 auto execute_tasks(command_buffer& command_buffer) -> void {
47 for (const auto& task : _tasks) {
48 task->execute(command_buffer);
49 }
50 }
51
52 auto add_render_stage(std::vector<attachment>&& attachments, std::vector<subpass_binding>&& subpass_bindings, const viewport& viewport = graphics::viewport::window()) -> void {
53 _render_stages.push_back(std::make_unique<graphics::render_stage>(std::move(attachments), std::move(subpass_bindings), viewport));
54 }
55
56 auto render_stages() const noexcept -> const std::vector<std::unique_ptr<graphics::render_stage>>& {
57 return _render_stages;
58 }
59
61 return *_render_stages.at(stage.renderpass);
62 }
63
64protected:
65
66 // template<utility::implements<subrenderer> Type, typename... Args>
67 template<typename Type, typename... Args>
68 requires (std::is_constructible_v<Type, const std::filesystem::path&, const pipeline::stage&, Args...>)
69 auto add_subrenderer(const std::filesystem::path& path, const pipeline::stage& stage, Args&&... args) -> Type& {
70 _subrenderer_stages.insert({stage, _subrenderers.size()});
71
72 _subrenderers.push_back(std::make_unique<Type>(path, stage, std::forward<Args>(args)...));
73
74 return *static_cast<Type*>(_subrenderers.back().get());
75 }
76
77 template<typename Type, typename... Args>
78 requires (std::is_constructible_v<Type, std::filesystem::path, Args...>)
79 auto add_task(const std::filesystem::path& path, Args&&... args) -> Type& {
80 _tasks.push_back(std::make_unique<Type>(path, std::forward<Args>(args)...));
81
82 return *static_cast<Type*>(_tasks.back().get());
83 }
84
85private:
86
87 std::vector<std::unique_ptr<graphics::task>> _tasks;
88
89 std::vector<std::unique_ptr<graphics::render_stage>> _render_stages;
90
91 std::vector<std::unique_ptr<subrenderer>> _subrenderers;
92 std::multimap<pipeline::stage, std::size_t> _subrenderer_stages;
93
94 render_graph _graph;
95
96}; // class renderer
97
98} // namespace sbx::graphics
99
100#endif // LIBSBX_GRAPHICS_RENDERER_HPP_
Definition: command_buffer.hpp:14
Definition: render_graph.hpp:43
Definition: render_stage.hpp:251
Definition: renderer.hpp:25
Definition: task.hpp:8
Definition: render_stage.hpp:123
Definition: pipeline.hpp:22
Definition: noncopyable.hpp:6