Triforce v0.4.0

What was merged from NX2navi (DLSS frame generation, AMD FSR 4 in Setup builds, texture detail by video memory, sprint, volume, frame caps, safer saves, the above-60 fixes), the ultrawide HUD at the screen edges, camera speed, movies without the AV1 Video Extension, the 5120x2880 + RenoDX HDR start fix, the renderer work of 11-14 and the first float changes of the lift.
This commit is contained in:
ruckus7382sfgsgdf
2026-11-15 06:39:00 -06:00
parent 183b05d8c4
commit 93eeb714c2
87 changed files with 5947 additions and 153 deletions

235
tests/fpx_hot_test.c Normal file
View File

@@ -0,0 +1,235 @@
/* tests/test_fpx_hot.py's driver: the hot functions' float helpers (a64_fpx_hot_*: the fast path inline, the exact path
called without the context) and the four-lane helpers lifted code calls (a64_vfpx_add32 / sub32 / mul32, a64_vfpx_fma32)
against the exact bodies (recomp_fpe_*), lane by lane in order: the value, FPSR and the rest of the context.
Built against the real a64.h with the a64_suyu.h / a64_suyu.c / a64_fpx.h the generators write, at the build's FP flags,
at -march=x86-64-v3 (the binary32 FMA by the host's instruction) and -march=x86-64-v2 (its binary64 form; the four-lane
multiply-add lane by lane).
fpx_hot_test <n> <seed> n random operand sets for every helper over random FPCR / FPSR states
Exit 0: no mismatch. 1: a mismatch (the first few are printed). 3: the CPU lacks this build's -march. */
#include "a64.h"
#include <stdio.h>
#include <stdlib.h>
#include <cpuid.h>
static uint64_t rs;
static uint64_t rnd(void) { rs ^= rs << 13; rs ^= rs >> 7; rs ^= rs << 17; return rs; }
static uint32_t r32(void) { return (uint32_t)(rnd() >> 16); }
static uint32_t rbelow(uint32_t n) { return (uint32_t)(((rnd() >> 32) * n) >> 32); }
static uint32_t mkf(uint32_t sign, int e, uint32_t frac) { return (sign << 31) | ((uint32_t)e << 23) | (frac & 0x7fffff); }
static uint64_t mkd(uint64_t sign, int e, uint64_t frac) { return (sign << 63) | ((uint64_t)e << 52) | (frac & UINT64_C(0xfffffffffffff)); }
static float F(uint32_t x) { float f; memcpy(&f, &x, 4); return f; }
static uint32_t B(float f) { uint32_t x; memcpy(&x, &f, 4); return x; }
static double D(uint64_t x) { double f; memcpy(&f, &x, 8); return f; }
static uint64_t BD(double f) { uint64_t x; memcpy(&x, &f, 8); return x; }
/* ---- operand classes and states (as tests/fpx_sticky_test.c's) ---- */
static uint32_t sparse_frac(void) {
switch (rbelow(4)) {
case 0: return 0;
case 1: return (uint32_t)1 << rbelow(23);
case 2: return r32() & 0x7ff000;
default: return r32() & 0x7fffff;
}
}
static uint32_t any32(void) {
const uint32_t s = r32() & 1;
switch (rbelow(16)) {
case 0: return s << 31;
case 1: return mkf(s, 0, r32() | 1);
case 2: return mkf(s, 0, rbelow(8) + 1);
case 3: return mkf(s, 0, 0x7fffff - rbelow(8));
case 4: return mkf(s, 1 + rbelow(3), sparse_frac());
case 5: return mkf(s, 254 - rbelow(3), sparse_frac());
case 6: return mkf(s, 255, 0);
case 7: return mkf(s, 255, (r32() | 1) & 0x7fffff);
case 8: return mkf(s, 255, 0x400000 | (r32() & 0x3fffff));
case 9: case 10: return mkf(s, 120 + rbelow(15), sparse_frac());
default: return mkf(s, 1 + rbelow(254), r32());
}
}
static uint64_t any64(void) {
const uint64_t s = r32() & 1;
const uint64_t fr = rnd() & UINT64_C(0xfffffffffffff), sp = rbelow(3) ? fr : (rbelow(2) ? 0 : UINT64_C(1) << rbelow(52));
switch (rbelow(16)) {
case 0: return s << 63;
case 1: return mkd(s, 0, fr | 1);
case 2: return mkd(s, 0, rbelow(8) + 1);
case 3: return mkd(s, 0, UINT64_C(0xfffffffffffff) - rbelow(8));
case 4: return mkd(s, 1 + (int)rbelow(3), sp);
case 5: return mkd(s, 2046 - (int)rbelow(3), sp);
case 6: return mkd(s, 2047, 0);
case 7: return mkd(s, 2047, fr | 1);
case 8: return mkd(s, 2047, UINT64_C(0x8000000000000) | fr);
case 9: case 10: return mkd(s, 1016 + (int)rbelow(15), sp);
default: return mkd(s, 1 + (int)rbelow(2046), fr);
}
}
static void edge_pair32(uint32_t *a, uint32_t *b) { /* a product near the bottom of the normal range or the overflow edge */
const int target = (rbelow(8) == 0) ? 255 - (int)rbelow(3) : (int)rbelow(60) - 30;
int ea = 1 + (int)rbelow(253);
int eb = target - ea + 127;
if (eb < 1 || eb > 254) { eb = 1 + (int)rbelow(254); ea = target - eb + 127; }
if (ea < 1) ea = rbelow(4) ? 1 : 0;
if (ea > 254) ea = 254;
*a = mkf(r32() & 1, ea, rbelow(2) ? sparse_frac() : r32());
*b = mkf(r32() & 1, eb, rbelow(2) ? sparse_frac() : r32());
}
static uint32_t addend32(uint32_t a, uint32_t b) {
const double p = (double)F(a) * (double)F(b);
switch (rbelow(10)) {
case 0: return any32();
case 1: return B((float)-p);
case 2: return B((float)-p) + (uint32_t)((int)rbelow(9) - 4);
case 3: return r32() & 0x80000000u;
case 4: return B((float)p) ^ (rbelow(2) ? 0x80000000u : 0);
case 5: return mkf(r32() & 1, 0, r32());
case 6: return mkf(r32() & 1, 1 + rbelow(4), r32());
default: return mkf(r32() & 1, 1 + rbelow(254), r32());
}
}
static uint64_t addend64(uint64_t a, uint64_t b) {
const double p = D(a) * D(b);
switch (rbelow(8)) {
case 0: return any64();
case 1: return BD(-p);
case 2: return BD(-p) + (uint64_t)((int)rbelow(9) - 4);
case 3: return (uint64_t)(r32() & 1) << 63;
default: return mkd(r32() & 1, 1 + (int)rbelow(2046), rnd());
}
}
static void gen_state(Ctx *c) {
uint64_t cr = 0, sr = 0;
if (rbelow(2)) cr |= 1u << 24; /* FZ */
if (rbelow(10) == 0) cr |= 1u << 25; /* DN (gate closed) */
if (rbelow(10) == 0) cr |= (uint64_t)rbelow(4) << 22; /* RMode */
if (rbelow(40) == 0) cr |= r32() & 0x07ff9f07u; /* anything else */
if (rbelow(50) == 0) cr |= UINT64_C(1) << 32; /* the host's kill switch */
switch (rbelow(6)) {
case 0: sr = 0x9f; break; /* every cumulative flag */
case 1: sr = 0x10; break; /* IXC only */
case 2: sr = 0; break;
default: sr = (r32() & 0x9f) | (rbelow(4) ? 0x10 : 0); break;
}
memset(c, 0, sizeof *c);
c->fpcr = cr;
c->fpsr = sr;
c->x[5] = 0x1234; /* (a helper must leave the rest alone) */
c->vreg[7][1] = 0x5678;
}
static unsigned long long checks, bad, per_bad[16], per_n[16];
static const char *const NAMES[16] = {"add32", "sub32", "div32", "sqrt32", "mul32", "fma32", "recps32", "rsqrts32", "add64", "sub64",
"div64", "sqrt64", "mul64", "fma64", "vector add/sub/mul", "vector mul/fma by element"};
static void report(int k, const Ctx *c0, const Ctx *got, const Ctx *want, const uint64_t *g, const uint64_t *w, int n, const uint64_t *in, int nin) {
int diff = memcmp(got, want, sizeof *got) != 0;
for (int i = 0; i < n; i++) diff |= g[i] != w[i];
checks++, per_n[k]++;
if (!diff) return;
bad++, per_bad[k]++;
if (bad <= 12) {
printf("MISMATCH %s: fpcr %#llx fpsr %#llx; in", NAMES[k], (unsigned long long)c0->fpcr, (unsigned long long)c0->fpsr);
for (int i = 0; i < nin; i++) printf(" %#llx", (unsigned long long)in[i]);
printf("; got");
for (int i = 0; i < n; i++) printf(" %#llx", (unsigned long long)g[i]);
printf(" fpsr %#llx; want", (unsigned long long)got->fpsr);
for (int i = 0; i < n; i++) printf(" %#llx", (unsigned long long)w[i]);
printf(" fpsr %#llx\n", (unsigned long long)want->fpsr);
}
}
#define SCALAR(k, hot_call, exact_call, ...) do { \
Ctx c0, c1, c2; gen_state(&c0); c1 = c0; c2 = c0; \
const uint64_t in_[] = {__VA_ARGS__}; \
uint64_t g_ = hot_call, w_ = exact_call; \
report(k, &c0, &c1, &c2, &g_, &w_, 1, in_, (int)(sizeof in_ / sizeof in_[0])); } while (0)
static void vec(int k, const Ctx *c0, Ctx *got, Ctx *want, const uint32_t g[4], const uint32_t w[4], const uint32_t *in, int nin) {
uint64_t gg[4], ww[4], ii[12];
for (int i = 0; i < 4; i++) gg[i] = g[i], ww[i] = w[i];
for (int i = 0; i < nin; i++) ii[i] = in[i];
report(k, c0, got, want, gg, ww, 4, ii, nin);
}
int main(int argc, char **argv) {
unsigned a_, b_, c_, d_;
#if defined(__FMA__)
if (!__get_cpuid(1, &a_, &b_, &c_, &d_) || !(c_ & (1u << 12)) || !(c_ & (1u << 28))) return 3;
if (!__get_cpuid_count(7, 0, &a_, &b_, &c_, &d_) || !(b_ & (1u << 5))) return 3;
#else
if (!__get_cpuid(1, &a_, &b_, &c_, &d_) || !(c_ & (1u << 20)) || !(c_ & (1u << 23))) return 3;
#endif
const unsigned long long n = argc > 1 ? strtoull(argv[1], NULL, 10) : 1000000;
rs = argc > 2 ? strtoull(argv[2], NULL, 10) : 20261114;
if (!rs) rs = 1;
for (unsigned long long it = 0; it < n; it++) {
uint32_t a = any32(), b = any32();
if (rbelow(3) == 0) edge_pair32(&a, &b);
const uint32_t z = addend32(a, b), zs = (a ^ b) & 0x80000000u;
SCALAR(0, a64_fpx_hot_add32_add32(&c1, a, b), recomp_fpe_add32(&c2, a, b), a, b);
SCALAR(1, a64_fpx_hot_sub32_sub32(&c1, a, b), recomp_fpe_sub32(&c2, a, b), a, b);
SCALAR(2, a64_fpx_hot_div32_div32(&c1, a, b), recomp_fpe_div32(&c2, a, b), a, b);
SCALAR(3, a64_fpx_hot_sqrt32_sqrt32(&c1, a), recomp_fpe_sqrt32(&c2, a), a);
SCALAR(4, a64_fpx_hot_mul32_fma32(&c1, a, b, zs), recomp_fpe_fma32(&c2, a, b, zs), a, b, zs);
SCALAR(5, a64_fpx_hot_fma32_fma32(&c1, a, b, z), recomp_fpe_fma32(&c2, a, b, z), a, b, z);
SCALAR(6, a64_fpx_hot_recps32_recps32(&c1, a, b), recomp_fpe_recps32(&c2, a, b), a, b);
SCALAR(7, a64_fpx_hot_rsqrts32_rsqrts32(&c1, a, b), recomp_fpe_rsqrts32(&c2, a, b), a, b);
{
const uint64_t p = any64(), q = any64(), r = addend64(p, q), rz = (p ^ q) & (UINT64_C(1) << 63);
SCALAR(8, a64_fpx_hot_add64_add64(&c1, p, q), recomp_fpe_add64(&c2, p, q), p, q);
SCALAR(9, a64_fpx_hot_sub64_sub64(&c1, p, q), recomp_fpe_sub64(&c2, p, q), p, q);
SCALAR(10, a64_fpx_hot_div64_div64(&c1, p, q), recomp_fpe_div64(&c2, p, q), p, q);
SCALAR(11, a64_fpx_hot_sqrt64_sqrt64(&c1, p), recomp_fpe_sqrt64(&c2, p), p);
SCALAR(12, a64_fpx_hot_mul64_fma64(&c1, p, q, rz), recomp_fpe_fma64(&c2, p, q, rz), p, q, rz);
SCALAR(13, a64_fpx_hot_fma64_fma64(&c1, p, q, r), recomp_fpe_fma64(&c2, p, q, r), p, q, r);
}
/* ---- four lanes: mostly ordinary numbers (so the one-op path is taken), now and then any operand in a lane ---- */
uint32_t vn[4], vm[4], vz[4];
for (int i = 0; i < 4; i++) {
vn[i] = rbelow(6) ? mkf(r32() & 1, 100 + rbelow(60), r32()) : any32();
vm[i] = rbelow(6) ? mkf(r32() & 1, 100 + rbelow(60), r32()) : any32();
vz[i] = rbelow(6) ? mkf(r32() & 1, 100 + rbelow(60), r32()) : addend32(vn[i], vm[i]);
}
const uint32_t el = rbelow(8) ? mkf(r32() & 1, 100 + rbelow(60), r32()) : any32();
for (int op = 0; op < 3; op++) { /* straight add, sub (4 and 2 lanes), mul */
for (int k = 4; k >= (op == 2 ? 4 : 2); k -= 2) {
Ctx c0, c1, c2; gen_state(&c0); c1 = c0; c2 = c0;
uint64_t dn[2], dm[2], dd[2] = {0x1111111111111111ULL, 0x2222222222222222ULL};
uint32_t g[4], w[4] = {0};
memcpy(dn, vn, 16); memcpy(dm, vm, 16);
const int alias = rbelow(4) == 0; /* (the destination is the first source's register) */
uint64_t *dst = alias ? dn : dd;
if (op == 0) a64_vfpx_add32(&c1, dst, dn, dm, k);
else if (op == 1) a64_vfpx_sub32(&c1, dst, dn, dm, k);
else a64_vfpx_mul32(&c1, dst, dn, dm, 4);
memcpy(g, dst, 16);
for (int j = 0; j < k; j++)
w[j] = (uint32_t)(op == 0 ? recomp_fpe_add32(&c2, vn[j], vm[j]) : op == 1 ? recomp_fpe_sub32(&c2, vn[j], vm[j])
: recomp_fpe_fma32(&c2, vn[j], vm[j], (vn[j] ^ vm[j]) & 0x80000000u));
uint32_t in[8]; memcpy(in, vn, 16); memcpy(in + 4, vm, 16);
vec(14, &c0, &c1, &c2, g, w, in, 8);
}
}
for (int op = 0; op < 3; op++) { /* by element, as vectorize_elem writes it: mul, fmla, fmls */
Ctx c0, c1, c2; gen_state(&c0); c1 = c0; c2 = c0;
uint64_t dn[2], dd[2], e[2];
uint32_t g[4], w[4];
memcpy(dn, vn, 16); memcpy(dd, vz, 16);
e[0] = e[1] = (uint64_t)el << 32 | el;
const int alias = op == 0 && rbelow(4) == 0;
uint64_t *dst = alias ? dn : dd;
if (op == 0) a64_vfpx_mul32(&c1, dst, dn, e, 4);
else a64_vfpx_fma32(&c1, dst, dn, e, op == 2);
memcpy(g, dst, 16);
for (int j = 0; j < 4; j++) {
const uint32_t x = op == 2 ? vn[j] ^ 0x80000000u : vn[j];
w[j] = (uint32_t)recomp_fpe_fma32(&c2, x, el, op == 0 ? (vn[j] ^ el) & 0x80000000u : vz[j]);
}
uint32_t in[9]; memcpy(in, vn, 16); memcpy(in + 4, vz, 16); in[8] = el;
vec(15, &c0, &c1, &c2, g, w, in, 9);
}
}
for (int k = 0; k < 16; k++)
if (per_bad[k]) printf(" %s: %llu of %llu mismatch\n", NAMES[k], per_bad[k], per_n[k]);
printf("%llu checks, %llu mismatches\n", checks, bad);
return bad ? 1 : 0;
}

