From 214cc42c93e9a51f7dc66d99684d076c04a9d0a9 Mon Sep 17 00:00:00 2001 From: ktyl Date: Mon, 20 Mar 2023 21:06:56 +0000 Subject: [PATCH] create vulkan instance --- src/main.cpp | 126 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 101 insertions(+), 25 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 37e10ce..b9f24e1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,37 +1,113 @@ #define GLFW_INCLUDE_VULKAN #include -#define GLM_FORCE_RADIANS -#define GLM_FORCE_DEPth_ZERO_TO_ONE -#include -#include - #include +#include +#include +#include + +const uint32_t WIDTH = 800; +const uint32_t HEIGHT = 600; + +class HelloTriangleApplication +{ +public: + void run() + { + initWindow(); + initVulkan(); + mainLoop(); + cleanup(); + } + +private: + GLFWwindow* _window; + VkInstance _instance; + + void initWindow() + { + glfwInit(); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + + _window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); + } + + void initVulkan() + { + createInstance(); + } + + void mainLoop() + { + while (!glfwWindowShouldClose(_window)) + { + glfwPollEvents(); + } + } + + void cleanup() + { + vkDestroyInstance(_instance, nullptr); + + glfwDestroyWindow(_window); + glfwTerminate(); + } + + void createInstance() + { + VkApplicationInfo appInfo{}; + appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; + appInfo.pApplicationName = "Hello Triangle"; + appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0); + appInfo.pEngineName = "No Engine"; + appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0); + appInfo.apiVersion = VK_API_VERSION_1_0; + + VkInstanceCreateInfo createInfo{}; + createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + createInfo.pApplicationInfo = &appInfo; + + uint32_t glfwExtensionCount = 0; + const char** glfwExtensions; + + glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); + + createInfo.enabledExtensionCount = glfwExtensionCount; + createInfo.ppEnabledExtensionNames = glfwExtensions; + createInfo.enabledLayerCount = 0; + + if (vkCreateInstance(&createInfo, nullptr, &_instance) != VK_SUCCESS) + { + throw std::runtime_error("failed to create instance!"); + } + + uint32_t extensionCount = 0; + std::vector extensions(extensionCount); + vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data()); + + std::cout << "available extensions: " << extensionCount << std::endl; + + for (const auto& extension : extensions) + { + std::cout << '\t' << extension.extensionName << '\n'; + } + } +}; int main() { - glfwInit(); + HelloTriangleApplication app; - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr); - - uint32_t extensionCount = 0; - vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr); - - std::cout << extensionCount << " extensions supported\n"; - - glm::mat4 matrix; - glm::vec4 vec; - auto test = matrix * vec; - - while (!glfwWindowShouldClose(window)) + try { - glfwPollEvents(); + app.run(); + } + catch(const std::exception& e) + { + std::cerr << e.what() << std::endl; + return EXIT_FAILURE; } - glfwDestroyWindow(window); - - glfwTerminate(); - - return 0; + return EXIT_SUCCESS; }