176 lines
4.8 KiB
Go
176 lines
4.8 KiB
Go
//go:build windows
|
|
|
|
package host
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"syscall"
|
|
"unsafe"
|
|
)
|
|
|
|
const (
|
|
vkSuccess = 0
|
|
vkIncomplete = 5
|
|
vkErrorIncompatibleDriver = -9
|
|
vkStructureTypeAppInfo = 0
|
|
vkStructureTypeInstCreate = 1
|
|
vkAPIVersion10 = (1 << 22)
|
|
vkAPIVersion13 = (1 << 22) | (3 << 12)
|
|
)
|
|
|
|
// vkApplicationInfo matches VkApplicationInfo on x86-64 Windows (48 bytes).
|
|
type vkApplicationInfo struct {
|
|
sType uint32
|
|
pNext uintptr
|
|
pApplicationName uintptr
|
|
applicationVersion uint32
|
|
pEngineName uintptr
|
|
engineVersion uint32
|
|
apiVersion uint32
|
|
}
|
|
|
|
// vkInstanceCreateInfo matches VkInstanceCreateInfo on x86-64 Windows (64 bytes).
|
|
type vkInstanceCreateInfo struct {
|
|
sType uint32
|
|
pNext uintptr
|
|
flags uint32
|
|
pApplicationInfo *vkApplicationInfo
|
|
enabledLayerCount uint32
|
|
ppEnabledLayerNames uintptr
|
|
enabledExtensionCount uint32
|
|
ppEnabledExtensionNames uintptr
|
|
}
|
|
|
|
// vkExtensionProperties matches VkExtensionProperties (260 bytes).
|
|
type vkExtensionProperties struct {
|
|
ExtensionName [256]byte
|
|
SpecVersion uint32
|
|
}
|
|
|
|
// probeVulkanDevices loads vulkan-1.dll and enumerates all visible Vulkan
|
|
// physical devices along with their API version, PCI IDs, device type, and
|
|
// advertised device extensions. Returns nil cleanly if the Vulkan loader is
|
|
// absent or refuses instance creation.
|
|
func probeVulkanDevices() []VulkanDeviceProbe {
|
|
dll := syscall.NewLazyDLL("vulkan-1.dll")
|
|
if err := dll.Load(); err != nil {
|
|
return nil
|
|
}
|
|
pCreateInst := dll.NewProc("vkCreateInstance")
|
|
pDestroyInst := dll.NewProc("vkDestroyInstance")
|
|
pEnumPhys := dll.NewProc("vkEnumeratePhysicalDevices")
|
|
pGetProps := dll.NewProc("vkGetPhysicalDeviceProperties")
|
|
pEnumExts := dll.NewProc("vkEnumerateDeviceExtensionProperties")
|
|
if pCreateInst.Find() != nil || pDestroyInst.Find() != nil ||
|
|
pEnumPhys.Find() != nil || pGetProps.Find() != nil || pEnumExts.Find() != nil {
|
|
return nil
|
|
}
|
|
|
|
appName := append([]byte("reims-go"), 0)
|
|
appInfo := vkApplicationInfo{
|
|
sType: vkStructureTypeAppInfo,
|
|
pApplicationName: uintptr(unsafe.Pointer(&appName[0])),
|
|
apiVersion: vkAPIVersion13,
|
|
}
|
|
createInfo := vkInstanceCreateInfo{
|
|
sType: vkStructureTypeInstCreate,
|
|
pApplicationInfo: &appInfo,
|
|
}
|
|
|
|
var instance uintptr
|
|
res, _, _ := pCreateInst.Call(
|
|
uintptr(unsafe.Pointer(&createInfo)),
|
|
0,
|
|
uintptr(unsafe.Pointer(&instance)),
|
|
)
|
|
if int32(res) == vkErrorIncompatibleDriver {
|
|
appInfo.apiVersion = vkAPIVersion10
|
|
res, _, _ = pCreateInst.Call(
|
|
uintptr(unsafe.Pointer(&createInfo)),
|
|
0,
|
|
uintptr(unsafe.Pointer(&instance)),
|
|
)
|
|
}
|
|
if int32(res) != vkSuccess || instance == 0 {
|
|
return nil
|
|
}
|
|
defer pDestroyInst.Call(instance, 0)
|
|
|
|
var count uint32
|
|
res, _, _ = pEnumPhys.Call(instance, uintptr(unsafe.Pointer(&count)), 0)
|
|
if (int32(res) != vkSuccess && int32(res) != vkIncomplete) || count == 0 {
|
|
return nil
|
|
}
|
|
if count > 32 {
|
|
count = 32
|
|
}
|
|
devices := make([]uintptr, count)
|
|
res, _, _ = pEnumPhys.Call(
|
|
instance,
|
|
uintptr(unsafe.Pointer(&count)),
|
|
uintptr(unsafe.Pointer(&devices[0])),
|
|
)
|
|
if int32(res) != vkSuccess && int32(res) != vkIncomplete {
|
|
return nil
|
|
}
|
|
|
|
out := make([]VulkanDeviceProbe, 0, count)
|
|
for i := uint32(0); i < count; i++ {
|
|
pd := devices[i]
|
|
if pd == 0 {
|
|
continue
|
|
}
|
|
// VkPhysicalDeviceProperties is ~824 bytes; use 2048 bytes to stay
|
|
// immune to padding differences while reading fixed header offsets 0..276.
|
|
var propsBuf [2048]byte
|
|
pGetProps.Call(pd, uintptr(unsafe.Pointer(&propsBuf[0])))
|
|
|
|
probe := VulkanDeviceProbe{
|
|
APIVersion: binary.LittleEndian.Uint32(propsBuf[0:4]),
|
|
VendorID: binary.LittleEndian.Uint32(propsBuf[8:12]),
|
|
DeviceID: binary.LittleEndian.Uint32(propsBuf[12:16]),
|
|
DeviceType: binary.LittleEndian.Uint32(propsBuf[16:20]),
|
|
Name: cstrFromBytes(propsBuf[20:276]),
|
|
Extensions: probeVulkanDeviceExtensions(pEnumExts, pd),
|
|
}
|
|
out = append(out, probe)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func probeVulkanDeviceExtensions(pEnumExts *syscall.LazyProc, pd uintptr) map[string]bool {
|
|
var extCount uint32
|
|
res, _, _ := pEnumExts.Call(pd, 0, uintptr(unsafe.Pointer(&extCount)), 0)
|
|
if (int32(res) != vkSuccess && int32(res) != vkIncomplete) || extCount == 0 {
|
|
return nil
|
|
}
|
|
if extCount > 2048 {
|
|
extCount = 2048
|
|
}
|
|
exts := make([]vkExtensionProperties, extCount)
|
|
res, _, _ = pEnumExts.Call(
|
|
pd,
|
|
0,
|
|
uintptr(unsafe.Pointer(&extCount)),
|
|
uintptr(unsafe.Pointer(&exts[0])),
|
|
)
|
|
if int32(res) != vkSuccess && int32(res) != vkIncomplete {
|
|
return nil
|
|
}
|
|
m := make(map[string]bool, extCount)
|
|
for i := uint32(0); i < extCount; i++ {
|
|
if name := cstrFromBytes(exts[i].ExtensionName[:]); name != "" {
|
|
m[name] = true
|
|
}
|
|
}
|
|
return m
|
|
}
|
|
|
|
func cstrFromBytes(b []byte) string {
|
|
if idx := bytes.IndexByte(b, 0); idx >= 0 {
|
|
b = b[:idx]
|
|
}
|
|
return string(b)
|
|
}
|