source: opengl-game/vulkan-ref.cpp@ 7d2b0b9

feature/imgui-sdl points-test
Last change on this file since 7d2b0b9 was 7d2b0b9, checked in by Dmitry Portnoy <dmp1488@…>, 5 years ago

Add and begin implementing a GraphicsPipeline class to hold info for the Vulkan graphics pipeline

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