source: opengl-game/vulkan-ref.cpp@ 771b33a

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

In openglgame, port over some more of the pipeline creation code and the functionality to specify and initialize varying attributes

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