View File

@@ -25,6 +25,10 @@ static uint64_t want_gpu = 0x123456789abULL, want_size = 0x100002345ULL;
static const MkwVkRenderer* vk;
static int hos_vcores_on() { return enabled; }
static int64_t hos_now_ns() { return 0; } // (the wrapper times its call for the lab's HOS_NVN_LOG_FLUSH log)
static void hos_log(const char*, ...) {}
int hos_ft_on() { return 0; } // (the frame trace, HOS_FT: off here)
double hos_ft_ms() { return 0.0; }
static void vc_release(HosThread* t) {
std::lock_guard<std::mutex> lock(lease_mu);
CHECK(owner == t && t->vc_held == 1);

View File

@@ -91,6 +91,8 @@ std::atomic<int64_t> st_cache_hits{0};
// is the cache's own, so the counts below are of what the loader made)
const std::vector<uint32_t>* renodx_replacement(uint32_t) { return nullptr; }
#define PROF_EXACT() // (vk_prof.h's exact-timing scope, since #989: nothing to time here)
// (local, texture detail: vk_shader.cpp's note of which bindings a module reads by exact texel. Nothing to note here)
void exact_note(VkShaderModule, const std::vector<uint32_t>&) {}
#include "cache_functions.inc"
void save(uint64_t key, bool empty_second = false, int bad_answer = 0) {

128
tests/test_camera_speed.py Normal file
View File

@@ -0,0 +1,128 @@
"""The Camera speed row (host/prompts.c build_camera, nx2/prompt_assets.py CAMERA).
The game's five camera sensitivities are five floats in a BYML file inside its camera pack. Setup's Update writes the
pack decompressed and a table of where the floats are; at the start the host serves the pack with each one times the
row's factor. Checked here, without the game:
- The table: each float of the StickSensitivity array, at its place in the pack, with the game's value; other arrays
and other files of the pack are left out.
- A pack that is not the one known gives no table: no such file, no such array, an array of something else than
floats, a value that is no speed.
- The host's and the menu's ends are wired: the pack's path, the row, its key in the options file, the mouse.
python -I -B tests/test_camera_speed.py
"""
import struct
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "nx2"))
import prompt_assets # noqa: E402
RATES = (1.2, 1.6, 2.0, 2.4, 3.0)
def byml(entries):
"""a little-endian BYML whose root dictionary holds entries: [(key, value)], a value a float, or a list of floats (an
array), or a list of ints (an array of ints)"""
names = sorted(k for k, _ in entries)
keys = bytearray(struct.pack("<I", 0xc2 | len(names) << 8))
texts = b""
first = 4 + 4 * (len(names) + 1)
for n in names:
keys += struct.pack("<I", first + len(texts))
texts += n.encode() + b"\0"
keys += struct.pack("<I", first + len(texts))
keys += texts
keys += bytes(-len(keys) % 4)
keys_at = 16
root_at = keys_at + len(keys)
root = bytearray(struct.pack("<I", 0xc1 | len(entries) << 8))
tail = bytearray()
tail_at = root_at + 4 + 8 * len(entries)
for k, v in sorted(entries):
if isinstance(v, list):
is_float = all(isinstance(x, float) for x in v)
arr = struct.pack("<I", 0xc0 | len(v) << 8) + bytes([0xd2 if is_float else 0xd1] * len(v))
arr += bytes(-len(arr) % 4)
arr += b"".join(struct.pack("<f", x) if is_float else struct.pack("<i", x) for x in v)
root += struct.pack("<I", names.index(k) | 0xc0 << 24) + struct.pack("<I", tail_at + len(tail))
tail += arr
else:
root += struct.pack("<I", names.index(k) | 0xd2 << 24) + struct.pack("<f", v)
return b"YB\x07\x00" + struct.pack("<III", keys_at, 0, root_at) + bytes(keys) + bytes(root) + bytes(tail)
def sarc(files):
"""a SARC of files: [(name, data)], as nx2/prompt_assets.py sarc_member reads one"""
hl = 0x14
n = len(files)
names = b""
nodes = b""
data = b""
for name, d in files:
data += bytes(-len(data) % 8)
nodes += struct.pack("<IIII", 0, 0x01000000 | len(names) // 4, len(data), len(data) + len(d))
names += name.encode() + b"\0"
names += bytes(-len(names) % 4)
data += d
sfat = b"SFAT" + struct.pack("<HHI", 12, n, 0x65) + nodes
sfnt = b"SFNT" + struct.pack("<HH", 8, 0) + names
data_at = hl + len(sfat) + len(sfnt)
data_at += -data_at % 8
head = b"SARC" + struct.pack("<HHIIHH", hl, 0xfeff, data_at + len(data), data_at, 0x100, 0)
return (head + sfat + sfnt).ljust(data_at, b"\0") + data
GOOD = byml([("MoveSpeedRateFarDist", 40.0), ("Other", [0.5, 0.25]), ("StickSensitivity", list(RATES)),
("StickSensitivityLat2LngRatio", 1.78)])
def pack(member=GOOD, name=prompt_assets.CAMERA_MEMBER):
return sarc([("GameParameter/Other.bgyml", byml([("StickSensitivity", [9.0, 9.5])])), (name, member), ("Last.bin", b"12345")])
class CameraSpeedTest(unittest.TestCase):
def test_table(self):
p = pack()
got = prompt_assets.camera_speeds(p)
self.assertEqual(len(got), 5)
for (off, v), want in zip(got, RATES):
self.assertAlmostEqual(v, want, places=5)
self.assertEqual(struct.unpack_from("<f", p, off)[0], v) # (its place in the pack)
self.assertEqual([off for off, _ in got], list(range(got[0][0], got[0][0] + 20, 4))) # (five in a row)
with tempfile.TemporaryDirectory(prefix="camera-") as d:
path = Path(d) / "camera.bin"
prompt_assets.write_camera(path, got)
raw = path.read_bytes()
self.assertEqual(raw[:4], b"HPCM")
self.assertEqual(struct.unpack_from("<I", raw, 4)[0], 5)
self.assertEqual(len(raw), 8 + 8 * 5)
self.assertEqual(struct.unpack_from("<If", raw, 8), got[0])
def test_unknown_pack(self):
self.assertIsNone(prompt_assets.camera_speeds(b"not a pack at all, only some bytes here"))
self.assertIsNone(prompt_assets.camera_speeds(pack(name="Component/Elsewhere.bgyml"))) # (no such file)
self.assertIsNone(prompt_assets.camera_speeds(pack(byml([("MoveSpeedRateFarDist", 40.0)])))) # (no such array)
self.assertIsNone(prompt_assets.camera_speeds(pack(byml([("StickSensitivity", [1, 2, 3])])))) # (not floats)
self.assertIsNone(prompt_assets.camera_speeds(pack(byml([("StickSensitivity", 2.0)])))) # (not an array)
self.assertIsNone(prompt_assets.camera_speeds(pack(byml([("StickSensitivity", [1.2, 0.0, 2.0])])))) # (no speed)
self.assertIsNone(prompt_assets.camera_speeds(pack(b"YB\x07\x00" + bytes(40)))) # (an empty file)
def test_wired(self):
prompts = (ROOT / "host/prompts.c").read_text(encoding="utf-8")
self.assertIn('#define CAMERA_PATH "' + prompt_assets.CAMERA.lower() + '"', prompts)
self.assertIn("{build, build_textures, build_layouts, build_camera}", prompts)
self.assertIn('hos_ini_option("camera_speed", 100)', prompts)
self.assertIn("v = v < 25 ? 25 : v > 300 ? 300 : v;", prompts)
menu = (ROOT / "gpu/vk/vk_menu.cpp").read_text(encoding="utf-8")
self.assertIn('{TAB_CONTROLS, "Camera speed", &opts.camera_speed, 25, 300, 5, tx_sens,', menu)
hid = (ROOT / "host/nn_hid.c").read_text(encoding="utf-8")
self.assertIn("ox /= cam, oy /= cam;", hid)
if __name__ == "__main__":
unittest.main()

78
tests/test_fpx_hot.py Normal file
View File

@@ -0,0 +1,78 @@
"""The hot functions' float helpers and the four-lane helpers, game-free: bit-exact with the exact bodies.
a64_lift.py gives hot lifted functions their own copy of every FPX1 helper (a64_fpx_hot_*): the fast path inline, and past it
the exact path called without the context (a64_fpx_nc_*: the control word in, the status word in and out), the result
carried as a float or double. NEON's four-lane FADD / FSUB / FMUL, and FMUL / FMLA / FMLS by element (vectorize_elem), go
through a64_vfpx_add32 / sub32 / mul32 and a64_vfpx_fma32: one SSE or FMA op when every lane passes the scalar fast path's
tests, otherwise lane by lane through the scalar helpers, again without the context. And where the build has FMA
(x86-64-v3) the binary32 fused multiply-add's fast path is the host's instruction (a64_rtgen.fma32_native) instead of
suyu's binary64 form.
This builds what the real build builds (a64_fpx.h from a64_lift.fpx_header(), a64_suyu.h / a64_suyu.c from a64_rtgen.py,
through tests/test_fpx_sticky.py's generator) and compiles tests/fpx_hot_test.c against the real rt/a64.h at the build's
FP flags, at -march=x86-64-v3 and at -march=x86-64-v2 (the binary64 form; the four-lane multiply-add lane by lane). For
random operands (zeros, subnormals, infinities, NaNs, the extreme normals, products at the underflow and overflow edges,
cancellations) under random FPCR and FPSR states, each helper's value, FPSR and the rest of the context must equal the
exact body's: recomp_fpe_* for the scalars, and for four lanes the exact body lane by lane in order, FPSR carried from
lane to lane (what the lane loops did before).
python -I -B tests/test_fpx_hot.py [--cc clang] [--cxx clang++] [--n 2000000]
Skipped (exit 0) where tests/test_fpx_sticky.py is.
"""
import argparse
import os
import platform
import sys
import tempfile
import time
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import test_fpx_sticky as S # noqa: E402 (its compilers, flags and generator)
SHAPES = {(a + s, a + s, n, n) for s in ("32", "64") for a, n in (("add", 2), ("sub", 2), ("div", 2), ("sqrt", 1), ("fma", 3))} | \
{("mul32", "fma32", 2, 3), ("mul64", "fma64", 2, 3), ("recps32", "recps32", 2, 2), ("rsqrts32", "rsqrts32", 2, 2)}
def main():
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument("--cc")
p.add_argument("--cxx")
p.add_argument("--n", type=int, default=2000000)
a = p.parse_args()
if platform.machine().lower() not in ("x86_64", "amd64"):
S.skip("not an x86-64 host: %s" % platform.machine())
cc = S.compiler(a.cc, "CC", ("clang", "gcc", "cc"), "clang")
cxx = S.compiler(a.cxx, "CXX", ("clang++", "g++", "c++"), "clang++")
if not cc or not cxx:
S.skip("no C compiler" if not cc else "no C++ compiler")
t0 = time.time()
failed, ran = [], []
with tempfile.TemporaryDirectory(prefix="hookshot-fpx-hot-") as tmp:
S.generate(cxx, tmp, S.target_flags(cxx, tmp))
import a64_lift
with open(os.path.join(tmp, "a64_fpx.h"), "w", newline="\n") as f: # (every shape, not only the sticky test's two)
f.write(a64_lift.fpx_header(SHAPES))
for march, _ in S.BUILDS:
exe = os.path.join(tmp, "fpx_hot_test_%s.exe" % march)
S.build([cc] + S.target_flags(cc, tmp) + ["-march=" + march] + S.FLAGS +
["-D_CRT_SECURE_NO_WARNINGS", "-Wno-everything", "-I" + os.path.join(S.KIT, "rt"), "-I" + tmp,
os.path.join(S.ROOT, "tests", "fpx_hot_test.c"), os.path.join(tmp, "a64_suyu.c"), "-o", exe],
"the driver at " + march)
r = S.run([exe, str(a.n), "20261114"])
print("%s: %s" % (march, r.stdout.rstrip().replace("\n", "\n ")))
if r.returncode == 3:
print("fpx hot: %s skipped (this CPU is not %s)" % (march, march))
continue
ran.append(march)
if r.returncode:
failed.append(march)
if not ran:
S.skip("this CPU is not x86-64-v2")
if failed:
raise SystemExit("fpx hot: FAIL (%s) in %.1f s" % (", ".join(failed), time.time() - t0))
print("fpx hot: pass in %.1f s" % (time.time() - t0))
if __name__ == "__main__":
main()

97
tests/test_fsr_sdk.py Normal file
View File

@@ -0,0 +1,97 @@
"""AMD's FSR 4 libraries for the players' build (scripts/fsr_sdk.py, nx2/make_game.py --fsr).
Without the network: a stand-in for AMD's repository hands out this test's own files, and the script's table is pointed
at them.
- both files are fetched into the folder; a second fetch downloads nothing; a damaged one alone is fetched again;
- a file of another size or SHA-256 stops the fetch and is not left in the folder;
- the game folder gets the two libraries and AMD's license text beside the exe, and loses exactly those again;
- make_game.py fetches with --fsr, places them at the end, and Setup asks for --fsr.
python -I -B tests/test_fsr_sdk.py
"""
import hashlib
import importlib.util
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location("fsr_sdk", ROOT / "scripts" / "fsr_sdk.py")
fsr = importlib.util.module_from_spec(spec)
spec.loader.exec_module(fsr)
def content(name):
return (("AMD's %s\n" % name) * 300).encode()
class FsrSdkTest(unittest.TestCase):
def setUp(self):
self.saved = fsr.RUNTIME
fsr.RUNTIME = [(name, len(content(name)), hashlib.sha256(content(name)).hexdigest()) for name, _, _ in self.saved]
self.tmp = tempfile.TemporaryDirectory(prefix="fsr-sdk-")
self.dir = Path(self.tmp.name)
self.asked = []
self.said = []
def tearDown(self):
fsr.RUNTIME = self.saved
self.tmp.cleanup()
def get(self, url):
self.asked.append(url)
return content(url.rsplit("/", 1)[1])
def test_fetch(self):
folder = self.dir / "sdk"
fsr.fetch(str(folder), say=self.said.append, get=self.get)
self.assertTrue(fsr.complete(str(folder)))
self.assertEqual(len(self.asked), 2)
self.assertTrue(all(url.startswith(fsr.BASE) for url in self.asked))
fsr.fetch(str(folder), say=self.said.append, get=self.get)
self.assertEqual(len(self.asked), 2) # (nothing asked for again)
self.assertIn("already", self.said[-1])
(folder / "amd_fidelityfx_loader_vk.dll").write_bytes(b"damaged")
self.assertEqual(fsr.missing(str(folder)), ["amd_fidelityfx_loader_vk.dll"])
fsr.fetch(str(folder), say=self.said.append, get=self.get)
self.assertEqual(len(self.asked), 3)
self.assertTrue(fsr.complete(str(folder)))
def test_another_file_refused(self):
folder = self.dir / "sdk"
with self.assertRaises(ValueError):
fsr.fetch(str(folder), say=self.said.append, get=lambda url: content(url) + b"x")
self.assertEqual(sorted(p.name for p in folder.iterdir()), [])
self.assertFalse(fsr.complete(str(folder)))
def test_game_folder(self):
folder, game = self.dir / "sdk", self.dir / "game"
fsr.fetch(str(folder), say=self.said.append, get=self.get)
game.mkdir()
(game / "Triforce.exe").write_bytes(b"the game")
placed = fsr.install_runtime(str(folder), str(game))
self.assertEqual(sorted(Path(p).name for p in placed), ["amd_fidelityfx_loader_vk.dll", "amd_fidelityfx_upscaler_vk.dll"])
self.assertTrue((game / fsr.LICENSE_NAME).is_file())
self.assertIn("Advanced Micro Devices", (game / fsr.LICENSE_NAME).read_text(encoding="utf-8", errors="replace"))
self.assertTrue(fsr.remove_runtime(str(game)))
self.assertEqual([p.name for p in game.iterdir()], ["Triforce.exe"])
self.assertFalse(fsr.remove_runtime(str(game)))
def test_real_table(self):
self.assertEqual([name for name, _, _ in self.saved], ["amd_fidelityfx_loader_vk.dll", "amd_fidelityfx_upscaler_vk.dll"])
for name, size, digest in self.saved:
self.assertGreater(size, 30000, name)
self.assertRegex(digest, r"^[0-9a-f]{64}$", name)
self.assertTrue(Path(fsr.LICENSE_SOURCE).is_file())
def test_wired(self):
src = (ROOT / "nx2" / "make_game.py").read_text(encoding="utf-8")
for needle in ("fetch_fsr_runtime(work)", 'place_fsr_runtime(out, work, os.environ.get("HOOKSHOT_FSR") == "1")',
"fsr.remove_runtime(out)"):
self.assertIn(needle, src)
shell = (ROOT / "setup" / "Shell.cs").read_text(encoding="utf-8")
self.assertIn('args.Add("--fsr")', shell)
if __name__ == "__main__":
raise SystemExit(unittest.main(verbosity=1))

View File

@@ -13,7 +13,19 @@ Solver: the game cuts frame step * n + 0.001 to its solver steps (n a 60 fps fra
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
Floors: the follow-surface updates keep a tenth of a floor's velocity above an inverse step of 100, which is every update above
100 fps; the number compared with becomes 100 / the frame's 60 fps frames under one frame (200 at 120 fps, 240 at 144), and stays
100 at 60 fps and below and without a frame time.
Surfaces: the water managers and the interactive floors cut seconds / (1/60 s) to whole fixed steps, 0 above 60 fps; with the
fractions carried per object 144 updates of 60/144 give 60 steps, each object its own, and without a frame time, or for a
quotient that is no count, the quotient is handed back as it is.
Recorder: the ocarina recorder's clock adds (unsigned)step, 0 above 60 fps; with the fraction carried 144 frames give 60, a
whole step gives the game's count, and without a frame time it is the game's cut.
The patches put the gate at the job's head and the step through the host, hook both solver places, the three floor compares, the three surface cuts and the four
recorder adds, 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
@@ -113,6 +125,48 @@ int main(void) {
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) */
/* (a floor's carry: the number the inverse step is compared with) */
CHECK(oot_floor_cut(0) == bits(100.0f)); /* (a cap of 60) */
CHECK(oot_floor_cut(bits(1.0f / 60.0f)) == bits(100.0f) && oot_floor_cut(bits(1.0f / 30.0f)) == bits(100.0f));
CHECK(oot_bits_float(oot_floor_cut(bits(1.0f / 120.0f))) > 199.9f && oot_bits_float(oot_floor_cut(bits(1.0f / 120.0f))) < 200.1f);
CHECK(oot_bits_float(oot_floor_cut(bits(1.0f / 144.0f))) > 239.9f && oot_bits_float(oot_floor_cut(bits(1.0f / 144.0f))) < 240.1f);
for (int fps = 61; fps <= 240; fps++) { /* (the inverse step of a frame at fps is under the cut, as 60 is under 100) */
const float secs = 1.0f / (float)fps;
CHECK(1.0f / secs < oot_bits_float(oot_floor_cut(bits(secs))));
CHECK(1.0f / (secs * 0.59f) > oot_bits_float(oot_floor_cut(bits(secs)))); /* (and a slow motion under 0.6 still passes it) */
}
CHECK(oot_floor_cut(bits(NAN)) == bits(100.0f) && oot_floor_cut(bits(-1.0f)) == bits(100.0f));
{ /* (the surfaces' fixed steps) */
static FrameWhole w;
float n = 0, m = 0;
for (int i = 0; i < 144; i++) {
n += oot_bits_float(oot_sim_steps(&w, 0x1000, bits(60.0f / 144.0f), 1));
m += oot_bits_float(oot_sim_steps(&w, 0x2000, bits(60.0f / 144.0f), 1));
}
CHECK((n == 60.0f || n == 59.0f) && m == n); /* (each object its own remainder) */
CHECK(oot_sim_steps(&w, 0x3000, bits(1.0f), 1) == bits(1.0f) && oot_sim_steps(&w, 0x3000, bits(2.0f), 1) == bits(2.0f));
CHECK(oot_sim_steps(&w, 0x3000, bits(0.42f), 0) == bits(0.42f)); /* (a cap of 60: the quotient, for the game's cut) */
CHECK(oot_sim_steps(&w, 0x3000, bits(0.0f), 1) == bits(0.0f) && oot_sim_steps(&w, 0x3000, bits(-2.0f), 1) == bits(-2.0f));
CHECK(oot_sim_steps(&w, 0x3000, bits(NAN), 1) == bits(NAN) && oot_sim_steps(&w, 0x3000, bits(3e9f), 1) == bits(3e9f));
float slow = 0; /* (a frame slower than 60 fps: 1.5 steps a frame are 1 and 2 in turn) */
for (int i = 0; i < 40; i++) slow += oot_bits_float(oot_sim_steps(&w, 0x4000, bits(1.5f), 1));
CHECK(slow == 60.0f);
}
{ /* (the ocarina recorder's clock) */
float rem = 0;
uint32_t n = 0;
for (int i = 0; i < 144; i++) n += oot_record_frames(&rem, bits(60.0f / 144.0f), 1);
CHECK(n == 60 || n == 59);
rem = 0, n = 0;
for (int i = 0; i < 1200; i++) n += oot_record_frames(&rem, bits(0.5f), 1);
CHECK(n == 600);
rem = 0;
CHECK(oot_record_frames(&rem, bits(1.0f), 1) == 1 && rem == 0.0f && oot_record_frames(&rem, bits(2.0f), 1) == 2 && rem == 0.0f);
CHECK(oot_record_frames(&rem, bits(0.75f), 1) == 0 && rem == 0.75f && oot_record_frames(&rem, bits(0.75f), 1) == 1 && rem == 0.5f);
CHECK(oot_record_frames(&rem, bits(0.42f), 0) == 0 && rem == 0.0f); /* (a cap of 60: the game's cut, the remainder cleared) */
CHECK(oot_record_frames(&rem, bits(1.9f), 0) == 1 && oot_record_frames(&rem, bits(-1.0f), 1) == 0 && oot_record_frames(&rem, bits(NAN), 1) == 0);
CHECK(oot_record_frames(&rem, bits(5e9f), 0) == 0xffffffffu); /* (fcvtzu saturates) */
}
if (!fails) printf("PASS\n");
return fails != 0;
}
@@ -125,6 +179,18 @@ LIFTED_SOLVER = '#include "a64.h"\n' + "".join(head + " c->x[8]=1;\n" + oot_p
for head in oot_patches.SOLVER_HEADS)
LIFTED_FLOOR = '#include "a64.h"\n' + "".join(
head + " { { uint64_t _v=recomp_load32(c,(c->x[8]+40ULL)); a64_vset64(c->vreg[0],_v); } }\n" + oot_patches.FLOOR_OLD
+ "if ((c->n == c->v) && !c->z) { goto L_1; }\nL_1:;\n return;\n}\n" for head in oot_patches.FLOOR_HEADS)
# (a divide, the cut into the site's register, and one more cut of another register after it, as f_80652cb0 has many)
LIFTED_SIM = '#include "a64.h"\n' + "".join(
head + " _v=a64_fpx_div32_div32(c,_a,_b);\n a64_vset64(c->vreg[0],_v);}\n" + oot_patches.SIM_OLD % reg
+ (oot_patches.SIM_OLD % (reg + 1)).replace("vreg[0]", "vreg[1]") + " return;\n}\n" for head, reg in oot_patches.SIM_SITES)
LIFTED_RECORD = '#include "a64.h"\n' + "".join(
head + (oot_patches.RECORD_OLD + " recomp_store32(c,c->x[20] + 1532,c->x[8]);\n") * count
+ oot_patches.RECORD_OLD.replace(",32,32,0,3,", ",32,32,1,3,") + " return;\n}\n" for head, count in oot_patches.RECORD_SITES)
class HighFpsTests(unittest.TestCase):
def test_gate_and_steps(self):
compiler = shutil.which("gcc") or shutil.which("clang")
@@ -168,5 +234,48 @@ class HighFpsTests(unittest.TestCase):
self.assertEqual(oot_patches.patch_solver_steps(other), (other, 0))
def test_floor_patch(self):
out, n = oot_patches.patch_floor_carry(LIFTED_FLOOR)
self.assertEqual(n, 3)
self.assertEqual(out.count(oot_patches.FLOOR_NEW), 3)
self.assertNotIn("0x42c80000", out)
self.assertIn(oot_patches.FLOOR_DECL, out)
self.assertEqual(oot_patches.patch_floor_carry(out), (out, 3)) # (patched before: as it is)
other = LIFTED_FLOOR.replace("0x42c80000ULL", "0x42c90000ULL") # (another game version's code)
self.assertEqual(oot_patches.patch_floor_carry(other), (other, 0))
# (the same words in a function that is not one of the three stay the game's)
stray = LIFTED_FLOOR + "void f_80000000(Ctx *__restrict A64_NOESC c) {\n" + oot_patches.FLOOR_OLD + "}\n"
out, n = oot_patches.patch_floor_carry(stray)
self.assertEqual((n, out.count(oot_patches.FLOOR_OLD)), (3, 1))
def test_sim_patch(self):
out, n = oot_patches.patch_sim_steps(LIFTED_SIM)
self.assertEqual(n, 3)
self.assertEqual(out.count(oot_patches.SIM_HOOK), 3)
for _, reg in oot_patches.SIM_SITES: # (the hook right before the game's own cut, which stays)
self.assertIn(oot_patches.SIM_HOOK + oot_patches.SIM_OLD % reg, out)
self.assertEqual(out.count("recomp_fp_to_int"), LIFTED_SIM.count("recomp_fp_to_int"))
self.assertIn(oot_patches.SIM_DECL, out)
self.assertEqual(oot_patches.patch_sim_steps(out), (out, 3))
other = LIFTED_SIM.replace(",32,32,1,3,", ",32,31,1,3,")
self.assertEqual(oot_patches.patch_sim_steps(other), (other, 0))
twice = LIFTED_SIM.replace(oot_patches.SIM_OLD % 22, (oot_patches.SIM_OLD % 22) * 2) # (two such cuts: not this build)
self.assertEqual(oot_patches.patch_sim_steps(twice)[1], 2)
def test_record_patch(self):
out, n = oot_patches.patch_record_clock(LIFTED_RECORD)
self.assertEqual(n, 4)
self.assertEqual(out.count(oot_patches.RECORD_NEW), 4)
self.assertNotIn(oot_patches.RECORD_OLD, out)
self.assertEqual(out.count(",32,32,1,3,"), 3) # (the functions' signed cuts stay the game's)
self.assertIn(oot_patches.RECORD_DECL, out)
self.assertEqual(oot_patches.patch_record_clock(out), (out, 4))
# (a function with another number of adds than this build's is left whole)
head, _ = oot_patches.RECORD_SITES[2]
other = LIFTED_RECORD.replace(head, head + oot_patches.RECORD_OLD, 1)
out, n = oot_patches.patch_record_clock(other)
self.assertEqual((n, out.count(oot_patches.RECORD_OLD)), (2, 3))
if __name__ == "__main__":
raise SystemExit(unittest.main(verbosity=1))

View File

@@ -28,7 +28,9 @@ def main():
raise SystemExit("the menu lost a mouse row: " + row)
if "id == 7 ? opts.mouse_sens" not in replay or "id == 8 ? opts.mouse_invert" not in replay:
raise SystemExit("options 7 and 8 do not reach the host")
if "t.option_set = menu_option_set" not in replay or "void (*option_set)(int id, int value);" not in api:
sets = "t.option_set = menu_option_set" in replay or ("t.option_set = [](int id, int value) {" in replay
and " menu_option_set(id, value);" in replay)
if not sets or "void (*option_set)(int id, int value);" not in api:
raise SystemExit("the host cannot change the menu's sensitivity")
if "vk->option_set(id, value)" not in backend:
raise SystemExit("rx_option_set does not reach the renderer")

View File

@@ -74,6 +74,7 @@ static uint64_t h2g(void *p) { return (uint64_t)(uintptr_t)p; }
#define RETP(p) RET(h2g(p))
#define U64(i) (c->x[i])
static void rx_nvn_invalidate(uint64_t a, uint64_t b) { (void)a; (void)b; }
static void flush_note(Ctx *c, int kind, uint64_t gpu, uint64_t size) { (void)c; (void)kind; (void)gpu; (void)size; } /* (the lab's HOS_NVN_LOG_FLUSH line) */
''' + guard + "\n" + flags + "\n" + size + "\n" + map_ + "\n" + address + "\n" + invalidate + r'''
int main(void) {
Ctx c;

View File

@@ -31,6 +31,7 @@ constexpr int MAX_PATH = 260;
DWORD hos_game_path_a(char *path, int size) { return snprintf(path, size, "./hookshot.exe"); }
void hos_log(const char *, ...) {}
volatile uint64_t hos_wide_out = 0; // (host/hos_main.c: the widescreen picture, 0 = none; nn_applet.c reads it)
int hos_scene_rgba16f(void) { return 0; } // (host/nn_vi.c: the scene's colour target, the game's own here; tests/test_rt_pool_minimum.py has the RGBA16F cases)
struct { int handheld, output_res; } opts;
namespace mkwvk {
int up_now = 0; // (the Upscaler shown: 0 the game's own, 1 DLSS, 2 DLAA)
@@ -98,7 +99,7 @@ int main(int argc, char **argv) {
source.write_text(fixture, encoding="utf-8")
subprocess.run([args.cxx, "-std=c++17", "-I", str(ROOT), str(source), "-o", str(exe)], check=True)
for saved, handheld, override, mode, w, h, label in cases:
(work / "hookshot_options.ini").write_text(f"output_res={saved}\nhandheld_profile={handheld}\n", encoding="ascii")
(work / "triforce_options.ini").write_text(f"output_res={saved}\nhandheld_profile={handheld}\n", encoding="ascii")
env = os.environ.copy()
for key in ("HOS_OUTPUT_RES", "HOS_HANDHELD", "HOS_RT_POOL_MB"):
env.pop(key, None)

View File

@@ -84,6 +84,14 @@ int main(void) {
if (hos_rt_pool_size(305 * MIB) != (512 + 128) * MIB) { fprintf(stderr, "RGBA16F scene: not 640 MiB\n"); failed = 1; }
override = "256";
if (hos_rt_pool_size(305 * MIB) != 256 * MIB) { fprintf(stderr, "RGBA16F scene: the override not kept\n"); failed = 1; }
/* (the 5120 x 2880 output with an RGBA16F scene: 900 MiB for the whole pool, not 900 + 128, which failed the game's
reservation at boot; 3840 x 2160 keeps its 128 MiB more, and an explicit override still wins) */
output_width = 5120; output_height = 2880; override = NULL;
if (hos_rt_pool_size(305 * MIB) != 900 * MIB) { fprintf(stderr, "RGBA16F scene at 5120x2880: not 900 MiB\n"); failed = 1; }
override = "1028";
if (hos_rt_pool_size(305 * MIB) != 1028 * MIB) { fprintf(stderr, "RGBA16F scene at 5120x2880: the override not kept\n"); failed = 1; }
output_width = 3840; output_height = 2160; override = NULL;
if (hos_rt_pool_size(305 * MIB) != (687 + 128) * MIB) { fprintf(stderr, "RGBA16F scene at 3840x2160: not 815 MiB\n"); failed = 1; }
return failed;
}
'''
@@ -98,7 +106,7 @@ int main(void) {
subprocess.run([compiler, "-std=c11", "-O2", str(source), "-o", str(binary)], check=True)
subprocess.run([str(binary)], check=True, timeout=5)
print("PASS: 512 MiB floor, 4K/ultrawide growth and explicit overrides, the 5K output's 900 MiB (14 cases), "
"an RGBA16F scene's 128 MiB more")
"an RGBA16F scene's 128 MiB more, and the 5K output's 900 MiB with it")
if __name__ == "__main__":

242
tests/test_save_tx.py Normal file
View File

@@ -0,0 +1,242 @@
"""The save as a transaction, with backups (host/save_tx.h), on real folders in a temporary place.
A Commit copies the working copy to a staging folder, renames that to ready-<n> (the commit point) and swaps it in for the
committed save, whose old state becomes a backup at most every five minutes, the newest five kept. Checked here:
- a first save, a second one and its backup, the five-minute gap, the five kept, a player's own folder among them left alone;
- a Commit killed before each of its steps, then the next start: the committed save is the whole old one or the whole new one,
never a mix, it is the new one once the commit point was passed, and the working copy equals it;
- changes no Commit took are gone at the next start, as on the console;
- a staging folder that cannot be made: the files are copied over the save's as before the transactions (result 0);
- (Windows) a save folder another program holds open: the commit waits as ready-<n>, the next start plays from it, and the
next Commit with the folder free puts it in place.
python -I -B tests/test_save_tx.py
"""
import shutil
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
FIXTURE = r"""
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define HOS_FS_PATH 1024
#include "save_tx.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 void put(const char *dir, const char *name, const char *text) {
char path[HOS_FS_PATH];
snprintf(path, sizeof path, "%s\\%s", dir, name);
FILE *f = fopen(path, "wb");
if (!f) { printf("FAIL: cannot write %s\n", path); fails++; return; }
for (int i = 0; i < 3000; i++) fputs(text, f); /* (more than one copy chunk would hold: 3000 x a few bytes) */
fclose(f);
}
static int has(const char *dir, const char *name, const char *text) {
char path[HOS_FS_PATH], got[16] = "";
snprintf(path, sizeof path, "%s\\%s", dir, name);
FILE *f = fopen(path, "rb");
if (!f) return 0;
fseek(f, 0, SEEK_END);
const long size = ftell(f);
fseek(f, size - (long)strlen(text), SEEK_SET);
const size_t n = fread(got, 1, strlen(text), f);
fclose(f);
return size == 3000L * (long)strlen(text) && n == strlen(text) && !memcmp(got, text, n);
}
/* (a save of three files, one in a slot folder, all written with one tag) */
static void write_save(const char *dir, const char *tag) {
char slot[HOS_FS_PATH];
tx_make_dirs(dir);
snprintf(slot, sizeof slot, "%s\\slot_00", dir);
tx_make_dirs(slot);
put(dir, "account.sav", tag);
put(dir, "option.sav", tag);
put(slot, "progress.sav", tag);
}
/* 1: the folder is exactly a save written with tag */
static int is_save(const char *dir, const char *tag) {
char slot[HOS_FS_PATH];
snprintf(slot, sizeof slot, "%s\\slot_00", dir);
return has(dir, "account.sav", tag) && has(dir, "option.sav", tag) && has(slot, "progress.sav", tag);
}
static int count_dirs(const char *dir) {
char pat[HOS_FS_PATH];
WIN32_FIND_DATAA fd;
int n = 0;
snprintf(pat, sizeof pat, "%s\\*", dir);
HANDLE f = FindFirstFileA(pat, &fd);
if (f == INVALID_HANDLE_VALUE) return 0;
do {
if (strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) n++;
} while (FindNextFileA(f, &fd));
FindClose(f);
return n;
}
static void new_run(void) { save_tx_crash_at = save_tx_step = save_tx_dead = 0; save_tx_last_backup = 0; }
static int clean_tx(const SaveTxPaths *p) { /* nothing of a commit left behind */
return !tx_exists(p->staging) && !tx_exists(p->old) && !tx_ready_highest(p, NULL, 0);
}
int main(int argc, char **argv) {
if (argc < 2) return 2;
char user[HOS_FS_PATH], note[256], backup[HOS_FS_PATH];
SaveTxPaths p;
save_tx_test_now = 1800000000;
/* a first save, a second one and its backup, the gap, the five kept */
snprintf(user, sizeof user, "%s\\a", argv[1]);
save_tx_paths(&p, user, "0123456789abcdef0000000000000001");
new_run();
CHECK(save_tx_mount(&p, note, sizeof note) == 1 && !note[0]);
CHECK(tx_is_dir(p.work) && tx_is_dir(p.save));
write_save(p.work, "v1");
CHECK(save_tx_commit(&p, backup, sizeof backup) == 1 && !backup[0]);
CHECK(is_save(p.save, "v1") && clean_tx(&p) && count_dirs(p.backups) == 0);
write_save(p.work, "v2");
CHECK(save_tx_commit(&p, backup, sizeof backup) == 1 && backup[0] && is_save(backup, "v1"));
CHECK(is_save(p.save, "v2") && clean_tx(&p) && count_dirs(p.backups) == 1);
save_tx_test_now += 10; /* (ten seconds on: no backup) */
write_save(p.work, "v3");
CHECK(save_tx_commit(&p, backup, sizeof backup) == 1 && !backup[0] && is_save(p.save, "v3") && count_dirs(p.backups) == 1);
save_tx_test_now += SAVE_TX_BACKUP_GAP_S;
write_save(p.work, "v4");
CHECK(save_tx_commit(&p, backup, sizeof backup) == 1 && is_save(backup, "v3") && count_dirs(p.backups) == 2);
{ /* (a folder of the player's own among the backups stays) */
char own[HOS_FS_PATH];
snprintf(own, sizeof own, "%s\\my old save", p.backups);
write_save(own, "me");
for (int i = 0; i < 9; i++) {
save_tx_test_now += SAVE_TX_BACKUP_GAP_S;
write_save(p.work, i & 1 ? "wa" : "wb");
CHECK(save_tx_commit(&p, backup, sizeof backup) == 1 && backup[0]);
}
CHECK(count_dirs(p.backups) == SAVE_TX_BACKUPS + 1 && is_save(own, "me"));
CHECK(is_save(backup, "wa") && is_save(p.save, "wb")); /* (the last backup is the save before the last) */
}
{ /* (two backups in one second get two names) */
save_tx_last_backup = 0;
write_save(p.work, "x1");
CHECK(save_tx_commit(&p, backup, sizeof backup) == 1 && backup[0]);
save_tx_last_backup = 0;
char second[HOS_FS_PATH];
write_save(p.work, "x2");
CHECK(save_tx_commit(&p, second, sizeof second) == 1 && second[0] && strcmp(second, backup));
CHECK(is_save(second, "x1") && count_dirs(p.backups) == SAVE_TX_BACKUPS + 1);
}
/* changes no Commit took are gone at the next start */
write_save(p.work, "zz");
new_run();
CHECK(save_tx_mount(&p, note, sizeof note) == 1 && !note[0]);
CHECK(is_save(p.work, "x2") && is_save(p.save, "x2"));
/* a Commit killed before each of its steps, then the next start */
int reached_end = 0;
for (int at = 1; at <= 12 && !reached_end; at++) {
char sub[32];
snprintf(sub, sizeof sub, "%s\\k%d", argv[1], at);
snprintf(user, sizeof user, "%s", sub);
save_tx_paths(&p, user, "0123456789abcdef0000000000000001");
new_run();
CHECK(save_tx_mount(&p, NULL, 0) == 1);
write_save(p.work, "o1");
CHECK(save_tx_commit(&p, NULL, 0) == 1 && is_save(p.save, "o1"));
write_save(p.work, "n2");
save_tx_crash_at = at, save_tx_step = 0, save_tx_dead = 0;
const int r = save_tx_commit(&p, NULL, 0);
const int died = save_tx_dead;
if (!died) reached_end = 1; /* (more steps asked for than a Commit has: it ran whole) */
new_run();
CHECK(save_tx_mount(&p, note, sizeof note) == 1);
const int old = is_save(p.save, "o1"), new_ = is_save(p.save, "n2");
if (!(old ^ new_)) { printf("FAIL: killed before step %d, the save is neither the old nor the new one\n", at); fails++; }
if (r == 1 && !new_) { printf("FAIL: killed before step %d after the commit point, the save is not the new one\n", at); fails++; }
if (r != 1 && died && !old) { printf("FAIL: killed before step %d before the commit point, the save is not the old one\n", at); fails++; }
CHECK(is_save(p.work, new_ ? "n2" : "o1") && clean_tx(&p));
/* (and the game saves on from there) */
write_save(p.work, "n3");
CHECK(save_tx_commit(&p, NULL, 0) == 1 && is_save(p.save, "n3") && clean_tx(&p));
}
CHECK(reached_end);
/* a staging folder that cannot be made: the files copied over the save's, as before the transactions */
snprintf(user, sizeof user, "%s\\f", argv[1]);
save_tx_paths(&p, user, "0123456789abcdef0000000000000001");
new_run();
CHECK(save_tx_mount(&p, NULL, 0) == 1);
write_save(p.work, "f1");
CHECK(save_tx_commit(&p, NULL, 0) == 1);
{ FILE *f = fopen(p.staging, "wb"); if (f) fclose(f); } /* (a file where the staging folder goes) */
write_save(p.work, "f2");
CHECK(save_tx_commit(&p, NULL, 0) == 0 && is_save(p.save, "f2"));
DeleteFileA(p.staging);
write_save(p.work, "f3");
CHECK(save_tx_commit(&p, NULL, 0) == 1 && is_save(p.save, "f3") && clean_tx(&p));
#ifdef _WIN32
/* a save folder another program holds open */
snprintf(user, sizeof user, "%s\\h", argv[1]);
save_tx_paths(&p, user, "0123456789abcdef0000000000000001");
new_run();
CHECK(save_tx_mount(&p, NULL, 0) == 1);
write_save(p.work, "h1");
CHECK(save_tx_commit(&p, NULL, 0) == 1 && is_save(p.save, "h1"));
char held[HOS_FS_PATH];
snprintf(held, sizeof held, "%s\\option.sav", p.save);
HANDLE h = CreateFileA(held, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
CHECK(h != INVALID_HANDLE_VALUE);
write_save(p.work, "h2");
CHECK(save_tx_commit(&p, NULL, 0) == 1);
CHECK(is_save(p.save, "h1") && tx_ready_highest(&p, NULL, 0) == 1); /* (committed, waiting) */
new_run();
CHECK(save_tx_mount(&p, note, sizeof note) == 1 && note[0]);
CHECK(is_save(p.work, "h2") && is_save(p.save, "h1")); /* (the next start plays from the newest save) */
write_save(p.work, "h3");
CHECK(save_tx_commit(&p, NULL, 0) == 1 && tx_ready_highest(&p, NULL, 0) == 2 && is_save(p.save, "h1"));
CloseHandle(h);
write_save(p.work, "h4");
CHECK(save_tx_commit(&p, NULL, 0) == 1 && is_save(p.save, "h4") && clean_tx(&p));
/* (and held again, then freed before the next start: that start puts the waiting save in place) */
h = CreateFileA(held, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
write_save(p.work, "h5");
CHECK(save_tx_commit(&p, NULL, 0) == 1 && is_save(p.save, "h4"));
CloseHandle(h);
new_run();
CHECK(save_tx_mount(&p, note, sizeof note) == 1 && note[0] && is_save(p.save, "h5") && is_save(p.work, "h5") && clean_tx(&p));
#endif
if (!fails) printf("PASS\n");
return fails != 0;
}
"""
class SaveTxTests(unittest.TestCase):
def test_transactions(self):
compiler = shutil.which("gcc") or shutil.which("clang")
if not compiler:
self.skipTest("no C compiler")
if sys.platform != "win32":
self.skipTest("the fixture calls the Windows file functions (the game's Linux build has them from host/posix)")
with tempfile.TemporaryDirectory(prefix="triforce-save-tx-") 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)], text=True, capture_output=True)
self.assertEqual(build.returncode, 0, build.stderr)
work = tmp / "saves"
work.mkdir()
run = subprocess.run([str(binary), str(work)], text=True, capture_output=True, timeout=120)
self.assertEqual(run.returncode, 0, run.stdout + run.stderr)
self.assertIn("PASS", run.stdout)
if __name__ == "__main__":
raise SystemExit(unittest.main(verbosity=1))

243
tests/test_sprint.py Normal file
View File

@@ -0,0 +1,243 @@
"""The sprint button (host/oot_sprint.h, nx2/oot_patches.py patch_sprint).
Link sprints while his input flags hold the dash input, which the game sets only out of a roll. With a button chosen,
the host puts the bit in while the button's sprint is on. The rules checked here, without the game:
- Off: nothing is taken from the controller and no sprint starts.
- Toggle with the left stick's click: a press in gameplay starts the sprint, the next ends it; the click never reaches
the game in gameplay; standing still ends a sprint that had moved after 4 frames (60 Hz frames: 8 updates at 120 fps do
not end it, 9 do), one that has not moved yet after 90; a state the list does not know ends it at once; ZL ends it.
- A press with no update of Link for 8 polls (a menu, a cutscene) is the game's and starts nothing.
- B: a sprint only for a press that begins while Link moves, kept from the game until it is let go; a press while
standing is the game's own B, whole.
- Hold: on while the button is held, off when it is let go.
- Changing the button ends a sprint.
The patch: the two calls go in once each, a second run changes nothing, other code is left as it is.
python -I -B tests/test_sprint.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 <stdio.h>
#include "oot_sprint.h"
static int fails;
#define CHECK(cond, what) do { if (!(cond)) { fails++; printf("FAIL line %d: %s\n", __LINE__, what); } } while (0)
static OotSprint s;
static void fresh(void) { OotSprint z = OOT_SPRINT_INIT; s = z; }
/* one frame: Link's update, then the controller's poll; the buttons the game gets */
static int on;
static uint64_t frame(int kind, int button, int hold, double frames, uint64_t buttons) {
on = oot_sprint_step(&s, kind, button, hold, frames);
oot_sprint_pad(&s, button, &buttons);
return buttons;
}
int main(void) {
const uint64_t LS = OOT_SPRINT_BIT_LS, B = OOT_SPRINT_BIT_B, ZL = OOT_SPRINT_BIT_ZL, A = 1;
uint64_t got;
/* the states */
CHECK(oot_sprint_kind(1) == OOT_SPRINT_MOVE && oot_sprint_kind(13) == OOT_SPRINT_MOVE && oot_sprint_kind(68) == OOT_SPRINT_MOVE, "Walk, Jump, Clamber move");
CHECK(oot_sprint_kind(35) == OOT_SPRINT_KEEP && oot_sprint_kind(253) == OOT_SPRINT_KEEP, "Slide, StepOn keep");
CHECK(oot_sprint_kind(40) == OOT_SPRINT_WAIT, "Wait waits");
CHECK(oot_sprint_kind(0) == OOT_SPRINT_STOP && oot_sprint_kind(-1) == OOT_SPRINT_STOP && oot_sprint_kind(999) == OOT_SPRINT_STOP, "anything else stops");
/* off */
fresh();
got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_OFF, 0, 1, LS | A);
got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_OFF, 0, 1, LS | A);
CHECK(got == (LS | A) && !on, "off: the click is the game's, no sprint");
/* toggle, the stick's click */
fresh();
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0);
got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS | A); /* the press: seen by the next update */
CHECK(got == A, "the click is kept from the game in gameplay");
got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS);
CHECK(on && got == 0, "the press started the sprint");
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0);
CHECK(on, "it stays on with the button let go");
for (int i = 0; i < 4; i++) frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, 0);
CHECK(on, "four frames of standing do not end a sprint that moved");
frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, 0);
CHECK(!on, "the fifth does");
/* the next press starts again, the one after ends it */
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS);
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0);
CHECK(on, "on again");
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS);
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0);
CHECK(!on, "the next press ends it");
/* at 120 fps the standing time is the same 4 frames of 60 Hz */
fresh();
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 0.5, 0);
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 0.5, LS);
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 0.5, 0);
CHECK(on, "on at 120 fps");
for (int i = 0; i < 8; i++) frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 0.5, 0);
CHECK(on, "eight updates of half a frame do not end it");
frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 0.5, 0);
CHECK(!on, "the ninth does");
/* a press while standing: on, and 90 frames to start moving */
fresh();
frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, 0);
frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, LS);
frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, 0);
CHECK(on, "a press while standing arms the sprint");
for (int i = 0; i < 89; i++) frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, 0);
CHECK(on, "still armed after 89 frames");
frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, 0);
frame(OOT_SPRINT_WAIT, OOT_SPRINT_LS, 0, 1, 0);
CHECK(!on, "not after 91");
/* an unknown state, and ZL */
fresh();
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0);
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS);
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0);
frame(OOT_SPRINT_KEEP, OOT_SPRINT_LS, 0, 1, 0);
CHECK(on, "a slide keeps it");
frame(OOT_SPRINT_STOP, OOT_SPRINT_LS, 0, 1, 0);
CHECK(!on, "a state the list does not know ends it");
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS);
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, ZL);
CHECK(on, "on (ZL is seen by the next update)");
got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, ZL);
CHECK(!on && got == ZL, "targeting ends it, and ZL is the game's");
/* no update of Link: a menu. The press is the game's and starts nothing */
fresh();
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0);
for (int i = 0; i < 9; i++) { uint64_t b = 0; oot_sprint_pad(&s, OOT_SPRINT_LS, &b); }
got = LS;
oot_sprint_pad(&s, OOT_SPRINT_LS, &got);
CHECK(got == LS, "in a menu the click reaches the game");
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS);
CHECK(!on, "and starts no sprint when play goes on with it still held");
got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS);
CHECK(!on && got == LS, "the whole press stays the game's");
/* B */
fresh();
frame(OOT_SPRINT_WAIT, OOT_SPRINT_B, 0, 1, 0);
got = frame(OOT_SPRINT_WAIT, OOT_SPRINT_B, 0, 1, B);
CHECK(got == B, "B while standing is the game's B");
got = frame(OOT_SPRINT_WAIT, OOT_SPRINT_B, 0, 1, B);
CHECK(!on && got == B, "and no sprint");
frame(OOT_SPRINT_MOVE, OOT_SPRINT_B, 0, 1, 0);
got = frame(OOT_SPRINT_MOVE, OOT_SPRINT_B, 0, 1, B);
CHECK(got == 0, "B while moving is kept from the game");
got = frame(OOT_SPRINT_WAIT, OOT_SPRINT_B, 0, 1, B);
CHECK(got == 0, "until it is let go, whatever Link does meanwhile");
frame(OOT_SPRINT_MOVE, OOT_SPRINT_B, 0, 1, 0);
got = frame(OOT_SPRINT_WAIT, OOT_SPRINT_B, 0, 1, B);
CHECK(got == B, "the next press, standing, is the game's again");
/* hold */
fresh();
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 1, 1, 0);
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 1, 1, LS);
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 1, 1, LS);
CHECK(on, "hold: on while held");
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 1, 1, 0);
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 1, 1, 0);
CHECK(!on, "hold: off when let go");
/* another button chosen */
fresh();
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0);
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS);
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, 0);
CHECK(on, "on with the click");
frame(OOT_SPRINT_MOVE, OOT_SPRINT_B, 0, 1, 0);
CHECK(!on, "choosing B ends it");
frame(OOT_SPRINT_MOVE, OOT_SPRINT_LS, 0, 1, LS);
frame(OOT_SPRINT_MOVE, OOT_SPRINT_OFF, 0, 1, 0);
CHECK(!on && !s.active && !s.pressed, "off leaves nothing on");
printf(fails ? "sprint: FAILED\n" : "sprint: all checks passed\n");
return fails != 0;
}
'''
LIFTED = (
'#include "a64.h"\n'
"void f_804caa80(Ctx *__restrict A64_NOESC c) {\n"
" c->x[10]=recomp_load64(c,c->x[10] + 0);\n" # (the same load elsewhere in the function: left)
"L_804ce790:;\n"
+ oot_patches.SPRINT_LINK_OLD +
"if (((c->x[9] >> 20) & 1)) { goto L_804cfa90; }\n"
"if (((c->x[10] >> 4) & 1)) { goto L_804ced50; }\n"
"}\n"
"\nvoid f_80c225d0(Ctx *__restrict A64_NOESC c) {\n"
+ oot_patches.SPRINT_STANCE_OLD +
"}\n"
"\nvoid f_80000000(Ctx *__restrict A64_NOESC c) {\n" # (another function with the same lines: left)
+ oot_patches.SPRINT_LINK_OLD + oot_patches.SPRINT_STANCE_OLD +
"}\n"
)
class SprintTest(unittest.TestCase):
def test_rules(self):
compiler = shutil.which("gcc") or shutil.which("clang")
if not compiler:
self.skipTest("no C compiler")
with tempfile.TemporaryDirectory(prefix="sprint-") as d:
tmp = Path(d)
(tmp / "t.c").write_text(FIXTURE, encoding="utf-8")
binary = tmp / ("t.exe" if sys.platform == "win32" else "t")
build = subprocess.run([compiler, "-std=gnu11", "-Wall", "-Wextra", "-O1", "-I", str(ROOT / "host"),
str(tmp / "t.c"), "-o", str(binary)], 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)
self.assertIn("all checks passed", run.stdout)
def test_patch(self):
out, n = oot_patches.patch_sprint(LIFTED)
self.assertEqual(n, 2)
self.assertEqual(out.count("hos_oot_sprint_link(c,c->x[19],c->x[10]);"), 1)
self.assertEqual(out.count("hos_oot_sprint_stance(c,c->x[19],c->x[8]);"), 1)
self.assertEqual(out.count(oot_patches.SPRINT_DECL), 1)
link = out.index("hos_oot_sprint_link(c,")
self.assertLess(out.index("c->x[10]=recomp_load64(c,c->x[19] + 80);"), link)
self.assertLess(link, out.index("if (((c->x[9] >> 20) & 1))")) # (before both tests of the flags)
stance = out.index("hos_oot_sprint_stance(c,")
self.assertLess(stance, out.index("goto L_80c27f54;"))
self.assertIn(oot_patches.SPRINT_LINK_OLD + oot_patches.SPRINT_STANCE_OLD, out) # (the other function: as it was)
self.assertEqual(oot_patches.patch_sprint(out), (out, 2)) # (a second run)
def test_other_code_left(self):
other = LIFTED.replace("c->x[10]=recomp_load64(c,c->x[19] + 80);", "c->x[10]=recomp_load64(c,c->x[19] + 88);", 1)
out, n = oot_patches.patch_sprint(other)
self.assertEqual(n, 1) # (the stance's alone)
self.assertNotIn("hos_oot_sprint_link(c,", out)
def test_wired(self):
hid = (ROOT / "host/nn_hid.c").read_text(encoding="utf-8")
self.assertIn("hos_oot_sprint_pad(&buttons);", hid)
api = (ROOT / "gpu/vk/vk_api.h").read_text(encoding="utf-8")
self.assertIn("HOS_OPT_SPRINT, HOS_OPT_SPRINT_HOLD", api)
replay = (ROOT / "gpu/vk/vk_replay.cpp").read_text(encoding="utf-8")
self.assertIn("if (id == HOS_OPT_SPRINT) return opts.sprint_button;", replay)
self.assertIn("if (id == HOS_OPT_SPRINT_HOLD) return opts.sprint_mode;", replay)
if __name__ == "__main__":
raise SystemExit(unittest.main(verbosity=1))

143
tests/test_spv_exact.cpp Normal file
View File

@@ -0,0 +1,143 @@
// spv_exact::scan (gpu/vk/spv_exact.h): the texture bindings a shader reads by texel position or asks the size of.
// tests/test_spv_exact.py
#include "../gpu/vk/spv_exact.h"
#include <cstdio>
#include <cstdlib>
#include <initializer_list>
static int failures = 0;
static void expect(bool ok, const char* what) {
if (!ok) {
std::printf("FAIL: %s\n", what);
failures++;
}
}
struct Module {
std::vector<uint32_t> w{0x07230203u, 0x00010500u, 0, 100, 0};
void op(uint16_t code, std::initializer_list<uint32_t> args) {
w.push_back((uint32_t)(args.size() + 1) << 16 | code);
for (uint32_t a : args) w.push_back(a);
}
// a combined image sampler variable `var` at `binding`: dim 1 is 2D, 2 is 3D
void texture(uint32_t var, uint32_t binding, uint32_t dim, bool arrayed, bool ms = false, uint32_t sampled = 1, bool in_array = false) {
const uint32_t base = var * 10;
op(71, {var, 33, binding}); // OpDecorate Binding
op(71, {var, 34, 0}); // OpDecorate DescriptorSet
op(25, {base + 1, 1, dim, 0, arrayed ? 1u : 0u, ms ? 1u : 0u, sampled, 0}); // OpTypeImage
op(27, {base + 2, base + 1}); // OpTypeSampledImage
uint32_t t = base + 2;
if (in_array) op(28, {base + 3, t, 2}), t = base + 3; // OpTypeArray
op(32, {base + 4, 0, t}); // OpTypePointer
op(59, {base + 4, var, 0}); // OpVariable
}
// the image of the variable, loaded: returns its id
uint32_t image(uint32_t var, uint32_t id, bool through_chain = false) {
uint32_t from = var;
if (through_chain) op(65, {0, id + 2, var, 7}), from = id + 2; // OpAccessChain
op(61, {0, id, from}); // OpLoad
op(100, {0, id + 1, id}); // OpImage
return id + 1;
}
spv_exact::Bindings scan() const { return spv_exact::scan(w.data(), w.size()); }
};
int main() {
using spv_exact::has;
using spv_exact::any;
{ // sampled only: nothing
Module m;
m.texture(10, 3, 1, false);
m.op(61, {0, 200, 10}); // OpLoad
m.op(87, {0, 201, 200, 202}); // OpImageSampleImplicitLod
expect(!any(m.scan()), "a sampled read marks nothing");
}
{ // a fetch
Module m;
m.texture(10, 3, 1, false);
m.texture(11, 4, 1, false);
m.op(95, {0, 300, m.image(10, 200), 301}); // OpImageFetch
const auto b = m.scan();
expect(has(b, 3) && !has(b, 4), "a fetch marks its binding and no other");
}
{ // a fetch through an access chain into an array of textures
Module m;
m.texture(10, 9, 1, false, false, 1, true);
m.op(95, {0, 300, m.image(10, 200, true), 301});
expect(has(m.scan(), 9), "a fetch through an array of textures marks the array's binding");
}
{ // the size of a 2D texture
Module m;
m.texture(10, 5, 1, false);
m.op(103, {0, 300, m.image(10, 200), 301}); // OpImageQuerySizeLod
m.op(81, {0, 302, 300, 0}); // .x
expect(has(m.scan(), 5), "a size query on a 2D texture marks it");
}
{ // the number of levels
Module m;
m.texture(10, 6, 1, true);
m.op(106, {0, 300, m.image(10, 200)}); // OpImageQueryLevels
expect(has(m.scan(), 6), "a level count query marks it");
}
{ // the layer count of an array alone
Module m;
m.texture(10, 7, 1, true);
m.op(103, {0, 300, m.image(10, 200), 301});
m.op(81, {0, 302, 300, 2}); // .z
m.op(81, {0, 303, 300, 2});
expect(!any(m.scan()), "an array's size used for its layer count alone marks nothing");
}
{ // an array's size used for its width too
Module m;
m.texture(10, 7, 1, true);
m.op(103, {0, 300, m.image(10, 200), 301});
m.op(81, {0, 302, 300, 2});
m.op(81, {0, 303, 300, 0});
expect(has(m.scan(), 7), "an array's size used for its width marks it");
}
{ // an array's size handed on whole
Module m;
m.texture(10, 7, 1, true);
m.op(103, {0, 300, m.image(10, 200), 301});
m.op(111, {0, 302, 300}); // OpConvertSToF of the whole vector
expect(has(m.scan(), 7), "an array's size used whole marks it");
}
{ // a 2D texture's third component does not exist: any use of its size counts
Module m;
m.texture(10, 8, 1, false);
m.op(103, {0, 300, m.image(10, 200), 301});
expect(has(m.scan(), 8), "a 2D texture's size query marks it whatever is done with it");
}
{ // the kinds the texture detail never touches
Module m;
m.texture(10, 1, 2, false); // 3D
m.texture(11, 2, 1, false, true); // multisampled
m.texture(12, 3, 1, false, false, 2); // a storage image
m.op(95, {0, 300, m.image(10, 200), 301});
m.op(95, {0, 310, m.image(11, 210), 311});
m.op(104, {0, 320, m.image(12, 220)}); // OpImageQuerySize
expect(!any(m.scan()), "3D, multisampled and storage images mark nothing");
}
{ // two textures, one fetched and one sampled, bindings past 63
Module m;
m.texture(10, 70, 1, false);
m.texture(11, 200, 1, true);
m.op(95, {0, 300, m.image(11, 200), 301});
const auto b = m.scan();
expect(has(b, 200) && !has(b, 70) && !has(b, 255) && !has(b, 300), "bindings up to 255");
}
{ // not SPIR-V, or cut short
const uint32_t junk[] = {1, 2, 3, 4, 5, 6};
expect(!any(spv_exact::scan(junk, 6)), "another magic number");
Module m;
m.texture(10, 3, 1, false);
m.op(95, {0, 300, m.image(10, 200), 301});
m.w.push_back(9u << 16 | 95); // an instruction longer than what is left
expect(has(m.scan(), 3), "a module cut short keeps what was read");
expect(!any(spv_exact::scan(m.w.data(), 3)), "shorter than a header");
}
if (failures) return EXIT_FAILURE;
std::printf("spv exact: all checks passed\n");
return EXIT_SUCCESS;
}

38
tests/test_spv_exact.py Normal file
View File

@@ -0,0 +1,38 @@
"""Compile and run the check for texture bindings read by texel position (gpu/vk/spv_exact.h; no game/GPU)."""
import argparse
import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cxx", "g++"]
ROOT = Path(__file__).resolve().parents[1]
def check_source():
"""The shader modules are still scanned, and a draw still asks for a texture whole at such a binding"""
shader = (ROOT / "gpu/vk/vk_shader.cpp").read_text(encoding="utf-8")
draw = (ROOT / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8")
texture = (ROOT / "gpu/vk/vk_texture.cpp").read_text(encoding="utf-8")
for src, name, needles in ((shader, "vk_shader.cpp", ('#include "spv_exact.h"', "spv_exact::scan(")),
(draw, "vk_draw.cpp", ("texture_want_whole(tex)",)),
(texture, "vk_texture.cpp", ("void texture_want_whole(Texture* tex)", "whole_keys.count(key)"))):
for needle in needles:
if needle not in src:
raise SystemExit(name + " no longer has: " + needle)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="g++")
args = parser.parse_args()
check_source()
with tempfile.TemporaryDirectory(prefix="spv-exact-") as tmp:
exe = Path(tmp) / "spv-exact"
subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror",
str(ROOT / "tests/test_spv_exact.cpp"), "-o", str(exe)], check=True)
subprocess.run([str(exe)], check=True)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,190 @@
"""NVIDIA's Streamline files for DLSS frame generation (scripts/streamline_sdk.py, nx2/make_game.py place_dlssg_runtime).
Without the network: an archive made here stands for NVIDIA's release, and the script's file table is pointed at it.
- every listed file is taken out of the archive into the SDK folder at its own path, also from an archive whose paths
have a folder in front; a second fetch downloads nothing;
- a file whose SHA-256 is another than the pinned one stops the fetch, and is not left in the folder;
- a file missing from the archive stops the fetch;
- the archive read by HTTP ranges gives the same files, and far less than the archive is read; a server that serves
no ranges gets the archive read whole;
- the game folder gets the DLLs and the two license texts in dlssg, and loses them again;
- make_game.py puts them there only for a complete SDK folder, and takes them away for a build without it.
python -I -B tests/test_streamline_sdk.py
"""
import hashlib
import importlib.util
import io
import os
import sys
import tempfile
import unittest
import zipfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def load(name, path):
spec = importlib.util.spec_from_file_location(name, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
sl = load("streamline_sdk", ROOT / "scripts" / "streamline_sdk.py")
def content(name):
return (("the file %s\n" % name) * 400).encode()
def fake_tables():
"""The script's three tables with the SHA-256 of this test's own contents"""
def with_digests(table):
return [(name, hashlib.sha256(content(name)).hexdigest()) for name, _ in table]
return with_digests(sl.HEADERS), with_digests(sl.RUNTIME), with_digests(sl.LICENSES)
def archive_bytes(prefix="", leave_out=(), change=()):
data = io.BytesIO()
with zipfile.ZipFile(data, "w", zipfile.ZIP_DEFLATED) as z:
z.writestr(prefix + "docs/big.bin", os.urandom(600000)) # (what a fetch has no reason to read)
for name, _ in sl.HEADERS + sl.RUNTIME + sl.LICENSES:
if name in leave_out:
continue
z.writestr(prefix + name, content(name) + (b"changed" if name in change else b""))
z.writestr(prefix + "bin/x64/other.dll", os.urandom(300000))
return data.getvalue()
class FakeResponse:
def __init__(self, data, status):
self.data, self.status = data, status
def read(self):
return self.data
def __enter__(self):
return self
def __exit__(self, *a):
return False
class StreamlineSdkTest(unittest.TestCase):
def setUp(self):
self.saved = (sl.HEADERS, sl.RUNTIME, sl.LICENSES)
sl.HEADERS, sl.RUNTIME, sl.LICENSES = fake_tables()
self.tmp = tempfile.TemporaryDirectory(prefix="streamline-sdk-")
self.dir = Path(self.tmp.name)
self.said = []
def tearDown(self):
sl.HEADERS, sl.RUNTIME, sl.LICENSES = self.saved
self.tmp.cleanup()
def say(self, line):
self.said.append(line)
def test_fetch_from_archive(self):
sdk = self.dir / "sdk"
sl.fetch(str(sdk), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes())))
self.assertEqual(sl.missing(str(sdk)), [])
self.assertTrue(sl.complete(str(sdk)))
self.assertTrue((sdk / "include" / "sl_dlss_g.h").is_file())
self.assertTrue((sdk / "bin" / "x64" / "nvngx_dlssg.dll").is_file())
self.assertFalse((sdk / "bin" / "x64" / "other.dll").exists())
self.said.clear()
sl.fetch(str(sdk), say=self.say, archive=None) # (nothing is missing: no archive is opened at all)
self.assertIn("already", self.said[0])
def test_folder_in_front(self):
sdk = self.dir / "sdk"
sl.fetch(str(sdk), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes("streamline-sdk-v2/"))))
self.assertTrue(sl.complete(str(sdk)))
def test_only_what_is_missing(self):
sdk = self.dir / "sdk"
sl.fetch(str(sdk), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes())))
victim = sdk / "bin" / "x64" / "sl.pcl.dll"
victim.write_bytes(b"damaged")
self.assertEqual(sl.missing(str(sdk)), ["bin/x64/sl.pcl.dll"])
self.assertFalse(sl.complete(str(sdk)))
sl.fetch(str(sdk), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes())))
self.assertEqual(victim.read_bytes(), content("bin/x64/sl.pcl.dll"))
def test_another_file_refused(self):
sdk = self.dir / "sdk"
with self.assertRaises(ValueError):
sl.fetch(str(sdk), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes(change=("bin/x64/sl.dlss_g.dll",)))))
self.assertFalse((sdk / "bin" / "x64" / "sl.dlss_g.dll").exists())
self.assertFalse(sl.complete(str(sdk)))
def test_missing_from_archive(self):
with self.assertRaises(ValueError):
sl.fetch(str(self.dir / "sdk"), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes(leave_out=("license.txt",)))))
def test_ranges(self):
data = archive_bytes()
asked = []
def opener(request, timeout=0):
first, last = (int(x) for x in request.headers["Range"].split("=")[1].split("-"))
asked.append(last - first + 1)
return FakeResponse(data[first:last + 1], 206)
archive, reader = sl.open_archive("https://example.invalid/sdk.zip", len(data), opener, self.say)
sdk = self.dir / "sdk"
sl.fetch(str(sdk), say=self.say, archive=archive)
self.assertTrue(sl.complete(str(sdk)))
self.assertIsNotNone(reader)
self.assertEqual(reader.fetched, sum(asked))
def test_no_ranges(self):
data = archive_bytes()
def opener(request, timeout=0):
return FakeResponse(data, 200)
archive, reader = sl.open_archive("https://example.invalid/sdk.zip", len(data), opener, self.say)
self.assertIsNone(reader)
self.assertTrue(any("read whole" in line for line in self.said))
sdk = self.dir / "sdk"
sl.fetch(str(sdk), say=self.say, archive=archive)
self.assertTrue(sl.complete(str(sdk)))
def test_game_folder(self):
sdk, game = self.dir / "sdk", self.dir / "game"
sl.fetch(str(sdk), say=self.say, archive=zipfile.ZipFile(io.BytesIO(archive_bytes())))
game.mkdir()
dst = Path(sl.install_runtime(str(sdk), str(game)))
self.assertEqual(dst, game / "dlssg")
names = sorted(p.name for p in dst.iterdir())
self.assertEqual(names, sorted(["nvngx_dlssg.dll", "sl.common.dll", "sl.dlss_g.dll", "sl.interposer.dll", "sl.pcl.dll",
"sl.reflex.dll", "streamline-LICENSE.txt", "nvidia-rtx-sdks-LICENSE.txt"]))
(dst / "the players own note.txt").write_text("kept")
self.assertTrue(sl.remove_runtime(str(game)))
self.assertEqual([p.name for p in dst.iterdir()], ["the players own note.txt"]) # (only NVIDIA's files go)
self.assertFalse(sl.remove_runtime(str(self.dir / "no such game")))
def test_real_table(self):
headers, runtime, licenses = self.saved
self.assertEqual(len(runtime), 6)
self.assertIn("include/sl_dlss_g.h", dict(headers))
for name, digest in headers + runtime + licenses:
self.assertRegex(digest, r"^[0-9a-f]{64}$", name)
self.assertEqual(set(sl.GAME_LICENSE_NAMES), {name for name, _ in licenses})
def test_make_game(self):
src = (ROOT / "nx2" / "make_game.py").read_text(encoding="utf-8")
for needle in ("fetch_streamline_sdk(a)", "place_dlssg_runtime(out)", 'os.environ["HOOKSHOT_STREAMLINE_SDK"] = sdk',
"sl.remove_runtime(out)"):
self.assertIn(needle, src)
build = (ROOT / "nx2" / "build.py").read_text(encoding="utf-8")
self.assertIn('os.environ.get("HOOKSHOT_STREAMLINE_SDK", "")', build)
self.assertIn('"-DHOOKSHOT_DLSSG"', build)
if __name__ == "__main__":
raise SystemExit(unittest.main(verbosity=1))

