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 DeviceUUID string // optional physical device UUID from VkPhysicalDeviceIDProperties DeviceLUID string // optional adapter LUID:nodeMask from VkPhysicalDeviceIDProperties } 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 } func probeRank(p VulkanDeviceProbe) (int, uint32, int) { reqCount := 0 for _, ext := range RequiredVulkanExtensions { if p.Extensions[ext] { reqCount++ } } return reqCount, p.APIVersion, len(p.Extensions) } func betterVulkanProbe(a, b VulkanDeviceProbe) bool { ar, av, ae := probeRank(a) br, bv, be := probeRank(b) if ar != br { return ar > br } if av != bv { return av > bv } return ae > be } func sameStrongVulkanIdentity(a, b VulkanDeviceProbe) bool { if a.DeviceUUID != "" && b.DeviceUUID != "" && a.DeviceUUID == b.DeviceUUID { return true } if a.DeviceLUID != "" && b.DeviceLUID != "" && a.DeviceLUID == b.DeviceLUID { return true } return a.DeviceType == 1 && b.DeviceType == 1 && a.VendorID != 0 && a.DeviceID != 0 && a.VendorID == b.VendorID && a.DeviceID == b.DeviceID } func sameVulkanDuplicate(a, b VulkanDeviceProbe) bool { if sameStrongVulkanIdentity(a, b) { return true } if a.DeviceUUID != "" && b.DeviceUUID != "" && a.DeviceUUID != b.DeviceUUID { return false } if a.DeviceLUID != "" && b.DeviceLUID != "" && a.DeviceLUID != b.DeviceLUID { return false } return a.VendorID != 0 && a.DeviceID != 0 && a.VendorID == b.VendorID && a.DeviceID == b.DeviceID && strings.EqualFold(strings.TrimSpace(a.Name), strings.TrimSpace(b.Name)) } func probeMatchesExistingGPU(p VulkanDeviceProbe, out []GPUInfo, outUUIDs, outLUIDs []string) bool { pVen, pDev := "", "" if p.VendorID != 0 { pVen = fmt.Sprintf("%04x", p.VendorID) } if p.DeviceID != 0 { pDev = fmt.Sprintf("%04x", p.DeviceID) } pName := strings.ToLower(strings.TrimSpace(p.Name)) for i, existing := range out { if p.DeviceUUID != "" && outUUIDs[i] != "" && p.DeviceUUID == outUUIDs[i] { return true } if p.DeviceLUID != "" && outLUIDs[i] != "" && p.DeviceLUID == outLUIDs[i] { return true } eVen := strings.ToLower(strings.TrimSpace(existing.VendorID)) eDev := strings.ToLower(strings.TrimSpace(existing.DeviceID)) samePCI := pVen != "" && pDev != "" && eVen != "" && eDev != "" && pVen == eVen && pDev == eDev if samePCI && (p.DeviceType == 1 || existing.Kind == GPUIntegrated) { return true } if (p.DeviceUUID != "" && outUUIDs[i] != "" && p.DeviceUUID != outUUIDs[i]) || (p.DeviceLUID != "" && outLUIDs[i] != "" && p.DeviceLUID != outLUIDs[i]) { continue } if pVen != "" && pDev != "" && eVen != "" && eDev != "" { if samePCI { return true } continue } if pName != "" && strings.ToLower(strings.TrimSpace(existing.Name)) == pName { return true } } return false } // 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)) markUsed := func(idx int) { used[idx] = true for j := range vkProbes { if !used[j] && sameStrongVulkanIdentity(vkProbes[idx], vkProbes[j]) { used[j] = true } } } matchProbe := func(g GPUInfo) *VulkanDeviceProbe { if g.VendorID != "" && g.DeviceID != "" { wantVen := strings.ToLower(g.VendorID) wantDev := strings.ToLower(g.DeviceID) best := -1 for i := range vkProbes { if used[i] { continue } if fmt.Sprintf("%04x", vkProbes[i].VendorID) == wantVen && fmt.Sprintf("%04x", vkProbes[i].DeviceID) == wantDev { if best < 0 || betterVulkanProbe(vkProbes[i], vkProbes[best]) { best = i } } } if best >= 0 { markUsed(best) return &vkProbes[best] } } lowName := strings.ToLower(strings.TrimSpace(g.Name)) if lowName != "" { best := -1 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) { if best < 0 || betterVulkanProbe(vkProbes[i], vkProbes[best]) { best = i } } } if best >= 0 { markUsed(best) return &vkProbes[best] } } return nil } out := make([]GPUInfo, 0, len(regGPUs)+len(vkProbes)) outUUIDs := make([]string, 0, len(regGPUs)+len(vkProbes)) outLUIDs := make([]string, 0, len(regGPUs)+len(vkProbes)) for _, g := range regGPUs { p := matchProbe(g) out = append(out, EvaluateGPUCompat(g, p)) if p != nil { outUUIDs = append(outUUIDs, p.DeviceUUID) outLUIDs = append(outLUIDs, p.DeviceLUID) } else { outUUIDs = append(outUUIDs, "") outLUIDs = append(outLUIDs, "") } } // Include any non-CPU Vulkan GPU enumerated by vulkan-1.dll that wasn't // listed under the display-class registry, skipping duplicate ICD entries // for a physical device already present in out while keeping distinct // identical-model discrete GPUs (different DeviceUUID/DeviceLUID) separate. for i := range vkProbes { if used[i] || vkProbes[i].DeviceType == 4 { continue } p := vkProbes[i] if probeMatchesExistingGPU(p, out, outUUIDs, outLUIDs) { continue } for j := i + 1; j < len(vkProbes); j++ { if used[j] || vkProbes[j].DeviceType == 4 { continue } if sameVulkanDuplicate(p, vkProbes[j]) { used[j] = true if betterVulkanProbe(vkProbes[j], p) { p = vkProbes[j] } } } base := GPUInfo{ Name: p.Name, Kind: ClassifyGPU(p.Name), } out = append(out, EvaluateGPUCompat(base, &p)) outUUIDs = append(outUUIDs, p.DeviceUUID) outLUIDs = append(outLUIDs, p.DeviceLUID) } return out }