diff options
Diffstat (limited to '')
-rwxr-xr-x | engine/xe_swap_chain.hpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/engine/xe_swap_chain.hpp b/engine/xe_swap_chain.hpp new file mode 100755 index 0000000..50aa03f --- /dev/null +++ b/engine/xe_swap_chain.hpp @@ -0,0 +1,88 @@ +#pragma once + +#include "xe_device.hpp" + +#include <vulkan/vulkan.h> + +#include <memory> +#include <string> +#include <vector> +#include <vulkan/vulkan_core.h> + +namespace xe { + +class XeSwapChain { + public: + static constexpr int MAX_FRAMES_IN_FLIGHT = 2; + + XeSwapChain(XeDevice &deviceRef, VkExtent2D windowExtent); + XeSwapChain(XeDevice &deviceRef, VkExtent2D windowExtent, std::shared_ptr<XeSwapChain> previous); + ~XeSwapChain(); + + XeSwapChain(const XeSwapChain &) = delete; + XeSwapChain operator=(const XeSwapChain &) = 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<float>(swapChainExtent.width) / static_cast<float>(swapChainExtent.height); + } + VkFormat findDepthFormat(); + + VkResult acquireNextImage(uint32_t *imageIndex); + VkResult submitCommandBuffers(const VkCommandBuffer *buffers, uint32_t *imageIndex); + + bool compareSwapFormats(const XeSwapChain& swapChain) const { + return swapChain.swapChainDepthFormat == swapChainDepthFormat && + swapChain.swapChainImageFormat == swapChainImageFormat; + } + + private: + void init(); + void createSwapChain(); + void createImageViews(); + void createDepthResources(); + void createRenderPass(); + void createFramebuffers(); + void createSyncObjects(); + + VkSurfaceFormatKHR chooseSwapSurfaceFormat( + const std::vector<VkSurfaceFormatKHR> &availableFormats); + VkPresentModeKHR chooseSwapPresentMode( + const std::vector<VkPresentModeKHR> &availablePresentModes); + VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities); + + VkFormat swapChainImageFormat; + VkFormat swapChainDepthFormat; + VkExtent2D swapChainExtent; + + std::vector<VkFramebuffer> swapChainFramebuffers; + VkRenderPass renderPass; + + std::vector<VkImage> depthImages; + std::vector<VkDeviceMemory> depthImageMemorys; + std::vector<VkImageView> depthImageViews; + std::vector<VkImage> swapChainImages; + std::vector<VkImageView> swapChainImageViews; + + XeDevice &device; + VkExtent2D windowExtent; + + VkSwapchainKHR swapChain; + std::shared_ptr<XeSwapChain> oldSwapChain; + + std::vector<VkSemaphore> imageAvailableSemaphores; + std::vector<VkSemaphore> renderFinishedSemaphores; + std::vector<VkFence> inFlightFences; + std::vector<VkFence> imagesInFlight; + size_t currentFrame = 0; +}; + +} |