MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
VKSwapchain.cpp
Go to the documentation of this file.
1#include "VKSwapchain.hpp"
3
4#include "VKEnumUtils.hpp"
5
6namespace MayaFlux::Core {
7
12
14 vk::SurfaceKHR surface,
15 const WindowCreateInfo& window_config)
16{
17 m_context = &context;
18 m_surface = surface;
19
20 const GraphicsSurfaceInfo& surface_info = m_context->get_surface_info();
21
22 GraphicsSurfaceInfo::SurfaceFormat desired_format = window_config.surface_format.has_value()
23 ? window_config.surface_format.value()
24 : surface_info.format;
25
26 GraphicsSurfaceInfo::ColorSpace desired_color_space = surface_info.color_space;
27
28 GraphicsSurfaceInfo::PresentMode desired_present_mode = window_config.present_mode.has_value()
29 ? window_config.present_mode.value()
30 : surface_info.present_mode;
31
32 uint32_t desired_image_count = surface_info.image_count;
33
34 vk::PhysicalDevice physical_device = m_context->get_physical_device();
35 vk::Device device = m_context->get_device();
36
37 SwapchainSupportDetails support = query_support(physical_device, surface);
38
39 if (!support.is_adequate()) {
41 "Swapchain support inadequate (formats: {}, modes: {})",
42 support.formats.size(), support.present_modes.size());
43 return false;
44 }
45
46 vk::SurfaceFormatKHR surface_format;
47
48 if (surface_info.enable_hdr) {
49 auto hdr_format = find_hdr_format(support.formats);
50
51 if (hdr_format.has_value()) {
52 surface_format = hdr_format.value();
53 } else {
55 "HDR requested but no HDR-capable format/colorspace supported by device/surface. Falling back to default.");
56
57 surface_format = choose_surface_format(support.formats, desired_format, desired_color_space);
58 }
59
60 } else {
61 surface_format = choose_surface_format(support.formats, desired_format, desired_color_space);
62 }
63
65 "Chosen swapchain format: {}, color space: {}",
66 vk::to_string(surface_format.format),
67 vk::to_string(surface_format.colorSpace));
68
69 vk::PresentModeKHR present_mode = choose_present_mode(
70 support.present_modes, desired_present_mode);
71 vk::Extent2D extent = choose_extent(support.capabilities, window_config.width, window_config.height);
72
73 uint32_t image_count = std::max(desired_image_count, support.capabilities.minImageCount);
74 if (support.capabilities.maxImageCount > 0) {
75 image_count = std::min(image_count, support.capabilities.maxImageCount);
76 }
77
78 vk::SwapchainCreateInfoKHR create_info {};
79 create_info.surface = surface;
80 create_info.minImageCount = image_count;
81 create_info.imageFormat = surface_format.format;
82 create_info.imageColorSpace = surface_format.colorSpace;
83 create_info.imageExtent = extent;
84 create_info.imageArrayLayers = 1;
85 create_info.imageUsage = vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eTransferDst;
86
87 const auto& queue_families = m_context->get_queue_families();
88
89 std::vector<uint32_t> queue_family_indices;
90 uint32_t graphics_family = queue_families.graphics_family.value();
91 uint32_t present_family = queue_families.present_family.value();
92
93 if (graphics_family != present_family) {
94 queue_family_indices = { graphics_family, present_family };
95 create_info.imageSharingMode = vk::SharingMode::eConcurrent;
96 create_info.queueFamilyIndexCount = static_cast<uint32_t>(queue_family_indices.size());
97 create_info.pQueueFamilyIndices = queue_family_indices.data();
98 } else {
99 create_info.imageSharingMode = vk::SharingMode::eExclusive;
100 create_info.queueFamilyIndexCount = 0;
101 create_info.pQueueFamilyIndices = nullptr;
102 }
103
104 create_info.preTransform = support.capabilities.currentTransform;
105 create_info.compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque;
106 create_info.presentMode = present_mode;
107 create_info.clipped = VK_TRUE;
108 create_info.oldSwapchain = nullptr;
109
110 try {
111 m_swapchain = device.createSwapchainKHR(create_info);
112 } catch (const vk::SystemError& e) {
114 "Failed to create swapchain: {}", e.what());
115 return false;
116 }
117
118 m_images = device.getSwapchainImagesKHR(m_swapchain);
119
120 m_image_format = surface_format.format;
121 m_extent = extent;
122
123 m_window_config = &window_config;
124
125 if (!create_image_views()) {
127 return false;
128 }
129
131 "Swapchain created: {}x{}, {} images, format {}",
132 extent.width, extent.height, m_images.size(),
133 vk::to_string(surface_format.format));
134
135 return true;
136}
137
138bool VKSwapchain::recreate(uint32_t width, uint32_t height)
139{
140 if (!m_context) {
142 "Cannot recreate swapchain: no context set");
143 return false;
144 }
145
146 m_context->get_device().waitIdle();
147
149
151}
152
154{
156 m_context = nullptr;
157}
158
160{
161 if (!m_context) {
162 return;
163 }
164
165 vk::Device device = m_context->get_device();
166
167 for (auto image_view : m_image_views) {
168 device.destroyImageView(image_view);
169 }
170 m_image_views.clear();
171
172 if (m_swapchain) {
173 device.destroySwapchainKHR(m_swapchain);
174 m_swapchain = nullptr;
175 }
176
177 m_images.clear();
178}
179
180std::optional<uint32_t> VKSwapchain::acquire_next_image(
181 vk::Semaphore signal_semaphore,
182 uint64_t timeout_ns)
183{
184 if (!m_context) {
186 "Cannot acquire image: no context set");
187 return std::nullopt;
188 }
189
190 vk::Device device = m_context->get_device();
191
192 try {
193 auto result = device.acquireNextImageKHR(
195 timeout_ns,
196 signal_semaphore,
197 nullptr);
198
199 if (result.result == vk::Result::eErrorOutOfDateKHR) {
200 return std::nullopt;
201 }
202
203 if (result.result != vk::Result::eSuccess && result.result != vk::Result::eSuboptimalKHR) {
205 "Failed to acquire swapchain image");
206 return std::nullopt;
207 }
208
209 return result.value;
210
211 } catch (const vk::SystemError& e) {
213 "Exception acquiring swapchain image: {}", e.what());
214 return std::nullopt;
215 }
216}
217
218bool VKSwapchain::present(uint32_t image_index,
219 vk::Semaphore wait_semaphore,
220 vk::Queue present_queue)
221{
222 if (!m_context) {
224 "Cannot present: no context set");
225 return false;
226 }
227
228 vk::Queue queue = present_queue ? present_queue : m_context->get_graphics_queue();
229
230 vk::PresentInfoKHR present_info {};
231 present_info.waitSemaphoreCount = 1;
232 present_info.pWaitSemaphores = &wait_semaphore;
233
234 present_info.swapchainCount = 1;
235 present_info.pSwapchains = &m_swapchain;
236 present_info.pImageIndices = &image_index;
237 present_info.pResults = nullptr;
238
239 try {
240 auto result = queue.presentKHR(present_info);
241
242 if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR) {
243 return false;
244 }
245
246 if (result != vk::Result::eSuccess) {
248 "Failed to present swapchain image");
249 return false;
250 }
251
252 return true;
253
254 } catch (const vk::SystemError& e) {
256 "Exception presenting swapchain image: {}", e.what());
257 return false;
258 }
259}
260
262{
263 if (!m_context) {
265 "Cannot create image views: no context set");
266 return false;
267 }
268
269 vk::Device device = m_context->get_device();
270 m_image_views.resize(m_images.size());
271
272 for (size_t i = 0; i < m_images.size(); i++) {
273 vk::ImageViewCreateInfo create_info {};
274 create_info.image = m_images[i];
275 create_info.viewType = vk::ImageViewType::e2D;
276 create_info.format = m_image_format;
277
278 create_info.components.r = vk::ComponentSwizzle::eIdentity;
279 create_info.components.g = vk::ComponentSwizzle::eIdentity;
280 create_info.components.b = vk::ComponentSwizzle::eIdentity;
281 create_info.components.a = vk::ComponentSwizzle::eIdentity;
282
283 create_info.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
284 create_info.subresourceRange.baseMipLevel = 0;
285 create_info.subresourceRange.levelCount = 1;
286 create_info.subresourceRange.baseArrayLayer = 0;
287 create_info.subresourceRange.layerCount = 1;
288
289 try {
290 m_image_views[i] = device.createImageView(create_info);
291 } catch (const vk::SystemError& e) {
293 "Failed to create image view {}: {}", i, e.what());
294 return false;
295 }
296 }
297
298 return true;
299}
300
302 vk::PhysicalDevice physical_device,
303 vk::SurfaceKHR surface)
304{
306
307 details.capabilities = physical_device.getSurfaceCapabilitiesKHR(surface);
308 details.formats = physical_device.getSurfaceFormatsKHR(surface);
309 details.present_modes = physical_device.getSurfacePresentModesKHR(surface);
310
311 return details;
312}
313
315 const std::vector<vk::SurfaceFormatKHR>& available_formats,
317 GraphicsSurfaceInfo::ColorSpace desired_color_space) const
318{
319 vk::Format vk_format = to_vk_format(desired_format);
320 vk::ColorSpaceKHR vk_color_space = to_vk_color_space(desired_color_space);
321
322 for (const auto& format : available_formats) {
323 if (format.format == vk_format && format.colorSpace == vk_color_space) {
324 return format;
325 }
326 }
327
328 for (const auto& format : available_formats) {
329 if (format.format == vk_format) {
331 "Exact color space match not found, using format match with different color space");
332 return format;
333 }
334 }
335
337 "Desired format not available, falling back to: {}",
338 vk::to_string(available_formats[0].format));
339 return available_formats[0];
340}
341
343 const std::vector<vk::PresentModeKHR>& available_modes,
344 GraphicsSurfaceInfo::PresentMode desired_mode) const
345{
346 vk::PresentModeKHR vk_mode = to_vk_present_mode(desired_mode);
347
348 for (const auto& mode : available_modes) {
349 if (mode == vk_mode) {
350 return mode;
351 }
352 }
353
355 for (const auto& mode : available_modes) {
356 if (mode == vk::PresentModeKHR::eMailbox) {
358 "Desired present mode not available, using MAILBOX");
359 return mode;
360 }
361 }
362 for (const auto& mode : available_modes) {
363 if (mode == vk::PresentModeKHR::eImmediate) {
365 "Desired present mode not available, using IMMEDIATE");
366 return mode;
367 }
368 }
369 }
370
372 "Desired present mode not available, falling back to FIFO (VSync)");
373 return vk::PresentModeKHR::eFifo;
374}
375
376std::optional<vk::SurfaceFormatKHR> VKSwapchain::find_hdr_format(
377 const std::vector<vk::SurfaceFormatKHR>& available_formats) const
378{
379 vk::SurfaceFormatKHR selected_format;
380 std::vector<std::pair<vk::Format, vk::ColorSpaceKHR>> hdr_candidates = {
381 { vk::Format::eR16G16B16A16Sfloat, vk::ColorSpaceKHR::eHdr10St2084EXT },
382 { vk::Format::eR16G16B16A16Sfloat, vk::ColorSpaceKHR::eExtendedSrgbLinearEXT },
383 { vk::Format::eA2B10G10R10UnormPack32, vk::ColorSpaceKHR::eHdr10St2084EXT },
384 { vk::Format::eA2B10G10R10UnormPack32, vk::ColorSpaceKHR::eExtendedSrgbLinearEXT },
385 };
386
387 bool hdr_supported = false;
388 for (const auto& candidate : hdr_candidates) {
389 for (const auto& fmt : available_formats) {
390 if (fmt.format == candidate.first && fmt.colorSpace == candidate.second) {
391 selected_format = fmt;
392 hdr_supported = true;
393 break;
394 }
395 }
396 if (hdr_supported) {
397 return selected_format;
398 }
399 }
400 return std::nullopt;
401}
402
404 const vk::SurfaceCapabilitiesKHR& capabilities,
405 uint32_t width,
406 uint32_t height) const
407{
408 if (capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max()) {
409 return capabilities.currentExtent;
410 }
411 vk::Extent2D actual_extent = { width, height };
412
413 actual_extent.width = std::clamp(
414 actual_extent.width,
415 capabilities.minImageExtent.width,
416 capabilities.maxImageExtent.width);
417
418 actual_extent.height = std::clamp(
419 actual_extent.height,
420 capabilities.minImageExtent.height,
421 capabilities.maxImageExtent.height);
422
423 return actual_extent;
424}
425
426} // namespace MayaFlux::Core
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
vk::Device get_device() const
Get logical device.
Definition VKContext.hpp:49
const GraphicsSurfaceInfo & get_surface_info() const
Get graphics surface info.
vk::Queue get_graphics_queue() const
Get graphics queue.
Definition VKContext.hpp:54
vk::PhysicalDevice get_physical_device() const
Get physical device.
Definition VKContext.hpp:44
const QueueFamilyIndices & get_queue_families() const
Get queue family indices.
Definition VKContext.hpp:69
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.
bool create_image_views()
Create image views for swapchain images.
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.
bool recreate(uint32_t width, uint32_t height)
Recreate swapchain (for window resize)
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.
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
vk::Format to_vk_format(GraphicsSurfaceInfo::SurfaceFormat fmt)
vk::PresentModeKHR to_vk_present_mode(GraphicsSurfaceInfo::PresentMode mode)
vk::ColorSpaceKHR to_vk_color_space(GraphicsSurfaceInfo::ColorSpace space)
@ GraphicsBackend
Graphics/visual rendering backend (Vulkan, OpenGL)
@ Core
Core engine, backend, subsystems.
bool enable_hdr
Enable HDR output if available.
PresentMode present_mode
Default presentation mode for new windows.
ColorSpace color_space
Default color space for new windows.
ColorSpace
Default color space for window surfaces.
SurfaceFormat
Default pixel format for window surfaces (Vulkan-compatible)
uint32_t image_count
Default number of swapchain images (double/triple buffering)
PresentMode
Frame presentation strategy.
SurfaceFormat format
Default surface format for new windows.
System-wide configuration for visual stream processing.
std::optional< uint32_t > graphics_family
Definition VKDevice.hpp:14
std::vector< vk::PresentModeKHR > present_modes
std::vector< vk::SurfaceFormatKHR > formats
vk::SurfaceCapabilitiesKHR capabilities
Holds swapchain capability information for a physical device.
std::optional< GraphicsSurfaceInfo::PresentMode > present_mode
Override global present mode (nullopt = use global default)
std::optional< GraphicsSurfaceInfo::SurfaceFormat > surface_format
Override global surface format (nullopt = use global default)
uint32_t width
Initial window dimensions.
Configuration for creating a single window instance.