1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#![allow(dead_code)]
pub mod ascii_art;
pub mod device;
pub mod vendor;

use ash::*;
use device::PhysicalDevice;
use std::str;
use vendor::Vendor;

pub fn fetch_device(instance: &Instance, device: vk::PhysicalDevice) {
    let properties = unsafe { instance.get_physical_device_properties(device) };
    let mut properties2 = vk::PhysicalDeviceProperties2::default();
    unsafe { instance.get_physical_device_properties2(device, &mut properties2) }

    let vendor = Vendor::from_vendor_id(properties.vendor_id)
        .unwrap_or_else(|| panic!("unknown vendor: {}", properties.vendor_id));

    let art = vendor.get_ascii_art();

    let device = PhysicalDevice::new(instance, device);

    let info = get_device_info(device);

    let empty = "".to_string();
    for i in 0..art.len().max(info.len()) {
        let art_line = art.get(i).unwrap_or(&empty);
        let info_line = info.get(i).unwrap_or(&empty);
        println!(" {} {}", art_line, info_line);
    }

    println!();
}

const BOLD: &str = "\x1B[1m";
const RESET: &str = "\x1B[0m";
const ALIGNMENT: &str = "    ";

fn get_device_info(device: PhysicalDevice) -> Vec<String> {
    let mut output: Vec<String> = Vec::new();

    output.push(format!(
        "{}{} : {}{}",
        BOLD,
        device.device_name,
        RESET,
        device.device_type.name()
    ));

    output.push(format!(
        "{}Device: 0x{:X} : 0x{:X} ({})",
        ALIGNMENT,
        device.device_id,
        device.vendor_id,
        device.vendor.name(),
    ));

    output.push(format!(
        "{}Driver: {} : {}",
        ALIGNMENT, device.driver_name, device.driver_info
    ));

    output.push(format!("{}API: {}", ALIGNMENT, device.api_version,));

    output
}

pub fn iterate_devices() {
    let entry = unsafe { Entry::load().unwrap() };
    let appinfo = vk::ApplicationInfo {
        api_version: vk::API_VERSION_1_3,
        ..Default::default()
    };

    let create_info = vk::InstanceCreateInfo::default().application_info(&appinfo);
    let instance = unsafe { entry.create_instance(&create_info, None).unwrap() };

    let devices = unsafe { instance.enumerate_physical_devices().unwrap() };
    for device in devices {
        // let x = unsafe {
        //     instance
        //         .enumerate_device_extension_properties(device)
        //         .unwrap()
        // };
        // for x in x {
        //     println!(
        //         "{:?}",
        //         x.extension_name_as_c_str()
        //             .unwrap()
        //             .to_string_lossy()
        //             .to_string()
        //     );
        // }
        fetch_device(&instance, device);
    }
}