MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
VKSwapchain.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "VKContext.hpp"
4
5namespace MayaFlux::Core {
6
7class Window;
8
9/**
10 * @struct SwapchainSupportDetails
11 * @brief Holds swapchain capability information for a physical device
12 */
14 vk::SurfaceCapabilitiesKHR capabilities;
15 std::vector<vk::SurfaceFormatKHR> formats;
16 std::vector<vk::PresentModeKHR> present_modes;
17
18 [[nodiscard]] bool is_adequate() const
19 {
20 return !formats.empty() && !present_modes.empty();
21 }
22};
23
24/**
25 * @class VKSwapchain
26 * @brief Manages Vulkan swapchain using C++ API (vulkan.hpp)
27 */
29public:
30 VKSwapchain() = default;
32
33 VKSwapchain(const VKSwapchain&) = delete;
35
36 /**
37 * @brief Create swapchain for the given surface using VKContext
38 * @param context Vulkan context (provides device and configuration)
39 * @param surface Window surface
40 * @param window_config per-window configuration overrides
41 * @return True if creation succeeded
42 */
43 bool create(VKContext& context, vk::SurfaceKHR surface, const WindowCreateInfo& window_config);
44
45 /**
46 * @brief Recreate swapchain (for window resize)
47 * @param width New width
48 * @param height New height
49 * @return True if recreation succeeded
50 */
51 bool recreate(uint32_t width, uint32_t height);
52
53 /**
54 * @brief Cleanup swapchain resources
55 */
56 void cleanup();
57
58 /**
59 * @brief Acquire next image from swapchain
60 * @param signal_semaphore Semaphore to signal when image is acquired
61 * @param timeout_ns Timeout in nanoseconds (UINT64_MAX = no timeout)
62 * @return Image index, or empty if acquisition failed
63 */
64 std::optional<uint32_t> acquire_next_image(
65 vk::Semaphore signal_semaphore,
66 uint64_t timeout_ns = UINT64_MAX);
67
68 /**
69 * @brief Present image to screen
70 * @param image_index Index of image to present
71 * @param wait_semaphore Semaphore to wait on before presenting
72 * @param present_queue Queue to submit present operation to (optional, uses context's graphics queue if null)
73 * @return True if present succeeded, false if swapchain out of date
74 */
75 bool present(uint32_t image_index,
76 vk::Semaphore wait_semaphore,
77 vk::Queue present_queue = nullptr);
78
79 // Getters
80 [[nodiscard]] vk::SwapchainKHR get_swapchain() const { return m_swapchain; }
81 [[nodiscard]] vk::Format get_image_format() const { return m_image_format; }
82 [[nodiscard]] vk::Extent2D get_extent() const { return m_extent; }
83 [[nodiscard]] uint32_t get_image_count() const { return static_cast<uint32_t>(m_images.size()); }
84 [[nodiscard]] const std::vector<vk::Image>& get_images() const { return m_images; }
85 [[nodiscard]] const std::vector<vk::ImageView>& get_image_views() const { return m_image_views; }
86
87 /**
88 * @brief Query swapchain support details for a device
89 */
91 vk::PhysicalDevice physical_device,
92 vk::SurfaceKHR surface);
93
94private:
95 VKContext* m_context = nullptr; // Non-owning pointer to context
96 vk::SurfaceKHR m_surface;
97 const WindowCreateInfo* m_window_config = nullptr; // Store for recreation
98
99 vk::SwapchainKHR m_swapchain;
100 std::vector<vk::Image> m_images;
101 std::vector<vk::ImageView> m_image_views;
102
103 vk::Format m_image_format;
104 vk::Extent2D m_extent;
105
106 /**
107 * @brief Choose best surface format from available formats based on config
108 */
109 [[nodiscard]] vk::SurfaceFormatKHR choose_surface_format(
110 const std::vector<vk::SurfaceFormatKHR>& available_formats,
112 GraphicsSurfaceInfo::ColorSpace desired_color_space) const;
113
114 /**
115 * @brief Choose best present mode from available modes based on config
116 */
117 [[nodiscard]] vk::PresentModeKHR choose_present_mode(
118 const std::vector<vk::PresentModeKHR>& available_modes,
119 GraphicsSurfaceInfo::PresentMode desired_mode) const;
120
121 /**
122 * @brief Choose swap extent based on capabilities
123 */
124 [[nodiscard]] vk::Extent2D choose_extent(
125 const vk::SurfaceCapabilitiesKHR& capabilities,
126 uint32_t width,
127 uint32_t height) const;
128
129 /**
130 * @brief Find an HDR-capable format from available formats
131 */
132 [[nodiscard]] std::optional<vk::SurfaceFormatKHR> find_hdr_format(
133 const std::vector<vk::SurfaceFormatKHR>& available_formats) const;
134
135 /**
136 * @brief Create image views for swapchain images
137 */
138 bool create_image_views();
139
140 /**
141 * @brief Cleanup old swapchain during recreation
142 */
143 void cleanup_swapchain();
144};
145
146} // namespace MayaFlux::Core
High-level wrapper for Vulkan instance and device.
Definition VKContext.hpp:16
vk::Extent2D choose_extent(const vk::SurfaceCapabilitiesKHR &capabilities, uint32_t width, uint32_t height) const
Choose swap extent based on capabilities.
vk::PresentModeKHR choose_present_mode(const std::vector< vk::PresentModeKHR > &available_modes, GraphicsSurfaceInfo::PresentMode desired_mode) const
Choose best present mode from available modes based on config.
vk::Format get_image_format() const
vk::SwapchainKHR get_swapchain() const
const std::vector< vk::Image > & get_images() const
bool create_image_views()
Create image views for swapchain images.
VKSwapchain(const VKSwapchain &)=delete
vk::Extent2D get_extent() const
static SwapchainSupportDetails query_support(vk::PhysicalDevice physical_device, vk::SurfaceKHR surface)
Query swapchain support details for a device.
bool present(uint32_t image_index, vk::Semaphore wait_semaphore, vk::Queue present_queue=nullptr)
Present image to screen.
std::optional< uint32_t > acquire_next_image(vk::Semaphore signal_semaphore, uint64_t timeout_ns=UINT64_MAX)
Acquire next image from swapchain.
VKSwapchain & operator=(const VKSwapchain &)=delete
bool recreate(uint32_t width, uint32_t height)
Recreate swapchain (for window resize)
uint32_t get_image_count() const
void cleanup()
Cleanup swapchain resources.
std::optional< vk::SurfaceFormatKHR > find_hdr_format(const std::vector< vk::SurfaceFormatKHR > &available_formats) const
Find an HDR-capable format from available formats.
vk::SurfaceFormatKHR choose_surface_format(const std::vector< vk::SurfaceFormatKHR > &available_formats, GraphicsSurfaceInfo::SurfaceFormat desired_format, GraphicsSurfaceInfo::ColorSpace desired_color_space) const
Choose best surface format from available formats based on config.
void cleanup_swapchain()
Cleanup old swapchain during recreation.
const std::vector< vk::ImageView > & get_image_views() const
std::vector< vk::Image > m_images
bool create(VKContext &context, vk::SurfaceKHR surface, const WindowCreateInfo &window_config)
Create swapchain for the given surface using VKContext.
std::vector< vk::ImageView > m_image_views
const WindowCreateInfo * m_window_config
vk::SwapchainKHR m_swapchain
Manages Vulkan swapchain using C++ API (vulkan.hpp)
ColorSpace
Default color space for window surfaces.
SurfaceFormat
Default pixel format for window surfaces (Vulkan-compatible)
PresentMode
Frame presentation strategy.
std::vector< vk::PresentModeKHR > present_modes
std::vector< vk::SurfaceFormatKHR > formats
vk::SurfaceCapabilitiesKHR capabilities
Holds swapchain capability information for a physical device.
Configuration for creating a single window instance.