source: opengl-game/vulkan-ref.cpp@ 317ad13

feature/imgui-sdl
Last change on this file since 317ad13 was 317ad13, checked in by Dmitry Portnoy <dportnoy@…>, 3 years ago

Update linker include flags in VulkanReference

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