1 // Copyright 2016 Dolphin Emulator Project 2 // Licensed under GPLv2+ 3 // Refer to the license.txt file included. 4 5 #pragma once 6 7 #include <memory> 8 #include <vector> 9 10 #include "Common/CommonTypes.h" 11 #include "Common/WindowSystemInfo.h" 12 #include "VideoBackends/Vulkan/Constants.h" 13 #include "VideoCommon/TextureConfig.h" 14 15 namespace Vulkan 16 { 17 class CommandBufferManager; 18 class ObjectCache; 19 class VKTexture; 20 class VKFramebuffer; 21 22 class SwapChain 23 { 24 public: 25 SwapChain(const WindowSystemInfo& wsi, VkSurfaceKHR surface, bool vsync); 26 ~SwapChain(); 27 28 // Creates a vulkan-renderable surface for the specified window handle. 29 static VkSurfaceKHR CreateVulkanSurface(VkInstance instance, const WindowSystemInfo& wsi); 30 31 // Create a new swap chain from a pre-existing surface. 32 static std::unique_ptr<SwapChain> Create(const WindowSystemInfo& wsi, VkSurfaceKHR surface, 33 bool vsync); 34 GetSurface()35 VkSurfaceKHR GetSurface() const { return m_surface; } GetSurfaceFormat()36 VkSurfaceFormatKHR GetSurfaceFormat() const { return m_surface_format; } GetTextureFormat()37 AbstractTextureFormat GetTextureFormat() const { return m_texture_format; } IsVSyncEnabled()38 bool IsVSyncEnabled() const { return m_vsync_enabled; } IsStereoEnabled()39 bool IsStereoEnabled() const { return m_layers == 2; } GetSwapChain()40 VkSwapchainKHR GetSwapChain() const { return m_swap_chain; } GetWidth()41 u32 GetWidth() const { return m_width; } GetHeight()42 u32 GetHeight() const { return m_height; } GetCurrentImageIndex()43 u32 GetCurrentImageIndex() const { return m_current_swap_chain_image_index; } GetCurrentImage()44 VkImage GetCurrentImage() const 45 { 46 return m_swap_chain_images[m_current_swap_chain_image_index].image; 47 } GetCurrentTexture()48 VKTexture* GetCurrentTexture() const 49 { 50 return m_swap_chain_images[m_current_swap_chain_image_index].texture.get(); 51 } GetCurrentFramebuffer()52 VKFramebuffer* GetCurrentFramebuffer() const 53 { 54 return m_swap_chain_images[m_current_swap_chain_image_index].framebuffer.get(); 55 } 56 VkResult AcquireNextImage(); 57 58 bool RecreateSurface(void* native_handle); 59 bool ResizeSwapChain(); 60 bool RecreateSwapChain(); 61 62 // Change vsync enabled state. This may fail as it causes a swapchain recreation. 63 bool SetVSync(bool enabled); 64 65 // Is exclusive fullscreen supported? IsFullscreenSupported()66 bool IsFullscreenSupported() const { return m_fullscreen_supported; } 67 68 // Retrieves the "next" fullscreen state. Safe to call off-thread. GetCurrentFullscreenState()69 bool GetCurrentFullscreenState() const { return m_current_fullscreen_state; } GetNextFullscreenState()70 bool GetNextFullscreenState() const { return m_next_fullscreen_state; } SetNextFullscreenState(bool state)71 void SetNextFullscreenState(bool state) { m_next_fullscreen_state = state; } 72 73 // Updates the fullscreen state. Must call on-thread. 74 bool SetFullscreenState(bool state); 75 76 private: 77 bool SelectSurfaceFormat(); 78 bool SelectPresentMode(); 79 80 bool CreateSwapChain(); 81 void DestroySwapChain(); 82 83 bool SetupSwapChainImages(); 84 void DestroySwapChainImages(); 85 86 void DestroySurface(); 87 88 struct SwapChainImage 89 { 90 VkImage image; 91 std::unique_ptr<VKTexture> texture; 92 std::unique_ptr<VKFramebuffer> framebuffer; 93 }; 94 95 WindowSystemInfo m_wsi; 96 VkSurfaceKHR m_surface = VK_NULL_HANDLE; 97 VkSurfaceFormatKHR m_surface_format = {}; 98 VkPresentModeKHR m_present_mode = VK_PRESENT_MODE_RANGE_SIZE_KHR; 99 AbstractTextureFormat m_texture_format = AbstractTextureFormat::Undefined; 100 bool m_vsync_enabled = false; 101 bool m_fullscreen_supported = false; 102 bool m_current_fullscreen_state = false; 103 bool m_next_fullscreen_state = false; 104 105 VkSwapchainKHR m_swap_chain = VK_NULL_HANDLE; 106 std::vector<SwapChainImage> m_swap_chain_images; 107 u32 m_current_swap_chain_image_index = 0; 108 109 u32 m_width = 0; 110 u32 m_height = 0; 111 u32 m_layers = 0; 112 }; 113 114 } // namespace Vulkan 115