Overte C++ Documentation
VulkanSwapChain.h
1 /*
2 * Class wrapping access to the swap chain
3 *
4 * A swap chain is a collection of framebuffers used for rendering and presentation to the windowing system
5 *
6 * Copyright (C) 2016-2024 by Sascha Willems - www.saschawillems.de
7 *
8 * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
9 */
10 
11 #pragma once
12 
13 #include <stdlib.h>
14 #include <string>
15 #include <assert.h>
16 #include <stdio.h>
17 #include <vector>
18 
19 #include <vulkan/vulkan.h>
20 #include "Context.h"
21 #include "VulkanTools.h"
22 
23 #ifdef __ANDROID__
24 #include "VulkanAndroid.h"
25 #endif
26 
27 #ifdef __APPLE__
28 #include <sys/utsname.h>
29 #endif
30 
31 typedef struct _SwapChainBuffers {
32  VkImage image;
33  VkImageView view;
34 } SwapChainBuffer;
35 
36 class VulkanSwapChain
37 {
38 private:
39  vks::Context *_context {nullptr};
40  VkSurfaceKHR surface;
41 public:
42  VkExtent2D extent{0, 0};
43  VkFormat colorFormat;
44  VkColorSpaceKHR colorSpace;
45  VkSwapchainKHR swapChain = VK_NULL_HANDLE;
46  uint32_t imageCount;
47  std::vector<VkImage> images;
48  std::vector<SwapChainBuffer> buffers;
49  uint32_t queueNodeIndex = UINT32_MAX;
50 
51 #if defined(VK_USE_PLATFORM_WIN32_KHR)
52  void initSurface(void* platformHandle, void* platformWindow);
53 #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
54  void initSurface(ANativeWindow* window);
55 #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
56  void initSurface(IDirectFB* dfb, IDirectFBSurface* window);
57 #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
58  void initSurface(wl_display* display, wl_surface* window);
59 #elif defined(VK_USE_PLATFORM_XCB_KHR)
60  void initSurface(xcb_connection_t* connection, xcb_window_t window);
61 #elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
62  void initSurface(void* view);
63 #elif defined(VK_USE_PLATFORM_METAL_EXT)
64  void initSurface(CAMetalLayer* metalLayer);
65 #elif (defined(_DIRECT2DISPLAY) || defined(VK_USE_PLATFORM_HEADLESS_EXT))
66  void initSurface(uint32_t width, uint32_t height);
67 #if defined(_DIRECT2DISPLAY)
68  void createDirect2DisplaySurface(uint32_t width, uint32_t height);
69 #endif
70 #elif defined(VK_USE_PLATFORM_SCREEN_QNX)
71  void initSurface(screen_context_t screen_context, screen_window_t screen_window);
72 #endif
73  /* Set the Vulkan objects required for swapchain creation and management, must be called before swapchain creation */
74  void setContext(vks::Context *context);
82  void create(uint32_t* width, uint32_t* height, bool vsync = false, bool fullscreen = false);
93  VkResult acquireNextImage(VkSemaphore presentCompleteSemaphore, uint32_t* imageIndex);
103  VkResult queuePresent(VkQueue queue, uint32_t imageIndex, VkSemaphore waitSemaphore = VK_NULL_HANDLE);
104  /* Free all Vulkan resources acquired by the swapchain */
105  void cleanup();
106 };