Skip to content

Commit d6091d7

Browse files
committed
use PCI device for prime
1 parent ad15f4b commit d6091d7

File tree

5 files changed

+28
-30
lines changed

5 files changed

+28
-30
lines changed

cmd/vinegar/vinegar.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"log"
88
"os"
9+
"path"
910
"path/filepath"
1011
"runtime/debug"
1112
"syscall"
@@ -160,7 +161,7 @@ func Sysinfo(pfx *wine.Prefix) {
160161

161162
fmt.Println("* Cards:")
162163
for i, c := range sysinfo.Cards {
163-
fmt.Printf(" * Card %d: %s %s\n", i, c.Driver, c.Path)
164+
fmt.Printf(" * Card %d: %s %s %s\n", i, c.Driver, path.Base(c.Device), c.Path)
164165
}
165166
}
166167

config/cardpick.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package config
22

33
import (
44
"errors"
5+
"path"
56
"strconv"
7+
"strings"
68

79
"github.com/vinegarhq/vinegar/sysinfo"
810
)
@@ -62,10 +64,14 @@ func (b *Binary) pickCard() error {
6264
return ErrNoCardFound
6365
}
6466

67+
c := sysinfo.Cards[idx]
68+
6569
b.Env.Set("MESA_VK_DEVICE_SELECT_FORCE_DEFAULT_DEVICE", "1")
66-
b.Env.Set("DRI_PRIME", strconv.Itoa(idx))
70+
b.Env.Set("DRI_PRIME",
71+
"pci-"+strings.NewReplacer(":", "_", ".", "_").Replace(path.Base(c.Device)),
72+
)
6773

68-
if sysinfo.Cards[idx].Driver == "nvidia" { // Workaround for OpenGL in nvidia GPUs
74+
if c.Driver == "nvidia" { // Workaround for OpenGL in nvidia GPUs
6975
b.Env.Set("__GLX_VENDOR_LIBRARY_NAME", "nvidia")
7076
} else {
7177
b.Env.Set("__GLX_VENDOR_LIBRARY_NAME", "mesa")

config/cardpick_test.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,20 @@ func TestCard(t *testing.T) {
3030
}
3131
}
3232

33-
func TestIntegratedCard(t *testing.T) {
33+
func TestIntegratedAndDiscreteCard(t *testing.T) {
3434
b := Binary{
3535
ForcedGpu: "integrated",
3636
Env: Environment{},
3737
}
3838
sysinfo.Cards = []sysinfo.Card{
3939
{
4040
Driver: "i915",
41+
Device: "0000:01:00.0",
4142
Embedded: true,
4243
},
4344
{
4445
Driver: "nvidia",
46+
Device: "0000:02:00.0",
4547
Embedded: false,
4648
},
4749
}
@@ -50,36 +52,22 @@ func TestIntegratedCard(t *testing.T) {
5052
t.Fatal(err)
5153
}
5254

53-
if v := b.Env["DRI_PRIME"]; v != "0" {
55+
if v := b.Env["DRI_PRIME"]; v != "pci-0000_01_00_0" {
5456
t.Fatal("expected change in integrated prime index")
5557
}
5658

5759
if v := b.Env["__GLX_VENDOR_LIBRARY_NAME"]; v != "mesa" {
5860
t.Fatal("expected glx vendor to be mesa")
5961
}
60-
}
6162

62-
func TestDiscreteCard(t *testing.T) {
63-
b := Binary{
64-
ForcedGpu: "prime-discrete",
65-
Env: Environment{},
66-
}
67-
sysinfo.Cards = []sysinfo.Card{
68-
{
69-
Driver: "i915",
70-
Embedded: true,
71-
},
72-
{
73-
Driver: "nvidia",
74-
Embedded: false,
75-
},
76-
}
63+
b.Env = Environment{}
64+
b.ForcedGpu = "prime-discrete"
7765

7866
if err := b.pickCard(); err != nil {
7967
t.Fatal(err)
8068
}
8169

82-
if v := b.Env["DRI_PRIME"]; v != "1" {
70+
if v := b.Env["DRI_PRIME"]; v != "pci-0000_02_00_0" {
8371
t.Fatal("expected change in discrete prime index")
8472
}
8573

config/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package config
22

33
import (
4-
"os"
54
"errors"
5+
"os"
66
"path/filepath"
77
"testing"
88
)
@@ -40,7 +40,7 @@ func TestGlobal(t *testing.T) {
4040
if c.Player.Env["MEOW"] != "PLAYER" {
4141
t.Error("expected player overrides global env")
4242
}
43-
43+
4444
if c.Studio.Env["MEOW"] != "DEPRECATED" {
4545
t.Error("expected studio applies global env")
4646
}

sysinfo/card.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import (
1010
)
1111

1212
type Card struct {
13-
Path string
14-
Driver string
15-
Embedded bool
13+
Path string // Path to the drm card
14+
Device string // Path to the PCI device
15+
Driver string // Base driver name
16+
Embedded bool // Integrated display
1617
}
1718

1819
const drmPath = "/sys/class/drm"
@@ -25,12 +26,14 @@ func getCards() (cs []Card) {
2526
drmCards, _ := filepath.Glob(path.Join(drmPath, "card[0-9]"))
2627

2728
for _, c := range drmCards {
28-
d, _ := filepath.EvalSymlinks(path.Join(c, "device/driver"))
29-
d = path.Base(d)
29+
dev, _ := filepath.EvalSymlinks(path.Join(c, "device"))
30+
driver, _ := filepath.EvalSymlinks(path.Join(dev, "driver"))
31+
driver = path.Base(driver)
3032

3133
cs = append(cs, Card{
3234
Path: c,
33-
Driver: d,
35+
Device: dev,
36+
Driver: driver,
3437
Embedded: embedded(c),
3538
})
3639
}

0 commit comments

Comments
 (0)