Files
Reims-Go/internal/host/gpu_compat.go
T
icaruszezen b3b5f7a0fd fix(host): exclude unplugged and inactive display adapters on Windows
Enumerate Windows display adapters via SetupAPI (DIGCF_PRESENT) and CfgMgr32 (CM_Get_DevNode_Status: DN_STARTED, no DN_HAS_PROBLEM, no DN_WILL_BE_REMOVED) instead of blindly scanning Control\Class\{4d36e968-...}\0000..0031, so unplugged (phantom) and disabled GPUs whose DriverStore ICD files still remain on disk are no longer listed as available GPUs. Also reuse active adapter filtering for DriverStore manifest recovery and prevent MergeRegistryAndVulkanGPUs from cross-matching distinct PCI devices by generic name.
2026-12-02 10:09:57 +08:00

269 lines
8.1 KiB
Go

package host
import (
"fmt"
"regexp"
"strings"
)
// RequiredVulkanExtensions mirrors the mandatory device extensions enforced by
// upstream metal2vulkan-runtime (crates/metal2vulkan-runtime/src/floor.rs:36-41).
var RequiredVulkanExtensions = []string{
"VK_KHR_swapchain",
"VK_EXT_robustness2",
"VK_EXT_calibrated_timestamps",
"VK_EXT_line_rasterization",
}
// VulkanDeviceProbe holds raw Vulkan physical-device properties queried via
// vulkan-1.dll on Windows.
type VulkanDeviceProbe struct {
Name string
VendorID uint32
DeviceID uint32
DeviceType uint32 // VkPhysicalDeviceType: 1=integrated, 2=discrete, 3=virtual, 4=cpu
APIVersion uint32
Extensions map[string]bool
}
var (
rePCIVen = regexp.MustCompile(`(?i)VEN_([0-9a-f]{4})`)
rePCIDev = regexp.MustCompile(`(?i)DEV_([0-9a-f]{4})`)
)
// ParsePCIIDs extracts lowercase 4-hex vendor and device IDs from a Windows
// PnP MatchingDeviceId string (e.g. "PCI\VEN_1002&DEV_164C&REV_C2").
func ParsePCIIDs(matchingID string) (vendorID, deviceID string) {
if m := rePCIVen.FindStringSubmatch(matchingID); len(m) == 2 {
vendorID = strings.ToLower(m[1])
}
if m := rePCIDev.FindStringSubmatch(matchingID); len(m) == 2 {
deviceID = strings.ToLower(m[1])
}
return vendorID, deviceID
}
// FormatVulkanVersion decodes a Vulkan packed API version uint32 into "major.minor.patch".
func FormatVulkanVersion(apiVersion uint32) string {
if apiVersion == 0 {
return ""
}
major := (apiVersion >> 22) & 0x7F
minor := (apiVersion >> 12) & 0x3FF
patch := apiVersion & 0xFFF
return fmt.Sprintf("%d.%d.%d", major, minor, patch)
}
func vulkanSupports13(apiVersion uint32) bool {
major := (apiVersion >> 22) & 0x7F
minor := (apiVersion >> 12) & 0x3FF
return major > 1 || (major == 1 && minor >= 3)
}
// amdLegacyVegaOrPolarisIDs covers AMD GCN4 (Polaris) and GCN5 (Vega) GPUs
// whose Windows proprietary Adrenalin driver moved to the legacy vega-polaris
// maintenance branch (26.5.2) and currently hits WindowServer black screen /
// vkQueueSubmit2 ERROR_DEVICE_LOST on Windows (steelbrain/reims-vgpu#181).
var amdLegacyVegaOrPolarisIDs = map[string]bool{
// Vega integrated APUs (Raven / Picasso / Renoir / Cezanne / Lucienne / Barcelo)
"15dd": true, // Raven Ridge / Picasso (Ryzen 2000/3000)
"15d8": true, // Picasso (Ryzen 3000U/H)
"1636": true, // Renoir (Ryzen 4000U/H/G)
"1638": true, // Cezanne / Barcelo (Ryzen 5000H/G)
"164c": true, // Lucienne (Ryzen 3 5300U / Ryzen 5 5500U / Ryzen 7 5700U)
"15e7": true, // Barcelo / Barcelo-R (Ryzen 5 5625U / Ryzen 7 5825U / 7530U / 7730U)
// Vega discrete GPUs (Vega 10 / 12 / 20)
"6860": true, "6861": true, "6862": true, "6863": true, "6864": true,
"6867": true, "6868": true, "686c": true, "687f": true,
"69a0": true, "69a1": true, "69a2": true, "69a3": true, "69af": true,
"66a0": true, "66a1": true, "66a2": true, "66a3": true, "66a7": true, "66af": true,
// Polaris discrete GPUs (RX 460/470/480/550/560/570/580/590)
"67df": true, "67ef": true, "67ff": true, "6fdf": true,
}
func isAMDLegacyVegaOrPolaris(vendorID, deviceID, name string) bool {
if vendorID == "1002" && deviceID != "" {
return amdLegacyVegaOrPolarisIDs[strings.ToLower(deviceID)]
}
if vendorID != "" && vendorID != "1002" {
return false
}
// Name fallback only when PCI device ID is unavailable.
low := strings.ToLower(name)
if !strings.Contains(low, "radeon") {
return false
}
for _, pat := range []string{
"radeon(tm) vega", "radeon vega", "rx vega", "radeon vii",
"rx 460", "rx 470", "rx 480", "rx 550", "rx 560", "rx 570", "rx 580", "rx 590",
} {
if strings.Contains(low, pat) {
return true
}
}
return false
}
// isAMDRDNA2DeviceLost reports GPUs hitting deterministic Windows 11
// vkQueueSubmit2 ERROR_DEVICE_LOST / GPU fault (READ_INVALID) at boot
// (steelbrain/reims-vgpu#173).
func isAMDRDNA2DeviceLost(vendorID, deviceID string) bool {
return vendorID == "1002" && strings.ToLower(deviceID) == "73ff"
}
// EvaluateGPUCompat merges optional Vulkan runtime probe data into g and
// assigns a compatibility grade aligned with metal2vulkan-runtime's floor
// requirements and verified Windows upstream issues.
func EvaluateGPUCompat(g GPUInfo, probe *VulkanDeviceProbe) GPUInfo {
if probe != nil {
if g.Name == "" {
g.Name = probe.Name
}
if probe.VendorID != 0 {
g.VendorID = fmt.Sprintf("%04x", probe.VendorID)
}
if probe.DeviceID != 0 {
g.DeviceID = fmt.Sprintf("%04x", probe.DeviceID)
}
g.VulkanVersion = FormatVulkanVersion(probe.APIVersion)
// VkPhysicalDeviceType: 1=INTEGRATED_GPU, 2=DISCRETE_GPU, 3=VIRTUAL_GPU, 4=CPU
switch probe.DeviceType {
case 4:
g.Kind = GPUVirtual
g.Compat = GPUCompatUnsupported
g.CompatCode = "vulkan_cpu"
g.CompatDetail = g.Name
return g
case 3:
g.Kind = GPUVirtual
case 2:
if g.Kind != GPUVirtual {
g.Kind = GPUDiscrete
}
case 1:
if g.Kind != GPUVirtual {
g.Kind = GPUIntegrated
}
}
}
if g.Kind == GPUVirtual {
g.Compat = GPUCompatUnsupported
g.CompatCode = "virtual_adapter"
return g
}
if probe != nil {
if !vulkanSupports13(probe.APIVersion) {
g.Compat = GPUCompatUnsupported
g.CompatCode = "vulkan_too_old"
g.CompatDetail = g.VulkanVersion
return g
}
if len(probe.Extensions) > 0 {
var missing []string
for _, ext := range RequiredVulkanExtensions {
if !probe.Extensions[ext] {
missing = append(missing, ext)
}
}
if len(missing) > 0 {
g.Compat = GPUCompatUnsupported
g.CompatCode = "vulkan_missing_extensions"
g.CompatDetail = strings.Join(missing, ", ")
return g
}
}
}
if isAMDLegacyVegaOrPolaris(g.VendorID, g.DeviceID, g.Name) {
g.Compat = GPUCompatWarn
g.CompatCode = "amd_legacy_win_driver"
g.CompatDetail = "steelbrain/reims-vgpu#181"
return g
}
if isAMDRDNA2DeviceLost(g.VendorID, g.DeviceID) {
g.Compat = GPUCompatWarn
g.CompatCode = "amd_rdna2_device_lost"
g.CompatDetail = "steelbrain/reims-vgpu#173"
return g
}
if probe != nil {
g.Compat = GPUCompatSupported
g.CompatCode = "ok"
return g
}
g.Compat = GPUCompatUnknown
g.CompatCode = "vulkan_unprobed"
return g
}
// MergeRegistryAndVulkanGPUs reconciles Windows display-class registry entries
// with live Vulkan physical devices from vulkan-1.dll, preserving ICD paths and
// driver versions while enriching each GPU with real Vulkan 1.3 capability and
// compatibility grades.
func MergeRegistryAndVulkanGPUs(regGPUs []GPUInfo, vkProbes []VulkanDeviceProbe) []GPUInfo {
used := make([]bool, len(vkProbes))
matchProbe := func(g GPUInfo) *VulkanDeviceProbe {
// 1. Exact PCI vendor:device match.
if g.VendorID != "" && g.DeviceID != "" {
wantVen := strings.ToLower(g.VendorID)
wantDev := strings.ToLower(g.DeviceID)
for i := range vkProbes {
if used[i] {
continue
}
if fmt.Sprintf("%04x", vkProbes[i].VendorID) == wantVen &&
fmt.Sprintf("%04x", vkProbes[i].DeviceID) == wantDev {
used[i] = true
return &vkProbes[i]
}
}
}
// 2. Case-insensitive name fallback only when PCI IDs are absent on either side;
// never cross-match two different PCI devices that share a generic marketing name
// (e.g. "AMD Radeon(TM) Graphics").
lowName := strings.ToLower(strings.TrimSpace(g.Name))
if lowName != "" {
for i := range vkProbes {
if used[i] {
continue
}
if g.VendorID != "" && g.DeviceID != "" &&
vkProbes[i].VendorID != 0 && vkProbes[i].DeviceID != 0 {
continue
}
pName := strings.ToLower(strings.TrimSpace(vkProbes[i].Name))
if pName == lowName || strings.Contains(pName, lowName) || strings.Contains(lowName, pName) {
used[i] = true
return &vkProbes[i]
}
}
}
return nil
}
out := make([]GPUInfo, 0, len(regGPUs)+len(vkProbes))
for _, g := range regGPUs {
p := matchProbe(g)
out = append(out, EvaluateGPUCompat(g, p))
}
// Include any non-CPU Vulkan GPU enumerated by vulkan-1.dll that wasn't
// listed under the display-class registry (e.g. registered only under
// HKLM\SOFTWARE\Khronos\Vulkan\Drivers).
for i := range vkProbes {
if used[i] || vkProbes[i].DeviceType == 4 {
continue
}
p := vkProbes[i]
base := GPUInfo{
Name: p.Name,
Kind: ClassifyGPU(p.Name),
}
out = append(out, EvaluateGPUCompat(base, &p))
}
return out
}