14 vk::SurfaceKHR surface,
32 uint32_t desired_image_count = surface_info.
image_count;
41 "Swapchain support inadequate (formats: {}, modes: {})",
46 vk::SurfaceFormatKHR surface_format;
51 if (hdr_format.has_value()) {
52 surface_format = hdr_format.value();
55 "HDR requested but no HDR-capable format/colorspace supported by device/surface. Falling back to default.");
65 "Chosen swapchain format: {}, color space: {}",
66 vk::to_string(surface_format.format),
67 vk::to_string(surface_format.colorSpace));
73 uint32_t image_count = std::max(desired_image_count, support.
capabilities.minImageCount);
75 image_count = std::min(image_count, support.
capabilities.maxImageCount);
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;
89 std::vector<uint32_t> queue_family_indices;
91 uint32_t present_family = queue_families.present_family.value();
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();
99 create_info.imageSharingMode = vk::SharingMode::eExclusive;
100 create_info.queueFamilyIndexCount = 0;
101 create_info.pQueueFamilyIndices =
nullptr;
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;
111 m_swapchain = device.createSwapchainKHR(create_info);
112 }
catch (
const vk::SystemError& e) {
114 "Failed to create swapchain: {}", e.what());
131 "Swapchain created: {}x{}, {} images, format {}",
132 extent.width, extent.height,
m_images.size(),
133 vk::to_string(surface_format.format));
142 "Cannot recreate swapchain: no context set");
168 device.destroyImageView(image_view);
181 vk::Semaphore signal_semaphore,
186 "Cannot acquire image: no context set");
193 auto result = device.acquireNextImageKHR(
199 if (result.result == vk::Result::eErrorOutOfDateKHR) {
203 if (result.result != vk::Result::eSuccess && result.result != vk::Result::eSuboptimalKHR) {
205 "Failed to acquire swapchain image");
211 }
catch (
const vk::SystemError& e) {
213 "Exception acquiring swapchain image: {}", e.what());
219 vk::Semaphore wait_semaphore,
220 vk::Queue present_queue)
224 "Cannot present: no context set");
230 vk::PresentInfoKHR present_info {};
231 present_info.waitSemaphoreCount = 1;
232 present_info.pWaitSemaphores = &wait_semaphore;
234 present_info.swapchainCount = 1;
236 present_info.pImageIndices = &image_index;
237 present_info.pResults =
nullptr;
240 auto result = queue.presentKHR(present_info);
242 if (result == vk::Result::eErrorOutOfDateKHR || result == vk::Result::eSuboptimalKHR) {
246 if (result != vk::Result::eSuccess) {
248 "Failed to present swapchain image");
254 }
catch (
const vk::SystemError& e) {
256 "Exception presenting swapchain image: {}", e.what());
265 "Cannot create image views: no context set");
272 for (
size_t i = 0; i <
m_images.size(); i++) {
273 vk::ImageViewCreateInfo create_info {};
275 create_info.viewType = vk::ImageViewType::e2D;
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;
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;
291 }
catch (
const vk::SystemError& e) {
293 "Failed to create image view {}: {}", i, e.what());
302 vk::PhysicalDevice physical_device,
303 vk::SurfaceKHR surface)
307 details.
capabilities = physical_device.getSurfaceCapabilitiesKHR(surface);
308 details.
formats = physical_device.getSurfaceFormatsKHR(surface);
309 details.
present_modes = physical_device.getSurfacePresentModesKHR(surface);
315 const std::vector<vk::SurfaceFormatKHR>& available_formats,
322 for (
const auto& format : available_formats) {
323 if (format.format == vk_format && format.colorSpace == vk_color_space) {
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");
337 "Desired format not available, falling back to: {}",
338 vk::to_string(available_formats[0].format));
339 return available_formats[0];
343 const std::vector<vk::PresentModeKHR>& available_modes,
348 for (
const auto& mode : available_modes) {
349 if (mode == vk_mode) {
355 for (
const auto& mode : available_modes) {
356 if (mode == vk::PresentModeKHR::eMailbox) {
358 "Desired present mode not available, using MAILBOX");
362 for (
const auto& mode : available_modes) {
363 if (mode == vk::PresentModeKHR::eImmediate) {
365 "Desired present mode not available, using IMMEDIATE");
372 "Desired present mode not available, falling back to FIFO (VSync)");
373 return vk::PresentModeKHR::eFifo;
377 const std::vector<vk::SurfaceFormatKHR>& available_formats)
const
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 },
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;
397 return selected_format;
404 const vk::SurfaceCapabilitiesKHR& capabilities,
406 uint32_t height)
const
408 if (capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max()) {
409 return capabilities.currentExtent;
411 vk::Extent2D actual_extent = { width, height };
413 actual_extent.width = std::clamp(
415 capabilities.minImageExtent.width,
416 capabilities.maxImageExtent.width);
418 actual_extent.height = std::clamp(
419 actual_extent.height,
420 capabilities.minImageExtent.height,
421 capabilities.maxImageExtent.height);
423 return actual_extent;
#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.
const GraphicsSurfaceInfo & get_surface_info() const
Get graphics surface info.
vk::Queue get_graphics_queue() const
Get graphics queue.
vk::PhysicalDevice get_physical_device() const
Get physical device.
const QueueFamilyIndices & get_queue_families() const
Get queue family indices.
High-level wrapper for Vulkan instance and device.
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.
vk::Format m_image_format
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.
@ MAILBOX
Triple buffering, no tear.
@ IMMEDIATE
No vsync, tear possible.
SurfaceFormat format
Default surface format for new windows.
System-wide configuration for visual stream processing.
std::optional< uint32_t > graphics_family
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.