Repository navigation
Expand file tree
/
Copy pathentrypoint.cpp
More file actions
121 lines (101 loc) · 4.26 KB
/
Copy pathentrypoint.cpp
File metadata and controls
121 lines (101 loc) · 4.26 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
#include "entrypoint.hpp"
// std
#include <stdexcept>
#include <array>
namespace vsr {
EntryPoint::EntryPoint() {
createPipelineLayout();
createPipeline();
createCommandBuffers();
}
EntryPoint::~EntryPoint() {
vkDestroyPipelineLayout(vsrDevice.device(), pipelineLayout, nullptr);
}
void EntryPoint::run() {
while (!vsrWindow.shouldClose()) {
glfwPollEvents();
drawFrame();
}
vkDeviceWaitIdle(vsrDevice.device());
}
void EntryPoint::createPipelineLayout() {
// default empty pipeline layout
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = 0;
pipelineLayoutInfo.pSetLayouts = nullptr;
if (vkCreatePipelineLayout(vsrDevice.device(), &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
throw std::runtime_error("failed to create pipeline layout");
}
}
void EntryPoint::createPipeline() {
// auto is good to use when we easily infer the type from the right or the data type is monstorous
PipelineConfigInfo pipelineConfig{};
VsrPipeline::defaultPipelineConfigInfo(
pipelineConfig,
vsrSwapChain.width(),
vsrSwapChain.height()
);
// right now, we're building out our render pass with our pipeline, REMEMBER TO MOVE THIS LATER
pipelineConfig.renderPass = vsrSwapChain.getRenderPass();
pipelineConfig.pipelineLayout = pipelineLayout;
vsrPipeline = std::make_unique<VsrPipeline>(
vsrDevice,
"shaders/simple_shader.vert.spv",
"shaders/simple_shader.frag.spv",
pipelineConfig
);
}
void EntryPoint::createCommandBuffers() {
commandBuffers.resize(vsrSwapChain.imageCount());
// Buffers are created and destroyed often, this allows us to do this operation quickly by memory management
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandPool = vsrDevice.getCommandPool();
allocInfo.commandBufferCount = static_cast<uint32_t>(commandBuffers.size());
if (vkAllocateCommandBuffers(vsrDevice.device(), &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
throw std::runtime_error("failed to allocate command buffers");
}
for (int i = 0; i < commandBuffers.size(); i++) {
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
if (vkBeginCommandBuffer(commandBuffers[i], &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 = vsrSwapChain.getRenderPass();
renderPassInfo.framebuffer = vsrSwapChain.getFrameBuffer(i);
renderPassInfo.renderArea.offset = { 0, 0 };
renderPassInfo.renderArea.extent = vsrSwapChain.getSwapChainExtent();
// array over a vector here because we only have 2 clearValues ever
std::array<VkClearValue, 2> clearValues{};
// attachment decisions made in render pass
clearValues[0].color = { 0.1f, 0.1f, 0.1f, 1.0f }; // attachment one of frame buffer is color
clearValues[1].depthStencil = { 0.1f, 0 }; // attachment two of frame buffer is depth
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
renderPassInfo.pClearValues = clearValues.data();
vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); // not using secondary command buffers rn
vsrPipeline->bind(commandBuffers[i]);
vkCmdDraw(commandBuffers[i], 3, 1, 0, 0);
vkCmdEndRenderPass(commandBuffers[i]);
if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) {
throw std::runtime_error("failed to record command buffer");
}
}
}
// to be implemented soon
void EntryPoint::drawFrame() {
uint32_t imageIndex;
// result determines if process of swapping chain buffers worked
auto result = vsrSwapChain.acquireNextImage(&imageIndex);
if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
throw std::runtime_error("failed to acquire swap chain image");
}
result = vsrSwapChain.submitCommandBuffers(&commandBuffers[imageIndex], &imageIndex);
if (result != VK_SUCCESS) {
throw std::runtime_error("failed to show image from swap chain");
}
}
}