You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.8 KiB
104 lines
2.8 KiB
1 year ago
|
using Microsoft.Win32;
|
||
2 years ago
|
|
||
1 year ago
|
namespace StabilityMatrix.Core.Helper;
|
||
2 years ago
|
|
||
1 year ago
|
public static class HardwareHelper
|
||
2 years ago
|
{
|
||
1 year ago
|
private const string GpuRegistryKeyPath =
|
||
|
@"SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}";
|
||
|
|
||
2 years ago
|
public static ulong GetGpuMemoryBytes()
|
||
|
{
|
||
|
var registry = Registry.LocalMachine;
|
||
1 year ago
|
var key = registry.OpenSubKey(
|
||
|
@"SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0000", false);
|
||
2 years ago
|
if (key == null)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
var vram = key.GetValue("HardwareInformation.qwMemorySize");
|
||
|
var vramLong = Convert.ToUInt64(vram);
|
||
|
return vramLong;
|
||
|
}
|
||
|
|
||
|
public static string GetGpuChipName()
|
||
|
{
|
||
|
var registry = Registry.LocalMachine;
|
||
1 year ago
|
var key = registry.OpenSubKey(
|
||
|
@"SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0000", false);
|
||
2 years ago
|
if (key == null)
|
||
|
{
|
||
|
return "Unknown";
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
var gpuName = key.GetValue("HardwareInformation.ChipType");
|
||
|
return gpuName?.ToString() ?? "Unknown";
|
||
|
}
|
||
1 year ago
|
|
||
|
/// <summary>
|
||
|
/// Yields GpuInfo for each GPU in the system.
|
||
|
/// </summary>
|
||
|
public static IEnumerable<GpuInfo> IterGpuInfo()
|
||
|
{
|
||
|
using var baseKey = Registry.LocalMachine.OpenSubKey(GpuRegistryKeyPath);
|
||
|
if (baseKey == null)
|
||
|
{
|
||
|
yield break;
|
||
|
}
|
||
|
|
||
|
foreach (var subKeyName in baseKey.GetSubKeyNames().Where(k => k.StartsWith("0")))
|
||
|
{
|
||
|
using var subKey = baseKey.OpenSubKey(subKeyName);
|
||
|
if (subKey != null)
|
||
|
{
|
||
|
yield return new GpuInfo
|
||
|
{
|
||
|
Name = subKey.GetValue("DriverDesc")?.ToString(),
|
||
|
MemoryBytes = Convert.ToUInt64(subKey.GetValue("HardwareInformation.qwMemorySize")),
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Return true if the system has at least one Nvidia GPU.
|
||
|
/// </summary>
|
||
|
public static bool HasNvidiaGpu()
|
||
|
{
|
||
1 year ago
|
return IterGpuInfo().Any(gpu => gpu.IsNvidia);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Return true if the system has at least one AMD GPU.
|
||
|
/// </summary>
|
||
|
public static bool HasAmdGpu()
|
||
|
{
|
||
|
return IterGpuInfo().Any(gpu => gpu.IsAmd);
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
|
public enum Level
|
||
|
{
|
||
|
Unknown,
|
||
|
Low,
|
||
|
Medium,
|
||
|
High
|
||
|
}
|
||
|
|
||
|
public record GpuInfo
|
||
|
{
|
||
|
public string? Name { get; init; } = string.Empty;
|
||
1 year ago
|
public ulong MemoryBytes { get; init; }
|
||
1 year ago
|
public Level? MemoryLevel => MemoryBytes switch
|
||
|
{
|
||
|
<= 0 => Level.Unknown,
|
||
|
< 4 * Size.GiB => Level.Low,
|
||
|
< 8 * Size.GiB => Level.Medium,
|
||
|
_ => Level.High
|
||
|
};
|
||
1 year ago
|
|
||
|
public bool IsNvidia => Name?.ToLowerInvariant().Contains("nvidia") ?? false;
|
||
|
public bool IsAmd => Name?.ToLowerInvariant().Contains("amd") ?? false;
|
||
2 years ago
|
}
|