source: opengl-game/vulkan-ref.cpp@ 0ae182f

feature/imgui-sdl points-test
Last change on this file since 0ae182f was 0ae182f, checked in by Dmitry Portnoy <dmp1488@…>, 5 years ago

In vulkangame, finish implementing recreateSwapChain()

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