#pragma once #include "hk_device.hpp" // vulkan headers #include // std lib headers #include #include #include 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 previous); ~SwapChain(); SwapChain(const SwapChain &) = delete; SwapChain &operator=(const SwapChain &) = delete; 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; } float extentAspectRatio() { return static_cast(m_swapChainExtent.width) / static_cast(m_swapChainExtent.height); } VkFormat findDepthFormat(); VkResult acquireNextImage(uint32_t *imageIndex); VkResult submitCommandBuffers(const VkCommandBuffer *buffers, uint32_t *imageIndex); bool compareSwapFormats(const SwapChain &swapCahin) const { return swapCahin.m_swapChainDepthFormat == m_swapChainDepthFormat && swapCahin.m_swapChainImageFormat == m_swapChainImageFormat; } private: void init(); void createSwapChain(); void createImageViews(); void createDepthResources(); void createRenderPass(); void createFramebuffers(); void createSyncObjects(); // Helper functions VkSurfaceFormatKHR chooseSwapSurfaceFormat( const std::vector &availableFormats); VkPresentModeKHR chooseSwapPresentMode( const std::vector &availablePresentModes); VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities); VkFormat m_swapChainImageFormat; VkFormat m_swapChainDepthFormat; VkExtent2D m_swapChainExtent; std::vector m_swapChainFramebuffers; VkRenderPass m_renderPass; std::vector m_depthImages; std::vector m_depthImageMemorys; std::vector m_depthImageViews; std::vector m_swapChainImages; std::vector m_swapChainImageViews; Device &m_device; VkExtent2D m_windowExtent; VkSwapchainKHR m_swapChain; std::shared_ptr m_oldSwapChain; std::vector m_imageAvailableSemaphores; std::vector m_renderFinishedSemaphores; std::vector m_inFlightFences; std::vector m_imagesInFlight; size_t m_currentFrame = 0; }; } // namespace hk