Skip to content

Commit f75e263

Browse files
committed
fixed amd gpu name detection
1 parent 796701e commit f75e263

File tree

3 files changed

+87
-12
lines changed

3 files changed

+87
-12
lines changed

src-tauri/Cargo.lock

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ battery = "0.7.8"
2020
libmedium = "0.8.1"
2121
dirs = "5.0.1"
2222
nvml-wrapper = "0.10.0"
23-
ash = "0.38.0"
23+
ash = "0.37.3"
2424

2525
[features]
2626
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!

src-tauri/src/gpu.rs

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use ash::vk;
2-
use ash::Entry;
31
use serde::{Deserialize, Serialize};
42
use nvml_wrapper::Nvml;
53
use nvml_wrapper::error::NvmlError;
64
use std::error::Error;
7-
use std::ffi::CStr;
85
use std::fs::{read_dir, read_to_string};
96
use std::path::PathBuf;
7+
use ash::vk;
8+
use ash::Entry;
9+
use std::ffi::CStr;
1010
use std::ptr;
1111

1212
#[derive(Serialize, Deserialize, Debug)]
@@ -114,10 +114,10 @@ fn read_hwmon_info(
114114

115115
if let Ok(power_input) = read_to_string(hwmon_path.join("power1_average")) {
116116
let power_mw = power_input.trim().parse::<u64>().unwrap_or(0);
117-
wattage = Some(format!("{:.3} W", power_mw as f64 / 1000.0)); // Divide by 1000 to convert mW to W
117+
wattage = Some(format!("{:.3} W", power_mw as f64 / 1000000.0));
118118
} else if let Ok(power_input) = read_to_string(hwmon_path.join("power1_input")) {
119119
let power_mw = power_input.trim().parse::<u64>().unwrap_or(0);
120-
wattage = Some(format!("{:.3} W", power_mw as f64 / 1000.0)); // Divide by 1000 to convert mW to W
120+
wattage = Some(format!("{:.3} W", power_mw as f64 / 1000000.0));
121121
}
122122
}
123123
}
@@ -126,6 +126,71 @@ fn read_hwmon_info(
126126
(fan_speed, temperature, clock_speed, wattage)
127127
}
128128

129+
fn get_amd_gpu_name() -> String {
130+
// Initialize Vulkan entry point
131+
let entry = unsafe { Entry::load().expect("Failed to create Vulkan entry") };
132+
133+
// Create a Vulkan instance
134+
let app_name = CStr::from_bytes_with_nul(b"AMD GPU Detector\0").unwrap();
135+
let engine_name = CStr::from_bytes_with_nul(b"No Engine\0").unwrap();
136+
137+
let app_info = vk::ApplicationInfo {
138+
s_type: vk::StructureType::APPLICATION_INFO,
139+
p_next: ptr::null(),
140+
p_application_name: app_name.as_ptr(),
141+
p_engine_name: engine_name.as_ptr(),
142+
application_version: vk::make_api_version(0, 1, 0, 0),
143+
engine_version: vk::make_api_version(0, 1, 0, 0),
144+
api_version: vk::make_api_version(0, 1, 0, 0),
145+
};
146+
147+
let create_info = vk::InstanceCreateInfo {
148+
s_type: vk::StructureType::INSTANCE_CREATE_INFO,
149+
p_next: ptr::null(),
150+
flags: vk::InstanceCreateFlags::empty(),
151+
p_application_info: &app_info,
152+
enabled_layer_count: 0,
153+
pp_enabled_layer_names: ptr::null(),
154+
enabled_extension_count: 0,
155+
pp_enabled_extension_names: ptr::null(),
156+
};
157+
158+
let instance = unsafe {
159+
entry
160+
.create_instance(&create_info, None)
161+
.expect("Failed to create Vulkan instance")
162+
};
163+
164+
// Enumerate physical devices (GPUs)
165+
let physical_devices = unsafe {
166+
instance
167+
.enumerate_physical_devices()
168+
.expect("Failed to enumerate physical devices")
169+
};
170+
171+
// Get the name of the first GPU
172+
let device_name = if let Some(&device) = physical_devices.first() {
173+
// Get device properties
174+
let properties = unsafe { instance.get_physical_device_properties(device) };
175+
176+
// Convert the device name to a Rust String
177+
unsafe {
178+
CStr::from_ptr(properties.device_name.as_ptr())
179+
.to_string_lossy()
180+
.into_owned()
181+
}
182+
} else {
183+
String::from("No GPU found")
184+
};
185+
186+
// Clean up Vulkan instance
187+
unsafe {
188+
instance.destroy_instance(None);
189+
}
190+
191+
device_name
192+
}
193+
129194
fn get_amd_gpu_info() -> Result<GpuInformations, Box<dyn Error>> {
130195
let drm_path = PathBuf::from("/sys/class/drm/");
131196
let mut amd_gpu_path = None;
@@ -184,7 +249,7 @@ fn get_amd_gpu_info() -> Result<GpuInformations, Box<dyn Error>> {
184249
let utilization = read_gpu_busy_percent(&device_path);
185250

186251
Ok(GpuInformations {
187-
name: Some("AMD GPU".to_string()),
252+
name: Some(get_amd_gpu_name()),
188253
driver_version: None,
189254
memory_total,
190255
memory_used,

0 commit comments

Comments
 (0)