mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 04:59:57 -04:00
Port Ocarina of Time (Switch 2) to the recomp host
The NVN2 front end and the SM86 to Maxwell shader lowering (tools/xlate/AmpereLowering.cs, mesh and task stages, bindless texture pools, BRX jump tables, the NVN2 compute c0 layout) with four translator patches (0017-0020) bring the opening of the game to 58-60 fps on the Vulkan renderer. This round fixes the haze: SM86 IPA is lowered as a plain Maxwell IPA.PASS (texture coordinates were corrected for perspective twice); LDSM, TXD, both F2IP byte forms, HMMA, MOVM and HFMA2.RELU are lowered and spill mode has eight uniform register groups; conditional rendering keeps the newest counter report per address and treats a report as pending until its value is in guest memory, so occlusion tests and the depth clears around the depth copy behave; and the upscaler stand-in decodes the post pass's sRGB picture before writing the linear target. Tensor-core programs keep the stand-in unless NX2NAVI_NATIVE_TENSOR=1, because their lowering does not match the hardware yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -966,6 +966,13 @@ void plat_mem_fault_hook(int (*fn)(uint64_t addr)) {
|
||||
fault_install();
|
||||
}
|
||||
|
||||
void plat_crash_context(const char *s) { (void)s; } // the Windows crash notes only
|
||||
|
||||
int plat_frame_capture(PlatJmp *out, int (*want)(uintptr_t rip, void *arg), void *arg) { // not on Linux yet
|
||||
(void)out, (void)want, (void)arg;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void plat_crash_hook(void (*fn)(const PlatCrash *c)) {
|
||||
crash_fn = fn;
|
||||
fault_install();
|
||||
|
||||
@@ -78,6 +78,11 @@ typedef struct { jmp_buf b; } PlatJmp;
|
||||
#define plat_jmp_save(j) setjmp((j)->b)
|
||||
#define plat_jmp_go(j) longjmp((j)->b, 1)
|
||||
#endif
|
||||
// The host state of a caller further up this thread's stack, as it will be when the call it is making returns: the
|
||||
// stack is unwound frame by frame (from the caller of this function) until want(return address, arg) accepts one, and
|
||||
// that frame's callee-saved registers, stack pointer and return address go to *out for plat_jmp_go, which then makes
|
||||
// that call return a second time (eax 1). 1 on success, 0 when no frame is accepted (or on a platform without it).
|
||||
int plat_frame_capture(PlatJmp *out, int (*want)(uintptr_t rip, void *arg), void *arg);
|
||||
// Scheduling priority, Windows' seven levels: of the calling thread, or of t. On Linux a thread lowered without the
|
||||
// right to raise its priority again (CAP_SYS_NICE, or an RLIMIT_NICE of 20) could never return to normal, and the
|
||||
// runtime lowers threads that later take shared locks at normal priority; without that right every change is skipped
|
||||
@@ -239,6 +244,9 @@ typedef struct {
|
||||
const char *module; // the module holding rip: NULL for this executable, "" for none (a jump to address 0)
|
||||
} PlatCrash;
|
||||
void plat_crash_hook(void (*fn)(const PlatCrash *c));
|
||||
// What this thread is doing, named by the crash notes (a stack overflow leaves no room for the full report); NULL
|
||||
// clears it. s must stay valid until it is cleared.
|
||||
void plat_crash_context(const char *s);
|
||||
int plat_mem_read(uint64_t addr, void *out, size_t n); // copies n bytes, 0 instead of faulting on unmapped memory
|
||||
int plat_mem_readable(uint64_t addr, uint64_t n); // every page of [addr, addr + n) can be read; no fault
|
||||
void *plat_aligned_alloc(size_t size, size_t align); // free with plat_aligned_free; NULL on failure
|
||||
|
||||
@@ -262,6 +262,34 @@ __asm__(".text\n"
|
||||
" movl $1, %eax\n"
|
||||
" jmpq *72(%rcx)\n");
|
||||
|
||||
int plat_frame_capture(PlatJmp *out, int (*want)(uintptr_t rip, void *arg), void *arg) {
|
||||
CONTEXT c;
|
||||
RtlCaptureContext(&c);
|
||||
ULONG_PTR low, high;
|
||||
GetCurrentThreadStackLimits(&low, &high);
|
||||
for (int i = 0; i < 256 && c.Rsp >= low && c.Rsp + 8 <= high; i++) {
|
||||
DWORD64 img;
|
||||
RUNTIME_FUNCTION *fe = RtlLookupFunctionEntry(c.Rip, &img, NULL);
|
||||
if (fe) {
|
||||
void *data;
|
||||
DWORD64 frame;
|
||||
RtlVirtualUnwind(UNW_FLAG_NHANDLER, img, c.Rip, fe, &c, &data, &frame, NULL);
|
||||
} else { // a leaf: the return address is on top of the stack
|
||||
c.Rip = *(const DWORD64 *)c.Rsp;
|
||||
c.Rsp += 8;
|
||||
}
|
||||
if (!c.Rip) return 0;
|
||||
if (!want((uintptr_t)c.Rip, arg)) continue;
|
||||
out->gpr[0] = c.Rbx, out->gpr[1] = c.Rbp, out->gpr[2] = c.Rdi, out->gpr[3] = c.Rsi;
|
||||
out->gpr[4] = c.R12, out->gpr[5] = c.R13, out->gpr[6] = c.R14, out->gpr[7] = c.R15;
|
||||
out->gpr[8] = c.Rsp, out->gpr[9] = c.Rip;
|
||||
__asm__ volatile("stmxcsr %0\n\tfnstcw %1" : "=m"(out->mxcsr), "=m"(out->fpcw));
|
||||
memcpy(out->xmm, &c.Xmm6, 160); // xmm6-xmm15, consecutive in CONTEXT
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int plat_thread_spawn(PlatThreadFn fn, void *arg, const char *name, size_t stack) {
|
||||
PlatThread *t = plat_thread_new_ex(fn, arg, name, stack);
|
||||
if (t) plat_thread_detach(t);
|
||||
@@ -626,8 +654,33 @@ void plat_mem_fault_hook(int (*fn)(uint64_t addr)) {
|
||||
|
||||
static void (*crash_fn)(const PlatCrash *c);
|
||||
|
||||
// A line written straight to the standard error handle, for the crash path: no C runtime buffering or locks.
|
||||
static const char *image_name(const char *base);
|
||||
static __thread const char *crash_ctx;
|
||||
void plat_crash_context(const char *s) { crash_ctx = s; }
|
||||
static void raw_note(const char *tag, DWORD code, uintptr_t rip, uintptr_t addr) {
|
||||
char b[256];
|
||||
void *base = NULL;
|
||||
RtlPcToFileHeader((void *)rip, &base);
|
||||
ULONG_PTR lo = 0, hi = 0;
|
||||
GetCurrentThreadStackLimits(&lo, &hi);
|
||||
int n = snprintf(b, sizeof b, "[host] %s: exception %#lx at %p (%s+%#llx), address %p, thread %lu, stack %llu KiB reserved\n", tag,
|
||||
(unsigned long)code, (void *)rip, base ? image_name(base) : "no module",
|
||||
(unsigned long long)(rip - (uintptr_t)base), (void *)addr, (unsigned long)GetCurrentThreadId(),
|
||||
(unsigned long long)(hi - lo) >> 10);
|
||||
DWORD w;
|
||||
if (n > 0) WriteFile(GetStdHandle(STD_ERROR_HANDLE), b, (DWORD)n, &w, NULL);
|
||||
if (crash_ctx) {
|
||||
n = snprintf(b, sizeof b, "[host] %s: the thread was %s\n", tag, crash_ctx);
|
||||
if (n > 0) WriteFile(GetStdHandle(STD_ERROR_HANDLE), b, (DWORD)n, &w, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static LONG CALLBACK crash_veh(EXCEPTION_POINTERS *ep) {
|
||||
DWORD code = ep->ExceptionRecord->ExceptionCode;
|
||||
if (code == EXCEPTION_ACCESS_VIOLATION || code == EXCEPTION_STACK_OVERFLOW || code == EXCEPTION_ILLEGAL_INSTRUCTION)
|
||||
raw_note("crash_veh", code, (uintptr_t)ep->ContextRecord->Rip,
|
||||
ep->ExceptionRecord->NumberParameters >= 2 ? (uintptr_t)ep->ExceptionRecord->ExceptionInformation[1] : 0);
|
||||
const char *what = code == EXCEPTION_ACCESS_VIOLATION ? "access violation"
|
||||
: code == EXCEPTION_ILLEGAL_INSTRUCTION ? "illegal instruction"
|
||||
: code == EXCEPTION_INT_DIVIDE_BY_ZERO ? "integer division by zero"
|
||||
@@ -643,6 +696,13 @@ static LONG CALLBACK crash_veh(EXCEPTION_POINTERS *ep) {
|
||||
PlatCrash c = {what, (uint32_t)code, rip, rip - (uintptr_t)mi.lpBaseOfDll, -1, 0};
|
||||
if (code == EXCEPTION_ACCESS_VIOLATION && ep->ExceptionRecord->NumberParameters >= 2)
|
||||
c.access = (int)ep->ExceptionRecord->ExceptionInformation[0], c.addr = ep->ExceptionRecord->ExceptionInformation[1];
|
||||
{ // the faulting host stack, so the report can name the runtime frame that took it
|
||||
void *f[32];
|
||||
USHORT n = RtlCaptureStackBackTrace(0, 32, f, NULL);
|
||||
for (USHORT i = 0; i < n && c.nframes < (int)(sizeof c.frames / sizeof *c.frames); i++)
|
||||
if ((uintptr_t)f[i] - (uintptr_t)mi.lpBaseOfDll < mi.SizeOfImage)
|
||||
c.frames[c.nframes++] = (uintptr_t)f[i];
|
||||
}
|
||||
crash_fn(&c);
|
||||
return EXCEPTION_CONTINUE_SEARCH;
|
||||
}
|
||||
@@ -708,6 +768,7 @@ static LONG WINAPI crash_filter(EXCEPTION_POINTERS *ep) {
|
||||
const EXCEPTION_RECORD *er = ep->ExceptionRecord;
|
||||
DWORD code = er->ExceptionCode;
|
||||
uintptr_t rip = (uintptr_t)ep->ContextRecord->Rip;
|
||||
raw_note("crash_filter", code, rip, er->NumberParameters >= 2 ? (uintptr_t)er->ExceptionInformation[1] : 0);
|
||||
PlatCrash c = {code == EXCEPTION_ACCESS_VIOLATION ? "access violation"
|
||||
: code == EXCEPTION_ILLEGAL_INSTRUCTION ? "illegal instruction"
|
||||
: code == EXCEPTION_INT_DIVIDE_BY_ZERO ? "integer division by zero"
|
||||
@@ -754,6 +815,7 @@ void plat_crash_hook(void (*fn)(const PlatCrash *c)) {
|
||||
// A fresh process gets a fresh layout: Windows places DLLs, heaps and thread stacks at random addresses. The parent
|
||||
// waits and exits with the child's code.
|
||||
void plat_reexec(void) {
|
||||
fprintf(stderr, "[host] memory: a fixed region is taken; re-running the process\n");
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
STARTUPINFOW si = {.cb = sizeof si};
|
||||
|
||||
Reference in New Issue
Block a user