vktutorial/hk_swap_chain.hpp

95 lines
2.9 KiB
C++
Raw Normal View History

2024-02-21 13:28:50 +08:00
#pragma once
#include "hk_device.hpp"
// vulkan headers
#include <vulkan/vulkan.h>
// std lib headers
#include <memory>
2024-02-21 13:28:50 +08:00
#include <string>
#include <vector>
namespace hk
{
class SwapChain
{
public:
static constexpr int MAX_FRAMES_IN_FLIGHT = 2;
SwapChain(Device &deviceRef, VkExtent2D windowExtent);
SwapChain(Device &deviceRef, VkExtent2D windowExtent, std::shared_ptr<SwapChain> previous);
2024-02-21 13:28:50 +08:00
~SwapChain();
SwapChain(const SwapChain &) = delete;
SwapChain &operator=(const SwapChain &) = delete;
2025-12-21 10:57:45 +08:00
VkFramebuffer getFrameBuffer(int index) { return m_swapChainFramebuffers[index]; }
VkRenderPass getRenderPass() { return m_renderPass; }
VkImageView getImageView(int index) { return m_swapChainImageViews[index]; }
size_t imageCount() { return m_swapChainImages.size(); }
VkFormat getSwapChainImageFormat() { return m_swapChainImageFormat; }
VkExtent2D getSwapChainExtent() { return m_swapChainExtent; }
uint32_t width() { return m_swapChainExtent.width; }
uint32_t height() { return m_swapChainExtent.height; }
2024-02-21 13:28:50 +08:00
float extentAspectRatio()
{
2025-12-21 10:57:45 +08:00
return static_cast<float>(m_swapChainExtent.width) / static_cast<float>(m_swapChainExtent.height);
2024-02-21 13:28:50 +08:00
}
VkFormat findDepthFormat();
VkResult acquireNextImage(uint32_t *imageIndex);
VkResult submitCommandBuffers(const VkCommandBuffer *buffers, uint32_t *imageIndex);
2024-04-03 22:22:29 +08:00
bool compareSwapFormats(const SwapChain &swapCahin) const
{
2025-12-21 10:57:45 +08:00
return swapCahin.m_swapChainDepthFormat == m_swapChainDepthFormat &&
swapCahin.m_swapChainImageFormat == m_swapChainImageFormat;
2024-04-03 22:22:29 +08:00
}
2024-02-21 13:28:50 +08:00
private:
void init();
2024-02-21 13:28:50 +08:00
void createSwapChain();
void createImageViews();
void createDepthResources();
void createRenderPass();
void createFramebuffers();
void createSyncObjects();
// Helper functions
VkSurfaceFormatKHR chooseSwapSurfaceFormat(
const std::vector<VkSurfaceFormatKHR> &availableFormats);
VkPresentModeKHR chooseSwapPresentMode(
const std::vector<VkPresentModeKHR> &availablePresentModes);
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities);
2025-12-21 10:57:45 +08:00
VkFormat m_swapChainImageFormat;
VkFormat m_swapChainDepthFormat;
VkExtent2D m_swapChainExtent;
2024-02-21 13:28:50 +08:00
2025-12-21 10:57:45 +08:00
std::vector<VkFramebuffer> m_swapChainFramebuffers;
VkRenderPass m_renderPass;
2024-02-21 13:28:50 +08:00
2025-12-21 10:57:45 +08:00
std::vector<VkImage> m_depthImages;
std::vector<VkDeviceMemory> m_depthImageMemorys;
std::vector<VkImageView> m_depthImageViews;
std::vector<VkImage> m_swapChainImages;
std::vector<VkImageView> m_swapChainImageViews;
2024-02-21 13:28:50 +08:00
2025-12-21 10:57:45 +08:00
Device &m_device;
VkExtent2D m_windowExtent;
2024-02-21 13:28:50 +08:00
2025-12-21 10:57:45 +08:00
VkSwapchainKHR m_swapChain;
std::shared_ptr<SwapChain> m_oldSwapChain;
2024-02-21 13:28:50 +08:00
2025-12-21 10:57:45 +08:00
std::vector<VkSemaphore> m_imageAvailableSemaphores;
std::vector<VkSemaphore> m_renderFinishedSemaphores;
std::vector<VkFence> m_inFlightFences;
std::vector<VkFence> m_imagesInFlight;
size_t m_currentFrame = 0;
2024-02-21 13:28:50 +08:00
};
} // namespace hk