Triforce v0.3.2

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

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

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
+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) */
/* 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) */
static void realtime_step(int64_t now) {
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);
uint32_t bits;
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 {
hos_frame_seconds_bits = 0;
__atomic_store_n(&hos_frame_seconds_bits, 0, __ATOMIC_RELAXED);
}
const int top = timed ? 60 : fps_max();
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;
}