MCPcopy Create free account
hub / github.com/KhronosGroup/Vulkan-Samples / init_swapchain

Method init_swapchain

samples/api/hello_triangle/hello_triangle.cpp:462–595  ·  view source on GitHub ↗

* @brief Initializes the Vulkan swapchain. */

Source from the content-addressed store, hash-verified

460 * @brief Initializes the Vulkan swapchain.
461 */
462void HelloTriangle::init_swapchain()
463{
464 VkSurfaceCapabilitiesKHR surface_properties;
465 VK_CHECK(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(context.gpu, context.surface, &surface_properties));
466
467 VkSurfaceFormatKHR format = vkb::select_surface_format(context.gpu, context.surface);
468
469 VkExtent2D swapchain_size{};
470 if (surface_properties.currentExtent.width == 0xFFFFFFFF)
471 {
472 swapchain_size.width = context.swapchain_dimensions.width;
473 swapchain_size.height = context.swapchain_dimensions.height;
474 }
475 else
476 {
477 swapchain_size = surface_properties.currentExtent;
478 }
479
480 // FIFO must be supported by all implementations.
481 VkPresentModeKHR swapchain_present_mode = VK_PRESENT_MODE_FIFO_KHR;
482
483 // Determine the number of VkImage's to use in the swapchain.
484 // Ideally, we desire to own 1 image at a time, the rest of the images can
485 // either be rendered to and/or being queued up for display.
486 uint32_t desired_swapchain_images = surface_properties.minImageCount + 1;
487 if ((surface_properties.maxImageCount > 0) && (desired_swapchain_images > surface_properties.maxImageCount))
488 {
489 // Application must settle for fewer images than desired.
490 desired_swapchain_images = surface_properties.maxImageCount;
491 }
492
493 // Figure out a suitable surface transform.
494 VkSurfaceTransformFlagBitsKHR pre_transform;
495 if (surface_properties.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR)
496 {
497 pre_transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
498 }
499 else
500 {
501 pre_transform = surface_properties.currentTransform;
502 }
503
504 VkSwapchainKHR old_swapchain = context.swapchain;
505
506 // Find a supported composite type.
507 VkCompositeAlphaFlagBitsKHR composite = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
508 if (surface_properties.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR)
509 {
510 composite = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
511 }
512 else if (surface_properties.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR)
513 {
514 composite = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
515 }
516 else if (surface_properties.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR)
517 {
518 composite = VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
519 }

Callers

nothing calls this directly

Calls 3

select_surface_formatFunction · 0.50
clearMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected