diff --git a/.gitignore b/.gitignore index 259148f..139e668 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +main # Prerequisites *.d diff --git a/README.md b/README.md index 02726dc..edfad05 100644 --- a/README.md +++ b/README.md @@ -6,5 +6,5 @@ Install VulkanSDK: https://vulkan.lunarg.com/ brew install glfw glm export VULKAN_SDK=$HOME/path/to/your/VulkanSDK/1.3.236.0 -make +make run ``` diff --git a/main b/main deleted file mode 100755 index 142a19b..0000000 Binary files a/main and /dev/null differ diff --git a/main.cpp b/main.cpp index e7335bc..bcc5a70 100644 --- a/main.cpp +++ b/main.cpp @@ -1,35 +1,127 @@ +#include #define GLFW_INCLUDE_VULKAN #include -#define GLM_FORCE_RADIANS -#define GLM_FORCE_DEPTH_ZERO_TO_ONE -#include -#include - +#include #include +#include +#include -int main() { +class HelloTriangleApplication { +public: + void run() { + initWindow(); + initVulkan(); + mainLoop(); + cleanup(); + } + +private: + GLFWwindow* window; + + VkInstance instance; + + const uint32_t WIDTH = 800; + const uint32_t HEIGHT = 600; + + void initWindow() { glfwInit(); - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr); + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); - uint32_t extensionCount = 0; - vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr); + window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); + } - std::cout << extensionCount << " extensions supported\n"; + 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; - glm::mat4 matrix; - glm::vec4 vec; - auto test = matrix * vec; + VkInstanceCreateInfo createInfo{}; + createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + createInfo.pApplicationInfo = &appInfo; - while(!glfwWindowShouldClose(window)) { - glfwPollEvents(); + std::vector requiredExtensions; + + uint32_t glfwExtensionCount = 0; + const char** glfwExtensions; + + // Enable glfw extensions + glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); + + for (uint32_t i = 0; i < glfwExtensionCount; i++) { + requiredExtensions.emplace_back(glfwExtensions[i]); } + // Enable Portability Enumeration extension (required for macOS) + requiredExtensions.emplace_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME); + + createInfo.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; + + createInfo.enabledExtensionCount = (uint32_t) requiredExtensions.size(); + createInfo.ppEnabledExtensionNames = requiredExtensions.data(); + createInfo.enabledLayerCount = 0; + + VkResult result = vkCreateInstance(&createInfo, nullptr, &instance); + if (result != VK_SUCCESS) { + std::cout << "error creating instance: " << result << std::endl; + throw std::runtime_error("failed to create instance!"); + } + + std::cout << "Created instance" << std::endl; + + // Check for available extensions + uint32_t extensionCount = 0; + vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr); + std::vector extensions(extensionCount); + + vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data()); + + std::cout << "available extensions:" << std::endl; + for (const auto& extension : extensions) { + std::cout << '\t' << extension.extensionName; + for (const char* required : requiredExtensions) { + if (strcmp(required, extension.extensionName) == 0) { + std::cout << " *"; + } + } + + std::cout << std::endl; + } + } + + void initVulkan() { + createInstance(); + } + + void mainLoop() { + while (!glfwWindowShouldClose(window)) { + glfwPollEvents(); + } + } + + void cleanup() { + vkDestroyInstance(instance, nullptr); + glfwDestroyWindow(window); - glfwTerminate(); + } +}; - return 0; + +int main() { + HelloTriangleApplication app; + + try { + app.run(); + } catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; }