source: opengl-game/vulkan-ref.cpp@ 187b0f5

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

Change VulkanGame and SDLGame to only use discrete GPUs and switch the timer class to steady_clock

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