2#ifndef LIBSBX_GRAPHICS_GRAPHICS_MODULE_HPP_
3#define LIBSBX_GRAPHICS_GRAPHICS_MODULE_HPP_
6#include <unordered_map>
11#include <magic_enum/magic_enum.hpp>
13#include <libsbx/core/module.hpp>
16#include <libsbx/math/uuid.hpp>
18#include <libsbx/memory/tracking_allocator.hpp>
20#include <libsbx/devices/devices_module.hpp>
23#include <libsbx/utility/concepts.hpp>
24#include <libsbx/utility/enum.hpp>
28#include <libsbx/signals/signal.hpp>
30#include <libsbx/filesystem/filesystem_module.hpp>
32#include <libsbx/graphics/devices/instance.hpp>
33#include <libsbx/graphics/devices/physical_device.hpp>
34#include <libsbx/graphics/devices/logical_device.hpp>
35#include <libsbx/graphics/devices/allocator.hpp>
36#include <libsbx/graphics/devices/surface.hpp>
37#include <libsbx/graphics/devices/query_pool.hpp>
39#include <libsbx/graphics/commands/command_pool.hpp>
40#include <libsbx/graphics/commands/command_buffer.hpp>
42#include <libsbx/graphics/render_pass/swapchain.hpp>
44#include <libsbx/graphics/pipeline/pipeline.hpp>
45#include <libsbx/graphics/pipeline/shader.hpp>
46#include <libsbx/graphics/pipeline/graphics_pipeline.hpp>
47#include <libsbx/graphics/pipeline/compute_pipeline.hpp>
48#include <libsbx/graphics/pipeline/compiler.hpp>
50#include <libsbx/graphics/buffers/buffer.hpp>
51#include <libsbx/graphics/buffers/storage_buffer.hpp>
53#include <libsbx/graphics/images/image2d.hpp>
54#include <libsbx/graphics/images/cube_image.hpp>
55#include <libsbx/graphics/images/sampler_state.hpp>
56#include <libsbx/graphics/images/depth_image.hpp>
58#include <libsbx/graphics/renderer.hpp>
59#include <libsbx/graphics/viewport_registry.hpp>
60#include <libsbx/graphics/profiler.hpp>
62#include <libsbx/graphics/resource_storage.hpp>
64namespace sbx::graphics {
71auto validate(VkResult result) -> void;
73template<
typename VkEnum,
typename Enum>
74requires ((std::is_enum_v<VkEnum> || std::is_same_v<VkEnum, VkFlags>) && std::is_enum_v<Enum> && std::is_same_v<std::underlying_type_t<Enum>, std::int32_t>)
75constexpr auto to_vk_enum(Enum value) -> VkEnum {
76 return static_cast<VkEnum
>(value);
81 std::uint32_t frames_to_live;
91 inline static const auto is_registered = register_module(stage::rendering, dependencies<devices::devices_module, filesystem::filesystem_module>{});
93 inline static constexpr auto max_deletion_queue_size = std::size_t{16u};
101 auto update() ->
void override;
111 auto command_pool(
const queue::type type,
const std::thread::id& thread_id = std::this_thread::get_id()) ->
const std::shared_ptr<command_pool>&;
115 template<utility::implements<renderer> Renderer,
typename... Args>
116 requires (std::is_constructible_v<Renderer, Args...>)
117 auto set_renderer(Args&&... args) -> Renderer& {
118 _renderer = std::make_unique<Renderer>(std::forward<Args>(args)...);
120 _recreate_swapchain();
122 return *
static_cast<Renderer*
>(_renderer.get());
125 auto current_frame()
const noexcept -> std::uint32_t {
126 return _current_frame;
131 template<
typename Type,
typename... Args>
132 requires (std::is_constructible_v<Type, Args...>)
134 return _storage<Type>().emplace(std::forward<Args>(args)...);
137 template<
typename Type>
139 return _storage<Type>().get(handle);
142 template<
typename Type>
144 return _storage<Type>().get(handle);
147 template<
typename Type>
149 return _storage<Type>().remove(handle);
156 template<
typename Type = graphics::renderer>
158 return *
static_cast<Type*
>(_renderer.get());
161 template<queue::type Source, queue::type Destination,
typename Type>
162 requires (std::is_same_v<Type, graphics::buffer> || std::is_same_v<Type, graphics::storage_buffer>)
163 auto transfer_ownership(
const resource_handle<Type>& handle,
const VkPipelineStageFlagBits2 stage = VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT) ->
void {
164 auto&
buffer = get_resource<Type>(handle);
167 .src_stage_mask = VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT,
168 .src_access_mask = VK_ACCESS_2_SHADER_WRITE_BIT,
169 .src_queue_family = _logical_device->queue<Source>().family(),
170 .dst_queue_family = _logical_device->queue<Destination>().family(),
175 .dst_stage_mask = stage,
176 .dst_access_mask = _access_mask_from_stage(stage),
177 .src_queue_family = _logical_device->queue<Source>().family(),
178 .dst_queue_family = _logical_device->queue<Destination>().family(),
195 template<
typename Callable>
196 requires (std::is_invocable_r_v<void, Callable, graphics::allocator&>)
197 auto enqueue_destruction(Callable&& callable) ->
void {
198 _deletion_queue.push_back({std::forward<Callable>(callable), graphics::swapchain::max_frames_in_flight});
203 static constexpr auto _access_mask_from_stage(VkPipelineStageFlagBits2 stage) -> VkAccessFlagBits2 {
205 case VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT: {
206 return VK_ACCESS_2_INDIRECT_COMMAND_READ_BIT;
208 case VK_PIPELINE_STAGE_2_VERTEX_SHADER_BIT:
209 case VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT:
210 case VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT:
211 case VK_PIPELINE_STAGE_2_MESH_SHADER_BIT_EXT:
212 case VK_PIPELINE_STAGE_2_TASK_SHADER_BIT_EXT: {
213 return VK_ACCESS_2_SHADER_READ_BIT;
215 case VK_PIPELINE_STAGE_2_TRANSFER_BIT: {
216 return VK_ACCESS_2_TRANSFER_READ_BIT;
219 return VK_ACCESS_2_MEMORY_READ_BIT;
224 auto _reset_render_stages() -> void;
226 auto _recreate_swapchain() -> void;
228 auto _recreate_per_frame_data() -> void;
230 auto _recreate_per_image_data() -> void;
232 auto _recreate_command_buffers() -> void;
234 auto _recreate_attachments() -> void;
236 struct per_frame_data {
238 VkSemaphore image_available_semaphore{
nullptr};
239 VkFence graphics_in_flight_fence{
nullptr};
241 VkSemaphore compute_finished_semaphore{
nullptr};
242 VkFence compute_in_flight_fence{
nullptr};
245 std::vector<std::string> active_scopes;
248 struct per_image_data {
249 VkSemaphore render_finished_semaphore{
nullptr};
252 struct command_pool_key {
254 std::thread::id thread_id;
257 struct command_pool_key_hash {
258 auto operator()(
const command_pool_key& key)
const noexcept -> std::size_t {
259 auto hast = std::size_t{0};
260 utility::hash_combine(hast, key.type, key.thread_id);
265 struct command_pool_key_equality {
266 auto operator()(
const command_pool_key& lhs,
const command_pool_key& rhs)
const noexcept ->
bool {
267 return lhs.type == rhs.type && lhs.thread_id == rhs.thread_id;
272 static_assert(std::is_class_v<graphics::graphics_pipeline>,
"graphics_pipeline is not a class in sbx::graphics");
274 template<
typename Type>
276 if constexpr (std::is_same_v<Type, shader>) {
278 }
else if constexpr (std::is_same_v<Type, graphics_pipeline>) {
279 return _graphics_pipelines;
280 }
else if constexpr (std::is_same_v<Type, compute_pipeline>) {
281 return _compute_pipelines;
282 }
else if constexpr (std::is_same_v<Type, buffer>) {
284 }
else if constexpr (std::is_same_v<Type, storage_buffer>) {
285 return _storage_buffers;
286 }
else if constexpr (std::is_same_v<Type, uniform_buffer>) {
287 return _uniform_buffers;
288 }
else if constexpr (std::is_same_v<Type, image2d>) {
290 }
else if constexpr (std::is_same_v<Type, depth_image>) {
291 return _depth_images;
292 }
else if constexpr (std::is_same_v<Type, cube_image>) {
294 }
else if constexpr (std::is_same_v<Type, sampler_state>) {
295 return _sampler_states;
297 static_assert(!
sizeof(Type),
"Invalid resource type");
301 template<
typename Type>
303 if constexpr (std::is_same_v<Type, shader>) {
305 }
else if constexpr (std::is_same_v<Type, graphics_pipeline>) {
306 return _graphics_pipelines;
307 }
else if constexpr (std::is_same_v<Type, compute_pipeline>) {
308 return _compute_pipelines;
309 }
else if constexpr (std::is_same_v<Type, buffer>) {
311 }
else if constexpr (std::is_same_v<Type, storage_buffer>) {
312 return _storage_buffers;
313 }
else if constexpr (std::is_same_v<Type, uniform_buffer>) {
314 return _uniform_buffers;
315 }
else if constexpr (std::is_same_v<Type, image2d>) {
317 }
else if constexpr (std::is_same_v<Type, depth_image>) {
318 return _depth_images;
319 }
else if constexpr (std::is_same_v<Type, cube_image>) {
321 }
else if constexpr (std::is_same_v<Type, sampler_state>) {
322 return _sampler_states;
324 static_assert(!
sizeof(Type),
"Invalid resource type");
328 auto _poll_deletion_queue() -> void;
330 std::unique_ptr<graphics::instance> _instance{};
331 std::unique_ptr<graphics::physical_device> _physical_device{};
332 std::unique_ptr<graphics::logical_device> _logical_device{};
334 std::unordered_map<command_pool_key, std::shared_ptr<graphics::command_pool>, command_pool_key_hash, command_pool_key_equality> _command_pools{};
336 std::map<std::string, memory::observer_ptr<const descriptor>> _attachments{};
338 std::unique_ptr<graphics::surface> _surface{};
340 std::unique_ptr<graphics::swapchain> _swapchain{};
342 std::array<per_frame_data, swapchain::max_frames_in_flight> _per_frame_data{};
343 std::vector<per_image_data> _per_image_data{};
344 std::vector<graphics::command_buffer> _graphics_command_buffers;
345 std::vector<graphics::command_buffer> _compute_command_buffers;
347 std::unique_ptr<graphics::renderer> _renderer{};
364 std::vector<command_buffer::buffer_acquire_data> _acquire_ownership_data;
365 std::vector<command_buffer::buffer_release_data> _release_ownership_data;
367 std::uint32_t _current_frame{};
368 bool _is_framebuffer_resized{};
372 std::deque<deferred_deletion> _deletion_queue;
Definition: delegate.hpp:56
Definition: module.hpp:92
Definition: allocator.hpp:13
Definition: render_graph.hpp:94
Definition: buffer.hpp:21
Definition: command_pool.hpp:13
Definition: compiler.hpp:19
Definition: descriptor.hpp:38
Module for managing rendering specific tasks.
Definition: graphics_module.hpp:89
Definition: instance.hpp:11
Definition: logical_device.hpp:61
Definition: physical_device.hpp:17
Definition: renderer.hpp:43
Definition: resource_storage.hpp:18
Definition: resource_storage.hpp:66
Definition: surface.hpp:15
Definition: swapchain.hpp:15
Definition: viewport_registry.hpp:16
Definition: command_buffer.hpp:34
Definition: command_buffer.hpp:44
Definition: graphics_module.hpp:79