mirror of
https://github.com/seedds/nxnative.git
synced 2026-11-23 11:39:42 -10:00
The Metal 4 specialization descriptor is owned once whether the driver returns it retained or autoreleased: iOS 26.0 no longer crashes before the first frame with Metal 4 pipelines on (PR #1213)
With DM_PSO_MTL4=1 the iOS app crashed before its first frame. The crash was in objc_release under
objc_autoreleasePoolPop at the end of warm_task, on several prepare workers at once.
newRenderPipelineDescriptorForSpecialization is in the new family, so ARC takes a +1 object and releases it
once at the end of pso_build_ex. iOS 26.0 (23A341; -[AGXG16GFamilyRenderPipeline ...] in AGXMetalG16G_B0 on
an iPad Pro M4) returns it autoreleased, so the pool released it a second time. Under LLDB on the iPad:
- the returned MTL4RenderPipelineDescriptor is in the autorelease pool (_objc_autoreleasePoolPrint);
- NSZombieEnabled reports "-[MTL4RenderPipelineDescriptor release]: message sent to deallocated instance".
ARC's one release freed it, so the driver keeps no reference. macOS 27.0 returns it retained: there, the
direct call made the same 34 specializations with no crash.
spec_desc finds out on its first call which of the two it is. A weak reference survives the call's own pool
only if the caller owns the descriptor. From then on it takes the descriptor the way the driver gives it. The
answer holds for the process, because every base comes from the one device's compiler or its archives.
- The "pipeline bases so far" line now names the answer.
- t_pso_stall's own specialization goes through spec_desc too.
Checked:
- t_pso_archive: stand-in bases return the descriptor retained and autoreleased. On both calls, the
descriptor outlives the pools and is freed once dropped (102 checks, 0 failed). The previous direct call
dies on the autoreleased stand-in. t_pso_stall: 41 checks, 0 failed.
- iPad Pro 11-inch (M4), iPadOS 26.0, Axiom Verge 1.0.3 with DM_PSO_MTL4=1:
- before the fix, every start crashed, both installed from Xcode and in LiveContainer;
- after it, a cold start compiled 18 bases and made 25 specializations at 60 fps over 1200 frames, with
0 [RED]. Later starts take the 18 bases from the archive. The log says "their descriptors returned
autoreleased".
- It runs in LiveContainer too.
- iPhone 16 Pro Max, iOS 26.0: the same crash before the fix.
- Mac (M4 Pro, macOS 27.0), DM_PSO_MTL4=1: 34 specializations, 60 fps, "their descriptors returned
retained".
- make -C dm/gfx/tests GAME=010015100B514000 run on the Mac: 678 PASS, 0 FAIL. clang-tidy: no new finding.
Reference entry in docs/reference/rendering.md; symptom index regenerated.
Landed on main: t_pso_archive 102 checks, t_pso_stall 41, 0 failed on the Mac. No game run.
Co-authored-by: mmkzer0 <[email protected]>
This commit is contained in:
1 parent
a9d06b6f58
commit
88c7e03f51
5 files changed
+75
-5
No files matched your search
+26
-3
@@ -71,6 +71,7 @@ static uint64_t c2_handle(int stage, unsigned b); // the handle c2 holds at by
|
||||
#include <execinfo.h>
|
||||
#include <os/lock.h>
|
||||
#include <objc/runtime.h>
|
||||
#include <objc/message.h>
|
||||
// Function parameters in this file are never retained again in the callee (the caller / global tables already hold them, and lifetime is guaranteed by defer_obj_release): saves a retain / release pair per call on the hot path
|
||||
#pragma clang attribute push(__attribute__((objc_externally_retained)), apply_to = function)
|
||||
|
||||
@@ -7133,6 +7134,27 @@ static id<MTLRenderPipelineState> pso_build_dev(MTLRenderPipelineDescriptor *d,
|
||||
MTLRenderPipelineDescriptor *dd = [d copy]; dd.vertexFunction = vf; dd.fragmentFunction = ff;
|
||||
return [g_dev newRenderPipelineStateWithDescriptor:dd error:e];
|
||||
}
|
||||
// newRenderPipelineDescriptorForSpecialization's descriptor, owned by the caller whatever the driver returns. Its new prefix promises a +1 object, and macOS 27.0 returns one. iOS 26.0
|
||||
// returns it autoreleased (23A341; on an iPad Pro M4 the method is -[AGXG16GFamilyRenderPipeline newRenderPipelineDescriptorForSpecialization] in AGXMetalG16G_B0, and an iPhone 16 Pro Max
|
||||
// crashed the same way). ARC's release at the end of pso_build_ex then freed it, and the release of the warm task's pool crashed in objc_release before the first frame
|
||||
// (NSZombieEnabled: -[MTL4RenderPipelineDescriptor release]: message sent to deallocated instance; Axiom Verge with DM_PSO_MTL4=1). That one release freed it, so the driver keeps no
|
||||
// reference of its own. The first call finds out which: a weak reference outlives the call's own pool only if the caller owns the descriptor. The answer holds for the process: every base
|
||||
// comes from the one device's compiler or its archives (with the Metal debug layer on, every one is wrapped alike). *own: 0 not known yet, 1 returned retained, 2 returned autoreleased
|
||||
static MTL4RenderPipelineDescriptor *spec_desc(id base, _Atomic int *own) { // base: the base pipeline (id: the test gives stand-ins of both kinds)
|
||||
void *(*call)(id, SEL) = (void *(*)(id, SEL))objc_msgSend; SEL sel = @selector(newRenderPipelineDescriptorForSpecialization);
|
||||
int o = atomic_load_explicit(own, memory_order_relaxed);
|
||||
if (o == 1) return (__bridge_transfer MTL4RenderPipelineDescriptor *)call(base, sel);
|
||||
if (o == 2) { MTL4RenderPipelineDescriptor *s; @autoreleasepool { s = (__bridge MTL4RenderPipelineDescriptor *)call(base, sel); } return s; }
|
||||
void *raw; __weak MTL4RenderPipelineDescriptor *w;
|
||||
@autoreleasepool { raw = call(base, sel); w = (__bridge MTL4RenderPipelineDescriptor *)raw; }
|
||||
if (!raw) return nil; // nothing to tell the two apart by
|
||||
MTL4RenderPipelineDescriptor *s = w;
|
||||
if (!s) { atomic_store_explicit(own, 2, memory_order_relaxed); return spec_desc(base, own); } // the pool freed it: ask again for one to keep
|
||||
atomic_store_explicit(own, 1, memory_order_relaxed);
|
||||
CFRelease(raw); // the call's +1; s holds its own
|
||||
return s;
|
||||
}
|
||||
static _Atomic int g_spec_own;
|
||||
// The pipeline pso_desc describes: its base (built, from the archive or already made), then specialized with its colour state. Called on several threads.
|
||||
// compile 0: nil on an archive miss (*e untouched), with no MTLDevice fallback. Prewarm compaction may still refresh a found base for serialization.
|
||||
static id<MTLRenderPipelineState> pso_build_ex(MTLRenderPipelineDescriptor *d, NSError **e, int compile) {
|
||||
@@ -7205,7 +7227,7 @@ static id<MTLRenderPipelineState> pso_build_ex(MTLRenderPipelineDescriptor *d, N
|
||||
}
|
||||
if (!rast) return base; // NOLINT(clang-analyzer-osx.cocoa.RetainCount): job stays in g_pbase_pending (hm_put_id's CFBridgingRetain) until hm_rel releases it, not a leak
|
||||
uint64_t ts = bp_now();
|
||||
MTL4RenderPipelineDescriptor *s = (MTL4RenderPipelineDescriptor *)[base newRenderPipelineDescriptorForSpecialization];
|
||||
MTL4RenderPipelineDescriptor *s = spec_desc(base, &g_spec_own);
|
||||
for (int i = 0; i < 8; i++) pso_col4(s.colorAttachments[i], d.colorAttachments[i]);
|
||||
id<MTLRenderPipelineState> ps = [g_comp4 newRenderPipelineStateBySpecializationWithDescriptor:s pipeline:base error:e];
|
||||
if (ps) g_pb_spec++;
|
||||
@@ -7818,8 +7840,9 @@ static void warm_task(WarmTask *t) {
|
||||
static void warm_finish(void) {
|
||||
MLOG("pipeline prewarm done: shaders %llu / render pipelines %llu (specialized fragment skipped %llu) / compute pipelines %llu, background time %.1f ms; shader keys not in the shader map (another game version) %llu",
|
||||
(unsigned long long)g_warm_shaders, (unsigned long long)g_warm_rps, (unsigned long long)g_warm_skip_fz, (unsigned long long)g_warm_cps, (bp_now() - g_warm_t0) / 1e6, (unsigned long long)g_warm_skip_stale);
|
||||
if (g_comp4) MLOG("pipeline bases so far: %llu compiled, %llu from the archive, %llu reused (%.1f ms for the bases); %llu specializations, %llu pipelines without a base (MTLDevice)",
|
||||
(unsigned long long)g_pb_built, (unsigned long long)g_pb_archived, (unsigned long long)g_pb_reused, g_pb_base_ns / 1e6, (unsigned long long)g_pb_spec, (unsigned long long)g_pb_legacy);
|
||||
if (g_comp4) MLOG("pipeline bases so far: %llu compiled, %llu from the archive, %llu reused (%.1f ms for the bases); %llu specializations (their descriptors %s), %llu pipelines without a base (MTLDevice)",
|
||||
(unsigned long long)g_pb_built, (unsigned long long)g_pb_archived, (unsigned long long)g_pb_reused, g_pb_base_ns / 1e6, (unsigned long long)g_pb_spec,
|
||||
(const char *[]){"not asked for yet", "returned retained", "returned autoreleased"}[atomic_load(&g_spec_own) % 3], (unsigned long long)g_pb_legacy);
|
||||
// All warm tasks have joined. WarmItems retain published shaders until the submit thread claims them.
|
||||
// Drop negative results too: demand loading keeps its existing retry behavior, independent of the warm dictionary.
|
||||
g_warm_dict = nil; g_warm_loads = nil;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
// Metal 4 descriptor keeps the MTLDevice path; pso_key_norm drops what no pipeline depends on; compaction merges the files the prewarm took bases from into one despite a
|
||||
// mid-prewarm flush, and a compaction that did not finish is not tried again on the same system build; depth-only draws write depth; concurrent colour variants compile
|
||||
// one base; archive flushes preserve unrelated compiler state; a function with function constants is specialized by its pipeline's descriptor, not through its library,
|
||||
// and an MTLDevice pipeline gets it specialized
|
||||
// and an MTLDevice pipeline gets it specialized; the descriptor for a specialization is the caller's whether the driver returns it retained or autoreleased
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
@@ -253,6 +253,38 @@ static void capture_preserves_foreign_folder(void) {
|
||||
if (proxy->created && [NSFileManager.defaultManager fileExistsAtPath:proxy->fixture])
|
||||
EXPECT("foreign capture: test-owned fixture removed", 1, [NSFileManager.defaultManager removeItemAtPath:proxy->fixture error:nil]);
|
||||
}
|
||||
// Stand-ins for a base pipeline's newRenderPipelineDescriptorForSpecialization: one returns the descriptor retained, as its name promises (macOS 27.0), the other autoreleased,
|
||||
// as iOS 26.0's driver does. Plain functions, so ARC's naming convention does not decide what they return
|
||||
static __weak MTL4RenderPipelineDescriptor *g_spec_made;
|
||||
static void *spec_retained(id self, SEL cmd) { (void)self; (void)cmd; MTL4RenderPipelineDescriptor *d = [MTL4RenderPipelineDescriptor new]; g_spec_made = d; return (void *)CFBridgingRetain(d); }
|
||||
static void *spec_autoreleased(id self, SEL cmd) { (void)self; (void)cmd; MTL4RenderPipelineDescriptor *d = [MTL4RenderPipelineDescriptor new]; g_spec_made = d; return (void *)CFAutorelease(CFBridgingRetain(d)); }
|
||||
// spec_desc owns the descriptor either way: it outlives every pool once returned (no over-release; the warm task's pool crashed on iOS 26.0) and goes away once dropped (no leak),
|
||||
// on the first call, which finds out how the driver returns it, and on later ones
|
||||
static void spec_desc_ownership(void) {
|
||||
SEL sel = @selector(newRenderPipelineDescriptorForSpecialization);
|
||||
const char *names[2] = {"retained", "autoreleased"}; IMP imps[2] = {(IMP)spec_retained, (IMP)spec_autoreleased};
|
||||
for (int k = 0; k < 2; k++) {
|
||||
Class c = objc_allocateClassPair(NSObject.class, k ? "TSpecBaseAutoreleased" : "TSpecBaseRetained", 0);
|
||||
if (!c) { fprintf(stderr, "spec descriptor %s: no stand-in class\n", names[k]); failed++; continue; }
|
||||
class_addMethod(c, sel, imps[k], "@@:"); objc_registerClassPair(c);
|
||||
id base = [c new]; _Atomic int own = 0;
|
||||
for (int call = 0; call < 2; call++) {
|
||||
char name[96];
|
||||
__weak MTL4RenderPipelineDescriptor *kept; // not w: EXPECT declares its own
|
||||
@autoreleasepool {
|
||||
MTL4RenderPipelineDescriptor *s;
|
||||
@autoreleasepool { s = spec_desc(base, &own); }
|
||||
kept = s;
|
||||
snprintf(name, sizeof name, "spec descriptor %s, call %d: the caller's after its pools", names[k], call + 1);
|
||||
EXPECT(name, 1, s != nil && s == g_spec_made);
|
||||
}
|
||||
snprintf(name, sizeof name, "spec descriptor %s, call %d: freed once dropped", names[k], call + 1);
|
||||
EXPECT(name, 1, kept == nil);
|
||||
snprintf(name, sizeof name, "spec descriptor %s, call %d: ownership found", names[k], call + 1);
|
||||
EXPECT(name, k + 1, atomic_load(&own));
|
||||
}
|
||||
}
|
||||
}
|
||||
int main(int argc, char **argv) { @autoreleasepool {
|
||||
if (!mkdtemp(archive_cache_root)) abort();
|
||||
atexit(archive_cache_cleanup);
|
||||
@@ -451,6 +483,7 @@ int main(int argc, char **argv) { @autoreleasepool {
|
||||
EXPECT("constants: the vertex function given to it is specialized once", 1, fn_dev(cv) != cv && fn_dev(cv) == fn_dev(cv));
|
||||
EXPECT("constants: a function without constants is its own", 1, fn_dev(cd.fragmentFunction) == cd.fragmentFunction);
|
||||
}
|
||||
spec_desc_ownership();
|
||||
capture_preserves_foreign_folder();
|
||||
[NSFileManager.defaultManager removeItemAtPath:@(dir) error:nil];
|
||||
printf("t_pso_archive: %d checks, %d failed\n", checks, failed);
|
||||
|
||||
@@ -112,7 +112,7 @@ int main(void) { @autoreleasepool {
|
||||
}
|
||||
EXPECT("every base in the files", 4, found);
|
||||
// a base whose period is written and gone still specializes (the draws keep bases for the whole run)
|
||||
MTL4RenderPipelineDescriptor *s = (MTL4RenderPipelineDescriptor *)[drawn newRenderPipelineDescriptorForSpecialization];
|
||||
MTL4RenderPipelineDescriptor *s = spec_desc(drawn, &g_spec_own);
|
||||
s.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA8Unorm; s.colorAttachments[0].writeMask = MTLColorWriteMaskAll; s.colorAttachments[0].blendingState = MTL4BlendStateDisabled;
|
||||
NSError *se = nil;
|
||||
EXPECT("a base of a written period specializes", 1, [g_comp4 newRenderPipelineStateBySpecializationWithDescriptor:s pipeline:drawn error:&se] != nil);
|
||||
|
||||
@@ -678,6 +678,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
|
||||
- `FATAL deferred encoding: DmRecEnc has no stand-in for -copyFromTexture:sourceSlice:sourceLevel:sourceOrigin:sourceSize:toBuffer:destinationOffset:destinationBytesPerRow:destinationBytesPerImage:options:` with Metal encoding on its own thread (`DM_NVN_ENC_THREAD=1`, the default on iOS), at the first depth plane copy. → [Depth plane copies on the encode thread](../reference/rendering.md#depth-plane-copies-on-the-encode-thread)
|
||||
- a scene opens near white and fades to its normal colours over several seconds, at each scene change, while the settled picture is right. → [Pending clears land before a compute dispatch](../reference/rendering.md#pending-clears-land-before-a-compute-dispatch)
|
||||
- a regular lattice of dots over the ground, and the light wrong: three of four layers of an interleaved ambient occlusion array stay empty, while the draws and their bindings look right. → [`[[primitive_id]]` in a specialized Metal 4 pipeline](../reference/rendering.md#primitive_id-in-a-specialized-metal-4-pipeline)
|
||||
- with Metal 4 pipelines on (`DM_PSO_MTL4=1`), the iOS app crashes before its first frame, right after `pipeline prewarm list … lines`: `CRASH sig=11 pc=objc_release+0x10`, `bt: objc_autoreleasePoolPop warm_task __warm_prepare_block_invoke`, on several prepare workers at once. The Mac runs the same pipelines. → [A specialization descriptor iOS 26.0 returns autoreleased](../reference/rendering.md#a-specialization-descriptor-ios-260-returns-autoreleased)
|
||||
- a Switch 2 game calls `nvnCommandBufferEvaluateDlss` and its builder functions, which the host did not have. → [NVN2's DLSS on MetalFX](../reference/rendering.md#nvn2s-dlss-on-metalfx)
|
||||
- `[RED] NVN unimplemented function called: nvnDlssNetworkResize (x0=… x1=0x300 x2=0x1b0)` at the title. → [The DLSS network resized](../reference/rendering.md#the-dlss-network-resized)
|
||||
- a Switch 2 game's frame in its heavy scenes is dominated by one compute dispatch between the render-resolution passes and the output-resolution ones: Donkey Kong Bananza's upscaler takes 3.5-5 ms of a 24 ms GPU frame on an M1 in handheld mode (960x540 → 1920x1080, `DM_NVN_GPUPROF`), and the game cannot reach its 60 fps interval there. → [A game's own upscaler drawn by MetalFX](../reference/rendering.md#a-games-own-upscaler-drawn-by-metalfx)
|
||||
|
||||
@@ -1012,6 +1012,19 @@ The replay on Metal (`dm/gfx/nvn/nvn_mtl.m`) and the Vulkan and OpenGL layers. E
|
||||
- **Check:** `t_image_primitive_id` draws four full-screen triangles that store into the layer of their primitive ID through the host's draw path (an attachment-less pass, a metallib loaded as game shaders are). Each of the four layers holds its own primitive's value; without the flag, layers 1 to 3 stay empty (macOS 27.0.1, M3 Pro). `test_pso_offline.py` checks that such a pair is left out.
|
||||
- **Found on:** [Fire Emblem Fortune's Weave](../../dm/games/04007EF01F8AE000/README.md): `b6726be4afc44691`.
|
||||
|
||||
### A specialization descriptor iOS 26.0 returns autoreleased
|
||||
|
||||
- **Symptom:** with Metal 4 pipelines on (`DM_PSO_MTL4=1`), the iOS app crashes before its first frame, right after `pipeline prewarm list … lines`: `CRASH sig=11 pc=objc_release+0x10`, `bt: objc_autoreleasePoolPop warm_task __warm_prepare_block_invoke`, on several prepare workers at once. The Mac runs the same pipelines.
|
||||
- **Cause:** `newRenderPipelineDescriptorForSpecialization` promises a +1 object by its name, so ARC releases it once at the end of `pso_build_ex`. On iOS 26.0 (23A341) the driver returns it autoreleased. On an iPad Pro M4 the method is `-[AGXG16GFamilyRenderPipeline newRenderPipelineDescriptorForSpecialization]` in `AGXMetalG16G_B0`. So ARC's release freed it, and the warm task's pool released it again. Under LLDB on the iPad:
|
||||
- the descriptor sits in the autorelease pool when the call returns (`_objc_autoreleasePoolPrint`);
|
||||
- `NSZombieEnabled=YES` reports `-[MTL4RenderPipelineDescriptor release]: message sent to deallocated instance`.
|
||||
That one release freed it, so the driver keeps no reference of its own. macOS 27.0 (26A428, M4 Pro) returns it retained. There, the direct call made the same prewarm's 34 specializations without a crash.
|
||||
- **Fix:** `spec_desc` (`nvn_mtl.m`) finds out on its first call how the driver returns the descriptor. A weak reference survives the call's own autorelease pool only if the caller owns the descriptor. After that it takes the descriptor the way the driver gives it, so the caller owns exactly one reference either way. The answer holds for the process: every base comes from the one device's compiler or its archives. The `pipeline bases so far` line names it: `their descriptors returned retained` or `returned autoreleased`. `t_pso_stall` specializes through it too.
|
||||
- **Check:** `make -C dm/gfx/tests GAME=01005CA01580E000 check_t_pso_archive` runs `spec_desc` against two stand-in bases, one that returns the descriptor retained and one that returns it autoreleased. On the first call and on later ones, the descriptor outlives every pool and is freed once dropped. The previous direct call dies on the autoreleased stand-in. Axiom Verge with `DM_PSO_MTL4=1`:
|
||||
- on the iPad, it crashed on every start before the fix. Now it runs at 60 fps: the cold start compiled 18 bases and made 25 specializations, and later starts take the bases from the archive. The log says `returned autoreleased`;
|
||||
- on the Mac, it says `returned retained`.
|
||||
- **Found on:** Axiom Verge 1.0.3 (`0100052004384000`, its profile a separate change) with the launcher's **Metal 4 pipelines** on: an iPhone 16 Pro Max (iOS 26.0, LiveContainer) and an iPad Pro 11-inch (M4, iPadOS 26.0, from Xcode and in LiveContainer) crashed the same way.
|
||||
|
||||
### NVN2's DLSS on MetalFX
|
||||
|
||||
- **Symptom:** a Switch 2 game calls `nvnCommandBufferEvaluateDlss` and its builder functions, which the host did not have.
|
||||
|
||||
Reference in new issue
Block a user