diff --git a/bin/omarchy-hw-hybrid-gpu b/bin/omarchy-hw-hybrid-gpu index 42f3c19dfe45..10f7cd00a00e 100755 --- a/bin/omarchy-hw-hybrid-gpu +++ b/bin/omarchy-hw-hybrid-gpu @@ -2,8 +2,27 @@ # omarchy:summary=Detect whether the system has an active hybrid GPU configuration +# Count display-class devices from the cached sysfs IDs rather than lspci. lspci reads PCI +# config space, which resumes a runtime-suspended GPU -- the same reason omarchy-hw-nvidia +# and omarchy-hw-nvidia-gsp already avoid it. That matters more here than anywhere else: +# this check runs on a hybrid laptop, where the dGPU it is asking about is usually asleep, +# and the result feeds session environment during Hyprland's config load. +# +# 0x03* covers VGA (0x0300), 3D (0x0302) and Display (0x0380) controllers, matching what +# the previous `lspci | grep -E 'VGA|3D|Display'` counted. +pci_devices_path="${OMARCHY_PCI_DEVICES_PATH:-/sys/bus/pci/devices}" + multiple_gpus() { - (($(lspci | grep -cE 'VGA|3D|Display') >= 2)) + local device count=0 + + shopt -s nullglob + + for device in "$pci_devices_path"/*; do + [[ -r $device/class ]] || continue + [[ $(< "$device/class") == 0x03* ]] && ((count++)) + done + + ((count >= 2)) } if omarchy-cmd-present supergfxctl; then diff --git a/default/hypr/nvidia.lua b/default/hypr/nvidia.lua index 663fe79e1bb8..c575cbd80cc0 100644 --- a/default/hypr/nvidia.lua +++ b/default/hypr/nvidia.lua @@ -3,17 +3,29 @@ local paths = require("default.hypr.paths") local nvidia = paths.omarchy_path .. "/bin/omarchy-hw-nvidia" local nvidia_gsp = paths.omarchy_path .. "/bin/omarchy-hw-nvidia-gsp" local nvidia_without_gsp = paths.omarchy_path .. "/bin/omarchy-hw-nvidia-without-gsp" +local hybrid_gpu = paths.omarchy_path .. "/bin/omarchy-hw-hybrid-gpu" --- These detectors read cached sysfs IDs rather than shelling out to lspci. --- lspci reads PCI config space, which resumes a runtime-suspended GPU, and on a --- hybrid laptop that wake alone outlasts Hyprland's 1.5s config reload budget. +-- These detectors read cached sysfs IDs rather than shelling out to lspci. lspci reads +-- PCI config space, which resumes a runtime-suspended GPU, and on a hybrid laptop that +-- wake alone outlasts Hyprland's 1.5s config reload budget. if o.shell_succeeds(o.shell_quote(nvidia)) then - if o.shell_succeeds(o.shell_quote(nvidia_gsp)) then - hl.env("NVD_BACKEND", "direct") - hl.env("LIBVA_DRIVER_NAME", "nvidia") - hl.env("__GLX_VENDOR_LIBRARY_NAME", "nvidia") - elseif o.shell_succeeds(o.shell_quote(nvidia_without_gsp)) then - hl.env("NVD_BACKEND", "egl") - hl.env("__GLX_VENDOR_LIBRARY_NAME", "nvidia") + -- On a hybrid laptop the panel hangs off the iGPU and the dGPU is meant to stay + -- suspended until something asks for it. Pointing GLX at the NVIDIA vendor library + -- makes EVERY OpenGL client render on the dGPU, so it wakes at login and never gets + -- to sleep again -- a constant battery cost for no visible gain, since the frames + -- still have to be copied back to the iGPU to reach the display. + -- + -- Leave the Mesa/iGPU default in place there and let the discrete card be opted into + -- per application (prime-run, or __NV_PRIME_RENDER_OFFLOAD=1). Desktops where NVIDIA + -- actually drives the display are unaffected: they are not hybrid, so this is skipped. + if not o.shell_succeeds(o.shell_quote(hybrid_gpu)) then + if o.shell_succeeds(o.shell_quote(nvidia_gsp)) then + hl.env("NVD_BACKEND", "direct") + hl.env("LIBVA_DRIVER_NAME", "nvidia") + hl.env("__GLX_VENDOR_LIBRARY_NAME", "nvidia") + elseif o.shell_succeeds(o.shell_quote(nvidia_without_gsp)) then + hl.env("NVD_BACKEND", "egl") + hl.env("__GLX_VENDOR_LIBRARY_NAME", "nvidia") + end end end diff --git a/test/shell.d/hw-hybrid-gpu-test.sh b/test/shell.d/hw-hybrid-gpu-test.sh index d7b7ae777b69..47d884785bcd 100644 --- a/test/shell.d/hw-hybrid-gpu-test.sh +++ b/test/shell.d/hw-hybrid-gpu-test.sh @@ -28,18 +28,33 @@ esac printf '%s\n' "${SUPPORTED_MODES:-Integrated Hybrid}" STUB -cat >"$fake_bin/lspci" <<'STUB' -#!/bin/bash - -for _ in $(seq "${GPU_COUNT:-1}"); do - echo "0000:00:02.0 VGA compatible controller: Stub GPU" -done -STUB - chmod +x "$fake_bin"/* +# The detector counts display-class devices from sysfs rather than parsing lspci, so the +# GPU count is expressed as PCI device fixtures. 0x0300 is VGA, 0x0380 Display; 0x0200 +# (ethernet) is present throughout to prove non-display devices are not counted. +write_pci_devices() { + local count=${GPU_COUNT:-1} index=0 + + rm -rf "$test_tmp/devices" + mkdir -p "$test_tmp/devices/0000:00:1f.6" + printf '0x8086\n' >"$test_tmp/devices/0000:00:1f.6/vendor" + printf '0x020000\n' >"$test_tmp/devices/0000:00:1f.6/class" + + while ((index < count)); do + local slot + slot=$(printf '0000:%02x:00.0' "$index") + mkdir -p "$test_tmp/devices/$slot" + printf '0x10de\n' >"$test_tmp/devices/$slot/vendor" + printf '0x030000\n' >"$test_tmp/devices/$slot/class" + index=$((index + 1)) + done +} + hybrid_gpu() { - PATH="$fake_bin:$PATH" timeout --kill-after=1s 10s bash "$ROOT/bin/omarchy-hw-hybrid-gpu" + write_pci_devices + PATH="$fake_bin:$PATH" OMARCHY_PCI_DEVICES_PATH="$test_tmp/devices" \ + timeout --kill-after=1s 10s bash "$ROOT/bin/omarchy-hw-hybrid-gpu" } hybrid_gpu || @@ -89,3 +104,12 @@ chmod +x "$fake_bin/omarchy-cmd-present" GPU_COUNT=2 hybrid_gpu || fail "hybrid GPU detection counts GPUs without supergfxctl" pass "hybrid GPU detection counts GPUs without supergfxctl" + +GPU_COUNT=2 hybrid_gpu || + fail "hybrid GPU detection counts GPUs from sysfs without reading PCI config space" +pass "hybrid GPU detection counts GPUs from sysfs without reading PCI config space" + +# Comments in the detector explain why lspci is avoided, so only inspect real code. +grep -v '^[[:space:]]*#' "$ROOT/bin/omarchy-hw-hybrid-gpu" | grep -q 'lspci' && + fail "hybrid GPU detection avoids lspci, which resumes a suspended GPU" +pass "hybrid GPU detection avoids lspci, which resumes a suspended GPU"