62
tests/test_tex_detail.cpp Normal file
View File

@@ -0,0 +1,62 @@
// tex_detail (gpu/vk/tex_detail.h): the detail level a card gets and the levels a texture loses at it.
// tests/test_tex_detail.py
#include "../gpu/vk/tex_detail.h"
#include <cstdio>
#include <cstdlib>
static int failures = 0;
static void expect(bool ok, const char* what) {
if (!ok) {
std::printf("FAIL: %s\n", what);
failures++;
}
}
int main() {
const uint64_t MB = 1ull << 20;
using namespace tex_detail;
// ---- the allowances in players' logs
expect(by_vram(11311 * MB, false) == kFull, "a 12 GB card (11,311 MB allowed): full");
expect(by_vram(11247 * MB, false) == kFull, "the same card with something else open");
expect(by_vram(15569 * MB, false) == kFull, "a 16 GB card");
expect(by_vram(31419 * MB, false) == kFull, "a 32 GB card");
expect(by_vram(9300 * MB, false) == kReduced, "a 10 GB card");
expect(by_vram(7365 * MB, false) == kReduced, "an 8 GB RX 7600");
expect(by_vram(7133 * MB, false) == kReduced, "an 8 GB RX 7700S");
expect(by_vram(4730 * MB, false) == kLow, "a 6 GB RTX 3060 Laptop");
expect(by_vram(3379 * MB, false) == kLow, "a 4 GB laptop card");
// ---- the two lines
expect(by_vram(10240 * MB, false) == kFull, "full starts at 10,240 MB");
expect(by_vram(10239 * MB, false) == kReduced, "one MB under it");
expect(by_vram(6144 * MB, false) == kReduced, "reduced starts at 6,144 MB");
expect(by_vram(6143 * MB, false) == kLow, "one MB under it");
// ---- no allowance reported: whole, as before
expect(by_vram(0, false) == kFull, "nothing reported");
expect(by_vram(0, true) == kFull, "nothing reported, integrated");
// ---- an integrated GPU never gets full, and follows the allowance below that
expect(by_vram(10361 * MB, true) == kReduced, "a handheld that reported 10,361 MB");
expect(by_vram(16000 * MB, true) == kReduced, "integrated with 16 GB allowed");
expect(by_vram(8034 * MB, true) == kReduced, "an Iris Xe (8,034 MB)");
expect(by_vram(5000 * MB, true) == kLow, "integrated with 5 GB allowed");
// ---- the switch
expect(parse(nullptr) == -1 && parse("auto") == -1 && parse("") == -1 && parse("1") == -1, "auto, unset, anything else");
expect(parse("full") == kFull && parse("reduced") == kReduced && parse("low") == kLow, "the three names");
// ---- what a texture loses
expect(skip(kFull, 2048, 2048, 12) == 0, "full: nothing");
expect(skip(kReduced, 2048, 2048, 12) == 1, "reduced: the largest level");
expect(skip(kLow, 2048, 2048, 12) == 2, "low: the two largest of a 2048");
expect(skip(kLow, 1024, 1024, 11) == 2, "low: two of a 1024");
expect(skip(kLow, 1023, 1024, 10) == 1, "low: one where a side is under 1024");
expect(skip(kLow, 512, 512, 10) == 1, "low: one of a 512");
expect(skip(kLow, 1024, 1024, 3) == 1, "low: one where two would leave a single level");
expect(skip(kReduced, 256, 256, 9) == 1, "reduced: a 256 still loses one");
expect(skip(kReduced, 255, 512, 9) == 0 && skip(kLow, 512, 128, 9) == 0, "a side under 256: whole");
expect(skip(kReduced, 1024, 1024, 1) == 0 && skip(kLow, 2048, 2048, 1) == 0, "no mip levels: whole");
expect(skip(kReduced, 1365, 1365, 11) == 1, "a terrain material array's 1365");
if (failures) return EXIT_FAILURE;
std::printf("tex detail: all checks passed\n");
return EXIT_SUCCESS;
}

