Files
VIctory-xmem/run.sh
port-um local 2d1a3ec6b7 Benchmark routes: VICTORY_BENCH_ROUTE=downtown|drive
Two fixed, reproducible routes built into the layer, so every machine
benchmarks the same places the same way without mods (game 1.0.2.1).

- victory-loader bench_route: a minimal script-native caller. Handlers
  are found by their 1.0.2.1 offsets and each checked by the id its cold
  block writes; PLAYER_PED_ID's handler is hooked for a per-frame tick in
  one elected game script thread (the approach of ViceHook, MIT). Every
  call goes through an SEH-guarded stub: a fault stops the route, not the
  game. Nothing is hooked without the variable.
- downtown: a noclip flight over a 2400 m square at (-2600, -1000), 30 m/s,
  40 m over the smoothed ground, camera sweeping, in a Blista: the city's
  densest blocks (found by a frame-rate scout over the map).
- drive: the game's AI drives a Blista around a loop on the roads in the
  hills from (-2556, 6948), with traffic (about 3.5 min a lap).
- Both set 12:00 (clock paused), held sunny weather and no wanted level.
- With VICTORY_BENCH, the run is timed from the route's start (nothing
  skipped) and bench_stats.txt names the route; run.sh does not carry
  the route over into the restart after a benchmark.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-05 14:13:21 -06:00

332 lines
16 KiB
Bash
Executable File

