#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_VULKAN #include #include #include #include #include #include #include #include #include "src/util/types.h" const uint32_t WIDTH = 800; const uint32_t HEIGHT = 600; const std::array validationLayers = { "VK_LAYER_KHRONOS_validation" }; #ifdef NDEBUG const bool enableValidationLayers = false; #else const bool enableValidationLayers = true; #endif fn CreateDebugUtilsMessengerEXT( VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugUtilsMessengerEXT* pDebugMessenger ) -> VkResult { auto func = std::bit_cast( vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT") ); if (func != nullptr) return func(instance, pCreateInfo, pAllocator, pDebugMessenger); return VK_ERROR_EXTENSION_NOT_PRESENT; } fn DestroyDebugUtilsMessengerEXT( VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, const VkAllocationCallbacks* pAllocator ) -> void { auto func = std::bit_cast( vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT") ); if (func != nullptr) func(instance, debugMessenger, pAllocator); } struct QueueFamilyIndices { std::optional graphics_family; std::optional present_family; fn isComplete() -> bool { return graphics_family.has_value() && present_family.has_value(); } }; class Application { public: fn run() -> void { initWindow(); initVulkan(); mainLoop(); cleanup(); } private: GLFWwindow* mWindow; VkInstance mInstance; VkDebugUtilsMessengerEXT mDebugMessenger; VkSurfaceKHR mSurface; VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE; VkDevice mDevice; VkQueue mGraphicsQueue; VkQueue mPresentQueue; fn initWindow() -> void { glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); mWindow = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr); } fn initVulkan() -> void { createInstance(); setupDebugMessenger(); createSurface(); pickPhysicalDevice(); createLogicalDevice(); } fn mainLoop() -> void { while (!glfwWindowShouldClose(mWindow)) { glfwPollEvents(); } } fn cleanup() -> void { vkDestroyDevice(mDevice, nullptr); if (enableValidationLayers) DestroyDebugUtilsMessengerEXT(mInstance, mDebugMessenger, nullptr); vkDestroySurfaceKHR(mInstance, mSurface, nullptr); vkDestroyInstance(mInstance, nullptr); glfwDestroyWindow(mWindow); glfwTerminate(); } fn createInstance() -> void { if (enableValidationLayers && !checkValidationLayerSupport()) throw std::runtime_error("validation layers requested, but not available!"); 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; std::vector extensions = getRequiredExtensions(); VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo {}; if (enableValidationLayers) { createInfo.enabledLayerCount = static_cast(validationLayers.size()); createInfo.ppEnabledLayerNames = validationLayers.data(); populateDebugMessengerCreateInfo(debugCreateInfo); createInfo.pNext = static_cast(&debugCreateInfo); } else { createInfo.enabledLayerCount = 0; createInfo.pNext = nullptr; } extensions.emplace_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME); createInfo.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR; createInfo.enabledExtensionCount = static_cast(extensions.size()); createInfo.ppEnabledExtensionNames = extensions.data(); for (const char* extension : extensions) std::cout << extension << std::endl; if (vkCreateInstance(&createInfo, nullptr, &mInstance) != VK_SUCCESS) throw std::runtime_error("failed to create instance!"); } static fn populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) -> void { createInfo = {}; createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; 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; 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; createInfo.pfnUserCallback = debugCallback; } fn setupDebugMessenger() -> void { if (!enableValidationLayers) return; VkDebugUtilsMessengerCreateInfoEXT createInfo; populateDebugMessengerCreateInfo(createInfo); if (CreateDebugUtilsMessengerEXT(mInstance, &createInfo, nullptr, &mDebugMessenger) != VK_SUCCESS) throw std::runtime_error("failed to set up debug messenger!"); } fn createSurface() -> void { if (glfwCreateWindowSurface(mInstance, mWindow, nullptr, &mSurface) != VK_SUCCESS) throw std::runtime_error("failed to create window surface!"); } fn pickPhysicalDevice() -> void { uint32_t deviceCount = 0; vkEnumeratePhysicalDevices(mInstance, &deviceCount, nullptr); if (deviceCount == 0) throw std::runtime_error("failed to find GPUs with Vulkan support!"); std::vector devices(deviceCount); vkEnumeratePhysicalDevices(mInstance, &deviceCount, devices.data()); for (const auto& device : devices) if (isDeviceSuitable(device)) { mPhysicalDevice = device; break; } if (mPhysicalDevice == VK_NULL_HANDLE) throw std::runtime_error("failed to find a suitable GPU!"); } fn createLogicalDevice() -> void { QueueFamilyIndices indices = findQueueFamilies(mPhysicalDevice); std::vector queueCreateInfos; std::set uniqueQueueFamilies = { indices.graphics_family.value(), indices.present_family.value() }; float queuePriority = 1.0F; for (uint32_t queueFamily : uniqueQueueFamilies) { VkDeviceQueueCreateInfo queueCreateInfo {}; queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; queueCreateInfo.queueFamilyIndex = queueFamily; queueCreateInfo.queueCount = 1; queueCreateInfo.pQueuePriorities = &queuePriority; queueCreateInfos.push_back(queueCreateInfo); } VkPhysicalDeviceFeatures deviceFeatures {}; VkDeviceCreateInfo createInfo {}; createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; createInfo.queueCreateInfoCount = static_cast(queueCreateInfos.size()); createInfo.pQueueCreateInfos = queueCreateInfos.data(); createInfo.pEnabledFeatures = &deviceFeatures; createInfo.enabledExtensionCount = 0; if (enableValidationLayers) { createInfo.enabledLayerCount = static_cast(validationLayers.size()); createInfo.ppEnabledLayerNames = validationLayers.data(); } else { createInfo.enabledLayerCount = 0; } if (vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice) != VK_SUCCESS) { throw std::runtime_error("failed to create logical device!"); } vkGetDeviceQueue(mDevice, indices.graphics_family.value(), 0, &mGraphicsQueue); vkGetDeviceQueue(mDevice, indices.present_family.value(), 0, &mPresentQueue); } fn isDeviceSuitable(VkPhysicalDevice device) -> bool { QueueFamilyIndices indices = findQueueFamilies(device); return indices.isComplete(); } fn findQueueFamilies(VkPhysicalDevice device) -> QueueFamilyIndices { QueueFamilyIndices indices; uint32_t queueFamilyCount = 0; vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr); std::vector queueFamilies(queueFamilyCount); vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data()); u32 idx = 0; for (const auto& queueFamily : queueFamilies) { if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) indices.graphics_family = idx; VkBool32 presentSupport = false; vkGetPhysicalDeviceSurfaceSupportKHR(device, idx, mSurface, &presentSupport); if (presentSupport) indices.present_family = idx; if (indices.isComplete()) break; idx++; } return indices; } static fn getRequiredExtensions() -> std::vector { uint32_t 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 fn checkValidationLayerSupport() -> bool { uint32_t layerCount = 0; vkEnumerateInstanceLayerProperties(&layerCount, nullptr); std::vector availableLayers(layerCount); vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data()); for (const char* layerName : validationLayers) { bool layerFound = false; for (const auto& layerProperties : availableLayers) { if (strcmp( static_cast(layerName), static_cast(layerProperties.layerName) ) == 0) { layerFound = true; break; } } if (!layerFound) return false; } return true; } static VKAPI_ATTR fn VKAPI_CALL debugCallback( VkDebugUtilsMessageSeverityFlagBitsEXT /*messageSeverity*/, VkDebugUtilsMessageTypeFlagsEXT /*messageType*/, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* /*pUserData*/ ) -> VkBool32 { std::cerr << "validation layer: " << pCallbackData->pMessage << std::endl; return VK_FALSE; } }; auto main() -> int { Application app; try { app.run(); } catch (const std::exception& e) { std::cerr << e.what() << std::endl; return EXIT_FAILURE; } return EXIT_SUCCESS; }