35
tests/test_tex_detail.py Normal file
View File

@@ -0,0 +1,35 @@
"""Compile and run the texture detail rule (gpu/vk/tex_detail.h; no game/GPU)."""
import argparse
import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cxx", "g++"]
ROOT = Path(__file__).resolve().parents[1]
def check_source():
"""The texture setup still asks the rule, reads the switch and keeps the textures it must leave whole"""
src = (ROOT / "gpu/vk/vk_texture.cpp").read_text(encoding="utf-8")
for needle in ('#include "tex_detail.h"', "tex_detail::by_vram(", 'getenv("RX_VK_TEX_DETAIL")',
"tex_detail::skip(detail_level(), x.width, x.height, x.levels)",
"!as_target && astc && !x.sparse"):
if needle not in src:
raise SystemExit("vk_texture.cpp no longer has: " + needle)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="g++")
args = parser.parse_args()
check_source()
with tempfile.TemporaryDirectory(prefix="tex-detail-") as tmp:
exe = Path(tmp) / "tex-detail"
subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror",
str(ROOT / "tests/test_tex_detail.cpp"), "-o", str(exe)], check=True)
subprocess.run([str(exe)], check=True)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,58 @@
// gpu/vk/texture_gone.h: which textures were destroyed, by their creation number. Run by tests/test_texture_gone.py.
#include <cstdio>
#include <cstdlib>
#include "../gpu/vk/texture_gone.h"
static void require(bool ok, const char* what) {
if (!ok) {
fprintf(stderr, "FAIL: %s\n", what);
exit(1);
}
}
int main() {
using texture_gone::Record;
{
Record r;
require(r.count() == 0, "nothing went yet");
// (the upscale's output, texture 7, noted while it is there)
const uint64_t noted = r.count();
require(!r.gone_since(7, noted), "a texture noted is there while nothing went");
// (the cache lets other textures go, as in most frames while the video memory is full: the generation changed for
// each, and each cost the frame its picture at the output's size)
for (uint32_t id = 100; id < 140; id++) r.went(id);
require(r.count() == 40, "the count is the textures that went");
require(!r.gone_since(7, noted), "other textures' ends do not make a noted texture gone");
require(r.gone_since(120, noted), "a texture that went since is gone");
// (noted again later: what went before does not count, a pointer's address reused by a new texture's number)
const uint64_t later = r.count();
require(!r.gone_since(120, later), "what went before the note is not asked about");
r.went(7);
require(r.gone_since(7, noted) && r.gone_since(7, later), "the noted texture's own end is seen");
require(!r.gone_since(8, later), "and it is no other texture's");
}
{
// (no number: a texture that was never numbered cannot be told from one that went)
Record r;
require(r.gone_since(0, r.count()), "a texture without a number counts as gone");
// (a count this record never gave)
require(r.gone_since(7, 5), "a count from ahead of the record counts as gone");
}
{
// (more went since than are kept, an area's change: it cannot be told, and it counts as gone, the old rule's answer)
Record r;
const uint64_t noted = r.count();
for (uint32_t i = 0; i < Record::kKept; i++) r.went(1000 + i);
require(!r.gone_since(7, noted), "as many as are kept: still told");
require(r.gone_since(1000, noted), "the oldest kept is still seen");
r.went(5000);
require(r.gone_since(7, noted), "one more than are kept: not told, gone");
// (and a note taken since is told again)
const uint64_t later = r.count();
r.went(5001);
require(!r.gone_since(7, later) && r.gone_since(5001, later), "a later note is told from what is kept");
}
printf("PASS: a kept texture is gone only when it was destroyed itself, or when it cannot be told\n");
return 0;
}

