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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
#include "xe_renderer.hpp"
namespace xe {
Renderer::Renderer(Window& window, Device& device) : xeWindow{window}, xeDevice{device} {
recreateSwapChain();
createCommandBuffers();
}
Renderer::~Renderer() { freeCommandBuffers(); }
void Renderer::recreateSwapChain() {
auto extent = xeWindow.getExtent();
while (extent.width == 0 || extent.height == 0) {
extent = xeWindow.getExtent();
glfwWaitEvents();
}
vkDeviceWaitIdle(xeDevice.device());
if(xeSwapChain == nullptr) {
xeSwapChain = std::make_unique<SwapChain>(xeDevice, extent);
} else {
std::shared_ptr<SwapChain> oldSwapChain = std::move(xeSwapChain);
xeSwapChain = std::make_unique<SwapChain>(xeDevice, extent, oldSwapChain);
if(!oldSwapChain->compareSwapFormats(*xeSwapChain.get())) {
throw std::runtime_error("Swap chain image (or depth) format has changed");
}
}
// we'll come back to this in just a moment
}
void Renderer::createCommandBuffers() {
commandBuffers.resize(SwapChain::MAX_FRAMES_IN_FLIGHT);
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandPool = xeDevice.getCommandPool();
allocInfo.commandBufferCount = static_cast<uint32_t>(commandBuffers.size());
if(vkAllocateCommandBuffers(xeDevice.device(), &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
throw std::runtime_error("failed to allocate command buffers!");
}
}
void Renderer::freeCommandBuffers() {
vkFreeCommandBuffers(
xeDevice.device(),
xeDevice.getCommandPool(),
static_cast<uint32_t>(commandBuffers.size()),
commandBuffers.data());
commandBuffers.clear();
}
VkCommandBuffer Renderer::beginFrame() {
assert(!isFrameStarted && "Can't acll beingFrame while already in progress");
auto result = xeSwapChain->acquireNextImage(¤tImageIndex);
if(result == VK_ERROR_OUT_OF_DATE_KHR) {
recreateSwapChain();
return nullptr;
}
if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
throw std::runtime_error("failed to acquire swap chain image");
}
isFrameStarted = true;
auto commandBuffer = getCurrentCommandBuffer();
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
if(vkBeginCommandBuffer(commandBuffer, &beginInfo) != VK_SUCCESS) {
throw std::runtime_error("failed to begin recording command buffers");
}
return commandBuffer;
}
void Renderer::endFrame() {
assert(isFrameStarted && "Can't call endFrame while frame is not in progress");
auto commandBuffer = getCurrentCommandBuffer();
if(vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
throw std::runtime_error("failed to record command buffer");
}
auto result = xeSwapChain->submitCommandBuffers(&commandBuffer, ¤tImageIndex);
if(result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || xeWindow.wasWindowResized()) {
xeWindow.resetWindowResizedFlag();
recreateSwapChain();
}
isFrameStarted = false;
currentFrameIndex = (currentFrameIndex + 1) % SwapChain::MAX_FRAMES_IN_FLIGHT;
}
void Renderer::beginSwapChainRenderPass(VkCommandBuffer commandBuffer){
assert(isFrameStarted && "Can't call beginSwapChainRenderPass while frame is not in progress");
assert(commandBuffer == getCurrentCommandBuffer() && "Can't begin render pass on command buffer from a different frame");
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = xeSwapChain->getRenderPass();
renderPassInfo.framebuffer = xeSwapChain->getFrameBuffer(currentImageIndex);
renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent = xeSwapChain->getSwapChainExtent();
std::array<VkClearValue, 2> clearValues{};
clearValues[0].color = {0.1f, 0.1f, 0.1f, 1.0f};
clearValues[1].depthStencil = {1.0f, 0};
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
renderPassInfo.pClearValues = clearValues.data();
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = static_cast<float>(xeSwapChain->getSwapChainExtent().height);
viewport.width = static_cast<float>(xeSwapChain->getSwapChainExtent().width);
viewport.height = -static_cast<float>(xeSwapChain->getSwapChainExtent().height);
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
VkRect2D scissor{{0, 0}, xeSwapChain->getSwapChainExtent()};
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
}
void Renderer::endSwapChainRenderPass(VkCommandBuffer commandBuffer){
assert(isFrameStarted && "Can't call endSwapChainRenderPass while frame is not in progress");
assert(commandBuffer == getCurrentCommandBuffer() && "Can't end render pass on command buffer from a different frame");
vkCmdEndRenderPass(commandBuffer);
}
}
|