sandbox
Loading...
Searching...
No Matches
render_stage.hpp
1#ifndef LIBSBX_GRAPHICS_RENDER_STAGE_HPP_
2#define LIBSBX_GRAPHICS_RENDER_STAGE_HPP_
3
4#include <string>
5#include <cinttypes>
6#include <optional>
7#include <vector>
8
9#include <vulkan/vulkan.hpp>
10
12
13#include <libsbx/devices/devices_module.hpp>
14
15#include <libsbx/math/color.hpp>
16#include <libsbx/math/vector2.hpp>
17
18#include <libsbx/graphics/images/depth_image.hpp>
19#include <libsbx/graphics/images/image2d.hpp>
20
21#include <libsbx/graphics/render_pass/swapchain.hpp>
22
23namespace sbx::graphics {
24
25enum class format : std::uint32_t {
26 undefined = VK_FORMAT_UNDEFINED,
27 r32_sfloat = VK_FORMAT_R32_SFLOAT,
28 r32g32_sfloat = VK_FORMAT_R32G32_SFLOAT,
29 r8g8b8a8_unorm = VK_FORMAT_R8G8B8A8_UNORM,
30 r32g32b32a32_sfloat = VK_FORMAT_R32G32B32A32_SFLOAT
31}; // enum class format
32
33enum class address_mode : std::uint32_t {
34 repeat = VK_SAMPLER_ADDRESS_MODE_REPEAT,
35 clamp_to_edge = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE
36}; // enum class address_mode
37
39
40public:
41
42 enum class type {
43 image,
44 depth,
46 }; // enum class type
47
48 attachment(const std::uint32_t binding, const std::string& name, type type, const math::color& clear_color = math::color::black, const format format = format::r8g8b8a8_unorm, const address_mode address_mode = address_mode::repeat) noexcept
49 : _binding{binding},
50 _name{std::move(name)},
51 _type{type},
52 _clear_color{clear_color},
53 _format{format},
54 _address_mode{address_mode} { }
55
56 auto binding() const noexcept -> std::uint32_t {
57 return _binding;
58 }
59
60 auto name() const noexcept -> const std::string& {
61 return _name;
62 }
63
64 auto image_type() const noexcept -> type {
65 return _type;
66 }
67
68 auto format() const noexcept -> graphics::format {
69 return _format;
70 }
71
72 auto address_mode() const noexcept -> graphics::address_mode {
73 return _address_mode;
74 }
75
76 auto clear_color() const noexcept -> const math::color& {
77 return _clear_color;
78 }
79
80private:
81
82 std::uint32_t _binding;
83 std::string _name;
84 type _type;
85 bool _is_multi_sampled;
86 math::color _clear_color;
87 graphics::format _format;
88 graphics::address_mode _address_mode;
89
90}; // class attachment
91
93
94public:
95
96 subpass_binding(std::uint32_t binding, std::vector<std::uint32_t> color_attachments, std::vector<std::uint32_t> input_attachments = {}) noexcept
97 : _binding{binding},
98 _color_attachments{std::move(color_attachments)},
99 _input_attachments{std::move(input_attachments)} { }
100
101 auto binding() const noexcept -> std::uint32_t {
102 return _binding;
103 }
104
105 auto color_attachments() const noexcept -> const std::vector<std::uint32_t>& {
106 return _color_attachments;
107 }
108
109 auto input_attachments() const noexcept -> const std::vector<std::uint32_t>& {
110 return _input_attachments;
111 }
112
113 auto input_attachment_bindings() const noexcept -> const std::vector<std::uint32_t>& {
114 return _input_attachment_bindings;
115 }
116
117private:
118
119 std::uint32_t _binding;
120 std::vector<std::uint32_t> _color_attachments;
121 std::vector<std::uint32_t> _input_attachments;
122
123}; // class subpass_binding
124
125class viewport {
126
127public:
128
129 viewport() noexcept
130 : _scale{1.0f, 1.0f},
131 _offset{0, 0},
132 _size{std::nullopt} { }
133
134 viewport(const math::vector2u& size) noexcept
135 : _scale{1.0f, 1.0f},
136 _offset{0, 0},
137 _size{size} { }
138
139 auto scale() const noexcept -> const math::vector2f& {
140 return _scale;
141 }
142
143 auto set_scale(const math::vector2f& scale) noexcept -> void {
144 _scale = scale;
145 }
146
147 auto offset() const noexcept -> const math::vector2i& {
148 return _offset;
149 }
150
151 auto set_offset(const math::vector2i& offset) noexcept -> void {
152 _offset = offset;
153 }
154
155 auto size() const noexcept -> const std::optional<math::vector2u>& {
156 return _size;
157 }
158
159 auto set_size(const math::vector2u& size) noexcept -> void {
160 _size = size;
161 }
162
163private:
164
165 math::vector2f _scale;
166 math::vector2i _offset;
167 std::optional<math::vector2u> _size;
168
169}; // class viewport
170
172
173public:
174
175 render_area(const math::vector2u& extent = math::vector2u{}, const math::vector2i& offset = math::vector2i{}) noexcept
176 : _extent{extent},
177 _offset{offset},
178 _aspect_ratio{static_cast<std::float_t>(extent.x()) / static_cast<std::float_t>(extent.y())} { }
179
180 auto operator==(const render_area& other) const noexcept -> bool {
181 return _extent == other._extent && _offset == other._offset;
182 }
183
184 auto extent() const noexcept -> const math::vector2u& {
185 return _extent;
186 }
187
188 auto set_extent(const math::vector2u& extent) noexcept -> void {
189 _extent = extent;
190 }
191
192 auto offset() const noexcept -> const math::vector2i& {
193 return _offset;
194 }
195
196 auto set_offset(const math::vector2i& offset) noexcept -> void {
197 _offset = offset;
198 }
199
200 auto aspect_ratio() const noexcept -> std::float_t {
201 return _aspect_ratio;
202 }
203
204 auto set_aspect_ratio(std::float_t aspect_ratio) noexcept -> void {
205 _aspect_ratio = aspect_ratio;
206 }
207
208private:
209
210 math::vector2u _extent;
211 math::vector2i _offset;
212 std::float_t _aspect_ratio;
213
214}; // class render_area
215
217
218public:
219
220 render_stage(std::vector<attachment>&& attachments, std::vector<subpass_binding>&& subpass_bindings, const graphics::viewport& viewport = graphics::viewport{});
221
223
224 auto attachments() const noexcept -> const std::vector<graphics::attachment>&;
225
226 auto find_attachment(const std::string& name) const noexcept -> std::optional<graphics::attachment>;
227
228 auto find_attachment(std::uint32_t binding) const noexcept -> std::optional<graphics::attachment>;
229
230 auto subpasses() const noexcept -> const std::vector<subpass_binding>&;
231
232 auto attachment_count(std::uint32_t subpass) const -> std::uint32_t;
233
234 auto clear_values() const noexcept -> const std::vector<VkClearValue>&;
235
236 auto has_depth_attachment() const noexcept -> bool;
237
238 auto has_swapchain_attachment() const noexcept -> bool;
239
240 auto viewport() const noexcept -> const viewport&;
241
242 auto render_area() const noexcept -> const render_area&;
243
244 auto render_pass() const noexcept -> const VkRenderPass&;
245
246 auto rebuild(const swapchain& swapchain) -> void;
247
248 auto framebuffer(std::uint32_t index) noexcept -> const VkFramebuffer&;
249
250 auto descriptor(const std::string& name) const noexcept -> memory::observer_ptr<const graphics::descriptor>;
251
252 auto descriptors() const noexcept -> const std::map<std::string, memory::observer_ptr<const graphics::descriptor>>&;
253
254private:
255
256 auto _create_render_pass(VkFormat depth_format, VkFormat surface_format) -> void;
257
258 auto _rebuild_framebuffers(const swapchain& swapchain) -> void;
259
260 auto _update_subpass_attachment_counts(const graphics::attachment& attachment) -> void;
261
262 auto _create_attachment_descriptions(VkFormat depth_format, VkFormat surface_format) -> std::vector<VkAttachmentDescription>;
263
264 auto _create_subpass_dependencies() -> std::vector<VkSubpassDependency>;
265
266 std::vector<graphics::attachment> _attachments;
267 std::vector<subpass_binding> _subpass_bindings;
268
269 graphics::viewport _viewport;
270
271 VkRenderPass _render_pass;
272
273 std::map<std::string, memory::observer_ptr<const graphics::descriptor>> _descriptors;
274
275 std::unique_ptr<graphics::depth_image> _depth_image;
276 std::unordered_map<std::uint32_t, std::unique_ptr<graphics::image2d>> _color_images;
277
278 std::vector<VkFramebuffer> _framebuffers;
279
280 std::vector<VkClearValue> _clear_values;
281 std::vector<std::uint32_t> _subpass_attachment_counts;
282 std::optional<graphics::attachment> _depth_attachment;
283 std::optional<graphics::attachment> _swapchain_attachment;
284
285 graphics::render_area _render_area;
286
287}; // class render_stage
288
289} // namespace sbx::graphics
290
291#endif // LIBSBX_GRAPHICS_RENDER_STAGE_HPP_
Definition: render_stage.hpp:38
Definition: descriptor.hpp:33
Definition: image.hpp:18
Definition: render_stage.hpp:171
Definition: render_stage.hpp:216
Definition: render_stage.hpp:92
Definition: swapchain.hpp:12
Definition: render_stage.hpp:125
A vector in two-dimensional space.
Definition: vector2.hpp:27
Definition: color.hpp:14
A non-owning pointer that can be used to observe the value of a pointer.
Definition: observer_ptr.hpp:27