diff --git a/.gitignore b/.gitignore
index d2fdab74..e365e074 100644
--- a/.gitignore
+++ b/.gitignore
@@ -42,6 +42,9 @@ __pycache__/
*.o
*.lib
*.a
+*.so
+*.so.*
+*.AppImage
# .NET build output of the shader translator (tools/build_xlate.py)
/tools/xlate/bin/
diff --git a/docs/building.md b/docs/building.md
index 30ebfd21..f2450d33 100644
--- a/docs/building.md
+++ b/docs/building.md
@@ -117,6 +117,33 @@ your machines without MSYS2, Python or .NET. `romfs.bin` stays out unless you pa
`romfs\` next to the exe or point `TOTK_ROMFS` at its folder. Run `totk.exe --prepare-shaders` once in the copied
folder. The folder holds code and data derived from your game: for your own machines only.
+## Linux (in progress)
+
+The runtime builds and runs on x86-64 Linux with a window, input and menu on SDL3 (`*_sdl.c`) in place of the Win32
+files (`*_win.c`); `runtime/platform/linux.c` implements `platform.h`. Status: the selftest and the probes that need no
+game files pass on RADV. `--probe-vkmem` still fails, because watched guest memory cannot be imported on Linux (below);
+playing the game is not verified yet.
+
+Requirements:
+
+- Linux 6.7 or later: the write watch uses userfaultfd write protection with `PAGEMAP_SCAN`. SteamOS 3.8 qualifies.
+- A Vulkan 1.3 driver with `VK_EXT_external_memory_host` (RADV, NVIDIA).
+- Packages on Arch:
+
+ ```sh
+ pacman -S --needed clang lld ninja make sdl3 opus libvpx zstd vulkan-headers vulkan-icd-loader \
+ vulkan-memory-allocator vulkan-validation-layers shaderc spirv-tools dotnet-sdk uv git
+ ```
+
+The steps are the Windows ones with POSIX paths: `.venv/bin/python` in place of `.venv\Scripts\python`, and `totk` in
+place of `totk.exe`. `build_game.py` takes clang, lld and Ninja from PATH (or the ones in `
/bin` with `--toolchain `) and links against
+the system libraries, with `libstdc++` static and an `$ORIGIN` rpath; `build_hactool.py` builds `hactool` with make.
+
+Memory: on Windows the GPU imports guest memory and `GetWriteWatch` tracks the CPU's writes to it. On Linux,
+re-protecting imported memory invalidates the driver's userptr buffer on every submit (about 1.3 ms per 16 MiB on
+amdgpu), and importing pins the pages, which drops their write protection. Watched guest memory is therefore copied to
+host-visible buffers instead of being imported; that part of `runtime/vk_memory.c` is the next step.
+
## Checks
Before a commit:
diff --git a/runtime/astc_probe.c b/runtime/astc_probe.c
index 21e6f352..1961aaec 100644
--- a/runtime/astc_probe.c
+++ b/runtime/astc_probe.c
@@ -8,8 +8,8 @@
#include
#include
#include
-#include
+#include "platform/platform.h"
#include "host.h"
#include "tex.h"
#include "vk.h"
@@ -109,11 +109,7 @@ static void to_general(VkCommandBuffer cb, VkImage img) {
vkCmdPipelineBarrier2(cb, &di);
}
-static double now_ms(void) {
- LARGE_INTEGER t, f;
- QueryPerformanceCounter(&t), QueryPerformanceFrequency(&f);
- return (double)t.QuadPart * 1e3 / (double)f.QuadPart;
-}
+static double now_ms(void) { return (double)plat_now_ns() / 1e6; }
// Decodes regions (w0 x h0 at level 0, w1 x h1 at level 1) of blk on the GPU; out_rb: the decoder's texels, out_img:
// the image's two levels read back. 0 on a Vulkan failure.
diff --git a/runtime/audren.c b/runtime/audren.c
index f031d031..0ac05f53 100644
--- a/runtime/audren.c
+++ b/runtime/audren.c
@@ -5,7 +5,6 @@
// service validates every section against the header, keeps server-side voice state, advances it in real time (one
// frame of sample_count / sample_rate seconds per render tick, signalling the renderer's event) and writes the output
// status the client expects. The mixer behind it is audio_mixer.c.
-#include
#include
#include
#include
@@ -14,10 +13,11 @@
#include "audren.h"
#include "host.h"
#include "ipc.h"
+#include "platform/platform.h"
-static volatile long g_live;
+static volatile int32_t g_live;
-long audren_renderers_live(void) { return InterlockedCompareExchange(&g_live, 0, 0); }
+long audren_renderers_live(void) { return plat_cas32(&g_live, 0, 0); }
static int revision_number(uint32_t rev) { return (int)(rev >> 24) - '0'; }
@@ -44,7 +44,7 @@ static AudioRenderer *renderer_new(const AudrenParameter *p) {
r->effects = calloc((size_t)(p->effect_count ? p->effect_count : 1), sizeof *r->effects);
r->channels = calloc((size_t)p->voice_count * 6, sizeof *r->channels);
need(r->voices && r->pools && r->mixes && r->sinks && r->effects && r->channels, "renderer tables");
- InitializeSRWLock(&r->lock);
+ plat_mutex_init(&r->lock);
r->event = kern_event_new();
r->state = 1; // stopped
return r;
@@ -195,7 +195,7 @@ static void request_update(AudioRenderer *r, const uint8_t *input, uint64_t in_s
memcpy(&h, input, sizeof h);
need(h.total <= in_size, "update total size");
Reader in = {input + sizeof h, input + h.total};
- AcquireSRWLockExclusive(&r->lock);
+ plat_mutex_lock(&r->lock);
r->revision = h.revision;
const int vc = r->param.voice_count, ec = r->param.effect_count, sc = r->param.sink_count,
mc = r->param.submix_count + 1;
@@ -313,7 +313,7 @@ static void request_update(AudioRenderer *r, const uint8_t *input, uint64_t in_s
w += 0x10; // performance: nothing written
w += AUDREN_BEHAVIOR_OUT; // behavior: no errors
memcpy(w, &r->elapsed_frames, 8);
- ReleaseSRWLockExclusive(&r->lock);
+ plat_mutex_unlock(&r->lock);
}
// ---- render tick: one frame every sample_count / sample_rate seconds, on a host thread
@@ -348,17 +348,14 @@ static void advance_voice(AudioRenderer *r, ServerVoice *v, double out_seconds)
}
}
-static DWORD WINAPI render_thread(void *p) {
+static void render_thread(void *p) {
AudioRenderer *r = p;
- SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
- HANDLE timer = CreateWaitableTimerExW(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
- if (!timer) timer = CreateWaitableTimerExW(NULL, NULL, 0, TIMER_ALL_ACCESS);
- if (!timer) rt_fatal("audren: CreateWaitableTimerExW failed (error %lu)", GetLastError());
+ plat_thread_priority(PLAT_PRIO_HIGHEST);
const uint64_t frame_ns = (uint64_t)r->param.sample_count * 1000000000ull / (uint64_t)r->param.sample_rate;
const double frame = (double)r->param.sample_count / r->param.sample_rate;
uint64_t next = kern_now_ns();
- while (InterlockedCompareExchange(&r->running, 0, 0)) {
- AcquireSRWLockExclusive(&r->lock);
+ while (plat_cas32(&r->running, 0, 0)) {
+ plat_mutex_lock(&r->lock);
if (r->state == 0) {
voices_prepare(r);
audio_mixer_render(r, frame);
@@ -372,20 +369,13 @@ static DWORD WINAPI render_thread(void *p) {
(unsigned long long)r->flushed);
}
}
- ReleaseSRWLockExclusive(&r->lock);
+ plat_mutex_unlock(&r->lock);
kern_event_signal(r->event);
next += frame_ns;
uint64_t now = kern_now_ns();
- if (next > now) {
- LARGE_INTEGER due = {.QuadPart = -(LONGLONG)((next - now + 99) / 100)}; // relative, 100 ns units
- if (!SetWaitableTimer(timer, &due, 0, NULL, NULL, FALSE))
- rt_fatal("audren: SetWaitableTimer failed (error %lu)", GetLastError());
- WaitForSingleObject(timer, INFINITE);
- } else if (now - next > 100000000ull)
- next = now; // fell far behind (debugger, sleep): resynchronise
+ if (next > now) plat_wait_ns(next - now);
+ else if (now - next > 100000000ull) next = now; // fell far behind (debugger, sleep): resynchronise
}
- CloseHandle(timer);
- return 0;
}
// ---- IAudioRenderer
@@ -417,16 +407,16 @@ static void renderer_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *r
return;
}
case 5: // Start
- AcquireSRWLockExclusive(&r->lock);
+ plat_mutex_lock(&r->lock);
r->state = 0;
audio_mixer_set_running(r, 1);
- ReleaseSRWLockExclusive(&r->lock);
+ plat_mutex_unlock(&r->lock);
return;
case 6: // Stop
- AcquireSRWLockExclusive(&r->lock);
+ plat_mutex_lock(&r->lock);
r->state = 1;
audio_mixer_set_running(r, 0);
- ReleaseSRWLockExclusive(&r->lock);
+ plat_mutex_unlock(&r->lock);
return;
case 7: rs->copy[rs->ncopy++] = kern_event_readable(r->event); return; // QuerySystemEvent
case 8: r->time_limit = IPC_ARG(rq, uint32_t, 0); return; // SetRenderingTimeLimit
@@ -437,12 +427,11 @@ static void renderer_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *r
static void renderer_close(void *p) {
AudioRenderer *r = p;
- InterlockedExchange(&r->running, 0);
+ plat_xchg32(&r->running, 0);
int away = kern_core_away();
- WaitForSingleObject(r->thread, INFINITE);
+ plat_thread_join(r->thread);
kern_core_back(&away);
- CloseHandle(r->thread);
- InterlockedDecrement(&g_live);
+ plat_dec32(&g_live);
audio_mixer_close(r);
kern_handle_close(r->event);
rt_log("audren: renderer closed after %llu frames", (unsigned long long)r->elapsed_frames);
@@ -461,9 +450,9 @@ void audren_manager_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs
AudioRenderer *r = renderer_new(&p);
r->running = 1;
audio_mixer_open(r);
- r->thread = CreateThread(NULL, 0, render_thread, r, 0, NULL);
- if (!r->thread) rt_fatal("audren: render thread CreateThread failed (error %lu)", GetLastError());
- InterlockedIncrement(&g_live);
+ r->thread = plat_thread_new(render_thread, r, "audren");
+ if (!r->thread) rt_fatal("audren: cannot start the render thread");
+ plat_inc32(&g_live);
rt_log("audren: renderer open %d Hz x%d, mix buffers %d, voices %d, effects %d, submixes %d, sinks %d, "
"splitters %d/%d, REV%d, work buffer %#llx+%#llx",
p.sample_rate, p.sample_count, p.mix_buffer_count, p.voice_count, p.effect_count, p.submix_count,
diff --git a/runtime/audren.h b/runtime/audren.h
index 92da770d..276ac08b 100644
--- a/runtime/audren.h
+++ b/runtime/audren.h
@@ -3,10 +3,10 @@
// audren:u, the audio renderer service: RequestUpdate wire formats, the server-side
// renderer state, and the mixer interface (audio_mixer.c: the real mixer with SDL3 output).
#pragma once
-#include
#include
#include "ipc.h"
+#include "platform/platform.h"
typedef struct {
int32_t sample_rate, sample_count, mix_buffer_count, submix_count, voice_count, sink_count, effect_count,
@@ -150,9 +150,9 @@ enum { SV_STOPPED, SV_STARTED, SV_PAUSED, SV_STOPPING };
typedef struct AudioRenderer {
AudrenParameter param;
- SRWLOCK lock;
- HANDLE thread;
- volatile long running;
+ PlatMutex lock;
+ PlatThread *thread;
+ volatile int32_t running;
uint32_t state, revision, time_limit, event; // state 0 started, 1 stopped; event: writable handle
int logged;
uint64_t behavior_flags, elapsed_frames;
diff --git a/runtime/boot.c b/runtime/boot.c
index 382ac850..591067e4 100644
--- a/runtime/boot.c
+++ b/runtime/boot.c
@@ -9,7 +9,6 @@
// nvnBootstrapLoader called from main+0xe7eb2c (1.4.3: +0xbe0360) in the graphics bring-up; logs the run
// statistics, exits 0
// With --seconds S the run continues past that call; rt_frame_presented ends it once the world clock passed S.
-#include
#include
#include
#include
@@ -19,6 +18,7 @@
#include "gfx.h"
#include "nvn_api.h"
#include "vk.h"
+#include "platform/platform.h"
#define SYM_INIT_START "_ZN2nn4init5StartEmmPFvvES2_S2_"
// main: return addresses of nnMain's SetFocusHandlingMode call (1.2.1 from this port's analysis of main; 1.4.3 from
@@ -34,7 +34,7 @@ static const InitCount inits_121[] = {{"main", 10673}, {"subsdk0", 45}, {"sdk",
static const InitCount inits_143[] = {{"main", 10717}, {"subsdk0", 45}, {"sdk", 83}};
#define expected_inits (RT_V143 ? inits_143 : inits_121)
-static volatile LONG inits_done, checkpoint_seen;
+static volatile int32_t inits_done, checkpoint_seen;
uint64_t rt_guest_start_ns;
static const RtLoaded *module_named(const char *name) {
@@ -71,7 +71,7 @@ static void cb_loader_ready(Cpu *c) {
// DT_INIT registers the module with musl (__nnmusl_init_dso) and runs its init_array; the constructor flag reads 2
// afterwards (MacNativeGames loader.c:458-462).
static void cb_module_inits(Cpu *c) {
- if (InterlockedCompareExchange(&inits_done, 0, 0)) rt_fatal("boot: module constructors requested twice");
+ if (plat_cas32(&inits_done, 0, 0)) rt_fatal("boot: module constructors requested twice");
for (unsigned k = 0; k < sizeof inits_121 / sizeof *inits_121; k++) {
const RtLoaded *m = module_named(expected_inits[k].name);
uint64_t entries = m->init_array_size / 8;
@@ -85,7 +85,7 @@ static void cb_module_inits(Cpu *c) {
rt_fatal("boot: %s has %llu init_array entries, this game version has %llu", m->name, (unsigned long long)entries,
(unsigned long long)expected_inits[k].entries);
}
- InterlockedExchange(&inits_done, 1);
+ plat_xchg32(&inits_done, 1);
}
static void cb_module_finis(Cpu *c) {
@@ -109,12 +109,12 @@ void rt_boot_event(const char *what, Cpu *c, const char *detail) {
if (ret != main_mod->base + CHECKPOINT_RET)
rt_fatal("boot: checkpoint %s reached from %s, expected main+%#llx", detail, at,
(unsigned long long)CHECKPOINT_RET);
- if (!InterlockedCompareExchange(&inits_done, 0, 0)) rt_fatal("boot: checkpoint %s before DT_INIT", detail);
+ if (!plat_cas32(&inits_done, 0, 0)) rt_fatal("boot: checkpoint %s before DT_INIT", detail);
for (int i = 0; i < ldr_count(); i++)
if (ctor_state(ldr_module(i)) != CTOR_DONE)
rt_fatal("boot: checkpoint %s: %s constructor flag reads %u", detail, ldr_module(i)->name,
ctor_state(ldr_module(i)));
- InterlockedExchange(&checkpoint_seen, 1);
+ plat_xchg32(&checkpoint_seen, 1);
rt_logf("boot: checkpoint %s from main+%#llx\n", detail, (unsigned long long)(ret - main_mod->base));
return;
}
@@ -122,7 +122,7 @@ void rt_boot_event(const char *what, Cpu *c, const char *detail) {
if (ret != main_mod->base + NVN_RET)
rt_fatal("boot: first NVN call %s(\"%s\") from %s, expected main+%#llx", "nvnBootstrapLoader", detail, at,
(unsigned long long)NVN_RET);
- if (!InterlockedCompareExchange(&checkpoint_seen, 0, 0))
+ if (!plat_cas32(&checkpoint_seen, 0, 0))
rt_fatal("boot: first NVN call before the SetFocusHandlingMode checkpoint");
rt_logf("boot: first NVN call nvnBootstrapLoader(\"%s\") from main+%#llx\n", detail,
(unsigned long long)(ret - main_mod->base));
@@ -131,7 +131,7 @@ void rt_boot_event(const char *what, Cpu *c, const char *detail) {
stats_line();
fflush(stdout);
fflush(stderr);
- _exit(0); // other guest threads are still running; nothing of theirs needs an orderly end
+ plat_exit(0); // other guest threads are still running; nothing of theirs needs an orderly end
}
rt_fatal("boot: unknown boot event %s (%s) from %s", what, detail ? detail : "", at);
}
@@ -241,7 +241,7 @@ static void series_shot(const tex_t *t, uint64_t n) {
step = (unsigned long long)(ws * 1e6), next = lo;
else if (!e || sscanf(e, "%llu-%llu:%llu", &lo, &hi, &step) != 3 || !step) hi = 0;
dir = cfg_get("TOTK_SHOTS_DIR") ? cfg_get("TOTK_SHOTS_DIR") : ".";
- if (hi) CreateDirectoryA(dir, NULL);
+ if (hi) plat_file_mkdir_p(dir);
parsed = 1;
}
if (world) { // the first present at or after each step of the world clock
@@ -275,7 +275,7 @@ static void series_shot(const tex_t *t, uint64_t n) {
for (int c = 0; c < 4; c++) o[c] = (uint8_t)(sum[bgra && c != 3 ? 2 - c : c] / k);
o[3] = 255;
}
- char path[MAX_PATH];
+ char path[PLAT_PATH_MAX];
snprintf(path, sizeof path, "%s/f%05llu.png", dir, (unsigned long long)n);
rt_write_png(path, out, ow, oh);
free(out);
@@ -304,7 +304,7 @@ void rt_frame_presented(const tex_t *t, uint64_t n) {
vk_pipeline_save(); // no-op without the Vulkan back end
prof_write();
fflush(stderr);
- _exit(0);
+ plat_exit(0);
}
// ---- steps 9-14
@@ -321,22 +321,22 @@ static void guest_main(Cpu *c) {
rt_fatal("boot: nn::init::Start returned");
}
-static DWORD WINAPI prewarm_main(void *arg) {
+static void prewarm_main(void *arg) {
(void)arg;
vk_pipeline_prewarm();
const char *prep = cfg_get("TOTK_TEST_PREPARE_SHADERS"); // Task 2.3 test hook; Task 2.6 builds the preparation
if (prep && !strcmp(prep, "1")) {
- int cpus = (int)GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
+ int cpus = (int)plat_cpu_count();
gfx_shader_prepare_recorded(cpus, cpus / 2 > 16 ? 16 : cpus / 2 < 1 ? 1 : cpus / 2);
}
- return 0;
+ return;
}
void rt_boot(void) {
check_npdm();
if (!rt_config()->shot) vk_pipeline_async_enable(); // a --shot image must not depend on build timing
// Shaders and pipelines recorded in earlier runs are made ready while the modules load; the guest waits for them.
- HANDLE warm = CreateThread(NULL, 0, prewarm_main, NULL, 0, NULL);
+ PlatThread *warm = plat_thread_new(prewarm_main, NULL, "prewarm");
ldr_load(rt_config()->exefs);
rt_patches_init();
frame_step_init();
@@ -345,17 +345,12 @@ void rt_boot(void) {
cb_addr[1] = hostfn_register("", cb_module_inits);
cb_addr[2] = hostfn_register("", cb_module_finis);
hostfn_seal();
- ULONGLONG held = GetTickCount64();
- if (warm) WaitForSingleObject(warm, INFINITE), CloseHandle(warm);
+ uint64_t held = plat_now_ms();
+ if (warm) plat_thread_join(warm);
else vk_pipeline_prewarm();
- held = GetTickCount64() - held;
- FILETIME created, x0, x1, x2, now;
- GetProcessTimes(GetCurrentProcess(), &created, &x0, &x1, &x2);
- GetSystemTimeAsFileTime(&now);
- ULARGE_INTEGER c = {.LowPart = created.dwLowDateTime, .HighPart = created.dwHighDateTime},
- n = {.LowPart = now.dwLowDateTime, .HighPart = now.dwHighDateTime};
+ held = plat_now_ms() - held;
rt_log("boot: guest starts %llu ms after process start (%llu ms of it waiting for the prewarm)",
- (unsigned long long)((n.QuadPart - c.QuadPart) / 10000), (unsigned long long)held);
+ (unsigned long long)(plat_process_uptime_ns() / 1000000), (unsigned long long)held);
rt_guest_start_ns = kern_now_ns();
kern_run_on_guest_main(guest_main);
rt_fatal("boot: the guest main thread ended");
diff --git a/runtime/cfg.c b/runtime/cfg.c
index 84abed08..e52a7fb9 100644
--- a/runtime/cfg.c
+++ b/runtime/cfg.c
@@ -163,11 +163,11 @@ static void add(char *key, char *value, int line) {
for (int i = 0; i < n_entries; i++)
if (!strcmp(entries[i].key, key)) { // a repeated key: the later line wins
free(entries[i].value);
- entries[i].value = _strdup(value);
+ entries[i].value = strdup(value);
return;
}
if (n_entries == CFG_MAX_KEYS) return;
- entries[n_entries++] = (CfgEntry){_strdup(key), _strdup(value), 0, 0};
+ entries[n_entries++] = (CfgEntry){strdup(key), strdup(value), 0, 0};
}
int cfg_load(const char *path) {
@@ -201,7 +201,7 @@ const char *cfg_get(const char *name) {
if (e || strncmp(name, PREFIX, PREFIX_LEN)) return e;
int n = __atomic_load_n(&n_entries, __ATOMIC_ACQUIRE);
for (int i = 0; i < n; i++)
- if (!_stricmp(entries[i].key, name + PREFIX_LEN)) return __atomic_load_n(&entries[i].value, __ATOMIC_ACQUIRE);
+ if (!strcasecmp(entries[i].key, name + PREFIX_LEN)) return __atomic_load_n(&entries[i].value, __ATOMIC_ACQUIRE);
return NULL;
}
@@ -219,12 +219,12 @@ void cfg_set(const char *name, const char *value) {
while (i < n && strcmp(entries[i].key, key)) i++;
if (i < n) {
if (!entries[i].value || strcmp(entries[i].value, value)) {
- __atomic_store_n(&entries[i].value, _strdup(value), __ATOMIC_RELEASE); // the old string stays for readers
+ __atomic_store_n(&entries[i].value, strdup(value), __ATOMIC_RELEASE); // the old string stays for readers
entries[i].dirty = 1;
}
entries[i].removed = 0; // a value set after cfg_reset replaces the line instead of deleting it
} else if (n < CFG_MAX_KEYS) {
- entries[n] = (CfgEntry){_strdup(key), _strdup(value), 1, 0};
+ entries[n] = (CfgEntry){strdup(key), strdup(value), 1, 0};
__atomic_store_n(&n_entries, n + 1, __ATOMIC_RELEASE);
}
__atomic_add_fetch(&version, 1, __ATOMIC_RELEASE);
@@ -244,7 +244,7 @@ int cfg_reset(const char *const *keys, int n) {
while (i < n_entries && strcmp(entries[i].key, name)) i++;
if (i == n_entries) { // no value in memory ("key =" in the file): the line still has to go
if (i == CFG_MAX_KEYS) continue;
- entries[i] = (CfgEntry){_strdup(name), NULL, 0, 1};
+ entries[i] = (CfgEntry){strdup(name), NULL, 0, 1};
__atomic_store_n(&n_entries, i + 1, __ATOMIC_RELEASE);
} else if (entries[i].value) {
__atomic_store_n(&entries[i].value, NULL, __ATOMIC_RELEASE); // the old string stays for readers
@@ -540,7 +540,7 @@ static int find_ini(void) {
if (ini_path[0]) return 1;
char path[1024];
if (!plat_exe_path(path, sizeof path)) return 0;
- char *cut = strrchr(path, '\\');
+ char *cut = plat_last_sep(path);
if (!cut) cut = strrchr(path, '/');
if (!cut || cut + 10 >= path + sizeof path) return 0;
strcpy(cut + 1, "totk.ini");
diff --git a/runtime/cfg_probe.c b/runtime/cfg_probe.c
index 3e4ee435..99ff3fc7 100644
--- a/runtime/cfg_probe.c
+++ b/runtime/cfg_probe.c
@@ -17,7 +17,7 @@ static int fails;
int probe_cfg(void) {
char path[1024];
if (!plat_exe_path(path, sizeof path)) return 1;
- char *cut = strrchr(path, '\\');
+ char *cut = plat_last_sep(path);
if (!cut) return 1;
strcpy(cut + 1, "cfg-probe.ini");
static const char ini[] =
@@ -35,8 +35,8 @@ int probe_cfg(void) {
"no_such_setting = 1\r\n"
"no equals sign\r\n"
"last = no newline at the end";
- _putenv_s("TOTK_LOCALE", ""); // an empty variable is how _putenv_s spells "remove" on Windows
- _putenv_s("TOTK_FPS60", "");
+ plat_setenv("TOTK_LOCALE", ""); // an empty variable is how _putenv_s spells "remove" on Windows
+ plat_setenv("TOTK_FPS60", "");
CHECK(plat_file_write(path, ini, sizeof ini - 1), "write the probe ini");
CHECK(cfg_load(path) == 1, "load the probe ini");
CHECK(EQ(cfg_get("TOTK_FPS60"), "0"), "value read, whitespace trimmed, lower-case key");
@@ -48,9 +48,9 @@ int probe_cfg(void) {
CHECK(cfg_get("TOTK_CLOTH_REALTIME") == NULL, "absent key is NULL");
CHECK(cfg_get("TOTK_comment") == NULL, "comment is not a key");
CHECK(EQ(cfg_get("TOTK_NO_SUCH_SETTING"), "1"), "unknown key is still readable");
- _putenv_s("TOTK_FPS60", "1");
+ plat_setenv("TOTK_FPS60", "1");
CHECK(EQ(cfg_get("TOTK_FPS60"), "1"), "environment overrides the file");
- _putenv_s("TOTK_FPS60", "");
+ plat_setenv("TOTK_FPS60", "");
CHECK(EQ(cfg_get("TOTK_FPS60"), "0"), "file value returns when the variable is removed");
plat_file_delete(path);
CHECK(cfg_load(path) == 0, "missing file is not an error");
@@ -90,9 +90,9 @@ int probe_cfg(void) {
"\r\n"
"[audio]\r\n"
"volume = 40\r\n";
- _putenv_s("TOTK_MOTION", "");
- _putenv_s("TOTK_VOLUME", "");
- _putenv_s("TOTK_RUMBLE_STRENGTH", "");
+ plat_setenv("TOTK_MOTION", "");
+ plat_setenv("TOTK_VOLUME", "");
+ plat_setenv("TOTK_RUMBLE_STRENGTH", "");
CHECK(plat_file_write(path, before, sizeof before - 1), "write the save ini");
CHECK(cfg_load(path) == 1, "load the save ini");
unsigned v0 = cfg_version();
@@ -117,11 +117,11 @@ int probe_cfg(void) {
EQ(cfg_get("TOTK_RUMBLE_STRENGTH"), "80") && EQ(cfg_get("TOTK_VOLUME"), "40") &&
EQ(cfg_get("TOTK_ROMFS_MODS"), "C:\\mods"), "the saved file reads back");
const char *held = cfg_get("TOTK_VOLUME");
- _putenv_s("TOTK_VOLUME", "70");
+ plat_setenv("TOTK_VOLUME", "70");
cfg_set("TOTK_VOLUME", "90");
CHECK(cfg_from_env("TOTK_VOLUME") && EQ(cfg_get("TOTK_VOLUME"), "70"), "environment wins over cfg_set");
CHECK(EQ(held, "40"), "a pointer cfg_get handed out stays valid after cfg_set");
- _putenv_s("TOTK_VOLUME", "");
+ plat_setenv("TOTK_VOLUME", "");
CHECK(!cfg_from_env("TOTK_VOLUME") && EQ(cfg_get("TOTK_VOLUME"), "90"), "cfg_set value returns without the variable");
plat_file_delete(path);
CHECK(cfg_save_to(path) == 1, "cfg_save_to creates a missing file");
@@ -147,7 +147,7 @@ int probe_cfg(void) {
// cfg_reset: the key's active lines leave totk.ini, the commented example stays
static const char rbase[] = "[audio]\n# volume = 100\n[input]\nmotion = 0\n";
static const char *const vol[] = {"VOLUME"};
- _putenv_s("TOTK_VOLUME", "");
+ plat_setenv("TOTK_VOLUME", "");
CHECK(plat_file_write(path, rbase, sizeof rbase - 1), "write the reset ini");
CHECK(cfg_load(path) == 1, "load the reset ini");
cfg_set("TOTK_VOLUME", "40");
@@ -186,9 +186,9 @@ int probe_cfg(void) {
// a key the environment sets is skipped and keeps its file line
CHECK(cfg_load(path) == 1 && EQ(cfg_get("TOTK_VOLUME"), "60"), "reset: reload");
- _putenv_s("TOTK_VOLUME", "70");
+ plat_setenv("TOTK_VOLUME", "70");
CHECK(cfg_reset(vol, 1) == 0, "reset: an environment-set key is skipped");
- _putenv_s("TOTK_VOLUME", "");
+ plat_setenv("TOTK_VOLUME", "");
CHECK(EQ(cfg_get("TOTK_VOLUME"), "60"), "reset: the skipped key keeps its value");
CHECK(cfg_save_to(path) == 1, "reset: save after a skip");
got = plat_file_read(path, &got_n);
@@ -200,7 +200,7 @@ int probe_cfg(void) {
cfg_set("TOTK_VOLUME", "20");
char bad[1100];
snprintf(bad, sizeof bad, "%s", path);
- strcpy(strrchr(bad, '\\') + 1, "no-such-dir\\cfg-probe.ini");
+ strcpy(plat_last_sep(bad) + 1, "no-such-dir/cfg-probe.ini");
CHECK(cfg_save_to(bad) == 0 && cfg_last_save_ok() == 0 && EQ(cfg_get("TOTK_VOLUME"), "20"),
"save status: an unwritable path fails, the value stays");
cfg_set("TOTK_VOLUME", "21");
@@ -331,24 +331,24 @@ int probe_cfg(void) {
CHECK(!strcmp(line, "none"), "edit: a stick direction without keys is none");
// defaults live in the CfgMeta table: cfg_value and the typed accessors fall back to it
cfg_load("cfg-probe-missing.ini");
- _putenv_s("TOTK_FPS60", "");
- _putenv_s("TOTK_MOUSE_SENS", "");
- _putenv_s("TOTK_FREE_CORES", "");
+ plat_setenv("TOTK_FPS60", "");
+ plat_setenv("TOTK_MOUSE_SENS", "");
+ plat_setenv("TOTK_FREE_CORES", "");
CHECK(cfg_bool("TOTK_FPS60") == 1, "default: unset FPS60 is on");
CHECK(EQ(cfg_value("TOTK_VK_PRESENT"), "mailbox") && cfg_int("TOTK_SPLIT_DRAWS") == 64, "default: text and integer");
- _putenv_s("TOTK_MOUSE_SENS", "2.5");
+ plat_setenv("TOTK_MOUSE_SENS", "2.5");
CHECK(cfg_double("TOTK_MOUSE_SENS") == 2.5, "double: the environment value");
- _putenv_s("TOTK_MOUSE_SENS", "fast");
+ plat_setenv("TOTK_MOUSE_SENS", "fast");
CHECK(cfg_double("TOTK_MOUSE_SENS") == 1.0, "double: a value that does not parse gives the default");
- _putenv_s("TOTK_MOUSE_SENS", "");
+ plat_setenv("TOTK_MOUSE_SENS", "");
cfg_set("TOTK_MOUSE_SENS", "3");
CHECK(cfg_double("TOTK_MOUSE_SENS") == 3.0 && cfg_int("TOTK_MOUSE_SENS") == 3, "a cfg_set value beats the default");
CHECK(cfg_bool("TOTK_FREE_CORES") == 0, "free_cores unset: off");
- _putenv_s("TOTK_FREE_CORES", "0");
+ plat_setenv("TOTK_FREE_CORES", "0");
CHECK(cfg_bool("TOTK_FREE_CORES") == 0, "free_cores 0 gives the same as unset");
- _putenv_s("TOTK_FREE_CORES", "1");
+ plat_setenv("TOTK_FREE_CORES", "1");
CHECK(cfg_bool("TOTK_FREE_CORES") == 1, "free_cores 1: on");
- _putenv_s("TOTK_FREE_CORES", "");
+ plat_setenv("TOTK_FREE_CORES", "");
cfg_load("cfg-probe-missing.ini");
int listed = 0;
for (int i = 0; cfg_meta_at(i); i++) {
@@ -357,7 +357,7 @@ int probe_cfg(void) {
listed++;
char name[96];
snprintf(name, sizeof name, "TOTK_%s", m->key);
- _putenv_s(name, "");
+ plat_setenv(name, "");
CHECK(EQ(cfg_value(name), m->def), "cfg_value returns the table default");
int ok = 0;
if (!m->values) ok = 1;
diff --git a/runtime/dispatch.c b/runtime/dispatch.c
index e9bc46a9..79bff30f 100644
--- a/runtime/dispatch.c
+++ b/runtime/dispatch.c
@@ -12,17 +12,13 @@
// is also fixed at init; they are looked up only after the flat table misses.
#include "dispatch.h"
#include "host.h"
+#include "platform/platform.h"
#include
#include
#include
#include
-#ifdef _WIN32
-#include
-#else
-#include
-#endif
#define TABLE_SPAN_LIMIT (1ull << 30) // guest bytes covered by the table, and its size in bytes
#define HOST_CAP 8192 // host function registry, power of two
@@ -92,21 +88,11 @@ void rt_register_host(uint64_t addr, GuestFn fn) {
}
static void *table_alloc(uint64_t bytes) {
-#ifdef _WIN32
- return VirtualAlloc(NULL, bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); // zero pages, backed on first write
-#else
- void *p = mmap(NULL, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
- return p == MAP_FAILED ? NULL : p;
-#endif
+ return plat_mem_alloc(bytes); // zero pages, backed on first write
}
static void table_seal(void *p, uint64_t bytes) {
-#ifdef _WIN32
- DWORD old;
- if (!VirtualProtect(p, bytes, PAGE_READONLY, &old)) rt_fatal("dispatch: VirtualProtect failed (%lu)", GetLastError());
-#else
- if (mprotect(p, bytes, PROT_READ)) rt_fatal("dispatch: mprotect failed");
-#endif
+ if (!plat_mem_protect((uint64_t)(uintptr_t)p, bytes, PLAT_PROT_READ)) rt_fatal("dispatch: sealing the table failed");
}
void rt_dispatch_init(const RtModule *mods, int count) {
diff --git a/runtime/flight.c b/runtime/flight.c
index cb9f26f1..d32918e4 100644
--- a/runtime/flight.c
+++ b/runtime/flight.c
@@ -1,5 +1,5 @@
// TOTK_FLIGHT=1 flight recorder: a ring of the last TOTK_FLIGHT_FRAMES (600) presents, one record each (interval,
-// render-thread blocked time per perf kind, event counts) and a 320x180 thumbnail each (gfx_window.c, blitted on the
+// render-thread blocked time per perf kind, event counts) and a 320x180 thumbnail each (gfx_present.c, blitted on the
// GPU by the present thread). A dump goes to /-f/: frames.tsv and f.png per
// thumbnail, written by a background thread. Triggers: F8 while the game window has focus, a frame interval over
// TOTK_FLIGHT_SPIKE_MS (100; 0 off; at most one dump per 10 s), and TOTK_FLIGHT_AT=[,...]. A dump holds
@@ -7,7 +7,6 @@
//
// Records come from perf_present (prof_win.c) on the render thread; the counts are differences of the TOTK_TRACE=perf
// running totals and of vk_exec_frame. Off, the only cost is the flight_on branch in perf_present and vk_backend.c.
-#include
#include
#include
#include
@@ -16,8 +15,10 @@
#include "flight.h"
#include "gfx.h"
#include "host.h"
+#include "input.h"
#include "vk.h"
#include "vk_exec.h"
+#include "platform/platform.h"
#define FLIGHT_POST 30 // presents recorded after the trigger before the dump starts
#define FLIGHT_SPIKE_GAP_NS 10000000000ull
@@ -41,7 +42,7 @@ typedef struct {
int n; // records, oldest first
uint64_t trig_frame, trig_interval_us;
const char *why;
- char dir[MAX_PATH];
+ char dir[PLAT_PATH_MAX];
Rec r[];
} DumpJob;
@@ -54,7 +55,7 @@ static uint64_t dump_at, trig_frame, trig_interval_us, last_spike_ns;
static const char *trig_why;
static uint64_t at_frames[16];
static int n_at;
-static char base_dir[MAX_PATH];
+static char base_dir[PLAT_PATH_MAX];
static uint64_t prev_cnt[PERF_CNT_N], prev_xlated;
static VkExecFrame prev_exec;
static volatile uint64_t cost_ns, cost_max_ns; // render-thread time in flight_frame, for the dump log line
@@ -79,15 +80,14 @@ void flight_init(void) {
p = *end == ',' ? end + 1 : end;
}
const char *d = cfg_get("TOTK_FLIGHT_DIR");
- char tmp[MAX_PATH];
+ char tmp[PLAT_PATH_MAX];
if (d && *d) snprintf(tmp, sizeof tmp, "%s", d);
else { // /../flight: build/flight for build/v121*/totk.exe
- DWORD k = GetModuleFileNameA(NULL, tmp, sizeof tmp);
- char *slash = k ? strrchr(tmp, '\\') : NULL;
- if (slash) snprintf(slash, sizeof tmp - (size_t)(slash - tmp), "\\..\\flight");
+ char *slash = plat_exe_path(tmp, sizeof tmp) ? plat_last_sep(tmp) : NULL;
+ if (slash) snprintf(slash, sizeof tmp - (size_t)(slash - tmp), "/../flight");
else snprintf(tmp, sizeof tmp, "flight");
}
- if (!GetFullPathNameA(tmp, sizeof base_dir, base_dir, NULL)) snprintf(base_dir, sizeof base_dir, "%s", tmp);
+ if (!plat_full_path(tmp, base_dir, sizeof base_dir)) snprintf(base_dir, sizeof base_dir, "%s", tmp);
thumbs_on = env_u("TOTK_FLIGHT_THUMBS", 1) != 0;
if (thumbs_on) vk_thumbs_enable(ring_n);
flight_on = 1;
@@ -225,15 +225,15 @@ static int write_png(const char *path, const uint8_t *rgba, PngBufs *pb) {
}
// ---------------------------------------------------------------- dump thread
-static DWORD WINAPI dump_main(void *arg) {
+static void dump_main(void *arg) {
DumpJob *j = arg;
- uint64_t t_start = GetTickCount64();
- SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
- CreateDirectoryA(base_dir, NULL);
- if (!CreateDirectoryA(j->dir, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) {
- rt_log("flight: cannot create %s (error %lu)", j->dir, GetLastError());
+ uint64_t t_start = plat_now_ms();
+ plat_thread_priority(PLAT_PRIO_BELOW_NORMAL);
+ plat_file_mkdir_p(j->dir);
+ if (!plat_dir_exists(j->dir)) {
+ rt_log("flight: cannot create %s", j->dir);
free(j);
- return 0;
+ return;
}
uint8_t *have = calloc((size_t)j->n, 1);
int n_thumbs = 0;
@@ -250,23 +250,23 @@ static DWORD WINAPI dump_main(void *arg) {
// Oldest first: each slot is copied out long before the presents that go on meanwhile reach it again. The newest
// few complete only when their present fence has been waited for, so they get a second try.
for (int pass = 0; pass < 2 && pb.raw && pb.z && pb.head && px; pass++) {
- if (pass) Sleep(200);
+ if (pass) plat_sleep_ms(200);
for (int i = pass ? (j->n > 8 ? j->n - 8 : 0) : 0; i < j->n; i++) {
if (have[i] || !vk_thumbs_read(j->r[i].frame, px)) continue;
- char path[MAX_PATH + 32];
- snprintf(path, sizeof path, "%s\\f%06llu.png", j->dir, (unsigned long long)j->r[i].frame);
+ char path[PLAT_PATH_MAX + 32];
+ snprintf(path, sizeof path, "%s/f%06llu.png", j->dir, (unsigned long long)j->r[i].frame);
if (write_png(path, px, &pb)) have[i] = 1, n_thumbs++;
}
}
free(px), free(pb.raw), free(pb.z), free(pb.head);
}
- char path[MAX_PATH + 32];
- snprintf(path, sizeof path, "%s\\frames.tsv", j->dir);
+ char path[PLAT_PATH_MAX + 32];
+ snprintf(path, sizeof path, "%s/frames.tsv", j->dir);
FILE *f = fopen(path, "w");
if (!f) {
rt_log("flight: cannot write %s", path);
free(have), free(j);
- return 0;
+ return;
}
fprintf(f, "# totk flight dump: %s at frame %llu, interval %.1f ms\n", j->why, (unsigned long long)j->trig_frame,
j->trig_interval_us / 1000.0);
@@ -285,11 +285,11 @@ static DWORD WINAPI dump_main(void *arg) {
rt_log("flight: dump %s: %s at frame %llu (%.1f ms), frames %llu-%llu, %d thumbnails, written in %llu ms; "
"recorder %.1f us per frame (max %.0f us)", j->dir,
j->why, (unsigned long long)j->trig_frame, j->trig_interval_us / 1000.0, (unsigned long long)j->r[0].frame,
- (unsigned long long)j->r[j->n - 1].frame, n_thumbs, (unsigned long long)(GetTickCount64() - t_start),
+ (unsigned long long)j->r[j->n - 1].frame, n_thumbs, (unsigned long long)(plat_now_ms() - t_start),
recorded ? cost_ns / 1e3 / (double)recorded : 0.0, cost_max_ns / 1e3);
free(have);
free(j);
- return 0;
+ return;
}
static void start_dump(void) {
@@ -301,13 +301,11 @@ static void start_dump(void) {
j->trig_interval_us = trig_interval_us;
j->why = trig_why;
for (int i = 0; i < n; i++) j->r[i] = ring[(recorded - (uint64_t)n + (uint64_t)i) % (uint64_t)ring_n];
- SYSTEMTIME st;
- GetLocalTime(&st);
- snprintf(j->dir, sizeof j->dir, "%s\\%04u%02u%02u-%02u%02u%02u-f%llu", base_dir, st.wYear, st.wMonth, st.wDay, st.wHour,
- st.wMinute, st.wSecond, (unsigned long long)trig_frame);
- HANDLE h = CreateThread(NULL, 0, dump_main, j, 0, NULL);
- if (h) CloseHandle(h);
- else rt_log("flight: cannot start the dump thread (error %lu)", GetLastError()), free(j);
+ struct tm st;
+ plat_local_time(&st);
+ snprintf(j->dir, sizeof j->dir, "%s/%04d%02d%02d-%02d%02d%02d-f%llu", base_dir, st.tm_year + 1900, st.tm_mon + 1,
+ st.tm_mday, st.tm_hour, st.tm_min, st.tm_sec, (unsigned long long)trig_frame);
+ if (!plat_thread_spawn(dump_main, j, "flight dump", 0)) rt_log("flight: cannot start the dump thread"), free(j);
}
static void trigger(uint64_t frame, uint64_t interval_ns, const char *why) {
@@ -351,8 +349,7 @@ void flight_frame(uint64_t frame, uint64_t now_ns, uint64_t interval_ns, const u
recorded++;
static int key_was;
- HWND w = gfx_window_handle();
- int key = w && w == GetForegroundWindow() && (GetAsyncKeyState(VK_F8) & 0x8000);
+ int key = input_flight_key();
if (key && !key_was) trigger(frame, interval_ns, "F8");
key_was = key;
for (int i = 0; i < n_at; i++)
diff --git a/runtime/flight.h b/runtime/flight.h
index 32be3ab2..5125f7b6 100644
--- a/runtime/flight.h
+++ b/runtime/flight.h
@@ -14,7 +14,7 @@ void flight_init(void); // prof_start calls it
void flight_frame(uint64_t frame, uint64_t now_ns, uint64_t interval_ns, const uint64_t blocked_ns[],
const volatile int64_t counts[]);
-// gfx_window.c: the present thread blits every presented frame into a host-visible ring of n thumbnails.
+// gfx_present.c: the present thread blits every presented frame into a host-visible ring of n thumbnails.
void vk_thumbs_enable(int n); // before the first present; n slots of FLIGHT_THUMB_W x FLIGHT_THUMB_H
void vk_thumbs_frame(uint64_t frame); // the present thread names the frame of the vk_present_image call that follows
// Copies frame's thumbnail as RGBA when the ring still holds it complete; 0 when it was never written, is still in
diff --git a/runtime/frame_rate.c b/runtime/frame_rate.c
index bb360bf8..be472736 100644
--- a/runtime/frame_rate.c
+++ b/runtime/frame_rate.c
@@ -24,7 +24,7 @@
//
// 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
+// (input.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
diff --git a/runtime/fs.h b/runtime/fs.h
index 07cfe7a0..cc72e289 100644
--- a/runtime/fs.h
+++ b/runtime/fs.h
@@ -42,8 +42,6 @@ const char *romfs_overlay(size_t k, const char **file, uint64_t *size, int *is_a
void fs_trace(const char *what, const char *path); // TOTK_TRACE=fs: one line per open
// ---- fs_host.c
-wchar_t *fs_wide(const char *utf8); // UTF-16 copy for the W APIs; free() it. Fatal on
- // invalid UTF-8
void fs_host_set_profile(const char *dir); // profile directory; before the first storage mount only
// (default rt_config()->profile)
int fs_save_busy(void); // a save is being written: a save file open for writing,
diff --git a/runtime/fs_host.c b/runtime/fs_host.c
index 69c88f8e..ba38214e 100644
--- a/runtime/fs_host.c
+++ b/runtime/fs_host.c
@@ -15,8 +15,7 @@
// 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. 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
+// UTF-8 (platform.h plat_fs_*).
#include
#include
#include
@@ -37,58 +36,79 @@
enum { M_FREE, M_SAVE, M_TEMP, M_CACHE, M_SAVE_RO }; // M_SAVE: the working copy; M_SAVE_RO: the committed save
typedef struct { int kind; char name[32]; char root[1024]; } Mount;
-typedef struct { HANDLE h; int mode, save; } File; // h NULL = free; save: on a save-data mount
-typedef struct { HANDLE find; WIN32_FIND_DATAW next; int has_next, mode; } Dir; // find NULL = free
+typedef struct { PlatFile *h; int mode, save; } File; // h NULL = free; save: on a save-data mount
+typedef struct { char *name; int kind; uint64_t size; } Listed;
+typedef struct { Listed *e; int n, next, mode, used; } Dir; // the directory's entries at open; used 0 = free
static Mount mounts[NMOUNTS];
static File files[NFILES];
static Dir dirs[NDIRS];
-static SRWLOCK lock = SRWLOCK_INIT;
+static PlatMutex lock = PLAT_MUTEX_INIT;
static char profile[1024], save_root[1100], work_root[1100];
-static volatile LONG storage_ready;
+static volatile int32_t storage_ready;
static void need(int c, const char *m) {
if (!c) rt_fatal("fs: %s", m);
}
-wchar_t *fs_wide(const char *s) {
- int n = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s, -1, NULL, 0);
- if (n <= 0) rt_fatal("fs: path is not valid UTF-8");
- wchar_t *w = malloc((size_t)n * sizeof *w);
- need(w != NULL, "path allocation");
- MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s, -1, w, n);
- return w;
+// ---- directory listings: every entry but "." and "..", taken whole before the caller changes the directory.
+typedef struct { Listed *e; int n, cap; } Listing;
+
+static int listing_add(const char *name, int kind, uint64_t size, void *ctx) {
+ Listing *l = ctx;
+ if (l->n == l->cap) {
+ l->cap = l->cap ? l->cap * 2 : 16;
+ l->e = realloc(l->e, (size_t)l->cap * sizeof *l->e);
+ need(l->e != NULL, "directory listing");
+ }
+ l->e[l->n].name = strdup(name);
+ need(l->e[l->n].name != NULL, "directory listing");
+ l->e[l->n].kind = kind, l->e[l->n].size = size;
+ l->n++;
+ return 1;
}
-static DWORD attributes(const char *path) {
- wchar_t *w = fs_wide(path);
- DWORD a = GetFileAttributesW(w);
- free(w);
- return a;
+static int list_dir(const char *dir, Listing *l) {
+ *l = (Listing){0};
+ return plat_fs_list(dir, listing_add, l);
+}
+
+static void listing_free(Listed *e, int n) {
+ for (int i = 0; i < n; i++) free(e[i].name);
+ free(e);
+}
+
+// dir/name in a malloc'd string.
+static char *join(const char *dir, const char *name) {
+ size_t n = strlen(dir), k = strlen(name);
+ char *p = malloc(n + k + 2);
+ need(p != NULL, "path allocation");
+ memcpy(p, dir, n);
+ p[n] = '/';
+ memcpy(p + n + 1, name, k + 1);
+ return p;
}
// Save in progress: a save-data file open for writing, or save data changed in the last 3 s and not committed yet
// (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 volatile int32_t save_writers, save_dirty;
+static volatile int64_t 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);
+ plat_xchg64(&save_changed_ns, (int64_t)plat_now_ns());
+ plat_xchg32(&save_dirty, 1);
}
int fs_save_busy(void) {
- if (InterlockedCompareExchange(&save_writers, 0, 0) > 0) return 1;
- return InterlockedCompareExchange(&save_dirty, 0, 0) &&
- plat_now_ns() - (uint64_t)InterlockedCompareExchange64(&save_changed_ns, 0, 0) < 3000000000ull;
+ if (plat_cas32(&save_writers, 0, 0) > 0) return 1;
+ return plat_cas32(&save_dirty, 0, 0) &&
+ plat_now_ns() - (uint64_t)plat_cas64(&save_changed_ns, 0, 0) < 3000000000ull;
}
// ---- host directories
static int make_dir(const char *p) {
- wchar_t *w = fs_wide(p);
- int ok = CreateDirectoryW(w, NULL) || GetLastError() == ERROR_ALREADY_EXISTS;
- free(w);
- return ok;
+ int e = plat_fs_mkdir(p);
+ return !e || (e == PLAT_FS_EXISTS && plat_fs_kind(p) == PLAT_FS_DIR);
}
// p and every missing parent.
@@ -102,51 +122,27 @@ static void make_dirs(const char *p) {
make_dir(buf);
*q = c;
}
- if (!make_dir(buf)) rt_fatal("fs: cannot create directory %s (error %lu)", buf, GetLastError());
-}
-
-// Remove a directory tree; a reparse point is removed as a link, never followed. 0 also when nothing is there.
-static int remove_tree_w(const wchar_t *path) {
- DWORD a = GetFileAttributesW(path);
- if (a == INVALID_FILE_ATTRIBUTES)
- return GetLastError() == ERROR_FILE_NOT_FOUND || GetLastError() == ERROR_PATH_NOT_FOUND ? 0 : -1;
- if (!(a & FILE_ATTRIBUTE_DIRECTORY)) {
- if (a & FILE_ATTRIBUTE_READONLY) SetFileAttributesW(path, a & ~FILE_ATTRIBUTE_READONLY);
- return DeleteFileW(path) ? 0 : -1;
- }
- int bad = 0;
- if (!(a & FILE_ATTRIBUTE_REPARSE_POINT)) {
- size_t n = wcslen(path);
- wchar_t *pattern = malloc((n + 3) * sizeof *pattern);
- need(pattern != NULL, "path allocation");
- memcpy(pattern, path, n * sizeof *pattern);
- memcpy(pattern + n, L"\\*", 3 * sizeof *pattern);
- WIN32_FIND_DATAW fd;
- HANDLE f = FindFirstFileW(pattern, &fd);
- free(pattern);
- if (f != INVALID_HANDLE_VALUE) {
- do {
- if (!wcscmp(fd.cFileName, L".") || !wcscmp(fd.cFileName, L"..")) continue;
- size_t k = wcslen(fd.cFileName);
- wchar_t *child = malloc((n + k + 2) * sizeof *child);
- need(child != NULL, "path allocation");
- memcpy(child, path, n * sizeof *child);
- child[n] = L'\\';
- memcpy(child + n + 1, fd.cFileName, (k + 1) * sizeof *child);
- bad |= remove_tree_w(child);
- free(child);
- } while (FindNextFileW(f, &fd));
- FindClose(f);
- }
- }
- return bad | (RemoveDirectoryW(path) ? 0 : -1);
+ if (!make_dir(buf)) rt_fatal("fs: cannot create directory %s", buf);
}
+// Remove a directory tree; a link (reparse point, symbolic link) is removed as a link, never followed. 0 also when
+// nothing is there.
static int remove_tree(const char *path) {
- wchar_t *w = fs_wide(path);
- int r = remove_tree_w(w);
- free(w);
- return r;
+ int kind = plat_fs_kind(path);
+ if (kind == PLAT_FS_MISSING) return 0;
+ if (kind == PLAT_FS_FILE) return plat_fs_unlink(path) ? -1 : 0;
+ int bad = 0;
+ if (kind == PLAT_FS_DIR) {
+ Listing l;
+ list_dir(path, &l);
+ for (int i = 0; i < l.n; i++) {
+ char *child = join(path, l.e[i].name);
+ bad |= remove_tree(child);
+ free(child);
+ }
+ listing_free(l.e, l.n);
+ }
+ return bad | (plat_fs_rmdir(path) ? -1 : 0);
}
// ---- save transactions
@@ -161,7 +157,7 @@ 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. The game's Commit returns 0 once steps (1) to (4) are
+// Both renames are durable (plat_fs_rename). 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
@@ -174,9 +170,8 @@ static int remove_tree(const char *path) {
#define SAVE_BACKUPS 3
#define COPY_CHUNK (1u << 20)
-static char tx_root[1100], backup_root[1100];
-static wchar_t *w_save, *w_work, *w_new, *w_old, *w_backup;
-static SRWLOCK tx_lock = SRWLOCK_INIT; // taken before lock, never inside it
+static char tx_root[1100], backup_root[1100], new_root[1100], old_root[1100];
+static PlatMutex tx_lock = PLAT_MUTEX_INIT; // taken before lock, never inside it
static int save_mounts; // M_SAVE mounts: the working copy exists while this is above 0
// Probe hook: Commit stops at its tx_crash_at-th step (from 1) and leaves the disk as a process killed there would.
@@ -186,168 +181,133 @@ static int crashed(void) {
return tx_dead;
}
-static wchar_t *wjoin(const wchar_t *a, const wchar_t *b) {
- size_t n = wcslen(a), k = wcslen(b);
- wchar_t *w = malloc((n + k + 2) * sizeof *w);
- need(w != NULL, "path allocation");
- memcpy(w, a, n * sizeof *w);
- w[n] = L'\\';
- memcpy(w + n + 1, b, (k + 1) * sizeof *w);
- return w;
-}
-
-static int exists_w(const wchar_t *p) { return GetFileAttributesW(p) != INVALID_FILE_ATTRIBUTES; }
+static int exists(const char *p) { return plat_fs_kind(p) != PLAT_FS_MISSING; }
// Rename a directory, durably; retried for up to 2 s while another process (a virus scanner, the indexer) holds a
// file in it. 0 on success.
-static int move_dir_w(const wchar_t *from, const wchar_t *to) {
+static int move_dir(const char *from, const char *to) {
for (int i = 0; i < 40; i++) {
- if (MoveFileExW(from, to, MOVEFILE_WRITE_THROUGH)) return 0;
- DWORD e = GetLastError();
- if (e != ERROR_ACCESS_DENIED && e != ERROR_SHARING_VIOLATION) return -1;
- Sleep(50);
+ int e = plat_fs_rename(from, to, 1);
+ if (!e) return 0;
+ if (e != PLAT_FS_BUSY) return -1;
+ plat_sleep_ms(50);
}
return -1;
}
// Copy one file; the source may be open for writing (a save file the game has not closed). 0 on success.
-static int copy_file_w(const wchar_t *src, const wchar_t *dst, int durable, uint64_t *bytes) {
- HANDLE in = CreateFileW(src, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
- OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
- if (in == INVALID_HANDLE_VALUE) return -1;
- HANDLE out = CreateFileW(dst, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
- if (out == INVALID_HANDLE_VALUE) {
- CloseHandle(in);
+static int copy_file(const char *src, const char *dst, int durable, uint64_t *bytes) {
+ PlatFile *in = plat_fs_open(src, 0, NULL);
+ if (!in) return -1;
+ PlatFile *out = plat_fs_create(dst, NULL);
+ if (!out) {
+ plat_file_close(in);
return -1;
}
uint8_t *buf = malloc(COPY_CHUNK);
need(buf != NULL, "copy buffer");
int ok = 1;
- for (;;) {
- DWORD got = 0, put = 0;
- if (!ReadFile(in, buf, COPY_CHUNK, &got, NULL)) ok = 0;
+ for (uint64_t at = 0;;) {
+ int64_t got = plat_file_pread(in, buf, COPY_CHUNK, at);
+ if (got < 0) ok = 0;
if (!ok || !got) break;
- if (!WriteFile(out, buf, got, &put, NULL) || put != got) ok = 0;
+ if (plat_file_pwrite(out, buf, (uint32_t)got, at) != got) ok = 0;
if (!ok) break;
- *bytes += got;
+ at += (uint64_t)got;
+ *bytes += (uint64_t)got;
}
- if (ok && durable) ok = FlushFileBuffers(out);
+ if (ok && durable) ok = plat_file_fsync(out);
free(buf);
- CloseHandle(in);
- CloseHandle(out);
+ plat_file_close(in);
+ plat_file_close(out);
return ok ? 0 : -1;
}
// Whether files a and b hold the same bytes; 0 when either cannot be read.
-static int same_file_w(const wchar_t *a, const wchar_t *b) {
- HANDLE h[2];
- const wchar_t *p[2] = {a, b};
- for (int i = 0; i < 2; i++)
- h[i] = CreateFileW(p[i], GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
- OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
- LARGE_INTEGER na, nb;
- int same = h[0] != INVALID_HANDLE_VALUE && h[1] != INVALID_HANDLE_VALUE && GetFileSizeEx(h[0], &na) &&
- GetFileSizeEx(h[1], &nb) && na.QuadPart == nb.QuadPart;
+static int same_file(const char *a, const char *b) {
+ PlatFile *h[2] = {plat_fs_open(a, 0, NULL), plat_fs_open(b, 0, NULL)};
+ PlatFileId ia, ib;
+ int same = h[0] && h[1] && plat_file_id(h[0], &ia) && plat_file_id(h[1], &ib) && ia.size == ib.size;
uint8_t *buf = same ? malloc(2 * (size_t)COPY_CHUNK) : NULL;
if (same) need(buf != NULL, "compare buffer");
- while (same) {
- DWORD ga = 0, gb = 0;
- if (!ReadFile(h[0], buf, COPY_CHUNK, &ga, NULL) || !ReadFile(h[1], buf + COPY_CHUNK, COPY_CHUNK, &gb, NULL) ||
- ga != gb || memcmp(buf, buf + COPY_CHUNK, ga))
- same = 0;
- else if (!ga)
- break;
+ for (uint64_t at = 0; same;) {
+ int64_t ga = plat_file_pread(h[0], buf, COPY_CHUNK, at), gb = plat_file_pread(h[1], buf + COPY_CHUNK, COPY_CHUNK, at);
+ if (ga < 0 || ga != gb || memcmp(buf, buf + COPY_CHUNK, (size_t)ga)) same = 0;
+ else if (!ga) break;
+ at += (uint64_t)ga;
}
free(buf);
for (int i = 0; i < 2; i++)
- if (h[i] != INVALID_HANDLE_VALUE) CloseHandle(h[i]);
+ if (h[i]) plat_file_close(h[i]);
return same;
}
-// Copy the tree src to dst, which must not exist; reparse points are skipped. durable: every file is flushed, and
-// every directory as far as the file system allows (NTFS journals the entries either way). base (or NULL): the
-// committed save; a file whose bytes equal base's file at the same path becomes a hard link to it instead of a copy
-// (*nlinked), as committed files are never written in place (the game writes the working copy). A save commit then
-// writes the 3-4 files of the slot the game saved instead of all 82 (300-380 ms of flushes on the game thread).
-// 0 on success.
-static int copy_tree_w(const wchar_t *src, const wchar_t *dst, const wchar_t *base, int durable, int *nfiles,
- int *nlinked, uint64_t *bytes) {
- if (!CreateDirectoryW(dst, NULL)) return -1;
- wchar_t *pattern = wjoin(src, L"*");
- WIN32_FIND_DATAW fd;
- HANDLE f = FindFirstFileW(pattern, &fd);
- free(pattern);
- if (f == INVALID_HANDLE_VALUE) return -1;
+// Copy the tree src to dst, which must not exist; links are skipped. durable: every file is flushed, and every
+// directory as far as the file system allows. base (or NULL): the committed save; a file whose bytes equal base's file
+// at the same path becomes a hard link to it instead of a copy (*nlinked), as committed files are never written in
+// place (the game writes the working copy). A save commit then writes the 3-4 files of the slot the game saved instead
+// of all 82 (300-380 ms of flushes on the game thread). 0 on success.
+static int copy_tree(const char *src, const char *dst, const char *base, int durable, int *nfiles, int *nlinked,
+ uint64_t *bytes) {
+ if (plat_fs_mkdir(dst)) return -1;
+ Listing l;
+ if (list_dir(src, &l)) return -1;
int bad = 0;
- do {
- if (!wcscmp(fd.cFileName, L".") || !wcscmp(fd.cFileName, L"..") ||
- (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
- continue;
- wchar_t *s = wjoin(src, fd.cFileName), *d = wjoin(dst, fd.cFileName);
- wchar_t *b = base ? wjoin(base, fd.cFileName) : NULL;
- if (b && !exists_w(b)) free(b), b = NULL;
- if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) bad = copy_tree_w(s, d, b, durable, nfiles, nlinked, bytes);
+ for (int i = 0; i < l.n && !bad; i++) {
+ if (l.e[i].kind == PLAT_FS_LINK) continue;
+ char *s = join(src, l.e[i].name), *d = join(dst, l.e[i].name);
+ char *b = base ? join(base, l.e[i].name) : NULL;
+ if (b && !exists(b)) free(b), b = NULL;
+ if (l.e[i].kind == PLAT_FS_DIR) bad = copy_tree(s, d, b, durable, nfiles, nlinked, bytes);
else {
- if (b && same_file_w(s, b) && CreateHardLinkW(d, b, NULL)) bad = 0, ++*nlinked;
- else bad = copy_file_w(s, d, durable, bytes);
+ if (b && same_file(s, b) && !plat_fs_link(b, d)) bad = 0, ++*nlinked;
+ else bad = copy_file(s, d, durable, bytes);
*nfiles += !bad;
if (!bad && durable && crashed()) bad = -1;
}
free(s);
free(d);
free(b);
- } while (!bad && FindNextFileW(f, &fd));
- FindClose(f);
- if (!bad && durable) {
- HANDLE h = CreateFileW(dst, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
- OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
- if (h != INVALID_HANDLE_VALUE) {
- FlushFileBuffers(h);
- CloseHandle(h);
- }
}
+ listing_free(l.e, l.n);
+ if (!bad && durable) plat_fs_sync_dir(dst);
return bad;
}
-static int name_cmp(const void *a, const void *b) { return wcscmp(a, b); }
+static int name_cmp(const void *a, const void *b) { return strcmp(a, b); }
// Move old to save-backup/ and delete all backups but the newest SAVE_BACKUPS.
static void rotate_backup(void) {
- if (!exists_w(w_old)) return;
- SYSTEMTIME t;
- GetSystemTime(&t);
- wchar_t name[48], *dst = NULL;
- for (int k = 0; !dst || exists_w(dst); k++) {
+ if (!exists(old_root)) return;
+ uint64_t ns = plat_unix_time_ns();
+ struct tm t;
+ plat_gmtime((int64_t)(ns / 1000000000u), &t);
+ char name[48], *dst = NULL;
+ for (int k = 0; !dst || exists(dst); k++) {
free(dst);
- int n = swprintf(name, 48, L"%04u%02u%02u-%02u%02u%02u-%03u", (unsigned)t.wYear, (unsigned)t.wMonth,
- (unsigned)t.wDay, (unsigned)t.wHour, (unsigned)t.wMinute, (unsigned)t.wSecond,
- (unsigned)t.wMilliseconds);
- if (k) swprintf(name + n, 48 - (size_t)n, L"-%d", k);
- dst = wjoin(w_backup, name);
+ int n = snprintf(name, sizeof name, "%04d%02d%02d-%02d%02d%02d-%03u", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
+ t.tm_hour, t.tm_min, t.tm_sec, (unsigned)(ns / 1000000u % 1000u));
+ if (k) snprintf(name + n, sizeof name - (size_t)n, "-%d", k);
+ dst = join(backup_root, name);
}
- if (move_dir_w(w_old, dst)) {
- rt_log("fs: cannot move the replaced save to save-backup (error %lu); deleted", GetLastError());
- remove_tree_w(w_old);
+ if (move_dir(old_root, dst)) {
+ rt_log("fs: cannot move the replaced save to save-backup; deleted");
+ remove_tree(old_root);
}
free(dst);
- static wchar_t names[64][48];
+ static char names[64][48];
int n = 0;
- wchar_t *pattern = wjoin(w_backup, L"*");
- WIN32_FIND_DATAW fd;
- HANDLE f = FindFirstFileW(pattern, &fd);
- free(pattern);
- if (f != INVALID_HANDLE_VALUE) {
- do
- if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && fd.cFileName[0] >= L'0' &&
- fd.cFileName[0] <= L'9' && wcslen(fd.cFileName) < 48 && n < 64)
- wcscpy(names[n++], fd.cFileName);
- while (FindNextFileW(f, &fd));
- FindClose(f);
- }
+ Listing l;
+ list_dir(backup_root, &l);
+ for (int i = 0; i < l.n; i++)
+ if (l.e[i].kind == PLAT_FS_DIR && l.e[i].name[0] >= '0' && l.e[i].name[0] <= '9' && strlen(l.e[i].name) < 48 &&
+ n < 64)
+ strcpy(names[n++], l.e[i].name);
+ listing_free(l.e, l.n);
qsort(names, (size_t)n, sizeof *names, name_cmp);
for (int i = 0; i + SAVE_BACKUPS < n; i++) {
- wchar_t *p = wjoin(w_backup, names[i]);
- remove_tree_w(p);
+ char *p = join(backup_root, names[i]);
+ remove_tree(p);
free(p);
}
}
@@ -355,23 +315,21 @@ static void rotate_backup(void) {
// Startup: finish or roll back an interrupted Commit and drop uncommitted changes (the rule above).
static void save_recover(void) {
const char *did = "nothing to recover";
- int has_save = exists_w(w_save);
- if (!has_save && exists_w(w_new)) {
- if (move_dir_w(w_new, w_save)) rt_fatal("fs: cannot complete an interrupted save commit (error %lu)",
- GetLastError());
+ int has_save = exists(save_root);
+ if (!has_save && exists(new_root)) {
+ if (move_dir(new_root, save_root)) rt_fatal("fs: cannot complete an interrupted save commit");
did = "completed an interrupted commit";
- } else if (!has_save && exists_w(w_old)) {
- if (move_dir_w(w_old, w_save)) rt_fatal("fs: cannot roll back an interrupted save commit (error %lu)",
- GetLastError());
+ } else if (!has_save && exists(old_root)) {
+ if (move_dir(old_root, save_root)) rt_fatal("fs: cannot roll back an interrupted save commit");
did = "rolled back an interrupted commit";
- } else if (exists_w(w_new)) {
- if (remove_tree_w(w_new)) rt_fatal("fs: cannot delete an unfinished save commit (error %lu)", GetLastError());
+ } else if (exists(new_root)) {
+ if (remove_tree(new_root)) rt_fatal("fs: cannot delete an unfinished save commit");
did = "rolled back an unfinished commit";
- } else if (exists_w(w_old))
+ } else if (exists(old_root))
did = "moved the save an interrupted commit replaced to save-backup";
rotate_backup();
- if (exists_w(w_work)) {
- if (remove_tree_w(w_work)) rt_fatal("fs: cannot delete the old save working copy (error %lu)", GetLastError());
+ if (exists(work_root)) {
+ if (remove_tree(work_root)) rt_fatal("fs: cannot delete the old save working copy");
if (!strcmp(did, "nothing to recover"))
did = "deleted the last run's working copy (changes after its last Commit)";
}
@@ -382,15 +340,15 @@ static void save_recover(void) {
static void work_create(void) {
int n = 0, linked = 0;
uint64_t bytes = 0;
- if (remove_tree_w(w_work) || copy_tree_w(w_save, w_work, NULL, 0, &n, &linked, &bytes))
- rt_fatal("fs: cannot make the save working copy (error %lu)", GetLastError());
+ if (remove_tree(work_root) || copy_tree(save_root, work_root, NULL, 0, &n, &linked, &bytes))
+ rt_fatal("fs: cannot make the save working copy");
rt_log("host save working copy: %d files, %.1f MiB", n, bytes / 1048576.0);
}
// The last save unmount: changes no Commit took are dropped.
static void work_discard(void) {
- if (InterlockedExchange(&save_dirty, 0)) rt_log("fs: save unmounted without Commit; changes discarded");
- if (remove_tree_w(w_work)) rt_log("fs: save working copy not fully deleted (a file still open?)");
+ if (plat_xchg32(&save_dirty, 0)) rt_log("fs: save unmounted without Commit; changes discarded");
+ if (remove_tree(work_root)) rt_log("fs: save working copy not fully deleted (a file still open?)");
}
// Commit of the save mount, tx_lock held. Steps (1) to (4) above; every failure before (3) leaves the committed save.
@@ -399,91 +357,86 @@ static uint32_t save_commit_locked(void) {
int nfiles = 0, linked = 0;
tx_step = tx_dead = 0;
rotate_backup(); // an old left by a failed (4)
- const wchar_t *base = exists_w(w_save) ? w_save : NULL;
- if (remove_tree_w(w_new) || copy_tree_w(w_work, w_new, base, 1, &nfiles, &linked, &bytes)) {
+ const char *base = exists(save_root) ? save_root : NULL;
+ if (remove_tree(new_root) || copy_tree(work_root, new_root, base, 1, &nfiles, &linked, &bytes)) {
if (tx_dead) return FS_R_INVALID_ARGUMENT;
- rt_log("fs: save commit: copying the working copy failed (error %lu); committed save unchanged",
- GetLastError());
- remove_tree_w(w_new);
+ rt_log("fs: save commit: copying the working copy failed; committed save unchanged");
+ remove_tree(new_root);
return FS_R_INVALID_ARGUMENT;
}
if (crashed()) return FS_R_INVALID_ARGUMENT;
- if (exists_w(w_save) && move_dir_w(w_save, w_old)) {
- rt_log("fs: save commit: cannot move the committed save (error %lu); committed save unchanged",
- GetLastError());
- remove_tree_w(w_new);
+ if (exists(save_root) && move_dir(save_root, old_root)) {
+ rt_log("fs: save commit: cannot move the committed save; committed save unchanged");
+ remove_tree(new_root);
return FS_R_INVALID_ARGUMENT;
}
if (crashed()) return FS_R_INVALID_ARGUMENT;
- if (move_dir_w(w_new, w_save)) {
- DWORD e = GetLastError();
- if (move_dir_w(w_old, w_save))
- rt_fatal("fs: save commit: cannot swap in the new save (error %lu); the next start completes it", e);
- rt_log("fs: save commit: cannot swap in the new save (error %lu); committed save unchanged", e);
- remove_tree_w(w_new);
+ if (move_dir(new_root, save_root)) {
+ if (move_dir(old_root, save_root))
+ rt_fatal("fs: save commit: cannot swap in the new save; the next start completes it");
+ rt_log("fs: save commit: cannot swap in the new save; committed save unchanged");
+ remove_tree(new_root);
return FS_R_INVALID_ARGUMENT;
}
if (crashed()) return FS_R_INVALID_ARGUMENT;
rotate_backup();
- InterlockedExchange(&save_dirty, 0);
+ plat_xchg32(&save_dirty, 0);
rt_log("fs commit save: %d files, %d changed (%.1f MiB written), %d unchanged linked, in %.0f ms", nfiles,
nfiles - linked, bytes / 1048576.0, linked, (plat_now_ns() - t0) / 1e6);
return 0;
}
// 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 PlatMutex pend_lock = PLAT_MUTEX_INIT;
+static PlatCond pend_cv = PLAT_COND_INIT;
+static volatile int32_t 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);
+ if (!plat_cas32(&commit_pending, 0, 0)) return;
+ plat_mutex_lock(&pend_lock);
+ while (commit_pending) plat_cond_wait(&pend_cv, &pend_lock);
+ plat_mutex_unlock(&pend_lock);
}
void fs_save_flush(void) { save_wait_commit(); }
static uint32_t commit_run(void) {
- AcquireSRWLockExclusive(&tx_lock);
+ plat_mutex_lock(&tx_lock);
uint32_t r = save_commit_locked();
- InterlockedDecrement(&save_writers);
- ReleaseSRWLockExclusive(&tx_lock);
- AcquireSRWLockExclusive(&pend_lock);
+ plat_dec32(&save_writers);
+ plat_mutex_unlock(&tx_lock);
+ plat_mutex_lock(&pend_lock);
commit_pending = 0;
- ReleaseSRWLockExclusive(&pend_lock);
- WakeAllConditionVariable(&pend_cv);
+ plat_mutex_unlock(&pend_lock);
+ plat_cond_broadcast(&pend_cv);
return r;
}
-static DWORD WINAPI commit_main(void *arg) {
+static void commit_main(void *arg) {
(void)arg;
commit_run();
- return 0;
+ return;
}
static uint32_t save_commit(void) {
save_wait_commit(); // the previous Commit
static int quiet;
- if (!InterlockedCompareExchange(&save_dirty, 0, 0)) {
+ if (!plat_cas32(&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);
+ plat_inc32(&save_writers); // fs_save_busy until the thread is done
+ plat_xchg32(&commit_pending, 1);
+ if (plat_thread_spawn(commit_main, NULL, "save commit", 0)) {
return 0;
}
- rt_log("fs: cannot start the save commit thread (error %lu); committing on the game thread", GetLastError());
+ rt_log("fs: cannot start the save commit thread; committing on the game thread");
return commit_run();
}
// ---- storage roots
-static BOOL CALLBACK storage_init(PINIT_ONCE once, PVOID param, PVOID *ctx) {
- (void)once, (void)param, (void)ctx;
+static void storage_init(void *param) {
+ (void)param;
if (!*profile)
need(snprintf(profile, sizeof profile, "%s", rt_config()->profile) < (int)sizeof profile, "profile path");
uint64_t uid[2];
@@ -499,13 +452,8 @@ static BOOL CALLBACK storage_init(PINIT_ONCE once, PVOID param, PVOID *ctx) {
snprintf(backup_root, sizeof backup_root, "%s/save-backup", profile);
make_dirs(tx_root);
make_dirs(backup_root);
- w_save = fs_wide(save_root);
- w_work = fs_wide(work_root);
- snprintf(p, sizeof p, "%s/new", tx_root);
- w_new = fs_wide(p);
- snprintf(p, sizeof p, "%s/old", tx_root);
- w_old = fs_wide(p);
- w_backup = fs_wide(backup_root);
+ snprintf(new_root, sizeof new_root, "%s/new", tx_root);
+ snprintf(old_root, sizeof old_root, "%s/old", tx_root);
save_recover(); // before save_root is created: an absent save_root means a commit to complete
make_dirs(save_root);
snprintf(p, sizeof p, "%s/temp", profile);
@@ -513,18 +461,18 @@ static BOOL CALLBACK storage_init(PINIT_ONCE once, PVOID param, PVOID *ctx) {
make_dirs(p);
snprintf(p, sizeof p, "%s/cache", profile);
make_dirs(p);
- InterlockedExchange(&storage_ready, 1);
+ plat_xchg32(&storage_ready, 1);
rt_log("host save storage: %s", save_root);
- return TRUE;
+ return;
}
static void ready(void) {
- static INIT_ONCE once = INIT_ONCE_STATIC_INIT;
- InitOnceExecuteOnce(&once, storage_init, NULL, NULL);
+ static PlatOnce once = PLAT_ONCE_INIT;
+ plat_once(&once, storage_init, NULL);
}
void fs_host_set_profile(const char *dir) {
- if (InterlockedCompareExchange(&storage_ready, 0, 0)) rt_fatal("fs_host_set_profile after the first storage use");
+ if (plat_cas32(&storage_ready, 0, 0)) rt_fatal("fs_host_set_profile after the first storage use");
need(dir && snprintf(profile, sizeof profile, "%s", dir) < (int)sizeof profile, "profile path");
}
@@ -534,11 +482,11 @@ static uint32_t mount_add(const char *name, int kind, const char *root) {
char as_path[40];
snprintf(as_path, sizeof as_path, "%s:", name);
if (romfs_owns(as_path)) return FS_R_ALREADY_EXISTS; // the RomFS mount's name
- AcquireSRWLockExclusive(&lock);
+ plat_mutex_lock(&lock);
int slot = -1;
for (int i = 0; i < NMOUNTS; i++) {
if (mounts[i].kind && !strcmp(mounts[i].name, name)) {
- ReleaseSRWLockExclusive(&lock);
+ plat_mutex_unlock(&lock);
return FS_R_ALREADY_EXISTS;
}
if (!mounts[i].kind && slot < 0) slot = i;
@@ -547,12 +495,12 @@ static uint32_t mount_add(const char *name, int kind, const char *root) {
mounts[slot].kind = kind;
strcpy(mounts[slot].name, name);
snprintf(mounts[slot].root, sizeof mounts[slot].root, "%s", root);
- ReleaseSRWLockExclusive(&lock);
+ plat_mutex_unlock(&lock);
rt_log("fs mount %s: -> %s", name, root);
return 0;
}
-// A path component Windows stores under exactly this name: not empty, ".", "..", no reserved character, no trailing
+// A path component every host stores under exactly this name: not empty, ".", "..", no reserved character, no trailing
// dot or space (Windows strips those, which would alias two names).
static int component_ok(const char *c, size_t n) {
if (!n || (n == 1 && c[0] == '.') || (n == 2 && c[0] == '.' && c[1] == '.')) return 0;
@@ -570,13 +518,13 @@ static uint32_t host_path_kind(const char *path, char *out, size_t cap, int *kin
size_t n = (size_t)(colon - path);
char root[1024];
int k = M_FREE;
- AcquireSRWLockShared(&lock);
+ plat_mutex_lock_shared(&lock);
for (int i = 0; i < NMOUNTS; i++)
if (mounts[i].kind && strlen(mounts[i].name) == n && !strncmp(mounts[i].name, path, n)) {
strcpy(root, mounts[i].root);
k = mounts[i].kind;
}
- ReleaseSRWLockShared(&lock);
+ plat_mutex_unlock_shared(&lock);
if (k == M_FREE) return FS_R_PATH_NOT_FOUND;
if (kind) *kind = k;
const char *rest = colon + 1;
@@ -604,11 +552,12 @@ static uint32_t save_change_path(const char *path, char *out, size_t cap) {
return r;
}
-static uint32_t win_result(DWORD e) {
+static uint32_t fs_result(int e) {
switch (e) {
- case ERROR_FILE_NOT_FOUND: case ERROR_PATH_NOT_FOUND: case ERROR_DIRECTORY: return FS_R_PATH_NOT_FOUND;
- case ERROR_FILE_EXISTS: case ERROR_ALREADY_EXISTS: return FS_R_ALREADY_EXISTS;
- case ERROR_DIR_NOT_EMPTY: return FS_R_NOT_EMPTY;
+ case PLAT_FS_OK: return 0;
+ case PLAT_FS_NOT_FOUND: return FS_R_PATH_NOT_FOUND;
+ case PLAT_FS_EXISTS: return FS_R_ALREADY_EXISTS;
+ case PLAT_FS_NOT_EMPTY: return FS_R_NOT_EMPTY;
default: return FS_R_INVALID_ARGUMENT;
}
}
@@ -623,12 +572,12 @@ static int my_uid(const uint64_t *uid) {
static uint32_t mount_save(const char *name, const uint64_t *uid) {
ready();
if (!my_uid(uid)) return FS_R_TARGET_NOT_FOUND;
- AcquireSRWLockExclusive(&tx_lock);
+ plat_mutex_lock(&tx_lock);
if (!save_mounts) work_create();
uint32_t r = mount_add(name, M_SAVE, work_root);
if (!r) save_mounts++;
- else if (!save_mounts) remove_tree_w(w_work);
- ReleaseSRWLockExclusive(&tx_lock);
+ else if (!save_mounts) remove_tree(work_root);
+ plat_mutex_unlock(&tx_lock);
return r;
}
@@ -657,10 +606,7 @@ static uint32_t create_cache(int index, int64_t size, int64_t journal) {
if (index < 0 || size < 0 || journal < 0) return FS_R_INVALID_ARGUMENT;
char p[PATH_CAP];
cache_dir(index, p, sizeof p);
- wchar_t *w = fs_wide(p);
- uint32_t r = CreateDirectoryW(w, NULL) ? 0 : win_result(GetLastError());
- free(w);
- return r;
+ return fs_result(plat_fs_mkdir(p));
}
static uint32_t delete_cache(int index) {
@@ -674,8 +620,7 @@ static uint32_t mount_cache(const char *name, int index) {
ready();
char p[PATH_CAP];
cache_dir(index, p, sizeof p);
- DWORD a = attributes(p);
- if (a == INVALID_FILE_ATTRIBUTES || !(a & FILE_ATTRIBUTE_DIRECTORY)) return FS_R_TARGET_NOT_FOUND;
+ if (plat_fs_kind(p) != PLAT_FS_DIR) return FS_R_TARGET_NOT_FOUND;
return mount_add(name, M_CACHE, p);
}
@@ -689,20 +634,15 @@ static uint32_t open_cache_list(uint64_t *out) {
CacheList *l = calloc(1, sizeof *l);
need(l != NULL, "cache list allocation");
char p[PATH_CAP];
- snprintf(p, sizeof p, "%s/cache/*", profile);
- wchar_t *w = fs_wide(p);
- WIN32_FIND_DATAW fd;
- HANDLE f = FindFirstFileW(w, &fd);
- free(w);
- if (f != INVALID_HANDLE_VALUE) {
- do {
- wchar_t *end;
- long v = wcstol(fd.cFileName, &end, 10);
- if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && *fd.cFileName && !*end && v >= 0 && l->count < 64)
- l->list[l->count++] = (int)v;
- } while (FindNextFileW(f, &fd));
- FindClose(f);
+ snprintf(p, sizeof p, "%s/cache", profile);
+ Listing ls;
+ list_dir(p, &ls);
+ for (int i = 0; i < ls.n; i++) {
+ char *end;
+ long v = strtol(ls.e[i].name, &end, 10);
+ if (ls.e[i].kind == PLAT_FS_DIR && *ls.e[i].name && !*end && v >= 0 && l->count < 64) l->list[l->count++] = (int)v;
}
+ listing_free(ls.e, ls.n);
*out = (uint64_t)(uintptr_t)l;
return 0;
}
@@ -723,20 +663,20 @@ 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);
+ plat_mutex_lock(&tx_lock);
+ plat_mutex_lock(&lock);
for (int i = 0; i < NMOUNTS; i++)
if (name && mounts[i].kind && !strcmp(mounts[i].name, name)) {
int save = mounts[i].kind == M_SAVE;
mounts[i].kind = M_FREE;
- ReleaseSRWLockExclusive(&lock);
+ plat_mutex_unlock(&lock);
if (save && !--save_mounts) work_discard();
- ReleaseSRWLockExclusive(&tx_lock);
+ plat_mutex_unlock(&tx_lock);
rt_log("fs unmount %s", name);
return;
}
- ReleaseSRWLockExclusive(&lock);
- ReleaseSRWLockExclusive(&tx_lock);
+ plat_mutex_unlock(&lock);
+ plat_mutex_unlock(&tx_lock);
size_t n = name ? strlen(name) : 0;
char probe[40];
if (!n || n >= 32 || (snprintf(probe, sizeof probe, "%s:", name), !romfs_owns(probe)))
@@ -745,43 +685,38 @@ static void unmount(const char *name) {
static uint32_t commit(const char *name) {
int found = 0, save = 0;
- AcquireSRWLockShared(&lock);
+ plat_mutex_lock_shared(&lock);
for (int i = 0; i < NMOUNTS; i++)
if (name && mounts[i].kind && !strcmp(mounts[i].name, name)) found = 1, save = mounts[i].kind == M_SAVE;
- ReleaseSRWLockShared(&lock);
+ plat_mutex_unlock_shared(&lock);
if (!found) return FS_R_PATH_NOT_FOUND;
if (save) return save_commit(); // durability comes from the staging copy, not the working copy's handles
- AcquireSRWLockShared(&lock);
+ plat_mutex_lock_shared(&lock);
for (int i = 0; i < NFILES; i++)
- if (files[i].h && (files[i].mode & FS_OPEN_WRITE)) FlushFileBuffers(files[i].h);
- ReleaseSRWLockShared(&lock);
+ if (files[i].h && (files[i].mode & FS_OPEN_WRITE)) plat_file_fsync(files[i].h);
+ plat_mutex_unlock_shared(&lock);
return 0;
}
// ---- files
-static File *file_of(uint64_t h, HANDLE *fh, int *mode) {
+static File *file_of(uint64_t h, PlatFile **fh, int *mode) {
if (h < FILE_BASE || h >= FILE_BASE + NFILES) return NULL;
- AcquireSRWLockShared(&lock);
+ plat_mutex_lock_shared(&lock);
File *f = &files[h - FILE_BASE];
- HANDLE v = f->h;
+ PlatFile *v = f->h;
if (fh) *fh = f->h;
if (mode) *mode = f->mode;
- ReleaseSRWLockShared(&lock);
+ plat_mutex_unlock_shared(&lock);
return v ? f : NULL;
}
-// Positional I/O on a synchronous handle: ReadFile/WriteFile with an OVERLAPPED offset.
-static int64_t host_read_at(HANDLE f, void *buf, uint32_t n, uint64_t off) {
- OVERLAPPED ov = {.Offset = (DWORD)off, .OffsetHigh = (DWORD)(off >> 32)};
- DWORD got = 0;
+// Positional reads: bytes read at off, 0 at the end, -1 on an error.
+static int64_t host_read_at(PlatFile *f, void *buf, uint32_t n, uint64_t off) {
mem_thaw((uintptr_t)buf, n); // the kernel's write would fail on a frozen page instead of faulting
- if (!ReadFile(f, buf, n, &got, &ov)) {
- DWORD e = GetLastError();
- if (e == ERROR_NOACCESS) // a read-only page in the buffer: a frozen region missed its thaw
- rt_log("fs: read of %u bytes into %p refused: the buffer is not writable", n, buf);
- return e == ERROR_HANDLE_EOF ? 0 : -1;
- }
- return got;
+ int64_t got = plat_file_pread(f, buf, n, off);
+ if (got == -2) // a read-only page in the buffer: a frozen region missed its thaw
+ rt_log("fs: read of %u bytes into %p refused: the buffer is not writable", n, buf);
+ return got < 0 ? -1 : got;
}
static uint32_t open_file(uint64_t *out, const char *path, int mode) {
@@ -792,36 +727,31 @@ static uint32_t open_file(uint64_t *out, const char *path, int mode) {
int kind = M_FREE;
uint32_t r = host_path_kind(path, p, sizeof p, &kind);
if (r) return r;
- DWORD a = attributes(p);
- if (a == INVALID_FILE_ATTRIBUTES || (a & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)))
- return FS_R_PATH_NOT_FOUND;
+ if (plat_fs_kind(p) != PLAT_FS_FILE) return FS_R_PATH_NOT_FOUND;
if (kind == M_SAVE_RO && (mode & FS_OPEN_WRITE)) return FS_R_UNSUPPORTED_OPEN;
int save_writer = kind == M_SAVE && (mode & FS_OPEN_WRITE);
- DWORD access = (mode & FS_OPEN_READ ? GENERIC_READ : 0) | (mode & FS_OPEN_WRITE ? GENERIC_WRITE : 0);
- wchar_t *w = fs_wide(p);
- HANDLE f = CreateFileW(w, access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL, NULL);
- free(w);
- if (f == INVALID_HANDLE_VALUE) return win_result(GetLastError());
- AcquireSRWLockExclusive(&lock);
+ int err = 0;
+ PlatFile *f = plat_fs_open(p, (mode & FS_OPEN_WRITE) != 0, &err);
+ if (!f) return fs_result(err);
+ plat_mutex_lock(&lock);
for (int i = 0; i < NFILES; i++)
if (!files[i].h) {
files[i] = (File){f, mode, save_writer};
*out = FILE_BASE + (uint64_t)i;
- ReleaseSRWLockExclusive(&lock);
+ plat_mutex_unlock(&lock);
if (save_writer) {
- InterlockedIncrement(&save_writers);
+ plat_inc32(&save_writers);
save_touched();
}
return 0;
}
- ReleaseSRWLockExclusive(&lock);
+ plat_mutex_unlock(&lock);
rt_fatal("fs: host file handle table full (%d)", NFILES);
}
static uint32_t read_file(uint64_t *out, uint64_t h, int64_t offset, void *buffer, uint64_t size) {
if (romfs_handle(h)) return romfs_read(out, h, offset, buffer, size);
- HANDLE f;
+ PlatFile *f;
int mode;
if (!out || offset < 0 || (!buffer && size) || !file_of(h, &f, &mode)) return FS_R_INVALID_ARGUMENT;
if (!(mode & FS_OPEN_READ)) return FS_R_OPEN_MODE;
@@ -829,7 +759,7 @@ static uint32_t read_file(uint64_t *out, uint64_t h, int64_t offset, void *buffe
while (done < size) {
uint32_t chunk = size - done > 0x40000000 ? 0x40000000 : (uint32_t)(size - done);
int64_t n = host_read_at(f, (uint8_t *)buffer + done, chunk, (uint64_t)offset + done);
- if (n < 0) rt_fatal("fs: host save read failure (error %lu)", GetLastError());
+ if (n < 0) rt_fatal("fs: host save read failure");
if (!n) break;
done += (uint64_t)n;
}
@@ -844,53 +774,49 @@ static uint32_t read_exact(uint64_t h, int64_t off, void *buf, uint64_t size) {
}
static uint32_t write_file(uint64_t h, int64_t offset, const void *buffer, uint64_t size, const int32_t *option) {
- HANDLE f;
+ PlatFile *f;
int mode;
File *file = offset < 0 || (!buffer && size) ? NULL : file_of(h, &f, &mode);
if (!file) return FS_R_INVALID_ARGUMENT;
if (!(mode & FS_OPEN_WRITE)) return FS_R_OPEN_MODE;
- LARGE_INTEGER sz;
- if (!GetFileSizeEx(f, &sz)) rt_fatal("fs: host save size (error %lu)", GetLastError());
- if ((uint64_t)offset + size > (uint64_t)sz.QuadPart && !(mode & FS_OPEN_APPEND)) return FS_R_APPEND_REQUIRED;
+ PlatFileId id;
+ if (!plat_file_id(f, &id)) rt_fatal("fs: host save size");
+ if ((uint64_t)offset + size > id.size && !(mode & FS_OPEN_APPEND)) return FS_R_APPEND_REQUIRED;
if (file->save) save_touched();
for (uint64_t done = 0; done < size;) {
uint32_t chunk = size - done > 0x40000000 ? 0x40000000 : (uint32_t)(size - done);
- uint64_t at = (uint64_t)offset + done;
- OVERLAPPED ov = {.Offset = (DWORD)at, .OffsetHigh = (DWORD)(at >> 32)};
- DWORD put = 0;
- if (!WriteFile(f, (const uint8_t *)buffer + done, chunk, &put, &ov) || !put)
- rt_fatal("fs: host save write failure (error %lu)", GetLastError());
- done += put;
+ int64_t put = plat_file_pwrite(f, (const uint8_t *)buffer + done, chunk, (uint64_t)offset + done);
+ if (put <= 0) rt_fatal("fs: host save write failure");
+ done += (uint64_t)put;
}
- if (option && (*option & 1)) FlushFileBuffers(f); // WriteOptionFlag_Flush
+ if (option && (*option & 1)) plat_file_fsync(f); // WriteOptionFlag_Flush
return 0;
}
static uint32_t file_size(int64_t *out, uint64_t h) {
if (romfs_handle(h)) return romfs_size(out, h);
- HANDLE f;
- LARGE_INTEGER sz;
- if (!out || !file_of(h, &f, NULL) || !GetFileSizeEx(f, &sz)) return FS_R_INVALID_ARGUMENT;
- *out = sz.QuadPart;
+ PlatFile *f;
+ PlatFileId id;
+ if (!out || !file_of(h, &f, NULL) || !plat_file_id(f, &id)) return FS_R_INVALID_ARGUMENT;
+ *out = (int64_t)id.size;
return 0;
}
static uint32_t set_file_size(uint64_t h, int64_t size) {
- HANDLE f;
+ PlatFile *f;
int mode;
File *file = size < 0 ? NULL : file_of(h, &f, &mode);
if (!file) return FS_R_INVALID_ARGUMENT;
if (!(mode & FS_OPEN_WRITE)) return FS_R_OPEN_MODE;
if (file->save) save_touched();
- FILE_END_OF_FILE_INFO eof = {.EndOfFile.QuadPart = size};
- return SetFileInformationByHandle(f, FileEndOfFileInfo, &eof, sizeof eof) ? 0 : win_result(GetLastError());
+ return fs_result(plat_file_set_size(f, (uint64_t)size));
}
static uint32_t flush_file(uint64_t h) {
- HANDLE f;
+ PlatFile *f;
int mode;
if (!file_of(h, &f, &mode)) return FS_R_INVALID_ARGUMENT;
- if (mode & FS_OPEN_WRITE) FlushFileBuffers(f);
+ if (mode & FS_OPEN_WRITE) plat_file_fsync(f);
return 0;
}
@@ -900,15 +826,15 @@ static void close_file(uint64_t h) {
return;
}
if (!file_of(h, NULL, NULL)) rt_fatal("fs: close of invalid file handle %#llx", (unsigned long long)h);
- AcquireSRWLockExclusive(&lock);
- HANDLE f = files[h - FILE_BASE].h;
+ plat_mutex_lock(&lock);
+ PlatFile *f = files[h - FILE_BASE].h;
int save = files[h - FILE_BASE].save;
files[h - FILE_BASE] = (File){0};
- ReleaseSRWLockExclusive(&lock);
- CloseHandle(f);
+ plat_mutex_unlock(&lock);
+ plat_file_close(f);
if (save) {
save_touched();
- InterlockedDecrement(&save_writers);
+ plat_dec32(&save_writers);
}
}
@@ -917,25 +843,20 @@ static uint32_t create_file(const char *path, int64_t size) {
uint32_t r = save_change_path(path, p, sizeof p);
if (r) return r;
if (size < 0) return FS_R_INVALID_ARGUMENT;
- wchar_t *w = fs_wide(p);
- HANDLE f = CreateFileW(w, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
- free(w);
- if (f == INVALID_HANDLE_VALUE) return win_result(GetLastError());
- FILE_END_OF_FILE_INFO eof = {.EndOfFile.QuadPart = size};
- int ok = !size || SetFileInformationByHandle(f, FileEndOfFileInfo, &eof, sizeof eof);
- DWORD e = GetLastError();
- CloseHandle(f);
- return ok ? 0 : win_result(e);
+ int err = 0;
+ PlatFile *f = plat_fs_create(p, &err);
+ if (!f) return fs_result(err);
+ err = size ? plat_file_set_size(f, (uint64_t)size) : 0;
+ plat_file_close(f);
+ return fs_result(err);
}
static uint32_t delete_file(const char *path) {
char p[PATH_CAP];
uint32_t r = save_change_path(path, p, sizeof p);
if (r) return r;
- wchar_t *w = fs_wide(p);
- r = DeleteFileW(w) ? 0 : win_result(GetLastError());
- free(w);
- return r;
+ if (plat_fs_kind(p) == PLAT_FS_DIR) return FS_R_INVALID_ARGUMENT; // DeleteFile on a directory
+ return fs_result(plat_fs_unlink(p));
}
static uint32_t rename_file(const char *from, const char *to) {
@@ -945,12 +866,8 @@ static uint32_t rename_file(const char *from, const char *to) {
if (r) return r;
size_t n = strcspn(from, ":");
if (strcspn(to, ":") != n || strncmp(from, to, n)) return FS_R_INVALID_PATH; // across mounts
- if (attributes(b) != INVALID_FILE_ATTRIBUTES) return FS_R_ALREADY_EXISTS;
- wchar_t *wa = fs_wide(a), *wb = fs_wide(b);
- r = MoveFileExW(wa, wb, 0) ? 0 : win_result(GetLastError());
- free(wa);
- free(wb);
- return r;
+ if (exists(b)) return FS_R_ALREADY_EXISTS;
+ return fs_result(plat_fs_rename(a, b, 0));
}
static uint32_t entry_type(int *out, const char *path) {
@@ -959,9 +876,9 @@ static uint32_t entry_type(int *out, const char *path) {
uint32_t r = host_path(path, p, sizeof p);
if (r) return r;
if (!out) return FS_R_INVALID_ARGUMENT;
- DWORD a = attributes(p);
- if (a == INVALID_FILE_ATTRIBUTES) return FS_R_PATH_NOT_FOUND;
- *out = a & FILE_ATTRIBUTE_DIRECTORY ? FS_ENTRY_DIRECTORY : FS_ENTRY_FILE;
+ int kind = plat_fs_kind(p);
+ if (kind == PLAT_FS_MISSING) return FS_R_PATH_NOT_FOUND;
+ *out = kind == PLAT_FS_DIR ? FS_ENTRY_DIRECTORY : FS_ENTRY_FILE;
return 0;
}
@@ -973,19 +890,14 @@ static uint32_t create_directory(const char *path) {
char p[PATH_CAP];
uint32_t r = save_change_path(path, p, sizeof p);
if (r) return r;
- wchar_t *w = fs_wide(p);
- r = CreateDirectoryW(w, NULL) ? 0 : win_result(GetLastError());
- free(w);
- return r;
+ return fs_result(plat_fs_mkdir(p));
}
static uint32_t delete_recursive(const char *path) {
char p[PATH_CAP];
uint32_t r = save_change_path(path, p, sizeof p);
if (r) return r;
- DWORD a = attributes(p);
- if (a == INVALID_FILE_ATTRIBUTES) return FS_R_PATH_NOT_FOUND;
- if (!(a & FILE_ATTRIBUTE_DIRECTORY)) return FS_R_PATH_NOT_FOUND;
+ if (plat_fs_kind(p) != PLAT_FS_DIR) return FS_R_PATH_NOT_FOUND;
return remove_tree(p) ? FS_R_INVALID_ARGUMENT : 0;
}
@@ -995,46 +907,37 @@ static uint32_t open_directory(uint64_t *out, const char *path, int mode) {
uint32_t r = host_path(path, p, sizeof p);
if (r) return r;
if (!out || mode < 1 || mode > 3) return FS_R_INVALID_ARGUMENT;
- DWORD a = attributes(p);
- if (a == INVALID_FILE_ATTRIBUTES || !(a & FILE_ATTRIBUTE_DIRECTORY)) return FS_R_PATH_NOT_FOUND;
- char pattern[PATH_CAP + 4];
- snprintf(pattern, sizeof pattern, "%s/*", p);
- wchar_t *w = fs_wide(pattern);
- WIN32_FIND_DATAW fd;
- HANDLE f = FindFirstFileW(w, &fd);
- free(w);
- if (f == INVALID_HANDLE_VALUE) return win_result(GetLastError());
- AcquireSRWLockExclusive(&lock);
+ if (plat_fs_kind(p) != PLAT_FS_DIR) return FS_R_PATH_NOT_FOUND;
+ Listing l;
+ int e = list_dir(p, &l);
+ if (e) return fs_result(e);
+ plat_mutex_lock(&lock);
for (int i = 0; i < NDIRS; i++)
- if (!dirs[i].find) {
- dirs[i] = (Dir){f, fd, 1, mode};
+ if (!dirs[i].used) {
+ dirs[i] = (Dir){l.e, l.n, 0, mode, 1};
*out = DIR_BASE + (uint64_t)i;
- ReleaseSRWLockExclusive(&lock);
+ plat_mutex_unlock(&lock);
return 0;
}
- ReleaseSRWLockExclusive(&lock);
+ plat_mutex_unlock(&lock);
rt_fatal("fs: host directory handle table full (%d)", NDIRS);
}
static uint32_t read_directory(int64_t *count, DirectoryEntry *out, uint64_t h, int64_t max) {
if (h < DIR_BASE || h >= DIR_BASE + NDIRS || !count || max < 0 || (!out && max)) return FS_R_INVALID_ARGUMENT;
Dir *d = &dirs[h - DIR_BASE];
- if (!d->find) return FS_R_INVALID_ARGUMENT;
+ if (!d->used) return FS_R_INVALID_ARGUMENT;
int64_t n = 0;
- while (n < max) {
- if (!d->has_next && !FindNextFileW(d->find, &d->next)) break;
- d->has_next = 0;
- const WIN32_FIND_DATAW *e = &d->next;
- if (!wcscmp(e->cFileName, L".") || !wcscmp(e->cFileName, L"..")) continue;
- char name[800];
- int k = WideCharToMultiByte(CP_UTF8, 0, e->cFileName, -1, name, sizeof name, NULL, NULL);
- if (k <= 0 || k > 769) continue; // over 768 bytes: nn::fs cannot name it
- int is_dir = !!(e->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
+ while (n < max && d->next < d->n) {
+ const Listed *e = &d->e[d->next++];
+ size_t k = strlen(e->name) + 1;
+ if (k > 769) continue; // over 768 bytes: nn::fs cannot name it
+ int is_dir = e->kind == PLAT_FS_DIR;
if (!(d->mode & (is_dir ? 1 : 2))) continue;
memset(&out[n], 0, sizeof *out);
- memcpy(out[n].name, name, (size_t)k);
+ memcpy(out[n].name, e->name, k);
out[n].type = is_dir ? FS_ENTRY_DIRECTORY : FS_ENTRY_FILE;
- out[n].size = is_dir ? 0 : (int64_t)((uint64_t)e->nFileSizeHigh << 32 | e->nFileSizeLow);
+ out[n].size = is_dir ? 0 : (int64_t)e->size;
n++;
}
*count = n;
@@ -1042,13 +945,13 @@ static uint32_t read_directory(int64_t *count, DirectoryEntry *out, uint64_t h,
}
static void close_directory(uint64_t h) {
- if (h < DIR_BASE || h >= DIR_BASE + NDIRS || !dirs[h - DIR_BASE].find)
+ if (h < DIR_BASE || h >= DIR_BASE + NDIRS || !dirs[h - DIR_BASE].used)
rt_fatal("fs: close of invalid directory handle %#llx", (unsigned long long)h);
- AcquireSRWLockExclusive(&lock);
- HANDLE f = dirs[h - DIR_BASE].find;
+ plat_mutex_lock(&lock);
+ Dir d = dirs[h - DIR_BASE];
dirs[h - DIR_BASE] = (Dir){0};
- ReleaseSRWLockExclusive(&lock);
- FindClose(f);
+ plat_mutex_unlock(&lock);
+ listing_free(d.e, d.n);
}
// ---- overrides: AAPCS64 thunks. FileHandle, DirectoryHandle, CacheStorageListHandle
@@ -1192,7 +1095,7 @@ static uint64_t fs_call(const char *sym, const uint64_t a[6]) {
#define CALL(sym, ...) fs_call(sym, (const uint64_t[6]){__VA_ARGS__})
#define RES(sym, ...) ((uint32_t)CALL(sym, __VA_ARGS__))
-static int64_t independent_read(HANDLE f, uint8_t *buf, uint64_t n, uint64_t off) {
+static int64_t independent_read(PlatFile *f, uint8_t *buf, uint64_t n, uint64_t off) {
uint64_t done = 0;
while (done < n) {
int64_t r = host_read_at(f, buf + done, (uint32_t)(n - done), off + done);
@@ -1204,7 +1107,7 @@ static int64_t independent_read(HANDLE f, uint8_t *buf, uint64_t n, uint64_t off
// Read [pos, pos+n) of the open RomFS handle h through one of the three ReadFile overloads, and compare it with an
// independent read of romfs.bin at the index offset.
-static int compare_range(HANDLE image, uint64_t h, uint64_t offset, uint64_t pos, uint64_t n, int variant,
+static int compare_range(PlatFile *image, uint64_t h, uint64_t offset, uint64_t pos, uint64_t n, int variant,
uint8_t *a, uint8_t *b) {
uint64_t got = ~0ull;
uint32_t r;
@@ -1251,10 +1154,8 @@ static uint64_t probe_romfs(void) {
CHECK(RES(S_MOUNT_ROM, U("../x"), 0, 0) == FS_R_INVALID, "MountRom with a bad name");
probe_paths();
- wchar_t *w = fs_wide(romfs_image_path());
- HANDLE image = CreateFileW(w, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- free(w);
- if (image == INVALID_HANDLE_VALUE) rt_fatal("probe-fs: cannot open the RomFS image (error %lu)", GetLastError());
+ PlatFile *image = plat_file_open(romfs_image_path());
+ if (!image) rt_fatal("probe-fs: cannot open the RomFS image");
uint8_t *a = malloc(HEAD_BYTES), *b = malloc(HEAD_BYTES);
need(a && b, "probe buffers");
uint64_t step = count / SAMPLES, bytes = 0, sampled = 0, deep = 0, high = 0, largest = 0, lsize = 0;
@@ -1352,16 +1253,14 @@ static uint64_t probe_romfs(void) {
RES(S_READ_FILE, U(&got), h, 0, U(a), n) == 0 && got == n;
CALL(S_CLOSE_FILE, h);
}
- wchar_t *wf = fs_wide(file);
- HANDLE of = CreateFileW(wf, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- free(wf);
- ok = ok && of != INVALID_HANDLE_VALUE && independent_read(of, b, n, 0) == (int64_t)n && !memcmp(a, b, n);
- if (of != INVALID_HANDLE_VALUE) CloseHandle(of);
+ PlatFile *of = plat_file_open(file);
+ ok = ok && of && independent_read(of, b, n, 0) == (int64_t)n && !memcmp(a, b, n);
+ if (of) plat_file_close(of);
CHECK(ok, "overlay %zu (%s)", k, is_added ? "added" : "replacement");
ovok += ok;
}
if (nov) printf("romfs: %zu of %zu overlay files served from their overlay\n", ovok, nov);
- CloseHandle(image);
+ plat_file_close(image);
free(a);
free(b);
return count;
@@ -1370,12 +1269,12 @@ static uint64_t probe_romfs(void) {
// ---- save transactions: restarts, uncommitted and committed changes, a crash at every step of a Commit
// The process dies here: the save mount goes without Unmount, and the next start runs save_recover and mounts again.
static void probe_restart(const uint64_t *uid) {
- AcquireSRWLockExclusive(&lock);
+ plat_mutex_lock(&lock);
for (int i = 0; i < NMOUNTS; i++)
if (mounts[i].kind == M_SAVE) mounts[i].kind = M_FREE;
- ReleaseSRWLockExclusive(&lock);
+ plat_mutex_unlock(&lock);
save_mounts = tx_crash_at = tx_dead = 0;
- InterlockedExchange(&save_dirty, 0);
+ plat_xchg32(&save_dirty, 0);
save_recover();
CHECK(RES(S_MOUNT_SAVE, U("save"), U(uid)) == 0, "MountSaveData after a restart");
}
@@ -1418,39 +1317,29 @@ static int tx_read(const char *path) {
// The same, read from the committed save on disk.
static int tx_read_committed(const char *name) {
- wchar_t *n = fs_wide(name), *p = wjoin(w_save, n);
- HANDLE f = CreateFileW(p, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- free(n);
+ char *p = join(save_root, name);
+ PlatFile *f = plat_file_open(p);
free(p);
- if (f == INVALID_HANDLE_VALUE) return -1;
+ if (!f) return -1;
int ok = independent_read(f, tx_buf, TX_SIZE, 0) == TX_SIZE;
- CloseHandle(f);
+ plat_file_close(f);
return tx_byte(ok);
}
// Hard links of a file of the committed save on disk; 0 when absent.
static int tx_links_committed(const char *name) {
- wchar_t *n = fs_wide(name), *p = wjoin(w_save, n);
- HANDLE f = CreateFileW(p, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- free(n);
+ char *p = join(save_root, name);
+ int links = plat_fs_links(p);
free(p);
- BY_HANDLE_FILE_INFORMATION fi;
- int links = f != INVALID_HANDLE_VALUE && GetFileInformationByHandle(f, &fi) ? (int)fi.nNumberOfLinks : 0;
- if (f != INVALID_HANDLE_VALUE) CloseHandle(f);
return links;
}
-static int count_dirs(const wchar_t *dir) {
- wchar_t *pattern = wjoin(dir, L"*");
- WIN32_FIND_DATAW fd;
- HANDLE f = FindFirstFileW(pattern, &fd);
- free(pattern);
+static int count_dirs(const char *dir) {
+ Listing l;
+ list_dir(dir, &l);
int n = 0;
- if (f != INVALID_HANDLE_VALUE) {
- do n += (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && fd.cFileName[0] != L'.';
- while (FindNextFileW(f, &fd));
- FindClose(f);
- }
+ for (int i = 0; i < l.n; i++) n += l.e[i].kind == PLAT_FS_DIR && l.e[i].name[0] != '.';
+ listing_free(l.e, l.n);
return n;
}
@@ -1499,11 +1388,11 @@ static void probe_save_tx(const uint64_t *uid) {
else mixed++;
CHECK(ok && (va == vb) && (va == committed), "crash at commit step %d: tx_a %d, tx_b %d (old %d, new %d)", k,
va, vb, committed, next);
- CHECK(!exists_w(w_new) && !exists_w(w_old), "crash at commit step %d: staging left after recovery", k);
+ CHECK(!exists(new_root) && !exists(old_root), "crash at commit step %d: staging left after recovery", k);
}
CHECK(steps >= 4 && olds && news && !mixed, "crash points: %d (%d old, %d new, %d mixed)", steps, olds, news,
mixed);
- int backups = count_dirs(w_backup);
+ int backups = count_dirs(backup_root);
CHECK(backups == SAVE_BACKUPS, "save-backup holds %d saves", backups);
// a Commit writes the changed file and links the unchanged one to the committed save it replaces
@@ -1540,11 +1429,9 @@ static void probe_save_tx(const uint64_t *uid) {
}
static void probe_storage(void) {
- wchar_t tmpw[MAX_PATH + 1];
- char tmp[MAX_PATH * 3], dir[MAX_PATH * 3 + 64];
- DWORD n = GetTempPathW(MAX_PATH + 1, tmpw);
- need(n && WideCharToMultiByte(CP_UTF8, 0, tmpw, -1, tmp, sizeof tmp, NULL, NULL) > 0, "temporary directory");
- snprintf(dir, sizeof dir, "%stotk-probe-fs-%lu", tmp, GetCurrentProcessId());
+ char tmp[PLAT_PATH_MAX * 3], dir[PLAT_PATH_MAX * 3 + 64];
+ need(plat_temp_dir(tmp, sizeof tmp), "temporary directory");
+ snprintf(dir, sizeof dir, "%s/totk-probe-fs-%u", tmp, plat_process_id());
remove_tree(dir);
fs_host_set_profile(dir);
uint64_t uid[2], other[2], h = 0, h2 = 0, d = 0, l = 0;
@@ -1629,13 +1516,11 @@ static void probe_storage(void) {
char host[1200];
snprintf(host, sizeof host, "%s/save/%016llx%016llx/probe.sav", dir, (unsigned long long)uid[1],
(unsigned long long)uid[0]);
- wchar_t *w = fs_wide(host);
- HANDLE f = CreateFileW(w, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- free(w);
+ PlatFile *f = plat_file_open(host);
memset(back, 0, sizeof back);
- CHECK(f != INVALID_HANDLE_VALUE && independent_read(f, back, sizeof back, 0) == sizeof back &&
- !memcmp(back, data, sizeof data), "the save file on disk");
- if (f != INVALID_HANDLE_VALUE) CloseHandle(f);
+ CHECK(f && independent_read(f, back, sizeof back, 0) == sizeof back && !memcmp(back, data, sizeof data),
+ "the save file on disk");
+ if (f) plat_file_close(f);
// resize, rename, delete
CHECK(RES(S_OPEN_FILE, U(&h), U("save:/probe.sav"), FS_OPEN_READ | FS_OPEN_WRITE) == 0 &&
@@ -1673,7 +1558,7 @@ static void probe_storage(void) {
CHECK(RES(S_DELETE_CACHE, 3) == 0, "DeleteCacheStorage");
CALL(S_UNMOUNT, U("save"));
- int removed = !remove_tree(dir) && attributes(dir) == INVALID_FILE_ATTRIBUTES;
+ int removed = !remove_tree(dir) && !exists(dir);
CHECK(removed, "the temporary profile was not deleted");
printf("storage: save create/write/commit/reopen, directories, rename/delete, temporary and cache storage in a "
"temporary profile: %s; profile deleted: %s\n", failures ? "see FAIL lines" : "ok", removed ? "yes" : "no");
@@ -1687,7 +1572,7 @@ int probe_fs(void) {
probe_addr[i] = hostfn_register(probe_symbols[i], fn);
}
printf("overrides: %d nn::fs overrides registered\n", NPROBE);
- pcpu = _aligned_malloc(sizeof(Cpu), 64);
+ pcpu = plat_aligned_alloc(sizeof(Cpu), 64);
need(pcpu != NULL, "probe Cpu");
memset(pcpu, 0, sizeof *pcpu);
uint8_t *stack = mem_kernel_alloc(0x10000, 16);
@@ -1701,7 +1586,7 @@ int probe_fs(void) {
for (int r = 19; r < 30; r++) kept &= pcpu->x[r] == 0x1900u + r;
CHECK(kept && pcpu->sp == (uint64_t)(uintptr_t)stack + 0x10000, "an override lost a callee-saved register");
mem_kernel_free(stack, 0x10000);
- _aligned_free(pcpu);
+ plat_aligned_free(pcpu);
if (failures) {
printf("probe-fs: FAILED (%d of %d checks)\n", failures, checks);
return 1;
diff --git a/runtime/gfx.h b/runtime/gfx.h
index 5acadd2a..4004d46b 100644
--- a/runtime/gfx.h
+++ b/runtime/gfx.h
@@ -16,7 +16,7 @@ const OvNvState *ov_nv_state(void); // ov_gfx.c
#define RT_GPU_VA_BASE 0x8000000000ull // 2^39, never mapped on the host
#define RT_GPU_VA_SIZE (64ull << 30)
-// ---- window, vsync, focus (gfx_window.c, vk_device.c). Host threads only; never run guest code.
+// ---- window, vsync, focus (gfx_present.c and the window back end, gfx_wsys.h; vk_device.c). Host threads only; never run guest code.
void gfx_window_start(void); // window thread, Vulkan device, swapchain; idempotent
void gfx_vsync_bind(uint64_t system_event); // nn::vi vsync event; signalled at 60 Hz
uint64_t gfx_vsync_ticks(void); // ticks since start
diff --git a/runtime/gfx_abi_probe.c b/runtime/gfx_abi_probe.c
index 69eb4709..24ff6632 100644
--- a/runtime/gfx_abi_probe.c
+++ b/runtime/gfx_abi_probe.c
@@ -5,9 +5,9 @@
// Needs no game files.
#include
#include
-#include
#include "gfx.h"
+#include "platform/platform.h"
static int failures, checks;
#define CHECK(cond, ...) do { checks++; if (!(cond)) { failures++; printf(" FAIL: " __VA_ARGS__); putchar('\n'); } } while (0)
@@ -65,8 +65,8 @@ static void check_write_watch(void) {
// read) thaws it and holds it thawed; SetHeapSize below decommits it.
mem_written_pages(heap, 0x200000, pages, 8);
CHECK(mem_freeze(heap) == 1 && mem_frozen(heap), "freeze of an idle committed 2 MiB region");
- MEMORY_BASIC_INFORMATION mbi;
- CHECK(VirtualQuery((void *)(uintptr_t)heap, &mbi, sizeof mbi) && mbi.Protect == PAGE_READONLY, "frozen region is not read-only");
+ uint64_t end;
+ CHECK(plat_mem_state(heap, heap + 1, &end) == PLAT_PROT_READ, "frozen region is not read-only");
p[0x3000] = 7;
CHECK(!mem_frozen(heap) && p[0x3000] == 7, "a write into a frozen region did not thaw it");
n = mem_written_pages(heap, 0x200000, pages, 8);
@@ -88,12 +88,11 @@ static void check_write_watch(void) {
// A region with reserved holes freezes its committed pages only, and a write thaws them without committing a hole.
uint64_t ra = a & ~0x1fffffull;
CHECK(mem_freeze(ra) == 1 && mem_frozen(ra), "freeze of a 2 MiB region with reserved holes");
- CHECK(VirtualQuery((void *)(uintptr_t)a, &mbi, sizeof mbi) && mbi.Protect == PAGE_READONLY, "its committed pages are not read-only");
+ CHECK(plat_mem_state(a, a + 1, &end) == PLAT_PROT_READ, "its committed pages are not read-only");
*(volatile uint8_t *)(uintptr_t)(a + 0x2000) = 9;
CHECK(!mem_frozen(ra) && *(volatile uint8_t *)(uintptr_t)(a + 0x2000) == 9, "a write did not thaw the region with holes");
- CHECK(VirtualQuery((void *)(uintptr_t)a, &mbi, sizeof mbi) && mbi.Protect == PAGE_READWRITE && mbi.RegionSize == 0x4000,
- "the thawed pages are not read-write");
- CHECK(VirtualQuery((void *)(uintptr_t)(a + 0x4000), &mbi, sizeof mbi) && mbi.State == MEM_RESERVE, "a hole got committed");
+ CHECK(plat_mem_state(a, a + 0x10000, &end) == PLAT_PROT_RW && end == a + 0x4000, "the thawed pages are not read-write");
+ CHECK(plat_mem_state(a + 0x4000, a + 0x4001, &end) == PLAT_MEM_RESERVED, "a hole got committed");
printf("write-watch: region with reserved holes frozen and thawed by a write\n");
CHECK(mem_unmap_physical(a, 0x4000) == 0, "UnmapPhysicalMemory");
CHECK(mem_written_pages(RT_KERNEL_BASE, 0x1000, pages, 8) == UINT64_MAX, "kernel region is not write-watched");
diff --git a/runtime/gfx_window.c b/runtime/gfx_present.c
similarity index 63%
rename from runtime/gfx_window.c
rename to runtime/gfx_present.c
index f62de225..969f1330 100644
--- a/runtime/gfx_window.c
+++ b/runtime/gfx_present.c
@@ -1,30 +1,31 @@
-// Win32 window, Vulkan surface and swapchain, present blit, 60 Hz vsync thread, focus.
+// Vulkan surface and swapchain, present blit, 60 Hz vsync thread, focus: the window-system-free half of the window
+// (the other half: gfx_window_win.c with Win32, gfx_window_sdl.c with SDL, through gfx_wsys.h).
// Swapchain set-up ported from LAS Recomp, runtime/nvn/vulkan/device.c:254-330 (https://github.com/peiikai/LASRecomp @
-// da35b83a9611d046a9a681aae668d7085954d37f, MIT, Copyright (c) 2026 Peiikai), SDL replaced by Win32. Focus and vsync
-// behaviour from MacNativeGames, games/totk/runtime/frontend.m:88-97, 175-176, 350-352 (https://github.com/ly05010419/
-// MacNativeGames @ b75be8cfa704df3701e86130c8ced268e79d6b11, MIT, Copyright (c) 2026 MacNativeGames contributors).
+// da35b83a9611d046a9a681aae668d7085954d37f, MIT, Copyright (c) 2026 Peiikai). Focus and vsync behaviour from
+// MacNativeGames, games/totk/runtime/frontend.m:88-97, 175-176, 350-352 (https://github.com/ly05010419/MacNativeGames @
+// b75be8cfa704df3701e86130c8ced268e79d6b11, MIT, Copyright (c) 2026 MacNativeGames contributors).
//
-// Threads: the window thread owns the HWND and its message loop and never runs guest code. The vsync thread signals the
+// Threads: the window thread (the back end's) owns the window and never runs guest code. The vsync thread signals the
// bound system events. Presents come from any host thread (the back end's present worker) and take g_pl; every queue
-// submission takes the recursive queue lock (vk_queue_lock). The swapchain is rebuilt lazily: WM_SIZE only marks it
-// dirty, the next present (or gfx_window_resize) rebuilds it.
+// submission takes the recursive queue lock (vk_queue_lock). The swapchain is rebuilt lazily: a resize only marks it
+// dirty (gfx_window_mark_dirty), the next present (or gfx_window_resize) rebuilds it.
//
-// The in-game menu (ui/menu.h) draws over the game image in the present's command buffer, after the blit; its keyboard
-// and mouse come from the window procedure (ui/menu_win.cpp). The window's size and fullscreen state are the settings
-// TOTK_WINDOW_SIZE and TOTK_FULLSCREEN, applied at start and whenever a cfg_set changes them (window_settings_live).
+// The in-game menu (ui/menu.h) draws over the game image in the present's command buffer, after the blit. The window's
+// size and fullscreen state are the settings TOTK_WINDOW_SIZE and TOTK_FULLSCREEN, which the back end applies at start
+// and whenever a cfg_set changes them (window_settings_live -> gfx_window_apply_settings).
#include
#include
#include
-#include
#include "cfg.h"
#include "flight.h"
#include "fs.h"
#include "gfx.h"
-#include "input.h"
+#include "gfx_wsys.h"
#include "nvn_api.h"
#include "ui/menu.h"
#include "vk.h"
+#include "platform/platform.h"
#define FRAMES 2
#define MAX_IMAGES 16
@@ -32,25 +33,63 @@
#define VSYNC_HZ 60
// ---- queue lock
-static CRITICAL_SECTION g_ql;
-__attribute__((constructor)) static void window_init_locks(void);
-void vk_queue_lock(void) { EnterCriticalSection(&g_ql); }
-void vk_queue_unlock(void) { LeaveCriticalSection(&g_ql); }
+static PlatRMutex g_ql;
+void vk_queue_lock(void) { plat_rmutex_lock(&g_ql); }
+void vk_queue_unlock(void) { plat_rmutex_unlock(&g_ql); }
-// ---- window state
-static HWND g_hwnd;
-static HANDLE g_wnd_ready;
+// ---- window state shared with the back end
static VkSurfaceKHR g_surface;
-static volatile LONG g_dirty; // WM_SIZE seen; swapchain stale
-static volatile LONG g_focused = 1;
-static volatile LONG g_focus_msgs;
-static CRITICAL_SECTION g_start_lock; // gfx_window_start / vsync start
-static int g_started, g_wnd_failed;
-static int g_background; // TOTK_BACKGROUND_WINDOW: the window stays behind the user's windows
-static int g_fs_pending; // window thread: TOTK_FULLSCREEN waits for the first activation
+static volatile int32_t g_dirty; // the window was resized; swapchain stale
+static volatile int32_t g_focused = 1;
+static volatile int32_t g_focus_msgs;
+static PlatRMutex g_start_lock; // gfx_window_start / vsync start
+static int g_started;
+
+void gfx_window_mark_dirty(void) { plat_xchg32(&g_dirty, 1); }
+
+// The window gained or lost the focus: the game hears it as nn::oe's focus state and a notification message.
+void gfx_window_focus_changed(int in) {
+ plat_xchg32(&g_focused, in);
+ oe_set_focus(in); // pinned to 1 by TOTK_ASSUME_FOCUS inside
+ oe_push_message(OE_MSG_FOCUS_STATE_CHANGED);
+ plat_inc32(&g_focus_msgs);
+}
+
+// The window was closed: the process ends with it, as in MNG, once a save Commit still running in the background is
+// done.
+void gfx_window_closed(void) {
+ rt_log("gfx: window closed; exiting");
+ fs_save_flush();
+ vk_pipeline_save();
+ prof_write(); // plat_exit runs no atexit handlers
+ plat_exit(0);
+}
+
+// TOTK_FULLSCREEN=1: borderless window over the monitor. TOTK_WINDOW_SIZE=x: client area of the window (the
+// swapchain follows the window; the game image is scaled to it).
+int gfx_cfg_fullscreen(void) { return cfg_bool("TOTK_FULLSCREEN"); }
+
+void gfx_cfg_window_size(int *w, int *h) {
+ int x = 0, y = 0;
+ if (sscanf(cfg_value("TOTK_WINDOW_SIZE"), "%dx%d", &x, &y) != 2 || x < 640 || y < 360 || x > 7680 || y > 4320)
+ sscanf(cfg_meta("WINDOW_SIZE")->def, "%dx%d", &x, &y); // unparsable or out of range: the default
+ *w = x;
+ *h = y;
+}
+
+// The refresh rate of the window's monitor (TOTK_FPS_LIMIT=display), measured by the back end's window thread when the
+// window opens, lands on another monitor or the display mode changes; readers take the stored value.
+static volatile int32_t g_rate_mhz; // millihertz, 0 unknown
+
+void gfx_window_set_refresh(double hz) {
+ int32_t mhz = (int32_t)(hz * 1000 + 0.5);
+ if (plat_xchg32(&g_rate_mhz, mhz) != mhz) rt_log("gfx: monitor refresh rate %.3f Hz", hz);
+}
+
+double gfx_window_refresh_hz(void) { return plat_cas32(&g_rate_mhz, 0, 0) / 1000.0; }
// ---- swapchain and present state (g_pl)
-static CRITICAL_SECTION g_pl;
+static PlatRMutex g_pl;
static VkSwapchainKHR g_sc;
static VkFormat g_sc_fmt;
static VkExtent2D g_sc_ext;
@@ -88,187 +127,82 @@ static VkBuffer g_th_buf;
static VmaAllocation g_th_alloc;
static uint8_t *g_th_ptr;
-static void window_init_locks(void) {
- InitializeCriticalSection(&g_ql);
- InitializeCriticalSection(&g_pl);
- InitializeCriticalSection(&g_start_lock);
-}
-
// ---- vsync
-static SRWLOCK g_vs_lock = SRWLOCK_INIT;
+static PlatMutex g_vs_lock = PLAT_MUTEX_INIT;
static uint64_t g_vs_ev[16];
static int g_vs_nev;
-static volatile long *g_vs_ctr[4];
+static volatile int32_t *g_vs_ctr[4];
static int g_vs_nctr;
-static volatile LONGLONG g_ticks;
-static volatile LONGLONG g_vs_start_ns;
+static volatile int64_t g_ticks;
+static volatile int64_t g_vs_start_ns;
static int g_vs_started;
-static DWORD WINAPI vsync_main(void *arg) {
+static void vsync_main(void *arg) {
(void)arg;
- SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
- HANDLE t = CreateWaitableTimerExW(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
- if (!t) { rt_log("gfx: no high-resolution timer; vsync falls back to the system timer tick"); t = CreateWaitableTimerW(NULL, TRUE, NULL); }
+ plat_thread_priority(PLAT_PRIO_HIGHEST);
uint64_t n = 0;
for (;;) {
uint64_t start = (uint64_t)g_vs_start_ns;
uint64_t due = start + (n + 1) * VSYNC_NS / VSYNC_HZ;
uint64_t now = kern_now_ns();
if (now > due + 100000000ull) { // the process stalled: restart the schedule, no catch-up burst
- g_vs_start_ns = (LONGLONG)(now - (n + 1) * VSYNC_NS / VSYNC_HZ);
+ g_vs_start_ns = (int64_t)(now - (n + 1) * VSYNC_NS / VSYNC_HZ);
} else if (due > now) {
- LARGE_INTEGER d;
- d.QuadPart = -(LONGLONG)((due - now) / 100);
- if (d.QuadPart == 0) d.QuadPart = -1;
- SetWaitableTimer(t, &d, 0, NULL, NULL, FALSE);
- WaitForSingleObject(t, INFINITE);
+ plat_wait_ns(due - now);
}
n++;
- InterlockedExchange64(&g_ticks, (LONGLONG)n);
+ plat_xchg64(&g_ticks, (int64_t)n);
uint64_t ev[16];
- volatile long *ctr[4];
+ volatile int32_t *ctr[4];
int ne, nc;
- AcquireSRWLockShared(&g_vs_lock);
+ plat_mutex_lock_shared(&g_vs_lock);
ne = g_vs_nev;
nc = g_vs_nctr;
memcpy(ev, g_vs_ev, sizeof ev);
memcpy(ctr, g_vs_ctr, sizeof ctr);
- ReleaseSRWLockShared(&g_vs_lock);
- for (int i = 0; i < nc; i++) InterlockedIncrement(ctr[i]);
+ plat_mutex_unlock_shared(&g_vs_lock);
+ for (int i = 0; i < nc; i++) plat_inc32(ctr[i]);
for (int i = 0; i < ne; i++) kern_signal_system_event(ev[i]);
}
- return 0;
+ return;
}
static void vsync_ensure(void) {
- EnterCriticalSection(&g_start_lock);
+ plat_rmutex_lock(&g_start_lock);
if (!g_vs_started) {
g_vs_started = 1;
- g_vs_start_ns = (LONGLONG)kern_now_ns();
- HANDLE h = CreateThread(NULL, 0, vsync_main, NULL, 0, NULL);
- if (!h) rt_fatal("gfx: cannot start the vsync thread (error %lu)", GetLastError());
- CloseHandle(h);
+ g_vs_start_ns = (int64_t)kern_now_ns();
+ if (!plat_thread_spawn(vsync_main, NULL, "vsync", 0)) rt_fatal("gfx: cannot start the vsync thread");
}
- LeaveCriticalSection(&g_start_lock);
+ plat_rmutex_unlock(&g_start_lock);
}
void gfx_vsync_bind(uint64_t system_event) {
- AcquireSRWLockExclusive(&g_vs_lock);
+ plat_mutex_lock(&g_vs_lock);
int have = 0;
for (int i = 0; i < g_vs_nev; i++) have |= g_vs_ev[i] == system_event;
if (!have) {
if (g_vs_nev == 16) rt_fatal("gfx: more than 16 vsync events");
g_vs_ev[g_vs_nev++] = system_event;
}
- ReleaseSRWLockExclusive(&g_vs_lock);
+ plat_mutex_unlock(&g_vs_lock);
vsync_ensure();
}
-void gfx_vsync_bind_counter(volatile long *counter) {
- AcquireSRWLockExclusive(&g_vs_lock);
+void gfx_vsync_bind_counter(volatile int32_t *counter) {
+ plat_mutex_lock(&g_vs_lock);
if (g_vs_nctr == 4) rt_fatal("gfx: more than 4 vsync counters");
g_vs_ctr[g_vs_nctr++] = counter;
- ReleaseSRWLockExclusive(&g_vs_lock);
+ plat_mutex_unlock(&g_vs_lock);
vsync_ensure();
}
-uint64_t gfx_vsync_ticks(void) { return (uint64_t)InterlockedCompareExchange64(&g_ticks, 0, 0); }
+uint64_t gfx_vsync_ticks(void) { return (uint64_t)plat_cas64(&g_ticks, 0, 0); }
uint64_t gfx_vsync_time_ns(uint64_t tick) { return (uint64_t)g_vs_start_ns + tick * VSYNC_NS / VSYNC_HZ; }
// ---- focus
int gfx_window_focused(void) { return g_focused; }
unsigned gfx_window_focus_messages(void) { return (unsigned)g_focus_msgs; }
-HWND gfx_window_handle(void) { return g_hwnd; }
-
-// ---- window thread
-#define WND_STYLE WS_OVERLAPPEDWINDOW
-#define WM_APP_SETTINGS (WM_APP + 1)
-#define WM_APP_FULLSCREEN (WM_APP + 2) // the fullscreen setting, held back until the window is first activated
-
-static WINDOWPLACEMENT g_saved_place = {sizeof g_saved_place}; // the window behind fullscreen (window thread)
-
-static int is_fullscreen(HWND h) { return !(GetWindowLongW(h, GWL_STYLE) & WS_OVERLAPPEDWINDOW); }
-
-// TOTK_FULLSCREEN=1: borderless window over the monitor. TOTK_WINDOW_SIZE=x: client area of the window (the
-// swapchain follows the window; the game image is scaled to it).
-static int cfg_fullscreen(void) {
- return cfg_bool("TOTK_FULLSCREEN");
-}
-
-static void cfg_window_size(int *w, int *h) {
- int x = 0, y = 0;
- if (sscanf(cfg_value("TOTK_WINDOW_SIZE"), "%dx%d", &x, &y) != 2 || x < 640 || y < 360 || x > 7680 || y > 4320)
- sscanf(cfg_meta("WINDOW_SIZE")->def, "%dx%d", &x, &y); // unparsable or out of range: the default
- *w = x;
- *h = y;
-}
-
-// F11: borderless window over the whole monitor, and back to the saved window. The swapchain follows through WM_SIZE.
-// Not Alt+Enter: Enter is the Plus button (input_win.c). A background window (test runs) keeps its place behind the
-// user's windows and takes no focus.
-static void toggle_fullscreen(HWND h) {
- LONG style = GetWindowLongW(h, GWL_STYLE);
- if (style & WS_OVERLAPPEDWINDOW) {
- MONITORINFO mi = {sizeof mi};
- if (!GetWindowPlacement(h, &g_saved_place) || !GetMonitorInfoW(MonitorFromWindow(h, MONITOR_DEFAULTTOPRIMARY), &mi))
- return;
- SetWindowLongW(h, GWL_STYLE, (style & ~WS_OVERLAPPEDWINDOW) | WS_POPUP);
- SetWindowPos(h, HWND_TOP, mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right - mi.rcMonitor.left,
- mi.rcMonitor.bottom - mi.rcMonitor.top,
- SWP_NOOWNERZORDER | SWP_FRAMECHANGED | (g_background ? SWP_NOZORDER | SWP_NOACTIVATE : 0));
- } else {
- SetWindowLongW(h, GWL_STYLE, (style & ~WS_POPUP) | WND_STYLE);
- if (g_background) g_saved_place.showCmd = SW_SHOWNOACTIVATE;
- SetWindowPlacement(h, &g_saved_place);
- SetWindowPos(h, NULL, 0, 0, 0, 0,
- SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED |
- (g_background ? SWP_NOACTIVATE : 0));
- }
-}
-
-// The window size and fullscreen state last taken from the settings (window thread). apply_settings acts on a change of
-// either value only, so a window resized by hand keeps its size when another setting changes.
-static char g_applied_size[32];
-static int g_applied_fs;
-
-static void note_applied(void) {
- snprintf(g_applied_size, sizeof g_applied_size, "%s", cfg_value("TOTK_WINDOW_SIZE"));
- g_applied_fs = cfg_fullscreen();
-}
-
-// Window thread: the window takes the size and fullscreen state of the settings when they changed. In fullscreen the
-// size goes into the placement restored when fullscreen ends.
-static void apply_settings(HWND h) {
- int size_changed = strcmp(cfg_value("TOTK_WINDOW_SIZE"), g_applied_size) != 0, fs = cfg_fullscreen(), fs_changed = fs != g_applied_fs;
- note_applied();
- if (size_changed) {
- int cw, ch;
- cfg_window_size(&cw, &ch);
- RECT r = {0, 0, cw, ch};
- AdjustWindowRect(&r, WND_STYLE, FALSE);
- if (is_fullscreen(h)) {
- RECT *n = &g_saved_place.rcNormalPosition;
- n->right = n->left + (r.right - r.left);
- n->bottom = n->top + (r.bottom - r.top);
- } else {
- RECT have;
- if (GetClientRect(h, &have) && (have.right != cw || have.bottom != ch)) {
- if (IsZoomed(h)) ShowWindow(h, SW_RESTORE);
- SetWindowPos(h, NULL, 0, 0, r.right - r.left, r.bottom - r.top,
- SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
- }
- }
- }
- if (fs_changed && fs != is_fullscreen(h)) {
- input_cursor_suspend(1);
- toggle_fullscreen(h);
- input_cursor_suspend(0);
- }
-}
-
-void gfx_window_apply_settings(void) {
- if (g_hwnd) PostMessageW(g_hwnd, WM_APP_SETTINGS, 0, 0);
-}
// Present path (g_pl held): a settings change (cfg_version) reaches the window thread, which applies a changed size or
// fullscreen; a changed TOTK_VK_PRESENT rebuilds the swapchain at this present.
@@ -282,176 +216,12 @@ static void window_settings_live(void) {
const char *e = cfg_value("TOTK_VK_PRESENT");
if (!started || strncmp(e, present, sizeof present - 1)) {
snprintf(present, sizeof present, "%s", e);
- if (started) InterlockedExchange(&g_dirty, 1);
+ if (started) plat_xchg32(&g_dirty, 1);
}
if (started) gfx_window_apply_settings();
started = 1;
}
-void gfx_window_request_close(void) {
- if (g_hwnd) PostMessageW(g_hwnd, WM_CLOSE, 0, 0);
-}
-
-// ---- refresh rate of the window's monitor (TOTK_FPS_LIMIT=display). The window thread measures it when the window
-// opens, lands on another monitor or the display mode changes; readers take the stored value. QueryDisplayConfig gives
-// the exact rational rate (59.94, 143.998); EnumDisplaySettings' whole hertz is the fallback.
-static HMONITOR g_rate_mon;
-static volatile LONG g_rate_mhz; // millihertz, 0 unknown
-
-static double monitor_rate_hz(HMONITOR mon) {
- MONITORINFOEXW mi = {.cbSize = sizeof mi};
- if (!GetMonitorInfoW(mon, (MONITORINFO *)&mi)) return 0;
- double hz = 0;
- UINT32 np = 0, nm = 0;
- if (GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &np, &nm) == ERROR_SUCCESS && np && nm) {
- DISPLAYCONFIG_PATH_INFO *paths = calloc(np, sizeof *paths);
- DISPLAYCONFIG_MODE_INFO *modes = calloc(nm, sizeof *modes);
- if (paths && modes && QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &np, paths, &nm, modes, NULL) == ERROR_SUCCESS)
- for (UINT32 i = 0; i < np && !hz; i++) {
- DISPLAYCONFIG_SOURCE_DEVICE_NAME src = {.header = {.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME,
- .size = sizeof src,
- .adapterId = paths[i].sourceInfo.adapterId,
- .id = paths[i].sourceInfo.id}};
- DISPLAYCONFIG_RATIONAL r = paths[i].targetInfo.refreshRate;
- if (DisplayConfigGetDeviceInfo(&src.header) == ERROR_SUCCESS && !wcscmp(src.viewGdiDeviceName, mi.szDevice) &&
- r.Denominator)
- hz = (double)r.Numerator / r.Denominator;
- }
- free(paths);
- free(modes);
- }
- DEVMODEW dm = {.dmSize = sizeof dm};
- if (!hz && EnumDisplaySettingsW(mi.szDevice, ENUM_CURRENT_SETTINGS, &dm) && dm.dmDisplayFrequency > 1)
- hz = dm.dmDisplayFrequency; // 0 and 1 mean the hardware default
- return hz;
-}
-
-// Window thread. force: the display mode changed, measure even on the same monitor.
-static void refresh_rate_update(HWND h, int force) {
- HMONITOR mon = MonitorFromWindow(h, MONITOR_DEFAULTTOPRIMARY);
- if (!force && mon == g_rate_mon) return;
- g_rate_mon = mon;
- double hz = monitor_rate_hz(mon);
- LONG mhz = (LONG)(hz * 1000 + 0.5);
- if (InterlockedExchange(&g_rate_mhz, mhz) != mhz) rt_log("gfx: monitor refresh rate %.3f Hz", hz);
-}
-
-double gfx_window_refresh_hz(void) { return InterlockedCompareExchange(&g_rate_mhz, 0, 0) / 1000.0; }
-
-int gfx_window_monitor_size(int *w, int *h) {
- MONITORINFO mi = {sizeof mi};
- if (!g_hwnd || !GetMonitorInfoW(MonitorFromWindow(g_hwnd, MONITOR_DEFAULTTOPRIMARY), &mi)) return 0;
- *w = mi.rcMonitor.right - mi.rcMonitor.left;
- *h = mi.rcMonitor.bottom - mi.rcMonitor.top;
- return 1;
-}
-
-static LRESULT CALLBACK wnd_proc(HWND h, UINT m, WPARAM w, LPARAM l) {
- LRESULT input_res;
- if (input_window_msg(h, m, w, l, &input_res)) return input_res;
- if (menu_window_msg(h, m, w, l, &input_res)) return input_res;
- switch (m) {
- case WM_KEYDOWN:
- if (w == VK_F11 && !(l & (1 << 30))) { // bit 30: auto-repeat
- input_cursor_suspend(1);
- toggle_fullscreen(h);
- input_cursor_suspend(0);
- cfg_set("TOTK_FULLSCREEN", is_fullscreen(h) ? "1" : "0"); // the menu shows it; the next start keeps it
- cfg_save();
- note_applied(); // what cfg_get reads now (the environment may still override it)
- }
- break;
- case WM_APP_SETTINGS:
- apply_settings(h);
- return 0;
- case WM_APP_FULLSCREEN:
- if (cfg_fullscreen() && !is_fullscreen(h)) {
- input_cursor_suspend(1);
- toggle_fullscreen(h);
- input_cursor_suspend(0);
- }
- return 0;
- case WM_SIZE:
- if (w != SIZE_MINIMIZED) InterlockedExchange(&g_dirty, 1);
- refresh_rate_update(h, 0);
- return 0;
- case WM_EXITSIZEMOVE: // dragged onto another monitor
- refresh_rate_update(h, 0);
- break;
- case WM_DISPLAYCHANGE:
- refresh_rate_update(h, 1);
- break;
- case WM_ACTIVATEAPP: {
- int in = w != 0;
- InterlockedExchange(&g_focused, in);
- oe_set_focus(in); // pinned to 1 by TOTK_ASSUME_FOCUS inside
- oe_push_message(OE_MSG_FOCUS_STATE_CHANGED);
- InterlockedIncrement(&g_focus_msgs);
- if (in && g_fs_pending) {
- g_fs_pending = 0;
- PostMessageW(h, WM_APP_FULLSCREEN, 0, 0);
- }
- return 0;
- }
- case WM_CLOSE:
- // 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);
- case WM_ERASEBKGND:
- return 1;
- }
- return DefWindowProcW(h, m, w, l);
-}
-
-static DWORD WINAPI window_main(void *arg) {
- (void)arg;
- HINSTANCE inst = GetModuleHandleW(NULL);
- // Icon resource 1 of totk.rc (runtime/totk.ico): the title bar, the taskbar button and Alt+Tab.
- WNDCLASSEXW wc = {.cbSize = sizeof wc, .lpfnWndProc = wnd_proc, .hInstance = inst,
- .hIcon = LoadImageW(inst, MAKEINTRESOURCEW(1), IMAGE_ICON, GetSystemMetrics(SM_CXICON),
- GetSystemMetrics(SM_CYICON), LR_SHARED),
- .hIconSm = LoadImageW(inst, MAKEINTRESOURCEW(1), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
- GetSystemMetrics(SM_CYSMICON), LR_SHARED),
- .hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW), .lpszClassName = L"TotKRecompWindow"};
- RegisterClassExW(&wc);
- int cw, ch;
- cfg_window_size(&cw, &ch);
- RECT r = {0, 0, cw, ch};
- AdjustWindowRect(&r, WND_STYLE, FALSE);
- // The window opens behind the user's windows without taking focus: it shows the game's blank loading frames for a
- // while, and must not cover what the player works on meanwhile. The game still counts itself in focus until the
- // first WM_ACTIVATEAPP, so loading goes on, and keyboard input waits for the foreground (input_win.c). Its taskbar
- // button flashes until it is brought up, and TOTK_FULLSCREEN waits for that first activation.
- // TOTK_BACKGROUND_WINDOW=1 (test runs): no flashing, never fullscreen at start, and F11 keeps the window's place.
- int background = g_background = cfg_get("TOTK_BACKGROUND_WINDOW") && atoi(cfg_get("TOTK_BACKGROUND_WINDOW"));
- g_hwnd = CreateWindowExW(0, wc.lpszClassName, L"TotK Recomp", WND_STYLE, CW_USEDEFAULT, CW_USEDEFAULT,
- r.right - r.left, r.bottom - r.top, NULL, NULL, inst, NULL);
- if (!g_hwnd) {
- rt_log("gfx: CreateWindowEx failed (%lu)", GetLastError());
- g_wnd_failed = 1;
- } else {
- SetWindowPos(g_hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW);
- if (!background) {
- FLASHWINFO fw = {.cbSize = sizeof fw, .hwnd = g_hwnd, .dwFlags = FLASHW_TRAY | FLASHW_TIMERNOFG};
- FlashWindowEx(&fw);
- g_fs_pending = cfg_fullscreen();
- }
- note_applied();
- refresh_rate_update(g_hwnd, 1);
- }
- SetEvent(g_wnd_ready);
- if (g_wnd_failed) return 1;
- MSG msg;
- while (GetMessageW(&msg, NULL, 0, 0) > 0) {
- TranslateMessage(&msg);
- DispatchMessageW(&msg);
- }
- return 0;
-}
-
// ---- swapchain
static const char *g_present_got = ""; // the TOTK_VK_PRESENT value of the mode the swapchain got; "" before one
@@ -490,7 +260,7 @@ static void flip_locked(void) {
vk_queue_lock();
VkResult r = vkQueuePresentKHR(vk_ctx()->gfx_queue, &pi);
vk_queue_unlock();
- if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR) InterlockedExchange(&g_dirty, 1);
+ if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR) plat_xchg32(&g_dirty, 1);
}
static void free_capture(void) {
@@ -508,10 +278,10 @@ static int create_swapchain(void) {
VkCtx *c = vk_ctx();
VkSurfaceCapabilitiesKHR caps;
if (vkGetPhysicalDeviceSurfaceCapabilitiesKHR(c->phys, g_surface, &caps) != VK_SUCCESS) return 0;
- RECT rc = {0};
- GetClientRect(g_hwnd, &rc);
+ int cw = 0, ch = 0;
+ wsys_client_size(&cw, &ch);
VkExtent2D ext = caps.currentExtent.width != UINT32_MAX ? caps.currentExtent
- : (VkExtent2D){(uint32_t)rc.right, (uint32_t)rc.bottom};
+ : (VkExtent2D){(uint32_t)cw, (uint32_t)ch};
if (!ext.width || !ext.height) return 0;
uint32_t nf = 0;
vkGetPhysicalDeviceSurfaceFormatsKHR(c->phys, g_surface, &nf, NULL);
@@ -568,7 +338,7 @@ static int create_swapchain(void) {
}
free_capture();
vk_queue_unlock();
- InterlockedExchange(&g_dirty, 0);
+ plat_xchg32(&g_dirty, 0);
return 1;
}
@@ -598,15 +368,15 @@ static int ensure_capture_buffer(void) {
}
void vk_thumbs_enable(int n) {
- EnterCriticalSection(&g_pl);
+ plat_rmutex_lock(&g_pl);
if (!g_th_n && n > 0 && (g_th_tag = calloc((size_t)n, sizeof *g_th_tag))) g_th_n = n;
- LeaveCriticalSection(&g_pl);
+ plat_rmutex_unlock(&g_pl);
}
void vk_thumbs_frame(uint64_t frame) {
- EnterCriticalSection(&g_pl);
+ plat_rmutex_lock(&g_pl);
g_th_frame = frame;
- LeaveCriticalSection(&g_pl);
+ plat_rmutex_unlock(&g_pl);
}
static void free_thumbs(void) {
@@ -689,7 +459,7 @@ static void record_thumb(VkCommandBuffer cb, VkImage dst, unsigned fence_slot) {
int vk_thumbs_read(uint64_t frame, uint8_t *rgba) {
int ok = 0;
- EnterCriticalSection(&g_pl);
+ plat_rmutex_lock(&g_pl);
if (g_th_n && g_th_ptr && frame) {
unsigned ring = (unsigned)(frame % (uint64_t)g_th_n);
if (g_th_tag[ring] == frame) {
@@ -699,7 +469,7 @@ int vk_thumbs_read(uint64_t frame, uint8_t *rgba) {
ok = g_th_fmt == VK_FORMAT_B8G8R8A8_UNORM || g_th_fmt == VK_FORMAT_B8G8R8A8_SRGB ? 2 : 1;
}
}
- LeaveCriticalSection(&g_pl);
+ plat_rmutex_unlock(&g_pl);
if (ok == 2)
for (size_t i = 0; i < TH_BYTES; i += 4) {
uint8_t t = rgba[i];
@@ -709,17 +479,17 @@ int vk_thumbs_read(uint64_t frame, uint8_t *rgba) {
}
void vk_present_capture(int on) {
- EnterCriticalSection(&g_pl);
+ plat_rmutex_lock(&g_pl);
g_capture = on;
if (!on) g_cap_valid = 0;
- LeaveCriticalSection(&g_pl);
+ plat_rmutex_unlock(&g_pl);
}
int vk_present_captured(const uint8_t **px, int *w, int *h, VkFormat *fmt) {
- EnterCriticalSection(&g_pl);
+ plat_rmutex_lock(&g_pl);
int ok = g_cap_valid;
if (ok) { *px = g_cap_ptr; *w = g_cap_w; *h = g_cap_h; *fmt = g_sc_fmt; }
- LeaveCriticalSection(&g_pl);
+ plat_rmutex_unlock(&g_pl);
return ok;
}
@@ -744,12 +514,12 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w
int general = layout == VK_IMAGE_LAYOUT_GENERAL;
gfx_window_start();
VkCtx *c = vk_ctx();
- EnterCriticalSection(&g_pl);
+ plat_rmutex_lock(&g_pl);
flip_locked();
- if (!g_sc || InterlockedCompareExchange(&g_dirty, 0, 0)) {
- if (!IsIconic(g_hwnd) && !create_swapchain() && !g_sc) goto out;
+ if (!g_sc || plat_cas32(&g_dirty, 0, 0)) {
+ if (!wsys_minimized() && !create_swapchain() && !g_sc) goto out;
}
- if (IsIconic(g_hwnd) || !g_sc) goto out;
+ if (wsys_minimized() || !g_sc) goto out;
unsigned slot = g_frame % FRAMES;
if (vkWaitForFences(c->device, 1, &g_fence[slot], VK_TRUE, 5000000000ull) != VK_SUCCESS) {
@@ -759,12 +529,12 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w
uint32_t idx = 0;
VkResult r = vkAcquireNextImageKHR(c->device, g_sc, 1000000000ull, g_acquire[slot], VK_NULL_HANDLE, &idx);
if (r == VK_ERROR_OUT_OF_DATE_KHR) {
- InterlockedExchange(&g_dirty, 1);
+ plat_xchg32(&g_dirty, 1);
if (!create_swapchain()) goto out;
r = vkAcquireNextImageKHR(c->device, g_sc, 1000000000ull, g_acquire[slot], VK_NULL_HANDLE, &idx);
}
if (r != VK_SUCCESS && r != VK_SUBOPTIMAL_KHR) goto out; // lost, not ready: skip the frame
- if (r == VK_SUBOPTIMAL_KHR) InterlockedExchange(&g_dirty, 1);
+ if (r == VK_SUBOPTIMAL_KHR) plat_xchg32(&g_dirty, 1);
vkResetFences(c->device, 1, &g_fence[slot]);
if (g_th_pending[slot]) { // that fence has signalled: the thumbnail its submission wrote is complete
g_th_tag[g_th_pending[slot] % (uint64_t)g_th_n] = g_th_pending[slot];
@@ -882,7 +652,7 @@ static void present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w
if (menu_shot) save_capture(menu_shot);
}
out:
- LeaveCriticalSection(&g_pl);
+ plat_rmutex_unlock(&g_pl);
}
void vk_present_image(VkImage src, VkImageLayout layout, VkFormat fmt, int w, int h, const int crop[4]) {
@@ -894,25 +664,23 @@ void vk_present_blit(VkImage src, VkImageLayout layout, VkFormat fmt, int w, int
}
void vk_present_flip(void) {
- EnterCriticalSection(&g_pl);
+ plat_rmutex_lock(&g_pl);
flip_locked();
- LeaveCriticalSection(&g_pl);
+ plat_rmutex_unlock(&g_pl);
}
void gfx_window_resize(int cw, int chh) {
gfx_window_start();
- RECT r = {0, 0, cw, chh};
- AdjustWindowRect(&r, WND_STYLE, FALSE);
- SetWindowPos(g_hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
- EnterCriticalSection(&g_pl);
+ wsys_resize(cw, chh);
+ plat_rmutex_lock(&g_pl);
create_swapchain();
- LeaveCriticalSection(&g_pl);
+ plat_rmutex_unlock(&g_pl);
}
// Called by vk_shutdown before the device goes away; the window and its thread stay.
void gfx_window_release_vk(void) {
VkCtx *c = vk_ctx();
- EnterCriticalSection(&g_pl);
+ plat_rmutex_lock(&g_pl);
if (c && g_started) {
vk_queue_lock();
vkDeviceWaitIdle(c->device);
@@ -937,28 +705,18 @@ void gfx_window_release_vk(void) {
g_sc = VK_NULL_HANDLE; g_surface = VK_NULL_HANDLE; g_pool = VK_NULL_HANDLE; g_sc_n = 0;
g_started = 0; // the window and its thread stay; the next gfx_window_start makes a new surface
}
- LeaveCriticalSection(&g_pl);
+ plat_rmutex_unlock(&g_pl);
}
void gfx_window_start(void) {
- EnterCriticalSection(&g_start_lock);
+ plat_rmutex_lock(&g_start_lock);
if (!g_started) {
if (!vk_init()) rt_fatal("gfx: no usable Vulkan device (see the vk: lines above)");
VkCtx *c = vk_ctx();
- if (!g_hwnd) {
- g_wnd_failed = 0;
- g_wnd_ready = CreateEventW(NULL, TRUE, FALSE, NULL);
- HANDLE t = CreateThread(NULL, 0, window_main, NULL, 0, NULL);
- if (!t) rt_fatal("gfx: cannot start the window thread (error %lu)", GetLastError());
- CloseHandle(t);
- WaitForSingleObject(g_wnd_ready, INFINITE);
- CloseHandle(g_wnd_ready);
- if (g_wnd_failed) rt_fatal("gfx: window creation failed");
- }
- VkWin32SurfaceCreateInfoKHR si = {.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR,
- .hinstance = GetModuleHandleW(NULL), .hwnd = g_hwnd};
- VkResult vr = vkCreateWin32SurfaceKHR(c->instance, &si, NULL, &g_surface);
- if (vr != VK_SUCCESS) rt_fatal("gfx: vkCreateWin32SurfaceKHR failed (%d)", (int)vr);
+ static int opened;
+ if (!opened && !(opened = wsys_open())) rt_fatal("gfx: window creation failed");
+ g_surface = wsys_surface(c->instance);
+ if (!g_surface) rt_fatal("gfx: no Vulkan surface for the window");
VkCommandPoolCreateInfo pci = {.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, .queueFamilyIndex = c->gfx_family};
@@ -974,14 +732,13 @@ void gfx_window_start(void) {
}
g_frame = 0;
g_started = 1;
- EnterCriticalSection(&g_pl);
+ plat_rmutex_lock(&g_pl);
create_swapchain();
- LeaveCriticalSection(&g_pl);
- RECT rc = {0};
- GetClientRect(g_hwnd, &rc);
- rt_log("gfx: window %ldx%ld, swapchain %ux%u format %d", rc.right, rc.bottom, g_sc_ext.width, g_sc_ext.height,
- (int)g_sc_fmt);
+ plat_rmutex_unlock(&g_pl);
+ int cw = 0, ch = 0;
+ wsys_client_size(&cw, &ch);
+ rt_log("gfx: window %dx%d, swapchain %ux%u format %d", cw, ch, g_sc_ext.width, g_sc_ext.height, (int)g_sc_fmt);
}
- LeaveCriticalSection(&g_start_lock);
+ plat_rmutex_unlock(&g_start_lock);
vsync_ensure();
}
diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c
index 4df23fea..05fa5b82 100644
--- a/runtime/gfx_shader.c
+++ b/runtime/gfx_shader.c
@@ -33,7 +33,6 @@
#include
#include
#include
-#include
#include "cfg.h"
#include "gfx.h"
@@ -84,10 +83,10 @@ struct GfxShader {
uint32_t local[3];
};
-static SRWLOCK cache_lock = SRWLOCK_INIT; // memory cache
+static PlatMutex cache_lock = PLAT_MUTEX_INIT; // memory cache
static GfxShader *buckets[BUCKETS];
static UseEnt *use_buckets[BUCKETS]; // under cache_lock
-static volatile LONG64 n_requests, n_hits, n_failures, n_keys, n_xlated; // n_keys: memory cache entries
+static volatile int64_t n_requests, n_hits, n_failures, n_keys, n_xlated; // n_keys: memory cache entries
typedef struct { // one translator child
PlatChild *child; // NULL: not started
@@ -97,18 +96,18 @@ typedef struct { // one translator chi
} Svc;
static struct {
- SRWLOCK lock;
- CONDITION_VARIABLE freed;
+ PlatMutex lock;
+ PlatCond freed;
int limit; // children in use at most (0: svc_runtime_limit)
Svc s[MAX_SVC];
-} S = {SRWLOCK_INIT, CONDITION_VARIABLE_INIT};
+} S = {PLAT_MUTEX_INIT, PLAT_COND_INIT};
static struct { // translations in flight: cache key, or code hash while the
- SRWLOCK lock; // use mask is unknown; one translation per id at a time
- CONDITION_VARIABLE done;
+ PlatMutex lock; // use mask is unknown; one translation per id at a time
+ PlatCond done;
uint8_t id[2 * MAX_SVC][32];
int n;
-} F = {SRWLOCK_INIT, CONDITION_VARIABLE_INIT};
+} F = {PLAT_MUTEX_INIT, PLAT_COND_INIT};
#define PATH_CAP 1024 // bytes of a UTF-8 path
static char cache_dir[PATH_CAP];
@@ -116,7 +115,7 @@ static char xlate_exe[PATH_CAP];
static uint8_t xlate_id[32]; // translator identity (see the keys at the top)
static int host_f64 = 1, host_set; // gfx_shader_set_host
static uint32_t host_sbuf, host_sg[3] = {32, 32, 32};
-static INIT_ONCE paths_once = INIT_ONCE_STATIC_INIT;
+static PlatOnce paths_once = PLAT_ONCE_INIT;
// ---- small helpers
@@ -171,7 +170,7 @@ static void xlate_identity(void) {
} else {
char p[PATH_CAP + 8];
strcpy(p, xlate_exe);
- char *dot = strrchr(p, '.'), *sep = strrchr(p, '\\');
+ char *dot = strrchr(p, '.'), *sep = plat_last_sep(p);
if (dot && dot > sep) *dot = 0;
strcat(p, ".id");
uint64_t size = 0;
@@ -193,16 +192,16 @@ static void xlate_identity(void) {
// 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;
+static void init_paths(void *param) {
+ (void)param;
char exe[PATH_CAP], root[PATH_CAP] = "", exe_dir[PATH_CAP] = "";
if (plat_exe_path(exe, sizeof exe)) {
- char *last = strrchr(exe, '\\');
+ char *last = plat_last_sep(exe);
if (last) snprintf(exe_dir, sizeof exe_dir, "%.*s", (int)(last - exe), exe);
- for (char *cut = strrchr(exe, '\\'); cut; cut = strrchr(exe, '\\')) {
+ for (char *cut = plat_last_sep(exe); cut; cut = plat_last_sep(exe)) {
*cut = 0;
char probe[PATH_CAP + 32];
- snprintf(probe, sizeof probe, "%s\\build\\xlate\\totk_xlate.exe", exe);
+ snprintf(probe, sizeof probe, "%s/build/xlate/totk_xlate" PLAT_EXE_SUFFIX, exe);
if (file_exists(probe)) {
strcpy(root, exe);
break;
@@ -211,18 +210,18 @@ static BOOL CALLBACK init_paths(PINIT_ONCE once, PVOID param, PVOID *ctx) {
}
const char *xlate_cfg = cfg_get("TOTK_XLATE"), *cache_cfg = cfg_get("TOTK_SHADER_CACHE");
char beside[PATH_CAP + 32]; // a packaged folder (tools/package_build.py) keeps the translator next to totk.exe
- if (exe_dir[0]) snprintf(beside, sizeof beside, "%s\\totk_xlate.exe", exe_dir);
+ if (exe_dir[0]) snprintf(beside, sizeof beside, "%s/totk_xlate" PLAT_EXE_SUFFIX, exe_dir);
if (xlate_cfg && *xlate_cfg && strlen(xlate_cfg) < sizeof xlate_exe) strcpy(xlate_exe, xlate_cfg);
else if (exe_dir[0] && file_exists(beside)) strcpy(xlate_exe, beside);
- else if (root[0]) snprintf(xlate_exe, sizeof xlate_exe, "%s\\build\\xlate\\totk_xlate.exe", root);
+ else if (root[0]) snprintf(xlate_exe, sizeof xlate_exe, "%s/build/xlate/totk_xlate" PLAT_EXE_SUFFIX, root);
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);
+ else if (exe_dir[0]) snprintf(cache_dir, sizeof cache_dir, "%s/shader-cache", exe_dir);
xlate_identity();
use_preload_start();
- return TRUE;
+ return;
}
-static void paths(void) { InitOnceExecuteOnce(&paths_once, init_paths, NULL, NULL); }
+static void paths(void) { plat_once(&paths_once, init_paths, NULL); }
// Host answers (vk_caps.c, at device creation, before any translation). Answers other than the defaults (Float64,
// exact storage offsets, 32 lanes) are mixed into the translator identity, so they get their own cache keys.
@@ -267,7 +266,7 @@ static int child_start(Svc *v) {
// One line out, one line in (no newline in the reply). 0 when the pipe broke.
static int child_exchange_raw(Svc *v, const char *line, char *reply, size_t reply_size) {
if (!v->child && !child_start(v)) return 0;
- InterlockedIncrement64(&n_requests);
+ plat_inc64(&n_requests);
size_t n = strlen(line);
if (!plat_child_write(v->child, line, n)) return 0;
if (!plat_child_write(v->child, "\n", 1)) return 0;
@@ -298,11 +297,11 @@ static int child_exchange(Svc *v, const char *line, char *reply, size_t reply_si
// Children allowed at run time: TOTK_XLATE_PROCS, default 2.
static int svc_runtime_limit(void) {
- static volatile LONG n;
+ static volatile int32_t n;
if (!n) {
const char *e = cfg_get("TOTK_XLATE_PROCS");
int v = e && *e ? atoi(e) : 2;
- InterlockedExchange(&n, v < 1 ? 1 : v > MAX_SVC ? MAX_SVC : v);
+ plat_xchg32(&n, v < 1 ? 1 : v > MAX_SVC ? MAX_SVC : v);
}
return (int)n;
}
@@ -315,7 +314,7 @@ static int svc_used(void) { // caller holds S.loc
// A child for one request: an idle running one, else a new slot while fewer than S.limit are in use, else waits.
static Svc *svc_get(void) {
- AcquireSRWLockExclusive(&S.lock);
+ plat_mutex_lock(&S.lock);
if (!S.limit) S.limit = svc_runtime_limit();
Svc *v;
for (;;) {
@@ -328,42 +327,42 @@ static Svc *svc_get(void) {
}
if (!v && empty && svc_used() < S.limit) v = empty;
if (v) break;
- SleepConditionVariableSRW(&S.freed, &S.lock, INFINITE, 0);
+ plat_cond_wait(&S.freed, &S.lock);
}
v->busy = 1;
- ReleaseSRWLockExclusive(&S.lock);
+ plat_mutex_unlock(&S.lock);
return v;
}
// Returns a child; one over the limit (svc_set_limit lowered it) is stopped.
static void svc_put(Svc *v) {
- AcquireSRWLockExclusive(&S.lock);
+ plat_mutex_lock(&S.lock);
int stop = v->child && svc_used() > S.limit;
if (!stop) v->busy = 0;
- ReleaseSRWLockExclusive(&S.lock);
+ plat_mutex_unlock(&S.lock);
if (stop) {
child_stop(v);
- AcquireSRWLockExclusive(&S.lock);
+ plat_mutex_lock(&S.lock);
v->busy = 0;
- ReleaseSRWLockExclusive(&S.lock);
+ plat_mutex_unlock(&S.lock);
}
- WakeAllConditionVariable(&S.freed);
+ plat_cond_broadcast(&S.freed);
}
// Sets the number of children; idle ones over it are stopped now, busy ones when they are returned.
static void svc_set_limit(int n) {
Svc *stop[MAX_SVC];
int k = 0;
- AcquireSRWLockExclusive(&S.lock);
+ plat_mutex_lock(&S.lock);
S.limit = n < 1 ? 1 : n > MAX_SVC ? MAX_SVC : n;
for (int i = 0, used = svc_used(); i < MAX_SVC && used > S.limit; i++)
if (!S.s[i].busy && S.s[i].child) S.s[i].busy = 1, stop[k++] = &S.s[i], used--;
- ReleaseSRWLockExclusive(&S.lock);
+ plat_mutex_unlock(&S.lock);
for (int i = 0; i < k; i++) child_stop(stop[i]);
- AcquireSRWLockExclusive(&S.lock);
+ plat_mutex_lock(&S.lock);
for (int i = 0; i < k; i++) stop[i]->busy = 0;
- ReleaseSRWLockExclusive(&S.lock);
- WakeAllConditionVariable(&S.freed);
+ plat_mutex_unlock(&S.lock);
+ plat_cond_broadcast(&S.freed);
}
// One request on a child; a dead child is restarted once. 0 when the translator is unavailable. The caller has given
@@ -379,35 +378,35 @@ static int svc_request(const char *line, char *reply, size_t reply_size) {
// One translation per id at a time (F); F.id full also waits.
static void flight_enter(const uint8_t id[32]) {
- AcquireSRWLockExclusive(&F.lock);
+ plat_mutex_lock(&F.lock);
for (;;) {
int busy = F.n == 2 * MAX_SVC;
for (int i = 0; i < F.n && !busy; i++) busy = !memcmp(F.id[i], id, 32);
if (!busy) break;
- SleepConditionVariableSRW(&F.done, &F.lock, INFINITE, 0);
+ plat_cond_wait(&F.done, &F.lock);
}
memcpy(F.id[F.n++], id, 32);
- ReleaseSRWLockExclusive(&F.lock);
+ plat_mutex_unlock(&F.lock);
}
static void flight_leave(const uint8_t id[32]) {
- AcquireSRWLockExclusive(&F.lock);
+ plat_mutex_lock(&F.lock);
for (int i = 0; i < F.n; i++)
if (!memcmp(F.id[i], id, 32)) {
memcpy(F.id[i], F.id[--F.n], 32);
break;
}
- ReleaseSRWLockExclusive(&F.lock);
- WakeAllConditionVariable(&F.done);
+ plat_mutex_unlock(&F.lock);
+ plat_cond_broadcast(&F.done);
}
// ---- cache
static GfxShader *mem_find(const uint8_t key[32]) {
- AcquireSRWLockShared(&cache_lock);
+ plat_mutex_lock_shared(&cache_lock);
GfxShader *s = buckets[(key[0] | key[1] << 8) % BUCKETS];
while (s && memcmp(s->key, key, 32)) s = s->next;
- ReleaseSRWLockShared(&cache_lock);
+ plat_mutex_unlock_shared(&cache_lock);
return s;
}
@@ -420,7 +419,7 @@ static void reflect(GfxShader *s);
// Inserts s, or frees it and returns the entry another thread inserted first.
static GfxShader *mem_insert(GfxShader *s) {
- AcquireSRWLockExclusive(&cache_lock);
+ plat_mutex_lock(&cache_lock);
GfxShader **head = &buckets[(s->key[0] | s->key[1] << 8) % BUCKETS], *r = *head;
while (r && memcmp(r->key, s->key, 32)) r = r->next;
if (r) {
@@ -431,7 +430,7 @@ static GfxShader *mem_insert(GfxShader *s) {
*head = s;
n_keys++;
}
- ReleaseSRWLockExclusive(&cache_lock);
+ plat_mutex_unlock(&cache_lock);
return s;
}
@@ -471,20 +470,20 @@ static int spirv_complete(const uint32_t *w, uint64_t n) {
// Reads .spv and .res.json; NULL if either is missing or malformed (the caller translates again and
// overwrites both). iid: the input id, NULL when the caller sets it.
static GfxShader *disk_load(const uint8_t key[32], const uint8_t *iid) {
- static volatile LONG n_bad;
+ static volatile int32_t n_bad;
char hex[65], p[PATH_CAP + 80];
key_hex(key, hex);
uint64_t spv_size = 0, res_size = 0;
- snprintf(p, PATH_CAP + 80, "%s\\%s.res.json", cache_dir, hex);
+ snprintf(p, PATH_CAP + 80, "%s/%s.res.json", cache_dir, hex);
uint8_t *res = read_file(p, &res_size);
if (!res) return NULL;
if (!reflection_valid((const char *)res, res_size)) {
- LONG n = InterlockedIncrement(&n_bad);
- if (n <= 3 || !(n & (n - 1))) rt_log("gfx_shader: %s.res.json is malformed, translating again (%ld so far)", hex, n);
+ int32_t n = plat_inc32(&n_bad);
+ if (n <= 3 || !(n & (n - 1))) rt_log("gfx_shader: %s.res.json is malformed, translating again (%d so far)", hex, n);
free(res);
return NULL;
}
- snprintf(p, PATH_CAP + 80, "%s\\%s.spv", cache_dir, hex);
+ snprintf(p, PATH_CAP + 80, "%s/%s.spv", cache_dir, hex);
uint8_t *spv = read_file(p, &spv_size);
uint32_t magic = 0;
if (spv && spv_size >= 20) memcpy(&magic, spv, 4);
@@ -713,7 +712,7 @@ static void use_name(const uint8_t h[32], uint8_t name[32]) {
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);
+ snprintf(p, PATH_CAP + 80, "%s/%s.use", cache_dir, hex);
}
static void use_path(const uint8_t h[32], char *p) {
@@ -729,7 +728,7 @@ static void use_path(const uint8_t h[32], char *p) {
#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 volatile int32_t 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];
@@ -747,50 +746,52 @@ static int hex_byte(const char *c) {
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);
+typedef struct { int n, bad; } UsePreload;
+
+// One <64 hex digits>.use file of the cache directory into the table.
+static int use_preload_file(const char *name, void *ctx) {
+ UsePreload *st = ctx;
+ char p[PATH_CAP + 80];
+ UseFile e = {0};
+ int ok = strlen(name) == 68;
+ for (int i = 0; ok && i < 32; i++) {
+ int v = hex_byte(name + 2 * i);
+ ok = v >= 0, e.name[i] = (uint8_t)v;
}
- InterlockedExchange(&use_files_ready, 1);
+ uint64_t len = 0;
+ uint32_t *u = NULL;
+ if (ok) {
+ snprintf(p, sizeof p, "%s/%s", cache_dir, name);
+ 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, st->n++;
+ } else if (!ok) {
+ st->bad++;
+ }
+ free(u);
+ return 1;
+}
+
+static void use_preload(void *arg) {
+ (void)arg;
+ plat_thread_priority(PLAT_PRIO_BELOW_NORMAL);
+ uint64_t t0 = plat_now_ns();
+ UsePreload st = {0};
+ plat_file_list(cache_dir, ".use", use_preload_file, &st);
+ int n = st.n, bad = st.bad;
+ plat_xchg32(&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;
+ return;
}
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);
+ plat_thread_spawn(use_preload, NULL, "use preload", 0);
}
static UseEnt **use_slot(const uint8_t h[32]) { // caller holds cache_lock
@@ -809,15 +810,15 @@ static void use_put(const uint8_t h[32], int known, const UseMask *m) {
// 1 with *m when the use mask of h is known (memory, then .use; a disk miss is remembered).
static int use_find(const uint8_t h[32], UseMask *m) {
- AcquireSRWLockShared(&cache_lock);
+ plat_mutex_lock_shared(&cache_lock);
UseEnt *e = *use_slot(h);
int seen = e != NULL, known = e && e->known;
if (known) *m = e->m;
- ReleaseSRWLockShared(&cache_lock);
+ plat_mutex_unlock_shared(&cache_lock);
if (seen) return known;
uint8_t name[32];
use_name(h, name);
- if (InterlockedCompareExchange(&use_files_ready, 0, 0)) { // complete: a name not in it has no file
+ if (plat_cas32(&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 {
@@ -829,9 +830,9 @@ static int use_find(const uint8_t h[32], UseMask *m) {
if (known) m->slots = f[2], m->attrs = f[3], m->fouts = f[4];
free(f);
}
- AcquireSRWLockExclusive(&cache_lock);
+ plat_mutex_lock(&cache_lock);
use_put(h, known, m);
- ReleaseSRWLockExclusive(&cache_lock);
+ plat_mutex_unlock(&cache_lock);
return known;
}
@@ -840,9 +841,9 @@ static void use_store(const uint8_t h[32], const UseMask *m) {
use_path(h, p);
uint32_t f[5] = {USE_MAGIC, USE_VERSION, m->slots, m->attrs, m->fouts};
plat_file_write_atomic(p, f, sizeof f);
- AcquireSRWLockExclusive(&cache_lock);
+ plat_mutex_lock(&cache_lock);
use_put(h, 1, m);
- ReleaseSRWLockExclusive(&cache_lock);
+ plat_mutex_unlock(&cache_lock);
}
static void put_u32(uint8_t **p, uint32_t v) { memcpy(*p, &v, 4), *p += 4; }
@@ -997,7 +998,7 @@ uint64_t gfx_shader_cache_hits(void) { return (uint64_t)n_hits; } // gets an
// Drops the memory cache (the probe uses it to prove the disk cache). Pointers handed out before are invalid.
void gfx_shader_drop_memory(void) {
- AcquireSRWLockExclusive(&cache_lock);
+ plat_mutex_lock(&cache_lock);
for (int i = 0; i < BUCKETS; i++) {
for (GfxShader *s = buckets[i], *n; s; s = n) {
n = s->next;
@@ -1009,16 +1010,16 @@ void gfx_shader_drop_memory(void) {
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);
+ plat_xchg32(&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);
+ plat_mutex_unlock(&cache_lock);
}
static const GfxShader *fail(const char *why, const char *hex) {
- InterlockedIncrement64(&n_failures);
+ plat_inc64(&n_failures);
rt_log("gfx_shader: translation failed (%s): %s", hex ? hex : "-", why);
return NULL;
}
@@ -1064,10 +1065,10 @@ static const GfxShader *get_async_one(Req r, int stage, const uint8_t *control,
static const GfxShader *get_one(const Req *r, int stage, const uint8_t *control, const uint8_t *code,
const GfxShaderAnswers *a) {
GfxShader *s = mem_find(r->key);
- if (s) return InterlockedIncrement64(&n_hits), s;
+ if (s) return plat_inc64(&n_hits), s;
paths();
if (!cache_dir[0]) return fail("no cache directory", NULL);
- if (r->final && (s = disk_load(r->key, r->iid))) return InterlockedIncrement64(&n_hits), mem_insert(s);
+ if (r->final && (s = disk_load(r->key, r->iid))) return plat_inc64(&n_hits), mem_insert(s);
return translate(r, stage, control, code, a);
}
@@ -1097,7 +1098,7 @@ typedef struct {
static void in_path(const uint8_t iid[32], char *p) {
char hex[65];
key_hex(iid, hex);
- snprintf(p, PATH_CAP + 80, "%s\\in\\%s.in", cache_dir, hex);
+ snprintf(p, PATH_CAP + 80, "%s/in/%s.in", cache_dir, hex);
}
// Stores the inputs of a translation unless they are stored already; the file appears whole (written, then renamed).
@@ -1116,7 +1117,7 @@ static void in_store(const uint8_t iid[32], int stage, const uint8_t *control, c
memcpy(o, control, CONTROL_SIZE), o += CONTROL_SIZE;
memcpy(o, code, r->length), o += r->length;
if (r->c1_len) memcpy(o, r->c1, r->c1_len);
- snprintf(tmp, PATH_CAP + 120, "%s\\in", cache_dir);
+ snprintf(tmp, PATH_CAP + 120, "%s/in", cache_dir);
mkdir_p(tmp);
plat_file_write_atomic(p, buf, n);
free(buf);
@@ -1154,16 +1155,16 @@ static const GfxShader *translate_one(const Req *r, int stage, const uint8_t *co
// Another thread may have translated it while this one waited in flight_enter.
const uint8_t *look = r->final ? key : NULL, *look_iid = r->iid;
if (!r->final && use_find(r->code_hash, &m)) build_key(r->code_hash, a, &m, mk, miid), look = mk, look_iid = miid;
- if (look && (s = mem_find(look))) return InterlockedIncrement64(&n_hits), s;
- if (look && (s = disk_load(look, look_iid))) return InterlockedIncrement64(&n_hits), mem_insert(s);
+ if (look && (s = mem_find(look))) return plat_inc64(&n_hits), s;
+ if (look && (s = disk_load(look, look_iid))) return plat_inc64(&n_hits), mem_insert(s);
uint64_t skip = stage == GFX_STAGE_CS ? 0 : CODE_SKIP;
char hex[65], code_path[PATH_CAP + 80], spv_path[PATH_CAP + 80], res_path[PATH_CAP + 80];
key_hex(key, hex);
mkdir_p(cache_dir);
- snprintf(code_path, PATH_CAP + 80, "%s\\%s.code", cache_dir, hex);
- snprintf(spv_path, PATH_CAP + 80, "%s\\%s.spv", cache_dir, hex);
- snprintf(res_path, PATH_CAP + 80, "%s\\%s.res.json", cache_dir, hex);
+ snprintf(code_path, PATH_CAP + 80, "%s/%s.code", cache_dir, hex);
+ snprintf(spv_path, PATH_CAP + 80, "%s/%s.spv", cache_dir, hex);
+ snprintf(res_path, PATH_CAP + 80, "%s/%s.res.json", cache_dir, hex);
if (!write_file(code_path, code + skip, (size_t)(r->length - skip))) return fail("cannot write the code file", hex);
size_t cap = 2 * PATH_CAP * 6 + 2 * r->c1_len + 8192;
char *req = malloc(cap), reply[LINE_MAX_LEN];
@@ -1184,22 +1185,22 @@ static const GfxShader *translate_one(const Req *r, int stage, const uint8_t *co
build_key(r->code_hash, a, &m, mk, miid);
char mhex[65], to[PATH_CAP + 80];
key_hex(mk, mhex);
- snprintf(to, PATH_CAP + 80, "%s\\%s.spv", cache_dir, mhex);
+ snprintf(to, PATH_CAP + 80, "%s/%s.spv", cache_dir, mhex);
int moved = plat_file_replace(spv_path, to);
- snprintf(to, PATH_CAP + 80, "%s\\%s.res.json", cache_dir, mhex);
+ snprintf(to, PATH_CAP + 80, "%s/%s.res.json", cache_dir, mhex);
moved = moved && plat_file_replace(res_path, to);
if (moved) {
use_store(r->code_hash, &m);
} else { // keep the mask for this run only
plat_file_delete(spv_path), plat_file_delete(res_path), rt_log("gfx_shader: cannot rename %s", hex);
- AcquireSRWLockExclusive(&cache_lock);
+ plat_mutex_lock(&cache_lock);
use_put(r->code_hash, 1, &m);
- ReleaseSRWLockExclusive(&cache_lock);
+ plat_mutex_unlock(&cache_lock);
}
memcpy(s->key, mk, 32), memcpy(s->iid, miid, 32);
}
in_store(s->iid, stage, control, code, r, a);
- InterlockedIncrement64(&n_xlated);
+ plat_inc64(&n_xlated);
return mem_insert(s);
}
@@ -1235,14 +1236,14 @@ typedef struct XWait {
} XWait;
static struct {
- SRWLOCK lock;
- CONDITION_VARIABLE work, done; // done: a job finished (gfx_shader_await)
+ PlatMutex lock;
+ PlatCond work, done; // done: a job finished (gfx_shader_await)
XJob *head, *tail;
XWait *waits[256];
int started; // 1 running, -1 no thread could be created
-} X = {SRWLOCK_INIT, CONDITION_VARIABLE_INIT, CONDITION_VARIABLE_INIT};
+} X = {PLAT_MUTEX_INIT, PLAT_COND_INIT, PLAT_COND_INIT};
-static volatile LONG64 n_async;
+static volatile int64_t n_async;
// Caller holds X.lock.
static XWait **wait_slot(const uint8_t key[32]) {
@@ -1251,25 +1252,25 @@ static XWait **wait_slot(const uint8_t key[32]) {
return w;
}
-static DWORD WINAPI xlate_worker(void *arg) {
+static void xlate_worker(void *arg) {
(void)arg;
for (;;) {
- AcquireSRWLockExclusive(&X.lock);
- while (!X.head) SleepConditionVariableSRW(&X.work, &X.lock, INFINITE, 0);
+ plat_mutex_lock(&X.lock);
+ while (!X.head) plat_cond_wait(&X.work, &X.lock);
XJob *j = X.head;
if (!(X.head = j->next)) X.tail = NULL;
- ReleaseSRWLockExclusive(&X.lock);
+ plat_mutex_unlock(&X.lock);
const GfxShader *s = translate(&j->r, j->stage, j->control, j->code, &j->a);
- AcquireSRWLockExclusive(&X.lock);
+ plat_mutex_lock(&X.lock);
XWait **w = wait_slot(j->r.key), *e = *w;
if (e && s) *w = e->next, free(e);
else if (e) e->failed = 1;
- ReleaseSRWLockExclusive(&X.lock);
- WakeAllConditionVariable(&X.done);
- if (s) InterlockedIncrement64(&n_async);
+ plat_mutex_unlock(&X.lock);
+ plat_cond_broadcast(&X.done);
+ if (s) plat_inc64(&n_async);
free(j->control), free(j->code), free(j);
}
- return 0;
+ return;
}
const GfxShader *gfx_shader_get_async(int stage, const uint8_t *control, const uint8_t *code, uint64_t code_size,
const GfxShaderAnswers *a, int *pending) {
@@ -1284,25 +1285,24 @@ const GfxShader *gfx_shader_get_async(int stage, const uint8_t *control, const u
static const GfxShader *get_async_one(Req r, int stage, const uint8_t *control, const uint8_t *code,
const GfxShaderAnswers *a, int *pending) {
GfxShader *s = mem_find(r.key);
- if (s) return InterlockedIncrement64(&n_hits), s;
- AcquireSRWLockShared(&X.lock);
+ if (s) return plat_inc64(&n_hits), s;
+ plat_mutex_lock_shared(&X.lock);
XWait *w = *wait_slot(r.key);
int failed = w && w->failed;
- ReleaseSRWLockShared(&X.lock);
+ plat_mutex_unlock_shared(&X.lock);
if (w) return *pending = !failed, NULL;
paths();
if (!cache_dir[0]) return fail("no cache directory", NULL);
- if (r.final && (s = disk_load(r.key, r.iid))) return InterlockedIncrement64(&n_hits), mem_insert(s);
+ if (r.final && (s = disk_load(r.key, r.iid))) return plat_inc64(&n_hits), mem_insert(s);
XJob *j = calloc(1, sizeof *j);
XWait *e = calloc(1, sizeof *e);
uint8_t *ctl = malloc(CONTROL_SIZE), *cd = malloc(r.length);
- AcquireSRWLockExclusive(&X.lock);
+ plat_mutex_lock(&X.lock);
if (!X.started) {
int n = 0;
for (int i = 0; i < svc_runtime_limit(); i++) {
- HANDLE t = CreateThread(NULL, 0, xlate_worker, NULL, 0, NULL);
- if (t) CloseHandle(t), n++;
+ if (plat_thread_spawn(xlate_worker, NULL, "xlate", 0)) n++;
}
X.started = n ? 1 : -1;
if (n) rt_log("gfx_shader: graphics shaders are translated by %d background workers (TOTK_XLATE_PROCS)", n);
@@ -1320,9 +1320,9 @@ static const GfxShader *get_async_one(Req r, int stage, const uint8_t *control,
if (X.tail) X.tail->next = j;
else X.head = j;
X.tail = j;
- WakeConditionVariable(&X.work);
+ plat_cond_signal(&X.work);
}
- ReleaseSRWLockExclusive(&X.lock);
+ plat_mutex_unlock(&X.lock);
if (queued) return *pending = 1, NULL;
free(j), free(e), free(ctl), free(cd);
if (raced) return *pending = 1, NULL;
@@ -1336,7 +1336,7 @@ const GfxShader *gfx_shader_await(int stage, const uint8_t *control, const uint8
Req r;
if (!pending || !req_init(&r, stage, control, code, code_size, NULL, a)) return s;
uint64_t p0 = perf_begin(); // the wait counts as the render thread's translation time
- AcquireSRWLockExclusive(&X.lock);
+ plat_mutex_lock(&X.lock);
for (XJob *j = X.head, *prev = NULL; j; prev = j, j = j->next)
if (!memcmp(j->r.key, r.key, 32)) { // queued: to the front, the worker takes it next
if (prev) {
@@ -1347,8 +1347,8 @@ const GfxShader *gfx_shader_await(int stage, const uint8_t *control, const uint8
break;
}
XWait *w;
- while ((w = *wait_slot(r.key)) && !w->failed) SleepConditionVariableSRW(&X.done, &X.lock, INFINITE, 0);
- ReleaseSRWLockExclusive(&X.lock);
+ while ((w = *wait_slot(r.key)) && !w->failed) plat_cond_wait(&X.done, &X.lock);
+ plat_mutex_unlock(&X.lock);
perf_end(PERF_XLATE, p0);
if (w) return NULL; // failed: logged by the worker
s = gfx_shader_get_async(stage, control, code, code_size, a, &pending);
@@ -1407,28 +1407,28 @@ typedef struct {
const GfxShader **out;
uint8_t (*other)[32]; // other stored inputs: translated when not cached, not loaded
size_t n_other;
- volatile LONG64 next, done;
- volatile LONG missing, other_cached;
+ volatile int64_t next, done;
+ volatile int32_t missing, other_cached;
} Warm;
-static DWORD WINAPI warm_worker(void *arg) {
+static void warm_worker(void *arg) {
Warm *w = arg;
for (;;) {
- size_t i = (size_t)InterlockedIncrement64(&w->next) - 1;
- if (i >= w->n_named + w->n_other) return 0;
+ size_t i = (size_t)plat_inc64(&w->next) - 1;
+ if (i >= w->n_named + w->n_other) return;
if (i < w->n_named) {
- if (!(w->out[i] = gfx_shader_load_input(w->named[i], 1))) InterlockedIncrement(&w->missing);
+ if (!(w->out[i] = gfx_shader_load_input(w->named[i], 1))) plat_inc32(&w->missing);
} else {
const uint8_t *iid = w->other[i - w->n_named];
uint8_t key[32];
char hex[65], p[PATH_CAP + 80];
key_of_input(iid, key);
key_hex(key, hex);
- snprintf(p, PATH_CAP + 80, "%s\\%s.spv", cache_dir, hex);
- if (file_exists(p)) InterlockedIncrement(&w->other_cached);
+ snprintf(p, PATH_CAP + 80, "%s/%s.spv", cache_dir, hex);
+ if (file_exists(p)) plat_inc32(&w->other_cached);
else gfx_shader_load_input(iid, 1);
}
- InterlockedIncrement64(&w->done);
+ plat_inc64(&w->done);
}
}
@@ -1461,7 +1461,7 @@ static int in_collect(const char *file, void *ctx) {
// The stored inputs not in named (sorted): ids parsed from the in/ file names.
static size_t other_inputs(const uint8_t (*named)[32], size_t n_named, uint8_t (**out)[32]) {
char dir[PATH_CAP + 80];
- snprintf(dir, sizeof dir, "%s\\in", cache_dir);
+ snprintf(dir, sizeof dir, "%s/in", cache_dir);
InList l = {.named = named, .n_named = n_named};
plat_file_list(dir, ".in", in_collect, &l);
return *out = l.v, l.n;
@@ -1469,7 +1469,7 @@ static size_t other_inputs(const uint8_t (*named)[32], size_t n_named, uint8_t (
void gfx_shader_prewarm(const uint8_t (*iids)[32], size_t n, const GfxShader **out, int threads, int procs) {
paths();
- double t0 = (double)GetTickCount64();
+ double t0 = (double)plat_now_ms();
memset(out, 0, n * sizeof *out);
if (!cache_dir[0]) return;
uint64_t xl0 = (uint64_t)n_xlated, fail0 = (uint64_t)n_failures;
@@ -1482,23 +1482,20 @@ void gfx_shader_prewarm(const uint8_t (*iids)[32], size_t n, const GfxShader **o
threads = threads < 1 ? 1 : threads > 64 ? 64 : threads;
if ((size_t)threads > total) threads = (int)(total ? total : 1);
svc_set_limit(procs);
- HANDLE t[64];
+ PlatThread *t[64];
int k = 0;
for (int i = 0; i < threads; i++)
- if ((t[k] = CreateThread(NULL, 0, warm_worker, &w, 0, NULL))) k++;
+ if ((t[k] = plat_thread_new(warm_worker, &w, "shader prewarm"))) k++;
if (!k) warm_worker(&w);
- for (int m = 0; k && m < k; m += MAXIMUM_WAIT_OBJECTS) {
- int c = k - m < MAXIMUM_WAIT_OBJECTS ? k - m : MAXIMUM_WAIT_OBJECTS;
- while (WaitForMultipleObjects((DWORD)c, t + m, TRUE, 2000) == WAIT_TIMEOUT)
+ for (int m = 0; m < k; m++)
+ while (!plat_thread_join_ns(t[m], 2000000000u))
rt_log("gfx_shader: prewarm: %lld of %zu shaders, %llu translated, %.0f ms", (long long)w.done, total,
- (unsigned long long)((uint64_t)n_xlated - xl0), (double)GetTickCount64() - t0);
- }
- for (int i = 0; i < k; i++) CloseHandle(t[i]);
+ (unsigned long long)((uint64_t)n_xlated - xl0), (double)plat_now_ms() - t0);
svc_set_limit(svc_runtime_limit());
rt_log("gfx_shader: prewarm: %zu shaders named by pipeline keys (%ld not available), %zu other stored inputs (%ld "
"cached); %llu translated, %llu failed, with up to %d translators and %d threads, in %.0f ms", w.n_named,
(long)w.missing, w.n_other, (long)w.other_cached, (unsigned long long)((uint64_t)n_xlated - xl0),
- (unsigned long long)((uint64_t)n_failures - fail0), procs, k, (double)GetTickCount64() - t0);
+ (unsigned long long)((uint64_t)n_failures - fail0), procs, k, (double)plat_now_ms() - t0);
free(w.other);
}
const uint32_t *gfx_shader_spirv(const GfxShader *s, uint64_t *words) {
@@ -1555,13 +1552,13 @@ typedef struct { uint64_t *v; uint32_t cap, n; } Set64; // open addressing, 0
typedef struct { uint8_t *p; size_t n, cap; } Bytes;
static struct {
- SRWLOCK lock;
- volatile LONG on;
+ PlatMutex lock;
+ volatile int32_t on;
char ans_path[PATH_CAP], prg_path[PATH_CAP];
Set64 pairs, progs; // input ids, code hashes seen (in the files or this run)
Bytes ans, prg; // pending
uint64_t pairs_new; // added this run
-} RC = {SRWLOCK_INIT};
+} RC = {PLAT_MUTEX_INIT};
static int set_add(Set64 *s, uint64_t h) { // 1 if new
h = h ? h : 1;
@@ -1661,7 +1658,7 @@ static void truncate_to(const char *path, size_t size) {
void gfx_shader_record_at(const char *keys_path) {
if (!keys_path || !keys_path[0] || RC.on) return;
- AcquireSRWLockExclusive(&RC.lock);
+ plat_mutex_lock(&RC.lock);
const char *slash = strrchr(keys_path, '\\'), *fs = strrchr(keys_path, '/');
if (fs > slash) slash = fs;
int dir = slash ? (int)(slash + 1 - keys_path) : 0;
@@ -1681,8 +1678,8 @@ void gfx_shader_record_at(const char *keys_path) {
free(b);
rt_log("gfx_shader: recording answers met in play to %s (%u records, %u programs recorded before)", RC.ans_path,
RC.pairs.n, RC.progs.n);
- InterlockedExchange(&RC.on, 1);
- ReleaseSRWLockExclusive(&RC.lock);
+ plat_xchg32(&RC.on, 1);
+ plat_mutex_unlock(&RC.lock);
}
// A get resolved (r, a) to s: queue the record when its input id is new, and its program when new. Cost: a set
@@ -1691,7 +1688,7 @@ static void ans_note(const Req *r, int stage, const uint8_t *control, const uint
const GfxShader *s) {
if (!RC.on) return;
uint32_t st = (uint32_t)stage;
- AcquireSRWLockExclusive(&RC.lock);
+ plat_mutex_lock(&RC.lock);
if (set_add(&RC.pairs, hash8(s->iid))) {
uint8_t rec[ANS_REC] = {0};
memcpy(rec, &st, 4), memcpy(rec + 4, r->code_hash, 32), memcpy(rec + 36, s->iid, 32);
@@ -1705,7 +1702,7 @@ static void ans_note(const Req *r, int stage, const uint8_t *control, const uint
bytes_put(&RC.ans, rec, sizeof rec);
RC.pairs_new++;
}
- ReleaseSRWLockExclusive(&RC.lock);
+ plat_mutex_unlock(&RC.lock);
}
static int rec_append(const char *path, const char *magic, uint32_t version, uint32_t rec_size, const Bytes *b) {
@@ -1722,23 +1719,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
+static PlatMutex rec_io = PLAT_MUTEX_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();
+ static volatile int64_t last;
+ int64_t now = (int64_t)plat_now_ms();
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);
+ plat_mutex_lock(&rec_io);
+ plat_mutex_lock(&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);
+ plat_mutex_unlock(&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(&rec_io);
+ plat_mutex_unlock(&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);
@@ -1755,7 +1752,7 @@ typedef struct {
const uint8_t *ans;
size_t n_ans;
uint8_t (*iid)[32]; // per answers record: the resolved input id, zero on failure
- volatile LONG64 next, done, missing, failed;
+ volatile int64_t next, done, missing, failed;
} Prep;
static int prg_cmp(const void *x, const void *y) { return memcmp(((const PrgIdx *)x)->rec + 12, ((const PrgIdx *)y)->rec + 12, 32); }
@@ -1765,17 +1762,17 @@ static void prg_collect(const uint8_t *rec, void *ctx) {
l->idx[l->n++].rec = rec;
}
-static DWORD WINAPI prep_worker(void *arg) {
+static void prep_worker(void *arg) {
Prep *p = arg;
for (;;) {
- size_t i = (size_t)InterlockedIncrement64(&p->next) - 1;
- if (i >= p->n_ans) return 0;
+ size_t i = (size_t)plat_inc64(&p->next) - 1;
+ if (i >= p->n_ans) return;
const uint8_t *r = p->ans + i * ANS_REC;
uint8_t key_rec[12 + 32];
memcpy(key_rec + 12, r + 4, 32);
PrgIdx k = {key_rec}, *e = bsearch(&k, p->progs.idx, p->progs.n, sizeof k, prg_cmp);
if (!e) {
- InterlockedIncrement64(&p->missing), InterlockedIncrement64(&p->done);
+ plat_inc64(&p->missing), plat_inc64(&p->done);
continue;
}
uint32_t st, len;
@@ -1784,9 +1781,9 @@ static DWORD WINAPI prep_worker(void *arg) {
GfxShaderAnswers a;
unpack_answers(r + 68, &a);
const GfxShader *s = gfx_shader_get((int)st, control, code, len, c1, &a);
- if (!s) InterlockedIncrement64(&p->failed);
+ if (!s) plat_inc64(&p->failed);
else if (p->iid) memcpy(p->iid[i], s->iid, 32);
- InterlockedIncrement64(&p->done);
+ plat_inc64(&p->done);
}
}
@@ -1796,7 +1793,7 @@ static DWORD WINAPI prep_worker(void *arg) {
static long long prepare_files(const char *ans_path, const char *prg_path, int threads, int procs, const char *what,
uint8_t **ans_out, uint8_t (**iid_out)[32]) {
paths();
- double t0 = (double)GetTickCount64();
+ double t0 = (double)plat_now_ms();
uint8_t *ab = NULL, *pb = NULL;
size_t an = 0, pn = 0;
if (!rec_read(ans_path, "TOTKSANS", ANS_VERSION, ANS_REC, &ab, &an) ||
@@ -1813,22 +1810,19 @@ static long long prepare_files(const char *ans_path, const char *prg_path, int t
uint64_t xl0 = (uint64_t)n_xlated;
threads = threads < 1 ? 1 : threads > 64 ? 64 : threads;
svc_set_limit(procs);
- HANDLE t[64];
+ PlatThread *t[64];
int k = 0;
for (int i = 0; i < threads; i++)
- if ((t[k] = CreateThread(NULL, 0, prep_worker, &p, 0, NULL))) k++;
+ if ((t[k] = plat_thread_new(prep_worker, &p, "shader prepare"))) k++;
if (!k) prep_worker(&p);
- for (int m = 0; k && m < k; m += MAXIMUM_WAIT_OBJECTS) {
- int c = k - m < MAXIMUM_WAIT_OBJECTS ? k - m : MAXIMUM_WAIT_OBJECTS;
- while (WaitForMultipleObjects((DWORD)c, t + m, TRUE, 5000) == WAIT_TIMEOUT)
+ for (int m = 0; m < k; m++)
+ while (!plat_thread_join_ns(t[m], 5000000000u))
rt_log("gfx_shader: prepare: %lld of %zu pairs, %llu translated, %.0f ms", (long long)p.done, p.n_ans,
- (unsigned long long)((uint64_t)n_xlated - xl0), (double)GetTickCount64() - t0);
- }
- for (int i = 0; i < k; i++) CloseHandle(t[i]);
+ (unsigned long long)((uint64_t)n_xlated - xl0), (double)plat_now_ms() - t0);
svc_set_limit(svc_runtime_limit());
rt_log("gfx_shader: prepare: %zu %s of %zu programs: %llu translated, %lld failed, %lld without a program, the rest "
"cached; %.0f ms", p.n_ans, what, p.progs.n, (unsigned long long)((uint64_t)n_xlated - xl0), (long long)p.failed,
- (long long)p.missing, (double)GetTickCount64() - t0);
+ (long long)p.missing, (double)plat_now_ms() - t0);
free(p.progs.idx), free(pb);
if (ans_out) *ans_out = ab;
else free(ab);
@@ -1852,10 +1846,10 @@ void gfx_shader_prepare_recorded(int threads, int procs) {
int gfx_shader_prepare_seed(const char *dir, int threads, int procs) {
char ans[PATH_CAP + 32], prg[PATH_CAP + 32], out[PATH_CAP + 32];
- snprintf(ans, sizeof ans, "%s\\seed-answers.bin", dir);
- snprintf(prg, sizeof prg, "%s\\seed-programs.bin", dir);
- snprintf(out, sizeof out, "%s\\shader-prepared.bin", dir);
- double t0 = (double)GetTickCount64();
+ snprintf(ans, sizeof ans, "%s/seed-answers.bin", dir);
+ snprintf(prg, sizeof prg, "%s/seed-programs.bin", dir);
+ snprintf(out, sizeof out, "%s/shader-prepared.bin", dir);
+ double t0 = (double)plat_now_ms();
uint64_t fail0 = (uint64_t)n_failures;
uint8_t *ab = NULL, (*iid)[32] = NULL;
long long n = prepare_files(ans, prg, threads, procs, "seed variants", &ab, &iid);
@@ -1884,7 +1878,7 @@ int gfx_shader_prepare_seed(const char *dir, int threads, int procs) {
snprintf(msg, sizeof msg, "gfx_shader: prepared %zu shader variants (vertex %zu, fragment %zu, compute %zu; title %zu, "
"warm-up %zu, other %zu) from %lld seed records, %llu translations failed, in %.0f ms; %s %s", u,
stages[GFX_STAGE_VS], stages[GFX_STAGE_FS], stages[GFX_STAGE_CS], by[0], by[1], by[2], n,
- (unsigned long long)((uint64_t)n_failures - fail0), (double)GetTickCount64() - t0,
+ (unsigned long long)((uint64_t)n_failures - fail0), (double)plat_now_ms() - t0,
ok ? "wrote" : "cannot write", out);
rt_log("%s", msg);
printf("%s\n", msg);
@@ -1902,9 +1896,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(&rec_io); // no append meanwhile
+ plat_mutex_lock(&rec_io); // no append meanwhile
rec_read(RC.ans_path, "TOTKSANS", ANS_VERSION, ANS_REC, &ab, &an);
- ReleaseSRWLockExclusive(&rec_io);
+ plat_mutex_unlock(&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_shader_probe.c b/runtime/gfx_shader_probe.c
index d4553c9c..79f4f69f 100644
--- a/runtime/gfx_shader_probe.c
+++ b/runtime/gfx_shader_probe.c
@@ -6,9 +6,9 @@
#include
#include
#include
-#include
#include "gfx.h"
+#include "platform/platform.h"
int gfx_shader_raw_request(const char *line, char *reply, size_t reply_size);
uint64_t gfx_shader_requests(void);
@@ -50,14 +50,14 @@ static int hexval(char c) { return c <= '9' ? c - '0' : (c | 32) - 'a' + 10; }
// The repository root: the nearest parent of totk.exe holding build/v121/shaders/jobs.json.
static int find_root(char *root, size_t n) {
- char exe[MAX_PATH * 2];
- DWORD len = GetModuleFileNameA(NULL, exe, sizeof exe);
+ char exe[PLAT_PATH_MAX * 2];
+ int len = plat_exe_path(exe, sizeof exe);
if (!len || len >= sizeof exe) return 0;
- for (char *cut = strrchr(exe, '\\'); cut; cut = strrchr(exe, '\\')) {
+ for (char *cut = plat_last_sep(exe); cut; cut = plat_last_sep(exe)) {
*cut = 0;
- char p[MAX_PATH * 2 + 40];
- snprintf(p, sizeof p, "%s\\build\\v121\\shaders\\jobs.json", exe);
- if (GetFileAttributesA(p) != INVALID_FILE_ATTRIBUTES) return snprintf(root, n, "%s", exe) < (int)n;
+ char p[PLAT_PATH_MAX * 2 + 40];
+ snprintf(p, sizeof p, "%s/build/v121/shaders/jobs.json", exe);
+ if (plat_file_exists(p)) return snprintf(root, n, "%s", exe) < (int)n;
}
return 0;
}
@@ -95,15 +95,15 @@ static int pick_entries(const char *jobs, Entry *out) {
return got;
}
-static char root[MAX_PATH * 2];
+static char root[PLAT_PATH_MAX * 2];
// Builds the program the game would hand NVN for a census entry and translates it.
static const GfxShader *translate(const Entry *e, int *err) {
- char path[MAX_PATH * 2 + 64];
+ char path[PLAT_PATH_MAX * 2 + 64];
uint8_t c1[8192], control[0x880] = {0};
if (e->c1_len > sizeof c1) return *err = 1, NULL;
for (size_t b = 0; b < e->c1_len; b++) c1[b] = (uint8_t)(hexval(e->c1[b * 2]) << 4 | hexval(e->c1[b * 2 + 1]));
- snprintf(path, sizeof path, "%s\\build\\v121\\shaders\\code\\%s.code", root, e->key);
+ snprintf(path, sizeof path, "%s/build/v121/shaders/code/%s.code", root, e->key);
size_t body_size;
char *body = slurp(path, &body_size);
if (!body) return *err = 1, NULL;
@@ -120,28 +120,25 @@ static const GfxShader *translate(const Entry *e, int *err) {
return s;
}
+static int remove_entry(const char *name, int kind, uint64_t size, void *dir) {
+ (void)size;
+ char f[PLAT_PATH_MAX * 2];
+ snprintf(f, sizeof f, "%s/%s", (const char *)dir, name);
+ if (kind == PLAT_FS_FILE) plat_fs_unlink(f);
+ return 1;
+}
+
static void remove_dir(const char *dir) {
- char pat[MAX_PATH * 2];
- snprintf(pat, sizeof pat, "%s\\*", dir);
- WIN32_FIND_DATAA fd;
- HANDLE h = FindFirstFileA(pat, &fd);
- if (h != INVALID_HANDLE_VALUE) {
- do {
- char f[MAX_PATH * 2];
- snprintf(f, sizeof f, "%s\\%s", dir, fd.cFileName);
- if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) DeleteFileA(f);
- } while (FindNextFileA(h, &fd));
- FindClose(h);
- }
- RemoveDirectoryA(dir);
+ plat_fs_list(dir, remove_entry, (void *)dir);
+ plat_fs_rmdir(dir);
}
#define FAIL_IF(cond, ...) do { if (cond) { printf("probe-shader: FAILED: " __VA_ARGS__); printf("\n"); remove_dir(dir); return 1; } } while (0)
int probe_shader(void) {
- char path[MAX_PATH * 2 + 64];
+ char path[PLAT_PATH_MAX * 2 + 64];
CHECK(find_root(root, sizeof root), "build/v121/shaders/jobs.json not found (run tools/shader_census.py)");
- snprintf(path, sizeof path, "%s\\build\\v121\\shaders\\jobs.json", root);
+ snprintf(path, sizeof path, "%s/build/v121/shaders/jobs.json", root);
size_t jobs_size;
char *jobs = slurp(path, &jobs_size);
CHECK(jobs, "cannot read %s", path);
@@ -149,9 +146,11 @@ int probe_shader(void) {
int count = pick_entries(jobs, entries);
CHECK(count == WANT, "only %d census shaders usable", count);
- char dir[MAX_PATH];
- snprintf(dir, sizeof dir, "%s\\totk-shader-probe-%lu", getenv("TEMP") ? getenv("TEMP") : ".", GetCurrentProcessId());
- CHECK(_putenv_s("TOTK_SHADER_CACHE", dir) == 0, "cannot set TOTK_SHADER_CACHE");
+ char dir[PLAT_PATH_MAX];
+ char tmp[PLAT_PATH_MAX];
+ if (!plat_temp_dir(tmp, sizeof tmp)) strcpy(tmp, ".");
+ snprintf(dir, sizeof dir, "%s/totk-shader-probe-%u", tmp, plat_process_id());
+ CHECK(plat_setenv("TOTK_SHADER_CACHE", dir), "cannot set TOTK_SHADER_CACHE");
// pass 1: translate through the service
const GfxShader *first[WANT];
@@ -198,10 +197,10 @@ int probe_shader(void) {
FAIL_IF(strncmp(reply, "fail ", 5), "malformed line answered with \"%s\"", reply);
FAIL_IF(!gfx_shader_raw_request("{\"job\":{\"stage\":\"ps\"},\"out\":\"x\"}", reply, sizeof reply) || strncmp(reply, "fail ", 5),
"job without key not rejected");
- char dir_json[MAX_PATH * 2], line[MAX_PATH * 4 + 1024], *o = dir_json;
+ char dir_json[PLAT_PATH_MAX * 2], line[PLAT_PATH_MAX * 4 + 1024], *o = dir_json;
for (const char *s = dir; *s; s++) { if (*s == '\\') *o++ = '\\'; *o++ = *s; }
*o = 0;
- char esc_root[MAX_PATH * 2];
+ char esc_root[PLAT_PATH_MAX * 2];
o = esc_root;
for (const char *s = root; *s; s++) { if (*s == '\\') *o++ = '\\'; *o++ = *s; }
*o = 0;
diff --git a/runtime/gfx_window_probe.c b/runtime/gfx_window_probe.c
index 8c4ad97f..93175507 100644
--- a/runtime/gfx_window_probe.c
+++ b/runtime/gfx_window_probe.c
@@ -1,14 +1,16 @@
// --probe-window: presents 300 frames of a cycling clear colour through vk_present_image, reads the swapchain image back
-// and compares every pixel, measures the vsync rate, flips the focus state with synthetic WM_ACTIVATEAPP messages and
-// resizes the window. Run with TOTK_VK_VALIDATION=1 to count validation errors.
+// and compares every pixel, measures the vsync rate, flips the focus state and drives the cursor release with synthetic
+// window events (the window back end's wsys_probe_*, gfx_wsys.h) and resizes the window. Run with TOTK_VK_VALIDATION=1
+// to count validation errors.
#include
#include
-#include
#include "gfx.h"
+#include "gfx_wsys.h"
#include "input.h"
#include "ui/menu.h"
#include "vk.h"
+#include "platform/platform.h"
#define SRC_W 640
#define SRC_H 360
@@ -136,11 +138,11 @@ int probe_window(void) {
if (present_and_check(&s, i == 0, i, &w, &h)) return fail("presented pixels differ");
printf("probe-window: %d presents, readback %dx%d exact\n", FRAMES, w, h);
- // Focus: synthetic WM_ACTIVATEAPP off and on, delivered synchronously on the window thread.
+ // Focus: synthetic focus loss and gain, delivered synchronously on the window thread.
unsigned m0 = gfx_window_focus_messages();
- SendMessageW(gfx_window_handle(), WM_ACTIVATEAPP, FALSE, 0);
+ wsys_probe_focus(0);
int off = gfx_window_focused();
- SendMessageW(gfx_window_handle(), WM_ACTIVATEAPP, TRUE, 0);
+ wsys_probe_focus(1);
int on = gfx_window_focused();
unsigned m1 = gfx_window_focus_messages();
if (off != 0 || on != 1 || m1 - m0 != 2) {
@@ -149,19 +151,17 @@ int probe_window(void) {
}
printf("probe-window: focus off/on pushed 2 messages (15)\n");
- // Cursor release: Esc releases, a client click recaptures, a title bar activation releases. The probe window may not be
- // the foreground window, so this checks the release flag, not the clip state.
- HWND hw = gfx_window_handle();
- SendMessageW(hw, WM_KEYDOWN, VK_ESCAPE, 0);
+ // Cursor release: Esc releases, a client click recaptures, a title bar activation releases (where the window system
+ // reports one). The probe window may not be the foreground window, so this checks the release flag, not the capture.
+ wsys_probe_escape();
int esc = input_cursor_released();
- SendMessageW(hw, WM_LBUTTONDOWN, MK_LBUTTON, 0);
+ wsys_probe_click(0);
int click = input_cursor_released();
- SendMessageW(hw, WM_MOUSEACTIVATE, (WPARAM)hw, MAKELPARAM(HTCLIENT, WM_LBUTTONDOWN));
+ wsys_probe_click(1);
int client = input_cursor_released();
- SendMessageW(hw, WM_MOUSEACTIVATE, (WPARAM)hw, MAKELPARAM(HTCAPTION, WM_LBUTTONDOWN));
- int caption = input_cursor_released();
- SendMessageW(hw, WM_ACTIVATEAPP, FALSE, 0);
- SendMessageW(hw, WM_ACTIVATEAPP, TRUE, 0);
+ int caption = wsys_probe_click(2) ? input_cursor_released() : 1;
+ wsys_probe_focus(0);
+ wsys_probe_focus(1);
int back = input_cursor_released();
if (menu_is_open()) menu_toggle(); // Esc opened the menu: the presents below compare the bare game image
if (!esc || click || client || !caption || back) {
@@ -172,33 +172,31 @@ int probe_window(void) {
// Resize: the client area changes, the swapchain follows, presenting still reads back exact.
gfx_window_resize(1280, 720);
- RECT rc;
- GetClientRect(gfx_window_handle(), &rc);
+ int cw = 0, ch = 0;
+ wsys_client_size(&cw, &ch);
for (int i = 0; i < 10; i++)
if (present_and_check(&s, 0, FRAMES + i, &w, &h)) return fail("presents after resize");
- if (w != rc.right || h != rc.bottom) {
- printf("probe-window: readback %dx%d, client area %ldx%ld\n", w, h, (long)rc.right, (long)rc.bottom);
+ if (w != cw || h != ch) {
+ printf("probe-window: readback %dx%d, client area %dx%d\n", w, h, cw, ch);
return fail("swapchain size after resize");
}
printf("probe-window: resized to %dx%d, presents still exact\n", w, h);
vk_present_capture(0);
// Vsync: count ticks between two tick edges about 5 s apart.
- static volatile long ticks;
+ static volatile int32_t ticks;
gfx_vsync_bind_counter(&ticks);
- LARGE_INTEGER f, t0, t1;
- QueryPerformanceFrequency(&f);
long a = ticks;
- while (ticks == a) Sleep(1);
- QueryPerformanceCounter(&t0);
+ while (ticks == a) plat_sleep_ms(1);
+ uint64_t t0 = plat_now_ns();
long n0 = ticks;
- Sleep(5000);
+ plat_sleep_ms(5000);
long b = ticks;
- while (ticks == b) Sleep(1);
- QueryPerformanceCounter(&t1);
+ while (ticks == b) plat_sleep_ms(1);
+ uint64_t t1 = plat_now_ns();
long n1 = ticks;
- double rate = (double)(n1 - n0) * (double)f.QuadPart / (double)(t1.QuadPart - t0.QuadPart);
- printf("probe-window: vsync %ld ticks in %.3f s = %.3f Hz\n", n1 - n0, (double)(t1.QuadPart - t0.QuadPart) / (double)f.QuadPart, rate);
+ double rate = (double)(n1 - n0) * 1e9 / (double)(t1 - t0);
+ printf("probe-window: vsync %ld ticks in %.3f s = %.3f Hz\n", n1 - n0, (double)(t1 - t0) / 1e9, rate);
if (rate < 60.0 * 0.995 || rate > 60.0 * 1.005) return fail("vsync rate");
source_destroy(&s);
diff --git a/runtime/gfx_window_sdl.c b/runtime/gfx_window_sdl.c
new file mode 100644
index 00000000..d9566178
--- /dev/null
+++ b/runtime/gfx_window_sdl.c
@@ -0,0 +1,332 @@
+// The SDL3 back end of the window (gfx_wsys.h), for hosts without Win32: the window on a thread of its own that also
+// runs SDL's event loop, F11 fullscreen (borderless, the desktop's mode), the settings TOTK_WINDOW_SIZE and
+// TOTK_FULLSCREEN, the display's refresh rate, focus, the Vulkan surface. The swapchain and the present are
+// gfx_present.c's. Keyboard and mouse events go to input_sdl.c and to the menu (ui/menu_sdl.cpp).
+//
+// SDL wants its video calls on the thread that initialised video, so this thread does them all: the others hand it
+// work through run_on_window (a user event and a wait) or post a user event. Video is initialised before the window
+// exists: the Vulkan instance asks for SDL's surface extensions first (gfx_window_vk_extensions), the window comes with
+// gfx_window_start (wsys_open).
+//
+// TOTK_WINDOW_SIZE is in pixels. On a display that scales windows (Wayland, HiDPI) the window asks for its pixels in
+// the display's units (SDL_GetWindowDisplayScale), and SDL reports mouse positions in those units: input_sdl.c and the
+// menu scale them back (gfx_window_sdl_density).
+#include
+#include
+#include
+
+#include
+#include
+
+#include "cfg.h"
+#include "gfx.h"
+#include "gfx_wsys.h"
+#include "input.h"
+#include "input_sdl.h"
+#include "ui/menu.h"
+#include "vk.h"
+#include "platform/platform.h"
+
+static SDL_Window *g_win;
+static uint32_t g_ev_base; // SDL_RegisterEvents: the user events below
+enum { EV_RUN, EV_SETTINGS, EV_CLOSE, EV_COUNT };
+static PlatEvent g_video_ready; // manual-reset: SDL video is up (or failed)
+static int g_video_ok;
+static PlatOnce g_thread_once = PLAT_ONCE_INIT;
+static uint32_t g_window_tid; // the window thread, which runs the video calls
+static volatile int32_t g_density_milli = 1000; // pixels per window unit, x1000
+
+// ---- work on the window thread
+typedef struct {
+ void (*fn)(void *arg);
+ void *arg;
+ PlatEvent done;
+} RunReq;
+
+static void run_on_window(void (*fn)(void *arg), void *arg) {
+ if (plat_thread_id() == g_window_tid) {
+ fn(arg);
+ return;
+ }
+ RunReq r = {fn, arg};
+ plat_event_init(&r.done, 1, 0);
+ SDL_Event e = {.user = {.type = g_ev_base + EV_RUN, .data1 = &r}};
+ if (!SDL_PushEvent(&e)) rt_fatal("gfx: cannot reach the window thread (%s)", SDL_GetError());
+ plat_event_wait(&r.done);
+}
+
+static void post(int ev) {
+ SDL_Event e = {.user = {.type = g_ev_base + (uint32_t)ev}};
+ SDL_PushEvent(&e);
+}
+
+double gfx_window_sdl_density(void) { return __atomic_load_n(&g_density_milli, __ATOMIC_RELAXED) / 1000.0; }
+
+SDL_Window *gfx_window_sdl(void) { return g_win; }
+
+// ---- settings: size and fullscreen
+static char g_applied_size[32];
+static int g_applied_fs;
+
+static void note_applied(void) {
+ snprintf(g_applied_size, sizeof g_applied_size, "%s", cfg_value("TOTK_WINDOW_SIZE"));
+ g_applied_fs = gfx_cfg_fullscreen();
+}
+
+static int is_fullscreen(void) { return (SDL_GetWindowFlags(g_win) & SDL_WINDOW_FULLSCREEN) != 0; }
+
+// The window's size in its units for w x h pixels.
+static void set_pixel_size(int w, int h) {
+ float scale = SDL_GetWindowDisplayScale(g_win);
+ if (!(scale > 0)) scale = 1;
+ SDL_SetWindowSize(g_win, (int)(w / scale + 0.5f), (int)(h / scale + 0.5f));
+}
+
+static void set_fullscreen(int on) {
+ input_cursor_suspend(1);
+ SDL_SetWindowFullscreen(g_win, on); // the desktop's mode: borderless over the display
+ SDL_SyncWindow(g_win);
+ input_cursor_suspend(0);
+}
+
+// Window thread: the window takes the size and fullscreen state of the settings when they changed. A window resized by
+// hand keeps its size when another setting changes.
+static void apply_settings(void) {
+ int size_changed = strcmp(cfg_value("TOTK_WINDOW_SIZE"), g_applied_size) != 0, fs = gfx_cfg_fullscreen();
+ int fs_changed = fs != g_applied_fs;
+ note_applied();
+ if (size_changed) {
+ int w, h;
+ gfx_cfg_window_size(&w, &h);
+ if (is_fullscreen()) SDL_SetWindowFullscreen(g_win, false);
+ set_pixel_size(w, h);
+ if (is_fullscreen() || (fs && !fs_changed)) SDL_SetWindowFullscreen(g_win, true);
+ }
+ if (fs_changed && fs != is_fullscreen()) set_fullscreen(fs);
+}
+
+void gfx_window_apply_settings(void) {
+ if (g_win) post(EV_SETTINGS);
+}
+
+void gfx_window_request_close(void) {
+ if (g_win) post(EV_CLOSE);
+}
+
+// ---- the display's refresh rate and size
+static void refresh_rate_update(void) {
+ const SDL_DisplayMode *m = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(g_win));
+ double hz = 0;
+ if (m && m->refresh_rate_denominator) hz = (double)m->refresh_rate_numerator / m->refresh_rate_denominator;
+ else if (m) hz = m->refresh_rate;
+ gfx_window_set_refresh(hz);
+}
+
+int gfx_window_monitor_size(int *w, int *h) {
+ if (!g_win) return 0;
+ const SDL_DisplayMode *m = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(g_win));
+ if (!m) return 0;
+ *w = (int)(m->w * m->pixel_density + 0.5f);
+ *h = (int)(m->h * m->pixel_density + 0.5f);
+ return 1;
+}
+
+static void density_update(void) {
+ float d = SDL_GetWindowPixelDensity(g_win);
+ __atomic_store_n(&g_density_milli, (int32_t)((d > 0 ? d : 1) * 1000 + 0.5f), __ATOMIC_RELAXED);
+}
+
+// ---- the window thread
+static void handle(const SDL_Event *e) {
+ if (e->type == g_ev_base + EV_RUN) {
+ RunReq *r = e->user.data1;
+ r->fn(r->arg);
+ plat_event_set(&r->done);
+ return;
+ }
+ if (e->type == g_ev_base + EV_SETTINGS) {
+ if (g_win) apply_settings();
+ return;
+ }
+ if (e->type == g_ev_base + EV_CLOSE) gfx_window_closed();
+ if (!g_win) return;
+ input_sdl_event(e);
+ menu_sdl_event(e);
+ switch (e->type) {
+ case SDL_EVENT_KEY_DOWN:
+ if (e->key.scancode == SDL_SCANCODE_F11 && !e->key.repeat) {
+ set_fullscreen(!is_fullscreen());
+ cfg_set("TOTK_FULLSCREEN", is_fullscreen() ? "1" : "0"); // the menu shows it; the next start keeps it
+ cfg_save();
+ note_applied();
+ }
+ break;
+ case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
+ case SDL_EVENT_WINDOW_RESIZED:
+ case SDL_EVENT_WINDOW_RESTORED:
+ density_update();
+ gfx_window_mark_dirty();
+ break;
+ case SDL_EVENT_WINDOW_DISPLAY_CHANGED:
+ case SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED:
+ case SDL_EVENT_DISPLAY_CURRENT_MODE_CHANGED:
+ density_update();
+ refresh_rate_update();
+ gfx_window_mark_dirty();
+ break;
+ case SDL_EVENT_WINDOW_FOCUS_GAINED:
+ case SDL_EVENT_WINDOW_FOCUS_LOST:
+ gfx_window_focus_changed(e->type == SDL_EVENT_WINDOW_FOCUS_GAINED);
+ break;
+ case SDL_EVENT_QUIT:
+ case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
+ gfx_window_closed();
+ break;
+ }
+}
+
+static void window_main(void *arg) {
+ (void)arg;
+ g_window_tid = plat_thread_id();
+ SDL_SetHint(SDL_HINT_APP_ID, "totk-recomp"); // Wayland's app id: the desktop file and the taskbar match it
+ SDL_SetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "0");
+ g_video_ok = SDL_Init(SDL_INIT_VIDEO);
+ if (!g_video_ok) rt_log("gfx: SDL video init failed (%s)", SDL_GetError());
+ // SDL's Vulkan queries need its Vulkan loader, which a window would load at creation: the instance and the device
+ // are made before the window.
+ else if (!SDL_Vulkan_LoadLibrary(NULL)) {
+ rt_log("gfx: SDL cannot load Vulkan (%s)", SDL_GetError());
+ g_video_ok = 0;
+ } else {
+ g_ev_base = SDL_RegisterEvents(EV_COUNT);
+ rt_log("gfx: SDL %d.%d.%d, video driver %s", SDL_VERSIONNUM_MAJOR(SDL_GetVersion()),
+ SDL_VERSIONNUM_MINOR(SDL_GetVersion()), SDL_VERSIONNUM_MICRO(SDL_GetVersion()), SDL_GetCurrentVideoDriver());
+ }
+ plat_event_set(&g_video_ready);
+ if (!g_video_ok) return;
+ for (;;) {
+ SDL_Event e;
+ if (SDL_WaitEventTimeout(&e, 100)) handle(&e);
+ else input_sdl_idle(); // a cursor change requested from another thread meanwhile
+ }
+}
+
+static void video_start(void *arg) {
+ (void)arg;
+ plat_event_init(&g_video_ready, 1, 0);
+ if (!plat_thread_spawn(window_main, NULL, "window", 0)) rt_fatal("gfx: cannot start the window thread");
+ plat_event_wait(&g_video_ready);
+}
+
+static int video_up(void) {
+ plat_once(&g_thread_once, video_start, NULL);
+ return g_video_ok;
+}
+
+int gfx_window_vk_extensions(const char **out, int max) {
+ if (!video_up()) return 0;
+ Uint32 n = 0;
+ const char *const *ext = SDL_Vulkan_GetInstanceExtensions(&n);
+ if (!ext) {
+ rt_log("gfx: SDL_Vulkan_GetInstanceExtensions failed (%s)", SDL_GetError());
+ return 0;
+ }
+ int k = 0;
+ for (Uint32 i = 0; i < n && k < max; i++) out[k++] = ext[i];
+ return k;
+}
+
+int gfx_window_vk_presentable(VkInstance inst, VkPhysicalDevice pd, uint32_t family) {
+ return video_up() && SDL_Vulkan_GetPresentationSupport(inst, pd, family);
+}
+
+// ---- gfx_wsys.h
+static int open_ok;
+
+static void open_window(void *arg) {
+ (void)arg;
+ int w, h;
+ gfx_cfg_window_size(&w, &h);
+ g_win = SDL_CreateWindow("TotK Recomp", w, h, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY);
+ if (!g_win) {
+ rt_log("gfx: SDL_CreateWindow failed (%s)", SDL_GetError());
+ return;
+ }
+ set_pixel_size(w, h); // w x h were window units; the setting is in pixels
+ density_update();
+ note_applied();
+ refresh_rate_update();
+ if (cfg_get("TOTK_BACKGROUND_WINDOW")) rt_log("gfx: TOTK_BACKGROUND_WINDOW has no effect with SDL windows");
+ if (gfx_cfg_fullscreen()) SDL_SetWindowFullscreen(g_win, true);
+ SDL_SyncWindow(g_win);
+ input_sdl_window(g_win);
+ open_ok = 1;
+}
+
+int wsys_open(void) {
+ if (!video_up()) return 0;
+ if (!g_win) run_on_window(open_window, NULL);
+ return open_ok;
+}
+
+typedef struct { VkInstance inst; VkSurfaceKHR surface; } SurfaceReq;
+
+static void make_surface(void *arg) {
+ SurfaceReq *r = arg;
+ if (!SDL_Vulkan_CreateSurface(g_win, r->inst, NULL, &r->surface)) {
+ rt_log("gfx: SDL_Vulkan_CreateSurface failed (%s)", SDL_GetError());
+ r->surface = VK_NULL_HANDLE;
+ }
+}
+
+VkSurfaceKHR wsys_surface(VkInstance inst) {
+ SurfaceReq r = {inst, VK_NULL_HANDLE};
+ run_on_window(make_surface, &r);
+ return r.surface;
+}
+
+void wsys_client_size(int *w, int *h) {
+ *w = *h = 0;
+ if (g_win) SDL_GetWindowSizeInPixels(g_win, w, h);
+}
+
+int wsys_minimized(void) { return g_win && (SDL_GetWindowFlags(g_win) & (SDL_WINDOW_MINIMIZED | SDL_WINDOW_HIDDEN)); }
+
+static void resize_window(void *arg) {
+ const int *wh = arg;
+ set_pixel_size(wh[0], wh[1]);
+ SDL_SyncWindow(g_win);
+}
+
+void wsys_resize(int w, int h) {
+ int wh[2] = {w, h};
+ run_on_window(resize_window, wh);
+}
+
+// ---- --probe-window: synthetic events through the event handler, on the window thread
+static void probe_event(void *arg) { handle(arg); }
+
+void wsys_probe_focus(int in) {
+ SDL_Event e = {.window = {.type = in ? SDL_EVENT_WINDOW_FOCUS_GAINED : SDL_EVENT_WINDOW_FOCUS_LOST,
+ .windowID = SDL_GetWindowID(g_win)}};
+ run_on_window(probe_event, &e);
+}
+
+void wsys_probe_escape(void) {
+ SDL_Event e = {.key = {.type = SDL_EVENT_KEY_DOWN, .scancode = SDL_SCANCODE_ESCAPE, .key = SDLK_ESCAPE, .down = true,
+ .windowID = SDL_GetWindowID(g_win)}};
+ run_on_window(probe_event, &e);
+ e.key.type = SDL_EVENT_KEY_UP, e.key.down = false;
+ run_on_window(probe_event, &e);
+}
+
+// SDL reports no activation by a title bar click: only the client click exists.
+int wsys_probe_click(int where) {
+ if (where == 2) return 0;
+ SDL_Event e = {.button = {.type = SDL_EVENT_MOUSE_BUTTON_DOWN, .button = SDL_BUTTON_LEFT, .down = true,
+ .windowID = SDL_GetWindowID(g_win)}};
+ run_on_window(probe_event, &e);
+ e.button.type = SDL_EVENT_MOUSE_BUTTON_UP, e.button.down = false;
+ run_on_window(probe_event, &e);
+ return 1;
+}
diff --git a/runtime/gfx_window_win.c b/runtime/gfx_window_win.c
new file mode 100644
index 00000000..a4883f32
--- /dev/null
+++ b/runtime/gfx_window_win.c
@@ -0,0 +1,316 @@
+// The Win32 back end of the window (gfx_wsys.h): the window and its message loop on a thread of their own, F11
+// fullscreen, the settings TOTK_WINDOW_SIZE and TOTK_FULLSCREEN, the monitor's refresh rate, focus, the Vulkan surface.
+// The swapchain and the present are gfx_present.c's. The in-game menu's keyboard and mouse come from the window
+// procedure (ui/menu_win.cpp), and so do the game's (input_win.c).
+#include
+#include
+#include
+#include
+
+#include "cfg.h"
+#include "flight.h"
+#include "fs.h"
+#include "gfx.h"
+#include "gfx_wsys.h"
+#include "input.h"
+#include "input_win.h"
+#include "nvn_api.h"
+#include "ui/menu.h"
+#include "vk.h"
+#include "platform/platform.h"
+#include
+
+// ---- window state
+static HWND g_hwnd;
+static PlatEvent g_wnd_ready; // manual-reset: the window thread made the window or gave up
+static int g_wnd_failed;
+static int g_background; // TOTK_BACKGROUND_WINDOW: the window stays behind the user's windows
+static int g_fs_pending; // window thread: TOTK_FULLSCREEN waits for the first activation
+
+HWND gfx_window_handle(void) { return g_hwnd; }
+
+int gfx_window_vk_extensions(const char **out, int max) {
+ if (max < 2) return 0;
+ out[0] = VK_KHR_SURFACE_EXTENSION_NAME;
+ out[1] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
+ return 2;
+}
+
+int gfx_window_vk_presentable(VkInstance inst, VkPhysicalDevice pd, uint32_t family) {
+ (void)inst;
+ return vkGetPhysicalDeviceWin32PresentationSupportKHR(pd, family) != VK_FALSE;
+}
+
+// ---- window thread
+#define WND_STYLE WS_OVERLAPPEDWINDOW
+#define WM_APP_SETTINGS (WM_APP + 1)
+#define WM_APP_FULLSCREEN (WM_APP + 2) // the fullscreen setting, held back until the window is first activated
+
+static WINDOWPLACEMENT g_saved_place = {sizeof g_saved_place}; // the window behind fullscreen (window thread)
+
+static int is_fullscreen(HWND h) { return !(GetWindowLongW(h, GWL_STYLE) & WS_OVERLAPPEDWINDOW); }
+
+#define cfg_fullscreen gfx_cfg_fullscreen
+#define cfg_window_size gfx_cfg_window_size
+
+// F11: borderless window over the whole monitor, and back to the saved window. The swapchain follows through WM_SIZE.
+// Not Alt+Enter: Enter is the Plus button (input_win.c). A background window (test runs) keeps its place behind the
+// user's windows and takes no focus.
+static void toggle_fullscreen(HWND h) {
+ LONG style = GetWindowLongW(h, GWL_STYLE);
+ if (style & WS_OVERLAPPEDWINDOW) {
+ MONITORINFO mi = {sizeof mi};
+ if (!GetWindowPlacement(h, &g_saved_place) || !GetMonitorInfoW(MonitorFromWindow(h, MONITOR_DEFAULTTOPRIMARY), &mi))
+ return;
+ SetWindowLongW(h, GWL_STYLE, (style & ~WS_OVERLAPPEDWINDOW) | WS_POPUP);
+ SetWindowPos(h, HWND_TOP, mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right - mi.rcMonitor.left,
+ mi.rcMonitor.bottom - mi.rcMonitor.top,
+ SWP_NOOWNERZORDER | SWP_FRAMECHANGED | (g_background ? SWP_NOZORDER | SWP_NOACTIVATE : 0));
+ } else {
+ SetWindowLongW(h, GWL_STYLE, (style & ~WS_POPUP) | WND_STYLE);
+ if (g_background) g_saved_place.showCmd = SW_SHOWNOACTIVATE;
+ SetWindowPlacement(h, &g_saved_place);
+ SetWindowPos(h, NULL, 0, 0, 0, 0,
+ SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED |
+ (g_background ? SWP_NOACTIVATE : 0));
+ }
+}
+
+// The window size and fullscreen state last taken from the settings (window thread). apply_settings acts on a change of
+// either value only, so a window resized by hand keeps its size when another setting changes.
+static char g_applied_size[32];
+static int g_applied_fs;
+
+static void note_applied(void) {
+ snprintf(g_applied_size, sizeof g_applied_size, "%s", cfg_value("TOTK_WINDOW_SIZE"));
+ g_applied_fs = cfg_fullscreen();
+}
+
+// Window thread: the window takes the size and fullscreen state of the settings when they changed. In fullscreen the
+// size goes into the placement restored when fullscreen ends.
+static void apply_settings(HWND h) {
+ int size_changed = strcmp(cfg_value("TOTK_WINDOW_SIZE"), g_applied_size) != 0, fs = cfg_fullscreen(), fs_changed = fs != g_applied_fs;
+ note_applied();
+ if (size_changed) {
+ int cw, ch;
+ cfg_window_size(&cw, &ch);
+ RECT r = {0, 0, cw, ch};
+ AdjustWindowRect(&r, WND_STYLE, FALSE);
+ if (is_fullscreen(h)) {
+ RECT *n = &g_saved_place.rcNormalPosition;
+ n->right = n->left + (r.right - r.left);
+ n->bottom = n->top + (r.bottom - r.top);
+ } else {
+ RECT have;
+ if (GetClientRect(h, &have) && (have.right != cw || have.bottom != ch)) {
+ if (IsZoomed(h)) ShowWindow(h, SW_RESTORE);
+ SetWindowPos(h, NULL, 0, 0, r.right - r.left, r.bottom - r.top,
+ SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
+ }
+ }
+ }
+ if (fs_changed && fs != is_fullscreen(h)) {
+ input_cursor_suspend(1);
+ toggle_fullscreen(h);
+ input_cursor_suspend(0);
+ }
+}
+
+void gfx_window_apply_settings(void) {
+ if (g_hwnd) PostMessageW(g_hwnd, WM_APP_SETTINGS, 0, 0);
+}
+
+
+void gfx_window_request_close(void) {
+ if (g_hwnd) PostMessageW(g_hwnd, WM_CLOSE, 0, 0);
+}
+
+// ---- refresh rate of the window's monitor (TOTK_FPS_LIMIT=display), measured when the window opens, lands on another
+// monitor or the display mode changes (gfx_window_set_refresh keeps it). QueryDisplayConfig gives the exact rational
+// rate (59.94, 143.998); EnumDisplaySettings' whole hertz is the fallback.
+static HMONITOR g_rate_mon;
+
+static double monitor_rate_hz(HMONITOR mon) {
+ MONITORINFOEXW mi = {.cbSize = sizeof mi};
+ if (!GetMonitorInfoW(mon, (MONITORINFO *)&mi)) return 0;
+ double hz = 0;
+ UINT32 np = 0, nm = 0;
+ if (GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &np, &nm) == ERROR_SUCCESS && np && nm) {
+ DISPLAYCONFIG_PATH_INFO *paths = calloc(np, sizeof *paths);
+ DISPLAYCONFIG_MODE_INFO *modes = calloc(nm, sizeof *modes);
+ if (paths && modes && QueryDisplayConfig(QDC_ONLY_ACTIVE_PATHS, &np, paths, &nm, modes, NULL) == ERROR_SUCCESS)
+ for (UINT32 i = 0; i < np && !hz; i++) {
+ DISPLAYCONFIG_SOURCE_DEVICE_NAME src = {.header = {.type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME,
+ .size = sizeof src,
+ .adapterId = paths[i].sourceInfo.adapterId,
+ .id = paths[i].sourceInfo.id}};
+ DISPLAYCONFIG_RATIONAL r = paths[i].targetInfo.refreshRate;
+ if (DisplayConfigGetDeviceInfo(&src.header) == ERROR_SUCCESS && !wcscmp(src.viewGdiDeviceName, mi.szDevice) &&
+ r.Denominator)
+ hz = (double)r.Numerator / r.Denominator;
+ }
+ free(paths);
+ free(modes);
+ }
+ DEVMODEW dm = {.dmSize = sizeof dm};
+ if (!hz && EnumDisplaySettingsW(mi.szDevice, ENUM_CURRENT_SETTINGS, &dm) && dm.dmDisplayFrequency > 1)
+ hz = dm.dmDisplayFrequency; // 0 and 1 mean the hardware default
+ return hz;
+}
+
+// Window thread. force: the display mode changed, measure even on the same monitor.
+static void refresh_rate_update(HWND h, int force) {
+ HMONITOR mon = MonitorFromWindow(h, MONITOR_DEFAULTTOPRIMARY);
+ if (!force && mon == g_rate_mon) return;
+ g_rate_mon = mon;
+ gfx_window_set_refresh(monitor_rate_hz(mon));
+}
+
+int gfx_window_monitor_size(int *w, int *h) {
+ MONITORINFO mi = {sizeof mi};
+ if (!g_hwnd || !GetMonitorInfoW(MonitorFromWindow(g_hwnd, MONITOR_DEFAULTTOPRIMARY), &mi)) return 0;
+ *w = mi.rcMonitor.right - mi.rcMonitor.left;
+ *h = mi.rcMonitor.bottom - mi.rcMonitor.top;
+ return 1;
+}
+
+static LRESULT CALLBACK wnd_proc(HWND h, UINT m, WPARAM w, LPARAM l) {
+ LRESULT input_res;
+ if (input_window_msg(h, m, w, l, &input_res)) return input_res;
+ if (menu_window_msg(h, m, w, l, &input_res)) return input_res;
+ switch (m) {
+ case WM_KEYDOWN:
+ if (w == VK_F11 && !(l & (1 << 30))) { // bit 30: auto-repeat
+ input_cursor_suspend(1);
+ toggle_fullscreen(h);
+ input_cursor_suspend(0);
+ cfg_set("TOTK_FULLSCREEN", is_fullscreen(h) ? "1" : "0"); // the menu shows it; the next start keeps it
+ cfg_save();
+ note_applied(); // what cfg_get reads now (the environment may still override it)
+ }
+ break;
+ case WM_APP_SETTINGS:
+ apply_settings(h);
+ return 0;
+ case WM_APP_FULLSCREEN:
+ if (cfg_fullscreen() && !is_fullscreen(h)) {
+ input_cursor_suspend(1);
+ toggle_fullscreen(h);
+ input_cursor_suspend(0);
+ }
+ return 0;
+ case WM_SIZE:
+ if (w != SIZE_MINIMIZED) gfx_window_mark_dirty();
+ refresh_rate_update(h, 0);
+ return 0;
+ case WM_EXITSIZEMOVE: // dragged onto another monitor
+ refresh_rate_update(h, 0);
+ break;
+ case WM_DISPLAYCHANGE:
+ refresh_rate_update(h, 1);
+ break;
+ case WM_ACTIVATEAPP: {
+ int in = w != 0;
+ gfx_window_focus_changed(in);
+ if (in && g_fs_pending) {
+ g_fs_pending = 0;
+ PostMessageW(h, WM_APP_FULLSCREEN, 0, 0);
+ }
+ return 0;
+ }
+ case WM_CLOSE:
+ gfx_window_closed();
+ case WM_ERASEBKGND:
+ return 1;
+ }
+ return DefWindowProcW(h, m, w, l);
+}
+
+static void window_main(void *arg) {
+ (void)arg;
+ HINSTANCE inst = GetModuleHandleW(NULL);
+ // Icon resource 1 of totk.rc (runtime/totk.ico): the title bar, the taskbar button and Alt+Tab.
+ WNDCLASSEXW wc = {.cbSize = sizeof wc, .lpfnWndProc = wnd_proc, .hInstance = inst,
+ .hIcon = LoadImageW(inst, MAKEINTRESOURCEW(1), IMAGE_ICON, GetSystemMetrics(SM_CXICON),
+ GetSystemMetrics(SM_CYICON), LR_SHARED),
+ .hIconSm = LoadImageW(inst, MAKEINTRESOURCEW(1), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
+ GetSystemMetrics(SM_CYSMICON), LR_SHARED),
+ .hCursor = LoadCursorW(NULL, (LPCWSTR)IDC_ARROW), .lpszClassName = L"TotKRecompWindow"};
+ RegisterClassExW(&wc);
+ int cw, ch;
+ cfg_window_size(&cw, &ch);
+ RECT r = {0, 0, cw, ch};
+ AdjustWindowRect(&r, WND_STYLE, FALSE);
+ // The window opens behind the user's windows without taking focus: it shows the game's blank loading frames for a
+ // while, and must not cover what the player works on meanwhile. The game still counts itself in focus until the
+ // first WM_ACTIVATEAPP, so loading goes on, and keyboard input waits for the foreground (input_win.c). Its taskbar
+ // button flashes until it is brought up, and TOTK_FULLSCREEN waits for that first activation.
+ // TOTK_BACKGROUND_WINDOW=1 (test runs): no flashing, never fullscreen at start, and F11 keeps the window's place.
+ int background = g_background = cfg_get("TOTK_BACKGROUND_WINDOW") && atoi(cfg_get("TOTK_BACKGROUND_WINDOW"));
+ g_hwnd = CreateWindowExW(0, wc.lpszClassName, L"TotK Recomp", WND_STYLE, CW_USEDEFAULT, CW_USEDEFAULT,
+ r.right - r.left, r.bottom - r.top, NULL, NULL, inst, NULL);
+ if (!g_hwnd) {
+ rt_log("gfx: CreateWindowEx failed (%lu)", GetLastError());
+ g_wnd_failed = 1;
+ } else {
+ SetWindowPos(g_hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW);
+ if (!background) {
+ FLASHWINFO fw = {.cbSize = sizeof fw, .hwnd = g_hwnd, .dwFlags = FLASHW_TRAY | FLASHW_TIMERNOFG};
+ FlashWindowEx(&fw);
+ g_fs_pending = cfg_fullscreen();
+ }
+ note_applied();
+ refresh_rate_update(g_hwnd, 1);
+ }
+ plat_event_set(&g_wnd_ready);
+ if (g_wnd_failed) return;
+ MSG msg;
+ while (GetMessageW(&msg, NULL, 0, 0) > 0) {
+ TranslateMessage(&msg);
+ DispatchMessageW(&msg);
+ }
+ return;
+}
+
+// ---- gfx_wsys.h
+// Makes the window on its thread and waits until it is there.
+int wsys_open(void) {
+ g_wnd_failed = 0;
+ plat_event_init(&g_wnd_ready, 1, 0);
+ if (!plat_thread_spawn(window_main, NULL, "window", 0)) rt_fatal("gfx: cannot start the window thread");
+ plat_event_wait(&g_wnd_ready);
+ return !g_wnd_failed;
+}
+
+VkSurfaceKHR wsys_surface(VkInstance inst) {
+ VkWin32SurfaceCreateInfoKHR si = {.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR,
+ .hinstance = GetModuleHandleW(NULL), .hwnd = g_hwnd};
+ VkSurfaceKHR surface = VK_NULL_HANDLE;
+ VkResult r = vkCreateWin32SurfaceKHR(inst, &si, NULL, &surface);
+ if (r != VK_SUCCESS) rt_log("gfx: vkCreateWin32SurfaceKHR failed (%d)", (int)r);
+ return r == VK_SUCCESS ? surface : VK_NULL_HANDLE;
+}
+
+void wsys_client_size(int *w, int *h) {
+ RECT rc = {0};
+ GetClientRect(g_hwnd, &rc);
+ *w = rc.right, *h = rc.bottom;
+}
+
+int wsys_minimized(void) { return IsIconic(g_hwnd) != 0; }
+
+void wsys_resize(int cw, int ch) {
+ RECT r = {0, 0, cw, ch};
+ AdjustWindowRect(&r, WND_STYLE, FALSE);
+ SetWindowPos(g_hwnd, NULL, 0, 0, r.right - r.left, r.bottom - r.top, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
+}
+
+void wsys_probe_focus(int in) { SendMessageW(g_hwnd, WM_ACTIVATEAPP, in ? TRUE : FALSE, 0); }
+
+void wsys_probe_escape(void) { SendMessageW(g_hwnd, WM_KEYDOWN, VK_ESCAPE, 0); }
+
+int wsys_probe_click(int where) {
+ if (where == 0) SendMessageW(g_hwnd, WM_LBUTTONDOWN, MK_LBUTTON, 0);
+ else SendMessageW(g_hwnd, WM_MOUSEACTIVATE, (WPARAM)g_hwnd, MAKELPARAM(where == 1 ? HTCLIENT : HTCAPTION, WM_LBUTTONDOWN));
+ return 1;
+}
diff --git a/runtime/gfx_wsys.h b/runtime/gfx_wsys.h
new file mode 100644
index 00000000..90fdeef1
--- /dev/null
+++ b/runtime/gfx_wsys.h
@@ -0,0 +1,25 @@
+// Between gfx_present.c (swapchain, present, vsync) and the window system's back end (gfx_window_win.c with Win32,
+// gfx_window_sdl.c with SDL). Host threads only.
+#pragma once
+#include "vk.h"
+
+// ---- what the back end provides
+int wsys_open(void); // makes the window (and its thread); 0 on failure. Once.
+VkSurfaceKHR wsys_surface(VkInstance inst); // a new Vulkan surface of the window; VK_NULL_HANDLE on failure
+void wsys_client_size(int *w, int *h); // the drawable area in pixels
+int wsys_minimized(void); // nothing to present into
+void wsys_resize(int w, int h); // gfx_window_resize: the drawable area becomes w x h
+// --probe-window (gfx_window_probe.c): synthetic window events, handled on the window thread before these return.
+void wsys_probe_focus(int in); // the window gains (1) or loses (0) the focus
+void wsys_probe_escape(void); // Esc pressed
+// A left click: 0 in the client area, 1 activating the window from its client area, 2 activating it from the title bar.
+// 0 when the window system has no such event (then the caller skips that check).
+int wsys_probe_click(int where);
+
+// ---- what gfx_present.c provides the back end
+void gfx_window_mark_dirty(void); // the window changed size: the next present rebuilds the swapchain
+void gfx_window_focus_changed(int in); // the window gained (1) or lost (0) the focus
+void gfx_window_closed(void); // the player closed the window: saves, then ends the process
+void gfx_window_set_refresh(double hz); // the refresh rate of the window's monitor, 0 unknown
+int gfx_cfg_fullscreen(void); // TOTK_FULLSCREEN
+void gfx_cfg_window_size(int *w, int *h); // TOTK_WINDOW_SIZE, the default when unparsable or out of range
diff --git a/runtime/host.h b/runtime/host.h
index 87b55007..5e454fff 100644
--- a/runtime/host.h
+++ b/runtime/host.h
@@ -91,11 +91,19 @@ static inline void ret_d(Cpu *c, double d) { uint64_t b; memcpy(&b, &d, 8); c->v
typedef struct { const char *name; GuestFn fn; } RtHostEntry;
#define RT_CAT2(a, b) a##b
#define RT_CAT(a, b) RT_CAT2(a, b)
-// File-scope declarations; fn must be declared before. The loader binds them by symbol name.
+// File-scope declarations; fn must be declared before. The loader binds them by symbol name. The entries of every
+// object file meet in one section per kind, which hostfn.c walks: on PE/COFF ".ovr$m", which the linker sorts between
+// hostfn.c's ".ovr$a" and ".ovr$z" markers; on ELF "totk_ovr", which the linker brackets with __start_totk_ovr and
+// __stop_totk_ovr.
+#ifdef _WIN32
+#define RT_HOST_SECTION(kind) section("." #kind "$m")
+#else
+#define RT_HOST_SECTION(kind) section("totk_" #kind)
+#endif
#define HOST_OVERRIDE(sym, fn) /* replaces sym in every GOT/GLOB_DAT/ABS64 of every module */ \
- __attribute__((used, section(".ovr$m"))) static const RtHostEntry RT_CAT(rt_ovr_, __COUNTER__) = {sym, fn}
+ __attribute__((used, RT_HOST_SECTION(ovr))) static const RtHostEntry RT_CAT(rt_ovr_, __COUNTER__) = {sym, fn}
#define HOST_OBSERVER(sym, fn) /* JUMP_SLOT only; fn calls rt_original(c) */ \
- __attribute__((used, section(".obs$m"))) static const RtHostEntry RT_CAT(rt_obs_, __COUNTER__) = {sym, fn}
+ __attribute__((used, RT_HOST_SECTION(obs))) static const RtHostEntry RT_CAT(rt_obs_, __COUNTER__) = {sym, fn}
uint64_t hostfn_register(const char *name, GuestFn fn); // synthetic address; before hostfn_seal only. The same
// (name, fn) pair returns the same address
GuestFn hostfn_override(const char *name); // NULL if none
@@ -285,7 +293,7 @@ int probe_loader(void), probe_kernel(void), probe_ipc(void), probe_services(void
int probe_gfx_abi(void); // gfx_abi_probe.c
int probe_vkdevice(void); // vk_device.c
int probe_nvn(void), probe_nvn_tex(void); // nvn_probe.c, nvn_tex_probe.c
-int probe_window(void); // gfx_window.c
+int probe_window(void); // gfx_window_probe.c
int probe_shader(void); // gfx_shader.c
int probe_vkmem(void), probe_vkexec(void), probe_astc(void); // tex_probe.c, vk_exec_probe.c, astc_probe.c
int probe_movie(void); // movie_probe.c: host VP9 movie::Decoder on one RomFS movie
diff --git a/runtime/hostfn.c b/runtime/hostfn.c
index d3ef85dd..e57a7d41 100644
--- a/runtime/hostfn.c
+++ b/runtime/hostfn.c
@@ -14,11 +14,22 @@
#define MAX_HOSTFN 4096 // dispatch.c holds HOST_CAP / 2 host functions
#define MAX_ADDED 64
+#ifdef _WIN32
// PE/COFF: the linker sorts .ovr$a < .ovr$m (entries, host.h) < .ovr$z by the text after '$'.
__attribute__((section(".ovr$a"), used)) static const RtHostEntry ovr_start[1];
__attribute__((section(".ovr$z"), used)) static const RtHostEntry ovr_stop[1];
__attribute__((section(".obs$a"), used)) static const RtHostEntry obs_start[1];
__attribute__((section(".obs$z"), used)) static const RtHostEntry obs_stop[1];
+#else
+// ELF: the linker defines __start_ and __stop_ around a section named like a C identifier. Weak: a
+// build without observers has no such section, and both bounds are then NULL.
+extern const RtHostEntry __start_totk_ovr[] __attribute__((weak)), __stop_totk_ovr[] __attribute__((weak));
+extern const RtHostEntry __start_totk_obs[] __attribute__((weak)), __stop_totk_obs[] __attribute__((weak));
+#define ovr_start __start_totk_ovr
+#define ovr_stop __stop_totk_ovr
+#define obs_start __start_totk_obs
+#define obs_stop __stop_totk_obs
+#endif
typedef struct {
const char *name;
diff --git a/runtime/input.c b/runtime/input.c
new file mode 100644
index 00000000..e0b4e3f3
--- /dev/null
+++ b/runtime/input.c
@@ -0,0 +1,636 @@
+// Host input for nn::hid (runtime/ov_input.c): an SDL3 gamepad (no SDL window needed), the keyboard and the mouse,
+// merged into Switch npad button bits and stick axes. Button order follows libnx HidNpadButton (ISC). The key and pad
+// bindings are the tables in input_map.c (totk.ini overrides them). The keyboard and the mouse come from the window
+// system's back end (input_wsys.h: input_win.c with Win32, input_sdl.c with SDL), which also owns the cursor capture.
+// Keys and their map: docs/controls.md.
+// The gamepad also rumbles (input_rumble) and, when it has a gyro and an accelerometer, feeds the six-axis sensor from a
+// thread of its own (input_motion_start). Every SDL call on the gamepad is made under `lock`.
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include "cfg.h"
+#include "host.h"
+#include "input.h"
+#include "input_map.h"
+#include "input_wsys.h"
+#include "nvn_api.h"
+#include "ui/menu.h"
+#include "vk.h"
+#include "platform/platform.h"
+
+enum { STICK_MAX = 32767, STICK_DEAD = 2500, STICK_DIR = 16384, TRIGGER_ON = 16384, RESCAN_MS = 1000 };
+
+static PlatMutex lock = PLAT_MUTEX_INIT;
+static int state; // 0 not started, 1 running, -1 disabled or SDL failed
+static SDL_Gamepad *pad;
+static uint64_t next_scan;
+static int pad_rumble; // the open gamepad has rumble motors
+static volatile int32_t pad_labels[4]; // ... and the labels of its south, east, west, north buttons (PAD_LABEL_*), any thread
+static int pad_east_a; // ... and labels its east face button A (Nintendo layout under TOTK_PAD_LAYOUT=auto)
+static int pad_layout = -1; // PAD_LAYOUT_* in effect for the open gamepad, -1 to pick again; live (cfg_version)
+static unsigned pad_layout_seen;
+static volatile int32_t pad_layout_shown = -1; // pad_layout for the menu (any thread), -1 without a gamepad
+static SDL_JoystickID sensor_pad; // the gamepad whose sensors the motion thread set up, 0 none
+static int sensor_ok; // ... and it has both sensors, enabled
+static int motion_thread; // 0 not started, 1 running, -1 off (TOTK_MOTION=0, no SDL)
+
+// SDL buttons of the gamepad inputs (input_map.h PAD_*), by position; PAD_ZL and PAD_ZR are the triggers (axes).
+static const SDL_GamepadButton pad_sdl[PAD_COUNT] = {
+ [PAD_SOUTH] = SDL_GAMEPAD_BUTTON_SOUTH, [PAD_EAST] = SDL_GAMEPAD_BUTTON_EAST, [PAD_WEST] = SDL_GAMEPAD_BUTTON_WEST,
+ [PAD_NORTH] = SDL_GAMEPAD_BUTTON_NORTH, [PAD_LSTICK] = SDL_GAMEPAD_BUTTON_LEFT_STICK,
+ [PAD_RSTICK] = SDL_GAMEPAD_BUTTON_RIGHT_STICK, [PAD_L] = SDL_GAMEPAD_BUTTON_LEFT_SHOULDER,
+ [PAD_R] = SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, [PAD_START] = SDL_GAMEPAD_BUTTON_START, [PAD_BACK] = SDL_GAMEPAD_BUTTON_BACK,
+ [PAD_DPAD_LEFT] = SDL_GAMEPAD_BUTTON_DPAD_LEFT, [PAD_DPAD_UP] = SDL_GAMEPAD_BUTTON_DPAD_UP,
+ [PAD_DPAD_RIGHT] = SDL_GAMEPAD_BUTTON_DPAD_RIGHT, [PAD_DPAD_DOWN] = SDL_GAMEPAD_BUTTON_DPAD_DOWN,
+};
+
+// TOTK_INPUT_SCRIPT=: lines "