From 6fe01249f668cbc5b5b659d5fc91e87c754f81f7 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 06:47:26 +0000 Subject: [PATCH] run_game_wine.sh: keep the drivers' shader cache from run to run (--cache, or the caller's XDG_CACHE_HOME) Finding by z3r0rc in #1608: the launcher set XDG_CACHE_HOME to a new folder in each run folder, so NVIDIA's GLCache (and Mesa's shader cache) started empty on every launch. The caller's XDG_CACHE_HOME is now used when it is set, --cache DIR names one, and --cache fresh keeps the old cold-cache behaviour. session.txt says which. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01JPqXc6svM4pbBagTsVEYA3 --- docs/linux_proton.md | 4 ++++ scripts/run_game_wine.sh | 15 ++++++++++++-- tests/test_run_game_wine.py | 40 ++++++++++++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/docs/linux_proton.md b/docs/linux_proton.md index 42487ed496..2e2a7d643b 100644 --- a/docs/linux_proton.md +++ b/docs/linux_proton.md @@ -98,6 +98,10 @@ What it does: - **Limits:** a hard time limit (`--seconds`, then SIGTERM and SIGKILL 10 s later), a lock so one run happens at a time (`--lock`, by default in `XDG_RUNTIME_DIR`), and teardown of Wine and the whole session on exit, time-out or signal. +- **Shader cache:** the session's `XDG_CACHE_HOME`, where NVIDIA's `GLCache` and Mesa's shader cache go, is the + caller's when it is set, or the folder `--cache DIR` names; both are kept from run to run, so a later launch does + not compile every pipeline in the driver again. Without either, or with `--cache fresh`, it is a new folder in the + run folder, for a cold-cache run. The summary's first line says which (`shader cache: kept in DIR` or `fresh`). - **Traces:** `gta6rt --run` with `--trace` and `--trace-d3d12`, a fresh `--overlay` and save folder in the run folder. Every other `LEONIDA_*` variable reaches the runtime unchanged, so `LEONIDA_INPUT_SCRIPT=a@210,a@240` presses A as above. Arguments after `--` go to `gta6rt`. diff --git a/scripts/run_game_wine.sh b/scripts/run_game_wine.sh index ce01b0a07d..7fa6c10634 100755 --- a/scripts/run_game_wine.sh +++ b/scripts/run_game_wine.sh @@ -34,6 +34,9 @@ Options: --device VID:DID the Vulkan device, as MESA_VK_DEVICE_SELECT=VID:DID! (e.g. 1002:744c); without it Mesa picks --screenshot-every S capture the game window into /shots every S seconds (needs ImageMagick's import and xprop); the shots are game content and stay local + --cache DIR|fresh XDG_CACHE_HOME of the run, where the drivers keep their shader caches (NVIDIA's GLCache, + Mesa's shader cache): DIR is kept from run to run, fresh is a new one in the run folder + (default: the caller's XDG_CACHE_HOME when it is set, otherwise fresh) --lock FILE the lock that allows one run at a time (default $XDG_RUNTIME_DIR/leonida-run-game-wine.lock, or /run.lock without XDG_RUNTIME_DIR) -h, --help this text @@ -103,7 +106,7 @@ if [ "${1:-}" = --inner ]; then fi # ---- arguments ---- -SRC= BUILD_DIR= OUT= PREFIX=${WINEPREFIX:-} LIMIT=600 VISIBLE=0 DEVICE= SHOT_EVERY=0 LOCK= +SRC= BUILD_DIR= OUT= PREFIX=${WINEPREFIX:-} LIMIT=600 VISIBLE=0 DEVICE= SHOT_EVERY=0 LOCK= CACHE=${XDG_CACHE_HOME:-fresh} EXE=${LEONIDA_GAME_EXE:-} GAME=${LEONIDA_GAME_DIR:-} SHADERS=${LEONIDA_SHADERS:-} need() { [ $# -ge 2 ] && [ -n "$2" ] || die "$1 needs a value (see --help)"; } while [ $# -gt 0 ]; do @@ -121,6 +124,7 @@ while [ $# -gt 0 ]; do --device) need "$@"; DEVICE=$2; shift 2 ;; --screenshot-every) need "$@"; SHOT_EVERY=$2; shift 2 ;; --lock) need "$@"; LOCK=$2; shift 2 ;; + --cache) need "$@"; CACHE=$2; shift 2 ;; --) shift; break ;; *) die "unknown argument: $1 (see --help)" ;; esac @@ -138,6 +142,10 @@ if [ -n "$DEVICE" ] && ! [[ $DEVICE =~ ^[0-9a-fA-F]{4}:[0-9a-fA-F]{4}$ ]]; then fi [ -f "$EXE" ] || die "no game executable at $EXE" [ -d "$GAME" ] || die "no package folder at $GAME" +if [ "$CACHE" != fresh ]; then + mkdir -p "$CACHE" || die "cannot create the cache folder $CACHE" + CACHE=$(readlink -f "$CACHE") +fi if [ "$SHADERS" != none ]; then SHADERS=${SHADERS#Z:} [ -d "$SHADERS" ] || die "no shader library at $SHADERS" @@ -205,9 +213,12 @@ unset LEONIDA_GAME_EXE LEONIDA_GAME_DIR # launcher inputs; LEONIDA_GAME_DIR als export WINE export XDG_RUNTIME_DIR=$RUN/xdg rm -rf "$XDG_RUNTIME_DIR"; mkdir -p -m 700 "$XDG_RUNTIME_DIR" -export XDG_CONFIG_HOME=$RUN/session/config XDG_DATA_HOME=$RUN/session/data XDG_CACHE_HOME=$RUN/session/cache \ +# The cache folder alone may outlive the run: a fresh one makes every launch compile its pipelines again in the driver. +if [ "$CACHE" = fresh ]; then XDG_CACHE_HOME=$RUN/session/cache; else XDG_CACHE_HOME=$CACHE; fi +export XDG_CONFIG_HOME=$RUN/session/config XDG_DATA_HOME=$RUN/session/data XDG_CACHE_HOME \ XDG_STATE_HOME=$RUN/session/state mkdir -p "$XDG_CONFIG_HOME" "$XDG_DATA_HOME" "$XDG_CACHE_HOME" "$XDG_STATE_HOME" +if [ "$CACHE" = fresh ]; then echo "shader cache: fresh"; else echo "shader cache: kept in $CACHE"; fi >> "$RUN/session.txt" unset WAYLAND_DISPLAY WAYLAND_SOCKET DBUS_SESSION_BUS_ADDRESS if [ "$VISIBLE" = 1 ]; then if command -v xdpyinfo >/dev/null 2>&1 && ! xdpyinfo >/dev/null 2>&1; then diff --git a/tests/test_run_game_wine.py b/tests/test_run_game_wine.py index a7d09c8899..e04f05061f 100644 --- a/tests/test_run_game_wine.py +++ b/tests/test_run_game_wine.py @@ -29,8 +29,9 @@ WINE_STUB = """#!/bin/sh if [ "$1" = wineboot ]; then exit 0; fi for a in "$@"; do printf '%s\\n' "$a"; done > "$STUB_LOG/wine.args" -env | grep -E '^(LEONIDA_|MESA_VK|WINEDLL|WINEPREFIX|PULSE_SERVER|PIPEWIRE_REMOTE|DISPLAY)' | sort > "$STUB_LOG/wine.env" +env | grep -E '^(LEONIDA_|MESA_VK|WINEDLL|WINEPREFIX|PULSE_SERVER|PIPEWIRE_REMOTE|DISPLAY|XDG_CACHE_HOME)' | sort > "$STUB_LOG/wine.env" pwd > "$STUB_LOG/wine.cwd" +if [ -n "${XDG_CACHE_HOME:-}" ]; then mkdir -p "$XDG_CACHE_HOME/nvidia" && echo x >> "$XDG_CACHE_HOME/nvidia/GLCache"; fi echo "[rt] audio: serving the silent output endpoint (LEONIDA_AUDIO=silent): 48 kHz float 7.1" >&2 echo "[d3d12-trace] #1 XboxID3D12CommandQueue::PresentX ok" >&2 exit 0 @@ -86,8 +87,8 @@ def inputs(self, **drop): flat += [flag, str(value)] return flat - def run_script(self, *args, display=":99"): - env = {"PATH": str(self.bin), "HOME": str(self.tmp), "STUB_LOG": str(self.log)} + def run_script(self, *args, display=":99", **extra): + env = {"PATH": str(self.bin), "HOME": str(self.tmp), "STUB_LOG": str(self.log), **extra} if display: env["DISPLAY"] = display return subprocess.run([str(self.bin / "bash"), str(SCRIPT), *args], cwd=self.tmp, env=env, capture_output=True, text=True, @@ -197,6 +198,39 @@ def test_without_a_device_it_warns(self): env = (self.log / "wine.env").read_text(encoding="utf-8") self.assertNotIn("MESA_VK_DEVICE_SELECT", env) + def wine_env(self): + return dict(line.split("=", 1) for line in (self.log / "wine.env").read_text(encoding="utf-8").splitlines()) + + def run_with_cache(self, out, *args, **extra): + self.out = self.tmp / out + p = self.run_script(*self.inputs(), "--visible", "--device", "1002:744C", "--seconds", "30", *args, **extra) + self.assertEqual(p.returncode, 0, p.stdout + p.stderr) + return p, self.wine_env()["XDG_CACHE_HOME"] + + def test_without_a_cache_each_run_gets_a_fresh_one(self): + p, cache = self.run_with_cache("run1") + self.assertEqual(cache, str(self.tmp / "run1" / "session" / "cache")) + self.assertIn("shader cache: fresh", p.stdout) + + def test_the_callers_cache_is_kept_from_run_to_run(self): + # The drivers' shader caches (NVIDIA's GLCache here) live under XDG_CACHE_HOME: a caller's one is used, so + # the second launch finds what the first compiled. + kept = self.tmp / "cache" + for n in (1, 2): + p, cache = self.run_with_cache(f"run{n}", XDG_CACHE_HOME=str(kept)) + self.assertEqual(cache, str(kept)) + self.assertIn(f"shader cache: kept in {kept}", p.stdout) + self.assertEqual((kept / "nvidia" / "GLCache").read_text(encoding="utf-8"), "x\nx\n") + + def test_cache_option_names_the_folder_or_fresh(self): + named = self.tmp / "named-cache" + _, cache = self.run_with_cache("run1", "--cache", str(named), XDG_CACHE_HOME=str(self.tmp / "other")) + self.assertEqual(cache, str(named)) + self.assertTrue((named / "nvidia" / "GLCache").is_file()) + _, cache = self.run_with_cache("run2", "--cache", "fresh", XDG_CACHE_HOME=str(self.tmp / "other")) + self.assertEqual(cache, str(self.tmp / "run2" / "session" / "cache")) + self.assertFalse((self.tmp / "other").exists()) + if __name__ == "__main__": unittest.main()