source: opengl-game/vulkan-game.cpp@ d22ae72

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

Create a separapte descriptor pool and vector of descriptor sets for each graphics pipeline

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