diff --git a/psprecomp/games/dissidia/docs/frame-rate.md b/psprecomp/games/dissidia/docs/frame-rate.md index 6851680..41e53ba 100644 --- a/psprecomp/games/dissidia/docs/frame-rate.md +++ b/psprecomp/games/dissidia/docs/frame-rate.md @@ -58,6 +58,40 @@ update run one update in `rate / 30` with dt 2. **Not yet tested in play**: expe fades that truncate at a small dt. There is no vsync, so a rate above the monitor's refresh tears or drops frames. In multiplayer the lowest rate any player asked for wins, whatever the values. +## 30 Hz logic (on by default; `PSPRECOMP_LOGIC30=0` or Debug > Developer options > Timing turns it off) + +Above 30 fps the battle can run its gameplay at the PSP's 30 updates a second instead of `rate` updates with a smaller step: every +`rate / 30`-th frame is a gameplay frame (timer step 2); the frames between are drawn-only (step 0). Native high-rate play can never +match 30 fps exactly, because a keyed event at 30 fps acts for a whole 33 ms frame; with 30 Hz logic every move plays as at 30 fps +(docs/testing.md "30 Hz logic" for the parity results). + +- **Pacing** (`l30_decide_next`): in play a gameplay frame is due every 33.3 ms of real time (the frame shown nearest to it runs the + gameplay), and each frame is blended by the time actually passed since the last gameplay frame. Counting frames instead (every + rate / 30-th frame, phases k/n) looked jittery on the RP6 at 120 fps while moving: it drops ~7 % of frames, and a late frame then + showed the picture a quarter step on although more time had passed. Tests stay on frame counting (repeatable): parity runs, a test + script, `PSPRECOMP_VCLOCK`, or `PSPRECOMP_L30_PACE=frames`; `=clock` forces real time. `PSPRECOMP_L30_PACE_LOG=` logs decisions, + `PSPRECOMP_L30_TIMING=1` frame times by position after the gameplay frame. +- **Task tree** (framerate.cpp `task_run_hook` on the runner FUN_0888245C): on drawn-only frames the battle scene list runs with the + pause filter (flags mask/value 4/4 instead of 1/1), so the scene is still built and drawn, minus 11 tasks that change gameplay when + run that way (the skip list in framerate.cpp; `PSPRECOMP_L30_KEEP` keeps them, for checks). +- **Clocks**: the timer's dt (`*(0x089D69E0)+0x18`, read directly by ~85 battle sites) is the current frame's step; the global step + (0x089D5EAC, copied a frame late by FUN_08864DD8) is made to show the step of the frame it will be used in (`global_step_hook`), and + the slow-motion clock only advances before gameplay frames. +- **Input**: the pad changes only on gameplay frames; a press that comes and goes between two of them is latched and shown once. +- **Camera**: the camera manager update (FUN_08B3CA24) is in the gameplay part of the battle list: it runs on gameplay frames only + (counted), and the renderer blends the view between them. +- **Random sequences** (0x09AF84D0, 0x09AFB89C) are put back after drawn-only frames (measured: not drawn from there; a guard). +- **Compensations for native high rates** (press latch, Jecht grid, EX Burst gating, gauges, UI step, steered moves, action start step, + exact steps, FX60) all stand down: they test for a step below 2 or for 30 Hz logic. A new one must too (FX60 did not at first: + it lengthened hitboxes, extra hits at 60 / 120 fps). +- **Drawing the frames between** (runtime/src/psp_ge_draw.cpp, `PSPRECOMP_INTERP`): the game module tags each display list with + (gameplay generation, phase) via `ge_interp_set_frame`; the render queue hands the tag to the renderer per list. On gameplay frames the + world / view / projection / bone matrices, texture-gen matrix and texture scale / offset of every 3D draw are recorded, keyed by the + draw's identity and occurrence (with a looser key without the vertex / index addresses for double-buffered vertices such as weapons). + Drawn-only frames draw with those values blended between the last two gameplay frames by phase (1, default: smooth, up to one 30 Hz + frame behind) or extrapolated from them (2); 0 draws the last gameplay frame as is. 2D (through-mode) draws, moves over 3 m and camera + cuts (the eye jumps over 3 m) are not blended. Effects whose vertices the CPU writes each update still move at 30 Hz. + ## What 60 fps means here It is **native**: the battle simulation runs 60 updates a second. Nothing is interpolated, and there's no extra diff --git a/psprecomp/games/dissidia/docs/testing.md b/psprecomp/games/dissidia/docs/testing.md index a8ecb7d..12b2db6 100644 --- a/psprecomp/games/dissidia/docs/testing.md +++ b/psprecomp/games/dissidia/docs/testing.md @@ -16,6 +16,27 @@ Needs: Xvfb, python3 + Pillow, a save in `/ms0` (Quick Battle unlocked) an battle_test.py actions --work W --actions 2423,2424 --rates 30,120 [--hold SQUARE --stick 128,255 --hold-ms 1500] battle_test.py actions --work W --all # every action the fighter has +## Rendering on the GPU without an X server (`BT_GPU=1`) + +Xvfb has no GPU driver: every run on its virtual display renders with Mesa's llvmpipe (software GL, `[RT] GL renderer: llvmpipe` in +the log), whatever GPU the machine has. The NVIDIA driver can render without any display through EGL, and SDL2's `offscreen` video +driver makes its GL context through EGL: + + SDL_VIDEODRIVER=offscreen __EGL_VENDOR_LIBRARY_FILENAMES=/usr/share/glvnd/egl_vendor.d/10_nvidia.json + +No Xvfb and no `DISPLAY` are needed; the log shows `[RT] GL renderer: NVIDIA GeForce GTX 1660 SUPER/PCIe/SSE2` (the GPU of the +test host) and one harmless `xcb_connection_has_error() returned true` line. The debug socket's screenshots (`S`) still read the +picture, so the HUD checks of the menu walks work as before. + +- `battle_test.py` (and every tool built on its `Game` / `start_xvfb`: `combo_timing.py`, `fps_parity.py`, ...): `BT_GPU=1` skips Xvfb + and sets those two variables; `BT_GPU=auto` does so when the NVIDIA EGL vendor file exists; `BT_EGL_VENDOR=` picks another + vendor. `fps_parity.py --gpu auto|on|off` (default auto). +- Check that it took: `grep "GL renderer" run_*.log`. `eglinfo -B` lists what EGL offers (the GBM, surfaceless and device platforms + all reach the NVIDIA GPU on the test host); `vulkaninfo --summary` shows the GPU for Vulkan too (not tried with the game headless). +- Speed: with the GPU, drawing is no longer what limits a fast-forwarded run (a 120 fps parity run took 78 s drawing and 79 s with + drawing off; with llvmpipe, turning drawing off made 120 fps runs 2.6x faster). Several games share the GPU fine. +- Older notes in this file that say a run used the GPU on Xvfb were wrong: those runs were llvmpipe. + ## Pieces - **Script player** (`runtime/framerate.cpp`, `PSPRECOMP_TEST_SCRIPT=`): lines ` set|tap|analog|mark|action|quit`. @@ -273,6 +294,28 @@ Notes from the Jecht combo work (2026-11-11), for whoever needs a live value aga ## Combo input windows across frame rates (`tools/combo_timing.py`) +**Optional part of the parity tests (15 Nov):** `fps_parity.py run --jecht-combos` (or `combo_timing.py combos --work W`) runs this after +the parity games, at every rate the settings offer (30, 60, 90 ... 240; `--rates` / `--combo-rates` to pick fewer, `--jobs` games at once, +default 4), and judges it: every script a probe starts at 30 fps (chain move, too-early / too-late repeat, HP +finisher) must start at every rate, first and last probe delay within `--tol` 34 ms (one 30 fps update), and no rate may start a script +30 fps never does; exit code 1 otherwise, results in `/jecht_combos/combos.txt`. It now boots like the parity tests: the recorded +walk with the fighter swapped in (`PSPRECOMP_TEST_CHAR_SWAP=15=20`, ~35 s to the fight instead of ~3 min; `--menus` for the old picture +walk), on the GPU when there is one (`--gpu`), analogue camera off, 3 s settle before the first trial. The verdict was checked against +the saved 11 Nov results: PASS for the windows after the action start step fix (120 fps within 25 ms of 30 fps), FAIL naming Rush > +Neutral 2 / Up 2 (317..408 vs 267..358 ms) for the ones before it. Checked in the game (15 Nov, 30 and 120 fps, box shared with 6 parity games): PASS in +30 min (`--rates 30,120`). The 30 fps windows are the ones measured on 11 Nov (Rush > Neutral 2 267..358 ms, Neutral 2 > Neutral 3 +542..633 ms); 120 fps: Rush > Neutral 2 / Up 2 identical, Neutral 2 > Neutral 3 / Up 3 8-17 ms earlier, the HP finisher at 600 vs 633 ms +(a single probe delay: the narrowest window). One fight ended during the run (time limit) and its batch was replayed in the next one. +The sweep is adaptive: a 1/30 s grid, then 1/120 s only between neighbouring probes whose outcome differs (same windows as the full +1/120 s grid on all four saved 11 Nov runs, with 40-50% of the trials). +**All eight rates (15 Nov, `--jobs 3`, 1 h 53 min next to the roster run): FAIL, one real finding.** Rush > Neutral 2 and Rush > Up 2 are +accepted 308..400 ms after the Rush at 90 and 150 fps against 267..358 ms at 30, 60, 120, 180, 210 and 240 fps (41 ms late: a press timed +for 30 fps can miss the chain). Neutral 2 > Neutral 3 / Up 3 are within 34 ms everywhere; Neutral 3 > HP 575..733 ms at every rate +(30 fps: 600..733). 90 and 150 fps are also the rates where Exdeath's multi-hit lands late (see the parity findings); not fixed. +Harness lesson from the first all-rate run: the HP finisher sweep KO'd the dummy (only parity runs cleared its live HP damage), after +which every trial timed out and the HP step silently shrank. Now the dummy's HP damage is cleared in every AI-off run, trials start with +`home`, and the verdict reports a step where over a quarter of the trials could not be played as a harness problem. + Built 2026-11-11 for Jecht (his chain inputs have 3 to 5 frame windows at 30 fps; the wiki list is https://dissidia.wiki/Jecht_(Dissidia_012)/Combos). One battle per frame rate runs in parallel (own display / port / memory stick); the CPU fighter has no AI input (`PSPRECOMP_TEST_AI_OFF=1`) but is hit normally; trials are fed as script batches (`batch`), so menus are walked once @@ -313,6 +356,144 @@ Added with `psp_settings_add_debug_toggle` (runtime/include/psp_settings.h: a ga - Jecht's Circle / Square are handed to the game on a 30 Hz grid above 30 fps (`jecht_grid_filter`, `PSPRECOMP_JECHT_GRID=0` to turn it off): the Jecht Combo Timing helper above therefore sees each press on a grid update, and a press can reach the game up to one 30 Hz frame after you pressed it. Details and measurements: docs/frame-rate.md "Jecht timed windows on the 30 Hz grid". +## Frame-rate parity tests (`tools/fps_parity.py`) + +Every move of every playable character has to play at every frame rate the settings offer (30, 60, 90, 120, 150, 180, 210, 240) as it +does at 30 fps, the PSP's rate (issue #55: Exdeath's teleport and Squall's ground moves at 60 fps). `fps_parity.py` checks that, +character by character, with no menu walking per character: + + fps_parity.py run --work W [--chars all | exdeath,squall,10] [--rates 30,60,...,240] [--jobs 5] [--repeat-base] [--out DIR] + fps_parity.py compare DIR [--frame-tol 1 --pos-tol 0.25 --pos-rel 0.05] # judge finished runs again + fps_parity.py show DIR squall 1454 mash # one trial at every rate, side by side, 30 Hz frames + fps_parity.py chars # the 31 playable characters and their charIds + tests/run_fps_parity_test.sh W [out] [run options] # the same as a test (exit code 1 on a difference) + fps_parity.py run --work W ... --jecht-combos [--combo-rates 30,120] # optional: Jecht's chain windows too (combo_timing.py), every rate by default + fps_parity.py run --work W --chars none --jecht-combos # only the Jecht combo test, every rate (30..240) + +Output: `DIR/summary.txt` (characters x rates), `DIR//report.txt` (every failing trial and why), `DIR/results.json`. + +### How a run works + +- **One game per character and rate**, in parallel (`--jobs`), each with its own display / port / memory stick copy. On the GPU by + default (`--gpu auto`, see "Rendering on the GPU without an X server"). +- **Any character from the one recorded walk.** `--direct` replays `tests/replays/quick_battle_walk.txt` (Squall vs Lightning) and + `PSPRECOMP_TEST_CHAR_SWAP=15=` (runtime/direct_battle.cpp) swaps the fighter. What the menu hands the battle is the block + at 0x09AFB8A0 (FUN_088FB1A4 copies it): per side a 0x1060-byte fighter record (stride 0x1060) that starts with a copy of the + character's 0x778-byte save record (byte 0 charId, byte 1 level, abilities...; the CPU side's is generated), and at +0xF3A the object + table id of the model the loading screen (FUN_0888C4C8, `bs+2008`) loads (`P__1P`: object table `*(0x089D4968)`, byte 6 + charId, byte 7 costume 1, byte 8 type 0; +0xF3E is the assist's `_A` entry). Team structs (count byte, charIds, stride 0x38) + follow at +0x272F. On every pad poll a side whose record holds `` gets ``'s own save record (save records at + `*(0x089D712C) + 0xF58 + 0x778 * index in the 31-entry id list at 0x089CBC98`), its charId and its model id. Lightning under test + uses `15=31,31=15` (the dummy becomes Squall). Things that did NOT change the fighter: the menu's selectable list, the grid cells + (their +4 is not a charId), the battle setters FUN_08A48DB0 / 08A491E0 and FUN_089132A4 / 089121F4 (not on the Quick Battle path), + FUN_08846E18 (charId -> object entry; not called there), the team structs alone. +- **The dummy**: the CPU fighter never presses a button and its brain does not tick (`PSPRECOMP_TEST_NO_AI`, `_AI_OFF`) but it runs at + the normal time step and reacts to hits; 100,000,000 HP so no hit ends the fight. +- **Trials**: the game lists the player's plya actions (`action -1 player`: id, script, `later` +3, `side` +5 2 ground / 1 air, + `button` +0x0F 1 brave / 2 HP); the tool writes the trials into the running battle (`batch`). Each trial: `waitidle`, `home 3.5 0 0` + (dummy at stage point 0,0, player 3.5 m away, both turned to face each other with FUN_08835BA4, velocities zeroed), then + - every action, **plain**: `action player` (air actions 0.4 s after a jump); + - first presses of an attack, **mash**: the same, plus the move's button (Circle / Square) tapped every 200 ms for 2.4 s, so the + follow-up presses and chains run too; + - movement every fighter has (pad input, not plya): jump, double jump, dodge, air dodge, dash (R+Triangle held), air dash, block + (R held), run (stick forward 1.5 s), run + jump. They start after 1 s of settling. Running is camera-relative, so `run` also checks + that the camera ends up in the same place. +- **Samples every update** (`PSPRECOMP_TEST_PARITY=1`, framerate.cpp): `[PAR] | player: script motion motion-time x y z fwd.x fwd.z | + dummy: same | hits`; updates between two 30 Hz frames carry a `~` after the time. Every rate is a multiple of 30, so each has an update + at game time k x 33.3 ms (the lattice) as well. + `sync` / `waitidle` end on the lattice and event times are compared with a 0.01 ms tolerance (the sum of 8.33 ms steps is + 99.99999 at 100 ms), so a trial starts at the same game moment at every rate. `[PARHIT] dummy atkp= react= flags dmg by=` + comes from FUN_0883A150 (take hit: a0 defender, a1 the 64-byte hit record). +- **Judging**, each rate against 30 fps, per trial: the player's script sequence and the dummy's motion sequence on the lattice (same + ids in order, each change within `--frame-tol` 30 Hz frames); the dummy's hits (count, attack ids and reactions in order, time within + `--frame-tol` frames, HP damage within `--dmg-tol` 3%: HP damage is the attacker's bravery, which varies by ~2% between two 30 fps + runs); both paths (largest gap <= `--pos-tol` m or `--pos-rel` of the distance covered) at the best alignment within `--shift` 1 frame, + in whole updates of the faster rate; peak heights within `--height-tol` 0.1 m; when the player is idle again. +- **Phase is not judged.** A move starts at the end of the first update after it was asked for: 33 ms after the press at 30 fps, 17 ms + at 60, 8 ms at 120, so a faster rate can lead by up to a frame (the same 25 ms the Jecht chain windows showed). Comparing paths at a fixed + game time would fail every move for that; the report counts the trials where the player ran ahead instead. `--repeat-base` makes a second 30 fps run: trials that differ between two 30 fps runs are reported as + NONDETERMINISTIC, not as failures. With the GPU, two 30 fps runs agreed on every trial in the checks made (Squall, Exdeath). +- **Speed**: boot to fight ~35 s (`--direct`, 4x menus); then fast-forward. On the GPU about 10x real time at 30 fps, 3.5x at 120. + `PSPRECOMP_TEST_NO_DRAW=2` (set by `--no-draw`) stops the draw calls once the clock runs (`ge_draw_request_no_raster`): 2.6x faster + with llvmpipe, no gain on the GPU. + +### Findings so far (15 Nov, Squall and Exdeath, 30 vs 60 / 120 fps) + +- **Moves lead by up to a frame at high rates** (block, jump, dodge and most attacks were 0.3-0.8 m "off" in the first 100 ms when + compared at the same game time): the phase described above, not a speed difference. Delaying the scripted presses by a whole frame + (`PSPRECOMP_TEST_INPUT_DELAY=33.3`, `--input-delay`) makes the fast rates a frame late instead; the right delay would be 33.3 ms minus + one update. Dodge and block still differ after the alignment (0.8 m / 0.3 m around the first frame). +- **Jumps get lower as the rate rises**: Squall's jump peaks at 4.10 m (30 fps), 3.94 (60), 3.85 (120), 3.80 (240); double jump 6.71, + 6.39, 6.21, 6.12; an Exdeath air action started 0.4 s after a jump starts 0.2 m lower at 120 fps and lands 67 ms earlier. +- **Multi-hit moves land a different number of hits, and at 90 / 150 / 180 / 210 fps every hit is one update late.** Exdeath's action + 1960 (script 10000, attack 1271 every 10 ticks): a hit every 166.7 ms at 30, 60, 120 and 240 fps, but 177.8 ms at 90, 173.3 at 150, + 172.2 at 180, 171.4 at 210: exactly the rates whose step (2/3, 0.4, 1/3, 2/7 ticks) is not exact in floating point. The summed motion + time lands at 69.99999 instead of 70, so the event keyed at tick 70 fires an update later; 17 hits instead of 19 at 90 fps. Squall's + action 1454 (script 10015): 2 hits at 30, 1 at 60; follow-ups chosen by a mash differ. + Experiments (framerate.cpp): `PSPRECOMP_MT_SNAP=1` trims the step so the motion time lands on the step lattice; about half the hits come + back on time (intervals 166.7 and 177.8 alternate), so another summed clock is involved. The rehit timer (FUN_0880ACBC, record `+68 <= 0`, + reloaded from 0x089C6CA0 by FUN_0880AD28) is not it: counting 0 < t < 0.001 as run out changed nothing. Not fixed. +- **Repeatability (two 30 fps runs of the same character)**: what made runs differ and what was done about it. + The analogue camera (`camera.cpp`) times itself with the wall clock, which varies under fast-forward, and turns the left stick: off in + parity runs (`PSPRECOMP_CAM_FREE=0`; the game's own camera, on game time, stays). Trial state carried over (bravery, break, HP damage): + `home` puts both fighters' status blocks (actor+0x52C, 0x80 bytes) back as they were at the first `home`. Right after the `home` + teleport a jump was sometimes not taken and the dummy sometimes not hittable: trials start 0.6 s after it. Left (Squall, 80 trials): + 7 trials whose dummy reactions or one air jump differ, and the HP damage of an HP attack (the attacker's bravery) differing by a steady + ~190 between runs in 11 trials; where that comes from was not found. `--repeat-base` reports such trials as NONDETERMINISTIC. +- **The dummy's brain is stopped (`PSPRECOMP_TEST_NO_AI`) and its virtual pad cleared every update** (brain `actor+0x5C4`, pad brain+0x53C: + +4 state, +8 pending, +12 previous). A CPU caught mid-dash when the clock started kept its dash buttons held and dashed through a whole + run (Gabranth's second 30 fps run: 71 idle waits timed out). A brain left running with only its presses blocked still starts moves. + A run where over a quarter of the idle waits time out is retried; one where the character under test is not in the fight fails at once. +- **Character swap and unusual model entries**: the player model entry is found by name (`P__1P`, costume 1, not an assist / manikin + type); Prishe's is type 16, not 0, and with the old type-0 test she was never swapped in (Squall's model loaded). +- **The first seconds after the intro are not repeatable**: a first trial right after the fight starts gave 0 hits in one 30 fps run and + 19 in the next. Every run now starts with a 3 s settle (`mark SETTLE`); with it two 30 fps runs agree. +- **Camera**: after `home` the 120 fps camera ends up on the other side (running forward goes +z instead of -z). +- **Whole roster, all eight rates (full4, 15-16 Nov, `~/dissidia/fpsparity/full4` on the test host, ~11 h with 6 games at once):** every + character fails at every rate above 30: 75 % of the trials at 60 fps, 80-81 % at 90-240 (1665 of 2054 at 120). At 120 fps the failing + trials show: player path off 1232, dummy path off 1072, player peak height 638, end of the move 636, dummy reaction order 612, dummy + peak height 570, hit count / timing ~600. All 31 characters fail jump, double jump and dodge, 30 air dodge; where the jump height + differs it is LOWER at 120 fps for every character (30 of 30, e.g. Warrior of Light 3.55 vs 3.77 m). Two 30 fps runs differ on 1-15 % + of a character's trials (Kefka 27 of 67, Squall 18 of 80, Onion Knight 15 of 48 are the noisiest; Prishe 1 of 136, Kain 1 of 52). + +### 30 Hz logic (default since 16 Nov; `PSPRECOMP_LOGIC30=0` for the native behaviour) + +The native findings above come from one cause: at 30 fps a keyed event (a hit frame, a jump impulse, a pad edge) acts on the game for a +whole 33 ms frame, and smaller steps cannot reproduce that exactly. With 30 Hz logic the fight itself runs at the PSP's rate whatever the +frame rate, and the frames between are only drawn (docs/frame-rate.md "30 Hz logic"). The parity tests run it with `PSPRECOMP_LOGIC30=1` +in the environment (the tool passes the environment on). + +- Squall and Exdeath at 30 / 60 / 120 / 240 (v30): 0-3 failing trials of 80-90 per rate, against 75-81 % natively. What was left: + Squall's air moves 2247 / 3026 (mash) took extra hits from a lingering hitbox at 60 and 120 fps: the FX60 effect-component + compensation still ran (it steps the components one update in rate / 30, with no regard for which frames are gameplay frames). It is + off under 30 Hz logic now. After that those trials differ between two 30 fps runs as well (juggles: chaotic, reported NONDETERMINISTIC). +- **Comparator fix**: the action start is logged in whole ms (`gt=410867` for 410866.67), and samples were taken from `>= start`, so a start + that rounded up lost its first sample. That showed as a one-frame lead ("the player ran ahead") and, for air actions, a lower peak + (Exdeath 1685 / 1687: 3.38 vs 3.88 m, with logs identical to the digit). Samples and hits within 1 ms of the start now count; the + "ran ahead" counts went to 0 at every rate under 30 Hz logic. Natively judged runs are affected the same way (part of the phase lead + reported there is this). +- `waitidle` timeouts on long stances (Squall 1259, script 8000) happen at 30 fps too; they are not a rate problem. +- **Whole roster, all eight rates (l30full, 16 Nov, ~8 h with 5 games at once):** 3.1-4.1 % of trials fail per rate (84 of 2054 at + 60 fps, 75 of 1976 at 120), against 75-81 % natively; two 30 fps runs differ on 15 % (318 of 2054, reported apart). Jecht's combo + test with 30 Hz logic passes at every rate (natively Rush > Neutral 2 / Up 2 failed at 90 / 150). Of the 503 failures left: + - 221 were HP damage only (the hits right, the damage not): **harness bug**, fixed. `home` restores the status blocks from a snapshot + taken at the first `home`, and status `+0x14` is ``: one run's snapshot caught the current + bravery at 0 (Sephiroth 60 fps: `0x005F0000`, the others `0x005F005F`), so every trial of that run hit for ~82 less. `home` now sets + the current bravery to the initial one. Sephiroth (91 of them) logged identical damage at 30 / 30b / 60 / 120 after the fix. + - 94 were `run` / `run_jump`: the player ran the opposite way (+z at 30 fps, -z at 60-240, the same numbers at every rate; running is + camera-relative). NOT the camera running on drawn-only frames: the camera manager update (FUN_08B3CA24) only runs on gameplay + frames under 30 Hz logic (counted: 2400 vs 0). A Kefka run with the bravery fix passed them (90 fps: 7 -> 3 failures), so the + trial before probably left the fighters / camera differently through the bravery state; the l30full2 rerun confirms or not. + Only shows after the full action list: `--moves-only` passes either way. + - The rest (path, peak, hit count) is spread thinly, 0-8 per character and rate; many are juggles that also differ between two 30 fps + runs. A character whose failures are the same at every rate (Sephiroth's air dash 1.03 m) points at an odd 30 fps reference run. +- **Things ruled out**: the game's random sequences (rand() LCG word 0x09AF84D0, effect LCG 0x09AFB89C) are never drawn from on + drawn-only frames (0 of 3600); framerate.cpp puts them back after each drawn-only frame anyway (`PSPRECOMP_L30_RNG=0` turns that off). +- **Harness tips**: `[fps_parity] ... FAILED: the game exited` with a log that just stops is a crash: `journalctl -k | grep segfault` + (a fault address like `9dd4c0` = an emulated address read through a null base, e.g. a module's own `g_ram` never set). Other sessions + run games on the same host, so match the time. A character's games that "never started (direct)" (Vaan 90/120/180/210 once) passed on a + rerun. Categorise a report by failure kind before reading it trial by trial (a few lines of Python over `*/report.txt`). + ## Frame pacing and vertical sync (issue #67) `PSPRECOMP_PRESENT_STATS=1` logs `[PRESENT_STATS]` every 5 s: present-to-present mean, standard deviation, median, p99, max and diff --git a/psprecomp/games/dissidia/runtime/direct_battle.cpp b/psprecomp/games/dissidia/runtime/direct_battle.cpp index ce5bc6b..6b20816 100644 --- a/psprecomp/games/dissidia/runtime/direct_battle.cpp +++ b/psprecomp/games/dissidia/runtime/direct_battle.cpp @@ -12,8 +12,20 @@ // PSPRECOMP_TEST_PAD_RECORD=1 log the pad of a normal (harness) walk: "[PADREC] ", and a // "[PADREC] loaded " line when the save finishes loading // PSPRECOMP_TEST_DIRECT_BATTLE= replay the recording (tools/battle_test.py --direct, tests/replays/quick_battle_walk.txt) -// Both are off by default; with neither set the filter returns the pad state unchanged. +// PSPRECOMP_TEST_CHAR_SWAP==[,=] fight with other characters than the recording picked (tools/fps_parity.py), +// so the Squall vs Lightning walk loads any match-up. The menu fills the battle block at 0x09AFB8A0 +// (FUN_088FB1A4 copies it for the battle): per side a 0x1060-byte fighter record that starts with a copy of +// the character's 0x778-byte save record (byte 0 = charId, then level, abilities ...; the CPU side gets a +// generated one), and behind them a team struct per side (+0x272F, stride 0x38: a count byte, five bytes, +// the charIds, 0xFF filled). On every poll a side whose record holds gets 's own save record +// (save records: *(0x089D712C) + 0xF58 + 0x778 x the charId's index in the 31-entry id list at 0x089CBC98) +// and its team charId; the CPU side's generated record only gets the charId. What the loading +// screen (FUN_0888C4C8) loads is the record's +0xF3A halfword: the object table id of the fighter's +// model entry (P__1P: byte 6 charId, byte 7 costume 1, byte 8 type 0 (Prishe: 16); table *(0x089D4968)); +// it is set to 's entry on every poll. The assist (+0xF3E) stays as the menu picked it. +// All are off by default; with none set the filter returns the pad state unchanged. #include "psp_game_module.h" +#include "hle/psp_hle.h" #include "psp_platform.h" #include "psp_runtime.h" #include "recomp.h" @@ -21,6 +33,7 @@ #include #include #include +#include #include extern std::atomic g_savedata_list_opened, g_savedata_completed; // runtime/src/hle/psp_hle_utility.cpp @@ -42,8 +55,73 @@ uint32_t g_cur = 0; } // namespace +namespace { +uint8_t* g_rdram = nullptr; +std::vector> g_swap; // PSPRECOMP_TEST_CHAR_SWAP +uint8_t* g_at(uint32_t a) { return g_rdram + (a & 0x07FFFFFFu); } +uint32_t g_w32(uint32_t a) { uint32_t v; std::memcpy(&v, g_at(a), 4); return v; } +uint32_t save_record(int charId) { // the save record of a charId (0 = none) + for (uint32_t i = 0; i < 31; i++) { + if (*g_at(0x089CBC98u + i) != charId) continue; + const uint32_t base = g_w32(0x089D712Cu); + return base >= 0x08800000u && base < 0x0A000000u ? base + 0xF58u + 0x778u * i : 0u; + } + return 0; +} +int model_id(int charId) { // object table id of a character's first-costume player model entry (P__1P), -1 = none + const uint32_t t = g_w32(0x089D4968u); + if (t < 0x08800000u || t >= 0x0A000000u) return -1; + const uint32_t h = g_w32(t); + if (h < 0x08800000u || h >= 0x0A000000u) return -1; + const uint32_t n = g_w32(h + 4u); + for (uint32_t i = 0; i < n && i < 4000u; i++) { + const uint8_t* r = g_at(h + 8u + 0x44u * i); + if (r[6] != charId || r[7] != 1 || r[8] == 5 || r[8] == 8 || r[8] == 13) continue; // costume 1, not an assist (8 / 13) or manikin (5): Prishe's is type 16 + const char* name = reinterpret_cast(r + 36); + const size_t len = strnlen(name, 16); + if (len < 4 || std::strncmp(name, "P_", 2) != 0 || std::strncmp(name + len - 3, "_1P", 3) != 0) continue; + uint16_t id; std::memcpy(&id, r, 2); + return id; + } + return -1; +} +void team_swap() { + constexpr uint32_t kBlock = 0x09AFB8A0u, kSide = 0x1060u, kTeam = 0x2735u, kTeamStride = 0x38u; + static int done[2] = {-1, -1}; // the pair applied to each side (one per side: "15=31,31=15" must not flip a side back) + static int want[2] = {-2, -2}, from_id[2] = {-2, -2}; + for (uint32_t side = 0; side < 2; side++) { + uint8_t* rec = g_at(kBlock + kSide * side); + uint8_t* ids = g_at(kBlock + kTeam + kTeamStride * side); + if (done[side] >= 0 && rec[0] != g_swap[done[side]].second) done[side] = -1; // the menu wrote the record again + if (done[side] < 0) { + for (size_t k = 0; k < g_swap.size(); k++) { + const auto& s = g_swap[k]; + if (rec[0] != s.first || rec[1] < 1 || rec[1] > 100) continue; // a fighter record (charId, level 1..100) + const uint32_t src = side == 0 ? save_record(s.second) : 0u; + if (src) std::memcpy(rec, g_at(src), 0x778); + rec[0] = static_cast(s.second); + done[side] = static_cast(k); + want[side] = model_id(s.second); from_id[side] = model_id(s.first); + std::fprintf(stderr, "[DIRECT] side %u fighter record: charId %d -> %d (%s)\n", side, s.first, s.second, src ? "its own save record" : "charId only"); + break; + } + } + if (done[side] < 0) continue; + const auto& s = g_swap[done[side]]; + if (ids[-6] == 1 && ids[0] == s.first && ids[1] == 0xFF) ids[0] = static_cast(s.second); // a 1-on-1 team struct + uint16_t m; std::memcpy(&m, rec + 0xF3A, 2); // the model the loading screen loads: 's (the menu sets it from its own pick) + if (want[side] >= 0 && m != want[side] && (m == from_id[side] || from_id[side] < 0)) { + const uint16_t w = static_cast(want[side]); + std::memcpy(rec + 0xF3A, &w, 2); + std::fprintf(stderr, "[DIRECT] side %u model id %u -> %u (charId %d)\n", side, m, w, s.second); + } + } +} +} // namespace + // Called by the pad filter of framerate.cpp for every poll; returns the buttons to hand the game. uint32_t dissidia_walk_filter(uint32_t buttons) { + if (!g_swap.empty()) team_swap(); if (!g_replay && !g_record) return buttons; const uint64_t n = ++g_poll; if (g_record) { @@ -81,8 +159,28 @@ uint32_t dissidia_walk_filter(uint32_t buttons) { } } +// FUN_08846E18(table, charId, costume): the object table entry (byte 6 charId, byte 7 costume) of a character's model files. +FuncPtr g_orig_objfind = nullptr; +void objfind_hook(uint8_t* rdram, recomp_context* ctx) { + const int id = static_cast(ctx->r[5]); + static int logged = 0; + if (logged++ < 40) std::fprintf(stderr, "[DIRECT] objfind charId %d costume %d ra=%08X\n", id, static_cast(ctx->r[6]), static_cast(ctx->r[31])); + g_orig_objfind(rdram, ctx); +} + void dissidia_direct_battle_install(uint8_t* rdram) { - (void)rdram; + g_rdram = rdram; + if (std::getenv("PSPRECOMP_TEST_OBJFIND_LOG") && (g_orig_objfind = psp_dispatch_probe_lookup(0x08846E18u))) psp_dispatch_register(0x08846E18u, objfind_hook); + if (const char* sw = std::getenv("PSPRECOMP_TEST_CHAR_SWAP"); sw && sw[0]) { + for (const char* q = sw; *q;) { + int a = 0, b = 0, used = 0; + if (std::sscanf(q, "%d=%d%n", &a, &b, &used) != 2) break; + if (a > 0 && a < 255 && b > 0 && b < 255) g_swap.push_back({a, b}); + q += used; + if (*q == ',') q++; + } + std::fprintf(stderr, "[DIRECT] character swap: %zu pair(s) from %s\n", g_swap.size(), sw); + } if (const char* r = std::getenv("PSPRECOMP_TEST_PAD_RECORD"); r && r[0] && r[0] != '0') { g_record = true; std::fprintf(stderr, "[DIRECT] recording the pad (PADREC lines)\n"); diff --git a/psprecomp/games/dissidia/runtime/framerate.cpp b/psprecomp/games/dissidia/runtime/framerate.cpp index 417f64f..3820dbd 100644 --- a/psprecomp/games/dissidia/runtime/framerate.cpp +++ b/psprecomp/games/dissidia/runtime/framerate.cpp @@ -45,6 +45,11 @@ #include "psp_screenfx.h" #include "psp_present_sync.h" #include +#if defined(__GLIBC__) || defined(__APPLE__) // PSPRECOMP_TASK_TRACE's backtraces (not on Android API < 33 or Windows) +#define PSPRECOMP_HAVE_BACKTRACE 1 +#include +#include +#endif #ifdef __linux__ #include #endif @@ -90,6 +95,7 @@ constexpr uint32_t CPU_AI = 0x08A75D38u; // battle overlay: CPU AI action constexpr uint32_t STEER_MOVE = 0x08A2C960u; // battle overlay: ground steered move controller (Squall's Revolver Drive rush): a0 = out vector (metres to move this update) constexpr uint32_t UI_PARAM_STEP = 0x088DA520u; // EBOOT: UI animation parameter stepper (value += rate x UI step toward a target) constexpr uint32_t UI_MGR_PTR = 0x089D7830u; // EBOOT: pointer to the UI manager (+33: use the frame dt as the UI step, +34: paused) +constexpr uint32_t UI_UPDATE = 0x088CF550u; // EBOOT: the UI update (sets the UI step, runs the HUD / banner animations and their state) constexpr uint32_t UI_STEP = 0x089D7834u; // EBOOT: the UI step FUN_088CF550 sets every update (0 paused, 1.0 fixed, or the frame dt) constexpr uint32_t OWN_MASK0 = 0x089C7FD8u, OWN_MASK1 = 0x089C7FDCu; constexpr uint32_t EXB_PRESSED = 0x08B326C4u; @@ -111,7 +117,32 @@ int g_interval = 2; // interval written on the last frame end (2 int g_rate = 30; // logic rate in effect (30, 60, 90, ... one frame per vblank above 30) double g_play_ms = 0; // wall-independent play time: advances by the frame's ticks (every frame, menus and battle) bool g_input_dbg = false; // PSPRECOMP_INPUT_DEBUG=1: pressed buttons on screen and in the log -float g_ticks = 2.0f; // 1/60 s ticks one frame advances: 60 / g_rate +float g_ticks = 2.0f; // 1/60 s ticks one frame advances: 60 / g_rate (the step the update that just ran used) +// Exact steps (PSPRECOMP_EXACT_STEP=0 turns it off). At 90 / 150 / 180 / 210 fps the step 60 / rate (2/3, 0.4, 1/3, 2/7 ticks) is not exact in +// floating point: every clock the game sums it into (motion time, timeline keys, countdowns such as the rehit timer, Jecht's chain windows) +// lands a hair short of a whole tick, so an event keyed at tick 70 fired one update late (Exdeath's multi-hit every 177.8 ms at 90 fps +// instead of 166.7; Jecht's Rush chain window 41 ms late at 90 / 150; tools/fps_parity.py, combo_timing.py). Instead each update gets the +// next value of a fixed cycle of rate / 30 multiples of 1/256 tick that add up to exactly 2.0 (spread evenly, within 0.3 % of 60 / rate): +// any rate / 30 consecutive updates are exactly one 30 Hz frame, and every sum is exact. 60 / 120 / 240 fps (1, 0.5, 0.25) are exact already. +bool g_exact_step = true; +// 30 Hz logic (on by default; PSPRECOMP_LOGIC30=0 turns it off): above 30 fps only every rate/30-th frame runs the battle's gameplay, with the PSP's 2-tick +// step; the frames in between run the battle scene's task list with the pause filter (mask / value 4: what the game runs while paused, +// every gameplay task skipped) and a zero step, so the game still draws but nothing advances. Gameplay is then the 30 fps game's by +// construction; smoothing the frames in between is the renderer's job. The game's own frame-rate hooks see 30 fps (g_ticks 2). +bool g_logic30 = true; +bool l30_active(); // 30 Hz logic is on and the fight is past its intro +uint64_t g_l30_frame = 0; +bool g_l30_logic_now = true; // the frame being run is a gameplay frame +uint64_t g_l30_calls[2][2] = {}; // diagnostic (PSPRECOMP_L30_TIMING): [0 UI update, 1 effect components][0 gameplay, 1 drawn-only frame] +float g_logic_step = 2.0f; // ticks of gameplay the frame that just ran advanced (2 or 0 in 30 Hz logic mode, else g_ticks) +uint64_t g_step_idx = 0; // index of the step the next update uses +float step_for(int rate, uint64_t i) { + const int n = rate / 30; + if (n <= 1 || !g_exact_step || (n & (n - 1)) == 0) return 60.0f / static_cast(rate); // 30, 60, 120, 240: exact + const uint64_t j = i % static_cast(n); + const uint64_t a = (j + 1) * 512u / static_cast(n), b = j * 512u / static_cast(n); // 512 = 2 ticks in 1/256 units + return static_cast(a - b) / 256.0f; +} int g_fx_period = 1; // updates per effect-component step at 30 Hz pacing: g_rate / 30 bool g_scope_all = false, g_log = false; int g_switch_logs = 0; @@ -223,6 +254,21 @@ bool g_scan_seeded = false; // `scan`: the candidate lists exist (second scan bool g_test_ai_off = false; // PSPRECOMP_TEST_AI_OFF=1: the CPU fighter's pad never gets a button (no AI input) but runs at the normal time step, so it takes hits and reacts bool g_test_freeze_cpu = false; // PSPRECOMP_TEST_FREEZE_CPU=1: every fighter but the first one seen gets time step 0 while a test runs (a standing dummy) int g_player_char = -1; // PSPRECOMP_TEST_PLAYER_CHAR=: the fighter under test (default: the first one seen) +// PSPRECOMP_TEST_PARITY=1 (tools/fps_parity.py): frame-rate parity runs. +// - `[PAR]` sample lines on the 30 Hz lattice (game time k x 33.3 ms; every rate the game offers is a multiple of 30 Hz, so each one +// has an update exactly there): the player's and the dummy's script / motion / motion time / position / facing, and the hit count; +// - `[PARHIT]` for every hit a fighter takes (FUN_0883A150, the take-hit routine): attack (atkp) id, reaction, flags, HP damage; +// - `sync` / `waitidle` end on the lattice (the next lattice update), so `+` events land on the same game moment at every rate; +// - event times are compared with a small tolerance (the sum of 8.33 ms steps can be 99.99999 at 100 ms, a whole update late). +bool g_parity = false; +uint64_t g_par_updates = 0; // logic updates since the test clock started +uint32_t g_par_hits[2] = {}; // hits taken by the player [0] / the dummy [1] since the clock started +uint32_t g_par_scratch = 0; // 16 guest bytes for the facing vector handed to FUN_08835BA4 +// PSPRECOMP_TEST_INPUT_DELAY= (parity runs above 30 fps, experiments only): scripted pad changes reach the game this much game time later. +// Not a latency fix: a press starts the motion on the same update at every rate (motion time 0 on the first update); what differs is +// how far the fighter moves on that first update (see docs/testing.md "Frame-rate parity tests"). +double g_par_input_delay = 0; +bool g_mt_snap = false; // PSPRECOMP_MT_SNAP=1, see mt_snap() uint32_t g_human_vt = 0; // vtable of the human's controller object (found at the first battle: the one a scripted press reaches) FILE* g_record = nullptr; char g_record_path[512] = {}; @@ -278,9 +324,11 @@ void parse_script(std::istream& in, std::vector& ev) { } else if (cmd == "poke") { // poke : write one word of a fighter (finding which stat a number on the HUD comes from) std::string who = "player", off; long v = 0; ss >> who >> off >> v; ev.push_back({t, 15, static_cast(std::strtoul(off.c_str(), nullptr, 16)), static_cast(v), who, rel}); - } else if (cmd == "place") { // place : the player stands that far from the dummy (on the line they were on), at once - double d = 4; ss >> d; - ev.push_back({t, 12, static_cast(d * 100.0 + 0.5), 0, "", rel}); + } else if (cmd == "place" || cmd == "home") { // place : the player stands that far from the dummy (on the line they were on), at once + double d = 4; ss >> d; // home [x z]: the same, the dummy at stage point x,z (default: where it stood at the + std::string xz; // first place / home), both fighters turned to face each other (a trial's fixed start) + if (cmd == "home") { double x, z; if (ss >> x >> z) xz = std::to_string(x) + " " + std::to_string(z); } + ev.push_back({t, 12, static_cast(d * 100.0 + 0.5), cmd == "home" ? 1u : 0u, xz, rel}); } else if (cmd == "set" || cmd == "tap") { std::string b; ss >> b; const uint32_t m = parse_buttons(b); @@ -333,11 +381,14 @@ void testdrive_load(const char* path) { } uint32_t assist_gauge_addr(int side); +// Updates without a fighter update after which a fighter counts as gone (the fight is over). Parity runs allow 20 s: an HP attack can +// stop both fighters for ~2 s (a hit freeze with no fighter update), which ended the test clock and restarted the script mid-run. +uint64_t stale_ticks() { return g_parity ? static_cast(20 * std::max(30, g_rate)) : 60u; } bool battle_running() { if (overlay_id() != 1) return false; for (int i = 0; i < 4; i++) { const uint32_t a = g_fighters[i]; - if (a && ok(a) && g_tick - g_fighter_seen[i] < 60) return true; + if (a && ok(a) && g_tick - g_fighter_seen[i] < stale_ticks()) return true; } return false; } @@ -364,7 +415,7 @@ uint32_t test_player() { // the fighter under test uint32_t first = 0; for (int i = 0; i < 4; i++) { const uint32_t a = g_fighters[i]; - if (!a || !ok(a) || g_tick - g_fighter_seen[i] > 60) continue; + if (!a || !ok(a) || g_tick - g_fighter_seen[i] > stale_ticks()) continue; if (!first) first = a; if (g_player_char >= 0 && fighter_char(a) == g_player_char) return a; } @@ -374,7 +425,7 @@ uint32_t test_fighter(uint32_t who) { uint32_t list[4]; int n = 0; for (int i = 0; i < 4; i++) { const uint32_t a = g_fighters[i]; - if (a && ok(a) && g_tick - g_fighter_seen[i] < 60) list[n++] = a; + if (a && ok(a) && g_tick - g_fighter_seen[i] < stale_ticks()) list[n++] = a; } if (who >= 2) return static_cast(who - 2) < n ? list[who - 2] : 0u; const uint32_t pl = test_player(); @@ -384,7 +435,7 @@ uint32_t test_fighter(uint32_t who) { void test_describe_fighters() { for (int i = 0; i < 4; i++) { const uint32_t a = g_fighters[i]; - if (!a || !ok(a) || g_tick - g_fighter_seen[i] > 60) continue; + if (!a || !ok(a) || g_tick - g_fighter_seen[i] > stale_ticks()) continue; const uint32_t ctrl = r32(a + 0x5C4u), info = r32(a + 0x1108u - 0x1108u + 0x0u); (void)info; std::fprintf(stderr, "[TEST] fighter %08X char=%d ctrl=%08X vt=%08X scr=%u pos=%.2f,%.2f\n", a, fighter_char(a), ctrl, ok(ctrl) ? r32(ctrl) : 0u, r32(a + 0x540u) & 0xFFFFu, rdf(a + 0x80u), rdf(a + 0x88u)); @@ -439,6 +490,11 @@ void testdrive_frame(uint8_t* rdram, recomp_context* ctx) { } if (!g_gt_started) { g_gt_started = true; g_gt_ms = 0; g_test_next = 0; g_test_buttons = 0; g_test_analog = 0x8080; g_wait_kind = 0; g_ref_gt = 0; + g_par_updates = 0; g_par_hits[0] = g_par_hits[1] = 0; + if (g_parity) { // PSPRECOMP_TEST_NO_DRAW=: nothing needs a picture once the clock runs (the harness has seen the HUD); runs go several times faster + static const char* nd = std::getenv("PSPRECOMP_TEST_NO_DRAW"); + if (nd && nd[0] && nd[0] != '0') ge_draw_request_no_raster(std::atoi(nd)); + } std::fprintf(stderr, "[TEST] battle started (tick %llu)\n", (unsigned long long)g_tick); if (g_record_path[0]) { // one file per fight: , .2, .3 ... static int fights = 0; @@ -452,8 +508,10 @@ void testdrive_frame(uint8_t* rdram, recomp_context* ctx) { std::fprintf(stderr, "[TEST] recording to %s\n", path); } } else { - g_gt_ms += g_ticks * (1000.0 / 60.0); + g_gt_ms += g_logic_step * (1000.0 / 60.0); + g_par_updates++; } + const bool lattice = !g_parity || (l30_active() ? g_logic_step > 0.0f : g_par_updates % static_cast(std::max(1, g_rate / 30)) == 0); // a 30 Hz frame boundary (every update below 60 fps; the gameplay frames in 30 Hz logic mode) if (g_record) { uint32_t b, a; psp_ctrl_last_sample(&b, &a); if (b != g_rec_buttons || a != g_rec_analog) { @@ -469,9 +527,19 @@ void testdrive_frame(uint8_t* rdram, recomp_context* ctx) { static uint32_t keep_actor = 0, keep_hp = 0; for (int i = 0; i < 4; i++) { const uint32_t a = g_fighters[i]; - if (!a || !ok(a) || g_tick - g_fighter_seen[i] > 60 || a == test_player()) continue; + if (!a || !ok(a) || g_tick - g_fighter_seen[i] > stale_ticks() || a == test_player()) continue; if (a != keep_actor || !g_gt_started || g_gt_ms < 34) { keep_actor = a; keep_hp = r32(a + 0x10B0u); } else if (keep_hp) w32(a + 0x10B0u, g_test_ai_off ? 100000000u : keep_hp); // AI-off dummies get a huge pool: once broken, hits do HP damage and the KO check runs inside the update, before this write + if (g_test_ai_off) { // the CPU's virtual pad (brain actor+0x5C4, pad brain+0x53C: +4 state, +8 pending, +12 previous) keeps what the brain held when it + // stopped (its setters are blocked): a CPU caught mid-dash at the start dashed for the whole run. Nothing stays held. + const uint32_t brain = r32(a + 0x5C4u); + if (ok(brain)) for (uint32_t o : {4u, 8u, 12u}) w32(brain + 0x53Cu + o, 0u); + } + if (g_parity || g_test_ai_off) { // +0x10B0 is only the maximum: the live HP is the status block's (actor+0x52C: +0x8 HP damage taken, +0xC HP; docs/tag-team.md). No damage + // piles up (the Jecht combo sweep's HP finishers KO'd the dummy after ~10 min of game time: every later trial timed out) + const uint32_t st = r32(a + 0x52Cu); + if (ok(st)) w32(st + 0x8u, 0u); + } } } // the player's action (script id): every change is logged, and sync / waitidle watch it @@ -514,13 +582,15 @@ void testdrive_frame(uint8_t* rdram, recomp_context* ctx) { const bool done = g_wait_kind == 10 ? (g_wait_target >= 0 ? static_cast(cur_scr) == g_wait_target : cur_scr != g_wait_scr) : (cur_scr == 0xFFFFu && dummy_neutral); const bool late = g_gt_ms - g_wait_since > g_wait_timeout; if (!done && !late) break; + if (!lattice) break; // parity runs: the wait ends on the next 30 Hz frame boundary, the same game moment at every rate std::fprintf(stderr, "[TEST] gt=%.0f %s %s (scr %d)\n", g_gt_ms, g_wait_kind == 10 ? "sync" : "waitidle", done ? "done" : "TIMEOUT", cur_scr == 0xFFFFu ? -1 : static_cast(cur_scr)); - g_ref_gt = g_gt_ms; + g_ref_gt = g_parity ? std::round(g_gt_ms * 0.03) / 0.03 : g_gt_ms; // (on the lattice: k x 33.3 ms without the rounding error of the sum) g_wait_kind = 0; } if (g_test_next >= g_test_events.size()) break; const TestEvent& e = g_test_events[g_test_next]; - if (g_gt_ms < (e.rel ? g_ref_gt + e.t : e.t)) break; + const double delay = (e.kind == 0 || e.kind == 1) && g_rate > 30 ? g_par_input_delay : 0.0; // pad input only + if (g_gt_ms + (g_parity ? 0.01 : 0.0) < (e.rel ? g_ref_gt + e.t : e.t) + delay) break; g_test_next++; if (e.kind == 13) { // batch: wait for the file, then splice its events in here if (FILE* f = std::fopen(e.text.c_str(), "r")) { @@ -584,6 +654,7 @@ void testdrive_frame(uint8_t* rdram, recomp_context* ctx) { static float hx = 0, hz = 0; const uint32_t me = test_player(), dm = test_fighter(1); if (me && dm) { + if (!e.text.empty()) { std::sscanf(e.text.c_str(), "%f %f", &hx, &hz); have_home = true; } if (!have_home) { hx = rdf(dm + 0x110u); hz = rdf(dm + 0x118u); have_home = true; } auto put = [](uint32_t a, float x, float z) { for (uint32_t o : {0x30u, 0x80u, 0x100u, 0x110u}) { uint32_t u; std::memcpy(&u, &x, 4); w32(a + o, u); } @@ -592,7 +663,42 @@ void testdrive_frame(uint8_t* rdram, recomp_context* ctx) { const float d = e.a / 100.0f; put(dm, hx, hz); put(me, hx - d, hz); - std::fprintf(stderr, "[TEST] gt=%.0f place: dummy at %.2f,%.2f, player %.2f m away\n", g_gt_ms, hx, hz, d); + if (e.b == 1) { // home: both fighters' status blocks (actor+0x52C, 0x80 bytes: +0x8 HP damage, +0xC HP, +0x14 bravery , break + // state ...) as at the first home, so a trial does not inherit a break, a big bravery or HP damage from the one before (with only HP and + // bravery put back, the same HP attack did 465 or 1254 damage in two 30 fps runs) + static uint8_t first[2][0x80]; + static bool have_stats = false; + for (int w = 0; w < 2; w++) { + const uint32_t st = r32((w ? dm : me) + 0x52Cu); + if (!ok(st)) continue; + uint8_t* p = g_ram + (st & 0x07FFFFFFu); + if (!have_stats) std::memcpy(first[w], p, sizeof first[w]); else std::memcpy(p, first[w], sizeof first[w]); + // +0x14 holds ; the first home's snapshot catches whatever the current bravery was then + // (0 right after a break in one run, the initial in another: a steady HP damage offset over a whole run). Every trial starts + // with the initial bravery. + const uint32_t bv = r32(st + 0x14u); + w32(st + 0x14u, (bv & 0xFFFF0000u) | (bv >> 16)); + } + have_stats = true; + if (const uint32_t st = r32(me + 0x52Cu); ok(st)) // what every trial starts with (bravery = +0x14) + std::fprintf(stderr, "[TEST] gt=%.0f home status me %d %d %d %d %d %d %d %d\n", g_gt_ms, (int)r32(st), (int)r32(st + 4u), (int)r32(st + 8u), + (int)r32(st + 0xCu), (int)r32(st + 0x10u), (int)r32(st + 0x14u), (int)r32(st + 0x18u), (int)r32(st + 0x1Cu)); + // face each other (FUN_08835BA4(actor, &dir): yaw from the direction's x / z) + static FuncPtr turn = psp_dispatch_probe_lookup(0x08835BA4u); + if (!g_par_scratch) g_par_scratch = psp_kmem_bump_alloc_named(16, "TEST_FACE_VEC"); + if (turn && g_par_scratch) { + const recomp_context keep = *ctx; + for (int w = 0; w < 2; w++) { + wrf(g_par_scratch, w ? -1.0f : 1.0f); wrf(g_par_scratch + 4u, 0.0f); wrf(g_par_scratch + 8u, 0.0f); // the player looks along +x (at the dummy), the dummy along -x + recomp_context c = *ctx; + c.r[4] = static_cast(w ? dm : me); c.r[5] = static_cast(g_par_scratch); + turn(rdram, &c); + } + *ctx = keep; + } + for (uint32_t a : {me, dm}) for (uint32_t o : {0xE0u, 0xE4u, 0xE8u}) wrf(a + o, 0.0f); // no velocity left over from the last trial + } + std::fprintf(stderr, "[TEST] gt=%.0f %s: dummy at %.2f,%.2f, player %.2f m away\n", g_gt_ms, e.b == 1 ? "home" : "place", hx, hz, d); } } else if (e.kind == 0) { g_test_buttons = e.a; if (e.text == "set") g_test_analog = e.b; } @@ -600,7 +706,10 @@ void testdrive_frame(uint8_t* rdram, recomp_context* ctx) { else if (e.kind == 2) std::fprintf(stderr, "[TEST] gt=%.0f mark%s\n", g_gt_ms, e.text.c_str()); else if (e.kind == 4) { const uint32_t a = test_fighter(e.b); + const float keep_step = rdf(FRAME_STEP); + if (l30_active()) wrf(FRAME_STEP, 2.0f); // 30 Hz logic: the move starts with the gameplay step, as one started inside a gameplay update const int r = a ? dissidia_test_action(rdram, ctx, a, static_cast(e.a)) : -2; + if (l30_active()) wrf(FRAME_STEP, keep_step); std::fprintf(stderr, "[TEST] gt=%.0f action %d on %08X (%s): %s\n", g_gt_ms, static_cast(e.a), a, e.b == 1 ? "cpu" : "player", r == 1 ? "started" : r == 0 ? "NOT started" : r == -1 ? "no such record" : "no such fighter"); } @@ -642,6 +751,35 @@ void testdrive_frame(uint8_t* rdram, recomp_context* ctx) { else if (e.kind == 3) { std::fprintf(stderr, "[TEST] quit at gt=%.0f ms\n", g_gt_ms); g_should_exit.store(true); } } psp_ctrl_set_script(true, g_test_buttons, g_test_analog); + if (g_parity) { // a sample every update (fps_parity.py compares on the 30 Hz lattice and aligns paths by whole updates): both fighters at this game moment + const uint32_t pa = test_player(), da = test_fighter(1); + char buf[512]; int n = std::snprintf(buf, sizeof buf, "[PAR] %.2f%s", g_gt_ms, lattice ? "" : "~"); + for (uint32_t a : {pa, da}) { + if (!a || !ok(a)) { n += std::snprintf(buf + n, sizeof buf - n, " | -"); continue; } + n += std::snprintf(buf + n, sizeof buf - n, " | %d %d %.4f %.3f %.3f %.3f %.3f %.3f", static_cast(static_cast(r32(a + 0x540u) & 0xFFFFu)), + static_cast(static_cast(r32(a + 0x180u) & 0xFFFFu)), rdf(a + 0x1A8u), rdf(a + 0x110u), rdf(a + 0x114u), rdf(a + 0x118u), rdf(a + 0x90u), rdf(a + 0x98u)); + } + std::fprintf(stderr, "%s | %u %u\n", buf, g_par_hits[0], g_par_hits[1]); + } +} + +// Parity runs: every hit a fighter takes. FUN_0883A150(a0 = defender, a1 = the 64-byte hit record: +0x20 attacker, +0x28 flags, +0x30 atkp record +// (+0 id), +0x34 reaction, +0x38 HP damage); see docs/archive/hit-reaction. +FuncPtr g_orig_take_hit = nullptr; +void take_hit_hook(uint8_t* rdram, recomp_context* ctx) { + const uint32_t def = static_cast(ctx->r[4]), rec = static_cast(ctx->r[5]); + if (g_parity && g_gt_started && ok(def) && ok(rec)) { + const uint32_t pa = test_player(); + const int who = def == pa ? 0 : 1; + if (def == pa || def == test_fighter(1)) { + g_par_hits[who]++; + const uint32_t atkp = r32(rec + 0x30u), att = r32(rec + 0x20u); + std::fprintf(stderr, "[PARHIT] %.0f %s atkp=%d react=%u flags=%X dmg=%d by=%s\n", g_gt_ms, who ? "dummy" : "player", + ok(atkp) ? static_cast(static_cast(r32(atkp) & 0xFFFFu)) : -1, static_cast(g_ram[(rec + 0x34u) & 0x07FFFFFFu]), + r32(rec + 0x28u), static_cast(r32(rec + 0x38u)), att == pa ? "player" : (ok(att) && g_ram[(att + 0x542u) & 0x07FFFFFFu] != 0 ? "object" : "other")); + } + } + g_orig_take_hit(rdram, ctx); } // ---- Debug menu: Jecht combo timing helper ------------------------------------------------------------------------------------------ @@ -694,6 +832,12 @@ uint32_t dissidia_walk_filter(uint32_t buttons); // direct_battle.cpp namespace { uint32_t jecht_grid_filter(uint32_t raw) { raw = dissidia_walk_filter(raw); // direct_battle.cpp: test-only pad record / replay of the menu walk (a no-op unless asked for) + if (l30_active()) { // 30 Hz logic: the pad changes only on gameplay frames (the PSP reads it once per 30 Hz frame); + static uint32_t shown = 0, latched = 0; // a press that came and went between two of them is shown once (latched) + latched |= raw & ~shown; + if (g_l30_logic_now) { shown = raw | (latched & ~shown); latched = 0; } + return shown; + } static uint32_t held = 0, pend = 0; static uint64_t decided = ~0ull; bool jecht = false; @@ -895,6 +1039,10 @@ void testdrive_install() { if (const char* ao = std::getenv("PSPRECOMP_TEST_AI_OFF"); ao && ao[0] && ao[0] != '0') g_test_ai_off = true; if (const char* fz = std::getenv("PSPRECOMP_TEST_FREEZE_CPU"); fz && fz[0] && fz[0] != '0') g_test_freeze_cpu = true; if (const char* pc = std::getenv("PSPRECOMP_TEST_PLAYER_CHAR"); pc && pc[0]) g_player_char = std::atoi(pc); + if (const char* pp = std::getenv("PSPRECOMP_TEST_PARITY"); pp && pp[0] && pp[0] != '0') g_parity = true; + if (const char* id = std::getenv("PSPRECOMP_TEST_INPUT_DELAY"); id && id[0]) g_par_input_delay = std::atof(id); + if (const char* ms = std::getenv("PSPRECOMP_MT_SNAP"); ms && ms[0] == '1') g_mt_snap = true; + if (const char* es = std::getenv("PSPRECOMP_EXACT_STEP"); es && es[0] == '0') g_exact_step = false; g_test_go = std::getenv("PSPRECOMP_TEST_GO"); if (g_test_go && !g_test_go[0]) g_test_go = nullptr; const char* e = std::getenv("PSPRECOMP_TEST_SCRIPT"); @@ -925,6 +1073,75 @@ void note_negotiated_rate(int cap) { shown = cap; } + +// The global step (0x089D5EAC, what actors move by) is copied from the frame timer by the root task FUN_08864DD8, which runs near the end of a +// frame (after the battle's tasks): what it computes is used by the NEXT frame, while the timer's own step (+0x18, read directly by the +// battle code too) is the current frame's. With a constant step that does not matter; with a varying one (exact steps, 30 Hz logic) the two +// disagreed by a frame (30 Hz logic: direct readers got 0 on the gameplay frame). While the task runs, the timer shows the NEXT frame's step. +FuncPtr g_orig_global_step = nullptr; +// 30 Hz logic: is the frame after the current one a gameplay frame? Decided once per frame (the global step needs it before the frame ends). +// Paced by the clock (play): a gameplay frame is due every 33.3 ms of real time, so a late frame does not leave the picture behind and the +// game keeps its speed when frames are dropped; the frames between are blended by the time that has passed (g_l30_next_phase). Paced +// by frame count (tests: PSPRECOMP_TEST_PARITY / _TEST_SCRIPT / VCLOCK, or PSPRECOMP_L30_PACE=frames): every rate/30-th frame, repeatable. +uint64_t g_l30_dec_frame = ~0ull; +bool g_l30_next_logic = true; +float g_l30_next_phase = 0.0f; +double g_l30_due_ms = 0, g_l30_last_ms = 0; // when the next gameplay frame is due / the last one was (real time, ms) +double g_l30_end_ms = 0; // when the last frame ended (frame_end_hook, after the frame's wait: a steady point) +GeInterpTag g_l30_list_tag; // the interpolation tag of the frame that just ended: its display list is submitted after frame_end_hook (see there) +double l30_now_ms() { return std::chrono::duration(std::chrono::steady_clock::now().time_since_epoch()).count(); } +bool l30_clock_paced() { + static const int m = [] { + const char* e = std::getenv("PSPRECOMP_L30_PACE"); + if (e && !std::strcmp(e, "frames")) return 0; + if (e && !std::strcmp(e, "clock")) return 1; + return std::getenv("PSPRECOMP_TEST_PARITY") || std::getenv("PSPRECOMP_TEST_SCRIPT") || std::getenv("PSPRECOMP_VCLOCK") ? 0 : 1; + }(); + return m == 1; +} +bool l30_decide_next() { + if (g_l30_dec_frame == g_l30_frame) return g_l30_next_logic; + g_l30_dec_frame = g_l30_frame; + const uint64_t n = static_cast(std::max(1, g_rate / 30)); + if (!l30_clock_paced()) { + g_l30_next_logic = (g_l30_frame + 1) % n == 0; + g_l30_next_phase = static_cast((g_l30_frame + 1) % n) / static_cast(n); + return g_l30_next_logic; + } + // Timed from the end of the last frame, not from now: this runs part way through the frame, later on a gameplay frame (more work + // before it) than on a drawn-only one, and timing from there gave uneven phases (0, 0.26, 0.73, 0.23 at a steady 60 fps: jitter). + const double frame = 1000.0 / g_rate, period = 1000.0 / 30.0; + const double now = l30_now_ms(); + const double shown = g_l30_end_ms > 0 && now - g_l30_end_ms < 4 * frame ? g_l30_end_ms + 2 * frame : now + frame; // the frame after this one reaches the screen about two frame times after the last frame ended + if (g_l30_due_ms == 0 || shown - g_l30_due_ms > 100.0) g_l30_due_ms = shown; // start, or far behind (a hitch): catch up at once + g_l30_next_logic = shown + frame * 0.5 >= g_l30_due_ms; // the 30 Hz step falls within this frame (nearest frame) + if (g_l30_next_logic) { g_l30_last_ms = g_l30_due_ms; g_l30_due_ms += period; } + // not clamped at 0: a gameplay frame shown a little before its slot gets a small negative phase (the blend runs that bit back from + // the step before); clamping it made the steps uneven (0 -> 0.31 -> 1.0 at 60 fps instead of a steady half step) + g_l30_next_phase = static_cast(std::clamp((shown - g_l30_last_ms) / period, -0.5, 1.0)); + static int dlog = [] { const char* e = std::getenv("PSPRECOMP_L30_PACE_LOG"); return e ? std::atoi(e) : 0; }(); + if (dlog > 0 && g_battle_age > 300.f) { dlog--; std::fprintf(stderr, "[L30P] frame %llu now %.2f shown %.2f due %.2f last %.2f -> %s phase %.2f\n", (unsigned long long)g_l30_frame, + now, shown, g_l30_due_ms, g_l30_last_ms, g_l30_next_logic ? "GAMEPLAY" : "drawn", g_l30_next_phase); } + return g_l30_next_logic; +} +float next_frame_step() { + if (l30_active()) return l30_decide_next() ? 2.0f : 0.0f; + if (g_rate > 60) return step_for(g_rate, g_step_idx + 1); + return -1.0f; // constant step: leave it +} +void global_step_hook(uint8_t* rdram, recomp_context* ctx) { + const uint32_t timer = r32(0x089D69E0u); + const float nx = ok(timer) && overlay_id() == 1 ? next_frame_step() : -1.0f; + if (nx < 0.0f) { g_orig_global_step(rdram, ctx); return; } + if (l30_active() && nx == 0.0f) { wrf(FRAME_STEP, 0.0f); return; } // before a drawn-only frame: nothing moves, and the slow-motion clock (also + // advanced by this task) only runs on gameplay frames, as at 30 fps + const float cur = rdf(timer + 0x18u); + wrf(timer + 0x18u, nx); + g_orig_global_step(rdram, ctx); + wrf(timer + 0x18u, cur); +} + +void task_dump_frame(); void frame_end_hook(uint8_t* rdram, recomp_context* ctx) { const uint32_t timer = static_cast(ctx->r[4]); g_tick++; @@ -950,15 +1167,18 @@ void frame_end_hook(uint8_t* rdram, recomp_context* ctx) { } } g_interval = interval; + if (g_rate != rate) g_step_idx = 0; g_rate = rate; - g_ticks = 60.0f / static_cast(rate); + g_ticks = rate > 60 ? step_for(rate, g_step_idx) : 60.0f / static_cast(rate); // the step the update that just ran was given + g_logic_step = g_ticks; + if (l30_active()) { g_ticks = 2.0f; g_logic_step = g_l30_logic_now ? 2.0f : 0.0f; } g_fx_period = rate / 30; } g_orig_frame_end(rdram, ctx); - g_play_ms += g_ticks * (1000.0 / 60.0); + g_play_ms += 1000.0 / static_cast(std::max(30, g_rate)); input_debug_frame(); testdrive_frame(rdram, ctx); - if (battle_running()) g_battle_age += g_ticks; else g_battle_age = 0.f; + if (battle_running()) g_battle_age += g_logic_step; else g_battle_age = 0.f; { // tall windows: the picture is centred from the moment the fight runs until the battle overlay goes (summons, the pause menu and // the results keep it; the loading / opponent pages before the fight are at the top like the menus), see ge_draw_note_scene_view static bool in_battle = false; @@ -967,6 +1187,16 @@ void frame_end_hook(uint8_t* rdram, recomp_context* ctx) { if (in_battle) ge_draw_note_scene_view(); } fighter_log_frame(); + task_dump_frame(); + { // PSPRECOMP_L30_PRIMLOG= (diagnostic): GE prims per frame for n frames once 30 Hz logic runs ("G" gameplay frame, "d" drawn-only) + static int left = [] { const char* e = std::getenv("PSPRECOMP_L30_PRIMLOG"); return e ? std::atoi(e) : 0; }(); + static int last = 0; + if (left > 0 && l30_active() && g_battle_age > 600.f) { + int prims = 0; ge_draw_get_stats(nullptr, &prims, nullptr, nullptr, nullptr); + std::fprintf(stderr, "[L30PRIM] %s %d\n", g_logic_step > 0.0f ? "G" : "d", prims - last); + last = prims; left--; + } + } combo_frame(); if (g_battle_age > 420.f) g_ai_armed = true; else if (overlay_id() != 1) g_ai_armed = false; for (int i = 0; i < 4; i++) { @@ -975,7 +1205,68 @@ void frame_end_hook(uint8_t* rdram, recomp_context* ctx) { if (scr != g_prev_scr[i] && g_fighters[i] && human_jecht(g_fighters[i])) g_grid_anchor = g_tick; // the next update is the first read of the new move's grid g_prev_scr[i] = scr; } - if (g_rate > 60 && ok(timer)) wrf(timer + 0x18u, g_ticks); // the frame waited for one (short) vblank: dt = 60 / rate + if (l30_active() && ok(timer)) { // 30 Hz logic: the next frame is a gameplay frame (step 2) or a drawn-only one (step 0) + // The game's random sequences (the rand() LCG word 0x09AF84D0 and the effect LCG 0x09AFB89C) are shared by gameplay and effects; a + // drawn-only frame that drew from them would shift what gameplay draws next (HP damage, projectile spreads) away from 30 fps. + // They are put back after every drawn-only frame. PSPRECOMP_L30_RNG=0 leaves them alone (A/B). + static const bool rng_keep = [] { const char* e = std::getenv("PSPRECOMP_L30_RNG"); return !(e && e[0] == '0'); }(); + static uint32_t rng_saved[2] = {}; static bool rng_have = false; static uint64_t rng_moved = 0, rng_frames = 0; + constexpr uint32_t RNG_WORDS[2] = {0x09AF84D0u, 0x09AFB89Cu}; + if (rng_keep && rng_have && !g_l30_logic_now) { // the frame that just ended was drawn-only + rng_frames++; + for (int i = 0; i < 2; i++) if (r32(RNG_WORDS[i]) != rng_saved[i]) { rng_moved++; w32(RNG_WORDS[i], rng_saved[i]); } + if (rng_frames % 3600 == 0) std::fprintf(stderr, "[FPS] 30 Hz logic: random sequence drawn from in %llu of %llu drawn-only frames (put back)\n", + (unsigned long long)rng_moved, (unsigned long long)rng_frames); + } + static const bool timing = std::getenv("PSPRECOMP_L30_TIMING") != nullptr; // diagnostic: frame time by position in the 30 Hz frame + if (timing) { + using clk = std::chrono::steady_clock; + static clk::time_point last{}; static double sum[8] = {}, mx[8] = {}; static uint64_t cnt[8] = {}, late[8] = {}, frames = 0; + const clk::time_point t = clk::now(); + static int since = 0; since = g_l30_logic_now ? 0 : std::min(since + 1, 7); + const int ph = since; // 0 = the gameplay frame that just ended + if (last.time_since_epoch().count()) { + const double ms = std::chrono::duration(t - last).count(); + sum[ph] += ms; cnt[ph]++; mx[ph] = std::max(mx[ph], ms); if (ms > 1000.0 / g_rate * 1.5) late[ph]++; + if (++frames % 240 == 0) { + std::fprintf(stderr, "[L30T] %d fps, frame time by position (0 = gameplay):", g_rate); + for (int i = 0; i < std::min(8, g_rate / 30); i++) { + std::fprintf(stderr, " %d: avg %.2f max %.1f late %llu |", i, cnt[i] ? sum[i] / cnt[i] : 0.0, mx[i], (unsigned long long)late[i]); + sum[i] = mx[i] = 0; cnt[i] = late[i] = 0; + } + std::fprintf(stderr, " UI updates %llu / %llu, effect steps %llu / %llu (gameplay / drawn-only frames)\n", + (unsigned long long)g_l30_calls[0][0], (unsigned long long)g_l30_calls[0][1], (unsigned long long)g_l30_calls[1][0], (unsigned long long)g_l30_calls[1][1]); + std::memset(g_l30_calls, 0, sizeof g_l30_calls); + } + } + last = t; + } + const bool next_logic = l30_decide_next(); + const float next_phase = g_l30_next_phase; + g_l30_end_ms = l30_now_ms(); + g_l30_frame++; + g_l30_logic_now = next_logic; + wrf(timer + 0x18u, g_l30_logic_now ? 2.0f : 0.0f); // the coming frame's step (read directly by 85 battle sites); the global step follows via global_step_hook + // The renderer blends each frame's draws between the last two gameplay frames (psp_ge_draw.h ge_interp_set_frame). A frame's + // display list is built during the frame and submitted (sceGeListEnQueue, where the render queue captures the tag) right after + // this hook returns, i.e. after the frame's end: so the tag published here is the one computed at the previous frame end, the + // one describing the frame that just ran; the coming frame's tag waits until that frame has ended. Publishing the coming frame's + // tag at once tagged every list with the following frame's step: the gameplay frame's new matrices arrived in a list marked + // drawn-only (blended against a table one gameplay frame too old) and the list marked "record" re-emitted the old scene, so the + // camera and stage stepped 1.1 steps then -0.1 at 60 fps (frame dumps: neighbouring frame differences 1.0 / 6.6 / 1.3 / 6.9). + static uint32_t gen = 0; + if (g_l30_logic_now) gen++; + static uint32_t seq = 0; + GeInterpTag tag; tag.gen = gen; tag.seq = ++seq; tag.phase = next_phase; tag.record = g_l30_logic_now; + ge_interp_set_frame(g_l30_list_tag); + static const bool iptrace2 = [] { const char* e = std::getenv("PSPRECOMP_INTERP_TRACE"); return e && std::atoi(e) >= 2; }(); // where the frame end falls among the lists (psp_ge_draw.cpp [IPL]) + if (iptrace2 && g_battle_age > 600.f) std::fprintf(stderr, "[IPF] frame end tick %llu: list tag seq %u rec %d; next frame seq %u gen %u rec %d phase %.3f\n", (unsigned long long)g_tick, + g_l30_list_tag.seq, g_l30_list_tag.record ? 1 : 0, tag.seq, tag.gen, tag.record ? 1 : 0, tag.phase); + g_l30_list_tag = tag; + if (rng_keep && !g_l30_logic_now) { for (int i = 0; i < 2; i++) rng_saved[i] = r32(RNG_WORDS[i]); rng_have = true; } // the coming frame is drawn-only + } else if (g_rate > 60 && ok(timer)) wrf(timer + 0x18u, step_for(g_rate, ++g_step_idx)); // the frame waited for one (short) vblank: dt = 60 / rate (exact steps above) + if (!l30_active()) { ge_interp_set_frame(GeInterpTag{}); g_l30_list_tag = GeInterpTag{}; } + if (g_log && ok(timer)) { const int64_t t = now_ms(); if (!g_win_t0) g_win_t0 = t; @@ -1019,11 +1310,28 @@ void slowmo_frame_hook(uint8_t* rdram, recomp_context* ctx) { // the fixed step is scaled by ticks / 2 for the call and put back. The dt-driven ones already follow the frame time. // PSPRECOMP_UI_STEP=0 turns it off (A/B). bool g_ui_step = true; +// 30 Hz logic: the UI update runs on gameplay frames only, as on the PSP (once per 30 Hz frame, its own fixed steps). Run on every frame +// (even with its steps scaled) the HUD sequences that count updates or wait on gameplay ran 2-4x as often: the 'VS ' banner could +// stay on screen, the AP Chance prompt / HP bars misbehaved now and then. The HUD is still drawn every frame (by its draw tasks). +// PSPRECOMP_L30_UI=every runs it on every frame (with scaled steps) for A/B. +FuncPtr g_orig_ui_update = nullptr; +bool l30_ui_every() { static const bool e = [] { const char* v = std::getenv("PSPRECOMP_L30_UI"); return v && !std::strcmp(v, "every"); }(); return e; } +void ui_update_hook(uint8_t* rdram, recomp_context* ctx) { + if (l30_active() && !g_l30_logic_now && !l30_ui_every()) return; + g_orig_ui_update(rdram, ctx); +} void ui_param_hook(uint8_t* rdram, recomp_context* ctx) { + if (l30_active()) g_l30_calls[0][g_l30_logic_now ? 0 : 1]++; + // 30 Hz logic: the UI update still runs on every frame (counted: as often on drawn-only frames as on gameplay ones), so its fixed + // steps are scaled by 30 / rate as at a native high rate (EX gauge, EX core, lock-on reticle, banter ran 2-4x fast). + // (exact 1/256-tick steps, step_for: 30 / rate is not exact at 90, 150, 180, 210 fps, and a HUD animation that waits to land on its end + // value could then miss it) + static uint64_t ui_idx = 0; + const float k = l30_active() ? (l30_ui_every() ? step_for(g_rate, ui_idx++) * 0.5f : 1.0f) : g_ticks * 0.5f; const uint32_t mgr = r32(UI_MGR_PTR); - if (!g_ui_step || g_ticks == 2.0f || overlay_id() != 1 || !ok(mgr) || g_ram[(mgr + 33u) & 0x07FFFFFFu] != 0 || g_ram[(mgr + 34u) & 0x07FFFFFFu] != 0) { g_orig_ui_param(rdram, ctx); return; } + if (!g_ui_step || k == 1.0f || overlay_id() != 1 || !ok(mgr) || g_ram[(mgr + 33u) & 0x07FFFFFFu] != 0 || g_ram[(mgr + 34u) & 0x07FFFFFFu] != 0) { g_orig_ui_param(rdram, ctx); return; } const float was = rdf(UI_STEP); - wrf(UI_STEP, was * g_ticks * 0.5f); + wrf(UI_STEP, was * k); g_orig_ui_param(rdram, ctx); wrf(UI_STEP, was); } @@ -1350,9 +1658,127 @@ CoreSeen g_cores[8]; // kind-6 objects (EX cores) and the g_tick they last upd struct ActorSeen { uint32_t actor; float res; int own; }; ActorSeen g_actor_seen[24]; +// PSPRECOMP_MT_SNAP=1 (experiment): a move's motion time (actor +0x1A8) sums the step every update. At 90 / 150 / 180 / 210 fps the step +// (2/3, 0.4, 1/3, 2/7 ticks) is not exact in floating point and the sum lands just under a whole tick (69.99999 instead of 70), so an event +// keyed at tick 70 fires one update late (Exdeath's multi-hit 1960: a hit every 177.8 ms at 90 fps instead of 166.7; fps_parity.py). The +// step handed to the actor is trimmed so the new motion time lands on the step lattice (+1e-5, so a key on that tick fires this update). +// Result (15 Nov): about half of the late hits come back on time (90 fps: intervals 166.7 and 177.8 ms alternate), so another summed +// clock is involved too. The rehit timer (FUN_0880ACBC: record +68 <= 0) is not it: treating 0 < t < 0.001 as run out changed nothing. +void mt_snap(uint32_t actor, recomp_context* ctx) { + if (!g_mt_snap || g_ticks >= 2.0f || !ok(actor)) return; + const float mt = rdf(actor + 0x1A8u), dt = ctx->f[0]; + if (!(dt > 0.0f) || !(mt >= 0.0f) || mt > 100000.0f) return; + const double want = static_cast(mt) + dt, k = std::round(want / g_ticks), target = k * static_cast(g_ticks); + if (std::fabs(want - target) < 1e-3 && std::fabs(dt - g_ticks) < 1e-4) ctx->f[0] = static_cast(target + 1e-5 - mt); +} + +// PSPRECOMP_TASK_TRACE=1 (diagnostic): who calls the object update task and the draw task (return addresses), once each per caller. +FuncPtr g_orig_task_exec = nullptr, g_orig_task_draw = nullptr; +void task_note(const char* what, recomp_context* ctx) { + static uint32_t seen[32]; static int n = 0; + const uint32_t ra = static_cast(ctx->r[31]); + for (int i = 0; i < n; i++) if (seen[i] == ra) return; + if (n < 32) seen[n++] = ra; + std::fprintf(stderr, "[TASKTRACE] %s called from ra=%08X a0=%08X tick=%llu bt:", what, ra, static_cast(ctx->r[4]), (unsigned long long)g_tick); +#ifdef PSPRECOMP_HAVE_BACKTRACE + void* bt[24]; const int k = backtrace(bt, 24); + Dl_info di{}; dladdr(reinterpret_cast(&task_note), &di); + for (int i = 0; i < k; i++) std::fprintf(stderr, " %lx", static_cast(reinterpret_cast(bt[i]) - reinterpret_cast(di.dli_fbase))); +#else + std::fprintf(stderr, " (not available on this platform)"); +#endif + std::fprintf(stderr, "\n"); +} +// PSPRECOMP_TASK_DUMP= (diagnostic): once the battle has run that long, list the root task list (sys = *(0x089D69A8): list +// *(sys+8192) -> head +4 first task, filter mask sys+8196 / value sys+8200; task: +0 function, +40 flags, +44 priority, +52 next). +void task_dump_frame() { + static const char* e = std::getenv("PSPRECOMP_TASK_DUMP"); + static int dumps = 0; + if (!e || dumps >= 8 || overlay_id() != 1 || g_tick % static_cast(std::max(1, std::atoi(e))) != 0) return; // every frames in the battle overlay + dumps++; + const uint32_t sys = r32(0x089D0000u + 27048u); + if (!ok(sys)) return; + const uint32_t list = r32(sys + 8192u), mask = r32(sys + 8196u), value = r32(sys + 8200u); + std::fprintf(stderr, "[TASKDUMP] battle age %.0f sys=%08X list=%08X mask=%08X value=%08X\n", g_battle_age, sys, list, mask, value); + uint32_t t = ok(list) ? r32(list + 4u) : 0u; + for (int i = 0; i < 200 && ok(t); i++, t = r32(t + 52u)) { + std::fprintf(stderr, "[TASKDUMP] task %08X fn=%08X flags=%08X prio=%d runs=%d words:", t, r32(t), r32(t + 40u), (int)r32(t + 44u), ((r32(t + 40u) & mask) == value) ? 1 : 0); + for (uint32_t o = 4; o < 40; o += 4) std::fprintf(stderr, " %08X", r32(t + o)); + std::fprintf(stderr, "\n"); + } +} + +// PSPRECOMP_TASK_RUNLOG=,,... (diagnostic): during those frames (g_tick), every task-runner call (FUN_0888245C(list, mask, value)) +// with the tasks of the list: function, flags, and whether this filter runs it. +FuncPtr g_orig_task_run = nullptr; +std::vector g_runlog_ticks; +bool battle_scene_list(uint32_t list) { // the battle scene's task list: the one holding the object manager task (FUN_08841798) + static uint32_t yes = 0, no = 0; + if (list == yes) return true; + if (list == no || !ok(list)) return false; + uint32_t t = r32(list + 4u); + for (int i = 0; i < 100 && ok(t); i++, t = r32(t + 52u)) if (r32(t) == 0x08841798u) { yes = list; return true; } + no = list; + return false; +} +void task_run_hook(uint8_t* rdram, recomp_context* ctx) { + if (l30_active() && !g_l30_logic_now && ctx->r[5] == 1 && ctx->r[6] == 1 && battle_scene_list(static_cast(ctx->r[4]))) { + ctx->r[5] = 4; ctx->r[6] = 4; // a drawn-only frame: the battle runs as if paused + static const bool none = std::getenv("PSPRECOMP_L30_NONE") != nullptr; // experiment: no battle task at all on drawn-only frames + if (none) return; + // PSPRECOMP_L30_KEEP=,... (experiment): on drawn-only frames ONLY these tasks run (every other one's filter flags cleared for this pass) + static std::vector keep = [] { std::vector v; if (const char* e = std::getenv("PSPRECOMP_L30_KEEP")) + for (const char* q = e; *q;) { v.push_back(static_cast(std::strtoul(q, nullptr, 16))); const char* c = std::strchr(q, ','); if (!c) break; q = c + 1; } return v; }(); + if (!keep.empty()) { + std::vector> saved; + uint32_t t = r32(static_cast(ctx->r[4]) + 4u); + for (int i = 0; i < 200 && ok(t); i++, t = r32(t + 52u)) { + bool k = false; + for (uint32_t f : keep) k |= r32(t) == f; + if (!k) { saved.push_back({t, r32(t + 40u)}); w32(t + 40u, 0u); } + } + g_orig_task_run(rdram, ctx); + for (auto& sv : saved) w32(sv.first + 40u, sv.second); + return; + } + // PSPRECOMP_L30_SKIP=,... (experiment): tasks also left out of drawn-only frames (their filter flags cleared for this pass) + // By default the tasks below are left out: they run while paused but change gameplay (a binary search with the parity tests found that + // with them a few hits / reactions differed from 30 fps) and none of them draws the battle scene (the scene is still drawn whole on every + // frame without them: 3,635 prims as on a gameplay frame). Most are flagged 0x27 / 0x2F (UI). PSPRECOMP_L30_SKIP replaces the list. + static std::vector skip = [] { std::vector v{0x088892C8u, 0x088CD704u, 0x088CCB18u, 0x088DE5ACu, 0x088D7028u, 0x088CE5F4u, + 0x08ADFA74u, 0x08A229C8u, 0x08A22A18u, 0x08B01BC4u, 0x08B1DAB0u}; + if (const char* e = std::getenv("PSPRECOMP_L30_SKIP")) { v.clear(); + for (const char* q = e; *q;) { v.push_back(static_cast(std::strtoul(q, nullptr, 16))); const char* c = std::strchr(q, ','); if (!c) break; q = c + 1; } } return v; }(); + if (!skip.empty()) { + std::vector> saved; + uint32_t t = r32(static_cast(ctx->r[4]) + 4u); + for (int i = 0; i < 200 && ok(t); i++, t = r32(t + 52u)) + for (uint32_t f : skip) if (r32(t) == f) { saved.push_back({t, r32(t + 40u)}); w32(t + 40u, 0u); } + g_orig_task_run(rdram, ctx); + for (auto& sv : saved) w32(sv.first + 40u, sv.second); + return; + } + } + bool on = false; + for (uint64_t t : g_runlog_ticks) on |= g_tick == t; + if (on) { + const uint32_t list = static_cast(ctx->r[4]), mask = static_cast(ctx->r[5]), value = static_cast(ctx->r[6]); + std::fprintf(stderr, "[TASKRUN] tick %llu list %08X mask %08X value %08X:", (unsigned long long)g_tick, list, mask, value); + uint32_t t = ok(list) ? r32(list + 4u) : 0u; + for (int i = 0; i < 100 && ok(t); i++, t = r32(t + 52u)) + std::fprintf(stderr, " %s%08X/%X", ((r32(t + 40u) & mask) == value) ? "+" : "-", r32(t), r32(t + 40u)); + std::fprintf(stderr, "\n"); + } + g_orig_task_run(rdram, ctx); +} + +void task_exec_hook(uint8_t* rdram, recomp_context* ctx) { task_note("OBJ_MANAGER::ExecTask 088413D0", ctx); g_orig_task_exec(rdram, ctx); } +void task_draw_hook(uint8_t* rdram, recomp_context* ctx) { task_note("draw task 088881BC", ctx); g_orig_task_draw(rdram, ctx); } + void actor_dt_hook(uint8_t* rdram, recomp_context* ctx) { const uint32_t actor = static_cast(ctx->r[4]); g_orig_actor_dt(rdram, ctx); + mt_snap(actor, ctx); bool own = false; if (ok(actor)) own = ((r32(actor + 0x388u) & r32(OWN_MASK0)) | (r32(actor + 0x38Cu) & r32(OWN_MASK1))) != 0; if (ok(actor)) { // remember the fighters (kind 0: camera.cpp pivots on one) and, for the log, the other actors @@ -1878,10 +2304,11 @@ struct DtScale { } ~DtScale() { for (int i = n - 1; i >= 0; i--) wrf(addr[i], was[i]); } }; -bool fx_fix_on() { return g_rate > 30 && g_fx60; } +bool fx_fix_on() { return g_rate > 30 && g_fx60 && !l30_active(); } // 30 Hz logic: the components already step once per 30 Hz frame, with step 2 template void fx_every_other_hook(uint8_t* rdram, recomp_context* ctx) { + if (l30_active()) g_l30_calls[1][g_l30_logic_now ? 0 : 1]++; if (!fx_fix_on()) { g_fx_orig[I](rdram, ctx); return; } if (!fx_step_due(fx_step_of(static_cast(ctx->r[7])))) { g_fx_holds++; return; } g_fx_runs++; @@ -2034,6 +2461,17 @@ bool dissidia_fighter_human(uint32_t a) { // Ticks (1/60 s) one game frame advances: 2 at 30 Hz, 1 at 60 Hz, 2/3 at 90 Hz, 0.5 at 120 Hz. float dissidia_frame_ticks() { return g_ticks; } +// 30 Hz logic applies once the fight is past its intro (7 s of battle: the intro sequence did not finish with drawn-only frames in it) +namespace { +bool l30_active() { + static bool on = false; + static const bool from_clock = std::getenv("PSPRECOMP_L30_FROM_CLOCK") != nullptr; // experiment: only from the test clock on + const bool want = g_logic30 && g_rate > 30 && overlay_id() == 1 && (from_clock ? g_gt_started : battle_running()); + if (want && !on) { g_l30_frame = 0; g_l30_logic_now = true; g_l30_dec_frame = ~0ull; g_l30_due_ms = 0; g_l30_end_ms = 0; } + on = want || (on && g_logic30 && g_rate > 30 && overlay_id() == 1 && battle_running()); + return on; +} +} // namespace // Factor for a value the game keeps as "ticks per 30 Hz frame" (the actor own clock): 1 at 30 Hz, 0.5 at 60 Hz. float dissidia_own_clock_scale() { return g_ticks * 0.5f; } @@ -2124,7 +2562,16 @@ void dissidia_framerate_install(uint8_t* rdram) { if (const char* d = std::getenv("PSPRECOMP_DBG_COMBO"); d && d[0] && d[0] != '0') g_dbg_combo = true; // start with the debug options on (tests) if (const char* d = std::getenv("PSPRECOMP_DBG_NOAI"); d && d[0] && d[0] != '0') g_dbg_no_ai = true; if (const char* na = std::getenv("PSPRECOMP_TEST_NO_AI"); na && na[0] && na[0] != '0') g_env_no_ai = true; - if ((g_orig_cpu_tick = ovl_battle_lookup(CPU_TICK))) psp_dispatch_register(CPU_TICK, cpu_tick_noai_hook); // always: the debug menu switches it live + if ((g_orig_cpu_tick = ovl_battle_lookup(CPU_TICK))) psp_dispatch_register(CPU_TICK, cpu_tick_noai_hook); + if (const char* l3 = std::getenv("PSPRECOMP_LOGIC30"); l3 && l3[0] == '0') g_logic30 = false; // default on (16 Nov: checked on the RP6 and the desktop) + if (const char* rl = std::getenv("PSPRECOMP_TASK_RUNLOG")) + for (const char* q = rl; *q;) { g_runlog_ticks.push_back(std::strtoull(q, nullptr, 10)); const char* c = std::strchr(q, ','); if (!c) break; q = c + 1; } + if ((g_logic30 || !g_runlog_ticks.empty()) && (g_orig_task_run = psp_dispatch_probe_lookup(0x0888245Cu))) psp_dispatch_register(0x0888245Cu, task_run_hook); + if (std::getenv("PSPRECOMP_TASK_TRACE")) { + if ((g_orig_task_exec = psp_dispatch_probe_lookup(0x088413D0u))) psp_dispatch_register(0x088413D0u, task_exec_hook); + if ((g_orig_task_draw = psp_dispatch_probe_lookup(0x088881BCu))) psp_dispatch_register(0x088881BCu, task_draw_hook); + } + if (std::getenv("PSPRECOMP_TEST_PARITY") && (g_orig_take_hit = psp_dispatch_probe_lookup(0x0883A150u))) psp_dispatch_register(0x0883A150u, take_hit_hook); // test only // always: the debug menu switches it live else std::fprintf(stderr, "[FPS] CPU brain tick not found: no Enemy AI option\n"); if (g_orig_cpu_tick) { psp_settings_add_debug_toggle({"Enemy AI", "Enemy AI (debug)\nOff: the CPU fighter never chooses an action; it stands and takes hits. Starts to apply about 7 s into a fight. This session only.", @@ -2142,7 +2589,12 @@ void dissidia_framerate_install(uint8_t* rdram) { if (const char* us = std::getenv("PSPRECOMP_UI_STEP"); us && us[0] == '0') g_ui_step = false; if ((g_orig_ui_param = psp_dispatch_probe_lookup(UI_PARAM_STEP))) psp_dispatch_register(UI_PARAM_STEP, ui_param_hook); else std::fprintf(stderr, "[FPS] UI step hook NOT installed (no function at %08X)\n", UI_PARAM_STEP); - if ((g_orig_slowmo = psp_dispatch_probe_lookup(SLOWMO_FRAME))) psp_dispatch_register(SLOWMO_FRAME, slowmo_frame_hook); + if ((g_orig_ui_update = psp_dispatch_probe_lookup(UI_UPDATE))) psp_dispatch_register(UI_UPDATE, ui_update_hook); + else std::fprintf(stderr, "[FPS] UI update hook NOT installed (no function at %08X)\n", UI_UPDATE); + if ((g_orig_slowmo = psp_dispatch_probe_lookup(SLOWMO_FRAME))) { + psp_dispatch_register(SLOWMO_FRAME, global_step_hook); // global_step_hook -> slowmo_frame_hook -> the game's FUN_08864DD8 + g_orig_global_step = slowmo_frame_hook; + } else std::fprintf(stderr, "[FPS] slow-motion hook NOT installed (no function at %08X)\n", SLOWMO_FRAME); if (const char* sm = std::getenv("PSPRECOMP_STEER_MOVE"); !(sm && sm[0] == '0') && (g_orig_steer_move = ovl_battle_lookup(STEER_MOVE))) { psp_dispatch_register(STEER_MOVE, steer_move_hook); std::fprintf(stderr, "[FPS] steered-move hook installed\n"); } if (g_log && (g_orig_skel = psp_dispatch_probe_lookup(SKEL_BUILD))) psp_dispatch_register(SKEL_BUILD, skel_build_hook); diff --git a/psprecomp/games/dissidia/runtime/roster.cpp b/psprecomp/games/dissidia/runtime/roster.cpp index 01e640b..71fd990 100644 --- a/psprecomp/games/dissidia/runtime/roster.cpp +++ b/psprecomp/games/dissidia/runtime/roster.cpp @@ -5818,7 +5818,8 @@ int dissidia_test_action(uint8_t* rdram, recomp_context* ctx, uint32_t actor, in std::fprintf(stderr, "[TEST] plya actor %08X: %u records\n", actor, n); for (uint32_t i = 0; i < n && i < 4096u && guest_ok(t, 8u + (i + 1) * PLYA_STRIDE); i++) { const uint32_t r = t + 8u + i * PLYA_STRIDE; - std::fprintf(stderr, "[TEST] plya action %u script %d\n", guest_rd(rdram, r), (int)guest_rd(rdram, r + 0x1C)); + std::fprintf(stderr, "[TEST] plya action %u script %d later %u side %u button %u\n", guest_rd(rdram, r), (int)guest_rd(rdram, r + 0x1C), + guest_rd(rdram, r + 3), guest_rd(rdram, r + 5), guest_rd(rdram, r + 0x0F)); // +3 1 = a later press, +5 2 ground / 1 air, +0x0F 1 brave / 2 HP } return 1; } diff --git a/psprecomp/games/dissidia/tests/run_fps_parity_test.sh b/psprecomp/games/dissidia/tests/run_fps_parity_test.sh new file mode 100755 index 0000000..1a34e39 --- /dev/null +++ b/psprecomp/games/dissidia/tests/run_fps_parity_test.sh @@ -0,0 +1,18 @@ +#!/bin/sh +# Frame-rate parity test: every move of every playable character must play at every frame rate the game offers as it does at +# 30 fps (the PSP's rate). Boots the game headless once per character and rate (tools/fps_parity.py, docs/testing.md +# "Frame-rate parity tests"); exit code 1 when any trial differs. Long: hours for the whole roster at all 8 rates. +# +# run_fps_parity_test.sh [out dir] [fps_parity.py run options...] +# e.g. run_fps_parity_test.sh ~/dissidia/work /tmp/parity --chars squall,exdeath --rates 30,60,120 +# run_fps_parity_test.sh ~/dissidia/work /tmp/parity --jecht-combos # + Jecht's combo windows (optional, every rate) +# run_fps_parity_test.sh ~/dissidia/work /tmp/combos --chars none --jecht-combos # only the Jecht combo test (30..240) +# +# Needs a runtime built from this tree in /build, Xvfb, python3 + Pillow and a save with Quick Battle unlocked. +set -e +here=$(cd "$(dirname "$0")" && pwd) +work=${1:?usage: run_fps_parity_test.sh [out dir] [options]} +shift +out=${1:-${TMPDIR:-/tmp}/fps_parity} +[ $# -gt 0 ] && shift +exec python3 "$here/../tools/fps_parity.py" run --work "$work" --out "$out" "$@" diff --git a/psprecomp/games/dissidia/tools/battle_test.py b/psprecomp/games/dissidia/tools/battle_test.py index cc93a17..0648f80 100755 --- a/psprecomp/games/dissidia/tools/battle_test.py +++ b/psprecomp/games/dissidia/tools/battle_test.py @@ -78,6 +78,8 @@ def start(self): env = dict(os.environ, DISPLAY=self.display, PSPRECOMP_SETTINGS=ini, PSPRECOMP_MS0=ms0, PSPRECOMP_FIGHTER_LOG='1', PSPRECOMP_DEBUG_PORT=str(self.port), PSPRECOMP_SAVEDATA_AUTO='0', PSPRECOMP_TEST_FREEZE_CPU='1') env.pop('PSPRECOMP_FPS', None) + if gpu_headless(): + env.update(gpu_env()) env.setdefault('PSPRECOMP_TEST_NO_AI', '1') # the CPU brain never ticks: the dummy does not attack (BT_NO_FREEZE=1 lets it act) if os.environ.get('BT_NO_FREEZE'): env['PSPRECOMP_TEST_NO_AI'] = '0' # let the CPU fighter act (the dummy is the default) @@ -181,7 +183,23 @@ def terminate(self): pass +NVIDIA_EGL = '/usr/share/glvnd/egl_vendor.d/10_nvidia.json' + + +def gpu_headless(): + """BT_GPU=1: render on the NVIDIA GPU with no X server (SDL's offscreen video driver makes an EGL context; Xvfb only has + llvmpipe). BT_GPU=auto: when the NVIDIA EGL vendor file is there.""" + v = os.environ.get('BT_GPU', '0') + return v == '1' or (v == 'auto' and os.path.exists(NVIDIA_EGL)) + + +def gpu_env(): + return {'SDL_VIDEODRIVER': 'offscreen', '__EGL_VENDOR_LIBRARY_FILENAMES': os.environ.get('BT_EGL_VENDOR', NVIDIA_EGL)} + + def start_xvfb(): + if gpu_headless(): # no display at all: the game renders offscreen on the GPU + return _NoProc(), ':gpu-offscreen' if os.environ.get('BT_DISPLAY'): # no Xvfb on this machine: use an existing X display (the 480x272 test windows show on it) return _NoProc(), os.environ['BT_DISPLAY'] n = free_display() diff --git a/psprecomp/games/dissidia/tools/combo_timing.py b/psprecomp/games/dissidia/tools/combo_timing.py index 4129848..d0af455 100644 --- a/psprecomp/games/dissidia/tools/combo_timing.py +++ b/psprecomp/games/dissidia/tools/combo_timing.py @@ -14,7 +14,17 @@ at 30, 60 and 120 fps (at 30 fps the edges can only fall on update boundaries, 33.3 ms apart). combo_timing.py sweep --work W --rates 30,120 --prefix 'CIRCLE@800=10001' --probe CIRCLE --from 150 --to 700 # one transition - combo_timing.py combos --work W --rates 30,60,120 # every wiki combo + combo_timing.py combos --work W [--rates 30,60,...,240] [--jobs 4] # every wiki combo, every rate + # (exit 1 when a window differs) +Boot: by default the recorded Quick Battle walk is replayed inside the game (`--direct`, tests/replays/quick_battle_walk.txt) with the +fighter swapped in (PSPRECOMP_TEST_CHAR_SWAP, runtime/direct_battle.cpp): ~35 s to the fight instead of the ~3 min picture walk +(`--menus` for the old walk). On the GPU when there is one (battle_test BT_GPU, default auto here). The analogue camera is off +(PSPRECOMP_CAM_FREE=0: it runs on the wall clock and turns the stick, and Up 2 is a stick input). Also run by +`fps_parity.py run --jecht-combos` (docs/testing.md "Frame-rate parity tests"). + +Verdict (`combos`): for every transition, every script the probe starts at 30 fps (the chain move, the too-early / too-late repeat, +the HP finisher) must be started at every rate, with its first and last probe delay within --tol ms of 30 fps (default 34: at 30 fps an +edge can only fall on an update, 33.3 ms apart), and no rate may start a script 30 fps never does. Step syntax: BUTTON[:STICK][@delay][=script] STICK n (neutral) | u (up) | d (down) | l | r delay in ms after the previous move started (default 400); `=script` is the script id the press must start (the next step is timed from that start). @@ -33,6 +43,7 @@ import battle_test as bt # noqa: E402 STICK = {'n': (128, 128), 'u': (128, 0), 'd': (128, 255), 'l': (0, 128), 'r': (255, 128)} +WALK = os.path.join(HERE, '..', 'tests', 'replays', 'quick_battle_walk.txt') TAP_MS = 67 # a press: two updates at 30 fps (a 1-update tap is sometimes missed by the game) GRID = 1000.0 / 120.0 # default probe grid: one update at 120 fps @@ -51,7 +62,7 @@ def press_lines(delay, btn, stick, hold=TAP_MS): def trial_script(i, prefix, probe, d, dist=3.0, tail=900): """script lines of trial i. prefix: steps (parse_step) pressed in order; probe: a step whose delay is replaced by d""" - L = ['+0 waitidle 8000', '+400 place %g' % dist, '+700 mark trial %d' % i] + L = ['+0 waitidle 8000', '+400 home %g' % dist, '+700 mark trial %d' % i] # home: place, face each other, status blocks (bravery, HP damage) as at the first trial first = True for st in prefix: L += press_lines(st['delay'] if not first else max(st['delay'], 800), st['btn'], st['stick']) @@ -68,9 +79,9 @@ def trial_script(i, prefix, probe, d, dist=3.0, tail=900): class Session(threading.Thread): """a running battle at one frame rate""" - def __init__(self, work, fps, out, char='jecht', ff_menus=0): + def __init__(self, work, fps, out, char='jecht', ff_menus=0, direct=True): super().__init__(daemon=True) - self.work, self.fps, self.out, self.char, self.ff_menus = work, fps, out, char, ff_menus + self.work, self.fps, self.out, self.char, self.ff_menus, self.direct = work, fps, out, char, ff_menus, direct self.ready = threading.Event() self.error = None self.n = 0 @@ -86,13 +97,22 @@ def run(self): xp, disp = bt.start_xvfb() try: init = os.path.join(self.out, 'init_%d.txt' % self.fps) - with open(init, 'w') as f: - f.write('5000 waitidle 8000\n+0 batch %s\n' % self.batch_path(0)) - self.g = bt.Game(self.work, self.fps, self.out, disp, bt.free_port(), script=init, - extra_env={'PSPRECOMP_TEST_PLAYER_CHAR': str(bt.CHAR_ID[self.char]), 'PSPRECOMP_TEST_FREEZE_CPU': '0', 'PSPRECOMP_TEST_AI_OFF': '1', - 'PSPRECOMP_FIGHTER_LOG': '0'}) # the fighter log is a line per fighter per update: tens of GB at 120 fps + with open(init, 'w') as f: # (3 s of settling: the first seconds after the intro are not repeatable) + f.write('5000 waitidle 8000\n+3000 mark settled\n+0 batch %s\n' % self.batch_path(0)) + cid = bt.CHAR_ID[self.char] + env = {'PSPRECOMP_TEST_PLAYER_CHAR': str(cid), 'PSPRECOMP_TEST_FREEZE_CPU': '0', 'PSPRECOMP_TEST_AI_OFF': '1', + 'PSPRECOMP_FIGHTER_LOG': '0', # the fighter log is a line per fighter per update: tens of GB at 120 fps + 'PSPRECOMP_CAM_FREE': '0'} # the analogue camera runs on the wall clock and turns the stick + if self.direct: + env['PSPRECOMP_TEST_DIRECT_BATTLE'] = os.path.abspath(WALK) + if cid != 15: + env['PSPRECOMP_TEST_CHAR_SWAP'] = '15=%d' % cid if cid != 31 else '15=31,31=15' + self.g = bt.Game(self.work, self.fps, self.out, disp, bt.free_port(), script=init, extra_env=env) self.g.start() - bt.walk_menus(self.g, self.char, ff_menus=self.ff_menus) + if self.direct: + bt.walk_direct(self.g, ff_menus=4) + else: + bt.walk_menus(self.g, self.char, ff_menus=self.ff_menus) self.ready.set() last = 0 while not self.stop_flag and self.g.alive(): @@ -203,6 +223,23 @@ def sweep(sess, prefix, probe, ds, dist=3.0, chunk=24): return out +def sweep_adaptive(sess, prefix, probe, lo, hi, coarse=1000.0 / 30.0, fine=GRID, dist=3.0): + """the same rows as sweep() over a fine grid, with fewer trials: a coarse grid first (one 30 Hz frame), then the fine grid only + between neighbouring coarse probes whose outcome differs (the window edges keep the fine resolution).""" + ds = grid(lo, hi, coarse) + if ds[-1] < hi - 0.01: + ds.append(float(hi)) # the end of the range too (the outer edge of a window that runs to it) + rows = sweep(sess, prefix, probe, ds, dist) + key = lambda r: (r[1][0] if r[1] else None) if r[2] and r[1] is not None else 'bad' + extra = [] + for a, b in zip(rows, rows[1:]): + if key(a) != key(b): + extra += [d for d in grid(a[0], b[0], fine)[1:] if d < b[0] - 0.01] + if extra: + rows += sweep(sess, prefix, probe, sorted(set(extra)), dist) + return sorted(rows, key=lambda r: r[0]) + + def windows(rows): """{first outcome script id: (min D, max D, count, contiguous)} from sweep rows""" by = {} @@ -220,14 +257,24 @@ def windows(rows): return res -def run_rates(work, rates, out, fn, char='jecht', ff_menus=0, attempts=3): - """boot one session per rate (in parallel), run fn(session) in a thread each, return {rate: result}. A session whose display was killed - (this host: low memory, other sessions' cleanups) is started again from the menus, up to `attempts` times.""" +RATES = [30, 60, 90, 120, 150, 180, 210, 240] # every rate the settings offer (framerate.cpp k_rates) + + +def run_rates(work, rates, out, fn, char='jecht', ff_menus=0, attempts=3, direct=True, jobs=0): + """boot one session per rate (at most `jobs` at once, 0 = all), run fn(session) in a thread each, return {rate: result}. A session + whose boot failed (display killed, the battle never started) is started again, up to `attempts` times.""" results, errs = {}, {} + gate = threading.Semaphore(jobs if jobs > 0 else len(rates)) def go(r): + with gate: + go1(r) + + def go1(r): for attempt in range(attempts): - s = Session(work, r, os.path.join(out, 'r%d' % r if attempt == 0 else 'r%d_try%d' % (r, attempt + 1)), char, ff_menus) + s = Session(work, r, os.path.join(out, 'r%d' % r if attempt == 0 else 'r%d_try%d' % (r, attempt + 1)), char, ff_menus, direct) + if attempt == 0 and r != rates[0]: + time.sleep(3 * (rates.index(r) % max(1, jobs or len(rates)))) # two games started in the same second can pick the same X display s.start() s.ready.wait() try: @@ -239,7 +286,7 @@ def go(r): except Exception as e: # noqa: BLE001 errs[r] = e print('rate %d: attempt %d failed: %r' % (r, attempt + 1, e), file=sys.stderr) - if 'display died' not in str(e): + if 'display died' not in str(e) and 'never started' not in str(e) and 'Connection refused' not in str(e): return finally: s.finish() @@ -279,18 +326,18 @@ def combos_run(sess): out = {} rush = parse_step('CIRCLE@800=10001') P = parse_step - out['Rush > Neutral 2 (Circle)'] = sweep(sess, [rush], P('CIRCLE:n'), grid(150, 500)) - out['Rush > Up 2 (Circle+Up)'] = sweep(sess, [rush], P('CIRCLE:u'), grid(150, 500)) + out['Rush > Neutral 2 (Circle)'] = sweep_adaptive(sess, [rush], P('CIRCLE:n'), 150, 500) + out['Rush > Up 2 (Circle+Up)'] = sweep_adaptive(sess, [rush], P('CIRCLE:u'), 150, 500) pk = pick_success(out['Rush > Neutral 2 (Circle)']) if not pk: return out n2 = P('CIRCLE:n@%.1f=%d' % (pk[2], pk[0])) - out['Neutral 2 > Neutral 3 (Circle)'] = sweep(sess, [rush, n2], P('CIRCLE:n'), grid(300, 900)) - out['Neutral 2 > Up 3 (Circle+Up)'] = sweep(sess, [rush, n2], P('CIRCLE:u'), grid(300, 900)) + out['Neutral 2 > Neutral 3 (Circle)'] = sweep_adaptive(sess, [rush, n2], P('CIRCLE:n'), 300, 900) + out['Neutral 2 > Up 3 (Circle+Up)'] = sweep_adaptive(sess, [rush, n2], P('CIRCLE:u'), 300, 900) pk3 = pick_success(out['Neutral 2 > Neutral 3 (Circle)'], exclude=(pk[0],)) if pk3: n3 = P('CIRCLE:n@%.1f=%d' % (pk3[2], pk3[0])) - out['Neutral 3 > HP (Square)'] = sweep(sess, [rush, n2, n3], P('SQUARE'), grid(300, 900)) + out['Neutral 3 > HP (Square)'] = sweep_adaptive(sess, [rush, n2, n3], P('SQUARE'), 300, 900) return out @@ -310,6 +357,56 @@ def report(res, rates): return '\n'.join(lines) +def verdict(res, rates, tol=34.0): + """[(transition, rate, problem)] where a rate differs from the reference (rates[0], 30 fps) ([] = all the same): every script the probe + starts at the reference (the chain move, the too-early / too-late repeats, the HP finisher; not "nothing started") must be started at + the other rate too, with its first and last probe delay within `tol` ms. A rate whose session failed counts as a difference.""" + bad = [] + base_rate = rates[0] + if base_rate not in res: + return [('*', base_rate, 'no result at the reference rate')] + for r in rates: # a sweep whose combo steps often did not start says nothing: the harness failed there + for name, rows in res.get(r, {}).items(): + lost = sum(1 for x in rows if not x[2] or x[1] is None) + if rows and lost > 0.25 * len(rows): + bad.append((name, r, '%d of %d trials could not be played (a combo step before the probe did not start): harness problem' % (lost, len(rows)))) + for name, rows in res[base_rate].items(): + w0 = {k: v for k, v in windows(rows).items() if k is not None} + if not w0: + bad.append((name, base_rate, 'the probe never started a move at the reference rate')) + continue + for r in rates[1:]: + if r not in res or name not in res[r]: + bad.append((name, r, 'not measured')) + continue + w = windows(res[r][name]) + for k, v0 in sorted(w0.items()): + v = w.get(k) + if not v: + bad.append((name, r, 'script %d never starts (%d fps: %.0f..%.0f ms)' % (k, base_rate, v0[0], v0[1]))) + elif abs(v[0] - v0[0]) > tol or abs(v[1] - v0[1]) > tol: + bad.append((name, r, 'script %d window %.0f..%.0f ms vs %.0f..%.0f ms at %d fps' % (k, v[0], v[1], v0[0], v0[1], base_rate))) + for k in sorted(set(x for x in w if x is not None) - set(w0)): + bad.append((name, r, 'script %d starts (%.0f..%.0f ms), never at %d fps' % (k, w[k][0], w[k][1], base_rate))) + return bad + + +def run_combos(work, rates, out, char='jecht', direct=True, ff_menus=0, tol=34.0, jobs=0): + """the `combos` test: report + verdict files in out, returns 0 (all windows the same as at 30 fps) or 1""" + os.makedirs(out, exist_ok=True) + t0 = time.time() + res = run_rates(work, rates, out, combos_run, char, ff_menus, direct=direct, jobs=jobs) + bad = verdict(res, rates, tol) + text = report(res, rates) + '\n\n' + ('PASS: every chain window the same as at %d fps (within %.0f ms)' % (rates[0], tol) if not bad else + 'FAIL:\n' + '\n'.join(' %s, %d fps: %s' % b for b in bad)) + '\ntook %.0f s\n' % (time.time() - t0) + with open(os.path.join(out, 'combos.txt'), 'w') as f: + f.write(text) + with open(os.path.join(out, 'combos.json'), 'w') as f: + json.dump({str(r): res[r] for r in res}, f) + print(text) + return 1 if bad else 0 + + def main(argv): ap = argparse.ArgumentParser(description=__doc__.split('\n')[0]) sub = ap.add_subparsers(dest='what', required=True) @@ -328,27 +425,29 @@ def main(argv): p = sub.add_parser('combos') p.add_argument('--work', required=True) p.add_argument('--out', default='/tmp/combo_timing') - p.add_argument('--rates', default='30,120') + p.add_argument('--rates', default=','.join(map(str, RATES)), help='default: every rate the settings offer, 30..240') + p.add_argument('--jobs', type=int, default=4, help='games at once (default 4)') p.add_argument('--char', default='jecht') p.add_argument('--ff-menus', type=int, default=0) + p.add_argument('--tol', type=float, default=34.0, help='ms a window edge may differ from 30 fps (default 34: one 30 fps update)') + for x in sub.choices.values(): + x.add_argument('--menus', action='store_true', help='walk the menus by pictures (slow) instead of the recorded walk') + x.add_argument('--gpu', default='auto', choices=['auto', 'on', 'off'], help='render on the NVIDIA GPU with no X server (default auto)') a = ap.parse_args(argv) + os.environ['BT_GPU'] = {'auto': 'auto', 'on': '1', 'off': '0'}[a.gpu] if a.what == 'combos': work, out = os.path.abspath(os.path.expanduser(a.work)), os.path.abspath(a.out) rates = [int(x) for x in a.rates.split(',')] - t0 = time.time() - res = run_rates(work, rates, out, combos_run, a.char, a.ff_menus) - print(report(res, rates)) - with open(os.path.join(out, 'combos.json'), 'w') as f: - json.dump({str(r): res[r] for r in res}, f) - print('took %.0f s' % (time.time() - t0)) - return 0 + if 30 in rates: + rates = [30] + [r for r in rates if r != 30] + return run_combos(work, rates, out, a.char, not a.menus, a.ff_menus, a.tol, a.jobs) work, out = os.path.abspath(os.path.expanduser(a.work)), os.path.abspath(a.out) rates = [int(x) for x in a.rates.split(',')] prefix = [parse_step(x) for x in a.prefix.split(',')] if a.prefix else [] probe = parse_step(a.probe) ds = [round(a.lo + k * a.step, 2) for k in range(int((a.hi - a.lo) / a.step) + 1)] t0 = time.time() - res = run_rates(work, rates, out, lambda s: sweep(s, prefix, probe, ds, a.dist), a.char, a.ff_menus) + res = run_rates(work, rates, out, lambda s: sweep(s, prefix, probe, ds, a.dist), a.char, a.ff_menus, direct=not a.menus) for r in rates: if r in res: print('%3d fps %s' % (r, fmt_windows(windows(res[r])))) diff --git a/psprecomp/games/dissidia/tools/fps_parity.py b/psprecomp/games/dissidia/tools/fps_parity.py new file mode 100644 index 0000000..a1b6d1d --- /dev/null +++ b/psprecomp/games/dissidia/tools/fps_parity.py @@ -0,0 +1,686 @@ +#!/usr/bin/env python3 +"""Frame-rate parity tests: every move of every playable character must play the same at every frame rate the game +offers as at 30 fps (the PSP's rate, the reference). See docs/testing.md "Frame-rate parity tests". + + fps_parity.py run --work W [--chars all | squall,exdeath,10] [--rates 30,60,90,120,150,180,210,240] [--out DIR] [--jobs 4] + [--actions 1234,1235] [--variants plain,mash] [--repeat-base] + fps_parity.py compare DIR [--chars ...] (compare finished runs again, e.g. with other tolerances) + fps_parity.py chars (the playable characters and their charIds) + +How a run works + * One game per (character, rate), each on its own virtual display with its own memory stick copy and debug port + (battle_test.Game). Nothing is played through menus: the recorded Quick Battle walk (tests/replays/quick_battle_walk.txt, + Squall vs Lightning) is replayed inside the game, and PSPRECOMP_TEST_CHAR_SWAP (runtime/direct_battle.cpp) turns Squall + into the character under test (with his own save record and model); for Lightning the dummy becomes Squall. + * The dummy (the CPU fighter) never presses a button (PSPRECOMP_TEST_NO_AI / _AI_OFF) but runs and reacts to hits, with a + huge HP pool so no hit ends the fight. + * The game lists the fighter's actions (plya records: action id, script, first / later press, ground / air, button), then + this tool writes the trials into the running battle (test script `batch`): for each action + - plain: both fighters back at their spots facing each other (`home`), start the action (`action `), let it run; + - mash (first presses of an attack only): the same, and the move's button tapped every 200 ms for 2.4 s, so the + follow-up presses (combos, chains) happen too. + Air actions start 0.4 s after a jump. Every trial starts on the 30 Hz lattice (PSPRECOMP_TEST_PARITY), so the same game + moment exists at every rate. + * The runtime logs a sample of both fighters on every 30 Hz frame ([PAR]: script, motion, motion time, position, facing) + and every hit they take ([PARHIT]: attack id, reaction, damage). + * Each rate is compared with 30 fps, trial by trial: + - the player's script sequence and the dummy's motion sequence (same ids in the same order, each change within + --frame-tol 30 Hz frames); + - the hits the dummy took (same count, same attacks in the same order, each within --frame-tol frames, same damage); + - the paths of both fighters (largest gap at the same game moment <= --pos-tol metres, or --pos-rel of the distance + the fighter covered); + - the length of the trial (until the player is idle again) within --frame-tol frames. + With --repeat-base a second 30 fps run is made: trials where two 30 fps runs already disagree are reported as + NONDETERMINISTIC instead of failing. + * Output: //report.txt (every failing trial with its reasons), /summary.txt, /results.json; exit code 1 + when any trial fails. + +Needs what battle_test.py needs (Xvfb, a work folder with a save that has Quick Battle unlocked) and a runtime built with this +branch's test hooks. Runs are long: ~3-6 min per character and rate on a busy 20-core box; --jobs N runs N games at once.""" +import argparse +import bisect +import json +import math +import os +import re +import shutil +import subprocess +import sys +import tempfile +import threading +import time + +HERE = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, HERE) +import battle_test as bt # noqa: E402 + +RATES = [30, 60, 90, 120, 150, 180, 210, 240] # framerate.cpp k_rates: every rate the settings menu offers +WALK = os.path.join(HERE, '..', 'tests', 'replays', 'quick_battle_walk.txt') +WALK_PLAYER, WALK_CPU = 15, 31 # who the recorded walk picks (Squall vs Lightning) + +# charId -> (name, code). The 31 playable characters of Dissidia 012 (code strings at 0x089D6CA8; 23 = Chaos, the boss, is not +# playable, 33 = Aerith is an assist only). https://dissidia.wiki/Dissidia_012_Final_Fantasy +CHARS = { + 1: ('Warrior of Light', 'one100'), 2: ('Garland', 'one200'), 3: ('Firion', 'two100'), 4: ('The Emperor', 'two200'), + 5: ('Onion Knight', 'thr100'), 6: ('Cloud of Darkness', 'thr200'), 7: ('Cecil', 'for100'), 8: ('Golbez', 'for200'), + 9: ('Bartz', 'fiv100'), 10: ('Exdeath', 'fiv200'), 11: ('Terra', 'six100'), 12: ('Kefka', 'six200'), + 13: ('Cloud', 'sev100'), 14: ('Sephiroth', 'sev200'), 15: ('Squall', 'eht100'), 16: ('Ultimecia', 'eht200'), + 17: ('Zidane', 'nin100'), 18: ('Kuja', 'nin200'), 19: ('Tidus', 'ten100'), 20: ('Jecht', 'ten200'), + 21: ('Shantotto', 'gst100'), 22: ('Gabranth', 'gst200'), 24: ('Kain', 'for110'), 25: ('Gilgamesh', 'fiv210'), + 26: ('Tifa', 'sev110'), 27: ('Laguna', 'eht110'), 28: ('Yuna', 'ten110'), 29: ('Prishe', 'gst110'), 30: ('Vaan', 'gst120'), + 31: ('Lightning', 'gst130'), 32: ('Feral Chaos', 'org210'), +} +BUTTON = {1: 'CIRCLE', 2: 'SQUARE'} # plya +0x0F: 1 brave (Circle), 2 HP (Square) +# Movement every fighter has (pad input, not plya actions; default controls: Cross jump, R + Cross dodge, R + Triangle dash, +# R block). Each is a list of test script lines with times relative to the trial start (input begins at +0). +MOVES = { + 'jump': ['+0 tap CROSS 67'], + 'double_jump': ['+0 tap CROSS 67', '+400 tap CROSS 67'], + 'dodge': ['+0 tap R+CROSS 67'], + 'air_dodge': ['+0 tap CROSS 67', '+400 tap R+CROSS 67'], + 'dash': ['+0 set R+TRIANGLE', '+1200 set -'], + 'air_dash': ['+0 tap CROSS 67', '+400 set R+TRIANGLE', '+1400 set -'], + 'block': ['+0 set R', '+1000 set -'], + 'run': ['+0 set - 128 0', '+1500 set -'], + 'run_jump': ['+0 set - 128 0', '+600 set CROSS 128 0', '+667 set - 128 0', '+1500 set -'], +} +FRAME_MS = 1000.0 / 30.0 +HOME_DIST, HOME_X, HOME_Z = 3.5, 0.0, 0.0 # every trial: the dummy at stage point (x, z), the player HOME_DIST m from it, facing each other + + +def slug(cid): + return CHARS[cid][0].lower().replace(' ', '_') + + +def parse_chars(spec): + if spec in ('all', ''): + return sorted(CHARS) + if spec == 'none': # e.g. only the Jecht combo test: --chars none --jecht-combos + return [] + out = [] + for t in spec.split(','): + t = t.strip().lower() + if t.isdigit(): + out.append(int(t)) + continue + hit = [c for c, (n, code) in CHARS.items() if t in (n.lower(), n.lower().replace(' ', '_'), n.lower().replace(' ', ''), code)] + if not hit: + raise SystemExit('unknown character %r (fps_parity.py chars lists them)' % t) + out.append(hit[0]) + return out + + +# ---- one game: one character at one rate ------------------------------------------------------------------------------ + +_start_lock = threading.Lock() + + +def trial_lines(acts, variants, only=None, moves=True): + """the batch of trials for a fighter's action list [(id, script, later, side, button)] (the same text at every rate)""" + lines = ['+0 waitidle 8000', '+0 home %.2f %.2f %.2f' % (HOME_DIST, HOME_X, HOME_Z), '+3000 mark SETTLE'] # the first seconds after the intro are not + for name, steps in (MOVES.items() if moves and not only else []): # repeatable (a first trial gave 0 or 19 hits) + lines.append('+0 waitidle 8000') + lines.append('+0 home %.2f %.2f %.2f' % (HOME_DIST, HOME_X, HOME_Z)) + lines.append('+1100 mark T 0 %s' % name) # 1 s to settle first: running is camera-relative, and the camera swings after `home` + for st in steps: + t, rest = st.split(' ', 1) + lines.append('+%d %s' % (1200 + int(t[1:]), rest)) + lines.append('+4500 mark E 0 %s' % name) + for (aid, scr, later, side, button) in acts: + if only and aid not in only: + continue + for v in variants: + if v == 'mash' and (later or button not in BUTTON): + continue + lines.append('+0 waitidle 8000') + lines.append('+0 home %.2f %.2f %.2f' % (HOME_DIST, HOME_X, HOME_Z)) + lines.append('+100 mark T %d %s' % (aid, v)) + t = 700 # 0.6 s after `home`: right after the teleport a jump was sometimes not taken and the + if side == 1: # dummy sometimes not hittable (still getting up); an air action: jump first + lines.append('+700 tap CROSS 100') + t = 1100 + lines.append('+%d action %d player' % (t, aid)) + if v == 'mash': + for k in range(12): + lines.append('+%d tap %s 67' % (t + 300 + 200 * k, BUTTON[button])) + lines.append('+%d mark E %d %s' % (t + 3300, aid, v)) + lines.append('+0 waitidle 8000') + lines.append('+500 mark DONE') + lines.append('+600 quit') + return lines + + +def run_one(a, cid, rate, out, tag=None): + """boot, list the actions, play every trial, quit. Returns the log path (raises on failure).""" + tag = tag or str(rate) + os.makedirs(out, exist_ok=True) + log = os.path.join(out, 'run_%d.log' % rate) + batch = os.path.join(out, 'batch_%s.txt' % tag) + script = os.path.join(out, 'script_%s.txt' % tag) + for p in (batch, log): + if os.path.exists(p): + os.remove(p) + with open(script, 'w') as f: + f.write('1000 action -1 player\n1500 batch %s\n' % batch) + swap = '%d=%d' % (WALK_PLAYER, cid) if cid != WALK_PLAYER else '' + if cid == WALK_CPU: + swap = '%d=%d,%d=%d' % (WALK_PLAYER, WALK_CPU, WALK_CPU, WALK_PLAYER) # Lightning under test: the dummy becomes Squall + env = {'PSPRECOMP_TEST_PLAYER_CHAR': str(cid), 'PSPRECOMP_TEST_PARITY': '1', 'PSPRECOMP_TEST_DIRECT_BATTLE': os.path.abspath(WALK), + 'PSPRECOMP_FIGHTER_LOG': '1' if a.fighter_log else '0', # debugging only: [FIGHTER] / [MOTION] lines every update (big logs) + 'PSPRECOMP_TEST_NO_AI': '1', # the CPU's brain stopped (a running brain still starts moves even with its presses blocked) + 'PSPRECOMP_TEST_AI_OFF': '1', # its pad setters blocked and its pad cleared every update (framerate.cpp: nothing stays held) + 'PSPRECOMP_TEST_FREEZE_CPU': '0', # the dummy runs at the normal time step and reacts to hits + 'LP_NUM_THREADS': os.environ.get('LP_NUM_THREADS', '2'), 'PSPRECOMP_TEST_NO_DRAW': '0' if (a.draw or bt.gpu_headless()) and not a.no_draw else '2', + 'PSPRECOMP_TEST_INPUT_DELAY': str(a.input_delay), + 'PSPRECOMP_CAM_FREE': '0'} # the analogue camera runs on the wall clock (not repeatable under fast-forward) and turns the stick + if swap: + env['PSPRECOMP_TEST_CHAR_SWAP'] = swap + with _start_lock: # two games started in the same second pick the same X display + xp, disp = bt.start_xvfb() + g = bt.Game(os.path.abspath(a.work), rate, out, disp, bt.free_port(), script=script, extra_env=env) + g.log = log + g.start() + time.sleep(3) + t0 = time.time() + try: + bt.walk_direct(g, ff_menus=4) + g.cmd('FF 120000') + last_ff = time.time() + acts, wrote, want, last_check = [], False, None, 0.0 + deadline = time.time() + a.timeout + while g.alive(): + if time.time() > deadline: + raise RuntimeError('timeout (%d s)' % a.timeout) + if time.time() - last_ff > 50: + try: + g.cmd('FF 120000') + except OSError: + pass + last_ff = time.time() + if not wrote: + with open(log, errors='replace') as f: + if 'action -1 on 00000000' in f.read(): # the character under test is not in the fight (the swap did not take): nothing to wait for + raise RuntimeError('charId %d is not in the fight (character swap failed)' % cid) + acts, want = read_listing(log) # from the file: the harness's stream reader may have passed the lines already + if want is not None and len(acts) >= want: + only = set(int(x) for x in a.actions.split(',')) if a.actions else None + if a.max_actions: + acts = acts[:a.max_actions] + if a.moves_only: + acts = [] + with open(batch + '.tmp', 'w') as f: + f.write('\n'.join(trial_lines(acts, a.variants.split(','), only, moves=not a.no_moves)) + '\n') + os.replace(batch + '.tmp', batch) + wrote = True + if wrote and time.time() - last_check > 5: # a battle that ends after the trials began restarts the script: give up, the worker retries + last_check = time.time() + with open(log, errors='replace') as f: + txt = f.read() + first = txt.find('mark T ') + if first >= 0 and '[TEST] battle ended' in txt[first:]: + raise RuntimeError('the battle ended during the trials') + try: + size = os.path.getsize(log) + except OSError: + size = 0 + if size > a.max_log_mb * 1e6: + raise RuntimeError('log over %d MB (runaway output)' % a.max_log_mb) + time.sleep(1.0) + if not wrote: + raise RuntimeError('the game exited before listing the actions') + with open(log, errors='replace') as f: + txt = f.read() + if 'mark DONE' not in txt: + raise RuntimeError('the trials did not finish (no DONE mark)') + if re.search(r'\[TEST\] battle ended.*', txt[txt.find('mark T '):] if 'mark T ' in txt else ''): + raise RuntimeError('the battle ended during the trials') + n_trials, n_stuck = txt.count('mark T '), len(re.findall(r'waitidle TIMEOUT', txt)) + if n_trials and n_stuck > 0.25 * n_trials: # the fighters were rarely idle between trials: a stuck dummy / player, the run says nothing + raise RuntimeError('%d of %d waits for idle fighters timed out' % (n_stuck, n_trials)) + return log, time.time() - t0 + finally: + g.stop() + xp.terminate() + + +def read_listing(log): + """the fighter's actions from the last `action -1` listing in the log: ([(id, script, later, side, button)], record count or None)""" + try: + with open(log, errors='replace') as f: + txt = f.read() + except OSError: + return [], None + i = txt.rfind('[TEST] plya actor ') + if i < 0: + return [], None + m = re.match(r'\[TEST\] plya actor \w+: (\d+) records', txt[i:]) + acts = [tuple(int(x) for x in m2.groups()) for m2 in + re.finditer(r'\[TEST\] plya action (\d+) script (-?\d+) later (\d+) side (\d+) button (\d+)', txt[i:])] + return acts, (int(m.group(1)) if m else None) + + +def worker(a, jobs, results, lock): + while True: + with lock: + if not jobs: + return + cid, rate, tag = jobs.pop(0) + out = os.path.join(a.out, slug(cid), 'r%s' % tag) + msg = '' + for attempt in range(2): # a busy box sometimes loses a boot ("the battle never started"): once more + try: + log, secs = run_one(a, cid, rate, out, tag) + msg = 'ok %.0f s' % secs + with lock: + results[(cid, tag)] = log + break + except Exception as e: # noqa: BLE001 + msg = 'FAILED: %s' % e + print('[fps_parity] %-18s %4s fps: %s' % (CHARS[cid][0], tag, msg), file=sys.stderr, flush=True) + + +# ---- reading a log ---------------------------------------------------------------------------------------------------- + +class Trial: + def __init__(self, aid, variant, t0): + self.aid, self.variant, self.t0 = aid, variant, t0 + self.start = None # gt of the action start + self.result = None # 'started' / 'NOT started' / ... + self.end = None # gt of the E mark + self.idle = None # gt the player was idle again (the next waitidle), or None on a timeout + self.samples = [] # on the 30 Hz lattice: (gt, pscr, pcur, pmt, px, py, pz, dscr, dcur, dx, dy, dz) + self.every = [] # every update (same tuples): paths are aligned by whole updates + self.hits = [] # (gt, atkp, react, dmg, by) + self.scr = [] # (gt, new script) of the player, every update + + +def keyed(trials): + """{(action, variant, n-th occurrence): trial}""" + out, seen = {}, {} + for t in trials: + k = (t.aid, t.variant) + seen[k] = seen.get(k, 0) + 1 + out[(t.aid, t.variant, seen[k])] = t + return out + + +def parse(path): + trials, cur = [], None + rx_par = re.compile(r'^\[PAR\] ([\d.]+)(~?) \| (.*?) \| (.*?) \| (\d+) (\d+)') + pending_idle = None + with open(path, errors='replace') as f: + for l in f: + if l.startswith('[PAR] '): + if cur is None: + continue + m = rx_par.match(l) + if not m: + continue + p, d = m.group(3).split(), m.group(4).split() + if len(p) < 6 or len(d) < 6: + continue + smp = (float(m.group(1)), int(p[0]), int(p[1]), float(p[2]), float(p[3]), float(p[4]), float(p[5]), + int(d[0]), int(d[1]), float(d[3]), float(d[4]), float(d[5])) + cur.every.append(smp) + if not m.group(2): # '~' marks an update between two 30 Hz frames + cur.samples.append(smp) + elif l.startswith('[PARHIT] '): + if cur is None: + continue + m = re.match(r'\[PARHIT\] (\d+) (\w+) atkp=(-?\d+) react=(\d+) flags=\w+ dmg=(-?\d+) by=(\w+)', l) + if m and m.group(2) == 'dummy': + cur.hits.append((int(m.group(1)), int(m.group(3)), int(m.group(4)), int(m.group(5)), m.group(6))) + elif l.startswith('[TEST] gt='): + m = re.match(r'\[TEST\] gt=(\d+) mark T (\d+) (\w+)', l) + if m: + if cur is not None and cur.idle is None and pending_idle is not None: + cur.idle = pending_idle + cur = Trial(int(m.group(2)), m.group(3), int(m.group(1))) + if cur.aid == 0: # a movement trial: no action, the input starts 100 ms after the mark + cur.start, cur.result = cur.t0 + 100, 'started' + trials.append(cur) + pending_idle = None + continue + m = re.match(r'\[TEST\] gt=(\d+) mark E', l) + if m and cur is not None: + cur.end = int(m.group(1)) + continue + m = re.match(r'\[TEST\] gt=(\d+) mark DONE', l) + if m and cur is not None: + continue + m = re.match(r'\[TEST\] gt=(\d+) action (-?\d+) on \S+ \(\w+\): (.*)', l) + if m and cur is not None and int(m.group(2)) == cur.aid: + cur.start, cur.result = int(m.group(1)), m.group(3).strip() + continue + m = re.match(r'\[TEST\] gt=(\d+) waitidle (done|TIMEOUT)', l) + if m and cur is not None and cur.end is not None and cur.idle is None: + cur.idle = int(m.group(1)) if m.group(2) == 'done' else -1 + continue + m = re.match(r'\[TEST\] gt=(\d+) scr (-?\d+) -> (-?\d+)', l) + if m and cur is not None: + cur.scr.append((int(m.group(1)), int(m.group(3)))) + elif l.startswith('[TEST] battle ended') and cur is not None: + cur.result = (cur.result or '') + ' (battle ended)' + return trials + + +def segments(seq): + """[(value, first index, last index)] of a list of values""" + out = [] + for i, v in enumerate(seq): + if out and out[-1][0] == v: + out[-1][2] = i + else: + out.append([v, i, i]) + return out + + +def compare_trials(base, other, a, info=None): + """reasons why `other` does not play like `base` (both Trial), [] = the same. `info` (a list) gets notes that are not failures.""" + why = [] + info = [] if info is None else info + if base.result != other.result: + return ['start: %s vs %s at 30 fps' % (other.result, base.result)] + if base.result != 'started': + return [] + tb, to = base.start, other.start + sb = [s for s in base.samples if s[0] >= tb - 1] # (the log gives the start in whole ms: 410866.67 is logged as 410867) + so = [s for s in other.samples if s[0] >= to - 1] + n = min(len(sb), len(so)) + if n < 3: + return ['too few samples (%d / %d)' % (len(sb), len(so))] + # samples are on the 30 Hz lattice from the action start on: index k = the same game moment at both rates + for label, col in (('player script', 1), ('dummy motion', 8)): + a_seg = segments([s[col] for s in sb[:n]]) + o_seg = segments([s[col] for s in so[:n]]) + ids_a = [x[0] for x in a_seg] + ids_o = [x[0] for x in o_seg] + if ids_a != ids_o: + # first difference + i = next((i for i in range(min(len(ids_a), len(ids_o))) if ids_a[i] != ids_o[i]), min(len(ids_a), len(ids_o))) + why.append('%s sequence differs at change %d: %s vs %s at 30 fps (%.0f ms into the move)' % ( + label, i, ids_o[i:i + 4], ids_a[i:i + 4], (a_seg[i][1] if i < len(a_seg) else n) * FRAME_MS)) + else: + worst = max((abs(x[1] - y[1]) for x, y in zip(a_seg, o_seg)), default=0) + if worst > a.frame_tol: + k = next(i for i, (x, y) in enumerate(zip(a_seg, o_seg)) if abs(x[1] - y[1]) == worst) + why.append('%s %d starts %+d frames off (%.0f ms vs %.0f ms into the move)' % ( + label, a_seg[k][0], o_seg[k][1] - a_seg[k][1], o_seg[k][1] * FRAME_MS, a_seg[k][1] * FRAME_MS)) + # hits on the dummy + hb = [h for h in base.hits if h[0] >= tb - 1 and (base.end is None or h[0] <= base.end + 2000)] + ho = [h for h in other.hits if h[0] >= to - 1 and (other.end is None or h[0] <= other.end + 2000)] + if len(hb) != len(ho): + why.append('dummy took %d hits vs %d at 30 fps (attacks %s vs %s)' % (len(ho), len(hb), [h[1] for h in ho][:10], [h[1] for h in hb][:10])) + else: + for i, (x, y) in enumerate(zip(hb, ho)): + if x[1] != y[1] or x[2] != y[2]: + why.append('hit %d: attack %d reaction %d vs attack %d reaction %d at 30 fps' % (i + 1, y[1], y[2], x[1], x[2])) + break + dt = (y[0] - to) - (x[0] - tb) + if abs(dt) > a.frame_tol * FRAME_MS + 1: + why.append('hit %d (attack %d) lands %+.0f ms off (%.0f vs %.0f ms into the move)' % (i + 1, x[1], dt, y[0] - to, x[0] - tb)) + break + if abs(x[3] - y[3]) > max(5, a.dmg_tol * abs(x[3])): # HP damage = the attacker's bravery, which varies a little between runs + why.append('hit %d (attack %d) damage %d vs %d at 30 fps' % (i + 1, x[1], y[3], x[3])) + break + # paths: judged at the best alignment within +-a.shift 30 Hz frames. A move starts at the end of the first update after it was asked + # for: 33 ms after the press at 30 fps, 17 ms at 60, 8 ms at 120, so a high rate can lead by up to a frame (a phase, reported as info) + for label, cols in (('player', (4, 5, 6)), ('dummy', (9, 10, 11))): + travel = sum(math.sqrt(sum((sb[k][c] - sb[k - 1][c]) ** 2 for c in cols)) for k in range(1, n)) + ev = [x for x in other.every if x[0] >= to - FRAME_MS * a.shift - 1] or so + times = [x[0] for x in ev] + step = min((b - a_) for a_, b in zip(times, times[1:]) if b - a_ > 0.5) if len(times) > 2 else FRAME_MS + m_ = int(round(a.shift * FRAME_MS / step)) + best = None + for j in range(-m_, m_ + 1): + sh_ms = j * step + gap, at_k = 0.0, 0 + for k in range(n): + t = so[0][0] + (sb[k][0] - sb[0][0]) + sh_ms + i = bisect.bisect_left(times, t - step / 2) + if i >= len(times) or abs(times[i] - t) > step / 2: + continue + d = math.sqrt(sum((sb[k][c] - ev[i][c]) ** 2 for c in cols)) + if d > gap: + gap, at_k = d, k + if best is None or gap < best[0] - 1e-6 or (abs(gap - best[0]) <= 1e-6 and abs(j) < abs(best[2])): + best = (gap, at_k, j) + gap, at_k, j = best + sh = -1 if j < 0 else (1 if j > 0 else 0) + shift_ms = j * step + lim = max(a.pos_tol, a.pos_rel * travel) + if gap > lim: + why.append('%s path %.2f m off at %.0f ms into the move (moved %.2f m at 30 fps; limit %.2f m; best alignment %+.0f ms)' % ( + label, gap, at_k * FRAME_MS, travel, lim, shift_ms)) + elif sh and label == 'player': + info.append('player %.0f ms %s' % (abs(shift_ms), 'ahead' if sh < 0 else 'behind')) + # peak height (jumps, launches): a lower or higher arc is a difference even when the path stays within the tolerance + hb, ho = max(s[cols[1]] for s in sb[:n]), max(s[cols[1]] for s in so[:n]) + if abs(hb - ho) > a.height_tol and max(hb, ho) - min(sb[0][cols[1]], so[0][cols[1]]) > 0.5: + why.append('%s peak height %.2f m vs %.2f m at 30 fps' % (label, ho, hb)) + # length + if base.idle and other.idle and base.idle > 0 and other.idle > 0: + lb, lo = base.idle - tb, other.idle - to + if abs(lb - lo) > a.frame_tol * FRAME_MS + 1: + why.append('ends %+.0f ms off (player idle after %.0f ms vs %.0f ms at 30 fps)' % (lo - lb, lo, lb)) + elif (base.idle == -1) != (other.idle == -1): + why.append('the player %s idle again within 8 s (at 30 fps: %s)' % ('was not' if other.idle == -1 else 'was', 'not' if base.idle == -1 else 'yes')) + return why + + +def compare_char(a, cid, logs): + """logs: {tag: path}. Writes /report.txt, returns {rate: {'trials': n, 'fail': [...], 'nondet': [...]}}""" + parsed = {t: parse(p) for t, p in logs.items() if p and os.path.exists(p)} + res = {} + lines = ['%s (charId %d)' % (CHARS[cid][0], cid)] + if '30' not in parsed: + lines.append('no 30 fps run: nothing to compare') + res['error'] = 'no 30 fps run' + _write(a, cid, lines) + return res + base = keyed(parsed['30']) + nondet = set() + if '30b' in parsed: + for k, t in keyed(parsed['30b']).items(): + b = base.get(k) + if b and compare_trials(b, t, a): + nondet.add(k) + lines.append('30 fps twice: %d of %d trials differ between two 30 fps runs (NONDETERMINISTIC, not counted as failures)' % (len(nondet), len(base))) + started = sum(1 for t in base.values() if t.result == 'started') + lines.append('%d trials at 30 fps (%d actions started)' % (len(base), started)) + for tag in sorted((t for t in parsed if t not in ('30', '30b')), key=int): + fails, nd, n, ahead = [], [], 0, 0 + seen = keyed(parsed[tag]) + for key, b in base.items(): + o = seen.get(key) + if o is None: + fails.append((key, ['missing at this rate (run cut short?)'])) + continue + n += 1 + inf = [] + why = compare_trials(b, o, a, inf) + ahead += any('ahead' in x for x in inf) + if why: + (nd if key in nondet else fails).append((key, why)) + res[tag] = {'trials': n, 'fail': [[k[0], k[1], w] for k, w in fails], 'nondet': [[k[0], k[1], w] for k, w in nd]} + lines.append('') + lines.append('== %s fps vs 30 fps: %d trials, %d FAIL, %d nondeterministic; the player ran ahead (up to a frame: phase, not judged) in %d trials' % (tag, n, len(fails), len(nd), ahead)) + for (aid, v, occ), why in fails: + b = base[(aid, v, occ)] + scr = next((s[1] for s in b.samples if b.start and s[0] >= b.start), '?') + lines.append((' FAIL %s:' % v) if aid == 0 else (' FAIL action %d (%s, script %s):' % (aid, v, scr))) + for w in why: + lines.append(' ' + w) + _write(a, cid, lines) + return res + + +def _write(a, cid, lines): + d = os.path.join(a.out, slug(cid)) + os.makedirs(d, exist_ok=True) + with open(os.path.join(d, 'report.txt'), 'w') as f: + f.write('\n'.join(lines) + '\n') + + +def find_logs(a, cid): + d = os.path.join(a.out, slug(cid)) + logs = {} + if not os.path.isdir(d): + return logs + for sub in os.listdir(d): + m = re.match(r'r(\d+b?)$', sub) + if m: + tag = m.group(1) + p = os.path.join(d, sub, 'run_%d.log' % int(tag.rstrip('b'))) + if os.path.exists(p): + logs[tag] = p + return logs + + +def summarize(a, chars): + allres, bad = {}, 0 + rows = [] + for cid in chars: + r = compare_char(a, cid, find_logs(a, cid)) + allres[CHARS[cid][0]] = r + if 'error' in r: + rows.append('%-18s %s' % (CHARS[cid][0], r['error'])) + bad += 1 + continue + cells = [] + for tag in sorted((t for t in r), key=int): + nf = len(r[tag]['fail']) + bad += nf + cells.append('%s:%s' % (tag, 'ok' if nf == 0 else '%d FAIL' % nf)) + rows.append('%-18s %s' % (CHARS[cid][0], ' '.join(cells))) + text = 'Frame-rate parity vs 30 fps (per rate: failing trials; details in /report.txt)\n' + '\n'.join(rows) + '\n' + with open(os.path.join(a.out, 'summary.txt'), 'w') as f: + f.write(text) + with open(os.path.join(a.out, 'results.json'), 'w') as f: + json.dump(allres, f, indent=1) + print(text) + return 1 if bad else 0 + + +def show(a): + """one trial at every rate, side by side on the 30 Hz lattice""" + cid = parse_chars(a.char)[0] + logs = find_logs(a, cid) + tr = {} + for tag, p in logs.items(): + k = keyed(parse(p)) + t = k.get((a.action, a.variant, a.nth)) + if t: + tr[tag] = t + tags = sorted(tr, key=lambda x: (int(x.rstrip('b')), x)) + if not tags: + print('no such trial') + return 1 + for tag in tags: + t = tr[tag] + print('%4s fps: %s, hits %s' % (tag, t.result, [(h[0] - t.start, h[1], h[2], h[3]) for h in t.hits if t.start and h[0] >= t.start][:30])) + print('%6s ' % 'ms' + ' | '.join('%-46s' % ('%s fps: scr cur x y z / dummy cur x z' % tg) for tg in tags)) + rows = {tg: [s for s in tr[tg].samples if tr[tg].start is not None and s[0] >= tr[tg].start] for tg in tags} + n = max(len(r) for r in rows.values()) + for k in range(0, n, a.step): + cells = [] + for tg in tags: + r = rows[tg] + if k < len(r): + s = r[k] + cells.append('%5d %4d %6.2f %5.2f %6.2f / %4d %6.2f %6.2f' % (s[1], s[2], s[4], s[5], s[6], s[8], s[9], s[11])) + else: + cells.append(' ' * 46) + print('%6.0f ' % (k * FRAME_MS) + ' | '.join(cells)) + return 0 + + +def main(argv): + ap = argparse.ArgumentParser(description=__doc__.split('\n')[0]) + sub = ap.add_subparsers(dest='what', required=True) + p = sub.add_parser('run') + p.add_argument('--work', required=True) + p.add_argument('--out', default=None) + p.add_argument('--chars', default='all') + p.add_argument('--rates', default=','.join(map(str, RATES))) + p.add_argument('--jobs', type=int, default=4) + p.add_argument('--actions', default='', help='only these action ids (default: every action the fighter has)') + p.add_argument('--max-actions', dest='max_actions', type=int, default=0, help='only the first N actions (quick checks)') + p.add_argument('--jecht-combos', dest='jecht_combos', action='store_true', + help='also run the Jecht combo window test (tools/combo_timing.py combos) after the parity games; off by default') + p.add_argument('--combo-rates', dest='combo_rates', default='', help='rates for --jecht-combos (default: --rates, i.e. every rate 30..240)') + p.add_argument('--fighter-log', dest='fighter_log', action='store_true', help='debugging: the runtime\'s per-update fighter / object logs (big; use with --actions)') + p.add_argument('--moves-only', dest='moves_only', action='store_true', help='only the movement trials (jump, dodge, dash, block, run)') + p.add_argument('--no-moves', dest='no_moves', action='store_true', help='leave out the movement trials (jump, dodge, dash, block, run)') + p.add_argument('--variants', default='plain,mash') + p.add_argument('--repeat-base', dest='repeat_base', action='store_true', help='a second 30 fps run: trials that differ between two 30 fps runs are NONDETERMINISTIC') + p.add_argument('--timeout', type=int, default=3600, help='seconds per game') + p.add_argument('--input-delay', dest='input_delay', type=float, default=0.0, help='experiments: scripted presses reach the game this many ms later above 30 fps') + p.add_argument('--gpu', default='auto', choices=['auto', 'on', 'off'], + help='render on the NVIDIA GPU with no X server (battle_test BT_GPU; auto = when the NVIDIA EGL driver is installed); off = Xvfb + llvmpipe') + p.add_argument('--draw', action='store_true', help='keep drawing during the trials with Xvfb too (on the GPU it is always kept: it costs nothing there)') + p.add_argument('--no-draw', dest='no_draw', action='store_true', help='stop drawing once the fight runs (Xvfb + llvmpipe default: ~2.5x faster there)') + p.add_argument('--max-log-mb', dest='max_log_mb', type=int, default=1500) + q = sub.add_parser('compare') + q.add_argument('out') + q.add_argument('--chars', default='all') + sub.add_parser('chars') + w = sub.add_parser('show', help='one trial at every rate, side by side') + w.add_argument('out') + w.add_argument('char') + w.add_argument('action', type=int) + w.add_argument('variant', nargs='?', default='plain') + w.add_argument('--nth', type=int, default=1) + w.add_argument('--step', type=int, default=1, help='every n-th 30 Hz frame') + for x in (p, q): + x.add_argument('--frame-tol', dest='frame_tol', type=int, default=1, help='30 Hz frames a change / hit may be off (default 1)') + x.add_argument('--pos-tol', dest='pos_tol', type=float, default=0.25, help='metres a path may be off (default 0.25)') + x.add_argument('--pos-rel', dest='pos_rel', type=float, default=0.05, help='or this share of the distance covered (default 0.05)') + x.add_argument('--shift', type=int, default=1, help='30 Hz frames a path may be shifted in time (phase) before it is compared (default 1)') + x.add_argument('--height-tol', dest='height_tol', type=float, default=0.10, help='metres a peak height may differ (default 0.10)') + x.add_argument('--dmg-tol', dest='dmg_tol', type=float, default=0.03, help='share an HP hit\'s damage may differ (default 0.03; two 30 fps runs differ by up to 2%%)') + a = ap.parse_args(argv) + if a.what == 'chars': + for c, (n, code) in sorted(CHARS.items()): + print('%2d %-18s %s' % (c, n, code)) + return 0 + if a.what == 'show': + return show(a) + if a.what == 'compare': + chars = [c for c in parse_chars(a.chars) if os.path.isdir(os.path.join(a.out, slug(c)))] + return summarize(a, chars) + os.environ['BT_GPU'] = {'auto': 'auto', 'on': '1', 'off': '0'}[a.gpu] + a.out = os.path.abspath(a.out or tempfile.mkdtemp(prefix='fps_parity_')) + os.makedirs(a.out, exist_ok=True) + chars = parse_chars(a.chars) + rates = [int(x) for x in a.rates.split(',')] + if 30 not in rates: + rates = [30] + rates + jobs = [] + for cid in chars: # character by character, all rates of one character together + for r in rates: + jobs.append((cid, r, str(r))) + if a.repeat_base: + jobs.append((cid, 30, '30b')) + print('[fps_parity] %d characters x %d rates = %d games, %d at once, output %s' % (len(chars), len(rates), len(jobs), a.jobs, a.out), file=sys.stderr) + results, lock = {}, threading.Lock() + th = [threading.Thread(target=worker, args=(a, jobs, results, lock)) for _ in range(max(1, a.jobs))] + for t in th: + t.start() + for t in th: + t.join() + rc = summarize(a, chars) if jobs is not None and chars else 0 + if a.jecht_combos: # optional: Jecht's timed chain windows (tools/combo_timing.py), one game per rate + import combo_timing + crates = [int(x) for x in (a.combo_rates or a.rates).split(',')] + crates = [30] + [r for r in crates if r != 30] + print('[fps_parity] Jecht combo windows at %s fps' % ','.join(map(str, crates)), file=sys.stderr) + crc = combo_timing.run_combos(os.path.abspath(a.work), crates, os.path.join(a.out, 'jecht_combos'), jobs=a.jobs) + with open(os.path.join(a.out, 'summary.txt'), 'a') as f: + f.write('\nJecht combo windows (jecht_combos/combos.txt): %s\n' % ('PASS' if crc == 0 else 'FAIL')) + rc = rc or crc + return rc + + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) diff --git a/psprecomp/runtime/include/psp_ge_draw.h b/psprecomp/runtime/include/psp_ge_draw.h index 2c01da1..92d386f 100644 --- a/psprecomp/runtime/include/psp_ge_draw.h +++ b/psprecomp/runtime/include/psp_ge_draw.h @@ -43,6 +43,24 @@ void ge_draw_set_hud_zones(int top_rows, int bottom_rows, int bottom_visible); int ge_draw_hud_bottom_rows(); // the room kept for the HUD under the touch controls, PSP rows (0 = none) // Textures (tex_addr & 0xFFFFFF) whose 2D draws are never pinned: markers that follow a fighter (the lock-on ring) stay on the picture. void ge_draw_set_hud_fixed_tex(const uint32_t* addrs, int n); +// Test harnesses: 0 draw as usual, 1 rasterizer discard, 2 skip the draw calls (lists are still processed). Applied at the next present. +void ge_draw_request_no_raster(int mode); + +/// Frame interpolation (a game that runs its logic at 30 Hz and re-issues its frame on the frames in between). +/// Game thread, before each frame's lists are enqueued (a list takes the tag current when it is enqueued, so the game module must publish +/// the tag of the frame whose list is about to be submitted: Dissidia submits a frame's list after its frame-end hook, see framerate.cpp): +/// gen = number of the gameplay frame whose state the frame shows, +/// phase = how far the frame is shown past that gameplay frame, in gameplay frames (0..1; k/n for the k-th of n-1 drawn-only frames +/// when paced by frame count, measured time when paced by the clock; down to -0.5 for a gameplay frame shown early); -1 turns it off. record = this is the gameplay frame +/// itself (its draws become the current table); seq numbers the frames. +/// The renderer records every 3D draw's world / view / projection / bone matrices on gameplay frames and draws each frame with +/// matrices between the last two gameplay frames (PSPRECOMP_INTERP=1, default: one gameplay frame behind) or projected ahead of +/// the last one (=2); =0 off. A draw is matched with the one of the previous gameplay frame by what it draws (vertex / index / texture +/// address, type, count, target) and its occurrence; unmatched draws and jumps (cuts, teleports) are drawn as issued. +struct GeInterpTag { uint32_t gen = 0, seq = 0; float phase = -1.0f; bool record = false; }; +void ge_interp_set_frame(const GeInterpTag& tag); +GeInterpTag ge_interp_current_tag(); // render queue: the tag a list gets at enqueue +void ge_interp_list_tag(const GeInterpTag& tag); // render thread: the tag of the list about to be processed // Height (PSP rows) of the displayed buffers: 272, or more on a tall window. The settings screen draws a canvas that tall; an overlay // canvas taller than 480:272 is drawn over the whole picture instead of the PSP's band. int ge_draw_tall_rows(); diff --git a/psprecomp/runtime/include/psp_render_queue.h b/psprecomp/runtime/include/psp_render_queue.h index 5c63719..da0512f 100644 --- a/psprecomp/runtime/include/psp_render_queue.h +++ b/psprecomp/runtime/include/psp_render_queue.h @@ -1,5 +1,6 @@ #pragma once #include +#include "psp_ge_draw.h" // GeInterpTag /// Render request types for the GL-on-main-thread queue. /// Phase 5 fills in actual GL dispatch for each type. @@ -40,6 +41,7 @@ struct GePendingList { bool processed = false; bool stalled = false; // True if hit stall, waiting for update bool begun = false; // True if ge_draw_begin_list was called + GeInterpTag ip_tag; // frame interpolation tag captured at enqueue (ge_interp_set_frame, psp_ge_draw.h) }; /// Game thread side: post FramePresent request and block until main thread diff --git a/psprecomp/runtime/src/psp_env_options.inc b/psprecomp/runtime/src/psp_env_options.inc index 10e9b57..e195704 100644 --- a/psprecomp/runtime/src/psp_env_options.inc +++ b/psprecomp/runtime/src/psp_env_options.inc @@ -75,6 +75,8 @@ const EnvOpt k_env_opts[] = { // ---- Timing ---- {"FPS_SCOPE", EC_TIMING, 'c', "battle|all", "Where the chosen frame rate applies. Battle (default): only in fights. All: everywhere, so menus run fast too (testing)."}, + {"LOGIC30", EC_TIMING, 'k', "", "30 Hz battle logic (default on): above 30 fps the fight itself runs at the PSP's 30 updates a second and the frames between are drawn by interpolating models and camera, so every move plays exactly as at 30 fps."}, + {"INTERP", EC_TIMING, 'c', "0|1|2", "With 30 Hz battle logic: how the frames between two updates are drawn. 1 (default): blend the last two updates (smooth, up to one update late). 2: extrapolate ahead (less lag, can overshoot). 0: show each update as it is (30 fps motion)."}, {"FX60", EC_TIMING, 'k', "", "Slow the effect components that run too fast above 30 fps. Off: leave them as they are (A/B check)."}, {"GAUGE30", EC_TIMING, 'k', "", "Keep the assist and EX gauges draining at the 30 fps rate. Off: they drain faster at high frame rates."}, {"UI_STEP", EC_TIMING, 'k', "", "Menu and HUD animations that count frames run in real time at any rate. Off: they speed up above 30 fps."}, diff --git a/psprecomp/runtime/src/psp_ge_draw.cpp b/psprecomp/runtime/src/psp_ge_draw.cpp index c7118ed..5e0cf25 100644 --- a/psprecomp/runtime/src/psp_ge_draw.cpp +++ b/psprecomp/runtime/src/psp_ge_draw.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -356,6 +357,10 @@ static std::atomic g_draw_ready{false}; // PSPRECOMP_NO_RASTER=1: GL_RASTERIZER_DISCARD (draws submitted, nothing // rasterised); =2: skip glDrawArrays entirely. Measures CPU-side cost only. static int g_no_raster = 0; +static std::atomic g_no_raster_request{-1}; +// Test harnesses (games/dissidia frame-rate parity runs): stop drawing once the fight's HUD has been seen. 0 draw, 1 rasterizer discard, +// 2 no draw calls (the GE lists are still processed). Taken over by the render thread at the next present. +void ge_draw_request_no_raster(int mode) { g_no_raster_request.store(mode); } static int g_no_raster_mode = 0; // requested mode (PSPRECOMP_NO_RASTER) static double g_no_raster_after = 0.0; // PSPRECOMP_NO_RASTER_AFTER=: enable only after s seconds static std::chrono::steady_clock::time_point g_t_start = std::chrono::steady_clock::now(); @@ -2702,7 +2707,201 @@ void ge_draw_end_list() { static void ge_draw_prim_one(uint8_t* rdram, int prim_type, int count); +// ---- Frame interpolation (ge_interp_set_frame, psp_ge_draw.h) ---- +namespace { +struct IpMats { float w[12], v[12], p[16], b[96], tg[12], uv[4]; }; // uv: texture scale u/v, offset u/v (scrolling textures) +std::mutex g_ip_set_mu; +GeInterpTag g_ip_set; // the tag the game thread set for the frame it is building +uint32_t g_ip_gen = 0; // tag of the list being processed (render thread) +float g_ip_phase = -1.0f; +uint32_t g_ip_seq = 0; +bool g_ip_rec = false; +uint32_t g_ip_tab_gen = 0; // gameplay frame recorded in g_ip_cur +bool g_ip_have = false; +std::unordered_map g_ip_prev, g_ip_cur; +std::unordered_map g_ip_occ; // occurrences of each draw key in the frame being drawn +uint32_t g_ip_occ_seq = ~0u; +int ip_mode() { + static const int m = [] { const char* e = std::getenv("PSPRECOMP_INTERP"); return e && e[0] ? std::atoi(e) : 1; }(); + return m; +} +uint64_t ip_key(const GeState& st, int prim_type, int count, bool loose = false) { + uint64_t h = loose ? 0x5bd1e9955bd1e995ull : 1469598103934665603ull; + auto mix = [&](uint64_t v) { h = (h ^ v) * 1099511628211ull; }; + if (!loose) { mix(st.vertex_addr); mix(st.index_addr); } // loose: without the addresses (the game double-buffers some vertex data, e.g. weapons) + mix(st.vertex_type); mix(static_cast(prim_type)); mix(static_cast(count)); + mix(st.tex_addr[0]); // not the render target: the game alternates two framebuffers every frame + return h; +} +void ip_lerp(float* dst, const float* a, const float* b, float t, int n) { for (int i = 0; i < n; i++) dst[i] = a[i] + (b[i] - a[i]) * t; } +bool ip_far(const float* a, const float* b) { // world positions more than 3 m apart: a teleport, do not blend + const float dx = a[9] - b[9], dy = a[10] - b[10], dz = a[11] - b[11]; + return dx * dx + dy * dy + dz * dz > 9.0f; +} +void ip_eye(const float* m, float* e) { // camera position of a GE view matrix (rows 0..2 the basis, row 3 = m[9..11] the translation): e = -t R^T + for (int j = 0; j < 3; j++) e[j] = -(m[9] * m[j * 3 + 0] + m[10] * m[j * 3 + 1] + m[11] * m[j * 3 + 2]); +} +bool ip_cut(const float* a, const float* b) { // the camera moved more than 3 m between two gameplay frames: a cut, do not blend + float ea[3], eb[3]; ip_eye(a, ea); ip_eye(b, eb); + const float dx = ea[0] - eb[0], dy = ea[1] - eb[1], dz = ea[2] - eb[2]; + return dx * dx + dy * dy + dz * dz > 9.0f; +} +} // namespace + +void ge_interp_set_frame(const GeInterpTag& tag) { std::lock_guard lk(g_ip_set_mu); g_ip_set = tag; } +GeInterpTag ge_interp_current_tag() { std::lock_guard lk(g_ip_set_mu); return g_ip_set; } +static int ip_trace_level() { // PSPRECOMP_INTERP_TRACE: 1 = per frame the drawn (blended) position of the biggest skinned draw and the camera; 2 = also the lists' tags and one stage draw's live / previous / blended view + static const int l = [] { const char* e = std::getenv("PSPRECOMP_INTERP_TRACE"); return e && e[0] ? std::max(1, std::atoi(e)) : 0; }(); + return l; +} +void ge_interp_list_tag(const GeInterpTag& tag) { + if (ip_trace_level() >= 2) std::fprintf(stderr, "[IPL] list tag seq %u gen %u rec %d phase %.3f\n", tag.seq, tag.gen, tag.record ? 1 : 0, tag.phase); + g_ip_gen = tag.gen; g_ip_phase = tag.phase; g_ip_seq = tag.seq; g_ip_rec = tag.record; +} + +// Swap the draw's matrices for interpolated ones (restored by the caller); false = drawn as issued. +struct IpStat { float pw[3] = {}, eye[3] = {}; int pcount = -1; uint32_t gen = 0, seq = ~0u; float phase = -2.f; int draws = 0, applied = 0, nomatch = 0, cut_n = 0, noprev = 0, flat = 0, skinned = 0, matmove = 0, vtxonly = 0, vtxprims = 0; + int scount = -1; float s_live[3] = {}, s_prev[3] = {}, s_blend[3] = {}; uint32_t s_key = 0; int s_how = 0; bool s_rec = false; }; // trace 2: the biggest unskinned 3D draw (stage): eye of its live / previous / blended view +std::unordered_map g_ip_vhash_prev, g_ip_vhash_cur; // diagnostic (PSPRECOMP_INTERP_LOG): vertex bytes per draw key on gameplay frames +uint8_t* g_ip_rdram = nullptr; +// 2D draws that follow a fighter (lock-on ring, target pointer): the game aims them at the newest gameplay state, the 3D scene is drawn +// blended. Per frame, every skinned draw's root on screen (PSP pixels) with the newest and with the blended matrices; a following 2D draw +// moves by the difference of the nearest one. +struct IpAnchor { float live[2], drawn[2]; }; +std::vector g_ip_anchors; +uint32_t g_ip_anchor_seq = ~0u; +bool g_ip2d_on = false; // the draw being decoded is such a draw (set by ip_apply, cleared after the draw) +bool ip_screen(const float* w, const float* v, const float* p, const float* b, float* out) { // root (bone 0 origin) -> PSP screen pixels + float o[3] = {b[9], b[10], b[11]}, wp[3], vp[3], c[4]; + for (int j = 0; j < 3; j++) wp[j] = o[0] * w[j] + o[1] * w[3 + j] + o[2] * w[6 + j] + w[9 + j]; + for (int j = 0; j < 3; j++) vp[j] = wp[0] * v[j] + wp[1] * v[3 + j] + wp[2] * v[6 + j] + v[9 + j]; + for (int j = 0; j < 4; j++) c[j] = vp[0] * p[j] + vp[1] * p[4 + j] + vp[2] * p[8 + j] + p[12 + j]; + if (c[3] <= 0.01f) return false; + out[0] = (c[0] / c[3] + 1.0f) * 240.0f; out[1] = (1.0f - c[1] / c[3]) * 136.0f; + return true; +} +static IpStat g_ip_stat; +static void ip_stat_flush() { + static int left = [] { const char* e = std::getenv("PSPRECOMP_INTERP_LOG"); return e ? std::atoi(e) : 0; }(); + if (left > 0 && g_ip_stat.draws > 0) { left--; std::fprintf(stderr, "[INTERP] gen %u phase %.2f: %d 3D draws, %d blended, %d no match, %d far, %d no previous frame, %d 2D; %d skinned, %d moved by matrix, %d moved by vertices only (%d prims)\n", + g_ip_stat.gen, g_ip_stat.phase, g_ip_stat.draws, g_ip_stat.applied, g_ip_stat.nomatch, g_ip_stat.cut_n, g_ip_stat.noprev, g_ip_stat.flat, g_ip_stat.skinned, g_ip_stat.matmove, g_ip_stat.vtxonly, g_ip_stat.vtxprims); } + static const bool trace = std::getenv("PSPRECOMP_INTERP_TRACE") != nullptr; // per frame: where the biggest skinned draw (a fighter) and the camera are drawn + if (trace && g_ip_stat.pcount >= 0) std::fprintf(stderr, "[IPTRACE] seq %u gen %u phase %.3f rec %d model %.4f %.4f %.4f eye %.4f %.4f %.4f\n", g_ip_stat.seq, g_ip_stat.gen, g_ip_stat.phase, + 0, g_ip_stat.pw[0], g_ip_stat.pw[1], g_ip_stat.pw[2], g_ip_stat.eye[0], g_ip_stat.eye[1], g_ip_stat.eye[2]); + if (ip_trace_level() >= 2 && g_ip_stat.scount >= 0) std::fprintf(stderr, "[IPSTAGE] seq %u gen %u phase %.3f rec %d key %08X n %d how %d live %.4f %.4f %.4f prev %.4f %.4f %.4f blend %.4f %.4f %.4f; %d draws %d blended %d nomatch\n", + g_ip_stat.seq, g_ip_stat.gen, g_ip_stat.phase, g_ip_stat.s_rec ? 1 : 0, g_ip_stat.s_key, g_ip_stat.scount, g_ip_stat.s_how, g_ip_stat.s_live[0], g_ip_stat.s_live[1], g_ip_stat.s_live[2], + g_ip_stat.s_prev[0], g_ip_stat.s_prev[1], g_ip_stat.s_prev[2], g_ip_stat.s_blend[0], g_ip_stat.s_blend[1], g_ip_stat.s_blend[2], g_ip_stat.draws, g_ip_stat.applied, g_ip_stat.nomatch); + g_ip_stat = IpStat{}; g_ip_stat.gen = g_ip_gen; g_ip_stat.phase = g_ip_phase; g_ip_stat.seq = g_ip_seq; +} +static bool ip_apply(int prim_type, int count, IpMats* saved) { + if (g_ip_phase <= -1.0f || ip_mode() == 0) return false; // -1 = off; a gameplay frame shown a bit early has a small negative phase + if (g_ip_stat.seq != g_ip_seq) ip_stat_flush(); + GeState& st = ge_get_state_mut(); + if (st.clear_mode) { g_ip_stat.flat++; return false; } + if (g_ip_rec && (!g_ip_have || g_ip_gen != g_ip_tab_gen)) { // a new gameplay frame: its draws become the current table + g_ip_prev.swap(g_ip_cur); g_ip_cur.clear(); + g_ip_have = g_ip_tab_gen + 1 == g_ip_gen; // the previous table is the gameplay frame just before + g_ip_tab_gen = g_ip_gen; + } + if (g_ip_seq != g_ip_occ_seq) { g_ip_occ.clear(); g_ip_occ_seq = g_ip_seq; } + if (ge_vtype_through(st.vertex_type)) { // 2D (HUD, screen copies): no matrices. The HUD that follows a fighter (lock-on ring, target + g_ip_stat.flat++; // pointer: hud_fixed_tex) is placed by the game on gameplay frames only: its screen position is + if (hud_fixed_tex(st)) g_ip2d_on = true; // moved with the blended fighter it is on (ge_draw_through_ip, called by the vertex decoder) + return false; + } + g_ip_stat.draws++; + const uint64_t base = ip_key(st, prim_type, count), lbase = ip_key(st, prim_type, count, true); + const uint64_t key = base ^ (static_cast(g_ip_occ[base]++) * 0x9E3779B97F4A7C15ull); + const uint64_t lkey = lbase ^ (static_cast(g_ip_occ[lbase]++) * 0x9E3779B97F4A7C15ull); + IpMats cur; + std::memcpy(cur.w, st.world_matrix, sizeof cur.w); std::memcpy(cur.v, st.view_matrix, sizeof cur.v); + std::memcpy(cur.p, st.proj_matrix, sizeof cur.p); std::memcpy(cur.b, st.bone_matrix, sizeof cur.b); + std::memcpy(cur.tg, st.tgen_matrix, sizeof cur.tg); + cur.uv[0] = st.tex_scale_u; cur.uv[1] = st.tex_scale_v; cur.uv[2] = st.tex_offset_u; cur.uv[3] = st.tex_offset_v; + if (g_ip_rec && g_ip_gen == g_ip_tab_gen) { g_ip_cur[key] = cur; g_ip_cur[lkey] = cur; } + static const bool diag = std::getenv("PSPRECOMP_INTERP_LOG") != nullptr; + if (diag && g_ip_rec && g_ip_rdram) { // what moves this draw between gameplay frames: its matrices or its vertex bytes + if (st.vertex_type & 0x1C000u) g_ip_stat.skinned++; // weight count bits: GE skinning + uint64_t vh = 1469598103934665603ull; const uint32_t va = st.vertex_addr & 0x07FFFFFFu; + for (uint32_t i = 0; i < 256 && va + i < 0x08000000u; i += 4) { uint32_t w; std::memcpy(&w, g_ip_rdram + va + i, 4); vh = (vh ^ w) * 1099511628211ull; } + if (g_ip_gen == g_ip_tab_gen) { if (g_ip_vhash_cur.empty() || g_ip_vhash_cur.count(~0ull) == 0 || g_ip_vhash_cur[~0ull] != g_ip_gen) { g_ip_vhash_prev.swap(g_ip_vhash_cur); g_ip_vhash_cur.clear(); g_ip_vhash_cur[~0ull] = g_ip_gen; } g_ip_vhash_cur[key] = vh; } + auto pm = g_ip_prev.find(key); auto ph = g_ip_vhash_prev.find(key); + if (pm != g_ip_prev.end() && ph != g_ip_vhash_prev.end()) { + const bool mat = std::memcmp(pm->second.w, cur.w, sizeof cur.w) || std::memcmp(pm->second.v, cur.v, sizeof cur.v) || std::memcmp(pm->second.b, cur.b, sizeof cur.b); + if (mat) g_ip_stat.matmove++; else if (ph->second != vh) { g_ip_stat.vtxonly++; g_ip_stat.vtxprims += count; } + } + } + const bool stage_tr = ip_trace_level() >= 2 && !(st.vertex_type & 0x1C000u) && !st.depth_write_disable && st.depth_test_enable && count > g_ip_stat.scount; + if (stage_tr) { // trace 2: this (so far biggest unskinned, depth-writing) draw's live view, drawn as issued unless replaced below + g_ip_stat.scount = count; g_ip_stat.s_key = static_cast(base); g_ip_stat.s_how = 0; g_ip_stat.s_rec = g_ip_rec; + ip_eye(cur.v, g_ip_stat.s_live); ip_eye(cur.v, g_ip_stat.s_blend); std::memset(g_ip_stat.s_prev, 0, sizeof g_ip_stat.s_prev); + } + if (!g_ip_have) { g_ip_stat.noprev++; return false; } + auto it = g_ip_prev.find(key); + if (it == g_ip_prev.end()) it = g_ip_prev.find(lkey); // the same draw from the other of two vertex buffers + if (it == g_ip_prev.end()) { g_ip_stat.nomatch++; return false; } + const IpMats& pv = it->second; + if (stage_tr) { g_ip_stat.s_how = it->first == key ? 1 : 2; ip_eye(pv.v, g_ip_stat.s_prev); } + if (ip_far(pv.w, cur.w) || ip_cut(pv.v, cur.v)) { + static int lg = [] { const char* e = std::getenv("PSPRECOMP_INTERP_LOG"); return e ? 12 : 0; }(); + if (lg > 0) { lg--; float ea[3], eb[3]; ip_eye(pv.v, ea); ip_eye(cur.v, eb); + std::fprintf(stderr, "[INTERPFAR] phase %.2f w %.2f,%.2f,%.2f -> %.2f,%.2f,%.2f v %.2f,%.2f,%.2f -> %.2f,%.2f,%.2f eye %.2f,%.2f,%.2f -> %.2f,%.2f,%.2f\n", g_ip_phase, + pv.w[9], pv.w[10], pv.w[11], cur.w[9], cur.w[10], cur.w[11], pv.v[9], pv.v[10], pv.v[11], cur.v[9], cur.v[10], cur.v[11], ea[0], ea[1], ea[2], eb[0], eb[1], eb[2]); } + g_ip_stat.cut_n++; return false; } + g_ip_stat.applied++; + *saved = cur; + if (st.vertex_type & 0x1C000u) { // a skinned draw (fighter): its root on screen, newest and blended (for following 2D draws) + if (g_ip_anchor_seq != g_ip_seq) { g_ip_anchors.clear(); g_ip_anchor_seq = g_ip_seq; } + float bw[12], bv[12], bp[16], bb[12]; + ip_lerp(bw, pv.w, cur.w, ip_mode() == 2 ? 1.0f + g_ip_phase : g_ip_phase, 12); ip_lerp(bv, pv.v, cur.v, ip_mode() == 2 ? 1.0f + g_ip_phase : g_ip_phase, 12); + ip_lerp(bp, pv.p, cur.p, ip_mode() == 2 ? 1.0f + g_ip_phase : g_ip_phase, 16); ip_lerp(bb, pv.b, cur.b, ip_mode() == 2 ? 1.0f + g_ip_phase : g_ip_phase, 12); + IpAnchor a; + if (g_ip_anchors.size() < 64 && ip_screen(cur.w, cur.v, cur.p, cur.b, a.live) && ip_screen(bw, bv, bp, bb, a.drawn)) g_ip_anchors.push_back(a); + } + g_ge_state_gen++; // the draw-key cache (keyed by this) must not reuse matrices built for another draw: the blend changes them without a GE command + const float t = ip_mode() == 2 ? 1.0f + g_ip_phase : g_ip_phase; // 1: prev -> cur (one frame behind); 2: cur projected ahead + ip_lerp(st.world_matrix, pv.w, cur.w, t, 12); ip_lerp(st.view_matrix, pv.v, cur.v, t, 12); + ip_lerp(st.proj_matrix, pv.p, cur.p, t, 16); ip_lerp(st.bone_matrix, pv.b, cur.b, t, 96); + ip_lerp(st.tgen_matrix, pv.tg, cur.tg, t, 12); + float uv[4]; ip_lerp(uv, pv.uv, cur.uv, t, 4); + for (int i = 0; i < 4; i++) if (std::fabs(cur.uv[i] - pv.uv[i]) > 0.5f) uv[i] = cur.uv[i]; // a scrolling offset that wrapped: no blend + st.tex_scale_u = uv[0]; st.tex_scale_v = uv[1]; st.tex_offset_u = uv[2]; st.tex_offset_v = uv[3]; + if (stage_tr) ip_eye(st.view_matrix, g_ip_stat.s_blend); + if ((st.vertex_type & 0x1C000u) && count > g_ip_stat.pcount) { // trace: the drawn (blended) position of the biggest skinned draw = world x bone 0 translation + g_ip_stat.pcount = count; + const float* w = st.world_matrix; const float* b = st.bone_matrix; + for (int j = 0; j < 3; j++) g_ip_stat.pw[j] = b[9] * w[j] + b[10] * w[3 + j] + b[11] * w[6 + j] + w[9 + j]; + ip_eye(st.view_matrix, g_ip_stat.eye); + } + return true; +} + +// Vertex decoder (through mode, PSP pixels): the offset that moves this 2D draw from where the game put it (centre cx, cy) to the blend of +// where it was on the last two gameplay frames. false = leave it. +bool ge_draw_through_ip(float cx, float cy, float* dx, float* dy) { + if (!g_ip2d_on || g_ip_anchor_seq != g_ip_seq || g_ip_anchors.empty()) return false; + const IpAnchor* best = nullptr; float bd = 90.0f * 90.0f; // the fighter whose newest root is nearest (within 90 px) + for (const auto& a : g_ip_anchors) { + const float d = (a.live[0] - cx) * (a.live[0] - cx) + (a.live[1] - cy) * (a.live[1] - cy); + if (d < bd) { bd = d; best = &a; } + } + if (!best) return false; + *dx = best->drawn[0] - best->live[0]; *dy = best->drawn[1] - best->live[1]; + if (*dx * *dx + *dy * *dy > 60.0f * 60.0f) return false; // a cut: leave it + if (ip_trace_level() >= 3) std::fprintf(stderr, "[IP2D] seq %u phase %.2f centre %.1f,%.1f anchor live %.1f,%.1f drawn %.1f,%.1f -> %+.1f,%+.1f\n", g_ip_seq, g_ip_phase, + cx, cy, best->live[0], best->live[1], best->drawn[0], best->drawn[1], *dx, *dy); + return true; +} + void ge_draw_prim(uint8_t* rdram, int prim_type, int count) { + g_ip_rdram = rdram; + struct Ip2dOff { ~Ip2dOff() { g_ip2d_on = false; } } ip2d_off; + IpMats ip_saved; + struct IpRestore { IpMats* m; bool on; ~IpRestore() { if (!on) return; GeState& st = ge_get_state_mut(); + std::memcpy(st.world_matrix, m->w, sizeof m->w); std::memcpy(st.view_matrix, m->v, sizeof m->v); + std::memcpy(st.proj_matrix, m->p, sizeof m->p); std::memcpy(st.bone_matrix, m->b, sizeof m->b); + std::memcpy(st.tgen_matrix, m->tg, sizeof m->tg); + st.tex_scale_u = m->uv[0]; st.tex_scale_v = m->uv[1]; st.tex_offset_u = m->uv[2]; st.tex_offset_v = m->uv[3]; g_ge_state_gen++; } } ip_restore{&ip_saved, count > 0 && ip_apply(prim_type, count, &ip_saved)}; psp_perf::Scope perf_scope_(psp_perf::PRIM); if (!g_ab || !g_fast_enabled || g_current_target < 0 || !g_targets[g_current_target].ab_fbo) { ge_draw_prim_one(rdram, prim_type, count); @@ -5892,6 +6091,12 @@ void ge_present_frame( } } psp_prof::Scope prof_present(psp_prof::S_PRESENT); + if (const int req = g_no_raster_request.exchange(-1); req >= 0 && req != g_no_raster) { // ge_draw_request_no_raster (render thread applies it) + if (g_no_raster == 1) glDisable(GL_RASTERIZER_DISCARD); + g_no_raster = req; + if (g_no_raster == 1) glEnable(GL_RASTERIZER_DISCARD); + std::fprintf(stderr, "[DRAW] raster mode %d (requested)\n", g_no_raster); + } if (g_no_raster_mode && g_no_raster == 0 && g_no_raster_after > 0.0 && std::chrono::duration(std::chrono::steady_clock::now() - g_t_start).count() >= g_no_raster_after) { diff --git a/psprecomp/runtime/src/psp_ge_vertex.cpp b/psprecomp/runtime/src/psp_ge_vertex.cpp index d63703e..71227ba 100644 --- a/psprecomp/runtime/src/psp_ge_vertex.cpp +++ b/psprecomp/runtime/src/psp_ge_vertex.cpp @@ -769,6 +769,13 @@ void ge_transform_vertices( if (g_hud_top_rows && maxy <= float(g_hud_top_rows)) tt = g_hud_top_t; else if (g_hud_bottom_rows && miny >= float(272 - g_hud_bottom_rows)) tt = g_hud_bottom_t; } + if (!verts.empty()) { // frame interpolation: a 2D draw that follows a fighter moves with the blended 3D scene (psp_ge_draw.cpp) + extern bool ge_draw_through_ip(float cx, float cy, float* dx, float* dy); + float minx = 1e9f, maxx = -1e9f, miny = 1e9f, maxy = -1e9f; + for (const auto& v : verts) { minx = std::min(minx, v.pos[0]); maxx = std::max(maxx, v.pos[0]); miny = std::min(miny, v.pos[1]); maxy = std::max(maxy, v.pos[1]); } + float dx = 0.0f, dy = 0.0f; + if (ge_draw_through_ip(0.5f * (minx + maxx), 0.5f * (miny + maxy), &dx, &dy)) for (auto& v : verts) { v.pos[0] += dx; v.pos[1] += dy; } + } for (auto& v : verts) { v.pos[0] = (v.pos[0] / 240.0f - 1.0f) * wx; v.pos[1] = (1.0f - v.pos[1] / 136.0f) * ty + tt; diff --git a/psprecomp/runtime/src/psp_render_queue.cpp b/psprecomp/runtime/src/psp_render_queue.cpp index a10f2cd..ea1906f 100644 --- a/psprecomp/runtime/src/psp_render_queue.cpp +++ b/psprecomp/runtime/src/psp_render_queue.cpp @@ -111,6 +111,9 @@ int render_queue_enqueue_ge_list( entry.processed = false; entry.stalled = false; entry.begun = false; + entry.ip_tag = ge_interp_current_tag(); + static const bool iptrace2 = [] { const char* e = std::getenv("PSPRECOMP_INTERP_TRACE"); return e && std::atoi(e) >= 2; }(); + if (iptrace2 && entry.ip_tag.seq) std::fprintf(stderr, "[IPQ] enqueue uid %d list %08X stall %08X tag seq %u rec %d\n", uid, list_addr, stall_addr, entry.ip_tag.seq, entry.ip_tag.record ? 1 : 0); g_ge_queue.push_back(entry); @@ -252,6 +255,7 @@ void render_queue_process() { for (auto& entry : to_process) { psp_prof::seq("list-begin", entry.uid); + ge_interp_list_tag(entry.ip_tag); GeListResult result = ge_process_display_list( entry.rdram, entry.current_pc,