1 Commits

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

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-12 01:29:02 -06:00
12 changed files with 1157 additions and 19 deletions

View File

@@ -96,8 +96,8 @@ bool UseShuffleFallback(EmitContext& ctx) {
(ctx.profile.has_broken_spirv_fragment_shuffle && ctx.stage == Stage::Fragment); (ctx.profile.has_broken_spirv_fragment_shuffle && ctx.stage == Stage::Fragment);
} }
Id ShuffleAbsolute(EmitContext& ctx, Id value, Id src_thread_id) { Id ShuffleAbsolute(EmitContext& ctx, Id value, Id src_thread_id, bool broadcast) {
if (!UseShuffleFallback(ctx)) { if (!broadcast) {
return ctx.OpGroupNonUniformShuffle(ctx.U32[1], SubgroupScope(ctx), value, src_thread_id); return ctx.OpGroupNonUniformShuffle(ctx.U32[1], SubgroupScope(ctx), value, src_thread_id);
} }
Id result{ctx.u32_zero_value}; Id result{ctx.u32_zero_value};
@@ -111,8 +111,9 @@ Id ShuffleAbsolute(EmitContext& ctx, Id value, Id src_thread_id) {
} }
Id ShuffleRelative(EmitContext& ctx, Id value, Id delta, Id src_thread_id, spv::Op op) { Id ShuffleRelative(EmitContext& ctx, Id value, Id delta, Id src_thread_id, spv::Op op) {
if (!ctx.profile.support_shuffle_relative || UseShuffleFallback(ctx)) { const bool broadcast{UseShuffleFallback(ctx)};
return ShuffleAbsolute(ctx, value, HostThreadId(ctx, src_thread_id)); if (!ctx.profile.support_shuffle_relative || broadcast) {
return ShuffleAbsolute(ctx, value, HostThreadId(ctx, src_thread_id), broadcast);
} }
const Id result{ctx.OpGroupNonUniformShuffleXor(ctx.U32[1], SubgroupScope(ctx), value, delta)}; const Id result{ctx.OpGroupNonUniformShuffleXor(ctx.U32[1], SubgroupScope(ctx), value, delta)};
ctx.opcode_rewrites.emplace_back(result.value, op); ctx.opcode_rewrites.emplace_back(result.value, op);
@@ -260,7 +261,8 @@ Id EmitShuffleIndex(EmitContext& ctx, IR::Inst* inst, Id value, Id index, Id cla
return ctx.OpSelect(ctx.U32[1], in_range, BroadcastLane(ctx, value, lane.U32() & 31), return ctx.OpSelect(ctx.U32[1], in_range, BroadcastLane(ctx, value, lane.U32() & 31),
value); value);
} }
const Id shuffled{ShuffleAbsolute(ctx, value, HostThreadId(ctx, src_thread_id))}; const Id shuffled{
ShuffleAbsolute(ctx, value, HostThreadId(ctx, src_thread_id), UseShuffleFallback(ctx))};
return ctx.OpSelect(ctx.U32[1], in_range, shuffled, value); return ctx.OpSelect(ctx.U32[1], in_range, shuffled, value);
} }
@@ -316,7 +318,7 @@ Id EmitShuffleButterfly(EmitContext& ctx, IR::Inst* inst, Id value, Id index, Id
// (hookshot) through the broadcasts where shuffles are broken (UseShuffleFallback: AMD's fragment stages, Lichii #273), // (hookshot) through the broadcasts where shuffles are broken (UseShuffleFallback: AMD's fragment stages, Lichii #273),
// as the absolute and relative shuffles are; #273 left SHFL.BFLY native, and some of Kokiri Forest's plants use it // as the absolute and relative shuffles are; #273 left SHFL.BFLY native, and some of Kokiri Forest's plants use it
const Id shuffled{UseShuffleFallback(ctx) const Id shuffled{UseShuffleFallback(ctx)
? ShuffleAbsolute(ctx, value, HostThreadId(ctx, src_thread_id)) ? ShuffleAbsolute(ctx, value, HostThreadId(ctx, src_thread_id), true)
: ctx.OpGroupNonUniformShuffleXor(ctx.U32[1], SubgroupScope(ctx), value, mask)}; : ctx.OpGroupNonUniformShuffleXor(ctx.U32[1], SubgroupScope(ctx), value, mask)};
return ctx.OpSelect(ctx.U32[1], in_range, shuffled, value); return ctx.OpSelect(ctx.U32[1], in_range, shuffled, value);
} }
@@ -328,7 +330,8 @@ Id EmitQuadBroadcast(EmitContext& ctx, Id value, Id lane) {
const Id base{ctx.OpBitwiseAnd(ctx.U32[1], GetThreadId(ctx), ctx.Const(~3u))}; const Id base{ctx.OpBitwiseAnd(ctx.U32[1], GetThreadId(ctx), ctx.Const(~3u))};
const Id local_lane{ctx.OpBitwiseAnd(ctx.U32[1], lane, ctx.Const(3u))}; const Id local_lane{ctx.OpBitwiseAnd(ctx.U32[1], lane, ctx.Const(3u))};
const Id src_thread_id{ctx.OpBitwiseOr(ctx.U32[1], base, local_lane)}; const Id src_thread_id{ctx.OpBitwiseOr(ctx.U32[1], base, local_lane)};
return ShuffleAbsolute(ctx, value, src_thread_id); return ShuffleAbsolute(ctx, value, src_thread_id,
ctx.profile.has_broken_spirv_subgroup_shuffle);
} }
Id EmitQuadSwap(EmitContext& ctx, Id value, Id direction) { Id EmitQuadSwap(EmitContext& ctx, Id value, Id direction) {

View File

@@ -566,11 +566,9 @@ Shader::Profile make_profile() {
p.support_scaled_attributes = false; // (*_UI2F / *_I2F read as integers, converted in the shader: R32 has no scaled format) p.support_scaled_attributes = false; // (*_UI2F / *_I2F read as integers, converted in the shader: R32 has no scaled format)
p.support_multi_viewport = true; p.support_multi_viewport = true;
p.support_sampled_image_array_nonuniform_indexing = true; p.support_sampled_image_array_nonuniform_indexing = true;
// (hookshot) a graphics stage the GPU cannot pin to 32 lanes runs at its default: 64 on AMD, where only compute is pinned // Unpinned SPIR-V 1.6 stages can use any supported subgroup size, even when the default is 32.
// (vk_main.cpp). The translator then keeps each 32-lane guest warp to its half of the host's, as Eden does on AMD; the // The stage-specific profiles below keep the short path only where pin_subgroup actually pins 32 lanes.
// long path is right at 32 lanes too. The ballots and shuffles of the game's fragment shaders ran across both halves p.warp_size_potentially_larger_than_guest = dev.subgroup_max > 32;
p.warp_size_potentially_larger_than_guest =
dev.subgroup_size > 32 && (dev.sg32_stages & VK_SHADER_STAGE_ALL_GRAPHICS) != VK_SHADER_STAGE_ALL_GRAPHICS;
// (RX_VK_WARP64=0 or 1, a test: the graphics stages' long warp path off or on, whatever the GPU; in the cache's stamp) // (RX_VK_WARP64=0 or 1, a test: the graphics stages' long warp path off or on, whatever the GPU; in the cache's stamp)
if (const char* e = getenv("RX_VK_WARP64")) p.warp_size_potentially_larger_than_guest = e[0] == '1'; if (const char* e = getenv("RX_VK_WARP64")) p.warp_size_potentially_larger_than_guest = e[0] == '1';
p.lower_left_origin_mode = false; p.lower_left_origin_mode = false;
@@ -1225,9 +1223,11 @@ uint64_t disk_stamp() {
const uint32_t mesh = (dev.mesh_shader ? 1u : 0u) | (dev.mesh_fallback ? 2u : 0u) | (dev.task_mesh_fallback ? 4u : 0u); const uint32_t mesh = (dev.mesh_shader ? 1u : 0u) | (dev.mesh_fallback ? 2u : 0u) | (dev.task_mesh_fallback ? 4u : 0u);
add(&mesh, sizeof mesh); add(&mesh, sizeof mesh);
// (and what of the GPU the translator is told (make_profile): its warps' lanes and the driver's shuffle workaround, and // (and what of the GPU the translator is told (make_profile): its warps' lanes and the driver's shuffle workaround, and
// which stages it pins to 32 lanes, which keep the short path: profile_graphics) // which stages it pins to 32 lanes, which keep the short path: profile_graphics. An explicit warp override bypasses
// the pinned graphics profile even if the base profile already has the requested width)
const uint32_t lanes[] = {profile().warp_size_potentially_larger_than_guest ? 1u : 0u, profile().max_subgroup_size, const uint32_t lanes[] = {profile().warp_size_potentially_larger_than_guest ? 1u : 0u, profile().max_subgroup_size,
profile().has_broken_spirv_fragment_shuffle ? 1u : 0u, (uint32_t)dev.sg32_stages}; profile().has_broken_spirv_fragment_shuffle ? 1u : 0u, (uint32_t)dev.sg32_stages,
getenv("RX_VK_WARP64") ? 1u : 0u};
add(lanes, sizeof lanes); add(lanes, sizeof lanes);
// (the records keep Shader::Info as it lies in memory: a cache written by another C++ library (the MSVC build's, then the // (the records keep Shader::Info as it lies in memory: a cache written by another C++ library (the MSVC build's, then the
// llvm-mingw one's libc++) has other layouts, and reading it crashed at start. The library and the sizes, so it starts again) // llvm-mingw one's libc++) has other layouts, and reading it crashed at start. The library and the sizes, so it starts again)

47
host/frame_whole.h Normal file
View File

@@ -0,0 +1,47 @@
/* (hookshot) Whole frames out of a frame step that can be under one, for the game's counters that add the step, or the step
times a whole number, cut to whole frames (nx2/oot_patches.py patch_whole_frames). At the console's 60 fps the step is 1 and
they count frames. Under a Frame rate cap above 60 the step is the frame's own time (host/nn_vi.c, hos_frame_seconds_bits):
0.42 at 144 fps, cut to 0 every frame, and the counter never moves, so what waits on it never comes: the pick-up indicator
above Link's head counts the rupees shown up to the wallet's (f_80a87e50: the shown +864 moves by (int)(step * the
distance / 50, at least 1)) and fades only when they meet; it stayed at the old count until a door or a talk replaced it.
While the frames' own time is the step (carry), the amount is added to the counter's own remainder and the whole frames in
it are handed out, so the counter moves at the game's rate whatever the frame rate. Otherwise it is cut as the game cuts it:
60 fps as before. One remainder per counter address (or per object: each water surface keeps its own), reused oldest
first. 64 slots: a scene's water surfaces each take one every frame, and more of them than slots would evict each other in
turn, every one starting again from 0 and standing still. Not thread-safe by itself: the caller holds a lock. */
#pragma once
#include <stdint.h>
#include <string.h>
#define FRAME_WHOLE_SLOTS 64
typedef struct {
uint64_t at[FRAME_WHOLE_SLOTS];
float rem[FRAME_WHOLE_SLOTS];
uint32_t used[FRAME_WHOLE_SLOTS]; /* (the call count at the slot's last use: the oldest is reused) */
uint32_t calls;
} FrameWhole;
/* (as the lifted code cuts it: toward zero, NaN and below zero to 0, at most 0x7fffffff: the counts are small and positive) */
static inline uint32_t frame_whole_cut(float f) {
if (!(f > 0.0f)) return 0;
if (f >= 2147483648.0f) return 0x7fffffffu;
return (uint32_t)f;
}
static inline uint32_t frame_whole(FrameWhole *w, uint64_t at, uint32_t amount_bits, int carry) {
float amount;
memcpy(&amount, &amount_bits, 4);
if (!carry || !(amount > 0.0f) || amount >= 2147483648.0f) return frame_whole_cut(amount);
w->calls++;
int slot = -1, oldest = 0;
for (int i = 0; i < FRAME_WHOLE_SLOTS; i++) {
if (w->at[i] == at) { slot = i; break; }
if (w->used[i] < w->used[oldest]) oldest = i;
}
if (slot < 0) slot = oldest, w->at[slot] = at, w->rem[slot] = 0.0f;
w->used[slot] = w->calls;
const float sum = w->rem[slot] + amount;
const uint32_t whole = frame_whole_cut(sum);
w->rem[slot] = sum - (float)whole;
return whole;
}

View File

@@ -224,6 +224,59 @@ static float text_scale(const char *env, int menu) {
volatile uint64_t hos_step_probe = 0; /* (f_80706be0's object, set by the lifted code when the frame-time hook is in this build; its volatile uint64_t hos_step_probe = 0; /* (f_80706be0's object, set by the lifted code when the frame-time hook is in this build; its
+0xd58 is the game clock in 1/60 s: nx2/oot_patches.py) */ +0xd58 is the game clock in 1/60 s: nx2/oot_patches.py) */
volatile uint32_t hos_frame_seconds_bits = 0; /* (the frame's seconds as a float's bits for that hook, 0: the game's own step) */ volatile uint32_t hos_frame_seconds_bits = 0; /* (the frame's seconds as a float's bits for that hook, 0: the game's own step) */
/* A movie's frame count while the frames' time is given (a Frame rate cap above 60): how many 60ths of a second this update of
that movie player is worth, the rest carried to its next one, so the count the cutscene's cues are placed by keeps the clock
as the movie's picture does (issue 1742: the narration ran ahead above 60 fps). 1, the game's own step, otherwise, and with
HOS_FPS_MOVIE=0 (for comparisons). Called by the lifted f_83b7c710 (nx2/oot_patches.py). */
uint32_t hos_movie_frames(uint64_t player) {
enum { PLACES = 16 };
const uint32_t bits = __atomic_load_n(&hos_frame_seconds_bits, __ATOMIC_RELAXED); /* (realtime_step writes it at each present) */
if (!bits) return 1;
static SRWLOCK lock = SRWLOCK_INIT;
static int timed = -1; /* (these four under the lock) */
static struct { uint64_t player, used; double part; } carry[PLACES];
static uint64_t calls;
static unsigned made;
float secs;
uint32_t n = 1;
memcpy(&secs, &bits, 4);
AcquireSRWLockExclusive(&lock);
if (timed < 0) timed = !(getenv("HOS_FPS_MOVIE") && !atoi(getenv("HOS_FPS_MOVIE")));
if (timed) {
int i = 0;
while (i < PLACES && carry[i].player != player) i++;
if (i == PLACES) {
/* (a player not seen yet takes the place used longest ago, so a movie that plays keeps its own while others come
and go: its count is exact while no more than PLACES players, itself among them, update in turn. It starts at a part
of a 60th drawn from a hash of how many places were made: with that many players at once every update is a first
one, and each count then still grows by the frames' time on average, whatever the number of players, instead of
standing still. A sequence with a fixed step, the golden ratio's multiples, stood still for one player in 233) */
uint32_t x = made++;
x ^= x >> 16, x *= 0x85ebca6bu, x ^= x >> 13, x *= 0xc2b2ae35u, x ^= x >> 16; /* (MurmurHash3's last step) */
i = 0;
for (int k = 1; k < PLACES; k++)
if (carry[k].used < carry[i].used) i = k;
carry[i].player = player, carry[i].part = x / 4294967296.0;
}
carry[i].used = ++calls;
carry[i].part += (double)secs * 60.0;
n = (uint32_t)carry[i].part;
carry[i].part -= n;
}
ReleaseSRWLockExclusive(&lock);
return n;
}
/* (nx2/oot_patches.py patch_whole_frames) the whole frames a game counter at `at` takes for an amount of the frame step (its
float's bits): with the frames' own time as the step, their fractions carried per counter; else cut as the game cuts it */
#include "frame_whole.h"
static FrameWhole frame_whole_state;
static SRWLOCK frame_whole_lock = SRWLOCK_INIT; /* (the game's update threads) */
uint32_t hos_frame_whole(uint64_t at, uint32_t amount_bits) {
AcquireSRWLockExclusive(&frame_whole_lock);
const uint32_t whole = frame_whole(&frame_whole_state, at, amount_bits, __atomic_load_n(&hos_frame_seconds_bits, __ATOMIC_RELAXED) != 0);
ReleaseSRWLockExclusive(&frame_whole_lock);
return whole;
}
volatile uint64_t hos_res_module = 0; /* (f_831f3200's object, whose +0xe5c is the 3D's scale: nx2/oot_patches.py) */ volatile uint64_t hos_res_module = 0; /* (f_831f3200's object, whose +0xe5c is the 3D's scale: nx2/oot_patches.py) */
static void realtime_step(int64_t now) { static void realtime_step(int64_t now) {
int rx_option(int id, int otherwise); int rx_option(int id, int otherwise);
@@ -357,9 +410,9 @@ static void realtime_step(int64_t now) {
const float f = (float)(secs > 4.0 / 60.0 ? 4.0 / 60.0 : secs < 1.0 / 240.0 ? 1.0 / 240.0 : secs); const float f = (float)(secs > 4.0 / 60.0 ? 4.0 / 60.0 : secs < 1.0 / 240.0 ? 1.0 / 240.0 : secs);
uint32_t bits; uint32_t bits;
memcpy(&bits, &f, 4); memcpy(&bits, &f, 4);
hos_frame_seconds_bits = bits; __atomic_store_n(&hos_frame_seconds_bits, bits, __ATOMIC_RELAXED); /* (read by the game's threads) */
} else { } else {
hos_frame_seconds_bits = 0; __atomic_store_n(&hos_frame_seconds_bits, 0, __ATOMIC_RELAXED);
} }
const int top = timed ? 60 : fps_max(); const int top = timed ? 60 : fps_max();
double ratio = sum / count * 60.0 + carry; double ratio = sum / count * 60.0 + carry;

66
host/oot_high_fps.c Normal file
View File

@@ -0,0 +1,66 @@
/* (hookshot) The sound manager's job at 60 Hz and the physics solver's steps rounded up, under a Frame rate cap above 60: the
lifted f_80435300, f_838ceb80 and f_838cfc10 call these (nx2/oot_patches.py); host/oot_high_fps.h has why and the logic.
HOS_FPS_LOG (set): what they do, every 600 calls. */
#include "hos.h"
#include "oot_high_fps.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>
extern volatile uint32_t hos_frame_seconds_bits; /* (host/nn_vi.c: the frame's seconds, 0 at a cap of 60) */
static int timed(void) { return __atomic_load_n(&hos_frame_seconds_bits, __ATOMIC_RELAXED) != 0; }
static int fps_log(void) {
static int on = -1; /* (read by the game's threads) */
int v = __atomic_load_n(&on, __ATOMIC_RELAXED);
if (v < 0) v = getenv("HOS_FPS_LOG") != NULL, __atomic_store_n(&on, v, __ATOMIC_RELAXED);
return v;
}
static OotSoundGate sound_gate = OOT_SOUND_GATE_INIT;
static long long sound_calls, sound_runs; /* (HOS_FPS_LOG's counts, under sound_lock) */
static SRWLOCK sound_lock = SRWLOCK_INIT; /* (the job's phases may come from different threads) */
/* f_80435300's head: phase w2, slot the guest address of the pointer to the object whose +0x10 is the game step (the job's own
load). 0: the job returns at once */
int hos_oot_sound_gate(Ctx *c, uint32_t phase, uint64_t slot) {
(void)c;
float step = 1.0f;
const int t = timed();
if (t && phase == 0) {
const uint64_t object = G64(slot);
const uint64_t time = object ? G64(object) : 0;
if (time) step = oot_bits_float(*(const uint32_t *)GP(time + 0x10));
}
long long ran = -1;
AcquireSRWLockExclusive(&sound_lock);
const int run = oot_sound_gate(&sound_gate, phase, step, t);
if (phase == 0 && fps_log()) {
sound_runs += run;
if (!(++sound_calls % 600)) ran = sound_runs, sound_runs = 0;
}
ReleaseSRWLockExclusive(&sound_lock);
if (ran >= 0)
hos_log("oot: sound job ran %lld of the last 600 frames (step %.3f%s)", ran, step,
oot_sound_partial(step, t) ? ", held to 60 Hz" : "");
return run;
}
/* The step f_80435300's phase 0 hands to the sound engine (its float's bits) */
uint32_t hos_oot_sound_step(uint32_t step_bits) {
return oot_sound_step(step_bits, timed());
}
/* The solver steps of f_838ceb80 / f_838cfc10: game, the game's count; scaled_bits, the float it cut; per_frame_bits, the steps
of a 60 fps frame (a float) */
uint32_t hos_oot_solver_steps(uint32_t game, uint32_t scaled_bits, uint32_t per_frame_bits) {
const int32_t steps = oot_solver_steps((int32_t)game, scaled_bits, per_frame_bits, timed());
if (fps_log()) {
static volatile LONG64 calls;
const LONG64 n = InterlockedIncrement64(&calls);
if (n % 600 == 1)
hos_log("oot: physics solver steps %d (the game's %d: frame step %.3f x %.0f steps a 60 fps frame)", steps, (int32_t)game,
(oot_bits_float(scaled_bits) - 0.001f) / oot_bits_float(per_frame_bits), oot_bits_float(per_frame_bits));
}
return (uint32_t)steps;
}

86
host/oot_high_fps.h Normal file
View File

@@ -0,0 +1,86 @@
/* (hookshot) Two places in the game that need whole 60 fps frames, under a Frame rate cap above 60, where the frame step is the
frame's own time (host/nn_vi.c, hos_frame_seconds_bits: 0.5 at 120 fps). nx2/oot_patches.py hooks them; host/oot_high_fps.c
holds the state. At a cap of 60 (no frame time given: timed 0) both return what the game computed, so nothing changes.
The sound manager's job (f_80435300, its phase in w2): phase 0 hands the game step to the sound engine, whose fade and delay
timers and parameter ramps count updates of at least one whole step. Fed less, some sounds are not released when an area
unloads, the voices in use climb until the game's voice budget is full, and new sounds (sword, voices), then the area music,
stop until a restart. Above 60 fps the job runs at 60 Hz: phase 0 adds the step to a remainder and runs once a whole step has
built up, with a step of 1 (sound_step), keeping the rest; the job's other phases follow that frame's choice. Sound keeps the
clock on average. A step of 1 or more runs as the game has it and keeps the remainder: around 60 fps (a cap above 60 with
the game near 60, or VRR) steps of 0.98 and 1.02 alternate, and the 0.98 that did not run comes back as a whole step later
(dropped, the sound clock ran at half the real one). A step of 0 or less (or NaN), and every phase at a cap of 60, run as the
game has them, and clear the remainder.
(After zortos293/Lichii PR #1289 by auz466, GPL-3.0: the same gate as a code patch for the emulator.)
The physics world's solver steps: the game sets them each update to (int)(frame step * the steps of a 60 fps frame + 0.001)
(f_838ceb80 and f_838cfc10, into the solver's +712; 8 a 60 fps frame in the places seen). A step under 1 cuts that short,
and as the measured step wanders around its mean the count changes from frame to frame (8 x 0.49 is 3, 8 x 0.5 is 4 at 120
fps); 4 steps a 60 fps frame would be 0 above 240. A moving platform (the stone one under the spikes in the Deku Tree)
then slides out from under Link (issue 1798). While the step is under 1 the count is rounded up instead, so a solver step
never covers more time than at 60 fps, give or take 10% (the frame time's jitter, so a steady rate keeps a steady count),
and it is at least 1 and never under the game's. A step of 1 or more (a frame slower than 60 fps) keeps the game's count. */
#pragma once
#include <math.h>
#include <stdint.h>
#include <string.h>
typedef struct {
float part; /* (the steps added up since the job last ran, under 1) */
int run; /* (phase 0's choice, for the other phases of the same frame) */
} OotSoundGate;
#define OOT_SOUND_GATE_INIT {0.0f, 1} /* (every phase runs until a phase 0 has chosen) */
static inline float oot_bits_float(uint32_t bits) {
float f;
memcpy(&f, &bits, 4);
return f;
}
static inline uint32_t oot_float_bits(float f) {
uint32_t bits;
memcpy(&bits, &f, 4);
return bits;
}
/* (whether the step is a part of a frame: the only case the gate and the feed change) */
static inline int oot_sound_partial(float step, int timed) {
return timed && step > 0.0f && step < 1.0f;
}
/* 1: run the job now, 0: return from it at once */
static inline int oot_sound_gate(OotSoundGate *g, uint32_t phase, float step, int timed) {
if (!timed) { /* (a cap of 60: every phase of every frame, whatever came before) */
g->part = 0.0f;
g->run = 1;
return 1;
}
if (phase != 0) return g->run;
if (!(step > 0.0f)) { /* (0, below 0, NaN: as the game has it) */
g->part = 0.0f;
g->run = 1;
return 1;
}
if (step >= 1.0f) { /* (a whole step or more: runs with it, the remainder kept for the next partial ones) */
g->run = 1;
return 1;
}
g->part += step;
g->run = g->part >= 1.0f;
if (g->run) g->part -= 1.0f;
return g->run;
}
/* The step phase 0 hands to the sound engine: a whole one while the gate holds the job to whole steps */
static inline uint32_t oot_sound_step(uint32_t step_bits, int timed) {
return oot_sound_partial(oot_bits_float(step_bits), timed) ? 0x3f800000u : step_bits;
}
/* The solver's steps: game (the game's own count), scaled_bits (frame step * steps a 60 fps frame + 0.001, the float it cut),
per_frame_bits (the steps of a 60 fps frame, a float) */
static inline int32_t oot_solver_steps(int32_t game, uint32_t scaled_bits, uint32_t per_frame_bits, int timed) {
const float scaled = oot_bits_float(scaled_bits) - 0.001f, per_frame = oot_bits_float(per_frame_bits);
if (!timed || !(scaled > 0.0f) || !(per_frame > 0.0f) || !(scaled < per_frame) || per_frame > 1000.0f) return game;
int32_t steps = (int32_t)ceilf(scaled - 0.1f);
if (steps < 1) steps = 1;
return steps > game ? steps : game;
}

View File

@@ -247,6 +247,179 @@ def unpatch_frame_seconds(s):
return s if STEP_MARK in s else s.replace(STEP_DECL, "", 1) # (a hook of another shape stays, with its declarations) return s if STEP_MARK in s else s.replace(STEP_DECL, "", 1) # (a hook of another shape stays, with its declarations)
# A movie's frame count above a 60 cap. The game's movie player counts its updates while a movie plays (f_83b7c710: one more
# at +760 of the player each frame), and a cutscene that shows a movie takes that count as its timeline's frame (f_83b5ec10,
# f_83b5e9a0): the narration, the rumble and the cutscene's end are placed by it. The movie itself follows the clock, so above
# 60 fps those cues ran ahead of the picture, fps / 60 times too fast (issue 1742). The count now grows by the frames' time, as
# the game's clock does (hos_movie_frames, host/nn_vi.c: 1 as before while the host gives no frame time).
MOVIE_HEAD = "void f_83b7c710(Ctx *__restrict A64_NOESC c) {\n"
# (the load and the add alone: the store after them is rewritten by the page marks' pass, which runs after these edits)
MOVIE_OLD = (" c->x[8]=recomp_load32(c,c->x[20] + 760);\n"
" { uint64_t _a=c->x[8], _b=1ULL; uint64_t _r=_a+_b; _r&=0xFFFFFFFFULL; c->x[8]=_r; }\n")
MOVIE_NEW = MOVIE_OLD.replace("_b=1ULL", "_b=hos_movie_frames(c->x[20])")
MOVIE_DECL = "uint32_t hos_movie_frames(uint64_t player);\n"
def patch_movie_frames(s):
"""The movie player's count by the frames' time; None when the file has no such function, s unchanged when the function is
there but its code differs (another game version) or has the patch already."""
head = s.find(MOVIE_HEAD)
if head < 0:
return None
end = s.find("\nvoid f_", head + len(MOVIE_HEAD))
body = s[head:end if end >= 0 else len(s)]
if body.count(MOVIE_OLD) != 1:
return s
at = head + body.index(MOVIE_OLD)
s = s[:at] + MOVIE_NEW + s[at + len(MOVIE_OLD):]
return s if MOVIE_DECL in s else s.replace('#include "a64.h"\n', '#include "a64.h"\n' + MOVIE_DECL, 1)
# Counters that add the frame step, or the step times a whole number, cut to whole frames (host/frame_whole.h). At 60 fps the step
# is 1; under a Frame rate cap above 60 it is the frame's own time, under 1 (0.42 at 144 fps), and the cut is 0 every frame:
# - f_80a87e50, the pick-up indicator above Link's head (a rupee, a Deku Stick): the count it shows (+864) moves toward the
# wallet's (+860) by (int)(step * max(distance / 50, 1)) and the indicator fades when they meet. Cut to 0, it stayed at the old
# count until a door or a talk replaced it (issue 1832).
# - f_800672e0, the water renderer: its ripple simulation runs (int)step times the water's substeps (+76 of the pass) each frame.
# Cut to 0, the rings Link and the actors make in water stood still above 60 fps. Found by atk3001 for Lichii (zortos293/
# Lichii#1152), as ported to Fritchii (Frizti/Fritchii d389685a4, 063dbb741): its remainder is kept per water object (x21).
# - f_803edf70, the time of day: each frame it adds (int)(2 * step) game seconds (+60) to the seconds of the day (+40, a day of
# 86400, then the day count +44). Cut, it added 1 a frame below 120 fps (the day slower: 83% at 100) and 0 above 120, where
# the clock stood still and the sun swung back and forth over the frozen hour.
# hos_frame_whole carries the fractions per counter while the frames' own time is the step, so each moves at the game's rate;
# otherwise it cuts as the game does. A site is added here once it is seen to misbehave in game. (A site's function, its code to
# replace, and the counter's address for its remainder.)
WHOLE_SITES = (
# (the cut alone, the function's one: the multiply before it is a64_fpx_hot_mul32_fma32 in a lift where the function is
# among A64_HOT_FUNCS)
("void f_80a87e50(Ctx *__restrict A64_NOESC c) {@",
" c->x[12]=recomp_fp_to_int(c->vreg[0][0]&UINT64_C(0xffffffff),32,32,1,3,c->fpcr,&c->fpsr);@",
"c->x[19] + 864"),
# (the cut alone, the function's one)
("void f_800672e0(Ctx *__restrict A64_NOESC c) {@",
" c->x[9]=recomp_fp_to_int(c->vreg[0][0]&UINT64_C(0xffffffff),32,32,1,3,c->fpcr,&c->fpsr);@",
"c->x[21]"),
# (the cut with the store after it: the function cuts into x8 the same way once more, in the branch that sets
# the time from another source)
("void f_803edf70(Ctx *__restrict A64_NOESC c) {@",
" c->x[8]=recomp_fp_to_int(c->vreg[0][0]&UINT64_C(0xffffffff),32,32,1,3,c->fpcr,&c->fpsr);@"
" recomp_store32(c,c->x[19] + 60,c->x[8]);@",
"c->x[19] + 60"),
)
WHOLE_SITES = tuple((h.replace("@", "\n"), o.replace("@", "\n"), k) for h, o, k in WHOLE_SITES)
WHOLE_MARK = "/* (the step's whole frames: nx2/oot_patches.py) */"
WHOLE_DECL = "extern uint32_t hos_frame_whole(uint64_t at, uint32_t amount_bits);\n"
def whole_new(old, key):
"""old with its cut to whole frames (the one recomp_fp_to_int) made hos_frame_whole's for the counter at key"""
lines = old.split("\n")
for i, line in enumerate(lines):
if "recomp_fp_to_int(c->vreg[0][0]" in line:
reg = line.split("=", 1)[0]
lines[i] = "%s=hos_frame_whole(%s, (uint32_t)c->vreg[0][0]); %s" % (reg, key, WHOLE_MARK)
return "\n".join(lines)
def patch_whole_frames(s):
"""The counters of WHOLE_SITES take hos_frame_whole's whole frames: (s, the sites patched here). A file without the function,
one patched before, or one whose code differs (another game version) is left as it is."""
done = 0
for head_text, old, key in WHOLE_SITES:
head = s.find(head_text)
if head < 0:
continue
end = s.find("\n}\n", head)
body = s[head:end if end >= 0 else len(s)]
new = whole_new(old, key)
if new in body or body.count(old) != 1:
continue
at = head + body.index(old)
s = s[:at] + new + s[at + len(old):]
done += 1
if done and WHOLE_DECL not in s:
s = s.replace('#include "a64.h"\n', '#include "a64.h"\n' + WHOLE_DECL, 1)
return s, done
# Above a Frame rate cap of 60, two places that need whole 60 fps frames (host/oot_high_fps.h has why and the logic; at a cap of
# 60 the host hands back what the game computed):
# - the sound manager's job, f_80435300 (phase in w2; phase 0 loads the game step, [[slot]]+0x10, and hands it to the sound
# engine): its head asks hos_oot_sound_gate whether to run now (60 Hz above 60 fps, the other phases following phase 0), and
# the step it loads goes through hos_oot_sound_step (a whole one while the gate holds the job). After zortos293/Lichii PR #1289
# (auz466), which does the same as a code patch for the emulator.
# - the physics solver's step count, set each update from the frame step in f_838ceb80 and f_838cfc10 (the same code in both):
# hos_oot_solver_steps rounds it up instead of down, at least 1, so Link stays on moving platforms (issue 1798).
SOUND_HEAD = "void f_80435300(Ctx *__restrict A64_NOESC c) {\n"
SOUND_SLOT = "g_module_base + 0x80435000ULL + (int64_t)75927552" # (the page of the step's pointer, whose +2192 holds it)
SOUND_OLD = (" c->x[8]=" + SOUND_SLOT + ";\n"
" { uint64_t _r = ((uint64_t)0 | ((c->x[0]) << 0)); c->x[19] = _r; }\n"
" c->x[8]=recomp_load64(c,c->x[8] + 2192);\n"
" c->x[8]=recomp_load64(c,c->x[8] + 0);\n"
" { { uint64_t _v=recomp_load32(c,(c->x[8]+16ULL)); a64_vset64(c->vreg[0],_v); } }\n")
SOUND_NEW = SOUND_OLD.replace("uint64_t _v=recomp_load32(c,(c->x[8]+16ULL));",
"uint64_t _v=hos_oot_sound_step((uint32_t)recomp_load32(c,(c->x[8]+16ULL)));")
SOUND_GATE = (" if (!hos_oot_sound_gate(c, (uint32_t)c->x[2], " + SOUND_SLOT + " + 2192)) return;"
" /* (nx2/oot_patches.py: the sound job at 60 Hz above 60 fps) */\n")
SOUND_DECL = "extern int hos_oot_sound_gate(Ctx *c, uint32_t phase, uint64_t slot);\nextern uint32_t hos_oot_sound_step(uint32_t step_bits);\n"
SOLVER_HEADS = ("void f_838ceb80(Ctx *__restrict A64_NOESC c) {\n", "void f_838cfc10(Ctx *__restrict A64_NOESC c) {\n")
# (the cut of frame step * steps a frame + 0.001, then its store into the solver: vreg 0 the float cut, vreg 1 the steps a frame)
SOLVER_OLD = (" c->x[1]=recomp_fp_to_int(c->vreg[0][0]&UINT64_C(0xffffffff),32,32,1,3,c->fpcr,&c->fpsr);\n"
" recomp_store32(c,c->x[0] + 712,c->x[1]);\n")
SOLVER_NEW = SOLVER_OLD.replace(
" recomp_store32(", " c->x[1]=hos_oot_solver_steps((uint32_t)c->x[1], (uint32_t)c->vreg[0][0], (uint32_t)c->vreg[1][0]);"
" /* (nx2/oot_patches.py: solver steps rounded up above 60 fps) */\n recomp_store32(", 1)
SOLVER_DECL = "extern uint32_t hos_oot_solver_steps(uint32_t game, uint32_t scaled_bits, uint32_t per_frame_bits);\n"
def _function(s, head_text):
"""(start, end) of the function whose head is head_text, or None"""
head = s.find(head_text)
if head < 0:
return None
end = s.find("\nvoid f_", head + len(head_text))
return head, end if end >= 0 else len(s)
def patch_sound_job(s):
"""The sound job's gate and step: (s, 1) when the job is in s with them (made now or before), (s, 0) when it is not here or
its code differs (another game version: left as it is)"""
at = _function(s, SOUND_HEAD)
if at is None:
return s, 0
head, end = at
body = s[head:end]
if body.count(SOUND_NEW) == 1 and body.count(SOUND_GATE) == 1:
return s, 1
if body.count(SOUND_OLD) != 1 or SOUND_GATE in body:
return s, 0
body = body.replace(SOUND_OLD, SOUND_NEW, 1)
body = SOUND_HEAD + SOUND_GATE + body[len(SOUND_HEAD):]
s = s[:head] + body + s[end:]
if SOUND_DECL not in s:
s = s.replace('#include "a64.h"\n', '#include "a64.h"\n' + SOUND_DECL, 1)
return s, 1
def patch_solver_steps(s):
"""The solver step count's two places: (s, how many of them are in s with the hook, made now or before)"""
done = 0
for head_text in SOLVER_HEADS:
at = _function(s, head_text)
if at is None:
continue
head, end = at
body = s[head:end]
if body.count(SOLVER_NEW) == 1:
done += 1
elif body.count(SOLVER_OLD) == 1:
s = s[:head] + body.replace(SOLVER_OLD, SOLVER_NEW, 1) + s[end:]
done += 1
if done and SOLVER_DECL not in s:
s = s.replace('#include "a64.h"\n', '#include "a64.h"\n' + SOLVER_DECL, 1)
return s, done
def patch_text_speed(s): def patch_text_speed(s):
"""The scale at the head of the letters' block of f_80b1e140; None when the file has no such function, s unchanged when the """The scale at the head of the letters' block of f_80b1e140; None when the file has no such function, s unchanged when the
function is there but its code differs (another game version).""" function is there but its code differs (another game version)."""
@@ -274,7 +447,7 @@ def apply(gen, *, protect_hearts=False, required=False):
The other patches only warn when their place is not found once.""" The other patches only warn when their place is not found once."""
import oot_native_resolution # (HOS_SCENE_NATIVE=1: the scene's base scale 1.0; off by default since #849's output resolution) import oot_native_resolution # (HOS_SCENE_NATIVE=1: the scene's base scale 1.0; off by default since #849's output resolution)
oot_native_resolution.apply(gen) oot_native_resolution.apply(gen)
done = {"dynamic resolution branch": 0, "text speed block": 0, "fixed path's scale": 0} done = {"dynamic resolution branch": 0, "text speed block": 0, "fixed path's scale": 0, "movie's frame count": 0}
if protect_hearts: # (the supported build's: these places are its functions) if protect_hearts: # (the supported build's: these places are its functions)
done["menu focus hook"] = 0 done["menu focus hook"] = 0
done["menu tick hook"] = 0 done["menu tick hook"] = 0
@@ -284,6 +457,8 @@ def apply(gen, *, protect_hearts=False, required=False):
done["free camera"] = 0 done["free camera"] = 0
done["mouse camera"] = 0 done["mouse camera"] = 0
step_sites, step_files = 0, [] # (the divides with the hook, and the files that have it) step_sites, step_files = 0, [] # (the divides with the hook, and the files that have it)
whole_sites = 0 # (the counters of WHOLE_SITES with the whole frames: patch_whole_frames)
sound_jobs = solver_sites = 0 # (patch_sound_job, patch_solver_steps: the places found with their hooks)
for path in sorted(glob.glob(os.path.join(glob.escape(gen), "code_*.c"))): for path in sorted(glob.glob(os.path.join(glob.escape(gen), "code_*.c"))):
with open(path, encoding="utf-8", newline="") as f: with open(path, encoding="utf-8", newline="") as f:
s = f.read() s = f.read()
@@ -308,6 +483,13 @@ def apply(gen, *, protect_hearts=False, required=False):
out = tick out = tick
done["menu tick hook"] += 1 done["menu tick hook"] += 1
print("oot_patches: the menus' update hook in", os.path.basename(path)) print("oot_patches: the menus' update hook in", os.path.basename(path))
movie = patch_movie_frames(out)
if movie is not None:
if MOVIE_NEW in movie: # (made now, or by an earlier run over this file)
done["movie's frame count"] += 1
if movie != out:
print("oot_patches: the movie's frame count by the frames' time in", os.path.basename(path))
out = movie
out, n = fixed_scale(out) out, n = fixed_scale(out)
if n: if n:
done["fixed path's scale"] += 1 done["fixed path's scale"] += 1
@@ -343,8 +525,26 @@ def apply(gen, *, protect_hearts=False, required=False):
step_files.append(path) step_files.append(path)
if sites: if sites:
print("oot_patches: the frame's time at %d step divide(s) in %s" % (sites, os.path.basename(path))) print("oot_patches: the frame's time at %d step divide(s) in %s" % (sites, os.path.basename(path)))
out, whole = patch_whole_frames(out)
whole_sites += out.count(WHOLE_MARK) # (made now, or by an earlier run over this file)
if whole:
print("oot_patches: %d frame counter(s) with the step's whole frames in %s" % (whole, os.path.basename(path)))
sound, found = patch_sound_job(out)
sound_jobs += found
if sound != out:
print("oot_patches: the sound job at 60 Hz above 60 fps in", os.path.basename(path))
out = sound
solver, found = patch_solver_steps(out)
solver_sites += found
if solver != out:
print("oot_patches: the solver steps rounded up above 60 fps in", os.path.basename(path))
out = solver
if out != s: if out != s:
whole_write.write_whole(path, out, newline="") whole_write.write_whole(path, out, newline="")
if sound_jobs != 1: # (without it, above 60 fps sounds and then the music can stop after a few loading zones)
print("oot_patches: WARNING: the sound job was found %d times, not once" % sound_jobs)
if solver_sites != len(SOLVER_HEADS): # (without them, above 60 fps moving platforms can drop Link)
print("oot_patches: WARNING: the solver step count was found at %d of %d places" % (solver_sites, len(SOLVER_HEADS)))
for what, n in done.items(): for what, n in done.items():
if n != 1: if n != 1:
print("oot_patches: WARNING: the %s was found %d times" % (what, n)) print("oot_patches: WARNING: the %s was found %d times" % (what, n))
@@ -357,6 +557,9 @@ def apply(gen, *, protect_hearts=False, required=False):
whole_write.write_whole(path, unpatch_frame_seconds(s), newline="") whole_write.write_whole(path, unpatch_frame_seconds(s), newline="")
print("oot_patches: WARNING: the frame's time was found at %d of %d step divides, none patched (a Frame rate cap above 60 " print("oot_patches: WARNING: the frame's time was found at %d of %d step divides, none patched (a Frame rate cap above 60 "
"keeps the game's own step)" % (step_sites, len(STEP_SITES))) "keeps the game's own step)" % (step_sites, len(STEP_SITES)))
elif whole_sites != len(WHOLE_SITES): # (the frames' own time without them: the pick-up indicator stays above 60 fps)
print("oot_patches: WARNING: %d of %d frame counters found for the step's whole frames (above 60 fps a counter can stop: "
"the pick-up indicator, the water ripples, the time of day)" % (whole_sites, len(WHOLE_SITES)))
apply_output_size(gen, required=required) apply_output_size(gen, required=required)

View File

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

View File

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

120
tests/test_frame_whole.py Normal file
View File

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

View File

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

View File

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