#!/bin/bash
# The environment as given, for restarts: nothing set below is carried over.
mapfile -d '' given < <(env -0)
restart_env=()
for kv in "${given[@]}"; do
case "$kv" in VICTORY_BENCH=*|VICTORY_BENCH_MINUTES=*|VICTORY_BENCH_ROUTE=*) ;; *) restart_env+=("$kv");; esac
done
source "$(dirname "$(readlink -f "$0")")/env.sh"
mode=$1; shift
case "$mode" in
shaders|warm|play) ;;
*) echo "usage: run.sh shaders|warm|play [VAR=value...]"; exit 1;;
esac
if [ ! -d "${GAME:-}" ] && [ ! -f "$root/run.conf" ] && [ -t 0 ]; then
# First run: ask where the game dump is and keep the answer in run.conf.
echo "No run.conf yet. Where is your game folder (the one with game_scarlett_master.exe)?"
while read -r -e -p "Game folder: " answer; do
answer=${answer/#\~/$HOME}
answer=${answer%/}
if [ -f "$answer/game_scarlett_master.exe" ]; then
GAME=$(cd "$answer" && pwd)
# Based on run.conf.default: its settings come commented out, so nothing
# changes until a line is uncommented.
{
printf 'GAME=%q\n' "$GAME"
echo
sed -E 's/^([A-Za-z0-9_]+=)/#\1/' "$root/run.conf.default" 2>/dev/null
} > "$root/run.conf"
echo "Saved to $root/run.conf"
break
fi
echo "No game_scarlett_master.exe in '$answer'. Try again (Ctrl+C to quit)."
done || exit 1
fi
if [ ! -d "${GAME:-}" ]; then echo "no game: write GAME=/path/to/your/dump in $root/run.conf"; exit 1; fi
# One run at a time: two share the prefix and the caches, the second fails
# with "Access denied (os error 5)" and its exit stops the wineserver under
# the first (both runs of 11:09 and of 11:22 on 2026-12-02 ended together).
running=$(systemctl --user list-units --state=active --no-legend 'um-*.scope' 2>/dev/null | awk '{print $1; exit}')
if [ -n "$running" ]; then
echo "already running ($running): it shows no window while it prepares shaders and pipelines."
echo "Progress: tail -f $state/run/last.log Stop it: systemctl --user stop $running"
exit 1
fi
if [ -z "${EXE:-}" ]; then
if [ -f "$root/target/x86_64-pc-windows-msvc/release/victory.exe" ]; then
exe="$root/target/x86_64-pc-windows-msvc/release/victory.exe"
else
exe="$root/target/x86_64-pc-windows-msvc/diag/victory.exe"
fi
else
exe="$EXE"
fi
if [ ! -f "$exe" ] || [ ! -f "$VK_DRIVER_FILES" ]; then echo "not built yet: run $root/setup.sh"; exit 1; fi
for dev in /sys/bus/pci/devices/*; do
if [ "$(cat "$dev/vendor")" = 0x1002 ] && [[ $(cat "$dev/class") = 0x03* ]]; then
echo on | sudo -n tee "$dev/power/control" >/dev/null 2>&1
fi
done
for k in $(env | grep -o '^VICTORY_[A-Z0-9_]*'); do unset "$k"; done
# The menu used to keep its settings in local/menu.conf, over run.conf: put
# them in run.conf, once.
if [ -f "$root/local/menu.conf" ]; then
touch "$root/run.conf"
cp "$root/run.conf" "$root/local/run.conf.before-menu"
[ -z "$(tail -c1 "$root/run.conf")" ] || echo >> "$root/run.conf"
while IFS='=' read -r name value; do
[[ $name =~ ^[A-Za-z0-9_]+$ && $value =~ ^[A-Za-z0-9._-]+$ ]] || continue
grep -q "^$name=" "$root/run.conf.default" || continue
if grep -q "^$name=" "$root/run.conf"; then
sed -i "s|^$name=.*|$name=$value|" "$root/run.conf"
elif grep -q "^#$name=" "$root/run.conf"; then
sed -i "0,/^#$name=.*/s//$name=$value/" "$root/run.conf"
else
echo "$name=$value" >> "$root/run.conf"
fi
done < "$root/local/menu.conf"
mv "$root/local/menu.conf" "$root/local/menu.conf.moved"
echo "Moved the menu's settings from local/menu.conf into run.conf."
fi
export VICTORY_BVH_BUILDER=1 VICTORY_BVH_IN_PLACE=1
# A preset is a base below run.conf and the command line.
preset=
if [ -f "$root/run.conf" ]; then
preset=$(sed -n 's/^VICTORY_PRESET=//p' "$root/run.conf" | tail -n 1)
fi
for kv in "$@"; do case "$kv" in VICTORY_PRESET=*) preset=${kv#VICTORY_PRESET=};; esac; done
case "$preset" in
"") ;;
potato|low|medium|high|ultra)
if [ -f "$root/presets/$preset.conf" ]; then
set -a
source "$root/presets/$preset.conf"
set +a
echo "preset: $preset"
else
echo "warning: preset '$preset' has no presets/$preset.conf" >&2
fi
;;
*) echo "warning: unknown preset '$preset' (potato, low, medium, high, ultra)" >&2;;
esac
mkdir -p "$state/cache" "$state/data" "$state/run" "$state/mesa-cache" "$state/vkd3d-cache"
export WINEDLLOVERRIDES='mmdevapi=b;d3d12,d3d12core,dxgi=n;d3dcompiler_47=n,b;winemenubuilder.exe=d'
# Word of a pad plugged in during play comes from udev by a socket, which does
# not reach the network namespace below: SDL is to watch /dev/input instead.
export SDL_JOYSTICK_DISABLE_UDEV=1
export VICTORY_GAME_DIR=$(win "$GAME") VICTORY_CACHE_DIR=$(win "$state/cache") VICTORY_DATA_DIR=$(win "$state/data")
export VICTORY_DXC=$(win "$root/deps/dxc") VICTORY_DXIL=$(win "$root/deps/dxc/dxil.dll")
if [ -f "$root/deps/fsr4/amd_fidelityfx_upscaler_dx12.dll" ]; then
export VICTORY_FIDELITYFX_DIR=$(win "$root/deps/fsr4") FSR4_UPGRADE=1
fi
# Quiet: Wine, vkd3d-proton and DXVK log lines are off unless VERBOSE=1.
if [ "${VERBOSE:-0}" != 1 ]; then
export WINEDEBUG=-all VKD3D_DEBUG=none VKD3D_SHADER_DEBUG=none DXVK_LOG_LEVEL=none
fi
unset MLFG_UPGRADE FSR_FG_WATERMARK || true
export RADV_PERFTEST="${RADV_PERFTEST:+$RADV_PERFTEST,}nosam"
# Re-apply user overrides from run.conf (so VICTORY_* settings in run.conf stick),
# then apply explicit CLI VAR=value arguments on top.
if [ -f "$root/run.conf" ]; then
set -a
source "$root/run.conf"
set +a
fi
export VICTORY_MENU_FILE=$(win "$root/run.conf") VICTORY_MENU_STATE=$(win "$root/local") VICTORY_MENU_DEFS=$(win "$root/run.conf.default")
for kv in "$@"; do case "$kv" in *=*) export "$kv";; esac; done
# A benchmark run (VICTORY_BENCH=rt|nort): PortWatch's settings over the
# user's for this run only.
bench=
if [ "$mode" = play ] && [[ ${VICTORY_BENCH:-} =~ ^(rt|nort)$ ]]; then
bench=$VICTORY_BENCH
unset VICTORY_FIDELITYFX_FRAMEGEN VICTORY_FSR_HOST VICTORY_UI_PLANE VICTORY_RENDER_SCALE VICTORY_PILLARBOX \
VICTORY_LOD_SCALE VICTORY_SMALL_CULL VICTORY_VEHICLE_LOD_BIAS VICTORY_QUAD_GUARD VICTORY_BVH_HARDWARE \
VICTORY_PLACED_COPIED VICTORY_KEEP_GPU_WRITTEN VICTORY_WARM LSFGVK_PROFILE LSFG_PROCESS
export CONSOLE=series-x VICTORY_FSR_4K=quality VICTORY_OUTPUT=3840x2160 VICTORY_SKIP_BINK=1 VICTORY_PANEL=full
export VICTORY_BVH_BUILDER=$([ "$bench" = rt ] && echo 1 || echo 0)
export VICTORY_BENCH_MINUTES=${VICTORY_BENCH_MINUTES:-5}
benchdir=$root/local/benchmark/$(date +%Y%m%d-%H%M%S)-$bench
mkdir -p "$benchdir"
export VICTORY_BENCH_DIR=$(win "$benchdir")
echo "Benchmark (ray tracing $([ "$bench" = rt ] && echo on || echo off)${VICTORY_BENCH_ROUTE:+, route $VICTORY_BENCH_ROUTE}): the game closes itself after $VICTORY_BENCH_MINUTES min of frames. Results: $benchdir"
fi
if [ "${MUTE:-0}" = 1 ]; then source "$root/tools/mute.sh"; mute; fi
# lsfg-vk (Lossless Scaling frame generation, a Vulkan layer) only when asked for, by naming the profile
# (lsfg-vk 2.x: LSFGVK_PROFILE=<profile name>) or the entry (lsfg-vk 1.0: LSFG_PROCESS=<exe name>) in the
# environment, run.conf or as an argument. victory.exe keeps it off in its pipeline workers (run.rs).
if [ -n "${LSFGVK_PROFILE:-}" ]; then unset DISABLE_LSFGVK; else export DISABLE_LSFGVK=1; fi
if [ -n "${LSFG_PROCESS:-}" ]; then
unset DISABLE_LSFG
export VICTORY_LSFG_V1_BRIDGE="${VICTORY_LSFG_V1_BRIDGE:-1}"
else
export DISABLE_LSFG=1
fi
if [ "${VICTORY_NATIVE_COMPUTE:-}" = 1 ] && [ -z "${VICTORY_NATIVE_COMPUTE_ONLY:-}" ]; then
export VICTORY_NATIVE_COMPUTE_ONLY=$(win "$root/config/native-compute.txt")
fi
total=$(awk '/MemTotal/{print int($2/1024)}' /proc/meminfo)
avail=$(awk '/MemAvailable/{print int($2/1024)}' /proc/meminfo)
kept=$(sudo -n cat /sys/kernel/debug/ttm/page_pool 2>/dev/null | awk '/^total/{for(i=1;i<NF;i++) if($(i+1)=="of"){print int($i/256); exit}}')
avail=$((avail+${kept:-0}))
# On 16 GiB hosts, keeping 8 GiB free leaves a ~2-4 GiB MemoryMax that forces
# immediate swapping or fails shaders/warm; default to 2 GiB unless set.
keep_free=${KEEP_FREE:-$(( total <= 18432 ? 2048 : 8192 ))}
cap=$((avail-keep_free))
if (( cap < 2048 )); then
if [ "$mode" != play ]; then echo "not enough free memory: $avail MiB available"; exit 1; fi
cap=2048
fi
if [ ! -f /proc/self/fd/1 ]; then
log=$state/run/$mode-$(date +%Y%m%d-%H%M%S).log
ln -sfn "$log" "$state/run/last.log"
exec > >(tee "$log") 2>&1
fi
echo "Runtime: ${PROTON_DIR:-$WINE_BIN} | Binary: $exe"
# Play in a world runs at 10 GiB and more of its own (besides the title's
# graphics memory, which is the driver's): under a cap below that it swaps,
# and frames take seconds (both sessions play-1201: an 8.2 GiB cap, reached,
# and all 2 GiB of swap used).
if [ "$mode" = play ] && (( cap < ${WANT_RAM:-12288} )); then
echo "warning: the game may use $cap MiB of RAM ($avail MiB available, $keep_free kept free); below ${WANT_RAM:-12288} it swaps and stutters."
echo " Close what holds memory (largest now below), or set KEEP_FREE lower in run.conf."
ps -eo rss=,comm= --sort=-rss | head -5 | awk '{printf " %6d MiB %s\n", $1/1024, $2}'
fi
if [ "$mode" = play ]; then
gtt=$(awk '{if($1>m)m=$1} END{if(m>0)print int(m/1048576)}' /sys/class/drm/card*/device/mem_info_gtt_total 2>/dev/null)
if [ -n "$gtt" ] && (( gtt < 10240 )); then
echo "warning: GPU GTT is ${gtt} MiB (Linux defaults to 50% of RAM); below 10240 MiB the driver runs out of submission memory (0x887A0005)."
echo " Add amdgpu.gttsize=11264 ttm.pages_limit=3145728 to your kernel command line and reboot."
fi
fi
# The title's memory is the card driver's own (TTM pages), which the MemoryMax
# cap below does not count. On an APU it is the PC's RAM under the kernel's
# limit for the driver (amdgpu GTT size, ttm pages_limit: 8 GiB on a 16 GB
# Steam Deck). A run that reaches it deadlocks the kernel's swap-out of GPU
# memory and the whole desktop stops, compositor included (play-20261202-112510
# and -120422: 7757 MB in use while booting, then eDP-1, plasmashell and our
# threads blocked in ttm_global_swapout; a restart of the machine each time).
# So a play run is killed short of the limit when asked: VICTORY_GPU_GUARD=1
# (or its Steam Deck name, VICTORY_STEAMDECK=1), at 85 percent of the limit by
# default. Off by default: on a host whose driver limit is low this stops runs
# that would still have fitted. VICTORY_GPU_GUARD_PERCENT sets the line;
# GPU_GUARD_PERCENT still overrides it for local testing, 0 turns it off.
gpu=
for dev in /sys/class/drm/card*/device; do
if [ "$(cat "$dev/vendor" 2>/dev/null)" = 0x1002 ] && [ -r "$dev/mem_info_gtt_used" ]; then gpu=$dev; break; fi
done
guard=0
if [ "${VICTORY_GPU_GUARD:-0}" = 1 ] || [ "${VICTORY_STEAMDECK:-0}" = 1 ]; then
guard=${VICTORY_GPU_GUARD_PERCENT:-85}
fi
guard=${GPU_GUARD_PERCENT:-$guard}
if [ "$mode" = play ] && [ -n "$gpu" ] && (( guard > 0 )); then
gtt=$(( $(cat "$gpu/mem_info_gtt_total") >> 20 ))
stop_at=$(( gtt * guard / 100 ))
if (( gtt < ${WANT_GPU_RAM:-10240} )); then
echo "warning: the kernel gives the card's driver $gtt MiB of this PC's RAM; the title held 7757 MiB of it while still booting."
echo " The run is stopped at $stop_at MiB, before the desktop freezes. More needs a higher amdgpu.gttsize and ttm pages limit."
fi
used=$(( $(cat "$gpu/mem_info_gtt_used") >> 20 ))
if (( used >= stop_at )); then
echo "not started: the card driver's memory is at $used of $gtt MiB already (stop at $stop_at). Close what holds it."
exit 1
fi
# With a higher limit the card's driver can hold most of the PC's RAM, and
# what is left runs out instead: the run is also killed when everything
# stood still for memory for VICTORY_RAM_GUARD_STALL percent of the last
# 10 s (/proc/pressure/memory, "full"; 0 turns this off).
stall=${VICTORY_RAM_GUARD_STALL:-${RAM_GUARD_STALL:-40}}
psi=${PSI_FILE:-/proc/pressure/memory}
(
why=
n=0
while [ -d "/proc/$$" ]; do
read -r used < "$gpu/mem_info_gtt_used" || break
n=$((n + 1))
# Every 5 s, where the memory stands: a stop then has its curve in the log.
if (( n % 100 == 0 )); then
avail=0 swap_total=0 swap_free=0
while read -r key kb _; do
case $key in
MemAvailable:) avail=$kb;;
SwapTotal:) swap_total=$kb;;
SwapFree:) swap_free=$kb;;
esac
done < /proc/meminfo
echo "memory: card driver $((used >> 20)) of $gtt MiB (stop at $stop_at); RAM available $((avail >> 10)) MiB, swap used $(((swap_total - swap_free) >> 10)) MiB"
fi
if [ -z "$why" ]; then
if [ -n "${UM_LAUNCHER_PID:-}" ] && [ ! -d "/proc/$UM_LAUNCHER_PID" ]; then
# The run is a scope of its own and outlives what started it.
why="what started it is gone (its window was closed, or Steam's Exit game)."
elif (( (used >> 20) >= stop_at )); then
why="the card driver's memory reached $((used >> 20)) of $gtt MiB (stop at $stop_at). Past the limit the whole desktop freezes."
elif (( stall > 0 && n % 10 == 0 )) && { read -r _; read -r _ full _; } 2>/dev/null < "$psi" \
&& [[ $full =~ ^avg10=([0-9]+) ]] && (( BASH_REMATCH[1] >= stall )); then
why="everything stood still for memory ${BASH_REMATCH[1]}% of the last 10 s (stop at $stall%): RAM and swap are used up."
fi
# Said before the kill: the terminal goes with the run.
if [ -n "$why" ]; then
echo
echo "stopped: $why"
fi
fi
if [ -n "$why" ]; then
systemctl --user kill -s KILL "um-$mode-$$.scope" 2>/dev/null && break
fi
sleep 0.05
done
) &
guard_pid=$!
fi
cd "$root"
case "$mode" in
shaders) args=(shaders "$(win "$GAME/shaders.rpf")");;
warm) args=(warm --used);;
play) args=(run "$(win "$GAME/game_scarlett_master.exe")" --execute --console "${CONSOLE:-series-x}");;
esac
rm -f "$root/local/restart"
# While the game runs the PC neither sleeps nor counts as idle.
inhibit=()
if command -v systemd-inhibit >/dev/null && systemd-inhibit --what=sleep:idle true 2>/dev/null; then
inhibit=(systemd-inhibit --what=sleep:idle --who=VIctory "--why=game running ($mode)")
fi
if [ -n "${PROTON_DIR:-}" ]; then
"${inhibit[@]}" systemd-run --user --scope --collect -q --unit "um-$mode-$$" -p "MemoryMax=${cap}M" -p MemorySwapMax=2G -- \
"$root/tools/play-wine.sh" "$exe" "${args[@]}"
else
"${inhibit[@]}" systemd-run --user --scope --collect -q --unit "um-$mode-$$" -p "MemoryMax=${cap}M" -p MemorySwapMax=2G -- \
unshare -rnm --propagation private "$root/tools/play-wine.sh" "$exe" "${args[@]}"
fi
code=$?
# The guard watches this process, which the restart below keeps: it ends here.
if [ -n "${guard_pid:-}" ]; then kill "$guard_pid" 2>/dev/null; fi
next=
if [ -f "$root/local/restart" ]; then
# The in-game menu asked for a restart with the settings it saved.
next=$(cat "$root/local/restart")
rm -f "$root/local/restart"
again=1
else
again=
fi
if [ -n "$bench" ] && [ -z "$again" ]; then
"$root/tools/bench-results.sh" "$benchdir" "$bench" "$code" "$root"
# Back to the user's own settings, unless the run was closed by hand.
if [ -f "$benchdir/bench_stats.txt" ] || (( code != 0 )); then again=1; fi
fi
if [ -n "$again" ]; then
restart_args=()
for kv in "$@"; do case "$kv" in VICTORY_BENCH=*|VICTORY_BENCH_MINUTES=*|VICTORY_BENCH_ROUTE=*) ;; *) restart_args+=("$kv");; esac; done
[[ $next =~ ^(rt|nort):([0-9]+)$ ]] && restart_args+=("VICTORY_BENCH=${BASH_REMATCH[1]}" "VICTORY_BENCH_MINUTES=${BASH_REMATCH[2]}")
echo "Restarting the game..."
# Let the old session give its memory back first: the RAM cap of the next
# one is taken from what is available now. Wait for its unit to end, then
# until available memory has stopped growing (at most 60 s).
for _ in $(seq 30); do
systemctl --user is-active -q "um-$mode-$$.scope" || break
sleep 1
done
sync
prev=0
for _ in $(seq 60); do
now=$(awk '/MemAvailable/{print int($2/1024)}' /proc/meminfo)
(( prev > 0 && now - prev < 100 )) && break
prev=$now
sleep 1
done
sleep 3
exec env -i "${restart_env[@]}" "$root/run.sh" "$mode" "${restart_args[@]}"
fi
exit $code