Triforce v0.3.2

The above-60 fixes (the pick-up counter, water ripples, sound, moving platforms, the time of day, cutscene cues) and the
RDNA 1 subgroup fix, ported from NotRexed/Hookshot: Jordan Ella, hckrman101, klimovich008, Bruno Baccari.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
ruckus7382sfgsgdf
2026-11-12 01:29:02 -06:00
parent 362d576c7d
commit afbb0d140a
12 changed files with 1157 additions and 19 deletions

View File

@@ -22,8 +22,8 @@ def main():
shader = (VK / "vk_shader.cpp").read_text(encoding="utf-8")
profile = shader[shader.index("Shader::Profile make_profile()"):shader.index("Shader::HostTranslateInfo host_info()")]
for needle, why in (
("dev.subgroup_size > 32 && (dev.sg32_stages & VK_SHADER_STAGE_ALL_GRAPHICS)",
"a graphics stage running wider than 32 lanes is not told to the translator"),
("p.warp_size_potentially_larger_than_guest = dev.subgroup_max > 32",
"an unpinned stage's maximum subgroup width is not told to the translator"),
("p.max_subgroup_size = p.warp_size_potentially_larger_than_guest ? dev.subgroup_max : 32",
"the broadcast fallbacks do not walk the host's upper lanes"),
("p.has_broken_spirv_fragment_shuffle = dev.driver_id == VK_DRIVER_ID_AMD_PROPRIETARY",

View File

@@ -0,0 +1,152 @@
"""Check 32- and 64-lane shader profiles and AMD's fragment shuffle path without a GPU."""
import argparse
import os
from pathlib import Path
import subprocess
import tempfile
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
VK = ROOT / "gpu/vk"
FIXTURE = r'''
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include "shader_recompiler/profile.h"
#include "shader_recompiler/stage.h"
enum VkShaderStageFlagBits : uint32_t {
VK_SHADER_STAGE_VERTEX_BIT = 1,
VK_SHADER_STAGE_FRAGMENT_BIT = 16,
VK_SHADER_STAGE_COMPUTE_BIT = 32,
VK_SHADER_STAGE_TASK_BIT_EXT = 64,
VK_SHADER_STAGE_MESH_BIT_EXT = 128,
VK_SHADER_STAGE_ALL_GRAPHICS = 31,
VK_SHADER_STAGE_ALL = 0x7fffffff,
};
using VkShaderStageFlags = uint32_t;
constexpr uint32_t VK_DRIVER_ID_AMD_PROPRIETARY = 1;
struct {
uint32_t sg32_stages = 0, subgroup_size = 32, subgroup_max = 32, driver_id = 0;
struct { struct { uint64_t minStorageBufferOffsetAlignment = 16; } limits; } props;
} dev;
using Shader::Stage;
using Id = uint32_t;
struct EmitContext {
const Shader::Profile& profile;
Stage stage;
Id lane;
Id U32[2]{};
Id Const(Id value) { return value; }
Id OpShiftRightLogical(Id, Id a, Id b) { return a >> b; }
Id OpShiftLeftLogical(Id, Id a, Id b) { return a << b; }
Id OpIAdd(Id, Id a, Id b) { return a + b; }
};
Id GetThreadId(const EmitContext& ctx) { return ctx.lane; }
'''
CASES = r'''
void check(const Shader::Profile& p, uint32_t size) {
assert(p.max_subgroup_size == size);
assert(p.warp_size_potentially_larger_than_guest == (size > 32));
}
int main(int argc, char** argv) {
assert(argc == 12);
const auto number = [&](int i) { return uint32_t(std::strtoul(argv[i], nullptr, 10)); };
dev.subgroup_size = number(1);
dev.subgroup_max = number(2);
dev.sg32_stages = number(3);
dev.driver_id = number(4);
check(profile(), number(5));
check(profile_graphics(VK_SHADER_STAGE_VERTEX_BIT), number(6));
check(profile_graphics(VK_SHADER_STAGE_FRAGMENT_BIT), number(7));
check(profile_pinned(), number(8));
check(profile_graphics(VK_SHADER_STAGE_TASK_BIT_EXT), number(9));
check(profile_graphics(VK_SHADER_STAGE_MESH_BIT_EXT), number(10));
const bool workaround = number(11) != 0;
check_stamp(number(5), number(3), workaround, getenv("RX_VK_WARP64") != nullptr);
const auto& fragment = profile_graphics(VK_SHADER_STAGE_FRAGMENT_BIT);
const auto& compute = profile_pinned();
EmitContext lo{fragment, Stage::Fragment, 31};
EmitContext hi{fragment, Stage::Fragment, 32};
EmitContext cs{compute, Stage::Compute, 31};
assert(UseShuffleFallback(lo) == workaround && UseShuffleFallback(hi) == workaround);
assert(!UseShuffleFallback(cs));
assert(HostThreadId(lo, 7) == 7 && HostThreadId(cs, 7) == 7);
assert(HostThreadId(hi, 7) == (number(7) > 32 ? 39 : 7));
}
'''
def section(source, start, end):
first = source.index(start)
return source[first:source.index(end, first)]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="clang++")
args = parser.parse_args()
shader = (VK / "vk_shader.cpp").read_text(encoding="utf-8")
warp = (VK / "eden/shader_recompiler/backend/spirv/emit_spirv_warp.cpp").read_text(encoding="utf-8")
stamp = section(shader, " const uint32_t lanes[]", " // (the records keep Shader::Info")
check_stamp = r'''
void check_stamp(uint32_t size, uint32_t pinned, bool workaround, bool forced) {
bool added = false;
const auto add = [&](const uint32_t* values, size_t bytes) {
assert(bytes == 5 * sizeof(uint32_t));
assert(values[0] == uint32_t(size > 32) && values[1] == size);
assert(values[2] == uint32_t(workaround) && values[3] == pinned && values[4] == uint32_t(forced));
added = true;
};
''' + stamp + r'''
assert(added);
}
'''
fixture = (FIXTURE + section(shader, "Shader::Profile make_profile(", "Shader::HostTranslateInfo host_info()")
+ section(shader, "const Shader::Profile& profile()", "const std::vector<Code>& codes_of(")
+ section(warp, "Id HostThreadId(", "Id GuestLane(")
+ section(warp, "bool UseShuffleFallback(", "Id ShuffleAbsolute(") + check_stamp + CASES)
with tempfile.TemporaryDirectory(prefix="hookshot-subgroup-profile-") as tmp:
cpp, exe = Path(tmp) / "fixture.cpp", Path(tmp) / "fixture.exe"
cpp.write_text(fixture, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++20", "-I", str(VK / "eden"), "-I", str(ROOT / "gpu/third_party/suyu"),
str(cpp), "-o", str(exe)], check=True, timeout=60)
# A fresh process per device: the real profiles are cached once for the selected GPU.
# Inputs: default lanes, maximum lanes, pinned-stage mask, proprietary AMD driver.
# Expected widths: base, vertex, fragment, compute, task, mesh; then fragment workaround.
cases = [
((32, 32, 0, 0), (32, 32, 32, 32, 32, 32, 0), {}),
((64, 64, 32, 1), (64, 64, 64, 32, 64, 64, 1), {}),
((32, 64, 32, 1), (64, 64, 64, 32, 64, 64, 1), {}),
((32, 64, 0, 0), (64, 64, 64, 64, 64, 64, 0), {}),
((32, 64, 31, 1), (64, 32, 32, 64, 64, 64, 1), {}),
((64, 64, 240, 1), (64, 64, 32, 32, 32, 32, 1), {}),
((64, 64, 0, 0), (64, 64, 64, 64, 64, 64, 0), {}),
((64, 64, 32, 1), (32, 32, 32, 32, 32, 32, 1), {"RX_VK_WARP64": "0"}),
((64, 64, 240, 1), (64, 64, 64, 32, 64, 64, 1), {"RX_VK_WARP64": "1"}),
((64, 64, 32, 1), (64, 64, 64, 32, 64, 64, 0), {"RX_VK_FRAG_SHUFFLE_FALLBACK": "0"}),
((64, 64, 32, 0), (64, 64, 64, 32, 64, 64, 1), {"RX_VK_FRAG_SHUFFLE_FALLBACK": "1"}),
]
env = {k: v for k, v in os.environ.items() if k not in ("RX_VK_WARP64", "RX_VK_FRAG_SHUFFLE_FALLBACK")}
for device, expected, overrides in cases:
subprocess.run([str(exe), *map(str, device + expected)], env=env | overrides, check=True, timeout=10)
assert "EmitSPIRV(profile_pinned(), rt, t->program, bindings)" in shader
for stage in ("TASK", "MESH"):
assert f"fallback ? profile_pinned() : profile_graphics(VK_SHADER_STAGE_{stage}_BIT_EXT)" in shader
for start, end in (("Id ShuffleRelative(", "Id BroadcastLane("), ("Id EmitShuffleIndex(", "Id EmitShuffleUp("),
("Id EmitShuffleButterfly(", "Id EmitQuadBroadcast(")):
assert "UseShuffleFallback(ctx)" in section(warp, start, end)
quad = section(warp, "Id EmitQuadBroadcast(", "Id EmitQuadSwap(")
assert "ctx.profile.has_broken_spirv_subgroup_shuffle" in quad and "UseShuffleFallback" not in quad
print("stage subgroup widths, 32-lane partitions, AMD fragment shuffles, and cache identity ok")
if __name__ == "__main__":
main()

120
tests/test_frame_whole.py Normal file
View File

@@ -0,0 +1,120 @@
"""Game counters that add the frame step, or the step times a whole number, cut to whole frames keep counting above 60 fps
(host/frame_whole.h, nx2/oot_patches.py patch_whole_frames).
The pick-up indicator above Link's head (f_80a87e50) moves the count it shows toward the wallet's by (int)(step * per) and fades
when they meet; the time of day (f_803edf70) adds (int)(2 * step) game seconds a frame. Under a Frame rate cap above 60 the step is
the frame's own time, under 1 (0.42 at 144 fps): cut to 0 every frame, the count never moved and the indicator stayed, and the
clock stood still. With the frames' own time
as the step (carry), hos_frame_whole adds the amount to the counter's own remainder and hands out its whole frames: 144 frames of
60/144 are 60; an amount of 1 or more keeps its fraction too; two counters keep their own remainders; NaN and amounts below zero
give 0. Without it, the amount is cut as the game cuts it (60 fps as before). The water renderer (f_800672e0) runs its ripple
simulation (int)step times a frame the same way, its remainder kept per water object: forty surfaces stepped in turn each still
count their 60 frames a second. The patch replaces the one cut of each site, once,
declares the hook, and leaves a file patched before and a function whose code differs as they are.
python -I -B tests/test_frame_whole.py
"""
import shutil
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "nx2"))
import oot_patches # noqa: E402
FIXTURE = r"""
#include "frame_whole.h"
#include <math.h>
#include <stdio.h>
static int fails;
#define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0)
static uint32_t bits(float f) { uint32_t b; memcpy(&b, &f, 4); return b; }
int main(void) {
static FrameWhole w;
const uint64_t a = 0x1000, b = 0x2000;
uint32_t n = 0;
for (int i = 0; i < 144; i++) n += frame_whole(&w, a, bits(60.0f / 144.0f), 1);
CHECK(n == 60 || n == 59); /* (a second at 144 fps: 60 frames, give or take the float's last one) */
n = 0;
for (int i = 0; i < 1440; i++) n += frame_whole(&w, a, bits(60.0f / 144.0f), 1);
CHECK(n >= 599 && n <= 601);
CHECK(frame_whole(&w, b, bits(0.42f), 1) == 0 && frame_whole(&w, b, bits(0.42f), 1) == 0 && frame_whole(&w, b, bits(0.42f), 1) == 1);
n = 0;
for (int i = 0; i < 4; i++) n += frame_whole(&w, b + 8, bits(1.25f), 1); /* (the step times 3: its fractions kept) */
CHECK(n == 5);
n = 0;
for (int i = 0; i < 144; i++) n += frame_whole(&w, b + 16, bits(2.0f * 60.0f / 144.0f), 1); /* (the time of day) */
CHECK(n == 120 || n == 119); /* (a second at 144 fps: the 120 game seconds of one at 60) */
CHECK(frame_whole(&w, a, bits(0.42f), 0) == 0 && frame_whole(&w, a, bits(1.5f), 0) == 1 && frame_whole(&w, a, bits(2.0f), 0) == 2);
CHECK(frame_whole(&w, a, bits(-1.0f), 1) == 0 && frame_whole(&w, a, bits(NAN), 1) == 0 && frame_whole(&w, a, bits(0.0f), 1) == 0);
for (uint64_t k = 0; k < 100; k++) frame_whole(&w, 0x9000 + k, bits(0.5f), 1); /* (more counters than slots: the oldest go) */
CHECK(frame_whole(&w, 0x9000 + 99, bits(0.5f), 1) == 1); /* (a slot still held: 0.5 + 0.5) */
CHECK(frame_whole(&w, 0x9000, bits(0.5f), 1) == 0); /* (the oldest's slot was reused: it starts again from 0) */
/* (a scene's water surfaces, each stepped once a frame at 144 fps: 40 of them, more than the old 16 slots, each still counts
its 60 frames a second) */
{
static FrameWhole water;
uint32_t passes[40] = {0};
for (int f = 0; f < 144; f++)
for (int k = 0; k < 40; k++) passes[k] += frame_whole(&water, 0x7000000 + (uint64_t)k * 0x400, bits(60.0f / 144.0f), 1);
for (int k = 0; k < 40; k++) CHECK(passes[k] == 60 || passes[k] == 59);
}
if (!fails) printf("PASS\n");
return fails != 0;
}
"""
LIFTED = ['#include "a64.h"\n' + head + " c->x[9] = recomp_load32(c,(c->x[19] + 864));\n" + old + "return;\n}\n"
for head, old, _ in oot_patches.WHOLE_SITES]
class FrameWholeTests(unittest.TestCase):
def test_counter_keeps_counting_under_one(self):
compiler = shutil.which("gcc") or shutil.which("clang")
if not compiler:
self.skipTest("no C compiler")
with tempfile.TemporaryDirectory(prefix="hookshot-frame-whole-") as tmp:
tmp = Path(tmp)
(tmp / "t.c").write_text(FIXTURE, encoding="utf-8")
binary = tmp / "t.exe"
build = subprocess.run([compiler, "-std=gnu11", "-Wall", "-O1", "-I", str(ROOT / "host"), str(tmp / "t.c"),
"-o", str(binary), "-lm"], text=True, capture_output=True)
self.assertEqual(build.returncode, 0, build.stderr)
run = subprocess.run([str(binary)], text=True, capture_output=True, timeout=30)
self.assertEqual(run.returncode, 0, run.stdout + run.stderr)
def test_patch_replaces_the_cut_once(self):
for lifted, (_, old, key) in zip(LIFTED, oot_patches.WHOLE_SITES):
out, n = oot_patches.patch_whole_frames(lifted)
self.assertEqual(n, 1)
self.assertEqual(out.count("=hos_frame_whole(%s, (uint32_t)c->vreg[0][0]);" % key), 1)
self.assertNotIn("recomp_fp_to_int", out)
self.assertIn(oot_patches.WHOLE_DECL, out)
self.assertEqual(oot_patches.patch_whole_frames(out), (out, 0)) # (patched before: as it is)
other = lifted.replace("32,32,", "32,31,") # (another game version's code)
self.assertEqual(oot_patches.patch_whole_frames(other), (other, 0))
none = lifted.replace("void f_8", "void f_9")
self.assertEqual(oot_patches.patch_whole_frames(none), (none, 0))
# (a lift where the function is hot: its float helpers are a64_fpx_hot_*, the cut the same)
_, old0, _ = oot_patches.WHOLE_SITES[0]
hot = LIFTED[0].replace(old0, " _v=a64_fpx_hot_mul32_fma32(c,_a,_b,_z);\n a64_vset64(c->vreg[0],_v);}\n" + old0)
self.assertEqual(oot_patches.patch_whole_frames(hot)[1], 1)
def test_clock_patches_its_seconds_alone(self):
# (f_803edf70 cuts into x8 the same way once more, in the branch that sets the time from another source: that cut
# stays the game's)
i = next(i for i, (h, _, _) in enumerate(oot_patches.WHOLE_SITES) if "f_803edf70" in h)
cut = " c->x[8]=recomp_fp_to_int(c->vreg[0][0]&UINT64_C(0xffffffff),32,32,1,3,c->fpcr,&c->fpsr);\n"
lifted = LIFTED[i].replace("return;\n", cut + " recomp_store32(c,c->x[19] + 12,c->x[8]);\nreturn;\n")
out, n = oot_patches.patch_whole_frames(lifted)
self.assertEqual(n, 1)
self.assertIn("=hos_frame_whole(c->x[19] + 60, (uint32_t)c->vreg[0][0]);", out)
self.assertEqual(out.count(cut), 1)
self.assertIn(cut + " recomp_store32(c,c->x[19] + 12,", out)
if __name__ == "__main__":
raise SystemExit(unittest.main(verbosity=1))

View File

@@ -0,0 +1,172 @@
"""Above a 60 fps cap the sound job runs at 60 Hz and the physics solver keeps whole-frame steps (host/oot_high_fps.h,
nx2/oot_patches.py patch_sound_job and patch_solver_steps).
Sound: under a Frame rate cap above 60 the frame step is the frame's own time (0.5 at 120 fps). The sound manager's job (f_80435300)
fed steps under 1 left sounds unreleased at area changes until the game's voice budget was full and sounds, then the music,
stopped. The gate runs phase 0 once a whole step has built up (120 frames of 0.5: 60 runs; 144 of 60/144: 60), the other phases
follow phase 0's choice, the engine is handed a whole step then, and a step of 1 or more, 0 or below, NaN, or no frame time at
all (a cap of 60) runs every frame with the game's own step. A step of 1 or more keeps the remainder, so around 60 fps (0.98 and
1.02 alternating, 0.9 and 1.1, or mean 1 with jitter) the sound clock keeps the real one; a cap of 60 runs every phase, before
the first phase 0 too and after a phase 0 that held the job.
Solver: the game cuts frame step * n + 0.001 to its solver steps (n a 60 fps frame: 8 at 60 fps, 3 or 4 at 120 as the step
wanders; with n = 4, 0 above 240). With the frames' own time and a step under 1 it is rounded up (10% leeway for the frame time's
jitter) and at least 1, never under the game's; a step of 1 or more, or no frame time (a cap of 60), keeps the game's count.
The patches put the gate at the job's head and the step through the host, hook both solver places, declare the hooks, and leave
a file patched before, or whose code differs, as it is.
python -I -B tests/test_high_fps_fixes.py
"""
import shutil
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "nx2"))
import oot_patches # noqa: E402
FIXTURE = r"""
#include "oot_high_fps.h"
#include <stdio.h>
static int fails;
#define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0)
static uint32_t bits(float f) { return oot_float_bits(f); }
static int runs(float step, int frames, int timed) {
OotSoundGate g = OOT_SOUND_GATE_INIT;
int n = 0;
for (int i = 0; i < frames; i++) {
const int run = oot_sound_gate(&g, 0, step, timed);
CHECK(oot_sound_gate(&g, 1, step, timed) == run); /* (phase 1 follows phase 0) */
n += run;
}
return n;
}
/* (the sound engine's time over these frames' steps against the real time: what it is handed each frame the job runs) */
static double sound_clock(const float *steps, int n) {
OotSoundGate g = OOT_SOUND_GATE_INIT;
double real = 0, sound = 0;
for (int i = 0; i < n; i++) {
real += steps[i];
if (oot_sound_gate(&g, 0, steps[i], 1)) sound += oot_bits_float(oot_sound_step(oot_float_bits(steps[i]), 1));
}
return sound / real;
}
int main(void) {
CHECK(runs(0.5f, 120, 1) == 60);
const int n144 = runs(60.0f / 144.0f, 144, 1);
CHECK(n144 == 60 || n144 == 59);
CHECK(runs(60.0f / 1000.0f, 1000, 1) >= 59 && runs(60.0f / 1000.0f, 1000, 1) <= 60);
CHECK(runs(0.5f, 120, 0) == 120); /* (a cap of 60: every frame) */
CHECK(runs(1.0f, 60, 1) == 60 && runs(1.5f, 60, 1) == 60);
CHECK(runs(0.0f, 60, 1) == 60 && runs(-1.0f, 60, 1) == 60 && runs(NAN, 60, 1) == 60);
{ /* (a whole step after partial ones runs with the game's step and keeps the remainder) */
OotSoundGate g = OOT_SOUND_GATE_INIT;
CHECK(oot_sound_gate(&g, 0, 0.75f, 1) == 0 && oot_sound_gate(&g, 2, 0.75f, 1) == 0);
CHECK(oot_sound_gate(&g, 0, 1.0f, 1) == 1 && g.part == 0.75f && oot_sound_gate(&g, 1, 1.0f, 1) == 1);
CHECK(oot_sound_gate(&g, 0, 0.75f, 1) == 1 && g.part == 0.5f);
CHECK(oot_sound_gate(&g, 0, 0.0f, 1) == 1 && g.part == 0.0f); /* (a step of 0: as the game has it, the remainder cleared) */
}
{ /* (around 60 fps the sound clock keeps the real one: dropping the remainder at each whole step made it 0.51 of it) */
static float steps[20000];
for (int i = 0; i < 20000; i++) steps[i] = i & 1 ? 1.02f : 0.98f;
double r = sound_clock(steps, 20000);
CHECK(r > 0.999 && r < 1.001);
for (int i = 0; i < 20000; i++) steps[i] = i & 1 ? 1.1f : 0.9f;
r = sound_clock(steps, 20000);
CHECK(r > 0.999 && r < 1.001);
uint32_t x = 12345; /* (mean 1, about 0.03 apart: a sum of uniforms) */
for (int i = 0; i < 20000; i++) {
float u = 0;
for (int k = 0; k < 12; k++) x = x * 1664525u + 1013904223u, u += (float)(x >> 8) / 16777216.0f;
steps[i] = 1.0f + (u - 6.0f) * 0.03f;
}
r = sound_clock(steps, 20000);
CHECK(r > 0.999 && r < 1.001);
for (int i = 0; i < 20000; i++) steps[i] = 0.5f;
r = sound_clock(steps, 20000);
CHECK(r > 0.999 && r < 1.001);
}
{ /* (a cap of 60 runs every phase: before any phase 0, and after a phase 0 that held the job at a cap above 60) */
OotSoundGate g = OOT_SOUND_GATE_INIT;
CHECK(oot_sound_gate(&g, 1, 1.0f, 0) == 1 && oot_sound_gate(&g, 2, 1.0f, 0) == 1);
OotSoundGate h = OOT_SOUND_GATE_INIT;
CHECK(oot_sound_gate(&h, 1, 1.0f, 1) == 1); /* (above 60, before the first phase 0: it runs) */
CHECK(oot_sound_gate(&h, 0, 0.5f, 1) == 0 && oot_sound_gate(&h, 1, 0.5f, 0) == 1 && h.part == 0.0f); /* (the cap changed) */
CHECK(oot_sound_gate(&h, 2, 0.5f, 0) == 1 && oot_sound_gate(&h, 0, 0.5f, 0) == 1);
}
CHECK(oot_sound_step(bits(0.5f), 1) == bits(1.0f));
CHECK(oot_sound_step(bits(0.5f), 0) == bits(0.5f));
CHECK(oot_sound_step(bits(1.25f), 1) == bits(1.25f) && oot_sound_step(bits(0.0f), 1) == bits(0.0f));
/* (the game's: (int)(step * n + 0.001), n the steps of a 60 fps frame) */
#define GAME(step, n) ((int32_t)((step) * (n) + 0.001f))
#define OURS(step, n, timed) oot_solver_steps(GAME(step, n), bits((step) * (n) + 0.001f), bits(n), timed)
CHECK(OURS(1.0f, 4.0f, 1) == 4 && OURS(1.0f, 8.0f, 1) == 8 && OURS(1.0f, 8.0f, 0) == 8);
CHECK(OURS(0.5f, 4.0f, 1) == 2 && OURS(0.49f, 4.0f, 1) == 2 && OURS(0.52f, 4.0f, 1) == 2 && GAME(0.49f, 4.0f) == 1);
CHECK(OURS(0.49f, 8.0f, 1) == 4 && OURS(0.5f, 8.0f, 1) == 4 && GAME(0.49f, 8.0f) == 3);
CHECK(OURS(0.25f, 4.0f, 1) == 1 && OURS(0.24f, 4.0f, 1) == 1 && OURS(0.06f, 4.0f, 1) == 1 && GAME(0.24f, 4.0f) == 0);
CHECK(OURS(60.0f / 144.0f, 4.0f, 1) == 2 && OURS(60.0f / 90.0f, 4.0f, 1) == 3);
CHECK(OURS(0.49f, 4.0f, 0) == 1 && OURS(0.06f, 4.0f, 0) == 0); /* (a cap of 60: the game's count) */
CHECK(OURS(1.6f, 4.0f, 1) == 6 && OURS(1.708f, 8.0f, 1) == 13); /* (a slow frame: the game's count) */
CHECK(oot_solver_steps(3, bits(NAN), bits(4.0f), 1) == 3 && oot_solver_steps(0, bits(-1.0f), bits(4.0f), 1) == 0);
CHECK(oot_solver_steps(1, bits(2.0f), bits(0.0f), 1) == 1); /* (no steps a frame: the game's) */
if (!fails) printf("PASS\n");
return fails != 0;
}
"""
LIFTED_SOUND = ('#include "a64.h"\n' + oot_patches.SOUND_HEAD + " /* nop/hint */\nif ((uint32_t)c->x[2] != 0) { goto L_1; }\n"
+ oot_patches.SOUND_OLD + "L_1:;\n return;\n}\nvoid f_80435360(Ctx *__restrict A64_NOESC c) {\n"
+ oot_patches.SOUND_OLD + "}\n")
LIFTED_SOLVER = '#include "a64.h"\n' + "".join(head + " c->x[8]=1;\n" + oot_patches.SOLVER_OLD + "}\n"
for head in oot_patches.SOLVER_HEADS)
class HighFpsTests(unittest.TestCase):
def test_gate_and_steps(self):
compiler = shutil.which("gcc") or shutil.which("clang")
if not compiler:
self.skipTest("no C compiler")
with tempfile.TemporaryDirectory(prefix="hookshot-high-fps-") as tmp:
tmp = Path(tmp)
(tmp / "t.c").write_text(FIXTURE, encoding="utf-8")
binary = tmp / "t.exe"
build = subprocess.run([compiler, "-std=gnu11", "-Wall", "-O1", "-I", str(ROOT / "host"), str(tmp / "t.c"),
"-o", str(binary), "-lm"], text=True, capture_output=True)
self.assertEqual(build.returncode, 0, build.stderr)
run = subprocess.run([str(binary)], text=True, capture_output=True, timeout=30)
self.assertEqual(run.returncode, 0, run.stdout + run.stderr)
def test_sound_patch(self):
out, n = oot_patches.patch_sound_job(LIFTED_SOUND)
self.assertEqual(n, 1)
head = out.index(oot_patches.SOUND_HEAD) + len(oot_patches.SOUND_HEAD)
self.assertTrue(out[head:].startswith(oot_patches.SOUND_GATE)) # (before the job's own first instruction)
self.assertEqual(out.count("hos_oot_sound_gate(c,"), 1)
self.assertEqual(out.count(oot_patches.SOUND_NEW), 1) # (the job's own load only, not the next function's)
self.assertEqual(out.count(oot_patches.SOUND_OLD), 1)
self.assertIn(oot_patches.SOUND_DECL, out)
self.assertEqual(oot_patches.patch_sound_job(out), (out, 1)) # (patched before: as it is)
other = LIFTED_SOUND.replace("+ 2192)", "+ 2200)") # (another game version's code)
self.assertEqual(oot_patches.patch_sound_job(other), (other, 0))
none = LIFTED_SOUND.replace("void f_80435300", "void f_90435300")
self.assertEqual(oot_patches.patch_sound_job(none), (none, 0))
def test_solver_patch(self):
out, n = oot_patches.patch_solver_steps(LIFTED_SOLVER)
self.assertEqual(n, 2)
self.assertEqual(out.count(oot_patches.SOLVER_NEW), 2)
self.assertIn(oot_patches.SOLVER_DECL, out)
hook = out.index("hos_oot_solver_steps((uint32_t)c->x[1]")
self.assertLess(out.index("recomp_fp_to_int"), hook)
self.assertLess(hook, out.index("recomp_store32(c,c->x[0] + 712"))
self.assertEqual(oot_patches.patch_solver_steps(out), (out, 2))
other = LIFTED_SOLVER.replace("+ 712", "+ 716")
self.assertEqual(oot_patches.patch_solver_steps(other), (other, 0))
if __name__ == "__main__":
raise SystemExit(unittest.main(verbosity=1))

