source: opengl-game/vulkan-ref.cpp@ 7563b8a

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

In vulkanref, implement the ability to add new objects to the scene

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