1#ifndef LIBSBX_GRAPHICS_GRAPHICS_MODULE_HPP_
2#define LIBSBX_GRAPHICS_GRAPHICS_MODULE_HPP_
5#include <unordered_map>
9#include <libsbx/core/module.hpp>
12#include <libsbx/math/uuid.hpp>
14#include <libsbx/devices/devices_module.hpp>
17#include <libsbx/utility/concepts.hpp>
19#include <libsbx/signals/signal.hpp>
21#include <libsbx/graphics/devices/instance.hpp>
22#include <libsbx/graphics/devices/physical_device.hpp>
23#include <libsbx/graphics/devices/logical_device.hpp>
24#include <libsbx/graphics/devices/allocator.hpp>
25#include <libsbx/graphics/devices/surface.hpp>
27#include <libsbx/graphics/commands/command_pool.hpp>
28#include <libsbx/graphics/commands/command_buffer.hpp>
30#include <libsbx/graphics/render_pass/swapchain.hpp>
32#include <libsbx/graphics/pipeline/pipeline.hpp>
33#include <libsbx/graphics/pipeline/shader.hpp>
34#include <libsbx/graphics/pipeline/graphics_pipeline.hpp>
35#include <libsbx/graphics/pipeline/compute_pipeline.hpp>
36#include <libsbx/graphics/pipeline/compiler.hpp>
38#include <libsbx/graphics/buffers/buffer.hpp>
39#include <libsbx/graphics/buffers/storage_buffer.hpp>
41#include <libsbx/graphics/images/image2d.hpp>
42#include <libsbx/graphics/images/cube_image.hpp>
44#include <libsbx/graphics/renderer.hpp>
46#include <libsbx/graphics/resource_storage.hpp>
48namespace sbx::graphics {
55auto validate(VkResult result) -> void;
57template<
typename VkEnum,
typename Enum>
58requires ((std::is_enum_v<VkEnum> || std::is_same_v<VkEnum, VkFlags>) && std::is_enum_v<Enum>)
59constexpr auto to_vk_enum(Enum value) -> VkEnum {
60 return static_cast<VkEnum
>(value);
70 inline static const auto is_registered = register_module(stage::rendering, dependencies<devices::devices_module>{});
72 inline static constexpr auto max_deletion_queue_size = std::size_t{16u};
80 auto update() ->
void override;
90 auto command_pool(VkQueueFlagBits queue_type = VK_QUEUE_GRAPHICS_BIT,
const std::thread::id& thread_id = std::this_thread::get_id()) ->
const std::shared_ptr<command_pool>&;
94 template<utility::implements<renderer> Renderer,
typename... Args>
95 requires (std::is_constructible_v<Renderer, Args...>)
96 auto set_renderer(Args&&... args) ->
void {
97 _renderer = std::make_unique<Renderer>(std::forward<Args>(args)...);
98 _recreate_swapchain();
103 auto current_frame()
const noexcept -> std::uint32_t {
104 return _current_frame;
109 template<
typename Type,
typename... Args>
110 requires (std::is_constructible_v<Type, Args...>)
112 return _storage<Type>().emplace(std::forward<Args>(args)...);
115 template<
typename Type>
117 return _storage<Type>().get(handle);
120 template<
typename Type>
122 return _storage<Type>().get(handle);
125 template<
typename Type>
127 return _storage<Type>().remove(handle);
134 template<queue::type Source, queue::type Destination,
typename Type>
135 requires (std::is_same_v<Type, graphics::buffer> || std::is_same_v<Type, graphics::storage_buffer>)
136 auto transfer_ownership(
const resource_handle<Type>& handle,
const VkPipelineStageFlagBits2 stage = VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT) ->
void {
137 auto&
buffer = get_resource<Type>(handle);
140 .src_stage_mask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT,
141 .src_access_mask = VK_ACCESS_2_SHADER_WRITE_BIT,
142 .src_queue_family = _logical_device->queue<Source>().family(),
143 .dst_queue_family = _logical_device->queue<Destination>().family(),
148 .dst_stage_mask = stage,
149 .dst_access_mask = _access_mask_from_stage(stage),
150 .src_queue_family = _logical_device->queue<Source>().family(),
151 .dst_queue_family = _logical_device->queue<Destination>().family(),
156 template<
typename Callable>
157 requires (std::is_invocable_r_v<math::vector2u, Callable>)
158 auto set_dynamic_size_callback(Callable&& callback) ->
void {
159 _dynamic_size_callback = std::forward<Callable>(callback);
167 return _on_viewport_changed;
176 static constexpr auto _access_mask_from_stage(VkPipelineStageFlagBits2 stage) -> VkAccessFlagBits2 {
178 case VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT: {
179 return VK_ACCESS_2_INDIRECT_COMMAND_READ_BIT;
181 case VK_PIPELINE_STAGE_2_VERTEX_SHADER_BIT:
182 case VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT:
183 case VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT:
184 case VK_PIPELINE_STAGE_2_MESH_SHADER_BIT_EXT:
185 case VK_PIPELINE_STAGE_2_TASK_SHADER_BIT_EXT: {
186 return VK_ACCESS_2_SHADER_READ_BIT;
188 case VK_PIPELINE_STAGE_2_TRANSFER_BIT: {
189 return VK_ACCESS_2_TRANSFER_READ_BIT;
192 return VK_ACCESS_2_MEMORY_READ_BIT;
201 auto _reset_render_stages() -> void;
203 auto _recreate_viewport() -> void;
205 auto _recreate_swapchain() -> void;
207 auto _recreate_per_frame_data() -> void;
209 auto _recreate_per_image_data() -> void;
211 auto _recreate_command_buffers() -> void;
213 auto _recreate_attachments() -> void;
215 struct per_frame_data {
217 VkSemaphore image_available_semaphore{};
218 VkFence graphics_in_flight_fence{};
220 VkSemaphore compute_finished_semaphore{};
221 VkFence compute_in_flight_fence{};
224 struct per_image_data {
225 VkSemaphore render_finished_semaphore{};
228 struct command_pool_key {
229 VkQueueFlagBits queue_type;
230 std::thread::id thread_id;
233 struct command_pool_key_hash {
234 auto operator()(
const command_pool_key& key)
const noexcept -> std::size_t {
235 auto hast = std::size_t{0};
236 utility::hash_combine(hast, key.queue_type, key.thread_id);
241 struct command_pool_key_equality {
242 auto operator()(
const command_pool_key& lhs,
const command_pool_key& rhs)
const noexcept ->
bool {
243 return lhs.queue_type == rhs.queue_type && lhs.thread_id == rhs.thread_id;
248 static_assert(std::is_class_v<graphics::graphics_pipeline>,
"graphics_pipeline is not a class in sbx::graphics");
250 template<
typename Type>
252 if constexpr (std::is_same_v<Type, shader>) {
254 }
else if constexpr (std::is_same_v<Type, graphics_pipeline>) {
255 return _graphics_pipelines;
256 }
else if constexpr (std::is_same_v<Type, compute_pipeline>) {
257 return _compute_pipelines;
258 }
else if constexpr (std::is_same_v<Type, buffer>) {
260 }
else if constexpr (std::is_same_v<Type, storage_buffer>) {
261 return _storage_buffers;
262 }
else if constexpr (std::is_same_v<Type, uniform_buffer>) {
263 return _uniform_buffers;
264 }
if constexpr (std::is_same_v<Type, image2d>) {
266 }
else if constexpr (std::is_same_v<Type, depth_image>) {
267 return _depth_images;
268 }
else if constexpr (std::is_same_v<Type, cube_image>) {
272 utility::assert_that(
false,
"Invalid resource type");
275 template<
typename Type>
277 if constexpr (std::is_same_v<Type, shader>) {
279 }
else if constexpr (std::is_same_v<Type, graphics_pipeline>) {
280 return _graphics_pipelines;
281 }
else if constexpr (std::is_same_v<Type, compute_pipeline>) {
282 return _compute_pipelines;
283 }
else if constexpr (std::is_same_v<Type, buffer>) {
285 }
else if constexpr (std::is_same_v<Type, storage_buffer>) {
286 return _storage_buffers;
287 }
else if constexpr (std::is_same_v<Type, uniform_buffer>) {
288 return _uniform_buffers;
289 }
if constexpr (std::is_same_v<Type, image2d>) {
291 }
else if constexpr (std::is_same_v<Type, depth_image>) {
292 return _depth_images;
293 }
else if constexpr (std::is_same_v<Type, cube_image>) {
297 utility::assert_that(
false,
"Invalid resource type");
300 std::unique_ptr<graphics::instance> _instance{};
301 std::unique_ptr<graphics::physical_device> _physical_device{};
302 std::unique_ptr<graphics::logical_device> _logical_device{};
304 std::unordered_map<command_pool_key, std::shared_ptr<graphics::command_pool>, command_pool_key_hash, command_pool_key_equality> _command_pools{};
306 std::map<std::string, memory::observer_ptr<const descriptor>> _attachments{};
308 std::unique_ptr<graphics::surface> _surface{};
310 std::unique_ptr<graphics::swapchain> _swapchain{};
312 std::vector<per_frame_data> _per_frame_data{};
313 std::vector<per_image_data> _per_image_data{};
314 std::vector<graphics::command_buffer> _graphics_command_buffers{};
315 std::vector<graphics::command_buffer> _compute_command_buffers{};
317 std::unique_ptr<graphics::renderer> _renderer{};
333 std::vector<command_buffer::acquire_ownership_data> _acquire_ownership_data;
334 std::vector<command_buffer::release_ownership_data> _release_ownership_data;
336 std::uint32_t _current_frame{};
337 bool _is_framebuffer_resized{};
338 bool _is_viewport_resized{};
Definition: delegate.hpp:55
Definition: module.hpp:90
Definition: allocator.hpp:12
Definition: render_graph.hpp:103
Definition: buffer.hpp:20
Definition: command_pool.hpp:12
Definition: compiler.hpp:18
Definition: descriptor.hpp:37
Module for managing rendering specific tasks.
Definition: graphics_module.hpp:68
Definition: instance.hpp:10
Definition: logical_device.hpp:60
Definition: physical_device.hpp:16
Definition: resource_storage.hpp:17
Definition: resource_storage.hpp:67
Definition: surface.hpp:14
Definition: swapchain.hpp:12
A vector in two-dimensional space.
Definition: vector2.hpp:27
Definition: signal.hpp:16
Definition: command_buffer.hpp:43
Definition: command_buffer.hpp:33