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

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

Fix syntax errors in vulkan-game.cpp

  • Property mode set to 100644
File size: 59.0 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
[80edd70]16#include <glm/glm.hpp>
[de32fda]17#include <glm/gtc/matrix_transform.hpp>
[03f4c64]18
[eea05dd]19#define STB_IMAGE_IMPLEMENTATION
20#include "stb_image.h"
21
[03f4c64]22#include <iostream>
[80edd70]23#include <fstream>
24#include <algorithm>
25#include <vector>
26#include <array>
[0e6ecf3]27#include <set>
[80edd70]28#include <optional>
[de32fda]29#include <chrono>
[03f4c64]30
31using namespace std;
32
[826df16]33const int SCREEN_WIDTH = 800;
34const int SCREEN_HEIGHT = 600;
35
[47bff4c]36const int MAX_FRAMES_IN_FLIGHT = 2;
37
[826df16]38#ifdef NDEBUG
39 const bool enableValidationLayers = false;
40#else
41 const bool enableValidationLayers = true;
42#endif
43
[bfd620e]44const vector<const char*> validationLayers = {
45 "VK_LAYER_KHRONOS_validation"
46};
47
48const vector<const char*> deviceExtensions = {
49 VK_KHR_SWAPCHAIN_EXTENSION_NAME
50};
51
[909b51a]52struct QueueFamilyIndices {
53 optional<uint32_t> graphicsFamily;
[b3671b5]54 optional<uint32_t> presentFamily;
[909b51a]55
56 bool isComplete() {
[b3671b5]57 return graphicsFamily.has_value() && presentFamily.has_value();
[909b51a]58 }
59};
60
[bfd620e]61struct SwapChainSupportDetails {
62 VkSurfaceCapabilitiesKHR capabilities;
63 vector<VkSurfaceFormatKHR> formats;
64 vector<VkPresentModeKHR> presentModes;
65};
66
[80edd70]67struct Vertex {
68 glm::vec2 pos;
69 glm::vec3 color;
70
71 static VkVertexInputBindingDescription getBindingDescription() {
72 VkVertexInputBindingDescription bindingDescription = {};
73
74 bindingDescription.binding = 0;
75 bindingDescription.stride = sizeof(Vertex);
76 bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
77
78 return bindingDescription;
79 }
80
81 static array<VkVertexInputAttributeDescription, 2> getAttributeDescriptions() {
82 array<VkVertexInputAttributeDescription, 2> attributeDescriptions = {};
83
84 attributeDescriptions[0].binding = 0;
85 attributeDescriptions[0].location = 0;
86 attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT;
87 attributeDescriptions[0].offset = offsetof(Vertex, pos);
88
89 attributeDescriptions[1].binding = 0;
90 attributeDescriptions[1].location = 1;
91 attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
92 attributeDescriptions[1].offset = offsetof(Vertex, color);
93
94 return attributeDescriptions;
95 }
96};
97
[de32fda]98struct UniformBufferObject {
99 glm::mat4 model;
100 glm::mat4 view;
101 glm::mat4 proj;
102};
103
[80edd70]104const vector<Vertex> vertices = {
[cae7a2c]105 {{-0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}},
106 {{ 0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}},
107 {{ 0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}},
108 {{-0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}}
109};
110
111const vector<uint16_t> indices = {
112 0, 1, 2, 2, 3, 0
[80edd70]113};
114
[b6127d2]115VkResult CreateDebugUtilsMessengerEXT(VkInstance instance,
116 const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
117 const VkAllocationCallbacks* pAllocator,
118 VkDebugUtilsMessengerEXT* pDebugMessenger) {
119 auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(
120 instance, "vkCreateDebugUtilsMessengerEXT");
121
122 if (func != nullptr) {
123 return func(instance, pCreateInfo, pAllocator, pDebugMessenger);
124 } else {
125 return VK_ERROR_EXTENSION_NOT_PRESENT;
126 }
127}
128
[80de39d]129void DestroyDebugUtilsMessengerEXT(VkInstance instance,
130 VkDebugUtilsMessengerEXT debugMessenger,
131 const VkAllocationCallbacks* pAllocator) {
132 auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(
133 instance, "vkDestroyDebugUtilsMessengerEXT");
134
135 if (func != nullptr) {
136 func(instance, debugMessenger, pAllocator);
137 }
138}
139
[826df16]140class VulkanGame {
141 public:
142 void run() {
143 if (initWindow() == RTWO_ERROR) {
144 return;
145 }
146 initVulkan();
147 mainLoop();
148 cleanup();
149 }
150 private:
[98f3232]151 GameGui* gui = new GameGui_SDL();
[80de39d]152 SDL_Window* window = nullptr;
[826df16]153
154 VkInstance instance;
[b6127d2]155 VkDebugUtilsMessengerEXT debugMessenger;
[b3671b5]156 VkSurfaceKHR surface;
[321272c]157 SDL_Surface* sdlSurface = nullptr;
[b3671b5]158
[909b51a]159 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
160 VkDevice device;
[b3671b5]161
[909b51a]162 VkQueue graphicsQueue;
[b3671b5]163 VkQueue presentQueue;
[826df16]164
[bfd620e]165 VkSwapchainKHR swapChain;
166 vector<VkImage> swapChainImages;
167 VkFormat swapChainImageFormat;
168 VkExtent2D swapChainExtent;
169
170 vector<VkImageView> swapChainImageViews;
[be34c9a]171 VkRenderPass renderPass;
[de32fda]172 VkDescriptorSetLayout descriptorSetLayout;
[84216c7]173 VkPipelineLayout pipelineLayout;
[fd70015]174 VkPipeline graphicsPipeline;
[c7fb883]175 VkDescriptorPool descriptorPool;
176 vector<VkDescriptorSet> descriptorSets;
[bfd620e]177
[f5d5686]178 VkCommandPool commandPool;
179
180 VkImage textureImage;
181 VkDeviceMemory textureImageMemory;
182
[80edd70]183 VkBuffer vertexBuffer;
184 VkDeviceMemory vertexBufferMemory;
[de32fda]185
[cae7a2c]186 VkBuffer indexBuffer;
187 VkDeviceMemory indexBufferMemory;
[80edd70]188
[de32fda]189 vector<VkBuffer> uniformBuffers;
190 vector<VkDeviceMemory> uniformBuffersMemory;
191
[ebeb3aa]192 vector<VkFramebuffer> swapChainFramebuffers;
[47bff4c]193 vector<VkCommandBuffer> commandBuffers;
194
195 vector<VkSemaphore> imageAvailableSemaphores;
196 vector<VkSemaphore> renderFinishedSemaphores;
197 vector<VkFence> inFlightFences;
198
199 size_t currentFrame = 0;
[ebeb3aa]200
[75108ef]201 bool framebufferResized = false;
202
[826df16]203 bool initWindow() {
[98f3232]204 if (gui->Init() == RTWO_ERROR) {
[826df16]205 cout << "UI library could not be initialized!" << endl;
206 return RTWO_ERROR;
207 } else {
[0e6ecf3]208 window = (SDL_Window*) gui->CreateWindow("Vulkan Game", SCREEN_WIDTH, SCREEN_HEIGHT);
[826df16]209
[80de39d]210 if (window == nullptr) {
[826df16]211 cout << "Window could not be created!" << endl;
212 return RTWO_ERROR;
213 } else {
214 return RTWO_SUCCESS;
215 }
216 }
217 }
218
219 void initVulkan() {
220 createInstance();
[7dcd925]221 setupDebugMessenger();
[b3671b5]222 createSurface();
[909b51a]223 pickPhysicalDevice();
224 createLogicalDevice();
[bfd620e]225 createSwapChain();
226 createImageViews();
[be34c9a]227 createRenderPass();
[de32fda]228 createDescriptorSetLayout();
[4befb76]229 createGraphicsPipeline();
[ebeb3aa]230 createFramebuffers();
[47bff4c]231 createCommandPool();
[eea05dd]232 createTextureImage();
[80edd70]233 createVertexBuffer();
[cae7a2c]234 createIndexBuffer();
[de32fda]235 createUniformBuffers();
[c7fb883]236 createDescriptorPool();
237 createDescriptorSets();
[47bff4c]238 createCommandBuffers();
239 createSyncObjects();
[826df16]240 }
241
[75108ef]242 void recreateSwapChain() {
243 int width = 0, height = 0;
[8667f76]244 gui->GetWindowSize(&width, &height);
[75108ef]245
246 while (width == 0 || height == 0 ||
247 (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) {
248 SDL_WaitEvent(nullptr);
[8667f76]249 gui->GetWindowSize(&width, &height);
[75108ef]250 }
251
252 vkDeviceWaitIdle(device);
253
254 cleanupSwapChain();
255
256 createSwapChain();
257 createImageViews();
258 createRenderPass();
259 createGraphicsPipeline();
260 createFramebuffers();
[de32fda]261 createUniformBuffers();
[c7fb883]262 createDescriptorPool();
263 createDescriptorSets();
[75108ef]264 createCommandBuffers();
265 }
266
267 void cleanupSwapChain() {
268 for (auto framebuffer : swapChainFramebuffers) {
269 vkDestroyFramebuffer(device, framebuffer, nullptr);
270 }
271
272 vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
273
274 vkDestroyPipeline(device, graphicsPipeline, nullptr);
275 vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
276 vkDestroyRenderPass(device, renderPass, nullptr);
277
278 for (auto imageView : swapChainImageViews) {
279 vkDestroyImageView(device, imageView, nullptr);
280 }
281
282 vkDestroySwapchainKHR(device, swapChain, nullptr);
[de32fda]283
284 for (size_t i = 0; i < swapChainImages.size(); i++) {
285 vkDestroyBuffer(device, uniformBuffers[i], nullptr);
286 vkFreeMemory(device, uniformBuffersMemory[i], nullptr);
287 }
[c7fb883]288
289 vkDestroyDescriptorPool(device, descriptorPool, nullptr);
[75108ef]290 }
291
[826df16]292 void createInstance() {
[b6127d2]293 if (enableValidationLayers && !checkValidationLayerSupport()) {
294 throw runtime_error("validation layers requested, but not available!");
295 }
296
[826df16]297 VkApplicationInfo appInfo = {};
298 appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
299 appInfo.pApplicationName = "Vulkan Game";
300 appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
301 appInfo.pEngineName = "No Engine";
302 appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
303 appInfo.apiVersion = VK_API_VERSION_1_0;
304
305 VkInstanceCreateInfo createInfo = {};
306 createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
307 createInfo.pApplicationInfo = &appInfo;
308
[a8f0577]309 vector<const char*> extensions = getRequiredExtensions();
[b6127d2]310 createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
311 createInfo.ppEnabledExtensionNames = extensions.data();
[826df16]312
[8667f76]313 cout << endl << "Extensions:" << endl;
[b3671b5]314 for (const char* extensionName : extensions) {
315 cout << extensionName << endl;
316 }
317 cout << endl;
318
[80de39d]319 VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo;
[b6127d2]320 if (enableValidationLayers) {
321 createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
322 createInfo.ppEnabledLayerNames = validationLayers.data();
[80de39d]323
324 populateDebugMessengerCreateInfo(debugCreateInfo);
325 createInfo.pNext = &debugCreateInfo;
[b6127d2]326 } else {
327 createInfo.enabledLayerCount = 0;
[80de39d]328
329 createInfo.pNext = nullptr;
[b6127d2]330 }
[826df16]331
332 if (vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS) {
333 throw runtime_error("failed to create instance!");
334 }
335 }
336
[80de39d]337 void setupDebugMessenger() {
338 if (!enableValidationLayers) return;
339
340 VkDebugUtilsMessengerCreateInfoEXT createInfo;
341 populateDebugMessengerCreateInfo(createInfo);
[b6127d2]342
343 if (CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) {
344 throw runtime_error("failed to setup debug messenger!");
345 }
346 }
347
[b3671b5]348 void createSurface() {
[321272c]349 sdlSurface = SDL_GetWindowSurface(window);
350
351 if (sdlSurface == nullptr) {
352 cout << "Could not get SDL Surface! =(" << endl;
353 }
[b3671b5]354
[0e6ecf3]355 if (gui->CreateVulkanSurface(instance, &surface) == RTWO_ERROR) {
[b3671b5]356 throw runtime_error("failed to create window surface!");
357 }
358 }
359
[909b51a]360 void pickPhysicalDevice() {
361 uint32_t deviceCount = 0;
362 vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
363
364 if (deviceCount == 0) {
365 throw runtime_error("failed to find GPUs with Vulkan support!");
366 }
367
368 vector<VkPhysicalDevice> devices(deviceCount);
369 vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
370
371 cout << endl << "Graphics cards:" << endl;
372 for (const VkPhysicalDevice& device : devices) {
373 if (isDeviceSuitable(device)) {
374 physicalDevice = device;
375 break;
376 }
377 }
378 cout << endl;
379
380 if (physicalDevice == VK_NULL_HANDLE) {
381 throw runtime_error("failed to find a suitable GPU!");
382 }
383 }
384
385 bool isDeviceSuitable(VkPhysicalDevice device) {
386 VkPhysicalDeviceProperties deviceProperties;
387 VkPhysicalDeviceFeatures deviceFeatures;
388
389 vkGetPhysicalDeviceProperties(device, &deviceProperties);
390 vkGetPhysicalDeviceFeatures(device, &deviceFeatures);
391
392 cout << "Device: " << deviceProperties.deviceName << endl;
393
394 QueueFamilyIndices indices = findQueueFamilies(device);
395
[bfd620e]396 bool extensionsSupported = checkDeviceExtensionSupport(device);
397
398 bool swapChainAdequate = false;
399
400 if (extensionsSupported) {
401 SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device);
402 swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
403 }
404
405 return indices.isComplete() && extensionsSupported && swapChainAdequate;
406 }
407
408 bool checkDeviceExtensionSupport(VkPhysicalDevice device) {
409 uint32_t extensionCount;
410 vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
411
412 vector<VkExtensionProperties> availableExtensions(extensionCount);
413 vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
414
415 set<string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
416
417 for (const auto& extension : availableExtensions) {
418 requiredExtensions.erase(extension.extensionName);
419 }
420
421 return requiredExtensions.empty();
[909b51a]422 }
423
424 void createLogicalDevice() {
425 QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
426
[b3671b5]427 vector<VkDeviceQueueCreateInfo> queueCreateInfos;
428 set<uint32_t> uniqueQueueFamilies = {indices.graphicsFamily.value(), indices.presentFamily.value()};
[909b51a]429
430 float queuePriority = 1.0f;
[b3671b5]431 for (uint32_t queueFamily : uniqueQueueFamilies) {
432 VkDeviceQueueCreateInfo queueCreateInfo = {};
433
434 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
435 queueCreateInfo.queueFamilyIndex = queueFamily;
436 queueCreateInfo.queueCount = 1;
437 queueCreateInfo.pQueuePriorities = &queuePriority;
438
439 queueCreateInfos.push_back(queueCreateInfo);
440 }
[909b51a]441
442 VkPhysicalDeviceFeatures deviceFeatures = {};
443
444 VkDeviceCreateInfo createInfo = {};
445 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
446
[b3671b5]447 createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());;
448 createInfo.pQueueCreateInfos = queueCreateInfos.data();
[909b51a]449
450 createInfo.pEnabledFeatures = &deviceFeatures;
451
[bfd620e]452 createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
453 createInfo.ppEnabledExtensionNames = deviceExtensions.data();
[909b51a]454
455 // These fields are ignored by up-to-date Vulkan implementations,
456 // but it's a good idea to set them for backwards compatibility
457 if (enableValidationLayers) {
458 createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
459 createInfo.ppEnabledLayerNames = validationLayers.data();
460 } else {
461 createInfo.enabledLayerCount = 0;
462 }
463
464 if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
465 throw runtime_error("failed to create logical device!");
466 }
467
468 vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
[b3671b5]469 vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
[909b51a]470 }
471
[a8f0577]472 bool checkValidationLayerSupport() {
473 uint32_t layerCount;
474 vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
475
476 vector<VkLayerProperties> availableLayers(layerCount);
477 vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
478
479 for (const char* layerName : validationLayers) {
480 bool layerFound = false;
481
482 for (const auto& layerProperties : availableLayers) {
483 if (strcmp(layerName, layerProperties.layerName) == 0) {
484 layerFound = true;
485 break;
486 }
487 }
488
489 if (!layerFound) {
490 return false;
491 }
492 }
493
494 return true;
495 }
496
[909b51a]497 QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
498 QueueFamilyIndices indices;
499
500 uint32_t queueFamilyCount = 0;
501 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
502
503 vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
504 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
505
506 int i = 0;
507 for (const auto& queueFamily : queueFamilies) {
508 if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
509 indices.graphicsFamily = i;
510 }
511
[b3671b5]512 VkBool32 presentSupport = false;
513 vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
514
515 if (queueFamily.queueCount > 0 && presentSupport) {
516 indices.presentFamily = i;
517 }
518
[909b51a]519 if (indices.isComplete()) {
520 break;
521 }
522
523 i++;
524 }
525
526 return indices;
527 }
528
[bfd620e]529 SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) {
530 SwapChainSupportDetails details;
531
532 vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
533
534 uint32_t formatCount;
535 vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
536
537 if (formatCount != 0) {
538 details.formats.resize(formatCount);
539 vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());
540 }
541
542 uint32_t presentModeCount;
543 vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);
544
545 if (presentModeCount != 0) {
546 details.presentModes.resize(presentModeCount);
547 vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data());
548 }
549
550 return details;
551 }
552
553 VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) {
554 for (const auto& availableFormat : availableFormats) {
555 if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
556 return availableFormat;
557 }
558 }
559
560 return availableFormats[0];
561 }
562
563 VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes) {
564 VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR;
565
566 for (const auto& availablePresentMode : availablePresentModes) {
567 if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
568 return availablePresentMode;
569 } else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {
570 bestMode = availablePresentMode;
571 }
572 }
573
574 return bestMode;
575 }
576
577 VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {
578 if (capabilities.currentExtent.width != numeric_limits<uint32_t>::max()) {
579 return capabilities.currentExtent;
580 } else {
[75108ef]581 int width, height;
[8667f76]582 gui->GetWindowSize(&width, &height);
[75108ef]583
584 VkExtent2D actualExtent = {
585 static_cast<uint32_t>(width),
586 static_cast<uint32_t>(height)
587 };
[bfd620e]588
589 actualExtent.width = max(capabilities.minImageExtent.width, min(capabilities.maxImageExtent.width, actualExtent.width));
590 actualExtent.height = max(capabilities.minImageExtent.height, min(capabilities.maxImageExtent.height, actualExtent.height));
591
592 return actualExtent;
593 }
594 }
595
[909b51a]596 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {
597 createInfo = {};
598 createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
599 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;
600 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;
601 createInfo.pfnUserCallback = debugCallback;
602 }
603
[a8f0577]604 vector<const char*> getRequiredExtensions() {
[8667f76]605 vector<const char*> extensions = gui->GetRequiredExtensions();
[a8f0577]606
607 if (enableValidationLayers) {
608 extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
609 }
610
611 return extensions;
612 }
613
[bfd620e]614 void createSwapChain() {
615 SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice);
616
617 VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
618 VkPresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
619 VkExtent2D extent = chooseSwapExtent(swapChainSupport.capabilities);
620
621 uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
622 if (swapChainSupport.capabilities.maxImageCount > 0 &&
623 imageCount > swapChainSupport.capabilities.maxImageCount) {
624 imageCount = swapChainSupport.capabilities.maxImageCount;
625 }
626
627 VkSwapchainCreateInfoKHR createInfo = {};
628
629 createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
630 createInfo.surface = surface;
631 createInfo.minImageCount = imageCount;
632 createInfo.imageFormat = surfaceFormat.format;
633 createInfo.imageColorSpace = surfaceFormat.colorSpace;
634 createInfo.imageExtent = extent;
635 createInfo.imageArrayLayers = 1;
636 createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
637
638 QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
639 uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()};
640
641 if (indices.graphicsFamily != indices.presentFamily) {
642 createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
643 createInfo.queueFamilyIndexCount = 2;
644 createInfo.pQueueFamilyIndices = queueFamilyIndices;
645 } else {
646 createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
647 createInfo.queueFamilyIndexCount = 0; // Optional
648 createInfo.pQueueFamilyIndices = nullptr;
649 }
650
651 createInfo.preTransform = swapChainSupport.capabilities.currentTransform;
652 createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
653 createInfo.presentMode = presentMode;
654 createInfo.clipped = VK_TRUE;
655 createInfo.oldSwapchain = VK_NULL_HANDLE;
656
657 if (vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapChain) != VK_SUCCESS) {
658 throw runtime_error("failed to create swap chain!");
659 }
660
661 vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr);
662 swapChainImages.resize(imageCount);
663 vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
664
665 swapChainImageFormat = surfaceFormat.format;
666 swapChainExtent = extent;
667 }
668
669 void createImageViews() {
670 swapChainImageViews.resize(swapChainImages.size());
671
672 for (size_t i=0; i<swapChainImages.size(); i++) {
673 VkImageViewCreateInfo createInfo = {};
674 createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
675 createInfo.image = swapChainImages[i];
676
677 createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
678 createInfo.format = swapChainImageFormat;
679
680 createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
681 createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
682 createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
683 createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
684
685 createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
686 createInfo.subresourceRange.baseMipLevel = 0;
687 createInfo.subresourceRange.levelCount = 1;
688 createInfo.subresourceRange.baseArrayLayer = 0;
689 createInfo.subresourceRange.layerCount = 1;
690
691 if (vkCreateImageView(device, &createInfo, nullptr, &swapChainImageViews[i]) != VK_SUCCESS) {
692 throw runtime_error("failed to create image views!");
693 }
694 }
695 }
696
[be34c9a]697 void createRenderPass() {
698 VkAttachmentDescription colorAttachment = {};
699 colorAttachment.format = swapChainImageFormat;
700 colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
701 colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
702 colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
703 colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
704 colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
705 colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
706 colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
707
708 VkAttachmentReference colorAttachmentRef = {};
709 colorAttachmentRef.attachment = 0;
710 colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
711
712 VkSubpassDescription subpass = {};
713 subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
714 subpass.colorAttachmentCount = 1;
715 subpass.pColorAttachments = &colorAttachmentRef;
716
[47bff4c]717 VkSubpassDependency dependency = {};
718 dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
719 dependency.dstSubpass = 0;
720 dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
721 dependency.srcAccessMask = 0;
722 dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
723 dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
724
[be34c9a]725 VkRenderPassCreateInfo renderPassInfo = {};
726 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
727 renderPassInfo.attachmentCount = 1;
728 renderPassInfo.pAttachments = &colorAttachment;
729 renderPassInfo.subpassCount = 1;
730 renderPassInfo.pSubpasses = &subpass;
[47bff4c]731 renderPassInfo.dependencyCount = 1;
732 renderPassInfo.pDependencies = &dependency;
[be34c9a]733
734 if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
735 throw runtime_error("failed to create render pass!");
736 }
737 }
738
[de32fda]739 void createDescriptorSetLayout() {
740 VkDescriptorSetLayoutBinding uboLayoutBinding = {};
741 uboLayoutBinding.binding = 0;
742 uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
743 uboLayoutBinding.descriptorCount = 1;
744 uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
745 uboLayoutBinding.pImmutableSamplers = nullptr;
746
747 VkDescriptorSetLayoutCreateInfo layoutInfo = {};
748 layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
749 layoutInfo.bindingCount = 1;
750 layoutInfo.pBindings = &uboLayoutBinding;
751
752 if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS) {
753 throw runtime_error("failed to create descriptor set layout!");
754 }
755 }
756
[4befb76]757 void createGraphicsPipeline() {
[e09ad38]758 auto vertShaderCode = readFile("shaders/vert.spv");
759 auto fragShaderCode = readFile("shaders/frag.spv");
760
761 VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
762 VkShaderModule fragShaderModule = createShaderModule(fragShaderCode);
763
764 VkPipelineShaderStageCreateInfo vertShaderStageInfo = {};
765 vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
766 vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
767 vertShaderStageInfo.module = vertShaderModule;
768 vertShaderStageInfo.pName = "main";
769
770 VkPipelineShaderStageCreateInfo fragShaderStageInfo = {};
771 fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
772 fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
773 fragShaderStageInfo.module = fragShaderModule;
774 fragShaderStageInfo.pName = "main";
775
776 VkPipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };
777
[84216c7]778 VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
779 vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
[80edd70]780
781 auto bindingDescription = Vertex::getBindingDescription();
782 auto attributeDescriptions = Vertex::getAttributeDescriptions();
783
784 vertexInputInfo.vertexBindingDescriptionCount = 1;
785 vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributeDescriptions.size());
786 vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
787 vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();
[84216c7]788
789 VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
790 inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
791 inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
792 inputAssembly.primitiveRestartEnable = VK_FALSE;
793
794 VkViewport viewport = {};
795 viewport.x = 0.0f;
796 viewport.y = 0.0f;
797 viewport.width = (float) swapChainExtent.width;
798 viewport.height = (float) swapChainExtent.height;
799 viewport.minDepth = 0.0f;
800 viewport.maxDepth = 1.0f;
801
802 VkRect2D scissor = {};
803 scissor.offset = { 0, 0 };
804 scissor.extent = swapChainExtent;
805
806 VkPipelineViewportStateCreateInfo viewportState = {};
807 viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
808 viewportState.viewportCount = 1;
809 viewportState.pViewports = &viewport;
810 viewportState.scissorCount = 1;
811 viewportState.pScissors = &scissor;
812
813 VkPipelineRasterizationStateCreateInfo rasterizer = {};
814 rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
815 rasterizer.depthClampEnable = VK_FALSE;
816 rasterizer.rasterizerDiscardEnable = VK_FALSE;
817 rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
818 rasterizer.lineWidth = 1.0f;
819 rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
[c7fb883]820 rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
[84216c7]821 rasterizer.depthBiasEnable = false;
822
823 VkPipelineMultisampleStateCreateInfo multisampling = {};
824 multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
825 multisampling.sampleShadingEnable = VK_FALSE;
826 multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
827
828 VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
829 colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
830 colorBlendAttachment.blendEnable = VK_FALSE;
831
832 VkPipelineColorBlendStateCreateInfo colorBlending = {};
833 colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
834 colorBlending.logicOpEnable = VK_FALSE;
835 colorBlending.logicOp = VK_LOGIC_OP_COPY;
836 colorBlending.attachmentCount = 1;
837 colorBlending.pAttachments = &colorBlendAttachment;
838 colorBlending.blendConstants[0] = 0.0f;
839 colorBlending.blendConstants[1] = 0.0f;
840 colorBlending.blendConstants[2] = 0.0f;
841 colorBlending.blendConstants[3] = 0.0f;
842
843 VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
844 pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
[de32fda]845 pipelineLayoutInfo.setLayoutCount = 1;
846 pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout;
[84216c7]847 pipelineLayoutInfo.pushConstantRangeCount = 0;
848
849 if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
850 throw runtime_error("failed to create pipeline layout!");
851 }
852
[fd70015]853 VkGraphicsPipelineCreateInfo pipelineInfo = {};
854 pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
855 pipelineInfo.stageCount = 2;
856 pipelineInfo.pStages = shaderStages;
857 pipelineInfo.pVertexInputState = &vertexInputInfo;
858 pipelineInfo.pInputAssemblyState = &inputAssembly;
859 pipelineInfo.pViewportState = &viewportState;
860 pipelineInfo.pRasterizationState = &rasterizer;
861 pipelineInfo.pMultisampleState = &multisampling;
862 pipelineInfo.pDepthStencilState = nullptr;
863 pipelineInfo.pColorBlendState = &colorBlending;
864 pipelineInfo.pDynamicState = nullptr;
865 pipelineInfo.layout = pipelineLayout;
866 pipelineInfo.renderPass = renderPass;
867 pipelineInfo.subpass = 0;
868 pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
869 pipelineInfo.basePipelineIndex = -1;
870
871 if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) {
872 throw runtime_error("failed to create graphics pipeline!");
873 }
874
[e09ad38]875 vkDestroyShaderModule(device, vertShaderModule, nullptr);
876 vkDestroyShaderModule(device, fragShaderModule, nullptr);
877 }
878
879 VkShaderModule createShaderModule(const vector<char>& code) {
880 VkShaderModuleCreateInfo createInfo = {};
881 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
882 createInfo.codeSize = code.size();
883 createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
884
885 VkShaderModule shaderModule;
886 if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
887 throw runtime_error("failed to create shader module!");
888 }
889
890 return shaderModule;
[4befb76]891 }
892
[ebeb3aa]893 void createFramebuffers() {
894 swapChainFramebuffers.resize(swapChainImageViews.size());
895
896 for (size_t i = 0; i < swapChainImageViews.size(); i++) {
897 VkImageView attachments[] = {
898 swapChainImageViews[i]
899 };
900
901 VkFramebufferCreateInfo framebufferInfo = {};
902 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
903 framebufferInfo.renderPass = renderPass;
904 framebufferInfo.attachmentCount = 1;
905 framebufferInfo.pAttachments = attachments;
906 framebufferInfo.width = swapChainExtent.width;
907 framebufferInfo.height = swapChainExtent.height;
908 framebufferInfo.layers = 1;
909
910 if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) {
911 throw runtime_error("failed to create framebuffer!");
912 }
913 }
914 }
915
[47bff4c]916 void createCommandPool() {
917 QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice);
918
919 VkCommandPoolCreateInfo poolInfo = {};
920 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
921 poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
922 poolInfo.flags = 0;
923
924 if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) {
925 throw runtime_error("failed to create command pool!");
926 }
927 }
928
[eea05dd]929 void createTextureImage() {
930 int texWidth, texHeight, texChannels;
931
932 stbi_uc* pixels = stbi_load("textures/texture.jpg", &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
933 VkDeviceSize imageSize = texWidth * texHeight * 4;
934
935 if (!pixels) {
936 throw runtime_error("failed to load texture image!");
937 }
938
939 VkBuffer stagingBuffer;
940 VkDeviceMemory stagingBufferMemory;
941
942 createBuffer(imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
943 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
944 stagingBuffer, stagingBufferMemory);
945
946 void* data;
947
948 vkMapMemory(device, stagingBufferMemory, 0, imageSize, 0, &data);
949 memcpy(data, pixels, static_cast<size_t>(imageSize));
950 vkUnmapMemory(device, stagingBufferMemory);
951
952 stbi_image_free(pixels);
953
954 createImage(texWidth, texHeight, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TILING_OPTIMAL,
955 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
956 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, textureImage, textureImageMemory);
957
958 transitionImageLayout(textureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
959 copyBufferToImage(stagingBuffer, textureImage, static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight));
960 transitionImageLayout(textureImage, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
961
962 vkDestroyBuffer(device, stagingBuffer, nullptr);
[f5d5686]963 vkFreeMemory(device, stagingBufferMemory, nullptr);
[eea05dd]964 }
965
966 void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling,
[f5d5686]967 VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image,
968 VkDeviceMemory& imageMemory) {
[eea05dd]969 VkImageCreateInfo imageInfo = {};
970 imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
971 imageInfo.imageType = VK_IMAGE_TYPE_2D;
972 imageInfo.extent.width = width;
973 imageInfo.extent.height = height;
974 imageInfo.extent.depth = 1;
975 imageInfo.mipLevels = 1;
976 imageInfo.arrayLayers = 1;
977 imageInfo.format = format;
978 imageInfo.tiling = tiling;
979 imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
980 imageInfo.usage = usage;
981 imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
982 imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
983
984 if (vkCreateImage(device, &imageInfo, nullptr, &image) != VK_SUCCESS) {
985 throw runtime_error("failed to create image!");
986 }
987
988 VkMemoryRequirements memRequirements;
989 vkGetImageMemoryRequirements(device, image, &memRequirements);
990
991 VkMemoryAllocateInfo allocInfo;
992 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
993 allocInfo.allocationSize = memRequirements.size;
994 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
995
996 if (vkAllocateMemory(device, &allocInfo, nullptr, &imageMemory) != VK_SUCCESS) {
997 throw runtime_error("failed to allocate image memory!");
998 }
999
1000 vkBindImageMemory(device, image, imageMemory, 0);
1001 }
1002
[f5d5686]1003 void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout,
1004 VkImageLayout newLayout) {
[eea05dd]1005 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
1006
1007 VkImageMemoryBarrier barrier = {};
1008 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1009 barrier.oldLayout = oldLayout;
1010 barrier.newLayout = newLayout;
1011 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1012 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1013 barrier.image = image;
1014 barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1015 barrier.subresourceRange.baseMipLevel = 0;
1016 barrier.subresourceRange.levelCount = 1;
1017 barrier.subresourceRange.baseArrayLayer = 0;
1018 barrier.subresourceRange.layerCount = 1;
[f5d5686]1019
1020 VkPipelineStageFlags sourceStage;
1021 VkPipelineStageFlags destinationStage;
[eea05dd]1022
1023 if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
1024 barrier.srcAccessMask = 0;
1025 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1026
1027 sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
1028 destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
1029 } else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
1030 barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1031 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1032
1033 sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
1034 destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
1035 } else {
1036 throw invalid_argument("unsupported layout transition!");
1037 }
1038
1039 vkCmdPipelineBarrier(
1040 commandBuffer,
[f5d5686]1041 sourceStage, destinationStage,
[eea05dd]1042 0,
1043 0, nullptr,
1044 0, nullptr,
1045 1, &barrier
1046 );
1047
1048 endSingleTimeCommands(commandBuffer);
1049 }
1050
1051 void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height) {
1052 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
1053
1054 VkBufferImageCopy region = {};
1055 region.bufferOffset = 0;
1056 region.bufferRowLength = 0;
1057 region.bufferImageHeight = 0;
1058 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1059 region.imageSubresource.mipLevel = 0;
1060 region.imageSubresource.baseArrayLayer = 0;
1061 region.imageSubresource.layerCount = 1;
1062 region.imageOffset = { 0, 0, 0 };
1063 region.imageExtent = { width, height, 1 };
1064
1065 vkCmdCopyBufferToImage(
1066 commandBuffer,
1067 buffer,
1068 image,
1069 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1070 1,
1071 &region
1072 );
1073
1074 endSingleTimeCommands(commandBuffer);
1075 }
1076
[80edd70]1077 void createVertexBuffer() {
[d9ef6ab]1078 VkDeviceSize bufferSize = sizeof(vertices[0]) * vertices.size();
1079
1080 VkBuffer stagingBuffer;
1081 VkDeviceMemory stagingBufferMemory;
1082 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1083 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1084 stagingBuffer, stagingBufferMemory);
1085
1086 void* data;
1087 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
1088 memcpy(data, vertices.data(), (size_t)bufferSize);
1089 vkUnmapMemory(device, stagingBufferMemory);
1090
1091 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
1092 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vertexBuffer, vertexBufferMemory);
1093
1094 copyBuffer(stagingBuffer, vertexBuffer, bufferSize);
1095
1096 vkDestroyBuffer(device, stagingBuffer, nullptr);
1097 vkFreeMemory(device, stagingBufferMemory, nullptr);
1098 }
1099
[cae7a2c]1100 void createIndexBuffer() {
1101 VkDeviceSize bufferSize = sizeof(indices[0]) * indices.size();
1102
1103 VkBuffer stagingBuffer;
1104 VkDeviceMemory stagingBufferMemory;
1105 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1106 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1107 stagingBuffer, stagingBufferMemory);
1108
1109 void* data;
1110 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data);
1111 memcpy(data, indices.data(), (size_t)bufferSize);
1112 vkUnmapMemory(device, stagingBufferMemory);
1113
1114 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
1115 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, indexBuffer, indexBufferMemory);
1116
1117 copyBuffer(stagingBuffer, indexBuffer, bufferSize);
1118
1119 vkDestroyBuffer(device, stagingBuffer, nullptr);
1120 vkFreeMemory(device, stagingBufferMemory, nullptr);
1121 }
1122
[d9ef6ab]1123 void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties,
1124 VkBuffer& buffer, VkDeviceMemory& bufferMemory) {
[80edd70]1125 VkBufferCreateInfo bufferInfo = {};
1126 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
[d9ef6ab]1127 bufferInfo.size = size;
1128 bufferInfo.usage = usage;
[80edd70]1129 bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1130
[d9ef6ab]1131 if (vkCreateBuffer(device, &bufferInfo, nullptr, &buffer) != VK_SUCCESS) {
1132 throw runtime_error("failed to create buffer!");
[80edd70]1133 }
1134
[d9ef6ab]1135 VkMemoryRequirements memRequirements;
1136 vkGetBufferMemoryRequirements(device, buffer, &memRequirements);
[80edd70]1137
1138 VkMemoryAllocateInfo allocInfo = {};
1139 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
[d9ef6ab]1140 allocInfo.allocationSize = memRequirements.size;
1141 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties);
1142
1143 if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) {
1144 throw runtime_error("failed to allocate buffer memory!");
[80edd70]1145 }
1146
[d9ef6ab]1147 vkBindBufferMemory(device, buffer, bufferMemory, 0);
1148 }
[80edd70]1149
[d9ef6ab]1150 void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) {
[eea05dd]1151 VkCommandBuffer commandBuffer = beginSingleTimeCommands();
1152
1153 VkBufferCopy copyRegion = {};
1154 copyRegion.size = size;
1155 vkCmdCopyBuffer(commandBuffer, srcBuffer, dstBuffer, 1, &copyRegion);
1156
1157 endSingleTimeCommands(commandBuffer);
1158 }
1159
1160 VkCommandBuffer beginSingleTimeCommands() {
[d9ef6ab]1161 VkCommandBufferAllocateInfo allocInfo = {};
1162 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1163 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1164 allocInfo.commandPool = commandPool;
1165 allocInfo.commandBufferCount = 1;
1166
1167 VkCommandBuffer commandBuffer;
1168 vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer);
1169
1170 VkCommandBufferBeginInfo beginInfo = {};
1171 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1172 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1173
1174 vkBeginCommandBuffer(commandBuffer, &beginInfo);
1175
[eea05dd]1176 return commandBuffer;
1177 }
[d9ef6ab]1178
[eea05dd]1179 void endSingleTimeCommands(VkCommandBuffer commandBuffer) {
[d9ef6ab]1180 vkEndCommandBuffer(commandBuffer);
1181
1182 VkSubmitInfo submitInfo = {};
1183 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1184 submitInfo.commandBufferCount = 1;
1185 submitInfo.pCommandBuffers = &commandBuffer;
1186
1187 vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
1188 vkQueueWaitIdle(graphicsQueue);
1189
1190 vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
[80edd70]1191 }
1192
1193 uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {
1194 VkPhysicalDeviceMemoryProperties memProperties;
1195 vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
1196
1197 for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
1198 if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
1199 return i;
1200 }
1201 }
1202
1203 throw runtime_error("failed to find suitable memory type!");
1204 }
1205
[c7fb883]1206 void createUniformBuffers() {
1207 VkDeviceSize bufferSize = sizeof(UniformBufferObject);
1208
1209 uniformBuffers.resize(swapChainImages.size());
1210 uniformBuffersMemory.resize(swapChainImages.size());
1211
1212 for (size_t i = 0; i < swapChainImages.size(); i++) {
1213 createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
1214 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1215 uniformBuffers[i], uniformBuffersMemory[i]);
1216 }
1217 }
1218
1219 void createDescriptorPool() {
1220 VkDescriptorPoolSize poolSize = {};
1221 poolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1222 poolSize.descriptorCount = static_cast<uint32_t>(swapChainImages.size());
1223
1224 VkDescriptorPoolCreateInfo poolInfo = {};
1225 poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
1226 poolInfo.poolSizeCount = 1;
1227 poolInfo.pPoolSizes = &poolSize;
1228 poolInfo.maxSets = static_cast<uint32_t>(swapChainImages.size());
1229
1230 if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &descriptorPool) != VK_SUCCESS) {
1231 throw runtime_error("failed to create descriptor pool!");
1232 }
1233 }
1234
1235 void createDescriptorSets() {
1236 vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), descriptorSetLayout);
1237 VkDescriptorSetAllocateInfo allocInfo = {};
1238 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
1239 allocInfo.descriptorPool = descriptorPool;
1240 allocInfo.descriptorSetCount = static_cast<uint32_t>(swapChainImages.size());
1241 allocInfo.pSetLayouts = layouts.data();
1242
1243 descriptorSets.resize(swapChainImages.size());
1244 if (vkAllocateDescriptorSets(device, &allocInfo, descriptorSets.data()) != VK_SUCCESS) {
1245 throw runtime_error("failed to allocate descriptor sets!");
1246 }
1247
1248 for (size_t i = 0; i < swapChainImages.size(); i++) {
1249 VkDescriptorBufferInfo bufferInfo = {};
1250 bufferInfo.buffer = uniformBuffers[i];
1251 bufferInfo.offset = 0;
1252 bufferInfo.range = sizeof(UniformBufferObject);
1253
1254 VkWriteDescriptorSet descriptorWrite = {};
1255 descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1256 descriptorWrite.dstSet = descriptorSets[i];
1257 descriptorWrite.dstBinding = 0;
1258 descriptorWrite.dstArrayElement = 0;
1259 descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1260 descriptorWrite.descriptorCount = 1;
1261 descriptorWrite.pBufferInfo = &bufferInfo;
1262 descriptorWrite.pImageInfo = nullptr;
1263 descriptorWrite.pTexelBufferView = nullptr;
1264
1265 vkUpdateDescriptorSets(device, 1, &descriptorWrite, 0, nullptr);
1266 }
1267 }
1268
[47bff4c]1269 void createCommandBuffers() {
1270 commandBuffers.resize(swapChainFramebuffers.size());
1271
1272 VkCommandBufferAllocateInfo allocInfo = {};
1273 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
1274 allocInfo.commandPool = commandPool;
1275 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
1276 allocInfo.commandBufferCount = (uint32_t)commandBuffers.size();
1277
1278 if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
1279 throw runtime_error("failed to create command buffers!");
1280 }
1281
1282 for (size_t i = 0; i < commandBuffers.size(); i++) {
1283 VkCommandBufferBeginInfo beginInfo = {};
1284 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1285 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
1286 beginInfo.pInheritanceInfo = nullptr;
1287
1288 if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) {
1289 throw runtime_error("failed to begin recording command buffer!");
1290 }
1291
1292 VkRenderPassBeginInfo renderPassInfo = {};
1293 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
1294 renderPassInfo.renderPass = renderPass;
1295 renderPassInfo.framebuffer = swapChainFramebuffers[i];
1296 renderPassInfo.renderArea.offset = { 0, 0 };
1297 renderPassInfo.renderArea.extent = swapChainExtent;
1298
1299 VkClearValue clearColor = { 0.0f, 0.0f, 0.0f, 1.0f };
1300 renderPassInfo.clearValueCount = 1;
1301 renderPassInfo.pClearValues = &clearColor;
1302
1303 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
1304 vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
[80edd70]1305
1306 VkBuffer vertexBuffers[] = { vertexBuffer };
1307 VkDeviceSize offsets[] = { 0 };
1308 vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets);
1309
[cae7a2c]1310 vkCmdBindIndexBuffer(commandBuffers[i], indexBuffer, 0, VK_INDEX_TYPE_UINT16);
[c7fb883]1311 vkCmdBindDescriptorSets(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets[i], 0, nullptr);
[cae7a2c]1312
1313 vkCmdDrawIndexed(commandBuffers[i], static_cast<uint32_t>(indices.size()), 1, 0, 0, 0);
[47bff4c]1314 vkCmdEndRenderPass(commandBuffers[i]);
1315
1316 if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) {
1317 throw runtime_error("failed to record command buffer!");
1318 }
1319 }
1320 }
1321
1322 void createSyncObjects() {
1323 imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
1324 renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
1325 inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
1326
1327 VkSemaphoreCreateInfo semaphoreInfo = {};
1328 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
1329
1330 VkFenceCreateInfo fenceInfo = {};
1331 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1332 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
1333
1334 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
1335 if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS ||
1336 vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS ||
1337 vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {
1338 throw runtime_error("failed to create synchronization objects for a frame!");
1339 }
1340 }
1341 }
1342
[826df16]1343 void mainLoop() {
1344 // TODO: Create some generic event-handling functions in game-gui-*
1345 SDL_Event e;
1346 bool quit = false;
1347
[7dcd925]1348 while (!quit) {
[826df16]1349 while (SDL_PollEvent(&e)) {
1350 if (e.type == SDL_QUIT) {
1351 quit = true;
1352 }
1353 if (e.type == SDL_KEYDOWN) {
1354 quit = true;
1355 }
1356 if (e.type == SDL_MOUSEBUTTONDOWN) {
1357 quit = true;
1358 }
[75108ef]1359 if (e.type == SDL_WINDOWEVENT) {
1360 if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
1361 framebufferResized = true;
1362 } else if (e.window.event == SDL_WINDOWEVENT_MINIMIZED) {
1363 framebufferResized = true;
1364 }
1365 }
[47bff4c]1366 }
[321272c]1367
[47bff4c]1368 drawFrame();
[321272c]1369
[47bff4c]1370 //SDL_FillRect(sdlSurface, nullptr, SDL_MapRGB(sdlSurface->format, 0x00, 0x99, 0x99));
1371 //SDL_UpdateWindowSurface(window);
1372 }
1373
1374 vkDeviceWaitIdle(device);
1375 }
1376
1377 void drawFrame() {
1378 vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max());
1379
1380 uint32_t imageIndex;
1381
[75108ef]1382 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(), imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
1383
1384 if (result == VK_ERROR_OUT_OF_DATE_KHR) {
1385 recreateSwapChain();
1386 return;
1387 } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
1388 throw runtime_error("failed to acquire swap chain image!");
1389 }
[47bff4c]1390
[de32fda]1391 updateUniformBuffer(imageIndex);
1392
[47bff4c]1393 VkSubmitInfo submitInfo = {};
1394 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1395
1396 VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
1397 VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
1398
1399 submitInfo.waitSemaphoreCount = 1;
1400 submitInfo.pWaitSemaphores = waitSemaphores;
1401 submitInfo.pWaitDstStageMask = waitStages;
1402 submitInfo.commandBufferCount = 1;
1403 submitInfo.pCommandBuffers = &commandBuffers[imageIndex];
1404
1405 VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] };
1406
1407 submitInfo.signalSemaphoreCount = 1;
1408 submitInfo.pSignalSemaphores = signalSemaphores;
1409
[75108ef]1410 vkResetFences(device, 1, &inFlightFences[currentFrame]);
1411
[47bff4c]1412 if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) {
1413 throw runtime_error("failed to submit draw command buffer!");
[bfd620e]1414 }
[47bff4c]1415
1416 VkPresentInfoKHR presentInfo = {};
1417 presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
1418
1419 presentInfo.waitSemaphoreCount = 1;
1420 presentInfo.pWaitSemaphores = signalSemaphores;
1421
1422 VkSwapchainKHR swapChains[] = { swapChain };
1423 presentInfo.swapchainCount = 1;
1424 presentInfo.pSwapchains = swapChains;
1425 presentInfo.pImageIndices = &imageIndex;
1426 presentInfo.pResults = nullptr;
1427
[75108ef]1428 result = vkQueuePresentKHR(presentQueue, &presentInfo);
1429
1430 if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized) {
1431 framebufferResized = false;
1432 recreateSwapChain();
1433 } else if (result != VK_SUCCESS) {
1434 throw runtime_error("failed to present swap chain image!");
1435 }
[47bff4c]1436
1437 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
[826df16]1438 }
1439
[de32fda]1440 void updateUniformBuffer(uint32_t currentImage) {
1441 static auto startTime = chrono::high_resolution_clock::now();
1442
1443 auto currentTime = chrono::high_resolution_clock::now();
1444 float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count();
1445
1446 UniformBufferObject ubo = {};
1447 ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
1448 ubo.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
1449 ubo.proj = glm::perspective(glm::radians(45.0f), swapChainExtent.width / (float) swapChainExtent.height, 0.1f, 10.0f);
1450 ubo.proj[1][1] *= -1;
1451
1452 void* data;
1453 vkMapMemory(device, uniformBuffersMemory[currentImage], 0, sizeof(ubo), 0, &data);
1454 memcpy(data, &ubo, sizeof(ubo));
1455 vkUnmapMemory(device, uniformBuffersMemory[currentImage]);
1456 }
1457
[826df16]1458 void cleanup() {
[75108ef]1459 cleanupSwapChain();
1460
[eea05dd]1461 vkDestroyImage(device, textureImage, nullptr);
[f5d5686]1462 vkFreeMemory(device, textureImageMemory, nullptr);
[eea05dd]1463
[de32fda]1464 vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
1465
[cae7a2c]1466 vkDestroyBuffer(device, indexBuffer, nullptr);
1467 vkFreeMemory(device, indexBufferMemory, nullptr);
1468
[80edd70]1469 vkDestroyBuffer(device, vertexBuffer, nullptr);
1470 vkFreeMemory(device, vertexBufferMemory, nullptr);
1471
[47bff4c]1472 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
1473 vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr);
1474 vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);
1475 vkDestroyFence(device, inFlightFences[i], nullptr);
1476 }
1477
1478 vkDestroyCommandPool(device, commandPool, nullptr);
1479
[909b51a]1480 vkDestroyDevice(device, nullptr);
1481
[80de39d]1482 if (enableValidationLayers) {
1483 DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
1484 }
1485
[b3671b5]1486 vkDestroySurfaceKHR(instance, surface, nullptr);
[826df16]1487 vkDestroyInstance(instance, nullptr);
1488
[0e6ecf3]1489 gui->DestroyWindow();
[98f3232]1490 gui->Shutdown();
1491 delete gui;
[826df16]1492 }
[e09ad38]1493
1494 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
1495 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
1496 VkDebugUtilsMessageTypeFlagsEXT messageType,
1497 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
1498 void* pUserData) {
1499 cerr << "validation layer: " << pCallbackData->pMessage << endl;
1500
1501 return VK_FALSE;
[0e6ecf3]1502 }
[e09ad38]1503
1504 static vector<char> readFile(const string& filename) {
1505 ifstream file(filename, ios::ate | ios::binary);
1506
1507 if (!file.is_open()) {
1508 throw runtime_error("failed to open file!");
1509 }
1510
1511 size_t fileSize = (size_t)file.tellg();
1512 vector<char> buffer(fileSize);
1513
1514 file.seekg(0);
1515 file.read(buffer.data(), fileSize);
1516
1517 file.close();
1518
1519 return buffer;
1520 }
[826df16]1521};
1522
[1c6cd5e]1523int main(int argc, char* argv[]) {
[826df16]1524
[b6127d2]1525#ifdef NDEBUG
1526 cout << "DEBUGGING IS OFF" << endl;
1527#else
1528 cout << "DEBUGGING IS ON" << endl;
1529#endif
[a8f0577]1530
[826df16]1531 cout << "Starting Vulkan game..." << endl;
1532
1533 VulkanGame game;
1534
1535 try {
1536 game.run();
1537 } catch (const exception& e) {
1538 cerr << e.what() << endl;
1539 return EXIT_FAILURE;
1540 }
[03f4c64]1541
[826df16]1542 cout << "Finished running the game" << endl;
[03f4c64]1543
[826df16]1544 return EXIT_SUCCESS;
[03f4c64]1545}
Note: See TracBrowser for help on using the repository browser.