View File

@@ -0,0 +1,99 @@
"""A texture the temporal upscaler keeps from one draw to a later one is given up only when that texture itself was destroyed.
With the engine (NVIDIA DLSS, AMD FSR 4) before the game's bloom and scaling up, the tone mapping is drawn once more at the
output's size from the engine's picture, into the upscale's output as the last frame's dispatch had it. That output, and the
scene's colour and depth kept from the scene's draws, were checked with texture_destroy_generation, which changes whenever
any texture is destroyed. On a card whose video memory is full the cache lets textures go in most frames (a tester's 8 GB
card at 2560x1440: 154 times in some 2300 frames), and each of those frames was shown from the scene's size scaled up in
place of the engine's picture: a soft frame, reported as a blur that comes and goes.
What must hold: gpu/vk/texture_gone.h tells one texture's end from another's (compiled and run here, no game or GPU),
vk_texture.cpp records every texture that goes there, and vk_temporal.cpp and vk_fsr3_inputs.cpp (AMD FSR 3.1's scene depth)
ask it, not the generation, for the textures they keep (the generation stays for the views' caches, which any texture's end
may make stale).
python -I -B tests/test_texture_gone.py [--cxx g++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def block(text, marker):
start = text.index(marker)
end = text.index("{", start) + 1
depth = 1
while depth:
depth += (text[end] == "{") - (text[end] == "}")
end += 1
return text[start:end]
def need(where, text, needles):
for needle in needles:
if needle not in text:
raise SystemExit(where + " no longer has: " + needle)
def check_source():
tex = (ROOT / "gpu/vk/vk_texture.cpp").read_text(encoding="utf-8")
temporal = (ROOT / "gpu/vk/vk_temporal.cpp").read_text(encoding="utf-8")
header = (ROOT / "gpu/vk/vk_texture.h").read_text(encoding="utf-8")
# (every texture's end is recorded, with the generation: destroy_later is where a texture goes)
need("vk_texture.cpp: destroy_later()", block(tex, "void destroy_later(Texture& tex)"), ["destroy_gen++;", "gone_record.went(tex.id);"])
need("vk_texture.cpp", tex, ['#include "texture_gone.h"', "texture_gone::Record gone_record;"])
need("vk_texture.cpp: texture_destroyed_count()", block(tex, "uint64_t texture_destroyed_count()"), ["return gone_record.count();"])
need("vk_texture.cpp: texture_destroyed_since()", block(tex, "bool texture_destroyed_since(uint32_t id, uint64_t since)"),
["return gone_record.gone_since(id, since);"])
need("vk_texture.h", header, ["uint64_t texture_destroyed_count();", "bool texture_destroyed_since(uint32_t id, uint64_t since);"])
# (every texture has a number: one without would always count as gone, and the engine would never draw before the bloom)
need("vk_texture.cpp: create()", block(tex, "Texture* create(const Key& key"), ["p->id = ++ids;"])
if tex.count("std::make_unique<Texture>()") != 1:
raise SystemExit("vk_texture.cpp: textures are made in another place than create(), where they get their number")
# (the upscaler: the textures it keeps are asked about themselves)
need("vk_temporal.cpp: there_still()", temporal,
["bool there_still(uint32_t id, uint64_t noted) { return !texture_destroyed_since(id, noted); }",
"bool there_still(const Camera& cam) { return there_still(cam.color_id, cam.noted) && there_still(cam.depth_id, cam.noted); }"])
need("vk_temporal.cpp: temporal_note_scene_draw()", block(temporal, "void temporal_note_scene_draw(const Texture* color0"),
["k.color_id = color0->id, k.depth_id = depth->id, k.noted = texture_destroyed_count();"])
need("vk_temporal.cpp: pre_dispatch()", block(temporal, "bool pre_dispatch(VkCommandBuffer cmd, int mode"),
["s.pre_out = out, s.pre_out_id = out->id, s.pre_out_noted = texture_destroyed_count();"])
need("vk_temporal.cpp: temporal_pre_composite()", block(temporal, "bool temporal_pre_composite(const Texture* color0"),
["!there_still(s.pre_out_id, s.pre_out_noted)"])
need("vk_temporal.cpp: temporal_pre_tonemap()", block(temporal, "Texture* temporal_pre_tonemap(VkCommandBuffer cmd)"), ["!there_still(cam)"])
# (and no texture it keeps is given up for another texture's end: the generation is left to the views' cache alone)
uses = [line.strip() for line in temporal.splitlines() if "texture_destroy_generation()" in line and not line.strip().startswith("//")]
if uses != ["const uint64_t gen = texture_destroy_generation();"]:
raise SystemExit("vk_temporal.cpp asks texture_destroy_generation for more than its views' cache: " + "; ".join(uses))
need("vk_temporal.cpp: depth_view()", block(temporal, "VkImageView depth_view(Texture* t)"),
["const uint64_t gen = texture_destroy_generation();", "if (gen != s.depth_views_gen)"])
# (AMD FSR 3.1's own inputs keep the scene's depth the same way: a frame is not left to the game's upscale for another
# texture's end)
fsr3 = (ROOT / "gpu/vk/vk_fsr3_inputs.cpp").read_text(encoding="utf-8")
need("vk_fsr3_inputs.cpp", fsr3,
["k.depth = depth, k.depth_id = depth->id, k.noted = texture_destroyed_count(), k.frame = now;",
'if (texture_destroyed_since(cam.depth_id, cam.noted)) return fail("the scene depth is gone");'])
uses = [line.strip() for line in fsr3.splitlines() if "texture_destroy_generation()" in line and not line.strip().startswith("//")]
if uses != ["const uint64_t gen = texture_destroy_generation();"]:
raise SystemExit("vk_fsr3_inputs.cpp asks texture_destroy_generation for more than its views' cache: " + "; ".join(uses))
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="g++")
args = parser.parse_args()
check_source()
with tempfile.TemporaryDirectory(prefix="texture-gone-") as tmp:
exe = Path(tmp) / "texture-gone"
subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror", str(ROOT / "tests/test_texture_gone.cpp"), "-o", str(exe)],
check=True)
subprocess.run([str(exe)], check=True)
if __name__ == "__main__":
main()

213
tests/test_wide_hud.py Normal file
View File

@@ -0,0 +1,213 @@
"""The HUD on a picture wider than 16:9 (host/oot_wide_hud.h, host/oot_wide_hud.c, nx2/prompt_assets.py HUD_SIDE_PANES).
With the Insert menu's Ultrawide HUD row on Screen edges the hearts and the item slot sit in the picture's top corners
and the small map at its right edge; on Middle everything keeps the middle 16:9, as before. The rules checked here,
without the game:
- A layout in the middle: its projection times the squeeze, nothing moved. A kept one (a fade, a menu's background):
untouched. A 16:9 picture (squeeze 1) or no squeeze: untouched.
- A layout at an edge (the lab's switch): a point at the 16:9 grid's right edge lands on the picture's right edge, one
at its left edge on the picture's left edge, and the layout keeps its shape.
- A pane moved in a layout (the hearts', the item slot's, the small map's) goes as far as a whole layout moved by its
projection: a corner pane lands on the picture's corner through the middle projection.
- prompts/wide.bin: one entry per pane and copy of each layout, and one per animation key that puts a pane at the small
map's place (the keys at 0 and other curves are left); a layout that is not the one known gives none.
python -I -B tests/test_wide_hud.py
"""
import shutil
import struct
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 prompt_assets # noqa: E402
FIXTURE = r'''
#include <math.h>
#include <stdio.h>
#include "oot_wide_hud.h"
static int fails;
#define CHECK(cond, what) do { if (!(cond)) { fails++; printf("FAIL line %d: %s\n", __LINE__, what); } } while (0)
static int near(float a, float b) { return fabsf(a - b) < 1e-4f; }
/* clip x of the layout's point x through the projection (first column's m00, the move m03) */
static float clip(const float first[4], float move, float x) { return first[0] * x + move; }
int main(void) {
const float m00 = 2.0f / 3840.0f;
const float q = (16.0f / 9.0f) / (21.0f / 9.0f); /* a 21:9 picture */
float first[4], move;
/* the middle */
first[0] = m00, first[1] = first[2] = first[3] = 0, move = 0;
oot_hud_row(first, &move, q, 0, OOT_HUD_MIDDLE);
CHECK(near(clip(first, move, 1920.0f), q) && near(clip(first, move, -1920.0f), -q) && move == 0.0f, "the middle 16:9");
/* kept, and a 16:9 picture */
first[0] = m00, move = 0;
oot_hud_row(first, &move, q, 1, OOT_HUD_RIGHT);
CHECK(first[0] == m00 && move == 0.0f, "a kept layout is untouched");
oot_hud_row(first, &move, 1.0f, 0, OOT_HUD_RIGHT);
CHECK(first[0] == m00 && move == 0.0f, "a 16:9 picture is untouched");
oot_hud_row(first, &move, 0.0f, 0, OOT_HUD_LEFT);
CHECK(first[0] == m00 && move == 0.0f, "no squeeze: untouched");
/* the edges */
first[0] = m00, move = 0;
oot_hud_row(first, &move, q, 0, OOT_HUD_RIGHT);
CHECK(near(clip(first, move, 1920.0f), 1.0f), "the grid's right edge on the picture's right edge");
CHECK(near(clip(first, move, 1920.0f) - clip(first, move, -1920.0f), 2.0f * q), "its shape kept");
first[0] = m00, move = 0;
oot_hud_row(first, &move, q, 0, OOT_HUD_LEFT);
CHECK(near(clip(first, move, -1920.0f), -1.0f), "the grid's left edge on the picture's left edge");
/* a pane moved in the layout: as far as the projection would move it */
const float out = oot_hud_out(q);
first[0] = m00, move = 0;
oot_hud_row(first, &move, q, 0, OOT_HUD_MIDDLE);
CHECK(near(clip(first, move, 1920.0f + out), 1.0f), "the right corner pane on the picture's corner");
CHECK(near(clip(first, move, -1920.0f - out), -1.0f), "the left corner pane on the picture's corner");
CHECK(near(clip(first, move, 1626.0f + out) - 1.0f, clip(first, move, 1626.0f) - q), "a pane beside it keeps its distance to the edge");
CHECK(oot_hud_out(1.0f) == 0.0f && oot_hud_out(0.0f) == 0.0f && oot_hud_out(1.5f) == 0.0f, "nothing on a 16:9 or narrower picture");
CHECK(near(oot_hud_out((16.0f / 9.0f) / (32.0f / 9.0f)), 1920.0f), "32:9: a whole half grid further out");
/* the classes */
CHECK(oot_hud_keep_bit(0x84c04fb0ULL) == 2u && oot_hud_keep_bit(0x84c08300ULL) == 0u, "the menus' background is kept, the map's screen is not");
printf(fails ? "wide hud: FAILED\n" : "wide hud: all checks passed\n");
return fails != 0;
}
'''
def layout(name, panes, curves=()):
"""a made-up layout as nx2/prompt_assets.py reads one: "BFFH", the names' and the records' tables' offsets at +0x118 and
+0x120, the count at +0x130, the name before "RootPane" among the texts; panes: [(name, x)]; curves: [(value past the
last key, [(value, slope)])], each an array as layout_keys reads one, 8-aligned, the first value 8 bytes before it"""
names = ["RootPane"] + [n for n, _ in panes]
xs = [0.0] + [x for _, x in panes]
n = len(names)
head = 0x140
names_at, recs_at = head, head + 8 * n
rec0 = recs_at + 8 * n
texts_at = rec0 + 0x44 * n
texts = b"\0" + name.encode() + b"\0"
offs = []
for t in names:
offs.append(texts_at + len(texts))
texts += t.encode() + b"\0"
b = bytearray(texts_at) + texts
b[0:4] = b"BFFH"
struct.pack_into("<QQ", b, 0x118, names_at, recs_at)
struct.pack_into("<I", b, 0x130, n)
for k in range(n):
struct.pack_into("<Q", b, names_at + 8 * k, offs[k])
struct.pack_into("<Q", b, recs_at + 8 * k, rec0 + 0x44 * k)
struct.pack_into("<f", b, rec0 + 0x44 * k + 0x1c, xs[k])
for last, keys in curves:
b += bytes(-len(b) % 8)
b += struct.pack("<ff", last, 0.0)
b += struct.pack("<QII", len(b) + 16, 2 * len(keys), 4)
for value, slope in keys:
b += struct.pack("<ff", value, slope)
return bytes(b)
GOOD = [("N_VRS_00", 1626.0), ("N_ScaleL_00", -1920.0), ("N_ScaleR_00", 1920.0), ("N_SubItemBaseFishing_00", 1270.0)]
MAP = [("N_VRS_00", 1536.0), ("N_MiniMapCap_00", 1470.0), ("N_LocalMap_00", 0.0), ("N_ItemIcon_00", 1666.0),
("N_Field_01", 1490.0), ("N_Dungeon_01", 1536.0), ("N_MiniMapReplace_00", 1470.0), ("P_MiniMapFrame_00", 1536.0),
("S_Floor_00", 1706.0), ("P_CapDungeon_00", 1536.0), ("N_PatternFieldMini_01", 0.0)]
# (the icons' layers: at 0 for the whole-screen map's patterns, at the small map's place for its own; then two curves that
# are not places: one with a slope, one with another value)
MAP_CURVES = [(0.0, [(0.0, 0.0), (0.0, 0.0), (1490.0, 0.0), (1490.0, 0.0), (0.0, 0.0)]),
(1536.0, [(0.0, 0.0), (1536.0, 0.0), (1536.0, 0.0)]),
(0.0, [(0.0, 0.0), (1.0, 0.5), (2.0, 0.0)]),
(0.0, [(0.0, 0.0), (0.5, 0.0), (1.0, 0.0)])]
def archive(status=None, the_map=None):
one = layout("PlayerStatus_00", GOOD) if status is None else status
two = layout("MapMenu_00", MAP, MAP_CURVES) if the_map is None else the_map
return b"SARC" + bytes(60) + layout("Other_00", [("N_ScaleL_00", -1920.0)]) + one + two + one + two
class WideHudTest(unittest.TestCase):
def test_rules(self):
compiler = shutil.which("gcc") or shutil.which("clang")
if not compiler:
self.skipTest("no C compiler")
with tempfile.TemporaryDirectory(prefix="widehud-") as d:
tmp = Path(d)
(tmp / "t.c").write_text(FIXTURE, encoding="utf-8")
binary = tmp / ("t.exe" if sys.platform == "win32" else "t")
build = subprocess.run([compiler, "-std=gnu11", "-Wall", "-Wextra", "-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)
self.assertIn("all checks passed", run.stdout)
def test_table(self):
blarc = archive()
got = prompt_assets.wide_panes(blarc)
# (the hearts' layout: four panes; the map's: eight panes, the two keys at 1490, the two at 1536 and that curve's
# value past its last key; two copies of each)
self.assertEqual(len(got), 2 * (4 + 8 + 2 + 2 + 1))
for off, side, x in got:
self.assertEqual(struct.unpack_from("<f", blarc, off)[0], x)
self.assertNotEqual(x, 0.0)
self.assertEqual({x for _, side, x in got if side < 0}, {-1920.0})
self.assertEqual({x for _, side, x in got if side > 0}, {1920.0, 1626.0, 1270.0, 1536.0, 1470.0, 1490.0, 1706.0})
first = blarc.index(layout("PlayerStatus_00", GOOD))
self.assertTrue(all(off >= first for off, _, _ in got)) # (the other layout's pane of the same name: left)
self.assertNotIn(1666.0, {x for _, _, x in got}) # (the whole-screen map's own pane: left)
self.assertEqual(len({off for off, _, _ in got}), len(got))
with tempfile.TemporaryDirectory(prefix="widehud-") as d:
path = Path(d) / "wide.bin"
prompt_assets.write_wide(path, got)
raw = path.read_bytes()
self.assertEqual(raw[:4], b"HPWD")
self.assertEqual(struct.unpack_from("<I", raw, 4)[0], len(got))
self.assertEqual(len(raw), 8 + 12 * len(got))
self.assertEqual(struct.unpack_from("<Ib3xf", raw, 8), got[0])
def test_keys(self):
lay = layout("MapMenu_00", MAP, MAP_CURVES)
keys = prompt_assets.layout_keys(lay, {1490.0, 1536.0})
self.assertEqual(sorted(struct.unpack_from("<f", lay, o)[0] for o in keys), [1490.0, 1490.0, 1536.0, 1536.0, 1536.0])
self.assertEqual(prompt_assets.layout_keys(lay, {1.0, 2.0}), []) # (a curve with a slope is no place)
self.assertEqual(prompt_assets.layout_keys(lay, {1.0}), []) # (nor one with another value among them)
def test_unknown_layout(self):
self.assertIsNone(prompt_assets.wide_panes(b"SARC" + bytes(60))) # (no such layout)
self.assertIsNone(prompt_assets.wide_panes(archive(status=layout("PlayerStatus_00", GOOD[:3])))) # (a pane missing)
moved = [(n, 1800.0 if n == "N_ScaleR_00" else x) for n, x in GOOD]
self.assertIsNone(prompt_assets.wide_panes(archive(status=layout("PlayerStatus_00", moved)))) # (not at the corner)
twice = layout("PlayerStatus_00", GOOD + [("N_Other_00", 1270.0)])
self.assertIsNone(prompt_assets.wide_panes(archive(status=twice))) # (a place held elsewhere)
other = layout("MapMenu_00", MAP + [("N_Else_00", 1706.0)], MAP_CURVES)
self.assertIsNone(prompt_assets.wide_panes(archive(the_map=other))) # (the same, the map's)
ramp = layout("MapMenu_00", MAP, MAP_CURVES + [(0.0, [(0.0, 0.0), (1536.0, 3.0)])])
self.assertIsNone(prompt_assets.wide_panes(archive(the_map=ramp))) # (a key that moves there)
self.assertIsNone(prompt_assets.wide_panes(archive(the_map=b""))) # (no map layout)
self.assertIsInstance(prompt_assets.WIDE, float) # (the icons' own constant: a table of this file once took its name)
def test_wired(self):
host = (ROOT / "host/oot_wide_hud.c").read_text(encoding="utf-8")
self.assertIn('hos_ini_option("ultrawide_hud", 0)', host)
prompts = (ROOT / "host/prompts.c").read_text(encoding="utf-8")
self.assertIn("hos_wide_hud_out()", prompts)
wide = (ROOT / "nx2/oot_wide.py").read_text(encoding="utf-8")
self.assertIn("hos_wide_hud_row(c, c->x[0] - 0x1b8, _row, &_tx);", wide)
menu = (ROOT / "gpu/vk/vk_menu.cpp").read_text(encoding="utf-8")
self.assertIn('"Ultrawide HUD", &opts.wide_hud, 0, 1, 1, tx_wide_hud,', menu)
if __name__ == "__main__":
unittest.main()