Skip to content

Commit 4f25737

Browse files
Record the tested Ocarina opening movie repair for Metal 3
1 parent e221694 commit 4f25737

2 files changed

Lines changed: 235 additions & 0 deletions

File tree

‎dm/ios/Engines/NxNative/README.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,24 @@ whose host lacks file-backed guest memory. Source SHA-256 before patching:
2121

2222
- `dm/host/hle_svc.c`: `51f35785f230a7c3727f9f4ab7e4b839cd5633ff6c368e9a1b80a2e6dca50044`
2323
- `dm/gfx/nvn/nvn_mtl.m`: `2e311373518793777787f96d233c748a8fbb0fca8a17d3bc8d68e172d02ae40c`
24+
25+
## Ocarina of Time: opening movie on Metal 3 (M2 60.60)
26+
27+
The opening video's decode kernel writes a private RGBA8 target which the
28+
legacy path created without shader-write usage. Promote the exact decoder
29+
output before encoding its dispatch. Scale that decoded frame into the
30+
original movie destination and omit the matched 13 reconstruction dispatches
31+
only when the replacement render pass was successfully encoded. Other
32+
sequences retain the ordinary path. The independent engine keeps shader chain
33+
`70fa6008c58d`; all six guest sections and its sealed shader companions are
34+
byte-identical.
35+
36+
The installed M2 build was checked against the same opening. The opening
37+
video is now visible; GPU median over the first reported 300 frames was
38+
1.93 ms versus 76.50 ms on the preceding build. This does not establish 3D
39+
gameplay performance or completeness.
40+
41+
Apply `ocarina-m2-opening-movie.patch` to the pinned 60.57 Ocarina Metal 3
42+
renderer. Source SHA-256 before patching:
43+
44+
- `dm/gfx/nvn/nvn_mtl_legacy.inc`: `4a5bf9dfb5e825d1ac9e1122c7a53fdd841346a0d47f88ae54100accafcc0c4a`
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
--- a/dm/gfx/nvn/nvn_mtl_legacy.inc
2+
+++ b/dm/gfx/nvn/nvn_mtl_legacy.inc
3+
@@ -930,7 +930,12 @@
4+
static int g_ruler_truth = -1, g_ruler_pending;
5+
static int tbdr_on(void);
6+
7+
+// The image is valid only within the submit that decoded it.
8+
+static id<MTLTexture> g_oot_movie_decoded;
9+
+static const uint8_t *g_oot_movie_skip_end;
10+
void mtl_sub_begin(void) {
11+
+ g_oot_movie_decoded = nil;
12+
+ g_oot_movie_skip_end = NULL;
13+
mtl_init(); g_sub_serial++; g_rc_npend = 0; g_rc_skip = 0;
14+
if (tbdr_on() == 0 && g_ruler_pending) { g_ruler_truth = 0; g_ruler_pending = 0; MLOG("DM_NVN_RULER:切到被测模式"); }
15+
defer_drain();
16+
@@ -1714,12 +1719,12 @@
17+
}
18+
// In-place upgrade: when the original texture was not created with PixelFormatView (off by default, to keep Apple GPU lossless compression) but a different-format view is needed, create a new one with the original spec + PixelFormatView, copy over every slice × level, and replace the registration.
19+
// Count: across the 4 screens character select / camp / wilderness / combat, 17473 textures needed a different-format view 0 times, so this is a fallback and does not rely on predicting which textures need it
20+
-static id<MTLTexture> tex_upgrade_pfv(const tex_t *t, id<MTLTexture> base, MTLPixelFormat view_pf) {
21+
+static id<MTLTexture> tex_upgrade_usage(const tex_t *t, id<MTLTexture> base, MTLPixelFormat view_pf, MTLTextureUsage usage) {
22+
MTLTextureDescriptor *d = [MTLTextureDescriptor new];
23+
d.textureType = base.textureType; d.pixelFormat = base.pixelFormat; d.width = base.width; d.height = base.height; d.depth = base.depth; // Keep the original format, only add the format-view usage
24+
d.arrayLength = base.arrayLength; d.mipmapLevelCount = base.mipmapLevelCount; d.sampleCount = base.sampleCount;
25+
d.storageMode = base.storageMode; d.cpuCacheMode = base.cpuCacheMode; d.hazardTrackingMode = base.hazardTrackingMode;
26+
- d.resourceOptions = base.resourceOptions; d.usage = base.usage | MTLTextureUsagePixelFormatView;
27+
+ d.resourceOptions = base.resourceOptions; d.usage = base.usage | usage;
28+
id<MTLTexture> upgraded = [g_dev newTextureWithDescriptor:d];
29+
if (!upgraded) { RED_ONCE(45, "贴图升级失败 ser=%llu 格式 %lu → %lu", t->serial, (unsigned long)base.pixelFormat, (unsigned long)view_pf); return base; }
30+
tex_tag_format(upgraded, t->d.format);
31+
@@ -1742,6 +1747,9 @@
32+
}
33+
MLOG("贴图升级为可换格式视图 ser=%llu 基格式 %lu → 视图格式 %lu(升级次数=%llu)", t->serial, (unsigned long)base.pixelFormat, (unsigned long)view_pf, ++g_pfv_upgrades);
34+
return upgraded;
35+
+}
36+
+static id<MTLTexture> tex_upgrade_pfv(const tex_t *t, id<MTLTexture> base, MTLPixelFormat pf) {
37+
+ return tex_upgrade_usage(t, base, pf, MTLTextureUsagePixelFormatView);
38+
}
39+
// Packaged texture upload: first set read-only, then take the stamp, then read the pool memory and deswizzle into a staging buffer, and blit into a new private texture.
40+
// Batching: uploads within one replay submission share one upload command buffer (compute encoder deswizzle + a unified blit), flushed before the replay command buffer is committed;
41+
@@ -8718,12 +8726,140 @@
42+
}
43+
return ps;
44+
}
45+
-static void dispatch(const p_dispatch *dp) {
46+
+
47+
+static int oot_movie_direct_on(void) {
48+
+ static int enable = -1;
49+
+ if (enable < 0) {
50+
+ const char *e = getenv("DM_NVN_NDVE_MOVIE_DIRECT");
51+
+ enable = DM_GAME_PROGRAM_ID == 0x0400486024DEC000ull && (!e || strcmp(e, "0"));
52+
+ }
53+
+ return enable;
54+
+}
55+
+static id<MTLTexture> oot_movie_image(NvShader *cs, unsigned slot, uint64_t handle, uint64_t *serial, bool promote) {
56+
+ bool found = false;
57+
+ for (int i = 0; i < cs->rf.nb; ++i) {
58+
+ const nvn_rbind *b = &cs->rf.b[i];
59+
+ if (b->kind == NVN_RB_IMG && b->reg == slot && b->rw) { found = true; break; }
60+
+ }
61+
+ int tid = (int)(handle & 0xfffff);
62+
+ if (!found || !tid) return nil;
63+
+ pthread_mutex_lock(&g_rmu);
64+
+ id<MTLTexture> t = nil;
65+
+ if (tid < g_ntreg && g_treg[tid].ok) {
66+
+ t = tex_by_id(tid);
67+
+ const tex_t *tt = &g_treg[tid].t;
68+
+ if (promote && t && !(t.usage & MTLTextureUsageShaderWrite) && !t.parentTexture && !t.buffer &&
69+
+ t.width == 1920 && t.height == 1080 && t.textureType == MTLTextureType2D &&
70+
+ t.pixelFormat == MTLPixelFormatRGBA8Unorm && t.sampleCount == 1 && t.mipmapLevelCount == 1 &&
71+
+ t.storageMode == MTLStorageModePrivate && (t.usage & MTLTextureUsageRenderTarget) &&
72+
+ !tt->has_layout && !tex_cpu_written(tt->serial)) {
73+
+ id<MTLTexture> upgraded = tex_upgrade_usage(tt, t, t.pixelFormat, MTLTextureUsageShaderWrite);
74+
+ if (upgraded != t) {
75+
+ t = tex_by_id(tid);
76+
+ static unsigned promoted;
77+
+ if (t == upgraded && promoted++ < 4)
78+
+ MLOG("M2 Ocarina NDVE: promoted decoder output %d to shader-write", tid);
79+
+ }
80+
+ }
81+
+ if (serial) *serial = tt->serial;
82+
+ }
83+
+ pthread_mutex_unlock(&g_rmu);
84+
+ if (t.textureType != MTLTextureType2D || t.pixelFormat != MTLPixelFormatRGBA8Unorm || t.sampleCount != 1) return nil;
85+
+ return t;
86+
+}
87+
+static int copy_t2t_draw(id<MTLTexture>, id<MTLTexture>, unsigned, unsigned, unsigned, unsigned,
88+
+ const NVNcopyRegion *, const NVNcopyRegion *, int, int);
89+
+static int oot_movie_direct_draw(NvShader *cs, uint64_t dst) {
90+
+ id<MTLTexture> in = g_oot_movie_decoded;
91+
+ if (!in) return 0;
92+
+ uint64_t serial = 0;
93+
+ id<MTLTexture> out = oot_movie_image(cs, 1, dst, &serial, false);
94+
+ if (in.width != 1920 || in.height != 1080 || out.width != 2560 || out.height != 1440 ||
95+
+ in == out || !serial || !(out.usage & MTLTextureUsageRenderTarget)) return 0;
96+
+ rp_flush();
97+
+ const NVNcopyRegion r0 = {0, 0, 0, 1920, 1080, 1}, r1 = {0, 0, 0, 2560, 1440, 1};
98+
+ if (!copy_t2t_draw(in, out, 0, 0, 0, 0, &r0, &r1, 1, 0)) return 0;
99+
+ zskip_del(serial);
100+
+ gpu_tex_mark(serial);
101+
+ args_dirty_all();
102+
+ g_oot_movie_decoded = nil;
103+
+ static int once;
104+
+ if (!once++) MLOG("M2 Ocarina NDVE: scaled the decoded RGBA frame directly");
105+
+ return 1;
106+
+}
107+
+// Preserve all bindings and barriers. Drop only the captured, contiguous
108+
+// reconstruction sequence, only after its final output is encoded.
109+
+static void oot_movie_chain(const uint8_t *p) {
110+
+ static const char *const keys[] = {
111+
+ "ba079f68ae288dba_b", "a8b4fb48fee0ccf8_b", "e2eebd7ba18fe35f_b",
112+
+ "e6be82d835fbe4d3_b", "9442573ee4ace011_b", "0a56002cdc7e1fce_b",
113+
+ "0a56002cdc7e1fce_b", "d15370a382630d4e_b", "e6be82d835fbe4d3_b",
114+
+ "7f7ff0f36b54d90f_b", "a8b4fb48fee0ccf8_b", "5a7d69b067a7f011_b",
115+
+ "4f41ccf24f163e0d_b",
116+
+ };
117+
+ if (!g_oot_movie_decoded) return;
118+
+ uint64_t dst = S.img[5][1];
119+
+ NvShader *cs = nil;
120+
+ unsigned index = 0;
121+
+ for (int count = 0; count < 128; ++count) {
122+
+ op_hdr h;
123+
+ memcpy(&h, p, sizeof h);
124+
+ if (h.size < sizeof h || h.size > 65536) return;
125+
+ const void *a = p + sizeof h;
126+
+ switch (h.op) {
127+
+ case OP_BIND_PROGRAM: {
128+
+ if (h.size < sizeof h + sizeof(p_bind_program)) return;
129+
+ const p_bind_program *b = a;
130+
+ if (b->stages != 0x20 || !b->prog) return;
131+
+ NvProg *pr = prog_get(b->prog);
132+
+ cs = pr ? pr->sh[5] : nil;
133+
+ break;
134+
+ }
135+
+ case OP_BIND_IMG: {
136+
+ if (h.size < sizeof h + offsetof(p_bind_handles, h)) return;
137+
+ const p_bind_handles *b = a;
138+
+ if (b->stage != 5 || b->defer || b->first < 0 || b->n <= 0 ||
139+
+ b->first >= 32 || b->n > 32 - b->first ||
140+
+ h.size < sizeof h + offsetof(p_bind_handles, h) + sizeof(b->h[0]) * b->n) return;
141+
+ if (b->first <= 1 && b->first + b->n > 1) dst = b->h[1 - b->first];
142+
+ break;
143+
+ }
144+
+ case OP_BIND_SSBO: case OP_BIND_UBO: case OP_BIND_UBO_SMALL:
145+
+ case OP_BIND_TEX: case OP_BIND_TEX1: case OP_BIND_SAMP:
146+
+ case OP_PUSH_CONST: case OP_BARRIER: break;
147+
+ case OP_DISPATCH: {
148+
+ if (h.size < sizeof h + sizeof(p_dispatch)) return;
149+
+ const p_dispatch *d = a;
150+
+ if (d->ind || d->x <= 0 || d->y <= 0 || d->z != 1 || !cs ||
151+
+ strcmp(cs->key, keys[index])) return;
152+
+ if (++index != sizeof keys / sizeof keys[0]) break;
153+
+ if (oot_movie_direct_draw(cs, dst)) {
154+
+ g_oot_movie_skip_end = p;
155+
+ static int once;
156+
+ if (!once++) MLOG("M2 Ocarina NDVE: skipped %u verified reconstruction dispatches", index);
157+
+ }
158+
+ return;
159+
+ }
160+
+ default: return; // Including END, JUMP, CALL and submit boundaries.
161+
+ }
162+
+ p += h.size;
163+
+ }
164+
+}
165+
+
166+
+static void dispatch(const p_dispatch *dp, const uint8_t *next) {
167+
zskip_clear();
168+
if (!S.cprog) { RED_ONCE(27, "compute dispatch has no bound program"); return; }
169+
NvProg *pr = prog_get(S.cprog);
170+
NvShader *cs = pr->sh[5];
171+
if (!cs) { RED_ONCE(28, "dispatched program has no mapped compute shader"); return; }
172+
+ int movie = oot_movie_direct_on() && !dp->ind && dp->x > 0 && dp->y > 0 && dp->z == 1;
173+
+ int decoded = movie && !strcmp(cs->key, "49dff010b9832251_b");
174+
+ if (decoded) {
175+
+ g_oot_movie_decoded = nil;
176+
+ oot_movie_image(cs, 0, S.img[5][0], NULL, true);
177+
+ }
178+
+ if (movie && !strcmp(cs->key, "4f41ccf24f163e0d_b") &&
179+
+ oot_movie_direct_draw(cs, S.img[5][1])) return;
180+
int min_cs = min_reduction_candidate(cs);
181+
id<MTLComputePipelineState> ps = min_cs ? nil : compute_pipeline(cs, cs->fn);
182+
if (!min_cs && !ps) return;
183+
@@ -8778,6 +8914,17 @@
184+
enc_c_end(ce);
185+
compute_atomic_finish(cs);
186+
bright_lut(cs); // 3D picture brightening: rewrite after the game finishes computing the color lookup table
187+
+ if (decoded) {
188+
+ id<MTLTexture> t = oot_movie_image(cs, 0, S.img[5][0], NULL, false);
189+
+ if (t.width == 1920 && t.height == 1080 && (t.usage & MTLTextureUsageShaderWrite)) {
190+
+ g_oot_movie_decoded = t;
191+
+ oot_movie_chain(next);
192+
+ } else {
193+
+ static int once;
194+
+ if (!once++) MLOG("M2 Ocarina NDVE: decoded image unavailable (format %lu usage %#lx %lux%lu)",
195+
+ (unsigned long)t.pixelFormat, (unsigned long)t.usage, (unsigned long)t.width, (unsigned long)t.height);
196+
+ }
197+
+ }
198+
}
199+
// GPU: texture -> buffer (row stride taken from SetCopyRowStride, layer stride from SetCopyImageStride, 0 = tightly packed)
200+
static void copy_t2b(const p_copy_b2t *c) {
201+
@@ -9631,7 +9778,12 @@
202+
case OP_COPY_T2B: copy_t2b(a); break;
203+
case OP_COPY_T2T: copy_t2t(a); break;
204+
case OP_CLEAR_TEX: clear_tex(a); break;
205+
- case OP_DISPATCH: dispatch(a); break;
206+
+ case OP_DISPATCH:
207+
+ if (g_oot_movie_skip_end) {
208+
+ if (p == g_oot_movie_skip_end) g_oot_movie_skip_end = NULL;
209+
+ break;
210+
+ }
211+
+ dispatch(a, p + h.size); break;
212+
case OP_BARRIER: {
213+
int32_t bits; memcpy(&bits, a, 4);
214+
if (bits) {

0 commit comments

Comments
 (0)