View File

@@ -0,0 +1,236 @@
"""Above a 60 cap a movie's frame count grows by the frames' time, so a cutscene's cues stay with its movie (issue 1742).
The game's movie player counts its updates while a movie plays, and a cutscene that shows a movie places its narration, its
rumble and its end by that count. The movie's picture and sound follow the clock, so at 120 fps the cues came twice too early:
narration lines started over each other. nx2/oot_patches.py makes the count's step hos_movie_frames(player), and host/nn_vi.c
answers 1 while the host gives no frame time (a cap of 60: as shipped) and the frames' time in 60ths, the rest carried to the
player's next update, while it does (HOS_FPS_MOVIE=0: 1 then too, for comparisons). A game version whose code differs keeps
its own code and the build goes on with a warning.
python -I -B tests/test_movie_frame_count.py [--cc gcc]
"""
import argparse
import contextlib
import io
import os
import re
import subprocess
import sys
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cc", "gcc"]
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "nx2"))
import oot_patches # noqa: E402
# (the shape of the lifted update: the count loaded, one added, stored; the same lines in another function stay as they are)
STORE = " recomp_store32(c,c->x[20] + 760,c->x[8]);\n"
LIFTED = ('#include "a64.h"\n'
"void f_83b7c710(Ctx *__restrict A64_NOESC c) {\n"
" c->x[8] = 0x1ULL;\n" + oot_patches.MOVIE_OLD + STORE + "}\n"
"void f_83b7c870(Ctx *__restrict A64_NOESC c) {\n" + oot_patches.MOVIE_OLD + STORE + "}\n")
HOST_ANCHOR = "uint32_t hos_movie_frames(uint64_t player) {"
def block(source, marker):
if marker not in source:
raise SystemExit("FAIL: host/nn_vi.c has no '%s'" % marker)
start = source.index(marker)
return source[start:start + re.search(r"^}", source[start:], re.M).end()]
def check(cond, what):
if not cond:
raise SystemExit("FAIL: " + what)
FIXTURE = r'''#define _CRT_SECURE_NO_WARNINGS
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CHECK(x) do { if (!(x)) { fprintf(stderr, "FAIL:%d %s\n", __LINE__, #x); exit(1); } } while (0)
/* (the lock as a flag: taken once, given back once) */
typedef int SRWLOCK;
#define SRWLOCK_INIT 0
static void AcquireSRWLockExclusive(SRWLOCK *l) { CHECK(!*l); *l = 1; }
static void ReleaseSRWLockExclusive(SRWLOCK *l) { CHECK(*l); *l = 0; }
volatile uint32_t hos_frame_seconds_bits;
@HOST@
/* (the lifted update with the patch, on a small guest: the count at +760 of the player) */
typedef struct { uint64_t x[31]; } Ctx;
static uint32_t guest[2][256];
static uint32_t recomp_load32(Ctx *c, uint64_t a) { (void)c; return guest[a >> 12][(a & 0xfff) / 4]; }
static void recomp_store32(Ctx *c, uint64_t a, uint64_t v) { (void)c; guest[a >> 12][(a & 0xfff) / 4] = (uint32_t)v; }
static uint32_t update(int player) {
Ctx ctx, *c = &ctx;
memset(&ctx, 0, sizeof ctx);
c->x[20] = (uint64_t)player << 12;
@LIFTED@
return guest[player][760 / 4];
}
static void seconds(double s) {
const float f = (float)s;
uint32_t bits;
memcpy(&bits, &f, 4);
hos_frame_seconds_bits = s > 0 ? bits : 0;
}
static int within(uint32_t got, uint32_t want) { return got + 1 >= want && got <= want + 1; }
int main(int argc, char **argv) {
uint32_t n = 0, before;
if (argc > 1 && !strcmp(argv[1], "off")) { /* (HOS_FPS_MOVIE=0: the game's own count at any rate) */
seconds(1.0 / 120);
for (int i = 0; i < 600; i++) n = update(0);
CHECK(n == 600);
printf("PASS: off\n");
return 0;
}
/* no frame time (a cap of 60): one an update, as the game counts */
seconds(0);
for (int i = 0; i < 600; i++) n = update(0);
CHECK(n == 600);
CHECK(hos_movie_frames(0x5000) == 1 && hos_movie_frames(0x5000) == 1);
/* 120 fps: half a 60th an update, a whole one every second update */
seconds(1.0 / 120);
before = n;
for (int i = 0; i < 600; i++) {
const uint32_t was = n;
n = update(0);
CHECK(n - was <= 1);
}
CHECK(within(n - before, 300));
/* 240 fps and 40 fps: a quarter and one and a half */
seconds(1.0 / 240);
before = n;
for (int i = 0; i < 960; i++) n = update(0);
CHECK(within(n - before, 240));
seconds(1.0 / 40);
before = n;
for (int i = 0; i < 400; i++) {
const uint32_t was = n;
n = update(0);
CHECK(n - was == 1 || n - was == 2);
}
CHECK(within(n - before, 600));
/* two movies at once: each its own count (one carry for both would give each half) */
seconds(1.0 / 120);
before = n;
{
const uint32_t other = update(1) - 0; /* (its first update) */
uint32_t m = other;
for (int i = 0; i < 600; i++) n = update(0), m = update(1);
CHECK(within(n - before, 300) && within(m - other, 300));
}
/* a movie that plays while others come and go keeps its count: 14 more that play with it and a new player every update,
16 at a time, at 240 fps (places taken in the order they were made gave this one a new start every second update: a
quarter of a 60th an update then counted 0 or 1 every two, 1200 only on average) */
seconds(1.0 / 240);
{
uint32_t mine = 0, steady = 0;
for (int i = 0; i < 4800; i++) {
mine += hos_movie_frames(0x7000000);
for (int k = 0; k < 14; k++) steady += hos_movie_frames(0x7100000 + (uint64_t)k * 0x1000);
hos_movie_frames(0x8000000 + (uint64_t)i * 0x1000);
}
CHECK(within(mine, 1200) && steady + 14 >= 14 * 1200 && steady <= 14 * 1200 + 14);
}
seconds(1.0 / 120);
/* more movies at once than the host keeps places for, each updated in turn: every update finds its player's place taken and
starts from a drawn part of a 60th, so 480 updates of half a 60th give 240 on average, 11 either way as one standard
deviation. Every count within 5 of those, for any number of players: with a new place started at 0 every count stood
still, and with a fixed step between the starts one player's did among 233 (and 29 players gave 108 to 131 of 120) */
{
static const int counts[] = {9, 17, 29, 40, 233, 377};
static uint32_t sum[377];
uint32_t least = ~0u, most = 0;
for (unsigned t = 0; t < sizeof counts / sizeof counts[0]; t++) {
const int players = counts[t];
memset(sum, 0, sizeof sum);
for (int i = 0; i < 480; i++)
for (int k = 0; k < players; k++) sum[k] += hos_movie_frames(0x100000 + (uint64_t)t * 0x1000000 + (uint64_t)k * 0x1000);
for (int k = 0; k < players; k++) {
CHECK(sum[k] >= 240 - 55 && sum[k] <= 240 + 55);
least = sum[k] < least ? sum[k] : least, most = sum[k] > most ? sum[k] : most;
}
}
printf("more players than places: %u to %u of 240\n", least, most);
}
before = n;
for (int i = 0; i < 600; i++) n = update(0);
CHECK(within(n - before, 300));
/* back at a cap of 60: one an update again */
seconds(0);
before = n;
for (int i = 0; i < 10; i++) n = update(0);
CHECK(n - before == 10);
printf("PASS\n");
return 0;
}
'''
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("--cc", default="gcc")
a = p.parse_args()
out = oot_patches.patch_movie_frames(LIFTED)
check(out is not None and out.count("_b=hos_movie_frames(c->x[20])") == 1, "the count's step is not the host's, once")
update = out[out.index("void f_83b7c710"):out.index("void f_83b7c870")]
check("hos_movie_frames" in update and "hos_movie_frames" not in out[out.index("void f_83b7c870"):],
"the same lines in another function were patched")
check(out.startswith('#include "a64.h"\n' + oot_patches.MOVIE_DECL), "the lifted file does not declare the host's function")
back = out.replace(oot_patches.MOVIE_DECL, "", 1).replace(oot_patches.MOVIE_NEW, oot_patches.MOVIE_OLD)
check(back == LIFTED, "more than the count's step changed")
check(oot_patches.patch_movie_frames(out) == out, "a second run changed a patched file")
check(oot_patches.patch_movie_frames(LIFTED.replace("f_83b7c710", "f_83b7c714")) is None,
"a file without the function was not left to the caller")
for what, other in (("another step", LIFTED.replace("_b=1ULL", "_b=2ULL")),
("another field", LIFTED.replace("+ 760", "+ 764")),
("the lines twice", LIFTED.replace(STORE + "}\nvoid f_83b7c870", STORE + oot_patches.MOVIE_OLD + STORE + "}\nvoid f_83b7c870"))):
check(oot_patches.patch_movie_frames(other) == other, "a function with %s was patched (another game version)" % what)
with tempfile.TemporaryDirectory(prefix="movie-count-") as tmp:
f = Path(tmp) / "code_000.c"
f.write_text(LIFTED, encoding="utf-8", newline="")
for run in ("first", "second"): # (the second over the patched file: counted as made, nothing written)
log = io.StringIO()
with contextlib.redirect_stdout(log):
oot_patches.apply(tmp)
check("WARNING: the movie's frame count" not in log.getvalue(), "the %s run warns though the count is patched" % run)
check(f.read_text(encoding="utf-8") == out, "the %s run did not leave the patched file" % run)
other = LIFTED.replace("_b=1ULL", "_b=2ULL")
f.write_text(other, encoding="utf-8", newline="")
log = io.StringIO()
with contextlib.redirect_stdout(log):
oot_patches.apply(tmp)
check("WARNING: the movie's frame count was found 0 times" in log.getvalue(), "no warning for a count that was not found")
check(f.read_text(encoding="utf-8") == other, "another version's function was changed")
vi = (ROOT / "host" / "nn_vi.c").read_text(encoding="utf-8")
lifted = oot_patches.MOVIE_NEW + STORE
fixture = FIXTURE.replace("@HOST@", block(vi, HOST_ANCHOR)).replace("@LIFTED@", lifted)
with tempfile.TemporaryDirectory(prefix="movie-count-") as tmp:
c = Path(tmp) / "fixture.c"
exe = Path(tmp) / "fixture.exe"
c.write_text(fixture, encoding="utf-8")
subprocess.run([a.cc, "-std=c11", "-Wall", "-Wextra", "-Werror", str(c), "-o", str(exe)], check=True)
env = {k: v for k, v in os.environ.items() if not k.startswith("HOS_")}
subprocess.run([str(exe)], env=env, check=True)
subprocess.run([str(exe)], env=dict(env, HOS_FPS_MOVIE="1"), check=True) # (read by its value: 1 is on)
subprocess.run([str(exe), "off"], env=dict(env, HOS_FPS_MOVIE="0"), check=True)
r = subprocess.run([str(exe), "off"], env=env, capture_output=True, text=True)
check(r.returncode != 0 and "FAIL:" in r.stderr, "the count is the game's own without HOS_FPS_MOVIE=0")
# (the negative control: the game's own step of one an update fails the same checks at 120 fps)
plain = Path(tmp) / "plain.c"
plain.write_text(FIXTURE.replace("@HOST@", block(vi, HOST_ANCHOR)).replace("@LIFTED@", oot_patches.MOVIE_OLD + STORE),
encoding="utf-8")
subprocess.run([a.cc, "-std=c11", "-Wall", "-Wextra", "-Werror", "-Wno-unused-function", str(plain), "-o", str(exe)], check=True)
r = subprocess.run([str(exe)], env=env, capture_output=True, text=True)
check(r.returncode != 0 and "FAIL:" in r.stderr, "the unpatched count passes the checks: they prove nothing")
if __name__ == "__main__":
main()