-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloTriangleApplication.cpp
More file actions
290 lines (232 loc) · 8.25 KB
/
Copy pathHelloTriangleApplication.cpp
File metadata and controls
290 lines (232 loc) · 8.25 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <cstring>
#include <stdexcept>
#include <iostream>
#include <vector>
#include <optional>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include "HelloTriangleApplication.h"
void HelloTriangleApplication::run() {
initWindow();
initVulkan();
mainLoop();
cleanup();
}
void HelloTriangleApplication::initWindow() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
m_window_size = glm::ivec2(800, 600);
m_window = glfwCreateWindow(m_window_size.x, m_window_size.y, "Vulkan", nullptr, nullptr);
if (m_debug) {
if (m_window != nullptr) {
std::cout << "GLFW window created!" << std::endl;
}
}
}
void HelloTriangleApplication::initVulkan() {
createInstance();
setupDebugMessenger();
pickPhysicalDevice();
createLogicalDevice();
}
void HelloTriangleApplication::createLogicalDevice() {
QueueFamilyIndices indices = findQueueFamilies(m_physicalDevice);
VkDeviceQueueCreateInfo queueCreateInfo{};
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueCreateInfo.queueFamilyIndex = indices.graphicsFamily.value();
queueCreateInfo.queueCount = 1;
float queuePriority = 1.0f;
queueCreateInfo.pQueuePriorities = &queuePriority;
// Empthy for now, but we will need to have it
VkPhysicalDeviceFeatures deviceFeatures{};
VkDeviceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
createInfo.pQueueCreateInfos = &queueCreateInfo;
createInfo.queueCreateInfoCount = 1;
createInfo.pEnabledFeatures = &deviceFeatures;
createInfo.enabledExtensionCount = 0;
if (m_debug) {
createInfo.enabledLayerCount = static_cast<uint32_t>(m_validationLayers.size());
createInfo.ppEnabledLayerNames = m_validationLayers.data();
} else {
createInfo.enabledLayerCount = 0;
}
if (vkCreateDevice(m_physicalDevice, &createInfo, nullptr, &m_device) != VK_SUCCESS) {
throw std::runtime_error("failed to create logical device!");
}
if (m_debug) {
std::cout << "Device created!" << std::endl;
}
vkGetDeviceQueue(m_device, indices.graphicsFamily.value(), 0, &m_graphicsQueue);
if (m_debug) {
std::cout << "Graphics queue created!" << std::endl;
}
}
void HelloTriangleApplication::pickPhysicalDevice() {
uint32_t deviceCount = 0;
vkEnumeratePhysicalDevices(m_instance, &deviceCount, nullptr);
if (deviceCount == 0) {
throw std::runtime_error("failed to find GPUs with Vulkan support!");
}
std::vector<VkPhysicalDevice> devices(deviceCount);
vkEnumeratePhysicalDevices(m_instance, &deviceCount, devices.data());
if (m_debug) {
std::cout << "There are " << deviceCount << " devices" << std::endl;
}
std::optional<size_t> deviceIndex;
int i = 0;
for (const auto& device : devices) {
if (m_debug) {
std::cout << i << ":" << std::endl;
}
if (isDeviceSuitable(device) && !deviceIndex.has_value()) {
deviceIndex = i;
}
++i;
}
if (deviceIndex.has_value()) {
m_physicalDevice = devices[*deviceIndex];
}
if (m_physicalDevice == VK_NULL_HANDLE) {
throw std::runtime_error("failed to find a suitable GPU!");
}
if (m_debug) {
std::cout << "Suitable GPU found:" << std::endl;
std::cout << getDeviceInfo(m_physicalDevice) << std::endl;
}
}
QueueFamilyIndices HelloTriangleApplication::findQueueFamilies(const VkPhysicalDevice& device) {
QueueFamilyIndices indices;
uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
int i = 0;
for (const auto& queueFamily : queueFamilies) {
if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
indices.graphicsFamily = i;
}
if (indices.isComplete()) {
break;
}
i++;
}
return indices;
}
bool HelloTriangleApplication::isDeviceSuitable(const VkPhysicalDevice& device) {
if (m_debug) {
std::cout << getDeviceInfo(device) << std::endl;
}
QueueFamilyIndices indices = findQueueFamilies(device);
return indices.isComplete();
}
void HelloTriangleApplication::createInstance() {
if (m_debug && !checkValidationLayerSupport()) {
throw std::runtime_error("Validation layers requested, but not available!");
}
VkApplicationInfo appInfo{};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Hello Triangle";
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName = "No Engine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_0;
VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
// Check that all required extensions are available
auto required_extensions = getRequiredExtensions();
if (!checkExtensionSupport(required_extensions)) {
throw std::runtime_error("Missing VK extensions!");
} else {
createInfo.enabledExtensionCount = static_cast<uint32_t>(required_extensions.size());
createInfo.ppEnabledExtensionNames = required_extensions.data();
}
// If we want validation layers
// Enable until we get the extension correctlly (if not there is a crash)
VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo{};
if (m_debug) {
createInfo.enabledLayerCount = static_cast<uint32_t>(m_validationLayers.size());
createInfo.ppEnabledLayerNames = m_validationLayers.data();
populateDebugMessengerCreateInfo(debugCreateInfo);
createInfo.pNext = (VkDebugUtilsMessengerCreateInfoEXT*) &debugCreateInfo;
} else {
createInfo.enabledLayerCount = 0;
}
if (vkCreateInstance(&createInfo, nullptr, &m_instance) != VK_SUCCESS) {
throw std::runtime_error("failed to create instance!");
} else if (m_debug) {
std::cout << "VK instance created!" << std::endl;
}
}
bool HelloTriangleApplication::checkExtensionSupport(const std::vector<const char*>& required_extensions) {
// Query for available extensions
// 1) How many are there
uint32_t extensionCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
// 2) which are they
std::vector<VkExtensionProperties> avail_extensions(extensionCount);
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, avail_extensions.data());
// print just for debug
if (m_debug) {
std::cout << "Required extensions:\n";
for (const auto& extension_name : required_extensions) {
std::cout << '\t' << extension_name << '\n';
}
std::cout << "Available extensions:\n";
for (const auto& extension : avail_extensions) {
std::cout << '\t' << extension.extensionName << '\n';
}
}
bool allThere = true;
for (const auto& req_extension : required_extensions) {
// Check that this required extension is on the available ones
bool found = false;
for (const auto& avail_extension : avail_extensions) {
if (strcmp(avail_extension.extensionName, req_extension) == 0) {
found = true;
break;
}
}
allThere &= found;
}
if (allThere) {
std::cout << "Extensions dependency meet!" << std::endl;
}
return allThere;
}
std::vector<const char*> HelloTriangleApplication::getRequiredExtensions() {
// Query the required extensions
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions;
glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
std::vector<const char*> required_extensions{glfwExtensions, glfwExtensions + glfwExtensionCount};
if (m_debug) {
required_extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
}
return required_extensions;
}
void HelloTriangleApplication::mainLoop() {
while (!glfwWindowShouldClose(m_window)) {
glfwPollEvents();
}
}
void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, const VkAllocationCallbacks* pAllocator) {
auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
if (func != nullptr) {
func(instance, debugMessenger, pAllocator);
}
}
void HelloTriangleApplication::cleanup() {
// VK debug
if (m_debug) {
DestroyDebugUtilsMessengerEXT(m_instance, m_debugMessenger, nullptr);
}
// VK
vkDestroyDevice(m_device, nullptr);
vkDestroyInstance(m_instance, nullptr);
// GLFW
glfwDestroyWindow(m_window);
glfwTerminate();
}