zenyx-engine-telemetry/subcrates/telemetry/src/modules/gpu/mod.rs
2025-05-04 00:36:38 -04:00

131 lines
3.5 KiB
Rust

use super::UNKNOWN;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use wgpu;
use winit::{
application::ApplicationHandler,
event::WindowEvent,
event_loop::{ActiveEventLoop, EventLoop},
window::WindowId,
};
mod vram;
#[derive(Debug, Serialize, Deserialize)]
pub struct DriverInfo {
version: String,
name: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AdapterInfo {
vendor: String,
model: String,
driver: DriverInfo,
vram: String,
display: DisplayInfo,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GPUInfo {
supported_backends: Vec<String>,
gpus: Vec<AdapterInfo>,
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct DisplayInfo {
resolution: String,
refresh_rate: String,
}
impl ApplicationHandler for DisplayInfo {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
if let Some(monitor) = event_loop.primary_monitor() {
let size = monitor.size();
let refresh_rate = monitor.refresh_rate_millihertz();
self.resolution = format!("{}x{}", size.width, size.height);
self.refresh_rate = if let Some(refresh) = refresh_rate {
format!("{} hz", refresh / 1000)
} else {
UNKNOWN.to_string()
}
} else {
self.resolution = UNKNOWN.to_string();
self.refresh_rate = UNKNOWN.to_string();
}
event_loop.exit();
}
fn window_event(
&mut self,
_event_loop: &ActiveEventLoop,
_window_id: WindowId,
_event: WindowEvent,
) {
}
}
fn vendor_name(vendor: u32) -> &'static str {
match vendor {
0x10DE => "NVIDIA",
0x1002 => "AMD(Advanced Micro Devices), Inc.",
0x8086 => "Intel(integrated electronics)",
0x13B5 => "ARM(Advanced RISC Machines)",
0x5143 => "Qualcomm(Quality Communications)",
0x1010 => "Apple Inc.",
_ => UNKNOWN,
}
}
#[allow(dead_code)]
pub fn get_struct() -> GPUInfo {
let mut gpu_data: Vec<AdapterInfo> = Vec::new();
let mut backends: Vec<String> = Vec::new();
let instance_descriptor = wgpu::InstanceDescriptor {
backends: wgpu::Backends::all(),
flags: wgpu::InstanceFlags::empty(),
backend_options: Default::default(),
};
let instance = wgpu::Instance::new(&instance_descriptor);
let event_loop = EventLoop::new().unwrap();
let mut app = DisplayInfo::default();
event_loop.run_app(&mut app).unwrap();
for adapter in instance.enumerate_adapters(wgpu::Backends::all()) {
let info = adapter.get_info();
if !backends.contains(&info.backend.to_string()) {
backends.push(info.backend.to_string());
}
if info.driver.is_empty() || info.driver_info.is_empty() {
continue;
}
gpu_data.push(AdapterInfo {
vendor: vendor_name(info.vendor).to_string(),
model: info.name,
driver: DriverInfo {
version: info.driver_info,
name: info.driver,
},
vram: vram::get(info.vendor, info.device),
display: DisplayInfo {
resolution: app.resolution.to_string(),
refresh_rate: app.refresh_rate.to_string(),
},
});
}
GPUInfo {
supported_backends: backends,
gpus: gpu_data,
}
}
#[allow(dead_code)]
pub fn get_json() -> Value {
json!(get_struct())
}