source: opengl-game/vulkan-ref.cpp@ 2ba5617

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

Replace some couts with runtime_exceptions and, in vulkangame, only call SDL_SetRenderTarget once, during initialization

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