Skip to content

Commit

Permalink
use PCI device for prime
Browse files Browse the repository at this point in the history
  • Loading branch information
apprehensions committed Nov 28, 2023
1 parent ad15f4b commit d6091d7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 30 deletions.
3 changes: 2 additions & 1 deletion cmd/vinegar/vinegar.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log"
"os"
"path"
"path/filepath"
"runtime/debug"
"syscall"
Expand Down Expand Up @@ -160,7 +161,7 @@ func Sysinfo(pfx *wine.Prefix) {

fmt.Println("* Cards:")
for i, c := range sysinfo.Cards {
fmt.Printf(" * Card %d: %s %s\n", i, c.Driver, c.Path)
fmt.Printf(" * Card %d: %s %s %s\n", i, c.Driver, path.Base(c.Device), c.Path)
}
}

Expand Down
10 changes: 8 additions & 2 deletions config/cardpick.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package config

import (
"errors"
"path"
"strconv"
"strings"

"github.com/vinegarhq/vinegar/sysinfo"
)
Expand Down Expand Up @@ -62,10 +64,14 @@ func (b *Binary) pickCard() error {
return ErrNoCardFound
}

c := sysinfo.Cards[idx]

b.Env.Set("MESA_VK_DEVICE_SELECT_FORCE_DEFAULT_DEVICE", "1")
b.Env.Set("DRI_PRIME", strconv.Itoa(idx))
b.Env.Set("DRI_PRIME",
"pci-"+strings.NewReplacer(":", "_", ".", "_").Replace(path.Base(c.Device)),
)

if sysinfo.Cards[idx].Driver == "nvidia" { // Workaround for OpenGL in nvidia GPUs
if c.Driver == "nvidia" { // Workaround for OpenGL in nvidia GPUs
b.Env.Set("__GLX_VENDOR_LIBRARY_NAME", "nvidia")
} else {
b.Env.Set("__GLX_VENDOR_LIBRARY_NAME", "mesa")
Expand Down
26 changes: 7 additions & 19 deletions config/cardpick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@ func TestCard(t *testing.T) {
}
}

func TestIntegratedCard(t *testing.T) {
func TestIntegratedAndDiscreteCard(t *testing.T) {
b := Binary{
ForcedGpu: "integrated",
Env: Environment{},
}
sysinfo.Cards = []sysinfo.Card{
{
Driver: "i915",
Device: "0000:01:00.0",
Embedded: true,
},
{
Driver: "nvidia",
Device: "0000:02:00.0",
Embedded: false,
},
}
Expand All @@ -50,36 +52,22 @@ func TestIntegratedCard(t *testing.T) {
t.Fatal(err)
}

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

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

func TestDiscreteCard(t *testing.T) {
b := Binary{
ForcedGpu: "prime-discrete",
Env: Environment{},
}
sysinfo.Cards = []sysinfo.Card{
{
Driver: "i915",
Embedded: true,
},
{
Driver: "nvidia",
Embedded: false,
},
}
b.Env = Environment{}
b.ForcedGpu = "prime-discrete"

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

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

Expand Down
4 changes: 2 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package config

import (
"os"
"errors"
"os"
"path/filepath"
"testing"
)
Expand Down Expand Up @@ -40,7 +40,7 @@ func TestGlobal(t *testing.T) {
if c.Player.Env["MEOW"] != "PLAYER" {
t.Error("expected player overrides global env")
}

if c.Studio.Env["MEOW"] != "DEPRECATED" {
t.Error("expected studio applies global env")
}
Expand Down
15 changes: 9 additions & 6 deletions sysinfo/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
)

type Card struct {
Path string
Driver string
Embedded bool
Path string // Path to the drm card
Device string // Path to the PCI device
Driver string // Base driver name
Embedded bool // Integrated display
}

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

for _, c := range drmCards {
d, _ := filepath.EvalSymlinks(path.Join(c, "device/driver"))
d = path.Base(d)
dev, _ := filepath.EvalSymlinks(path.Join(c, "device"))
driver, _ := filepath.EvalSymlinks(path.Join(dev, "driver"))
driver = path.Base(driver)

cs = append(cs, Card{
Path: c,
Driver: d,
Device: dev,
Driver: driver,
Embedded: embedded(c),
})
}
Expand Down

0 comments on commit d6091d7

Please sign in to comment.