From 30fe7e6e714e6727cc422b678e7dbf3c357ed449 Mon Sep 17 00:00:00 2001 From: Ash <101582426+ashuntu@users.noreply.github.com> Date: Tue, 10 Jan 2023 15:59:23 -0600 Subject: [PATCH 01/13] Added python script to find env variables for gpu --- src/gpu | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/gpu diff --git a/src/gpu b/src/gpu new file mode 100644 index 0000000..26b103b --- /dev/null +++ b/src/gpu @@ -0,0 +1,41 @@ +#!/usr/bin/python3 + +import os + +NVIDIA_ENV = "__NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia __VK_LAYER_NV_optimus=NVIDIA_only" + +def main(): + lshw = os.popen("lshw -numeric -c display").read() + + gpus = {} + gpu_text = lshw.split("*-display") + env_string = "" + + for text in gpu_text: + data_arr = [t.strip() for t in text.split("\n")] + data = {} + for d in data_arr: + if ": " in d: + s = d.split(": ") + data[s[0].replace(" ", "_")] = s[1] + + if data: + gpus[data["physical_id"]] = data + pci = data["bus_info"]\ + .replace("@", "-")\ + .replace(":", "_")\ + .replace(".", "_") + + if "nvidia" in data["vendor"].lower(): + env_string = f"DRI_PRIME={pci} {NVIDIA_ENV} " + break + + elif "amd" in data["vendor"].lower(): + env_string = f"DRI_PRIME={pci} " + break + + print(env_string) + + +if __name__ == "__main__": + main() From df1a255bc1a1760f54be574ec1b7b3e7d12dbfa7 Mon Sep 17 00:00:00 2001 From: Ash <101582426+ashuntu@users.noreply.github.com> Date: Wed, 11 Jan 2023 11:42:49 -0600 Subject: [PATCH 02/13] Simplified parsing --- src/gpu | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/src/gpu b/src/gpu index 26b103b..636d5a3 100644 --- a/src/gpu +++ b/src/gpu @@ -1,38 +1,28 @@ #!/usr/bin/python3 +import json import os NVIDIA_ENV = "__NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia __VK_LAYER_NV_optimus=NVIDIA_only" def main(): - lshw = os.popen("lshw -numeric -c display").read() - - gpus = {} - gpu_text = lshw.split("*-display") + lshw = os.popen("lshw -json -numeric -c display 2> /dev/null").read() + gpus = json.loads(lshw) env_string = "" - for text in gpu_text: - data_arr = [t.strip() for t in text.split("\n")] - data = {} - for d in data_arr: - if ": " in d: - s = d.split(": ") - data[s[0].replace(" ", "_")] = s[1] - - if data: - gpus[data["physical_id"]] = data - pci = data["bus_info"]\ - .replace("@", "-")\ - .replace(":", "_")\ - .replace(".", "_") - - if "nvidia" in data["vendor"].lower(): - env_string = f"DRI_PRIME={pci} {NVIDIA_ENV} " - break - - elif "amd" in data["vendor"].lower(): - env_string = f"DRI_PRIME={pci} " - break + for gpu in gpus: + pci = gpu["businfo"]\ + .replace("@", "-")\ + .replace(":", "_")\ + .replace(".", "_") + + if "nvidia" in gpu["vendor"].lower(): + env_string = f"DRI_PRIME={pci} {NVIDIA_ENV} " + break + + elif "amd" in gpu["vendor"].lower(): + env_string = f"DRI_PRIME={pci} " + break print(env_string) From ce7b2c0683074d43ff81180762ecb6a9af75e2e2 Mon Sep 17 00:00:00 2001 From: Ash <101582426+ashuntu@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:32:58 -0600 Subject: [PATCH 03/13] Use lspci instead of lshw --- snap/snapcraft.yaml | 1 + src/desktop-launch | 7 ++++++- src/gpu | 19 +++++++++---------- 3 files changed, 16 insertions(+), 11 deletions(-) mode change 100644 => 100755 src/gpu diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 4600bc5..e445e18 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -123,6 +123,7 @@ parts: cp vkcube $CRAFT_PART_INSTALL/bin/ cp glxinfo $CRAFT_PART_INSTALL/bin/ cp glxgears $CRAFT_PART_INSTALL/bin/ + cp gpu $CRAFT_PART_INSTALL/bin/ alsa-mixin: plugin: dump diff --git a/src/desktop-launch b/src/desktop-launch index 89e2d9b..516994b 100755 --- a/src/desktop-launch +++ b/src/desktop-launch @@ -329,4 +329,9 @@ if [ -n "$SNAP_DESKTOP_DEBUG" ]; then echo "Now running: exec $*" fi -exec "$@" +# Check for GPUs + +gpu_env=$($SNAP/bin/gpu) +echo "Running with $gpu_env" + +eval "$gpu_env exec $@" diff --git a/src/gpu b/src/gpu old mode 100644 new mode 100755 index 636d5a3..125a529 --- a/src/gpu +++ b/src/gpu @@ -1,27 +1,26 @@ #!/usr/bin/python3 -import json import os NVIDIA_ENV = "__NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia __VK_LAYER_NV_optimus=NVIDIA_only" def main(): - lshw = os.popen("lshw -json -numeric -c display 2> /dev/null").read() - gpus = json.loads(lshw) + lspci = os.popen("lspci | grep VGA").read() + gpus = lspci.splitlines() env_string = "" for gpu in gpus: - pci = gpu["businfo"]\ - .replace("@", "-")\ + split = gpu.split(" VGA compatible controller: ") + pci = f"pci-0000_{split[0]}"\ .replace(":", "_")\ - .replace(".", "_") + .replace(".", "_")\ - if "nvidia" in gpu["vendor"].lower(): - env_string = f"DRI_PRIME={pci} {NVIDIA_ENV} " + if "nvidia" in split[1].lower(): + env_string = f"DRI_PRIME={pci} {NVIDIA_ENV}" break - elif "amd" in gpu["vendor"].lower(): - env_string = f"DRI_PRIME={pci} " + elif "amd" in split[1].lower(): + env_string = f"DRI_PRIME={pci}" break print(env_string) From 4c641688afc1c1b078e045e1e3977937430c0814 Mon Sep 17 00:00:00 2001 From: Zoe Spellman Date: Thu, 15 Dec 2022 12:01:58 -0800 Subject: [PATCH 04/13] Add upower support for sleep issues --- snap/snapcraft.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index e445e18..3995253 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -359,6 +359,7 @@ apps: - fuse-support - steam-support - removable-media + - uinput - upower-observe report: command-chain: [bin/desktop-launch] From eaf3b05daec8f398d4cdb187b6834ca9da23533f Mon Sep 17 00:00:00 2001 From: Ken VanDine Date: Tue, 10 Jan 2023 16:50:35 -0500 Subject: [PATCH 05/13] Fixed tests so they actually reflect state of the system. They were reporting success even when they failed. Cleaned up some duplication and make the vulkan and glx utilities wrap the binaries providing from the gaming-graphics content interface. --- snap/snapcraft.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 3995253..9f8531f 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -124,6 +124,10 @@ parts: cp glxinfo $CRAFT_PART_INSTALL/bin/ cp glxgears $CRAFT_PART_INSTALL/bin/ cp gpu $CRAFT_PART_INSTALL/bin/ + cp vulkaninfo $CRAFT_PART_INSTALL/bin/ + cp vkcube $CRAFT_PART_INSTALL/bin/ + cp glxinfo $CRAFT_PART_INSTALL/bin/ + cp glxgears $CRAFT_PART_INSTALL/bin/ alsa-mixin: plugin: dump From 706e1d370d5b4b999c214f44030d669db6cd7989 Mon Sep 17 00:00:00 2001 From: Ken VanDine Date: Wed, 11 Jan 2023 16:11:17 -0500 Subject: [PATCH 06/13] Revert "Fixed tests and cleaned up duplication and broken symlinks" --- snap/snapcraft.yaml | 42 ++++++++++++------------------------------ src/desktop-launch | 2 +- src/glxgears | 4 ---- src/glxinfo | 4 ---- src/vkcube | 4 ---- src/vulkaninfo | 4 ---- tests/run.sh | 5 ++--- tests/vulkan | 2 +- 8 files changed, 16 insertions(+), 51 deletions(-) delete mode 100755 src/glxgears delete mode 100755 src/glxinfo delete mode 100755 src/vkcube delete mode 100755 src/vulkaninfo diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 9f8531f..87feb79 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -42,6 +42,10 @@ layout: bind: $SNAP/graphics/libdrm /usr/share/drirc.d: bind: $SNAP/graphics/usr/share/drirc.d + /usr/lib/i386-linux-gnu: + bind: $SNAP/usr/lib/i386-linux-gnu + /usr/lib/x86_64-linux-gnu/dri: + bind: $SNAP/graphics/usr/lib/x86_64-linux-gnu/dri /usr/share/glvnd/egl_vendor.d: bind: $SNAP/graphics/usr/share/glvnd/egl_vendor.d /usr/lib/x86_64-linux-gnu/alsa-lib: @@ -50,16 +54,10 @@ layout: bind: $SNAP/usr/share/alsa /usr/lib/x86_64-linux-gnu/libvulkan_intel.so: symlink: $SNAP/graphics/usr/lib/x86_64-linux-gnu/libvulkan_intel.so - /usr/lib/i386-linux-gnu/libvulkan_intel.so: - symlink: $SNAP/graphics/usr/lib/i386-linux-gnu/libvulkan_intel.so /usr/lib/x86_64-linux-gnu/libvulkan_lvp.so: symlink: $SNAP/graphics/usr/lib/x86_64-linux-gnu/libvulkan_lvp.so - /usr/lib/i386-linux-gnu/libvulkan_lvp.so: - symlink: $SNAP/graphics/usr/lib/i386-linux-gnu/libvulkan_lvp.so /usr/lib/x86_64-linux-gnu/libvulkan_radeon.so: symlink: $SNAP/graphics/usr/lib/x86_64-linux-gnu/libvulkan_radeon.so - /usr/lib/i386-linux-gnu/libvulkan_radeon.so: - symlink: $SNAP/graphics/usr/lib/i386-linux-gnu/libvulkan_radeon.so /usr/lib/x86_64-linux-gnu/libxcb-dri3.so.0.0.0: symlink: $SNAP/graphics/usr/lib/x86_64-linux-gnu/libxcb-dri3.so.0.0.0 /usr/lib/x86_64-linux-gnu/libxcb-dri3.so.0: @@ -107,8 +105,8 @@ hooks: - opengl environment: - LD_LIBRARY_PATH: $SNAP/graphics/lib/i386-linux-gnu:$SNAP/graphics/usr/lib:$SNAP/usr/lib/i386-linux-gnu:$SNAP/lib/i386-linux-gnu:$SNAP/usr/lib/i386-linux-gnu/pulseaudio${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} - LIBGL_DRIVERS_PATH: $SNAP/graphics/usr/lib/i386-linux-gnu/dri:$SNAP/graphics/usr/lib/x86_64-linux-gnu/dri:${LIBGL_DRIVERS_PATH:+:$LIBGL_DRIVERS_PATH} + LD_LIBRARY_PATH: $SNAP/graphics/lib:$SNAP/graphics/usr/lib:$SNAP/usr/lib/i386-linux-gnu:$SNAP/lib/i386-linux-gnu:$SNAP/usr/lib/i386-linux-gnu/pulseaudio${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} + LIBGL_DRIVERS_PATH: $SNAP/graphics/lib/i386-linux-gnu/dri:$SNAP/graphics/lib/x86_64-linux-gnu/dri:$SNAP/graphics/usr/lib/i386-linux-gnu/dri:$SNAP/graphics/usr/lib/x86_64-linux-gnu/dri:${LIBGL_DRIVERS_PATH:+:$LIBGL_DRIVERS_PATH} parts: launcher: @@ -223,11 +221,8 @@ parts: debug-tools: plugin: nil stage-packages: - - jq - prime: - - usr/bin/jq - - usr/lib/*/libjq.so* - - usr/lib/*/libonig.so* + - vulkan-tools + - mesa-utils tests: plugin: dump @@ -256,10 +251,6 @@ parts: - libpci3 - libvulkan1:i386 - libvulkan1:amd64 - - libxml2:i386 - - libxml2:amd64 - - libicu70:i386 - - libicu70:amd64 - zlib1g:i386 - zlib1g:amd64 - xdg-utils @@ -288,15 +279,6 @@ parts: - -usr/share/gdb - -usr/share/emacs* - -usr/share/lintian - - -usr/share/drirc.d - - -usr/share/vulkan - - -usr/share/Xsession.d - - -usr/lib/*/dri - - -usr/lib/*/vdpau - - -usr/lib/*/libvkd3d* - - -usr/lib/*/libvulkan* - - -usr/lib/*/libVk* - - -usr/lib/*/libLLVM* build-snaps: [core22] override-prime: | craftctl default @@ -376,28 +358,28 @@ apps: - desktop vulkaninfo: command-chain: [bin/desktop-launch] - command: bin/vulkaninfo + command: usr/bin/vulkaninfo plugs: - opengl - x11 - desktop vkcube: command-chain: [bin/desktop-launch] - command: bin/vkcube + command: usr/bin/vkcube plugs: - opengl - x11 - desktop glxinfo: command-chain: [bin/desktop-launch] - command: bin/glxinfo + command: usr/bin/glxinfo plugs: - opengl - x11 - desktop glxgears: command-chain: [bin/desktop-launch] - command: bin/glxgears + command: usr/bin/glxgears plugs: - opengl - x11 diff --git a/src/desktop-launch b/src/desktop-launch index 516994b..3d68b0d 100755 --- a/src/desktop-launch +++ b/src/desktop-launch @@ -195,7 +195,7 @@ if [ -e "/var/lib/snapd/lib/gl/vdpau/libvdpau_nvidia.so" ]; then fi # Export Vulkan ICD filename paths -export VK_ICD_FILENAMES="/var/lib/snapd/lib/vulkan/icd.d/nvidia_icd.json:$SNAP/graphics/usr/share/vulkan/icd.d/radeon_icd.x86_64.json:$SNAP/graphics/usr/share/vulkan/icd.d/radeon_icd.i686.json:$SNAP/graphics/usr/share/vulkan/icd.d/intel_icd.x86_64.json:$SNAP/graphics/usr/share/vulkan/icd.d/intel_icd.i686.json" +export VK_ICD_FILENAMES="/var/lib/snapd/lib/vulkan/icd.d/nvidia_icd.json:$SNAP/graphics/usr/share/vulkan/icd.d/radeon_icd.x86_64.json:$SNAP/graphics/usr/share/vulkan/icd.d/intel_icd.x86_64.json" # Workaround in snapd for proprietary nVidia drivers mounts the drivers in # /var/lib/snapd/lib/gl that needs to be in LD_LIBRARY_PATH diff --git a/src/glxgears b/src/glxgears deleted file mode 100755 index 92fbec0..0000000 --- a/src/glxgears +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -exec $SNAP/graphics/usr/bin/glxgears "$@" -exit $? diff --git a/src/glxinfo b/src/glxinfo deleted file mode 100755 index 983efb7..0000000 --- a/src/glxinfo +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -exec $SNAP/graphics/usr/bin/glxinfo "$@" -exit $? diff --git a/src/vkcube b/src/vkcube deleted file mode 100755 index 76e4d98..0000000 --- a/src/vkcube +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -exec $SNAP/graphics/usr/bin/vkcube "$@" -exit $? diff --git a/src/vulkaninfo b/src/vulkaninfo deleted file mode 100755 index c5a32dc..0000000 --- a/src/vulkaninfo +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -exec $SNAP/graphics/usr/bin/vulkaninfo "$@" -exit $? diff --git a/tests/run.sh b/tests/run.sh index 7d08089..962786a 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -1,5 +1,4 @@ #!/bin/bash -dir=$(dirname $0) -`$dir/glx` && echo "GLX passed" || echo "GLX failed" -`$dir/vulkan` && echo "Vulkan passed" || echo "Vulkan failed" +test glx 2>&1> /dev/null && echo "GLX passed" || echo "GLX failed" +test vulkan 2>&1> /dev/null && echo "Vulkan passed" || echo "Vulkan failed" diff --git a/tests/vulkan b/tests/vulkan index 90be7fc..2c0d9ee 100755 --- a/tests/vulkan +++ b/tests/vulkan @@ -1,4 +1,4 @@ #!/bin/bash -vulkaninfo --json 2>/dev/null | jq .VkPhysicalDeviceProperties 2>&1> /dev/null +vulkaninfo --json|jq .VkPhysicalDeviceProperties 2>&1> /dev/null exit $? From ce0c7e91d47960d0d67966b7390245d35a325018 Mon Sep 17 00:00:00 2001 From: Ken VanDine Date: Wed, 11 Jan 2023 21:10:16 -0500 Subject: [PATCH 07/13] Revert "Revert "Fixed tests and cleaned up duplication and broken symlinks"" --- snap/snapcraft.yaml | 42 ++++++++++++++++++++++++++++++------------ src/desktop-launch | 2 +- src/glxgears | 4 ++++ src/glxinfo | 4 ++++ src/vkcube | 4 ++++ src/vulkaninfo | 4 ++++ tests/run.sh | 5 +++-- tests/vulkan | 2 +- 8 files changed, 51 insertions(+), 16 deletions(-) create mode 100755 src/glxgears create mode 100755 src/glxinfo create mode 100755 src/vkcube create mode 100755 src/vulkaninfo diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 87feb79..9f8531f 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -42,10 +42,6 @@ layout: bind: $SNAP/graphics/libdrm /usr/share/drirc.d: bind: $SNAP/graphics/usr/share/drirc.d - /usr/lib/i386-linux-gnu: - bind: $SNAP/usr/lib/i386-linux-gnu - /usr/lib/x86_64-linux-gnu/dri: - bind: $SNAP/graphics/usr/lib/x86_64-linux-gnu/dri /usr/share/glvnd/egl_vendor.d: bind: $SNAP/graphics/usr/share/glvnd/egl_vendor.d /usr/lib/x86_64-linux-gnu/alsa-lib: @@ -54,10 +50,16 @@ layout: bind: $SNAP/usr/share/alsa /usr/lib/x86_64-linux-gnu/libvulkan_intel.so: symlink: $SNAP/graphics/usr/lib/x86_64-linux-gnu/libvulkan_intel.so + /usr/lib/i386-linux-gnu/libvulkan_intel.so: + symlink: $SNAP/graphics/usr/lib/i386-linux-gnu/libvulkan_intel.so /usr/lib/x86_64-linux-gnu/libvulkan_lvp.so: symlink: $SNAP/graphics/usr/lib/x86_64-linux-gnu/libvulkan_lvp.so + /usr/lib/i386-linux-gnu/libvulkan_lvp.so: + symlink: $SNAP/graphics/usr/lib/i386-linux-gnu/libvulkan_lvp.so /usr/lib/x86_64-linux-gnu/libvulkan_radeon.so: symlink: $SNAP/graphics/usr/lib/x86_64-linux-gnu/libvulkan_radeon.so + /usr/lib/i386-linux-gnu/libvulkan_radeon.so: + symlink: $SNAP/graphics/usr/lib/i386-linux-gnu/libvulkan_radeon.so /usr/lib/x86_64-linux-gnu/libxcb-dri3.so.0.0.0: symlink: $SNAP/graphics/usr/lib/x86_64-linux-gnu/libxcb-dri3.so.0.0.0 /usr/lib/x86_64-linux-gnu/libxcb-dri3.so.0: @@ -105,8 +107,8 @@ hooks: - opengl environment: - LD_LIBRARY_PATH: $SNAP/graphics/lib:$SNAP/graphics/usr/lib:$SNAP/usr/lib/i386-linux-gnu:$SNAP/lib/i386-linux-gnu:$SNAP/usr/lib/i386-linux-gnu/pulseaudio${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} - LIBGL_DRIVERS_PATH: $SNAP/graphics/lib/i386-linux-gnu/dri:$SNAP/graphics/lib/x86_64-linux-gnu/dri:$SNAP/graphics/usr/lib/i386-linux-gnu/dri:$SNAP/graphics/usr/lib/x86_64-linux-gnu/dri:${LIBGL_DRIVERS_PATH:+:$LIBGL_DRIVERS_PATH} + LD_LIBRARY_PATH: $SNAP/graphics/lib/i386-linux-gnu:$SNAP/graphics/usr/lib:$SNAP/usr/lib/i386-linux-gnu:$SNAP/lib/i386-linux-gnu:$SNAP/usr/lib/i386-linux-gnu/pulseaudio${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH} + LIBGL_DRIVERS_PATH: $SNAP/graphics/usr/lib/i386-linux-gnu/dri:$SNAP/graphics/usr/lib/x86_64-linux-gnu/dri:${LIBGL_DRIVERS_PATH:+:$LIBGL_DRIVERS_PATH} parts: launcher: @@ -221,8 +223,11 @@ parts: debug-tools: plugin: nil stage-packages: - - vulkan-tools - - mesa-utils + - jq + prime: + - usr/bin/jq + - usr/lib/*/libjq.so* + - usr/lib/*/libonig.so* tests: plugin: dump @@ -251,6 +256,10 @@ parts: - libpci3 - libvulkan1:i386 - libvulkan1:amd64 + - libxml2:i386 + - libxml2:amd64 + - libicu70:i386 + - libicu70:amd64 - zlib1g:i386 - zlib1g:amd64 - xdg-utils @@ -279,6 +288,15 @@ parts: - -usr/share/gdb - -usr/share/emacs* - -usr/share/lintian + - -usr/share/drirc.d + - -usr/share/vulkan + - -usr/share/Xsession.d + - -usr/lib/*/dri + - -usr/lib/*/vdpau + - -usr/lib/*/libvkd3d* + - -usr/lib/*/libvulkan* + - -usr/lib/*/libVk* + - -usr/lib/*/libLLVM* build-snaps: [core22] override-prime: | craftctl default @@ -358,28 +376,28 @@ apps: - desktop vulkaninfo: command-chain: [bin/desktop-launch] - command: usr/bin/vulkaninfo + command: bin/vulkaninfo plugs: - opengl - x11 - desktop vkcube: command-chain: [bin/desktop-launch] - command: usr/bin/vkcube + command: bin/vkcube plugs: - opengl - x11 - desktop glxinfo: command-chain: [bin/desktop-launch] - command: usr/bin/glxinfo + command: bin/glxinfo plugs: - opengl - x11 - desktop glxgears: command-chain: [bin/desktop-launch] - command: usr/bin/glxgears + command: bin/glxgears plugs: - opengl - x11 diff --git a/src/desktop-launch b/src/desktop-launch index 3d68b0d..516994b 100755 --- a/src/desktop-launch +++ b/src/desktop-launch @@ -195,7 +195,7 @@ if [ -e "/var/lib/snapd/lib/gl/vdpau/libvdpau_nvidia.so" ]; then fi # Export Vulkan ICD filename paths -export VK_ICD_FILENAMES="/var/lib/snapd/lib/vulkan/icd.d/nvidia_icd.json:$SNAP/graphics/usr/share/vulkan/icd.d/radeon_icd.x86_64.json:$SNAP/graphics/usr/share/vulkan/icd.d/intel_icd.x86_64.json" +export VK_ICD_FILENAMES="/var/lib/snapd/lib/vulkan/icd.d/nvidia_icd.json:$SNAP/graphics/usr/share/vulkan/icd.d/radeon_icd.x86_64.json:$SNAP/graphics/usr/share/vulkan/icd.d/radeon_icd.i686.json:$SNAP/graphics/usr/share/vulkan/icd.d/intel_icd.x86_64.json:$SNAP/graphics/usr/share/vulkan/icd.d/intel_icd.i686.json" # Workaround in snapd for proprietary nVidia drivers mounts the drivers in # /var/lib/snapd/lib/gl that needs to be in LD_LIBRARY_PATH diff --git a/src/glxgears b/src/glxgears new file mode 100755 index 0000000..92fbec0 --- /dev/null +++ b/src/glxgears @@ -0,0 +1,4 @@ +#!/bin/bash + +exec $SNAP/graphics/usr/bin/glxgears "$@" +exit $? diff --git a/src/glxinfo b/src/glxinfo new file mode 100755 index 0000000..983efb7 --- /dev/null +++ b/src/glxinfo @@ -0,0 +1,4 @@ +#!/bin/bash + +exec $SNAP/graphics/usr/bin/glxinfo "$@" +exit $? diff --git a/src/vkcube b/src/vkcube new file mode 100755 index 0000000..76e4d98 --- /dev/null +++ b/src/vkcube @@ -0,0 +1,4 @@ +#!/bin/bash + +exec $SNAP/graphics/usr/bin/vkcube "$@" +exit $? diff --git a/src/vulkaninfo b/src/vulkaninfo new file mode 100755 index 0000000..c5a32dc --- /dev/null +++ b/src/vulkaninfo @@ -0,0 +1,4 @@ +#!/bin/bash + +exec $SNAP/graphics/usr/bin/vulkaninfo "$@" +exit $? diff --git a/tests/run.sh b/tests/run.sh index 962786a..7d08089 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -1,4 +1,5 @@ #!/bin/bash -test glx 2>&1> /dev/null && echo "GLX passed" || echo "GLX failed" -test vulkan 2>&1> /dev/null && echo "Vulkan passed" || echo "Vulkan failed" +dir=$(dirname $0) +`$dir/glx` && echo "GLX passed" || echo "GLX failed" +`$dir/vulkan` && echo "Vulkan passed" || echo "Vulkan failed" diff --git a/tests/vulkan b/tests/vulkan index 2c0d9ee..90be7fc 100755 --- a/tests/vulkan +++ b/tests/vulkan @@ -1,4 +1,4 @@ #!/bin/bash -vulkaninfo --json|jq .VkPhysicalDeviceProperties 2>&1> /dev/null +vulkaninfo --json 2>/dev/null | jq .VkPhysicalDeviceProperties 2>&1> /dev/null exit $? From 457bb6c455f04d23f03d2454825f1bb3d19e7e9b Mon Sep 17 00:00:00 2001 From: Ash <101582426+ashuntu@users.noreply.github.com> Date: Tue, 17 Jan 2023 17:32:58 -0600 Subject: [PATCH 08/13] Use lspci instead of lshw --- snap/snapcraft.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 9f8531f..a3df9d9 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -128,6 +128,7 @@ parts: cp vkcube $CRAFT_PART_INSTALL/bin/ cp glxinfo $CRAFT_PART_INSTALL/bin/ cp glxgears $CRAFT_PART_INSTALL/bin/ + cp gpu $CRAFT_PART_INSTALL/bin/ alsa-mixin: plugin: dump From 40ec03a6aa477b07f288654482e29b21b475eb80 Mon Sep 17 00:00:00 2001 From: Ash <101582426+ashuntu@users.noreply.github.com> Date: Wed, 18 Jan 2023 13:32:49 -0600 Subject: [PATCH 09/13] Remove uinput --- snap/snapcraft.yaml | 1 - src/newgpu.sh | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100755 src/newgpu.sh diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index a3df9d9..35cd6c2 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -364,7 +364,6 @@ apps: - fuse-support - steam-support - removable-media - - uinput - upower-observe report: command-chain: [bin/desktop-launch] diff --git a/src/newgpu.sh b/src/newgpu.sh new file mode 100755 index 0000000..cb065c9 --- /dev/null +++ b/src/newgpu.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +NVIDIA_ENV="__NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia __VK_LAYER_NV_optimus=NVIDIA_only" +lspci=$(lspci | grep VGA) +IFS=$'\n' read -rd '' -a gpus <<< "$lspci" +env_string="" + +for gpu in "${gpus[@]}" +do + split=($(echo $gpu | awk '{split($0,a," VGA compatible controller: "); print a[1], a[2]}')) + pci=$(echo ${split[0]} | sed 's/:/_/g' | sed 's/\./_/g' | awk '{print "pci-0000_"$0}') + + if [[ $(echo ${split[1]} | tr '[:upper:]' '[:lower:]') =~ "nvidia" ]]; then + env_string="DRI_PRIME=$pci $NVIDIA_ENV" + break + elif [[ $(echo ${split[1]} | tr '[:upper:]' '[:lower:]') =~ "amd" ]]; then + env_string="DRI_PRIME=$pci" + break + fi +done + +echo $env_string From b7ab0cbade949bc705174e29eb927a3011987d9c Mon Sep 17 00:00:00 2001 From: Ash <101582426+ashuntu@users.noreply.github.com> Date: Thu, 19 Jan 2023 19:30:29 -0600 Subject: [PATCH 10/13] Prioritize largest PCI --- src/gpu | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/gpu b/src/gpu index 125a529..247b86b 100755 --- a/src/gpu +++ b/src/gpu @@ -1,28 +1,31 @@ #!/usr/bin/python3 import os +import re -NVIDIA_ENV = "__NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia __VK_LAYER_NV_optimus=NVIDIA_only" +NVIDIA_ENV = ( + "__NV_PRIME_RENDER_OFFLOAD=1 " + "__GLX_VENDOR_LIBRARY_NAME=nvidia " + "__VK_LAYER_NV_optimus=NVIDIA_only" +) def main(): lspci = os.popen("lspci | grep VGA").read() - gpus = lspci.splitlines() + gpus = {} env_string = "" - for gpu in gpus: + for gpu in lspci.splitlines(): split = gpu.split(" VGA compatible controller: ") pci = f"pci-0000_{split[0]}"\ .replace(":", "_")\ .replace(".", "_")\ if "nvidia" in split[1].lower(): - env_string = f"DRI_PRIME={pci} {NVIDIA_ENV}" - break - - elif "amd" in split[1].lower(): - env_string = f"DRI_PRIME={pci}" - break + gpus[int(re.sub(r"[^0-9]+", "", pci))] = f"DRI_PRIME={pci} {NVIDIA_ENV}" + else: + gpus[int(re.sub(r"[^0-9]+", "", pci))] = f"DRI_PRIME={pci}" + env_string = gpus[max(list(gpus.keys()))] print(env_string) From b242336a8d703c8683d63878b9da63cc50d3c0ce Mon Sep 17 00:00:00 2001 From: Ash <101582426+ashuntu@users.noreply.github.com> Date: Mon, 23 Jan 2023 16:08:40 -0600 Subject: [PATCH 11/13] Add --list, --json, and --gpu options --- snap/snapcraft.yaml | 10 ++++++- src/desktop-launch | 7 +---- src/gpu | 68 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 66 insertions(+), 19 deletions(-) diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 35cd6c2..5c16c07 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -129,6 +129,8 @@ parts: cp glxinfo $CRAFT_PART_INSTALL/bin/ cp glxgears $CRAFT_PART_INSTALL/bin/ cp gpu $CRAFT_PART_INSTALL/bin/ + cp gpuenv $CRAFT_PART_INSTALL/bin/ + cp steam-snap.sh $CRAFT_PART_INSTALL/bin/ alsa-mixin: plugin: dump @@ -331,7 +333,7 @@ parts: apps: steam: - command-chain: [snap/command-chain/alsa-launch, bin/desktop-launch] + command-chain: [snap/command-chain/alsa-launch, bin/desktop-launch, bin/gpu] #command: bin/steam-snap.sh command: usr/lib/steam/bin_steam.sh -no-cef-sandbox desktop: steam.desktop @@ -365,6 +367,12 @@ apps: - steam-support - removable-media - upower-observe + gpu: + command-chain: [bin/desktop-launch] + command: bin/gpu + plugs: + - system-observe + - hardware-observe report: command-chain: [bin/desktop-launch] command: bin/steamreport diff --git a/src/desktop-launch b/src/desktop-launch index 516994b..89e2d9b 100755 --- a/src/desktop-launch +++ b/src/desktop-launch @@ -329,9 +329,4 @@ if [ -n "$SNAP_DESKTOP_DEBUG" ]; then echo "Now running: exec $*" fi -# Check for GPUs - -gpu_env=$($SNAP/bin/gpu) -echo "Running with $gpu_env" - -eval "$gpu_env exec $@" +exec "$@" diff --git a/src/gpu b/src/gpu index 247b86b..ba06846 100755 --- a/src/gpu +++ b/src/gpu @@ -1,32 +1,76 @@ #!/usr/bin/python3 +import json import os import re +import argparse +import subprocess +import sys -NVIDIA_ENV = ( - "__NV_PRIME_RENDER_OFFLOAD=1 " - "__GLX_VENDOR_LIBRARY_NAME=nvidia " - "__VK_LAYER_NV_optimus=NVIDIA_only" -) +NVIDIA_ENV = { + "__NV_PRIME_RENDER_OFFLOAD": 1, + "__GLX_VENDOR_LIBRARY_NAME": "nvidia", + "__VK_LAYER_NV_optimus": "NVIDIA_only", +} def main(): + parser = argparse.ArgumentParser() + parser.add_argument( + "--gpu", "-g", + help="Select a GPU to use" + ) + parser.add_argument( + "--list", "-l", + help="List available GPUs", + const=True, + action="store_const" + ) + parser.add_argument( + "--json", "-j", + help="List all data in json format", + const=True, + action="store_const" + ) + args, nonargs = parser.parse_known_args() + lspci = os.popen("lspci | grep VGA").read() gpus = {} - env_string = "" + max_pci = -1 for gpu in lspci.splitlines(): split = gpu.split(" VGA compatible controller: ") pci = f"pci-0000_{split[0]}"\ .replace(":", "_")\ - .replace(".", "_")\ + .replace(".", "_") + pci_num = int(re.sub(r"[^0-9]+", "", pci)) + max_pci = max(max_pci, pci_num) + gpus[pci_num] = {} + gpus[pci_num]["gpu"] = split[1] + gpus[pci_num]["env"] = {"DRI_PRIME": pci} if "nvidia" in split[1].lower(): - gpus[int(re.sub(r"[^0-9]+", "", pci))] = f"DRI_PRIME={pci} {NVIDIA_ENV}" - else: - gpus[int(re.sub(r"[^0-9]+", "", pci))] = f"DRI_PRIME={pci}" + gpus[pci_num]["env"].update(NVIDIA_ENV) + + if args.list: + [print(f"{k:6}: {v['gpu']}") for k, v in gpus.items()] + return + + if args.json: + print(json.dumps(gpus, indent=4)) + return + + if args.gpu: + max_pci = int(args.gpu) + + if max_pci >= 0: + if max_pci not in gpus: + print("Invalid GPU ID, use --list to show valid IDs.") + return + for k, v in gpus[max_pci]["env"].items(): + os.environ[k] = str(v) - env_string = gpus[max(list(gpus.keys()))] - print(env_string) + if nonargs: + subprocess.call(nonargs) if __name__ == "__main__": From 3b52635fc7cb9963e79c08b7e7f52616cb3c2b01 Mon Sep 17 00:00:00 2001 From: Ash <101582426+ashuntu@users.noreply.github.com> Date: Mon, 23 Jan 2023 17:41:26 -0600 Subject: [PATCH 12/13] Allow args to be passed to snap run steam --- snap/snapcraft.yaml | 2 +- src/desktop-launch | 2 +- src/gpu | 80 ++++++++++++++++++++++++++------------------- 3 files changed, 49 insertions(+), 35 deletions(-) diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 5c16c07..65bd497 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -333,7 +333,7 @@ parts: apps: steam: - command-chain: [snap/command-chain/alsa-launch, bin/desktop-launch, bin/gpu] + command-chain: [snap/command-chain/alsa-launch, bin/desktop-launch] #command: bin/steam-snap.sh command: usr/lib/steam/bin_steam.sh -no-cef-sandbox desktop: steam.desktop diff --git a/src/desktop-launch b/src/desktop-launch index 89e2d9b..06ba0a5 100755 --- a/src/desktop-launch +++ b/src/desktop-launch @@ -329,4 +329,4 @@ if [ -n "$SNAP_DESKTOP_DEBUG" ]; then echo "Now running: exec $*" fi -exec "$@" +$SNAP/bin/gpu $@ diff --git a/src/gpu b/src/gpu index ba06846..a56c146 100755 --- a/src/gpu +++ b/src/gpu @@ -13,7 +13,34 @@ NVIDIA_ENV = { "__VK_LAYER_NV_optimus": "NVIDIA_only", } -def main(): +def get_gpus() -> dict: + """ + Get GPU data including: + shortened PCI number, + GPU name, + and environment variables to use the GPU. + """ + lspci = os.popen("lspci | grep VGA").read() + gpus = {} + + for gpu in lspci.splitlines(): + split = gpu.split(" VGA compatible controller: ") + pci = f"pci-0000_{split[0]}"\ + .replace(":", "_")\ + .replace(".", "_") + pci_num = int(re.sub(r"[^0-9]+", "", pci)) + + gpus[pci_num] = {} + gpus[pci_num]["gpu"] = split[1] + gpus[pci_num]["env"] = {"DRI_PRIME": pci} + if "nvidia" in split[1].lower(): + gpus[pci_num]["env"].update(NVIDIA_ENV) + + return gpus + + +def main() -> None: + # Parse arugments parser = argparse.ArgumentParser() parser.add_argument( "--gpu", "-g", @@ -32,43 +59,30 @@ def main(): action="store_const" ) args, nonargs = parser.parse_known_args() + nargs = sum([1 for a in vars(args).values() if a is not None]) - lspci = os.popen("lspci | grep VGA").read() - gpus = {} - max_pci = -1 - - for gpu in lspci.splitlines(): - split = gpu.split(" VGA compatible controller: ") - pci = f"pci-0000_{split[0]}"\ - .replace(":", "_")\ - .replace(".", "_") - pci_num = int(re.sub(r"[^0-9]+", "", pci)) - - max_pci = max(max_pci, pci_num) - gpus[pci_num] = {} - gpus[pci_num]["gpu"] = split[1] - gpus[pci_num]["env"] = {"DRI_PRIME": pci} - if "nvidia" in split[1].lower(): - gpus[pci_num]["env"].update(NVIDIA_ENV) + # Handle arguments + if nargs != 0: + gpus = get_gpus() - if args.list: - [print(f"{k:6}: {v['gpu']}") for k, v in gpus.items()] - return - - if args.json: - print(json.dumps(gpus, indent=4)) - return - - if args.gpu: - max_pci = int(args.gpu) + # List GPUs with shortened PCI ID and name + if args.list: + [print(f"{k:6}: {v['gpu']}") for k, v in gpus.items()] + return - if max_pci >= 0: - if max_pci not in gpus: - print("Invalid GPU ID, use --list to show valid IDs.") + # List GPU data in JSON format + if args.json: + print(json.dumps(gpus, indent=4)) return - for k, v in gpus[max_pci]["env"].items(): - os.environ[k] = str(v) + # Sets environment variables for the specified GPU + if args.gpu: + if int(args.gpu) not in gpus: + print("Invalid GPU ID, use --list to list GPUs.") + for k, v in gpus[int(args.gpu)]["env"].items(): + os.environ[k] = str(v) + + # Run remaining arguments as a command if nonargs: subprocess.call(nonargs) From 1b1a3e272e95e537e608b05b679627dde465e498 Mon Sep 17 00:00:00 2001 From: Ash <101582426+ashuntu@users.noreply.github.com> Date: Tue, 24 Jan 2023 16:07:16 -0600 Subject: [PATCH 13/13] Remove unneeded file --- snap/snapcraft.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 65bd497..83fac3b 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -129,7 +129,6 @@ parts: cp glxinfo $CRAFT_PART_INSTALL/bin/ cp glxgears $CRAFT_PART_INSTALL/bin/ cp gpu $CRAFT_PART_INSTALL/bin/ - cp gpuenv $CRAFT_PART_INSTALL/bin/ cp steam-snap.sh $CRAFT_PART_INSTALL/bin/ alsa-mixin: