#pragma once #include "../Devices/lve_device.hpp" // vulkan headers #include // std lib headers #include #include #include namespace lve { class LveSwapChain { public: static constexpr int MAX_FRAMES_IN_FLIGHT = 2; LveSwapChain(LveDevice &deviceRef, VkExtent2D windowExtent); LveSwapChain( LveDevice &deviceRef, VkExtent2D windowExtent, std::shared_ptr previous); ~LveSwapChain(); LveSwapChain(const LveSwapChain &) = delete; LveSwapChain &operator=(const LveSwapChain &) = delete; VkFramebuffer getFrameBuffer(int index) { return swapChainFramebuffers[index]; } VkRenderPass getRenderPass() { return renderPass; } VkImageView getImageView(int index) { return swapChainImageViews[index]; } size_t imageCount() { return swapChainImages.size(); } VkFormat getSwapChainImageFormat() { return swapChainImageFormat; } VkExtent2D getSwapChainExtent() { return swapChainExtent; } uint32_t width() { return swapChainExtent.width; } uint32_t height() { return swapChainExtent.height; } float extentAspectRatio() { return static_cast(swapChainExtent.width) / static_cast(swapChainExtent.height); } VkFormat findDepthFormat(); VkResult acquireNextImage(uint32_t *imageIndex); VkResult submitCommandBuffers(const VkCommandBuffer *buffers, uint32_t *imageIndex); bool compareSwapFormats(const LveSwapChain &swapChain) const { return swapChain.swapChainDepthFormat == swapChainDepthFormat && swapChain.swapChainImageFormat == 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 swapChainImageFormat; VkFormat swapChainDepthFormat; VkExtent2D swapChainExtent; std::vector swapChainFramebuffers; VkRenderPass renderPass; std::vector depthImages; std::vector depthImageMemorys; std::vector depthImageViews; std::vector swapChainImages; std::vector swapChainImageViews; LveDevice &device; VkExtent2D windowExtent; VkSwapchainKHR swapChain; std::shared_ptr oldSwapChain; std::vector imageAvailableSemaphores; std::vector renderFinishedSemaphores; std::vector inFlightFences; std::vector imagesInFlight; size_t currentFrame = 0; }; } // namespace lve