From f0ee820f225153ea6e5ac0007fba71b648eb06e5 Mon Sep 17 00:00:00 2001 From: Vitalii Apostolyuk Date: Sat, 7 Nov 2026 21:31:47 -0600 Subject: [PATCH] feat(oot): live output resolution up to the game's 2560x1440 swapchain Ocarina of Time's output size (layer render targets and the NVN crop) is DisplayConfig's base size, written only by setBaseSize at main+0x1087f40. Two verified veneers redirect it to a host-chosen size: one at setBaseSize's entry, one in the per-frame cfg update at main+0x6502f8 that calls setBaseSize when the request changes, so the engine's own resize path (the one dock/undock uses) re-creates the targets and the 3D scene follows the output every frame. Both original words are checked, as is the padding after the frame-step stub and the loader's parameter page. The Resolution row in the settings panel, the "resolution WxH" input command and TOTK_OUTPUT_RES now work for Ocarina of Time: 16:9, both sides multiples of 8, from 640x360 up to the game's own 2560x1440 swapchain. The default stays the shipped 2048x1152 and TOTK_OUTPUT_RES=0 skips the patch. Aspect stays 16:9. TOTK_OOT_DISPLAY_LOG=1 logs request vs. what setBaseSize was given (off by default). runtime_output_size_valid is shared by the panel's choices and Custom input; Bananza's and Pokopia's acceptance rule is unchanged. Co-Authored-By: Claude Opus 5.5 --- games/totk/runtime/frontend.m | 2 +- games/totk/runtime/game_patches_ns2.c | 90 ++++++++++++++++++++++++--- games/totk/runtime/input.c | 2 +- games/totk/runtime/ns2_resolution.h | 9 +++ games/totk/runtime/oot_display.h | 49 +++++++++++++++ games/totk/runtime/runtime.h | 1 + games/totk/runtime/settings_panel.h | 30 +++++++-- tests/test_ns2_resolution.py | 89 ++++++++++++++++++++++++++ 8 files changed, 254 insertions(+), 18 deletions(-) create mode 100644 games/totk/runtime/oot_display.h diff --git a/games/totk/runtime/frontend.m b/games/totk/runtime/frontend.m index c125727fd..3efdaf306 100644 --- a/games/totk/runtime/frontend.m +++ b/games/totk/runtime/frontend.m @@ -890,7 +890,7 @@ void runtime_front_run(void(*callback)(void)){ /* Default window: the docked output size, 1920x1080, scaled down (16:9 kept) when the screen's visible area is smaller. */ /* TOTK_WINDOW_SIZE=x (test scripts only, e.g. the 2K output test; the desktop launcher never sets it): another starting size. */ NSRect vis=NSScreen.mainScreen?NSScreen.mainScreen.visibleFrame:NSMakeRect(0,0,1920,1080+28);CGFloat ww=1920,wh=1080; -#if defined(GAME_DKB) || defined(GAME_POKOPIA) +#if defined(GAME_DKB) || defined(GAME_POKOPIA) || defined(GAME_OOT) {uint64_t size=runtime_output_size_current();unsigned w=(unsigned)size,h=(unsigned)(size>>32);if(w&&h){ww=w;wh=h;}} #endif {const char *e=getenv("TOTK_WINDOW_SIZE");int w=0,h=0;if(e&&sscanf(e,"%dx%d",&w,&h)==2&&w>=320&&h>=180&&w<=8192&&h<=8192){ww=w;wh=h;}} diff --git a/games/totk/runtime/game_patches_ns2.c b/games/totk/runtime/game_patches_ns2.c index 38eb5fa6d..a6d7bd203 100644 --- a/games/totk/runtime/game_patches_ns2.c +++ b/games/totk/runtime/game_patches_ns2.c @@ -16,6 +16,7 @@ #include "ns2_resolution.h" #include "bananza_display.h" #include "pokopia_display.h" +#include "oot_display.h" #ifdef GAME_OOT /* Ocarina of Time's game time: each frame main+0x6e6cc4 takes the step as 60 / fps (fps = the frame-rate controller's, whole divisors of 60), in units of 1/60 s, adds it @@ -26,9 +27,11 @@ static _Atomic uint32_t oot_step_bits; /* a float: seconds of game time for th static uint64_t oot_last_ns, oot_report_ns, oot_frames; static double oot_game_s, oot_scale = 1, oot_samples[5]; static unsigned oot_sample_count, oot_sample_next; +static void oot_display_watch(void); /* patch_oot_display's diagnostic, once a frame */ void runtime_frame_step_pin(double ms) { (void)ms; } void runtime_frame_step_update(uint64_t now_ns) { if (!now_ns) return; + oot_display_watch(); if (!oot_last_ns) { oot_last_ns = oot_report_ns = now_ns; return; } double dt = (double)(now_ns - oot_last_ns) * 1e-9; oot_last_ns = now_ns; @@ -84,7 +87,15 @@ void runtime_frame_step_pin(double ms) { (void)ms; } void runtime_frame_step_update(uint64_t now_ns) { (void)now_ns; } #endif -#if defined(GAME_DKB) || defined(GAME_POKOPIA) +#ifdef GAME_OOT +/* Ocarina of Time v1: its sizes and limits are ns2_resolution.h's (oot_output_size_fits); the default is the shipped (docked) size. */ +#define OUTPUT_DEFAULT "2048x1152" +#define OUTPUT_FITS(w,h) oot_output_size_fits(w,h) +#else +#define OUTPUT_DEFAULT "1920x1080" +#define OUTPUT_FITS(w,h) 1 +#endif +#if defined(GAME_DKB) || defined(GAME_POKOPIA) || defined(GAME_OOT) static pthread_once_t output_once=PTHREAD_ONCE_INIT; static unsigned output_w,output_h; static unsigned output_start_w,output_start_h; @@ -93,22 +104,24 @@ static _Atomic(uint64_t *) output_page; static void output_initialize(void) { const char *e = getenv("TOTK_OUTPUT_RES"); #if TARGET_OS_IPHONE - if(!e||!*e)e="1920x1080"; + if(!e||!*e)e=OUTPUT_DEFAULT; #endif #if !TARGET_OS_IPHONE char saved[32]; if (!e || !*e) { if(runtime_setting_word("graphics-output-size.txt",saved,sizeof saved)){ - if(ns2_parse_saved_resolution(saved,&output_w,&output_h)){saved[strcspn(saved,"\r\n")]=0;e=saved;} + if(ns2_parse_saved_resolution(saved,&output_w,&output_h)&&OUTPUT_FITS(output_w,output_h)){saved[strcspn(saved,"\r\n")]=0;e=saved;} else - fprintf(stderr,"[totk] invalid saved render resolution; using 1920x1080\n"); + fprintf(stderr,"[totk] invalid saved render resolution; using " OUTPUT_DEFAULT "\n"); } - if(!e||!*e)e="1920x1080"; + if(!e||!*e)e=OUTPUT_DEFAULT; } #endif if (!strcmp(e, "0")) { #ifdef GAME_DKB output_start_w=2880;output_start_h=1620; +#elif defined(GAME_OOT) + output_start_w=OOT_OUTPUT_SHIPPED_W;output_start_h=OOT_OUTPUT_SHIPPED_H; #else output_start_w=1920;output_start_h=1080; #endif @@ -118,7 +131,13 @@ static void output_initialize(void) { runtime_fail("TOTK_OUTPUT_RES must be x, each 1..16384 (Metal texture extent)"); output_has_size=1; output_start_w=output_w;output_start_h=output_h; -#if !TARGET_OS_IPHONE +#ifdef GAME_OOT + if(!OUTPUT_FITS(output_w,output_h)){ + char m[200];snprintf(m,sizeof m,"TOTK_OUTPUT_RES: Ocarina of Time takes 16:9, both sides multiples of %u, from %ux%u to %ux%u (its swapchain), " + "e.g. 1920x1080; 0 = as shipped",OOT_OUTPUT_ALIGN,OOT_OUTPUT_MIN_W,OOT_OUTPUT_MIN_H,OOT_OUTPUT_MAX_W,OOT_OUTPUT_MAX_H);runtime_fail(m); + } + output_w=OOT_OUTPUT_MAX_W;output_h=OOT_OUTPUT_MAX_H; +#elif !TARGET_OS_IPHONE /* Reserve the largest supported canvas once; render at the saved extent. Bananza also reserves its matching gsys pool and parent heap at startup. */ #ifdef GAME_DKB @@ -133,7 +152,7 @@ static void output_initialize(void) { } #endif int runtime_output_res(unsigned *w, unsigned *h) { -#if defined(GAME_DKB) || defined(GAME_POKOPIA) +#if defined(GAME_DKB) || defined(GAME_POKOPIA) || defined(GAME_OOT) pthread_once(&output_once,output_initialize); if(!output_has_size)return 0; *w=output_w;*h=output_h;return 1; @@ -150,7 +169,7 @@ uint64_t runtime_output_res_extra_bytes(void) { return 0; } uint64_t runtime_output_size_current(void) { -#if defined(GAME_DKB) || defined(GAME_POKOPIA) +#if defined(GAME_DKB) || defined(GAME_POKOPIA) || defined(GAME_OOT) pthread_once(&output_once,output_initialize); uint64_t *p=atomic_load(&output_page); return p?atomic_load((_Atomic uint64_t *)(p+1)):((uint64_t)output_start_h<<32|output_start_w); @@ -158,10 +177,16 @@ uint64_t runtime_output_size_current(void) { return 1920ull|(1080ull<<32); #endif } -#if defined(GAME_DKB) || defined(GAME_POKOPIA) +#if defined(GAME_DKB) || defined(GAME_POKOPIA) || defined(GAME_OOT) +/* The one rule for a live size: a parameter page, within this run's capacity, and the game's own shape (OUTPUT_FITS). */ +int runtime_output_size_valid(unsigned w,unsigned h) { + unsigned mw,mh; + if(!atomic_load(&output_page)||!w||!h||!runtime_output_res(&mw,&mh)||w>mw||h>mh)return 0; + return OUTPUT_FITS(w,h); +} int runtime_output_size_set(unsigned w,unsigned h) { unsigned mw,mh;uint64_t *p=atomic_load(&output_page); - if(!p||!w||!h||!runtime_output_res(&mw,&mh)||w>mw||h>mh)return 0; + if(!p||!runtime_output_size_valid(w,h)||!runtime_output_res(&mw,&mh))return 0; atomic_store((_Atomic uint64_t *)(p+1),(uint64_t)h<<32|w); #if !TARGET_OS_IPHONE void runtime_front_resize_output(unsigned,unsigned);runtime_front_resize_output(w,h); @@ -411,6 +436,50 @@ static void patch_pokopia_display(Module *m) { } #endif +#ifdef GAME_OOT +/* Ocarina of Time's live output size (oot_display.h): setBaseSize main+0x1087f40 takes the host's size whatever it is asked for (the + docked/handheld scale and the per-tier clamp main+0x112c5e0 no longer apply), and the per-frame cfg update main+0x6502f8 calls it when + the base differs from the request; the engine then re-creates the layer targets, moves the crop, and the scene follows. The swapchain + stays the game's 2560x1440 (main+0x1202ee0): that is the capacity. Veneers after the frame step's stub, below rodata (main+0x3c47000). */ +static int oot_display_log; /* TOTK_OOT_DISPLAY_LOG=1: each change of the request (p[1]) or of the size setBaseSize was last given (p[2]) */ +static void oot_display_watch(void) { /* the page only: the game's DisplayConfig (p[3]) has no lifetime the host thread can rely on */ + static uint64_t last_want,last_base;if(!oot_display_log)return; + uint64_t *p=atomic_load_explicit(&output_page,memory_order_acquire);if(!p)return; + uint64_t want=atomic_load_explicit((_Atomic uint64_t *)(p+1),memory_order_relaxed),base=atomic_load_explicit((_Atomic uint64_t *)(p+2),memory_order_relaxed); + if(want==last_want&&base==last_base)return; + last_want=want;last_base=base; + fprintf(stderr,"[totk] Ocarina of Time display: requested %ux%u, setBaseSize last given %ux%u\n",(unsigned)want,(unsigned)(want>>32),(unsigned)base,(unsigned)(base>>32)); +} +static void patch_oot_display(Module *m) { + unsigned mw,mh; + if(!runtime_output_res(&mw,&mh)){fprintf(stderr,"[totk] Ocarina of Time output: as shipped, " OUTPUT_DEFAULT " (TOTK_OUTPUT_RES=0)\n");return;} + enum { BASE_SITE=0x1087f40, FRAME_SITE=0x6502f8 }; + const uint64_t page=(m->file_size+0x3fff)&~0x3fffULL,stub=((m->text_end+15)&~15ULL)+0x20,frame_stub=stub+0x20; + if(page+0x4000!=m->size||frame_stub+OOT_FRAME_WORDS*4>m->ro_off)runtime_fail("Ocarina of Time display patch layout"); + uint32_t word;memcpy(&word,m->base+BASE_SITE,4); + if(word!=0x29422408u)runtime_fail("Ocarina of Time setBaseSize (main+0x1087f40) differs from verified 1.0.0"); + memcpy(&word,m->base+FRAME_SITE,4); + if(word!=0x2941240bu)runtime_fail("Ocarina of Time display update (main+0x6502f8) differs from verified 1.0.0"); + for(uint64_t k=stub;kbase[k])runtime_fail("Ocarina of Time display text padding in use"); + uint64_t *p=(void *)(m->base+page);for(unsigned k=0;k<4;k++)if(p[k])runtime_fail("Ocarina of Time display parameter page in use"); + p[0]=(uint64_t)mh<<32|mw;p[1]=p[2]=(uint64_t)output_start_h<<32|output_start_w; /* p[0]: the capacity, a record for the host side (Pokopia's page layout); no veneer reads it */ + atomic_store(&output_page,p); + uint32_t base[OOT_BASE_WORDS],frame[OOT_FRAME_WORDS]; + oot_base_code(base,(uintptr_t)m->base+stub,(uintptr_t)p,(uintptr_t)m->base+BASE_SITE+4); + oot_frame_code(frame,(uintptr_t)m->base+frame_stub,(uintptr_t)p,(uintptr_t)m->base+FRAME_SITE+4,(uintptr_t)m->base+BASE_SITE); + memcpy(m->base+stub,base,sizeof base);memcpy(m->base+frame_stub,frame,sizeof frame); + uint32_t jump=bananza_display_branch(BASE_SITE,stub);memcpy(m->base+BASE_SITE,&jump,4); + jump=bananza_display_branch(FRAME_SITE,frame_stub);memcpy(m->base+FRAME_SITE,&jump,4); + __builtin___clear_cache((char *)m->base+FRAME_SITE,(char *)m->base+FRAME_SITE+4);__builtin___clear_cache((char *)m->base+BASE_SITE,(char *)m->base+BASE_SITE+4); + __builtin___clear_cache((char *)m->base+stub,(char *)m->base+frame_stub+sizeof frame); + const char *e=getenv("TOTK_OOT_DISPLAY_LOG");oot_display_log=e&&!strcmp(e,"1"); + fprintf(stderr,"[totk] Ocarina of Time output: %ux%u (capacity %ux%u) at main+%#x/+%#x; change it live in the settings panel (Resolution) or with \"resolution WxH\" " + "in the input file, at start with TOTK_OUTPUT_RES=WxH (16:9, both sides multiples of %u, %ux%u up to the game's %ux%u swapchain; 0 = as shipped; " + "TOTK_OOT_DISPLAY_LOG=1 logs each request and what setBaseSize was given)\n",output_start_w,output_start_h,mw,mh,(unsigned)BASE_SITE,(unsigned)FRAME_SITE, + OOT_OUTPUT_ALIGN,OOT_OUTPUT_MIN_W,OOT_OUTPUT_MIN_H,OOT_OUTPUT_MAX_W,OOT_OUTPUT_MAX_H); +} +#endif + void runtime_game_patches(Module *m) { if (strcmp(m->name, "main")) runtime_fail("game patches expect the main module"); if (getenv("TOTK_PCHTXT") || getenv("TOTK_ASPECT")) @@ -421,6 +490,7 @@ void runtime_game_patches(Module *m) { patch_pokopia_display(m); #elif defined(GAME_OOT) patch_oot_frame_step(m); + patch_oot_display(m); #endif const char *a = getenv("TOTK_EXCL_ACQUIRE"); if (TARGET_OS_IPHONE || getenv("TOTK_EXPORT_IOS") || !a || strcmp(a, "0")) { diff --git a/games/totk/runtime/input.c b/games/totk/runtime/input.c index 0c8b6396e..f52cf9a80 100644 --- a/games/totk/runtime/input.c +++ b/games/totk/runtime/input.c @@ -291,7 +291,7 @@ static void *input_file(void *arg) { if(!vr_set_resolution(w,h))fprintf(stderr,"[totk] live VR render resolution rejected: %dx%d\n",w,h); } else fprintf(stderr,"[totk] vr_resolution command needs widthxheight per eye\n"); #endif -#if defined(GAME_DKB) || defined(GAME_POKOPIA) +#if defined(GAME_DKB) || defined(GAME_POKOPIA) || defined(GAME_OOT) } else if (!strncmp(line,"resolution ",11)) { unsigned w,h;int n=0; if(sscanf(line+11,"%ux%u %n",&w,&h,&n)==2&&n&&!line[11+n]) { diff --git a/games/totk/runtime/ns2_resolution.h b/games/totk/runtime/ns2_resolution.h index 5e1e6f2fd..22f8a3ab0 100644 --- a/games/totk/runtime/ns2_resolution.h +++ b/games/totk/runtime/ns2_resolution.h @@ -29,6 +29,15 @@ static inline int ns2_parse_saved_resolution(const char *s,unsigned *w,unsigned return ns2_parse_resolution(word,w,h); } +/* Ocarina of Time v1's sizes (game_patches_ns2.c runtime_output_size_valid, the settings panel's text): 16:9 (the camera and UI were not + traced for other shapes), both sides on the scene's grid (main+0x654e70 floors it to view+0x5330 multiples, believed 8), from 640x360 + up to the game's own swapchain, 2560x1440 (main+0x1202ee0, created once; the crop it publishes must never exceed it). Shipped: 2048x1152. */ +enum { OOT_OUTPUT_ALIGN=8, OOT_OUTPUT_MIN_W=640, OOT_OUTPUT_MIN_H=360, OOT_OUTPUT_MAX_W=2560, OOT_OUTPUT_MAX_H=1440, OOT_OUTPUT_SHIPPED_W=2048, OOT_OUTPUT_SHIPPED_H=1152 }; +static inline int oot_output_size_fits(unsigned w,unsigned h) { + return w>=OOT_OUTPUT_MIN_W&&h>=OOT_OUTPUT_MIN_H&&w<=OOT_OUTPUT_MAX_W&&h<=OOT_OUTPUT_MAX_H&&!(w%OOT_OUTPUT_ALIGN)&&!(h%OOT_OUTPUT_ALIGN) + &&(w*9>h*16?w*9-h*16:h*16-w*9)<=16; /* 16:9 within 16/9 of a pixel */ +} + /* Compare rounded pixels before fitting: float 32/9 is slightly above its exact value, but a 3840x1080 canvas still fits the 3840-pixel capacity. */ static inline int ns2_aspect_size(float ratio,unsigned height,unsigned max_w,unsigned max_h,unsigned *w,unsigned *h) { diff --git a/games/totk/runtime/oot_display.h b/games/totk/runtime/oot_display.h new file mode 100644 index 000000000..b613515dc --- /dev/null +++ b/games/totk/runtime/oot_display.h @@ -0,0 +1,49 @@ +/* Ocarina of Time 1.0.0: the output size (layer render targets and the NVN crop) is DisplayConfig's base size cfg+0x10, written only by + * setBaseSize(cfg, const int2 *size) at main+0x1087f40 (sets cfg+8 bit 21 when it changes; a leaf that clobbers w8-w11 only). Its callers are + * the display init (main+0x1087c68) and the docked/handheld recompute in the per-frame cfg update (main+0x650600). On bit 21 that update + * republishes the crop (main+0x650540 -> main+0xe9a850), main+0x652260 sizes layer 0 from cfg+0x10, and main+0x6549d0 -> main+0x6531c0 -> + * main+0x9cd030 re-creates its targets: the engine's own resize path. The scene follows the layer every frame (main+0x654e70). + * Page after main (loader.c): p[0] capacity, p[1] host request (h<<32|w, stored atomically), p[2] the size setBaseSize was last given, p[3] cfg. */ +#pragma once +#include "bananza_display.h" +enum { OOT_BASE_WORDS=7, OOT_FRAME_WORDS=11 }; +/* At setBaseSize's entry: every request becomes the host's size, snapshotted once into p[2] (the function reads *x1 three times). */ +static inline void oot_base_code(uint32_t code[OOT_BASE_WORDS],uintptr_t stub,uintptr_t page,uintptr_t resume) { + const uint32_t words[OOT_BASE_WORDS]={ + 0x90000010, /* adrp x16, page (relocated below) */ + 0x91002210, /* add x16,x16,#8 */ + 0xc8dffe11, /* ldar x17,[x16]: the request */ + 0xa9008211, /* stp x17,x0,[x16,#8]: p[2] = this call's size, p[3] = cfg */ + 0x91002201, /* add x1,x16,#8 */ + 0x29422408, /* original ldp w8,w9,[x0,#0x10] */ + 0x14000000 /* b main+0x1087f44 (relocated below) */ + }; + memcpy(code,words,sizeof words); + int64_t delta=((int64_t)(page&~4095ULL)-(int64_t)(stub&~4095ULL))/4096; + uint32_t imm=(uint32_t)delta&0x1fffff; + code[0]=0x90000010|((imm&3)<<29)|((imm>>2)<<5); + code[OOT_BASE_WORDS-1]=bananza_display_branch(stub+sizeof words-4,resume); +} +/* main+0x6502f8, the cfg update after its prologue (x19-x22, x29, x30 saved; x0 = cfg, x1 live until main+0x65030c; x8-x11 and the flags + * written before they are read): a base that differs from the request calls setBaseSize, so this same update sees bit 21. */ +static inline void oot_frame_code(uint32_t code[OOT_FRAME_WORDS],uintptr_t stub,uintptr_t page,uintptr_t resume,uintptr_t set_base) { + const uint32_t words[OOT_FRAME_WORDS]={ + 0x90000010, /* adrp x16, page (relocated below) */ + 0x91002210, /* add x16,x16,#8 */ + 0xc8dffe11, /* ldar x17,[x16]: the request */ + 0xf9400810, /* ldr x16,[x0,#0x10]: the base */ + 0xeb11021f, /* cmp x16,x17 */ + 0x54000080, /* b.eq original */ + 0xa9bf7be1, /* stp x1,x30,[sp,#-16]! */ + 0x94000000, /* bl setBaseSize (relocated below; x0 kept) */ + 0xa8c17be1, /* ldp x1,x30,[sp],#16 */ + 0x2941240b, /* original ldp w11,w9,[x0,#8] */ + 0x14000000 /* b main+0x6502fc (relocated below) */ + }; + memcpy(code,words,sizeof words); + int64_t delta=((int64_t)(page&~4095ULL)-(int64_t)(stub&~4095ULL))/4096; + uint32_t imm=(uint32_t)delta&0x1fffff; + code[0]=0x90000010|((imm&3)<<29)|((imm>>2)<<5); + code[7]=bananza_display_branch(stub+28,set_base)|0x80000000u; + code[OOT_FRAME_WORDS-1]=bananza_display_branch(stub+sizeof words-4,resume); +} diff --git a/games/totk/runtime/runtime.h b/games/totk/runtime/runtime.h index 218629a60..0edb13e90 100644 --- a/games/totk/runtime/runtime.h +++ b/games/totk/runtime/runtime.h @@ -123,6 +123,7 @@ int runtime_output_mode_set(int mode); /* returns 0 where that int runtime_output_native(int mode, unsigned *w, unsigned *h); /* the canvas of OUTPUT_MODE_NATIVE / _NATIVE_METALFX / _NATIVE_4K; 0 when not offered */ uint64_t runtime_output_size_current(void); /* configured canvas: width in low 32 bits, height in high 32 bits */ int runtime_output_size_set(unsigned w,unsigned h); /* NS2: queue the native game-thread display rebuild */ +int runtime_output_size_valid(unsigned w,unsigned h); /* NS2: 1 when runtime_output_size_set takes this size (the settings panel offers only these) */ void runtime_warm_progress(uint64_t done,uint64_t total,uint64_t shaders,uint64_t shader_total,uint64_t pipelines,uint64_t pipeline_total); void runtime_warm_done(void); void runtime_warm_title(void); /* nvn_mtl.m warm_start: the title may be seen now, the rest of the warm-up behind it or done */ diff --git a/games/totk/runtime/settings_panel.h b/games/totk/runtime/settings_panel.h index 37fe4b4fc..375f6a28a 100644 --- a/games/totk/runtime/settings_panel.h +++ b/games/totk/runtime/settings_panel.h @@ -120,7 +120,7 @@ #include "controller_map.h" /* the controller layout */ #include "feedback_settings.h" /* feedback_word: ssao.txt */ #include "users.h" /* the local users (runtime/account.c): the User page */ -#if defined(GAME_DKB) || defined(GAME_POKOPIA) +#if defined(GAME_DKB) || defined(GAME_POKOPIA) || defined(GAME_OOT) #include "ns2_resolution.h" #endif @@ -369,26 +369,31 @@ static TOTKSettingRow *settings_fixed(NSString *title, NSString *value) { return settings_row(title,@[value],@[@0],^int{return 0;},nil); } static NSString *settings_on_off(int on) { return on?settings_text("开","On"):settings_text("关","Off"); } -#if defined(GAME_DKB) || defined(GAME_POKOPIA) +#if defined(GAME_DKB) || defined(GAME_POKOPIA) || defined(GAME_OOT) /* The game consumes this request in its native display calculation. */ static void settings_ns2_save_size(void) { uint64_t s=runtime_output_size_current();unsigned w=(unsigned)s,h=(unsigned)(s>>32);char word[32]; snprintf(word,sizeof word,"%ux%u",w,h);runtime_setting_save("graphics-output-size.txt",word,"native render resolution"); +#ifndef GAME_OOT /* Ocarina of Time keeps 16:9 (runtime_aspect_ratio 0): no ratio to save */ snprintf(word,sizeof word,"%.8g",h?(double)w/h:16.0/9);runtime_setting_save("graphics-aspect.txt",word,"aspect ratio"); +#endif } static TOTKSettingRow *settings_ns2_resolution_row(void) { NSString *title=settings_text("分辨率","Resolution"); uint64_t current=runtime_output_size_current();unsigned w=(unsigned)current,h=(unsigned)(current>>32); NSString *picked=[NSString stringWithFormat:@"%ux%u",w,h]; NSMutableArray *sizes=[NSMutableArray array],*names=[NSMutableArray array]; - unsigned mw=3840,mh=2160;runtime_output_res(&mw,&mh); + unsigned mw=3840,mh=2160;int capacity=runtime_output_res(&mw,&mh); +#ifdef GAME_OOT /* as shipped (TOTK_OUTPUT_RES=0): no parameter page, so nothing to change live */ + if(!capacity)return settings_fixed(title,[picked stringByReplacingOccurrencesOfString:@"x" withString:@"×"]); +#endif const unsigned heights[]={540,720,1080,1440,2160}; const char *labels[]={"540p","720p","1080p","2K","4K"}; for(unsigned k=0;k<5;k++){ unsigned nh=heights[k],nw=h?(unsigned)(((uint64_t)w*nh+h/2)/h):nh*16/9; if(nw>mw){nh=w?(unsigned)((uint64_t)mw*h/w):0;nw=h?(unsigned)(((uint64_t)w*nh+h/2)/h):0;} if(nh>mh){nw=(unsigned)((uint64_t)nw*mh/nh);nh=mh;} - if(!nw||!nh)continue; + if(!nw||!nh||(capacity&&!runtime_output_size_valid(nw,nh)))continue; /* only what the game takes (Ocarina of Time: not 960x540, off its 8-pixel grid) */ NSString *s=[NSString stringWithFormat:@"%ux%u",nw,nh];if([sizes containsObject:s])continue; [sizes addObject:s];[names addObject:nh==heights[k]?[NSString stringWithFormat:@"%s (%u×%u)",labels[k],nw,nh]:[NSString stringWithFormat:@"%u×%u",nw,nh]]; } @@ -400,7 +405,13 @@ static TOTKSettingRow *settings_ns2_resolution_row(void) { NSString *size=value>=0&&value<(int)sizes.count?sizes[(NSUInteger)value]:nil; if(value<0){ NSAlert *alert=[NSAlert new];alert.messageText=settings_text("自定义渲染分辨率","Custom render resolution"); +#ifdef GAME_OOT /* ns2_resolution.h oot_output_size_fits */ + alert.informativeText=[NSString stringWithFormat:settings_text("输入宽x高:16:9,宽高均为 %u 的倍数,%u×%u 至 %u×%u,例如%ux%u。保存后立即应用。", + "Enter widthxheight: 16:9, both sides multiples of %u, from %u×%u up to %u×%u, e.g. %ux%u. Applies immediately."), + (unsigned)OOT_OUTPUT_ALIGN,(unsigned)OOT_OUTPUT_MIN_W,(unsigned)OOT_OUTPUT_MIN_H,mw,mh,mw,mh]; +#else alert.informativeText=settings_text("输入宽x高,例如3840x2160。保存后立即应用。","Enter widthxheight, e.g. 3840x2160. Applies immediately."); +#endif NSTextField *field=[[NSTextField alloc] initWithFrame:NSMakeRect(0,0,240,24)];field.stringValue=picked;alert.accessoryView=field; [alert addButtonWithTitle:settings_text("保存","Save")];[alert addButtonWithTitle:settings_text("取消","Cancel")]; if([alert runModal]!=NSAlertFirstButtonReturn){dispatch_async(dispatch_get_main_queue(),^{settings_rebuild();});return;} @@ -411,10 +422,17 @@ static TOTKSettingRow *settings_ns2_resolution_row(void) { NSAlert *error=[NSAlert new];error.messageText=settings_text("分辨率格式无效","Invalid resolution"); error.informativeText=settings_text("请输入宽x高,每边1–16384。原设置保留。","Use widthxheight, each 1–16384. The previous choice is kept.");[error runModal];dispatch_async(dispatch_get_main_queue(),^{settings_rebuild();});return; } - if(!runtime_output_size_set(nw,nh)){ + if(!runtime_output_size_valid(nw,nh)||!runtime_output_size_set(nw,nh)){ NSAlert *error=[NSAlert new];error.messageText=settings_text("分辨率超出本次启动容量","Resolution exceeds this run's capacity"); unsigned mw=0,mh=0;runtime_output_res(&mw,&mh); error.informativeText=[NSString stringWithFormat:settings_text("本次最高支持 %u×%u。原设置保留。","This run supports up to %u×%u. The previous choice is kept."),mw,mh]; +#ifdef GAME_OOT /* within the capacity, but off the shape or the grid (ns2_resolution.h oot_output_size_fits) */ + if(nw<=mw&&nh<=mh){ + error.messageText=settings_text("不支持该分辨率","Not a supported size"); + error.informativeText=[NSString stringWithFormat:settings_text("仅支持 16:9,宽高均为 %u 的倍数,%u×%u 至 %u×%u。原设置保留。", + "Use 16:9, both sides multiples of %u, from %u×%u up to %u×%u. The previous choice is kept."),(unsigned)OOT_OUTPUT_ALIGN,(unsigned)OOT_OUTPUT_MIN_W,(unsigned)OOT_OUTPUT_MIN_H,mw,mh]; + } +#endif [error runModal];dispatch_async(dispatch_get_main_queue(),^{settings_rebuild();});return; } settings_ns2_save_size(); @@ -866,7 +884,7 @@ static NSArray *settings_rows(void) { vr_eye_sizing=vr_enabled(); #endif if(!vr_eye_sizing){ -#if defined(GAME_DKB) || defined(GAME_POKOPIA) +#if defined(GAME_DKB) || defined(GAME_POKOPIA) || defined(GAME_OOT) (void)resolution; [rows addObject:settings_ns2_resolution_row()]; #else diff --git a/tests/test_ns2_resolution.py b/tests/test_ns2_resolution.py index a85546b60..8d8fb4aae 100644 --- a/tests/test_ns2_resolution.py +++ b/tests/test_ns2_resolution.py @@ -83,6 +83,50 @@ def test_pokopia_frame_snapshot_preserves_controller(self): result=subprocess.run([str(exe)],capture_output=True,text=True) self.assertEqual(result.returncode,0,result.stdout+result.stderr) @unittest.skipUnless(sys.platform=='darwin' and platform.machine()=='arm64','native Apple ARM64 required') + def test_oot_base_size_follows_request(self): + source=r''' +#include "oot_display.h" +#include +#include +#include +int main(void) { + size_t ps=(size_t)sysconf(_SC_PAGESIZE); + unsigned char *m=mmap(0,ps*2,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0); + if(m==MAP_FAILED)return 1; + uint64_t *p=(void *)(m+ps);const uintptr_t v1=(uintptr_t)m,v2=v1+0x20,set=v1+0x100,frame=v1+0x200; + uint32_t base[OOT_BASE_WORDS],upd[OOT_FRAME_WORDS]; + oot_base_code(base,v1,(uintptr_t)p,set+4);oot_frame_code(upd,v2,(uintptr_t)p,frame+4,set); + memcpy(m,base,sizeof base);memcpy(m+0x20,upd,sizeof upd); + /* stand-ins: setBaseSize after its first ldp (store *x1, set bit 21 on a change, clobber w8-w11), and the cfg update after its ldp + (record w11,w9 at +0x20 and x1 at +0x28); their first words branch to the veneers as the patched game's do */ + const uint32_t set_body[]={0xf9400028,0xf9400809,0xeb09011f,0x540000a0,0xf9000808,0xb940080a,0x320b014a,0xb900080a,0x528000eb,0xd65f03c0}; + const uint32_t frame_body[]={0x2904240b,0xf9001401,0xd65f03c0}; + uint32_t jump=bananza_display_branch(set,v1);memcpy(m+0x100,&jump,4);memcpy(m+0x104,set_body,sizeof set_body); + jump=bananza_display_branch(frame,v2);memcpy(m+0x200,&jump,4);memcpy(m+0x204,frame_body,sizeof frame_body); + sys_icache_invalidate(m,0x300);if(mprotect(m,ps,PROT_READ|PROT_EXEC))return 2; + unsigned char cfg[0x40];memset(cfg,0xa5,sizeof cfg);uint32_t flags=5,word;uint64_t v,shipped=2048ull|(1152ull<<32),other=1280ull|(720ull<<32); + memcpy(cfg+8,&flags,4);memcpy(cfg+0x10,&shipped,8);p[1]=shipped; + ((void(*)(void *,uint64_t))frame)(cfg,0xdead); + memcpy(&v,cfg+0x28,8);if(v!=0xdead)return 3; /* x1 kept */ + memcpy(&word,cfg+0x20,4);if(word!=5)return 4; /* the displaced ldp ran */ + memcpy(&v,cfg+0x10,8);if(v!=shipped||p[3])return 5; /* an unchanged base: no setBaseSize call */ + p[1]=2560ull|(1440ull<<32);memset(cfg+0x20,0,0x10); + ((void(*)(void *,uint64_t))frame)(cfg,0xbeef); + memcpy(&v,cfg+0x10,8);if(v!=p[1]||p[2]!=p[1]||p[3]!=(uintptr_t)cfg)return 6; + memcpy(&word,cfg+0x20,4);if(word!=(5|0x200000))return 7; /* the same update sees bit 21 */ + memcpy(&v,cfg+0x28,8);if(v!=0xbeef)return 8; + for(unsigned i=0x18;i<0x20;i++)if(cfg[i]!=0xa5)return 9; + p[1]=960ull|(540ull<<32);((void(*)(void *,void *))set)(cfg,&other); /* the game's own request: the host's size wins */ + memcpy(&v,cfg+0x10,8);if(v!=p[1]||other!=(1280ull|(720ull<<32)))return 10; + return munmap(m,ps*2)!=0; +} +''' + with tempfile.TemporaryDirectory() as d: + c=pathlib.Path(d)/'oot.c';exe=pathlib.Path(d)/'oot';c.write_text(source) + subprocess.run(['cc','-std=c11','-Wall','-Wextra','-Werror','-I',str(ROOT/'games/totk/runtime'),str(c),'-o',str(exe)],check=True,capture_output=True) + result=subprocess.run([str(exe)],capture_output=True,text=True) + self.assertEqual(result.returncode,0,result.stdout+result.stderr) + @unittest.skipUnless(sys.platform=='darwin' and platform.machine()=='arm64','native Apple ARM64 required') def test_live_display_runs_on_game_thread(self): source=r''' #include "bananza_display.h" @@ -198,3 +242,48 @@ def test_phone_pane_uses_engine_parameter(self): self.assertEqual(rows['TOTK_OUTPUT_RES']['Type'],'PSTextFieldSpecifier') self.assertNotIn('TOTK_DOCKED',rows) self.assertNotIn('TOTK_RENDER_RES',rows) + def test_output_size_valid_per_game(self): + """runtime_output_size_valid, cut from game_patches_ns2.c: Ocarina of Time takes 16:9 on the 8-pixel grid from 640x360 up to its + capacity; Bananza and Pokopia keep exactly their earlier rule (a page, both sides nonzero, within the capacity).""" + compiler=shutil.which('cc') + if not compiler:self.skipTest('C compiler required') + import re + text=(ROOT/'games/totk/runtime/game_patches_ns2.c').read_text() + fits=re.search(r'#ifdef GAME_OOT\n/\* Ocarina of Time v1: its sizes.*?\n#endif\n',text,re.S).group(0) + valid=re.search(r'int runtime_output_size_valid\(unsigned w,unsigned h\) \{.*?\n\}\n',text,re.S).group(0) + source=r''' +#include "ns2_resolution.h" +#include +static _Atomic(uint64_t *) output_page;static int has;static unsigned cw,ch; +static int runtime_output_res(unsigned *w,unsigned *h){if(!has)return 0;*w=cw;*h=ch;return 1;} +'''+fits+'static '+valid+r''' +#ifndef GAME_OOT /* runtime_output_size_set's own test before runtime_output_size_valid */ +static int earlier(unsigned w,unsigned h){unsigned mw,mh;return !(!atomic_load(&output_page)||!w||!h||!runtime_output_res(&mw,&mh)||w>mw||h>mh);} +#endif +int main(void) { + static uint64_t page[4]; +#ifdef GAME_OOT + const unsigned good[][2]={{640,360},{1024,576},{1280,720},{1920,1080},{2048,1152},{2560,1440},{1152,648}}; + const unsigned bad[][2]={{960,540},{1600,900},{1366,768},{1360,768},{1921,1080},{1920,1088},{632,356},{2568,1444},{3840,2160},{0,0},{2560,0},{1280,736}}; + has=1;cw=OOT_OUTPUT_MAX_W;ch=OOT_OUTPUT_MAX_H;atomic_store(&output_page,page); + for(unsigned i=0;i