/* Minimal end-to-end Vulkan program: create an instance and a device,
 * destroy both from an atexit handler.
 */

#include <vulkan/vulkan.h>
#include <stdio.h>
#include <stdlib.h>

static VkInstance instance;
static VkDevice   device;

static void cleanup(void) {
    if (device)   vkDestroyDevice(device, NULL);
    if (instance) vkDestroyInstance(instance, NULL);
}

int main(void) {
    atexit(cleanup);

    VkInstanceCreateInfo instanceCreateInfo = {
        .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
    };
    if (vkCreateInstance(&instanceCreateInfo, NULL, &instance) != VK_SUCCESS) {
        fprintf(stderr, "vkCreateInstance failed\n");
        return 1;
    }

    uint32_t physicalDeviceCount = 1;
    VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
    vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, &physicalDevice);
    if (!physicalDevice) {
        fprintf(stderr, "no physical devices enumerated (mesa-vulkan-drivers installed?)\n");
        return 1;
    }

    float queuePriority = 1.0f;
    VkDeviceQueueCreateInfo queueCreateInfo = {
        .sType            = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
        .queueFamilyIndex = 0,
        .queueCount       = 1,
        .pQueuePriorities = &queuePriority,
    };
    VkDeviceCreateInfo deviceCreateInfo = {
        .sType                = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
        .queueCreateInfoCount = 1,
        .pQueueCreateInfos    = &queueCreateInfo,
    };
    if (vkCreateDevice(physicalDevice, &deviceCreateInfo, NULL, &device) != VK_SUCCESS) {
        fprintf(stderr, "vkCreateDevice failed\n");
        return 1;
    }

    return 0;
}
