Speed up the replay and fix what the perf pass found, and add a frame capture key for chasing rendering faults.

Performance: draws reuse the bindings that did not change and record only the dynamic states that differ from what
the command buffer holds; small uploads come from a staging arena; finished submissions are held briefly and handed to
the queue in batches; the recorder thread can keep a CPU core of its own; ASTC block-linear textures are deswizzled on
the GPU; dynamic resolution steps the upscaler mode to hold a frame-time target. Each has a setting in nx2navi.ini
(bind_reuse, dyn_delta, stg_arena, submit_batch, recorder_core, recorder_prio, astc_gpu_deswizzle, dyn_res) and the
staging cache now frees what it holds at shutdown.

Capture: NX2NAVI_DUMP=key saves the frame after each press of Insert, with mesh draws listed by task, mesh and fragment
shader, depth test and face culling, and mesh_prims.txt giving each mesh draw's primitive, clipping and fragment counts.
All of it is off unless NX2NAVI_DUMP is set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-09 01:35:47 -05:00
parent 86de3320a0
commit 1f7e9c975b
38 changed files with 3105 additions and 1840 deletions

View File

@@ -15,6 +15,18 @@ void plat_rmutex_lock(PlatRMutex *m) {
m->depth = 1;
}
int plat_rmutex_trylock(PlatRMutex *m) {
uint32_t me = plat_thread_id();
if (__atomic_load_n(&m->owner, __ATOMIC_RELAXED) == me) {
m->depth++;
return 1;
}
if (!plat_mutex_trylock(&m->m)) return 0;
__atomic_store_n(&m->owner, me, __ATOMIC_RELAXED);
m->depth = 1;
return 1;
}
void plat_rmutex_unlock(PlatRMutex *m) {
if (--m->depth) return;
__atomic_store_n(&m->owner, 0, __ATOMIC_RELAXED);

View File

@@ -390,6 +390,29 @@ int plat_thread_join_ns(PlatThread *t, uint64_t ns) {
// The thread keeps using t->fn and t->arg, so the struct stays allocated (a few bytes per detached thread).
void plat_thread_detach(PlatThread *t) { pthread_detach(t->t); }
// The calling thread gets the last processor of its affinity mask to itself: every other thread of the process now
// running loses that processor (threads they start later inherit their masks), and the caller keeps only it. Returns
// 1, or 0 when the mask has fewer than three processors or the caller's mask cannot be set.
int plat_cpu_reserve(void) {
cpu_set_t all, mine, rest;
if (sched_getaffinity(0, sizeof all, &all) || CPU_COUNT(&all) < 3) return 0;
int last = -1;
for (int i = CPU_SETSIZE - 1; i >= 0 && last < 0; i--)
if (CPU_ISSET(i, &all)) last = i;
rest = all;
CPU_CLR(last, &rest);
CPU_ZERO(&mine);
CPU_SET(last, &mine);
pid_t me = (pid_t)syscall(SYS_gettid);
DIR *d = opendir("/proc/self/task");
for (struct dirent *e; d && (e = readdir(d));) {
pid_t tid = (pid_t)atoi(e->d_name);
if (tid > 0 && tid != me) sched_setaffinity(tid, sizeof rest, &rest);
}
if (d) closedir(d);
return sched_setaffinity(0, sizeof mine, &mine) == 0;
}
uint64_t plat_thread_affinity(PlatThread *t, uint64_t mask) {
pthread_t th = t ? t->t : pthread_self();
cpu_set_t old, set;

View File

@@ -31,6 +31,10 @@ void plat_thread_detach(PlatThread *t); // frees t; the thread runs on
// Restricts t (NULL: the calling thread) to the logical processors in mask (bit n = processor n of the first group).
// Returns the previous mask, 0 on failure.
uint64_t plat_thread_affinity(PlatThread *t, uint64_t mask);
// The calling thread gets a core to itself: the process's other threads are kept off it and the caller on it (Windows:
// CPU sets, a performance core; Linux: the last processor of the affinity mask). Returns the logical processors
// reserved, 0 when it could not.
int plat_cpu_reserve(void);
uint32_t plat_cpu_count(void); // logical processors of the machine (system info)
uint32_t plat_cpu_usable(void); // the ones this process may run on (its affinity mask): size thread pools by it
uint64_t plat_thread_cpu_ns(uint32_t tid); // CPU time of the thread with id tid (plat_thread_id) so far; 0 unknown
@@ -154,6 +158,7 @@ void plat_cond_broadcast(PlatCond *c);
typedef struct { PlatMutex m; uint32_t owner, depth; } PlatRMutex;
#define PLAT_RMUTEX_INIT {PLAT_MUTEX_INIT, 0, 0}
void plat_rmutex_lock(PlatRMutex *m);
int plat_rmutex_trylock(PlatRMutex *m); // 1 when acquired (or already held by this thread)
void plat_rmutex_unlock(PlatRMutex *m);
// One-time initialisation: the first caller runs fn(arg), every other caller waits until it returned. Zero-initialised

View File

@@ -300,6 +300,66 @@ uint64_t plat_thread_affinity(PlatThread *t, uint64_t mask) {
return (uint64_t)SetThreadAffinityMask(t ? t->h : GetCurrentThread(), (DWORD_PTR)mask);
}
// The calling thread gets a core of its own through CPU sets: the process's default sets, which every thread without
// sets of its own runs on (those started later too), leave the core out, and the caller's selected sets are that
// core. The core is the highest-numbered one of the fastest efficiency class (a performance core on a hybrid CPU),
// with both its logical processors when it has two; only processors in group 0 and in the process's affinity mask
// count. CPU sets are soft: the scheduler may still use the core when nothing else wants it. Returns the logical
// processors reserved, 0 when Windows has no CPU sets (before Windows 10) or fewer than two cores qualify.
int plat_cpu_reserve(void) {
typedef BOOL(WINAPI * GetInfoFn)(PSYSTEM_CPU_SET_INFORMATION, ULONG, PULONG, HANDLE, ULONG);
typedef BOOL(WINAPI * SetProcFn)(HANDLE, const ULONG *, ULONG);
typedef BOOL(WINAPI * SetThreadFn)(HANDLE, const ULONG *, ULONG);
HMODULE k = GetModuleHandleW(L"kernel32.dll");
GetInfoFn get = (GetInfoFn)(void *)GetProcAddress(k, "GetSystemCpuSetInformation");
SetProcFn set_proc = (SetProcFn)(void *)GetProcAddress(k, "SetProcessDefaultCpuSets");
SetThreadFn set_thread = (SetThreadFn)(void *)GetProcAddress(k, "SetThreadSelectedCpuSets");
if (!get || !set_proc || !set_thread) return 0;
ULONG len = 0;
get(NULL, 0, &len, GetCurrentProcess(), 0);
uint8_t *buf = len ? malloc(len) : NULL;
if (!buf || !get((PSYSTEM_CPU_SET_INFORMATION)buf, len, &len, GetCurrentProcess(), 0)) {
free(buf);
return 0;
}
DWORD_PTR proc = 0, sys = 0;
if (!GetProcessAffinityMask(GetCurrentProcess(), &proc, &sys) || !proc) proc = ~(DWORD_PTR)0;
int best_class = -1, best_core = -1;
for (ULONG o = 0; o + sizeof(DWORD) * 2 <= len;) { // the core: fastest class, then highest index
const SYSTEM_CPU_SET_INFORMATION *s = (const void *)(buf + o);
if (!s->Size) break;
if (s->Type == CpuSetInformation && s->CpuSet.Group == 0 && s->CpuSet.LogicalProcessorIndex < 64 &&
(proc >> s->CpuSet.LogicalProcessorIndex & 1)) {
int cls = s->CpuSet.EfficiencyClass, core = s->CpuSet.CoreIndex;
if (cls > best_class || (cls == best_class && core > best_core)) best_class = cls, best_core = core;
}
o += s->Size;
}
ULONG rest[64], mine[8];
int nrest = 0, nmine = 0;
for (ULONG o = 0; o + sizeof(DWORD) * 2 <= len;) {
const SYSTEM_CPU_SET_INFORMATION *s = (const void *)(buf + o);
if (!s->Size) break;
if (s->Type == CpuSetInformation && s->CpuSet.Group == 0 && s->CpuSet.LogicalProcessorIndex < 64 &&
(proc >> s->CpuSet.LogicalProcessorIndex & 1)) {
if (s->CpuSet.EfficiencyClass == best_class && s->CpuSet.CoreIndex == best_core) {
if (nmine < 8) mine[nmine++] = s->CpuSet.Id;
} else if (nrest < 64) {
rest[nrest++] = s->CpuSet.Id;
}
}
o += s->Size;
}
free(buf);
if (!nmine || nrest < 2) return 0;
if (!set_proc(GetCurrentProcess(), rest, (ULONG)nrest)) return 0;
if (!set_thread(GetCurrentThread(), mine, (ULONG)nmine)) {
set_proc(GetCurrentProcess(), NULL, 0); // back to every processor
return 0;
}
return nmine;
}
uint32_t plat_cpu_count(void) {
DWORD n = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
return n ? (uint32_t)n : 1;