#include #include #include #include #include #include #define GLFW_INCLUDE_VULKAN #include #ifdef NDEBUG const bool enableValidationLayers = false; #else const bool enableValidationLayers = true; #endif static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData, void *pUserData) { // TODO: Put all messages into a log file if (messageSeverity >= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) { fprintf(stderr, "validation layer: %s\n", pCallbackData->pMessage); } return VK_FALSE; } const char *validationLayers[] = {"VK_LAYER_KHRONOS_validation"}; size_t validationLayerCount = sizeof(validationLayers) / sizeof(validationLayers[0]); typedef struct Application { GLFWwindow *window; VkInstance instance; VkDebugUtilsMessengerEXT debugMessenger; VkPhysicalDevice physicalDevice; VkDevice device; VkQueue graphicsQueue; } Application; bool checkValidationLayerSupport() { uint32_t layerCount; vkEnumerateInstanceLayerProperties(&layerCount, NULL); VkLayerProperties availableLayers[layerCount]; vkEnumerateInstanceLayerProperties(&layerCount, availableLayers); for (int i = 0; i < validationLayerCount; i++) { bool layerFound = false; for (int j = 0; j < layerCount; j++) { if (strcmp(availableLayers[j].layerName, validationLayers[i]) == 0) { layerFound = true; break; } } if (!layerFound) { return false; } } return true; } bool verifyExtensionSupport(uint32_t extensionCount, VkExtensionProperties *extentions, uint32_t glfwExtensionCount, const char **glfwExtensions) { for (uint32_t i = 0; i < glfwExtensionCount; i++) { bool layerFound = false; for (uint32_t j = 0; j < extensionCount; j++) { if (strcmp(glfwExtensions[i], extentions[j].extensionName) == 0) { layerFound = true; break; } } if (!layerFound) { fprintf(stderr, "Missing %s vulkan extention\n", glfwExtensions[i]); return false; } } return true; } void populateDebugMessengerCreateInfo( VkDebugUtilsMessengerCreateInfoEXT *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; } void createInstance(Application *app) { if (enableValidationLayers && !checkValidationLayerSupport()) { fprintf(stderr, "Validation layers requested, but not available!\n"); exit(EXIT_FAILURE); } VkApplicationInfo appInfo = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .pApplicationName = "Hello Triangle", .applicationVersion = VK_MAKE_VERSION(1, 0, 0), .pEngineName = "No Engine", .engineVersion = VK_MAKE_VERSION(1, 0, 0), .apiVersion = VK_API_VERSION_1_0, }; uint32_t glfwExtensionCount = 0; const char **glfwExtensions; glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); const char *glfwExtensionsDebug[glfwExtensionCount + 1]; VkInstanceCreateInfo createInfo = { .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, .pApplicationInfo = &appInfo, .enabledExtensionCount = glfwExtensionCount, .ppEnabledExtensionNames = glfwExtensions, .enabledLayerCount = 0, }; uint32_t extensionCount = 0; vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, NULL); VkExtensionProperties extensions[extensionCount]; vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, extensions); VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo = {0}; if (enableValidationLayers) { populateDebugMessengerCreateInfo(&debugCreateInfo); createInfo.pNext = (VkDebugUtilsMessengerCreateInfoEXT *)&debugCreateInfo; createInfo.enabledLayerCount = validationLayerCount; createInfo.ppEnabledLayerNames = validationLayers; for (size_t i = 0; i < glfwExtensionCount; i++) { glfwExtensionsDebug[i] = glfwExtensions[i]; } glfwExtensionsDebug[glfwExtensionCount] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME; createInfo.enabledExtensionCount = glfwExtensionCount + 1; createInfo.ppEnabledExtensionNames = glfwExtensionsDebug; if (!verifyExtensionSupport(extensionCount, extensions, glfwExtensionCount + 1, glfwExtensionsDebug)) { fprintf( stderr, "Failed to find all required vulkan extentions for glfw and debug\n"); exit(EXIT_FAILURE); } } else { if (!verifyExtensionSupport(extensionCount, extensions, glfwExtensionCount, glfwExtensions)) { fprintf(stderr, "Failed to find all required vulkan extentions for glfw\n"); exit(EXIT_FAILURE); } } VkResult result = vkCreateInstance(&createInfo, NULL, &app->instance); if (result != VK_SUCCESS) { fprintf(stderr, "Failed to create vulkan instance\n"); exit(EXIT_FAILURE); } printf("Instance Extentions Available:\n"); for (uint i = 0; i < extensionCount; i++) { printf("\t%s\n", extensions[i].extensionName); } } void initWindow(Application *app) { glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); app->window = glfwCreateWindow(800, 600, "Vulkan", NULL, NULL); } VkResult CreateDebugUtilsMessengerEXT( VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkDebugUtilsMessengerEXT *pDebugMessenger) { PFN_vkCreateDebugUtilsMessengerEXT func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr( instance, "vkCreateDebugUtilsMessengerEXT"); if (func != NULL) { return func(instance, pCreateInfo, pAllocator, pDebugMessenger); } else { return VK_ERROR_EXTENSION_NOT_PRESENT; } } void setupDebugMessenger(Application *app) { if (!enableValidationLayers) { return; } if (!checkValidationLayerSupport()) { fprintf(stderr, "Validation layers requested, but not available!\n"); exit(EXIT_FAILURE); } VkDebugUtilsMessengerCreateInfoEXT createInfo = {}; populateDebugMessengerCreateInfo(&createInfo); if (CreateDebugUtilsMessengerEXT(app->instance, &createInfo, NULL, &app->debugMessenger) != VK_SUCCESS) { fprintf(stderr, "failed to set up debug messenger!\n"); } } void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, const VkAllocationCallbacks *pAllocator) { PFN_vkDestroyDebugUtilsMessengerEXT func = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr( instance, "vkDestroyDebugUtilsMessengerEXT"); if (func != NULL) { func(instance, debugMessenger, pAllocator); } } struct QueueFamilyIndices_s { bool graphicsFamilyExists; uint32_t graphicsFamily; } QueueFamilyIndices_default = {.graphicsFamilyExists = false, .graphicsFamily = 0}; typedef struct QueueFamilyIndices_s QueueFamilyIndices; QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) { QueueFamilyIndices indices = QueueFamilyIndices_default; uint32_t queueFamilyCount = 0; vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, NULL); VkQueueFamilyProperties queueFamilies[queueFamilyCount]; vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies); for (uint32_t i = 0; i < queueFamilyCount; i++) { if (queueFamilies[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { indices.graphicsFamily = i; indices.graphicsFamilyExists = true; break; // remove if we need additional checks in future } } return indices; } bool isDeviceSuitable(VkPhysicalDevice device) { QueueFamilyIndices indices = findQueueFamilies(device); return indices.graphicsFamilyExists; } void pickPhysicalDevice(Application *app) { VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; uint32_t physicalDeviceCount; vkEnumeratePhysicalDevices(app->instance, &physicalDeviceCount, NULL); if (physicalDeviceCount == 0) { fprintf(stderr, "Failed to find GPU with vulkan support!\n"); exit(EXIT_FAILURE); } VkPhysicalDevice physicalDevices[physicalDeviceCount]; vkEnumeratePhysicalDevices(app->instance, &physicalDeviceCount, physicalDevices); // TODO: pick device off of more than if it's just suitable for (int i = 0; i < physicalDeviceCount; i++) { if (isDeviceSuitable(physicalDevices[i])) { physicalDevice = physicalDevices[i]; break; } } if (physicalDevice == VK_NULL_HANDLE) { fprintf(stderr, "Failed to find suitable GPU\n"); exit(EXIT_FAILURE); } app->physicalDevice = physicalDevice; } void createLogicalDevice(Application *app) { // Specify Queues QueueFamilyIndices indeces = findQueueFamilies(app->physicalDevice); VkDeviceQueueCreateInfo queueCreateInfo = {}; queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; queueCreateInfo.queueFamilyIndex = indeces.graphicsFamily; queueCreateInfo.queueCount = 1; float queuePriority = 1.0f; queueCreateInfo.pQueuePriorities = &queuePriority; // Specify device features VkPhysicalDeviceFeatures deviceFeatures = {VK_FALSE}; // Specify logical device VkDeviceCreateInfo createInfo = {}; createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; createInfo.pQueueCreateInfos = &queueCreateInfo; createInfo.pEnabledFeatures = &deviceFeatures; createInfo.queueCreateInfoCount = 1; // Old Vulkan specify layers createInfo.enabledLayerCount = 0; createInfo.ppEnabledLayerNames = 0; if (enableValidationLayers) { createInfo.enabledLayerCount = validationLayerCount; createInfo.ppEnabledLayerNames = validationLayers; } if (vkCreateDevice(app->physicalDevice, &createInfo, NULL, &app->device) != VK_SUCCESS) { fprintf(stderr, "Failed to create logical device.\n"); exit(EXIT_FAILURE); } // Set graphics queue from logical device vkGetDeviceQueue(app->device, indeces.graphicsFamily, 0, &app->graphicsQueue); } void initVulkan(Application *app) { createInstance(app); setupDebugMessenger(app); pickPhysicalDevice(app); createLogicalDevice(app); } void mainLoop(Application *app) { while (!glfwWindowShouldClose(app->window)) { glfwPollEvents(); } } void cleanup(Application *app) { vkDestroyDevice(app->device, NULL); if (enableValidationLayers) { DestroyDebugUtilsMessengerEXT(app->instance, app->debugMessenger, NULL); } vkDestroyInstance(app->instance, NULL); glfwDestroyWindow(app->window); glfwTerminate(); } int main(void) { Application app; initWindow(&app); initVulkan(&app); mainLoop(&app); cleanup(&app); return EXIT_SUCCESS; }