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.
282 lines
7.5 KiB
Go
282 lines
7.5 KiB
Go
package host
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
const displayClass = "{4d36e968-e325-11ce-bfc1-08002be10318}"
|
|
|
|
var infStoreLocation = windows.NewLazySystemDLL("setupapi.dll").NewProc("SetupGetInfDriverStoreLocationW")
|
|
|
|
// Look only in the installed INF's package. DriverStore can also contain
|
|
// older versions of the same vendor's driver.
|
|
func driverStoreManifest(inf, vendor string) string {
|
|
if !filepath.IsLocal(inf) || filepath.Base(inf) != inf || !strings.EqualFold(filepath.Ext(inf), ".inf") {
|
|
return ""
|
|
}
|
|
var names []string
|
|
switch vendor {
|
|
case "10de":
|
|
names = []string{"nv-vk64.json"}
|
|
case "8086":
|
|
names = []string{"igvk64.json"}
|
|
case "1002":
|
|
names = []string{"amd-vulkan64.json", "amdvlk64.json"}
|
|
default:
|
|
return ""
|
|
}
|
|
input, err := windows.UTF16PtrFromString(inf)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
var size uint32
|
|
infStoreLocation.Call(uintptr(unsafe.Pointer(input)), 0, 0, 0, 0, uintptr(unsafe.Pointer(&size)))
|
|
if size == 0 {
|
|
return ""
|
|
}
|
|
buf := make([]uint16, size)
|
|
ok, _, _ := infStoreLocation.Call(uintptr(unsafe.Pointer(input)), 0, 0, uintptr(unsafe.Pointer(&buf[0])), uintptr(size), 0)
|
|
if ok == 0 {
|
|
return ""
|
|
}
|
|
dir := filepath.Dir(windows.UTF16ToString(buf))
|
|
var manifest string
|
|
for _, name := range names {
|
|
path := filepath.Join(dir, name)
|
|
if isRegularFile(path) {
|
|
if manifest != "" {
|
|
return "" // Multiple driver implementations in this package.
|
|
}
|
|
manifest = path
|
|
}
|
|
}
|
|
return manifest
|
|
}
|
|
|
|
func isRegularFile(path string) bool {
|
|
info, err := os.Stat(path)
|
|
return err == nil && info.Mode().IsRegular()
|
|
}
|
|
|
|
type displayAdapterRecord struct {
|
|
instanceID string
|
|
driverKey string
|
|
status uint32
|
|
statusOK bool
|
|
}
|
|
|
|
type activeDisplayAdapter struct {
|
|
instanceID string
|
|
driverKey string
|
|
classKey string
|
|
vendorID string
|
|
deviceID string
|
|
}
|
|
|
|
// isActiveDevNodeStatus reports whether a Windows PnP devnode is started,
|
|
// problem-free, and not pending removal. Unplugged (phantom) GPUs are excluded
|
|
// by DIGCF_PRESENT; disabled or failed adapters (e.g. Code 22 / Code 43) lack
|
|
// DN_STARTED and/or carry DN_HAS_PROBLEM.
|
|
func isActiveDevNodeStatus(status uint32) bool {
|
|
return status&windows.DN_STARTED != 0 &&
|
|
status&windows.DN_HAS_PROBLEM == 0 &&
|
|
status&windows.DN_WILL_BE_REMOVED == 0
|
|
}
|
|
|
|
func enumeratePresentDisplayAdapters() []displayAdapterRecord {
|
|
guid, err := windows.GUIDFromString(displayClass)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
devs, err := windows.SetupDiGetClassDevsEx(&guid, "", 0, windows.DIGCF_PRESENT, 0, "")
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
defer devs.Close()
|
|
|
|
var records []displayAdapterRecord
|
|
for i := 0; ; i++ {
|
|
dev, err := devs.EnumDeviceInfo(i)
|
|
if err != nil {
|
|
break
|
|
}
|
|
var status, problem uint32
|
|
statusErr := windows.CM_Get_DevNode_Status(&status, &problem, dev.DevInst, 0)
|
|
id, _ := devs.DeviceInstanceID(dev)
|
|
val, _ := devs.DeviceRegistryProperty(dev, windows.SPDRP_DRIVER)
|
|
driverKey, _ := val.(string)
|
|
records = append(records, displayAdapterRecord{
|
|
instanceID: id,
|
|
driverKey: driverKey,
|
|
status: status,
|
|
statusOK: statusErr == nil,
|
|
})
|
|
}
|
|
return records
|
|
}
|
|
|
|
func filterActiveDisplayAdapters(records []displayAdapterRecord) []activeDisplayAdapter {
|
|
seen := make(map[string]bool, len(records))
|
|
out := make([]activeDisplayAdapter, 0, len(records))
|
|
for _, r := range records {
|
|
if !r.statusOK || !isActiveDevNodeStatus(r.status) {
|
|
continue
|
|
}
|
|
driverKey := strings.Trim(strings.TrimSpace(r.driverKey), `\`)
|
|
if driverKey == "" || strings.Contains(driverKey, "..") {
|
|
continue
|
|
}
|
|
lowKey := strings.ToLower(driverKey)
|
|
if seen[lowKey] {
|
|
continue
|
|
}
|
|
seen[lowKey] = true
|
|
vendorID, deviceID := ParsePCIIDs(r.instanceID)
|
|
out = append(out, activeDisplayAdapter{
|
|
instanceID: r.instanceID,
|
|
driverKey: driverKey,
|
|
classKey: `SYSTEM\CurrentControlSet\Control\Class\` + driverKey,
|
|
vendorID: vendorID,
|
|
deviceID: deviceID,
|
|
})
|
|
}
|
|
slices.SortFunc(out, func(a, b activeDisplayAdapter) int {
|
|
return strings.Compare(strings.ToLower(a.driverKey), strings.ToLower(b.driverKey))
|
|
})
|
|
return out
|
|
}
|
|
|
|
func activeDisplayAdapters() []activeDisplayAdapter {
|
|
return filterActiveDisplayAdapters(enumeratePresentDisplayAdapters())
|
|
}
|
|
|
|
func readRegistryGPUs(
|
|
adapters []activeDisplayAdapter,
|
|
readString func(subKey, valueName string) (string, bool),
|
|
regularFile func(path string) bool,
|
|
) []GPUInfo {
|
|
var regGPUs []GPUInfo
|
|
for _, a := range adapters {
|
|
desc, okDesc := readString(a.classKey, "DriverDesc")
|
|
if !okDesc || desc == "" {
|
|
continue
|
|
}
|
|
icd, _ := readString(a.classKey, "VulkanDriverName")
|
|
if icd == "" || !regularFile(icd) {
|
|
continue
|
|
}
|
|
vendorID, deviceID := a.vendorID, a.deviceID
|
|
if vendorID == "" || deviceID == "" {
|
|
matchID, _ := readString(a.classKey, "MatchingDeviceId")
|
|
v2, d2 := ParsePCIIDs(matchID)
|
|
if vendorID == "" {
|
|
vendorID = v2
|
|
}
|
|
if deviceID == "" {
|
|
deviceID = d2
|
|
}
|
|
}
|
|
drvVer, _ := readString(a.classKey, "DriverVersion")
|
|
regGPUs = append(regGPUs, GPUInfo{
|
|
Name: desc,
|
|
VulkanICD: icd,
|
|
Kind: ClassifyGPU(desc),
|
|
VendorID: vendorID,
|
|
DeviceID: deviceID,
|
|
DriverVersion: drvVer,
|
|
})
|
|
}
|
|
return regGPUs
|
|
}
|
|
|
|
func probeActiveRegistryGPUs() []GPUInfo {
|
|
return readRegistryGPUs(
|
|
activeDisplayAdapters(),
|
|
func(subKey, valueName string) (string, bool) {
|
|
return regReadString(hkeyLocalMachine, subKey, valueName)
|
|
},
|
|
isRegularFile,
|
|
)
|
|
}
|
|
|
|
type adapterManifest struct {
|
|
id, path string
|
|
}
|
|
|
|
func unregisteredDriverManifests() []adapterManifest {
|
|
var candidates []adapterManifest
|
|
for _, a := range activeDisplayAdapters() {
|
|
if a.vendorID == "" || a.deviceID == "" || !strings.HasPrefix(strings.ToUpper(a.instanceID), `PCI\`) {
|
|
continue
|
|
}
|
|
if path, _ := regReadString(hkeyLocalMachine, a.classKey, "VulkanDriverName"); path != "" {
|
|
if isRegularFile(path) {
|
|
continue
|
|
}
|
|
}
|
|
inf, _ := regReadString(hkeyLocalMachine, a.classKey, "InfPath")
|
|
if path := driverStoreManifest(inf, a.vendorID); path != "" {
|
|
candidates = append(candidates, adapterManifest{a.vendorID + ":" + a.deviceID, path})
|
|
}
|
|
}
|
|
return candidates
|
|
}
|
|
|
|
// Only bind a PCI ID to a manifest when its active driver package is unambiguous
|
|
// and the loader has not already enumerated that GPU through another source.
|
|
func missingDriverPackages(candidates []adapterManifest, probes []VulkanDeviceProbe) map[string]string {
|
|
packages := make(map[string]string)
|
|
for _, c := range candidates {
|
|
if previous, seen := packages[c.id]; seen && previous != c.path {
|
|
packages[c.id] = ""
|
|
} else {
|
|
packages[c.id] = c.path
|
|
}
|
|
}
|
|
for _, p := range probes {
|
|
delete(packages, fmt.Sprintf("%04x:%04x", p.VendorID, p.DeviceID))
|
|
}
|
|
for id, path := range packages {
|
|
if path == "" {
|
|
delete(packages, id)
|
|
}
|
|
}
|
|
return packages
|
|
}
|
|
|
|
var recoveredManifests = sync.OnceValue(func() map[string]string {
|
|
for _, key := range []string{"VK_DRIVER_FILES", "VK_ICD_FILENAMES", "VK_ADD_DRIVER_FILES"} {
|
|
if _, set := os.LookupEnv(key); set {
|
|
return nil
|
|
}
|
|
}
|
|
candidates := unregisteredDriverManifests()
|
|
if len(candidates) == 0 {
|
|
return nil
|
|
}
|
|
probes, _ := probeVulkanDevices()
|
|
missing := missingDriverPackages(candidates, probes)
|
|
var paths []string
|
|
for _, path := range missing {
|
|
if !slices.Contains(paths, path) {
|
|
paths = append(paths, path)
|
|
}
|
|
}
|
|
slices.Sort(paths)
|
|
if len(paths) == 0 || os.Setenv("VK_ADD_DRIVER_FILES", strings.Join(paths, ";")) != nil {
|
|
return nil
|
|
}
|
|
return missing
|
|
})
|
|
|
|
// ConfigureVulkanDrivers prepares process-local discovery for runtime children.
|
|
func ConfigureVulkanDrivers() { recoveredManifests() }
|