1//go:build linux
2
3package vkdevice
4
5import (
6 "encoding/binary"
7 "os"
8 "testing"
9
10 "golang.org/x/sys/unix"
11)
12
13func TestDeviceIdentity(t *testing.T) {
14 for _, tc := range []struct{ major, minor uint32 }{{0, 0}, {226, 128}, {226, 0}, {4095, 1 << 20}, {2, 255}} {
15 // Linux makedev as used by x/sys/unix and stat on /dev/dri.
16 raw := unix.Mkdev(tc.major, tc.minor)
17 b := make([]byte, 8)
18 binary.NativeEndian.PutUint64(b, raw)
19 got, err := DecodeDevice(b)
20 if err != nil || got != (Identity{tc.major, tc.minor}) || got != DeviceIdentity(raw) {
21 t.Fatalf("dev_t %d:%d -> %+v: %v", tc.major, tc.minor, got, err)
22 }
23 }
24 if _, err := DecodeDevice([]byte{1}); err == nil {
25 t.Fatal("accepted truncated dev_t")
26 }
27}
28
29func TestNoCrossGPUFallback(t *testing.T) {
30 main := Identity{226, 128}
31 if Matches(main, Identity{226, 0}, Identity{226, 129}, true, true) {
32 t.Fatal("matched different GPU")
33 }
34 if !Matches(main, main, Identity{226, 129}, true, true) {
35 t.Fatal("did not match primary")
36 }
37 if !Matches(main, Identity{226, 0}, main, true, true) {
38 t.Fatal("did not match render")
39 }
40 if Matches(main, main, main, false, false) {
41 t.Fatal("matched device without DRM capability")
42 }
43}
44
45func TestVulkanDeviceHardware(t *testing.T) {
46 path := os.Getenv("NEFERGUI_RENDER_NODE")
47 if path == "" {
48 t.Skip("opt in with NEFERGUI_RENDER_NODE=/dev/dri/renderD... (matching compositor main_device)")
49 }
50 var stat unix.Stat_t
51 if err := unix.Stat(path, &stat); err != nil {
52 t.Fatal(err)
53 }
54 d, err := Open(DeviceIdentity(uint64(stat.Rdev)))
55 if err != nil {
56 t.Fatal(err)
57 }
58 defer d.Close()
59 t.Logf("selected main_device %d:%d -> Vulkan render node %d:%d queue family %d", unix.Major(uint64(stat.Rdev)), unix.Minor(uint64(stat.Rdev)), d.Render.Major, d.Render.Minor, d.QueueFamily)
60 if d.Render != DeviceIdentity(uint64(stat.Rdev)) {
61 t.Fatalf("render node mismatch: %+v", d.Render)
62 }
63}