diff --git a/runtime/boot.c b/runtime/boot.c index d83b04fc..66f23e81 100644 --- a/runtime/boot.c +++ b/runtime/boot.c @@ -15,6 +15,7 @@ #include #include "cfg.h" +#include "fs.h" #include "gfx.h" #include "nvn_api.h" #include "vk.h" @@ -283,6 +284,7 @@ void rt_frame_presented(const tex_t *t, uint64_t n) { stats_line(); fflush(stdout); fflush(stderr); + fs_save_flush(); vk_pipeline_save(); // no-op without the Vulkan back end prof_write(); fflush(stderr); diff --git a/runtime/frame_rate.c b/runtime/frame_rate.c index b5d57c88..17ad1aed 100644 --- a/runtime/frame_rate.c +++ b/runtime/frame_rate.c @@ -22,6 +22,11 @@ // One 64-bit and one 32-bit store per present; the guest reads the floats on its own threads, and each stays finite // and in range whichever store it sees. // +// The world clock (frame_step_world_us) sums the steps from the first present that finds the player actor away from +// the origin, where the title screen keeps it: the end of the first save load. Recorded and scripted input +// (input_win.c) runs on it, so a replay presses at the same game time at any frame rate and after a first load of any +// length; a later load (fast travel) still shifts what follows by its length. +// // TOTK_TRACE=perf adds a line per 120 presents: fps, the game seconds stepped against wall seconds, and with // TOTK_SPEED_PROBE=1 the player's position and ground speed (an independent check: Link runs at the same speed at // any frame rate when game time follows wall time). @@ -201,11 +206,30 @@ static void speed_report(uint64_t now, double step, int interval) { memset(win_hist, 0, sizeof win_hist); } +static _Atomic int64_t world_us = -1; +static uint64_t presents; + +int64_t frame_step_world_us(void) { return atomic_load_explicit(&world_us, memory_order_relaxed); } + +static void world_clock(double dt, double step) { + presents++; + int64_t w = atomic_load_explicit(&world_us, memory_order_relaxed); + if (w < 0) { + float p[3]; + if (!player_pos(p) || fabsf(p[0]) + fabsf(p[2]) < 1.0f) return; // a few guest reads, until the load ends + rt_log("frame step: world clock starts at present %llu (the player at %.1f %.1f %.1f)", + (unsigned long long)presents, p[0], p[1], p[2]); + atomic_store_explicit(&world_us, 0, memory_order_relaxed); + return; + } + if (!step_word) step = dt > STEP_MAX_S ? STEP_MAX_S : dt; // whole-vsync steps: the display time stands in + atomic_store_explicit(&world_us, w + (int64_t)(step * 1e6 + 0.5), memory_order_relaxed); +} + void frame_step_present(uint64_t display_ns, int interval) { frame_limit_live(); - double step = 0; + double step = 0, dt = last_ns ? (double)(display_ns - last_ns) * 1e-9 : 0; if (step_word && last_ns) { - double dt = (double)(display_ns - last_ns) * 1e-9; if (dt > STEP_RESET_S || dt < 0.001) { sample_count = sample_next = 0; } else { @@ -219,5 +243,6 @@ void frame_step_present(uint64_t display_ns, int interval) { } } last_ns = display_ns; + world_clock(dt, step); if (rt_trace & RT_TRACE_PERF) speed_report(display_ns, step, interval); } diff --git a/runtime/fs.h b/runtime/fs.h index ac2e6240..07cfe7a0 100644 --- a/runtime/fs.h +++ b/runtime/fs.h @@ -48,3 +48,5 @@ void fs_host_set_profile(const char *dir); // profile directory; // (default rt_config()->profile) int fs_save_busy(void); // a save is being written: a save file open for writing, // or save data changed in the last 3 s and not committed +void fs_save_flush(void); // wait for a save Commit still running in the background; + // every exit path calls it before the process ends diff --git a/runtime/fs_host.c b/runtime/fs_host.c index 5d1ccfd8..69c88f8e 100644 --- a/runtime/fs_host.c +++ b/runtime/fs_host.c @@ -13,8 +13,9 @@ // /cache// cache storage // // The save mount is journaled as on Horizon: changes go to a working copy and become the committed save atomically at -// Commit; a crash or an Unmount before Commit leaves the previous committed save. fs_save_busy still tells a window -// close to wait until a save in progress is committed. Paths are UTF-8 and go to the W APIs as UTF-16. +// Commit; a crash or an Unmount before Commit leaves the previous committed save. Commit returns at once and the copy +// runs on a background thread (section "save transactions"); fs_save_flush lets the exit paths wait for it. Paths are +// UTF-8 and go to the W APIs as UTF-16. #include #include #include @@ -70,7 +71,9 @@ static DWORD attributes(const char *path) { // (TotK writes a slot's files back to back and then commits; between the files nothing is open for a moment). static volatile LONG save_writers, save_dirty; static volatile LONG64 save_changed_ns; +static void save_wait_commit(void); static void save_touched(void) { + save_wait_commit(); // every change to save data comes here first: none lands in a commit's snapshot InterlockedExchange64(&save_changed_ns, (LONG64)plat_now_ns()); InterlockedExchange(&save_dirty, 1); } @@ -158,7 +161,10 @@ static int remove_tree(const char *path) { // Commit: (1) copy work to new and flush every file (FlushFileBuffers) and directory, a file equal to the committed // one as a hard link to it; (2) rename save/ to old; // (3) rename new to save/, the commit point; (4) move old to save-backup/ and keep the newest three. -// Both renames are MoveFileExW with MOVEFILE_WRITE_THROUGH. save/ is renamed away only after new is complete +// Both renames are MoveFileExW with MOVEFILE_WRITE_THROUGH. The game's Commit returns 0 once steps (1) to (4) are +// queued; a thread runs them (95-135 ms on the game thread before, in the 1.4.3 fight save) while every change to the +// working copy waits in save_touched, and Unmount and the next Commit wait too. A crash in that time loses this Commit +// only, and a failed one is logged instead of returned. save/ is renamed away only after new is complete // on disk, so the recovery rule at start (save_recover) is: // save/ present it is the committed save (the old one if the crash came before (3), the new one after); // new, if present, is an unfinished copy and is deleted (rollback); @@ -425,18 +431,56 @@ static uint32_t save_commit_locked(void) { return 0; } -static uint32_t save_commit(void) { +// The background commit: commit_pending from the game's Commit until the thread has run save_commit_locked. +static SRWLOCK pend_lock = SRWLOCK_INIT; +static CONDITION_VARIABLE pend_cv = CONDITION_VARIABLE_INIT; +static volatile LONG commit_pending; + +static void save_wait_commit(void) { + if (!InterlockedCompareExchange(&commit_pending, 0, 0)) return; + AcquireSRWLockExclusive(&pend_lock); + while (commit_pending) SleepConditionVariableSRW(&pend_cv, &pend_lock, INFINITE, 0); + ReleaseSRWLockExclusive(&pend_lock); +} + +void fs_save_flush(void) { save_wait_commit(); } + +static uint32_t commit_run(void) { AcquireSRWLockExclusive(&tx_lock); - InterlockedIncrement(&save_writers); // fs_save_busy for the duration - static int quiet; - uint32_t r = 0; - if (InterlockedCompareExchange(&save_dirty, 0, 0)) r = save_commit_locked(); - else if (!quiet++) rt_log("fs commit save: nothing changed (logged once)"); + uint32_t r = save_commit_locked(); InterlockedDecrement(&save_writers); ReleaseSRWLockExclusive(&tx_lock); + AcquireSRWLockExclusive(&pend_lock); + commit_pending = 0; + ReleaseSRWLockExclusive(&pend_lock); + WakeAllConditionVariable(&pend_cv); return r; } +static DWORD WINAPI commit_main(void *arg) { + (void)arg; + commit_run(); + return 0; +} + +static uint32_t save_commit(void) { + save_wait_commit(); // the previous Commit + static int quiet; + if (!InterlockedCompareExchange(&save_dirty, 0, 0)) { + if (!quiet++) rt_log("fs commit save: nothing changed (logged once)"); + return 0; + } + InterlockedIncrement(&save_writers); // fs_save_busy until the thread is done + InterlockedExchange(&commit_pending, 1); + HANDLE t = CreateThread(NULL, 0, commit_main, NULL, 0, NULL); + if (t) { + CloseHandle(t); + return 0; + } + rt_log("fs: cannot start the save commit thread (error %lu); committing on the game thread", GetLastError()); + return commit_run(); +} + // ---- storage roots static BOOL CALLBACK storage_init(PINIT_ONCE once, PVOID param, PVOID *ctx) { (void)once, (void)param, (void)ctx; @@ -678,6 +722,7 @@ static uint32_t read_cache_list(int *count, CacheStorageInfo *out, uint64_t h, i static void close_cache_list(uint64_t h) { free((void *)(uintptr_t)h); } static void unmount(const char *name) { + save_wait_commit(); // else the working copy could go before a Commit queued just now has copied it AcquireSRWLockExclusive(&tx_lock); AcquireSRWLockExclusive(&lock); for (int i = 0; i < NMOUNTS; i++) @@ -1335,6 +1380,13 @@ static void probe_restart(const uint64_t *uid) { CHECK(RES(S_MOUNT_SAVE, U("save"), U(uid)) == 0, "MountSaveData after a restart"); } +// Commit through the override, then wait for its background thread, as the checks read the disk next. +static uint32_t tx_commit(void) { + uint32_t r = RES(S_COMMIT, U("save")); + fs_save_flush(); + return r; +} + #define TX_SIZE 4096 static uint8_t tx_buf[TX_SIZE]; @@ -1405,7 +1457,7 @@ static int count_dirs(const wchar_t *dir) { static void probe_save_tx(const uint64_t *uid) { const char *a = "save:/tx_a.sav", *b = "save:/tx_b.sav"; uint64_t h = 0; - CHECK(tx_write(a, 1) && tx_write(b, 1) && RES(S_COMMIT, U("save")) == 0 && tx_read_committed("tx_a.sav") == 1, + CHECK(tx_write(a, 1) && tx_write(b, 1) && tx_commit() == 0 && tx_read_committed("tx_a.sav") == 1, "Commit of generation 1"); // uncommitted changes: the game reads them; the committed save and the next start do not see them @@ -1417,7 +1469,7 @@ static void probe_save_tx(const uint64_t *uid) { CHECK(tx_read(a) == 1 && tx_read("save:/tx_c.sav") == -1, "an uncommitted write survived a restart"); // committed changes survive a restart; an Unmount without Commit drops changes - CHECK(tx_write(a, 3) && tx_write(b, 3) && RES(S_COMMIT, U("save")) == 0, "Commit of generation 3"); + CHECK(tx_write(a, 3) && tx_write(b, 3) && tx_commit() == 0, "Commit of generation 3"); probe_restart(uid); CHECK(tx_read(a) == 3 && tx_read(b) == 3, "a commit lost in a restart"); CHECK(tx_write(a, 4), "write before Unmount"); @@ -1430,7 +1482,7 @@ static void probe_save_tx(const uint64_t *uid) { for (int k = 1; k < 64; k++) { int next = ++gen, ok = tx_write(a, next) && tx_write(b, next); tx_crash_at = k; - uint32_t r = RES(S_COMMIT, U("save")); + uint32_t r = tx_commit(); int died = tx_dead; tx_crash_at = 0; if (!died) { @@ -1456,19 +1508,31 @@ static void probe_save_tx(const uint64_t *uid) { // a Commit writes the changed file and links the unchanged one to the committed save it replaces int kept = committed; - CHECK(tx_write(a, ++gen) && RES(S_COMMIT, U("save")) == 0 && tx_read_committed("tx_a.sav") == gen && + CHECK(tx_write(a, ++gen) && tx_commit() == 0 && tx_read_committed("tx_a.sav") == gen && tx_read_committed("tx_b.sav") == kept && tx_links_committed("tx_b.sav") >= 2 && tx_links_committed("tx_a.sav") == 1, "Commit of one changed file: tx_a %d links, tx_b %d links", tx_links_committed("tx_a.sav"), tx_links_committed("tx_b.sav")); committed = gen; + // the background commit: a write right after Commit waits for it and stays out of it; an Unmount right after + // Commit waits for it and keeps it + int queued = ++gen; + CHECK(tx_write(a, queued) && RES(S_COMMIT, U("save")) == 0 && tx_write(a, ++gen) && tx_read(a) == gen && + tx_read_committed("tx_a.sav") == queued, "a write right after Commit: committed tx_a %d, expected %d", + tx_read_committed("tx_a.sav"), queued); + CHECK(RES(S_COMMIT, U("save")) == 0, "Commit before Unmount"); + CALL(S_UNMOUNT, U("save")); + CHECK(tx_read_committed("tx_a.sav") == gen && RES(S_MOUNT_SAVE, U("save"), U(uid)) == 0 && tx_read(a) == gen, + "an Unmount right after Commit lost it"); + committed = gen; + // the read-only mount shows the committed save and refuses changes CHECK(tx_write(a, 99) && RES(S_MOUNT_SAVE_RO, U("ro"), TOTK_APPLICATION_ID, U(uid)) == 0 && tx_read("ro:/tx_a.sav") == committed && RES(S_OPEN_FILE, U(&h), U("ro:/tx_a.sav"), FS_OPEN_WRITE) == FS_R_UNSUPPORTED_OPEN && RES(S_CREATE_FILE, U("ro:/x"), 0) == FS_R_UNSUPPORTED_OPEN && RES(S_RENAME_FILE, U("save:/tx_a.sav"), U("ro:/y")) == FS_R_INVALID_PATH, "MountSaveDataReadOnly"); CALL(S_UNMOUNT, U("ro")); - CHECK(RES(S_DELETE_FILE, U(a)) == 0 && RES(S_DELETE_FILE, U(b)) == 0 && RES(S_COMMIT, U("save")) == 0 && + CHECK(RES(S_DELETE_FILE, U(a)) == 0 && RES(S_DELETE_FILE, U(b)) == 0 && tx_commit() == 0 && tx_read_committed("tx_a.sav") == -1, "Commit of deletions"); printf("save tx: uncommitted writes visible to the game and dropped by a restart and by Unmount, commits kept; a " "crash at each of %d commit steps recovers to the old save %d times, the new one %d times, mixed %d " @@ -1518,7 +1582,7 @@ static void probe_storage(void) { CHECK(fs_save_busy(), "a save file open for writing does not count as a save in progress"); CALL(S_CLOSE_FILE, h); CHECK(fs_save_busy(), "closed but uncommitted save data does not count as a save in progress"); - CHECK(RES(S_COMMIT, U("save")) == 0 && !fs_save_busy(), "Commit"); + CHECK(tx_commit() == 0 && !fs_save_busy(), "Commit"); CHECK(RES(S_COMMIT, U("nomount")) == FS_R_PATH_NOT_FOUND, "Commit of an unknown mount"); // paths @@ -1549,7 +1613,7 @@ static void probe_storage(void) { count == 1 && ents[0].type == FS_ENTRY_FILE, "ReadDirectory, files only"); CALL(S_CLOSE_DIRECTORY, d); - CHECK(RES(S_COMMIT, U("save")) == 0, "Commit of the directories"); + CHECK(tx_commit() == 0, "Commit of the directories"); // reopen after unmount: the bytes come back through the overrides and are on disk CALL(S_UNMOUNT, U("save")); @@ -1588,7 +1652,7 @@ static void probe_storage(void) { RES(S_DELETE_FILE, U("save:/renamed.sav")) == FS_R_PATH_NOT_FOUND, "DeleteFile"); CHECK(RES(S_DELETE_RECURSIVE, U("save:/dir")) == 0 && RES(S_ENTRY_TYPE, U(&type), U("save:/dir")) == FS_R_PATH_NOT_FOUND, "DeleteDirectoryRecursively"); - CHECK(RES(S_COMMIT, U("save")) == 0 && !fs_save_busy(), "Commit after the changes"); + CHECK(tx_commit() == 0 && !fs_save_busy(), "Commit after the changes"); probe_save_tx(uid); // temporary and cache storage diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index 099218db..3b2b44e5 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -191,6 +191,8 @@ static void xlate_identity(void) { // The repository root is the nearest parent of totk.exe that holds build/xlate/totk_xlate.exe. The shader cache lives // next to totk.exe, so every build dir has its own (as the pipeline cache, vk_pipeline.c cache_path_init). +static void use_preload_start(void); + static BOOL CALLBACK init_paths(PINIT_ONCE once, PVOID param, PVOID *ctx) { (void)once, (void)param, (void)ctx; char exe[PATH_CAP], root[PATH_CAP] = "", exe_dir[PATH_CAP] = ""; @@ -216,6 +218,7 @@ static BOOL CALLBACK init_paths(PINIT_ONCE once, PVOID param, PVOID *ctx) { if (cache_cfg && *cache_cfg && strlen(cache_cfg) < sizeof cache_dir) strcpy(cache_dir, cache_cfg); else if (exe_dir[0]) snprintf(cache_dir, sizeof cache_dir, "%s\\shader-cache", exe_dir); xlate_identity(); + use_preload_start(); return TRUE; } @@ -701,15 +704,95 @@ static UseMask use_of(const GfxShader *s) { } // .use: masks are per translator. -static void use_path(const uint8_t h[32], char *p) { - uint8_t buf[4 + 32 + 32], name[32]; - char hex[65]; +static void use_name(const uint8_t h[32], uint8_t name[32]) { + uint8_t buf[4 + 32 + 32]; memcpy(buf, "TKSU", 4), memcpy(buf + 4, xlate_id, 32), memcpy(buf + 36, h, 32); rt_sha256(buf, sizeof buf, name); +} + +static void use_path_of(const uint8_t name[32], char *p) { + char hex[65]; key_hex(name, hex); snprintf(p, PATH_CAP + 80, "%s\\%s.use", cache_dir, hex); } +static void use_path(const uint8_t h[32], char *p) { + uint8_t name[32]; + use_name(h, name); + use_path_of(name, p); +} + +// Every .use file of the cache, read by a background thread when the paths are set (use_preload), by file name: a +// lookup on the render thread then opens no file. Opening one took 0.1 ms alone but 11-40 ms in play, behind the +// translator's and the pipeline cache's writes and flushes on the same volume, and each new shader of a draw opened one. +// The table is complete and read-only once use_files_ready is 1; masks found later are in use_buckets. +#define USE_FILE_BUCKETS 65536 +typedef struct UseFile { struct UseFile *next; uint8_t name[32]; UseMask m; } UseFile; +static UseFile *use_files[USE_FILE_BUCKETS]; +static volatile LONG use_files_ready; + +static UseFile **use_file_slot(const uint8_t name[32]) { + UseFile **e = &use_files[(name[0] | name[1] << 8) % USE_FILE_BUCKETS]; + while (*e && memcmp((*e)->name, name, 32)) e = &(*e)->next; + return e; +} + +static int hex_byte(const char *c) { + int v = 0; + for (int i = 0; i < 2; i++) { + int d = isdigit((unsigned char)c[i]) ? c[i] - '0' : c[i] >= 'a' && c[i] <= 'f' ? c[i] - 'a' + 10 : -1; + if (d < 0) return -1; + v = v << 4 | d; + } + return v; +} + +static DWORD WINAPI use_preload(void *arg) { + (void)arg; + uint64_t t0 = plat_now_ns(); + char pattern[PATH_CAP + 16], p[PATH_CAP + 80]; + snprintf(pattern, sizeof pattern, "%s\\*.use", cache_dir); + WIN32_FIND_DATAA fd; + HANDLE f = FindFirstFileExA(pattern, FindExInfoBasic, &fd, FindExSearchNameMatch, NULL, FIND_FIRST_EX_LARGE_FETCH); + int n = 0, bad = 0; + if (f != INVALID_HANDLE_VALUE) { + do { + UseFile e = {0}; + int ok = strlen(fd.cFileName) == 68; + for (int i = 0; ok && i < 32; i++) { + int v = hex_byte(fd.cFileName + 2 * i); + ok = v >= 0, e.name[i] = (uint8_t)v; + } + uint64_t len = 0; + uint32_t *u = NULL; + if (ok) { + snprintf(p, sizeof p, "%s\\%s", cache_dir, fd.cFileName); + u = (uint32_t *)read_file(p, &len); + ok = u && len == 20 && u[0] == USE_MAGIC && u[1] == USE_VERSION; + } + UseFile **slot = ok ? use_file_slot(e.name) : NULL, *x; + if (slot && !*slot && (x = malloc(sizeof *x))) { + e.m.slots = u[2], e.m.attrs = u[3], e.m.fouts = u[4]; + *x = e, *slot = x, n++; + } else if (!ok) { + bad++; + } + free(u); + } while (FindNextFileA(f, &fd)); + FindClose(f); + } + InterlockedExchange(&use_files_ready, 1); + rt_log("gfx_shader: %d use masks read in %.0f ms on a background thread (%d unreadable)", n, + (plat_now_ns() - t0) / 1e6, bad); + return 0; +} + +static void use_preload_start(void) { + if (!cache_dir[0]) return; + HANDLE t = CreateThread(NULL, 0, use_preload, NULL, 0, NULL); + if (t) SetThreadPriority(t, THREAD_PRIORITY_BELOW_NORMAL), CloseHandle(t); +} + static UseEnt **use_slot(const uint8_t h[32]) { // caller holds cache_lock UseEnt **e = &use_buckets[(h[0] | h[1] << 8) % BUCKETS]; while (*e && memcmp((*e)->code_hash, h, 32)) e = &(*e)->next; @@ -732,13 +815,20 @@ static int use_find(const uint8_t h[32], UseMask *m) { if (known) *m = e->m; ReleaseSRWLockShared(&cache_lock); if (seen) return known; - char p[PATH_CAP + 80]; - use_path(h, p); - uint64_t n = 0; - uint32_t *f = (uint32_t *)read_file(p, &n); - known = f && n == 20 && f[0] == USE_MAGIC && f[1] == USE_VERSION; - if (known) m->slots = f[2], m->attrs = f[3], m->fouts = f[4]; - free(f); + uint8_t name[32]; + use_name(h, name); + if (InterlockedCompareExchange(&use_files_ready, 0, 0)) { // complete: a name not in it has no file + UseFile *u = *use_file_slot(name); + if ((known = u != NULL)) *m = u->m; + } else { + char p[PATH_CAP + 80]; + use_path_of(name, p); + uint64_t n = 0; + uint32_t *f = (uint32_t *)read_file(p, &n); + known = f && n == 20 && f[0] == USE_MAGIC && f[1] == USE_VERSION; + if (known) m->slots = f[2], m->attrs = f[3], m->fouts = f[4]; + free(f); + } AcquireSRWLockExclusive(&cache_lock); use_put(h, known, m); ReleaseSRWLockExclusive(&cache_lock); @@ -918,6 +1008,12 @@ void gfx_shader_drop_memory(void) { for (UseEnt *e = use_buckets[i], *n; e; e = n) n = e->next, free(e); use_buckets[i] = NULL; } + // the preloaded .use table misses the masks this run wrote, which only use_buckets held: back to the disk + InterlockedExchange(&use_files_ready, 0); + for (int i = 0; i < USE_FILE_BUCKETS; i++) { + for (UseFile *e = use_files[i], *n; e; e = n) n = e->next, free(e); + use_files[i] = NULL; + } ReleaseSRWLockExclusive(&cache_lock); } @@ -1626,18 +1722,23 @@ static int rec_append(const char *path, const char *magic, uint32_t version, uin return !fclose(f) && ok; } +static SRWLOCK rec_io = SRWLOCK_INIT; // the record files: appends in order, no read during an append + void gfx_shader_record_flush(int force) { static volatile LONG64 last; LONG64 now = (LONG64)GetTickCount64(); if (!RC.on || (!force && now - last < 2000)) return; last = now; + // The files are written outside RC.lock, which the render thread takes to record a new answer. + AcquireSRWLockExclusive(&rec_io); AcquireSRWLockExclusive(&RC.lock); Bytes ans = RC.ans, prg = RC.prg; RC.ans = (Bytes){0}, RC.prg = (Bytes){0}; uint64_t total = RC.pairs.n, added = RC.pairs_new; + ReleaseSRWLockExclusive(&RC.lock); if (prg.n && !rec_append(RC.prg_path, "TOTKSPRG", PRG_VERSION, 0, &prg)) rt_log("gfx_shader: cannot write %s", RC.prg_path); if (ans.n && !rec_append(RC.ans_path, "TOTKSANS", ANS_VERSION, ANS_REC, &ans)) rt_log("gfx_shader: cannot write %s", RC.ans_path); - ReleaseSRWLockExclusive(&RC.lock); + ReleaseSRWLockExclusive(&rec_io); if (ans.n) rt_log("gfx_shader: answers: %zu new records appended (%llu new this run, %llu recorded)", ans.n / ANS_REC, (unsigned long long)added, (unsigned long long)total); free(ans.p), free(prg.p); @@ -1801,9 +1902,9 @@ size_t gfx_shader_warm_list(GfxWarmEnt **out) { RC.ans_path); uint8_t *ab = NULL, *pb = NULL; size_t an = 0, pn = 0; - AcquireSRWLockExclusive(&RC.lock); // no append meanwhile + AcquireSRWLockExclusive(&rec_io); // no append meanwhile rec_read(RC.ans_path, "TOTKSANS", ANS_VERSION, ANS_REC, &ab, &an); - ReleaseSRWLockExclusive(&RC.lock); + ReleaseSRWLockExclusive(&rec_io); rec_read(prp, "TOTKSPRP", PRP_VERSION, PRP_REC, &pb, &pn); size_t na = an / ANS_REC, np = pn / PRP_REC, n = 0; GfxWarmEnt *e = calloc(na + np + 1, sizeof *e); diff --git a/runtime/gfx_window.c b/runtime/gfx_window.c index eb01ea28..3da9d1eb 100644 --- a/runtime/gfx_window.c +++ b/runtime/gfx_window.c @@ -19,6 +19,7 @@ #include "cfg.h" #include "flight.h" +#include "fs.h" #include "gfx.h" #include "input.h" #include "nvn_api.h" @@ -392,8 +393,9 @@ static LRESULT CALLBACK wnd_proc(HWND h, UINT m, WPARAM w, LPARAM l) { return 0; } case WM_CLOSE: - // The process ends with the window, as in MNG. Save writes go straight to files (runtime/fs_host.c). + // The process ends with the window, as in MNG, once a save Commit still running in the background is done. rt_log("gfx: window closed; exiting"); + fs_save_flush(); vk_pipeline_save(); prof_write(); // ExitProcess runs no atexit handlers ExitProcess(0); diff --git a/runtime/host.h b/runtime/host.h index 43057521..0b38715d 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -237,6 +237,7 @@ void *rt_patch_page(void); // the page the patch void frame_step_init(void); // after rt_patches_init: the real-time step's first value int frame_step_realtime(void); // the real-time step is on: presents ignore the interval uint64_t frame_step_cap_ns(void); // with the real-time step: the shortest frame in ns +int64_t frame_step_world_us(void); // game time since the loaded save's first frame, -1 before // (TOTK_FPS_LIMIT, live) void frame_step_present(uint64_t display_ns, int interval); // nvnQueuePresentTexture: the frame's display time and // present interval (60 Hz ticks) diff --git a/runtime/input_win.c b/runtime/input_win.c index f0c37d20..c2a2553f 100644 --- a/runtime/input_win.c +++ b/runtime/input_win.c @@ -41,7 +41,11 @@ static const SDL_GamepadButton pad_sdl[PAD_COUNT] = { }; // TOTK_INPUT_SCRIPT=: lines "