TGUI 1.14-dev
Loading...
Searching...
No Matches
SwapchainVulkan.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_SWAPCHAIN_VULKAN_HPP
26#define TGUI_SWAPCHAIN_VULKAN_HPP
27
28#include <TGUI/Color.hpp>
29#include <TGUI/Vector2.hpp>
30
31#include <memory>
32#include <vector>
33#include <vulkan/vulkan.h>
34
36
37namespace tgui
38{
40
51 {
52 public:
53 // Amount of frames that can be executing on the GPU at the same time, which has to be passed to setRenderPass.
54 // With FIFO presentation, more frames would only increase the input latency.
55 static constexpr std::uint32_t FramesInFlight = 2;
56
65 SwapchainVulkan(std::shared_ptr<BackendRendererVulkan> renderer, VkSurfaceKHR surface);
66
71
72 SwapchainVulkan(const SwapchainVulkan&) = delete;
73 SwapchainVulkan& operator=(const SwapchainVulkan&) = delete;
74
85 [[nodiscard]] VkCommandBuffer beginFrame(Vector2u framebufferSize, Color clearColor);
86
90 void endFrame();
91
97 [[nodiscard]] VkRenderPass getRenderPass() const;
98
104 [[nodiscard]] VkFormat getColorFormat() const;
105
113 [[nodiscard]] bool isSurfaceLost() const;
114
116
117 private:
119 // Creates the render pass, command buffers and synchronization objects
121 void createVulkanObjects();
122
124 // Destroys the objects that were created in createVulkanObjects
126 void destroyVulkanObjects();
127
129 // Creates the swapchain and the objects that depend on it. Returns false if the window has no size.
131 bool recreateSwapchain(Vector2u framebufferSize);
132
134 // Destroys the image views, framebuffers and semaphores that depend on the swapchain images
136 void destroySwapchainImageResources();
137
139
140 private:
141 struct FrameSync
142 {
143 VkCommandBuffer cmdBuffer = VK_NULL_HANDLE;
144 VkFence inFlightFence = VK_NULL_HANDLE;
145 VkSemaphore imageAvailableSemaphore = VK_NULL_HANDLE;
146 };
147
148 std::shared_ptr<BackendRendererVulkan> m_renderer;
149 VkDevice m_device = VK_NULL_HANDLE;
150 VkSurfaceKHR m_surface = VK_NULL_HANDLE;
151 VkSurfaceFormatKHR m_surfaceFormat = {};
152 VkSwapchainKHR m_swapchain = VK_NULL_HANDLE;
153 VkExtent2D m_extent = {};
154 Vector2u m_framebufferSize; // Window size that was passed when the swapchain was last created
155 VkRenderPass m_renderPass = VK_NULL_HANDLE;
156 VkCommandPool m_commandPool = VK_NULL_HANDLE;
157
158 // One per swapchain image
159 std::vector<VkImageView> m_imageViews;
160 std::vector<VkFramebuffer> m_framebuffers;
161 std::vector<VkSemaphore> m_renderFinishedSemaphores;
162
163 std::vector<FrameSync> m_frames;
164 std::size_t m_currentFrame = 0;
165 std::uint32_t m_imageIndex = 0;
166 bool m_swapchainOutdated = false;
167 bool m_surfaceLost = false;
168 bool m_rotatedByCompositor = false;
169 };
170} // namespace tgui
171
172#endif // TGUI_SWAPCHAIN_VULKAN_HPP
Backend renderer that uses Vulkan.
Definition BackendRendererVulkan.hpp:72
Wrapper for colors.
Definition Color.hpp:63
bool isSurfaceLost() const
Returns whether the surface was lost and the swapchain can no longer be used.
void endFrame()
Ends the render pass, submits the command buffer and presents the frame.
VkFormat getColorFormat() const
Returns the format of the swapchain images.
SwapchainVulkan(std::shared_ptr< BackendRendererVulkan > renderer, VkSurfaceKHR surface)
Constructor.
VkRenderPass getRenderPass() const
Returns the render pass that is started by beginFrame.
VkCommandBuffer beginFrame(Vector2u framebufferSize, Color clearColor)
Starts a new frame.
~SwapchainVulkan()
Destructor.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37