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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use ash::{
    vk::{self},
    Instance,
};

use crate::vendor::Vendor;

pub struct PhysicalDevice {
    pub vendor: Vendor,
    pub device_name: String, // :
    pub device_type: DeviceType,
    // Device :
    pub device_id: u32, // :
    pub vendor_id: u32, // (VendorName)
    // Driver:
    pub driver_name: String, // :
    pub driver_info: String,
    // API:
    pub api_version: String,
    // VRAM:
    // pub heapbudget: u64,
    // pub heapsize: u64,
    // pub characteristics: Option<GPUCharacteristics>,
}

#[allow(dead_code)]
struct GPUCharacteristics {
    compute_units: u32,
    shader_engines: u32,
    shader_arrays_per_engine_count: u32,
    compute_units_per_shader_array: u32,
    simd_per_compute_unit: u32,
    wavefronts_per_simd: u32,
    wavefront_size: u32,
}

impl PhysicalDevice {
    pub fn new(instance: &Instance, physical_device: vk::PhysicalDevice) -> Self {
        let physical_device_properties: vk::PhysicalDeviceProperties =
            unsafe { instance.get_physical_device_properties(physical_device) };

        let mut driver_properties: vk::PhysicalDeviceDriverProperties =
            vk::PhysicalDeviceDriverProperties::default();

        let mut properties2: vk::PhysicalDeviceProperties2 =
            vk::PhysicalDeviceProperties2::default().push_next(&mut driver_properties);

        let _: () = unsafe {
            instance.get_physical_device_properties2(physical_device, &mut properties2);
        };

        let vendor_id = physical_device_properties.vendor_id;

        let vendor = Vendor::from_vendor_id(vendor_id).unwrap();

        let device_name = physical_device_properties
            .device_name_as_c_str()
            .unwrap()
            .to_str()
            .unwrap();

        let device_type = DeviceType::from(physical_device_properties.device_type.as_raw());

        let device_id = physical_device_properties.device_id;

        let api_version = decode_version_number(physical_device_properties.api_version);

        let driver_name = driver_properties
            .driver_name_as_c_str()
            .unwrap()
            .to_string_lossy()
            .to_string();

        let driver_info = driver_properties
            .driver_info_as_c_str()
            .unwrap()
            .to_string_lossy()
            .to_string();

        // asdtest(driver_properties);

        PhysicalDevice {
            vendor,
            device_name: device_name.to_string(),
            device_type,
            device_id,
            vendor_id,
            driver_name,
            driver_info,
            api_version,
        }
    }
}

// fn asdtest(driver_properties: vk::PhysicalDeviceDriverProperties<'static>) {
//     println!(
//         "Driver Name: {:?}",
//         driver_properties.driver_name_as_c_str().unwrap()
//     );

//     println!(
//         "Driver Info: {:?}",
//         driver_properties.driver_info_as_c_str().unwrap()
//     );

//     println!(
//         "Conformance Version: {:?}",
//         driver_properties.conformance_version
//     );
// }

pub enum DeviceType {
    Other = 0,
    IntegratedGPU = 1,
    DiscreteGPU = 2,
    VirtualGPU = 3,
    CPU = 4,
    Unknown = 5,
}

impl DeviceType {
    pub fn from(id: i32) -> Self {
        match id {
            0 => DeviceType::Other,
            1 => DeviceType::IntegratedGPU,
            2 => DeviceType::DiscreteGPU,
            3 => DeviceType::VirtualGPU,
            4 => DeviceType::CPU,
            _ => DeviceType::Unknown,
        }
    }

    pub fn name(&self) -> &'static str {
        match self {
            DeviceType::Other => "Other",
            DeviceType::IntegratedGPU => "Integrated GPU",
            DeviceType::DiscreteGPU => "Discrete GPU",
            DeviceType::VirtualGPU => "Virtual GPU",
            DeviceType::CPU => "CPU",
            DeviceType::Unknown => "Unknown",
        }
    }
}

/*
    The variant is a 3-bit integer packed into bits 31-29.
    The major version is a 7-bit integer packed into bits 28-22.
    The minor version number is a 10-bit integer packed into bits 21-12.
    The patch version number is a 12-bit integer packed into bits 11-0.
*/
pub fn decode_version_number(version: u32) -> String {
    let variant = (version >> 29) & 0b111;
    let major = (version >> 22) & 0b1111111;
    let minor = (version >> 12) & 0b1111111111;
    let patch = version & 0b111111111111;
    format!("{}.{}.{}.{}", variant, major, minor, patch)
}