sandbox
Loading...
Searching...
No Matches
render_graph.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_GRAPHICS_RENDER_GRAPH_HPP_
3#define LIBSBX_GRAPHICS_RENDER_GRAPH_HPP_
4
5#include <vector>
6#include <variant>
7#include <string>
8#include <memory>
9#include <unordered_map>
10#include <optional>
11#include <functional>
12#include <queue>
13
14#include <vulkan/vulkan.h>
15
16#include <libsbx/utility/enum.hpp>
17#include <libsbx/utility/exception.hpp>
18#include <libsbx/utility/hashed_string.hpp>
19#include <libsbx/utility/logger.hpp>
20#include <libsbx/utility/overload.hpp>
21
22#include <libsbx/math/color.hpp>
23
24#include <libsbx/memory/observer_ptr.hpp>
25
26#include <libsbx/graphics/viewport.hpp>
27#include <libsbx/graphics/draw_list.hpp>
28
29#include <libsbx/graphics/images/image2d.hpp>
30#include <libsbx/graphics/images/depth_image.hpp>
31
32#include <libsbx/graphics/render_pass/swapchain.hpp>
33
34namespace sbx::graphics {
35
36enum class blend_factor : std::int32_t {
37 zero = VK_BLEND_FACTOR_ZERO,
38 one = VK_BLEND_FACTOR_ONE,
39 source_color = VK_BLEND_FACTOR_SRC_COLOR,
40 one_minus_source_color = VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR,
41 source_alpha = VK_BLEND_FACTOR_SRC_ALPHA,
42 one_minus_source_alpha = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
43 destination_color = VK_BLEND_FACTOR_DST_COLOR,
44 one_minus_destination_color = VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR,
45 destination_alpha = VK_BLEND_FACTOR_DST_ALPHA,
46 one_minus_destination_alpha = VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA,
47 constant_color = VK_BLEND_FACTOR_CONSTANT_COLOR,
48 one_minus_constant_color = VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR,
49 constant_alpha = VK_BLEND_FACTOR_CONSTANT_ALPHA,
50 one_minus_constant_alpha = VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA,
51 source_alpha_saturate = VK_BLEND_FACTOR_SRC_ALPHA_SATURATE
52}; // enum class blend_factor
53
54enum class blend_operation : std::int32_t {
55 add = VK_BLEND_OP_ADD,
56 subtract = VK_BLEND_OP_SUBTRACT,
57 reverse_subtract = VK_BLEND_OP_REVERSE_SUBTRACT,
58 min = VK_BLEND_OP_MIN,
59 max = VK_BLEND_OP_MAX
60}; // enum class blend_operation
61
62enum class color_component : std::int32_t {
63 r = VK_COLOR_COMPONENT_R_BIT,
64 g = VK_COLOR_COMPONENT_G_BIT,
65 b = VK_COLOR_COMPONENT_B_BIT,
66 a = VK_COLOR_COMPONENT_A_BIT
67}; // enum class color_component
68
69inline constexpr auto operator|(const color_component lhs, const color_component rhs) noexcept -> color_component {
70 return static_cast<color_component>(static_cast<std::underlying_type_t<color_component>>(lhs) | static_cast<std::underlying_type_t<color_component>>(rhs));
71}
72
73inline constexpr auto operator&(const color_component lhs, const color_component rhs) noexcept -> color_component {
74 return static_cast<color_component>(static_cast<std::underlying_type_t<color_component>>(lhs) & static_cast<std::underlying_type_t<color_component>>(rhs));
75}
76
78 blend_factor color_source{blend_factor::source_alpha};
79 blend_factor color_destination{blend_factor::one_minus_source_alpha};
80 blend_operation color_operation{blend_operation::add};
81 blend_factor alpha_source{blend_factor::one};
82 blend_factor alpha_destination{blend_factor::zero};
83 blend_operation alpha_operation{blend_operation::add};
84 color_component color_write_mask{color_component::r | color_component::g | color_component::b | color_component::a};
85}; // struct blend_state
86
87enum class attachment_load_operation : std::int32_t {
88 load = VK_ATTACHMENT_LOAD_OP_LOAD,
89 clear = VK_ATTACHMENT_LOAD_OP_CLEAR,
90 dont_care = VK_ATTACHMENT_LOAD_OP_DONT_CARE
91}; // enum class color_component
92
94
95public:
96
97 enum class type {
98 image,
99 depth,
100 storage,
102 }; // enum class type
103
104 attachment(const utility::hashed_string& name, type type, const math::color& clear_color = math::color::black(), const format format = format::r8g8b8a8_unorm, const graphics::blend_state& blend_state = graphics::blend_state{}, const filter filter = filter::linear, const address_mode address_mode = address_mode::repeat, std::uint32_t array_layers = 1u) noexcept;
105
106 attachment(const utility::hashed_string& name, type type, const math::color& clear_color, const format format, const filter filter, const address_mode address_mode, std::uint32_t array_layers = 1u) noexcept;
107
108 auto name() const noexcept -> const utility::hashed_string&;
109
110 auto image_type() const noexcept -> type;
111
112 auto format() const noexcept -> graphics::format;
113
114 auto address_mode() const noexcept -> graphics::address_mode;
115
116 auto clear_color() const noexcept -> const math::color&;
117
118 auto blend_state() const noexcept -> const graphics::blend_state&;
119
120 auto array_layers() const noexcept -> std::uint32_t;
121
122private:
123
125 type _type;
126 math::color _clear_color;
127 graphics::format _format;
128 graphics::filter _filter;
129 graphics::address_mode _address_mode;
130 graphics::blend_state _blend_state;
131 std::uint32_t _array_layers;
132
133}; // class attachment
134
136 attachment::type image_type;
137 graphics::format format;
139 std::uint32_t array_layers{1u};
140}; // struct attachment_description
141
143
144 std::uint32_t index{0xFFFFFFFF};
145
146 [[nodiscard]] auto is_valid() const noexcept -> bool {
147 return index != 0xFFFFFFFF;
148 }
149
150}; // struct attachment_handle
151
153
154 std::uint32_t index{0xFFFFFFFF};
155
156 [[nodiscard]] auto is_valid() const noexcept -> bool {
157 return index != 0xFFFFFFFF;
158 }
159
160}; // struct pass_handle
161
163
164 friend class render_graph;
165
166public:
167
168 enum class kind : std::uint8_t {
169 graphics,
170 compute
171 }; // enum class kind
172
173 pass_node(const utility::hashed_string& name, const viewport& viewport, const kind kind)
174 : _name{name},
175 _viewport{viewport},
176 _kind{kind} { }
177
178 auto reads(const attachment_handle attachment) -> void {
179 _reads.emplace_back(attachment);
180 }
181
182 template<typename... Attachments>
183 requires (sizeof...(Attachments) > 1u && (std::is_same_v<std::remove_cvref_t<Attachments>, attachment_handle> && ...))
184 auto reads(Attachments&&... attachments) -> void {
185 (reads(attachments), ...);
186 }
187
188 auto writes(const attachment_handle attachment, const attachment_load_operation load_operation = attachment_load_operation::clear) -> void {
189 _writes.emplace_back(attachment, load_operation);
190 }
191
192 auto depends_on(const pass_handle pass) -> void {
193 _dependencies.emplace_back(pass);
194 }
195
196 template<typename... Passes>
197 requires (sizeof...(Passes) > 1u && (std::is_same_v<std::remove_cvref_t<Passes>, pass_handle> && ...))
198 auto depends_on(Passes&&... passes) -> void {
199 (depends_on(passes), ...);
200 }
201
202private:
203
205 std::vector<attachment_handle> _reads;
206 std::vector<std::pair<attachment_handle, attachment_load_operation>> _writes;
207 std::vector<pass_handle> _dependencies;
208 viewport _viewport;
209 render_area _render_area;
210 kind _kind;
211
212}; // struct pass_node
213
216 VkImageLayout new_layout;
217}; // struct transition_instruction
218
220 pass_handle pass;
221 std::vector<std::pair<attachment_handle, attachment_load_operation>> attachments;
222}; // struct pass_instruction
223
225 pass_handle pass;
226}; // struct compute_instruction
227
228using instruction = std::variant<transition_instruction, pass_instruction, compute_instruction>;
229
231 VkImage image;
232 VkImageView view;
233 VkImageLayout current_layout = VK_IMAGE_LAYOUT_UNDEFINED;
234 VkFormat format;
235 VkExtent2D extent;
236 attachment::type type;
237 std::uint32_t array_layers{1u};
238}; // struct attachment_state
239
241
242public:
243
244 struct context {
245 auto graphics_pass(const utility::hashed_string& name, const viewport& viewport = viewport::window()) const -> pass_node;
246 auto compute_pass(const utility::hashed_string& name) const -> pass_node;
247 }; // struct context
248
249 render_graph();
250
252
253 template<typename... Args>
254 requires (std::is_constructible_v<attachment, Args...>)
255 auto create_attachment(Args&&... args) -> attachment_handle;
256
257 template<typename Callable>
258 requires (std::is_invocable_r_v<pass_node, Callable, context&>)
259 auto create_pass(Callable&& callable) -> pass_handle;
260
261 auto find_attachment(const std::string& name) const -> const image2d&;
262
263 auto attachment_descriptions(const pass_handle handle) const -> std::vector<attachment_description>;
264
265 auto build() -> void;
266
267 auto resize(const std::string& viewport_name) -> void;
268
269 auto pass_kind(const pass_handle handle) const -> pass_node::kind {
270 utility::assert_that(handle.is_valid() && handle.index < _passes.size(), "Invalid pass handle");
271
272 return _passes[handle.index]._kind;
273 }
274
275 template<typename PassCallback, typename ComputeCallback>
276 requires (std::is_invocable_v<PassCallback, const pass_handle&> && std::is_invocable_v<ComputeCallback, const pass_handle&>)
277 auto execute(command_buffer& command_buffer, const swapchain& swapchain, PassCallback&& pass_callback, ComputeCallback&& compute_callback) -> void {
278 for (auto& state : _attachment_states) {
279 if (state.type == attachment::type::swapchain) {
280 state.current_layout = VK_IMAGE_LAYOUT_UNDEFINED;
281 }
282 }
283
284 for (const auto& instruction : _instructions) {
285 std::visit(utility::overload{
286 [&](const transition_instruction& instruction) { _execute_transition_instruction(command_buffer, swapchain, instruction); },
287 [&](const pass_instruction& instruction) { _execute_pass_instruction(command_buffer, swapchain, instruction, std::forward<PassCallback>(pass_callback)); },
288 [&](const compute_instruction& instruction) { _execute_compute_instruction(command_buffer, instruction, std::forward<ComputeCallback>(compute_callback)); },
289 }, instruction);
290 }
291 }
292
293private:
294
295 auto _update_viewports() -> void;
296
297 auto _clear_attachments(const std::string& viewport_name) -> void;
298
299 auto _create_attachments(const pass_node& node) -> void;
300
301 auto _pass_matches(const pass_node& pass, const std::string& viewport_name) const -> bool;
302
303 auto _build_color_attachment_info(const attachment& attachment, const attachment_state& state, const swapchain& swapchain, const attachment_load_operation load_op) -> VkRenderingAttachmentInfo;
304
305 auto _build_depth_attachment_info(const attachment& attachment, const attachment_state& state, const attachment_load_operation load_op) -> VkRenderingAttachmentInfo;
306
307 template<typename Callable>
308 auto _execute_pass_instruction(command_buffer& command_buffer, const swapchain& swapchain, const pass_instruction& instruction, Callable&& callable) -> void;
309
310 template<typename Callable>
311 auto _execute_compute_instruction(command_buffer& command_buffer, const compute_instruction& instruction, Callable&& callable) -> void;
312
313 auto _execute_transition_instruction(command_buffer& command_buffer, const swapchain& swapchain, const transition_instruction& instruction) -> void;
314
315 std::vector<attachment> _attachments;
316 std::vector<pass_node> _passes;
317
318 std::vector<image2d_handle> _color_images;
319 std::vector<depth_image_handle> _depth_images;
320
321 std::vector<attachment_state> _attachment_states;
322
323 std::unordered_map<utility::hashed_string, std::uint32_t> _image_by_name;
324
325 std::vector<instruction> _instructions;
326
327}; // class render_graph
328
329} // namespace sbx::graphics
330
331#include <libsbx/graphics/render_graph.ipp>
332
333#endif // LIBSBX_GRAPHICS_RENDER_GRAPH_HPP_
Definition: tests.cpp:6
Definition: render_graph.hpp:93
Definition: command_buffer.hpp:15
Definition: image2d.hpp:52
Definition: image.hpp:21
Definition: render_graph.hpp:162
Definition: viewport.hpp:81
Definition: render_graph.hpp:240
Definition: swapchain.hpp:15
Definition: viewport.hpp:13
RGBA color value type.
Definition: color.hpp:48
static auto black() noexcept -> color
Returns a black color.
Definition: color.cpp:45
Definition: hashed_string.hpp:17
RGBA color representation and utilities.
Definition: render_graph.hpp:135
Definition: render_graph.hpp:142
Definition: render_graph.hpp:230
Definition: render_graph.hpp:77
Definition: render_graph.hpp:224
Definition: render_graph.hpp:152
Definition: render_graph.hpp:219
Definition: render_graph.hpp:244
Definition: render_graph.hpp:214