-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderer.cpp
More file actions
146 lines (120 loc) · 5.66 KB
/
Renderer.cpp
File metadata and controls
146 lines (120 loc) · 5.66 KB
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
146
#include "Renderer.hpp"
namespace engine{
Renderer::Renderer(Window& window, Device& device) :
m_window{ window },
m_device{ device } {
recreateSwapChain();
createCommandBuffers();
}
Renderer::~Renderer() {
freeCommandBuffers();
}
void Renderer::recreateSwapChain() {
auto extent = m_window.getExtent();
while (extent.width == 0 || extent.height == 0) {
extent = m_window.getExtent();
}
vkDeviceWaitIdle(m_device.device());
if (m_swapChain == nullptr) {
m_swapChain = std::make_unique<SwapChain>(m_device, extent);
}
else {
std::shared_ptr<SwapChain> oldSwapChain = std::move(m_swapChain);
m_swapChain = std::make_unique<SwapChain>(m_device, extent, oldSwapChain);
if(!oldSwapChain->compareSwapFormats(*m_swapChain.get())) {
throw std::runtime_error("SwapChain image(or depth) format has changed");
}
}
}
void Renderer::createCommandBuffers() {
m_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 = m_device.getCommandPool();
allocInfo.commandBufferCount = static_cast<uint32_t>(m_commandBuffers.size());
if (vkAllocateCommandBuffers(m_device.device(), &allocInfo, m_commandBuffers.data()) !=
VK_SUCCESS) {
throw std::runtime_error("Could not allocate commandBuffers");
}
}
void Renderer::freeCommandBuffers() {
vkFreeCommandBuffers(
m_device.device(),
m_device.getCommandPool(),
static_cast<uint32_t>(m_commandBuffers.size()),
m_commandBuffers.data()
);
m_commandBuffers.clear();
}
VkCommandBuffer Renderer::beginFrame() {
assert(!isFrameStarted && "Cannot call beginFrame while already in progress");
auto result = m_swapChain->acquireNextImage(¤tImageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR || m_window.wasWindowResized()) {
m_window.resetWindowResizedFlag();
recreateSwapChain();
return nullptr;
}
if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
throw std::runtime_error("failed to acquire swapchain 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 command buffer!");
}
return commandBuffer;
}
void Renderer::endFrame() {
assert(isFrameStarted && "Cannot call endFrame while frame is not in progress");
auto commandBuffer = getCurrentCommandBuffer();
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
throw std::runtime_error("failed to end command buffer!");
}
auto result = m_swapChain->submitCommandBuffers(&commandBuffer, ¤tImageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || m_window.wasWindowResized()) {
m_window.resetWindowResizedFlag();
recreateSwapChain();
}
if (result != VK_SUCCESS) {
throw std::runtime_error("failed to present swapchain image!");
}
isFrameStarted = false;
currentFrameIndex = (currentFrameIndex + 1) % SwapChain::MAX_FRAMES_IN_FLIGHT;
}
void Renderer::beginSwapChainRenderPass(VkCommandBuffer commandBuffer) {
assert(isFrameStarted && "Cannot call beginSwapChainRenderPass while frame is not in progress");
assert(commandBuffer == getCurrentCommandBuffer() &&
"Cannot begin render pass on commandBuffer from a different frame");
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = m_swapChain->getRenderPass();
renderPassInfo.framebuffer = m_swapChain->getFrameBuffer(currentImageIndex);
renderPassInfo.renderArea.offset = { 0,0 };
renderPassInfo.renderArea.extent = m_swapChain->getSwapChainExtent();
std::array<VkClearValue, 2> clearValues{};
clearValues[0].color = { 0.02f,0.02f ,0.0225f ,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 = 0.0f;
viewport.width = static_cast<float>(m_swapChain->getSwapChainExtent().width);
viewport.height = static_cast<float>(m_swapChain->getSwapChainExtent().height);
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
VkRect2D scissor{ {0, 0}, m_swapChain->getSwapChainExtent() };
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
}
void Renderer::endSwapChainRenderPass(VkCommandBuffer commandBuffer) {
assert(isFrameStarted && "Cannot call endSwapChainRenderPass while frame is not in progress");
assert(commandBuffer == getCurrentCommandBuffer() &&
"Cannot end render pass on commandBuffer from a different frame");
vkCmdEndRenderPass(commandBuffer);
}
} // namespace engine