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

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

Change all game-gui function names to lower camel case

  • Property mode set to 100644
File size: 80.0 KB
RevLine 
[f00ee54]1#define STB_IMAGE_IMPLEMENTATION
2#include "stb_image.h" // TODO: Probably switch to SDL_image
[826df16]3
4//#define _USE_MATH_DEFINES // Will be needed when/if I need to # include <cmath>
[03f4c64]5
6#define GLM_FORCE_RADIANS
[fba08f2]7#define GLM_FORCE_DEPTH_ZERO_TO_ONE
[f00ee54]8
[80edd70]9#include <glm/glm.hpp>
[de32fda]10#include <glm/gtc/matrix_transform.hpp>
[03f4c64]11
12#include <iostream>
[80edd70]13#include <fstream>
14#include <algorithm>
15#include <vector>
16#include <array>
[0e6ecf3]17#include <set>
[80edd70]18#include <optional>
[de32fda]19#include <chrono>
[03f4c64]20
[7fc5e27]21#include "consts.hpp"
[f00ee54]22#include "utils.h"
[03f4c64]23
[f00ee54]24#include "game-gui-sdl.hpp"
25
26using namespace std;
27using namespace glm;
[e1a7f5a]28
[826df16]29const int SCREEN_WIDTH = 800;
30const int SCREEN_HEIGHT = 600;
31
[47bff4c]32const int MAX_FRAMES_IN_FLIGHT = 2;
33
[826df16]34#ifdef NDEBUG
35 const bool enableValidationLayers = false;
36#else
37 const bool enableValidationLayers = true;
38#endif
39
[bfd620e]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
[909b51a]48struct QueueFamilyIndices {
49 optional<uint32_t> graphicsFamily;
[b3671b5]50 optional<uint32_t> presentFamily;
[909b51a]51
52 bool isComplete() {
[b3671b5]53 return graphicsFamily.has_value() && presentFamily.has_value();
[909b51a]54 }
55};
56
[bfd620e]57struct SwapChainSupportDetails {
58 VkSurfaceCapabilitiesKHR capabilities;
59 vector<VkSurfaceFormatKHR> formats;
60 vector<VkPresentModeKHR> presentModes;
61};
62
[80edd70]63struct Vertex {
[adcd252]64 glm::vec3 pos;
[80edd70]65 glm::vec3 color;
[fba08f2]66 glm::vec2 texCoord;
[f00ee54]67};
[80edd70]68
[f00ee54]69struct OverlayVertex {
70 glm::vec3 pos;
71 glm::vec2 texCoord;
[80edd70]72};
73
[de32fda]74struct UniformBufferObject {
[f00ee54]75 alignas(16) mat4 model;
76 alignas(16) mat4 view;
77 alignas(16) mat4 proj;
[de32fda]78};
79
[721e8be]80struct DescriptorInfo {
81 VkDescriptorType type;
82 VkShaderStageFlags stageFlags;
83
84 vector<VkDescriptorBufferInfo>* bufferDataList;
85 VkDescriptorImageInfo* imageData;
86};
87
[b8b32bd]88struct GraphicsPipelineInfo {
[d22ae72]89 VkPipelineLayout pipelineLayout;
[b8b32bd]90 VkPipeline pipeline;
91
[f00ee54]92 VkVertexInputBindingDescription bindingDescription;
93 vector<VkVertexInputAttributeDescription> attributeDescriptions;
94
[721e8be]95 vector<DescriptorInfo> descriptorInfoList;
96
[d22ae72]97 VkDescriptorPool descriptorPool;
98 VkDescriptorSetLayout descriptorSetLayout;
99 vector<VkDescriptorSet> descriptorSets;
100
[f00ee54]101 size_t numVertices; // Currently unused
[c8b0357]102 VkBuffer vertexBuffer;
103 VkDeviceMemory vertexBufferMemory;
104
[f00ee54]105 size_t numIndices;
[c8b0357]106 VkBuffer indexBuffer;
107 VkDeviceMemory indexBufferMemory;
[80edd70]108};
109
[b6127d2]110VkResult CreateDebugUtilsMessengerEXT(VkInstance instance,
111 const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
112 const VkAllocationCallbacks* pAllocator,
113 VkDebugUtilsMessengerEXT* pDebugMessenger) {
[621664a]114 auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
[b6127d2]115
116 if (func != nullptr) {
117 return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
118 } else {
119 return VK_ERROR_EXTENSION_NOT_PRESENT;
120 }
121}
122
[80de39d]123void DestroyDebugUtilsMessengerEXT(VkInstance instance,
124 VkDebugUtilsMessengerEXT debugMessenger,
125 const VkAllocationCallbacks* pAllocator) {
[621664a]126 auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
[80de39d]127
128 if (func != nullptr) {
129 func(instance, debugMessenger, pAllocator);
130 }
131}
132
[826df16]133class VulkanGame {
134 public:
135 void run() {
136 if (initWindow() == RTWO_ERROR) {
137 return;
138 }
139 initVulkan();
140 mainLoop();
141 cleanup();
142 }
[621664a]143
[826df16]144 private:
[98f3232]145 GameGui* gui = new GameGui_SDL();
[c8c6da8]146 SDL_version sdlVersion;
[80de39d]147 SDL_Window* window = nullptr;
[826df16]148
[5f3dba8]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
[826df16]157 VkInstance instance;
[b6127d2]158 VkDebugUtilsMessengerEXT debugMessenger;
[b3671b5]159 VkSurfaceKHR surface;
[5f3dba8]160
[909b51a]161 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
162 VkDevice device;
[b3671b5]163
[909b51a]164 VkQueue graphicsQueue;
[b3671b5]165 VkQueue presentQueue;
[826df16]166
[bfd620e]167 VkSwapchainKHR swapChain;
168 vector<VkImage> swapChainImages;
169 VkFormat swapChainImageFormat;
170 VkExtent2D swapChainExtent;
171 vector<VkImageView> swapChainImageViews;
[621664a]172 vector<VkFramebuffer> swapChainFramebuffers;
173
[be34c9a]174 VkRenderPass renderPass;
[1187ef5]175
[f5d5686]176 VkCommandPool commandPool;
[1187ef5]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
[f5d5686]182
[adcd252]183 VkImage depthImage;
184 VkDeviceMemory depthImageMemory;
185 VkImageView depthImageView;
186
[f5d5686]187 VkImage textureImage;
188 VkDeviceMemory textureImageMemory;
[fba08f2]189 VkImageView textureImageView;
[69dccfe]190
191 VkImage overlayImage;
192 VkDeviceMemory overlayImageMemory;
193 VkImageView overlayImageView;
194
[e1a7f5a]195 VkImage sdlOverlayImage;
196 VkDeviceMemory sdlOverlayImageMemory;
197 VkImageView sdlOverlayImageView;
198
[fba08f2]199 VkSampler textureSampler;
[f5d5686]200
[1187ef5]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
[de32fda]205 vector<VkBuffer> uniformBuffers;
206 vector<VkDeviceMemory> uniformBuffersMemory;
207
[721e8be]208 VkDescriptorImageInfo sceneImageInfo;
209 VkDescriptorImageInfo overlayImageInfo;
210
211 vector<VkDescriptorBufferInfo> uniformBufferInfoList;
212
[1187ef5]213 GraphicsPipelineInfo scenePipeline;
214 GraphicsPipelineInfo overlayPipeline;
[47bff4c]215
216 vector<VkSemaphore> imageAvailableSemaphores;
217 vector<VkSemaphore> renderFinishedSemaphores;
218 vector<VkFence> inFlightFences;
219
220 size_t currentFrame = 0;
[ebeb3aa]221
[75108ef]222 bool framebufferResized = false;
223
[5f3dba8]224 // TODO: Make make some more initi functions, or call this initUI if the
225 // amount of things initialized here keeps growing
[826df16]226 bool initWindow() {
[5f3dba8]227 // TODO: Put all fonts, textures, and images in the assets folder
228
[7fc5e27]229 if (gui->init() == RTWO_ERROR) {
[826df16]230 cout << "UI library could not be initialized!" << endl;
[5f3dba8]231 cout << SDL_GetError() << endl;
[826df16]232 return RTWO_ERROR;
[5f3dba8]233 }
234 cout << "GUI init succeeded" << endl;
[826df16]235
[7fc5e27]236 window = (SDL_Window*) gui->createWindow("Vulkan Game", SCREEN_WIDTH, SCREEN_HEIGHT, false);
[5f3dba8]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
[c8c6da8]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
[5f3dba8]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;
[826df16]295 }
[5f3dba8]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;
[826df16]307 }
308
309 void initVulkan() {
310 createInstance();
[7dcd925]311 setupDebugMessenger();
[b3671b5]312 createSurface();
[909b51a]313 pickPhysicalDevice();
314 createLogicalDevice();
[bfd620e]315 createSwapChain();
316 createImageViews();
[be34c9a]317 createRenderPass();
[d22ae72]318
[47bff4c]319 createCommandPool();
[1187ef5]320
[69dccfe]321 createImageResources("textures/texture.jpg", textureImage, textureImageMemory, textureImageView);
[e1a7f5a]322 createImageResourcesFromSDLTexture(uiOverlay, sdlOverlayImage, sdlOverlayImageMemory, sdlOverlayImageView);
[fba08f2]323 createTextureSampler();
[c8b0357]324
[721e8be]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
[f00ee54]335 vector<Vertex> sceneVertices = {
[c8b0357]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}}
[f00ee54]345 };
346 vector<uint16_t> sceneIndices = {
[e5d4aca]347 0, 1, 2, 2, 3, 0,
348 4, 5, 6, 6, 7, 4
[f00ee54]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
[721e8be]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);
[c8b0357]363
[f00ee54]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
[721e8be]382 addDescriptorInfo(overlayPipeline, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr, &overlayImageInfo);
383
384 createDescriptorSetLayout(overlayPipeline);
[c8b0357]385
[e5d4aca]386 createBufferResources();
[d22ae72]387
[47bff4c]388 createSyncObjects();
[826df16]389 }
390
391 void createInstance() {
[b6127d2]392 if (enableValidationLayers && !checkValidationLayerSupport()) {
393 throw runtime_error("validation layers requested, but not available!");
394 }
395
[826df16]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
[a8f0577]408 vector<const char*> extensions = getRequiredExtensions();
[b6127d2]409 createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
410 createInfo.ppEnabledExtensionNames = extensions.data();
[826df16]411
[8667f76]412 cout << endl << "Extensions:" << endl;
[b3671b5]413 for (const char* extensionName : extensions) {
414 cout << extensionName << endl;
415 }
416 cout << endl;
417
[80de39d]418 VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
[b6127d2]419 if (enableValidationLayers) {
420 createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
421 createInfo.ppEnabledLayerNames = validationLayers.data();
[80de39d]422
423 populateDebugMessengerCreateInfo(debugCreateInfo);
424 createInfo.pNext = &debugCreateInfo;
[b6127d2]425 } else {
426 createInfo.enabledLayerCount = 0;
[80de39d]427
428 createInfo.pNext = nullptr;
[b6127d2]429 }
[826df16]430
431 if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
432 throw runtime_error("failed to create instance!");
433 }
434 }
435
[621664a]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() {
[7fc5e27]462 vector<const char*> extensions = gui->getRequiredExtensions();
[621664a]463
464 if (enableValidationLayers) {
465 extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
466 }
467
468 return extensions;
469 }
470
[80de39d]471 void setupDebugMessenger() {
472 if (!enableValidationLayers) return;
473
474 VkDebugUtilsMessengerCreateInfoEXT createInfo;
475 populateDebugMessengerCreateInfo(createInfo);
[b6127d2]476
477 if (CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
[621664a]478 throw runtime_error("failed to set up debug messenger!");
[b6127d2]479 }
480 }
481
[621664a]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
[b3671b5]490 void createSurface() {
[7fc5e27]491 if (gui->createVulkanSurface(instance, &surface) == RTWO_ERROR) {
[b3671b5]492 throw runtime_error("failed to create window surface!");
493 }
494 }
495
[909b51a]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);
[bfd620e]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
[fba08f2]536 VkPhysicalDeviceFeatures supportedFeatures;
537 vkGetPhysicalDeviceFeatures(device, &supportedFeatures);
538
539 return indices.isComplete() && extensionsSupported && swapChainAdequate && supportedFeatures.samplerAnisotropy;
[bfd620e]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();
[909b51a]556 }
557
558 void createLogicalDevice() {
559 QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
560
[b3671b5]561 vector<VkDeviceQueueCreateInfo> queueCreateInfos;
562 set<uint32_t> uniqueQueueFamilies = {indices.graphicsFamily.value(), indices.presentFamily.value()};
[909b51a]563
564 float queuePriority = 1.0f;
[b3671b5]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 }
[909b51a]574
575 VkPhysicalDeviceFeatures deviceFeatures = {};
[fba08f2]576 deviceFeatures.samplerAnisotropy = VK_TRUE;
[909b51a]577
578 VkDeviceCreateInfo createInfo = {};
579 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
[621664a]580 createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
[b3671b5]581 createInfo.pQueueCreateInfos = queueCreateInfos.data();
[909b51a]582
583 createInfo.pEnabledFeatures = &deviceFeatures;
584
[bfd620e]585 createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
586 createInfo.ppEnabledExtensionNames = deviceExtensions.data();
[909b51a]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);
[b3671b5]602 vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
[909b51a]603 }
604
[621664a]605 void createSwapChain() {
606 SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice);
[a8f0577]607
[621664a]608 VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
609 VkPresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
610 VkExtent2D extent = chooseSwapExtent(swapChainSupport.capabilities);
[a8f0577]611
[621664a]612 uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
613 if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
614 imageCount = swapChainSupport.capabilities.maxImageCount;
[a8f0577]615 }
616
[621664a]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;
[909b51a]626
[621664a]627 QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
628 uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()};
[b3671b5]629
[621664a]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 }
[b3671b5]639
[621664a]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;
[909b51a]645
[621664a]646 if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) {
647 throw runtime_error("failed to create swap chain!");
[909b51a]648 }
649
[621664a]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;
[909b51a]656 }
657
[bfd620e]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;
[621664a]698 }
699 else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
[bfd620e]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;
[621664a]710 }
711 else {
[75108ef]712 int width, height;
[7fc5e27]713 gui->getWindowSize(&width, &height);
[75108ef]714
715 VkExtent2D actualExtent = {
716 static_cast<uint32_t>(width),
717 static_cast<uint32_t>(height)
718 };
[bfd620e]719
[f00ee54]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));
[bfd620e]722
723 return actualExtent;
724 }
725 }
726
727 void createImageViews() {
728 swapChainImageViews.resize(swapChainImages.size());
729
[621664a]730 for (size_t i = 0; i < swapChainImages.size(); i++) {
[adcd252]731 swapChainImageViews[i] = createImageView(swapChainImages[i], swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT);
[bfd620e]732 }
733 }
734
[be34c9a]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
[adcd252]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
[be34c9a]764 VkSubpassDescription subpass = {};
765 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
766 subpass.colorAttachmentCount = 1;
767 subpass.pColorAttachments = &colorAttachmentRef;
[adcd252]768 subpass.pDepthStencilAttachment = &depthAttachmentRef;
[be34c9a]769
[621664a]770 VkSubpassDependency dependency = {};
[47bff4c]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
[adcd252]778 array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment };
[be34c9a]779 VkRenderPassCreateInfo renderPassInfo = {};
780 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
[adcd252]781 renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
782 renderPassInfo.pAttachments = attachments.data();
[be34c9a]783 renderPassInfo.subpassCount = 1;
784 renderPassInfo.pSubpasses = &subpass;
[47bff4c]785 renderPassInfo.dependencyCount = 1;
786 renderPassInfo.pDependencies = &dependency;
[be34c9a]787
788 if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
789 throw runtime_error("failed to create render pass!");
790 }
791 }
792
[f00ee54]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
[721e8be]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
[d22ae72]847 void createGraphicsPipeline(string vertShaderFile, string fragShaderFile, GraphicsPipelineInfo& info) {
[b8b32bd]848 auto vertShaderCode = readFile(vertShaderFile);
849 auto fragShaderCode = readFile(fragShaderFile);
[e09ad38]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
[84216c7]868 VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
869 vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
[80edd70]870
871 vertexInputInfo.vertexBindingDescriptionCount = 1;
[f00ee54]872 vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(info.attributeDescriptions.size());
873 vertexInputInfo.pVertexBindingDescriptions = &info.bindingDescription;
874 vertexInputInfo.pVertexAttributeDescriptions = info.attributeDescriptions.data();
[84216c7]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;
[c7fb883]907 rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
[621664a]908 rasterizer.depthBiasEnable = VK_FALSE;
[84216c7]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;
[69dccfe]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;
[84216c7]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
[adcd252]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
[84216c7]948 VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
949 pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
[de32fda]950 pipelineLayoutInfo.setLayoutCount = 1;
[d22ae72]951 pipelineLayoutInfo.pSetLayouts = &info.descriptorSetLayout;
[84216c7]952 pipelineLayoutInfo.pushConstantRangeCount = 0;
953
[d22ae72]954 if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &info.pipelineLayout) != VK_SUCCESS) {
[84216c7]955 throw runtime_error("failed to create pipeline layout!");
956 }
957
[fd70015]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;
[adcd252]967 pipelineInfo.pDepthStencilState = &depthStencil;
[fd70015]968 pipelineInfo.pColorBlendState = &colorBlending;
969 pipelineInfo.pDynamicState = nullptr;
[d22ae72]970 pipelineInfo.layout = info.pipelineLayout;
[fd70015]971 pipelineInfo.renderPass = renderPass;
972 pipelineInfo.subpass = 0;
973 pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
974 pipelineInfo.basePipelineIndex = -1;
975
[d22ae72]976 if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &info.pipeline) != VK_SUCCESS) {
[fd70015]977 throw runtime_error("failed to create graphics pipeline!");
978 }
979
[e09ad38]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;
[4befb76]996 }
997
[ebeb3aa]998 void createFramebuffers() {
999 swapChainFramebuffers.resize(swapChainImageViews.size());
1000
1001 for (size_t i = 0; i < swapChainImageViews.size(); i++) {
[adcd252]1002 array <VkImageView, 2> attachments = {
1003 swapChainImageViews[i],
1004 depthImageView
[ebeb3aa]1005 };
1006
1007 VkFramebufferCreateInfo framebufferInfo = {};
1008 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
1009 framebufferInfo.renderPass = renderPass;
[adcd252]1010 framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
1011 framebufferInfo.pAttachments = attachments.data();
[ebeb3aa]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
[47bff4c]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) {
[621664a]1031 throw runtime_error("failed to create graphics command pool!");
[47bff4c]1032 }
1033 }
1034
[621664a]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
[adcd252]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
[69dccfe]1107 void createImageResources(string filename, VkImage& image, VkDeviceMemory& imageMemory, VkImageView& view) {
[eea05dd]1108 int texWidth, texHeight, texChannels;
1109
[69dccfe]1110 stbi_uc* pixels = stbi_load(filename.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
[eea05dd]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,
[69dccfe]1134 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image, imageMemory);
[eea05dd]1135
[69dccfe]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);
[eea05dd]1139
1140 vkDestroyBuffer(device, stagingBuffer, nullptr);
[f5d5686]1141 vkFreeMemory(device, stagingBufferMemory, nullptr);
[69dccfe]1142
1143 view = createImageView(image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
[eea05dd]1144 }
1145
[e1a7f5a]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) {
[f00ee54]1160 int a, w, h;
[e1a7f5a]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
[8a40f4b]1192 delete[] pixels;
1193
[e1a7f5a]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
[621664a]1202 void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage,
1203 VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) {
[eea05dd]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
[621664a]1226 VkMemoryAllocateInfo allocInfo = {};
[eea05dd]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
[621664a]1238 void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout) {
[eea05dd]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;
[adcd252]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
[eea05dd]1259 barrier.subresourceRange.baseMipLevel = 0;
1260 barrier.subresourceRange.levelCount = 1;
1261 barrier.subresourceRange.baseArrayLayer = 0;
1262 barrier.subresourceRange.layerCount = 1;
[f5d5686]1263
1264 VkPipelineStageFlags sourceStage;
1265 VkPipelineStageFlags destinationStage;
[eea05dd]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;
[adcd252]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;
[eea05dd]1285 } else {
1286 throw invalid_argument("unsupported layout transition!");
1287 }
1288
1289 vkCmdPipelineBarrier(
1290 commandBuffer,
[f5d5686]1291 sourceStage, destinationStage,
[eea05dd]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
[adcd252]1327 VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) {
[fba08f2]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
[adcd252]1339 viewInfo.subresourceRange.aspectMask = aspectFlags;
[fba08f2]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
[f00ee54]1379 void createVertexBuffer(GraphicsPipelineInfo& info, const void* vertexData, VkDeviceSize bufferSize) {
[d9ef6ab]1380 VkBuffer stagingBuffer;
1381 VkDeviceMemory stagingBufferMemory;
[621664a]1382 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
[d9ef6ab]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);
[f00ee54]1388 memcpy(data, vertexData, (size_t) bufferSize);
[d9ef6ab]1389 vkUnmapMemory(device, stagingBufferMemory);
1390
1391 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
[f00ee54]1392 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.vertexBuffer, info.vertexBufferMemory);
[d9ef6ab]1393
[f00ee54]1394 copyBuffer(stagingBuffer, info.vertexBuffer, bufferSize);
[d9ef6ab]1395
1396 vkDestroyBuffer(device, stagingBuffer, nullptr);
1397 vkFreeMemory(device, stagingBufferMemory, nullptr);
1398 }
1399
[f00ee54]1400 void createIndexBuffer(GraphicsPipelineInfo& info, const void* indexData, VkDeviceSize bufferSize) {
[cae7a2c]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);
[f00ee54]1409 memcpy(data, indexData, (size_t) bufferSize);
[cae7a2c]1410 vkUnmapMemory(device, stagingBufferMemory);
1411
1412 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
[f00ee54]1413 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, info.indexBuffer, info.indexBufferMemory);
[cae7a2c]1414
[f00ee54]1415 copyBuffer(stagingBuffer, info.indexBuffer, bufferSize);
[cae7a2c]1416
1417 vkDestroyBuffer(device, stagingBuffer, nullptr);
1418 vkFreeMemory(device, stagingBufferMemory, nullptr);
1419 }
1420
[621664a]1421 void createUniformBuffers() {
1422 VkDeviceSize bufferSize = sizeof(UniformBufferObject);
1423
1424 uniformBuffers.resize(swapChainImages.size());
1425 uniformBuffersMemory.resize(swapChainImages.size());
[721e8be]1426 uniformBufferInfoList.resize(swapChainImages.size());
[621664a]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]);
[721e8be]1432
1433 uniformBufferInfoList[i].buffer = uniformBuffers[i];
1434 uniformBufferInfoList[i].offset = 0;
1435 uniformBufferInfoList[i].range = sizeof(UniformBufferObject);
[621664a]1436 }
1437 }
1438
1439 void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory) {
[80edd70]1440 VkBufferCreateInfo bufferInfo = {};
1441 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
[d9ef6ab]1442 bufferInfo.size = size;
1443 bufferInfo.usage = usage;
[80edd70]1444 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1445
[d9ef6ab]1446 if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
1447 throw runtime_error("failed to create buffer!");
[80edd70]1448 }
1449
[d9ef6ab]1450 VkMemoryRequirements memRequirements;
1451 vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
[80edd70]1452
1453 VkMemoryAllocateInfo allocInfo = {};
1454 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
[d9ef6ab]1455 allocInfo.allocationSize = memRequirements.size;
1456 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
[621664a]1457
[d9ef6ab]1458 if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
1459 throw runtime_error("failed to allocate buffer memory!");
[80edd70]1460 }
1461
[d9ef6ab]1462 vkBindBufferMemory(device, buffer, bufferMemory, 0);
1463 }
[80edd70]1464
[d9ef6ab]1465 void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) {
[eea05dd]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() {
[d9ef6ab]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
[eea05dd]1491 return commandBuffer;
1492 }
[d9ef6ab]1493
[eea05dd]1494 void endSingleTimeCommands(VkCommandBuffer commandBuffer) {
[d9ef6ab]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);
[80edd70]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
[721e8be]1521 void createDescriptorPool(GraphicsPipelineInfo& info) {
1522 vector<VkDescriptorPoolSize> poolSizes(info.descriptorInfoList.size());
[c7fb883]1523
[721e8be]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());
[c7fb883]1527 }
[e5d4aca]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
[721e8be]1540 void createDescriptorSets(GraphicsPipelineInfo& info) {
[e5d4aca]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++) {
[721e8be]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 }
[e5d4aca]1579
1580 vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
1581 }
1582 }
1583
[47bff4c]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;
[621664a]1591 allocInfo.commandBufferCount = (uint32_t) commandBuffers.size();
[47bff4c]1592
1593 if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
[621664a]1594 throw runtime_error("failed to allocate command buffers!");
[47bff4c]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
[adcd252]1614 array<VkClearValue, 2> clearValues = {};
[f00ee54]1615 clearValues[0].color = {{ 0.0f, 0.0f, 0.0f, 1.0f }};
[adcd252]1616 clearValues[1].depthStencil = { 1.0f, 0 };
1617
1618 renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
1619 renderPassInfo.pClearValues = clearValues.data();
[47bff4c]1620
1621 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
[621664a]1622
[d22ae72]1623 createGraphicsPipelineCommands(scenePipeline, i);
1624 createGraphicsPipelineCommands(overlayPipeline, i);
[621664a]1625
[47bff4c]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
[1187ef5]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);
[d22ae72]1638
1639 VkBuffer vertexBuffers[] = { info.vertexBuffer };
1640 VkDeviceSize offsets[] = { 0 };
[1187ef5]1641 vkCmdBindVertexBuffers(commandBuffers[currentImage], 0, 1, vertexBuffers, offsets);
[d22ae72]1642
[1187ef5]1643 vkCmdBindIndexBuffer(commandBuffers[currentImage], info.indexBuffer, 0, VK_INDEX_TYPE_UINT16);
[d22ae72]1644
[1187ef5]1645 vkCmdDrawIndexed(commandBuffers[currentImage], static_cast<uint32_t>(info.numIndices), 1, 0, 0, 0);
[d22ae72]1646 }
1647
[47bff4c]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 ||
[1187ef5]1662 vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS ||
1663 vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
[47bff4c]1664 throw runtime_error("failed to create synchronization objects for a frame!");
1665 }
1666 }
1667 }
1668
[826df16]1669 void mainLoop() {
1670 // TODO: Create some generic event-handling functions in game-gui-*
1671 SDL_Event e;
1672 bool quit = false;
1673
[7dcd925]1674 while (!quit) {
[826df16]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 }
[75108ef]1685 if (e.type == SDL_WINDOWEVENT) {
[1187ef5]1686 if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
1687 e.window.event == SDL_WINDOWEVENT_MINIMIZED) {
[75108ef]1688 framebufferResized = true;
1689 }
1690 }
[47bff4c]1691 }
[321272c]1692
[5f3dba8]1693 drawUI();
[e1a7f5a]1694
1695 drawFrame();
[47bff4c]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
[621664a]1706 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(),
1707 imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
[75108ef]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 }
[47bff4c]1715
[de32fda]1716 updateUniformBuffer(imageIndex);
1717
[47bff4c]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
[75108ef]1735 vkResetFences(device, 1, &inFlightFences[currentFrame]);
1736
[47bff4c]1737 if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
1738 throw runtime_error("failed to submit draw command buffer!");
[bfd620e]1739 }
[47bff4c]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
[75108ef]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 }
[47bff4c]1760
1761 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
[fba08f2]1762 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
[826df16]1763 }
1764
[5f3dba8]1765 void drawUI() {
[e1a7f5a]1766 // TODO: Since I currently don't use any other render targets,
1767 // I may as well set this once before the render loop
[5f3dba8]1768 SDL_SetRenderTarget(gRenderer, uiOverlay);
1769
[5936c58]1770 SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0x00);
[5f3dba8]1771 SDL_RenderClear(gRenderer);
1772
1773 SDL_Rect rect;
1774
1775 rect = {280, 220, 100, 100};
[5936c58]1776 SDL_SetRenderDrawColor(gRenderer, 0x00, 0xFF, 0x00, 0xFF);
[5f3dba8]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
[5936c58]1788 SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0xFF, 0xFF);
[5f3dba8]1789 SDL_RenderDrawLine(gRenderer, 50, 5, 150, 500);
1790
[e1a7f5a]1791 populateImageFromSDLTexture(uiOverlay, sdlOverlayImage);
[5f3dba8]1792 }
1793
[de32fda]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 = {};
[f00ee54]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);
[4f63fa8]1804 ubo.proj[1][1] *= -1; // flip the y-axis so that +y is up
[fba08f2]1805
[de32fda]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
[621664a]1812 void recreateSwapChain() {
1813 int width = 0, height = 0;
1814
[7fc5e27]1815 gui->getWindowSize(&width, &height);
[621664a]1816
1817 while (width == 0 || height == 0 ||
1818 (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) {
1819 SDL_WaitEvent(nullptr);
[7fc5e27]1820 gui->getWindowSize(&width, &height);
[621664a]1821 }
1822
1823 vkDeviceWaitIdle(device);
1824
1825 cleanupSwapChain();
1826
1827 createSwapChain();
1828 createImageViews();
1829 createRenderPass();
[d22ae72]1830
[e5d4aca]1831 createBufferResources();
1832 }
[d22ae72]1833
[e5d4aca]1834 void createBufferResources() {
[adcd252]1835 createDepthResources();
[621664a]1836 createFramebuffers();
1837 createUniformBuffers();
[d22ae72]1838
[e5d4aca]1839 createGraphicsPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv", scenePipeline);
[721e8be]1840 createDescriptorPool(scenePipeline);
1841 createDescriptorSets(scenePipeline);
[d22ae72]1842
[e5d4aca]1843 createGraphicsPipeline("shaders/overlay-vert.spv", "shaders/overlay-frag.spv", overlayPipeline);
[721e8be]1844 createDescriptorPool(overlayPipeline);
1845 createDescriptorSets(overlayPipeline);
[d22ae72]1846
[621664a]1847 createCommandBuffers();
1848 }
1849
[826df16]1850 void cleanup() {
[75108ef]1851 cleanupSwapChain();
1852
[fba08f2]1853 vkDestroySampler(device, textureSampler, nullptr);
[69dccfe]1854
[fba08f2]1855 vkDestroyImageView(device, textureImageView, nullptr);
[eea05dd]1856 vkDestroyImage(device, textureImage, nullptr);
[f5d5686]1857 vkFreeMemory(device, textureImageMemory, nullptr);
[eea05dd]1858
[69dccfe]1859 vkDestroyImageView(device, overlayImageView, nullptr);
1860 vkDestroyImage(device, overlayImage, nullptr);
1861 vkFreeMemory(device, overlayImageMemory, nullptr);
1862
[e1a7f5a]1863 vkDestroyImageView(device, sdlOverlayImageView, nullptr);
1864 vkDestroyImage(device, sdlOverlayImage, nullptr);
1865 vkFreeMemory(device, sdlOverlayImageMemory, nullptr);
1866
[d22ae72]1867 cleanupPipelineBuffers(scenePipeline);
1868 cleanupPipelineBuffers(overlayPipeline);
[80edd70]1869
[47bff4c]1870 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
1871 vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
[621664a]1872 vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
[47bff4c]1873 vkDestroyFence(device, inFlightFences[i], nullptr);
1874 }
1875
1876 vkDestroyCommandPool(device, commandPool, nullptr);
1877
[909b51a]1878 vkDestroyDevice(device, nullptr);
1879
[80de39d]1880 if (enableValidationLayers) {
1881 DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
1882 }
1883
[b3671b5]1884 vkDestroySurfaceKHR(instance, surface, nullptr);
[826df16]1885 vkDestroyInstance(instance, nullptr);
1886
[5f3dba8]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
[7fc5e27]1911 gui->destroyWindow();
1912 gui->shutdown();
[98f3232]1913 delete gui;
[826df16]1914 }
[e09ad38]1915
[621664a]1916 void cleanupSwapChain() {
[adcd252]1917 vkDestroyImageView(device, depthImageView, nullptr);
1918 vkDestroyImage(device, depthImage, nullptr);
1919 vkFreeMemory(device, depthImageMemory, nullptr);
1920
[621664a]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
[d22ae72]1927 cleanupPipeline(scenePipeline);
1928 cleanupPipeline(overlayPipeline);
[b8b32bd]1929
[621664a]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 }
[d22ae72]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);
[621664a]1952
[d22ae72]1953 vkDestroyBuffer(device, pipeline.vertexBuffer, nullptr);
1954 vkFreeMemory(device, pipeline.vertexBufferMemory, nullptr);
1955 vkDestroyBuffer(device, pipeline.indexBuffer, nullptr);
1956 vkFreeMemory(device, pipeline.indexBufferMemory, nullptr);
[621664a]1957 }
1958
[e09ad38]1959 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
[621664a]1960 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
1961 VkDebugUtilsMessageTypeFlagsEXT messageType,
1962 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
1963 void* pUserData) {
[e09ad38]1964 cerr << "validation layer: " << pCallbackData->pMessage << endl;
1965
1966 return VK_FALSE;
[0e6ecf3]1967 }
[e09ad38]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
[621664a]1976 size_t fileSize = (size_t) file.tellg();
[e09ad38]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 }
[826df16]1986};
1987
[1c6cd5e]1988int main(int argc, char* argv[]) {
[826df16]1989
[b6127d2]1990#ifdef NDEBUG
1991 cout << "DEBUGGING IS OFF" << endl;
1992#else
1993 cout << "DEBUGGING IS ON" << endl;
1994#endif
[a8f0577]1995
[826df16]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 }
[03f4c64]2006
[826df16]2007 cout << "Finished running the game" << endl;
[03f4c64]2008
[826df16]2009 return EXIT_SUCCESS;
[03f4c64]2010}
Note: See TracBrowser for help on using the repository browser.