From 0169b7f782396e2b64109447534fb10f5c197463 Mon Sep 17 00:00:00 2001 From: Jrelvas <55360900+Noted-Jrelvas@users.noreply.github.com> Date: Tue, 24 Oct 2023 17:28:27 +0100 Subject: [PATCH] Update cardpick code for suggestions made --- internal/config/cardpick.go | 20 ++++++++++---------- internal/config/env.go | 9 +++++---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/internal/config/cardpick.go b/internal/config/cardpick.go index a68d42c5..4e1b6b57 100644 --- a/internal/config/cardpick.go +++ b/internal/config/cardpick.go @@ -25,7 +25,7 @@ func prime(vk bool) (bool, error) { //Don't mess with devices that have more than two cards. if len(sysinfo.Cards) > 2 { //OpenGL cannot choose the right card properly. Prompt user to define it themselves - if !isVulkan { + if !vk { return false, fmt.Errorf("opengl cannot select the right gpu. gpus detected: %d", len(sysinfo.Cards)) } log.Printf("System has %d cards. Skipping prime logic and leaving card selection up to Vulkan.", len(sysinfo.Cards)) @@ -42,16 +42,16 @@ func pickCard(opt string, env Environment, isVulkan bool) error { var cIndex int var indexStr string - prime := false + usePrime := false switch opt { //Handle PRIME options case "integrated": cIndex = 0 - prime = true + usePrime = true case "prime-discrete": cIndex = 1 - prime = true + usePrime = true //Skip pickCard logic option case "none": return nil @@ -71,8 +71,8 @@ func pickCard(opt string, env Environment, isVulkan bool) error { indexStr = strconv.Itoa(cIndex) //PRIME Validation - if prime { - allowed, err := primeIsAllowed(isVulkan) + if usePrime { + allowed, err := prime(isVulkan) if err != nil { return err } @@ -87,13 +87,13 @@ func pickCard(opt string, env Environment, isVulkan bool) error { c := sysinfo.Cards[cIndex] - env.SetIfUndefined("MESA_VK_DEVICE_SELECT_FORCE_DEFAULT_DEVICE", "1") - env.SetIfUndefined("DRI_PRIME", indexStr) + env.Set("MESA_VK_DEVICE_SELECT_FORCE_DEFAULT_DEVICE", "1") + env.Set("DRI_PRIME", indexStr) if strings.HasSuffix(c.Driver, "nvidia") { //Workaround for OpenGL in nvidia GPUs - env.SetIfUndefined("__GLX_VENDOR_LIBRARY_NAME", "nvidia") + env.Set("__GLX_VENDOR_LIBRARY_NAME", "nvidia") } else { - env.SetIfUndefined("__GLX_VENDOR_LIBRARY_NAME", "mesa") + env.Set("__GLX_VENDOR_LIBRARY_NAME", "mesa") } log.Printf("Chose card %s (%s).", c.Path, indexStr) diff --git a/internal/config/env.go b/internal/config/env.go index 98912bc5..6f8b6396 100644 --- a/internal/config/env.go +++ b/internal/config/env.go @@ -1,7 +1,6 @@ package config import ( - "log" "os" ) @@ -9,11 +8,13 @@ type Environment map[string]string // Set will only set the given environment key and value if it isn't already set // within the Environment. -func (e *Environment) Set(k string, v string) { - if _, ok := e[k]; ok { +func (e *Environment) Set(key, value string) { + env := *e + + if _, ok := env[key]; ok { return } - e[k] = v + env[key] = value } func (e *Environment) Setenv() {