Android port (Adreno): Vulkan backend, GameActivity app, touch HUD, lifecycle, minimap quest goals

- host/android: Vulkan replay backend (nvn_vk.c) with pipeline/descriptor caching, ADPF,
  AChoreographer present link; Linux compat layer; Makefile.android (headless dmhost + APK lib)
- android/app: GameActivity shell without Gradle (build_apk.sh), AAudio output, touch HUD port
  (touch_app.c + TouchOverlay.java, three layers: body / joystick / minimap goals),
  lifecycle save-on-background like iOS, art_from_ipa.py for touch / HUD / mapgoal art
- touch: shared touch controls moved from ios/Sources to dm/touch
- patch_nso.py Android ruleset, dmsh_vk.py shader pack, DM_TOUCH_PRODUCT defaults
- hle_audio: AAudio resume waits through STARTING and reopens once on failure
- .gitignore: local resources/ (original game data) never committed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-14 14:55:05 +09:00
co-authored by Claude Opus 5.5
parent 5bc5a01087
commit 4d91f80bae
106 changed files with 7009 additions and 181 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
// 生成文件(dm/gfx/tools/dmsh_chain.py --header):本源码树期望的 B 着色器库翻译链版本(缺省选项);宿主只加载 <库目录>/b-<V>/
// Generated file (dm/gfx/tools/dmsh_chain.py --header): the translation chain version of the B shader library expected by this source tree (default options); the host only loads <library dir>/b-<V>/
#define DMSH_CHAIN "15cf7ca29eaf"
#define DMSH_CHAIN "f0d74f7f12a9"
+46 -4
View File
@@ -66,13 +66,13 @@ typedef struct { const char *name; void *fn; } nvn_impl;
#define NVN(name, ret, ...) \
static ret h_##name(__VA_ARGS__); \
static const NVNT_##name chk_##name __attribute__((unused)) = h_##name; \
__attribute__((used, section("__DATA,swnvn"))) static const nvn_impl e_##name = {#name, (void *)h_##name}; \
__attribute__((used, DM_SECT("swnvn"))) static const nvn_impl e_##name = {#name, (void *)h_##name}; \
static ret h_##name(__VA_ARGS__)
// 无原型的新版接口:不做签名检查
// Newer interfaces without prototypes: no signature check
#define NVN_NOPROTO(name, ret, ...) \
static ret h_##name(__VA_ARGS__); \
__attribute__((used, section("__DATA,swnvn"))) static const nvn_impl e_##name = {#name, (void *)h_##name}; \
__attribute__((used, DM_SECT("swnvn"))) static const nvn_impl e_##name = {#name, (void *)h_##name}; \
static ret h_##name(__VA_ARGS__)
static int g_log;
@@ -164,7 +164,12 @@ NVN(nvnDeviceGetSeparateSamplerHandle, NVNseparateSamplerHandle, const NVNdevice
NVN(nvnDeviceInstallDebugCallback, void, NVNdevice *d, const PFNNVNDEBUGCALLBACKPROC cb, void *data, uint32_t enable) {
dev_t_ *o = (dev_t_ *)d; o->dbg_cb = B(enable) ? (void *)cb : NULL; o->dbg_data = data;
}
NVN(nvnDeviceSetWindowOriginMode, void, NVNdevice *d, NVNwindowOriginMode m) { ((dev_t_ *)d)->origin = m; LOG("[nvn] 窗口原点 %s", m ? "左上(同 D3D / Metal)" : "左下(GL)"); }
NVN(nvnDeviceSetWindowOriginMode, void, NVNdevice *d, NVNwindowOriginMode m) {
((dev_t_ *)d)->origin = m;
#ifdef __ANDROID__
mtl_set_window_origin_mode(m); // Vulkan 后端按原点选负高视口 | the Vulkan backend picks a negative-height viewport by origin
#endif
LOG("[nvn] 窗口原点 %s", m ? "左上(同 D3D / Metal)" : "左下(GL)"); }
NVN(nvnDeviceSetDepthMode, void, NVNdevice *d, NVNdepthMode m) { ((dev_t_ *)d)->depth_mode = m; LOG("[nvn] 深度模式 %s", m ? "近=0(同 D3D / Metal)" : "近=-w(GL)"); }
NVN(nvnDeviceFinalizeCommandHandle, void, NVNdevice *d, NVNcommandHandle h) { (void)d; (void)h; }
@@ -399,7 +404,11 @@ NVN(nvnQueueFinalize, void, NVNqueue *q) { ((queue_t *)q)->magic = 0; }
NVN(nvnQueueGetError, NVNqueueGetErrorResult, NVNqueue *q, NVNqueueErrorInfo *info) {
(void)q; if (info) memset(info, 0, sizeof *info); return NVN_QUEUE_GET_ERROR_RESULT_GPU_NO_ERROR;
}
NVN(nvnQueueFlush, void, NVNqueue *q) { (void)q; }
NVN(nvnQueueFlush, void, NVNqueue *q) { (void)q;
#ifdef __ANDROID__
mtl_queue_flush();
#endif
}
NVN(nvnQueueFinish, void, NVNqueue *q) { (void)q; mtl_finish(); }
// ---------- 命令缓冲:录进游戏的命令内存,提交时执行 CPU 可见副作用 ----------
@@ -535,6 +544,30 @@ static void *rec(NVNcommandBuffer *c, uint32_t op, size_t payload) {
return p + 8;
}
#define REC(c, OP, T) ((T *)rec((c), (OP), sizeof(T)))
#ifdef __ANDROID__
// Vulkan 后端要按命令序处理的同步命令(Metal 端仍当空操作,见 g_nops);格式与 dodola/d2r-mobile 的 nvn_vk.c 一致
// Sync commands the Vulkan backend handles in command order (still no-ops on Metal, see g_nops); format matches nvn_vk.c of dodola/d2r-mobile
NVN(nvnCommandBufferSetShaderScratchMemory, void, NVNcommandBuffer *c, const NVNmemoryPool *p, ptrdiff_t off, size_t size) {
const pool_t *pool = (const pool_t *)p;
if (!pool || pool->magic != MAGIC('P', 'O', 'O', 'L') || !pool->mem || off < 0 || (uint64_t)off > pool->size || size > pool->size - (uint64_t)off)
die("ShaderScratchMemory 范围非法 off=%lld size=%llu", (long long)off, (unsigned long long)size);
*REC(c, OP_SHADER_SCRATCH, p_bind_buf) = (p_bind_buf){.addr = (uint64_t)(uintptr_t)(pool->mem + off), .size = size};
}
NVN(nvnCommandBufferWaitSync, void, NVNcommandBuffer *c, const NVNsync *s) {
if (!s || ((const sync_t *)s)->magic != MAGIC('S', 'Y', 'N', 'C')) die("CommandBufferWaitSync 同步对象非法");
*REC(c, OP_WAIT_SYNC, p_sync) = (p_sync){(sync_t *)s, 0, 0};
}
NVN(nvnCommandBufferFenceSync, void, NVNcommandBuffer *c, NVNsync *s, NVNsyncCondition cond, int flags) {
if (!s || ((sync_t *)s)->magic != MAGIC('S', 'Y', 'N', 'C') || cond < 0 || cond > 1 || (flags & ~1)) die("CommandBufferFenceSync 参数非法");
*REC(c, OP_FENCE_SYNC, p_sync) = (p_sync){(sync_t *)s, cond, flags};
}
NVN(nvnCommandBufferWaitEvent, void, NVNcommandBuffer *c, const NVNevent *e, NVNeventWaitMode mode, uint32_t v) {
const ev_t *ev = (const ev_t *)e;
if (!ev || ev->magic != MAGIC('E', 'V', 'N', 'T') || !ev->val || mode < 0 || mode > 1) die("CommandBufferWaitEvent 参数非法");
uint8_t *p = rec(c, OP_WAIT_EVENT, 16); uint64_t a = (uint64_t)(uintptr_t)ev->val; uint32_t mm = mode;
memcpy(p, &a, 8); memcpy(p + 8, &mm, 4); memcpy(p + 12, &v, 4);
}
#endif
// 带数组的绑定记录按实际个数分配:前缀字段 + n 个元素(回放端只读前 n 个)。原先按最大个数定长记(绑顶点缓冲 272 字节、绑贴图 / 采样器 280 字节,
// 实际常只有 1–2 个),空尾占每帧命令字节的四到五成;88 级法师的高级属性页每帧录 12.5 MB,把游戏自己的命令内存池(约每帧 105 段 × 128 KB)用空
// Binding records with arrays are allocated by the actual count: prefix fields + n elements (the replay side reads only the first n). Previously they were recorded at fixed length for the maximum count (bind vertex buffers 272 bytes, bind textures / samplers 280 bytes,
@@ -777,12 +810,18 @@ static const char *const g_nops[] = {
"nvnCommandBufferDispatchComputeIndirect",
"nvnCommandBufferSetDepthBounds",
"nvnCommandBufferDownsampleTextureView",
#ifndef __ANDROID__
"nvnCommandBufferWaitSync",
#endif
#ifndef __ANDROID__
"nvnCommandBufferSetShaderScratchMemory",
#endif
"nvnCommandBufferPushDebugGroupDynamic",
"nvnCommandBufferPopDebugGroup",
"nvnCommandBufferInsertDebugMarkerDynamic",
#ifndef __ANDROID__
"nvnCommandBufferWaitEvent",
#endif
"nvnWindowFinalize",
};
__attribute__((constructor)) static void nvn_nops_register(void) {
@@ -840,6 +879,7 @@ static double g_rt_period_ms;
static void sleep_until_ns(uint64_t dl);
static uint64_t present_period(const win_t *o);
__attribute__((weak)) void dm_main_yield_stats(uint64_t *n, uint64_t *ns) { *n = 0; *ns = 0; } // 宿主 hle_thread.c 提供真值;图形单测里为 0 | The host's hle_thread.c provides the ground truth; 0 in graphics unit tests
__attribute__((weak)) void mtl_pace_slept(uint64_t ns) { (void)ns; } // 取图里节拍等待的墙钟;安卓后端(nvn_vk.c)据此算每帧真实工作时长报给 ADPF | Wall time spent in the acquire pacing wait; the Android backend (nvn_vk.c) uses it to derive each frame's real work time for ADPF
__attribute__((weak)) uint32_t dm_guest_work_qos(unsigned *count) { if (count) *count = 0; return QOS_CLASS_UNSPECIFIED; }
extern uint32_t mtl_present_qos(void);
static uint64_t g_frame;
@@ -856,8 +896,10 @@ NVN(nvnWindowAcquireTexture, NVNwindowAcquireTextureResult, NVNwindow *w, NVNsyn
static uint64_t rt_period; uint64_t period = present_period(o);
if (period && rt_period != period) { rt_period = period; set_time_constraint(period); }
rt_comp_tick(period);
uint64_t pace_t0 = now_ns();
if (mtl_link_active()) { if (!mtl_link_wait_tick(period ? 2 * period : 33333333ull) && wait) sleep_until_ns(at); } // DM_PRESENT_LINK:节拍完全跟显示节拍(跑得快就等下一拍,落后立刻走);自己的 60 Hz 时钟只作等不到节拍时的兜底 | DM_PRESENT_LINK: pacing fully follows the display link (wait for the next display-link tick when running fast, go immediately when behind); our own 60 Hz clock is only the fallback when no display-link tick arrives
else if (wait) sleep_until_ns(at);
mtl_pace_slept(now_ns() - pace_t0);
} else { // DM_DETCLOCK_PACE=1(画面闸门用):客体时钟按帧走的同时,每帧墙钟不短于一个步长——帧数与游戏内时间确定,后台加载线程每帧至少有一个步长的墙钟可用; | DM_DETCLOCK_PACE=1 (used by the screenshot gate): while the guest clock advances per frame, each frame's wall clock is no shorter than one step -- frame count and in-game time are deterministic, and the background loading thread has at least one step of wall clock available per frame;
// 机器满载时帧只会更慢、后台得到的墙钟只会更多,所以「消息后第 N 帧」截到的画面与负载无关
// When the machine is fully loaded frames only get slower and the background only gets more wall clock, so the picture captured at "the Nth frame after the message" is independent of load
+3
View File
@@ -37,6 +37,9 @@ uint64_t mtl_sub_commit(void);
uint64_t mtl_last_fence(void); // 最近一次提交的栅栏值(0 = 从未提交) | Fence value of the most recent submit (0 = never submitted)
int mtl_wait(uint64_t fence, uint64_t timeout_ns); // GPU 完成到 fence 返回 1,超时 0 | Returns 1 when the GPU has completed up to fence, 0 on timeout
#ifdef __ANDROID__
void mtl_queue_flush(void); // Submit deferred Vulkan work without waiting.
#endif
void mtl_finish(void); // 等所有已提交的命令完成 | Wait for all submitted commands to complete
void mtl_drain(void); // 等队列上全部命令(含出图)完成:提交空命令缓冲并等它 | Wait for all commands on the queue (including presents) to complete: submit an empty command buffer and wait for it
void mtl_xv_window_tex(uint64_t serial); // 登记窗口(交换链)贴图:跨提交有效性账本永不收它 | Register a window (swapchain) texture: the cross-submit validity ledger never collects it
+5 -1
View File
@@ -92,7 +92,7 @@ enum {
OP_BIND_PROGRAM, OP_BIND_STATE, OP_BIND_VATTR, OP_BIND_VSTREAM, OP_BIND_VBUF,
OP_BIND_UBO, OP_BIND_SSBO, OP_BIND_TEX, OP_BIND_SAMP,
OP_STENCIL, OP_POLY_OFFSET, OP_DRAW, OP_DRAW_IDX, OP_DISPATCH, OP_BARRIER,
OP_SET_TEX_POOL, OP_SET_SAMP_POOL, OP_COPY_B2T, OP_COPY_STRIDE, OP_BLEND_COLOR, OP_COPY_T2B, OP_BIND_IMG, OP_COPY_T2T, OP_RESET_COUNTER, OP_CLEAR_TEX, OP_COUNT_
OP_SET_TEX_POOL, OP_SET_SAMP_POOL, OP_COPY_B2T, OP_COPY_STRIDE, OP_BLEND_COLOR, OP_COPY_T2B, OP_BIND_IMG, OP_COPY_T2T, OP_RESET_COUNTER, OP_CLEAR_TEX, OP_SHADER_SCRATCH, OP_WAIT_SYNC, OP_FENCE_SYNC, OP_WAIT_EVENT, OP_COUNT_
};
// 载荷(紧跟 op_hdr)
// Payload (immediately follows op_hdr)
@@ -120,6 +120,7 @@ typedef struct { const tex_t *src, *dst; view_t sv, dv; uint8_t has_sv, has_dv;
typedef struct { const tex_t *tex; view_t view; uint8_t has_view, is_uint; NVNcopyRegion r; int32_t mask; union { float f[4]; uint32_t u[4]; } c; } p_clear_tex;
typedef struct { int64_t stride; } p_copy_stride;
typedef struct { float rgba[4]; } p_blend_color;
typedef struct { sync_t *sync; int32_t condition, flags; } p_sync; // OP_WAIT_SYNC / OP_FENCE_SYNC(仅 Android 录制) | OP_WAIT_SYNC / OP_FENCE_SYNC (recorded on Android only)
// Metal 回放层入口(nvn_mtl.m):把一组命令句柄回放进当前提交
// Entry point of the Metal replay layer (nvn_mtl.m): replays a set of command handles into the current submission
@@ -171,6 +172,9 @@ void mtl_read_texels(const tex_t *t, unsigned level, unsigned layer0, const NVNc
void mtl_reg_tex(int id, const tex_t *t, const view_t *v);
void mtl_reg_samp(int id, const samp_par *p);
void mtl_prog_release(prog_t *p);
#ifdef __ANDROID__
void mtl_set_window_origin_mode(int mode);
#endif
// 新序号(> 0,全局递增)
// New serial (> 0, globally increasing)
uint64_t nvn_new_serial(void);