From 695e2f3a3ba99817cb4ebf61655ee30ee4c946cd Mon Sep 17 00:00:00 2001
From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
Date: Sat, 14 Nov 2026 03:10:34 -0500
Subject: [PATCH 1/2] Tools: the in-game run script and the offline create
preflight, config-driven
tools/ingame: one in-game run of a candidate build with evidence and a
byte-for-byte restore (run.py), as used for Model L in two games (#89).
Every path comes from a game config (examples/*.json, placeholders only)
or from arguments; nothing is specific to one PC.
- run.py: preflight, prepare (manifest, backups, aside / hash-only files,
the launcher's update, the model choice in runtime_options and
OptiScaler's UpscalerIndex, the model in the game's data folder,
settings), launch (the GPU flag per game run, released by game_hold.py
in finally), hold, shot, collect (our logs only, proof.json, launch
trace counts), restore (verified against the manifest), all --dry-run.
- offline_create.py: the harness create test of Model L on D3D12 before
any game run, and the L / M synthetic timing; an optional GPU-slot
wrapper after "--".
- make_record.py: the timing record from the runtime's own timing lines,
scenes from a JSON file.
- settings_edit.py: graphics settings backup / stage / restore (ini and
Cyberpunk 2077's UserSettings.json).
- The helpers: game_manifest, game_backup, game_restore, game_hold,
aside, check_record, set_upscaler_index, vram_log.
Refs #89
Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
---
tools/ingame/README.md | 101 ++++
tools/ingame/aside.py | 88 ++++
tools/ingame/check_record.py | 47 ++
tools/ingame/examples/cyberpunk2077.json | 22 +
tools/ingame/examples/re9.json | 22 +
tools/ingame/game_backup.py | 57 ++
tools/ingame/game_hold.py | 73 +++
tools/ingame/game_manifest.py | 89 ++++
tools/ingame/game_restore.py | 53 ++
tools/ingame/make_record.py | 119 +++++
tools/ingame/offline_create.py | 217 ++++++++
tools/ingame/run.py | 633 +++++++++++++++++++++++
tools/ingame/set_upscaler_index.py | 25 +
tools/ingame/settings_edit.py | 100 ++++
tools/ingame/vram_log.py | 60 +++
15 files changed, 1706 insertions(+)
create mode 100644 tools/ingame/README.md
create mode 100644 tools/ingame/aside.py
create mode 100644 tools/ingame/check_record.py
create mode 100644 tools/ingame/examples/cyberpunk2077.json
create mode 100644 tools/ingame/examples/re9.json
create mode 100644 tools/ingame/game_backup.py
create mode 100644 tools/ingame/game_hold.py
create mode 100644 tools/ingame/game_manifest.py
create mode 100644 tools/ingame/game_restore.py
create mode 100644 tools/ingame/make_record.py
create mode 100644 tools/ingame/offline_create.py
create mode 100644 tools/ingame/run.py
create mode 100644 tools/ingame/set_upscaler_index.py
create mode 100644 tools/ingame/settings_edit.py
create mode 100644 tools/ingame/vram_log.py
diff --git a/tools/ingame/README.md b/tools/ingame/README.md
new file mode 100644
index 00000000..33321aa1
--- /dev/null
+++ b/tools/ingame/README.md
@@ -0,0 +1,101 @@
+# In-game run tools
+
+One in-game run of a WrongGPU candidate build, with evidence and a byte-for-byte restore. Used for Model L in
+Cyberpunk 2077 and Resident Evil Requiem (#89). Windows only, Python 3, no GPU work of their own except
+`offline_create.py`.
+
+Nothing in these tools is specific to one PC. Every path comes from a game config (`run.py --config`) or from
+arguments (`offline_create.py`, `make_record.py`).
+
+## Before a game: the offline create test
+
+```
+python offline_create.py build --work
+python offline_create.py create --work --runtime-dir --model --model-sha [-- ]
+```
+
+`create` runs the interop harness once:
+- `model=L` and `executor=dx12`;
+- 600 frames, 1280x720 to 1920x1080, with a launch trace.
+
+It passes only if all of these hold:
+- the runtime ran on D3D12;
+- the timing windows came back;
+- Model L kernels appear in the trace.
+
+Run it before every game run with a new build.
+
+`timing` measures Model L against Model M on six shapes, interleaved, and writes `l-timing-harness.json`.
+
+Everything after a lone `--` wraps each GPU run, for example a GPU-slot tool.
+
+## The game run
+
+1. Copy `examples/.json` and fill in every ``:
+ - `game_root`, `exe_dir`, `exe`, `launchers`;
+ - `out`: an empty folder for this run;
+ - `launcher_package` and `payload`: from `build.py --model-l`;
+ - `model`: the file and its SHA-256;
+ - `aside`: files moved out of the game for the run, then moved back. Never opened, only hashed and renamed;
+ - `hash_only`: files hashed before and after but never touched, for example a script loader the game needs;
+ - `settings`: the file, its format (`ini` or `cp2077`) and the keys to set;
+ - `gpu_flag`: the GPU flag file;
+ - `disk_free_gb`: minimum free space per drive.
+
+ `%VARIABLES%` are expanded. `exe_dir` is relative to `game_root`.
+
+2. Run the phases:
+
+```
+python run.py --config my.json all --dry-run every phase, nothing changed
+python run.py --config my.json preflight
+python run.py --config my.json prepare
+python run.py --config my.json launch then start the game in its launcher
+python run.py --config my.json hold
+python run.py --config my.json shot ... use-m / use-l between runs (game closed)
+python run.py --config my.json collect after the game has exited
+python run.py --config my.json restore
+```
+
+What each phase does:
+- **`prepare`**:
+ - makes a manifest of the game folder;
+ - backs up our install's files and the settings file;
+ - moves the `aside` files out and hashes the `hash_only` files;
+ - installs the candidate with the launcher's `update`;
+ - sets `runtime_options model=` and OptiScaler's `[WrongGPU] UpscalerIndex`;
+ - adds the timing and launch-trace options;
+ - copies the model to `\WrongGPU\model\\model.wgcm`.
+
+ The model goes into the game's own folder, not `%LOCALAPPDATA%`, because a tool running inside a packaged
+ (MSIX) app writes new `%LOCALAPPDATA%` files to a private copy that the game never sees.
+- **`launch`**:
+ - takes the GPU flag for this one game run;
+ - starts `game_hold.py`, which releases the flag in `finally` when the game exits or never starts;
+ - starts the VRAM log.
+- **`collect`**:
+ - copies our own logs. OptiScaler's log is never read;
+ - writes `proof.json` with the model option, the model file and digest, Model L kernels, the executor, dispatch
+ counts, refusals and the launch-trace counts.
+
+ The verdict is PASS or NOT PROVEN.
+- **`restore`**:
+ - puts every backed-up file back;
+ - removes what the update added and the placed model, with the folders it created;
+ - moves the `aside` files back and checks the `hash_only` hashes;
+ - restores the settings file;
+ - verifies the game folder against the manifest.
+
+3. Timing record from the runtime's own timing lines, split by game process and scene:
+
+```
+python make_record.py --log \logs\wronggpu-fsr.log --scenes scenes.json --out record.json [--meta meta.json] [--checkout ] [--gpu-name-like "*name*"]
+```
+
+## Rules these tools keep
+
+- Only our files and the files the config names are touched.
+- Vendor files (`nvngx*`, `*dlss*`, `sl.*`, `nvapi*`) are recorded by name, size and time only. They are never opened.
+- Another mod's files are hashed at most.
+- The model stays in place until the game has exited. Only `restore` removes it.
+- Do not use these tools on games with anti-cheat.
diff --git a/tools/ingame/aside.py b/tools/ingame/aside.py
new file mode 100644
index 00000000..d366ad34
--- /dev/null
+++ b/tools/ingame/aside.py
@@ -0,0 +1,88 @@
+# SPDX-License-Identifier: MIT
+"""Move files out of a game tree for a test and put them back byte for byte (e.g. another mod's files).
+The files' contents are never parsed: only hashed (SHA-256), stat'ed and renamed (same volume: a rename keeps bytes,
+timestamps and attributes).
+
+ python aside.py out [ ...] -> moves, writes \\MANIFEST.json
+ python aside.py restore -> moves back to the original paths
+ python aside.py verify -> checks the original paths against the manifest
+"""
+import ctypes
+import hashlib
+import json
+import os
+import sys
+from ctypes import wintypes
+
+k32 = ctypes.WinDLL('kernel32', use_last_error=True)
+
+
+class FILETIME(ctypes.Structure):
+ _fields_ = [('lo', wintypes.DWORD), ('hi', wintypes.DWORD)]
+
+
+class WIN32_FILE_ATTRIBUTE_DATA(ctypes.Structure):
+ _fields_ = [('attrs', wintypes.DWORD), ('ctime', FILETIME), ('atime', FILETIME), ('mtime', FILETIME),
+ ('size_hi', wintypes.DWORD), ('size_lo', wintypes.DWORD)]
+
+
+def info(path):
+ d = WIN32_FILE_ATTRIBUTE_DATA()
+ if not k32.GetFileAttributesExW(path, 0, ctypes.byref(d)):
+ raise OSError(ctypes.get_last_error(), 'GetFileAttributesExW', path)
+ ft = lambda f: (f.hi << 32) | f.lo
+ h = hashlib.sha256()
+ with open(path, 'rb') as f: # hashed only, never parsed
+ for b in iter(lambda: f.read(1 << 20), b''):
+ h.update(b)
+ return {'size': (d.size_hi << 32) | d.size_lo, 'sha256': h.hexdigest(), 'attributes': d.attrs,
+ 'creation_filetime': ft(d.ctime), 'last_write_filetime': ft(d.mtime)}
+
+
+def out(folder, files):
+ os.makedirs(folder, exist_ok=False)
+ man = []
+ for i, src in enumerate(files):
+ src = os.path.abspath(src)
+ e = {'original_path': src, 'aside_name': '%02d_%s' % (i, os.path.basename(src))}
+ e.update(info(src))
+ dst = os.path.join(folder, e['aside_name'])
+ os.rename(src, dst) # same volume: bytes, timestamps and attributes kept
+ after = info(dst)
+ e['aside_check'] = 'ok' if all(after[x] == e[x] for x in ('size', 'sha256', 'creation_filetime', 'last_write_filetime', 'attributes')) else 'MISMATCH %r' % after
+ man.append(e)
+ print('moved %s -> %s (%s)' % (src, dst, e['aside_check']))
+ json.dump(man, open(os.path.join(folder, 'MANIFEST.json'), 'w', encoding='utf-8'), indent=2)
+ print('manifest: %s' % os.path.join(folder, 'MANIFEST.json'))
+
+
+def restore(folder):
+ man = json.load(open(os.path.join(folder, 'MANIFEST.json'), encoding='utf-8'))
+ for e in man:
+ src = os.path.join(folder, e['aside_name'])
+ if os.path.exists(e['original_path']):
+ raise SystemExit('refusing: %s exists again' % e['original_path'])
+ os.rename(src, e['original_path'])
+ print('restored %s' % e['original_path'])
+
+
+def verify(folder):
+ man = json.load(open(os.path.join(folder, 'MANIFEST.json'), encoding='utf-8'))
+ bad = 0
+ for e in man:
+ now = info(e['original_path'])
+ diff = [x for x in ('size', 'sha256', 'creation_filetime', 'last_write_filetime', 'attributes') if now[x] != e[x]]
+ bad += bool(diff)
+ print('%s %s%s' % ('ok ' if not diff else 'MISMATCH', e['original_path'], '' if not diff else ' ' + repr(diff)))
+ print('verify: %d file(s), %d mismatch(es)' % (len(man), bad))
+ return 1 if bad else 0
+
+
+if __name__ == '__main__':
+ cmd = sys.argv[1]
+ if cmd == 'out':
+ out(sys.argv[2], sys.argv[3:])
+ elif cmd == 'restore':
+ restore(sys.argv[2])
+ else:
+ sys.exit(verify(sys.argv[2]))
diff --git a/tools/ingame/check_record.py b/tools/ingame/check_record.py
new file mode 100644
index 00000000..46d5ace1
--- /dev/null
+++ b/tools/ingame/check_record.py
@@ -0,0 +1,47 @@
+# SPDX-License-Identifier: MIT
+"""Check a game's WrongGPU install record against the folder: every recorded 'copy' file's SHA-256 against the
+record's installed hash (files owned by wg / wg-optibundle only), and list the files in the exe folder's top level and
+OptiScaler\\ that the record doesn't name. Vendor-named files (nvngx*, *dlss*, sl.*, nvapi*) are never opened.
+usage: check_record.py """
+import fnmatch
+import hashlib
+import os
+import sys
+
+d = sys.argv[1]
+rec = open(os.path.join(d, 'wronggpu-setup', 'install.record'), encoding='utf-8').read().splitlines()
+owner = {}
+copies = []
+for l in rec:
+ f = l.split('\t')
+ if f[0] == 'owner' and len(f) >= 4:
+ owner[(f[1], f[2])] = f[3]
+ elif f[0] == 'copy':
+ copies.append((f[4], f[3]))
+ elif f[0] == 'disable':
+ copies.append((f[3], None))
+VENDOR = ['nvngx*', '*dlss*', 'sl.*', 'nvapi*']
+bad = 0
+named = set()
+for rel, inst in copies:
+ named.add(rel.lower())
+ p = os.path.join(d, rel)
+ own = owner.get(('copy', rel), owner.get(('disable', rel), '?'))
+ if inst is None:
+ print('%-8s %-14s %s present=%s (moved aside by the install; backup in wronggpu-setup\\backup)' % ('disable', own, rel, os.path.exists(p)))
+ continue
+ if not os.path.exists(p):
+ print('MISSING %-14s %s' % (own, rel)); bad += 1; continue
+ h = hashlib.sha256(open(p, 'rb').read()).hexdigest()
+ ok = h == inst
+ bad += not ok
+ print('%-8s %-14s %s' % ('ok' if ok else 'CHANGED', own, rel))
+print('--- not named by the record (top level and OptiScaler\\, names only):')
+for sub in ('', 'OptiScaler'):
+ base = os.path.join(d, sub)
+ for n in sorted(os.listdir(base)):
+ rel = os.path.join(sub, n) if sub else n
+ if os.path.isfile(os.path.join(base, n)) and rel.lower() not in named:
+ tag = 'vendor' if any(fnmatch.fnmatch(n.lower(), v) for v in VENDOR) else ''
+ print(' %s %s' % (rel, tag))
+print('record check: %d problem(s)' % bad)
diff --git a/tools/ingame/examples/cyberpunk2077.json b/tools/ingame/examples/cyberpunk2077.json
new file mode 100644
index 00000000..2309db1c
--- /dev/null
+++ b/tools/ingame/examples/cyberpunk2077.json
@@ -0,0 +1,22 @@
+{
+ "name": "Cyberpunk 2077",
+ "game_root": "",
+ "exe_dir": "bin\\x64",
+ "exe": "Cyberpunk2077.exe",
+ "launchers": ["REDprelauncher.exe", "REDlauncher.exe"],
+ "out": "",
+ "launcher_package": ".exe (build.py --model-l)>",
+ "payload": "",
+ "model": {"name": "L", "file": "", "sha256": ""},
+ "aside": [],
+ "hash_only": [],
+ "settings": {
+ "file": "%LOCALAPPDATA%\\CD Projekt Red\\Cyberpunk 2077\\UserSettings.json",
+ "format": "cp2077",
+ "keys": {"ResolutionScaling": "DLSS", "DLSS": "Quality", "DLSS_D": "false", "RayTracedPathTracing": "false",
+ "FrameGeneration": "Off", "DLSSFrameGen": "false", "WindowMode": "BorderlessWindowed"}
+ },
+ "gpu_flag": "",
+ "flag_owner": "ingame-run",
+ "disk_free_gb": {"C:\\": 20}
+}
diff --git a/tools/ingame/examples/re9.json b/tools/ingame/examples/re9.json
new file mode 100644
index 00000000..8572adfc
--- /dev/null
+++ b/tools/ingame/examples/re9.json
@@ -0,0 +1,22 @@
+{
+ "name": "Resident Evil Requiem",
+ "game_root": "",
+ "exe_dir": ".",
+ "exe": "re9.exe",
+ "launchers": [],
+ "out": "",
+ "launcher_package": ".exe (build.py --model-l)>",
+ "payload": "",
+ "model": {"name": "L", "file": "", "sha256": ""},
+ "aside": [],
+ "hash_only": ["dinput8.dll", "_storage_\\dinput8.dll"],
+ "settings": {
+ "file": "\\config.ini",
+ "format": "ini",
+ "keys": {"UpscalingAlgorithm": "DLSS", "UpscalingQuality_DLSS": "Quality", "RayTracingSetting": "High",
+ "FrameGeneration": "Off", "WindowMode": "Borderless"}
+ },
+ "gpu_flag": "",
+ "flag_owner": "ingame-run",
+ "disk_free_gb": {"C:\\": 20}
+}
diff --git a/tools/ingame/game_backup.py b/tools/ingame/game_backup.py
new file mode 100644
index 00000000..99b7c722
--- /dev/null
+++ b/tools/ingame/game_backup.py
@@ -0,0 +1,57 @@
+# SPDX-License-Identifier: MIT
+"""Backups before an in-game run (copies, nothing moved or changed):
+ - every file the game's WrongGPU install record names (copy entries), the record folder wronggpu-setup\\ (with its
+ backup\\, which holds the owner's earlier OptiScaler dxgi.dll: copied as a file, never opened), OptiScaler.ini,
+ wronggpu*.ini / logs in the exe folder -> \\game\\ (copy2: timestamps kept);
+ - %LOCALAPPDATA%\\WrongGPU except model\\ (list + SHA-256 in localappdata-before.json, copies in \\lad\\).
+usage: game_backup.py """
+import hashlib
+import json
+import os
+import shutil
+import sys
+
+exe_dir, out = sys.argv[1], sys.argv[2]
+sha = lambda p: hashlib.sha256(open(p, 'rb').read()).hexdigest()
+rels = set()
+for l in open(os.path.join(exe_dir, 'wronggpu-setup', 'install.record'), encoding='utf-8').read().splitlines():
+ f = l.split('\t')
+ if f[0] == 'copy':
+ rels.add(f[4])
+for dp, dn, fn in os.walk(os.path.join(exe_dir, 'wronggpu-setup')):
+ for n in fn:
+ rels.add(os.path.relpath(os.path.join(dp, n), exe_dir))
+for n in os.listdir(exe_dir):
+ low = n.lower()
+ if os.path.isfile(os.path.join(exe_dir, n)) and (low.startswith('wronggpu') or low == 'optiscaler.ini'):
+ rels.add(n)
+for sub in ('WrongGPU', os.path.join('OptiScaler', 'custom', 'WrongGPU')):
+ d = os.path.join(exe_dir, sub)
+ for dp, dn, fn in os.walk(d):
+ for n in fn:
+ rels.add(os.path.relpath(os.path.join(dp, n), exe_dir))
+copied = []
+for r in sorted(rels):
+ src = os.path.join(exe_dir, r)
+ if not os.path.isfile(src):
+ continue
+ dst = os.path.join(out, 'game', r)
+ os.makedirs(os.path.dirname(dst), exist_ok=True)
+ shutil.copy2(src, dst)
+ copied.append(r)
+lad = os.path.join(os.environ['LOCALAPPDATA'], 'WrongGPU')
+lad_list = {}
+for dp, dn, fn in os.walk(lad):
+ rel_dp = os.path.relpath(dp, lad)
+ if rel_dp.split(os.sep)[0] == 'model':
+ continue
+ for n in fn:
+ p = os.path.join(dp, n)
+ r = os.path.relpath(p, lad)
+ lad_list[r] = {'size': os.path.getsize(p), 'sha256': sha(p)}
+ dst = os.path.join(out, 'lad', r)
+ os.makedirs(os.path.dirname(dst), exist_ok=True)
+ shutil.copy2(p, dst)
+json.dump(lad_list, open(os.path.join(out, 'localappdata-before.json'), 'w'), indent=1, sort_keys=True)
+json.dump(copied, open(os.path.join(out, 'game-backup-list.json'), 'w'), indent=1)
+print('game files backed up: %d; %%LOCALAPPDATA%%\\WrongGPU files (model\\ excluded): %d' % (len(copied), len(lad_list)))
diff --git a/tools/ingame/game_hold.py b/tools/ingame/game_hold.py
new file mode 100644
index 00000000..5938c249
--- /dev/null
+++ b/tools/ingame/game_hold.py
@@ -0,0 +1,73 @@
+# SPDX-License-Identifier: MIT
+"""The GPU flag for ONE game run (a session flag must never outlive the game). Started by run.py launch after it
+took the flag: waits up to --start-s for the game process, holds while it runs, and
+releases the flag in `finally` on every path (game exit, start timeout, exception, Ctrl-C, kill of the game). The flag
+is removed only when its text is still ours.
+usage: game_hold.py --exe [--start-s 300] [--max-s 3600]"""
+import argparse
+import ctypes
+import json
+import os
+import subprocess
+import time
+
+ap = argparse.ArgumentParser()
+ap.add_argument('flag')
+ap.add_argument('owner')
+ap.add_argument('record')
+ap.add_argument('--exe', required=True, help="the game's process image name, e.g. game.exe")
+ap.add_argument('--start-s', type=int, default=300)
+ap.add_argument('--max-s', type=int, default=3600)
+a = ap.parse_args()
+state = {'status': 'waiting', 'exe': a.exe, 'holder_pid': os.getpid(), 'started_unix': time.time()}
+
+
+def save():
+ json.dump(state, open(a.record, 'w'), indent=1)
+
+
+def find_pid():
+ out = subprocess.run(['tasklist', '/fo', 'csv', '/nh', '/fi', 'IMAGENAME eq ' + a.exe], capture_output=True,
+ text=True, creationflags=0x08000000).stdout
+ for l in out.splitlines():
+ f = l.split('","')
+ if len(f) >= 2 and f[0].strip('"').lower() == a.exe.lower():
+ return int(f[1])
+ return None
+
+
+k = ctypes.WinDLL('kernel32', use_last_error=True)
+k.OpenProcess.argtypes = [ctypes.c_uint32, ctypes.c_int, ctypes.c_uint32]
+k.OpenProcess.restype = ctypes.c_void_p
+k.WaitForSingleObject.argtypes = [ctypes.c_void_p, ctypes.c_uint32]
+k.WaitForSingleObject.restype = ctypes.c_uint32
+k.CloseHandle.argtypes = [ctypes.c_void_p]
+try:
+ save()
+ t0 = time.time()
+ pid = None
+ while time.time() - t0 < a.start_s and not pid:
+ pid = find_pid()
+ if not pid:
+ time.sleep(2)
+ if not pid:
+ state['status'] = 'game did not start within %d s' % a.start_s
+ else:
+ state.update(status='held', game_pid=pid, acquired_unix=time.time())
+ save()
+ h = k.OpenProcess(0x100000, 0, pid) # SYNCHRONIZE
+ if h:
+ r = k.WaitForSingleObject(h, a.max_s * 1000)
+ k.CloseHandle(h)
+ state['status'] = 'game exited' if r == 0 else 'max hold time reached (%d s)' % a.max_s
+ else:
+ state['status'] = 'game exited before the hold'
+finally:
+ released = False
+ try:
+ if os.path.exists(a.flag) and open(a.flag, encoding='utf-8', errors='replace').read().startswith(a.owner):
+ os.remove(a.flag)
+ released = True
+ finally:
+ state.update(flag_released=released, released_unix=time.time())
+ save()
diff --git a/tools/ingame/game_manifest.py b/tools/ingame/game_manifest.py
new file mode 100644
index 00000000..a73a6d80
--- /dev/null
+++ b/tools/ingame/game_manifest.py
@@ -0,0 +1,89 @@
+# SPDX-License-Identifier: MIT
+"""Manifest of a game folder for an in-game run, and its verification afterwards.
+
+ python game_manifest.py make
+ python game_manifest.py verify (exit 1 on any difference)
+ python game_manifest.py scan (foreign-mod check only)
+
+Every file: relative path, size, mtime. SHA-256 for files up to 256 MiB outside the game's archive folders, EXCEPT
+vendor-named files (nvngx*, *dlss*, sl.*, nvapi*, NvLowLatency*), which are recorded by name, size and mtime only and
+never opened (owner's rule). scan lists names that point at another or older mod (version.dll, winmm.dll, dxgi.dll,
+OptiScaler*, *.asi, nvngx.dll, d3d12.dll in bin/x64 ...) so the tester can check them against the WrongGPU install
+record before anything runs. Read-only on the game folder.
+"""
+import fnmatch
+import hashlib
+import json
+import os
+import sys
+
+VENDOR = ['nvngx*', '*dlss*', 'sl.*', 'nvapi*', 'nvlowlatency*', 'nvperf*']
+SKIP_HASH_DIRS = ['archive', 'r6/cache', 'tools']
+HASH_MAX = 256 * 1024 * 1024
+SUSPECT = ['version.dll', 'winmm.dll', 'dxgi.dll', 'd3d12.dll', 'dinput8.dll', 'optiscaler*', '*.asi', 'nvngx.dll',
+ 'nvapi64.dll', 'wronggpu*', 'amd_fidelityfx*', 'libxess*', 'reshade*', 'dxvk*', 'winhttp.dll']
+
+
+def vendor(name):
+ n = name.lower()
+ return any(fnmatch.fnmatch(n, p) for p in VENDOR)
+
+
+def walk(root):
+ for dp, dn, fn in os.walk(root):
+ dn.sort()
+ for n in sorted(fn):
+ p = os.path.join(dp, n)
+ yield os.path.relpath(p, root).replace('\\', '/'), p
+
+
+def entry(rel, p):
+ st = os.stat(p)
+ e = {'size': st.st_size, 'mtime': int(st.st_mtime)}
+ low = rel.lower()
+ if vendor(os.path.basename(rel)):
+ e['sha256'] = 'not read (vendor file)'
+ elif st.st_size <= HASH_MAX and not any(low.startswith(d + '/') for d in SKIP_HASH_DIRS):
+ h = hashlib.sha256()
+ with open(p, 'rb') as f:
+ for b in iter(lambda: f.read(1 << 20), b''):
+ h.update(b)
+ e['sha256'] = h.hexdigest()
+ return e
+
+
+def make(root, out):
+ m = {rel: entry(rel, p) for rel, p in walk(root)}
+ json.dump({'root': root, 'files': m}, open(out, 'w', encoding='utf-8'), indent=0, sort_keys=True)
+ print('manifest: %d files -> %s' % (len(m), out))
+
+
+def verify(root, man):
+ old = json.load(open(man, encoding='utf-8'))['files']
+ new = {rel: entry(rel, p) for rel, p in walk(root)}
+ added = sorted(set(new) - set(old))
+ gone = sorted(set(old) - set(new))
+ changed = sorted(r for r in set(old) & set(new) if old[r] != new[r])
+ for tag, lst in (('added', added), ('removed', gone), ('changed', changed)):
+ for r in lst:
+ print('%-8s %s' % (tag, r))
+ print('verify: %d files; added %d, removed %d, changed %d' % (len(new), len(added), len(gone), len(changed)))
+ return 1 if (added or gone or changed) else 0
+
+
+def scan(root):
+ hits = [rel for rel, _ in walk(root) if any(fnmatch.fnmatch(os.path.basename(rel).lower(), p) for p in SUSPECT)
+ and not rel.lower().startswith('archive/')]
+ for h in hits:
+ print('check: ' + h)
+ print('scan: %d name(s) to check against the WrongGPU install record' % len(hits))
+
+
+if __name__ == '__main__':
+ cmd, root = sys.argv[1], sys.argv[2]
+ if cmd == 'make':
+ make(root, sys.argv[3])
+ elif cmd == 'verify':
+ sys.exit(verify(root, sys.argv[3]))
+ else:
+ scan(root)
diff --git a/tools/ingame/game_restore.py b/tools/ingame/game_restore.py
new file mode 100644
index 00000000..7f7983a4
--- /dev/null
+++ b/tools/ingame/game_restore.py
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: MIT
+"""After an in-game run, for any exe folder (the game root itself or a subfolder such as \\bin\\x64): put back every file of the
+backup (copy2), remove the files the update added under \\wronggpu-setup\\previous\\ (listed as 'added' by
+the manifest check, file by file, then their now-empty folders), and bring %LOCALAPPDATA%\\WrongGPU (model\\ excluded)
+back to localappdata-before.json. Other added files are listed, not removed.
+usage: game_restore.py """
+import hashlib
+import json
+import os
+import shutil
+import sys
+
+root, exe_dir, a4, verify_txt = sys.argv[1:5]
+sha = lambda p: hashlib.sha256(open(p, 'rb').read()).hexdigest()
+copied = json.load(open(os.path.join(a4, 'backup', 'game-backup-list.json')))
+for r in copied:
+ shutil.copy2(os.path.join(a4, 'backup', 'game', r), os.path.join(exe_dir, r))
+print('restored from backup: %d files' % len(copied))
+rel_exe = os.path.relpath(exe_dir, root).replace(os.sep, '/')
+prefix = ('' if rel_exe == '.' else rel_exe + '/') + 'wronggpu-setup/previous/'
+added = [l.split(None, 1)[1].strip() for l in open(verify_txt, encoding='utf-8') if l.startswith('added')]
+removed, dirs = [], set()
+for rel in added:
+ if not rel.startswith(prefix):
+ print('NOT REMOVED (outside %s): %s' % (prefix, rel))
+ continue
+ p = os.path.join(root, rel.replace('/', os.sep))
+ os.remove(p)
+ removed.append(rel)
+ d = os.path.dirname(p)
+ while os.path.normcase(d) != os.path.normcase(os.path.join(exe_dir, 'wronggpu-setup')):
+ dirs.add(d)
+ d = os.path.dirname(d)
+for d in sorted(dirs, key=len, reverse=True):
+ if os.path.isdir(d) and not os.listdir(d):
+ os.rmdir(d)
+print('removed added files: %d; empty folders removed: %d' % (len(removed), sum(1 for d in dirs if not os.path.exists(d))))
+lad = os.path.join(os.environ['LOCALAPPDATA'], 'WrongGPU')
+before = json.load(open(os.path.join(a4, 'backup', 'localappdata-before.json')))
+now = {}
+for dp, dn, fn in os.walk(lad):
+ if os.path.relpath(dp, lad).split(os.sep)[0] == 'model':
+ continue
+ for n in fn:
+ now[os.path.relpath(os.path.join(dp, n), lad)] = os.path.join(dp, n)
+changed = [r for r in before if r not in now or sha(now[r]) != before[r]['sha256']]
+extra = sorted(set(now) - set(before))
+for r in changed:
+ dst = os.path.join(lad, r)
+ os.makedirs(os.path.dirname(dst), exist_ok=True)
+ shutil.copy2(os.path.join(a4, 'backup', 'lad', r), dst)
+print('%%LOCALAPPDATA%%\\WrongGPU: restored %d changed file(s): %s; files added since the backup (left, listed): %s'
+ % (len(changed), changed, extra))
diff --git a/tools/ingame/make_record.py b/tools/ingame/make_record.py
new file mode 100644
index 00000000..f22bf4e3
--- /dev/null
+++ b/tools/ingame/make_record.py
@@ -0,0 +1,119 @@
+# SPDX-License-Identifier: MIT
+"""The in-game timing record of a run (e.g. Model L against Model M in one session), from the runtime's own timing
+lines in our front-end log only ("timing, last 300 dispatches: network GPU (max ) ms [enc0 .., Swin ..,
+dec0 .., post ..] ( frames); TOTAL(t3-t0) (max ) ms"): the runtime's D3D12 timestamps in the game's
+command list, each line the mean over its 300-dispatch window. Split by game process and by scene (wall-clock
+boundaries you take from run.py's session log and screenshots). No other source of numbers.
+
+ python make_record.py --log --scenes --out
+ [--meta ] [--checkout ] [--gpu-name-like ]
+
+scenes.json: a list of {"label", "model", "pid", "scene", "from", "to"} (times HH:MM:SS; a window counts when it
+starts in [from, to)). meta.json: free fields copied into the record (game, settings, model files, notes such as which
+post the run used). --checkout: the build's source checkout (its HEAD goes in as build_commit). --gpu-name-like: the
+display adapter whose driver version goes in (Win32_VideoController name filter, e.g. "*9070*")."""
+import argparse
+import json
+import re
+import statistics
+import subprocess
+import sys
+
+LINE = re.compile(r'^\[(\d\d:\d\d:\d\d)\.\d+ (\d+)\].*timing, last 300 dispatches: network GPU ([0-9.]+) \(max ([0-9.]+)\) ms '
+ r'\[enc0 ([0-9.]+), Swin ([0-9.]+), dec0 ([0-9.]+), post ([0-9.]+)\] \((\d+) frames\); '
+ r'TOTAL\(t3-t0\) ([0-9.]+) \(max ([0-9.]+)\) ms')
+DESTROYED = re.compile(r'^\[[0-9:.]+ (\d+)\].*destroyed after (\d+) dispatches')
+FAILED = re.compile(r'^\[[0-9:.]+ (\d+)\].*(?:dispatch failed|evaluate failed|(\d+) failed)')
+
+
+def p95(v):
+ s = sorted(v)
+ return s[min(len(s) - 1, int(round(0.95 * (len(s) - 1))))]
+
+
+def stats(v):
+ return {'median_ms': round(statistics.median(v), 3), 'p95_ms': round(p95(v), 3), 'max_ms': round(max(v), 3),
+ 'min_ms': round(min(v), 3)}
+
+
+def parse(log):
+ rows, disp, failed = [], {}, {}
+ for l in open(log, encoding='utf-8', errors='replace'):
+ m = LINE.match(l)
+ if m:
+ g = [float(x) for x in m.group(3, 4, 5, 6, 7, 8)] + [int(m.group(9))] + [float(x) for x in m.group(10, 11)]
+ rows.append({'time': m.group(1), 'pid': m.group(2), 'net': g[0], 'net_max': g[1], 'enc0': g[2], 'Swin': g[3],
+ 'dec0': g[4], 'post': g[5], 'frames': g[6], 'total': g[7], 'total_max': g[8]})
+ continue
+ m = DESTROYED.match(l)
+ if m:
+ disp[m.group(1)] = disp.get(m.group(1), 0) + int(m.group(2))
+ continue
+ m = FAILED.match(l)
+ if m:
+ failed[m.group(1)] = failed.get(m.group(1), 0) + (int(m.group(2)) if m.group(2) else 1)
+ return rows, disp, failed
+
+
+def gpu_driver(like):
+ if not like:
+ return None
+ ps = ("(Get-CimInstance Win32_VideoController | Where-Object { $_.Name -like '%s' } | Select-Object -First 1 | "
+ "ForEach-Object { $_.Name + ' | driver ' + $_.DriverVersion })" % like.replace("'", "''"))
+ return subprocess.run(['powershell', '-NoProfile', '-Command', ps], capture_output=True, text=True,
+ creationflags=0x08000000).stdout.strip() or None
+
+
+def main(argv):
+ ap = argparse.ArgumentParser(description=__doc__.split('\n')[0])
+ ap.add_argument('--log', required=True, help='our front-end log (wronggpu-fsr.log) copied by run.py collect')
+ ap.add_argument('--scenes', required=True, help='scenes.json (see the docstring)')
+ ap.add_argument('--out', required=True, help='the record to write (JSON)')
+ ap.add_argument('--meta', help='meta.json: fields copied into the record')
+ ap.add_argument('--checkout', help="the build's source checkout (build_commit)")
+ ap.add_argument('--gpu-name-like', help='display adapter name filter for the driver version')
+ a = ap.parse_args(argv)
+ scenes = json.load(open(a.scenes, encoding='utf-8'))
+ rows, disp, failed = parse(a.log)
+ commit = None
+ if a.checkout:
+ commit = subprocess.run(['git', '-C', a.checkout, 'rev-parse', 'HEAD'], capture_output=True,
+ text=True).stdout.strip() or None
+ out = {
+ 'timer': "the runtime's D3D12 timestamps in the game's command list (timing_log=1, timing_stages=1): each sample "
+ "is one 'timing, last 300 dispatches' line = the mean over 300 frames; stage spans [0] start of the "
+ "network -> [1] first Swin kernel -> [2] dec0 -> [3] post -> [4] after the last kernel; network_total = "
+ "[0]->[4]; TOTAL_t3_t0 = the frame's full D3D12 span",
+ 'statistics': 'over the 300-frame windows of each scene: median, p95, min of the window means; max_ms = the '
+ 'largest window mean; the largest single frame per window is in the log (max ...) and given as '
+ 'worst_frame_network_ms / worst_frame_total_ms',
+ }
+ if a.meta:
+ out.update(json.load(open(a.meta, encoding='utf-8')))
+ out.update({'gpu_driver': gpu_driver(a.gpu_name_like), 'build_commit': commit, 'dispatches_per_process': disp,
+ 'failed_dispatches_per_process': failed, 'log': a.log, 'scenes': {}})
+ for s in scenes:
+ w = [r for r in rows if r['pid'] == str(s['pid']) and s['from'] <= r['time'] < s['to']]
+ if not w:
+ print('no timing windows for scene %r' % s['label'])
+ continue
+ out['scenes'][s['label']] = {
+ 'model': s['model'], 'pid': str(s['pid']), 'scene': s.get('scene', ''), 'windows': len(w),
+ 'frames': sum(r['frames'] for r in w), 'from': w[0]['time'], 'to': w[-1]['time'],
+ 'enc0': stats([r['enc0'] for r in w]), 'Swin': stats([r['Swin'] for r in w]),
+ 'dec0': stats([r['dec0'] for r in w]), 'post': stats([r['post'] for r in w]),
+ 'network_total': stats([r['net'] for r in w]), 'TOTAL_t3_t0': stats([r['total'] for r in w]),
+ 'worst_frame_network_ms': max(r['net_max'] for r in w), 'worst_frame_total_ms': max(r['total_max'] for r in w)}
+ with open(a.out, 'w', encoding='utf-8', newline='\n') as f:
+ json.dump(out, f, indent=1)
+ for k, s in out['scenes'].items():
+ print('%-22s n=%3d net %s enc0 %s Swin %s dec0 %s post %s' % (
+ k, s['windows'], s['network_total']['median_ms'], s['enc0']['median_ms'], s['Swin']['median_ms'],
+ s['dec0']['median_ms'], s['post']['median_ms']))
+ print('driver:', out['gpu_driver'], '| commit:', commit, '| dispatches:', disp, '| failed:', failed)
+ print('record', a.out)
+ return 0
+
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv[1:]))
diff --git a/tools/ingame/offline_create.py b/tools/ingame/offline_create.py
new file mode 100644
index 00000000..8a737249
--- /dev/null
+++ b/tools/ingame/offline_create.py
@@ -0,0 +1,217 @@
+# SPDX-License-Identifier: MIT
+"""Offline create + run test of Model L on the D3D12 executor before any game run (issue #89), and the synthetic
+timing harness (test_interop_d3d12.exe hipbench) for Model L and Model M.
+
+ python offline_create.py build --work
+ compile the harness from this checkout (MSVC, CPU only)
+ python offline_create.py create --work --runtime-dir --model --model-sha [-- ]
+ one short run: model=L, executor=dx12, the converted Model L file copied into \\data\\L\\model.wgcm,
+ 600 frames 1280x720 -> 1920x1080; launch trace; PASS / FAIL in \\create-L.json
+ python offline_create.py timing --work --runtime-dir --model ... --model-sha ... [--m-model-dir ]
+ [--rounds 3] [--shapes "1080p Quality,..."] [-- ]
+ Model L and Model M interleaved per shape, one run each; record \\l-timing-harness.json
+
+--runtime-dir is a built runtime folder (wronggpu_runtime.dll and the kernels, build.py --model-l). Everything after
+a lone "--" is a command prefix that wraps each GPU run (e.g. a GPU-slot tool: `-- python gpu_slot.py --cap-s 170 --`);
+without it the harness runs directly. --tmp sets TMP / TEMP for the runs, --rocm-path ROCM_PATH.
+
+The model data folder is under --work, not %LOCALAPPDATA%: inside a packaged (MSIX) app new files there are private
+to the package. Model M uses --m-model-dir (default %LOCALAPPDATA%\\WrongGPU\\model, read only)."""
+import argparse
+import hashlib
+import json
+import os
+import re
+import shutil
+import statistics
+import subprocess
+import sys
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+REPO = os.path.dirname(os.path.dirname(HERE))
+SHAPES = [('1080p Quality', 1280, 720, 1920, 1080), ('1080p Ultra Quality', 1477, 831, 1920, 1080),
+ ('1080p DLAA', 1920, 1080, 1920, 1080), ('1440p Quality', 1707, 960, 2560, 1440),
+ ('1440p Ultra Quality', 1969, 1108, 2560, 1440), ('1440p DLAA', 2560, 1440, 2560, 1440)]
+NOWIN = 0x08000000 | 0x00004000 # CREATE_NO_WINDOW | BELOW_NORMAL_PRIORITY_CLASS
+L_KERNELS = ('l_', 'lev/', 'swin_block_c64_add_ex_nc')
+VSWHERE = os.path.join(os.environ.get('ProgramFiles(x86)', r'C:\Program Files (x86)'), 'Microsoft Visual Studio',
+ 'Installer', 'vswhere.exe')
+A = None # the parsed arguments
+WRAP = [] # the command prefix after "--"
+
+
+def harness():
+ return os.path.join(A.work, 'harness', 'test_interop_d3d12.exe')
+
+
+def env_base():
+ e = {k: v for k, v in os.environ.items()
+ if not (any(s in k.upper() for s in ('HIP', 'LLVM', 'ROCM', 'HSA')) and k.upper() not in ('HIP_PLATFORM', 'HIP_PATH_72'))
+ and not k.upper().startswith('WG_')}
+ e.update(AMD_COMGR_CACHE='0', WG_TEST_DEFAULT_EXECUTOR='1')
+ if A.rocm_path:
+ e['ROCM_PATH'] = A.rocm_path
+ if A.tmp:
+ os.makedirs(A.tmp, exist_ok=True)
+ e.update(TMP=A.tmp, TEMP=A.tmp)
+ return e
+
+
+def build():
+ exe = harness()
+ os.makedirs(os.path.dirname(exe), exist_ok=True)
+ vs = subprocess.check_output([A.vswhere, '-latest', '-find', r'Common7\Tools\VsDevCmd.bat'], text=True).splitlines()[0]
+ src = os.path.join(A.src, 'clean', 'runtime', 'interop', 'tests', 'test_interop_d3d12.cpp')
+ bat = os.path.join(A.work, 'harness', 'build.bat')
+ open(bat, 'w').write('@call "%s" -arch=x64 -host_arch=x64 >nul\r\ncd /d "%s"\r\n'
+ 'cl /nologo /std:c++17 /O2 /EHsc /D_CRT_SECURE_NO_WARNINGS "%s" /Fe:test_interop_d3d12.exe '
+ 'd3d12.lib dxgi.lib\r\n' % (vs, os.path.dirname(exe), src))
+ r = subprocess.run(['cmd.exe', '/d', '/c', bat], capture_output=True, text=True, creationflags=NOWIN)
+ print(r.stdout[-3000:], r.stderr[-2000:])
+ print('harness', 'built' if os.path.isfile(exe) else 'NOT built', exe)
+ return 0 if os.path.isfile(exe) else 1
+
+
+def need_run_args():
+ miss = [n for n in ('runtime_dir', 'model', 'model_sha') if not getattr(A, n)]
+ if miss:
+ raise SystemExit('missing: %s' % ', '.join('--' + m.replace('_', '-') for m in miss))
+ if not os.path.isfile(harness()):
+ raise SystemExit('no harness at %s (run build first)' % harness())
+
+
+def l_data():
+ d = os.path.join(A.work, 'data')
+ os.makedirs(os.path.join(d, 'L'), exist_ok=True)
+ dst = os.path.join(d, 'L', 'model.wgcm')
+ if not os.path.isfile(dst):
+ shutil.copy2(A.model, dst)
+ got = hashlib.sha256(open(dst, 'rb').read()).hexdigest()
+ if got != A.model_sha.lower():
+ raise SystemExit('L model digest %s != %s' % (got, A.model_sha))
+ return d
+
+
+def options(model, data, stages):
+ o = 'kernels_dir=%s;timing_log=1;timing_stages=%d;executor=dx12' % (A.runtime_dir, 1 if stages else 0)
+ if model == 'L':
+ return 'model=L;model_dir=%s;' % data + o
+ return 'model=M;model_dir=%s;' % A.m_model_dir + o
+
+
+def run(name, model, shape, frames, stages, trace=None):
+ label, rw, rh, ow, oh = shape
+ e = dict(env_base(), WG_TEST_RENDER_WIDTH=str(rw), WG_TEST_RENDER_HEIGHT=str(rh), WG_TEST_OUTPUT_WIDTH=str(ow),
+ WG_TEST_OUTPUT_HEIGHT=str(oh))
+ if trace:
+ e.update(WG_LAUNCH_TRACE=trace, WG_LAUNCH_TRACE_MAX='60000')
+ log = os.path.join(A.work, '%s.log' % name)
+ dll = os.path.join(A.runtime_dir, 'wronggpu_runtime.dll')
+ with open(log, 'w', encoding='utf-8') as f:
+ rc = subprocess.call(WRAP + [harness(), dll, 'hipbench', options(model, l_data(), stages), str(frames)], env=e,
+ stdout=f, stderr=subprocess.STDOUT, timeout=900, creationflags=NOWIN)
+ text = open(log, encoding='utf-8', errors='replace').read()
+ tot = [float(x) for x in re.findall(r'TOTAL\(t3-t0\) ([0-9.]+)', text)]
+ net = re.findall(r'network GPU ([0-9.]+) \(max [0-9.]+\) ms \[([^\]]*)\]', text)
+ ex = re.findall(r'network runtime ready: executor (\w+)', text)
+ took = re.findall(r'could not take[^\n]*', text)
+ errs = re.findall(r'(?:ERROR|error|refus)[^\n]*', text)
+ ok = rc == 0 and len(tot) >= 2 and ex and ex[-1] == 'dx12' and not took and re.search(r'^PASS\s*$', text, re.M)
+ return {'name': name, 'model': model, 'shape': label, 'render': [rw, rh], 'output': [ow, oh], 'rc': rc, 'ok': bool(ok),
+ 'executor': ex[-1] if ex else None, 'total_windows_ms': tot, 'network_windows': net,
+ 'value_ms': statistics.median(tot[1:]) if len(tot) >= 2 else None, 'errors': errs[:5], 'took': took[:3],
+ 'log': log}
+
+
+def create():
+ need_run_args()
+ trace = os.path.join(A.work, 'create-trace.txt')
+ if os.path.exists(trace):
+ os.remove(trace)
+ res = run('create-L', 'L', SHAPES[0], 600, True, trace)
+ counts = {}
+ if os.path.isfile(trace):
+ for l in open(trace, encoding='utf-8', errors='replace'):
+ f = l.split()
+ if f:
+ counts[f[0]] = counts.get(f[0], 0) + 1
+ lk = {k: v for k, v in counts.items() if k.startswith(L_KERNELS)}
+ text = open(res['log'], encoding='utf-8', errors='replace').read()
+ res['launch_trace'] = {'file': trace, 'kernels': len(counts), 'model_l_launches': lk}
+ res['lines'] = [l.strip()[:400] for l in text.splitlines()
+ if re.search(r'network runtime ready|network kernels|model=L|L[\\/]model\.wgcm|ERROR|refus|PASS|FAIL', l)][:20]
+ res['verdict'] = 'PASS' if res['ok'] and lk else 'FAIL'
+ json.dump(res, open(os.path.join(A.work, 'create-L.json'), 'w'), indent=1)
+ print(json.dumps({k: res[k] for k in ('verdict', 'rc', 'executor', 'value_ms', 'errors', 'took', 'launch_trace', 'lines')},
+ indent=1))
+ return 0 if res['verdict'] == 'PASS' else 1
+
+
+def build_commit():
+ try:
+ return subprocess.check_output(['git', '-C', A.src, 'rev-parse', 'HEAD'], text=True).strip()
+ except (OSError, subprocess.CalledProcessError):
+ return None
+
+
+def timing():
+ need_run_args()
+ only = A.shapes.split(',') if A.shapes else None
+ shapes = [s for s in SHAPES if not only or s[0] in only]
+ runs = []
+ for rnd in range(A.rounds):
+ for shape in shapes:
+ for model in ('L', 'M'):
+ r = run('t%d_%s_%s' % (rnd, model, shape[0].replace(' ', '_')), model, shape, 1500, False)
+ runs.append(r)
+ print('%s %-20s round %d: %s ms ok=%s' % (model, shape[0], rnd, r['value_ms'], r['ok']), flush=True)
+ stages = {}
+ for shape in shapes:
+ for model in ('L', 'M'):
+ r = run('stages_%s_%s' % (model, shape[0].replace(' ', '_')), model, shape, 1500, True)
+ stages['%s %s' % (model, shape[0])] = r['network_windows'][1:]
+ out = {'definition': 'TOTAL(t3-t0) of the runtime timing log, test_interop_d3d12.exe hipbench 1500 frames, executor '
+ 'dx12 (native D3D12), first 300-frame window dropped, median of the other windows per run, '
+ 'median over rounds interleaved L / M per shape',
+ 'runtime': os.path.join(A.runtime_dir, 'wronggpu_runtime.dll'), 'build_commit': build_commit(),
+ 'l_model_sha256': A.model_sha.lower(), 'm_model': os.path.join(A.m_model_dir, 'model.wgcm'), 'shapes': {},
+ 'stage_windows': stages, 'runs': runs}
+ for shape in shapes:
+ for model in ('L', 'M'):
+ v = [r['value_ms'] for r in runs if r['model'] == model and r['shape'] == shape[0] and r['ok']]
+ out['shapes']['%s %s' % (model, shape[0])] = {'median_ms': statistics.median(v) if v else None,
+ 'rounds_ms': v, 'n': len(v)}
+ p = os.path.join(A.work, 'l-timing-harness.json')
+ json.dump(out, open(p, 'w'), indent=1)
+ print(json.dumps(out['shapes'], indent=1))
+ print('record', p)
+ return 0
+
+
+def main(argv):
+ global A, WRAP
+ if '--' in argv:
+ i = argv.index('--')
+ argv, WRAP = argv[:i], argv[i + 1:]
+ ap = argparse.ArgumentParser(description=__doc__.split('\n')[0])
+ ap.add_argument('cmd', choices=['build', 'create', 'timing'])
+ ap.add_argument('--work', required=True, help='output folder (harness, model data copy, logs, records)')
+ ap.add_argument('--src', default=REPO, help='the checkout with the harness source (default: this one)')
+ ap.add_argument('--runtime-dir', help='built runtime folder (wronggpu_runtime.dll + kernels, with Model L)')
+ ap.add_argument('--model', help='the converted Model L file (model.wgcm)')
+ ap.add_argument('--model-sha', help="the Model L file's SHA-256")
+ ap.add_argument('--m-model-dir', default=os.path.join(os.environ.get('LOCALAPPDATA', ''), 'WrongGPU', 'model'),
+ help="Model M's data folder (timing; read only)")
+ ap.add_argument('--rounds', type=int, default=3)
+ ap.add_argument('--shapes', help='comma list of shape names (default: all six)')
+ ap.add_argument('--tmp', help='TMP / TEMP for the runs')
+ ap.add_argument('--rocm-path', help='ROCM_PATH for the runs')
+ ap.add_argument('--vswhere', default=VSWHERE)
+ A = ap.parse_args(argv)
+ A.work = os.path.abspath(A.work)
+ os.makedirs(A.work, exist_ok=True)
+ return {'build': build, 'create': create, 'timing': timing}[A.cmd]()
+
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv[1:]))
diff --git a/tools/ingame/run.py b/tools/ingame/run.py
new file mode 100644
index 00000000..1f0ef8f4
--- /dev/null
+++ b/tools/ingame/run.py
@@ -0,0 +1,633 @@
+# SPDX-License-Identifier: MIT
+"""One in-game run of WrongGPU with a record and a byte-for-byte restore: a game folder's manifest, backups, foreign
+files moved aside or hashed, the candidate installed through the launcher's command line, a model choice (e.g. Model L,
+issue #89), screenshots, the runtime's own proof lines and timing, then everything put back and verified.
+
+ python run.py --config .json preflight checks only (always safe)
+ python run.py --config .json prepare [--dry-run] manifest, backups, aside, update, model choice, settings
+ python run.py --config .json use-m | use-l the model choice (the game closed): runtime_options and
+ OptiScaler's [WrongGPU] UpscalerIndex
+ python run.py --config .json launch [--dry-run] GPU flag (released by game_hold.py when the game exits),
+ VRAM log, the launcher's `play`
+ python run.py --config .json hold reports the game's pid once game_hold.py holds it
+ python run.py --config .json shot screenshot of the desktop into \\shots
+ python run.py --config .json collect [--logs ] our logs (the game closed), proof lines, verdict
+ python run.py --config .json restore [--dry-run] everything back, the placed model removed, verified
+ python run.py --config .json all --dry-run every phase as a dry run
+
+The config (examples/*.json, see README.md) names every path: the game, the exe folder, the output folder, the
+candidate launcher and its payload, the model file and its SHA-256, the files moved aside or only hashed, the settings
+file and its keys, the GPU flag. Nothing here is specific to one PC.
+
+Game-folder rules: only our files and the files the config names are touched; other mods' files are never opened
+(hashed at most); vendor files (nvngx*, *dlss*, sl.*, nvapi*) are recorded by name only; OptiScaler.log is never read.
+The model goes into the game's data folder (\\WrongGPU\\model\\\\model.wgcm), which the front end looks
+at first. Not %LOCALAPPDATA%: inside a packaged (MSIX) app, new files there go to the package's private copy, which the
+game never sees. It stays there until the game has exited; restore removes it and the folders it created."""
+import argparse
+import datetime
+import glob
+import hashlib
+import json
+import os
+import re
+import shutil
+import subprocess
+import sys
+import time
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+NOWIN = 0x08000000 | 0x00004000 # CREATE_NO_WINDOW | BELOW_NORMAL_PRIORITY_CLASS
+sha = lambda p: hashlib.sha256(open(p, 'rb').read()).hexdigest()
+
+
+def load_config(path):
+ """The game's config (JSON). %VARIABLES% are expanded; exe_dir is relative to game_root, other relative paths are
+ relative to the config file. A value still in is an unfilled placeholder (an error)."""
+ with open(path, encoding='utf-8') as f:
+ c = json.load(f)
+ base = os.path.dirname(os.path.abspath(path))
+
+ def ab(p, rel_to=base):
+ if not p:
+ return p
+ if '<' in p and '>' in p:
+ raise SystemExit('%s: fill in the placeholder %r' % (path, p))
+ p = os.path.expandvars(p)
+ return os.path.normpath(p if os.path.isabs(p) else os.path.join(rel_to, p))
+ for k in ('game_root', 'out', 'launcher_package', 'payload', 'aside_parent', 'gpu_flag'):
+ if k in c:
+ c[k] = ab(c[k])
+ c['exe_dir'] = ab(c.get('exe_dir') or '.', c['game_root'])
+ c['model']['file'] = ab(c['model']['file'])
+ if c.get('settings'):
+ c['settings']['file'] = ab(c['settings']['file'])
+ return c
+
+
+def _config_path(argv):
+ if '--config' not in argv or argv.index('--config') + 1 >= len(argv):
+ raise SystemExit('--config .json is required (see tools/ingame/examples)')
+ return argv[argv.index('--config') + 1]
+
+
+CFG = load_config(_config_path(sys.argv)) if __name__ == '__main__' else None
+if CFG:
+ G = CFG['game_root']
+ X = CFG['exe_dir']
+ EXE = os.path.join(X, CFG['exe'])
+ A = CFG['out']
+ PKG_EXE = CFG['launcher_package']
+ PAYLOAD = CFG['payload']
+ MODEL_NAME = CFG['model'].get('name', 'L')
+ MODEL_SRC = CFG['model']['file']
+ MODEL_SHA = CFG['model']['sha256'].lower()
+ MODEL_DST = os.path.join(X, 'WrongGPU', 'model', MODEL_NAME, 'model.wgcm')
+ FLAG = CFG['gpu_flag']
+ SANDBOX = os.path.dirname(FLAG)
+ ASIDE_FILES = [os.path.join(G, p) for p in CFG.get('aside', [])]
+ ASIDE_PARENT = CFG.get('aside_parent') or os.path.join(os.path.dirname(G), '_wronggpu_aside')
+ FOREIGN_HASH = [os.path.join(G, p) for p in CFG.get('hash_only', [])]
+ OUR_LOGS = { # our own logs only (OptiScaler.log is OptiScaler's: never copied or read)
+ 'wronggpu-fsr.log': os.path.join(X, 'OptiScaler', 'custom', 'WrongGPU', 'ffx', 'wronggpu-fsr.log'),
+ 'wronggpu-diagnostics.jsonl': os.path.join(X, 'OptiScaler', 'custom', 'WrongGPU', 'ffx',
+ 'wronggpu-diagnostics-amd_fidelityfx_dx12.jsonl'),
+ 'wronggpu-rr.log': os.path.join(X, 'wronggpu-rr.log'),
+ }
+ # the installed payload against the build's payload (installed name <- payload name)
+ PAYLOAD_MAP = {'wronggpu_runtime.dll': 'wronggpu_runtime.dll',
+ r'OptiScaler\custom\WrongGPU\ffx\amd_fidelityfx_upscaler_dx12.dll': 'amd_fidelityfx_dx12.dll'}
+ OWNER = CFG.get('flag_owner', 'ingame-run') + ' (%s, %s)' % (CFG.get('name', CFG['exe']), MODEL_NAME)
+ GAME_EXE = CFG['exe'].lower()
+ PROC = os.path.splitext(CFG['exe'])[0]
+ LAUNCHERS = tuple(x.lower() for x in CFG.get('launchers', []))
+ DISK_FREE_GB = CFG.get('disk_free_gb', {})
+ S = CFG.get('settings') or {}
+ os.makedirs(A, exist_ok=True)
+ # the runtime's timing lines and the launch trace (one line per D3D12 kernel launch, capped; the runtime sets
+ # WG_LAUNCH_TRACE from the option, since a store launcher starts the game without our environment)
+ TRACE = os.path.join(A, 'launch-trace.txt')
+ OPTS_EXTRA = 'timing_log=1;timing_stages=1;launch_trace=%s;launch_trace_max=%d' % (
+ TRACE, int(CFG.get('launch_trace_max', 200000)))
+
+
+def settings_cmd(verb):
+ """settings_edit.py backup / stage / restore of the config's settings file (None: no settings file)."""
+ if not S:
+ return None
+ tool = os.path.join(HERE, 'settings_edit.py')
+ backup = os.path.join(A, os.path.basename(S['file']) + '.before')
+ if verb == 'stage':
+ return [tool, 'stage', S['file'], S['format']] + ['%s=%s' % kv for kv in S['keys'].items()]
+ return [tool, verb, S['file'], backup]
+
+
+def clean_env():
+ e = {k: v for k, v in os.environ.items()
+ if not (any(s in k.upper() for s in ('HIP', 'LLVM', 'ROCM', 'HSA')) and k.upper() not in ('HIP_PLATFORM', 'HIP_PATH_72'))
+ and not k.upper().startswith('WG_')}
+ if CFG.get('rocm_path'):
+ e['ROCM_PATH'] = CFG['rocm_path']
+ return e
+
+
+class Run:
+ def __init__(self, dry):
+ self.dry = dry
+ self.a = os.path.join(A, 'dry-' + datetime.datetime.now().strftime('%Y%m%d-%H%M%S')) if dry else A
+ os.makedirs(self.a, exist_ok=True)
+ self.state_path = os.path.join(A, 'state.json')
+ self.state = json.load(open(self.state_path)) if os.path.exists(self.state_path) else {}
+ self.problems = []
+
+ def log(self, s):
+ line = '%s %s%s' % (datetime.datetime.now().strftime('%H:%M:%S'), 'DRY ' if self.dry else '', s)
+ print(line, flush=True)
+ with open(os.path.join(self.a, 'session.log'), 'a', encoding='utf-8') as f:
+ f.write(line + '\n')
+
+ def need(self, ok, what):
+ self.log(('ok ' if ok else 'FAIL ') + what)
+ if not ok:
+ self.problems.append(what)
+ return ok
+
+ def save(self, **kv):
+ if self.dry:
+ return
+ self.state.update(kv)
+ json.dump(self.state, open(self.state_path, 'w'), indent=1)
+
+ def py(self, name, args, out=None, check=True):
+ """Run a Python tool (no window, below normal); output to \\.txt. In a dry run: printed only."""
+ cmd = [sys.executable] + args
+ if self.dry:
+ self.log('would run: ' + ' '.join('"%s"' % c if ' ' in c else c for c in cmd))
+ return 0
+ path = out or os.path.join(self.a, name + '.txt')
+ with open(path, 'w', encoding='utf-8') as f:
+ r = subprocess.run(cmd, stdout=f, stderr=subprocess.STDOUT, creationflags=NOWIN, env=clean_env())
+ self.log('%s exit %d -> %s' % (name, r.returncode, path))
+ if check and r.returncode != 0:
+ raise SystemExit('%s failed (exit %d), see %s' % (name, r.returncode, path))
+ return r.returncode
+
+ def wg(self, name, verb_args):
+ """The candidate launcher's command line (--out / --log into )."""
+ cmd = [PKG_EXE] + verb_args + ['--out=' + os.path.join(self.a, name + '.json'),
+ '--log=' + os.path.join(self.a, name + '.log')]
+ if self.dry:
+ self.log('would run: ' + ' '.join('"%s"' % c if ' ' in c else c for c in cmd))
+ return 0
+ r = subprocess.run(cmd, creationflags=NOWIN, env=clean_env())
+ self.log('launcher %s exit %d' % (' '.join(verb_args[:1]), r.returncode))
+ return r.returncode
+
+
+def tasks():
+ out = subprocess.run(['tasklist', '/fo', 'csv', '/nh'], capture_output=True, text=True).stdout
+ return {l.split('","')[0].strip('"').lower(): int(l.split('","')[1]) for l in out.splitlines() if l.count('","') >= 2}
+
+
+def memory_used():
+ import ctypes
+
+ class MS(ctypes.Structure):
+ _fields_ = [('l', ctypes.c_ulong), ('load', ctypes.c_ulong)] + [(n, ctypes.c_ulonglong) for n in
+ ('a', 'b', 'c', 'd', 'e', 'f', 'g')]
+ m = MS()
+ m.l = ctypes.sizeof(MS)
+ ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(m))
+ return m.load
+
+
+def ini_runtime_options(text):
+ m = re.search(r'^runtime_options\s*=\s*(.*)$', text, re.M)
+ return m.group(1).strip() if m else None
+
+
+TOOLS = ('game_manifest.py', 'check_record.py', 'aside.py', 'vram_log.py', 'game_backup.py', 'game_restore.py',
+ 'game_hold.py', 'set_upscaler_index.py', 'settings_edit.py')
+tool = lambda n: os.path.join(HERE, n)
+
+
+# ---- phases -----------------------------------------------------------------------------------------------------------
+
+def preflight(r):
+ t = tasks()
+ for p in (GAME_EXE,) + LAUNCHERS:
+ r.need(p not in t, 'not running: ' + p)
+ r.need(not os.path.exists(FLAG) or OWNER.split(' ')[0] in open(FLAG, encoding='utf-8', errors='replace').read(),
+ 'GPU flag free (or ours): ' + FLAG)
+ req = glob.glob(os.path.join(SANDBOX, 'GPU-REQUEST*')) + glob.glob(os.path.join(SANDBOX, 'GPU-SLOTS-PAUSED*'))
+ r.need(not req, 'no GPU request / pause file: %s' % req)
+ mu = memory_used()
+ r.need(mu < 80, 'memory used %d %% (< 80)' % mu)
+ for d, gb in sorted(DISK_FREE_GB.items()):
+ free = shutil.disk_usage(d).free / 2**30
+ r.need(free > gb, '%s free %.1f GB (> %s)' % (d, free, gb))
+ r.need(os.path.isfile(EXE), 'game: ' + EXE)
+ r.need(os.path.isfile(PKG_EXE), 'candidate launcher: ' + PKG_EXE)
+ for inst, pay in PAYLOAD_MAP.items():
+ r.need(os.path.isfile(os.path.join(PAYLOAD, pay)), 'payload file: ' + pay)
+ r.need(os.path.isfile(MODEL_SRC) and sha(MODEL_SRC) == MODEL_SHA,
+ 'model %s file %s sha256 %s' % (MODEL_NAME, MODEL_SRC, MODEL_SHA))
+ r.need(not os.path.exists(MODEL_DST) or sha(MODEL_DST) == MODEL_SHA or r.state.get('model_placed'),
+ 'no other file at ' + MODEL_DST)
+ for f in ASIDE_FILES:
+ r.need(os.path.isfile(f) or r.state.get('aside'), 'aside candidate present: ' + f)
+ ini = os.path.join(X, 'wronggpu.ini')
+ r.need(os.path.isfile(ini) and ini_runtime_options(open(ini, encoding='utf-8', errors='replace').read()) is not None,
+ 'runtime_options line in ' + ini)
+ for n in TOOLS:
+ r.need(os.path.isfile(tool(n)), 'tool ' + n)
+ if S:
+ r.need(os.path.isfile(S['file']), 'settings file ' + S['file'])
+ r.need(S.get('format') in ('ini', 'cp2077'), 'settings format %r (ini or cp2077)' % S.get('format'))
+ # foreign-mod scan and the install record (read-only; vendor files by name only)
+ for name, args in (('scan', [tool('game_manifest.py'), 'scan', G]),
+ ('record-check', [tool('check_record.py'), X])):
+ out = subprocess.run([sys.executable] + args, capture_output=True, text=True, creationflags=NOWIN).stdout
+ open(os.path.join(r.a, 'preflight-%s.txt' % name), 'w', encoding='utf-8').write(out)
+ r.log('%s: %s' % (name, (out.strip().splitlines() or ['(no output)'])[-1][:200]))
+ return not r.problems
+
+
+def prepare(r):
+ if not preflight(r):
+ raise SystemExit('preflight: %s' % r.problems)
+ if r.state.get('prepared'):
+ raise SystemExit('already prepared (state.json); run restore first')
+ # 0. (the GPU flag is taken per game run by launch and released by game_hold.py when the game exits)
+ # 1. manifest, record, backups (copies; nothing changed)
+ r.py('manifest-before', [tool('game_manifest.py'), 'make', G, os.path.join(r.a, 'manifest-before.json')])
+ r.py('record-before', [tool('check_record.py'), X])
+ r.py('backup', [tool('game_backup.py'), X, os.path.join(r.a, 'backup')])
+ if S:
+ r.py('settings-backup', settings_cmd('backup'))
+ foreign_hashes(r, 'before')
+ r.save(backup=True)
+ # 2. the config's foreign files aside (hash + stat + rename; contents never opened)
+ aside_dir = os.path.join(ASIDE_PARENT, 'aside-' + datetime.datetime.now().strftime('%Y%m%d-%H%M'))
+ if ASIDE_FILES:
+ r.py('aside-out', [tool('aside.py'), 'out', aside_dir] + ASIDE_FILES)
+ r.save(aside=aside_dir)
+ else:
+ r.log('no files to move aside for this game')
+ # 3. the candidate through the launcher's update verb
+ rc = r.wg('update', ['update', EXE])
+ if rc != 0:
+ raise SystemExit('update failed (exit %d): restore next' % rc)
+ r.save(updated=True)
+ for inst, pay in PAYLOAD_MAP.items():
+ if r.dry:
+ r.log('would compare %s with payload %s' % (inst, pay))
+ else:
+ r.need(sha(os.path.join(X, inst)) == sha(os.path.join(PAYLOAD, pay)), 'installed %s = payload %s' % (inst, pay))
+ # 4. the model choice: runtime_options model= (executor kept) and OptiScaler's UpscalerIndex
+ if not r.dry and not os.path.isfile(os.path.join(X, 'wronggpu.ini')):
+ raise SystemExit('no wronggpu.ini in %s after the update: restore next' % X)
+ set_options(r, MODEL_NAME, OPTS_EXTRA)
+ # 5. the model file where the front end and the runtime look first (the game's WrongGPU\model\)
+ place_model(r)
+ # 6. the game's graphics settings named in the config
+ if S:
+ r.py('settings-stage', settings_cmd('stage'))
+ r.save(prepared=True)
+ r.log("prepared: next launch, then start the game in its launcher, then hold")
+
+
+def set_options(r, model='L', extra=''):
+ """runtime_options = model=; in the exe folder's
+ wronggpu.ini, and OptiScaler.ini [WrongGPU] UpscalerIndex (0 = WrongGPU Model M, 1 = WrongGPU Model L: the host's
+ version choice, which the front end appends after runtime_options)."""
+ ini = os.path.join(X, 'wronggpu.ini')
+ raw = open(ini, 'rb').read()
+ nl = b'\r\n' if b'\r\n' in raw else b'\n'
+ lines = raw.split(nl)
+ idx = [i for i, l in enumerate(lines) if re.match(rb'^runtime_options\s*=', l)]
+ if len(idx) != 1:
+ raise SystemExit('wronggpu.ini: %d runtime_options lines' % len(idx))
+ old = lines[idx[0]].split(b'=', 1)[1].decode()
+ items = [i for i in re.split(r'[;,]', old) if i.strip() and not re.match(r'\s*(model|timing_log|timing_stages)=', i)]
+ new = ';'.join(['model=' + model] + items + [e for e in extra.split(';') if e])
+ r.log('wronggpu.ini runtime_options: %r -> %r' % (old, new))
+ if not r.dry:
+ lines[idx[0]] = b'runtime_options=' + new.encode()
+ open(ini, 'wb').write(nl.join(lines))
+ r.save(ini_model=model)
+ r.py('upscaler-index', [tool('set_upscaler_index.py'), os.path.join(X, 'OptiScaler.ini'),
+ '1' if model == 'L' else '0'])
+
+
+def reinstall(r):
+ """Within a prepared session: the launcher's update with the (rebuilt) candidate, the payload check, then the
+ model choice again (the update may rewrite both ini files)."""
+ if not r.dry and not r.state.get('prepared'):
+ raise SystemExit('not prepared')
+ if GAME_EXE in tasks():
+ raise SystemExit('the game is running')
+ rc = r.wg('update2', ['update', EXE])
+ if rc != 0:
+ raise SystemExit('update failed (exit %d)' % rc)
+ for inst, pay in PAYLOAD_MAP.items():
+ if not r.dry:
+ r.need(sha(os.path.join(X, inst)) == sha(os.path.join(PAYLOAD, pay)), 'installed %s = payload %s' % (inst, pay))
+ set_options(r, MODEL_NAME, OPTS_EXTRA)
+
+
+def foreign_hashes(r, when):
+ """SHA-256 + size + mtime of the foreign files (the config's hash_only: e.g. a script loader the game needs), before and after;
+ 'after' must equal 'before'."""
+ if not FOREIGN_HASH:
+ return
+ res = {p: {'sha256': sha(p), 'size': os.path.getsize(p), 'mtime': os.path.getmtime(p)} if os.path.isfile(p) else None
+ for p in FOREIGN_HASH}
+ path = os.path.join(r.a, 'foreign-%s.json' % when)
+ if r.dry:
+ r.log('foreign files (%s): %s' % (when, {os.path.basename(os.path.dirname(p)) + '/' + os.path.basename(p):
+ (v or {}).get('sha256', 'absent')[:16] for p, v in res.items()}))
+ return
+ json.dump(res, open(path, 'w'), indent=1)
+ if when == 'after':
+ before = json.load(open(os.path.join(r.a, 'foreign-before.json')))
+ r.need(before == res, 'foreign files unchanged (%d)' % len(res))
+
+
+def place_model(r):
+ """Copy the model file to MODEL_DST; the folders it creates are recorded (restore removes them, deepest
+ first, only when empty)."""
+ created, d = [], os.path.dirname(MODEL_DST)
+ while not os.path.isdir(d):
+ created.append(d)
+ d = os.path.dirname(d)
+ if r.dry:
+ r.log('would copy %s -> %s (folders created: %s)' % (MODEL_SRC, MODEL_DST, created))
+ return
+ os.makedirs(os.path.dirname(MODEL_DST), exist_ok=True)
+ shutil.copy2(MODEL_SRC, MODEL_DST)
+ r.need(sha(MODEL_DST) == MODEL_SHA, 'placed %s sha256 %s' % (MODEL_DST, MODEL_SHA))
+ r.save(model_placed=MODEL_DST, model_dirs_created=created)
+
+
+def launch(r):
+ if not r.dry and not r.state.get('prepared'):
+ raise SystemExit('not prepared')
+ vcsv = os.path.join(r.a, 'vram.csv')
+ vcmd = [sys.executable, tool('vram_log.py'), PROC, vcsv, '--interval', '5', '--max-min', '40']
+ if r.dry:
+ r.log('would start (background): ' + ' '.join(vcmd))
+ else:
+ subprocess.Popen(vcmd, stdout=open(os.path.join(r.a, 'vram.txt'), 'w'), stderr=subprocess.STDOUT,
+ creationflags=NOWIN)
+ r.log('VRAM log started -> ' + vcsv)
+ # the GPU flag for this one game run; game_hold.py releases it in `finally` when the game exits (or does not start)
+ rec = os.path.join(r.a, 'hold-%s.json' % datetime.datetime.now().strftime('%H%M%S'))
+ hcmd = [sys.executable, tool('game_hold.py'), FLAG, OWNER.split(' ')[0], rec, '--exe', GAME_EXE]
+ if r.dry:
+ r.log('would take %s as %r, start (background) %s, then play' % (FLAG, OWNER, ' '.join(hcmd)))
+ r.wg('play', ['play', EXE])
+ return
+ fd = os.open(FLAG, os.O_CREAT | os.O_EXCL | os.O_WRONLY) # fails if anyone holds the GPU
+ try:
+ os.write(fd, ('%s %s\n' % (OWNER, datetime.datetime.now().isoformat(timespec='seconds'))).encode())
+ finally:
+ os.close(fd)
+ try:
+ subprocess.Popen(hcmd, stdout=open(rec[:-5] + '.txt', 'w'), stderr=subprocess.STDOUT, creationflags=NOWIN)
+ r.log('flag taken; game_hold.py releases it when the game exits (%s)' % rec)
+ rc = r.wg('play', ['play', EXE])
+ if rc != 0:
+ raise SystemExit('play failed (exit %d)' % rc)
+ except BaseException:
+ release_flag(r) # game_hold.py would also release it after its start timeout
+ raise
+ r.save(hold_record=rec)
+ r.log("next: start the game in its launcher; then: run.py hold (reports the game pid)")
+
+
+def release_flag(r):
+ try:
+ if os.path.exists(FLAG) and open(FLAG, encoding='utf-8', errors='replace').read().startswith(OWNER.split(' ')[0]):
+ os.remove(FLAG)
+ r.log('flag released')
+ except OSError as e:
+ r.log('flag release failed: %s' % e)
+
+
+def hold(r):
+ """Report the game pid once game_hold.py holds it (the holder itself releases the flag at the game's exit)."""
+ if r.dry:
+ r.log('would wait for game_hold.py to report the game pid')
+ return
+ rec = r.state.get('hold_record')
+ t0 = time.time()
+ st = {}
+ while time.time() - t0 < 320:
+ st = json.load(open(rec)) if rec and os.path.exists(rec) else {}
+ if st.get('status') != 'waiting' and st:
+ break
+ time.sleep(2)
+ r.log('hold: %s' % st)
+ if st.get('game_pid'):
+ r.save(game_pid=st['game_pid'])
+
+
+def shot(r, label):
+ d = os.path.join(r.a, 'shots')
+ os.makedirs(d, exist_ok=True)
+ n = len(glob.glob(os.path.join(d, '*.png'))) + 1
+ path = os.path.join(d, '%02d-%s.png' % (n, re.sub(r'[^A-Za-z0-9_-]', '_', label)))
+ from PIL import ImageGrab
+ img = ImageGrab.grab(all_screens=True)
+ if r.dry:
+ r.log('screenshot works: %dx%d (dry run: saved to %s)' % (img.size[0], img.size[1], path))
+ img.save(path)
+ r.log('screenshot %s (%dx%d)' % (path, img.size[0], img.size[1]))
+
+
+def proof_patterns():
+ """(key, regex over our logs); the run passes only with the required ones (required_keys)."""
+ sub = re.escape(MODEL_NAME)
+ return [
+ ('model_option', r'model=' + sub),
+ ('model_loaded', r'(model|loaded)[^\n]*[\\/]' + sub + r'[\\/]model\.wgcm'),
+ ('model_digest', MODEL_SHA[:16]),
+ ('l_kernels', r'l_(swin_block|ops|post|coef|linear|project|enc_values|dec_project|post_recon)\w*'),
+ ('executor_dx12', r'executor dx12'),
+ ('executor_hip', r'executor hip|the HIP executor (runs|takes)'),
+ ('refused', r'refus|not part of this build|Model L runs on the HIP backend'),
+ ('dispatches', r'destroyed after (\d+) dispatches'),
+ ('failed', r'(\d+) failed|dispatch failed|evaluate failed'),
+ ('network_ready', r'network runtime ready[^\n]*'),
+ ]
+
+
+def required_keys():
+ if MODEL_NAME == 'L': # Model L must load its own file, launch its own kernels and run on D3D12
+ return ('model_option', 'model_loaded', 'l_kernels', 'executor_dx12', 'network_ready')
+ return ('model_option', 'executor_dx12', 'network_ready')
+
+
+def proof(text):
+ res = {}
+ for key, rx in proof_patterns():
+ hits = [m.group(0) for m in re.finditer(rx, text)]
+ res[key] = {'count': len(hits), 'first': hits[0][:300] if hits else '', 'lines': []}
+ if hits:
+ res[key]['lines'] = [l[:400] for l in text.splitlines() if re.search(rx, l)][:5]
+ disp = [int(m) for m in re.findall(r'destroyed after (\d+) dispatches', text)]
+ res['dispatch_total'] = sum(disp)
+ verdict = []
+ for k in required_keys():
+ if not res[k]['count']:
+ verdict.append('missing: ' + k)
+ if res['executor_hip']['count']:
+ verdict.append('HIP executor named (the run must be on D3D12)')
+ if res['refused']['count']:
+ verdict.append('a refusal line')
+ if not disp or not sum(disp):
+ verdict.append('no dispatches')
+ res['verdict'] = 'PASS' if not verdict else 'NOT PROVEN: ' + '; '.join(verdict)
+ return res
+
+
+def collect(r, logs=None):
+ d = logs or os.path.join(r.a, 'logs')
+ if not logs:
+ if r.dry:
+ for name, src in OUR_LOGS.items():
+ r.log('would copy (PowerShell Copy-Item) %s -> %s' % (src, os.path.join(d, name)))
+ return
+ if GAME_EXE in tasks():
+ raise SystemExit('the game is still running: close it first (CloseMainWindow)')
+ os.makedirs(d, exist_ok=True)
+ for name, src in OUR_LOGS.items():
+ ps = "Copy-Item -LiteralPath '%s' -Destination '%s' -ErrorAction Stop" % (src, os.path.join(d, name))
+ rc = subprocess.run(['powershell.exe', '-NoProfile', '-Command', ps], creationflags=NOWIN).returncode
+ r.log('copied %s: exit %d' % (name, rc))
+ text = ''
+ for n in sorted(os.listdir(d)):
+ if n.endswith('.log'):
+ text += open(os.path.join(d, n), encoding='utf-8', errors='replace').read() + '\n'
+ res = proof(text)
+ vcsv = os.path.join(r.a if not logs else d, 'vram.csv')
+ if os.path.isfile(vcsv):
+ import csv
+ rows = [x for x in csv.DictReader(open(vcsv)) if x.get('game_dedicated_MiB') not in ('', '0.0', None)]
+ v = [float(x['game_dedicated_MiB']) for x in rows]
+ if v:
+ res['vram'] = {'samples': len(v), 'from': rows[0]['time'], 'to': rows[-1]['time'], 'first': v[0], 'last': v[-1],
+ 'min': min(v), 'max': max(v)}
+ res['model_file'] = {'path': MODEL_DST, 'sha256': sha(MODEL_DST) if os.path.isfile(MODEL_DST) else None,
+ 'expected': MODEL_SHA}
+ res['shots'] = sorted(glob.glob(os.path.join(r.a, 'shots', '*.png')))
+ tr = os.path.join(d if logs else r.a, 'launch-trace.txt')
+ if os.path.isfile(tr):
+ counts = {}
+ for l in open(tr, encoding='utf-8', errors='replace'):
+ f = l.split()
+ if f:
+ counts[f[0]] = counts.get(f[0], 0) + 1
+ lk = {k: v for k, v in counts.items() if k.startswith(('l_', 'lev/')) or k == 'swin_block_c64_add_ex_nc'}
+ res['launch_trace'] = {'file': tr, 'lines': sum(counts.values()), 'kernels': len(counts), 'model_l_launches': lk,
+ 'all': dict(sorted(counts.items()))}
+ r.log('launch trace: %d lines, %d kernels; Model L kernels: %s' % (sum(counts.values()), len(counts), lk))
+ if not logs:
+ release_flag(r) # the game has exited (checked above); normally game_hold.py has released it already
+ out = os.path.join(r.a, 'proof.json')
+ json.dump(res, open(out, 'w'), indent=1)
+ for k, _ in proof_patterns():
+ r.log('%-15s %4d %s' % (k, res[k]['count'], res[k]['first'][:160]))
+ r.log('dispatches %d; VRAM %s' % (res['dispatch_total'], res.get('vram')))
+ r.log('verdict: %s (%s)' % (res['verdict'], out))
+
+
+def restore(r):
+ st = r.state
+ if r.dry:
+ r.log('would require the game closed, then (in this order, each only if prepare did it):')
+ elif GAME_EXE in tasks():
+ raise SystemExit('the game is still running: close it first')
+ if st.get('backup') or r.dry:
+ r.py('verify-after-run', [tool('game_manifest.py'), 'verify', G, os.path.join(A, 'manifest-before.json')],
+ check=False)
+ r.py('game-restore', [tool('game_restore.py'), G, X, A, os.path.join(A, 'verify-after-run.txt')])
+ if st.get('model_placed') or r.dry:
+ if r.dry:
+ r.log('would remove %s (and the folders place_model created, when empty)' % MODEL_DST)
+ else:
+ placed = st['model_placed'] if isinstance(st['model_placed'], str) else MODEL_DST
+ if os.path.isfile(placed):
+ os.remove(placed)
+ for d in st.get('model_dirs_created', []): # deepest first
+ if os.path.isdir(d) and not os.listdir(d):
+ os.rmdir(d)
+ r.log('removed the placed model (%s) and created folders %s' % (placed, st.get('model_dirs_created', [])))
+ r.save(model_placed=False, model_dirs_created=[])
+ if st.get('aside') or r.dry:
+ a = st.get('aside', os.path.join(ASIDE_PARENT, 'aside-'))
+ r.py('aside-restore', [tool('aside.py'), 'restore', a])
+ r.py('aside-verify', [tool('aside.py'), 'verify', a])
+ r.save(aside=None, aside_restored=a)
+ if st.get('backup') or r.dry:
+ if S:
+ r.py('settings-restore', settings_cmd('restore'))
+ foreign_hashes(r, 'after')
+ r.py('verify-final', [tool('game_manifest.py'), 'verify', G, os.path.join(A, 'manifest-before.json')],
+ check=False)
+ r.py('record-after', [tool('check_record.py'), X])
+ if r.dry:
+ r.log('would release ' + FLAG + ' if it is still ours')
+ else:
+ release_flag(r)
+ r.save(prepared=False, flag=False, backup=False, updated=False, ini_model=None)
+ r.log("restored; check verify-final.txt (expected: only the game's and OptiScaler's own logs changed) and "
+ 'record-after.txt')
+
+
+def main():
+ ap = argparse.ArgumentParser(description=__doc__.split('\n')[0])
+ ap.add_argument('--config', required=True, help='the game config (JSON; examples/*.json)')
+ ap.add_argument('phase', choices=['preflight', 'prepare', 'place-model', 'reinstall', 'use-m', 'use-l', 'launch',
+ 'hold', 'shot', 'collect', 'restore', 'all'])
+ ap.add_argument('label', nargs='?', default='game')
+ ap.add_argument('--dry-run', action='store_true')
+ ap.add_argument('--logs', help='collect: logs already copied (proof extraction only)')
+ a = ap.parse_args()
+ if a.phase == 'all' and not a.dry_run:
+ raise SystemExit('all is a dry run only (use --dry-run)')
+ r = Run(a.dry_run or a.phase == 'preflight')
+ r.log('phase %s (%s)' % (a.phase, r.a))
+ if a.phase == 'preflight':
+ ok = preflight(r)
+ r.log('preflight %s' % ('PASS' if ok else 'FAIL: %s' % r.problems))
+ return 0 if ok else 1
+ if a.phase == 'all':
+ for f in (prepare, launch, hold):
+ try:
+ f(r)
+ except SystemExit as e:
+ r.log('stopped: %s' % e)
+ r.problems.append(str(e))
+ shot(r, 'dry-run')
+ collect(r)
+ restore(r)
+ r.log('dry run %s' % ('PASS' if not r.problems else 'FAIL: %s' % r.problems))
+ return 0 if not r.problems else 1
+ if a.phase in ('use-m', 'use-l'): # the comparison run in the same session and settings (the game must be closed)
+ if GAME_EXE in tasks():
+ raise SystemExit('the game is running')
+ set_options(r, 'M' if a.phase == 'use-m' else 'L', OPTS_EXTRA)
+ elif a.phase == 'shot':
+ shot(r, a.label)
+ elif a.phase == 'collect':
+ collect(r, a.logs)
+ else:
+ {'prepare': prepare, 'place-model': place_model, 'reinstall': reinstall, 'launch': launch, 'hold': hold,
+ 'restore': restore}[a.phase](r)
+ return 1 if r.problems else 0
+
+
+if __name__ == '__main__':
+ sys.exit(main())
diff --git a/tools/ingame/set_upscaler_index.py b/tools/ingame/set_upscaler_index.py
new file mode 100644
index 00000000..3147dd1d
--- /dev/null
+++ b/tools/ingame/set_upscaler_index.py
@@ -0,0 +1,25 @@
+# SPDX-License-Identifier: MIT
+"""Set UpscalerIndex in the [WrongGPU] section of the game's OptiScaler.ini (our install's file, backed up by
+game_backup.py; restored by game_restore.py). 0 = WrongGPU Model M, 1 = WrongGPU Model L. Only that one line changes; bytes and line endings otherwise kept.
+usage: set_upscaler_index.py """
+import re
+import sys
+
+path, idx = sys.argv[1], sys.argv[2].encode()
+raw = open(path, 'rb').read()
+nl = b'\r\n' if b'\r\n' in raw else b'\n'
+lines = raw.split(nl)
+sec = None
+hits = []
+for i, l in enumerate(lines):
+ s = l.strip()
+ if s.startswith(b'[') and s.endswith(b']'):
+ sec = s
+ elif sec == b'[WrongGPU]' and re.match(rb'^UpscalerIndex\s*=', s):
+ hits.append(i)
+if len(hits) != 1:
+ raise SystemExit('expected one UpscalerIndex line in [WrongGPU], found %d' % len(hits))
+old = lines[hits[0]]
+lines[hits[0]] = b'UpscalerIndex=' + idx
+open(path, 'wb').write(nl.join(lines))
+print('line %d: %r -> %r' % (hits[0] + 1, old, lines[hits[0]]))
diff --git a/tools/ingame/settings_edit.py b/tools/ingame/settings_edit.py
new file mode 100644
index 00000000..a37d8030
--- /dev/null
+++ b/tools/ingame/settings_edit.py
@@ -0,0 +1,100 @@
+# SPDX-License-Identifier: MIT
+"""A game's graphics settings for an in-game run: back the file up byte for byte, change only the named keys, put the
+backup back and check its SHA-256.
+
+ python settings_edit.py backup
+ python settings_edit.py stage [ ...]
+ python settings_edit.py restore
+
+Formats:
+ ini lines key=value (the first line of each key changes; the rest of the file is kept byte for byte), e.g.
+ Resident Evil Requiem's config.ini
+ cp2077 Cyberpunk 2077's UserSettings.json: data[].options[] with name / value; values true / false / numbers are
+ written as JSON values, everything else as strings
+Every named key must exist; nothing is added."""
+import hashlib
+import json
+import os
+import re
+import shutil
+import sys
+
+sha = lambda p: hashlib.sha256(open(p, 'rb').read()).hexdigest()
+
+
+def parse_pairs(pairs):
+ out = {}
+ for p in pairs:
+ if '=' not in p:
+ raise SystemExit('use key=value: %s' % p)
+ k, v = p.split('=', 1)
+ out[k] = v
+ return out
+
+
+def stage_ini(path, want):
+ raw = open(path, 'rb').read()
+ nl = b'\r\n' if b'\r\n' in raw else b'\n'
+ lines = raw.split(nl)
+ done = {}
+ for i, l in enumerate(lines):
+ m = re.match(rb'^([A-Za-z0-9_.]+)=(.*)$', l)
+ if m and m.group(1).decode() in want and m.group(1).decode() not in done:
+ k = m.group(1).decode()
+ lines[i] = (k + '=' + want[k]).encode()
+ done[k] = (m.group(2).decode(), want[k])
+ missing = sorted(set(want) - set(done))
+ if missing:
+ raise SystemExit('keys not found: %s' % missing)
+ open(path, 'wb').write(nl.join(lines))
+ return done
+
+
+def json_value(v):
+ if v in ('true', 'false'):
+ return v == 'true'
+ try:
+ return int(v)
+ except ValueError:
+ return v
+
+
+def stage_cp2077(path, want):
+ d = json.load(open(path, encoding='utf-8'))
+ done = {}
+ for g in d.get('data', []):
+ for o in g.get('options', []):
+ if o.get('name') in want:
+ new = json_value(want[o['name']])
+ done[o['name']] = (o.get('value'), new)
+ o['value'] = new
+ missing = sorted(set(want) - set(done))
+ if missing:
+ raise SystemExit('keys not found: %s' % missing)
+ with open(path, 'w', encoding='utf-8', newline='\n') as f:
+ json.dump(d, f, indent=4)
+ return done
+
+
+def main(argv):
+ cmd = argv[1]
+ if cmd == 'backup':
+ f, b = argv[2], argv[3]
+ if os.path.exists(b):
+ raise SystemExit('backup exists already: %s' % b)
+ shutil.copy2(f, b)
+ print('backup %s sha256 %s' % (b, sha(b)))
+ elif cmd == 'stage':
+ f, fmt, want = argv[2], argv[3], parse_pairs(argv[4:])
+ done = {'ini': stage_ini, 'cp2077': stage_cp2077}[fmt](f, want)
+ print('staged: %s' % done)
+ elif cmd == 'restore':
+ f, b = argv[2], argv[3]
+ shutil.copy2(b, f)
+ print('restored; sha256 %s (backup %s) %s' % (sha(f), sha(b), 'ok' if sha(f) == sha(b) else 'MISMATCH'))
+ else:
+ raise SystemExit(__doc__)
+
+
+if __name__ == '__main__':
+ main(sys.argv)
diff --git a/tools/ingame/vram_log.py b/tools/ingame/vram_log.py
new file mode 100644
index 00000000..85fb6964
--- /dev/null
+++ b/tools/ingame/vram_log.py
@@ -0,0 +1,60 @@
+# SPDX-License-Identifier: MIT
+"""Video memory over time during an in-game run: every s, the dedicated GPU memory of the game process and of the
+whole adapter (Windows performance counters, read with typeperf; no GPU work of our own).
+
+ python vram_log.py [--interval 5] [--max-min 30]
+
+Waits for the process, then samples until it exits (or --max-min). CSV: time, seconds since start, game MiB, adapter MiB.
+"""
+import argparse
+import csv
+import io
+import subprocess
+import time
+
+NW = 0x08000000 | 0x00004000
+
+
+def pids(name):
+ out = subprocess.run(['tasklist', '/FI', 'IMAGENAME eq %s.exe' % name, '/FO', 'CSV', '/NH'], capture_output=True,
+ text=True, creationflags=NW).stdout
+ return [int(r[1]) for r in csv.reader(io.StringIO(out)) if len(r) > 1 and r[1].isdigit()]
+
+
+def sample(pid):
+ ctr = [r'\GPU Process Memory(pid_%d_*)\Dedicated Usage' % pid, r'\GPU Adapter Memory(*)\Dedicated Usage']
+ out = subprocess.run(['typeperf'] + ctr + ['-sc', '1'], capture_output=True, text=True, creationflags=NW).stdout
+ rows = [r for r in csv.reader(io.StringIO(out)) if r]
+ if len(rows) < 2:
+ return None, None
+ head, vals = rows[0], rows[1]
+ game = sum(float(v) for h, v in zip(head[1:], vals[1:]) if 'GPU Process Memory' in h and v.strip())
+ adapter = max([float(v) for h, v in zip(head[1:], vals[1:]) if 'GPU Adapter Memory' in h and v.strip()] or [0.0])
+ return game / 2**20, adapter / 2**20
+
+
+def main():
+ ap = argparse.ArgumentParser()
+ ap.add_argument('name')
+ ap.add_argument('out')
+ ap.add_argument('--interval', type=float, default=5)
+ ap.add_argument('--max-min', type=float, default=30)
+ a = ap.parse_args()
+ while not pids(a.name):
+ time.sleep(2)
+ pid = pids(a.name)[0]
+ t0 = time.time()
+ with open(a.out, 'w', newline='') as f:
+ w = csv.writer(f)
+ w.writerow(['time', 'seconds', 'game_dedicated_MiB', 'adapter_dedicated_MiB', 'pid'])
+ while pid in pids(a.name) and time.time() - t0 < a.max_min * 60:
+ g, ad = sample(pid)
+ w.writerow([time.strftime('%H:%M:%S'), round(time.time() - t0, 1), None if g is None else round(g, 1),
+ None if ad is None else round(ad, 1), pid])
+ f.flush()
+ time.sleep(a.interval)
+ print('vram log done: %s' % a.out)
+
+
+if __name__ == '__main__':
+ main()
From 3c851cc38cc897981e504956cbbd89655bfe14a3 Mon Sep 17 00:00:00 2001
From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
Date: Sat, 14 Nov 2026 06:15:36 -0500
Subject: [PATCH 2/2] Tools (in-game): --extra runtime options per model
choice; one launch trace per choice
- run.py: runtime_options_extra in the config and --extra on prepare / use-m /
use-l (e.g. l_post=own, then --extra '' for the default post in the same
session); set_options replaces the items it added before (timing, launch
trace, the previous extra) instead of appending them again.
- run.py: each model choice writes its own launch-trace-.txt; collect
summarises every trace on its own.
- offline_create.py: --extra adds runtime options to Model L's runs.
Used for the Model L own-reconstruction game run (#162).
Refs #89
Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
---
tools/ingame/README.md | 9 ++++---
tools/ingame/offline_create.py | 6 +++--
tools/ingame/run.py | 49 +++++++++++++++++++++++-----------
3 files changed, 44 insertions(+), 20 deletions(-)
diff --git a/tools/ingame/README.md b/tools/ingame/README.md
index 33321aa1..bd03a447 100644
--- a/tools/ingame/README.md
+++ b/tools/ingame/README.md
@@ -11,7 +11,7 @@ arguments (`offline_create.py`, `make_record.py`).
```
python offline_create.py build --work
-python offline_create.py create --work --runtime-dir --model --model-sha [-- ]
+python offline_create.py create --work --runtime-dir --model --model-sha [--extra l_post=own] [-- ]
```
`create` runs the interop harness once:
@@ -42,7 +42,9 @@ Everything after a lone `--` wraps each GPU run, for example a GPU-slot tool.
- `gpu_flag`: the GPU flag file;
- `disk_free_gb`: minimum free space per drive.
- `%VARIABLES%` are expanded. `exe_dir` is relative to `game_root`.
+ `%VARIABLES%` are expanded. `exe_dir` is relative to `game_root`. Optional `runtime_options_extra` adds runtime
+ options to every model choice (e.g. `l_post=own`); `--extra` on `prepare`, `use-m` or `use-l` replaces it for that
+ choice (`--extra ''` for none), so two variants can run in one session.
2. Run the phases:
@@ -76,7 +78,8 @@ What each phase does:
- **`collect`**:
- copies our own logs. OptiScaler's log is never read;
- writes `proof.json` with the model option, the model file and digest, Model L kernels, the executor, dispatch
- counts, refusals and the launch-trace counts.
+ counts, refusals and the kernel counts of each launch trace (one trace per model choice,
+ `launch-trace-.txt`).
The verdict is PASS or NOT PROVEN.
- **`restore`**:
diff --git a/tools/ingame/offline_create.py b/tools/ingame/offline_create.py
index 8a737249..6cde0c2a 100644
--- a/tools/ingame/offline_create.py
+++ b/tools/ingame/offline_create.py
@@ -13,7 +13,8 @@
--runtime-dir is a built runtime folder (wronggpu_runtime.dll and the kernels, build.py --model-l). Everything after
a lone "--" is a command prefix that wraps each GPU run (e.g. a GPU-slot tool: `-- python gpu_slot.py --cap-s 170 --`);
-without it the harness runs directly. --tmp sets TMP / TEMP for the runs, --rocm-path ROCM_PATH.
+without it the harness runs directly. --extra adds runtime options to Model L's runs (e.g. l_post=own), --tmp sets
+TMP / TEMP for the runs, --rocm-path ROCM_PATH.
The model data folder is under --work, not %LOCALAPPDATA%: inside a packaged (MSIX) app new files there are private
to the package. Model M uses --m-model-dir (default %LOCALAPPDATA%\\WrongGPU\\model, read only)."""
@@ -95,7 +96,7 @@ def l_data():
def options(model, data, stages):
o = 'kernels_dir=%s;timing_log=1;timing_stages=%d;executor=dx12' % (A.runtime_dir, 1 if stages else 0)
if model == 'L':
- return 'model=L;model_dir=%s;' % data + o
+ return 'model=L;model_dir=%s;' % data + o + (';' + A.extra if A.extra else '')
return 'model=M;model_dir=%s;' % A.m_model_dir + o
@@ -204,6 +205,7 @@ def main(argv):
help="Model M's data folder (timing; read only)")
ap.add_argument('--rounds', type=int, default=3)
ap.add_argument('--shapes', help='comma list of shape names (default: all six)')
+ ap.add_argument('--extra', help="more runtime options for Model L's runs, e.g. l_post=own")
ap.add_argument('--tmp', help='TMP / TEMP for the runs')
ap.add_argument('--rocm-path', help='ROCM_PATH for the runs')
ap.add_argument('--vswhere', default=VSWHERE)
diff --git a/tools/ingame/run.py b/tools/ingame/run.py
index 1f0ef8f4..116f003c 100644
--- a/tools/ingame/run.py
+++ b/tools/ingame/run.py
@@ -104,11 +104,17 @@ def _config_path(argv):
DISK_FREE_GB = CFG.get('disk_free_gb', {})
S = CFG.get('settings') or {}
os.makedirs(A, exist_ok=True)
- # the runtime's timing lines and the launch trace (one line per D3D12 kernel launch, capped; the runtime sets
- # WG_LAUNCH_TRACE from the option, since a store launcher starts the game without our environment)
- TRACE = os.path.join(A, 'launch-trace.txt')
- OPTS_EXTRA = 'timing_log=1;timing_stages=1;launch_trace=%s;launch_trace_max=%d' % (
- TRACE, int(CFG.get('launch_trace_max', 200000)))
+ EXTRA = CFG.get('runtime_options_extra', '') # e.g. "l_post=own"; run.py --extra replaces it for one phase
+
+
+def opts_extra(r):
+ """The items this tool adds to runtime_options: the runtime's timing lines, a launch trace of its own for each model
+ choice (launch-trace-.txt: one line per D3D12 kernel launch, capped; the runtime sets WG_LAUNCH_TRACE from
+ the option, since a store launcher starts the game without our environment) and EXTRA."""
+ trace = os.path.join(A, 'launch-trace-%s.txt' % datetime.datetime.now().strftime('%H%M%S'))
+ r.save(trace=trace, extra=EXTRA)
+ return 'timing_log=1;timing_stages=1;launch_trace=%s;launch_trace_max=%d%s' % (
+ trace, int(CFG.get('launch_trace_max', 200000)), ';' + EXTRA if EXTRA else '')
def settings_cmd(verb):
@@ -287,7 +293,7 @@ def prepare(r):
# 4. the model choice: runtime_options model= (executor kept) and OptiScaler's UpscalerIndex
if not r.dry and not os.path.isfile(os.path.join(X, 'wronggpu.ini')):
raise SystemExit('no wronggpu.ini in %s after the update: restore next' % X)
- set_options(r, MODEL_NAME, OPTS_EXTRA)
+ set_options(r, MODEL_NAME, opts_extra(r))
# 5. the model file where the front end and the runtime look first (the game's WrongGPU\model\)
place_model(r)
# 6. the game's graphics settings named in the config
@@ -309,8 +315,15 @@ def set_options(r, model='L', extra=''):
if len(idx) != 1:
raise SystemExit('wronggpu.ini: %d runtime_options lines' % len(idx))
old = lines[idx[0]].split(b'=', 1)[1].decode()
- items = [i for i in re.split(r'[;,]', old) if i.strip() and not re.match(r'\s*(model|timing_log|timing_stages)=', i)]
- new = ';'.join(['model=' + model] + items + [e for e in extra.split(';') if e])
+ add = [e.strip() for e in extra.split(';') if e.strip()]
+ # replaced: the model, the items this tool adds (timing, launch trace), the items a previous --extra added (state)
+ # and any key that extra sets again
+ ours = {'model', 'timing_log', 'timing_stages', 'launch_trace', 'launch_trace_max'}
+ ours |= set(r.state.get('extra_keys', [])) | {e.split('=', 1)[0] for e in add}
+ items = [i for i in re.split(r'[;,]', old) if i.strip() and i.split('=', 1)[0].strip() not in ours]
+ new = ';'.join(['model=' + model] + items + add)
+ r.save(extra_keys=sorted({e.split('=', 1)[0] for e in add} - {'timing_log', 'timing_stages', 'launch_trace',
+ 'launch_trace_max'}))
r.log('wronggpu.ini runtime_options: %r -> %r' % (old, new))
if not r.dry:
lines[idx[0]] = b'runtime_options=' + new.encode()
@@ -333,7 +346,7 @@ def reinstall(r):
for inst, pay in PAYLOAD_MAP.items():
if not r.dry:
r.need(sha(os.path.join(X, inst)) == sha(os.path.join(PAYLOAD, pay)), 'installed %s = payload %s' % (inst, pay))
- set_options(r, MODEL_NAME, OPTS_EXTRA)
+ set_options(r, MODEL_NAME, opts_extra(r))
def foreign_hashes(r, when):
@@ -522,17 +535,18 @@ def collect(r, logs=None):
res['model_file'] = {'path': MODEL_DST, 'sha256': sha(MODEL_DST) if os.path.isfile(MODEL_DST) else None,
'expected': MODEL_SHA}
res['shots'] = sorted(glob.glob(os.path.join(r.a, 'shots', '*.png')))
- tr = os.path.join(d if logs else r.a, 'launch-trace.txt')
- if os.path.isfile(tr):
+ res['launch_traces'] = [] # one per model choice (opts_extra), each summarised on its own
+ for tr in sorted(glob.glob(os.path.join(d if logs else r.a, 'launch-trace*.txt'))):
counts = {}
for l in open(tr, encoding='utf-8', errors='replace'):
f = l.split()
if f:
counts[f[0]] = counts.get(f[0], 0) + 1
lk = {k: v for k, v in counts.items() if k.startswith(('l_', 'lev/')) or k == 'swin_block_c64_add_ex_nc'}
- res['launch_trace'] = {'file': tr, 'lines': sum(counts.values()), 'kernels': len(counts), 'model_l_launches': lk,
- 'all': dict(sorted(counts.items()))}
- r.log('launch trace: %d lines, %d kernels; Model L kernels: %s' % (sum(counts.values()), len(counts), lk))
+ res['launch_traces'].append({'file': tr, 'lines': sum(counts.values()), 'kernels': len(counts),
+ 'model_l_launches': lk, 'all': dict(sorted(counts.items()))})
+ r.log('launch trace %s: %d lines, %d kernels; Model L kernels: %s' % (os.path.basename(tr), sum(counts.values()),
+ len(counts), lk))
if not logs:
release_flag(r) # the game has exited (checked above); normally game_hold.py has released it already
out = os.path.join(r.a, 'proof.json')
@@ -594,7 +608,12 @@ def main():
ap.add_argument('label', nargs='?', default='game')
ap.add_argument('--dry-run', action='store_true')
ap.add_argument('--logs', help='collect: logs already copied (proof extraction only)')
+ ap.add_argument('--extra', help='prepare / reinstall / use-m / use-l: more runtime options for this model choice '
+ "(replaces the config's runtime_options_extra), e.g. l_post=own; '' for none")
a = ap.parse_args()
+ if a.extra is not None:
+ global EXTRA
+ EXTRA = a.extra
if a.phase == 'all' and not a.dry_run:
raise SystemExit('all is a dry run only (use --dry-run)')
r = Run(a.dry_run or a.phase == 'preflight')
@@ -618,7 +637,7 @@ def main():
if a.phase in ('use-m', 'use-l'): # the comparison run in the same session and settings (the game must be closed)
if GAME_EXE in tasks():
raise SystemExit('the game is running')
- set_options(r, 'M' if a.phase == 'use-m' else 'L', OPTS_EXTRA)
+ set_options(r, "M" if a.phase == "use-m" else "L", opts_extra(r))
elif a.phase == 'shot':
shot(r, a.label)
elif a.phase == 'collect':