mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-19 20:25:57 +00:00
Save the pipeline cache and append pipeline keys on an autosave thread, without P.lock: the save held P.lock through vkGetPipelineCacheData and the file write (about 60 ms) and ran on the present thread, so every 20 s the replay stopped at its next pipeline lookup or waited for the present thread (60-80 ms freezes on the 1.4.3 fight); shader record files are written outside RC.lock for the same reason
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
dc69797746
commit
650c69bcb5
3 files changed
+40
-7
No files matched your search
@@ -1626,18 +1626,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 +1806,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);
|
||||
|
||||
@@ -84,7 +84,7 @@ static DWORD WINAPI present_main(void *arg) {
|
||||
j.at && t1 > j.at ? (t1 - j.at) / 1e6 : 0.0);
|
||||
last = t1;
|
||||
last_queued = j.queued;
|
||||
vk_pipeline_autosave(); // off the render thread, outside the present lock
|
||||
vk_pipeline_autosave(); // starts the autosave thread once
|
||||
AcquireSRWLockExclusive(&pl);
|
||||
}
|
||||
return 0;
|
||||
|
||||
+31
-3
@@ -3009,6 +3009,8 @@ void vk_pipeline_stats(VkPipeStats *s) {
|
||||
ReleaseSRWLockShared(&P.lock);
|
||||
}
|
||||
|
||||
static SRWLOCK save_lock = SRWLOCK_INIT; // one cache save at a time; shutdown takes it to destroy the cache
|
||||
|
||||
void vk_pipeline_save(void) {
|
||||
if (P.ready) keys_flush();
|
||||
if (P.ready && P.gpl) {
|
||||
@@ -3036,7 +3038,13 @@ void vk_pipeline_save(void) {
|
||||
}
|
||||
gfx_shader_record_flush(1);
|
||||
if (!P.ready || !P.cache || !P.cache_path[0]) return;
|
||||
AcquireSRWLockExclusive(&P.lock);
|
||||
// Not under P.lock: the driver synchronises P.cache, and holding P.lock through the copy and the file write (about
|
||||
// 60 ms on the 1.4.3 fight) stopped every pipeline lookup of the replay, a freeze at each 20 s autosave.
|
||||
AcquireSRWLockExclusive(&save_lock);
|
||||
if (!P.cache) { // vk_pipeline_shutdown destroyed it
|
||||
ReleaseSRWLockExclusive(&save_lock);
|
||||
return;
|
||||
}
|
||||
size_t size = 0;
|
||||
void *data = NULL;
|
||||
if (vkGetPipelineCacheData(P.device, P.cache, &size, NULL) == VK_SUCCESS && size && (data = malloc(size)) &&
|
||||
@@ -3044,12 +3052,12 @@ void vk_pipeline_save(void) {
|
||||
if (!plat_file_write_atomic(P.cache_path, data, size)) rt_log("vk_pipeline: cannot write %s", P.cache_path);
|
||||
}
|
||||
free(data);
|
||||
ReleaseSRWLockExclusive(&P.lock);
|
||||
ReleaseSRWLockExclusive(&save_lock);
|
||||
}
|
||||
|
||||
// The game never shuts the device down (boot.c and the window end the process), so the cache is saved while it
|
||||
// runs: at most every 20 s, and only after new pipelines were built. New pipeline keys are appended every 2 s.
|
||||
void vk_pipeline_autosave(void) {
|
||||
static void autosave_once(void) {
|
||||
static uint64_t last_n, last_ms, last_keys;
|
||||
if (!P.ready) return;
|
||||
uint64_t now = GetTickCount64();
|
||||
@@ -3063,6 +3071,24 @@ void vk_pipeline_autosave(void) {
|
||||
vk_pipeline_save();
|
||||
}
|
||||
|
||||
// On its own thread: the save (vkGetPipelineCacheData and the file write, about 60 ms) and the key appends (a flush to
|
||||
// the disk each) ran on the present thread, and the render thread waited for it to take the next frame.
|
||||
static DWORD WINAPI autosave_main(void *arg) {
|
||||
(void)arg;
|
||||
for (;;) {
|
||||
Sleep(500);
|
||||
autosave_once();
|
||||
}
|
||||
}
|
||||
|
||||
void vk_pipeline_autosave(void) {
|
||||
static LONG started;
|
||||
if (!P.ready || InterlockedExchange(&started, 1)) return;
|
||||
HANDLE t = CreateThread(NULL, 0, autosave_main, NULL, 0, NULL);
|
||||
if (t) SetThreadPriority(t, THREAD_PRIORITY_BELOW_NORMAL), CloseHandle(t);
|
||||
else rt_log("vk_pipeline: cannot start the autosave thread; the cache is saved at exit only");
|
||||
}
|
||||
|
||||
void vk_pipeline_shutdown(void) {
|
||||
if (!P.ready) return;
|
||||
lb_stop();
|
||||
@@ -3094,8 +3120,10 @@ void vk_pipeline_shutdown(void) {
|
||||
}
|
||||
layouts[i] = NULL;
|
||||
}
|
||||
AcquireSRWLockExclusive(&save_lock); // not while the autosave thread copies it
|
||||
if (P.cache) vkDestroyPipelineCache(P.device, P.cache, NULL);
|
||||
P.cache = VK_NULL_HANDLE;
|
||||
ReleaseSRWLockExclusive(&save_lock);
|
||||
memset(&P.stats, 0, sizeof P.stats);
|
||||
InterlockedExchange(&P.ready, 0);
|
||||
ReleaseSRWLockExclusive(&P.lock);
|
||||
|
||||
Reference in new issue
Block a user