Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifndef INCLUDED_RENDERER_BACKEND_VULKAN_SWAPCHAIN 19 : #define INCLUDED_RENDERER_BACKEND_VULKAN_SWAPCHAIN 20 : 21 : #include "renderer/backend/IFramebuffer.h" 22 : #include "renderer/backend/ISwapChain.h" 23 : #include "renderer/backend/vulkan/SubmitScheduler.h" 24 : 25 : #include <cstddef> 26 : #include <cstdint> 27 : #include <glad/vulkan.h> 28 : #include <limits> 29 : #include <memory> 30 : #include <tuple> 31 : #include <unordered_map> 32 : #include <vector> 33 : 34 : namespace Renderer::Backend::Vulkan { class CDevice; } 35 : namespace Renderer::Backend::Vulkan { class CFramebuffer; } 36 : namespace Renderer::Backend::Vulkan { class CRingCommandContext; } 37 : namespace Renderer::Backend::Vulkan { class CTexture; } 38 : 39 : namespace Renderer 40 : { 41 : 42 : namespace Backend 43 : { 44 : 45 : namespace Vulkan 46 : { 47 : 48 : class CSwapChain final : public ISwapChain 49 : { 50 : public: 51 : ~CSwapChain() override; 52 : 53 : IDevice* GetDevice() override; 54 : 55 0 : bool IsValid() const override { return m_IsValid; } 56 : 57 : bool AcquireNextBackbuffer() override; 58 : 59 : IFramebuffer* GetCurrentBackbuffer( 60 : const AttachmentLoadOp colorAttachmentLoadOp, 61 : const AttachmentStoreOp colorAttachmentStoreOp, 62 : const AttachmentLoadOp depthStencilAttachmentLoadOp, 63 : const AttachmentStoreOp depthStencilAttachmentStoreOp) override; 64 : 65 : void Present() override; 66 : 67 0 : VkSwapchainKHR GetVkSwapchain() { return m_SwapChain; } 68 : 69 : bool AcquireNextImage(); 70 : void SubmitCommandsAfterAcquireNextImage( 71 : CRingCommandContext& commandContext); 72 : void SubmitCommandsBeforePresent( 73 : CRingCommandContext& commandContext); 74 : void Present(VkSemaphore submitDone, VkQueue queue); 75 : 76 : CTexture* GetCurrentBackbufferTexture(); 77 : 78 : CTexture* GetOrCreateBackbufferReadbackTexture(); 79 : 80 : CTexture* GetDepthTexture() { return m_DepthTexture.get(); } 81 : 82 : private: 83 : friend class CDevice; 84 : 85 : static std::unique_ptr<CSwapChain> Create( 86 : CDevice* device, CSubmitScheduler* submitScheduler, 87 : const uint32_t queueFamilyIndex, VkQueue queue, 88 : const char* name, VkSurfaceKHR surface, 89 : int surfaceDrawableWidth, int surfaceDrawableHeight, 90 : const bool vsync, std::unique_ptr<ISwapChain> oldSwapChain); 91 : 92 : CSwapChain(); 93 : 94 : CDevice* m_Device = nullptr; 95 : CSubmitScheduler* m_SubmitScheduler{nullptr}; 96 : VkQueue m_Queue{VK_NULL_HANDLE}; 97 : 98 : bool m_IsValid = false; 99 : VkSwapchainKHR m_SwapChain = VK_NULL_HANDLE; 100 : 101 : uint32_t m_CurrentImageIndex = std::numeric_limits<uint32_t>::max(); 102 : 103 : std::vector<VkImage> m_Images; 104 : std::vector<std::unique_ptr<CTexture>> m_Textures; 105 : std::unique_ptr<CTexture> m_DepthTexture; 106 : VkFormat m_ImageFormat = VK_FORMAT_UNDEFINED; 107 : 108 : uint32_t m_FrameID{0}; 109 : 110 : // We can't reuse the same acquire semaphore immediately after present 111 : // because it might still be processing on GPU as vkQueuePresentKHR doesn't 112 : // have to be blocking. 113 : // We need to wait for the image on GPU to draw to it. 114 : struct FrameObject 115 : { 116 : VkSemaphore acquireImageSemaphore{VK_NULL_HANDLE}; 117 : // We need to know when we can reuse the semaphore. 118 : CSubmitScheduler::SubmitHandle submitHandle{CSubmitScheduler::INVALID_SUBMIT_HANDLE}; 119 : }; 120 : std::vector<FrameObject> m_FrameObjects; 121 : 122 : // The number of submit semaphores should be equal to the number of images 123 : // in the swapchain. We could use NUMBER_OF_FRAMES_IN_FLIGHT of objects but 124 : // it might be not safe. Since vkQueuePresentKHR doesn't provide a way to 125 : // tell that a semaphore was signaled. 126 : // 127 : // A possible situation, list of acquired indices: 128 : // 0, 1, 2, 0, 1, 0, 1 129 : // ^ 130 : // In theory in the end we might still have a semaphore in use for the 131 : // 2nd swapchain image. 132 : // 133 : // See also: 134 : // https://docs.vulkan.org/guide/latest/swapchain_semaphore_reuse.html 135 : // We need to present only after all submit work is done. 136 : std::vector<VkSemaphore> m_SubmitSemaphores; 137 : 138 0 : struct SwapChainBackbuffer 139 : { 140 : using BackbufferKey = std::tuple< 141 : AttachmentLoadOp, AttachmentStoreOp, 142 : AttachmentLoadOp, AttachmentStoreOp>; 143 : struct BackbufferKeyHash 144 : { 145 : size_t operator()(const BackbufferKey& key) const; 146 : }; 147 : std::unordered_map< 148 : BackbufferKey, std::unique_ptr<CFramebuffer>, BackbufferKeyHash> backbuffers; 149 : 150 : SwapChainBackbuffer(); 151 : 152 : SwapChainBackbuffer(const SwapChainBackbuffer&) = delete; 153 : SwapChainBackbuffer& operator=(const SwapChainBackbuffer&) = delete; 154 : 155 : SwapChainBackbuffer(SwapChainBackbuffer&& other); 156 : SwapChainBackbuffer& operator=(SwapChainBackbuffer&& other); 157 : }; 158 : std::vector<SwapChainBackbuffer> m_Backbuffers; 159 : 160 : std::unique_ptr<CTexture> m_BackbufferReadbackTexture; 161 : 162 : std::unique_ptr<CRingCommandContext> m_AcquireCommandContext; 163 : std::unique_ptr<CRingCommandContext> m_PresentCommandContext; 164 : 165 : bool m_DebugWaitIdleBeforeAcquire = false; 166 : bool m_DebugWaitIdleBeforePresent = false; 167 : bool m_DebugWaitIdleAfterPresent = false; 168 : }; 169 : 170 : } // namespace Vulkan 171 : 172 : } // namespace Backend 173 : 174 : } // namespace Renderer 175 : 176 : #endif // INCLUDED_RENDERER_BACKEND_VULKAN_SWAPCHAIN