source: opengl-game/vulkan-ref.cpp@ 603b5bc

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

In vulkangame, add code to create the frame buffers and command buffers

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