-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.cpp
111 lines (85 loc) · 3.44 KB
/
run.cpp
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
#include <header.hpp>
void HelloTriangleApplication::run() {
initWindow();
initVulkan();
mainLoop();
cleanup();
}
void HelloTriangleApplication::initWindow() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
_window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
}
void HelloTriangleApplication::initVulkan() {
createVulkanInstance();
setupDebugMessenger();
createSurface();
pickPhysicalDevice();
createLogicalDevice();
createSwapChain();
createImageViews();
createRenderPass();
createGraphicsPipeline();
createFramebuffers();
setupCommandResources();
createSyncObjects();
}
void HelloTriangleApplication::mainLoop() {
while (!glfwWindowShouldClose(_window)) {
glfwPollEvents();
drawFrame();
}
vkDeviceWaitIdle(_device);
}
void HelloTriangleApplication::drawFrame() {
vkWaitForFences(_device, 1, &_inFlightFence, VK_TRUE, UINT64_MAX);
vkResetFences(_device, 1, &_inFlightFence);
uint32_t imageIndex;
vkAcquireNextImageKHR(_device, _swapChain, UINT64_MAX, _imageAvailableSemaphore, VK_NULL_HANDLE, &imageIndex);
vkResetCommandBuffer(_commandBuffer, 0);
recordCommandBuffer(_commandBuffer, imageIndex);
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
VkSemaphore waitSemaphores[] = {_imageAvailableSemaphore};
VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &_commandBuffer;
VkSemaphore signalSemaphores[] = {_renderFinishedSemaphore};
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;
if (vkQueueSubmit(_graphicsQueue, 1, &submitInfo, _inFlightFence) != VK_SUCCESS)
throw std::runtime_error("failed to submit draw command buffer!");
VkPresentInfoKHR presentInfo{};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
VkSwapchainKHR swapChains[] = {_swapChain};
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains;
presentInfo.pImageIndices = &imageIndex;
vkQueuePresentKHR(_presentQueue, &presentInfo);
}
void HelloTriangleApplication::cleanup() {
vkDestroySemaphore(_device, _imageAvailableSemaphore, nullptr);
vkDestroySemaphore(_device, _renderFinishedSemaphore, nullptr);
vkDestroyFence(_device, _inFlightFence, nullptr);
vkDestroyCommandPool(_device, _commandPool, nullptr);
for (auto framebuffer : _swapChainFramebuffers)
vkDestroyFramebuffer(_device, framebuffer, nullptr);
vkDestroyPipeline(_device, _graphicsPipeline, nullptr);
vkDestroyPipelineLayout(_device, _pipelineLayout, nullptr);
vkDestroyRenderPass(_device, _renderPass, nullptr);
for (auto imageView : _swapChainImageViews)
vkDestroyImageView(_device, imageView, nullptr);
vkDestroySwapchainKHR(_device, _swapChain, nullptr);
vkDestroyDevice(_device, nullptr);
DestroyDebugUtilsMessengerEXT(_instance, _debugMessenger, nullptr);
vkDestroySurfaceKHR(_instance, _surface, nullptr);
vkDestroyInstance(_instance, nullptr);
glfwDestroyWindow(_window);
glfwTerminate();
}