daily pastebin goal
80%
SHARE
TWEET

Vulkan Application

a guest Jan 24th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "pch.h"
  2. #define GLFW_INCLUDE_VULKAN
  3. #include <GLFW/glfw3.h>
  4.  
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <stdexcept>
  8. #include <functional>
  9. #include <cstdlib>
  10. #include <assert.h>
  11. #include <optional>
  12. #include <set>
  13.  
  14. struct QueueFamilyIndices
  15. {
  16.   std::optional<uint32_t> graphicsFamily;
  17.   std::optional<uint32_t> presentFamily;
  18.  
  19.   bool isComplete()
  20.   {
  21.     return graphicsFamily.has_value() && presentFamily.has_value();
  22.   }
  23. };
  24.  
  25. struct SwapChainSupportDetails
  26. {
  27.     VkSurfaceCapabilitiesKHR capabilities;
  28.     std::vector<VkSurfaceFormatKHR> formats;
  29.     std::vector<VkPresentModeKHR> presentModes;
  30. };
  31.  
  32. VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugUtilsMessengerEXT* pCallback)
  33. {
  34.     auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
  35.  
  36.     if(func != nullptr)
  37.     {
  38.         return func(instance, pCreateInfo, pAllocator, pCallback);
  39.     }
  40.     else
  41.     {
  42.         return VK_ERROR_EXTENSION_NOT_PRESENT;
  43.     }
  44. }
  45.  
  46. void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT callback, const VkAllocationCallbacks* pAllocator)
  47. {
  48.     auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
  49.  
  50.     if(func != nullptr)
  51.     {
  52.         func(instance, callback, pAllocator);
  53.     }
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60. class HelloTriangleApplication {
  61. public:
  62.     void run() {
  63.         initWindow();
  64.         initVulkan();
  65.         mainLoop();
  66.         cleanup();
  67.     }
  68.  
  69. private:
  70.     //FUNCTIONS
  71.  
  72.     //CALLBACK Function
  73.     static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
  74.       VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
  75.       VkDebugUtilsMessageTypeFlagsEXT messageType,
  76.       const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
  77.       void* pUserData)
  78.     {
  79.         std::cerr << "validation layers: " << pCallbackData->pMessage << std::endl;
  80.  
  81.         return VK_FALSE;
  82.     }
  83.  
  84.     //INITIALIZERS
  85.  
  86.     //Initialize glfw and create a window
  87.     void initWindow()
  88.     {
  89.         glfwInit();
  90.  
  91.         //Not creating a opengl context
  92.         glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
  93.  
  94.         //Resizability
  95.         glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
  96.  
  97.         window = glfwCreateWindow(WIDTH, HEIGHT, "ASH", nullptr, nullptr);
  98.     }
  99.  
  100.     //Initialise Vulkan
  101.     void initVulkan()
  102.     {
  103.         createInstance();
  104.         setupDebugCallBack();
  105.         createSurface();
  106.         pickPhysicalDevice();
  107.         createLogicalDevice();
  108.         createSwapChain();
  109.         createImageViews();
  110.     }
  111.  
  112.     //Create a Vulkan Instance
  113.     void createInstance()
  114.     {
  115.         if (enableValidationLayers && !checkValidationLayerSupport())
  116.         {
  117.           throw std::runtime_error("Validation layers requested, but not available");
  118.         }
  119.  
  120.  
  121.         //Optional but recommended. Provides the driver with additional info
  122.         VkApplicationInfo appInfo = {};
  123.         appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
  124.         appInfo.pApplicationName = "Ash";
  125.         appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
  126.         appInfo.pEngineName = "No Engine";
  127.         appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
  128.         appInfo.apiVersion = VK_API_VERSION_1_0;
  129.  
  130.         //Tells the driver what extensions and validation layers we want to use
  131.         VkInstanceCreateInfo createInfo = {};
  132.         createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
  133.         createInfo.pApplicationInfo = &appInfo;
  134.  
  135.         //extensions
  136.         auto extensions = getRequiredExtensions();
  137.         createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
  138.         createInfo.ppEnabledExtensionNames = extensions.data();
  139.  
  140.         //validation layers
  141.         if (enableValidationLayers)
  142.         {
  143.             createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
  144.             createInfo.ppEnabledLayerNames = validationLayers.data();
  145.         }
  146.         else
  147.         {
  148.             createInfo.enabledLayerCount = 0;
  149.         }
  150.  
  151.         //Create the instance
  152.         if(vkCreateInstance(&createInfo, nullptr, &instance) != VK_SUCCESS)
  153.         {
  154.             throw std::runtime_error("Failed to create instance!");
  155.         }
  156.     }
  157.  
  158.     void createLogicalDevice()
  159.     {
  160.         QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
  161.  
  162.         std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
  163.         std::set<uint32_t> uniqueQueueFamilies = {indices.graphicsFamily.value()};
  164.  
  165.         float queuePriority = 1.0f;
  166.         for(uint32_t queueFamily : uniqueQueueFamilies)
  167.         {
  168.             VkDeviceQueueCreateInfo queueCreateInfo = {};      
  169.             queueCreateInfo.pQueuePriorities = &queuePriority;
  170.             queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
  171.             queueCreateInfo.queueFamilyIndex = indices.graphicsFamily.value();
  172.             queueCreateInfo.queueCount = 1;
  173.             queueCreateInfos.push_back(queueCreateInfo);
  174.         }
  175.  
  176.         VkPhysicalDeviceFeatures deviceFeatures = {};
  177.  
  178.         VkDeviceCreateInfo createInfo = {};
  179.         createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
  180.         createInfo.pQueueCreateInfos = queueCreateInfos.data();
  181.         createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
  182.  
  183.         createInfo.pEnabledFeatures = &deviceFeatures;
  184.  
  185.         createInfo.enabledExtensionCount = static_cast<uint32_t>(deviceExtensions.size());
  186.         createInfo.ppEnabledExtensionNames = deviceExtensions.data();
  187.  
  188.         if (enableValidationLayers)
  189.         {
  190.             createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
  191.             createInfo.ppEnabledLayerNames = validationLayers.data();
  192.         }
  193.         else
  194.         {
  195.             createInfo.enabledLayerCount = 0;
  196.         }
  197.  
  198.         if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS)
  199.         {
  200.             throw std::runtime_error("Failed to create logical device!");
  201.         }
  202.  
  203.         vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);
  204.         vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue);
  205.  
  206.     }
  207.  
  208.     void createSurface()
  209.     {
  210.         if (glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS)
  211.         {
  212.             throw std::runtime_error("Failed to create window surface!");
  213.         }
  214.     }
  215.  
  216.     //Validation helper function
  217.     bool checkValidationLayerSupport()
  218.     {
  219.         uint32_t layerCount;
  220.         vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
  221.  
  222.         std::vector<VkLayerProperties> availableLayers(layerCount);
  223.         vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());
  224.  
  225.         for (auto layerName : validationLayers)
  226.         {
  227.             bool layerFound = false;
  228.  
  229.             for (const auto& layerProperties : availableLayers)
  230.             {
  231.                 if(strcmp(layerName, layerProperties.layerName) == 0)
  232.                 {
  233.                     layerFound = true;
  234.                     break;
  235.                 }
  236.             }
  237.  
  238.             if (!layerFound)
  239.             {
  240.                 return false;
  241.             }
  242.         }
  243.  
  244.         return true;
  245.     }
  246.  
  247.     //Extensions helper function
  248.     std::vector<const char*> getRequiredExtensions()
  249.     {
  250.         uint32_t glfwExtensionCount = 0;
  251.         const char** glfwExtensions;
  252.  
  253.         glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
  254.  
  255.         std::vector<const char*> extensions(glfwExtensions, glfwExtensions + glfwExtensionCount);
  256.  
  257.         if (enableValidationLayers)
  258.         {
  259.             extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
  260.         }
  261.  
  262.         return extensions;
  263.     }
  264.  
  265.     QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device)
  266.     {
  267.       QueueFamilyIndices indices;
  268.  
  269.       uint32_t QFcount = 0;
  270.       vkGetPhysicalDeviceQueueFamilyProperties(device, &QFcount, nullptr);
  271.  
  272.       std::vector<VkQueueFamilyProperties> QFs(QFcount);
  273.       vkGetPhysicalDeviceQueueFamilyProperties(device, &QFcount, QFs.data());
  274.  
  275.       int i = 0;
  276.       for (const auto& QF : QFs)
  277.       {
  278.         if (QF.queueCount > 0 && QF.queueFlags & VK_QUEUE_GRAPHICS_BIT)
  279.         {
  280.           indices.graphicsFamily = i;
  281.         }
  282.  
  283.         VkBool32 presentSupport = false;
  284.  
  285.         vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
  286.  
  287.         if (QF.queueCount > 0 && presentSupport)
  288.         {
  289.           indices.presentFamily = i;
  290.         }
  291.  
  292.         if (indices.isComplete())
  293.         {
  294.           break;
  295.         }
  296.  
  297.         i++;
  298.       }
  299.  
  300.       return indices;
  301.     }
  302.  
  303.     bool isDeviceSuitable(VkPhysicalDevice device)
  304.     {
  305.       QueueFamilyIndices indices = findQueueFamilies(device);
  306.  
  307.       bool extensionsSupported = checkDeviceExtensionsSupport(device);
  308.  
  309.       bool swapChainAdequate = false;
  310.       if(extensionsSupported)
  311.       {
  312.           SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device);
  313.           swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty();
  314.       }
  315.  
  316.       return indices.isComplete() && extensionsSupported && swapChainAdequate;
  317.     }
  318.  
  319.     bool checkDeviceExtensionsSupport(VkPhysicalDevice device)
  320.     {
  321.         uint32_t extensionCount;
  322.         vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr);
  323.  
  324.         std::vector<VkExtensionProperties> availableExtensions(extensionCount);
  325.         vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, availableExtensions.data());
  326.  
  327.         std::set<std::string> requiredExtensions(deviceExtensions.begin(), deviceExtensions.end());
  328.  
  329.         for (const auto& extension : availableExtensions)
  330.         {
  331.             requiredExtensions.erase(extension.extensionName);
  332.         }
  333.  
  334.         return requiredExtensions.empty();
  335.     }
  336.  
  337.     SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device)
  338.     {
  339.         SwapChainSupportDetails details;
  340.  
  341.         vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);
  342.  
  343.         uint32_t formatCount;
  344.         vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);
  345.  
  346.         if(formatCount != 0)
  347.         {
  348.             details.formats.resize(formatCount);
  349.             vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());
  350.         }
  351.  
  352.         uint32_t presentModeCount;
  353.         vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);
  354.  
  355.         if(presentModeCount != 0)
  356.         {
  357.             details.presentModes.resize(presentModeCount);
  358.             vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data());
  359.         }
  360.  
  361.         return details;
  362.     }
  363.  
  364.     VkSurfaceFormatKHR chooseSwapSurfaceFormats(const std::vector<VkSurfaceFormatKHR>& availableFormats)
  365.     {
  366.         if(availableFormats.size() == 1 && availableFormats[0].format == VK_FORMAT_UNDEFINED)
  367.         {
  368.             return{VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR};
  369.         }
  370.  
  371.         for(const auto& availableFormat : availableFormats)
  372.         {
  373.             if(availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
  374.             {
  375.                 return availableFormat;
  376.             }
  377.         }
  378.  
  379.         //can be expanded by ranking available options
  380.         return availableFormats[0];
  381.     }
  382.  
  383.     VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR> availablePresentModes)
  384.     {
  385.         VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR; // Double Buffering V-Sync
  386.  
  387.         for(const auto& availablePresentMode : availablePresentModes)
  388.         {
  389.             if(availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) // Triple Buffering V-Sync
  390.             {
  391.                 return availablePresentMode;
  392.             }
  393.             else if(availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR)
  394.             {
  395.                 bestMode = availablePresentMode;
  396.             }
  397.         }
  398.  
  399.         return bestMode;
  400.     }
  401.  
  402.     VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities)
  403.     {
  404.         if(capabilities.currentExtent.width != std::numeric_limits<uint32_t>::max())
  405.         {
  406.             return capabilities.currentExtent;
  407.         }
  408.         else
  409.         {
  410.             VkExtent2D actualExtent = {WIDTH, HEIGHT};
  411.  
  412.             actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width));
  413.             actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height));
  414.  
  415.             return actualExtent;
  416.         }
  417.     }
  418.  
  419.     void createSwapChain()
  420.     {
  421.         SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice);
  422.  
  423.         VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormats(swapChainSupport.formats);
  424.         VkPresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
  425.         VkExtent2D extent = chooseSwapExtent(swapChainSupport.capabilities);
  426.  
  427.         uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
  428.         if(swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount)
  429.         {
  430.             imageCount = swapChainSupport.capabilities.maxImageCount;
  431.         }
  432.  
  433.         VkSwapchainCreateInfoKHR createInfo = {};
  434.         createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
  435.         createInfo.surface = surface;
  436.  
  437.         createInfo.minImageCount = imageCount;
  438.         createInfo.imageFormat = surfaceFormat.format;
  439.         createInfo.imageColorSpace = surfaceFormat.colorSpace;
  440.         createInfo.imageExtent = extent;
  441.         createInfo.imageArrayLayers = 1; // 1 for normal screens, more for stereoscopic 3D
  442.         createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; // What operations we want to do with the image
  443.  
  444.         QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
  445.         uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()};
  446.  
  447.         if(indices.graphicsFamily != indices.presentFamily)
  448.         {
  449.             createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT; //Image can be owned by multiple queues
  450.             createInfo.queueFamilyIndexCount = 2;
  451.             createInfo.pQueueFamilyIndices = queueFamilyIndices;
  452.         }
  453.         else
  454.         {
  455.             createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; //Image is owned by one queue
  456.             createInfo.queueFamilyIndexCount = 0;
  457.             createInfo.pQueueFamilyIndices = nullptr;
  458.         }
  459.  
  460.         createInfo.preTransform = swapChainSupport.capabilities.currentTransform; // Base transform, change for flips or rotations for entire application
  461.         createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; // How to blend the alpha value with other windows applications.
  462.  
  463.         createInfo.presentMode = presentMode;
  464.         createInfo.clipped = VK_TRUE;
  465.  
  466.         createInfo.oldSwapchain = VK_NULL_HANDLE; // Only one swapchain will be created in this application
  467.  
  468.         if(vkCreateSwapchainKHR(device, &createInfo, nullptr, &swapchain) != VK_SUCCESS)
  469.         {
  470.             throw std::runtime_error("Failed to create swapchain");
  471.         }
  472.     }
  473.  
  474.     void createImageViews()
  475.     {
  476.         swapChainImageViews.resize(swapChainImages.size());
  477.  
  478.         for(size_t i = 0; i < swapChainImages.size(); i++)
  479.         {
  480.             VkImageViewCreateInfo createInfo = { };
  481.             createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  482.             createInfo.image = swapChainImages[i];
  483.  
  484.             createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
  485.             createInfo.format = swapChainImageFormat;
  486.  
  487.             createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
  488.             createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
  489.             createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
  490.             createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
  491.  
  492.             createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  493.             createInfo.subresourceRange.baseMipLevel = 0;
  494.             createInfo.subresourceRange.levelCount = 1;
  495.             createInfo.subresourceRange.baseArrayLayer = 0;
  496.             createInfo.subresourceRange.layerCount = 1;
  497.  
  498.             if(vkCreateImageView(device, &createInfo, nullptr, &swapChainImageViews[i]) != VK_SUCCESS)
  499.             {
  500.                 throw std::runtime_error("Failed to create image view");
  501.             }
  502.         }
  503.     }
  504.  
  505.     //Set up debug callbacks
  506.     void setupDebugCallBack()
  507.     {
  508.         if(!enableValidationLayers) return;
  509.  
  510.         VkDebugUtilsMessengerCreateInfoEXT createInfo = {};
  511.         createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
  512.         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;
  513.         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;
  514.         createInfo.pfnUserCallback = &debugCallback;
  515.         createInfo.pUserData = nullptr; //optional
  516.  
  517.         if (CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &callback) != VK_SUCCESS)
  518.         {
  519.             throw std::runtime_error("failed to set up debug callback!");
  520.         }
  521.     }
  522.  
  523.     //Picks Graphics Card
  524.     void pickPhysicalDevice()
  525.     {
  526.         uint32_t deviceCount = 0;
  527.         vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
  528.  
  529.         if (deviceCount == 0)
  530.         {
  531.             throw std::runtime_error("failed to find GPUs with Vulkan support!");
  532.         }
  533.  
  534.         std::vector<VkPhysicalDevice> devices(deviceCount);
  535.         vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
  536.  
  537.         for(const auto& dev : devices)
  538.         {
  539.             if(isDeviceSuitable(dev))
  540.             {
  541.                 physicalDevice = dev;
  542.                 break;
  543.             }
  544.         }
  545.  
  546.         if (physicalDevice == VK_NULL_HANDLE)
  547.         {
  548.             throw std::runtime_error("failed to find suitable GPU!");
  549.         }
  550.     }
  551.  
  552.     //UPDATE
  553.  
  554.     void mainLoop()
  555.     {
  556.         while (!glfwWindowShouldClose(window))
  557.         {
  558.             glfwPollEvents();
  559.         }
  560.     }
  561.  
  562.     //DELETION/CLEANUP
  563.  
  564.     void cleanup()
  565.     {
  566.         for (auto imageView : swapChainImageViews)
  567.         {
  568.             vkDestroyImageView(device, imageView, nullptr);
  569.         }
  570.  
  571.         vkDestroySwapchainKHR(device, swapchain, nullptr);
  572.         vkDestroyDevice(device, nullptr);
  573.  
  574.         if(enableValidationLayers)
  575.         {
  576.             DestroyDebugUtilsMessengerEXT(instance, callback, nullptr);
  577.         }
  578.  
  579.         vkDestroySurfaceKHR(instance, surface, nullptr);
  580.         vkDestroyInstance(instance, nullptr);
  581.  
  582.         glfwDestroyWindow(window);
  583.  
  584.         glfwTerminate();
  585.     }
  586.  
  587.     //VARIABLES
  588.     GLFWwindow* window = nullptr;
  589.  
  590.     const int WIDTH = 2000;
  591.     const int HEIGHT = 1500;
  592.  
  593.     VkInstance instance;
  594.     VkDebugUtilsMessengerEXT callback;
  595.     VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
  596.     VkDevice device;
  597.     VkQueue graphicsQueue;
  598.     VkQueue presentQueue;
  599.     VkSurfaceKHR surface;
  600.     VkSwapchainKHR swapchain;
  601.     std::vector<VkImageView> swapChainImageViews;
  602.     std::vector<VkImage> swapChainImages;
  603.     VkFormat swapChainImageFormat;
  604.  
  605.     const std::vector<const char*> validationLayers = { "VK_LAYER_LUNARG_standard_validation" };
  606.     const std::vector<const char*> deviceExtensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME };
  607.  
  608.     #ifdef NDEBUG
  609.       const bool enableValidationLayers = false;
  610.     #else
  611.       const bool enableValidationLayers = true;
  612.     #endif
  613.  
  614. };
  615.  
  616. int main() {
  617.     HelloTriangleApplication app;
  618.  
  619.     try
  620.     {
  621.         app.run();
  622.     }
  623.     catch (const std::exception& e)
  624.     {
  625.         std::cerr << e.what() << std::endl;
  626.         return EXIT_FAILURE;
  627.     }
  628.    
  629.  
  630.     return EXIT_SUCCESS;
  631. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top