#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_VULKAN #include #include #include #include #define VULKAN_HPP_NO_CONSTRUCTORS #include #include "fmt/base.h" #include "util/types.h" constexpr int WIDTH = 800; constexpr int HEIGHT = 600; constexpr std::array validationLayers = { "VK_LAYER_KHRONOS_validation" }; #ifdef NDEBUG constexpr bool enableValidationLayers = false; #else constexpr bool enableValidationLayers = true; #endif class HelloTriangleApplication { public: void run() { initWindow(); initVulkan(); mainLoop(); cleanup(); } private: GLFWwindow* mWindow; vk::UniqueInstance mInstance; vk::UniqueHandle mDebugMessenger; vk::DispatchLoaderDynamic mLoader; void initWindow() { glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); mWindow = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); } void initVulkan() { createInstance(); setupDebugMessenger(); } void setupDebugMessenger() { if (!enableValidationLayers) return; vk::DebugUtilsMessengerCreateInfoEXT messengerCreateInfo { .messageSeverity = vk::DebugUtilsMessageSeverityFlagBitsEXT::eVerbose | vk::DebugUtilsMessageSeverityFlagBitsEXT::eWarning | vk::DebugUtilsMessageSeverityFlagBitsEXT::eError, .messageType = vk::DebugUtilsMessageTypeFlagBitsEXT::eGeneral | vk::DebugUtilsMessageTypeFlagBitsEXT::eValidation | vk::DebugUtilsMessageTypeFlagBitsEXT::ePerformance, .pfnUserCallback = debugCallback, }; mDebugMessenger = mInstance->createDebugUtilsMessengerEXTUnique(messengerCreateInfo, nullptr, mLoader); } void mainLoop() { while (!glfwWindowShouldClose(mWindow)) { glfwPollEvents(); } } void cleanup() { // NOTE: instance destruction is handled by UniqueInstance glfwDestroyWindow(mWindow); glfwTerminate(); } fn createInstance() -> void { if (enableValidationLayers && !checkValidationLayerSupport()) throw std::runtime_error("validation layers requested, but not available!"); vk::ApplicationInfo appInfo { .pApplicationName = "Hello Triangle", .applicationVersion = 1, .pEngineName = "No Engine", .engineVersion = 1, .apiVersion = VK_API_VERSION_1_0 }; vk::InstanceCreateInfo createInfo { .pApplicationInfo = &appInfo }; // Retrieve extensions using custom function std::vector extensions = getRequiredExtensions(); if (enableValidationLayers) { createInfo.setEnabledLayerCount(static_cast(validationLayers.size())) .setPpEnabledLayerNames(validationLayers.data()); } else { createInfo.setEnabledLayerCount(0); } // Enable the portability extension and set flags extensions.push_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME); createInfo.setEnabledExtensionCount(static_cast(extensions.size())) .setPpEnabledExtensionNames(extensions.data()) .setFlags(vk::InstanceCreateFlagBits::eEnumeratePortabilityKHR); fmt::println("Available extensions:"); for (const char* extension : extensions) fmt::println("\t{}", extension); try { mInstance = vk::createInstanceUnique(createInfo); mLoader = vk::DispatchLoaderDynamic(mInstance.get(), vkGetInstanceProcAddr); } catch (const vk::SystemError& err) { throw std::runtime_error("Failed to create instance: " + std::string(err.what())); } } static fn checkValidationLayerSupport() -> bool { std::vector availableLayers = vk::enumerateInstanceLayerProperties(); for (const char* layerName : validationLayers) { bool layerFound = false; for (const auto& layerProperties : availableLayers) if (strcmp(layerName, layerProperties.layerName) == 0) { layerFound = true; break; } if (!layerFound) return false; } return true; } static fn getRequiredExtensions() -> std::vector { u32 glfwExtensionCount = 0; const char** glfwExtensions = nullptr; glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); std::vector extensions; if (glfwExtensions) { std::span extSpan(glfwExtensions, glfwExtensionCount); extensions.assign(extSpan.begin(), extSpan.end()); } if (enableValidationLayers) extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); return extensions; } static VKAPI_ATTR fn VKAPI_CALL debugCallback( VkDebugUtilsMessageSeverityFlagBitsEXT /*messageSeverity*/, VkDebugUtilsMessageTypeFlagsEXT /*messageType*/, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* /*pUserData*/ ) -> VkBool32 { fmt::println("Validation layer: {}", pCallbackData->pMessage); return VK_FALSE; } }; fn main() -> i32 { HelloTriangleApplication app; try { app.run(); } catch (const std::exception& e) { std::cerr << e.what() << std::endl; return EXIT_FAILURE; } return EXIT_SUCCESS; }