Files
Reims-Go/internal/host/vulkan_windows.go

234 lines
7.0 KiB
Go

//go:build windows
package host
import (
"bytes"
"encoding/binary"
"fmt"
"syscall"
"unsafe"
)
const (
vkSuccess = 0
vkIncomplete = 5
vkErrorIncompatibleDriver = -9
vkStructureTypeAppInfo = 0
vkStructureTypeInstCreate = 1
vkStructureTypePhysicalDeviceProps2 = 1000059001
vkStructureTypePhysicalDeviceIDProps = 1000071004
vkAPIVersion10 = (1 << 22)
vkAPIVersion11 = (1 << 22) | (1 << 12)
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
}
// vkPhysicalDeviceIDProperties matches VkPhysicalDeviceIDProperties on x86-64 Windows (64 bytes).
type vkPhysicalDeviceIDProperties struct {
sType uint32
_ uint32
pNext uintptr
deviceUUID [16]byte
driverUUID [16]byte
deviceLUID [8]byte
deviceNodeMask uint32
deviceLUIDValid 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. An empty list with no error means enumeration succeeded.
func probeVulkanDevices() ([]VulkanDeviceProbe, error) {
dll := syscall.NewLazyDLL("vulkan-1.dll")
if err := dll.Load(); err != nil {
return nil, err
}
pCreateInst := dll.NewProc("vkCreateInstance")
pDestroyInst := dll.NewProc("vkDestroyInstance")
pEnumPhys := dll.NewProc("vkEnumeratePhysicalDevices")
pGetProps := dll.NewProc("vkGetPhysicalDeviceProperties")
pGetProps2 := dll.NewProc("vkGetPhysicalDeviceProperties2")
pEnumExts := dll.NewProc("vkEnumerateDeviceExtensionProperties")
for _, proc := range []*syscall.LazyProc{pCreateInst, pDestroyInst, pEnumPhys, pGetProps, pEnumExts} {
if err := proc.Find(); err != nil {
return nil, err
}
}
if err := pGetProps2.Find(); err != nil {
pGetProps2 = 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 {
return nil, vulkanResultError("vkCreateInstance", int32(res))
}
if instance == 0 {
return nil, fmt.Errorf("vkCreateInstance: VK_NULL_HANDLE")
}
defer pDestroyInst.Call(instance, 0)
var count uint32
res, _, _ = pEnumPhys.Call(instance, uintptr(unsafe.Pointer(&count)), 0)
if int32(res) != vkSuccess && int32(res) != vkIncomplete {
return nil, vulkanResultError("vkEnumeratePhysicalDevices", int32(res))
}
if count == 0 {
return nil, 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, vulkanResultError("vkEnumeratePhysicalDevices", int32(res))
}
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])))
apiVersion := binary.LittleEndian.Uint32(propsBuf[0:4])
probe := VulkanDeviceProbe{
APIVersion: apiVersion,
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),
}
probe.DeviceUUID, probe.DeviceLUID = probeVulkanDeviceIDs(pGetProps2, pd, appInfo.apiVersion, apiVersion)
out = append(out, probe)
}
return out, nil
}
func vulkanResultError(call string, result int32) error {
if result == vkErrorIncompatibleDriver {
return fmt.Errorf("%s: VK_ERROR_INCOMPATIBLE_DRIVER (%d)", call, result)
}
return fmt.Errorf("%s: VkResult %d", call, result)
}
func probeVulkanDeviceIDs(pGetProps2 *syscall.LazyProc, pd uintptr, instanceAPIVersion, deviceAPIVersion uint32) (uuid, luid string) {
if pGetProps2 == nil || instanceAPIVersion < vkAPIVersion11 || deviceAPIVersion < vkAPIVersion11 {
return "", ""
}
idProps := vkPhysicalDeviceIDProperties{
sType: vkStructureTypePhysicalDeviceIDProps,
}
var props2Buf [2048]byte
binary.LittleEndian.PutUint32(props2Buf[0:4], vkStructureTypePhysicalDeviceProps2)
binary.LittleEndian.PutUint64(props2Buf[8:16], uint64(uintptr(unsafe.Pointer(&idProps))))
pGetProps2.Call(pd, uintptr(unsafe.Pointer(&props2Buf[0])))
for _, b := range idProps.deviceUUID {
if b != 0 {
uuid = fmt.Sprintf("%x", idProps.deviceUUID)
break
}
}
if idProps.deviceLUIDValid != 0 {
luid = fmt.Sprintf("%x:%x", idProps.deviceLUID, idProps.deviceNodeMask)
}
return uuid, luid
}
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)
}