From 2eb5758771f110cd53082b999d88d242766610da Mon Sep 17 00:00:00 2001 From: jhaizhou <258432107+jhaizhou-ops@users.noreply.github.com> Date: Tue, 13 Oct 2026 22:00:06 +0800 Subject: [PATCH] Turn snap-back smoothing and gamepad turning feel (r42); close several slow memory accumulations; Mac App mouse, language switch and warm-up list --- dm/PRODUCT | 2 +- dm/gfx/nvn/nvn_mtl.m | 50 ++++++++++++- dm/host/Makefile | 11 ++- dm/host/app_mac.m | 1 + dm/host/diag.c | 6 ++ dm/host/dm.h | 2 +- dm/host/dm_padmode.c | 35 +++++++++ dm/host/dm_turnsmooth.c | 115 ++++++++++++++++++++++++++++++ dm/host/hle_svc.c | 3 + dm/host/hle_tact.c | 12 +++- dm/host/tests/Makefile | 5 +- dm/host/tests/t_thread_leak.c | 43 +++++++++++ dm/host/tests/t_touch_mac.c | 1 + dm/host/tests/t_touch_tick_stub.c | 1 + dm/host/thread.c | 20 ++++-- dm/host/touch_mac.c | 24 +++++-- dm/ios/Sources/dm_sharelog.m | 11 +-- dm/ios/Sources/front_ios.m | 3 +- dm/ios/Sources/main.m | 17 +++++ dm/ios/tools/burst_sad.py | 27 +++++++ dm/ios/tools/turn_trace.py | 13 ++++ dm/ios/tools/uireg/affected.tsv | 1 + dm/ios/tools/uireg/uireg.py | 7 ++ dm/tools/mac_app.sh | 31 +++++--- dm/tools/patch_nso.py | 2 + 25 files changed, 411 insertions(+), 32 deletions(-) create mode 100644 dm/host/dm_turnsmooth.c create mode 100644 dm/host/tests/t_thread_leak.c create mode 100644 dm/ios/tools/burst_sad.py create mode 100644 dm/ios/tools/turn_trace.py diff --git a/dm/PRODUCT b/dm/PRODUCT index 69849f8..e7d16f0 100644 --- a/dm/PRODUCT +++ b/dm/PRODUCT @@ -1 +1 @@ -3.3.94012-r41 +3.3.94012-r42 diff --git a/dm/gfx/nvn/nvn_mtl.m b/dm/gfx/nvn/nvn_mtl.m index 156a3ca..0a0fa2c 100644 --- a/dm/gfx/nvn/nvn_mtl.m +++ b/dm/gfx/nvn/nvn_mtl.m @@ -358,10 +358,12 @@ void mtl_pool_add(const void *mem, uint64_t size) { void mtl_pool_del(const void *mem) { g_reg_gen++; + id gone = nil; pthread_mutex_lock(&g_mu); for (int i = 0; i < g_npool; i++) - if (g_pools[i].key == mem) { [g_keep removeObject:g_pools[i].buf]; g_pools[i] = g_pools[--g_npool]; break; } + if (g_pools[i].key == mem) { gone = g_pools[i].buf; [g_keep removeObject:gone]; g_pools[i] = g_pools[--g_npool]; break; } pthread_mutex_unlock(&g_mu); + defer_obj_release(gone); // 退役:等用过它的提交完成后放掉,并移出驻留集(原先只从 g_keep 摘掉——驻留集强持有它,零拷贝缓冲对象永远不销毁) | retire: release after the submissions that used it complete, and remove from the residency set (it used to be only removed from g_keep - the residency set holds it strongly, so the zero-copy buffer object was never destroyed) } int mtl_addr(uint64_t addr, uint64_t len, void **buf, uint64_t *off) { @@ -405,6 +407,7 @@ typedef struct { int n; vis_slot s[]; } vis_list; typedef struct { __strong id buf; uint64_t fence; } vis_buf; static vis_buf g_vis_ring[4]; static int g_vis_ring_n, g_vis_ring_i = -1; static id g_vis; static uint32_t g_vis_n; // 本次提交的可见性缓冲与已用槽数 | This submission's visibility buffer and number of slots used +static int vis_in_ring(id b) { for (int i = 0; i < g_vis_ring_n; i++) if (g_vis_ring[i].buf == b) return 1; return 0; } static int g_vis_on, g_vis_counting, g_vis_cur = -1; static uint64_t g_vis_cur_pass; static vis_slot *g_vs; static int g_nvs, g_vscap; // 自上次清零以来的槽 | Slots since the last reset static uint64_t g_cnt_report[32], g_cnt_reset[32]; // 各计数器类型的报告 / 清零次数(诊断) | Report / reset counts per counter type (diagnostics) @@ -496,6 +499,7 @@ void mtl_sub_begin(void) { if (pick < 0) { uint64_t oldest = UINT64_MAX; for (int i = 0; i < 4; i++) if (g_vis_ring[i].fence < oldest) { oldest = g_vis_ring[i].fence; pick = i; } if (oldest > g_done) mtl_wait(oldest, UINT64_MAX); } g_vis_ring_i = pick; g_vis_ring[pick].fence = UINT64_MAX; if (!g_vis_ring[pick].buf) { g_vis_ring[pick].buf = [g_dev newBufferWithLength:VIS_SLOTS * 8 options:MTLResourceStorageModeShared]; res_add(g_vis_ring[pick].buf); } + if (g_vis && !vis_in_ring(g_vis)) defer_obj_release(g_vis); // 上次提交留下的溢出缓冲(见 g_vis_n >= VIS_SLOTS 那一处):退役,否则它一直留在驻留集里 | the overflow buffer left by the previous submission (see the place with g_vis_n >= VIS_SLOTS): retire it, otherwise it stays in the residency set forever g_vis = g_vis_ring[pick].buf; void pg_sub_begin_ext(uint64_t fence); pg_sub_begin_ext(g_fence + 1); void arena_begin(void); arena_begin(); @@ -1243,6 +1247,7 @@ static void res_census(unsigned long long frame, unsigned long long rss_mb) { nl[0], bl[0] >> 20, nl[1], bl[1] >> 20, nr[0], br[0] >> 20, nr[1], br[1] >> 20); } } +unsigned long long mtl_gpu_allocated_mb(void) { id d = g_dev; return d ? (unsigned long long)(d.currentAllocatedSize >> 20) : 0; } // 显卡已分配(图形层还没起时 0);任何线程可调 | GPU allocated (0 before the graphics layer starts); callable from any thread void mtl_mem_stat(unsigned long long frame, unsigned long long rss_mb, int heap_ok, unsigned long long heap_mb, unsigned long long heap_free_mb, unsigned long long heap_maxblk_kb) { if (g_res_census > 0) res_census(frame, rss_mb); static unsigned long long last_rss, last_gpu, last_frame, last_hfree, last_hmax; static int lines; unsigned long long gpu = (unsigned long long)(g_dev.currentAllocatedSize >> 20); @@ -1799,7 +1804,11 @@ static id rp_begin_ex(int clear_only) { g_enc_pso = nil; g_enc_dss = nil; } if (g_vis_on) { - if (!g_vis || g_vis_n >= VIS_SLOTS) { if (g_vis) defer_obj_release(g_vis); g_vis = [g_dev newBufferWithLength:VIS_SLOTS * 8 options:MTLResourceStorageModeShared]; res_add(g_vis); g_vis_n = 0; } + // 槽用满(单次提交超过 VIS_SLOTS 个计数通道):另建一块溢出缓冲接着用。环里的缓冲不许交去退役(它还要轮着用,退役会把它移出驻留集); + // 退役的只能是上一块溢出缓冲。溢出缓冲本身在下次提交换回环里的缓冲时退役(mtl_sub_begin) + // Slots used up (one submission exceeds VIS_SLOTS counting passes): create an overflow buffer and continue with it. Buffers in the ring must not be handed to retirement (they are still used in rotation, and retiring would remove them from the residency set); + // only the previous overflow buffer may be retired. The overflow buffer itself is retired when the next submission switches back to a ring buffer (mtl_sub_begin) + if (!g_vis || g_vis_n >= VIS_SLOTS) { if (g_vis && !vis_in_ring(g_vis)) defer_obj_release(g_vis); g_vis = [g_dev newBufferWithLength:VIS_SLOTS * 8 options:MTLResourceStorageModeShared]; res_add(g_vis); g_vis_n = 0; } pd.visibilityResultBuffer = g_vis; } g_enc_vis = 0; g_enc_has_vis = g_vis_on; @@ -4142,7 +4151,7 @@ static int bloomq_make_pipeline(MTLPixelFormat fmt) { static int bloomq_capture(void) { uint64_t ser; int lv, sl; if (!bloomq_target(&ser, &lv, &sl) || !g_bloomq.s_tex) return 0; id base = tex_mtl(R.rt.color[0]), src = tex_mtl(g_bloomq.s_tex); if (!base || !src) return 0; - id dst = base; if (lv || sl) { static id vb; static int vl, vs; if (vb != base || vl != lv || vs != sl) { g_bloomq_view = [base newTextureViewWithPixelFormat:base.pixelFormat textureType:base.textureType levels:NSMakeRange(lv, 1) slices:NSMakeRange(sl, 1)]; if (g_bloomq_view) res_add(g_bloomq_view); vb = base; vl = lv; vs = sl; } dst = g_bloomq_view; } + id dst = base; if (lv || sl) { static id vb; static int vl, vs; if (vb != base || vl != lv || vs != sl) { defer_obj_release(g_bloomq_view); /* 旧视图退役(原先直接被覆盖:留在驻留集里,连带钉住旧底图) | retire the old view (it used to be overwritten directly: it stayed in the residency set and pinned the old base texture with it) */ g_bloomq_view = [base newTextureViewWithPixelFormat:base.pixelFormat textureType:base.textureType levels:NSMakeRange(lv, 1) slices:NSMakeRange(sl, 1)]; if (g_bloomq_view) res_add(g_bloomq_view); vb = base; vl = lv; vs = sl; } dst = g_bloomq_view; } if (!dst || !bloomq_make_pipeline(dst.pixelFormat)) return 0; rp_end(); MTL4RenderPassDescriptor *rp = [MTL4RenderPassDescriptor new]; rp.colorAttachments[0].texture = dst; rp.colorAttachments[0].loadAction = MTLLoadActionDontCare; rp.colorAttachments[0].storeAction = MTLStoreActionStore; @@ -5200,6 +5209,40 @@ static void grab_present(id src, const int crop[4], CGSize sz) { CGImageRelease(img); CGContextRelease(ctx); CGColorSpaceRelease(cs); }]; [cb2 commit]; } +// ---------- 测试:连拍(量画面连续性用,缺省不动;触点脚本步骤「<时刻> burst <帧数> <目录名>」触发,目录建在 Documents 下)---------- +// 连续 N 帧(上限 BURST_MAX)各拷一块上屏前的游戏画面(屏幕中心、边长 = 画面高 × 0.6 的方块,原始像素):抓的那一帧只多一次显存拷贝、不等显卡, +// 帧间隔不受影响(Mac 实测 16.5–16.9 ms);全部抓完后台线程写盘:f000.raw…(宽 × 高 × 4 字节)+ meta.txt(每帧的出图帧号、墙钟、客体时钟、本地玩家逻辑位置)。 +// 分析:dm/ios/tools/burst_sad.py <目录>(逐帧画面位移与逻辑位移对照) +// ---------- Test: burst capture (for measuring picture continuity, inactive by default; triggered by the touch-script step "