Pyrogenesis  trunk
SwapChain.h
Go to the documentation of this file.
1 /* Copyright (C) 2023 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 
22 
23 #include <glad/vulkan.h>
24 #include <memory>
25 #include <tuple>
26 #include <unordered_map>
27 #include <vector>
28 
29 namespace Renderer
30 {
31 
32 namespace Backend
33 {
34 
35 namespace Vulkan
36 {
37 
38 class CDevice;
39 class CFramebuffer;
40 class CRingCommandContext;
41 class CTexture;
42 
43 class CSwapChain final
44 {
45 public:
46  ~CSwapChain();
47 
48  VkSwapchainKHR GetVkSwapchain() { return m_SwapChain; }
49 
50  bool IsValid() const { return m_IsValid; }
51 
52  bool AcquireNextImage(VkSemaphore acquireImageSemaphore);
54  CRingCommandContext& commandContext);
56  CRingCommandContext& commandContext);
57  void Present(VkSemaphore submitDone, VkQueue queue);
58 
60  const AttachmentLoadOp colorAttachmentLoadOp,
61  const AttachmentStoreOp colorAttachmentStoreOp,
62  const AttachmentLoadOp depthStencilAttachmentLoadOp,
63  const AttachmentStoreOp depthStencilAttachmentStoreOp);
64 
66 
67 private:
68  friend class CDevice;
69 
70  static std::unique_ptr<CSwapChain> Create(
71  CDevice* device, VkSurfaceKHR surface, int surfaceDrawableWidth, int surfaceDrawableHeight,
72  std::unique_ptr<CSwapChain> oldSwapChain);
73 
74  CSwapChain();
75 
76  CDevice* m_Device = nullptr;
77 
78  bool m_IsValid = false;
79  VkSwapchainKHR m_SwapChain = VK_NULL_HANDLE;
80 
81  uint32_t m_CurrentImageIndex = std::numeric_limits<uint32_t>::max();
82 
83  std::vector<VkImage> m_Images;
84  std::vector<std::unique_ptr<CTexture>> m_Textures;
85  std::unique_ptr<CTexture> m_DepthTexture;
86  VkFormat m_ImageFormat = VK_FORMAT_UNDEFINED;
87 
89  {
90  using BackbufferKey = std::tuple<
92  AttachmentLoadOp, AttachmentStoreOp>;
94  {
95  size_t operator()(const BackbufferKey& key) const;
96  };
97  std::unordered_map<
98  BackbufferKey, std::unique_ptr<CFramebuffer>, BackbufferKeyHash> backbuffers;
99 
101 
102  SwapChainBackbuffer(const SwapChainBackbuffer&) = delete;
104 
107  };
108  std::vector<SwapChainBackbuffer> m_Backbuffers;
109 };
110 
111 } // namespace Vulkan
112 
113 } // namespace Backend
114 
115 } // namespace Renderer
116 
117 #endif // INCLUDED_RENDERER_BACKEND_VULKAN_SWAPCHAIN
AttachmentLoadOp
Load operation is set for each attachment, what should be done with its content on BeginFramebufferPa...
Definition: IFramebuffer.h:36
VkFormat m_ImageFormat
Definition: SwapChain.h:86
AttachmentStoreOp
Store operation is set for each attachment, what should be done with its content on EndFramebufferPas...
Definition: IFramebuffer.h:51
bool AcquireNextImage(VkSemaphore acquireImageSemaphore)
Definition: SwapChain.cpp:245
VkSwapchainKHR m_SwapChain
Definition: SwapChain.h:79
CFramebuffer * GetCurrentBackbuffer(const AttachmentLoadOp colorAttachmentLoadOp, const AttachmentStoreOp colorAttachmentStoreOp, const AttachmentLoadOp depthStencilAttachmentLoadOp, const AttachmentStoreOp depthStencilAttachmentStoreOp)
Definition: SwapChain.cpp:330
VkSwapchainKHR GetVkSwapchain()
Definition: SwapChain.h:48
std::vector< std::unique_ptr< CTexture > > m_Textures
Definition: SwapChain.h:84
Definition: Texture.h:39
void SubmitCommandsBeforePresent(CRingCommandContext &commandContext)
Definition: SwapChain.cpp:290
Definition: SwapChain.h:43
bool IsValid() const
Definition: SwapChain.h:50
Definition: Device.h:59
size_t operator()(const BackbufferKey &key) const
Definition: SwapChain.cpp:229
std::tuple< AttachmentLoadOp, AttachmentStoreOp, AttachmentLoadOp, AttachmentStoreOp > BackbufferKey
Definition: SwapChain.h:92
std::vector< VkImage > m_Images
Definition: SwapChain.h:83
~CSwapChain()
Definition: SwapChain.cpp:218
SwapChainBackbuffer & operator=(const SwapChainBackbuffer &)=delete
Definition: Framebuffer.h:39
uint32_t m_CurrentImageIndex
Definition: SwapChain.h:81
pthread_key_t key
Definition: wpthread.cpp:140
void Present(VkSemaphore submitDone, VkQueue queue)
Definition: SwapChain.cpp:302
Represents a texture object.
Definition: TextureManager.h:256
std::unique_ptr< CTexture > m_DepthTexture
Definition: SwapChain.h:85
CTexture * GetDepthTexture()
Definition: SwapChain.h:65
static std::unique_ptr< CSwapChain > Create(CDevice *device, VkSurfaceKHR surface, int surfaceDrawableWidth, int surfaceDrawableHeight, std::unique_ptr< CSwapChain > oldSwapChain)
Definition: SwapChain.cpp:45
CDevice * m_Device
Definition: SwapChain.h:76
Backend
Definition: Backend.h:27
A simple helper class to decouple command buffers rotation from frames presenting.
Definition: RingCommandContext.h:47
void SubmitCommandsAfterAcquireNextImage(CRingCommandContext &commandContext)
Definition: SwapChain.cpp:266
std::unordered_map< BackbufferKey, std::unique_ptr< CFramebuffer >, BackbufferKeyHash > backbuffers
Definition: SwapChain.h:98
std::vector< SwapChainBackbuffer > m_Backbuffers
Definition: SwapChain.h:108
unsigned int uint32_t
Definition: wposix_types.h:53
Definition: VideoMode.h:28
bool m_IsValid
Definition: SwapChain.h:78