154 lines
4.2 KiB
Go
154 lines
4.2 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 info, err := os.Stat(path); err == nil && info.Mode().IsRegular() {
|
|
if manifest != "" {
|
|
return "" // Multiple driver implementations in this package.
|
|
}
|
|
manifest = path
|
|
}
|
|
}
|
|
return manifest
|
|
}
|
|
|
|
type adapterManifest struct {
|
|
id, path string
|
|
}
|
|
|
|
func unregisteredDriverManifests() []adapterManifest {
|
|
guid, _ := windows.GUIDFromString(displayClass)
|
|
devs, err := windows.SetupDiGetClassDevsEx(&guid, "", 0, windows.DIGCF_PRESENT, 0, "")
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
defer devs.Close()
|
|
var candidates []adapterManifest
|
|
for i := 0; ; i++ {
|
|
dev, err := devs.EnumDeviceInfo(i)
|
|
if err != nil {
|
|
break
|
|
}
|
|
id, _ := devs.DeviceInstanceID(dev)
|
|
vendor, device := ParsePCIIDs(id)
|
|
if device == "" || !strings.HasPrefix(strings.ToUpper(id), `PCI\`) {
|
|
continue
|
|
}
|
|
value, _ := devs.DeviceRegistryProperty(dev, windows.SPDRP_DRIVER)
|
|
driverKey, ok := value.(string)
|
|
if !ok || driverKey == "" {
|
|
continue
|
|
}
|
|
sub := `SYSTEM\CurrentControlSet\Control\Class\` + driverKey
|
|
if path, _ := regReadString(hkeyLocalMachine, sub, "VulkanDriverName"); path != "" {
|
|
if info, err := os.Stat(path); err == nil && info.Mode().IsRegular() {
|
|
continue
|
|
}
|
|
}
|
|
inf, _ := regReadString(hkeyLocalMachine, sub, "InfPath")
|
|
if path := driverStoreManifest(inf, vendor); path != "" {
|
|
candidates = append(candidates, adapterManifest{vendor + ":" + device, 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() }
|