Files
DroidDeck/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env
T
The412Banner d16ca4d8a8 Open driver pairs: vkd3d-proton reports the driver's caps; buffer attach/release tracing; Mesa log
- With a PanVK or RADV pair (BL_OPEN_DRIVER=1), droiddeck-game-env drops the app-wide
  VKD3D_FEATURE_LEVEL=12_2 and VKD3D_SHADER_MODEL=6_9 so vkd3d-proton reports what the driver has
  (an Unreal DX12 game crashed at start on PanVK with the overrides); a game's own setting still
  wins, and launch.txt records DROIDDECK_VKD3D_CAPS=driver.
- The compositor traces, on a GPU that is not an Adreno, the first 400 dma-buf attaches and buffer
  releases by wl_buffer id and sending line, and logs any release of a buffer already released
  and not attached again (gamescope's "Compositor released us but we were not acquired" on G925).
- Per-game logs no longer set MESA_DEBUG=silent, which kept mesa.log empty.
2026-10-12 23:01:47 -04:00

474 lines
20 KiB
Python
Executable File

#!/usr/bin/python3
import json
import os
import re
import runpy
import subprocess
import sys
from pathlib import Path
SYNC_KEYS = ("BL_SYNC_FALLBACK", "BL_FSYNC_FIRST", "BL_SYNC_MIN_NOFILE", "PROTON_NO_ESYNC", "PROTON_NO_FSYNC", "PROTON_NO_NTSYNC", "WINEESYNC")
SEED_REDISTS = "/usr/local/bin/droiddeck-seed-redists"
PACK_KEYS = ("WINESERVER", "BL_SYNC_PACK", "WINEESYNC", "PROTON_NO_ESYNC", "PROTON_NO_FSYNC")
def game_id(env):
prefix = env.get("STEAM_COMPAT_DATA_PATH", "").rstrip("/")
if not prefix:
return None
value = prefix.rsplit("/", 1)[-1]
if not re.fullmatch(r"-?[0-9]+", value):
return None
number = int(value)
if not -(2**31) <= number < 2**32 or number == 0:
return None
return str(number % 2**32)
# A .NET (CoreCLR) runtime under FEX crashes in the runtime itself unless memory ordering is fully
# emulated and multiblock is off: the Stability preset.
DOTNET_FEX = {
"FEX_TSOENABLED": "1",
"FEX_VECTORTSOENABLED": "1",
"FEX_MEMCPYSETTSOENABLED": "1",
"FEX_HALFBARRIERTSOENABLED": "1",
"FEX_X87REDUCEDPRECISION": "0",
"FEX_MULTIBLOCK": "0",
}
def game_files(exe):
"""Lower-cased names of the files next to the game's executable and one directory below."""
names = set()
folder = os.path.dirname(exe)
try:
with os.scandir(folder) as top:
for entry in top:
names.add(entry.name.lower())
if entry.is_dir(follow_symlinks=False):
try:
with os.scandir(entry.path) as inner:
names.update(e.name.lower() for e in inner)
except OSError:
pass
except OSError:
pass
return names
class Game:
"""What a fix gets to look at: the executable, its launch arguments and the files beside it."""
def __init__(self, exe, args):
self.exe = exe
self.args = args
self.stem = os.path.splitext(os.path.basename(exe))[0].lower()
self.files = game_files(exe)
def has_arg(self, name):
return any(a == name or a.startswith(name + "=") for a in self.args)
# Each fix looks at a Game and returns (environment, extra arguments, note) for the games it
# applies to, or None. Adding one is writing a function and decorating it.
FIXES = []
def fix(function):
FIXES.append(function)
return function
def is_godot(game):
return game.stem + ".pck" in game.files or "godotsharp.dll" in game.files
@fix
def dotnet_memory_ordering(game):
if "coreclr.dll" in game.files:
return DOTNET_FEX, [], ".NET runtime: FEX with full TSO and without multiblock"
@fix
def godot_vulkan(game):
# Godot 4 on Direct3D 12 through VKD3D-Proton fails to create its swap chain and runs behind a
# black window; its own Vulkan renderer draws.
if is_godot(game) and not game.has_arg("--rendering-driver"):
return {}, ["--rendering-driver", "vulkan"], "Godot: Vulkan renderer"
@fix
def godot_sentry(game):
# The Sentry GDExtension's native crash handler takes the .NET runtime down under FEX. Godot
# loads it on demand and carries on without it.
sentry = sorted(n[:-4] for n in game.files if n.startswith("libsentry.windows.") and n.endswith(".dll"))
if is_godot(game) and sentry:
return {"WINEDLLOVERRIDES": ";".join(n + "=d" for n in sentry)}, [], "Godot: Sentry crash handler off"
def skip_red_launcher(command):
"""Cyberpunk 2077's prelauncher opens REDlauncher, a Qt WebEngine window that hands over to the
game half a minute later and then stays up beside it, well over a core busy for as long as the
game runs. The game is started itself instead; the prelauncher's own --launcher-skip started it
twice."""
if len(command) > 2 and os.path.basename(command[2]).lower() == "redprelauncher.exe":
game = os.path.join(os.path.dirname(command[2]), "bin", "x64", "Cyberpunk2077.exe")
if os.path.isfile(game):
print("droiddeck-game-env: REDlauncher skipped", file=sys.stderr)
return command[:2] + [game] + command[3:]
return command
def engine_fixes(exe, args):
"""Environment and extra arguments a game needs because of what it is built with."""
game = Game(exe, args)
env, extra, notes = {}, [], []
for check in FIXES:
found = check(game)
if found:
env.update(found[0])
extra += found[1]
notes.append(found[2])
return env, extra, notes
def win_components(compat, app_id):
"""The game's Windows components put into its prefix; returns their WINEDLLOVERRIDES entries."""
if not compat:
return ""
try:
module = runpy.run_path(str(Path(__file__).resolve().parent / "droiddeck-wincomponents"))
names, notes = module["apply"](compat.rstrip("/"), app_id)
for note in notes:
print("droiddeck-game-env: " + note, file=sys.stderr)
return module["overrides_value"](names)
except Exception as error:
print("droiddeck-game-env: Windows components not applied (%s)" % error, file=sys.stderr)
return ""
def apply_config(env, config, app_id, fixes=None):
if config.get("version") != 1:
raise ValueError("unsupported environment configuration")
result = env.copy()
entries = dict(config.get("shared", {}))
for name, value in (fixes or {}).items():
if name == "WINEDLLOVERRIDES" and entries.get(name, env.get(name)):
value = entries.get(name, env.get(name)) + ";" + value
entries[name] = value
entries.update(config.get("games", {}).get(app_id, {}))
for name, value in entries.items():
if not re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*", name):
raise ValueError("invalid environment name")
if value is None:
result.pop(name, None)
elif isinstance(value, str) and "\0" not in value and len(value) <= 8192:
result[name] = value
else:
raise ValueError("invalid environment value")
# The app's texture filtering (Display page), as DXVK options appended after the profiles so
# a DXVK_CONFIG entry of the user's keeps its own.
game_dxvk = config.get("dxvkConfigGames", {})
if not isinstance(game_dxvk, dict):
raise ValueError("invalid game dxvk configuration")
dxvk = game_dxvk.get(app_id, config.get("dxvkConfig"))
if dxvk is not None:
if not isinstance(dxvk, str) or "\0" in dxvk or len(dxvk) > 8192:
raise ValueError("invalid dxvk configuration")
if dxvk:
own = result.get("DXVK_CONFIG", "").strip().rstrip(";").strip()
result["DXVK_CONFIG"] = own + "; " + dxvk if own else dxvk
# An open driver pair (PanVK, RADV) on a GPU that is not an Adreno: vkd3d-proton reports the
# driver's own feature level and shader model. The app-wide 12_2 / 6_9 over-promised what PanVK
# has, and an Unreal DX12 game took paths that crashed. A game's own setting still wins.
if env.get("BL_OPEN_DRIVER") == "1":
game = config.get("games", {}).get(app_id, {}) or {}
for name in ("VKD3D_FEATURE_LEVEL", "VKD3D_SHADER_MODEL"):
if name not in game and name in result:
result.pop(name)
result["DROIDDECK_VKD3D_CAPS"] = "driver"
return result
def sync_config(config, app_id):
games = config.get("games", {})
return {
"version": config.get("version"),
"shared": {name: value for name, value in config.get("shared", {}).items() if name in SYNC_KEYS},
"games": {app_id: {name: value for name, value in games.get(app_id, {}).items() if name in SYNC_KEYS}},
}
def load_sync():
try:
return runpy.run_path(str(Path(__file__).resolve().parent / "droiddeck-esync"))
except Exception as error:
print("droiddeck-game-env: sync selection failed (%s); stock launch" % error, file=sys.stderr)
return None
def select_sync(command, env, sync=None):
try:
sync = sync if sync is not None else load_sync()
if sync is None:
return command, env
chosen, chosen_env = sync["select"](command, env)
if chosen and all(isinstance(value, str) for value in chosen) and isinstance(chosen_env, dict):
return list(chosen), dict(chosen_env)
except Exception as error:
print("droiddeck-game-env: sync selection failed (%s); stock launch" % error, file=sys.stderr)
return command, env
def record_stock(sync, command, env):
try:
if sync is not None and "record" in sync:
sync["record"](command, env)
except Exception as error:
print("droiddeck-game-env: stock launch not recorded (%s)" % error, file=sys.stderr)
def without_pack(chosen_env, env):
result = dict(chosen_env)
for name in PACK_KEYS:
if name in env:
result[name] = env[name]
else:
result.pop(name, None)
return result
SSBS_LIB = "/usr/local/lib/libssbs.so"
def force_ssbs(command, env):
"""DROIDDECK_FORCE_SSBS=1 (a game's or the shared environment): preload libssbs.so into this
Proton's Wine, which then keeps PSTATE.SSBS set across signal returns - speculative store
bypass stays allowed, as force_ssbs.diff does inside a rebuilt ntdll. ARM64 Protons only: the
library is aarch64 and only arm64 Wine has the signal path it fixes."""
if env.get("DROIDDECK_FORCE_SSBS", "").lower() not in ("1", "on", "true", "yes"):
return env
proton = os.path.dirname(command[0]) if command else ""
if not os.path.isdir(os.path.join(proton, "files/lib/wine/aarch64-unix")):
print("droiddeck-game-env: force SSBS asked for, but this Proton is not an ARM64 one; left off", file=sys.stderr)
return env
if not os.path.isfile(SSBS_LIB):
print("droiddeck-game-env: force SSBS asked for, but %s is missing; left off" % SSBS_LIB, file=sys.stderr)
return env
parts = [p for p in re.split(r"[:\s]+", env.get("LD_PRELOAD", "")) if p]
if SSBS_LIB not in parts:
env = dict(env, LD_PRELOAD=":".join(parts + [SSBS_LIB]))
print("droiddeck-game-env: force SSBS on (libssbs.so preloaded)", file=sys.stderr)
return env
def umu_capable(proton):
"""Whether this Proton's script has umu's launch without Steam: GE-Proton, Proton-CachyOS and
DroidDeck-Proton run umu.exe in place of steam.exe when UMU_ID is set; Valve's Proton does not."""
try:
with open(proton, encoding="utf-8", errors="replace") as source:
text = source.read()
except OSError:
return False
return "UMU_ID" in text and "umu.exe" in text
def start_without_steam(command, env, app_id):
"""DROIDDECK_NO_STEAM=1 (a game's Start without Steam): launch the game without Proton's
steam.exe, for games that close right away because of what it sets up. A Proton with umu's
launch gets UMU_ID - Proton still sets up the prefix, and a Steam game keeps its protonfixes
under its own id; any other Proton runs the game straight in the prefix (runinprefix)."""
if env.get("DROIDDECK_NO_STEAM", "").lower() not in ("1", "on", "true", "yes"):
return command, env
if len(command) < 3 or command[1] != "waitforexitandrun":
return command, env
if umu_capable(command[0]):
if not env.get("UMU_ID"):
steam_game = app_id is not None and int(app_id) < 2**31
env = dict(env, UMU_ID=app_id if steam_game else "0")
print("droiddeck-game-env: starting without Steam (umu.exe in place of steam.exe, UMU_ID=%s)" % env["UMU_ID"], file=sys.stderr)
return command, env
print("droiddeck-game-env: starting without Steam (this Proton has no umu launch: runinprefix)", file=sys.stderr)
return [command[0], "runinprefix"] + command[2:], env
EVALUATOR_SCRIPT = re.compile(r"(?:^|[\\/])evaluatorscript_[0-9]+\.vdf$", re.IGNORECASE)
def mark_install_scripts(command, env):
"""The client is about to run a game's install scripts (iscriptevaluator.exe): mark every process
in them but a Vulkan runtime installer as already run first, so no redistributable installer
starts - on first launch the shared ones may still be downloading, and on ARM64 Proton the VC++
ones stop with an error."""
if not any(arg.lower().replace("\\", "/").endswith("iscriptevaluator.exe") for arg in command):
return
root = env.get("STEAM_COMPAT_CLIENT_INSTALL_PATH") or str(Path.home() / ".local/share/Steam")
scripts = [str(Path(root) / arg.replace("\\", "/")) for arg in command if EVALUATOR_SCRIPT.search(arg)]
scripts = [path for path in scripts if os.path.isfile(path)]
if not scripts:
return
try:
subprocess.run([SEED_REDISTS, root], env=dict(env, DROIDDECK_SEED_SCRIPTS="\n".join(scripts)),
stdout=sys.stderr, timeout=60, check=False)
except (OSError, subprocess.SubprocessError) as error:
print("droiddeck-game-env: install scripts not marked (%s)" % error, file=sys.stderr)
class SystemGraphicsError(ValueError):
pass
def system_graphics_launch(command, env):
"""Use the APK's PowerVR compatibility files only for actual Proton game launches."""
runtime = env.get("DROIDDECK_POWERVR_RUNTIME")
if not runtime or not game_id(env) or len(command) < 3 or command[1] != "waitforexitandrun":
return command, env
directory = Path(runtime)
if not directory.is_absolute():
raise SystemGraphicsError("system Vulkan runtime path is not absolute")
# run-proton.py picks the same payload by name; Xclipse needs a DXVK within 128 bytes of push data.
payload = env.get("DROIDDECK_HYBRIS_DXVK") or "dxvk"
if not payload.startswith("dxvk") or "/" in payload:
raise SystemGraphicsError("invalid DXVK payload name: %s" % payload)
required = (
"run-proton.py", "wsi/libVkLayer_window_system_integration.so",
"wsi/VkLayer_window_system_integration.json", "lib/libVkLayer_hybris_compat.so",
*("%s/%s/%s.dll" % (payload, arch, name) for arch in ("x32", "x64")
for name in ("d3d8", "d3d9", "d3d10core", "d3d11", "dxgi")),
)
try:
for name in required:
path = directory / name
if not path.is_file() or not path.stat().st_size:
raise SystemGraphicsError("missing system Vulkan component: %s" % name)
wrapper = directory / "run-proton.py"
if not os.access(wrapper, os.X_OK):
raise SystemGraphicsError("system Vulkan Proton wrapper is not executable")
except OSError as error:
raise SystemGraphicsError("system Vulkan component is unreadable: %s" % error) from error
result = dict(env)
layers = "VK_LAYER_window_system_integration:" + env.get("DROIDDECK_HYBRIS_LAYERS", "VK_LAYER_HYBRIS_compat")
result.update({
"HYBRIS_BC_TEXTURES": "dxvk",
"VK_LAYER_PATH": ":".join(filter(None, (str(directory / "wsi"), str(directory / "lib"),
env.get("DROIDDECK_HYBRIS_LAYER_PATH")))),
"VK_INSTANCE_LAYERS": layers,
"ENABLE_GAMESCOPE_WSI": "0",
"VK_LOADER_LAYERS_DISABLE": "*gamescope*",
"DISABLE_VK_LAYER_VALVE_steam_fossilize_1": "1",
})
for name in ("DISABLE_WSI_LAYER", "PROTON_USE_WINED3D", "ENABLE_VK_LAYER_VALVE_steam_fossilize_1"):
result.pop(name, None)
return [str(wrapper), *command], result
def game_logs(command, env, app_id, now=None):
"""Detailed graphics logs for one game launch, in the session's log folder: Proton's own log,
DXVK's and vkd3d-proton's, and the Vulkan driver's startup messages. The app turns this on
(BL_GAME_LOGS=1) on a GPU that is not an Adreno, or with Setup's "Detailed graphics logs"."""
if env.get("BL_GAME_LOGS") != "1" or not env.get("BL_DEBUG_DIR") or len(command) < 3 \
or command[1] != "waitforexitandrun":
return env
import time
stamp = time.strftime("%Y%m%d-%H%M%S", time.localtime(now))
name = app_id or re.sub(r"[^A-Za-z0-9._-]+", "_", Path(command[2]).stem)[:40] or "game"
folder = Path(env["BL_DEBUG_DIR"]) / "games" / ("%s-%s" % (stamp, name))
try:
folder.mkdir(parents=True, exist_ok=True)
except OSError as error:
print("droiddeck-game-env: no game log folder (%s)" % error, file=sys.stderr)
return env
result = dict(env)
result.update({
"PROTON_LOG": "1",
"PROTON_LOG_DIR": str(folder),
"DXVK_LOG_PATH": str(folder),
"DXVK_LOG_LEVEL": env.get("DXVK_LOG_LEVEL", "info"),
"VKD3D_LOG_FILE": str(folder / "vkd3d.log"),
"VKD3D_DEBUG": env.get("VKD3D_DEBUG", "warn"),
"VKD3D_SHADER_DEBUG": env.get("VKD3D_SHADER_DEBUG", "none"),
"MESA_LOG_FILE": str(folder / "mesa.log"),
"PANVK_DEBUG": env.get("PANVK_DEBUG", "startup"),
})
try:
(folder / "launch.txt").write_text("\n".join([
"app: %s" % (app_id or "-"),
"exe: %s" % command[2],
"proton: %s" % command[0],
"started: %s" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(now)),
] + ["%s=%s" % (key, env[key]) for key in sorted(env)
if key.startswith(("VK_", "BL_VK", "BL_DRM", "BL_OPEN", "MESA_", "PANVK", "DXVK_", "VKD3D", "ZINK", "TU_", "DROIDDECK_VKD3D"))]) + "\n")
except OSError:
pass
# Whether the game's sound reaches PulseAudio: the streams 20 s and 60 s in, without waiting.
try:
subprocess.Popen(["sh", "-c", 'for t in 20 40; do sleep $t; { echo "== $(date +%T)"; '
'pactl list sink-inputs 2>&1 | head -80; } >> "$1/audio-streams.txt"; done', "audio", str(folder)],
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
start_new_session=True)
except OSError:
pass
return result
def exec_proton(command, env):
# Synchronization selection has already picked the actual Proton executable.
command, env = system_graphics_launch(command, env)
os.execvpe(command[0], command, env)
def main():
command = sys.argv[1:]
if not command:
return 2
env = os.environ.copy()
mark_install_scripts(command, env)
app_id = game_id(env)
if app_id:
waiting = len(command) > 1 and command[1] == "waitforexitandrun"
fixes = {}
if waiting:
command = skip_red_launcher(command)
fixes, extra, notes = engine_fixes(command[2], command[3:]) if len(command) > 2 else ({}, [], [])
for note in notes:
print("droiddeck-game-env: " + note, file=sys.stderr)
command += extra
components = win_components(env.get("STEAM_COMPAT_DATA_PATH", ""), app_id)
if components:
own = fixes.get("WINEDLLOVERRIDES")
fixes["WINEDLLOVERRIDES"] = components + ";" + own if own else components
path = Path.home() / ".config/droiddeck/game-environment.json"
try:
with path.open(encoding="utf-8") as source:
config = json.load(source)
if waiting:
env = apply_config(env, config, app_id, fixes)
else:
env = apply_config(env, sync_config(config, app_id), app_id)
except FileNotFoundError:
if waiting:
env = apply_config(env, {"version": 1}, app_id, fixes)
except (OSError, ValueError, TypeError, AttributeError):
print("droiddeck-game-env: invalid configuration; using inherited environment", file=sys.stderr)
env = game_logs(command, env, app_id)
env = force_ssbs(command, env)
command, env = start_without_steam(command, env, app_id)
sync = load_sync()
launch, launch_env = select_sync(command, env, sync)
if launch[0] != command[0]:
try:
exec_proton(launch, launch_env)
except OSError as error:
print("droiddeck-game-env: %s could not start (%s); stock launch" % (launch[0], error), file=sys.stderr)
launch, launch_env = command, without_pack(launch_env, env)
record_stock(sync, launch, launch_env)
exec_proton(launch, launch_env)
if __name__ == "__main__":
try:
sys.exit(main())
except SystemGraphicsError as error:
print("droiddeck-game-env: PowerVR graphics runtime unavailable (%s)" % error, file=sys.stderr)
sys.exit(1)