mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-12-03 10:15:19 +01:00
92ed2c39f0
The starter read the cards from the registry's list of video devices, which each driver fills as it chooses: a player with an RTX 3060 beside a Ryzen's Radeon saw only the Radeon. The cards now come from DXGI, the adapters Windows draws with and whose drivers the simulator's OpenGL runs on, without software adapters such as the Basic Render Driver. The registry answers only when DXGI cannot be asked or lists no card. DXGI is called through function pointers, which NativeAOT compiles without reflection. Co-authored-by: maj00r <maj00r@host>
276 lines
10 KiB
C#
276 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.Versioning;
|
|
using System.Security;
|
|
using StarterNG.Application.Abstractions;
|
|
using StarterNG.Domain.Settings;
|
|
|
|
namespace StarterNG.Infrastructure;
|
|
|
|
/// <summary>
|
|
/// The names and memory of the machine's graphics cards, the one a simulator
|
|
/// runs on first. Nothing on macOS, where the simulator does not run natively.
|
|
/// </summary>
|
|
public sealed class GraphicsCards
|
|
{
|
|
private static readonly string[] PciLists =
|
|
{
|
|
"/usr/share/hwdata/pci.ids", "/usr/share/misc/pci.ids", "/usr/share/pci.ids",
|
|
// The host's, from inside the Steam Linux Runtime container.
|
|
"/run/host/usr/share/hwdata/pci.ids", "/run/host/usr/share/misc/pci.ids"
|
|
};
|
|
|
|
private const string RegistryMachine = @"\Registry\Machine\";
|
|
|
|
private readonly IFileSystem _files;
|
|
private readonly IEnvironment _environment;
|
|
private readonly LinuxGpus _gpus;
|
|
private readonly Func<IReadOnlyList<string>> _nvidiaSmi;
|
|
private readonly Func<IReadOnlyList<DxgiAdapter>?> _dxgi;
|
|
private readonly Func<IReadOnlyList<GraphicsCard>> _registry;
|
|
|
|
public GraphicsCards(IFileSystem files, IEnvironment environment, LinuxGpus gpus,
|
|
Func<IReadOnlyList<string>>? nvidiaSmi = null,
|
|
Func<IReadOnlyList<DxgiAdapter>?>? dxgi = null,
|
|
Func<IReadOnlyList<GraphicsCard>>? registry = null)
|
|
{
|
|
_files = files;
|
|
_environment = environment;
|
|
_gpus = gpus;
|
|
_nvidiaSmi = nvidiaSmi ?? AskNvidiaSmi;
|
|
_dxgi = dxgi ?? AskDxgi;
|
|
_registry = registry ?? ReadRegistry;
|
|
}
|
|
|
|
/// <summary>Takes a while on Linux, where the PCI list is read: not for the UI thread.</summary>
|
|
public IReadOnlyList<GraphicsCard> List()
|
|
{
|
|
IEnumerable<GraphicsCard> cards = Array.Empty<GraphicsCard>();
|
|
if (_environment.IsLinux)
|
|
cards = FromLinux();
|
|
else if (_environment.IsWindows)
|
|
cards = FromWindows();
|
|
|
|
// A program goes to the discrete card, on Windows by its own request.
|
|
return cards.OrderBy(card => card.SharedMemory).ThenByDescending(card => card.MemoryBytes).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// DXGI's list, the adapters the simulator's OpenGL runs on. The registry's list of
|
|
/// video devices only when DXGI cannot be asked or lists no card: there each driver
|
|
/// writes what it chooses, and some leave their card out (an RTX 3060 beside a Ryzen's
|
|
/// Radeon was missing from it).
|
|
/// </summary>
|
|
private IReadOnlyList<GraphicsCard> FromWindows()
|
|
{
|
|
var adapters = _dxgi();
|
|
var cards = adapters is null ? Array.Empty<GraphicsCard>() : DxgiAdapters.Cards(adapters);
|
|
return cards.Count > 0 ? cards : _registry();
|
|
}
|
|
|
|
private static IReadOnlyList<DxgiAdapter>? AskDxgi() =>
|
|
OperatingSystem.IsWindows() ? DxgiAdapters.Read() : null;
|
|
|
|
private static IReadOnlyList<GraphicsCard> ReadRegistry() =>
|
|
OperatingSystem.IsWindows() ? FromRegistry() : Array.Empty<GraphicsCard>();
|
|
|
|
[SupportedOSPlatform("windows")]
|
|
private static IReadOnlyList<GraphicsCard> FromRegistry()
|
|
{
|
|
try
|
|
{
|
|
using var video = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"HARDWARE\DEVICEMAP\VIDEO");
|
|
if (video is null)
|
|
return Array.Empty<GraphicsCard>();
|
|
|
|
var cards = new List<GraphicsCard>();
|
|
foreach (string name in video.GetValueNames())
|
|
{
|
|
if (!name.StartsWith(@"\Device\Video", StringComparison.OrdinalIgnoreCase) ||
|
|
video.GetValue(name) is not string path ||
|
|
!path.StartsWith(RegistryMachine, StringComparison.OrdinalIgnoreCase))
|
|
continue;
|
|
|
|
using var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path[RegistryMachine.Length..]);
|
|
// The system's basic display driver and remote desktops are no cards.
|
|
if (key?.GetValue("DriverDesc") is not string card ||
|
|
key.GetValue("MatchingDeviceId") is not string device ||
|
|
!device.StartsWith(@"pci\", StringComparison.OrdinalIgnoreCase))
|
|
continue;
|
|
|
|
long memory = MemoryIn(key.GetValue("HardwareInformation.qwMemorySize"));
|
|
if (memory == 0)
|
|
memory = MemoryIn(key.GetValue("HardwareInformation.MemorySize"));
|
|
cards.Add(new GraphicsCard(card, memory));
|
|
}
|
|
|
|
// A card is listed once for each of its outputs.
|
|
return cards.DistinctBy(card => card.Name).ToList();
|
|
}
|
|
catch (Exception e) when (e is IOException or UnauthorizedAccessException or SecurityException)
|
|
{
|
|
return Array.Empty<GraphicsCard>();
|
|
}
|
|
}
|
|
|
|
private static long MemoryIn(object? value) => value switch
|
|
{
|
|
long qword => qword,
|
|
int dword => (uint)dword,
|
|
byte[] { Length: 8 } bytes => BitConverter.ToInt64(bytes),
|
|
byte[] { Length: 4 } bytes => BitConverter.ToUInt32(bytes),
|
|
_ => 0
|
|
};
|
|
|
|
private IEnumerable<GraphicsCard> FromLinux()
|
|
{
|
|
var gpus = _gpus.List();
|
|
if (gpus.Count == 0)
|
|
return Array.Empty<GraphicsCard>();
|
|
|
|
IReadOnlyList<string>? pciList = null;
|
|
var nvidiaMemory = gpus.Any(gpu => gpu.Driver == "nvidia")
|
|
? ParseNvidiaMemory(_nvidiaSmi())
|
|
: new Dictionary<string, long>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
var cards = new List<GraphicsCard>();
|
|
foreach (var gpu in gpus)
|
|
{
|
|
string? model = NvidiaModel(gpu.Slot);
|
|
if (model is null)
|
|
{
|
|
pciList ??= ReadPciList();
|
|
model = ModelInPciList(pciList, gpu.PciId);
|
|
}
|
|
|
|
string name = model is null ? $"{gpu.Vendor} {gpu.PciId}"
|
|
: model.StartsWith(gpu.Vendor, StringComparison.OrdinalIgnoreCase) ? model
|
|
: $"{gpu.Vendor} {model}";
|
|
long memory = gpu.MemoryBytes > 0 ? gpu.MemoryBytes : nvidiaMemory.GetValueOrDefault(gpu.Slot);
|
|
cards.Add(new GraphicsCard(name, memory));
|
|
}
|
|
|
|
return cards;
|
|
}
|
|
|
|
private string? NvidiaModel(string slot)
|
|
{
|
|
string path = $"/proc/driver/nvidia/gpus/{slot}/information";
|
|
try
|
|
{
|
|
if (!_files.FileExists(path))
|
|
return null;
|
|
|
|
foreach (string line in _files.ReadAllLines(path))
|
|
if (line.StartsWith("Model:", StringComparison.Ordinal))
|
|
return line["Model:".Length..].Trim();
|
|
}
|
|
catch (Exception e) when (e is IOException or UnauthorizedAccessException)
|
|
{
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private IReadOnlyList<string> ReadPciList()
|
|
{
|
|
foreach (string path in PciLists)
|
|
{
|
|
try
|
|
{
|
|
if (_files.FileExists(path))
|
|
return _files.ReadAllLines(path);
|
|
}
|
|
catch (Exception e) when (e is IOException or UnauthorizedAccessException)
|
|
{
|
|
}
|
|
}
|
|
|
|
return Array.Empty<string>();
|
|
}
|
|
|
|
/// <summary>What a pci.ids list calls a card: the marketing name in brackets, if it has one.</summary>
|
|
public static string? ModelInPciList(IEnumerable<string> lines, string pciId)
|
|
{
|
|
string[] ids = pciId.ToLowerInvariant().Split(':');
|
|
if (ids.Length != 2)
|
|
return null;
|
|
|
|
string vendorLine = ids[0] + " ";
|
|
string deviceLine = "\t" + ids[1] + " ";
|
|
bool inVendor = false;
|
|
foreach (string line in lines)
|
|
{
|
|
if (line.Length == 0 || line[0] == '#')
|
|
continue;
|
|
|
|
if (line[0] != '\t')
|
|
{
|
|
if (inVendor)
|
|
return null;
|
|
inVendor = line.StartsWith(vendorLine, StringComparison.Ordinal);
|
|
}
|
|
else if (inVendor && line.StartsWith(deviceLine, StringComparison.Ordinal))
|
|
{
|
|
string model = line[deviceLine.Length..].Trim();
|
|
int bracket = model.LastIndexOf('[');
|
|
return bracket >= 0 && model.EndsWith(']') ? model[(bracket + 1)..^1] : model;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/// <summary>Lines of "00000000:01:00.0, 8188": the PCI address, as /sys gives it with a shorter domain, and MiB.</summary>
|
|
public static IReadOnlyDictionary<string, long> ParseNvidiaMemory(IEnumerable<string> lines)
|
|
{
|
|
var memory = new Dictionary<string, long>(StringComparer.OrdinalIgnoreCase);
|
|
foreach (string line in lines)
|
|
{
|
|
string[] fields = line.Split(',', StringSplitOptions.TrimEntries);
|
|
if (fields.Length == 2 && fields[0].Length >= 12 && long.TryParse(fields[1], out long mib))
|
|
memory[fields[0][^12..]] = mib << 20;
|
|
}
|
|
|
|
return memory;
|
|
}
|
|
|
|
private static IReadOnlyList<string> AskNvidiaSmi()
|
|
{
|
|
var start = new ProcessStartInfo("nvidia-smi", "--query-gpu=pci.bus_id,memory.total --format=csv,noheader,nounits")
|
|
{
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true
|
|
};
|
|
|
|
try
|
|
{
|
|
using var process = Process.Start(start);
|
|
if (process is null)
|
|
return Array.Empty<string>();
|
|
|
|
process.ErrorDataReceived += (_, _) => { };
|
|
process.BeginErrorReadLine();
|
|
var output = process.StandardOutput.ReadToEndAsync();
|
|
if (!process.WaitForExit(3000))
|
|
{
|
|
process.Kill();
|
|
return Array.Empty<string>();
|
|
}
|
|
|
|
return output.GetAwaiter().GetResult().Split('\n', StringSplitOptions.RemoveEmptyEntries);
|
|
}
|
|
// Not installed, as it often is not inside the Steam Linux Runtime container.
|
|
catch (Exception e) when (e is Win32Exception or InvalidOperationException or IOException)
|
|
{
|
|
return Array.Empty<string>();
|
|
}
|
|
}
|
|
}
|