short vulkan driver query

 avatar
unknown
c_cpp
3 years ago
3.5 kB
4
Indexable
#include <vulkan/vulkan.h>
#include <fmt/core.h>
#include <cstdint>
#include <string>
#include <vector>

#define ITOP(i) reinterpret_cast<std::uint64_t>(instance)

std::string conver_to_str(VkConformanceVersion ver)
{
    return fmt::format("{}.{}.{}-{}",
            ver.major,
            ver.minor,
            ver.subminor,
            ver.patch);
}

constexpr std::string_view device_type_to_str(VkPhysicalDeviceType type)
{
    if(VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_CPU == type)
    {
        return "CPU";
    }
    if(VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU == type)
    {
        return "Integrated GPU";
    }
    if(VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU == type)
    {
        return "Discrete GPU";
    }
    if(VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU == type)
    {
        return "Virtual GPU";
    }
    if(VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_OTHER == type)
    {
        return "Other";
    }

    return "Unknown";
}

int main()
{
    VkInstance instance;

    VkApplicationInfo app_info =
    {
        .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
        .pNext = nullptr,
        .pApplicationName = "vulkan-linear-example",
        .applicationVersion = 0x01,
        .pEngineName = "no-engine",
        .engineVersion = 0x01,
        .apiVersion = VK_API_VERSION_1_2,
    };

    VkInstanceCreateInfo instance_create_info
    {
        .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
        .pNext = nullptr,
        .flags = 0,
        .pApplicationInfo = &app_info,
        .enabledLayerCount = 0,
        .ppEnabledLayerNames = nullptr,
        .enabledExtensionCount = 0,
        .ppEnabledExtensionNames = nullptr
    };

    auto res = vkCreateInstance(&instance_create_info, nullptr, &instance);

    if(VK_SUCCESS != res)
    {
        fmt::print("Error creating Vulkan Instance: {}\n",res);
        return -1;
    }

    //fmt::print("Vulkan instance created successfully: {:#08X}\n", ITOP(instance));


    // Let's see what kind of physical devices we have
    std::uint32_t physical_device_count;
    std::vector<VkPhysicalDevice> physical_devices;

    vkEnumeratePhysicalDevices(instance, &physical_device_count, nullptr);
    physical_devices.resize(physical_device_count);

    vkEnumeratePhysicalDevices(instance, &physical_device_count, physical_devices.data());
    
    for(std::uint32_t i = 0; i < physical_device_count; i++)
    {
        VkPhysicalDeviceDriverProperties driver_properties = 
        {
            .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES,
            .pNext = nullptr
        };
        VkPhysicalDeviceProperties2 properties =
        {
            .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
            .pNext = &driver_properties
        };
        auto phy_device = physical_devices[i];
        vkGetPhysicalDeviceProperties2(phy_device, &properties);

        fmt::print("Physical device {}:\n", i);
        fmt::print("  Name:              {}\n", properties.properties.deviceName);
        fmt::print("  Device type:       {}\n", device_type_to_str(properties.properties.deviceType));

        fmt::print("  Driver ID:         {}\n", driver_properties.driverID);
        fmt::print("  Driver name:       {}\n", driver_properties.driverName);
        fmt::print("  Driver conformance\n"
                   "  version:           {}\n", conver_to_str(driver_properties.conformanceVersion));
    }


    vkDestroyInstance(instance, nullptr);


}

Editor is loading...