/* (hookshot) The sound manager's job at 60 Hz (and the music manager's update in it), the physics solver's steps rounded up, the floors' carry, the height-field surfaces' fixed steps and the ocarina recorder's clock, under a Frame rate cap above 60: the lifted code calls 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 #include extern volatile uint32_t hos_frame_seconds_bits; /* (host/nn_vi.c: the frame's seconds, 0 at a cap of 60) */ extern volatile uint64_t hos_step_probe; /* (host/nn_vi.c: f_80706be0's object, its +0xd58 the game clock in 60ths) */ /* ((Triforce) host/nn_vi.c carry_fractions: whether a counter cut to whole steps keeps its fractions. While the frames' own time is the step (timed, below), and at a cap of 60 or under while the game's step is not a whole frame (46 fps: 1.30), where the cut dropped the fraction every frame: the surfaces' fixed steps and the ocarina recorder's clock at 77 %) */ int hos_frame_carry(void); 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; } /* ((Triforce) the switches' rule for the ones read here, as gpu/vk's env_value_off has it: a value says off when it is "", "0", "false", "off" or "no", any case, blanks around it aside) */ static int fps_value_off(const char *v) { while (*v == ' ' || *v == '\t') v++; size_t n = strlen(v); while (n && (v[n - 1] == ' ' || v[n - 1] == '\t' || v[n - 1] == '\r' || v[n - 1] == '\n')) n--; if (n > 5) return 0; char s[6] = {0}; for (size_t i = 0; i < n; i++) s[i] = (char)(v[i] | 0x20); return !n || !strcmp(s, "0") || !strcmp(s, "false") || !strcmp(s, "off") || !strcmp(s, "no"); } 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) */ static uint64_t bgm_flags_at; /* (the guest address of the music manager's change flags, from its last read; under sound_lock) */ static uint8_t bgm_held; /* (the flags seen on frames the gate held the job, since the update last ran; under sound_lock) */ /* shortcut: one bit set on two held frames between runs is handled once (bit 6 then counts down once); count per bit if that is ever seen to matter */ static uint8_t bgm_run; /* (those, handed to the update's run: its own thread) */ static long long sound_job_runs; /* (phase 0's runs, for the song playbacks' note flag; under sound_lock) */ static OotSongCue song_cue[2]; /* (each playback's last read of it; under sound_lock) */ static long long cue_reads, cue_held; /* (HOS_FPS_LOG's counts, under sound_lock) */ /* 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 (!run && bgm_flags_at) bgm_held |= *(const uint8_t *)GP(bgm_flags_at); /* (what the update would have read now) */ if (run && phase == 0) sound_job_runs++; 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; } /* After f_832b7b50 in a song's playback (site 0: f_832b2af0, the song just played; 1: f_832b2dc0, the ocarina's other melody playback): game, its result (bit 0: the note flag is set). Above a cap of 60, bit 0 cleared when the flag was read set before and the sound job has not run since */ uint64_t hos_oot_song_cue(uint32_t site, uint64_t game) { const int t = timed(); long long held = -1; AcquireSRWLockExclusive(&sound_lock); const int cue = oot_song_cue(&song_cue[site & 1], (int)(game & 1), sound_job_runs, t); if (t && fps_log()) { cue_held += (int)(game & 1) && !cue; if (!(++cue_reads % 600)) held = cue_held, cue_held = 0; } ReleaseSRWLockExclusive(&sound_lock); if (held >= 0) hos_log("oot: a song's playback read its note flag set again before the sound job ran %lld of the last 600 reads", held); return cue ? game : game & ~(uint64_t)1; } /* The step f_80435300's phase 0 hands to the sound engine, and the music manager's update f_83373400 reads (its float's bits) */ uint32_t hos_oot_sound_step(uint32_t step_bits) { return oot_sound_step(step_bits, timed()); } /* The frame's seconds f_83373400 reads, with the step beside it (floats' bits) */ uint32_t hos_oot_sound_seconds(uint32_t seconds_bits, uint32_t step_bits) { return oot_sound_seconds(seconds_bits, step_bits, timed()); } /* f_83373400's head: the change flags held back since its last run are this run's */ void hos_oot_bgm_begin(void) { AcquireSRWLockExclusive(&sound_lock); bgm_run = bgm_held; bgm_held = 0; ReleaseSRWLockExclusive(&sound_lock); } /* f_83373400's reads of the change flags (at, their guest address; flags, the byte it read): with the ones held back */ uint64_t hos_oot_bgm_flags(uint64_t at, uint64_t flags) { AcquireSRWLockExclusive(&sound_lock); bgm_flags_at = at; ReleaseSRWLockExclusive(&sound_lock); return flags | bgm_run; } /* 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; } /* The follow-surface updates' compare (f_8015d490, f_80159dc0, f_838baa40): what the physics' inverse step is compared with */ uint32_t hos_oot_floor_cut(void) { const uint32_t cut = oot_floor_cut(__atomic_load_n(&hos_frame_seconds_bits, __ATOMIC_RELAXED)); if (fps_log()) { static volatile LONG64 calls; const LONG64 n = InterlockedIncrement64(&calls); if (n % 600 == 1) hos_log("oot: a floor's carry is cut to a tenth above an inverse step of %.1f (the game's 100)", oot_bits_float(cut)); } return cut; } static FrameWhole sim_whole; /* (a table of the surfaces' own: sharing hos_frame_whole's, they and the waters' ripples would take each other's slots in turn, each starting again without its remainder) */ static long long sim_calls, sim_given; /* (HOS_FPS_LOG's counts, under sim_lock) */ static SRWLOCK sim_lock = SRWLOCK_INIT; /* (the physics step's jobs) */ /* The fixed steps of f_83895f20, f_838a2660 and f_80652cb0: object, the manager or floor (its x19); quotient_bits, the update's seconds over 1/60 s. The float the game's cut then takes */ uint32_t hos_oot_sim_steps(uint64_t object, uint32_t quotient_bits) { const int t = hos_frame_carry(); long long given = -1; AcquireSRWLockExclusive(&sim_lock); const uint32_t bits = oot_sim_steps(&sim_whole, object, quotient_bits, t); if (fps_log()) { const float steps = oot_bits_float(bits); if (steps > 0.0f && steps < 1000.0f) sim_given += (long long)steps; if (!(++sim_calls % 600)) given = sim_given, sim_given = 0; } ReleaseSRWLockExclusive(&sim_lock); if (given >= 0) hos_log("oot: the water and floor surfaces got %lld fixed steps in the last 600 updates (quotient %.3f%s)", given, oot_bits_float(quotient_bits), t ? ", fractions carried" : ""); return bits; } static float record_rem; static SRWLOCK record_lock = SRWLOCK_INIT; /* (the ocarina manager's update alone runs the four places: a lock all the same) */ /* The ocarina recorder's clock (f_832b2690, f_832b32b0, f_832b6840): the frames it adds for the frame step */ uint32_t hos_oot_record_frames(uint32_t step_bits) { AcquireSRWLockExclusive(&record_lock); const uint32_t frames = oot_record_frames(&record_rem, step_bits, hos_frame_carry()); ReleaseSRWLockExclusive(&record_lock); return frames; } /* f_82c59eb0's test of its step against 1 (a horse's jump): step_bits its step (a float's bits), slot the guest address of the pointer to the time object (+0x10 this frame's step). 1: the step is the whole frame's, the branch for steps under 1 is skipped */ int hos_oot_horse_whole_step(uint32_t step_bits, uint64_t slot) { const int t = timed(); uint32_t frame = 0; if (t) { const uint64_t object = G64(slot); const uint64_t time = object ? G64(object) : 0; if (time) frame = *(const uint32_t *)GP(time + 0x10); } const int whole = oot_horse_whole_step(step_bits, frame, t); if (fps_log()) { static volatile LONG64 calls, skipped; const LONG64 s = whole ? InterlockedIncrement64(&skipped) : skipped; const LONG64 n = InterlockedIncrement64(&calls); if (n % 600 == 0) hos_log("oot: a horse's jump blend: the whole-frame step skipped the slow-motion branch %lld of %lld times (step %.3f, " "the frame's %.3f)", (long long)s, (long long)n, oot_bits_float(step_bits), oot_bits_float(frame)); } return whole; } static volatile uint64_t link_body; /* (Link's rigid body: hos_oot_note_link, from his update) */ static volatile uint32_t link_updates; /* (Link's updates, counted there: a carry needs the update before) */ static OotDriveCarry drive_carry; /* (under drive_lock) */ static SRWLOCK drive_lock = SRWLOCK_INIT; /* Link's update (host/hos_main.c, hos_oot_lift_player): his components (actor +624); their +0x50, +0x20, +0x10, +8 is his physics character, whose +0x20 is his rigid body. Each pointer is checked to be in guest memory. The count of his updates goes first, whatever the walk finds */ void hos_oot_note_link(uint64_t components) { __atomic_add_fetch(&link_updates, 1, __ATOMIC_RELAXED); uint64_t p = components; static const uint32_t walk[5] = {0x50, 0x20, 0x10, 8, 0x20}; for (int i = 0; i < 5; i++) p = p >= HOS_HEAP_BASE && hos_guest_range(p + walk[i], 8) ? G64(p + walk[i]) : 0; if (p >= HOS_HEAP_BASE && hos_guest_range(p + 0x8c, 0x24)) __atomic_store_n(&link_body, p, __ATOMIC_RELAXED); } /* f_804f5030, on its path for a body driven to a target, before it reads the target: controller x20 (its target at +0x3c, its flags at +0x100), body x23 or x24 (its position at +0x8c, +0x9c, +0xac), slot the guest address of the pointer to the game's time object (its +0x10 the frame's step) */ void hos_oot_drive_target(uint64_t controller, uint64_t body, uint64_t slot) { if (!body || body != __atomic_load_n(&link_body, __ATOMIC_RELAXED)) return; const uint64_t object = G64(slot); const uint64_t time = object ? G64(object) : 0; const float step = time ? oot_bits_float(*(const uint32_t *)GP(time + 0x10)) : 0.0f; const int driven = timed() && step > 0.0f && step < 1.0f && (G64(controller + 0x100) & 0x180) == 0x180; float at[3], target[3], game[3]; memcpy(&at[0], GP(body + 0x8c), 4); memcpy(&at[1], GP(body + 0x9c), 4); memcpy(&at[2], GP(body + 0xac), 4); memcpy(target, GP(controller + 0x3c), sizeof target); memcpy(game, target, sizeof game); AcquireSRWLockExclusive(&drive_lock); oot_drive_carry(&drive_carry, body, __atomic_load_n(&link_updates, __ATOMIC_RELAXED), driven, at, target); ReleaseSRWLockExclusive(&drive_lock); if (!driven || !memcmp(game, target, sizeof game)) return; memcpy(GP(controller + 0x3c), target, sizeof target); if (fps_log()) { static volatile LONG64 calls; if (InterlockedIncrement64(&calls) % 600 == 0) hos_log("oot: Link's driven body: its target carried %.4f m more than the game's (step %.3f)", sqrtf((target[0] - game[0]) * (target[0] - game[0]) + (target[1] - game[1]) * (target[1] - game[1]) + (target[2] - game[2]) * (target[2] - game[2])), step); } } /* A jump's takeoff speed (host/oot_high_fps.h oot_takeoff_rest_speed): f_80b97d10's end hands the velocity it wrote (vec: x, y, z); its vertical part, when (host time) and which takeoff it is are kept for the bodies' velocities in the next 50 ms */ static volatile uint32_t jump_bits; static volatile int64_t jump_ns; /* (0: none waiting) */ static volatile uint32_t jump_count; /* (the takeoffs so far: a body is watched once a takeoff) */ void hos_oot_jump_speed(uint64_t vec) { if (!vec || !hos_guest_range(vec, 12)) return; __atomic_store_n(&jump_bits, *(const uint32_t *)GP(vec + 4), __ATOMIC_RELAXED); __atomic_add_fetch(&jump_count, 1, __ATOMIC_RELAXED); __atomic_store_n(&jump_ns, hos_now_ns(), __ATOMIC_RELEASE); } /* The bodies handed a takeoff's speed on its update (under takeoff_lock: the physics' workers hand Havok the bodies' velocities): the speed and that frame's step, and whether the next update is still to come */ typedef struct { uint64_t object; uint32_t count; float vy0, step0; int waiting; } OotTakeoffBody; static OotTakeoffBody takeoff_bodies[8]; static unsigned takeoff_next; static SRWLOCK takeoff_lock = SRWLOCK_INIT; /* f_804f5030, before its set-velocity call for a body (the world's vtable +0x190): object (the body's object, x19), velocity (the guest address of the linear velocity the call reads: x, y, z), slot (the guest address of the pointer to the game's time object, its +0x10 the frame's step). A body handed the takeoff's speed in the takeoff's own update (within half that frame of f_80b97d10) is kept; on its next call, the vertical speed oot_takeoff_rest_speed gives. It returns at once while no takeoff is waiting. HOS_FPS_TAKEOFF=0 (or off, false, no): the game's speed */ void hos_oot_takeoff_velocity(uint64_t object, uint64_t velocity, uint64_t slot) { int64_t when = __atomic_load_n(&jump_ns, __ATOMIC_ACQUIRE); if (!when) return; static int on = -1; /* (read by the game's threads) */ int v = __atomic_load_n(&on, __ATOMIC_RELAXED); if (v < 0) { const char *e = getenv("HOS_FPS_TAKEOFF"); v = !(e && *e && fps_value_off(e)); __atomic_store_n(&on, v, __ATOMIC_RELAXED); } if (!v) return; const int64_t since = hos_now_ns() - when; if (since > 50000000) { /* (that takeoff is over: the calls return at once again) */ __atomic_compare_exchange_n(&jump_ns, &when, 0, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED); return; } if (!timed() || !hos_guest_range(velocity, 12) || !hos_guest_range(slot, 8)) return; const uint64_t time_object = G64(slot); const uint64_t time = time_object && hos_guest_range(time_object, 8) ? G64(time_object) : 0; const float step = time && hos_guest_range(time + 0x10, 4) ? oot_bits_float(*(const uint32_t *)GP(time + 0x10)) : 0.0f; const float vy = oot_bits_float(*(const uint32_t *)GP(velocity + 4)); const float jump = oot_bits_float(__atomic_load_n(&jump_bits, __ATOMIC_RELAXED)); const uint32_t count = __atomic_load_n(&jump_count, __ATOMIC_RELAXED); float vy0 = 0.0f, step0 = 0.0f; int next = 0; AcquireSRWLockExclusive(&takeoff_lock); OotTakeoffBody *seen = NULL; for (unsigned k = 0; k < sizeof takeoff_bodies / sizeof takeoff_bodies[0]; k++) if (takeoff_bodies[k].object == object && takeoff_bodies[k].count == count) seen = &takeoff_bodies[k]; if (!seen) { /* (the takeoff's own update: a 60 fps frame is 16.7 ms, this one step times that; a body handed the speed later follows Link's a step behind) */ if (oot_takeoff_match(vy, jump, step) && (double)since < step * (1e9 / 120.0)) takeoff_bodies[takeoff_next++ % (sizeof takeoff_bodies / sizeof takeoff_bodies[0])] = (OotTakeoffBody){object, count, vy, step, 1}; } else if (seen->waiting && (double)since >= seen->step0 * (1e9 / 120.0)) { /* (its next update) */ seen->waiting = 0; vy0 = seen->vy0, step0 = seen->step0, next = 1; } ReleaseSRWLockExclusive(&takeoff_lock); if (!next) return; const float speed = oot_takeoff_rest_speed(vy, vy0, step0, step); if (speed == vy) return; /* (still rising: a side hop) */ { void *const to = GP(velocity + 4); memcpy(to, &speed, 4); HOS_WRITTEN(to, 4); } if (fps_log()) { static volatile LONG64 lifts; const LONG64 n = InterlockedIncrement64(&lifts); if (n % 20 == 1) hos_log("oot: a jump's takeoff: a body rises %.1f cm more on the update after it (%.3f m/s, the game's %.3f; steps " "%.3f, %.3f; %lld lifts)", 100.0 * (speed - vy) * step / 60.0, speed, vy, step0, step, (long long)n); } } /* The time f_81b3aa48 loads from the cloth's step context, times the game's frame step below 60 fps (floats' bits). HOS_FPS_CLOTH=0 (or off, false, no): the game's, a 60th a frame */ uint32_t hos_oot_cloth_seconds(uint32_t seconds_bits, uint32_t step_bits) { static int on = -1; /* (read by the game's threads) */ int v = __atomic_load_n(&on, __ATOMIC_RELAXED); if (v < 0) { const char *e = getenv("HOS_FPS_CLOTH"); v = !(e && *e && fps_value_off(e)); __atomic_store_n(&on, v, __ATOMIC_RELAXED); } const uint32_t seconds = oot_cloth_seconds(seconds_bits, step_bits, v); if (fps_log()) { static volatile LONG64 calls; const LONG64 n = InterlockedIncrement64(&calls); if (n % 600 == 1) hos_log("oot: cloth time %.2f ms (the game's %.2f ms: frame step %.3f%s; %lld setups)", oot_bits_float(seconds) * 1000.0f, oot_bits_float(seconds_bits) * 1000.0f, oot_bits_float(step_bits), v ? "" : ", HOS_FPS_CLOTH=0", (long long)n); } return seconds; } static OotClothGate cloth_gate = OOT_CLOTH_GATE_INIT; static long long cloth_calls, cloth_solves; /* (HOS_FPS_LOG's counts, under cloth_lock) */ static SRWLOCK cloth_lock = SRWLOCK_INIT; /* (the solve jobs run on the game's job workers, several at once) */ /* f_81b47080's head, the cloth's solve job: step_bits the game's frame step (the float f_83b6ea90 writes). 0: the job returns at once, the cloth held this frame. HOS_FPS_CLOTH_60=1 (by the switches' rule, fps_value_off): at most 60 solves a second above a cap of 60 */ int hos_oot_cloth_solve(uint32_t step_bits) { static int on = -1; /* (read by the game's threads) */ int v = __atomic_load_n(&on, __ATOMIC_RELAXED); if (v < 0) { const char *e = getenv("HOS_FPS_CLOTH_60"); v = e && !fps_value_off(e); __atomic_store_n(&on, v, __ATOMIC_RELAXED); } const int t = timed(); if (!v || !t) return 1; /* (off, or a cap of 60: every job solves, and no lock taken) */ double clock = NAN; /* (no clock: the gate stands aside) */ const uint64_t probe = __atomic_load_n(&hos_step_probe, __ATOMIC_RELAXED); if (probe) memcpy(&clock, GP(probe + 0xd58), 8); const float step = oot_bits_float(step_bits); long long solved = -1, calls = 0; AcquireSRWLockExclusive(&cloth_lock); const int solve = oot_cloth_gate(&cloth_gate, clock, step, v, t); if (fps_log()) { cloth_solves += solve; if (!(++cloth_calls % 600)) solved = cloth_solves, calls = cloth_calls, cloth_solves = 0; } ReleaseSRWLockExclusive(&cloth_lock); if (solved >= 0) hos_log("oot: cloth solved %lld of the last 600 solve jobs (step %.3f, HOS_FPS_CLOTH_60; %lld jobs)", solved, step, calls); return solve; }