blob: dad3baf680692be1c48ffc3ac683f41c3f8c019a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#pragma once
#include "xe_device.hpp"
#include <vulkan/vulkan.h>
#include <array>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <limits>
#include <memory>
#include <set>
#include <stdexcept>
namespace xe {
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);
~SwapChain();
SwapChain(const SwapChain &) = delete;
SwapChain operator=(const SwapChain &) = 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 SwapChain& 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;
Device &device;
VkExtent2D windowExtent;
VkSwapchainKHR swapChain;
std::shared_ptr<SwapChain> oldSwapChain;
std::vector<VkSemaphore> imageAvailableSemaphores;
std::vector<VkSemaphore> renderFinishedSemaphores;
std::vector<VkFence> inFlightFences;
std::vector<VkFence> imagesInFlight;
size_t currentFrame = 0;
static bool initialSwapChainCreated;
};
}
|