source: opengl-game/vulkan-game.cpp@ 850e84c

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

Create space-game.cpp, which will be the original opengl game re-implemented in Vulkan.

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