-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_app.cpp
More file actions
213 lines (176 loc) · 7.64 KB
/
first_app.cpp
File metadata and controls
213 lines (176 loc) · 7.64 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "first_app.hpp"
// libs
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
// std
#include <array>
#include <cassert>
#include <stdexcept>
namespace lve {
struct SimplePushConstantData {
glm::vec2 offset;
alignas(16) glm::vec3 color;
};
FirstApp::FirstApp() {
loadModels();
createPipelineLayout();
recreateSwapChain();
createCommandBuffers();
}
FirstApp::~FirstApp() { vkDestroyPipelineLayout(lveDevice.device(), pipelineLayout, nullptr); }
void FirstApp::run() {
while (!lveWindow.shouldClose()) {
glfwPollEvents();
drawFrame();
}
vkDeviceWaitIdle(lveDevice.device());
}
void FirstApp::loadModels() {
std::vector<LveModel::Vertex> vertices{
{{0.0f, -0.5f}, {1.0f, 0.0f, 0.0f}},
{{0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}},
{{-0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}} };
lveModel = std::make_unique<LveModel>(lveDevice, vertices);
}
void FirstApp::createPipelineLayout() {
VkPushConstantRange pushConstantRange{};
pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
pushConstantRange.offset = 0;
pushConstantRange.size = sizeof(SimplePushConstantData);
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = 0;
pipelineLayoutInfo.pSetLayouts = nullptr;
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
if (vkCreatePipelineLayout(lveDevice.device(), &pipelineLayoutInfo, nullptr, &pipelineLayout) !=
VK_SUCCESS) {
throw std::runtime_error("failed to create pipeline layout!");
}
}
void FirstApp::recreateSwapChain() {
auto extent = lveWindow.getExtent();
while (extent.width == 0 || extent.height == 0) {
extent = lveWindow.getExtent();
glfwWaitEvents();
}
vkDeviceWaitIdle(lveDevice.device());
if (lveSwapChain == nullptr) {
lveSwapChain = std::make_unique<LveSwapChain>(lveDevice, extent);
}
else {
lveSwapChain = std::make_unique<LveSwapChain>(lveDevice, extent, std::move(lveSwapChain));
if (lveSwapChain->imageCount() != commandBuffers.size()) {
freeCommandBuffers();
createCommandBuffers();
}
}
createPipeline();
}
void FirstApp::createPipeline() {
assert(lveSwapChain != nullptr && "Cannot create pipeline before swap chain");
assert(pipelineLayout != nullptr && "Cannot create pipeline before pipeline layout");
PipelineConfigInfo pipelineConfig{};
LvePipeline::defaultPipelineConfigInfo(pipelineConfig);
pipelineConfig.renderPass = lveSwapChain->getRenderPass();
pipelineConfig.pipelineLayout = pipelineLayout;
lvePipeline = std::make_unique<LvePipeline>(
lveDevice,
"shaders/simple_shader.vert.spv",
"shaders/simple_shader.frag.spv",
pipelineConfig);
}
void FirstApp::createCommandBuffers() {
commandBuffers.resize(lveSwapChain->imageCount());
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandPool = lveDevice.getCommandPool();
allocInfo.commandBufferCount = static_cast<uint32_t>(commandBuffers.size());
if (vkAllocateCommandBuffers(lveDevice.device(), &allocInfo, commandBuffers.data()) !=
VK_SUCCESS) {
throw std::runtime_error("failed to allocate command buffers!");
}
}
void FirstApp::freeCommandBuffers() {
vkFreeCommandBuffers(
lveDevice.device(),
lveDevice.getCommandPool(),
static_cast<uint32_t>(commandBuffers.size()),
commandBuffers.data());
commandBuffers.clear();
}
void FirstApp::recordCommandBuffer(int imageIndex) {
static int frame = 30;
frame = (frame + 1) % 200;
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
if (vkBeginCommandBuffer(commandBuffers[imageIndex], &beginInfo) != VK_SUCCESS) {
throw std::runtime_error("failed to begin recording command buffer!");
}
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = lveSwapChain->getRenderPass();
renderPassInfo.framebuffer = lveSwapChain->getFrameBuffer(imageIndex);
renderPassInfo.renderArea.offset = { 0, 0 };
renderPassInfo.renderArea.extent = lveSwapChain->getSwapChainExtent();
std::array<VkClearValue, 2> clearValues{};
clearValues[0].color = { 0.01f, 0.01f, 0.01f, 1.0f };
clearValues[1].depthStencil = { 1.0f, 0 };
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
renderPassInfo.pClearValues = clearValues.data();
vkCmdBeginRenderPass(commandBuffers[imageIndex], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = static_cast<float>(lveSwapChain->getSwapChainExtent().width);
viewport.height = static_cast<float>(lveSwapChain->getSwapChainExtent().height);
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
VkRect2D scissor{ {0, 0}, lveSwapChain->getSwapChainExtent() };
vkCmdSetViewport(commandBuffers[imageIndex], 0, 1, &viewport);
vkCmdSetScissor(commandBuffers[imageIndex], 0, 1, &scissor);
lvePipeline->bind(commandBuffers[imageIndex]);
lveModel->bind(commandBuffers[imageIndex]);
for (int j = 0; j < 4; j++) {
SimplePushConstantData push{};
push.offset = { -0.5f + frame * 0.01f, -0.4f + j * 0.25f };
push.color = { 0.0f, 0.0f, 0.2f + 0.2f * j };
vkCmdPushConstants(
commandBuffers[imageIndex],
pipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0,
sizeof(SimplePushConstantData),
&push);
lveModel->draw(commandBuffers[imageIndex]);
}
vkCmdEndRenderPass(commandBuffers[imageIndex]);
if (vkEndCommandBuffer(commandBuffers[imageIndex]) != VK_SUCCESS) {
throw std::runtime_error("failed to record command buffer!");
}
}
void FirstApp::drawFrame() {
uint32_t imageIndex;
auto result = lveSwapChain->acquireNextImage(&imageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR) {
recreateSwapChain();
return;
}
if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
throw std::runtime_error("failed to acquire swap chain image!");
}
recordCommandBuffer(imageIndex);
result = lveSwapChain->submitCommandBuffers(&commandBuffers[imageIndex], &imageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR ||
lveWindow.wasWindowResized()) {
lveWindow.resetWindowResizedFlag();
recreateSwapChain();
return;
}
else if (result != VK_SUCCESS) {
throw std::runtime_error("failed to present swap chain image!");
}
}
} // namespace lve