source: opengl-game/vulkan-game.cpp@ 1187ef5

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

Minor code reformatting

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