Guest User

Vulkan-Instance

a guest
Jan 1st, 2024
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.68 KB | Source Code | 0 0
  1. #define GLFW_INCLUDE_VULKAN
  2. #include <GLFW/glfw3.h>
  3. #include <vulkan/vulkan.hpp>
  4.  
  5. #include <iostream>
  6. #include <stdexcept>
  7. #include <cstdlib>
  8. #include <chrono>
  9.  
  10. const uint32_t WIDTH = 800;
  11. const uint32_t HEIGHT = 600;
  12.  
  13. // Validation Layers are used for debugging
  14. const std::vector<const char*> validationLayers =
  15. {
  16.     "VK_LAYER_KHRONOS_validation"
  17. };
  18.  
  19. #ifdef NDEBUG //Not Debug
  20.     const bool enableValidationLayers = false;
  21. #else
  22.     const bool enableValidationLayers = true;
  23. #endif
  24.  
  25. class HelloTriangleApplication
  26. {
  27. public:
  28.     void run()
  29.     {
  30.         initWindow(); //GLFW window init
  31.         initVulkan();
  32.         mainLoop();
  33.         cleanup();
  34.     }
  35.  
  36. private:
  37.  
  38.     GLFWwindow* window;
  39.     vk::UniqueInstance instance; //can be Instance or UniqueInstance for smart pointers instead of RAII
  40.  
  41.     void initWindow() //GLFW window init
  42.     {
  43.         glfwInit(); //Init glfw library
  44.  
  45.         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  46.         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  47.  
  48.         glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); //Tell glfw to not use opengl context. Enabled by default.
  49.         glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); //Disable window resizing. Needs explicit control.
  50.  
  51.         window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); //Window context
  52.         //std::this_thread::sleep_for (std::chrono::seconds(1));
  53.         //glfwSetWindowPos(window, 100, 150); // Set window Position
  54.     }
  55.  
  56.     void initVulkan()
  57.     {
  58.         createInstance();
  59.     }
  60.  
  61.     void createInstance()
  62.     {
  63.         checkValidationLayerSupport();
  64.         // Optional for InstanceCreateInfo
  65.         vk::ApplicationInfo appInfo
  66.         (
  67.  
  68.             "Hello Triangle",           // Application Name
  69.             VK_MAKE_VERSION(1, 0, 0),   // Application Version
  70.             "No Engine",                // Engine Name
  71.             VK_MAKE_VERSION(1, 0, 0),   // Engine Version
  72.             VK_API_VERSION_1_0          // API Version
  73.         );
  74.  
  75.         uint32_t glfwExtensionCount = 0;
  76.         const char** glfwExtensions;
  77.  
  78.         glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
  79.  
  80.         // Required for OS interaction
  81.         vk::InstanceCreateInfo createInfo
  82.         (
  83.             vk::InstanceCreateFlags(),         // Can be empty with {}, null or vk::InstanceCreateFlags()
  84.             &appInfo,                          // AppInfo object we created
  85.             0, nullptr,                        // enabled layers. Size and Data
  86.             glfwExtensionCount, glfwExtensions // enabled extensions. Size and Data
  87.         );
  88.  
  89.         try
  90.         {
  91.             instance = vk::createInstanceUnique(createInfo, nullptr); //can be createInstance or createInstanceUnique for smart pointers instead of normal RAII
  92.         }
  93.         catch (vk::SystemError err)
  94.         {
  95.             throw std::runtime_error("failed to create instance!");
  96.         }
  97.  
  98.         std::cout << "Available Extensions:\n";
  99.  
  100.         for (const auto& extension : vk::enumerateInstanceExtensionProperties())
  101.         {
  102.             std::cout << "\t" << extension.extensionName << std::endl;
  103.         }
  104.     }
  105.  
  106.     bool checkValidationLayerSupport()
  107.     {
  108.         std::vector<vk::LayerProperties> availableLayers = vk::enumerateInstanceLayerProperties();
  109.  
  110.         for(const char* layerName : validationLayers)
  111.         {
  112.             std::cout << "Selected Layer Names:\n" << "\t" << layerName << "\n";
  113.             std::cout << "Available Layer Properties:\n";
  114.  
  115.             for (const auto& layerProperties : availableLayers)
  116.             {
  117.                 std::cout << "\t" << layerProperties.layerName << "\n";
  118.             }
  119.         }
  120.  
  121.  
  122.         return false;
  123.     }
  124.  
  125.     void mainLoop()
  126.     {
  127.         uint32_t x{0}, y{0};
  128.         while (!glfwWindowShouldClose(window))
  129.         {
  130.             glfwPollEvents();
  131.  
  132.             // Delete when needed
  133.             #if (0)
  134.             for(uint32_t i{0}; i < 1000 ; i++)
  135.             {
  136.                 glfwSetWindowPos(window, x++, y++);
  137.                 glfwPollEvents();
  138.             }
  139.             for(uint32_t i{1000}; i > 0 ; i--)
  140.             {
  141.                 glfwSetWindowPos(window, x--, y--);
  142.                 glfwPollEvents();
  143.             }
  144.             #endif
  145.         }
  146.     }
  147.  
  148.     void cleanup()
  149.     {
  150.         //vkDestroyInstance(instance, nullptr);
  151.  
  152.         glfwDestroyWindow(window);
  153.  
  154.         glfwTerminate();
  155.     }
  156. };
  157.  
  158. int main() {
  159.     HelloTriangleApplication app;
  160.  
  161.     try
  162.     {
  163.         app.run();
  164.     }
  165.     catch (const std::exception& e)
  166.     {
  167.         std::cerr << e.what() << std::endl;
  168.         return EXIT_FAILURE;
  169.     }
  170.  
  171.     return EXIT_SUCCESS;
  172. }
Tags: C++ vulkan
Advertisement
Add Comment
Please, Sign In to add comment