vengine  0.0.1
3D graphics engine
Loading...
Searching...
No Matches
SwapChain.hpp
Go to the documentation of this file.
1///
2/// @file SwapChain.hpp
3/// @brief This file contains the Shader class
4/// @namespace ven
5///
6
7#pragma once
8
9#include <memory>
10
12
13namespace ven {
14
15 static constexpr int MAX_FRAMES_IN_FLIGHT = 2;
16
17 ///
18 /// @class SwapChain
19 /// @brief Class for swap chain
20 /// @namespace ven
21 ///
22 class SwapChain {
23
24 public:
25
26 SwapChain(Device &deviceRef, const VkExtent2D windowExtentRef) : m_device{deviceRef}, m_windowExtent{windowExtentRef} { init(); }
27 SwapChain(Device &deviceRef, const VkExtent2D windowExtentRef, std::shared_ptr<SwapChain> previous) : m_device{deviceRef}, m_windowExtent{windowExtentRef}, m_oldSwapChain{std::move(previous)} { init(); m_oldSwapChain = nullptr; }
28 ~SwapChain();
29
30 SwapChain(const SwapChain &) = delete;
31 SwapChain& operator=(const SwapChain &) = delete;
32 SwapChain(SwapChain &&) = delete;
34
35 [[nodiscard]] VkFramebuffer getFrameBuffer(const unsigned long index) const { return m_swapChainFrameBuffers[index]; }
36 [[nodiscard]] VkRenderPass getRenderPass() const { return m_renderPass; }
37 [[nodiscard]] VkImageView getImageView(const int index) const { return m_swapChainImageViews[static_cast<unsigned long>(index)]; }
38 [[nodiscard]] size_t imageCount() const { return m_swapChainImages.size(); }
39 [[nodiscard]] VkFormat getSwapChainImageFormat() const { return m_swapChainImageFormat; }
40 [[nodiscard]] VkExtent2D getSwapChainExtent() const { return m_swapChainExtent; }
41 [[nodiscard]] uint32_t width() const { return m_swapChainExtent.width; }
42 [[nodiscard]] uint32_t height() const { return m_swapChainExtent.height; }
43
44 [[nodiscard]] float extentAspectRatio() const { return static_cast<float>(m_swapChainExtent.width) / static_cast<float>(m_swapChainExtent.height); }
45 [[nodiscard]] VkFormat findDepthFormat() const;
46
47 VkResult acquireNextImage(uint32_t *imageIndex) const;
48 VkResult submitCommandBuffers(const VkCommandBuffer *buffers, const uint32_t *imageIndex);
49
50 [[nodiscard]] bool compareSwapFormats(const SwapChain &swapChain) const { return m_swapChainImageFormat == swapChain.m_swapChainImageFormat && m_swapChainDepthFormat == swapChain.m_swapChainDepthFormat; }
51
52 private:
53
54 void init();
55 void createSwapChain();
56 void createImageViews();
58 void createRenderPass();
59 void createFrameBuffers();
60 void createSyncObjects();
61
62 static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR> &availableFormats);
63 static VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR> &availablePresentModes);
64 [[nodiscard]] VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities) const;
65
68 VkExtent2D m_swapChainExtent{};
69
70 std::vector<VkFramebuffer> m_swapChainFrameBuffers;
71 VkRenderPass m_renderPass{};
72
73 std::vector<VkImage> m_depthImages;
74 std::vector<VkDeviceMemory> m_depthImageMemory;
75 std::vector<VkImageView> m_depthImageViews;
76 std::vector<VkImage> m_swapChainImages;
77 std::vector<VkImageView> m_swapChainImageViews;
78
80 VkExtent2D m_windowExtent;
81
82 VkSwapchainKHR m_swapChain{};
83 std::shared_ptr<SwapChain> m_oldSwapChain;
84
85 std::vector<VkSemaphore> m_imageAvailableSemaphores;
86 std::vector<VkSemaphore> m_renderFinishedSemaphores;
87 std::vector<VkFence> m_inFlightFences;
88 std::vector<VkFence> m_imagesInFlight;
89 size_t m_currentFrame{0};
90
91 }; // class SwapChain
92
93} // namespace ven
This file contains the Device class.
Class for device.
Definition Device.hpp:35
Class for swap chain.
Definition SwapChain.hpp:22
VkFormat m_swapChainImageFormat
Definition SwapChain.hpp:66
SwapChain & operator=(const SwapChain &)=delete
SwapChain & operator=(SwapChain &&)=delete
SwapChain(const SwapChain &)=delete
size_t imageCount() const
Definition SwapChain.hpp:38
size_t m_currentFrame
Definition SwapChain.hpp:89
VkFramebuffer getFrameBuffer(const unsigned long index) const
Definition SwapChain.hpp:35
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities) const
VkRenderPass getRenderPass() const
Definition SwapChain.hpp:36
VkFormat m_swapChainDepthFormat
Definition SwapChain.hpp:67
std::vector< VkImageView > m_swapChainImageViews
Definition SwapChain.hpp:77
uint32_t height() const
Definition SwapChain.hpp:42
std::vector< VkFence > m_inFlightFences
Definition SwapChain.hpp:87
static VkPresentModeKHR chooseSwapPresentMode(const std::vector< VkPresentModeKHR > &availablePresentModes)
std::vector< VkSemaphore > m_imageAvailableSemaphores
Definition SwapChain.hpp:85
SwapChain(SwapChain &&)=delete
uint32_t width() const
Definition SwapChain.hpp:41
std::shared_ptr< SwapChain > m_oldSwapChain
Definition SwapChain.hpp:83
bool compareSwapFormats(const SwapChain &swapChain) const
Definition SwapChain.hpp:50
VkExtent2D getSwapChainExtent() const
Definition SwapChain.hpp:40
VkFormat getSwapChainImageFormat() const
Definition SwapChain.hpp:39
float extentAspectRatio() const
Definition SwapChain.hpp:44
VkExtent2D m_windowExtent
Definition SwapChain.hpp:80
VkRenderPass m_renderPass
Definition SwapChain.hpp:71
std::vector< VkFence > m_imagesInFlight
Definition SwapChain.hpp:88
SwapChain(Device &deviceRef, const VkExtent2D windowExtentRef, std::shared_ptr< SwapChain > previous)
Definition SwapChain.hpp:27
VkResult acquireNextImage(uint32_t *imageIndex) const
Definition swapChain.cpp:49
VkImageView getImageView(const int index) const
Definition SwapChain.hpp:37
void createSwapChain()
void createDepthResources()
std::vector< VkDeviceMemory > m_depthImageMemory
Definition SwapChain.hpp:74
void createSyncObjects()
void createImageViews()
std::vector< VkFramebuffer > m_swapChainFrameBuffers
Definition SwapChain.hpp:70
std::vector< VkImage > m_swapChainImages
Definition SwapChain.hpp:76
VkFormat findDepthFormat() const
Device & m_device
Definition SwapChain.hpp:79
std::vector< VkImage > m_depthImages
Definition SwapChain.hpp:73
std::vector< VkSemaphore > m_renderFinishedSemaphores
Definition SwapChain.hpp:86
VkResult submitCommandBuffers(const VkCommandBuffer *buffers, const uint32_t *imageIndex)
Definition swapChain.cpp:56
VkExtent2D m_swapChainExtent
Definition SwapChain.hpp:68
VkSwapchainKHR m_swapChain
Definition SwapChain.hpp:82
SwapChain(Device &deviceRef, const VkExtent2D windowExtentRef)
Definition SwapChain.hpp:26
void createRenderPass()
void createFrameBuffers()
std::vector< VkImageView > m_depthImageViews
Definition SwapChain.hpp:75
static VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector< VkSurfaceFormatKHR > &availableFormats)
static constexpr int MAX_FRAMES_IN_FLIGHT
Definition SwapChain.hpp:15