source: opengl-game/vulkan-ref.cpp@ cabdd5c

feature/imgui-sdl points-test
Last change on this file since cabdd5c was cabdd5c, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

Mark which parts of the old code were moved over to the new openglgame and vulkangame code and remove some TODOs that were moved over

  • Property mode set to 100644
File size: 80.2 KB
Line 
1#define STB_IMAGE_IMPLEMENTATION
2#include "stb_image.h" // TODO: Probably switch to SDL_image
3
4//#define _USE_MATH_DEFINES // Will be needed when/if I need to # include <cmath>
5
6#define GLM_FORCE_RADIANS
7#define GLM_FORCE_DEPTH_ZERO_TO_ONE
8
9#include <glm/glm.hpp>
10#include <glm/gtc/matrix_transform.hpp>
11
12#include <iostream>
13#include <fstream>
14#include <algorithm>
15#include <vector>
16#include <array>
17#include <set>
18#include <optional>
19#include <chrono>
20
21#include "consts.hpp"
22#include "utils.hpp"
23
24#include "game-gui-sdl.hpp"
25
26using namespace std;
27using namespace glm;
28
29const int SCREEN_WIDTH = 800;
30const int SCREEN_HEIGHT = 600;
31
32const int MAX_FRAMES_IN_FLIGHT = 2;
33
34#ifdef NDEBUG
35 const bool enableValidationLayers = false;
36#else
37 const bool enableValidationLayers = true;
38#endif
39
40const vector<const char*> validationLayers = {
41 "VK_LAYER_KHRONOS_validation"
42};
43
44const vector<const char*> deviceExtensions = {
45 VK_KHR_SWAPCHAIN_EXTENSION_NAME
46};
47
48struct QueueFamilyIndices {
49 optional<uint32_t> graphicsFamily;
50 optional<uint32_t> presentFamily;
51
52 bool isComplete() {
53 return graphicsFamily.has_value() && presentFamily.has_value();
54 }
55};
56
57struct SwapChainSupportDetails {
58 VkSurfaceCapabilitiesKHR capabilities;
59 vector<VkSurfaceFormatKHR> formats;
60 vector<VkPresentModeKHR> presentModes;
61};
62
63struct Vertex {
64 glm::vec3 pos;
65 glm::vec3 color;
66 glm::vec2 texCoord;
67};
68
69struct OverlayVertex {
70 glm::vec3 pos;
71 glm::vec2 texCoord;
72};
73
74struct UniformBufferObject {
75 alignas(16) mat4 model;
76 alignas(16) mat4 view;
77 alignas(16) mat4 proj;
78};
79
80struct DescriptorInfo {
81 VkDescriptorType type;
82 VkShaderStageFlags stageFlags;
83
84 vector<VkDescriptorBufferInfo>* bufferDataList;
85 VkDescriptorImageInfo* imageData;
86};
87
88struct GraphicsPipelineInfo {
89 VkPipelineLayout pipelineLayout;
90 VkPipeline pipeline;
91
92 VkVertexInputBindingDescription bindingDescription;
93 vector<VkVertexInputAttributeDescription> attributeDescriptions;
94
95 vector<DescriptorInfo> descriptorInfoList;
96
97 VkDescriptorPool descriptorPool;
98 VkDescriptorSetLayout descriptorSetLayout;
99 vector<VkDescriptorSet> descriptorSets;
100
101 size_t numVertices; // Currently unused
102 VkBuffer vertexBuffer;
103 VkDeviceMemory vertexBufferMemory;
104
105 size_t numIndices;
106 VkBuffer indexBuffer;
107 VkDeviceMemory indexBufferMemory;
108};
109
110/*** START OF REFACTORED CODE ***/
111VkResult CreateDebugUtilsMessengerEXT(VkInstance instance,
112 const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
113 const VkAllocationCallbacks* pAllocator,
114 VkDebugUtilsMessengerEXT* pDebugMessenger) {
115 auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
116
117 if (func != nullptr) {
118 return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
119 } else {
120 return VK_ERROR_EXTENSION_NOT_PRESENT;
121 }
122}
123
124void DestroyDebugUtilsMessengerEXT(VkInstance instance,
125 VkDebugUtilsMessengerEXT debugMessenger,
126 const VkAllocationCallbacks* pAllocator) {
127 auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
128
129 if (func != nullptr) {
130 func(instance, debugMessenger, pAllocator);
131 }
132}
133
134class VulkanGame {
135 public:
136 void run() {
137 if (initWindow() == RTWO_ERROR) {
138 return;
139 }
140 initVulkan();
141 mainLoop();
142 cleanup();
143 }
144
145 private:
146 GameGui* gui = new GameGui_SDL();
147
148 SDL_version sdlVersion;
149 SDL_Window* window = nullptr;
150 SDL_Renderer* gRenderer = nullptr;
151/*** END OF REFACTORED CODE ***/
152 SDL_Texture* uiOverlay = nullptr;
153
154 TTF_Font* gFont = nullptr;
155 SDL_Texture* uiText = nullptr;
156 SDL_Texture* uiImage = nullptr;
157
158/*** START OF REFACTORED CODE ***/
159 VkInstance instance;
160 VkDebugUtilsMessengerEXT debugMessenger;
161/*** END OF REFACTORED CODE ***/
162 VkSurfaceKHR surface;
163
164 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
165 VkDevice device;
166
167 VkQueue graphicsQueue;
168 VkQueue presentQueue;
169
170 VkSwapchainKHR swapChain;
171 vector<VkImage> swapChainImages;
172 VkFormat swapChainImageFormat;
173 VkExtent2D swapChainExtent;
174 vector<VkImageView> swapChainImageViews;
175 vector<VkFramebuffer> swapChainFramebuffers;
176
177 VkRenderPass renderPass;
178
179 VkCommandPool commandPool;
180 vector<VkCommandBuffer> commandBuffers;
181
182 // The images and the sampler are used to store data for specific attributes. I probably
183 // want to keep them separate from the GraphicsPipelineInfo objects and start passing
184 // references to them once I start defining uniform and varying attributes in GraphicsPipelineInfo objects
185
186 VkImage depthImage;
187 VkDeviceMemory depthImageMemory;
188 VkImageView depthImageView;
189
190 VkImage textureImage;
191 VkDeviceMemory textureImageMemory;
192 VkImageView textureImageView;
193
194 VkImage overlayImage;
195 VkDeviceMemory overlayImageMemory;
196 VkImageView overlayImageView;
197
198 VkImage sdlOverlayImage;
199 VkDeviceMemory sdlOverlayImageMemory;
200 VkImageView sdlOverlayImageView;
201
202 VkSampler textureSampler;
203
204 // These are currently to store the MVP matrix
205 // I should figure out if it makes sense to use them for other uniforms in the future
206 // If not, I should rename them to better indicate their puprose.
207 // I should also decide if I can use these for all shaders, or if I need a separapte set of buffers for each one
208 vector<VkBuffer> uniformBuffers;
209 vector<VkDeviceMemory> uniformBuffersMemory;
210
211 VkDescriptorImageInfo sceneImageInfo;
212 VkDescriptorImageInfo overlayImageInfo;
213
214 vector<VkDescriptorBufferInfo> uniformBufferInfoList;
215
216 GraphicsPipelineInfo scenePipeline;
217 GraphicsPipelineInfo overlayPipeline;
218
219 vector<VkSemaphore> imageAvailableSemaphores;
220 vector<VkSemaphore> renderFinishedSemaphores;
221 vector<VkFence> inFlightFences;
222
223 size_t currentFrame = 0;
224
225 bool framebufferResized = false;
226
227/*** START OF REFACTORED CODE ***/
228 bool initWindow() {
229 if (gui->init() == RTWO_ERROR) {
230 cout << "UI library could not be initialized!" << endl;
231 cout << SDL_GetError() << endl;
232 return RTWO_ERROR;
233 }
234 cout << "GUI init succeeded" << endl;
235
236 window = (SDL_Window*) gui->createWindow("Vulkan Game", SCREEN_WIDTH, SCREEN_HEIGHT, false);
237 if (window == nullptr) {
238 cout << "Window could not be created!" << endl;
239 return RTWO_ERROR;
240 }
241
242 gRenderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
243 if (gRenderer == nullptr) {
244 cout << "Renderer could not be created! SDL Error: " << SDL_GetError() << endl;
245 return RTWO_ERROR;
246 }
247/*** END OF REFACTORED CODE ***/
248
249 SDL_VERSION(&sdlVersion);
250
251 // In SDL 2.0.10 (currently, the latest), SDL_TEXTUREACCESS_TARGET is required to get a transparent overlay working
252 // However, the latest SDL version available through homebrew on Mac is 2.0.9, which requires SDL_TEXTUREACCESS_STREAMING
253 // I tried building sdl 2.0.10 (and sdl_image and sdl_ttf) from source on Mac, but had some issues, so this is easier
254 // until the homebrew recipe is updated
255 if (sdlVersion.major == 2 && sdlVersion.minor == 0 && sdlVersion.patch == 9) {
256 uiOverlay = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
257 } else {
258 uiOverlay = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
259 }
260
261 if (uiOverlay == nullptr) {
262 cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << endl;
263 }
264 if (SDL_SetTextureBlendMode(uiOverlay, SDL_BLENDMODE_BLEND) != 0) {
265 cout << "Unable to set texture blend mode! SDL Error: " << SDL_GetError() << endl;
266 }
267
268 gFont = TTF_OpenFont("fonts/lazy.ttf", 28);
269 if (gFont == nullptr) {
270 cout << "Failed to load lazy font! SDL_ttf Error: " << TTF_GetError() << endl;
271 return RTWO_ERROR;
272 }
273
274 SDL_Color textColor = { 0, 0, 0 };
275
276 SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, "Great sucess!", textColor);
277 if (textSurface == nullptr) {
278 cout << "Unable to render text surface! SDL_ttf Error: " << TTF_GetError() << endl;
279 return RTWO_ERROR;
280 }
281
282 uiText = SDL_CreateTextureFromSurface(gRenderer, textSurface);
283 if (uiText == nullptr) {
284 cout << "Unable to create texture from rendered text! SDL Error: " << SDL_GetError() << endl;
285 SDL_FreeSurface(textSurface);
286 return RTWO_ERROR;
287 }
288
289 SDL_FreeSurface(textSurface);
290
291 // TODO: Load a PNG instead
292 SDL_Surface* uiImageSurface = SDL_LoadBMP("assets/images/spaceship.bmp");
293 if (uiImageSurface == nullptr) {
294 cout << "Unable to load image " << "spaceship.bmp" << "! SDL Error: " << SDL_GetError() << endl;
295 return RTWO_ERROR;
296 }
297
298 uiImage = SDL_CreateTextureFromSurface(gRenderer, uiImageSurface);
299 if (uiImage == nullptr) {
300 cout << "Unable to create texture from BMP surface! SDL Error: " << SDL_GetError() << endl;
301 SDL_FreeSurface(uiImageSurface);
302 return RTWO_ERROR;
303 }
304
305 SDL_FreeSurface(uiImageSurface);
306
307 return RTWO_SUCCESS;
308 }
309
310/*** START OF REFACTORED CODE ***/
311 void initVulkan() {
312 createInstance();
313 setupDebugMessenger();
314/*** END OF REFACTORED CODE ***/
315 createSurface();
316 pickPhysicalDevice();
317 createLogicalDevice();
318 createSwapChain();
319 createImageViews();
320 createRenderPass();
321
322 createCommandPool();
323
324 createImageResources("textures/texture.jpg", textureImage, textureImageMemory, textureImageView);
325 createImageResourcesFromSDLTexture(uiOverlay, sdlOverlayImage, sdlOverlayImageMemory, sdlOverlayImageView);
326 createTextureSampler();
327
328 sceneImageInfo = {};
329 sceneImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
330 sceneImageInfo.imageView = textureImageView;
331 sceneImageInfo.sampler = textureSampler;
332
333 overlayImageInfo = {};
334 overlayImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
335 overlayImageInfo.imageView = sdlOverlayImageView;
336 overlayImageInfo.sampler = textureSampler;
337
338 vector<Vertex> sceneVertices = {
339 {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
340 {{ 0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
341 {{ 0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
342 {{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
343
344 {{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
345 {{ 0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
346 {{ 0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
347 {{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
348 };
349 vector<uint16_t> sceneIndices = {
350 0, 1, 2, 2, 3, 0,
351 4, 5, 6, 6, 7, 4
352 };
353
354 initGraphicsPipelineInfo(scenePipeline,
355 sceneVertices.data(), sizeof(Vertex), sceneVertices.size(),
356 sceneIndices.data(), sizeof(uint16_t), sceneIndices.size());
357
358 addAttributeDescription(scenePipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::pos));
359 addAttributeDescription(scenePipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::color));
360 addAttributeDescription(scenePipeline, VK_FORMAT_R32G32_SFLOAT, offset_of(&Vertex::texCoord));
361
362 addDescriptorInfo(scenePipeline, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList, nullptr);
363 addDescriptorInfo(scenePipeline, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr, &sceneImageInfo);
364
365 createDescriptorSetLayout(scenePipeline);
366
367
368 vector<OverlayVertex> overlayVertices = {
369 {{-1.0f, 1.0f, 0.0f}, {0.0f, 1.0f}},
370 {{ 1.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
371 {{ 1.0f, -1.0f, 0.0f}, {1.0f, 0.0f}},
372 {{-1.0f, -1.0f, 0.0f}, {0.0f, 0.0f}}
373 };
374 vector<uint16_t> overlayIndices = {
375 0, 1, 2, 2, 3, 0
376 };
377
378 initGraphicsPipelineInfo(overlayPipeline,
379 overlayVertices.data(), sizeof(OverlayVertex), overlayVertices.size(),
380 overlayIndices.data(), sizeof(uint16_t), overlayIndices.size());
381
382 addAttributeDescription(overlayPipeline, VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos));
383 addAttributeDescription(overlayPipeline, VK_FORMAT_R32G32_SFLOAT, offset_of(&OverlayVertex::texCoord));
384
385 addDescriptorInfo(overlayPipeline, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr, &overlayImageInfo);
386
387 createDescriptorSetLayout(overlayPipeline);
388
389 createBufferResources();
390
391 createSyncObjects();
392 }
393
394 void createInstance() {
395 if (enableValidationLayers && !checkValidationLayerSupport()) {
396 throw runtime_error("validation layers requested, but not available!");
397 }
398
399 VkApplicationInfo appInfo = {};
400 appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
401 appInfo.pApplicationName = "Vulkan Game";
402 appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
403 appInfo.pEngineName = "No Engine";
404 appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
405 appInfo.apiVersion = VK_API_VERSION_1_0;
406
407 VkInstanceCreateInfo createInfo = {};
408 createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
409 createInfo.pApplicationInfo = &appInfo;
410
411 vector<const char*> extensions = getRequiredExtensions();
412 createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
413 createInfo.ppEnabledExtensionNames = extensions.data();
414
415 cout << endl << "Extensions:" << endl;
416 for (const char* extensionName : extensions) {
417 cout << extensionName << endl;
418 }
419 cout << endl;
420
421 VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
422 if (enableValidationLayers) {
423 createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
424 createInfo.ppEnabledLayerNames = validationLayers.data();
425
426 populateDebugMessengerCreateInfo(debugCreateInfo);
427 createInfo.pNext = &debugCreateInfo;
428 } else {
429 createInfo.enabledLayerCount = 0;
430
431 createInfo.pNext = nullptr;
432 }
433
434 if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
435 throw runtime_error("failed to create instance!");
436 }
437 }
438
439 bool checkValidationLayerSupport() {
440 uint32_t layerCount;
441 vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
442
443 vector<VkLayerProperties> availableLayers(layerCount);
444 vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
445
446 for (const char* layerName : validationLayers) {
447 bool layerFound = false;
448
449 for (const auto& layerProperties : availableLayers) {
450 if (strcmp(layerName, layerProperties.layerName) == 0) {
451 layerFound = true;
452 break;
453 }
454 }
455
456 if (!layerFound) {
457 return false;
458 }
459 }
460
461 return true;
462 }
463
464/*** START OF REFACTORED CODE ***/
465 vector<const char*> getRequiredExtensions() {
466 vector<const char*> extensions = gui->getRequiredExtensions();
467
468 if (enableValidationLayers) {
469 extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
470 }
471
472 return extensions;
473 }
474
475 void setupDebugMessenger() {
476 if (!enableValidationLayers) return;
477
478 VkDebugUtilsMessengerCreateInfoEXT createInfo;
479 populateDebugMessengerCreateInfo(createInfo);
480
481 if (CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
482 throw runtime_error("failed to set up debug messenger!");
483 }
484 }
485
486 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
487 createInfo = {};
488 createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
489 createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
490 createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
491 createInfo.pfnUserCallback = debugCallback;
492 }
493 /*** END OF REFACTORED CODE ***/
494
495 void createSurface() {
496 if (gui->createVulkanSurface(instance, &surface) == RTWO_ERROR) {
497 throw runtime_error("failed to create window surface!");
498 }
499 }
500
501 void pickPhysicalDevice() {
502 uint32_t deviceCount = 0;
503 vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
504
505 if (deviceCount == 0) {
506 throw runtime_error("failed to find GPUs with Vulkan support!");
507 }
508
509 vector<VkPhysicalDevice> devices(deviceCount);
510 vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
511
512 cout << endl << "Graphics cards:" << endl;
513 for (const VkPhysicalDevice& device : devices) {
514 if (isDeviceSuitable(device)) {
515 physicalDevice = device;
516 break;
517 }
518 }
519 cout << endl;
520
521 if (physicalDevice == VK_NULL_HANDLE) {
522 throw runtime_error("failed to find a suitable GPU!");
523 }
524 }
525
526 bool isDeviceSuitable(VkPhysicalDevice device) {
527 VkPhysicalDeviceProperties deviceProperties;
528 vkGetPhysicalDeviceProperties(device, &deviceProperties);
529
530 cout << "Device: " << deviceProperties.deviceName << endl;
531
532 QueueFamilyIndices indices = findQueueFamilies(device);
533 bool extensionsSupported = checkDeviceExtensionSupport(device);
534 bool swapChainAdequate = false;
535
536 if (extensionsSupported) {
537 SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device);
538 swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
539 }
540
541 VkPhysicalDeviceFeatures supportedFeatures;
542 vkGetPhysicalDeviceFeatures(device, &supportedFeatures);
543
544 return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
545 }
546
547 bool checkDeviceExtensionSupport(VkPhysicalDevice device) {
548 uint32_t extensionCount;
549 vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
550
551 vector<VkExtensionProperties> availableExtensions(extensionCount);
552 vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
553
554 set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
555
556 for (const auto& extension : availableExtensions) {
557 requiredExtensions.erase(extension.extensionName);
558 }
559
560 return requiredExtensions.empty();
561 }
562
563 void createLogicalDevice() {
564 QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
565
566 vector<VkDeviceQueueCreateInfo> queueCreateInfos;
567 set<uint32_t> uniqueQueueFamilies = {indices.graphicsFamily.value(), indices.presentFamily.value()};
568
569 float queuePriority = 1.0f;
570 for (uint32_t queueFamily : uniqueQueueFamilies) {
571 VkDeviceQueueCreateInfo queueCreateInfo = {};
572 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
573 queueCreateInfo.queueFamilyIndex = queueFamily;
574 queueCreateInfo.queueCount = 1;
575 queueCreateInfo.pQueuePriorities = &queuePriority;
576
577 queueCreateInfos.push_back(queueCreateInfo);
578 }
579
580 VkPhysicalDeviceFeatures deviceFeatures = {};
581 deviceFeatures.samplerAnisotropy = VK_TRUE;
582
583 VkDeviceCreateInfo createInfo = {};
584 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
585 createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
586 createInfo.pQueueCreateInfos = queueCreateInfos.data();
587
588 createInfo.pEnabledFeatures = &deviceFeatures;
589
590 createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
591 createInfo.ppEnabledExtensionNames = deviceExtensions.data();
592
593 // These fields are ignored by up-to-date Vulkan implementations,
594 // but it's a good idea to set them for backwards compatibility
595 if (enableValidationLayers) {
596 createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
597 createInfo.ppEnabledLayerNames = validationLayers.data();
598 } else {
599 createInfo.enabledLayerCount = 0;
600 }
601
602 if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
603 throw runtime_error("failed to create logical device!");
604 }
605
606 vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
607 vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
608 }
609
610 void createSwapChain() {
611 SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice);
612
613 VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
614 VkPresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
615 VkExtent2D extent = chooseSwapExtent(swapChainSupport.capabilities);
616
617 uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
618 if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
619 imageCount = swapChainSupport.capabilities.maxImageCount;
620 }
621
622 VkSwapchainCreateInfoKHR createInfo = {};
623 createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
624 createInfo.surface = surface;
625 createInfo.minImageCount = imageCount;
626 createInfo.imageFormat = surfaceFormat.format;
627 createInfo.imageColorSpace = surfaceFormat.colorSpace;
628 createInfo.imageExtent = extent;
629 createInfo.imageArrayLayers = 1;
630 createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
631
632 QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
633 uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()};
634
635 if (indices.graphicsFamily != indices.presentFamily) {
636 createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
637 createInfo.queueFamilyIndexCount = 2;
638 createInfo.pQueueFamilyIndices = queueFamilyIndices;
639 } else {
640 createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
641 createInfo.queueFamilyIndexCount = 0;
642 createInfo.pQueueFamilyIndices = nullptr;
643 }
644
645 createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
646 createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
647 createInfo.presentMode = presentMode;
648 createInfo.clipped = VK_TRUE;
649 createInfo.oldSwapchain = VK_NULL_HANDLE;
650
651 if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) {
652 throw runtime_error("failed to create swap chain!");
653 }
654
655 vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
656 swapChainImages.resize(imageCount);
657 vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
658
659 swapChainImageFormat = surfaceFormat.format;
660 swapChainExtent = extent;
661 }
662
663 SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) {
664 SwapChainSupportDetails details;
665
666 vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
667
668 uint32_t formatCount;
669 vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
670
671 if (formatCount != 0) {
672 details.formats.resize(formatCount);
673 vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());
674 }
675
676 uint32_t presentModeCount;
677 vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);
678
679 if (presentModeCount != 0) {
680 details.presentModes.resize(presentModeCount);
681 vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data());
682 }
683
684 return details;
685 }
686
687 VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) {
688 for (const auto& availableFormat : availableFormats) {
689 if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
690 return availableFormat;
691 }
692 }
693
694 return availableFormats[0];
695 }
696
697 VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes) {
698 VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR;
699
700 for (const auto& availablePresentMode : availablePresentModes) {
701 if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
702 return availablePresentMode;
703 }
704 else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
705 bestMode = availablePresentMode;
706 }
707 }
708
709 return bestMode;
710 }
711
712 VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {
713 if (capabilities.currentExtent.width != numeric_limits<uint32_t>::max()) {
714 return capabilities.currentExtent;
715 } else {
716 VkExtent2D actualExtent = {
717 static_cast<uint32_t>(gui->getWindowWidth()),
718 static_cast<uint32_t>(gui->getWindowHeight())
719 };
720
721 actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width));
722 actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height));
723
724 return actualExtent;
725 }
726 }
727
728 void createImageViews() {
729 swapChainImageViews.resize(swapChainImages.size());
730
731 for (size_t i = 0; i < swapChainImages.size(); i++) {
732 swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT);
733 }
734 }
735
736 void createRenderPass() {
737 VkAttachmentDescription colorAttachment = {};
738 colorAttachment.format = swapChainImageFormat;
739 colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
740 colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
741 colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
742 colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
743 colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
744 colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
745 colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
746
747 VkAttachmentReference colorAttachmentRef = {};
748 colorAttachmentRef.attachment = 0;
749 colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
750
751 VkAttachmentDescription depthAttachment = {};
752 depthAttachment.format = findDepthFormat();
753 depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
754 depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
755 depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
756 depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
757 depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
758 depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
759 depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
760
761 VkAttachmentReference depthAttachmentRef = {};
762 depthAttachmentRef.attachment = 1;
763 depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
764
765 VkSubpassDescription subpass = {};
766 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
767 subpass.colorAttachmentCount = 1;
768 subpass.pColorAttachments = &colorAttachmentRef;
769 subpass.pDepthStencilAttachment = &depthAttachmentRef;
770
771 VkSubpassDependency dependency = {};
772 dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
773 dependency.dstSubpass = 0;
774 dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
775 dependency.srcAccessMask = 0;
776 dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
777 dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
778
779 array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment };
780 VkRenderPassCreateInfo renderPassInfo = {};
781 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
782 renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
783 renderPassInfo.pAttachments = attachments.data();
784 renderPassInfo.subpassCount = 1;
785 renderPassInfo.pSubpasses = &subpass;
786 renderPassInfo.dependencyCount = 1;
787 renderPassInfo.pDependencies = &dependency;
788
789 if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
790 throw runtime_error("failed to create render pass!");
791 }
792 }
793
794 void initGraphicsPipelineInfo(GraphicsPipelineInfo& info,
795 const void* vertexData, int vertexSize, size_t numVertices,
796 const void* indexData, int indexSize, size_t numIndices) {
797 // Since there is only one array of vertex data, we use binding = 0
798 // I'll probably do that for the foreseeable future
799 // I can calculate the stride myself given info about all the varying attributes
800
801 info.bindingDescription.binding = 0;
802 info.bindingDescription.stride = vertexSize;
803 info.bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
804
805 info.numVertices = numVertices;
806 createVertexBuffer(info, vertexData, vertexSize * numVertices);
807
808 info.numIndices = numIndices;
809 createIndexBuffer(info, indexData, indexSize * numIndices);
810 }
811
812 void addAttributeDescription(GraphicsPipelineInfo& info, VkFormat format, size_t offset) {
813 VkVertexInputAttributeDescription attributeDesc = {};
814
815 attributeDesc.binding = 0;
816 attributeDesc.location = info.attributeDescriptions.size();
817 attributeDesc.format = format;
818 attributeDesc.offset = offset;
819
820 info.attributeDescriptions.push_back(attributeDesc);
821 }
822
823 void addDescriptorInfo(GraphicsPipelineInfo& info, VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData, VkDescriptorImageInfo* imageData) {
824 info.descriptorInfoList.push_back({ type, stageFlags, bufferData, imageData });
825 }
826
827 void createDescriptorSetLayout(GraphicsPipelineInfo& info) {
828 vector<VkDescriptorSetLayoutBinding> bindings(info.descriptorInfoList.size());
829
830 for (size_t i = 0; i < bindings.size(); i++) {
831 bindings[i].binding = i;
832 bindings[i].descriptorCount = 1;
833 bindings[i].descriptorType = info.descriptorInfoList[i].type;
834 bindings[i].stageFlags = info.descriptorInfoList[i].stageFlags;
835 bindings[i].pImmutableSamplers = nullptr;
836 }
837
838 VkDescriptorSetLayoutCreateInfo layoutInfo = {};
839 layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
840 layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
841 layoutInfo.pBindings = bindings.data();
842
843 if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &info.descriptorSetLayout) != VK_SUCCESS) {
844 throw runtime_error("failed to create descriptor set layout!");
845 }
846 }
847
848 void createGraphicsPipeline(string vertShaderFile, string fragShaderFile, GraphicsPipelineInfo& info) {
849 auto vertShaderCode = readFile(vertShaderFile);
850 auto fragShaderCode = readFile(fragShaderFile);
851
852 VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
853 VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
854
855 VkPipelineShaderStageCreateInfo vertShaderStageInfo = {};
856 vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
857 vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
858 vertShaderStageInfo.module = vertShaderModule;
859 vertShaderStageInfo.pName = "main";
860
861 VkPipelineShaderStageCreateInfo fragShaderStageInfo = {};
862 fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
863 fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
864 fragShaderStageInfo.module = fragShaderModule;
865 fragShaderStageInfo.pName = "main";
866
867 VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
868
869 VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
870 vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
871
872 vertexInputInfo.vertexBindingDescriptionCount = 1;
873 vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(info.attributeDescriptions.size());
874 vertexInputInfo.pVertexBindingDescriptions = &info.bindingDescription;
875 vertexInputInfo.pVertexAttributeDescriptions = info.attributeDescriptions.data();
876
877 VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
878 inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
879 inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
880 inputAssembly.primitiveRestartEnable = VK_FALSE;
881
882 VkViewport viewport = {};
883 viewport.x = 0.0f;
884 viewport.y = 0.0f;
885 viewport.width = (float) swapChainExtent.width;
886 viewport.height = (float) swapChainExtent.height;
887 viewport.minDepth = 0.0f;
888 viewport.maxDepth = 1.0f;
889
890 VkRect2D scissor = {};
891 scissor.offset = { 0, 0 };
892 scissor.extent = swapChainExtent;
893
894 VkPipelineViewportStateCreateInfo viewportState = {};
895 viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
896 viewportState.viewportCount = 1;
897 viewportState.pViewports = &viewport;
898 viewportState.scissorCount = 1;
899 viewportState.pScissors = &scissor;
900
901 VkPipelineRasterizationStateCreateInfo rasterizer = {};
902 rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
903 rasterizer.depthClampEnable = VK_FALSE;
904 rasterizer.rasterizerDiscardEnable = VK_FALSE;
905 rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
906 rasterizer.lineWidth = 1.0f;
907 rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
908 rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
909 rasterizer.depthBiasEnable = VK_FALSE;
910
911 VkPipelineMultisampleStateCreateInfo multisampling = {};
912 multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
913 multisampling.sampleShadingEnable = VK_FALSE;
914 multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
915
916 VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
917 colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
918 colorBlendAttachment.blendEnable = VK_TRUE;
919 colorBlendAttachment.colorBlendOp = VK_BLEND_OP_ADD;
920 colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
921 colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
922 colorBlendAttachment.alphaBlendOp = VK_BLEND_OP_ADD;
923 colorBlendAttachment.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
924 colorBlendAttachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
925
926 VkPipelineColorBlendStateCreateInfo colorBlending = {};
927 colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
928 colorBlending.logicOpEnable = VK_FALSE;
929 colorBlending.logicOp = VK_LOGIC_OP_COPY;
930 colorBlending.attachmentCount = 1;
931 colorBlending.pAttachments = &colorBlendAttachment;
932 colorBlending.blendConstants[0] = 0.0f;
933 colorBlending.blendConstants[1] = 0.0f;
934 colorBlending.blendConstants[2] = 0.0f;
935 colorBlending.blendConstants[3] = 0.0f;
936
937 VkPipelineDepthStencilStateCreateInfo depthStencil = {};
938 depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
939 depthStencil.depthTestEnable = VK_TRUE;
940 depthStencil.depthWriteEnable = VK_TRUE;
941 depthStencil.depthCompareOp = VK_COMPARE_OP_LESS;
942 depthStencil.depthBoundsTestEnable = VK_FALSE;
943 depthStencil.minDepthBounds = 0.0f;
944 depthStencil.maxDepthBounds = 1.0f;
945 depthStencil.stencilTestEnable = VK_FALSE;
946 depthStencil.front = {};
947 depthStencil.back = {};
948
949 VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
950 pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
951 pipelineLayoutInfo.setLayoutCount = 1;
952 pipelineLayoutInfo.pSetLayouts = &info.descriptorSetLayout;
953 pipelineLayoutInfo.pushConstantRangeCount = 0;
954
955 if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &info.pipelineLayout) != VK_SUCCESS) {
956 throw runtime_error("failed to create pipeline layout!");
957 }
958
959 VkGraphicsPipelineCreateInfo pipelineInfo = {};
960 pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
961 pipelineInfo.stageCount = 2;
962 pipelineInfo.pStages = shaderStages;
963 pipelineInfo.pVertexInputState = &vertexInputInfo;
964 pipelineInfo.pInputAssemblyState = &inputAssembly;
965 pipelineInfo.pViewportState = &viewportState;
966 pipelineInfo.pRasterizationState = &rasterizer;
967 pipelineInfo.pMultisampleState = &multisampling;
968 pipelineInfo.pDepthStencilState = &depthStencil;
969 pipelineInfo.pColorBlendState = &colorBlending;
970 pipelineInfo.pDynamicState = nullptr;
971 pipelineInfo.layout = info.pipelineLayout;
972 pipelineInfo.renderPass = renderPass;
973 pipelineInfo.subpass = 0;
974 pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
975 pipelineInfo.basePipelineIndex = -1;
976
977 if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &info.pipeline) != VK_SUCCESS) {
978 throw runtime_error("failed to create graphics pipeline!");
979 }
980
981 vkDestroyShaderModule(device, vertShaderModule, nullptr);
982 vkDestroyShaderModule(device, fragShaderModule, nullptr);
983 }
984
985 VkShaderModule createShaderModule(const vector<char>& code) {
986 VkShaderModuleCreateInfo createInfo = {};
987 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
988 createInfo.codeSize = code.size();
989 createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
990
991 VkShaderModule shaderModule;
992 if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
993 throw runtime_error("failed to create shader module!");
994 }
995
996 return shaderModule;
997 }
998
999 void createFramebuffers() {
1000 swapChainFramebuffers.resize(swapChainImageViews.size());
1001
1002 for (size_t i = 0; i < swapChainImageViews.size(); i++) {
1003 array <VkImageView, 2> attachments = {
1004 swapChainImageViews[i],
1005 depthImageView
1006 };
1007
1008 VkFramebufferCreateInfo framebufferInfo = {};
1009 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
1010 framebufferInfo.renderPass = renderPass;
1011 framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
1012 framebufferInfo.pAttachments = attachments.data();
1013 framebufferInfo.width = swapChainExtent.width;
1014 framebufferInfo.height = swapChainExtent.height;
1015 framebufferInfo.layers = 1;
1016
1017 if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) {
1018 throw runtime_error("failed to create framebuffer!");
1019 }
1020 }
1021 }
1022
1023 void createCommandPool() {
1024 QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice);
1025
1026 VkCommandPoolCreateInfo poolInfo = {};
1027 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
1028 poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
1029 poolInfo.flags = 0;
1030
1031 if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
1032 throw runtime_error("failed to create graphics command pool!");
1033 }
1034 }
1035
1036 QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
1037 QueueFamilyIndices indices;
1038
1039 uint32_t queueFamilyCount = 0;
1040 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
1041
1042 vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
1043 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
1044
1045 int i = 0;
1046 for (const auto& queueFamily : queueFamilies) {
1047 if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
1048 indices.graphicsFamily = i;
1049 }
1050
1051 VkBool32 presentSupport = false;
1052 vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
1053
1054 if (queueFamily.queueCount > 0 && presentSupport) {
1055 indices.presentFamily = i;
1056 }
1057
1058 if (indices.isComplete()) {
1059 break;
1060 }
1061
1062 i++;
1063 }
1064
1065 return indices;
1066 }
1067
1068 void createDepthResources() {
1069 VkFormat depthFormat = findDepthFormat();
1070
1071 createImage(swapChainExtent.width, swapChainExtent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL,
1072 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImage, depthImageMemory);
1073 depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
1074
1075 transitionImageLayout(depthImage, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
1076 }
1077
1078 VkFormat findDepthFormat() {
1079 return findSupportedFormat(
1080 { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT },
1081 VK_IMAGE_TILING_OPTIMAL,
1082 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
1083 );
1084 }
1085
1086 VkFormat findSupportedFormat(const vector<VkFormat>& candidates, VkImageTiling tiling,
1087 VkFormatFeatureFlags features) {
1088 for (VkFormat format : candidates) {
1089 VkFormatProperties props;
1090 vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &props);
1091
1092 if (tiling == VK_IMAGE_TILING_LINEAR &&
1093 (props.linearTilingFeatures & features) == features) {
1094 return format;
1095 } else if (tiling == VK_IMAGE_TILING_OPTIMAL &&
1096 (props.optimalTilingFeatures & features) == features) {
1097 return format;
1098 }
1099 }
1100
1101 throw runtime_error("failed to find supported format!");
1102 }
1103
1104 bool hasStencilComponent(VkFormat format) {
1105 return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
1106 }
1107
1108 void createImageResources(string filename, VkImage& image, VkDeviceMemory& imageMemory, VkImageView& view) {
1109 int texWidth, texHeight, texChannels;
1110
1111 stbi_uc* pixels = stbi_load(filename.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
1112 VkDeviceSize imageSize = texWidth * texHeight * 4;
1113
1114 if (!pixels) {
1115 throw runtime_error("failed to load texture image!");
1116 }
1117
1118 VkBuffer stagingBuffer;
1119 VkDeviceMemory stagingBufferMemory;
1120
1121 createBuffer(imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1122 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1123 stagingBuffer, stagingBufferMemory);
1124
1125 void* data;
1126
1127 vkMapMemory(device, stagingBufferMemory, 0, imageSize, 0, &data);
1128 memcpy(data, pixels, static_cast<size_t>(imageSize));
1129 vkUnmapMemory(device, stagingBufferMemory);
1130
1131 stbi_image_free(pixels);
1132
1133 createImage(texWidth, texHeight, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL,
1134 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
1135 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image, imageMemory);
1136
1137 transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
1138 copyBufferToImage(stagingBuffer, image, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight));
1139 transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
1140
1141 vkDestroyBuffer(device, stagingBuffer, nullptr);
1142 vkFreeMemory(device, stagingBufferMemory, nullptr);
1143
1144 view = createImageView(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
1145 }
1146
1147 void createImageResourcesFromSDLTexture(SDL_Texture* texture, VkImage& image, VkDeviceMemory& imageMemory, VkImageView& view) {
1148 int a, w, h;
1149
1150 // I only need this here for the width and height, which are constants, so just use those instead
1151 SDL_QueryTexture(texture, nullptr, &a, &w, &h);
1152
1153 createImage(w, h, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL,
1154 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
1155 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image, imageMemory);
1156
1157 view = createImageView(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
1158 }
1159
1160 void populateImageFromSDLTexture(SDL_Texture* texture, VkImage& image) {
1161 int a, w, h;
1162
1163 SDL_QueryTexture(texture, nullptr, &a, &w, &h);
1164
1165 VkDeviceSize imageSize = w * h * 4;
1166 unsigned char* pixels = new unsigned char[imageSize];
1167
1168 SDL_RenderReadPixels(gRenderer, nullptr, SDL_PIXELFORMAT_ABGR8888, pixels, w * 4);
1169
1170 VkBuffer stagingBuffer;
1171 VkDeviceMemory stagingBufferMemory;
1172
1173 createBuffer(imageSize,
1174 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1175 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1176 stagingBuffer, stagingBufferMemory);
1177
1178 void* data;
1179
1180 vkMapMemory(device, stagingBufferMemory, 0, VK_WHOLE_SIZE, 0, &data);
1181 memcpy(data, pixels, static_cast<size_t>(imageSize));
1182
1183 VkMappedMemoryRange mappedMemoryRange = {};
1184 mappedMemoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
1185 mappedMemoryRange.memory = stagingBufferMemory;
1186 mappedMemoryRange.offset = 0;
1187 mappedMemoryRange.size = VK_WHOLE_SIZE;
1188
1189 // TODO: Should probably check that the function succeeded
1190 vkFlushMappedMemoryRanges(device, 1, &mappedMemoryRange);
1191 vkUnmapMemory(device, stagingBufferMemory);
1192
1193 delete[] pixels;
1194
1195 transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
1196 copyBufferToImage(stagingBuffer, image, static_cast<uint32_t>(w), static_cast<uint32_t>(h));
1197 transitionImageLayout(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
1198
1199 vkDestroyBuffer(device, stagingBuffer, nullptr);
1200 vkFreeMemory(device, stagingBufferMemory, nullptr);
1201 }
1202
1203 void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage,
1204 VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) {
1205 VkImageCreateInfo imageInfo = {};
1206 imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
1207 imageInfo.imageType = VK_IMAGE_TYPE_2D;
1208 imageInfo.extent.width = width;
1209 imageInfo.extent.height = height;
1210 imageInfo.extent.depth = 1;
1211 imageInfo.mipLevels = 1;
1212 imageInfo.arrayLayers = 1;
1213 imageInfo.format = format;
1214 imageInfo.tiling = tiling;
1215 imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
1216 imageInfo.usage = usage;
1217 imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
1218 imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1219
1220 if (vkCreateImage(device, &imageInfo, nullptr, &image) != VK_SUCCESS) {
1221 throw runtime_error("failed to create image!");
1222 }
1223
1224 VkMemoryRequirements memRequirements;
1225 vkGetImageMemoryRequirements(device, image, &memRequirements);
1226
1227 VkMemoryAllocateInfo allocInfo = {};
1228 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1229 allocInfo.allocationSize = memRequirements.size;
1230 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
1231
1232 if (vkAllocateMemory(device, &allocInfo, nullptr, &imageMemory) != VK_SUCCESS) {
1233 throw runtime_error("failed to allocate image memory!");
1234 }
1235
1236 vkBindImageMemory(device, image, imageMemory, 0);
1237 }
1238
1239 void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout) {
1240 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
1241
1242 VkImageMemoryBarrier barrier = {};
1243 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1244 barrier.oldLayout = oldLayout;
1245 barrier.newLayout = newLayout;
1246 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1247 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1248 barrier.image = image;
1249
1250 if (newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
1251 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1252
1253 if (hasStencilComponent(format)) {
1254 barrier.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
1255 }
1256 } else {
1257 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1258 }
1259
1260 barrier.subresourceRange.baseMipLevel = 0;
1261 barrier.subresourceRange.levelCount = 1;
1262 barrier.subresourceRange.baseArrayLayer = 0;
1263 barrier.subresourceRange.layerCount = 1;
1264
1265 VkPipelineStageFlags sourceStage;
1266 VkPipelineStageFlags destinationStage;
1267
1268 if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
1269 barrier.srcAccessMask = 0;
1270 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1271
1272 sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
1273 destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
1274 } else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
1275 barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1276 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1277
1278 sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
1279 destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
1280 } else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) {
1281 barrier.srcAccessMask = 0;
1282 barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
1283
1284 sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
1285 destinationStage = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
1286 } else {
1287 throw invalid_argument("unsupported layout transition!");
1288 }
1289
1290 vkCmdPipelineBarrier(
1291 commandBuffer,
1292 sourceStage, destinationStage,
1293 0,
1294 0, nullptr,
1295 0, nullptr,
1296 1, &barrier
1297 );
1298
1299 endSingleTimeCommands(commandBuffer);
1300 }
1301
1302 void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height) {
1303 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
1304
1305 VkBufferImageCopy region = {};
1306 region.bufferOffset = 0;
1307 region.bufferRowLength = 0;
1308 region.bufferImageHeight = 0;
1309 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1310 region.imageSubresource.mipLevel = 0;
1311 region.imageSubresource.baseArrayLayer = 0;
1312 region.imageSubresource.layerCount = 1;
1313 region.imageOffset = { 0, 0, 0 };
1314 region.imageExtent = { width, height, 1 };
1315
1316 vkCmdCopyBufferToImage(
1317 commandBuffer,
1318 buffer,
1319 image,
1320 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1321 1,
1322 &region
1323 );
1324
1325 endSingleTimeCommands(commandBuffer);
1326 }
1327
1328 VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) {
1329 VkImageViewCreateInfo viewInfo = {};
1330 viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
1331 viewInfo.image = image;
1332 viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
1333 viewInfo.format = format;
1334
1335 viewInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
1336 viewInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
1337 viewInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
1338 viewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
1339
1340 viewInfo.subresourceRange.aspectMask = aspectFlags;
1341 viewInfo.subresourceRange.baseMipLevel = 0;
1342 viewInfo.subresourceRange.levelCount = 1;
1343 viewInfo.subresourceRange.baseArrayLayer = 0;
1344 viewInfo.subresourceRange.layerCount = 1;
1345
1346 VkImageView imageView;
1347 if (vkCreateImageView(device, &viewInfo, nullptr, &imageView) != VK_SUCCESS) {
1348 throw runtime_error("failed to create texture image view!");
1349 }
1350
1351 return imageView;
1352 }
1353
1354 void createTextureSampler() {
1355 VkSamplerCreateInfo samplerInfo = {};
1356 samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
1357 samplerInfo.magFilter = VK_FILTER_LINEAR;
1358 samplerInfo.minFilter = VK_FILTER_LINEAR;
1359
1360 samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1361 samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1362 samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
1363
1364 samplerInfo.anisotropyEnable = VK_TRUE;
1365 samplerInfo.maxAnisotropy = 16;
1366 samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
1367 samplerInfo.unnormalizedCoordinates = VK_FALSE;
1368 samplerInfo.compareEnable = VK_FALSE;
1369 samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
1370 samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
1371 samplerInfo.mipLodBias = 0.0f;
1372 samplerInfo.minLod = 0.0f;
1373 samplerInfo.maxLod = 0.0f;
1374
1375 if (vkCreateSampler(device, &samplerInfo, nullptr, &textureSampler) != VK_SUCCESS) {
1376 throw runtime_error("failed to create texture sampler!");
1377 }
1378 }
1379
1380 void createVertexBuffer(GraphicsPipelineInfo& info, const void* vertexData, VkDeviceSize bufferSize) {
1381 VkBuffer stagingBuffer;
1382 VkDeviceMemory stagingBufferMemory;
1383 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1384 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1385 stagingBuffer, stagingBufferMemory);
1386
1387 void* data;
1388 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
1389 memcpy(data, vertexData, (size_t) bufferSize);
1390 vkUnmapMemory(device, stagingBufferMemory);
1391
1392 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
1393 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.vertexBuffer, info.vertexBufferMemory);
1394
1395 copyBuffer(stagingBuffer, info.vertexBuffer, bufferSize);
1396
1397 vkDestroyBuffer(device, stagingBuffer, nullptr);
1398 vkFreeMemory(device, stagingBufferMemory, nullptr);
1399 }
1400
1401 void createIndexBuffer(GraphicsPipelineInfo& info, const void* indexData, VkDeviceSize bufferSize) {
1402 VkBuffer stagingBuffer;
1403 VkDeviceMemory stagingBufferMemory;
1404 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1405 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1406 stagingBuffer, stagingBufferMemory);
1407
1408 void* data;
1409 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
1410 memcpy(data, indexData, (size_t) bufferSize);
1411 vkUnmapMemory(device, stagingBufferMemory);
1412
1413 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
1414 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.indexBuffer, info.indexBufferMemory);
1415
1416 copyBuffer(stagingBuffer, info.indexBuffer, bufferSize);
1417
1418 vkDestroyBuffer(device, stagingBuffer, nullptr);
1419 vkFreeMemory(device, stagingBufferMemory, nullptr);
1420 }
1421
1422 void createUniformBuffers() {
1423 VkDeviceSize bufferSize = sizeof(UniformBufferObject);
1424
1425 uniformBuffers.resize(swapChainImages.size());
1426 uniformBuffersMemory.resize(swapChainImages.size());
1427 uniformBufferInfoList.resize(swapChainImages.size());
1428
1429 for (size_t i = 0; i < swapChainImages.size(); i++) {
1430 createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
1431 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1432 uniformBuffers[i], uniformBuffersMemory[i]);
1433
1434 uniformBufferInfoList[i].buffer = uniformBuffers[i];
1435 uniformBufferInfoList[i].offset = 0;
1436 uniformBufferInfoList[i].range = sizeof(UniformBufferObject);
1437 }
1438 }
1439
1440 void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory) {
1441 VkBufferCreateInfo bufferInfo = {};
1442 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1443 bufferInfo.size = size;
1444 bufferInfo.usage = usage;
1445 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1446
1447 if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
1448 throw runtime_error("failed to create buffer!");
1449 }
1450
1451 VkMemoryRequirements memRequirements;
1452 vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
1453
1454 VkMemoryAllocateInfo allocInfo = {};
1455 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1456 allocInfo.allocationSize = memRequirements.size;
1457 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
1458
1459 if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
1460 throw runtime_error("failed to allocate buffer memory!");
1461 }
1462
1463 vkBindBufferMemory(device, buffer, bufferMemory, 0);
1464 }
1465
1466 void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) {
1467 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
1468
1469 VkBufferCopy copyRegion = {};
1470 copyRegion.size = size;
1471 vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, &copyRegion);
1472
1473 endSingleTimeCommands(commandBuffer);
1474 }
1475
1476 VkCommandBuffer beginSingleTimeCommands() {
1477 VkCommandBufferAllocateInfo allocInfo = {};
1478 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1479 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1480 allocInfo.commandPool = commandPool;
1481 allocInfo.commandBufferCount = 1;
1482
1483 VkCommandBuffer commandBuffer;
1484 vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer);
1485
1486 VkCommandBufferBeginInfo beginInfo = {};
1487 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1488 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1489
1490 vkBeginCommandBuffer(commandBuffer, &beginInfo);
1491
1492 return commandBuffer;
1493 }
1494
1495 void endSingleTimeCommands(VkCommandBuffer commandBuffer) {
1496 vkEndCommandBuffer(commandBuffer);
1497
1498 VkSubmitInfo submitInfo = {};
1499 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1500 submitInfo.commandBufferCount = 1;
1501 submitInfo.pCommandBuffers = &commandBuffer;
1502
1503 vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
1504 vkQueueWaitIdle(graphicsQueue);
1505
1506 vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
1507 }
1508
1509 uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {
1510 VkPhysicalDeviceMemoryProperties memProperties;
1511 vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
1512
1513 for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
1514 if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
1515 return i;
1516 }
1517 }
1518
1519 throw runtime_error("failed to find suitable memory type!");
1520 }
1521
1522 void createDescriptorPool(GraphicsPipelineInfo& info) {
1523 vector<VkDescriptorPoolSize> poolSizes(info.descriptorInfoList.size());
1524
1525 for (size_t i = 0; i < poolSizes.size(); i++) {
1526 poolSizes[i].type = info.descriptorInfoList[i].type;
1527 poolSizes[i].descriptorCount = static_cast<uint32_t>(swapChainImages.size());
1528 }
1529
1530 VkDescriptorPoolCreateInfo poolInfo = {};
1531 poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
1532 poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
1533 poolInfo.pPoolSizes = poolSizes.data();
1534 poolInfo.maxSets = static_cast<uint32_t>(swapChainImages.size());
1535
1536 if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &info.descriptorPool) != VK_SUCCESS) {
1537 throw runtime_error("failed to create descriptor pool!");
1538 }
1539 }
1540
1541 void createDescriptorSets(GraphicsPipelineInfo& info) {
1542 vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), info.descriptorSetLayout);
1543
1544 VkDescriptorSetAllocateInfo allocInfo = {};
1545 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
1546 allocInfo.descriptorPool = info.descriptorPool;
1547 allocInfo.descriptorSetCount = static_cast<uint32_t>(swapChainImages.size());
1548 allocInfo.pSetLayouts = layouts.data();
1549
1550 info.descriptorSets.resize(swapChainImages.size());
1551 if (vkAllocateDescriptorSets(device, &allocInfo, info.descriptorSets.data()) != VK_SUCCESS) {
1552 throw runtime_error("failed to allocate descriptor sets!");
1553 }
1554
1555 for (size_t i = 0; i < swapChainImages.size(); i++) {
1556 vector<VkWriteDescriptorSet> descriptorWrites(info.descriptorInfoList.size());
1557
1558 for (size_t j = 0; j < descriptorWrites.size(); j++) {
1559 descriptorWrites[j].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1560 descriptorWrites[j].dstSet = info.descriptorSets[i];
1561 descriptorWrites[j].dstBinding = j;
1562 descriptorWrites[j].dstArrayElement = 0;
1563 descriptorWrites[j].descriptorType = info.descriptorInfoList[j].type;
1564 descriptorWrites[j].descriptorCount = 1;
1565 descriptorWrites[j].pBufferInfo = nullptr;
1566 descriptorWrites[j].pImageInfo = nullptr;
1567 descriptorWrites[j].pTexelBufferView = nullptr;
1568
1569 switch (descriptorWrites[j].descriptorType) {
1570 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1571 descriptorWrites[j].pBufferInfo = &(*info.descriptorInfoList[j].bufferDataList)[i];
1572 break;
1573 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1574 descriptorWrites[j].pImageInfo = info.descriptorInfoList[j].imageData;
1575 break;
1576 default:
1577 cout << "Unknown descriptor type: " << descriptorWrites[j].descriptorType << endl;
1578 }
1579 }
1580
1581 vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
1582 }
1583 }
1584
1585 void createCommandBuffers() {
1586 commandBuffers.resize(swapChainFramebuffers.size());
1587
1588 VkCommandBufferAllocateInfo allocInfo = {};
1589 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1590 allocInfo.commandPool = commandPool;
1591 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1592 allocInfo.commandBufferCount = (uint32_t) commandBuffers.size();
1593
1594 if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
1595 throw runtime_error("failed to allocate command buffers!");
1596 }
1597
1598 for (size_t i = 0; i < commandBuffers.size(); i++) {
1599 VkCommandBufferBeginInfo beginInfo = {};
1600 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1601 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
1602 beginInfo.pInheritanceInfo = nullptr;
1603
1604 if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) {
1605 throw runtime_error("failed to begin recording command buffer!");
1606 }
1607
1608 VkRenderPassBeginInfo renderPassInfo = {};
1609 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
1610 renderPassInfo.renderPass = renderPass;
1611 renderPassInfo.framebuffer = swapChainFramebuffers[i];
1612 renderPassInfo.renderArea.offset = { 0, 0 };
1613 renderPassInfo.renderArea.extent = swapChainExtent;
1614
1615 array<VkClearValue, 2> clearValues = {};
1616 clearValues[0].color = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
1617 clearValues[1].depthStencil = { 1.0f, 0 };
1618
1619 renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
1620 renderPassInfo.pClearValues = clearValues.data();
1621
1622 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
1623
1624 createGraphicsPipelineCommands(scenePipeline, i);
1625 createGraphicsPipelineCommands(overlayPipeline, i);
1626
1627 vkCmdEndRenderPass(commandBuffers[i]);
1628
1629 if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) {
1630 throw runtime_error("failed to record command buffer!");
1631 }
1632 }
1633 }
1634
1635 void createGraphicsPipelineCommands(GraphicsPipelineInfo& info, uint32_t currentImage) {
1636 vkCmdBindPipeline(commandBuffers[currentImage], VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipeline);
1637 vkCmdBindDescriptorSets(commandBuffers[currentImage], VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipelineLayout, 0, 1,
1638 &info.descriptorSets[currentImage], 0, nullptr);
1639
1640 VkBuffer vertexBuffers[] = { info.vertexBuffer };
1641 VkDeviceSize offsets[] = { 0 };
1642 vkCmdBindVertexBuffers(commandBuffers[currentImage], 0, 1, vertexBuffers, offsets);
1643
1644 vkCmdBindIndexBuffer(commandBuffers[currentImage], info.indexBuffer, 0, VK_INDEX_TYPE_UINT16);
1645
1646 vkCmdDrawIndexed(commandBuffers[currentImage], static_cast<uint32_t>(info.numIndices), 1, 0, 0, 0);
1647 }
1648
1649 void createSyncObjects() {
1650 imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
1651 renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
1652 inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
1653
1654 VkSemaphoreCreateInfo semaphoreInfo = {};
1655 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
1656
1657 VkFenceCreateInfo fenceInfo = {};
1658 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1659 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
1660
1661 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
1662 if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS ||
1663 vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS ||
1664 vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
1665 throw runtime_error("failed to create synchronization objects for a frame!");
1666 }
1667 }
1668 }
1669
1670 void mainLoop() {
1671 // TODO: Create some generic event-handling functions in game-gui-*
1672 SDL_Event e;
1673 bool quit = false;
1674
1675 while (!quit) {
1676 while (SDL_PollEvent(&e)) {
1677 if (e.type == SDL_QUIT) {
1678 quit = true;
1679 }
1680 if (e.type == SDL_KEYDOWN) {
1681 quit = true;
1682 }
1683 if (e.type == SDL_MOUSEBUTTONDOWN) {
1684 quit = true;
1685 }
1686 if (e.type == SDL_WINDOWEVENT) {
1687 if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
1688 e.window.event == SDL_WINDOWEVENT_MINIMIZED) {
1689 framebufferResized = true;
1690 }
1691 }
1692 }
1693
1694 drawUI();
1695
1696 drawFrame();
1697 }
1698
1699 vkDeviceWaitIdle(device);
1700 }
1701
1702 void drawFrame() {
1703 vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max());
1704
1705 uint32_t imageIndex;
1706
1707 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(),
1708 imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
1709
1710 if (result == VK_ERROR_OUT_OF_DATE_KHR) {
1711 recreateSwapChain();
1712 return;
1713 } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
1714 throw runtime_error("failed to acquire swap chain image!");
1715 }
1716
1717 updateUniformBuffer(imageIndex);
1718
1719 VkSubmitInfo submitInfo = {};
1720 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1721
1722 VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
1723 VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
1724
1725 submitInfo.waitSemaphoreCount = 1;
1726 submitInfo.pWaitSemaphores = waitSemaphores;
1727 submitInfo.pWaitDstStageMask = waitStages;
1728 submitInfo.commandBufferCount = 1;
1729 submitInfo.pCommandBuffers = &commandBuffers[imageIndex];
1730
1731 VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] };
1732
1733 submitInfo.signalSemaphoreCount = 1;
1734 submitInfo.pSignalSemaphores = signalSemaphores;
1735
1736 vkResetFences(device, 1, &inFlightFences[currentFrame]);
1737
1738 if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
1739 throw runtime_error("failed to submit draw command buffer!");
1740 }
1741
1742 VkPresentInfoKHR presentInfo = {};
1743 presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
1744 presentInfo.waitSemaphoreCount = 1;
1745 presentInfo.pWaitSemaphores = signalSemaphores;
1746
1747 VkSwapchainKHR swapChains[] = { swapChain };
1748 presentInfo.swapchainCount = 1;
1749 presentInfo.pSwapchains = swapChains;
1750 presentInfo.pImageIndices = &imageIndex;
1751 presentInfo.pResults = nullptr;
1752
1753 result = vkQueuePresentKHR(presentQueue, &presentInfo);
1754
1755 if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized) {
1756 framebufferResized = false;
1757 recreateSwapChain();
1758 } else if (result != VK_SUCCESS) {
1759 throw runtime_error("failed to present swap chain image!");
1760 }
1761
1762 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
1763 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
1764 }
1765
1766 void drawUI() {
1767 // TODO: Since I currently don't use any other render targets,
1768 // I may as well set this once before the render loop
1769 SDL_SetRenderTarget(gRenderer, uiOverlay);
1770
1771 SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0x00);
1772 SDL_RenderClear(gRenderer);
1773
1774 SDL_Rect rect;
1775
1776 rect = {280, 220, 100, 100};
1777 SDL_SetRenderDrawColor(gRenderer, 0x00, 0xFF, 0x00, 0xFF);
1778 SDL_RenderFillRect(gRenderer, &rect);
1779 SDL_SetRenderDrawColor(gRenderer, 0x00, 0x9F, 0x9F, 0xFF);
1780
1781 rect = {10, 10, 0, 0};
1782 SDL_QueryTexture(uiText, nullptr, nullptr, &(rect.w), &(rect.h));
1783 SDL_RenderCopy(gRenderer, uiText, nullptr, &rect);
1784
1785 rect = {10, 80, 0, 0};
1786 SDL_QueryTexture(uiImage, nullptr, nullptr, &(rect.w), &(rect.h));
1787 SDL_RenderCopy(gRenderer, uiImage, nullptr, &rect);
1788
1789 SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0xFF, 0xFF);
1790 SDL_RenderDrawLine(gRenderer, 50, 5, 150, 500);
1791
1792 populateImageFromSDLTexture(uiOverlay, sdlOverlayImage);
1793 }
1794
1795 void updateUniformBuffer(uint32_t currentImage) {
1796 static auto startTime = chrono::high_resolution_clock::now();
1797
1798 auto currentTime = chrono::high_resolution_clock::now();
1799 float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count();
1800
1801 UniformBufferObject ubo = {};
1802 ubo.model = rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
1803 ubo.view = lookAt(glm::vec3(0.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
1804 ubo.proj = perspective(radians(45.0f), swapChainExtent.width / (float)swapChainExtent.height, 0.1f, 10.0f);
1805 ubo.proj[1][1] *= -1; // flip the y-axis so that +y is up
1806
1807 void* data;
1808 vkMapMemory(device, uniformBuffersMemory[currentImage], 0, sizeof(ubo), 0, &data);
1809 memcpy(data, &ubo, sizeof(ubo));
1810 vkUnmapMemory(device, uniformBuffersMemory[currentImage]);
1811 }
1812
1813 void recreateSwapChain() {
1814 gui->refreshWindowSize();
1815
1816 while (gui->getWindowWidth() == 0 || gui->getWindowHeight() == 0 ||
1817 (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) {
1818 SDL_WaitEvent(nullptr);
1819 gui->refreshWindowSize();
1820 }
1821
1822 vkDeviceWaitIdle(device);
1823
1824 cleanupSwapChain();
1825
1826 createSwapChain();
1827 createImageViews();
1828 createRenderPass();
1829
1830 createBufferResources();
1831 }
1832
1833 void createBufferResources() {
1834 createDepthResources();
1835 createFramebuffers();
1836 createUniformBuffers();
1837
1838 createGraphicsPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv", scenePipeline);
1839 createDescriptorPool(scenePipeline);
1840 createDescriptorSets(scenePipeline);
1841
1842 createGraphicsPipeline("shaders/overlay-vert.spv", "shaders/overlay-frag.spv", overlayPipeline);
1843 createDescriptorPool(overlayPipeline);
1844 createDescriptorSets(overlayPipeline);
1845
1846 createCommandBuffers();
1847 }
1848
1849 void cleanup() {
1850 cleanupSwapChain();
1851
1852 vkDestroySampler(device, textureSampler, nullptr);
1853
1854 vkDestroyImageView(device, textureImageView, nullptr);
1855 vkDestroyImage(device, textureImage, nullptr);
1856 vkFreeMemory(device, textureImageMemory, nullptr);
1857
1858 vkDestroyImageView(device, overlayImageView, nullptr);
1859 vkDestroyImage(device, overlayImage, nullptr);
1860 vkFreeMemory(device, overlayImageMemory, nullptr);
1861
1862 vkDestroyImageView(device, sdlOverlayImageView, nullptr);
1863 vkDestroyImage(device, sdlOverlayImage, nullptr);
1864 vkFreeMemory(device, sdlOverlayImageMemory, nullptr);
1865
1866 cleanupPipelineBuffers(scenePipeline);
1867 cleanupPipelineBuffers(overlayPipeline);
1868
1869 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
1870 vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
1871 vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
1872 vkDestroyFence(device, inFlightFences[i], nullptr);
1873 }
1874
1875 vkDestroyCommandPool(device, commandPool, nullptr);
1876 vkDestroyDevice(device, nullptr);
1877 vkDestroySurfaceKHR(instance, surface, nullptr);
1878
1879/*** START OF REFACTORED CODE ***/
1880 if (enableValidationLayers) {
1881 DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
1882 }
1883
1884 vkDestroyInstance(instance, nullptr);
1885/*** END OF REFACTORED CODE ***/
1886
1887 // TODO: Check if any of these functions accept null parameters
1888 // If they do, I don't need to check for that
1889
1890 if (uiOverlay != nullptr) {
1891 SDL_DestroyTexture(uiOverlay);
1892 uiOverlay = nullptr;
1893 }
1894
1895 TTF_CloseFont(gFont);
1896 gFont = nullptr;
1897
1898 if (uiText != nullptr) {
1899 SDL_DestroyTexture(uiText);
1900 uiText = nullptr;
1901 }
1902
1903 if (uiImage != nullptr) {
1904 SDL_DestroyTexture(uiImage);
1905 uiImage = nullptr;
1906 }
1907
1908/*** START OF REFACTORED CODE ***/
1909 SDL_DestroyRenderer(gRenderer);
1910 gRenderer = nullptr;
1911
1912 gui->destroyWindow();
1913 gui->shutdown();
1914 delete gui;
1915/*** END OF REFACTORED CODE ***/
1916 }
1917
1918 void cleanupSwapChain() {
1919 vkDestroyImageView(device, depthImageView, nullptr);
1920 vkDestroyImage(device, depthImage, nullptr);
1921 vkFreeMemory(device, depthImageMemory, nullptr);
1922
1923 for (auto framebuffer : swapChainFramebuffers) {
1924 vkDestroyFramebuffer(device, framebuffer, nullptr);
1925 }
1926
1927 vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
1928
1929 cleanupPipeline(scenePipeline);
1930 cleanupPipeline(overlayPipeline);
1931
1932 vkDestroyRenderPass(device, renderPass, nullptr);
1933
1934 for (auto imageView : swapChainImageViews) {
1935 vkDestroyImageView(device, imageView, nullptr);
1936 }
1937
1938 vkDestroySwapchainKHR(device, swapChain, nullptr);
1939
1940 for (size_t i = 0; i < swapChainImages.size(); i++) {
1941 vkDestroyBuffer(device, uniformBuffers[i], nullptr);
1942 vkFreeMemory(device, uniformBuffersMemory[i], nullptr);
1943 }
1944 }
1945
1946 void cleanupPipeline(GraphicsPipelineInfo& pipeline) {
1947 vkDestroyPipeline(device, pipeline.pipeline, nullptr);
1948 vkDestroyDescriptorPool(device, pipeline.descriptorPool, nullptr);
1949 vkDestroyPipelineLayout(device, pipeline.pipelineLayout, nullptr);
1950 }
1951
1952 void cleanupPipelineBuffers(GraphicsPipelineInfo& pipeline) {
1953 vkDestroyDescriptorSetLayout(device, pipeline.descriptorSetLayout, nullptr);
1954
1955 vkDestroyBuffer(device, pipeline.vertexBuffer, nullptr);
1956 vkFreeMemory(device, pipeline.vertexBufferMemory, nullptr);
1957 vkDestroyBuffer(device, pipeline.indexBuffer, nullptr);
1958 vkFreeMemory(device, pipeline.indexBufferMemory, nullptr);
1959 }
1960
1961 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
1962 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
1963 VkDebugUtilsMessageTypeFlagsEXT messageType,
1964 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
1965 void* pUserData) {
1966 cerr << "validation layer: " << pCallbackData->pMessage << endl;
1967
1968 return VK_FALSE;
1969 }
1970
1971 static vector<char> readFile(const string& filename) {
1972 ifstream file(filename, ios::ate | ios::binary);
1973
1974 if (!file.is_open()) {
1975 throw runtime_error("failed to open file!");
1976 }
1977
1978 size_t fileSize = (size_t) file.tellg();
1979 vector<char> buffer(fileSize);
1980
1981 file.seekg(0);
1982 file.read(buffer.data(), fileSize);
1983
1984 file.close();
1985
1986 return buffer;
1987 }
1988};
1989
1990/*** START OF REFACTORED CODE ***/
1991int main(int argc, char* argv[]) {
1992
1993#ifdef NDEBUG
1994 cout << "DEBUGGING IS OFF" << endl;
1995#else
1996 cout << "DEBUGGING IS ON" << endl;
1997#endif
1998
1999 cout << "Starting Vulkan game..." << endl;
2000
2001 VulkanGame game;
2002
2003 try {
2004 game.run();
2005 } catch (const exception& e) {
2006 cerr << e.what() << endl;
2007 return EXIT_FAILURE;
2008 }
2009
2010 cout << "Finished running the game" << endl;
2011
2012 return EXIT_SUCCESS;
2013}
2014/*** END OF REFACTORED CODE ***/
Note: See TracBrowser for help on using the repository browser.