diff --git a/.gitignore b/.gitignore index 7d8792d62c..74c000d286 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Build outputs (Xcode derived data, produced by the scripts under dm/ios) +/build/ .iosdd/ .iosdd_mac/ .iosdd_*/ diff --git a/dm/games/0100152000022000/game.tsv b/dm/games/0100152000022000/game.tsv index 6f8dbf4eb1..a765cb2852 100644 --- a/dm/games/0100152000022000/game.tsv +++ b/dm/games/0100152000022000/game.tsv @@ -16,6 +16,7 @@ program_id 0100152000022000 name Mario Kart 8 Deluxe ipa MarioKart8Deluxe.ipa app_category public.app-category.racing-games +pad_layout full # the race records SetRenderEnableConditional as a course loads: evaluated at replay, as on the GPU (drawn anyway, the same race route at the same frames looked the same; evaluated is NVN's behaviour) render_cond on diff --git a/dm/games/0100152000022000/ios.env b/dm/games/0100152000022000/ios.env new file mode 100644 index 0000000000..821ad11115 --- /dev/null +++ b/dm/games/0100152000022000/ios.env @@ -0,0 +1,4 @@ +# Always expose the complete Switch controller surface, including L3/R3 and +# the D-pad, and keep touch controls usable beside a keyboard or controller. +DM_PAD_FORCE_FULL=1 +DM_PAD_KEEP_TOUCH=1 diff --git a/dm/games/0100F43008C44000/game.tsv b/dm/games/0100F43008C44000/game.tsv index 23db0ce27f..ac9b829d15 100644 --- a/dm/games/0100F43008C44000/game.tsv +++ b/dm/games/0100F43008C44000/game.tsv @@ -11,7 +11,8 @@ program_id 0100F43008C44000 name Pokémon Legends: Z-A ipa PokMonLegendsZA.ipa app_category public.app-category.games -pad_layout simple +pad_layout full +render_cond on # ---- Versions ---- build v393216 2.0.2 main=b1f12fd919eae86ab8a978317677e64b sdk=df7a3960c74c3a4803f826c587153252 @@ -23,6 +24,8 @@ shader_versions v393216 modules main sdk romfs_marker arc/data.trpfs zip_store .bnk:.pck:.trpfd:.trpfs:.metallib +# Old Apple GPU builds are invalid without this profile's proven shader companions. +ios_companions ios_companions.json # ---- Memory ---- # nninitStartup (main+0x1b2e650) initializes its StandardAllocator with virtual address memory over 12 GB of address space (StandardAllocator::Initialize(nullptr, 0x300000000), main+0x1b3e3ec), diff --git a/dm/games/0100F43008C44000/ios.env b/dm/games/0100F43008C44000/ios.env new file mode 100644 index 0000000000..26532e453c --- /dev/null +++ b/dm/games/0100F43008C44000/ios.env @@ -0,0 +1,11 @@ +# Production iOS defaults for Pokemon Legends: Z-A 2.0.2. +# Documents/env.txt has priority over every value below. +DM_PRESENT_INTERVAL=game +DM_ZA_SKILL_LAYOUT=1 +DM_DISPLAY_HZ=120 +DM_PRESENT_HZ=120 +DM_ZA_FPS_MODE=dynamic60 +# Always expose the complete Switch controller surface, including L3/R3 and +# the D-pad, and keep touch controls usable beside a keyboard or controller. +DM_PAD_FORCE_FULL=1 +DM_PAD_KEEP_TOUCH=1 diff --git a/dm/games/0100F43008C44000/ios_companions.json b/dm/games/0100F43008C44000/ios_companions.json new file mode 100644 index 0000000000..b4433fdc22 --- /dev/null +++ b/dm/games/0100F43008C44000/ios_companions.json @@ -0,0 +1,55 @@ +{ + "schema": 1, + "manifests": [ + { + "directory": "layered-v1", + "kind": "layered-slices-v1", + "required_records": [ + "57246f9a04af8529_b", + "d7d72bbbb0e32517_b", + "b29f08619f379a83_b", + "5b931243ad28aeeb_b" + ] + }, + { + "directory": "nonpoint-v1", + "required_records": [ + "94a4c491a475b170_b", + "ce7eacf81c6e4ffc_b" + ] + }, + { + "directory": "sampler-limit-v1", + "kind": "sampler-limit-v1", + "limit": 16, + "required_records": [ + "a6c06cda878a4fe0_b" + ] + }, + { + "directory": "guarded-index-v1", + "kind": "guarded-index-v1", + "original_directory": "nonpoint-v1", + "required_records": [ + "94a4c491a475b170_b", + "ce7eacf81c6e4ffc_b" + ] + }, + { + "directory": "zero-image-v1", + "kind": "zero-image-clear-v1", + "required_records": [ + "5b107ddfd06551fc_b" + ] + } + ], + "records": [ + { + "directory": "atomic-v1", + "kind": "bounded-image-atomics-v1", + "required_records": [ + "2bd5037003980716_b" + ] + } + ] +} diff --git a/dm/games/0100F43008C44000/patches.py b/dm/games/0100F43008C44000/patches.py index 5fbdeac064..67e608146f 100644 --- a/dm/games/0100F43008C44000/patches.py +++ b/dm/games/0100F43008C44000/patches.py @@ -8,12 +8,23 @@ (0x1b3e3e4, '6200c0d2', '2200c0d2', 'StandardAllocator::Initialize(nullptr, size): address space mov x2,#0x300000000 → mov x2,#0x100000000 (12 GB → 4 GB)'), ], } -# The same cut for builds without registered CUTS (dm/tools/nso_match.py), found by the fingerprint of the function that makes the call (132 bytes, the mov at +52) and its direct -# callees' (the allocator option getter it calls first, StandardAllocator::Initialize). The base game 1.0.0 (v0, main 7222e13e…, GitHub issue #266) has it at main+0x19f84b4 -# (function main+0x19f8480: an exact nso_fnmap.py match of 2.0.2's main+0x1b3e3b0, the same fingerprint) and ships 2.0.2's sdk (df7a3960…), so the node area cut and the -# 5 GB layout hold for it unchanged -allocator = 'StandardAllocator::Initialize(nullptr, size): address space 12 GB → 4 GB' -MATCH_CUTS = {'main': [ - {'name': allocator, 'size': 132, 'fingerprints': ['af8eeda3d34bbfbfee1896ee0469ada1804c957695f3c7f2b8a970a20f495879'], - 'cuts': [(52, '6200c0d2', '2200c0d2', allocator + ': mov x2,#0x300000000 → mov x2,#0x100000000 (as 2.0.2 main+0x1b3e3e4)')]}, -]} + +# No timing behavior changes by default. Both callbacks return without touching +# guest registers unless their diagnostics / experiment is explicitly enabled. +# hle_za_timing.c checks the same build ID again at runtime. +OBS = { + 'b1f12fd919eae86ab8a978317677e64b': [ + # Clock channel 0 returns {total_ns, delta_ns}. The next instructions convert + # delta_ns / 1e9 to float and hand it to engine updates (main+0x1b29314/334). + (0x1b292dc, 26, 'fd400400', 'Trinity base-clock delta: orig ldr d0,[x0,#8]; observe game seconds versus monotonic time'), + # After the configured interval and native adaptive candidate were selected, + # immediately after bl main+0x113488. The original mov forwards w20 to the + # interval setter. An opt-in mutable callback can reproduce Static60 without + # changing an instruction in memory; off preserves w20 and w22 exactly. + (0x1b296f0, 27, '2a1403e1', 'Trinity interval: orig mov w1,w20; optional saved-register experiment with timing diagnostics'), + # Generic layout visibility, filtered by the exact WazaLayoutView vtable in + # the host. Diagnostics only: no input remapping or register changes. + (0x425c70, 28, 'f9405408', 'Layout visibility: orig ldr x8,[x0,#0xa8]; observe the actual battle move layout, not ZL alone'), + (0x2b7bfbc, 29, 'a9be7bfd', 'Waza layout destructor: orig stp x29,x30,[sp,#-0x20]!; expire the observed skill context'), + ], +} diff --git a/dm/games/README.md b/dm/games/README.md index 3623bfd23e..fd49851d82 100644 --- a/dm/games/README.md +++ b/dm/games/README.md @@ -12,6 +12,7 @@ Each game the port knows has a folder `dm/games//`, named after the base gam - **`bf_want.tsv`**. - **`README.md`**: the game's notes: what its profile needs and why, what was found on it, how to reach a scene on the Mac, what is still open. Listed in [Games](#games). - **`patches.py`** (optional): the rewriter's patches of the game's own builds (`main`), keyed by NSO build ID: `CUTS` (byte cuts, each original asserted), `OBS` (observation gates) and `FRAME_STEP` (frame-step stubs). `dm/tools/patch_nso.py` loads every profile's file and applies those of a module's build, wherever it comes from; a build claimed twice stops the rewrite. A provisional profile (in `DM_GAMES_DIR`) may have one too, which the rewriter and the NSP script's rewrite step take as well. Cuts of Nintendo's `sdk` modules, which several games ship, are in `dm/tools/sdk_patches.py`. Opt-in `MATCH_CUTS` can locate verified cuts in unregistered builds of the selected game by function fingerprints (a patch of several functions applies whole or not at all); see [Function-matched cuts](../../docs/reference/rewriter.md#function-matched-cuts-for-unregistered-builds) for enrollment, profile precedence and limits. A game without patches has no file, and a derived profile uses its repository profile's rules. `dm/tools/tests/test_patches.py` checks that a file names only its own profile's builds. +- **`ios.env`** (optional): production iOS-only `KEY=VALUE` defaults bundled as `dm/env.txt`. An explicit `DM_ENV_FILE=` replaces it, `DM_ENV_FILE=` disables it, and the player's `Documents/env.txt` overrides either source at runtime. It is not read by the Mac host or other platforms. The chain version is not in the profile: one version serves every game (`dm/gfx/nvn/dmsh_chain.h`, written by `dmsh_chain.py --header`). Two more tables live in the profile folder but are not committed (git ignores them). A full `dmsh_build.py` run makes both from its Mac outputs, so each checkout makes its own, and stamps its libraries with the digest of the profile's inputs (`b-/inputs.tsv`, `dmsh_chain.py --inputs`). The host takes a table only when its header names its library's chain version and that digest. - **`c1_full.tsv`**: written by `c1_guard.py`. Without it every shader's c1 gets its own 64 KiB. diff --git a/dm/gfx/nvn/dmsh_chain.h b/dm/gfx/nvn/dmsh_chain.h index 0315568697..749e15aa18 100644 --- a/dm/gfx/nvn/dmsh_chain.h +++ b/dm/gfx/nvn/dmsh_chain.h @@ -1,3 +1,3 @@ // Generated file (dm/gfx/tools/dmsh_chain.py --header): the B shader library translation chain version this source tree expects (default options), one for every game; // the host loads only /b-/. Run dmsh_chain.py --header after changing the chain's tools and commit this file with the change -#define DMSH_CHAIN "b34b226edf5a" +#define DMSH_CHAIN "0cb0dfeacee3" diff --git a/dm/gfx/nvn/nvn_argument_encoding.h b/dm/gfx/nvn/nvn_argument_encoding.h new file mode 100644 index 0000000000..4142ef413e --- /dev/null +++ b/dm/gfx/nvn/nvn_argument_encoding.h @@ -0,0 +1,71 @@ +#pragma once +// Tier 1 argument buffers have a GPU-private layout. Populate their public +// encoder instead of assuming an eight-byte pointer/resource-ID representation. +#import +#include "nvn_refl.h" +#include +#include + +static int nvn_argument_encode(id encoder, id destination, + NSUInteger offset, const nvn_refl *reflection, const uint64_t *slots, + size_t count, char *error, size_t error_size) { + if (!encoder || !destination || !reflection || !slots || reflection->nb < 0 || reflection->nb > NVN_REFL_MAXB || + offset > destination.length || encoder.encodedLength > destination.length - offset || + (encoder.alignment && offset % encoder.alignment)) { + snprintf(error, error_size, "invalid encoder, destination, alignment or reflection"); return 0; + } + @try { + [encoder setArgumentBuffer:destination offset:offset]; + for (int i = 0; i < reflection->nb; i++) { + const nvn_rbind *binding = &reflection->b[i]; + unsigned n = binding->n ? binding->n : 1; + if (binding->aid >= count || n > count - binding->aid) { + snprintf(error, error_size, "argument index %u length %u exceeds slot capacity %zu", binding->aid, n, count); return 0; + } + for (unsigned element = 0; element < n; element++) { + NSUInteger index = binding->aid + element; + uint64_t token = slots[index]; + switch (binding->kind) { + case NVN_RB_CBUF: case NVN_RB_SBUFP: case NVN_RB_SBUFU: case NVN_RB_UBOP: + case NVN_RB_SUPPORT: case NVN_RB_C1: case NVN_RB_DRV0: case NVN_RB_DRV2: + case NVN_RB_IMGA: case NVN_RB_GEOMETRY: { + NSUInteger buffer_offset = 0; + id buffer = legacy_buffer_at(token, &buffer_offset); + if (token && !buffer) { snprintf(error, error_size, "unregistered buffer token at argument %lu", (unsigned long)index); return 0; } + [encoder setBuffer:buffer offset:buffer_offset atIndex:index]; + break; + } + case NVN_RB_TEX: case NVN_RB_TEXU: case NVN_RB_IMG: case NVN_RB_TBUF: + case NVN_RB_TEXD: case NVN_RB_TEXA: + [encoder setTexture:(__bridge id)(void *)(uintptr_t)token atIndex:index]; break; + case NVN_RB_SAMP: case NVN_RB_SAMPT: case NVN_RB_SAMPU: case NVN_RB_SAMPI: + case NVN_RB_SAMPD: case NVN_RB_SAMPA: + [encoder setSamplerState:(__bridge id)(void *)(uintptr_t)token atIndex:index]; break; + default: + snprintf(error, error_size, "unsupported binding kind %u at argument %lu", binding->kind, (unsigned long)index); return 0; + } + } + } + } @catch (NSException *exception) { + snprintf(error, error_size, "%s: %s", exception.name.UTF8String, exception.reason.UTF8String); return 0; + } + return 1; +} + +// Use argumentIndex rather than fixed byte offsets when reading the compiled +// pointer access on a device whose argument layout is private. +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" +static void nvn_argument_private_access(nvn_refl *reflection, MTLArgument *argument) { + if (!argument || argument.type != MTLArgumentTypeBuffer || argument.index != (NSUInteger)reflection->abi || + argument.bufferDataType != MTLDataTypeStruct) return; + for (MTLStructMember *member in argument.bufferStructType.members) { + if (member.dataType != MTLDataTypePointer) continue; + MTLPointerType *pointer = member.pointerType; + if (!pointer || pointer.elementIsArgumentBuffer || + (pointer.access != MTLArgumentAccessReadWrite && pointer.access != MTLArgumentAccessWriteOnly)) continue; + for (int i = 0; i < reflection->nb; i++) if (reflection->b[i].kind == NVN_RB_SBUFP && + reflection->b[i].aid == member.argumentIndex) reflection->b[i].rw = 1; + } +} +#pragma clang diagnostic pop diff --git a/dm/gfx/nvn/nvn_cpu_decode_cache.h b/dm/gfx/nvn/nvn_cpu_decode_cache.h new file mode 100644 index 0000000000..fbf47c0662 --- /dev/null +++ b/dm/gfx/nvn/nvn_cpu_decode_cache.h @@ -0,0 +1,143 @@ +// Exact, bounded cache of CPU-decoded BC pixels. The caller still uploads every +// dirty candidate: only CPU decoding is reused, so image writes and lost-upload +// retries retain the ordinary upload behavior. Serialized by the upload owner. +#pragma once +#include "nvn_tex.h" +#include +#include +#include +#include +#include + +#ifndef NVN_CPU_CACHE_LOG +#define NVN_CPU_CACHE_LOG(...) do { fprintf(stderr, "[cpu-decode-cache] " __VA_ARGS__); fputc('\n', stderr); } while (0) +#endif +#ifndef NVN_CPU_CACHE_BUDGET +#define NVN_CPU_CACHE_BUDGET (32u * 1024u * 1024u) +#endif +#ifndef NVN_CPU_CACHE_SOURCE_MAX +#define NVN_CPU_CACHE_SOURCE_MAX (8u * 1024u * 1024u) +#endif +#ifndef NVN_CPU_CACHE_DECODED_MAX +#define NVN_CPU_CACHE_DECODED_MAX (16u * 1024u * 1024u) +#endif +enum { CPU_CACHE_SLOTS = 32 }; +typedef struct { + uint64_t serial, offset, source_bytes, decoded_bytes; + const void *mem, *pool; + nvn_texdesc desc; +} nvn_cpu_decode_key; +typedef struct { + nvn_cpu_decode_key key; + uint8_t *source, *decoded; + uint64_t used; + int valid; +} nvn_cpu_decode_entry; +typedef struct { + nvn_cpu_decode_entry *entry; + const uint8_t *source; + uint8_t *decoded; + int hit; +} nvn_cpu_decode_ticket; +static struct { + nvn_cpu_decode_entry entries[CPU_CACHE_SLOTS]; + uint64_t bytes, clock, hits, misses, evictions, uncached, compare_ns, copy_ns; + uint64_t polled_frame, reported_frame; + int initialized, on; +} g_cpu_decode_cache; + +static uint64_t nvn_cpu_cache_ns(void) { + struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); + return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; +} +static void nvn_cpu_decode_drop(nvn_cpu_decode_entry *e) { + if (!e->source && !e->decoded) return; + g_cpu_decode_cache.bytes -= e->key.source_bytes + e->key.decoded_bytes; + free(e->source); free(e->decoded); memset(e, 0, sizeof *e); +} +static void nvn_cpu_decode_clear(void) { + for (int i = 0; i < CPU_CACHE_SLOTS; i++) nvn_cpu_decode_drop(&g_cpu_decode_cache.entries[i]); +} +static int nvn_cpu_decode_on(uint64_t frame) { + if (!g_cpu_decode_cache.initialized || g_cpu_decode_cache.polled_frame != frame) { + g_cpu_decode_cache.initialized = 1; g_cpu_decode_cache.polled_frame = frame; + const char *e = getenv("DM_NVN_CPU_DECODE_CACHE"); int on = e && !strcmp(e, "1"); + if (on != g_cpu_decode_cache.on) { + nvn_cpu_decode_clear(); g_cpu_decode_cache.on = on; + NVN_CPU_CACHE_LOG("[CPU-DECODE-CACHE] %s frame=%llu budget=%u source_cap=%u decoded_cap=%u; exact bytes, every upload retained", + on ? "enabled" : "disabled", (unsigned long long)frame, + NVN_CPU_CACHE_BUDGET, NVN_CPU_CACHE_SOURCE_MAX, NVN_CPU_CACHE_DECODED_MAX); + } + } + return g_cpu_decode_cache.on; +} +static int nvn_cpu_decode_same_key(const nvn_cpu_decode_key *a, const nvn_cpu_decode_key *b) { + return a->serial == b->serial && a->offset == b->offset && a->mem == b->mem && a->pool == b->pool + && a->source_bytes == b->source_bytes && a->decoded_bytes == b->decoded_bytes + && !memcmp(&a->desc, &b->desc, sizeof a->desc); +} +static nvn_cpu_decode_entry *nvn_cpu_decode_oldest(nvn_cpu_decode_entry *except) { + nvn_cpu_decode_entry *old = NULL; + for (int i = 0; i < CPU_CACHE_SLOTS; i++) { + nvn_cpu_decode_entry *e = &g_cpu_decode_cache.entries[i]; + if (e != except && e->source && (!old || e->used < old->used)) old = e; + } + return old; +} +// Called only on a CPU-BC reupload, after its normal protect + generation stamp. +// The owned source is used for a miss's decode. A subsequent guest write cannot +// make the cached baseline disagree with the bytes actually decoded. +static nvn_cpu_decode_ticket nvn_cpu_decode_begin(uint64_t frame, const nvn_cpu_decode_key *key) { + nvn_cpu_decode_ticket out = {0}; + if (!nvn_cpu_decode_on(frame)) return out; + if (!key->serial || !key->mem || !key->source_bytes || !key->decoded_bytes + || key->source_bytes > NVN_CPU_CACHE_SOURCE_MAX || key->decoded_bytes > NVN_CPU_CACHE_DECODED_MAX + || key->source_bytes + key->decoded_bytes > NVN_CPU_CACHE_BUDGET) { + g_cpu_decode_cache.uncached++; return out; + } + nvn_cpu_decode_entry *e = NULL; + for (int i = 0; i < CPU_CACHE_SLOTS; i++) + if (g_cpu_decode_cache.entries[i].source && g_cpu_decode_cache.entries[i].key.serial == key->serial) { e = &g_cpu_decode_cache.entries[i]; break; } + if (e && e->valid && nvn_cpu_decode_same_key(&e->key, key)) { + uint64_t start = nvn_cpu_cache_ns(); + int same = !memcmp(e->source, key->mem, (size_t)key->source_bytes); + g_cpu_decode_cache.compare_ns += nvn_cpu_cache_ns() - start; + if (same) { + e->used = ++g_cpu_decode_cache.clock; g_cpu_decode_cache.hits++; + return (nvn_cpu_decode_ticket){e, e->source, e->decoded, 1}; + } + } + g_cpu_decode_cache.misses++; + if (!e) for (int i = 0; i < CPU_CACHE_SLOTS; i++) + if (!g_cpu_decode_cache.entries[i].source) { e = &g_cpu_decode_cache.entries[i]; break; } + if (!e) { e = nvn_cpu_decode_oldest(NULL); g_cpu_decode_cache.evictions++; } + if (!e) return out; + nvn_cpu_decode_drop(e); + uint64_t need = key->source_bytes + key->decoded_bytes; + while (g_cpu_decode_cache.bytes + need > NVN_CPU_CACHE_BUDGET) { + nvn_cpu_decode_entry *old = nvn_cpu_decode_oldest(e); + if (!old) return out; + nvn_cpu_decode_drop(old); g_cpu_decode_cache.evictions++; + } + uint8_t *source = malloc((size_t)key->source_bytes); + uint8_t *decoded = calloc(1, (size_t)key->decoded_bytes); + if (!source || !decoded) { free(source); free(decoded); g_cpu_decode_cache.uncached++; return out; } + uint64_t start = nvn_cpu_cache_ns(); memcpy(source, key->mem, (size_t)key->source_bytes); + g_cpu_decode_cache.copy_ns += nvn_cpu_cache_ns() - start; + e->key = *key; e->source = source; e->decoded = decoded; e->used = ++g_cpu_decode_cache.clock; + g_cpu_decode_cache.bytes += need; + return (nvn_cpu_decode_ticket){e, source, decoded, 0}; +} +static void nvn_cpu_decode_finish(nvn_cpu_decode_ticket *t, int decoded_ok) { + if (!t->entry || t->hit) return; + if (decoded_ok) t->entry->valid = 1; else nvn_cpu_decode_drop(t->entry); +} +static void nvn_cpu_decode_report(uint64_t frame) { + if (!g_cpu_decode_cache.on || frame / 300 == g_cpu_decode_cache.reported_frame) return; + g_cpu_decode_cache.reported_frame = frame / 300; + NVN_CPU_CACHE_LOG("[CPU-DECODE-CACHE] frame=%llu hits=%llu misses=%llu evictions=%llu uncached=%llu retained_bytes=%llu compare_ms=%.3f source_copy_ms=%.3f", + (unsigned long long)frame, (unsigned long long)g_cpu_decode_cache.hits, + (unsigned long long)g_cpu_decode_cache.misses, (unsigned long long)g_cpu_decode_cache.evictions, + (unsigned long long)g_cpu_decode_cache.uncached, (unsigned long long)g_cpu_decode_cache.bytes, + g_cpu_decode_cache.compare_ns / 1e6, g_cpu_decode_cache.copy_ns / 1e6); +} diff --git a/dm/gfx/nvn/nvn_dirty.c b/dm/gfx/nvn/nvn_dirty.c index d31d8cf6d4..e88952126a 100644 --- a/dm/gfx/nvn/nvn_dirty.c +++ b/dm/gfx/nvn/nvn_dirty.c @@ -123,9 +123,10 @@ static void prep_write(void *addr, size_t len) { } } +static void nvn_dirty_fault_diagnostic(const void *addr); void nvn_dirty_add_range(const void *mem, size_t size) { static int inited; - if (!inited) { inited = 1; g_pg = (uintptr_t)getpagesize(); g_shift = __builtin_ctzl(g_pg); mach_timebase_info(&g_tb); dm_fault_hook_register(on_fault); dm_guest_write_prep_register(prep_write); } + if (!inited) { inited = 1; g_pg = (uintptr_t)getpagesize(); g_shift = __builtin_ctzl(g_pg); mach_timebase_info(&g_tb); dm_fault_hook_register(on_fault); dm_guest_write_prep_register(prep_write); dm_fault_diagnostic_register(nvn_dirty_fault_diagnostic); } atomic_fetch_add(&g_adds_total, 1); pthread_mutex_lock(&g_slot_mu); int n = atomic_load(&g_nr); @@ -177,8 +178,15 @@ int nvn_dirty_tracked(const void *addr, size_t len) { const void *nvn_dirty_span(const void *addr, size_t len) { return len ? find_span((uintptr_t)addr, len) : NULL; } // Set read-only: mark the pages BUSY first and mark them PROT only after mprotect has landed (for the order and the invariant see the state notes at the top of the file) void nvn_dirty_protect(const void *addr, size_t len) { + // Keep the descriptor live until the final mprotect has landed. Otherwise + // retirement can restore RW and publish live=0 between lookup and PG_BUSY, + // after which this thread leaves retired guest memory read-only forever. + pthread_mutex_lock(&g_slot_mu); range_t *r = len ? find_span((uintptr_t)addr, len) : NULL; - if (!r) return; + if (!r) { pthread_mutex_unlock(&g_slot_mu); return; } +#ifdef NVN_DIRTY_PROTECT_LOOKUP_HOOK + NVN_DIRTY_PROTECT_LOOKUP_HOOK(); // Deterministic pool-retirement race test. +#endif uintptr_t a = (uintptr_t)addr, p = (a - r->base) >> g_shift, q = (a + len - 1 - r->base) >> g_shift; // Consecutive pages are merged into one mprotect (per-page calls reach thousands per frame on the first upload of large textures; character select page startup frame 5: 4888 calls). // Pages already PROT / BUSY break the consecutive run: do not protect again, and do not touch pages another thread is changing. @@ -192,6 +200,7 @@ void nvn_dirty_protect(const void *addr, size_t len) { if (!took && open) { mp(r->base + (run << g_shift), (i - run) << g_shift, PROT_READ); for (uintptr_t k = run; k < i; k++) atomic_store(&r->state[k], PG_PROT); open = 0; } if (!took && i <= q && st == PG_BUSY && busy_wait(r, i)) i--; // this page again } + pthread_mutex_unlock(&g_slot_mu); } uint64_t nvn_dirty_now(void) { return atomic_load(&g_now); } int nvn_dirty_since_span(const void *span, const void *addr, size_t len, uint64_t stamp) { @@ -210,6 +219,21 @@ void nvn_dirty_slot_stats(int *live, int *used_slots, uint64_t *adds_total) { *live = active; *used_slots = n; *adds_total = atomic_load(&g_adds_total); } void nvn_dirty_stats(uint64_t *faults, uint64_t *preps) { *faults = atomic_load(&g_faults); *preps = atomic_load(&g_preps); } +// Terminal crash diagnostics only: never changes protection or retries a fault. +static void nvn_dirty_fault_diagnostic(const void *addr) { + uintptr_t a = (uintptr_t)addr; int found = 0, n = atomic_load(&g_nr); + for (int i = 0; i < n; i++) { + range_t *r = rg(i); int live = live_at(i); + if (a < r->base || a >= r->end) continue; + uintptr_t p = (a - r->base) >> g_shift; + if (p >= r->cap) continue; + LOG("fault dirty slot=%d live=%d mem=%p range=%p..%p page=%zu state=%u gen=%llu", + i, live, (void *)r->mem, (void *)r->base, (void *)r->end, (size_t)p, + (unsigned)atomic_load(&r->state[p]), (unsigned long long)atomic_load(&r->gen[p])); + found++; + } + if (!found) LOG("fault dirty: no current or retired slot covers %p (%d slots)", addr, n); +} void nvn_dirty_mp_stats(uint64_t *calls, uint64_t *pages, uint64_t *ns) { static mach_timebase_info_data_t tb; if (!tb.denom) mach_timebase_info(&tb); *calls = atomic_load(&g_mp_calls); *pages = atomic_load(&g_mp_pages); *ns = atomic_load(&g_mp_ticks) * tb.numer / tb.denom; diff --git a/dm/gfx/nvn/nvn_entry.S b/dm/gfx/nvn/nvn_entry.S index 9fd930b082..e318091624 100644 --- a/dm/gfx/nvn/nvn_entry.S +++ b/dm/gfx/nvn/nvn_entry.S @@ -1,4 +1,6 @@ +#if defined(__ARM_FEATURE_ATOMICS) .arch_extension lse +#endif // Common entry for unimplemented NVN functions: when the thunk comes in, x16 = this function's slot address (nvn_slot[i]). // Calls nvn_unimpl_c(slot, x0, x1, x2) to report a failure and log the name, returns 0. .text @@ -67,7 +69,14 @@ _nvn_prof_entry: adrp x10, _nvn_prof_ticks@PAGE add x10, x10, _nvn_prof_ticks@PAGEOFF add x10, x10, x16, lsl #3 +#if defined(__ARM_FEATURE_ATOMICS) stadd x9, [x10] +#else +1: ldxr x11, [x10] + add x11, x11, x9 + stxr w12, x11, [x10] + cbnz w12, 1b +#endif ldp x0, x1, [sp, #16] ldp q0, q1, [sp, #96] ldp q2, q3, [sp, #128] diff --git a/dm/gfx/nvn/nvn_guarded_index.h b/dm/gfx/nvn/nvn_guarded_index.h new file mode 100644 index 0000000000..20241eeeb4 --- /dev/null +++ b/dm/gfx/nvn/nvn_guarded_index.h @@ -0,0 +1,50 @@ +#pragma once + +// Apple3 compatibility experiment for a suspected speculative constant-buffer +// read in a disabled branch. Replace only index seeds proven dead whenever +// the guarded read is active. Keep the original shader chain and point/TF +// entries; this source is consumed only by the non-point compatibility path. +static NSData *guarded_index_source(NSString *key, NSData *original, NSData *library, NSError **error) { + if (g_fragment_image_native) return original; // Apple4/Mac1 and later. + static NSDictionary *manifest; + static dispatch_once_t once; + NSString *directory = [g_msl_dir stringByAppendingPathComponent:@"guarded-index-v1"]; + dispatch_once(&once, ^{ + NSData *data = [NSData dataWithContentsOfFile:[directory stringByAppendingPathComponent:@"manifest.json"]]; + manifest = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil; + }); + if (![manifest isKindOfClass:NSDictionary.class]) return original; + id shaders = manifest[@"shaders"]; + if (![shaders isKindOfClass:NSDictionary.class]) { + if (error) *error = [NSError errorWithDomain:@"NVNGuardedIndex" code:1 userInfo:@{NSLocalizedDescriptionKey:@"Invalid guarded-index shader table"}]; + return nil; + } + NSDictionary *record = shaders[key]; + if (!record) return original; + NSString *reason = nil; + if (![record isKindOfClass:NSDictionary.class]) reason = @"Invalid guarded-index shader record"; + if (!reason && (![manifest[@"schema"] isEqual:@1] || ![manifest[@"chain"] isEqual:@DMSH_CHAIN] || + ![manifest[@"kind"] isEqual:@"guarded-index-v1"] || + ![record[@"proof"] isEqual:@"dead-guarded-index-seeds-v1"] || + ![record[@"replacement_count"] isKindOfClass:NSNumber.class] || + [record[@"replacement_count"] unsignedIntegerValue] == 0)) reason = @"Invalid guarded-index source proof"; + if (reason) { + if (error) *error = [NSError errorWithDomain:@"NVNGuardedIndex" code:1 userInfo:@{NSLocalizedDescriptionKey:reason}]; + return nil; + } + NSString *name = record[@"source"]; + if (!reason && (![name isKindOfClass:NSString.class] || ![name isEqual:name.lastPathComponent])) reason = @"Invalid guarded-index source path"; + NSData *source = reason ? nil : [NSData dataWithContentsOfFile:[directory stringByAppendingPathComponent:name]]; + NSData *reflection = reason ? nil : [NSData dataWithContentsOfFile:[g_msl_dir stringByAppendingPathComponent:[key stringByAppendingString:@".json"]]]; + if (!reason && (![record[@"source_sha256"] isEqual:nonpoint_digest(source)] || + ![record[@"original_source_sha256"] isEqual:nonpoint_digest(original)] || + ![record[@"base_library_sha256"] isEqual:nonpoint_digest(library)] || + ![record[@"reflection_sha256"] isEqual:nonpoint_digest(reflection)])) reason = @"Guarded-index source, base library or reflection hash mismatch"; + if (reason) { + if (error) *error = [NSError errorWithDomain:@"NVNGuardedIndex" code:1 userInfo:@{NSLocalizedDescriptionKey:reason}]; + return nil; + } + MLOG("Guarded-index vertex %s: verified dead index seeds (%lu replacements)", key.UTF8String, + (unsigned long)[record[@"replacement_count"] unsignedIntegerValue]); + return source; +} diff --git a/dm/gfx/nvn/nvn_layered_slices.h b/dm/gfx/nvn/nvn_layered_slices.h new file mode 100644 index 0000000000..0efe6291cc --- /dev/null +++ b/dm/gfx/nvn/nvn_layered_slices.h @@ -0,0 +1,36 @@ +#pragma once +// The companion is generated from the exact base library/reflection pair. +// Only entries whose instance-layer expression and resource access were proven +// by export_layered_sources.py are eligible for repeated vertex execution. +static id layered_slice_library(NSString *key, id original, + NSData *reflection, int *variant, NSError **error) { + static NSDictionary *manifest; + static dispatch_once_t once; + NSString *directory = [g_msl_dir stringByAppendingPathComponent:@"layered-v1"]; + dispatch_once(&once, ^{ + NSData *data = [NSData dataWithContentsOfFile:[directory stringByAppendingPathComponent:@"manifest.json"]]; + manifest = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil; + }); + NSString *reason = nil; + if (![manifest[@"schema"] isEqual:@1] || ![manifest[@"chain"] isEqual:@DMSH_CHAIN] || + ![manifest[@"kind"] isEqual:@"layered-slices-v1"]) reason = @"Missing or mismatched layered-v1 companion manifest"; + NSDictionary *record = manifest[@"shaders"][key]; + if (!reason && !record) return original; + if (!reason && (![record[@"supported"] isEqual:@YES] || ![record[@"proof"] isEqual:@"instance-layer-pure-v1"] || + ![record[@"slot"] isEqual:@30])) reason = [@"Unproven per-slice shader: " stringByAppendingString:record[@"reason"] ?: @"unknown proof"]; + NSData *source = reason ? nil : [NSData dataWithContentsOfFile:[directory stringByAppendingPathComponent:[key stringByAppendingString:@".metal"]]]; + NSData *base = reason ? nil : [NSData dataWithContentsOfFile:[g_msl_dir stringByAppendingPathComponent:[key stringByAppendingString:@".metallib"]] options:NSDataReadingMappedIfSafe error:nil]; + if (!reason && (![record[@"source_sha256"] isEqual:nonpoint_digest(source)] || + ![record[@"library_sha256"] isEqual:nonpoint_digest(base)] || + ![record[@"reflection_sha256"] isEqual:nonpoint_digest(reflection)])) reason = @"Per-slice source, reflection or library hash mismatch"; + if (reason) { + if (error) *error = [NSError errorWithDomain:@"NVNLayeredSlices" code:1 userInfo:@{NSLocalizedDescriptionKey:reason}]; + return nil; + } + NSString *text = [[NSString alloc] initWithData:source encoding:NSUTF8StringEncoding]; + MTLCompileOptions *options = [MTLCompileOptions new]; options.languageVersion = MTLLanguageVersion3_0; + options.preserveInvariance = YES; options.fastMathEnabled = NO; + id library = [g_dev newLibraryWithSource:text options:options error:error]; + if (library) { *variant = 1; MLOG("Per-slice shader %s: verified instance-layer proof, compatibility buffer 30", key.UTF8String); } + return library; +} diff --git a/dm/gfx/nvn/nvn_legacy_buffers.h b/dm/gfx/nvn/nvn_legacy_buffers.h new file mode 100644 index 0000000000..b6c1905eb1 --- /dev/null +++ b/dm/gfx/nvn/nvn_legacy_buffers.h @@ -0,0 +1,89 @@ +#pragma once +// Buffer-only weak address index. Membership changes under this lock; the +// owning resource table/deferred-release queue controls buffer lifetime. +// Dynamic capacity avoids silently dropping buffers in larger game profiles. +typedef struct { + uint64_t gpu, length; + __unsafe_unretained id buffer; +} legacy_buffer_entry; +static legacy_buffer_entry *g_legacy_buffers; +static size_t g_legacy_count, g_legacy_capacity; +static os_unfair_lock g_legacy_lock = OS_UNFAIR_LOCK_INIT; +static int g_legacy_direct_arguments = 1; +static uint64_t g_legacy_next_buffer = UINT64_C(0x10000000000); +static void legacy_arguments_configure(id device) { + const char *force = getenv("DM_NVN_ARGUMENT_ENCODER"); + g_legacy_direct_arguments = device.argumentBuffersSupport == MTLArgumentBuffersTier2 && !(force && atoi(force)); +} +static void legacy_buffer_add(id b) { + if (!b) return; + os_unfair_lock_lock(&g_legacy_lock); + for (size_t i = 0; i < g_legacy_count; i++) if (g_legacy_buffers[i].buffer == b) { + os_unfair_lock_unlock(&g_legacy_lock); return; + } + uint64_t gpu; + if (g_legacy_direct_arguments) gpu = b.gpuAddress; + else { + // CPU-only address arithmetic. These tokens never reach GPU memory; + // the argument encoder resolves them to a real buffer and offset. + uint64_t span = ((uint64_t)b.length + 65535) & ~UINT64_C(65535); + if (!span || span > UINT64_MAX - g_legacy_next_buffer) { + os_unfair_lock_unlock(&g_legacy_lock); MDIE("[RED] Buffer token range exhausted"); + } + gpu = g_legacy_next_buffer; g_legacy_next_buffer += span; + } + size_t at = 0; + while (at < g_legacy_count && g_legacy_buffers[at].gpu < gpu) at++; + if (g_legacy_count == g_legacy_capacity) { + size_t cap = g_legacy_capacity ? g_legacy_capacity * 2 : 256; + legacy_buffer_entry *next = realloc(g_legacy_buffers, cap * sizeof *next); + if (!next) { os_unfair_lock_unlock(&g_legacy_lock); MDIE("【红】Buffer index allocation failed"); } + g_legacy_buffers = next; g_legacy_capacity = cap; + } + memmove(g_legacy_buffers + at + 1, g_legacy_buffers + at, + (g_legacy_count - at) * sizeof *g_legacy_buffers); + g_legacy_buffers[at] = (legacy_buffer_entry){gpu, b.length, b}; + g_legacy_count++; + os_unfair_lock_unlock(&g_legacy_lock); +} +static uint64_t legacy_buffer_gpu(id b) { + if (!b) return 0; + if (g_legacy_direct_arguments) return b.gpuAddress; + legacy_buffer_add(b); + os_unfair_lock_lock(&g_legacy_lock); + uint64_t gpu = 0; + for (size_t i = 0; i < g_legacy_count; i++) if (g_legacy_buffers[i].buffer == b) { gpu = g_legacy_buffers[i].gpu; break; } + os_unfair_lock_unlock(&g_legacy_lock); + return gpu; +} +static uint64_t legacy_texture_rid(id t) { + return !t ? 0 : g_legacy_direct_arguments ? t.gpuResourceID._impl : (uint64_t)(uintptr_t)(__bridge void *)t; +} +static uint64_t legacy_sampler_rid(id s) { + return !s ? 0 : g_legacy_direct_arguments ? s.gpuResourceID._impl : (uint64_t)(uintptr_t)(__bridge void *)s; +} +static void legacy_buffer_remove(id b) { + os_unfair_lock_lock(&g_legacy_lock); + for (size_t i = 0; i < g_legacy_count; i++) if (g_legacy_buffers[i].buffer == b) { + memmove(g_legacy_buffers + i, g_legacy_buffers + i + 1, + (g_legacy_count - i - 1) * sizeof *g_legacy_buffers); + g_legacy_count--; break; + } + os_unfair_lock_unlock(&g_legacy_lock); +} +static id legacy_buffer_at(uint64_t gpu, NSUInteger *offset) { + *offset = 0; + if (!gpu) return nil; + os_unfair_lock_lock(&g_legacy_lock); + size_t lo = 0, hi = g_legacy_count; + while (lo < hi) { size_t m = lo + (hi - lo) / 2; + if (g_legacy_buffers[m].gpu <= gpu) lo = m + 1; else hi = m; + } + id b = nil; + if (lo && gpu - g_legacy_buffers[lo - 1].gpu < g_legacy_buffers[lo - 1].length) { + b = g_legacy_buffers[lo - 1].buffer; + *offset = (NSUInteger)(gpu - g_legacy_buffers[lo - 1].gpu); + } + os_unfair_lock_unlock(&g_legacy_lock); + return b; +} diff --git a/dm/gfx/nvn/nvn_min_reduction.h b/dm/gfx/nvn/nvn_min_reduction.h new file mode 100644 index 0000000000..2f0c911b42 --- /dev/null +++ b/dm/gfx/nvn/nvn_min_reduction.h @@ -0,0 +1,125 @@ +// Metal 3 has no sampler reduction mode. These two source-hash-checked legacy +// companions implement the observed integer explicit-LOD, linear MIN sample. +// Keep the ordinary function alive: selection depends on each actual binding, +// and the live opt-in switch permits an otherwise identical A/B comparison. +#pragma once + +static const char *min_reduction_original(NvShader *sh) { + if (!sh || strcmp(DMSH_CHAIN, "43806fc88b5e")) return NULL; + if (sh->stage == 1 && !strcmp(sh->key, "151607c7a92386cc_b")) + return "7c6e776f1cccd8ecf7310e5083664697e35de1ab85a9f21466cd307426de297f"; + if (sh->stage == 5 && !strcmp(sh->key, "ec5ebd6d9902a9a1_b")) + return "bcd3d2c291a896ee1a22a828b7a461cc6501a33e835465df24d5c3cb71610435"; + return NULL; +} +static int min_reduction_candidate(NvShader *sh) { return min_reduction_original(sh) != NULL; } +static int min_reduction_sampler(const samp_par *p) { + return p->reduction == 1 && p->minf == 3 && p->magf == 1 && + p->ws == 7 && p->wt == 7 && p->wr == 7 && !p->cmp_mode && + p->lod_min == 0.0f && p->lod_max == 1000.0f && p->lod_bias == 0.0f && p->aniso == 1.0f; +} +typedef struct { int textures, samplers, texture_ok, sampler_ok, tid, sid; } min_reduction_binding; +static void min_reduction_bind_sampler(min_reduction_binding *v, const nvn_rbind *b, int sid, const samp_par *p, int real) { + if (b->reg != 17 || b->kind != NVN_RB_SAMPT) return; + v->samplers++; v->sid = sid; v->sampler_ok = real && p && min_reduction_sampler(p); +} +static void min_reduction_bind_texture(min_reduction_binding *v, const nvn_rbind *b, int tid, id t) { + if (b->reg != 17 || b->kind != NVN_RB_TEX) return; + v->textures++; v->tid = tid; + v->texture_ok = b->dtt == MTLTextureType2D && b->dpf == NVN_DPF_FLOAT && t && t.textureType == MTLTextureType2D && t.sampleCount == 1 && + (t.pixelFormat == MTLPixelFormatDepth16Unorm || t.pixelFormat == MTLPixelFormatDepth32Float || t.pixelFormat == MTLPixelFormatDepth32Float_Stencil8); +} +static void min_reduction_binding_finish(NvShader *sh, const min_reduction_binding *v) { + // This decision belongs to the encoded argument block, including reuse of + // that block. Later descriptor registration must not change its function. + sh->min_reduction_bound = v->textures == 1 && v->samplers == 1 && v->texture_ok && v->sampler_ok; + sh->min_reduction_sid = v->sid; sh->min_reduction_tid = v->tid; +} +static BOOL min_reduction_record(NSDictionary *r, NvShader *sh, NSData *source, NSData *library, NSString *src) { + if (![r isKindOfClass:NSDictionary.class]) return NO; + NSDictionary *s = r[@"sampler"]; + if (![s isKindOfClass:NSDictionary.class]) return NO; + const char *original = min_reduction_original(sh); + if (!original || ![r[@"schema"] isEqual:@1] || ![r[@"chain"] isEqual:@DMSH_CHAIN] || + ![r[@"kind"] isEqual:@"linear-min-reduction-v1"] || ![r[@"key"] isEqual:@(sh->key)] || + ![r[@"stage"] isEqual:sh->stage == 1 ? @"ps" : @"cs"] || + ![r[@"entry"] isEqual:sh->fn.name] || ![r[@"reg"] isEqual:@17] || ![r[@"calls"] isEqual:@1] || + ![r[@"integer_explicit_lod"] isEqual:@YES] || ![r[@"texture_dimension"] isEqual:@"2d"] || + ![r[@"original_source_sha256"] isEqual:@(original)] || + ![r[@"source_sha256"] isEqual:nonpoint_digest(source)] || + ![r[@"library_sha256"] isEqual:nonpoint_digest(library)] || + ![s[@"reduction"] isEqual:@1] || ![s[@"minf"] isEqual:@3] || ![s[@"magf"] isEqual:@1] || + ![s[@"ws"] isEqual:@7] || ![s[@"wt"] isEqual:@7] || ![s[@"wr"] isEqual:@7] || + ![s[@"cmp_mode"] isEqual:@0] || ![s[@"lod_min"] isEqual:@0] || ![s[@"lod_max"] isEqual:@1000] || + ![s[@"lod_bias"] isEqual:@0] || ![s[@"aniso"] isEqual:@1] || ![r[@"helper_file"] isEqual:@"embedded"]) return NO; + // The exporter hashes the exact UTF-8 helper body, excluding both markers. + NSString *begin = @"// DM_MIN_REDUCTION_HELPER_BEGIN\n", *end = @"// DM_MIN_REDUCTION_HELPER_END\n"; + NSArray *parts = [src componentsSeparatedByString:begin]; + if (parts.count != 2) return NO; + NSArray *tail = [parts[1] componentsSeparatedByString:end]; + if (tail.count != 2) return NO; + return [r[@"helper_sha256"] isEqual:nonpoint_digest([tail[0] dataUsingEncoding:NSUTF8StringEncoding])]; +} +static id min_reduction_compile(NvShader *sh) { + @synchronized (sh) { + if (!sh->min_reduction_tried) { + sh->min_reduction_tried = 1; + NSString *root = [g_msl_dir stringByAppendingPathComponent:@"min-reduction-v1"], *key = @(sh->key); + NSData *meta = [NSData dataWithContentsOfFile:[root stringByAppendingPathComponent:[key stringByAppendingString:@".json"]]]; + NSDictionary *record = meta ? [NSJSONSerialization JSONObjectWithData:meta options:0 error:nil] : nil; + NSData *source = [NSData dataWithContentsOfFile:[root stringByAppendingPathComponent:[key stringByAppendingString:@".metal"]]]; + NSData *library = [NSData dataWithContentsOfFile:[g_msl_dir stringByAppendingPathComponent:[key stringByAppendingString:@".metallib"]] options:NSDataReadingMappedIfSafe error:nil]; + NSString *src = source ? [[NSString alloc] initWithData:source encoding:NSUTF8StringEncoding] : nil; + NSError *e = nil; + if (!src || !min_reduction_record(record, sh, source, library, src)) { + e = [NSError errorWithDomain:@"NVNMinReduction" code:1 userInfo:@{NSLocalizedDescriptionKey:@"Missing or mismatched min-reduction-v1 companion source/manifest"}]; + } else { + MTLCompileOptions *options = [MTLCompileOptions new]; options.languageVersion = MTLLanguageVersion3_0; + options.preserveInvariance = YES; options.fastMathEnabled = NO; + CFAbsoluteTime t0 = CFAbsoluteTimeGetCurrent(); + sh->min_reduction_lib = [g_dev newLibraryWithSource:src options:options error:&e]; + if (sh->min_reduction_lib) { + sh->min_reduction_fn = sh->specialized ? + [sh->min_reduction_lib newFunctionWithName:sh->fn.name constantValues:[MTLFunctionConstantValues new] error:&e] : + [sh->min_reduction_lib newFunctionWithName:sh->fn.name]; + MTLFunctionType want = sh->stage == 1 ? MTLFunctionTypeFragment : MTLFunctionTypeKernel; + if (!sh->min_reduction_fn || sh->min_reduction_fn.functionType != want || + (sh->rf.nb && [sh->min_reduction_fn newArgumentEncoderWithBufferIndex:sh->rf.abi].encodedLength != sh->arg_len)) { + sh->min_reduction_fn = nil; + e = [NSError errorWithDomain:@"NVNMinReduction" code:2 userInfo:@{NSLocalizedDescriptionKey:@"Min-reduction entry point type or argument layout changed"}]; + } + } + if (sh->min_reduction_fn) { + sh->min_reduction_fn.label = [NSString stringWithFormat:@"%s:min-reduction-v1:%@", sh->key, record[@"source_sha256"]]; + MLOG("Min-reduction variant %s stage=%d entry=%s source=%s compiled in %.1fms; original function retained", sh->key, sh->stage, + sh->fn.name.UTF8String, [record[@"source_sha256"] UTF8String], (CFAbsoluteTimeGetCurrent() - t0) * 1000); + } + } + if (!sh->min_reduction_fn) MLOG("[RED] Min-reduction variant %s failed: %s (ordinary function retained)", sh->key, e.localizedDescription.UTF8String); + } + return sh->min_reduction_fn; + } +} +static void min_reduction_note(NvShader *sh, int state, int sid, int tid) { + if (state != sh->min_reduction_last) { + sh->min_reduction_last = state; + if (sh->min_reduction_notes++ < 16) MLOG("Min-reduction selection frame=%llu key=%s stage=%d state=%s sid=%d tid=%d", + (unsigned long long)atomic_load(&g_cur_frame), sh->key, sh->stage, + state == 1 ? "explicit-lod-min" : state == 2 ? "binding-mismatch" : state == 3 ? "companion-failed" : state == 4 ? "pipeline-failed" : "ordinary", sid, tid); + } +} +static id min_reduction_function(NvShader *sh) { + if (!min_reduction_candidate(sh)) return nil; + if (!gfx_diagnostic_bool(&g_min_reduction, "DM_NVN_MIN_REDUCTION") || + [MTLSamplerDescriptor instancesRespondToSelector:NSSelectorFromString(@"setReductionMode:")]) { + min_reduction_note(sh, -1, 0, 0); return nil; + } + // encode_args captured this contract while binding the actual sampler and + // final texture view under g_rmu. Never re-read the mutable registry here. + int sid = sh->min_reduction_sid, tid = sh->min_reduction_tid; + if (sh->min_reduction_pso_failed) { min_reduction_note(sh, 4, sid, tid); return nil; } + if (!sh->min_reduction_bound) { min_reduction_note(sh, 2, sid, tid); return nil; } + id fn = min_reduction_compile(sh); + min_reduction_note(sh, fn ? 1 : 3, sid, tid); + return fn; +} diff --git a/dm/gfx/nvn/nvn_mtl.m b/dm/gfx/nvn/nvn_mtl.m index 9ec0cde7d6..92806f1411 100644 --- a/dm/gfx/nvn/nvn_mtl.m +++ b/dm/gfx/nvn/nvn_mtl.m @@ -1,3 +1,132 @@ +// Keep the current upstream Metal 4 renderer intact on macOS while older +// iOS/tvOS devices use the compatibility renderer maintained by this fork. +// The split prevents an upstream renderer rebase from silently deleting either +// path and makes the extra maintenance surface explicit. +#include +#if TARGET_OS_IPHONE || defined(DM_NVN_LEGACY_RENDERER_TEST) +#include "nvn_mtl_legacy.inc" + +// The host-facing NVN layer follows current upstream and therefore expects a +// few entry points added with the Metal 4 renderer. Keep the legacy renderer +// honest at that boundary: adapt operations it already supports, and report +// zero only for Metal-4-only diagnostics/optimisations which are not present +// on this path. +// Queue bindings are persistent NVN state. Keep one legacy S/R snapshot per +// queue just like the current renderer, so destroying a launcher's queue cannot +// leave its program bindings in the game queue (FFX/X-2), and alternating queues +// retain their own bindings (Three Houses). +typedef struct legacy_queue_state { + uint64_t serial; + typeof(S) s; + typeof(R) r; + struct legacy_queue_state *next; +} legacy_queue_state; +static legacy_queue_state *g_legacy_queue_states, *g_legacy_active_queue; + +void mtl_queue_activate(uint64_t serial) { + if (g_legacy_active_queue && g_legacy_active_queue->serial == serial) return; + if (g_legacy_active_queue) { + g_legacy_active_queue->s = S; + g_legacy_active_queue->r = R; + } + legacy_queue_state *q = g_legacy_queue_states; + while (q && q->serial != serial) q = q->next; + if (q) { + S = q->s; + R = q->r; + const prog_t *program = bind_stages(NULL, 0); + if (program != S.prog) S.prog = program; + } else { + q = calloc(1, sizeof *q); + if (!q) MDIE("queue state allocation failed"); + q->serial = serial; + q->next = g_legacy_queue_states; + g_legacy_queue_states = q; + state_reset(); + memset(&R, 0, sizeof R); + } + g_legacy_active_queue = q; + g_pso_dirty = g_dss_dirty = 1; + args_dirty_all(); +} + +void mtl_queue_forget(uint64_t serial) { + legacy_queue_state **p = &g_legacy_queue_states; + while (*p && (*p)->serial != serial) p = &(*p)->next; + if (!*p) return; + legacy_queue_state *q = *p; + *p = q->next; + if (g_legacy_active_queue == q) { + g_legacy_active_queue = NULL; + state_reset(); + memset(&R, 0, sizeof R); + args_dirty_all(); + } + free(q); +} + +void *mtl_device_classic(void) { return mtl_device(); } +int mtl_pso_prebuild_tool(void) { return 0; } +int mtl_wait_acq(uint64_t fence, uint64_t timeout_ns) { return mtl_wait(fence, timeout_ns); } + +unsigned mtl_cpu_tex_count(void) { + pthread_mutex_lock(&g_cpu_tex_mu); + unsigned count = g_cpu_tex ? (unsigned)CFSetGetCount(g_cpu_tex) : 0; + pthread_mutex_unlock(&g_cpu_tex_mu); + return count; +} + +uint64_t mtl_pkg_edge_skips(void) { return 0; } + +void mtl_reg_image(int tid, const tex_t *t, const view_t *v) { + if (!t) { mtl_reg_tex(tid, NULL, v); return; } + tex_t image = *t; + image.d.flags |= NVN_TEXTURE_FLAGS_IMAGE; + mtl_reg_tex(tid, &image, v); +} + +int mtl_link_rate(void) { return g_link_mode == 3 ? 30 : g_link_mode == 2 ? 120 : 60; } +int mtl_link_stalled(void) { return __atomic_load_n(&g_stall_t0, __ATOMIC_RELAXED) != 0; } + +int mtl_link_wait_ticks(int ticks, uint64_t timeout_ns) { + if (!mtl_link_active()) return 0; + if (ticks < 1) ticks = 1; + static uint64_t started_tick; + pthread_mutex_lock(&g_link_mu); + if (g_link_tick_n >= started_tick + (uint64_t)ticks) { + started_tick = g_link_tick_n; + pthread_mutex_unlock(&g_link_mu); + return 1; + } + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec += timeout_ns / 1000000000ull; + ts.tv_nsec += timeout_ns % 1000000000ull; + if (ts.tv_nsec >= 1000000000L) { ts.tv_sec++; ts.tv_nsec -= 1000000000L; } + while (g_link_tick_n < started_tick + (uint64_t)ticks) { + if (pthread_cond_timedwait(&g_link_cv, &g_link_mu, &ts) == ETIMEDOUT) { + pthread_mutex_unlock(&g_link_mu); + return 0; + } + } + started_tick = g_link_tick_n; + pthread_mutex_unlock(&g_link_mu); + return 1; +} + +int mtl_snap_on(void) { return 0; } +uint64_t mtl_snap_acquire_wait(void) { return 0; } +void mtl_sm_stats(uint64_t *passes, uint64_t *continued, uint64_t *waited) { *passes = *continued = *waited = 0; } +void mtl_cshoist_stats(uint64_t *moved, uint64_t *depth, uint64_t *stops) { *moved = *depth = 0; memset(stops, 0, 8 * sizeof *stops); } +void mtl_hybrid_stats(uint64_t *draws, uint64_t *made, uint64_t *none) { *draws = *made = *none = 0; } +void mtl_pso_line(char *out, size_t size) { if (size) snprintf(out, size, "legacy renderer (Metal 4 pipeline archive unavailable)"); } +void mtl_ab_gpu(uint64_t frame, double gpu_ms, double frame_ms) { (void)frame; (void)gpu_ms; (void)frame_ms; } +int mtl_link_tick(uint64_t n, double *target, double *lead_ms, int *ticks, double *gap_ms) { + (void)n; (void)target; (void)lead_ms; (void)ticks; (void)gap_ms; + return 0; // The legacy display-link path does not retain per-present tick diagnostics. +} +#else + // Metal execution layer of the NVN null device, see nvn_mtl.h. #import #import @@ -11000,3 +11129,8 @@ void mtl_wait_state_dump(void) { } void mtl_alloc_stats(uint64_t *waits, uint64_t *grown, int *n) { *waits = g_alloc_waits; *grown = g_alloc_grown; *n = g_alloc_n; } // Main submit allocator pool: how many times it waited, how many times it grew, how many exist now #pragma clang attribute pop + +// The compatibility renderer understands these live diagnostics; the current +// Metal 4 path has no corresponding mutable state. +void dm_gfx_env_changed(const char *key, const char *value) { (void)key; (void)value; } +#endif diff --git a/dm/gfx/nvn/nvn_mtl_legacy.inc b/dm/gfx/nvn/nvn_mtl_legacy.inc new file mode 100644 index 0000000000..787d4b2b7f --- /dev/null +++ b/dm/gfx/nvn/nvn_mtl_legacy.inc @@ -0,0 +1,9812 @@ +// iOS 17 traditional Metal backend, ported from D2R compatibility mechanisms. +// Metal execution layer of the NVN null device, see nvn_mtl.h. +#import +#import +#import +#import +#import +#import +#include +#include +#include +#include +#include +#include +#if TARGET_OS_OSX || __IPHONE_OS_VERSION_MIN_REQUIRED >= 170000 +#define DM_HAVE_METAL_DISPLAY_LINK 1 +#else +#define DM_HAVE_METAL_DISPLAY_LINK 0 +#endif +#if TARGET_OS_IPHONE +#include // os_proc_available_memory: for the memory readout line +#endif +#include "nvn_mtl.h" +#include "nvn_prepare.h" +#include "nvn_obj.h" +#include "../tex/nvn_fmt_mtl.h" +#include "../tex/nvn_deswizzle.h" +#include "../tex/nvn_deswizzle_gpu.h" +#include "../tex/nvn_bcdec.h" +#include "../tex/nvn_bc2astc.h" +#include "../tex/bc2astc_msl.h" // nvn_bca_msl: BC1/2/3 -> ASTC 4x4 GPU transcoder +#include "nvn_builtin.h" +#include "nvn_dirty.h" +#include "nvn_refl.h" +#include "nvn_cover.h" +#include "dmsh_chain.h" // DMSH_CHAIN: the translation chain version of the B shader library expected by this source tree (in the game profile folder, -I from the Makefile) +#include "game_profile.h" // game profile constants (NVN driver constant segment bases, profile id) +// Texture slots 0-127 hold combined NVN handles. Slots 128-255 hold the +// separate texture bindings produced by nvnCommandBufferBindSeparateTexture. +#define NTEX 256 +// Unified NVN driver constant layout used by the current shader translator. +// c2: combined textures, images, separate textures, then separate samplers. +// c0: storage-buffer descriptors at a stage-specific base. +enum { C2_TEX = 0x20, C2_TEX_SLOTS = 41, C2_IMG = 0x120, C2_IMG_SLOTS = 8, + C2_SEPTEX = 0x168, C2_SAMP = 0x568, DRV_SIZE = 0xA00 }; +_Static_assert(C2_TEX + 8 * C2_TEX_SLOTS == C2_SEPTEX && + C2_SEPTEX + 8 * 128 == C2_SAMP && + C2_SAMP + 8 * 128 <= DRV_SIZE && + NVN_SEPTEX_SLOT0 + 128 == NTEX, + "NVN driver handle tables"); +static int c0_ssbo_base(int stage) { + return stage == 0 ? 0x110 : stage == 1 ? 0x510 : stage == 5 ? 0x310 : -1; +} +#include "nvn_hmap.h" +#include "../../host/dm.h" +#import +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// 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) + +#define MLOG(...) do { fprintf(stderr, "[dm] [nvn-mtl] " __VA_ARGS__); fputc('\n', stderr); } while (0) +#define MDIE(...) do { MLOG("FATAL " __VA_ARGS__); abort(); } while (0) + +static void warm_start(void); +static void warm_claim(void); +static _Atomic int g_prepare_state, g_prepare_frontend; +static _Atomic uint32_t g_prepare_total, g_prepare_done, g_prepare_failed, g_prepare_skipped; +static pthread_mutex_t g_prepare_mu = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t g_prepare_cv = PTHREAD_COND_INITIALIZER; +static int g_prepare_active = 1; +void mtl_prepare_active(int active) { + pthread_mutex_lock(&g_prepare_mu); g_prepare_active = active; + pthread_cond_broadcast(&g_prepare_cv); pthread_mutex_unlock(&g_prepare_mu); +} +static void prepare_foreground_wait(void) { + if (!atomic_load(&g_prepare_frontend)) return; + pthread_mutex_lock(&g_prepare_mu); + while (!g_prepare_active) pthread_cond_wait(&g_prepare_cv, &g_prepare_mu); + pthread_mutex_unlock(&g_prepare_mu); +} +void mtl_prepare_snapshot(nvn_prepare_status *out) { + // State publication pairs with READY/PARTIAL after all PSO tables are claimed. + out->state = atomic_load(&g_prepare_state); + out->total = atomic_load(&g_prepare_total); out->completed = atomic_load(&g_prepare_done); + out->failed = atomic_load(&g_prepare_failed); out->skipped = atomic_load(&g_prepare_skipped); + nvn_cache_snapshot(&out->cache); +} + +static id g_dev; +static int g_layered_native = 1, g_bare_native = 1, g_fragment_image_native = 1, g_slice_replaying; +static uint32_t g_selected_slice; +static NSMutableArray *g_l3_gpu_leaks; +static int g_nvn_deswz_gpu; +static id g_q; + + +static _Atomic uint32_t g_present_qos; +#define ALLOC_MAX 8 +static int g_alloc_n, g_alloc_cur; +static int g_vbuf_cache = 1, g_buffer_offset_cache = 1; +static uint64_t g_vbuf_resolve, g_vbuf_reuse, g_buffer_offset_calls; +static id g_present_uv[3]; +static NSString *g_shmap_path; +static void pso_pend_harvest(void); +int g_bloom_q = -1; // Bloom 1/4 starting point (non-static: real-device same-process alternation sets it to 0 / 1 directly) + +static id g_bloomq_const[3]; +static id g_bloomq_samp; +static id g_bloomq_pso; +static MTLPixelFormat g_bloomq_fmt; +static id g_bloomq_view; +static int g_bloomq_i; +static struct { int state; uint64_t s_ser, b0_ser, b1_ser, s_h, samp_h, b1_h; const tex_t *s_tex; float threshold, coeff_g, coeff_b; } g_bloomq; +static uint64_t g_bloomq_last_b0, g_bloomq_last_b1h, g_bloomq_last_b1s; // Most recent bloom 1/4 overlay read redirection: B0 serial → B1 handle (kept across frames, see the 2e2d6700a2569795 branch) +static uint64_t g_present_alloc_fence[3]; +static pthread_mutex_t g_pq_mu = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t g_pq_cv = PTHREAD_COND_INITIALIZER; +static pthread_cond_t g_link_cv = PTHREAD_COND_INITIALIZER; +static pthread_mutex_t g_link_mu = PTHREAD_MUTEX_INITIALIZER; +static uint64_t g_link_tick_ns, g_link_tick_n; +static uint64_t g_link_dropped; +static double g_link_hold_until; static uint64_t g_link_held; // Changed only on the display-link callback thread: the time the previous frame must stay on screen until by its present interval (0 = no hold), and the ticks that took no new frame because of it +static int g_pq_inflight; // Under g_pq_mu: number of presents committed to the GPU whose completion callback has not arrived yet +// Under g_pq_mu: claim the layer before attaching a display link, and keep each +// classic frame protected from dequeue until its scheduled present call returns. +static int g_link_owns_layer, g_classic_pending; +int g_dm_present_link; // Present pacing mode (runtime value of DM_PRESENT_LINK, modified directly during same-process alternating measurement): 0 = acquire the drawable itself; 1 = display link at 60 Hz; 2 = display link at 120 Hz, the game presents a frame every other display-link tick +static int g_present_latency = 1; +static uint64_t g_pq_done; +static uint64_t g_pq_read; + +// On-device render diagnostics. These stay behind environment switches and +// publish the complete Objective-C object set once before parallel pipeline +// builders read it. +static dispatch_once_t g_hilite_once; +static id g_hilite_fs[6], g_hilite_vs; +static char g_hilite_keys[6][24]; +static int g_hilite_n, g_hilite_full; +static NSMutableDictionary> *g_diag_c0_cache; + +static int g_res_dirty; +static id g_ev; +static _Atomic uint64_t g_fence; // Highest committed fence value (incremented exclusively by the submit thread, read by guest registration threads) +static _Atomic uint64_t g_sub_serial; // Submission index (also sampled by deferred-release diagnostics on guest registration threads) +static _Atomic uint64_t g_sub_pending_fence; // Fence owned by the active replay, published before g_sub_active +static _Atomic int g_sub_active; // A main replay exists and may have encoded references not covered by g_done yet +static _Atomic uint64_t g_cut_committed_diag; // Host segments committed so far; diagnostic only, never guest-visible +static pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t g_cv = PTHREAD_COND_INITIALIZER; +static _Atomic uint64_t g_done; // Highest completed fence value (written by the completion callback) +static uint64_t g_arena_made; +static int g_allocprof = -1; static uint64_t g_ap_bytes[64], g_ap_fn[8]; +#define AP_B uint64_t _ap0 = g_allocprof > 0 ? g_dev.currentAllocatedSize : 0 +#define AP_E(i) do { if (g_allocprof > 0) { uint64_t _n = g_dev.currentAllocatedSize; if (_n > _ap0) g_ap_fn[i] += _n - _ap0; } } while (0) // Per-frame arenas created so far (4 MiB each; for the memory report) +static _Atomic int g_last_commit_error; +static _Atomic int g_up_pending; + +static pthread_mutex_t g_res_mu = PTHREAD_MUTEX_INITIALIZER; // Dedicated lock for the residency set: res_add is called on paths that hold g_mu (arena_new), so it cannot share g_mu +static NSHashTable *g_res_objects; +#include "nvn_legacy_buffers.h" +#include "nvn_argument_encoding.h" +// Residency churn by call site (the power line every 300 frames, mtl_res_stats): each commit with changes is a kernel call whose cost grows with what was added or removed +enum { RS_SITES = 16 }; static struct { int line; uint64_t n, bytes; } g_rs_site[RS_SITES]; static uint64_t g_rs_rm, g_rs_commits, g_rs_commit_ns; +#define res_add(r) res_add_at((r), __LINE__) +// These paths also run on guest threads whose implicit autorelease pools do not +// drain. NSHashTable membership calls autorelease their arguments, so bound +// that lifetime here instead of retaining finalized Metal resources forever. +static void res_add_at(id r, int line) { @autoreleasepool { + if (!r) return; + pthread_mutex_lock(&g_res_mu); + if ([r conformsToProtocol:@protocol(MTLBuffer)]) legacy_buffer_add((id)r); + if (!g_res_objects) g_res_objects = [NSHashTable weakObjectsHashTable]; + if ([g_res_objects containsObject:r]) { pthread_mutex_unlock(&g_res_mu); return; } // already resident (a shared texture resolved again): marking the set dirty would cost a commit that changes nothing + { int k = 0; while (k < RS_SITES - 1 && g_rs_site[k].line && g_rs_site[k].line != line) k++; g_rs_site[k].line = line; g_rs_site[k].n++; g_rs_site[k].bytes += r.allocatedSize; } + [g_res_objects addObject:r]; + g_res_dirty = 1; + pthread_mutex_unlock(&g_res_mu); +} } +static void res_remove(id r) { @autoreleasepool { + if (!r) return; + pthread_mutex_lock(&g_res_mu); + if ([r conformsToProtocol:@protocol(MTLBuffer)]) legacy_buffer_remove((id)r); + if (![g_res_objects containsObject:r]) { pthread_mutex_unlock(&g_res_mu); return; } + [g_res_objects removeObject:r]; + g_res_dirty = 1; g_rs_rm++; + pthread_mutex_unlock(&g_res_mu); +} } +// DM_RES_CHECK=1 (diagnostic): if textures / buffers the shader accesses through the argument block are not in the residency set, print the first 40 (textures are attributed to the actual allocation via parent texture / underlying buffer) +static int g_res_check_on; +static void res_check(id r, const char *what, int reg) { + static int nbad; + if (!g_res_check_on || !r || nbad >= 40) return; + int is_tex = [r conformsToProtocol:@protocol(MTLTexture)], is_buf = [r conformsToProtocol:@protocol(MTLBuffer)]; + if (!is_tex && !is_buf) return; // Samplers etc. are not allocations + id a = r; int hops = 0; + if (is_tex) { id t = (id)a; while (t.parentTexture) { t = t.parentTexture; hops++; } a = t.buffer ? (id)t.buffer : (id)t; } + pthread_mutex_lock(&g_res_mu); int ok = [g_res_objects containsObject:a]; pthread_mutex_unlock(&g_res_mu); + if (!ok) { nbad++; id t = is_tex ? (id)r : nil; + MLOG("【红】驻留缺失 #%d:%s 槽 %d %s %p(父链 %d 层)分配 %lu B 存储 %lu%s", nbad, what, reg, is_tex ? "贴图" : "缓冲", (__bridge void *)a, hops, (unsigned long)a.allocatedSize, (unsigned long)a.storageMode, + t ? [NSString stringWithFormat:@" 格式 %lu %lux%lu 类型 %lu 用途 %#lx", (unsigned long)t.pixelFormat, (unsigned long)t.width, (unsigned long)t.height, (unsigned long)t.textureType, (unsigned long)t.usage].UTF8String : ""); } +} +static void res_commit(void) { + pthread_mutex_lock(&g_res_mu); + if (g_res_dirty) { g_res_dirty = 0; } + pthread_mutex_unlock(&g_res_mu); +} +// Since the last call: residency commits (count, time), additions by source line (count, MB; the four largest by bytes) and removals +void mtl_res_stats(char *out, size_t n) { + pthread_mutex_lock(&g_res_mu); + uint64_t tn = 0, tb = 0; for (int k = 0; k < RS_SITES; k++) { tn += g_rs_site[k].n; tb += g_rs_site[k].bytes; } + size_t o = snprintf(out, n, "提交 %llu 次 / %.1f ms,加 %llu 个 %.0f MB(", (unsigned long long)g_rs_commits, g_rs_commit_ns / 1e6, (unsigned long long)tn, tb / 1048576.0); + for (int i = 0; i < 4 && o < n; i++) { int best = -1; for (int k = 0; k < RS_SITES; k++) if (g_rs_site[k].n && (best < 0 || g_rs_site[k].bytes > g_rs_site[best].bytes)) best = k; + if (best < 0) break; o += snprintf(out + o, n - o, "%s行 %d:%llu 个 %.0f MB", i ? "," : "", g_rs_site[best].line, (unsigned long long)g_rs_site[best].n, g_rs_site[best].bytes / 1048576.0); g_rs_site[best].n = 0; } + if (o < n) snprintf(out + o, n - o, "),删 %llu 个", (unsigned long long)g_rs_rm); + memset(g_rs_site, 0, sizeof g_rs_site); g_rs_rm = g_rs_commits = g_rs_commit_ns = 0; + pthread_mutex_unlock(&g_res_mu); +} + +@interface NvDeferredRelease : NSObject { @public + id object; + uint64_t fence, sub_serial, direct_bytes, root_ref_bytes; + int site; + uint8_t keep_resident, cut_eligible, submission_active, replay_view_retirement; + uint8_t kind, is_view; // kind: 0 other, 1 buffer, 2 texture, 3 other MTLResource +} @end +@implementation NvDeferredRelease @end +static NSMutableArray *g_deferred; +static pthread_mutex_t g_deferred_mu = PTHREAD_MUTEX_INITIALIZER; +// Replay-only derived-view records may become releasable while their caller still holds a +// registry lock. Enqueue only requests a drain; public registry entry points +// perform it after dropping their lock so object destruction cannot invert the +// registry/residency lock order. +static _Atomic int g_defer_replay_view_drain_needed; +// g_sub_active and g_sub_pending_fence are atomic for diagnostics, but every +// state transition and every retirement decision is serialized by +// g_deferred_mu. An acquire load alone may legally observe an older false while +// begin races a guest registration thread; the mutex gives that race one exact +// order instead. +static void defer_submission_begin_state(void) { + pthread_mutex_lock(&g_deferred_mu); + uint64_t pending = atomic_load_explicit(&g_fence, memory_order_relaxed) + 1; + atomic_store_explicit(&g_sub_pending_fence, pending, memory_order_relaxed); + atomic_store_explicit(&g_sub_active, 1, memory_order_relaxed); + pthread_mutex_unlock(&g_deferred_mu); +} +static uint64_t defer_submission_prepare_commit(void) { + pthread_mutex_lock(&g_deferred_mu); + uint64_t fence = atomic_load_explicit(&g_fence, memory_order_relaxed) + 1; + uint64_t pending = atomic_load_explicit(&g_sub_pending_fence, memory_order_relaxed); + int good = atomic_load_explicit(&g_sub_active, memory_order_relaxed) && fence == pending; + if (good) atomic_store_explicit(&g_fence, fence, memory_order_release); + pthread_mutex_unlock(&g_deferred_mu); + if (!good) MDIE("submission fence publication mismatch: committed %llu pending %llu active=%d", + (unsigned long long)fence, (unsigned long long)pending, + atomic_load_explicit(&g_sub_active, memory_order_relaxed)); + return fence; +} +static void defer_submission_did_commit(uint64_t fence) { + pthread_mutex_lock(&g_deferred_mu); + uint64_t published = atomic_load_explicit(&g_fence, memory_order_relaxed); + uint64_t pending = atomic_load_explicit(&g_sub_pending_fence, memory_order_relaxed); + int good = atomic_load_explicit(&g_sub_active, memory_order_relaxed) && published == fence && pending == fence; + if (good) atomic_store_explicit(&g_sub_active, 0, memory_order_relaxed); + pthread_mutex_unlock(&g_deferred_mu); + if (!good) MDIE("submission close mismatch: committed %llu published %llu pending %llu", + (unsigned long long)fence, (unsigned long long)published, (unsigned long long)pending); +} +enum { DEFER_DIAG_SITES = 48 }; +typedef struct { + int site; + uint64_t count, direct_bytes, root_ref_bytes, cut, keep, active, replay_view, views; + uint64_t buffers, textures, resources, other, first_sub, last_sub; +} defer_diag_site; +typedef struct { + uint64_t count, direct_bytes, root_ref_bytes, cut, keep, active, replay_view, views; + uint64_t buffers, textures, resources, other, first_sub, last_sub, first_fence, last_fence; + defer_diag_site sites[DEFER_DIAG_SITES]; + int nsites; +} defer_diag_summary; +static _Atomic unsigned g_defer_diag_thresholds; +static int defer_diag_on(void) { + static dispatch_once_t once; static int on; + dispatch_once(&once, ^{ on = getenv("DM_NVN_DEFER_DIAG") != NULL; }); + return on; +} +static void defer_diag_add(defer_diag_summary *s, NvDeferredRelease *d) { + s->count++; s->direct_bytes += d->direct_bytes; s->root_ref_bytes += d->root_ref_bytes; + if (s->count == 1) { + s->first_sub = s->last_sub = d->sub_serial; + s->first_fence = s->last_fence = d->fence; + } else { + if (d->sub_serial < s->first_sub) s->first_sub = d->sub_serial; + if (d->sub_serial > s->last_sub) s->last_sub = d->sub_serial; + if (d->fence < s->first_fence) s->first_fence = d->fence; + if (d->fence > s->last_fence) s->last_fence = d->fence; + } + s->cut += d->cut_eligible; s->keep += d->keep_resident; s->active += d->submission_active; + s->replay_view += d->replay_view_retirement; s->views += d->is_view; + s->buffers += d->kind == 1; s->textures += d->kind == 2; s->resources += d->kind == 3; s->other += d->kind == 0; + int k = 0; while (k < s->nsites && s->sites[k].site != d->site) k++; + if (k == s->nsites) { + if (k >= DEFER_DIAG_SITES) return; + s->nsites++; s->sites[k].site = d->site; s->sites[k].first_sub = d->sub_serial; + } + defer_diag_site *p = &s->sites[k]; + p->count++; p->direct_bytes += d->direct_bytes; p->root_ref_bytes += d->root_ref_bytes; + p->cut += d->cut_eligible; p->keep += d->keep_resident; p->active += d->submission_active; + p->replay_view += d->replay_view_retirement; p->views += d->is_view; + p->buffers += d->kind == 1; p->textures += d->kind == 2; p->resources += d->kind == 3; p->other += d->kind == 0; + if (d->sub_serial < p->first_sub) p->first_sub = d->sub_serial; + if (d->sub_serial > p->last_sub) p->last_sub = d->sub_serial; +} +static void defer_diag_collect_locked(defer_diag_summary *s) { + memset(s, 0, sizeof *s); + for (NvDeferredRelease *d in g_deferred) defer_diag_add(s, d); +} +static void defer_diag_snapshot(const char *why) { @autoreleasepool { + if (!defer_diag_on()) return; + defer_diag_summary s; pthread_mutex_lock(&g_deferred_mu); defer_diag_collect_locked(&s); pthread_mutex_unlock(&g_deferred_mu); + uint64_t current_sub = atomic_load_explicit(&g_sub_serial, memory_order_relaxed); + uint64_t pending_age = s.count && current_sub >= s.first_sub ? current_sub - s.first_sub : 0; + MLOG("deferred-release diagnostic (%s): queued=%llu direct=%lluMB root-reference-sum=%lluMB replay-eligible=%llu keep-resident=%llu active-submit=%llu replay-view-retirements=%llu views=%llu types=%lluB/%lluT/%lluR/%lluO submissions=%llu..%llu age=%llu fences=%llu..%llu done=%llu cuts=%llu", + why, (unsigned long long)s.count, (unsigned long long)(s.direct_bytes >> 20), (unsigned long long)(s.root_ref_bytes >> 20), + (unsigned long long)s.cut, (unsigned long long)s.keep, (unsigned long long)s.active, (unsigned long long)s.replay_view, + (unsigned long long)s.views, (unsigned long long)s.buffers, (unsigned long long)s.textures, + (unsigned long long)s.resources, (unsigned long long)s.other, + (unsigned long long)s.first_sub, (unsigned long long)s.last_sub, (unsigned long long)pending_age, + (unsigned long long)s.first_fence, (unsigned long long)s.last_fence, + (unsigned long long)atomic_load_explicit(&g_done, memory_order_relaxed), + (unsigned long long)atomic_load_explicit(&g_cut_committed_diag, memory_order_relaxed)); + uint8_t used[DEFER_DIAG_SITES] = {0}; + for (int rank = 0; rank < 6; rank++) { + int best = -1; for (int i = 0; i < s.nsites; i++) if (!used[i] && (best < 0 || s.sites[i].count > s.sites[best].count)) best = i; + if (best < 0 || !s.sites[best].count) break; used[best] = 1; defer_diag_site *p = &s.sites[best]; + uint64_t site_age = current_sub >= p->first_sub ? current_sub - p->first_sub : 0; + MLOG("deferred-release diagnostic top%d: line=%d count=%llu direct=%lluMB root-reference-sum=%lluMB replay-eligible=%llu keep=%llu active=%llu replay-view=%llu views=%llu types=%lluB/%lluT/%lluR/%lluO submissions=%llu..%llu age=%llu", + rank + 1, p->site, (unsigned long long)p->count, (unsigned long long)(p->direct_bytes >> 20), + (unsigned long long)(p->root_ref_bytes >> 20), (unsigned long long)p->cut, (unsigned long long)p->keep, + (unsigned long long)p->active, (unsigned long long)p->replay_view, (unsigned long long)p->views, + (unsigned long long)p->buffers, (unsigned long long)p->textures, (unsigned long long)p->resources, + (unsigned long long)p->other, (unsigned long long)p->first_sub, (unsigned long long)p->last_sub, + (unsigned long long)site_age); + } +} } +// Set only on the thread while it is replaying one NVN submission. A retired +// object queued there cannot be looked up by a later replay segment: the call +// that queues it has already removed / replaced the owning serial, view, pool +// or scratch slot. Retirements from other guest threads stay on the original +// whole-submission fence because they may race command recording / replay. +static _Thread_local int g_defer_cut_active; +// Deferred release has two meanings that must be kept apart (the root of the GPU memory fault on the first frame on entering Travincal): +// defer_obj_release = this object is retired: drop the reference after the submissions that used it complete, and remove it from the residency set; +// defer_obj_unref = only the reference I hold is no longer wanted, the object itself may still be in use (the view cache of a texture ID may hold the base texture itself): only drop the reference, leave the residency set alone. +// Previously only the former existed: when the view cache was invalidated, a texture that 'is still the current storage of some serial' was also removed from the residency set. Invalidation used to happen only when the base texture was really replaced, so it was never hit; +// after the transparency chain's in-place composite swapped the storage of two textures, each of the two live textures was removed once this way, and afterwards they are used as attachments and sampled every frame as usual - the GPU accesses non-resident memory. +static void defer_drain(void); +static void defer_obj_release_ex(id obj, int keep_resident, int replay_view_retirement, int site) { + if (!obj) return; + NvDeferredRelease *d = [NvDeferredRelease new]; d->object = obj; d->keep_resident = keep_resident; + d->cut_eligible = g_defer_cut_active; d->replay_view_retirement = replay_view_retirement; + d->site = site; d->sub_serial = atomic_load_explicit(&g_sub_serial, memory_order_relaxed); + if ([obj conformsToProtocol:@protocol(MTLBuffer)]) { + id b = obj; d->kind = 1; d->direct_bytes = d->root_ref_bytes = MAX((uint64_t)b.allocatedSize, (uint64_t)b.length); + } else if ([obj conformsToProtocol:@protocol(MTLTexture)]) { + id t = obj, root = t; d->kind = 2; d->is_view = t.parentTexture != nil || t.buffer != nil; + while (root.parentTexture) root = root.parentTexture; + id storage = root.buffer ? (id)root.buffer : (id)root; + d->root_ref_bytes = storage.allocatedSize; + if (!d->is_view) d->direct_bytes = d->root_ref_bytes; + } else if ([obj conformsToProtocol:@protocol(MTLResource)]) { + d->kind = 3; d->direct_bytes = d->root_ref_bytes = ((id)obj).allocatedSize; + } + NSUInteger queued; uint64_t fence; int active; + pthread_mutex_lock(&g_deferred_mu); + active = atomic_load_explicit(&g_sub_active, memory_order_relaxed); + fence = active ? atomic_load_explicit(&g_sub_pending_fence, memory_order_relaxed) : + atomic_load_explicit(&g_fence, memory_order_relaxed) + !replay_view_retirement; + d->fence = fence; d->submission_active = active; + if (!g_deferred) g_deferred = [NSMutableArray array]; + [g_deferred addObject:d]; + queued = g_deferred.count; + if (replay_view_retirement && fence <= atomic_load_explicit(&g_done, memory_order_acquire)) + atomic_store_explicit(&g_defer_replay_view_drain_needed, 1, memory_order_release); + pthread_mutex_unlock(&g_deferred_mu); + // Threshold diagnostics run only after releasing g_deferred_mu: snapshot + // takes that same lock while aggregating the queue. + if (defer_diag_on()) { + unsigned bit = queued >= 1024 ? 4u : queued >= 512 ? 2u : queued >= 128 ? 1u : 0u; + if (bit && !(atomic_fetch_or(&g_defer_diag_thresholds, bit) & bit)) defer_diag_snapshot("queue threshold"); + } + // Never drain here: descriptor registration callers hold g_rmu. Releasing + // a Metal object can enter residency bookkeeping, so defer that work to the + // explicit post-unlock safe point below. +} +#define defer_obj_release(obj) defer_obj_release_ex((obj), 0, 0, __LINE__) +#define defer_obj_unref(obj) defer_obj_release_ex((obj), 1, 0, __LINE__) +#define defer_replay_view_unref(obj) defer_obj_release_ex((obj), 1, 1, __LINE__) +static void defer_replay_view_drain_safe_point(void) { + if (atomic_exchange_explicit(&g_defer_replay_view_drain_needed, 0, memory_order_acq_rel)) defer_drain(); +} +static void defer_release_batch(NSArray *ready) { @autoreleasepool { + for (NvDeferredRelease *d in ready) { + id obj = d->object; + if (!d->keep_resident && [obj conformsToProtocol:@protocol(MTLResource)]) { + pthread_mutex_lock(&g_res_mu); + int registered = [g_res_objects containsObject:obj]; + pthread_mutex_unlock(&g_res_mu); + if (registered) res_remove((id)obj); + } + d->object = nil; + } +} } +// Drain retired objects on the guest submit thread as well as completion threads. +// Upstream 0fc0ce43b184: the guest thread has no draining implicit autorelease pool. +static void defer_drain(void) { @autoreleasepool { + NSMutableArray *ready = [NSMutableArray array]; + pthread_mutex_lock(&g_deferred_mu); + for (NSInteger i = g_deferred.count - 1; i >= 0; i--) { + NvDeferredRelease *d = g_deferred[(NSUInteger)i]; + if (d->fence <= g_done) { [ready addObject:d]; [g_deferred removeObjectAtIndex:(NSUInteger)i]; } + } + pthread_mutex_unlock(&g_deferred_mu); + defer_release_batch(ready); +} } + +// Take only objects retired by this replay thread before the current split. +// The returned array is captured by that Metal command buffer's completion +// handler. Guest-visible fence completion remains on the terminal segment. +static NSArray *defer_take_cut_batch(void) { @autoreleasepool { + NSMutableArray *ready = nil; + pthread_mutex_lock(&g_deferred_mu); + uint64_t fence = atomic_load_explicit(&g_sub_pending_fence, memory_order_relaxed); + for (NSInteger i = g_deferred.count - 1; i >= 0; i--) { + NvDeferredRelease *d = g_deferred[(NSUInteger)i]; + if (d->cut_eligible && d->fence == fence) { + if (!ready) ready = [NSMutableArray array]; + [ready addObject:d]; + [g_deferred removeObjectAtIndex:(NSUInteger)i]; + } + } + pthread_mutex_unlock(&g_deferred_mu); + return ready.count ? [ready copy] : nil; +} } + +static pthread_mutex_t g_bf_want_mu = PTHREAD_MUTEX_INITIALIZER; +static char **g_bf_want_keys; static size_t g_bf_want_n, g_bf_want_cap; static int g_bf_want_loaded; +void bf_want_note(const char *key) { + const char *path = getenv("DM_NVN_BF_WANT"); + if (!path || !key || strlen(key) != 16) return; + for (int i = 0; i < 16; i++) if (!((key[i] >= '0' && key[i] <= '9') || (key[i] >= 'a' && key[i] <= 'f') || (key[i] >= 'A' && key[i] <= 'F'))) return; + pthread_mutex_lock(&g_bf_want_mu); + if (!g_bf_want_loaded) { + FILE *f = fopen(path, "r"); char line[64]; + if (f) { while (fgets(line, sizeof line, f)) { char *e = strpbrk(line, "\r\n"); if (e) *e = 0; if (strlen(line) != 16) continue; + if (g_bf_want_n == g_bf_want_cap) { g_bf_want_cap = g_bf_want_cap ? g_bf_want_cap * 2 : 64; g_bf_want_keys = realloc(g_bf_want_keys, g_bf_want_cap * sizeof *g_bf_want_keys); } + g_bf_want_keys[g_bf_want_n++] = strdup(line); } fclose(f); } + g_bf_want_loaded = 1; + } + for (size_t i = 0; i < g_bf_want_n; i++) if (!strcmp(g_bf_want_keys[i], key)) { pthread_mutex_unlock(&g_bf_want_mu); return; } + if (g_bf_want_n == g_bf_want_cap) { g_bf_want_cap = g_bf_want_cap ? g_bf_want_cap * 2 : 64; g_bf_want_keys = realloc(g_bf_want_keys, g_bf_want_cap * sizeof *g_bf_want_keys); } + g_bf_want_keys[g_bf_want_n++] = strdup(key); + FILE *f = fopen(path, "a"); if (f) { fprintf(f, "%s\n", key); fclose(f); } + pthread_mutex_unlock(&g_bf_want_mu); +} + +// ---------- Memory pools ---------- +typedef struct { uintptr_t lo, hi; uintptr_t base; __unsafe_unretained id buf; uint64_t gpu; const void *key; int res; } pool_ent; // res: already in the residency set (on demand, see pool_res) +static pool_ent *g_pools; static int g_npool, g_cap; +static int g_pidx_ok, g_plast = -1; // whether the address→pool index is valid, the last hit (pool_find) +static NSMutableArray *g_keep; // Holds the buffer references (pool_ent has non-owning pointers) +static uint64_t g_pool_adds; +static int g_pool_res_n; // registered pools already in the residency set (for the memory readout; under g_mu) + +#define C1_BANK_SIZE (4u << 20) +#define C1_DECL_SIZE (64u << 10) +static NSMutableArray *g_c1_banks; +static size_t g_c1_bank_off, g_c1_alloc_bytes, g_c1_data_bytes; +static uint64_t g_c1_count, g_c1_exclusive; +static NSMutableSet *g_c1_full; +static int g_c1_full_valid; +static os_unfair_lock g_c1_lock = OS_UNFAIR_LOCK_INIT; + +// DM_UDC_SELFTEST=1 (diagnostic, checks DontCare behavior on a real A19 device): private-storage Depth32Float_Stencil8 / RGBA8 (1952x900) are first cleared to write known values, +// then Load + DontCare without drawing, read back and compared pixel by pixel, printing one result line +static void udc_selftest(void) { + if (!getenv("DM_UDC_SELFTEST")) return; + id q = [g_dev newCommandQueue]; + for (int depth = 1; depth >= 0; depth--) { + NSUInteger w = 1952, h = 900; + MTLTextureDescriptor *td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:depth ? MTLPixelFormatDepth32Float_Stencil8 : MTLPixelFormatRGBA8Unorm width:w height:h mipmapped:NO]; + td.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead; td.storageMode = MTLStorageModePrivate; + id t = [g_dev newTextureWithDescriptor:td]; + id cb = [q commandBuffer]; + for (int pass = 0; pass < 2; pass++) { + MTLRenderPassDescriptor *rp = [MTLRenderPassDescriptor renderPassDescriptor]; + MTLLoadAction la = pass ? MTLLoadActionLoad : MTLLoadActionClear; MTLStoreAction sa = pass ? MTLStoreActionDontCare : MTLStoreActionStore; + if (depth) { rp.depthAttachment.texture = t; rp.depthAttachment.loadAction = la; rp.depthAttachment.storeAction = sa; rp.depthAttachment.clearDepth = 0.3125; + rp.stencilAttachment.texture = t; rp.stencilAttachment.loadAction = la; rp.stencilAttachment.storeAction = sa; rp.stencilAttachment.clearStencil = 0x5a; } + else { rp.colorAttachments[0].texture = t; rp.colorAttachments[0].loadAction = la; rp.colorAttachments[0].storeAction = sa; rp.colorAttachments[0].clearColor = MTLClearColorMake(0.2, 0.4, 0.6, 0.8); } + [[cb renderCommandEncoderWithDescriptor:rp] endEncoding]; + } + id out = [g_dev newBufferWithLength:w * h * 4 options:MTLResourceStorageModeShared]; + id b = [cb blitCommandEncoder]; + [b copyFromTexture:t sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(w, h, 1) toBuffer:out destinationOffset:0 + destinationBytesPerRow:w * 4 destinationBytesPerImage:w * h * 4 options:depth ? MTLBlitOptionDepthFromDepthStencil : MTLBlitOptionNone]; + [b endEncoding]; [cb commit]; [cb waitUntilCompleted]; + const uint32_t *px = out.contents; NSUInteger bad = 0; for (NSUInteger i = 0; i < w * h; i++) if (px[i] != px[0]) bad++; + MLOG("UDC自检 %s %s %lux%lu 首值 %08x 与首值不同 %lu / %lu", g_dev.name.UTF8String, depth ? "Depth32F_S8" : "RGBA8", (unsigned long)w, (unsigned long)h, px[0], (unsigned long)bad, (unsigned long)(w * h)); + } +} +void mtl_init(void) { + if (g_dev) return; + const char *vbc = getenv("DM_NVN_VBUF_CACHE"); g_vbuf_cache = !vbc || strcmp(vbc, "0"); + const char *boc = getenv("DM_NVN_BUFFER_OFFSET"); g_buffer_offset_cache = !boc || strcmp(boc, "0"); + const char *link = getenv("DM_PRESENT_LINK"); + g_dm_present_link = link ? atoi(link) : 1; if (g_dm_present_link < 0 || g_dm_present_link > 2) g_dm_present_link = 1; // Default 1 (real-device same-process alternation: when load exceeds one display-link period, long frames 15% → 0 and frame intervals even out; no difference from 0 at a steady 60); DM_PRESENT_LINK=0 turns it off // 1 = present on every tick of a 60 Hz display link; 2 = 120 Hz display link, the game presents a frame every other display-link tick (when behind, it can present on the next 8.33 ms display-link tick) + const char *latency = getenv("DM_PRESENT_LATENCY"); + if (latency && atoi(latency) > 0) g_present_latency = atoi(latency); + g_res_check_on = getenv("DM_RES_CHECK") != NULL; + g_dev = MTLCreateSystemDefaultDevice(); + if (!g_dev) MDIE("没有 Metal 设备"); + legacy_arguments_configure(g_dev); + g_layered_native = [g_dev supportsFamily:MTLGPUFamilyApple5] || [g_dev supportsFamily:MTLGPUFamilyMac1]; + MLOG("layered rendering: %s", g_layered_native ? "native" : "verified per-slice companions required"); + g_bare_native = [g_dev supportsFamily:MTLGPUFamilyApple4] || [g_dev supportsFamily:MTLGPUFamilyMac2]; + g_fragment_image_native = [g_dev supportsFamily:MTLGPUFamilyApple4] || [g_dev supportsFamily:MTLGPUFamilyMac1]; + MLOG("attachment-less rendering: %s", g_bare_native ? "native" : "transient attachment preserving raster bounds"); + MLOG("argument buffers: tier=%s encoding=%s", g_dev.argumentBuffersSupport == MTLArgumentBuffersTier1 ? "1" : "2", + g_legacy_direct_arguments ? "direct GPU addresses" : "MTLArgumentEncoder private layout"); + if (!g_dev.hasUnifiedMemory) MDIE("需要统一内存设备(零拷贝内存池)"); + g_q = [g_dev newCommandQueue]; + if (!g_q) MDIE("【红】Cannot create the iOS 17 command queue"); + g_alloc_n = 3; + { void crash_set_wait_dump(void (*fn)(void)); void mtl_wait_state_dump(void); crash_set_wait_dump(mtl_wait_state_dump); } + for (int i = 0; i < 3; i++) { + g_present_uv[i] = [g_dev newBufferWithLength:sizeof(float) * 8 options:MTLResourceStorageModeShared]; + if (!g_present_uv[i]) MDIE("建立出图 UV 缓冲[%d] 失败", i); + res_add(g_present_uv[i]); + g_bloomq_const[i] = [g_dev newBufferWithLength:sizeof(float) * 8 options:MTLResourceStorageModeShared]; + if (!g_bloomq_const[i]) MDIE("建立泛光 1/4 常量缓冲[%d] 失败", i); + res_add(g_bloomq_const[i]); + } + g_ev = [g_dev newSharedEvent]; + g_keep = [NSMutableArray array]; + g_nvn_deswz_gpu = nvn_deswz_gpu_init(g_dev) == 0; + dm_res_report_register(mtl_res_counts); + if (!g_nvn_deswz_gpu) MLOG("【红】显卡解排布初始化失败,预排布贴图回退 CPU"); + MLOG("Metal 设备 %s", g_dev.name.UTF8String); + udc_selftest(); +} + +void dm_mtl_l3_tick(uint32_t tick) { + static int leak = -1; + if (leak < 0) leak = getenv("DM_TEST_LEAK_GPU") != NULL; + if (leak && tick % 100 == 0) { + if (!g_l3_gpu_leaks) g_l3_gpu_leaks = [NSMutableArray array]; + id leak = [g_dev newBufferWithLength:1 << 20 options:MTLResourceStorageModeShared]; + if (leak) [g_l3_gpu_leaks addObject:leak]; + } +} + +uint64_t dm_mtl_l3_sample(uint32_t tick) { + (void)tick; + return g_dev ? g_dev.currentAllocatedSize : 0; +} + +// No-copy buffers of finalized pools (weak references, under g_mu): the memory report counts how many are still alive and their length -- a buffer that outlives its pool is still strongly referenced by something else (a texture built directly on the pool, etc.) +static NSHashTable *g_dead_pools; +// Address → pool cache per thread (mtl_addr / mtl_gpu_addr): 8 entries replaced in turn, valid while g_pool_gen is unchanged (every pool add / removal bumps it under g_mu), holding only pools +// already in the residency set. Every buffer binding of every draw used to take g_mu, which game threads creating pools and the GPU completion handler also take, and search the pool index +static _Atomic uint64_t g_pool_gen = 1; +typedef struct { uintptr_t lo, hi, base; __unsafe_unretained id buf; uint64_t gpu; } pc_ent; +static __thread struct { uint64_t gen; unsigned n, next; pc_ent e[8]; } t_pc; +static inline const pc_ent *pc_get(uint64_t addr, uint64_t len) { + if (t_pc.gen != atomic_load_explicit(&g_pool_gen, memory_order_acquire)) return NULL; + for (unsigned k = 0; k < t_pc.n; k++) { const pc_ent *c = &t_pc.e[k]; if (addr >= c->lo && addr + len <= c->hi) return c; } + return NULL; +} +static void pc_put(int i) { // caller holds g_mu, pool i is resident + uint64_t gen = atomic_load_explicit(&g_pool_gen, memory_order_relaxed); + if (t_pc.gen != gen) { t_pc.gen = gen; t_pc.n = 0; t_pc.next = 0; } + pc_ent *c = &t_pc.e[t_pc.n < 8 ? t_pc.n++ : t_pc.next++ % 8]; + *c = (pc_ent){g_pools[i].lo, g_pools[i].hi, g_pools[i].base, g_pools[i].buf, g_pools[i].gpu}; +} +static _Atomic uint64_t g_reg_gen = 1; // Change generation of memory pool / texture / sampler registration: one of the invalidation conditions for argument buffer reuse +// On-demand residency of memory pools (from d2r-mobile e2a0c9a9b): putting a zero-copy buffer into the residency set makes iOS read the whole pool into memory and pin it as wired, even a pool the game never uses +// (they measured on a 17 Pro Max a 512 MB render-target pool addressed 0 times in a run, with about 530 MB more device wired memory; a 4 GB A14 was ended by the system within seconds of launch because of it). +// So a pool is not put into the residency set when created; it is put in, and the residency set committed on the spot, the first time the GPU gets its address through mtl_addr / mtl_gpu_addr -- these are the only two ways the GPU gets pool addresses, +// and committing on the spot is earlier than any command buffer commit that uses it. CPU-side reads of pool contents through mtl_addr trigger it too (conservative, never misses). Caller holds g_mu +static void pool_res(int i) { + if (g_pools[i].res) return; + res_add(g_pools[i].buf); res_commit(); g_pools[i].res = 1; g_pool_res_n++; +} +// A failed no-copy registration is a CPU VM/buffer creation error. Report the +// actual host-page span rather than only the guest's smaller logical range. +static void pool_nocopy_failure(uintptr_t a, uintptr_t b, uintptr_t pg) { + MLOG("no-copy pool failure: host-page=%#llx span=%#llx+%#llx max-buffer=%#llx allocated=%#llx", + (unsigned long long)pg, (unsigned long long)a, (unsigned long long)(b - a), + (unsigned long long)g_dev.maxBufferLength, (unsigned long long)g_dev.currentAllocatedSize); + vm_address_t cursor = a; + for (int n = 0; cursor < b && n < 16; n++) { + vm_address_t start = cursor; vm_size_t length = 0; + vm_region_submap_info_data_64_t info; natural_t depth = 0; + mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64; + kern_return_t kr; + for (;;) { + kr = vm_region_recurse_64(mach_task_self(), &start, &length, &depth, + (vm_region_recurse_info_t)&info, &count); + if (kr != KERN_SUCCESS || !info.is_submap) break; + depth++; count = VM_REGION_SUBMAP_INFO_COUNT_64; + } + if (kr != KERN_SUCCESS || !length) { MLOG("no-copy pool VM query failed at %#llx: %d", (unsigned long long)cursor, kr); break; } + MLOG("no-copy pool VM: %#llx+%#llx prot=%x max=%x share=%u external=%u gap=%d", + (unsigned long long)start, (unsigned long long)length, info.protection, + info.max_protection, info.share_mode, info.external_pager, start > cursor); + if (start + length <= cursor) break; + cursor = start + length; + } +} +void mtl_pool_add(const void *mem, uint64_t size) { + mtl_init(); g_reg_gen++; + uintptr_t pg = (uintptr_t)getpagesize(), lo = (uintptr_t)mem, hi = lo + size; + uintptr_t a = lo & ~(pg - 1), b = (hi + pg - 1) & ~(pg - 1); + id buf = [g_dev newBufferWithBytesNoCopy:(void *)a length:b - a options:MTLResourceStorageModeShared deallocator:nil]; + // Restored from the original backend: pinning an otherwise valid VM span + // may be refused transiently while the guest maps/unmaps large regions. + // Keep the original zero-copy storage and logical bounds; wait a bounded + // interval before treating a temporary allocation refusal as fatal. + for (int retry = 0; !buf && retry < 12; retry++) { + usleep(10000u << (retry < 4 ? retry : 4)); + buf = [g_dev newBufferWithBytesNoCopy:(void *)a length:b - a options:MTLResourceStorageModeShared deallocator:nil]; + if (buf) MLOG("[RED] memory pool %p+%#llx: no-copy buffer created at retry %d", mem, (unsigned long long)size, retry + 1); + } + if (!buf) { pool_nocopy_failure(a, b, pg); MDIE("Cannot register no-copy memory pool %p+%#llx", mem, (unsigned long long)size); } + pthread_mutex_lock(&g_mu); + if (g_npool == g_cap) { g_cap = g_cap ? g_cap * 2 : 64; g_pools = realloc(g_pools, g_cap * sizeof *g_pools); } + g_pidx_ok = 0; g_plast = -1; atomic_fetch_add(&g_pool_gen, 1); // the address index and the per-thread caches are invalid (pool_find, pc_get) + g_pools[g_npool++] = (pool_ent){lo, hi, a, buf, legacy_buffer_gpu(buf), mem, 0}; // not in the residency set yet: put in the first time it is addressed (pool_res) + g_pool_adds++; + [g_keep addObject:buf]; + pthread_mutex_unlock(&g_mu); +} + +void mtl_pool_del(const void *mem) { + g_reg_gen++; + pthread_mutex_lock(&g_mu); + // The no-copy buffer goes through deferred release (out of the residency set and unreferenced after submitted commands complete). It used to be removed only from g_keep while the residency set still held a strong reference: after the pool was freed the buffer stayed alive wrapping that guest memory, + // and a new pool at the same address wrapped it again. Engage's title demo cycles scenes; GPU allocated grew to 5.4 GB and resident memory to 3.6 GB in 850 s. + // This may run on a guest thread whose implicit autorelease pool never drains; + // keep weak-table operations from extending the finalized buffer's lifetime. + @autoreleasepool { + for (int i = 0; i < g_npool; i++) + if (g_pools[i].key == mem) { id b = g_pools[i].buf; [g_keep removeObject:b]; defer_obj_release(b); if (g_pools[i].res) g_pool_res_n--; + if (!g_dead_pools) g_dead_pools = [NSHashTable weakObjectsHashTable]; + [g_dead_pools addObject:b]; g_pools[i] = g_pools[--g_npool]; g_pidx_ok = 0; g_plast = -1; atomic_fetch_add(&g_pool_gen, 1); break; } + } + pthread_mutex_unlock(&g_mu); +} + +// Address → pool: the last hit first, then a binary search over indices sorted by start address, and a full linear scan only when that fails (pools overlapping). +// The Legend of Zelda: Breath of the Wild keeps about 1760 memory pools alive and every buffer binding of every draw looks one up; the old full scan took about 4% of the submit thread's samples (mtl_gpu_addr 222 / 6 s). Adding / removing pools invalidates the index, which is re-sorted when next used. Caller holds g_mu +static int *g_pidx; static int g_pidx_cap; +static int pidx_cmp(const void *a, const void *b) { uintptr_t x = g_pools[*(const int *)a].lo, y = g_pools[*(const int *)b].lo; return x < y ? -1 : x > y; } +static int pool_find(uint64_t addr, uint64_t len) { + if (g_plast >= 0 && g_plast < g_npool && addr >= g_pools[g_plast].lo && addr + len <= g_pools[g_plast].hi) return g_plast; + if (!g_pidx_ok) { + if (g_pidx_cap < g_npool) { g_pidx_cap = g_npool * 2; g_pidx = realloc(g_pidx, (size_t)g_pidx_cap * sizeof *g_pidx); } + for (int i = 0; i < g_npool; i++) g_pidx[i] = i; + qsort(g_pidx, (size_t)g_npool, sizeof *g_pidx, pidx_cmp); g_pidx_ok = 1; + } + int lo = 0, hi = g_npool - 1, hit = -1; // the last one whose start ≤ addr + while (lo <= hi) { int m = (lo + hi) / 2; if (g_pools[g_pidx[m]].lo <= addr) { hit = m; lo = m + 1; } else hi = m - 1; } + if (hit >= 0 && addr + len <= g_pools[g_pidx[hit]].hi) return g_plast = g_pidx[hit]; + for (int i = 0; i < g_npool; i++) if (addr >= g_pools[i].lo && addr + len <= g_pools[i].hi) return g_plast = i; + return -1; +} +int mtl_addr(uint64_t addr, uint64_t len, void **buf, uint64_t *off) { + const pc_ent *c = pc_get(addr, len); + if (c) { *buf = (__bridge void *)c->buf; *off = addr - c->base; return 1; } + int ok = 0; + pthread_mutex_lock(&g_mu); + int i = pool_find(addr, len); + if (i >= 0) { pool_res(i); pc_put(i); *buf = (__bridge void *)g_pools[i].buf; *off = addr - g_pools[i].base; ok = 1; } + pthread_mutex_unlock(&g_mu); + return ok; +} +int mtl_gpu_addr(uint64_t addr, uint64_t len, void **buf, uint64_t *off, uint64_t *gpu) { + const pc_ent *c = pc_get(addr, len); + if (c) { *buf = (__bridge void *)c->buf; *off = addr - c->base; *gpu = c->gpu + *off; return 1; } + int ok = 0; + pthread_mutex_lock(&g_mu); + int i = pool_find(addr, len); + if (i >= 0) { pool_res(i); pc_put(i); *buf = (__bridge void *)g_pools[i].buf; *off = addr - g_pools[i].base; *gpu = g_pools[i].gpu + *off; ok = 1; } + pthread_mutex_unlock(&g_mu); + return ok; +} + +// ---------- Submission ---------- +static void defer_reset(void); static void defer_fallback(void); +typedef struct { int kind; volatile uint32_t *val; uint32_t mode, v; uint64_t addr; uint32_t type; void *vis; } post_op; // vis: the visibility slot table for kind 3 +// ---------- Conditional rendering (profile render_cond) ---------- +// Metal has no predication, so replay evaluates NVN's render-enable condition +// from its two counter records. Timestamp reports have a known counter value of +// zero; samples-passed reports remain unknown until completion and are drawn. +static int g_rc_skip; +#define RC_PEND 256 +static struct { uint64_t addr; int kind; } g_rc_pend[RC_PEND]; +static int g_rc_npend; +static uint64_t g_rc_occ[4096]; +static void rc_occ_add(uint64_t a) { + for (uint32_t i = (uint32_t)(a >> 4) & 4095, k = 0; k < 4096; k++, i = (i + 1) & 4095) { + if (g_rc_occ[i] == a) return; + if (!g_rc_occ[i]) { g_rc_occ[i] = a; return; } + } +} +static int rc_occ_has(uint64_t a) { + for (uint32_t i = (uint32_t)(a >> 4) & 4095, k = 0; k < 4096; k++, i = (i + 1) & 4095) { + if (g_rc_occ[i] == a) return 1; + if (!g_rc_occ[i]) return 0; + } + return 0; +} +static void rc_note(uint64_t a, int kind) { + if (kind == 2) rc_occ_add(a); + for (int i = 0; i < g_rc_npend; i++) if (g_rc_pend[i].addr == a) { g_rc_pend[i].kind = kind; return; } + if (g_rc_npend < RC_PEND) g_rc_pend[g_rc_npend++] = (typeof(g_rc_pend[0])){a, kind}; +} +static int rc_value(uint64_t a, uint64_t *v) { + if (rc_occ_has(a)) return 0; + for (int i = g_rc_npend - 1; i >= 0; i--) if (g_rc_pend[i].addr == a) { + if (g_rc_pend[i].kind != 1) return 0; + *v = 0; + return 1; + } + *v = *(const volatile uint64_t *)a; + return 1; +} +// Diagnostic A/B for games whose conditional-render profile has not yet been +// proven on the device. Keep decoding and logging the condition, but draw the +// command anyway. This is runtime-controlled so the same binary can be +// compared with and without the override. +static _Atomic int g_rc_always_draw = -1, g_force_visible = -1, g_min_reduction = -1; +static int gfx_diagnostic_bool(_Atomic int *state, const char *key) { + int value = atomic_load_explicit(state, memory_order_acquire); + if (value >= 0) return value; + const char *e = getenv(key); value = e && !strcmp(e, "1"); + int expected = -1; + // A live notification wins over an initializer that read the old env. + if (!atomic_compare_exchange_strong_explicit(state, &expected, value, memory_order_acq_rel, memory_order_acquire)) return expected; + if (value) MLOG("%s=1: graphics diagnostic initialized", key); + return value; +} +// DM_CTL publishes the parsed value directly. Never reset to getenv here: +// completion and replay threads may read these diagnostic switches together. +void dm_gfx_env_changed(const char *key, const char *value) { + if (!key) return; + _Atomic int *state; + if (!strcmp(key, "DM_NVN_RENDER_COND_ALWAYS_DRAW")) state = &g_rc_always_draw; + else if (!strcmp(key, "DM_NVN_FORCE_VISIBLE")) state = &g_force_visible; + else if (!strcmp(key, "DM_NVN_MIN_REDUCTION")) state = &g_min_reduction; + else return; + int on = value && !strcmp(value, "1"); + atomic_store_explicit(state, on, memory_order_release); + MLOG("Live graphics diagnostic %s=%d", key, on); +} +static int rc_always_draw(void) { + return gfx_diagnostic_bool(&g_rc_always_draw, "DM_NVN_RENDER_COND_ALWAYS_DRAW"); +} +static void rc_override_note(const p_render_cond *rc, int would_skip, int known, uint64_t x, uint64_t y) { + if (!would_skip || !rc_always_draw()) return; + static uint64_t n; + n++; + if (n <= 64 || !(n % 600)) + MLOG("conditional-render override #%llu: mode=%d addr=%#llx known=%d values=%llu/%llu (draw kept)", + (unsigned long long)n, rc->mode, (unsigned long long)rc->addr, known, + (unsigned long long)x, (unsigned long long)y); +} +// ---------- Occlusion queries (NVN_COUNTER_TYPE_SAMPLES_PASSED) ---------- +// NVN semantics: samples passed is a cumulative counter on the queue - ResetCounter zeroes it, every draw afterwards adds to it, and ReportCounter writes out the cumulative value at that moment (+ timestamp) when the GPU completes. +// Metal: every render pass attaches a visibility result buffer; draws during counting use counting mode, with a new slot per pass (no reliance on accumulation across passes); a reset empties the 'slot table since the reset', +// and a report hands the slot table together with its buffer (retained) to the completion callback to sum and write back; after a report a new slot is used, so later draws are not counted into the reported value. +// (Previously always wrote 0: occlusion query = 'fully occluded', campfires / light sources got culled - the character select screen calls ResetCounter 21 times per frame) +typedef struct { void *buf; uint32_t slot; } vis_slot; +typedef struct { int n; vis_slot s[]; } vis_list; +#define VIS_SLOTS 8192 +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) +// iOS 16 exposes no sampler min/max reduction mode. Z-A uses a min-reduced +// depth pyramid for occlusion, so an averaged pyramid can report visible props +// as zero samples. This opt-in fallback promotes only zero results to visible. +static int vis_force_visible(void) { + return gfx_diagnostic_bool(&g_force_visible, "DM_NVN_FORCE_VISIBLE"); +} +static void vis_clear(void) { for (int i = 0; i < g_nvs; i++) CFRelease(g_vs[i].buf); g_nvs = 0; g_vis_cur = -1; } +static vis_list *vis_snapshot(void) { + vis_list *l = malloc(sizeof *l + g_nvs * sizeof(vis_slot)); l->n = g_nvs; + for (int i = 0; i < g_nvs; i++) { l->s[i] = g_vs[i]; CFRetain(g_vs[i].buf); } + return l; +} +// Target ranges of buffer writes (copies / inline uniform updates) already encoded in this submission but not yet executed on the GPU: reading these addresses on the CPU side at encode time yields old values (uv_proof avoids them when reading uniforms) +#define PEND_MAX 256 +static uint64_t g_pend_lo[PEND_MAX], g_pend_hi[PEND_MAX]; static int g_npend, g_pend_over; +static int pend_hits(uint64_t a, uint64_t n) { if (g_pend_over) return 1; for (int i = 0; i < g_npend; i++) if (a < g_pend_hi[i] && a + n > g_pend_lo[i]) return 1; return 0; } +// Persistent GPU-write ranges. g_npend is reset at submission boundaries, but +// the CPU may replay the next submission while the previous one is still in +// flight. Indirect arguments must therefore remember GPU writers across that +// boundary instead of trusting the current bytes in shared memory. +#define GPU_WRITE_MAX 512 +typedef struct { uint64_t lo, hi, epoch; } gpu_write_range; +static gpu_write_range g_gpu_write[GPU_WRITE_MAX]; static int g_gpu_write_n; +static uint64_t g_gpu_write_epoch, g_gpu_write_synced_epoch, g_indirect_sync_sub; +static uint64_t g_gpu_write_over_epoch; +static void gpu_write_note(uint64_t a, uint64_t n) { + if (!a || !n || a > UINT64_MAX - n) { g_gpu_write_over_epoch = ++g_gpu_write_epoch; return; } + uint64_t hi = a + n, epoch = ++g_gpu_write_epoch; + for (int i = 0; i < g_gpu_write_n; i++) if (a < g_gpu_write[i].hi && g_gpu_write[i].lo < hi) { + if (a < g_gpu_write[i].lo) g_gpu_write[i].lo = a; + if (hi > g_gpu_write[i].hi) g_gpu_write[i].hi = hi; + g_gpu_write[i].epoch = epoch; return; + } + if (g_gpu_write_n < GPU_WRITE_MAX) g_gpu_write[g_gpu_write_n++] = (gpu_write_range){a, hi, epoch}; + else g_gpu_write_over_epoch = epoch; +} +static uint64_t gpu_write_hit_epoch(uint64_t a, uint64_t n) { + uint64_t epoch = g_gpu_write_over_epoch; + if (!a || !n || a > UINT64_MAX - n) return g_gpu_write_epoch; + for (int i = 0; i < g_gpu_write_n; i++) if (a < g_gpu_write[i].hi && g_gpu_write[i].lo < a + n && g_gpu_write[i].epoch > epoch) epoch = g_gpu_write[i].epoch; + return epoch; +} +static void pend_add(uint64_t a, uint64_t n) { + if (!a || !n) return; + if (g_npend < PEND_MAX && a <= UINT64_MAX - n) { g_pend_lo[g_npend] = a; g_pend_hi[g_npend++] = a + n; } + else g_pend_over = 1; + gpu_write_note(a, n); +} +typedef struct { uint64_t ser; uint16_t level, slice; } zskip_zero; +static zskip_zero g_zskip_zero[4096]; static int g_zskip_n, g_zskip_count; +static zskip_zero g_zskip_cand[8]; static uint8_t g_zskip_cand_ok[8]; +static void zskip_clear(void); +static int tbdr_on(void); +static id g_cb; +static id g_blit; +static NSMutableArray *g_commit_blocks; +static NSMutableArray *g_sub_argument_checks; +// Raw sampler resource IDs in argument buffers do not retain their objects. +// Keep each referenced sampler alive until this submission's GPU completion. +static NSMutableSet> *g_sub_samplers; +static NSMutableString *g_sub_sample_notes; +static uint64_t g_alloc_fence[ALLOC_MAX]; static uint64_t g_alloc_waits, g_alloc_grown; +int mtl_wait(uint64_t fence, uint64_t timeout_ns); +static void up_flush(void); +// The upload batch belongs only to the submit thread (the thread that called mtl_sub_begin): the batch's encoder cannot be used across threads. Every entry point asserts current thread == submit thread, +// and on mismatch prints the stack and aborts on the spot - so that 'another thread touched the batch' is exposed deterministically on Mac too, not by timing (real iOS device stuck at the main menu: the present thread flushed the submit thread's batch via q_cb). +static pthread_t g_sub_thread; static _Atomic int g_sub_thread_set; +static int on_sub_thread(void) { return atomic_load(&g_sub_thread_set) && pthread_equal(pthread_self(), g_sub_thread); } +static void up_owner_check(const char *where) { + if (!atomic_load(&g_sub_thread_set) || on_sub_thread()) return; + char name[64] = ""; pthread_getname_np(pthread_self(), name, sizeof name); + MLOG("【红】上传批只属于提交线程:%s 在线程「%s」上被调用", where, name); + void *bt[32]; int n = backtrace(bt, 32); backtrace_symbols_fd(bt, n, 2); + abort(); +} +// Every command buffer created outside the replay / upload batch goes through this. On the submit thread: flush this batch's uploads first, so that commands that then read or write these textures are ordered after the uploads; +// other threads (the present thread etc.) creating a command buffer do not touch the batch: they do not read or write textures uploaded in this batch; commands that need these textures all come after this replay submission (the batch is flushed before it) +// Old-queue command buffers (readback / diagnostics / present; to be deleted once M4-4 and M4-5 are migrated away): uploads are already on the Metal 4 queue, and there is no ordering guarantee between the two queues - +// wait for the upload to complete after flushing it, and only then can the old queue read the upload result (mtl_read_texels waited first and flushed afterwards, the readback beat the upload, 4 failures on t_nvn upload content) +static void up_wait_zero(_Atomic int *v); +static id q_cb(void) { if (on_sub_thread()) { up_flush(); up_wait_zero(&g_up_pending); } return [g_q commandBuffer]; } +// Ask Metal to preserve per-encoder execution status for replay command buffers. +// A bare GPU-hang error only identifies the whole frame; with this option the +// error carries the label and debug signposts of the encoder that faulted. +static id replay_cb(void) { + MTLCommandBufferDescriptor *d = [MTLCommandBufferDescriptor new]; + d.errorOptions = MTLCommandBufferErrorOptionEncoderExecutionStatus; + return [g_q commandBufferWithDescriptor:d]; +} +static void log_encoder_errors(NSError *error, uint64_t fence) { + NSArray> *infos = error.userInfo[MTLCommandBufferEncoderInfoErrorKey]; + for (id info in infos) { + if (info.errorState == MTLCommandEncoderErrorStateUnknown || info.errorState == MTLCommandEncoderErrorStateCompleted) continue; + MLOG("【红】GPU 故障 encoder fence=%llu state=%ld label=%s signposts=%s", + (unsigned long long)fence, (long)info.errorState, + (info.label ?: @"-").UTF8String, (info.debugSignposts ?: @[]).description.UTF8String); + } +} +static void log_function_errors(id cb, const char *kind, uint64_t fence) { + static _Atomic unsigned reports; + for (id item in cb.logs) { + if (atomic_fetch_add(&reports, 1) >= 64) break; + id loc = item.debugLocation; + MLOG("[RED] Shader validation %s fence=%llu encoder=%s function=%s source=%s:%lu:%lu detail=%s", kind, (unsigned long long)fence, (item.encoderLabel ?: @"-").UTF8String, (loc.functionName ?: item.function.name ?: @"-").UTF8String, (loc.URL.path ?: @"-").UTF8String, (unsigned long)loc.line, (unsigned long)loc.column, item.description.UTF8String); + } +} +static post_op *g_post; static int g_npost, g_postcap; +// One autorelease pool per submission: submissions run on a guest thread (long-lived, no run loop, never drains the implicit pool), so objects autoreleased in the Metal replay +// (command buffers, encoders, pass descriptors, etc.) are never released unless put in a pool. Measured on the character select screen of a real iOS device: the host heap grows by about 47 objects per frame, 0.22 MiB/s. +void *objc_autoreleasePoolPush(void); void objc_autoreleasePoolPop(void *); +static void *g_sub_pool; +static int g_ruler_truth = -1, g_ruler_pending; +static int tbdr_on(void); + +void mtl_sub_begin(void) { + mtl_init(); g_sub_serial++; g_rc_npend = 0; g_rc_skip = 0; + if (tbdr_on() == 0 && g_ruler_pending) { g_ruler_truth = 0; g_ruler_pending = 0; MLOG("DM_NVN_RULER:切到被测模式"); } + defer_drain(); + defer_reset(); + warm_claim(); + if (g_cb) MDIE("提交重入"); + defer_submission_begin_state(); + g_sub_thread = pthread_self(); atomic_store(&g_sub_thread_set, 1); + atomic_store(&g_last_commit_error, 0); + g_sub_pool = objc_autoreleasePoolPush(); + g_cb = replay_cb(); + g_defer_cut_active = 1; + if (!g_sub_samplers) g_sub_samplers = [NSMutableSet set]; + [g_sub_samplers removeAllObjects]; + [g_sub_sample_notes setString:@""]; + // Do not wait for the GPU mid-frame (hot real device: with a fixed rotation of 3, when the GPU cannot keep up every frame blocks here, and the submit thread runs at low frequency after waking, instruction count −18% but CPU time +49%): + // pick an allocator the GPU has finished with; if all are in use create a new one; only at the cap wait for the oldest one. How far ahead it runs is bounded by the wait where the game acquires the swapchain texture, same as the old interface. + int alloc_i = -1; uint64_t done = g_done; + for (int i = 0; i < g_alloc_n; i++) if (g_alloc_fence[i] <= done) { alloc_i = i; break; } + if (alloc_i < 0 && g_alloc_n < ALLOC_MAX) { alloc_i = g_alloc_n++; g_alloc_grown++; } + if (alloc_i < 0) { alloc_i = 0; for (int i = 1; i < g_alloc_n; i++) if (g_alloc_fence[i] < g_alloc_fence[alloc_i]) alloc_i = i; g_alloc_waits++; mtl_wait(g_alloc_fence[alloc_i], UINT64_MAX); } + g_alloc_cur = alloc_i; + g_commit_blocks = [NSMutableArray array]; + g_sub_argument_checks = nil; + // The overflow buffer created when the previous submission ran out of slots (see g_vis_n >= VIS_SLOTS): retire it. It used to be just cleared here and stayed in the residency set for good (from d2r-mobile 2eb575877) + if (g_vis && !vis_in_ring(g_vis)) defer_obj_release(g_vis); + g_npost = 0; g_vis = nil; g_vis_n = 0; g_vis_cur = -1; g_npend = 0; g_pend_over = 0; + zskip_clear(); g_zskip_count = 0; + int pick = -1; + for (int i = 0; i < g_vis_ring_n; i++) if (g_vis_ring[i].fence <= g_done) { pick = i; break; } + if (pick < 0 && g_vis_ring_n < 4) pick = g_vis_ring_n++; + 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); } + 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(); +} + +static void rp_end(void); +extern int g_frz_on; static int g_frz_state; // World freeze: the state latched for this frame (0 normal / 1 full frame / 2 frozen frame), see the note above frz_op +// DM_NVN_PASSGPU_SERIAL=1 (for measurement): before creating each encoder on the main command buffer, encode 'signal n+1 → wait for signal n+1' - the signal is only raised once all prior work has completed, +// so the following encoder starts only after the previous one has fully finished; on TBDR passes no longer overlap in the pipeline, and the per-pass start-to-end span of DM_NVN_PASSGPU is that pass's exclusive time (the whole-frame total is inflated; look only at relative cost) +static void ser_gate(void) { + static int on = -1; static id ev; static uint64_t n; + if (on < 0) { on = getenv("DM_NVN_PASSGPU_SERIAL") != NULL; if (on) { ev = [g_dev newEvent]; MLOG("DM_NVN_PASSGPU_SERIAL:通道串行化(测量用,耗时偏大)"); } } + if (!on) return; + (void)ev; (void)n; +} +// Guest memory pools can expose the same allocation through different Metal +// buffer/texture objects. Automatic tracking cannot infer all such aliases. +// Keep the original backend's full pass ordering, including texture-cache +// visibility, with a queue-local fence. Z-A v47 faulted in a single-sample +// 7x4 postprocess pass while OP_BARRIER only set an otherwise-unused flag. +static id g_pass_order_fence; +static __unsafe_unretained id g_pass_order_encoder; +static int g_pass_order_kind, g_pass_order_valid; +static uint64_t g_pass_order_waits, g_explicit_barriers; +static void legacy_order_begin(id e, int kind, id cb) { + if (!e || !on_sub_thread() || cb != g_cb) return; + if (!g_pass_order_fence) { g_pass_order_fence = [g_dev newFence]; MLOG("legacy Metal pass ordering: explicit render/compute/blit fence for aliased guest resources"); } + if (g_pass_order_valid) { + if (kind == 1) [(id)e waitForFence:g_pass_order_fence beforeStages:MTLRenderStageVertex]; + else if (kind == 2) [(id)e waitForFence:g_pass_order_fence]; + else [(id)e waitForFence:g_pass_order_fence]; + g_pass_order_waits++; + } + g_pass_order_encoder = e; g_pass_order_kind = kind; +} +static inline void m4_end(id e) { + if (e && on_sub_thread() && e == g_pass_order_encoder) { + if (g_pass_order_kind == 1) [(id)e updateFence:g_pass_order_fence afterStages:MTLRenderStageFragment]; + else if (g_pass_order_kind == 2) [(id)e updateFence:g_pass_order_fence]; + else [(id)e updateFence:g_pass_order_fence]; + g_pass_order_valid = 1; g_pass_order_encoder = nil; + } + [e endEncoding]; +} +// Explicit creation epochs prevent reused Objective-C encoder addresses from +// inheriting declarations. Flags are a union even when dedup is disabled. +#include "nvn_resource_usage.h" +static nvn_resource_usage_set g_legacy_use; +static uint64_t g_legacy_use_calls, g_legacy_use_skipped; +// Submit-thread-only encoder cache. Reset on creation, never keyed by an Objective-C address alone. +static struct { + uint32_t valid, buffer_valid[2]; + uint32_t pso_valid, dss_valid; + MTLViewport viewport; MTLScissorRect scissor; + MTLCullMode cull; MTLWinding winding; MTLTriangleFillMode fill; + float bias[3], blend[4]; uint32_t stencil[2]; + __unsafe_unretained id buffer[2][32]; NSUInteger offset[2][32]; + __unsafe_unretained id pso; + __unsafe_unretained id dss; +} g_legacy_state; +static uint64_t g_legacy_state_calls, g_legacy_state_skips; +static uint64_t g_legacy_pso_calls, g_legacy_pso_skips; +static uint64_t g_legacy_dss_calls, g_legacy_dss_skips; +static int legacy_state_on(void) { + static int on = -1; if (on < 0) { const char *e = getenv("DM_NVN_STATE_CACHE"); on = !e || strcmp(e, "0"); } + return on; +} +static int legacy_pipeline_bind_cache_on(void) { + static int on = -1; + if (on < 0) { + if (!legacy_state_on()) on = 0; + else { const char *e = getenv("DM_NVN_PIPELINE_BIND_CACHE"); on = !e || strcmp(e, "0"); } + } + return on; +} +static int legacy_same(unsigned bit, void *saved, const void *value, size_t size) { + if (legacy_state_on() && (g_legacy_state.valid & bit) && !memcmp(saved, value, size)) { g_legacy_state_skips++; return 1; } + memcpy(saved, value, size); g_legacy_state.valid |= bit; g_legacy_state_calls++; return 0; +} +static void legacy_bind_buffer(id e, int fragment, NSUInteger index, id b, NSUInteger off) { + int offset_only = 0; + if (index < 32) { + uint32_t bit = 1u << index; + int same_buffer = legacy_state_on() && (g_legacy_state.buffer_valid[fragment] & bit) && g_legacy_state.buffer[fragment][index] == b; + if (same_buffer && g_legacy_state.offset[fragment][index] == off) { g_legacy_state_skips++; return; } + // Metal retains the first binding. Offset-only updates require that same + // non-nil buffer on this encoder; enc_r_begin invalidates every slot. + offset_only = same_buffer && b && g_buffer_offset_cache; + g_legacy_state.buffer_valid[fragment] |= bit; g_legacy_state.buffer[fragment][index] = b; g_legacy_state.offset[fragment][index] = off; + } + g_legacy_state_calls++; + if (offset_only) { + g_buffer_offset_calls++; + if (fragment) [e setFragmentBufferOffset:off atIndex:index]; else [e setVertexBufferOffset:off atIndex:index]; + } else if (fragment) [e setFragmentBuffer:b offset:off atIndex:index]; else [e setVertexBuffer:b offset:off atIndex:index]; +} +static inline id enc_r_begin(MTLRenderPassDescriptor *d) { + memset(&g_legacy_state, 0, sizeof g_legacy_state); + id e = [g_cb renderCommandEncoderWithDescriptor:d]; + if (e) nvn_resource_usage_begin(&g_legacy_use); + legacy_order_begin(e, 1, g_cb); return e; +} +static void legacy_use_resource(id en, id r, MTLResourceUsage usage, int compute) { + if (!r) return; + static int dedup = -1; + if (dedup < 0) { const char *v = getenv("DM_NVN_USE_DEDUP"); dedup = !v || strcmp(v, "0"); } + uint64_t combined; int skip; + if (!nvn_resource_usage_add(&g_legacy_use, (uintptr_t)(__bridge void *)r, usage, dedup, &combined, &skip)) + MDIE("resource-usage union allocation failed; refusing an incomplete GPU declaration"); + if (skip) { g_legacy_use_skipped++; return; } + usage = (MTLResourceUsage)combined; + if (compute) [(id)en useResource:r usage:usage]; + else [(id)en useResource:r usage:usage]; + g_legacy_use_calls++; +} +void mtl_barrier_stats(uint64_t *skip, uint64_t *full) { *skip = 0; *full = g_pass_order_waits; } +static int g_game_barrier; +static inline id enc_r_begin_pass(MTLRenderPassDescriptor *d) { return enc_r_begin(d); } +static inline void enc_r_end_pass(id e) { m4_end(e); +} +static inline void enc_r_end(id e) { m4_end(e); } +static inline void enc_r_pso(id e, id p) { + if (legacy_pipeline_bind_cache_on() && g_legacy_state.pso_valid && g_legacy_state.pso == p) { + g_legacy_pso_skips++; g_legacy_state_skips++; return; + } + g_legacy_state.pso_valid = 1; g_legacy_state.pso = p; + g_legacy_pso_calls++; g_legacy_state_calls++; + [e setRenderPipelineState:p]; +} +static inline void enc_r_dss(id e, id s) { + if (legacy_pipeline_bind_cache_on() && g_legacy_state.dss_valid && g_legacy_state.dss == s) { + g_legacy_dss_skips++; g_legacy_state_skips++; return; + } + g_legacy_state.dss_valid = 1; g_legacy_state.dss = s; + g_legacy_dss_calls++; g_legacy_state_calls++; + [e setDepthStencilState:s]; +} +static inline void enc_r_viewport(id e, MTLViewport v) { if (!legacy_same(1, &g_legacy_state.viewport, &v, sizeof v)) [e setViewport:v]; } +static inline void enc_r_scissor(id e, MTLScissorRect r) { if (!legacy_same(2, &g_legacy_state.scissor, &r, sizeof r)) [e setScissorRect:r]; } +static inline void enc_r_cull(id e, MTLCullMode m) { if (!legacy_same(4, &g_legacy_state.cull, &m, sizeof m)) [e setCullMode:m]; } +static inline void enc_r_winding(id e, MTLWinding w) { if (!legacy_same(8, &g_legacy_state.winding, &w, sizeof w)) [e setFrontFacingWinding:w]; } +static inline void enc_r_fill(id e, MTLTriangleFillMode m) { if (!legacy_same(16, &g_legacy_state.fill, &m, sizeof m)) [e setTriangleFillMode:m]; } +static inline void enc_r_depth_bias(id e, float u, float s, float c) { float v[3] = {u,s,c}; if (!legacy_same(32, g_legacy_state.bias, v, sizeof v)) [e setDepthBias:u slopeScale:s clamp:c]; } +static inline void enc_r_depth_clip(id e, BOOL v) { [e setDepthClipMode:v ? MTLDepthClipModeClip : MTLDepthClipModeClamp]; } +static inline void enc_r_stencil_ref(id e, uint32_t f, uint32_t b) { + static int separate = -1; if (separate < 0) { const char *v = getenv("DM_NVN_SEPARATE_STENCIL"); separate = !v || strcmp(v, "0"); } + if (!separate) b = f; + uint32_t v[2] = {f,b}; if (!legacy_same(64, g_legacy_state.stencil, v, sizeof v)) [e setStencilFrontReferenceValue:f backReferenceValue:b]; +} +static inline void enc_r_blend_color(id e, float r, float g, float b, float a) { float v[4] = {r,g,b,a}; if (!legacy_same(128, g_legacy_state.blend, v, sizeof v)) [e setBlendColorRed:r green:g blue:b alpha:a]; } +static inline void enc_r_visibility(id e, MTLVisibilityResultMode m, NSUInteger o) { [e setVisibilityResultMode:m offset:o]; } +static inline void enc_r_draw(id e, MTLPrimitiveType p, NSUInteger v, NSUInteger n) { [e drawPrimitives:p vertexStart:v vertexCount:n]; } +static inline void enc_r_draw_instanced(id e, MTLPrimitiveType p, NSUInteger v, NSUInteger n, NSUInteger i, NSUInteger b) { [e drawPrimitives:p vertexStart:v vertexCount:n instanceCount:i baseInstance:b]; } +static inline void enc_r_draw_indexed(id e, MTLPrimitiveType p, NSUInteger n, MTLIndexType t, uint64_t gpu, NSUInteger len, NSUInteger i, NSInteger bv, NSUInteger bi) { (void)len; if (!n) return; NSUInteger off; id b = legacy_buffer_at(gpu, &off); if (!b || off > b.length || n > (b.length - off) / (t == MTLIndexTypeUInt16 ? 2 : 4)) MDIE("【红】Invalid index buffer range"); [e drawIndexedPrimitives:p indexCount:n indexType:t indexBuffer:b indexBufferOffset:off instanceCount:i baseVertex:bv baseInstance:bi]; } +static inline void enc_r_draw_indirect(id e, MTLPrimitiveType p, id a, NSUInteger ao) { + if (!a || ao > a.length || a.length - ao < sizeof(MTLDrawPrimitivesIndirectArguments)) MDIE("[RED] Invalid indirect argument buffer range"); + legacy_use_resource(e, a, MTLResourceUsageRead, 0); + [e drawPrimitives:p indirectBuffer:a indirectBufferOffset:ao]; +} +static inline void enc_r_draw_indexed_indirect(id e, MTLPrimitiveType p, MTLIndexType t, + id ib, NSUInteger io, NSUInteger il, + id a, NSUInteger ao) { + if (!ib || io > ib.length || il > ib.length - io || !a || ao > a.length || a.length - ao < sizeof(MTLDrawIndexedPrimitivesIndirectArguments)) + MDIE("[RED] Invalid indexed-indirect buffer range"); + legacy_use_resource(e, ib, MTLResourceUsageRead, 0); + legacy_use_resource(e, a, MTLResourceUsageRead, 0); + [e drawIndexedPrimitives:p indexType:t indexBuffer:ib indexBufferOffset:io indirectBuffer:a indirectBufferOffset:ao]; +} +static inline id enc_c_begin(MTLComputePassDescriptor *d) { + id e = d ? [g_cb computeCommandEncoderWithDescriptor:d] : [g_cb computeCommandEncoder]; + if (e) nvn_resource_usage_begin(&g_legacy_use); + legacy_order_begin(e, 2, g_cb); return e; +} +static inline id enc_c_begin_concurrent(id cb) { + id e = [cb computeCommandEncoderWithDispatchType:MTLDispatchTypeConcurrent]; + if (e) nvn_resource_usage_begin(&g_legacy_use); + return e; +} +static inline void enc_c_end(id e) { m4_end(e); } +static inline void enc_c_pso(id e, id p) { [e setComputePipelineState:p]; } +static inline void enc_c_args(id e, uint64_t gpu, NSUInteger i) { NSUInteger off; id b = legacy_buffer_at(gpu, &off); if (gpu && !b) MDIE("【红】Unregistered compute argument address"); [e setBuffer:b offset:off atIndex:i]; } +static inline void enc_c_dispatch(id e, MTLSize n, MTLSize t) { [e dispatchThreadgroups:n threadsPerThreadgroup:t]; } +static inline id enc_b_begin(id cb) { id e = [cb blitCommandEncoder]; legacy_order_begin(e, 3, cb); return e; } +static inline void enc_b_end(id e) { m4_end(e); } +static inline void enc_b_copy_buffer(id e, id s, NSUInteger so, id d, NSUInteger doff, NSUInteger n) { [e copyFromBuffer:s sourceOffset:so toBuffer:d destinationOffset:doff size:n]; } +static inline void enc_b_copy_buffer_texture(id e, id s, NSUInteger so, NSUInteger row, NSUInteger img, MTLSize z, id d, NSUInteger sl, NSUInteger lv, MTLOrigin o) { [e copyFromBuffer:s sourceOffset:so sourceBytesPerRow:row sourceBytesPerImage:img sourceSize:z toTexture:d destinationSlice:sl destinationLevel:lv destinationOrigin:o]; } +static inline void enc_b_copy_texture_buffer(id e, id s, NSUInteger sl, NSUInteger lv, MTLOrigin o, MTLSize z, id d, NSUInteger doff, NSUInteger row, NSUInteger img) { [e copyFromTexture:s sourceSlice:sl sourceLevel:lv sourceOrigin:o sourceSize:z toBuffer:d destinationOffset:doff destinationBytesPerRow:row destinationBytesPerImage:img]; } +static inline void enc_b_copy_texture(id e, id s, NSUInteger sl, NSUInteger lv, MTLOrigin so, MTLSize z, id d, NSUInteger dl, NSUInteger dv, MTLOrigin doo) { [e copyFromTexture:s sourceSlice:sl sourceLevel:lv sourceOrigin:so sourceSize:z toTexture:d destinationSlice:dl destinationLevel:dv destinationOrigin:doo]; } +static void blit_note(void); // Per-pass dump: a copy / buffer update ended the render pass (defined after PD) +// Traditional blits are ordered by the API. Keep range accounting for the +// existing diagnostics; it does not emit Metal 4 barrier messages. +enum { BW_MAX = 64 }; +static uint64_t g_bw_lo[BW_MAX], g_bw_hi[BW_MAX]; static int g_bw_n, g_blit_ops; static uint64_t g_blit_barriers; +static uint64_t g_blit_splits; +uint64_t mtl_blit_splits(void) { return g_blit_splits; } +static int enc_open(void); +static id blit_raw(void) { blit_note(); if (enc_open()) g_blit_splits++; rp_end(); if (!g_blit) { ser_gate(); g_blit = enc_b_begin(g_cb); g_bw_n = 0; g_blit_ops = 0; } return g_blit; } +static void blit_barrier(void) { + g_bw_n = 0; g_blit_ops = 0; g_blit_barriers++; +} +uint64_t mtl_blit_barriers(void) { return g_blit_barriers; } +static int bw_hit(uint64_t lo, uint64_t hi) { for (int i = 0; i < g_bw_n; i++) if (lo < g_bw_hi[i] && g_bw_lo[i] < hi) return 1; return 0; } +// buffer → buffer / fill: reads [rlo, rhi) (0, 0 when nothing is read), writes [wlo, whi) +static id blit_buf(uint64_t rlo, uint64_t rhi, uint64_t wlo, uint64_t whi) { + id e = blit_raw(); + if (g_blit_ops && (g_bw_n == BW_MAX || (rhi > rlo && bw_hit(rlo, rhi)) || bw_hit(wlo, whi))) blit_barrier(); + g_bw_lo[g_bw_n] = wlo; g_bw_hi[g_bw_n++] = whi; g_blit_ops++; + return e; +} +// texture copies / readbacks: barrier if anything came before, and later operations must wait for it too +static id blit(void) { + id e = blit_raw(); + if (g_blit_ops) blit_barrier(); + g_bw_lo[0] = 0; g_bw_hi[0] = ~0ull; g_bw_n = 1; g_blit_ops = 1; // record as "everything written": the next operation always gets a barrier + return e; +} + +void mtl_sub_copy(uint64_t src, uint64_t dst, uint64_t n) { + pend_add(dst, n); + void *sb, *db; uint64_t so, dof; + if (!mtl_addr(src, n, &sb, &so) || !mtl_addr(dst, n, &db, &dof)) + MDIE("缓冲拷贝地址不在任何内存池内 src=%#llx dst=%#llx n=%#llx", (unsigned long long)src, (unsigned long long)dst, (unsigned long long)n); + enc_b_copy_buffer(blit_buf(src, src + n, dst, dst + n), (__bridge id)sb, so, (__bridge id)db, dof, n); +} + +// Inline uniform update (UpdateUniformBuffer): the data is copied into the host's own shared ring at encode time, then copied in order to the destination. +// Previously it was read from command memory only when the GPU executed - if the game recycles command memory before the GPU runs, it reads the bytes of the next batch of commands (SMBW's fountain water wave simulation got the wrong coefficients and diverged to ±65504). +static id g_ubs_buf; static size_t g_ubs_off, g_ubs_size; +// Inline updates inside an open render pass are deferred instead of ending the pass. Metal cannot copy into a buffer inside a render pass, so each update used to end it: all attachments stored, +// reloaded by the next pass, new encoders (Metroid Dread about 246 times a frame, Tears of the Kingdom about 50). NVN applies the update in command order, so the draws after it in the pass must +// read the new data: a draw whose uniform binding overlaps a deferred update reads a shadow made only of the updates deferred in this pass, latest first per byte, and only when they cover the +// whole bound range (then the shadow is exactly what the GPU would read, whatever earlier frames still have in flight). The real copies are encoded in order when the pass ends (rp_end), so later +// passes and the CPU see them as before. The pass still ends before a draw that would need more: a binding the deferred updates cover only in part, or a read of the deferred data through a vertex, +// index, indirect or storage buffer (ubd_conflict). Off in TBDR mode (it reorders passes). DM_NVN_UB_DEFER=0 turns it off +static size_t arena_alloc(size_t n, __strong id *buf); static inline void args_dirty_all(void); +typedef struct { uint64_t lo, hi, gpu; size_t off; __unsafe_unretained id buf; } ubd_ent; // data at buf + off (the ubs ring; a ring block replaced meanwhile lives until this submission completes) +enum { UBD_MAX = 512 }; +static ubd_ent g_ubd[UBD_MAX]; static int g_nubd; +static uint64_t g_ubd_deferred, g_ubd_conflicts; // updates deferred, passes ended before a draw that needed more (the 驻留集 line) +static int ubd_on(void) { static int v = -1; if (v < 0) { const char *e = getenv("DM_NVN_UB_DEFER"); v = !(e && !strcmp(e, "0")); if (!v) MLOG("DM_NVN_UB_DEFER=0:通道内的 uniform 更新照旧结束通道"); } return v && !tbdr_on(); } +void mtl_ubd_stats(uint64_t *deferred, uint64_t *conflicts) { *deferred = g_ubd_deferred; *conflicts = g_ubd_conflicts; } +static int ubd_hits(uint64_t lo, uint64_t hi) { for (int i = 0; i < g_nubd; i++) if (lo < g_ubd[i].hi && g_ubd[i].lo < hi) return 1; return 0; } +// Whether the deferred updates cover [lo, hi) entirely (any order of overlap): the union of their ranges clipped to it, swept from lo +static int ubd_covers(uint64_t lo, uint64_t hi) { + for (uint64_t at = lo; at < hi;) { uint64_t next = at; + for (int i = 0; i < g_nubd; i++) if (g_ubd[i].lo <= at && g_ubd[i].hi > next) next = g_ubd[i].hi; + if (next == at) return 0; at = next; } + return 1; +} +// GPU address of the shadow of uniform range [lo, hi) (ubd_covers holds): one deferred update holding all of it and suitably aligned is used in place; otherwise the updates are laid into the arena in order +static uint64_t ubd_shadow(uint64_t lo, uint64_t hi) { + for (int i = g_nubd - 1; i >= 0; i--) { const ubd_ent *e = &g_ubd[i]; + if (e->lo <= lo && hi <= e->hi) { int later = 0; for (int j = i + 1; j < g_nubd; j++) later |= lo < g_ubd[j].hi && g_ubd[j].lo < hi; + uint64_t g = e->gpu + (lo - e->lo); if (!later && !(g & 15)) return g; break; } } + id ab; size_t o = arena_alloc((size_t)(hi - lo), &ab); uint8_t *d = (uint8_t *)ab.contents + o; + for (int i = 0; i < g_nubd; i++) { const ubd_ent *e = &g_ubd[i]; uint64_t a = e->lo > lo ? e->lo : lo, b = e->hi < hi ? e->hi : hi; + if (a < b) memcpy(d + (a - lo), (const uint8_t *)e->buf.contents + e->off + (a - e->lo), (size_t)(b - a)); } + return legacy_buffer_gpu(ab) + o; +} +// Encode the deferred copies in order (rp_end, once the pass has ended) +static void ubd_flush(void) { + int n = g_nubd; g_nubd = 0; + for (int i = 0; i < n; i++) { const ubd_ent *e = &g_ubd[i]; void *db; uint64_t dof; + if (!mtl_addr(e->lo, e->hi - e->lo, &db, &dof)) continue; + enc_b_copy_buffer(blit_buf(0, 0, e->lo, e->hi), e->buf, e->off, (__bridge id)db, dof, e->hi - e->lo); } +} +// The bytes [lo, lo+n) as the deferred updates leave them (ubd_covers holds): for bindless handles read from uniform memory (ubo_handle) +static void ubd_read(uint64_t lo, size_t n, void *out) { + for (int i = 0; i < g_nubd; i++) { const ubd_ent *e = &g_ubd[i]; uint64_t a = e->lo > lo ? e->lo : lo, b = e->hi < lo + n ? e->hi : lo + n; + if (a < b) memcpy((uint8_t *)out + (a - lo), (const uint8_t *)e->buf.contents + e->off + (a - e->lo), (size_t)(b - a)); } +} +void mtl_sub_update(const void *data, uint64_t dst, uint64_t n) { + if (!n) return; + pend_add(dst, n); + void *db; uint64_t dof; + if (!mtl_addr(dst, n, &db, &dof)) MDIE("uniform 更新目标不在任何内存池内 dst=%#llx n=%#llx", (unsigned long long)dst, (unsigned long long)n); + int defer = enc_open() && g_nubd < UBD_MAX && ubd_on(); + size_t need = (n + 255u) & ~(size_t)255u; // 256-aligned slots: a deferred update is read in place by draws as a uniform buffer + if (!g_ubs_buf || g_ubs_off + need > g_ubs_size) { + if (g_ubs_buf) defer_obj_release(g_ubs_buf); // copies already encoded still reference the old block: release after this submission completes + g_ubs_size = MAX((size_t)1 << 20, need); + g_ubs_buf = [g_dev newBufferWithLength:g_ubs_size options:MTLResourceStorageModeShared]; + if (!g_ubs_buf) MDIE("uniform 更新环分配失败 %#zx", g_ubs_size); + res_add(g_ubs_buf); g_ubs_off = 0; + } + memcpy((uint8_t *)g_ubs_buf.contents + g_ubs_off, data, n); + if (defer) { g_ubd[g_nubd++] = (ubd_ent){dst, dst + n, legacy_buffer_gpu(g_ubs_buf) + g_ubs_off, g_ubs_off, g_ubs_buf}; g_ubd_deferred++; args_dirty_all(); } // see ubd_shadow + else enc_b_copy_buffer(blit_buf(0, 0, dst, dst + n), g_ubs_buf, g_ubs_off, (__bridge id)db, dof, n); + g_ubs_off += need; +} + +// In-order buffer fill within the submission: all four bytes equal → fillBuffer; otherwise copied in chunks from a 64 KiB pattern buffer +void mtl_sub_fill(uint64_t dst, uint64_t n, uint32_t value) { + if (!n) return; + pend_add(dst, n); + void *db; uint64_t dof; + if (!mtl_addr(dst, n, &db, &dof)) MDIE("缓冲填充地址不在任何内存池内 dst=%#llx n=%#llx", (unsigned long long)dst, (unsigned long long)n); + uint8_t b0 = (uint8_t)value; + if (value == (uint32_t)b0 * 0x01010101u) { [blit_buf(0, 0, dst, dst + n) fillBuffer:(__bridge id)db range:NSMakeRange(dof, n) value:b0]; return; } + enum { PAT = 0x10000 }; static __strong id pat; static uint32_t pat_v; static int pat_ok; + if (!pat_ok || pat_v != value) { uint32_t *tmp = malloc(PAT); for (unsigned i = 0; i < PAT / 4; i++) tmp[i] = value; + pat = [g_dev newBufferWithBytes:tmp length:PAT options:MTLResourceStorageModeShared]; free(tmp); pat_v = value; pat_ok = 1; res_add(pat); } + for (uint64_t off = 0; off < n; off += PAT) { uint64_t k = n - off < PAT ? n - off : PAT; enc_b_copy_buffer(blit_buf(0, 0, dst + off, dst + off + k), pat, 0, (__bridge id)db, dof + off, k); } +} + +static void post(post_op o) { + if (g_npost == g_postcap) { g_postcap = g_postcap ? g_postcap * 2 : 64; g_post = realloc(g_post, g_postcap * sizeof *g_post); } + g_post[g_npost++] = o; +} +void mtl_sub_event(volatile uint32_t *val, uint32_t mode, uint32_t v) { post((post_op){1, val, mode, v, 0, 0, NULL}); } +void mtl_sub_counter(uint64_t addr, uint32_t type) { post((post_op){2, NULL, 0, 0, addr, type, NULL}); } +void mtl_counter_stats(uint64_t rep[32], uint64_t rst[32]) { memcpy(rep, g_cnt_report, sizeof g_cnt_report); memcpy(rst, g_cnt_reset, sizeof g_cnt_reset); } + +// Counter timestamps (19.2 MHz) are taken from the guest clock (hle_os.c guest_ns, the same source as svc GetSystemTick / cntpct): on hardware GPU timestamps and the CPU counter share one timebase, and games compare them. +// They used to come from CLOCK_MONOTONIC, which keeps running while the Mac sleeps while the guest clock (mach_absolute_time) does not, so the two differed by the sleep time; once timestamps were converted to ns, Mario halved its frame rate. In graphics unit tests guest_ns is a weak definition returning 0; fall back to the monotonic clock +uint64_t guest_ns(void); +static uint64_t gpu_ticks(void) { uint64_t g = guest_ns(); if (!g) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); g = (uint64_t)t.tv_sec * 1000000000ull + t.tv_nsec; } return g * 12 / 625; } + +// ---------- Boot instrumentation (DM_BOOTPROF=1): metallib loading, pipeline creation (both happen synchronously during replay on the submit thread), texture upload, first-frame present ---------- +static uint64_t bp_now(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (uint64_t)t.tv_sec * 1000000000ull + t.tv_nsec; } +static uint64_t g_bp_t0; // Time of process start (when this module is loaded) +__attribute__((constructor)) static void bp_t0(void) { g_bp_t0 = bp_now(); } +static int bootprof(void) { static int v = -1; if (v < 0) v = getenv("DM_BOOTPROF") != NULL; return v; } +static uint64_t g_bp_lib_n, g_bp_lib_ns, g_bp_pso_n, g_bp_pso_ns, g_bp_up_bytes, g_bp_first_present; +static uint64_t g_up_total_ns; // Cumulative upload time (same source as the per-frame @upload_ms) +// ---------- Per-frame GPU time aggregation ---------- +#define GRING 1024 +static struct { _Atomic uint64_t frame, ns, t0, t1; _Atomic int pending; } g_gpu[GRING]; // t0 / t1: the frame's earliest GPUStart / latest GPUEnd (mach absolute time, ns) +static _Atomic uint64_t g_cur_frame; +// ---------- Same-process alternating measurement (DM_NVN_UB, off by default: without the environment variable the code below only adds one 'is it enabled' check) ---------- +// On a hot real device the system keeps adjusting power / frequency, and segment comparison (segments A and B each run for a minute) cannot resolve differences of <10%; within the same run, toggle by phase in turn and take differences of adjacent phase pairs, so the drift cancels. +// DM_NVN_UB="name=item,item;name=item", one phase every DM_NVN_UB_N frames (default 180, minimum 60): even phases = baseline (nothing changed), odd phases take one category in turn. Each item of a category: +// @set:= on entering the phase set this int global variable to the value, on leaving restore it (address found by symbol name; switches to be used on a real device must have a line added to the registration table below) +// or * the phase drops all draws of these fragment shaders (the picture will be wrong; it only measures 'how much this kind of draw is worth'; a nearly idle category needs a round of its own) +// Readings: each phase discards its first 45 frames (transition + present lag); for the remaining frames, the mean of the actual present interval (same source as perf.txt) and the frame counts in the three buckets 16.7 / 25 / ≥33 ms, one log line 「UB 上屏」(UB present). +// Table output: $DM_ROOT/run/ruler/ub_table.py; real-device script: $DM_ROOT/run/ruler/ub_alt.sh. +static int g_ub_n = -1, g_ub_N = 180; static _Atomic int g_ub_cur = -1; static char g_ub_name[16][16], g_ub_keys[16][16][48]; static int g_ub_nk[16]; +static _Atomic int g_ubp_on, g_ubp_c[4]; static _Atomic uint64_t g_ubp_sum_us; +void mtl_ub_present(double iv) { // Called once per frame by the present thread: actual present interval in ms (−1 = dropped, 0 = no reading) + if (!atomic_load(&g_ubp_on) || iv == 0) return; + if (iv < 0) { atomic_fetch_add(&g_ubp_c[3], 1); return; } + atomic_fetch_add(&g_ubp_c[iv <= 20.8 ? 0 : iv <= 29.2 ? 1 : 2], 1); atomic_fetch_add(&g_ubp_sum_us, (uint64_t)(iv * 1000)); +} +// Registered runtime switches: name → address. Main-program symbols of an iOS release build are not in the dynamic symbol table and dlsym cannot find them (in the first real-device round all four categories were 「找不到全局变量」(global variable not found) and the whole round was wasted), +// so a switch to be measured on a real device must have a line added here; on Mac / in unit tests unregistered ones are still looked up by symbol name. +static int *ub_var(const char *nm) { + extern int g_bloom_q, g_dm_present_link; + static const struct { const char *n; int *p; } t[] = { {"g_bloom_q", &g_bloom_q}, {"g_dm_present_link", &g_dm_present_link}, {"g_frz_on", &g_frz_on} }; + for (size_t i = 0; i < sizeof t / sizeof t[0]; i++) if (!strcmp(nm, t[i].n)) return t[i].p; + return dlsym(RTLD_DEFAULT, nm); +} +static void ub_frame(uint64_t f) { + if (g_ub_n < 0) { g_ub_n = 0; const char *e = getenv("DM_NVN_UB"), *n = getenv("DM_NVN_UB_N"); if (n && atoi(n) >= 60) g_ub_N = atoi(n); + for (const char *q = e; q && *q && g_ub_n < 16; ) { const char *eq = strchr(q, '='), *end = strchr(q, ';'); if (!end) end = q + strlen(q); if (!eq || eq > end) break; + int c = g_ub_n++; size_t l = (size_t)(eq - q); if (l > 15) l = 15; memcpy(g_ub_name[c], q, l); + for (const char *k = eq + 1; k < end && g_ub_nk[c] < 16; ) { size_t kl = 0; while (k + kl < end && k[kl] != ',') kl++; size_t cl = kl > 47 ? 47 : kl; memcpy(g_ub_keys[c][g_ub_nk[c]++], k, cl); k += kl; if (k < end) k++; } + q = *end ? end + 1 : end; } + if (g_ub_n) MLOG("DM_NVN_UB:%d 类,每相 %d 帧(同进程交替测量;丢绘制的相画面不对)", g_ub_n, g_ub_N); } + if (!g_ub_n) return; + uint64_t ph = f / (uint64_t)g_ub_N, r = f % (uint64_t)g_ub_N; + if (r == 45) { for (int i = 0; i < 4; i++) atomic_store(&g_ubp_c[i], 0); atomic_store(&g_ubp_sum_us, 0); atomic_store(&g_ubp_on, 1); } + if (r != 0) return; + if (f && atomic_load(&g_ubp_on)) { atomic_store(&g_ubp_on, 0); uint64_t pp = ph - 1; int pc = (pp & 1) ? (int)((pp / 2) % (uint64_t)g_ub_n) : -1; + int c0 = atomic_load(&g_ubp_c[0]), c1 = atomic_load(&g_ubp_c[1]), c2 = atomic_load(&g_ubp_c[2]), c3 = atomic_load(&g_ubp_c[3]), n_ = c0 + c1 + c2; + MLOG("UB 上屏 %llu %s 帧 %d 均 %.3f ms 2拍 %d 3拍 %d 4拍以上 %d 丢弃 %d", (unsigned long long)pp, pc < 0 ? "base" : g_ub_name[pc], n_, n_ ? atomic_load(&g_ubp_sum_us) / 1000.0 / n_ : 0.0, c0, c1, c2, c3); } + int nc = (ph & 1) ? (int)((ph / 2) % (uint64_t)g_ub_n) : -1; + static int *sp[16]; static int sv[16], ns; // Variables set by the previous phase: restore first + for (int i = 0; i < ns; i++) *sp[i] = sv[i]; ns = 0; + for (int i = 0; nc >= 0 && i < g_ub_nk[nc] && ns < 16; i++) { const char *k = g_ub_keys[nc][i]; if (strncmp(k, "@set:", 5)) continue; + char nm[48]; const char *eq = strchr(k, '='); if (!eq) continue; size_t l = (size_t)(eq - (k + 5)); if (l >= sizeof nm) continue; memcpy(nm, k + 5, l); nm[l] = 0; + int *a_ = ub_var(nm); static int bad, ok; + if (!a_) { if (bad++ < 8) MLOG("【红】DM_NVN_UB:找不到全局变量 %s(真机上要先在 ub_var 的表里登记)", nm); continue; } + sp[ns] = a_; sv[ns] = *a_; ns++; *a_ = atoi(eq + 1); if (ok++ < 8) MLOG("DM_NVN_UB:%s = %d(原值 %d)", nm, atoi(eq + 1), sv[ns - 1]); } + atomic_store(&g_ub_cur, nc); +} +static int ub_skip(const char *key) { // Whether this phase should drop draws of this fragment shader + int uc = atomic_load(&g_ub_cur); if (uc < 0) return 0; + for (int i = 0; i < g_ub_nk[uc]; i++) { const char *k = g_ub_keys[uc][i]; if (k[0] == '@') continue; if (k[0] == '*' || !strncmp(key, k, strlen(k))) return 1; } + return 0; +} +void mtl_set_frame(uint64_t f) { atomic_store(&g_cur_frame, f); if (g_ub_n) ub_frame(f); } +// GPU timing registration: gpu_tag_begin(frame) before commit, gpu_tag_done(frame, GPU start, GPU end) on completion (seconds on the CACurrentMediaTime base). +// Old-queue command buffers go through gpu_tag_f; the Metal 4 main submission backfills in the feedback of mtl_sub_commit (when switching to Metal 4 all the call sites were lost and the GPU column of the 「计时」(timing) line became empty) +static void gpu_tag_begin(uint64_t f) { + unsigned k = f % GRING; + if (atomic_load(&g_gpu[k].frame) != f) { atomic_store(&g_gpu[k].frame, f); atomic_store(&g_gpu[k].ns, 0); atomic_store(&g_gpu[k].pending, 0); atomic_store(&g_gpu[k].t0, UINT64_MAX); atomic_store(&g_gpu[k].t1, 0); } + atomic_fetch_add(&g_gpu[k].pending, 1); +} +static void gpu_tag_done(uint64_t f, double g0, double g1) { + unsigned k = f % GRING; + if (atomic_load(&g_gpu[k].frame) != f) return; + double d = g1 - g0; + if (d > 0) { + atomic_fetch_add(&g_gpu[k].ns, (uint64_t)(d * 1e9)); + uint64_t s0 = (uint64_t)(g0 * 1e9), s1 = (uint64_t)(g1 * 1e9), v; + for (v = atomic_load(&g_gpu[k].t0); s0 < v && !atomic_compare_exchange_weak(&g_gpu[k].t0, &v, s0);) {} + for (v = atomic_load(&g_gpu[k].t1); s1 > v && !atomic_compare_exchange_weak(&g_gpu[k].t1, &v, s1);) {} + } + atomic_fetch_sub(&g_gpu[k].pending, 1); +} +static void gpu_tag_f(id cb, uint64_t f) { gpu_tag_begin(f); [cb addCompletedHandler:^(id c) { gpu_tag_done(f, c.GPUStartTime, c.GPUEndTime); }]; } +void gpu_tag(id cb) { gpu_tag_f(cb, atomic_load(&g_cur_frame)); } +double mtl_gpu_ms(uint64_t f) { + unsigned k = f % GRING; + if (atomic_load(&g_gpu[k].frame) != f || atomic_load(&g_gpu[k].pending)) return -1; + return atomic_load(&g_gpu[k].ns) / 1e6; +} +int mtl_gpu_span(uint64_t f, uint64_t *t0, uint64_t *t1) { + unsigned k = f % GRING; + if (atomic_load(&g_gpu[k].frame) != f || atomic_load(&g_gpu[k].pending)) return 0; + *t0 = atomic_load(&g_gpu[k].t0); *t1 = atomic_load(&g_gpu[k].t1); + return *t1 >= *t0 && *t1; +} +static void rp_flush(void); +static const char *cctx_snap(void); // What is in this submission (printed into the failure line on error; defined before mtl_exec) +uint64_t mtl_sub_commit(void) { + defer_fallback(); // Deferred merged pass: at the end of the submission there are still P draws not replayed (the normal path never gets here) + rp_flush(); + if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + up_flush(); + uint64_t fence = defer_submission_prepare_commit(); + if (g_vis_ring_i >= 0) g_vis_ring[g_vis_ring_i].fence = fence; + // CPU-visible effects after completion: copy the op table into the callback + int n = g_npost; post_op *ops = n ? malloc(n * sizeof *ops) : NULL; + if (n) memcpy(ops, g_post, n * sizeof *ops); + NSArray *commit_blocks = [g_commit_blocks copy]; + __block NSArray> *sampler_refs = g_sub_samplers.allObjects; + + [g_cb addCompletedHandler:^(id f) { (void)f; for (dispatch_block_t block in commit_blocks) block(); sampler_refs = nil; }]; + uint64_t gpu_f = atomic_load(&g_cur_frame); gpu_tag_begin(gpu_f); + NSString *cctx = @(cctx_snap()); + NSString *sample_notes = [g_sub_sample_notes copy]; + NSArray *argument_checks = [g_sub_argument_checks copy]; + [g_cb addCompletedHandler:^(id cb) { + // The diagnostic reads run in this callback before g_done advances, + // so an arena cannot be legitimately recycled before the comparison. + for (dispatch_block_t check in argument_checks) check(); + log_function_errors(cb, "replay", fence); + gpu_tag_done(gpu_f, cb.GPUStartTime, cb.GPUEndTime); + if (cb.error && sample_notes.length) { static int notes; if (notes++ < 3) MLOG("[sample-trace] failed fence=%llu\n%s", (unsigned long long)fence, sample_notes.UTF8String); } + if (cb.error) { atomic_store(&g_last_commit_error, 1); static int n, nv; int victim = [cb.error.description containsString:@"InnocentVictim"]; if (victim ? nv++ < 1 : n++ < 8) MLOG("【红】命令缓冲错误详情 fence=%llu:%s | GPU %.3f→%.3f", (unsigned long long)fence, cb.error.description.UTF8String, cb.GPUStartTime, cb.GPUEndTime); if (!victim) { MLOG("【红】出错那次提交的现场 fence=%llu:%s", (unsigned long long)fence, cctx.UTF8String); log_encoder_errors(cb.error, fence); } MLOG("【红】命令缓冲执行出错:%s", cb.error.localizedDescription.UTF8String); } + for (int i = 0; i < n; i++) { + if (ops[i].kind == 1) { if (ops[i].mode == 1) *ops[i].val += ops[i].v; else *ops[i].val = ops[i].v; } + else if (ops[i].kind == 3) { // Samples passed: the sum of all slots since the reset + vis_list *l = ops[i].vis; uint64_t sum = 0; + for (int k = 0; k < l->n; k++) { sum += ((const uint64_t *)((__bridge id)l->s[k].buf).contents)[l->s[k].slot]; CFRelease(l->s[k].buf); } + if (!sum && vis_force_visible()) sum = 1; + free(l); uint64_t rec[2] = {sum, gpu_ticks()}; memcpy((void *)ops[i].addr, rec, sizeof rec); + } + else { uint64_t t = gpu_ticks(), rec[2] = {(ops[i].type == 0 || ops[i].type == 0x10) && !DM_GAME_RENDER_COND ? t : 0, t}; memcpy((void *)ops[i].addr, rec, sizeof rec); } + } + free(ops); + pthread_mutex_lock(&g_mu); if (fence > g_done) g_done = fence; pthread_cond_broadcast(&g_cv); pthread_mutex_unlock(&g_mu); + defer_drain(); + }]; + void arena_commit(uint64_t fence); arena_commit(fence); + res_commit(); + [g_cb encodeSignalEvent:g_ev value:fence]; + [g_cb commit]; + // Registration threads can now linearize after a real queue commit. Keep + // active=true until this point so a concurrently retired replay view is + // never assigned to an uncommitted/current fence by a stale observation. + defer_submission_did_commit(fence); + g_alloc_fence[g_alloc_cur] = fence; + g_cb = nil; + g_defer_cut_active = 0; + g_commit_blocks = nil; + g_sub_argument_checks = nil; + objc_autoreleasePoolPop(g_sub_pool); g_sub_pool = NULL; + return fence; +} + +uint64_t mtl_last_fence(void) { return g_fence; } +int mtl_last_commit_error(void) { return atomic_load(&g_last_commit_error); } + +int mtl_wait(uint64_t fence, uint64_t timeout_ns) { + struct timespec dl; clock_gettime(CLOCK_REALTIME, &dl); + uint64_t ns = (uint64_t)dl.tv_nsec + (timeout_ns > 3600000000000ull ? 3600000000000ull : timeout_ns); + dl.tv_sec += ns / 1000000000ull; dl.tv_nsec = ns % 1000000000ull; + pthread_mutex_lock(&g_mu); + int r = 0; + while (g_done < fence) if ((r = pthread_cond_timedwait(&g_cv, &g_mu, &dl))) break; + int ok = g_done >= fence; + pthread_mutex_unlock(&g_mu); + return ok; +} + +static void mtl_finish_gpu(void) { + if (g_fence) mtl_wait(g_fence, UINT64_MAX); + up_wait_zero(&g_up_pending); +} +void mtl_present_flush(void); +// nvnQueueFinish waits for the GPU work submitted so far, not for queued presents to reach the screen: with a window, the display link takes one present per tick, so waiting for them +// made every QueueFinish cost one or two display frames. Fire Emblem: Three Houses' loader submits a copy and calls QueueFinish for each resource it uploads, and its first loading screen +// took 78 s on the iPhone (5 s windowless on the Mac). A swapchain texture is not handed back before its previous present is processed (AcquireTexture waits for that ticket) +static NSString *pso_user_path(void) { + const char *u = getenv("DM_PSO_USER"), *save = getenv("DM_SAVE"); + if (u && *u) return @(u); + return save && *save ? [[@(save) stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"pso_user.tsv"] : nil; +} +// The current generic host no longer ships the older fork's private PSO-learning +// helper. Keep its public counters inert while the Metal 3 backend is brought +// forward; on-demand pipeline creation remains functional. +typedef struct { uint64_t attempted, accepted, written, dropped, failed; } pso_learning_stats; +static inline pso_learning_stats pso_learning_snapshot(void) { return (pso_learning_stats){0}; } +static inline void pso_learning_drain_timeout(unsigned ms) { (void)ms; } +static inline void pso_learning_start(void) {} +static inline void pso_user_record(const char *fmt, ...) { (void)fmt; } + +void mtl_pso_user_stats(uint64_t *attempted, uint64_t *accepted, uint64_t *written, uint64_t *dropped, uint64_t *failed) { + pso_learning_stats s = pso_learning_snapshot(); + if (attempted) *attempted = s.attempted; + if (accepted) *accepted = s.accepted; + if (written) *written = s.written; + if (dropped) *dropped = s.dropped; + if (failed) *failed = s.failed; +} + +void mtl_pso_user_drain(void) { + pso_learning_drain_timeout(100); +} +void mtl_finish(void) { mtl_finish_gpu(); } +static void present_drop_for_suspend(void); +void mtl_drain(void) { pso_learning_drain_timeout(100); present_drop_for_suspend(); mtl_present_flush(); mtl_init(); id cb = q_cb(); [cb commit]; [cb waitUntilCompleted]; mtl_finish_gpu(); } + +// ================= Render replay ================= +#include "../../host/front.h" +static int g_red_once[128]; +#define RED_ONCE(k, ...) do { if (!g_red_once[k]++) MLOG("[RED] " __VA_ARGS__); } while (0) +// NVN vertex streams 0–15 → Metal vertex buffer slots: 0–13 go to 16–29 (30 is the constant fallback buffer), 14 and 15 to 14 and 15. Metal has only the 31 slots 0–30; translated shaders use only buffer(0) (the argument buffer), +// and the host's own vertex functions read no vertex buffers. 13 Sentinels uses stream 15, and the old 16 + stream went out of range (Metal assertion: buffer index (31) must be < 31) +#define VB_SLOT(st) ((st) < 14 ? 16 + (st) : (st)) + +// ---------- Texture → MTLTexture ---------- +static int fmt_compressed(int f) { return (f >= 0x41 && f <= 0x50) || (f >= 0x79 && f <= 0x94); } +static int fmt_depth(int f) { return f >= 0x31 && f <= 0x36; } +typedef struct { MTLPixelFormat a, b; } pfv_pair; +// Metal's BC enum names are SDK-gated to iOS 16.4 even though the numeric +// pixel-format ABI is stable. The 16.1 build never creates them: no_bc() +// routes every BC upload through nvn_bcdec first. +enum { + DM_MTL_BC1 = 130, DM_MTL_BC1_SRGB = 131, + DM_MTL_BC2 = 132, DM_MTL_BC2_SRGB = 133, + DM_MTL_BC3 = 134, DM_MTL_BC3_SRGB = 135, + DM_MTL_BC7 = 152, DM_MTL_BC7_SRGB = 153, + DM_MTL_ASTC4_SRGB = 186, DM_MTL_ASTC4 = 204, +}; +static const pfv_pair g_pfv_srgb[] = { + {MTLPixelFormatRGBA8Unorm, MTLPixelFormatRGBA8Unorm_sRGB}, + {MTLPixelFormatBGRA8Unorm, MTLPixelFormatBGRA8Unorm_sRGB}, + {(MTLPixelFormat)DM_MTL_BC1, (MTLPixelFormat)DM_MTL_BC1_SRGB}, + {(MTLPixelFormat)DM_MTL_BC2, (MTLPixelFormat)DM_MTL_BC2_SRGB}, + {(MTLPixelFormat)DM_MTL_BC3, (MTLPixelFormat)DM_MTL_BC3_SRGB}, + {(MTLPixelFormat)DM_MTL_BC7, (MTLPixelFormat)DM_MTL_BC7_SRGB}, + {(MTLPixelFormat)DM_MTL_ASTC4, (MTLPixelFormat)DM_MTL_ASTC4_SRGB}, +}; +static int tex_need_pfv(MTLPixelFormat base, MTLPixelFormat view) { + if (base == view) return 0; + for (size_t i = 0; i < sizeof g_pfv_srgb / sizeof *g_pfv_srgb; i++) + if ((base == g_pfv_srgb[i].a && view == g_pfv_srgb[i].b) || (base == g_pfv_srgb[i].b && view == g_pfv_srgb[i].a)) return 0; + return 1; +} +// Cache tables on the replay hot path always use nvn_hmap (C hash, keys not boxed); the table stores CFBridgingRetain'ed objects, CFRelease'd on replace / delete +static void hm_rel(void *v) { CFRelease(v); } +static __unsafe_unretained id hm_get_id(const nvn_hmap *m, const void *k) { return (__bridge id)nvn_hm_get(m, k); } +static void hm_put_id(nvn_hmap *m, const void *k, id o) { void *old = nvn_hm_put(m, k, (void *)CFBridgingRetain(o)); if (old) CFRelease(old); } +static void hm_del_id(nvn_hmap *m, const void *k) { void *old = nvn_hm_del(m, k); if (old) CFRelease(old); } +static nvn_hmap g_by_serial = {.klen = 8}; // Serial → MTLTexture / NvPkg / NvProg +static pthread_mutex_t g_smu = PTHREAD_MUTEX_INITIALIZER; +// Serial table generation: +1 when entries are added / removed or a packaged texture is replaced. Downstream caches (texture views, programs) record the generation at fetch time; if it is unchanged they can be used directly without another table lookup +static _Atomic uint64_t g_ser_gen = 1; +// Textures shared by storage (non-packaged): key = memory pool + offset + texture description. An NVN texture object is only a descriptor; an object rebuilt over the same storage with the same description is the same texture; +// SMBW rebuilds its transient render targets on new objects every frame and never Finalizes them, so building one per serial leaked about 13 MB of GPU memory per frame. Textures here are kept resident by the sharing table, +// and when the serial table replaces / deletes them it only drops its reference (defer_obj_unref), never removing them from the residency set. The sharing table does not evict (entries = distinct storage + description combinations). +typedef struct { uint64_t pool; int64_t off; nvn_texdesc d; } alias_key; +static nvn_hmap g_alias = {.klen = sizeof(alias_key)}; +static CFMutableSetRef g_alias_set; // Textures in the sharing table (non-owning; used to ask "is this shared") +static uint64_t g_alias_evicted; // shared textures evicted on pool Finalize so far (for the memory report) +static int alias_has(id o) { return g_alias_set && o && CFSetContainsValue(g_alias_set, (__bridge const void *)o); } +// On memory pool Finalize, evict the textures built on that pool from the sharing table (collect the keys first, then delete: deletion shifts entries back). The sharing table used not to evict: Mario's pools stay for good, so entries are bounded; +// Engage frees pools on scene changes and rebuilds on new ones, so the old pools' render targets and other private textures stayed in the table forever (about 0.8 GB more GPU memory over 850 s of the title demo), and linear textures built directly on a pool also kept the whole pool buffer alive. +// Also, when a pool object's address is reused, old entries would hand a texture on the old storage to the new pool's texture at the same offset and description. Evicted textures go through deferred release; if the serial table still holds one, that is just one more reference +static void treg_drop_pool_views(const void *pool); +void mtl_alias_drop_pool(const void *pool) { + if (!pool) return; + pthread_mutex_lock(&g_smu); + uint32_t n = 0; alias_key *ks = NULL; + for (uint32_t i = 0; i < g_alias.cap; i++) { + if (!g_alias.vals[i]) continue; + alias_key k; memcpy(&k, g_alias.keys + (size_t)i * g_alias.klen, sizeof k); + if (k.pool != (uint64_t)(uintptr_t)pool) continue; + alias_key *nk = realloc(ks, (n + 1) * sizeof *ks); if (!nk) break; ks = nk; ks[n++] = k; + } + for (uint32_t j = 0; j < n; j++) { + void *old = nvn_hm_del(&g_alias, &ks[j]); + if (!old) continue; + id o = CFBridgingRelease(old); + if (g_alias_set) CFSetRemoveValue(g_alias_set, (__bridge const void *)o); + defer_obj_release(o); + } + if (n) { g_ser_gen++; g_alias_evicted += n; } + pthread_mutex_unlock(&g_smu); + free(ks); + treg_drop_pool_views(pool); // outside g_smu: the texture ID registry lock g_rmu is taken before g_smu +} +static __unsafe_unretained id serial_get(uint64_t k) { pthread_mutex_lock(&g_smu); __unsafe_unretained id o = hm_get_id(&g_by_serial, &k); pthread_mutex_unlock(&g_smu); return o; } +static void serial_put(uint64_t k, id o) { pthread_mutex_lock(&g_smu); id old = hm_get_id(&g_by_serial, &k); if (old) { if (alias_has(old)) defer_obj_unref(old); else defer_obj_release(old); } hm_put_id(&g_by_serial, &k, o); g_ser_gen++; pthread_mutex_unlock(&g_smu); } +static void serial_del(uint64_t k) { pthread_mutex_lock(&g_smu); id old = hm_get_id(&g_by_serial, &k); if (old) { if (alias_has(old)) defer_obj_unref(old); else defer_obj_release(old); hm_del_id(&g_by_serial, &k); g_ser_gen++; } pthread_mutex_unlock(&g_smu); } // nothing to drop: the texture ID caches stay valid +// Texture serials owned by a live NVN texture object (TextureInitialize until Finalize or re-initialization, hle_nvn.c). Command records and texture pool registrations snapshot the +// texture object (tex_snap in hle_nvn.c, mtl_reg_tex), so replay can resolve a texture the game has already finalized: Breath of the Wild records each frame's transient render targets +// (the HUD captures, the 640x360 half-resolution buffers) and finalizes their objects before it submits. Those serials went back into g_by_serial and stayed there for good, as no second +// Finalize comes: about 10 a frame, 200,000 after an hour in the first shrine. A dead serial resolves through g_ghost instead, a small table whose oldest entry goes when a new one comes, +// so a record replayed again soon still finds its texture +enum { NGHOST = 512 }; +static nvn_hmap g_tex_live = {.klen = 8}; // serial → (void *)1, under g_smu +static nvn_hmap g_ghost = {.klen = 8}; static uint64_t g_ghost_ring[NGHOST]; static unsigned g_ghost_next; static uint64_t g_ghost_puts; +void mtl_tex_init(const tex_t *t) { if (!t->serial) return; pthread_mutex_lock(&g_smu); nvn_hm_put(&g_tex_live, &t->serial, (void *)1); pthread_mutex_unlock(&g_smu); } +static void obj_drop(id o) { if (alias_has(o)) defer_obj_unref(o); else defer_obj_release(o); } // caller holds g_smu +static __unsafe_unretained id tex_get(uint64_t k) { + pthread_mutex_lock(&g_smu); __unsafe_unretained id o = hm_get_id(&g_by_serial, &k); if (!o && g_ghost.n) o = hm_get_id(&g_ghost, &k); pthread_mutex_unlock(&g_smu); return o; +} +static void tex_put(uint64_t k, id o) { // serials from 2^62 up are external textures (mtl_ext_tex), never initialized by the game + pthread_mutex_lock(&g_smu); + nvn_hmap *m = k >= (1ull << 62) || nvn_hm_get(&g_tex_live, &k) ? &g_by_serial : &g_ghost; + id old = hm_get_id(m, &k); + if (old) obj_drop(old); + else if (m == &g_ghost) { + uint64_t *slot = &g_ghost_ring[g_ghost_next++ % NGHOST]; + if (*slot) { id ev = hm_get_id(&g_ghost, slot); if (ev) obj_drop(ev); hm_del_id(&g_ghost, slot); } + *slot = k; g_ghost_puts++; + } + hm_put_id(m, &k, o); g_ser_gen++; + pthread_mutex_unlock(&g_smu); +} +// Host record of a packaged texture: the current MTLTexture, the upload stamp, and the global write generation at the last check +// spare: the texture the last re-upload replaced, kept resident and written by the next re-upload once the GPU has passed spare_fence (a texture the CPU rewrites every frame, a movie frame, +// alternates between two textures instead of allocating one per frame: Link's Awakening's title movie made and freed an 8 MB texture a frame) +@interface NvPkg : NSObject { @public id tex, spare; uint64_t stamp, seen, hash, spare_fence; const void *span; } @end // hash: full hash of the uploaded content under DM_NVN_DIRTYCHECK; span: its tracked range (nvn_dirty_span, NULL = untracked) +@implementation NvPkg +- (void)dealloc { res_remove(tex); if (spare) res_remove(spare); } +@end +static uint64_t g_pkg_uploads, g_pkg_reuploads, g_pkg_miss; +static uint64_t g_pfv_upgrades; +// DM_NVN_DIRTYCHECK=1 (diagnostic, compared against ground truth): fully hash the pool content on every use; changed but not detected by dirty-page tracking = a missed detection +static int dirtycheck(void) { static int v = -1; if (v < 0) v = getenv("DM_NVN_DIRTYCHECK") != NULL; return v; } +static uint64_t full_hash(const uint8_t *m, uint64_t n) { uint64_t h = 1469598103934665603ull; for (uint64_t i = 0; i < n; i++) h = (h ^ m[i]) * 1099511628211ull; return h; } +#define NVN_UPDIAG_LOG MLOG +#include "nvn_upload_diag.h" +#undef NVN_UPDIAG_LOG +#define NVN_CPU_CACHE_LOG MLOG +#include "nvn_cpu_decode_cache.h" +#undef NVN_CPU_CACHE_LOG +// The GPU does not support BC compressed formats (A15 / A16 etc.). BC1-3 are +// transcoded block-for-block to ASTC 4x4; the remaining BC formats are created +// in decompressed form. DM_TEST_NO_BC=1 forces this path on any machine. +static int no_bc(void) { + static int v = -1; + if (v < 0) { const char *e = getenv("DM_TEST_NO_BC"); +#if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED < 160400 + v = 1; +#else + v = (e && *e && strcmp(e, "0")) || ![g_dev supportsBCTextureCompression]; +#endif + if (v) MLOG("GPU BC compression is unavailable%s: transcode BC1-3 to ASTC 4x4 and decompress the remaining BC formats", + e && *e && strcmp(e, "0") ? " (forced by DM_TEST_NO_BC)" : ""); } + return v; +} +static int bc_unpack(uint32_t nvn_fmt, uint32_t *pf, int *bpp) { return no_bc() && nvn_bc_unpacked(nvn_fmt, pf, bpp); } +// BC1/2/3 use 4x4 blocks just like ASTC 4x4. The runtime transcoder replaces +// one source block with one valid ASTC block, retaining compressed residency. +// 3D and multisample textures keep the proven decompression path. +static int no_bc_astc_mode(void) { + static int v = -1; + if (v < 0) { + const char *e = getenv("DM_NO_BC_ASTC"); + v = !e || !*e || !strcmp(e, "all") ? 2 : (!strcmp(e, "bc1") ? 1 : 0); + MLOG("No-BC storage: DM_NO_BC_ASTC=%s (%s)", e && *e ? e : "all", + v == 2 ? "keep BC1/2/3 compressed as ASTC" : v == 1 ? "keep only BC1 compressed as ASTC" : "decompress all BC formats for comparison"); + } + return v; +} +static int bc_astc(const tex_t *t, uint32_t *pf) { + if (!no_bc()) return 0; + uint32_t apf; int kind, mode = no_bc_astc_mode(); + if (!mode || !nvn_bc_astc_fmt((uint32_t)t->d.format, &apf, &kind) || (mode == 1 && kind != 1)) return 0; + if (t->d.target == NVN_TEXTURE_TARGET_3D || t->d.samples > 1) return 0; + *pf = apf; + return 1; +} +static int is_astc4(MTLPixelFormat f) { return f == (MTLPixelFormat)DM_MTL_ASTC4 || f == (MTLPixelFormat)DM_MTL_ASTC4_SRGB; } +static int is_native_bc(MTLPixelFormat f) { return f >= (MTLPixelFormat)DM_MTL_BC1 && f <= (MTLPixelFormat)DM_MTL_BC7_SRGB; } +static void tex_tag_format(id mt, uint32_t nvn_fmt); +static void tex_format_stat(void); +static MTLTextureDescriptor *tex_desc(const tex_t *t, int packaged) { + uint32_t pf = 0; uint8_t swz[4]; + if (!nvn_fmt_to_mtl(t->d.format, &pf, swz) || !pf) { RED_ONCE(0, "贴图格式 %#x Metal 无对应(首例)", t->d.format); return nil; } + int unp = 0, astc = 0; { uint32_t upf; int ubpp; if (bc_astc(t, &upf)) { pf = upf; astc = 1; } else if (bc_unpack(t->d.format, &upf, &ubpp)) { pf = upf; unp = 1; } } + // Metal's limits: 16384 in 2D, 2048 in 3D, 2048 array layers; beyond them Metal aborts the process on an assertion, so they are caught here with a red line + if (t->d.width > 16384 || t->d.height > 16384 || t->d.depth > 2048 || t->d.levels > 15) { RED_ONCE(71, "贴图尺寸超出 Metal 上限:%u×%u×%u 级 %u 格式 %#x 目标 %d(首例 ser=%llu)", t->d.width, t->d.height, t->d.depth, t->d.levels, t->d.format, t->d.target, (unsigned long long)t->serial); return nil; } + MTLTextureDescriptor *d = [MTLTextureDescriptor new]; + d.pixelFormat = (MTLPixelFormat)pf; + d.width = t->d.width ? t->d.width : 1; d.height = t->d.height ? t->d.height : 1; d.depth = 1; d.arrayLength = 1; + d.mipmapLevelCount = t->d.levels ? t->d.levels : 1; + switch (t->d.target) { + // 1D / 1D array are created as one-row 2D / 2D array textures: translated shaders never declare texture1d (0 in all five games' libraries) and always sample them as 2D; a Metal 1D texture bound to a texture2d argument is a type mismatch with undefined reads. + // Super Mario 3D World + Bowser's Fury's material shaders sample 64x1 R32F 1D lookup tables heavily every frame (on Maxwell a 2D sample of a 1D texture reads that one row) + case 0: d.textureType = MTLTextureType2D; d.height = 1; break; + case 2: d.textureType = MTLTextureType3D; d.depth = t->d.depth; break; + case 3: d.textureType = MTLTextureType2DArray; d.arrayLength = t->d.height; d.height = 1; break; + case 4: d.textureType = MTLTextureType2DArray; d.arrayLength = t->d.depth; break; + case 5: d.textureType = MTLTextureType2DMultisample; d.sampleCount = t->d.samples; d.mipmapLevelCount = 1; break; + case 6: d.textureType = MTLTextureType2DMultisampleArray; d.sampleCount = t->d.samples; d.arrayLength = t->d.depth; d.mipmapLevelCount = 1; break; + // Cube textures are created as 6-layer 2D arrays and sampled through cube / cube array views made by tex_fit: Metal only allows cube array views of a 2D array base texture (not of a cube base). + // The Legend of Zelda: Breath of the Wild has 1512 shaders that sample the environment light probes as a cube array while a single cube texture is bound (texture ID 1064 onwards): with mismatched types the reads are undefined, and every lit surface came out black + case 8: d.textureType = MTLTextureType2DArray; d.arrayLength = 6; break; + // NVN gives a cube array's depth in layer-faces (a multiple of 6), while + // Metal's cube-array length counts cubes. Keep the face layers in a 2D + // array and let tex_fit create the compatible cube-array view. + case 9: d.textureType = MTLTextureType2DArray; d.arrayLength = t->d.depth; break; + case 10: RED_ONCE(1, "缓冲贴图(TARGET_BUFFER)未实现"); return nil; + default: d.textureType = MTLTextureType2D; break; + } + d.storageMode = MTLStorageModePrivate; // Render targets and assets both go in GPU private storage (lossless compression available) + d.usage = MTLTextureUsageShaderRead | (fmt_compressed(t->d.format) || packaged ? 0 : MTLTextureUsageRenderTarget) + | (t->d.flags & NVN_TEXTURE_FLAGS_IMAGE ? MTLTextureUsageShaderWrite : 0) // Image usage: shader-writable (compute kernels generating the 3D color lookup table, etc.) + | (astc ? MTLTextureUsagePixelFormatView : 0) + | (unp ? MTLTextureUsageShaderWrite | MTLTextureUsagePixelFormatView : 0); // No BC: textures in the decompressed format are written by the GPU decompression kernel (up_dec) / receive whole-block copies from ASTC sources (copy_t2t_astc) + // iOS 16 validation requires format-view usage for the X32 stencil plane. + // Declare it at allocation, before the combined texture becomes a live attachment. + if (pf == MTLPixelFormatDepth32Float_Stencil8) d.usage |= MTLTextureUsagePixelFormatView; + return d; +} +// 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. +// 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 +static id tex_upgrade_pfv(const tex_t *t, id base, MTLPixelFormat view_pf) { + MTLTextureDescriptor *d = [MTLTextureDescriptor new]; + 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 + d.arrayLength = base.arrayLength; d.mipmapLevelCount = base.mipmapLevelCount; d.sampleCount = base.sampleCount; + d.storageMode = base.storageMode; d.cpuCacheMode = base.cpuCacheMode; d.hazardTrackingMode = base.hazardTrackingMode; + d.resourceOptions = base.resourceOptions; d.usage = base.usage | MTLTextureUsagePixelFormatView; + id upgraded = [g_dev newTextureWithDescriptor:d]; + if (!upgraded) { RED_ONCE(45, "贴图升级失败 ser=%llu 格式 %lu → %lu", t->serial, (unsigned long)base.pixelFormat, (unsigned long)view_pf); return base; } + tex_tag_format(upgraded, t->d.format); + res_add(upgraded); + id b = blit(); + NSUInteger slices = base.textureType == MTLTextureType3D ? 1 : base.arrayLength * (base.textureType == MTLTextureTypeCube || base.textureType == MTLTextureTypeCubeArray ? 6 : 1); + for (NSUInteger l = 0; l < base.mipmapLevelCount; l++) { + NSUInteger w = MAX(base.width >> l, 1u), h = MAX(base.height >> l, 1u), z = base.textureType == MTLTextureType3D ? MAX(base.depth >> l, 1u) : 1; + if (base.textureType == MTLTextureType3D) + enc_b_copy_texture(b, base, 0, l, MTLOriginMake(0, 0, 0), MTLSizeMake(w, h, z), upgraded, 0, l, MTLOriginMake(0, 0, 0)); + else for (NSUInteger s = 0; s < slices; s++) + enc_b_copy_texture(b, base, s, l, MTLOriginMake(0, 0, 0), MTLSizeMake(w, h, 1), upgraded, s, l, MTLOriginMake(0, 0, 0)); + } + id holder = tex_get(t->serial); + if ([holder isKindOfClass:[NvPkg class]]) { id old = ((NvPkg *)holder)->tex; ((NvPkg *)holder)->tex = upgraded; defer_obj_release(old); g_ser_gen++; } + else { + tex_put(t->serial, upgraded); + alias_key ak; memset(&ak, 0, sizeof ak); ak.pool = (uint64_t)(uintptr_t)t->pool; ak.off = t->off; ak.d = t->d; // Switch the sharing table to the upgraded one too, so later objects on the same storage get it directly + pthread_mutex_lock(&g_smu); if (hm_get_id(&g_alias, &ak) == base) { hm_put_id(&g_alias, &ak, upgraded); CFSetAddValue(g_alias_set, (__bridge const void *)upgraded); } pthread_mutex_unlock(&g_smu); + } + MLOG("贴图升级为可换格式视图 ser=%llu 基格式 %lu → 视图格式 %lu(升级次数=%llu)", t->serial, (unsigned long)base.pixelFormat, (unsigned long)view_pf, ++g_pfv_upgrades); + return upgraded; +} +// 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. +// 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; +// the same queue executes in commit order ⇒ uploads are always finished before the first sample. Uploads outside a submission are flushed on the spot. The staging area is cut from 32 MB private blocks, held by the command buffer until completion. +static _Atomic uint64_t g_upload_ns; // Cumulative time of deswizzle + upload encoding on the submit thread (timing instrumentation) +static _Atomic uint64_t g_upload_gpu_ns; +static id g_up_cb; +static id g_up_ce; +static _Atomic int g_up_alloc_busy[3]; +// Waiting for upload batches to finish (g_up_pending, g_up_alloc_busy): the batches' completion handler broadcasts after it updates them, so waiters sleep until then (they used to poll every 0.2–1 ms; +// Three Houses' loader calls QueueFinish after each resource, each one waited up to a millisecond late). The 10 ms timeout is only a safety net +static pthread_mutex_t g_up_wmu = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t g_up_wcv = PTHREAD_COND_INITIALIZER; +static void up_wake(void) { pthread_mutex_lock(&g_up_wmu); pthread_cond_broadcast(&g_up_wcv); pthread_mutex_unlock(&g_up_wmu); } +static void up_wait_zero(_Atomic int *v) { + if (!atomic_load(v)) return; + pthread_mutex_lock(&g_up_wmu); + while (atomic_load(v)) { struct timespec dl; clock_gettime(CLOCK_REALTIME, &dl); dl.tv_nsec += 10000000; if (dl.tv_nsec >= 1000000000) { dl.tv_sec++; dl.tv_nsec -= 1000000000; } pthread_cond_timedwait(&g_up_wcv, &g_up_wmu, &dl); } + pthread_mutex_unlock(&g_up_wmu); +} +static unsigned g_up_seq; +#define UP_BLOCK_SIZE (32u * 1024u * 1024u) +// Each of the three upload slots keeps one staging block of UP_SLOT_BLOCK for good (resident, reused once that slot's previous batch has finished: up_begin waits for it). Every batch used to make a fresh +// 32 MB private block, put it in the residency set and retire it after the GPU was done: Breath of the Wild re-uploads a few CPU-written textures each frame (about 40 KB each), so that was about 1.7 blocks +// a frame, about 3 GB/s of GPU memory allocated, made resident and freed, and 2.4 residency commits a frame. A slot's block grows to fit the first texture of a batch (up to UP_BLOCK_SIZE); what does +// not fit after that continues in fresh blocks of UP_BLOCK_SIZE as before +#define UP_SLOT_BLOCK (4u * 1024u * 1024u) +static __strong id g_up_slot_blk[3]; static unsigned g_up_cur_slot; +typedef struct { + __unsafe_unretained id src; + size_t off, row, image; + MTLSize size; + __unsafe_unretained id dst; // Strongly referenced by g_up_texs until the flush is encoded + NSUInteger slice, level; + uint8_t fill; // zero initialization; encoded before transcode/decode work +} up_blit; +static up_blit *g_up_blits; static int g_nup_blits, g_up_blitcap; +// Without BC, BC1–5 are decompressed on the GPU (A16 logs: CPU deswizzle + CPU decompression took over 300 ms in a single frame): the GPU deswizzle writes the compressed blocks into the staging area, and at flush time, after the barrier, +// the compute kernel bcd decodes them into the texture one thread per block (the target is a 2D view in the decompressed format; an sRGB base texture is written with raw values through an RGBA8Unorm view). BC6H / BC7 / 3D textures are very few and still go through the CPU (nvn_bcdec.c) +typedef struct { __unsafe_unretained id src, params; size_t off, poff; uint32_t bw, bh; __unsafe_unretained id view; } up_dec; // view: held by g_up_texs until this upload batch (not the whole replay submission) finishes +static up_dec *g_up_decs; static int g_nup_decs, g_up_deccap; +// BC1-3 stored as ASTC: GPU deswizzle first writes BC blocks into src, then +// bca transcodes them into a disjoint region of that same staging buffer. A +// following blit encoder copies the ASTC blocks into the compressed texture. +typedef struct { __unsafe_unretained id src, params; size_t poff; uint32_t nbx, nby; } up_ast; +static up_ast *g_up_asts; static int g_nup_asts, g_up_astcap; +static id bca_pso(void) { + static id ps; static int failed; + if (!ps && !failed) { + NSError *e = nil; + id lib = [g_dev newLibraryWithSource:@(nvn_bca_msl) options:nil error:&e]; + id fn = lib ? [lib newFunctionWithName:@"bca"] : nil; + ps = fn ? nvn_cached_compute(g_dev, fn, &e) : nil; + if (!ps) { failed = 1; MLOG("[RED] No-BC GPU BC-to-ASTC pipeline creation failed: %s; falling back to CPU transcoding", e.localizedDescription.UTF8String); } + else MLOG("No-BC storage: transcoding BC1-3 to ASTC 4x4 on the GPU"); + } + return ps; +} +static const char k_bcd_msl[] = +"#include \nusing namespace metal;\n" +"struct P { uint off, pitch, w, h, fmt; };\n" // fmt: 1 BC1, 2 BC2, 3 BC3, 4 BC4, 5 BC4 signed, 6 BC5, 7 BC5 signed +"static float3 c565(uint c) { return float3((c >> 11) & 31, (c >> 5) & 63, c & 31) / float3(31, 63, 31); }\n" +"static void col(device const uchar *b, thread float4 *o, bool four) {\n" +" uint c0 = b[0] | (b[1] << 8), c1 = b[2] | (b[3] << 8), ix = b[4] | (b[5] << 8) | (b[6] << 16) | (b[7] << 24); float3 e0 = c565(c0), e1 = c565(c1); float4 p[4];\n" +" p[0] = float4(e0, 1); p[1] = float4(e1, 1);\n" +" if (four || c0 > c1) { p[2] = float4((2 * e0 + e1) / 3, 1); p[3] = float4((e0 + 2 * e1) / 3, 1); } else { p[2] = float4((e0 + e1) / 2, 1); p[3] = float4(0); }\n" +" for (int i = 0; i < 16; i++) o[i] = p[(ix >> (2 * i)) & 3]; }\n" +"static void a8(device const uchar *b, thread float *o, bool sn) {\n" // Single-channel block of BC4 / BC3 alpha / BC5 +" float a0 = sn ? max(float(as_type(b[0])), -127.0) / 127 : b[0] / 255.0, a1 = sn ? max(float(as_type(b[1])), -127.0) / 127 : b[1] / 255.0;\n" +" bool eight = sn ? as_type(b[0]) > as_type(b[1]) : b[0] > b[1]; float lo = sn ? -1 : 0; float p[8]; p[0] = a0; p[1] = a1;\n" +" for (int k = 2; k < 8; k++) p[k] = eight ? ((8 - k) * a0 + (k - 1) * a1) / 7 : (k < 6 ? ((6 - k) * a0 + (k - 1) * a1) / 5 : (k == 6 ? lo : 1));\n" +" uint lo24 = b[2] | (b[3] << 8) | (b[4] << 16), hi24 = b[5] | (b[6] << 8) | (b[7] << 16);\n" +" for (int i = 0; i < 16; i++) o[i] = p[i < 8 ? (lo24 >> (3 * i)) & 7 : (hi24 >> (3 * (i - 8))) & 7]; }\n" +"kernel void bcd(device const uchar *src [[buffer(0)]], device const P &p [[buffer(1)]], texture2d dst [[texture(0)]], uint2 g [[thread_position_in_grid]]) {\n" +" uint nbx = (p.w + 3) / 4, nby = (p.h + 3) / 4; if (g.x >= nbx || g.y >= nby) return;\n" +" uint bb = (p.fmt == 1 || p.fmt == 4 || p.fmt == 5) ? 8 : 16; device const uchar *b = src + p.off + g.y * p.pitch + g.x * bb; float4 o[16]; float a[16], c[16];\n" +" if (p.fmt == 1) col(b, o, false);\n" +" else if (p.fmt == 2) { col(b + 8, o, true); for (int i = 0; i < 16; i++) o[i].a = ((b[i / 2] >> (4 * (i & 1))) & 15) / 15.0; }\n" +" else if (p.fmt == 3) { col(b + 8, o, true); a8(b, a, false); for (int i = 0; i < 16; i++) o[i].a = a[i]; }\n" +" else if (p.fmt == 4 || p.fmt == 5) { a8(b, a, p.fmt == 5); for (int i = 0; i < 16; i++) o[i] = float4(a[i], 0, 0, 1); }\n" +" else { a8(b, a, p.fmt == 7); a8(b + 8, c, p.fmt == 7); for (int i = 0; i < 16; i++) o[i] = float4(a[i], c[i], 0, 1); }\n" +" for (int i = 0; i < 16; i++) { uint2 q = g * 4 + uint2(i & 3, i >> 2); if (q.x < p.w && q.y < p.h) dst.write(o[i], q); } }\n"; +static int bcd_fmt(uint32_t f) { // NVN format → bcd fmt; 0 = not decoded on the GPU (BC6H / BC7 / non-BC) + switch (f) { case 0x41: case 0x42: case 0x45: case 0x46: return 1; case 0x43: case 0x47: return 2; case 0x44: case 0x48: return 3; + case 0x49: return 4; case 0x4a: return 5; case 0x4b: return 6; case 0x4c: return 7; default: return 0; } +} +static id bcd_pso(void) { + static id ps; static int failed; + if (!ps && !failed) { NSError *e = nil; id lib = [g_dev newLibraryWithSource:@(k_bcd_msl) options:nil error:&e]; id fn = lib ? [lib newFunctionWithName:@"bcd"] : nil; + ps = fn ? nvn_cached_compute(g_dev, fn, &e) : nil; + if (!ps) { failed = 1; MLOG("【红】无 BC:GPU 解压管线建不出 %s(退回 CPU 解压)", e.localizedDescription.UTF8String); } else MLOG("无 BC:BC1–5 改在 GPU 上解压"); } + return ps; +} +// Every target and temporary writable view referenced by the batch. A +// scene-load replay can create more than 1,500 BC-decompression views before +// its terminal fence. Deferring those views to that fence pins their parent +// textures while the CPU is still translating the replay and can cross tvOS's +// memory limit. The upload command buffer is their exact GPU owner, so this +// array is transferred to its completion handler at flush time. +static NSMutableArray *g_up_texs; +static id g_up_block; +static size_t g_up_block_off, g_up_block_size; +static NSMutableArray *g_up_keep; +// Limit overflow staging per batch. The three upload slots bound in-flight +// batches. A single texture larger than the budget gets its own block (uploads +// are not tiled here). +static size_t g_up_staging_bytes; +#define UP_BATCH_BYTES (128u * 1024u * 1024u) +// BC1-5 expands to ordinary RGBA/RG textures on Apple GPUs without native BC. +// The compressed staging footprint can therefore stay below UP_BATCH_BYTES +// while one upload command buffer accumulates hundreds of MiB of writable +// target textures. Bound that distinct lifetime as well. Three upload slots +// mean a fourth batch waits for the first one's completion, limiting ordinary +// in-flight decompressed targets to about 3 * this value. +#ifndef UP_BATCH_TARGET_BYTES +#define UP_BATCH_TARGET_BYTES (64u * 1024u * 1024u) +#endif +static size_t g_up_target_bytes, g_up_target_peak; +static uint64_t g_up_target_splits; + + +// The upload command buffer was discarded by the system (dropped along during GPU error recovery; the first frame on entering Travincal drops one every run): the objects of this batch of textures are built and the timestamps updated, but the content was never written in, +// and if the pool memory on the game side no longer changes it will never be re-uploaded. Record this batch of target textures, and the next time any of them is fetched treat it as dirty and re-upload once. +// DM_TEST_UP_LOST= (for testing): treat that batch of uploads as dropped, to verify the re-upload path. +static NSHashTable *g_up_lost; static os_unfair_lock g_up_lost_lk = OS_UNFAIR_LOCK_INIT; static _Atomic int g_up_lost_n; static _Atomic uint64_t g_up_lost_redo; // Weak-reference table: if a texture in the record is never fetched again and is released normally, this table does not keep it alive +static int up_lost_take(id tex) { // This texture's upload was dropped: take it out of the record and return 1 + if (!tex) return 0; os_unfair_lock_lock(&g_up_lost_lk); int hit = [g_up_lost containsObject:tex]; if (hit) { [g_up_lost removeObject:tex]; atomic_store(&g_up_lost_n, (int)g_up_lost.allObjects.count); atomic_fetch_add(&g_up_lost_redo, 1); } os_unfair_lock_unlock(&g_up_lost_lk); return hit; +} +static void up_diag(void); +static void up_flush(void) { + up_owner_check("up_flush"); + if (!g_up_cb) return; + if (g_up_ce) { + m4_end(g_up_ce); g_up_ce = nil; + } + // Three encoder phases give explicit ordering on traditional Metal: + // transparent zero fills -> BC decode/transcode -> uploaded content. + // This prevents a large ASTC copy from overtaking its transcoder and + // decoding as Metal's magenta invalid-block colour. + int nfill = 0; for (int i = 0; i < g_nup_blits; i++) nfill += g_up_blits[i].fill; + id b = nfill ? [g_up_cb blitCommandEncoder] : nil; + for (int i = 0; b && i < g_nup_blits; i++) { + up_blit *x = &g_up_blits[i]; if (!x->fill) continue; + enc_b_copy_buffer_texture(b, x->src, x->off, x->row, x->image, x->size, x->dst, x->slice, x->level, MTLOriginMake(0, 0, 0)); + } + [b endEncoding]; + id dc = g_nup_decs || g_nup_asts ? [g_up_cb computeCommandEncoder] : nil; + if (dc) { + if (g_nup_decs) { + [dc setComputePipelineState:bcd_pso()]; + for (int i = 0; i < g_nup_decs; i++) { + up_dec *x = &g_up_decs[i]; + [dc setBuffer:x->src offset:0 atIndex:0]; + [dc setBuffer:x->params offset:x->poff atIndex:1]; + [dc setTexture:x->view atIndex:0]; + [dc dispatchThreadgroups:MTLSizeMake((x->bw + 7) / 8, (x->bh + 7) / 8, 1) threadsPerThreadgroup:MTLSizeMake(8, 8, 1)]; + } + } + if (g_nup_asts) { + [dc setComputePipelineState:bca_pso()]; + for (int i = 0; i < g_nup_asts; i++) { + up_ast *x = &g_up_asts[i]; + [dc setBuffer:x->src offset:0 atIndex:0]; + [dc setBuffer:x->params offset:x->poff atIndex:1]; + [dc setBuffer:x->src offset:0 atIndex:2]; + [dc dispatchThreadgroups:MTLSizeMake((x->nbx + 7) / 8, (x->nby + 7) / 8, 1) threadsPerThreadgroup:MTLSizeMake(8, 8, 1)]; + } + } + } + [dc endEncoding]; g_up_ce = nil; + b = g_nup_blits > nfill ? [g_up_cb blitCommandEncoder] : nil; + for (int i = 0; b && i < g_nup_blits; i++) { + up_blit *x = &g_up_blits[i]; if (x->fill) continue; + enc_b_copy_buffer_texture(b, x->src, x->off, x->row, x->image, x->size, x->dst, x->slice, x->level, MTLOriginMake(0, 0, 0)); + } + [b endEncoding]; + id cb = g_up_cb; + unsigned slot = (g_up_seq - 1) % 3; + // Only this upload batch uses its overflow and CPU staging. Retire it as + // soon as the upload completes, even while the main rendering submission + // is still being built. + NSMutableArray *staging = g_up_keep; + NSMutableArray *batch_refs = g_up_texs; + if (g_up_block && g_up_block != g_up_slot_blk[slot]) [staging addObject:g_up_block]; + g_up_keep = nil; g_up_texs = nil; g_up_block = nil; + g_up_block_off = g_up_block_size = g_up_staging_bytes = g_up_target_bytes = 0; + NSMutableArray *dsts = [NSMutableArray arrayWithCapacity:(NSUInteger)g_nup_blits]; for (int i = 0; i < g_nup_blits; i++) if (g_up_blits[i].dst) [dsts addObject:g_up_blits[i].dst]; + static int lost_at = -2; static uint64_t batch_n; if (lost_at == -2) { const char *e = getenv("DM_TEST_UP_LOST"); lost_at = e ? atoi(e) : -1; } int fake_lost = dsts.count && lost_at > 0 && (int)++batch_n == lost_at; + + atomic_fetch_add(&g_up_pending, 1); + [cb addCompletedHandler:^(id f) { + if (f.error) MLOG("【红】上传命令缓冲执行出错:%s", f.error.description.UTF8String); + if (f.error || fake_lost) { + os_unfair_lock_lock(&g_up_lost_lk); if (!g_up_lost) g_up_lost = [NSHashTable weakObjectsHashTable]; for (id d in dsts) [g_up_lost addObject:d]; int n = (int)g_up_lost.allObjects.count; atomic_store(&g_up_lost_n, n); os_unfair_lock_unlock(&g_up_lost_lk); + pthread_mutex_lock(&g_smu); g_ser_gen++; pthread_mutex_unlock(&g_smu); // Make the view cache of every texture ID take the slow path once (on a cache hit tex_mtl is not passed through and this record is not seen) + MLOG("%s上传命令缓冲被丢:这一批 %lu 张贴图记为内容未写,下次取用时重传(待重传共 %d 张)", fake_lost ? "DM_TEST_UP_LOST:" : "", (unsigned long)dsts.count, n); + } + double d = f.GPUEndTime - f.GPUStartTime; + if (d > 0) atomic_fetch_add(&g_upload_gpu_ns, (uint64_t)(d * 1e9)); + @autoreleasepool { + for (id obj in staging) res_remove(obj); + [staging removeAllObjects]; + // Views were never separate residency allocations; releasing the + // upload owner's references is sufficient and may also release a + // replaced parent texture immediately. + [batch_refs removeAllObjects]; + res_commit(); + } + atomic_store(&g_up_alloc_busy[slot], 0); + atomic_fetch_sub(&g_up_pending, 1); + up_wake(); + }]; + res_commit(); + [cb commit]; + g_up_cb = nil; + up_diag(); + g_nup_blits = 0; g_nup_decs = 0; g_nup_asts = 0; +} + +static void up_begin(void) { + up_owner_check("up_begin"); + if (g_up_cb) return; + unsigned slot = g_up_seq++ % 3; + up_wait_zero(&g_up_alloc_busy[slot]); + atomic_store(&g_up_alloc_busy[slot], 1); g_up_cur_slot = slot; + g_up_cb = [g_q commandBuffer]; + g_up_ce = [g_up_cb computeCommandEncoder]; + + + if (!g_up_texs) g_up_texs = [NSMutableArray array]; + if (!g_up_keep) g_up_keep = [NSMutableArray array]; +} + +// Start (or keep) the batch that will own one GPU-BC texture. A texture larger +// than the budget is allowed to occupy a batch by itself; the next texture +// splits. Charge only after successful encoding: up_alloc can independently +// flush for its staging budget and must then leave the charge on the new batch. +static void up_target_prepare(size_t bytes) { + size_t used = MIN(g_up_target_bytes, (size_t)UP_BATCH_TARGET_BYTES); + if (g_up_cb && g_up_target_bytes && bytes > (size_t)UP_BATCH_TARGET_BYTES - used) { + g_up_target_splits++; + up_flush(); + } + up_begin(); +} +static void up_target_charge(size_t bytes) { + if (bytes > SIZE_MAX - g_up_target_bytes) MDIE("GPU-BC target byte counter overflow"); + g_up_target_bytes += bytes; + if (g_up_target_bytes > g_up_target_peak) g_up_target_peak = g_up_target_bytes; +} + +// Diagnostic readout (low frequency; printed only when the block switch count changed and ≥ 1 second since the last line, at most 200 lines per game): how many times an upload batch ran out of staging block and switched blocks midway; GPU allocated, residency set object count, deferred-release queue length. +// If the old block is not retired on a switch it stays in the residency set and can never be freed, and "GPU allocated" climbs one block at a time with the switch count +static uint64_t g_up_blocks_new, g_up_swaps, g_up_swap_bytes; static NSHashTable *g_up_old; // g_up_old: old blocks switched out (weak references); the diagnostic line counts how many are still alive +static void up_diag(void) { @autoreleasepool { + // Weak-table enumeration autoreleases the buffers it finds. Bound those + // temporary references so completed batches can disappear immediately. + // Print a line when the block switch count changes; then one more line 5 seconds later (that batch's commit is long done and deferred retirement drained, so "GPU allocated" is the settled number) + static uint64_t said_swaps = ~0ull, said_target_splits = ~0ull, said_gpu_bucket = ~0ull, last_ns; static int lines, follow; uint64_t now = clock_gettime_nsec_np(CLOCK_MONOTONIC); + uint64_t gpu_bucket = g_dev.currentAllocatedSize >> 27; // force a census at every additional 128 MiB, even when staging counters stay flat + int chg = g_up_swaps != said_swaps || g_up_target_splits != said_target_splits || gpu_bucket != said_gpu_bucket, fol = follow && now - last_ns >= 5000000000ull; if ((!chg && !fol) || lines >= 200) return; + if (chg && last_ns && now - last_ns < 1000000000ull) return; last_ns = now; said_swaps = g_up_swaps; said_target_splits = g_up_target_splits; said_gpu_bucket = gpu_bucket; lines++; follow = chg; + unsigned long alive = 0, inres = 0; uint64_t abytes = 0; NSArray *olds = g_up_old.allObjects; + pthread_mutex_lock(&g_res_mu); unsigned long nres = (unsigned long)g_res_objects.allObjects.count; for (id o in olds) { alive++; abytes += o.length; inres += [g_res_objects containsObject:o]; } pthread_mutex_unlock(&g_res_mu); + pthread_mutex_lock(&g_deferred_mu); unsigned long ndef = (unsigned long)g_deferred.count; pthread_mutex_unlock(&g_deferred_mu); + MLOG("upload staging%s: %llu blocks created, %llu mid-batch switches (%llu MB replaced); %llu GPU-BC target-budget splits, %llu MB peak target batch; %lu replaced blocks still alive (%llu MB), %lu still resident; GPU allocated %llu MB, %lu resident objects, %lu deferred releases", fol && !chg ? " (settled)" : "", + (unsigned long long)g_up_blocks_new, (unsigned long long)g_up_swaps, (unsigned long long)(g_up_swap_bytes >> 20), + (unsigned long long)g_up_target_splits, (unsigned long long)(g_up_target_peak >> 20), alive, + (unsigned long long)(abytes >> 20), inres, (unsigned long long)(g_dev.currentAllocatedSize >> 20), nres, ndef); + defer_diag_snapshot(fol && !chg ? "upload settled" : "upload batch"); + tex_format_stat(); +} } +static id up_alloc(size_t n, size_t *off) { + size_t need = (n + 255u) & ~((size_t)255u); + size_t extra = MAX(need, UP_BLOCK_SIZE); + // This call starts one texture upload. Everything previously recorded can + // be split without ending the main render pass. + if (g_up_staging_bytes && (!g_up_block || need > g_up_block_size - g_up_block_off) + && extra > UP_BATCH_BYTES - MIN(g_up_staging_bytes, UP_BATCH_BYTES)) { + up_flush(); + up_begin(); + } + if (!g_up_block && need <= UP_BLOCK_SIZE) { // first allocation of the batch: the slot's own block, grown to the next power of two that holds it (a movie frame re-uploaded every frame, 8 MB in Link's Awakening) + __strong id *sb = &g_up_slot_blk[g_up_cur_slot]; + size_t want = UP_SLOT_BLOCK; while (want < need) want <<= 1; + if (*sb && (*sb).length < want) { defer_obj_release(*sb); *sb = nil; } // the slot's previous batch has finished (up_begin waited for it) + if (!*sb) { *sb = [g_dev newBufferWithLength:want options:MTLResourceStorageModePrivate]; if (!*sb) MDIE("建预排布上传暂存块失败 %#zx", want); res_add(*sb); g_up_blocks_new++; } + g_up_block = *sb; g_up_block_size = (*sb).length; g_up_block_off = 0; + g_up_staging_bytes += (*sb).length; + } + if (!g_up_block || g_up_block_off + need > g_up_block_size) { + // Block switch: the old block is still referenced by copy / decompress commands already recorded in this batch and cannot be freed on the spot; hand it to g_up_keep, to be retired later with the other staging after the flush commits (removed from the residency set on retire). + // Previously g_up_block was simply replaced with the new block here: nobody retired the old block, yet it stayed in the residency set, so each extra block switch in an upload batch leaked one block (usually 32 MB) of GPU memory. + // The old block and the "current block" retired at flush are two objects, so nothing is retired twice + g_up_blocks_new++; if (g_up_block) { g_up_swaps++; g_up_swap_bytes += g_up_block_size; @autoreleasepool { if (!g_up_old) g_up_old = [NSHashTable weakObjectsHashTable]; [g_up_old addObject:g_up_block]; } static int leak = -1; if (leak < 0) leak = getenv("DM_TEST_UP_LEAK") != NULL; // test switch DM_TEST_UP_LEAK=1: do not retire the old block, as before the fix (for comparison, to measure the leak) + if (!leak && g_up_block != g_up_slot_blk[g_up_cur_slot]) { if (!g_up_keep) g_up_keep = [NSMutableArray array]; [g_up_keep addObject:g_up_block]; } } // the slot's block is not retired: it stays for the slot's later batches + g_up_block_size = n > UP_BLOCK_SIZE ? need : UP_BLOCK_SIZE; + g_up_block = [g_dev newBufferWithLength:g_up_block_size options:MTLResourceStorageModePrivate]; + if (!g_up_block) MDIE("建预排布上传暂存块失败 %#zx", g_up_block_size); + g_up_staging_bytes += g_up_block_size; + res_add(g_up_block); + g_up_block_off = 0; + } + *off = g_up_block_off; + g_up_block_off += need; + return g_up_block; +} + +// Deswizzle parameter blocks: 64 bytes per cell, cut sequentially from a 64 KB shared-storage block; a full block is replaced by a new one, and the old block is released lazily by fence +static id g_up_pblock; static size_t g_up_pblock_off; +static id up_param_alloc(size_t *off) { + if (!g_up_pblock || g_up_pblock_off + 64 > g_up_pblock.length) { + if (g_up_pblock) defer_obj_release(g_up_pblock); + g_up_pblock = [g_dev newBufferWithLength:64 << 10 options:MTLResourceStorageModeShared]; + if (!g_up_pblock) MDIE("建解排布参数块失败"); + res_add(g_up_pblock); g_up_pblock_off = 0; + } + *off = g_up_pblock_off; g_up_pblock_off += 64; + return g_up_pblock; +} +static void up_blit_add(id src, size_t off, size_t row, size_t image, MTLSize size, + id dst, NSUInteger slice, NSUInteger level) { + if (g_nup_blits == g_up_blitcap) { + g_up_blitcap = g_up_blitcap ? g_up_blitcap * 2 : 256; + g_up_blits = realloc(g_up_blits, g_up_blitcap * sizeof *g_up_blits); + } + g_up_blits[g_nup_blits++] = (up_blit){src, off, row, image, size, dst, slice, level, 0}; +} +static void up_blit_add_fill(id src, size_t off, size_t row, size_t image, MTLSize size, + id dst, NSUInteger slice, NSUInteger level) { + up_blit_add(src, off, row, image, size, dst, slice, level); + g_up_blits[g_nup_blits - 1].fill = 1; +} + +// A CPU-visible linear texture can normally be exposed directly as a texture view of its +// memory-pool buffer. Metal requires both the buffer offset and row stride to meet the device's +// linear-texture alignment, however. NVN permits smaller strides (Z-A uses one with a 32-byte +// row), so copy those exceptional textures into a private texture and refresh it through the +// existing dirty-page path instead of silently leaving the private fallback black. +static int linear_needs_upload(const tex_t *t, int *out_bpb, size_t *out_row) { + if (!(t->d.flags & (NVN_TEXTURE_FLAGS_LINEAR | NVN_TEXTURE_FLAGS_LINEAR_RENDER_TARGET)) || !t->mem || !t->pool || + (t->pool->flags & NVN_MEMORY_POOL_FLAGS_CPU_NO_ACCESS) || t->d.target != NVN_TEXTURE_TARGET_2D || t->d.levels > 1 || t->d.samples > 1) return 0; + uint32_t pf = 0; uint8_t swz[4]; int bw, bh, bpb = nvn_fmt_bpb(t->d.format, &bw, &bh); + if (!nvn_fmt_to_mtl(t->d.format, &pf, swz) || !pf || !bpb || bw != 1 || bh != 1) return 0; + size_t row = t->d.stride ? t->d.stride : (size_t)t->d.width * (size_t)bpb; + if (out_bpb) *out_bpb = bpb; if (out_row) *out_row = row; + void *mb = NULL; uint64_t mo = 0; + if (!mtl_addr((uint64_t)t->mem, (uint64_t)row * (t->d.height ? t->d.height : 1), &mb, &mo)) return 1; + NSUInteger al = [g_dev minimumLinearTextureAlignmentForPixelFormat:(MTLPixelFormat)pf]; + int need = mo % al || row % al; + return need; +} + +static id pkg_upload_(const tex_t *t, uint64_t *stamp, id reuse, nvn_updiag_record *diag, int reupload); +static id pkg_upload(const tex_t *t, uint64_t *stamp, id reuse, int reupload) { // reuse: a texture of the same description, idle on the GPU, to upload into instead of a new one + nvn_updiag_meta meta = {atomic_load(&g_cur_frame), t->serial, t->off, t->L.size, + t->pool ? t->pool->size : 0, t->mem, t->pool, + t->d.width, t->d.height, t->d.depth, t->d.format, t->d.target, t->d.levels, t->d.flags, reupload}; + nvn_updiag_record diag; nvn_updiag_begin(&diag, &meta); + struct timespec a, b; clock_gettime(CLOCK_MONOTONIC, &a); + id r = pkg_upload_(t, stamp, reuse, &diag, reupload); + clock_gettime(CLOCK_MONOTONIC, &b); uint64_t d_ = (uint64_t)((b.tv_sec - a.tv_sec) * 1000000000ll + b.tv_nsec - a.tv_nsec); + atomic_fetch_add(&g_upload_ns, d_); g_up_total_ns += d_; + nvn_updiag_end(&diag, d_, r != nil); + return r; +} +uint64_t mtl_upload_ns_take(void) { return atomic_exchange(&g_upload_ns, 0); } +uint64_t mtl_upload_gpu_ns_take(void) { return atomic_exchange(&g_upload_gpu_ns, 0); } +void mtl_create_stats(uint64_t *lib, uint64_t *pso) { *lib = g_bp_lib_n; *pso = g_bp_pso_n; } +void mtl_create_ns(uint64_t *lib_ns, uint64_t *pso_ns) { *lib_ns = g_bp_lib_ns; *pso_ns = g_bp_pso_ns; } // Cumulative time spent synchronously creating shader libraries / pipelines on the spot (the long-frame log takes the per-frame difference) +uint64_t mtl_upload_bytes(void) { return g_bp_up_bytes; } // Cumulative texture upload bytes +static id pkg_upload_(const tex_t *t, uint64_t *stamp, id reuse, nvn_updiag_record *diag, int reupload) { + MTLTextureDescriptor *d = tex_desc(t, 1); + if (!d) return nil; + id mt = reuse && reuse.pixelFormat == d.pixelFormat && reuse.width == d.width && reuse.height == d.height && reuse.mipmapLevelCount == d.mipmapLevelCount && reuse.textureType == d.textureType + ? reuse : [g_dev newTextureWithDescriptor:d]; + if (!mt) { RED_ONCE(2, "建 MTLTexture 失败 %ux%u 格式 %#x 目标 %d", t->d.width, t->d.height, t->d.format, t->d.target); return nil; } + tex_tag_format(mt, t->d.format); + if (g_allocprof > 0) { static int n; if (n++ < 30 || n % 500 == 0) MLOG("新建贴图(%s)#%d ser=%llu %ux%ux%u 格式 %#x 目标 %d 级 %u 存储 %p", __func__, n, (unsigned long long)t->serial, t->d.width, t->d.height, t->d.depth, t->d.format, t->d.target, t->d.levels, (void *)t->mem); } + res_add(mt); // Both first upload and re-upload register residency here (Metal 4 does not make resources resident automatically; a missed registration = GPU address page fault) + nvn_dirty_protect(t->mem, t->L.size); + *stamp = nvn_dirty_now(); + nvn_updiag_capture(diag); + int linear_bpb = 0; size_t linear_row = 0; + if (linear_needs_upload(t, &linear_bpb, &linear_row)) { + diag->path = "cpu-linear"; + size_t w = t->d.width ? t->d.width : 1, h = t->d.height ? t->d.height : 1, tight = w * (size_t)linear_bpb; + if (!linear_bpb || linear_row < tight || h > 1 && linear_row > (SIZE_MAX - tight) / (h - 1) || linear_row * (h - 1) + tight > t->L.size) { + RED_ONCE(66, "线性贴图上传范围无效:%zux%zu 行距 %zu 存储 %llu", w, h, linear_row, (unsigned long long)t->L.size); + return mt; + } + up_begin(); + id stg = [g_dev newBufferWithLength:tight * h options:MTLResourceStorageModeShared]; + if (!stg) return mt; + res_add(stg); [g_up_keep addObject:stg]; + for (size_t y = 0; y < h; y++) memcpy((uint8_t *)stg.contents + y * tight, t->mem + y * linear_row, tight); + up_blit_add(stg, 0, tight, tight * h, MTLSizeMake(w, h, 1), mt, 0, 0); + g_bp_up_bytes += tight * h; + [g_up_texs addObject:mt]; + if (!g_cb) up_flush(); + static int once; if (!once++) MLOG("未对齐线性贴图:通过暂存上传并跟踪 CPU 写入"); + return mt; + } + int bw, bh, bpb = nvn_fmt_bpb(t->d.format, &bw, &bh); + unsigned layers = (t->d.target == 4 || t->d.target == 9) ? t->d.depth : t->d.target == 8 ? 6 : t->d.target == 3 ? t->d.height : 1; // a cube array's depth counts layer-faces (tex_desc) + uint32_t upf; int ubpp = 0, unp = 0, astc = bc_astc(t, &upf); + if (!astc) unp = bc_unpack(t->d.format, &upf, &ubpp); + int akind = 0; if (astc) { uint32_t apf; nvn_bc_astc_fmt((uint32_t)t->d.format, &apf, &akind); } + size_t total = 0; + for (unsigned l = 0; l < d.mipmapLevelCount; l++) { + uint32_t w = MAX(t->d.width >> l, 1u), h = MAX(t->d.height >> l, 1u), z = t->d.target == 2 ? MAX(t->d.depth >> l, 1u) : 1; + total += astc ? (size_t)((w + 3) / 4) * ((h + 3) / 4) * 16 * layers + : unp ? (size_t)w * ubpp * h * z * layers + : (size_t)((w + bw - 1) / bw) * bpb * ((h + bh - 1) / bh) * z * layers; + } + g_bp_up_bytes += total; + void *mb; uint64_t mo; + int gdec = unp && t->d.target != 2 && bcd_fmt(t->d.format) && bcd_pso(); // BC1–5 without BC: GPU deswizzle + GPU decompression + int gast = astc && bca_pso(); + size_t ctotal = 0, abase = 0, atotal = 0; + if (gast) for (unsigned l = 0; l < d.mipmapLevelCount; l++) { + uint32_t w = MAX(t->d.width >> l, 1u), h = MAX(t->d.height >> l, 1u); + ctotal += (size_t)((w + bw - 1) / bw) * bpb * ((h + bh - 1) / bh) * layers; + atotal += (size_t)((w + 3) / 4) * ((h + 3) / 4) * 16 * layers; + } + if (gast) abase = (ctotal + 255u) & ~(size_t)255u; + if (gdec) for (unsigned l = 0; l < d.mipmapLevelCount; l++) { uint32_t w = MAX(t->d.width >> l, 1u), h = MAX(t->d.height >> l, 1u); ctotal += (size_t)((w + bw - 1) / bw) * bpb * ((h + bh - 1) / bh) * layers; } + int use_gpu = (astc ? gast : (!unp || gdec)) && g_nvn_deswz_gpu && mtl_addr((uint64_t)t->mem, t->L.size, &mb, &mo); + if (g_nvn_deswz_gpu && !use_gpu && !unp && !astc) RED_ONCE(40, "pre-tiled texture storage is outside a memory pool; falling back to CPU upload"); + if (use_gpu) { + diag->path = gast ? "gpu-bc-astc" : gdec ? "gpu-bc" : "gpu-deswizzle"; + // The GPU reads the memory pool at execution time, which is closer to a real Switch than a snapshot at CPU upload time; the dirty-page stamp is still taken before encoding, and later writes still trigger a re-upload. + if (gdec || gast) up_target_prepare(total); else up_begin(); + size_t stg_off; + id stg = up_alloc(gast ? abase + atotal : gdec ? ctotal : total ? total : 16, &stg_off); + size_t off = 0, aoff = 0; int enc_ok = g_up_ce != nil, nb0 = g_nup_blits, nd0 = g_nup_decs, na0 = g_nup_asts; + for (unsigned l = 0; enc_ok && l < d.mipmapLevelCount; l++) { + uint32_t w = MAX(t->d.width >> l, 1u), h = MAX(t->d.height >> l, 1u), z = t->d.target == 2 ? MAX(t->d.depth >> l, 1u) : 1; + size_t pitch = (size_t)((w + bw - 1) / bw) * bpb, rows = (h + bh - 1) / bh; + for (unsigned y = 0; y < layers; y++) { + if (nvn_deswz_gpu_encode(g_up_ce, (__bridge id)mb, mo, &t->d, l, y, stg, stg_off + off, pitch)) { enc_ok = 0; break; } + if (gast) { + uint32_t nbx = (w + 3) / 4, nby = (h + 3) / 4; + size_t qoff; id q = up_param_alloc(&qoff); if (!q) { enc_ok = 0; break; } + uint32_t *pp = (uint32_t *)((uint8_t *)q.contents + qoff); + pp[0] = (uint32_t)(stg_off + off); pp[1] = (uint32_t)pitch; + pp[2] = (uint32_t)(stg_off + abase + aoff); pp[3] = nbx * 16; + pp[4] = nbx; pp[5] = nby; pp[6] = (uint32_t)akind; + if (g_nup_asts == g_up_astcap) { g_up_astcap = g_up_astcap ? g_up_astcap * 2 : 256; g_up_asts = realloc(g_up_asts, g_up_astcap * sizeof *g_up_asts); } + g_up_asts[g_nup_asts++] = (up_ast){stg, q, qoff, nbx, nby}; + up_blit_add(stg, stg_off + abase + aoff, (size_t)nbx * 16, (size_t)nbx * 16 * nby, MTLSizeMake(w, h, 1), mt, y, l); + aoff += (size_t)nbx * nby * 16; + } else if (gdec) { // Decompression job: dispatched at flush time after the barrier (see up_flush) + MTLPixelFormat vf = mt.pixelFormat == MTLPixelFormatRGBA8Unorm_sRGB ? MTLPixelFormatRGBA8Unorm : mt.pixelFormat; + id v = [mt newTextureViewWithPixelFormat:vf textureType:MTLTextureType2D levels:NSMakeRange(l, 1) slices:NSMakeRange(y, 1)]; + size_t qoff; id q = up_param_alloc(&qoff); + if (!v || !q) { enc_ok = 0; break; } + uint32_t *pp = (uint32_t *)((uint8_t *)q.contents + qoff); pp[0] = (uint32_t)(stg_off + off); pp[1] = (uint32_t)pitch; pp[2] = w; pp[3] = h; pp[4] = (uint32_t)bcd_fmt(t->d.format); + if (g_nup_decs == g_up_deccap) { g_up_deccap = g_up_deccap ? g_up_deccap * 2 : 256; g_up_decs = realloc(g_up_decs, g_up_deccap * sizeof *g_up_decs); } + g_up_decs[g_nup_decs++] = (up_dec){stg, q, 0, qoff, (w + 3) / 4, (h + 3) / 4, v}; [g_up_texs addObject:v]; // Completion owns the view only until this upload CB finishes; the base texture, not this view, is resident. + } else + up_blit_add(stg, stg_off + off, pitch, pitch * rows, MTLSizeMake(w, h, z), mt, y, l); + off += pitch * rows * z; + } + } + if (enc_ok) { + if (gdec || gast) up_target_charge(total); + [g_up_texs addObject:mt]; + if (!g_cb) up_flush(); + return mt; + } + g_nup_blits = nb0; g_nup_decs = nd0; g_nup_asts = na0; // Roll back this texture's pending work; an already-encoded staging deswizzle is harmless + if (!g_cb) up_flush(); + RED_ONCE(41, "预排布贴图 GPU 解排布不支持格式 %#x 目标 %d,回退 CPU", t->d.format, t->d.target); + } + + diag->path = astc ? "cpu-bc-astc" : unp ? "cpu-bc" : "cpu-deswizzle"; + // Cache decoded pixels, not the live texture: even an IMAGE-capable source + // still receives its ordinary upload, including a lost-upload retry. + nvn_cpu_decode_ticket cached = {0}; + if (unp && reupload) { + nvn_texlayout footprint; nvn_tex_layout(&t->d, &footprint); + // The copied source must cover every byte the descriptor's deswizzle + // can read; inconsistent packaged layout metadata takes the old path. + if (footprint.size && footprint.size <= t->L.size) { + nvn_cpu_decode_key key = {t->serial, t->off, t->L.size, total, t->mem, t->pool, t->d}; + cached = nvn_cpu_decode_begin(atomic_load(&g_cur_frame), &key); + } + if (cached.hit) diag->path = "cpu-bc-cached"; + } + up_begin(); + id stg = [g_dev newBufferWithLength:total ? total : 16 options:MTLResourceStorageModeShared]; + res_add(stg); [g_up_keep addObject:stg]; + const uint8_t *decode_source = cached.entry ? cached.source : t->mem; + uint8_t *decode_target = cached.entry ? cached.decoded : stg.contents; + int decoded_ok = 1; + size_t off = 0; + for (unsigned l = 0; l < d.mipmapLevelCount; l++) { + uint32_t w = MAX(t->d.width >> l, 1u), h = MAX(t->d.height >> l, 1u), z = t->d.target == 2 ? MAX(t->d.depth >> l, 1u) : 1; + size_t pitch = (size_t)((w + bw - 1) / bw) * bpb, rows = (h + bh - 1) / bh; + for (unsigned y = 0; y < layers; y++) { + if (astc) { + uint8_t *cb = malloc(pitch * rows); size_t ap = (size_t)((w + 3) / 4) * 16; + if (!cb || nvn_deswizzle(&t->d, l, y, t->mem, cb, pitch)) { decoded_ok = 0; free(cb); RED_ONCE(9, "texture deswizzle failed for format %#x target %d", t->d.format, t->d.target); break; } + if (nvn_bc2astc((uint32_t)t->d.format, cb, pitch, w, h, (uint8_t *)stg.contents + off, ap)) decoded_ok = 0; + free(cb); + up_blit_add(stg, off, ap, ap * ((h + 3) / 4), MTLSizeMake(w, h, 1), mt, y, l); + off += ap * ((h + 3) / 4); + continue; + } + if (unp) { // No BC: first deswizzle into a temporary area (compressed blocks), then decompress layer by layer into the staging area + size_t up = (size_t)w * ubpp; + if (!cached.hit) { + uint8_t *cb = malloc(pitch * rows * z); + if (!cb || nvn_deswizzle(&t->d, l, y, decode_source, cb, pitch)) { decoded_ok = 0; free(cb); RED_ONCE(9, "texture deswizzle failed for format %#x target %d", t->d.format, t->d.target); break; } + for (uint32_t zi = 0; zi < z; zi++) + if (nvn_bc_decode(t->d.format, cb + zi * pitch * rows, pitch, w, h, decode_target + off + zi * up * h, up)) decoded_ok = 0; + free(cb); + } + up_blit_add(stg, off, up, up * h, MTLSizeMake(w, h, z), mt, y, l); + off += up * h * z; + continue; + } + if (nvn_deswizzle(&t->d, l, y, t->mem, (uint8_t *)stg.contents + off, pitch)) { RED_ONCE(9, "解排布失败 格式 %#x 目标 %d", t->d.format, t->d.target); break; } + up_blit_add(stg, off, pitch, pitch * rows, MTLSizeMake(w, h, z), mt, y, l); + off += pitch * rows * z; + } + } + if (cached.entry) { + // Staging owns its copy; later LRU eviction cannot change an in-flight + // GPU read. No hash match or GPU-texture contents decide cache hits. + memcpy(stg.contents, cached.decoded, total); + nvn_cpu_decode_finish(&cached, decoded_ok); + } + if (unp && reupload) nvn_cpu_decode_report(atomic_load(&g_cur_frame)); + up_flush(); + return mt; +} +// Zero newly created non-packaged textures: a new Metal texture's content is undefined, while a freshly mapped memory pool on the Switch reads as 0. Games depend on it: SMBW's fountain water surface uses a 512×1 RGBA16F wave texture +// that every frame is "read last frame → render into a render target → copy back" and is never cleared; the undefined initial value keeps swinging through the feedback and pushes the water vertices to the top of the screen (the tall panels above the fountain). +// Copied level by level / slice by slice from one shared all-zero buffer, executed with the upload batch (ordered before this replay). Multisample, depth / stencil and block-compressed formats are not cleared (games always clear them before use, and they cannot be copied into anyway). +static size_t mtl_pf_bytes(MTLPixelFormat pf) { + switch ((int)pf) { + case 10: case 11: case 12: case 13: case 14: return 1; // R8 + case 20: case 22: case 23: case 24: case 25: case 30: case 31: case 32: case 33: case 34: return 2; // R16 / RG8 + case 40: case 41: case 42: case 43: return 2; // packed 16-bit colour + case 53: case 54: case 55: case 60: case 62: case 63: case 64: case 65: + case 70: case 71: case 72: case 73: case 74: case 80: case 81: + case 90: case 91: case 92: case 93: case 94: return 4; // R32 / RG16 / RGBA8 / BGRA8 / 10-10-10-2 / 11-11-10 / 9-9-9-5 + case 103: case 104: case 105: case 110: case 112: case 113: case 114: case 115: return 8; // RG32 / RGBA16 + case 123: case 124: case 125: return 16; // RGBA32 + } + return 0; +} +// Shared all-zero buffer (for zeroing new textures and scissored zero clears): replaced by a bigger one when too small; the old one is released after in-flight submissions finish +static id zero_buf(size_t need) { + static id zb; static size_t zn; + if (need > zn) { + size_t n = MAX(need, (size_t)4 << 20); + id b = [g_dev newBufferWithLength:n options:MTLResourceStorageModeShared]; + if (!b) return nil; + memset(b.contents, 0, n); res_add(b); + if (zb) defer_obj_release(zb); + zb = b; zn = n; + } + return zb; +} +static id astc_zero_buf(size_t need) { + static id zb; static size_t zn; + if (need > zn) { + size_t n = MAX(need, (size_t)1 << 20); + id b = [g_dev newBufferWithLength:n options:MTLResourceStorageModeShared]; + if (!b) return nil; + uint8_t block[16]; nvn_astc_zero_block(block); + for (size_t i = 0; i + sizeof block <= n; i += sizeof block) memcpy((uint8_t *)b.contents + i, block, sizeof block); + res_add(b); if (zb) defer_obj_release(zb); zb = b; zn = n; + } + return zb; +} +static uint64_t g_tex_zeroed; +static void tex_zero_fill(id mt, int astc_bc) { + static int warned; + if (!atomic_load(&g_sub_thread_set) || !on_sub_thread()) return; // the upload batch belongs to the submit thread only + MTLTextureType tt = mt.textureType; + if (tt == MTLTextureType2DMultisample || tt == MTLTextureType2DMultisampleArray || tt == MTLTextureTypeTextureBuffer) return; + if (astc_bc && is_astc4(mt.pixelFormat)) { + size_t need = (((size_t)mt.width + 3) / 4) * (((size_t)mt.height + 3) / 4) * 16; + id zb = astc_zero_buf(need); if (!zb) return; + up_begin(); + NSUInteger slices = mt.arrayLength * (tt == MTLTextureTypeCube || tt == MTLTextureTypeCubeArray ? 6 : 1); + for (NSUInteger l = 0; l < mt.mipmapLevelCount; l++) { + NSUInteger w = MAX(mt.width >> l, 1u), h = MAX(mt.height >> l, 1u), nbx = (w + 3) / 4, nby = (h + 3) / 4; + for (NSUInteger y = 0; y < slices; y++) up_blit_add_fill(zb, 0, nbx * 16, nbx * 16 * nby, MTLSizeMake(w, h, 1), mt, y, l); + } + [g_up_texs addObject:mt]; if (!g_cb) up_flush(); + return; + } + size_t bpp = mtl_pf_bytes(mt.pixelFormat); + if (!bpp) { if (!warned++) MLOG("新建贴图清零:像素格式 %lu 不清(深度 / 模板 / 压缩等)", (unsigned long)mt.pixelFormat); return; } + NSUInteger slices = mt.arrayLength * (tt == MTLTextureTypeCube || tt == MTLTextureTypeCubeArray ? 6 : 1); + size_t need = (size_t)mt.width * bpp * mt.height * mt.depth; // level 0, one slice is the largest + if (need > (64u << 20)) { static int big; if (!big++) MLOG("新建贴图清零:%lux%lux%lu 超过 64 MB,不清", (unsigned long)mt.width, (unsigned long)mt.height, (unsigned long)mt.depth); return; } + id zb = zero_buf(need); if (!zb) return; + up_begin(); + for (NSUInteger l = 0; l < mt.mipmapLevelCount; l++) { + NSUInteger w = MAX(mt.width >> l, 1u), h = MAX(mt.height >> l, 1u), z = tt == MTLTextureType3D ? MAX(mt.depth >> l, 1u) : 1; + for (NSUInteger y = 0; y < slices; y++) up_blit_add_fill(zb, 0, w * bpp, w * bpp * h, MTLSizeMake(w, h, z), mt, y, l); + } + [g_up_texs addObject:mt]; + if (!g_cb) up_flush(); + if (!g_tex_zeroed++) MLOG("新建贴图清零:开(Metal 新贴图内容未定义,Switch 新内存读 0)"); +} + +static int clear_rect_zero(int i); +// Linear textures (LINEAR) in a CPU-visible pool: the game writes the pool memory directly from the CPU (SMBW's glyph cache: 2048×8192 R8, IMAGE|LINEAR, pool CPU_UNCACHED|GPU_CACHED, +// the font engine rasterizes glyphs into it on the CPU). A GPU-private texture never sees those writes → all menu / dialog text is blank. On unified memory, create a linear texture directly on the pool buffer (zero copy): the GPU reads the current content at execution time, same as NVN. +static id tex_linear(const tex_t *t) { + // LINEAR_RENDER_TARGET is a linear layout too (SMBW's sky colour ramps: four 64×1 RGBA16F, written by the CPU every frame) + if (!(t->d.flags & (NVN_TEXTURE_FLAGS_LINEAR | NVN_TEXTURE_FLAGS_LINEAR_RENDER_TARGET)) || !t->mem || !t->pool || (t->pool->flags & NVN_MEMORY_POOL_FLAGS_CPU_NO_ACCESS)) return nil; + if (t->d.target != NVN_TEXTURE_TARGET_2D || t->d.levels > 1 || t->d.samples > 1) return nil; + uint32_t pf = 0; uint8_t swz[4]; int bw, bh, bpb = nvn_fmt_bpb(t->d.format, &bw, &bh); + if (!nvn_fmt_to_mtl(t->d.format, &pf, swz) || !pf || !bpb || bw != 1 || bh != 1) return nil; + size_t row = t->d.stride ? t->d.stride : (size_t)t->d.width * bpb; + void *mb; uint64_t mo; + if (!mtl_addr((uint64_t)t->mem, (uint64_t)row * (t->d.height ? t->d.height : 1), &mb, &mo)) { RED_ONCE(64, "线性贴图的存储不在内存池里(%ux%u 格式 %#x)", t->d.width, t->d.height, t->d.format); return nil; } + id b = (__bridge id)mb; + NSUInteger al = [g_dev minimumLinearTextureAlignmentForPixelFormat:(MTLPixelFormat)pf]; + if (mo % al || row % al) { RED_ONCE(65, "线性贴图偏移 %#llx / 行距 %zu 不满足 %lu 字节对齐,按私有贴图建(CPU 写入不可见)", (unsigned long long)mo, row, (unsigned long)al); return nil; } + MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:(MTLPixelFormat)pf width:t->d.width ? t->d.width : 1 height:t->d.height ? t->d.height : 1 mipmapped:NO]; + d.resourceOptions = b.resourceOptions; + d.usage = MTLTextureUsageShaderRead | (t->d.flags & NVN_TEXTURE_FLAGS_IMAGE ? MTLTextureUsageShaderWrite : 0) | (t->d.flags & NVN_TEXTURE_FLAGS_LINEAR_RENDER_TARGET ? MTLTextureUsageRenderTarget : 0); + return [b newTextureWithDescriptor:d offset:mo bytesPerRow:row]; +} +// CPU-written block-linear textures (WriteTexels): the game gave no packaged layout and changes the content from the CPU each frame (Engage's movie frames: a Y plane R8 + a UV plane RG8, WriteTexels every frame). +// Only packaged textures used to be uploaded from guest memory, so such a texture became a GPU-only texture whose content stayed 0. Their serials are recorded and they resolve through the packaged path (upload + re-upload on dirty pages) +static pthread_mutex_t g_cpu_tex_mu = PTHREAD_MUTEX_INITIALIZER; static CFMutableSetRef g_cpu_tex; +void mtl_tex_cpu_written(const tex_t *t) { + uint64_t serial = t->serial; + if (!serial) return; + pthread_mutex_lock(&g_cpu_tex_mu); + if (!g_cpu_tex) g_cpu_tex = CFSetCreateMutable(NULL, 0, NULL); + int added = 0; + if (!CFSetContainsValue(g_cpu_tex, (const void *)(uintptr_t)serial)) { + CFSetAddValue(g_cpu_tex, (const void *)(uintptr_t)serial); added = 1; + static int n; if (n++ < 16) MLOG("CPU 写贴图 ser=%llu %ux%ux%u 格式 %#x 目标 %d 级 %u:按预排布贴图上传 / 重传", (unsigned long long)serial, t->d.width, t->d.height, t->d.depth, t->d.format, t->d.target, t->d.levels); + } + pthread_mutex_unlock(&g_cpu_tex_mu); + if (added) { pthread_mutex_lock(&g_smu); g_ser_gen++; pthread_mutex_unlock(&g_smu); } // the texture IDs that resolve to it take the slow path, which now treats it as packaged (tex_by_id view_pkg) +} +static int tex_cpu_written(uint64_t serial) { + pthread_mutex_lock(&g_cpu_tex_mu); int r = g_cpu_tex && CFSetContainsValue(g_cpu_tex, (const void *)(uintptr_t)serial); pthread_mutex_unlock(&g_cpu_tex_mu); + return r; +} +static id tex_mtl_(const tex_t *ct); +static id tex_mtl(const tex_t *ct) { AP_B; id r = tex_mtl_(ct); AP_E(4); return r; } +static uint64_t g_texinfo_ser[8]; static int g_texinfo_n; +static id tex_mtl_(const tex_t *ct) { + tex_t *t = (tex_t *)ct; + if (!t || !t->serial) return nil; + if (t->magic != MAGIC('T', 'E', 'X', 0)) { RED_ONCE(70, "贴图对象已失效(magic %#x,首例 ser=%llu):不建 MTLTexture", t->magic, (unsigned long long)t->serial); return nil; } + int packaged = (t->has_layout || tex_cpu_written(t->serial) || linear_needs_upload(t, NULL, NULL)) && t->mem; + { // Diagnostic DM_NVN_TEXINFO=WxH: log storage and description of textures of that size on first resolution (to tell whether two textures share storage and why they were not merged) + static int ti = -1; static uint32_t tw, th; static uint64_t last[64]; static int nl; + if (ti > 0 && t->d.width == tw && t->d.height == th && g_texinfo_n < 8) { int dup = 0; for (int i = 0; i < g_texinfo_n; i++) dup |= g_texinfo_ser[i] == t->serial; if (!dup && !(t->d.flags & 8)) g_texinfo_ser[g_texinfo_n++] = t->serial; } // remember the non-render-target ones (no COMPRESSIBLE) for DM_NVN_TEXRAW=info + if (ti < 0) { const char *e = getenv("DM_NVN_TEXINFO"); ti = e && sscanf(e, "%ux%u", &tw, &th) == 2; } + if (ti && t->d.width == tw && t->d.height == th) { + int seen = 0; for (int i = 0; i < nl; i++) if (last[i] == t->serial) seen = 1; + static uint64_t n; if (!seen && (n++ < 40 || n % 600 == 0)) { if (nl < 64) last[nl++] = t->serial; else last[n % 64] = t->serial; + uint64_t nz = 0; if (t->mem && t->L.size <= (64u << 20)) for (uint64_t i = 0; i < t->L.size; i++) nz += t->mem[i] != 0; // non-zero bytes in guest memory: content the CPU wrote + MLOG("TEXINFO ser=%llu 预排布=%d has_layout=%d mem=%p pool=%p(%#x) off=%#llx 目标=%d 格式=%#x 标志=%#x 采样=%d %ux%ux%u 级=%u 跨距=%u 块高=%d 存储 %llu 字节 非零 %llu", + (unsigned long long)t->serial, packaged, t->has_layout, t->mem, t->pool, t->pool ? t->pool->flags : 0, (unsigned long long)t->off, t->d.target, t->d.format, t->d.flags, t->d.samples, + t->d.width, t->d.height, t->d.depth, t->d.levels, t->d.stride, t->d.pkg_block_h, (unsigned long long)t->L.size, (unsigned long long)nz); } + } + } + id have = tex_get(t->serial); + if (packaged) { + NvPkg *pk = [have isKindOfClass:[NvPkg class]] ? have : nil; // resolved as GPU-only before the CPU wrote it: dropped and rebuilt as packaged + if (pk) { // Already uploaded: check the covered pages only if there has been a guest write since the last check + uint64_t now = nvn_dirty_now(); + int tracked = pk->span != NULL; // kept from the upload: a texture's pool stays registered while the texture lives + int clean = tracked && (now == pk->seen || !nvn_dirty_since_span(pk->span, t->mem, t->L.size, pk->stamp)); + pk->seen = now; + if (clean && atomic_load_explicit(&g_up_lost_n, memory_order_relaxed) && up_lost_take(pk->tex)) { clean = 0; uint64_t k = atomic_load(&g_up_lost_redo); if (k <= 3 || !atomic_load(&g_up_lost_n)) MLOG("上传被丢的贴图 ser=%llu 重传(累计 %llu 张,记录里还剩 %d 张)", (unsigned long long)t->serial, (unsigned long long)k, atomic_load(&g_up_lost_n)); } + if (clean) { + if (dirtycheck() && full_hash(t->mem, t->L.size) != pk->hash && !g_pkg_miss++) + MLOG("【红】脏页漏检:贴图 ser=%llu 池内容已变但未检出", t->serial); + return pk->tex; + } + // Pool content changed (streamed assets written into the same storage, a movie frame): switch to another texture, as in-flight draws still read the old one. That is the spare + // when the GPU is done with it (see NvPkg), else a new one; the old one becomes the spare (pkg_upload_ made nt resident) + id reuse = nil; + if (pk->spare) { pthread_mutex_lock(&g_mu); uint64_t done = g_done; pthread_mutex_unlock(&g_mu); if (pk->spare_fence <= done && pk->spare.usage == pk->tex.usage) reuse = pk->spare; } + uint64_t st; id nt = pkg_upload(t, &st, reuse, 1); + if (nt) { id old = pk->tex; pk->tex = nt; + if (pk->spare && pk->spare != nt) defer_obj_release(pk->spare); // still in use, or of another shape: retired as before + // A write may fault after deswizzling read its source but before + // this upload returns. Keep the checked generation at the + // upload stamp so the next lookup cannot hide that write. + pk->spare = old; pk->spare_fence = g_fence + 1; g_ser_gen++; pk->stamp = st; pk->span = nvn_dirty_span(t->mem, t->L.size); pk->seen = st; g_pkg_reuploads++; if (dirtycheck()) pk->hash = full_hash(t->mem, t->L.size); } + return pk->tex; + } + pk = [NvPkg new]; + pk->tex = pkg_upload(t, &pk->stamp, nil, 0); + if (!pk->tex) return nil; + pk->span = nvn_dirty_span(t->mem, t->L.size); + pk->seen = pk->stamp; g_pkg_uploads++; if (dirtycheck()) pk->hash = full_hash(t->mem, t->L.size); + tex_put(t->serial, pk); + return pk->tex; + } + if (have) return have; + if (t->d.target == 10) { // Buffer texture (TARGET_BUFFER): create the texture-buffer view directly on the memory pool buffer (zero copy; the GPU reads the pool's current content at execution time, same as NVN semantics) + uint32_t pf = 0; uint8_t swz[4]; int bw, bh, bpb = nvn_fmt_bpb(t->d.format, &bw, &bh); + void *mb; uint64_t mo; + if (!nvn_fmt_to_mtl(t->d.format, &pf, swz) || !pf || !bpb) { RED_ONCE(36, "缓冲贴图格式 %#x 未映射", t->d.format); return nil; } + if (!t->mem || !mtl_addr((uint64_t)t->mem, (uint64_t)t->d.width * bpb, &mb, &mo)) { RED_ONCE(37, "缓冲贴图的存储不在内存池里"); return nil; } + id b = (__bridge id)mb; + NSUInteger al = [g_dev minimumLinearTextureAlignmentForPixelFormat:(MTLPixelFormat)pf]; + if (mo % al) { RED_ONCE(38, "缓冲贴图偏移 %#llx 不满足 %lu 字节对齐", (unsigned long long)mo, (unsigned long)al); return nil; } + MTLTextureDescriptor *d = [MTLTextureDescriptor textureBufferDescriptorWithPixelFormat:(MTLPixelFormat)pf width:t->d.width resourceOptions:b.resourceOptions + usage:MTLTextureUsageShaderRead | (t->d.flags & NVN_TEXTURE_FLAGS_IMAGE ? MTLTextureUsageShaderWrite : 0)]; + size_t row = ((size_t)t->d.width * bpb + al - 1) / al * al; + id mt = [b newTextureWithDescriptor:d offset:mo bytesPerRow:row]; + if (!mt) { RED_ONCE(39, "缓冲贴图建不出:%u 元素 格式 %#x", t->d.width, t->d.format); return nil; } + res_add(mt); + tex_put(t->serial, mt); + return mt; + } + alias_key ak; memset(&ak, 0, sizeof ak); ak.pool = (uint64_t)(uintptr_t)t->pool; ak.off = t->off; ak.d = t->d; + pthread_mutex_lock(&g_smu); __unsafe_unretained id shared = hm_get_id(&g_alias, &ak); pthread_mutex_unlock(&g_smu); + if (shared) { tex_put(t->serial, shared); return shared; } + id mt = tex_linear(t); + if (!mt) { + MTLTextureDescriptor *d = tex_desc(t, 0); + if (!d) return nil; + mt = [g_dev newTextureWithDescriptor:d]; + if (!mt) { RED_ONCE(2, "建 MTLTexture 失败 %ux%u 格式 %#x 目标 %d", t->d.width, t->d.height, t->d.format, t->d.target); return nil; } + tex_tag_format(mt, t->d.format); + res_add(mt); { uint32_t apf; tex_zero_fill(mt, bc_astc(t, &apf)); } // register residency before zeroing (the copy destination must be resident too) + } + tex_tag_format(mt, t->d.format); + if (g_allocprof > 0) { static int n; if (n++ < 30 || n % 500 == 0) MLOG("新建贴图(%s)#%d ser=%llu %ux%ux%u 格式 %#x 目标 %d 级 %u 存储 %p", __func__, n, (unsigned long long)t->serial, t->d.width, t->d.height, t->d.depth, t->d.format, t->d.target, t->d.levels, (void *)t->mem); } + if (getenv("DM_NVN_TEXCPU") && t->pool && !(t->pool->flags & NVN_MEMORY_POOL_FLAGS_CPU_NO_ACCESS)) MLOG("CPU 可见池上的非预排布贴图 ser=%llu %ux%ux%u 格式 %#x 目标 %d 级 %u 贴图标志 %#x 池标志 %#x 存储 %p", (unsigned long long)t->serial, t->d.width, t->d.height, t->d.depth, t->d.format, t->d.target, t->d.levels, t->d.flags, t->pool->flags, (void *)t->mem); + res_add(mt); + pthread_mutex_lock(&g_smu); + if (!g_alias_set) g_alias_set = CFSetCreateMutable(NULL, 0, NULL); + hm_put_id(&g_alias, &ak, mt); CFSetAddValue(g_alias_set, (__bridge const void *)mt); + pthread_mutex_unlock(&g_smu); + tex_put(t->serial, mt); + return mt; +} +// Live texture memory, bucketed by the guest NVN source format and the actual +// Metal storage. This is emitted at ordinary memory samples and upload-batch +// diagnostics so a jetsam between frame samples still leaves useful evidence. +static const void *const k_tex_fmt_key = &k_tex_fmt_key; +static void tex_tag_format(id mt, uint32_t nvn_fmt) { + if (mt) objc_setAssociatedObject(mt, k_tex_fmt_key, @(nvn_fmt), OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} +static int tex_src_class(uint32_t f) { + switch (f) { + case 0x41: case 0x42: case 0x45: case 0x46: return 1; + case 0x43: case 0x47: return 2; + case 0x44: case 0x48: return 3; + case 0x49: case 0x4a: return 4; + case 0x4b: case 0x4c: return 5; + case 0x4f: case 0x50: return 6; + case 0x4d: case 0x4e: return 7; + default: return 0; + } +} +static void tex_format_stat(void) { + enum { TEX_SRC_CLASSES = 8, TEX_STORAGE_CLASSES = 4 }; + static const char *const src[] = {"non-BC", "BC1", "BC2", "BC3", "BC4", "BC5", "BC6H", "BC7"}; + static const char *const dst[] = {"as-is", "native-BC", "ASTC", "decompressed"}; + _Static_assert(sizeof src / sizeof *src == TEX_SRC_CLASSES, "texture source bucket count"); + _Static_assert(sizeof dst / sizeof *dst == TEX_STORAGE_CLASSES, "texture storage bucket count"); + uint64_t n[TEX_SRC_CLASSES][TEX_STORAGE_CLASSES] = {{0}}, bytes[TEX_SRC_CLASSES][TEX_STORAGE_CLASSES] = {{0}}; + uint64_t tex_bytes = 0, untagged_bytes = 0, buffer_bytes = 0, other_bytes = 0; + uint64_t tex_n = 0, untagged_n = 0, buffer_n = 0, other_n = 0; + @autoreleasepool { + // Take one strong point-in-time snapshot while holding only the + // residency lock. Classification and logging stay outside the lock: + // they may call Objective-C runtime / Metal accessors and must not + // introduce a g_res_mu -> logging or cache-lock ordering edge. + pthread_mutex_lock(&g_res_mu); + NSArray *roots = g_res_objects ? g_res_objects.allObjects : @[]; + pthread_mutex_unlock(&g_res_mu); + + CFMutableSetRef seen_tex = CFSetCreateMutable(NULL, 0, NULL); + CFMutableSetRef seen_buf = CFSetCreateMutable(NULL, 0, NULL); + for (id resource in roots) { + if ([resource conformsToProtocol:@protocol(MTLBuffer)]) { + const void *key = (__bridge const void *)resource; + if (!CFSetContainsValue(seen_buf, key)) { + CFSetAddValue(seen_buf, key); buffer_n++; buffer_bytes += resource.allocatedSize; + } + continue; + } + if (![resource conformsToProtocol:@protocol(MTLTexture)]) { + other_n++; other_bytes += resource.allocatedSize; + continue; + } + + // Views are not allocations. Canonicalize any accidentally + // registered view to its root so a base + view pair is counted + // once. A buffer-backed texture is accounted as its root buffer. + id mt = (id)resource; + while (mt.parentTexture) mt = mt.parentTexture; + if (mt.buffer) { + id buffer = mt.buffer; + const void *key = (__bridge const void *)buffer; + if (!CFSetContainsValue(seen_buf, key)) { + CFSetAddValue(seen_buf, key); buffer_n++; buffer_bytes += buffer.allocatedSize; + } + continue; + } + const void *key = (__bridge const void *)mt; + if (CFSetContainsValue(seen_tex, key)) continue; + CFSetAddValue(seen_tex, key); + + uint64_t sz = mt.allocatedSize; tex_n++; tex_bytes += sz; + NSNumber *fmt = objc_getAssociatedObject(mt, k_tex_fmt_key); + if (!fmt) { untagged_n++; untagged_bytes += sz; continue; } + int c = tex_src_class(fmt.unsignedIntValue); + int st = !c ? 0 : is_native_bc(mt.pixelFormat) ? 1 : is_astc4(mt.pixelFormat) ? 2 : 3; + n[c][st]++; bytes[c][st] += sz; + } + CFRelease(seen_tex); CFRelease(seen_buf); + } + char line[896]; int at = snprintf(line, sizeof line, "%llu resident root textures, %.1f MB:", (unsigned long long)tex_n, tex_bytes / 1048576.0); + for (int c = 0; c < TEX_SRC_CLASSES && at < (int)sizeof line - 112; c++) for (int st = 0; st < TEX_STORAGE_CLASSES && at < (int)sizeof line - 112; st++) if (n[c][st]) + at += snprintf(line + at, sizeof line - at, " %s/%s %llu textures %.1f MB;", src[c], dst[st], (unsigned long long)n[c][st], bytes[c][st] / 1048576.0); + snprintf(line + at, sizeof line - at, " untagged textures %llu %.1f MB; root buffers %llu %.1f MB; other resources %llu %.1f MB", + (unsigned long long)untagged_n, untagged_bytes / 1048576.0, + (unsigned long long)buffer_n, buffer_bytes / 1048576.0, + (unsigned long long)other_n, other_bytes / 1048576.0); + MLOG("texture memory by format: %s", line); +} +// Memory readout line (always on, low frequency, capped): resident memory, GPU allocated, deferred-release queue, residency set object count, registered memory pools / peak / tracking table capacity, cumulative texture re-uploads. +// Purpose: pin down "crashes with no log". The system gives no notice before killing a process for memory, so only readouts left beforehand can show which number kept climbing. +// Called by the frame hook every 300 frames; printed only when resident or GPU allocated changed by ≥ 64 MB since the last line, otherwise one line every 3000 frames; capped at 400 lines per game. All integers (the frame hook runs on the guest thread's small stack, so no floating-point formatting) +void mtl_mem_stat(unsigned long long frame, unsigned long long rss_mb) { + static unsigned long long last_rss, last_gpu, last_frame; static int lines; unsigned long long gpu = (unsigned long long)(g_dev.currentAllocatedSize >> 20); + unsigned long long dr = rss_mb > last_rss ? rss_mb - last_rss : last_rss - rss_mb, dg = gpu > last_gpu ? gpu - last_gpu : last_gpu - gpu; + if (lines >= 400 || (lines && dr < 64 && dg < 64 && frame - last_frame < 3000)) return; lines++; last_rss = rss_mb; last_gpu = gpu; last_frame = frame; + pthread_mutex_lock(&g_deferred_mu); unsigned long nd = (unsigned long)g_deferred.count; pthread_mutex_unlock(&g_deferred_mu); + pthread_mutex_lock(&g_res_mu); unsigned long nr = (unsigned long)g_res_objects.count; pthread_mutex_unlock(&g_res_mu); + int live = 0, peak = 0, cap = 0; nvn_dirty_stat(&live, &peak, &cap); + pthread_mutex_lock(&g_smu); unsigned ns = g_by_serial.n, ashared = g_alias.n, nghost = g_ghost.n; uint64_t ghost_puts = g_ghost_puts; pthread_mutex_unlock(&g_smu); + unsigned long dead_n = 0; unsigned long long dead_mb = 0; + // Objects read from a weak table come back retained + autoreleased: the frame hook runs on a guest thread with no autorelease pool that drains, so without this wrapper every buffer counted would be pinned for good (the readout creating the leak itself) + int pres = 0; @autoreleasepool { pthread_mutex_lock(&g_mu); pres = g_pool_res_n; for (id b in g_dead_pools) { dead_n++; dead_mb += b.length; } pthread_mutex_unlock(&g_mu); } dead_mb >>= 20; // host objects held by serial (textures / packaged textures / programs): if it climbs along with GPU allocated, textures are not being released + // os_proc_available_memory: how far this process is from being terminated by the system for memory, iOS only (from d2r-mobile 704147218); the Mac writes 「无」(none). On a phone, a crash with no log is most likely this number reaching 0 + char avail[32] = "无"; +#if TARGET_OS_IPHONE + snprintf(avail, sizeof avail, "%llu MB", (unsigned long long)(os_proc_available_memory() >> 20)); +#endif + MLOG("内存 至帧 %llu:常驻 %llu MB,显卡已分配 %llu MB,延迟释放队列 %lu,驻留集 %lu 个,序号对象 %u 个,内存池 %d 个(其中已驻留 %d;峰值 %d / 跟踪表容量 %d;已释放仍活 %lu 个 %llu MB),共用贴图 %u 张(随池淘汰累计 %llu),贴图重传累计 %llu 次,每帧竞技场累计新建 %llu 块,已 Finalize 贴图小表 %u 张(累计 %llu);离系统内存上限还有 %s", frame, rss_mb, gpu, nd, nr, ns, live, pres, peak, cap, dead_n, dead_mb, ashared, (unsigned long long)g_alias_evicted, (unsigned long long)g_pkg_reuploads, (unsigned long long)g_arena_made, nghost, (unsigned long long)ghost_puts, avail); + tex_format_stat(); + if (g_allocprof > 0) { + MLOG("显卡分配增长按函数:结束通道 %llu MB,开通道 %llu MB,取管线 %llu MB,参数缓冲 %llu MB,取贴图 %llu MB", g_ap_fn[0] >> 20, g_ap_fn[1] >> 20, g_ap_fn[2] >> 20, g_ap_fn[3] >> 20, g_ap_fn[4] >> 20); } + if (g_allocprof > 0) { int top[4] = {-1, -1, -1, -1}; for (int o = 0; o < 64; o++) for (int k = 0; k < 4; k++) if (top[k] < 0 || g_ap_bytes[o] > g_ap_bytes[top[k]]) { for (int j = 3; j > k; j--) top[j] = top[j - 1]; top[k] = o; break; } + MLOG("显卡分配增长按操作:op%d %llu MB,op%d %llu MB,op%d %llu MB,op%d %llu MB", top[0], g_ap_bytes[top[0]] >> 20, top[1], g_ap_bytes[top[1]] >> 20, top[2], g_ap_bytes[top[2]] >> 20, top[3], g_ap_bytes[top[3]] >> 20); } +} +void mtl_pkg_stats(uint64_t *uploads, uint64_t *reuploads) { *uploads = g_pkg_uploads; *reuploads = g_pkg_reuploads; } +uint64_t mtl_pkg_miss(void) { return g_pkg_miss; } +void mtl_tex_release(tex_t *t) { + if (!t->serial) return; + pthread_mutex_lock(&g_smu); nvn_hm_del(&g_tex_live, &t->serial); pthread_mutex_unlock(&g_smu); + serial_del(t->serial); +} + +// ---------- Render passes: SetRenderTargets opens a new pass; clears before the first command in the pass are folded into the load action ---------- +static struct { + p_set_rt rt; int has_rt; + uint8_t clear_c[8]; MTLClearColor cc[8]; + uint8_t clear_d, clear_s; double cd; uint32_t cs; + p_scissor sc; int has_sc; +} R; + +// ---------- Zero-source skip ---------- +// Shader fact table (statically proven keys): 'the textures it samples are all 0 and the coefficient is finite ⇒ the output is all 0'. Coefficient = component comp of vector vec of fragment-stage constant buffer register ubo_slot. +// 4a819818b8e489c5 ps_outline_blur_p: output = (sum of several samples of the same texture along a row / column) × fp_c7._m0[1].x (fp_c7 = cbuf reg 4), homogeneous linear in the sampled values, no constant term +// (source of the proof: dmsh_b//4a819818b8e489c5_b.metal, read line by line). Add other shaders here once they are proven. +typedef struct { char key[17]; int ubo_slot, vec, comp; } zskip_fact; +static zskip_fact g_zskip_facts[16] = {{"4a819818b8e489c5", 4, 1, 0}}; +static int g_zskip_nf = 1; +void zskip_test_add(const char *key, int ubo_slot, int vec, int comp) { + if (!key || g_zskip_nf >= 16) return; + snprintf(g_zskip_facts[g_zskip_nf].key, sizeof g_zskip_facts[0].key, "%s", key); + g_zskip_facts[g_zskip_nf].ubo_slot = ubo_slot; g_zskip_facts[g_zskip_nf].vec = vec; g_zskip_facts[g_zskip_nf].comp = comp; g_zskip_nf++; +} +int zskip_test_count(void) { return g_zskip_count; } +static int g_zskip_env = -1; // Cache of DM_NVN_ZSKIP (−1 = not read). This function is called on every draw, and getenv once took about 8% of the submit path; unit tests set it back to −1 after toggling the switch mid-run +static int zskip_on(void) { if (g_zskip_env < 0) { const char *e = getenv("DM_NVN_ZSKIP"); g_zskip_env = !e || strcmp(e, "0"); } return g_zskip_env && tbdr_on(); } +static void zskip_clear(void) { g_zskip_n = 0; memset(g_zskip_cand_ok, 0, sizeof g_zskip_cand_ok); } +static void zskip_del(uint64_t ser) { for (int i = 0; i < g_zskip_n;) if (g_zskip_zero[i].ser == ser) g_zskip_zero[i] = g_zskip_zero[--g_zskip_n]; else i++; } +static int zskip_has(uint64_t ser, int lv, int sl) { for (int i = 0; i < g_zskip_n; i++) if (g_zskip_zero[i].ser == ser && g_zskip_zero[i].level == lv && g_zskip_zero[i].slice == sl) return 1; return 0; } +static void zskip_add(uint64_t ser, int lv, int sl) { if (!ser || zskip_has(ser, lv, sl) || g_zskip_n >= 4096) return; g_zskip_zero[g_zskip_n++] = (zskip_zero){ser, (uint16_t)lv, (uint16_t)sl}; } +static int zskip_zero_clear(int i) { return R.clear_c[i] && R.cc[i].red == 0 && R.cc[i].green == 0 && R.cc[i].blue == 0 && R.cc[i].alpha == 0; } +static void zskip_commit_clears(void) { for (int i = 0; i < 8; i++) if (g_zskip_cand_ok[i]) zskip_add(g_zskip_cand[i].ser, g_zskip_cand[i].level, g_zskip_cand[i].slice); } +static int zskip_target_zero(const tex_t *t, const view_t *v, int hv) { int lv = hv && v->set_levels ? v->base_level : 0, sl = hv && v->set_layers ? v->min_layer : 0; return t && zskip_has(t->serial, lv, sl); } +#ifdef NVN_RT_RANGE_TEST +static int g_rr_checking; +static void rr_begin(void), rr_finish(int ranged); +static void rr_snap(const p_draw *, const p_draw_idx *); +static void rr_reset(void); +#endif +static id g_enc; +static int enc_open(void) { return g_enc != nil; } +// Target info of the current render pass: determined once from the attachments at rp_begin and shared by all draws in the pass (pipeline key, depth-stencil, viewport / scissor all depend only on it) +static struct { uint32_t cfmt[8], dfmt, sfmt, samples; int hd, hs; NSUInteger tw, th; uint64_t serial, reg_gen, ser_gen, range_dirty; int range_external, range_depth_nonro; __unsafe_unretained id range_color, range_depth; } g_pass; +static id g_enc_pso; static id g_enc_dss; // Already set on this encoder (not set again if identical) +// Dynamic state already sent on the open render encoder: each draw used to send all nine (stencil reference, viewport, scissor, cull, winding, fill, depth bias, depth clip, blend colour); +// now a value is sent only when it differs. g_ed.ok holds one bit per item and is cleared for every new encoder and after a clear triangle (which sets its own) +static struct { unsigned ok; uint32_t sref; MTLViewport vp; MTLScissorRect sc; int cull, wind, fill, clip; float bias[3], blend[4]; } g_ed; +enum { ED_SREF = 1, ED_VP = 2, ED_SC = 4, ED_CULL = 8, ED_WIND = 16, ED_FILL = 32, ED_BIAS = 64, ED_CLIP = 128, ED_BLEND = 256 }; +static int g_enc_vis, g_enc_has_vis; // This encoder's current visibility count mode (0 off / slot number + 1), and whether a visibility buffer is attached +static int g_pso_dirty = 1, g_dss_dirty = 1; // Pipeline / depth-stencil related state was changed during replay +static uint64_t g_stat_args_enc, g_stat_args_reuse; // Argument buffer: new-encode / reuse counts +static uint64_t g_stat_draw, g_stat_pass, g_bw_load, g_bw_store, g_pass_full_rw, g_udc_saved_bytes; // Bandwidth ledger: attachment load / store bytes, number of full load-store passes +// DM_NVN_PASSDUMP: records pass attachments / samples / presents one by one (textures by serial) for offline data-flow analysis; the pass numbers of P / D / R / S lines agree (= g_stat_pass after the pass is opened) +// DM_NVN_PASSDUMP=frame number, or start-end (inclusive at both ends) +static int g_kd_force; // the draw DM_NVN_KEYDUMP matched: force the per-binding records on (see kd_draw) +static _Atomic uint64_t g_passdump_live; // Upper 56 bits: start frame; lower 8 bits: count. +static int passdump(void) { + if (g_kd_force) return 1; +#ifdef NVN_RT_RANGE_TEST + if (g_rr_checking) return 0; +#endif + uint64_t live = atomic_load_explicit(&g_passdump_live, memory_order_acquire); + if (live) { + uint64_t frame = atomic_load(&g_cur_frame), start = live >> 8, count = live & 255; + if (frame >= start && frame - start < count) return 1; + } + static long a0 = -2, a1; if (a0 == -2) { const char *e = getenv("DM_NVN_PASSDUMP"); a0 = e ? atol(e) : -1; a1 = e && strchr(e, '-') ? atol(strchr(e, '-') + 1) : a0; } + long f = (long)atomic_load(&g_cur_frame); return a0 >= 0 && f >= a0 && f <= a1; +} +#define PD(...) do { if (passdump()) { fprintf(stderr, "[pass] F%llu ", (unsigned long long)atomic_load(&g_cur_frame)); fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); } } while (0) +static void blit_note(void) { if (g_enc) PD("E %llu 拷贝 / 缓冲更新结束渲染通道", g_stat_pass); } +// DM_GFXUSE=: once the character select screen is stable (starting 1 second after the first OpenPanel MainMenuButtonRibbon, requires DM_BOOTPROF=1), over 30 consecutive frames, +// the ranges in guest memory of the resources the GPU actually reads (texture storage, UBO / SSBO, vertex / index buffers), deduplicated by (kind, address, length), one line recorded on first sight. +// For intersecting with the inflate output ranges (which data decompressed before character select is really used by the character select screen). Columns: kind guest_addr len first_frame tex_id fmt w×h +__attribute__((weak)) uint64_t dm_prof_ribbon_ns(void) { return 0; } // diag.c provides the strong definition; when not merged it is always 0 → the time window does not open, and a failure is reported at the end +__attribute__((weak)) void dm_shot_written(const char *path, int ok) { (void)path; (void)ok; } +__attribute__((weak)) void dm_guest_frame_tick(void) {} // hle_os.c provides the strong definition (DM_DETCLOCK deterministic guest clock); a no-op when graphics unit tests do not link the host // diag.c provides the strong definition (DM_TICK_HOLD=E:SHOT unfreeze); a no-op when graphics unit tests do not link the host +__attribute__((weak)) int dm_detclock_enabled(void) { return 0; } // Same as above: the host's hle_os.c provides the strong definition; when graphics unit tests do not link the host, the deterministic clock is treated as off +__attribute__((weak)) uint64_t guest_ns(void) { return 0; } // Same as above: read only when the deterministic clock is on; never reached in graphics unit tests +enum { USE_OFF, USE_WAIT, USE_REC, USE_DONE }; +static int g_use = -1; static FILE *g_use_f; static long g_use_f0, g_use_rows; static nvn_hmap g_use_seen = {.klen = 24}; +#define USE_FRAMES 30 +static void use_exit(void) { + if (g_use == USE_WAIT) fprintf(stderr, "[dm] 【红】DM_GFXUSE 时间窗始终没开:dm_prof_ribbon_ns() = %llu(需 DM_BOOTPROF=1 且宿主含面板打点)\n", (unsigned long long)dm_prof_ribbon_ns()); + else if (g_use == USE_REC) fprintf(stderr, "[dm] 【红】DM_GFXUSE 只记了 %ld 帧(不足 %d)即退出,文件未完整\n", (long)atomic_load(&g_cur_frame) - g_use_f0, USE_FRAMES); + if (g_use_f) fflush(g_use_f); +} +static void use_tick(void) { // Called on every present + if (g_use < 0) { const char *e = getenv("DM_GFXUSE"); g_use = USE_OFF; + if (e) { g_use_f = fopen(e, "w"); if (!g_use_f) { MLOG("【红】DM_GFXUSE 打不开 %s", e); return; } + fprintf(g_use_f, "kind\tguest_addr\tlen\tfirst_frame\ttex_id\tfmt\twxh\n"); fflush(g_use_f); g_use = USE_WAIT; atexit(use_exit); + MLOG("[gfxuse] 等面板事件:首次 OpenPanel MainMenuButtonRibbon 后 1 秒起记 %d 帧 → %s", USE_FRAMES, e); } } + long f = (long)atomic_load(&g_cur_frame); + if (g_use == USE_WAIT) { + uint64_t t = dm_prof_ribbon_ns(); + if (t && now_ns() - t >= 1000000000ull) { g_use = USE_REC; g_use_f0 = f; MLOG("[gfxuse] 开始记录:帧 %ld 起 %d 帧", f, USE_FRAMES); } + } else if (g_use == USE_REC && f - g_use_f0 >= USE_FRAMES) { + g_use = USE_DONE; fclose(g_use_f); g_use_f = NULL; + MLOG("[gfxuse] 记录完成:帧 %ld–%ld,去重后 %ld 个区间", g_use_f0, f - 1, g_use_rows); + } +} +static __attribute__((always_inline)) inline void use_rec(const char *kind, uint64_t addr, uint64_t len, int tid, const tex_t *t) { + if (g_use != USE_REC || !addr || !len) return; + uint64_t k[3] = {addr, len, (uint64_t)(uint8_t)kind[0]}; + if (nvn_hm_get(&g_use_seen, k)) return; + nvn_hm_put(&g_use_seen, k, (void *)1); + g_use_rows++; + if (t) fprintf(g_use_f, "%s\t%#llx\t%llu\t%lld\t%d\t%#x\t%ux%u\n", kind, (unsigned long long)addr, (unsigned long long)len, (long long)atomic_load(&g_cur_frame), tid, t->d.format, t->d.width, t->d.height); + else fprintf(g_use_f, "%s\t%#llx\t%llu\t%lld\t\t\t\n", kind, (unsigned long long)addr, (unsigned long long)len, (long long)atomic_load(&g_cur_frame)); +} +static uint64_t att_bytes(id t, NSUInteger level) { + if (!t) return 0; + NSUInteger w = MAX(t.width >> level, 1), h = MAX(t.height >> level, 1); + NSUInteger bpp = t.pixelFormat == MTLPixelFormatDepth32Float_Stencil8 ? 5 : t.pixelFormat == MTLPixelFormatRGBA16Float || t.pixelFormat == MTLPixelFormatRG32Float ? 8 : + t.pixelFormat == MTLPixelFormatRGBA32Float ? 16 : t.pixelFormat == MTLPixelFormatR8Unorm || t.pixelFormat == MTLPixelFormatStencil8 ? 1 : + t.pixelFormat == MTLPixelFormatRG8Unorm || t.pixelFormat == MTLPixelFormatR16Float ? 2 : 4; + return (uint64_t)w * h * bpp * t.sampleCount; +} +// The render attachment selects a particular mip, while MTLTexture.width/height still describe the base level. Empty attachments do not take part in the bounds. +static NSUInteger mip_extent(NSUInteger size, NSUInteger level) { + return level < sizeof(size) * 8 ? MAX(size >> level, 1) : 1; +} +static void pass_dims(MTLRenderPassDescriptor *pd, NSUInteger nc, NSUInteger *w, NSUInteger *h) { + *w = *h = 0; + for (NSUInteger i = 0; i < nc + 2; i++) { + MTLRenderPassAttachmentDescriptor *a = i < nc ? pd.colorAttachments[i] : i == nc ? pd.depthAttachment : pd.stencilAttachment; + if (!a.texture) continue; + NSUInteger aw = mip_extent(a.texture.width, a.level), ah = mip_extent(a.texture.height, a.level); + *w = *w ? MIN(*w, aw) : aw; *h = *h ? MIN(*h, ah) : ah; + } + if (!*w) { *w = MAX(pd.renderTargetWidth, 1); *h = MAX(pd.renderTargetHeight, 1); } +} + +// ---------- DM_NVN_PASSGPU=: per-pass GPU time (stage-boundary timestamp counters) ---------- +// Each render pass samples 4 points (vertex start/end, fragment start/end), a compute pass 2 points; resolved after the command buffer completes and accumulated by 'index of the pass within the frame'. +// Each line: frame idx kind gpu_ms inc_ms w h fmt0 dfmt load draws t0_ns t1_ns (kind: R render / C compute; load: L load / C clear for each color attachment + depth) +// On TBDR passes overlap in the pipeline (the later pass's vertex stage is earlier than the end of the earlier pass's fragment stage): gpu_ms = this pass's start-to-end span (including queueing) and cannot be summed; +// attribution uses the timeline increment inc_ms = t1 − max(t0, latest t1 of the passes completed so far); the sum of the increments of all passes = total time the GPU is busy (command buffers complete in submission order) +typedef struct { uint64_t frame, pass; int idx, kind, w, h, draws, slot; uint32_t cfmt0, dfmt; char load[12]; } pg_pass; // pass: global pass number (same as the pass number in the DM_NVN_PASSDUMP lines, for joining the two records) +#define PG_SLOTS 4096 // Counter sample buffer limit 32768 bytes = 4096 timestamps (measured from the error on this machine) +#define PG_RING 8 +static int g_pg = -1; static FILE *g_pg_f; +static id g_pg_buf[PG_RING]; static int g_pg_b; static uint32_t g_pg_used; +static pg_pass *g_pg_pass; static int g_pg_np, g_pg_cap, g_pg_open = -1; +static uint64_t g_pg_frame = ~0ull; static int g_pg_idx; +static pthread_mutex_t g_pg_mu = PTHREAD_MUTEX_INITIALIZER; +static inline void args_dirty_all(void); +typedef struct { double sum, last; uint64_t n; pg_pass meta; } pg_stat; +static pg_stat g_pg_st[256]; static uint64_t g_pg_rep_frame; +static int pg_on(void) { + if (g_pg >= 0) return g_pg; + const char *e = getenv("DM_NVN_PASSGPU"); g_pg = 0; + if (!e) return 0; + if (![g_dev supportsCounterSampling:MTLCounterSamplingPointAtStageBoundary]) { MLOG("【红】DM_NVN_PASSGPU:本设备不支持阶段边界计数采样"); return 0; } + id ts = nil; for (id c in g_dev.counterSets) if ([c.name isEqualToString:MTLCommonCounterSetTimestamp]) ts = c; + if (!ts) { MLOG("【红】DM_NVN_PASSGPU:没有时间戳计数器集"); return 0; } + MTLCounterSampleBufferDescriptor *d = [MTLCounterSampleBufferDescriptor new]; + d.counterSet = ts; d.storageMode = MTLStorageModeShared; d.sampleCount = PG_SLOTS; + for (int i = 0; i < PG_RING; i++) { NSError *er = nil; g_pg_buf[i] = [g_dev newCounterSampleBufferWithDescriptor:d error:&er]; if (!g_pg_buf[i]) { MLOG("【红】计数采样缓冲建不出:%s", er.localizedDescription.UTF8String); return 0; } } + if (!(g_pg_f = fopen(e, "w"))) { MLOG("【红】DM_NVN_PASSGPU 打不开 %s", e); return 0; } + fprintf(g_pg_f, "frame\tidx\tkind\tgpu_ms\tinc_ms\tw\th\tfmt0\tdfmt\tload\tdraws\tt0_ns\tt1_ns\tpass\n"); + MLOG("[passgpu] 逐通道 GPU 计时 → %s", e); + return g_pg = 1; +} +static void pg_sub_begin(uint64_t fence) { // This submission uses block fence % PG_RING of the ring; first wait for the submission that used it PG_RING submissions ago to complete + if (!pg_on()) return; + if (fence > PG_RING) mtl_wait(fence - PG_RING, UINT64_MAX); + g_pg_b = (int)(fence % PG_RING); g_pg_used = 0; g_pg_np = 0; g_pg_open = -1; +} +static pg_pass *pg_new(int kind, int nslot) { + if (!pg_on() || g_pg_used + nslot > PG_SLOTS) return NULL; + uint64_t f = atomic_load(&g_cur_frame); if (f != g_pg_frame) { g_pg_frame = f; g_pg_idx = 0; } + if (g_pg_np == g_pg_cap) { g_pg_cap = g_pg_cap ? g_pg_cap * 2 : 128; g_pg_pass = realloc(g_pg_pass, g_pg_cap * sizeof *g_pg_pass); } + pg_pass *p = &g_pg_pass[g_pg_np++]; memset(p, 0, sizeof *p); + p->frame = f; p->pass = g_stat_pass; p->idx = g_pg_idx++; p->kind = kind; p->slot = (int)g_pg_used; p->draws = -(int)g_stat_draw; + g_pg_used += nslot; + return p; +} +static void pg_render(MTLRenderPassDescriptor *pd, NSUInteger nc) { + (void)pd; (void)nc; + return; +#if 0 + pg_pass *p = pg_new(0, 4); if (!p) return; + MTLRenderPassSampleBufferAttachmentDescriptor *a = pd.sampleBufferAttachments[0]; + a.sampleBuffer = g_pg_buf[g_pg_b]; + a.startOfVertexSampleIndex = p->slot; a.endOfVertexSampleIndex = p->slot + 1; a.startOfFragmentSampleIndex = p->slot + 2; a.endOfFragmentSampleIndex = p->slot + 3; + id t0 = nil; int k = 0; + for (NSUInteger i = 0; i < nc && i < 8; i++) { id t = pd.colorAttachments[i].texture; if (!t) continue; + if (!t0) t0 = t; if (k < 10) p->load[k++] = pd.colorAttachments[i].loadAction == MTLLoadActionClear ? 'C' : 'L'; } + id dt = pd.depthAttachment.texture; + if (dt) { if (k < 10) { p->load[k++] = '|'; p->load[k++] = pd.depthAttachment.loadAction == MTLLoadActionClear ? 'C' : 'L'; } p->dfmt = (uint32_t)dt.pixelFormat; } + if (!t0) t0 = dt; + NSUInteger pw, ph; pass_dims(pd, nc, &pw, &ph); + p->w = (int)pw; p->h = (int)ph; p->cfmt0 = t0 && t0 != dt ? (uint32_t)t0.pixelFormat : 0; + g_pg_open = g_pg_np - 1; +#endif +} +static id pg_compute(void) { + pg_pass *p = pg_new(1, 2); + ser_gate(); + if (!p) { args_dirty_all(); return enc_c_begin(NULL); } + MTLComputePassDescriptor *cd = [MTLComputePassDescriptor computePassDescriptor]; + cd.sampleBufferAttachments[0].sampleBuffer = g_pg_buf[g_pg_b]; + cd.sampleBufferAttachments[0].startOfEncoderSampleIndex = p->slot; cd.sampleBufferAttachments[0].endOfEncoderSampleIndex = p->slot + 1; + p->draws = 0; + args_dirty_all(); + return enc_c_begin(cd); +} +static void pg_end_render(void) { if (g_pg_open >= 0) { g_pg_pass[g_pg_open].draws += (int)g_stat_draw; g_pg_open = -1; } } +static void pg_commit(id cb) { // Hand this submission's pass table to the completion callback for resolving + if (!g_pg || !g_pg_np) return; + int n = g_pg_np; pg_pass *ps = malloc(n * sizeof *ps); memcpy(ps, g_pg_pass, n * sizeof *ps); + id buf = g_pg_buf[g_pg_b]; uint32_t used = g_pg_used; + [cb addCompletedHandler:^(id c) { + (void)c; + NSData *d = [buf resolveCounterRange:NSMakeRange(0, used)]; + const MTLCounterResultTimestamp *ts = d.bytes; + pthread_mutex_lock(&g_pg_mu); + for (int i = 0; d && i < n; i++) { + pg_pass *p = &ps[i]; uint64_t a, b; + #define OK(x) ((x) != MTLCounterErrorValue && (x) != 0) + if (p->kind == 0) { a = OK(ts[p->slot].timestamp) ? ts[p->slot].timestamp : ts[p->slot + 2].timestamp; b = OK(ts[p->slot + 3].timestamp) ? ts[p->slot + 3].timestamp : ts[p->slot + 1].timestamp; } + else { a = ts[p->slot].timestamp; b = ts[p->slot + 1].timestamp; } + if (!OK(a) || !OK(b) || b < a) continue; + #undef OK + static uint64_t prev_end; // Latest end time of the passes completed so far (completion callbacks are serialized in submission order) + double ms = (b - a) / 1e6; // Timestamps are in ns, same as the CPU (measured 1:1 via sampleTimestamps on this machine) + double inc = b > prev_end ? (b - (a > prev_end ? a : prev_end)) / 1e6 : 0; + if (b > prev_end) prev_end = b; + fprintf(g_pg_f, "%llu\t%d\t%c\t%.4f\t%.4f\t%d\t%d\t%u\t%u\t%s\t%d\t%llu\t%llu\t%llu\n", (unsigned long long)p->frame, p->idx, p->kind ? 'C' : 'R', ms, inc, + p->w, p->h, p->cfmt0, p->dfmt, p->load, p->draws, (unsigned long long)a, (unsigned long long)b, (unsigned long long)p->pass); + if (p->idx < 256 && p->frame > 300) { pg_stat *st = &g_pg_st[p->idx]; st->sum += inc; st->n++; st->last = inc; st->meta = *p; } + if (p->frame >= g_pg_rep_frame + 300 && p->frame > 300) { // Report the top five every 300 frames (accumulated from frame 300) + g_pg_rep_frame = p->frame; double tot = 0; int top[5] = {-1, -1, -1, -1, -1}; + for (int k = 0; k < 256; k++) if (g_pg_st[k].n) { double m = g_pg_st[k].sum / g_pg_st[k].n; tot += m; + for (int j = 0; j < 5; j++) if (top[j] < 0 || m > g_pg_st[top[j]].sum / g_pg_st[top[j]].n) { for (int q = 4; q > j; q--) top[q] = top[q - 1]; top[j] = k; break; } } + char line[1024]; size_t o = 0; + for (int j = 0; j < 5 && top[j] >= 0; j++) { pg_stat *t = &g_pg_st[top[j]]; + o += snprintf(line + o, sizeof line - o, " #%d %.2f ms %dx%d fmt%u d%u %s %d 绘制;", top[j], t->sum / t->n, t->meta.w, t->meta.h, t->meta.cfmt0, t->meta.dfmt, t->meta.load, t->meta.draws); } + MLOG("[passgpu] 至帧 %llu 每帧通道均值合计 %.2f ms;前五:%s", (unsigned long long)p->frame, tot, line); + fflush(g_pg_f); + } + } + pthread_mutex_unlock(&g_pg_mu); + free(ps); + }]; +} +void ts_after(void); +void ms_after(void); +// ---------- Store lookahead: at pass end, decide from the later commands of the same submission whether each attachment's write-back is redundant ---------- +// On TBDR every attachment is written back to memory in full when the pass ends; Switch (Maxwell immediate-mode rendering) has no such cost. If an attachment is cleared in full before being read, the write-back is redundant (DontCare). +// For the decision see mtl_sub_index / sdx_decide; only what can be proven within the same submission is accepted, and anything still undecided at the end of the submission is always written back. +typedef struct { uint64_t ser, bytes; uint32_t lv, sl; int idx; uint8_t depth, state, ridx, drawn, why, load, wrote, wwhy; const char *wkey; int wslot; } sd_att; // depth: 0 color 1 depth 2 stencil (the two parts of a combined depth-stencil texture are decided separately) // state 0 undecided 1 needed 2 redundant; idx: color attachment index, -1 for depth +static sd_att g_sd[9]; static int g_nsd; +static int g_udc_ab = -1, g_udc_ab_on; +// Attachments this pass did not write (depth / stencil test only, color write mask 0, etc.) get their write-back changed to DontCare: the content is already in memory, saving one full write-back. +// On by default: on Mac, same-process on / off screenshots follow the same distribution as the null control; on A19 Pro, private Depth32F_S8 / RGBA8 read back after DontCare differ in 0 places. +// DM_NVN_UNWRITTEN_DC=0 turns it off (for equivalence comparison) +static int udc_on(void) { + if (g_udc_ab < 0) { const char *e = getenv("DM_NVN_UNWRITTEN_DC"); g_udc_ab = !(e && !strcmp(e, "0")); + const char *a = getenv("DM_NVN_UDC_AB"); if (a && !strcmp(a, "1")) { g_udc_ab = 0; g_udc_ab_on = 0; } } + return g_udc_ab || g_udc_ab_on; +} +// Diagnostic DM_NVN_TBDR=0: turns off all TBDR optimizations (store lookahead, load elision, same-target continuation, clear-only pass hoist, on-demand depth-stencil attach); replay takes the plainest 'attach all, load all, store all' path. +// Only for equivalence comparison (the same replay run on / off, comparing the picture byte by byte), not a product configuration +// Mario: off by default (only DM_NVN_TBDR=1 turns it on). These optimizations infer read / write relations from "which textures are bound in texture slots", while 38% of this game's shaders use bindless textures (handles in uniform buffers, NVN_RB_TEXU), +// so the inference would miss readers - e.g. deciding an attachment is never read afterwards and not storing it. Turn them back on once they recognize bindless readers. +static int tbdr_on(void) { static int v = -1; if (v < 0) { const char *e = getenv("DM_NVN_TBDR"), *r = getenv("DM_NVN_RULER"); v = e && !strcmp(e, "1"); g_ruler_truth = r && !strcmp(r, "1"); if (!v) MLOG("TBDR 优化缺省关(本作有无绑定贴图;DM_NVN_TBDR=1 开)"); } return v && !g_ruler_truth; } +int dm_nvn_ruler_truth(void) { tbdr_on(); return g_ruler_truth; } // Visual ruler ground-truth phase (host-side settings under test yield accordingly, e.g. the 3D render scale) +static int rt_range_on(void) { static int v = -1; if (v < 0) { const char *e=getenv("DM_NVN_RT_RANGE"); v=e && !strcmp(e,"1"); } return v; } // Narrow candidate, off by default +static uint64_t g_sd_dead_bytes, g_sd_n_dead, g_sd_n_all; +// ---------- Cross-submission attachment validity ledger ---------- +// Entry = subresource (texture serial, level, layer) + state, surviving across submissions: +// XV_INVALID content was not written back / not generated and must not be read. The next touch must be a full Clear, or use as an attachment with load action DontCare / Clear; otherwise it is a violation. +// XV_PENDING_CLEAR reserved for 'clear-only pending across submissions' (not implemented): accepts only full, unscissored clears with the write mask fully open; clearing again while pending = replace the clear value without landing it; +// as an attachment with load DontCare / Clear → the pending clear is voided, with load Load → changed to a Clear load; depth-stencil is recorded per plane. +// Violation = report a failure + that stable key (format / width / height / level) permanently disabled (in memory + persisted to DM_XV_FILE, read at the next launch) + all entries of the same texture deleted. The fallback can only detect, not undo (in that frame the attachment holds old content). +// Its first use: color attachments 'scanned to the end undecided' at the end of a submission are not written back (sd_finish) - conditions: its first touch in this submission is a full clear / DontCare load (reset every frame before use), it is not a window texture, and the key is not disabled. +// Switch DM_NVN_XV=0 turns it off; has no effect when tbdr_on()==0 (visual ruler ground-truth mode). +// Trial period (XV_PROBE): for a key's first XV_TRIAL times it is only 'shadow-accounted' - written back as usual, but its next touch is tracked all the same; only after XV_TRIAL consecutive compliant touches does the write-back really start being skipped. +// On a read / Load during the trial period: only disable that key (within this process, not persisted, no failure), the picture is intact - in practice there are readers as early as the launch / menu screens (upscale target 36), and not every fresh install should get one wrong frame. +enum { XV_INVALID = 1, XV_PENDING_CLEAR = 2, XV_PROBE = 3 }; +#define XV_TRIAL 3 +typedef struct { uint64_t ser, tag; uint32_t lv, sl; uint8_t state; } xv_ent; +static xv_ent g_xv[64]; static int g_xv_n, g_xv_block, g_xv_init; +static uint64_t g_xv_disabled[128]; static int g_xv_ndis; +static struct { uint64_t tag; int good; } g_xv_trial[128]; static int g_xv_ntrial; // Number of consecutive compliant touches for each key +static int xv_trial_get(uint64_t tag, int add) { for (int i = 0; i < g_xv_ntrial; i++) if (g_xv_trial[i].tag == tag) return g_xv_trial[i].good += add; if (g_xv_ntrial < 128) { g_xv_trial[g_xv_ntrial].tag = tag; return g_xv_trial[g_xv_ntrial++].good = add; } return 0; } +static uint64_t g_xv_present[128]; static int g_xv_npresent; +static uint64_t g_xv_dc, g_xv_viol; +typedef struct { uint64_t ser; uint32_t lv, sl; uint8_t good; } xv_touch; +static xv_touch g_xv_touch[128]; static int g_xv_ntouch; +static uint64_t xv_tag(const tex_t *t, uint32_t lv) { return ((uint64_t)t->d.format << 40) ^ ((uint64_t)t->d.width << 24) ^ ((uint64_t)t->d.height << 8) ^ lv; } +// Stable key (unchanged across launches, does not use the texture serial): format / width / height / level, then mixed with the formats of the color attachments of the pass where the write-back is skipped and this attachment's index - textures of the same format and size but different purpose (e.g. transmittance 27 and upscale target 36) do not share a key +static uint64_t xv_tag_pass(const tex_t *t, uint32_t lv, int idx, const p_set_rt *rt) { uint64_t h = xv_tag(t, lv) ^ ((uint64_t)(idx + 1) << 60); for (int i = 0; i < rt->n && i < 8; i++) h = (h ^ (rt->color[i] ? (uint64_t)rt->color[i]->d.format + 1 : 0)) * 0x100000001b3ull; return h; } +static void xv_init(void) { + if (g_xv_init) return; g_xv_init = 1; + const char *p = getenv("DM_XV_FILE"); if (!p || !*p) return; + FILE *f = fopen(p, "r"); if (!f) return; + char line[256], why[160]; unsigned long long tag; int ch, n = 0; + while (fgets(line, sizeof line, f) && g_xv_ndis < 128) if (sscanf(line, "%llx\t%159[^\t]\t%d", &tag, why, &ch) >= 1) g_xv_disabled[g_xv_ndis++] = tag, n++; + fclose(f); MLOG("读到 %d 条禁用记录", n); +} +static int xv_disabled(uint64_t tag) { xv_init(); for (int i = 0; i < g_xv_ndis; i++) if (g_xv_disabled[i] == tag) return 1; return 0; } +static xv_ent *xv_find(uint64_t ser, uint32_t lv, uint32_t sl) { for (int i = 0; i < g_xv_n; i++) if (g_xv[i].ser == ser && g_xv[i].lv == lv && g_xv[i].sl == sl) return &g_xv[i]; return NULL; } +static void xv_drop(uint64_t ser, uint32_t lv, uint32_t sl) { for (int i = 0; i < g_xv_n; i++) if (g_xv[i].ser == ser && g_xv[i].lv == lv && g_xv[i].sl == sl) { g_xv[i] = g_xv[--g_xv_n]; return; } } +static void xv_drop_all_of(uint64_t ser) { for (int i = 0; i < g_xv_n;) if (g_xv[i].ser == ser) g_xv[i] = g_xv[--g_xv_n]; else i++; } +static void xv_set(uint64_t ser, uint32_t lv, uint32_t sl, uint8_t state, uint64_t tag) { + xv_init(); if (g_xv_block || xv_disabled(tag)) return; + xv_ent *e = xv_find(ser, lv, sl); if (e) { e->state = state; e->tag = tag; return; } + if (g_xv_n < 64) g_xv[g_xv_n++] = (xv_ent){ser, tag, lv, sl, state}; +} +static void xv_violation(const xv_ent *e, const char *why) { + xv_init(); + if (e->state == XV_PROBE) { // Trial period: the content is still there and the picture is intact - only disable the key (this process) + if (!xv_disabled(e->tag) && g_xv_ndis < 128) g_xv_disabled[g_xv_ndis++] = e->tag; + PD("V xv 试用期见读者:键=%016llx 贴图=%llu 级=%u 原因=%s → 该键不做", (unsigned long long)e->tag, (unsigned long long)e->ser, e->lv, why); + xv_drop_all_of(e->ser); return; } + g_xv_viol++; + int seen = xv_disabled(e->tag); if (!seen && g_xv_ndis < 128) g_xv_disabled[g_xv_ndis++] = e->tag; + MLOG("【红】xv 标签=%016llx 贴图=%llu 级=%u 层=%u 原因=%s 通道=%llu", (unsigned long long)e->tag, (unsigned long long)e->ser, e->lv, e->sl, why, (unsigned long long)g_stat_pass); + const char *p = getenv("DM_XV_FILE"); if (p && *p) { FILE *f = fopen(p, "a"); if (f) { fprintf(f, "%016llx\t%s\t%llu\n", (unsigned long long)e->tag, why, (unsigned long long)g_stat_pass); fclose(f); } } + xv_drop_all_of(e->ser); +} +static void xv_check(uint64_t ser, uint32_t lv, uint32_t sl, const char *why) { xv_ent *e = xv_find(ser, lv, sl); if (e && (e->state == XV_INVALID || e->state == XV_PROBE)) { xv_ent copy = *e; xv_violation(©, why); } } +static void xv_good(xv_ent *e) { if (e->state == XV_PROBE) xv_trial_get(e->tag, 1); xv_drop(e->ser, e->lv, e->sl); } // Compliant touch: counts once toward the trial period, and the entry returns to valid +static void xv_touch_add(uint64_t ser, uint32_t lv, uint32_t sl, int good) { for (int i = 0; i < g_xv_ntouch; i++) if (g_xv_touch[i].ser == ser && g_xv_touch[i].lv == lv && g_xv_touch[i].sl == sl) return; if (g_xv_ntouch < 128) g_xv_touch[g_xv_ntouch++] = (xv_touch){ser, lv, sl, (uint8_t)good}; } +static int xv_first_good(uint64_t ser, uint32_t lv, uint32_t sl) { for (int i = 0; i < g_xv_ntouch; i++) if (g_xv_touch[i].ser == ser && g_xv_touch[i].lv == lv && g_xv_touch[i].sl == sl) return g_xv_touch[i].good; return 0; } +static int xv_present(uint64_t ser) { for (int i = 0; i < g_xv_npresent; i++) if (g_xv_present[i] == ser) return 1; return 0; } +static void xv_forward(void); +static int tbdr_on(void); +static int xv_on(void) { static int off = -1; if (off < 0) { const char *e = getenv("DM_NVN_XV"); off = e && !strcmp(e, "0"); } return !off && tbdr_on(); } +void mtl_xv_window_tex(uint64_t serial) { if (!xv_present(serial) && g_xv_npresent < 128) g_xv_present[g_xv_npresent++] = serial; } // Window (swapchain) texture: never enters the ledger +void xv_test_counts(int *dc, int *viol) { if (dc) *dc = (int)g_xv_dc; if (viol) *viol = (int)g_xv_viol; } +static void sd_finish(void); +// Load elision (fully covering first draw): if the draw that opens the pass can be proven to cover a color attachment in full (first_draw_covers), that attachment's load becomes DontCare - +// the first draw does not blend and writes all channels, every covered pixel is rewritten by it, and the original content is not visible (Switch immediate-mode rendering has no such cost; on TBDR load = reading the whole thing from memory into the tile) +static const p_draw *g_fd_dw; static const p_draw_idx *g_fd_di; // The draw that opens the pass (one of non-indexed / indexed); non-null only while draw() is opening a pass +static uint64_t g_fd_n; // Cumulative number of attachments judged elidable +static int g_fd_why; // Reason for the most recent failure (Q line of PASSDUMP: 1 no program 2 shader 3 attachment 4 blend/mask/raster 5 depth-stencil test 6 vertex count 7 position attribute 8 format/stream 9 out of bounds 10 address 11 geometry) +static int first_draw_covers(int i, id t, NSUInteger level); +static int pf_is_float(MTLPixelFormat f) { // Float color formats that can store NaN (sentinel choice for the load-elision probe; RGB9E5 cannot store NaN and does not count) + switch (f) { case MTLPixelFormatR16Float: case MTLPixelFormatRG16Float: case MTLPixelFormatRGBA16Float: case MTLPixelFormatR32Float: + case MTLPixelFormatRG32Float: case MTLPixelFormatRGBA32Float: case MTLPixelFormatRG11B10Float: return 1; default: return 0; } +} +static int fz_load_covers(int i, id t, NSUInteger level); +static NSMutableArray *g_lp; // DM_NVN_LOADPROBE=1 / 2 / 3 (two constant sentinel values / NaN - under float formats normal shader output cannot reach NaN, ruling out numeric coincidence): attachments judged elidable are cleared to the sentinel color instead, and at pass end the remaining sentinel pixels are read back and counted (diagnostic: should be 0) +static void lp_finish(void); +// Same-target continuation: when the game calls SetRenderTargets again on the same set of targets (with no copy / dispatch / clear in between) the pass is not ended - on TBDR this saves one full write-back + load. +// Basis for equivalence: NVN's Barrier between two passes requires the latter to see all writes of the former. Within one Metal render encoder, later draws' reads and writes of the attachment itself (blend, depth / stencil test) +// are ordered with earlier draws in submission order; but 'sampling' the attachment texture is not covered (reading and writing the same attachment within one pass is undefined). So continuation happens only when later draws do not sample these attachments: +// if during continuation a draw needs to sample them, or a clear arrives, the pass is ended before it, returning to the separate semantics. Copies / dispatches end the render encoder anyway +static int g_enc_mode; // How the current encoder was opened (rp_begin_ex argument: 0 all attachments 1 only for clears 2 depth omitted) +static int g_rt_range_active, g_rt_range_pending; +static int g_rt_merged; // The current pass has already been continued (at least one same-target SetRenderTargets was merged in) +static uint64_t g_rt_merge_n; // Cumulative continuation count +static int g_defer_replaying; +static void fz_end(void); // Wrap-up of the readable-tile pass merge (defined before draw) +static void defer_fallback(void); +// DM_NVN_ENC_LABEL=1: label every render encoder with 'p x d'; in Instruments (xctrace Game Performance / Metal System Trace) +// the encoder track and the GPU interval table carry this name directly, and shader_map.tsv maps it back to the shader; off by default (one string allocation per pass) +static int g_el_w, g_el_h, g_el_draws; static char g_el_key[12]; +static int enc_label_on(void) { static int v = -1; if (v < 0) { const char *e = getenv("DM_NVN_ENC_LABEL"); v = e ? atoi(e) : 1; } return v; } +static void ghost_flush(void); +static void za_atomic_sync_end(void); +static void zero_image_sync_end(void); +static void za_diag_pass_end(void); +static void za_diag_op(const char *kind, const tex_t *t); +static void pixel_history_begin(MTLRenderPassDescriptor *pd, NSUInteger nc); +static void pixel_history_store(void); +static void pixel_history_end(void); +static void pixel_history_draw(const char *vs, const char *fs); +static void rp_end(void) { if (g_enc) { AP_B; + ghost_flush(); + if (enc_label_on()) { static uint64_t lf = ~0ull, base; uint64_t f = atomic_load(&g_cur_frame); if (f != lf) { lf = f; base = g_stat_pass - 1; } // Intra-frame serial: the same pass has the same name every frame, so the table can aggregate by name + [(id)g_enc setLabel:[NSString stringWithFormat:@"p%02llu %dx%d %s d%d", (unsigned long long)(g_stat_pass - base), g_el_w, g_el_h, g_el_key[0] ? g_el_key : "-", g_el_draws]]; } +#ifdef NVN_RT_RANGE_TEST + int ranged=g_rt_range_active && !g_rt_range_pending; +#endif + sd_finish(); pixel_history_store(); enc_r_end_pass(g_enc); g_enc = nil; zero_image_sync_end(); za_atomic_sync_end(); za_diag_pass_end(); pixel_history_end(); zskip_commit_clears(); g_rt_merged = 0; g_rt_range_active = g_rt_range_pending = 0; pg_end_render(); ts_after(); ms_after(); if (g_lp.count) lp_finish(); fz_end(); + if (g_nubd) ubd_flush(); // the inline updates deferred in this pass, in order (see mtl_sub_update) +#ifdef NVN_RT_RANGE_TEST + rr_finish(ranged); +#endif + AP_E(0); +} else zero_image_sync_end(); } +void pg_sub_begin_ext(uint64_t fence) { pg_sub_begin(fence); } +void pg_commit_ext(id cb) { pg_commit(cb); } +// The format an attachment is actually attached with: the view's format if the view changes the format and can be attached, otherwise the base texture's. Shared by attaching (attach) and pipeline prediction (pso_predict_rest) so both agree -- +// prediction used the base format, so for a pass written through an sRGB view (Mario's last pass) the predicted pipeline had the wrong format and the real one was compiled at the first draw (from d2r-mobile e2a0c9a9b) +// A compressed texture as a render target (The Legend of Zelda: Breath of the Wild: a 32x32 RGTC1 (BC4) texture written through an RGBA16UI view, one texel = one 8-byte block, i.e. BC4 encoded on the GPU): +// Metal can neither attach a compressed format as a colour attachment nor create compressed <-> uncompressed views (the pipeline descriptor validation aborts the process). For now the attachment is left out (a pass with no attachment at all skips its draws), with a red line; +// drawing it correctly needs an uncompressed proxy texture with the same block size, copied into the compressed texture through a buffer after the pass +// On a GPU without BC the same texture is stored decompressed (R8 for BC4), so the attachment check must go by the NVN format, not the Metal one: attaching it through the RGBA16UI view +// asked Metal for an R8Unorm -> RGBA16Uint view, which aborts the process (GitHub issue #4, iPhone 14 Pro; DM_TEST_NO_BC=1 reproduces it on the Mac) +static int mtl_fmt_block(MTLPixelFormat f) { return (f >= (MTLPixelFormat)DM_MTL_BC1 && f <= (MTLPixelFormat)DM_MTL_BC7_SRGB) || (f >= MTLPixelFormatPVRTC_RGB_2BPP && f < MTLPixelFormatGBGR422); } +static int nvn_fmt_bc(uint32_t f) { uint32_t pf; int bpp; return nvn_bc_unpacked(f, &pf, &bpp); } // NVN BC1-7 / BC6H formats, whether or not this GPU stores them compressed +static MTLPixelFormat rt_view_fmt(id base, const view_t *v, int has_v) { + if (base && mtl_fmt_block(base.pixelFormat)) { + RED_ONCE(78, "渲染目标是压缩格式贴图(Metal 格式 %lu %lux%lu 层 %lu,视图换格式 %d → NVN %#x):不挂,该附件的绘制落空", (unsigned long)base.pixelFormat, (unsigned long)base.width, (unsigned long)base.height, + (unsigned long)base.arrayLength, has_v ? v->set_format : 0, has_v ? v->format : 0); + return MTLPixelFormatInvalid; + } + if (!base || !has_v || !v->set_format) return base.pixelFormat; + uint32_t pf = 0; uint8_t swz[4]; + if (!nvn_fmt_to_mtl(v->format, &pf, swz) || !pf || pf == (uint32_t)base.pixelFormat) return base.pixelFormat; + if (tex_need_pfv(base.pixelFormat, (MTLPixelFormat)pf) && !(base.usage & MTLTextureUsagePixelFormatView)) return base.pixelFormat; + return (MTLPixelFormat)pf; +} +// The same texture level / layer attached at more than one colour slot: only the first is attached, later ones are not (their writes are dropped). The G-buffer passes of The Legend of Zelda: Breath of the Wild attach the albedo and another buffer twice each +// (c1 = c2, c3 = c4), and the channel write masks of c2 / c4 are always 0 (cmask 0x00f0f0ff). The Switch's ROP writes only the masked channels and the two slots do not interfere; +// on Apple's TBDR each attachment has its own tile memory and the whole tile is stored back at the end of the pass, so the copy stored later (loaded, never written) overwrites the writes of the earlier one -- the order differs per 32x32 tile, +// the albedo kept only scattered squares, and the whole 3D picture went black. A red line is printed when a dropped slot is used with a non-zero write mask (those writes are really lost) +static int rt_dup_slot(const p_set_rt *r, int i) { + if (!r->color[i]) return 0; + uint8_t lv = r->has_cview[i] && r->cview[i].set_levels ? r->cview[i].base_level : 0; uint16_t ly = r->has_cview[i] && r->cview[i].set_layers ? r->cview[i].min_layer : 0; + for (int j = 0; j < i; j++) { + if (!r->color[j] || r->color[j]->serial != r->color[i]->serial) continue; // compare serials: records hold copies of the texture objects (tex_snap), so the pointers differ + uint8_t lj = r->has_cview[j] && r->cview[j].set_levels ? r->cview[j].base_level : 0; uint16_t yj = r->has_cview[j] && r->cview[j].set_layers ? r->cview[j].min_layer : 0; + if (lj == lv && yj == ly) return 1; + } + return 0; +} +static void attach(MTLRenderPassAttachmentDescriptor *a, const tex_t *t, const view_t *v, int has_v) { + za_diag_op("render-target", t); + id base = tex_mtl(t); a.texture = base; + if (base && mtl_fmt_block(base.pixelFormat)) { rt_view_fmt(base, v, has_v); a.texture = nil; return; } // compressed formats cannot be attached (see rt_view_fmt) + if (base && nvn_fmt_bc(t->d.format)) { // the same texture stored decompressed on a GPU without BC: not attached either (see above rt_view_fmt) + RED_ONCE(78, "渲染目标是压缩格式贴图(无 BC,按解压格式存:Metal 格式 %lu %lux%lu,视图换格式 %d → NVN %#x):不挂,该附件的绘制落空", (unsigned long)base.pixelFormat, (unsigned long)base.width, (unsigned long)base.height, + has_v ? v->set_format : 0, has_v ? v->format : 0); + a.texture = nil; return; + } + if (has_v && v->set_levels) a.level = v->base_level; + if (has_v && v->set_layers) { if (t->d.target == 2) a.depthPlane = v->min_layer; else a.slice = v->min_layer; } + // A view that changes the format is attached as a Metal view of that format (pipeline formats follow the attachment). SMBW's last pass writes the RGBA8 swapchain texture through an RGBA8_SRGB view, so the hardware encodes sRGB on write; + // attaching the base texture instead stored the linear values as they were and the whole picture came out dark (every value decoded from sRGB once more, on the Mac and the iPhone). The view lives until this submission completes + if (base && has_v && v->set_format) { + MTLPixelFormat pf = rt_view_fmt(base, v, has_v); + if (pf != base.pixelFormat) { + id vw = [base newTextureViewWithPixelFormat:pf]; + if (vw) { a.texture = vw; defer_obj_unref(vw); } + else RED_ONCE(67, "渲染目标视图建不出:格式 %#x(底图 Metal 格式 %lu)", v->format, (unsigned long)base.pixelFormat); + } else { uint32_t want = 0; uint8_t swz[4]; + if (nvn_fmt_to_mtl(v->format, &want, swz) && want && want != (uint32_t)base.pixelFormat) + RED_ONCE(68, "渲染目标视图格式 %#x 挂不上(底图 Metal 格式 %lu 建时没有换格式用途):按底图格式画", v->format, (unsigned long)base.pixelFormat); } + } +} + +// Metal requires renderTargetArrayLength for layered rendering. Without it, +// a vertex shader's [[render_target_array_index]] is effectively confined to +// layer zero on iOS 16. Z-A exposes this with its instanced 32x32x32 colour-LUT +// pass: all 32 instances run, but only depth plane zero is written and the +// following tone mapper samples zero from the other 31 planes. +// +// A 3D attachment without a single-layer view represents the remaining depth +// planes starting at depthPlane. Multiple attachments must expose the same +// number of active layers, so return one (the non-layered default) if any +// attached target cannot cover the candidate range. +static NSUInteger pass_3d_layer_count(MTLRenderPassDescriptor *pd, NSUInteger nc) { + NSUInteger layers = NSUIntegerMax; + int found_3d = 0; + for (NSUInteger i = 0; i < nc; i++) { + MTLRenderPassColorAttachmentDescriptor *a = pd.colorAttachments[i]; + id t = a.texture; + if (!t) continue; + NSUInteger n = 1; + if (t.textureType == MTLTextureType3D) { + NSUInteger depth = MAX(t.depth >> a.level, 1ul); + n = a.depthPlane < depth ? depth - a.depthPlane : 1; + if (i < (NSUInteger)R.rt.n && R.rt.has_cview[i] && R.rt.cview[i].set_layers && R.rt.cview[i].nlayers) + n = MIN(n, (NSUInteger)R.rt.cview[i].nlayers); + found_3d |= n > 1; + } + layers = MIN(layers, n); + } + // Depth/stencil attachments participate in every active layer too. A + // conventional 2D depth target therefore makes this a one-layer pass. + MTLRenderPassAttachmentDescriptor *extra[2] = { pd.depthAttachment, pd.stencilAttachment }; + for (int i = 0; i < 2; i++) if (extra[i].texture) { + id t = extra[i].texture; + NSUInteger n = 1; + if (t.textureType == MTLTextureType3D) { + NSUInteger depth = MAX(t.depth >> extra[i].level, 1ul); + n = extra[i].depthPlane < depth ? depth - extra[i].depthPlane : 1; + found_3d |= n > 1; + } + layers = MIN(layers, n); + } + return found_3d && layers != NSUIntegerMax ? layers : 1; +} +// Initial fallback supports color-only, single-sample 3D attachments with an +// identical relative layer range. Other attachment combinations stay explicit. +static NSUInteger slice_target_count(void) { + if (g_layered_native || !R.has_rt || R.rt.depth) return 1; + NSUInteger count = 0; + for (int i = 0; i < R.rt.n && i < 8; i++) if (R.rt.color[i]) { + const tex_t *t = R.rt.color[i]; const view_t *v = &R.rt.cview[i]; int hv = R.rt.has_cview[i]; + if (t->d.target != 2 || t->d.samples > 1) return 1; + NSUInteger level = hv && v->set_levels ? v->base_level : 0; + NSUInteger base = hv && v->set_layers ? v->min_layer : 0; + NSUInteger depth = MAX((NSUInteger)t->d.depth >> level, 1ul); + if (base >= depth) return 1; + NSUInteger n = depth - base; + if (hv && v->set_layers && v->nlayers) n = MIN(n, (NSUInteger)v->nlayers); + if (count && n != count) return 1; + count = n; + } + return count ? count : 1; +} +static void slice_select(uint32_t slice) { + for (int i = 0; i < R.rt.n && i < 8; i++) if (R.rt.color[i]) { + view_t *v = &R.rt.cview[i]; + v->min_layer = (R.rt.has_cview[i] && v->set_layers ? v->min_layer : 0) + slice; + v->set_layers = 1; v->nlayers = 1; R.rt.has_cview[i] = 1; + } + g_selected_slice = slice; +} +static void bare_dims(NSUInteger *w, NSUInteger *h); +static int diag_fill_c0_on(void) { + static int on = -1; + if (on < 0) { + const char *e = getenv("DM_NVN_FILL_C0"); + on = e && !strcmp(e, "1"); + if (on) MLOG("DM_NVN_FILL_C0=1: attach a transient color 0 target to sparse MRT passes"); + } + return on; +} +static id diag_c0_texture(NSUInteger w, NSUInteger h, NSUInteger samples) { + if (!g_diag_c0_cache) g_diag_c0_cache = [NSMutableDictionary dictionary]; + NSString *key = [NSString stringWithFormat:@"%lux%lu@%lu", (unsigned long)w, (unsigned long)h, (unsigned long)samples]; + id t = g_diag_c0_cache[key]; + if (t) return t; + MTLTextureDescriptor *d = [MTLTextureDescriptor new]; + d.textureType = samples > 1 ? MTLTextureType2DMultisample : MTLTextureType2D; + d.pixelFormat = MTLPixelFormatRGBA16Float; + d.width = MAX(w, 1ul); d.height = MAX(h, 1ul); d.sampleCount = MAX(samples, 1ul); + d.storageMode = MTLStorageModeMemoryless; d.usage = MTLTextureUsageRenderTarget; + t = [g_dev newTextureWithDescriptor:d]; + if (t) g_diag_c0_cache[key] = t; + else MLOG("[RED] DM_NVN_FILL_C0 could not allocate %lux%lu@%lu", (unsigned long)w, (unsigned long)h, (unsigned long)samples); + return t; +} +// mode 1 (clear_only): a pass opened only to land pending clears, attaching only the attachments that have clears; mode 2: the lookahead proves this pass does not use depth-stencil, so depth is not attached. +// clear_only: a pass opened only to land pending clears, attaching only the attachments that have clears - the other attachments are not touched by this pass, and attaching them costs a pointless load and store on TBDR +// (Mac character select screen: the color-clear pass drags along a 400x225 depth load and store) +static id rp_begin_ex(int clear_only); +static id rp_begin(void) { return rp_begin_ex(0); } +static id rp_begin_ex_(int clear_only); +static id rp_begin_ex(int clear_only) { AP_B; id e = rp_begin_ex_(clear_only); AP_E(1); return e; } +static id rp_begin_ex_(int clear_only) { + if (g_enc) return g_enc; + memset(g_zskip_cand_ok, 0, sizeof g_zskip_cand_ok); + g_enc_mode = clear_only; + if (!R.has_rt) { RED_ONCE(3, "绘制前没有设置渲染目标"); return nil; } + // The version of the attachment-range candidate is taken before capturing any attachment; if it changes during setup, there is no continuation. + uint64_t reg0 = 0, ser0 = 0, dirty0 = 0; + if (rt_range_on()) { reg0 = g_reg_gen; ser0 = g_ser_gen; } + if (rt_range_on()) dirty0=nvn_dirty_now(); + if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + MTLRenderPassDescriptor *pd = [MTLRenderPassDescriptor new]; + int any = 0, diag_c0 = 0; + for (int i = 0; i < R.rt.n; i++) { + if (!R.rt.color[i] || (clear_only == 1 && !R.clear_c[i])) continue; + if (rt_dup_slot(&R.rt, i)) continue; // a slot attached twice is not attached (see rt_dup_slot) + attach(pd.colorAttachments[i], R.rt.color[i], &R.rt.cview[i], R.rt.has_cview[i]); + if (!pd.colorAttachments[i].texture) continue; + any = 1; + pd.colorAttachments[i].loadAction = R.clear_c[i] ? MTLLoadActionClear : MTLLoadActionLoad; + pd.colorAttachments[i].clearColor = R.cc[i]; + if (R.clear_c[i]) PD("C %llu c%d 清成 %g,%g,%g,%g", g_stat_pass + 1, i, R.cc[i].red, R.cc[i].green, R.cc[i].blue, R.cc[i].alpha); + int cov = tbdr_on() && !R.clear_c[i] && (g_fd_dw || g_fd_di) && fz_load_covers(i, pd.colorAttachments[i].texture, pd.colorAttachments[i].level); + if (!R.clear_c[i] && (g_fd_dw || g_fd_di) && !cov) PD("Q %llu c%d 读入不可省 why=%d", g_stat_pass + 1, i, g_fd_why); + if (cov) { + static int probe = -1; if (probe < 0) { probe = getenv("DM_NVN_LOADPROBE") ? atoi(getenv("DM_NVN_LOADPROBE")) : 0; if (probe) MLOG("DM_NVN_LOADPROBE:读入可省的附件改清成哨兵色并回读核对"); } + if (probe) { pd.colorAttachments[i].loadAction = MTLLoadActionClear; int pm = probe == 1 && pf_is_float(pd.colorAttachments[i].texture.pixelFormat) ? 3 : probe; // Float formats use a NaN sentinel by default: a fixed sentinel value can be hit by real output (it has been hit in practice) + pd.colorAttachments[i].clearColor = pm == 3 ? MTLClearColorMake(NAN, NAN, NAN, NAN) : probe == 2 ? MTLClearColorMake(0.777, 0.222, 0.555, 0.888) : MTLClearColorMake(0.123, 0.456, 0.789, 0.321); + if (!g_lp) g_lp = [NSMutableArray array]; + [g_lp addObject:@[pd.colorAttachments[i].texture, @(pd.colorAttachments[i].level), @(pd.colorAttachments[i].slice), @(g_stat_pass + 1), @(i), @(R.rt.color[i]->serial), @(pm)]]; } + else pd.colorAttachments[i].loadAction = MTLLoadActionDontCare; + g_fd_n++; + } + xv_touch_add(R.rt.color[i]->serial, (uint32_t)pd.colorAttachments[i].level, (uint32_t)pd.colorAttachments[i].slice, + pd.colorAttachments[i].loadAction != MTLLoadActionLoad); + if (g_xv_n) { xv_ent *xe = xv_find(R.rt.color[i]->serial, (uint32_t)pd.colorAttachments[i].level, (uint32_t)pd.colorAttachments[i].slice); + if (xe && (xe->state == XV_INVALID || xe->state == XV_PROBE)) { if (pd.colorAttachments[i].loadAction == MTLLoadActionLoad) { xv_ent copy = *xe; xv_violation(©, "作附件读入旧内容"); } else xv_good(xe); } } + pd.colorAttachments[i].storeAction = MTLStoreActionStore; + } + if (!pd.colorAttachments[0].texture && clear_only != 1 && diag_fill_c0_on()) { + MTLRenderPassColorAttachmentDescriptor *first = nil; + for (int i = 1; i < R.rt.n && !first; i++) if (pd.colorAttachments[i].texture) first = pd.colorAttachments[i]; + if (first) { + NSUInteger w = MAX(first.texture.width >> first.level, 1ul), h = MAX(first.texture.height >> first.level, 1ul); + id t = diag_c0_texture(w, h, first.texture.sampleCount); + if (t) { + pd.colorAttachments[0].texture = t; + pd.colorAttachments[0].loadAction = MTLLoadActionDontCare; + pd.colorAttachments[0].storeAction = MTLStoreActionDontCare; + any = diag_c0 = 1; + PD("P %llu c0 diagnostic filler fmt=%lu %lux%lu ms%lu", g_stat_pass + 1, (unsigned long)t.pixelFormat, + (unsigned long)t.width, (unsigned long)t.height, (unsigned long)t.sampleCount); + } + } + } + // clear_only: 0 attach all 1 only for clears 2 neither depth nor stencil attached 3 stencil only 4 depth only (3 / 4: the pass_ds_need lookahead proves the whole pass uses only that one) + if (R.rt.depth && clear_only == 3) { // Stencil only: the depth plane of the combined format does not enter the tile and is neither read nor written (Metal allows an empty depth attachment with the combined texture on the stencil attachment) + attach(pd.stencilAttachment, R.rt.depth, &R.rt.dview, R.rt.has_dview); + MTLPixelFormat pf = pd.stencilAttachment.texture.pixelFormat; + if (pf == MTLPixelFormatDepth32Float_Stencil8 || pf == MTLPixelFormatStencil8) { + any = 1; pd.stencilAttachment.loadAction = R.clear_s ? MTLLoadActionClear : MTLLoadActionLoad; + pd.stencilAttachment.clearStencil = R.cs; pd.stencilAttachment.storeAction = MTLStoreActionStore; + } else pd.stencilAttachment.texture = nil; // No stencil plane: the stencil test always passes, attach nothing + } else if (R.rt.depth && clear_only != 2 && !(clear_only == 1 && !R.clear_d && !R.clear_s)) { + attach(pd.depthAttachment, R.rt.depth, &R.rt.dview, R.rt.has_dview); + if (pd.depthAttachment.texture) { + any = 1; + pd.depthAttachment.loadAction = R.clear_d ? MTLLoadActionClear : MTLLoadActionLoad; + pd.depthAttachment.clearDepth = R.cd; pd.depthAttachment.storeAction = MTLStoreActionStore; + MTLPixelFormat pf = pd.depthAttachment.texture.pixelFormat; + if (clear_only != 4 && (pf == MTLPixelFormatDepth32Float_Stencil8 || pf == MTLPixelFormatStencil8)) { // 4: depth only + pd.stencilAttachment.texture = pd.depthAttachment.texture; pd.stencilAttachment.level = pd.depthAttachment.level; + pd.stencilAttachment.slice = pd.depthAttachment.slice; + pd.stencilAttachment.loadAction = R.clear_s ? MTLLoadActionClear : MTLLoadActionLoad; + pd.stencilAttachment.clearStencil = R.cs; pd.stencilAttachment.storeAction = MTLStoreActionStore; + } + } + } + // NVN permits a fragment shader whose only effects are image or storage + // writes to draw with zero color/depth targets. Metal supports the same + // operation when the render-pass size is explicit. Z-A uses this path for + // its image-atomic pass; skipping it leaves the later 3D composite black. + int bare = !any && clear_only != 1 && !R.rt.depth; + for (int i = 0; i < R.rt.n && bare; i++) if (R.rt.color[i]) bare = 0; + if (bare) { + NSUInteger bw, bh; + bare_dims(&bw, &bh); + pd.renderTargetWidth = bw; + pd.renderTargetHeight = bh; + pd.defaultRasterSampleCount = 1; + if (!g_bare_native) { + // Apple3 requires a real attachment even when the shader writes + // only buffers. Match the attachment-less raster bounds exactly; + // no guest image is substituted and no tile content is stored. + id t = diag_c0_texture(bw, bh, 1); + if (!t) { MLOG("[RED] Could not allocate attachment-less compatibility target %lux%lu", (unsigned long)bw, (unsigned long)bh); return nil; } + pd.colorAttachments[0].texture = t; + pd.colorAttachments[0].loadAction = MTLLoadActionDontCare; + pd.colorAttachments[0].storeAction = MTLStoreActionDontCare; + diag_c0 = 1; + } + static int once; + if (!once++) MLOG("attachment-less pass: opened at viewport size %lux%lu (first case pass %llu)", + (unsigned long)bw, (unsigned long)bh, g_stat_pass + 1); + PD("P %llu attachment-less %lux%lu", g_stat_pass + 1, (unsigned long)bw, (unsigned long)bh); + any = 1; + } + if (!any) { + RED_ONCE(4, "渲染目标都建不出 MTLTexture"); + if (passdump()) { // PASSDUMP: when a whole pass is skipped, list its targets (serial, format, compressed or not) + char b[256]; size_t o = 0; + for (int i = 0; i < R.rt.n && i < 8; i++) if (R.rt.color[i]) o += snprintf(b + o, sizeof b - o, " c%d=%llu/f%#x%s", i, (unsigned long long)R.rt.color[i]->serial, R.rt.color[i]->d.format, fmt_compressed(R.rt.color[i]->d.format) ? "(压缩)" : ""); + if (R.rt.depth) o += snprintf(b + o, sizeof b - o, " d=%llu", (unsigned long long)R.rt.depth->serial); + PD("N %llu 通道跳过(附件都挂不上)mode=%d%s", g_stat_pass + 1, clear_only, b); + } + return nil; + } + NSUInteger layer_count = pass_3d_layer_count(pd, (NSUInteger)R.rt.n); + if (layer_count > 1) { + if (!g_layered_native) { + MLOG("[RED] Unsupported layered attachment combination: %lu layers; no proven per-slice replay", (unsigned long)layer_count); + return nil; + } + pd.renderTargetArrayLength = layer_count; + static int once; + if (!once++) MLOG("layered 3D render target: renderTargetArrayLength=%lu (first pass %llu)", + (unsigned long)layer_count, (unsigned long long)(g_stat_pass + 1)); + PD("P %llu layered-3d layers=%lu", g_stat_pass + 1, (unsigned long)layer_count); + } + for (int i = 0; i < R.rt.n && i < 8; i++) if (R.rt.color[i] && zskip_zero_clear(i)) { + g_zskip_cand_ok[i] = 1; g_zskip_cand[i] = (zskip_zero){R.rt.color[i]->serial, (uint16_t)(R.rt.has_cview[i] && R.rt.cview[i].set_levels ? R.rt.cview[i].base_level : 0), (uint16_t)(R.rt.has_cview[i] && R.rt.cview[i].set_layers ? R.rt.cview[i].min_layer : 0)}; + } + // Bandwidth ledger: attachments with Load count as load, all attachments count as store (currently always Store) + int nload = 0, natt = 0; + for (int i = 0; i < R.rt.n; i++) { + MTLRenderPassColorAttachmentDescriptor *c = pd.colorAttachments[i]; + if (!c.texture) continue; + if (diag_c0 && i == 0) continue; + uint64_t b = att_bytes(c.texture, c.level); natt++; g_bw_store += b; if (c.loadAction == MTLLoadActionLoad) { g_bw_load += b; nload++; } + if (R.rt.has_cview[i] && R.rt.cview[i].set_format) PD("V %llu c%d 视图格式 %#x(基贴图 NVN 格式 %#x)", g_stat_pass + 1, i, R.rt.cview[i].format, R.rt.color[i]->d.format); + PD("P %llu c%d ser=%llu fmt=%lu %lux%lu ms%lu lv%lu sl%lu %s mtl=%p", g_stat_pass + 1, i, R.rt.color[i]->serial, (unsigned long)c.texture.pixelFormat, + (unsigned long)c.texture.width, (unsigned long)c.texture.height, (unsigned long)c.texture.sampleCount, (unsigned long)c.level, (unsigned long)c.slice, + c.loadAction == MTLLoadActionClear ? "clear" : c.loadAction == MTLLoadActionDontCare ? "dontcare" : "load", (__bridge void *)(c.texture.parentTexture ? c.texture.parentTexture : c.texture)); + } + { // Depth and stencil planes are counted separately (combined format 5 bytes = depth 4 + stencil 1; when only one is attached only that one is counted) + id dt = pd.depthAttachment.texture, stt = pd.stencilAttachment.texture; + uint64_t whole = att_bytes(dt ? dt : stt, dt ? pd.depthAttachment.level : pd.stencilAttachment.level); + int comb = (dt ? dt : stt).pixelFormat == MTLPixelFormatDepth32Float_Stencil8; + uint64_t db = dt ? (comb ? whole / 5 * 4 : whole) : 0, sb = stt && stt != dt ? (comb ? whole / 5 : whole) : stt ? whole - db : 0; + if (dt) { natt++; g_bw_store += db; if (pd.depthAttachment.loadAction == MTLLoadActionLoad) { g_bw_load += db; nload++; } } + if (stt) { natt += !dt; g_bw_store += sb; if (pd.stencilAttachment.loadAction == MTLLoadActionLoad) { g_bw_load += sb; nload += !dt; } } + if (dt) PD("P %llu d ser=%llu fmt=%lu %lux%lu %s depth=%g", g_stat_pass + 1, R.rt.depth->serial, (unsigned long)dt.pixelFormat, + (unsigned long)dt.width, (unsigned long)dt.height, pd.depthAttachment.loadAction == MTLLoadActionClear ? "clear" : "load", pd.depthAttachment.clearDepth); + else if (stt) PD("P %llu s ser=%llu fmt=%lu %lux%lu %s(只挂模板)", g_stat_pass + 1, R.rt.depth->serial, (unsigned long)stt.pixelFormat, + (unsigned long)stt.width, (unsigned long)stt.height, pd.stencilAttachment.loadAction == MTLLoadActionClear ? "clear" : "load"); + } + if (natt && nload == natt) g_pass_full_rw++; + g_nsd = 0; + { + for (int i = 0; i < R.rt.n && i < 8; i++) { MTLRenderPassColorAttachmentDescriptor *c = pd.colorAttachments[i]; if (!c.texture) continue; + if (diag_c0 && i == 0) continue; + g_sd[g_nsd] = (sd_att){0}; g_sd[g_nsd].ser = R.rt.color[i]->serial; g_sd[g_nsd].bytes = att_bytes(c.texture, c.level); g_sd[g_nsd].lv = (uint32_t)c.level; g_sd[g_nsd].sl = (uint32_t)(c.texture.textureType == MTLTextureType3D ? c.depthPlane : c.slice); g_sd[g_nsd].idx = i; g_sd[g_nsd].load = c.loadAction == MTLLoadActionLoad; g_sd[g_nsd].wrote = R.clear_c[i] != 0; g_nsd++; + c.storeAction = MTLStoreActionUnknown; } + id dt = pd.depthAttachment.texture, stt = pd.stencilAttachment.texture; + uint64_t db = att_bytes(dt, pd.depthAttachment.level), sb = 0; + if (stt) { int comb = stt.pixelFormat == MTLPixelFormatDepth32Float_Stencil8; sb = att_bytes(stt, pd.stencilAttachment.level) / (comb ? 5 : 1); if (stt == dt) db -= sb; } // Combined format 5 bytes = depth 4 + stencil 1 (when only stencil is attached, only 1 is counted) + if (dt) { g_sd[g_nsd] = (sd_att){0}; g_sd[g_nsd].ser = R.rt.depth->serial; g_sd[g_nsd].bytes = db; g_sd[g_nsd].lv = (uint32_t)pd.depthAttachment.level; g_sd[g_nsd].sl = (uint32_t)pd.depthAttachment.slice; g_sd[g_nsd].idx = -1; g_sd[g_nsd].depth = 1; g_sd[g_nsd].load = pd.depthAttachment.loadAction == MTLLoadActionLoad; g_sd[g_nsd].wrote = R.clear_d != 0; g_nsd++; + pd.depthAttachment.storeAction = MTLStoreActionUnknown; } + if (stt) { g_sd[g_nsd] = (sd_att){0}; g_sd[g_nsd].ser = R.rt.depth->serial; g_sd[g_nsd].bytes = sb; g_sd[g_nsd].lv = (uint32_t)pd.stencilAttachment.level; g_sd[g_nsd].sl = (uint32_t)pd.stencilAttachment.slice; g_sd[g_nsd].idx = -1; g_sd[g_nsd].depth = 2; g_sd[g_nsd].load = pd.stencilAttachment.loadAction == MTLLoadActionLoad; g_sd[g_nsd].wrote = R.clear_s != 0; g_nsd++; + pd.stencilAttachment.storeAction = MTLStoreActionUnknown; } + } + memset(R.clear_c, 0, sizeof R.clear_c); R.clear_d = R.clear_s = 0; + { // Pass info (same convention as the former per-draw fetch: colors take each attachment's base texture format; sample count takes the last color attachment, or depth if there is depth) + uint64_t ser = g_pass.serial + 1; memset(&g_pass, 0, sizeof g_pass); g_pass.serial = ser; + int physical_colors = MAX(R.rt.n, bare && !g_bare_native ? 1 : 0); + for (int i = 0; i < physical_colors && i < 8; i++) { id t = pd.colorAttachments[i].texture; + if (t) { g_pass.cfmt[i] = (uint32_t)t.pixelFormat; g_pass.samples = (uint32_t)t.sampleCount; } } + id dt = R.rt.depth ? pd.depthAttachment.texture : nil, stt = R.rt.depth ? pd.stencilAttachment.texture : nil; // Depth / stencil attachments are fetched separately (possibly only one of them is attached) + g_pass.hd = dt && dt.pixelFormat != MTLPixelFormatStencil8; g_pass.hs = stt != nil; + g_pass.dfmt = g_pass.hd ? (uint32_t)dt.pixelFormat : 0; g_pass.sfmt = stt ? (uint32_t)stt.pixelFormat : 0; + if (dt || stt) g_pass.samples = (uint32_t)(dt ? dt : stt).sampleCount; + pass_dims(pd, (NSUInteger)physical_colors, &g_pass.tw, &g_pass.th); + if (rt_range_on()) { g_pass.reg_gen = reg0; g_pass.ser_gen = ser0; } + if (rt_range_on()) { g_pass.range_dirty=dirty0; g_pass.range_color=pd.colorAttachments[0].texture; g_pass.range_depth=pd.depthAttachment.texture; } + g_enc_pso = nil; g_enc_dss = nil; g_ed.ok = 0; + } + if (g_vis_on) { + // Slots used up (one submission exceeds VIS_SLOTS counting passes): continue in a new overflow buffer. A ring buffer must not be retired (it is still used in rotation, and retiring removes it from the residency set); only a previous overflow buffer is + 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; + pg_render(pd, (NSUInteger)R.rt.n); + { void td_note(id pd, NSUInteger nc); td_note(pd, (NSUInteger)R.rt.n); + void ts_note(id pd, NSUInteger nc); ts_note(pd, (NSUInteger)R.rt.n); + void ms_note(id pd, NSUInteger nc); ms_note(pd, (NSUInteger)R.rt.n); } + ser_gate(); + pixel_history_begin(pd, (NSUInteger)R.rt.n); + g_enc = enc_r_begin_pass(pd); + args_dirty_all(); + g_stat_pass++; + if (enc_label_on()) { id lt = pd.colorAttachments[0].texture ?: pd.depthAttachment.texture ?: pd.stencilAttachment.texture; g_el_w = (int)lt.width; g_el_h = (int)lt.height; g_el_draws = 0; g_el_key[0] = 0; } + return g_enc; +} +// Empty-scissor precheck before opening a pass: the size of the selected level of the first valid target is an upper bound on the minimum bounds of the actual pass. +static void rt_dims(NSUInteger *tw, NSUInteger *th) { + id t0 = nil; NSUInteger level = 0; + for (int i = 0; i < R.rt.n && i < 8 && !t0; i++) if (R.rt.color[i]) { + t0 = tex_mtl(R.rt.color[i]); level = R.rt.has_cview[i] && R.rt.cview[i].set_levels ? R.rt.cview[i].base_level : 0; + } + if (!t0 && R.rt.depth) { t0 = tex_mtl(R.rt.depth); level = R.rt.has_dview && R.rt.dview.set_levels ? R.rt.dview.base_level : 0; } + if (!t0 && !R.rt.depth) { + int bare = 1; for (int i = 0; i < R.rt.n; i++) bare &= R.rt.color[i] == NULL; + if (bare) { bare_dims(tw, th); return; } + } + *tw = t0 ? mip_extent(t0.width, level) : 1; *th = t0 ? mip_extent(t0.height, level) : 1; +} +// There are pending clears but no draw in the pass: open a pass once to land the clears +static void rp_flush(void) { + int pend = R.clear_d || R.clear_s; + for (int i = 0; i < 8; i++) pend |= R.clear_c[i]; + NSUInteger slices = !g_slice_replaying ? slice_target_count() : 1; + if (pend && !g_enc && g_cb && slices > 1) { + typeof(R) saved = R; g_slice_replaying = 1; + for (uint32_t i = 0; i < slices; i++) { R = saved; slice_select(i); rp_begin_ex(1); rp_end(); } + R = saved; memset(R.clear_c, 0, sizeof R.clear_c); R.clear_d = R.clear_s = 0; + g_slice_replaying = 0; g_selected_slice = 0; + return; + } + if (pend && !g_enc && g_cb) rp_begin_ex(1); + rp_end(); +} +static int scissor_full(const tex_t *t, const view_t *v, int has_v) { + NSUInteger level = has_v && v->set_levels ? v->base_level : 0; + NSUInteger w = mip_extent(t->d.width, level), h = mip_extent(t->d.target == 0 || t->d.target == 3 ? 1 : t->d.height, level); + return !R.has_sc || (R.sc.x <= 0 && R.sc.y <= 0 && (int64_t)R.sc.x + R.sc.w >= (int64_t)w && (int64_t)R.sc.y + R.sc.h >= (int64_t)h); +} +// Scissored zero clear (see OP_CLEAR_COLOR): land pending clears first, then copy from the zero buffer into the scissor rectangle of the attachment's level / slice in the replay stream's copy encoder. Multisample, 3D, depth / compressed formats are not handled (returns 0 and takes the old path) +static int clear_rect_zero(int i) { + const tex_t *t = R.rt.color[i]; id mt = tex_mtl(t); if (!mt) return 0; + MTLTextureType tt = mt.textureType; + if (tt == MTLTextureType2DMultisample || tt == MTLTextureType2DMultisampleArray || tt == MTLTextureType3D || tt == MTLTextureTypeTextureBuffer) return 0; + size_t bpp = mtl_pf_bytes(mt.pixelFormat); if (!bpp) return 0; + const view_t *v = &R.rt.cview[i]; int hv = R.rt.has_cview[i]; + NSUInteger lv = hv && v->set_levels ? v->base_level : 0, sl = hv && v->set_layers ? v->min_layer : 0; + long lw = (long)MAX(mt.width >> lv, 1u), lh = (long)MAX(mt.height >> lv, 1u); + long x0 = R.sc.x < 0 ? 0 : R.sc.x, y0 = R.sc.y < 0 ? 0 : R.sc.y, x1 = (long)R.sc.x + R.sc.w, y1 = (long)R.sc.y + R.sc.h; + if (x1 > lw) x1 = lw; if (y1 > lh) y1 = lh; + if (x1 <= x0 || y1 <= y0) return 1; + size_t row = (size_t)(x1 - x0) * bpp, img = row * (size_t)(y1 - y0); + id zb = zero_buf(img); if (!zb) return 0; + rp_flush(); // a pending full clear on the same attachment must land first, or it would overwrite this one + enc_b_copy_buffer_texture(blit(), zb, 0, row, img, MTLSizeMake((NSUInteger)(x1 - x0), (NSUInteger)(y1 - y0), 1), mt, sl, lv, MTLOriginMake((NSUInteger)x0, (NSUInteger)y0, 0)); + static uint64_t n; if (!n++) MLOG("剪裁清零:首例 %ldx%ld @%ld,%ld(目标 %lux%lu 级 %lu)", x1 - x0, y1 - y0, x0, y0, (unsigned long)mt.width, (unsigned long)mt.height, (unsigned long)lv); + return 1; +} + + +// ================= Shaders / descriptor pool / pipelines / draws ================= +static pthread_mutex_t g_rmu = PTHREAD_MUTEX_INITIALIZER; +// ---------- Shader mapping table: Switch control section hash → PC metallib (produced by dm/gfx/tools/shader_map.py) ---------- +static NSMutableDictionary *g_shmap; +static NSString *g_msl_dir; +static NSString *exe_rel(const char *rel) { + char buf[4096]; uint32_t n = sizeof buf; + if (_NSGetExecutablePath(buf, &n)) return nil; + char *dir = dirname(buf); + return [NSString stringWithFormat:@"%s/%s", dir, rel]; +} +// Shader mapping table (output of tools/shader_map.py): at runtime only 'Switch control section key → stage' is used; shaders are always translations of the Switch originals (tools/dmsh_build.py produces _b.metallib + reflection offline) +static void shmap_load(void) { + if (g_shmap) return; + const char *e = getenv("DM_NVN_SHMAP"); + NSString *path = e ? @(e) : exe_rel("../games/" DM_GAME_ID "/shader_map.tsv"); // default: the current game profile folder (Mac: dm/games/; on iOS main.m sets DM_NVN_SHMAP) + NSString *txt = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; + if (!txt) MDIE("读不到着色器映射表 %s(DM_NVN_SHMAP 可指定)", path.UTF8String); + g_shmap = [NSMutableDictionary dictionary]; + for (NSString *ln in [txt componentsSeparatedByString:@"\n"]) { + if (!ln.length || [ln hasPrefix:@"#"] || [ln hasPrefix:@"package"]) continue; + NSArray *c = [ln componentsSeparatedByString:@"\t"]; + if (c.count > 3) g_shmap[c[3]] = c[2]; + } + g_shmap_path = path; + const char *m = getenv("DM_MSL_DIR"); + // Library directory = /b-: the version comes from the game profile's dmsh_chain.h (tools/dmsh_chain.py); a host built from an old source tree only accepts libraries of the old version + g_msl_dir = [NSString stringWithFormat:@"%@/b-%s", m ? @(m) : [NSString stringWithFormat:@"%s%s/build/cache/metal_shader_cache", getenv("DM_ROOT") ?: getenv("HOME"), getenv("DM_ROOT") ? "" : "/dm-work"], DMSH_CHAIN]; + if (![NSFileManager.defaultManager fileExistsAtPath:g_msl_dir]) MLOG("【红】着色器库目录 %s 不存在:本源码树的翻译链版本 %s 还没出包(dm/gfx/tools/dmsh_build.py)", g_msl_dir.UTF8String, DMSH_CHAIN); + NSString *cp = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"c1_full.tsv"]; // Same directory as the mapping table (Mac: dm/games/; iOS: dm/ inside the App, copied along by build_ipa.sh) + NSString *ct = cp ? [NSString stringWithContentsOfFile:cp encoding:NSUTF8StringEncoding error:nil] : nil; + NSString *head = ct ? [[ct componentsSeparatedByString:@"\n"] firstObject] : nil; + NSString *want = [NSString stringWithFormat:@"# chain b-%s", DMSH_CHAIN]; + if (!head || ![head hasPrefix:want]) { + MLOG("【红】c1_full.tsv 清单过期或缺失,全部 c1 独占 64 KiB"); + } else { + g_c1_full_valid = 1; g_c1_full = [NSMutableSet set]; + for (NSString *line in [ct componentsSeparatedByString:@"\n"]) if (line.length && ![line hasPrefix:@"#"]) [g_c1_full addObject:[line stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]]; + } + MLOG("着色器路线 B(Switch 原版翻译):映射表 %lu 条(%s),metallib 目录 %s", (unsigned long)g_shmap.count, path.UTF8String, g_msl_dir.UTF8String); + pso_learning_start(); + warm_start(); +} + +@interface NvShader : NSObject { @public id fn, tf_fn; uint16_t tf_stride[4]; nvn_refl rf; id ae; NSUInteger arg_len; int stage, range_ro, afr, specialized, slice_variant, zero_image_variant, zero_image_aid, zero_image_reg; const char *builtin; uint64_t logged; char key[24]; id c1buf; uint64_t c1_gpu; size_t c1off; + id nonpoint_fn; id nonpoint_lib; NSError *nonpoint_error; int nonpoint_tried; + id min_reduction_fn; id min_reduction_lib; int min_reduction_tried, min_reduction_last, min_reduction_notes; + int min_reduction_bound, min_reduction_sid, min_reduction_tid, min_reduction_pso_failed; + // The argument buffer encoded last time + id ab_buf; uint64_t ab_gpu; size_t ab_off; uint64_t ab_pass; uint32_t ab_binst; // ab_pass: serial of the render pass that encoded this argument block - reused only within the same pass (the arena holding a block from another pass / another submission may already have been reclaimed and rewritten) + id bflib; int bflib_tried; NSMutableDictionary> *bffn; } @end // bflib: readable-tile version _bf (merge B step 2) +@implementation NvShader @end +#include "nvn_pso_skey.h" +@interface NvProg : NSObject { @public NvShader *sh[6]; } @end +@implementation NvProg @end +// Warm-up entry: built in the background, waiting to be taken by the submit thread. vs / fs are strong references (in pso they are only raw pointers, and the background dictionary is released when the task ends) +@interface WarmItem : NSObject { @public int kind; NSString *key, *vs_key, *fs_key; NvShader *shader, *vs, *fs; pso_key pso; id fsfn; id object; } @end +@implementation WarmItem @end +static NSMutableDictionary *g_shcache; + +static void c1_store(NvShader *o) { + size_t len = o->rf.c1_len; + NSString *guardKey = [NSString stringWithUTF8String:o->key]; + if ([guardKey hasSuffix:@"_b"]) guardKey = [guardKey substringToIndex:guardKey.length - 2]; + os_unfair_lock_lock(&g_c1_lock); // Both the warm-up thread and the submit thread load shaders + if (!g_c1_full_valid || [g_c1_full containsObject:guardKey]) { // Runtime index not proven bounded: exclusive 64 KiB of all zeros (same as the original implementation) + o->c1buf = [g_dev newBufferWithLength:C1_DECL_SIZE options:MTLResourceStorageModeShared]; o->c1_gpu = legacy_buffer_gpu(o->c1buf); + if (!o->c1buf) { os_unfair_lock_unlock(&g_c1_lock); MDIE("c1 独占缓冲分配失败"); } + res_add(o->c1buf); + memset(o->c1buf.contents, 0, C1_DECL_SIZE); memcpy(o->c1buf.contents, o->rf.c1, len); + g_c1_alloc_bytes += C1_DECL_SIZE; g_c1_data_bytes += len; g_c1_count++; g_c1_exclusive++; + os_unfair_lock_unlock(&g_c1_lock); + return; + } + size_t need = (len + 255u) & ~((size_t)255u); + if (!g_c1_banks) g_c1_banks = [NSMutableArray array]; + if (!g_c1_banks.count || g_c1_bank_off + need + 256u + C1_DECL_SIZE > C1_BANK_SIZE) { + id b = [g_dev newBufferWithLength:C1_BANK_SIZE options:MTLResourceStorageModeShared]; + if (!b) { os_unfair_lock_unlock(&g_c1_lock); MDIE("c1 缓冲块分配失败"); } + res_add(b); + memset(b.contents, 0, C1_BANK_SIZE); + [g_c1_banks addObject:b]; + g_c1_bank_off = 0; + g_c1_alloc_bytes += C1_BANK_SIZE; + } + id b = g_c1_banks.lastObject; + size_t off = g_c1_bank_off; + memcpy((uint8_t *)b.contents + off, o->rf.c1, len); + g_c1_bank_off += need + 256u; + g_c1_data_bytes += len; + g_c1_count++; + o->c1buf = b; + o->c1_gpu = legacy_buffer_gpu(b); + o->c1off = off; + os_unfair_lock_unlock(&g_c1_lock); +} + +static NSMutableArray *g_warm_items; +static os_unfair_lock g_warm_lock = OS_UNFAIR_LOCK_INIT; +static _Atomic int g_warm_ready; +static int g_warm_started; +static uint64_t g_warm_t0; +static _Atomic int g_warm_running; // Background warm-up has not finished yet (the loading screen on entering a run waits a little longer based on this, see mtl_warm_wait) +static uint64_t g_warm_shaders, g_warm_rps, g_warm_cps, g_warm_skip_fz; +static uint64_t g_warm_claim_shaders, g_warm_claim_rps, g_warm_claim_cps; +static id bf_fn(NvShader *fs, const int *fx, uint8_t fbmask, const uint8_t *lmap); +static int rt_range_bindings_ro(NSDictionary *j); +static FILE *g_pso_record; static int g_pso_record_checked; +static FILE *pso_record_file(void) { + if (g_pso_record_checked) return g_pso_record; + g_pso_record_checked = 1; + const char *path = getenv("DM_PSO_RECORD"); + if (path && *path) g_pso_record = fopen(path, "a"); + return g_pso_record; +} +static void pso_record(const char *fmt, ...) { + FILE *f = pso_record_file(); + if (!f) return; + va_list ap; va_start(ap, fmt); vfprintf(f, fmt, ap); va_end(ap); fflush(f); +} +// Build the library from a .metallib file without permanently occupying a file descriptor: newLibraryWithURL keeps that file open for as long as the library object lives (one per game shader - of the 258 descriptors the iOS app permanently holds in a run, 213 are these, +// while the default soft limit of an iOS process is 256; new shaders created in other acts raise it further). Here the file is mapped into memory and closed immediately, and the library is built from the in-memory data; the mapping is released when the library's data object is freed +static id lib_from_file(NSString *path, NSError **err) { + int fd = open(path.fileSystemRepresentation, O_RDONLY); struct stat st; + if (fd < 0 || fstat(fd, &st) || st.st_size <= 0) { if (fd >= 0) close(fd); if (err) *err = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"打不开 %@", path]}]; return nil; } + size_t len = (size_t)st.st_size; void *m = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); close(fd); + if (m == MAP_FAILED) { if (err) *err = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"映射失败 %@", path]}]; return nil; } +#ifdef DM_SHELL + // Shell: when the user's computer has no Apple shader compiler (the no-compiler mode of tools/d2rm_pack.py, gfx/tools/nometal_run.py), the "library files" contain + // shader source rather than compiled libraries (a compiled library starts with MTLB). They are compiled here on the spot; the system caches the compiled result, and from the second launch on the same shader no longer costs this time. + // Measured on a real device (cold): about 80 ms each on average, about 450 ms for the longest single one; one session uses several hundred. + if (len < 4 || memcmp(m, "MTLB", 4)) { NSString *src = [[NSString alloc] initWithBytes:m length:len encoding:NSUTF8StringEncoding]; munmap(m, len); + if (!src) { if (err) *err = [NSError errorWithDomain:NSPOSIXErrorDomain code:EILSEQ userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:@"既不是编好的库也不是源码 %@", path]}]; return nil; } + static _Atomic int n; static _Atomic long long us; CFAbsoluteTime t0 = CFAbsoluteTimeGetCurrent(); + id l = [g_dev newLibraryWithSource:src options:nil error:err]; long long dt = (long long)((CFAbsoluteTimeGetCurrent() - t0) * 1e6); + int k = atomic_fetch_add(&n, 1) + 1; long long tot = atomic_fetch_add(&us, dt) + dt; + if (k == 1 || k % 100 == 0 || dt >= 300000) MLOG("着色器现编:第 %d 个 %.0f ms,累计 %.1f 秒", k, dt / 1e3, tot / 1e6); + return l; } +#endif + dispatch_data_t d = dispatch_data_create(m, len, dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{ munmap(m, len); }); + return [g_dev newLibraryWithData:d error:err]; +} +// NVN depth mode. The offline translator emits both entry points for vertex +// shaders: `entry` converts the default GL-style [-w,+w] depth range to Metal, +// while `entry_z` keeps the already-[0,w] form selected by +// nvnDeviceSetDepthMode. The mode is set before shaders are first loaded. +static _Atomic int g_depth_zero, g_vs_loaded; +static NSString *nonpoint_digest(NSData *data); +#include "nvn_layered_slices.h" +#include "nvn_zero_image_library.h" +#include "nvn_sampler_limit.h" +static id bounded_atomic_library(NSString *key, NSError **error) { + NSString *root = [g_msl_dir stringByAppendingPathComponent:@"atomic-v1"]; + NSData *meta = [NSData dataWithContentsOfFile:[root stringByAppendingPathComponent:[key stringByAppendingString:@".json"]]]; + NSDictionary *record = meta ? [NSJSONSerialization JSONObjectWithData:meta options:0 error:nil] : nil; + NSData *source = [NSData dataWithContentsOfFile:[root stringByAppendingPathComponent:[key stringByAppendingString:@".metal"]]]; + NSData *base = [NSData dataWithContentsOfFile:[g_msl_dir stringByAppendingPathComponent:[key stringByAppendingString:@".metallib"]] options:NSDataReadingMappedIfSafe error:nil]; + if (![record[@"schema"] isEqual:@1] || ![record[@"chain"] isEqual:@DMSH_CHAIN] || ![record[@"kind"] isEqual:@"bounded-image-atomics-v1"] || ![record[@"source_sha256"] isEqual:nonpoint_digest(source)] || ![record[@"library_sha256"] isEqual:nonpoint_digest(base)]) { + if (error) *error = [NSError errorWithDomain:@"NVNLegacyAtomics" code:1 userInfo:@{NSLocalizedDescriptionKey:@"Missing or mismatched bounded image-atomic source; export_legacy_atomics.py is required"}]; + return nil; + } + NSString *src = [[NSString alloc] initWithData:source encoding:NSUTF8StringEncoding]; + MTLCompileOptions *options = [MTLCompileOptions new]; options.languageVersion = MTLLanguageVersion3_0; options.preserveInvariance = YES; options.fastMathEnabled = NO; + id library = [g_dev newLibraryWithSource:src options:options error:error]; + if (library) MLOG("Bounded image-atomic variant %s: image-coordinate guards and array-layer addressing enabled", key.UTF8String); + return library; +} +void mtl_set_depth_zero(int on) { + if (atomic_exchange(&g_depth_zero, on) == on) return; + MLOG("深度模式:%s", on ? "near=0,顶点着色器使用 entry_z" : "near=-w(GL),顶点着色器使用 entry"); + int n = atomic_load(&g_vs_loaded); + if (n) MLOG("【红】已加载 %d 个顶点着色器后才改变深度模式;这些着色器保留旧模式", n); +} +#define NVN_STORAGE_ACCESS_LOG(...) MLOG(__VA_ARGS__) +#include "nvn_storage_access_metal.h" +#undef NVN_STORAGE_ACCESS_LOG +static NvShader *shader_load(NSString *pc, int stage) { + NSError *e = nil; + NSString *lp = [NSString stringWithFormat:@"%@/%@.metallib", g_msl_dir, pc]; + id lib = lib_from_file(lp, &e); + NSData *jd = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.json", g_msl_dir, pc]]; + NSDictionary *j = jd ? [NSJSONSerialization JSONObjectWithData:jd options:0 error:nil] : nil; + if (!lib || !j) { MLOG("【红】加载着色器失败 %s:%s", pc.UTF8String, e ? e.localizedDescription.UTF8String : "无反射 json"); return nil; } + if (![j[@"chain"] isEqual:@DMSH_CHAIN]) { MLOG("【红】着色器 %s 的翻译链版本 %s 与本宿主期望的 %s 不符", pc.UTF8String, [[j[@"chain"] description] UTF8String], DMSH_CHAIN); return nil; } + NvShader *o = [NvShader new]; + if (!g_layered_native) { + lib = layered_slice_library(pc, lib, jd, &o->slice_variant, &e); + if (!lib) { MLOG("[RED] Shader %s per-slice compatibility unavailable: %s", pc.UTF8String, e.localizedDescription.UTF8String); return nil; } + o->specialized = o->slice_variant; + } + if (g_dev.argumentBuffersSupport == MTLArgumentBuffersTier1) { + NSDictionary *limited_j = j; + int sampler_limit_variant = 0; + lib = sampler_limit_library(pc, lib, jd, j, o->specialized, &limited_j, + &sampler_limit_variant, &e); + if (!lib) { + MLOG("[RED] Shader %s Tier 1 sampler compatibility unavailable: %s", + pc.UTF8String, e.localizedDescription.UTF8String); + return nil; + } + j = limited_j; + if (sampler_limit_variant) o->specialized = 1; + } + char err[160]; + if (nvn_refl_from_json(j, &o->rf, err, sizeof err)) { MLOG("【红】着色器 %s 反射转换失败:%s", pc.UTF8String, err); return nil; } + if (stage == 1 && !g_fragment_image_native) { + int images = 0; for (int i = 0; i < o->rf.nb; i++) images += o->rf.b[i].kind == NVN_RB_IMG && o->rf.b[i].rw; + if (images) { + lib = zero_image_library(pc, lib, jd, &o->zero_image_variant, &o->zero_image_aid, &o->zero_image_reg, &e); + if (!lib) { MLOG("[RED] Shader %s fragment image-write compatibility unavailable: %s", pc.UTF8String, e.localizedDescription.UTF8String); return nil; } + int matched = 0; + for (int i = 0; i < o->rf.nb; i++) if (o->rf.b[i].kind == NVN_RB_IMG && o->rf.b[i].rw && o->rf.b[i].aid == o->zero_image_aid && o->rf.b[i].reg == o->zero_image_reg && o->rf.b[i].n <= 1) { + o->rf.b[i].kind = NVN_RB_IMGA; o->rf.b[i].rw = 1; matched++; + } + if (images != 1 || matched != 1 || o->zero_image_reg >= 32 || o->zero_image_reg < 0) { + MLOG("[RED] Shader %s zero-image reflection does not match the proven single image", pc.UTF8String); return nil; + } + o->specialized = 1; + } + } + for (int i = 0; i < o->rf.nb && !o->zero_image_variant; i++) if (o->rf.b[i].kind == NVN_RB_IMGA) { + lib = bounded_atomic_library(pc, &e); + if (!lib) { MLOG("[RED] Shader %s bounded image-atomic variant failed: %s", pc.UTF8String, e.localizedDescription.UTF8String); return nil; } + break; + } + if (rt_range_on()) o->range_ro=rt_range_bindings_ro(j); + NSString *entry = j[@"entry"]; + if (stage == 0) { + atomic_fetch_add(&g_vs_loaded, 1); + if (atomic_load(&g_depth_zero)) { + if (j[@"entry_z"]) entry = j[@"entry_z"]; + else MLOG("【红】顶点着色器 %s 没有 entry_z;仍使用 GL 深度入口", pc.UTF8String); + } + } + o->fn = [lib newFunctionWithName:entry]; o->stage = stage; strlcpy(o->key, pc.UTF8String, sizeof o->key); + if (j[@"entry_tf"]) { + o->tf_fn = [lib newFunctionWithName:j[@"entry_tf"]]; + NSArray *strides = j[@"tf_strides"]; + for (NSUInteger b = 0; b < 4 && b < strides.count; b++) o->tf_stride[b] = (uint16_t)[strides[b] unsignedIntValue]; + if (!o->tf_fn) MLOG("[RED] shader %s has no transform-feedback entry point %s in its library", pc.UTF8String, [j[@"entry_tf"] UTF8String]); + } + if (o->fn.functionConstantsDictionary.count) { + MTLFunctionConstantValues *values = [MTLFunctionConstantValues new]; + NSError *functionError = nil; + id specialized = [lib newFunctionWithName:entry constantValues:values error:&functionError]; + if (!specialized) { MLOG("[RED] Shader %s default function-constant specialization failed: %s", pc.UTF8String, functionError.localizedDescription.UTF8String); return nil; } + o->fn = specialized; o->specialized = 1; + MLOG("Shader %s uses default function-constant specialization", pc.UTF8String); + } + if (o->rf.nb) { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + MTLArgument *argument = nil; + @try { o->ae = nvn_storage_access_encoder(o->fn, o->rf.abi, &argument); } + @catch (NSException *exception) { + MLOG("[RED] Shader %s stage %d argument encoder unavailable: %s: %s", o->key, stage, + exception.name.UTF8String, exception.reason.UTF8String); + return nil; + } + int max_aid = -1; + for (int i = 0; i < o->rf.nb; i++) max_aid = MAX(max_aid, o->rf.b[i].aid); + o->arg_len = o->ae.encodedLength; + if (!o->ae || !o->arg_len || (g_legacy_direct_arguments && + (o->arg_len != (NSUInteger)(8 * (max_aid + 1)) || (o->arg_len & 7)))) + MDIE("着色器 %s 参数块布局异常:encodedLength=%lu maxAid=%d", o->key, (unsigned long)o->arg_len, max_aid); + if (g_legacy_direct_arguments) nvn_storage_access_promote(&o->rf, argument, o->arg_len, o->key, o->stage); + else nvn_argument_private_access(&o->rf, argument); +#pragma clang diagnostic pop + } + o->fn.label = pc; + if (o->rf.has_c1) c1_store(o); + return o; +} + +static int stage_of(NSString *name) { + if ([name isEqualToString:@"vertex"]) return 0; + if ([name isEqualToString:@"pixel"]) return 1; + if ([name isEqualToString:@"hull"]) return 3; + if ([name isEqualToString:@"domain"]) return 4; + if ([name isEqualToString:@"compute"]) return 5; + return -1; +} +#define NVN_RT_RANGE_RO_IMPL 1 +// Private continuation accepts only B reflection that is explicitly RO; a missing rw on sbuf does not let the default 0 count as proof; does not change the existing reflection / translation chain. +static int rt_range_bindings_ro(NSDictionary *j) { + if (![j[@"route"] isEqual:@"B"] || ![j[@"bindings"] isKindOfClass:[NSArray class]] || + ![j[@"drv_reads"] isKindOfClass:[NSDictionary class]] || [j[@"drv_reads"] count]) return 0; + for (NSDictionary *b in j[@"bindings"]) { + if (![b isKindOfClass:[NSDictionary class]] || ![b[@"kind"] isKindOfClass:[NSString class]] || ![b[@"msl_type"] isKindOfClass:[NSString class]]) return 0; + NSString *k=b[@"kind"], *t=b[@"msl_type"]; + if (b[@"rw"] && (![b[@"rw"] isKindOfClass:[NSNumber class]] || [b[@"rw"] boolValue])) return 0; + if ([k isEqual:@"cbuf"] || [k isEqual:@"c1"]) { if (![t hasPrefix:@"constant "]) return 0; } + else if ([k isEqual:@"sbuf"]) { if (![b[@"rw"] isKindOfClass:[NSNumber class]] || [b[@"rw"] boolValue] || ![t hasPrefix:@"const device "]) return 0; } + else if ([k isEqual:@"samp"] || [k isEqual:@"sampt"]) { if (![t isEqual:@"sampler"]) return 0; } + else if ([k isEqual:@"tex"] || [k isEqual:@"tbuf"]) { + if (![t hasPrefix:@"texture"] || ([t containsString:@"access::"] && ![t containsString:@"access::sample>"] && ![t containsString:@"access::read>"])) return 0; } + else return 0; + } + return 1; +} +static int rt_range_pr_ro(NvProg *p) { + if (!p || !p->sh[0] || !p->sh[1]) return 0; + for (int k=0;k<6;k++) if (p->sh[k] && (k>1 || p->sh[k]->stage != k || !p->sh[k]->range_ro || p->sh[k]->rf.drv_read[0] || p->sh[k]->rf.drv_read[2])) return 0; + return 1; +} +static NvShader *shader_get(NSString *pc, int stage) { + if (!g_shcache) g_shcache = [NSMutableDictionary dictionary]; + NvShader *o = g_shcache[pc]; + if (o) return o; + uint64_t bt = bp_now(); o = shader_load(pc, stage); + if (!o) return nil; + g_shcache[pc] = o; + pso_record("S\t%s\t%d\n", pc.UTF8String, stage); + pso_user_record("S\t%s\t%d\n", pc.UTF8String, stage); + g_bp_lib_n++; g_bp_lib_ns += bp_now() - bt; + return o; +} +static id g_builtin_lib; +static NSMutableDictionary *g_builtin; +static NvShader *builtin_get(const char *ctl) { + for (unsigned i = 0; i < sizeof k_nvn_builtin_map / sizeof *k_nvn_builtin_map; i++) { + if (strcmp(ctl, k_nvn_builtin_map[i].ctl)) continue; + if (!g_builtin) g_builtin = [NSMutableDictionary dictionary]; + NvShader *o = g_builtin[@(ctl)]; + if (o) return o; + if (!g_builtin_lib) { + NSError *e = nil; g_builtin_lib = [g_dev newLibraryWithSource:@(k_nvn_builtin_msl) options:nil error:&e]; + if (!g_builtin_lib) MDIE("内嵌计算核 MSL 编译失败:%s", e.localizedDescription.UTF8String); + } + o = [NvShader new]; o->fn = [g_builtin_lib newFunctionWithName:@(k_nvn_builtin_map[i].fn)]; o->stage = 5; o->builtin = k_nvn_builtin_map[i].fn; + g_builtin[@(ctl)] = o; + return o; + } + return nil; +} +static __unsafe_unretained NvProg *prog_get(const prog_t *cp) { // Called only on the submit thread + prog_t *p = (prog_t *)cp; + static const prog_t *lp; static uint64_t lser, lgen; static NvProg *lpr; // Last result: same object, same serial and unchanged serial table means it is used directly + uint64_t gen = g_ser_gen; + if (p == lp && p->serial == lser && gen == lgen && lpr) return lpr; + __unsafe_unretained NvProg *have = p->serial ? serial_get(p->serial) : nil; + if (have) { lp = p; lser = p->serial; lgen = gen; lpr = have; return have; } + shmap_load(); + NvProg *o = [NvProg new]; + for (unsigned i = 0; i < p->nstage; i++) { + // Key = sha256(first 0x880 bytes of the control section + code area): many of Mario's shaders share a control section but differ in code (dm/gfx/tools/shader_split.py). It was computed at SetShaders (hle_nvn.c): by now the game may have freed the control section + uint8_t md[8]; prog_key(p, i, md); + NSString *k = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x", md[0], md[1], md[2], md[3], md[4], md[5], md[6], md[7]]; + NSString *stageName = g_shmap[k]; + if (!stageName) { // Not in the package: look up the compute kernels embedded in the main body (nvn_builtin.h) + NvShader *b = builtin_get(k.UTF8String); + if (b) { o->sh[5] = b; continue; } + MLOG("【红】着色器 %s 不在映射表里(DM_NVN_SHDUMP=目录 可把控制段与代码区存盘)", k.UTF8String); + // DM_NVN_SHDUMP=directory: shaders missing from the map are saved as .ctl (0x880-byte control section) and .code (code area, length from control +0x704), to trace their source / add them to the map offline + static const char *shd; static int shd_init; if (!shd_init) { shd = getenv("DM_NVN_SHDUMP"); shd_init = 1; } + if (shd) { char path[1024]; FILE *f; uint32_t ncode; memcpy(&ncode, (const uint8_t *)p->sd[i].control + 0x704, 4); // with SHDUMP set the control section is the host's copy + snprintf(path, sizeof path, "%s/%s.ctl", shd, k.UTF8String); if ((f = fopen(path, "wb"))) { fwrite(p->sd[i].control, 1, 0x880, f); fclose(f); } + snprintf(path, sizeof path, "%s/%s.code", shd, k.UTF8String); + if (p->sd[i].data && ncode && ncode < (1u << 24) && (f = fopen(path, "wb"))) { fwrite((const void *)(uintptr_t)p->sd[i].data, 1, ncode, f); fclose(f); } } + continue; + } + int st = stage_of(stageName); + NvShader *sh = st >= 0 ? shader_get([k stringByAppendingString:@"_b"], st) : nil; + if (sh) o->sh[st] = sh; else MLOG("【红】着色器 %s(%s)加载失败:先跑 dm/gfx/tools/dmsh_build.py 出包", k.UTF8String, stageName.UTF8String); + } + if (p->serial) serial_put(p->serial, o); + return o; +} +void mtl_prog_release(prog_t *p) { if (p->serial) serial_del(p->serial); } + +// ---------- Descriptor pool: texture ID / sampler ID → object ---------- +// At registration the whole texture object is snapshotted (NVN semantics: the descriptor has been written into the pool); view: the MTLTexture for that ID (a view or the base texture, held via CFBridgingRetain) and the serial-table / guest-write generations at the time it was obtained +typedef struct { tex_t t; int ok; view_t v; int has_v; void *view; uint64_t view_id, view_rid, view_ser, view_dirty; int view_pkg; // view_pkg: CPU-written (packaged); only those depend on guest writes (view_dirty) + void *alt, *alt_of; uint64_t alt_rid; uint8_t alt_type; // alt: compatible view used when the shader's declared type differs from view (tex_fit), alt_of = the view it was built from + void *ar, *ar_of; uint64_t ar_rid; } treg_t; // ar: the alpha-from-red view for the profile's alpha_from_red shaders (tex_alpha_red), ar_of = the view it was built from +static treg_t *g_treg; static int g_ntreg; +// Lock-free copy of texture ID → serial (the replay hot path only needs the serial: store lookahead index, binding set of the clear-only hoist): texture IDs are 20 bits, fixed 1M entries (BSS, only touched pages are used), +// release write at registration, relaxed atomic read on the reader side, without entering g_rmu (the former h_ser locked and unlocked on every lookup, thousands of times per frame in the camp) +static _Atomic uint64_t g_tser[1 << 20]; +static samp_par *g_sreg; static uint8_t *g_sreg_ok; static void **g_sview; static uint64_t *g_srid; static int g_nsreg; // g_sview: sampler ID → MTLSamplerState (retained) +// Memory pool Finalize: for texture IDs whose registered texture lives on this pool, drop the cached MTLTexture (view or base texture). The cache used to be released only when the ID was registered again, +// so a linear texture built directly on a pool kept the whole pool buffer alive (6 to 17 finalized 16 MB pool buffers stayed alive in Engage's title demo). If the ID is still used, the next lookup rebuilds it on the slow path +static void treg_drop_ar(treg_t *r) { if (r->ar) { defer_replay_view_unref((__bridge id)r->ar); CFRelease(r->ar); r->ar = r->ar_of = NULL; r->ar_rid = 0; } } +static void treg_drop_pool_views(const void *pool) { + pthread_mutex_lock(&g_rmu); + for (int i = 0; i < g_ntreg; i++) + if (g_treg[i].ok && (const void *)g_treg[i].t.pool == pool) { + // The base view may also be referenced by independent upload or + // present command buffers, neither of which advances g_fence. + if (g_treg[i].view) { defer_obj_unref((__bridge id)g_treg[i].view); CFRelease(g_treg[i].view); g_treg[i].view = NULL; g_treg[i].view_id = g_treg[i].view_rid = 0; } + // alt/ar are replay-only derived views. Both retain their parent; + // leaving alt attached here pins the finalized pool even after the + // base cache entry is gone. + if (g_treg[i].alt) { defer_replay_view_unref((__bridge id)g_treg[i].alt); CFRelease(g_treg[i].alt); g_treg[i].alt = g_treg[i].alt_of = NULL; g_treg[i].alt_rid = 0; g_treg[i].alt_type = 0; } + treg_drop_ar(&g_treg[i]); } + pthread_mutex_unlock(&g_rmu); + defer_replay_view_drain_safe_point(); +} +void mtl_reg_tex(int tid, const tex_t *t, const view_t *v) { + int id_ = tid; if (id_ < 0) return; + pthread_mutex_lock(&g_rmu); + if (id_ >= g_ntreg) { int n = id_ + 1024; g_treg = realloc(g_treg, n * sizeof *g_treg); memset(g_treg + g_ntreg, 0, (n - g_ntreg) * sizeof *g_treg); g_ntreg = n; } + g_reg_gen++; + // Keep the base view conservative: a present/readback/upload CB may still + // hold an unsafe reference and those independent CBs do not signal g_done. + if (g_treg[id_].view) { defer_obj_unref((__bridge id)g_treg[id_].view); CFRelease(g_treg[id_].view); g_treg[id_].view = NULL; g_treg[id_].view_id = g_treg[id_].view_rid = 0; } + if (g_treg[id_].alt) { defer_replay_view_unref((__bridge id)g_treg[id_].alt); CFRelease(g_treg[id_].alt); g_treg[id_].alt = g_treg[id_].alt_of = NULL; g_treg[id_].alt_rid = 0; } + treg_drop_ar(&g_treg[id_]); + g_treg[id_].ok = t != NULL; if (t) g_treg[id_].t = *t; g_treg[id_].has_v = v != NULL; if (v) g_treg[id_].v = *v; + if (id_ < (1 << 20)) atomic_store_explicit(&g_tser[id_], t ? t->serial : 0, memory_order_release); + pthread_mutex_unlock(&g_rmu); + defer_replay_view_drain_safe_point(); + { // DM_NVN_TEXINFO=WxH also logs registrations of textures of that size: the channel swizzle from the builder and from the view + static int ti = -1; static uint32_t tw, th; static uint64_t n; + if (ti < 0) { const char *e = getenv("DM_NVN_TEXINFO"); ti = e && sscanf(e, "%ux%u", &tw, &th) == 2; } + if (ti && t && t->d.width == tw && t->d.height == th && (n++ < 40 || n % 600 == 0)) + MLOG("TEXINFO 登记 id %d ser=%llu 格式=%#x 建时重排 %d:%d%d%d%d 视图 %d 视图重排 %d:%d%d%d%d 视图格式 %d:%#x", tid, (unsigned long long)t->serial, t->d.format, + t->set_swz, t->swz & 7, t->swz >> 3 & 7, t->swz >> 6 & 7, t->swz >> 9 & 7, v != NULL, v ? v->set_swizzle : 0, v ? v->swz[0] : 0, v ? v->swz[1] : 0, v ? v->swz[2] : 0, v ? v->swz[3] : 0, + v ? v->set_format : 0, v ? v->format : 0); + } +} +void mtl_reg_samp(int sid, const samp_par *sp) { + int id_ = sid; if (id_ < 0) return; + pthread_mutex_lock(&g_rmu); + if (id_ >= g_nsreg) { int n = id_ + 256; g_sreg = realloc(g_sreg, n * sizeof *g_sreg); g_sreg_ok = realloc(g_sreg_ok, n); g_sview = realloc(g_sview, n * sizeof *g_sview); g_srid = realloc(g_srid, n * sizeof *g_srid); + memset(g_sreg_ok + g_nsreg, 0, n - g_nsreg); memset(g_sview + g_nsreg, 0, (n - g_nsreg) * sizeof *g_sview); memset(g_srid + g_nsreg, 0, (n - g_nsreg) * sizeof *g_srid); g_nsreg = n; } + if (g_sview[id_]) { + static unsigned replacements; + if (replacements++ < 128) MLOG("Sampler replacement frame=%llu sid=%d old-rid=%#llx lod=%g..%g bias=%g; GPU references retained by submission", (unsigned long long)atomic_load(&g_cur_frame), id_, (unsigned long long)g_srid[id_], sp->lod_min, sp->lod_max, sp->lod_bias); + CFRelease(g_sview[id_]); g_sview[id_] = NULL; g_srid[id_] = 0; + } + g_reg_gen++; + g_sreg[id_] = *sp; g_sreg_ok[id_] = 1; + pthread_mutex_unlock(&g_rmu); +} +// Handles of deferred binds (BindTexturesDeferred) are read at execution: the record keeps the address of the handles (see hle_nvn.c); every reader of a bind record refreshes it first. Command memory is writable, so it is updated in place +static inline void bh_fresh(const p_bind_handles *b) { + if (b->defer && b->src && b->n > 0 && b->n <= 32) memcpy((void *)b->h, (const void *)(uintptr_t)b->src, 8 * (size_t)b->n); +} +static MTLTextureType ttype(int target, int fallback) { + switch (target) { case 0: return MTLTextureType2D; case 1: case 7: return MTLTextureType2D; case 2: return MTLTextureType3D; // 1D as one-row 2D (see where textures are created) + case 3: return MTLTextureType2DArray; case 4: return MTLTextureType2DArray; case 5: return MTLTextureType2DMultisample; + case 6: return MTLTextureType2DMultisampleArray; case 8: return MTLTextureTypeCube; case 9: return MTLTextureTypeCubeArray; } + return (MTLTextureType)fallback; +} +// Out-of-pool texture ID = empty slot: the game itself uses GetSeparateTextureHandle(0xFFFFF) to get a handle and bind it to unused slots (measured call site main+0x1746820, +// texture pool capacity 15256) and never registers that ID. Read 0 per D3D / empty-slot semantics (the same all-zero dummy textures as unbound), no failure; unregistered IDs inside the pool are still reported a failure +static int g_tpool_n; static uint64_t g_stat_tex_oob; +// NVN window origin. In lower-left mode (GL convention, NVN's default) NDC y = -1 lands on a render target's first memory row and window y grows with the row; Metal puts NDC y = +1 on the first row. +// Viewport and scissor rectangles, fragment coordinates, copies and texture coordinates all address the same memory rows in both conventions, so only the direction inside the viewport differs: +// lower-left draws get a viewport of negative height (and the opposite front-face winding), and the window image, stored bottom-up, is flipped at present (and in raw screenshots). +// Link's Awakening keeps the default: read as upper-left, every texture it renders and samples came out consistently flipped and cancelled out, except where the game compensates for the +// convention itself (text rendered into textures for the title and the menus came out upside down, and a MIRROR_Y copy of the frame) +static _Atomic int g_origin_ll; +void mtl_set_origin_lower_left(int on) { if (atomic_exchange(&g_origin_ll, on) != on) MLOG("窗口原点:%s", on ? "左下(GL,视口倒向、绕向反、出图翻转)" : "左上"); } +static int g_queue_origin_ll; // SET_WINDOW_ORIGIN's raster flip is queue state, independent of later device-origin changes. +void mtl_set_queue_origin_lower_left(int on) { g_queue_origin_ll = on != 0; } +void mtl_set_tex_pool_n(int n) { g_tpool_n = n; } +static id tex_by_id(int tid) { // Caller holds g_rmu + if (g_tpool_n && tid >= g_tpool_n) { if (!g_stat_tex_oob++) MLOG("池外贴图号 %d(池容量 %d)按空槽处理:读出 0", tid, g_tpool_n); return nil; } + if (tid < 0 || tid >= g_ntreg || !g_treg[tid].ok) { RED_ONCE(10, "贴图号 %d 未登记", tid); return nil; } + treg_t *rp = &g_treg[tid]; + uint64_t sg = g_ser_gen, dg = nvn_dirty_now(); // Take the generations first and then look up: if a change happens in between, the next time must take the slow path + if (rp->view && rp->view_ser == sg && (!rp->view_pkg || rp->view_dirty == dg)) return (__bridge id)rp->view; + id v = (__bridge id)rp->view; + // Guest writes (nvn_dirty_now) matter only to CPU-written textures: a texture that becomes CPU-written later bumps g_ser_gen once (mtl_tex_cpu_written), which sends its ID through here again. + // Before, every guest write to any protected page sent every texture ID, render targets included, down this path + rp->view_pkg = (rp->t.has_layout || tex_cpu_written(rp->t.serial)) && rp->t.mem; + if (v) { // The serial table or guest memory has changed: check whether the base texture was re-uploaded and replaced; if so the view is invalidated + id cur = tex_mtl(&rp->t); + if (cur == v || v.parentTexture == cur) { rp->view_ser = sg; rp->view_dirty = dg; return v; } + defer_obj_unref(v); CFRelease(rp->view); rp->view = NULL; rp->view_rid = 0; v = nil; + } + treg_t r = *rp; + id base = tex_mtl(&rp->t); + if (!base) return nil; + v = base; + // Channel swizzle: the view's if set, otherwise the one from texture building; identity builds no view (SMBW's font cache is (1 1 1 R): without the swizzle glyphs become white blocks) + uint8_t sw[4] = {MTLTextureSwizzleRed, MTLTextureSwizzleGreen, MTLTextureSwizzleBlue, MTLTextureSwizzleAlpha}; int need_sw = 0; + if (r.has_v && r.v.set_swizzle) memcpy(sw, r.v.swz, 4); + else if (r.t.set_swz) for (int c = 0; c < 4; c++) sw[c] = (uint8_t)(r.t.swz >> (3 * c) & 7); + // Format conversion is applied before the builder/view channel selection. RGBX alpha is 1; + // packed RGB formats may swap red/blue even when the explicit channel selection is identity. + static int format_swizzle = -1; + if (format_swizzle < 0) { const char *e = getenv("DM_NVN_FORMAT_SWIZZLE"); format_swizzle = e ? strcmp(e, "0") != 0 : DM_GAME_PROGRAM_ID == 0x010015100B514000ull; } + uint8_t format_sw[4] = {NVN_SWZ_R, NVN_SWZ_G, NVN_SWZ_B, NVN_SWZ_A}; uint32_t ignored_pf; + if (format_swizzle && !nvn_fmt_to_mtl(r.has_v && r.v.set_format ? r.v.format : r.t.d.format, &ignored_pf, format_sw)) nvn_fmt_to_mtl(r.t.d.format, &ignored_pf, format_sw); + for (int c = 0; c < 4; c++) { + if (sw[c] > MTLTextureSwizzleAlpha) sw[c] = (uint8_t)(MTLTextureSwizzleRed + c); + if (sw[c] >= MTLTextureSwizzleRed) sw[c] = format_sw[sw[c] - MTLTextureSwizzleRed]; + if (sw[c] != MTLTextureSwizzleRed + c) need_sw = 1; + } + if (format_swizzle) { static unsigned reports; int changed = 0; for (int c = 0; c < 4; c++) changed |= format_sw[c] != NVN_SWZ_R + c; + if (changed && reports++ < 16) MLOG("[color] 格式通道修正 tid=%d nvn=%#x view=%#x 最终=%u,%u,%u,%u", tid, r.t.d.format, r.has_v && r.v.set_format ? r.v.format : 0, sw[0],sw[1],sw[2],sw[3]); } + // STENCIL depth/stencil mode means sampling the stencil plane, not the + // depth plane. Z-A's deferred-lighting shader reads this view as raw + // float bits and tests stencil bit 6; binding depth instead produces the + // characteristic black/white bands that follow scene depth. + int want_st = r.has_v && r.v.set_dsmode ? r.v.dsmode == NVN_TEXTURE_DEPTH_STENCIL_MODE_STENCIL : r.t.stencil; + int stv = want_st && base.pixelFormat == MTLPixelFormatDepth32Float_Stencil8; + if (want_st && !stv) + RED_ONCE(104, "stencil-mode texture view of Metal format %lu (texture ID %d): no stencil-only view, depth is read", (unsigned long)base.pixelFormat, tid); + if ((r.has_v && (r.v.set_format || r.v.set_levels || r.v.set_layers || r.v.set_target)) || need_sw || stv) { + if (!r.has_v) memset(&r.v, 0, sizeof r.v); + uint32_t pf = (uint32_t)base.pixelFormat; uint8_t swz[4]; + if (r.v.set_format && !(nvn_fmt_to_mtl(r.v.format, &pf, swz) && pf)) pf = (uint32_t)base.pixelFormat; + { uint32_t upf; int ubpp, kind; + if (r.v.set_format && is_astc4(base.pixelFormat) && nvn_bc_astc_fmt((uint32_t)r.v.format, &upf, &kind)) pf = upf; + else if (r.v.set_format && bc_unpack(r.v.format, &upf, &ubpp)) pf = upf; } + if (stv) pf = MTLPixelFormatX32_Stencil8; + if (r.v.set_format && nvn_fmt_bc(rp->t.d.format) && !nvn_fmt_bc(r.v.format)) { // the raw blocks of a BC texture through an uncompressed view (see rt_view_fmt): Metal has no such view, with or without BC + RED_ONCE(86, "压缩格式贴图的原始块视图(NVN 格式 %#x → 视图 %#x)Metal 建不出:按空槽", rp->t.d.format, r.v.format); + return nil; + } + if (tex_need_pfv(base.pixelFormat, (MTLPixelFormat)pf) && !(base.usage & MTLTextureUsagePixelFormatView)) { + base = tex_upgrade_pfv(&rp->t, base, (MTLPixelFormat)pf); + sg = g_ser_gen; + } + NSUInteger l0 = r.v.set_levels ? r.v.base_level : 0, nl = r.v.set_levels && r.v.nlevels ? r.v.nlevels : base.mipmapLevelCount - l0; + NSUInteger s0 = r.v.set_layers ? r.v.min_layer : 0; + NSUInteger ns = base.textureType == MTLTextureType3D ? 1 : (r.v.set_layers && r.v.nlayers ? r.v.nlayers : base.arrayLength * (base.textureType == MTLTextureTypeCube || base.textureType == MTLTextureTypeCubeArray ? 6 : 1) - s0); + MTLTextureType tt = r.v.set_target ? ttype(r.v.target, base.textureType) : base.textureType; + if (tt == MTLTextureType2D && ns > 1) ns = 1; + v = need_sw ? [base newTextureViewWithPixelFormat:(MTLPixelFormat)pf textureType:tt levels:NSMakeRange(l0, nl) slices:NSMakeRange(s0, ns) swizzle:MTLTextureSwizzleChannelsMake(sw[0], sw[1], sw[2], sw[3])] + : [base newTextureViewWithPixelFormat:(MTLPixelFormat)pf textureType:tt levels:NSMakeRange(l0, nl) slices:NSMakeRange(s0, ns)]; + if (!v) { RED_ONCE(11, "贴图视图建不出 id %d", tid); v = base; } + } + rp->view = (void *)CFBridgingRetain(v); rp->view_rid = legacy_texture_rid(v); rp->view_ser = sg; rp->view_dirty = dg; + return v; +} +// Hot-path object fetch: if the table entry is valid, return the table-held pointer directly (without ARC retain / autorelease), otherwise take the slow path to build / validate. Caller holds g_rmu. +static inline __attribute__((always_inline)) void *tex_fast(int tid) { + if (tid > 0 && tid < g_ntreg && g_treg[tid].ok && g_treg[tid].view && g_treg[tid].view_ser == g_ser_gen && (!g_treg[tid].view_pkg || g_treg[tid].view_dirty == nvn_dirty_now())) return g_treg[tid].view; + return (__bridge void *)tex_by_id(tid); +} +// Some Z-A descriptors expose one ordinary 2D texture even though the translated sampling +// instruction is a cube read. Metal cannot make a cube view from a standalone 2D allocation; +// binding it anyway returns undefined (black on Apple M2). Promote the image to a six-slice +// private allocation on a separate, earlier-committed command buffer, then sample its cube +// view. Square images are replicated to every face. The two common six-face strip layouts +// and the 4x3 / 3x4 cross layouts are split into their individual faces. +// +// The copy command buffer is committed while the current render command buffer is still being +// encoded. Metal executes committed buffers in queue order, so the promotion finishes before +// the current draw without ending its render pass. A packaged upload is flushed first for the +// same reason. The alternate view cache is invalidated automatically when the source MTLTexture +// object changes after a guest rewrite. +static id tex_promote_2d_cube(int tid, id t, uint64_t *rid) { + if (!t || t.textureType != MTLTextureType2D || t.sampleCount != 1 || !t.width || !t.height) return nil; + + NSUInteger face = MIN(t.width, t.height), layout = 0; + if (t.width >= t.height * 6) { face = t.height; layout = 1; } + else if (t.height >= t.width * 6) { face = t.width; layout = 2; } + else if (t.width * 3 == t.height * 4) { face = t.width / 4; layout = 3; } + else if (t.width * 4 == t.height * 3) { face = t.width / 3; layout = 4; } + if (!face) return nil; + + NSUInteger levels = MIN(t.mipmapLevelCount, (NSUInteger)(64 - __builtin_clzll(face))); + MTLTextureDescriptor *d = [MTLTextureDescriptor new]; + d.textureType = MTLTextureType2DArray; d.pixelFormat = t.pixelFormat; + d.width = face; d.height = face; d.depth = 1; d.arrayLength = 6; + d.mipmapLevelCount = MAX(levels, 1u); d.sampleCount = 1; + d.storageMode = MTLStorageModePrivate; d.usage = MTLTextureUsageShaderRead; + id base = [g_dev newTextureWithDescriptor:d]; + if (!base) return nil; + res_add(base); + + up_flush(); + id cb = [g_q commandBuffer]; + cb.label = [NSString stringWithFormat:@"promote tid %d 2D to cube", tid]; + id b = [cb blitCommandEncoder]; + static const uint8_t cross4x3[6][2] = {{2,1},{0,1},{1,0},{1,2},{1,1},{3,1}}; + static const uint8_t cross3x4[6][2] = {{2,1},{0,1},{1,0},{1,2},{1,1},{1,3}}; + for (NSUInteger lv = 0; lv < d.mipmapLevelCount; lv++) { + NSUInteger f = MAX(face >> lv, 1u); + for (NSUInteger sl = 0; sl < 6; sl++) { + NSUInteger x = 0, y = 0; + if (layout == 1) x = sl * f; + else if (layout == 2) y = sl * f; + else if (layout == 3) { x = cross4x3[sl][0] * f; y = cross4x3[sl][1] * f; } + else if (layout == 4) { x = cross3x4[sl][0] * f; y = cross3x4[sl][1] * f; } + NSUInteger sw = MAX(t.width >> lv, 1u), sh = MAX(t.height >> lv, 1u); + if (x + f > sw || y + f > sh) { x = y = 0; f = MIN(f, MIN(sw, sh)); } + [b copyFromTexture:t sourceSlice:0 sourceLevel:lv sourceOrigin:MTLOriginMake(x, y, 0) + sourceSize:MTLSizeMake(f, f, 1) toTexture:base destinationSlice:sl destinationLevel:lv destinationOrigin:MTLOriginMake(0, 0, 0)]; + } + } + [b endEncoding]; + [cb addCompletedHandler:^(id done) { if (done.error) MLOG("【红】2D→Cube 提升拷贝失败(贴图号 %d):%s", tid, done.error.description.UTF8String); }]; + res_commit(); [cb commit]; + + id v = [base newTextureViewWithPixelFormat:t.pixelFormat textureType:MTLTextureTypeCube + levels:NSMakeRange(0, d.mipmapLevelCount) slices:NSMakeRange(0, 6) swizzle:t.swizzle]; + if (!v) { res_remove(base); return nil; } + treg_t *r = &g_treg[tid]; + r->alt = (void *)CFBridgingRetain(v); r->alt_of = (__bridge void *)t; + r->alt_type = (uint8_t)MTLTextureTypeCube; r->alt_rid = legacy_texture_rid(v); + *rid = r->alt_rid; + static uint64_t promoted; + MLOG("独立 2D 贴图提升为 Cube:tid=%d ser=%llu %lux%lu fmt=%lu 布局=%lu(第 %llu 张)", tid, + tid > 0 && tid < g_ntreg ? (unsigned long long)g_treg[tid].t.serial : 0ull, + (unsigned long)t.width, (unsigned long)t.height, (unsigned long)t.pixelFormat, + (unsigned long)layout, (unsigned long long)++promoted); + return v; +} +// The texture type the shader declares differs from the one the descriptor gives: use a compatible view. Maxwell sample instructions carry their own dimension, and a non-array instruction reading a 2D array texture reads layer 0; +// on Metal a 2D array bound to a texture2d argument is a type mismatch with undefined reads (measured: one fixed value). Super Mario 3D World + Bowser's Fury creates its pre-lighting buffer as a one-layer 2D array +// and the deferred lighting shader samples it as 2D: the ambient light of the whole picture became one colour, washed out white. Supports 2D array <-> 2D and 2D array -> cube / cube array; any other mismatch is reported once. Caller holds g_rmu +static id tex_fit(int tid, id t, MTLTextureType want, uint64_t *rid) { + MTLTextureType have = t.textureType; NSUInteger ns = 0, l0 = 0, nl = t.mipmapLevelCount, s0 = 0; id src = t; + int cube_parent = t.parentTexture && t.parentTexture.textureType == MTLTextureType2DArray && + t.parentRelativeSlice + 6 <= t.parentTexture.arrayLength; + if (want == MTLTextureTypeCube && have == MTLTextureType2D && !cube_parent && tid > 0 && tid < g_ntreg) { + treg_t *r = &g_treg[tid]; + if (r->alt && r->alt_of == (__bridge void *)t && r->alt_type == (uint8_t)want) { *rid = r->alt_rid; return (__bridge id)r->alt; } + if (r->alt) { defer_obj_unref((__bridge id)r->alt); CFRelease(r->alt); r->alt = r->alt_of = NULL; r->alt_rid = 0; } + id promoted = tex_promote_2d_cube(tid, t, rid); + if (promoted) return promoted; + } + if (want == MTLTextureType2D && have == MTLTextureType2DArray) ns = 1; + else if (want == MTLTextureType2DArray && have == MTLTextureType2D) ns = 1; + else if (want == MTLTextureTypeCube && have == MTLTextureType2DArray && t.arrayLength >= 6) ns = 6; + else if (want == MTLTextureTypeCubeArray && have == MTLTextureType2DArray && t.arrayLength >= 6) ns = t.arrayLength / 6 * 6; + else if (want == MTLTextureTypeCube && have == MTLTextureTypeCubeArray) ns = 6; // a cube array read as a cube: the first cube + else if (want == MTLTextureTypeCube && have == MTLTextureType2D && t.parentTexture && t.parentTexture.textureType == MTLTextureType2DArray && + t.parentRelativeSlice + 6 <= t.parentTexture.arrayLength) { // A descriptor view narrowed a cube's 2D-array base to one 2D slice, but the sampling instruction still declares cube + src = t.parentTexture; ns = 6; l0 = t.parentRelativeLevel; nl = t.mipmapLevelCount; s0 = t.parentRelativeSlice; + } + if (!ns || tid <= 0 || tid >= g_ntreg) { RED_ONCE(76, "贴图类型不符:着色器声明 %lu,贴图 %lu(贴图号 %d),照原样绑", (unsigned long)want, (unsigned long)have, tid); return nil; } + treg_t *r = &g_treg[tid]; + if (r->alt && r->alt_of == (__bridge void *)t && r->alt_type == (uint8_t)want) { *rid = r->alt_rid; return (__bridge id)r->alt; } + if (r->alt) { defer_obj_unref((__bridge id)r->alt); CFRelease(r->alt); r->alt = r->alt_of = NULL; r->alt_rid = 0; } + id v = [src newTextureViewWithPixelFormat:t.pixelFormat textureType:want levels:NSMakeRange(l0, nl) slices:NSMakeRange(s0, ns) swizzle:t.swizzle]; + if (!v) { RED_ONCE(77, "贴图类型兼容视图建不出:%lu → %lu(贴图号 %d)", (unsigned long)have, (unsigned long)want, tid); return nil; } + r->alt = (void *)CFBridgingRetain(v); r->alt_of = (__bridge void *)t; r->alt_type = (uint8_t)want; r->alt_rid = legacy_texture_rid(v); + *rid = r->alt_rid; return v; +} +// Alpha from red: the fragment shaders the game profile's alpha_from_red lists read a single-channel render target whose channel mapping is exactly (R, 0, 0, 1) as (R, 0, 0, R). +// A workaround for NintendoWare layout captures (BotW, Odyssey): the game rebuilds each capture on the same storage with its own mapping ((1, 1, 1, R) for an A8 capture), which was lost while +// nvnTextureCompare ignored the mapping and left the creation-time (R, 0, 0, 1), alpha always 1 (hard-edged prompt boxes, white coin cards). Since that fix it no longer fires (docs/agents/04-graphics.md). +// Only render targets qualify (captures always are; asset and CPU-written textures are not). DM_NVN_ALPHA_RED=0 turns it off, replaces the list. Caller holds g_rmu +static char g_afr_keys[48][20]; static int g_afr_nk = -1; +static int afr_want(NvShader *sh) { + if (!sh->afr) { + if (g_afr_nk < 0) { g_afr_nk = 0; const char *e = getenv("DM_NVN_ALPHA_RED"), *src = e && *e ? e : ""; + if (strcmp(src, "0")) for (const char *q = src; *q && g_afr_nk < 48;) { size_t n = strcspn(q, ", "); if (n) { if (n > 16) n = 16; memcpy(g_afr_keys[g_afr_nk], q, n); g_afr_keys[g_afr_nk++][n] = 0; } q += n; q += strspn(q, ", "); } + if (g_afr_nk) MLOG("单通道贴图 alpha 取红:%s%s", src, e && *e ? "(DM_NVN_ALPHA_RED)" : "(游戏档案 alpha_from_red)"); } + sh->afr = 1; for (int i = 0; i < g_afr_nk; i++) if (!strncmp(sh->key, g_afr_keys[i], strlen(g_afr_keys[i]))) sh->afr = 2; + } + return sh->afr == 2; +} +static id tex_alpha_red(int tid, id t, uint64_t *rid) { + MTLPixelFormat pf = t.pixelFormat; MTLTextureSwizzleChannels sw = t.swizzle; + if (pf != MTLPixelFormatR8Unorm && pf != MTLPixelFormatR16Unorm && pf != MTLPixelFormatR16Float && pf != MTLPixelFormatR32Float) return nil; + if (sw.red != MTLTextureSwizzleRed || sw.green != MTLTextureSwizzleZero || sw.blue != MTLTextureSwizzleZero || sw.alpha != MTLTextureSwizzleOne || tid <= 0 || tid >= g_ntreg) return nil; + id base = t.parentTexture ? t.parentTexture : t; + if (!(base.usage & MTLTextureUsageRenderTarget)) return nil; // packaged and CPU-written textures are built without render-target usage + treg_t *r = &g_treg[tid]; + if (r->ar && r->ar_of == (__bridge void *)t) { *rid = r->ar_rid; return (__bridge id)r->ar; } + treg_drop_ar(r); + NSUInteger l0 = t.parentTexture ? t.parentRelativeLevel : 0, s0 = t.parentTexture ? t.parentRelativeSlice : 0; + NSUInteger ns = t.textureType == MTLTextureType3D ? 1 : t.arrayLength * (t.textureType == MTLTextureTypeCube || t.textureType == MTLTextureTypeCubeArray ? 6 : 1); + id v = [base newTextureViewWithPixelFormat:pf textureType:t.textureType levels:NSMakeRange(l0, t.mipmapLevelCount) slices:NSMakeRange(s0, ns) + swizzle:MTLTextureSwizzleChannelsMake(MTLTextureSwizzleRed, MTLTextureSwizzleZero, MTLTextureSwizzleZero, MTLTextureSwizzleRed)]; + if (!v) { RED_ONCE(82, "alpha 取红的视图建不出(贴图号 %d 格式 %lu)", tid, (unsigned long)pf); return nil; } + { static int once; if (!once++) MLOG("单通道贴图 alpha 取红:首例 贴图号 %d %lux%lu", tid, (unsigned long)t.width, (unsigned long)t.height); } + r->ar = (void *)CFBridgingRetain(v); r->ar_of = (__bridge void *)t; r->ar_rid = legacy_texture_rid(v); + *rid = r->ar_rid; return v; +} +static inline __attribute__((always_inline)) uint64_t tex_fast_rid(int tid, void **out) { + if (tid > 0 && tid < g_ntreg && g_treg[tid].ok && g_treg[tid].view && g_treg[tid].view_ser == g_ser_gen && (!g_treg[tid].view_pkg || g_treg[tid].view_dirty == nvn_dirty_now())) { if (out) *out = g_treg[tid].view; return g_treg[tid].view_rid; } + id t = tex_by_id(tid); if (out) *out = (__bridge void *)t; return t ? legacy_texture_rid(t) : 0; +} +static MTLSamplerAddressMode wrapm(int w) { + switch (w) { case 0: case 7: return MTLSamplerAddressModeClampToEdge; case 1: return MTLSamplerAddressModeRepeat; + case 3: case 2: return MTLSamplerAddressModeMirrorClampToEdge; case 5: case 4: return MTLSamplerAddressModeClampToBorderColor; + case 6: return MTLSamplerAddressModeMirrorRepeat; } + return MTLSamplerAddressModeRepeat; +} +static id samp_by_id(int sid) { // Caller holds g_rmu + if (sid < 0 || sid >= g_nsreg || !g_sreg_ok[sid]) { RED_ONCE(12, "采样器号 %d 未登记", sid); return nil; } + if (g_sview[sid]) return (__bridge id)g_sview[sid]; + samp_par p = g_sreg[sid]; + MTLSamplerDescriptor *d = [MTLSamplerDescriptor new]; + d.magFilter = p.magf ? MTLSamplerMinMagFilterLinear : MTLSamplerMinMagFilterNearest; + d.minFilter = (p.minf == 1 || p.minf == 3 || p.minf == 5) ? MTLSamplerMinMagFilterLinear : MTLSamplerMinMagFilterNearest; + d.mipFilter = p.minf <= 1 ? MTLSamplerMipFilterNotMipmapped : (p.minf == 2 || p.minf == 3) ? MTLSamplerMipFilterNearest : MTLSamplerMipFilterLinear; + d.sAddressMode = wrapm(p.ws); d.tAddressMode = wrapm(p.wt); d.rAddressMode = wrapm(p.wr); + d.lodMinClamp = p.lod_min < 0 ? 0 : p.lod_min; d.lodMaxClamp = p.lod_max; + d.maxAnisotropy = p.aniso < 1 ? 1 : p.aniso > 16 ? 16 : (NSUInteger)p.aniso; + if (p.cmp_mode) d.compareFunction = (MTLCompareFunction)(p.cmp_func - 1); + // Public on Metal 4 / iOS 26. Dynamic lookup keeps the same binary + // launchable on iOS 16, whose descriptor has no reduction-mode selector. + if (p.reduction == 1 || p.reduction == 2) { + SEL setReduction = NSSelectorFromString(@"setReductionMode:"); + if ([d respondsToSelector:setReduction]) { + [d setValue:@(p.reduction == 1 ? 1 : 2) forKey:@"reductionMode"]; + if (d.mipFilter == MTLSamplerMipFilterNotMipmapped) { d.lodMinClamp = 0; d.lodMaxClamp = 0; } + d.mipFilter = MTLSamplerMipFilterLinear; + } + } + d.borderColor = p.border[3] > 0.5f ? (p.border[0] > 0.5f ? MTLSamplerBorderColorOpaqueWhite : MTLSamplerBorderColorOpaqueBlack) : MTLSamplerBorderColorTransparentBlack; + d.supportArgumentBuffers = YES; + id s = [g_dev newSamplerStateWithDescriptor:d]; + if (s) { g_sview[sid] = (void *)CFBridgingRetain(s); g_srid[sid] = legacy_sampler_rid(s); } + return s; +} +static inline __attribute__((always_inline)) void *samp_fast(int sid) { + if (sid > 0 && sid < g_nsreg && g_sreg_ok[sid] && g_sview[sid]) return g_sview[sid]; + return (__bridge void *)samp_by_id(sid); +} + +// ---------- Argument buffer blocks: one block per submission, reclaimed after the GPU completes ---------- +@interface NvArena : NSObject { @public id buf; uint64_t gpu, fence; size_t used, len; } @end +@implementation NvArena @end +static NSMutableArray *g_arenas; static NvArena *g_cur_arena; static NSMutableArray *g_sub_arenas; +static NvArena *arena_new(void) { + // The for-in loop variable does not retain the object: take a strong reference first and then remove it from the array, otherwise what is returned after the array releases it is a dangling pointer + for (NvArena *it in g_arenas) if (it->fence && it->fence <= g_done) { NvArena *r = it; [g_arenas removeObject:r]; r->fence = 0; r->used = 0; return r; } + NvArena *a = [NvArena new]; a->buf = [g_dev newBufferWithLength:4 << 20 options:MTLResourceStorageModeShared]; a->len = a->buf.length; a->gpu = legacy_buffer_gpu(a->buf); res_add(a->buf); g_arena_made++; return a; +} +void arena_begin(void) { if (!g_arenas) { g_arenas = [NSMutableArray array]; g_sub_arenas = [NSMutableArray array]; } g_cur_arena = nil; } +void arena_commit(uint64_t fence) { for (NvArena *a in g_sub_arenas) { a->fence = fence; [g_arenas addObject:a]; } [g_sub_arenas removeAllObjects]; g_cur_arena = nil; } +// The out parameter is strong: under ARC an unqualified id * is autoreleasing, +// which otherwise retains the arena once per allocation until the thread exits. +static size_t arena_alloc(size_t n, __strong id *buf) { + n = (n + 255) & ~(size_t)255; + if (!g_cur_arena || g_cur_arena->used + n > g_cur_arena->len) { + pthread_mutex_lock(&g_mu); g_cur_arena = arena_new(); pthread_mutex_unlock(&g_mu); + [g_sub_arenas addObject:g_cur_arena]; + } + size_t off = g_cur_arena->used; g_cur_arena->used += n; *buf = g_cur_arena->buf; return off; +} + +// ---------- Draw state ---------- +static struct { + const prog_t *prog; + const prog_t *bst[5]; // program bound per stage (NVN stage bits 1 vertex 2 fragment 4 geometry 8 / 16 tessellation, indices as in NvProg.sh), see bind_stages + blend_st blend[8]; color_st color; chmask_st chmask; ms_st ms; poly_st poly; ds_st ds; + va_st attr[16]; int nattr; vs_st stream[16]; int nstream; + NVNbufferRange vbuf[16]; + p_bind_buf ubo[6][16], ssbo[6][16]; + uint64_t tex[6][NTEX], samp[6][128], img[6][32]; + uint32_t st_vmask[2], st_wmask[2], st_ref[2]; + p_poly_offset po; float vp[16][4]; float dr[2]; int has_vp, depth_clamp; + float blend_color[4]; int64_t copy_stride, copy_img_stride; + p_tf_bind tf[4]; uint64_t tf_off[4]; int tf_on, rast_discard; +} S; + +// Size of an attachment-less pass: the far corner of viewport 0, otherwise of +// the scissor, clamped to Metal's supported render-target extent. +static void bare_dims(NSUInteger *w, NSUInteger *h) { + double x1 = S.has_vp ? MAX(S.vp[0][0], S.vp[0][0] + S.vp[0][2]) : R.has_sc ? (double)R.sc.x + R.sc.w : 1; + double y1 = S.has_vp ? MAX(S.vp[0][1], S.vp[0][1] + S.vp[0][3]) : R.has_sc ? (double)R.sc.y + R.sc.h : 1; + *w = (NSUInteger)MIN(MAX(ceil(x1), 1.0), 16384.0); + *h = (NSUInteger)MIN(MAX(ceil(y1), 1.0), 16384.0); +} + +// Adapted from upstream 89b608d: cache resolution separately from encoder state. +// A pool replacement invalidates all entries even if guest addresses are reused. +static void bind_vbufs(id en) { + static struct { uint64_t addr, gen; NSUInteger offset; __unsafe_unretained id buffer; } cache[16]; + uint64_t gen = atomic_load_explicit(&g_pool_gen, memory_order_acquire); + for (int s = 0; s < 16; s++) { + uint64_t addr = S.vbuf[s].address; + if (!addr) continue; + if (g_vbuf_cache && cache[s].addr == addr && cache[s].gen == gen) g_vbuf_reuse++; + else { + void *mb; uint64_t off; g_vbuf_resolve++; + if (!mtl_addr(addr, 1, &mb, &off)) continue; + cache[s].addr = addr; cache[s].gen = gen; + cache[s].buffer = (__bridge id)mb; cache[s].offset = (NSUInteger)off; + } + legacy_bind_buffer(en, 0, VB_SLOT(s), cache[s].buffer, cache[s].offset); + use_rec("buf", addr, S.vbuf[s].size, 0, NULL); + } + static id zero; + if (!zero) { zero = [g_dev newBufferWithLength:64 options:MTLResourceStorageModeShared]; res_add(zero); } + legacy_bind_buffer(en, 0, 30, zero, 0); +} +static void udc_note_draw(NvProg *pr) { + if (!tbdr_on()) { // without the TBDR set only the duplicate-slot report below is live (the zero-source skip and the unwritten-attachment store are part of the set): skip the per-draw scans + uint32_t cm = pr && pr->sh[1] ? pr->sh[1]->rf.color_mask : 0; + if (R.has_rt && cm >> 1) for (int i = 1; i < R.rt.n && i < 8; i++) + if ((cm >> i & 1) && ((S.chmask.mask >> (4 * i)) & 0xf) && rt_dup_slot(&R.rt, i)) + RED_ONCE(79, "颜色槽 %d 与前面的槽挂同一张贴图(ser=%llu)且本次绘制写它(掩码 %#x):这些写入丢了", i, (unsigned long long)R.rt.color[i]->serial, (S.chmask.mask >> (4 * i)) & 0xf); + return; + } + if (R.has_rt) for (int i = 0; i < R.rt.n && i < 8; i++) if (R.rt.color[i]) { zskip_del(R.rt.color[i]->serial); g_zskip_cand_ok[i] = 0; } // Zero-source set: once really drawn it is no longer all zeros + if (pr) for (int st = 0; st < 6; st++) { __unsafe_unretained NvShader *sh = pr->sh[st]; if (!sh) continue; for (int i = 0; i < sh->rf.nb; i++) if (sh->rf.b[i].kind == NVN_RB_IMG) { zskip_clear(); break; } } // Image slots are writable: conservatively clear + int image_write = 0; + uint32_t shader_color_mask = 0; + if (pr) for (int st = 0; st < 6; st++) { __unsafe_unretained NvShader *sh = pr->sh[st]; if (!sh) continue; + if (st == 1) shader_color_mask = sh->rf.color_mask; + for (int i = 0; i < sh->rf.nb; i++) if (sh->rf.b[i].kind == NVN_RB_IMG && sh->rf.b[i].rw) { image_write = 1; break; } + if (image_write) break; + } + if (R.has_rt) for (int i = 1; i < R.rt.n && i < 8; i++) // a slot attached twice (left unattached) really written: the writes are lost (see rt_dup_slot) + if ((shader_color_mask >> i & 1) && ((S.chmask.mask >> (4 * i)) & 0xf) && rt_dup_slot(&R.rt, i)) + RED_ONCE(79, "颜色槽 %d 与前面的槽挂同一张贴图(ser=%llu)且本次绘制写它(掩码 %#x):这些写入丢了", i, (unsigned long long)R.rt.color[i]->serial, (S.chmask.mask >> (4 * i)) & 0xf); + for (int i = 0; i < g_nsd; i++) { + sd_att *t = &g_sd[i]; + if (image_write) { t->wrote = 1; t->wwhy = 1; } + else if (t->depth == 0 && t->idx >= 0 && (((S.chmask.mask >> (4 * t->idx)) & 0xf) || (shader_color_mask >> t->idx & 1))) { t->wrote = 1; t->wwhy = (S.chmask.mask >> (4 * t->idx)) & 0xf ? 2 : 3; } + else if (t->depth == 1 && S.ds.dw) { t->wrote = 1; t->wwhy = 4; } + else if (t->depth == 2 && S.ds.st && (S.st_wmask[0] || S.st_wmask[1]) + && (S.ds.fail_f != NVN_STENCIL_OP_KEEP || S.ds.zfail_f != NVN_STENCIL_OP_KEEP || S.ds.zpass_f != NVN_STENCIL_OP_KEEP + || S.ds.fail_b != NVN_STENCIL_OP_KEEP || S.ds.zfail_b != NVN_STENCIL_OP_KEEP || S.ds.zpass_b != NVN_STENCIL_OP_KEEP)) { t->wrote = 1; t->wwhy = 5; } + } +} +// NVN's BindProgram(program, stage bits) binds per stage: the stages in the bits take that program's shaders (a null program unbinds them), the other stages stay. +// Metroid Dread's depth prepass calls BindProgram(program, vertex | fragment), then BindProgram(null, fragment): only the vertex shader remains and depth is drawn. +// Every bind used to replace S.prog as a whole, a null program meant "no program bound", and the prepass's ~140 draws per frame were all dropped: depth stayed empty, the G-buffer's depth EQUAL test passed no pixel, and scenes and characters were black. +// When every stage comes from the same program, that program is the effective one (all of Mario's and Engage's binds are like this, so nothing changes for them); otherwise a composite is made: a host-made prog_t carrying only a serial, whose NvProg in the serial table takes each stage from its source program (a null fragment = a vertex-only pipeline) +static const prog_t *bind_stages(const prog_t *p, int stages) { + for (int st = 0; st < 5; st++) if (stages >> st & 1) S.bst[st] = p; + if (stages & 0x20) return p; // compute: replaced as a whole as before + const prog_t *v = S.bst[0]; + if (!v) return NULL; + int same = 1; for (int st = 1; st < 5; st++) if (S.bst[st] && S.bst[st] != v) same = 0; + if (same && S.bst[1] == v) return v; + enum { NCMP = 64 }; + static prog_t cp[NCMP]; static const prog_t *ck[NCMP][5]; static uint64_t cs[NCMP][5]; static int ncp, next; + for (int i = 0; i < ncp; i++) { + int hit = 1; for (int st = 0; st < 5 && hit; st++) hit = ck[i][st] == S.bst[st] && (!S.bst[st] || cs[i][st] == S.bst[st]->serial); + if (hit) return &cp[i]; + } + int i = ncp < NCMP ? ncp++ : next++ % NCMP; + if (cp[i].serial) serial_del(cp[i].serial); + NvProg *o = [NvProg new]; + for (int st = 0; st < 5; st++) { ck[i][st] = S.bst[st]; cs[i][st] = S.bst[st] ? S.bst[st]->serial : 0; if (S.bst[st]) { __unsafe_unretained NvProg *src = prog_get(S.bst[st]); o->sh[st] = src ? src->sh[st] : nil; } } + memset(&cp[i], 0, sizeof cp[i]); cp[i].magic = MAGIC('P', 'R', 'O', 'G'); cp[i].serial = nvn_new_serial(); serial_put(cp[i].serial, o); + { static int once; if (!once++) MLOG("按阶段绑程序:首个合成程序(顶点 %s 片元 %s)", o->sh[0] ? o->sh[0]->key : "-", o->sh[1] ? o->sh[1]->key : "-"); } + return &cp[i]; +} +static void state_reset(void) { + memset(&S, 0, sizeof S); + g_pso_dirty = g_dss_dirty = 1; + for (int i = 0; i < 8; i++) S.blend[i] = (blend_st){i, NVN_BLEND_FUNC_ONE, NVN_BLEND_FUNC_ZERO, NVN_BLEND_FUNC_ONE, NVN_BLEND_FUNC_ZERO, NVN_BLEND_EQUATION_ADD, NVN_BLEND_EQUATION_ADD}; + S.color = (color_st){0, NVN_LOGIC_OP_COPY, NVN_ALPHA_FUNC_ALWAYS}; S.chmask.mask = 0xffffffffu; + S.poly = (poly_st){NVN_FACE_NONE, NVN_FRONT_FACE_CCW, NVN_POLYGON_MODE_FILL, 0}; + S.ds = (ds_st){0, 1, NVN_DEPTH_FUNC_LESS, 0, NVN_STENCIL_FUNC_ALWAYS, NVN_STENCIL_FUNC_ALWAYS, 1, 1, 1, 1, 1, 1}; + S.st_vmask[0] = S.st_vmask[1] = S.st_wmask[0] = S.st_wmask[1] = 0xff; S.dr[1] = 1; +} + +// Correct the vertex format by the shader input's component type: Metal requires integer inputs to get integer formats of the same signedness, and float inputs float / normalized formats. +// Integer input (inferred at translation time as "only used as an integer", dmsh_build.py int_inputs) with an integer format: on a signedness mismatch switch to the same width with the other sign (bit pattern unchanged); with a 32-bit float format read it as an integer of the same width (bit pattern unchanged). +// Other mismatches (integer input with a normalized / half format, float input with an integer format) cannot keep the bit pattern: report once and keep the format. +static MTLVertexFormat vfmt_for_ct(MTLVertexFormat f, int ct, int nvn_fmt) { + static const MTLVertexFormat U[] = { MTLVertexFormatUChar, MTLVertexFormatUChar2, MTLVertexFormatUChar3, MTLVertexFormatUChar4, MTLVertexFormatUShort, MTLVertexFormatUShort2, MTLVertexFormatUShort3, MTLVertexFormatUShort4, MTLVertexFormatUInt, MTLVertexFormatUInt2, MTLVertexFormatUInt3, MTLVertexFormatUInt4 }; + static const MTLVertexFormat I[] = { MTLVertexFormatChar, MTLVertexFormatChar2, MTLVertexFormatChar3, MTLVertexFormatChar4, MTLVertexFormatShort, MTLVertexFormatShort2, MTLVertexFormatShort3, MTLVertexFormatShort4, MTLVertexFormatInt, MTLVertexFormatInt2, MTLVertexFormatInt3, MTLVertexFormatInt4 }; + static const MTLVertexFormat F[] = { MTLVertexFormatFloat, MTLVertexFormatFloat2, MTLVertexFormatFloat3, MTLVertexFormatFloat4 }; + int iu = -1, ii = -1, iff = -1; + for (int i = 0; i < 12; i++) { if (U[i] == f) iu = i; if (I[i] == f) ii = i; } + for (int i = 0; i < 4; i++) if (F[i] == f) iff = i; + if (ct == NVN_CT_UINT) { if (iu >= 0) return f; if (ii >= 0) return U[ii]; if (iff >= 0) return U[8 + iff]; } + else if (ct == NVN_CT_INT) { if (ii >= 0) return f; if (iu >= 0) return I[iu]; if (iff >= 0) return I[8 + iff]; } + else { if (iu < 0 && ii < 0) return f; } + RED_ONCE(66, "顶点属性格式 %#x 与着色器输入类型 %s 对不上,位模式保不住(照原格式)", nvn_fmt, ct == NVN_CT_UINT ? "uint" : ct == NVN_CT_INT ? "int" : "float"); + return f; +} +static MTLVertexFormat vfmt(int f) { + switch (f) { + case 0x01: return MTLVertexFormatUCharNormalized; case 0x02: return MTLVertexFormatCharNormalized; case 0x03: return MTLVertexFormatUChar; case 0x04: return MTLVertexFormatChar; + case 0x05: return MTLVertexFormatHalf; case 0x06: return MTLVertexFormatUShortNormalized; case 0x07: return MTLVertexFormatShortNormalized; + case 0x08: return MTLVertexFormatUShort; case 0x09: return MTLVertexFormatShort; case 0x0A: return MTLVertexFormatFloat; case 0x0B: return MTLVertexFormatUInt; case 0x0C: return MTLVertexFormatInt; + case 0x0D: return MTLVertexFormatUChar2Normalized; case 0x0E: return MTLVertexFormatChar2Normalized; case 0x0F: return MTLVertexFormatUChar2; case 0x10: return MTLVertexFormatChar2; + case 0x11: return MTLVertexFormatHalf2; case 0x12: return MTLVertexFormatUShort2Normalized; case 0x13: return MTLVertexFormatShort2Normalized; case 0x14: return MTLVertexFormatUShort2; + case 0x15: return MTLVertexFormatShort2; case 0x16: return MTLVertexFormatFloat2; case 0x17: return MTLVertexFormatUInt2; case 0x18: return MTLVertexFormatInt2; + case 0x19: return MTLVertexFormatUChar3Normalized; case 0x1A: return MTLVertexFormatChar3Normalized; case 0x1B: return MTLVertexFormatUChar3; case 0x1C: return MTLVertexFormatChar3; + case 0x1D: return MTLVertexFormatHalf3; case 0x1E: return MTLVertexFormatUShort3Normalized; case 0x1F: return MTLVertexFormatShort3Normalized; case 0x20: return MTLVertexFormatUShort3; + case 0x21: return MTLVertexFormatShort3; case 0x22: return MTLVertexFormatFloat3; case 0x23: return MTLVertexFormatUInt3; case 0x24: return MTLVertexFormatInt3; + case 0x25: return MTLVertexFormatUChar4Normalized; case 0x26: return MTLVertexFormatChar4Normalized; case 0x27: return MTLVertexFormatUChar4; case 0x28: return MTLVertexFormatChar4; + case 0x29: return MTLVertexFormatHalf4; case 0x2A: return MTLVertexFormatUShort4Normalized; case 0x2B: return MTLVertexFormatShort4Normalized; case 0x2C: return MTLVertexFormatUShort4; + case 0x2D: return MTLVertexFormatShort4; case 0x2E: return MTLVertexFormatFloat4; case 0x2F: return MTLVertexFormatUInt4; case 0x30: return MTLVertexFormatInt4; + case 0x3D: return MTLVertexFormatUInt1010102Normalized; case 0x69: return MTLVertexFormatInt1010102Normalized; case 0x3F: return MTLVertexFormatInvalid; case 0x9A: return MTLVertexFormatUChar4Normalized_BGRA; + } + return MTLVertexFormatInvalid; +} +static MTLBlendFactor bfac(int f) { + switch (f) { case 1: return MTLBlendFactorZero; case 2: return MTLBlendFactorOne; case 3: return MTLBlendFactorSourceColor; case 4: return MTLBlendFactorOneMinusSourceColor; + case 5: return MTLBlendFactorSourceAlpha; case 6: return MTLBlendFactorOneMinusSourceAlpha; case 7: return MTLBlendFactorDestinationAlpha; case 8: return MTLBlendFactorOneMinusDestinationAlpha; + case 9: return MTLBlendFactorDestinationColor; case 10: return MTLBlendFactorOneMinusDestinationColor; case 11: return MTLBlendFactorSourceAlphaSaturated; + case 0x10: return MTLBlendFactorSource1Color; case 0x11: return MTLBlendFactorOneMinusSource1Color; case 0x12: return MTLBlendFactorSource1Alpha; case 0x13: return MTLBlendFactorOneMinusSource1Alpha; + case 0x61: return MTLBlendFactorBlendColor; case 0x62: return MTLBlendFactorOneMinusBlendColor; case 0x63: return MTLBlendFactorBlendAlpha; case 0x64: return MTLBlendFactorOneMinusBlendAlpha; } + return MTLBlendFactorOne; +} +static MTLBlendOperation bop(int e) { switch (e) { case 2: return MTLBlendOperationSubtract; case 3: return MTLBlendOperationReverseSubtract; case 4: return MTLBlendOperationMin; case 5: return MTLBlendOperationMax; } return MTLBlendOperationAdd; } + +// Pipeline: cached by (program, target formats, blend / write mask, vertex layout, sample count) +static const uint8_t *g_pso_lmap; static id g_pso_fsfn; static pso_fz_desc g_pso_fz, g_fz_desc; +static os_unfair_lock g_fz_fn_lock = OS_UNFAIR_LOCK_INIT; +static NSMutableDictionary> *g_bf_fn_cache; +static NSMutableDictionary> *g_bf_lib_cache; +static nvn_hmap g_pso = {.klen = sizeof(pso_key)}, g_dss; +static nvn_hmap g_pso_fail = {.klen = sizeof(pso_key)}; // pipelines that failed to build: remembered and never retried (previously every draw recompiled it, over 10,000 times in one iPhone session) +static const char g_tf_mark; +#define TF_MARK ((const void *)&g_tf_mark) +static uint8_t g_pso_topology = MTLPrimitiveTopologyClassTriangle; +static uint8_t pso_topology_class(int prim) { + if (prim == 0) return MTLPrimitiveTopologyClassPoint; + if (prim == 1 || prim == 3) return MTLPrimitiveTopologyClassLine; + return MTLPrimitiveTopologyClassTriangle; +} +static int hilite_index(NvShader *fs) { + dispatch_once(&g_hilite_once, ^{ + const char *env = getenv("DM_NVN_HILITE"); + for (const char *q = env; q && *q && g_hilite_n < 6; q = strchr(q, ',') ? strchr(q, ',') + 1 : q + strlen(q)) { + size_t n = MIN(strcspn(q, ","), sizeof g_hilite_keys[0] - 1); + memcpy(g_hilite_keys[g_hilite_n], q, n); g_hilite_keys[g_hilite_n++][n] = 0; + } + const char *full = getenv("DM_NVN_HILITE_FULL"); g_hilite_full = full && !strcmp(full, "1"); + if (!g_hilite_n) return; + NSError *error = nil; + id lib = [g_dev newLibraryWithSource: + @"#include \nusing namespace metal;\n" + "struct O { float4 c0 [[color(0)]]; float4 c1 [[color(1)]]; float4 c2 [[color(2)]]; float4 c3 [[color(3)]]; float4 c4 [[color(4)]]; };\n" + "struct V { float4 p [[position]]; };\n" + "O ho(float4 c) { O o; o.c0=c; o.c1=c; o.c2=c; o.c3=c; o.c4=c; return o; }\n" + "fragment O hl0(){return ho(float4(1,0,1,1));} fragment O hl1(){return ho(float4(0,1,0,1));}\n" + "fragment O hl2(){return ho(float4(0,1,1,1));} fragment O hl3(){return ho(float4(1,1,0,1));}\n" + "fragment O hl4(){return ho(float4(0,0,1,1));} fragment O hl5(){return ho(float4(1,1,1,1));}\n" + "vertex V hv(uint id [[vertex_id]]) { float2 p[3]={float2(-1,-1),float2(3,-1),float2(-1,3)}; V o; o.p=float4(p[id%3],0.5,1); return o; }\n" + options:nil error:&error]; + for (int i = 0; lib && i < 6; i++) g_hilite_fs[i] = [lib newFunctionWithName:[NSString stringWithFormat:@"hl%d", i]]; + g_hilite_vs = [lib newFunctionWithName:@"hv"]; + if (!lib || !g_hilite_fs[0] || !g_hilite_vs) MLOG("[RED] DM_NVN_HILITE shader compilation failed: %s", error.localizedDescription.UTF8String); + else MLOG("DM_NVN_HILITE: %d fragment keys%s", g_hilite_n, g_hilite_full ? ", synthetic full-screen vertex output" : ""); + }); + if (!fs) return -1; + for (int i = 0; i < g_hilite_n; i++) if (!strncmp(fs->key, g_hilite_keys[i], strlen(g_hilite_keys[i]))) return i; + return -1; +} +static MTLRenderPipelineDescriptor *pso_desc(const pso_key *k, NvShader *vs, NvShader *fs, id fsfn, int merged) { + MTLRenderPipelineDescriptor *d = [MTLRenderPipelineDescriptor new]; + d.label = [NSString stringWithFormat:@"vs=%s fs=%s topology=%u", vs->key, fs ? fs->key : "-", k->topology]; + d.vertexFunction = vs->fn; d.fragmentFunction = fsfn ? fsfn : fs ? fs->fn : nil; + if (k->fsfn == TF_MARK) { d.vertexFunction = vs->tf_fn; d.fragmentFunction = nil; d.rasterizationEnabled = NO; } + // Diagnostic DM_NVN_HILITE=fragment shader key,...: replace matching + // fragments with solid colors on outputs 0-4. DM_NVN_HILITE_FULL=1 also + // replaces the vertex function with a full-screen triangle generator. + int hi = hilite_index(fs); + if (hi >= 0 && g_hilite_fs[hi]) { + d.fragmentFunction = g_hilite_fs[hi]; + if (g_hilite_full && g_hilite_vs) d.vertexFunction = g_hilite_vs; + } + d.rasterSampleCount = k->samples ? k->samples : 1; + d.inputPrimitiveTopology = (MTLPrimitiveTopologyClass)k->topology; + d.alphaToCoverageEnabled = k->a2c; + for (int i = 0; i < 8; i++) { + if (!k->cfmt[i]) continue; + MTLRenderPipelineColorAttachmentDescriptor *c = d.colorAttachments[i]; + c.pixelFormat = (MTLPixelFormat)k->cfmt[i]; + int l = merged ? k->lmap[i] : i; + uint32_t m = l == 0xff ? 0 : (k->chmask >> (4 * l)) & 0xf; + // Output numbers the fragment shader does not write: zero write mask, no blending. Maxwell writes only the targets in the shader's output map and leaves the others untouched (whatever the blend state and channel mask); + // on Metal an attachment the shader does not write has undefined contents, and with blending enabled that undefined value is blended in. Super Mario 3D World + Bowser's Fury's volume draws after the G-buffer (50d813761ca3f1f9 and others) write only outputs 0 and 2, + // while targets 1 (normals) and 3 (pre-lighting) have SRC_ALPHA blending and all channels enabled + int unwritten = fs && fs->rf.color_known && !fsfn && l != 0xff && !(fs->rf.color_mask >> l & 1); + if (unwritten) m = 0; + c.writeMask = (m & 1 ? MTLColorWriteMaskRed : 0) | (m & 2 ? MTLColorWriteMaskGreen : 0) | (m & 4 ? MTLColorWriteMaskBlue : 0) | (m & 8 ? MTLColorWriteMaskAlpha : 0); + if (l != 0xff && k->blend_en >> l & 1 && !unwritten) { + c.blendingEnabled = YES; + c.sourceRGBBlendFactor = bfac(k->blend[l].src_rgb); c.destinationRGBBlendFactor = bfac(k->blend[l].dst_rgb); + c.sourceAlphaBlendFactor = bfac(k->blend[l].src_a); c.destinationAlphaBlendFactor = bfac(k->blend[l].dst_a); + c.rgbBlendOperation = bop(k->blend[l].eq_rgb); c.alphaBlendOperation = bop(k->blend[l].eq_a); + } + } + d.depthAttachmentPixelFormat = (MTLPixelFormat)k->dfmt; d.stencilAttachmentPixelFormat = (MTLPixelFormat)k->sfmt; + MTLVertexDescriptor *vd = [MTLVertexDescriptor vertexDescriptor]; + const nvn_refl *rf = &vs->rf; + for (int v = 0; v < rf->nv; v++) { + int loc = rf->v[v].loc, a = rf->v[v].nvn; // Metal slot loc ← NVN attribute a + MTLVertexFormat vf = a < 16 ? vfmt(k->attr[a].fmt) : MTLVertexFormatInvalid; + // When a >= S.nattr, k.attr[a] keeps the 0 from memset, vfmt(0) is Invalid, the same fallback branch is taken and the RED_ONCE condition of fmt being non-0 does not hold. + if (a < 16 && k->attr[a].fmt && vf == MTLVertexFormatInvalid) RED_ONCE(15, "顶点属性格式 %#x 未映射", k->attr[a].fmt); + if (vf == MTLVertexFormatInvalid) { + int ct = rf->v[v].ct; + vd.attributes[loc].format = ct == NVN_CT_UINT ? MTLVertexFormatUInt4 : ct == NVN_CT_INT ? MTLVertexFormatInt4 : MTLVertexFormatFloat4; + vd.attributes[loc].offset = 0; vd.attributes[loc].bufferIndex = 30; + vd.layouts[30].stride = 16; vd.layouts[30].stepFunction = MTLVertexStepFunctionConstant; vd.layouts[30].stepRate = 0; + continue; + } + int st = k->attr[a].stream; + vf = vfmt_for_ct(vf, rf->v[v].ct, k->attr[a].fmt); + vd.attributes[loc].format = vf; vd.attributes[loc].offset = k->attr[a].offset; vd.attributes[loc].bufferIndex = VB_SLOT(st); + vd.layouts[VB_SLOT(st)].stride = k->stream[st].stride ? k->stream[st].stride : 4; + vd.layouts[VB_SLOT(st)].stepFunction = k->stream[st].stride == 0 ? MTLVertexStepFunctionConstant : k->stream[st].divisor ? MTLVertexStepFunctionPerInstance : MTLVertexStepFunctionPerVertex; + vd.layouts[VB_SLOT(st)].stepRate = k->stream[st].stride == 0 ? 0 : k->stream[st].divisor ? k->stream[st].divisor : 1; + } + d.vertexDescriptor = vd; + return d; +} +static NSString *nonpoint_digest(NSData *data) { + if (!data) return nil; + unsigned char hash[CC_SHA256_DIGEST_LENGTH]; + CC_SHA256(data.bytes, (CC_LONG)data.length, hash); + NSMutableString *s = [NSMutableString stringWithCapacity:64]; + for (unsigned i = 0; i < sizeof hash; i++) [s appendFormat:@"%02x", hash[i]]; + return s; +} +#include "nvn_min_reduction.h" +#include "nvn_guarded_index.h" +// NVN ignores point size for lines/triangles. Older Metal compilers require it +// to be absent from those vertex outputs. Keep the original point/TF functions; +// demote only the non-point variant's raster output to an unused user varying. +// Supplemental sources are exact, hash-checked copies of this chain's inputs, +// exported by dm/ios/tools/export_nonpoint_sources.py (never game assets in git). +static NSDictionary *nonpoint_sources_manifest(void) { + static NSDictionary *manifest; static dispatch_once_t once; + dispatch_once(&once, ^{ + NSString *root = [g_msl_dir stringByAppendingPathComponent:@"nonpoint-v1"]; + NSData *data = [NSData dataWithContentsOfFile:[root stringByAppendingPathComponent:@"manifest.json"]]; + manifest = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil; + }); + return manifest; +} +static id nonpoint_vertex(NvShader *vs, NSError **error) { + @synchronized (vs) { + if (!vs->nonpoint_tried) { + vs->nonpoint_tried = 1; + // Pipeline prediction can enqueue hundreds of different shaders. + // Bound source/library/compiler memory while those jobs are pending. + static dispatch_semaphore_t compile_slots; static dispatch_once_t slots_once; + dispatch_once(&slots_once, ^{ compile_slots = dispatch_semaphore_create(2); }); + dispatch_semaphore_wait(compile_slots, DISPATCH_TIME_FOREVER); + NSString *root = [g_msl_dir stringByAppendingPathComponent:@"nonpoint-v1"]; + NSDictionary *manifest = nonpoint_sources_manifest(); + NSString *key = @(vs->key); + NSDictionary *record = manifest[@"shaders"][key]; + NSData *source = [NSData dataWithContentsOfFile:[root stringByAppendingPathComponent:[key stringByAppendingString:@".metal"]]]; + NSData *library = [NSData dataWithContentsOfFile:[g_msl_dir stringByAppendingPathComponent:[key stringByAppendingString:@".metallib"]] options:NSDataReadingMappedIfSafe error:nil]; + NSError *e = nil; + BOOL valid = [manifest[@"schema"] isEqual:@1] && [manifest[@"chain"] isEqual:@DMSH_CHAIN] + && [record[@"source_sha256"] isEqual:nonpoint_digest(source)] + && [record[@"library_sha256"] isEqual:nonpoint_digest(library)]; + if (valid) source = guarded_index_source(key, source, library, &e); + NSString *src = valid ? [[NSString alloc] initWithData:source encoding:NSUTF8StringEncoding] : nil; + if (!src || ![src containsString:@"[[point_size]]"]) { + if (!e) e = [NSError errorWithDomain:@"NVNNonPoint" code:1 userInfo:@{NSLocalizedDescriptionKey: + [NSString stringWithFormat:@"Missing or mismatched non-point source for %@ (chain %s)", key, DMSH_CHAIN]}]; + } else { + src = [src stringByReplacingOccurrencesOfString:@"[[point_size]]" withString:@"[[user(dm_unused_point_size)]]"]; + MTLCompileOptions *options = [MTLCompileOptions new]; + options.languageVersion = MTLLanguageVersion3_0; + options.preserveInvariance = YES; + options.fastMathEnabled = NO; // iOS 16 has no relaxed mathMode; preserve NaN/Inf semantics. + CFAbsoluteTime t0 = CFAbsoluteTimeGetCurrent(); + vs->nonpoint_lib = [g_dev newLibraryWithSource:src options:options error:&e]; + if (vs->nonpoint_lib) { + if (vs->specialized) { + vs->nonpoint_fn = [vs->nonpoint_lib newFunctionWithName:vs->fn.name constantValues:[MTLFunctionConstantValues new] error:&e]; + } else vs->nonpoint_fn = [vs->nonpoint_lib newFunctionWithName:vs->fn.name]; + if (!vs->nonpoint_fn && !e) e = [NSError errorWithDomain:@"NVNNonPoint" code:2 userInfo:@{NSLocalizedDescriptionKey:@"Non-point entry point missing"}]; + if (vs->nonpoint_fn && vs->rf.nb && [vs->nonpoint_fn newArgumentEncoderWithBufferIndex:vs->rf.abi].encodedLength != vs->arg_len) { + vs->nonpoint_fn = nil; + e = [NSError errorWithDomain:@"NVNNonPoint" code:3 userInfo:@{NSLocalizedDescriptionKey:@"Non-point argument layout changed"}]; + } + } + if (vs->nonpoint_fn) MLOG("Non-point vertex variant %s entry=%s compiled in %.1fms; original point/TF entry retained", vs->key, vs->fn.name.UTF8String, (CFAbsoluteTimeGetCurrent() - t0) * 1000); + } + if (!vs->nonpoint_fn) { + vs->nonpoint_error = e; + MLOG("[RED] Non-point vertex variant %s failed: %s", vs->key, e.localizedDescription.UTF8String); + } + dispatch_semaphore_signal(compile_slots); + } + if (error) *error = vs->nonpoint_error; + return vs->nonpoint_fn; + } +} +static id pso_build(const pso_key *k, NvShader *vs, NvShader *fs, id fsfn, int merged, int specialized, NSError **error) { + MTLRenderPipelineDescriptor *d = pso_desc(k, vs, fs, fsfn, merged); + if (!g_bare_native && d.rasterizationEnabled) { + BOOL attached = d.depthAttachmentPixelFormat || d.stencilAttachmentPixelFormat; + for (int i = 0; i < 8; i++) attached |= d.colorAttachments[i].pixelFormat != MTLPixelFormatInvalid; + // Parallel prediction may speculate a format-less descriptor. The + // actual pass supplies the transient target and a different PSO key. + // Reject the speculation before legacy Metal's fatal validation. + if (!attached) { + if (error) *error = [NSError errorWithDomain:@"NVNMetalCompatibility" code:1 userInfo:@{NSLocalizedDescriptionKey:@"Rasterization without attachments requires Apple4 or Mac2; actual pass uses a transient attachment"}]; + return nil; + } + } + BOOL nonpoint = k->topology != MTLPrimitiveTopologyClassPoint && k->fsfn != TF_MARK && d.vertexFunction == vs->fn; + // The manifest names exactly the original vertex sources containing a point + // size output. Make those legal for line/triangle topology before asking + // Metal to validate the descriptor: a different validation error can mask + // the point-size conflict, and parallel prediction can finish the variant + // only after that first failure has already been cached permanently. + if (nonpoint && nonpoint_sources_manifest()[@"shaders"][@(vs->key)]) { + NSError *variant_error = nil; + id fn = nonpoint_vertex(vs, &variant_error); + if (!fn) { if (error) *error = variant_error; return nil; } + d.vertexFunction = fn; + } else if (nonpoint) { @synchronized (vs) { if (vs->nonpoint_fn) d.vertexFunction = vs->nonpoint_fn; } } + id ps = nvn_cached_render_specialized(g_dev, d, specialized, error); + NSString *why = error && *error ? (*error).localizedDescription : nil; + if (!ps && nonpoint && d.vertexFunction == vs->fn && [why containsString:@"writes point size"]) { + NSError *retry = nil; + id fn = nonpoint_vertex(vs, &retry); + if (fn) { + d.vertexFunction = fn; // Never change topology: Metal validates it against each actual draw. + ps = nvn_cached_render_specialized(g_dev, d, specialized, &retry); + } + if (error) *error = ps ? nil : retry ?: *error; + } + if (!ps) MLOG("[RED] Pipeline descriptor rejected vs=%s fs=%s actual_formats=%lu/%lu/%lu topology=%lu rasterization=%d vertex=%s fragment=%s nonpoint=%d error=%s", + vs->key, fs ? fs->key : "-", (unsigned long)d.colorAttachments[0].pixelFormat, + (unsigned long)d.depthAttachmentPixelFormat, (unsigned long)d.stencilAttachmentPixelFormat, + (unsigned long)d.inputPrimitiveTopology, d.rasterizationEnabled, d.vertexFunction.name.UTF8String, + d.fragmentFunction.name.UTF8String, d.vertexFunction == vs->nonpoint_fn, error && *error ? (*error).localizedDescription.UTF8String : "-"); + return ps; +} +// Pipeline key: current state S + pass formats (normal pass; a merged pass additionally carries lmap / specialized function, filled in by pso_get) +static void pso_key_fill(pso_key *k, NvShader *vs, NvShader *fs, const uint32_t cfmt[8], uint32_t dfmt, uint32_t sfmt, uint32_t samples, int lmap) { + memset(k, 0, sizeof *k); + k->vs = (__bridge void *)vs; k->fs = (__bridge void *)fs; + k->topology = g_pso_topology; + memcpy(k->cfmt, cfmt, sizeof k->cfmt); k->dfmt = dfmt; k->sfmt = sfmt; k->samples = samples; + k->blend_en = S.color.blend_en; k->chmask = S.chmask.mask; k->a2c = S.ms.a2c; + for (int i = 0; i < 8; i++) if (k->cfmt[i] || lmap) k->blend[i] = S.blend[i]; + const nvn_refl *rf = &vs->rf; + for (int v = 0; v < rf->nv; v++) { int a = rf->v[v].nvn; if (a < 16 && a < S.nattr) { k->attr[a] = S.attr[a]; int st = S.attr[a].stream; if (st < 16) k->stream[st] = S.stream[st]; } } +} +// ---- Parallel precompile: pipelines met for the first time used to be compiled synchronously one by one on the submit thread; 9 misses in a row in one frame = 284 ms (heavy scene on a real device). +// Now: on the first pipeline miss in a submission, scan ahead once along the remaining commands (replaying state only, no drawing) and kick all the normal-pass pipelines that will also miss later to the background to compile in parallel; +// when the submit thread reaches that draw it only waits for 'the remaining time not yet compiled'. The compiled pipelines are identical to synchronously compiled ones (same descriptor). Pipeline keys of merged passes / specialized fragments depend on the planning result at replay time, are not predicted, and are compiled synchronously as before. +#define PSO_PEND_MAX 384 +static struct { pso_key k; id ps; int used, done; } g_pp[PSO_PEND_MAX]; +static pthread_mutex_t g_pp_mu = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t g_pp_cv = PTHREAD_COND_INITIALIZER; +static uint64_t g_pp_submit_mark, g_pp_kicked, g_pp_taken; // Whether this submission has already been predicted (by submission serial); cumulative kicked / taken counts +static void pso_predict_rest(void); +static int pso_parallel_on(void) { + static int on = -1; + if (on < 0) { + const char *parallel = getenv("DM_NVN_PSO_PARALLEL"), *predict = getenv("DM_NVN_PREDICT"); + on = !((parallel && !strcmp(parallel, "0")) || (predict && !strcmp(predict, "0"))); + if (!on) MLOG("%s=0: predictive pipeline scan disabled; first-use pipelines compile synchronously", + parallel && !strcmp(parallel, "0") ? "DM_NVN_PSO_PARALLEL" : "DM_NVN_PREDICT"); + } + return on; +} +static int pso_pend_find(const pso_key *k) { for (int i = 0; i < PSO_PEND_MAX; i++) if (g_pp[i].used && !memcmp(&g_pp[i].k, k, sizeof *k)) return i; return -1; } +static void pso_pend_kick(const pso_key *k, NvShader *vs, NvShader *fs) { // Called by the submit thread: kick only if it is neither in the cache nor in the table + if (hm_get_id(&g_pso, k)) return; + pthread_mutex_lock(&g_pp_mu); + int at = pso_pend_find(k), slot = -1; + if (at < 0) for (int i = 0; i < PSO_PEND_MAX; i++) if (!g_pp[i].used) { slot = i; break; } + if (at < 0 && slot >= 0) { g_pp[slot].k = *k; g_pp[slot].ps = nil; g_pp[slot].done = 0; g_pp[slot].used = 1; } + pthread_mutex_unlock(&g_pp_mu); + if (at >= 0 || slot < 0) return; + pso_key pk = *k; g_pp_kicked++; + dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ + NSError *e = nil; id ps = pso_build(&pk, vs, fs, nil, 0, vs->specialized || (fs && fs->specialized), &e); + pthread_mutex_lock(&g_pp_mu); g_pp[slot].ps = ps; g_pp[slot].done = 1; pthread_cond_broadcast(&g_pp_cv); pthread_mutex_unlock(&g_pp_mu); + }); +} +static id pso_pend_take(const pso_key *k) { // Called by the submit thread: if it is in the table, wait for it to finish compiling and take it; otherwise return nil + pthread_mutex_lock(&g_pp_mu); + int at = pso_pend_find(k); id ps = nil; + if (at >= 0) { while (!g_pp[at].done) pthread_cond_wait(&g_pp_cv, &g_pp_mu); ps = g_pp[at].ps; g_pp[at].ps = nil; g_pp[at].used = 0; } + pthread_mutex_unlock(&g_pp_mu); + return ps; +} +static void pso_pend_harvest(void) { + pthread_mutex_lock(&g_pp_mu); + for (int i = 0; i < PSO_PEND_MAX; i++) { + if (g_pp[i].used && g_pp[i].done) { + pso_key k = g_pp[i].k; + id ps = g_pp[i].ps; + g_pp[i].ps = nil; g_pp[i].used = 0; g_pp[i].done = 0; + if (ps) { + pthread_mutex_unlock(&g_pp_mu); + if (!hm_get_id(&g_pso, &k)) { + pso_skey sk; char shex[sizeof sk * 2 + 1]; + pso_skey_from(&k, (__bridge NvShader *)k.vs, (__bridge NvShader *)k.fs, 0, NULL, &sk); + pso_skey_hex(&sk, shex); + pso_record("R\t%s\n", shex); + pso_user_record("R\t%s\n", shex); + hm_put_id(&g_pso, &k, ps); + } + pthread_mutex_lock(&g_pp_mu); + } + } + } + pthread_mutex_unlock(&g_pp_mu); +} +static __unsafe_unretained id pso_get_(NvProg *pr, MTLRenderPassDescriptor *unused); +static id ms_fs_override(NvShader *fs); // DM_NVN_MAGSCAN (defined before draw) +static __unsafe_unretained id pso_get(NvProg *pr, MTLRenderPassDescriptor *unused) { AP_B; __unsafe_unretained id r = pso_get_(pr, unused); AP_E(2); return r; } +static __unsafe_unretained id pso_get_(NvProg *pr, MTLRenderPassDescriptor *unused) { + (void)unused; + __unsafe_unretained NvShader *vs = pr->sh[0], *fs = pr->sh[1]; + if (!vs) { RED_ONCE(13, "程序没有顶点着色器(或未映射)"); return nil; } + pso_key k; pso_key_fill(&k, vs, fs, g_pass.cfmt, g_pass.dfmt, g_pass.sfmt, g_pass.samples, g_pso_lmap != NULL); + if (g_pso_lmap) { memcpy(k.lmap, g_pso_lmap, 8); k.fsfn = (__bridge void *)g_pso_fsfn; } + id ms_fn = ms_fs_override(fs); if (ms_fn) k.fsfn = (__bridge void *)ms_fn; // DM_NVN_MAGSCAN phases 2 / 3 + // This variant depends on the actual sampler binding. Keep it distinct + // from the original pipeline and from readable-tile/diagnostic overrides. + id min_fn = !k.fsfn && !g_pso_lmap ? min_reduction_function(fs) : nil; + if (min_fn) k.fsfn = (__bridge void *)min_fn; + __unsafe_unretained id cached = hm_get_id(&g_pso, &k); + if (cached) return cached; + pso_pend_harvest(); + cached = hm_get_id(&g_pso, &k); + if (cached) return cached; + if (hm_get_id(&g_pso_fail, &k)) { + if (min_fn) { fs->min_reduction_pso_failed = 1; return pso_get_(pr, unused); } + return nil; + } + id ps; + NSError *e = nil; uint64_t bt = bp_now(); + ps = g_pso_lmap || ms_fn || min_fn ? nil : pso_pend_take(&k); + if (ps) g_pp_taken++; + else { + if (pso_parallel_on() && g_pp_submit_mark != g_sub_serial) { g_pp_submit_mark = g_sub_serial; pso_predict_rest(); } // First miss in this submission: first kick the later ones out to compile in parallel, then synchronously compile the one at hand + ps = pso_build(&k, vs, fs, ms_fn ? ms_fn : min_fn ? min_fn : g_pso_fsfn, g_pso_lmap != NULL, + k.fsfn != NULL || vs->specialized || (fs && fs->specialized), &e); + } + g_bp_pso_n++; g_bp_pso_ns += bp_now() - bt; + if (!ps) { MLOG("[RED] Render pipeline failed vs=%s fs=%s pass=%llu formats=%u/%u/%u depth=%d/%d bound-depth=%llu dt=%d dw=%d topology=%u: %s", vs->key, fs ? fs->key : "-", (unsigned long long)g_pass.serial, k.cfmt[0], k.dfmt, k.sfmt, g_pass.hd, g_pass.hs, (unsigned long long)(R.rt.depth ? R.rt.depth->serial : 0), S.ds.dt, S.ds.dw, k.topology, e.localizedDescription.UTF8String); hm_put_id(&g_pso_fail, &k, NSNull.null); + if (min_fn) { fs->min_reduction_pso_failed = 1; + MLOG("Min-reduction %s pipeline rejected; disabling its companion and retrying the ordinary pipeline", fs->key); + return pso_get_(pr, unused); } + return nil; } + if (!ms_fn && !min_fn) { pso_skey sk; char shex[sizeof sk * 2 + 1]; pso_skey_from(&k, vs, fs, g_pso_lmap != NULL, g_pso_lmap ? &g_pso_fz : NULL, &sk); pso_skey_hex(&sk, shex); + pso_record("R\t%s\n", shex); + pso_user_record("R\t%s\n", shex); } + hm_put_id(&g_pso, &k, ps); + return ps; +} +static NSString *warm_key(const char *s, size_t n) { + size_t z = strnlen(s, n); + return [[NSString alloc] initWithBytes:s length:z encoding:NSUTF8StringEncoding]; +} +static NvShader *warm_shader(NSMutableDictionary *dict, NSString *key, int stage) { + NvShader *sh = dict[key]; + if (!sh) { sh = shader_load(key, stage); if (sh) dict[key] = sh; } + return sh; +} +static void warm_enqueue(WarmItem *item) { + os_unfair_lock_lock(&g_warm_lock); + [g_warm_items addObject:item]; + atomic_store_explicit(&g_warm_ready, 1, memory_order_release); + os_unfair_lock_unlock(&g_warm_lock); +} +// 0 = prepared, 1 = failure, 2 = unsupported recorded specialization. +static int warm_row(NSString *line, NSMutableDictionary *dict) { + NSArray *c = [line componentsSeparatedByString:@"\t"]; + if ([c[0] isEqualToString:@"S"] && c.count >= 3) { + NSString *key = c[1]; NvShader *sh = warm_shader(dict, key, c[2].intValue); + if (!sh) return 1; + WarmItem *item = [WarmItem new]; item->kind = 0; item->key = key; item->shader = sh; + warm_enqueue(item); g_warm_shaders++; return 0; + } + if ([c[0] isEqualToString:@"R"] && c.count >= 2) { + pso_skey sk; if (pso_skey_parse(c[1].UTF8String, &sk)) return 1; + NSString *vkey = warm_key(sk.vs, sizeof sk.vs), *fkey = warm_key(sk.fs, sizeof sk.fs); + NvShader *vs = vkey.length ? warm_shader(dict, vkey, sk.vs_stage) : nil; + NvShader *fs = fkey.length ? warm_shader(dict, fkey, sk.fs_stage) : nil; + if (!vs || (fkey.length && !fs)) return 1; + id fsfn = nil; + if (sk.fz) { + if (sk.fz_desc.kind == 1 && fs) { int fx[32]; for (int i = 0; i < 32; i++) fx[i] = sk.fz_desc.fx[i]; fsfn = bf_fn(fs, fx, sk.fz_desc.fbmask, sk.k.lmap); } + if (!fsfn) { g_warm_skip_fz++; return 2; } + } + pso_key k = sk.k; k.vs = (__bridge void *)vs; k.fs = (__bridge void *)fs; k.fsfn = (__bridge void *)fsfn; + NSError *e = nil; id ps = pso_build(&k, vs, fs, fsfn, sk.merged, sk.fz || vs->specialized || (fs && fs->specialized), &e); + if (!ps) { MLOG("【红】Startup render pipeline failed: %s", e.localizedDescription.UTF8String); return 1; } + WarmItem *item = [WarmItem new]; item->kind = 1; item->vs_key = vkey; item->fs_key = fkey; item->vs = vs; item->fs = fs; item->fsfn = fsfn; item->pso = k; item->object = ps; + warm_enqueue(item); g_warm_rps++; return 0; + } + if ([c[0] isEqualToString:@"C"] && c.count >= 2) { + NSString *key = c[1]; if (!key.length) return 1; + NvShader *cs = warm_shader(dict, key, 5); if (!cs) return 1; + NSError *e = nil; id ps = nvn_cached_compute(g_dev, cs->fn, &e); + if (!ps) { MLOG("【红】Startup compute pipeline failed: %s", e.localizedDescription.UTF8String); return 1; } + WarmItem *item = [WarmItem new]; item->kind = 2; item->key = key; item->shader = cs; item->object = ps; + warm_enqueue(item); g_warm_cps++; return 0; + } + return 1; +} +static void warm_worker(NSArray *paths) { + @autoreleasepool { + NSMutableDictionary *dict = [NSMutableDictionary dictionary]; + NSMutableOrderedSet *rows = [NSMutableOrderedSet orderedSet]; + for (NSString *path in paths) { + NSString *txt = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; + if (!txt) { atomic_fetch_add(&g_prepare_failed, 1); MLOG("【红】Warmup manifest unavailable: %s", path.UTF8String); continue; } + for (NSString *raw in [txt componentsSeparatedByString:@"\n"]) { + NSString *line = [raw stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]; + if (line.length && ![line hasPrefix:@"#"]) [rows addObject:line]; + } + } + atomic_store(&g_prepare_total, (uint32_t)rows.count); + if (!rows.count) atomic_fetch_add(&g_prepare_failed, 1); + if (atomic_load(&g_prepare_frontend)) atomic_store(&g_prepare_state, NVN_PREP_WORKING); + for (NSString *line in rows) { + prepare_foreground_wait(); + @autoreleasepool { + int result = warm_row(line, dict); + if (result == 1) atomic_fetch_add(&g_prepare_failed, 1); + if (result == 2) atomic_fetch_add(&g_prepare_skipped, 1); + uint32_t done = atomic_fetch_add(&g_prepare_done, 1) + 1; + // Before guest startup there is only one owner of these tables. + // Drain transient warm entries regularly without drawing any frame. + if (atomic_load(&g_prepare_frontend) && done % 64 == 0) warm_claim(); + } + } + MLOG("Pipeline preparation: known=%u done=%u failed=%u skipped=%u shaders=%llu render=%llu compute=%llu elapsed=%.1fms", + atomic_load(&g_prepare_total), atomic_load(&g_prepare_done), atomic_load(&g_prepare_failed), atomic_load(&g_prepare_skipped), + g_warm_shaders, g_warm_rps, g_warm_cps, (bp_now() - g_warm_t0) / 1e6); + } + atomic_store(&g_warm_running, 0); +} +// Wait for the background warm-up to finish (at most timeout_ms): returns how many milliseconds were actually waited, 0 if it is not running. On first install the system shader cache is fully cold; if warm-up is slower than the player entering a run, +// pipelines not yet warmed are compiled on the spot on the submit thread = in-run stutter; the loading screen on entering a run blocks the player anyway, so let it load a little longer (called where hle_nvn.c presents). +int mtl_warm_wait(int timeout_ms) { + if (!atomic_load(&g_warm_running)) return 0; + uint64_t t0 = bp_now(); + while (atomic_load(&g_warm_running) && (bp_now() - t0) / 1000000ull < (uint64_t)timeout_ms) usleep(2000); + return (int)((bp_now() - t0) / 1000000ull) + 1; +} +static void warm_start(void) { + if (g_warm_started++) return; + const char *e = getenv("DM_PSO_MANIFEST"); + if (e && !strcmp(e, "off")) { atomic_fetch_add(&g_prepare_skipped, 1); return; } + NSArray *paths; + if (e && *e) { + NSMutableArray *all = [NSMutableArray arrayWithObject:@(e)]; + NSString *user = pso_user_path(); + if (user && ![user isEqualToString:@(e)] && [NSFileManager.defaultManager fileExistsAtPath:user]) [all addObject:user]; + paths = all; + } else { + NSString *seed = [g_shmap_path.stringByDeletingLastPathComponent stringByAppendingPathComponent:@"pso_manifest.tsv"]; + NSMutableArray *all = [NSMutableArray arrayWithObject:seed]; + NSString *user = pso_user_path(); + if (user && [NSFileManager.defaultManager fileExistsAtPath:user]) [all addObject:user]; + else MLOG("管线预热:本机用户清单%s%s", user ? "还没有:" : "没有路径(DM_PSO_USER / DM_SAVE 都没设)", user ? user.UTF8String : ""); + if (![NSFileManager.defaultManager fileExistsAtPath:seed]) MLOG("【红】管线预热:种子清单不存在:%s", seed.UTF8String); + paths = all; + } + if (!g_warm_items) g_warm_items = [NSMutableArray array]; + g_warm_t0 = bp_now(); + dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, 0); // Not optional background work: it decides whether the pipelines are ready before the player enters a run + dispatch_queue_t q = dispatch_queue_create("dm.nvn.pso-warm", attr); + atomic_store(&g_warm_running, 1); + if (atomic_load(&g_prepare_frontend)) warm_worker(paths); + else dispatch_async(q, ^{ warm_worker(paths); }); +} +void mtl_prepare_start(void) { + int expected = NVN_PREP_IDLE; + if (!atomic_compare_exchange_strong(&g_prepare_state, &expected, NVN_PREP_LOADING)) return; + atomic_store(&g_prepare_frontend, 1); + dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ + @autoreleasepool { + prepare_foreground_wait(); + const char *map = getenv("DM_NVN_SHMAP"), *msl = getenv("DM_MSL_DIR"); + NSString *libraries = msl ? [@(msl) stringByAppendingPathComponent:@"b-" DMSH_CHAIN] : nil; + if (!map || !libraries || ![NSFileManager.defaultManager fileExistsAtPath:@(map)] || + ![NSFileManager.defaultManager fileExistsAtPath:libraries]) { + atomic_fetch_add(&g_prepare_failed, 1); atomic_store(&g_prepare_state, NVN_PREP_FAILED); + MLOG("【红】Startup preparation missing shader map/libraries"); return; + } + mtl_init(); shmap_load(); // warm_start runs synchronously on this worker. + warm_claim(); + prepare_foreground_wait(); + atomic_store(&g_prepare_state, NVN_PREP_SAVING); + nvn_cache_flush(); + nvn_cache_status cs; nvn_cache_snapshot(&cs); + MLOG("Startup gate ready: hits=%llu misses=%llu bypassed=%llu persisted=%llu failed_write=%d", cs.hits, cs.misses, cs.bypassed, cs.saved, cs.write_failed); + atomic_store(&g_prepare_state, atomic_load(&g_prepare_failed) || atomic_load(&g_prepare_skipped) || cs.write_failed ? NVN_PREP_PARTIAL : NVN_PREP_READY); + } + }); +} +static int g_rt_ovr; // Stencil override for runtime extrapolation: 0 no override; 1 pass only on the unit / effect / extrapolation-hole bits (0xD0), no stencil write; 3 depth test as usual, write the effect bit 0x40 where it passes +static id dss_get(int has_depth, int has_stencil) { + static int diag = -1; + if (diag < 0) { const char *e = getenv("DM_NVN_DEPTH_ALWAYS"); diag = e && !strcmp(e, "1"); if (diag) MLOG("DM_NVN_DEPTH_ALWAYS=1: force depth compare Always and disable stencil tests"); } + struct { ds_st ds; uint32_t vm[2], wm[2]; int hd, hs, ovr, diag; } k; + memset(&k, 0, sizeof k); k.ds = S.ds; memcpy(k.vm, S.st_vmask, 8); memcpy(k.wm, S.st_wmask, 8); k.hd = has_depth; k.hs = has_stencil; k.ovr = has_stencil ? g_rt_ovr : 0; k.diag = diag; + if (!g_dss.klen) g_dss.klen = sizeof k; + id s = hm_get_id(&g_dss, &k); + if (s) return s; + MTLDepthStencilDescriptor *d = [MTLDepthStencilDescriptor new]; + if (has_depth && diag) { d.depthCompareFunction = MTLCompareFunctionAlways; d.depthWriteEnabled = S.ds.dw; } + else if (has_depth && S.ds.dt) { d.depthCompareFunction = (MTLCompareFunction)(S.ds.df - 1); d.depthWriteEnabled = S.ds.dw; } + if (has_stencil && S.ds.st && !diag) { + MTLStencilDescriptor *f = [MTLStencilDescriptor new], *b = [MTLStencilDescriptor new]; + f.stencilCompareFunction = (MTLCompareFunction)(S.ds.sf_f - 1); f.stencilFailureOperation = (MTLStencilOperation)(S.ds.fail_f - 1); + f.depthFailureOperation = (MTLStencilOperation)(S.ds.zfail_f - 1); f.depthStencilPassOperation = (MTLStencilOperation)(S.ds.zpass_f - 1); + f.readMask = S.st_vmask[0]; f.writeMask = S.st_wmask[0]; + b.stencilCompareFunction = (MTLCompareFunction)(S.ds.sf_b - 1); b.stencilFailureOperation = (MTLStencilOperation)(S.ds.fail_b - 1); + b.depthFailureOperation = (MTLStencilOperation)(S.ds.zfail_b - 1); b.depthStencilPassOperation = (MTLStencilOperation)(S.ds.zpass_b - 1); + b.readMask = S.st_vmask[1]; b.writeMask = S.st_wmask[1]; + d.frontFaceStencil = f; d.backFaceStencil = b; + } + if (has_stencil && k.ovr) { + MTLStencilDescriptor *f = [MTLStencilDescriptor new]; + if (k.ovr == 1) { f.stencilCompareFunction = MTLCompareFunctionNotEqual; f.readMask = 0xD0; f.writeMask = 0; } // Reference value 0: passes only if (s & 0xD0) != 0 (unit 0x10, effect 0x40, extrapolation hole / screen edge 0x80) + else { f.stencilCompareFunction = MTLCompareFunctionAlways; f.depthStencilPassOperation = MTLStencilOperationReplace; f.readMask = 0; f.writeMask = 0x40; } // Reference value 0x40 + d.frontFaceStencil = f; d.backFaceStencil = f; + } + s = [g_dev newDepthStencilStateWithDescriptor:d]; + hm_put_id(&g_dss, &k, s); + return s; +} +// Argument buffer: filled item by item per the B reflection (uniform slot r, separate texture / sampler slot r, storage buffer slot r, driver segments c0 / c2, c1, support_buffer) +// Unbound resources read 0: give all-zero dummy resources (NVN descriptors 0..255 are reserved for the driver; the game binding handle 0 means 'this slot is empty') +static uint64_t g_dummy_tex_rid[8][4], g_dummy_img_rid[8][4], g_dummy_tbuf_rid[4]; +static id dummy_tex(int dtt, int dpf) { + static __strong id cache[8][4]; + MTLTextureType tt = (MTLTextureType)dtt; + MTLPixelFormat pf = dpf == NVN_DPF_DEPTH ? MTLPixelFormatDepth32Float : dpf == NVN_DPF_UINT ? MTLPixelFormatRGBA32Uint : dpf == NVN_DPF_SINT ? MTLPixelFormatRGBA32Sint : MTLPixelFormatRGBA8Unorm; + id t = cache[dtt & 7][dpf & 3]; + if (t) return t; + MTLTextureDescriptor *d = [MTLTextureDescriptor new]; + d.textureType = tt; d.pixelFormat = pf; d.width = d.height = d.depth = 1; d.storageMode = MTLStorageModeShared; d.usage = MTLTextureUsageShaderRead; + t = [g_dev newTextureWithDescriptor:d]; // Shared storage is all zeros when newly created + if (pf != MTLPixelFormatDepth32Float) { uint8_t z[64] = {0}; NSUInteger faces = tt == MTLTextureTypeCube || tt == MTLTextureTypeCubeArray ? 6 : 1; + for (NSUInteger f = 0; f < faces; f++) [t replaceRegion:MTLRegionMake3D(0, 0, 0, 1, 1, 1) mipmapLevel:0 slice:f withBytes:z bytesPerRow:16 bytesPerImage:16]; } + cache[dtt & 7][dpf & 3] = t; g_dummy_tex_rid[dtt & 7][dpf & 3] = legacy_texture_rid(t); res_add(t); + return t; +} +// Write sink for unbound image slots: kept separate from the dummy textures that read 0 (writes must not pollute them); one is shared per same type and same pixel class +static id dummy_img(int dtt, int dpf) { + static __strong id cache[8][4]; + id t = cache[dtt & 7][dpf & 3]; if (t) return t; + MTLTextureDescriptor *d = [MTLTextureDescriptor new]; + d.textureType = (MTLTextureType)dtt; d.pixelFormat = dpf == NVN_DPF_UINT ? MTLPixelFormatRGBA32Uint : dpf == NVN_DPF_SINT ? MTLPixelFormatRGBA32Sint : MTLPixelFormatRGBA16Float; + d.width = d.height = d.depth = 1; d.storageMode = MTLStorageModePrivate; d.usage = MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite; + t = [g_dev newTextureWithDescriptor:d]; cache[dtt & 7][dpf & 3] = t; g_dummy_img_rid[dtt & 7][dpf & 3] = legacy_texture_rid(t); res_add(t); return t; +} +static id dummy_buf(void); +static id dummy_tbuf(int dpf) { // Unbound typed buffer: all zeros (pixel class matches the shader's texture_buffer) + static __strong id cache[4]; + id t = cache[dpf & 3]; if (t) return t; + MTLPixelFormat pf = dpf == NVN_DPF_UINT ? MTLPixelFormatRGBA32Uint : dpf == NVN_DPF_SINT ? MTLPixelFormatRGBA32Sint : MTLPixelFormatRGBA32Float; + id b = dummy_buf(); + MTLTextureDescriptor *d = [MTLTextureDescriptor textureBufferDescriptorWithPixelFormat:pf width:b.length / 16 resourceOptions:b.resourceOptions usage:MTLTextureUsageShaderRead]; + t = [b newTextureWithDescriptor:d offset:0 bytesPerRow:b.length]; + cache[dpf & 3] = t; g_dummy_tbuf_rid[dpf & 3] = legacy_texture_rid(t); return t; +} +static id dummy_samp(void) { + static id s; if (!s) { MTLSamplerDescriptor *d = [MTLSamplerDescriptor new]; d.supportArgumentBuffers = YES; s = [g_dev newSamplerStateWithDescriptor:d]; } + return s; +} +static uint64_t dummy_samp_rid(void) { static uint64_t rid; if (!rid) rid = legacy_sampler_rid(dummy_samp()); return rid; } +static id dummy_buf(void) { static id b; if (!b) { b = [g_dev newBufferWithLength:64 << 10 options:MTLResourceStorageModeShared]; res_add(b); } return b; } +static uint64_t dummy_buf_gpu(void) { static uint64_t gpu; if (!gpu) gpu = legacy_buffer_gpu(dummy_buf()); return gpu; } +// A shader can still calculate image-atomic addresses from the dimensions of a +// bound block-linear texture even though its guest layout cannot be exposed as +// a linear Metal buffer. Keep those writes inside a sink large enough for the +// logical image rather than the generic 64 KB unbound-resource buffer. +static id imga_dummy(size_t need) { + static id b; + if (need < (64u << 10)) need = 64u << 10; + if (need > SIZE_MAX - 0xffffu) MDIE("Image-atomic sink size overflows: %zu bytes", need); + need = (need + 0xffffu) & ~(size_t)0xffffu; + if (!b || b.length < need) { + id next = [g_dev newBufferWithLength:need options:MTLResourceStorageModeShared]; + // Binding the generic 64 KB dummy here would leave valid image + // coordinates outside the allocation. Fail before GPU encoding. + if (!next || next.length < need) MDIE("Cannot allocate %zu-byte image-atomic sink", need); + id old = b; + b = next; + res_add(next); + if (old) defer_obj_release(old); + } + return b; +} + +// Z-A's 2bd5037003980716_b first clears a 480x270x3 R32Uint image, then +// performs atomicMax through SPIRV-Cross's linear-buffer companion. The next +// pass samples that same image. A block-linear NVN image resolves +// to a private Metal texture, so the pointer and the texture otherwise refer +// to different storage. Keep one zeroed shadow per submission allocator and +// copy every completed array plane before the consumer pass. The bounded +// shader variant uses the same packed row/slice layout as this companion. +static __strong id g_za_atomic_ring[ALLOC_MAX]; +static size_t g_za_atomic_ring_size[ALLOC_MAX]; +static uint64_t g_za_atomic_ring_submit[ALLOC_MAX]; +static int g_za_atomic_pending; +static __strong id g_za_atomic_pending_buf; +static __strong id g_za_atomic_pending_tex; +static NSUInteger g_za_atomic_pending_w, g_za_atomic_pending_h; + +static id za_atomic_shadow(size_t need) { + int k = g_alloc_cur; + if (k < 0 || k >= ALLOC_MAX) return nil; + if (need < (64u << 10)) need = 64u << 10; + if (need > SIZE_MAX - 0xffffu) return nil; + need = (need + 0xffffu) & ~(size_t)0xffffu; + if (!g_za_atomic_ring[k] || g_za_atomic_ring_size[k] < need) { + id old = g_za_atomic_ring[k]; + id b = [g_dev newBufferWithLength:need options:MTLResourceStorageModeShared]; + if (!b) return nil; + g_za_atomic_ring[k] = b; g_za_atomic_ring_size[k] = need; g_za_atomic_ring_submit[k] = 0; + res_add(b); if (old) defer_obj_release(old); + } + if (g_za_atomic_ring_submit[k] != g_sub_serial) { + memset(g_za_atomic_ring[k].contents, 0, g_za_atomic_ring[k].length); + g_za_atomic_ring_submit[k] = g_sub_serial; + } + return g_za_atomic_ring[k]; +} + +static void za_atomic_schedule(id b, id t) { + if (!b || !t || t.textureType != MTLTextureType2DArray || t.pixelFormat != MTLPixelFormatR32Uint) { + RED_ONCE(94, "Z-A image-atomic shadow has an unsupported destination (buffer %p, texture type %lu format %lu)", + (__bridge void *)b, (unsigned long)t.textureType, (unsigned long)t.pixelFormat); + return; + } + g_za_atomic_pending = 1; g_za_atomic_pending_buf = b; g_za_atomic_pending_tex = t; + g_za_atomic_pending_w = t.width; g_za_atomic_pending_h = t.height; +} + +static void za_atomic_sync_end(void) { + if (!g_za_atomic_pending) return; + g_za_atomic_pending = 0; + id b = g_za_atomic_pending_buf; id t = g_za_atomic_pending_tex; + NSUInteger w = g_za_atomic_pending_w, h = g_za_atomic_pending_h; + g_za_atomic_pending_buf = nil; g_za_atomic_pending_tex = nil; + size_t row = (size_t)w * 4, image = row * (size_t)h, layers = t.arrayLength; + if (!w || !h || !layers || image / row != h || layers > b.length / image) { + RED_ONCE(95, "Z-A image-atomic shadow copy is out of range (%lux%lu, %zu-byte buffer)", + (unsigned long)w, (unsigned long)h, (size_t)b.length); + return; + } + id bl = enc_b_begin(g_cb); + for (NSUInteger layer = 0; layer < layers; layer++) + enc_b_copy_buffer_texture(bl, b, layer * image, row, image, MTLSizeMake(w, h, 1), t, layer, 0, MTLOriginMake(0, 0, 0)); + [bl fillBuffer:b range:NSMakeRange(0, image * layers) value:0]; + enc_b_end(bl); + static int once; + if (!once++) MLOG("Z-A image atomics: synchronized %lux%lux%lu R32Uint shadow before the consumer pass", + (unsigned long)w, (unsigned long)h, (unsigned long)layers); +} +#include "nvn_zero_image_runtime.h" + +// Pokémon Legends: Z-A black-frame diagnostic. Its fragment shader +// 2bd5037003980716_b has no colour output: it rasterizes into an R32Uint image +// through an atomic buffer pointer. On Metal the block-linear image and the +// linear atomic pointer do not share storage, so record the complete producer → +// consumer chain before changing its semantics. DM_NVN_ZA_DIAG=1 captures at +// most twelve frames. Every other value leaves the production path with no +// diagnostic references, allocations, blits or readbacks. +static int g_za_diag_armed, g_za_diag_frames, g_za_diag_use_n; +static uint64_t g_za_diag_last_frame = UINT64_MAX, g_za_diag_ser, g_za_diag_src_ser; +static int g_za_diag_tid, g_za_diag_src_tid; +static size_t g_za_diag_atomic_bytes; +static __strong id g_za_diag_atomic; +static __strong id g_za_diag_out, g_za_diag_src; + +static int za_diag_enabled(void) { + static dispatch_once_t once; static int enabled; + dispatch_once(&once, ^{ + const char *value = getenv("DM_NVN_ZA_DIAG"); + enabled = value && !strcmp(value, "1"); + }); + return enabled; +} + +static void za_diag_words(const char *what, uint64_t frame, uint64_t pass, int tid, uint64_t ser, + const void *data, size_t bytes) { + const uint32_t *p = data; size_t n = bytes / 4, nz = 0; uint32_t lo = UINT32_MAX, hi = 0; + uint64_t hash = 1469598103934665603ull; + for (size_t i = 0; i < n; i++) { + uint32_t v = p[i]; hash ^= v; hash *= 1099511628211ull; + if (v) { nz++; if (v < lo) lo = v; if (v > hi) hi = v; } + } + MLOG("[ZA-DIAG] %s frame=%llu pass=%llu tid=%d ser=%llu bytes=%zu nonzero_words=%zu min=%#x max=%#x hash=%016llx", + what, (unsigned long long)frame, (unsigned long long)pass, tid, (unsigned long long)ser, + bytes, nz, nz ? lo : 0, hi, (unsigned long long)hash); +} + +static size_t za_diag_texture_bytes(id t, NSUInteger *slices, size_t *row, size_t *image) { + if (!t) return 0; + size_t bpp = mtl_pf_bytes(t.pixelFormat); if (!bpp) return 0; + NSUInteger ns = 1; + if (t.textureType == MTLTextureType2DArray) ns = MAX(t.arrayLength, 1u); + else if (t.textureType == MTLTextureTypeCube) ns = 6; + else if (t.textureType == MTLTextureTypeCubeArray) ns = MAX(t.arrayLength, 1u) * 6; + else if (t.textureType == MTLTextureType3D) ns = MAX(t.depth, 1u); + size_t r = (size_t)t.width * bpp, im = r * (size_t)t.height; + if (!r || im / r != t.height || ns > SIZE_MAX / im) return 0; + *slices = ns; *row = r; *image = im; return im * ns; +} + +static void za_diag_arm(NvShader *sh, int stage, int reg, int tid, const tex_t *tt, + id atomic, size_t atomic_bytes) { + if (!za_diag_enabled()) return; + if (!tt || strcmp(sh->key, "2bd5037003980716_b")) return; + uint64_t frame = atomic_load(&g_cur_frame); + g_za_diag_ser = tt->serial; g_za_diag_tid = tid; + if (g_za_diag_armed || frame == g_za_diag_last_frame || g_za_diag_frames >= 12) return; + g_za_diag_last_frame = frame; g_za_diag_frames++; + int src_tid = stage < 6 ? (int)(S.tex[stage][0] & 0xfffff) : 0; + const tex_t *src_tt = src_tid > 0 && src_tid < g_ntreg && g_treg[src_tid].ok ? &g_treg[src_tid].t : NULL; + void *outv = NULL, *srcv = NULL; + (void)tex_fast_rid(tid, &outv); if (src_tid) (void)tex_fast_rid(src_tid, &srcv); + g_za_diag_armed = 1; g_za_diag_atomic = atomic; g_za_diag_atomic_bytes = atomic_bytes; + g_za_diag_out = (__bridge id)outv; g_za_diag_src = (__bridge id)srcv; + g_za_diag_src_tid = src_tid; g_za_diag_src_ser = src_tt ? src_tt->serial : 0; + MLOG("[ZA-DIAG] arm frame=%llu pass=%llu shader=%s stage=%d image_reg=%d out_tid=%d ser=%llu nvn=%#x target=%u flags=%#x size=%ux%ux%u levels=%u layout=%llu atomic_bytes=%zu metal=%lu %lux%lux%lu array=%lu type=%lu usage=%#lx src_tid=%d src_ser=%llu src_metal=%lu %lux%lux%lu type=%lu", + (unsigned long long)frame, (unsigned long long)g_stat_pass, sh->key, stage, reg, tid, + (unsigned long long)tt->serial, tt->d.format, tt->d.target, tt->d.flags, tt->d.width, tt->d.height, + tt->d.depth, tt->d.levels, (unsigned long long)tt->L.size, atomic_bytes, + (unsigned long)g_za_diag_out.pixelFormat, (unsigned long)g_za_diag_out.width, + (unsigned long)g_za_diag_out.height, (unsigned long)g_za_diag_out.depth, + (unsigned long)g_za_diag_out.arrayLength, (unsigned long)g_za_diag_out.textureType, + (unsigned long)g_za_diag_out.usage, src_tid, (unsigned long long)g_za_diag_src_ser, + (unsigned long)g_za_diag_src.pixelFormat, (unsigned long)g_za_diag_src.width, + (unsigned long)g_za_diag_src.height, (unsigned long)g_za_diag_src.depth, + (unsigned long)g_za_diag_src.textureType); +} + +static void za_diag_use(const char *kind, NvShader *sh, int stage, int reg, int tid, uint64_t ser) { + if (!za_diag_enabled()) return; + if (!g_za_diag_ser || (tid != g_za_diag_tid && ser != g_za_diag_ser)) return; + if (g_za_diag_use_n++ < 160) + MLOG("[ZA-DIAG] consume frame=%llu pass=%llu kind=%s shader=%s stage=%d reg=%d tid=%d ser=%llu", + (unsigned long long)atomic_load(&g_cur_frame), (unsigned long long)g_stat_pass, kind, + sh && sh->key[0] ? sh->key : "-", stage, reg, tid, (unsigned long long)ser); +} + +static void za_diag_op(const char *kind, const tex_t *t) { + if (!za_diag_enabled()) return; + if (!t || !g_za_diag_ser || t->serial != g_za_diag_ser) return; + if (g_za_diag_use_n++ < 160) + MLOG("[ZA-DIAG] texture-op frame=%llu pass=%llu kind=%s ser=%llu nvn=%#x size=%ux%ux%u", + (unsigned long long)atomic_load(&g_cur_frame), (unsigned long long)g_stat_pass, kind, + (unsigned long long)t->serial, t->d.format, t->d.width, t->d.height, t->d.depth); +} + +static void za_diag_pass_end(void) { + if (!za_diag_enabled()) return; + if (!g_za_diag_armed) return; + g_za_diag_armed = 0; + uint64_t frame = g_za_diag_last_frame, pass = g_stat_pass, ser = g_za_diag_ser, src_ser = g_za_diag_src_ser; + int tid = g_za_diag_tid, src_tid = g_za_diag_src_tid; + id atomic = g_za_diag_atomic; size_t atomic_bytes = MIN(g_za_diag_atomic_bytes, atomic.length); + id out = g_za_diag_out, src = g_za_diag_src; + g_za_diag_atomic = nil; g_za_diag_out = nil; g_za_diag_src = nil; + + NSUInteger out_slices = 0, src_slices = 0; size_t out_row = 0, out_img = 0, src_row = 0, src_img = 0; + size_t out_bytes = za_diag_texture_bytes(out, &out_slices, &out_row, &out_img); + size_t src_bytes = za_diag_texture_bytes(src, &src_slices, &src_row, &src_img); + if (out_bytes > (64u << 20)) out_bytes = 0; + if (src_bytes > (64u << 20)) src_bytes = 0; + id out_rb = out_bytes ? [g_dev newBufferWithLength:out_bytes options:MTLResourceStorageModeShared] : nil; + id src_rb = src_bytes ? [g_dev newBufferWithLength:src_bytes options:MTLResourceStorageModeShared] : nil; + if (out_rb) res_add(out_rb); if (src_rb) res_add(src_rb); + if (out_rb || src_rb) { + id b = enc_b_begin(g_cb); + if (out_rb) for (NSUInteger s = 0; s < out_slices; s++) + enc_b_copy_texture_buffer(b, out, out.textureType == MTLTextureType3D ? 0 : s, 0, + MTLOriginMake(0, 0, out.textureType == MTLTextureType3D ? s : 0), + MTLSizeMake(out.width, out.height, 1), out_rb, s * out_img, out_row, out_img); + if (src_rb) for (NSUInteger s = 0; s < src_slices; s++) + enc_b_copy_texture_buffer(b, src, src.textureType == MTLTextureType3D ? 0 : s, 0, + MTLOriginMake(0, 0, src.textureType == MTLTextureType3D ? s : 0), + MTLSizeMake(src.width, src.height, 1), src_rb, s * src_img, src_row, src_img); + enc_b_end(b); + } + [g_commit_blocks addObject:^{ + za_diag_words("atomic-buffer-after-producer", frame, pass, tid, ser, atomic.contents, atomic_bytes); + if (out_rb) za_diag_words("output-texture-after-producer", frame, pass, tid, ser, out_rb.contents, out_bytes); + else MLOG("[ZA-DIAG] output texture readback unavailable frame=%llu pass=%llu tid=%d ser=%llu", (unsigned long long)frame, (unsigned long long)pass, tid, (unsigned long long)ser); + if (src_rb) za_diag_words("source-texture-at-producer", frame, pass, src_tid, src_ser, src_rb.contents, src_bytes); + else MLOG("[ZA-DIAG] source texture readback unavailable frame=%llu pass=%llu tid=%d ser=%llu", (unsigned long long)frame, (unsigned long long)pass, src_tid, (unsigned long long)src_ser); + }]; +} +// useResource is idempotent for the same (resource, usage, stage) within one encoder: remember what this render encoder has already declared and skip repeats (a compute encoder is created anew for each dispatch, no dedup) +// Snapshot substitution for same-pass feedback sampling (registered by fb_prepare, used by encode_args; see the notes before draw) +static uint64_t g_draw_seq, g_fb_seq, g_fb_count; static int g_fb_n, g_fb_rebind; +static __unsafe_unretained id g_fb_src[4]; static __strong id g_fb_use[4]; static uint64_t g_fb_rid[4]; +static uint8_t g_args_dirty[6] = {1, 1, 1, 1, 1, 1}; +static uint64_t g_args_reg_seen, g_args_dirty_seen; +static inline void args_dirty_all(void) { memset(g_args_dirty, 1, sizeof g_args_dirty); } +static inline void args_sync_dirty(void) { + uint64_t reg = g_reg_gen, dirty = nvn_dirty_now(); + if (reg != g_args_reg_seen || dirty != g_args_dirty_seen) { + g_args_reg_seen = reg; g_args_dirty_seen = dirty; args_dirty_all(); + } +} +static void bind_args(id en, int nvn_stage, int metal_vertex, id ab, uint64_t gpu, size_t off, NSUInteger ai) { + (void)gpu; // The resolved buffer and offset are already available; avoid the second address-index lookup. + if (nvn_stage == 5) [(id)en setBuffer:ab offset:off atIndex:ai]; + else legacy_bind_buffer((id)en, !metal_vertex, ai, ab, off); +} +// Static constant buffers of route B: the Ryujinx support_buffer (layout in Ryujinx.Graphics.Shader/SupportBuffer.cs: alpha_test u32, is_bgra u32[8], +// viewport_inverse f4, viewport_size f4, frag_scale_count i32, render_scale f32[73]..., each field aligned to 16 bytes) and the all-0 driver constant segment (64 KB = the upper bound declared by shaders) +static id support_buf(void) { + static __strong id b; if (b) return b; + b = [g_dev newBufferWithLength:65536 options:MTLResourceStorageModeShared]; res_add(b); // All 0 when newly created + const int render_scale_off = 192; // Measured MSL layout: alpha_test 0, is_bgra[8] 16, viewport_inverse 144, viewport_size 160, frag_scale_count 176, render_scale float4[73] 192 (take .x) + for (int i = 0; i < 73; i++) *(float *)((uint8_t *)b.contents + render_scale_off + 16 * i) = 1.0f; + return b; +} +static uint64_t support_gpu(void) { static uint64_t gpu; if (!gpu) gpu = legacy_buffer_gpu(support_buf()); return gpu; } +static id zero_bank(void) { static __strong id b; if (!b) { b = [g_dev newBufferWithLength:65536 options:MTLResourceStorageModeShared]; res_add(b); } return b; } +static uint64_t zero_bank_gpu(void) { static uint64_t gpu; if (!gpu) gpu = legacy_buffer_gpu(zero_bank()); return gpu; } +// Shift all UI draws down as a whole (iPad true fullscreen, the UI rect does not touch the top edge of the picture): add a clip-space vertical offset to the projection matrix at the start of the vertex stage's cb0, and bind a modified copy instead. +// Moving the viewport is not used: the viewport's top edge is the edge of the clip volume, so top-anchored widgets that the host lifts back to the real top of the picture (UI coordinate y negative) would be clipped away entirely. +// g_ui_moff is set per draw by draw() (0 = no change); the copies live in a ring buffer, and the same address in the same pass is copied only once. +static double g_ui_moff; +static int ui_mat_ok(void) { const p_bind_buf *u = &S.ubo[0][0]; void *mb; uint64_t mo; return u->addr && u->size >= 64 && u->size <= 4096 && !pend_hits(u->addr, u->size) && mtl_addr(u->addr, u->size, &mb, &mo); } +static uint64_t ui_mat_patch(const p_bind_buf *u, void *mb, uint64_t mo) { + enum { RING = 4 << 20 }; static __strong id ring; static uint64_t ring_gpu, la, ls, lp, lg; static size_t at; static double lo; + if (!ring) { ring = [g_dev newBufferWithLength:RING options:MTLResourceStorageModeShared]; res_add(ring); ring_gpu = legacy_buffer_gpu(ring); } + if (la == u->addr && ls == u->size && lp == g_pass.serial && lo == g_ui_moff) return lg; + const uint8_t *src = [(__bridge id)mb contents]; if (!src || u->size < 64 || u->size > 4096) return 0; + size_t n = (u->size + 255) & ~(size_t)255; if (at + n > RING) at = 0; + uint8_t *dst = (uint8_t *)ring.contents + at; memcpy(dst, src + mo, u->size); + float m[16], o = (float)g_ui_moff; memcpy(m, dst, 64); m[1] += o * m[3]; m[5] += o * m[7]; m[9] += o * m[11]; m[13] += o * m[15]; memcpy(dst, m, 64); + la = u->addr; ls = u->size; lp = g_pass.serial; lo = g_ui_moff; lg = ring_gpu + at; at += n; + return lg; +} +// Bindless handle (TEXU / SAMPU): the 32-bit word at byte offset off of uniform slot reg = the low 32 bits of the NVN handle the game wrote there (low 20 bits texture ID, high 12 bits sampler ID). +// Read on the CPU side at replay time (the memory at the same moment a real GPU would read it at execution); slot unbound / out of range / unreadable = 0 (empty slot: dummy texture / dummy sampler). +// A location with a GPU-side write queued in this submit (pend_hits) is still read at its current value; the first case is reported red. +static uint32_t ubo_handle(int stage, int reg, int off) { + if (stage < 0 || stage >= 6 || reg < 0 || reg >= 16) return 0; + const p_bind_buf *u = &S.ubo[stage][reg]; + if (!u->addr || (uint64_t)off + 4 > u->size) return 0; + if (g_nubd && ubd_hits(u->addr + off, u->addr + off + 4) && ubd_covers(u->addr + off, u->addr + off + 4)) { uint32_t h; ubd_read(u->addr + off, 4, &h); return h; } // written by an inline update deferred in this pass + if (pend_hits(u->addr + off, 4)) RED_ONCE(47, "无绑定句柄所在的 uniform 缓冲(阶段 %d 槽 %d +%#x)本提交里有 GPU 侧写入:读的是当前值", stage, reg, off); + void *mb; uint64_t mo; const uint8_t *base; + if (!mtl_addr(u->addr + off, 4, &mb, &mo) || !(base = [(__bridge id)mb contents])) return 0; + uint32_t h; memcpy(&h, base + mo, 4); return h; +} +// Opt-in Z-A geometry diagnostics. An empty trigger file is inert; changing a +// non-empty file captures one frame. Limits keep live-device logging bounded. +// No pixel reads, synchronization waits or rendering overrides are introduced. +static struct { + int initialized, frames, samplers, textures, indirect, mismatches; + const char *file; + uint64_t probe_frame, frame, token, sampler_seen[64], mismatch_seen[64]; +} g_geom_diag; +static const char *geometry_diag_file(void) { + if (!g_geom_diag.initialized) { + g_geom_diag.initialized = 1; + g_geom_diag.probe_frame = g_geom_diag.frame = UINT64_MAX; + if (DM_GAME_PROGRAM_ID == 0x0100F43008C44000ull) + g_geom_diag.file = getenv("DM_NVN_GEOMETRY_DIAG_FILE"); + if (g_geom_diag.file && *g_geom_diag.file) + MLOG("Geometry diagnostics: change non-empty trigger %s; at most 8 frames, 512 indirect records and 64 reduction bindings per frame", g_geom_diag.file); + } + return g_geom_diag.file && *g_geom_diag.file ? g_geom_diag.file : NULL; +} +static int geometry_diag_frame(void) { + const char *file = geometry_diag_file(); if (!file) return 0; + uint64_t frame = atomic_load(&g_cur_frame); + if (g_geom_diag.probe_frame != frame && g_geom_diag.frames < 8) { + g_geom_diag.probe_frame = frame; + struct stat st; + if (!stat(file, &st)) { + uint64_t token = ((uint64_t)st.st_mtimespec.tv_sec * 1000000000ull + (uint64_t)st.st_mtimespec.tv_nsec) ^ (uint64_t)st.st_size; + if (token != g_geom_diag.token) { + g_geom_diag.token = token; + if (st.st_size > 0) { + g_geom_diag.frame = frame; g_geom_diag.frames++; + g_geom_diag.samplers = g_geom_diag.textures = g_geom_diag.indirect = g_geom_diag.mismatches = 0; + MLOG("[geometry] capture=%d frame=%llu", g_geom_diag.frames, (unsigned long long)frame); + } + } + } + } + return frame == g_geom_diag.frame; +} +static void geometry_diag_sampler(NvShader *sh, int stage, const nvn_rbind *binding, int sid) { + if (!geometry_diag_frame() || sid <= 0 || sid >= g_nsreg || !g_sreg_ok[sid] || + !g_sreg[sid].reduction || g_geom_diag.samplers >= 64) return; + uint64_t key = 1469598103934665603ull; + for (const char *p = sh->key; *p; p++) key = (key ^ (unsigned char)*p) * 1099511628211ull; + key = (key ^ ((uint64_t)stage << 48 | (uint64_t)binding->kind << 40 | (uint64_t)binding->reg << 24 | (unsigned)sid)) * 1099511628211ull; + for (int i = 0; i < g_geom_diag.samplers; i++) if (g_geom_diag.sampler_seen[i] == key) return; + g_geom_diag.sampler_seen[g_geom_diag.samplers++] = key; + const samp_par *s = &g_sreg[sid]; + uint32_t handle = binding->kind == NVN_RB_SAMPU ? ubo_handle(stage, binding->reg, binding->off) : + binding->kind == NVN_RB_SAMPT && binding->reg < NTEX ? (uint32_t)S.tex[stage][binding->reg] : 0; + int tid = (int)(handle & 0xfffff); treg_t *r = tid > 0 && tid < g_ntreg && g_treg[tid].ok ? &g_treg[tid] : NULL; + id t = r ? (__bridge id)r->view : nil; + MLOG("[geometry-sampler] frame=%llu st=%d key=%s reg=%u kind=%u sid=%d reduction=%u native_reduction=%d minmag=%u/%u lod=%g..%g bias=%g combined_tid=%d serial=%llu nvn_target=%u nvn_format=%#x view=%lux%lux%lu array=%lu mips=%lu type=%lu format=%lu", + (unsigned long long)g_geom_diag.frame, stage, sh->key, binding->reg, binding->kind, sid, s->reduction, + [MTLSamplerDescriptor instancesRespondToSelector:NSSelectorFromString(@"setReductionMode:")], + s->minf, s->magf, s->lod_min, s->lod_max, s->lod_bias, tid, (unsigned long long)(r ? r->t.serial : 0), + r ? r->t.d.target : 0, r ? r->t.d.format : 0, (unsigned long)t.width, (unsigned long)t.height, + (unsigned long)t.depth, (unsigned long)t.arrayLength, (unsigned long)t.mipmapLevelCount, + (unsigned long)t.textureType, (unsigned long)t.pixelFormat); + // Separate samplers have no inherent paired texture. Record the shader's + // texture bindings as candidates rather than inventing such a pairing. + if (!handle) for (int i = 0; i < sh->rf.nb && g_geom_diag.textures < 32; i++) { + const nvn_rbind *b = &sh->rf.b[i]; + if (b->kind != NVN_RB_TEX && b->kind != NVN_RB_TEXU) continue; + uint32_t h = b->kind == NVN_RB_TEXU ? ubo_handle(stage, b->reg, b->off) : + b->reg < NTEX ? (uint32_t)S.tex[stage][b->reg] : 0; + int texture_id = (int)(h & 0xfffff); + treg_t *texture = texture_id > 0 && texture_id < g_ntreg && g_treg[texture_id].ok ? &g_treg[texture_id] : NULL; + id view = texture ? (__bridge id)texture->view : nil; + g_geom_diag.textures++; + MLOG("[geometry-sampler-texture] frame=%llu st=%d key=%s sampler_reg=%u texture_reg=%u tid=%d serial=%llu nvn_target=%u nvn_format=%#x view=%lux%lux%lu array=%lu mips=%lu type=%lu format=%lu", + (unsigned long long)g_geom_diag.frame, stage, sh->key, binding->reg, b->reg, texture_id, + (unsigned long long)(texture ? texture->t.serial : 0), texture ? texture->t.d.target : 0, + texture ? texture->t.d.format : 0, (unsigned long)view.width, (unsigned long)view.height, + (unsigned long)view.depth, (unsigned long)view.arrayLength, (unsigned long)view.mipmapLevelCount, + (unsigned long)view.textureType, (unsigned long)view.pixelFormat); + } +} +// Report unresolved binding mismatches on demand, even after tex_fit's +// process-wide RED_ONCE has fired. Texture IDs may be reused across scenes. +static void geometry_diag_type_mismatch(NvShader *sh, int stage, const nvn_rbind *b, int tid, id t) { + if (!geometry_diag_frame() || g_geom_diag.mismatches >= 64) return; + uint64_t hash = 1469598103934665603ull; + for (const char *p = sh->key; *p; p++) hash = (hash ^ (unsigned char)*p) * 1099511628211ull; + hash = (hash ^ ((uint64_t)stage << 48 | (uint64_t)b->reg << 32 | (uint64_t)b->dtt << 24 | (uint64_t)t.textureType << 16 | (unsigned)tid)) * 1099511628211ull; + for (int i = 0; i < g_geom_diag.mismatches; i++) if (g_geom_diag.mismatch_seen[i] == hash) return; + g_geom_diag.mismatch_seen[g_geom_diag.mismatches++] = hash; + const treg_t *r = tid > 0 && tid < g_ntreg && g_treg[tid].ok ? &g_treg[tid] : NULL; + MLOG("[geometry-type-mismatch] frame=%llu pass=%llu st=%d key=%s reg=%u aid=%u kind=%u expected=%u actual=%lu pixel_class=%u tid=%d serial=%llu pool=%p offset=%#llx nvn_target=%u nvn_format=%#x nvn_size=%ux%ux%u nvn_levels=%u has_view=%u view_target=%u/%u view_format=%u/%#x view_levels=%u/%u+%u view_layers=%u/%u+%u metal=%lux%lux%lu array=%lu mips=%lu format=%lu usage=%#lx parent_type=%lu parent_level=%lu parent_slice=%lu", + (unsigned long long)g_geom_diag.frame, (unsigned long long)g_stat_pass, stage, sh->key, b->reg, b->aid, b->kind, + b->dtt, (unsigned long)t.textureType, b->dpf, tid, (unsigned long long)(r ? r->t.serial : 0), r ? r->t.pool : NULL, + (unsigned long long)(r ? r->t.off : 0), r ? r->t.d.target : 0, r ? r->t.d.format : 0, + r ? r->t.d.width : 0, r ? r->t.d.height : 0, r ? r->t.d.depth : 0, r ? r->t.d.levels : 0, + r ? r->has_v : 0, r && r->has_v ? r->v.set_target : 0, r && r->has_v ? r->v.target : 0, + r && r->has_v ? r->v.set_format : 0, r && r->has_v ? r->v.format : 0, + r && r->has_v ? r->v.set_levels : 0, r && r->has_v ? r->v.base_level : 0, r && r->has_v ? r->v.nlevels : 0, + r && r->has_v ? r->v.set_layers : 0, r && r->has_v ? r->v.min_layer : 0, r && r->has_v ? r->v.nlayers : 0, + (unsigned long)t.width, (unsigned long)t.height, (unsigned long)t.depth, (unsigned long)t.arrayLength, + (unsigned long)t.mipmapLevelCount, (unsigned long)t.pixelFormat, (unsigned long)t.usage, + (unsigned long)t.parentTexture.textureType, (unsigned long)t.parentRelativeLevel, (unsigned long)t.parentRelativeSlice); +} +#include "nvn_pixel_history.h" +// Bounded Wonder/A16 colour diagnostics. Metadata only: no pixel readback, uniform dumps, +// GPU waits or per-draw disk files. Caller holds g_rmu; only runs when fragment arguments rebuild. +// At most 8 snapshots per 600-frame window, one per frame, 96 per process. DM_NVN_COLOR_DIAG=0 disables. +static struct { int initialized, on, total, used; uint64_t window, last, seen[96]; } g_color_diag; +static uint64_t color_mix(uint64_t h, uint64_t v) { return (h ^ v) * 1099511628211ull; } +static int color_diag_begin(NvShader *sh, int stage) { + if (stage != 1 || DM_GAME_PROGRAM_ID != 0x010015100B514000ull) return 0; + if (!g_color_diag.initialized) { + g_color_diag.initialized = 1; + const char *e = getenv("DM_NVN_COLOR_DIAG"); + g_color_diag.on = e ? strcmp(e, "0") != 0 : no_bc(); + g_color_diag.last = ~0ull; + if (g_color_diag.on) MLOG("[color] 颜色诊断启用:每600帧最多8组、每帧1组、全程96组;按着色器/格式/尺寸/通道去重,不按瞬时序号;只记绑定元数据;DM_NVN_COLOR_DIAG=0关闭"); + } + if (!g_color_diag.on || g_color_diag.total >= 96) return 0; + uint64_t frame = atomic_load(&g_cur_frame), window = frame / 600; + if (window != g_color_diag.window) { g_color_diag.window = window; g_color_diag.used = 0; } + if (g_color_diag.used >= 8 || g_color_diag.last == frame) return 0; + unsigned reasons = 0; uint64_t hash = 1469598103934665603ull; + for (const char *k = sh->key; *k; k++) hash = color_mix(hash, (unsigned char)*k); + hash = color_mix(hash, R.rt.n); + for (int k = 0; k < R.rt.n && k < 8; k++) if (R.rt.color[k]) { + hash = color_mix(hash, R.rt.color[k]->d.format); + hash = color_mix(hash, (uint64_t)R.rt.color[k]->d.width << 32 | R.rt.color[k]->d.height); + } + for (int i = 0; i < sh->rf.nb; i++) { + const nvn_rbind *b = &sh->rf.b[i]; int reg = b->reg; + if (b->kind == NVN_RB_SAMP || b->kind == NVN_RB_SAMPT || b->kind == NVN_RB_SAMPU) { + int sid = b->kind == NVN_RB_SAMPU ? (int)(ubo_handle(stage, reg, b->off) >> 20) : + b->kind == NVN_RB_SAMPT ? (reg < NTEX ? (int)(S.tex[stage][reg] >> 20) : 0) : + reg < 128 ? (int)(S.samp[stage][reg] >> 20) : 0; + if (sid > 0 && sid < g_nsreg && g_sreg_ok[sid]) { + hash = color_mix(hash, g_sreg[sid].reduction); + if (g_sreg[sid].reduction) reasons |= 1; + } + } else if (b->kind == NVN_RB_TEX || b->kind == NVN_RB_TEXU) { + int tid = b->kind == NVN_RB_TEXU ? (int)(ubo_handle(stage, reg, b->off) & 0xfffff) : reg < NTEX ? (int)(S.tex[stage][reg] & 0xfffff) : 0; + if (tid <= 0 || tid >= g_ntreg || !g_treg[tid].ok) continue; + const treg_t *t = &g_treg[tid]; + hash = color_mix(hash, t->t.d.format); + hash = color_mix(hash, (uint64_t)t->t.d.width << 32 | t->t.d.height); + hash = color_mix(hash, t->t.set_swz ? t->t.swz : (2u | 3u << 3 | 4u << 6 | 5u << 9)); + if (t->has_v && t->v.set_swizzle) for (int c = 0; c < 4; c++) hash = color_mix(hash, t->v.swz[c]); + if (t->t.d.format == 0x4a || t->t.d.format == 0x4c) reasons |= 2; // signed BC4/5 + if (t->t.set_swz || (t->has_v && t->v.set_swizzle)) reasons |= 4; + for (int k = 0; k < R.rt.n && k < 8; k++) if (R.rt.color[k] && R.rt.color[k]->serial == t->t.serial) reasons |= 8; + } + } + if (!reasons) return 0; + for (int i = 0; i < g_color_diag.total; i++) if (g_color_diag.seen[i] == hash) return 0; + g_color_diag.seen[g_color_diag.total] = hash; g_color_diag.used++; g_color_diag.total++; g_color_diag.last = frame; + MLOG("[color] #%d F%llu pass=%llu fs=%s reasons=%u rt=%d depth=%#x fb=%d(1归约/2带符号BC/4通道重排/8附件采样)", + g_color_diag.total, (unsigned long long)frame, g_stat_pass, sh->key, reasons, R.rt.n, + R.rt.depth ? R.rt.depth->d.format : 0, g_fb_n && g_fb_seq == g_draw_seq); + for (int k = 0; k < R.rt.n && k < 8; k++) if (R.rt.color[k]) + MLOG("[color] rt%d ser=%llu nvn=%#x size=%ux%u", k, (unsigned long long)R.rt.color[k]->serial, + R.rt.color[k]->d.format, R.rt.color[k]->d.width, R.rt.color[k]->d.height); + return 1; +} +static uint32_t g_dispatch_dim[3]; // the current dispatch's workgroup counts (driver segment c0 + 0x200 for compute) +static uint32_t g_draw_binst; // current draw's base instance (driver segment c0 + 0x644) +// An indirect draw's base instance is produced on the GPU, so it is not in the +// CPU-side p_draw record when encode_args runs. safe_indirect_encode gives the +// draw a private c0 and patches +0x644 from the same guest indirect record. The +// rest of c0 is still populated here from the replay state before submission. +static __unsafe_unretained id g_indirect_drv0_buf; +static uint64_t g_indirect_drv0_gpu; +static NSUInteger g_indirect_drv0_off; +static int encode_args_(id en, NvShader *sh, int nvn_stage, int metal_vertex); +// DM_NVN_SAMPLE_TRACE=: keep binding metadata in memory, +// attached to this submission's first GPU error, without per-draw log I/O/waits. +// Optional DM_NVN_SAMPLE_TRACE_CB=: records one float4 +// from the actual encoded GPU buffer address (only if CPU-readable and in range). +// DM_NVN_SAMPLE_TRACE_VERTEX=1 also observes vertex arguments. BYTES=1 includes +// bounded CPU-readable constant, vertex and index snapshots; never GPU readback. +static const p_draw_idx *g_sample_trace_di; +static const p_draw *g_sample_trace_dw; +static int sample_trace_key(const char *key, const char *shader) { + for (const char *p = key; p && *p;) { + const char *comma = strchr(p, ','); size_t n = comma ? (size_t)(comma - p) : strlen(p); + if (n && !strncmp(shader, p, n)) return 1; + p = comma ? comma + 1 : NULL; + } + return 0; +} +static void sample_trace_bytes(id buffer, NSUInteger offset, uint64_t bytes) { + if (!buffer.contents || offset > buffer.length) return; + NSUInteger n = (NSUInteger)MIN(MIN(bytes, 1024ull), (uint64_t)(buffer.length - offset)); + const uint8_t *p = (const uint8_t *)buffer.contents + offset; + [g_sub_sample_notes appendFormat:@" bytes=%lu hex=", (unsigned long)n]; + for (NSUInteger j = 0; j < n; j++) [g_sub_sample_notes appendFormat:@"%02x", p[j]]; +} +static uint64_t sample_trace_hash(id buffer, NSUInteger offset, NSUInteger length) { + const uint8_t *p = (const uint8_t *)buffer.contents + offset; + uint64_t hash = UINT64_C(14695981039346656037); + for (NSUInteger i = 0; i < length; i++) { hash ^= p[i]; hash *= UINT64_C(1099511628211); } + return hash; +} +static void sample_trace_root(NvShader *sh, int stage, id buffer, NSUInteger offset, NSUInteger length) { + static const char *keys; static int init; static unsigned count; + static unsigned long long first_frame, last_frame = UINT64_MAX; + if (!init) { + init = 1; keys = getenv("DM_NVN_SAMPLE_TRACE"); + const char *range = getenv("DM_NVN_SAMPLE_TRACE_ROOT_FRAMES"); + if (range && sscanf(range, "%llu-%llu", &first_frame, &last_frame) != 2) keys = NULL; + } + uint64_t frame = atomic_load(&g_cur_frame); + if (!keys || frame < first_frame || frame > last_frame || !sample_trace_key(keys, sh->key) || count >= 192 || !buffer.contents || offset > buffer.length || length > buffer.length - offset) return; + count++; + uint64_t hash = sample_trace_hash(buffer, offset, length), pass = g_pass.serial; + NSString *key = @(sh->key); + if (!g_sub_argument_checks) g_sub_argument_checks = [NSMutableArray array]; + [g_sub_argument_checks addObject:^{ + uint64_t completed = sample_trace_hash(buffer, offset, length); + MLOG("%s[arg-trace] frame=%llu pass=%llu stage=%d shader=%s buffer=%p length=%lu offset=%lu encoded=%lu alignment=%lu before=%016llx completed=%016llx unchanged=%d", + hash == completed ? "" : "[RED] ", (unsigned long long)frame, (unsigned long long)pass, stage, key.UTF8String, + (__bridge void *)buffer, (unsigned long)buffer.length, (unsigned long)offset, (unsigned long)length, + (unsigned long)sh->ae.alignment, (unsigned long long)hash, (unsigned long long)completed, hash == completed); + }]; +} +static void sample_trace_args(NvShader *sh, int stage, const uint64_t *slots) { + static const char *key; static int init, cbreg = -1, vertex, bytes; static unsigned long long cboff; + if (!init) { init = 1; key = getenv("DM_NVN_SAMPLE_TRACE"); const char *c = getenv("DM_NVN_SAMPLE_TRACE_CB"); if (c && sscanf(c, "%d:%llx", &cbreg, &cboff) != 2) cbreg = -1; + const char *v = getenv("DM_NVN_SAMPLE_TRACE_VERTEX"), *b = getenv("DM_NVN_SAMPLE_TRACE_BYTES"); vertex = v && atoi(v); bytes = b && atoi(b); } + if ((stage != 1 && !(stage == 0 && vertex)) || !key || !*key || !sample_trace_key(key, sh->key)) return; + if (!g_sub_sample_notes) g_sub_sample_notes = [NSMutableString string]; + if (g_sub_sample_notes.length >= (bytes ? 262144 : 24576)) return; + [g_sub_sample_notes appendFormat:@"frame=%llu pass=%llu stage=%d shader=%s target=%ux%u fmt=%u ae=%lu/%lu\n", (unsigned long long)atomic_load(&g_cur_frame), (unsigned long long)g_pass.serial, stage, sh->key, R.rt.n && R.rt.color[0] ? R.rt.color[0]->d.width : 0, R.rt.n && R.rt.color[0] ? R.rt.color[0]->d.height : 0, g_pass.cfmt[0], (unsigned long)sh->ae.encodedLength, (unsigned long)sh->ae.alignment]; + if (stage == 0 && bytes) { + for (unsigned stream = 0; stream < 16; stream++) if (S.vbuf[stream].address && S.vbuf[stream].size) { + void *mb; uint64_t off; + [g_sub_sample_notes appendFormat:@" vertex-stream=%u address=%#llx size=%llu stride=%u divisor=%u", stream, (unsigned long long)S.vbuf[stream].address, (unsigned long long)S.vbuf[stream].size, S.stream[stream].stride, S.stream[stream].divisor]; + if (!pend_hits(S.vbuf[stream].address, MIN(S.vbuf[stream].size, 1024ull)) && mtl_addr(S.vbuf[stream].address, 1, &mb, &off)) sample_trace_bytes((__bridge id)mb, off, S.vbuf[stream].size); + [g_sub_sample_notes appendString:@"\n"]; + } + if (g_sample_trace_di) { + const p_draw_idx *di = g_sample_trace_di; void *mb; uint64_t off; + uint64_t n = di->count > 0 && di->itype >= 0 && di->itype <= 2 ? (uint64_t)di->count << di->itype : 0; + [g_sub_sample_notes appendFormat:@" indices=%#llx type=%d count=%d base-vertex=%d base-instance=%d indirect=%#llx", (unsigned long long)di->ibuf, di->itype, di->count, di->base_vertex, di->base_instance, (unsigned long long)di->ind]; + if (n && !pend_hits(di->ibuf, MIN(n, 1024ull)) && mtl_addr(di->ibuf, 1, &mb, &off)) sample_trace_bytes((__bridge id)mb, off, n); + [g_sub_sample_notes appendString:@"\n"]; + } else if (g_sample_trace_dw) [g_sub_sample_notes appendFormat:@" arrays first=%d count=%d base-instance=%d\n", g_sample_trace_dw->first, g_sample_trace_dw->count, g_sample_trace_dw->base_instance]; + } + for (int i = 0; i < sh->rf.nb; i++) { + const nvn_rbind *b = &sh->rf.b[i]; unsigned reg = b->reg; + [g_sub_sample_notes appendFormat:@" kind=%u reg=%u aid=%u encoded=%#llx", b->kind, reg, b->aid, (unsigned long long)slots[b->aid]]; + if (b->kind == NVN_RB_TEX || b->kind == NVN_RB_TEXU) { + int tid = b->kind == NVN_RB_TEXU ? (int)(ubo_handle(stage, reg, b->off) & 0xfffff) : reg < NTEX ? S.tex[stage][reg] & 0xfffff : 0; + if (tid > 0 && tid < g_ntreg && g_treg[tid].ok) { + treg_t *r = &g_treg[tid]; id t = (__bridge id)r->view; + [g_sub_sample_notes appendFormat:@" tid=%d ser=%llu pool=%p off=%#llx view=%p size=%lux%lu mips=%lu fmt=%lu type=%lu usage=%#lx parent=%p level=%lu", tid, (unsigned long long)r->t.serial, r->t.pool, (unsigned long long)r->t.off, (__bridge void *)t, (unsigned long)t.width, (unsigned long)t.height, (unsigned long)t.mipmapLevelCount, (unsigned long)t.pixelFormat, (unsigned long)t.textureType, (unsigned long)t.usage, (__bridge void *)t.parentTexture, (unsigned long)t.parentRelativeLevel]; + } + } else if (b->kind == NVN_RB_SAMP || b->kind == NVN_RB_SAMPT || b->kind == NVN_RB_SAMPU) { + int sid = b->kind == NVN_RB_SAMPU ? ubo_handle(stage, reg, b->off) >> 20 : b->kind == NVN_RB_SAMPT ? (reg < NTEX ? S.tex[stage][reg] >> 20 : 0) : reg < 128 ? S.samp[stage][reg] >> 20 : 0; + if (sid > 0 && sid < g_nsreg && g_sreg_ok[sid]) { samp_par p = g_sreg[sid]; [g_sub_sample_notes appendFormat:@" sid=%d lod=%g..%g bias=%g minmag=%u/%u refs=%lu", sid, p.lod_min, p.lod_max, p.lod_bias, p.minf, p.magf, (unsigned long)g_sub_samplers.count]; } + } else if (b->kind == NVN_RB_C1) { + NSUInteger off; id buffer = legacy_buffer_at(slots[b->aid], &off); + [g_sub_sample_notes appendFormat:@" c1-size=%u buffer=%p length=%lu offset=%lu cpu-readable=%d", sh->rf.c1_len, (__bridge void *)buffer, (unsigned long)buffer.length, (unsigned long)off, buffer.contents != NULL]; + if (bytes) sample_trace_bytes(buffer, off, sh->rf.c1_len); + } else if (b->kind == NVN_RB_CBUF && reg < 16 && (bytes || reg == cbreg)) { + NSUInteger off; id buffer = legacy_buffer_at(slots[b->aid], &off); + [g_sub_sample_notes appendFormat:@" bound-size=%llu buffer=%p length=%lu offset=%lu cpu-readable=%d", (unsigned long long)S.ubo[stage][reg].size, (__bridge void *)buffer, (unsigned long)buffer.length, (unsigned long)off, buffer.contents != NULL]; + if (bytes && !pend_hits(S.ubo[stage][reg].addr, MIN(S.ubo[stage][reg].size, 1024ull))) sample_trace_bytes(buffer, off, S.ubo[stage][reg].size); + if (buffer.contents && cboff <= S.ubo[stage][reg].size && 16 <= S.ubo[stage][reg].size - cboff && off <= buffer.length && cboff <= buffer.length - off && 16 <= buffer.length - off - cboff) { + float v[4]; memcpy(v, (const uint8_t *)buffer.contents + off + cboff, sizeof v); + [g_sub_sample_notes appendFormat:@" float4+%#llx=%g,%g,%g,%g buffer=%p offset=%lu", cboff, v[0], v[1], v[2], v[3], (__bridge void *)buffer, (unsigned long)off]; + } + } + [g_sub_sample_notes appendString:@"\n"]; + } +} +static int encode_args(id en, NvShader *sh, int nvn_stage, int metal_vertex) { AP_B; int r = encode_args_(en, sh, nvn_stage, metal_vertex); AP_E(3); return r; } +static int encode_args_(id en, NvShader *sh, int nvn_stage, int metal_vertex) { + if (!sh->ae) { sh->min_reduction_bound = 0; return 1; } + args_sync_dirty(); + if (!passdump() && !g_args_dirty[nvn_stage] && sh->ab_buf && sh->ab_pass == g_pass.serial && nvn_stage != 5 && !(sh->rf.drv_read[0] && sh->ab_binst != g_draw_binst)) { + bind_args(en, nvn_stage, metal_vertex, sh->ab_buf, sh->ab_gpu, sh->ab_off, sh->rf.abi); + g_stat_args_reuse++; + return 1; + } + NSUInteger arg_len = sh->arg_len ? sh->arg_len : sh->ae.encodedLength; + uint64_t slots[NVN_REFL_MAXARG] = {0}; + #define USE(r, u) do { legacy_use_resource(en, (id)(r), (u), nvn_stage == 5); if (__builtin_expect(g_res_check_on, 0)) res_check((r), sh->key, reg); } while (0) + // DM_NVN_PASSDUMP: on the first new encode per pass per stage, records the shader key (K) and the actual source of each binding (B: UBO address / length / first 4 floats, texture ID and serial, or 「哑」(dummy)) + static uint64_t bl_pass[6]; static int bl_all = -1; if (bl_all < 0) bl_all = getenv("DM_NVN_PASSDUMP_ALL") != NULL; // Record on every new encode (otherwise only the first time per pass per stage) + int bl = passdump() && (bl_all || g_kd_force || bl_pass[nvn_stage] != g_stat_pass); + if (bl) { bl_pass[nvn_stage] = g_stat_pass; PD("K %llu st%d key=%s nb=%d", g_stat_pass, nvn_stage, sh->key[0] ? sh->key : sh->builtin ? sh->builtin : "?", sh->rf.nb); } + pthread_mutex_lock(&g_rmu); // The texture / sampler registration tables may be being grown or rewritten by the game thread + int min_candidate = min_reduction_candidate(sh); min_reduction_binding min_binding = {0}; + int color_diag = color_diag_begin(sh, nvn_stage), color_lines = 0; + for (int i = 0; i < sh->rf.nb; i++) { + const nvn_rbind *b = &sh->rf.b[i]; + int reg = b->reg, aid = b->aid; + if (b->kind == NVN_RB_CBUF) { + const p_bind_buf *u = reg < 16 ? &S.ubo[nvn_stage][reg] : NULL; + void *mb; uint64_t mo, gpu; + if (!u || !u->addr || !mtl_gpu_addr(u->addr, 1, &mb, &mo, &gpu)) { RED_ONCE(16, "阶段 %d cb%d 没有绑定 uniform 缓冲", nvn_stage, reg); continue; } + use_rec("buf", u->addr, u->size, 0, NULL); + if (g_nubd && ubd_hits(u->addr, u->addr + u->size)) { // inline updates deferred in this pass (see mtl_sub_update); ubd_conflict made sure they cover the binding + if (ubd_covers(u->addr, u->addr + u->size)) { + gpu = ubd_shadow(u->addr, u->addr + u->size); + // Argument-buffer pointees need an explicit useResource on Metal 3. + NSUInteger shadow_off; id shadow = legacy_buffer_at(gpu, &shadow_off); + if (!shadow) MDIE("推迟 uniform 的影子缓冲未登记"); + USE(shadow, MTLResourceUsageRead); + } + else RED_ONCE(92, "通道内推迟的 uniform 更新只盖住绑定的一部分(阶段 %d cb%d):读的是更新前的内容", nvn_stage, reg); } + if (nvn_stage == 0 && reg == 0 && g_ui_moff != 0) { uint64_t pg = ui_mat_patch(u, mb, mo); if (pg) gpu = pg; } + if (bl) { + PD("B %llu st%d cb reg%d addr=%#llx size=%llu pool=%#llx+%#llx f=%g,%g,%g,%g", g_stat_pass, nvn_stage, reg, (unsigned long long)u->addr, (unsigned long long)u->size, + (unsigned long long)(u->addr - mo), (unsigned long long)mo, ((float *)u->addr)[0], ((float *)u->addr)[1], ((float *)u->addr)[2], ((float *)u->addr)[3]); + // DM_NVN_CBDUMP=directory: also saves the whole uniform block to disk as-is (F_p_st_cb_
.bin), for the ground-truth side to compare byte by byte by guest address + // DM_NVN_CBDUMP_KEY=shader key: only save for draws using that shader (any stage) + static const char *cbd, *cbk; static int cbd_init; if (!cbd_init) { cbd = getenv("DM_NVN_CBDUMP"); cbk = getenv("DM_NVN_CBDUMP_KEY"); cbd_init = 1; } + if (cbd && (!cbk || !strcmp(sh->key, cbk))) { + char p[1024]; snprintf(p, sizeof p, "%s/F%llu_p%llu_st%d_cb%d_%llx.bin", cbd, (unsigned long long)atomic_load(&g_cur_frame), g_stat_pass, nvn_stage, reg, (unsigned long long)u->addr); + FILE *f = fopen(p, "wb"); if (f) { fwrite((const void *)u->addr, 1, u->size, f); fclose(f); } else RED_ONCE(41, "CBDUMP 写不出 %s", p); + } + } + slots[aid] = gpu; + USE((__bridge id)mb, MTLResourceUsageRead); + // ui_mat_patch and deferred updates can replace the original pool. + // Declare the final indirect pointee as well as its source storage. + NSUInteger final_offset; id final_buffer = legacy_buffer_at(gpu, &final_offset); + if (final_buffer != (__bridge id)mb) USE(final_buffer, MTLResourceUsageRead); + } else if (b->kind == NVN_RB_SAMP || b->kind == NVN_RB_SAMPT || b->kind == NVN_RB_SAMPU) { // SAMPT: the sampler part of a combined handle (texture ID | sampler ID << 20); SAMPU: the same, with the handle in a uniform buffer + int sid = b->kind == NVN_RB_SAMPU ? (int)(ubo_handle(nvn_stage, reg, b->off) >> 20) : + b->kind == NVN_RB_SAMPT ? (reg < NTEX ? (int)(S.tex[nvn_stage][reg] >> 20) : 0) : + reg < 128 ? (int)(S.samp[nvn_stage][reg] >> 20) : 0; + __unsafe_unretained id ss = sid ? (__bridge id)samp_fast(sid) : nil; + if (ss) [g_sub_samplers addObject:ss]; // useResource cannot retain non-MTLResource sampler states. + slots[aid] = sid && sid < g_nsreg && g_srid[sid] ? g_srid[sid] : dummy_samp_rid(); + if (min_candidate) min_reduction_bind_sampler(&min_binding, b, sid, sid > 0 && sid < g_nsreg && g_sreg_ok[sid] ? &g_sreg[sid] : NULL, ss != nil); + geometry_diag_sampler(sh, nvn_stage, b, sid); + if (color_diag && color_lines < 16 && sid > 0 && sid < g_nsreg && g_sreg_ok[sid]) { + const samp_par *sp = &g_sreg[sid]; color_lines++; + MLOG("[color] samp reg=%d sid=%d requested_reduction=%u actual=average minmag=%u/%u lod=%g..%g bias=%g compare=%u/%u", + reg, sid, sp->reduction, sp->minf, sp->magf, sp->lod_min, sp->lod_max, sp->lod_bias, sp->cmp_mode, sp->cmp_func); + } + // Samplers are not MTLResource objects and require no useResource declaration. + if (bl && sid > 0) { int ok = sid < g_nsreg && g_sreg_ok[sid]; samp_par sp = ok ? g_sreg[sid] : (samp_par){0}; // g_rmu is already held in this section + if (ok) PD("B %llu st%d samp reg%d sid=%d mag=%d min=%d(0 最近点 / 1 双线性;2–5 带 mip)wrap=%d,%d,%d border=%g,%g,%g,%g lod=%g..%g bias=%g aniso=%g cmp=%d/%d %s", g_stat_pass, nvn_stage, reg, sid, (int)sp.magf, (int)sp.minf, + (int)sp.ws, (int)sp.wt, (int)sp.wr, sp.border[0], sp.border[1], sp.border[2], sp.border[3], sp.lod_min, sp.lod_max, sp.lod_bias, sp.aniso, (int)sp.cmp_mode, (int)sp.cmp_func, ss ? "实" : "建不出"); } + } else if (b->kind == NVN_RB_TEX || b->kind == NVN_RB_TEXU) { + int tid = b->kind == NVN_RB_TEXU ? (int)(ubo_handle(nvn_stage, reg, b->off) & 0xfffff) : reg < NTEX ? (int)(S.tex[nvn_stage][reg] & 0xfffff) : 0; + void *tv = NULL; uint64_t trid = tid ? tex_fast_rid(tid, &tv) : 0; __unsafe_unretained id t = (__bridge id)tv; + za_diag_use("sample", sh, nvn_stage, reg, tid, t && tid < g_ntreg ? g_treg[tid].t.serial : 0); + if (t && g_fb_n && g_fb_seq == g_draw_seq && nvn_stage == 1) for (int q = 0; q < g_fb_n; q++) if (t == g_fb_src[q]) { t = g_fb_use[q]; trid = g_fb_rid[q]; break; } // same-pass feedback sampling: read the snapshot instead (see fb_prepare) + if (t && tid < g_ntreg) use_rec("tex", (uint64_t)g_treg[tid].t.mem, g_treg[tid].t.L.size, tid, &g_treg[tid].t); + if (t && passdump() && tid < g_ntreg) PD("R %llu st%d ser=%llu", g_stat_pass, nvn_stage, g_treg[tid].t.serial); + if (bl) PD("B %llu st%d tex reg%d tid=%d %s ser=%llu fmt=%lu %lux%lux%lu view_lv=%lu+%lu view_sl=%lu array=%lu target=%lu swz=%d%d%d%d mtl=%p", g_stat_pass, nvn_stage, reg, tid, t ? "实" : "哑", t && tid < g_ntreg ? g_treg[tid].t.serial : 0ull, + (unsigned long)t.pixelFormat, (unsigned long)t.width, (unsigned long)t.height, (unsigned long)t.depth, + (unsigned long)(t.parentTexture ? t.parentRelativeLevel : 0), (unsigned long)t.mipmapLevelCount, + (unsigned long)(t.parentTexture ? t.parentRelativeSlice : 0), (unsigned long)t.arrayLength, (unsigned long)t.textureType, t ? (int)t.swizzle.red : 0, t ? (int)t.swizzle.green : 0, t ? (int)t.swizzle.blue : 0, t ? (int)t.swizzle.alpha : 0, (__bridge void *)(t.parentTexture ? t.parentTexture : t)); + if (t && t.textureType != (MTLTextureType)b->dtt) { uint64_t r2 = 0; id v = tex_fit(tid, t, (MTLTextureType)b->dtt, &r2); if (v) { t = v; trid = r2; } else geometry_diag_type_mismatch(sh, nvn_stage, b, tid, t); } + if (t && nvn_stage == 1 && afr_want(sh)) { uint64_t r2 = 0; id v = tex_alpha_red(tid, t, &r2); if (v) { t = v; trid = r2; } } // see tex_alpha_red + if (color_diag && color_lines < 16) { + const treg_t *tr = tid > 0 && tid < g_ntreg && g_treg[tid].ok ? &g_treg[tid] : NULL; + color_lines++; + MLOG("[color] tex reg=%d tid=%d ser=%llu nvn=%#x requested_view=%#x metal=%lu size=%lux%lu type=%lu/%u levels=%lu+%lu slice=%lu swz=%u,%u,%u,%u dummy=%d", + reg, tid, tr ? (unsigned long long)tr->t.serial : 0, tr ? tr->t.d.format : 0, + tr && tr->has_v && tr->v.set_format ? tr->v.format : 0, (unsigned long)t.pixelFormat, + (unsigned long)t.width, (unsigned long)t.height, (unsigned long)t.textureType, b->dtt, + (unsigned long)(t.parentTexture ? t.parentRelativeLevel : 0), (unsigned long)t.mipmapLevelCount, + (unsigned long)(t.parentTexture ? t.parentRelativeSlice : 0), + t ? (unsigned)t.swizzle.red : 0, t ? (unsigned)t.swizzle.green : 0, t ? (unsigned)t.swizzle.blue : 0, t ? (unsigned)t.swizzle.alpha : 0, t == nil); + } + if (min_candidate) min_reduction_bind_texture(&min_binding, b, tid, t); + if (!t) { t = dummy_tex(b->dtt, b->dpf); trid = g_dummy_tex_rid[b->dtt & 7][b->dpf & 3]; } + slots[aid] = trid; + USE(t, MTLResourceUsageRead); + } else if (b->kind == NVN_RB_IMG) { // Writable texture: NVN image slot r (BindImages) ↔ D3D u_r + int tid = reg < 32 ? (int)(S.img[nvn_stage][reg] & 0xfffff) : 0; + void *tv = NULL; uint64_t trid = tid ? tex_fast_rid(tid, &tv) : 0; __unsafe_unretained id t = (__bridge id)tv; + za_diag_use("image", sh, nvn_stage, reg, tid, t && tid < g_ntreg ? g_treg[tid].t.serial : 0); + if (passdump()) PD("W %llu st%d reg%d tid=%d %s ser=%llu", g_stat_pass, nvn_stage, reg, tid, t ? "实" : "哑", t && tid < g_ntreg ? g_treg[tid].t.serial : 0ull); + if (t && !(t.usage & MTLTextureUsageShaderWrite)) { RED_ONCE(32, "图像槽绑的贴图没有可写用途(贴图号 %d)", tid); t = nil; } + if (!t) { t = dummy_img(b->dtt, b->dpf); trid = g_dummy_img_rid[b->dtt & 7][b->dpf & 3]; } // Unbound: D3D semantics discard the write - write into a dedicated dummy texture so the set that reads 0 is not polluted + slots[aid] = trid; + USE(t, MTLResourceUsageRead | MTLResourceUsageWrite); + } else if (b->kind == NVN_RB_IMGA) { // Image atomics: bind the pool storage behind image slot r as a device pointer. + int tid = reg < 32 ? (int)(S.img[nvn_stage][reg] & 0xfffff) : 0; + if (sh->zero_image_variant) { + if (sh != g_zero_image_shader || !g_zero_image_buffer || nvn_stage != 1 || + aid != sh->zero_image_aid || tid != g_zero_image_tid || + tid <= 0 || tid >= g_ntreg || !g_treg[tid].ok || g_treg[tid].t.serial != g_zero_image_serial || + g_treg[tid].view != (__bridge void *)g_zero_image_texture) + MDIE("[RED] Zero-image destination changed between shadow capture and argument encoding"); + slots[aid] = legacy_buffer_gpu(g_zero_image_buffer); + USE(g_zero_image_buffer, MTLResourceUsageRead | MTLResourceUsageWrite); + continue; + } + const tex_t *tt = tid > 0 && tid < g_ntreg && g_treg[tid].ok ? &g_treg[tid].t : NULL; + void *mb = NULL; uint64_t mo = 0, gpu = 0; + // A linear image and its Metal texture view share the same pool + // words. Block-linear images use a deswizzled private copy, so their + // atomics cannot safely target the guest layout. + int linear = tt && (tt->d.target == 10 || (tt->d.flags & (NVN_TEXTURE_FLAGS_LINEAR | NVN_TEXTURE_FLAGS_LINEAR_RENDER_TARGET))); + int za_shadow = tt && !linear && !strcmp(sh->key, "2bd5037003980716_b"); + if (tt && !linear && !za_shadow) RED_ONCE(93, "image atomics on a block-linear texture (texture ID %d, target %d): writing to a dummy buffer", tid, (int)tt->d.target); + if (linear && tt->mem && mtl_gpu_addr((uint64_t)tt->mem, tt->L.size ? tt->L.size : 4, &mb, &mo, &gpu)) { + slots[aid] = gpu; + USE((__bridge id)mb, MTLResourceUsageRead | MTLResourceUsageWrite); + static int nlog; + if (nlog < 8 && !(sh->logged & 2)) { + sh->logged |= 2; nlog++; + MLOG("shader %s (stage %d): image atomics on image slot %d, texture ID %d (target %d, %llu bytes)", sh->key, nvn_stage, reg, tid, (int)tt->d.target, (unsigned long long)tt->L.size); + } + } else { + size_t need = 64u << 10; + if (tt) { + size_t w = MAX((size_t)tt->d.width, 1), h = MAX((size_t)tt->d.height, 1), d = MAX((size_t)tt->d.depth, 1); + size_t logical = w <= SIZE_MAX / h ? w * h : SIZE_MAX; + logical = logical <= SIZE_MAX / d ? logical * d : SIZE_MAX; + logical = logical <= SIZE_MAX / 4 ? logical * 4 : SIZE_MAX; + need = MAX((size_t)tt->L.size, logical); + } + id sink = za_shadow ? za_atomic_shadow(need) : nil; + int using_shadow = sink != nil; + if (!sink) sink = imga_dummy(need); + slots[aid] = legacy_buffer_gpu(sink); + USE(sink, MTLResourceUsageRead | MTLResourceUsageWrite); + if (using_shadow) { + void *tv = NULL; (void)tex_fast_rid(tid, &tv); + za_atomic_schedule(sink, (__bridge id)tv); + } + } + if (bl) PD("B %llu st%d imga reg%d tid=%d %s", g_stat_pass, nvn_stage, reg, tid, linear && tt->mem ? "real" : "dummy"); + } else if (b->kind == NVN_RB_TBUF) { // Typed buffer: the buffer texture (target BUFFER) in texture slot r + int tid = reg < NTEX ? (int)(S.tex[nvn_stage][reg] & 0xfffff) : 0; + void *tv = NULL; uint64_t trid = tid ? tex_fast_rid(tid, &tv) : 0; __unsafe_unretained id t = (__bridge id)tv; + if (t && t.textureType != MTLTextureTypeTextureBuffer) { RED_ONCE(40, "类型化缓冲槽绑的不是缓冲贴图(贴图号 %d 类型 %lu)", tid, (unsigned long)t.textureType); t = nil; } + if (bl) PD("B %llu st%d tbuf reg%d tid=%d %s ser=%llu", g_stat_pass, nvn_stage, reg, tid, t ? "实" : "哑", t && tid < g_ntreg ? g_treg[tid].t.serial : 0ull); + if (!t) { t = dummy_tbuf(b->dpf); trid = g_dummy_tbuf_rid[b->dpf & 3]; } + slots[aid] = trid; + USE(t, MTLResourceUsageRead); + } else if (b->kind == NVN_RB_SBUFP || b->kind == NVN_RB_UBOP) { // B: storage buffers / compute-stage uniforms are device pointers; the address is taken by NVN slot + const p_bind_buf *u = reg < 16 ? (b->kind == NVN_RB_UBOP ? &S.ubo[nvn_stage][reg] : &S.ssbo[nvn_stage][reg]) : NULL; + void *mb; uint64_t mo, gpu; + if (u && u->addr && mtl_gpu_addr(u->addr, u->size ? u->size : 1, &mb, &mo, &gpu)) { use_rec("buf", u->addr, u->size, 0, NULL); slots[aid] = gpu; USE((__bridge id)mb, b->rw ? MTLResourceUsageRead | MTLResourceUsageWrite : MTLResourceUsageRead); if (nvn_stage == 5 && b->kind == NVN_RB_SBUFP) pend_add(u->addr, u->size ? u->size : ((__bridge id)mb).length - mo); } + else { slots[aid] = dummy_buf_gpu(); USE(dummy_buf(), MTLResourceUsageRead); } + if (bl) PD("B %llu st%d sbufp reg%d %s addr=%#llx size=%llu %s", g_stat_pass, nvn_stage, reg, u && u->addr ? "实" : "哑", u ? (unsigned long long)u->addr : 0ull, u ? (unsigned long long)u->size : 0ull, b->rw ? "rw" : "ro"); + } else if (b->kind == NVN_RB_C1) { // B: the shader's own constant segment, built when the shader is fetched + id cb = sh->c1buf ? sh->c1buf : zero_bank(); // c1 has length 0 (600 of them in the whole library) yet the shader still declares it: reads 0 + slots[aid] = (sh->c1buf ? sh->c1_gpu : zero_bank_gpu()) + (sh->c1buf ? sh->c1off : 0); + USE(cb, MTLResourceUsageRead); + } else if (b->kind == NVN_RB_SUPPORT) { // support_buffer: scale 1.0, BGRA swap 0, viewport transform not disabled (viewport fields are not read) + slots[aid] = support_gpu(); + USE(support_buf(), MTLResourceUsageRead); + } else if (b->kind == NVN_RB_DRV0 || b->kind == NVN_RB_DRV2) { + // NVN driver constant segments: those proven unread by static analysis at packaging time are supplied as 0; those read are filled on the spot from the current bindings - c0 = storage buffer table (base + 0x10 r: address 64 bits, size 32 bits), + // c2 = texture handle table (0x168 + 8 r) and sampler handle table (0x568 + 8 r), the values being the NVN handles the game gave at bind time; positions read outside the tables are registered at packaging time and reported a failure at load time + int bank = b->kind == NVN_RB_DRV2 ? 2 : 0; + if (!sh->rf.drv_read[bank]) { slots[aid] = zero_bank_gpu(); USE(zero_bank(), MTLResourceUsageRead); } + else { + int gpu_binst = bank == 0 && nvn_stage == 0 && g_indirect_drv0_buf != nil; + id db; size_t doff; + if (gpu_binst) { db = g_indirect_drv0_buf; doff = g_indirect_drv0_off; } + else doff = arena_alloc(DRV_SIZE, &db); + uint8_t *d = (uint8_t *)db.contents + doff; + // A lookahead conversion can already be running after sub_cut. + // Preserve the GPU-owned word while populating this draw's c0. + if (gpu_binst) { memset(d, 0, 0x644); memset(d + 0x648, 0, DRV_SIZE - 0x648); } + else memset(d, 0, DRV_SIZE); + if (bank == 0) { + int base = c0_ssbo_base(nvn_stage); + if (base < 0) RED_ONCE(45, "阶段 %d 的着色器读驱动段 c0,存储缓冲表基址未实测", nvn_stage); + else for (int r = 0; r < 16; r++) { uint64_t a = S.ssbo[nvn_stage][r].addr; uint32_t n = (uint32_t)S.ssbo[nvn_stage][r].size; memcpy(d + base + 16 * r, &a, 8); memcpy(d + base + 16 * r + 8, &n, 4); } + // Compute also has the dispatch size at 0x200 (x, y, z) and the uniform buffer table at 0x210 + 16 r (address 64 bits, size 32 bits), ending where the storage buffer table + // starts (0x310). Measured on Link's Awakening's light culling (light_culling_compute.bfsha): tile index = workgroup y × c0[0x200] + workgroup x, and c0[0x280] is + // the address of uniform buffer 7 (the light parameters, 144 bytes per light); Breath of the Wild's compute shaders read 0x280 / 0x290 / 0x2d0 of the same table + // Graphics driver constant: maximum point size (upstream ca7216ceb89c). + if (nvn_stage != 5) { const float max_point = 2047.0f; memcpy(d + 0x610, &max_point, 4); if (!gpu_binst) memcpy(d + 0x644, &g_draw_binst, 4); } + if (nvn_stage == 5) { + memcpy(d + 0x200, g_dispatch_dim, 12); + for (int r = 0; r < 16; r++) { uint64_t a = S.ubo[5][r].addr; uint32_t n = (uint32_t)S.ubo[5][r].size; memcpy(d + 0x210 + 16 * r, &a, 8); memcpy(d + 0x210 + 16 * r + 8, &n, 4); } + } + } else { + const uint64_t *tx = S.tex[nvn_stage], *im = S.img[nvn_stage]; + // Combined slots 32-40 overlap the image words. A bound + // image wins at the overlapping driver offset. + for (int r = 0; r < C2_TEX_SLOTS; r++) { + if (r >= 32 && (!tx[r] || (r - 32 < C2_IMG_SLOTS && im[r - 32]))) continue; + uint32_t th = (uint32_t)tx[r]; memcpy(d + C2_TEX + 8 * r, &th, 4); + } + for (int r = 0; r < C2_IMG_SLOTS; r++) { + uint32_t ih = (uint32_t)im[r]; memcpy(d + C2_IMG + 8 * r, &ih, 4); + } + int sep_bound = 0; + for (int r = 0; r < 128; r++) { + uint32_t th = (uint32_t)tx[NVN_SEPTEX_SLOT0 + r]; + uint32_t sh_ = (uint32_t)S.samp[nvn_stage][r]; + sep_bound += th != 0; + memcpy(d + C2_SEPTEX + 8 * r, &th, 4); + memcpy(d + C2_SAMP + 8 * r, &sh_, 4); + } + if (sep_bound) { static int once; if (!once++) MLOG("unified NVN driver c2: populated %d separate texture handles for stage %d", sep_bound, nvn_stage); } + } + slots[aid] = gpu_binst ? g_indirect_drv0_gpu : g_cur_arena->gpu + doff; + USE(db, MTLResourceUsageRead); + } + } else RED_ONCE(46, "绑定种类 %d 未实现", b->kind); + } + sample_trace_args(sh, nvn_stage, slots); + if (min_candidate) min_reduction_binding_finish(sh, &min_binding); + #undef USE + pthread_mutex_unlock(&g_rmu); + NSUInteger argument_alignment = g_legacy_direct_arguments ? 256 : MAX((NSUInteger)256, sh->ae.alignment); + if (argument_alignment > SIZE_MAX - arg_len) MDIE("[RED] Shader %s argument allocation overflows", sh->key); + id ab; size_t off = arena_alloc(arg_len + (argument_alignment > 256 ? argument_alignment - 1 : 0), &ab); + off += (argument_alignment - off % argument_alignment) % argument_alignment; + if (g_legacy_direct_arguments) { + if (arg_len > sizeof slots) MDIE("[RED] Shader %s argument block exceeds slot capacity", sh->key); + memcpy((uint8_t *)ab.contents + off, slots, arg_len); + } else { + char failure[256]; + if (!nvn_argument_encode(sh->ae, ab, off, &sh->rf, slots, NVN_REFL_MAXARG, failure, sizeof failure)) { + MLOG("[RED] Shader %s stage %d private argument encoding failed: %s", sh->key, nvn_stage, failure); + return 0; + } + } + bind_args(en, nvn_stage, metal_vertex, ab, g_cur_arena->gpu + off, off, sh->rf.abi); + sample_trace_root(sh, nvn_stage, ab, off, arg_len); + sh->ab_buf = ab; sh->ab_gpu = g_cur_arena->gpu + off; sh->ab_off = off; sh->ab_pass = g_pass.serial; sh->ab_binst = g_draw_binst; g_args_dirty[nvn_stage] = 0; + g_stat_args_enc++; + return 1; +} +static id g_quad_ib; static uint64_t g_quad_gpu; +static id g_fan_ib; static uint64_t g_fan_gpu; // non-indexed triangle fans: shared indices (0, i, i+1), base vertex = first +static id rp_begin(void); +// ---------- Pass lookahead: does this pass use depth / stencil ---------- +// When opening a pass, scan forward from the op after the current draw, simulating the changes of depth-stencil state, up to where this pass must end (target reset, copy, compute dispatch - +// matching one-to-one the ops that end a pass in the replay). If no draw in between enables depth test / depth write / stencil test, this pass may leave the depth attachment unattached: +// not one byte of the depth content is touched, and on TBDR the load and store of the whole depth texture are saved (measured on a real device: a single-draw fullscreen pass takes half the GPU, with an extra D32S8 load and store attached). +// The scan follows real replay order across sub-list returns and across the remaining handles of this submission; end of submission = the pass must end. Undecidable cases such as a mid-pass clear or too-deep nesting always count as 'used'. +static const uint8_t *g_exec_next; // The op following the current draw in the replay +static const uint8_t *g_ret[16]; static int g_ret_n; // Call return stack of the replay (at OP_CALL the caller's next op is pushed) +static const uint64_t *g_rest; static int g_nrest; // Handles in this submission still to be replayed after the current handle +void mtl_exec_rest(const uint64_t *h, int n) { g_rest = h; g_nrest = n; } +static uint64_t g_stat_ds_omit; // Number of passes that attach less depth-stencil because the lookahead proved it unused (including stencil-only / depth-only) +static uint64_t g_stat_s_only, g_stat_d_only; // Of those, the number of passes that attach only stencil / only depth +// Clear-only pass hoist (hoist_try): ops that were already executed early and must be skipped when replay reaches their original position (each group's SetRenderTargets and the clears in it). +// The exec loop, the store lookahead (sdx index) and the depth-stencil lookahead (ds_scan) all skip by it - otherwise the lookahead would take the clear at the original position as 'cleared again' and wrongly judge the early-cleared content redundant +#define HOIST_MAX 40 // At most 4 groups × (1 set-target op + 8 clears) +static const uint8_t *g_hoist_skip[HOIST_MAX]; static int g_hoist_n; static uint64_t g_hoist_groups; +static int hoist_skipped(const uint8_t *p) { for (int i = 0; i < g_hoist_n; i++) if (g_hoist_skip[i] == p) return 1; return 0; } +typedef struct { const uint8_t *p, *ret[8]; int ret_n, rest_i, bad; } fz_cursor; +static int fz_next(fz_cursor *c, const uint8_t **out, op_hdr *oh, const void **oa) { + for (int guard = 0; guard < 1000000; guard++) { + if (!c->p) return 0; + op_hdr h; memcpy(&h, c->p, sizeof h); const void *a = c->p + 8; + switch (h.op) { + case OP_END: + if (c->ret_n) c->p = c->ret[--c->ret_n]; + else if (c->rest_i < g_nrest) c->p = (const uint8_t *)g_rest[c->rest_i++]; + else { c->p = NULL; return 0; } + continue; + case OP_JUMP: memcpy(&c->p, a, 8); continue; + case OP_CALL: { uint64_t t; memcpy(&t, a, 8); if (c->ret_n >= 8) { c->bad = 1; return 0; } + c->ret[c->ret_n++] = c->p + h.size; c->p = (const uint8_t *)t; continue; } + default: break; + } + const uint8_t *p = c->p; c->p += h.size; + if (g_hoist_n && hoist_skipped(p)) continue; + if (out) *out = p; if (oh) *oh = h; if (oa) *oa = a; return 1; + } + c->bad = 1; return 0; +} +// State of the readable-tile pass merge (merge B step 2): the earlier pass (phase 1) and the later pass (phase 2) share one render pass, and the attachments are the union +typedef struct { + int active, phase, n1, nskip; + int nreuse; + const uint8_t *rt2_op, *reuse[8], *skip[12]; + p_set_rt rt1, rt2; // Logical targets of each of the two passes (R.rt is the union during the merge) + int8_t p2map[8]; // Logical color number of the later pass → physical attachment + uint8_t lmap1[8], lmap2[8]; // Physical attachment → logical color number of this phase's draws (0xff = not written) + uint8_t clr2[8], cd2f, cs2f; MTLClearColor cc2[8]; double cd2; uint32_t cs2; // Clears at the start of the later pass (used as-is when the in-run dual render redraws) + uint8_t wrote[8]; // Physical attachments written by the later pass + uint8_t cov2[8]; // Physical attachments covered by the first draw of the later pass +} fuse_t; +static fuse_t g_fz; static uint64_t g_fz_n, g_fz_draws, g_fz_fallback; +typedef struct { int idx; p_draw dw; p_draw_idx di; p_scissor sc; int has_sc; void *S_; uint8_t pm[8]; } defer_snap; // pm: group P target index → logical color number of this draw (identity for its own draws; a composed mapping for absorbed upstream groups) +typedef struct { + int active, at_q, in_p, n, reason, reason_op, logged, reset_between; uint64_t born; // born: creation serial (replay is ordered by it, not by slot) + p_set_rt p_rt, q_rt; + const uint8_t *p_end_op, *q_op; + uint8_t clear_c[8], lmap[8]; + MTLClearColor cc[8]; + uint8_t clear_d, clear_s; + double cd; uint32_t cs; + uint64_t p_reads[512]; int nreads, p_dw, p_st; + defer_snap snap[64]; +} defer_t; +static defer_t g_defer_slots[4]; +static defer_t *g_defer_cur = &g_defer_slots[0]; +#define g_defer (*g_defer_cur) +static int sub_eq(const p_set_rt *a, int ka, const p_set_rt *b, int kb); +static uint64_t g_defer_n, g_defer_draws, g_defer_fallbacks; +static const uint8_t *g_defer_pend; // The P-range end op of the group that is in its P range (NULL if none): the exec loop compares each op against just this one pointer +static int g_defer_nact, g_defer_natq; // Number of active groups, number of groups that reached Q and await replay (the hot path only looks at the counts, no per-slot scan) +static int g_defer_ab = -1, g_defer_ab_on; +static void draw(const p_draw *dw, const p_draw_idx *di); +static int defer_has_in_p(void) { return g_defer_pend != NULL; } +static int defer_has_active(void) { return g_defer_nact > 0; } +static int defer_slot_free(void) { for (int i = 0; i < 4; i++) if (!g_defer_slots[i].active) return i; return -1; } +static int defer_same_q_depth(const defer_t *a, const p_set_rt *q) { + if (!a->q_rt.depth || !q->depth) return 1; + return sub_eq(&a->q_rt, 8, q, 8); +} +static int defer_next(int at_q_only, uint64_t after) { // Slot number of the next active group (smallest creation serial that is > after); returns −1 if none + int best = -1; for (int i = 0; i < 4; i++) { const defer_t *d = &g_defer_slots[i]; if (!d->active || (at_q_only && !d->at_q) || d->born <= after) continue; if (best < 0 || d->born < g_defer_slots[best].born) best = i; } + return best; +} +static void defer_release_one(defer_t *d) { if (d->active) { g_defer_nact--; if (d->at_q) g_defer_natq--; if (d->in_p) g_defer_pend = NULL; } for (int i = 0; i < d->n; i++) free(d->snap[i].S_); memset(d, 0, sizeof *d); } +static void defer_clear_all(void) { for (int i = 0; i < 4; i++) defer_release_one(&g_defer_slots[i]); g_defer_nact = g_defer_natq = 0; g_defer_pend = NULL; } +static int hoist_take(const uint8_t *p) { for (int i = 0; i < g_hoist_n; i++) if (g_hoist_skip[i] == p) { g_hoist_skip[i] = g_hoist_skip[--g_hoist_n]; return 1; } return 0; } +enum { DS_FREE = 0, DS_USED = 1, DS_RET = 2 }; +static int g_ds_why; static int g_ds_scanned; static int g_ds_need; // g_ds_need: need accumulated during the scan, 1 depth 2 stencil // Diagnostics: the opcode that triggered the 'used' verdict (for a draw, records 1000 + dt*100 + dw*10 + st), and the number of ops scanned +static int ds_scan(const uint8_t *p, ds_st *ds, int depth) { + for (int guard = 0; guard < 1000000; guard++) { + op_hdr h; memcpy(&h, p, sizeof h); + const void *a = p + 8; g_ds_scanned++; + if (g_hoist_n && hoist_skipped(p)) { p += h.size; continue; } // Clear pass already executed early + switch (h.op) { + case OP_END: return DS_RET; + case OP_JUMP: { const uint8_t *t; memcpy(&t, a, 8); p = t; continue; } + case OP_CALL: { uint64_t t; memcpy(&t, a, 8); if (depth >= 8) return DS_USED; + int r = ds_scan((const uint8_t *)t, ds, depth + 1); if (r != DS_RET) return r; break; } + case OP_SET_RT: case OP_COPY_B2B: case OP_CLEAR_BUF: case OP_UPDATE_UB: case OP_COPY_B2T: case OP_COPY_T2B: case OP_COPY_T2T: case OP_CLEAR_TEX: case OP_DISPATCH: return DS_FREE; + case OP_CLEAR_COLOR: case OP_CLEAR_DS: g_ds_why = h.op; return DS_USED; + case OP_BIND_STATE: { const p_bind_state *b = a; if (b->kind == 5) memcpy(ds, b->bytes, sizeof *ds); break; } + case OP_DRAW: case OP_DRAW_IDX: // Depth test / write → depth needed; stencil test → stencil needed; can stop once both are needed + g_ds_need |= (ds->dt || ds->dw ? 1 : 0) | (ds->st ? 2 : 0); + if (g_ds_need == 3) { g_ds_why = 1000 + ds->dt * 100 + ds->dw * 10 + ds->st; return DS_USED; } break; + default: break; + } + p += h.size; + } + return DS_USED; +} +// Store lookahead decision (command by command, in real replay order): +// Redundant: after being set as an attachment again and before any draw, it is cleared in full (color: all four channels of that index written; depth: depth clear; stencil: all 8 stencil bits cleared. Depth and stencil are decided separately and their write-back is set separately) +// Needed: at a draw / dispatch it is bound to a texture or image slot of any stage (including ones already bound when the scan starts); used as copy source / copy destination / texture-clear target; drawn before cleared after becoming an attachment again (load) +// Different levels / layers of the same texture are decided separately (a downsample chain writes level k and reads level k−1); a sampler binding counts as a read of the whole texture (conservative) +static const uint8_t *g_exec_cur; // The op currently being replayed (inclusive) +static void sd_need(sd_att *t, int why) { if (!t->state) { t->state = 1; t->why = (uint8_t)why; } } // why: 1 a draw reads it as a texture 2 as an attachment, drawn before cleared 3 copy 4 texture clear 5 scanned to the end undecided 6 no start point / index unavailable +static int sdx_decide(uint8_t st[9]); // Store lookahead decision (for the index see mtl_sub_index) +static void sd_finish(void) { + if (!g_nsd) return; + if (!tbdr_on()) { // All optimizations off: always write back + for (int i = 0; i < g_nsd; i++) { sd_att *t = &g_sd[i]; + if (t->depth == 1) [g_enc setDepthStoreAction:MTLStoreActionStore]; else if (t->depth == 2) [g_enc setStencilStoreAction:MTLStoreActionStore]; else [g_enc setColorStoreAction:MTLStoreActionStore atIndex:(NSUInteger)t->idx]; } + g_nsd = 0; return; } + uint8_t nst[9]; + if (sdx_decide(nst)) { for (int i = 0; i < g_nsd; i++) if (nst[i] == 2) g_sd[i].state = 2; else sd_need(&g_sd[i], nst[i] >> 2); } + else for (int i = 0; i < g_nsd; i++) sd_need(&g_sd[i], 6); // Pass ended outside the list / index unavailable: always write back + for (int i = 0; i < g_nsd; i++) { + sd_att *t = &g_sd[i]; int udc_dead = udc_on() && t->load && !t->wrote; + // A color attachment written in the same pass as the window texture does not enter the ledger: it is a link in the present chain, not a transient attachment that is 'reset every frame before use'. + // The in-run 'upscale' pass writes two textures at once - scene texture + window texture - and the UI composite was merged into the same pass, so in-run nobody reads back the scene texture; as soon as the game stops drawing 3D (loading screen) + // a separate composite pass samples it afterwards instead. 'About to enter the loading screen' is only known after the previous frame was submitted, so prediction cannot stop it: on iPad true fullscreen every install hits it once on the first act change + // (the 2732×2048 attachment of frame N was not written back and is sampled for 4 consecutive frames starting at frame N+1; the key is permanently disabled). At the iPhone aspect ratio the same-size key already sees a reader during the trial period and is silently disabled. + int xv_win = 0; for (int k = 0; k < R.rt.n && k < 8; k++) if (R.rt.color[k] && xv_present(R.rt.color[k]->serial)) xv_win = 1; + uint64_t xtag = 0; int xvcand = xv_on() && !xv_win && t->depth == 0 && t->state != 2 && t->why == 5 && xv_first_good(t->ser, t->lv, t->sl) && !xv_present(t->ser) + && !xv_disabled(xtag = xv_tag_pass(R.rt.color[t->idx], t->lv, t->idx, &R.rt)); + int xvdead = xvcand && xv_trial_get(xtag, 0) >= XV_TRIAL; // Only after the trial period is the write-back really skipped + if (xvcand && !xvdead) xv_set(t->ser, t->lv, t->sl, XV_PROBE, xtag); // Trial period: write back as usual, only track the next touch + int dead = t->state == 2 || udc_dead || xvdead; MTLStoreAction sa = dead ? MTLStoreActionDontCare : MTLStoreActionStore; + if (xvdead) { xv_set(t->ser, t->lv, t->sl, XV_INVALID, xtag); g_xv_dc++; } + if (t->depth == 1) [g_enc setDepthStoreAction:sa]; else if (t->depth == 2) [g_enc setStencilStoreAction:sa]; else [g_enc setColorStoreAction:sa atIndex:(NSUInteger)t->idx]; + g_sd_n_all++; if (dead) { g_sd_n_dead++; g_sd_dead_bytes += t->bytes; g_bw_store -= t->bytes; if (udc_dead) g_udc_saved_bytes += t->bytes; } + if (!t->state) t->why = 5; + PD("Z %llu %s ser=%llu lv%u sl%u %s why=%d by=%s slot=%d", g_stat_pass, t->depth == 1 ? "d" : t->depth == 2 ? "s" : "c", (unsigned long long)t->ser, t->lv, t->sl, xvdead ? "dontcare-xv" : dead ? "dontcare" : "store", dead ? 0 : t->why, t->why == 1 && t->wkey ? t->wkey : "-", t->wslot); + if (udc_on()) PD("U %llu %s ser=%llu lv%u sl%u runtime=%s reason=%s state=%d load=%d", g_stat_pass, t->depth == 1 ? "d" : t->depth == 2 ? "s" : "c", (unsigned long long)t->ser, t->lv, t->sl, t->wrote ? "写过" : "没写", t->wwhy == 1 ? "图像写" : t->wwhy == 2 ? "颜色写掩码" : t->wwhy == 3 ? "片元输出" : t->wwhy == 4 ? "深度写" : t->wwhy == 5 ? "模板写" : "无", t->state, t->load); + } + g_nsd = 0; +} +// Continue scanning in real replay order: current list -> callers at each level (return stack from the top down) -> remaining handles of this submit; the render pass necessarily ends when the submit ends (rp_flush before commit) +// Coverage proof (holds only if all are satisfied; anything unrecognized is treated as not holding): +// Shader: the vertex position xyz is taken as-is from one vertex attribute, w is 1 or the same attribute's .w (dmsh_build raster_facts); the fragment does not discard, does not write sample_mask, and writes this color index +// State: this attachment has no blending, a full write mask, no alpha-to-coverage, fill mode, no depth test (or ALWAYS), no stencil test, single sample, not 3D +// Vertices: non-indexed or 16 / 32-bit indexed draws, with indices and positions both read from CPU-visible pool memory; the scissor / viewport / geometry / winding / depth clipping decisions are in nvn_cover.h (pure functions, with unit tests) +// Read out attribute a of all vertices of this draw (non-indexed or 16 / 32-bit indexed; vertex number = first + k or index value + base_vertex), with missing components filled with 0 / 1 per the fetch rules. +// Returns 0 on success, a reason code on failure (6 vertex count / index type, 7 attribute index, 8 format / stream, 9 out of bounds, 10 address) +static int attr_read(int a, const p_draw *dw, const p_draw_idx *di, float P[64][4]) { + int n = dw ? dw->count : di->count; + if (n < 1 || n > 64 || (dw && dw->first < 0) || (di && (di->itype < 1 || di->itype > 2))) return 6; + if (a < 0 || a >= S.nattr || S.attr[a].stream >= 16) return 7; + va_st at = S.attr[a]; vs_st sm = S.stream[at.stream]; NVNbufferRange vb = S.vbuf[at.stream]; + MTLVertexFormat vf = vfmt(at.fmt); int nc = vf == MTLVertexFormatFloat2 ? 2 : vf == MTLVertexFormatFloat3 ? 3 : vf == MTLVertexFormatFloat4 ? 4 : 0; + if (!nc || !sm.stride || sm.divisor || !vb.address) return 8; + const uint8_t *ib = NULL; int isz = di ? (di->itype == 1 ? 2 : 4) : 0; + if (di && pend_hits(di->ibuf, (uint64_t)n * isz)) return 10; // indices written by a copy / update earlier in this submission: the CPU still sees the old ones + if (di) { void *m; uint64_t o; if (!mtl_addr(di->ibuf, (uint64_t)n * isz, &m, &o) || !(ib = [(__bridge id)m contents])) return 10; ib += o; } + for (int k = 0; k < n; k++) { + int64_t vid = dw ? (int64_t)dw->first + k : (isz == 2 ? (int64_t)((const uint16_t *)ib)[k] : (int64_t)((const uint32_t *)ib)[k]) + di->base_vertex; + uint64_t off = (uint64_t)vid * sm.stride + at.offset; + if (vid < 0 || off + 4 * nc > vb.size) return 9; + if (pend_hits(vb.address + off, 4 * nc)) return 10; // position written on the GPU earlier in this submission + void *mb; uint64_t mo; const uint8_t *base; + if (!mtl_addr(vb.address + off, 4 * nc, &mb, &mo) || !(base = [(__bridge id)mb contents])) return 10; + float v[4] = {0, 0, 0, 1}; memcpy(v, base + mo, 4 * nc); memcpy(P[k], v, sizeof v); + } + return 0; +} +static int first_draw_covers(int i, id t, NSUInteger level) { + const p_draw *dw = g_fd_dw; const p_draw_idx *di = g_fd_di; + int n = dw ? dw->count : di->count, prim = dw ? dw->prim : di->prim; + __unsafe_unretained NvProg *pr = S.prog ? prog_get(S.prog) : nil; if (!pr) return g_fd_why = 1, 0; + NvShader *vs = pr->sh[0], *fs = pr->sh[1]; + if (!vs || !fs || vs->rf.pos_attr < 0 || fs->rf.may_discard || fs->rf.writes_smask || !(fs->rf.color_mask >> i & 1)) return g_fd_why = 2, 0; + if (!t || t.sampleCount != 1 || t.textureType == MTLTextureType3D) return g_fd_why = 3, 0; + if ((S.color.blend_en >> i & 1) || ((S.chmask.mask >> (4 * i)) & 0xf) != 0xf || S.ms.a2c || S.poly.mode != NVN_POLYGON_MODE_FILL) return g_fd_why = 4, 0; + if (R.rt.depth && ((S.ds.dt && S.ds.df != NVN_DEPTH_FUNC_ALWAYS) || S.ds.st)) return g_fd_why = 5, 0; + if (n < 3 || n > 64) return g_fd_why = 6, 0; + float P[64][4]; int why = attr_read(vs->rf.pos_attr, dw, di, P); + if (why) return g_fd_why = why, 0; + if (vs->rf.pos_w1) for (int k = 0; k < n; k++) P[k][3] = 1.0f; // Position w is the constant 1 (the attribute's .w is not taken) + int sc[4] = {R.sc.x, R.sc.y, R.sc.w, R.sc.h}; float vp[4] = {S.vp[0][0], S.vp[0][1], S.vp[0][2], S.vp[0][3]}; + g_fd_why = 11; + return nvn_cover((const float (*)[4])P, n, prim, (int)S.poly.cull, S.poly.front == NVN_FRONT_FACE_CCW, + S.has_vp, vp, R.has_sc, sc, (long)MAX(t.width >> level, 1), (long)MAX(t.height >> level, 1)); +} + +static int state_op(uint32_t op, const void *a) { + p_bind_handles one; + if (op == OP_BIND_TEX1) { a = bind_rec(op, a, &one); op = OP_BIND_TEX; } + switch (op) { + case OP_SCISSOR: R.sc = *(const p_scissor *)a; R.has_sc = 1; break; + case OP_BIND_PROGRAM: { const p_bind_program *bp_ = a; const prog_t *np = bind_stages(bp_->prog, bp_->stages); if (np != S.prog) { S.prog = np; g_pso_dirty = 1; args_dirty_all(); } break; } + case OP_BIND_STATE: { + const p_bind_state *b = a; void *dst = NULL; size_t n = 0; + blend_st bs; + switch (b->kind) { + case 0: memcpy(&bs, b->bytes, sizeof bs); dst = &S.blend[bs.target]; n = sizeof bs; break; + case 1: dst = &S.color; n = sizeof S.color; break; + case 2: dst = &S.chmask; n = sizeof S.chmask; break; + case 3: dst = &S.ms; n = sizeof S.ms; break; + case 4: dst = &S.poly; n = sizeof S.poly; break; + case 5: dst = &S.ds; n = sizeof S.ds; break; + } + if (dst && memcmp(dst, b->bytes, n)) { memcpy(dst, b->bytes, n); if (b->kind == 5) g_dss_dirty = 1; else g_pso_dirty = 1; } + break; + } + case OP_BIND_VATTR: { const p_bind_vattr *b = a; if (S.nattr != b->n || memcmp(S.attr, b->st, 4 * b->n)) { memcpy(S.attr, b->st, 4 * b->n); S.nattr = b->n; g_pso_dirty = 1; } break; } + case OP_BIND_VSTREAM: { const p_bind_vstream *b = a; if (S.nstream != b->n || memcmp(S.stream, b->st, 8 * b->n)) { memcpy(S.stream, b->st, 8 * b->n); S.nstream = b->n; g_pso_dirty = 1; } break; } + case OP_BIND_VBUF: { const p_bind_vbuf *b = a; memcpy(&S.vbuf[b->first], b->r, sizeof *b->r * b->n); break; } + case OP_BIND_UBO: { const p_bind_buf *b = a; if (b->stage < 6 && b->index < 16 && memcmp(&S.ubo[b->stage][b->index], b, sizeof *b)) { S.ubo[b->stage][b->index] = *b; g_args_dirty[b->stage] = 1; } break; } + case OP_BIND_SSBO: { const p_bind_buf *b = a; if (b->stage < 6 && b->index < 16 && memcmp(&S.ssbo[b->stage][b->index], b, sizeof *b)) { S.ssbo[b->stage][b->index] = *b; g_args_dirty[b->stage] = 1; } break; } + case OP_BIND_TEX: { const p_bind_handles *b = a; bh_fresh(b); if (b->stage < 6 && b->first + b->n <= NTEX && memcmp(&S.tex[b->stage][b->first], b->h, 8 * b->n)) { memcpy(&S.tex[b->stage][b->first], b->h, 8 * b->n); g_args_dirty[b->stage] = 1; if (b->first + b->n > NVN_SEPTEX_SLOT0) { static int once; if (!once++) MLOG("unified NVN texture ABI: accepted separate texture slots %d..%d for stage %d", b->first, b->first + b->n - 1, b->stage); } } break; } + case OP_BIND_SAMP: { const p_bind_handles *b = a; bh_fresh(b); if (b->stage < 6 && b->first + b->n <= 128 && memcmp(&S.samp[b->stage][b->first], b->h, 8 * b->n)) { memcpy(&S.samp[b->stage][b->first], b->h, 8 * b->n); g_args_dirty[b->stage] = 1; } break; } + case OP_BIND_IMG: { const p_bind_handles *b = a; bh_fresh(b); if (b->stage < 6 && b->first >= 0 && b->first + b->n <= 32) { if (memcmp(&S.img[b->stage][b->first], b->h, 8 * b->n)) { memcpy(&S.img[b->stage][b->first], b->h, 8 * b->n); g_args_dirty[b->stage] = 1; } } else RED_ONCE(31, "图像槽越界 阶段 %d 起 %d 数 %d", b->stage, b->first, b->n); break; } + case OP_STENCIL: { + const p_stencil *b = a; + if (b->which != 2) g_dss_dirty = 1; + for (int f = 0; f < 2; f++) if (b->face & (1 << f)) { if (b->which == 0) S.st_vmask[f] = b->value_mask; else if (b->which == 1) S.st_wmask[f] = b->mask; else S.st_ref[f] = b->ref; } + break; + } + case OP_POLY_OFFSET: S.po = *(const p_poly_offset *)a; break; + case OP_VIEWPORTS: { const p_viewports *b = a; for (int i = 0; i < b->n; i++) memcpy(S.vp[b->first + i], b->v[i], 16); S.has_vp = 1; break; } + case OP_DEPTH_RANGE: { const p_depth_range *b = a; S.dr[0] = b->n; S.dr[1] = b->f; break; } + case OP_DEPTH_CLAMP: S.depth_clamp = ((const p_depth_clamp *)a)->on; break; + case OP_BLEND_COLOR: memcpy(S.blend_color, ((const p_blend_color *)a)->rgba, 16); break; + case OP_COPY_STRIDE: { const p_copy_stride *cs = a; if (cs->image) S.copy_img_stride = cs->stride; else S.copy_stride = cs->stride; break; } + case OP_TF_BIND: { const p_tf_bind *b = a; if (b->index >= 0 && b->index < 4) S.tf[b->index] = *b; break; } + case OP_TF_STATE: { const p_tf_state *b = a; if (b->what == 0) { S.tf_on = 1; memset(S.tf_off, 0, sizeof S.tf_off); } else S.tf_on = b->what == 3; break; } + case OP_RAST_DISCARD: S.rast_discard = ((const p_rast_discard *)a)->on; break; + default: return 0; + } + return 1; +} + +static void rt_sub(const p_set_rt *r, int k, uint64_t *ser, int *lv, int *sl); + +// Prediction for parallel precompilation (see the note before pso_get): scan from after the current operation to the end of this submit, replaying only state; at a draw, compute the pipeline key from "the state at that time + that render pass's attachment formats" and dispatch those not yet built to the background. +// Whether a render pass actually attaches depth / stencil is only decided at replay (depth-only / stencil-only / neither attached are omitted), so one key is computed for each possible attach mode -- guessing too many only means wasted background compiles and does not affect the result. +static void pso_predict_rest(void) { + if (!g_exec_next) return; + static typeof(S) Ssave; memcpy(&Ssave, &S, sizeof S); + int pso_dirty = g_pso_dirty, dss_dirty = g_dss_dirty; uint8_t topology = g_pso_topology; + uint32_t cf[8]; memcpy(cf, g_pass.cfmt, sizeof cf); uint32_t df = g_pass.dfmt, sf = g_pass.sfmt, samples = g_pass.samples, dfull = df, sfull = sf; int kicked0 = (int)g_pp_kicked, draws = 0; + fz_cursor cur = {.p = g_exec_next}; + // Collect them all first, then dispatch: keys for "attachments attached as-is" go first (most likely to be used), the other attach modes after -- background concurrency is limited, and what is dispatched first is compiled first + enum { QN = 160 }; static struct { pso_key k; void *vs, *fs; } qa[QN], qb[QN]; int na = 0, nb = 0; + for (int guard = 0; guard < 200000 && na < QN; guard++) { + const uint8_t *q; op_hdr hq; const void *aq; + if (!fz_next(&cur, &q, &hq, &aq)) break; + if (hq.op == OP_SET_RT) { + const p_set_rt *r = aq; memset(cf, 0, sizeof cf); dfull = sfull = 0; samples = 1; + for (int i = 0; i < r->n && i < 8; i++) { id t = r->color[i] && !rt_dup_slot(r, i) ? tex_mtl(r->color[i]) : nil; if (t && nvn_fmt_bc(r->color[i]->d.format)) t = nil; if (t) { cf[i] = (uint32_t)rt_view_fmt(t, &r->cview[i], r->has_cview[i]); samples = (uint32_t)t.sampleCount; } } // BC targets are never attached (see attach) + id dt = r->depth ? tex_mtl(r->depth) : nil; + if (dt) { MTLPixelFormat f = dt.pixelFormat; samples = (uint32_t)dt.sampleCount; + dfull = f != MTLPixelFormatStencil8 ? (uint32_t)f : 0; sfull = (f == MTLPixelFormatStencil8 || f == MTLPixelFormatDepth32Float_Stencil8) ? (uint32_t)f : 0; } + df = dfull; sf = sfull; continue; + } + if (hq.op == OP_DRAW || hq.op == OP_DRAW_IDX) { + if (S.rast_discard) continue; + draws++; + g_pso_topology = pso_topology_class(hq.op == OP_DRAW ? ((const p_draw *)aq)->prim : ((const p_draw_idx *)aq)->prim); + __unsafe_unretained NvProg *pr = S.prog ? prog_get(S.prog) : nil; if (!pr || !pr->sh[0]) continue; + __unsafe_unretained NvShader *vs = pr->sh[0], *fs = pr->sh[1]; + const uint32_t dv[4] = {dfull, 0, 0, dfull}, sv[4] = {sfull, 0, sfull, 0}; + for (int v = 0; v < 4; v++) { + if (v && dv[v] == dv[0] && sv[v] == sv[0]) continue; + if (v == 2 && !sfull) continue; if (v == 3 && (!dfull || !sfull)) continue; + pso_key k; pso_key_fill(&k, vs, fs, cf, dv[v], sv[v], samples, 0); + if (hm_get_id(&g_pso, &k)) continue; + int dup = 0; for (int i = 0; i < (v ? nb : na) && !dup; i++) dup = !memcmp(&(v ? qb : qa)[i].k, &k, sizeof k); + if (dup) continue; + if (!v && na < QN) { qa[na].k = k; qa[na].vs = (__bridge void *)vs; qa[na].fs = (__bridge void *)fs; na++; } + else if (v && nb < QN) { qb[nb].k = k; qb[nb].vs = (__bridge void *)vs; qb[nb].fs = (__bridge void *)fs; nb++; } + } + continue; + } + if (hq.op == OP_CLEAR_COLOR || hq.op == OP_CLEAR_DS) continue; + state_op(hq.op, aq); + } + (void)df; (void)sf; + for (int i = 0; i < na; i++) pso_pend_kick(&qa[i].k, (__bridge NvShader *)qa[i].vs, (__bridge NvShader *)qa[i].fs); + for (int i = 0; i < nb; i++) pso_pend_kick(&qb[i].k, (__bridge NvShader *)qb[i].vs, (__bridge NvShader *)qb[i].fs); + memcpy(&S, &Ssave, sizeof S); args_dirty_all(); g_pso_dirty = pso_dirty; g_dss_dirty = dss_dirty; g_pso_topology = topology; + static int logged; if (g_pp_kicked > (uint64_t)kicked0 && logged++ < 20) MLOG("并行预编:本次提交余下 %d 次绘制,丢给后台 %d 条管线", draws, (int)g_pp_kicked - kicked0); +} + +static void fz_probe_cov2(const p_set_rt *r2, const uint8_t *first, const int8_t *p2map, const uint8_t *clr, int n1, int n) { + static typeof(S) Ssave; memcpy(&Ssave, &S, sizeof S); typeof(R) Rsave = R; + const p_draw *fdw = g_fd_dw; const p_draw_idx *fdi = g_fd_di; int fdwhy = g_fd_why; + int pso_dirty = g_pso_dirty, dss_dirty = g_dss_dirty; + op_hdr fh; memcpy(&fh, first, sizeof fh); + g_fd_dw = fh.op == OP_DRAW ? (const p_draw *)(first + 8) : NULL; + g_fd_di = fh.op == OP_DRAW_IDX ? (const p_draw_idx *)(first + 8) : NULL; + R.rt = *r2; R.has_rt = 1; + fz_cursor cur = {.p = g_exec_next}; + for (;;) { + const uint8_t *q; op_hdr hq; const void *aq; + if (!fz_next(&cur, &q, &hq, &aq)) break; + if (q == first) break; + if (hq.op == OP_DRAW || hq.op == OP_DRAW_IDX || hq.op == OP_SET_RT || hq.op == OP_CLEAR_COLOR || hq.op == OP_CLEAR_DS) continue; + state_op(hq.op, aq); + } + for (int j = n1; j < n && j < 8; j++) { + int k = -1; + for (int q = 0; q < 8; q++) if (p2map[q] == j) { k = q; break; } + if (k < 0 || clr[k] || !r2->color[k]) continue; + uint64_t ser; int level, slice; + rt_sub(r2, k, &ser, &level, &slice); + (void)ser; (void)slice; + g_fz.cov2[j] = (uint8_t)first_draw_covers(k, tex_mtl(r2->color[k]), (NSUInteger)level); + } + memcpy(&S, &Ssave, sizeof S); args_dirty_all(); R = Rsave; + g_fd_dw = fdw; g_fd_di = fdi; g_fd_why = fdwhy; + g_pso_dirty = pso_dirty; g_dss_dirty = dss_dirty; +} + +static int fz_load_covers(int i, id t, NSUInteger level) { + if (g_fz.active && g_fz.phase == 1 && i >= g_fz.n1) return g_fz.cov2[i]; + return first_draw_covers(i, t, level); +} + +// Probe wrap-up: for every attachment changed to clear to the sentinel color, read back that level / layer, compare pixel by pixel with a same-format 1x1 sentinel pixel, and count what remains (> 0 means the coverage proof is wrong, report a failure) +static void lp_finish(void) { + id dcb = q_cb(); + NSArray *ps = g_lp; g_lp = nil; + for (NSArray *e in ps) { + id t = e[0]; NSUInteger lv = [e[1] unsignedIntegerValue], sl = [e[2] unsignedIntegerValue]; + NSUInteger W = MAX(t.width >> lv, 1), H = MAX(t.height >> lv, 1), bpp = (NSUInteger)(att_bytes(t, lv) / (W * H)); + MTLTextureDescriptor *rd = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:t.pixelFormat width:1 height:1 mipmapped:NO]; + rd.usage = MTLTextureUsageRenderTarget; rd.storageMode = MTLStorageModePrivate; + id ref = [g_dev newTextureWithDescriptor:rd]; + MTLRenderPassDescriptor *pd = [MTLRenderPassDescriptor renderPassDescriptor]; + pd.colorAttachments[0].texture = ref; pd.colorAttachments[0].loadAction = MTLLoadActionClear; pd.colorAttachments[0].storeAction = MTLStoreActionStore; + pd.colorAttachments[0].clearColor = [e[6] intValue] == 3 ? MTLClearColorMake(NAN, NAN, NAN, NAN) : [e[6] intValue] == 2 ? MTLClearColorMake(0.777, 0.222, 0.555, 0.888) : MTLClearColorMake(0.123, 0.456, 0.789, 0.321); + [[dcb renderCommandEncoderWithDescriptor:pd] endEncoding]; + id bt = [g_dev newBufferWithLength:W * H * bpp options:MTLResourceStorageModeShared], br = [g_dev newBufferWithLength:bpp options:MTLResourceStorageModeShared]; + id b = [dcb blitCommandEncoder]; + [b copyFromTexture:t sourceSlice:sl sourceLevel:lv sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(W, H, 1) toBuffer:bt destinationOffset:0 destinationBytesPerRow:W * bpp destinationBytesPerImage:W * H * bpp]; + [b copyFromTexture:ref sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(1, 1, 1) toBuffer:br destinationOffset:0 destinationBytesPerRow:bpp destinationBytesPerImage:bpp]; + enc_b_end(b); + unsigned long long pass = [e[3] unsignedLongLongValue], ser = [e[5] unsignedLongLongValue]; int idx = [e[4] intValue]; + [dcb addCompletedHandler:^(id c) { (void)c; + const uint8_t *p = bt.contents, *r = br.contents; uint64_t left = 0; char at[96] = ""; size_t ao = 0; + for (NSUInteger k = 0; k < W * H; k++) if (!memcmp(p + k * bpp, r, bpp)) { if (left++ < 4) ao += snprintf(at + ao, sizeof at - ao, " (%lu,%lu)", (unsigned long)(k % W), (unsigned long)(k / W)); } + if (left) MLOG("【红】读入可省探针:通道 %llu c%d ser=%llu lv%lu sl%lu %lux%lu 剩哨兵像素 %llu,前几个位置%s", pass, idx, ser, (unsigned long)lv, (unsigned long)sl, (unsigned long)W, (unsigned long)H, (unsigned long long)left, at); + else MLOG("读入可省探针:通道 %llu c%d ser=%llu lv%lu sl%lu %lux%lu 全覆盖(哨兵剩 0)", pass, idx, ser, (unsigned long)lv, (unsigned long)sl, (unsigned long)W, (unsigned long)H); + }]; + } + [dcb commit]; +} +static int rt_same(const p_set_rt *a, const p_set_rt *b) { // Same set of targets: compare by texture serial and view (not by object pointer: the game may copy texture objects by value) + if (a->n != b->n || !a->depth != !b->depth || (a->depth && a->depth->serial != b->depth->serial) || a->has_dview != b->has_dview) return 0; + if (a->has_dview && memcmp(&a->dview, &b->dview, sizeof a->dview)) return 0; + for (int k = 0; k < a->n && k < 8; k++) { + if (!a->color[k] != !b->color[k] || (a->color[k] && a->color[k]->serial != b->color[k]->serial) || a->has_cview[k] != b->has_cview[k]) return 0; + if (a->has_cview[k] && memcmp(&a->cview[k], &b->cview[k], sizeof a->cview[k])) return 0; + } + return 1; +} +// Experimental RT range candidate: only ignore positive nlevels differences not used by the actual render attachments; other fields stay word-for-word identical. +#define NVN_RT_RANGE_IMPL 1 +static int rt_range_shape(const p_set_rt *a, const p_set_rt *b) { + if (a->n != 1 || b->n != 1 || !a->color[0] || !b->color[0] || !a->has_cview[0] || !b->has_cview[0] || + !a->depth != !b->depth || a->has_dview != b->has_dview || + (a->has_dview && memcmp(&a->dview,&b->dview,sizeof a->dview))) return 0; + const tex_t *x=a->color[0], *y=b->color[0]; + if (!x->serial || x->serial != y->serial || x->d.target != NVN_TEXTURE_TARGET_2D || y->d.target != NVN_TEXTURE_TARGET_2D || + x->d.samples > 1 || y->d.samples > 1 || (x->has_layout && x->mem) || (y->has_layout && y->mem) || + memcmp(&x->d,&y->d,sizeof x->d) || x->mem != y->mem || x->pool != y->pool || x->off != y->off) return 0; + const view_t *v=&a->cview[0], *w=&b->cview[0]; int levels=x->d.levels; + if (!v->set_levels || !w->set_levels || v->base_level < 0 || v->base_level >= levels || + v->nlevels <= 0 || v->nlevels > levels-v->base_level || w->nlevels <= 0 || w->nlevels > levels-v->base_level || + (v->set_layers && (v->min_layer || v->nlayers != 1)) || (v->set_format && v->format != x->d.format) || + (v->set_target && v->target != NVN_TEXTURE_TARGET_2D)) return 0; + view_t cv=*w; cv.nlevels=v->nlevels; + if (memcmp(v,&cv,sizeof cv)) return 0; + if (a->depth) { const tex_t *d=a->depth,*e=b->depth; + if (!d->serial || d->serial != e->serial || d->d.target != NVN_TEXTURE_TARGET_2D || e->d.target != NVN_TEXTURE_TARGET_2D || + d->d.samples > 1 || e->d.samples > 1 || (d->has_layout && d->mem) || (e->has_layout && e->mem) || + memcmp(&d->d,&e->d,sizeof d->d) || d->mem != e->mem || d->pool != e->pool || d->off != e->off) return 0; + int dl=a->has_dview && a->dview.set_levels ? a->dview.base_level : 0; + if (dl < 0 || dl >= d->d.levels || mip_extent(d->d.width,dl) < mip_extent(x->d.width,v->base_level) || + mip_extent(d->d.height,dl) < mip_extent(x->d.height,v->base_level)) return 0; } + + return v->nlevels != w->nlevels; +} +#define NVN_RT_RANGE_DEPTH_IMPL 1 +static void rt_range_note_depth(void) { if (rt_range_on() && S.ds.dw) g_pass.range_depth_nonro=1; } +static int rt_range_same(const p_set_rt *a, const p_set_rt *b) { + if (!rt_range_on() || !g_enc || g_fz.active || g_pass.range_external || g_pass.range_depth_nonro || !S.ds.dt || S.ds.dw || S.ds.st || g_enc_mode != 4 || !g_pass.hd || g_pass.hs || + (g_vis_on && !g_enc_has_vis) || g_pass.reg_gen != g_reg_gen || g_pass.ser_gen != g_ser_gen || g_pass.range_dirty != nvn_dirty_now() || !rt_range_shape(a,b) || !b->depth) return 0; + id color=tex_mtl(b->color[0]), depth=tex_mtl(b->depth); + int lv=b->cview[0].base_level; + return mip_extent(color.width,lv) == g_pass.tw && mip_extent(color.height,lv) == g_pass.th && + color == g_pass.range_color && depth == g_pass.range_depth && color.textureType == MTLTextureType2D && + depth.textureType == MTLTextureType2D && color.sampleCount == 1 && depth.sampleCount == 1 && + g_pass.reg_gen == g_reg_gen && g_pass.ser_gen == g_ser_gen && g_pass.range_dirty == nvn_dirty_now(); +} +static int draw_samples_rt(NvProg *pr) { // Sampled attachments keep the original conservative ser-level guard, not split by level/layer + uint64_t rs[9]; int nr = 0; + for (int k = 0; k < R.rt.n && k < 8; k++) if (R.rt.color[k]) rs[nr++] = R.rt.color[k]->serial; + if (R.rt.depth) rs[nr++] = R.rt.depth->serial; + if (!pr) return 1; + for (int st = 0; st < 5; st++) { __unsafe_unretained NvShader *sh = pr->sh[st]; if (!sh) continue; + for (int i = 0; i < sh->rf.nb; i++) { const nvn_rbind *b = &sh->rf.b[i]; uint64_t h; + if (b->kind == NVN_RB_IMG) h = b->reg < 32 && sh->stage < 6 ? S.img[sh->stage][b->reg] : 0; else if (b->kind == NVN_RB_TEX || b->kind == NVN_RB_TBUF) h = b->reg < NTEX && sh->stage < 6 ? S.tex[sh->stage][b->reg] : 0; else continue; + int tid = (int)(h & 0xfffff); if (!tid) continue; + pthread_mutex_lock(&g_rmu); uint64_t ser = tid < g_ntreg && g_treg[tid].ok ? g_treg[tid].t.serial : 0; pthread_mutex_unlock(&g_rmu); + for (int j = 0; j < nr; j++) if (ser && ser == rs[j]) return 1; } } + return 0; +} +// The whole render pass's need for depth / stencil (bits: 1 depth 2 stencil; 3 = both needed or cannot be proven). Starting from the draw that opens the render pass, scan in real replay order to the end of the render pass: +// the attached clears and this draw's state are counted first; later draws accumulate by their depth-stencil state; anything unprovable such as a mid-pass clear or nesting too deep is treated as both needed +static int pass_ds_need(void) { + int need = (R.clear_d || S.ds.dt || S.ds.dw ? 1 : 0) | (R.clear_s || S.ds.st ? 2 : 0); + if (!R.rt.depth || need == 3 || !g_exec_next || g_ret_n > 16) { + PD("S %llu 不前瞻 depth=%d clear_d=%d clear_s=%d dt=%d dw=%d st=%d next=%d ret=%d", g_stat_pass + 1, R.rt.depth != NULL, R.clear_d, R.clear_s, (int)S.ds.dt, (int)S.ds.dw, (int)S.ds.st, g_exec_next != NULL, g_ret_n); + return 3; + } + ds_st ds = S.ds; g_ds_why = -1; g_ds_scanned = 0; g_ds_need = need; + int r = ds_scan(g_exec_next, &ds, 0); + for (int k = g_ret_n - 1; r == DS_RET && k >= 0; k--) r = ds_scan(g_ret[k], &ds, 0); + for (int k = 0; r == DS_RET && k < g_nrest; k++) r = ds_scan((const uint8_t *)g_rest[k], &ds, 0); + int out = r == DS_USED ? 3 : g_ds_need; + PD("S %llu 前瞻 结果=%d 需求=%d(1 深度 2 模板)原因=%d 扫过 %d 条 返回栈 %d 余句柄 %d", g_stat_pass + 1, r, out, g_ds_why, g_ds_scanned, g_ret_n, g_nrest); + return out; +} +// Pure-clear render pass hoisting: when a render pass is opened, scan forward, and if the shape is "several draws of this render pass -> several groups (SetRenderTargets + only clears) -> back to the same targets as this render pass", +// then hoist those groups of clears to execute before this render pass and skip them at their original positions, and on returning to the same targets the same-target continuation picks up -- this render pass's attachments are no longer stored whole and loaded again for the clears sandwiched in between +// (character select page: grunge and particles both draw 28 / 27 level 0 + depth, with only levels 1 and 2 cleared in between). +// Equivalence conditions (hoist only if all hold, otherwise leave as-is): the scan range has only draws, state and these clears, no copies / dispatches / texture clears / sub-lists; the cleared subresources (texture + level + layer) +// are disjoint from this render pass's attachments; the cleared textures are neither on any current texture / image slot nor bound within the scan range (conservative: regardless of whether the program samples them); the scissor at the time of the clear covers the whole block (same requirement as executing at the original position) +static uint64_t h_ser(uint64_t h) { int tid = (int)(h & 0xfffff); return tid ? atomic_load_explicit(&g_tser[tid], memory_order_relaxed) : 0; } +// ---------- Store lookahead index: at the start of a submit do one linear scan in replay order and record events per texture; when a render pass ends, binary-search each attachment for "the first valid event afterwards" to decide ---------- +// Replaces the original "scan the whole remainder forward at every render pass end" (pass count x command count, 49 render passes per frame in camp). Cross-checked per attachment against the original scan: after 0 mismatches over the 17 pages of the screenshot gate + a camp run with about 340,000 attachment decisions (about 46,000 redundant), the original scan was removed. +// Events (seq = replay sequence number): READ a draw / dispatch reads this texture per program reflection; ATT it is a current attachment at a draw; CLEAR cleared as a whole block while an attachment; COPY / CLRTEX either end of a copy / the cleared texture. +// Decision (the first valid event decides): READ / COPY / CLRTEX -> needed; ATT whose SetRenderTargets is after the render pass end -> needed (load); CLEAR with the same -> redundant; no event -> needed. +// On any uncertainty (replay sequence number does not match the index, nesting too deep) the index is invalidated and all remaining render passes of this submit store (storing is always correct, it just saves less bandwidth). +typedef struct { uint32_t seq, rtseq; uint64_t ser; const uint8_t *op; uint16_t lv, sl; uint8_t kind, plane, anyls, why; } sdx_ev; // plane 0 color 8 depth-stencil (ATT) 1 depth 2 stencil (CLEAR) +enum { SDX_READ = 1, SDX_NEED, SDX_ATT, SDX_CLEAR }; +#define SDX_NONE 0xffffffffu +static sdx_ev *g_sdx, *g_sdx_tmp; static uint32_t g_sdx_n, g_sdx_cap; static int g_sdx_ok; // Index valid +// Candidate textures (those that have been attachments) -> buckets: open addressing; a texture that has not yet been an attachment when an event occurs is irrelevant to any render pass that ended before then, so it is not recorded +#define SDX_HB 2048 +static uint64_t g_sdx_hk[SDX_HB]; static uint16_t g_sdx_hb[SDX_HB]; static uint32_t g_sdx_nb, g_sdx_bcnt[SDX_HB + 1], g_sdx_bst[SDX_HB + 1]; +static int sdx_bucket(uint64_t ser, int add) { // Returns the bucket number; returns -1 if not in the set (and not adding); invalidates the index when full + if (!ser) return -1; + for (uint32_t i = (uint32_t)(ser * 0x9E3779B97F4A7C15ull >> 53), n = 0; n < SDX_HB; i = (i + 1) & (SDX_HB - 1), n++) { + if (g_sdx_hk[i] == ser) return g_sdx_hb[i]; + if (!g_sdx_hk[i]) { if (!add) return -1; if (g_sdx_nb >= SDX_HB / 2) { g_sdx_ok = 0; return -1; } g_sdx_hk[i] = ser; g_sdx_hb[i] = (uint16_t)g_sdx_nb; return (int)g_sdx_nb++; } + } + return -1; +} +static const uint8_t **g_sdx_op; static uint32_t g_sdx_nop, g_sdx_opcap; +static uint32_t g_exec_seq; // Sequence number of the current operation during replay +static uint64_t g_pp_slot[6][NTEX + 32]; static const prog_t *g_pp_prog; static const p_set_rt *g_pp_rt; static uint32_t g_pp_rtseq; +static uint32_t *g_rtx_seq, *g_drx_seq; static uint32_t g_rtx_n, g_rtx_cap, g_drx_n, g_drx_cap; // This submit's SET_RT / draw sequence number tables (used by the pre-screen of the readable tile merge) +static void seq_push(uint32_t **v, uint32_t *n, uint32_t *cap, uint32_t x) { if (*n == *cap) { *cap = *cap ? *cap * 2 : 1024; *v = realloc(*v, *cap * sizeof **v); } (*v)[(*n)++] = x; } +static void sdx_ev_add(sdx_ev e) { if (sdx_bucket(e.ser, 0) < 0) return; if (g_sdx_n == g_sdx_cap) { g_sdx_cap = g_sdx_cap ? g_sdx_cap * 2 : 4096; g_sdx = realloc(g_sdx, g_sdx_cap * sizeof *g_sdx); g_sdx_tmp = realloc(g_sdx_tmp, g_sdx_cap * sizeof *g_sdx); } g_sdx[g_sdx_n++] = e; } +static void sdx_lvsl(const view_t *v, int hv, uint16_t *lv, uint16_t *sl) { *lv = (uint16_t)(hv && v->set_levels ? v->base_level : 0); *sl = (uint16_t)(hv && v->set_layers ? v->min_layer : 0); } +static void sdx_walk(const uint8_t *p, int depth) { + if (depth > 64) { g_sdx_ok = 0; return; } + for (uint32_t guard = 0; guard < 4000000 && p && g_sdx_ok; guard++) { + op_hdr h; memcpy(&h, p, sizeof h); const void *a = p + 8; + if (g_sdx_nop == g_sdx_opcap) { g_sdx_opcap = g_sdx_opcap ? g_sdx_opcap * 2 : 16384; g_sdx_op = realloc(g_sdx_op, g_sdx_opcap * sizeof *g_sdx_op); } + uint32_t seq = g_sdx_nop; g_sdx_op[g_sdx_nop++] = p; + switch (h.op) { + case OP_END: return; + case OP_JUMP: { const uint8_t *t; memcpy(&t, a, 8); p = t; continue; } + case OP_CALL: { uint64_t t; memcpy(&t, a, 8); sdx_walk((const uint8_t *)t, depth + 1); break; } + case OP_SET_RT: { const p_set_rt *r = a; g_pp_rt = r; g_pp_rtseq = seq; seq_push(&g_rtx_seq, &g_rtx_n, &g_rtx_cap, seq); + for (int k = 0; k < r->n && k < 8; k++) if (r->color[k]) sdx_bucket(r->color[k]->serial, 1); + if (r->depth) sdx_bucket(r->depth->serial, 1); + break; } + case OP_CLEAR_COLOR: { const p_clear_color *c = a; const p_set_rt *r = g_pp_rt; + if (!r || (c->mask & 0xf) != 0xf || c->index < 0 || c->index >= r->n || c->index >= 8 || !r->color[c->index]) break; + uint16_t lv, sl; sdx_lvsl(&r->cview[c->index], r->has_cview[c->index], &lv, &sl); int last = -1; // When the same subresource is attached at several indices, the last one counts + for (int k = 0; k < r->n && k < 8; k++) { uint16_t l2, s2; if (!r->color[k] || r->color[k]->serial != r->color[c->index]->serial) continue; sdx_lvsl(&r->cview[k], r->has_cview[k], &l2, &s2); if (l2 == lv && s2 == sl) last = k; } + if (last == c->index) sdx_ev_add((sdx_ev){seq, g_pp_rtseq, r->color[c->index]->serial, p, lv, sl, SDX_CLEAR, 0, 0, 0}); + break; } + case OP_CLEAR_DS: { const p_clear_ds *c = a; const p_set_rt *r = g_pp_rt; if (!r || !r->depth) break; + uint16_t lv, sl; sdx_lvsl(&r->dview, r->has_dview, &lv, &sl); + if (c->depth_mask) sdx_ev_add((sdx_ev){seq, g_pp_rtseq, r->depth->serial, p, lv, sl, SDX_CLEAR, 1, !r->has_dview, 0}); + if ((c->stencil_mask & 0xff) == 0xff) sdx_ev_add((sdx_ev){seq, g_pp_rtseq, r->depth->serial, p, lv, sl, SDX_CLEAR, 2, !r->has_dview, 0}); + break; } + case OP_BIND_TEX: case OP_BIND_TEX1: case OP_BIND_IMG: { p_bind_handles one; const p_bind_handles *b = bind_rec(h.op, a, &one); bh_fresh(b); int base = h.op == OP_BIND_IMG ? NTEX : 0, lim = h.op == OP_BIND_IMG ? 32 : NTEX; + if (b->stage < 6 && b->first >= 0 && b->first + b->n <= lim) for (int k = 0; k < b->n; k++) g_pp_slot[b->stage][base + b->first + k] = h_ser(b->h[k]); + break; } + case OP_BIND_PROGRAM: g_pp_prog = ((const p_bind_program *)a)->prog; break; + case OP_DRAW: case OP_DRAW_IDX: case OP_DISPATCH: { + if (h.op != OP_DISPATCH) seq_push(&g_drx_seq, &g_drx_n, &g_drx_cap, seq); + __unsafe_unretained NvProg *pr = g_pp_prog ? prog_get(g_pp_prog) : nil; + if (!pr) { for (int st = 0; st < 6; st++) for (int k = 0; k < NTEX + 32; k++) if (g_pp_slot[st][k]) sdx_ev_add((sdx_ev){seq, 0, g_pp_slot[st][k], p, 0, 0, SDX_READ, 0, 0, 1}); } + else for (int st = 0; st < 6; st++) { __unsafe_unretained NvShader *sh = pr->sh[st]; if (!sh || (h.op == OP_DISPATCH) != (st == 5)) continue; + for (int i = 0; i < sh->rf.nb; i++) { const nvn_rbind *b = &sh->rf.b[i]; int k = b->kind == NVN_RB_IMG ? NTEX + b->reg : (b->kind == NVN_RB_TEX || b->kind == NVN_RB_TBUF) ? b->reg : -1; + if (k < 0 || k >= NTEX + 32 || sh->stage < 0 || sh->stage >= 6) continue; uint64_t ser = g_pp_slot[sh->stage][k]; + if (ser) sdx_ev_add((sdx_ev){seq, 0, ser, p, 0, 0, SDX_READ, 0, 0, 1}); } } + const p_set_rt *r = g_pp_rt; + if (h.op != OP_DISPATCH && r) { + for (int k = 0; k < r->n && k < 8; k++) if (r->color[k]) { uint16_t lv, sl; sdx_lvsl(&r->cview[k], r->has_cview[k], &lv, &sl); sdx_ev_add((sdx_ev){seq, g_pp_rtseq, r->color[k]->serial, p, lv, sl, SDX_ATT, 0, 0, 2}); } + if (r->depth) { uint16_t lv, sl; sdx_lvsl(&r->dview, r->has_dview, &lv, &sl); sdx_ev_add((sdx_ev){seq, g_pp_rtseq, r->depth->serial, p, lv, sl, SDX_ATT, 8, !r->has_dview, 2}); } + } + break; } + case OP_COPY_T2T: { const p_copy_t2t *c = a; if (c->src) sdx_ev_add((sdx_ev){seq, 0, c->src->serial, p, 0, 0, SDX_NEED, 0, 0, 3}); if (c->dst) sdx_ev_add((sdx_ev){seq, 0, c->dst->serial, p, 0, 0, SDX_NEED, 0, 0, 3}); break; } + case OP_COPY_T2B: case OP_COPY_B2T: { const p_copy_b2t *c = a; if (c->tex) sdx_ev_add((sdx_ev){seq, 0, c->tex->serial, p, 0, 0, SDX_NEED, 0, 0, 3}); break; } + case OP_CLEAR_TEX: { const p_clear_tex *c = a; if (c->tex) sdx_ev_add((sdx_ev){seq, 0, c->tex->serial, p, 0, 0, SDX_NEED, 0, 0, 4}); break; } + default: break; + } + p += h.size; + } +} +void mtl_sub_index(const uint64_t *h, int n) { // Start of submit (hle_nvn.c QueueSubmitCommands): build this submit's store lookahead index + g_xv_ntouch = 0; g_sdx_n = g_sdx_nop = 0; g_rtx_n = g_drx_n = 0; g_exec_seq = 0; g_sdx_ok = tbdr_on(); + if (!g_sdx_ok) { g_xv_n = 0; g_xv_block = 1; return; } // Index unavailable: clear the ledger and add nothing in this submit (the next touch cannot be verified) + g_xv_block = 0; + memset(g_sdx_hk, 0, sizeof g_sdx_hk); g_sdx_nb = 0; + for (int i = 0; i < g_xv_n; i++) sdx_bucket(g_xv[i].ser, 1); // Textures in the ledger enter the candidate set: otherwise, when one is only sampled and not used as an attachment in this submit, its read events are not indexed and the fallback cannot see them + if (R.has_rt) { for (int k = 0; k < R.rt.n && k < 8; k++) if (R.rt.color[k]) sdx_bucket(R.rt.color[k]->serial, 1); if (R.rt.depth) sdx_bucket(R.rt.depth->serial, 1); } // The targets at the start of the submit (the first draw may reopen the render pass without going through SetRenderTargets) + g_pp_prog = S.prog; g_pp_rt = NULL; g_pp_rtseq = SDX_NONE; + for (int st = 0; st < 6; st++) { for (int k = 0; k < NTEX; k++) g_pp_slot[st][k] = S.tex[st][k] ? h_ser(S.tex[st][k]) : 0; + for (int k = 0; k < 32; k++) g_pp_slot[st][NTEX + k] = S.img[st][k] ? h_ser(S.img[st][k]) : 0; } + for (int i = 0; i < n && g_sdx_ok; i++) sdx_walk((const uint8_t *)h[i], 0); + if (!g_sdx_ok) return; + // Counting sort into buckets: events are appended in sequence-number order, and within the same sequence number READ precedes ATT (the same draw is judged for reads first, then attachments), so after a stable scatter each bucket is ordered + memset(g_sdx_bcnt, 0, (g_sdx_nb + 1) * sizeof *g_sdx_bcnt); + for (uint32_t i = 0; i < g_sdx_n; i++) g_sdx_bcnt[sdx_bucket(g_sdx[i].ser, 0)]++; + for (uint32_t b = 0, acc = 0; b <= g_sdx_nb; b++) { g_sdx_bst[b] = acc; acc += b < g_sdx_nb ? g_sdx_bcnt[b] : 0; } + for (uint32_t b = 0; b < g_sdx_nb; b++) g_sdx_bcnt[b] = g_sdx_bst[b]; + for (uint32_t i = 0; i < g_sdx_n; i++) g_sdx_tmp[g_sdx_bcnt[sdx_bucket(g_sdx[i].ser, 0)]++] = g_sdx[i]; + sdx_ev *t = g_sdx; g_sdx = g_sdx_tmp; g_sdx_tmp = t; + xv_forward(); +} +static void sdx_seq(const uint8_t *p) { // Every operation in mtl_exec: check the sequence number against the index (on mismatch invalidate, and store everything for the rest) + if (!g_sdx_ok) return; + if (g_exec_seq >= g_sdx_nop || g_sdx_op[g_exec_seq] != p) { RED_ONCE(40, "写回前瞻索引与回放顺序不符(序号 %u / %u),本次提交余下通道一律写回", g_exec_seq, g_sdx_nop); g_sdx_ok = 0; return; } + g_exec_seq++; +} +static int sdx_decide(uint8_t st[9]) { // st[i]: 2 redundant; otherwise why << 2 (the reason it is needed, same as sd_need). Returns 0 = index unavailable + if (!g_sdx_ok || !g_exec_cur || !g_exec_seq || g_sdx_op[g_exec_seq - 1] != g_exec_cur) return 0; + uint32_t P = g_exec_seq - 1; + for (int i = 0; i < g_nsd; i++) { const sd_att *t = &g_sd[i]; st[i] = 5 << 2; + int b = sdx_bucket(t->ser, 0); if (b < 0) continue; // Not in the candidate set: no related event at all in this submit -> undecided after scanning to the end, needed + uint32_t lo = g_sdx_bst[b], end = g_sdx_bst[b + 1], hi = end; // First seq >= P in the bucket + while (lo < hi) { uint32_t m = (lo + hi) / 2; if (g_sdx[m].seq < P) lo = m + 1; else hi = m; } + for (uint32_t j = lo; j < end; j++) { const sdx_ev *e = &g_sdx[j]; + if (g_hoist_n && hoist_skipped(e->op)) continue; + if (e->kind == SDX_READ || e->kind == SDX_NEED) { st[i] = (uint8_t)(e->why << 2); break; } + if (e->rtseq == SDX_NONE || e->rtseq < P || (g_hoist_n && hoist_skipped(g_sdx_op[e->rtseq]))) continue; // This target was set before the render pass ended: not re-set as an attachment after the render pass ended, so it does not count + int hit = t->depth ? (e->kind == SDX_ATT ? e->plane == 8 : e->plane == t->depth) && (e->anyls || (e->lv == t->lv && e->sl == t->sl)) + : e->plane == 0 && e->lv == t->lv && e->sl == t->sl; + if (!hit) continue; + st[i] = e->kind == SDX_CLEAR ? 2 : (uint8_t)(2 << 2); break; } + } + return 1; +} +static void xv_forward(void) { + if (!g_xv_n) return; + if (!g_sdx_ok) { g_xv_n = 0; g_xv_block = 1; return; } + for (int i = 0; i < g_xv_n;) { + xv_ent *e = &g_xv[i]; int b = sdx_bucket(e->ser, 0), action = 0; + if (b >= 0) for (uint32_t j = g_sdx_bst[b]; j < g_sdx_bst[b + 1]; j++) { sdx_ev *v = &g_sdx[j]; + if (v->kind == SDX_READ || v->kind == SDX_NEED) { xv_ent copy = *e; xv_violation(©, "提交前瞻读/需求"); action = 2; break; } + if (v->lv != e->lv || v->sl != e->sl) continue; + if (v->kind == SDX_CLEAR) { xv_good(e); action = 1; break; } + if (v->kind == SDX_ATT) break; + } + if (!action) i++; + } +} +static void rt_sub(const p_set_rt *r, int k, uint64_t *ser, int *lv, int *sl) { // Subresource of attachment k (0–7 color, 8 depth) + const tex_t *t = k == 8 ? r->depth : r->color[k]; const view_t *v = k == 8 ? &r->dview : &r->cview[k]; int hv = k == 8 ? r->has_dview : r->has_cview[k]; + *ser = t ? t->serial : 0; *lv = hv && v->set_levels ? v->base_level : 0; *sl = hv && v->set_layers ? v->min_layer : 0; +} +static void hoist_try(void) { + if (g_hoist_n || !g_exec_next) return; + typedef struct { const uint8_t *rt_op; const p_set_rt *rt; uint8_t clear_c[8]; MTLClearColor cc[8]; uint8_t clear_d, clear_s; double cd; uint32_t cs; + p_scissor sc; int has_sc; const uint8_t *ops[8]; int nops; } grp; + grp G[4]; int ng = 0, in_grp = 0; p_scissor sc = R.sc; int has_sc = R.has_sc; + // The binding set needed for "the cleared texture is not on any slot": the table is looked up only when a hoistable clear group is really found and we are back to this render pass's targets (the vast majority of render passes never get that far). + // Handles bound within the scan range are noted first and converted to texture serials together with the current slots at lookup time; the scan only reads commands and the current slots S do not change meanwhile, so the result is the same as checking at the start + uint64_t bh[512]; int nbh = 0; + const uint8_t *p = g_exec_next; + for (int guard = 0; guard < 200000; guard++) { + op_hdr h; memcpy(&h, p, sizeof h); const void *a = p + 8; + switch (h.op) { + case OP_END: case OP_CALL: case OP_COPY_T2T: case OP_COPY_B2T: case OP_COPY_T2B: case OP_CLEAR_TEX: case OP_DISPATCH: case OP_RENDER_COND: return; + case OP_JUMP: { const uint8_t *t; memcpy(&t, a, 8); p = t; continue; } + case OP_DRAW: case OP_DRAW_IDX: if (in_grp) return; break; + case OP_SCISSOR: sc = *(const p_scissor *)a; has_sc = 1; break; + case OP_BIND_TEX: case OP_BIND_TEX1: case OP_BIND_IMG: { p_bind_handles one; const p_bind_handles *b = bind_rec(h.op, a, &one); bh_fresh(b); for (int k = 0; k < b->n && k < 32 && nbh < 512; k++) if (b->h[k]) bh[nbh++] = b->h[k]; break; } + case OP_CLEAR_COLOR: case OP_CLEAR_DS: { + if (!in_grp || G[ng - 1].nops >= 8) return; + grp *g = &G[ng - 1]; + if (h.op == OP_CLEAR_COLOR) { const p_clear_color *c = a; + if (c->index < 0 || c->index >= g->rt->n || !g->rt->color[c->index] || c->mask != 0xf) return; + const tex_t *t = g->rt->color[c->index]; + if (has_sc && !(sc.x <= 0 && sc.y <= 0 && sc.x + sc.w >= (int)t->d.width && sc.y + sc.h >= (int)t->d.height)) return; + g->clear_c[c->index] = 1; g->cc[c->index] = MTLClearColorMake(c->rgba[0], c->rgba[1], c->rgba[2], c->rgba[3]); } + else { const p_clear_ds *c = a; if (!g->rt->depth) return; + if (c->depth_mask) { g->clear_d = 1; g->cd = c->depth; } if (c->stencil_mask) { g->clear_s = 1; g->cs = (uint32_t)c->stencil; } } + g->sc = sc; g->has_sc = has_sc; g->ops[g->nops++] = p; break; } + case OP_SET_RT: { const p_set_rt *r = a; + if (rt_same(&R.rt, r)) { // Back to this render pass's targets: execute if hoisting holds + if (!ng) return; + uint64_t bound[512]; int nb = 0; // The order of the original "check at the start": the current slots (stage x textures 128 + images 32), followed by those bound within the scan range + for (int st = 0; st < 6; st++) { for (int k = 0; k < NTEX && nb < 512; k++) if (S.tex[st][k]) bound[nb++] = h_ser(S.tex[st][k]); + for (int k = 0; k < 32 && nb < 512; k++) if (S.img[st][k]) bound[nb++] = h_ser(S.img[st][k]); } + for (int k = 0; k < nbh && nb < 512; k++) bound[nb++] = h_ser(bh[k]); + for (int gi = 0; gi < ng; gi++) { // Disjoint and not bound + grp *g = &G[gi]; if (!g->nops) return; + for (int k = 0; k < 9; k++) { if (k < 8 ? !g->clear_c[k] : !(g->clear_d || g->clear_s)) continue; + uint64_t ser; int lv, sl; rt_sub(g->rt, k, &ser, &lv, &sl); if (!ser) return; + for (int j = 0; j < nb; j++) if (bound[j] == ser) return; + for (int m = 0; m < 9; m++) { uint64_t s2; int l2, sl2; if (m < 8 && m >= R.rt.n) continue; rt_sub(&R.rt, m, &s2, &l2, &sl2); + if (s2 == ser && l2 == lv && sl2 == sl) return; } } } + // Register all skip entries first, then execute: when a hoisted render pass ends the store lookahead scans forward, and the clears at their original positions must already be in the skip set + for (int gi = 0; gi < ng; gi++) { g_hoist_skip[g_hoist_n++] = G[gi].rt_op; for (int k = 0; k < G[gi].nops; k++) g_hoist_skip[g_hoist_n++] = G[gi].ops[k]; } + p_set_rt rt0 = R.rt; p_scissor sc0 = R.sc; int hs0 = R.has_sc; // This render pass's targets, scissor and pending clears: restored as they were after execution + uint8_t cc0f[8]; MTLClearColor cc0[8]; uint8_t cd0f = R.clear_d, cs0f = R.clear_s; double cd0 = R.cd; uint32_t cs0 = R.cs; + memcpy(cc0f, R.clear_c, 8); memcpy(cc0, R.cc, sizeof cc0); + for (int gi = 0; gi < ng; gi++) { // Execute: temporarily swap in that group's targets and clears and open a clear-only render pass + grp *g = &G[gi]; + R.rt = *g->rt; memcpy(R.clear_c, g->clear_c, 8); memcpy(R.cc, g->cc, sizeof R.cc); R.clear_d = g->clear_d; R.clear_s = g->clear_s; R.cd = g->cd; R.cs = g->cs; + R.sc = g->sc; R.has_sc = g->has_sc; + PD("H %llu 前提纯清除通道(原位置在本通道之后)", g_stat_pass + 1); + rp_begin_ex(1); rp_end(); + } + R.rt = rt0; R.sc = sc0; R.has_sc = hs0; memcpy(R.clear_c, cc0f, 8); memcpy(R.cc, cc0, sizeof cc0); R.clear_d = cd0f; R.clear_s = cs0f; R.cd = cd0; R.cs = cs0; + if (!g_hoist_groups) MLOG("纯清除通道前提:首例 %d 组(通道 %llu 前)", ng, g_stat_pass + 1); + g_hoist_groups += (uint64_t)ng; + return; + } + if (in_grp && !G[ng - 1].nops) return; // The previous group has no clear (only set targets): not recognized + if (ng >= 4) return; + memset(&G[ng], 0, sizeof G[ng]); G[ng].rt_op = p; G[ng].rt = r; ng++; in_grp = 1; break; } + default: break; + } + p += h.size; + } +} +// ---------- Readable tile render pass merge (merge B step 2) ---------- +// When a draw of the later pass samples, at the same pixel, a color attachment written by the earlier pass, the two passes are merged into one render pass: what the earlier pass wrote stays in the tile, +// and the later pass reads it directly with the readable tile version (_bf, function-constant specialization), instead of first landing in main memory and being read back just to be sampled. +// Planning (fuse_try, at the start of the earlier pass) does only structural checks; the closed-form proof of same-pixel addressing is done at every draw of the later pass (fz_p2_draw), and if it cannot be proven the merged render pass is ended and +// a normal render pass is reopened with the later pass's original targets (the earlier pass's result is stored as usual, equivalent to not merging). +static void fz_unskip(void) { for (int i = 0; i < g_fz.nskip; i++) for (int j = 0; j < g_hoist_n; j++) if (g_hoist_skip[j] == g_fz.skip[i]) { g_hoist_skip[j] = g_hoist_skip[--g_hoist_n]; break; } } +static int fz_reuse_op(const uint8_t *p) { for (int i = 0; i < g_fz.nreuse; i++) if (g_fz.reuse[i] == p) return 1; return 0; } +// Diagnostic DM_NVN_FUSE_CHECK=1: in-run double rendering. For the later pass's draws that can be proven, keep a state snapshot per draw; after the merged render pass ends, make a copy of the attachments the later pass wrote (the merge path's result), +// then turn off all lookahead and redraw one by one through the normal path with the later pass's original targets and original clears, and compare the two byte by byte (same frame, same input, no dependence on cross-run determinism). The redraw result is left on the attachment (no effect if identical) +static int fz_check_on(void) { static int v = -1; if (v < 0) v = getenv("DM_NVN_FUSE_CHECK") != NULL; return v; } +typedef struct { int idx; p_draw dw; p_draw_idx di; p_scissor sc; int has_sc; void *S_; } fz_snap; +static fz_snap g_fzs[8]; static int g_nfzs, g_fzs_bad, g_fz_checking; static uint64_t g_fzc_same, g_fzc_diff; +static void draw(const p_draw *dw, const p_draw_idx *di); +static void fz_snap_take(const p_draw *dw, const p_draw_idx *di) { + if (g_nfzs >= 8) { g_fzs_bad = 1; return; } + fz_snap *z = &g_fzs[g_nfzs++]; z->idx = di != NULL; if (dw) z->dw = *dw; if (di) z->di = *di; z->sc = R.sc; z->has_sc = R.has_sc; + z->S_ = malloc(sizeof S); memcpy(z->S_, &S, sizeof S); +} +static void fz_check_run(const fuse_t *fz) { + if (!g_nfzs || g_fzs_bad) { if (g_fzs_bad) MLOG("【红】局内双渲染:后一个通道绘制超过 8 次,未比"); goto out; } + { + id att[8], scr[8]; uint64_t sers[8]; int na = 0; + // fz_end has already restored R.rt from the physical union to the later pass's logical targets; wrote is still registered by physical slot and must be aligned through p2map. + for (int j = 0; j < fz->rt2.n && j < 8; j++) if (fz->p2map[j] >= 0 && fz->wrote[fz->p2map[j]] && fz->rt2.color[j]) { + id t = tex_mtl(fz->rt2.color[j]); if (!t) continue; + MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:t.pixelFormat width:t.width height:t.height mipmapped:NO]; + d.usage = MTLTextureUsageShaderRead; d.storageMode = MTLStorageModePrivate; + id c = [g_dev newTextureWithDescriptor:d]; if (!c) continue; + enc_b_copy_texture(blit(), t, 0, 0, MTLOriginMake(0, 0, 0), MTLSizeMake(t.width, t.height, 1), c, 0, 0, MTLOriginMake(0, 0, 0)); + att[na] = t; scr[na] = c; sers[na] = fz->rt2.color[j]->serial; na++; } + if (!na) { MLOG("【红】局内双渲染:已留 %d 次绘制快照,但没有可比附件", g_nfzs); goto out; } + static typeof(S) Ssave; memcpy(&Ssave, &S, sizeof S); typeof(R) Rsave = R; const uint8_t *en0 = g_exec_next; + g_fz_checking = 1; g_exec_next = NULL; // Turn off all lookahead during the redraw (continuation / hoisting / merging / depth-stencil on demand): normal path + R.rt = fz->rt2; memcpy(R.clear_c, fz->clr2, 8); memcpy(R.cc, fz->cc2, sizeof R.cc); R.clear_d = fz->cd2f; R.clear_s = fz->cs2f; R.cd = fz->cd2; R.cs = fz->cs2; + for (int i = 0; i < g_nfzs; i++) { fz_snap *z = &g_fzs[i]; memcpy(&S, z->S_, sizeof S); args_dirty_all(); R.sc = z->sc; R.has_sc = z->has_sc; g_pso_dirty = g_dss_dirty = 1; + draw(z->idx ? NULL : &z->dw, z->idx ? &z->di : NULL); } + rp_end(); + memcpy(&S, &Ssave, sizeof S); args_dirty_all(); R = Rsave; g_exec_next = en0; g_fz_checking = 0; g_pso_dirty = g_dss_dirty = 1; + for (int a = 0; a < na; a++) { + id t = att[a], c = scr[a]; NSUInteger W = t.width, H = t.height, bpp = (NSUInteger)(att_bytes(t, 0) / (W * H)); + id b1 = [g_dev newBufferWithLength:W * H * bpp options:MTLResourceStorageModeShared], b2 = [g_dev newBufferWithLength:W * H * bpp options:MTLResourceStorageModeShared]; + id b = blit(); + enc_b_copy_texture_buffer(b, c, 0, 0, MTLOriginMake(0, 0, 0), MTLSizeMake(W, H, 1), b1, 0, W * bpp, W * H * bpp); + enc_b_copy_texture_buffer(b, t, 0, 0, MTLOriginMake(0, 0, 0), MTLSizeMake(W, H, 1), b2, 0, W * bpp, W * H * bpp); + unsigned long long ser = sers[a], pass = g_stat_pass, frame = atomic_load(&g_cur_frame); NSUInteger nb = W * H * bpp; + [g_commit_blocks addObject:^{ + const uint8_t *x = b1.contents, *y = b2.contents; uint64_t dpx = 0, maxd = 0; + for (NSUInteger k = 0; k < nb; k += bpp) if (memcmp(x + k, y + k, bpp)) { dpx++; for (NSUInteger q = 0; q < bpp; q++) { uint64_t dd = (uint64_t)abs((int)x[k + q] - (int)y[k + q]); if (dd > maxd) maxd = dd; } } + if (dpx) { g_fzc_diff++; MLOG("【红】局内双渲染:帧 %llu 通道 %llu 附件 ser=%llu 合并 vs 普通 不同像素 %llu / %lu(逐字节最大差 %llu)", frame, pass, ser, (unsigned long long)dpx, (unsigned long)(nb / bpp), (unsigned long long)maxd); } + else { g_fzc_same++; MLOG("局内双渲染:帧 %llu 通道 %llu 附件 ser=%llu 合并 vs 普通 逐字节相同(%lu 像素)", frame, pass, ser, (unsigned long)(nb / bpp)); } + if ((g_fzc_same + g_fzc_diff) % 300 == 0) MLOG("局内双渲染累计:逐字节相同 %llu 次、不同 %llu 次", (unsigned long long)g_fzc_same, (unsigned long long)g_fzc_diff); + }]; + } + } +out: + for (int i = 0; i < g_nfzs; i++) free(g_fzs[i].S_); + g_nfzs = 0; g_fzs_bad = 0; +} +static void fz_end(void) { // End of a merged render pass (called by rp_end): interrupted before entering the later pass -> retract the skip entries and restore the targets to the earlier pass's; otherwise restore to the later pass's + if (!g_fz.active) return; + if (g_fz.phase == 1) { fz_unskip(); R.rt = g_fz.rt1; } else R.rt = g_fz.rt2; + g_fz.active = 0; + if (g_fz.phase == 2 && fz_check_on() && !g_fz_checking) { fuse_t fz = g_fz; R.rt = fz.rt2; fz_check_run(&fz); } + else { for (int i = 0; i < g_nfzs; i++) free(g_fzs[i].S_); g_nfzs = 0; g_fzs_bad = 0; } +} +static int sub_eq(const p_set_rt *a, int ka, const p_set_rt *b, int kb) { uint64_t s1, s2; int l1, l2, x1, x2; rt_sub(a, ka, &s1, &l1, &x1); rt_sub(b, kb, &s2, &l2, &x2); return s1 && s1 == s2 && l1 == l2 && x1 == x2; } +static int prog_samples_ser(const prog_t *pg, uint64_t tex[6][NTEX], uint64_t img[6][32], uint64_t ser) { // Whether this texture is bound in any texture / image / typed buffer slot declared by the program's reflection + __unsafe_unretained NvProg *pr = pg ? prog_get(pg) : nil; if (!pr) return 1; + for (int st = 0; st < 5; st++) { __unsafe_unretained NvShader *sh = pr->sh[st]; if (!sh || sh->stage >= 6) continue; + for (int i = 0; i < sh->rf.nb; i++) { const nvn_rbind *b = &sh->rf.b[i]; uint64_t h; + if (b->kind == NVN_RB_IMG) h = b->reg < 32 ? img[sh->stage][b->reg] : 0; else if (b->kind == NVN_RB_TEX || b->kind == NVN_RB_TBUF) h = b->reg < NTEX ? tex[sh->stage][b->reg] : 0; else continue; + if (h && h_ser(h) == ser) return 1; } } + return 0; +} +static int g_defer_env_ab, g_defer_env_off; // Environment switches: read once at the start of each submit (defer_reset); the hot path does not call getenv +static void defer_env(void) { const char *x = getenv("DM_NVN_DEFER_AB"), *e = getenv("DM_NVN_DEFER"); g_defer_env_ab = x && !strcmp(x, "1"); g_defer_env_off = e && !strcmp(e, "0"); } +static int defer_on(void) { + if (!g_defer_env_ab) return tbdr_on() && !g_defer_env_off; + if (g_defer_ab < 0) { g_defer_ab = 0; g_defer_ab_on = 0; } + return tbdr_on() && (g_defer_ab || g_defer_ab_on); } +static int defer_handle_sub(uint64_t h, const p_set_rt *r, int k, int cover) { + int tid = (int)(h & 0xfffff); if (!tid || tid >= g_ntreg || !g_treg[tid].ok) return 0; + uint64_t ser; int lv, sl; rt_sub(r, k, &ser, &lv, &sl); if (!ser || g_treg[tid].t.serial != ser) return 0; + if (!g_treg[tid].has_v || !g_treg[tid].v.set_levels) return cover; + view_t v = g_treg[tid].v; int n = v.nlevels > 0 ? v.nlevels : 1; + return lv >= v.base_level && lv < v.base_level + n && (!v.set_layers || (sl >= v.min_layer && sl < v.min_layer + (v.nlayers > 0 ? v.nlayers : 1))); +} +static int defer_prog_hits(const prog_t *pg, uint64_t tex[6][NTEX], uint64_t img[6][32], const p_set_rt *r, int color_only, int cover) { + __unsafe_unretained NvProg *pr = pg ? prog_get(pg) : nil; if (!pr) return 1; + for (int st = 0; st < 5; st++) { __unsafe_unretained NvShader *sh = pr->sh[st]; if (!sh || sh->stage < 0 || sh->stage >= 6) continue; + for (int i = 0; i < sh->rf.nb; i++) { const nvn_rbind *b = &sh->rf.b[i]; uint64_t h = 0; + if (b->kind == NVN_RB_IMG) h = b->reg < 32 ? img[sh->stage][b->reg] : 0; + else if (b->kind == NVN_RB_TEX || b->kind == NVN_RB_TBUF) h = b->reg < NTEX ? tex[sh->stage][b->reg] : 0; + else continue; + if (!h) continue; + for (int k = 0; k < (color_only ? r->n : 9); k++) if ((color_only ? r->color[k] : (k < 8 ? r->color[k] : r->depth)) && defer_handle_sub(h, r, k, cover)) return 1; + } + } + return 0; +} +static void defer_collect_reads(defer_t *d, const prog_t *pg, uint64_t tex[6][NTEX], uint64_t img[6][32]) { + __unsafe_unretained NvProg *pr = pg ? prog_get(pg) : nil; if (!pr) { d->nreads = 513; return; } + for (int st = 0; st < 5; st++) { __unsafe_unretained NvShader *sh = pr->sh[st]; if (!sh || sh->stage < 0 || sh->stage >= 6) continue; + for (int i = 0; i < sh->rf.nb; i++) { const nvn_rbind *b = &sh->rf.b[i]; uint64_t h = 0; + if (b->kind == NVN_RB_IMG) h = b->reg < 32 ? img[sh->stage][b->reg] : 0; + else if (b->kind == NVN_RB_TEX || b->kind == NVN_RB_TBUF) h = b->reg < NTEX ? tex[sh->stage][b->reg] : 0; + else continue; + if (!h) continue; int seen = 0; for (int j = 0; j < d->nreads; j++) seen |= d->p_reads[j] == h; + if (!seen && d->nreads < 512) d->p_reads[d->nreads++] = h; + } + } +} +static int defer_rt_hits_reads(const p_set_rt *r, int idx) { + if (idx < 0 || idx >= r->n || !r->color[idx]) return 0; + for (int i = 0; i < g_defer.nreads; i++) if (defer_handle_sub(g_defer.p_reads[i], r, idx, 1)) return 1; + return 0; +} +static int defer_q_superset(const p_set_rt *q) { + memset(g_defer.lmap, 0xff, sizeof g_defer.lmap); + for (int i = 0; i < g_defer.p_rt.n; i++) if (g_defer.p_rt.color[i]) { int found = 0; + // The indices must be the same: a normal fragment function's color(n) output is written to physical attachment n, and lmap only changes the write mask / blend and cannot move the output (only the _bf version with output mapping constants can). + // With different indices, deferral would write P's output into a different attachment of Q. + if (i < q->n && q->color[i] && sub_eq(&g_defer.p_rt, i, q, i)) { g_defer.lmap[i] = (uint8_t)i; found = 1; } + if (!found) return 0; + } + return 1; +} +static int defer_take(const p_draw *dw, const p_draw_idx *di) { + if (g_defer.n >= 64) { g_defer.reason = 20; return 0; } + defer_snap *z = &g_defer.snap[g_defer.n++]; z->idx = di != NULL; if (dw) z->dw = *dw; if (di) z->di = *di; + z->sc = R.sc; z->has_sc = R.has_sc; z->S_ = malloc(sizeof S); memcpy(z->S_, &S, sizeof S); for (int k = 0; k < 8; k++) z->pm[k] = (uint8_t)k; g_defer_draws++; return 1; +} +static void defer_release(void) { defer_release_one(&g_defer); } +static void defer_env(void); +static void defer_reset(void) { defer_clear_all(); g_defer_replaying = 0; defer_env(); } +static void defer_apply_p_clear(void) { + memset(R.clear_c, 0, sizeof R.clear_c); R.clear_d = R.clear_s = 0; + for (int j = 0; j < g_defer.q_rt.n; j++) { int i = g_defer.lmap[j]; if (i >= 0 && g_defer.clear_c[i]) { R.clear_c[j] = 1; R.cc[j] = g_defer.cc[i]; } } + if (g_defer.clear_d) { R.clear_d = 1; R.cd = g_defer.cd; } if (g_defer.clear_s) { R.clear_s = 1; R.cs = g_defer.cs; } +} +static int defer_begin_q(const uint8_t *op) { + int found = 0, first = -1; + for (int i = 0; i < 4; i++) if (g_defer_slots[i].active && !g_defer_slots[i].at_q && g_defer_slots[i].q_op == op) { found = 1; if (first < 0) first = i; } + if (!found) return 0; + rp_flush(); + g_defer_cur = &g_defer_slots[first]; R.rt = g_defer.q_rt; R.has_rt = 1; + for (int i = 0; i < 4 && !R.rt.depth; i++) { const defer_t *d = &g_defer_slots[i]; if (d->active && !d->at_q && d->q_op == op && d->q_rt.depth) { R.rt.depth = d->q_rt.depth; R.rt.dview = d->q_rt.dview; R.rt.has_dview = d->q_rt.has_dview; } } // Attach the depth-stencil needed by any group (the depths of groups with the same Q have been checked compatible) + memset(R.clear_c, 0, sizeof R.clear_c); R.clear_d = R.clear_s = 0; + for (int i = 0; i < 4; i++) { defer_t *d = &g_defer_slots[i]; if (!d->active || d->at_q || d->q_op != op) continue; + for (int j = 0; j < d->q_rt.n; j++) { int k = d->lmap[j]; if (k >= 0 && d->clear_c[k]) { R.clear_c[j] = 1; R.cc[j] = d->cc[k]; } } + if (d->clear_d) { R.clear_d = 1; R.cd = d->cd; } if (d->clear_s) { R.clear_s = 1; R.cs = d->cs; } + d->at_q = 1; g_defer_natq++; + } + return 1; +} +static void defer_replay(void) { + if (g_defer_replaying) return; + int any = 0; for (int i = 0; i < 4; i++) if (g_defer_slots[i].active && g_defer_slots[i].at_q) any = 1; + if (!any) return; + typeof(S) save; memcpy(&save, &S, sizeof S); p_scissor sc = R.sc; int has_sc = R.has_sc; int vis = g_vis_counting; + const uint8_t *en0 = g_exec_next; g_defer_replaying = 1; g_exec_next = NULL; + for (int gi, after = 0; (gi = defer_next(1, after)) >= 0;) { defer_t *d = &g_defer_slots[gi]; after = d->born; // In creation order: the groups' P ranges do not overlap and are arranged in sequence, so the group order is the original draw order + g_defer_cur = d; if (d->reset_between) g_vis_counting = 0; + { static int once; if (!once++) MLOG("推迟合并通道:首例 通道 %llu,组 %d 回放绘制 %d 次", g_stat_pass, gi, d->n); } // With multiple groups, several groups may already have been created before the first replay, so it cannot be judged by g_defer_n == 1 + for (int i = 0; i < d->n; i++) { defer_snap *z = &d->snap[i]; static uint8_t lm[8]; for (int j = 0; j < 8; j++) lm[j] = d->lmap[j] == 0xff || d->lmap[j] >= 8 ? 0xff : z->pm[d->lmap[j]]; + memcpy(&S, z->S_, sizeof S); args_dirty_all(); R.sc = z->sc; R.has_sc = z->has_sc; g_pso_dirty = g_dss_dirty = 1; g_pso_lmap = lm; draw(z->idx ? NULL : &z->dw, z->idx ? &z->di : NULL); } + defer_release_one(d); + } + g_vis_counting = vis; memcpy(&S, &save, sizeof S); args_dirty_all(); R.sc = sc; R.has_sc = has_sc; g_pso_lmap = NULL; g_pso_fsfn = nil; memset(&g_pso_fz, 0, sizeof g_pso_fz); g_pso_dirty = g_dss_dirty = 1; g_exec_next = en0; g_defer_replaying = 0; g_defer_cur = &g_defer_slots[0]; +} +static void defer_fallback(void) { + if (g_defer_replaying || !defer_has_active()) return; + g_defer_fallbacks++; MLOG("【红】推迟合并通道:提交结束仍有未回放组,按各组 P 原目标兜底"); + if (g_enc) { enc_r_end(g_enc); g_enc = nil; } + typeof(S) save; memcpy(&save, &S, sizeof S); p_set_rt rt = R.rt; g_defer_replaying = 1; + for (int gi, after = 0; (gi = defer_next(0, after)) >= 0;) { defer_t *d = &g_defer_slots[gi]; after = d->born; g_defer_cur = d; R.rt = d->p_rt; R.has_rt = 1; memcpy(R.clear_c, d->clear_c, 8); memcpy(R.cc, d->cc, sizeof R.cc); + R.clear_d = d->clear_d; R.clear_s = d->clear_s; R.cd = d->cd; R.cs = d->cs; + for (int i = 0; i < d->n; i++) { defer_snap *z = &d->snap[i]; memcpy(&S, z->S_, sizeof S); args_dirty_all(); R.sc = z->sc; R.has_sc = z->has_sc; g_pso_dirty = g_dss_dirty = 1; g_pso_lmap = z->pm; draw(z->idx ? NULL : &z->dw, z->idx ? &z->di : NULL); } + rp_end(); defer_release_one(d); + } + memcpy(&S, &save, sizeof S); args_dirty_all(); R.rt = rt; g_defer_replaying = 0; g_defer_cur = &g_defer_slots[0]; +} +static const uint8_t *defer_cont(int *ci) { // The next segment after a list ends (within the same submit): first the caller return stack, then the remaining handles of this submit; returns NULL when there are none left + for (;;) { int k = (*ci)++; + if (k < g_ret_n) { if (g_ret_n > 16) return NULL; return g_ret[g_ret_n - 1 - k]; } + k -= g_ret_n; if (k < g_nrest) return (const uint8_t *)g_rest[k]; + return NULL; } +} +// Deferral pre-screen (binary search on the store lookahead index, zero scanning): for every color subresource of P, the first "used as attachment / cleared" event after this draw must be "used as attachment under a different SetRenderTargets, +// whose targets cover all of P's colors"; cleared first / never used as attachment again in this submit -> deferral is impossible, return directly. Read events carry no level; handed to the full lookahead to check by level. +static int defer_pre(void) { + if (!g_sdx_ok || !g_exec_cur || !g_exec_seq || g_sdx_op[g_exec_seq - 1] != g_exec_cur) return 0; + uint32_t P = g_exec_seq - 1; + for (int k = 0; k < R.rt.n; k++) { if (!R.rt.color[k]) continue; + uint64_t ser; int lv, sl; rt_sub(&R.rt, k, &ser, &lv, &sl); + int b = sdx_bucket(ser, 0); if (b < 0) return 0; + uint32_t lo = g_sdx_bst[b], end = g_sdx_bst[b + 1], hi = end; + while (lo < hi) { uint32_t m = (lo + hi) / 2; if (g_sdx[m].seq <= P) lo = m + 1; else hi = m; } + int ok = 0, same = 0; uint32_t walk = 0, prt = lo > g_sdx_bst[b] && g_sdx[lo - 1].seq == P ? g_sdx[lo - 1].rtseq : SDX_NONE; // SetRenderTargets sequence number of P's own render pass (taken from this draw's used-as-attachment event) + for (uint32_t j = lo; j < end; j++) { const sdx_ev *e = &g_sdx[j]; + if (++walk > 64) return 0; // Too many events (a heavily sampled texture): not taken as a candidate, conservative and avoids a long walk + if (g_hoist_n && hoist_skipped(e->op)) continue; + if (e->kind == SDX_READ || e->kind == SDX_NEED) continue; // Read events carry no level (reading another level is not a conflict); handed to the full lookahead to check by level + if (e->plane != 0 || e->lv != lv || e->sl != sl) continue; + if (e->kind == SDX_CLEAR) return 0; + if (e->rtseq == SDX_NONE || e->rtseq >= g_sdx_nop) return 0; + const uint8_t *rop = g_sdx_op[e->rtseq]; const p_set_rt *r = (const p_set_rt *)(rop + 8); + if (rt_same(r, &R.rt)) { if (e->rtseq != prt) same = 1; continue; } // Same target: one immediately following is a continuation segment of P, one separated by other render passes is Q; the pre-screen does not distinguish them, and if there is still no other candidate at the end it is handed to the full lookahead + for (int i = 0; i < R.rt.n; i++) { if (!R.rt.color[i]) continue; int f = 0; + for (int q = 0; q < r->n && !f; q++) if (r->color[q] && sub_eq(&R.rt, i, r, q)) f = 1; + if (!f) return 0; } + ok = 1; break; } + if (!ok && !same) return 0; } + return 1; +} +static void defer_try(const p_draw *dw, const p_draw_idx *di) { + if (!defer_on() || defer_has_in_p() || g_defer_replaying || g_fz.active || g_fz_checking || !g_exec_next || !R.rt.n) return; + int slot = defer_slot_free(); if (slot < 0) return; + g_defer_cur = &g_defer_slots[slot]; + int ncol = 0; for (int i = 0; i < R.rt.n; i++) ncol += R.rt.color[i] != NULL; if (!ncol) return; // Render passes with only depth-stencil attached (shadow maps etc.): Q is defined as covering P's colors, so a P without color can never be deferred -- previously the pre-screen let it through idly and the full lookahead scanned dozens of draws for nothing + int has_clear = R.clear_d || R.clear_s; for (int i = 0; i < 8; i++) has_clear |= R.clear_c[i]; if (!has_clear) return; + if (!defer_pre()) return; + memset(&g_defer, 0, sizeof g_defer); g_defer.p_rt = R.rt; memcpy(g_defer.clear_c, R.clear_c, 8); memcpy(g_defer.cc, R.cc, sizeof R.cc); + g_defer.clear_d = R.clear_d; g_defer.clear_s = R.clear_s; g_defer.cd = R.cd; g_defer.cs = R.cs; + uint64_t tex[6][NTEX], img[6][32]; memcpy(tex, S.tex, sizeof tex); memcpy(img, S.img, sizeof img); const prog_t *pg = S.prog; ds_st ds = S.ds; + const uint8_t *p = g_exec_next, *p_end = NULL, *qop = NULL; const p_set_rt *q = NULL; p_set_rt scan_rt = R.rt; int srt_hit = -1, ci = 0, draws = 1, p_has_draw = 1; defer_collect_reads(&g_defer, pg, tex, img); // The current draw is P's first + for (int guard = 0; guard < 200000; guard++) { + op_hdr h; memcpy(&h, p, sizeof h); const void *a = p + 8; + if (g_hoist_n && hoist_skipped(p)) { p += h.size; continue; } + switch (h.op) { + case OP_END: { const uint8_t *nx = defer_cont(&ci); if (!nx) { g_defer.reason = 1; goto fail; } p = nx; continue; } + case OP_CALL: g_defer.reason = 2; goto fail; + case OP_JUMP: memcpy(&p, a, 8); continue; + case OP_BIND_PROGRAM: pg = ((const p_bind_program *)a)->prog; break; + case OP_BIND_TEX: case OP_BIND_TEX1: case OP_BIND_IMG: { p_bind_handles one; const p_bind_handles *b = bind_rec(h.op, a, &one); bh_fresh(b); int lim = h.op == OP_BIND_IMG ? 32 : NTEX; + if (b->stage >= 0 && b->stage < 6 && b->first >= 0 && b->first + b->n <= lim) for (int k = 0; k < b->n; k++) { if (h.op == OP_BIND_IMG) img[b->stage][b->first + k] = b->h[k]; else tex[b->stage][b->first + k] = b->h[k]; } break; } + case OP_BIND_STATE: { const p_bind_state *b = a; if (b->kind == 5) memcpy(&ds, b->bytes, sizeof ds); break; } + case OP_DRAW: case OP_DRAW_IDX: + if (defer_prog_hits(pg, tex, img, &g_defer.p_rt, 1, 1)) { /* Suggestion: here, after identifying that an output mapping is needed but _bf is missing, call bf_want_note. */ g_defer.reason = 7; goto fail; } + if (p_end) { // Draws in the intermediate range: must not write textures P reads, must not write P's depth-stencil (and must not test stencil when P writes stencil) + if (srt_hit < 0) { srt_hit = 0; for (int k = 0; k < scan_rt.n && !srt_hit; k++) if (scan_rt.color[k] && defer_rt_hits_reads(&scan_rt, k)) srt_hit = 1; } // Attachments change only at SetRenderTargets: computed once per segment + if (srt_hit) { g_defer.reason = 12; goto fail; } + if (scan_rt.depth && g_defer.p_rt.depth && scan_rt.depth->serial == g_defer.p_rt.depth->serial && (ds.dw || (g_defer.p_st && ds.st))) { g_defer.reason = 13; goto fail; } + break; } + draws++; if (draws > 64) { g_defer.reason = 20; goto fail; } p_has_draw = 1; defer_collect_reads(&g_defer, pg, tex, img); g_defer.p_dw |= ds.dw; g_defer.p_st |= ds.st && ((S.st_wmask[0] | S.st_wmask[1]) != 0) && (ds.fail_f != NVN_STENCIL_OP_KEEP || ds.zfail_f != NVN_STENCIL_OP_KEEP || ds.zpass_f != NVN_STENCIL_OP_KEEP || ds.fail_b != NVN_STENCIL_OP_KEEP || ds.zfail_b != NVN_STENCIL_OP_KEEP || ds.zpass_b != NVN_STENCIL_OP_KEEP); if (g_defer.p_dw) { g_defer.reason = 8; goto fail; } break; + case OP_RESET_COUNTER: g_defer.reset_between = 1; break; + case OP_COPY_T2T: { const p_copy_t2t *c = a; // Texture copy: allowed only if neither end touches P's attachments and the destination is not a texture P samples + uint64_t ss = c->src ? c->src->serial : 0, ds_ = c->dst ? c->dst->serial : 0; int bad = !ss || !ds_; + for (int k = 0; k < g_defer.p_rt.n && !bad; k++) if (g_defer.p_rt.color[k] && (g_defer.p_rt.color[k]->serial == ss || g_defer.p_rt.color[k]->serial == ds_)) bad = 1; + if (g_defer.p_rt.depth && (g_defer.p_rt.depth->serial == ss || g_defer.p_rt.depth->serial == ds_)) bad = 1; + for (int k = 0; k < g_defer.nreads && !bad; k++) if (h_ser(g_defer.p_reads[k]) == ds_) bad = 1; + if (g_defer.nreads > 512) bad = 1; + if (bad) { g_defer.reason = 3; g_defer.reason_op = h.op; goto fail; } break; } + case OP_REPORT_COUNTER: case OP_RENDER_COND: case OP_UPDATE_UB: case OP_COPY_B2B: case OP_CLEAR_BUF: case OP_COPY_B2T: case OP_COPY_T2B: case OP_CLEAR_TEX: case OP_DISPATCH: g_defer.reason = 3; g_defer.reason_op = h.op; goto fail; + case OP_CLEAR_COLOR: { const p_clear_color *c = a; if (!p_end) { g_defer.reason = 16; goto fail; } // A clear inside P's continuation segment: do not defer + if (c->index >= 0 && c->index < scan_rt.n && (defer_rt_hits_reads(&scan_rt, c->index) || (scan_rt.color[c->index] && defer_rt_hits_reads(&g_defer.p_rt, c->index)))) { g_defer.reason = 5; goto fail; } break; } + case OP_CLEAR_DS: if (!p_end) { g_defer.reason = 16; goto fail; } break; + case OP_SET_RT: { const p_set_rt *r = a; + if (!p_end && rt_same(r, &g_defer.p_rt)) break; // The same target set once more (the game often sends it repeatedly): still a continuation segment of P, and its draws are deferred together + if (!p_end) p_end = p; int shared = 0; for (int i = 0; i < g_defer.p_rt.n; i++) for (int j = 0; j < r->n; j++) if (g_defer.p_rt.color[i] && r->color[j] && sub_eq(&g_defer.p_rt, i, r, j)) shared = 1; + if (shared) { // An empty render pass (targets set, then changed again without drawing or clearing) touches no attachment: it is neither taken as Q nor counted as a conflict + const uint8_t *e = p + h.size; int ec = ci, empty = 0; + for (int g2 = 0; g2 < 200000; g2++) { op_hdr he; memcpy(&he, e, sizeof he); + if (he.op == OP_JUMP) { memcpy(&e, e + 8, 8); continue; } + if (he.op == OP_END) { const uint8_t *nx = defer_cont(&ec); if (!nx) break; e = nx; continue; } + if (he.op == OP_SET_RT) { empty = 1; break; } + if (he.op == OP_DRAW || he.op == OP_DRAW_IDX || he.op == OP_CLEAR_COLOR || he.op == OP_CLEAR_DS || he.op == OP_CALL || he.op == OP_DISPATCH || he.op == OP_CLEAR_TEX || he.op == OP_COPY_T2T || he.op == OP_COPY_B2T || he.op == OP_COPY_T2B) break; + e += he.size; } + if (empty) break; } + if (!shared) break; if (!p_has_draw || !defer_q_superset(r)) { g_defer.reason = 6; goto fail; } q = r; qop = p; goto q_found; } + default: break; + } + if (h.op == OP_SET_RT) { scan_rt = *(const p_set_rt *)a; srt_hit = -1; } + p += h.size; + } +fail: + if (g_defer.reason && passdump()) PD("G %llu 不成立 reason=%d op=%d", g_stat_pass + 1, g_defer.reason, g_defer.reason_op); memset(&g_defer, 0, sizeof g_defer); return; +q_found: + p = qop; op_hdr qh; memcpy(&qh, p, sizeof qh); p += qh.size; int q_body = 0, q_clears = 0; // Binding tracking continues from the main scan's state (does not go back to S at the start of P) + for (int guard = 0; guard < 200000; guard++) { op_hdr h; memcpy(&h, p, sizeof h); const void *a = p + 8; if (h.op == OP_JUMP) { memcpy(&p, a, 8); continue; } + // The whole Q segment (up to the next SetRenderTargets / end of list): no draw may sample P's color attachments through a view covering P's level -- after deferral P's result is still in the tile and has not landed in main memory, so sampling would read old content + if (h.op == OP_DRAW || h.op == OP_DRAW_IDX) { if (defer_prog_hits(pg, tex, img, &g_defer.p_rt, 1, 1)) { g_defer.reason = 14; goto fail; } q_body = 1; p += h.size; continue; } + if (q_body && (h.op == OP_SET_RT || h.op == OP_END)) break; + if (!q_body && h.op == OP_END) { const uint8_t *nx = defer_cont(&ci); if (!nx) { g_defer.reason = 9; goto fail; } p = nx; continue; } + if (h.op == OP_CALL) { g_defer.reason = 9; g_defer.reason_op = h.op; goto fail; } + // After Q's SetRenderTargets and before its first draw and any clear, another SetRenderTargets arrives (the game sets targets repeatedly, the previous one being an empty render pass): if the new targets still cover P take it as Q, otherwise do not defer + if (!q_body && h.op == OP_SET_RT && !q_clears) { const p_set_rt *r2 = a; if (!defer_q_superset(r2)) { g_defer.reason = 15; goto fail; } q = r2; qop = p; p += h.size; continue; } + if (h.op == OP_CLEAR_COLOR || h.op == OP_CLEAR_DS) q_clears = 1; + if (!q_body && (h.op == OP_END || h.op == OP_SET_RT || h.op == OP_UPDATE_UB || h.op == OP_COPY_B2B || h.op == OP_CLEAR_BUF || h.op == OP_DISPATCH)) { g_defer.reason = 9; g_defer.reason_op = h.op; goto fail; } + if (h.op == OP_BIND_PROGRAM) pg = ((const p_bind_program *)a)->prog; else if (h.op == OP_BIND_TEX || h.op == OP_BIND_TEX1 || h.op == OP_BIND_IMG) { p_bind_handles one; const p_bind_handles *b = bind_rec(h.op, a, &one); bh_fresh(b); int lim = h.op == OP_BIND_IMG ? 32 : NTEX; if (b->stage >= 0 && b->stage < 6 && b->first >= 0 && b->first + b->n <= lim) for (int k = 0; k < b->n; k++) { if (h.op == OP_BIND_IMG) img[b->stage][b->first + k] = b->h[k]; else tex[b->stage][b->first + k] = b->h[k]; } } + else if (h.op == OP_CLEAR_COLOR) { const p_clear_color *c = a; if (c->index >= 0 && c->index < q->n && q->color[c->index]) for (int i = 0; i < g_defer.p_rt.n; i++) if (g_defer.p_rt.color[i] && sub_eq(q, c->index, &g_defer.p_rt, i)) { g_defer.reason = 10; goto fail; } } + p += h.size; + } + if (!q || !defer_q_superset(q) || g_defer.n >= 64) { g_defer.reason = 11; goto fail; } + for (int i = 0; i < 4; i++) if (&g_defer_slots[i] != &g_defer && g_defer_slots[i].active && g_defer_slots[i].q_op == qop && !defer_same_q_depth(&g_defer_slots[i], q)) { g_defer.reason = 17; goto fail; } + g_defer.q_rt = *q; if (!q->depth && g_defer.p_rt.depth) { g_defer.q_rt.depth = g_defer.p_rt.depth; g_defer.q_rt.dview = g_defer.p_rt.dview; g_defer.q_rt.has_dview = g_defer.p_rt.has_dview; } { int na = 0; for (int i = 0; i < 4; i++) { const defer_t *d = &g_defer_slots[i]; if (d != &g_defer && d->active && d->at_q) na += d->n; } + if (na + 1 > 64) { g_defer.reason = 20; goto fail; } } + g_defer.q_op = qop; g_defer.p_end_op = p_end; g_defer.in_p = 1; g_defer.active = 1; g_defer_nact++; g_defer_pend = p_end; g_defer_n++; g_defer.born = g_defer_n; + // Chained: this render pass is exactly the Q of upstream groups (they are at_q, waiting to be replayed when this render pass opens), and this render pass is itself deferred as a whole -- the upstream groups are merged into the new group in creation order: + // the target space of an upstream group = this render pass's targets = the new group's P targets, and the snapshot's mapping is composed as pm' = upstream lmap o pm; the upstream clears are already in R and are carried along with the new group below + for (int gi, after = 0; (gi = defer_next(1, after)) >= 0;) { defer_t *d = &g_defer_slots[gi]; after = d->born; if (d == &g_defer) continue; + for (int k = 0; k < 8; k++) if (d->lmap[k] != 0xff && d->lmap[k] != k) { g_defer.reason = 18; goto fail; } // Only merge in upstream groups with an identity mapping (see defer_q_superset) + for (int i = 0; i < d->n; i++) { defer_snap z = d->snap[i]; uint8_t pm[8]; for (int k = 0; k < 8; k++) pm[k] = d->lmap[k] == 0xff || d->lmap[k] >= 8 ? 0xff : z.pm[d->lmap[k]]; + memcpy(z.pm, pm, 8); g_defer.snap[g_defer.n++] = z; } + g_defer.reset_between |= d->reset_between; d->n = 0; defer_release_one(d); } if (!defer_take(dw, di)) { defer_release_one(&g_defer); return; } + memset(R.clear_c, 0, sizeof R.clear_c); R.clear_d = R.clear_s = 0; PD("G %llu 成立 组 %d P→Q draws=%d", g_stat_pass + 1, slot, draws); +} +// Pre-screen of the necessary condition for merging (binary search on the submit index, zero scanning on failure): merging requires that "the first draw of the later pass (the next SET_RT with a different target that has not been hoisted) +// samples some color attachment of the earlier pass", i.e. there is an SDX_READ event for that attachment at that draw's sequence number. If not satisfied there is certainly no merge, saving the per-operation scan below; +// only if satisfied does it go through the original planning (the decision is exactly the same as without the pre-screen). When the index is unavailable, plan as before. +static int fz_prefilter(void) { + if (!g_sdx_ok || !g_exec_seq || g_sdx_op[g_exec_seq - 1] != g_exec_cur) return 1; + uint32_t P = g_exec_seq - 1, lo = 0, hi = g_rtx_n; + while (lo < hi) { uint32_t m = (lo + hi) / 2; if (g_rtx_seq[m] <= P) lo = m + 1; else hi = m; } + for (; lo < g_rtx_n; lo++) { const uint8_t *op = g_sdx_op[g_rtx_seq[lo]]; if (g_hoist_n && hoist_skipped(op)) continue; if (!rt_same(&R.rt, (const p_set_rt *)(op + 8))) break; } + if (lo >= g_rtx_n) return 0; + uint32_t r2 = g_rtx_seq[lo], a = 0, b = g_drx_n; + while (a < b) { uint32_t m = (a + b) / 2; if (g_drx_seq[m] <= r2) a = m + 1; else b = m; } + if (a >= g_drx_n) return 0; + uint32_t fd = g_drx_seq[a]; + for (uint32_t k = lo + 1; k < g_rtx_n && g_rtx_seq[k] < fd; k++) if (!(g_hoist_n && hoist_skipped(g_sdx_op[g_rtx_seq[k]]))) return 0; // The target changes again before the first draw: step two of the original planning gives up as well + for (int j = 0; j < R.rt.n && j < 8; j++) { if (!R.rt.color[j]) continue; int bk = sdx_bucket(R.rt.color[j]->serial, 0); if (bk < 0) continue; + uint32_t e0 = g_sdx_bst[bk], e1 = g_sdx_bst[bk + 1]; + while (e0 < e1) { uint32_t m = (e0 + e1) / 2; if (g_sdx[m].seq < fd) e0 = m + 1; else e1 = m; } + for (; e0 < g_sdx_bst[bk + 1] && g_sdx[e0].seq == fd; e0++) if (g_sdx[e0].kind == SDX_READ) return 1; } + return 0; +} +static void fuse_try(void) { + if (g_fz.active || !g_exec_next || g_hoist_n + 12 > HOIST_MAX || !R.rt.n) return; + if (!fz_prefilter()) return; + // One: from the earlier pass to the next SetRenderTargets, only draws and state are allowed + const uint8_t *p = NULL, *r2op = NULL, *reuse[8]; int nreuse = 0; const p_set_rt *r2 = NULL; p_scissor sc = R.sc; int has_sc = R.has_sc; + fz_cursor cur = {.p = g_exec_next}; op_hdr h; const void *a; + for (int guard = 0; guard < 200000 && !r2; guard++) { + if (!fz_next(&cur, &p, &h, &a)) return; + switch (h.op) { + case OP_COPY_T2T: case OP_COPY_B2T: case OP_COPY_T2B: case OP_CLEAR_TEX: case OP_DISPATCH: case OP_RENDER_COND: + case OP_CLEAR_COLOR: case OP_CLEAR_DS: case OP_UPDATE_UB: case OP_COPY_B2B: case OP_CLEAR_BUF: return; + case OP_SCISSOR: sc = *(const p_scissor *)a; has_sc = 1; break; + case OP_SET_RT: { + int pend = R.clear_d || R.clear_s; for (int i = 0; i < 8; i++) pend |= R.clear_c[i]; + if (rt_same(&R.rt, a)) { + if (g_enc_mode == 1 || pend || !R.has_rt || !tbdr_on() || nreuse >= 8) return; + reuse[nreuse++] = p; continue; + } + r2 = a; r2op = p; continue; + } + default: break; + } + } + if (!r2 || rt_same(&R.rt, r2)) return; + // Two: from the start of the later pass to its first draw, only clears (not clearing shared attachments) and state are allowed + const uint8_t *cl[8]; int ncl = 0; uint8_t clr[8] = {0}; MTLClearColor cc[8]; int cd_f = 0, cs_f = 0; double cdv = 0; uint32_t csv = 0; + if (!fz_next(&cur, &p, &h, &a)) return; // Continue from the cursor of step one (already past the later pass's SET_RT, keeping the cross-handle / return stack position) + for (int guard = 0; guard < 200000; guard++) { + if (h.op == OP_DRAW || h.op == OP_DRAW_IDX) break; + switch (h.op) { + case OP_COPY_T2T: case OP_COPY_B2T: case OP_COPY_T2B: case OP_CLEAR_TEX: case OP_DISPATCH: case OP_SET_RT: case OP_UPDATE_UB: case OP_COPY_B2B: case OP_CLEAR_BUF: case OP_RENDER_COND: return; + case OP_SCISSOR: sc = *(const p_scissor *)a; has_sc = 1; break; + case OP_CLEAR_COLOR: { const p_clear_color *c = a; if (ncl >= 8 || c->index < 0 || c->index >= r2->n || !r2->color[c->index] || c->mask != 0xf) return; + const tex_t *t = r2->color[c->index]; + if (has_sc && !(sc.x <= 0 && sc.y <= 0 && sc.x + sc.w >= (int)t->d.width && sc.y + sc.h >= (int)t->d.height)) return; + clr[c->index] = 1; cc[c->index] = MTLClearColorMake(c->rgba[0], c->rgba[1], c->rgba[2], c->rgba[3]); cl[ncl++] = p; break; } + case OP_CLEAR_DS: { const p_clear_ds *c = a; if (ncl >= 8 || !r2->depth) return; + if (c->depth_mask) { cd_f = 1; cdv = c->depth; } if (c->stencil_mask) { cs_f = 1; csv = (uint32_t)c->stencil; } cl[ncl++] = p; break; } + default: break; + } + if (!fz_next(&cur, &p, &h, &a)) return; + } + // Three: union of attachments (the earlier pass's attachments keep their original indices, those unique to the later pass are appended), consistent sizes, compatible depth + p_set_rt U = R.rt; int n = R.rt.n; int8_t p2map[8]; memset(p2map, -1, sizeof p2map); + for (int k = 0; k < r2->n && k < 8; k++) { if (!r2->color[k]) continue; + int j = -1; for (int m = 0; m < R.rt.n; m++) if (R.rt.color[m] && sub_eq(&R.rt, m, r2, k)) { j = m; break; } + if (j < 0) { if (n >= 8) return; j = n++; U.color[j] = r2->color[k]; U.cview[j] = r2->cview[k]; U.has_cview[j] = r2->has_cview[k]; } + else if (clr[k]) return; // The start of the later pass wants to clear an attachment the earlier pass is drawing to: after merging this clear has nowhere to go + p2map[k] = (int8_t)j; } + U.n = n; + { uint64_t s0; int l0, x0; rt_sub(&U, 0, &s0, &l0, &x0); if (!U.color[0]) return; + uint32_t w0 = MAX(U.color[0]->d.width >> l0, 1u), h0 = MAX(U.color[0]->d.height >> l0, 1u); + for (int j = 1; j < n; j++) { uint64_t s_; int l_, x_; rt_sub(&U, j, &s_, &l_, &x_); if (!U.color[j] || MAX(U.color[j]->d.width >> l_, 1u) != w0 || MAX(U.color[j]->d.height >> l_, 1u) != h0) return; } } + int depth_from_r2 = 0; + if (R.rt.depth && r2->depth && !sub_eq(&R.rt, 8, r2, 8)) return; + if (!R.rt.depth && r2->depth) { U.depth = r2->depth; U.dview = r2->dview; U.has_dview = r2->has_dview; depth_from_r2 = 1; } + if ((cd_f || cs_f) && !depth_from_r2) return; // The depth is already in use in the earlier pass: the depth clear at the start of the later pass has nowhere to go + // Four: draws of the earlier pass must not sample attachments brought in by the later pass; when the depth comes from the later pass, depth-stencil testing must not be enabled + static uint64_t tex[6][NTEX], img[6][32]; memcpy(tex, S.tex, sizeof tex); memcpy(img, S.img, sizeof img); + const prog_t *pg = S.prog; ds_st ds = S.ds; + cur = (fz_cursor){.p = g_exec_next}; + for (;;) { // The first loop iteration checks the draw that opens the render pass (current state), then checks the draws in the range one by one + if (depth_from_r2 && (ds.dt || ds.dw || ds.st)) return; + for (int j = R.rt.n; j < n; j++) if (prog_samples_ser(pg, tex, img, U.color[j]->serial)) return; + if (depth_from_r2 && prog_samples_ser(pg, tex, img, U.depth->serial)) return; + for (;;) { + const uint8_t *q; op_hdr hq; const void *aq; + if (!fz_next(&cur, &q, &hq, &aq)) return; + if (q == r2op) goto planned; + if (hq.op == OP_BIND_PROGRAM) pg = ((const p_bind_program *)aq)->prog; + else if (hq.op == OP_BIND_STATE) { const p_bind_state *b = aq; if (b->kind == 5) memcpy(&ds, b->bytes, sizeof ds); } + else if (hq.op == OP_BIND_TEX || hq.op == OP_BIND_TEX1 || hq.op == OP_BIND_IMG) { p_bind_handles one; const p_bind_handles *b = bind_rec(hq.op, aq, &one); bh_fresh(b); int lim = hq.op == OP_BIND_IMG ? 32 : NTEX; + if (b->stage >= 0 && b->stage < 6 && b->first >= 0 && b->first + b->n <= lim) for (int k = 0; k < b->n; k++) { if (hq.op == OP_BIND_IMG) img[b->stage][b->first + k] = b->h[k]; else tex[b->stage][b->first + k] = b->h[k]; } } + else if (hq.op == OP_DRAW || hq.op == OP_DRAW_IDX) break; + } + } +planned: + { // Five: pre-screen the first draw of the later pass (static conditions), to avoid attaching the union of attachments for nothing: track the program and texture bindings up to that draw + const prog_t *pg2 = S.prog; static uint64_t t2[6][NTEX], i2[6][32]; memcpy(t2, S.tex, sizeof t2); memcpy(i2, S.img, sizeof i2); + cur = (fz_cursor){.p = g_exec_next}; + for (;;) { const uint8_t *q; op_hdr hq; const void *aq; if (!fz_next(&cur, &q, &hq, &aq)) return; if (q == p) break; + if (hq.op == OP_BIND_PROGRAM) pg2 = ((const p_bind_program *)aq)->prog; + else if (hq.op == OP_BIND_TEX || hq.op == OP_BIND_TEX1 || hq.op == OP_BIND_IMG) { p_bind_handles one; const p_bind_handles *b = bind_rec(hq.op, aq, &one); bh_fresh(b); int lim = hq.op == OP_BIND_IMG ? 32 : NTEX; + if (b->stage >= 0 && b->stage < 6 && b->first >= 0 && b->first + b->n <= lim) for (int k = 0; k < b->n; k++) { if (hq.op == OP_BIND_IMG) i2[b->stage][b->first + k] = b->h[k]; else t2[b->stage][b->first + k] = b->h[k]; } } + } + NvProg *pr2 = pg2 ? prog_get(pg2) : nil; NvShader *vs2 = pr2 ? pr2->sh[0] : nil, *fs2 = pr2 ? pr2->sh[1] : nil; + if (!vs2 || !fs2) return; + int touches = 0; + for (int st = 0; st < 5; st++) { __unsafe_unretained NvShader *sh = pr2->sh[st]; if (!sh || sh->stage >= 6) continue; + for (int i = 0; i < sh->rf.nb; i++) { const nvn_rbind *bb = &sh->rf.b[i]; uint64_t h; + if (bb->kind == NVN_RB_IMG) h = bb->reg < 32 ? i2[sh->stage][bb->reg] : 0; else if (bb->kind == NVN_RB_TEX || bb->kind == NVN_RB_TBUF) h = bb->reg < NTEX ? t2[sh->stage][bb->reg] : 0; else continue; + uint64_t ser = h ? h_ser(h) : 0; if (!ser) continue; + int j = -1; for (int m = 0; m < R.rt.n; m++) if (R.rt.color[m] && R.rt.color[m]->serial == ser) { j = m; break; } + if (j < 0) continue; + if (sh != fs2 || bb->kind != NVN_RB_TEX || (fs2->rf.fc_mixed[bb->reg >> 6] >> (bb->reg & 63) & 1)) return; + int any = 0; + for (int c = 0; c < fs2->rf.nfc; c++) if (fs2->rf.fc_reg[c] == bb->reg) { int l = fs2->rf.fc_locn[c]; + if (l >= 32 || (vs2->rf.uv_attr[l] < 0 && vs2->rf.uv_aff_attr[l] < 0)) return; any = 1; } + if (!any) return; + touches = 1; } } + if (!touches) return; // The later pass does not read the earlier pass's attachments: merging has no benefit + int ident = 1; for (int c = 0; c < 8; c++) if (fs2->rf.color_mask >> c & 1) { if (p2map[c] < 0) return; if (p2map[c] != c) ident = 0; } + if (!ident && !fs2->rf.nfo) return; + } + memset(&g_fz, 0, sizeof g_fz); + g_fz.active = 1; g_fz.phase = 1; g_fz.n1 = R.rt.n; g_fz.nreuse = nreuse; g_fz.rt1 = R.rt; g_fz.rt2 = *r2; g_fz.rt2_op = r2op; memcpy(g_fz.reuse, reuse, sizeof reuse); memcpy(g_fz.p2map, p2map, 8); + for (int j = 0; j < 8; j++) g_fz.lmap1[j] = j < R.rt.n ? (uint8_t)j : 0xff; + fz_probe_cov2(r2, p, p2map, clr, g_fz.n1, n); + g_fz.skip[g_fz.nskip++] = r2op; for (int i = 0; i < ncl; i++) g_fz.skip[g_fz.nskip++] = cl[i]; + for (int i = 0; i < g_fz.nskip; i++) g_hoist_skip[g_hoist_n++] = g_fz.skip[i]; + R.rt = U; + memcpy(g_fz.clr2, clr, 8); memcpy(g_fz.cc2, cc, sizeof cc); g_fz.cd2f = (uint8_t)cd_f; g_fz.cs2f = (uint8_t)cs_f; g_fz.cd2 = cdv; g_fz.cs2 = csv; + for (int k = 0; k < 8; k++) if (clr[k]) { R.clear_c[p2map[k]] = 1; R.cc[p2map[k]] = cc[k]; } + if (cd_f) { R.clear_d = 1; R.cd = cdv; } if (cs_f) { R.clear_s = 1; R.cs = csv; } + if (!g_fz_n++) MLOG("可读 tile 通道合并:首例 通道 %llu(物理附件 %d 个)", g_stat_pass + 1, n); + PD("F %llu 合并下一个通道(可读 tile):物理附件 %d 个,前一个通道 %d 个", g_stat_pass + 1, n, R.rt.n == n ? g_fz.n1 : g_fz.n1); +} +static id bf_fn(NvShader *fs, const int *fx, uint8_t fbmask, const uint8_t *lmap) { // The readable tile version is specialized by constants (cached) + os_unfair_lock_lock(&g_fz_fn_lock); + if (!g_bf_fn_cache) { g_bf_fn_cache = [NSMutableDictionary dictionary]; g_bf_lib_cache = [NSMutableDictionary dictionary]; } + size_t kl = strlen(fs->key); NSString *base = [NSString stringWithFormat:@"%.*s", (int)(kl > 2 ? kl - 2 : kl), fs->key]; + id lib = g_bf_lib_cache[base]; + if (!lib) { + NSError *e = nil; NSString *path = [NSString stringWithFormat:@"%@/%@_bf.metallib", g_msl_dir, base]; + lib = lib_from_file(path, &e); + if (!lib) MLOG("【红】可读 tile 版本载入失败 %s:%s", path.UTF8String, e.localizedDescription.UTF8String); + else g_bf_lib_cache[base] = lib; + } + if (!lib) { os_unfair_lock_unlock(&g_fz_fn_lock); return nil; } + NSMutableString *k = [NSMutableString stringWithFormat:@"%02x", fbmask]; + for (int i = 0; i < fs->rf.nfc; i++) [k appendFormat:@",%d", fx[i]]; + for (int j = 0; j < 8; j++) [k appendFormat:@"/%02x", lmap[j]]; + NSString *cacheKey = [NSString stringWithFormat:@"%@|%@", base, k]; + id f = g_bf_fn_cache[cacheKey]; + if (f) { os_unfair_lock_unlock(&g_fz_fn_lock); return f; } + MTLFunctionConstantValues *cv = [MTLFunctionConstantValues new]; + for (int i = 0; i < fs->rf.nfc; i++) { int v = fx[i]; [cv setConstantValue:&v type:MTLDataTypeInt atIndex:(NSUInteger)i]; } + for (int q = 0; q < 8; q++) { bool b = fbmask >> q & 1; [cv setConstantValue:&b type:MTLDataTypeBool atIndex:(NSUInteger)(32 + q)]; } + for (int j = 0; j < fs->rf.nfo; j++) for (int q = 0; q < 8; q++) { bool b = lmap[q] == fs->rf.fo_color[j]; [cv setConstantValue:&b type:MTLDataTypeBool atIndex:(NSUInteger)(64 + 8 * j + q)]; } + NSError *e = nil; f = [lib newFunctionWithName:@"main0" constantValues:cv error:&e]; + if (!f) { MLOG("【红】可读 tile 版本特化失败 %s(%s):%s", fs->key, k.UTF8String, e.localizedDescription.UTF8String); os_unfair_lock_unlock(&g_fz_fn_lock); return nil; } + g_bf_fn_cache[cacheKey] = f; os_unfair_lock_unlock(&g_fz_fn_lock); return f; +} +static int ubo_float(int slot, uint16_t ref, float *out) { // Component cmp of vector vec of the uniform slot (read on the CPU side; not read if it falls within a buffer write pending execution in this submit) + int sl = ref >> 8, vec = ref >> 2 & 63, cmp = ref & 3; + if (slot < 0 || slot >= 6 || sl < 0 || sl >= 16) return 0; + const p_bind_buf *u = &S.ubo[slot][sl]; uint64_t off = (uint64_t)vec * 16 + (uint64_t)cmp * 4; + if (!u->addr || off + 4 > u->size || pend_hits(u->addr + off, 4)) return 0; + void *mb; uint64_t mo; const uint8_t *base; + if (!mtl_addr(u->addr + off, 4, &mb, &mo) || !(base = [(__bridge id)mb contents])) return 0; + memcpy(out, base + mo, 4); return 1; +} +static int zskip_fact_for(const char *key, zskip_fact *out) { + if (!key) return 0; + for (int i = 0; i < g_zskip_nf; i++) if (!strncmp(key, g_zskip_facts[i].key, strlen(g_zskip_facts[i].key))) { *out = g_zskip_facts[i]; return 1; } // Runtime keys carry the _b suffix + return 0; +} +static int zskip_view_zero(int tid) { + if (!tid || tid >= g_ntreg || !g_treg[tid].ok) return 0; + treg_t *r = &g_treg[tid]; const tex_t *t = &r->t; int lv0 = r->has_v && r->v.set_levels ? r->v.base_level : 0; + int nl = r->has_v && r->v.set_levels && r->v.nlevels > 0 ? r->v.nlevels : 1; + int sl0 = r->has_v && r->v.set_layers ? r->v.min_layer : 0; + int ns = r->has_v && r->v.set_layers && r->v.nlayers > 0 ? r->v.nlayers : 1; + for (int l = 0; l < nl; l++) for (int s = 0; s < ns; s++) if (!zskip_has(t->serial, lv0 + l, sl0 + s)) return 0; + return 1; +} +// While an occlusion query is counting, a skipped draw should have contributed passed samples: it only does not matter if "there is no report before the next reset". +// Looking forward in replay order from after this draw: sample count reset encountered first -> does not matter (return 1); sample count report encountered first, or the end cannot be seen (submit ends) -> matters (return 0) +static int zskip_vis_dead(void) { + if (!g_vis_counting) return 1; + const uint8_t *p = g_exec_next; int ci = 0; if (!p) return 0; + for (int guard = 0; guard < 200000; guard++) { op_hdr h; memcpy(&h, p, sizeof h); const void *a = p + 8; + if (h.op == OP_JUMP) { memcpy(&p, a, 8); continue; } + if (h.op == OP_END) { p = defer_cont(&ci); if (!p) return 0; continue; } + if (h.op == OP_CALL) return 0; + if (h.op == OP_RESET_COUNTER || h.op == OP_REPORT_COUNTER) { uint64_t t; memcpy(&t, a, 8); if (t == NVN_COUNTER_TYPE_SAMPLES_PASSED) return h.op == OP_RESET_COUNTER; } + p += h.size; } + return 0; +} +static int zskip_can_draw(NvProg *pr) { + // Skip conditions (all must hold): the fragment key is in the fact table, the view bound to every texture slot declared by its reflection covers only subresources known to be all zero, the coefficient is finite, + // no depth write and no stencil, not counting an occlusion query, and every color attachment to be written is already 0 (a candidate cleared entirely to 0 in this render pass, or known all zero) -- writing 0 over 0. + // Reasons for not holding (the N line of PASSDUMP): 1 depth / stencil 2 occlusion query counting with a report before the reset 3 coefficient unreadable or non-finite 4 image slot 5 sampled view not known all zero 6 no target 7 target is not 0 + if (!zskip_on() || !pr) return 0; + NvShader *fs = nil; for (int st = 0; st < 5; st++) if (pr->sh[st] && pr->sh[st]->stage == 1) fs = pr->sh[st]; + zskip_fact f; float coeff; int why = 0; + if (!fs || !zskip_fact_for(fs->key, &f)) return 0; + if (S.ds.dw || S.ds.st) why = 1; + else if (!zskip_vis_dead()) why = 2; + else if (f.ubo_slot >= 0 && (!ubo_float(1, (uint16_t)((f.ubo_slot << 8) | (f.vec << 2) | f.comp), &coeff) || !isfinite(coeff))) why = 3; // ubo_slot < 0: this shader has no coefficient + for (int i = 0; !why && i < fs->rf.nb; i++) { nvn_rbind *b = &fs->rf.b[i]; + if (b->kind == NVN_RB_IMG) { why = 4; break; } + if (b->kind != NVN_RB_TEX && b->kind != NVN_RB_TBUF) continue; + int tid = b->reg < NTEX ? (int)(S.tex[1][b->reg] & 0xfffff) : 0; if (!zskip_view_zero(tid)) why = 5; } + if (!why && (!R.has_rt || !R.rt.n)) why = 6; + for (int i = 0; !why && i < R.rt.n && i < 8; i++) { if (!R.rt.color[i]) continue; + int pending = g_enc ? g_zskip_cand_ok[i] : zskip_zero_clear(i); // Render pass already open: look at the candidates noted when the render pass was opened (not yet drawn to); not open: look at the pending clears + if (!pending && (g_enc || R.clear_c[i] || !zskip_target_zero(R.rt.color[i], &R.rt.cview[i], R.rt.has_cview[i]))) why = 7; } + if (why) PD("N %llu 零源不跳过 reason=%d 集合 %d", g_stat_pass + 1, why, g_zskip_n); + return !why; +} +static int uv_proof(NvShader *vs, int locn, const p_draw *dw, const p_draw_idx *di) { // The interpolated output locn at every pixel center is exactly this pixel's texel center: u = (x + 1) / 2, v = (1 - y) / 2, w = 1, viewport and scissor cover the whole block + if (locn < 0 || locn >= 32 || vs->rf.pos_attr < 0) return 0; + int direct = vs->rf.uv_attr[locn] >= 0, aff = vs->rf.uv_aff_attr[locn] >= 0; + if (!direct && !aff) return 0; + float sc[4] = {1, 0, 1, 0}; // Pass-through as-is = scale 1, offset 0 + if (!direct) for (int q = 0; q < 4; q++) if (!ubo_float(0, vs->rf.uv_aff[locn][q], &sc[q])) return 0; + NSUInteger W = g_pass.tw, H = g_pass.th; + if (S.has_vp && !(S.vp[0][0] == 0 && S.vp[0][1] == 0 && S.vp[0][2] == (float)W && S.vp[0][3] == (float)H)) return 0; + if (R.has_sc && !(R.sc.x <= 0 && R.sc.y <= 0 && R.sc.x + R.sc.w >= (int)W && R.sc.y + R.sc.h >= (int)H)) return 0; + float P[64][4], Q[64][4]; int n = dw ? dw->count : di->count; + if (attr_read(vs->rf.pos_attr, dw, di, P) || attr_read(direct ? vs->rf.uv_attr[locn] : vs->rf.uv_aff_attr[locn], dw, di, Q)) return 0; + for (int k = 0; k < n; k++) { float w = vs->rf.pos_w1 ? 1.0f : P[k][3]; + float u = direct ? Q[k][0] : fmaf(Q[k][0], sc[0], sc[1]), v = direct ? Q[k][1] : fmaf(Q[k][1], sc[2], sc[3]); // The same fma as the shader + if (w != 1.0f || u != (P[k][0] + 1.0f) * 0.5f || v != (1.0f - P[k][1]) * 0.5f) { PD("F %llu uv 证明:顶点 %d 位置 %g,%g uv %g,%g(缩放 / 偏移 %g,%g,%g,%g)", g_stat_pass, k, P[k][0], P[k][1], u, v, sc[0], sc[1], sc[2], sc[3]); return 0; } } + return 1; +} +// One draw of the later pass: verify and produce the specialization. Returns 0 = cannot be proven (the caller ends the merged render pass and reopens with the original targets) +static int fz_p2_draw(NvProg *pr, const p_draw *dw, const p_draw_idx *di, uint8_t lmap[8], __strong id *fn) { + NvShader *vs = pr->sh[0], *fs = pr->sh[1]; *fn = nil; + if (!vs || !fs) { PD("F %llu 证不出:无顶点 / 片元着色器", g_stat_pass); return 0; } + if (!g_fz.rt2.depth && R.rt.depth && (S.ds.dt || S.ds.dw || S.ds.st)) { PD("F %llu 证不出:深度来自前一个通道而本次绘制开了深度模板测试", g_stat_pass); return 0; } + int fx[32]; for (int i = 0; i < 32; i++) fx[i] = -1; uint8_t fbmask = 0; + for (int st = 0; st < 5; st++) { __unsafe_unretained NvShader *sh = pr->sh[st]; if (!sh || sh->stage >= 6) continue; + for (int i = 0; i < sh->rf.nb; i++) { const nvn_rbind *b = &sh->rf.b[i]; uint64_t h; + if (b->kind == NVN_RB_IMG) h = b->reg < 32 ? S.img[sh->stage][b->reg] : 0; else if (b->kind == NVN_RB_TEX || b->kind == NVN_RB_TBUF) h = b->reg < NTEX ? S.tex[sh->stage][b->reg] : 0; else continue; + uint64_t ser = h ? h_ser(h) : 0; if (!ser) continue; + int j = -1; for (int m = 0; m < g_fz.n1; m++) if (R.rt.color[m] && R.rt.color[m]->serial == ser) { j = m; break; } + if (j < 0) continue; // What is sampled is not an attachment of the earlier pass + if (sh != fs || b->kind != NVN_RB_TEX) { PD("F %llu 证不出:非片元阶段或非贴图槽读前一个通道的附件", g_stat_pass); return 0; } + if (fs->rf.fc_mixed[b->reg >> 6] >> (b->reg & 63) & 1) { PD("F %llu 证不出:槽 %d 混用", g_stat_pass, b->reg); return 0; } + uint64_t s_; int lv, sl; rt_sub(&R.rt, j, &s_, &lv, &sl); + if (lv || sl || MAX(R.rt.color[j]->d.width, 1u) != g_pass.tw || MAX(R.rt.color[j]->d.height, 1u) != g_pass.th) { PD("F %llu 证不出:附件不是第 0 级第 0 层或尺寸不符", g_stat_pass); return 0; } + { // The sampled view must also be exactly the attachment itself (previously only the attachment being level 0 layer 0 was checked, not the view bound by the shader -- when the same texture is sampled through a view of level k / layer k / a changed format, + // the proof would still hold, the sampling would be changed to read on-tile level 0 / the original format, and the picture would be wrong). The view's starting level / layer must be 0 and its format must equal the attachment's actual format, otherwise do not merge + int tid_ = (int)(h & 0xfffff); void *tv_ = NULL; if (tid_) tex_fast_rid(tid_, &tv_); __unsafe_unretained id tv = (__bridge id)tv_; + uint32_t apf_ = tv.parentTexture ? (uint32_t)tv.parentTexture.pixelFormat : (uint32_t)tv.pixelFormat; uint8_t swz_[4]; + if (R.rt.has_cview[j] && R.rt.cview[j].set_format && !(nvn_fmt_to_mtl(R.rt.cview[j].format, &apf_, swz_) && apf_)) apf_ = 0; // The attachment is attached through a format-changing view: take that format; if unrecognized, do not merge + MTLPixelFormat apf = (MTLPixelFormat)apf_; + if (!tv || (tv.parentTexture && (tv.parentRelativeLevel || tv.parentRelativeSlice)) || tv.pixelFormat != apf) { + static int lg; if (lg++ < 8) LOG("[nvn-mtl] 片上读合并:槽 %d 采样视图(起始级 %lu 层 %lu 格式 %lu)与附件(第 0 级第 0 层 格式 %lu)不符,不合并(帧 %llu 通道 %llu)", b->reg, + tv && tv.parentTexture ? (unsigned long)tv.parentRelativeLevel : 0ul, tv && tv.parentTexture ? (unsigned long)tv.parentRelativeSlice : 0ul, (unsigned long)tv.pixelFormat, (unsigned long)apf, + (unsigned long long)atomic_load(&g_cur_frame), g_stat_pass); + PD("F %llu 证不出:槽 %d 采样视图不是附件那一张(级 / 层 / 格式不符)", g_stat_pass, b->reg); return 0; } } + int any = 0; + for (int c = 0; c < fs->rf.nfc; c++) if (fs->rf.fc_reg[c] == b->reg) { + if (!uv_proof(vs, fs->rf.fc_locn[c], dw, di)) { PD("F %llu 证不出:槽 %d 的采样坐标不是本像素(插值输出 %d)", g_stat_pass, b->reg, fs->rf.fc_locn[c]); return 0; } + fx[c] = j; fbmask |= (uint8_t)(1u << j); any = 1; } + if (!any) { PD("F %llu 证不出:槽 %d 的采样不在可改表里", g_stat_pass, b->reg); return 0; } } } + memset(lmap, 0xff, 8); + for (int c = 0; c < 8; c++) if (fs->rf.color_mask >> c & 1) { if (g_fz.p2map[c] < 0) { PD("F %llu 证不出:输出颜色 %d 无对应附件", g_stat_pass, c); return 0; } lmap[g_fz.p2map[c]] = (uint8_t)c; } + int ident = !fbmask; for (int c = 0; c < 8; c++) if ((fs->rf.color_mask >> c & 1) && g_fz.p2map[c] != c) ident = 0; + if (ident) { for (int q = 0; q < 8; q++) if (lmap[q] != 0xff) g_fz.wrote[q] = 1; if (fz_check_on()) fz_snap_take(dw, di); return 1; } // Does not read the tile and the output is not shifted: use the original version (the write mask still follows the mapping) + if (!fs->rf.nfo) { bf_want_note(fs->key); PD("F %llu 证不出:没有可读 tile 版本(输出须移位)", g_stat_pass); return 0; } + *fn = bf_fn(fs, fx, fbmask, lmap); + if (!*fn) return 0; + memset(&g_fz_desc, 0, sizeof g_fz_desc); g_fz_desc.kind = 1; g_fz_desc.fbmask = fbmask; for (int i = 0; i < 32; i++) g_fz_desc.fx[i] = (int8_t)fx[i]; + for (int q = 0; q < 8; q++) if (lmap[q] != 0xff) g_fz.wrote[q] = 1; + if (fz_check_on()) fz_snap_take(dw, di); + g_fz_draws++; return 1; +} +static void bloomq_reset(void) { memset(&g_bloomq, 0, sizeof g_bloomq); } +static int bloomq_enabled(void) { + if (g_bloom_q < 0) { const char *e = getenv("DM_NVN_BLOOM_Q"); g_bloom_q = !e || strcmp(e, "0"); } + return g_bloom_q; +} +static int bloomq_target(uint64_t *ser, int *lv, int *sl) { + if (!R.has_rt || R.rt.n != 1 || !R.rt.color[0]) return 0; + rt_sub(&R.rt, 0, ser, lv, sl); return *ser != 0; +} +static int bloomq_make_pipeline(MTLPixelFormat fmt) { + if (g_bloomq_pso && g_bloomq_fmt == fmt) return 1; + static const char *src = + "#include \nusing namespace metal;\n" + "struct V { float4 p [[position]]; float2 uv; };\n" + "vertex V vs(uint id [[vertex_id]]) { float2 uv=float2((id<<1)&2,id&2); V o; o.p=float4(uv*float2(2,-2)+float2(-1,1),0,1); o.uv=uv; return o; }\n" + "struct P { float threshold; float coeff_g; float coeff_b; float dm; float sh; };\n" + "fragment float4 fs(V in [[stage_in]], texture2d t [[texture(0)]], sampler s [[sampler(0)]], constant P& p [[buffer(0)]]) { float2 d=float2(1.0/p.dm,1.0/p.sh); float3 c=0; for (int y=-1;y<=1;y+=2) for (int x=-1;x<=1;x+=2) { float3 v=t.sample(s,in.uv+float2(x,y)*d).rgb; float l=v.r*0.2126+v.g*p.coeff_g+v.b*p.coeff_b; if(l>=p.threshold) c+=v/(1.0+l); } return float4(c*0.25,1); }\n"; + NSError *e = nil; id lib = [g_dev newLibraryWithSource:@(src) options:nil error:&e]; + if (!lib) { MLOG("【红】泛光 1/4 着色器编译失败:%s", e.localizedDescription.UTF8String); return 0; } + MTLRenderPipelineDescriptor *pd = [MTLRenderPipelineDescriptor new]; pd.vertexFunction = [lib newFunctionWithName:@"vs"]; pd.fragmentFunction = [lib newFunctionWithName:@"fs"]; pd.colorAttachments[0].pixelFormat = fmt; + g_bloomq_pso = nvn_cached_render(g_dev, pd, &e); + if (!g_bloomq_pso) { MLOG("【红】泛光 1/4 管线失败:%s", e.localizedDescription.UTF8String); return 0; } + g_bloomq_fmt = fmt; + MTLSamplerDescriptor *sd = [MTLSamplerDescriptor new]; sd.minFilter = sd.magFilter = MTLSamplerMinMagFilterLinear; sd.sAddressMode = sd.tAddressMode = MTLSamplerAddressModeClampToEdge; sd.supportArgumentBuffers = YES; + g_bloomq_samp = [g_dev newSamplerStateWithDescriptor:sd]; return g_bloomq_samp != nil; +} +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; } + if (!dst || !bloomq_make_pipeline(dst.pixelFormat)) return 0; + rp_end(); + MTLRenderPassDescriptor *rp = [MTLRenderPassDescriptor new]; rp.colorAttachments[0].texture = dst; rp.colorAttachments[0].loadAction = MTLLoadActionDontCare; rp.colorAttachments[0].storeAction = MTLStoreActionStore; + id en = enc_r_begin(rp); if (!en) return 0; + + [en setFragmentTexture:src atIndex:0]; [en setFragmentSamplerState:g_bloomq_samp atIndex:0]; + float *p = g_bloomq_const[g_bloomq_i].contents; p[0] = g_bloomq.threshold; p[1] = g_bloomq.coeff_g; p[2] = g_bloomq.coeff_b; p[3] = src.width; p[4] = src.height; [en setFragmentBuffer:g_bloomq_const[g_bloomq_i] offset:0 atIndex:0]; + [en setViewport:(MTLViewport){0,0,dst.width,dst.height,0,1}]; [en setRenderPipelineState:g_bloomq_pso]; [en drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3]; m4_end(en); + g_bloomq_i = (g_bloomq_i + 1) % 3; args_dirty_all(); g_pso_dirty = g_dss_dirty = 1; return 1; +} +// ---------- Drag-follow image for shop items: items on a vendor's shelf have no "picked up" state, so during a drag the replay layer takes "the draw that draws this item's icon", translates it to the finger at the end of the same pass and draws it once more ---------- +// In the UI pass each rectangle is one 4-vertex draw (20-byte vertices: position 3 floats + texture coordinates 2 floats; the position goes to clip coordinates through the matrix in vertex constant 0). +// The front end (front_ios.m) gives the point where the finger pressed, the shelf grid and the center of the drag-follow image (normalized, top-left origin); here, in the UI pass, find the rectangle that "covers this point, has all four edges on grid lines, and has the smallest area" = that item +// (identifying by the point alone gets it wrong: about 0.1 seconds after the press the game draws a one-cell-sized rectangle centered on the press point, not on grid lines -- on a real device it was twice taken for the item) +// (does not depend on the game's selected state: selection has to wait about 0.1 seconds for the controller cursor to land on the item, and a quick drag that starts on press cannot wait for that). The same rectangle has two draws, cell background and icon: +// the cell background uses a common texture (many rectangles in this pass use it), while each item has its own icon texture -- take the draw with the texture "used the fewest times in this pass". The recognized rectangle is reported back to the front end (used to cover the original shelf position with a dark color). +// Vertical position and height of the UI rectangle in the real picture (both as a fraction of the picture height: y = 0 touches the top, h = 1 is the whole picture); registered by the front end and read fresh on every draw (changes when switching between docked / handheld mode). +// The game draws the UI into the upper h portion of the UI texture (the viewport is the whole texture, and the projection matrix carries the scaling). +static void (*g_ui_rect_fn)(double *y, double *h); +void mtl_set_ui_rect_fn(void (*fn)(double *y, double *h)) { g_ui_rect_fn = fn; } +static double ui_hfrac(void) { double y = 0, h = 1; if (g_ui_rect_fn) g_ui_rect_fn(&y, &h); return h > 0 && h < 1 ? h : 1; } +static _Atomic int g_ghost_on; static _Atomic uint64_t g_ghost_pt, g_ghost_at, g_ghost_found, g_ghost_grid; +void mtl_ui_ghost(int on, float px, float py, const float grid[4], float cx, float cy) { + if (on) { uint64_t gv = 0; for (int i = 0; i < 4; i++) gv |= (uint64_t)(uint16_t)(fminf(fmaxf(grid[i], 0), 1) * 65535 + .5f) << (16 * i); atomic_store(&g_ghost_grid, gv); + atomic_store(&g_ghost_pt, (uint64_t)(uint32_t)lroundf(fminf(fmaxf(px, 0), 1) * 65535) | (uint64_t)(uint32_t)lroundf(fminf(fmaxf(py, 0), 1) * 65535) << 32); + atomic_store(&g_ghost_at, (uint64_t)(uint32_t)(int32_t)lroundf(cx * 65535) | (uint64_t)(uint32_t)(int32_t)lroundf(cy * 65535) << 32); } + else atomic_store(&g_ghost_found, 0); + atomic_store(&g_ghost_on, on); +} +int mtl_ui_ghost_rect(float r[4]) { uint64_t v = atomic_load(&g_ghost_found); if (!v) return 0; for (int i = 0; i < 4; i++) r[i] = (float)((v >> (16 * i)) & 0xffff) / 65535.f; return 1; } +static struct { uint64_t pass; int n, nt, busy; float area, r[4]; struct { void *S_; p_draw dw; uint64_t tex; } c[4]; uint64_t tid[128]; uint16_t tn[128]; } g_gh; +static uint64_t ghost_tex(void) { // The texture actually sampled by this draw's fragment shader (slot taken from shader reflection; the other slots in S.tex are left over from earlier draws) + __unsafe_unretained NvProg *pr = S.prog ? prog_get(S.prog) : nil; NvShader *f = pr ? pr->sh[1] : nil; if (!f) return 0; + for (int i = 0; i < f->rf.nb; i++) if ((f->rf.b[i].kind == NVN_RB_TEX || f->rf.b[i].kind == NVN_RB_SAMP) && f->rf.b[i].reg < 128 && S.tex[1][f->rf.b[i].reg]) return S.tex[1][f->rf.b[i].reg]; + return 0; } +// Whether the current UI draw is "untextured and exactly covering the whole UI rectangle" (the UI rectangle occupies the upper hf of the texture) +static int ui_cover_all(double hf) { + if (S.vbuf[0].size != 80 || ghost_tex()) return 0; + const p_bind_buf *u = &S.ubo[0][0]; void *vb, *mb; uint64_t vo, mo; const uint8_t *vp_, *mp_; + if (!u->addr || u->size < 64 || pend_hits(u->addr, 64) || pend_hits(S.vbuf[0].address, 80)) return 0; + if (!mtl_addr(S.vbuf[0].address, 80, &vb, &vo) || !(vp_ = [(__bridge id)vb contents]) || !mtl_addr(u->addr, 64, &mb, &mo) || !(mp_ = [(__bridge id)mb contents])) return 0; + float m[16], x0 = 9, y0 = 9, x1 = -9, y1 = -9; memcpy(m, mp_ + mo, 64); + for (int i = 0; i < 4; i++) { float f[2]; memcpy(f, vp_ + vo + 20 * i, 8); float nx = (m[0] * f[0] + m[4] * f[1] + m[12] + 1) / 2, ny = (m[1] * f[0] + m[5] * f[1] + m[13] + 1) / 2; + x0 = fminf(x0, nx); x1 = fmaxf(x1, nx); y0 = fminf(y0, ny); y1 = fmaxf(y1, ny); } + return x0 < 0.002f && x1 > 0.998f && y0 < 0.002f && fabsf(y1 - (float)hf) < 0.002f; +} +static void ghost_note(const p_draw *dw) { + if (g_gh.busy || dw->count != 4 || S.nattr != 2 || S.stream[0].stride != 20 || S.vbuf[0].size != 80) return; + if (g_gh.pass != g_stat_pass) { for (int i = 0; i < g_gh.n; i++) free(g_gh.c[i].S_); g_gh.n = g_gh.nt = 0; g_gh.pass = g_stat_pass; } + uint64_t t = ghost_tex(); int k = 0; for (; k < g_gh.nt; k++) if (g_gh.tid[k] == t) break; + if (k == g_gh.nt && g_gh.nt < 128) { g_gh.tid[k] = t; g_gh.tn[k] = 0; g_gh.nt++; } if (k < g_gh.nt) g_gh.tn[k]++; + const p_bind_buf *u = &S.ubo[0][0]; void *vb, *mb; uint64_t vo, mo; const uint8_t *vp_, *mp_; + if (!u->addr || u->size < 64 || pend_hits(u->addr, 64) || pend_hits(S.vbuf[0].address, 80)) return; + if (!mtl_addr(S.vbuf[0].address, 80, &vb, &vo) || !(vp_ = [(__bridge id)vb contents]) || !mtl_addr(u->addr, 64, &mb, &mo) || !(mp_ = [(__bridge id)mb contents])) return; + float m[16], x0 = 9, y0 = 9, x1 = -9, y1 = -9; memcpy(m, mp_ + mo, 64); + for (int i = 0; i < 4; i++) { float f[2]; memcpy(f, vp_ + vo + 20 * i, 8); float nx = (m[0] * f[0] + m[4] * f[1] + m[12] + 1) / 2, ny = (m[1] * f[0] + m[5] * f[1] + m[13] + 1) / 2; + x0 = fminf(x0, nx); x1 = fmaxf(x1, nx); y0 = fminf(y0, ny); y1 = fmaxf(y1, ny); } + { float hf = (float)ui_hfrac(); y0 /= hf; y1 /= hf; } // The points given by the front end are normalized coordinates within the UI rectangle; when the UI occupies only the upper hf portion of the texture, convert to the same convention (unchanged when hf = 1) + uint64_t pv = atomic_load(&g_ghost_pt); float qx = (float)(uint32_t)pv / 65535.f, qy = (float)(pv >> 32) / 65535.f; + if (qx < x0 || qx > x1 || qy < y0 || qy > y1) return; + uint64_t gv = atomic_load(&g_ghost_grid); float gx = (float)(gv & 0xffff) / 65535.f, gy = (float)((gv >> 16) & 0xffff) / 65535.f, gw = (float)((gv >> 32) & 0xffff) / 65535.f, gh = (float)(gv >> 48) / 65535.f; + if (gw <= 0 || gh <= 0) return; + float w = x1 - x0, h = y1 - y0, area = w * h, c0 = (x0 - gx) / gw, r0 = (y0 - gy) / gh, cn = w / gw, rn = h / gh; // Which cell the top-left corner is in, and how many cells it occupies + if (fabsf(c0 - roundf(c0)) > 0.15f || fabsf(r0 - roundf(r0)) > 0.15f || fabsf(cn - roundf(cn)) > 0.15f || fabsf(rn - roundf(rn)) > 0.15f || cn < 0.85f || rn < 0.85f || cn > 2.15f || rn > 4.15f) return; // One item is at most 2x4 cells + if (g_gh.n && area > g_gh.area * 1.03f) return; // A smaller one already exists + if (g_gh.n && area < g_gh.area * 0.97f) { for (int i = 0; i < g_gh.n; i++) free(g_gh.c[i].S_); g_gh.n = 0; } // Found a smaller one: the earlier ones are invalidated + if (g_gh.n == 4) { free(g_gh.c[0].S_); memmove(&g_gh.c[0], &g_gh.c[1], 3 * sizeof g_gh.c[0]); g_gh.n = 3; } + g_gh.area = area; g_gh.r[0] = x0; g_gh.r[1] = y0; g_gh.r[2] = x1; g_gh.r[3] = y1; + typeof(g_gh.c[0]) *c = &g_gh.c[g_gh.n++]; c->S_ = malloc(sizeof S); memcpy(c->S_, &S, sizeof S); c->dw = *dw; c->tex = t; +} +static void ghost_flush(void) { // Before the render pass ends: translate the recognized icon draw to the finger and draw it once more (moves the viewport, does not change vertices; the scissor is opened up) + if (g_gh.busy || !g_gh.n) return; + if (g_gh.pass != g_stat_pass || !atomic_load(&g_ghost_on)) { for (int i = 0; i < g_gh.n; i++) free(g_gh.c[i].S_); g_gh.n = 0; return; } + int best = -1, bn = 1 << 30; for (int i = 0; i < g_gh.n; i++) { int cnt = 0; for (int k = 0; k < g_gh.nt; k++) if (g_gh.tid[k] == g_gh.c[i].tex) cnt = g_gh.tn[k]; if (cnt < bn) { bn = cnt; best = i; } } + typeof(g_gh.c[0]) *c = &g_gh.c[best]; uint64_t at = atomic_load(&g_ghost_at); float ax = (float)(int32_t)(uint32_t)at / 65535.f, ay = (float)(int32_t)(uint32_t)(at >> 32) / 65535.f; + float W = (float)g_pass.tw, H = (float)g_pass.th, ccx = (g_gh.r[0] + g_gh.r[2]) / 2, ccy = (g_gh.r[1] + g_gh.r[3]) / 2; + { uint64_t v = 0; for (int i = 0; i < 4; i++) v |= (uint64_t)(uint16_t)(fminf(fmaxf(g_gh.r[i], 0), 1) * 65535 + .5f) << (16 * i); atomic_store(&g_ghost_found, v ? v : 1); } + g_gh.busy = 1; typeof(S) save; memcpy(&save, &S, sizeof S); p_scissor sc = R.sc; int has_sc = R.has_sc; + memcpy(&S, c->S_, sizeof S); if (!S.has_vp) { S.vp[0][0] = S.vp[0][1] = 0; S.vp[0][2] = W; S.vp[0][3] = H; S.has_vp = 1; } + S.vp[0][0] += (ax - ccx) * W; S.vp[0][1] += (ay - ccy) * H * (float)ui_hfrac(); R.has_sc = 0; + args_dirty_all(); g_pso_dirty = g_dss_dirty = 1; draw(&c->dw, NULL); + { static int once; if (!once++) MLOG("拖货跟手图:首例 通道 %llu,候选 %d 个,取贴图本趟用到 %d 次的那次", g_stat_pass, g_gh.n, bn); } + memcpy(&S, &save, sizeof S); args_dirty_all(); R.sc = sc; R.has_sc = has_sc; g_pso_dirty = g_dss_dirty = 1; g_gh.busy = 0; + for (int i = 0; i < g_gh.n; i++) free(g_gh.c[i].S_); g_gh.n = 0; +} +// Diagnostic DM_NVN_KEYDUMP=[:[:]]: watch only this class of draws (for real devices; no frame number to guess). The first N matching draws force PASSDUMP's per-binding lines on (K / B / d: +// textures with format / view / channel swizzle, samplers with LOD clamps), plus [kd] lines: vertex attribute formats, each vertex stream (first 4 vertices of per-vertex streams, all instances of per-instance streams) and every value of each uniform buffer of both stages. +// Run the same switch once on the Mac and once on the device and compare item by item to see which input differs on the device (Super Mario 3D World + Bowser's Fury's magenta squares in 1-1: particle 5e4de7561c670fee, iPhone only, not reproducible on the Mac) +static void kd_floats(const char *what, int reg, uint64_t addr, uint64_t size, uint64_t cap) { + void *mb; uint64_t mo; const uint8_t *base; + if (!addr || !size) { fprintf(stderr, "[kd] %s r%d 未绑\n", what, reg); return; } + if (!mtl_addr(addr, 1, &mb, &mo) || !(base = [(__bridge id)mb contents])) { fprintf(stderr, "[kd] %s r%d addr=%#llx 不在内存池\n", what, reg, (unsigned long long)addr); return; } + uint64_t n = MIN(size, cap) / 4; const float *v = (const float *)(base + mo); + fprintf(stderr, "[kd] %s r%d addr=%#llx size=%llu%s\n", what, reg, (unsigned long long)addr, (unsigned long long)size, pend_hits(addr, size) ? "(本提交里还有待执行的写)" : ""); + for (uint64_t i = 0; i < n; i += 16) { + char line[1024]; size_t o = 0; + for (uint64_t k = i; k < n && k < i + 16 && o < sizeof line - 24; k++) o += snprintf(line + o, sizeof line - o, " %.6g", v[k]); + fprintf(stderr, "[kd] +%#06llx%s\n", (unsigned long long)(i * 4), line); + } +} +static int kd_draw(NvProg *pr, const p_draw *dw, const p_draw_idx *di) { + static char key[24]; static int left = -1, every; static uint64_t last = ~0ull; + if (left < 0) { left = 0; const char *e = getenv("DM_NVN_KEYDUMP"); + if (e && *e) { size_t n = strcspn(e, ":"); if (n > 23) n = 23; memcpy(key, e, n); key[n] = 0; const char *c = strchr(e, ':'); left = c ? atoi(c + 1) : 6; every = c && strchr(c + 1, ':') ? atoi(strchr(c + 1, ':') + 1) : 0; + MLOG("DM_NVN_KEYDUMP:片元着色器 %s 的 %d 次绘制逐项记录%s([kd] / [pass] 行)", key, left, every ? ",每隔这么多帧最多记一次" : ""); } } + if (left <= 0 || !pr->sh[1] || strncmp(pr->sh[1]->key, key, strlen(key))) return 0; + uint64_t fr = atomic_load(&g_cur_frame); + if (every > 0 && last != ~0ull && fr < last + (uint64_t)every) return 0; // at most once every `every` frames: samples spread over the session, no need to guess the frame of the scene + last = fr; left--; + int count = dw ? dw->count : di->count, inst = dw ? dw->instances : di->instances, first = dw ? dw->first : 0, binst = dw ? dw->base_instance : di->base_instance; + fprintf(stderr, "[kd] F%llu 通道 %llu vs=%s fs=%s 图元 %d 顶点 %d 实例 %d 起点 %d 基顶点 %d 基实例 %d 目标 %s\n", (unsigned long long)atomic_load(&g_cur_frame), g_stat_pass, + pr->sh[0] ? pr->sh[0]->key : "-", pr->sh[1]->key, dw ? dw->prim : di->prim, count, inst, first, di ? di->base_vertex : 0, binst, R.rt.n && R.rt.color[0] ? "有" : "无"); + for (int i = 0; i < S.nattr && i < 16; i++) fprintf(stderr, "[kd] 属性 %d 格式 %#x 偏移 %u 流 %u\n", i, (unsigned)S.attr[i].fmt, (unsigned)S.attr[i].offset, (unsigned)S.attr[i].stream); + for (int s_ = 0; s_ < S.nstream && s_ < 16; s_++) { + uint32_t st = S.stream[s_].stride, dv = S.stream[s_].divisor; + fprintf(stderr, "[kd] 流 %d 步长 %u 除数 %u 缓冲 %#llx 大小 %llu\n", s_, st, dv, (unsigned long long)S.vbuf[s_].address, (unsigned long long)S.vbuf[s_].size); + if (!S.vbuf[s_].address || !st) continue; + uint64_t from = (uint64_t)(dv ? binst : (di ? di->base_vertex : first)) * st, n = (uint64_t)st * (dv ? (uint64_t)MIN(inst, 8) : 4); + if (from < S.vbuf[s_].size) kd_floats("流", s_, S.vbuf[s_].address + from, MIN(n, S.vbuf[s_].size - from), 512); + } + for (int stg = 0; stg < 2; stg++) { NvShader *sh = pr->sh[stg]; if (!sh) continue; + for (int i = 0; i < sh->rf.nb; i++) if (sh->rf.b[i].kind == NVN_RB_CBUF && sh->rf.b[i].reg < 16) { + const p_bind_buf *u = &S.ubo[stg][sh->rf.b[i].reg]; kd_floats(stg ? "片元 uniform" : "顶点 uniform", sh->rf.b[i].reg, u->addr, u->size, 4096); } } + return 1; +} +// Diagnostic DM_NVN_MAGSCAN=::: on-device self-check for the magenta squares (Super Mario 3D World + Bowser's Fury 1-1, iPhone only, not reproducible on the Mac). +// In a scan frame, as soon as each render pass ends, a compute kernel reads its color attachments (read goes through the same format decode as the game's shader samples) and gathers per 16x16 block: the magenta pixel count (R and B both >= 0.8 of the largest channel, G <= 0.1 of it), +// the non-finite pixel count and each channel's min / max. An attachment with a block holding one single magenta value throughout (exactly what the squares in the iPhone screenshot are, aligned to the 16-pixel grid) or with non-finite values gets a [mag] line (index of the pass within the frame, distance from the pass holding this particle, attachment serial / format / size, block position and value). +// Four phases rotate: phase 0 draws as usual, phase 1 drops every draw of this fragment shader, phases 2 / 3 keep the geometry and replace the fragment output with a constant (grey / bright magenta). Comparing the phases tells whether this draw causes it +// and whether its fragment output (texture reads, value range) matters; the first pass where the magenta blocks show tells which step goes wrong +static char g_ms_key[24]; static int g_ms_every, g_ms_plen, g_ms_init; +static int ms_on(void); +static const char *const k_ms_phase[4] = {"照常画", "丢掉该着色器", "该着色器改出常量灰", "该着色器改出常量高亮品红"}; +static int ms_phase_now(void) { return (int)(atomic_load(&g_cur_frame) / (uint64_t)g_ms_plen % 4); } +// Phases 2 / 3: this shader's draws keep their geometry and the fragment stage is replaced by a constant output (grey 0.5 / bright magenta 1500,2,1500). The pipeline cache key carries the replacement function, kept apart from the original pipeline; these pipelines are not recorded in the prewarm list +static id ms_fs_override(NvShader *fs) { + if (!fs || !ms_on() || strncmp(fs->key, g_ms_key, strlen(g_ms_key))) return nil; + int ph = ms_phase_now(); if (ph < 2) return nil; + static id fn[2]; static int failed; + if (!fn[0] && !failed) { NSError *e = nil; id lib = [g_dev newLibraryWithSource:@"#include \nusing namespace metal;\n" + "fragment float4 msg() { return float4(0.5, 0.5, 0.5, 1.0); }\nfragment float4 msm() { return float4(1500.0, 2.0, 1500.0, 0.6); }\n" options:nil error:&e]; + fn[0] = [lib newFunctionWithName:@"msg"]; fn[1] = [lib newFunctionWithName:@"msm"]; + if (!fn[0] || !fn[1]) { failed = 1; MLOG("【红】DM_NVN_MAGSCAN:常量片元编不出 %s", e.localizedDescription.UTF8String); } } + return failed ? nil : fn[ph - 2]; +} +static int ms_on(void) { + if (!g_ms_init) { g_ms_init = 1; const char *e = getenv("DM_NVN_MAGSCAN"); + if (e && *e) { size_t n = strcspn(e, ":"); if (n > 23) n = 23; memcpy(g_ms_key, e, n); g_ms_key[n] = 0; + const char *c = strchr(e, ':'); g_ms_every = c ? atoi(c + 1) : 30; c = c ? strchr(c + 1, ':') : NULL; g_ms_plen = c ? atoi(c + 1) : 600; + if (g_ms_every < 1) g_ms_every = 30; if (g_ms_plen < g_ms_every) g_ms_plen = 600; + MLOG("DM_NVN_MAGSCAN:片元着色器 %s,每 %d 帧扫一次各通道颜色附件里的品红块;每 %d 帧换一相(相 0 照常画 / 1 丢掉这个着色器的绘制 / 2 它改出常量灰 / 3 它改出常量高亮品红)([mag] 行)", g_ms_key, g_ms_every, g_ms_plen); } } + return g_ms_key[0] != 0; +} +static uint64_t g_ms_ppass = ~0ull, g_ms_pframe = ~0ull, g_ms_drawn, g_ms_dropped; +// returns 1 = this phase drops the draw +static int ms_draw(NvProg *pr) { + if (!ms_on() || !pr->sh[1] || strncmp(pr->sh[1]->key, g_ms_key, strlen(g_ms_key))) return 0; // a deferred draw returns before this when recorded and only arrives here when replayed: every draw passes exactly once + uint64_t f = atomic_load(&g_cur_frame); + if (f / (uint64_t)g_ms_plen % 4 == 1) { g_ms_dropped++; return 1; } + g_ms_drawn++; if (g_ms_pframe != f) { g_ms_pframe = f; g_ms_ppass = g_stat_pass; } // the pass where this particle first draws this frame + return 0; +} +static const char k_ms_msl[] = +"#include \nusing namespace metal;\n" +"kernel void ms(texture2d t [[texture(0)]], device uint *o [[buffer(0)]], uint2 g [[thread_position_in_grid]], uint2 tg [[threadgroup_position_in_grid]],\n" +" uint li [[thread_index_in_threadgroup]], uint si [[simdgroup_index_in_threadgroup]], uint sl [[thread_index_in_simdgroup]]) {\n" +" threadgroup uint sh[8][8]; bool in = g.x < o[0] && g.y < o[1]; float4 c = in ? t.read(g) : float4(0); float m = max(c.r, max(c.g, c.b));\n" +" bool bad = in && (isnan(c.r) || isnan(c.g) || isnan(c.b) || isinf(c.r) || isinf(c.g) || isinf(c.b));\n" +" bool mag = in && !bad && m > 0.0 && c.r >= 0.8 * m && c.b >= 0.8 * m && c.g <= 0.1 * m;\n" +" uint r = as_type(max(c.r, 0.0)), gg = as_type(max(c.g, 0.0)), b = as_type(max(c.b, 0.0));\n" +" uint v[8] = {simd_sum(mag ? 1u : 0u), simd_sum(bad ? 1u : 0u), simd_min(in ? r : ~0u), simd_max(in ? r : 0u), simd_min(in ? gg : ~0u), simd_max(in ? gg : 0u), simd_min(in ? b : ~0u), simd_max(in ? b : 0u)};\n" +" if (sl == 0) for (int k = 0; k < 8; k++) sh[si][k] = v[k];\n" +" threadgroup_barrier(mem_flags::mem_threadgroup); if (li) return;\n" +" for (int s = 1; s < 8; s++) { v[0] += sh[s][0]; v[1] += sh[s][1]; for (int k = 2; k < 8; k += 2) { v[k] = min(v[k], sh[s][k]); v[k + 1] = max(v[k + 1], sh[s][k + 1]); } }\n" +" device uint *q = o + 4 + 4 * (tg.y * o[2] + tg.x); q[0] = v[0] | v[1] << 16 | (v[2] == v[3] && v[4] == v[5] && v[6] == v[7] ? 1u << 31 : 0u); q[1] = v[3]; q[2] = v[5]; q[3] = v[7]; }\n"; +#define MS_BUF (16u << 20) +#define MS_MAXE 256 +typedef struct { uint64_t pass_idx, ser; int att, pdist, fmt; uint32_t w, h, off; } ms_ent; +typedef struct { __strong id b; uint64_t frame, fence; uint32_t used; int n, busy, full; ms_ent e[MS_MAXE]; } ms_scan; +static ms_scan g_ms_s[2]; static int g_ms_cur = -1; // which buffer this scan frame writes (-1 = not scanning this frame) +static uint64_t g_ms_curf = ~0ull, g_ms_first; +static __strong id g_ms_att[8]; static NSUInteger g_ms_lv[8], g_ms_sl[8]; static uint64_t g_ms_aser[8]; static int g_ms_na; static uint64_t g_ms_apass; +static int g_ms_seq = -1, g_ms_sc, g_ms_hitf, g_ms_maxblk, g_ms_pscan; static uint64_t g_ms_f0, g_ms_f1, g_ms_pmag; // the phase being tallied (frame / frames per phase) and its totals +static void ms_poll(void) { + for (int k = 0; k < 2; k++) { ms_scan *s = &g_ms_s[k]; if (!s->busy || g_ms_cur == k || s->fence > g_done) continue; + int seq = (int)(s->frame / (uint64_t)g_ms_plen); + if (seq != g_ms_seq) { + if (g_ms_seq >= 0) MLOG("[mag] 相 %d(%s)帧 %llu–%llu:扫 %d 帧,有均一品红块(16×16 整块同一个品红值)的 %d 帧,单帧最多 %d 块;粒子所在通道的附件里品红像素 每帧均 %.1f(%d 帧);该着色器至今画 %llu 次、丢 %llu 次", + g_ms_seq % 4, k_ms_phase[g_ms_seq % 4], (unsigned long long)g_ms_f0, (unsigned long long)g_ms_f1, g_ms_sc, g_ms_hitf, g_ms_maxblk, g_ms_pscan ? (double)g_ms_pmag / g_ms_pscan : 0.0, g_ms_pscan, + (unsigned long long)g_ms_drawn, (unsigned long long)g_ms_dropped); + g_ms_seq = seq; g_ms_sc = g_ms_hitf = g_ms_maxblk = g_ms_pscan = 0; g_ms_pmag = 0; g_ms_f0 = s->frame; } + g_ms_sc++; g_ms_f1 = s->frame; int fblk = 0, pseen = 0; + for (int i = 0; i < s->n; i++) { const ms_ent *e = &s->e[i]; const uint32_t *o = (const uint32_t *)((const uint8_t *)s->b.contents + e->off); + uint32_t tw = (e->w + 15) / 16, th = (e->h + 15) / 16; uint64_t bad = 0, mag = 0; int ublk = 0, mblk = 0, nl = 0; char line[1400]; size_t lo = 0; + for (uint32_t t = 0; t < tw * th; t++) { const uint32_t *q = o + 4 + 4 * t; uint32_t nmag = q[0] & 0xffff, nbad = q[0] >> 16 & 0x7fff; bad += nbad; mag += nmag; + uint32_t npx = MIN(16u, e->w - t % tw * 16) * MIN(16u, e->h - t / tw * 16); + int uni = q[0] >> 31; // the whole block holds one value + int allm = nmag == npx; + if (allm) mblk++; if (allm && uni && npx >= 64) ublk++; + if (((allm && uni && npx >= 64) || nbad) && nl < 8 && lo < sizeof line - 120) { float r, g, b; memcpy(&r, &q[1], 4); memcpy(&g, &q[2], 4); memcpy(&b, &q[3], 4); nl++; + lo += snprintf(line + lo, sizeof line - lo, " [%u,%u%s 最大值 %g %g %g 非有限 %u]", t % tw * 16, t / tw * 16, uni ? " 均一" : "", r, g, b, nbad); } } + fblk += ublk; if (e->pdist == 0 && e->att == 0) { g_ms_pmag += mag; pseen = 1; } + if (ublk || bad) MLOG("[mag] F%llu 相 %llu 通道 %llu(粒子通道%+d)附件 %d ser=%llu 格式 %d %ux%u:均一品红块 %d(全品红块 %d),非有限 %llu 像素%s", (unsigned long long)s->frame, (unsigned long long)(s->frame / (uint64_t)g_ms_plen % 4), + (unsigned long long)e->pass_idx, e->pdist, e->att, (unsigned long long)e->ser, e->fmt, e->w, e->h, ublk, mblk, (unsigned long long)bad, line); + } + if (fblk) g_ms_hitf++; if (fblk > g_ms_maxblk) g_ms_maxblk = fblk; g_ms_pscan += pseen; + if (s->full) MLOG("[mag] F%llu 扫描缓冲满,本帧后面的通道没扫", (unsigned long long)s->frame); + s->busy = 0; s->n = 0; s->used = 0; s->full = 0; } +} +void ms_note(MTLRenderPassDescriptor *pd, NSUInteger nc) { + g_ms_na = 0; if (!ms_on()) return; + uint64_t f = atomic_load(&g_cur_frame); + if (f != g_ms_curf) { // a new frame: the previous scan frame's counts are processed once its submission completes + g_ms_curf = f; g_ms_first = g_stat_pass + 1; if (g_ms_cur >= 0) g_ms_cur = -1; + if (f % (uint64_t)g_ms_every == 0) { for (int k = 0; k < 2; k++) if (!g_ms_s[k].busy) { g_ms_cur = k; break; } + if (g_ms_cur >= 0) { ms_scan *s = &g_ms_s[g_ms_cur]; + if (!s->b) { s->b = [g_dev newBufferWithLength:MS_BUF options:MTLResourceStorageModeShared]; if (!s->b) { g_ms_cur = -1; return; } res_add(s->b); } + s->busy = 1; s->frame = f; s->n = 0; s->used = 0; s->full = 0; s->fence = 0; } } } + ms_poll(); + if (g_ms_cur < 0) return; + g_ms_apass = g_stat_pass + 1; + for (NSUInteger i = 0; i < nc && i < 8; i++) { id t = pd.colorAttachments[i].texture; if (!t) continue; + g_ms_att[g_ms_na] = t; g_ms_lv[g_ms_na] = pd.colorAttachments[i].level; g_ms_sl[g_ms_na] = pd.colorAttachments[i].slice; g_ms_aser[g_ms_na] = R.rt.color[i] ? R.rt.color[i]->serial : 0; g_ms_na++; } +} +void ms_after(void) { + if (!g_ms_na) return; + int na = g_ms_na; g_ms_na = 0; + if (g_ms_cur < 0 || g_stat_pass != g_ms_apass) { for (int i = 0; i < na; i++) g_ms_att[i] = nil; return; } + static int failed; static id ps; static int logged_att; + if (failed) return; + if (!ps) { NSError *e = nil; id lib = [g_dev newLibraryWithSource:@(k_ms_msl) options:nil error:&e]; id fn = lib ? [lib newFunctionWithName:@"ms"] : nil; + ps = fn ? nvn_cached_compute(g_dev, fn, &e) : nil; + if (!ps) { failed = 1; MLOG("【红】DM_NVN_MAGSCAN:计算管线建不出 %s", e.localizedDescription.UTF8String); return; } } + ms_scan *s = &g_ms_s[g_ms_cur]; int pdist = g_ms_pframe == s->frame && g_ms_ppass != ~0ull ? (int)((int64_t)g_stat_pass - (int64_t)g_ms_ppass) : 9999; + for (int i = 0; i < na; i++) { id t = g_ms_att[i]; g_ms_att[i] = nil; + MTLPixelFormat pf = t.pixelFormat; + int ok = pf == MTLPixelFormatRGBA8Unorm || pf == MTLPixelFormatRGBA8Unorm_sRGB || pf == MTLPixelFormatBGRA8Unorm || pf == MTLPixelFormatBGRA8Unorm_sRGB || pf == MTLPixelFormatRGB10A2Unorm + || pf == MTLPixelFormatRG11B10Float || pf == MTLPixelFormatRGBA16Float || pf == MTLPixelFormatRGBA16Unorm || pf == MTLPixelFormatRGBA32Float; + if (!ok || !(t.usage & MTLTextureUsageShaderRead) || t.sampleCount > 1 || (t.width >> g_ms_lv[i]) < 64 || t.storageMode == MTLStorageModeMemoryless || (t.textureType != MTLTextureType2D && t.textureType != MTLTextureType2DArray)) continue; + if (pdist == 0 && !logged_att++) MLOG("[mag] 粒子所在通道的附件 %d:ser=%llu 格式 %lu %lux%lu 级 %lu 层 %lu 存储 %lu 用途 %#lx 压缩类型 %ld 允许 GPU 优化排布 %d", i, (unsigned long long)g_ms_aser[i], (unsigned long)pf, + (unsigned long)t.width, (unsigned long)t.height, (unsigned long)g_ms_lv[i], (unsigned long)g_ms_sl[i], (unsigned long)t.storageMode, (unsigned long)t.usage, (long)t.compressionType, (int)t.allowGPUOptimizedContents); + uint32_t w = (uint32_t)MAX(t.width >> g_ms_lv[i], 1ul), h = (uint32_t)MAX(t.height >> g_ms_lv[i], 1ul), tw = (w + 15) / 16, th = (h + 15) / 16, need = 16 + tw * th * 16; + if (s->n >= MS_MAXE || s->used + need > MS_BUF) { s->full = 1; continue; } + id v = t; + if (t.textureType != MTLTextureType2D || g_ms_lv[i] || g_ms_sl[i]) v = [t newTextureViewWithPixelFormat:pf textureType:MTLTextureType2D levels:NSMakeRange(g_ms_lv[i], 1) slices:NSMakeRange(g_ms_sl[i], 1)]; + if (!v) continue; + uint32_t *o = (uint32_t *)((uint8_t *)s->b.contents + s->used); o[0] = w; o[1] = h; o[2] = tw; o[3] = 0; + ms_ent *e = &s->e[s->n++]; e->pass_idx = g_stat_pass - g_ms_first; e->ser = g_ms_aser[i]; e->att = i; e->pdist = pdist; e->fmt = (int)pf; e->w = w; e->h = h; e->off = s->used; s->used += need; + if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + id ce = (id)enc_c_begin(NULL); + [ce setTexture:v atIndex:0]; [ce setBuffer:s->b offset:e->off atIndex:0]; + enc_c_pso(ce, ps); enc_c_dispatch(ce, MTLSizeMake(tw, th, 1), MTLSizeMake(16, 16, 1)); enc_c_end(ce); + args_dirty_all(); s->fence = g_fence + 1; + if (v != t) defer_obj_unref(v); + } +} +// Same-pass feedback sampling: a texture view this draw's fragment stage samples is exactly the level and layer of a color attachment of the current render pass (Super Mario 3D World + Bowser's Fury: +// 66a71a3081b91b9a writes the tone-mapped 1920x1080 RGB10A2 image as an attachment while sampling it as a texture). On the Switch (Maxwell, immediate-mode) the read returns the content from before this draw; +// on Metal, writing and sampling the same attachment within one render pass is undefined: the Mac happens to read correctly, while the iPhone (A20 Pro) now and then reads whole 16x16 blocks as (1,0,1) and FXAA carries them into the picture - the magenta squares in 1-1 +// (measured with DM_NVN_MAGSCAN: the squares first appear on this attachment, and still appear with the particle dropped). +// The fix: before this draw opens its render pass, end the current render pass (the attachment lands in memory), copy that level and layer into a snapshot texture in the replay stream, make this draw sample the snapshot; later draws reopen the render pass as usual (loading the attachment). +// Only single-level 2D views that are exactly the attachment's level and layer are handled; other shapes are reported once and left as they were. Done only for the fragment shaders the game profile's feedback_snap lists (see fb_want) +// Which fragment shaders get it: the game profile's feedback_snap (key prefixes, space-separated); DM_NVN_FB_SNAP=0 none, all every shader, another list. The cost of doing all: +// Breath of the Wild has over a thousand draws a frame sampling the G-buffer of their own pass (frame time 16.6 -> 36.6 ms) and Mario Wonder's submit time went 1.4 -> 4.0 ms, so only shaders seen to break are listed +static char g_fb_keys[16][20]; static int g_fb_nk = -1, g_fb_all; +static int fb_want(const char *key) { + if (g_fb_nk < 0) { g_fb_nk = 0; const char *e = getenv("DM_NVN_FB_SNAP"), *src = e && *e ? e : DM_GAME_FEEDBACK_SNAP; + if (!strcmp(src, "all")) g_fb_all = 1; + else if (strcmp(src, "0")) for (const char *q = src; *q && g_fb_nk < 16;) { size_t n = strcspn(q, ", "); if (n) { if (n > 16) n = 16; memcpy(g_fb_keys[g_fb_nk], q, n); g_fb_keys[g_fb_nk++][n] = 0; } q += n; q += strspn(q, ", "); } + if (g_fb_all || g_fb_nk) MLOG("同通道反馈采样快照:%s%s", g_fb_all ? "全部片元着色器" : src, e && *e ? "(DM_NVN_FB_SNAP)" : "(游戏档案 feedback_snap)"); } + if (g_fb_all) return 1; + for (int i = 0; i < g_fb_nk; i++) if (!strncmp(key, g_fb_keys[i], strlen(g_fb_keys[i]))) return 1; + return 0; +} +static id fb_snap_tex(MTLPixelFormat pf, NSUInteger w, NSUInteger h) { // one per (format, size); full queue barriers separate consecutive snapshots, so reuse within a frame does not conflict + static __strong id pool[4]; static int next; + for (int i = 0; i < 4; i++) if (pool[i] && pool[i].pixelFormat == pf && pool[i].width == w && pool[i].height == h) return pool[i]; + MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pf width:w height:h mipmapped:NO]; + d.usage = MTLTextureUsageShaderRead | MTLTextureUsagePixelFormatView; d.storageMode = MTLStorageModePrivate; + id t = [g_dev newTextureWithDescriptor:d]; if (!t) return nil; res_add(t); + if (pool[next]) defer_obj_release(pool[next]); pool[next] = t; next = (next + 1) & 3; + return t; +} +// Called before the draw opens its render pass: if a snapshot is needed, end the current render pass, copy the snapshot and register the substitution (used by this draw's argument encoding) +static void fb_prepare(NvProg *pr) { + g_draw_seq++; g_fb_n = 0; + if (g_fb_rebind) { args_dirty_all(); g_fb_rebind = 0; } // the previous draw had a substituted binding: re-encode with the original bindings this time + if (!pr || !pr->sh[1] || !R.rt.n || g_fz.active || g_defer_replaying || !fb_want(pr->sh[1]->key)) return; + NvShader *fs = pr->sh[1]; + for (int i = 0; i < fs->rf.nb && g_fb_n < 4; i++) { const nvn_rbind *b = &fs->rf.b[i]; if (b->kind != NVN_RB_TEX && b->kind != NVN_RB_TEXU) continue; + int tid = b->kind == NVN_RB_TEXU ? (int)(ubo_handle(1, b->reg, b->off) & 0xfffff) : b->reg < NTEX ? (int)(S.tex[1][b->reg] & 0xfffff) : 0; if (!tid) continue; + pthread_mutex_lock(&g_rmu); uint64_t ser = tid < g_ntreg && g_treg[tid].ok ? g_treg[tid].t.serial : 0; pthread_mutex_unlock(&g_rmu); + if (!ser) continue; + for (int k = 0; k < R.rt.n && k < 8 && g_fb_n < 4; k++) { if (!R.rt.color[k] || R.rt.color[k]->serial != ser) continue; + uint64_t aser; int lv, sl; rt_sub(&R.rt, k, &aser, &lv, &sl); + pthread_mutex_lock(&g_rmu); void *tv = NULL; tex_fast_rid(tid, &tv); id t = (__bridge id)tv; pthread_mutex_unlock(&g_rmu); + if (!t) continue; + int dup = 0; for (int q = 0; q < g_fb_n; q++) dup |= g_fb_src[q] == t; if (dup) continue; + id base = t.parentTexture ? t.parentTexture : t; NSUInteger tl = t.parentTexture ? t.parentRelativeLevel : 0, ts = t.parentTexture ? t.parentRelativeSlice : 0; + if (tl + t.mipmapLevelCount <= (NSUInteger)lv || tl > (NSUInteger)lv) continue; // the sampled levels do not include the attachment's level (mip generation etc.): not feedback + if (t.textureType != MTLTextureType2D || t.mipmapLevelCount != 1 || t.sampleCount != 1 || ts != (NSUInteger)sl || b->dtt != MTLTextureType2D || base.parentTexture) { + RED_ONCE(80, "同通道反馈采样:采样视图不是附件那一级那一层的单级二维视图(类型 %lu 级 %lu+%lu 层 %lu,附件 级 %d 层 %d,着色器 %s),照旧", (unsigned long)t.textureType, (unsigned long)tl, (unsigned long)t.mipmapLevelCount, (unsigned long)ts, lv, sl, fs->key); + continue; } + NSUInteger w = MAX(base.width >> lv, 1ul), h = MAX(base.height >> lv, 1ul); + id snap = fb_snap_tex(base.pixelFormat, w, h); if (!snap) continue; + if (g_enc) rp_end(); + id e = blit(); enc_b_copy_texture(e, base, (NSUInteger)sl, (NSUInteger)lv, MTLOriginMake(0, 0, 0), MTLSizeMake(w, h, 1), snap, 0, 0, MTLOriginMake(0, 0, 0)); + MTLTextureSwizzleChannels sw = t.swizzle; + int same = t.pixelFormat == snap.pixelFormat && sw.red == MTLTextureSwizzleRed && sw.green == MTLTextureSwizzleGreen && sw.blue == MTLTextureSwizzleBlue && sw.alpha == MTLTextureSwizzleAlpha; + id use = same ? snap : [snap newTextureViewWithPixelFormat:t.pixelFormat textureType:MTLTextureType2D levels:NSMakeRange(0, 1) slices:NSMakeRange(0, 1) swizzle:sw]; + if (!use) { RED_ONCE(81, "同通道反馈采样:快照视图建不出(格式 %lu),照旧", (unsigned long)t.pixelFormat); continue; } + if (g_fb_use[g_fb_n] && g_fb_use[g_fb_n] != use) defer_obj_unref(g_fb_use[g_fb_n]); + g_fb_src[g_fb_n] = t; g_fb_use[g_fb_n] = use; g_fb_rid[g_fb_n] = legacy_texture_rid(use); g_fb_n++; + g_fb_count++; + if (g_fb_count == 1 || g_fb_count == 10 || g_fb_count == 1000 || g_fb_count % 100000 == 0) + MLOG("同通道反馈采样:快照第 %llu 次(帧 %llu 通道 %llu,着色器 %s 槽 %d 采样附件 %d ser=%llu %lux%lu 格式 %lu)", (unsigned long long)g_fb_count, (unsigned long long)atomic_load(&g_cur_frame), g_stat_pass + !g_enc, + fs->key, b->reg, k, (unsigned long long)ser, (unsigned long)w, (unsigned long)h, (unsigned long)base.pixelFormat); + PD("N %llu 同通道反馈采样:附件 %d ser=%llu 级 %d 层 %d 拷进快照,本次绘制改采样快照", g_stat_pass + !g_enc, k, (unsigned long long)ser, lv, sl); + } } + if (g_fb_n) { g_fb_seq = g_draw_seq; args_dirty_all(); g_fb_rebind = 1; } +} +// Whether this draw needs more from the inline updates deferred in the open pass than the uniform shadows give (see mtl_sub_update): a uniform binding they cover only in part, a bindless +// handle in such a part, or their data read as vertices, indices, indirect arguments or a storage buffer. Then the pass ends first and the updates land before the draw, as before +static int ubd_conflict(NvProg *pr, const p_draw *dw, const p_draw_idx *di) { + for (int s_ = 0; s_ < 16; s_++) if (S.vbuf[s_].address && ubd_hits(S.vbuf[s_].address, S.vbuf[s_].address + S.vbuf[s_].size)) return 1; + if (di && di->ibuf && ubd_hits(di->ibuf, di->ibuf + (uint64_t)di->count * 4)) return 1; + uint64_t ind = dw ? dw->ind : di ? di->ind : 0; + if (ind && ubd_hits(ind, ind + (dw ? sizeof(MTLDrawPrimitivesIndirectArguments) : sizeof(MTLDrawIndexedPrimitivesIndirectArguments)))) return 1; + for (int st = 0; pr && st < 2; st++) { __unsafe_unretained NvShader *sh = pr->sh[st]; if (!sh) continue; // the stages encode_args binds (vertex, fragment) + for (int i = 0; i < sh->rf.nb; i++) { const nvn_rbind *b = &sh->rf.b[i]; if (b->reg >= 16) continue; + const p_bind_buf *u = b->kind == NVN_RB_SBUFP ? &S.ssbo[st][b->reg] : &S.ubo[st][b->reg]; + if (!u->addr) continue; + if (b->kind == NVN_RB_CBUF) { if (ubd_hits(u->addr, u->addr + u->size) && !ubd_covers(u->addr, u->addr + u->size)) return 1; } + else if (b->kind == NVN_RB_TEXU || b->kind == NVN_RB_SAMPU) { uint64_t h = u->addr + b->off; if (ubd_hits(h, h + 4) && !ubd_covers(h, h + 4)) return 1; } + else if (b->kind == NVN_RB_SBUFP || b->kind == NVN_RB_UBOP) { if (ubd_hits(u->addr, u->addr + (u->size ? u->size : 1))) return 1; } } } + return 0; +} + +// NVN transform feedback is used by Pokémon Legends: Z-A as a vertex-only +// skinning pass. Metal has no native feedback stage, so the translated +// main0_tf vertex entry writes to four ordinary buffers while rasterization is +// disabled. Later character draws consume those buffers as vertex streams. +enum { TF_SLOT = 8 }; +static __unsafe_unretained id tf_pso(NvShader *vs) { + pso_key key; static const uint32_t no_formats[8]; + pso_key_fill(&key, vs, nil, no_formats, 0, 0, 1, 0); key.topology = MTLPrimitiveTopologyClassPoint; key.fsfn = TF_MARK; + if (!g_bare_native) key.cfmt[0] = MTLPixelFormatRGBA16Float; + __unsafe_unretained id cached = hm_get_id(&g_pso, &key); + if (cached) return cached; + if (hm_get_id(&g_pso_fail, &key)) return nil; + NSError *error = nil; + id pipeline = [g_dev newRenderPipelineStateWithDescriptor:pso_desc(&key, vs, nil, nil, 0) error:&error]; + if (!pipeline) { + MLOG("[RED] failed to create transform-feedback pipeline (vertex %s): %s", vs->key, error.localizedDescription.UTF8String); + hm_put_id(&g_pso_fail, &key, NSNull.null); return nil; + } + hm_put_id(&g_pso, &key, pipeline); + return pipeline; +} +static void tf_pend_add(uint64_t addr, uint64_t size) { + pend_add(addr, size); +} +static void tf_draw(NvProg *pr, const p_draw *dw, const p_draw_idx *di) { + __unsafe_unretained NvShader *vs = pr ? pr->sh[0] : nil; + if (!S.tf_on) { PD("Z %llu draw skipped: rasterizer discard", g_stat_pass + !g_enc); return; } + if (!vs || !vs->tf_fn) { RED_ONCE(100, "transform feedback vertex shader %s has no capture entry point", vs ? vs->key : "-"); return; } + if (!dw || di || dw->ind || dw->mdi_cnt || dw->instances > 1) { + RED_ONCE(101, "unsupported indexed, indirect or multi-instance transform-feedback draw (vertex %s, indexed %d indirect %#llx mdi %#llx instances %d prim %d)", + vs->key, di != NULL, (unsigned long long)(dw ? dw->ind : 0), (unsigned long long)(dw ? dw->mdi_cnt : 0), dw ? dw->instances : 0, dw ? dw->prim : -1); return; + } + if (dw->count <= 0) return; + uint64_t gpu[4] = {0}; + for (int b = 0; b < 4; b++) { + if (!vs->tf_stride[b]) continue; + uint64_t size = (uint64_t)dw->count * vs->tf_stride[b]; + uint64_t at = S.tf[b].addr + S.tf_off[b]; void *buffer; uint64_t offset; + if (!S.tf[b].addr || S.tf_off[b] + size > S.tf[b].size || !mtl_gpu_addr(at, size, &buffer, &offset, &gpu[b])) { + RED_ONCE(102, "transform feedback buffer %d of vertex %s is missing, too small or outside a memory pool", b, vs->key); return; + } + } + __unsafe_unretained id pipeline = tf_pso(vs); + if (!pipeline) return; + rp_end(); + MTLRenderPassDescriptor *pass = [MTLRenderPassDescriptor renderPassDescriptor]; + pass.renderTargetWidth = 1; pass.renderTargetHeight = 1; pass.defaultRasterSampleCount = 1; + if (!g_bare_native) { + pass.colorAttachments[0].texture = diag_c0_texture(1, 1, 1); + if (!pass.colorAttachments[0].texture) { MLOG("[RED] Transform-feedback compatibility target allocation failed"); return; } + pass.colorAttachments[0].loadAction = MTLLoadActionDontCare; + pass.colorAttachments[0].storeAction = MTLStoreActionDontCare; + } + id encoder = enc_r_begin_pass(pass); + if (enc_label_on()) encoder.label = [NSString stringWithFormat:@"transform-feedback %.16s", vs->key]; + enc_r_pso(encoder, pipeline); + bind_vbufs(encoder); + args_dirty_all(); encode_args(encoder, vs, 0, 1); + for (int b = 0; b < 4; b++) if (vs->tf_stride[b]) { + uint64_t base = gpu[b] - (uint64_t)dw->first * vs->tf_stride[b]; + NSUInteger offset = 0; id buffer = legacy_buffer_at(base, &offset); + if (!buffer) { RED_ONCE(103, "transform feedback output address for buffer %d is not registered", b); enc_r_end_pass(encoder); args_dirty_all(); return; } + legacy_bind_buffer(encoder, 0, TF_SLOT + b, buffer, offset); + legacy_use_resource(encoder, buffer, MTLResourceUsageWrite, 0); + uint64_t size = (uint64_t)dw->count * vs->tf_stride[b]; + tf_pend_add(S.tf[b].addr + S.tf_off[b], size); S.tf_off[b] += size; + } + enc_r_draw(encoder, MTLPrimitiveTypePoint, (NSUInteger)dw->first, (NSUInteger)dw->count); + enc_r_end_pass(encoder); + args_dirty_all(); g_enc_pso = nil; g_enc_dss = nil; g_ed.ok = 0; + { static int once; if (!once++) MLOG("transform feedback: first character-skinning capture (vertex %s, prim %d, first %d, %d vertices, %d instances)", vs->key, dw->prim, dw->first, dw->count, dw->instances); } +} + +// iOS 16 indirect draws must not consume unchecked guest records. The record +// is often written by an earlier GPU pass, so reading it on the CPU is stale and +// waiting for a half-finished frame can turn a recoverable GPU fault into a +// permanently ignored queue. Convert and bound it on the same GPU queue into +// a host-owned arena record instead. The following render pass then consumes +// only that sanitized record, with no CPU wait and no mid-submission commit. +#define SAFE_INDIRECT_INDEX_CAP (3u * 16384u) +typedef struct { + __unsafe_unretained id args; + NSUInteger args_off; + __unsafe_unretained id drv0; + NSUInteger drv0_off; + uint64_t drv0_gpu; + __unsafe_unretained id index; + NSUInteger index_off, index_len; + MTLIndexType index_type; + __unsafe_unretained id fence; + int indexed; + unsigned diag_id; uint64_t diag_frame; +} safe_indirect_draw; +static id g_safe_indirect_fence; +#define SAFE_INDIRECT_CACHE_MAX 1024 +typedef struct { + int arrays; + const uint8_t *command; + int consumed; + p_draw dw; + p_draw_idx di; + safe_indirect_draw out; +} safe_indirect_ent; +static safe_indirect_ent g_safe_indirect_cache[SAFE_INDIRECT_CACHE_MAX]; +static int g_safe_indirect_n; +static uint64_t g_safe_indirect_epoch = ~0ull, g_safe_indirect_sub; +static uint64_t g_safe_indirect_batches, g_safe_indirect_batched, g_safe_indirect_splits; + +static void quad_ib_make(void) { + if (g_quad_ib) return; + uint32_t n = 65536; uint32_t *ix = malloc((size_t)n * 6 * 4); + for (uint32_t q = 0; q < n; q++) { uint32_t b = q * 4, v[6] = {b, b + 1, b + 2, b, b + 2, b + 3}; memcpy(ix + q * 6, v, 24); } + g_quad_ib = [g_dev newBufferWithBytes:ix length:(size_t)n * 24 options:MTLResourceStorageModeShared]; + g_quad_gpu = legacy_buffer_gpu(g_quad_ib); res_add(g_quad_ib); free(ix); +} +static void fan_ib_make(void) { + if (g_fan_ib) return; + uint32_t n = 65536; uint32_t *ix = malloc((size_t)n * 3 * 4); + for (uint32_t i = 0; i < n; i++) { uint32_t v[3] = {0, i + 1, i + 2}; memcpy(ix + i * 3, v, 12); } + g_fan_ib = [g_dev newBufferWithBytes:ix length:(size_t)n * 12 options:MTLResourceStorageModeShared]; + g_fan_gpu = legacy_buffer_gpu(g_fan_ib); res_add(g_fan_ib); free(ix); +} + +static const char k_safe_indirect_msl[] = +"#include \nusing namespace metal;\n" +"struct P { uint kind, indexCap, ordinal, hasCount, prim, indexSize, outCap, pad; };\n" +"kernel void safe_args(device const uint *r [[buffer(0)]], device const uint *mc [[buffer(1)]], device uint *o [[buffer(2)]], constant P &p [[buffer(3)]], device uint *drv [[buffer(6)]]) {\n" +" bool live = !p.hasCount || p.ordinal < mc[0]; uint n = live ? r[0] : 0, inst = live ? r[1] : 0;\n" +" if (inst > (1u << 20)) { n = 0; inst = 0; }\n" +" drv[0x644/4] = p.indexSize ? r[4] : r[3];\n" +" if (p.kind == 0) { n = min(n, 1u << 27); o[0]=n; o[1]=inst; o[2]=r[2]; o[3]=r[3]; return; }\n" +" if (p.kind == 1) { uint first=r[2]; n = first < p.indexCap ? min(n, p.indexCap-first) : 0; o[0]=n; o[1]=inst; o[2]=first; o[3]=r[3]; o[4]=r[4]; return; }\n" +" uint maxn = p.prim == 7 ? p.outCap / 6 * 4 : p.outCap / 3 + 2; n=min(n,maxn);\n" +" uint c = p.prim == 7 ? n / 4 * 6 : n >= 3 ? (n - 2) * 3 : 0;\n" +" o[0]=min(c,p.outCap); o[1]=inst; o[2]=0; o[3]=r[2]; o[4]=r[3];\n" +"}\n" +"kernel void safe_expand(device const uint *r [[buffer(0)]], device const uint *mc [[buffer(1)]], device uint *a [[buffer(2)]], constant P &p [[buffer(3)]], device const uchar *ib [[buffer(4)]], device uint *out [[buffer(5)]], device uint *drv [[buffer(6)]], uint i [[thread_position_in_grid]]) {\n" +" bool live = !p.hasCount || p.ordinal < mc[0]; uint first=r[2], avail=first < p.indexCap ? p.indexCap-first : 0;\n" +" uint n=live ? min(r[0],avail) : 0, inst=live ? r[1] : 0; if (inst > (1u << 20)) { n=0; inst=0; }\n" +" uint maxn=p.prim==7 ? p.outCap/6*4 : p.prim==6 ? p.outCap/3+2 : p.outCap; n=min(n,maxn);\n" +" uint m=min(p.prim == 7 ? n/4*6 : p.prim == 6 ? (n >= 3 ? (n-2)*3 : 0) : n,p.outCap);\n" +" if (i==0) { a[0]=m; a[1]=inst; a[2]=0; a[3]=r[3]; a[4]=r[4]; drv[0x644/4]=p.indexSize ? r[4] : r[3]; } if (i>=m) return;\n" +" uint t=i/3,c=i%3,q=p.prim==7 ? t/2*4+(c ? c+(t&1) : 0) : p.prim==6 ? (c ? t+c : 0) : i, x=first+q;\n" +" out[i]=p.indexSize==1 ? uint(ib[x]) : p.indexSize==2 ? uint(((device const ushort*)ib)[x]) : ((device const uint*)ib)[x];\n" +"}\n" +"kernel void snapshot_args(device const uint *r [[buffer(0)]], device const uint *mc [[buffer(1)]], device uint *o [[buffer(2)]], constant P &p [[buffer(3)]]) {\n" +" o[0]=r[0]; o[1]=r[1]; o[2]=r[2]; o[3]=r[3]; o[4]=p.indexSize ? r[4] : 0; o[5]=p.hasCount ? mc[0] : 0xffffffffu; o[6]=p.ordinal; o[7]=p.kind;\n" +"}\n"; + +static int safe_indirect_same(const safe_indirect_ent *e, const p_draw *dw, const p_draw_idx *di) { + if (e->arrays != (dw != NULL)) return 0; + if (dw) return e->dw.prim == dw->prim && e->dw.ind == dw->ind && e->dw.mdi_cnt == dw->mdi_cnt && e->dw.mdi_i == dw->mdi_i; + return e->di.prim == di->prim && e->di.itype == di->itype && e->di.ibuf == di->ibuf && e->di.ind == di->ind + && e->di.mdi_cnt == di->mdi_cnt && e->di.mdi_i == di->mdi_i; +} +static int safe_indirect_find(const p_draw *dw, const p_draw_idx *di, const uint8_t *command) { + for (int i = 0; i < g_safe_indirect_n; i++) { + safe_indirect_ent *e = &g_safe_indirect_cache[i]; + if (!e->consumed && e->command == command && safe_indirect_same(e, dw, di)) return i; + } + return -1; +} +static int safe_indirect_take(const p_draw *dw, const p_draw_idx *di, const uint8_t *command) { + int i = safe_indirect_find(dw, di, command); + if (i >= 0 || g_safe_indirect_n >= SAFE_INDIRECT_CACHE_MAX) return i; + i = g_safe_indirect_n++; + safe_indirect_ent *e = &g_safe_indirect_cache[i]; memset(e, 0, sizeof *e); e->arrays = dw != NULL; e->command = command; + if (dw) e->dw = *dw; else e->di = *di; + return i; +} + +static int safe_indirect_encode(safe_indirect_ent *entry, id ce, + id ps_args, id ps_expand, + id ps_snapshot) { + const p_draw *dw = entry->arrays ? &entry->dw : NULL; + const p_draw_idx *di = entry->arrays ? NULL : &entry->di; + safe_indirect_draw *out = &entry->out; + uint64_t ind = dw ? dw->ind : di->ind, cnt = dw ? dw->mdi_cnt : di->mdi_cnt; + void *rv = NULL, *cv = NULL; uint64_t ro = 0, co = 0; + NSUInteger rec_size = dw ? 16 : 20; + if (!mtl_addr(ind, rec_size, &rv, &ro)) { RED_ONCE(89, "indirect record address %#llx is outside memory pools", (unsigned long long)ind); return 0; } + if (cnt) { if (!mtl_addr(cnt, 4, &cv, &co)) { RED_ONCE(93, "indirect-count address %#llx is outside memory pools", (unsigned long long)cnt); return 0; } } + else { cv = rv; co = ro; } + + typedef struct { uint32_t kind, index_cap, ordinal, has_count, prim, index_size, out_cap, pad; } safe_param; + safe_param p = {.ordinal = (uint32_t)(dw ? dw->mdi_i : di->mdi_i), .has_count = cnt != 0, + .prim = (uint32_t)(dw ? dw->prim : di->prim), .out_cap = SAFE_INDIRECT_INDEX_CAP}; + id ab; size_t ao = arena_alloc(32, &ab); + id db; size_t doff = arena_alloc(DRV_SIZE, &db); + memset((uint8_t *)db.contents + doff, 0, DRV_SIZE); + out->args = ab; out->args_off = ao; out->index = nil; out->index_off = out->index_len = 0; out->indexed = 0; + out->drv0 = db; out->drv0_off = doff; out->drv0_gpu = legacy_buffer_gpu(db) + doff; + if (!g_safe_indirect_fence) g_safe_indirect_fence = [g_dev newFence]; + out->fence = g_safe_indirect_fence; + + int special = di && (di->itype == 0 || di->prim == 6 || di->prim == 7); + void *iv = NULL; uint64_t io = 0; NSUInteger isz = di ? (di->itype == 0 ? 1 : di->itype == 1 ? 2 : 4) : 0; + if (di) { + if (!mtl_addr(di->ibuf, 1, &iv, &io)) { RED_ONCE(22, "indirect index buffer is outside memory pools"); return 0; } + p.index_cap = (uint32_t)MIN(((__bridge id)iv).length - io, (NSUInteger)UINT32_MAX) / (uint32_t)isz; + p.index_size = (uint32_t)isz; + } + if (dw && (dw->prim == 6 || dw->prim == 7)) { + if (dw->prim == 7) quad_ib_make(); else fan_ib_make(); + out->indexed = 1; out->index = dw->prim == 7 ? g_quad_ib : g_fan_ib; out->index_type = MTLIndexTypeUInt32; + out->index_len = out->index.length; p.kind = 2; p.out_cap = (uint32_t)(out->index_len / 4); + } else if (di && !special) { + out->indexed = 1; out->index = (__bridge id)iv; out->index_off = (NSUInteger)io; + out->index_len = out->index.length - out->index_off; out->index_type = di->itype == 1 ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32; p.kind = 1; + } else if (!di) p.kind = 0; + + [ce setBuffer:(__bridge id)rv offset:(NSUInteger)ro atIndex:0]; + [ce setBuffer:(__bridge id)cv offset:(NSUInteger)co atIndex:1]; + id snapshot = nil; size_t snapshot_off = 0; + if (ps_snapshot && geometry_diag_frame() && g_commit_blocks && g_geom_diag.indirect < 512) { + snapshot_off = arena_alloc(32, &snapshot); + out->diag_id = (unsigned)++g_geom_diag.indirect; out->diag_frame = g_geom_diag.frame; + [ce setBuffer:snapshot offset:snapshot_off atIndex:2]; [ce setBytes:&p length:sizeof p atIndex:3]; + legacy_use_resource(ce, (__bridge id)rv, MTLResourceUsageRead, 1); + legacy_use_resource(ce, (__bridge id)cv, MTLResourceUsageRead, 1); + legacy_use_resource(ce, snapshot, MTLResourceUsageWrite, 1); + enc_c_pso(ce, ps_snapshot); enc_c_dispatch(ce, MTLSizeMake(1, 1, 1), MTLSizeMake(1, 1, 1)); + } + [ce setBuffer:ab offset:ao atIndex:2]; [ce setBytes:&p length:sizeof p atIndex:3]; [ce setBuffer:db offset:doff atIndex:6]; + legacy_use_resource(ce, (__bridge id)rv, MTLResourceUsageRead, 1); + legacy_use_resource(ce, (__bridge id)cv, MTLResourceUsageRead, 1); + legacy_use_resource(ce, ab, MTLResourceUsageWrite, 1); + legacy_use_resource(ce, db, MTLResourceUsageWrite, 1); + if (special) { + id xb; size_t xo = arena_alloc((size_t)SAFE_INDIRECT_INDEX_CAP * 4, &xb); + out->indexed = 1; out->index = xb; out->index_off = xo; out->index_len = (size_t)SAFE_INDIRECT_INDEX_CAP * 4; out->index_type = MTLIndexTypeUInt32; + [ce setBuffer:(__bridge id)iv offset:(NSUInteger)io atIndex:4]; [ce setBuffer:xb offset:xo atIndex:5]; + legacy_use_resource(ce, (__bridge id)iv, MTLResourceUsageRead, 1); legacy_use_resource(ce, xb, MTLResourceUsageWrite, 1); + enc_c_pso(ce, ps_expand); enc_c_dispatch(ce, MTLSizeMake(SAFE_INDIRECT_INDEX_CAP / 64, 1, 1), MTLSizeMake(64, 1, 1)); + } else { enc_c_pso(ce, ps_args); enc_c_dispatch(ce, MTLSizeMake(1, 1, 1), MTLSizeMake(1, 1, 1)); } + static _Atomic uint32_t diag_n; + uint32_t diag_i = atomic_fetch_add_explicit(&diag_n, 1, memory_order_relaxed); + if (diag_i < 128 && g_commit_blocks) { + id keep = ab, keep_drv = db; NSUInteger keep_off = ao, keep_drv_off = doff; uint64_t keep_ind = ind, keep_cnt = cnt; + uint32_t keep_ordinal = p.ordinal, keep_kind = p.kind, keep_cap = p.index_cap; + [g_commit_blocks addObject:^{ + const uint32_t *v = (const uint32_t *)((const uint8_t *)keep.contents + keep_off); + const uint32_t *dv = (const uint32_t *)((const uint8_t *)keep_drv.contents + keep_drv_off); + MLOG("safe-indirect result #%u kind=%u source=%#llx multi=%#llx[%u] cap=%u -> %u,%u,%u,%d,%u drvBaseInstance=%u", + diag_i, keep_kind, (unsigned long long)keep_ind, (unsigned long long)keep_cnt, keep_ordinal, keep_cap, + v[0], v[1], v[2], (int32_t)v[3], keep_kind ? v[4] : 0, dv[0x644 / 4]); + }]; + } + if (snapshot) { + id raw_keep = snapshot, args_keep = ab, drv_keep = db; + size_t raw_offset = snapshot_off, args_offset = ao, drv_offset = doff; + uint64_t frame = out->diag_frame, source = ind, count_source = cnt; + unsigned record = out->diag_id; safe_param params = p; + [g_commit_blocks addObject:^{ + const uint32_t *r = (const uint32_t *)((const uint8_t *)raw_keep.contents + raw_offset); + const uint32_t *v = (const uint32_t *)((const uint8_t *)args_keep.contents + args_offset); + const uint32_t *drv = (const uint32_t *)((const uint8_t *)drv_keep.contents + drv_offset); + MLOG("[geometry-indirect] frame=%llu rec=%u source=%#llx count_source=%#llx ordinal=%u kind=%u prim=%u cap=%u raw=%u,%u,%u,%d,%u multi_count=%u result=%u,%u,%u,%d,%u driver_base=%u", + (unsigned long long)frame, record, (unsigned long long)source, (unsigned long long)count_source, + params.ordinal, params.kind, params.prim, params.index_cap, r[0], r[1], r[2], (int32_t)r[3], r[4], r[5], + v[0], v[1], v[2], (int32_t)v[3], params.kind ? v[4] : 0, drv[0x644 / 4]); + }]; + } + return 1; +} + +static int safe_indirect_prepare(const p_draw *dw, const p_draw_idx *di, safe_indirect_draw *out) { + uint64_t ind = dw ? dw->ind : di->ind; + if (!ind) return 1; + static id ps_args, ps_expand, ps_snapshot; static int failed; + if (failed) return 0; + if (!ps_args) { + NSError *error = nil; id lib = [g_dev newLibraryWithSource:@(k_safe_indirect_msl) options:nil error:&error]; + id fa = [lib newFunctionWithName:@"safe_args"], fe = [lib newFunctionWithName:@"safe_expand"]; + ps_args = fa ? [g_dev newComputePipelineStateWithFunction:fa error:&error] : nil; + ps_expand = fe ? [g_dev newComputePipelineStateWithFunction:fe error:&error] : nil; + if (geometry_diag_file()) { + id fs = [lib newFunctionWithName:@"snapshot_args"]; + ps_snapshot = fs ? [g_dev newComputePipelineStateWithFunction:fs error:&error] : nil; + if (!ps_snapshot) MLOG("[geometry] indirect snapshot pipeline failed: %s", error.localizedDescription.UTF8String); + } + if (!ps_args || !ps_expand) { failed = 1; MLOG("[RED] safe indirect conversion pipeline failed: %s", error.localizedDescription.UTF8String); return 0; } + } + if (g_safe_indirect_epoch != g_gpu_write_epoch || g_safe_indirect_sub != g_sub_serial) { + g_safe_indirect_n = 0; g_safe_indirect_epoch = g_gpu_write_epoch; g_safe_indirect_sub = g_sub_serial; + } + // Equal guest records do not imply equal driver state: shadow and main + // passes bind different SSBO tables. Each command occurrence owns its c0, + // and consuming it prevents a later draw from overwriting in-flight state. + int hit = safe_indirect_find(dw, di, g_exec_next); + if (hit >= 0) { g_safe_indirect_cache[hit].consumed = 1; *out = g_safe_indirect_cache[hit].out; return out->args != nil; } + if (g_safe_indirect_n > SAFE_INDIRECT_CACHE_MAX - 64) g_safe_indirect_n = 0; + int first = g_safe_indirect_n, current = safe_indirect_take(dw, di, g_exec_next); + if (current < 0) return 0; + if (g_exec_next && g_ret_n <= 8) { + fz_cursor cur = {.p = g_exec_next, .ret_n = g_ret_n}; memcpy(cur.ret, g_ret, sizeof(*g_ret) * (size_t)g_ret_n); + const uint8_t *q; op_hdr h; const void *a; + for (int guard = 0; guard < 200000 && g_safe_indirect_n < SAFE_INDIRECT_CACHE_MAX && fz_next(&cur, &q, &h, &a); guard++) { + if (h.op == OP_DISPATCH || h.op == OP_COPY_B2B || h.op == OP_UPDATE_UB || h.op == OP_CLEAR_BUF || h.op == OP_COPY_T2B) break; + p_draw dt; p_draw_idx it; + const p_draw *d = h.op == OP_DRAW ? draw_rec(a, h.size, &dt) : NULL; + const p_draw_idx *x = h.op == OP_DRAW_IDX ? draw_idx_rec(a, h.size, &it) : NULL; + if (d && d->ind) safe_indirect_take(d, NULL, q + h.size); + else if (x && x->ind) safe_indirect_take(NULL, x, q + h.size); + } + } + if (g_enc) { g_safe_indirect_splits++; rp_end(); } + if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + id ce = enc_c_begin(NULL); + int made = 0; + for (int i = first; i < g_safe_indirect_n; i++) made += safe_indirect_encode(&g_safe_indirect_cache[i], ce, ps_args, ps_expand, ps_snapshot); + if (g_safe_indirect_fence) [ce updateFence:g_safe_indirect_fence]; + enc_c_end(ce); args_dirty_all(); g_enc_pso = nil; g_enc_dss = nil; g_ed.ok = 0; + g_safe_indirect_batches++; g_safe_indirect_batched += (uint64_t)(g_safe_indirect_n - first); + if (g_safe_indirect_batches == 1 || !(g_safe_indirect_batches % 1800)) + MLOG("iOS16 batched safe indirect: batch %llu converted %d records (cumulative %llu), render-pass splits %llu; explicit compute-to-indirect fence, no CPU wait or mid-frame commit", + (unsigned long long)g_safe_indirect_batches, made, (unsigned long long)g_safe_indirect_batched, (unsigned long long)g_safe_indirect_splits); + if (!g_safe_indirect_cache[current].out.args) return 0; + g_safe_indirect_cache[current].consumed = 1; + *out = g_safe_indirect_cache[current].out; + { static int once; if (!once++) MLOG("indirect c0 ownership: one immutable driver block per command occurrence; GPU baseInstance preserved across split commits"); } + return 1; +} + +static void draw(const p_draw *dw, const p_draw_idx *di) { + if (!S.prog) { RED_ONCE(18, "绘制前没有绑定程序"); PD("Z %llu 绘制跳过:没有绑定程序", g_stat_pass + !g_enc); return; } + safe_indirect_draw sid = {0}; + if ((dw && dw->ind) || (di && di->ind)) if (!safe_indirect_prepare(dw, di, &sid)) return; + g_draw_binst = (uint32_t)(dw ? dw->base_instance : di ? di->base_instance : 0); + int driver_changed = sid.drv0 != g_indirect_drv0_buf; + g_indirect_drv0_buf = sid.drv0; g_indirect_drv0_off = sid.drv0_off; g_indirect_drv0_gpu = sid.drv0_gpu; + if (sid.drv0 || driver_changed) g_args_dirty[0] = 1; // also invalidate when returning to a direct draw + __unsafe_unretained NvProg *pr = prog_get(S.prog); + NSUInteger slices = !g_slice_replaying && !S.rast_discard ? slice_target_count() : 1; + if (slices > 1) { + int unsupported = !pr || !pr->sh[0] || (dw && dw->ind) || (di && di->ind) || g_defer.active || g_fz.active; + if (pr) for (int st = 2; st < 6; st++) unsupported |= pr->sh[st] != nil; + if (pr && pr->sh[1]) for (int i = 0; i < pr->sh[1]->rf.nb; i++) { + const nvn_rbind *b = &pr->sh[1]->rf.b[i]; + unsupported |= b->kind == NVN_RB_IMG || b->kind == NVN_RB_IMGA || (b->kind == NVN_RB_SBUFP && b->rw); + } + if (unsupported) { + MLOG("[RED] Per-slice draw unsupported: indirect, missing shader, writable resources or active geometry/merge path"); + return; + } + rp_end(); + typeof(R) saved = R; g_slice_replaying = 1; + for (uint32_t slice = 0; slice < slices; slice++) { + R = saved; slice_select(slice); + if (pr->sh[0]->slice_variant || slice == 0) draw(dw, di); + else { int pending = 0; for (int i = 0; i < 8; i++) pending |= R.clear_c[i]; if (pending) rp_begin_ex(1); } + rp_flush(); + } + R = saved; memset(R.clear_c, 0, sizeof R.clear_c); R.clear_d = R.clear_s = 0; + g_slice_replaying = 0; g_selected_slice = 0; args_dirty_all(); g_pso_dirty = 1; + static int notes; if (notes++ < 8) MLOG("Per-slice draw: %lu depth planes, vs=%s fs=%s; original instance/baseInstance preserved", (unsigned long)slices, pr->sh[0]->key, pr->sh[1] ? pr->sh[1]->key : "-"); + return; + } + if (sid.diag_id) MLOG("[geometry-draw] phase=replay frame=%llu rec=%u cmd=%p vs=%s fs=%s cull=%u depth=%u/%u/%u stencil=%u scissor=%d,%d,%d,%d", + (unsigned long long)sid.diag_frame, sid.diag_id, g_exec_next, pr && pr->sh[0] ? pr->sh[0]->key : "-", + pr && pr->sh[1] ? pr->sh[1]->key : "-", S.poly.cull, S.ds.dt, S.ds.dw, S.ds.df, S.ds.st, + R.sc.x, R.sc.y, R.sc.w, R.sc.h); + if (S.rast_discard) { tf_draw(pr, dw, di); return; } + if (g_nubd && g_enc && ubd_conflict(pr, dw, di)) { g_ubd_conflicts++; rp_end(); } // land the deferred inline updates before this draw (see ubd_conflict) + if (g_ruler_truth == 0) { // Ruler negative control (DM_NVN_RULER_NEG=): in the mode under test drop this shader's draws, and the ruler must report non-equivalence + static const char *neg; static int ng = -1; if (ng < 0) { neg = getenv("DM_NVN_RULER_NEG"); ng = neg && *neg; } + if (ng && pr) for (int st = 0; st < 5; st++) { __unsafe_unretained NvShader *sh = pr->sh[st]; if (sh && sh->stage == 1 && !strncmp(sh->key, neg, strlen(neg))) { static int once; if (!once++) MLOG("DM_NVN_RULER_NEG:被测模式丢弃着色器 %s 的绘制", sh->key); return; } } + } + if (zskip_can_draw(pr)) { // Zero-source skip: the output is always 0, writing 0 over 0, so this draw changes no result (see zskip_can_draw) + g_zskip_count++; { static int once; if (!once++) MLOG("零源跳过:首例 通道 %llu", g_stat_pass + 1); } + PD("N %llu 零源跳过", g_stat_pass + 1); return; } + if (!g_defer_replaying) { + if (g_defer.active && g_defer.in_p && !g_defer.at_q) { if (defer_take(dw, di)) return; } + } + // Draws that cannot draw anything (both faces culled, empty scissor) return before the render pass is opened: otherwise the render pass is already open and on TBDR the whole attachment is loaded and stored for nothing + // (Mac character select page: 3 render passes are opened only because of such draws, all loaded, zero draws) + // Diagnostic switch for measuring an upper bound DM_NVN_SKIP_FS=[,...] (none by default): drops all draws of these fragment shaders (the picture will be wrong; only used in real-device ABA to measure "how much GPU time this class of draws is worth in total") + { static char sk[16][20]; static int nsk = -1; + if (nsk < 0) { nsk = 0; for (const char *q = getenv("DM_NVN_SKIP_FS"); q && *q && nsk < 16; q = strchr(q, ',') ? strchr(q, ',') + 1 : q + strlen(q)) { size_t n = strcspn(q, ","); if (n > 16) n = 16; memcpy(sk[nsk], q, n); sk[nsk++][n] = 0; } + if (nsk) MLOG("DM_NVN_SKIP_FS:丢弃 %d 个片元着色器的绘制(诊断,画面不对)", nsk); } + if (g_ub_n > 0 && pr->sh[1] && !g_defer_replaying && ub_skip(pr->sh[1]->key)) return; // In-process alternating measurement: this phase drops this class + if (nsk && pr->sh[1] && !g_defer_replaying) for (int i = 0; i < nsk; i++) if (!strncmp(pr->sh[1]->key, sk[i], strlen(sk[i]))) return; + if (ms_draw(pr)) return; } + // Similar diagnostic DM_NVN_SKIP_SMALL=: drop all draws whose target width (by the attached level) is <= this value -- measures how much "dropping tiny render passes in bulk" is worth under the parallel pipeline (fixed per-pass overhead) + { static int small = -1; if (small < 0) { const char *e = getenv("DM_NVN_SKIP_SMALL"); small = e ? atoi(e) : 0; if (small) MLOG("DM_NVN_SKIP_SMALL:丢弃目标宽度 ≤ %d 的绘制(诊断,画面不对)", small); } + if (small > 0 && R.has_rt && !g_defer_replaying) { int k = R.rt.n && R.rt.color[0] ? 0 : R.rt.depth ? 8 : -1; + if (k >= 0) { uint64_t s_; int lv, sl; rt_sub(&R.rt, k, &s_, &lv, &sl); const tex_t *t = k == 8 ? R.rt.depth : R.rt.color[0]; if ((int)MAX(t->d.width >> lv, 1u) <= small) return; } } } + // Experimental switch DM_NVN_NOFXAA=1 (off by default; keep or drop decided by the visual ruler + looking at the picture on a real machine): skips the FXAA full-screen draw, and the later sharpening / upscale reads FXAA's input instead (the tone mapping result) + { static int nofxaa = -1; static uint64_t fx_from, fx_to; if (nofxaa < 0) { const char *e = getenv("DM_NVN_NOFXAA"); nofxaa = e && !strcmp(e, "1"); if (nofxaa) MLOG("DM_NVN_NOFXAA=1:跳过 FXAA"); } + if (nofxaa && tbdr_on() && !g_defer_replaying && pr->sh[1]) { NvShader *f = pr->sh[1]; + if (!strncmp(f->key, "ebcfcd1fa0a77dbc", 16) && !g_enc && R.rt.n == 1 && R.rt.color[0] && !R.clear_c[0]) { + uint64_t h = 0; for (int i = 0; i < f->rf.nb && !h; i++) if (f->rf.b[i].kind == NVN_RB_TEX && f->rf.b[i].reg < NTEX) h = S.tex[1][f->rf.b[i].reg]; + if (h) { fx_from = R.rt.color[0]->serial; fx_to = h; PD("N %llu 跳过 FXAA:ser=%llu 的读者改读 ser=%llu", g_stat_pass, (unsigned long long)fx_from, (unsigned long long)h_ser(h)); return; } } + else if (fx_from && !strncmp(f->key, "52aac1f2306b8fc5", 16)) { for (int r = 0; r < NTEX; r++) if (S.tex[1][r] && h_ser(S.tex[1][r]) == fx_from) S.tex[1][r] = fx_to; fx_from = 0; } } } + { static uint64_t bloomq_frame; if (bloomq_frame != atomic_load(&g_cur_frame)) { bloomq_frame = atomic_load(&g_cur_frame); bloomq_reset(); } + if (bloomq_enabled() && tbdr_on() && !g_defer_replaying && pr->sh[1]) { + NvShader *f = pr->sh[1]; uint64_t tgt = 0; int lv, sl; + if (!strncmp(f->key, "2938a5b71b8e21b2", 16)) { + if (g_bloomq.state) bloomq_reset(); + if (bloomq_target(&tgt, &lv, &sl) && R.rt.color[0]) { + uint64_t h = 0; const tex_t *st = NULL; int cbuf = 0; + for (int i = 0; i < f->rf.nb; i++) { nvn_rbind *b = &f->rf.b[i]; + if (b->kind == NVN_RB_TEX && b->reg < NTEX && !h) { h = S.tex[1][b->reg]; int tid = (int)(h & 0xfffff); if (tid > 0 && tid < g_ntreg && g_treg[tid].ok) st = &g_treg[tid].t; } + if (b->kind == NVN_RB_SAMPT && b->reg < NTEX) g_bloomq.samp_h = S.tex[1][b->reg] >> 20; + if (b->kind == NVN_RB_SAMP && b->reg < 128) g_bloomq.samp_h = S.samp[1][b->reg] >> 20; + if (b->kind == NVN_RB_CBUF && !cbuf) cbuf = ubo_float(1, (uint16_t)(b->reg << 8), &g_bloomq.threshold); // c7[0].x + } + if (h && st && cbuf && f->rf.c1_len >= sizeof(float) * 2) { + float c1[2]; memcpy(c1, f->rf.c1, sizeof c1); g_bloomq.s_tex = st; g_bloomq.s_h = h; g_bloomq.s_ser = st->serial; g_bloomq.b0_ser = tgt; g_bloomq.coeff_g = c1[0]; g_bloomq.coeff_b = c1[1]; g_bloomq.state = 1; + { static int once; if (!once++) MLOG("泛光 1/4 起点:生效(S ser=%llu B0 ser=%llu)", (unsigned long long)g_bloomq.s_ser, (unsigned long long)g_bloomq.b0_ser); } PD("A %llu 泛光 1/4 起点 S=%llu B0=%llu", g_stat_pass, (unsigned long long)g_bloomq.s_ser, (unsigned long long)g_bloomq.b0_ser); R.clear_c[0] = 0; // The whole B0 level is no longer needed: drop the pending clear too, otherwise a clear-only render pass would be opened just for it + return; + } + } + } else if (g_bloomq.state == 1 && !strncmp(f->key, "019ce2f82edbda84", 16)) { + uint64_t src = 0; for (int i = 0; i < f->rf.nb; i++) if (f->rf.b[i].kind == NVN_RB_TEX && f->rf.b[i].reg < NTEX) { src = h_ser(S.tex[1][f->rf.b[i].reg]); break; } + if (src == g_bloomq.b0_ser && bloomq_target(&tgt, &lv, &sl) && bloomq_capture()) { R.clear_c[0] = 0; g_bloomq.b1_ser = tgt; g_bloomq.state = 2; PD("B %llu 泛光 1/4 原生截取 B0=%llu B1=%llu", g_stat_pass, (unsigned long long)g_bloomq.b0_ser, (unsigned long long)g_bloomq.b1_ser); return; } + RED_ONCE(62, "泛光 1/4 起点:截取已跳过但原生截取没画成,本帧缺泛光"); bloomq_reset(); + } else if (g_bloomq.state == 2 && !strncmp(f->key, "019ce2f82edbda84", 16)) { + for (int i = 0; i < f->rf.nb; i++) if (f->rf.b[i].kind == NVN_RB_TEX && f->rf.b[i].reg < NTEX && h_ser(S.tex[1][f->rf.b[i].reg]) == g_bloomq.b1_ser) { g_bloomq.b1_h = S.tex[1][f->rf.b[i].reg]; break; } + } else if (g_bloomq.state == 2 && !strncmp(f->key, "cc264583f2000c81", 16)) { + if (bloomq_target(&tgt, &lv, &sl) && tgt == g_bloomq.b0_ser) { g_bloomq.state = 3; PD("C %llu 泛光 1/4 跳过 B0 上采样", g_stat_pass); return; } + } else if (g_bloomq.state == 3 && !strncmp(f->key, "2e2d6700a2569795", 16)) { + if (g_bloomq.b1_h) { for (int r = 0; r < NTEX; r++) if (S.tex[1][r] && h_ser(S.tex[1][r]) == g_bloomq.b0_ser) S.tex[1][r] = g_bloomq.b1_h; PD("D %llu 泛光 1/4 叠加改读 B1", g_stat_pass); g_bloomq_last_b0 = g_bloomq.b0_ser; g_bloomq_last_b1h = g_bloomq.b1_h; g_bloomq_last_b1s = g_bloomq.b1_ser; bloomq_reset(); } + else { RED_ONCE(63, "泛光 1/4 状态缺少 B1 句柄"); bloomq_reset(); } + } else if (!g_bloomq.state && g_bloomq_last_b1h && h_ser(g_bloomq_last_b1h) == g_bloomq_last_b1s && !strncmp(f->key, "2e2d6700a2569795", 16)) { // The B1 handle still points to the same texture (not recognized after a rebuild on resolution change) + // The bloom overlay is reached without the bloom chain having run this frame (the extrapolated frame of runtime extrapolation skipped it: outside the region the input 469 is the previous frame's LDR, so it cannot run): redirect the read to B1 as last time, overlaying the previous frame's bloom. + // Without the redirect it would read B0 -- with bloom 1/4 on, B0's upsample is always skipped and never written, so what is read is uninitialized video memory (on M4 Pro the units in the region turn entirely green; elsewhere it is mostly 0, i.e. missing bloom) + int hit = 0; for (int r = 0; r < NTEX; r++) if (S.tex[1][r] && h_ser(S.tex[1][r]) == g_bloomq_last_b0) { S.tex[1][r] = g_bloomq_last_b1h; hit = 1; } + if (hit) { PD("D %llu 泛光链没跑:叠加沿用上一帧 B1", g_stat_pass); static uint64_t n; if (n++ % 600 == 0) MLOG("泛光 1/4:泛光链没跑的帧叠加沿用上一帧 B1(第 %llu 次)", (unsigned long long)n); } + } else if (g_bloomq.state) { RED_ONCE(61, "泛光 1/4 起点:链中途出现别的绘制,本帧泛光不完整"); bloomq_reset(); } + } + } + if (S.poly.cull == NVN_FACE_FRONT_AND_BACK) { PD("Z %llu 绘制跳过(开通道前):两面全剔", g_stat_pass + !g_enc); return; } + // Opt-in fault localization: give selected draws their own encoder labels. + // Keep the original draw, resources, attachment load/store and query path. + static const char *split_keys; static int split_init; static unsigned split_count; + if (!split_init) { split_init = 1; split_keys = getenv("DM_NVN_DIAG_SPLIT_DRAW"); } + if (split_keys && *split_keys && split_count < 2048 && + ((pr->sh[0] && sample_trace_key(split_keys, pr->sh[0]->key)) || + (pr->sh[1] && sample_trace_key(split_keys, pr->sh[1]->key)))) { + split_count++; rp_end(); + } + // The continued render pass may have been opened with fewer depth / stencil attachments (open modes 2 / 3 / 4): if the plane this draw needs is not attached, split before it (on reopen, attach per the new lookahead) + if (g_rt_merged && g_enc_mode != 0 && (((S.ds.dt || S.ds.dw) && !g_pass.hd) || (S.ds.st && !g_pass.hs))) { + static int once2; if (!once2++) MLOG("同目标续用:续用通道内绘制要的深度 / 模板平面没挂,在此切开(首例 通道 %llu)", g_stat_pass); + PD("M %llu 续用通道内绘制要的深度 / 模板平面没挂,在此切开", g_stat_pass); rp_end(); } + if (g_rt_range_active && (!rt_range_pr_ro(pr) || !S.ds.dt || S.ds.dw || S.ds.st)) rp_end(); // The external write visibility at the original encoder boundary is not removed + if (g_rt_merged && draw_samples_rt(pr)) { static int once; if (!once++) MLOG("同目标续用:续用通道内绘制要采样附件,在此切开(首例 通道 %llu)", g_stat_pass); + PD("M %llu 续用通道内绘制要采样附件,在此切开", g_stat_pass); rp_end(); } + static id fz_fn; // Specialized fragment function for this draw of the later pass in a merged render pass (nil = use the original version) + if (g_fz.active && g_enc && g_fz.phase == 2 && !fz_p2_draw(pr, dw, di, g_fz.lmap2, &fz_fn)) { // Cannot be proven: end the merged render pass (the earlier pass's result is stored as usual) and reopen with the later pass's original targets + static int once3; if (!once3++) MLOG("可读 tile 通道合并:后一个通道的绘制证不出,退回分开(首例 通道 %llu)", g_stat_pass); + g_fz_fallback++; rp_end(); } + fb_prepare(pr); // same-pass feedback sampling: copy the snapshot first (see fb_prepare) + if (R.has_sc && !g_enc) { + NSUInteger tw0 = 0, th0 = 0; rt_dims(&tw0, &th0); + long x0 = R.sc.x < 0 ? 0 : R.sc.x, y0 = R.sc.y < 0 ? 0 : R.sc.y, x1 = (long)R.sc.x + R.sc.w, y1 = (long)R.sc.y + R.sc.h; + if (x1 > (long)tw0) x1 = tw0; if (y1 > (long)th0) y1 = th0; + if (x1 <= x0 || y1 <= y0) { PD("Z %llu 绘制跳过(开通道前):剪裁为空(%d,%d %dx%d,目标 %lux%lu)", g_stat_pass + 1, R.sc.x, R.sc.y, R.sc.w, R.sc.h, (unsigned long)tw0, (unsigned long)th0); return; } + } +#ifdef NVN_RT_RANGE_TEST + if (!g_enc) rr_begin(); +#endif + int defer_q_draw = !g_enc && !g_defer_replaying && g_defer_natq > 0; // The draw that opens Q's render pass: after the render pass is opened as usual through hoist_try / fuse_try, replay P first and then draw this one + if (g_frz_state) goto frz_plain; // Full frame / frozen frame of world freeze: no hoisting, no deferral, no merging (the pass-skipping state machine must see every set-target) + if (!g_enc && tbdr_on()) hoist_try(); // First hoist and execute the pure-clear render passes sandwiched between this render pass and its continuation (before rp_begin_ex, not occupying this render pass) + // The deferral decision comes after hoist_try: hoisted pure-clear render passes are skipped in the lookahead, and only then can P's same-target continuation segment be recognized + if (!g_enc && tbdr_on() && !defer_has_in_p() && !g_defer_replaying && !g_fz.active && !g_fz_checking && g_exec_next) { defer_try(dw, di); if (g_defer_cur->active && g_defer_cur->in_p) return; } + if (!g_enc && tbdr_on() && !g_fz_checking) fuse_try(); // Readable tile render pass merge is on by default; when the structure / same-pixel proof does not hold it still falls back to a normal render pass, and it is not re-entered during the CHECK redraw +frz_plain:; + int prim = dw ? dw->prim : di->prim; + uint8_t topology = pso_topology_class(prim); + if (g_pso_topology != topology) { g_pso_topology = topology; g_pso_dirty = 1; } + if (!zero_image_prepare(pr, dw, di, g_defer.active || g_fz.active)) return; + int need = g_enc || !tbdr_on() ? 3 : pass_ds_need(), mode = defer_q_draw ? 0 : need == 0 ? 2 : need == 2 ? 3 : need == 1 ? 4 : 0; // 2 attach neither, 3 attach stencil only, 4 attach depth only + int skip_d = mode != 0; + if (!g_enc) { g_fd_dw = dw; g_fd_di = di; } + id en = rp_begin_ex(mode); + g_fd_dw = NULL; g_fd_di = NULL; + if (!en) { PD("Z %llu 绘制跳过:没开成渲染通道", g_stat_pass); return; } // Z lines: why a draw was dropped at these early returns + if (defer_q_draw) defer_replay(); + if (skip_d) { g_stat_ds_omit++; if (mode == 3) g_stat_s_only++; else if (mode == 4) g_stat_d_only++; } + // Sampler-specialized fragments select their pipeline from the actual + // argument-block snapshot, not a second read of mutable descriptor IDs. + // fb_prepare has already supplied any feedback view. Bind this block once; + // a registry update during compilation is handled on the next encode. + int min_fs = min_reduction_candidate(pr->sh[1]); + if (min_fs) { g_kd_force = kd_draw(pr, dw, di); encode_args(en, pr->sh[1], 1, 0); } + // The pipeline depends only on (program, render pass targets, blend / color / write mask / multisample state, vertex layout): if these did not change, reuse the previous one + static id cur_ps; static NvProg *cur_pr; static uint64_t cur_ps_pass; + if (g_fz.active && !g_defer_replaying) { g_pso_lmap = g_fz.phase == 1 ? g_fz.lmap1 : g_fz.lmap2; g_pso_fsfn = g_fz.phase == 1 ? nil : fz_fn; g_pso_fz = g_fz.phase == 1 ? (pso_fz_desc){0} : g_fz_desc; g_pso_dirty = 1; } // Readable tile merge: fetch the pipeline by the physical -> logical mapping and the specialization + if (g_pso_dirty || pr != cur_pr || cur_ps_pass != g_pass.serial || min_fs) { cur_ps = pso_get(pr, nil); cur_pr = pr; cur_ps_pass = g_pass.serial; g_pso_dirty = !cur_ps; } + if (g_pso_lmap) { g_pso_lmap = NULL; g_pso_fsfn = nil; memset(&g_pso_fz, 0, sizeof g_pso_fz); g_pso_dirty = 1; } // The next draw fetches again according to its own situation + __unsafe_unretained id ps = cur_ps; + if (!ps) { PD("Z %llu 绘制跳过:没有渲染管线(顶点 %s 片元 %s)", g_stat_pass, pr->sh[0] ? pr->sh[0]->key : "-", pr->sh[1] ? pr->sh[1]->key : "-"); return; } + enc_r_pso(en, ps); g_enc_pso = ps; + static id cur_ds; static uint64_t cur_ds_pass; + if (g_dss_dirty || cur_ds_pass != g_pass.serial) { cur_ds = dss_get(g_pass.hd, g_pass.hs); cur_ds_pass = g_pass.serial; g_dss_dirty = 0; } + enc_r_dss(en, cur_ds); g_enc_dss = cur_ds; + { uint32_t sr = g_rt_ovr && g_pass.hs ? (g_rt_ovr == 3 ? 0x40 : 0) : S.st_ref[0]; // enc_r_stencil_ref sends the front value only + if (!(g_ed.ok & ED_SREF) || g_ed.sref != sr) { enc_r_stencil_ref(en, sr, sr); g_ed.sref = sr; g_ed.ok |= ED_SREF; } } + // Viewport / scissor (clamped within the target size) + NSUInteger tw = g_pass.tw, th = g_pass.th; + // When the UI rectangle does not touch the top edge of the picture (iPad true full screen: the UI is laid out at 16:9 at the bottom of the picture, see dm/host/dm_uiscreen.c), UI draws are shifted down as a whole: a vertical offset is added to the projection matrix (ui_mat_patch), and the scissor moves down with it. + // A UI draw = 4 vertices, 2 attributes, 20-byte vertices, in a pass of its own and composited at end of frame (measured: startup loading, character select, character creation, entering-game loading, in-game, open panel, and full-screen map never mix in other draws). + // When the matrix cannot be read this time (there are still unlanded writes in this submit), fall back to moving the viewport: same position, except that widgets lifted to the top get clipped by the viewport's top edge this one time. + // A scissor starting from the top edge (y <= 0) does not have its top edge moved down: when the host lifts top-anchored widgets (minimap, area name etc.) back to the real top of the picture, they are drawn above the UI rectangle and must not be clipped. + // Exception: the untextured block that exactly covers the whole UI rectangle (the dimming when a panel opens, the black curtain of a transition) is changed to cover the whole picture -- the viewport is scaled vertically by 1/h and not moved down, otherwise the 3D outside the UI rectangle is left with a light/dark dividing line. + // When nobody registered the entry or the UI rectangle is the whole picture (iPhone, Mac default), vo = so = 0, vs = 1, the matrix is not changed, and what follows is word-for-word the same as before. + long vo = 0, so = 0; double vs = 1, moff = 0; + if (g_ui_rect_fn && dw && dw->count == 4 && S.nattr == 2 && S.stream[0].stride == 20) { double uy = 0, uh = 1; g_ui_rect_fn(&uy, &uh); + if (uh > 0 && uh < 1) { if (ui_cover_all(uh)) vs = 1 / uh; else if (uy > 0) { so = lround(uy * th); if (ui_mat_ok()) moff = 2 * uy; else { vo = so; static int n; if (n++ < 4) MLOG("界面下移:投影矩阵这次读不到,退回挪视口(通道 %llu)", g_stat_pass); } } } } + if (moff != g_ui_moff) { g_ui_moff = moff; g_args_dirty[0] = 1; } + { MTLViewport v = S.has_vp ? (MTLViewport){S.vp[0][0], S.vp[0][1] + vo, S.vp[0][2], S.vp[0][3] * vs, S.dr[0], S.dr[1]} : (MTLViewport){0, vo, tw, th * vs, 0, 1}; + if (atomic_load_explicit(&g_origin_ll, memory_order_relaxed)) { v.originY += v.height; v.height = -v.height; } // lower-left origin (see g_origin_ll) + if (!(g_ed.ok & ED_VP) || memcmp(&g_ed.vp, &v, sizeof v)) { enc_r_viewport(en, v); g_ed.vp = v; g_ed.ok |= ED_VP; } } + MTLScissorRect sc = {0, 0, tw, th}; + if (R.has_sc) { + long x0 = R.sc.x < 0 ? 0 : R.sc.x, y0 = R.sc.y <= 0 ? 0 : R.sc.y + so, x1 = (long)R.sc.x + R.sc.w, y1 = vs != 1 ? (long)th : (long)R.sc.y + R.sc.h + so; + if (x1 > (long)tw) x1 = tw; if (y1 > (long)th) y1 = th; + if (x1 <= x0 || y1 <= y0) { PD("Z %llu 绘制跳过:剪裁为空(%d,%d %dx%d,目标 %lux%lu)", g_stat_pass, R.sc.x, R.sc.y, R.sc.w, R.sc.h, (unsigned long)tw, (unsigned long)th); return; } + sc = (MTLScissorRect){(NSUInteger)x0, (NSUInteger)y0, (NSUInteger)(x1 - x0), (NSUInteger)(y1 - y0)}; + } + if (!(g_ed.ok & ED_SC) || memcmp(&g_ed.sc, &sc, sizeof sc)) { enc_r_scissor(en, sc); g_ed.sc = sc; g_ed.ok |= ED_SC; } + if (S.poly.cull == NVN_FACE_FRONT_AND_BACK) { PD("Z %llu 绘制跳过:两面全剔", g_stat_pass); return; } + static int diag_no_cull = -1; + if (diag_no_cull < 0) { const char *e = getenv("DM_NVN_CULL_NONE"); diag_no_cull = e && !strcmp(e, "1"); if (diag_no_cull) MLOG("DM_NVN_CULL_NONE=1: disable triangle culling"); } + enc_r_cull(en, diag_no_cull ? MTLCullModeNone : S.poly.cull == NVN_FACE_FRONT ? MTLCullModeFront : S.poly.cull == NVN_FACE_BACK ? MTLCullModeBack : MTLCullModeNone); + enc_r_winding(en, (S.poly.front == NVN_FRONT_FACE_CCW) != g_queue_origin_ll ? MTLWindingCounterClockwise : MTLWindingClockwise); // NVN's raster flip is captured when the queue is initialized, rather than when the device origin changes. + enc_r_fill(en, S.poly.mode == NVN_POLYGON_MODE_LINE ? MTLTriangleFillModeLines : MTLTriangleFillModeFill); + if (S.poly.offset_en & 4) enc_r_depth_bias(en, S.po.units, S.po.factor, S.po.clamp); else enc_r_depth_bias(en, 0, 0, 0); + enc_r_depth_clip(en, !S.depth_clamp); // NVN depth clamp enabled means Metal depth clipping is disabled. + enc_r_blend_color(en, S.blend_color[0], S.blend_color[1], S.blend_color[2], S.blend_color[3]); + bind_vbufs(en); + if (!min_fs) g_kd_force = kd_draw(pr, dw, di); + g_sample_trace_dw = dw; g_sample_trace_di = di; + if (pr->sh[0]) encode_args(en, pr->sh[0], 0, 1); + if (pr->sh[1] && !min_fs) encode_args(en, pr->sh[1], 1, 0); + if (pr->sh[0] && pr->sh[0]->slice_variant) [en setVertexBytes:&g_selected_slice length:sizeof g_selected_slice atIndex:30]; + if (pr->sh[1] && pr->sh[1]->slice_variant) [en setFragmentBytes:&g_selected_slice length:sizeof g_selected_slice atIndex:30]; + if (pr->sh[1] && pr->sh[1]->zero_image_variant) [en setFragmentBytes:g_zero_image_layout length:sizeof g_zero_image_layout atIndex:29]; + // Parameter binding captures the actual resources under the registry lock; if the version changed between the eligibility check and the binding, split and retry before any GPU draw. + if (g_rt_range_active && (g_pass.reg_gen != g_reg_gen || g_pass.ser_gen != g_ser_gen || g_pass.range_dirty != nvn_dirty_now())) { rp_end(); draw(dw, di); return; } + MTLPrimitiveType mp; + switch (prim) { + case 0: mp = MTLPrimitiveTypePoint; break; case 1: mp = MTLPrimitiveTypeLine; break; case 3: mp = MTLPrimitiveTypeLineStrip; break; + case 4: case 6: case 7: mp = MTLPrimitiveTypeTriangle; break; case 5: mp = MTLPrimitiveTypeTriangleStrip; break; // 6 triangle fan, 7 quads: Metal has neither, expanded into triangle lists + default: RED_ONCE(19, "图元类型 %d 未实现", prim); return; + } + if (g_vis_counting) { // Occlusion query counting: a new slot for this render pass (also switch to a new slot after a report / reset) + if (!g_enc_has_vis) RED_ONCE(34, "遮挡查询计数开始时所在通道没挂可见性缓冲(首次清零发生在通道中途),该通道的采样不计"); + else { + if (g_vis_cur < 0 || g_vis_cur_pass != g_pass.serial) { + if (g_vis_n >= VIS_SLOTS) RED_ONCE(35, "可见性槽用尽"); + else { g_vis_cur = (int)g_vis_n++; g_vis_cur_pass = g_pass.serial; + if (g_nvs == g_vscap) { g_vscap = g_vscap ? g_vscap * 2 : 64; g_vs = realloc(g_vs, g_vscap * sizeof *g_vs); } + g_vs[g_nvs++] = (vis_slot){(void *)CFBridgingRetain(g_vis), (uint32_t)g_vis_cur}; } + } + if (g_vis_cur >= 0 && g_enc_vis != g_vis_cur + 1) { enc_r_visibility(en, MTLVisibilityResultModeCounting, (NSUInteger)g_vis_cur * 8); g_enc_vis = g_vis_cur + 1; } + } + } + g_stat_draw++; + pixel_history_draw(pr->sh[0] ? pr->sh[0]->key : "-", pr->sh[1] ? pr->sh[1]->key : "-"); + { static int tg; if (!tg) { tg = 1; const char *e = getenv("DM_TEST_GHOST"); float v[8]; // Test switch DM_TEST_GHOST=px,py,gx,gy,gw,gh,cx,cy (normalized; press point, grid, center of the drag-follow image): turns on the drag-follow image directly without going through the front end (for Mac host screenshot verification) + if (e && sscanf(e, "%f,%f,%f,%f,%f,%f,%f,%f", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7]) == 8) mtl_ui_ghost(1, v[0], v[1], &v[2], v[6], v[7]); } } + if (dw && atomic_load(&g_ghost_on)) ghost_note(dw); + if (enc_label_on() && !g_el_draws++ && pr->sh[1]) { memcpy(g_el_key, pr->sh[1]->key, 8); g_el_key[8] = 0; } + if (passdump()) { // Characteristics of a render pass's first draw (to judge whether the load can be omitted: full screen, no blending, writes all channels, does not depend on depth); with DM_NVN_PASSDUMP_ALL every draw is recorded + static uint64_t last_pass = ~0ull; static int all = -1; if (all < 0) all = getenv("DM_NVN_PASSDUMP_ALL") != NULL; + if (all || g_kd_force) PD("d %llu bv=%d bi=%d first=%d vs=%s fs=%s vc=%d inst=%d ind=%#llx mdi=%#llx mi=%d blend=%#x b0=%#llx dt=%d dw=%d df=%d cull=%d st=%d sf=%d,%d sop=%d,%d,%d/%d,%d,%d smask=%#x,%#x/%#x,%#x sref=%#x,%#x ib=%#llx it=%d cmask=%#x b1=%#llx b2=%#llx b3=%#llx", g_stat_pass, di ? di->base_vertex : 0, di ? di->base_instance : 0, dw ? dw->first : 0, pr->sh[0] ? pr->sh[0]->key : "-", pr->sh[1] ? pr->sh[1]->key : "-", + dw ? dw->count : di->count, dw ? dw->instances : di->instances, + (unsigned long long)(dw ? dw->ind : di->ind), (unsigned long long)(dw ? dw->mdi_cnt : di->mdi_cnt), dw ? dw->mdi_i : di->mdi_i, + S.color.blend_en, (unsigned long long)*(uint64_t *)&S.blend[0], (int)S.ds.dt, (int)S.ds.dw, (int)S.ds.df, (int)S.poly.cull, + (int)S.ds.st, (int)S.ds.sf_f, (int)S.ds.sf_b, (int)S.ds.fail_f, (int)S.ds.zfail_f, (int)S.ds.zpass_f, + (int)S.ds.fail_b, (int)S.ds.zfail_b, (int)S.ds.zpass_b, S.st_vmask[0], S.st_vmask[1], S.st_wmask[0], S.st_wmask[1], S.st_ref[0], S.st_ref[1], + di ? (unsigned long long)di->ibuf : 0ull, di ? di->itype : 0, S.chmask.mask, (unsigned long long)*(uint64_t *)&S.blend[1], (unsigned long long)*(uint64_t *)&S.blend[2], (unsigned long long)*(uint64_t *)&S.blend[3]); + if (all && pr->sh[0]) { // Content fingerprint of each constant buffer of the vertex stage (to judge whether a draw is completely identical across frames: used by the shadow cache etc.); when unreadable (a write pending execution in this submit) record P + char ub[400] = {0}; size_t o = 0; NvShader *v = pr->sh[0]; + for (int i = 0; i < v->rf.nb && o < sizeof ub - 40; i++) if (v->rf.b[i].kind == NVN_RB_CBUF && v->rf.b[i].reg < 16) { + const p_bind_buf *u = &S.ubo[0][v->rf.b[i].reg]; void *mb; uint64_t mo; const uint8_t *base; uint64_t h = 1469598103934665603ull; + if (!u->addr || !u->size) { o += snprintf(ub + o, sizeof ub - o, " r%d=-", v->rf.b[i].reg); continue; } + if (pend_hits(u->addr, u->size) || !mtl_addr(u->addr, u->size, &mb, &mo) || !(base = [(__bridge id)mb contents])) { o += snprintf(ub + o, sizeof ub - o, " r%d=P/%llu", v->rf.b[i].reg, (unsigned long long)u->size); continue; } + uint64_t h64 = 0; for (uint64_t q = 0; q < u->size; q++) { h = (h ^ base[mo + q]) * 1099511628211ull; if (q == 63) h64 = h; } // h64: the first 64 bytes (world matrix vec0–3 of the object buffer) + { static uint64_t hp = ~0ull; static int hn; if (hp != g_stat_pass) { hp = g_stat_pass; hn = 0; } // For the first 6 draws of each render pass, additionally print buffers of <= 608 bytes in hex (to compare byte by byte which fields changed across frames) + if (hn < 12 && u->size <= 608 && v->rf.b[i].reg == 3) { hn++; char hx[1300]; for (uint64_t q = 0; q < u->size; q++) snprintf(hx + 2 * q, 3, "%02x", base[mo + q]); PD("x %llu r%d %s", g_stat_pass, v->rf.b[i].reg, hx); } } + o += snprintf(ub + o, sizeof ub - o, " r%d=%016llx/%llu/%016llx", v->rf.b[i].reg, (unsigned long long)h, (unsigned long long)u->size, (unsigned long long)h64); } + PD("u %llu%s", g_stat_pass, ub); } + if (all) { char vb[1600]; size_t o = 0; o += snprintf(vb + o, sizeof vb - o, "nattr=%d", S.nattr); + for (int i = 0; i < S.nattr && i < 16 && o < sizeof vb - 100; i++) { int st = S.attr[i].stream; if (!S.attr[i].fmt) continue; // only attributes with a format + o += snprintf(vb + o, sizeof vb - o, " a%d{fmt=%#x off=%d s=%d stride=%u div=%u vb=%#llx/%llu}", i, S.attr[i].fmt, S.attr[i].offset, st, + st < 16 ? S.stream[st].stride : 0, st < 16 ? S.stream[st].divisor : 0, st < 16 ? (unsigned long long)S.vbuf[st].address : 0ull, st < 16 ? (unsigned long long)S.vbuf[st].size : 0ull); } + PD("v %llu %s", g_stat_pass, vb); + // DM_NVN_CBDUMP + DM_NVN_CBDUMP_KEY=vertex shader key: also save that draw's vertex streams and indices (F_p_vb_
.bin / _ib_
.bin) + const char *cbd = getenv("DM_NVN_CBDUMP"), *cbk = getenv("DM_NVN_CBDUMP_KEY"); + if (cbd && cbk && pr->sh[0] && !strcmp(pr->sh[0]->key, cbk)) { + char fp[1024]; + for (int st = 0; st < 16; st++) if (S.vbuf[st].address && S.vbuf[st].size) { + snprintf(fp, sizeof fp, "%s/F%llu_p%llu_vb%d_%llx.bin", cbd, (unsigned long long)atomic_load(&g_cur_frame), g_stat_pass, st, (unsigned long long)S.vbuf[st].address); + FILE *f = fopen(fp, "wb"); if (f) { fwrite((const void *)S.vbuf[st].address, 1, S.vbuf[st].size, f); fclose(f); } } + if (di && di->ibuf) { + snprintf(fp, sizeof fp, "%s/F%llu_p%llu_ib%d_%llx.bin", cbd, (unsigned long long)atomic_load(&g_cur_frame), g_stat_pass, di->itype, (unsigned long long)di->ibuf); + FILE *f = fopen(fp, "wb"); if (f) { fwrite((const void *)di->ibuf, 1, (size_t)di->count << (di->itype == 2 ? 2 : di->itype), f); fclose(f); } } + } } + if (last_pass != g_stat_pass) { + last_pass = g_stat_pass; + PD("D %llu prim=%d vc=%d inst=%d blend=%#x wm=%#x dt=%d dw=%d df=%d st=%d sc=%lu,%lu,%lu,%lu tgt=%lux%lu vp=%.0f,%.0f,%.0f,%.0f", g_stat_pass, prim, + dw ? dw->count : di->count, dw ? dw->instances : di->instances, S.color.blend_en, S.chmask.mask, (int)S.ds.dt, (int)S.ds.dw, (int)S.ds.df, (int)S.ds.st, + (unsigned long)sc.x, (unsigned long)sc.y, (unsigned long)sc.width, (unsigned long)sc.height, (unsigned long)tw, (unsigned long)th, + S.has_vp ? S.vp[0][0] : 0, S.has_vp ? S.vp[0][1] : 0, S.has_vp ? S.vp[0][2] : (float)tw, S.has_vp ? S.vp[0][3] : (float)th); + } + } + g_kd_force = 0; + if (rt_range_on() && !rt_range_pr_ro(pr)) g_pass.range_external=1; + rt_range_note_depth(); +#ifdef NVN_RT_RANGE_TEST + rr_snap(dw,di); +#endif + udc_note_draw(pr); + if (enc_label_on()) { + NSString *mark = [NSString stringWithFormat:@"d%llu v=%.16s f=%.16s n=%d", + (unsigned long long)g_stat_draw, + pr->sh[0] ? pr->sh[0]->key : "-", pr->sh[1] ? pr->sh[1]->key : "-", + dw ? dw->count : di->count]; + [en insertDebugSignpost:mark]; + } + // A draw with 0 primitives (the game does send count=0) draws no pixels, so skip encoding; Metal requires the count to be non-zero, otherwise the validation layer aborts on the spot (measured on the camp / wilderness / combat pages) + if ((dw && dw->ind) || (di && di->ind)) { + if (sid.fence) [en waitForFence:sid.fence beforeStages:MTLRenderStageVertex]; + if (sid.indexed) enc_r_draw_indexed_indirect(en, mp, sid.index_type, sid.index, sid.index_off, sid.index_len, sid.args, sid.args_off); + else enc_r_draw_indirect(en, mp, sid.args, sid.args_off); + } else if (dw && prim == 7) { // Quads: shared indices 0,1,2,0,2,3 + quad_ib_make(); + if (dw->count >= 4) enc_r_draw_indexed(en, mp, (dw->count / 4) * 6, MTLIndexTypeUInt32, g_quad_gpu, g_quad_ib.length, dw->instances > 0 ? dw->instances : 1, dw->first, dw->base_instance); + } else if (dw && prim == 6) { // triangle fans (13 Sentinels): shared indices (0, i, i+1), first as the base vertex + fan_ib_make(); + if (dw->count >= 3) enc_r_draw_indexed(en, mp, (dw->count - 2) * 3, MTLIndexTypeUInt32, g_fan_gpu, g_fan_ib.length, dw->instances > 0 ? dw->instances : 1, dw->first, dw->base_instance); + } else if (dw) { + if (dw->count && (dw->instances > 1 || dw->base_instance)) enc_r_draw_instanced(en, mp, dw->first, dw->count, dw->instances > 0 ? dw->instances : 1, dw->base_instance); + else if (dw->count) enc_r_draw(en, mp, dw->first, dw->count); + } else { + if (prim == 6 || prim == 7) { + // indexed triangle fans / quads (13 Sentinels): at replay the indices are read from guest memory and expanded into a 32-bit triangle list in this frame's arena (as for 8-bit indices) + int isz = di->itype == 0 ? 1 : di->itype == 1 ? 2 : 4; + uint32_t nt = prim == 7 ? (uint32_t)(di->count / 4) * 2 : di->count >= 3 ? (uint32_t)di->count - 2 : 0; + if (!nt) return; + void *mb; uint64_t mo; const uint8_t *src; + if (!mtl_addr(di->ibuf, (uint64_t)di->count * isz, &mb, &mo) || !(src = [(__bridge id)mb contents])) { RED_ONCE(22, "索引缓冲不在内存池里"); return; } + if (pend_hits(di->ibuf, (uint64_t)di->count * isz)) RED_ONCE(73, "三角扇 / 四边形索引缓冲本提交里有 GPU 侧写入:按当前内容展开"); + src += mo; + #define IX(i) (isz == 1 ? (uint32_t)src[i] : isz == 2 ? (uint32_t)((const uint16_t *)src)[i] : ((const uint32_t *)src)[i]) + id ab; size_t off = arena_alloc(((size_t)nt * 12 + 15) & ~(size_t)15, &ab); + uint32_t *d = (uint32_t *)((uint8_t *)ab.contents + off); + for (uint32_t t = 0; t < nt; t++) { + if (prim == 6) { d[3 * t] = IX(0); d[3 * t + 1] = IX(t + 1); d[3 * t + 2] = IX(t + 2); } + else { uint32_t q = (t >> 1) * 4, h = t & 1; d[3 * t] = IX(q); d[3 * t + 1] = IX(q + 1 + h); d[3 * t + 2] = IX(q + 2 + h); } + } + #undef IX + use_rec("buf", di->ibuf, (uint64_t)di->count * isz, 0, NULL); + enc_r_draw_indexed(en, mp, nt * 3, MTLIndexTypeUInt32, g_cur_arena->gpu + off, (NSUInteger)nt * 12, di->instances ? di->instances : 1, di->base_vertex, di->base_instance); + if (g_rt_range_pending) { PD("j rt_level_range pass=%llu vc=%u", g_stat_pass, (unsigned)di->count); g_rt_range_pending = 0; } + return; + } + if (di->itype == 0) { + // 8-bit indices: Metal only has 16 / 32-bit. At replay they are read from guest memory and widened to 16-bit in this frame's arena (SMBW's full-screen passes and the final copy into the window all use 8-bit indices; without this the whole screen is black) + if (!di->count) return; + void *mb; uint64_t mo; const uint8_t *src; + if (!mtl_addr(di->ibuf, di->count, &mb, &mo) || !(src = [(__bridge id)mb contents])) { RED_ONCE(22, "索引缓冲不在内存池里"); return; } + if (pend_hits(di->ibuf, di->count)) RED_ONCE(48, "8 位索引缓冲本提交里有 GPU 侧写入:按当前内容展开"); + id ab; size_t off = arena_alloc(((size_t)di->count * 2 + 15) & ~(size_t)15, &ab); + uint16_t *d = (uint16_t *)((uint8_t *)ab.contents + off); src += mo; + for (int32_t i = 0; i < di->count; i++) d[i] = src[i]; + use_rec("buf", di->ibuf, (uint64_t)di->count, 0, NULL); + enc_r_draw_indexed(en, mp, di->count, MTLIndexTypeUInt16, g_cur_arena->gpu + off, (NSUInteger)di->count * 2, di->instances ? di->instances : 1, di->base_vertex, di->base_instance); + if (g_rt_range_pending) { PD("j rt_level_range pass=%llu vc=%u", g_stat_pass, (unsigned)di->count); g_rt_range_pending = 0; } + return; + } + void *mb; uint64_t mo; + uint64_t gpu; + if (!mtl_gpu_addr(di->ibuf, 1, &mb, &mo, &gpu)) { RED_ONCE(22, "索引缓冲不在内存池里"); return; } + use_rec("buf", di->ibuf, (uint64_t)di->count * (di->itype == 1 ? 2 : 4), 0, NULL); + if (di->count) enc_r_draw_indexed(en, mp, di->count, di->itype == 1 ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32, gpu, ((__bridge id)mb).length - mo, + di->instances ? di->instances : 1, di->base_vertex, di->base_instance); + } + if (g_rt_range_pending) { PD("j rt_level_range pass=%llu vc=%u", g_stat_pass, (unsigned)(dw ? dw->count : di->count)); g_rt_range_pending=0; } +} +// Function identity is part of the cache key: a sampler-specialized companion +// must never replace a pipeline for the shader's ordinary sampler bindings. +typedef struct { const void *shader, *function; } compute_key; +static nvn_hmap g_cps = {.klen = sizeof(compute_key)}; +static void warm_claim(void) { + if (!atomic_exchange_explicit(&g_warm_ready, 0, memory_order_acq_rel)) return; + os_unfair_lock_lock(&g_warm_lock); + NSArray *items = [g_warm_items copy]; + [g_warm_items removeAllObjects]; + os_unfair_lock_unlock(&g_warm_lock); + for (WarmItem *item in items) { + if (item->kind == 0) { + if (!g_shcache) g_shcache = [NSMutableDictionary dictionary]; + if (!g_shcache[item->key]) { g_shcache[item->key] = item->shader; g_warm_claim_shaders++; } + } else if (item->kind == 1) { + if (!g_shcache) g_shcache = [NSMutableDictionary dictionary]; + NvShader *vs = g_shcache[item->vs_key], *fs = item->fs_key.length ? g_shcache[item->fs_key] : nil; + if (!vs) { vs = item->vs; g_shcache[item->vs_key] = vs; } + if (item->fs_key.length && !fs) { fs = item->fs; g_shcache[item->fs_key] = fs; } + item->pso.vs = (__bridge void *)vs; item->pso.fs = (__bridge void *)fs; + if (!hm_get_id(&g_pso, &item->pso)) { hm_put_id(&g_pso, &item->pso, item->object); g_warm_claim_rps++; } + } else { + NvShader *cs = g_shcache[item->key]; + if (!cs) { cs = item->shader; if (!g_shcache) g_shcache = [NSMutableDictionary dictionary]; g_shcache[item->key] = cs; } + compute_key key = {(__bridge const void *)cs, (__bridge const void *)cs->fn}; + if (!hm_get_id(&g_cps, &key)) { hm_put_id(&g_cps, &key, item->object); g_warm_claim_cps++; } + } + } +} +// ---------- 3D picture brightening ---------- +// Tone mapping (fragment a1b1aab66933fa8f) = PQ-encode the composited HDR x0.008, then look up a 32^3 color lookup table (recomputed per region every frame by compute shader 552f45c348a61b86, read only by tone mapping). +// Multiplying HDR by exposure k first is equivalent to replacing the table with L'(u) = L(PQ(k * PQ^-1(u))): after the dispatch in which the game finishes computing the table, make a copy and rewrite it in place accordingly (one small 32^3 dispatch); the table is recomputed every frame, so it does not get brighter with each multiplication. +// Only the 3D world is touched: UI, text and the health orbs are composited after tone mapping; highlights are compressed by PQ and do not blur into a sheet of white as a final whole-screen multiply would. The normal / readable tile merge / freeze paths all just read this table, so it takes effect in one place. +// k is given every frame by the host's dm_bright_k() (dm/host/dm_bright.c: 1 out of game, 4 for maze-type areas, 4 where the original game's picture does not follow day and night, the rest derived from the original game's ambient brightness, with smooth transitions); no rewrite when k ~= 1. +__attribute__((weak)) float dm_bright_k(void) { return 1.0f; } // The host's dm_bright.c provides the strong definition; no brightening when graphics unit tests do not link the host +static const char k_lut_msl[] = +"#include \nusing namespace metal;\n" +"constant float m1 = 0.1593017578125, m2 = 78.84375, c1 = 0.8359375, c2 = 18.8515625, c3 = 18.6875;\n" // Bit-identical to the PQ constants in the tone mapping shader +"static float3 pq(float3 v) { float3 p = pow(abs(v), m1); return pow((c1 + c2 * p) / (1.0 + c3 * p), m2); }\n" +"static float3 pq_inv(float3 u) { float3 q = pow(u, 1.0 / m2); return pow(max(q - c1, 0.0) / max(c2 - c3 * q, 1e-6), 1.0 / m1); }\n" +"kernel void lutk(texture3d src [[texture(0)]], texture3d dst [[texture(1)]], constant float &k [[buffer(0)]], uint3 g [[thread_position_in_grid]]) {\n" +" uint3 n = uint3(dst.get_width(), dst.get_height(), dst.get_depth()); if (any(g >= n)) return;\n" +" constexpr sampler ls(filter::linear, address::clamp_to_edge, coord::normalized);\n" +" float3 u = (float3(g) + 0.5) / float3(n); float3 u2 = clamp(pq(k * pq_inv(u)), 0.0, 1.0);\n" +" float4 b = src.sample(ls, u2), o = src.read(g);\n" // b: output after brightening by exposure k; o: this cell in the original game +" float s = smoothstep(0.65, 1.0, dot(b.rgb, float3(0.2126, 0.7152, 0.0722)));\n" // Highlight protection: after brightening, the closer to white, the more it is pulled back to the original value (snow and skylight do not blur into a sheet of white); in dark areas s = 0 and brightening proceeds as usual +" dst.write(float4(max(mix(b.rgb, o.rgb, s), o.rgb), b.a), g); }\n"; +static void bright_lut(NvShader *cs) { + static int failed; static id ps; static id tmp; static id kb[3]; static int ki; + if (failed || strncmp(cs->key, "552f45c348a61b86", 16)) return; + float k = dm_bright_k(); if (!(k > 1.005f)) return; // k ~= 1 (out of game / noon): the table stays as in the original game + if (k > 4.0f) k = 4.0f; + int tid = 0; for (int i = 0; i < cs->rf.nb && !tid; i++) if (cs->rf.b[i].kind == NVN_RB_IMG && cs->rf.b[i].reg < 32) tid = (int)(S.img[5][cs->rf.b[i].reg] & 0xfffff); + pthread_mutex_lock(&g_rmu); id lut = tid ? tex_by_id(tid) : nil; pthread_mutex_unlock(&g_rmu); + if (!lut || lut.textureType != MTLTextureType3D || !(lut.usage & MTLTextureUsageShaderWrite)) { static int n; if (!n++) MLOG("【红】3D 画面加亮:调色表认不出(贴图号 %d),不加亮", tid); return; } + if (!ps) { NSError *e = nil; id lib = [g_dev newLibraryWithSource:@(k_lut_msl) options:nil error:&e]; id f = lib ? [lib newFunctionWithName:@"lutk"] : nil; + ps = f ? nvn_cached_compute(g_dev, f, &e) : nil; + for (int i = 0; i < 3; i++) { kb[i] = [g_dev newBufferWithLength:16 options:MTLResourceStorageModeShared]; if (kb[i]) res_add(kb[i]); } + if (!ps || !kb[0] || !kb[1] || !kb[2]) { failed = 1; MLOG("【红】3D 画面加亮:计算管线建不出 %s(退回原版亮度)", e.localizedDescription.UTF8String); return; } + MLOG("3D 画面加亮:建好(调色表 %lux%lux%lu,曝光每帧由 dm_bright_k 给)", (unsigned long)lut.width, (unsigned long)lut.height, (unsigned long)lut.depth); } + if (!tmp || tmp.width != lut.width || tmp.height != lut.height || tmp.depth != lut.depth || tmp.pixelFormat != lut.pixelFormat) { + MTLTextureDescriptor *d = [MTLTextureDescriptor new]; d.textureType = MTLTextureType3D; d.pixelFormat = lut.pixelFormat; d.width = lut.width; d.height = lut.height; d.depth = lut.depth; + d.usage = MTLTextureUsageShaderRead; d.storageMode = MTLStorageModePrivate; if (tmp) defer_obj_release(tmp); tmp = [g_dev newTextureWithDescriptor:d]; if (!tmp) { failed = 1; return; } res_add(tmp); } + id b0 = kb[ki]; ki = (ki + 1) % 3; *(float *)b0.contents = k; // Three copies in rotation: in-flight frames are still reading the previous copy + id b = blit(); enc_b_copy_texture(b, lut, 0, 0, MTLOriginMake(0, 0, 0), MTLSizeMake(lut.width, lut.height, lut.depth), tmp, 0, 0, MTLOriginMake(0, 0, 0)); enc_b_end(g_blit); g_blit = nil; + id ce = (id)enc_c_begin(NULL); [ce setTexture:tmp atIndex:0]; [ce setTexture:lut atIndex:1]; [ce setBuffer:b0 offset:0 atIndex:0]; + enc_c_pso(ce, ps); enc_c_dispatch(ce, MTLSizeMake((lut.width + 3) / 4, (lut.height + 3) / 4, (lut.depth + 3) / 4), MTLSizeMake(4, 4, 4)); enc_c_end(ce); + args_dirty_all(); +} +static id compute_pipeline(NvShader *cs, id fn) { + compute_key k = {(__bridge const void *)cs, (__bridge const void *)fn}; + id ps = hm_get_id(&g_cps, &k); + if (!ps) { + NSError *e = nil; uint64_t bt = bp_now(); ps = nvn_cached_compute(g_dev, fn, &e); + g_bp_pso_n++; g_bp_pso_ns += bp_now() - bt; + if (!ps) { MLOG("[RED] Compute pipeline %s failed: %s", cs->key, e.localizedDescription.UTF8String); return nil; } + if (fn == cs->fn) { pso_record("C\t%s\n", cs->key); + if (cs->key[0]) pso_user_record("C\t%s\n", cs->key); } + hm_put_id(&g_cps, &k, ps); + } + return ps; +} +static void dispatch(const p_dispatch *dp) { + zskip_clear(); + if (!S.prog) { RED_ONCE(27, "compute dispatch has no bound program"); return; } + NvShader *cs = prog_get(S.prog)->sh[5]; + if (!cs) { RED_ONCE(28, "dispatched program has no mapped compute shader"); return; } + int min_cs = min_reduction_candidate(cs); + id ps = min_cs ? nil : compute_pipeline(cs, cs->fn); + if (!min_cs && !ps) return; + if (cs->builtin) { // Built-in compute kernel: n = uniform buffer 0, src = storage buffer 0, dst = storage buffer 1 + const p_bind_buf *u = &S.ubo[5][0], *a0 = &S.ssbo[5][0], *a1 = &S.ssbo[5][1]; + void *ub, *sb, *db; uint64_t uo, so, dof; + if (!mtl_addr(u->addr, 4, &ub, &uo) || !mtl_addr(a0->addr, 8, &sb, &so) || !mtl_addr(a1->addr, 8, &db, &dof)) { RED_ONCE(30, "内嵌计算核 %s 的缓冲没绑全", cs->builtin); return; } + if (!cs->logged++) { + module_t *m = module_of((void *)dp->caller); + MLOG("内嵌计算核 %s 首次派发:调用点 %s+%#llx,线程组 %dx%dx%d,元素数(CPU 侧当前值)%u", cs->builtin, m ? m->name : "?", + m ? (unsigned long long)(dp->caller - (uint64_t)m->base) : (unsigned long long)dp->caller, dp->x, dp->y, dp->z, *(uint32_t *)u->addr); + } + rp_end(); if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + id ce = pg_compute(); + enc_c_pso(ce, ps); + enc_c_args(ce, legacy_buffer_gpu((__bridge id)ub) + uo, 0); enc_c_args(ce, legacy_buffer_gpu((__bridge id)sb) + so, 1); enc_c_args(ce, legacy_buffer_gpu((__bridge id)db) + dof, 2); // Built-in compute kernel: cold path, take the addresses directly + enc_c_dispatch(ce, MTLSizeMake(dp->x, dp->y, dp->z), MTLSizeMake(32, 1, 1)); + enc_c_end(ce); + pend_add(a1->addr, a1->size ? a1->size : 1); + return; + } + const uint16_t *tg = cs->rf.tg; + if (!tg[0]) { RED_ONCE(29, "计算着色器缺线程组大小反射"); return; } + rp_end(); if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + id ce = pg_compute(); + if (enc_label_on()) ce.label = [NSString stringWithFormat:@"compute %.16s %dx%dx%d", cs->key, dp->x, dp->y, dp->z]; + if (!min_cs) enc_c_pso(ce, ps); + g_dispatch_dim[0] = (uint32_t)dp->x; g_dispatch_dim[1] = (uint32_t)dp->y; g_dispatch_dim[2] = (uint32_t)dp->z; // read back by the driver segment c0 (encode_args_) + encode_args(ce, cs, 5, 0); + if (min_cs) { + id fn = min_reduction_function(cs) ?: cs->fn; + ps = compute_pipeline(cs, fn); + if (!ps && fn != cs->fn) { cs->min_reduction_pso_failed = 1; + MLOG("Min-reduction %s pipeline rejected; disabling its companion and retrying the ordinary pipeline", cs->key); + ps = compute_pipeline(cs, cs->fn); } + if (!ps) { enc_c_end(ce); return; } + enc_c_pso(ce, ps); + } + enc_c_dispatch(ce, MTLSizeMake(dp->x, dp->y, dp->z), MTLSizeMake(tg[0], tg[1], tg[2])); + enc_c_end(ce); + bright_lut(cs); // 3D picture brightening: rewrite after the game finishes computing the color lookup table +} +// GPU: texture -> buffer (row stride taken from SetCopyRowStride, layer stride from SetCopyImageStride, 0 = tightly packed) +static void copy_t2b(const p_copy_b2t *c) { + za_diag_op("copy-texture-to-buffer", c->tex); + if (c->tex) xv_check(c->tex->serial, c->has_view && c->view.set_levels ? c->view.base_level : 0, c->has_view && c->view.set_layers ? c->view.min_layer : 0, "贴图读缓冲"); + id t = tex_mtl(c->tex); + if (!t) return; + int bw, bh, bpb = nvn_fmt_bpb(c->tex->d.format, &bw, &bh); + unsigned level = c->has_view && c->view.set_levels ? c->view.base_level : 0, layer0 = c->has_view && c->view.set_layers ? c->view.min_layer : 0; + size_t row = S.copy_stride ? (size_t)S.copy_stride : (size_t)((c->r.width + bw - 1) / bw) * bpb; + size_t rows = (c->r.height + bh - 1) / bh, img = S.copy_img_stride ? (size_t)S.copy_img_stride : row * rows; // per layer: SetCopyImageStride, else tightly packed + int is3d = c->tex->d.target == 2, nz = c->r.depth ? c->r.depth : 1; + void *mb; uint64_t mo; + if (!mtl_addr(c->src, img * nz, &mb, &mo)) { RED_ONCE(31, "贴图→缓冲拷贝的目标不在内存池里"); return; } + if (nvn_fmt_bc((uint32_t)c->tex->d.format) && !is_native_bc(t.pixelFormat)) { + RED_ONCE(52, "a BC texture stored decompressed or as ASTC cannot be copied back as original BC blocks; clearing the destination"); + id zero_blit = blit(); + [zero_blit fillBuffer:(__bridge id)mb range:NSMakeRange(mo, img * nz) value:0]; + return; + } + id b = blit(); + if (is3d) + enc_b_copy_texture_buffer(b, t, 0, level, MTLOriginMake(c->r.xoffset, c->r.yoffset, c->r.zoffset), MTLSizeMake(c->r.width, c->r.height, nz), (__bridge id)mb, mo, row, img); + else + for (int z = 0; z < nz; z++) + enc_b_copy_texture_buffer(b, t, layer0 + c->r.zoffset + z, level, MTLOriginMake(c->r.xoffset, c->r.yoffset, 0), MTLSizeMake(c->r.width, c->r.height, 1), (__bridge id)mb, mo + z * img, row, img); +} +// CPU texture read (ReadTexels): for packaged textures the pool memory is the ground truth, de-layout and read directly; the rest are read back from the MTLTexture by blit (the caller has already waited for the GPU per NVN rules) +void mtl_read_texels(const tex_t *t, unsigned level, unsigned layer0, const NVNcopyRegion *r, uint8_t *dst, size_t row, size_t img) { + xv_check(t->serial, level, layer0, "CPU读回"); + PD("U cpu_read ser=%llu lv%u sl%u xyz=%d,%d,%d size=%d,%d,%d dst=%p row=%lu img=%lu", t->serial, level, layer0, r->xoffset, r->yoffset, r->zoffset, r->width, r->height, r->depth, dst, (unsigned long)row, (unsigned long)img); + int bw, bh, bpb = nvn_fmt_bpb(t->d.format, &bw, &bh); + size_t wb = (size_t)((r->width + bw - 1) / bw) * bpb, hb = (r->height + bh - 1) / bh, x0 = (size_t)(r->xoffset / bw) * bpb, y0 = r->yoffset / bh; + int nz = r->depth ? r->depth : 1, is3d = t->d.target == 2; + if (t->has_layout && t->mem) { + uint32_t lw = MAX(t->d.width >> level, 1u), lh = MAX(t->d.height >> level, 1u), lz = is3d ? MAX(t->d.depth >> level, 1u) : 1; + size_t lp = (size_t)((lw + bw - 1) / bw) * bpb, lr = (lh + bh - 1) / bh; + uint8_t *lin = malloc(lp * lr * lz); + for (int z = 0; z < nz; z++) { + unsigned layer = is3d ? 0 : layer0 + r->zoffset + z; + if ((is3d && z == 0) || !is3d) if (nvn_deswizzle(&t->d, level, layer, t->mem, lin, lp)) { RED_ONCE(32, "ReadTexels 解排布失败"); free(lin); return; } + const uint8_t *src = lin + (is3d ? (size_t)(r->zoffset + z) * lp * lr : 0); + for (size_t y = 0; y < hb; y++) memcpy(dst + z * img + y * row, src + (y0 + y) * lp + x0, wb); + } + free(lin); + return; + } + // This runs on a game thread outside submission replay. Bound the Metal + // command objects' autorelease lifetime because that thread may live forever. + @autoreleasepool { + id mt = tex_mtl(t); + if (!mt) { memset(dst, 0, img * nz); return; } + if (nvn_fmt_bc((uint32_t)t->d.format) && !is_native_bc(mt.pixelFormat)) { RED_ONCE(51, "a GPU-written BC texture stored decompressed or as ASTC cannot be read back as original BC blocks; returning zeroes"); memset(dst, 0, img * nz); return; } + mtl_finish_gpu(); + id buf = [g_dev newBufferWithLength:wb * hb * nz options:MTLResourceStorageModeShared]; + id cb = q_cb(); id b = enc_b_begin(cb); + for (int z = 0; z < nz; z++) + [b copyFromTexture:mt sourceSlice:is3d ? 0 : layer0 + r->zoffset + z sourceLevel:level sourceOrigin:MTLOriginMake(r->xoffset, r->yoffset, is3d ? r->zoffset + z : 0) + sourceSize:MTLSizeMake(r->width, r->height, 1) toBuffer:buf destinationOffset:z * wb * hb destinationBytesPerRow:wb destinationBytesPerImage:wb * hb]; + enc_b_end(b); [cb commit]; [cb waitUntilCompleted]; + for (int z = 0; z < nz; z++) for (size_t y = 0; y < hb; y++) memcpy(dst + z * img + y * row, (uint8_t *)buf.contents + z * wb * hb + y * wb, wb); + } +} +static void copy_b2t(const p_copy_b2t *c) { + za_diag_op("copy-buffer-to-texture", c->tex); + if (c->tex) zskip_del(c->tex->serial); + id t = tex_mtl(c->tex); + PD("C ser=%llu", c->tex->serial); + if (!t) return; + int bw, bh, bpb = nvn_fmt_bpb(c->tex->d.format, &bw, &bh); + unsigned level = c->has_view && c->view.set_levels ? c->view.base_level : 0, layer0 = c->has_view && c->view.set_layers ? c->view.min_layer : 0; + size_t row = S.copy_stride ? (size_t)S.copy_stride : (size_t)((c->r.width + bw - 1) / bw) * bpb; + size_t rows = (c->r.height + bh - 1) / bh, img = S.copy_img_stride ? (size_t)S.copy_img_stride : row * rows; // per layer: SetCopyImageStride, else tightly packed + int is3d = c->tex->d.target == 2, nz = c->r.depth ? c->r.depth : 1; + void *mb; uint64_t mo; + if (!mtl_addr(c->src, img * nz, &mb, &mo)) { RED_ONCE(23, "缓冲→贴图拷贝的源不在内存池里"); return; } + uint32_t upf; int ubpp; + if (is_astc4(t.pixelFormat) && nvn_fmt_bc((uint32_t)c->tex->d.format)) { + const uint8_t *src = [(__bridge id)mb contents]; + if (!src) { RED_ONCE(46, "BC-to-ASTC copy source buffer is not CPU-readable"); return; } + size_t ap = (size_t)((c->r.width + 3) / 4) * 16, aimg = ap * ((c->r.height + 3) / 4); + id tb = [g_dev newBufferWithLength:aimg * nz ?: 16 options:MTLResourceStorageModeShared]; + if (!tb) return; + for (int z = 0; z < nz; z++) nvn_bc2astc((uint32_t)c->tex->d.format, src + mo + z * img, row, c->r.width, c->r.height, (uint8_t *)tb.contents + z * aimg, ap); + res_add(tb); + id b = blit(); + for (int z = 0; z < nz; z++) enc_b_copy_buffer_texture(b, tb, z * aimg, ap, aimg, MTLSizeMake(c->r.width, c->r.height, 1), t, layer0 + c->r.zoffset + z, level, MTLOriginMake(c->r.xoffset, c->r.yoffset, 0)); + defer_obj_release(tb); + return; + } + if (bc_unpack(c->tex->d.format, &upf, &ubpp)) { // No BC: the compressed blocks in the source are decompressed on the CPU into a temporary buffer, then copied pixel-wise into the decompressed-format texture + const uint8_t *src = [(__bridge id)mb contents]; + if (!src) { RED_ONCE(46, "无 BC 解压:缓冲→贴图拷贝的源缓冲 CPU 读不到"); return; } + size_t up = (size_t)c->r.width * ubpp, uimg = up * c->r.height; + id tb = [g_dev newBufferWithLength:uimg * nz ?: 16 options:MTLResourceStorageModeShared]; + for (int z = 0; z < nz; z++) nvn_bc_decode(c->tex->d.format, src + mo + z * img, row, c->r.width, c->r.height, (uint8_t *)tb.contents + z * uimg, up); + res_add(tb); + id b = blit(); + if (is3d) enc_b_copy_buffer_texture(b, tb, 0, up, uimg, MTLSizeMake(c->r.width, c->r.height, nz), t, 0, level, MTLOriginMake(c->r.xoffset, c->r.yoffset, c->r.zoffset)); + else for (int z = 0; z < nz; z++) enc_b_copy_buffer_texture(b, tb, z * uimg, up, uimg, MTLSizeMake(c->r.width, c->r.height, 1), t, layer0 + c->r.zoffset + z, level, MTLOriginMake(c->r.xoffset, c->r.yoffset, 0)); + defer_obj_release(tb); // Release after this submit has finished running (and remove from residency) + return; + } + id b = blit(); + if (is3d) + enc_b_copy_buffer_texture(b, (__bridge id)mb, mo, row, img, MTLSizeMake(c->r.width, c->r.height, nz), t, 0, level, MTLOriginMake(c->r.xoffset, c->r.yoffset, c->r.zoffset)); + else + for (int z = 0; z < nz; z++) + enc_b_copy_buffer_texture(b, (__bridge id)mb, mo + z * img, row, img, MTLSizeMake(c->r.width, c->r.height, 1), t, layer0 + c->r.zoffset + z, level, MTLOriginMake(c->r.xoffset, c->r.yoffset, 0)); +} + +// No BC + Legacy Device Pack: the game copies textures from files (already ASTC 4x4) whole into a BC3 texture array it creates itself (created as RGBA8 when there is no BC). On Switch the two formats have the same block size and +// the copy moves raw blocks; here it is changed to a compute kernel that decodes pixel by pixel and writes them in, with sRGB sources first read out as linear values and then encoded back to sRGB values, so the result = the values read in the destination format after moving the blocks as-is +static const char k_a2u_msl[] = +"#include \nusing namespace metal;\n" +"static float3 enc(float3 c) { return select(1.055 * pow(c, 1.0 / 2.4) - 0.055, c * 12.92, c <= 0.0031308); }\n" +"kernel void a2u(texture2d src [[texture(0)]], texture2d dst [[texture(1)]], constant uint4 *p [[buffer(0)]], uint2 g [[thread_position_in_grid]]) {\n" +" if (g.x >= p[1].x || g.y >= p[1].y) return; float4 v = src.read(g + p[0].xy); if (p[1].z) v.rgb = enc(v.rgb); dst.write(v, g + p[0].zw); }\n"; +static int copy_t2t_astc(id s, id d, unsigned sl, unsigned ss, unsigned dl, unsigned ds, int sx, int sy, int dx, int dy, int w, int h) { + static int failed; static id ps; + if (failed) return 0; + if (!ps) { NSError *e = nil; id lib = [g_dev newLibraryWithSource:@(k_a2u_msl) options:nil error:&e]; id f = lib ? [lib newFunctionWithName:@"a2u"] : nil; + ps = f ? nvn_cached_compute(g_dev, f, &e) : nil; + if (!ps) { failed = 1; MLOG("【红】无 BC:ASTC → 解压格式拷贝的计算管线建不出 %s", e.localizedDescription.UTF8String); return 0; } } + MTLTextureType tt = MTLTextureType2D; + id sv = [s newTextureViewWithPixelFormat:s.pixelFormat textureType:tt levels:NSMakeRange(sl, 1) slices:NSMakeRange(ss, 1)]; + id dv = [d newTextureViewWithPixelFormat:MTLPixelFormatRGBA8Unorm textureType:tt levels:NSMakeRange(dl, 1) slices:NSMakeRange(ds, 1)]; + id pb = [g_dev newBufferWithLength:32 options:MTLResourceStorageModeShared]; + if (!sv || !dv || !pb) { RED_ONCE(47, "无 BC:ASTC → 解压格式拷贝建不出视图 / 参数块"); return 0; } + uint32_t *p = pb.contents; p[0] = sx; p[1] = sy; p[2] = dx; p[3] = dy; p[4] = w; p[5] = h; p[6] = s.pixelFormat == MTLPixelFormatASTC_4x4_sRGB; p[7] = 0; + res_add(pb); + rp_end(); if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + id ce = (id)enc_c_begin(NULL); + [ce setTexture:sv atIndex:0]; [ce setTexture:dv atIndex:1]; [ce setBuffer:pb offset:0 atIndex:0]; + enc_c_pso(ce, ps); enc_c_dispatch(ce, MTLSizeMake((w + 7) / 8, (h + 7) / 8, 1), MTLSizeMake(8, 8, 1)); enc_c_end(ce); + args_dirty_all(); + defer_obj_unref(sv); defer_obj_unref(dv); defer_obj_release(pb); // Release after this submit has finished running (views are not made resident separately; the parameter block is removed from residency) + return 1; +} +// Texture copies a blit cannot do -- a format change, a scaled region or a mirrored one (MIRROR_X / MIRROR_Y) (Link's Awakening converts its 1408x792 RG11B10F scene to RGBA16F every frame; Super Mario 3D World scales 1920x1080 to 1280x720 +// when leaving the title): one render pass per layer that draws the source region into the destination region, nearest or (LINEAR_FILTER) bilinear. Float, unorm and sRGB formats only, 2D / 2D array, +// destinations that can be render targets; anything else returns 0 and keeps the red line. Depth (Depth32Float / Depth32Float_Stencil8 into the same format) is drawn by writing depth from the fragment shader, +// always nearest; stencil is not carried over (Link's Awakening halves its 1408x792 depth-stencil buffer to 704x396) +static int fmt_int_or_ds(MTLPixelFormat f) { + switch (f) { + case MTLPixelFormatR8Uint: case MTLPixelFormatR8Sint: case MTLPixelFormatR16Uint: case MTLPixelFormatR16Sint: case MTLPixelFormatRG8Uint: case MTLPixelFormatRG8Sint: + case MTLPixelFormatR32Uint: case MTLPixelFormatR32Sint: case MTLPixelFormatRG16Uint: case MTLPixelFormatRG16Sint: case MTLPixelFormatRGBA8Uint: case MTLPixelFormatRGBA8Sint: + case MTLPixelFormatRGB10A2Uint: case MTLPixelFormatRG32Uint: case MTLPixelFormatRG32Sint: case MTLPixelFormatRGBA16Uint: case MTLPixelFormatRGBA16Sint: + case MTLPixelFormatRGBA32Uint: case MTLPixelFormatRGBA32Sint: case MTLPixelFormatDepth16Unorm: case MTLPixelFormatDepth32Float: case MTLPixelFormatStencil8: + case MTLPixelFormatDepth32Float_Stencil8: case MTLPixelFormatX32_Stencil8: return 1; + default: return 0; + } +} +static int copy_t2t_draw(id s, id d, unsigned sl, unsigned ss, unsigned dl, unsigned ds, const NVNcopyRegion *sr, const NVNcopyRegion *dr, int linear, int mirror) { + static NSMutableDictionary> *pso; static id vf, ff, fd; static id samp[2]; static id dss; static int failed; + if (failed || sr->width <= 0 || sr->height <= 0 || dr->width <= 0 || dr->height <= 0) return 0; + int depth = (s.pixelFormat == MTLPixelFormatDepth32Float || s.pixelFormat == MTLPixelFormatDepth32Float_Stencil8) && d.pixelFormat == s.pixelFormat, stencil = depth && d.pixelFormat == MTLPixelFormatDepth32Float_Stencil8; + if (!depth && (fmt_int_or_ds(s.pixelFormat) || fmt_int_or_ds(d.pixelFormat))) return 0; + if (mtl_fmt_block(d.pixelFormat) || !(d.usage & MTLTextureUsageRenderTarget) || s.sampleCount > 1 || d.sampleCount > 1) return 0; + if (!pso) { + static const char *src = + "#include \nusing namespace metal;\n" + "struct V { float4 p [[position]]; };\n" + "vertex V vs(uint id [[vertex_id]]) { float2 uv = float2((id << 1) & 2, id & 2); V o; o.p = float4(uv * float2(2, -2) + float2(-1, 1), 0, 1); return o; }\n" + // p[0] = source origin (texels) + source texels per destination texel, p[1] = destination origin + source level size + "fragment float4 fs(V in [[stage_in]], texture2d t [[texture(0)]], sampler s [[sampler(0)]], constant float4 *p [[buffer(0)]]) {\n" + " float2 st = p[0].xy + (in.p.xy - p[1].xy) * p[0].zw; return t.sample(s, st / p[1].zw, level(0)); }\n" + "struct D { float d [[depth(any)]]; };\n" + "fragment D fsd(V in [[stage_in]], depth2d t [[texture(0)]], sampler s [[sampler(0)]], constant float4 *p [[buffer(0)]]) {\n" + " float2 st = p[0].xy + (in.p.xy - p[1].xy) * p[0].zw; D o; o.d = t.sample(s, st / p[1].zw, level(0)); return o; }\n"; + NSError *e = nil; id lib = [g_dev newLibraryWithSource:@(src) options:nil error:&e]; + vf = [lib newFunctionWithName:@"vs"]; ff = [lib newFunctionWithName:@"fs"]; fd = [lib newFunctionWithName:@"fsd"]; + MTLDepthStencilDescriptor *dd = [MTLDepthStencilDescriptor new]; dd.depthCompareFunction = MTLCompareFunctionAlways; dd.depthWriteEnabled = YES; dss = [g_dev newDepthStencilStateWithDescriptor:dd]; + for (int i = 0; i < 2; i++) { MTLSamplerDescriptor *sd = [MTLSamplerDescriptor new]; sd.minFilter = sd.magFilter = i ? MTLSamplerMinMagFilterLinear : MTLSamplerMinMagFilterNearest; + sd.sAddressMode = sd.tAddressMode = MTLSamplerAddressModeClampToEdge; sd.supportArgumentBuffers = YES; samp[i] = [g_dev newSamplerStateWithDescriptor:sd]; } + if (!vf || !ff || !fd || !dss || !samp[0] || !samp[1]) { failed = 1; MLOG("【红】换格式 / 缩放贴图拷贝的管线建不出:%s", e.localizedDescription.UTF8String); return 0; } + pso = [NSMutableDictionary dictionary]; + } + id ps = pso[@(d.pixelFormat)]; + if (!ps) { + MTLRenderPipelineDescriptor *pd = [MTLRenderPipelineDescriptor new]; pd.vertexFunction = vf; pd.fragmentFunction = depth ? fd : ff; + if (depth) { pd.depthAttachmentPixelFormat = d.pixelFormat; if (stencil) pd.stencilAttachmentPixelFormat = d.pixelFormat; } else pd.colorAttachments[0].pixelFormat = d.pixelFormat; + NSError *e = nil; ps = nvn_cached_render(g_dev, pd, &e); + if (!ps) { RED_ONCE(83, "换格式 / 缩放贴图拷贝:目标格式 %lu 的管线建不出:%s", (unsigned long)d.pixelFormat, e.localizedDescription.UTF8String); return 0; } + pso[@(d.pixelFormat)] = ps; + } + unsigned sw = MAX(s.width >> sl, 1ul), sh = MAX(s.height >> sl, 1ul), dw = MAX(d.width >> dl, 1ul), dh = MAX(d.height >> dl, 1ul); + if ((unsigned)(dr->xoffset + dr->width) > dw || (unsigned)(dr->yoffset + dr->height) > dh) return 0; + int nz = sr->depth ? sr->depth : 1; + rp_end(); if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + for (int z = 0; z < nz; z++) { + id sv = [s newTextureViewWithPixelFormat:s.pixelFormat textureType:MTLTextureType2D levels:NSMakeRange(sl, 1) slices:NSMakeRange(ss + sr->zoffset + z, 1)]; + id pb; size_t po = arena_alloc(32, &pb); // parameters in the frame arena (resident already): a fresh 32-byte buffer per copy was a residency add, a commit and a release (Link's Awakening, about 11 a frame) + if (!sv || !pb) { RED_ONCE(84, "换格式 / 缩放贴图拷贝:建不出源视图 / 参数块"); return z > 0; } + float *p = (float *)((uint8_t *)pb.contents + po); p[0] = sr->xoffset; p[1] = sr->yoffset; p[2] = (float)sr->width / dr->width; p[3] = (float)sr->height / dr->height; + if (mirror & NVN_COPY_FLAGS_MIRROR_X_BIT) { p[0] += sr->width; p[2] = -p[2]; } // mirrored: walk the source region from its far edge + if (mirror & NVN_COPY_FLAGS_MIRROR_Y_BIT) { p[1] += sr->height; p[3] = -p[3]; } + p[4] = dr->xoffset; p[5] = dr->yoffset; p[6] = sw; p[7] = sh; + int whole = !dr->xoffset && !dr->yoffset && (unsigned)dr->width == dw && (unsigned)dr->height == dh; + MTLRenderPassDescriptor *rp = [MTLRenderPassDescriptor new]; + MTLRenderPassAttachmentDescriptor *a = depth ? (MTLRenderPassAttachmentDescriptor *)rp.depthAttachment : (MTLRenderPassAttachmentDescriptor *)rp.colorAttachments[0]; + a.texture = d; a.level = dl; a.slice = ds + dr->zoffset + z; a.loadAction = whole ? MTLLoadActionDontCare : MTLLoadActionLoad; a.storeAction = MTLStoreActionStore; + if (stencil) { MTLRenderPassStencilAttachmentDescriptor *st = rp.stencilAttachment; st.texture = d; st.level = dl; st.slice = a.slice; st.loadAction = MTLLoadActionLoad; st.storeAction = MTLStoreActionStore; } + ser_gate(); id en = enc_r_begin(rp); + if (depth) enc_r_dss(en, dss); + + [en setFragmentTexture:sv atIndex:0]; [en setFragmentSamplerState:samp[!!linear && !depth] atIndex:0]; [en setFragmentBuffer:pb offset:po atIndex:0]; + [en setViewport:(MTLViewport){dr->xoffset, dr->yoffset, dr->width, dr->height, 0, 1}]; + [en setScissorRect:(MTLScissorRect){(NSUInteger)dr->xoffset, (NSUInteger)dr->yoffset, (NSUInteger)dr->width, (NSUInteger)dr->height}]; + enc_r_pso(en, ps); [en drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3]; enc_r_end(en); + defer_obj_unref(sv); // released after this submission has run + } + args_dirty_all(); g_pso_dirty = g_dss_dirty = 1; + { static int once[2]; if (!once[depth]++) MLOG("换格式 / 缩放贴图拷贝:首例 %dx%d 格式 %lu → %dx%d 格式 %lu%s%s", sr->width, sr->height, (unsigned long)s.pixelFormat, dr->width, dr->height, (unsigned long)d.pixelFormat, + linear && !depth ? "(双线性)" : "", stencil ? "(只拷深度,模板不拷)" : ""); } + return 1; +} +// GPU: texture -> texture (previously mislisted as a no-op, 3 times per frame on the character select page). Same size, not mirrored, same format -> blit layer by layer; the other semantics (scaling with filtering, mirroring, format change) cannot be expressed by blit: report a failure, to be implemented +static void copy_t2t(const p_copy_t2t *c) { + za_diag_op("copy-texture-source", c->src); za_diag_op("copy-texture-destination", c->dst); + if (c->src) xv_check(c->src->serial, c->has_sv && c->sv.set_levels ? c->sv.base_level : 0, c->has_sv && c->sv.set_layers ? c->sv.min_layer : 0, "贴图拷贝源"); + rp_flush(); // Land the pending pure-clear render pass first (zero-source candidates only enter the set when the render pass ends); the blit() below would end the render pass anyway + if (c->dst) zskip_del(c->dst->serial); + if (c->src && c->dst && c->sr.xoffset == 0 && c->sr.yoffset == 0 && c->dr.xoffset == 0 && c->dr.yoffset == 0 + && c->sr.width == (int)c->src->d.width && c->sr.height == (int)c->src->d.height && c->dr.width == (int)c->dst->d.width && c->dr.height == (int)c->dst->d.height) { + int sl = c->has_sv && c->sv.set_levels ? c->sv.base_level : 0, dl = c->has_dv && c->dv.set_levels ? c->dv.base_level : 0; + int ss = c->has_sv && c->sv.set_layers ? c->sv.min_layer : 0, ds = c->has_dv && c->dv.set_layers ? c->dv.min_layer : 0; + if (zskip_has(c->src->serial, sl, ss)) zskip_add(c->dst->serial, dl, ds); + } + id s = tex_mtl(c->src), d = tex_mtl(c->dst); + PD("T src=%llu dst=%llu %dx%dx%d→%dx%dx%d flags=%#x", c->src->serial, c->dst->serial, c->sr.width, c->sr.height, c->sr.depth, c->dr.width, c->dr.height, c->dr.depth, c->flags); + if (!s || !d) return; + unsigned sl = c->has_sv && c->sv.set_levels ? c->sv.base_level : 0, dl = c->has_dv && c->dv.set_levels ? c->dv.base_level : 0; + unsigned sz0 = c->has_sv && c->sv.set_layers ? c->sv.min_layer : 0, dz0 = c->has_dv && c->dv.set_layers ? c->dv.min_layer : 0; + if (c->flags & DM_COPY_RESOLVE) { // Downsample: multisample resolve into the single-sample destination (depth takes sample 0); with equal sample counts it is a plain whole copy + if (s.sampleCount == d.sampleCount && s.pixelFormat == d.pixelFormat && s.width == d.width && s.height == d.height) { + enc_b_copy_texture(blit(), s, sz0, sl, MTLOriginMake(0, 0, 0), MTLSizeMake(s.width, s.height, 1), d, dz0, dl, MTLOriginMake(0, 0, 0)); + return; + } + if (s.sampleCount <= 1 || d.sampleCount != 1 || s.pixelFormat != d.pixelFormat || s.width != d.width || s.height != d.height) { + RED_ONCE(72, "Downsample 未实现的形态:%lux%lu %lu 样本 格式 %lu → %lux%lu %lu 样本 格式 %lu", (unsigned long)s.width, (unsigned long)s.height, (unsigned long)s.sampleCount, + (unsigned long)s.pixelFormat, (unsigned long)d.width, (unsigned long)d.height, (unsigned long)d.sampleCount, (unsigned long)d.pixelFormat); + return; + } + rp_end(); if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + MTLRenderPassDescriptor *rp = [MTLRenderPassDescriptor new]; + MTLPixelFormat pf = s.pixelFormat; + int depth = pf == MTLPixelFormatDepth32Float || pf == MTLPixelFormatDepth16Unorm || pf == MTLPixelFormatDepth32Float_Stencil8; + MTLRenderPassAttachmentDescriptor *a = depth ? rp.depthAttachment : rp.colorAttachments[0]; + a.texture = s; a.level = sl; a.slice = sz0; a.resolveTexture = d; a.resolveLevel = dl; a.resolveSlice = dz0; + a.loadAction = MTLLoadActionLoad; a.storeAction = MTLStoreActionStoreAndMultisampleResolve; + if (depth) rp.depthAttachment.depthResolveFilter = MTLMultisampleDepthResolveFilterSample0; + ser_gate(); enc_r_end(enc_r_begin(rp)); + return; + } + PD("t src=%llu lv%u sl%u xyz=%d,%d,%d dst=%llu lv%u sl%u xyz=%d,%d,%d size=%d,%d,%d", c->src->serial, sl, sz0, c->sr.xoffset, c->sr.yoffset, c->sr.zoffset, + c->dst->serial, dl, dz0, c->dr.xoffset, c->dr.yoffset, c->dr.zoffset, c->sr.width, c->sr.height, c->sr.depth); + int s3 = c->src->d.target == 2, d3 = c->dst->d.target == 2, nz = c->sr.depth ? c->sr.depth : 1; + if (no_bc() && (s.pixelFormat == MTLPixelFormatASTC_4x4_LDR || s.pixelFormat == MTLPixelFormatASTC_4x4_sRGB) && (d.pixelFormat == MTLPixelFormatRGBA8Unorm || d.pixelFormat == MTLPixelFormatRGBA8Unorm_sRGB) + && !s3 && !d3 && c->sr.width == c->dr.width && c->sr.height == c->dr.height && (d.usage & MTLTextureUsageShaderWrite)) { // No BC: ASTC source -> decompressed-format destination (see copy_t2t_astc) + int ok = 1; + for (int z = 0; z < nz && ok; z++) ok = copy_t2t_astc(s, d, sl, sz0 + c->sr.zoffset + z, dl, dz0 + c->dr.zoffset + z, c->sr.xoffset, c->sr.yoffset, c->dr.xoffset, c->dr.yoffset, c->sr.width, c->sr.height); + if (ok) return; + } + if ((c->sr.width != c->dr.width || c->sr.height != c->dr.height || s.pixelFormat != d.pixelFormat || (c->flags & (NVN_COPY_FLAGS_MIRROR_X_BIT | NVN_COPY_FLAGS_MIRROR_Y_BIT))) && !s3 && !d3 + && (c->sr.depth ? c->sr.depth : 1) == (c->dr.depth ? c->dr.depth : 1) + && !(c->flags & ~(NVN_COPY_FLAGS_ENGINE_2D_BIT | NVN_COPY_FLAGS_LINEAR_FILTER_BIT | NVN_COPY_FLAGS_MIRROR_X_BIT | NVN_COPY_FLAGS_MIRROR_Y_BIT)) + && copy_t2t_draw(s, d, sl, sz0, dl, dz0, &c->sr, &c->dr, !!(c->flags & NVN_COPY_FLAGS_LINEAR_FILTER_BIT), c->flags)) return; // format change / scaling / mirroring: draw it (see copy_t2t_draw) + if (c->sr.width != c->dr.width || c->sr.height != c->dr.height || (c->sr.depth ? c->sr.depth : 1) != (c->dr.depth ? c->dr.depth : 1) || (c->flags & ~NVN_COPY_FLAGS_ENGINE_2D_BIT & ~NVN_COPY_FLAGS_LINEAR_FILTER_BIT) + || s.pixelFormat != d.pixelFormat || s3 != d3) { + RED_ONCE(33, "贴图→贴图拷贝未实现的形态:%dx%dx%d→%dx%dx%d 标志 %#x 格式 %lu→%lu 3D %d→%d", c->sr.width, c->sr.height, c->sr.depth, c->dr.width, c->dr.height, c->dr.depth, + c->flags, (unsigned long)s.pixelFormat, (unsigned long)d.pixelFormat, s3, d3); + return; + } + id b = blit(); + if (s3) + enc_b_copy_texture(b, s, 0, sl, MTLOriginMake(c->sr.xoffset, c->sr.yoffset, c->sr.zoffset), MTLSizeMake(c->sr.width, c->sr.height, nz), d, 0, dl, MTLOriginMake(c->dr.xoffset, c->dr.yoffset, c->dr.zoffset)); + else + for (int z = 0; z < nz; z++) + enc_b_copy_texture(b, s, sz0 + c->sr.zoffset + z, sl, MTLOriginMake(c->sr.xoffset, c->sr.yoffset, 0), MTLSizeMake(c->sr.width, c->sr.height, 1), d, dz0 + c->dr.zoffset + z, dl, MTLOriginMake(c->dr.xoffset, c->dr.yoffset, 0)); + // Diagnostic DM_NVN_T2T_TRACE=WxH:file: after each copy into an RGBA16F texture of that size, copy the result once more into a trace buffer on the GPU timeline (frame numbers kept on the CPU side); after 240 copies write the file + { static int on = -1; static uint32_t tw, th; static const char *tf; static id tb; static int tn; static uint64_t tfr[240], from; + if (on < 0) { const char *e = getenv("DM_NVN_T2T_TRACE"), *fe = getenv("DM_NVN_T2T_FROM"); on = e && sscanf(e, "%ux%u", &tw, &th) == 2 && (tf = strchr(e, ':')) && *++tf; from = fe ? strtoull(fe, NULL, 10) : 0; } + if (on > 0 && tn < 240 && atomic_load(&g_cur_frame) >= from && d.width == tw && d.height == th && d.pixelFormat == MTLPixelFormatRGBA16Float && !s3) { + if (!tb) { tb = [g_dev newBufferWithLength:(size_t)240 * tw * th * 8 options:MTLResourceStorageModeShared]; res_add(tb); } + enc_b_copy_texture_buffer(blit(), d, dz0, dl, MTLOriginMake(0, 0, 0), MTLSizeMake(tw, th, 1), tb, (size_t)tn * tw * th * 8, (size_t)tw * 8, (size_t)tw * th * 8); + tfr[tn++] = atomic_load(&g_cur_frame); + if (tn == 240) { + rp_end(); if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + mtl_finish_gpu(); + FILE *f = fopen(tf, "wb"); + if (f) { fprintf(f, "%u %u %d\n", tw, th, tn); fwrite(tfr, 8, 240, f); fwrite(tb.contents, 1, (size_t)240 * tw * th * 8, f); fclose(f); } + MLOG("[t2t-trace] %d 次 → %s(帧 %llu–%llu)", tn, tf, (unsigned long long)tfr[0], (unsigned long long)tfr[239]); + } + } + } +} + +// GPU: clear a texture region with the given value (previously mislisted as a no-op, called 5 times at initialization; the initial value of a Metal private texture is undefined, and the game relies on the cleared initial value). +// Whole level and whole face, all four channels written -> open one clear-only render pass per layer / depth slice (the hardware converts the clear value by the texture format, including integer and depth); +// view with changed format: clear on a view of that format; partial region, partial channels, compressed / packaged textures (no render target usage): report a failure, to be implemented +static void clear_tex(const p_clear_tex *c) { + za_diag_op("clear-texture", c->tex); + if (c->tex) zskip_del(c->tex->serial); + id t = tex_mtl(c->tex); + PD("X ser=%llu 视图格式 %#x 区域 %d,%d,%d %dx%dx%d 掩码 %#x %s %g,%g,%g,%g", c->tex->serial, c->has_view && c->view.set_format ? c->view.format : -1, c->r.xoffset, c->r.yoffset, c->r.zoffset, c->r.width, c->r.height, c->r.depth, c->mask, + c->is_uint ? "ui" : "f", c->is_uint ? (double)c->c.u[0] : c->c.f[0], c->is_uint ? (double)c->c.u[1] : c->c.f[1], c->is_uint ? (double)c->c.u[2] : c->c.f[2], c->is_uint ? (double)c->c.u[3] : c->c.f[3]); + if (!t) return; + unsigned lv = c->has_view && c->view.set_levels ? c->view.base_level : 0, l0 = c->has_view && c->view.set_layers ? c->view.min_layer : 0; + NSUInteger w = MAX(t.width >> lv, 1u), h = MAX(t.height >> lv, 1u); + int is3d = t.textureType == MTLTextureType3D, nz = c->r.depth ? c->r.depth : 1; + if (c->has_view && c->view.set_format) { // View with changed format: the clear value is interpreted in the view format (all 5 of the game's calls are the ui version with an integer view), and the clear is done on a view of the same format + uint32_t vpf = 0; uint8_t swz[4]; + if (!nvn_fmt_to_mtl(c->view.format, &vpf, swz) || !vpf) { RED_ONCE(42, "清贴图视图格式 %#x 无对应", c->view.format); return; } + if (tex_need_pfv(t.pixelFormat, (MTLPixelFormat)vpf) && !(t.usage & MTLTextureUsagePixelFormatView)) t = tex_upgrade_pfv(c->tex, t, (MTLPixelFormat)vpf); + if (tex_need_pfv(t.pixelFormat, (MTLPixelFormat)vpf)) { + id v = [t newTextureViewWithPixelFormat:(MTLPixelFormat)vpf]; + if (!v) { RED_ONCE(43, "清贴图建不出视图:格式 %lu → %u", (unsigned long)t.pixelFormat, vpf); return; } + t = v; + } + } + MTLPixelFormat pf = t.pixelFormat; + int depth = pf == MTLPixelFormatDepth32Float || pf == MTLPixelFormatDepth16Unorm || pf == MTLPixelFormatDepth32Float_Stencil8, stencil = pf == MTLPixelFormatStencil8 || pf == MTLPixelFormatDepth32Float_Stencil8; + if (c->r.xoffset || c->r.yoffset || (NSUInteger)c->r.width != w || (NSUInteger)c->r.height != h || (c->mask & 0xf) != 0xf + || !(t.usage & MTLTextureUsageRenderTarget)) { + RED_ONCE(44, "清贴图未实现的形态:区域 %d,%d %dx%d(本级 %lux%lu)掩码 %#x 可作渲染目标 %d", c->r.xoffset, c->r.yoffset, c->r.width, c->r.height, + (unsigned long)w, (unsigned long)h, c->mask, !!(t.usage & MTLTextureUsageRenderTarget)); + return; + } + rp_end(); if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + for (int z = 0; z < nz; z++) { + MTLRenderPassDescriptor *rp = [MTLRenderPassDescriptor new]; + MTLRenderPassAttachmentDescriptor *a = depth ? rp.depthAttachment : stencil ? rp.stencilAttachment : rp.colorAttachments[0]; + a.texture = t; a.level = lv; a.loadAction = MTLLoadActionClear; a.storeAction = MTLStoreActionStore; + if (is3d) a.depthPlane = c->r.zoffset + z; else a.slice = l0 + c->r.zoffset + z; + if (depth) { rp.depthAttachment.clearDepth = c->c.f[0]; + if (stencil) { rp.stencilAttachment.texture = t; rp.stencilAttachment.level = lv; rp.stencilAttachment.slice = a.slice; rp.stencilAttachment.loadAction = MTLLoadActionClear; + rp.stencilAttachment.storeAction = MTLStoreActionStore; rp.stencilAttachment.clearStencil = c->c.u[1]; } } + else if (stencil) rp.stencilAttachment.clearStencil = c->c.u[0]; + else rp.colorAttachments[0].clearColor = c->is_uint ? MTLClearColorMake(c->c.u[0], c->c.u[1], c->c.u[2], c->c.u[3]) : MTLClearColorMake(c->c.f[0], c->c.f[1], c->c.f[2], c->c.f[3]); + ser_gate(); enc_r_end(enc_r_begin(rp)); + } +} + +// Context description of one submit (mtl_sub_commit takes one per submit and prints it into the failure line when the command buffer errors): for the GPU memory fault on the first frame entering Travincal, the failure line had only the sequence number and error code, and it was impossible to tell what was in that submit +static const char *cctx_snap(void) { + static char b[320]; static uint64_t p_pass, p_up, p_reup, p_ser; uint64_t up = 0, reup = 0; mtl_pkg_stats(&up, &reup); + pthread_mutex_lock(&g_deferred_mu); unsigned long nd = (unsigned long)g_deferred.count; pthread_mutex_unlock(&g_deferred_mu); + snprintf(b, sizeof b, "帧 %llu;自上次提交:通道 +%llu、贴图首传 +%llu、重传 +%llu、序号表变动 +%llu;延迟释放队列 %lu 个;世界冻结状态 %d", + (unsigned long long)atomic_load(&g_cur_frame), (unsigned long long)(g_stat_pass - p_pass), (unsigned long long)(up - p_up), (unsigned long long)(reup - p_reup), + (unsigned long long)(g_ser_gen - p_ser), nd, g_frz_state); + p_pass = g_stat_pass; p_up = up; p_reup = reup; p_ser = g_ser_gen; return b; +} +// ---------- Do not draw the world while the world is frozen (the whole game can be frozen while a panel is open) ---------- +// The "world frozen" status bit g_frz_on is set by mtl_world_frozen() (the host's freeze handshake calls it; in tests DM_NVN_TEST_FRZ=1 can stand in, or it can be switched in-process via @set:g_frz_on of DM_NVN_UB). +// This code does not judge by itself whether a panel is open. Structure of a frame (3.3.94012 in-game, per-pass dump): UI pass -> [UI stencil pass] -> about 40 passes of shadows / depth / lighting / transparency / post-processing -> final compositing pass (writes the window texture). +// The first frame after the bit is set = full frame: draw everything as usual, dropping only the draws of the UI stencil pass (stencil all 0 = no pixels blocked by the panel) -- post-processing normally does not write pixels blocked by the panel, so a shrinking panel would expose old content; +// from the second frame on = frozen frame: from the first render pass without a color attachment up to before the render pass that writes the window texture, set-target / clear / draw / texture-to-texture copy / compute are all not executed, the scene result stays as the full frame's copy, and the UI pass and the final compositing pass are drawn as usual. +// Frame boundaries are identified by render pass: the first set-target without the window texture after the window texture has been written = start of a new frame, and the status bit is latched here. Unrecognized structure (no render pass without color in a frame) = skip nothing. +// State constants / buffer updates / events / counters / texture uploads execute as usual (the UI pass needs them, the game is waiting). +int g_frz_on = -1; +static int g_frz_phase = 2, g_frz_drop, g_frz_seen_ds; static uint64_t g_frz_full_n, g_frz_skip_n; +void mtl_world_frozen(int on) { g_frz_on = on != 0; } +static int frz_wanted(void) { if (g_frz_on < 0) { const char *e = getenv("DM_NVN_TEST_FRZ"); g_frz_on = e && atoi(e); if (g_frz_on) MLOG("DM_NVN_TEST_FRZ:世界冻结状态位由测试开关顶替(常开)"); } tbdr_on(); return g_frz_on > 0 && tbdr_on() && g_ruler_truth <= 0; } +// ---------- Runtime extrapolation (power-saving rendering method; no switch for the player; it caused a stutter defect in released builds, now off by default, to be re-enabled once there is evidence of no stutter on A17) ---------- +// 3D every other frame: a fully drawn frame draws as usual and, before the attachments are repurposed, copies the opaque HDR (468), the main depth-stencil, the post-tone-mapping 468 and the post-FXAA 469; +// extrapolated frame: the UI stencil pass runs as usual; shadow map / depth prepass / main pass keep only the draws of moving units (skinned); before the first skinned draw of the depth prepass run one extrapolation pass (nvn_rtex.inc: by cb3 +464 -> +336 +// move the previous frame's HDR and depth to this frame, treat old unit positions 0x10 / UI cutout 0x2 as holes to fill, and export stencil 0x80 for holes and pixels newly exposed at the screen edge); translucents are drawn as usual and write effect bit 0x40; +// compositing / add bloom / add fog / tone mapping / FXAA really run only in the 0xD0 region, and outside the region the previous frame's LDR result is copied in with a whole-image shift (orthographic camera, rounded); full-screen items such as the bloom chain are skipped; sharpening upscale and UI compositing run as usual. +// Fallback to the original rendering method: version file render_safe = true (dm_update.m), the extrapolation pipeline cannot be built (whole run), test DM_RT_AB=0. When a panel is open and the world is frozen, yield to the freeze path. +// On a camera jump (orientation change or translation over 64 pixels) and for 4 frames before and after switching the 3D render scale, whole frames are fully drawn. Unit shadows on the ground keep using the previous fully drawn frame. +static int rt_is_skin(void) { // Moving units = the 10 whose vertex shader reads constants by bone index (the key table is taken from the Mac shader cache b-424a072084e6, identified by the dynamic vp_c5 index in the source) + __unsafe_unretained NvProg *pr = S.prog ? prog_get(S.prog) : nil; if (!pr || !pr->sh[0]) return 0; + static const char *k[] = { "0fa4eb7559f1637a", "1d97d33ab66abcba", "3deaf68fd65b5298", "4dffa355f2fbcdaf", "54c93fa22abff9ee", + "64dd7d022f3fae39", "67e2ebc28c830f44", "add86ee8e20f89b9", "b3ae9be5cc670f35", "fbf6dc1873a449e9" }; + for (unsigned i = 0; i < sizeof k / sizeof *k; i++) if (!strncmp(pr->sh[0]->key, k[i], 16)) return 1; + return 0; +} +int g_dm_rt_ab = 0; // Default is the original rendering method (the new method stutters in-game on A17 iPhone 15 Pro and ghosts characters on the character select page, so off for now); DM_RT_AB=1 turns on the new method for testing. The version file render_safe can only set it to 0, no effect +static struct { int env, frame, filt, first, hdrp, mainp_seen, skipall, oit_seen, tm_seen, fx_state, comp_seen; uint64_t ldr_ser; float vpc[16], vpp[16]; id ldr, pl468, pl469; int ui_seen, main_seen, copied, ex_done, shadow, have_prev, have_ldr, sdx, sdy, cam_checked, force_full, disabled, was_on; uint64_t n_jump; uint64_t ds_ser, hdr_ser, n_ex, n_full, n_noex; + id ds, hdr, pds, phdr; struct { uint64_t key; int skin, other; } sh[16]; int nsh, cur_sh; } g_rt = { .env = -1 }; +static int rt_want(void); +static void rt_toggle_poll(void) { static int tg = -1; if (tg < 0) { const char *e = getenv("DM_RT_AB_TOGGLE"); tg = e ? atoi(e) : 0; (void)rt_want(); } // On first call also read DM_RT_AB (on a real device the world-freeze bit is set to 0 very early, so frz_op is not necessarily called) // test: switch back and forth between A / B every tg seconds by wall clock + if (tg > 0) { int v = (int)((time(NULL) / tg) & 1); if (v != g_dm_rt_ab) { g_dm_rt_ab = v; MLOG("DM_RT_AB_TOGGLE:画法切到 %s", v ? "B" : "A"); } } } +void mtl_rt_force_full(int n) { g_rt.force_full = n; } // Called by the host: before an overall picture change such as switching the 3D render scale, the following n frames are all fully drawn +// ---------- Power-saving mode (30 frames) ---------- +// On: the display link object changes to 30 Hz (frame rate range 30–30, a ProMotion screen lowers its refresh rate accordingly; the frame rate hint in front_ios.m is lowered together), the game presents one frame per display-link tick; runtime extrapolation off (really draws every frame). +// Game logic already runs at 25 ticks / second and unit positions are interpolated by time, so there are no repeated frames at 30 frames (measured on a real device: coefficient of variation of per-frame camera displacement 0.292 at 30 frames, 0.296 at 60 frames). +// The switch is on the 「自定义」(Custom) page (dm_custom.c eco_fps, off by default, takes effect on apply); the test switch DM_FPS=30/60 pins it. +__attribute__((weak)) int dm_fps_setting(void) { return 60; } // The host's dm_custom.c provides the strong definition; always 60 when graphics unit tests do not link the host +// When the game itself asks for 30 fps (pacing by the game's present interval, interval 2, hle_nvn.c) the display link runs at 30 Hz too: games whose logic counts frames, such as Engage, are not pulled up to 60 fps by the display link on iOS +__attribute__((weak)) int nvn_game_fps(void) { return 60; } // hle_nvn.c provides the strong definition +int mtl_fps_target(void) { static int env = -2; if (env == -2) { const char *e = getenv("DM_FPS"); env = e ? atoi(e) : -1; if (env != 30 && env != 60) env = -1; else MLOG("DM_FPS=%d:帧数由测试开关钉住", env); } + return env > 0 ? env : dm_fps_setting() == 30 || nvn_game_fps() == 30 ? 30 : 60; } +// Whenever the extrapolation enabled state changes (power-saving mode toggled, world freeze takes over / hands back): first let the "no store across frames" ledger relearn (during the trial period it stores as usual), and the extrapolation really switches one frame later -- +// guaranteeing the last submit before the switch is a full store. Otherwise attachments cleared first every frame at 30 frames are learned as no-store, and the first extrapolated frame after switching back to 60 frames reads old content and flashes for one frame (measured on a test build; only reverted after the xv self-check reported a failure) +static void xv_relearn(const char *why) { g_xv_n = 0; g_xv_ntrial = 0; g_xv_ntouch = 0; MLOG("跨帧不写回:%s,账本清空重学(试用期内照常写回)", why); } +static int rt_want_raw(void); +static int rt_want(void) { + static int eff = -1; static uint64_t pend; int want = rt_want_raw(); uint64_t f = atomic_load(&g_cur_frame); + if (eff < 0) eff = want; + if (want == eff) { pend = 0; return eff; } + if (!pend) { pend = f + 1; xv_relearn(want ? "外推将启用" : "外推将停用"); return eff; } + if (f > pend) { eff = want; pend = 0; } + return eff; +} +static int rt_want_raw(void) { static int forced; if (g_rt.env < 0) { const char *e = getenv("DM_RT_AB"); g_rt.env = e ? atoi(e) : 0; forced = e != NULL; if (e) MLOG("DM_RT_AB=%d:画法由环境变量钉住(测试用,自定义页的开关不再起作用)", g_rt.env); } + if (forced) g_dm_rt_ab = g_rt.env; + return g_dm_rt_ab == 1 && !g_rt.disabled && g_frz_on <= 0 && tbdr_on() && g_ruler_truth <= 0 && mtl_fps_target() == 60; } // When a panel is open and the world is frozen, yield to the freeze path (saves more); power-saving mode (30 frames) really draws every frame +#include "nvn_rtex.inc" +static uint64_t rt_key(const p_set_rt *r) { uint64_t s_; int lv, sl; rt_sub(r, 8, &s_, &lv, &sl); return s_ * 64 + (uint64_t)lv * 8 + (uint64_t)sl; } +static int rt_is_shadow(const p_set_rt *r) { return !r->n && r->depth && r->depth->d.width == r->depth->d.height; } +static id rt_cp(id src, id dst0) { + id dst = dst0; + if (!dst || dst.width != src.width || dst.height != src.height || dst.pixelFormat != src.pixelFormat) { + MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:src.pixelFormat width:src.width height:src.height mipmapped:NO]; + d.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget | MTLTextureUsagePixelFormatView; d.storageMode = MTLStorageModePrivate; + if (dst0) defer_obj_release(dst0); // Size change: the old copy may still be read by an in-flight command buffer (MTL4 command buffers do not retain resources), so release it deferred + dst = [g_dev newTextureWithDescriptor:d]; if (!dst) return nil; res_add(dst); } + id b = blit(); enc_b_copy_texture(b, src, 0, 0, MTLOriginMake(0, 0, 0), MTLSizeMake(src.width, src.height, 1), dst, 0, 0, MTLOriginMake(0, 0, 0)); return dst; } +static void rt_blit_shift(id src, id dst, int dx, int dy) { // Integer-offset copy: dst(x + dx, y + dy) = src(x, y); the strip at the screen edge that cannot be moved is left for the region recompute (the extrapolation pass has already marked it 0x80) + if (!src || !dst || src.width != dst.width || src.height != dst.height) return; + int W = (int)src.width, H = (int)src.height, w = W - abs(dx), h = H - abs(dy); if (w <= 0 || h <= 0) return; + id b = blit(); enc_b_copy_texture(b, src, 0, 0, MTLOriginMake(MAX(0, -dx), MAX(0, -dy), 0), MTLSizeMake(w, h, 1), dst, 0, 0, MTLOriginMake(MAX(0, dx), MAX(0, dy), 0)); } +static void rt_copy_prev(void) { // Fully drawn frame: copy the opaque HDR and the main depth-stencil once each (in order within the GPU, in this command buffer) + if (g_rt.copied || !g_rt.main_seen || !g_rt.hdr || !g_rt.ds) return; g_rt.copied = 1; // Do not copy when the main pass was not recognized this frame (foreground / loading screen): the textures recognized last time may already have been released by the game and not be in the residency set + if (!rx_setup(g_rt.hdr.pixelFormat, g_rt.ds.pixelFormat)) { if (!g_rt.disabled) { g_rt.disabled = 1; MLOG("【红】运行时外推:外推管线在这台设备上建不出,整局退回原画法"); } return; } // Cannot be built (old GPU does not support stencil export etc.): fall back for the whole run + g_rt.phdr = rt_cp(g_rt.hdr, g_rt.phdr); g_rt.pds = rt_cp(g_rt.ds, g_rt.pds); g_rt.have_prev = g_rt.phdr && g_rt.pds; } +static void rt_frame_begin(void) { g_rt.filt = g_rt.ui_seen = g_rt.main_seen = g_rt.copied = g_rt.ex_done = g_rt.mainp_seen = 0; g_rt.cur_sh = -1; + g_rt.oit_seen = g_rt.tm_seen = g_rt.fx_state = g_rt.comp_seen = g_rt.skipall = g_rt.cam_checked = 0; g_rt_ovr = 0; g_dss_dirty = 1; + if (g_frz_state == 0) { g_rt.have_prev = g_rt.have_ldr = 0; for (int i = 0; i < g_rt.nsh; i++) g_rt.sh[i].skin = g_rt.sh[i].other = 0; } } // Fully drawn frame: the next frame may extrapolate only if this frame's copies succeeded +static void rt_set_rt(const p_set_rt *r, int win) { // Every set-target (both fully drawn and extrapolated frames go through here): identify roles, find the copy points in fully drawn frames, and in extrapolated frames decide whether this render pass keeps only skinned draws + g_rt.cur_sh = -1; g_rt.shadow = 0; g_rt.first = 1; g_rt.hdrp = 0; + if (win) { if (g_frz_state == 0) { rt_copy_prev(); if (g_rt.fx_state == 2 && g_rt.ldr) { g_rt.pl469 = rt_cp(g_rt.ldr, g_rt.pl469); g_rt.have_ldr = g_rt.pl469 != nil; g_rt.fx_state = 3; } } + g_rt.filt = 0; g_rt.skipall = 0; if (g_rt_ovr) { g_rt_ovr = 0; g_dss_dirty = 1; } return; } + id c0 = r->n > 0 && r->color[0] ? tex_mtl(r->color[0]) : nil, dt = r->depth ? tex_mtl(r->depth) : nil; + int hdr_here = 0; for (int i = 0; i < r->n && i < 8; i++) if (r->color[i] && g_rt.hdr_ser && r->color[i]->serial == g_rt.hdr_ser) hdr_here = 1; + if (g_frz_state == 0) { + if (!g_rt.main_seen && r->n == 1 && c0 && c0.pixelFormat == MTLPixelFormatRG11B10Float && dt && dt.pixelFormat == MTLPixelFormatDepth32Float_Stencil8 && dt.width >= 500 && dt.width != dt.height) { + { static uint64_t lh, ld; if (lh != r->color[0]->serial || ld != r->depth->serial) { lh = r->color[0]->serial; ld = r->depth->serial; MLOG("运行时外推:认出主通道 不透明 HDR 序号 %llu %lux%lu、主深度模板序号 %llu %lux%lu(通道 %llu)", (unsigned long long)lh, (unsigned long)c0.width, (unsigned long)c0.height, (unsigned long long)ld, (unsigned long)dt.width, (unsigned long)dt.height, g_stat_pass + 1); } } + g_rt.main_seen = 1; g_rt.hdr_ser = r->color[0]->serial; g_rt.ds_ser = r->depth->serial; g_rt.hdr = c0; g_rt.ds = dt; } + else if (g_rt.main_seen && hdr_here && !g_rt.copied) rt_copy_prev(); // The first set-target after the main pass that attaches the opaque HDR again (the translucent pass; 468 occupies slot 0 but is not written): before it, copy the HDR and the main depth-stencil + else if (g_rt.main_seen && hdr_here && r->n == 1 && r->depth && r->depth->serial == g_rt.ds_ser) g_rt.fx_state = 1; // Only 468 + main depth attached = tone mapping (repurposes 468 as the LDR output) + else if (g_rt.fx_state == 1) { // The first render pass after tone mapping = FXAA (reads 468, writes 469): before it, copy the post-tone-mapping 468 (LDR) and note down 469 + if (r->n >= 1 && r->color[0] && c0) { g_rt.ldr_ser = r->color[0]->serial; g_rt.ldr = c0; g_rt.pl468 = rt_cp(g_rt.hdr, g_rt.pl468); g_rt.fx_state = 2; } else g_rt.fx_state = 0; } + else if (g_rt.fx_state == 2 && g_rt.ldr) { g_rt.pl469 = rt_cp(g_rt.ldr, g_rt.pl469); g_rt.have_ldr = g_rt.pl468 && g_rt.pl469; g_rt.fx_state = 3; } // Before the first render pass after FXAA: copy the post-FXAA 469 + } + if (rt_is_shadow(r)) { uint64_t k = rt_key(r); int i; for (i = 0; i < g_rt.nsh && g_rt.sh[i].key != k; i++) {} + if (i == g_rt.nsh && i < 16) { g_rt.sh[i].key = k; g_rt.sh[i].skin = g_rt.sh[i].other = 0; g_rt.nsh++; } g_rt.cur_sh = i < 16 ? i : -1; g_rt.shadow = 1; } + int ds_here = r->depth && g_rt.ds_ser && r->depth->serial == g_rt.ds_ser; + if (ds_here && !r->n && !g_rt.ui_seen) { g_rt.ui_seen = 1; g_rt.filt = 0; return; } // The first render pass in this frame with the main depth and no color = the UI stencil pass: run as usual + g_rt.hdrp = hdr_here && ds_here && r->n == 1 && !g_rt.mainp_seen; if (g_rt.hdrp) g_rt.mainp_seen = 1; // The first render pass in this frame with "only opaque HDR + main depth attached" = the main pass + g_rt.filt = g_rt.shadow || (ds_here && !r->n) || g_rt.hdrp; + if (g_frz_state == 2 && g_rt.mainp_seen && !g_rt.hdrp) { // Extrapolated frame, after the main pass: post-processing runs only in the unit / effect region (plan B) + int c0ldr = r->n >= 1 && r->color[0] && g_rt.ldr_ser && r->color[0]->serial == g_rt.ldr_ser, ovr = 0; g_rt.skipall = 0; + if (g_rt.fx_state == 2) {} // After FXAA (sharpening upscale + vignette, UI compositing): run everything + else if (g_rt.fx_state == 1) g_rt.fx_state = 2; + else if (!g_rt.oit_seen && ds_here && r->n >= 2) { g_rt.oit_seen = 1; ovr = 3; } // Translucent (weighted blended, two targets): draw as usual, writing effect bit 0x40 where it passes + else if (!g_rt.comp_seen) { if (ds_here && c0ldr) { g_rt.comp_seen = 1; ovr = 1; rt_blit_shift(g_rt.pl469, g_rt.ldr, g_rt.sdx, g_rt.sdy); } else g_rt.skipall = 1; } // Before compositing: the previous frame's post-FXAA 469 is copied in with an integer shift (then recomputed in the region) // the other passes before compositing (mip chain, 511 refraction layer etc.) are skipped + else if (ds_here && c0ldr && g_rt.tm_seen) { // The FXAA pass itself (recomputed in the region; the 469 outside the region was already shift-copied before compositing) + g_rt.fx_state = 1; ovr = 1; } + else if (ds_here) { if (hdr_here && r->n == 1) { g_rt.tm_seen = 1; rt_blit_shift(g_rt.pl468, g_rt.hdr, g_rt.sdx, g_rt.sdy); } ovr = 1; } // Before tone mapping: the previous frame's post-tone-mapping 468 (LDR) is copied in with an integer shift // add bloom, add fog, tone mapping: run only in the region + else g_rt.skipall = 1; // Those without the main depth attached, such as the bloom chain: skip (the region keeps using the previous frame's bloom) + if (ovr != g_rt_ovr) { g_rt_ovr = ovr; g_dss_dirty = 1; } + } +} +static int rt_op(unsigned op) { // Non-set-target operations in an extrapolated frame: returns 1 = not executed + if (g_frz_state == 2 && g_rt_ovr == 1 && op == OP_CLEAR_COLOR) return 1; // The region post-processing passes do not clear: outside the region is the previous frame's result copied in with a shift + if (g_rt.skipall && (op == OP_DRAW || op == OP_DRAW_IDX || op == OP_CLEAR_COLOR || op == OP_CLEAR_DS || op == OP_COPY_T2T || op == OP_CLEAR_TEX || op == OP_DISPATCH)) return 1; + if (op == OP_DRAW || op == OP_DRAW_IDX) { + if (g_frz_state == 2 && !g_rt.cam_checked) { // First draw of an extrapolated frame (UI stencil pass, before shadows / prepass): check this frame's camera, and on a jump switch the whole frame to fully drawn + g_rt.cam_checked = 1; const p_bind_buf *u = &S.ubo[0][3]; void *mb; uint64_t mo; const uint8_t *base; int ok = 0; + if (u->addr && u->size >= 528 && mtl_addr(u->addr, u->size, &mb, &mo) && (base = [(__bridge id)mb contents])) { + const float *cur = (const float *)(base + mo + 336), *prev = (const float *)(base + mo + 464); int dx, dy; float md = 0; + for (int r = 0; r < 3; r++) for (int c = 0; c < 4; c++) md = MAX(md, fabsf(cur[r * 4 + c] - prev[r * 4 + c])); // The first three rows (orientation / scale) should be unchanged: an orthographic camera only translates + rt_shift_px(prev, cur, g_rt.hdr.width, g_rt.hdr.height, &dx, &dy); + ok = md < 1e-4f && abs(dx) <= 64 && abs(dy) <= 64; if (!ok) { static int n; if (n++ < 20) MLOG("运行时外推:镜头跳变(平移 %d,%d 像素、朝向差 %g),本帧改全画", dx, dy, md); } } + if (!ok) { g_frz_state = 0; g_rt.n_jump++; rt_frame_begin(); g_rt.cam_checked = 1; return 0; } + } + if (g_frz_state == 0) { if (g_rt.cur_sh >= 0) { if (rt_is_skin()) g_rt.sh[g_rt.cur_sh].skin++; else g_rt.sh[g_rt.cur_sh].other++; } return 0; } + if (!g_rt.filt) return 0; + if (!rt_is_skin()) return 1; + if (!g_rt.ex_done && !g_rt.shadow) { // Before the first skinned draw of the depth prepass: the extrapolation pass (reads the cb3 bound by this draw: +336 this frame's, +464 the previous frame's view-projection) + g_rt.ex_done = 1; const p_bind_buf *u = &S.ubo[0][3]; void *mb; uint64_t mo; const uint8_t *base; + if (u->addr && u->size >= 528 && mtl_addr(u->addr, u->size, &mb, &mo) && (base = [(__bridge id)mb contents])) { + float cur[16], prev[16]; memcpy(cur, base + mo + 336, 64); memcpy(prev, base + mo + 464, 64); memcpy(g_rt.vpc, cur, 64); memcpy(g_rt.vpp, prev, 64); rt_shift_px(prev, cur, g_rt.hdr.width, g_rt.hdr.height, &g_rt.sdx, &g_rt.sdy); + rp_end(); if (rt_ex_encode(g_rt.hdr, g_rt.ds, g_rt.phdr, g_rt.pds, prev, cur, 0x10, 0x2)) g_rt.n_ex++; else g_rt.n_noex++; + } else g_rt.n_noex++; + } + return 0; + } + if (g_frz_state == 2 && g_rt.filt) { + if (op == OP_CLEAR_COLOR || op == OP_CLEAR_DS) { + if (g_rt.shadow && g_rt.cur_sh >= 0 && !g_rt.sh[g_rt.cur_sh].other) return 0; // A shadow level that had only skinned draws in the previous fully drawn frame (character-only level): clear and draw as usual + return 1; } + } + return 0; +} +static int frz_op(unsigned op, const void *a) { // Returns 1 = this operation is not executed + if (op == OP_SET_RT) { + const p_set_rt *r = a; int win = 0; + for (int i = 0; i < r->n; i++) { uint64_t s_; int lv, sl; rt_sub(r, i, &s_, &lv, &sl); if (s_ && xv_present(s_)) win = 1; } + if (g_frz_phase == 2 && !win && rt_want()) { // Runtime extrapolation: new frame, fully drawn frames and extrapolated frames alternate + int was = g_frz_state; g_frz_phase = 0; g_rt.was_on = 1; + { static int dbg = -1, n; if (dbg < 0) dbg = getenv("DM_RT_DBG") != NULL; if (dbg && n < 4000 && g_rt.ds_ser) { n++; MLOG("[rtdbg] 帧界 通道 %llu 上一段状态 %d 认出主通道 %d 拷了 %d 外推 %d fx %d ldr %d hoist %d fz %d defer %d/%d", g_stat_pass, was, g_rt.main_seen, g_rt.copied, g_rt.ex_done, g_rt.fx_state, g_rt.have_ldr, g_hoist_n, g_fz.active, defer_has_active(), g_defer_replaying); } } + if (g_rt.force_full > 0) g_rt.force_full--; + g_frz_state = !was && g_rt.have_prev && g_rt.have_ldr && !g_rt.force_full && !g_hoist_n && !g_fz.active && !defer_has_active() && !g_defer_replaying ? 2 : 0; + if (g_frz_state == 2) g_rt.frame++; else g_rt.n_full++; + if (g_frz_state != was && (g_rt.frame + g_rt.n_full) % 600 < 2) MLOG("运行时外推:全画帧 %llu、外推帧 %d(外推趟编了 %llu 次、没编 %llu 次;镜头跳变改全画 %llu 次)", g_rt.n_full, g_rt.frame, g_rt.n_ex, g_rt.n_noex, g_rt.n_jump); + rt_frame_begin(); + } + if (g_frz_phase != 2 && rt_want()) { if (win) g_frz_phase = 2; rt_set_rt(r, win); return g_rt.skipall; } + if (g_frz_phase == 2 && win && rt_want()) { rt_set_rt(r, win); return 0; } + { int on = rt_want(); if (g_rt.was_on && !on) { // The new rendering method was just turned off (version file fallback / panel freeze takes over / fallback for the whole run): reset state to zero and hand back to the original path + g_frz_state = 0; g_frz_phase = 2; g_rt.have_prev = g_rt.have_ldr = 0; if (g_rt_ovr) { g_rt_ovr = 0; g_dss_dirty = 1; } g_rt.skipall = 0; + MLOG("运行时外推:新画法停用(画法 %d、世界冻结 %d、整局退回 %d)", g_dm_rt_ab, g_frz_on, g_rt.disabled); } + g_rt.was_on = on; } + if (g_frz_phase == 2 && !win) { // New frame + int on = frz_wanted(), was = g_frz_state; g_frz_phase = 0; g_frz_seen_ds = 0; + { static uint64_t fr, p0, k0; if (was == 2 && (++fr == 4 || fr % 600 == 0)) MLOG("世界冻结:第 %llu 个冻结帧开了 %llu 个通道、跳过 %llu 条设目标", fr, g_stat_pass - p0, g_frz_skip_n - k0); if (was != 2) fr = 0; p0 = g_stat_pass; k0 = g_frz_skip_n; } + if (!on) g_frz_state = 0; + else if (was) g_frz_state = 2; + else if (!g_hoist_n && !g_fz.active && !defer_has_active() && !g_defer_replaying) g_frz_state = 1; // The premises / merges / deferrals left by the previous frame have not finished yet: enter next frame + if (g_frz_state != was) MLOG("世界冻结:通道 %llu 起 %s(完整帧累计 %llu,已跳过通道 %llu)", g_stat_pass + 1, g_frz_state == 1 ? "完整帧(不带界面模板)" : g_frz_state == 2 ? "冻结帧(只画界面与末尾合成)" : "恢复正常", g_frz_full_n += g_frz_state == 1, g_frz_skip_n); + } + g_frz_drop = 0; + if (win) { g_frz_phase = 2; return 0; } + if (!g_frz_state || g_frz_phase == 2) return 0; + if (g_frz_phase == 0 && !r->n && r->depth && !g_frz_seen_ds) { // The first render pass without color in this frame + g_frz_seen_ds = 1; + if (g_frz_state == 2) g_frz_phase = 1; + else { uint64_t s_; int lv, sl; rt_sub(r, 8, &s_, &lv, &sl); static int neg = -1; if (neg < 0) neg = getenv("DM_NVN_TEST_FRZ_NEG") != NULL; // Negative control: the full frame is drawn with the UI stencil as usual, and old pixels must be exposed when the panel shrinks + g_frz_drop = !neg && MAX(r->depth->d.width >> lv, 1u) != MAX(r->depth->d.height >> lv, 1u); } // Full frame: the one that is not square (shadow map) is the UI stencil pass + } + if (g_frz_phase == 1) { g_frz_skip_n++; return 1; } + return 0; + } + if (rt_want() && g_frz_phase != 2) return rt_op(op); + if (g_frz_drop) return op == OP_DRAW || op == OP_DRAW_IDX; + if (g_frz_state == 2 && g_frz_phase == 1) return op == OP_CLEAR_COLOR || op == OP_CLEAR_DS || op == OP_DRAW || op == OP_DRAW_IDX || op == OP_COPY_T2T || op == OP_CLEAR_TEX || op == OP_DISPATCH; + return 0; +} + +// Hand a submission to the GPU in segments (DM_NVN_CUT=draw count, 0 = off; default 64): when the game holds back the next frame's submit until this frame's GPU work is done (The Legend of Zelda: Breath of the Wild: the main thread's nvnSyncWait on the fence of the frame just submitted), +// the GPU sits idle for the milliseconds the host spends translating inside the submit (in the shrine each frame is GPU 13 ms + translation 7 ms back to back). Each time this many draws have accumulated, the command buffer encoded so far is committed between two passes: the GPU runs the earlier passes while the CPU translates the later ones. +// Cut only when no encoder is open and no pass is deferred / merged / hoisted; commit this batch's uploads first (later draws use them). The fence signal and completion work stay on the last segment only: the queue executes in commit order and every encoder starts with a queue barrier +static int cut_draws(void) { static int v = -1; if (v < 0) { const char *e = getenv("DM_NVN_CUT"); v = e ? atoi(e) : 64; MLOG("分段提交:每 %d 次绘制在通道之间先交一段(DM_NVN_CUT,0 关)", v); } return v; } +static uint64_t g_cut_draw0, g_cut_sub, g_cuts; +static void sub_cut(void) { + int n = cut_draws(); + if (n <= 0 || !g_cb || g_enc) return; + if (g_cut_sub != g_sub_serial) { g_cut_sub = g_sub_serial; g_cut_draw0 = g_stat_draw; } // count from the start of each submission + if (g_stat_draw - g_cut_draw0 < (uint64_t)n || g_hoist_n || g_defer_nact || g_defer_pend || g_fz.active || g_rt_merged) return; + if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + up_flush(); + uint64_t f = atomic_load(&g_cur_frame); gpu_tag_begin(f); + NSArray *cut_retire = defer_take_cut_batch(); + + [g_cb addCompletedHandler:^(id cb) { + log_function_errors(cb, "split", f); + gpu_tag_done(f, cb.GPUStartTime, cb.GPUEndTime); + if (cb.error) { atomic_store(&g_last_commit_error, 1); static int k; if (k++ < 8) MLOG("【红】分段命令缓冲执行出错:%s", cb.error.description.UTF8String); } + defer_release_batch(cut_retire); + res_commit(); + }]; + res_commit(); + [g_cb commit]; + atomic_fetch_add_explicit(&g_cut_committed_diag, 1, memory_order_relaxed); + g_cb = replay_cb(); + g_cut_draw0 = g_stat_draw; + if (!g_cuts++) MLOG("分段提交:首段在帧 %llu 通道 %llu 交出", (unsigned long long)f, (unsigned long long)g_stat_pass); +} +uint64_t mtl_cut_count(void) { return g_cuts; } + +// ---------- Clears that a load action cannot express ---------- +// A mid-pass clear, a partial channel mask, or a scissored clear is rendered as +// one full-pass triangle. This preserves the open pass and changes only the +// requested attachment channels inside the active scissor. +static int pf_int_kind(MTLPixelFormat f) { + switch (f) { + case MTLPixelFormatR8Uint: case MTLPixelFormatR16Uint: case MTLPixelFormatR32Uint: case MTLPixelFormatRG8Uint: case MTLPixelFormatRG16Uint: case MTLPixelFormatRG32Uint: + case MTLPixelFormatRGBA8Uint: case MTLPixelFormatRGB10A2Uint: case MTLPixelFormatRGBA16Uint: case MTLPixelFormatRGBA32Uint: return 1; + case MTLPixelFormatR8Sint: case MTLPixelFormatR16Sint: case MTLPixelFormatR32Sint: case MTLPixelFormatRG8Sint: case MTLPixelFormatRG16Sint: case MTLPixelFormatRG32Sint: + case MTLPixelFormatRGBA8Sint: case MTLPixelFormatRGBA16Sint: case MTLPixelFormatRGBA32Sint: return 2; + default: return 0; + } +} +static id clr_lib(int slot, int kind) { + static id libs[8][3]; static uint8_t failed[8][3]; + if (libs[slot][kind] || failed[slot][kind]) return libs[slot][kind]; + static const char *ty[3] = {"float", "uint", "int"}; char src[720]; + snprintf(src, sizeof src, "#include \nusing namespace metal;\nstruct V { float4 p [[position]]; };\n" + "vertex V clr_vs(uint i [[vertex_id]], device const float *z [[buffer(0)]]) { V o; o.p = float4(float2(float((i << 1) & 2), float(i & 2)) * 2.0 - 1.0, z[0], 1.0); return o; }\n" + "struct O { %s4 c [[color(%d)]]; };\nfragment O clr_fs(device const %s4 *v [[buffer(0)]]) { O o; o.c = v[0]; return o; }\n", ty[kind], slot, ty[kind]); + NSError *e = nil; libs[slot][kind] = [g_dev newLibraryWithSource:@(src) options:nil error:&e]; + if (!libs[slot][kind]) { failed[slot][kind] = 1; MLOG("[RED] clear-triangle shader compile failed (slot %d kind %d): %s", slot, kind, e.localizedDescription.UTF8String); } + return libs[slot][kind]; +} +typedef struct { int32_t slot, kind, mask; uint32_t cfmt[8], dfmt, sfmt, samples; } clr_key; +static id clr_pso(const clr_key *k) { + static NSMutableDictionary> *cache; if (!cache) cache = [NSMutableDictionary dictionary]; + NSData *kd = [NSData dataWithBytes:k length:sizeof *k]; id ps = cache[kd]; if (ps) return ps; + id lib = clr_lib(k->slot < 0 ? 0 : k->slot, k->slot < 0 ? 0 : k->kind); if (!lib) return nil; + MTLRenderPipelineDescriptor *d = [MTLRenderPipelineDescriptor new]; + d.vertexFunction = [lib newFunctionWithName:@"clr_vs"]; if (k->slot >= 0) d.fragmentFunction = [lib newFunctionWithName:@"clr_fs"]; + for (int i = 0; i < 8; i++) if (k->cfmt[i]) { d.colorAttachments[i].pixelFormat = (MTLPixelFormat)k->cfmt[i]; d.colorAttachments[i].writeMask = MTLColorWriteMaskNone; } + if (k->slot >= 0) d.colorAttachments[k->slot].writeMask = (MTLColorWriteMask)((k->mask & 1 ? MTLColorWriteMaskRed : 0) | (k->mask & 2 ? MTLColorWriteMaskGreen : 0) | (k->mask & 4 ? MTLColorWriteMaskBlue : 0) | (k->mask & 8 ? MTLColorWriteMaskAlpha : 0)); + d.depthAttachmentPixelFormat = (MTLPixelFormat)k->dfmt; d.stencilAttachmentPixelFormat = (MTLPixelFormat)k->sfmt; d.rasterSampleCount = k->samples ? k->samples : 1; + NSError *e = nil; ps = [g_dev newRenderPipelineStateWithDescriptor:d error:&e]; + if (!ps) { static int n; if (n++ < 4) MLOG("[RED] cannot create clear-triangle pipeline (slot %d color format %u depth %u stencil %u samples %u): %s", k->slot, k->cfmt[k->slot], k->dfmt, k->sfmt, k->samples, e.localizedDescription.UTF8String); return nil; } + cache[kd] = ps; return ps; +} +static id clr_dss(int dw, unsigned sm) { + static id cache[2][256]; if (cache[dw][sm]) return cache[dw][sm]; + MTLDepthStencilDescriptor *d = [MTLDepthStencilDescriptor new]; d.depthCompareFunction = MTLCompareFunctionAlways; d.depthWriteEnabled = dw != 0; + if (sm) { + MTLStencilDescriptor *st = [MTLStencilDescriptor new]; st.stencilCompareFunction = MTLCompareFunctionAlways; + st.stencilFailureOperation = st.depthFailureOperation = MTLStencilOperationKeep; st.depthStencilPassOperation = MTLStencilOperationReplace; + st.readMask = 0xff; st.writeMask = sm; d.frontFaceStencil = d.backFaceStencil = st; + } + return cache[dw][sm] = [g_dev newDepthStencilStateWithDescriptor:d]; +} +static int clr_scissor(MTLScissorRect *sc) { + NSUInteger tw = g_pass.tw, th = g_pass.th; *sc = (MTLScissorRect){0, 0, tw, th}; + if (!R.has_sc) return tw && th; + long x0 = R.sc.x < 0 ? 0 : R.sc.x, y0 = R.sc.y < 0 ? 0 : R.sc.y, x1 = (long)R.sc.x + R.sc.w, y1 = (long)R.sc.y + R.sc.h; + if (x1 > (long)tw) x1 = (long)tw; if (y1 > (long)th) y1 = (long)th; + if (x1 <= x0 || y1 <= y0) return 0; + *sc = (MTLScissorRect){(NSUInteger)x0, (NSUInteger)y0, (NSUInteger)(x1 - x0), (NSUInteger)(y1 - y0)}; return 1; +} +static void clr_tri(id en, id ps, id ds, MTLScissorRect sc, const uint32_t val[4], float z, uint32_t sref) { + id b; size_t o = arena_alloc(32, &b); uint8_t *p = (uint8_t *)b.contents + o; memcpy(p, val, 16); memset(p + 16, 0, 16); memcpy(p + 16, &z, 4); + legacy_bind_buffer(en, 0, 0, b, o + 16); legacy_bind_buffer(en, 1, 0, b, o); + enc_r_pso(en, ps); g_enc_pso = nil; enc_r_dss(en, ds); g_enc_dss = nil; + if (g_enc_vis) { enc_r_visibility(en, MTLVisibilityResultModeDisabled, 0); g_enc_vis = 0; } + enc_r_viewport(en, (MTLViewport){0, 0, (double)g_pass.tw, (double)g_pass.th, 0, 1}); enc_r_scissor(en, sc); + enc_r_cull(en, MTLCullModeNone); enc_r_fill(en, MTLTriangleFillModeFill); enc_r_depth_bias(en, 0, 0, 0); enc_r_depth_clip(en, YES); enc_r_stencil_ref(en, sref, sref); g_ed.ok = 0; + enc_r_draw(en, MTLPrimitiveTypeTriangle, 0, 3); +} +static void clr_note(int slot, int depth, int stencil) { + if (slot >= 0 && R.rt.color[slot]) { zskip_del(R.rt.color[slot]->serial); g_zskip_cand_ok[slot] = 0; } + for (int i = 0; i < g_nsd; i++) { sd_att *t = &g_sd[i]; + if ((t->depth == 0 && slot >= 0 && t->idx == slot) || (t->depth == 1 && depth) || (t->depth == 2 && stencil)) { t->wrote = 1; t->wwhy = 2; } } +} +static void clear_color_tri(const p_clear_color *c) { + int slot = c->index, mask = c->mask & 0xf; + if (!mask || slot < 0 || slot >= 8 || slot >= R.rt.n || !R.rt.color[slot]) return; + NSUInteger slices = !g_slice_replaying ? slice_target_count() : 1; + if (slices > 1) { + rp_end(); + typeof(R) saved = R; g_slice_replaying = 1; + for (uint32_t slice = 0; slice < slices; slice++) { + R = saved; slice_select(slice); + clear_color_tri(c); + // Pending full clears still cover every slice even when this + // masked triangle has an empty scissor or cannot draw. + rp_flush(); + } + R = saved; memset(R.clear_c, 0, sizeof R.clear_c); R.clear_d = R.clear_s = 0; + g_slice_replaying = 0; g_selected_slice = 0; args_dirty_all(); g_pso_dirty = 1; + return; + } + id en = rp_begin(); + if (en && !g_pass.cfmt[slot] && g_enc_mode) { rp_end(); en = rp_begin(); } + if (!en) return; + int at = slot; + if (!g_pass.cfmt[at] && rt_dup_slot(&R.rt, slot)) + for (int j = 0; j < slot; j++) { const tex_t *a = R.rt.color[j], *b = R.rt.color[slot]; if (!a || a->serial != b->serial || !g_pass.cfmt[j]) continue; + const view_t *va = &R.rt.cview[j], *vb = &R.rt.cview[slot]; int ha = R.rt.has_cview[j], hb = R.rt.has_cview[slot]; + if ((ha && va->set_levels ? va->base_level : 0) == (hb && vb->set_levels ? vb->base_level : 0) && (ha && va->set_layers ? va->min_layer : 0) == (hb && vb->set_layers ? vb->min_layer : 0)) { at = j; break; } } + if (!g_pass.cfmt[at]) { PD("c %llu color-clear triangle skipped: slot %d is not in this pass", g_stat_pass, slot); return; } + MTLScissorRect sc; if (!clr_scissor(&sc)) { PD("c %llu color-clear triangle skipped: empty scissor slot %d", g_stat_pass, slot); return; } + int kind = pf_int_kind((MTLPixelFormat)g_pass.cfmt[at]); + clr_key k; memset(&k, 0, sizeof k); k.slot = at; k.kind = kind; k.mask = mask; memcpy(k.cfmt, g_pass.cfmt, sizeof k.cfmt); k.dfmt = g_pass.dfmt; k.sfmt = g_pass.sfmt; k.samples = g_pass.samples; + id ps = clr_pso(&k); id ds = clr_dss(0, 0); if (!ps || !ds) return; + uint32_t v[4]; + for (int i = 0; i < 4; i++) { float f = c->rgba[i]; int32_t si; + if (kind == 1) v[i] = f > 0 ? (f < 4294967040.f ? (uint32_t)f : 0xffffffffu) : 0; + else if (kind == 2) { si = !(f == f) ? 0 : f >= 2147483520.f ? INT32_MAX : f <= -2147483648.f ? INT32_MIN : (int32_t)f; memcpy(&v[i], &si, 4); } + else memcpy(&v[i], &f, 4); } + clr_tri(en, ps, ds, sc, v, 0, 0); clr_note(at, 0, 0); + static uint64_t n; if (!n++) MLOG("mid-pass / partial-mask / partial-scissor color clear: drawn as a full-pass triangle (first case pass %llu slot %d mask %#x scissor %lu,%lu %lux%lu)", g_stat_pass, at, mask, + (unsigned long)sc.x, (unsigned long)sc.y, (unsigned long)sc.width, (unsigned long)sc.height); + PD("c %llu color clear drawn as triangle slot%d ser=%llu mask=%#x scissor=%lu,%lu,%lu,%lu", g_stat_pass, at, (unsigned long long)R.rt.color[at]->serial, mask, + (unsigned long)sc.x, (unsigned long)sc.y, (unsigned long)sc.width, (unsigned long)sc.height); +} +static void clear_ds_tri(const p_clear_ds *c) { + unsigned want_s = (unsigned)c->stencil_mask & 0xff; + if (!c->depth_mask && !want_s) return; + id en = rp_begin(); + if (en && g_enc_mode && ((c->depth_mask && !g_pass.hd) || (want_s && !g_pass.hs))) { rp_end(); en = rp_begin(); } + if (!en) return; + int dw = c->depth_mask && g_pass.hd; unsigned sm = g_pass.hs ? want_s : 0; + if (!dw && !sm) return; + MTLScissorRect sc; if (!clr_scissor(&sc)) { PD("c %llu depth-stencil clear triangle skipped: empty scissor", g_stat_pass); return; } + clr_key k; memset(&k, 0, sizeof k); k.slot = -1; memcpy(k.cfmt, g_pass.cfmt, sizeof k.cfmt); k.dfmt = g_pass.dfmt; k.sfmt = g_pass.sfmt; k.samples = g_pass.samples; + id ps = clr_pso(&k); id ds = clr_dss(dw, sm); if (!ps || !ds) return; + float z = c->depth > 0 ? (c->depth < 1 ? c->depth : 1) : 0; + const uint32_t v[4] = {0}; clr_tri(en, ps, ds, sc, v, z, (uint32_t)c->stencil & 0xff); clr_note(-1, dw, sm != 0); + static uint64_t n; if (!n++) MLOG("mid-pass scissored depth-stencil clear: drawn as a full-pass triangle (first case pass %llu depth %d stencil mask %#x scissor %lu,%lu %lux%lu)", g_stat_pass, dw, sm, + (unsigned long)sc.x, (unsigned long)sc.y, (unsigned long)sc.width, (unsigned long)sc.height); + PD("c %llu depth-stencil clear drawn as triangle ser=%llu depth=%g(%d) stencil=%d mask=%#x scissor=%lu,%lu,%lu,%lu", g_stat_pass, + R.rt.depth ? (unsigned long long)R.rt.depth->serial : 0ull, z, dw, c->stencil, sm, (unsigned long)sc.x, (unsigned long)sc.y, (unsigned long)sc.width, (unsigned long)sc.height); +} + +void mtl_exec(const uint8_t *p) { + static int inited; if (!inited) { inited = 1; state_reset(); } + static int dump_all = -1; if (dump_all < 0) dump_all = getenv("DM_NVN_PASSDUMP_ALL") != NULL; + int trace = dump_all && passdump(); // Normal mode does not check the frame range per operation; the full command position is only used for the designated diagnostic frames + for (;;) { + op_hdr h; memcpy(&h, p, sizeof h); + g_exec_cur = p; // Scan start point of the store lookahead: the operation that ends the render pass (inclusive) + if (p == g_defer_pend) { for (int gi = 0; gi < 4; gi++) if (g_defer_slots[gi].active && g_defer_slots[gi].p_end_op == p) g_defer_slots[gi].in_p = 0; g_defer_pend = NULL; } + sdx_seq(p); + if (h.op == OP_SET_RT) rt_toggle_poll(); + if ((g_frz_on || g_frz_state || g_dm_rt_ab == 1) && frz_op(h.op, p + 8)) { p += h.size; continue; } + if (trace) PD("o cmd=%p pass=%llu op=%u size=%u", p, g_stat_pass, (unsigned)h.op, (unsigned)h.size); + // Diagnostic DM_NVN_ALLOCPROF=1: growth of the GPU allocated size is charged to the previous operation's type (rough: allocations on other threads are counted too); the memory report line appends the top 4 + if (g_allocprof < 0) g_allocprof = getenv("DM_NVN_ALLOCPROF") != NULL; + if (g_allocprof) { static uint64_t last; static int prev = -1; uint64_t now = g_dev.currentAllocatedSize; if (prev >= 0 && prev < 64 && now > last) g_ap_bytes[prev] += now - last; last = now; prev = h.op; } + if (g_hoist_n && hoist_take(p)) { if (g_fz.active && p == g_fz.rt2_op) { g_fz.phase = 2; g_rt_merged = 0; PD("F %llu 合并通道进入后一个通道的绘制", g_stat_pass); } p += h.size; continue; } // A pure-clear render pass already executed early / the start of a later pass already merged in // Entering the later pass: the same-target continuation of the earlier pass ends here; the later pass reading the earlier pass's attachment is proven to be same-pixel by fz_p2_draw, and is no longer split by the continuation rule + const void *a = p + 8; + switch (h.op) { + case OP_END: g_exec_cur = NULL; + if (g_hoist_n && !g_ret_n && !g_nrest) { + if (g_fz.active) fz_unskip(); + if (g_hoist_n) { MLOG("【红】纯清除通道前提:提交结束时还有 %d 条已前提的操作没走到,清空(不应发生)", g_hoist_n); g_hoist_n = 0; } + } + return; // An end-of-pass triggered outside a list (present / CPU readback etc.) must not scan expired command memory: a null start point means store everything + case OP_BEGIN: break; + case OP_JUMP: { const uint8_t *t; memcpy(&t, a, 8); p = t; continue; } + case OP_CALL: { uint64_t t; memcpy(&t, a, 8); + if (g_ret_n < 16) g_ret[g_ret_n] = p + h.size; g_ret_n++; // Return stack for lookahead: after a sub-list ends, continue looking at the caller + mtl_exec((const uint8_t *)t); g_ret_n--; break; } + case OP_SIGNAL_EVENT: { volatile uint32_t *val; uint32_t m, v; memcpy(&val, a, 8); memcpy(&m, (const uint8_t *)a + 8, 4); memcpy(&v, (const uint8_t *)a + 12, 4); + PD("Y signal_event cmd=%p dst=%p mode=%u value=%u", p, val, m, v); mtl_sub_event(val, m, v); break; } + case OP_REPORT_COUNTER: { uint64_t t, ad; memcpy(&t, a, 8); memcpy(&ad, (const uint8_t *)a + 8, 8); g_cnt_report[t & 31]++; + PD("Q report type=%llu cmd=%p dst=%#llx", (unsigned long long)t, p, (unsigned long long)ad); + if (DM_GAME_RENDER_COND) rc_note(ad, t == 0 || t == 0x10 ? 1 : 2); + if (t == NVN_COUNTER_TYPE_SAMPLES_PASSED) { post((post_op){3, NULL, 0, 0, ad, (uint32_t)t, vis_snapshot()}); g_vis_cur = -1; } + else mtl_sub_counter(ad, (uint32_t)t); + break; } + case OP_RENDER_COND: { const p_render_cond *rc = a; + if (rc->mode >= 2) { + int would_skip = rc->mode == 3; + rc_override_note(rc, would_skip, 1, 0, 0); + g_rc_skip = would_skip && !rc_always_draw(); + PD("E %llu render enable %d", g_stat_pass, rc->mode == 2); + } else { + uint64_t x = 0, y = 0; + int known = rc_value(rc->addr, &x) & rc_value(rc->addr + 16, &y); + int would_skip = known && (rc->mode ? x == y : x != y); + rc_override_note(rc, would_skip, known, x, y); + g_rc_skip = would_skip && !rc_always_draw(); + PD("E %llu conditional render %s addr=%#llx values %llu / %llu%s%s", g_stat_pass, + rc->mode ? "draw if not equal" : "draw if equal", (unsigned long long)rc->addr, + (unsigned long long)x, (unsigned long long)y, known ? "" : " (unknown, drawn)", + g_rc_skip ? " -> not drawn" : ""); + } + break; } + case OP_RESET_COUNTER: { uint64_t t; memcpy(&t, a, 8); g_cnt_reset[t & 31]++; PD("Q reset type=%llu cmd=%p", (unsigned long long)t, p); + if (t == NVN_COUNTER_TYPE_SAMPLES_PASSED) { +#ifdef NVN_RT_RANGE_TEST + rr_reset(); +#endif + g_vis_on = 1; g_vis_counting = 1; vis_clear(); } + break; } + case OP_CLEAR_BUF: { const p_clear_buf *cb = a; mtl_sub_fill(cb->addr, cb->size, cb->value); break; } + case OP_COPY_B2B: { uint64_t s_, d, n; memcpy(&s_, a, 8); memcpy(&d, (const uint8_t *)a + 8, 8); memcpy(&n, (const uint8_t *)a + 16, 8); + PD("Y copy_b2b cmd=%p src=%#llx dst=%#llx bytes=%llu", p, (unsigned long long)s_, (unsigned long long)d, (unsigned long long)n); mtl_sub_copy(s_, d, n); break; } + case OP_UPDATE_UB: { uint64_t d, n; memcpy(&d, a, 8); memcpy(&n, (const uint8_t *)a + 8, 8); + PD("Y update_ub cmd=%p src=%#llx dst=%#llx bytes=%llu f=%g,%g,%g,%g", p, (unsigned long long)((const uint8_t *)a + 16), (unsigned long long)d, (unsigned long long)n, + n >= 4 ? ((const float *)((const uint8_t *)a + 16))[0] : 0.f, n >= 8 ? ((const float *)((const uint8_t *)a + 16))[1] : 0.f, n >= 12 ? ((const float *)((const uint8_t *)a + 16))[2] : 0.f, n >= 16 ? ((const float *)((const uint8_t *)a + 16))[3] : 0.f); mtl_sub_update((const uint8_t *)a + 16, d, n); break; } + case OP_SET_RT: { + const p_set_rt *r = a; + if (defer_begin_q(p)) break; + int pend = R.clear_d || R.clear_s; for (int i = 0; i < 8; i++) pend |= R.clear_c[i]; + if (g_enc && !pend && R.has_rt && tbdr_on() && rt_range_same(&R.rt,r)) { + g_rt_merged = g_rt_range_active = g_rt_range_pending = 1; R.rt = *r; break; } + if (g_enc && g_fz.active && g_fz.phase == 1 && fz_reuse_op(p) && !pend && g_enc_mode != 1 && R.has_rt && tbdr_on() && rt_same(&g_fz.rt1, r)) { + g_rt_merged = 1; if (!g_rt_merge_n++) MLOG("同目标续用:首例 通道 %llu", g_stat_pass); PD("M %llu 同目标续用通道", g_stat_pass); break; } + // Do not continue a render pass opened only for a clear; one with fewer depth / stencil attachments can be continued (check the planes before drawing) + if (g_enc && g_enc_mode != 1 && !pend && R.has_rt && tbdr_on() && rt_same(&R.rt, r)) { g_rt_merged = 1; if (!g_rt_merge_n++) MLOG("同目标续用:首例 通道 %llu", g_stat_pass); PD("M %llu 同目标续用通道", g_stat_pass); R.rt = *r; break; } + rp_flush(); sub_cut(); R.rt = *r; R.has_rt = 1; break; + } + case OP_CLEAR_COLOR: { + const p_clear_color *c = a; + if (g_rc_skip) { PD("c %llu color clear skipped by conditional rendering slot%d", g_stat_pass, c->index); break; } + // A scissored clear to 0 outside a pass with all channels (SMBW clears pyramid levels as sub-rectangles of 480×272 and other targets): copy from the zero buffer into the scissor rectangle in the replay stream + if (!g_enc && c->mask == 0xf && !c->rgba[0] && !c->rgba[1] && !c->rgba[2] && !c->rgba[3] && R.has_sc && c->index >= 0 && c->index < R.rt.n && R.rt.color[c->index] + && !scissor_full(R.rt.color[c->index], &R.rt.cview[c->index], R.rt.has_cview[c->index]) && clear_rect_zero(c->index)) break; + if (g_rt_merged) rp_end(); // A clear arrived in a continued render pass: end it here, and the clear is attached to the start of the next render pass as usual + if (c->index < 0 || c->index >= R.rt.n || !R.rt.color[c->index]) { RED_ONCE(5, "清色目标 %d 无效", c->index); break; } + if (g_enc || c->mask != 0xf || !scissor_full(R.rt.color[c->index], &R.rt.cview[c->index], R.rt.has_cview[c->index])) { + clear_color_tri(c); + break; } + R.clear_c[c->index] = 1; R.cc[c->index] = MTLClearColorMake(c->rgba[0], c->rgba[1], c->rgba[2], c->rgba[3]); + break; + } + case OP_CLEAR_DS: { + const p_clear_ds *c = a; + if (g_rc_skip) { PD("c %llu depth-stencil clear skipped by conditional rendering", g_stat_pass); break; } + if (g_rt_merged) rp_end(); + // Depth / stencil clear in the middle of a pass: end the current pass and attach the clear to the start of the next one (color attachments load as usual, as a continued pass does above when a clear arrives). It used to be skipped, and later draws depth-tested against stale depth. + // A partial clear cannot be represented by a Metal load action, so keep the pass open and draw it through clear_ds_tri. + if (g_enc) { + if (R.rt.depth && !scissor_full(R.rt.depth, &R.rt.dview, R.rt.has_dview)) { clear_ds_tri(c); break; } + { static int once; if (!once++) MLOG("通道中途的深度模板清除:结束本通道,清除挂到下一个通道开头(首例 通道 %llu)", g_stat_pass); } + rp_end(); + } + if (c->depth_mask) { R.clear_d = 1; R.cd = c->depth; } + if (c->stencil_mask) { R.clear_s = 1; R.cs = (uint32_t)c->stencil; } + break; + } + case OP_DRAW: { if (g_rc_skip) break; p_draw tmp; const p_draw *d = draw_rec(a, h.size, &tmp); + g_exec_next = p + h.size; draw(d, NULL); g_exec_next = NULL; break; } // Lookahead start point; compact direct draws omit the indirect fields + case OP_DRAW_IDX: { if (g_rc_skip) break; p_draw_idx tmp; const p_draw_idx *d = draw_idx_rec(a, h.size, &tmp); + g_exec_next = p + h.size; draw(NULL, d); g_exec_next = NULL; break; } + // Do not mark dirty if the state did not change (the game often re-sends the same program / state block / vertex format before each draw; the dirty flag only decides whether to re-fetch the pipeline / depth-stencil object, and equal values mean the same object) + case OP_SCISSOR: case OP_BIND_PROGRAM: case OP_BIND_STATE: case OP_BIND_VATTR: case OP_BIND_VSTREAM: case OP_BIND_VBUF: + case OP_BIND_UBO: case OP_BIND_SSBO: case OP_BIND_TEX: case OP_BIND_TEX1: case OP_BIND_SAMP: case OP_BIND_IMG: case OP_STENCIL: + case OP_POLY_OFFSET: case OP_VIEWPORTS: case OP_DEPTH_RANGE: case OP_DEPTH_CLAMP: case OP_BLEND_COLOR: case OP_COPY_STRIDE: + state_op(h.op, a); break; + case OP_TF_BIND: { const p_tf_bind *b = a; PD("Y tf bind %d addr=%#llx size=%llu", b->index, (unsigned long long)b->addr, (unsigned long long)b->size); state_op(h.op, a); break; } + case OP_TF_STATE: { const p_tf_state *b = a; static const char *const what[4] = {"begin", "end", "pause", "resume"}; PD("Y tf %s written %llu/%llu/%llu/%llu", what[b->what & 3], + (unsigned long long)S.tf_off[0], (unsigned long long)S.tf_off[1], (unsigned long long)S.tf_off[2], (unsigned long long)S.tf_off[3]); state_op(h.op, a); break; } + case OP_RAST_DISCARD: PD("Y rasterizer discard %d", ((const p_rast_discard *)a)->on); state_op(h.op, a); break; + case OP_COPY_B2T: copy_b2t(a); break; + case OP_COPY_T2B: copy_t2b(a); break; + case OP_COPY_T2T: copy_t2t(a); break; + case OP_CLEAR_TEX: clear_tex(a); break; + case OP_DISPATCH: dispatch(a); break; + case OP_BARRIER: { + int32_t bits; memcpy(&bits, a, 4); + if (bits) { + g_game_barrier = 1; + rp_end(); + if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + args_dirty_all(); + if (!g_explicit_barriers++) MLOG("NVN Barrier %#x: ends active pass and orders later access through the alias fence", bits); + } + break; + } + case OP_SET_TEX_POOL: case OP_SET_SAMP_POOL: break; + default: break; + } + p += h.size; + } +} + +// ---------- Present: draw the swapchain texture to the CAMetalLayer; optional screenshot ---------- +static id g_blit_pso; static MTLPixelFormat g_blit_fmt; +static id g_blit_samp; +static uint64_t g_present_n; +static _Atomic uint64_t g_displayed_n; +uint64_t mtl_present_count(void) { return atomic_load(&g_displayed_n); } // Actual drawable presentations, not attempts to queue a frame. +void mtl_res_snapshot(mtl_res_count *out) { + memset(out, 0, sizeof *out); + out->pso = g_pso.n; + out->cps = g_cps.n; + out->dss = g_dss.n; + out->shaders = (uint32_t)(g_shcache ? g_shcache.count : 0); + os_unfair_lock_lock(&g_c1_lock); + out->c1_bufs = (uint32_t)(g_c1_banks.count + g_c1_exclusive); out->c1_alloc = g_c1_alloc_bytes; out->c1_data = g_c1_data_bytes; + os_unfair_lock_unlock(&g_c1_lock); + pthread_mutex_lock(&g_smu); + out->live_tex = g_by_serial.n; + pthread_mutex_unlock(&g_smu); + pthread_mutex_lock(&g_mu); + out->pools = (uint32_t)g_npool; + out->pool_adds = g_pool_adds; + pthread_mutex_unlock(&g_mu); + pthread_mutex_lock(&g_rmu); + out->view_slots = (uint32_t)g_ntreg; + for (int i = 0; i < g_ntreg; i++) if (g_treg[i].view) out->view_nonnull++; + for (int i = 0; i < g_nsreg; i++) if (g_sview[i]) out->sview_nonnull++; + pthread_mutex_unlock(&g_rmu); +} +// Resource counts (read by long-run stability L3): the host calls it per game tick / event (every 250 ticks, loading screen dismissed, exit), not sampled by wall clock / present count. +// Line format: [nvn] 资源计数 拍 <拍号> <触发>:<名> <数值> … ([nvn] resource counts tick : ...) (names contain no spaces, single-space separated); new fields are appended to the end of this line +void mtl_res_counts(uint32_t tick, const char *why) { + mtl_res_count c; + mtl_res_snapshot(&c); + int dl, du; uint64_t da; nvn_dirty_slot_stats(&dl, &du, &da); + LOG("[nvn] 资源计数 拍 %u %s:渲染管线 %u 计算管线 %u 着色器 %u 深度模板 %u view非空 %u view槽 %u 存活贴图 %u 内存池存活 %u 内存池累计 %llu 采样器view非空 %u 脏页区间存活 %d 脏页区间累计 %llu c1缓冲 %u c1字节 %llu c1实际 %llu", + tick, why, c.pso, c.cps, c.shaders, c.dss, c.view_nonnull, c.view_slots, c.live_tex, + c.pools, (unsigned long long)c.pool_adds, c.sview_nonnull, dl, (unsigned long long)da, c.c1_bufs, (unsigned long long)c.c1_alloc, (unsigned long long)c.c1_data); +} +// Screenshot on request (used by DM_NVN_SHOT_UI): dm_gfx_shot_at(k, path) = take a screenshot at the kth frame after the current present frame; due requests are checked at every frame's present +#define SHOTQ_MAX 8 +static struct { uint64_t frame; char path[1024]; } g_shotq[SHOTQ_MAX]; static int g_nshotq; +static pthread_mutex_t g_shotq_mu = PTHREAD_MUTEX_INITIALIZER; +static int present_encode(id cb, id src, id dst, const int crop[4]); +static int present_encode_m4(id cb, id src, id dst, const int crop[4], int uv_i); +// Game picture needed by the whole-window screenshot (front end window_shot; no longer does drawViewHierarchy on a Metal layer with the display link attached): at the next present, the present thread draws the same frame +// with the same encoding as the present (present_encode) into an offscreen texture of the same size as the swapchain, and after the GPU finishes hands out a CGImage in the completion callback (the callback does not block the present). Guarded by g_shotq_mu +static void (^g_grab_cb)(CGImageRef img); +void mtl_grab_next_present(void (^cb)(CGImageRef img)) { pthread_mutex_lock(&g_shotq_mu); g_grab_cb = [cb copy]; pthread_mutex_unlock(&g_shotq_mu); } +static void grab_present(id src, const int crop[4], CGSize sz) { + pthread_mutex_lock(&g_shotq_mu); void (^cb)(CGImageRef) = g_grab_cb; g_grab_cb = nil; pthread_mutex_unlock(&g_shotq_mu); + if (!cb) return; + size_t w = (size_t)sz.width, h = (size_t)sz.height, row = w * 4; + MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm width:w height:h mipmapped:NO]; + d.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead; d.storageMode = MTLStorageModePrivate; + id out = w && h ? [g_dev newTextureWithDescriptor:d] : nil; id buf = out ? [g_dev newBufferWithLength:row * h options:MTLResourceStorageModeShared] : nil; + id cb2 = buf ? q_cb() : nil; + if (!cb2 || !present_encode(cb2, src, out, crop)) { MLOG("【红】整窗截图:游戏画面离屏编码失败(%zux%zu)", w, h); cb(NULL); return; } + id b = [cb2 blitCommandEncoder]; + [b copyFromTexture:out sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(w, h, 1) toBuffer:buf destinationOffset:0 destinationBytesPerRow:row destinationBytesPerImage:row * h]; + [b endEncoding]; + [cb2 addCompletedHandler:^(id c) { (void)c; + CGColorSpaceRef cs = CGColorSpaceCreateWithName(kCGColorSpaceSRGB); + CGContextRef ctx = CGBitmapContextCreate(buf.contents, w, h, 8, row, cs, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little); + CGImageRef img = ctx ? CGBitmapContextCreateImage(ctx) : NULL; cb(img); + CGImageRelease(img); CGContextRelease(ctx); CGColorSpaceRelease(cs); }]; + [cb2 commit]; +} +void dm_gfx_shot_at(long k, const char *path) { + pthread_mutex_lock(&g_shotq_mu); + if (g_nshotq < SHOTQ_MAX) { g_shotq[g_nshotq].frame = g_present_n + (uint64_t)(k > 0 ? k : 0); strlcpy(g_shotq[g_nshotq].path, path, sizeof g_shotq[0].path); g_nshotq++; } + else MLOG("【红】截图请求超过 %d 个,丢弃 %s", SHOTQ_MAX, path); + pthread_mutex_unlock(&g_shotq_mu); +} +// DM_NVN_SHOT_OUT=WIDTHxHEIGHT (for picture quality metrics): the present-frame screenshot instead captures the result "upscaled to this resolution by present_encode", i.e. the picture the player sees on screen; screen=0 (texture dump) is unaffected +static void shot(id src, const char *path, int screen, const int crop[4]) { + static int out_w = -1, out_h; + if (out_w < 0) { + out_w = 0; + const char *e = getenv("DM_NVN_SHOT_OUT"); int w = 0, h = 0; char sep = 0, extra = 0; + if (e && sscanf(e, "%d%c%d%c", &w, &sep, &h, &extra) == 3 && sep == 'x' && w > 0 && h > 0) { out_w = w; out_h = h; } + } + id read = src; + if (out_w && screen) { + MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm width:out_w height:out_h mipmapped:NO]; + d.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead; d.storageMode = MTLStorageModePrivate; + id out = [g_dev newTextureWithDescriptor:d]; + if (!out) { MLOG("【红】截图输出贴图建不出 %dx%d", out_w, out_h); return; } + id cb = q_cb(); + if (!present_encode(cb, src, out, crop)) return; + [cb commit]; [cb waitUntilCompleted]; + read = out; + } + if (read.pixelFormat != MTLPixelFormatRGBA8Unorm && read.pixelFormat != MTLPixelFormatRGBA8Unorm_sRGB && + read.pixelFormat != MTLPixelFormatBGRA8Unorm && read.pixelFormat != MTLPixelFormatBGRA8Unorm_sRGB) { MLOG("【红】截图不支持格式 %lu", (unsigned long)read.pixelFormat); return; } + size_t w = read.width, h = read.height, row = w * 4; + id buf = [g_dev newBufferWithLength:row * h options:MTLResourceStorageModeShared]; + id cb = q_cb(); + id b = [cb blitCommandEncoder]; + [b copyFromTexture:read sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(w, h, 1) + toBuffer:buf destinationOffset:0 destinationBytesPerRow:row destinationBytesPerImage:row * h]; + [b endEncoding]; [cb commit]; [cb waitUntilCompleted]; + if (screen && read == src && atomic_load_explicit(&g_origin_ll, memory_order_relaxed)) { // lower-left origin: the window image is stored bottom-up (see g_origin_ll) + uint8_t *p = buf.contents, *tmp = malloc(row); + if (tmp) { for (size_t y = 0; y < h / 2; y++) { memcpy(tmp, p + y * row, row); memcpy(p + y * row, p + (h - 1 - y) * row, row); memcpy(p + (h - 1 - y) * row, tmp, row); } free(tmp); } } + int bgra = read.pixelFormat == MTLPixelFormatBGRA8Unorm || read.pixelFormat == MTLPixelFormatBGRA8Unorm_sRGB; + CGColorSpaceRef cs = CGColorSpaceCreateWithName(kCGColorSpaceSRGB); + CGBitmapInfo bi = bgra ? (kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little) : (CGBitmapInfo)kCGImageAlphaNoneSkipLast; + CGContextRef ctx = CGBitmapContextCreate(buf.contents, w, h, 8, row, cs, bi); + CGImageRef img = ctx ? CGBitmapContextCreateImage(ctx) : NULL; + CFURLRef url = CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8 *)path, strlen(path), false); + CGImageDestinationRef dst = url ? CGImageDestinationCreateWithURL(url, (__bridge CFStringRef)UTTypePNG.identifier, 1, NULL) : NULL; + // If any step yields null (directory does not exist / not writable, bitmap context cannot be created), report a failure and return; previously it went straight to CFRelease(NULL) and crashed in CFRelease.cold (hit during a 1952-wide TEXDUMP) + int ok = 0; + if (!ctx || !img || !dst) MLOG("【红】截图 %s 失败:%s", path, !ctx ? "位图上下文建不出" : !img ? "取图失败" : "写不出(目录不存在或不可写)"); + else { CGImageDestinationAddImage(dst, img, NULL); ok = CGImageDestinationFinalize(dst); } + if (dst) CFRelease(dst); if (url) CFRelease(url); CGImageRelease(img); CGContextRelease(ctx); CGColorSpaceRelease(cs); + dm_shot_written(path, ok); // Let the host write the 「冻结拍」(frozen tick) line first, then write the 「成功」(success) line: the screenshot script stops the host as soon as it sees 「成功」(success), and with the order reversed the log would lack the 「冻结拍」(frozen tick) line under full load (false failure in the gate check's require_log) + MLOG("截图 %zux%zu 格式 %lu → %s %s", w, h, (unsigned long)read.pixelFormat, path, ok ? "成功" : "失败"); + if (g_ruler_truth && getenv("DM_NVN_RULER") && !strcmp(getenv("DM_NVN_RULER"), "1") && ok) { static int shots; if (++shots == 2) g_ruler_pending = 1; } + if (g_defer_ab >= 0 && getenv("DM_NVN_DEFER_AB") && !strcmp(getenv("DM_NVN_DEFER_AB"), "1") && ok) { static int shots; if (++shots == 2) { g_defer_ab_on = 1; MLOG("DM_NVN_DEFER_AB:第二张截图写盘完成,打开推迟合并通道"); } } + if (g_udc_ab >= 0 && getenv("DM_NVN_UDC_AB") && !strcmp(getenv("DM_NVN_UDC_AB"), "1") && ok) { + static int shots; + if (++shots == 2) { g_udc_ab_on = 1; MLOG("DM_NVN_UDC_AB:第二张截图写盘完成,打开 DM_NVN_UNWRITTEN_DC"); } + } +} +// ---------- DM_NVN_TEXDUMP=frame number:path (%d in the path is replaced with "index*100+mip level"): save each color render target used by that frame as an image ---------- +// Diagnostic: before the first render pass of the next frame is opened, wait for the GPU to finish, then draw each one (the mip level used by that frame) by full-screen sampling into RGBA8 and save as PNG (float formats are clamped to [0,1]) +static long g_td_frame = -2; static const char *g_td_path; static NSMutableArray *g_td_list; static int g_td_done; +static void td_dump(void); +// DM_NVN_TEXDUMP=size=WxH:path instead picks the frame by content: the first frame that opens a render pass whose first color attachment level is WxH (for one-off work whose frame differs between runs) +static unsigned g_td_w, g_td_h; +void td_note(MTLRenderPassDescriptor *pd, NSUInteger nc) { + if (g_td_frame == -2) { const char *e = getenv("DM_NVN_TEXDUMP"), *c = e ? strchr(e, ':') : NULL; g_td_frame = c ? atol(e) : -1; g_td_path = c ? c + 1 : NULL; g_td_list = [NSMutableArray array]; + if (c && !strncmp(e, "size=", 5) && sscanf(e + 5, "%ux%u", &g_td_w, &g_td_h) == 2) g_td_frame = LONG_MAX; } + if (g_td_frame < 0 || g_td_done) return; + long f = (long)atomic_load(&g_cur_frame); + if (g_td_frame == LONG_MAX) { id t0 = nc ? pd.colorAttachments[0].texture : nil; NSUInteger lv = pd.colorAttachments[0].level; + if (t0 && MAX(t0.width >> lv, 1ul) == g_td_w && MAX(t0.height >> lv, 1ul) == g_td_h) { g_td_frame = f; MLOG("[texdump] 首个 %ux%u 渲染目标在帧 %ld:转储这一帧", g_td_w, g_td_h, f); } } + if (f > g_td_frame) { td_dump(); return; } + if (f != g_td_frame) return; + for (NSUInteger i = 0; i < nc && i < 8; i++) { + id t = pd.colorAttachments[i].texture; if (!t) continue; + uint64_t ser = i < (NSUInteger)R.rt.n && R.rt.color[i] ? R.rt.color[i]->serial : 0; + NSUInteger lv = pd.colorAttachments[i].level, sl = t.textureType == MTLTextureType2DArray ? pd.colorAttachments[i].slice : 0; + BOOL seen = NO; for (NSArray *e in g_td_list) if ([e[1] unsignedLongLongValue] == ser && [e[2] unsignedIntegerValue] == lv && [e[3] unsignedIntegerValue] == sl) seen = YES; + if (!seen) [g_td_list addObject:@[t, @(ser), @(lv), @(sl)]]; + } +} +static void td_dump(void) { + g_td_done = 1; + rp_end(); if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + mtl_finish_gpu(); + static id ps; static id sp; + if (!ps) { + static const char *src_msl = + "#include \nusing namespace metal;\n" + "struct V { float4 p [[position]]; float2 uv; };\n" + "vertex V vs(uint id [[vertex_id]]) { float2 uv = float2((id << 1) & 2, id & 2); V o; o.p = float4(uv * float2(2, -2) + float2(-1, 1), 0, 1); o.uv = uv; return o; }\n" + "fragment float4 fs(V in [[stage_in]], texture2d t [[texture(0)]], sampler s [[sampler(0)]]) { float4 c = t.sample(s, in.uv); return float4(c.rgb, 1); }\n"; + NSError *e = nil; id lib = [g_dev newLibraryWithSource:@(src_msl) options:nil error:&e]; + MTLRenderPipelineDescriptor *d = [MTLRenderPipelineDescriptor new]; + d.vertexFunction = [lib newFunctionWithName:@"vs"]; d.fragmentFunction = [lib newFunctionWithName:@"fs"]; + d.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA8Unorm; + ps = nvn_cached_render(g_dev, d, &e); + MTLSamplerDescriptor *sd = [MTLSamplerDescriptor new]; sp = [g_dev newSamplerStateWithDescriptor:sd]; + if (!ps) { MLOG("【红】TEXDUMP 管线失败 %s", e.localizedDescription.UTF8String); return; } + } + MLOG("[texdump] 帧 %ld 共 %lu 张颜色目标", g_td_frame, (unsigned long)g_td_list.count); + // DM_NVN_TEXRAW=index,...: save raw bytes by texture index (including 3D; file: %d in the path is replaced with the index, suffix .raw, first-line text header: width height depth format bytes-per-row) + for (const char *q = getenv("DM_NVN_TEXRAW"); q && *q; q = strchr(q, ',') ? strchr(q, ',') + 1 : q + strlen(q)) { + if (!strncmp(q, "info", 4)) { // DM_NVN_TEXRAW=info: the textures remembered by DM_NVN_TEXINFO + static char buf[256]; size_t o = 0; for (int i = 0; i < g_texinfo_n; i++) o += snprintf(buf + o, sizeof buf - o, "%s%llu", i ? "," : "", (unsigned long long)g_texinfo_ser[i]); + if (o) { static char env[300]; snprintf(env, sizeof env, "%s", buf); setenv("DM_NVN_TEXRAW", env, 1); q = getenv("DM_NVN_TEXRAW"); MLOG("[texraw] info → %s", q); } else continue; + } + uint64_t ser = (uint64_t)atoll(q); id o = tex_get(ser); + id t = [o isKindOfClass:[NvPkg class]] ? ((NvPkg *)o)->tex : o; + if (!t) { MLOG("[texraw] ser=%llu 不在序号表", ser); continue; } + int bw, bh; NSUInteger w = t.width, h = t.height, dz = t.depth; + size_t bpp = t.pixelFormat == MTLPixelFormatRGBA16Float ? 8 : t.pixelFormat == MTLPixelFormatRGBA32Float ? 16 : 4; (void)bw; (void)bh; + size_t row = w * bpp, img = row * h; + id buf = [g_dev newBufferWithLength:img * dz options:MTLResourceStorageModeShared]; + id cb = q_cb(); id b = [cb blitCommandEncoder]; + [b copyFromTexture:t sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(w, h, dz) toBuffer:buf destinationOffset:0 destinationBytesPerRow:row destinationBytesPerImage:img]; + [b endEncoding]; [cb commit]; [cb waitUntilCompleted]; + char path[1024]; snprintf(path, sizeof path, g_td_path, (int)ser); strlcat(path, ".raw", sizeof path); + FILE *f = fopen(path, "wb"); if (f) { fprintf(f, "%lu %lu %lu %lu %zu\n", (unsigned long)w, (unsigned long)h, (unsigned long)dz, (unsigned long)t.pixelFormat, row); fwrite(buf.contents, 1, img * dz, f); fclose(f); } + MLOG("[texraw] ser=%llu %lux%lux%lu 格式 %lu → %s", ser, (unsigned long)w, (unsigned long)h, (unsigned long)dz, (unsigned long)t.pixelFormat, path); + } + for (NSArray *e in g_td_list) { + id t = e[0]; uint64_t ser = [e[1] unsignedLongLongValue]; NSUInteger lv = [e[2] unsignedIntegerValue], sl = [e[3] unsignedIntegerValue]; + // 2D arrays are saved by the attached layer (file index ser*100 + layer*10 + level; layer 0 is named like 2D) + if (t.textureType != MTLTextureType2D && t.textureType != MTLTextureType2DArray) { MLOG("[texdump] ser=%llu 类型 %lu 跳过", ser, (unsigned long)t.textureType); continue; } + if (getenv("DM_NVN_SNAPRAW")) { // Also save the raw bytes (converting an HDR target to an 8-bit image truncates; first-line text header: width height format bytes-per-row) + NSUInteger rw = MAX(t.width >> lv, 1u), rh = MAX(t.height >> lv, 1u); + size_t bpp = t.pixelFormat == MTLPixelFormatRGBA16Float ? 8 : t.pixelFormat == MTLPixelFormatRGBA32Float ? 16 : t.pixelFormat == MTLPixelFormatR16Float || t.pixelFormat == MTLPixelFormatR16Unorm ? 2 : 4; + id rb = [g_dev newBufferWithLength:rw * rh * bpp options:MTLResourceStorageModeShared]; + id rc = q_cb(); id bb = [rc blitCommandEncoder]; + [bb copyFromTexture:t sourceSlice:0 sourceLevel:lv sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(rw, rh, 1) toBuffer:rb destinationOffset:0 destinationBytesPerRow:rw * bpp destinationBytesPerImage:rw * rh * bpp]; + [bb endEncoding]; [rc commit]; [rc waitUntilCompleted]; + char rp_[1024]; snprintf(rp_, sizeof rp_, g_td_path, (int)(ser * 100 + lv)); strlcat(rp_, ".raw", sizeof rp_); + FILE *f = fopen(rp_, "wb"); if (f) { fprintf(f, "%lu %lu %lu %zu\n", (unsigned long)rw, (unsigned long)rh, (unsigned long)t.pixelFormat, rw * bpp); fwrite(rb.contents, 1, rw * rh * bpp, f); fclose(f); } + } + id v = [t newTextureViewWithPixelFormat:t.pixelFormat textureType:MTLTextureType2D levels:NSMakeRange(lv, 1) slices:NSMakeRange(sl, 1)]; + NSUInteger w = MAX(t.width >> lv, 1u), h = MAX(t.height >> lv, 1u); + MTLTextureDescriptor *od = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm width:w height:h mipmapped:NO]; + od.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead; od.storageMode = MTLStorageModePrivate; + id o = [g_dev newTextureWithDescriptor:od]; + id cb = q_cb(); + MTLRenderPassDescriptor *rp = [MTLRenderPassDescriptor renderPassDescriptor]; + rp.colorAttachments[0].texture = o; rp.colorAttachments[0].loadAction = MTLLoadActionDontCare; rp.colorAttachments[0].storeAction = MTLStoreActionStore; + id en = [cb renderCommandEncoderWithDescriptor:rp]; + [en setRenderPipelineState:ps]; [en setFragmentTexture:v atIndex:0]; [en setFragmentSamplerState:sp atIndex:0]; + [en drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3]; [en endEncoding]; [cb commit]; [cb waitUntilCompleted]; + char path[1024]; snprintf(path, sizeof path, g_td_path, (int)(ser * 100 + sl * 10 + lv)); + MLOG("[texdump] ser=%llu lv=%lu 层 %lu 格式 %lu %lux%lu", ser, (unsigned long)lv, (unsigned long)sl, (unsigned long)t.pixelFormat, (unsigned long)w, (unsigned long)h); + shot(o, path, 0, NULL); + } + [g_td_list removeAllObjects]; +} +// DM_NVN_TEXSNAP=in-frame index,... (together with the frame number and path of DM_NVN_TEXDUMP): as soon as these render passes end, copy their color attachments and save them together with the end-of-frame dump +// (the same target is often cleared and reused within a frame, so the end-of-frame state cannot reflect the output of intermediate passes); file name index = in-frame index*100000 + texture index*100 + mip level +static int g_ts_idx[64], g_ts_n = -1, g_ts_on; static uint64_t g_ts_first, g_ts_curf = ~0ull; +static __strong id g_ts_att[8]; static uint64_t g_ts_ser[8]; static NSUInteger g_ts_lv[8]; static int g_ts_na; +void ts_note(MTLRenderPassDescriptor *pd, NSUInteger nc) { + if (g_ts_n < 0) { g_ts_n = 0; const char *e = getenv("DM_NVN_TEXSNAP"); + for (const char *q = e; q && *q && g_ts_n < 64; q = strchr(q, ',') ? strchr(q, ',') + 1 : q + strlen(q)) g_ts_idx[g_ts_n++] = atoi(q); } + g_ts_on = 0; if (!g_ts_n || g_td_frame < 0 || g_td_done) return; + uint64_t f = atomic_load(&g_cur_frame); if (f != g_ts_curf) { g_ts_curf = f; g_ts_first = g_stat_pass; } + if ((long)f != g_td_frame) return; + int idx = (int)(g_stat_pass - g_ts_first); for (int i = 0; i < g_ts_n; i++) if (g_ts_idx[i] == idx) g_ts_on = idx + 1; + g_ts_na = 0; if (!g_ts_on) return; + for (NSUInteger i = 0; i < nc && i < 8; i++) { id t = pd.colorAttachments[i].texture; if (!t) continue; + g_ts_att[g_ts_na] = t; g_ts_ser[g_ts_na] = R.rt.color[i] ? R.rt.color[i]->serial : 0; g_ts_lv[g_ts_na] = pd.colorAttachments[i].level; g_ts_na++; } +} +void ts_after(void) { + if (!g_ts_on) return; + id dcb = q_cb(); + for (int i = 0; i < g_ts_na; i++) { + id t = g_ts_att[i]; if (t.textureType != MTLTextureType2D && t.textureType != MTLTextureType2DArray) continue; // 2D arrays take slice 0 + MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:t.pixelFormat width:t.width height:t.height mipmapped:t.mipmapLevelCount > 1]; + d.usage = MTLTextureUsageShaderRead; d.storageMode = MTLStorageModePrivate; d.mipmapLevelCount = t.mipmapLevelCount; + id c = [g_dev newTextureWithDescriptor:d]; + id b = [dcb blitCommandEncoder]; + [b copyFromTexture:t sourceSlice:0 sourceLevel:g_ts_lv[i] toTexture:c destinationSlice:0 destinationLevel:g_ts_lv[i] sliceCount:1 levelCount:1]; [b endEncoding]; + [g_td_list addObject:@[c, @((uint64_t)(g_ts_on - 1) * 1000 + g_ts_ser[i]), @(g_ts_lv[i])]]; + } + g_ts_on = 0; + [dcb commit]; +} +// Ring of actual presentation times: present sequence number -> presentedTime (seconds; -1 = dropped by the system, not presented) +#define PRING 1024 +typedef struct { uint64_t n; double t; } pres_ent; +static pres_ent g_pres[PRING]; +// Sparse software enqueue-to-display probes. A single ticket joins callbacks; +// this is not physical input latency or the whole frame's GPU critical path. +@interface NvPresentProbe : NSObject { +@public os_unfair_lock lock; uint64_t ticket, frame; unsigned ready; + double queued, commit, scheduled, gpu_start, gpu_end, shown; +} +@end +@implementation NvPresentProbe @end +static void present_probe_note(NvPresentProbe *p, unsigned phase, double a, double b) { + if (!p) return; + os_unfair_lock_lock(&p->lock); + if (phase == 1) p->scheduled = a; + else if (phase == 2) { p->gpu_start = a; p->gpu_end = b; } + else p->shown = a; + p->ready |= phase; + if (p->ready == 7) { + p->ready |= 8; + MLOG("[呈现采样] ticket=%llu frame=%llu enqueue=%.6f commit=%.6f scheduled=%.6f gpu_start=%.6f gpu_end=%.6f displayed=%.6f queue_to_display_ms=%.3f", + (unsigned long long)p->ticket,(unsigned long long)p->frame,p->queued,p->commit,p->scheduled, + p->gpu_start,p->gpu_end,p->shown,p->shown > 0 ? (p->shown-p->queued)*1000 : -1); + } + os_unfair_lock_unlock(&p->lock); +} +// Get the actual presentation time of the nth present (returns 0 if not ready, -1 if dropped) +double mtl_presented_time(uint64_t n) { pres_ent e = g_pres[n % PRING]; return e.n == n ? e.t : 0; } +static _Atomic uint64_t g_drawable_ns; // Accumulated wall clock waiting for nextDrawable in present (timing instrumentation) +uint64_t mtl_drawable_ns_take(void) { return atomic_exchange(&g_drawable_ns, 0); } +// Maximum refresh rate (Hz) of the screen when there is a window; returns 0 without a window. Mac uses CoreGraphics display modes (thread-safe C interface; the present configuration is read on the game thread) +int mtl_display_hz(void) { + if (!front_metal_layer()) return 0; +#if TARGET_OS_OSX + CGDisplayModeRef m = CGDisplayCopyDisplayMode(CGMainDisplayID()); + double hz = m ? CGDisplayModeGetRefreshRate(m) : 0; if (m) CGDisplayModeRelease(m); + return (int)(hz + 0.5); +#else + Class c = NSClassFromString(@"UIScreen"); id s = c ? [c valueForKey:@"mainScreen"] : nil; + return s ? [[s valueForKey:@"maximumFramesPerSecond"] intValue] : 0; +#endif +} +static _Atomic uint64_t g_present_stall_ns; // For unit tests: an extra pause before each present on the present thread (simulating a drawable that stays unavailable) + +void mtl_present_test_stall(uint64_t ns) { g_present_stall_ns = ns; } +static void present_pipeline(MTLPixelFormat format) { + if (!g_blit_pso || g_blit_fmt != format) { + static const char *src_msl = + "#include \nusing namespace metal;\n" + "struct V { float4 p [[position]]; float2 uv; };\n" + "vertex V vs(uint id [[vertex_id]]) { float2 uv = float2((id << 1) & 2, id & 2); V o; o.p = float4(uv * float2(2, -2) + float2(-1, 1), 0, 1); o.uv = uv; return o; }\n" + "struct Crop { float4 rect; float4 edge; };\n" + "fragment float4 fs(V in [[stage_in]], texture2d t [[texture(0)]], sampler s [[sampler(0)]], constant Crop& c [[buffer(0)]]) { return t.sample(s, clamp(in.uv * c.rect.zw + c.rect.xy, c.edge.xy, c.edge.zw)); }\n"; + NSError *e = nil; + id lib = [g_dev newLibraryWithSource:@(src_msl) options:nil error:&e]; + if (!lib) MDIE("出图着色器编译失败 %s", e.localizedDescription.UTF8String); + MTLRenderPipelineDescriptor *pd = [MTLRenderPipelineDescriptor new]; + pd.vertexFunction = [lib newFunctionWithName:@"vs"]; pd.fragmentFunction = [lib newFunctionWithName:@"fs"]; + pd.colorAttachments[0].pixelFormat = format; + g_blit_pso = nvn_cached_render(g_dev, pd, &e); + if (!g_blit_pso) MDIE("出图管线失败 %s", e.localizedDescription.UTF8String); + g_blit_fmt = format; + MTLSamplerDescriptor *sd = [MTLSamplerDescriptor new]; sd.minFilter = sd.magFilter = MTLSamplerMinMagFilterLinear; sd.supportArgumentBuffers = YES; + g_blit_samp = [g_dev newSamplerStateWithDescriptor:sd]; + } +} +// Present samples only the valid source rectangle declared by NVN; kept separate from the raw swapchain readback screenshot. Edges are clamped to the texel centers of the valid area, so bilinear does not read the black texels outside. +// Present samples the raw bytes stored in the swapchain texture (the Switch's scan-out sends the stored bytes as they are): an sRGB source is replaced by a non-sRGB view of the same storage, +// otherwise sampling decodes to linear first, the BGRA8Unorm layer does not encode again, and the whole picture comes out dark. Other formats are returned as they are (Mario's swapchain is RGBA8 and does not come here). +// View cache of 4 entries: swapchain textures live long and are few; a view holds its parent, so an equal parent pointer means the same object. A replaced view is released after the submissions that used it complete (Metal 4 does not retain resources) +static id present_raw_src(id src) { + MTLPixelFormat pf = src.pixelFormat == MTLPixelFormatRGBA8Unorm_sRGB ? MTLPixelFormatRGBA8Unorm : src.pixelFormat == MTLPixelFormatBGRA8Unorm_sRGB ? MTLPixelFormatBGRA8Unorm : MTLPixelFormatInvalid; + if (pf == MTLPixelFormatInvalid) return src; + static __strong id views[4]; static int next; static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_lock(&mu); + id v = nil; + for (int i = 0; i < 4 && !v; i++) if (views[i] && views[i].parentTexture == src) v = views[i]; + if (!v && (v = [src newTextureViewWithPixelFormat:pf])) { if (views[next]) defer_obj_unref(views[next]); views[next] = v; next = (next + 1) & 3; } + pthread_mutex_unlock(&mu); + if (!v) { RED_ONCE(69, "出图:sRGB 交换链贴图的非 sRGB 视图建不出(格式 %lu),按解码后的值出图", (unsigned long)src.pixelFormat); return src; } + return v; +} +static int present_encode(id cb, id src, id dst, const int crop[4]) { + NSUInteger cx = 0, cy = 0, cw = src.width, ch = src.height; + if (crop && (crop[2] || crop[3])) { + if (crop[0] < 0 || crop[1] < 0 || crop[2] <= 0 || crop[3] <= 0 || + (uint64_t)crop[0] + crop[2] > src.width || (uint64_t)crop[1] + crop[3] > src.height) { + MLOG("【红】出图裁切越界:%d,%d %dx%d / 原纹理 %lux%lu", crop[0], crop[1], crop[2], crop[3], (unsigned long)src.width, (unsigned long)src.height); return 0; + } + cx = crop[0]; cy = crop[1]; cw = crop[2]; ch = crop[3]; + } + static uint64_t last_crop[8]; uint64_t meta[8] = {src.width, src.height, cx, cy, cw, ch, dst.width, dst.height}; + if (memcmp(meta, last_crop, sizeof meta)) { memcpy(last_crop, meta, sizeof meta); + MLOG("出图裁切:原纹理 %lux%lu,有效 %lu,%lu %lux%lu → 目标 %lux%lu", (unsigned long)src.width, (unsigned long)src.height, + (unsigned long)cx, (unsigned long)cy, (unsigned long)cw, (unsigned long)ch, (unsigned long)dst.width, (unsigned long)dst.height); } + present_pipeline(dst.pixelFormat); + MTLRenderPassDescriptor *rp = [MTLRenderPassDescriptor renderPassDescriptor]; + // Keep the game picture's aspect ratio and center it, leaving black bars in the rest (no stretching when the screen ratio differs from the game's, e.g. iPhone about 19.5:9 versus the game's 16:9); + // when the ratio difference is < 1% (the remainder from the game width being a multiple of 8 in the full-screen setup, e.g. 1952x900 versus 2868x1320 differs by 0.17%) fill directly, leaving no few-pixel border + double dw = dst.width, dh = dst.height, s = fmin(dw / cw, dh / ch); + MTLViewport vp = {floor((dw - cw * s) / 2), floor((dh - ch * s) / 2), round(cw * s), round(ch * s), 0, 1}; + if (fabs((dw / dh) / ((double)cw / ch) - 1) < 0.01) vp = (MTLViewport){0, 0, dw, dh, 0, 1}; + int full = vp.originX == 0 && vp.originY == 0 && vp.width == dw && vp.height == dh; + static double last_vp[4]; // Log one line when the viewport changes (used by the real-device screen-level acceptance screen_check.py to check the aspect ratio) + if (memcmp(last_vp, &vp, sizeof last_vp)) { memcpy(last_vp, &vp, sizeof last_vp); + MLOG("出图视口 %.0f,%.0f %.0fx%.0f / 画布 %.0fx%.0f(游戏画面 %lux%lu)", vp.originX, vp.originY, vp.width, vp.height, dw, dh, (unsigned long)src.width, (unsigned long)src.height); } + rp.colorAttachments[0].texture = dst; rp.colorAttachments[0].loadAction = full ? MTLLoadActionDontCare : MTLLoadActionClear; + rp.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 1); + rp.colorAttachments[0].storeAction = MTLStoreActionStore; + id en = [cb renderCommandEncoderWithDescriptor:rp]; + [en setViewport:vp]; + [en setRenderPipelineState:g_blit_pso]; [en setFragmentTexture:present_raw_src(src) atIndex:0]; [en setFragmentSamplerState:g_blit_samp atIndex:0]; + float uv[8] = {(float)cx / src.width, (float)cy / src.height, (float)cw / src.width, (float)ch / src.height, + (cx + 0.5f) / src.width, (cy + 0.5f) / src.height, (cx + cw - 0.5f) / src.width, (cy + ch - 0.5f) / src.height}; + if (atomic_load_explicit(&g_origin_ll, memory_order_relaxed)) { uv[1] += uv[3]; uv[3] = -uv[3]; } // lower-left origin: the window image is stored bottom-up + [en setFragmentBytes:uv length:sizeof uv atIndex:0]; + [en drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3]; + [en endEncoding]; + return 1; +} +static int present_encode_m4(id cb, id src, id dst, const int crop[4], int uv_i) { + NSUInteger cx = 0, cy = 0, cw = src.width, ch = src.height; + if (crop && (crop[2] || crop[3])) { + if (crop[0] < 0 || crop[1] < 0 || crop[2] <= 0 || crop[3] <= 0 || + (uint64_t)crop[0] + crop[2] > src.width || (uint64_t)crop[1] + crop[3] > src.height) { + MLOG("【红】出图裁切越界:%d,%d %dx%d / 原纹理 %lux%lu", crop[0], crop[1], crop[2], crop[3], (unsigned long)src.width, (unsigned long)src.height); return 0; + } + cx = crop[0]; cy = crop[1]; cw = crop[2]; ch = crop[3]; + } + present_pipeline(dst.pixelFormat); + double dw = dst.width, dh = dst.height, s = fmin(dw / cw, dh / ch); + MTLViewport vp = {floor((dw - cw * s) / 2), floor((dh - ch * s) / 2), round(cw * s), round(ch * s), 0, 1}; + if (fabs((dw / dh) / ((double)cw / ch) - 1) < 0.01) vp = (MTLViewport){0, 0, dw, dh, 0, 1}; + // This is the active iOS17 path. The other present encoder's crop/viewport + // diagnostics did not describe these frames; record changes here as well. + static uint64_t last_layout[8]; + uint64_t layout[8] = {src.width, src.height, cx, cy, cw, ch, dst.width, dst.height}; + if (memcmp(layout, last_layout, sizeof layout)) { + memcpy(last_layout, layout, sizeof layout); + MLOG("[呈现几何] 源 %lux%lu 有效 %lu,%lu %lux%lu;目标 %lux%lu;视口 %.0f,%.0f %.0fx%.0f;横纵缩放比 %.6f", + (unsigned long)src.width, (unsigned long)src.height, (unsigned long)cx, (unsigned long)cy, + (unsigned long)cw, (unsigned long)ch, (unsigned long)dst.width, (unsigned long)dst.height, + vp.originX, vp.originY, vp.width, vp.height, (vp.width / cw) / (vp.height / ch)); + } + int full = vp.originX == 0 && vp.originY == 0 && vp.width == dw && vp.height == dh; + MTLRenderPassDescriptor *rp = [MTLRenderPassDescriptor new]; + rp.colorAttachments[0].texture = dst; + rp.colorAttachments[0].loadAction = full ? MTLLoadActionDontCare : MTLLoadActionClear; + rp.colorAttachments[0].storeAction = MTLStoreActionStore; + rp.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 1); + id en = [cb renderCommandEncoderWithDescriptor:rp]; + if (!en) { MLOG("【红】iOS17 出图渲染编码器创建失败"); return 0; } + + + [en setFragmentTexture:present_raw_src(src) atIndex:0]; + [en setFragmentSamplerState:g_blit_samp atIndex:0]; + float *uv = g_present_uv[uv_i].contents; + uv[0] = (float)cx / src.width; uv[1] = (float)cy / src.height; uv[2] = (float)cw / src.width; uv[3] = (float)ch / src.height; + uv[4] = (cx + 0.5f) / src.width; uv[5] = (cy + 0.5f) / src.height; uv[6] = (cx + cw - 0.5f) / src.width; uv[7] = (cy + ch - 0.5f) / src.height; + if (atomic_load_explicit(&g_origin_ll, memory_order_relaxed)) { uv[1] += uv[3]; uv[3] = -uv[3]; } // lower-left origin: the window image is stored bottom-up + [en setFragmentBuffer:g_present_uv[uv_i] offset:0 atIndex:0]; + [en setViewport:vp]; [en setRenderPipelineState:g_blit_pso]; + [en drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3]; + m4_end(en); + return 1; +} +static double g_link_it_tin; // Enqueue time of the frame the display-link callback is presenting (read and written only on the display-link thread) +static void classic_present_done(void) { + pthread_mutex_lock(&g_pq_mu); + g_classic_pending--; + pthread_cond_broadcast(&g_pq_cv); + pthread_mutex_unlock(&g_pq_mu); +} +static int present_now(const tex_t *t, double period, uint64_t frame, uint64_t ticket, const int crop[4], id supplied, double target_ts, double queued_at) { + if (g_present_stall_ns) { struct timespec ts = {0, (long)g_present_stall_ns}; nanosleep(&ts, NULL); } + g_present_n++; + if (g_use != USE_OFF && g_use != USE_DONE) use_tick(); + if (bootprof() && (g_present_n == 1 || g_present_n % 120 == 0)) { + if (g_present_n == 1) g_bp_first_present = bp_now() - g_bp_t0; + MLOG("[bootprof] t=%.3f s 出图第 %llu 次:metallib 载入 %llu 个 / %.1f ms;管线创建 %llu 个 / %.1f ms(均在提交线程同步);预热领用 着色器 %llu / 渲染管线 %llu / 计算管线 %llu;" + "贴图上传 %.1f MB / %llu 张首传 + %llu 次重传 / %.1f ms;首帧出图 t=%.3f s", (bp_now() - g_bp_t0) / 1e9, g_present_n, + g_bp_lib_n, g_bp_lib_ns / 1e6, g_bp_pso_n, g_bp_pso_ns / 1e6, g_warm_claim_shaders, g_warm_claim_rps, g_warm_claim_cps, + g_bp_up_bytes / 1048576.0, g_pkg_uploads, g_pkg_reuploads, + g_up_total_ns / 1e6, g_bp_first_present / 1e9); + } + PD("X ser=%llu", t->serial); + id src = tex_mtl(t); + if (!src) { RED_ONCE(8, "出图贴图建不出 MTLTexture"); return 0; } + // DM_NVN_REGLUM=x0,y0,x1,y1:file (normalized coordinates): read back this region on every present and record "present sequence number, present time ns, region mean luminance, subsampled grayscale thumbnail (hex)" (diagnostic: verify that animation speed is independent of frame rate). + // A separate command buffer is queued after this frame's rendering (the same queue executes in submit order); does not wait for the GPU and does not disturb frame pacing + static int rl = -1; static float rr[4]; static FILE *rlf; static pthread_mutex_t rlmu = PTHREAD_MUTEX_INITIALIZER; + if (rl < 0) { const char *e = getenv("DM_NVN_REGLUM"), *c = e ? strchr(e, ':') : NULL; + rl = c && sscanf(e, "%f,%f,%f,%f", &rr[0], &rr[1], &rr[2], &rr[3]) == 4 && (rlf = fopen(c + 1, "w")) != NULL; + if (e && !rl) MLOG("【红】DM_NVN_REGLUM 参数或文件无效:%s", e); } + if (rl && (src.pixelFormat == MTLPixelFormatRGBA8Unorm || src.pixelFormat == MTLPixelFormatBGRA8Unorm || src.pixelFormat == MTLPixelFormatRGBA8Unorm_sRGB || src.pixelFormat == MTLPixelFormatBGRA8Unorm_sRGB)) { + NSUInteger x0 = (NSUInteger)(rr[0] * src.width), y0 = (NSUInteger)(rr[1] * src.height), w = (NSUInteger)(rr[2] * src.width) - x0, h = (NSUInteger)(rr[3] * src.height) - y0; + id rb = [g_dev newBufferWithLength:w * h * 4 options:MTLResourceStorageModeShared]; + id cb = q_cb(); id b = [cb blitCommandEncoder]; + [b copyFromTexture:src sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(x0, y0, 0) sourceSize:MTLSizeMake(w, h, 1) toBuffer:rb destinationOffset:0 destinationBytesPerRow:w * 4 destinationBytesPerImage:w * h * 4]; + [b endEncoding]; + unsigned long long n = g_present_n, tns = clock_gettime_nsec_np(CLOCK_UPTIME_RAW); + [cb addCompletedHandler:^(id c) { (void)c; const uint8_t *q = rb.contents; uint64_t sum = 0; + uint8_t g[4096]; NSUInteger ng = 0; // Subsampled grayscale thumbnail (every 4th row / column, at most 4096 points), recorded along with the mean, for per-frame image correlation analysis + for (NSUInteger y = 0; y < h; y += 4) for (NSUInteger x = 0; x < w && ng < sizeof g; x += 4) { const uint8_t *px = q + 4 * (y * w + x); g[ng++] = (uint8_t)((px[0] + px[1] + px[2]) / 3); } + for (NSUInteger k = 0; k < w * h; k++) sum += q[4 * k] + q[4 * k + 1] + q[4 * k + 2]; + pthread_mutex_lock(&rlmu); fprintf(rlf, "%llu\t%llu\t%.4f\t", n, tns, sum / (3.0 * w * h)); + for (NSUInteger k = 0; k < ng; k++) fprintf(rlf, "%02x", g[k]); + fputc('\n', rlf); fflush(rlf); pthread_mutex_unlock(&rlmu); }]; + [cb commit]; + } + // DM_NVN_SHOT=frame number[,frame number...]:path (%d in the path is replaced with the frame number) + static long shots[4096]; static int nshot = -1; static const char *shot_path; // up to 4096 frames (a burst over a frame range, for scoring frame by frame) + if (nshot < 0) { + nshot = 0; const char *e = getenv("DM_NVN_SHOT"), *c = e ? strchr(e, ':') : NULL; + if (c) { shot_path = c + 1; for (const char *q = e; q < c && nshot < 4096; q = strchr(q, ',') ? strchr(q, ',') + 1 : c) shots[nshot++] = atol(q); } + } + for (int i = 0; i < nshot; i++) if ((long)g_present_n == shots[i]) { char path[1024]; snprintf(path, sizeof path, shot_path, (int)g_present_n); mtl_finish_gpu(); shot(src, path, 1, crop); } + pthread_mutex_lock(&g_shotq_mu); + for (int i = 0; i < g_nshotq;) if (g_present_n >= g_shotq[i].frame) { mtl_finish_gpu(); shot(src, g_shotq[i].path, 1, crop); g_shotq[i] = g_shotq[--g_nshotq]; } else i++; + pthread_mutex_unlock(&g_shotq_mu); + + CAMetalLayer *layer = (__bridge CAMetalLayer *)front_metal_layer(); + if (!layer) return 0; +#if TARGET_OS_OSX + if (!layer.displaySyncEnabled) layer.displaySyncEnabled = YES; // Windowed mode follows the display's vsync to prevent tearing +#endif + present_pipeline(layer.pixelFormat); // Same as the original path: the first frame compiles the pipeline before taking the drawable + if (g_grab_cb) grab_present(src, crop, layer.drawableSize); // Game picture needed by the whole-window screenshot (peek without the lock first; the real take locks inside the function) + uint64_t dt0 = now_ns(); + id dr = supplied ? supplied : [layer nextDrawable]; + if (!supplied) g_drawable_ns += now_ns() - dt0; + if (!dr) return 0; + int alloc_i = (int)(ticket % 3); + if (g_present_alloc_fence[alloc_i]) { + pthread_mutex_lock(&g_pq_mu); + while (g_pq_done < g_present_alloc_fence[alloc_i]) pthread_cond_wait(&g_pq_cv, &g_pq_mu); + pthread_mutex_unlock(&g_pq_mu); + } + id cb = [g_q commandBuffer]; + if (!present_encode_m4(cb, src, dr.texture, crop, alloc_i)) { return 0; } + + NvPresentProbe *probe = ticket % 120 == 0 ? [NvPresentProbe new] : nil; + if (probe) { probe->lock = (os_unfair_lock)OS_UNFAIR_LOCK_INIT; probe->ticket=ticket;probe->frame=frame;probe->queued=queued_at; } + [cb addCompletedHandler:^(id f) { + if (f.error) MLOG("【红】上屏命令缓冲错误:%s", f.error.description.UTF8String); +#if TARGET_OS_SIMULATOR + if (f.status == MTLCommandBufferStatusCompleted) atomic_fetch_add(&g_displayed_n, 1); +#endif + present_probe_note(probe,2,f.GPUStartTime,f.GPUEndTime); + pthread_mutex_lock(&g_pq_mu); if (ticket > g_pq_done) g_pq_done = ticket; g_pq_inflight--; pthread_cond_broadcast(&g_pq_cv); pthread_mutex_unlock(&g_pq_mu); + }]; + pthread_mutex_lock(&g_pq_mu); g_pq_inflight++; pthread_mutex_unlock(&g_pq_mu); + g_present_alloc_fence[alloc_i] = ticket; +#if TARGET_OS_SIMULATOR // The iOS simulator SDK has no presentation-time callback and no minimum display duration (used only for picture acceptance, display-link ticks are not counted) + (void)period; +#else + uint64_t pn = ticket; // Record the present time by the game's present sequence number (ticket number): hle's present statistics are taken by "the game's nth present"; in the display link path the actual present count != the game's present count (old frames get merged and dropped) + [dr addPresentedHandler:^(id d) { // Actual presentation time (the time this frame is really put on screen, CACurrentMediaTime base) + CFTimeInterval tt = d.presentedTime; + if (tt > 0) atomic_fetch_add(&g_displayed_n, 1); + g_pres[pn % PRING] = (pres_ent){pn, tt > 0 ? tt : -1}; + present_probe_note(probe,4,tt > 0 ? tt : -1,0); + }]; +#endif + // Scheduling stays asynchronous on both paths. A classic frame keeps its + // layer handoff claim until the drawable's present call returns, not merely + // until commit returns: attaching a link earlier forbids minimum duration. + // GPU completion independently gates swapchain/UV-buffer reuse. + [cb addScheduledHandler:^(id scheduled) { + (void)scheduled; + double scheduled_at = probe ? CACurrentMediaTime() : 0; +#if TARGET_OS_SIMULATOR + [dr present]; +#else + if (!supplied && period > 0) [dr presentAfterMinimumDuration:period]; else [dr present]; +#endif + present_probe_note(probe,1,scheduled_at,0); + if (!supplied) classic_present_done(); + }]; + if (probe) probe->commit = CACurrentMediaTime(); + [cb commit]; + if (target_ts > 0) { + static double leads[300], e2e[300]; // e2e: game enqueue -> system-scheduled present (including the time waiting in the queue for the display-link callback), the full span of input latency that our side can read + static uint64_t lead_n; + leads[lead_n % 300] = (target_ts - CACurrentMediaTime()) * 1000.0; e2e[lead_n % 300] = g_link_it_tin > 0 ? (target_ts - g_link_it_tin) * 1000.0 : 0; + lead_n++; + if (lead_n % 300 == 0) { + double a[300]; memcpy(a, leads, sizeof a); + for (int i = 1; i < 300; i++) { double v = a[i]; int j = i - 1; while (j >= 0 && a[j] > v) { a[j + 1] = a[j]; j--; } a[j + 1] = v; } + double b[300]; memcpy(b, e2e, sizeof b); + for (int i = 1; i < 300; i++) { double v = b[i]; int j = i - 1; while (j >= 0 && b[j] > v) { b[j + 1] = b[j]; j--; } b[j + 1] = v; } + MLOG("显示节拍 至 %llu:节拍 300、出图 %llu、空节拍 %llu(其中按出图间隔留拍 %llu)、合并丢弃旧帧 %llu;提交领先目标上屏 p50 %.3f ms / 最小 %.3f ms;游戏入队到目标上屏 p50 %.3f ms / 最小 %.3f / 最大 %.3f ms", + (unsigned long long)g_link_tick_n, (unsigned long long)g_present_n, (unsigned long long)(g_link_tick_n - g_present_n), (unsigned long long)g_link_held, + (unsigned long long)g_link_dropped, a[149], a[0], b[149], b[0], b[299]); + } + } + (void)frame; + return 1; +} +// ---------- Present thread ---------- +// NVN semantics: QueuePresentTexture only queues the present and returns immediately (the game often calls it while holding a lock; iOS real device: synchronously waiting for nextDrawable inside it +// takes about 30 ms, the lock is held about 90% of the time, and the loading thread starves). Taking the drawable, blit and present are all done on this thread in enqueue order. +// Ticket number = which enqueue it is (starting from 1); g_pq_done = the ticket number that has been processed (blit committed to the GPU queue). +// Swapchain texture return: before AcquireTexture gets a texture it waits for the ticket of that texture's previous present to be processed (mtl_present_wait), after which the commands the game draws into it +// are necessarily ordered after that blit on the GPU queue. +#define PQ 8 +typedef struct { tex_t t; double period; uint64_t frame; int crop[4]; double t_in; } pq_item; // t_in: the time the game handed this frame into the present queue (CACurrentMediaTime) +static pq_item g_pq[PQ]; +static uint64_t g_pq_in; +static int present_now(const tex_t *, double, uint64_t, uint64_t, const int[4], id, double, double); + +#if DM_HAVE_METAL_DISPLAY_LINK +@interface NvMetalDisplayLinkDelegate : NSObject +@end + +// Fallback for the display link going silent while in the foreground (two smoke-test hangs before merge: the display link stopped calling back while the app was in the foreground, nobody takes the queued frames, and the game waits forever for its previous present to be processed before "acquiring the next swapchain texture"): +// when mtl_present_wait has waited longer than STALL_NS and the display link has not called back for that long, drop the waiting frame and earlier queued frames as when entering the background (accounted as dropped frames) and let the game keep running; when the display link comes back things return to normal automatically. +// One stall logs one hint line, and one line on recovery (neither is a failure); only when the same stall lasts longer than STALL_RED_NS is it a failure. All under g_pq_mu +#define STALL_NS 100000000ull +#define STALL_RED_NS 2000000000ull +static uint64_t g_stall_t0, g_stall_drop, g_stall_n; static int g_stall_red, g_stall_logged; +#define LINK_RESCUE_NS 500000000ull +static int g_rescue_mode; static uint64_t g_rescue_back_at, g_rescue_back_ns = 3000000000ull, g_rescue_n; +// The display link watchdog only recognizes the "active" state: when the App loses active status (pulling down Control Center / notifications, incoming call, system popup) and in the background the system does not call back anyway, and the picture is covered too -- the frame-drop fallback stays as before, +// but it is not counted as stuck and no failure is reported; the stall is measured from the later of "the most recent callback / the most recent activation" (A16 logs: 8–17 seconds inactive and 17 seconds in the background were both reported as 「前台卡住」(stuck in foreground)) +__attribute__((weak)) int dm_focus_now(void) { return 1; } // The host's hle_oe.c provides the strong definition; when graphics unit tests do not link the host it is treated as always active +__attribute__((weak)) uint64_t dm_focus_in_since(void) { return 0; } +@implementation NvMetalDisplayLinkDelegate +- (void)metalDisplayLink:(CAMetalDisplayLink *)link needsUpdate:(CAMetalDisplayLinkUpdate *)update { + (void)link; + pthread_mutex_lock(&g_pq_mu); + if (g_stall_t0) { uint64_t since = dm_focus_in_since(), t = now_ns(); + MLOG("显示节拍恢复:这段停了 %.0f ms(其中激活中 %.0f ms),兜底丢了 %llu 帧(第 %llu 段)", (t - g_stall_t0) / 1e6, (t - (g_stall_t0 > since ? g_stall_t0 : since)) / 1e6, (unsigned long long)g_stall_drop, (unsigned long long)g_stall_n); g_stall_t0 = 0; } + pthread_mutex_unlock(&g_pq_mu); + pthread_mutex_lock(&g_link_mu); + g_link_tick_ns = now_ns(); + g_link_tick_n++; + pthread_cond_broadcast(&g_link_cv); + pthread_mutex_unlock(&g_link_mu); + pthread_mutex_lock(&g_pq_mu); + if (!g_dm_present_link || g_pq_read == g_pq_in) { pthread_mutex_unlock(&g_pq_mu); return; } + // The previous frame's present interval was >= 2 (period > 25 ms): keep it on screen for its whole period before taking the next one, as the Switch flips only every N vertical syncs. This also holds while the link runs at 60 Hz (Mario 1.2.1 sets 2 for single frames, see nvn_game_fps in hle_nvn.c); + // before, every tick took the newest frame, so that frame, carrying the extra time step the game advanced for it, was shown for only 16.7 ms and the game looked like it jumped ahead + if (g_link_hold_until > 0 && update.targetPresentationTimestamp < g_link_hold_until - 0.002) { g_link_held++; pthread_mutex_unlock(&g_pq_mu); return; } + uint64_t first = g_pq_read + 1, ticket = g_pq_in; + // In order, one frame per tick and at most one behind the newest: two waiting, the older now and the newer on the next tick; three or more, the older ones are dropped. Taking the newest + // dropped the one before whenever two frames landed in the same refresh, as frames of a 60 fps game that take 16-17.5 ms do, and the next frame then stayed up 33 ms + // (Dark Souls Remastered, from PR #31: presents within 14.7-18.7 ms 98.2 -> 99.98 %, about 2 ms more from enqueue to display). DM_PRESENT_FIFO=0 takes the newest, as before + static int fifo = -1; if (fifo < 0) { const char *e = getenv("DM_PRESENT_FIFO"); fifo = !(e && !strcmp(e, "0")); MLOG("display link: %s", fifo ? "frames in order, at most one behind" : "the newest frame each tick (DM_PRESENT_FIFO=0)"); } + if (fifo && ticket > first) ticket = ticket - first >= 2 ? g_pq_in - 1 : first; + pq_item it = g_pq[(ticket - 1) % PQ]; g_link_it_tin = it.t_in; + g_pq_read = ticket; + if (ticket > first) { g_link_dropped += ticket - first; for (uint64_t k = first; k < ticket; k++) g_pres[k % PRING] = (pres_ent){k, -1}; } // Old frames merged and dropped are recorded as "not presented", so the present statistics honestly count them as dropped frames + if (ticket > 1 && g_pq_done < ticket - 1) g_pq_done = ticket - 1; + pthread_cond_broadcast(&g_pq_cv); + pthread_mutex_unlock(&g_pq_mu); + int submitted; + @autoreleasepool { submitted = present_now(&it.t, it.period, it.frame, ticket, it.crop, update.drawable, update.targetPresentationTimestamp,it.t_in); } + g_link_hold_until = submitted && it.period > 0.025 ? update.targetPresentationTimestamp + it.period : 0; + if (!submitted) { + pthread_mutex_lock(&g_pq_mu); + if (ticket > g_pq_done) g_pq_done = ticket; + pthread_cond_broadcast(&g_pq_cv); + pthread_mutex_unlock(&g_pq_mu); + } +} +@end + +static CAMetalDisplayLink *g_link; static CFRunLoopRef g_link_rl; static int g_link_started; +static int link_on(void) { return g_dm_present_link && g_link != nil; } +static CAFrameRateRange link_range(int mode) { return mode == 3 ? CAFrameRateRangeMake(30, 30, 30) : mode == 2 ? CAFrameRateRangeMake(60, 120, 120) : CAFrameRateRangeMake(60, 60, 60); } +static int g_link_mode = 1; // Current mode of the display link object: 1 / 2 same as g_dm_present_link, 3 = power-saving mode 30 Hz +static void *link_main(void *arg) { // Display link thread: started when switching to display-link mode, exits when switching back to 0 + (void)arg; + pthread_setname_np("nvn-present-link"); + pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0); + CAMetalLayer *layer = (__bridge CAMetalLayer *)front_metal_layer(); + pthread_mutex_lock(&g_pq_mu); g_link_owns_layer = 1; while (g_classic_pending) pthread_cond_wait(&g_pq_cv, &g_pq_mu); pthread_mutex_unlock(&g_pq_mu); // Exclude new nextDrawable calls, then wait for every scheduled classic present to return before attaching +#if TARGET_OS_OSX + if (layer && !layer.displaySyncEnabled) layer.displaySyncEnabled = YES; +#endif + NvMetalDisplayLinkDelegate *delegate = [NvMetalDisplayLinkDelegate new]; + CAMetalDisplayLink *link = [[CAMetalDisplayLink alloc] initWithMetalLayer:layer]; + link.preferredFrameRateRange = link_range(g_link_mode); + link.preferredFrameLatency = g_present_latency; + link.delegate = delegate; + [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; + g_link_rl = CFRunLoopGetCurrent(); g_link = link; + CFRunLoopRun(); // When switching back to mode 0 the display link object is invalidated and this thread exits (nextDrawable cannot get a drawable while the layer still has the display link object attached, so pausing alone is not enough) + g_link_started = 0; + return NULL; +} +static void link_sync(void) { // Called every frame by the game's present thread: aligns the display link object's state to g_dm_present_link and the power-saving mode (can change at run time) + static int applied; int want = g_dm_present_link; + if (g_rescue_mode && now_ns() >= g_rescue_back_at) { + pthread_mutex_lock(&g_pq_mu); g_dm_present_link = g_rescue_mode; g_rescue_mode = 0; g_stall_t0 = 0; g_link_tick_ns = now_ns(); pthread_mutex_unlock(&g_pq_mu); + MLOG("显示节拍:抢救期满,换回节拍模式 %d 再试(帧 %llu)", g_dm_present_link, (unsigned long long)atomic_load(&g_cur_frame)); + } + if (want < 0 || want > 2) { g_dm_present_link = applied == 3 ? 1 : applied; return; } + if (want && mtl_fps_target() == 30) want = 3; // Power-saving mode: display link at 30 Hz + if (want == applied) return; + g_link_mode = want; + if (!g_link_started) { + if (!want) { applied = 0; return; } + if (!front_metal_layer()) return; + g_link_started = 1; pthread_t th; if (pthread_create(&th, NULL, link_main, NULL)) MDIE("显示节拍线程起不来"); pthread_detach(th); + return; // The display link object is created on a new thread; align again next frame + } + CAMetalDisplayLink *link = g_link; if (!link) return; + if (want) link.preferredFrameRateRange = link_range(want); + else { CFRunLoopRef rl = g_link_rl; g_link = nil; CFRunLoopPerformBlock(rl, kCFRunLoopDefaultMode, ^{ [link invalidate]; pthread_mutex_lock(&g_pq_mu); g_link_owns_layer = 0; pthread_cond_broadcast(&g_pq_cv); pthread_mutex_unlock(&g_pq_mu); CFRunLoopStop(rl); }); CFRunLoopWakeUp(rl); } + MLOG("出图节拍模式 %d → %d(帧 %llu)", applied, want, (unsigned long long)atomic_load(&g_cur_frame)); + applied = want; + pthread_mutex_lock(&g_pq_mu); pthread_cond_broadcast(&g_pq_cv); pthread_mutex_unlock(&g_pq_mu); +} +#else +// CAMetalDisplayLink first exists on iOS 17. iOS 16.1 uses the classic +// present thread and command-buffer presentDrawable path. +#define STALL_NS 100000000ull +#define STALL_RED_NS 2000000000ull +#define LINK_RESCUE_NS 500000000ull +static uint64_t g_stall_t0, g_stall_drop, g_stall_n; +static int g_stall_red, g_stall_logged; +static int g_rescue_mode; +static uint64_t g_rescue_back_at, g_rescue_back_ns = 3000000000ull, g_rescue_n; +__attribute__((weak)) int dm_focus_now(void) { return 1; } +__attribute__((weak)) uint64_t dm_focus_in_since(void) { return 0; } +static int g_link_mode; +static int link_on(void) { return 0; } +static void link_sync(void) { g_dm_present_link = 0; g_link_mode = 0; } +#endif +// Before going to the background (mtl_drain): when the display link drives presents, frames queued but not yet presented can only be taken by the display-link callback, and in the background the system no longer calls back -- +// waiting as-is for "the present queue to empty" would wait forever, the enter-background callback would not return, and the system would kill the process (the process disappears within 22 seconds after switching to another App). +// These frames will not be seen anyway: discard them directly (accounted as dropped frames) and only wait for the one present already submitted to the GPU to finish. +static void present_drop_for_suspend(void) { + pthread_mutex_lock(&g_pq_mu); + if (g_link_owns_layer) { + uint64_t n = g_pq_in - g_pq_read; + for (uint64_t k = g_pq_read + 1; k <= g_pq_in; k++) g_pres[k % PRING] = (pres_ent){k, -1}; + g_link_dropped += n; g_pq_read = g_pq_in; + while (g_pq_inflight) pthread_cond_wait(&g_pq_cv, &g_pq_mu); + if (g_pq_done < g_pq_in) g_pq_done = g_pq_in; + pthread_cond_broadcast(&g_pq_cv); + if (n) MLOG("进后台:丢弃排队中未上屏的 %llu 帧(显示节拍在后台不回调)", (unsigned long long)n); + } + pthread_mutex_unlock(&g_pq_mu); +} +static void *present_main(void *arg) { + (void)arg; + pthread_setname_np("nvn-present"); + pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0); + qos_class_t qc = QOS_CLASS_UNSPECIFIED; int rp = 0; pthread_get_qos_class_np(pthread_self(), &qc, &rp); atomic_store(&g_present_qos, (uint32_t)qc); + for (;;) { + pthread_mutex_lock(&g_pq_mu); + while (g_pq_read == g_pq_in || g_link_owns_layer) pthread_cond_wait(&g_pq_cv, &g_pq_mu); // In display-link mode the display-link callback takes them; an empty queue is judged by "taken"; "done" has to wait for the feedback of the present submit and lags behind taken + pq_item it = g_pq[g_pq_read % PQ]; + uint64_t ticket = ++g_pq_read; g_classic_pending++; + pthread_mutex_unlock(&g_pq_mu); + int submitted; + @autoreleasepool { submitted = present_now(&it.t, it.period, it.frame, ticket, it.crop, nil, 0,it.t_in); } + if (!submitted) { + classic_present_done(); // No scheduled callback owns this failed frame + pthread_mutex_lock(&g_pq_mu); + if (ticket > g_pq_done) g_pq_done = ticket; + pthread_cond_broadcast(&g_pq_cv); + pthread_mutex_unlock(&g_pq_mu); + } + } + return NULL; +} +uint32_t mtl_present_qos(void) { return atomic_load(&g_present_qos); } +static void present_start(void) { pthread_t th; if (pthread_create(&th, NULL, present_main, NULL)) MDIE("出图线程起不来"); pthread_detach(th); } +static _Atomic int g_present_discard; +void mtl_link_pause(int on); +// The short period when the front end goes to the background and waits for the game to save: presents are dropped directly (the display link does not call back in the background, enqueueing would block the game thread; nobody sees the picture anyway). +// When turned on, also clear the frames already queued: the system first stops the display-link callback and only afterwards notifies about going to the background, and in that gap the game has already filled the swapchain and is stuck at "before acquiring the next texture, wait for its previous present to be processed" +// (hle_nvn.c nvnWindowAcquireTexture -> mtl_present_wait). Without clearing, the game cannot advance a single tick during those 2.5 seconds of waiting for the save, and the save is not written (the same pattern three times in player logs: 3 frames queued, 0 submits) +void mtl_present_discard(int on) { atomic_store(&g_present_discard, on != 0); if (on) present_drop_for_suspend(); } +uint64_t mtl_present(const tex_t *t, double period, const int crop[4]) { + if (atomic_load(&g_present_discard)) return 0; // Ticket number 0 = no pending present; the swapchain texture acquire does not wait + if (t) { xv_check(t->serial, 0, 0, "出图"); if (g_xv_npresent < 128) g_xv_present[g_xv_npresent++] = t->serial; } + static pthread_once_t once = PTHREAD_ONCE_INIT; pthread_once(&once, present_start); + link_sync(); + { // Test switch DM_TEST_LINK_STALL=start seconds,duration seconds: counting from the first present, pause the display link at that point (imitating the display link not calling back while in the foreground) and release it when the duration is up. For verifying the fallback; not set in the product + static double t0, at = -1, dur; static int st; double now = CACurrentMediaTime(); + if (at < 0) { at = 0; const char *e = getenv("DM_TEST_LINK_STALL"); if (e && sscanf(e, "%lf,%lf", &at, &dur) == 2 && at > 0 && dur > 0) { t0 = now; st = 1; } else at = 0; } + if (st == 1 && link_on() && now - t0 >= at) { st = 2; mtl_link_pause(1); MLOG("DM_TEST_LINK_STALL:暂停显示节拍 %.1f 秒(帧 %llu)", dur, (unsigned long long)atomic_load(&g_cur_frame)); } + else if (st == 2 && now - t0 >= at + dur) { st = 3; mtl_link_pause(0); MLOG("DM_TEST_LINK_STALL:放开显示节拍(帧 %llu)", (unsigned long long)atomic_load(&g_cur_frame)); } + } + pthread_mutex_lock(&g_pq_mu); + while (g_pq_in - g_pq_done >= PQ) pthread_cond_wait(&g_pq_cv, &g_pq_mu); // Wait only when the queue is full; normally AcquireTexture blocks first + pq_item *it = &g_pq[g_pq_in % PQ]; *it = (pq_item){*t, period, atomic_load(&g_cur_frame), {0}, CACurrentMediaTime()}; + if (crop) memcpy(it->crop, crop, sizeof it->crop); // The current frame's state is enqueued with the ticket number, rather than reading the mutable window late on the present thread + uint64_t tk = ++g_pq_in; + pthread_cond_broadcast(&g_pq_cv); + pthread_mutex_unlock(&g_pq_mu); + return tk; +} + +// Bridge used by the Vulkan/OpenGL front ends that are linked into the common +// host. Z-A itself uses NVN, but the symbols remain part of the shared host. +void *mtl_device(void) { mtl_init(); return (__bridge void *)g_dev; } +uint64_t mtl_ext_tex(void *tex) { + static _Atomic uint64_t n; + id t = (__bridge id)tex; + uint64_t serial = (1ull << 62) + atomic_fetch_add(&n, 1) + 1; + res_add(t); tex_put(serial, t); + return serial; +} +uint64_t mtl_present_ext(uint64_t serial, double period, void *event, uint64_t event_value, const int crop[4]) { + (void)event; (void)event_value; + tex_t t; memset(&t, 0, sizeof t); t.magic = MAGIC('T', 'E', 'X', 0); t.serial = serial; + return mtl_present(&t, period, crop); +} +// Do not wait during discard: the frame enqueued only after the flag was set (the present thread had just passed the flag check) has nobody to take it, and waiting for it would stall again; it is left for the following mtl_drain to clear +// Do not wait indefinitely in display-link mode: see the note at STALL_NS. Only frames "not yet taken by the display link" are dropped; the one already on its way to the screen is waited for until handed to the GPU (same dropping as when entering the background) +void mtl_present_wait(uint64_t tk) { + pthread_mutex_lock(&g_pq_mu); + while (g_pq_done < tk && !atomic_load(&g_present_discard)) { + if (!g_link_owns_layer) { pthread_cond_wait(&g_pq_cv, &g_pq_mu); continue; } + if (g_pq_read >= tk) { pthread_cond_wait(&g_pq_cv, &g_pq_mu); continue; } // This one has already been taken by the display link and is being presented: wait for it to be handed to the GPU (the GPU completion callback will wake us) + struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); ts.tv_nsec += (long)STALL_NS; if (ts.tv_nsec >= 1000000000L) { ts.tv_sec++; ts.tv_nsec -= 1000000000L; } + // Already determined to be in a stall (when the display link comes back the callback first clears g_stall_t0, then records the display-link tick time): no longer wait 100 ms in vain every frame, drop directly, and the game proceeds by its own display-link wait timeout (about two ticks) + if (!g_stall_t0 && (pthread_cond_timedwait(&g_pq_cv, &g_pq_mu, &ts) != ETIMEDOUT || g_pq_done >= tk)) continue; + uint64_t now = now_ns(), last = __atomic_load_n(&g_link_tick_ns, __ATOMIC_RELAXED); + if (g_pq_read >= tk) continue; + if (now - last < STALL_NS) { pthread_cond_timedwait(&g_pq_cv, &g_pq_mu, &ts); continue; } // The display link is still running, it is just not this one's turn yet: keep waiting (bounded, no busy spinning) + uint64_t n = tk - g_pq_read; + for (uint64_t k = g_pq_read + 1; k <= tk; k++) g_pres[k % PRING] = (pres_ent){k, -1}; + g_link_dropped += n; g_pq_read = tk; + while (g_pq_inflight) pthread_cond_wait(&g_pq_cv, &g_pq_mu); + if (g_pq_done < tk) g_pq_done = tk; + pthread_cond_broadcast(&g_pq_cv); + if (!g_stall_t0) { g_stall_t0 = last ? last : now; g_stall_drop = 0; g_stall_red = 0; g_stall_logged = 0; g_stall_n++; } + g_stall_drop += n; + int active = dm_focus_now() == 1; uint64_t since = dm_focus_in_since(), from = g_stall_t0 > since ? g_stall_t0 : since; + if (active && !g_stall_logged && now - from >= STALL_NS) { g_stall_logged = 1; + MLOG("显示节拍 %.0f ms 没回调(激活中):丢掉排队未上屏的 %llu 帧,游戏继续(第 %llu 段;节拍回来自动照常)", (now - from) / 1e6, (unsigned long long)n, (unsigned long long)g_stall_n); } + if (active && !g_rescue_mode && g_dm_present_link && now - from >= LINK_RESCUE_NS) { + g_rescue_mode = g_dm_present_link; g_dm_present_link = 0; g_rescue_n++; g_rescue_back_at = now + g_rescue_back_ns; + MLOG("显示节拍停滞超过 %.0f ms(激活中):自动抢救切换至传统模式,画面保持刷新(第 %llu 次)", (now - from) / 1e6, (unsigned long long)g_rescue_n); + } + if (active && !g_stall_red && now - from >= STALL_RED_NS) { g_stall_red = 1; MLOG("【红】显示节拍激活中连续 %.1f 秒没回调,出图全靠兜底丢帧,画面不动", (now - from) / 1e9); } + } + pthread_mutex_unlock(&g_pq_mu); +} +// Wait until all queued presents have been processed (used by mtl_finish = nvnQueueFinish etc.): goes through mtl_present_wait, so there is still a fallback when the display link stops (real device: the game called QueueFinish once while the display link was stopped, +// and this used to wait here without a timeout; nobody took the 3 queued frames and the game stalled; those two runs on Mac did not hit this call) +void mtl_present_flush(void) { pthread_mutex_lock(&g_pq_mu); uint64_t h = g_pq_in; pthread_mutex_unlock(&g_pq_mu); mtl_present_wait(h); } +void mtl_stats(uint64_t *passes, uint64_t *draws) { *passes = g_stat_pass; *draws = g_stat_draw; } +void mtl_args_stats(uint64_t *enc, uint64_t *reuse) { *enc = g_stat_args_enc; *reuse = g_stat_args_reuse; } +void mtl_use_stats(uint64_t *call, uint64_t *skip) { *call = g_legacy_use_calls; *skip = g_legacy_use_skipped; } +void mtl_state_stats(uint64_t *call, uint64_t *skip) { *call = g_legacy_state_calls; *skip = g_legacy_state_skips; } +void mtl_pipeline_bind_stats(uint64_t *pso_call, uint64_t *pso_skip, uint64_t *dss_call, uint64_t *dss_skip) { + *pso_call = g_legacy_pso_calls; *pso_skip = g_legacy_pso_skips; + *dss_call = g_legacy_dss_calls; *dss_skip = g_legacy_dss_skips; +} +void mtl_buffer_bind_stats(uint64_t *resolve, uint64_t *reuse, uint64_t *offset) { + *resolve = g_vbuf_resolve; *reuse = g_vbuf_reuse; *offset = g_buffer_offset_calls; +} +void mtl_vbuf_stats(uint64_t *resolve, uint64_t *reuse, uint64_t *set, uint64_t *skip, uint64_t *copies, uint64_t *copy_bytes) { + *resolve = g_vbuf_resolve; *reuse = g_vbuf_reuse; *set = 0; *skip = 0; *copies = 0; *copy_bytes = 0; +} +uint64_t mtl_copy_cut_count(void) { return 0; } +void mtl_lc_stats(uint64_t *taken, uint64_t *landed) { *taken = 0; *landed = 0; } +void mtl_ubp_stats(uint64_t *made, uint64_t *reused, uint64_t *refused) { *made = 0; *reused = 0; *refused = 0; } +void mtl_ubp_why(uint64_t why[5]) { memset(why, 0, 5 * sizeof *why); } +uint64_t mtl_ds_omit(void) { return g_stat_ds_omit; } +void mtl_bw(uint64_t *load, uint64_t *store, uint64_t *full) { *load = g_bw_load; *store = g_bw_store; *full = g_pass_full_rw; } +uint64_t mtl_udc_saved_bytes(void) { return g_udc_saved_bytes; } +int mtl_has_window(void) { return front_metal_layer() != NULL; } +int mtl_link_active(void) { return link_on(); } +#if DM_HAVE_METAL_DISPLAY_LINK +void mtl_link_pause(int on) { CAMetalDisplayLink *l = g_link; if (l) l.paused = on != 0; } // For testing (DM_TEST_BG): imitates the system stopping the display-link callback in the background +#else +void mtl_link_pause(int on) { (void)on; } +#endif +int mtl_link_wait_tick(int vsyncs, uint64_t timeout_ns) { + if (!mtl_link_active()) return 0; + // Wait for the display link only when running faster than the display: if a display-link tick has already passed since the previous frame started work (this frame is already behind), start immediately instead of waiting in vain for the next display-link tick -- + // otherwise when the frame time is slightly more than one display-link period it would "miss the tick -> wait another whole tick", locking the frame rate straight to half (measured on Mac under windowed load: runs of dropped frames) + static uint64_t started_tick; // Display-link tick number when the previous frame started work (only the game's present thread calls this function) + // How many display-link ticks one frame takes: the game's present interval (vsyncs, 60 Hz) times 1 with a 60 Hz display link, times 2 with a 120 Hz one, halved and rounded up at 30 Hz. + // A frame presented with interval N blocks the game for N vsyncs on the Switch, as the display callback holds it N ticks: games that scale their time step by the interval (BotW's VFR: interval / 2) + // ran at double speed while they produced interval-2 frames every 60 Hz tick (iPhone log: Link sprinting with B, the interval flapping 1 ↔ 2) + if (vsyncs < 1) vsyncs = 1; + const uint64_t step = g_link_mode == 3 ? (uint64_t)(vsyncs + 1) / 2 : (uint64_t)vsyncs * (g_link_mode == 2 ? 2 : 1); + pthread_mutex_lock(&g_link_mu); + if (g_link_tick_n >= started_tick + step) { started_tick = g_link_tick_n; pthread_mutex_unlock(&g_link_mu); return 1; } + struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec += timeout_ns / 1000000000ull; ts.tv_nsec += timeout_ns % 1000000000ull; + if (ts.tv_nsec >= 1000000000L) { ts.tv_sec++; ts.tv_nsec -= 1000000000L; } + while (g_link_tick_n < started_tick + step) if (pthread_cond_timedwait(&g_link_cv, &g_link_mu, &ts) == ETIMEDOUT) { pthread_mutex_unlock(&g_link_mu); return 0; } + started_tick = g_link_tick_n; + pthread_mutex_unlock(&g_link_mu); return 1; +} + +// The display's vsync for vi::GetDisplayVsyncEvent (hle_sdk16.c): returns 1 at the first display-link tick of the next 60 Hz vsync after *seen (the tick count it last returned, updated), +// 0 when the link does not run, runs at 30 Hz (power saving: the 60 Hz event cannot come from it) or gives no tick within the timeout; the caller then keeps its own clock +int mtl_link_wait_vsync(uint64_t *seen, uint64_t timeout_ns) { + if (!mtl_link_active() || g_link_mode == 3) return 0; + const uint64_t step = g_link_mode == 2 ? 2 : 1; + pthread_mutex_lock(&g_link_mu); + if (*seen > g_link_tick_n || g_link_tick_n > *seen + 4 * step) *seen = g_link_tick_n; // first call, or far behind (a stall): from the current tick on, no burst + struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec += timeout_ns / 1000000000ull; ts.tv_nsec += timeout_ns % 1000000000ull; + if (ts.tv_nsec >= 1000000000L) { ts.tv_sec++; ts.tv_nsec -= 1000000000L; } + while (g_link_tick_n < *seen + step) if (pthread_cond_timedwait(&g_link_cv, &g_link_mu, &ts) == ETIMEDOUT) { pthread_mutex_unlock(&g_link_mu); return 0; } + *seen += step; + pthread_mutex_unlock(&g_link_mu); return 1; +} + +static id g_prefetched_drawable; // Used for compositing straight onto the drawable +id mtl_present_take_prefetched(void) { return g_prefetched_drawable; } + +#ifdef NVN_RT_RANGE_TEST +#include "rt_range_check.inc" +#endif + +// Called by the watchdog (no progress): writes the state of all blocking wait points on the graphics side to the log -- who is waiting for whom is visible at a glance (after switching to the Metal 4 submit model there was once a case of "ran to 900 frames then stopped, no failure line") +void mtl_wait_state_dump(void) { + MLOG("等待点状态:主栅栏 已提交 %llu / 已完成 %llu;主分配器栅栏 [%llu %llu %llu];上传 未完成 %d、分配器占用 [%d %d %d]、序号 %u、有未冲刷批次 %d;出图队列 入 %llu / 完成 %llu;主命令缓冲在编 %d、提交线程已设 %d", + (unsigned long long)g_fence, (unsigned long long)g_done, (unsigned long long)g_alloc_fence[0], (unsigned long long)g_alloc_fence[1], (unsigned long long)g_alloc_fence[2], + atomic_load(&g_up_pending), atomic_load(&g_up_alloc_busy[0]), atomic_load(&g_up_alloc_busy[1]), atomic_load(&g_up_alloc_busy[2]), g_up_seq, g_up_cb != nil, + (unsigned long long)g_pq_in, (unsigned long long)g_pq_done, g_cb != nil, atomic_load(&g_sub_thread_set)); +} +void mtl_alloc_stats(uint64_t *waits, uint64_t *grown, int *n) { *waits = g_alloc_waits; *grown = g_alloc_grown; *n = g_alloc_n; } // Main submit allocator pool: how many times it waited, how many times it grew, how many exist now + +// Live pass metadata is bounded and lock-free. Pixel / raw-texture control +// captures remain inert until their implementations are ported. +void dm_gfx_passdump_next(long n) { + if (n <= 0) { atomic_store_explicit(&g_passdump_live, 0, memory_order_release); return; } + uint64_t start = atomic_load(&g_cur_frame) + 1, count = n > 16 ? 16 : (uint64_t)n; + if (start > (UINT64_MAX >> 8)) return; + atomic_store_explicit(&g_passdump_live, (start << 8) | count, memory_order_release); + MLOG("Live pass metadata: frames %llu..%llu (%llu frames)", (unsigned long long)start, + (unsigned long long)(start + count - 1), (unsigned long long)count); +} +void dm_gfx_texdump_next(const char *path) { (void)path; } +void dm_gfx_texlist(int target) { (void)target; } +void dm_gfx_texraw(uint64_t serial, const char *path) { (void)serial; (void)path; } +#pragma clang attribute pop diff --git a/dm/gfx/nvn/nvn_pipeline_cache.h b/dm/gfx/nvn/nvn_pipeline_cache.h new file mode 100644 index 0000000000..8e848f8325 --- /dev/null +++ b/dm/gfx/nvn/nvn_pipeline_cache.h @@ -0,0 +1,16 @@ +// Traditional Metal binary archives. No Metal 4 APIs or game-state serialization. +#pragma once +#include +typedef struct { + uint64_t hits, misses, bypassed, added, saved; + int loaded, write_failed, disabled; +} nvn_cache_status; +void nvn_cache_snapshot(nvn_cache_status *out); +void nvn_cache_flush(void); // Worker-thread only; atomic replacement preserves the last good file. +void nvn_cache_flush_async(void); +#ifdef __OBJC__ +#import +id nvn_cached_render(id device, MTLRenderPipelineDescriptor *desc, NSError **error); +id nvn_cached_render_specialized(id device, MTLRenderPipelineDescriptor *desc, int specialized, NSError **error); +id nvn_cached_compute(id device, id function, NSError **error); +#endif diff --git a/dm/gfx/nvn/nvn_pipeline_cache.m b/dm/gfx/nvn/nvn_pipeline_cache.m new file mode 100644 index 0000000000..a9dae7bc5c --- /dev/null +++ b/dm/gfx/nvn/nvn_pipeline_cache.m @@ -0,0 +1,198 @@ +#import +#import +#include +#include +#include +#include +#include +#include +#include "nvn_pipeline_cache.h" +#include "dmsh_chain.h" +#include "game_profile.h" + +#define PCLOG(...) do { fprintf(stderr, "[pipeline-cache] " __VA_ARGS__); fputc('\n', stderr); } while (0) +// Bump only when descriptor generation or utility shader semantics change. +#define CACHE_ABI "nvn-ios17-pso-7" +#define CACHE_MAX_BYTES (256ull << 20) +#define CACHE_RESERVE_BYTES (128ull << 20) +static pthread_mutex_t cache_mu = PTHREAD_MUTEX_INITIALIZER; +static id archive; +static NSString *cache_file; +static nvn_cache_status stats; +static int initialized, save_scheduled; + +static NSString *digest(NSData *data) { + unsigned char hash[CC_SHA256_DIGEST_LENGTH]; + CC_SHA256(data.bytes, (CC_LONG)data.length, hash); + NSMutableString *s = [NSMutableString string]; + for (unsigned i = 0; i < sizeof hash; ++i) [s appendFormat:@"%02x", hash[i]]; + return s; +} +static NSString *file_digest(NSString *path) { + NSData *d = [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:nil]; + return d ? digest(d) : @"missing"; +} +// Called under cache_mu. The IPA build number is deliberately absent: compatible +// updates reuse binaries. Game modules, shader tables, GPU and OS partition them. +static void cache_init(id device) { + if (initialized) return; + initialized = 1; + const char *override = getenv("DM_PIPELINE_CACHE_DIR"); + if (getenv("DM_PIPELINE_CACHE") && !strcmp(getenv("DM_PIPELINE_CACHE"), "off")) { stats.disabled = 1; return; } +#if !TARGET_OS_IPHONE + if (!override) { stats.disabled = 1; return; } // Host fixtures opt in explicitly. +#endif + NSString *dm = getenv("DM_NVN_SHMAP") ? [@(getenv("DM_NVN_SHMAP")) stringByDeletingLastPathComponent] : nil; + if (!dm) { stats.disabled = 1; return; } + char osbuild[128] = {0}; size_t size = sizeof osbuild; + sysctlbyname("kern.osversion", osbuild, &size, NULL, 0); + NSMutableArray *identity = [NSMutableArray arrayWithArray:@[@CACHE_ABI, @DM_GAME_ID, @DMSH_CHAIN, + device.name ?: @"unknown", @(device.registryID), @(osbuild), + NSProcessInfo.processInfo.operatingSystemVersionString]]; + NSArray *files = [[NSFileManager.defaultManager contentsOfDirectoryAtPath:dm error:nil] sortedArrayUsingSelector:@selector(compare:)]; + BOOL has_main = NO; + for (NSString *name in files) if ([name hasSuffix:@".fixups.tsv"] || [@[@"shader_map.tsv", @"c1_full.tsv"] containsObject:name]) { + [identity addObject:name]; [identity addObject:file_digest([dm stringByAppendingPathComponent:name])]; + if ([name isEqualToString:@"main.fixups.tsv"]) has_main = YES; + } +#if TARGET_OS_IPHONE + if (!has_main) { stats.disabled = 1; PCLOG("missing module identity; persistence disabled"); return; } +#else + (void)has_main; +#endif + NSString *key = digest([NSJSONSerialization dataWithJSONObject:identity options:0 error:nil]); + NSString *root = override ? @(override) : [[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"NXPipelineCache"]; + NSString *dir = [[root stringByAppendingPathComponent:@DM_GAME_ID] stringByAppendingPathComponent:key]; + NSError *error = nil; + if (![NSFileManager.defaultManager createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:&error]) { + stats.disabled = stats.write_failed = 1; PCLOG("directory unavailable: %s", error.localizedDescription.UTF8String); return; + } + [[NSURL fileURLWithPath:root] setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil]; + cache_file = [dir stringByAppendingPathComponent:@"pipelines.metalarc"]; + MTLBinaryArchiveDescriptor *d = [MTLBinaryArchiveDescriptor new]; + NSDictionary *attr = [NSFileManager.defaultManager attributesOfItemAtPath:cache_file error:nil]; + if (attr && [attr fileSize] <= CACHE_MAX_BYTES) d.url = [NSURL fileURLWithPath:cache_file]; + archive = [device newBinaryArchiveWithDescriptor:d error:&error]; + if (!archive && d.url) { + PCLOG("archive rejected; rebuilding safely: %s", error.localizedDescription.UTF8String); + d.url = nil; error = nil; archive = [device newBinaryArchiveWithDescriptor:d error:&error]; + } else if (archive && d.url) stats.loaded = 1; + if (!archive) { stats.disabled = 1; PCLOG("archive unavailable: %s", error.localizedDescription.UTF8String); } + PCLOG("identity=%s loaded=%d disabled=%d", key.UTF8String, stats.loaded, stats.disabled); +} + +static BOOL can_archive(id function) { + if (!function) return YES; + // Device serialization of specialized functions is only supported from iOS18/ + // macOS15. Conservatively bypass functions declaring constants on older OSes. + if (@available(iOS 18.0, macOS 15.0, *)) return YES; + return function.functionConstantsDictionary.count == 0; +} +static void schedule_save_locked(void) { + if (save_scheduled || !archive || stats.write_failed) return; + save_scheduled = 1; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 30 * NSEC_PER_SEC), dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{ + nvn_cache_flush(); + pthread_mutex_lock(&cache_mu); save_scheduled = 0; pthread_mutex_unlock(&cache_mu); + }); +} + +id nvn_cached_render(id device, MTLRenderPipelineDescriptor *desc, NSError **error) { + return nvn_cached_render_specialized(device, desc, 0, error); +} +id nvn_cached_render_specialized(id device, MTLRenderPipelineDescriptor *desc, int specialized, NSError **error) { + pthread_mutex_lock(&cache_mu); cache_init(device); + BOOL eligible = archive && can_archive(desc.vertexFunction) && can_archive(desc.fragmentFunction); + if (specialized) { + if (@available(iOS 18.0, macOS 15.0, *)) { /* Device serialization supported. */ } + else eligible = NO; + } + MTLRenderPipelineDescriptor *d = eligible ? [desc copy] : nil; + id result = nil; + if (eligible) { + d.binaryArchives = @[archive]; NSError *miss = nil; + result = [device newRenderPipelineStateWithDescriptor:d options:MTLPipelineOptionFailOnBinaryArchiveMiss reflection:nil error:&miss]; + if (result) stats.hits++; else stats.misses++; + } else stats.bypassed++; + pthread_mutex_unlock(&cache_mu); + if (result) { if (error) *error = nil; return result; } + // Ordinary compilation stays outside the archive mutex so prediction retains + // its parallelism. A cache/storage failure never changes the actual descriptor. + result = [device newRenderPipelineStateWithDescriptor:desc error:error]; + if (result && eligible) { + pthread_mutex_lock(&cache_mu); NSError *add_error = nil; + d.binaryArchives = nil; + if ([archive addRenderPipelineFunctionsWithDescriptor:d error:&add_error]) { stats.added++; schedule_save_locked(); } + else { stats.bypassed++; if (stats.bypassed < 5) PCLOG("render archive fallback: %s", add_error.localizedDescription.UTF8String); } + pthread_mutex_unlock(&cache_mu); + } + return result; +} +id nvn_cached_compute(id device, id function, NSError **error) { + pthread_mutex_lock(&cache_mu); cache_init(device); + BOOL eligible = archive && can_archive(function); + MTLComputePipelineDescriptor *d = [MTLComputePipelineDescriptor new]; d.computeFunction = function; + id result = nil; + if (eligible) { + d.binaryArchives = @[archive]; NSError *miss = nil; + result = [device newComputePipelineStateWithDescriptor:d options:MTLPipelineOptionFailOnBinaryArchiveMiss reflection:nil error:&miss]; + if (result) stats.hits++; else stats.misses++; + } else stats.bypassed++; + pthread_mutex_unlock(&cache_mu); + if (result) { if (error) *error = nil; return result; } + result = [device newComputePipelineStateWithFunction:function error:error]; + if (result && eligible) { + pthread_mutex_lock(&cache_mu); NSError *add_error = nil; d.binaryArchives = nil; + if ([archive addComputePipelineFunctionsWithDescriptor:d error:&add_error]) { stats.added++; schedule_save_locked(); } + else { stats.bypassed++; if (stats.bypassed < 5) PCLOG("compute archive fallback: %s", add_error.localizedDescription.UTF8String); } + pthread_mutex_unlock(&cache_mu); + } + return result; +} +void nvn_cache_snapshot(nvn_cache_status *out) { + // UI polling must never wait behind a compiler or a disk serialization. + static _Thread_local nvn_cache_status last; + if (!pthread_mutex_trylock(&cache_mu)) { last = stats; pthread_mutex_unlock(&cache_mu); } + *out = last; +} +void nvn_cache_flush(void) { + @autoreleasepool { + pthread_mutex_lock(&cache_mu); + if (!archive || stats.added == stats.saved || stats.write_failed) { pthread_mutex_unlock(&cache_mu); return; } + NSString *dir = cache_file.stringByDeletingLastPathComponent; + NSString *temp = [dir stringByAppendingPathComponent:@"pipelines.pending"]; + struct statvfs space; NSError *error = nil; + BOOL ok = !statvfs(dir.fileSystemRepresentation, &space) && (uint64_t)space.f_bavail * space.f_frsize > CACHE_MAX_BYTES + CACHE_RESERVE_BYTES; + if (ok) ok = [archive serializeToURL:[NSURL fileURLWithPath:temp] error:&error]; + NSDictionary *attr = ok ? [NSFileManager.defaultManager attributesOfItemAtPath:temp error:nil] : nil; + ok = ok && attr && [attr fileSize] > 0 && [attr fileSize] <= CACHE_MAX_BYTES; + if (ok) { int fd = open(temp.fileSystemRepresentation, O_RDONLY); ok = fd >= 0 && fsync(fd) == 0; if (fd >= 0) close(fd); } + if (ok) ok = rename(temp.fileSystemRepresentation, cache_file.fileSystemRepresentation) == 0; + if (ok) { + stats.saved = stats.added; + int fd = open(dir.fileSystemRepresentation, O_RDONLY); if (fd >= 0) { fsync(fd); close(fd); } + PCLOG("saved added=%llu hits=%llu misses=%llu bypassed=%llu bytes=%llu", stats.saved, stats.hits, stats.misses, stats.bypassed, [attr fileSize]); + // Retain at most the current and most recent previous identity. Only + // our fixed archive/temp filenames in hash-named directories qualify. + NSString *parent = dir.stringByDeletingLastPathComponent; + NSMutableArray *old = [NSMutableArray array]; + NSCharacterSet *nonhex = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdef"] invertedSet]; + for (NSString *name in [NSFileManager.defaultManager contentsOfDirectoryAtPath:parent error:nil]) + if (name.length == 64 && [name rangeOfCharacterFromSet:nonhex].location == NSNotFound && ![name isEqual:dir.lastPathComponent]) [old addObject:[parent stringByAppendingPathComponent:name]]; + [old sortUsingComparator:^NSComparisonResult(NSString *a, NSString *b) { + NSDate *da = [[NSFileManager.defaultManager attributesOfItemAtPath:[a stringByAppendingPathComponent:@"pipelines.metalarc"] error:nil] fileModificationDate] ?: NSDate.distantPast; + NSDate *db = [[NSFileManager.defaultManager attributesOfItemAtPath:[b stringByAppendingPathComponent:@"pipelines.metalarc"] error:nil] fileModificationDate] ?: NSDate.distantPast; + return [db compare:da]; + }]; + for (NSUInteger i = 1; i < old.count; ++i) { + for (NSString *name in @[@"pipelines.metalarc", @"pipelines.pending"]) [NSFileManager.defaultManager removeItemAtPath:[old[i] stringByAppendingPathComponent:name] error:nil]; + rmdir(old[i].fileSystemRepresentation); // Never recursively delete unknown content. + } + } else { stats.write_failed = 1; PCLOG("persistence failed; previous archive retained: %s", error ? error.localizedDescription.UTF8String : "space/size/atomic write check"); } + [NSFileManager.defaultManager removeItemAtPath:temp error:nil]; + pthread_mutex_unlock(&cache_mu); + } +} +void nvn_cache_flush_async(void) { + dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{ nvn_cache_flush(); }); +} diff --git a/dm/gfx/nvn/nvn_pixel_history.h b/dm/gfx/nvn/nvn_pixel_history.h new file mode 100644 index 0000000000..452bb9d08a --- /dev/null +++ b/dm/gfx/nvn/nvn_pixel_history.h @@ -0,0 +1,196 @@ +// Opt-in, one-frame render-pass pixel history for the traditional Metal backend. +// Included after geometry_diag_frame; no shader variants or CPU/GPU waits. +// DM_NVN_PIXEL_HISTORY="u,v;u,v;u,v" selects up to three normalized points. +// A changed geometry.capture token arms the same single frame as geometry logs. +#pragma once + +typedef struct { + __strong id texture; + NSUInteger level, slice, plane; + uint64_t serial; + unsigned bpp; int slot, depth; +} ph_attachment; +typedef struct { + uint64_t frame, pass, serial; + uint32_t format, level, slice, plane, width, height, x, y, point, bpp, offset; + int32_t slot, depth; +} ph_pixel; +static struct { + int initialized, points, active, attachments, passes, draws; + double xy[3][2]; uint64_t frame, pass; + ph_attachment att[9]; + char first_vs[20], first_fs[20], last_vs[20], last_fs[20]; +} g_pixel_history; + +static int pixel_history_configured(void) { + if (!g_pixel_history.initialized) { + g_pixel_history.initialized = 1; + const char *p = getenv("DM_NVN_PIXEL_HISTORY"); + while (p && *p && g_pixel_history.points < 3) { + char *end; double u = strtod(p, &end); if (end == p || *end != ',') break; + p = end + 1; double v = strtod(p, &end); if (end == p) break; + if (!isfinite(u) || !isfinite(v) || u < 0 || u > 1 || v < 0 || v > 1) break; + int i = g_pixel_history.points++; g_pixel_history.xy[i][0] = u; g_pixel_history.xy[i][1] = v; + if (*end != ';') break; p = end + 1; + } + if (g_pixel_history.points) MLOG("[pixel-history] configured %d points, max 128 passes per captured frame; raw stored channel values, no CPU wait", g_pixel_history.points); + } + return g_pixel_history.points != 0; +} +// Explicit whitelist: unsupported / block-compressed formats are never copied. +static unsigned pixel_history_bytes(MTLPixelFormat f, int depth) { + if (depth) { + if (f == MTLPixelFormatDepth16Unorm) return 2; + if (f == MTLPixelFormatDepth32Float || f == MTLPixelFormatDepth32Float_Stencil8) return 4; + return 0; + } + switch (f) { + case MTLPixelFormatR8Unorm: case MTLPixelFormatR8Uint: return 1; + case MTLPixelFormatR16Unorm: case MTLPixelFormatR16Float: case MTLPixelFormatR16Uint: case MTLPixelFormatRG8Unorm: return 2; + case MTLPixelFormatR32Float: case MTLPixelFormatR32Uint: case MTLPixelFormatRG16Float: + case MTLPixelFormatRGBA8Unorm: case MTLPixelFormatRGBA8Unorm_sRGB: + case MTLPixelFormatBGRA8Unorm: case MTLPixelFormatBGRA8Unorm_sRGB: + case MTLPixelFormatRGB10A2Unorm: case MTLPixelFormatRG11B10Float: return 4; + case MTLPixelFormatRG32Float: case MTLPixelFormatRGBA16Float: return 8; + case MTLPixelFormatRGBA32Float: return 16; + default: return 0; + } +} +static void pixel_history_add(MTLRenderPassAttachmentDescriptor *a, int slot, int depth, uint64_t serial) { + id t = a.texture; if (!t || g_pixel_history.attachments >= 9) return; + const char *skip = NULL; unsigned bpp = pixel_history_bytes(t.pixelFormat, depth); + if (t.storageMode == MTLStorageModeMemoryless) skip = "memoryless"; + else if (t.framebufferOnly) skip = "framebuffer-only"; + else if (t.sampleCount != 1) skip = "multisampled"; + else if (!bpp) skip = "unsupported-format"; + else if (t.textureType != MTLTextureType2D && t.textureType != MTLTextureType2DArray && + t.textureType != MTLTextureTypeCube && t.textureType != MTLTextureTypeCubeArray && t.textureType != MTLTextureType3D) skip = "unsupported-type"; + else if (a.level >= t.mipmapLevelCount) skip = "invalid-level"; + if (skip) { + MLOG("[pixel-history-skip] frame=%llu pass=%llu slot=%d depth=%d format=%lu type=%lu reason=%s", + (unsigned long long)g_pixel_history.frame, (unsigned long long)g_pixel_history.pass, slot, depth, + (unsigned long)t.pixelFormat, (unsigned long)t.textureType, skip); return; + } + NSUInteger slices = t.arrayLength * (t.textureType == MTLTextureTypeCube || t.textureType == MTLTextureTypeCubeArray ? 6 : 1); + if ((t.textureType == MTLTextureType3D && a.depthPlane >= MAX(t.depth >> a.level, 1ul)) || + (t.textureType != MTLTextureType3D && a.slice >= slices)) { + MLOG("[pixel-history-skip] frame=%llu pass=%llu slot=%d reason=invalid-slice", (unsigned long long)g_pixel_history.frame, (unsigned long long)g_pixel_history.pass, slot); return; + } + ph_attachment *p = &g_pixel_history.att[g_pixel_history.attachments++]; + p->texture = t; p->level = a.level; p->slice = a.slice; p->plane = a.depthPlane; + p->serial = serial; p->slot = slot; p->depth = depth; p->bpp = bpp; +} +static void pixel_history_begin(MTLRenderPassDescriptor *pd, NSUInteger nc) { + g_pixel_history.active = 0; + if (!pixel_history_configured() || !geometry_diag_frame()) return; + uint64_t frame = atomic_load(&g_cur_frame); + if (g_pixel_history.frame != frame) { g_pixel_history.frame = frame; g_pixel_history.passes = 0; } + if (g_pixel_history.passes >= 128) return; + g_pixel_history.passes++; g_pixel_history.pass = g_stat_pass + 1; + g_pixel_history.attachments = g_pixel_history.draws = 0; + g_pixel_history.first_vs[0] = g_pixel_history.first_fs[0] = g_pixel_history.last_vs[0] = g_pixel_history.last_fs[0] = 0; + for (int i = 0; i < 9; i++) g_pixel_history.att[i].texture = nil; + for (NSUInteger i = 0; i < nc && i < 8; i++) + pixel_history_add(pd.colorAttachments[i], (int)i, 0, R.rt.color[i] ? R.rt.color[i]->serial : 0); + pixel_history_add(pd.depthAttachment, -1, 1, R.rt.depth ? R.rt.depth->serial : 0); + g_pixel_history.active = g_pixel_history.attachments > 0; +} +static void pixel_history_draw(const char *vs, const char *fs) { + if (!g_pixel_history.active) return; + if (!g_pixel_history.draws++) { + snprintf(g_pixel_history.first_vs, 20, "%s", vs); snprintf(g_pixel_history.first_fs, 20, "%s", fs); + } + snprintf(g_pixel_history.last_vs, 20, "%s", vs); snprintf(g_pixel_history.last_fs, 20, "%s", fs); +} +static void pixel_history_store(void) { + if (!g_pixel_history.active || !tbdr_on()) return; // sd_finish already stores every ordinary attachment when TBDR is off. + for (int i = 0; i < g_pixel_history.attachments; i++) { + const ph_attachment *a = &g_pixel_history.att[i]; + if (a->depth) [g_enc setDepthStoreAction:MTLStoreActionStore]; + else [g_enc setColorStoreAction:MTLStoreActionStore atIndex:(NSUInteger)a->slot]; + MLOG("[pixel-history-store] frame=%llu pass=%llu slot=%d diagnostic-only Store overrides deferred policy", + (unsigned long long)g_pixel_history.frame, (unsigned long long)g_pixel_history.pass, a->slot); + } +} +static float pixel_history_ufloat(unsigned x, unsigned bits) { + unsigned m = x & ((1u << bits) - 1), e = x >> bits; + if (e == 31) return m ? NAN : INFINITY; + return e ? ldexpf(1.0f + (float)m / (float)(1u << bits), (int)e - 15) : ldexpf((float)m, 1 - 15 - (int)bits); +} +static void pixel_history_rgba(const uint8_t *b, MTLPixelFormat f, int depth, float out[4]) { + uint16_t h[4] = {0}; uint32_t w[4] = {0}; memcpy(h, b, 8); memcpy(w, b, 16); + out[0] = out[1] = out[2] = 0; out[3] = 1; + if (depth) { if (f == MTLPixelFormatDepth16Unorm) out[0] = h[0] / 65535.0f; else memcpy(out, b, 4); return; } + switch (f) { + case MTLPixelFormatR8Unorm: out[0] = b[0] / 255.0f; break; + case MTLPixelFormatR8Uint: out[0] = b[0]; break; + case MTLPixelFormatR16Unorm: out[0] = h[0] / 65535.0f; break; + case MTLPixelFormatR16Uint: out[0] = h[0]; break; + case MTLPixelFormatRG8Unorm: out[0] = b[0] / 255.0f; out[1] = b[1] / 255.0f; break; + case MTLPixelFormatR32Uint: out[0] = (float)w[0]; break; + case MTLPixelFormatR32Float: memcpy(out, b, 4); break; + case MTLPixelFormatRG32Float: memcpy(out, b, 8); break; + case MTLPixelFormatRGBA32Float: memcpy(out, b, 16); break; + case MTLPixelFormatR16Float: case MTLPixelFormatRG16Float: case MTLPixelFormatRGBA16Float: { + int n = f == MTLPixelFormatR16Float ? 1 : f == MTLPixelFormatRG16Float ? 2 : 4; + for (int i = 0; i < n; i++) { _Float16 v; memcpy(&v, h + i, 2); out[i] = (float)v; } break; } + case MTLPixelFormatRGB10A2Unorm: out[0] = (w[0] & 1023) / 1023.0f; out[1] = (w[0] >> 10 & 1023) / 1023.0f; out[2] = (w[0] >> 20 & 1023) / 1023.0f; out[3] = (w[0] >> 30) / 3.0f; break; + case MTLPixelFormatRG11B10Float: out[0] = pixel_history_ufloat(w[0] & 2047, 6); out[1] = pixel_history_ufloat(w[0] >> 11 & 2047, 6); out[2] = pixel_history_ufloat(w[0] >> 22, 5); break; + default: { + int bgra = f == MTLPixelFormatBGRA8Unorm || f == MTLPixelFormatBGRA8Unorm_sRGB; + out[0] = b[bgra ? 2 : 0] / 255.0f; out[1] = b[1] / 255.0f; out[2] = b[bgra ? 0 : 2] / 255.0f; out[3] = b[3] / 255.0f; break; } + } +} +static void pixel_history_end(void) { + if (!g_pixel_history.active) return; + g_pixel_history.active = 0; + if (!g_cb) return; + if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + ph_pixel records[27]; int n = 0; + // This tiny diagnostic buffer is owned by its completion callback, rather + // than reused through a frame arena while a CPU logger is still reading it. + id buffer = [g_dev newBufferWithLength:(size_t)g_pixel_history.attachments * g_pixel_history.points * 256 options:MTLResourceStorageModeShared]; + if (!buffer) { + MLOG("[pixel-history] readback allocation failed; pass omitted"); + for (int i = 0; i < g_pixel_history.attachments; i++) g_pixel_history.att[i].texture = nil; + return; + } + size_t off = 0; + memset(buffer.contents, 0, buffer.length); + NSMutableArray *sources = [NSMutableArray array]; id bl = enc_b_begin(g_cb); + for (int i = 0; i < g_pixel_history.attachments; i++) { + ph_attachment *a = &g_pixel_history.att[i]; id t = a->texture; [sources addObject:t]; + NSUInteger width = MAX(t.width >> a->level, 1ul), height = MAX(t.height >> a->level, 1ul); + for (int k = 0; k < g_pixel_history.points; k++) { + NSUInteger x = MIN((NSUInteger)(g_pixel_history.xy[k][0] * width), width - 1), y = MIN((NSUInteger)(g_pixel_history.xy[k][1] * height), height - 1); + ph_pixel *p = &records[n]; *p = (ph_pixel){.frame = g_pixel_history.frame, .pass = g_pixel_history.pass, .serial = a->serial, + .format = (uint32_t)t.pixelFormat, .level = (uint32_t)a->level, .slice = (uint32_t)a->slice, .plane = (uint32_t)a->plane, + .width = (uint32_t)width, .height = (uint32_t)height, .x = (uint32_t)x, .y = (uint32_t)y, .point = (uint32_t)k, + .bpp = a->bpp, .offset = (uint32_t)(n * 256), .slot = a->slot, .depth = a->depth}; + MTLBlitOption options = a->depth && t.pixelFormat == MTLPixelFormatDepth32Float_Stencil8 ? MTLBlitOptionDepthFromDepthStencil : MTLBlitOptionNone; + [bl copyFromTexture:t sourceSlice:t.textureType == MTLTextureType3D ? 0 : a->slice sourceLevel:a->level + sourceOrigin:MTLOriginMake(x, y, t.textureType == MTLTextureType3D ? a->plane : 0) sourceSize:MTLSizeMake(1, 1, 1) + toBuffer:buffer destinationOffset:off + p->offset destinationBytesPerRow:256 destinationBytesPerImage:256 options:options]; + n++; + } + a->texture = nil; + } + enc_b_end(bl); + NSData *metadata = [NSData dataWithBytes:records length:(size_t)n * sizeof *records]; + NSString *draws = [NSString stringWithFormat:@"draws=%d first_vs=%s first_fs=%s last_vs=%s last_fs=%s", g_pixel_history.draws, + g_pixel_history.first_vs, g_pixel_history.first_fs, g_pixel_history.last_vs, g_pixel_history.last_fs]; + __block NSArray *source_refs = [sources copy]; id keep = buffer; + [g_cb addCompletedHandler:^(id cb) { + if (cb.error) { MLOG("[pixel-history] GPU command failed; captured values omitted"); source_refs = nil; return; } + const ph_pixel *r = metadata.bytes; + for (int i = 0; i < n; i++) { + const uint8_t *bytes = (const uint8_t *)keep.contents + off + r[i].offset; + uint32_t raw[4] = {0}; memcpy(raw, bytes, r[i].bpp); float rgba[4]; pixel_history_rgba(bytes, (MTLPixelFormat)r[i].format, r[i].depth, rgba); + MLOG("[pixel-history] frame=%llu pass=%llu slot=%d depth=%d serial=%llu format=%u level=%u slice=%u plane=%u size=%ux%u point=%u xy=%u,%u bytes=%u raw=%08x,%08x,%08x,%08x rgba_stored=%g,%g,%g,%g %s", + (unsigned long long)r[i].frame, (unsigned long long)r[i].pass, r[i].slot, r[i].depth, (unsigned long long)r[i].serial, + r[i].format, r[i].level, r[i].slice, r[i].plane, r[i].width, r[i].height, r[i].point, r[i].x, r[i].y, r[i].bpp, + raw[0], raw[1], raw[2], raw[3], rgba[0], rgba[1], rgba[2], rgba[3], draws.UTF8String); + } + source_refs = nil; + }]; +} diff --git a/dm/gfx/nvn/nvn_prepare.h b/dm/gfx/nvn/nvn_prepare.h new file mode 100644 index 0000000000..afc9032875 --- /dev/null +++ b/dm/gfx/nvn/nvn_prepare.h @@ -0,0 +1,13 @@ +// A frontend startup gate, independent of the guest loading-screen timeout. +#pragma once +#include "nvn_pipeline_cache.h" +enum { NVN_PREP_IDLE, NVN_PREP_LOADING, NVN_PREP_WORKING, NVN_PREP_SAVING, + NVN_PREP_READY, NVN_PREP_PARTIAL, NVN_PREP_FAILED }; +typedef struct { + int state; + uint32_t total, completed, failed, skipped; + nvn_cache_status cache; +} nvn_prepare_status; +void mtl_prepare_start(void); +void mtl_prepare_snapshot(nvn_prepare_status *out); +void mtl_prepare_active(int active); diff --git a/dm/gfx/nvn/nvn_pso_skey.h b/dm/gfx/nvn/nvn_pso_skey.h index 29e152157f..755283fe99 100644 --- a/dm/gfx/nvn/nvn_pso_skey.h +++ b/dm/gfx/nvn/nvn_pso_skey.h @@ -6,7 +6,7 @@ typedef struct { const void *vs, *fs; uint32_t cfmt[8], dfmt, sfmt, samples, blend_en, chmask, a2c; blend_st blend[8]; va_st attr[16]; vs_st stream[16]; - uint8_t lmap[8]; const void *fsfn; + uint8_t lmap[8], topology; const void *fsfn; } pso_key; typedef struct { diff --git a/dm/gfx/nvn/nvn_reduction_footprint.h b/dm/gfx/nvn/nvn_reduction_footprint.h new file mode 100644 index 0000000000..8561877b19 --- /dev/null +++ b/dm/gfx/nvn/nvn_reduction_footprint.h @@ -0,0 +1,60 @@ +// Shared C/MSL coordinate math for a restricted software reduction sampler. +// Contract: normalized 2D coordinates, linear min/mag, nearest mip selection, +// CLAMP_TO_EDGE in both axes, no comparison, no anisotropy or LOD bias. +// This header is not connected to the renderer or shader translation chain. +#pragma once +#ifdef __METAL_VERSION__ +#define DM_REDUCTION_FLOOR(x) metal::floor(x) +#define DM_REDUCTION_CEIL(x) metal::ceil(x) +#else +#include +#define DM_REDUCTION_FLOOR(x) floorf(x) +#define DM_REDUCTION_CEIL(x) ceilf(x) +#endif + +typedef struct { unsigned lo, hi; float fraction; } dm_reduction_axis; +typedef struct { + dm_reduction_axis x, y; + unsigned mask; // 00, 10, 01, 11: only texels with nonzero bilinear weights +} dm_reduction_footprint; + +static inline dm_reduction_axis dm_reduction_axis_make(float uv, unsigned extent) { + // Clamp before signed conversion: negative/outside/infinite coordinates + // must never become unsigned out-of-range texture reads. NaN maps to zero, + // matching the existing translated sample expressions' UV sanitization. + if (!(uv >= 0.0f)) uv = 0.0f; + if (uv > 1.0f) uv = 1.0f; + if (!extent) extent = 1; // valid texture views always have nonzero extents + float p = uv * (float)extent - 0.5f; + float base = DM_REDUCTION_FLOOR(p); + int lo = (int)base, hi = lo + 1, last = (int)extent - 1; + dm_reduction_axis a; + a.lo = (unsigned)(lo < 0 ? 0 : lo > last ? last : lo); + a.hi = (unsigned)(hi < 0 ? 0 : hi > last ? last : hi); + a.fraction = p - base; + return a; +} + +static inline dm_reduction_footprint dm_reduction_footprint_make(float u, float v, unsigned width, unsigned height) { + dm_reduction_footprint p; + p.x = dm_reduction_axis_make(u, width); + p.y = dm_reduction_axis_make(v, height); + unsigned x = p.x.fraction > 0.0f, y = p.y.fraction > 0.0f; + p.mask = 1u | (x ? 2u : 0u) | (y ? 4u : 0u) | (x && y ? 8u : 0u); + return p; +} + +static inline unsigned dm_reduction_mip_nearest(float lod, float min_lod, float max_lod, unsigned mip_count) { + if (!(lod >= min_lod)) lod = min_lod; + if (lod > max_lod) lod = max_lod; + // OpenGL/NVIDIA nearest-mip convention: an exact half tie chooses the lower + // level. The observed Z-A uses only integer explicit LODs; fractional NVN + // tie/quantization behavior needs a separate hardware fixture before use. + float mip = DM_REDUCTION_CEIL(lod + 0.5f) - 1.0f; + if (!(mip > 0.0f) || mip_count <= 1) return 0; + if (mip >= (float)(mip_count - 1)) return mip_count - 1; + return (unsigned)mip; +} + +#undef DM_REDUCTION_FLOOR +#undef DM_REDUCTION_CEIL diff --git a/dm/gfx/nvn/nvn_reduction_sample.metal.h b/dm/gfx/nvn/nvn_reduction_sample.metal.h new file mode 100644 index 0000000000..4d9612bed0 --- /dev/null +++ b/dm/gfx/nvn/nvn_reduction_sample.metal.h @@ -0,0 +1,26 @@ +// Standalone prototype; no game shaders or renderer paths include this yet. +// Only use after validating the sampler contract in nvn_reduction_footprint.h. +#pragma once +#include "nvn_reduction_footprint.h" + +inline float4 dm_reduction_sample_lmn_edge( + texture2d tex, float2 uv, float lod, + float min_lod, float max_lod, bool maximum) +{ + uint mip = dm_reduction_mip_nearest(lod, min_lod, max_lod, tex.get_num_mip_levels()); + dm_reduction_footprint p = dm_reduction_footprint_make(uv.x, uv.y, tex.get_width(mip), tex.get_height(mip)); + float4 value = tex.read(uint2(p.x.lo, p.y.lo), mip); + if (p.mask & 2u) { + float4 tap = tex.read(uint2(p.x.hi, p.y.lo), mip); + value = maximum ? max(value, tap) : min(value, tap); + } + if (p.mask & 4u) { + float4 tap = tex.read(uint2(p.x.lo, p.y.hi), mip); + value = maximum ? max(value, tap) : min(value, tap); + } + if (p.mask & 8u) { + float4 tap = tex.read(uint2(p.x.hi, p.y.hi), mip); + value = maximum ? max(value, tap) : min(value, tap); + } + return value; +} diff --git a/dm/gfx/nvn/nvn_refl.h b/dm/gfx/nvn/nvn_refl.h index 6a58a3bb4d..f48076b17f 100644 --- a/dm/gfx/nvn/nvn_refl.h +++ b/dm/gfx/nvn/nvn_refl.h @@ -7,9 +7,10 @@ #include enum { NVN_RB_CBUF = 1, NVN_RB_SAMP, NVN_RB_TEX, NVN_RB_IMG, NVN_RB_TBUF, - NVN_RB_SBUFP, NVN_RB_SUPPORT, NVN_RB_C1, NVN_RB_DRV0, NVN_RB_DRV2, NVN_RB_SAMPT, NVN_RB_UBOP, NVN_RB_TEXU, NVN_RB_SAMPU, NVN_RB_SAMPI, NVN_RB_SBUFU, NVN_RB_TEXD, NVN_RB_SAMPD, NVN_RB_TEXA, NVN_RB_SAMPA, NVN_RB_GEOMETRY }; + NVN_RB_SBUFP, NVN_RB_SUPPORT, NVN_RB_C1, NVN_RB_DRV0, NVN_RB_DRV2, NVN_RB_SAMPT, NVN_RB_UBOP, NVN_RB_TEXU, NVN_RB_SAMPU, NVN_RB_SAMPI, NVN_RB_SBUFU, NVN_RB_TEXD, NVN_RB_SAMPD, NVN_RB_IMGA, NVN_RB_TEXA, NVN_RB_SAMPA, NVN_RB_GEOMETRY }; // TEXA / SAMPA: an array of n handles in the driver table c2 that the shader indexes at run time (dmsh_build.py c2_array): element k = the texture / sampler of the slot whose c2 word lies // at byte off + 8k (combined, else separate); words outside both tables give the dummy. Luigi's Mansion 3's shadow cascades +// IMGA: SPIRV-Cross's MSL 2.3 image atomics: a device atomic_uint / atomic_int pointer (_atomic) beside the image of NVN image slot r; the replay binds the image's storage address // TEXD / SAMPD: Ryujinx's fallback for a texture handle it cannot trace to one slot (handle type 4, __cb0_40000000): an array of n textures / samplers indexed by // the handle's texture / sampler ID (capped at n - 1); the replay binds slot k at element k and puts slot indices in c2 (profile key tex_dyn_slots, Octopath Traveler 0) // SAMPI: the sampler ID is a constant in the shader (reg = the ID; Ryujinx's SeparateConstantSamplerHandle: the handle ORs a constant sampler into a bound texture, Monster Hunter Rise) diff --git a/dm/gfx/nvn/nvn_resource_usage.h b/dm/gfx/nvn/nvn_resource_usage.h new file mode 100644 index 0000000000..b91ad04268 --- /dev/null +++ b/dm/gfx/nvn/nvn_resource_usage.h @@ -0,0 +1,64 @@ +// Submit-thread per-encoder resource declarations. Dedup is optional; preserving +// the union of access flags is mandatory, including collisions and overflow. +#pragma once +#include +#include +#include +#include +#ifndef NVN_RESOURCE_USAGE_REALLOC +#define NVN_RESOURCE_USAGE_REALLOC realloc +#define NVN_RESOURCE_USAGE_LOCAL_REALLOC 1 +#endif +typedef struct { uintptr_t resource; uint64_t usage, epoch; } nvn_resource_usage_entry; +typedef struct { + nvn_resource_usage_entry fast[512]; + nvn_resource_usage_entry *overflow; + size_t overflow_n, overflow_cap; + uint64_t epoch; +} nvn_resource_usage_set; +static inline void nvn_resource_usage_begin(nvn_resource_usage_set *s) { + if (!++s->epoch) { memset(s->fast, 0, sizeof s->fast); s->epoch = 1; } + s->overflow_n = 0; +} +static inline void nvn_resource_usage_dispose(nvn_resource_usage_set *s) { + free(s->overflow); memset(s, 0, sizeof *s); +} +static inline int nvn_resource_usage_add(nvn_resource_usage_set *s, uintptr_t resource, + uint64_t requested, int dedup, uint64_t *combined, int *skip) { + nvn_resource_usage_entry *entry = NULL; + size_t at = (resource >> 4) & 511; + for (unsigned probe = 0; probe < 16; probe++, at = (at + 1) & 511) { + nvn_resource_usage_entry *e = &s->fast[at]; + if (e->epoch != s->epoch) { + *e = (nvn_resource_usage_entry){.resource = resource, .epoch = s->epoch}; + entry = e; break; + } + if (e->resource == resource) { entry = e; break; } + } + if (!entry) { + // No eviction: a collision must never forget a previous Write. Slots + // are not removed within an epoch, so an overflow key remains here. + for (size_t i = 0; i < s->overflow_n; i++) if (s->overflow[i].resource == resource) { + entry = &s->overflow[i]; break; + } + if (!entry) { + if (s->overflow_n == s->overflow_cap) { + size_t cap = s->overflow_cap ? s->overflow_cap * 2 : 32; + if (cap < s->overflow_cap || cap > SIZE_MAX / sizeof *s->overflow) return 0; + void *p = NVN_RESOURCE_USAGE_REALLOC(s->overflow, cap * sizeof *s->overflow); + if (!p) return 0; + s->overflow = p; s->overflow_cap = cap; + } + entry = &s->overflow[s->overflow_n++]; + *entry = (nvn_resource_usage_entry){.resource = resource, .epoch = s->epoch}; + } + } + uint64_t previous = entry->usage; + entry->usage |= requested; *combined = entry->usage; + *skip = dedup && previous == entry->usage && previous != 0; + return 1; +} +#ifdef NVN_RESOURCE_USAGE_LOCAL_REALLOC +#undef NVN_RESOURCE_USAGE_LOCAL_REALLOC +#undef NVN_RESOURCE_USAGE_REALLOC +#endif diff --git a/dm/gfx/nvn/nvn_rtex.inc b/dm/gfx/nvn/nvn_rtex.inc new file mode 100644 index 0000000000..6134428db2 --- /dev/null +++ b/dm/gfx/nvn/nvn_rtex.inc @@ -0,0 +1,157 @@ +// +// int rt_ex_encode(dst_hdr, dst_ds, src_hdr, src_ds, vp_prev[16], vp_cur[16], unit_bits, ui_bits) +// nvn_rtex.inc - shaders for runtime extrapolation (#included by nvn_mtl.m after the definitions of rp_end / m4_end / res_add / args_dirty_all / blit etc.) +// int rt_ex_encode(dst_hdr, dst_ds, src_hdr, src_ds, vp_prev[16], vp_cur[16], unit_bits, ui_bits) +// Encodes one render pass (a single fullscreen triangle) on the current MTL4 command buffer g_cb, extrapolating the opaque HDR and depth of the previous fully drawn frame into dst_hdr / dst_ds by this frame's camera: +// Backward mapping: destination pixel q → source pixel p, with 2 fixed-point iterations using the source depth; M = inv(vp_prev) · vp_cur is computed on the CPU in double precision (with translations on the order of 1e4 a single-precision inverse matrix would be off by 0.01 pixel). +// Non-hole: five-tap Catmull-Rom, clamped to the min / max of the 2×2 core texels against ringing, then sharpening compensation by the fractional phase of the displacement (strongest at phase 0.5, 0 at integer phase, strength k = 0.5). +// Hole (the stencil of the source 2×2 core contains unit_bits | ui_bits: old unit position / UI cut-out): along 8 directions search outward every 4 pixels for the first pixel that is neither a hole nor pure black (at most 24 steps), +// weighted average by 1/distance²; depth takes the nearest of them. Holes and pixels newly exposed at the screen edge (source out of bounds) get stencil 0x80 exported by the fragment, and the later regional post-processing is recomputed accordingly. +// Pixels covered by this frame's UI (the UI stencil pass already wrote 0x2) are skipped by the stencil test. +// Returns 1 = encoded; 0 = size mismatch / pipeline cannot be built (the caller rt_copy_prev prechecks with rx_setup first, and if it cannot be built the whole run falls back to the original drawing method). +// Before and after the call: this function first calls rp_end() to close the current render pass and copy encoder; after encoding it sets args_dirty_all() / g_pso_dirty / g_dss_dirty, and the game's next draw reopens its pass with 'load'. +// Resources: src_hdr / src_ds are created and res_add'ed by the caller; src_ds must be able to open an X32_Stencil8 view (created with MTLTextureUsagePixelFormatView). +static id g_rx_pso1; static MTLPixelFormat g_rx_fmt_c, g_rx_fmt_ds; +static id g_rx_dss; +static id g_rx_const[3]; static int g_rx_ci; +static id g_rx_stv; static __unsafe_unretained id g_rx_stv_src; +static const char *k_rx_msl = +"#include \n" +"using namespace metal;\n" +"struct VO { float4 p [[position]]; };\n" +"vertex VO rx_vs(uint id [[vertex_id]]) { float2 uv = float2((id << 1) & 2, id & 2); VO o; o.p = float4(uv * float2(2, -2) + float2(-1, 1), 0, 1); return o; }\n" +"struct K { float4x4 M; uint hole; float amount; float2 pad; };\n" +"static float4 cr_w(float t) { return float4(((-0.5 * t + 1.0) * t - 0.5) * t, (1.5 * t - 2.5) * t * t + 1.0, ((-1.5 * t + 2.0) * t + 0.5) * t, (0.5 * t - 0.5) * t * t); }\n" +"static uint stv(texture2d s, int2 p) { uint4 v = s.read(uint2(p)); return v.r | v.g; }\n" // X32_Stencil8 view: which component holds the stencil value is implementation-dependent, take both r | g (the other one is 0) +"static float zproj(float4x4 M, float2 p, float z, int W, int H) { float4 c = M * float4((p.x + 0.5) / W * 2 - 1, 1 - (p.y + 0.5) / H * 2, z, 1); return c.z / c.w; }\n" +"struct O1 { float4 c [[color(0)]]; float d [[depth(any)]]; uint s [[stencil]]; };\n" +"fragment O1 rx_fs1(VO in [[stage_in]], texture2d hdr [[texture(0)]], depth2d dep [[texture(1)]], texture2d stc [[texture(2)]], constant K& k [[buffer(0)]]) {\n" +" constexpr sampler ls(filter::linear, address::clamp_to_edge, coord::pixel);\n" +" int W = hdr.get_width(), H = hdr.get_height(); int2 hi = int2(W - 1, H - 1);\n" +" float2 q = floor(in.p.xy); float2 p = q;\n" +" for (int it = 0; it < 2; it++) {\n" +" int2 pi = clamp(int2(rint(p)), int2(0), hi); float z = dep.read(uint2(pi));\n" +" float4 c = k.M * float4((p.x + 0.5) / W * 2 - 1, 1 - (p.y + 0.5) / H * 2, z, 1);\n" +" float2 qq = float2((c.x / c.w + 1) * 0.5 * W - 0.5, (1 - c.y / c.w) * 0.5 * H - 0.5); p += q - qq; }\n" +" bool oob = p.x < -0.5 || p.y < -0.5 || p.x > float(hi.x) + 0.5 || p.y > float(hi.y) + 0.5;\n" +" p = clamp(p, float2(0), float2(hi));\n" +" int2 b0 = clamp(int2(floor(p)), int2(0), hi - 1);\n" +" uint s4 = stv(stc, b0) | stv(stc, b0 + int2(1, 0)) | stv(stc, b0 + int2(0, 1)) | stv(stc, b0 + int2(1, 1));\n" +" int2 cp = clamp(int2(rint(p)), int2(0), hi);\n" +" O1 o; o.s = ((s4 & k.hole) || oob) ? 0x80u : 0u;\n" +" if (!(s4 & k.hole)) {\n" +" float3 t00 = hdr.read(uint2(b0)).rgb, t10 = hdr.read(uint2(b0 + int2(1, 0))).rgb, t01 = hdr.read(uint2(b0 + int2(0, 1))).rgb, t11 = hdr.read(uint2(b0 + int2(1, 1))).rgb;\n" +" float3 lo = min(min(t00, t10), min(t01, t11)), hc = max(max(t00, t10), max(t01, t11)), avg = (t00 + t10 + t01 + t11) * 0.25;\n" +" float2 sp = p + 0.5; float2 tp1 = floor(sp - 0.5) + 0.5; float2 f = sp - tp1;\n" // Five-tap Catmull-Rom (bilinear sampling merges the two middle taps, the four corners are dropped) +" float2 w0 = f * (-0.5 + f * (1.0 - 0.5 * f)), w1 = 1.0 + f * f * (-2.5 + 1.5 * f), w2 = f * (0.5 + f * (2.0 - 1.5 * f)), w3 = f * f * (-0.5 + 0.5 * f);\n" +" float2 w12 = w1 + w2, t12 = tp1 + w2 / w12, t0 = tp1 - 1.0, t3 = tp1 + 2.0;\n" +" float3 acc = hdr.sample(ls, float2(t12.x, t0.y)).rgb * (w12.x * w0.y) + hdr.sample(ls, float2(t0.x, t12.y)).rgb * (w0.x * w12.y) + hdr.sample(ls, t12).rgb * (w12.x * w12.y)\n" +" + hdr.sample(ls, float2(t3.x, t12.y)).rgb * (w3.x * w12.y) + hdr.sample(ls, float2(t12.x, t3.y)).rgb * (w12.x * w3.y);\n" +" float wt = w12.x * w0.y + w0.x * w12.y + w12.x * w12.y + w3.x * w12.y + w12.x * w3.y; float3 cr = clamp(acc / wt, lo, hc);\n" +" o.c = float4(max(cr + k.amount * (cr - avg), 0.0), 1); o.d = zproj(k.M, p, dep.read(uint2(cp)), W, H); return o; }\n" // Sharpening compensation by fractional phase is folded into the same pass (the neighborhood takes the mean of the 2×2 core) +" const int2 dirs[8] = { int2(1, 0), int2(-1, 0), int2(0, 1), int2(0, -1), int2(1, 1), int2(-1, 1), int2(1, -1), int2(-1, -1) };\n" +" float3 sum = 0; float wsum = 0; int bestr = 1000; int2 bests = cp;\n" +" for (int d = 0; d < 8; d++) for (int r = 1; r <= 24; r++) { int2 s = cp + dirs[d] * (4 * r);\n" +" if (s.x < 0 || s.y < 0 || s.x > hi.x || s.y > hi.y) break;\n" +" if (stv(stc, s) & k.hole) continue;\n" +" float3 v = hdr.read(uint2(s)).rgb; if (max(v.r, max(v.g, v.b)) <= 0) continue;\n" +" float w = 1.0 / float(r * r); sum += v * w; wsum += w; if (r < bestr) { bestr = r; bests = s; } break; }\n" +" if (wsum > 0) { o.c = float4(sum / wsum, 1); o.d = zproj(k.M, float2(bests), dep.read(uint2(bests)), W, H); return o; }\n" +" o.c = float4(hdr.read(uint2(cp)).rgb, 1); o.d = zproj(k.M, p, dep.read(uint2(cp)), W, H); return o;\n" +"}\n"; +static int rx_setup(MTLPixelFormat fc, MTLPixelFormat fds) { + if (getenv("DM_RT_TEST_NOPSO")) return 0; // Test: pretend this device cannot build the extrapolation pipeline (verifies the whole run falls back to the original drawing method) + if (g_rx_pso1 && g_rx_fmt_c == fc && g_rx_fmt_ds == fds) return 1; + static int failed; if (failed) return 0; + NSError *e = nil; id lib = [g_dev newLibraryWithSource:@(k_rx_msl) options:nil error:&e]; + if (!lib) { MLOG("【红】运行时外推:着色器编译失败 %s", e.localizedDescription.UTF8String); failed = 1; return 0; } + MTLRenderPipelineDescriptor *pd = [MTLRenderPipelineDescriptor new]; pd.vertexFunction = [lib newFunctionWithName:@"rx_vs"]; pd.fragmentFunction = [lib newFunctionWithName:@"rx_fs1"]; + pd.colorAttachments[0].pixelFormat = fc; pd.depthAttachmentPixelFormat = fds; + if (fds == MTLPixelFormatDepth32Float_Stencil8) pd.stencilAttachmentPixelFormat = fds; + g_rx_pso1 = [g_dev newRenderPipelineStateWithDescriptor:pd error:&e]; + if (!g_rx_pso1) { MLOG("【红】运行时外推:管线建不出 %s", e.localizedDescription.UTF8String); g_rx_pso1 = nil; failed = 1; return 0; } + MTLDepthStencilDescriptor *dd = [MTLDepthStencilDescriptor new]; dd.depthCompareFunction = MTLCompareFunctionAlways; dd.depthWriteEnabled = YES; + if (fds == MTLPixelFormatDepth32Float_Stencil8) { MTLStencilDescriptor *sd = [MTLStencilDescriptor new]; sd.stencilCompareFunction = MTLCompareFunctionEqual; sd.readMask = 0x2; sd.writeMask = 0x80; sd.depthStencilPassOperation = MTLStencilOperationReplace; // Hole pixels / pixels newly entering from off-screen get 0x80 exported by the fragment, and the later LDR region is recomputed accordingly // Pixels covered by this frame's UI (the UI stencil pass already wrote 0x2) do not count + dd.frontFaceStencil = sd; dd.backFaceStencil = sd; } + g_rx_dss = [g_dev newDepthStencilStateWithDescriptor:dd]; + if (!g_rx_const[0]) { + for (int i = 0; i < 3; i++) { + g_rx_const[i] = [g_dev newBufferWithLength:256 options:MTLResourceStorageModeShared]; + if (!g_rx_const[i]) { failed = 1; g_rx_pso1 = nil; return 0; } + res_add(g_rx_const[i]); + } + } + if (!g_rx_dss) { g_rx_pso1 = nil; failed = 1; return 0; } + g_rx_fmt_c = fc; g_rx_fmt_ds = fds; MLOG("运行时外推:着色器建好(颜色格式 %lu,深度格式 %lu)", (unsigned long)fc, (unsigned long)fds); + return 1; +} +static void rx_inv4(const double *m, double *o) { // 4×4 inverse (adjugate method, double precision) + double inv[16]; + inv[0] = m[5]*m[10]*m[15] - m[5]*m[11]*m[14] - m[9]*m[6]*m[15] + m[9]*m[7]*m[14] + m[13]*m[6]*m[11] - m[13]*m[7]*m[10]; + inv[4] = -m[4]*m[10]*m[15] + m[4]*m[11]*m[14] + m[8]*m[6]*m[15] - m[8]*m[7]*m[14] - m[12]*m[6]*m[11] + m[12]*m[7]*m[10]; + inv[8] = m[4]*m[9]*m[15] - m[4]*m[11]*m[13] - m[8]*m[5]*m[15] + m[8]*m[7]*m[13] + m[12]*m[5]*m[11] - m[12]*m[7]*m[9]; + inv[12] = -m[4]*m[9]*m[14] + m[4]*m[10]*m[13] + m[8]*m[5]*m[14] - m[8]*m[6]*m[13] - m[12]*m[5]*m[10] + m[12]*m[6]*m[9]; + inv[1] = -m[1]*m[10]*m[15] + m[1]*m[11]*m[14] + m[9]*m[2]*m[15] - m[9]*m[3]*m[14] - m[13]*m[2]*m[11] + m[13]*m[3]*m[10]; + inv[5] = m[0]*m[10]*m[15] - m[0]*m[11]*m[14] - m[8]*m[2]*m[15] + m[8]*m[3]*m[14] + m[12]*m[2]*m[11] - m[12]*m[3]*m[10]; + inv[9] = -m[0]*m[9]*m[15] + m[0]*m[11]*m[13] + m[8]*m[1]*m[15] - m[8]*m[3]*m[13] - m[12]*m[1]*m[11] + m[12]*m[3]*m[9]; + inv[13] = m[0]*m[9]*m[14] - m[0]*m[10]*m[13] - m[8]*m[1]*m[14] + m[8]*m[2]*m[13] + m[12]*m[1]*m[10] - m[12]*m[2]*m[9]; + inv[2] = m[1]*m[6]*m[15] - m[1]*m[7]*m[14] - m[5]*m[2]*m[15] + m[5]*m[3]*m[14] + m[13]*m[2]*m[7] - m[13]*m[3]*m[6]; + inv[6] = -m[0]*m[6]*m[15] + m[0]*m[7]*m[14] + m[4]*m[2]*m[15] - m[4]*m[3]*m[14] - m[12]*m[2]*m[7] + m[12]*m[3]*m[6]; + inv[10] = m[0]*m[5]*m[15] - m[0]*m[7]*m[13] - m[4]*m[1]*m[15] + m[4]*m[3]*m[13] + m[12]*m[1]*m[7] - m[12]*m[3]*m[5]; + inv[14] = -m[0]*m[5]*m[14] + m[0]*m[6]*m[13] + m[4]*m[1]*m[14] - m[4]*m[2]*m[13] - m[12]*m[1]*m[6] + m[12]*m[2]*m[5]; + inv[3] = -m[1]*m[6]*m[11] + m[1]*m[7]*m[10] + m[5]*m[2]*m[11] - m[5]*m[3]*m[10] - m[9]*m[2]*m[7] + m[9]*m[3]*m[6]; + inv[7] = m[0]*m[6]*m[11] - m[0]*m[7]*m[10] - m[4]*m[2]*m[11] + m[4]*m[3]*m[10] + m[8]*m[2]*m[7] - m[8]*m[3]*m[6]; + inv[11] = -m[0]*m[5]*m[11] + m[0]*m[7]*m[9] + m[4]*m[1]*m[11] - m[4]*m[3]*m[9] - m[8]*m[1]*m[7] + m[8]*m[3]*m[5]; + inv[15] = m[0]*m[5]*m[10] - m[0]*m[6]*m[9] - m[4]*m[1]*m[10] + m[4]*m[2]*m[9] + m[8]*m[1]*m[6] - m[8]*m[2]*m[5]; + double det = m[0]*inv[0] + m[1]*inv[4] + m[2]*inv[8] + m[3]*inv[12]; det = det != 0 ? 1.0 / det : 0; + for (int i = 0; i < 16; i++) o[i] = inv[i] * det; +} +int rt_ex_encode(id dst_hdr, id dst_ds, id src_hdr, id src_ds, + const float vp_prev[16], const float vp_cur[16], uint32_t unit_bits, uint32_t ui_bits) { + if (!dst_hdr || !dst_ds || !src_hdr || !src_ds || !vp_prev || !vp_cur || !g_cb) return 0; + if (src_hdr.width != dst_hdr.width || src_hdr.height != dst_hdr.height || src_ds.width != src_hdr.width || src_ds.height != src_hdr.height + || dst_ds.width != dst_hdr.width || dst_ds.height != dst_hdr.height) return 0; // Resolution changed (loading screen / dynamic scale): no extrapolation this frame + if (!rx_setup(dst_hdr.pixelFormat, dst_ds.pixelFormat)) return 0; + if (g_rx_stv_src != src_ds) { + if (g_rx_stv) defer_obj_release(g_rx_stv); + g_rx_stv = [src_ds newTextureViewWithPixelFormat:MTLPixelFormatX32_Stencil8]; + if (!g_rx_stv) { static int n; if (!n++) MLOG("【红】运行时外推:开不出模板视图(src_ds 要带 PixelFormatView 用途)"); return 0; } + res_add(g_rx_stv); g_rx_stv_src = src_ds; + } + // Constant: M = inv(vp_prev) · vp_cur (row vectors: clip_cur = (ndc_prev, z, 1) · M); Metal's float4x4 is stored by column, and row i of the row-vector matrix serves exactly as column i ⇒ write M * v in the shader + double a[16], b[16], ia[16], m[16]; + for (int i = 0; i < 16; i++) { a[i] = vp_prev[i]; b[i] = vp_cur[i]; } + rx_inv4(a, ia); + for (int r = 0; r < 4; r++) for (int c = 0; c < 4; c++) { double s = 0; for (int k = 0; k < 4; k++) s += ia[r * 4 + k] * b[k * 4 + c]; m[r * 4 + c] = s; } + // Fractional phase: pixel displacement on screen between the two frames of the world origin (any point works; orthographic + translation is uniform across the screen), d = destination − source; the source of the backward mapping = destination − d + double W = dst_hdr.width, H = dst_hdr.height; + double c0x = m[12] / m[15], c0y = m[13] / m[15]; // Source NDC origin (0, 0, 0, 1) projected into this frame + double dx = c0x / 2 * W, dy = -c0y / 2 * H, sx = -dx, sy = -dy; // Offset of the source relative to the destination (pixels) + double fx = sx - floor(sx), fy = sy - floor(sy), amount = 0.5 * 2 * (fx * (1 - fx) + fy * (1 - fy)); + float *k = g_rx_const[g_rx_ci].contents; + for (int i = 0; i < 16; i++) k[i] = (float)m[i]; + ((uint32_t *)k)[16] = unit_bits | ui_bits; k[17] = (float)amount; + id cbuf = g_rx_const[g_rx_ci]; g_rx_ci = (g_rx_ci + 1) % 3; + rp_end(); if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + // Single pass: extrapolation + hole filling + sharpening compensation by fractional phase → dst_hdr; depth is written into dst_ds; pixels covered by this frame's UI are skipped by stencil (stencil not written) + MTLRenderPassDescriptor *rp = [MTLRenderPassDescriptor new]; + rp.colorAttachments[0].texture = dst_hdr; rp.colorAttachments[0].loadAction = MTLLoadActionLoad; rp.colorAttachments[0].storeAction = MTLStoreActionStore; + rp.depthAttachment.texture = dst_ds; rp.depthAttachment.loadAction = MTLLoadActionLoad; rp.depthAttachment.storeAction = MTLStoreActionStore; + if (dst_ds.pixelFormat == MTLPixelFormatDepth32Float_Stencil8) { rp.stencilAttachment.texture = dst_ds; rp.stencilAttachment.loadAction = MTLLoadActionLoad; rp.stencilAttachment.storeAction = MTLStoreActionStore; } + id en = [g_cb renderCommandEncoderWithDescriptor:rp]; if (!en) return 0; + [en setFragmentTexture:src_hdr atIndex:0]; [en setFragmentTexture:src_ds atIndex:1]; [en setFragmentTexture:g_rx_stv atIndex:2]; + [en setFragmentBuffer:cbuf offset:0 atIndex:0]; + [en setViewport:(MTLViewport){0, 0, W, H, 0, 1}]; [en setRenderPipelineState:g_rx_pso1]; [en setDepthStencilState:g_rx_dss]; [en setStencilReferenceValue:0]; + [en drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3]; m4_end(en); + args_dirty_all(); g_pso_dirty = g_dss_dirty = 1; + return 1; +} + +// Whole-image translation in pixels between two frames under an orthographic camera (rounded): destination = source + (dx, dy) +static void rt_shift_px(const float vp_prev[16], const float vp_cur[16], double W, double H, int *dx, int *dy) { + double a[16], b[16], ia[16], m[16]; + for (int i = 0; i < 16; i++) { a[i] = vp_prev[i]; b[i] = vp_cur[i]; } + rx_inv4(a, ia); + for (int r = 0; r < 4; r++) for (int c = 0; c < 4; c++) { double s = 0; for (int k = 0; k < 4; k++) s += ia[r * 4 + k] * b[k * 4 + c]; m[r * 4 + c] = s; } + *dx = (int)lround(m[12] / m[15] / 2 * W); *dy = (int)lround(-m[13] / m[15] / 2 * H); +} diff --git a/dm/gfx/nvn/nvn_sampler_limit.h b/dm/gfx/nvn/nvn_sampler_limit.h new file mode 100644 index 0000000000..0e50b20f90 --- /dev/null +++ b/dm/gfx/nvn/nvn_sampler_limit.h @@ -0,0 +1,150 @@ +#pragma once +// Metal Tier 1 argument buffers expose at most 16 indirect samplers. A +// companion generated by export_sampler_limit_sources.py may remove only scalar +// sampler declarations proven unused across the complete source. Argument IDs +// are never renumbered; this helper removes the same bindings from the runtime +// reflection before MTLArgumentEncoder is created. + +static NSInteger sampler_limit_weight(NSDictionary *binding) { + NSString *type = [binding isKindOfClass:NSDictionary.class] ? binding[@"msl_type"] : nil; + if (![type isKindOfClass:NSString.class]) return 0; + if ([type isEqualToString:@"sampler"]) return 1; + NSString *compact = [[type componentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet] + componentsJoinedByString:@""]; + if (![compact hasPrefix:@"array"]) return 0; + NSString *count = [compact substringWithRange:NSMakeRange(14, compact.length - 15)]; + NSScanner *scanner = [NSScanner scannerWithString:count]; + NSInteger value = 0; + return [scanner scanInteger:&value] && scanner.isAtEnd && value > 0 ? value : -1; +} + +static NSInteger sampler_limit_count(NSArray *bindings) { + if (![bindings isKindOfClass:NSArray.class]) return -1; + NSInteger total = 0; + for (id binding in bindings) { + if (![binding isKindOfClass:NSDictionary.class]) return -1; + NSInteger weight = sampler_limit_weight(binding); + if (weight < 0 || total > NSIntegerMax - weight) return -1; + total += weight; + } + return total; +} + +static NSDictionary *sampler_limit_binding_record(NSDictionary *binding) { + NSMutableDictionary *record = [NSMutableDictionary dictionary]; + for (NSString *field in @[@"msl_name", @"msl_type", @"arg_id", @"kind"]) { + if (!binding[field]) return nil; + record[field] = binding[field]; + } + if (binding[@"reg"]) record[@"reg"] = binding[@"reg"]; + return record; +} + +static id sampler_limit_library(NSString *key, id original, + NSData *reflection, NSDictionary *reflection_json, int already_specialized, + NSDictionary **lowered_reflection, int *variant, NSError **error) { + enum { limit = 16 }; + *lowered_reflection = reflection_json; + *variant = 0; + NSArray *bindings = reflection_json[@"bindings"]; + NSInteger original_count = sampler_limit_count(bindings); + if (original_count >= 0 && original_count <= limit) return original; + + static NSDictionary *manifest; + static dispatch_once_t once; + NSString *directory = [g_msl_dir stringByAppendingPathComponent:@"sampler-limit-v1"]; + dispatch_once(&once, ^{ + NSData *data = [NSData dataWithContentsOfFile:[directory stringByAppendingPathComponent:@"manifest.json"]]; + manifest = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil; + }); + + NSString *reason = nil; + if (original_count < 0) reason = @"Invalid shader reflection while counting sampler resources"; + else if (already_specialized) reason = @"Sampler pruning cannot be composed with another source companion"; + else if (![manifest[@"schema"] isEqual:@1] || ![manifest[@"chain"] isEqual:@DMSH_CHAIN] || + ![manifest[@"kind"] isEqual:@"sampler-limit-v1"] || ![manifest[@"limit"] isEqual:@(limit)]) + reason = @"Missing or mismatched sampler-limit-v1 companion manifest"; + NSDictionary *record = reason ? nil : manifest[@"shaders"][key]; + if (!reason && (![record isKindOfClass:NSDictionary.class] || ![record[@"supported"] isEqual:@YES] || + ![record[@"proof"] isEqual:@"unused-sampler-declaration-v1"] || ![record[@"limit"] isEqual:@(limit)])) + reason = [@"Missing or unproven sampler-limit companion: " stringByAppendingString: + [record[@"reason"] isKindOfClass:NSString.class] ? record[@"reason"] : @"no authorization record"]; + if (!reason && (![record[@"original_sampler_count"] isEqual:@(original_count)] || + ![record[@"remaining_sampler_count"] isKindOfClass:NSNumber.class] || + [record[@"remaining_sampler_count"] integerValue] < 0 || [record[@"remaining_sampler_count"] integerValue] > limit)) + reason = @"Sampler-limit resource counts do not match reflection"; + + NSString *name = reason ? nil : record[@"source"]; + NSString *expected_name = [key stringByAppendingString:@".metal"]; + if (!reason && (![name isKindOfClass:NSString.class] || ![name isEqualToString:expected_name] || + ![name isEqualToString:name.lastPathComponent])) reason = @"Invalid sampler-limit source path"; + + NSMutableArray *pruned = nil; + NSArray *removed = reason ? nil : record[@"removed"]; + if (!reason && (![removed isKindOfClass:NSArray.class] || removed.count != + (NSUInteger)(original_count - [record[@"remaining_sampler_count"] integerValue]))) + reason = @"Sampler-limit removal list has the wrong length"; + if (!reason) { + pruned = [NSMutableArray arrayWithCapacity:bindings.count]; + NSMutableSet *removed_names = [NSMutableSet set], *removed_ids = [NSMutableSet set]; + for (NSDictionary *binding in bindings) { + NSDictionary *binding_record = sampler_limit_binding_record(binding); + NSUInteger index = binding_record ? [removed indexOfObject:binding_record] : NSNotFound; + if (index == NSNotFound) { + [pruned addObject:binding]; + continue; + } + NSString *binding_name = binding[@"msl_name"]; + NSNumber *binding_id = binding[@"arg_id"]; + NSString *kind = binding[@"kind"]; + if (sampler_limit_weight(binding) != 1 || ![kind isKindOfClass:NSString.class] || + ![kind hasPrefix:@"samp"] || [removed_names containsObject:binding_name] || + [removed_ids containsObject:binding_id]) { + reason = @"Sampler-limit removal list does not identify unique scalar samplers"; + break; + } + [removed_names addObject:binding_name]; + [removed_ids addObject:binding_id]; + } + if (!reason && removed_names.count != removed.count) + reason = @"Sampler-limit removal list does not match reflection"; + if (!reason && sampler_limit_count(pruned) != [record[@"remaining_sampler_count"] integerValue]) + reason = @"Pruned sampler count does not match the companion manifest"; + } + + NSData *source = reason ? nil : [NSData dataWithContentsOfFile:[directory stringByAppendingPathComponent:name]]; + NSData *base = reason ? nil : [NSData dataWithContentsOfFile: + [g_msl_dir stringByAppendingPathComponent:[key stringByAppendingString:@".metallib"]] + options:NSDataReadingMappedIfSafe error:nil]; + if (!reason && (![record[@"source_sha256"] isEqual:nonpoint_digest(source)] || + ![record[@"base_library_sha256"] isEqual:nonpoint_digest(base)] || + ![record[@"reflection_sha256"] isEqual:nonpoint_digest(reflection)])) + reason = @"Sampler-limit source, base library or reflection hash mismatch"; + if (reason) { + if (error) *error = [NSError errorWithDomain:@"NVNSamplerLimit" code:1 + userInfo:@{NSLocalizedDescriptionKey:reason}]; + return nil; + } + + NSString *text = [[NSString alloc] initWithData:source encoding:NSUTF8StringEncoding]; + if (!text) { + if (error) *error = [NSError errorWithDomain:@"NVNSamplerLimit" code:2 + userInfo:@{NSLocalizedDescriptionKey:@"Sampler-limit source is not valid UTF-8"}]; + return nil; + } + MTLCompileOptions *options = [MTLCompileOptions new]; + options.languageVersion = MTLLanguageVersion3_0; + options.preserveInvariance = YES; + options.fastMathEnabled = NO; + id library = [g_dev newLibraryWithSource:text options:options error:error]; + if (library) { + NSMutableDictionary *json = [reflection_json mutableCopy]; + json[@"bindings"] = pruned; + *lowered_reflection = json; + *variant = 1; + MLOG("Sampler-limit shader %s: pruned %lu statically unused sampler argument%s (%ld -> %ld)", + key.UTF8String, (unsigned long)removed.count, removed.count == 1 ? "" : "s", + (long)original_count, (long)[record[@"remaining_sampler_count"] integerValue]); + } + return library; +} diff --git a/dm/gfx/nvn/nvn_storage_access.h b/dm/gfx/nvn/nvn_storage_access.h new file mode 100644 index 0000000000..6991443c46 --- /dev/null +++ b/dm/gfx/nvn/nvn_storage_access.h @@ -0,0 +1,78 @@ +// Conservative promotion of storage-buffer write declarations from compiled +// argument-buffer reflection. No game keys, shader rewriting, or GPU commands. +#pragma once +#include "nvn_refl.h" +#include +#include + +enum { NVN_STORAGE_READ_ONLY = 0, NVN_STORAGE_READ_WRITE = 1, NVN_STORAGE_WRITE_ONLY = 2 }; +typedef struct { + uint64_t aid, offset; + int is_pointer, has_pointer_type, nested_argument_buffer, access; +} nvn_storage_access_member; +enum { + NVN_STORAGE_NOT_STORAGE, NVN_STORAGE_BAD_METADATA, NVN_STORAGE_NO_REFLECTION, + NVN_STORAGE_MEMBER_MISSING, NVN_STORAGE_MEMBER_DUPLICATE, NVN_STORAGE_NOT_POINTER, + NVN_STORAGE_BAD_OFFSET, NVN_STORAGE_UNKNOWN_ACCESS, NVN_STORAGE_COMPILED_READ_ONLY, + NVN_STORAGE_COMPILED_WRITE +}; +typedef struct { + int status, access, promoted; + uint64_t offset; + unsigned before, after; +} nvn_storage_access_result; + +static inline const char *nvn_storage_access_status(int status) { + switch (status) { + case NVN_STORAGE_NOT_STORAGE: return "not-storage"; + case NVN_STORAGE_BAD_METADATA: return "metadata-mismatch"; + case NVN_STORAGE_NO_REFLECTION: return "reflection-unavailable"; + case NVN_STORAGE_MEMBER_MISSING: return "member-missing"; + case NVN_STORAGE_MEMBER_DUPLICATE: return "member-duplicate"; + case NVN_STORAGE_NOT_POINTER: return "pointer-type-mismatch"; + case NVN_STORAGE_BAD_OFFSET: return "offset-mismatch"; + case NVN_STORAGE_UNKNOWN_ACCESS: return "unknown-access"; + case NVN_STORAGE_COMPILED_READ_ONLY: return "compiled-read-only"; + case NVN_STORAGE_COMPILED_WRITE: return "compiled-write"; + default: return "invalid-result"; + } +} +static inline nvn_storage_access_result nvn_storage_access_apply( + nvn_refl *rf, int index, const nvn_storage_access_member *members, + size_t count, int root_valid, size_t encoded_len) { + nvn_storage_access_result r = {.status = NVN_STORAGE_BAD_METADATA, .access = -1, .offset = UINT64_MAX}; + if (!rf || rf->nb < 0 || rf->nb > NVN_REFL_MAXB || index < 0 || index >= rf->nb) return r; + nvn_rbind *b = &rf->b[index]; r.before = r.after = b->rw; + // Uniform/support/driver/image pointers have different contracts. Only a + // resolved NVN storage slot may gain Write from this policy. + if (b->kind != NVN_RB_SBUFP) { r.status = NVN_STORAGE_NOT_STORAGE; return r; } + if (!rf->route_b || b->reg >= 16 || b->aid >= NVN_REFL_MAXARG || b->n > 1 || + !encoded_len || (encoded_len & 7) || b->aid >= encoded_len / 8) return r; + for (int i = 0; i < rf->nb; i++) if (i != index) { + const nvn_rbind *other = &rf->b[i]; unsigned n = other->n ? other->n : 1; + if (b->aid >= other->aid && (unsigned)(b->aid - other->aid) < n) return r; + } + if (!root_valid || !members || count > NVN_REFL_MAXARG) { r.status = NVN_STORAGE_NO_REFLECTION; return r; } + const nvn_storage_access_member *match = NULL; + for (size_t i = 0; i < count; i++) if (members[i].aid == b->aid) { + if (match) { r.status = NVN_STORAGE_MEMBER_DUPLICATE; return r; } + match = &members[i]; + } + if (!match) { r.status = NVN_STORAGE_MEMBER_MISSING; return r; } + r.access = match->access; r.offset = match->offset; + if (!match->is_pointer || !match->has_pointer_type || match->nested_argument_buffer) { + r.status = NVN_STORAGE_NOT_POINTER; return r; + } + // This backend's already-validated route-B ABI writes raw 8-byte slots at + // aid*8. Argument ID is the identifier; byte offset is a separate ABI guard. + if (match->offset != (uint64_t)b->aid * 8 || match->offset > encoded_len - 8) { + r.status = NVN_STORAGE_BAD_OFFSET; return r; + } + if (match->access == NVN_STORAGE_READ_WRITE || match->access == NVN_STORAGE_WRITE_ONLY) { + r.status = NVN_STORAGE_COMPILED_WRITE; + if (!b->rw) { b->rw = 1; r.promoted = 1; } + r.after = b->rw; + } else r.status = match->access == NVN_STORAGE_READ_ONLY ? NVN_STORAGE_COMPILED_READ_ONLY : NVN_STORAGE_UNKNOWN_ACCESS; + // Missing, unknown, or read-only reflection never downgrades original rw. + return r; +} diff --git a/dm/gfx/nvn/nvn_storage_access_metal.h b/dm/gfx/nvn/nvn_storage_access_metal.h new file mode 100644 index 0000000000..c2a41632f2 --- /dev/null +++ b/dm/gfx/nvn/nvn_storage_access_metal.h @@ -0,0 +1,80 @@ +// Public iOS 11+ compiled-pointer reflection, collected once at shader load. +// The reflection-taking overload is deprecated, but remains available on iOS16. +#pragma once +#import +#include "nvn_storage_access.h" +#include +#include +#include +#include +#include +#ifndef NVN_STORAGE_ACCESS_LOG +#define NVN_STORAGE_ACCESS_LOG(...) do { fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); } while (0) +#define NVN_STORAGE_ACCESS_LOCAL_LOG 1 +#endif + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" +_Static_assert(MTLArgumentAccessReadOnly == NVN_STORAGE_READ_ONLY && + MTLArgumentAccessReadWrite == NVN_STORAGE_READ_WRITE && + MTLArgumentAccessWriteOnly == NVN_STORAGE_WRITE_ONLY, "Metal access enum changed"); + +static id nvn_storage_access_encoder( + id fn, NSUInteger abi, MTLArgument **reflection) { + id encoder = [fn newArgumentEncoderWithBufferIndex:abi reflection:reflection]; + if (!encoder) { + if (reflection) *reflection = nil; + encoder = [fn newArgumentEncoderWithBufferIndex:abi]; + } + return encoder; +} +static pthread_once_t g_storage_access_diag_once = PTHREAD_ONCE_INIT; +static char g_storage_access_diag_prefix[24]; +static _Atomic unsigned g_storage_access_notes, g_storage_access_diag_notes; +static void nvn_storage_access_diag_init(void) { + const char *s = getenv("DM_NVN_STORAGE_ACCESS_DIAG"); + if (!s) return; + size_t n = strlen(s); if (n >= sizeof g_storage_access_diag_prefix) n = sizeof g_storage_access_diag_prefix - 1; + memcpy(g_storage_access_diag_prefix, s, n); +} +static void nvn_storage_access_note(const char *key, int stage, const nvn_rbind *b, nvn_storage_access_result r) { + pthread_once(&g_storage_access_diag_once, nvn_storage_access_diag_init); + const char *prefix = g_storage_access_diag_prefix; + int selected = *prefix && (!strcmp(prefix, "1") || (key && !strncmp(key, prefix, strlen(prefix)))); + _Atomic unsigned *budget = selected ? &g_storage_access_diag_notes : &g_storage_access_notes; + unsigned note = atomic_load_explicit(budget, memory_order_relaxed); + do { if (note >= 128) return; } + while (!atomic_compare_exchange_weak_explicit(budget, ¬e, note + 1, memory_order_relaxed, memory_order_relaxed)); + NVN_STORAGE_ACCESS_LOG("[storage-access] key=%s stage=%d kind=%u reg=%u aid=%u offset=%llu access=%d rw=%u->%u promoted=%d result=%s", + key ? key : "-", stage, b->kind, b->reg, b->aid, (unsigned long long)r.offset, + r.access, r.before, r.after, r.promoted, nvn_storage_access_status(r.status)); +} +static unsigned nvn_storage_access_promote(nvn_refl *rf, MTLArgument *argument, + NSUInteger encoded_len, const char *key, int stage) { + if (!rf || rf->nb < 0 || rf->nb > NVN_REFL_MAXB) return 0; + nvn_storage_access_member members[NVN_REFL_MAXARG]; size_t count = 0; + MTLStructType *type = argument.bufferStructType; + BOOL valid = argument && argument.type == MTLArgumentTypeBuffer && rf->abi >= 0 && + argument.index == (NSUInteger)rf->abi && argument.bufferDataType == MTLDataTypeStruct && + type && type.members.count <= NVN_REFL_MAXARG; + if (valid) for (MTLStructMember *member in type.members) { + MTLPointerType *p = member.dataType == MTLDataTypePointer ? member.pointerType : nil; + members[count++] = (nvn_storage_access_member){.aid = member.argumentIndex, .offset = member.offset, + .is_pointer = member.dataType == MTLDataTypePointer, .has_pointer_type = p != nil, + .nested_argument_buffer = p ? p.elementIsArgumentBuffer : 0, .access = p ? (int)p.access : -1}; + } + // Never inspect argument.access here. The outer AB is constant/read-only; + // the pointee of a device pointer inside it may still be written. + unsigned promoted = 0; + for (int i = 0; i < rf->nb; i++) if (rf->b[i].kind == NVN_RB_SBUFP) { + nvn_storage_access_result r = nvn_storage_access_apply(rf, i, members, count, valid, encoded_len); + promoted += (unsigned)r.promoted; + nvn_storage_access_note(key, stage, &rf->b[i], r); + } + return promoted; +} +#pragma clang diagnostic pop +#ifdef NVN_STORAGE_ACCESS_LOCAL_LOG +#undef NVN_STORAGE_ACCESS_LOCAL_LOG +#undef NVN_STORAGE_ACCESS_LOG +#endif diff --git a/dm/gfx/nvn/nvn_thunks.S b/dm/gfx/nvn/nvn_thunks.S index b40e1a6b21..520214ae88 100644 --- a/dm/gfx/nvn/nvn_thunks.S +++ b/dm/gfx/nvn/nvn_thunks.S @@ -1,6 +1,22 @@ // Generated by dm/gfx/tools/gen_protos.py; do not edit. -// Counting uses the LSE atomic stadd (ARMv8.1; every Apple chip since A12 / M1 has it): the next line keeps this file independent of the compiler's -mcpu baseline. +// Use LSE only when the compilation target supports it. The baseline loop preserves x15 and NZCV, +// so the counting thunk still changes only x16/x17 before entering the implementation. +#if defined(__ARM_FEATURE_ATOMICS) .arch_extension lse +#endif + .macro NVN_COUNT_ONE +#if defined(__ARM_FEATURE_ATOMICS) + mov x17, #1 + stadd x17, [x16] +#else + str x15, [sp, #-16]! +1: ldxr x17, [x16] + add x17, x17, #1 + stxr w15, x17, [x16] + cbnz w15, 1b + ldr x15, [sp], #16 +#endif + .endm .text .p2align 2 .globl _nvnth_0 @@ -9,8 +25,7 @@ _nvnth_0: add x16, x16, _nvn_slot@PAGEOFF mov x17, #0 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_1 @@ -19,8 +34,7 @@ _nvnth_1: add x16, x16, _nvn_slot@PAGEOFF mov x17, #16 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_2 @@ -29,8 +43,7 @@ _nvnth_2: add x16, x16, _nvn_slot@PAGEOFF mov x17, #32 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_3 @@ -39,8 +52,7 @@ _nvnth_3: add x16, x16, _nvn_slot@PAGEOFF mov x17, #48 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_4 @@ -49,8 +61,7 @@ _nvnth_4: add x16, x16, _nvn_slot@PAGEOFF mov x17, #64 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_5 @@ -59,8 +70,7 @@ _nvnth_5: add x16, x16, _nvn_slot@PAGEOFF mov x17, #80 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_6 @@ -69,8 +79,7 @@ _nvnth_6: add x16, x16, _nvn_slot@PAGEOFF mov x17, #96 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_7 @@ -79,8 +88,7 @@ _nvnth_7: add x16, x16, _nvn_slot@PAGEOFF mov x17, #112 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_8 @@ -89,8 +97,7 @@ _nvnth_8: add x16, x16, _nvn_slot@PAGEOFF mov x17, #128 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_9 @@ -99,8 +106,7 @@ _nvnth_9: add x16, x16, _nvn_slot@PAGEOFF mov x17, #144 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_10 @@ -109,8 +115,7 @@ _nvnth_10: add x16, x16, _nvn_slot@PAGEOFF mov x17, #160 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_11 @@ -119,8 +124,7 @@ _nvnth_11: add x16, x16, _nvn_slot@PAGEOFF mov x17, #176 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_12 @@ -129,8 +133,7 @@ _nvnth_12: add x16, x16, _nvn_slot@PAGEOFF mov x17, #192 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_13 @@ -139,8 +142,7 @@ _nvnth_13: add x16, x16, _nvn_slot@PAGEOFF mov x17, #208 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_14 @@ -149,8 +151,7 @@ _nvnth_14: add x16, x16, _nvn_slot@PAGEOFF mov x17, #224 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_15 @@ -159,8 +160,7 @@ _nvnth_15: add x16, x16, _nvn_slot@PAGEOFF mov x17, #240 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_16 @@ -169,8 +169,7 @@ _nvnth_16: add x16, x16, _nvn_slot@PAGEOFF mov x17, #256 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_17 @@ -179,8 +178,7 @@ _nvnth_17: add x16, x16, _nvn_slot@PAGEOFF mov x17, #272 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_18 @@ -189,8 +187,7 @@ _nvnth_18: add x16, x16, _nvn_slot@PAGEOFF mov x17, #288 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_19 @@ -199,8 +196,7 @@ _nvnth_19: add x16, x16, _nvn_slot@PAGEOFF mov x17, #304 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_20 @@ -209,8 +205,7 @@ _nvnth_20: add x16, x16, _nvn_slot@PAGEOFF mov x17, #320 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_21 @@ -219,8 +214,7 @@ _nvnth_21: add x16, x16, _nvn_slot@PAGEOFF mov x17, #336 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_22 @@ -229,8 +223,7 @@ _nvnth_22: add x16, x16, _nvn_slot@PAGEOFF mov x17, #352 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_23 @@ -239,8 +232,7 @@ _nvnth_23: add x16, x16, _nvn_slot@PAGEOFF mov x17, #368 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_24 @@ -249,8 +241,7 @@ _nvnth_24: add x16, x16, _nvn_slot@PAGEOFF mov x17, #384 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_25 @@ -259,8 +250,7 @@ _nvnth_25: add x16, x16, _nvn_slot@PAGEOFF mov x17, #400 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_26 @@ -269,8 +259,7 @@ _nvnth_26: add x16, x16, _nvn_slot@PAGEOFF mov x17, #416 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_27 @@ -279,8 +268,7 @@ _nvnth_27: add x16, x16, _nvn_slot@PAGEOFF mov x17, #432 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_28 @@ -289,8 +277,7 @@ _nvnth_28: add x16, x16, _nvn_slot@PAGEOFF mov x17, #448 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_29 @@ -299,8 +286,7 @@ _nvnth_29: add x16, x16, _nvn_slot@PAGEOFF mov x17, #464 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_30 @@ -309,8 +295,7 @@ _nvnth_30: add x16, x16, _nvn_slot@PAGEOFF mov x17, #480 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_31 @@ -319,8 +304,7 @@ _nvnth_31: add x16, x16, _nvn_slot@PAGEOFF mov x17, #496 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_32 @@ -329,8 +313,7 @@ _nvnth_32: add x16, x16, _nvn_slot@PAGEOFF mov x17, #512 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_33 @@ -339,8 +322,7 @@ _nvnth_33: add x16, x16, _nvn_slot@PAGEOFF mov x17, #528 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_34 @@ -349,8 +331,7 @@ _nvnth_34: add x16, x16, _nvn_slot@PAGEOFF mov x17, #544 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_35 @@ -359,8 +340,7 @@ _nvnth_35: add x16, x16, _nvn_slot@PAGEOFF mov x17, #560 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_36 @@ -369,8 +349,7 @@ _nvnth_36: add x16, x16, _nvn_slot@PAGEOFF mov x17, #576 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_37 @@ -379,8 +358,7 @@ _nvnth_37: add x16, x16, _nvn_slot@PAGEOFF mov x17, #592 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_38 @@ -389,8 +367,7 @@ _nvnth_38: add x16, x16, _nvn_slot@PAGEOFF mov x17, #608 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_39 @@ -399,8 +376,7 @@ _nvnth_39: add x16, x16, _nvn_slot@PAGEOFF mov x17, #624 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_40 @@ -409,8 +385,7 @@ _nvnth_40: add x16, x16, _nvn_slot@PAGEOFF mov x17, #640 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_41 @@ -419,8 +394,7 @@ _nvnth_41: add x16, x16, _nvn_slot@PAGEOFF mov x17, #656 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_42 @@ -429,8 +403,7 @@ _nvnth_42: add x16, x16, _nvn_slot@PAGEOFF mov x17, #672 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_43 @@ -439,8 +412,7 @@ _nvnth_43: add x16, x16, _nvn_slot@PAGEOFF mov x17, #688 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_44 @@ -449,8 +421,7 @@ _nvnth_44: add x16, x16, _nvn_slot@PAGEOFF mov x17, #704 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_45 @@ -459,8 +430,7 @@ _nvnth_45: add x16, x16, _nvn_slot@PAGEOFF mov x17, #720 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_46 @@ -469,8 +439,7 @@ _nvnth_46: add x16, x16, _nvn_slot@PAGEOFF mov x17, #736 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_47 @@ -479,8 +448,7 @@ _nvnth_47: add x16, x16, _nvn_slot@PAGEOFF mov x17, #752 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_48 @@ -489,8 +457,7 @@ _nvnth_48: add x16, x16, _nvn_slot@PAGEOFF mov x17, #768 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_49 @@ -499,8 +466,7 @@ _nvnth_49: add x16, x16, _nvn_slot@PAGEOFF mov x17, #784 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_50 @@ -509,8 +475,7 @@ _nvnth_50: add x16, x16, _nvn_slot@PAGEOFF mov x17, #800 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_51 @@ -519,8 +484,7 @@ _nvnth_51: add x16, x16, _nvn_slot@PAGEOFF mov x17, #816 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_52 @@ -529,8 +493,7 @@ _nvnth_52: add x16, x16, _nvn_slot@PAGEOFF mov x17, #832 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_53 @@ -539,8 +502,7 @@ _nvnth_53: add x16, x16, _nvn_slot@PAGEOFF mov x17, #848 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_54 @@ -549,8 +511,7 @@ _nvnth_54: add x16, x16, _nvn_slot@PAGEOFF mov x17, #864 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_55 @@ -559,8 +520,7 @@ _nvnth_55: add x16, x16, _nvn_slot@PAGEOFF mov x17, #880 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_56 @@ -569,8 +529,7 @@ _nvnth_56: add x16, x16, _nvn_slot@PAGEOFF mov x17, #896 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_57 @@ -579,8 +538,7 @@ _nvnth_57: add x16, x16, _nvn_slot@PAGEOFF mov x17, #912 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_58 @@ -589,8 +547,7 @@ _nvnth_58: add x16, x16, _nvn_slot@PAGEOFF mov x17, #928 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_59 @@ -599,8 +556,7 @@ _nvnth_59: add x16, x16, _nvn_slot@PAGEOFF mov x17, #944 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_60 @@ -609,8 +565,7 @@ _nvnth_60: add x16, x16, _nvn_slot@PAGEOFF mov x17, #960 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_61 @@ -619,8 +574,7 @@ _nvnth_61: add x16, x16, _nvn_slot@PAGEOFF mov x17, #976 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_62 @@ -629,8 +583,7 @@ _nvnth_62: add x16, x16, _nvn_slot@PAGEOFF mov x17, #992 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_63 @@ -639,8 +592,7 @@ _nvnth_63: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1008 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_64 @@ -649,8 +601,7 @@ _nvnth_64: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1024 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_65 @@ -659,8 +610,7 @@ _nvnth_65: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1040 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_66 @@ -669,8 +619,7 @@ _nvnth_66: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1056 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_67 @@ -679,8 +628,7 @@ _nvnth_67: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1072 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_68 @@ -689,8 +637,7 @@ _nvnth_68: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1088 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_69 @@ -699,8 +646,7 @@ _nvnth_69: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1104 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_70 @@ -709,8 +655,7 @@ _nvnth_70: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1120 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_71 @@ -719,8 +664,7 @@ _nvnth_71: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1136 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_72 @@ -729,8 +673,7 @@ _nvnth_72: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1152 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_73 @@ -739,8 +682,7 @@ _nvnth_73: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1168 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_74 @@ -749,8 +691,7 @@ _nvnth_74: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1184 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_75 @@ -759,8 +700,7 @@ _nvnth_75: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1200 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_76 @@ -769,8 +709,7 @@ _nvnth_76: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1216 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_77 @@ -779,8 +718,7 @@ _nvnth_77: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1232 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_78 @@ -789,8 +727,7 @@ _nvnth_78: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1248 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_79 @@ -799,8 +736,7 @@ _nvnth_79: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1264 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_80 @@ -809,8 +745,7 @@ _nvnth_80: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1280 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_81 @@ -819,8 +754,7 @@ _nvnth_81: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1296 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_82 @@ -829,8 +763,7 @@ _nvnth_82: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1312 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_83 @@ -839,8 +772,7 @@ _nvnth_83: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1328 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_84 @@ -849,8 +781,7 @@ _nvnth_84: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1344 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_85 @@ -859,8 +790,7 @@ _nvnth_85: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1360 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_86 @@ -869,8 +799,7 @@ _nvnth_86: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1376 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_87 @@ -879,8 +808,7 @@ _nvnth_87: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1392 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_88 @@ -889,8 +817,7 @@ _nvnth_88: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1408 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_89 @@ -899,8 +826,7 @@ _nvnth_89: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1424 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_90 @@ -909,8 +835,7 @@ _nvnth_90: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1440 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_91 @@ -919,8 +844,7 @@ _nvnth_91: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1456 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_92 @@ -929,8 +853,7 @@ _nvnth_92: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1472 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_93 @@ -939,8 +862,7 @@ _nvnth_93: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1488 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_94 @@ -949,8 +871,7 @@ _nvnth_94: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1504 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_95 @@ -959,8 +880,7 @@ _nvnth_95: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1520 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_96 @@ -969,8 +889,7 @@ _nvnth_96: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1536 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_97 @@ -979,8 +898,7 @@ _nvnth_97: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1552 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_98 @@ -989,8 +907,7 @@ _nvnth_98: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1568 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_99 @@ -999,8 +916,7 @@ _nvnth_99: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1584 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_100 @@ -1009,8 +925,7 @@ _nvnth_100: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1600 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_101 @@ -1019,8 +934,7 @@ _nvnth_101: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1616 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_102 @@ -1029,8 +943,7 @@ _nvnth_102: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1632 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_103 @@ -1039,8 +952,7 @@ _nvnth_103: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1648 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_104 @@ -1049,8 +961,7 @@ _nvnth_104: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1664 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_105 @@ -1059,8 +970,7 @@ _nvnth_105: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1680 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_106 @@ -1069,8 +979,7 @@ _nvnth_106: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1696 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_107 @@ -1079,8 +988,7 @@ _nvnth_107: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1712 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_108 @@ -1089,8 +997,7 @@ _nvnth_108: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1728 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_109 @@ -1099,8 +1006,7 @@ _nvnth_109: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1744 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_110 @@ -1109,8 +1015,7 @@ _nvnth_110: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1760 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_111 @@ -1119,8 +1024,7 @@ _nvnth_111: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1776 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_112 @@ -1129,8 +1033,7 @@ _nvnth_112: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1792 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_113 @@ -1139,8 +1042,7 @@ _nvnth_113: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1808 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_114 @@ -1149,8 +1051,7 @@ _nvnth_114: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1824 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_115 @@ -1159,8 +1060,7 @@ _nvnth_115: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1840 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_116 @@ -1169,8 +1069,7 @@ _nvnth_116: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1856 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_117 @@ -1179,8 +1078,7 @@ _nvnth_117: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1872 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_118 @@ -1189,8 +1087,7 @@ _nvnth_118: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1888 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_119 @@ -1199,8 +1096,7 @@ _nvnth_119: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1904 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_120 @@ -1209,8 +1105,7 @@ _nvnth_120: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1920 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_121 @@ -1219,8 +1114,7 @@ _nvnth_121: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1936 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_122 @@ -1229,8 +1123,7 @@ _nvnth_122: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1952 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_123 @@ -1239,8 +1132,7 @@ _nvnth_123: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1968 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_124 @@ -1249,8 +1141,7 @@ _nvnth_124: add x16, x16, _nvn_slot@PAGEOFF mov x17, #1984 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_125 @@ -1259,8 +1150,7 @@ _nvnth_125: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2000 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_126 @@ -1269,8 +1159,7 @@ _nvnth_126: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2016 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_127 @@ -1279,8 +1168,7 @@ _nvnth_127: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2032 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_128 @@ -1289,8 +1177,7 @@ _nvnth_128: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2048 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_129 @@ -1299,8 +1186,7 @@ _nvnth_129: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2064 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_130 @@ -1309,8 +1195,7 @@ _nvnth_130: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2080 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_131 @@ -1319,8 +1204,7 @@ _nvnth_131: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2096 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_132 @@ -1329,8 +1213,7 @@ _nvnth_132: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2112 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_133 @@ -1339,8 +1222,7 @@ _nvnth_133: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2128 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_134 @@ -1349,8 +1231,7 @@ _nvnth_134: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2144 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_135 @@ -1359,8 +1240,7 @@ _nvnth_135: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2160 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_136 @@ -1369,8 +1249,7 @@ _nvnth_136: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2176 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_137 @@ -1379,8 +1258,7 @@ _nvnth_137: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2192 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_138 @@ -1389,8 +1267,7 @@ _nvnth_138: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2208 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_139 @@ -1399,8 +1276,7 @@ _nvnth_139: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2224 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_140 @@ -1409,8 +1285,7 @@ _nvnth_140: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2240 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_141 @@ -1419,8 +1294,7 @@ _nvnth_141: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2256 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_142 @@ -1429,8 +1303,7 @@ _nvnth_142: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2272 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_143 @@ -1439,8 +1312,7 @@ _nvnth_143: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2288 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_144 @@ -1449,8 +1321,7 @@ _nvnth_144: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2304 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_145 @@ -1459,8 +1330,7 @@ _nvnth_145: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2320 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_146 @@ -1469,8 +1339,7 @@ _nvnth_146: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2336 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_147 @@ -1479,8 +1348,7 @@ _nvnth_147: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2352 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_148 @@ -1489,8 +1357,7 @@ _nvnth_148: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2368 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_149 @@ -1499,8 +1366,7 @@ _nvnth_149: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2384 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_150 @@ -1509,8 +1375,7 @@ _nvnth_150: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2400 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_151 @@ -1519,8 +1384,7 @@ _nvnth_151: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2416 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_152 @@ -1529,8 +1393,7 @@ _nvnth_152: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2432 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_153 @@ -1539,8 +1402,7 @@ _nvnth_153: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2448 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_154 @@ -1549,8 +1411,7 @@ _nvnth_154: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2464 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_155 @@ -1559,8 +1420,7 @@ _nvnth_155: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2480 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_156 @@ -1569,8 +1429,7 @@ _nvnth_156: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2496 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_157 @@ -1579,8 +1438,7 @@ _nvnth_157: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2512 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_158 @@ -1589,8 +1447,7 @@ _nvnth_158: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2528 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_159 @@ -1599,8 +1456,7 @@ _nvnth_159: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2544 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_160 @@ -1609,8 +1465,7 @@ _nvnth_160: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2560 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_161 @@ -1619,8 +1474,7 @@ _nvnth_161: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2576 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_162 @@ -1629,8 +1483,7 @@ _nvnth_162: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2592 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_163 @@ -1639,8 +1492,7 @@ _nvnth_163: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2608 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_164 @@ -1649,8 +1501,7 @@ _nvnth_164: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2624 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_165 @@ -1659,8 +1510,7 @@ _nvnth_165: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2640 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_166 @@ -1669,8 +1519,7 @@ _nvnth_166: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2656 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_167 @@ -1679,8 +1528,7 @@ _nvnth_167: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2672 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_168 @@ -1689,8 +1537,7 @@ _nvnth_168: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2688 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_169 @@ -1699,8 +1546,7 @@ _nvnth_169: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2704 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_170 @@ -1709,8 +1555,7 @@ _nvnth_170: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2720 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_171 @@ -1719,8 +1564,7 @@ _nvnth_171: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2736 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_172 @@ -1729,8 +1573,7 @@ _nvnth_172: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2752 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_173 @@ -1739,8 +1582,7 @@ _nvnth_173: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2768 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_174 @@ -1749,8 +1591,7 @@ _nvnth_174: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2784 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_175 @@ -1759,8 +1600,7 @@ _nvnth_175: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2800 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_176 @@ -1769,8 +1609,7 @@ _nvnth_176: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2816 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_177 @@ -1779,8 +1618,7 @@ _nvnth_177: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2832 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_178 @@ -1789,8 +1627,7 @@ _nvnth_178: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2848 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_179 @@ -1799,8 +1636,7 @@ _nvnth_179: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2864 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_180 @@ -1809,8 +1645,7 @@ _nvnth_180: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2880 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_181 @@ -1819,8 +1654,7 @@ _nvnth_181: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2896 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_182 @@ -1829,8 +1663,7 @@ _nvnth_182: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2912 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_183 @@ -1839,8 +1672,7 @@ _nvnth_183: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2928 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_184 @@ -1849,8 +1681,7 @@ _nvnth_184: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2944 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_185 @@ -1859,8 +1690,7 @@ _nvnth_185: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2960 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_186 @@ -1869,8 +1699,7 @@ _nvnth_186: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2976 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_187 @@ -1879,8 +1708,7 @@ _nvnth_187: add x16, x16, _nvn_slot@PAGEOFF mov x17, #2992 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_188 @@ -1889,8 +1717,7 @@ _nvnth_188: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3008 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_189 @@ -1899,8 +1726,7 @@ _nvnth_189: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3024 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_190 @@ -1909,8 +1735,7 @@ _nvnth_190: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3040 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_191 @@ -1919,8 +1744,7 @@ _nvnth_191: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3056 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_192 @@ -1929,8 +1753,7 @@ _nvnth_192: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3072 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_193 @@ -1939,8 +1762,7 @@ _nvnth_193: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3088 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_194 @@ -1949,8 +1771,7 @@ _nvnth_194: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3104 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_195 @@ -1959,8 +1780,7 @@ _nvnth_195: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3120 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_196 @@ -1969,8 +1789,7 @@ _nvnth_196: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3136 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_197 @@ -1979,8 +1798,7 @@ _nvnth_197: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3152 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_198 @@ -1989,8 +1807,7 @@ _nvnth_198: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3168 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_199 @@ -1999,8 +1816,7 @@ _nvnth_199: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3184 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_200 @@ -2009,8 +1825,7 @@ _nvnth_200: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3200 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_201 @@ -2019,8 +1834,7 @@ _nvnth_201: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3216 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_202 @@ -2029,8 +1843,7 @@ _nvnth_202: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3232 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_203 @@ -2039,8 +1852,7 @@ _nvnth_203: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3248 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_204 @@ -2049,8 +1861,7 @@ _nvnth_204: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3264 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_205 @@ -2059,8 +1870,7 @@ _nvnth_205: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3280 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_206 @@ -2069,8 +1879,7 @@ _nvnth_206: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3296 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_207 @@ -2079,8 +1888,7 @@ _nvnth_207: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3312 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_208 @@ -2089,8 +1897,7 @@ _nvnth_208: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3328 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_209 @@ -2099,8 +1906,7 @@ _nvnth_209: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3344 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_210 @@ -2109,8 +1915,7 @@ _nvnth_210: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3360 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_211 @@ -2119,8 +1924,7 @@ _nvnth_211: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3376 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_212 @@ -2129,8 +1933,7 @@ _nvnth_212: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3392 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_213 @@ -2139,8 +1942,7 @@ _nvnth_213: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3408 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_214 @@ -2149,8 +1951,7 @@ _nvnth_214: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3424 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_215 @@ -2159,8 +1960,7 @@ _nvnth_215: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3440 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_216 @@ -2169,8 +1969,7 @@ _nvnth_216: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3456 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_217 @@ -2179,8 +1978,7 @@ _nvnth_217: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3472 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_218 @@ -2189,8 +1987,7 @@ _nvnth_218: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3488 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_219 @@ -2199,8 +1996,7 @@ _nvnth_219: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3504 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_220 @@ -2209,8 +2005,7 @@ _nvnth_220: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3520 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_221 @@ -2219,8 +2014,7 @@ _nvnth_221: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3536 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_222 @@ -2229,8 +2023,7 @@ _nvnth_222: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3552 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_223 @@ -2239,8 +2032,7 @@ _nvnth_223: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3568 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_224 @@ -2249,8 +2041,7 @@ _nvnth_224: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3584 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_225 @@ -2259,8 +2050,7 @@ _nvnth_225: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3600 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_226 @@ -2269,8 +2059,7 @@ _nvnth_226: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3616 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_227 @@ -2279,8 +2068,7 @@ _nvnth_227: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3632 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_228 @@ -2289,8 +2077,7 @@ _nvnth_228: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3648 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_229 @@ -2299,8 +2086,7 @@ _nvnth_229: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3664 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_230 @@ -2309,8 +2095,7 @@ _nvnth_230: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3680 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_231 @@ -2319,8 +2104,7 @@ _nvnth_231: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3696 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_232 @@ -2329,8 +2113,7 @@ _nvnth_232: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3712 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_233 @@ -2339,8 +2122,7 @@ _nvnth_233: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3728 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_234 @@ -2349,8 +2131,7 @@ _nvnth_234: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3744 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_235 @@ -2359,8 +2140,7 @@ _nvnth_235: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3760 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_236 @@ -2369,8 +2149,7 @@ _nvnth_236: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3776 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_237 @@ -2379,8 +2158,7 @@ _nvnth_237: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3792 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_238 @@ -2389,8 +2167,7 @@ _nvnth_238: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3808 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_239 @@ -2399,8 +2176,7 @@ _nvnth_239: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3824 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_240 @@ -2409,8 +2185,7 @@ _nvnth_240: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3840 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_241 @@ -2419,8 +2194,7 @@ _nvnth_241: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3856 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_242 @@ -2429,8 +2203,7 @@ _nvnth_242: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3872 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_243 @@ -2439,8 +2212,7 @@ _nvnth_243: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3888 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_244 @@ -2449,8 +2221,7 @@ _nvnth_244: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3904 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_245 @@ -2459,8 +2230,7 @@ _nvnth_245: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3920 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_246 @@ -2469,8 +2239,7 @@ _nvnth_246: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3936 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_247 @@ -2479,8 +2248,7 @@ _nvnth_247: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3952 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_248 @@ -2489,8 +2257,7 @@ _nvnth_248: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3968 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_249 @@ -2499,8 +2266,7 @@ _nvnth_249: add x16, x16, _nvn_slot@PAGEOFF mov x17, #3984 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_250 @@ -2509,8 +2275,7 @@ _nvnth_250: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4000 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_251 @@ -2519,8 +2284,7 @@ _nvnth_251: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4016 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_252 @@ -2529,8 +2293,7 @@ _nvnth_252: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4032 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_253 @@ -2539,8 +2302,7 @@ _nvnth_253: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4048 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_254 @@ -2549,8 +2311,7 @@ _nvnth_254: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4064 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_255 @@ -2559,8 +2320,7 @@ _nvnth_255: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4080 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_256 @@ -2569,8 +2329,7 @@ _nvnth_256: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4096 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_257 @@ -2579,8 +2338,7 @@ _nvnth_257: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4112 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_258 @@ -2589,8 +2347,7 @@ _nvnth_258: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4128 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_259 @@ -2599,8 +2356,7 @@ _nvnth_259: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4144 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_260 @@ -2609,8 +2365,7 @@ _nvnth_260: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4160 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_261 @@ -2619,8 +2374,7 @@ _nvnth_261: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4176 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_262 @@ -2629,8 +2383,7 @@ _nvnth_262: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4192 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_263 @@ -2639,8 +2392,7 @@ _nvnth_263: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4208 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_264 @@ -2649,8 +2401,7 @@ _nvnth_264: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4224 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_265 @@ -2659,8 +2410,7 @@ _nvnth_265: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4240 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_266 @@ -2669,8 +2419,7 @@ _nvnth_266: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4256 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_267 @@ -2679,8 +2428,7 @@ _nvnth_267: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4272 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_268 @@ -2689,8 +2437,7 @@ _nvnth_268: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4288 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_269 @@ -2699,8 +2446,7 @@ _nvnth_269: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4304 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_270 @@ -2709,8 +2455,7 @@ _nvnth_270: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4320 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_271 @@ -2719,8 +2464,7 @@ _nvnth_271: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4336 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_272 @@ -2729,8 +2473,7 @@ _nvnth_272: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4352 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_273 @@ -2739,8 +2482,7 @@ _nvnth_273: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4368 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_274 @@ -2749,8 +2491,7 @@ _nvnth_274: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4384 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_275 @@ -2759,8 +2500,7 @@ _nvnth_275: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4400 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_276 @@ -2769,8 +2509,7 @@ _nvnth_276: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4416 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_277 @@ -2779,8 +2518,7 @@ _nvnth_277: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4432 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_278 @@ -2789,8 +2527,7 @@ _nvnth_278: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4448 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_279 @@ -2799,8 +2536,7 @@ _nvnth_279: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4464 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_280 @@ -2809,8 +2545,7 @@ _nvnth_280: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4480 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_281 @@ -2819,8 +2554,7 @@ _nvnth_281: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4496 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_282 @@ -2829,8 +2563,7 @@ _nvnth_282: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4512 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_283 @@ -2839,8 +2572,7 @@ _nvnth_283: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4528 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_284 @@ -2849,8 +2581,7 @@ _nvnth_284: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4544 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_285 @@ -2859,8 +2590,7 @@ _nvnth_285: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4560 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_286 @@ -2869,8 +2599,7 @@ _nvnth_286: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4576 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_287 @@ -2879,8 +2608,7 @@ _nvnth_287: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4592 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_288 @@ -2889,8 +2617,7 @@ _nvnth_288: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4608 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_289 @@ -2899,8 +2626,7 @@ _nvnth_289: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4624 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_290 @@ -2909,8 +2635,7 @@ _nvnth_290: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4640 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_291 @@ -2919,8 +2644,7 @@ _nvnth_291: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4656 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_292 @@ -2929,8 +2653,7 @@ _nvnth_292: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4672 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_293 @@ -2939,8 +2662,7 @@ _nvnth_293: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4688 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_294 @@ -2949,8 +2671,7 @@ _nvnth_294: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4704 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_295 @@ -2959,8 +2680,7 @@ _nvnth_295: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4720 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_296 @@ -2969,8 +2689,7 @@ _nvnth_296: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4736 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_297 @@ -2979,8 +2698,7 @@ _nvnth_297: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4752 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_298 @@ -2989,8 +2707,7 @@ _nvnth_298: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4768 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_299 @@ -2999,8 +2716,7 @@ _nvnth_299: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4784 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_300 @@ -3009,8 +2725,7 @@ _nvnth_300: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4800 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_301 @@ -3019,8 +2734,7 @@ _nvnth_301: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4816 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_302 @@ -3029,8 +2743,7 @@ _nvnth_302: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4832 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_303 @@ -3039,8 +2752,7 @@ _nvnth_303: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4848 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_304 @@ -3049,8 +2761,7 @@ _nvnth_304: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4864 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_305 @@ -3059,8 +2770,7 @@ _nvnth_305: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4880 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_306 @@ -3069,8 +2779,7 @@ _nvnth_306: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4896 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_307 @@ -3079,8 +2788,7 @@ _nvnth_307: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4912 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_308 @@ -3089,8 +2797,7 @@ _nvnth_308: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4928 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_309 @@ -3099,8 +2806,7 @@ _nvnth_309: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4944 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_310 @@ -3109,8 +2815,7 @@ _nvnth_310: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4960 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_311 @@ -3119,8 +2824,7 @@ _nvnth_311: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4976 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_312 @@ -3129,8 +2833,7 @@ _nvnth_312: add x16, x16, _nvn_slot@PAGEOFF mov x17, #4992 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_313 @@ -3139,8 +2842,7 @@ _nvnth_313: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5008 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_314 @@ -3149,8 +2851,7 @@ _nvnth_314: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5024 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_315 @@ -3159,8 +2860,7 @@ _nvnth_315: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5040 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_316 @@ -3169,8 +2869,7 @@ _nvnth_316: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5056 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_317 @@ -3179,8 +2878,7 @@ _nvnth_317: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5072 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_318 @@ -3189,8 +2887,7 @@ _nvnth_318: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5088 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_319 @@ -3199,8 +2896,7 @@ _nvnth_319: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5104 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_320 @@ -3209,8 +2905,7 @@ _nvnth_320: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5120 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_321 @@ -3219,8 +2914,7 @@ _nvnth_321: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5136 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_322 @@ -3229,8 +2923,7 @@ _nvnth_322: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5152 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_323 @@ -3239,8 +2932,7 @@ _nvnth_323: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5168 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_324 @@ -3249,8 +2941,7 @@ _nvnth_324: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5184 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_325 @@ -3259,8 +2950,7 @@ _nvnth_325: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5200 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_326 @@ -3269,8 +2959,7 @@ _nvnth_326: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5216 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_327 @@ -3279,8 +2968,7 @@ _nvnth_327: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5232 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_328 @@ -3289,8 +2977,7 @@ _nvnth_328: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5248 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_329 @@ -3299,8 +2986,7 @@ _nvnth_329: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5264 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_330 @@ -3309,8 +2995,7 @@ _nvnth_330: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5280 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_331 @@ -3319,8 +3004,7 @@ _nvnth_331: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5296 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_332 @@ -3329,8 +3013,7 @@ _nvnth_332: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5312 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_333 @@ -3339,8 +3022,7 @@ _nvnth_333: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5328 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_334 @@ -3349,8 +3031,7 @@ _nvnth_334: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5344 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_335 @@ -3359,8 +3040,7 @@ _nvnth_335: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5360 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_336 @@ -3369,8 +3049,7 @@ _nvnth_336: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5376 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_337 @@ -3379,8 +3058,7 @@ _nvnth_337: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5392 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_338 @@ -3389,8 +3067,7 @@ _nvnth_338: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5408 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_339 @@ -3399,8 +3076,7 @@ _nvnth_339: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5424 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_340 @@ -3409,8 +3085,7 @@ _nvnth_340: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5440 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_341 @@ -3419,8 +3094,7 @@ _nvnth_341: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5456 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_342 @@ -3429,8 +3103,7 @@ _nvnth_342: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5472 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_343 @@ -3439,8 +3112,7 @@ _nvnth_343: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5488 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_344 @@ -3449,8 +3121,7 @@ _nvnth_344: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5504 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_345 @@ -3459,8 +3130,7 @@ _nvnth_345: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5520 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_346 @@ -3469,8 +3139,7 @@ _nvnth_346: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5536 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_347 @@ -3479,8 +3148,7 @@ _nvnth_347: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5552 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_348 @@ -3489,8 +3157,7 @@ _nvnth_348: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5568 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_349 @@ -3499,8 +3166,7 @@ _nvnth_349: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5584 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_350 @@ -3509,8 +3175,7 @@ _nvnth_350: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5600 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_351 @@ -3519,8 +3184,7 @@ _nvnth_351: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5616 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_352 @@ -3529,8 +3193,7 @@ _nvnth_352: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5632 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_353 @@ -3539,8 +3202,7 @@ _nvnth_353: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5648 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_354 @@ -3549,8 +3211,7 @@ _nvnth_354: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5664 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_355 @@ -3559,8 +3220,7 @@ _nvnth_355: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5680 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_356 @@ -3569,8 +3229,7 @@ _nvnth_356: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5696 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_357 @@ -3579,8 +3238,7 @@ _nvnth_357: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5712 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_358 @@ -3589,8 +3247,7 @@ _nvnth_358: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5728 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_359 @@ -3599,8 +3256,7 @@ _nvnth_359: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5744 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_360 @@ -3609,8 +3265,7 @@ _nvnth_360: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5760 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_361 @@ -3619,8 +3274,7 @@ _nvnth_361: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5776 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_362 @@ -3629,8 +3283,7 @@ _nvnth_362: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5792 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_363 @@ -3639,8 +3292,7 @@ _nvnth_363: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5808 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_364 @@ -3649,8 +3301,7 @@ _nvnth_364: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5824 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_365 @@ -3659,8 +3310,7 @@ _nvnth_365: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5840 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_366 @@ -3669,8 +3319,7 @@ _nvnth_366: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5856 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_367 @@ -3679,8 +3328,7 @@ _nvnth_367: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5872 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_368 @@ -3689,8 +3337,7 @@ _nvnth_368: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5888 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_369 @@ -3699,8 +3346,7 @@ _nvnth_369: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5904 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_370 @@ -3709,8 +3355,7 @@ _nvnth_370: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5920 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_371 @@ -3719,8 +3364,7 @@ _nvnth_371: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5936 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_372 @@ -3729,8 +3373,7 @@ _nvnth_372: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5952 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_373 @@ -3739,8 +3382,7 @@ _nvnth_373: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5968 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_374 @@ -3749,8 +3391,7 @@ _nvnth_374: add x16, x16, _nvn_slot@PAGEOFF mov x17, #5984 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_375 @@ -3759,8 +3400,7 @@ _nvnth_375: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6000 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_376 @@ -3769,8 +3409,7 @@ _nvnth_376: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6016 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_377 @@ -3779,8 +3418,7 @@ _nvnth_377: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6032 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_378 @@ -3789,8 +3427,7 @@ _nvnth_378: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6048 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_379 @@ -3799,8 +3436,7 @@ _nvnth_379: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6064 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_380 @@ -3809,8 +3445,7 @@ _nvnth_380: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6080 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_381 @@ -3819,8 +3454,7 @@ _nvnth_381: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6096 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_382 @@ -3829,8 +3463,7 @@ _nvnth_382: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6112 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_383 @@ -3839,8 +3472,7 @@ _nvnth_383: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6128 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_384 @@ -3849,8 +3481,7 @@ _nvnth_384: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6144 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_385 @@ -3859,8 +3490,7 @@ _nvnth_385: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6160 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_386 @@ -3869,8 +3499,7 @@ _nvnth_386: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6176 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_387 @@ -3879,8 +3508,7 @@ _nvnth_387: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6192 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_388 @@ -3889,8 +3517,7 @@ _nvnth_388: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6208 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_389 @@ -3899,8 +3526,7 @@ _nvnth_389: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6224 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_390 @@ -3909,8 +3535,7 @@ _nvnth_390: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6240 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_391 @@ -3919,8 +3544,7 @@ _nvnth_391: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6256 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_392 @@ -3929,8 +3553,7 @@ _nvnth_392: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6272 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_393 @@ -3939,8 +3562,7 @@ _nvnth_393: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6288 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_394 @@ -3949,8 +3571,7 @@ _nvnth_394: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6304 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_395 @@ -3959,8 +3580,7 @@ _nvnth_395: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6320 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_396 @@ -3969,8 +3589,7 @@ _nvnth_396: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6336 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_397 @@ -3979,8 +3598,7 @@ _nvnth_397: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6352 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_398 @@ -3989,8 +3607,7 @@ _nvnth_398: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6368 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_399 @@ -3999,8 +3616,7 @@ _nvnth_399: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6384 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_400 @@ -4009,8 +3625,7 @@ _nvnth_400: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6400 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_401 @@ -4019,8 +3634,7 @@ _nvnth_401: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6416 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_402 @@ -4029,8 +3643,7 @@ _nvnth_402: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6432 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_403 @@ -4039,8 +3652,7 @@ _nvnth_403: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6448 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_404 @@ -4049,8 +3661,7 @@ _nvnth_404: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6464 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_405 @@ -4059,8 +3670,7 @@ _nvnth_405: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6480 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_406 @@ -4069,8 +3679,7 @@ _nvnth_406: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6496 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_407 @@ -4079,8 +3688,7 @@ _nvnth_407: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6512 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_408 @@ -4089,8 +3697,7 @@ _nvnth_408: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6528 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_409 @@ -4099,8 +3706,7 @@ _nvnth_409: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6544 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_410 @@ -4109,8 +3715,7 @@ _nvnth_410: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6560 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_411 @@ -4119,8 +3724,7 @@ _nvnth_411: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6576 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_412 @@ -4129,8 +3733,7 @@ _nvnth_412: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6592 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_413 @@ -4139,8 +3742,7 @@ _nvnth_413: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6608 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_414 @@ -4149,8 +3751,7 @@ _nvnth_414: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6624 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_415 @@ -4159,8 +3760,7 @@ _nvnth_415: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6640 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_416 @@ -4169,8 +3769,7 @@ _nvnth_416: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6656 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_417 @@ -4179,8 +3778,7 @@ _nvnth_417: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6672 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_418 @@ -4189,8 +3787,7 @@ _nvnth_418: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6688 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_419 @@ -4199,8 +3796,7 @@ _nvnth_419: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6704 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_420 @@ -4209,8 +3805,7 @@ _nvnth_420: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6720 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_421 @@ -4219,8 +3814,7 @@ _nvnth_421: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6736 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_422 @@ -4229,8 +3823,7 @@ _nvnth_422: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6752 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_423 @@ -4239,8 +3832,7 @@ _nvnth_423: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6768 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_424 @@ -4249,8 +3841,7 @@ _nvnth_424: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6784 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_425 @@ -4259,8 +3850,7 @@ _nvnth_425: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6800 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_426 @@ -4269,8 +3859,7 @@ _nvnth_426: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6816 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_427 @@ -4279,8 +3868,7 @@ _nvnth_427: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6832 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_428 @@ -4289,8 +3877,7 @@ _nvnth_428: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6848 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_429 @@ -4299,8 +3886,7 @@ _nvnth_429: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6864 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_430 @@ -4309,8 +3895,7 @@ _nvnth_430: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6880 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_431 @@ -4319,8 +3904,7 @@ _nvnth_431: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6896 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_432 @@ -4329,8 +3913,7 @@ _nvnth_432: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6912 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_433 @@ -4339,8 +3922,7 @@ _nvnth_433: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6928 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_434 @@ -4349,8 +3931,7 @@ _nvnth_434: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6944 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_435 @@ -4359,8 +3940,7 @@ _nvnth_435: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6960 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_436 @@ -4369,8 +3949,7 @@ _nvnth_436: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6976 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_437 @@ -4379,8 +3958,7 @@ _nvnth_437: add x16, x16, _nvn_slot@PAGEOFF mov x17, #6992 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_438 @@ -4389,8 +3967,7 @@ _nvnth_438: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7008 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_439 @@ -4399,8 +3976,7 @@ _nvnth_439: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7024 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_440 @@ -4409,8 +3985,7 @@ _nvnth_440: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7040 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_441 @@ -4419,8 +3994,7 @@ _nvnth_441: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7056 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_442 @@ -4429,8 +4003,7 @@ _nvnth_442: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7072 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_443 @@ -4439,8 +4012,7 @@ _nvnth_443: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7088 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_444 @@ -4449,8 +4021,7 @@ _nvnth_444: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7104 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_445 @@ -4459,8 +4030,7 @@ _nvnth_445: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7120 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_446 @@ -4469,8 +4039,7 @@ _nvnth_446: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7136 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_447 @@ -4479,8 +4048,7 @@ _nvnth_447: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7152 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_448 @@ -4489,8 +4057,7 @@ _nvnth_448: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7168 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_449 @@ -4499,8 +4066,7 @@ _nvnth_449: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7184 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_450 @@ -4509,8 +4075,7 @@ _nvnth_450: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7200 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_451 @@ -4519,8 +4084,7 @@ _nvnth_451: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7216 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_452 @@ -4529,8 +4093,7 @@ _nvnth_452: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7232 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_453 @@ -4539,8 +4102,7 @@ _nvnth_453: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7248 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_454 @@ -4549,8 +4111,7 @@ _nvnth_454: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7264 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_455 @@ -4559,8 +4120,7 @@ _nvnth_455: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7280 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_456 @@ -4569,8 +4129,7 @@ _nvnth_456: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7296 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_457 @@ -4579,8 +4138,7 @@ _nvnth_457: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7312 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_458 @@ -4589,8 +4147,7 @@ _nvnth_458: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7328 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_459 @@ -4599,8 +4156,7 @@ _nvnth_459: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7344 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_460 @@ -4609,8 +4165,7 @@ _nvnth_460: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7360 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_461 @@ -4619,8 +4174,7 @@ _nvnth_461: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7376 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_462 @@ -4629,8 +4183,7 @@ _nvnth_462: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7392 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_463 @@ -4639,8 +4192,7 @@ _nvnth_463: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7408 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_464 @@ -4649,8 +4201,7 @@ _nvnth_464: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7424 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_465 @@ -4659,8 +4210,7 @@ _nvnth_465: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7440 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_466 @@ -4669,8 +4219,7 @@ _nvnth_466: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7456 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_467 @@ -4679,8 +4228,7 @@ _nvnth_467: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7472 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_468 @@ -4689,8 +4237,7 @@ _nvnth_468: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7488 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_469 @@ -4699,8 +4246,7 @@ _nvnth_469: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7504 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_470 @@ -4709,8 +4255,7 @@ _nvnth_470: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7520 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_471 @@ -4719,8 +4264,7 @@ _nvnth_471: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7536 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_472 @@ -4729,8 +4273,7 @@ _nvnth_472: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7552 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_473 @@ -4739,8 +4282,7 @@ _nvnth_473: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7568 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_474 @@ -4749,8 +4291,7 @@ _nvnth_474: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7584 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_475 @@ -4759,8 +4300,7 @@ _nvnth_475: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7600 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_476 @@ -4769,8 +4309,7 @@ _nvnth_476: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7616 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_477 @@ -4779,8 +4318,7 @@ _nvnth_477: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7632 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_478 @@ -4789,8 +4327,7 @@ _nvnth_478: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7648 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_479 @@ -4799,8 +4336,7 @@ _nvnth_479: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7664 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_480 @@ -4809,8 +4345,7 @@ _nvnth_480: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7680 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_481 @@ -4819,8 +4354,7 @@ _nvnth_481: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7696 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_482 @@ -4829,8 +4363,7 @@ _nvnth_482: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7712 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_483 @@ -4839,8 +4372,7 @@ _nvnth_483: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7728 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_484 @@ -4849,8 +4381,7 @@ _nvnth_484: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7744 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_485 @@ -4859,8 +4390,7 @@ _nvnth_485: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7760 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_486 @@ -4869,8 +4399,7 @@ _nvnth_486: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7776 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_487 @@ -4879,8 +4408,7 @@ _nvnth_487: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7792 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_488 @@ -4889,8 +4417,7 @@ _nvnth_488: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7808 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_489 @@ -4899,8 +4426,7 @@ _nvnth_489: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7824 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_490 @@ -4909,8 +4435,7 @@ _nvnth_490: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7840 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_491 @@ -4919,8 +4444,7 @@ _nvnth_491: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7856 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_492 @@ -4929,8 +4453,7 @@ _nvnth_492: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7872 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_493 @@ -4939,8 +4462,7 @@ _nvnth_493: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7888 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_494 @@ -4949,8 +4471,7 @@ _nvnth_494: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7904 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_495 @@ -4959,8 +4480,7 @@ _nvnth_495: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7920 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_496 @@ -4969,8 +4489,7 @@ _nvnth_496: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7936 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_497 @@ -4979,8 +4498,7 @@ _nvnth_497: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7952 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_498 @@ -4989,8 +4507,7 @@ _nvnth_498: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7968 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_499 @@ -4999,8 +4516,7 @@ _nvnth_499: add x16, x16, _nvn_slot@PAGEOFF mov x17, #7984 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_500 @@ -5009,8 +4525,7 @@ _nvnth_500: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8000 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_501 @@ -5019,8 +4534,7 @@ _nvnth_501: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8016 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_502 @@ -5029,8 +4543,7 @@ _nvnth_502: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8032 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_503 @@ -5039,8 +4552,7 @@ _nvnth_503: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8048 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_504 @@ -5049,8 +4561,7 @@ _nvnth_504: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8064 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_505 @@ -5059,8 +4570,7 @@ _nvnth_505: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8080 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_506 @@ -5069,8 +4579,7 @@ _nvnth_506: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8096 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_507 @@ -5079,8 +4588,7 @@ _nvnth_507: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8112 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_508 @@ -5089,8 +4597,7 @@ _nvnth_508: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8128 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_509 @@ -5099,8 +4606,7 @@ _nvnth_509: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8144 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_510 @@ -5109,8 +4615,7 @@ _nvnth_510: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8160 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_511 @@ -5119,8 +4624,7 @@ _nvnth_511: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8176 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_512 @@ -5129,8 +4633,7 @@ _nvnth_512: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8192 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_513 @@ -5139,8 +4642,7 @@ _nvnth_513: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8208 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_514 @@ -5149,8 +4651,7 @@ _nvnth_514: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8224 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_515 @@ -5159,8 +4660,7 @@ _nvnth_515: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8240 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_516 @@ -5169,8 +4669,7 @@ _nvnth_516: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8256 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_517 @@ -5179,8 +4678,7 @@ _nvnth_517: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8272 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_518 @@ -5189,8 +4687,7 @@ _nvnth_518: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8288 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_519 @@ -5199,8 +4696,7 @@ _nvnth_519: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8304 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_520 @@ -5209,8 +4705,7 @@ _nvnth_520: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8320 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_521 @@ -5219,8 +4714,7 @@ _nvnth_521: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8336 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_522 @@ -5229,8 +4723,7 @@ _nvnth_522: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8352 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_523 @@ -5239,8 +4732,7 @@ _nvnth_523: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8368 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_524 @@ -5249,8 +4741,7 @@ _nvnth_524: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8384 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_525 @@ -5259,8 +4750,7 @@ _nvnth_525: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8400 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_526 @@ -5269,8 +4759,7 @@ _nvnth_526: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8416 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_527 @@ -5279,8 +4768,7 @@ _nvnth_527: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8432 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_528 @@ -5289,8 +4777,7 @@ _nvnth_528: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8448 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_529 @@ -5299,8 +4786,7 @@ _nvnth_529: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8464 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_530 @@ -5309,8 +4795,7 @@ _nvnth_530: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8480 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_531 @@ -5319,8 +4804,7 @@ _nvnth_531: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8496 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_532 @@ -5329,8 +4813,7 @@ _nvnth_532: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8512 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_533 @@ -5339,8 +4822,7 @@ _nvnth_533: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8528 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_534 @@ -5349,8 +4831,7 @@ _nvnth_534: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8544 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_535 @@ -5359,8 +4840,7 @@ _nvnth_535: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8560 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .globl _nvnth_536 @@ -5369,8 +4849,7 @@ _nvnth_536: add x16, x16, _nvn_slot@PAGEOFF mov x17, #8576 add x16, x16, x17 - mov x17, #1 - stadd x17, [x16] + NVN_COUNT_ONE ldr x17, [x16, #8] br x17 .data diff --git a/dm/gfx/nvn/nvn_upload_diag.h b/dm/gfx/nvn/nvn_upload_diag.h new file mode 100644 index 0000000000..e6d22d61ac --- /dev/null +++ b/dm/gfx/nvn/nvn_upload_diag.h @@ -0,0 +1,157 @@ +// Bounded, opt-in inspection of packaged uploads. This never suppresses an +// upload or changes a dirty stamp. Repeated snapshots compare exact guest +// bytes; SHA256 is attribution only, not the equality decision. +#pragma once +#include "nvn_dirty.h" +#include +#include +#include +#include +#include +#include +#include + +#ifndef NVN_UPDIAG_LOG +#define NVN_UPDIAG_LOG(...) do { fprintf(stderr, "[upload-diag] " __VA_ARGS__); fputc('\n', stderr); } while (0) +#endif +enum { UPDIAG_SLOTS = 32, UPDIAG_RECORDS = 128, UPDIAG_MAX_FRAMES = 16 }; +#define UPDIAG_MAX_TEXTURE (8u * 1024u * 1024u) +#define UPDIAG_MAX_SNAPSHOTS (32u * 1024u * 1024u) +typedef struct { + uint64_t frame, serial, offset, bytes, pool_bytes; + const void *mem, *pool; + unsigned width, height, depth, format, target, levels, flags; + int reupload; +} nvn_updiag_meta; +typedef struct { + nvn_updiag_meta meta; + int enabled, tracked, stable, baseline, equal, different_pages; + uint64_t capture_ns, hash_ns, first_different_page; + const char *path, *uncaptured; + char digest[CC_SHA256_DIGEST_LENGTH * 2 + 1]; +} nvn_updiag_record; +typedef struct { + uint64_t serial, bytes; + const void *mem, *pool; + uint8_t *snapshot; + int stable; +} nvn_updiag_slot; +static struct { + char request[64]; + uint64_t polled_frame, first_frame, bytes, total_capture_ns; + unsigned frames, records, comparisons, unchanged, changed, racy; + int initialized, active; + nvn_updiag_slot slots[UPDIAG_SLOTS]; +} g_updiag; + +static uint64_t nvn_updiag_ns(void) { + struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); + return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; +} +static void nvn_updiag_finish(const char *why) { + if (g_updiag.active) + NVN_UPDIAG_LOG("[UPLOAD-DIAG] complete reason=%s records=%u comparisons=%u unchanged=%u changed=%u racy=%u snapshot_bytes=%llu capture_ms=%.3f", + why, g_updiag.records, g_updiag.comparisons, g_updiag.unchanged, + g_updiag.changed, g_updiag.racy, (unsigned long long)g_updiag.bytes, + g_updiag.total_capture_ns / 1e6); + for (int i = 0; i < UPDIAG_SLOTS; i++) { + free(g_updiag.slots[i].snapshot); + memset(&g_updiag.slots[i], 0, sizeof g_updiag.slots[i]); + } + g_updiag.bytes = 0; g_updiag.active = 0; +} +static int nvn_updiag_want(uint64_t frame) { + if (!g_updiag.initialized || frame != g_updiag.polled_frame) { + g_updiag.initialized = 1; g_updiag.polled_frame = frame; + const char *e = getenv("DM_NVN_UPLOAD_DIAG_FRAMES"); + char request[sizeof g_updiag.request]; + snprintf(request, sizeof request, "%s", e ? e : ""); + if (strcmp(request, g_updiag.request)) { + nvn_updiag_finish("request-changed"); + snprintf(g_updiag.request, sizeof g_updiag.request, "%s", request); + long n = strtol(request, NULL, 10); + g_updiag.frames = n > UPDIAG_MAX_FRAMES ? UPDIAG_MAX_FRAMES : n > 0 ? (unsigned)n : 0; + g_updiag.records = g_updiag.comparisons = g_updiag.unchanged = g_updiag.changed = g_updiag.racy = 0; + g_updiag.total_capture_ns = 0; + if (g_updiag.frames) { + g_updiag.first_frame = frame; g_updiag.active = 1; + NVN_UPDIAG_LOG("[UPLOAD-DIAG] begin frame=%llu frames=%u record_cap=%d snapshot_cap=%u; timing includes diagnostic overhead", + (unsigned long long)frame, g_updiag.frames, UPDIAG_RECORDS, UPDIAG_MAX_SNAPSHOTS); + } + } + if (g_updiag.active && frame - g_updiag.first_frame >= g_updiag.frames) + nvn_updiag_finish("frame-limit"); + } + if (g_updiag.active && g_updiag.records >= UPDIAG_RECORDS) nvn_updiag_finish("record-limit"); + return g_updiag.active; +} +static void nvn_updiag_begin(nvn_updiag_record *r, const nvn_updiag_meta *meta) { + memset(r, 0, sizeof *r); + if (!nvn_updiag_want(meta->frame)) return; + r->enabled = 1; r->meta = *meta; r->path = "unresolved"; + r->uncaptured = "upload-not-captured"; r->first_different_page = UINT64_MAX; + g_updiag.records++; +} +// Called immediately after the normal upload has protected its source. The +// generation check rejects a snapshot raced by a write into the covered pages. +static void nvn_updiag_capture(nvn_updiag_record *r) { + if (!r->enabled) return; + uint64_t started = nvn_updiag_ns(); + const nvn_updiag_meta *m = &r->meta; + r->uncaptured = !m->mem || !m->bytes ? "empty" : m->bytes > UPDIAG_MAX_TEXTURE ? "texture-size-cap" : NULL; + if (r->uncaptured) goto done; + int slot = -1; + for (int i = 0; i < UPDIAG_SLOTS; i++) if (g_updiag.slots[i].serial == m->serial) { slot = i; break; } + if (slot < 0) for (int i = 0; i < UPDIAG_SLOTS; i++) if (!g_updiag.slots[i].snapshot) { slot = i; break; } + if (slot < 0) { r->uncaptured = "serial-slot-cap"; goto done; } + nvn_updiag_slot *s = &g_updiag.slots[slot]; + if (g_updiag.bytes - s->bytes + m->bytes > UPDIAG_MAX_SNAPSHOTS) { r->uncaptured = "snapshot-byte-cap"; goto done; } + uint8_t *current = malloc((size_t)m->bytes); + if (!current) { r->uncaptured = "allocation-failed"; goto done; } + const void *span = nvn_dirty_span(m->mem, (size_t)m->bytes); + r->tracked = span != NULL; + uint64_t stamp = nvn_dirty_now(); + memcpy(current, m->mem, (size_t)m->bytes); + r->stable = span && !nvn_dirty_since_span(span, m->mem, (size_t)m->bytes, stamp); + unsigned char digest[CC_SHA256_DIGEST_LENGTH]; + uint64_t hash_started = nvn_updiag_ns(); + CC_SHA256(current, (CC_LONG)m->bytes, digest); + r->hash_ns = nvn_updiag_ns() - hash_started; + for (unsigned i = 0; i < sizeof digest; i++) snprintf(r->digest + i * 2, 3, "%02x", digest[i]); + r->baseline = !(s->snapshot && s->serial == m->serial && s->mem == m->mem && s->pool == m->pool && s->bytes == m->bytes); + if (!r->baseline && r->stable && s->stable) { + size_t pg = (size_t)getpagesize(); + r->equal = 1; g_updiag.comparisons++; + for (size_t offset = 0; offset < m->bytes;) { + size_t n = pg - (((uintptr_t)m->mem + offset) & (pg - 1)); + if (n > m->bytes - offset) n = (size_t)m->bytes - offset; + if (memcmp(s->snapshot + offset, current + offset, n)) { + r->equal = 0; r->different_pages++; + if (r->first_different_page == UINT64_MAX) r->first_different_page = offset; + } + offset += n; + } + if (r->equal) g_updiag.unchanged++; else g_updiag.changed++; + } else if (!r->stable || (!r->baseline && !s->stable)) g_updiag.racy++; + g_updiag.bytes = g_updiag.bytes - s->bytes + m->bytes; + free(s->snapshot); + *s = (nvn_updiag_slot){m->serial, m->bytes, m->mem, m->pool, current, r->stable}; +done: + r->capture_ns = nvn_updiag_ns() - started; + g_updiag.total_capture_ns += r->capture_ns; +} +static void nvn_updiag_end(nvn_updiag_record *r, uint64_t elapsed_ns, int success) { + if (!r->enabled) return; + const nvn_updiag_meta *m = &r->meta; + const char *exact = r->uncaptured ? r->uncaptured : r->baseline ? "baseline" : !r->stable ? "racy" : r->equal ? "unchanged" : r->different_pages ? "changed" : "prior-racy"; + size_t pg = (size_t)getpagesize(); uintptr_t start = (uintptr_t)m->mem & ~(pg - 1); + uintptr_t end = ((uintptr_t)m->mem + (uintptr_t)m->bytes + pg - 1) & ~(pg - 1); + NVN_UPDIAG_LOG("[UPLOAD-DIAG] frame=%llu serial=%llu reupload=%d path=%s success=%d mem=%p pool=%p pool_bytes=%llu offset=%#llx storage=%llu pages=%p..%p size=%ux%ux%u format=%#x target=%u levels=%u flags=%#x tracked=%d stable=%d exact=%s different_pages=%d first_different_page_offset=%#llx sha256=%s encoding_ms=%.3f capture_ms=%.3f hash_ms=%.3f", + (unsigned long long)m->frame, (unsigned long long)m->serial, m->reupload, + r->path, success, m->mem, m->pool, (unsigned long long)m->pool_bytes, + (unsigned long long)m->offset, (unsigned long long)m->bytes, (void *)start, (void *)end, + m->width, m->height, m->depth, m->format, m->target, m->levels, m->flags, + r->tracked, r->stable, exact, r->different_pages, (unsigned long long)r->first_different_page, + *r->digest ? r->digest : "-", (elapsed_ns > r->capture_ns ? elapsed_ns - r->capture_ns : 0) / 1e6, + r->capture_ns / 1e6, r->hash_ns / 1e6); +} diff --git a/dm/gfx/nvn/nvn_zero_image_library.h b/dm/gfx/nvn/nvn_zero_image_library.h new file mode 100644 index 0000000000..fed4758eee --- /dev/null +++ b/dm/gfx/nvn/nvn_zero_image_library.h @@ -0,0 +1,39 @@ +#pragma once +static id zero_image_library(NSString *key, id original, NSData *reflection, + int *variant, int *image_aid, int *image_reg, NSError **error) { + static NSDictionary *manifest; + static dispatch_once_t once; + NSString *directory = [g_msl_dir stringByAppendingPathComponent:@"zero-image-v1"]; + dispatch_once(&once, ^{ + NSData *data = [NSData dataWithContentsOfFile:[directory stringByAppendingPathComponent:@"manifest.json"]]; + manifest = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil; + }); + // A missing companion affects only fragment image writers on old GPUs. + NSDictionary *record = manifest[@"shaders"][key]; + NSString *reason = nil; + if (![manifest[@"schema"] isEqual:@1] || ![manifest[@"chain"] isEqual:@DMSH_CHAIN] || + ![manifest[@"kind"] isEqual:@"zero-image-clear-v1"] || !record || + ![record[@"proof"] isEqual:@"fragment-zero-writes-v1"] || + ![record[@"parameter_slot"] isEqual:@29] || ![record[@"image_aid"] isKindOfClass:NSNumber.class] || + ![record[@"image_reg"] isKindOfClass:NSNumber.class]) reason = @"Missing or unproven zero-image fragment companion"; + NSString *name = record[@"source"]; + if (!reason && (![name isKindOfClass:NSString.class] || ![name isEqual:name.lastPathComponent])) reason = @"Invalid zero-image source path"; + NSData *source = reason ? nil : [NSData dataWithContentsOfFile:[directory stringByAppendingPathComponent:name]]; + NSData *base = reason ? nil : [NSData dataWithContentsOfFile:[g_msl_dir stringByAppendingPathComponent:[key stringByAppendingString:@".metallib"]] options:NSDataReadingMappedIfSafe error:nil]; + if (!reason && (![record[@"source_sha256"] isEqual:nonpoint_digest(source)] || + ![record[@"base_library_sha256"] isEqual:nonpoint_digest(base)] || + ![record[@"reflection_sha256"] isEqual:nonpoint_digest(reflection)])) reason = @"Zero-image source, base library or reflection hash mismatch"; + if (reason) { + if (error) *error = [NSError errorWithDomain:@"NVNZeroImage" code:1 userInfo:@{NSLocalizedDescriptionKey:reason}]; + return nil; + } + NSString *text = [[NSString alloc] initWithData:source encoding:NSUTF8StringEncoding]; + MTLCompileOptions *options = [MTLCompileOptions new]; options.languageVersion = MTLLanguageVersion3_0; + options.preserveInvariance = YES; options.fastMathEnabled = NO; + id library = [g_dev newLibraryWithSource:text options:options error:error]; + if (library) { + *variant = 1; *image_aid = [record[@"image_aid"] intValue]; *image_reg = [record[@"image_reg"] intValue]; + MLOG("Zero-image fragment %s: verified zero-write proof, compatibility buffer 29", key.UTF8String); + } + return library; +} diff --git a/dm/gfx/nvn/nvn_zero_image_runtime.h b/dm/gfx/nvn/nvn_zero_image_runtime.h new file mode 100644 index 0000000000..e38096dde3 --- /dev/null +++ b/dm/gfx/nvn/nvn_zero_image_runtime.h @@ -0,0 +1,94 @@ +#pragma once +#include "nvn_zero_image_shadow.h" +static __strong id g_zero_image_buffer; +static __strong id g_zero_image_texture; +static __strong NvShader *g_zero_image_shader; +static uint32_t g_zero_image_layout[6]; +static uint64_t g_zero_image_serial; +static int g_zero_image_tid; + +static int zero_image_alias(const tex_t *a, const tex_t *b) { + if (!a || !b) return 0; + if (a->serial == b->serial) return 1; + uint64_t an = a->L.size, bn = b->L.size; + if (a->pool && a->pool == b->pool) { + if (!an || !bn) return 1; + if (a->off <= b->off ? b->off - a->off < an : a->off - b->off < bn) return 1; + } + if (a->mem && b->mem) { + uint64_t ap = (uintptr_t)a->mem, bp = (uintptr_t)b->mem; + if (!an || !bn) return ap == bp; + if (ap <= bp ? bp - ap < an : ap - bp < bn) return 1; + } + return 0; +} + +static void zero_image_sync_end(void) { + if (!g_zero_image_buffer) return; + id buffer = g_zero_image_buffer; + id texture = g_zero_image_texture; + g_zero_image_buffer = nil; g_zero_image_texture = nil; g_zero_image_shader = nil; + if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + id blit = enc_b_begin(g_cb); + nvn_zero_image_copy(blit, texture, buffer, g_zero_image_layout, YES); + enc_b_end(blit); + defer_obj_release(buffer); + static int notes; + if (notes++ < 8) MLOG("Zero-image shadow synchronized: %ux%ux%u format=%lu row=%u bytes; uncovered pixels preserved", + g_zero_image_layout[0], g_zero_image_layout[1], g_zero_image_layout[2], + (unsigned long)texture.pixelFormat, g_zero_image_layout[4] * 4); +} + +static int zero_image_prepare(NvProg *program, const p_draw *dw, const p_draw_idx *di, int merged) { + NvShader *fragment = program ? program->sh[1] : nil; + if (g_zero_image_buffer) rp_end(); + if (!fragment || !fragment->zero_image_variant) return 1; + // Preserve the original attachments, tests, raster bounds and visibility. + // Only non-aliasing attachments are allowed while the image is shadowed. + int unsupported = !program->sh[0] || merged || + (dw && dw->ind) || (di && di->ind); + for (int stage = 2; stage < 6; stage++) unsupported |= program->sh[stage] != nil; + if (program->sh[0]) { + unsupported |= program->sh[0]->rf.nv != 0; + for (int i = 0; i < program->sh[0]->rf.nb; i++) unsupported |= program->sh[0]->rf.b[i].kind != NVN_RB_DRV0; + } + if (unsupported) { + MLOG("[RED] Zero-image compatibility requires unmerged direct draw and driver-only vertex inputs (fs=%s)", fragment->key); + return 0; + } + rp_end(); + if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + int tid = (int)(S.img[1][fragment->zero_image_reg] & 0xfffff); + pthread_mutex_lock(&g_rmu); + void *view = NULL; (void)tex_fast_rid(tid, &view); + id texture = (__bridge id)view; + NSUInteger bytes = 0; uint32_t layout[6]; + int valid = tid > 0 && tid < g_ntreg && g_treg[tid].ok && + nvn_zero_image_layout(texture, layout, &bytes) && layout[2] >= 3 && + texture.storageMode != MTLStorageModeMemoryless; + const tex_t *destination = valid ? &g_treg[tid].t : NULL; + if (valid) { + for (int i = 0; i < R.rt.n; i++) valid &= !zero_image_alias(destination, R.rt.color[i]); + valid &= !zero_image_alias(destination, R.rt.depth); + } + uint64_t serial = valid ? g_treg[tid].t.serial : 0; + pthread_mutex_unlock(&g_rmu); + if (!valid) { + MLOG("[RED] Zero-image compatibility unsupported destination: tid=%d type=%lu format=%lu dimensions=%lux%lux%lu", + tid, (unsigned long)texture.textureType, (unsigned long)texture.pixelFormat, + (unsigned long)texture.width, (unsigned long)texture.height, (unsigned long)texture.arrayLength); + return 0; + } + id buffer = [g_dev newBufferWithLength:bytes options:MTLResourceStorageModeShared]; + if (!buffer) { MLOG("[RED] Zero-image shadow allocation failed: %lu bytes", (unsigned long)bytes); return 0; } + res_add(buffer); + if (g_blit) { enc_b_end(g_blit); g_blit = nil; } + id blit = enc_b_begin(g_cb); + nvn_zero_image_copy(blit, texture, buffer, layout, NO); + enc_b_end(blit); + g_zero_image_buffer = buffer; g_zero_image_texture = texture; g_zero_image_shader = fragment; + memcpy(g_zero_image_layout, layout, sizeof layout); + g_zero_image_serial = serial; g_zero_image_tid = tid; + args_dirty_all(); + return 1; +} diff --git a/dm/gfx/nvn/nvn_zero_image_shadow.h b/dm/gfx/nvn/nvn_zero_image_shadow.h new file mode 100644 index 0000000000..c21b4863e7 --- /dev/null +++ b/dm/gfx/nvn/nvn_zero_image_shadow.h @@ -0,0 +1,46 @@ +#pragma once +#import +#include +// Shared by the renderer and readback probe. Only these uncompressed formats +// encode a numeric four-component zero as all-zero 32-bit words. +static NSUInteger nvn_zero_image_bytes(MTLPixelFormat format) { + switch (format) { + case MTLPixelFormatR32Float: case MTLPixelFormatR32Uint: case MTLPixelFormatR32Sint: + case MTLPixelFormatRG16Float: case MTLPixelFormatRGBA8Unorm: case MTLPixelFormatRGBA8Unorm_sRGB: + case MTLPixelFormatBGRA8Unorm: case MTLPixelFormatBGRA8Unorm_sRGB: + case MTLPixelFormatRGBA8Snorm: case MTLPixelFormatRGBA8Uint: case MTLPixelFormatRGBA8Sint: + case MTLPixelFormatRGB10A2Unorm: case MTLPixelFormatRG11B10Float: return 4; + case MTLPixelFormatRG32Float: case MTLPixelFormatRG32Uint: case MTLPixelFormatRG32Sint: + case MTLPixelFormatRGBA16Float: case MTLPixelFormatRGBA16Unorm: + case MTLPixelFormatRGBA16Snorm: case MTLPixelFormatRGBA16Uint: case MTLPixelFormatRGBA16Sint: return 8; + case MTLPixelFormatRGBA32Float: case MTLPixelFormatRGBA32Uint: case MTLPixelFormatRGBA32Sint: return 16; + default: return 0; + } +} +// ABI: width, height, layers, words/pixel, words/row, words/image. +static int nvn_zero_image_layout(id texture, uint32_t layout[6], NSUInteger *bytes) { + NSUInteger pixel = nvn_zero_image_bytes(texture.pixelFormat), w = texture.width, h = texture.height, n = texture.arrayLength; + if (!texture || texture.textureType != MTLTextureType2DArray || texture.sampleCount != 1 || + !pixel || !w || !h || !n || w > UINT32_MAX || h > UINT32_MAX || n > UINT32_MAX || + w > (NSUIntegerMax - 255) / pixel) return 0; + NSUInteger row = (w * pixel + 255) & ~(NSUInteger)255; + if (row > NSUIntegerMax / h) return 0; + NSUInteger image = row * h; + if (image > NSUIntegerMax / n || image / 4 > UINT32_MAX || image * n / 4 > UINT32_MAX) return 0; + layout[0] = (uint32_t)w; layout[1] = (uint32_t)h; layout[2] = (uint32_t)n; + layout[3] = (uint32_t)(pixel / 4); layout[4] = (uint32_t)(row / 4); layout[5] = (uint32_t)(image / 4); + *bytes = image * n; + return 1; +} +static void nvn_zero_image_copy(id blit, id texture, + id buffer, const uint32_t layout[6], BOOL to_texture) { + NSUInteger row = (NSUInteger)layout[4] * 4, image = (NSUInteger)layout[5] * 4; + for (NSUInteger layer = 0; layer < layout[2]; layer++) { + if (to_texture) + [blit copyFromBuffer:buffer sourceOffset:layer * image sourceBytesPerRow:row sourceBytesPerImage:image + sourceSize:MTLSizeMake(layout[0],layout[1],1) toTexture:texture destinationSlice:layer destinationLevel:0 destinationOrigin:MTLOriginMake(0,0,0)]; + else + [blit copyFromTexture:texture sourceSlice:layer sourceLevel:0 sourceOrigin:MTLOriginMake(0,0,0) + sourceSize:MTLSizeMake(layout[0],layout[1],1) toBuffer:buffer destinationOffset:layer * image destinationBytesPerRow:row destinationBytesPerImage:image]; + } +} diff --git a/dm/gfx/tests/.gitignore b/dm/gfx/tests/.gitignore index 0ff7a0d695..c55230bbfa 100644 --- a/dm/gfx/tests/.gitignore +++ b/dm/gfx/tests/.gitignore @@ -147,3 +147,27 @@ t_float_pad t_float_pad.dSYM/ t_cpu_tex t_cpu_tex.dSYM/ +t_upload_diag +t_upload_diag.dSYM/ +t_cpu_decode_cache +t_cpu_decode_cache.dSYM/ +t_reduction_footprint +t_reduction_footprint.dSYM/ +t_storage_access +t_storage_access.dSYM/ +t_storage_access_adapter +t_storage_access_adapter.dSYM/ +t_storage_access_metal +t_storage_access_metal.dSYM/ +t_resource_usage +t_resource_usage.dSYM/ +t_upload_budget +t_upload_budget.dSYM/ +t_cut_retire +t_cut_retire.dSYM/ +t_bc2astc_gpu +t_bc2astc_gpu.dSYM/ +t_astc_hw +t_astc_hw.dSYM/ +t_dirty_retire_race +t_dirty_retire_race.dSYM/ diff --git a/dm/gfx/tests/Makefile b/dm/gfx/tests/Makefile index 87a0e8a3a6..28431060dd 100644 --- a/dm/gfx/tests/Makefile +++ b/dm/gfx/tests/Makefile @@ -26,12 +26,34 @@ t_transform_feedback: t_transform_feedback.m $(MTL_T_DEP) check_t_transform_feedback: t_transform_feedback DM_TEST_TRANSFORM_FEEDBACK=$(CURDIR)/t_transform_feedback MTL_DEBUG_LAYER=1 python3 -m unittest test_transform_feedback.py run: check_t_transform_feedback -t_nvn: t_nvn.c ../nvn/hle_nvn.c ../../host/crash.c ../../host/hle_bigstack.c $(GFX) $(GFX_H) - clang $(CFLAGS) -fobjc-arc -o $@ t_nvn.c ../../host/crash.c ../../host/hle_bigstack.c $(GFX) $(FW) +t_nvn: t_nvn.c ../nvn/hle_nvn.c ../../host/crash.c ../../host/hle_bigstack.c t_host_stubs.c $(GFX) $(GFX_H) + clang $(CFLAGS) -fobjc-arc -o $@ t_nvn.c ../../host/crash.c ../../host/hle_bigstack.c t_host_stubs.c $(GFX) $(FW) t_dirty_slots: t_dirty_slots.c ../nvn/nvn_dirty.c ../../host/crash.c ../nvn/nvn_dirty.h clang $(CFLAGS) -o $@ t_dirty_slots.c ../../host/crash.c ../nvn/nvn_dirty.c t_dirty_race: t_dirty_race.c ../nvn/nvn_dirty.c ../../host/crash.c ../nvn/nvn_dirty.h clang $(CFLAGS) -o $@ t_dirty_race.c ../../host/crash.c +t_dirty_retire_race: t_dirty_retire_race.c ../nvn/nvn_dirty.c ../../host/crash.c ../nvn/nvn_dirty.h + clang $(CFLAGS) -o $@ t_dirty_retire_race.c ../../host/crash.c +t_upload_diag: t_upload_diag.c ../nvn/nvn_upload_diag.h ../nvn/nvn_dirty.h + clang -O1 -g -Wall -Wno-deprecated-declarations -o $@ t_upload_diag.c +t_cpu_decode_cache: t_cpu_decode_cache.c ../nvn/nvn_cpu_decode_cache.h ../nvn/nvn_tex.h ../tex/nvn_bcdec.c ../tex/nvn_bcdec.h ../../third_party/bcdec/bcdec.h + clang -O1 -g -Wall -Wno-unused-function -o $@ t_cpu_decode_cache.c ../tex/nvn_bcdec.c +t_reduction_footprint: t_reduction_footprint.c ../nvn/nvn_reduction_footprint.h + clang -O1 -g -Wall -Wextra -Werror -o $@ t_reduction_footprint.c +t_storage_access: t_storage_access.c ../nvn/nvn_storage_access.h ../nvn/nvn_refl.h + clang -O1 -g -Wall -Wextra -Werror -o $@ t_storage_access.c +t_resource_usage: t_resource_usage.c ../nvn/nvn_resource_usage.h + clang -O1 -g -Wall -Wextra -Werror -o $@ t_resource_usage.c +t_storage_access_adapter: t_storage_access_adapter.m ../nvn/nvn_storage_access_metal.h ../nvn/nvn_storage_access.h ../nvn/nvn_refl.h + clang -O1 -g -Wall -Wextra -Werror -Wno-unused-function -fobjc-arc -o $@ t_storage_access_adapter.m -framework Metal -framework Foundation +t_storage_access_metal: t_storage_access_metal.m ../nvn/nvn_storage_access_metal.h ../nvn/nvn_storage_access.h ../nvn/nvn_resource_usage.h ../nvn/nvn_refl.h + clang -O1 -g -Wall -Wextra -Werror -fobjc-arc -o $@ t_storage_access_metal.m -framework Metal -framework Foundation +.PHONY: storage-access +storage-access: t_storage_access t_storage_access_adapter t_storage_access_metal t_resource_usage + ./t_storage_access + ./t_storage_access_adapter + ./t_storage_access_metal + ./t_resource_usage t_builtin: t_builtin.m ../nvn/nvn_builtin.h clang -O1 -g -Wall -fobjc-arc -o $@ t_builtin.m -framework Metal -framework Foundation t_vin: t_vin.m @@ -46,6 +68,10 @@ t_deswz_gpu: t_deswz_gpu.m ../tex/nvn_deswizzle.c ../tex/nvn_deswizzle_gpu.m ../ clang $(CFLAGS) -fobjc-arc -o $@ t_deswz_gpu.m ../tex/nvn_deswizzle.c ../tex/nvn_deswizzle_gpu.m ../nvn/nvn_tex.c -framework Metal -framework Foundation t_tex_layout: t_tex_layout.c ../nvn/nvn_tex.c ../nvn/nvn_tex.h clang -O1 -Wall -o $@ t_tex_layout.c ../nvn/nvn_tex.c +t_bc2astc_gpu: t_bc2astc_gpu.m ../tex/nvn_bc2astc.c ../tex/nvn_bc2astc.h ../tex/bc2astc_core.h ../tex/bc2astc_body.inc ../tex/bc2astc_msl.h + clang $(CFLAGS) -fobjc-arc -o $@ t_bc2astc_gpu.m ../tex/nvn_bc2astc.c -framework Metal -framework Foundation +t_astc_hw: t_astc_hw.m ../tex/nvn_bc2astc.c ../tex/nvn_bc2astc.h ../tex/bc2astc_core.h ../tex/bc2astc_body.inc + clang $(CFLAGS) -fobjc-arc -o $@ t_astc_hw.m ../tex/nvn_bc2astc.c -framework Metal -framework Foundation t_refl: t_refl.m ../nvn/nvn_refl.m ../nvn/nvn_refl.h ../nvn/dmsh_chain.h clang -O1 -g -Wall -I../nvn -fobjc-arc -o $@ t_refl.m ../nvn/nvn_refl.m -framework Metal -framework Foundation t_hmap: t_hmap.c ../nvn/nvn_hmap.h @@ -211,6 +237,8 @@ t_view_levels: t_view_levels.m $(MTL_T_DEP) clang $(CFLAGS) -fobjc-arc -o $@ t_view_levels.m $(MTL_T_SRC) $(FW) t_upload_budget: t_upload_budget.m $(MTL_T_DEP) clang $(CFLAGS) -fobjc-arc -o $@ t_upload_budget.m $(MTL_T_SRC) $(FW) +t_cut_retire: t_cut_retire.m $(MTL_T_DEP) + clang $(CFLAGS) -fobjc-arc -o $@ t_cut_retire.m $(MTL_T_SRC) $(FW) check_t_upload_budget: t_upload_budget MTL_DEBUG_LAYER=1 ./t_upload_budget DM_NVN_ENC_THREAD=1 MTL_DEBUG_LAYER=1 ./t_upload_budget @@ -274,10 +302,15 @@ gl-imports: t_gl_imports ./t_gl_imports ./t_gl_imports --stubs run: gl-imports -run: t_nvn t_dirty_slots t_dirty_race t_tex_layout t_builtin t_vin t_tbuf2d t_tbuf_align t_sparse_layout t_deswz_gpu t_pace t_timestamp t_fstep t_refl t_hmap t_cover t_fuse_check t_present_crop t_reorder_chain t_defer_chain t_zskip t_xv t_ub t_vbuf t_geometry t_mip_extent t_rt_range_shape t_rt_range_ro t_rt_range t_rt_depth_ro t_pso_skey t_pso_archive t_pso_bundle t_pso_stall t_pull t_pull_inv t_pso_pending t_pfv t_view_levels t_view_swizzle t_vk_desc t_integer_blend t_primitive_restart t_clear_ds t_depth16_clear t_upgrade_alias t_image_usage t_stencil_view +run: t_nvn t_dirty_slots t_dirty_race t_dirty_retire_race t_upload_diag t_cpu_decode_cache t_reduction_footprint t_tex_layout t_builtin t_vin t_tbuf2d t_tbuf_align t_sparse_layout t_deswz_gpu t_bc2astc_gpu t_astc_hw t_pace t_timestamp t_fstep t_refl t_hmap t_cover t_fuse_check t_present_crop t_reorder_chain t_defer_chain t_zskip t_xv t_ub t_vbuf t_geometry t_mip_extent t_rt_range_shape t_rt_range_ro t_rt_range t_rt_depth_ro t_pso_skey t_pso_archive t_pso_bundle t_pso_stall t_pull t_pull_inv t_pso_pending t_pfv t_view_levels t_view_swizzle t_upload_budget t_cut_retire t_vk_desc t_integer_blend t_primitive_restart t_clear_ds t_depth16_clear t_upgrade_alias t_image_usage t_stencil_view +run: storage-access ./t_nvn ./t_dirty_slots ./t_dirty_race + ./t_dirty_retire_race + ./t_upload_diag + ./t_cpu_decode_cache + ./t_reduction_footprint ./t_tex_layout ./t_builtin ./t_vin @@ -286,6 +319,8 @@ run: t_nvn t_dirty_slots t_dirty_race t_tex_layout t_builtin t_vin t_tbuf2d t_tb ./t_tbuf_align ./t_sparse_layout ./t_deswz_gpu + ./t_bc2astc_gpu + ./t_astc_hw ./t_pace ./t_timestamp ./t_fstep @@ -322,6 +357,9 @@ run: t_nvn t_dirty_slots t_dirty_race t_tex_layout t_builtin t_vin t_tbuf2d t_tb MTL_DEBUG_LAYER=1 ./t_pfv MTL_DEBUG_LAYER=1 ./t_view_levels MTL_DEBUG_LAYER=1 ./t_view_swizzle + MTL_DEBUG_LAYER=1 ./t_upload_budget + DM_NVN_ENC_THREAD=1 MTL_DEBUG_LAYER=1 ./t_upload_budget + MTL_DEBUG_LAYER=1 ./t_cut_retire MTL_DEBUG_LAYER=1 ./t_vk_desc MTL_DEBUG_LAYER=1 ./t_integer_blend MTL_DEBUG_LAYER=1 ./t_primitive_restart diff --git a/dm/gfx/tests/reduction_sample_fixture.metal b/dm/gfx/tests/reduction_sample_fixture.metal new file mode 100644 index 0000000000..337dcd957a --- /dev/null +++ b/dm/gfx/tests/reduction_sample_fixture.metal @@ -0,0 +1,15 @@ +// Asset-free compile/readback fixture. The host supplies synthetic R32Float +// mip levels and query records; this file never embeds a game's shader source. +#include +using namespace metal; +#include "../nvn/nvn_reduction_sample.metal.h" + +struct ReductionQuery { float2 uv; float lod; uint maximum; }; +kernel void reduction_sample_fixture( + texture2d tex [[texture(0)]], + device const ReductionQuery *queries [[buffer(0)]], + device float4 *output [[buffer(1)]], uint gid [[thread_position_in_grid]]) +{ + ReductionQuery q = queries[gid]; + output[gid] = dm_reduction_sample_lmn_edge(tex, q.uv, q.lod, 0.0f, 1000.0f, q.maximum != 0); +} diff --git a/dm/gfx/tests/t_astc_hw.m b/dm/gfx/tests/t_astc_hw.m new file mode 100644 index 0000000000..1e8f0fc824 --- /dev/null +++ b/dm/gfx/tests/t_astc_hw.m @@ -0,0 +1,82 @@ +// The GPU's own ASTC decoder against bcdec: BC1 / BC2 / BC3 blocks are transcoded (nvn_bc2astc.c), uploaded as ASTC 4x4 LDR and sRGB textures and read back through a compute kernel +// (what a shader samples). astcenc (tests/t_bc2astc.c in dm/gfx/tex) is the reference decoder there; this checks the hardware agrees, layout by layout. +// Usage: t_astc_hw [-v] +#import +#include +#include +#include +#include +#define BCDEC_IMPLEMENTATION +#include "../../third_party/bcdec/bcdec.h" +#include "../tex/nvn_bc2astc.h" +#include "../tex/bc2astc_core.h" + +static uint64_t rng = 0x9E3779B97F4A7C15ull; +static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 16); } +static void gen(int kind, uint8_t *b) { // as t_bc2astc_gpu.m: families that reach every layout + for (int i = 0; i < 16; i++) b[i] = (uint8_t)rnd(); + uint8_t *c = kind == 1 ? b : b + 8; uint32_t mode = rnd() % 12, c0 = c[0] | c[1] << 8, c1 = c[2] | c[3] << 8; + if (mode == 1) c[4] = c[5] = c[6] = c[7] = (uint8_t)(0x55 * (rnd() & 3)); + if (mode == 2) c1 = c0; + if (mode == 3) { c0 = (rnd() & 1) ? 0 : 0xffff; c1 = (rnd() & 1) ? 0 : 0xffff; } + if (mode == 4 && c0 < c1) { uint32_t t = c0; c0 = c1; c1 = t; } + if (mode == 5 && c0 > c1) { uint32_t t = c0; c0 = c1; c1 = t; } + c[0] = (uint8_t)c0; c[1] = (uint8_t)(c0 >> 8); c[2] = (uint8_t)c1; c[3] = (uint8_t)(c1 >> 8); + uint32_t am = rnd() % 8; + if (kind == 2) { if (am == 1) { uint8_t n = (uint8_t)(rnd() & 15); for (int i = 0; i < 8; i++) b[i] = (uint8_t)(n | n << 4); } if (am == 2) for (int i = 0; i < 8; i++) b[i] = 0xff; } + if (kind == 3) { if (am == 1) { b[1] = b[0]; for (int i = 2; i < 8; i++) b[i] = 0; } if (am == 2) { b[0] = 255; b[1] = 255; for (int i = 2; i < 8; i++) b[i] = 0; } if (am == 3 && b[0] < b[1]) { uint8_t t = b[0]; b[0] = b[1]; b[1] = t; } } +} +static float srgb2lin(float c) { return c <= 0.04045f ? c / 12.92f : powf((c + 0.055f) / 1.055f, 2.4f); } + +static const char *k_msl = +"#include \nusing namespace metal;\n" +"kernel void rd(texture2d t [[texture(0)]], device float4 *o [[buffer(0)]], constant uint &w [[buffer(1)]], uint2 g [[thread_position_in_grid]]) {\n" +" if (g.x >= t.get_width() || g.y >= t.get_height()) return; o[g.y * w + g.x] = t.read(g); }\n"; + +int main(void) { + @autoreleasepool { + id dev = MTLCreateSystemDefaultDevice(); NSError *err = nil; + id lib = [dev newLibraryWithSource:@(k_msl) options:nil error:&err]; if (!lib) { printf("MSL: %s\n", err.localizedDescription.UTF8String); return 1; } + id ps = [dev newComputePipelineStateWithFunction:[lib newFunctionWithName:@"rd"] error:&err]; + id q = [dev newCommandQueue]; + const uint32_t nbx = 256, nby = 128, n = nbx * nby, W = nbx * 4, H = nby * 4; + int fails = 0; const char *ln[5] = {"L0", "L1a", "L1b", "L2", "L3"}; + for (int srgb = 0; srgb < 2; srgb++) for (int kind = 1; kind <= 3; kind++) { + uint8_t *bc = malloc((size_t)n * 16), *astc = malloc((size_t)n * 16); uint8_t *lay = malloc(n); uint8_t (*ref)[16][4] = malloc((size_t)n * 64); + for (uint32_t i = 0; i < n; i++) { gen(kind, bc + (size_t)i * 16); uint32_t r = nvn_bc2astc_block(kind, bc + (size_t)i * 16, astc + (size_t)i * 16); lay[i] = (uint8_t)(r & 0xff); + if (kind == 1) bcdec_bc1(bc + (size_t)i * 16, ref[i], 16); else if (kind == 2) bcdec_bc2(bc + (size_t)i * 16, ref[i], 16); else bcdec_bc3(bc + (size_t)i * 16, ref[i], 16); } + MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:srgb ? MTLPixelFormatASTC_4x4_sRGB : MTLPixelFormatASTC_4x4_LDR width:W height:H mipmapped:NO]; + d.storageMode = MTLStorageModeShared; d.usage = MTLTextureUsageShaderRead; + id t = [dev newTextureWithDescriptor:d]; + [t replaceRegion:MTLRegionMake2D(0, 0, W, H) mipmapLevel:0 withBytes:astc bytesPerRow:nbx * 16]; + id out = [dev newBufferWithLength:(size_t)W * H * 16 options:MTLResourceStorageModeShared]; uint32_t wv = W; + id cb = [q commandBuffer]; id e = [cb computeCommandEncoder]; + [e setComputePipelineState:ps]; [e setTexture:t atIndex:0]; [e setBuffer:out offset:0 atIndex:0]; [e setBytes:&wv length:4 atIndex:1]; + [e dispatchThreads:MTLSizeMake(W, H, 1) threadsPerThreadgroup:MTLSizeMake(8, 8, 1)]; [e endEncoding]; [cb commit]; [cb waitUntilCompleted]; + const float (*px)[4] = out.contents; + long cnt[5] = {0}, bad[5] = {0}; float worst[5] = {0}, worst_a[5] = {0}; int firstbad[5]; for (int l = 0; l < 5; l++) firstbad[l] = -1; + for (uint32_t i = 0; i < n; i++) { + uint32_t bx = i % nbx, by = i / nbx; float mx = 0, ma = 0; + for (int p = 0; p < 16; p++) { + const float *g = px[(by * 4 + p / 4) * W + bx * 4 + (p & 3)]; + for (int c = 0; c < 4; c++) { + float want = ref[i][p][c] / 255.0f; if (srgb && c < 3) want = srgb2lin(want); + if (kind == 1 && ref[i][p][3] == 0 && c < 3) continue; // transparent BC1 texel: RGB is a don't-care + float df = fabsf(g[c] - want) * 255.0f; if (c == 3) { if (df > ma) ma = df; } else if (df > mx) mx = df; + } + } + int L = lay[i]; cnt[L]++; if (mx > worst[L]) worst[L] = mx; if (ma > worst_a[L]) worst_a[L] = ma; + // L3 carries four alpha levels per block, so its alpha differs from bcdec by design (the benchmark measures how much); everything else must agree within 4 levels (L3 colour: within its own endpoint precision) + float tol_rgb = L == 4 ? (srgb ? 12.0f : 6.0f) : 4.0f; // L3's colour endpoints have 48 levels (measured by the benchmark: about 5 of 255; an sRGB texture is compared after the linear conversion, which magnifies dark values) + if (mx > tol_rgb || (L != 4 && ma > 4.0f) || (L == 4 && ma > 80.0f)) { bad[L]++; if (firstbad[L] < 0) firstbad[L] = (int)i; } + } + printf("%s BC%d: ", srgb ? "sRGB" : "LDR ", kind); + for (int l = 0; l < 5; l++) if (cnt[l]) printf("%s %ld blocks, %ld off (worst RGB %.1f, alpha %.1f) ", ln[l], cnt[l], bad[l], worst[l], worst_a[l]); + printf("\n"); + for (int l = 0; l < 5; l++) if (bad[l]) { fails++; int i = firstbad[l]; printf(" first bad %s block %d: BC", ln[l], i); for (int k = 0; k < (kind == 1 ? 8 : 16); k++) printf("%02x", bc[(size_t)i * 16 + k]); printf(" → ASTC "); for (int k = 0; k < 16; k++) printf("%02x", astc[(size_t)i * 16 + k]); printf("\n"); } + free(bc); free(astc); free(lay); free(ref); + } + printf("%s\n", fails ? "hardware ASTC decode DIFFERS from bcdec in some layouts" : "hardware ASTC decode matches bcdec within 4 levels in every layout (L3 alpha: within its four levels)"); + return fails != 0; + } +} diff --git a/dm/gfx/tests/t_bc2astc_gpu.m b/dm/gfx/tests/t_bc2astc_gpu.m new file mode 100644 index 0000000000..5c131ae1b3 --- /dev/null +++ b/dm/gfx/tests/t_bc2astc_gpu.m @@ -0,0 +1,84 @@ +// GPU BC1/BC2/BC3 → ASTC 4x4 transcoder (kernel `bca`, text from bc2astc_msl.h) against the C reference (nvn_bc2astc.c): the output must be byte-identical. +// Millions of blocks per format, mixed so that every layout and both swap directions are reached, through a pitched and offset source / destination; the bytes between the rows must stay untouched. +// Usage: t_bc2astc_gpu +#import +#include +#include +#include +#include +#include "../tex/nvn_bc2astc.h" +#include "../tex/bc2astc_msl.h" + +static uint64_t rng = 0x243F6A8885A308D3ull; +static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 16); } +static uint64_t now_ns(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (uint64_t)t.tv_sec * 1000000000ull + t.tv_nsec; } + +// A BC block of this kind (1/2/3), drawn from families that reach every layout: raw random, solid colour, uniform alpha, equal endpoints, extreme endpoints, c0 > c1 / c0 <= c1 forced +static void gen(int kind, uint8_t *b) { + for (int i = 0; i < 16; i++) b[i] = (uint8_t)rnd(); + uint8_t *c = kind == 1 ? b : b + 8; uint32_t mode = rnd() % 12; + uint32_t c0 = c[0] | c[1] << 8, c1 = c[2] | c[3] << 8; + if (mode == 1) { c[4] = c[5] = c[6] = c[7] = (uint8_t)(0x55 * (rnd() & 3)); } // every colour index the same + if (mode == 2) { c1 = c0; } // equal endpoints + if (mode == 3) { c0 = (rnd() & 1) ? 0 : 0xffff; c1 = (rnd() & 1) ? 0 : 0xffff; } // black / white endpoints + if (mode == 4 && c0 < c1) { uint32_t t = c0; c0 = c1; c1 = t; } // c0 > c1 + if (mode == 5 && c0 > c1) { uint32_t t = c0; c0 = c1; c1 = t; } // c0 <= c1 + c[0] = (uint8_t)c0; c[1] = (uint8_t)(c0 >> 8); c[2] = (uint8_t)c1; c[3] = (uint8_t)(c1 >> 8); + uint32_t am = rnd() % 8; + if (kind == 2) { + if (am == 1) { uint8_t n = (uint8_t)(rnd() & 15); for (int i = 0; i < 8; i++) b[i] = (uint8_t)(n | n << 4); } // uniform alpha (including 15 = 255) + if (am == 2) { for (int i = 0; i < 8; i++) b[i] = 0xff; } + } + if (kind == 3) { + if (am == 1) { b[1] = b[0]; for (int i = 2; i < 8; i++) b[i] = 0; } // a0 == a1, every index 0 + if (am == 2) { b[0] = 255; b[1] = 255; for (int i = 2; i < 8; i++) b[i] = 0; } // opaque + if (am == 3 && b[0] < b[1]) { uint8_t t = b[0]; b[0] = b[1]; b[1] = t; } // 8-level mode + } +} + +int main(void) { + @autoreleasepool { + id dev = MTLCreateSystemDefaultDevice(); if (!dev) { printf("no Metal device\n"); return 1; } + NSError *err = nil; id lib = [dev newLibraryWithSource:@(nvn_bca_msl) options:nil error:&err]; + if (!lib) { printf("MSL compile failed: %s\n", err.localizedDescription.UTF8String); return 1; } + id ps = [dev newComputePipelineStateWithFunction:[lib newFunctionWithName:@"bca"] error:&err]; + if (!ps) { printf("pipeline failed: %s\n", err.localizedDescription.UTF8String); return 1; } + id q = [dev newCommandQueue]; + int fails = 0, checks = 0; + for (int kind = 1; kind <= 3; kind++) { + const uint32_t nbx = 1000, nby = 600, n = nbx * nby, bb = kind == 1 ? 8 : 16; + const uint32_t off_in = 48, pitch_in = nbx * bb + 16, off_out = 64, pitch_out = nbx * 16 + 32; // pitched and offset on both sides + size_t in_size = off_in + (size_t)pitch_in * nby + 32, out_size = off_out + (size_t)pitch_out * nby + 32; + uint8_t *in = calloc(1, in_size), *out_ref = malloc(out_size); + memset(out_ref, 0xCD, out_size); + memset(in, 0xEE, in_size); + long layouts[5] = {0}, swapped = 0, swap_by[5] = {0}; + for (uint32_t y = 0; y < nby; y++) for (uint32_t x = 0; x < nbx; x++) { + uint8_t blk[16]; gen(kind, blk); + memcpy(in + off_in + (size_t)y * pitch_in + (size_t)x * bb, blk, bb); + uint32_t r = nvn_bc2astc_block(kind, blk, out_ref + off_out + (size_t)y * pitch_out + (size_t)x * 16); + layouts[r & 0xff]++; if (r & 0x100) { swapped++; swap_by[r & 0xff]++; } + } + id bin = [dev newBufferWithBytes:in length:in_size options:MTLResourceStorageModeShared]; + id bout = [dev newBufferWithLength:out_size options:MTLResourceStorageModeShared]; memset(bout.contents, 0xCD, out_size); + uint32_t p[7] = {off_in, pitch_in, off_out, pitch_out, nbx, nby, (uint32_t)kind}; + uint64_t t0 = now_ns(); + id cb = [q commandBuffer]; id e = [cb computeCommandEncoder]; + [e setComputePipelineState:ps]; [e setBuffer:bin offset:0 atIndex:0]; [e setBytes:p length:sizeof p atIndex:1]; [e setBuffer:bout offset:0 atIndex:2]; + [e dispatchThreads:MTLSizeMake(nbx, nby, 1) threadsPerThreadgroup:MTLSizeMake(8, 8, 1)]; [e endEncoding]; [cb commit]; [cb waitUntilCompleted]; + double ms = (now_ns() - t0) / 1e6; + if (cb.error) { printf("BC%d: GPU error %s\n", kind, cb.error.localizedDescription.UTF8String); return 1; } + const uint8_t *got = bout.contents; size_t bad = 0, first = (size_t)-1; + for (size_t i = 0; i < out_size; i++) if (got[i] != out_ref[i]) { if (first == (size_t)-1) first = i; bad++; } + checks++; if (bad) { fails++; printf(" ✗ BC%d: %zu of %zu bytes differ (first at %zu)\n", kind, bad, out_size, first); } + // coverage: every layout the format can produce, and swaps in each layout that has endpoints to swap + int want[5] = {1, 1, kind != 1, kind == 1, kind != 1}; + for (int l = 0; l < 5; l++) { if (!want[l]) continue; checks++; if (!layouts[l]) { fails++; printf(" ✗ BC%d: layout %d never reached\n", kind, l); } } + for (int l = 1; l < 5; l++) { if (!want[l]) continue; checks++; if (!swap_by[l] || swap_by[l] == layouts[l]) { fails++; printf(" ✗ BC%d: layout %d swapped %ld of %ld blocks (need both)\n", kind, l, swap_by[l], layouts[l]); } } + printf("BC%d: %u blocks, L0 %ld L1a %ld L1b %ld L2 %ld L3 %ld, %ld swapped, %s, %.1f ms (%.0f Mblocks/s)\n", kind, n, layouts[0], layouts[1], layouts[2], layouts[3], layouts[4], swapped, bad ? "DIFFERENT" : "byte-identical", ms, n / ms / 1e3); + free(in); free(out_ref); + } + printf("%d checks, %d failed\n", checks, fails); + return fails != 0; + } +} diff --git a/dm/gfx/tests/t_cpu_decode_cache.c b/dm/gfx/tests/t_cpu_decode_cache.c new file mode 100644 index 0000000000..5e03149c71 --- /dev/null +++ b/dm/gfx/tests/t_cpu_decode_cache.c @@ -0,0 +1,70 @@ +// Cache parity uses the actual CPU BC decoder. A source write after capture +// must not change the bytes decoded or the cache baseline; GPU staging retains +// an independent copy after eviction. The cache never decides to skip uploads. +#include +#include +#include +static void quiet_log(const char *fmt, ...) { (void)fmt; } +#define NVN_CPU_CACHE_LOG quiet_log +#define NVN_CPU_CACHE_BUDGET 512u +#define NVN_CPU_CACHE_SOURCE_MAX 128u +#define NVN_CPU_CACHE_DECODED_MAX 384u +#include "../nvn/nvn_cpu_decode_cache.h" +#include "../tex/nvn_bcdec.h" + +static void decode(nvn_cpu_decode_ticket *t) { + assert(t->entry && !t->hit); + assert(!nvn_bc_decode(0x4e,t->source,16,4,4,t->decoded,16)); + nvn_cpu_decode_finish(t,1); +} +int main(void) { + uint8_t live[16] = {0x40,0x12,0x32,0x54,0x76,0x98,0xba,0xdc,0xfe,0x31,0x24,0x57,0x68,0x9b,0xac,0xdf}; + uint8_t original[16], expected[64], staging[64]; memcpy(original,live,sizeof live); + assert(!nvn_bc_decode(0x4e,original,16,4,4,expected,16)); + nvn_cpu_decode_key k = {.serial=1,.source_bytes=16,.decoded_bytes=64,.mem=live,.pool=live, + .desc={.target=1,.format=0x4e,.flags=4,.width=4,.height=4,.depth=1,.levels=1}}; + unsetenv("DM_NVN_CPU_DECODE_CACHE"); + nvn_cpu_decode_ticket t=nvn_cpu_decode_begin(0,&k); assert(!t.entry); + setenv("DM_NVN_CPU_DECODE_CACHE","1",1); + t=nvn_cpu_decode_begin(1,&k); assert(t.entry && !t.hit && t.source!=live); + memset(live,0x80,sizeof live); // A real write after the source snapshot. + decode(&t); assert(!memcmp(t.decoded,expected,sizeof expected)); + memcpy(live,original,sizeof live); + t=nvn_cpu_decode_begin(2,&k); assert(t.hit && !memcmp(t.decoded,expected,sizeof expected)); + memcpy(staging,t.decoded,sizeof staging); + live[1]^=0xff; + t=nvn_cpu_decode_begin(3,&k); assert(t.entry && !t.hit); + uint8_t changed[64]; assert(!nvn_bc_decode(0x4e,live,16,4,4,changed,16)); + decode(&t); assert(!memcmp(t.decoded,changed,sizeof changed)); + k.desc.levels=2; + t=nvn_cpu_decode_begin(4,&k); assert(t.entry && !t.hit); nvn_cpu_decode_finish(&t,0); + k.desc.levels=1; + t=nvn_cpu_decode_begin(5,&k); assert(t.entry && !t.hit); decode(&t); + k.pool=original; + t=nvn_cpu_decode_begin(6,&k); assert(t.entry && !t.hit); nvn_cpu_decode_finish(&t,0); + t=nvn_cpu_decode_begin(7,&k); assert(t.entry && !t.hit); decode(&t); + // 80 bytes per entry: the seventh distinct key must evict within 512 bytes. + for (unsigned i=2;i<12;i++) { + k.serial=i; t=nvn_cpu_decode_begin(8+i,&k); assert(t.entry && !t.hit); decode(&t); + assert(g_cpu_decode_cache.bytes<=NVN_CPU_CACHE_BUDGET); + } + assert(g_cpu_decode_cache.evictions>0 && !memcmp(staging,expected,sizeof staging)); + k.source_bytes=NVN_CPU_CACHE_SOURCE_MAX+1; + t=nvn_cpu_decode_begin(30,&k); assert(!t.entry); // Never reads this invalid extent. + k.source_bytes=16;k.decoded_bytes=NVN_CPU_CACHE_DECODED_MAX+1; + t=nvn_cpu_decode_begin(31,&k); assert(!t.entry); + setenv("DM_NVN_CPU_DECODE_CACHE","0",1);k.decoded_bytes=64; + t=nvn_cpu_decode_begin(32,&k);assert(!t.entry && !g_cpu_decode_cache.bytes); + for (int i=0;icut_eligible, "replay-thread retirement is eligible for this segment"); + CHECK(local_retirement->submission_active && local_retirement->fence == 42, + "active replay publishes its pending fence to retirement records"); + CHECK(local_retirement->site > 0 && local_retirement->sub_serial == 7 && local_retirement->kind == 0, + "retirement diagnostics record source, submission and object class"); + + NSObject *foreign = [NSObject new]; + pthread_t thread; CHECK(!pthread_create(&thread, NULL, retire_on_foreign_thread, (__bridge void *)foreign), "foreign retirement thread starts"); + pthread_join(thread, NULL); + NvDeferredRelease *foreign_retirement = g_deferred.lastObject; + foreign = nil; + CHECK(!foreign_retirement->cut_eligible, "other-thread retirement keeps the whole-submission fence"); + + defer_diag_summary summary; pthread_mutex_lock(&g_deferred_mu); defer_diag_collect_locked(&summary); pthread_mutex_unlock(&g_deferred_mu); + CHECK(summary.count == 2 && summary.cut == 1 && summary.active == 2 && summary.other == 2 && + summary.first_sub == 7 && summary.last_sub == 7 && summary.first_fence == 42 && summary.last_fence == 42, + "diagnostic aggregation classifies replay and foreign records without a Metal device"); + + NSArray *cut = defer_take_cut_batch(); + CHECK(cut.count == 1 && cut.firstObject == local_retirement, + "cut takes only replay-thread records for its pending fence"); + CHECK(g_deferred.count == 1 && g_deferred.firstObject == foreign_retirement, + "conservative record stays on the global deferred queue"); + CHECK(g_fence == fence0, "taking a split batch does not advance the guest-visible fence"); + CHECK(atomic_load(&g_done) == done0, "taking a split batch does not signal guest completion"); + defer_release_batch(cut); + CHECK(!local_retirement->object, "split completion retires replay-thread objects"); + CHECK(foreign_retirement->object != nil, "split completion preserves other-thread objects"); + + atomic_store(&g_done, 41); defer_drain(); + CHECK(foreign_retirement->object != nil, "record stays alive before its terminal fence"); + atomic_store(&g_done, 42); defer_drain(); + CHECK(!foreign_retirement->object, "terminal completion retires the conservative remainder"); + CHECK(g_deferred.count == 0, "terminal drain empties the global deferred queue"); + + // A CPU-only loader can retire a replay-only derived descriptor while no + // replay exists. Enqueue must not destroy it under g_rmu; the post-unlock + // safe point drains it against the already-completed current fence. + atomic_store(&g_sub_active, 0); g_defer_cut_active = 0; + atomic_store(&g_fence, 50); atomic_store(&g_done, 50); atomic_store(&g_sub_serial, 8); + NSObject *idle = [NSObject new]; __weak NSObject *idle_weak = idle; + defer_obj_release_ex(idle, 0, 1, 1234); idle = nil; + CHECK(g_deferred.count == 1 && idle_weak != nil && atomic_load(&g_defer_replay_view_drain_needed), + "idle descriptor retirement requests a drain without releasing under the registry lock"); + defer_replay_view_drain_safe_point(); + CHECK(g_deferred.count == 0 && idle_weak == nil && !atomic_load(&g_defer_replay_view_drain_needed), + "post-unlock safe point drains an idle descriptor on the completed current fence"); + + // With a prior command buffer still in flight, the same derived view waits for + // that real fence, not an invented next one. + atomic_store(&g_fence, 60); atomic_store(&g_done, 59); + NSObject *prior = [NSObject new]; defer_obj_release_ex(prior, 1, 1, 2345); + NvDeferredRelease *prior_retirement = g_deferred.lastObject; prior = nil; + CHECK(prior_retirement->fence == 60 && !prior_retirement->submission_active, + "idle replay-view retirement targets the last committed in-flight fence"); + pthread_mutex_lock(&g_deferred_mu); defer_diag_collect_locked(&summary); pthread_mutex_unlock(&g_deferred_mu); + CHECK(summary.count == 1 && summary.keep == 1 && summary.replay_view == 1 && summary.sites[0].site == 2345, + "diagnostic aggregation preserves keep-resident, replay-view and call-site fields"); + defer_drain(); CHECK(prior_retirement->object != nil, "prior in-flight fence still protects its derived view"); + atomic_store(&g_done, 60); defer_drain(); + CHECK(!prior_retirement->object && g_deferred.count == 0, "prior fence completion releases the idle derived view"); + + // A derived view replaced while a replay is active is still protected by that + // replay's pending terminal fence, even if the previous fence is done. + atomic_store(&g_fence, 70); atomic_store(&g_done, 70); + atomic_store(&g_sub_pending_fence, 71); atomic_store(&g_sub_active, 1); + NSObject *active_view = [NSObject new]; defer_obj_release_ex(active_view, 0, 1, 3456); + NvDeferredRelease *active_retirement = g_deferred.lastObject; active_view = nil; + CHECK(active_retirement->fence == 71 && active_retirement->submission_active && active_retirement->object != nil, + "active replay keeps derived-view retirement on its pending fence"); + atomic_store(&g_sub_active, 0); defer_drain(); + CHECK(active_retirement->object != nil, "completed previous fence cannot release an active replay view"); + atomic_store(&g_done, 71); defer_drain(); + CHECK(!active_retirement->object && g_deferred.count == 0, "active replay view releases at its terminal fence"); + + // Production begin/commit helpers serialize the state pair with enqueue. + // A replay-view retirement between those transitions must use exactly the + // pending fence, and closing the state must happen only after commit. + atomic_store(&g_fence, 75); atomic_store(&g_done, 75); atomic_store(&g_sub_active, 0); + defer_submission_begin_state(); + CHECK(atomic_load(&g_sub_active) && atomic_load(&g_sub_pending_fence) == 76, + "submission begin publishes one locked active/pending state"); + NSObject *state_view = [NSObject new]; defer_obj_release_ex(state_view, 1, 1, 4000); + NvDeferredRelease *state_retirement = g_deferred.lastObject; state_view = nil; + CHECK(state_retirement->fence == 76 && state_retirement->submission_active, + "retirement serialized after begin uses the active pending fence"); + uint64_t prepared = defer_submission_prepare_commit(); + CHECK(prepared == 76 && atomic_load(&g_sub_active), + "prepare publishes the fence but keeps replay active until queue commit"); + defer_submission_did_commit(prepared); + CHECK(!atomic_load(&g_sub_active), "did-commit closes the locked replay state"); + defer_drain(); CHECK(state_retirement->object != nil, "prepared fence is not complete early"); + atomic_store(&g_done, 76); defer_drain(); + CHECK(!state_retirement->object && g_deferred.count == 0, + "locked replay state retirement releases at completion"); + + // Pool/serial/base-view objects are deliberately not on the replay-view + // allowlist. An independent upload or present CB can still refer to them + // while g_done == g_fence; the next main-queue fence is the conservative + // same-queue completion gate. + atomic_store(&g_fence, 80); atomic_store(&g_done, 80); + NSObject *upload_ref = [NSObject new], *present_ref = [NSObject new]; + __weak NSObject *upload_weak = upload_ref; + __weak NSObject *present_weak = present_ref; + defer_obj_release_ex(upload_ref, 0, 0, 4567); + defer_obj_release_ex(present_ref, 1, 0, 5678); + NvDeferredRelease *upload_retirement = g_deferred.firstObject; + NvDeferredRelease *present_retirement = g_deferred.lastObject; + upload_ref = present_ref = nil; + CHECK(upload_retirement->fence == 81 && present_retirement->fence == 81 && + !upload_retirement->replay_view_retirement && !present_retirement->replay_view_retirement, + "upload/present-capable objects remain gated by a future same-queue submission"); + defer_replay_view_drain_safe_point(); defer_drain(); + CHECK(upload_weak != nil && present_weak != nil && g_deferred.count == 2, + "completed replay fence cannot release independent-CB references"); + atomic_store(&g_done, 81); defer_drain(); + CHECK(upload_weak == nil && present_weak == nil && g_deferred.count == 0, + "future same-queue completion releases conservative independent-CB references"); + + // Pool finalization must also detach replay-only derived views when the + // ordinary descriptor view is already empty. Those views retain their + // parent storage and otherwise pin a finalized pool indefinitely. + atomic_store(&g_fence, 90); atomic_store(&g_done, 90); atomic_store(&g_sub_active, 0); + pool_t *pool = (pool_t *)(uintptr_t)0x12340000; + g_treg = calloc(1, sizeof *g_treg); g_ntreg = 1; + g_treg[0].ok = 1; g_treg[0].t.pool = pool; + NSObject *alt = [NSObject new], *ar = [NSObject new]; + __weak NSObject *alt_weak = alt; + __weak NSObject *ar_weak = ar; + g_treg[0].alt = (void *)CFBridgingRetain(alt); + g_treg[0].ar = (void *)CFBridgingRetain(ar); + alt = ar = nil; + treg_drop_pool_views(pool); + CHECK(!g_treg[0].alt && !g_treg[0].ar, + "pool finalization detaches alt/ar without a base descriptor view"); + CHECK(alt_weak == nil && ar_weak == nil && g_deferred.count == 0, + "idle replay-only pool views drain after the registry lock is released"); + free(g_treg); g_treg = NULL; g_ntreg = 0; + + printf("cut retirement: %d checks, %d failed\n", checks, fails); + return fails != 0; +} } diff --git a/dm/gfx/tests/t_dirty_retire_race.c b/dm/gfx/tests/t_dirty_retire_race.c new file mode 100644 index 0000000000..9a8dd3e21c --- /dev/null +++ b/dm/gfx/tests/t_dirty_retire_race.c @@ -0,0 +1,47 @@ +// A pool retired while protect holds its descriptor must finish writable. +// Stage retirement after lookup, before the first protection-state update. +static void protect_lookup_hook(void); +#define NVN_DIRTY_PROTECT_LOOKUP_HOOK() protect_lookup_hook() +#include "../nvn/nvn_dirty.c" +#include +#include + +module_t *g_mod; +int g_nmod; +module_t *module_of(const void *p) { (void)p; return NULL; } +void crash_install(void); + +static void *page; +static _Atomic int go, removing, removed; +static void protect_lookup_hook(void) { + atomic_store(&go, 1); + while (!atomic_load(&removing)) usleep(100); + // The unfixed implementation lets retirement complete in this window. + usleep(100000); +} +static void *retire(void *unused) { + (void)unused; + while (!atomic_load(&go)) usleep(100); + atomic_store(&removing, 1); + nvn_dirty_remove_range(page); + atomic_store(&removed, 1); + return NULL; +} +int main(void) { + size_t size = (size_t)getpagesize(); + page = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + if (page == MAP_FAILED) return 2; + nvn_dirty_add_range(page, size); + pthread_t worker; + pthread_create(&worker, NULL, retire, NULL); + nvn_dirty_protect(page, size); + pthread_join(worker, NULL); + int fd = open("/dev/zero", O_RDONLY); + ssize_t bytes = read(fd, page, 16); + printf("retirement completed: %d; write into retired pool: %zd bytes (expected 16)\n", atomic_load(&removed), bytes); + close(fd); + // Restore permissions so the failing control exits without a second fault. + mprotect(page, size, PROT_READ | PROT_WRITE); + munmap(page, size); + return bytes == 16 && atomic_load(&removed) ? 0 : 1; +} diff --git a/dm/gfx/tests/t_host_stubs.c b/dm/gfx/tests/t_host_stubs.c index 5d381e29ed..60283ea28d 100644 --- a/dm/gfx/tests/t_host_stubs.c +++ b/dm/gfx/tests/t_host_stubs.c @@ -3,3 +3,4 @@ __attribute__((weak)) uint64_t g_t0_ns; // process start time (host main.c), read by hle_nvn's per-frame timing __attribute__((weak)) _Atomic uint64_t dm_present_ns; // when the last present was made, for DM_HITCH_DUMP (hle_thread.c) __attribute__((weak)) int dm_guest_as_contains(uintptr_t address) { (void)address; return 0; } // no guest address space (hle_bigstack.c) +__attribute__((weak)) int front_screen_max_fps(void) { return 60; } // ordinary test display; dedicated pacing tests override this diff --git a/dm/gfx/tests/t_reduction_footprint.c b/dm/gfx/tests/t_reduction_footprint.c new file mode 100644 index 0000000000..8aaf81790f --- /dev/null +++ b/dm/gfx/tests/t_reduction_footprint.c @@ -0,0 +1,77 @@ +// Synthetic, asset-free CPU fixture for the coordinate math used by MSL. +// clang -O1 -Wall dm/gfx/tests/t_reduction_footprint.c -o /tmp/t_reduction_footprint +#include "../nvn/nvn_reduction_footprint.h" +#include +#include + +static float sample(const float *image, unsigned width, unsigned height, float u, float v, int maximum) { + dm_reduction_footprint p = dm_reduction_footprint_make(u, v, width, height); + float result = image[p.y.lo * width + p.x.lo]; + const unsigned x[4] = {p.x.lo, p.x.hi, p.x.lo, p.x.hi}; + const unsigned y[4] = {p.y.lo, p.y.lo, p.y.hi, p.y.hi}; + for (unsigned i = 1; i < 4; i++) if (p.mask & (1u << i)) { + float tap = image[y[i] * width + x[i]]; + result = maximum ? fmaxf(result, tap) : fminf(result, tap); + } + return result; +} +static void expect(float actual, float expected) { assert(fabsf(actual - expected) < 1e-6f); } + +int main(void) { + const float image[] = { + .8f, .2f, .9f, .7f, + .6f, .4f, .3f, .1f, + .75f, .65f, .55f, .45f, + .35f, .25f, .15f, .05f + }; + dm_reduction_footprint p = dm_reduction_footprint_make(.125f, .125f, 4, 4); + assert(p.mask == 1 && p.x.lo == 0 && p.y.lo == 0); + // The unused right/bottom neighbors are smaller: unconditional four-tap MIN + // would fail this test even though all coordinates were clamped correctly. + expect(sample(image, 4, 4, .125f, .125f, 0), .8f); + expect(sample(image, 4, 4, .125f, .125f, 1), .8f); + p = dm_reduction_footprint_make(.25f, .125f, 4, 4); assert(p.mask == 3); + expect(sample(image, 4, 4, .25f, .125f, 0), .2f); + p = dm_reduction_footprint_make(.125f, .25f, 4, 4); assert(p.mask == 5); + expect(sample(image, 4, 4, .125f, .25f, 0), .6f); + p = dm_reduction_footprint_make(.25f, .25f, 4, 4); assert(p.mask == 15); + expect(sample(image, 4, 4, .25f, .25f, 0), .2f); + expect(sample(image, 4, 4, .25f, .25f, 1), .8f); + puts("footprint: texel center/axis zero weights and four-tap MIN/MAX: PASS"); + + const float edge_u[] = {0, 1, 0, 1, .5f}, edge_v[] = {0, 0, 1, 1, .5f}; + const float edge_min[] = {.8f, .7f, .35f, .05f, .3f}; + const float edge_max[] = {.8f, .7f, .35f, .05f, .65f}; + for (unsigned i = 0; i < 5; i++) { + expect(sample(image, 4, 4, edge_u[i], edge_v[i], 0), edge_min[i]); + expect(sample(image, 4, 4, edge_u[i], edge_v[i], 1), edge_max[i]); + } + expect(sample(image, 4, 4, -100, 100, 0), .35f); + expect(sample(image, 4, 4, INFINITY, -INFINITY, 0), .7f); + expect(sample(image, 4, 4, NAN, NAN, 0), .8f); + puts("address: five corner/center probes, outside/Inf clamps and NaN sanitize: PASS"); + + const float singleton[] = {.43f}; + expect(sample(singleton, 1, 1, 0, 1, 0), .43f); + expect(sample(singleton, 1, 1, .5f, .5f, 1), .43f); + const float odd[] = {.1f, .8f, .4f, .7f, .9f, .3f}; + expect(sample(odd, 3, 2, .5f, .25f, 0), .8f); + expect(sample(odd, 3, 2, .5f, .5f, 0), .8f); + expect(sample(odd, 3, 2, .5f, .5f, 1), .9f); + puts("extents: single texel and non-power-of-two/unequal dimensions: PASS"); + + for (unsigned i = 0; i < 11; i++) assert(dm_reduction_mip_nearest((float)i, 0, 1000, 11) == i); + assert(dm_reduction_mip_nearest(-10, 0, 1000, 11) == 0); + assert(dm_reduction_mip_nearest(1000, 0, 1000, 11) == 10); + assert(dm_reduction_mip_nearest(9, 2, 4, 11) == 4); + assert(dm_reduction_mip_nearest(0, 2, 4, 11) == 2); + assert(dm_reduction_mip_nearest(9, 0, 1000, 1) == 0); + // Fractional behavior is the documented GL prototype, not an NVN hardware + // validation claim. Both observed game shaders supply integer LODs. + assert(dm_reduction_mip_nearest(.5f, 0, 1000, 11) == 0); + assert(dm_reduction_mip_nearest(.5001f, 0, 1000, 11) == 1); + assert(dm_reduction_mip_nearest(1.5f, 0, 1000, 11) == 1); + assert(dm_reduction_mip_nearest(1.5001f, 0, 1000, 11) == 2); + puts("LOD: all 11 observed integer levels, view/sampler clamps and GL half-tie prototype: PASS"); + return 0; +} diff --git a/dm/gfx/tests/t_resource_usage.c b/dm/gfx/tests/t_resource_usage.c new file mode 100644 index 0000000000..d0c6d0befe --- /dev/null +++ b/dm/gfx/tests/t_resource_usage.c @@ -0,0 +1,140 @@ +// Asset-free per-encoder resource-usage union tests. No Metal/GPU required. +// clang -O1 -Wall -Wextra -Werror -fsanitize=address,undefined \ +// dm/gfx/tests/t_resource_usage.c -o /tmp/t_resource_usage +#include +#include +#include +#include + +static unsigned g_allocations, g_fail_next_allocation; +static void *test_realloc(void *p, size_t bytes) { + g_allocations++; + if (g_fail_next_allocation) { g_fail_next_allocation = 0; return NULL; } + return realloc(p, bytes); +} +#define NVN_RESOURCE_USAGE_REALLOC test_realloc +#include "../nvn/nvn_resource_usage.h" +#undef NVN_RESOURCE_USAGE_REALLOC + +enum { READ = 1, WRITE = 2, SAMPLE = 4, MAX_RESOURCES = 4096 }; +typedef struct { uintptr_t resource; uint64_t usage; } reference_entry; +typedef struct { + nvn_resource_usage_set actual; + reference_entry reference[MAX_RESOURCES]; + size_t count; +} context; +static unsigned g_checks; +#define REQUIRE(condition, what) do { g_checks++; if (!(condition)) { \ + fprintf(stderr, "FAIL line %d: %s\n", __LINE__, what); exit(1); } } while (0) + +static void begin(context *c) { + nvn_resource_usage_begin(&c->actual); c->count = 0; +} +static void add(context *c, uintptr_t resource, uint64_t requested, int dedup) { + size_t index = 0; + while (index < c->count && c->reference[index].resource != resource) index++; + uint64_t old = index < c->count ? c->reference[index].usage : 0; + uint64_t expected = old | requested, combined = UINT64_MAX; + int skip = -1; + REQUIRE(nvn_resource_usage_add(&c->actual, resource, requested, dedup, &combined, &skip), + "usage request succeeds when allocation is available"); + REQUIRE(combined == expected, "declaration contains the full encoder-lifetime access union"); + REQUIRE(skip == (dedup && old != 0 && (old & requested) == requested), + "dedup skips only a declaration already covered by the existing union"); + if (index == c->count) { + REQUIRE(c->count < MAX_RESOURCES, "reference oracle capacity"); + c->reference[c->count++] = (reference_entry){resource, 0}; + } + c->reference[index].usage = expected; +} +static uintptr_t crowded_key(size_t index) { + // These aligned fake objects share a fast-table start in the published + // 512-slot scheme. More than 512 distinct keys also exceed total fast + // capacity even if a future implementation changes its hash mixing. + return (uintptr_t)0x100000 + (uintptr_t)index * 8192; +} +static void ordinary_aliases_and_epochs(void) { + context c = {0}; begin(&c); + const uintptr_t input = 0x1110, pool = 0x2220, independent = 0x3330; + add(&c, input, READ, 1); add(&c, input, READ, 1); + add(&c, pool, READ, 1); // UBO view of a shared pool buffer + add(&c, pool, READ | WRITE, 1); // writable SBUF in that same buffer + add(&c, pool, READ, 1); + add(&c, input, READ, 1); // a different input stays read-only + add(&c, independent, WRITE, 1); add(&c, independent, READ, 1); + begin(&c); + add(&c, pool, READ | WRITE, 1); // reverse SBUF -> UBO call order + add(&c, pool, READ, 1); add(&c, pool, SAMPLE, 1); + add(&c, pool, READ, 0); // dedup off still sends the full union + add(&c, pool, WRITE, 0); add(&c, pool, SAMPLE, 0); + begin(&c); + add(&c, pool, READ | WRITE, 0); add(&c, pool, READ, 0); + add(&c, pool, READ, 0); add(&c, input, READ, 0); + puts("RO input, UBO/SBUF aliases in both orders, and dedup-off unions: PASS"); + + // The allocator may reuse both encoder and ObjC resource addresses. Every + // explicit begin represents a new encoder even when those addresses match. + for (unsigned i = 0; i < 128; i++) { + begin(&c); add(&c, pool, i & 1 ? READ : READ | WRITE, 1); + add(&c, pool, READ, 1); + } + // Exercise the epoch rollover without waiting for 2^64 encoder creations. + c.actual.epoch = UINT64_MAX; begin(&c); + // independent was written only in epoch 1. Its old epoch value now matches + // again, so failing to clear stale fast entries would resurrect Write. + add(&c, independent, READ, 1); add(&c, pool, READ, 1); + nvn_resource_usage_dispose(&c.actual); + REQUIRE(c.actual.overflow == NULL && c.actual.epoch == 0, "dispose releases storage and resets state"); + puts("same resource/encoder identities across fresh epochs and rollover: PASS"); +} +static void collisions_and_capacity(void) { + context c = {0}; begin(&c); + enum { COUNT = 2048 }; + for (size_t i = 0; i < COUNT; i++) add(&c, crowded_key(i), WRITE, (int)(i & 1)); + // Revisit from both ends after overflowing every possible fast-table slot. + // An evicting implementation would incorrectly forget earlier writes. + for (size_t i = COUNT; i-- > 0;) add(&c, crowded_key(i), READ, 1); + for (size_t i = 0; i < COUNT; i++) { + add(&c, crowded_key(i), READ, 0); add(&c, crowded_key(i), WRITE, 1); + } + begin(&c); + for (size_t i = 0; i < COUNT; i++) add(&c, crowded_key(i), READ, 1); + nvn_resource_usage_dispose(&c.actual); + puts("collisions, capacity overflow, revisit order, and overflow epoch reset: PASS"); +} +static size_t fill_until_allocation_failure(context *c, size_t first) { + g_fail_next_allocation = 1; unsigned before = g_allocations; + for (size_t i = first; i < MAX_RESOURCES; i++) { + uint64_t combined; int skip; + if (!nvn_resource_usage_add(&c->actual, crowded_key(i), WRITE, 1, &combined, &skip)) { + REQUIRE(!g_fail_next_allocation && g_allocations == before + 1, + "OOM was injected at a real overflow allocation"); + return i; + } + // Record the successful request through the independent oracle. Its + // second insertion must be a covered request, preserving Write. + REQUIRE(c->count < MAX_RESOURCES, "OOM oracle capacity"); + c->reference[c->count++] = (reference_entry){crowded_key(i), WRITE}; + add(c, crowded_key(i), WRITE, 1); + } + REQUIRE(0, "dynamic overflow storage reaches the injected allocator failure"); + return 0; +} +static void allocation_failure_preserves_history(void) { + context c = {0}; begin(&c); + size_t failed = fill_until_allocation_failure(&c, 0); + REQUIRE(failed > 0, "failure occurs after prior resource declarations exist"); + for (size_t i = 0; i < failed; i++) add(&c, crowded_key(i), READ, 1); + add(&c, crowded_key(failed), WRITE, 1); // failed insertion is retryable + failed = fill_until_allocation_failure(&c, failed + 1); // fail a later vector growth + for (size_t i = failed; i-- > 0;) add(&c, crowded_key(i), READ, 0); + add(&c, crowded_key(failed), WRITE, 1); + add(&c, crowded_key(failed), READ, 0); + nvn_resource_usage_dispose(&c.actual); + puts("initial/growth OOM returns failure while preserving previous Write history: PASS"); +} +int main(void) { + ordinary_aliases_and_epochs(); collisions_and_capacity(); allocation_failure_preserves_history(); + printf("Resource-usage union: PASS (%u checks, fake resource identities, no GPU)\n", g_checks); + return 0; +} diff --git a/dm/gfx/tests/t_storage_access.c b/dm/gfx/tests/t_storage_access.c new file mode 100644 index 0000000000..39093de330 --- /dev/null +++ b/dm/gfx/tests/t_storage_access.c @@ -0,0 +1,72 @@ +// Asset-free policy regression: metadata + compiled member IDs -> final rw. +#include "../nvn/nvn_storage_access.h" +#include +#include +#include + +static nvn_refl fixture(void) { + nvn_refl r = {.route_b = 1, .nb = 5, .abi = 0}; + r.b[0] = (nvn_rbind){.kind = NVN_RB_SBUFP, .reg = 0, .aid = 2}; + r.b[1] = (nvn_rbind){.kind = NVN_RB_SBUFP, .reg = 1, .aid = 7}; + r.b[2] = (nvn_rbind){.kind = NVN_RB_SBUFP, .reg = 2, .aid = 11}; + r.b[3] = (nvn_rbind){.kind = NVN_RB_SBUFP, .reg = 3, .aid = 15}; + r.b[4] = (nvn_rbind){.kind = NVN_RB_CBUF, .reg = 0, .aid = 17}; + return r; +} +static nvn_storage_access_member member(unsigned aid, int access) { + return (nvn_storage_access_member){.aid = aid, .offset = 8 * aid, + .is_pointer = 1, .has_pointer_type = 1, .access = access}; +} +int main(void) { + // Deliberately shuffled sparse IDs, not member ordinals. The pure fixture + // uses the route-B raw-slot layout; a separate Metal test observes real ABI. + nvn_storage_access_member m[] = {member(11, NVN_STORAGE_WRITE_ONLY), member(17, NVN_STORAGE_READ_WRITE), + member(2, NVN_STORAGE_READ_ONLY), member(7, NVN_STORAGE_READ_WRITE)}; + const size_t len = 18 * 8, n = sizeof m / sizeof *m; + nvn_refl r = fixture(); nvn_storage_access_result v[5]; + for (int i = 0; i < r.nb; i++) v[i] = nvn_storage_access_apply(&r, i, m, n, 1, len); + assert(!r.b[0].rw && r.b[1].rw && r.b[2].rw && !r.b[3].rw && !r.b[4].rw); + assert(v[1].promoted && v[2].promoted && !v[4].promoted); + assert(v[3].status == NVN_STORAGE_MEMBER_MISSING && v[4].status == NVN_STORAGE_NOT_STORAGE); + assert(!nvn_storage_access_apply(&r, 1, m, n, 1, len).promoted); // idempotent + r.b[0].rw = 1; + assert(nvn_storage_access_apply(&r, 0, m, n, 1, len).status == NVN_STORAGE_COMPILED_READ_ONLY && r.b[0].rw); + assert(!strcmp(nvn_storage_access_status(v[1].status), "compiled-write")); + + // Every unsuccessful/unknown outcome preserves both false and true flags. + for (int old = 0; old < 2; old++) { + r = fixture(); r.b[1].rw = (uint8_t)old; + nvn_storage_access_member bad[] = {m[3], m[3]}; + v[0] = nvn_storage_access_apply(&r, 1, m, n, 0, len); + assert(v[0].status == NVN_STORAGE_NO_REFLECTION && r.b[1].rw == old); + v[0] = nvn_storage_access_apply(&r, 1, NULL, n, 1, len); + assert(v[0].status == NVN_STORAGE_NO_REFLECTION && r.b[1].rw == old); + v[0] = nvn_storage_access_apply(&r, 1, bad, 2, 1, len); + assert(v[0].status == NVN_STORAGE_MEMBER_DUPLICATE && r.b[1].rw == old); + bad[0] = m[3]; bad[0].offset++; + v[0] = nvn_storage_access_apply(&r, 1, bad, 1, 1, len); + assert(v[0].status == NVN_STORAGE_BAD_OFFSET && r.b[1].rw == old); + bad[0] = m[3]; bad[0].access = 17; + assert(nvn_storage_access_apply(&r, 1, bad, 1, 1, len).status == NVN_STORAGE_UNKNOWN_ACCESS && r.b[1].rw == old); + bad[0] = m[3]; bad[0].is_pointer = 0; + assert(nvn_storage_access_apply(&r, 1, bad, 1, 1, len).status == NVN_STORAGE_NOT_POINTER && r.b[1].rw == old); + bad[0] = m[3]; bad[0].has_pointer_type = 0; + assert(nvn_storage_access_apply(&r, 1, bad, 1, 1, len).status == NVN_STORAGE_NOT_POINTER && r.b[1].rw == old); + bad[0] = m[3]; bad[0].nested_argument_buffer = 1; + assert(nvn_storage_access_apply(&r, 1, bad, 1, 1, len).status == NVN_STORAGE_NOT_POINTER && r.b[1].rw == old); + assert(nvn_storage_access_apply(&r, 1, m, n, 1, 0).status == NVN_STORAGE_BAD_METADATA && r.b[1].rw == old); + assert(nvn_storage_access_apply(&r, 1, m, n, 1, 63).status == NVN_STORAGE_BAD_METADATA && r.b[1].rw == old); + r.route_b = 0; + assert(nvn_storage_access_apply(&r, 1, m, n, 1, len).status == NVN_STORAGE_BAD_METADATA && r.b[1].rw == old); + r.route_b = 1; r.b[1].reg = 16; + assert(nvn_storage_access_apply(&r, 1, m, n, 1, len).status == NVN_STORAGE_BAD_METADATA && r.b[1].rw == old); + r.b[1].reg = 1; r.b[1].aid = NVN_REFL_MAXARG; + assert(nvn_storage_access_apply(&r, 1, m, n, 1, 4096).status == NVN_STORAGE_BAD_METADATA && r.b[1].rw == old); + r.b[1].aid = 7; r.b[4].aid = 7; + assert(nvn_storage_access_apply(&r, 1, m, n, 1, len).status == NVN_STORAGE_BAD_METADATA && r.b[1].rw == old); + r.b[4].aid = 5; r.b[4].n = 4; + assert(nvn_storage_access_apply(&r, 1, m, n, 1, len).status == NVN_STORAGE_BAD_METADATA && r.b[1].rw == old); + } + puts("storage-access policy: sparse IDs, writer promotion, preservation, ABI/kind/range/missing/duplicate rejection PASS"); + return 0; +} diff --git a/dm/gfx/tests/t_storage_access_adapter.m b/dm/gfx/tests/t_storage_access_adapter.m new file mode 100644 index 0000000000..ee89809a7e --- /dev/null +++ b/dm/gfx/tests/t_storage_access_adapter.m @@ -0,0 +1,96 @@ +// Exercise the real Objective-C adapter with asset-free reflection objects. +// No MTLDevice or GPU is created. These are plain test objects, not game inputs. +#import +#include +#include +#include +static unsigned g_logged; +static int g_seen_writer, g_seen_atomic; +static void storage_access_test_log(const char *fmt, ...) { + char line[512]; va_list args; va_start(args, fmt); vsnprintf(line, sizeof line, fmt, args); va_end(args); + g_logged++; + g_seen_writer |= strstr(line, "aid=7 ") && strstr(line, "access=1 rw=0->1 promoted=1"); + g_seen_atomic |= strstr(line, "aid=11 ") && strstr(line, "access=2 rw=0->1 promoted=1"); +} +#define NVN_STORAGE_ACCESS_LOG(...) storage_access_test_log(__VA_ARGS__) +#include "../nvn/nvn_storage_access_metal.h" +#undef NVN_STORAGE_ACCESS_LOG +#include + +@interface AccessPointer : NSObject +@property(nonatomic) NSUInteger access; +@property(nonatomic) BOOL elementIsArgumentBuffer; +@end +@implementation AccessPointer @end +@interface AccessMember : NSObject +@property(nonatomic) NSUInteger argumentIndex, offset, dataType; +@property(nonatomic, strong) AccessPointer *pointerType; +@end +@implementation AccessMember @end +@interface AccessStruct : NSObject +@property(nonatomic, strong) NSArray *members; +@end +@implementation AccessStruct @end +@interface AccessArgument : NSObject +@property(nonatomic) NSUInteger type, index, bufferDataType, access; +@property(nonatomic, strong) AccessStruct *bufferStructType; +@end +@implementation AccessArgument @end +@interface AccessFunction : NSObject +@property(nonatomic, strong) id encoder; +@property(nonatomic) NSUInteger fallbacks; +@end +@implementation AccessFunction +- (id)newArgumentEncoderWithBufferIndex:(NSUInteger)index reflection:(id __autoreleasing *)reflection { + (void)index; if (reflection) *reflection = self; return nil; +} +- (id)newArgumentEncoderWithBufferIndex:(NSUInteger)index { + (void)index; self.fallbacks++; return self.encoder; +} +@end + +static AccessMember *member(NSUInteger aid, NSUInteger access) { + AccessMember *m = [AccessMember new]; m.argumentIndex = aid; m.offset = aid * 8; m.dataType = MTLDataTypePointer; + m.pointerType = [AccessPointer new]; m.pointerType.access = access; return m; +} +int main(void) { @autoreleasepool { + setenv("DM_NVN_STORAGE_ACCESS_DIAG", "adapter", 1); + AccessMember *reader = member(2, 0), *writer = member(7, 1), *atomic = member(11, 2); + AccessArgument *a = [AccessArgument new]; a.type = 0; a.index = 0; a.bufferDataType = MTLDataTypeStruct; + a.access = 0; // outer constant AB is read-only even when its pointees write + a.bufferStructType = [AccessStruct new]; a.bufferStructType.members = @[atomic, reader, writer]; + nvn_refl rf = {.route_b = 1, .abi = 0, .nb = 5}; + rf.b[0] = (nvn_rbind){.kind = NVN_RB_SBUFP, .aid = 2, .reg = 0, .rw = 1}; + rf.b[1] = (nvn_rbind){.kind = NVN_RB_SBUFP, .aid = 7, .reg = 1}; + rf.b[2] = (nvn_rbind){.kind = NVN_RB_SBUFP, .aid = 11, .reg = 2}; + rf.b[3] = (nvn_rbind){.kind = NVN_RB_SBUFP, .aid = 15, .reg = 3}; + rf.b[4] = (nvn_rbind){.kind = NVN_RB_CBUF, .aid = 17}; + assert(nvn_storage_access_promote(&rf, (id)a, 18 * 8, "adapter", 5) == 2); + assert(rf.b[0].rw && rf.b[1].rw && rf.b[2].rw && !rf.b[3].rw && !rf.b[4].rw); + assert(g_seen_writer && g_seen_atomic); + assert(!nvn_storage_access_promote(&rf, (id)a, 18 * 8, "adapter", 5)); + // Root access is not a proxy for nested pointer access in either direction. + a.access = 1; reader.pointerType.access = 0; rf.b[0].rw = 0; + assert(!nvn_storage_access_promote(&rf, (id)a, 18 * 8, "adapter", 5) && !rf.b[0].rw); + writer.offset++; rf.b[1].rw = 0; + assert(!nvn_storage_access_promote(&rf, (id)a, 18 * 8, "adapter", 5) && !rf.b[1].rw); + writer.offset--; a.index = 1; + assert(!nvn_storage_access_promote(&rf, (id)a, 18 * 8, "adapter", 5) && !rf.b[1].rw); + a.index = 0; a.type = 2; + assert(!nvn_storage_access_promote(&rf, (id)a, 18 * 8, "adapter", 5) && !rf.b[1].rw); + a.type = 0; a.bufferDataType = MTLDataTypeFloat; + assert(!nvn_storage_access_promote(&rf, (id)a, 18 * 8, "adapter", 5) && !rf.b[1].rw); + a.bufferDataType = MTLDataTypeStruct; a.bufferStructType = nil; + assert(!nvn_storage_access_promote(&rf, (id)a, 18 * 8, "adapter", 5) && !rf.b[1].rw); + assert(!nvn_storage_access_promote(&rf, nil, 18 * 8, "adapter", 5) && rf.b[2].rw); + // The per-filter budget stays bounded despite repeated missing reflection. + for (int i = 0; i < 200; i++) nvn_storage_access_promote(&rf, nil, 18 * 8, "adapter", 5); + assert(atomic_load(&g_storage_access_diag_notes) == 128 && g_logged == 128); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + AccessFunction *fn = [AccessFunction new]; fn.encoder = [NSObject new]; MTLArgument *unusable = (id)a; + id result = nvn_storage_access_encoder((id)fn, 0, &unusable); + assert(result == fn.encoder && fn.fallbacks == 1 && !unusable); +#pragma clang diagnostic pop + puts("storage-access Objective-C adapter: outer-vs-pointee access, ID/offset, missing reflection and original true preservation PASS"); +} return 0; } diff --git a/dm/gfx/tests/t_storage_access_metal.m b/dm/gfx/tests/t_storage_access_metal.m new file mode 100644 index 0000000000..1ef628a419 --- /dev/null +++ b/dm/gfx/tests/t_storage_access_metal.m @@ -0,0 +1,225 @@ +// Asset-free Metal pointer-access reflection and sparse argument-ID fixture. +// clang -fobjc-arc -O1 -Wall -Wextra -Werror -framework Foundation \ +// -framework Metal dm/gfx/tests/t_storage_access_metal.m -o /tmp/t_storage_access_metal +// A missing local Metal device prints SKIP, never GPU PASS. +#import +#import +#include +#include +#include +#include "../nvn/nvn_refl.h" +#include "../nvn/nvn_storage_access_metal.h" +#include "../nvn/nvn_resource_usage.h" + +// These public APIs predate iOS 16. Current SDKs deprecate their old names in +// favor of newer binding reflection APIs that are unavailable on iOS 16. +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + +static const char k_storage_msl[] = + "#include \n" + "#include \n" + "using namespace metal;\n" + "struct StorageArgs {\n" + " const device uint *input [[id(2)]];\n" + " device uint *output [[id(7)]];\n" + " device atomic_uint *counter [[id(11)]];\n" + " device uint *unused [[id(15)]];\n" + " constant uint *cfg [[id(17)]];\n" + "};\n" + "kernel void storage_access_fixture(constant StorageArgs &a [[buffer(0)]],\n" + " uint gid [[thread_position_in_grid]]) {\n" + " if (gid >= a.cfg[0]) return;\n" + " a.output[gid] = a.input[gid] + a.cfg[1];\n" + " atomic_fetch_add_explicit(a.counter, 1u, memory_order_relaxed);\n" + "}\n" + "struct RouteBArgs {\n" + " const device uint *input [[id(0)]];\n" + " device uint *output [[id(1)]];\n" + " device atomic_uint *counter [[id(2)]];\n" + " device uint *unused [[id(3)]];\n" + " constant uint *cfg [[id(4)]];\n" + "};\n" + "kernel void storage_access_route_b(constant RouteBArgs &a [[buffer(0)]],\n" + " uint gid [[thread_position_in_grid]]) {\n" + " if (gid >= a.cfg[0]) return;\n" + " a.output[gid] = a.input[gid] + a.cfg[1];\n" + " atomic_fetch_add_explicit(a.counter, 1u, memory_order_relaxed);\n" + "}\n"; + +static const NSUInteger k_sparse_ids[] = {2, 7, 11, 15, 17}; +static const NSUInteger k_route_b_ids[] = {0, 1, 2, 3, 4}; + +static int g_checks, g_failures; +static int check(int ok, const char *what) { + g_checks++; + if (!ok) { g_failures++; fprintf(stderr, "FAIL: %s\n", what); } + return ok; +} +static int writable(MTLPointerType *p) { + return p && (p.access == MTLArgumentAccessWriteOnly || p.access == MTLArgumentAccessReadWrite); +} +static MTLStructMember *member_by_id(MTLStructType *type, NSUInteger aid) { + MTLStructMember *found = nil; + for (MTLStructMember *member in type.members) if (member.argumentIndex == aid) { + if (found) { check(0, "compiled argument IDs are unique"); return nil; } + found = member; + } + return found; +} +static int inspect_reflection(id encoder, MTLArgument *argument, + const NSUInteger ids[5], int sparse) { + if (!check(encoder != nil && argument != nil, "argument encoder returns real compiled reflection")) return 0; + if (!check(argument.type == MTLArgumentTypeBuffer && argument.index == 0, + "root reflection describes buffer(0)")) return 0; + MTLStructType *type = argument.bufferStructType; + if (!type && argument.bufferPointerType) type = argument.bufferPointerType.elementStructType; + if (!check(type != nil && type.members.count > 0, "argument-buffer struct members are available")) return 0; + printf("Compiled argument buffer: encodedLength=%lu members=%lu\n", + (unsigned long)encoder.encodedLength, (unsigned long)type.members.count); + for (MTLStructMember *member in type.members) { + MTLPointerType *pointer = member.dataType == MTLDataTypePointer ? member.pointerType : nil; + printf(" member=%s argumentIndex=%lu offset=%lu type=%lu pointerAccess=%ld\n", + member.name.UTF8String, (unsigned long)member.argumentIndex, (unsigned long)member.offset, + (unsigned long)member.dataType, pointer ? (long)pointer.access : -1l); + check(member.offset < encoder.encodedLength, "reflected member offset is inside encoded storage"); + } + // The sparse IDs deliberately differ from member enumeration ordinals. + // Use argumentIndex to identify a resource; do not infer it from offset. + MTLStructMember *input = member_by_id(type, ids[0]), *output = member_by_id(type, ids[1]); + MTLStructMember *counter = member_by_id(type, ids[2]), *cfg = member_by_id(type, ids[4]); + check(input && input.dataType == MTLDataTypePointer && input.pointerType, + "compiled input argument ID is a pointer"); + check(output && output.dataType == MTLDataTypePointer && writable(output.pointerType), + "compiled output argument ID permits writes"); + check(counter && counter.dataType == MTLDataTypePointer && writable(counter.pointerType), + "compiled atomic argument ID permits writes"); + check(cfg && cfg.dataType == MTLDataTypePointer && cfg.pointerType, + "used constant argument ID survives compilation"); + if (sparse) check(member_by_id(type, 0) == nil, "sparse ID 0 does not alias member ordinal zero"); + MTLStructMember *unused = member_by_id(type, ids[3]); + printf("Unused argument ID %lu: %s; absence is not evidence of read-only access\n", (unsigned long)ids[3], + unused ? "retained by this compiler" : "omitted by this compiler"); + // A retained unused member is allowed. Some compilers report declaration + // access conservatively rather than eliminating all unused pointer fields. + nvn_refl rf = {.nb = 5, .abi = 0, .route_b = 1}; + for (unsigned i = 0; i < 5; i++) rf.b[i] = (nvn_rbind){ + .kind = i == 4 ? NVN_RB_CBUF : NVN_RB_SBUFP, .aid = (uint16_t)ids[i], .reg = (uint16_t)i}; + nvn_storage_access_promote(&rf, argument, encoder.encodedLength, sparse ? "synthetic_sparse" : "synthetic_route_b", 5); + for (unsigned i = 0; i < 5; i++) { + MTLStructMember *m = member_by_id(type, ids[i]); + int raw_layout = m && m.offset == 8 * ids[i] && m.offset <= encoder.encodedLength && 8 <= encoder.encodedLength - m.offset; + if (!raw_layout || i == 4) check(!rf.b[i].rw, "helper rejects an incompatible raw slot or non-storage binding"); + } + if (!sparse) { + check(rf.b[1].rw, "helper promotes actual compiled route-B output pointer"); + check(rf.b[2].rw, "helper promotes actual compiled route-B atomic pointer"); + } + rf.b[0].rw = 1; + nvn_storage_access_promote(&rf, argument, encoder.encodedLength, "synthetic_preserve", 5); + check(rf.b[0].rw, "compiled reflection never clears an existing write flag"); + for (unsigned i = 0; i < 5; i++) rf.b[i].rw = 0; + nvn_storage_access_promote(&rf, argument, 1, "synthetic_short_block", 5); + for (unsigned i = 0; i < 5; i++) check(!rf.b[i].rw, "undersized raw argument storage never promotes a field"); + return g_failures == 0; +} + +static int declare_usage(id compute, nvn_resource_usage_set *set, + id resource, MTLResourceUsage requested, uint64_t *declared) { + int skip = -1; + if (!check(nvn_resource_usage_add(set, (uintptr_t)(__bridge void *)resource, + (uint64_t)requested, 0, declared, &skip), "resource-usage union insertion succeeds")) return 0; + check(!skip, "dedup-off still emits every Metal declaration"); + [compute useResource:resource usage:(MTLResourceUsage)*declared]; + return 1; +} + +static int execute_readback(id device, id function, + id encoder, const NSUInteger ids[5], int alias) { + enum { COUNT = 16 }; uint32_t values[COUNT], expected[COUNT], config[2] = {COUNT, 37}; + for (unsigned i = 0; i < COUNT; i++) { values[i] = i * 11u + 3u; expected[i] = values[i] + config[1]; } + id input = [device newBufferWithBytes:values length:sizeof values options:MTLResourceStorageModeShared]; + id output = alias ? input : [device newBufferWithLength:sizeof values options:MTLResourceStorageModeShared]; + id counter = [device newBufferWithLength:sizeof(uint32_t) options:MTLResourceStorageModeShared]; + id cfg = [device newBufferWithBytes:config length:sizeof config options:MTLResourceStorageModeShared]; + id arguments = [device newBufferWithLength:encoder.encodedLength options:MTLResourceStorageModeShared]; + if (!check(input && output && counter && cfg && arguments, "synthetic buffers allocate")) return 0; + memset(counter.contents, 0, counter.length); memset(arguments.contents, 0, arguments.length); + if (!alias) memset(output.contents, 0xa5, output.length); + [encoder setArgumentBuffer:arguments offset:0]; + [encoder setBuffer:input offset:0 atIndex:ids[0]]; + [encoder setBuffer:output offset:0 atIndex:ids[1]]; + [encoder setBuffer:counter offset:0 atIndex:ids[2]]; + [encoder setBuffer:cfg offset:0 atIndex:ids[4]]; + // Never encode the unused field: an optimizer may omit its argument ID. + // Public encoder setters handle physical offsets for sparse IDs; raw ABI + // slots are a separate contract checked by the production route-B loader. + NSError *error = nil; + id pipeline = [device newComputePipelineStateWithFunction:function error:&error]; + if (!check(pipeline != nil, "synthetic compute pipeline builds")) { + fprintf(stderr, "Pipeline error: %s\n", error.localizedDescription.UTF8String); return 0; + } + id queue = [device newCommandQueue]; + id command = [queue commandBuffer]; + id compute = [command computeCommandEncoder]; + if (!check(queue && command && compute, "synthetic command encoder allocates")) return 0; + [compute setComputePipelineState:pipeline]; [compute setBuffer:arguments offset:0 atIndex:0]; + nvn_resource_usage_set declarations = {0}; nvn_resource_usage_begin(&declarations); + uint64_t output_usage = 0, input_usage = 0, counter_usage = 0, cfg_usage = 0; + // Deliberately declare the writer before the reader. When both arguments + // alias one MTLBuffer, the later Read must emit Read|Write rather than + // downgrade the encoder's latest declaration for that resource. + int declarations_ok = declare_usage(compute, &declarations, output, MTLResourceUsageWrite, &output_usage) + && declare_usage(compute, &declarations, input, MTLResourceUsageRead, &input_usage) + && declare_usage(compute, &declarations, counter, MTLResourceUsageRead | MTLResourceUsageWrite, &counter_usage) + && declare_usage(compute, &declarations, cfg, MTLResourceUsageRead, &cfg_usage); + if (!declarations_ok) { + [compute endEncoding]; nvn_resource_usage_dispose(&declarations); return 0; + } + check(output_usage == MTLResourceUsageWrite, "first output declaration requests Write"); + check(input_usage == (alias ? MTLResourceUsageRead | MTLResourceUsageWrite : MTLResourceUsageRead), + "later input Read preserves Write exactly when input/output alias"); + check(counter_usage == (MTLResourceUsageRead | MTLResourceUsageWrite) && cfg_usage == MTLResourceUsageRead, + "counter and constant declarations retain their separate access contracts"); + [compute dispatchThreadgroups:MTLSizeMake(1, 1, 1) threadsPerThreadgroup:MTLSizeMake(COUNT, 1, 1)]; + [compute endEncoding]; [command commit]; [command waitUntilCompleted]; + nvn_resource_usage_dispose(&declarations); + if (!check(command.status == MTLCommandBufferStatusCompleted && !command.error, + "synthetic GPU command completed")) { + fprintf(stderr, "GPU command error: %s\n", command.error.localizedDescription.UTF8String); return 0; + } + check(!memcmp(output.contents, expected, sizeof expected), "GPU output matches synthetic expected values"); + check(*(const uint32_t *)counter.contents == COUNT, "GPU atomic counter matches dispatched work"); + if (!g_failures) printf("GPU readback (%s buffers): PASS\n", alias ? "aliased input/output" : "separate input/output"); + return g_failures == 0; +} + +int main(void) { @autoreleasepool { + id device = MTLCreateSystemDefaultDevice(); + if (!device) { + puts("Storage-access Metal fixture: SKIP (no local Metal device; reflection/GPU execution not validated)"); + return 0; + } + printf("Storage-access Metal fixture device: %s\n", device.name.UTF8String); + MTLCompileOptions *options = [MTLCompileOptions new]; options.languageVersion = MTLLanguageVersion3_0; + options.fastMathEnabled = NO; + NSError *error = nil; + id library = [device newLibraryWithSource:@(k_storage_msl) options:options error:&error]; + if (!check(library != nil, "synthetic sparse-ID MSL compiles")) { + fprintf(stderr, "MSL error: %s\n", error.localizedDescription.UTF8String); return 1; + } + for (int sparse = 1; sparse >= 0; sparse--) { + NSString *entry = sparse ? @"storage_access_fixture" : @"storage_access_route_b"; + const NSUInteger *ids = sparse ? k_sparse_ids : k_route_b_ids; + id function = [library newFunctionWithName:entry]; + if (!check(function != nil, "synthetic entry point exists")) return 1; + printf("Inspecting %s\n", entry.UTF8String); + MTLArgument *argument = nil; + id encoder = nvn_storage_access_encoder(function, 0, &argument); + if (!inspect_reflection(encoder, argument, ids, sparse)) return 1; + if (!execute_readback(device, function, encoder, ids, 0) || !execute_readback(device, function, encoder, ids, 1)) return 1; + } + printf("Storage-access Metal fixture: PASS (%d checks; real compiled reflection and GPU readback)\n", g_checks); + return g_failures != 0; +} } +#pragma clang diagnostic pop diff --git a/dm/gfx/tests/t_upload_budget.m b/dm/gfx/tests/t_upload_budget.m index 685fe17da0..0b449c5344 100644 --- a/dm/gfx/tests/t_upload_budget.m +++ b/dm/gfx/tests/t_upload_budget.m @@ -1,9 +1,12 @@ -// A streaming burst inside one still-open rendering submission must not retain all -// staging until its rendering fence. Exercise the real Metal upload/retirement path. +// A streaming burst inside one still-open rendering submission must not retain +// all staging until its rendering fence. Exercise the real Metal upload and +// completion-retirement path. +#define DM_NVN_LEGACY_RENDERER_TEST 1 #include "../nvn/nvn_mtl.m" uint64_t g_t0_ns; int dm_guest_as_contains(uintptr_t address) { (void)address; return 0; } void *front_metal_layer(void) { return NULL; } +int front_screen_max_fps(void) { return 60; } module_t *module_of(const void *p) { (void)p; return NULL; } module_t *g_mod; int g_nmod; void svc_thread_dump(void) {} @@ -21,12 +24,33 @@ static void upload_one(id dst, NSHashTable *buffers, uint8_t value) size_t off; id b = up_alloc(32u << 20, &off); @autoreleasepool { [buffers addObject:b]; } - [g_up_ce fillBuffer:b range:NSMakeRange(off, 256) value:value]; + // The legacy Metal path uses a compute encoder for deswizzle and a blit + // encoder for copies. Temporarily switch encoders so the regression can + // initialize this private staging buffer on the GPU. + if (g_up_ce) { m4_end(g_up_ce); g_up_ce = nil; } + id fill = [g_up_cb blitCommandEncoder]; + [fill fillBuffer:b range:NSMakeRange(off, 256) value:value]; + [fill endEncoding]; + g_up_ce = [g_up_cb computeCommandEncoder]; up_blit_add(b, off, 256, 256, MTLSizeMake(1, 1, 1), dst, 0, 0); [g_up_texs addObject:dst]; } int main(void) { @autoreleasepool { mtl_init(); + tex_t cube = {.magic=MAGIC('T','E','X',0), .serial=96999}; + cube.d = (nvn_texdesc){.target=NVN_TEXTURE_TARGET_CUBEMAP_ARRAY, + .format=NVN_FORMAT_RGBA8, .width=64, .height=64, + .depth=12, .levels=1, .samples=1}; + MTLTextureDescriptor *cube_desc = tex_desc(&cube, 0); + CHECK(cube_desc.textureType == MTLTextureType2DArray, + "NVN cube array uses a 2D-array Metal base"); + CHECK(cube_desc.arrayLength == 12, + "NVN cube-array depth remains its twelve layer-faces"); + id cube_base = [g_dev newTextureWithDescriptor:cube_desc]; + id cube_view = [cube_base newTextureViewWithPixelFormat:cube_base.pixelFormat + textureType:MTLTextureTypeCubeArray levels:NSMakeRange(0, 1) slices:NSMakeRange(0, 12)]; + CHECK(cube_view && cube_view.textureType == MTLTextureTypeCubeArray && cube_view.arrayLength == 2, + "twelve face layers form a two-cube Metal view"); NSHashTable *buffers = [NSHashTable weakObjectsHashTable]; NSMutableArray *textures = [NSMutableArray new]; MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm width:1 height:1 mipmapped:NO]; @@ -34,7 +58,70 @@ int main(void) { @autoreleasepool { d.usage = MTLTextureUsageRenderTarget; id target = [g_dev newTextureWithDescriptor:d]; res_add(target); mtl_sub_begin(); - MTL4RenderPassDescriptor *pass = [MTL4RenderPassDescriptor new]; + + // A temporary decompression view belongs to its upload command buffer, + // not to the terminal replay fence. This is the ownership pattern used + // by the large BC scene-load burst on tvOS. + up_begin(); + id temporary_view = [target newTextureViewWithPixelFormat:target.pixelFormat]; + __weak id temporary_view_weak = temporary_view; + [g_up_texs addObject:temporary_view]; + temporary_view = nil; + CHECK(temporary_view_weak != nil, "upload batch retains its temporary decompression view"); + NSUInteger deferred_before_view_flush = g_deferred.count; + up_flush(); up_wait_zero(&g_up_pending); + CHECK(temporary_view_weak == nil, "upload completion releases its temporary decompression view before the replay fence"); + CHECK(g_deferred.count == deferred_before_view_flush, + "temporary upload views never enter the whole-submission deferred queue"); + + // Decompressed-target accounting splits only when the next texture would + // exceed the budget. Equality stays in the current batch. + NSUInteger deferred_before_budget = g_deferred.count; + size_t quarter = (size_t)UP_BATCH_TARGET_BYTES / 4; + up_target_prepare(quarter * 2); up_target_charge(quarter * 2); + unsigned seq_at_half = g_up_seq; + up_target_prepare(quarter * 2); up_target_charge(quarter * 2); + CHECK(g_up_seq == seq_at_half && g_up_target_bytes == (size_t)UP_BATCH_TARGET_BYTES, + "target budget accepts an exact-boundary texture in the current batch"); + up_target_prepare(1); + CHECK(g_up_seq == seq_at_half + 1 && g_up_target_bytes == 0, + "the first byte beyond the target budget starts one new batch"); + up_target_charge(1); up_flush(); up_wait_zero(&g_up_pending); + CHECK(g_up_target_bytes == 0 && g_up_pending == 0, + "target accounting clears when its upload batch completes"); + + // The independent staging limit may flush after target preparation. A + // successful texture must be charged to the replacement batch, not the + // batch that up_alloc just committed. + up_target_prepare(quarter); size_t staging_off; + (void)up_alloc(96u << 20, &staging_off); up_target_charge(quarter); + unsigned seq_before_staging_split = g_up_seq; + up_target_prepare(quarter); + (void)up_alloc(64u << 20, &staging_off); + CHECK(g_up_seq == seq_before_staging_split + 1 && g_up_target_bytes == 0, + "staging-budget flush resets decompressed-target accounting"); + up_target_charge(quarter); + CHECK(g_up_target_bytes == quarter, + "post-encode charge belongs to the batch created by up_alloc"); + up_flush(); up_wait_zero(&g_up_pending); + + // An indivisible texture may exceed the limit alone. It must not loop + // while entering an empty batch, and the following texture must split. + up_target_prepare(1); up_target_charge(1); + unsigned seq_before_oversize = g_up_seq; + up_target_prepare((size_t)UP_BATCH_TARGET_BYTES + 1); + CHECK(g_up_seq == seq_before_oversize + 1 && g_up_target_bytes == 0, + "an oversized texture first retires the preceding non-empty batch"); + up_target_charge((size_t)UP_BATCH_TARGET_BYTES + 1); + unsigned seq_with_oversize = g_up_seq; + up_target_prepare(1); + CHECK(g_up_seq == seq_with_oversize + 1 && g_up_target_bytes == 0, + "the texture following an oversized batch starts a new batch"); + up_target_charge(1); up_flush(); up_wait_zero(&g_up_pending); + CHECK(g_deferred.count == deferred_before_budget, + "target-budget splitting creates no whole-submission retirements"); + + MTLRenderPassDescriptor *pass = [MTLRenderPassDescriptor renderPassDescriptor]; pass.colorAttachments[0].texture = target; pass.colorAttachments[0].loadAction = MTLLoadActionClear; pass.colorAttachments[0].storeAction = MTLStoreActionStore; @@ -47,7 +134,7 @@ int main(void) { @autoreleasepool { upload_one(t, buffers, (uint8_t)(i + 1)); peak = MAX(peak, live_bytes(buffers)); } - up_flush(); rec_publish(); up_wait_zero(&g_up_pending, WS_DIAG); + up_flush(); up_wait_zero(&g_up_pending); size_t idle = live_bytes(buffers); CHECK(peak <= (512u << 20), "streaming burst staging stays within 512 MiB"); CHECK(idle <= (96u << 20), "only three reusable staging blocks survive upload completion before the rendering fence"); diff --git a/dm/gfx/tests/t_upload_diag.c b/dm/gfx/tests/t_upload_diag.c new file mode 100644 index 0000000000..61914d168d --- /dev/null +++ b/dm/gfx/tests/t_upload_diag.c @@ -0,0 +1,52 @@ +// Exact byte comparison, race attribution and strict bounds of upload diagnostics. +#include +#include +#include +static char last_log[2048]; +static void test_log(const char *fmt, ...) { + va_list ap; va_start(ap, fmt); vsnprintf(last_log, sizeof last_log, fmt, ap); va_end(ap); +} +#define NVN_UPDIAG_LOG test_log +#include "../nvn/nvn_upload_diag.h" +static int writes_during_capture; +const void *nvn_dirty_span(const void *addr, size_t len) { (void)len; return addr; } +uint64_t nvn_dirty_now(void) { return 7; } +int nvn_dirty_since_span(const void *span, const void *addr, size_t len, uint64_t stamp) { + (void)span; (void)addr; (void)len; (void)stamp; return writes_during_capture; +} +static nvn_updiag_record capture(nvn_updiag_meta *m) { + nvn_updiag_record r; nvn_updiag_begin(&r, m); nvn_updiag_capture(&r); + return r; +} +int main(void) { + size_t pg = (size_t)getpagesize(); void *p = NULL; + assert(!posix_memalign(&p, pg, pg * 2)); memset(p, 0, pg * 2); + nvn_updiag_meta m = {.frame=1, .serial=1, .mem=p, .pool=p, .bytes=pg*2, .reupload=1}; + unsetenv("DM_NVN_UPLOAD_DIAG_FRAMES"); + nvn_updiag_record r = capture(&m); assert(!r.enabled && !g_updiag.bytes); + setenv("DM_NVN_UPLOAD_DIAG_FRAMES", "4:first", 1); m.frame++; + r=capture(&m); assert(r.enabled && r.stable && r.baseline && !r.uncaptured); + char digest[sizeof r.digest]; memcpy(digest, r.digest, sizeof digest); + r=capture(&m); assert(!r.baseline && r.equal && !r.different_pages && !strcmp(digest,r.digest)); + ((char *)p)[pg+5]=3; + r=capture(&m); assert(!r.equal && r.different_pages==1 && r.first_different_page==pg && strcmp(digest,r.digest)); + writes_during_capture=1; r=capture(&m); assert(!r.stable); + writes_during_capture=0; r=capture(&m); assert(r.stable && !r.equal && !r.different_pages); + nvn_updiag_end(&r,r.capture_ns+1000,1); assert(strstr(last_log,"exact=prior-racy")); + r=capture(&m); assert(r.equal); + m.serial=2; m.bytes=UPDIAG_MAX_TEXTURE+1; + r=capture(&m); assert(!strcmp(r.uncaptured,"texture-size-cap")); + // Oversized requests never read the undersized buffer used above. + m.bytes=pg*2; m.frame=100; + r=capture(&m); assert(!r.enabled && !g_updiag.bytes); + setenv("DM_NVN_UPLOAD_DIAG_FRAMES", "16:record-cap", 1); m.frame++; + for (unsigned i=0;i> i) & 1u)) w[p >> 5] |= 1u << (p & 31u); + } +} +BCA_FN u32 bca_getb(BCA_T const u32 *w, u32 p) { return (w[p >> 5] >> (p & 31u)) & 1u; } +// Weights are stored from bit 127 downwards: stream bit i of tmp goes to block bit 127 - i +BCA_FN void bca_rev(BCA_T u32 *out, BCA_T const u32 *tmp, u32 n) { + for (u32 i = 0; i < n; i++) if (bca_getb(tmp, i)) out[(127u - i) >> 5] |= 1u << ((127u - i) & 31u); +} +// Integer sequence encoding with trits: value v = trit * 2^nb + m. Writes the stream at bit pos, returns its length n*nb + ceil(8n/5). The last group is cut after the length. +BCA_FN u32 bca_ise_trit(BCA_T u32 *dst, u32 pos, BCA_T const u32 *vals, u32 n, u32 nb) { + u32 total = n * nb + (n * 8u + 4u) / 5u, lim = pos + total, p = pos, mask = (1u << nb) - 1u; + for (u32 g = 0; g < n; g += 5) { + u32 m[5], t[5]; + for (u32 k = 0; k < 5; k++) { u32 v = (g + k < n) ? vals[g + k] : 0u; m[k] = v & mask; t[k] = v >> nb; } + u32 T = bca_trit_enc[t[0] + 3u * t[1] + 9u * t[2] + 27u * t[3] + 81u * t[4]]; + bca_putl(dst, p, lim, nb, m[0]); p += nb; + bca_putl(dst, p, lim, 2, T & 3u); p += 2; + bca_putl(dst, p, lim, nb, m[1]); p += nb; + bca_putl(dst, p, lim, 2, (T >> 2) & 3u); p += 2; + bca_putl(dst, p, lim, nb, m[2]); p += nb; + bca_putl(dst, p, lim, 1, (T >> 4) & 1u); p += 1; + bca_putl(dst, p, lim, nb, m[3]); p += nb; + bca_putl(dst, p, lim, 2, (T >> 5) & 3u); p += 2; + bca_putl(dst, p, lim, nb, m[4]); p += nb; + bca_putl(dst, p, lim, 1, (T >> 7) & 1u); p += 1; + } + return total; +} + +// ---- BC decode (same arithmetic as dm/third_party/bcdec, which the tests compare against) ---- +BCA_FN void bca_col(u32 c0, u32 c1, u32 three, BCA_T s32 *ref) { // ref[4 colours][4 = r g b a]; three: BC1 three-colour mode (index 3 = transparent black) + s32 r0 = (s32)((c0 >> 11) & 31u), g0 = (s32)((c0 >> 5) & 63u), b0 = (s32)(c0 & 31u); + s32 r1 = (s32)((c1 >> 11) & 31u), g1 = (s32)((c1 >> 5) & 63u), b1 = (s32)(c1 & 31u); + ref[0] = (r0 * 527 + 23) >> 6; ref[1] = (g0 * 259 + 33) >> 6; ref[2] = (b0 * 527 + 23) >> 6; ref[3] = 255; + ref[4] = (r1 * 527 + 23) >> 6; ref[5] = (g1 * 259 + 33) >> 6; ref[6] = (b1 * 527 + 23) >> 6; ref[7] = 255; + if (!three) { + ref[8] = ((2 * r0 + r1) * 351 + 61) >> 7; ref[9] = ((2 * g0 + g1) * 2763 + 1039) >> 11; ref[10] = ((2 * b0 + b1) * 351 + 61) >> 7; ref[11] = 255; + ref[12] = ((r0 + r1 * 2) * 351 + 61) >> 7; ref[13] = ((g0 + g1 * 2) * 2763 + 1039) >> 11; ref[14] = ((b0 + b1 * 2) * 351 + 61) >> 7; ref[15] = 255; + } else { + ref[8] = ((r0 + r1) * 1053 + 125) >> 8; ref[9] = ((g0 + g1) * 4145 + 1019) >> 11; ref[10] = ((b0 + b1) * 1053 + 125) >> 8; ref[11] = 255; + ref[12] = 0; ref[13] = 0; ref[14] = 0; ref[15] = 0; + } +} + +// ---- endpoint fitting ---- +BCA_FN s32 bca_lerp(s32 e0, s32 e1, s32 w) { return ((((e0 * (64 - w) + e1 * w) * 257 + 32) >> 6) >> 8); } // the decoder's interpolation, reduced to 8 bits +BCA_FN s32 bca_near48(s32 x) { // index of the unquantized value nearest to x (the lower index wins a tie) + s32 best = 0, bd = 1 << 20; + for (s32 i = 0; i < 48; i++) { s32 d = (s32)bca_unq48[i] - x; d = d < 0 ? -d : d; if (d < bd) { bd = d; best = i; } } + return best; +} +// Least-squares endpoints for fixed weights wt (0..64), quantized to the 48-value range; the nearest index and its neighbours are tried and the smallest squared error wins +BCA_FN void bca_fit48(BCA_T const s32 *vals, BCA_T const s32 *wt, BCA_T s32 *q0, BCA_T s32 *q1) { + s64 saa = 0, sab = 0, sbb = 0, sac = 0, sbc = 0; s32 sum = 0; + for (u32 i = 0; i < 16; i++) { + s64 a = 64 - wt[i], b = wt[i], c = vals[i]; + saa += a * a; sab += a * b; sbb += b * b; sac += a * c; sbc += b * c; sum += vals[i]; + } + s64 det = saa * sbb - sab * sab; + s32 e0, e1; + if (det <= 0) { e0 = (sum + 8) >> 4; e1 = e0; } + else { + s64 n0 = sac * sbb - sbc * sab, n1 = saa * sbc - sab * sac; + s64 x0 = (n0 * 64 + det / 2) / det, x1 = (n1 * 64 + det / 2) / det; + e0 = (s32)(x0 < 0 ? 0 : (x0 > 255 ? 255 : x0)); e1 = (s32)(x1 < 0 ? 0 : (x1 > 255 ? 255 : x1)); + } + s32 c0 = bca_near48(e0), c1 = bca_near48(e1), best = 1 << 30, b0 = c0, b1 = c1; + for (s32 d0 = -1; d0 <= 1; d0++) for (s32 d1 = -1; d1 <= 1; d1++) { + s32 i0 = c0 + d0, i1 = c1 + d1; + if (i0 < 0 || i0 > 47 || i1 < 0 || i1 > 47) continue; + s32 err = 0; + for (u32 i = 0; i < 16; i++) { s32 d = bca_lerp((s32)bca_unq48[i0], (s32)bca_unq48[i1], wt[i]) - vals[i]; err += d * d; } + if (err < best) { best = err; b0 = i0; b1 = i1; } + } + *q0 = b0; *q1 = b1; +} +BCA_FN s32 bca_near7(s32 x) { // 7-bit endpoint index whose bit-replicated 8-bit value is nearest to x + s32 q = x >> 1, best = q, bd = 1 << 20; + for (s32 c = q; c <= q + 1 && c < 128; c++) { + s32 u = (c << 1) | (c >> 6), d = u - x; d = d < 0 ? -d : d; + if (d < bd) { bd = d; best = c; } + } + return best; +} + +// bc: the BC block as four little-endian words (BC1 uses words 0..1). kind 1 = BC1, 2 = BC2, 3 = BC3. out: the ASTC block as four words (zeroed here). Returns the layout id (| BCA_SWAPPED). +BCA_FN u32 bca_transcode(BCA_T const u32 *bc, u32 kind, BCA_T u32 *out) { + out[0] = 0; out[1] = 0; out[2] = 0; out[3] = 0; + u32 co = kind == 1 ? 0u : 2u, c0 = bc[co] & 0xffffu, c1 = bc[co] >> 16, ix = bc[co + 1]; + u32 three = (kind == 1 && c0 <= c1) ? 1u : 0u; + s32 ref[16]; bca_col(c0, c1, three, ref); + s32 ci[16], px[16][4], al[16]; + for (u32 i = 0; i < 16; i++) { + ci[i] = (s32)((ix >> (2 * i)) & 3u); + for (u32 c = 0; c < 4; c++) px[i][c] = ref[ci[i] * 4 + (s32)c]; + } + if (kind == 2) { for (u32 i = 0; i < 16; i++) al[i] = (s32)((bc[i >> 3] >> ((i & 7u) * 4u)) & 15u) * 17; } + else if (kind == 3) { + s32 a0 = (s32)(bc[0] & 255u), a1 = (s32)((bc[0] >> 8) & 255u), at[8]; + at[0] = a0; at[1] = a1; + if (a0 > a1) { for (s32 k = 1; k < 7; k++) at[k + 1] = ((7 - k) * a0 + k * a1) / 7; } + else { for (s32 k = 1; k < 5; k++) at[k + 1] = ((5 - k) * a0 + k * a1) / 5; at[6] = 0; at[7] = 255; } + u64 ai = ((u64)bc[0] | ((u64)bc[1] << 32)) >> 16; + for (u32 i = 0; i < 16; i++) al[i] = at[(ai >> (3u * i)) & 7u]; + } else { for (u32 i = 0; i < 16; i++) al[i] = px[i][3]; } + if (kind != 1) for (u32 i = 0; i < 16; i++) px[i][3] = al[i]; + + u32 same = 1; + for (u32 i = 1; i < 16; i++) for (u32 c = 0; c < 4; c++) if (px[i][c] != px[0][c]) same = 0; + if (same) { // void-extent block: one constant colour (exact for 8-bit values: v * 257 is the 16-bit value) + out[0] = 0xFFFFFDFCu; out[1] = 0xFFFFFFFFu; + out[2] = (u32)(px[0][0] * 257) | ((u32)(px[0][1] * 257) << 16); + out[3] = (u32)(px[0][2] * 257) | ((u32)(px[0][3] * 257) << 16); + return BCA_L0_SOLID; + } + + u32 auni = 1; + for (u32 i = 1; i < 16; i++) if (al[i] != al[0]) auni = 0; + // colour weight index (0..3 of bca_w4) of each BC colour index: 0 → w0, 1 → w3, 2 → w1, 3 → w2 + u32 wmap[4]; wmap[0] = 0; wmap[1] = 3; wmap[2] = 1; wmap[3] = 2; + + if (!three && (kind == 1 || auni)) { // L1a / L1b: one plane, 2-bit weights, 8-bit endpoints + s32 e0[4], e1[4]; u32 w[16]; + for (u32 c = 0; c < 3; c++) { e0[c] = ref[c]; e1[c] = ref[4 + c]; } + e0[3] = al[0]; e1[3] = al[0]; + for (u32 i = 0; i < 16; i++) w[i] = wmap[ci[i]]; + u32 sw = 0; + if (e1[0] + e1[1] + e1[2] < e0[0] + e0[1] + e0[2]) { // the decoder would blue-contract: swap the endpoints (all four channels) and invert the weights + for (u32 c = 0; c < 4; c++) { s32 t = e0[c]; e0[c] = e1[c]; e1[c] = t; } + for (u32 i = 0; i < 16; i++) w[i] = 3u - w[i]; + sw = BCA_SWAPPED; + } + u32 rgba = (kind != 1 && al[0] != 255) ? 1u : 0u, tmp[4] = { 0, 0, 0, 0 }; + bca_putl(out, 0, 128, 11, 0x42u); bca_putl(out, 13, 128, 4, rgba ? 12u : 8u); + u32 pos = 17; + for (u32 c = 0; c < (rgba ? 4u : 3u); c++) { bca_putl(out, pos, 128, 8, (u32)e0[c]); pos += 8; bca_putl(out, pos, 128, 8, (u32)e1[c]); pos += 8; } + for (u32 i = 0; i < 16; i++) bca_putl(tmp, 2 * i, 128, 2, w[i]); + bca_rev(out, tmp, 32); + return (rgba ? BCA_L1B_RGBA_UNIFORM : BCA_L1A_RGB) | sw; + } + + if (three) { // L2: BC1 three-colour mode. Plane 0: colour, weights 0 / 32 / 64 (trit); plane 1: alpha 0 / 255 + s32 q0[3], q1[3], u0[3], u1[3]; + for (u32 c = 0; c < 3; c++) { q0[c] = bca_near7(ref[c]); q1[c] = bca_near7(ref[4 + c]); u0[c] = (q0[c] << 1) | (q0[c] >> 6); u1[c] = (q1[c] << 1) | (q1[c] >> 6); } + u32 dark = (u0[0] + u0[1] + u0[2] <= u1[0] + u1[1] + u1[2]) ? 0u : 2u; // transparent texels take the colour weight that lands on the endpoint closest to black + u32 wc[16], wa[16]; + for (u32 i = 0; i < 16; i++) { + u32 k = (u32)ci[i]; + wc[i] = k == 0 ? 0u : (k == 1 ? 2u : (k == 2 ? 1u : dark)); + wa[i] = k == 3 ? 0u : 2u; + } + s32 a0 = 0, a1 = 127; // alpha endpoint indices: endpoint 0 holds alpha 0, endpoint 1 alpha 255 (index 127 unquantizes to 255) + u32 sw = 0; + if (u1[0] + u1[1] + u1[2] < u0[0] + u0[1] + u0[2]) { // blue contraction would apply: swap all endpoints, invert both planes + sw = BCA_SWAPPED; + for (u32 c = 0; c < 3; c++) { s32 t = q0[c]; q0[c] = q1[c]; q1[c] = t; } + s32 t = a0; a0 = a1; a1 = t; + for (u32 i = 0; i < 16; i++) { wc[i] = 2u - wc[i]; wa[i] = 2u - wa[i]; } + } + u32 ev[8], wv[32], tmp[4] = { 0, 0, 0, 0 }; + for (u32 c = 0; c < 3; c++) { ev[2 * c] = (u32)q0[c]; ev[2 * c + 1] = (u32)q1[c]; } + ev[6] = (u32)a0; ev[7] = (u32)a1; + bca_putl(out, 0, 128, 11, 0x451u); bca_putl(out, 13, 128, 4, 12u); + for (u32 i = 0; i < 8; i++) bca_putl(out, 17 + 7 * i, 128, 7, ev[i]); + for (u32 i = 0; i < 16; i++) { wv[2 * i] = wc[i]; wv[2 * i + 1] = wa[i]; } + u32 nbits = bca_ise_trit(tmp, 0, wv, 32, 0); + bca_rev(out, tmp, nbits); + bca_putl(out, 128u - nbits - 2u, 128, 2, 3u); + return BCA_L2_BC1_PUNCH | sw; + } + + // L3: BC2 / BC3 with varying alpha. Plane 0 colour, plane 1 alpha, 2-bit weights, endpoints in 48 levels + s32 cw[16], vals[16], q0[4], q1[4]; + u32 wc[16], wa[16], sw = 0; + for (u32 i = 0; i < 16; i++) { wc[i] = wmap[ci[i]]; cw[i] = (s32)bca_w4[wc[i]]; } + for (u32 c = 0; c < 3; c++) { + for (u32 i = 0; i < 16; i++) vals[i] = px[i][c]; + bca_fit48(vals, cw, &q0[c], &q1[c]); + } + { // alpha: start from min / max, then reassign the four levels and refit twice + s32 lo = 255, hi = 0, aw[16]; + for (u32 i = 0; i < 16; i++) { lo = al[i] < lo ? al[i] : lo; hi = al[i] > hi ? al[i] : hi; } + s32 a0 = bca_near48(lo), a1 = bca_near48(hi); + for (u32 it = 0; it < 3; it++) { + for (u32 i = 0; i < 16; i++) { + s32 bk = 0, bd = 1 << 20; + for (s32 k = 0; k < 4; k++) { s32 d = bca_lerp((s32)bca_unq48[a0], (s32)bca_unq48[a1], (s32)bca_w4[k]) - al[i]; d = d < 0 ? -d : d; if (d < bd) { bd = d; bk = k; } } + wa[i] = (u32)bk; aw[i] = (s32)bca_w4[bk]; + } + if (it < 2) bca_fit48(al, aw, &a0, &a1); + } + q0[3] = a0; q1[3] = a1; + } + { + s32 s0 = (s32)bca_unq48[q0[0]] + (s32)bca_unq48[q0[1]] + (s32)bca_unq48[q0[2]]; + s32 s1 = (s32)bca_unq48[q1[0]] + (s32)bca_unq48[q1[1]] + (s32)bca_unq48[q1[2]]; + if (s1 < s0) { + sw = BCA_SWAPPED; // swap all endpoints (alpha too) and invert both planes + for (u32 c = 0; c < 4; c++) { s32 t = q0[c]; q0[c] = q1[c]; q1[c] = t; } + for (u32 i = 0; i < 16; i++) { wc[i] = 3u - wc[i]; wa[i] = 3u - wa[i]; } + } + } + { + u32 ev[8], tmp[4] = { 0, 0, 0, 0 }; + for (u32 c = 0; c < 4; c++) { ev[2 * c] = bca_raw48[q0[c]]; ev[2 * c + 1] = bca_raw48[q1[c]]; } + bca_putl(out, 0, 128, 11, 0x442u); bca_putl(out, 13, 128, 4, 12u); + u32 n = bca_ise_trit(out, 17, ev, 8, 4); + for (u32 i = 0; i < 16; i++) { bca_putl(tmp, 4 * i, 128, 2, wc[i]); bca_putl(tmp, 4 * i + 2, 128, 2, wa[i]); } + bca_rev(out, tmp, 64); + bca_putl(out, 128u - 64u - 2u, 128, 2, 3u); + (void)n; + return BCA_L3_ALPHA | sw; + } +} +) diff --git a/dm/gfx/tex/bc2astc_core.h b/dm/gfx/tex/bc2astc_core.h new file mode 100644 index 0000000000..6b2d0b6a55 --- /dev/null +++ b/dm/gfx/tex/bc2astc_core.h @@ -0,0 +1,14 @@ +// BC1 / BC2 / BC3 block → ASTC 4x4 LDR block (16 bytes), integer only. Public C interface: nvn_bc2astc.h. See dm/gfx/tex/README.md ("Devices without BC support") for the layouts and bit budgets. +// The code is in bc2astc_body.inc, which is written in the subset of C that Metal Shading Language also accepts; bc2astc_msl.h turns the same text into the GPU kernel (nvn_mtl.m, tests). +// The C build is the reference and the GPU output must match it bit for bit (dm/gfx/tests/t_bc2astc_gpu.m). +// Rules for editing the body: no floats, no static state, no pointers except the BCA_T arrays, fixed loop counts, 64-bit integers only where they already are, no preprocessor lines or string literals. +#pragma once +#include +typedef uint32_t u32; typedef int32_t s32; typedef int64_t s64; typedef uint64_t u64; typedef uint8_t u8; +#define BCA_FN static inline // MSL: inline +#define BCA_T // MSL: thread +#define BCA_K static const // MSL: constant + +#define BCA_BODY(...) __VA_ARGS__ +#include "bc2astc_body.inc" +#undef BCA_BODY diff --git a/dm/gfx/tex/bc2astc_msl.h b/dm/gfx/tex/bc2astc_msl.h new file mode 100644 index 0000000000..2e3d802de2 --- /dev/null +++ b/dm/gfx/tex/bc2astc_msl.h @@ -0,0 +1,19 @@ +// The Metal source of the BC1/2/3 → ASTC 4x4 transcoder: the text of bc2astc_body.inc (stringified) plus the kernel `bca`. Include in one place per program; the string is nvn_bca_msl. +// Kernel: one thread per BC block. buffer(0) the BC blocks (bytes), buffer(1) BcaP, buffer(2) the ASTC blocks (written as four words per block, so off_out and pitch_out are multiples of 16). +#pragma once +#define BCA_BODY(...) #__VA_ARGS__ +static const char nvn_bca_msl[] = +"#include \nusing namespace metal;\n" +"typedef uint u32; typedef int s32; typedef long s64; typedef ulong u64; typedef uchar u8;\n" +"#define BCA_FN inline\n#define BCA_T thread\n#define BCA_K constant\n" +#include "bc2astc_body.inc" +"\nstruct BcaP { uint off_in, pitch_in, off_out, pitch_out, nbx, nby, kind; };\n" // pitch_in: bytes per BC block row; kind: 1 BC1, 2 BC2, 3 BC3 +"kernel void bca(device const uchar *src [[buffer(0)]], device const BcaP &p [[buffer(1)]], device uint *dst [[buffer(2)]], uint2 g [[thread_position_in_grid]]) {\n" +" if (g.x >= p.nbx || g.y >= p.nby) return;\n" +" uint bb = p.kind == 1 ? 8 : 16; device const uchar *b = src + p.off_in + g.y * p.pitch_in + g.x * bb;\n" +" u32 w[4], o[4];\n" +" for (uint i = 0; i < 4; i++) w[i] = i * 4 < bb ? (u32(b[i * 4]) | (u32(b[i * 4 + 1]) << 8) | (u32(b[i * 4 + 2]) << 16) | (u32(b[i * 4 + 3]) << 24)) : 0;\n" +" bca_transcode(w, p.kind, o);\n" +" device uint *d = dst + (p.off_out + g.y * p.pitch_out + g.x * 16) / 4;\n" +" for (uint i = 0; i < 4; i++) d[i] = o[i]; }\n"; +#undef BCA_BODY diff --git a/dm/gfx/tex/nvn_bc2astc.c b/dm/gfx/tex/nvn_bc2astc.c new file mode 100644 index 0000000000..b66809bc06 --- /dev/null +++ b/dm/gfx/tex/nvn_bc2astc.c @@ -0,0 +1,33 @@ +// See nvn_bc2astc.h. The transcoder itself is bc2astc_core.h. +#include "nvn_bc2astc.h" +#include "bc2astc_core.h" +#include + +int nvn_bc_astc_fmt(uint32_t f, uint32_t *pf, int *kind) { + // NVN: 0x41 RGB_DXT1, 0x42 RGBA_DXT1, 0x43 DXT3, 0x44 DXT5, 0x45–0x48 the same in sRGB + if (f < 0x41 || f > 0x48) return 0; + *pf = f >= 0x45 ? 186 : 204; + *kind = (f & 3) == 1 || (f & 3) == 2 ? 1 : ((f & 3) == 3 ? 2 : 3); + return 1; +} + +uint32_t nvn_bc2astc_block(int kind, const uint8_t *bc, uint8_t *astc) { + uint32_t w[4] = {0, 0, 0, 0}, o[4]; + memcpy(w, bc, kind == 1 ? 8 : 16); + uint32_t r = bca_transcode(w, (u32)kind, o); + memcpy(astc, o, 16); + return r; +} + +int nvn_bc2astc(uint32_t f, const uint8_t *src, size_t pitch, uint32_t w, uint32_t h, uint8_t *dst, size_t dpitch) { + uint32_t pf; int kind; if (!nvn_bc_astc_fmt(f, &pf, &kind)) return -1; + size_t bb = kind == 1 ? 8 : 16; + for (uint32_t by = 0; by * 4 < h; by++) for (uint32_t bx = 0; bx * 4 < w; bx++) + nvn_bc2astc_block(kind, src + by * pitch + bx * bb, dst + by * dpitch + (size_t)bx * 16); + return 0; +} + +void nvn_astc_zero_block(uint8_t out[16]) { + static const uint8_t z[16] = {0xFC, 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; // void-extent block, colour 0 + memcpy(out, z, 16); +} diff --git a/dm/gfx/tex/nvn_bc2astc.h b/dm/gfx/tex/nvn_bc2astc.h new file mode 100644 index 0000000000..38215241b9 --- /dev/null +++ b/dm/gfx/tex/nvn_bc2astc.h @@ -0,0 +1,14 @@ +// GPUs without BC support: BC1 / BC2 / BC3 (NVN 0x41–0x48) are stored as ASTC 4x4 (8 bits per texel) by transcoding every BC block into one ASTC block, which is 4× smaller than RGBA8. +// The algorithm is bc2astc_core.h, which the GPU kernel (nvn_mtl.m) compiles from the same text; this file is the CPU side (sparse pools, copy_b2t, tests) and the reference for the kernel. +#pragma once +#include +#include + +// NVN format is BC1/2/3 → returns 1, *mtl_pf = MTLPixelFormatASTC_4x4_LDR (204) or ASTC_4x4_sRGB (186), *kind = 1/2/3 (BC1/BC2/BC3); returns 0 for every other format +int nvn_bc_astc_fmt(uint32_t nvn_fmt, uint32_t *mtl_pf, int *kind); +// One block: bc has 8 bytes (kind 1) or 16 bytes (kind 2, 3), astc receives 16 bytes. Returns the layout id of bc2astc_core.h (| BCA_SWAPPED) +uint32_t nvn_bc2astc_block(int kind, const uint8_t *bc, uint8_t *astc); +// A w×h texel region: src has pitch bytes per block row, dst dpitch bytes per block row (16 per ASTC block). Returns 0 on success, -1 if the format is not BC1/2/3 +int nvn_bc2astc(uint32_t nvn_fmt, const uint8_t *src, size_t pitch, uint32_t w, uint32_t h, uint8_t *dst, size_t dpitch); +// The block that ASTC textures are filled with where BC textures are zero-filled: a constant (0,0,0,0) block (an all-zero ASTC block is invalid) +void nvn_astc_zero_block(uint8_t out[16]); diff --git a/dm/gfx/tex/nvn_bcdec.c b/dm/gfx/tex/nvn_bcdec.c new file mode 100644 index 0000000000..6f1cb09193 --- /dev/null +++ b/dm/gfx/tex/nvn_bcdec.c @@ -0,0 +1,56 @@ +// See nvn_bcdec.h. Decoding uses bcdec (dm/third_party/bcdec, single header, MIT / public domain). +#define BCDEC_IMPLEMENTATION +#define BCDEC_BC4BC5_PRECISE // interface with signed variants of BC4 / BC5 +#include "../../third_party/bcdec/bcdec.h" +#include "nvn_bcdec.h" +#include + +enum { R8Unorm = 10, R8Snorm = 12, RG8Unorm = 30, RG8Snorm = 32, RGBA8Unorm = 70, RGBA8Unorm_sRGB = 71, RGBA16Float = 115 }; +// NVN: 0x41 RGB_DXT1, 0x42 RGBA_DXT1, 0x43 DXT3, 0x44 DXT5, 0x45–0x48 the same in sRGB, 0x49 / 0x4a BC4 unsigned / signed, 0x4b / 0x4c BC5, 0x4d / 0x4e BC7 (sRGB), 0x4f / 0x50 BC6H signed / unsigned +int nvn_bc_unpacked(uint32_t f, uint32_t *pf, int *bpp) { + switch (f) { + case 0x41: case 0x42: case 0x43: case 0x44: case 0x4d: *pf = RGBA8Unorm; *bpp = 4; return 1; + case 0x45: case 0x46: case 0x47: case 0x48: case 0x4e: *pf = RGBA8Unorm_sRGB; *bpp = 4; return 1; + case 0x49: *pf = R8Unorm; *bpp = 1; return 1; + case 0x4a: *pf = R8Snorm; *bpp = 1; return 1; + case 0x4b: *pf = RG8Unorm; *bpp = 2; return 1; + case 0x4c: *pf = RG8Snorm; *bpp = 2; return 1; + case 0x4f: case 0x50: *pf = RGBA16Float; *bpp = 8; return 1; + default: return 0; + } +} + +static void block(uint32_t f, const uint8_t *b, uint8_t *o, int op) { // one block → 4×4 pixels (o has op bytes per row) + switch (f) { + case 0x41: case 0x42: case 0x45: case 0x46: bcdec_bc1(b, o, op); break; + case 0x43: case 0x47: bcdec_bc2(b, o, op); break; + case 0x44: case 0x48: bcdec_bc3(b, o, op); break; + case 0x49: bcdec_bc4(b, o, op, 0); break; + case 0x4a: bcdec_bc4(b, o, op, 1); break; + case 0x4b: bcdec_bc5(b, o, op, 0); break; + case 0x4c: bcdec_bc5(b, o, op, 1); break; + case 0x4d: case 0x4e: bcdec_bc7(b, o, op); break; + case 0x4f: case 0x50: { // bcdec outputs RGB half-float; add alpha = 1.0 + uint16_t rgb[16 * 3]; bcdec_bc6h_half(b, rgb, 4 * 3, f == 0x4f); + for (int y = 0; y < 4; y++) for (int x = 0; x < 4; x++) { + uint16_t *d = (uint16_t *)(o + y * op) + x * 4; const uint16_t *s = rgb + (y * 4 + x) * 3; + d[0] = s[0]; d[1] = s[1]; d[2] = s[2]; d[3] = 0x3c00; + } + break; } + } +} + +int nvn_bc_decode(uint32_t f, const uint8_t *src, size_t pitch, uint32_t w, uint32_t h, uint8_t *dst, size_t dpitch) { + uint32_t pf; int bpp; if (!nvn_bc_unpacked(f, &pf, &bpp)) return -1; + int bb = (f == 0x41 || f == 0x42 || f == 0x45 || f == 0x46 || f == 0x49 || f == 0x4a) ? 8 : 16; // bytes per block + uint8_t tmp[4 * 4 * 8]; + for (uint32_t by = 0; by * 4 < h; by++) for (uint32_t bx = 0; bx * 4 < w; bx++) { + const uint8_t *b = src + by * pitch + bx * bb; + uint32_t cw = w - bx * 4 < 4 ? w - bx * 4 : 4, ch = h - by * 4 < 4 ? h - by * 4 : 4; + uint8_t *o = dst + (size_t)by * 4 * dpitch + (size_t)bx * 4 * bpp; + if (cw == 4 && ch == 4) { block(f, b, o, (int)dpitch); continue; } + block(f, b, tmp, 4 * bpp); // blocks at the edge that are not a full 4×4: decode into a temporary block first, then copy the needed part + for (uint32_t y = 0; y < ch; y++) memcpy(o + y * dpitch, tmp + y * 4 * bpp, cw * bpp); + } + return 0; +} diff --git a/dm/gfx/tex/nvn_bcdec.h b/dm/gfx/tex/nvn_bcdec.h new file mode 100644 index 0000000000..bcc9676014 --- /dev/null +++ b/dm/gfx/tex/nvn_bcdec.h @@ -0,0 +1,10 @@ +// GPUs without BC compressed format support (A15 / A16 etc., Apple GPU family Apple8 and below): BC textures are decompressed on the CPU into uncompressed formats. +// BC3 in game textures is converted offline to ASTC by the Legacy Device Pack (dm/tools/astc_pack.py); the remaining BC (BC3 in sprites, a few BC1 / BC4 / BC5 / BC6H) goes through here. +#pragma once +#include +#include + +// NVN format is BC → returns 1, *mtl_pf = Metal pixel format after decompression, *bpp = bytes per pixel after decompression; returns 0 if not BC +int nvn_bc_unpacked(uint32_t nvn_fmt, uint32_t *mtl_pf, int *bpp); +// Decode a w×h pixel region: src is laid out by block rows (pitch bytes per block row), dst has dpitch bytes per pixel row. Returns 0 on success +int nvn_bc_decode(uint32_t nvn_fmt, const uint8_t *src, size_t pitch, uint32_t w, uint32_t h, uint8_t *dst, size_t dpitch); diff --git a/dm/gfx/tex/nvn_deswizzle_gpu.h b/dm/gfx/tex/nvn_deswizzle_gpu.h index e312bb969d..43ee5f3822 100644 --- a/dm/gfx/tex/nvn_deswizzle_gpu.h +++ b/dm/gfx/tex/nvn_deswizzle_gpu.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include #import #include "nvn_deswizzle.h" @@ -8,7 +9,9 @@ int nvn_deswz_gpu_init(id dev); int nvn_deswz_gpu_encode(id enc, id src, uint64_t src_off, const nvn_texdesc *d, unsigned level, unsigned layer, id dst, uint64_t dst_off, size_t dst_pitch); +#if TARGET_OS_OSX int nvn_deswz_gpu_encode_m4(id enc, id table, id params, size_t params_off, id src, uint64_t src_off, const nvn_texdesc *d, unsigned level, unsigned layer, id dst, uint64_t dst_off, size_t dst_pitch); +#endif diff --git a/dm/gfx/tex/nvn_deswizzle_gpu.m b/dm/gfx/tex/nvn_deswizzle_gpu.m index 54fe5295d8..12627a8957 100644 --- a/dm/gfx/tex/nvn_deswizzle_gpu.m +++ b/dm/gfx/tex/nvn_deswizzle_gpu.m @@ -48,6 +48,7 @@ int nvn_deswz_gpu_encode(id enc, id src, ui return 0; } +#if TARGET_OS_OSX int nvn_deswz_gpu_encode_m4(id enc, id table, id params, size_t params_off, id src, uint64_t src_off, const nvn_texdesc *d, unsigned level, unsigned layer, @@ -71,3 +72,4 @@ int nvn_deswz_gpu_encode_m4(id enc, id= MAX_FAULT_HOOK) die("too many page fault hooks"); g_fault_hook[g_nfault_hook++] = fn; @@ -153,6 +155,21 @@ static void on_crash(int sig, siginfo_t *si, void *uc_) { fprintf(stderr, "[dm] CRASH sig=%d", sig); off("addr", (uint64_t)si->si_addr); off("pc", uc->uc_mcontext->__ss.__pc); off("lr", uc->uc_mcontext->__ss.__lr); fprintf(stderr, " sp=%#llx\n", uc->uc_mcontext->__ss.__sp); + if (sig == SIGSEGV || sig == SIGBUS) { + fprintf(stderr, "[dm] fault si_code=%d esr=%#x write=%u\n", si->si_code, + uc->uc_mcontext->__es.__esr, (uc->uc_mcontext->__es.__esr >> 6) & 1); + vm_address_t a = (vm_address_t)si->si_addr; + vm_size_t size = 0; natural_t depth = 0; + vm_region_submap_info_data_64_t info; + mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64; + kern_return_t kr = vm_region_recurse_64(mach_task_self(), &a, &size, &depth, + (vm_region_recurse_info_t)&info, &count); + if (kr == KERN_SUCCESS) + fprintf(stderr, "[dm] fault VM region=%#llx..%#llx prot=%#x max=%#x submap=%d\n", + (unsigned long long)a, (unsigned long long)(a + size), info.protection, info.max_protection, info.is_submap); + else fprintf(stderr, "[dm] fault VM query failed=%d\n", kr); + if (g_fault_diagnostic) g_fault_diagnostic(si->si_addr); + } for (int i = 0; i < 29; i++) fprintf(stderr, "x%d=%#llx%s", i, uc->uc_mcontext->__ss.__x[i], i % 6 == 5 ? "\n" : " "); fprintf(stderr, "\n"); // What the registers point at: the first six words of each readable target (read with vm_read_overwrite, which fails instead of faulting); a word inside a module prints as a module offset, so an object's vtable names its class @@ -184,6 +201,11 @@ static void on_crash(int sig, siginfo_t *si, void *uc_) { } // The signal alternate stack is per thread: every guest thread must call this when it starts running (only then can the crash state be printed on the alternate stack on a stack overflow) // The alternate stack is released when the thread exits: the pthread key destructor is called at thread exit (disable the alternate stack first, then free). The game frequently starts short-lived threads, and without releasing each one leaks 64 KiB. +#if TARGET_OS_TV +// tvOS does not expose sigaltstack. Keep ordinary signal diagnostics without +// allocating an alternate stack or calling an unavailable platform API. +void crash_thread_init(void) {} +#else static pthread_key_t g_altstack_key; static pthread_once_t g_altstack_once = PTHREAD_ONCE_INIT; static void altstack_free(void *p) { stack_t off = {.ss_flags = SS_DISABLE}; sigaltstack(&off, 0); free(p); } @@ -194,6 +216,7 @@ void crash_thread_init(void) { stack_t ss = {.ss_sp = malloc(65536), .ss_size = 65536}; sigaltstack(&ss, 0); pthread_setspecific(g_altstack_key, ss.ss_sp); } +#endif void crash_install(void) { crash_thread_init(); struct sigaction sa = {.sa_sigaction = on_crash, .sa_flags = SA_SIGINFO | SA_ONSTACK}; diff --git a/dm/host/dm.h b/dm/host/dm.h index c1be8d46fb..c08864b3d9 100644 --- a/dm/host/dm.h +++ b/dm/host/dm.h @@ -96,7 +96,9 @@ uint64_t gate_dispatch(uint64_t ret_addr, uint64_t *regs); enum { OBS_UE_MALLOC = 21, // DRAGON QUEST VII Reimagined 1.2.0: Unreal's allocator object just made (hle_uemalloc.c) OBS_REGION_ASK = 22, OBS_REGION_GOT = 23, OBS_REGION_FREE = 24, // the SDK's address regions, gates the rewriter places by symbol in every sdk (patch_nso.py "Address regions", hle_svc.c) OBS_STDALLOC_INIT = 25, // StandardAllocator::Initialize's entry, the same way: the one gate whose callback writes a register (hle_svc.c) - OBS_MAX = 26 }; + OBS_ZA_CLOCK_DELTA = 26, OBS_ZA_INTERVAL = 27, // Pokemon Legends: Z-A 2.0.2 timing diagnostics / optional interval experiment + OBS_ZA_SKILL_VISIBILITY = 28, OBS_ZA_SKILL_DESTROY = 29, // Read-only observation of the actual move layout lifetime + OBS_MAX = 30 }; // A gate number's bits from 8 up are a parameter for its callback (OBS_REGION_GOT: the register that holds the address). Callbacks read the registers; a write to x0–x29 takes // effect when the gate returns (entry.S restores them from the frame), which only OBS_STDALLOC_INIT does void gate_obs_set(int id, void (*fn)(uint64_t *regs, int param)); @@ -129,6 +131,7 @@ uint64_t hle_system_event_binding(void *g); // Opaque lifetime ID of a binding int hle_signal_system_event_binding(uint64_t binding); // Signal only this binding lifetime (any thread); 0 after its write handle closes // Page fault hook / hook before the host writes guest memory (crash.c; used for write-protect dirty page tracking) void dm_fault_hook_register(int (*fn)(void *addr, int is_write, void *uc)); +void dm_fault_diagnostic_register(void (*fn)(const void *addr)); void dm_guest_write_prep_register(void (*fn)(void *addr, size_t len)); void dm_res_report_register(void (*fn)(uint32_t tick, const char *why)); // Resource count callback (registered by the graphics layer): called every 250 ticks, when the loading screen has been dismissed, and on exit when the record is full void (*dm_res_report_cb(void))(uint32_t tick, const char *why); // Get the registered resource count callback (null if not registered) diff --git a/dm/host/dm_input.h b/dm/host/dm_input.h index 9cda0d0217..054ce0c6c9 100644 --- a/dm/host/dm_input.h +++ b/dm/host/dm_input.h @@ -13,8 +13,12 @@ enum { #define DM_STICK_MAX 32767 void dm_input_set(uint64_t buttons, int32_t lx, int32_t ly, int32_t rx, int32_t ry); uint64_t dm_input_samples(void); // Cumulative number of times the game has sampled the controller -// Physical controller path (Mac / iOS GameController): stored separately from keyboard / touch and scripted input (DM_KEYS / DM_CTL); when the game reads player 1, -// buttons are ORed bitwise and each stick axis takes whichever source is deflected more. All 0 when no controller is connected. +// Exact-build observation of the game's actual move selector. False by default; +// only the explicit Z-A virtual-layout option enables it. Does not alter HID, +// keyboard bindings, native button meaning or the game's original skill HUD. +int dm_za_skill_layout_active(void); +// Physical controller path (Mac GameController, DM_KEYS / DM_CTL): stored separately from the keyboard / touch input; when the game reads player 1, buttons are +// ORed bitwise and each stick axis takes whichever source is deflected more. All 0 when no controller is connected, so values are bit-for-bit the same as before. // dm_pad_set is player 1's controller (DM_BTN_SHAKE counts there only); dm_pad_set_player sets player n + 1's, which the game reads on its own npad (hle_hid.c) while // dm_pad_players has its bit set; a player whose npad the game does not support (SetSupportedNpadIdType) feeds player 1 instead, so a one-player game sees every controller #define DM_PLAYERS 8 // npads No1..No8; player 1 is the Handheld npad in handheld mode (DM_OPMODE=0) diff --git a/dm/host/hle_audio.c b/dm/host/hle_audio.c index 6ad2c8a467..fb8ea53320 100644 --- a/dm/host/hle_audio.c +++ b/dm/host/hle_audio.c @@ -8,6 +8,7 @@ #include #include #include +#include enum { FMT_PCM16 = 2, STATE_STARTED = 0, STATE_STOPPED = 1, RATE = 48000 }; enum { BK_NONE, BK_AU, BK_NULL }; // what is consuming an output's queue right now (aout.backend) @@ -25,6 +26,8 @@ typedef struct { gbuf *rel[RELMAX]; _Atomic uint32_t rel_head, rel_tail; // Released: written by the output side, read by the game side uint64_t pos; // Bytes already played of the current block AudioUnit au; + int real_requested, mute; // cached at open, never getenv() on the device callback + _Atomic int consumer, pulling; // one queue consumer even if a failed Stop leaves stale callbacks int backend; // BK_*: the running consumer. Start, Stop and the background suspend change it only under g_aos_lock and act on it rather than re-deriving it from state and g_suspended pthread_t null_th; _Atomic int null_run; dispatch_semaphore_t notify_sem; @@ -33,7 +36,10 @@ typedef struct { int id; // open sequence number (#N in the log; tells simultaneous outputs apart, e.g. Engage's Wwise and Unity's built-in FMOD) int nlog; // Start / Stop / Close lines logged so far (at most 32 per output) _Atomic uint64_t under_n, under_b; // underruns: times the pending queue was empty when the callback needed data / zero-filled bytes - uint64_t st_calls, st_qsum, st_u0, st_ub0; uint32_t st_fmin, st_fmax; // sound card callback statistics (read and written only on the callback thread) + _Atomic uint64_t st_calls, st_qsum; + _Atomic uint32_t st_fmin, st_fmax; + uint64_t st_u0, st_ub0, st_print_calls, st_print_qsum; // watchdog only, under g_aos_lock + _Atomic uint64_t pcm_levels, pcm_counts; // last callback: peak/mean-square and zero/sample counts, no PCM content _Atomic uint64_t cb_n; // device callbacks of this output (the watchdog's evidence that the device still plays it) uint64_t wd_n, wd_t; // watchdog: callback count last seen and when it last moved (under g_aos_lock) int stalled, retry_ms; uint64_t retry_at; // the device stopped calling back (or would not start): the silent sink stands in and the watchdog retries the AudioUnit at retry_at, backing off @@ -46,6 +52,17 @@ typedef struct { static aout *g_aos[AOMAX]; static pthread_mutex_t g_aos_lock = PTHREAD_MUTEX_INITIALIZER; static int g_suspended; +// Platform-neutral CTL notification. A Mac host or a test has no UIKit observer; +// the iOS frontend registers a static callback and dispatches onto its main queue. +static _Atomic(void (*)(const char *, const char *)) g_audio_env_callback; +void dm_audio_env_callback_set(void (*fn)(const char *, const char *)) { + atomic_store_explicit(&g_audio_env_callback, fn, memory_order_release); +} +void dm_audio_env_changed(const char *key, const char *value) { + if (!key || strcmp(key, "DM_AUDIO_SESSION_CATEGORY")) return; + void (*fn)(const char *, const char *) = atomic_load_explicit(&g_audio_env_callback, memory_order_acquire); + if (fn) fn(key, value); +} static aout *ao(const void *g) { aout *a = objmap_get(g); if (!a) die("AudioOut %p was not created by the host", g); return a; } // the call site as "module+offset"; "host" when the host itself calls (in-process audio renderer) static const char *site(void *lr, char *buf, size_t n) { @@ -143,21 +160,50 @@ static void pull_device(aout *a, uint8_t *dst, uint64_t bytes) { static _Atomic uint64_t g_render_calls; // Cumulative sound card callbacks (lifecycle verification: must keep growing after resume) uint64_t dm_audio_render_calls(void) { return atomic_load_explicit(&g_render_calls, memory_order_relaxed); } +static void backend_pull(aout *a, int backend, uint8_t *dst, uint64_t bytes) { + int free = 0; + if (atomic_load_explicit(&a->consumer, memory_order_acquire) != backend || + !atomic_compare_exchange_strong_explicit(&a->pulling, &free, 1, memory_order_acquire, memory_order_relaxed)) { + memset(dst, 0, bytes); return; + } + if (atomic_load_explicit(&a->consumer, memory_order_acquire) == backend) { + // The device callback requests device-channel bytes. Six-channel guest + // output therefore needs the existing 6 -> 2 downmix, while the null + // sink consumes the original guest-channel stream directly. + if (backend == BK_AU) pull_device(a, dst, bytes); + else pull(a, dst, bytes); + } + else memset(dst, 0, bytes); + atomic_store_explicit(&a->pulling, 0, memory_order_release); +} +// Bounded integer-only meter. No allocation, locks, file I/O or logging on the +// real-time callback. The management/watchdog thread converts mean-square to RMS. +static void au_meter_publish(aout *a, const int16_t *pcm, uint64_t samples) { + uint32_t n = (uint32_t)(samples < 4096 ? samples : 4096), peak = 0, zeros = 0; + uint64_t square = 0; + for (uint32_t i = 0; i < n; i++) { + int32_t v = pcm[(uint64_t)i * samples / n]; uint32_t magnitude = v < 0 ? (uint32_t)-v : (uint32_t)v; + if (magnitude > peak) peak = magnitude; + square += (uint64_t)((int64_t)v * v); zeros += v == 0; + } + uint32_t mean = n ? (uint32_t)(square / n) : 0; + atomic_store_explicit(&a->pcm_levels, (uint64_t)peak << 32 | mean, memory_order_release); + atomic_store_explicit(&a->pcm_counts, (uint64_t)zeros << 32 | n, memory_order_release); +} static OSStatus au_render(void *ctx, AudioUnitRenderActionFlags *f, const AudioTimeStamp *ts, UInt32 bus, UInt32 frames, AudioBufferList *bl) { (void)f; (void)ts; (void)bus; (void)frames; aout *a = ctx; atomic_fetch_add_explicit(&g_render_calls, 1, memory_order_relaxed); atomic_fetch_add_explicit(&a->cb_n, 1, memory_order_relaxed); { - // Every 2000 callbacks of each output, log one line: frames requested per call, pending blocks at callback start, underruns (check this line on a new device; docs/reference/audio.md "RemoteIO's default I/O buffer duration"). - // Kept per output: with two outputs open, combined numbers mislead (one fully fed and one fed by nobody look like "every other callback underruns") uint32_t qd = atomic_load(&a->q_tail) - atomic_load(&a->q_head); - if (!a->st_calls || frames < a->st_fmin) a->st_fmin = frames; if (frames > a->st_fmax) a->st_fmax = frames; a->st_qsum += qd; - if (++a->st_calls % 2000 == 0) { uint64_t un = atomic_load(&a->under_n), ubb = atomic_load(&a->under_b); - LOG("audio #%d device callbacks %llu: %u–%u frames each, %.1f blocks queued on average at callback; in these 2000, %llu underruns, %.1f ms zero-padded", a->id, (unsigned long long)a->st_calls, a->st_fmin, a->st_fmax, a->st_qsum / 2000.0, - (unsigned long long)(un - a->st_u0), (ubb - a->st_ub0) / (2.0 * a->ch) / (RATE / 1000.0)); a->st_fmin = ~0u; a->st_fmax = 0; a->st_qsum = 0; a->st_u0 = un; a->st_ub0 = ubb; } } - pull_device(a, bl->mBuffers[0].mData, bl->mBuffers[0].mDataByteSize); - { static int mute = -1; if (mute < 0) { const char *e = getenv("DM_AUDIO_MUTE"); mute = e && atoi(e); } // diagnostic: the whole real-output path (AudioUnit callback pacing, background take-over) but with the output silenced (lifecycle tests on the Mac without noise) - if (mute) memset(bl->mBuffers[0].mData, 0, bl->mBuffers[0].mDataByteSize); } + uint32_t lo = atomic_load(&a->st_fmin), hi = atomic_load(&a->st_fmax); + while ((!lo || frames < lo) && !atomic_compare_exchange_weak(&a->st_fmin, &lo, frames)) {} + while (frames > hi && !atomic_compare_exchange_weak(&a->st_fmax, &hi, frames)) {} + atomic_fetch_add(&a->st_qsum, qd); atomic_fetch_add(&a->st_calls, 1); + } + backend_pull(a, BK_AU, bl->mBuffers[0].mData, bl->mBuffers[0].mDataByteSize); + if (a->mute) memset(bl->mBuffers[0].mData, 0, bl->mBuffers[0].mDataByteSize); + au_meter_publish(a, bl->mBuffers[0].mData, bl->mBuffers[0].mDataByteSize / 2); return noErr; } #if !TARGET_OS_IPHONE @@ -208,7 +254,9 @@ static OSStatus au_create(aout *a) { #endif .componentManufacturer = kAudioUnitManufacturer_Apple}; AudioComponent c = AudioComponentFindNext(NULL, &d); - a->au = NULL; if (!c || AudioComponentInstanceNew(c, &a->au)) { a->au = NULL; return -1; } + a->au = NULL; if (!c) return kAudio_ParamError; + OSStatus created = AudioComponentInstanceNew(c, &a->au); + if (created) { a->au = NULL; return created; } unsigned ch = (unsigned)device_channels(a); AudioStreamBasicDescription f = {.mSampleRate = RATE, .mFormatID = kAudioFormatLinearPCM, .mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked, @@ -233,10 +281,10 @@ static OSStatus au_create(aout *a) { // left (Super Mario 3D World's save thread has 8 KB in all; Pokémon: Let's Go overflowed here at startup, PR #36). So it runs on a host thread, once per AudioOut. Not HLE_FORCE_BIG: // the open hooks log the game's call site through __builtin_return_address(0), which under the big-stack entry would be the trampoline static void *au_create_th(void *a) { return (void *)(intptr_t)au_create(a); } -static void au_open(aout *a) { +static OSStatus au_open(aout *a) { pthread_t t; void *r = NULL; - if (pthread_create(&t, NULL, au_create_th, a) || dm_host_thread_join(t, &r)) die("AudioUnit setup: cannot start its thread"); - OSStatus st = (OSStatus)(intptr_t)r; if (st) die("AudioUnit setup failed (status %d)", (int)st); + if (pthread_create(&t, NULL, au_create_th, a) || pthread_join(t, &r)) die("AudioUnit setup: cannot start its thread"); + return (OSStatus)(intptr_t)r; } // Silent sink: consumes in real time at 48 kHz every 5 ms. When more than 100 ms behind (iOS suspended the whole process in the background, SIGSTOP on the Mac), restart the count from now and do not make up the missed time -- // making it up would pull hundreds of times in one go (seconds' worth), releasing all of the game's buffers and logging hundreds of underruns; a suspended console does not play that stretch either @@ -246,24 +294,26 @@ static void *null_sink(void *p) { while (atomic_load_explicit(&a->null_run, memory_order_acquire)) { t += 5000000; uint64_t now = now_ns(); if (t > now) usleep((useconds_t)((t - now) / 1000)); else if (now - t > 100000000) t = now; - pull(a, scratch, bytes); + backend_pull(a, BK_NULL, scratch, bytes); } return NULL; } // Backend changes; callers hold g_aos_lock. pull() takes no lock, so joining a sink under it cannot deadlock -static void null_start(aout *a) { atomic_store_explicit(&a->null_run, 1, memory_order_release); pthread_create(&a->null_th, NULL, null_sink, a); a->backend = BK_NULL; } -static void null_stop(aout *a) { atomic_store_explicit(&a->null_run, 0, memory_order_release); pthread_join(a->null_th, NULL); a->backend = BK_NONE; } +static void null_start(aout *a) { atomic_store(&a->consumer, BK_NULL); atomic_store_explicit(&a->null_run, 1, memory_order_release); if (pthread_create(&a->null_th, NULL, null_sink, a)) die("cannot start silent audio consumer"); a->backend = BK_NULL; } +static void null_stop(aout *a) { atomic_store(&a->consumer, BK_NONE); atomic_store_explicit(&a->null_run, 0, memory_order_release); pthread_join(a->null_th, NULL); a->backend = BK_NONE; } static void backend_stop(aout *a) { - if (a->backend == BK_AU) { AudioOutputUnitStop(a->au); a->backend = BK_NONE; } + if (a->backend == BK_AU) { atomic_store(&a->consumer, BK_NONE); OSStatus st = AudioOutputUnitStop(a->au); if (st) LOG("audio #%d: AudioUnit stop status %d (session may have changed)", a->id, (int)st); a->backend = BK_NONE; } else if (a->backend == BK_NULL) null_stop(a); } static int real_audio(void) { const char *e = getenv("DM_AUDIO"); return e ? atoi(e) != 0 : TARGET_OS_IPHONE; } // Start the AudioUnit of an output whose backend is BK_NONE (callers hold g_aos_lock). When the device refuses (the session is held by a call, or is being reset), the silent sink stands in and the watchdog retries static void au_go(aout *a, uint64_t now) { - OSStatus st = a->au ? AudioOutputUnitStart(a->au) : -1; + OSStatus st = a->au ? noErr : au_open(a); // recreating a lost unit also needs a large helper stack + atomic_store(&a->consumer, BK_AU); + if (!st) st = AudioOutputUnitStart(a->au); a->wd_n = atomic_load_explicit(&a->cb_n, memory_order_relaxed); a->wd_t = now; if (!st) { a->backend = BK_AU; a->stalled = 0; return; } - null_start(a); + atomic_store(&a->consumer, BK_NONE); null_start(a); if (!a->stalled) { a->stalled = 1; a->retry_ms = 1000; LOG("[RED] audio #%d: the AudioUnit did not start (status %d): the silent sink stands in, retrying", a->id, (int)st); } else if (a->retry_ms < 8000) a->retry_ms *= 2; a->retry_at = now + (uint64_t)a->retry_ms * 1000000; @@ -274,28 +324,60 @@ static void au_go(aout *a, uint64_t now) { // The time is read under g_aos_lock: au_go stamps wd_t under it too, and a watchdog time read before the lock was older than the stamp of an AudioUnit the return from the background had just // started (DRAGON QUEST VII Reimagined on an iPhone: "has not called back for 18446744073662 ms", a restart of a running AudioUnit) enum { WD_TICK_MS = 50, WD_STALL_MS = 200 }; +static void au_diagnostic_locked(aout *a, const char *why) { + uint64_t levels = atomic_load_explicit(&a->pcm_levels, memory_order_acquire); + uint64_t counts = atomic_load_explicit(&a->pcm_counts, memory_order_acquire); + uint32_t volume_bits = atomic_load(&a->vol); float volume; memcpy(&volume, &volume_bits, sizeof volume); + LOG("audio #%d status (%s): state=%d backend=%d unit=%s suspended=%d callbacks=%llu queued=%u gain=%.3f diagnosticMute=%d PCM_after_gain_and_mute peak=%u rms=%.6f zeros=%u/%u; PCM level does not establish speaker audibility", + a->id, why, atomic_load(&a->state), a->backend, a->au ? "ready" : "missing", g_suspended, + (unsigned long long)atomic_load(&a->cb_n), atomic_load(&a->q_tail) - atomic_load(&a->q_head), volume, a->mute, + (uint32_t)(levels >> 32), sqrt((double)(uint32_t)levels) / 32768.0, (uint32_t)(counts >> 32), (uint32_t)counts); +} +void dm_audio_diagnostics(const char *why) { + pthread_mutex_lock(&g_aos_lock); + for (int i = 0; i < AOMAX; i++) if (g_aos[i]) au_diagnostic_locked(g_aos[i], why ?: "requested"); + pthread_mutex_unlock(&g_aos_lock); +} +// Called under g_aos_lock on a management thread, never by the real-time callback. +static void au_watchdog_step_locked(uint64_t t, uint64_t gap) { + for (int i = 0; i < AOMAX; i++) { + aout *a = g_aos[i]; if (!a || !a->real_requested) continue; + uint64_t calls = atomic_load(&a->st_calls); + if ((!a->st_print_calls && calls >= 100) || (a->st_print_calls && calls - a->st_print_calls >= 2000)) { + uint64_t un = atomic_load(&a->under_n), ubb = atomic_load(&a->under_b), qsum = atomic_load(&a->st_qsum); + uint64_t ncalls = calls - a->st_print_calls; + LOG("audio #%d device callbacks %llu: %u-%u frames each, %.1f blocks queued on average at callback; in these %llu, %llu underruns, %.1f ms zero-padded", a->id, + (unsigned long long)calls, atomic_exchange(&a->st_fmin, 0), atomic_exchange(&a->st_fmax, 0), (qsum - a->st_print_qsum) / (double)ncalls, + (unsigned long long)ncalls, (unsigned long long)(un - a->st_u0), (ubb - a->st_ub0) / (2.0 * a->ch) / (RATE / 1000.0)); + a->st_print_calls = calls; a->st_print_qsum = qsum; a->st_u0 = un; a->st_ub0 = ubb; + au_diagnostic_locked(a, "callback meter"); + } + // A failed recreation deliberately leaves au=NULL. Preserve real-output + // intent and retry it; a stopped/suspended output must never be restarted. + if (atomic_load(&a->state) != STATE_STARTED) continue; + uint64_t n = atomic_load_explicit(&a->cb_n, memory_order_relaxed); + if (a->backend != BK_AU || g_suspended || gap > 4ull * WD_TICK_MS * 1000000 || n != a->wd_n) { + if (a->backend == BK_AU && n != a->wd_n) a->retry_ms = 0; + a->wd_n = n; a->wd_t = t; + } else if (t >= a->wd_t && t - a->wd_t > WD_STALL_MS * 1000000ull) { + LOG("[RED] audio #%d: the device has not called back for %.0f ms while playing: the silent sink stands in, retrying the AudioUnit", a->id, (t - a->wd_t) / 1e6); + backend_stop(a); null_start(a); + a->stalled = 1; a->retry_ms = a->retry_ms ? (a->retry_ms < 8000 ? a->retry_ms * 2 : 8000) : 1000; + a->retry_at = t + (uint64_t)a->retry_ms * 1000000; + } + if (a->stalled && a->backend == BK_NULL && !g_suspended && t >= a->retry_at) { + null_stop(a); au_go(a, t); + if (a->backend == BK_AU) LOG("audio #%d: AudioUnit recreated/restarted", a->id); + } + } +} static void *au_watchdog(void *p) { (void)p; uint64_t last = now_ns(); for (;;) { usleep(WD_TICK_MS * 1000); pthread_mutex_lock(&g_aos_lock); uint64_t t = now_ns(), gap = t - last; last = t; - for (int i = 0; i < AOMAX; i++) { - aout *a = g_aos[i]; if (!a || !a->au) continue; - uint64_t n = atomic_load_explicit(&a->cb_n, memory_order_relaxed); - if (a->backend != BK_AU || g_suspended || gap > 4ull * WD_TICK_MS * 1000000 || n != a->wd_n) { - if (a->backend == BK_AU && n != a->wd_n) a->retry_ms = 0; // callbacks seen: a later stall starts the back-off from 1 s again - a->wd_n = n; a->wd_t = t; - } else if (t - a->wd_t > WD_STALL_MS * 1000000ull) { - LOG("[RED] audio #%d: the device has not called back for %.0f ms while playing: the silent sink stands in, retrying the AudioUnit", a->id, (t - a->wd_t) / 1e6); - AudioOutputUnitStop(a->au); a->backend = BK_NONE; null_start(a); - a->stalled = 1; a->retry_ms = a->retry_ms ? (a->retry_ms < 8000 ? a->retry_ms * 2 : 8000) : 1000; a->retry_at = t + (uint64_t)a->retry_ms * 1000000; - } - if (a->stalled && a->backend == BK_NULL && !g_suspended && t >= a->retry_at) { - null_stop(a); au_go(a, t); - if (a->backend == BK_AU) LOG("audio #%d: AudioUnit restarted", a->id); - } - } + au_watchdog_step_locked(t, gap); pthread_mutex_unlock(&g_aos_lock); } return NULL; @@ -320,6 +402,7 @@ static Result au_open_at(void *g, void *ev, const char *name, const uint8_t *par if (rate && rate != RATE) die("AudioOut sample rate %d not supported (48 kHz only)", rate); aout *a = calloc(1, sizeof *a); pthread_mutex_init(&a->m, NULL); a->event = ev; a->ch = ch; + a->real_requested = real_audio(); const char *mute = getenv("DM_AUDIO_MUTE"); a->mute = mute && atoi(mute); atomic_init(&a->state, STATE_STOPPED); atomic_init(&a->vol, 0x3f800000u); atomic_init(&a->q_head, 0); atomic_init(&a->q_tail, 0); atomic_init(&a->rel_head, 0); atomic_init(&a->rel_tail, 0); @@ -328,18 +411,17 @@ static Result au_open_at(void *g, void *ev, const char *name, const uint8_t *par a->notify_sem = dispatch_semaphore_create(0); a->id = atomic_fetch_add(&nopen, 1); pthread_create(&a->notify_th, NULL, event_notify, a); if (ev) hle_bind_system_event(ev, 1); // Buffer release event: bind a kernel event on the guest thread (auto-clear), afterwards signaled by the audio thread - char sb[64]; int use_au = real_audio(); - LOG("audio #%d OpenAudioOut %s channels %d %s%s (call site %s)", a->id, name ? name : "(default)", ch, use_au ? "real output" : "silent sink", ev ? "" : ", no release event (the game polls)", site(lr, sb, sizeof sb)); - // The helper finishes (and is joined) before lifecycle/watchdog readers can find this output. - // Keep construction outside g_aos_lock: joining the helper can yield the guest core. - if (use_au) au_open(a); objmap_put(g, a); + // Finish creation before publishing to lifecycle/reset/watchdog scans. + if (a->real_requested) { OSStatus st = au_open(a); if (st) LOG("[RED] audio #%d: initial AudioUnit creation failed (status %d); retrying when started", a->id, (int)st); } pthread_mutex_lock(&g_aos_lock); int slot = -1; for (int i = 0; i < AOMAX; i++) if (!g_aos[i]) { slot = i; break; } if (slot < 0) die("more than %d AudioOuts open at once", AOMAX); g_aos[slot] = a; pthread_mutex_unlock(&g_aos_lock); - if (use_au) { static pthread_once_t wd = PTHREAD_ONCE_INIT; pthread_once(&wd, au_watchdog_start); } + char sb[64]; + LOG("audio #%d OpenAudioOut %s channels %d %s%s (call site %s)", a->id, name ? name : "(default)", ch, a->real_requested ? "real output" : "silent sink", ev ? "" : ", no release event (the game polls)", site(lr, sb, sizeof sb)); + if (a->real_requested) { static pthread_once_t wd = PTHREAD_ONCE_INIT; pthread_once(&wd, au_watchdog_start); } au_info(g, ch, STATE_STOPPED); return RES_OK; } @@ -356,7 +438,7 @@ static Result au_start_at(void *g, void *lr) { int was = atomic_exchange_explicit(&a->state, STATE_STARTED, memory_order_acq_rel); au_note(a, was == STATE_STARTED ? "Start (already playing)" : g_suspended ? "Start (app in the background: the silent sink takes over for now)" : "Start", lr); if (was != STATE_STARTED) { - if (a->au && !g_suspended) { a->stalled = 0; au_go(a, now_ns()); } + if (a->real_requested && !g_suspended) { a->stalled = 0; au_go(a, now_ns()); } else null_start(a); } pthread_mutex_unlock(&g_aos_lock); @@ -499,9 +581,9 @@ void dm_audio_suspend(int on) { g_suspended = on; int failed = 0; for (int i = 0; i < AOMAX; i++) { aout *a = g_aos[i]; - if (!a || !a->au) continue; + if (!a || !a->real_requested || atomic_load(&a->state) != STATE_STARTED) continue; // While suspended the silent sink consumes in real time instead: otherwise buffers are no longer released, the FMOD output thread waits forever and the game's audio pacing stalls (same for an incoming call interruption) - if (on) { if (a->backend == BK_AU) { AudioOutputUnitStop(a->au); null_start(a); } } + if (on) { if (a->backend == BK_AU) { backend_stop(a); null_start(a); } } else if (a->backend == BK_NULL) { // The session may still be held by someone else (back in the game with a call not yet hung up): not fatal, the silent sink keeps covering and the watchdog retries null_stop(a); a->stalled = 0; au_go(a, now_ns()); failed |= a->backend != BK_AU; } @@ -514,13 +596,19 @@ void dm_audio_suspend(int on) { void dm_audio_reset(void) { pthread_mutex_lock(&g_aos_lock); for (int i = 0; i < AOMAX; i++) { - aout *a = g_aos[i]; if (!a || !a->au) continue; - int playing = a->backend == BK_AU; - if (playing) { AudioOutputUnitStop(a->au); a->backend = BK_NONE; } - AudioUnitUninitialize(a->au); AudioComponentInstanceDispose(a->au); - OSStatus st = au_create(a); - if (st) LOG("[RED] audio #%d: no AudioUnit after the media services reset (status %d): this output stays silent", a->id, (int)st); - if (playing) { if (a->au && !g_suspended) { a->stalled = 0; au_go(a, now_ns()); } else null_start(a); } + aout *a = g_aos[i]; if (!a || !a->real_requested) continue; + int playing = atomic_load(&a->state) == STATE_STARTED; + backend_stop(a); + if (a->au) { AudioUnitUninitialize(a->au); AudioComponentInstanceDispose(a->au); a->au = NULL; } + // Inactive sessions must not be reactivated indirectly by creating a new + // RemoteIO. Foreground resume will create the unit on its helper stack. + OSStatus st = g_suspended ? noErr : au_open(a); + if (st) LOG("[RED] audio #%d: AudioUnit recreation failed after media reset (status %d); keeping the silent sink and retrying", a->id, (int)st); + a->stalled = 0; a->retry_ms = 0; + if (playing) { + if (a->au && !g_suspended) au_go(a, now_ns()); + else { null_start(a); a->stalled = 1; a->retry_ms = 1000; a->retry_at = now_ns() + 1000000000ull; } + } } LOG("audio outputs set up again after the media services reset"); pthread_mutex_unlock(&g_aos_lock); diff --git a/dm/host/hle_fs.c b/dm/host/hle_fs.c index 26da5c055d..ac2bea06ba 100644 --- a/dm/host/hle_fs.c +++ b/dm/host/hle_fs.c @@ -58,7 +58,96 @@ static pthread_mutex_t g_path_lock = PTHREAD_MUTEX_INITIALIZER; static void path_unlock(pthread_mutex_t **m) { pthread_mutex_unlock(*m); } #define PATH_LOCK() pthread_mutex_t *path_guard __attribute__((cleanup(path_unlock))) = &g_path_lock; pthread_mutex_lock(path_guard) static uint8_t g_fmode[65536]; // fd → open mode +static uint8_t g_fsave[65536]; // fd → whether it is on a transactional save mount (the save write injection point only counts these) static _Atomic uint16_t g_ftxn[65536]; // fd → 1-based canonical transaction index, or 0 +// DM_ROMFS_REDIRECTS=: virtual sequential archives backed by disjoint +// ranges of one materialized RomFS file. Some archive formats require their +// index offsets to stay both sorted and separated by each record's aligned +// size. A compact overlay can keep unmodified records in the cloned source, +// put larger replacements in an inserted range, and describe the resulting +// logical archive here without physically moving every later record. +typedef struct { + uint64_t virtual_offset, virtual_bytes; + uint64_t physical_offset, physical_bytes; +} rom_redirect_t; +static rom_redirect_t *g_rom_redirect; +static size_t g_nrom_redirect; +static uint64_t g_rom_virtual_bytes, g_rom_physical_bytes; +static char g_rom_redirect_target[1024]; +static pthread_once_t g_rom_redirect_once = PTHREAD_ONCE_INIT; +static uint8_t g_fredirect[65536]; // fd -> matches the configured archive + +static void rom_redirect_init(void) { + const char *path = getenv("DM_ROMFS_REDIRECTS"); + if (!path) return; + if (!*path) die("DM_ROMFS_REDIRECTS is empty"); + FILE *f = fopen(path, "r"); + if (!f) die("cannot open DM_ROMFS_REDIRECTS %s (errno=%d)", path, errno); + char magic[64], target[sizeof g_rom_redirect_target], newline = 0; + unsigned long long count = 0, virtual_bytes = 0, physical_bytes = 0; + if (fscanf(f, "%63[^\t]\t%1023[^\t]\t%llu\t%llu\t%llu%c", + magic, target, &count, &virtual_bytes, &physical_bytes, &newline) != 6 || + strcmp(magic, "DM_ROMFS_REDIRECTS_V1") || newline != '\n' || + !count || count > 20000 || !virtual_bytes || !physical_bytes || + virtual_bytes > INT64_MAX || physical_bytes > INT64_MAX || + target[0] == '/' || strchr(target, ':') || strstr(target, "..")) { + fclose(f); die("invalid DM_ROMFS_REDIRECTS header in %s", path); + } + rom_redirect_t *rows = calloc((size_t)count, sizeof *rows); + if (!rows) { fclose(f); die("DM_ROMFS_REDIRECTS allocation failed"); } + uint64_t previous_virtual_end = 0; + for (size_t i = 0; i < (size_t)count; i++) { + unsigned long long vo, vb, po, pb; newline = 0; + if (fscanf(f, "%llu\t%llu\t%llu\t%llu%c", &vo, &vb, &po, &pb, &newline) != 5 || + newline != '\n' || !vb || !pb || pb < vb || + vo > virtual_bytes || vb > virtual_bytes - vo || + po > physical_bytes || pb > physical_bytes - po || + vo != previous_virtual_end) { + free(rows); fclose(f); die("invalid DM_ROMFS_REDIRECTS row %zu in %s", i + 1, path); + } + rows[i] = (rom_redirect_t){vo, vb, po, pb}; + previous_virtual_end = vo + vb; + } + if (previous_virtual_end != virtual_bytes || fgetc(f) != EOF || ferror(f)) { + free(rows); fclose(f); die("invalid end of DM_ROMFS_REDIRECTS %s", path); + } + fclose(f); + strlcpy(g_rom_redirect_target, target, sizeof g_rom_redirect_target); + g_rom_virtual_bytes = virtual_bytes; + g_rom_physical_bytes = physical_bytes; + g_rom_redirect = rows; + g_nrom_redirect = (size_t)count; + LOG("fs redirects: %zu virtual ranges for %s from %s", g_nrom_redirect, + g_rom_redirect_target, path); +} + +// Translate one bounded part of an arbitrary logical read. The validated +// rows cover [0, virtual EOF) without gaps, so a read may start in a row and +// continue across any number of later rows. +static int rom_redirect_chunk(uint64_t logical, size_t *bytes, uint64_t *physical) { + if (logical >= g_rom_virtual_bytes || !g_nrom_redirect || !*bytes) return 0; + size_t lo = 0, hi = g_nrom_redirect; + while (lo < hi) { + size_t mid = lo + (hi - lo) / 2; + if (g_rom_redirect[mid].virtual_offset <= logical) lo = mid + 1; + else hi = mid; + } + if (!lo) return 0; + const rom_redirect_t *row = &g_rom_redirect[lo - 1]; + uint64_t delta = logical - row->virtual_offset; + if (delta >= row->virtual_bytes) return 0; + uint64_t available = row->virtual_bytes - delta; + if ((uint64_t)*bytes > available) *bytes = (size_t)available; + *physical = row->physical_offset + delta; + return 1; +} + +static int64_t rom_redirect_reported_size(const char *relative, int64_t physical) { + pthread_once(&g_rom_redirect_once, rom_redirect_init); + if (!relative || !g_nrom_redirect) return physical; + while (*relative == '/') relative++; + return !strcmp(relative, g_rom_redirect_target) ? (int64_t)g_rom_virtual_bytes : physical; +} static _Atomic int g_save_writes; // number of writes on the save mount (shared by the injection point and the commit log) static pthread_once_t g_save_test_once = PTHREAD_ONCE_INIT; static int g_save_kill_n, g_save_fail_n, g_save_fail_e; @@ -711,9 +800,26 @@ HLE_FORCE("_ZN2nn2fs8OpenFileEPNS0_10FileHandleEPKci", "fsp-srv → open(2)", Re struct stat st; fstat(fd, &st); if (S_ISDIR(st.st_mode)) { close(fd); return RES_PATH_NOT_FOUND; } if (fd >= (int)sizeof g_fmode) die("fd beyond the table"); - g_fmode[fd] = (uint8_t)mode; *out = (uint64_t)fd; - { const char *c = strchr(path, ':'); size_t ln = c ? (size_t)(c - path) : 0; uint16_t token = 0; - for (int i = 0; c && i < g_nmnt; i++) if (strlen(g_mnt[i].name) == ln && !strncmp(g_mnt[i].name, path, ln) && g_mnt[i].txn_root >= 0) { token = (uint16_t)(g_mnt[i].txn_root + 1); break; } + g_fmode[fd] = (uint8_t)mode; g_fredirect[fd] = 0; *out = (uint64_t)fd; + pthread_once(&g_rom_redirect_once, rom_redirect_init); + if (g_nrom_redirect) { + const char *colon = strchr(path, ':'); + const char *relative = colon && colon[1] == '/' ? colon + 2 : NULL; + if (relative && !strcmp(relative, g_rom_redirect_target)) { + if ((uint64_t)st.st_size != g_rom_physical_bytes) { + close(fd); g_fmode[fd] = 0; *out = 0; + die("DM_ROMFS_REDIRECTS target %s has %lld bytes, expected %llu", path, + (long long)st.st_size, (unsigned long long)g_rom_physical_bytes); + } + g_fredirect[fd] = 1; + } + } + { const char *c = strchr(path, ':'); size_t ln = c ? (size_t)(c - path) : 0; uint16_t token = 0; g_fsave[fd] = 0; + for (int i = 0; c && i < g_nmnt; i++) if (strlen(g_mnt[i].name) == ln && !strncmp(g_mnt[i].name, path, ln)) { + g_fsave[fd] = g_mnt[i].commit[0] != 0; + if (g_mnt[i].txn_root >= 0) token = (uint16_t)(g_mnt[i].txn_root + 1); + break; + } atomic_store(&g_ftxn[fd], token); } fslog_init(); if (g_fslog && fd < 4096) { pthread_mutex_lock(&g_fslog_lock); free(g_fdpath[fd]); g_fdpath[fd] = strdup(path); pthread_mutex_unlock(&g_fslog_lock); } @@ -722,15 +828,27 @@ HLE_FORCE("_ZN2nn2fs8OpenFileEPNS0_10FileHandleEPKci", "fsp-srv → open(2)", Re } HLE_FORCE("_ZN2nn2fs8ReadFileEPmNS0_10FileHandleElPvm", "fsp-srv → pread(2)", Result, h_fs_ReadFile, size_t *out, uint64_t h, int64_t off, void *buf, size_t sz) { if (!h) { *out = 0; return RES_FAIL; } // a file handle is an fd; 0 is stdin and never a file the game opened: the null handle the game keeps using after a failed open - size_t got = 0; int reprep = 0, fd = (int)h; int64_t base = 0; - if (IMG_H(h)) { // an image entry: read the image's descriptor at the entry's offset, never past its end + int image = IMG_H(h); size_t got = 0; int reprep = 0, fd = (int)h; int64_t base = 0; + if (image) { // an image entry: read the image's descriptor at the entry's offset, never past its end const img_ent *x = &g_mnt[IMG_M(h)].img->e[IMG_K(h)]; fd = g_mnt[IMG_M(h)].img->fd; base = (int64_t)x->a; sz = off < 0 || (uint64_t)off >= x->b ? 0 : (size_t)(x->b - off) < sz ? (size_t)(x->b - off) : sz; } + int redirected = !image && g_fredirect[(int)h & 0xffff]; + if (redirected && (off < 0 || (uint64_t)off > g_rom_virtual_bytes || + (uint64_t)sz > g_rom_virtual_bytes - (uint64_t)off)) { + LOG("[RED] fs virtual read leaves the archive: offset=%lld length=%#zx virtual-bytes=%#llx", + (long long)off, sz, (unsigned long long)g_rom_virtual_bytes); + *out = 0; errno = EINVAL; return RES_FAIL; + } dm_guest_write_prepare(buf, sz); // The destination may be a texture pool page write-protected by the graphics layer: a kernel write to a read-only page returns EFAULT instead of page-faulting, so unprotect and mark dirty first while (got < sz) { - ssize_t r = pread(fd, (uint8_t *)buf + got, sz - got, base + off + got); + size_t want = sz - got; uint64_t read_offset = (uint64_t)(base + off + (int64_t)got); + if (redirected && !rom_redirect_chunk(read_offset, &want, &read_offset)) { + LOG("[RED] fs virtual redirect table has a gap at %#llx", (unsigned long long)(off + (int64_t)got)); + *out = got; errno = EINVAL; return RES_FAIL; + } + ssize_t r = pread(fd, (uint8_t *)buf + got, want, (off_t)read_offset); if (r < 0) { if (errno == EINTR) continue; // The graphics layer write-protects tracked pages again after each upload (nvn_dirty.c), so a page made writable above can be read-only again by the time pread // writes it: a plain write would fault into the tracker's handler, a system call gets EFAULT. Prepare the rest again (unprotect, mark dirty) and read on. Monster Train 2 @@ -744,7 +862,10 @@ HLE_FORCE("_ZN2nn2fs8ReadFileEPmNS0_10FileHandleElPvm", "fsp-srv → pread(2)", LOG("[RED] fs ReadFile into %p+%#zx failed at +%#zx (EFAULT): VM region %#lx+%#lx protection %d%s", buf, sz, got, (unsigned long)ra, (unsigned long)rs, kr == KERN_SUCCESS ? vi.protection : -1, kr == KERN_SUCCESS ? "" : " (none)"); errno = e; } return err_res("pread", "fd"); } - if (r == 0) break; + if (r == 0) { + if (redirected) { LOG("[RED] fs virtual redirect physical range ended early at %#llx", (unsigned long long)read_offset); *out = got; errno = EIO; return RES_FAIL; } + break; + } got += r; } *out = got; @@ -786,6 +907,7 @@ HLE_FORCE("_ZN2nn2fs9WriteFileENS0_10FileHandleElPKvmRKNS0_11WriteOptionE", "fsp HLE_FORCE("_ZN2nn2fs11GetFileSizeEPlNS0_10FileHandleE", "fsp-srv → fstat(2)", Result, h_fs_GetFileSize, int64_t *out, uint64_t h) { if (!h) return RES_FAIL; if (IMG_H(h)) { *out = (int64_t)g_mnt[IMG_M(h)].img->e[IMG_K(h)].b; return RES_OK; } + if (g_fredirect[(int)h & 0xffff]) { *out = (int64_t)g_rom_virtual_bytes; return RES_OK; } struct stat st; if (fstat((int)h, &st)) return err_res("fstat", "fd"); *out = st.st_size; return RES_OK; } @@ -801,7 +923,8 @@ HLE_FORCE("_ZN2nn2fs11SetFileSizeENS0_10FileHandleEl", "fsp-srv → ftruncate(2) HLE_FORCE("_ZN2nn2fs9FlushFileENS0_10FileHandleE", "fsp-srv; host writes are already in the page cache, flushing is a no-op", Result, h_fs_FlushFile, uint64_t h) { (void)h; return RES_OK; } HLE_FORCE("_ZN2nn2fs9CloseFileENS0_10FileHandleE", "fsp-srv → close(2)", void, h_fs_CloseFile, uint64_t h) { if (!h || IMG_H(h)) return; // the null handle after a failed open (see ReadFile): do not touch fd 0; an image entry holds no descriptor - g_fmode[(int)h & 0xffff] = 0; atomic_store(&g_ftxn[(int)h & 0xffff], 0); + g_fmode[(int)h & 0xffff] = 0; g_fsave[(int)h & 0xffff] = 0; + atomic_store(&g_ftxn[(int)h & 0xffff], 0); g_fredirect[(int)h & 0xffff] = 0; close((int)h); } HLE_FORCE("_ZN2nn2fs15GetFileOpenModeENS0_10FileHandleE", "fsp-srv; the open mode is recorded by the host", int, h_fs_GetFileOpenMode, uint64_t h) { return IMG_H(h) ? MODE_READ : g_fmode[(int)h & 0xffff]; } @@ -889,7 +1012,7 @@ HLE_FORCE("_ZN2nn2fs12GetEntryTypeEPNS0_18DirectoryEntryTypeEPKc", "fsp-srv → } // ---------- Directories ---------- -typedef struct { DIR *d; char path[1024]; DIR *od; char opath[1024]; DirectoryEntry *snap; int64_t nsnap, psnap; int mode; } dirh_t; +typedef struct { DIR *d; char path[1024]; char guest[1024]; DIR *od; char opath[1024]; DirectoryEntry *snap; int64_t nsnap, psnap; int mode; } dirh_t; static void dir_snapshot(dirh_t *h, const mount_t *m, const char *rest) { char *roots = strdup(m->overlay); size_t cap = 2048; char (*names)[0x301] = malloc(cap * sizeof *names); @@ -930,7 +1053,7 @@ static void dir_snapshot(dirh_t *h, const mount_t *m, const char *rest) { strlcpy(h->snap[j].name, names[j], sizeof h->snap[j].name); snprintf(rel, 2048, "%s/%s", rest, names[j]); struct stat st; int64_t k; - if (rom_overlay_path(m, rel, resolved, 1024)) { if (!stat(resolved, &st)) { h->snap[j].type = S_ISDIR(st.st_mode) ? ENT_DIR : ENT_FILE; h->snap[j].size = S_ISDIR(st.st_mode) ? 0 : st.st_size; } } + if (rom_overlay_path(m, rel, resolved, 1024)) { if (!stat(resolved, &st)) { h->snap[j].type = S_ISDIR(st.st_mode) ? ENT_DIR : ENT_FILE; h->snap[j].size = S_ISDIR(st.st_mode) ? 0 : rom_redirect_reported_size(rel, st.st_size); } } else if (m->img && (k = img_find(m->img, rel)) >= 0) { h->snap[j].type = m->img->e[k].type ? ENT_FILE : ENT_DIR; h->snap[j].size = m->img->e[k].type ? (int64_t)m->img->e[k].b : 0; } } free(resolved); free(rel); free(guest); free(parent); free(all); free(names); free(roots); @@ -954,6 +1077,7 @@ HLE_FORCE("_ZN2nn2fs13OpenDirectoryEPNS0_15DirectoryHandleEPKci", "fsp-srv → o if (!d) return err_res("opendir", hp); dirh_t *h = calloc(1, sizeof *h); h->d = d; strlcpy(h->path, hp, sizeof h->path); h->mode = mode; const char *c = strchr(path, ':'); + if (c) strlcpy(h->guest, c + 1, sizeof h->guest); if (c) for (int i = 0; i < g_nmnt; i++) if (g_mnt[i].overlay[0] && strlen(g_mnt[i].name) == (size_t)(c - path) && !strncmp(g_mnt[i].name, path, (size_t)(c - path))) { dir_snapshot(h, &g_mnt[i], c + 1); break; } @@ -973,7 +1097,9 @@ HLE_FORCE("_ZN2nn2fs13ReadDirectoryEPlPNS0_14DirectoryEntryENS0_15DirectoryHandl if (!(h->mode & (isdir ? DIRMODE_DIR : DIRMODE_FILE))) continue; DirectoryEntry *o = &ents[n++]; memset(o, 0, sizeof *o); if (!guest_name(h->path, e->d_name, o->name, sizeof o->name)) { char c[1280]; snprintf(c, sizeof c, "%s/%s", h->path, e->d_name); *count = n - 1; return err_res("directory name", c); } - o->type = isdir ? ENT_DIR : ENT_FILE; o->size = isdir ? 0 : st.st_size; + o->type = isdir ? ENT_DIR : ENT_FILE; + if (isdir) o->size = 0; + else { char relative[1400]; snprintf(relative, sizeof relative, "%s/%s", h->guest, o->name); o->size = rom_redirect_reported_size(relative, st.st_size); } } *count = n; return RES_OK; } diff --git a/dm/host/hle_hid.c b/dm/host/hle_hid.c index 148fdb3c7d..429b057561 100644 --- a/dm/host/hle_hid.c +++ b/dm/host/hle_hid.c @@ -31,6 +31,47 @@ _Static_assert(sizeof(NpadState) == 0x28, "NpadState"); // their own path because a connected controller publishes its whole state on every change (a DualShock 4's sticks change all the time), which overwrote them on the shared one static _Atomic uint64_t g_btn, g_axes, g_latch, g_sc_btn, g_sc_axes, g_sc_latch, g_pad_btn[DM_PLAYERS], g_pad_axes[DM_PLAYERS], g_pad_latch[DM_PLAYERS]; static _Atomic int64_t g_sampling = 1 << 16; // starts high so the history entries below the newest keep positive sampling numbers +// Opt-in observations of the state actually returned to the guest. Keep file +// I/O and clock reads out of the normal polling path, and bound diagnostics. +static pthread_once_t g_receipt_once = PTHREAD_ONCE_INIT; +static pthread_mutex_t g_receipt_mu = PTHREAD_MUTEX_INITIALIZER; +static FILE *g_receipt_file; +static unsigned g_receipt_count; +static struct { NpadState state; uint64_t time; int seen; } g_receipt_last[9][5]; +static void receipt_init(void) { + const char *path = getenv("DM_HID_RECEIPT_LOG"); + if (!path || !*path) return; + g_receipt_file = fopen(path, "w"); + if (!g_receipt_file) LOG("[RED] Cannot open HID receipt log: %s", path); +} +static int receipt_enabled(void) { + pthread_once(&g_receipt_once, receipt_init); + return g_receipt_file != NULL; +} +static void receipt_note(const NpadState *s, uint32_t id, uint32_t style, uint64_t start) { + unsigned row = id < DM_PLAYERS ? id : 8, col = 0; + while (col < 4 && style != (1u << col)) col++; + uint64_t end = now_ns(); + pthread_mutex_lock(&g_receipt_mu); + if (g_receipt_count < 1024) { + NpadState previous = g_receipt_last[row][col].state, current = *s; + previous.sampling = current.sampling = 0; + int changed = !g_receipt_last[row][col].seen || memcmp(&previous, ¤t, sizeof current); + if (changed || end - g_receipt_last[row][col].time >= 1000000000ull) { + fprintf(g_receipt_file, "{\"read_start_ns\":%llu,\"time_ns\":%llu,\"id\":%u,\"style\":%u,\"sampling\":%lld,\"buttons\":%llu,\"axes\":[%d,%d,%d,%d],\"attributes\":%u,\"script_mask\":%llu}\n", + (unsigned long long)start, (unsigned long long)end, id, style, + (long long)s->sampling, (unsigned long long)s->buttons, + s->lx, s->ly, s->rx, s->ry, s->attributes, + (unsigned long long)atomic_load(&g_pad_btn[0])); + g_receipt_last[row][col].state = *s; + g_receipt_last[row][col].time = end; + g_receipt_last[row][col].seen = 1; + if (++g_receipt_count == 1024) fputs("{\"limit_reached\":true}\n", g_receipt_file); + fflush(g_receipt_file); + } + } + pthread_mutex_unlock(&g_receipt_mu); +} static uint64_t pack(int32_t lx, int32_t ly, int32_t rx, int32_t ry) { return (uint64_t)(uint16_t)lx | (uint64_t)(uint16_t)ly << 16 | (uint64_t)(uint16_t)rx << 32 | (uint64_t)(uint16_t)ry << 48; } @@ -129,7 +170,8 @@ void hid_keys_init(void) { // DM_CTL= (Mac, for driving a game step by step from a shell): the host reads the lines appended to the file (polled every 100 ms) and acts on each at once: // "key[+key][~hold]" presses as a DM_KEYS step does (hold 0.15 s by default), "shot " saves the next presented frame as a PNG (dm_gfx_shot_at), "passdump " dumps the // render passes of the next n frames as DM_NVN_PASSDUMP does (to stderr, the run's log), "texdump " saves the colour targets of the next frame as DM_NVN_TEXDUMP does, -// "env KEY=VALUE" sets a switch read at the time of use (DM_NVN_SNAPRAW for the next texdump), "cvarf
" sets an Unreal float console variable +// "env KEY=VALUE" sets a switch read at the time of use (DM_NVN_SNAPRAW for the next texdump), and notifies the live conditional-render / force-visible diagnostics; +// "cvarf
" sets an Unreal float console variable // (hle_fpslock.c dm_ctl_cvar_f32; "cvari" an int one), "peek
" logs that many floats of guest memory, "memdump
" saves guest memory // to a file (a buffer texture's storage, which the pass dump's tbuf lines print, holds what the GPU wrote: buffer textures are views of the pool), "texlist " logs the // registered textures of that target (2 = 3D) with storage address and block-linear layout (nvn_mtl.m dm_gfx_texlist), "texraw " @@ -153,7 +195,24 @@ static void *ctl_main(void *arg) { while (*e3 == ' ') e3++; dm_ctl_memdump(a, n, e3); continue; } if (!strncmp(line, "cvari ", 6)) { void dm_ctl_cvar_i32(uint64_t off, int32_t v); char *e2; uint64_t off = strtoull(line + 6, &e2, 16); dm_ctl_cvar_i32(off, (int32_t)strtol(e2, NULL, 0)); continue; } if (!strncmp(line, "cvarf ", 6)) { void dm_ctl_cvar_f32(uint64_t off, float v); char *e2; uint64_t off = strtoull(line + 6, &e2, 16); dm_ctl_cvar_f32(off, strtof(e2, NULL)); continue; } - if (!strncmp(line, "env ", 4)) { char *eq = strchr(line + 4, '='); if (eq) { *eq = 0; setenv(line + 4, eq + 1, 1); LOG("DM_CTL: env %s=%s", line + 4, eq + 1); } continue; } + if (!strncmp(line, "env ", 4)) { char *eq = strchr(line + 4, '='); if (eq) { + *eq = 0; + int opmode_change = !strcmp(line + 4, "DM_OPMODE"); + if (opmode_change && strcmp(eq + 1, "0") && strcmp(eq + 1, "1")) { + LOG("[RED] DM_CTL: DM_OPMODE must be 0 (handheld) or 1 (console)"); + continue; + } + if (!setenv(line + 4, eq + 1, 1)) { + if (opmode_change) dm_set_opmode(eq[1] - '0'); + void dm_gfx_env_changed(const char *key, const char *value); + dm_gfx_env_changed(line + 4, eq + 1); + void dm_za_timing_env_changed(const char *key, const char *value); + dm_za_timing_env_changed(line + 4, eq + 1); + void dm_audio_env_changed(const char *key, const char *value); + dm_audio_env_changed(line + 4, eq + 1); + LOG("DM_CTL: env %s=%s", line + 4, eq + 1); + } else LOG("[RED] DM_CTL: failed to set env %s", line + 4); + } continue; } if (!strncmp(line, "texdump ", 8)) { void dm_gfx_texdump_next(const char *path); dm_gfx_texdump_next(line + 8); LOG("DM_CTL: colour targets of the next frame → %s (as DM_NVN_TEXDUMP)", line + 8); continue; } if (!strncmp(line, "passdump ", 9)) { void dm_gfx_passdump_next(long n); dm_gfx_passdump_next(atol(line + 9)); LOG("DM_CTL: passes of the next %ld frames (as DM_NVN_PASSDUMP)", atol(line + 9)); continue; } if (!strncmp(line, "drag ", 5)) { // one finger, a step every 8 ms @@ -230,10 +289,15 @@ void dm_pad_players(uint32_t mask) { } static void fill(NpadState *s, uint32_t id, uint32_t style) { + int receipt = receipt_enabled(); + uint64_t read_start = receipt ? now_ns() : 0; memset(s, 0, sizeof *s); s->sampling = atomic_fetch_add(&g_sampling, 1) + 1; int pl = player_of(id); - if (!(style_for_player(id, pl) & style)) return; // no controller of this id / style: attributes = 0 + if (!(style_for_player(id, pl) & style)) { // no controller of this id / style: attributes = 0 + if (receipt) receipt_note(s, id, style, read_start); + return; + } // Player 1: keyboard / touch, script, its controller and the players merged into it; another player: its own controller path uint32_t src = pl ? 1u << pl : 1u | merged_players(); uint64_t b = 0; int32_t a[4] = {0}; @@ -257,6 +321,7 @@ static void fill(NpadState *s, uint32_t id, uint32_t style) { if (s->ry < -STICK_THRESH) b |= 1u << 23; s->buttons = b; s->attributes = ATTR_CONNECTED | (id == NPAD_HANDHELD ? ATTR_WIRED | ATTR_LEFT_CONN | ATTR_LEFT_WIRED | ATTR_RIGHT_CONN | ATTR_RIGHT_WIRED : ATTR_LEFT_CONN | ATTR_RIGHT_CONN); + if (receipt) receipt_note(s, id, style, read_start); } // GetNpadStates(out, n, id): newest first, n entries: the current state repeated with consecutive sampling numbers. The host keeps no history, but on hardware the // 16-entry history is always full, and games read all of it whatever count comes back: Super Mario Odyssey (main+0x93fc20) polls every controller slot each frame, which advances diff --git a/dm/host/hle_oodle.c b/dm/host/hle_oodle.c index db1a2d162e..d3de8fa4a3 100644 --- a/dm/host/hle_oodle.c +++ b/dm/host/hle_oodle.c @@ -3,11 +3,14 @@ // type 3), PlatinumGames' engine the entries of its PKZ archives (Bayonetta 3: OodleKraken, block header 8C 06), and DRAGON QUEST XI S (Unreal Engine 4.18) the files of its paks with LZ5 2.0 // (pak compression method 3; each block starts with the LZ5 level, 0x2d). Shaders are translated offline // only and Oodle's decoder is not open source, so they are decoded on the Mac with the copy statically linked into main (LZ5 the same way, so the tools need no decoder of their own), in one -// of two modes: +// of three modes: // DM_OODLE_JOB= runs the decoder on every record right before nnMain, writes .out and exits (the NSP script, Unreal); // DM_OODLE_SERVE=1 answers requests on stdin / stdout from the guest main thread before the SDK starts, until stdin ends (shader_split.py's workers, Trinity: the packs are decoded file by file // while they are scanned, about 4.7 GB, so they are never written out). Request: u64 compressed length, u64 decompressed length, the compressed bytes; answer: u64 bytes decoded (0 on failure), // then those bytes. +// DM_OODLE_COMPRESS_JOB= runs OodleLZ_Compress after the SDK and module constructors have initialized the game's allocator, verifies an exact decode round trip, writes the raw +// compressed stream to .out and exits. Its intentionally small, single-record format is "DMCJ", u32 compressor, u32 level, u32 raw length, then the raw bytes. It is used only by +// offline, same-slot resource tools; the original archive stays read-only and the caller still has to reject a stream larger than its existing FlatBuffer vector. // Job file: "DMOJ", u32 count, then per record u32 compressed length, u32 raw length and the compressed bytes. Output: "DMOR", u32 count, then per record u32 decoded length // (0 = failed) and the decoded bytes. #include "hle.h" @@ -32,15 +35,13 @@ static uint64_t func_start(const module_t *m, uint64_t off) { // start of the for (uint64_t i = 0; i < n; i++) { uint64_t s = eh_read(b, &o, tenc, ehs); eh_read(b, &o, tenc, ehs); if (s <= off && s > best) best = s; } return best; } -static uint64_t func_next(const module_t *m, uint64_t off) { // start of the function after the one holding off (the text end when there is none) +static uint64_t func_next(const module_t *m, uint64_t off) { const uint8_t *b = m->base; uint32_t m0; int32_t eh; memcpy(&m0, b + 4, 4); memcpy(&eh, b + m0 + 16, 4); uint64_t ehs = m0 + (int64_t)eh, o = ehs + 4, best = m->text_end; int penc = b[ehs + 1], cenc = b[ehs + 2], tenc = b[ehs + 3]; eh_read(b, &o, penc, ehs); uint64_t n = eh_read(b, &o, cenc, ehs); for (uint64_t i = 0; i < n; i++) { uint64_t s = eh_read(b, &o, tenc, ehs); eh_read(b, &o, tenc, ehs); if (s > off && s < best) best = s; } return best; } -// The functions whose code takes the address of a NUL-terminated string of main's read-only data (adrp + add) that holds needle: with whole, the string is needle; else the needle is anywhere -// in it, and the address taken is the string's start. Up to max distinct functions into fns, in address order; returns how many static int str_fns(const module_t *m, const char *needle, int whole, uint64_t *fns, int max) { const uint8_t *b = m->base, *ro = b + m->ro_off, *re = b + m->data_off; size_t len = strlen(needle); uint64_t strs[16]; int ns = 0, nf = 0; @@ -50,12 +51,12 @@ static int str_fns(const module_t *m, const char *needle, int whole, uint64_t *f } for (uint64_t pc = 0; ns && pc + 4 <= m->text_end; pc += 4) { uint32_t w; memcpy(&w, b + pc, 4); - if ((w & 0x9F000000) != 0x90000000) continue; // adrp xd, page + if ((w & 0x9F000000) != 0x90000000) continue; int64_t imm = (int64_t)(((w >> 29) & 3) | (((w >> 5) & 0x7FFFF) << 2)); if (imm & (1 << 20)) imm -= 1 << 21; uint64_t page = (pc & ~0xFFFull) + (uint64_t)(imm << 12); - for (int k = 1; k < 8 && pc + 4 * k + 4 <= m->text_end; k++) { // the add that completes it, within a few instructions + for (int k = 1; k < 8 && pc + 4 * k + 4 <= m->text_end; k++) { uint32_t w2; memcpy(&w2, b + pc + 4 * k, 4); - if ((w2 & 0xFF800000) != 0x91000000 || ((w2 >> 5) & 31) != (w & 31)) continue; // add xd, xn = xd of the adrp, #imm + if ((w2 & 0xFF800000) != 0x91000000 || ((w2 >> 5) & 31) != (w & 31)) continue; uint64_t a = page + ((w2 >> 10) & 0xFFF); for (int s = 0; s < ns; s++) if (a == strs[s]) { uint64_t f = func_start(m, pc + 4 * k); int seen = 0; @@ -68,36 +69,33 @@ static int str_fns(const module_t *m, const char *needle, int whole, uint64_t *f for (int i = 1; i < nf; i++) for (int j = i; j > 0 && fns[j - 1] > fns[j]; j--) { uint64_t t = fns[j]; fns[j] = fns[j - 1]; fns[j - 1] = t; } return nf; } -static uint64_t oodle_find(const module_t *m) { // 0: main has no "OodleLZ_Decompress" name string +static uint64_t oodle_named_find(const module_t *m, const char *name) { + uint64_t fns[4]; int n = str_fns(m, name, 1, fns, 4); + if (n != 1) die("DM_OODLE_JOB: %d functions reference \"%s\" in main (expected exactly 1)", n, name); + return fns[0]; +} +static uint64_t oodle_find(const module_t *m) { uint64_t fns[4]; int nfn = str_fns(m, "OodleLZ_Decompress", 1, fns, 4); if (nfn > 1) die("DM_OODLE_JOB: %d functions reference \"OodleLZ_Decompress\" in main (expected exactly 1)", nfn); return nfn ? fns[0] : 0; } -// Oodle's streaming decoder, for a main that has no "OodleLZ_Decompress" name (Bayonetta 3 1.2.0, PlatinumGames: it reads its PKZ archives only through the streaming API, main+0xcbc698 / -// +0xcbc7f8, so OodleLZ_Decompress was never linked in). The four functions come from Oodle's oodlelzcompressors.cpp, whose path two of them pass to its error handler with a line number: -// OodleLZDecoder_MemorySizeNeeded, the one of the two that also takes the message "OODLE ERROR : OodleLZ_Decompress: asked to decode Legacy compressor", and OodleLZDecoder_DecodeSome, -// the other; between them lie OodleLZDecoder_Create (right after the first) and OodleLZDecoder_Destroy (right before the second), in the library's own order (main+0x12fefd0, +0x12ff110, -// +0x12ff2d0, +0x12ff2f0 there). Checked as far as the code allows: exactly two functions reference the path, the legacy message is in the first, and exactly two functions lie between them typedef int32_t (*lzd_size_fn)(int32_t compressor, int64_t raw_len); typedef void *(*lzd_create_fn)(int32_t compressor, int64_t raw_len, void *mem, int64_t mem_size); typedef void (*lzd_destroy_fn)(void *dec); -// OodleLZDecoder_DecodeSome(decoder, out, decBuf, decBufPos, decBufferSize, decBufAvail, compPtr, compAvail, fuzzSafe, checkCRC, verbosity, threadPhase): arguments 9-12 on the stack, 64-bit -// here so the Apple and AAPCS64 stack layouts agree; out = {s32 decoded, s32 compressed bytes used, s32 current quantum's raw length, s32 its compressed length} typedef int32_t (*lzd_some_fn)(void *dec, int32_t *out, void *buf, int64_t pos, int64_t size, int64_t avail, const void *comp, int64_t comp_avail, int64_t fuzz, int64_t crc, int64_t verb, int64_t phase); static struct { lzd_size_fn size; lzd_create_fn create; lzd_destroy_fn destroy; lzd_some_fn some; } g_lzd; static uint64_t lzd_find(const module_t *m) { uint64_t src[4], leg[4]; int ns = str_fns(m, "oodlelzcompressors.cpp", 0, src, 4), nl = str_fns(m, "OODLE ERROR : OodleLZ_Decompress: asked to decode Legacy compressor", 0, leg, 4); if (ns != 2 || nl != 1 || leg[0] != src[0]) - die("DM_OODLE_JOB: main has no \"OodleLZ_Decompress\" name and no streaming decoder to find (%d functions reference oodlelzcompressors.cpp, %d the legacy compressor message)", ns, nl); + die("DM_OODLE_JOB: main has no Oodle decoder entry point to find"); uint64_t create = func_next(m, src[0]), destroy = func_next(m, create); - if (func_next(m, destroy) != src[1]) die("DM_OODLE_JOB: Oodle's streaming decoder at main+%#llx / +%#llx: not two functions between them", (unsigned long long)src[0], (unsigned long long)src[1]); + if (func_next(m, destroy) != src[1]) die("DM_OODLE_JOB: Oodle streaming decoder layout is not recognized"); const uint8_t *b = m->base; - g_lzd.size = (lzd_size_fn)(void *)(b + src[0]); g_lzd.create = (lzd_create_fn)(void *)(b + create); g_lzd.destroy = (lzd_destroy_fn)(void *)(b + destroy); g_lzd.some = (lzd_some_fn)(void *)(b + src[1]); + g_lzd.size = (lzd_size_fn)(void *)(b + src[0]); g_lzd.create = (lzd_create_fn)(void *)(b + create); + g_lzd.destroy = (lzd_destroy_fn)(void *)(b + destroy); g_lzd.some = (lzd_some_fn)(void *)(b + src[1]); return src[1]; } -// The compressor an Oodle stream names in its first block header (byte 0: 0x8C or 0xCC; byte 1 low 7 bits: the decoder: 6 Kraken, 10 Mermaid / Selkie, 12 Leviathan) as the API's -// OodleLZ_Compressor (Kraken 8, Mermaid 9, Leviathan 13), which Create sizes its memory for; -1 when it is none of them static int32_t lzd_compressor(const uint8_t *c, int64_t cl) { if (cl < 2 || (c[0] & 0x0F) != 0x0C) return -1; switch (c[1] & 0x7F) { case 6: return 8; case 10: return 9; case 12: return 13; default: return -1; } @@ -107,12 +105,12 @@ static int64_t lzd_run(const void *c, int64_t cl, void *r, int64_t rl, void *scr int64_t need = g_lzd.size(comp, rl); void *mem = scratch, *own = NULL; if (need <= 0) return -1; if (need + 64 > scratch_n) { if (!(own = malloc((size_t)need + 64))) return -1; mem = own; } - void *dec = g_lzd.create(comp, rl, mem, need + 64); // Create keeps 16 bytes for its own alignment + void *dec = g_lzd.create(comp, rl, mem, need + 64); if (!dec) { free(own); return -1; } int64_t pos = 0, cpos = 0; while (pos < rl) { int32_t out[4] = {0}; - if (!g_lzd.some(dec, out, r, pos, rl, rl - pos, (const uint8_t *)c + cpos, cl - cpos, 1, 0, 0, 3) || (out[0] <= 0 && out[1] <= 0)) break; // fuzz safe, no CRC check, silent, unthreaded + if (!g_lzd.some(dec, out, r, pos, rl, rl - pos, (const uint8_t *)c + cpos, cl - cpos, 1, 0, 0, 3) || (out[0] <= 0 && out[1] <= 0)) break; pos += out[0]; cpos += out[1]; } g_lzd.destroy(dec); free(own); @@ -173,6 +171,47 @@ void dm_oodle_job_maybe(void) { fflush(stderr); _exit(bad ? 1 : 0); } +// OodleLZ_Compress(compressor, rawBuf, rawLen, compBuf, level, options, dictionaryBase, lrm, scratchMem, scratchSize). +// Keep every scalar and every stack argument 64-bit so the Apple and AAPCS64 call layouts agree, as with oodle_fn above. This ABI and the scratch size are also independently checked by an +// exact encoder -> game decoder round trip before any stream is published. +typedef int64_t (*oodle_compress_fn)(int64_t, const void *, int64_t, void *, int64_t, const void *, const void *, const void *, void *, int64_t); + +static void oodle_compress_job(void) { + const char *job = getenv("DM_OODLE_COMPRESS_JOB"); + if (!job || !*job) return; + dec_find("DM_OODLE_COMPRESS_JOB"); + if (!g_dec.oodle) die("DM_OODLE_COMPRESS_JOB: game has no Oodle decoder"); + module_t *main = NULL; + for (int i = 0; i < g_nmod; i++) if (!strcmp(g_mod[i].name, "main")) main = &g_mod[i]; + if (!main) die("DM_OODLE_COMPRESS_JOB: no main module"); + uint64_t off = oodle_named_find(main, "OodleLZ_Compress"); + oodle_compress_fn encode = (oodle_compress_fn)(void *)(main->base + off); + + FILE *in = fopen(job, "rb"); if (!in) die("DM_OODLE_COMPRESS_JOB: input unavailable"); + if (rd32(in) != 0x4a434d44) die("DM_OODLE_COMPRESS_JOB: invalid header"); + uint32_t compressor = rd32(in), level = rd32(in), n = rd32(in); + // The old, audited harness deliberately bounded one request to 64 MiB. Keep that bound: derived resource files larger than it must be skipped instead of relying on unbounded guest memory. + if (!n || n > (64u << 20) || level >= 10) die("DM_OODLE_COMPRESS_JOB: invalid request"); + size_t comp_n = (size_t)n * 2 + 0x10000, scratch_n = 128u << 20; + uint8_t *raw = malloc(n), *comp = malloc(comp_n), *decoded = malloc((size_t)n + 0x10000), *scratch = malloc(scratch_n); + if (!raw || !comp || !decoded || !scratch) die("DM_OODLE_COMPRESS_JOB: buffers unavailable"); + if (fread(raw, 1, n, in) != n || fgetc(in) != EOF) die("DM_OODLE_COMPRESS_JOB: invalid input length"); + fclose(in); + + LOG("DM_OODLE_COMPRESS_JOB: encoder main+%#llx compressor=%u level=%u bytes=%u", (unsigned long long)off, compressor, level, n); + int64_t got = encode(compressor, raw, n, comp, level, NULL, NULL, NULL, scratch, (int64_t)scratch_n); + if (got <= 0 || (uint64_t)got > comp_n) die("DM_OODLE_COMPRESS_JOB: encoder returned %lld", (long long)got); + int64_t roundtrip = dec_run(comp, got, decoded, n, scratch, (int64_t)scratch_n); + if (roundtrip != n || memcmp(raw, decoded, n)) die("DM_OODLE_COMPRESS_JOB: round-trip failed"); + + char outp[1024]; snprintf(outp, sizeof outp, "%s.out", job); + FILE *out = fopen(outp, "wb"); if (!out) die("DM_OODLE_COMPRESS_JOB: output unavailable"); + if (fwrite(comp, 1, (size_t)got, out) != (size_t)got || fclose(out)) die("DM_OODLE_COMPRESS_JOB: output failed"); + LOG("DM_OODLE_COMPRESS_JOB: %u -> %lld bytes; exact decoder round-trip passed", n, (long long)got); + free(raw); free(comp); free(decoded); free(scratch); + fflush(stderr); _exit(0); +} + static int read_all(int fd, void *p, size_t n) { for (uint8_t *q = p; n; ) { ssize_t k = read(fd, q, n); if (k <= 0) return 0; q += k; n -= (size_t)k; } return 1; @@ -184,6 +223,7 @@ static void write_all(int fd, const void *p, size_t n) { // host. LZ5 at the second: LZ5_decompress_safe takes 512 KB of work memory from the game's malloc on every call, a StandardAllocator that nninitStartup sets up (DRAGON QUEST XI S main+0x4530440), // and returns -1 before that void dm_oodle_serve_maybe(int late) { + if (late) oodle_compress_job(); if (!getenv("DM_OODLE_SERVE")) return; dec_find("DM_OODLE_SERVE"); if (late != (g_dec.lz5 != NULL)) return; diff --git a/dm/host/hle_svc.c b/dm/host/hle_svc.c index 3ded3ab3a0..f6d3fd6b01 100644 --- a/dm/host/hle_svc.c +++ b/dm/host/hle_svc.c @@ -1,6 +1,11 @@ // Horizon kernel system calls (triggered by the SDK runtime once the SDK is loaded). Only what the P1 boot chain needs is implemented: memory layout + heap + query + debug output + timing. // Register convention: inputs w1.. / outputs w0=Result, x1=value (GetInfo / SetHeapSize), consistent with the public Horizon ABI. #include "dm.h" +#include "map_headroom.h" +#include +#if TARGET_OS_IPHONE +#include +#endif #include #include #include @@ -251,6 +256,29 @@ static void map_source_query(uint64_t addr, uint64_t *base, uint64_t *size, uint *base = lo; *size = remaining; } +#if TARGET_OS_IPHONE +static pthread_once_t g_map_headroom_once = PTHREAD_ONCE_INIT; +static uint64_t g_map_headroom; +static void map_headroom_init(void) { + g_map_headroom = dm_map_headroom_default_bytes(TARGET_OS_TV, TARGET_OS_SIMULATOR); + const char *text = getenv("DM_MAP_HEADROOM_MB"); + if (!text || !*text) return; + errno = 0; char *end = NULL; unsigned long long mb = strtoull(text, &end, 10); + if (errno || end == text || *end || mb > (UINT64_MAX >> 20)) { + LOG("[RED] invalid DM_MAP_HEADROOM_MB=%s; using %llu MB", text, + (unsigned long long)(g_map_headroom >> 20)); + return; + } + g_map_headroom = (uint64_t)mb << 20; +} +static uint64_t map_headroom_bytes(void) { + pthread_once(&g_map_headroom_once, map_headroom_init); + return g_map_headroom; +} +#else +static uint64_t map_headroom_bytes(void) { return 0; } +#endif + // Diagnostic DM_ALIAS_WINDOWS=: every 30 s, the mapped memory of each window of that size in the alias region (windows aligned to the size in absolute addresses, counted from the page // bitmap; windows with nothing mapped are left out). For sizing an allocator that reserves fixed, aligned windows and commits pages from each window's start: Unreal's FMallocBinned3 takes // 74 pools of 512 MB (Octopath Traveler 0, main+0x9f748) @@ -562,12 +590,27 @@ uint64_t svc_dispatch(int num, uint64_t *regs, uint64_t orig_addr) { // and maps new ones for later allocations, until a map fails: then it unmaps the cached block and retries (measured on the Mac: the refused 4 MB map at +0x43e2c000 was followed by // UnmapPhysicalMemory of the 3.15 GB block and the same map again). Without the limit the mapped total went past the pool in the first minute and the freed block stayed mapped. // Off by default: Ace Attorney Investigations Collection maps a little more than the 1 GB pool its profile reports - if (DM_GAME_MEM_MAP_LIMIT && num == 0x2C) { - int64_t room = (int64_t)APP_POOL - (int64_t)(g_heap_cur + DM_GAME_MEM_USED + big_bss_bytes()) - atomic_load(&g_alias_mapped); - if (need && need > room) { + uint64_t headroom = num == 0x2C ? map_headroom_bytes() : 0; + int guard_map = num == 0x2C && (DM_GAME_MEM_MAP_LIMIT || headroom); + if (guard_map) { + int64_t room = (int64_t)APP_POOL - (int64_t)(g_heap_cur + DM_GAME_MEM_USED + big_bss_bytes()) - atomic_load(&g_alias_mapped); // the pool less the used memory GetInfo reports + int pool_full = DM_GAME_MEM_MAP_LIMIT && need > room; + uint64_t available = 0; int process_full = 0; +#if TARGET_OS_IPHONE + if (!pool_full && headroom && need > 0) { + available = os_proc_available_memory(); + process_full = dm_map_headroom_should_refuse(available, (uint64_t)need, headroom); + } +#endif + if (pool_full || process_full) { pthread_mutex_unlock(&g_map_lock); - static _Atomic int nref; if (atomic_fetch_add(&nref, 1) < 8) LOG("[mem] MapPhysicalMemory +%#zx refused: %.2f GB mapped, the application pool is full (ResultLimitReached, the SDK frees cached pages and retries)", - size, atomic_load(&g_alias_mapped) / 1073741824.0); + if (pool_full) { + static _Atomic int nref; if (atomic_fetch_add(&nref, 1) < 8) LOG("[mem] MapPhysicalMemory +%#zx refused: %.2f GB mapped, the application pool is full (ResultLimitReached, the SDK frees cached pages and retries)", + size, atomic_load(&g_alias_mapped) / 1073741824.0); + } else { + static _Atomic int npressure; if (atomic_fetch_add(&npressure, 1) < 8) LOG("[mem] MapPhysicalMemory +%#zx refused under process pressure: %llu MB available, reserving %llu MB (ResultLimitReached; the SDK may discard cached pages and retry)", + size, (unsigned long long)(available >> 20), (unsigned long long)(headroom >> 20)); + } regs[0] = 0x10801; return 0; } } @@ -582,8 +625,10 @@ uint64_t svc_dispatch(int num, uint64_t *regs, uint64_t orig_addr) { if ((mhi - hi) / 4096 != (uint64_t)alias_unmapped(hi - (uintptr_t)g_alias, mhi - hi)) mhi -= pg; } if (changed && mhi > mlo) { - if (mprotect((void *)mlo, mhi - mlo, commit ? PROT_READ | PROT_WRITE : PROT_NONE)) + if (mprotect((void *)mlo, mhi - mlo, commit ? PROT_READ | PROT_WRITE : PROT_NONE)) { + pthread_mutex_unlock(&g_map_lock); die("SVC %#x mprotect %p+%#zx errno=%d", num, (void *)a, size, errno); + } if (commit) dm_guest_write_prepare((void *)mlo, mhi - mlo); else (void)madvise((void *)mlo, mhi - mlo, MADV_FREE); } diff --git a/dm/host/hle_za_timing.c b/dm/host/hle_za_timing.c new file mode 100644 index 0000000000..57c243e2f4 --- /dev/null +++ b/dm/host/hle_za_timing.c @@ -0,0 +1,364 @@ +// Trinity timing on Pokemon Legends: Z-A 2.0.2. All addresses and original +// instructions are asserted offline by this build's OBS table in patches.py. +// Default: callbacks do nothing. DM_ZA_TIMING=1 enables clock diagnostics only. +// DM_ZA_FPS_MODE=static60 reproduces the original author's single-instruction +// experiment at main+0x1b296d8, without modifying guest instructions at runtime. +// DM_ZA_FPS_MODE=adaptive60 additionally selects the controller's measured code +// even when the native adaptive flag is off. This is an experiment, not a claim +// of correct speed: the base-clock/wall ratio below must establish that first. +// dynamic60 uses whole-frame active guest time and two verified duration data +// slots. It retains the native channel pause/zero handling and never edits code. +// Use DM_PRESENT_INTERVAL=game. Raising display frequency alone is insufficient. +// Reference: Fl4sh9174/Switch-Emulator-Ultrawide-FPS-Mods, this game's v2.0.2 pack. +#include "hle.h" +#include +#include +#include +#include +#include "za_frame_delta.h" + +enum { ZA_GAME, ZA_STATIC60, ZA_ADAPTIVE60, ZA_DYNAMIC60 }; +static int g_za_diag, g_za_valid; +static _Atomic int g_za_mode, g_za_live_allowed; +static _Atomic uint64_t g_za_epoch; +static pthread_once_t g_za_once = PTHREAD_ONCE_INIT; +static pthread_mutex_t g_za_mu = PTHREAD_MUTEX_INITIALIZER; +static int g_za_interval_seen, g_za_warmup; +static za_frame_delta_state g_za_frame; +static za_frame_delta_result g_za_step; +static uint64_t g_za_exception_logs; +static uint64_t *g_za_table, g_za_table_owned[2]; +static const uint64_t k_za_duration[5] = {16666667, 33333334, 50000000, 66666667, 83333334}; +#define ZA_DURATION_OFFSET 0x3393e20ull +static struct { + uint64_t calls, start_wall, start_guest, sum_ns, invalid, zero; + uint64_t min_ns, max_ns, interval_calls, codes[6]; + uint64_t report_wall, report_guest, report_sum; + int64_t last_delta, last_elapsed; + int configured, adaptive, forwarded, selected; +} g_za; + +// The original signed comparisons at main+0x1b29684..0x1b296d0. These codes +// are the native controller's values, not a guessed seconds-per-frame formula. +static int za_elapsed_code(int64_t ns) { + if (ns > 66666667) return 4; + if (ns > 50000000) return 3; + if (ns > 33333334) return 2; + return ns > 16666667; +} +static int za_read(uint64_t at, void *dst, size_t len) { + vm_size_t got = 0; + return at >= 0x1000 && vm_read_overwrite(mach_task_self(), (vm_address_t)at, + len, (vm_address_t)dst, &got) == KERN_SUCCESS && got == len; +} +static const char *za_mode_name(int mode) { + return mode == ZA_STATIC60 ? "static60" : mode == ZA_ADAPTIVE60 ? "adaptive60" : mode == ZA_DYNAMIC60 ? "dynamic60" : "game"; +} +// The loader intentionally maps NSO ro/data as RW, with text separately RX. +// Never change a mapping's protection to make this optional experiment work. +static int za_table_rw_nonexec(const void *p, size_t len) { + vm_address_t target = (vm_address_t)p, at = target; + vm_size_t size = 0; natural_t depth = 0; + for (int i = 0; i < 8; i++) { + vm_region_submap_info_data_64_t info; + mach_msg_type_number_t count = VM_REGION_SUBMAP_INFO_COUNT_64; + if (vm_region_recurse_64(mach_task_self(), &at, &size, &depth, + (vm_region_recurse_info_t)&info, &count) != KERN_SUCCESS) return 0; + if (info.is_submap) { depth++; continue; } + return at <= target && size >= len && target - at <= size - len + && (info.protection & (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)) == (VM_PROT_READ | VM_PROT_WRITE); + } + return 0; +} +// All table writers hold g_za_mu. Aligned atomic words also prevent tearing for +// original guest LDR readers. No code/cave page or third duration slot is used. +static int za_table_arm(void) { + module_t *m = NULL; + for (int i = 0; i < g_nmod; i++) if (!strcmp(g_mod[i].name, "main")) m = &g_mod[i]; + const size_t bytes = sizeof k_za_duration; + if (!m || !m->base || strcmp(m->build_id, "b1f12fd919eae86ab8a978317677e64b") + || m->text_end != 0x3163f70 || m->ro_off != 0x3164000 || m->data_off != 0x3bc2000 + || m->size < ZA_DURATION_OFFSET + bytes || ZA_DURATION_OFFSET < m->ro_off + || ZA_DURATION_OFFSET + bytes > m->data_off || dm_detclock_enabled()) { + LOG("[RED] Z-A dynamic60: module layout/build or active-clock prerequisite failed; table unchanged"); return 0; + } + uint64_t *p = (uint64_t *)(m->base + ZA_DURATION_OFFSET), original[5]; + if (((uintptr_t)p & 7) || !za_table_rw_nonexec(p, bytes) + || !za_read((uint64_t)(uintptr_t)p, original, bytes) || memcmp(original, k_za_duration, bytes)) { + LOG("[RED] Z-A dynamic60: duration table is not original aligned RW/non-X data; table unchanged"); return 0; + } + g_za_table = p; memcpy(g_za_table_owned, k_za_duration, sizeof g_za_table_owned); + za_frame_delta_reset(&g_za_frame); memset(&g_za_step, 0, sizeof g_za_step); + g_za_exception_logs = 0; + LOG("Z-A dynamic60: verified main+%#llx duration data; graphics code=0, clock code=0/1; active frame range=(0,50] ms, phase bound=2 ms; long active stalls are explicitly limited", + ZA_DURATION_OFFSET); + return 1; +} +static void za_table_restore(void) { + if (!g_za_table) return; + int conflicts = 0; + // Restore only values still owned by us. Never overwrite another writer. + // Restoring slot 0 first preserves the native table's monotonic order. + for (int i = 0; i < 2; i++) { + uint64_t expected = g_za_table_owned[i]; + if (!__atomic_compare_exchange_n(&g_za_table[i], &expected, k_za_duration[i], 0, + __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) conflicts++; + } + LOG("Z-A dynamic60: duration ownership released; restore_conflicts=%d", conflicts); + g_za_table = NULL; +} +static int za_table_publish(uint64_t step, int code) { + if (!g_za_table || code < 0 || code > 1 || !step || step > ZA_FRAME_MAX_NS) return 0; + uint64_t desired[2] = {k_za_duration[0], k_za_duration[1]}; desired[code] = step; + if (desired[0] > desired[1] || desired[1] > k_za_duration[2]) return 0; + for (int i = 0; i < 5; i++) { + uint64_t expected = i < 2 ? g_za_table_owned[i] : k_za_duration[i]; + if (__atomic_load_n(&g_za_table[i], __ATOMIC_ACQUIRE) != expected) return 0; + } + // The two aligned stores need not be transactional, but intermediate values + // must remain ordered for original ratio/quality callers on other threads. + int first = desired[0] > g_za_table_owned[1] ? 1 : 0; + for (int n = 0; n < 2; n++) { + int i = n ? 1 - first : first; + uint64_t expected = g_za_table_owned[i]; + if (!__atomic_compare_exchange_n(&g_za_table[i], &expected, desired[i], 0, + __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) return 0; + g_za_table_owned[i] = desired[i]; + } + return 1; +} +// Caller holds g_za_mu. The first new-mode interval must run before we discard +// two clock samples: one old-mode frame may already be in flight at the boundary. +// Reissuing the current mode also resets the epoch, useful after a pause/loading +// screen without hiding stalls automatically from the timing measurements. +static int za_reset_locked(int mode, const char *why) { + int old = atomic_load_explicit(&g_za_mode, memory_order_acquire); + if (mode == ZA_DYNAMIC60 && old != ZA_DYNAMIC60 && !za_table_arm()) return 0; + if (old == ZA_DYNAMIC60 && mode != ZA_DYNAMIC60) za_table_restore(); + // A diagnostic epoch in the same dynamic mode must not reseed the actual + // game controller. Dropping two observed samples cannot change engine dt. + memset(&g_za, 0, sizeof g_za); g_za.min_ns = UINT64_MAX; + g_za_interval_seen = 0; g_za_warmup = 2; + atomic_store_explicit(&g_za_mode, mode, memory_order_release); + uint64_t epoch = atomic_fetch_add_explicit(&g_za_epoch, 1, memory_order_acq_rel) + 1; + LOG("Z-A timing: epoch=%llu mode=%s boundary=%s; counters reset, awaiting new-mode interval and two clock samples", + (unsigned long long)epoch, za_mode_name(mode), why); + return 1; +} +static void za_init(void) { + const char *trace = getenv("DM_ZA_TIMING"), *mode = getenv("DM_ZA_FPS_MODE"); + int live_optin = trace && !strcmp(trace, "1"); + g_za_diag = trace && strcmp(trace, "0"); + int selected_mode = ZA_GAME; + if (mode && *mode && strcmp(mode, "game")) { + if (!strcmp(mode, "static60")) selected_mode = ZA_STATIC60; + else if (!strcmp(mode, "adaptive60")) selected_mode = ZA_ADAPTIVE60; + else if (!strcmp(mode, "dynamic60")) selected_mode = ZA_DYNAMIC60; + else LOG("[RED] DM_ZA_FPS_MODE=%s: expected game, static60, adaptive60 or dynamic60; leaving timing unchanged", mode); + } + if (!g_za_diag && !selected_mode) return; + if (strcmp(dm_main_build(), "b1f12fd919eae86ab8a978317677e64b")) { + LOG("[RED] Z-A timing: unsupported main build; diagnostics and experiment disabled"); + return; + } + if (!gate_obs_present(OBS_ZA_CLOCK_DELTA) || !gate_obs_present(OBS_ZA_INTERVAL)) { + LOG("[RED] Z-A timing: rewritten main lacks the required clock/interval observation gates"); + return; + } + g_za_valid = 1; + pthread_mutex_lock(&g_za_mu); + if (!za_reset_locked(selected_mode, "startup")) za_reset_locked(ZA_GAME, "dynamic prerequisites rejected"); + selected_mode = atomic_load(&g_za_mode); + pthread_mutex_unlock(&g_za_mu); + // Published only after the startup opt-in, exact build and both gates passed. + // A DM_CTL change never initializes or arms timing on a previously off run. + atomic_store_explicit(&g_za_live_allowed, live_optin, memory_order_release); + LOG("Z-A timing: mode=%s, base-clock diagnostics enabled; original instructions are retained, no runtime code patch; live_switch=%d", za_mode_name(selected_mode), live_optin); +} + +void dm_za_timing_env_changed(const char *key, const char *value) { + if (!key || strcmp(key, "DM_ZA_FPS_MODE")) return; + if (!atomic_load_explicit(&g_za_live_allowed, memory_order_acquire)) { + LOG("Z-A timing: live mode change ignored; startup DM_ZA_TIMING=1 and validated gates are required"); + return; + } + int mode; + if (value && !strcmp(value, "game")) mode = ZA_GAME; + else if (value && !strcmp(value, "static60")) mode = ZA_STATIC60; + else if (value && !strcmp(value, "adaptive60")) mode = ZA_ADAPTIVE60; + else if (value && !strcmp(value, "dynamic60")) mode = ZA_DYNAMIC60; + else { LOG("[RED] Z-A timing: invalid live mode %s; current epoch unchanged", value ?: "(null)"); return; } + pthread_mutex_lock(&g_za_mu); + za_reset_locked(mode, "DM_CTL"); + pthread_mutex_unlock(&g_za_mu); +} + +// main+0x1b292dc: x0 is the returned clock record. The original ldr d0,[x0,#8] +// follows this callback, then delta_ns / 1e9 is passed to the engine as a float. +static void za_clock_delta(uint64_t *regs, int param) { + (void)param; + pthread_once(&g_za_once, za_init); + if (!g_za_valid) return; + uint64_t epoch = atomic_load_explicit(&g_za_epoch, memory_order_acquire); + int64_t delta = 0; + if (!za_read(regs[0] + 8, &delta, sizeof delta)) { + static unsigned failures; + if (failures++ < 8) LOG("[RED] Z-A timing: unreadable base-clock record %p", (void *)(uintptr_t)regs[0]); + return; + } + uint64_t wall = now_ns(), guest = guest_ns(); + pthread_mutex_lock(&g_za_mu); + if (epoch != atomic_load_explicit(&g_za_epoch, memory_order_acquire) || !g_za_interval_seen) { pthread_mutex_unlock(&g_za_mu); return; } + int mode = atomic_load_explicit(&g_za_mode, memory_order_acquire); + if (g_za_warmup) { + if (!--g_za_warmup) LOG("Z-A timing: epoch=%llu mode=%s ready; next clock sample starts a fresh measurement", + (unsigned long long)epoch, za_mode_name(mode)); + pthread_mutex_unlock(&g_za_mu); return; + } + uint64_t call = ++g_za.calls; g_za.last_delta = delta; + if (!g_za.start_wall) { g_za.start_wall = wall; g_za.start_guest = guest; } + else if (delta >= 0 && delta <= 1000000000ll) { + g_za.sum_ns += (uint64_t)delta; + if (!delta) g_za.zero++; + if ((uint64_t)delta < g_za.min_ns) g_za.min_ns = (uint64_t)delta; + if ((uint64_t)delta > g_za.max_ns) g_za.max_ns = (uint64_t)delta; + } else g_za.invalid++; + if (call <= 8 || !(call % 300)) { + double real = (wall - g_za.start_wall) / 1e9, active = (guest - g_za.start_guest) / 1e9; + double window = g_za.report_wall ? (wall - g_za.report_wall) / 1e9 : 0; + double window_active = g_za.report_guest ? (guest - g_za.report_guest) / 1e9 : 0; + double stepped = (g_za.sum_ns - g_za.report_sum) / 1e9; + LOG("Z-A timing: epoch=%llu mode=%s frames=%llu delta=%.6f ms sum=%.6f s wall=%.6f s active=%.6f s ratio_wall=%.6f ratio_active=%.6f window=%.6f s window_ratio_wall=%.6f window_ratio_active=%.6f min/max=%.6f/%.6f ms zero=%llu invalid=%llu interval_calls=%llu configured=%d native_adaptive=%d elapsed=%.6f ms forwarded=%d selected=%d codes[0..4/other]=%llu/%llu/%llu/%llu/%llu/%llu", + (unsigned long long)epoch, za_mode_name(mode), (unsigned long long)call, delta / 1e6, g_za.sum_ns / 1e9, + real, active, real > 0 ? (g_za.sum_ns / 1e9) / real : 0, + active > 0 ? (g_za.sum_ns / 1e9) / active : 0, + window, window > 0 ? stepped / window : 0, + window_active > 0 ? stepped / window_active : 0, + g_za.min_ns == UINT64_MAX ? 0 : g_za.min_ns / 1e6, g_za.max_ns / 1e6, + (unsigned long long)g_za.zero, (unsigned long long)g_za.invalid, + (unsigned long long)g_za.interval_calls, g_za.configured, g_za.adaptive, + g_za.last_elapsed / 1e6, g_za.forwarded, g_za.selected, + (unsigned long long)g_za.codes[0], (unsigned long long)g_za.codes[1], + (unsigned long long)g_za.codes[2], (unsigned long long)g_za.codes[3], + (unsigned long long)g_za.codes[4], (unsigned long long)g_za.codes[5]); + g_za.report_wall = wall; g_za.report_guest = guest; g_za.report_sum = g_za.sum_ns; + if (mode == ZA_DYNAMIC60) LOG("Z-A dynamic60: epoch=%llu raw_frame=%.6f ms step=%.6f ms flags=%#x samples=%llu active_sum=%.6f s measured_issued=%.6f s phase=%lld ns limited=%.6f s seeds=%llu frozen=%llu resumed=%llu clock_resets=%llu; measured totals exclude explicit seed steps and are independent of diagnostic warmup", + (unsigned long long)epoch, g_za_step.raw_ns / 1e6, g_za_step.step_ns / 1e6, g_za_step.flags, + (unsigned long long)g_za_frame.samples, g_za_frame.raw_sum / 1e9, g_za_frame.step_sum / 1e9, + (long long)g_za_frame.debt, g_za_frame.limited_ns / 1e9, (unsigned long long)g_za_frame.seeds, + (unsigned long long)g_za_frame.frozen, (unsigned long long)g_za_frame.resumed, + (unsigned long long)g_za_frame.resets); + } + pthread_mutex_unlock(&g_za_mu); +} + +// main+0x1b296f0 is immediately after bl main+0x113488, at mov w1,w20. +// w20 already holds the configured code, w22 the measured/fixed candidate. +// Reproducing mov w20,#0 at the earlier load also requires w22=0 when the +// native adaptive flag is off; otherwise its original csel had copied w20. +static void za_interval(uint64_t *regs, int param) { + (void)param; + pthread_once(&g_za_once, za_init); + if (!g_za_valid) return; + uint64_t config = 0; uint8_t native[2]; int64_t elapsed = 0; + if (!za_read(regs[19] + 0x40, &config, sizeof config) + || !za_read(config + 0x20, native, sizeof native) + || !za_read(regs[21] + 8, &elapsed, sizeof elapsed)) { + static unsigned failures; + if (failures++ < 8) LOG("[RED] Z-A timing: interval inputs unreadable; leaving saved registers unchanged"); + return; + } + pthread_mutex_lock(&g_za_mu); + int mode = atomic_load_explicit(&g_za_mode, memory_order_acquire); + if (mode == ZA_DYNAMIC60) { + if (native[1] && elapsed > 1000000000ll) { + // This guard runs after our observation point. Respect its native + // code-4 behavior instead of silently supplying the wrong table. + LOG("[RED] Z-A dynamic60: native long-work guard reached; returning to game timing"); + za_reset_locked(ZA_GAME, "native long-work guard"); + } else { + g_za_step = za_frame_delta_advance(&g_za_frame, guest_ns(), now_ns()); + if (!(g_za_step.flags & ZA_FRAME_FROZEN)) { + if (za_table_publish(g_za_step.step_ns, g_za_step.code)) { + regs[20] = 0; regs[22] = (uint32_t)g_za_step.code; + } else { + LOG("[RED] Z-A dynamic60: duration ownership or bounds changed; returning to game timing"); + za_reset_locked(ZA_GAME, "duration ownership failure"); + } + } + if ((g_za_step.flags & (ZA_FRAME_LIMITED | ZA_FRAME_CLOCK_RESET)) && + (g_za_exception_logs++ < 8 || !(g_za_frame.samples % 300))) + LOG("Z-A dynamic60: exceptional frame raw=%.6f ms step=%.6f ms flags=%#x lost_active=%.6f s", + g_za_step.raw_ns / 1e6, g_za_step.step_ns / 1e6, g_za_step.flags, g_za_frame.limited_ns / 1e9); + } + } else if (mode) { + regs[20] = 0; + if (mode == ZA_ADAPTIVE60) regs[22] = (uint32_t)za_elapsed_code(elapsed); + else if (!native[1]) regs[22] = 0; + } + int forwarded = (int32_t)regs[20], candidate = (int32_t)regs[22]; + int selected = candidate > forwarded ? candidate : forwarded; + // The original guard after the max also fixes code 4 above one second when + // native adaptive mode is on. Normally the elapsed comparisons chose 4 first. + if (native[1] && elapsed > 1000000000ll) selected = 4; + g_za_interval_seen = 1; + g_za.interval_calls++; g_za.configured = (int8_t)native[0]; g_za.adaptive = native[1] != 0; + g_za.last_elapsed = elapsed; g_za.forwarded = forwarded; g_za.selected = selected; + g_za.codes[selected >= 0 && selected <= 4 ? selected : 5]++; + pthread_mutex_unlock(&g_za_mu); +} +// Observe the actual move layout, without changing guest registers or HID. +// Ordinary ZL, menus, and absent targets do not enable virtual skill layout. +static pthread_once_t g_skill_once = PTHREAD_ONCE_INIT; +static int g_skill_valid, g_skill_virtual_layout, g_skill_diag; +static uint64_t g_skill_vtable; +static _Atomic uint64_t g_skill_layout; +static _Atomic int g_skill_visible; +static void za_skill_init(void) { + const char *layout = getenv("DM_ZA_SKILL_LAYOUT"), *diag = getenv("DM_ZA_SKILL_DIAG"); + g_skill_virtual_layout = layout && atoi(layout); + g_skill_diag = diag && atoi(diag); + if (!g_skill_virtual_layout && !g_skill_diag) return; + for (int i = 0; i < g_nmod; i++) { + module_t *m = &g_mod[i]; + if (!strcmp(m->name, "main") && !strcmp(m->build_id, "b1f12fd919eae86ab8a978317677e64b") + && m->text_end == 0x3163f70 && m->data_off == 0x3bc2000) { + g_skill_vtable = (uint64_t)(uintptr_t)m->base + 0x3dbdc88; + g_skill_valid = 1; + } + } + LOG("Z-A skill context: exact build verified=%d; virtual skill layout option=%d; normal face functions preserved", + g_skill_valid, g_skill_virtual_layout); +} +int dm_za_skill_layout_active(void) { + // Do not initialize from UIKit before the guest module metadata is ready. + return atomic_load_explicit(&g_skill_visible, memory_order_acquire); +} +static void za_skill_visibility(uint64_t *regs, int param) { + (void)param; + pthread_once(&g_skill_once, za_skill_init); + if (!g_skill_valid) return; + uint64_t vtable; + // x0 is the live object the original method is about to dereference. + memcpy(&vtable, (const void *)(uintptr_t)regs[0], sizeof vtable); + if (vtable != g_skill_vtable) return; + int visible = regs[1] & 1; + atomic_store_explicit(&g_skill_layout, regs[0], memory_order_relaxed); + atomic_store_explicit(&g_skill_visible, visible && g_skill_virtual_layout, memory_order_release); + static unsigned reports; + if (g_skill_diag && reports++ < 128) LOG("Z-A skill context: layout=%#llx visible=%d active=%d", + (unsigned long long)regs[0], visible, visible && g_skill_virtual_layout); +} +static void za_skill_destroy(uint64_t *regs, int param) { + (void)param; + if (regs[0] != atomic_load_explicit(&g_skill_layout, memory_order_relaxed)) return; + atomic_store_explicit(&g_skill_visible, 0, memory_order_release); + atomic_store_explicit(&g_skill_layout, 0, memory_order_relaxed); + if (g_skill_diag) LOG("Z-A skill context: move layout destroyed; ordinary face functions active"); +} +__attribute__((constructor)) static void za_timing_gates(void) { + gate_obs_set(OBS_ZA_CLOCK_DELTA, za_clock_delta); + gate_obs_set(OBS_ZA_INTERVAL, za_interval); + gate_obs_set(OBS_ZA_SKILL_VISIBILITY, za_skill_visibility); + gate_obs_set(OBS_ZA_SKILL_DESTROY, za_skill_destroy); +} diff --git a/dm/host/loader.c b/dm/host/loader.c index 0ede3cca7a..0b3ba43faf 100644 --- a/dm/host/loader.c +++ b/dm/host/loader.c @@ -500,57 +500,94 @@ void loader_hash_log(void) { } #if TARGET_OS_IPHONE -// iOS does not allow code loaded at run time to execute, and Apple allows an executable only one segment with execute permission (ITMS-90999): the image is compiled into this executable by dm/ios/tools/gen_embed.py -- -// the two code blocks (original text, patch area) are sections in __TEXT, and W (the rest of ro + data + bss) is a separate RW segment. The three blocks are not adjacent in the file; here one contiguous address range is reserved, -// and they are vm_remap'ed back at their original relative positions: code pages with copy=FALSE (sharing the same signed pages with __TEXT, r-x), W with copy=TRUE (copy-on-write, paged only when used). -// The run-time layout is byte-identical to the original image. Remapping signed pages to a new address and then executing them on iOS is exactly what libffi's trampoline table (FFI_EXEC_TRAMPOLINE_TABLE) does +// iOS allows only one executable segment. For the normal (positive PATCH_OFF) +// layout, gen_embed.py places X0, a hole for W, and X1 at their final addresses +// inside __TEXT. Only the hole is replaced with a writable copy-on-write +// mapping; the code stays in dyld's original signed executable mapping. #include +#define IMAGE_MH (&_mh_execute_header) +#if TARGET_OS_TV +// The tvOS packager gives guest data its final, nonexecutable RW segment +// before signing. Find code sections across the split guest RX segments; +// all module-relative addresses retain their original contiguous layout. +static const uint8_t *tv_guest_section(const char *name, unsigned long *size) { + const uint8_t *p = (const uint8_t *)IMAGE_MH + sizeof(*IMAGE_MH); + for (uint32_t i = 0; i < IMAGE_MH->ncmds; i++) { + const struct load_command *lc = (const struct load_command *)p; + if (lc->cmd == LC_SEGMENT_64) { + const struct segment_command_64 *sg = (const struct segment_command_64 *)p; + const struct section_64 *ss = (const struct section_64 *)(sg + 1); + for (uint32_t j = 0; j < sg->nsects; j++) + if (!strncmp(ss[j].sectname, name, sizeof ss[j].sectname)) + return getsectiondata(IMAGE_MH, sg->segname, name, size); + } + p += lc->cmdsize; + } + return NULL; +} +#endif static void remap_at(module_t *m, vm_address_t to, const void *from, unsigned long size, boolean_t copy, vm_prot_t need) { vm_address_t a = to; vm_prot_t cur = 0, max = 0; kern_return_t kr = vm_remap(mach_task_self(), &a, size, 0, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE, mach_task_self(), (vm_address_t)from, copy, &cur, &max, VM_INHERIT_NONE); - if (kr != KERN_SUCCESS || a != to) die("%s vm_remap %p → %#lx (%#lx bytes) failed: %d", m->name, from, (unsigned long)to, size, kr); + if (kr != KERN_SUCCESS || a != to) die("%s vm_remap %p -> %#lx (%#lx bytes) failed: %d", m->name, from, (unsigned long)to, size, kr); if ((cur & need) != need) die("%s permissions after vm_remap %#x, missing %#x", m->name, cur, need); } -#define IMAGE_MH (&_mh_execute_header) -// The NRO modules (loader.c "NRO modules") are packed instead of getting sections of their own: a Mach-O file has at most 255 sections, and Super Smash Bros. Ultimate's 98 NROs would -// take 294. gen_embed.py puts their pieces one after another, each 16K aligned, in __TEXT,__dm_nro_x0 / __DM_NRO_W,__img / __TEXT,__dm_nro_x1, and __DATA,__dm_nro_tab lists them: -// 32 bytes of name, then the offset and length of its x0, W and x1 pieces in their sections (u64 each). A module name never holds an underscore, so these names meet no module's -typedef struct { char name[32]; uint64_t off[3][2]; } nro_ent; -static int nro_pieces(const char *name, const uint8_t **x0, unsigned long *z0, const uint8_t **w, unsigned long *zw, const uint8_t **x1, unsigned long *z1) { - unsigned long zt, s0, sw, s1; - const nro_ent *t = (const nro_ent *)getsectiondata(IMAGE_MH, "__DATA", "__dm_nro_tab", &zt); - const uint8_t *b0 = getsectiondata(IMAGE_MH, "__TEXT", "__dm_nro_x0", &s0), *bw = getsectiondata(IMAGE_MH, "__DM_NRO_W", "__img", &sw), *b1 = getsectiondata(IMAGE_MH, "__TEXT", "__dm_nro_x1", &s1); - if (!t || !b0 || !bw || !b1) return 0; - for (unsigned long i = 0; i < zt / sizeof *t; i++) if (!strncmp(t[i].name, name, sizeof t[i].name)) { - *x0 = b0 + t[i].off[0][0]; *z0 = t[i].off[0][1]; *w = bw + t[i].off[1][0]; *zw = t[i].off[1][1]; *x1 = b1 + t[i].off[2][0]; *z1 = t[i].off[2][1]; - return 1; - } - return 0; -} static void map_image(module_t *m, const char *dir) { (void)dir; - char sx0[17], sx1[17], dm[17]; snprintf(sx0, sizeof sx0, "__dm%sx0", m->name); snprintf(sx1, sizeof sx1, "__dm%sx1", m->name); snprintf(dm, sizeof dm, "__DM%sW", m->name); - unsigned long z0 = 0, zw = 0, z1 = 0; - const uint8_t *x0 = NULL, *w = NULL, *x1 = NULL; - if (m->nro) { if (!nro_pieces(m->name, &x0, &z0, &w, &zw, &x1, &z1)) die("NRO %s: not in the embedded NRO table (__DATA,__dm_nro_tab)", m->name); } - else { x0 = getsectiondata(IMAGE_MH, "__TEXT", sx0, &z0); w = getsectiondata(IMAGE_MH, dm, "__img", &zw); x1 = getsectiondata(IMAGE_MH, "__TEXT", sx1, &z1); } - if (!x0 || !w || !x1) die("embedded sections %s / %s / %s missing", sx0, dm, sx1); - // Patch area before the base (patch_off < 0): W ends at the end of data (FILE_IMAGE_LEN); bss and the key page are zero pages from vm_allocate and are not in the executable - int front = m->patch_off < 0; - if (z0 + zw != (front ? m->file_image_len : (uint64_t)m->patch_off) || (z0 & 0x3FFF) || (zw & 0x3FFF) || (z1 & 0x3FFF) || ((uintptr_t)x0 & 0x3FFF) || ((uintptr_t)w & 0x3FFF) || ((uintptr_t)x1 & 0x3FFF)) - die("%s embedded section layout mismatch (x0 %#lx w %#lx x1 %#lx, PATCH_OFF %#llx)", m->name, z0, zw, z1, (unsigned long long)m->patch_off); - m->size = front ? m->image_size : m->patch_off + z1; - uint64_t pre = front ? (uint64_t)-m->patch_off : 0; - if (front) m->patch_len = z1; +#if TARGET_OS_TV + char sx0[17], sx1[17], sd[17]; + snprintf(sx0, sizeof sx0, "__dm%sx0", m->name); + snprintf(sx1, sizeof sx1, "__dm%sx1", m->name); + snprintf(sd, sizeof sd, "__DM%sD", m->name); + unsigned long z0, z1, zw; + const uint8_t *x0 = tv_guest_section(sx0, &z0), *x1 = tv_guest_section(sx1, &z1); + const uint8_t *w = getsectiondata(IMAGE_MH, sd, "__data", &zw); + if (m->patch_off < 0 || !x0 || !x1 || !w || + z0 != ((m->text_end + 0x3FFF) & ~0x3FFFull) || + x0 + z0 != w || w + zw != x1 || x1 != x0 + m->patch_off || + (((uintptr_t)x0 | z0 | zw | z1) & 0x3FFF)) + die("%s invalid static tvOS guest segment layout", m->name); + m->base = (uint8_t *)x0; m->size = z0 + zw + z1; + install_key_page(m); + LOG("%s using static signed tvOS RX/RW/RX segments; no TEXT protection change", m->name); + return; +#else + char sw[17]; snprintf(sw, sizeof sw, "__DM%sW", m->name); + unsigned long zw; const uint8_t *w = getsectiondata(IMAGE_MH, sw, "__img", &zw); + if (!w) die("embedded section %s missing", sw); + uint64_t z0 = (m->text_end + 0x3FFF) & ~0x3FFFull; + + if (m->patch_off >= 0) { + char si[17]; snprintf(si, sizeof si, "__dm%simg", m->name); + unsigned long zi; const uint8_t *img = getsectiondata(IMAGE_MH, "__TEXT", si, &zi); + if (!img) die("embedded section __TEXT,%s missing", si); + if (z0 + zw != (uint64_t)m->patch_off || (zi & 0x3FFF) || ((uintptr_t)img & 0x3FFF) || ((uintptr_t)w & 0x3FFF)) + die("%s embedded section layout mismatch (image %#lx W %#lx PATCH_OFF %#llx)", m->name, zi, zw, (unsigned long long)m->patch_off); + m->base = (uint8_t *)img; m->size = zi; + vm_address_t data = (vm_address_t)(m->base + z0); + kern_return_t kr = vm_protect(mach_task_self(), data, zw, FALSE, VM_PROT_READ | VM_PROT_WRITE); + if (kr != KERN_SUCCESS) die("%s vm_protect __TEXT data hole %p (%#lx bytes) failed: %d", m->name, (void *)data, zw, kr); + memcpy((void *)data, w, zw); + LOG("%s using signed __TEXT in place; W copied to %p (%#lx bytes)", m->name, (void *)data, zw); + install_key_page(m); + return; + } + + // Negative PATCH_OFF builds retain the older three-piece representation. + char sx0[17], sx1[17]; snprintf(sx0, sizeof sx0, "__dm%sx0", m->name); snprintf(sx1, sizeof sx1, "__dm%sx1", m->name); + unsigned long zx0, zx1; + const uint8_t *x0 = getsectiondata(IMAGE_MH, "__TEXT", sx0, &zx0), *x1 = getsectiondata(IMAGE_MH, "__TEXT", sx1, &zx1); + if (!x0 || !x1 || zx0 + zw != m->file_image_len || (zx0 & 0x3FFF) || (zw & 0x3FFF) || (zx1 & 0x3FFF)) + die("%s embedded front-patch section layout mismatch", m->name); + uint64_t pre = (uint64_t)-m->patch_off; m->size = m->image_size; m->patch_len = zx1; vm_address_t base = 0; - if (vm_allocate(mach_task_self(), &base, pre + m->size, VM_FLAGS_ANYWHERE) != KERN_SUCCESS) die("%s failed to reserve %#llx bytes of address space", m->name, (unsigned long long)(pre + m->size)); + if (vm_allocate(mach_task_self(), &base, pre + m->size, VM_FLAGS_ANYWHERE) != KERN_SUCCESS) die("%s failed to reserve address space", m->name); base += pre; - remap_at(m, base, x0, z0, FALSE, VM_PROT_READ | VM_PROT_EXECUTE); - remap_at(m, base + z0, w, zw, TRUE, VM_PROT_READ | VM_PROT_WRITE); - // A module with nothing to rewrite has an empty patch area (87 of Super Smash Bros. Ultimate's 98 NROs: no x18, SVC or TLS register use); vm_remap refuses 0 bytes - // (KERN_INVALID_ARGUMENT), which stopped the app at launch on an iPhone at the first such NRO (found in PR #94) - if (z1) remap_at(m, base + m->patch_off, x1, z1, FALSE, VM_PROT_READ | VM_PROT_EXECUTE); + remap_at(m, base, x0, zx0, FALSE, VM_PROT_READ | VM_PROT_EXECUTE); + remap_at(m, base + zx0, w, zw, TRUE, VM_PROT_READ | VM_PROT_WRITE); + remap_at(m, base + m->patch_off, x1, zx1, FALSE, VM_PROT_READ | VM_PROT_EXECUTE); m->base = (uint8_t *)base; install_key_page(m); +#endif } #else static uint8_t *slurp(const char *path, size_t *n) { diff --git a/dm/host/map_headroom.h b/dm/host/map_headroom.h new file mode 100644 index 0000000000..e9ad0e7483 --- /dev/null +++ b/dm/host/map_headroom.h @@ -0,0 +1,27 @@ +#ifndef DM_MAP_HEADROOM_H +#define DM_MAP_HEADROOM_H + +#include + +// This is diagnostic-only. A b18 run proved that rejecting small runtime maps +// under pressure propagates ResultLimitReached into the game instead of making +// this SDK discard enough cached pages. Keep every platform's default off; +// DM_MAP_HEADROOM_MB can still reproduce and measure the behavior explicitly. +#define DM_MAP_HEADROOM_TV_DEFAULT_MB 0ull + +static inline uint64_t dm_map_headroom_default_bytes(int is_tvos, int is_simulator) { + return is_tvos && !is_simulator ? DM_MAP_HEADROOM_TV_DEFAULT_MB << 20 : 0; +} + +// MapPhysicalMemory only changes protection on reserved virtual pages; it does +// not commit every byte in the requested range. In particular, Z-A maps its +// roughly 3.15 GiB StandardAllocator arena at startup while the process still +// uses only a few MiB. Refuse only after the process's measured allowance has +// fallen below an explicitly requested reserve. new_bytes keeps already-mapped +// or no-op requests out of the guard. Equality is allowed. +static inline int dm_map_headroom_should_refuse(uint64_t available, uint64_t new_bytes, uint64_t reserve) { + if (!reserve || !new_bytes) return 0; + return available < reserve; +} + +#endif diff --git a/dm/host/tests/Makefile b/dm/host/tests/Makefile index 872316b745..b53ecbb11e 100644 --- a/dm/host/tests/Makefile +++ b/dm/host/tests/Makefile @@ -12,12 +12,13 @@ QUIET = $(if $(strip $(SAN)),,2>/dev/null) GINC = $(if $(strip $(GAME)),-I../gen/$(GAME) -I../../games/$(GAME),$(error GAME is not set: make GAME=: $(shell python3 ../../games/game.py list | awk -F'\t' '{printf "%s (%s) ", $$1, $$3}'))) $(B)/t_fs $(B)/t_fs_case $(B)/t_fs_commit_list $(B)/t_fs_commit_list_tsan: ../romfs_image.h GAME_H = ../gen/$(GAME)/game_profile.h -run: $(B)/t_motion $(B)/t_heap_size $(B)/t_touch $(B)/t_gesture $(B)/t_oe $(B)/t_fs $(B)/t_fs_commit_list $(B)/t_thread_leak $(B)/t_fs_case $(B)/t_audio $(B)/t_audio_spsc $(B)/t_swkbd $(B)/t_altstack $(B)/t_condvar $(B)/t_condvar_old $(B)/t_pctl $(B)/t_time $(B)/t_shared_font $(B)/t_vorbis $(B)/t_vorbis_divides $(B)/t_hid $(B)/t_haptics $(B)/t_haptics_apple +run: $(B)/t_motion $(B)/t_heap_size $(B)/t_touch $(B)/t_gesture $(B)/t_oe $(B)/t_fs $(B)/t_fs_redirect $(B)/t_fs_commit_list $(B)/t_thread_leak $(B)/t_fs_case $(B)/t_audio $(B)/t_audio_spsc $(B)/t_swkbd $(B)/t_altstack $(B)/t_condvar $(B)/t_condvar_old $(B)/t_pctl $(B)/t_time $(B)/t_shared_font $(B)/t_vorbis $(B)/t_vorbis_divides $(B)/t_hid $(B)/t_haptics $(B)/t_haptics_apple $(B)/t_map_headroom za-timing audio-recovery $(B)/t_heap_size $(B)/t_touch $(B)/t_gesture $(B)/t_oe $(QUIET) $(B)/t_fs $(QUIET) + $(B)/t_fs_redirect $(QUIET) TMPDIR="$(abspath $(B))" $(B)/t_fs_commit_list $(B)/t_thread_leak TMPDIR="$(abspath $(B))" $(B)/t_fs_case $(QUIET) @@ -46,6 +47,7 @@ run: $(B)/t_motion $(B)/t_heap_size $(B)/t_touch $(B)/t_gesture $(B)/t_oe $(B)/t $(B)/t_hid $(B)/t_haptics $(B)/t_haptics_apple + $(B)/t_map_headroom $(B)/t_nro $(B)/t_symbols run: $(B)/t_nro @@ -93,6 +95,8 @@ $(B)/t_thread_leak: t_thread_leak.c ../thread.c ../dm.h $(GAME_H) | $(B) $(CLANG) -O1 -Wno-unused-function -I.. $(GINC) -o $@ t_thread_leak.c $(B)/t_fs: t_fs.c t_fs_stub.c ../hle_fs.c ../hle_bigstack.c ../hle.h ../dm.h $(GAME_H) | $(B) $(CLANG) -O1 -Wno-unused-function -I.. $(GINC) -o $@ t_fs.c t_fs_stub.c ../hle_bigstack.c +$(B)/t_fs_redirect: t_fs_redirect.c t_fs_stub.c ../hle_fs.c ../hle_bigstack.c ../hle.h ../dm.h $(GAME_H) | $(B) + $(CLANG) -O1 -Wall -Wextra -Werror -Wno-unused-function -I.. $(GINC) -o $@ t_fs_redirect.c t_fs_stub.c ../hle_bigstack.c $(B)/t_fs_commit_list: t_fs_commit_list.c t_fs_stub.c ../hle_fs.c ../hle_bigstack.c ../hle.h ../dm.h $(GAME_H) | $(B) $(CLANG) -O1 -Wall -Wno-unused-function -I.. $(GINC) -o $@ t_fs_commit_list.c t_fs_stub.c ../hle_bigstack.c $(B)/t_fs_commit_list_tsan: t_fs_commit_list.c t_fs_stub.c ../hle_fs.c ../hle_bigstack.c ../hle.h ../dm.h $(GAME_H) | $(B) @@ -117,6 +121,14 @@ $(B)/t_audio_spsc_tsan: t_audio_spsc.c ../hle_audio.c ../hle.h ../dm.h $(GAME_H) run: $(B)/t_fpslock $(B)/t_fpslock: t_fpslock.c ../hle_fpslock.c ../hle.h ../dm.h $(GAME_H) | $(B) $(CLANG) -O1 -g -Wno-unused-function $(GINC) -o $@ t_fpslock.c +$(B)/t_audio_recovery: t_audio_recovery.c ../hle_audio.c ../hle.h ../dm.h $(GAME_H) | $(B) + $(CLANG) -O1 -g -Wall -Wno-unused-function -I.. $(GINC) -o $@ t_audio_recovery.c -framework AudioToolbox +$(B)/t_audio_session_retry: t_audio_session_retry.c ../../ios/Sources/DmAudioRetry.h | $(B) + $(CLANG) -O1 -Wall -Wextra -Werror -o $@ t_audio_session_retry.c +.PHONY: audio-recovery +audio-recovery: $(B)/t_audio_recovery $(B)/t_audio_session_retry + $(B)/t_audio_recovery + $(B)/t_audio_session_retry $(B)/t_swkbd: t_swkbd.c ../hle_swkbd.c ../hle.h ../dm.h $(GAME_H) | $(B) $(CLANG) -O1 -Wno-unused-function -I.. $(GINC) -o $@ t_swkbd.c ../hle_swkbd.c $(B)/t_altstack: t_altstack.c t_altstack_stub.c ../crash.c ../dm.h $(GAME_H) | $(B) @@ -143,8 +155,21 @@ $(B)/t_haptics: t_haptics.c ../hle_haptics.c ../hle_haptics.h ../dm_input.h | $( $(CLANG) -O1 -g -Wall -Werror -I.. -o $@ t_haptics.c ../hle_haptics.c $(B)/t_haptics_apple: t_haptics_apple.m ../haptics_apple.m ../haptics_apple.h ../hle_haptics.c ../hle_haptics.h | $(B) $(CLANG) -O1 -g -fobjc-arc -Wall -Werror -I.. -o $@ t_haptics_apple.m ../hle_haptics.c -framework Cocoa -framework GameController -framework CoreHaptics -framework IOKit +$(B)/t_map_headroom: t_map_headroom.c ../map_headroom.h | $(B) + $(CLANG) -O1 -Wall -Wextra -Werror -o $@ t_map_headroom.c $(B)/t_time: t_time.c ../hle_time.c ../hle.h ../dm.h $(GAME_H) | $(B) $(CLANG) -O1 -Wall -Werror -Wno-unused-function -I.. $(GINC) -o $@ t_time.c +$(B)/t_za_frame_delta: t_za_frame_delta.c ../za_frame_delta.h | $(B) + $(CLANG) -O1 -Wall -Wextra -Werror -o $@ t_za_frame_delta.c +$(B)/t_za_timing: t_za_timing.c ../hle_za_timing.c ../za_frame_delta.h ../hle.h ../dm.h $(GAME_H) | $(B) + $(CLANG) -O1 -Wall -Wextra -Werror -I.. $(GINC) -o $@ t_za_timing.c +$(B)/t_za_dynamic: t_za_dynamic.c ../hle_za_timing.c ../za_frame_delta.h ../hle.h ../dm.h $(GAME_H) | $(B) + $(CLANG) -O1 -Wall -Wextra -Werror -I.. $(GINC) -o $@ t_za_dynamic.c +.PHONY: za-timing +za-timing: $(B)/t_za_frame_delta $(B)/t_za_timing $(B)/t_za_dynamic + $(B)/t_za_frame_delta + @set -e; for c in off trace static-fixed static-adaptive native-adaptive wrong-build live concurrent; do $(B)/t_za_timing $$c 2>/dev/null; done + @set -e; for c in clock clock120 conflict guard wrong-layout wrong-build wrong-table unaligned detclock readonly executable; do $(B)/t_za_dynamic $$c 2>/dev/null; done $(B)/t_oe: t_oe.c ../hle_oe.c ../hle.h ../dm.h $(GAME_H) | $(B) $(CLANG) -O1 -Wno-unused-function -I.. $(GINC) -o $@ t_oe.c $(B)/t_condvar: t_condvar.c t_condvar_stub.c ../hle_thread.c ../dm.h $(GAME_H) | $(B) diff --git a/dm/host/tests/t_audio.c b/dm/host/tests/t_audio.c index 13acfd5133..cfbb5eba0b 100644 --- a/dm/host/tests/t_audio.c +++ b/dm/host/tests/t_audio.c @@ -32,6 +32,7 @@ static int test_surround_output(void) { gbuf b[2] = {{in, SPLIT * 12, SPLIT * 12}, {in + SPLIT * 6, (FRAMES - SPLIT) * 12, (FRAMES - SPLIT) * 12}}; aout a = {.ch = 6}; atomic_init(&a.state, STATE_STARTED); atomic_init(&a.vol, 0x3f800000u); + atomic_init(&a.consumer, BK_AU); a.q[0] = b; a.q[1] = b + 1; atomic_init(&a.q_tail, 2); AudioBufferList bl = {.mNumberBuffers = 1, .mBuffers = {{2, sizeof out, out}}}; au_render(&a, NULL, NULL, 0, FRAMES, &bl); diff --git a/dm/host/tests/t_audio_recovery.c b/dm/host/tests/t_audio_recovery.c new file mode 100644 index 0000000000..88dc89859f --- /dev/null +++ b/dm/host/tests/t_audio_recovery.c @@ -0,0 +1,247 @@ +// Hardwareless recovery regression: no AudioUnit, speaker or system session is +// opened. The real lifecycle/queue code runs against injected CoreAudio calls. +// clang -O1 -g -Wall -Wno-unused-function -I dm/host -I +// dm/host/tests/t_audio_recovery.c -framework AudioToolbox -o /tmp/t_audio_recovery +#include +#include +#include + +static AudioComponent mock_AudioComponentFindNext(AudioComponent, const AudioComponentDescription *); +static OSStatus mock_AudioComponentInstanceNew(AudioComponent, AudioComponentInstance *); +static OSStatus mock_AudioComponentInstanceDispose(AudioComponentInstance); +static OSStatus mock_AudioUnitSetProperty(AudioUnit, AudioUnitPropertyID, AudioUnitScope, AudioUnitElement, const void *, UInt32); +static OSStatus mock_AudioUnitInitialize(AudioUnit); +static OSStatus mock_AudioUnitUninitialize(AudioUnit); +static OSStatus mock_AudioOutputUnitStart(AudioUnit); +static OSStatus mock_AudioOutputUnitStop(AudioUnit); + +#define AudioComponentFindNext mock_AudioComponentFindNext +#define AudioComponentInstanceNew mock_AudioComponentInstanceNew +#define AudioComponentInstanceDispose mock_AudioComponentInstanceDispose +#define AudioUnitSetProperty mock_AudioUnitSetProperty +#define AudioUnitInitialize mock_AudioUnitInitialize +#define AudioUnitUninitialize mock_AudioUnitUninitialize +#define AudioOutputUnitStart mock_AudioOutputUnitStart +#define AudioOutputUnitStop mock_AudioOutputUnitStop +#include "../hle_audio.c" +#undef AudioComponentFindNext +#undef AudioComponentInstanceNew +#undef AudioComponentInstanceDispose +#undef AudioUnitSetProperty +#undef AudioUnitInitialize +#undef AudioUnitUninitialize +#undef AudioOutputUnitStart +#undef AudioOutputUnitStop + +typedef struct { int initialized, running, disposed; } fake_unit; +static fake_unit g_units[64]; +static int g_nunit, g_fail_new, g_fail_start, g_create_calls, g_start_calls, g_stop_calls, g_dispose_calls, g_running; +static aout *g_checked; +static int g_component; +static fake_unit *unit(AudioUnit au) { assert(au); return (fake_unit *)(void *)au; } +static AudioComponent mock_AudioComponentFindNext(AudioComponent previous, const AudioComponentDescription *description) { + (void)previous; assert(description->componentType == kAudioUnitType_Output); + return (AudioComponent)(void *)&g_component; +} +static OSStatus mock_AudioComponentInstanceNew(AudioComponent component, AudioComponentInstance *result) { + assert(component); g_create_calls++; + if (g_fail_new) { g_fail_new--; *result = NULL; return -10863; } + assert(g_nunit < (int)(sizeof g_units / sizeof *g_units)); + fake_unit *u = &g_units[g_nunit++]; memset(u, 0, sizeof *u); + *result = (AudioComponentInstance)(void *)u; + return noErr; +} +static OSStatus mock_AudioComponentInstanceDispose(AudioComponentInstance instance) { + fake_unit *u = unit(instance); assert(!u->running && !u->disposed); + u->disposed = 1; g_dispose_calls++; return noErr; +} +static OSStatus mock_AudioUnitSetProperty(AudioUnit au, AudioUnitPropertyID property, AudioUnitScope scope, AudioUnitElement element, const void *data, UInt32 size) { + (void)property; (void)scope; (void)element; (void)data; (void)size; + assert(!unit(au)->disposed); return noErr; +} +static OSStatus mock_AudioUnitInitialize(AudioUnit au) { assert(!unit(au)->disposed); unit(au)->initialized = 1; return noErr; } +static OSStatus mock_AudioUnitUninitialize(AudioUnit au) { assert(!unit(au)->running); unit(au)->initialized = 0; return noErr; } +static OSStatus mock_AudioOutputUnitStart(AudioUnit au) { + fake_unit *u = unit(au); + assert(u->initialized && !u->disposed && !u->running); + // A recreated unit must never play concurrently with the silent consumer. + assert(!g_running && g_checked && !atomic_load(&g_checked->null_run)); + g_start_calls++; + if (g_fail_start) { g_fail_start--; return -10863; } + u->running = 1; g_running++; return noErr; +} +static OSStatus mock_AudioOutputUnitStop(AudioUnit au) { + fake_unit *u = unit(au); assert(!u->disposed); + if (u->running) { u->running = 0; g_running--; } + g_stop_calls++; return noErr; +} + +uint64_t now_ns(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (uint64_t)t.tv_sec * 1000000000ull + t.tv_nsec; } +static const void *g_key; static void *g_value; +void *objmap_get(const void *key) { return key == g_key ? g_value : NULL; } +void objmap_put(const void *key, void *value) { g_key = key; g_value = value; } +void *objmap_del(const void *key) { void *value = objmap_get(key); if (value) { g_key = NULL; g_value = NULL; } return value; } +void hle_signal_system_event(void *event) { (void)event; } +void hle_bind_system_event(void *event, int autoclear) { (void)event; (void)autoclear; } +module_t *module_of(const void *address) { (void)address; return NULL; } +void dm_host_helper_qos(qos_class_t qos) { (void)qos; } + +static void begin(aout *a, int playing) { + assert(!g_running); memset(a, 0, sizeof *a); g_checked = a; + pthread_mutex_init(&a->m, NULL); a->ch = 2; a->id = 42; + a->real_requested = 1; + atomic_init(&a->state, playing ? STATE_STARTED : STATE_STOPPED); + atomic_init(&a->vol, 0x3f800000u); + g_fail_new = g_fail_start = 0; g_create_calls = g_start_calls = g_stop_calls = g_dispose_calls = 0; + g_aos[0] = a; g_suspended = 0; objmap_put(a, a); + assert(!au_create(a)); + if (playing) { au_go(a, now_ns()); assert(a->backend == BK_AU); } +} +static void finish(aout *a) { + pthread_mutex_lock(&g_aos_lock); backend_stop(a); g_aos[0] = NULL; g_suspended = 0; pthread_mutex_unlock(&g_aos_lock); + if (a->au) { mock_AudioUnitUninitialize(a->au); mock_AudioComponentInstanceDispose(a->au); a->au = NULL; } + assert(!g_running && !atomic_load(&a->null_run)); + objmap_del(a); pthread_mutex_destroy(&a->m); g_checked = NULL; +} +static void retry_due(aout *a) { + pthread_mutex_lock(&g_aos_lock); + au_watchdog_step_locked(a->retry_at + 1, WD_TICK_MS * 1000000ull); + pthread_mutex_unlock(&g_aos_lock); +} +static void expect_meter(aout *a, uint32_t peak, uint32_t mean, uint32_t zeros, uint32_t samples) { + uint64_t levels = atomic_load(&a->pcm_levels), counts = atomic_load(&a->pcm_counts); + assert((uint32_t)(levels >> 32) == peak && (uint32_t)levels == mean); + assert((uint32_t)(counts >> 32) == zeros && (uint32_t)counts == samples); +} +static void meter_boundaries(void) { + aout a = {0}; + const int16_t zero[] = {0, 0, 0, 0}; au_meter_publish(&a, zero, 4); expect_meter(&a, 0, 0, 4, 4); + const int16_t mixed[] = {0, 3, -4, 0}; au_meter_publish(&a, mixed, 4); expect_meter(&a, 4, 6, 2, 4); + const int16_t full[] = {32767, -32768}; au_meter_publish(&a, full, 2); expect_meter(&a, 32768, 1073709056u, 0, 2); + int16_t large[8192]; for (int i = 0; i < 8192; i++) large[i] = -5; + au_meter_publish(&a, large, 8192); expect_meter(&a, 5, 25, 0, 4096); + au_meter_publish(&a, NULL, 0); expect_meter(&a, 0, 0, 0, 0); + puts("PCM meter: silence, mixed/full-scale, zero-length and bounded long input: PASS"); +} +static void callback_owner_and_gain(void) { + aout a; begin(&a, 1); + int16_t input[] = {1000, -1000, 0, 2000}, output[4] = {777, 777, 777, 777}; + gbuf first = {input, sizeof input, sizeof input}, second = first; + a.q[0] = &first; atomic_store(&a.q_tail, 1); + AudioBufferList buffers = {.mNumberBuffers = 1, .mBuffers = {{.mNumberChannels = 2, .mDataByteSize = sizeof output, .mData = output}}}; + AudioTimeStamp time = {0}; AudioUnitRenderActionFlags flags = 0; + atomic_store(&a.consumer, BK_NULL); // stale AU callback after management handed the queue to its replacement + assert(!au_render(&a, &flags, &time, 0, 2, &buffers)); + for (int i = 0; i < 4; i++) assert(output[i] == 0); + assert(atomic_load(&a.q_head) == 0 && atomic_load(&a.q_tail) == 1); + expect_meter(&a, 0, 0, 4, 4); + + atomic_store(&a.consumer, BK_AU); atomic_store(&a.vol, 0x3f000000u); // 0.5 gain + assert(!au_render(&a, &flags, &time, 0, 2, &buffers)); + const int16_t scaled[] = {500, -500, 0, 1000}; assert(!memcmp(output, scaled, sizeof output)); + assert(atomic_load(&a.q_head) == 1); expect_meter(&a, 1000, 375000, 1, 4); + + a.q[1] = &second; atomic_store(&a.q_tail, 2); a.mute = 1; + assert(!au_render(&a, &flags, &time, 0, 2, &buffers)); + for (int i = 0; i < 4; i++) assert(output[i] == 0); + assert(atomic_load(&a.q_head) == 2); expect_meter(&a, 0, 0, 4, 4); + finish(&a); puts("real callback: ownership fence, post-gain meter and post-mute zero detection: PASS"); +} + +static unsigned g_env_calls; +static char g_env_value[64]; +static void env_observe(const char *key, const char *value) { + assert(!strcmp(key, "DM_AUDIO_SESSION_CATEGORY")); + g_env_calls++; snprintf(g_env_value, sizeof g_env_value, "%s", value ? value : ""); +} +static _Atomic unsigned g_env_stress_calls; +static void env_stress_observe(const char *key, const char *value) { + assert(!strcmp(key, "DM_AUDIO_SESSION_CATEGORY") && !strcmp(value, "playback_mix")); + atomic_fetch_add_explicit(&g_env_stress_calls, 1, memory_order_relaxed); +} +static void *env_registration_thread(void *unused) { + (void)unused; + for (unsigned i = 0; i < 100000; i++) dm_audio_env_callback_set(i & 1 ? env_stress_observe : NULL); + return NULL; +} +static void env_notification(void) { + dm_audio_env_callback_set(NULL); + dm_audio_env_changed("DM_AUDIO_SESSION_CATEGORY", "playback_mix"); + assert(!g_env_calls); // non-iOS host does not require a frontend symbol + dm_audio_env_callback_set(env_observe); + dm_audio_env_changed(NULL, "playback_mix"); + dm_audio_env_changed("DM_OPMODE", "1"); + dm_audio_env_changed("DM_AUDIO_SESSION_CATEGORY_EXTRA", "playback_mix"); + assert(!g_env_calls); + dm_audio_env_changed("DM_AUDIO_SESSION_CATEGORY", "playback_mix"); + assert(g_env_calls == 1 && !strcmp(g_env_value, "playback_mix")); + dm_audio_env_changed("DM_AUDIO_SESSION_CATEGORY", "solo"); + assert(g_env_calls == 2 && !strcmp(g_env_value, "solo")); + dm_audio_env_callback_set(NULL); + dm_audio_env_changed("DM_AUDIO_SESSION_CATEGORY", "playback_mix"); + assert(g_env_calls == 2); + + pthread_t registration; assert(!pthread_create(®istration, NULL, env_registration_thread, NULL)); + for (unsigned i = 0; i < 100000; i++) { + dm_audio_env_changed("DM_AUDIO_SESSION_CATEGORY", "playback_mix"); + dm_audio_env_changed("DM_OPMODE", "1"); + } + assert(!pthread_join(registration, NULL)); + dm_audio_env_callback_set(env_stress_observe); + unsigned before = atomic_load(&g_env_stress_calls); + dm_audio_env_changed("DM_AUDIO_SESSION_CATEGORY", "playback_mix"); + assert(atomic_load(&g_env_stress_calls) == before + 1); + dm_audio_env_callback_set(NULL); + puts("CTL category notification: no-frontend no-op, exact-key live round trip and concurrent registration: PASS"); +} + +int main(void) { + env_notification(); meter_boundaries(); callback_owner_and_gain(); + aout a; + begin(&a, 1); g_fail_new = 1; AudioUnit old = a.au; + dm_audio_reset(); + assert(unit(old)->disposed && !a.au && a.backend == BK_NULL && a.stalled && atomic_load(&a.null_run)); + retry_due(&a); + assert(a.au && a.au != old && a.backend == BK_AU && !a.stalled && !atomic_load(&a.null_run) && g_running == 1); + finish(&a); puts("media reset: transient creation failure recovers a NULL AudioUnit with one consumer: PASS"); + + begin(&a, 1); g_fail_start = 1; dm_audio_reset(); + assert(a.au && a.backend == BK_NULL && a.stalled && !g_running); + int created = g_create_calls; retry_due(&a); + assert(g_create_calls == created && a.backend == BK_AU && !a.stalled && g_running == 1); + finish(&a); puts("media reset: transient Start failure retries the existing recreated unit: PASS"); + + begin(&a, 0); g_fail_new = 1; dm_audio_reset(); + assert(!a.au && atomic_load(&a.state) == STATE_STOPPED); + int before = g_create_calls; retry_due(&a); dm_audio_suspend(1); dm_audio_suspend(0); + assert(g_create_calls == before && !g_running && a.backend == BK_NONE); + h_au_Start(&a); + if (!a.au || a.backend != BK_AU) retry_due(&a); + assert(a.au && a.backend == BK_AU && !atomic_load(&a.null_run) && g_running == 1); + finish(&a); puts("stopped output: stays stopped across reset/resume, explicit Start can recover: PASS"); + + begin(&a, 1); g_fail_new = 1; dm_audio_reset(); dm_audio_suspend(1); + before = g_create_calls; retry_due(&a); + assert(g_create_calls == before && !g_running && a.backend == BK_NULL); + dm_audio_suspend(0); + if (!a.au || a.backend != BK_AU) retry_due(&a); + assert(a.au && a.backend == BK_AU && !atomic_load(&a.null_run) && g_running == 1); + finish(&a); puts("suspended output: no recreation/start until foreground resume: PASS"); + + begin(&a, 1); g_fail_new = 1; dm_audio_reset(); h_au_Stop(&a); + before = g_create_calls; retry_due(&a); + assert(g_create_calls == before && !g_running && a.backend == BK_NONE && !atomic_load(&a.null_run)); + finish(&a); puts("Stop during recovery: pending retries do not revive stopped output: PASS"); + + begin(&a, 1); g_fail_new = 10; dm_audio_reset(); + int previous = a.retry_ms; + for (int i = 0; i < 6; i++) { + retry_due(&a); + assert(!a.au && a.backend == BK_NULL && atomic_load(&a.null_run) && !g_running); + assert(a.retry_ms >= previous && a.retry_ms <= 8000); previous = a.retry_ms; + } + assert(a.retry_ms == 8000); g_fail_new = 0; retry_due(&a); + assert(a.au && a.backend == BK_AU && !atomic_load(&a.null_run)); + finish(&a); puts("repeated creation failures: bounded backoff and eventual recovery: PASS"); + return 0; +} diff --git a/dm/host/tests/t_audio_session_retry.c b/dm/host/tests/t_audio_session_retry.c new file mode 100644 index 0000000000..df42d48af8 --- /dev/null +++ b/dm/host/tests/t_audio_session_retry.c @@ -0,0 +1,69 @@ +// Hardwareless main-queue retry-policy scenarios used by front_ios.m. +// clang -O1 -Wall -o /tmp/t_audio_session_retry dm/host/tests/t_audio_session_retry.c +#include "../../ios/Sources/DmAudioRetry.h" +#include +#include + +int main(void) { + // The category experiment is opt-in; typoed values must not choose a policy. + assert(dm_audio_session_category_parse(NULL) == DM_AUDIO_SESSION_SOLO); + assert(dm_audio_session_category_parse("") == DM_AUDIO_SESSION_SOLO); + assert(dm_audio_session_category_parse("solo") == DM_AUDIO_SESSION_SOLO); + assert(dm_audio_session_category_parse("playback_mix") == DM_AUDIO_SESSION_PLAYBACK_MIX); + assert(dm_audio_session_category_parse("playback") == -1); + assert(dm_audio_session_category_parse("playback_mix ") == -1); + assert(dm_audio_session_category_parse("SOLO") == -1); + puts("category parser: SoloAmbient default, exact opt-in, invalid value rejected: PASS"); + + dm_audio_retry r = {0}; uint64_t first, second, token; unsigned ms; + // A session held for longer than seven seconds must still be able to recover. + // Failed activation stays suspended, with rate capped at one attempt per 8 s. + for (unsigned i = 0; i < 7; i++) { + ms = dm_audio_retry_plan(&r, 0, 1, &token); assert(ms == (1000u << (i < 3 ? i : 3))); + assert(!dm_audio_retry_plan(&r, 0, 1, &second)); // no duplicate timer + assert(dm_audio_retry_fire(&r, token, 0, 1)); + } + assert(r.attempts == 7); // success can still occur after the fourth/later attempt + dm_audio_retry_reset(&r); assert(!dm_audio_retry_plan(&r, 0, 0, &token)); + puts("long-held session: retries 1/2/4/8 seconds, later success still possible: PASS"); + + // An old callback must neither activate in the background nor cancel a new + // foreground retry that happens to be pending when it eventually fires. + dm_audio_retry_reset(&r); assert(dm_audio_retry_plan(&r, 0, 1, &first) == 1000); + dm_audio_retry_reset(&r); assert(!dm_audio_retry_plan(&r, 1, 1, &token)); + assert(!dm_audio_retry_fire(&r, first, 1, 1)); + assert(dm_audio_retry_plan(&r, 0, 1, &second) == 1000); + assert(!dm_audio_retry_fire(&r, first, 0, 1) && r.pending); + assert(dm_audio_retry_fire(&r, second, 0, 1)); + puts("background/foreground generations: stale callback cannot activate or cancel newer retry: PASS"); + + // Stage Manager resign-active and interruptions are blocked states even + // when a scene stays visible and never receives didEnterBackground. + dm_audio_retry_reset(&r); assert(dm_audio_retry_plan(&r, 0, 1, &token)); + assert(!dm_audio_retry_fire(&r, token, 1, 1)); + assert(!dm_audio_retry_plan(&r, 1, 1, &token)); + dm_audio_retry_reset(&r); assert(dm_audio_retry_plan(&r, 0, 1, &token)); + assert(!dm_audio_retry_fire(&r, token, 1, 1)); + puts("visible-inactive card and interruption: never reactivate while blocked: PASS"); + + // Successful activation clears the backoff and invalidates any queued retry. + dm_audio_retry_reset(&r); assert(dm_audio_retry_plan(&r, 0, 1, &token)); + dm_audio_retry_reset(&r); + assert(!dm_audio_retry_fire(&r, token, 0, 0)); + assert(!dm_audio_retry_plan(&r, 0, 0, &token)); + assert(r.attempts == 0 && !r.pending); + puts("success: pending retry invalidated, no restart while already active: PASS"); + + // A category transition cancels retries from the preceding configuration. + // Going inactive before either the old timer or a new one fires must prevent + // activation, including a visible Stage Manager card that never backgrounds. + assert(dm_audio_retry_plan(&r, 0, 1, &first)); + dm_audio_retry_reset(&r); // live category changed on the main queue + assert(!dm_audio_retry_fire(&r, first, 0, 1)); + assert(dm_audio_retry_plan(&r, 0, 1, &second)); + dm_audio_retry_reset(&r); // resign-active cancels that category's retry too + assert(!dm_audio_retry_fire(&r, second, 1, 1)); + assert(!dm_audio_retry_plan(&r, 1, 1, &token)); + puts("live category change followed by inactive: neither configuration can reactivate: PASS"); + return 0; +} diff --git a/dm/host/tests/t_fs_redirect.c b/dm/host/tests/t_fs_redirect.c new file mode 100644 index 0000000000..1f60064af8 --- /dev/null +++ b/dm/host/tests/t_fs_redirect.c @@ -0,0 +1,122 @@ +// Virtual sequential RomFS archive test. Logical records remain contiguous +// and sorted while their physical bytes live in disjoint source/replacement +// ranges of one materialized file. +#include +#include +#include +#include +#include "../hle_fs.c" + +struct dm_flags g_flag; +uint64_t g_t0_ns; +uint64_t now_ns(void) { return 0; } +void dm_guest_write_prepare(void *p, size_t n) { (void)p; (void)n; } + +static int fails, checks; +#define CHECK(c, ...) do { checks++; if (!(c)) { fails++; printf(" ✗ "); } else printf(" ✓ "); printf(__VA_ARGS__); printf("\n"); } while (0) + +static void write_bytes(const char *path, const void *bytes, size_t count) { + FILE *f = fopen(path, "wb"); + if (!f || fwrite(bytes, 1, count, f) != count || fclose(f)) { perror(path); exit(2); } +} + +static void setup(char *root, size_t root_size, int malformed) { + const char *tmp = getenv("TMPDIR"); + snprintf(root, root_size, "%s/t_fs_redirect.XXXXXX", tmp ? tmp : "/private/tmp"); + if (!mkdtemp(root)) { perror("mkdtemp"); exit(2); } + char rom[1200], arc[1200], archive[1300], overlay[1200], oarc[1200], oarchive[1300], sidecar[1300]; + snprintf(rom, sizeof rom, "%s/rom", root); + snprintf(arc, sizeof arc, "%s/arc", rom); + snprintf(overlay, sizeof overlay, "%s/overlay", root); + snprintf(oarc, sizeof oarc, "%s/arc", overlay); + mkdir(rom, 0755); mkdir(arc, 0755); mkdir(overlay, 0755); mkdir(oarc, 0755); + snprintf(archive, sizeof archive, "%s/data.trpfs", arc); + uint8_t data[256]; memset(data, 0, sizeof data); + uint64_t virtual_root = 64; memcpy(data + 8, &virtual_root, sizeof virtual_root); + memcpy(data + 32, "PACK-ONE-1234567", 16); + memcpy(data + 160, "PACK-TWO-REPLACEMENT-12345678901", 32); + memcpy(data + 224, "ROOT-TABLE-123456", 16); + write_bytes(archive, data, sizeof data); + snprintf(oarchive, sizeof oarchive, "%s/data.trpfs", oarc); + write_bytes(oarchive, data, sizeof data); + snprintf(sidecar, sizeof sidecar, "%s/redirects.tsv", root); + FILE *f = fopen(sidecar, "w"); + if (!f) { perror(sidecar); exit(2); } + fputs("DM_ROMFS_REDIRECTS_V1\tarc/data.trpfs\t4\t80\t256\n" + "0\t16\t0\t16\n" + "16\t16\t32\t16\n" + "32\t32\t160\t32\n" + "64\t16\t224\t16\n", f); + if (malformed) fputs("trailing\n", f); + fclose(f); + setenv("DM_ROMFS", rom, 1); + setenv("DM_ROMFS_OVERLAY", overlay, 1); + setenv("DM_ROMFS_REDIRECTS", sidecar, 1); +} + +static int malformed_child(void) { + char root[1024]; setup(root, sizeof root, 1); + if (h_fs_MountRom("data", NULL, 0) != RES_OK) return 3; + uint64_t h = 0; + // The malformed configured sidecar must terminate before a handle is + // returned; surviving this call is a test failure. + return h_fs_OpenFile(&h, "data:/arc/data.trpfs", MODE_READ) == RES_OK ? 0 : 4; +} + +int main(int argc, char **argv) { + if (argc == 2 && !strcmp(argv[1], "malformed")) return malformed_child(); + char root[1024]; setup(root, sizeof root, 0); + CHECK(h_fs_MountRom("data", NULL, 0) == RES_OK, "mount redirect fixture"); + uint64_t h = 0; size_t got = 0; char out[64] = {0}; int64_t file_size = 0; + CHECK(h_fs_OpenFile(&h, "data:/arc/data.trpfs", MODE_READ) == RES_OK, + "open configured archive"); + CHECK(h_fs_GetFileSize(&file_size, h) == RES_OK && file_size == 80, + "GetFileSize reports the 80-byte virtual archive"); + uint64_t read_root = 0; + CHECK(h_fs_ReadFile(&got, h, 0, out, 16) == RES_OK && got == 16 && + (memcpy(&read_root, out + 8, sizeof read_root), read_root == 64), + "header read stays physical and exposes the virtual root pointer"); + memset(out, 0, sizeof out); got = 0; + CHECK(h_fs_ReadFile(&got, h, 16, out, 16) == RES_OK && got == 16 && + !memcmp(out, "PACK-ONE-1234567", 16), "first virtual pack reads its source range"); + memset(out, 0, sizeof out); got = 0; + CHECK(h_fs_ReadFile(&got, h, 32, out, 32) == RES_OK && got == 32 && + !memcmp(out, "PACK-TWO-REPLACEMENT-12345678901", 32), + "second virtual pack reads its disjoint replacement range"); + memset(out, 0, sizeof out); got = 0; + CHECK(h_fs_ReadFile(&got, h, 64, out, 16) == RES_OK && got == 16 && + !memcmp(out, "ROOT-TABLE-123456", 16), "virtual EOF root reads its physical root range"); + uint8_t cross[48] = {0}, expected[48] = {0}; + memcpy(expected, &read_root, 8); memcpy(expected + 8, "PACK-ONE-1234567", 16); + memcpy(expected + 24, "PACK-TWO-REPLACEMENT-12345678901", 24); got = 0; + CHECK(h_fs_ReadFile(&got, h, 8, cross, sizeof cross) == RES_OK && got == sizeof cross && + !memcmp(cross, expected, sizeof cross), "one partial read crosses header and two pack rows"); + got = 99; + CHECK(h_fs_ReadFile(&got, h, 79, out, 2) == RES_FAIL && got == 0, + "read past virtual EOF fails closed"); + h_fs_CloseFile(h); + + DirectoryEntry entries[8]; int64_t nentry = 0; uint64_t directory = 0; + CHECK(h_fs_OpenDirectory(&directory, "data:/arc", DIRMODE_FILE) == RES_OK && + h_fs_ReadDirectory(&nentry, entries, directory, 8) == RES_OK && nentry == 1 && + !strcmp(entries[0].name, "data.trpfs") && entries[0].size == 80, + "overlay directory snapshot reports virtual file size"); + h_fs_CloseDirectory(directory); + unsetenv("DM_ROMFS_OVERLAY"); + CHECK(h_fs_MountRom("plain", NULL, 0) == RES_OK && + h_fs_OpenDirectory(&directory, "plain:/arc", DIRMODE_FILE) == RES_OK && + h_fs_ReadDirectory(&nentry, entries, directory, 8) == RES_OK && nentry == 1 && + !strcmp(entries[0].name, "data.trpfs") && entries[0].size == 80, + "ordinary directory enumeration reports virtual file size"); + h_fs_CloseDirectory(directory); + + pid_t child = fork(); + if (!child) { execl(argv[0], argv[0], "malformed", NULL); _exit(127); } + int status = 0; waitpid(child, &status, 0); + CHECK(!(WIFEXITED(status) && WEXITSTATUS(status) == 0), + "malformed redirect sidecar terminates before opening the archive"); + + char command[1200]; snprintf(command, sizeof command, "rm -rf '%s'", root); (void)system(command); + printf("== %d checks, %d failed ==\n", checks, fails); + return fails != 0; +} diff --git a/dm/host/tests/t_hid.c b/dm/host/tests/t_hid.c index 58b9b3b804..1ff3022084 100644 --- a/dm/host/tests/t_hid.c +++ b/dm/host/tests/t_hid.c @@ -21,6 +21,10 @@ static int hid_clock_gettime(clockid_t clock, struct timespec *ts) { (void)clock static int hid_usleep(useconds_t usec) { (void)usec; if (script_hold) script_hold(); return 0; } #define CHECK(c, msg) do { checks++; if (!(c)) { fails++; fprintf(stderr, "FAIL: %s (line %d)\n", msg, __LINE__); } } while (0) int dm_opmode(void) { return g_mode; } +void dm_set_opmode(int mode) { g_mode = mode; } +void dm_gfx_env_changed(const char *key, const char *value) { (void)key; (void)value; } +void dm_za_timing_env_changed(const char *key, const char *value) { (void)key; (void)value; } +void dm_audio_env_changed(const char *key, const char *value) { (void)key; (void)value; } uint64_t guest_ns(void) { return 1000000000ull; } uint64_t now_ns(void) { return motion_now; } uint64_t dm_guest_frame(void) { return 0; } diff --git a/dm/host/tests/t_map_headroom.c b/dm/host/tests/t_map_headroom.c new file mode 100644 index 0000000000..cface43da4 --- /dev/null +++ b/dm/host/tests/t_map_headroom.c @@ -0,0 +1,35 @@ +#include "../map_headroom.h" +#include +#include +#include + +int main(void) { + const uint64_t mb = 1ull << 20; + const uint64_t reserve = 384 * mb; + + assert(dm_map_headroom_default_bytes(1, 0) == 0); + assert(dm_map_headroom_default_bytes(1, 1) == 0); + assert(dm_map_headroom_default_bytes(0, 0) == 0); + + assert(!dm_map_headroom_should_refuse(0, 64 * mb, 0)); + assert(!dm_map_headroom_should_refuse(reserve - 1, 0, reserve)); + assert(dm_map_headroom_should_refuse(reserve - 1, 1, reserve)); + assert(!dm_map_headroom_should_refuse(reserve, 1, reserve)); + assert(!dm_map_headroom_should_refuse(reserve + 4 * mb, 4 * mb, reserve)); + assert(!dm_map_headroom_should_refuse(reserve + 4 * mb - 1, 4 * mb, reserve)); + + // A large virtual map does not immediately consume its length in physical + // memory. This is Z-A's startup arena shape and must not be rejected. + assert(!dm_map_headroom_should_refuse(2076 * mb, 0xc8ab9000ull, reserve)); + + // The b16 observations used for the explicit diagnostic experiment. + assert(!dm_map_headroom_should_refuse(394 * mb, 4 * mb, reserve)); + assert(dm_map_headroom_should_refuse(364 * mb, 4 * mb, reserve)); + + assert(!dm_map_headroom_should_refuse(UINT64_MAX, UINT64_MAX - 1, 1)); + assert(!dm_map_headroom_should_refuse(UINT64_MAX, UINT64_MAX, 1)); + assert(dm_map_headroom_should_refuse(1, UINT64_MAX, UINT64_MAX)); + + puts("map headroom: PASS"); + return 0; +} diff --git a/dm/host/tests/t_svc_memory.c b/dm/host/tests/t_svc_memory.c index 0ec29b1376..0e56e9bcff 100644 --- a/dm/host/tests/t_svc_memory.c +++ b/dm/host/tests/t_svc_memory.c @@ -57,6 +57,7 @@ static void (*write_prepare)(void *, size_t); static int (*fault_hook)(void *, int, void *); void dm_fault_hook_register(int (*fn)(void *, int, void *)) { fault_hook = fn; } void dm_guest_write_prep_register(void (*fn)(void *, size_t)) { write_prepare = fn; } +void dm_fault_diagnostic_register(void (*fn)(const void *)) { (void)fn; } static void memory_fault(int sig, siginfo_t *info, void *ctx) { if (fault_hook && fault_hook(info->si_addr, 1, ctx)) return; _exit(128 + sig); diff --git a/dm/host/tests/t_za_dynamic.c b/dm/host/tests/t_za_dynamic.c new file mode 100644 index 0000000000..2c412a497e --- /dev/null +++ b/dm/host/tests/t_za_dynamic.c @@ -0,0 +1,162 @@ +// Real mapped-data and clock-provider integration test. No GPU or game files. +// Each case runs in a fresh process because production initialization is once. +#include "../hle_za_timing.c" +#include +#include +#include + +static module_t test_modules[1]; +module_t *g_mod = test_modules; int g_nmod; +static uint64_t g_test_guest = 1000000000, g_test_wall = 1000000000; +static int g_detclock; +static void (*g_readonly)(uint64_t *, int); +static void (*g_mutable)(uint64_t *, int); +static void (*g_skill_visibility_observer)(uint64_t *, int); +static void (*g_skill_destroy_observer)(uint64_t *, int); +uint64_t now_ns(void) { return g_test_wall; } +uint64_t guest_ns(void) { return g_test_guest; } +int dm_detclock_enabled(void) { return g_detclock; } +int gate_obs_present(int id) { return id == OBS_ZA_CLOCK_DELTA || id == OBS_ZA_INTERVAL; } +void gate_obs_set(int id, void (*fn)(uint64_t *, int)) { + switch (id) { + case OBS_ZA_CLOCK_DELTA: + assert(!g_readonly && fn == za_clock_delta); g_readonly = fn; break; + case OBS_ZA_INTERVAL: + assert(!g_mutable && fn == za_interval); g_mutable = fn; break; + case OBS_ZA_SKILL_VISIBILITY: + assert(!g_skill_visibility_observer && fn == za_skill_visibility); g_skill_visibility_observer = fn; break; + case OBS_ZA_SKILL_DESTROY: + assert(!g_skill_destroy_observer && fn == za_skill_destroy); g_skill_destroy_observer = fn; break; + default: assert(!"unexpected read-only observation gate"); + } +} + +static uint64_t g_regs[31], g_original[31], *g_table; +static int64_t g_record[2], g_elapsed[2] = {0, 6000000}; +static uint8_t g_frame[0x48], g_config[0x22]; +static uint64_t g_consumed, g_pending; +static void table_ordered(void) { + for (int i = 1; i < 5; i++) assert(g_table[i - 1] <= g_table[i]); + assert(!memcmp(g_table + 2, k_za_duration + 2, 3 * sizeof *g_table)); +} +static void interval(int paused) { + memcpy(g_regs, g_original, sizeof g_regs); g_mutable(g_regs, 0); + for (int i = 0; i < 31; i++) if (i != 20 && i != 22) assert(g_regs[i] == g_original[i]); + assert(g_config[0x20] == 1 && !g_config[0x21]); + int code = (int32_t)g_regs[20] > (int32_t)g_regs[22] ? (int)g_regs[20] : (int)g_regs[22]; + assert(code >= 0 && code < 5); if (g_za_table) table_ordered(); + // Original provider calls SetDeltaNs(0) on disabled channels. The optional + // controller changes the table lookup, not this channel pause decision. + g_pending = paused ? 0 : __atomic_load_n(&g_table[code], __ATOMIC_ACQUIRE); +} +static void consume_then_interval(uint64_t dt, uint64_t wall_extra, int paused) { + g_test_guest += dt; g_test_wall += dt + wall_extra; + g_record[1] = (int64_t)g_pending; g_consumed += g_pending; + g_readonly(g_original, 0); interval(paused); +} +static void setup(void) { + g_nmod = 1; module_t *m = &g_mod[0]; strcpy(m->name, "main"); + strcpy(m->build_id, "b1f12fd919eae86ab8a978317677e64b"); + m->size = 0x4000000; m->text_end = 0x3163f70; m->ro_off = 0x3164000; m->data_off = 0x3bc2000; + m->base = mmap(NULL, m->size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); + assert(m->base != MAP_FAILED); g_table = (uint64_t *)(m->base + ZA_DURATION_OFFSET); + memcpy(g_table, k_za_duration, sizeof k_za_duration); + uint64_t config = (uint64_t)(uintptr_t)g_config; memcpy(g_frame + 0x40, &config, 8); g_config[0x20] = 1; + for (int i = 0; i < 31; i++) g_original[i] = 0x1000 + i; + g_original[0] = (uint64_t)(uintptr_t)g_record; + g_original[19] = (uint64_t)(uintptr_t)g_frame; + g_original[21] = (uint64_t)(uintptr_t)g_elapsed; + g_original[20] = g_original[22] = 1; + setenv("DM_ZA_TIMING", "1", 1); setenv("DM_ZA_FPS_MODE", "dynamic60", 1); +} +int main(int argc, char **argv) { + assert(argc == 2); const char *test = argv[1]; setup(); + assert(g_skill_visibility_observer && g_skill_destroy_observer); + int rejection = 1; + if (!strcmp(test, "wrong-layout")) g_mod[0].ro_off++; + else if (!strcmp(test, "wrong-build")) g_mod[0].build_id[0] = '0'; + else if (!strcmp(test, "wrong-table")) g_table[4]++; + else if (!strcmp(test, "unaligned")) g_mod[0].base++; + else if (!strcmp(test, "detclock")) g_detclock = 1; + else if (!strcmp(test, "readonly") || !strcmp(test, "executable")) { + uintptr_t page = (uintptr_t)g_table & ~((uintptr_t)getpagesize() - 1); + int prot = PROT_READ | (!strcmp(test, "executable") ? PROT_EXEC : 0); + assert(!mprotect((void *)page, (size_t)getpagesize(), prot)); + } else { assert(!strcmp(test, "clock") || !strcmp(test, "clock120") || !strcmp(test, "conflict") || !strcmp(test, "guard")); rejection = 0; } + uint64_t before[5]; memcpy(before, g_table, sizeof before); + interval(0); + if (rejection) { + assert(atomic_load(&g_za_mode) == ZA_GAME && !g_za_table); + assert(!memcmp(before, g_table, sizeof before)); + assert(g_regs[20] == 1 && g_regs[22] == 1); + } else if (!strcmp(test, "conflict")) { + consume_then_interval(27000000, 0, 0); + g_table[1] = 31000001; // a foreign writer must retain ownership + consume_then_interval(27000000, 0, 0); + assert(atomic_load(&g_za_mode) == ZA_GAME && !g_za_table); + assert(g_table[0] == k_za_duration[0] && g_table[1] == 31000001); + assert(g_regs[20] == 1 && g_regs[22] == 1); + } else if (!strcmp(test, "guard")) { + g_config[0x21] = 1; g_elapsed[1] = 1000000001; + memcpy(g_regs, g_original, sizeof g_regs); g_mutable(g_regs, 0); + assert(atomic_load(&g_za_mode) == ZA_GAME && !g_za_table); + assert(!memcmp(g_table, k_za_duration, sizeof k_za_duration)); + assert(g_regs[20] == 1 && g_regs[22] == 1); + } else { + assert(g_za_table && g_regs[20] == 0 && g_regs[22] == 0); + // Consume the startup seed, then compare real engine time with every + // whole-frame input, with the original one-frame provider latency. + const uint64_t normal[] = {24000000,30000000,26500000,28500000}; + const uint64_t high_refresh[] = {8333333,8333334,7500000,9166667}; + const uint64_t *pattern = !strcmp(test, "clock120") ? high_refresh : normal; + uint64_t active = 0; + for (int i = 0; i < 3600; i++) { + uint64_t dt = pattern[i % 4]; active += dt; + if (!(i % 23)) { + uint64_t samples = g_za_frame.samples, pending = g_pending; + int64_t debt = g_za_frame.debt; + dm_za_timing_env_changed("DM_ZA_FPS_MODE", "dynamic60"); + assert(g_za_frame.samples == samples && g_za_frame.debt == debt && g_pending == pending); + } + consume_then_interval(dt, 0, 0); + assert(g_regs[20] == 0); // graphics code 0 keeps native interval 1 + // Add the pending step to close the genuine provider's one-frame + // latency. Diagnostic warmup resets every 23 frames, yet this + // consumed total still conserves the game clock across all frames. + int64_t error = (int64_t)active - (int64_t)(g_consumed + g_pending - ZA_FRAME_SEED_NS); + assert(llabs(error) <= ZA_FRAME_PHASE_NS); + // The clock has genuinely consumed only the preceding step. Check + // that unadjusted total too: its error is at most one pending frame + // plus the same phase bound, across every diagnostic epoch. + int64_t actual_error = (int64_t)active - (int64_t)(g_consumed - ZA_FRAME_SEED_NS); + assert(llabs(actual_error) <= (int64_t)ZA_FRAME_MAX_NS + ZA_FRAME_PHASE_NS); + } + assert(g_za_frame.samples == 3600 && !g_za_frame.limited_ns); + double ratio = (double)(g_consumed + g_pending - ZA_FRAME_SEED_NS) / active; + double tolerance = (double)ZA_FRAME_PHASE_NS / active + 1e-12; + assert(ratio >= 1 - tolerance && ratio <= 1 + tolerance); + double actual_ratio = (double)(g_consumed - ZA_FRAME_SEED_NS) / active; + assert(actual_ratio > .9994 && actual_ratio < 1.0006); + printf("actual consumed clock across epochs: ratio=%.9f; one-frame-latency-adjusted ratio=%.9f\n", actual_ratio, ratio); + // Disable the channel before backgrounding; disabled calls stay zero. + interval(1); uint64_t consumed = g_consumed; + consume_then_interval(0, 300000000000ull, 1); + assert(g_consumed == consumed && g_pending == 0 && (g_za_step.flags & ZA_FRAME_FROZEN)); + consume_then_interval(27000000, 0, 0); + assert(!g_record[1] && g_pending <= 31000000); + consume_then_interval(27000000, 60000000000ull, 0); + assert(g_pending <= 31000000 && (g_za_step.flags & ZA_FRAME_RESUMED)); + consume_then_interval(1000000000, 0, 0); + assert(g_za_step.flags & ZA_FRAME_LIMITED); assert(g_pending <= 50000000); + // Returning to every existing mode restores original values first. + dm_za_timing_env_changed("DM_ZA_FPS_MODE", "static60"); + assert(!g_za_table && !memcmp(g_table, k_za_duration, sizeof k_za_duration)); + interval(0); assert(g_regs[20] == 0 && g_regs[22] == 0); + dm_za_timing_env_changed("DM_ZA_FPS_MODE", "dynamic60"); interval(0); + assert(g_za_table && g_regs[20] == 0); + dm_za_timing_env_changed("DM_ZA_FPS_MODE", "game"); + assert(!g_za_table && !memcmp(g_table, k_za_duration, sizeof k_za_duration)); + interval(0); assert(g_regs[20] == 1 && g_regs[22] == 1); + } + printf("Z-A dynamic %s: PASS\n", test); return 0; +} diff --git a/dm/host/tests/t_za_frame_delta.c b/dm/host/tests/t_za_frame_delta.c new file mode 100644 index 0000000000..ecc06d3dff --- /dev/null +++ b/dm/host/tests/t_za_frame_delta.c @@ -0,0 +1,84 @@ +// clang -O1 -Wall -Wextra -Werror -o /tmp/t_za_frame_delta t_za_frame_delta.c +#include "../za_frame_delta.h" +#include +#include +#include + +static void invariant(const za_frame_delta_state *s, za_frame_delta_result r) { + assert(llabs(s->debt) <= ZA_FRAME_PHASE_NS); + assert((int64_t)(s->raw_sum - s->limited_ns - s->step_sum) == s->debt); + if (r.flags & ZA_FRAME_FROZEN) { assert(!r.step_ns); return; } + assert(r.step_ns && r.step_ns <= ZA_FRAME_MAX_NS); + uint64_t table[] = {16666667, 33333334, 50000000, 66666667, 83333334}; + table[r.code] = r.step_ns; + for (int i = 1; i < 5; i++) assert(table[i - 1] <= table[i]); +} +static void sequence(const char *name, const uint64_t *pattern, size_t n) { + za_frame_delta_state s = {0}; uint64_t guest = 1000000000, wall = guest; + za_frame_delta_result r = za_frame_delta_advance(&s, guest, wall); + assert(r.flags == ZA_FRAME_SEED); + // This independent game-clock total includes every emitted step, including + // the seed. No diagnostic epoch/sample counter participates in the check. + uint64_t consumed = r.step_ns, active = 0; + for (int i = 0; i < 12000; i++) { + uint64_t dt = pattern[i % n]; guest += dt; wall += dt; active += dt; + r = za_frame_delta_advance(&s, guest, wall); invariant(&s, r); + consumed += r.step_ns; + assert(llabs((int64_t)(active - (consumed - ZA_FRAME_SEED_NS))) <= ZA_FRAME_PHASE_NS); + } + double ratio = (double)(consumed - ZA_FRAME_SEED_NS) / active; + double tolerance = (double)ZA_FRAME_PHASE_NS / active + 1e-12; + assert(ratio >= 1 - tolerance && ratio <= 1 + tolerance); + printf("%s: consumed/active=%.9f error=%lld ns\n", name, ratio, + (long long)((int64_t)active - (int64_t)(consumed - ZA_FRAME_SEED_NS))); +} +int main(void) { + const uint64_t sixty[] = {16666667,16666666,16666667}; + const uint64_t one_twenty[] = {8333333,8333334,8333333}; + const uint64_t high_jitter[] = {7500000,9166667,8333333,8000000,8666667}; + const uint64_t steady[] = {27000000}; + const uint64_t jitter[] = {24000000,30000000}; + const uint64_t skew[] = {12000000,40000000,24000000,32000000,27000000}; + const uint64_t positive[] = {27500000,29000000,31500000,30000000,28000000}; + const uint64_t negative[] = {26500000,25000000,22500000,24000000,26000000}; + const uint64_t extremes[] = {1,50000000,16666667,33333334,50000000,1}; + sequence("60 FPS measured time", sixty, 3); + sequence("120 FPS measured time", one_twenty, 3); + sequence("120 FPS periodic jitter", high_jitter, 5); + sequence("37 FPS steady", steady, 1); sequence("signed 3 ms jitter", jitter, 2); + sequence("asymmetric timing", skew, 5); sequence("positive periodic jitter", positive, 5); + sequence("negative periodic jitter", negative, 5); sequence("valid boundaries", extremes, 6); + + za_frame_delta_state s = {0}; uint64_t guest = 1000000000, wall = guest; + za_frame_delta_advance(&s, guest, wall); + for (int i = 0; i < 20; i++) { + guest += 27000000; wall += 27000000; + invariant(&s, za_frame_delta_advance(&s, guest, wall)); + } + uint64_t before = s.step_sum; + wall += 300000000000ull; // guest clock is frozen during five minutes in background + za_frame_delta_result r = za_frame_delta_advance(&s, guest, wall); + assert((r.flags & (ZA_FRAME_RESUMED | ZA_FRAME_FROZEN)) == (ZA_FRAME_RESUMED | ZA_FRAME_FROZEN)); + assert(s.step_sum == before && !r.step_ns); invariant(&s, r); + guest += 27000000; wall += 27000000; + r = za_frame_delta_advance(&s, guest, wall); invariant(&s, r); + assert(r.step_ns <= 29000000 && !(r.flags & ZA_FRAME_SEED)); + // Resume may occur between two callbacks, with no callback while frozen. + guest += 27000000; wall += 60027000000ull; + r = za_frame_delta_advance(&s, guest, wall); invariant(&s, r); + assert(r.flags & ZA_FRAME_RESUMED); assert(r.step_ns <= 29000000); + // A real long active stall is capped explicitly; its loss is observable, + // and no subsequent catch-up frame can accelerate the game. + guest += 2000000000; wall += 2000000000; + r = za_frame_delta_advance(&s, guest, wall); invariant(&s, r); + assert(r.flags & ZA_FRAME_LIMITED); assert(s.limited_ns == 1950000000); + for (int i = 0; i < 40; i++) { + guest += 27000000; wall += 27000000; + r = za_frame_delta_advance(&s, guest, wall); invariant(&s, r); + assert(r.step_ns <= 31000000); + } + r = za_frame_delta_advance(&s, guest - 1, wall); invariant(&s, r); + assert(r.flags & ZA_FRAME_CLOCK_RESET); + puts("Z-A frame delta: PASS"); + return 0; +} diff --git a/dm/host/tests/t_za_timing.c b/dm/host/tests/t_za_timing.c new file mode 100644 index 0000000000..9292abbb72 --- /dev/null +++ b/dm/host/tests/t_za_timing.c @@ -0,0 +1,141 @@ +// Standalone host test of opt-in timing hooks and saved-register preservation. +// clang -O1 -Wall -I dm/host -I -o /tmp/t_za_timing dm/host/tests/t_za_timing.c +// Run each case in a fresh process: off, trace, static-fixed, static-adaptive, +// native-adaptive, wrong-build, live, concurrent. No game inputs/device needed. +#include "../hle_za_timing.c" +#include + +static module_t test_modules[1]; +module_t *g_mod = test_modules; int g_nmod; +static _Atomic uint64_t g_test_ns = 1000000000ull; +static void (*g_readonly)(uint64_t *, int); +static void (*g_mutable)(uint64_t *, int); +static void (*g_skill_visibility_observer)(uint64_t *, int); +static void (*g_skill_destroy_observer)(uint64_t *, int); +uint64_t now_ns(void) { return atomic_load(&g_test_ns); } +uint64_t guest_ns(void) { return atomic_load(&g_test_ns); } +int dm_detclock_enabled(void) { return 0; } +int gate_obs_present(int id) { return id == OBS_ZA_CLOCK_DELTA || id == OBS_ZA_INTERVAL; } +void gate_obs_set(int id, void (*fn)(uint64_t *, int)) { + switch (id) { + case OBS_ZA_CLOCK_DELTA: + assert(!g_readonly && fn == za_clock_delta); g_readonly = fn; break; + case OBS_ZA_INTERVAL: + assert(!g_mutable && fn == za_interval); g_mutable = fn; break; + case OBS_ZA_SKILL_VISIBILITY: + assert(!g_skill_visibility_observer && fn == za_skill_visibility); g_skill_visibility_observer = fn; break; + case OBS_ZA_SKILL_DESTROY: + assert(!g_skill_destroy_observer && fn == za_skill_destroy); g_skill_destroy_observer = fn; break; + default: assert(!"unexpected read-only observation gate"); + } +} + +static void clocks(uint64_t *regs, int count) { + for (int i = 0; i < count; i++) { g_test_ns += 16666667; g_readonly(regs, 0); } +} +static uint64_t g_worker_regs[31]; +static _Atomic int g_worker_started, g_switches; +static void *observe_concurrently(void *unused) { + (void)unused; uint64_t regs[31]; + atomic_store(&g_worker_started, 1); + for (int i = 0; i < 3000 || atomic_load(&g_switches) < 1000; i++) { + memcpy(regs, g_worker_regs, sizeof regs); g_mutable(regs, 0); + assert((regs[20] == 1 && regs[22] == 1) || + (regs[20] == 0 && (regs[22] == 0 || regs[22] == 2))); + for (int r = 0; r < 31; r++) if (r != 20 && r != 22) assert(regs[r] == g_worker_regs[r]); + clocks(regs, 1); + } + return NULL; +} + +int main(int argc, char **argv) { + assert(argc == 2); const char *test = argv[1]; + assert(g_skill_visibility_observer && g_skill_destroy_observer); + unsetenv("DM_ZA_TIMING"); unsetenv("DM_ZA_FPS_MODE"); + g_nmod = 1; strcpy(g_mod[0].name, "main"); + strcpy(g_mod[0].build_id, "b1f12fd919eae86ab8a978317677e64b"); + int native_adaptive = !strcmp(test, "static-adaptive"), change = 0, want22 = 1; + if (!strcmp(test, "trace") || !strcmp(test, "live") || !strcmp(test, "concurrent")) setenv("DM_ZA_TIMING", "1", 1); + else if (!strcmp(test, "static-fixed") || native_adaptive) { + setenv("DM_ZA_FPS_MODE", "static60", 1); change = 1; + want22 = native_adaptive ? 2 : 0; + } else if (!strcmp(test, "native-adaptive")) { + setenv("DM_ZA_FPS_MODE", "adaptive60", 1); change = 1; want22 = 2; + } else if (!strcmp(test, "wrong-build")) { + setenv("DM_ZA_FPS_MODE", "static60", 1); + strcpy(g_mod[0].build_id, "00000000000000000000000000000000"); + } else assert(!strcmp(test, "off")); + + const int64_t input[] = {-1, 0, 16666667, 16666668, 33333334, 33333335, + 50000000, 50000001, 66666667, 66666668}; + const int output[] = {0, 0, 0, 1, 1, 2, 2, 3, 3, 4}; + for (unsigned i = 0; i < sizeof input / sizeof *input; i++) assert(za_elapsed_code(input[i]) == output[i]); + + uint8_t frame[0x48] = {0}, config[0x22] = {0}; + int64_t elapsed[2] = {0, 35000000}; + config[0x20] = 1; config[0x21] = native_adaptive; + uint64_t pointer = (uint64_t)(uintptr_t)config; memcpy(frame + 0x40, &pointer, sizeof pointer); + uint64_t regs[31], expected[31]; + for (int i = 0; i < 31; i++) regs[i] = 0x1000 + i; + regs[19] = (uint64_t)(uintptr_t)frame; regs[21] = (uint64_t)(uintptr_t)elapsed; + regs[20] = 1; regs[22] = native_adaptive ? 2 : 1; + memcpy(expected, regs, sizeof regs); + if (change) { expected[20] = 0; expected[22] = want22; } + assert(g_readonly && g_mutable); g_mutable(regs, 0); + assert(!memcmp(regs, expected, sizeof regs)); + assert(config[0x20] == 1 && config[0x21] == native_adaptive); + + if (g_za_valid) { + int64_t record[2] = {0, 33333333}; + regs[0] = (uint64_t)(uintptr_t)record; clocks(regs, 2); + assert(g_za.calls == 0 && g_za_warmup == 0); + record[1] = 16666667; clocks(regs, 2); + assert(g_za.calls == 2 && g_za.sum_ns == 16666667 && g_za.invalid == 0); + if (!strcmp(test, "live")) { + uint64_t before = atomic_load(&g_za_epoch); + dm_za_timing_env_changed("UNRELATED", "static60"); + dm_za_timing_env_changed("DM_ZA_FPS_MODE", "unknown"); + assert(atomic_load(&g_za_epoch) == before && g_za.calls == 2); + dm_za_timing_env_changed("DM_ZA_FPS_MODE", "static60"); + assert(atomic_load(&g_za_epoch) == before + 1 && g_za.calls == 0 && g_za.sum_ns == 0); + assert(atomic_load(&g_za_mode) == ZA_STATIC60); + clocks(regs, 1); assert(g_za.calls == 0 && g_za_warmup == 2); // no new-mode interval yet + regs[20] = regs[22] = 1; g_mutable(regs, 0); + assert(regs[20] == 0 && regs[22] == 0); + clocks(regs, 4); assert(g_za.calls == 2 && g_za.sum_ns == 16666667); + g_test_ns += 10000000000ull; // pause before explicit reset must not enter the new epoch + dm_za_timing_env_changed("DM_ZA_FPS_MODE", "adaptive60"); + regs[20] = regs[22] = 1; g_mutable(regs, 0); assert(regs[20] == 0 && regs[22] == 2); + clocks(regs, 4); assert(g_za.calls == 2 && g_za.sum_ns == 16666667); + assert(now_ns() - g_za.start_wall == 16666667); + dm_za_timing_env_changed("DM_ZA_FPS_MODE", "game"); + regs[20] = regs[22] = 1; g_mutable(regs, 0); assert(regs[20] == 1 && regs[22] == 1); + before = atomic_load(&g_za_epoch); + dm_za_timing_env_changed("DM_ZA_FPS_MODE", "game"); // same-mode command resets after pause/loading + assert(atomic_load(&g_za_epoch) == before + 1 && g_za.calls == 0); + } else if (!strcmp(test, "concurrent")) { + memcpy(g_worker_regs, regs, sizeof regs); g_worker_regs[20] = g_worker_regs[22] = 1; + pthread_t worker; assert(!pthread_create(&worker, NULL, observe_concurrently, NULL)); + while (!atomic_load(&g_worker_started)) sched_yield(); + const char *modes[] = {"game", "static60", "adaptive60"}; + for (int i = 0; i < 1000; i++) { + dm_za_timing_env_changed("DM_ZA_FPS_MODE", modes[i % 3]); + atomic_store(&g_switches, i + 1); + } + assert(!pthread_join(worker, NULL)); + dm_za_timing_env_changed("DM_ZA_FPS_MODE", "game"); + assert(atomic_load(&g_za_mode) == ZA_GAME && g_za.calls == 0 && g_za.sum_ns == 0); + } else if (!atomic_load(&g_za_live_allowed)) { + int prior = atomic_load(&g_za_mode); uint64_t before = atomic_load(&g_za_epoch); + dm_za_timing_env_changed("DM_ZA_FPS_MODE", "game"); + assert(atomic_load(&g_za_mode) == prior && atomic_load(&g_za_epoch) == before); + } + } else assert(!strcmp(test, "off") || !strcmp(test, "wrong-build")); + if (!g_za_valid) { + int prior = atomic_load(&g_za_mode); uint64_t before = atomic_load(&g_za_epoch); + dm_za_timing_env_changed("DM_ZA_FPS_MODE", "static60"); + assert(atomic_load(&g_za_mode) == prior && atomic_load(&g_za_epoch) == before); + } + printf("Z-A timing %s: PASS\n", test); + return 0; +} diff --git a/dm/host/za_frame_delta.h b/dm/host/za_frame_delta.h new file mode 100644 index 0000000000..aa1fbc3b48 --- /dev/null +++ b/dm/host/za_frame_delta.h @@ -0,0 +1,84 @@ +// Whole-frame active-time controller for Z-A's optional dynamic60 experiment. +// Independent of Mach, game memory, and diagnostics so clock conservation can +// be tested against the deltas actually consumed by a model clock provider. +#pragma once +#include +#include + +#define ZA_FRAME_SEED_NS 16666667ull +#define ZA_FRAME_MAX_NS 50000000ull +#define ZA_FRAME_PHASE_NS 2000000ll +#define ZA_FRAME_SUSPEND_NS 250000000ull +enum { + ZA_FRAME_SEED = 1, ZA_FRAME_FROZEN = 2, ZA_FRAME_LIMITED = 4, + ZA_FRAME_RESUMED = 8, ZA_FRAME_CLOCK_RESET = 16 +}; +typedef struct { + uint64_t guest, wall; + int64_t ema, debt; + int initialized; + uint64_t samples, raw_sum, step_sum, limited_ns, frozen, resumed, resets, seeds; +} za_frame_delta_state; +typedef struct { + uint64_t raw_ns, step_ns; + unsigned flags; + int code; +} za_frame_delta_result; + +static inline void za_frame_delta_reset(za_frame_delta_state *s) { + memset(s, 0, sizeof *s); +} +static inline za_frame_delta_result za_frame_delta_advance( + za_frame_delta_state *s, uint64_t guest, uint64_t wall) { + za_frame_delta_result r = {0}; + if (!s->initialized || guest < s->guest || wall < s->wall) { + r.flags = ZA_FRAME_SEED; + if (s->initialized) { r.flags |= ZA_FRAME_CLOCK_RESET; s->resets++; } + s->initialized = 1; s->guest = guest; s->wall = wall; + s->ema = ZA_FRAME_SEED_NS; s->seeds++; + // A seed has no measured preceding frame. Keep it out of conservation + // totals, and report it explicitly instead of pretending it was timed. + r.step_ns = ZA_FRAME_SEED_NS; + return r; + } + uint64_t raw = guest - s->guest, real = wall - s->wall; + s->guest = guest; s->wall = wall; r.raw_ns = raw; + if (real > raw && real - raw > ZA_FRAME_SUSPEND_NS) { + r.flags |= ZA_FRAME_RESUMED; s->resumed++; s->ema = 0; + } + if (!raw) { + r.flags |= ZA_FRAME_FROZEN; s->frozen++; s->ema = 0; + // Do not write a zero duration into the shared table: other original + // callers divide by it. The caller leaves the native pause path alone. + return r; + } + s->samples++; s->raw_sum += raw; + int64_t bounded = (int64_t)(raw > ZA_FRAME_MAX_NS ? ZA_FRAME_MAX_NS : raw); + if (raw > ZA_FRAME_MAX_NS) { + r.flags |= ZA_FRAME_LIMITED; s->limited_ns += raw - ZA_FRAME_MAX_NS; + // Preserve the ordering before the untouched native 50 ms table tier. + // Long stalls are explicitly limited, not repaid as acceleration later. + s->ema = bounded; + } else if (!s->ema) s->ema = bounded; + else s->ema += (bounded - s->ema) / 4; + + // An EMA alone loses speed when frame time changes. Carry signed phase + // error and constrain it to +/-2 ms. Thus raw_sum - limited_ns - step_sum + // always equals debt, even for asymmetric or periodically signed jitter. + int64_t owed = s->debt + bounded; + int64_t lo = owed - ZA_FRAME_PHASE_NS, hi = owed + ZA_FRAME_PHASE_NS; + if (lo < 1) lo = 1; + if (hi > (int64_t)ZA_FRAME_MAX_NS) hi = ZA_FRAME_MAX_NS; + int64_t step = s->ema; + if (step < lo) step = lo; + if (step > hi) step = hi; + s->debt = owed - step; s->step_sum += (uint64_t)step; + r.step_ns = (uint64_t)step; + + // Only slots 0 and 1 may change. Keep the entire original duration table + // ordered, including whichever of those two slots remains nominal. + r.code = s->ema > 21739130; // 46 FPS, with no FPS integer quantization. + if (r.step_ns < ZA_FRAME_SEED_NS) r.code = 0; + if (r.step_ns > 33333334ull) r.code = 1; + return r; +} diff --git a/dm/ios/Sources/DmAudioRetry.h b/dm/ios/Sources/DmAudioRetry.h new file mode 100644 index 0000000000..36c4a79aba --- /dev/null +++ b/dm/ios/Sources/DmAudioRetry.h @@ -0,0 +1,27 @@ +// Main-queue audio-session retry policy, independent of UIKit/AVFoundation so +// lifecycle cancellation and rate-limited retry behavior can be tested without I/O. +#pragma once +#include +#include +enum { DM_AUDIO_SESSION_SOLO, DM_AUDIO_SESSION_PLAYBACK_MIX }; +// Unset/empty remains the original SoloAmbient default. A malformed diagnostic +// value must not silently change session policy. +static inline int dm_audio_session_category_parse(const char *value) { + if (!value || !*value || !strcmp(value, "solo")) return DM_AUDIO_SESSION_SOLO; + if (!strcmp(value, "playback_mix")) return DM_AUDIO_SESSION_PLAYBACK_MIX; + return -1; +} +typedef struct { uint64_t generation; unsigned attempts; int pending; } dm_audio_retry; +static inline void dm_audio_retry_reset(dm_audio_retry *r) { + r->generation++; r->attempts = 0; r->pending = 0; +} +static inline unsigned dm_audio_retry_plan(dm_audio_retry *r, int blocked, int off, uint64_t *token) { + if (blocked || !off || r->pending) return 0; + unsigned milliseconds = 1000u << (r->attempts < 3 ? r->attempts : 3); + if (r->attempts != UINT32_MAX) r->attempts++; + r->pending = 1; *token = r->generation; return milliseconds; +} +static inline int dm_audio_retry_fire(dm_audio_retry *r, uint64_t token, int blocked, int off) { + if (token != r->generation || !r->pending) return 0; + r->pending = 0; return !blocked && off; +} diff --git a/dm/ios/Sources/DmDisplayRefresh.h b/dm/ios/Sources/DmDisplayRefresh.h new file mode 100644 index 0000000000..94704259c2 --- /dev/null +++ b/dm/ios/Sources/DmDisplayRefresh.h @@ -0,0 +1,60 @@ +// Display-request policy and timestamp measurements only. No game clock, +// frame generation, drawable presentation or NVN pacing is controlled here. +#pragma once +#include +#include +#include + +static inline int dm_display_hz_parse(const char *value) { + if (!value || !*value) return 0; // retain the system's existing policy + if (!strcmp(value, "60")) return 60; + if (!strcmp(value, "120")) return 120; + return -1; +} +static inline int dm_display_hz_select(int requested, int supported_max) { + if (requested != 60 && requested != 120) return 0; + if (supported_max <= 0) return 60; + return supported_max < requested ? supported_max : requested; +} + +typedef struct { + double start, last, interval_min, interval_max, target_sum; + uint64_t intervals, target_intervals, presented_start; +} dm_display_probe; +typedef struct { + double seconds, callback_hz, target_period_ms, min_period_ms, max_period_ms, presented_fps; + uint64_t callback_intervals, presented_frames; +} dm_display_probe_result; +static inline void dm_display_probe_reset(dm_display_probe *probe) { + *probe = (dm_display_probe){0}; +} +static inline void dm_display_probe_seed(dm_display_probe *probe, double timestamp, uint64_t presented) { + *probe = (dm_display_probe){.start=timestamp, .last=timestamp, .presented_start=presented}; +} +static inline int dm_display_probe_tick(dm_display_probe *probe, double timestamp, + double target_timestamp, uint64_t presented, dm_display_probe_result *result) { + if (!isfinite(timestamp) || timestamp <= 0) return 0; + double delta = timestamp - probe->last; + // A pause, clock discontinuity or counter reset starts a new window. Long + // background time must not lower the reported foreground callback cadence. + if (!probe->last || delta <= 0 || delta > 0.25 || presented < probe->presented_start) { + dm_display_probe_seed(probe, timestamp, presented); return 0; + } + if (!probe->intervals || delta < probe->interval_min) probe->interval_min = delta; + if (delta > probe->interval_max) probe->interval_max = delta; + probe->intervals++; probe->last = timestamp; + double target = target_timestamp - timestamp; + if (isfinite(target) && target > 0 && target <= 0.25) { + probe->target_sum += target; probe->target_intervals++; + } + double elapsed = timestamp - probe->start; + if (elapsed < 2.0) return 0; + *result = (dm_display_probe_result){ + .seconds=elapsed, .callback_hz=probe->intervals / elapsed, + .target_period_ms=probe->target_intervals ? 1000 * probe->target_sum / probe->target_intervals : 0, + .min_period_ms=1000 * probe->interval_min, .max_period_ms=1000 * probe->interval_max, + .presented_fps=(presented - probe->presented_start) / elapsed, + .callback_intervals=probe->intervals, .presented_frames=presented - probe->presented_start}; + dm_display_probe_seed(probe, timestamp, presented); + return 1; +} diff --git a/dm/ios/Sources/DmFaceLayout.h b/dm/ios/Sources/DmFaceLayout.h new file mode 100644 index 0000000000..58db2a20e4 --- /dev/null +++ b/dm/ios/Sources/DmFaceLayout.h @@ -0,0 +1,21 @@ +// Display and hit-test geometry only. Native face-button bits never change. +#pragma once +typedef struct { int skill; } dm_face_layout; +typedef struct { int x, y; } dm_face_offset; + +static inline int dm_face_layout_update(dm_face_layout *layout, int requested, int face_owned) { + requested = requested != 0; + // A touch can own the diamond while resting between its keys. Wait for + // every such touch to end, not merely for the currently pressed bits to clear. + if (face_owned || layout->skill == requested) return 0; + layout->skill = requested; + return 1; +} + +static inline dm_face_offset dm_face_layout_offset(const dm_face_layout *layout, unsigned button) { + // A/B/X/Y. Positive y is downward in UIKit. During explicit move selection + // the frontend matches the original game's A right/B bottom/X top/Y left. + static const dm_face_offset normal[4] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; + static const dm_face_offset skill[4] = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}}; + return button < 4 ? (layout->skill ? skill[button] : normal[button]) : (dm_face_offset){0, 0}; +} diff --git a/dm/ios/Sources/DmGamepad.m b/dm/ios/Sources/DmGamepad.m index ad01002dbf..f0d142a13b 100644 --- a/dm/ios/Sources/DmGamepad.m +++ b/dm/ios/Sources/DmGamepad.m @@ -1,16 +1,18 @@ -// Virtual gamepad (Nintendo button positions) + physical controllers (GameController). Both are merged and handed to the host through dm_input_set (hle_hid.c, bit layout in dm_input.h). +// Virtual gamepad + physical controllers (GameController). Native face-button +// functions stay unchanged. Only the virtual diamond's geometry follows the +// original battle move HUD during explicit skill selection. // Two layouts, three in handheld mode; one tap on the layout button at the top centre (labelled with the current layout, "2D" / "3D" / "Tap") goes to the next, and the choice is kept in NSUserDefaults (each game is its own app, so per game); the first launch uses the game profile's pad_layout (DM_GAME_PAD_LAYOUT). // Tap (touch): no control but the layout button, every other touch goes to the game's touch screen; offered only while the game is in handheld mode, the only one in which it reads the touch screen (Balatro, played by touch). -// 2D (simple): top corners ZL above L (one column), ZR above R likewise, teardrops with the tip toward the screen corner; − and + side by side at the top centre; the left stick in the bottom-left corner (fixed ring, tilt measured from the ring centre, small dead zone); -// the A (right) B (bottom) X (top) Y (left) diamond in the bottom-right corner, sliding a finger between keys switches keys. Each bottom corner holds a single control, so it sits close in (reaching halfway into the side safe-area inset). -// 3D (full, Pro Controller style): everything in 2D (left stick and diamond keep their place and size), plus a D-pad (inward of and below the left stick, 8-way), a right stick that is not drawn -// (a touch landing in the right third of the screen on no control holds it: centred where it lands, the centre dragged along once the finger passes the stick radius) and L3 / R3 (small round keys in the top row, inward of ZL / ZR). +// 2D (simple): top corners ZL above L (one column), ZR above R likewise, teardrops with the tip toward the screen corner; − and + side by side at the top centre; the left stick in the bottom-left corner (fixed ring, tilt measured from the ring centre, small dead zone), with a separate 8-way D-pad immediately inward; +// the A (bottom) B (right) X (left) Y (top) diamond in the bottom-right corner, sliding a finger between keys switches keys. Each bottom corner holds a single control, so it sits close in (reaching halfway into the side safe-area inset). +// Both 2D and 3D have camera drag on empty space in the right half: a right stick that is not drawn, centred where the touch lands, with the centre dragged along past the stick radius. +// 3D (full, Pro Controller style): everything in 2D (left stick, D-pad and diamond keep their place and size), plus L3 / R3 (small round keys in the top row, inward of ZL / ZR). // Breath of the Wild crouches with L3 and uses the scope with R3; 3D World and Breath of the Wild turn the camera with the right stick. // Touch-down ownership: locked keys first (shoulders, − / +, the layout button, L3 / R3), then whichever of the diamond, the D-pad and the left stick has the smallest distance / grab radius (the left stick's grab circle borders the D-pad), -// then in 3D the right stick for a touch in the right third. +// then the camera for an unclaimed touch in the right half. Ownership stays with its original control until release/cancellation; sliding a camera finger over ABXY never presses a skill. // Everything hugs the edges and corners to leave the picture room; controls sit over the picture, one CAShapeLayer each: a press only swaps the fill, a stick only moves its knob, nothing redraws the whole screen. -// Touches no control takes (in 3D, those outside the right third) go to the game's touch screen (pushGame; Super Mario Galaxy's pointer in handheld mode). -// The virtual gamepad (layout button included) hides while a physical controller is connected and comes back when it is removed; meanwhile every touch goes to the game. Physical controllers keep every button (D-pad and right stick included) and map by position: bottom = B, right = A, left = Y, top = X (same positions as on a Switch controller). +// Touches no control takes go to the game's touch screen (pushGame; Super Mario Galaxy's pointer in handheld mode). Tap layout keeps direct game touches everywhere except its layout selector. +// Controllers and keyboards hide the pad by default. DM_PAD_KEEP_TOUCH=1 keeps the complete touch layer visible and usable beside them. Mouse input follows the same policy; without the switch a direct finger can reclaim touch controls when no controller/keyboard is present. Physical controllers map by position: bottom = B, right = A, left = Y, top = X. // Shaking the phone shakes the controller (DM_BTN_SHAKE: its six-axis sensor, hle_hid.c), whichever controls are in use; see "Shaking the phone" below. #import #import @@ -25,20 +27,55 @@ #include "motion_apple.h" #include "motion_fusion.h" #include +#include "DmFaceLayout.h" enum { B_A = 0, B_B, B_X, B_Y, B_LS, B_RS, B_L, B_R, B_ZL, B_ZR, B_PLUS, B_MINUS, B_LEFT, B_UP, B_RIGHT, B_DOWN, B_N }; -enum { C_NONE = 0, C_STICK, C_DPAD, C_FACE, C_BTN, C_SWAP, C_GAME }; // which control a touch holds; C_GAME: none, the touch goes to the game's touch screen (finger slot << 8) +enum { C_NONE = 0, C_STICK, C_DPAD, C_FACE, C_BTN, C_SWAP, C_GAME, C_RUN, C_AIM }; static pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER; -static uint64_t g_touch_btn, g_pad_btn, g_shake_btn; -static int32_t g_touch_lx, g_touch_ly, g_touch_rx, g_touch_ry, g_pad_lx, g_pad_ly, g_pad_rx, g_pad_ry; +static BOOL g_input_active = YES; +static uint64_t g_input_epoch; +static uint64_t g_touch_btn, g_pad_btn, g_key_btn, g_shake_btn; +static uint64_t g_touch_face, g_touch_native; +static int32_t g_touch_lx, g_touch_ly, g_touch_rx, g_touch_ry, g_pad_lx, g_pad_ly, g_pad_rx, g_pad_ry, g_key_lx, g_key_ly, g_key_rx, g_key_ry, g_mouse_rx, g_mouse_ry; +static BOOL g_key_w, g_key_a, g_key_s, g_key_d, g_key_left, g_key_up, g_key_right, g_key_down; +static BOOL g_shift_left, g_shift_right; +static BOOL g_command_left, g_command_right; +static uint64_t g_run_button, g_aim_button; // native Npad shortcuts, independent of visible face letters +static double g_mouse_deadline, g_raw_mouse_time; +static void read_mouse(float dx, float dy); +static void clear_mouse(void); static int tilted(int32_t x, int32_t y) { return (int64_t)x * x + (int64_t)y * y > (int64_t)4000 * 4000; } static void publish(void) { pthread_mutex_lock(&g_mu); + // GC handlers may still deliver global mouse/controller changes while a + // visible Stage Manager card is inactive. Never turn its camera off-focus. + if (!g_input_active) { dm_input_set(0, 0, 0, 0, 0); pthread_mutex_unlock(&g_mu); return; } int32_t lx = g_touch_lx, ly = g_touch_ly, rx = g_touch_rx, ry = g_touch_ry; + if (tilted(g_key_lx, g_key_ly)) { lx = g_key_lx; ly = g_key_ly; } + if (tilted(g_key_rx, g_key_ry)) { rx = g_key_rx; ry = g_key_ry; } + if (tilted(g_mouse_rx, g_mouse_ry)) { rx = g_mouse_rx; ry = g_mouse_ry; } if (tilted(g_pad_lx, g_pad_ly)) { lx = g_pad_lx; ly = g_pad_ly; } // a tilted physical stick wins if (tilted(g_pad_rx, g_pad_ry)) { rx = g_pad_rx; ry = g_pad_ry; } - dm_input_set(g_touch_btn | g_pad_btn | g_shake_btn, lx, ly, rx, ry); + uint64_t run = (g_shift_left || g_shift_right) ? g_run_button : 0; + uint64_t aim = (g_command_left || g_command_right) ? g_aim_button : 0; + // Keep A/B/X/Y identical in menus, exploration and battle. Repositioning + // our virtual controls must never change keyboard or touch button meaning. + g_touch_native = g_touch_face | g_touch_btn; + uint64_t keyboard = g_key_btn; + dm_input_set(g_touch_native | keyboard | g_pad_btn | g_shake_btn | run | aim, lx, ly, rx, ry); + static int records; + static uint64_t previousTouch, previousKey; + if (records < 128 && (g_touch_face != previousTouch || (g_key_btn & 15) != previousKey)) { + const char *diag = getenv("DM_TOUCH_DIAG"); + if (diag && atoi(diag)) { + LOG("[face-input] touch=%#llx -> %#llx keyboard=%#llx -> %#llx", + (unsigned long long)g_touch_face, (unsigned long long)(g_touch_native & 15), + (unsigned long long)(g_key_btn & 15), (unsigned long long)(keyboard & 15)); + records++; + } + } + previousTouch = g_touch_face; previousKey = g_key_btn & 15; pthread_mutex_unlock(&g_mu); } @@ -117,13 +154,22 @@ static void phone_motion_start(void) { K_DPAD_R = 48, K_ARM_W = 17, K_ARM_IN = 11, // D-pad: arm outer edge from the centre, half-width, tip from the centre K_DPAD_GRAB = 1.3, // D-pad grab radius (times the outer edge) K_RSTICK_R = 60, // right stick (not drawn): radius around its centre, the touch-down point - K_RSWIPE = 1.0 / 3, // right stick area: this fraction of the screen width from the right edge (not in u) + K_RSWIPE = 0.5, // camera drag area: this fraction of the screen width from the right edge (not in u) K_CLICK_X = 102, K_CLICK_R = 17; // L3 / R3: centre from the outer edge (level with ZL / ZR), radius static const int k_btns[] = {B_ZL, B_L, B_ZR, B_R, B_MINUS, B_PLUS, B_LS, B_RS}; // keys locked at touch-down (L3 / R3 only in the 3D layout) static NSString *const k_layout_key = @"DmPadLayout"; // NSUserDefaults: 0 = 2D, 1 = 3D, 2 = Tap (DM_GAME_PAD_LAYOUT's numbers) int dm_opmode(void); // hle_oe.c: 0 = handheld +static BOOL pad_env_on(const char *name) { + const char *value = getenv(name); + return value && atoi(value) != 0; +} +static BOOL pad_force_full(void) { return pad_env_on("DM_PAD_FORCE_FULL"); } +static BOOL pad_keep_touch(void) { return pad_env_on("DM_PAD_KEEP_TOUCH"); } + @interface DmGamepad : UIView +- (void)mouseMotionX:(float)dx y:(float)dy raw:(BOOL)raw; +- (void)updateSkillFaceLayout; @end static __weak DmGamepad *g_view; @@ -133,16 +179,22 @@ @implementation DmGamepad { CGPoint _rCentre; // the right stick's centre: where its touch landed, dragged along past K_RSTICK_R uint64_t _faceBits, _dpadBits, _btnBits; NSMapTable *_faceOf; // which keys each touch on the diamond currently holds + dm_face_layout _faceLayout; // shared visible/hit-test geometry; native bits remain A/B/X/Y CAShapeLayer *_key[B_N]; CATextLayer *_label[B_N]; // one layer per key CAShapeLayer *_ring, *_knob; // the left stick (the right one is not drawn) CAShapeLayer *_swap; CATextLayer *_swapLabel; BOOL _swapHeld; - UIImpactFeedbackGenerator *_touchFeedback; // one short feedback per touch-control press edge + UIImpactFeedbackGenerator *_touchFeedback; unsigned _hapticSticks; + CAShapeLayer *_run; CATextLayer *_runLabel; BOOL _runHeld; + CAShapeLayer *_aim; CATextLayer *_aimLabel; BOOL _aimHeld, _aimLatched; BOOL _stickShown; // the left stick was held at the last refresh BOOL _full, _tap; // the 3D layout / the Tap layout is current - CALayer *_controls; // holds every control layer: hidden while a physical controller is connected (the view itself stays, for game touches) - BOOL _padOff; // a physical controller is connected: every touch goes to the game + CALayer *_controls; // holds every control layer; the default external-input policy may hide it while the view stays active for game touches + BOOL _padOff; // when the policy hides the pad, every touch goes to the game + BOOL _controllerOrKeyboard, _touchPreferred; // direct fingers can take over from an idle persistent mouse UITouch *_game[16]; // touches on the game's touch screen by finger slot + CGPoint _hoverLast; + BOOL _hoverValid; } static UIColor *k_idle, *k_on, *k_line, *k_ringFill, *k_text, *k_textOn; @@ -157,7 +209,7 @@ @implementation DmGamepad { t.string = s; t.alignmentMode = kCAAlignmentCenter; t.foregroundColor = k_text.CGColor; t.contentsScale = UIScreen.mainScreen.scale; [parent addSublayer:t]; return t; } -static BOOL fullOnly(int b) { return b == B_LS || b == B_RS || b >= B_LEFT; } // keys shown only in the 3D layout +static BOOL fullOnly(int b) { return b == B_LS || b == B_RS; } // stick clicks are shown only in the 3D layout; directions are always available - (instancetype)initWithFrame:(CGRect)f { if ((self = [super initWithFrame:f])) { @@ -185,29 +237,94 @@ - (instancetype)initWithFrame:(CGRect)f { if (names[b]) _label[b] = label(_key[b], names[b]); // the D-pad arms carry no label } _swap = shape(_controls); _swapLabel = label(_swap, @""); + // Generic opt-in: games choose their own existing logical run button. + // No synthetic skill press is generated by moving or dragging a stick. + const char *run = getenv("DM_RUN_BUTTON"); g_run_button = 0; + static const char *runNames[] = {"A", "B", "X", "Y", "LS", "RS"}; + for (int b = 0; run && b < 6; b++) if (!strcmp(run, runNames[b])) g_run_button = 1ull << b; + _run = shape(_controls); _runLabel = label(_run, @"Run"); + const char *aim = getenv("DM_AIM_ZL"); g_aim_button = aim && atoi(aim) ? DM_BTN_ZL : 0; + _aim = shape(_controls); _aimLabel = label(_aim, @"Aim"); + if (g_run_button) LOG("front end: Run touch button and either Shift alias native %s independently of visible face labels", run); + if (g_aim_button) LOG("front end: tap Aim toggles ZL; hold either Command to aim; release/focus loss clears modifier input"); + LOG("front end: native A/B/X/Y unchanged; virtual face geometry alone can follow the original battle skill HUD"); NSNumber *saved = [NSUserDefaults.standardUserDefaults objectForKey:k_layout_key]; int l = saved ? (int)saved.integerValue : DM_GAME_PAD_LAYOUT; + // A profile may require the complete controller surface. This also + // upgrades an existing install whose saved preference predates newly + // exposed controls, while leaving every native button bit unchanged. + if (pad_force_full()) l = 1; if (l == 2 && dm_opmode() != 0) l = 0; // Tap only in handheld mode (env.txt's DM_OPMODE may have changed it) _full = l == 1; _tap = l == 2; [self applyLayout]; NSNotificationCenter *nc = NSNotificationCenter.defaultCenter; - [nc addObserver:self selector:@selector(padsChanged:) name:GCControllerDidConnectNotification object:nil]; - [nc addObserver:self selector:@selector(padsChanged:) name:GCControllerDidDisconnectNotification object:nil]; - [self padsChanged:nil]; + [nc addObserver:self selector:@selector(inputDevicesChanged:) name:GCControllerDidConnectNotification object:nil]; + [nc addObserver:self selector:@selector(inputDevicesChanged:) name:GCControllerDidDisconnectNotification object:nil]; + [nc addObserver:self selector:@selector(inputDevicesChanged:) name:GCKeyboardDidConnectNotification object:nil]; + [nc addObserver:self selector:@selector(inputDevicesChanged:) name:GCKeyboardDidDisconnectNotification object:nil]; + [nc addObserver:self selector:@selector(inputDevicesChanged:) name:GCMouseDidConnectNotification object:nil]; + [nc addObserver:self selector:@selector(inputDevicesChanged:) name:GCMouseDidDisconnectNotification object:nil]; + [self inputDevicesChanged:nil]; + UIHoverGestureRecognizer *hover = [[UIHoverGestureRecognizer alloc] initWithTarget:self action:@selector(pointerMoved:)]; + hover.allowedTouchTypes = @[@(UITouchTypeIndirectPointer)]; + hover.cancelsTouchesInView = NO; + [self addGestureRecognizer:hover]; + UIPanGestureRecognizer *scroll = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(trackpadScrolled:)]; + scroll.allowedTouchTypes = @[]; // scroll events only; never steal a finger from either stick + scroll.allowedScrollTypesMask = UIScrollTypeMaskAll; + scroll.cancelsTouchesInView = NO; + [self addGestureRecognizer:scroll]; + __weak DmGamepad *weakSelf = self; + NSTimer *releaseTimer = [NSTimer timerWithTimeInterval:1.0 / 60 repeats:YES block:^(NSTimer *timer) { + (void)timer; + pthread_mutex_lock(&g_mu); + BOOL expired = g_mouse_deadline > 0 && CACurrentMediaTime() >= g_mouse_deadline; + if (expired) { g_mouse_rx = g_mouse_ry = 0; g_mouse_deadline = 0; } + pthread_mutex_unlock(&g_mu); + if (expired) publish(); + [weakSelf updateSkillFaceLayout]; // the main run loop owns geometry and touch routing together + }]; + [NSRunLoop.mainRunLoop addTimer:releaseTimer forMode:NSRunLoopCommonModes]; + LOG("front end: trackpad raw/hover/scroll camera input with bounded release timer"); motion_start(); phone_motion_start(); } return self; } +- (BOOL)canBecomeFirstResponder { return YES; } +- (void)didMoveToWindow { [super didMoveToWindow]; if (self.window) [self becomeFirstResponder]; } +- (NSArray *)keyCommands { + if (!g_input_active) return @[]; + NSMutableArray *commands = [NSMutableArray array]; + // Keep arrows inside the game even when UIKit would otherwise use them for + // focus movement. GCKeyboard still supplies the matching down/up events. + for (NSString *key in @[UIKeyInputUpArrow, UIKeyInputDownArrow, UIKeyInputLeftArrow, UIKeyInputRightArrow]) { + UIKeyCommand *command = [UIKeyCommand keyCommandWithInput:key modifierFlags:0 action:@selector(gameCommand:)]; + command.wantsPriorityOverSystemBehavior = YES; + [commands addObject:command]; + } + // Consume only game combinations, in particular Command-W, rather than + // allowing the system to close a Stage Manager window while moving. + if (g_aim_button) for (NSString *key in @[@"w", @"a", @"s", @"d", @"u", @"i", @"j", @"k", @"l", @"q", @"e", @"z", @"c", UIKeyInputUpArrow, UIKeyInputDownArrow, UIKeyInputLeftArrow, UIKeyInputRightArrow]) + for (NSNumber *extra in @[@0, @(UIKeyModifierShift)]) { + UIKeyCommand *command = [UIKeyCommand keyCommandWithInput:key modifierFlags:UIKeyModifierCommand | extra.unsignedIntegerValue action:@selector(gameCommand:)]; + command.wantsPriorityOverSystemBehavior = YES; + [commands addObject:command]; + } + return commands; +} +- (void)gameCommand:(UIKeyCommand *)command { (void)command; } // GCKeyboard supplies down/up; do not synthesize another press here - (void)applyLayout { // show / hide the controls for the current layout [CATransaction begin]; [CATransaction setDisableActions:YES]; for (int b = 0; b < B_N; b++) _key[b].hidden = _tap || (fullOnly(b) && !_full); _ring.hidden = _knob.hidden = _tap; + _run.hidden = _tap || !g_run_button; + _aim.hidden = _tap || !g_aim_button; _swapLabel.string = _tap ? @"Tap" : _full ? @"3D" : @"2D"; [CATransaction commit]; } - (void)nextLayout { // 2D → 3D (→ Tap in handheld mode) → 2D: release every touch first (no key stays held), save, lay out again - int l = _tap ? 0 : _full ? (dm_opmode() == 0 ? 2 : 0) : 1; + int l = pad_force_full() ? 1 : _tap ? 0 : _full ? (dm_opmode() == 0 ? 2 : 0) : 1; [self reset]; _full = l == 1; _tap = l == 2; [NSUserDefaults.standardUserDefaults setObject:@(l) forKey:k_layout_key]; @@ -223,8 +340,20 @@ - (CGPoint)faceCenter { CGRect s = self.corner; CGFloat u = self.u; return side( - (CGFloat)faceGap { return K_FACE_GAP * self.u; } - (CGFloat)faceR { return K_FACE_R * self.u; } - (CGPoint)facePos:(int)b { CGPoint c = self.faceCenter; CGFloat g = self.faceGap; - switch (b) { case B_A: return CGPointMake(c.x + g, c.y); case B_B: return CGPointMake(c.x, c.y + g); case B_X: return CGPointMake(c.x, c.y - g); default: return CGPointMake(c.x - g, c.y); } } + dm_face_offset offset = dm_face_layout_offset(&_faceLayout, (unsigned)b); + return CGPointMake(c.x + offset.x * g, c.y + offset.y * g); } +- (void)updateSkillFaceLayout { + if (!NSThread.isMainThread) return; + // Freeze the entire diamond until every face-owned finger releases, even + // if a held finger slides into a gap. A UI transition cannot change its key. + if (!dm_face_layout_update(&_faceLayout, dm_za_skill_layout_active(), _faceOf.count != 0)) return; + [self setNeedsLayout]; [self layoutIfNeeded]; + LOG("front end: virtual face geometry=%s; native button functions unchanged", + _faceLayout.skill ? "skill A right B bottom X top Y left" : "normal A bottom B right X left Y top"); +} - (CGPoint)stickHome { CGRect s = self.corner; return side(s, 0, K_STICK_X * self.u, CGRectGetMaxY(s) - K_PAD_Y * self.u); } // the left stick's ring centre +- (CGPoint)runCenter { CGPoint c = self.stickHome; return CGPointMake(c.x, c.y - 145 * self.u); } +- (CGPoint)aimCenter { CGPoint c = self.runCenter; return CGPointMake(c.x + 70 * self.u, c.y); } - (CGPoint)stickCentre:(int)i { return i ? _rCentre : self.stickHome; } - (CGFloat)stickR:(int)i { return (i ? K_RSTICK_R : K_STICK_R) * self.u; } - (BOOL)inRightArea:(CGPoint)p { CGRect b = self.bounds; return p.x >= CGRectGetMaxX(b) - b.size.width * K_RSWIPE; } @@ -278,11 +407,18 @@ - (CGPoint)knobOffset:(int)i { // knob offset from the stick centre (clamped t // ---- Touch ---- - (void)recompute { uint64_t face = 0; for (UITouch *t in _faceOf) face |= [_faceOf objectForKey:t].unsignedLongLongValue; - uint64_t btn = 0, dpad = 0; BOOL swap = NO; + uint64_t btn = 0, dpad = 0; BOOL swap = NO, run = NO, aim = NO; for (UITouch *t in _owner) { uint64_t v = [_owner objectForKey:t].unsignedLongLongValue; int c = (int)(v & 0xff); if (c == C_BTN) btn |= 1ull << (v >> 8); else if (c == C_DPAD) dpad |= [self dpadAt:[t locationInView:self]]; else if (c == C_SWAP) swap = YES; } + for (UITouch *t in _owner) { + int c = [_owner objectForKey:t].unsignedLongLongValue & 0xff; + if (c == C_RUN) run = YES; + if (c == C_AIM) aim = YES; + } + if (run) btn |= g_run_button; + if (aim || _aimLatched) btn |= g_aim_button; int32_t ax[4] = {0}; for (int i = 0; i < 2; i++) { CGPoint o = [self knobOffset:i]; CGFloat R = [self stickR:i], d = hypot(o.x, o.y), dz = R * K_DEAD; @@ -290,7 +426,7 @@ - (void)recompute { CGFloat m = fmin((d - dz) / (R * K_FULL - dz), 1) / d; // outside the dead zone: 0 rising to full at the full-tilt radius ax[i * 2] = (int32_t)lrint(o.x * m * 32767); ax[i * 2 + 1] = (int32_t)lrint(-o.y * m * 32767); // host: up is positive; rounded so full tilt is exactly 32767 } - BOOL changed = face != _faceBits || dpad != _dpadBits || btn != _btnBits || swap != _swapHeld; + BOOL changed = face != _faceBits || dpad != _dpadBits || btn != _btnBits || swap != _swapHeld || run != _runHeld || aim != _aimHeld; uint64_t pressed = (face | dpad | btn) & ~(_faceBits | _dpadBits | _btnBits); unsigned gripping = (_stick[0] ? 1u : 0) | (_stick[1] ? 2u : 0); if (pressed || (swap && !_swapHeld) || (gripping & ~_hapticSticks)) { @@ -298,11 +434,26 @@ - (void)recompute { } _hapticSticks = gripping; _faceBits = face; _dpadBits = dpad; _btnBits = btn; _swapHeld = swap; + _runHeld = run; + _aimHeld = aim; pthread_mutex_lock(&g_mu); - g_touch_btn = face | dpad | btn; g_touch_lx = ax[0]; g_touch_ly = ax[1]; g_touch_rx = ax[2]; g_touch_ry = ax[3]; + g_touch_face = face; g_touch_btn = dpad | btn; g_touch_lx = ax[0]; g_touch_ly = ax[1]; g_touch_rx = ax[2]; g_touch_ry = ax[3]; pthread_mutex_unlock(&g_mu); publish(); - BOOL stick = _stick[0] != nil; // only the left stick is drawn: the right one needs no refresh + pthread_mutex_lock(&g_mu); uint64_t nativeTouch = g_touch_native; pthread_mutex_unlock(&g_mu); + // Observe actual UIKit ownership, rather than a separate input model. + static int diagnostic = -1, records; static unsigned prior; static uint64_t priorButtons; + if (diagnostic < 0) { const char *e = getenv("DM_TOUCH_DIAG"); diagnostic = e && atoi(e) != 0; } + unsigned state = (_stick[0] != nil) | ((_stick[1] != nil) << 1) | + ((ax[0] != 0 || ax[1] != 0) << 2) | ((ax[2] != 0 || ax[3] != 0) << 3); + if (diagnostic && records < 256 && (state != prior || (face | dpad | btn) != priorButtons)) { + LOG("[touch-input] owners=%lu left=%d right=%d buttons=%#llx native=%#llx axes=%d,%d,%d,%d", (unsigned long)_owner.count, + _stick[0] != nil, _stick[1] != nil, (unsigned long long)(face | dpad | btn), + (unsigned long long)nativeTouch, ax[0], ax[1], ax[2], ax[3]); + records++; + } + prior = state; priorButtons = face | dpad | btn; + BOOL stick = _stick[0] != nil; if (changed || stick || _stickShown) [self refresh]; // also refresh on the stick release so the knob returns to the centre _stickShown = stick; } @@ -325,19 +476,34 @@ - (BOOL)gameBegin:(UITouch *)t { return NO; } - (void)touchesBegan:(NSSet *)ts withEvent:(UIEvent *)e { + for (UITouch *t in ts) if (t.type == UITouchTypeDirect && _padOff && !_controllerOrKeyboard) { + // A persistent virtual mouse is not evidence that the player still has + // a keyboard cover attached. Let the first finger choose touch input. + [self reset]; clear_mouse(); + _touchPreferred = YES; _padOff = NO; _controls.hidden = NO; + LOG("front end: direct touch restored controls while only mice remain connected"); + break; + } BOOL game = NO; for (UITouch *t in ts) { + if (t.type == UITouchTypeIndirectPointer) continue; if (_padOff) { game |= [self gameBegin:t]; continue; } CGPoint p = [t locationInView:self]; int hit = _tap ? -1 : [self btnAt:p]; if (hit >= 0) { [_owner setObject:@(C_BTN | (uint64_t)hit << 8) forKey:t]; continue; } + if (!_tap && g_run_button && dist(p, self.runCenter) <= 28 * self.u) { [_owner setObject:@(C_RUN) forKey:t]; continue; } + if (!_tap && g_aim_button && dist(p, self.aimCenter) <= 28 * self.u) { [_owner setObject:@(C_AIM) forKey:t]; continue; } if ([self inSwap:p]) { [_owner setObject:@(C_SWAP) forKey:t]; continue; } if (_tap) { game |= [self gameBegin:t]; continue; } // diamond, D-pad, left stick: take the one with the smallest distance / grab radius, if it is <= 1 CGFloat best = 1; int kind = C_NONE, idx = 0; CGFloat f = dist(p, self.faceCenter) / (self.faceGap + self.faceR * 1.6); if (f <= best) { best = f; kind = C_FACE; } - if (_full) { CGFloat d = dist(p, self.dpadCenter) / (self.dpadR * K_DPAD_GRAB); if (d < best) { best = d; kind = C_DPAD; } } + { CGFloat d = dist(p, self.dpadCenter) / (self.dpadR * K_DPAD_GRAB); if (d < best) { best = d; kind = C_DPAD; } } if (!_stick[0]) { CGFloat s = dist(p, self.stickHome) / ([self stickR:0] * K_STICK_GRAB); if (s < best) { best = s; kind = C_STICK; } } - if (kind == C_NONE && _full && !_stick[1] && [self inRightArea:p]) { kind = C_STICK; idx = 1; _rCentre = p; } // the right stick: centred where the touch lands + // Camera is available in Simple too: detaching a keyboard restores the + // saved layout, and Simple previously routed every blank-area drag to + // the game's touchscreen instead of its right-stick camera. Controls + // above always claim first; no gesture recognizer competes for fingers. + if (kind == C_NONE && !_stick[1] && [self inRightArea:p]) { kind = C_STICK; idx = 1; _rCentre = p; } if (kind == C_FACE) { [_owner setObject:@(C_FACE) forKey:t]; [_faceOf setObject:@([self faceAt:p]) forKey:t]; } else if (kind == C_DPAD) [_owner setObject:@(C_DPAD) forKey:t]; else if (kind == C_STICK) { [_owner setObject:@(C_STICK | (uint64_t)idx << 8) forKey:t]; _stick[idx] = t; _stickNow[idx] = p; } @@ -363,17 +529,41 @@ - (void)endTouches:(NSSet *)ts tapped:(BOOL)ended { BOOL swap = NO, game = NO; for (UITouch *t in ts) { uint64_t v = [_owner objectForKey:t].unsignedLongLongValue; + if ((v & 0xff) == C_AIM) { + if (ended && dist([t locationInView:self], self.aimCenter) <= 28 * self.u) _aimLatched = !_aimLatched; + else if (!ended) _aimLatched = NO; + } if (ended && (v & 0xff) == C_SWAP && [self inSwap:[t locationInView:self]]) swap = YES; // layout button: counts only when released on the button if ((v & 0xff) == C_GAME && (v >> 8) < 16) { _game[v >> 8] = nil; game = YES; } [_owner removeObjectForKey:t]; [_faceOf removeObjectForKey:t]; for (int i = 0; i < 2; i++) if (t == _stick[i]) _stick[i] = nil; } if (game) [self pushGame]; [self recompute]; + [self updateSkillFaceLayout]; if (swap) [self nextLayout]; } - (void)touchesEnded:(NSSet *)ts withEvent:(UIEvent *)e { [self endTouches:ts tapped:YES]; } - (void)touchesCancelled:(NSSet *)ts withEvent:(UIEvent *)e { [self endTouches:ts tapped:NO]; } -- (void)reset { [_owner removeAllObjects]; [_faceOf removeAllObjects]; _stick[0] = _stick[1] = nil; for (int i = 0; i < 16; i++) _game[i] = nil; [self pushGame]; [self recompute]; } +- (void)reset { _hoverValid = NO; _aimLatched = NO; [_owner removeAllObjects]; [_faceOf removeAllObjects]; _stick[0] = _stick[1] = nil; for (int i = 0; i < 16; i++) _game[i] = nil; [self pushGame]; [self recompute]; [self updateSkillFaceLayout]; } + +// Some keyboard-cover trackpads deliver UIKit pointer/scroll events without +// appearing in GCMouse.mice. Prefer recent raw motion to avoid double counting. +- (void)pointerMoved:(UIHoverGestureRecognizer *)g { + if (g.state == UIGestureRecognizerStateEnded || g.state == UIGestureRecognizerStateCancelled) { _hoverValid = NO; return; } + CGPoint p = [g locationInView:self]; + if (_hoverValid && g.state == UIGestureRecognizerStateChanged) { + pthread_mutex_lock(&g_mu); BOOL raw = CACurrentMediaTime() - g_raw_mouse_time < 0.1; pthread_mutex_unlock(&g_mu); + if (!raw) { [self mouseMotionX:(float)(p.x - _hoverLast.x) y:(float)(_hoverLast.y - p.y) raw:NO]; + static int once; if (!once++) LOG("front end: UIKit trackpad hover is driving the camera"); } + } + _hoverLast = p; _hoverValid = YES; +} +- (void)trackpadScrolled:(UIPanGestureRecognizer *)g { + if (g.state == UIGestureRecognizerStateEnded || g.state == UIGestureRecognizerStateCancelled) { clear_mouse(); return; } + CGPoint d = [g translationInView:self]; [g setTranslation:CGPointZero inView:self]; + if (d.x || d.y) { [self mouseMotionX:(float)d.x y:(float)-d.y raw:NO]; + static int once; if (!once++) LOG("front end: two-finger trackpad scroll is driving the camera"); } +} // ---- Shapes ---- static CGPathRef circlePath(CGPoint c, CGFloat r) { return CGPathCreateWithEllipseInRect(CGRectMake(c.x - r, c.y - r, 2 * r, 2 * r), NULL); } @@ -411,6 +601,8 @@ - (void)layoutSubviews { CGFloat u = self.u; [CATransaction begin]; [CATransaction setDisableActions:YES]; _controls.frame = self.layer.bounds; + setPath(_run, circlePath(self.runCenter, 25 * u)); placeLabel(_runLabel, self.runCenter, 14 * u); + setPath(_aim, circlePath(self.aimCenter, 25 * u)); placeLabel(_aimLabel, self.aimCenter, 14 * u); for (int b = B_A; b <= B_Y; b++) { CGPoint c = [self facePos:b]; setPath(_key[b], circlePath(c, self.faceR)); placeLabel(_label[b], c, 20 * u); } for (int i = 0; i < (int)(sizeof k_btns / sizeof *k_btns); i++) { int b = k_btns[i]; if (isDrop(b)) { @@ -441,6 +633,9 @@ - (void)refresh { // pressed state → fill, sticks → knob position for (int b = 0; b < B_N; b++) { BOOL on = (all >> b) & 1; _key[b].fillColor = (on ? k_on : k_idle).CGColor; _label[b].foregroundColor = (on ? k_textOn : k_text).CGColor; } _swap.fillColor = (_swapHeld ? k_on : k_idle).CGColor; _swapLabel.foregroundColor = (_swapHeld ? k_textOn : k_text).CGColor; + _run.fillColor = (_runHeld ? k_on : k_idle).CGColor; _runLabel.foregroundColor = (_runHeld ? k_textOn : k_text).CGColor; + BOOL aim = _aimHeld || _aimLatched; + _aim.fillColor = (aim ? k_on : k_idle).CGColor; _aimLabel.foregroundColor = (aim ? k_textOn : k_text).CGColor; CGPoint h = self.stickHome, o = [self knobOffset:0]; _knob.position = CGPointMake(h.x + o.x, h.y + o.y); _knob.fillColor = (_stick[0] ? k_on : k_idle).CGColor; [CATransaction commit]; @@ -464,24 +659,187 @@ static void read_pad(GCExtendedGamepad *g) { pthread_mutex_unlock(&g_mu); publish(); } -- (void)padsChanged:(NSNotification *)note { +static uint64_t keyboard_button(GCKeyCode key) { + // Preserve the original keyboard bindings independently of screen layout. + if (key == GCKeyCodeKeyJ) return DM_BTN_A; + if (key == GCKeyCodeKeyK) return DM_BTN_B; + if (key == GCKeyCodeKeyU) return DM_BTN_X; + if (key == GCKeyCodeKeyI) return DM_BTN_Y; + if (key == GCKeyCodeKeyF) return DM_BTN_LS; + if (key == GCKeyCodeKeyG) return DM_BTN_RS; + if (key == GCKeyCodeKeyQ) return DM_BTN_L; + if (key == GCKeyCodeKeyE) return DM_BTN_R; + if (key == GCKeyCodeKeyZ) return DM_BTN_ZL; + if (key == GCKeyCodeKeyC) return DM_BTN_ZR; + if (key == GCKeyCodeReturnOrEnter) { + // Device automation can inject Return but not letter keys. Keep the + // normal Plus mapping unless an on-device diagnostic explicitly asks + // for Return to act as the face A button. + const char *e = getenv("DM_KEY_ENTER_A"); + return e && !strcmp(e, "1") ? DM_BTN_A : DM_BTN_PLUS; + } + if (key == GCKeyCodeTab) return DM_BTN_MINUS; + if (key == GCKeyCodeSpacebar) return DM_BTN_SHAKE; + if (key == GCKeyCodeLeftArrow) return DM_BTN_LEFT; + if (key == GCKeyCodeUpArrow) return DM_BTN_UP; + if (key == GCKeyCodeRightArrow) return DM_BTN_RIGHT; + if (key == GCKeyCodeDownArrow) return DM_BTN_DOWN; + return 0; +} +static void read_key(GCKeyCode key, BOOL pressed) { + pthread_mutex_lock(&g_mu); + if (key == GCKeyCodeKeyW) g_key_w = pressed; + else if (key == GCKeyCodeKeyA) g_key_a = pressed; + else if (key == GCKeyCodeKeyS) g_key_s = pressed; + else if (key == GCKeyCodeKeyD) g_key_d = pressed; + else if (key == GCKeyCodeLeftShift) g_shift_left = pressed; + else if (key == GCKeyCodeRightShift) g_shift_right = pressed; + else if (key == GCKeyCodeLeftGUI) g_command_left = pressed; + else if (key == GCKeyCodeRightGUI) g_command_right = pressed; + uint64_t bit = keyboard_button(key); + g_key_btn = pressed ? g_key_btn | bit : g_key_btn & ~bit; + g_key_lx = ((int)g_key_d - (int)g_key_a) * DM_STICK_MAX; + g_key_ly = ((int)g_key_w - (int)g_key_s) * DM_STICK_MAX; + // Arrow keys are the four digital directions. Camera input remains on the + // right-side touch drag, mouse/trackpad, or a physical right stick. + g_key_rx = g_key_ry = 0; + pthread_mutex_unlock(&g_mu); + publish(); +} +static void clear_keyboard(void) { + pthread_mutex_lock(&g_mu); + g_key_btn = 0; g_key_lx = g_key_ly = g_key_rx = g_key_ry = 0; + g_key_w = g_key_a = g_key_s = g_key_d = g_key_left = g_key_up = g_key_right = g_key_down = NO; + g_shift_left = g_shift_right = NO; + g_command_left = g_command_right = NO; + pthread_mutex_unlock(&g_mu); + publish(); +} +// A mouse or keyboard-cover trackpad supplies relative deltas through +// GameController. Turn a short burst into right-stick motion and return the +// stick to centre shortly after the last delta. This keeps camera motion +// responsive without leaving it drifting after the finger stops. +static int32_t mouse_axis(float v) { + static float scale; + if (!scale) { const char *e = getenv("DM_MOUSE_LOOK_SCALE"); scale = e && atof(e) > 0 ? (float)atof(e) : 1400.f; } + return (int32_t)fmaxf(-DM_STICK_MAX, fminf(DM_STICK_MAX, v * scale)); +} +static void read_mouse(float dx, float dy) { + if (!isfinite(dx) || !isfinite(dy) || (!dx && !dy)) return; + pthread_mutex_lock(&g_mu); + g_mouse_rx = mouse_axis(dx); g_mouse_ry = mouse_axis(dy); + g_mouse_deadline = CACurrentMediaTime() + 0.060; + pthread_mutex_unlock(&g_mu); + publish(); +} +static void clear_mouse(void) { + pthread_mutex_lock(&g_mu); g_mouse_deadline = 0; g_mouse_rx = g_mouse_ry = 0; pthread_mutex_unlock(&g_mu); publish(); +} +- (void)mouseMotionX:(float)dx y:(float)dy raw:(BOOL)raw { + if (!isfinite(dx) || !isfinite(dy) || (!dx && !dy)) return; + if (!NSThread.isMainThread) { + pthread_mutex_lock(&g_mu); uint64_t epoch = g_input_epoch; BOOL eligible = g_input_active; pthread_mutex_unlock(&g_mu); + if (!eligible) return; + dispatch_async(dispatch_get_main_queue(), ^{ + pthread_mutex_lock(&g_mu); BOOL current = g_input_active && epoch == g_input_epoch; pthread_mutex_unlock(&g_mu); + if (current) [self mouseMotionX:dx y:dy raw:raw]; + }); + return; + } + pthread_mutex_lock(&g_mu); BOOL active = g_input_active; pthread_mutex_unlock(&g_mu); + if (!active || _owner.count) return; // never steal a held movement/camera/skill finger + if (!pad_keep_touch() && (_touchPreferred || !_padOff)) { + _touchPreferred = NO; _padOff = YES; _controls.hidden = YES; + [self reset]; + LOG("front end: mouse motion selected pointer input; touch controls hidden"); + } + if (raw) { pthread_mutex_lock(&g_mu); g_raw_mouse_time = CACurrentMediaTime(); pthread_mutex_unlock(&g_mu); } + read_mouse(dx, dy); + if (raw) { static int once; if (!once++) LOG("front end: raw mouse/trackpad motion is driving the camera"); } +} +- (void)inputDevicesChanged:(NSNotification *)note { + // Notification delivery need not be on UIKit's queue. Read the latest + // device inventory and change touch ownership together on the main queue, + // rather than applying a captured, potentially stale disconnect snapshot. + if (!NSThread.isMainThread) { + if ([note.name isEqual:GCControllerDidDisconnectNotification]) { + GCController *gone = note.object; + gone.extendedGamepad.valueChangedHandler = nil; + dm_motion_detach(gone, 0); + } + dispatch_async(dispatch_get_main_queue(), ^{ [self inputDevicesChanged:nil]; }); + return; + } + BOOL controllerEvent = !note || [note.name isEqual:GCControllerDidConnectNotification] || [note.name isEqual:GCControllerDidDisconnectNotification]; GCController *gone = [note.name isEqual:GCControllerDidDisconnectNotification] ? note.object : nil; if (gone) { gone.extendedGamepad.valueChangedHandler = nil; dm_motion_detach(gone, 0); } - BOOL any = NO; int motion = 0; + BOOL controller = NO; + int motion = 0; for (GCController *c in GCController.controllers) if (c != gone && c.extendedGamepad) { + controller = YES; if (c.motion.hasRotationRate) motion = 1; - any = YES; c.extendedGamepad.valueChangedHandler = ^(GCExtendedGamepad *g, GCControllerElement *el) { (void)el; read_pad(g); }; - dm_motion_attach(c, 0); // all controllers are player 1 on iOS + if (controllerEvent) { + c.extendedGamepad.valueChangedHandler = ^(GCExtendedGamepad *g, GCControllerElement *el) { (void)el; read_pad(g); }; + dm_motion_attach(c, 0); // all controllers are player 1 on iOS + } read_pad(c.extendedGamepad); } - if (!any || !motion) dm_motion_clear(0); // the phone's gyro (if on) takes over from its next sample - if (!any) { dm_haptics_apple_controller(0, nil); pthread_mutex_lock(&g_mu); g_pad_btn = 0; g_pad_lx = g_pad_ly = g_pad_rx = g_pad_ry = 0; pthread_mutex_unlock(&g_mu); publish(); } - dispatch_async(dispatch_get_main_queue(), ^{ self->_padOff = any; self->_controls.hidden = any; [self reset]; }); + if (controllerEvent && (!controller || !motion)) dm_motion_clear(0); // the phone's gyro (if on) takes over from its next sample + if (!controller) { dm_haptics_apple_controller(0, nil); pthread_mutex_lock(&g_mu); g_pad_btn = 0; g_pad_lx = g_pad_ly = g_pad_rx = g_pad_ry = 0; pthread_mutex_unlock(&g_mu); publish(); } + GCKeyboard *keyboard = GCKeyboard.coalescedKeyboard; + BOOL hasKeyboard = keyboard.keyboardInput != nil; + _controllerOrKeyboard = controller || hasKeyboard; + if (_controllerOrKeyboard) _touchPreferred = NO; + if (hasKeyboard) { + keyboard.keyboardInput.keyChangedHandler = ^(GCKeyboardInput *input, GCDeviceButtonInput *button, GCKeyCode key, BOOL pressed) { + (void)input; (void)button; read_key(key, pressed); + }; + } else clear_keyboard(); + BOOL mouse = NO; + for (GCMouse *m in GCMouse.mice) if (m.mouseInput) { + mouse = YES; + LOG("front end: mouse inventory vendor=%s category=%s", m.vendorName.UTF8String ?: "unknown", m.productCategory.UTF8String ?: "unknown"); + m.mouseInput.mouseMovedHandler = ^(GCMouseInput *input, float dx, float dy) { + (void)input; [g_view mouseMotionX:dx y:dy raw:YES]; + }; + } + if (!mouse) clear_mouse(); + BOOL keepTouch = pad_keep_touch(); + BOOL any = !keepTouch && (_controllerOrKeyboard || (mouse && !_touchPreferred)); + LOG("front end: input devices controller=%d keyboard=%d mouse=%d; touch controls %s; blank right-half camera %s", controller, hasKeyboard, mouse, any ? "hidden" : "visible", !any && !_tap ? "enabled" : "disabled"); + BOOL changed = _padOff != any; + _padOff = any; _controls.hidden = any; + if (changed) [self reset]; } @end UIView *dm_gamepad_view(CGRect frame) { DmGamepad *v = [[DmGamepad alloc] initWithFrame:frame]; g_view = v; return v; } -void dm_gamepad_reset(void) { // On resign active: release everything (a key held while switching away and released in background gets no "release") - dispatch_async(dispatch_get_main_queue(), ^{ [g_view reset]; }); - pthread_mutex_lock(&g_mu); g_pad_btn = 0; g_shake_btn = 0; g_pad_lx = g_pad_ly = g_pad_rx = g_pad_ry = 0; pthread_mutex_unlock(&g_mu); publish(); +static void clear_input_locked(void) { + g_touch_btn = g_pad_btn = g_key_btn = 0; g_shake_btn = 0; + g_touch_face = g_touch_native = 0; + g_touch_lx = g_touch_ly = g_touch_rx = g_touch_ry = g_pad_lx = g_pad_ly = g_pad_rx = g_pad_ry = g_key_lx = g_key_ly = g_key_rx = g_key_ry = g_mouse_rx = g_mouse_ry = 0; + g_mouse_deadline = g_raw_mouse_time = 0; + g_key_w = g_key_a = g_key_s = g_key_d = g_key_left = g_key_up = g_key_right = g_key_down = NO; + g_shift_left = g_shift_right = NO; + g_command_left = g_command_right = NO; +} +static void reset_touch_owners(void) { + if (NSThread.isMainThread) [g_view reset]; + else dispatch_async(dispatch_get_main_queue(), ^{ [g_view reset]; }); +} +void dm_gamepad_reset(void) { // A key held while switching away and released in background gets no "release" + // Publish neutral touch axes immediately too: a queued UIKit reset must not + // leave the camera turning after focus has already moved to another app. + pthread_mutex_lock(&g_mu); clear_input_locked(); g_input_epoch++; pthread_mutex_unlock(&g_mu); + publish(); reset_touch_owners(); +} +void dm_gamepad_set_active(int active) { + // Clear old state and change eligibility under the same lock: a late global + // callback cannot publish an inactive epoch's held axes on reactivation. + pthread_mutex_lock(&g_mu); clear_input_locked(); g_input_epoch++; g_input_active = active != 0; pthread_mutex_unlock(&g_mu); + publish(); reset_touch_owners(); + if (active) dispatch_async(dispatch_get_main_queue(), ^{ + if (g_input_active && g_view.window) [g_view becomeFirstResponder]; + }); + LOG("front end: input focus active=%d; all input neutralized", active != 0); } diff --git a/dm/ios/Sources/front_ios.m b/dm/ios/Sources/front_ios.m index 9cd03bad71..147113686b 100644 --- a/dm/ios/Sources/front_ios.m +++ b/dm/ios/Sources/front_ios.m @@ -2,6 +2,7 @@ // The host runs on the big-stack thread started by main.m; front_run runs the guest directly on the calling thread. #import #import +#import #import #import #include @@ -9,17 +10,26 @@ #include "hle.h" #include "dm.h" #include "haptics_apple.h" +#include "DmAudioRetry.h" +#include "DmDisplayRefresh.h" UIView *dm_gamepad_view(CGRect frame); -void dm_gamepad_reset(void); +void dm_gamepad_set_active(int active); void front_ios_boot(void); int front_ios_boot_ok(void); -bool dm_show_launcher(void *view_controller, void (*done)(void)); // Launcher.swift: false = no launcher (no dm/launcher.json, or DM_LAUNCHER=0) +// iPadOS 16 compatibility build: start directly with the generated defaults. +// This avoids SwiftUI's compiler-plugin macros, which are unavailable in the +// iPad-hosted macOS build environment. Settings remain overridable through +// Documents/env.txt exactly as before. +bool dm_show_launcher(void *view_controller, void (*done)(void)) { (void)view_controller; (void)done; return false; } static CAMetalLayer *g_layer; static void front_ios_power_watch(void); static void front_ios_audio_watch(void); static UILabel *front_ios_fps_label(void); +static void front_ios_display_refresh_start(UIView *view); +static void front_ios_display_refresh_active(int active); +static void front_ios_present_hz_clamp(UIView *view); @interface DmGameView : UIView @end @@ -79,6 +89,8 @@ - (void)startGame { self.fps = front_ios_fps_label(); // above the pad, which hides while a controller is connected if (self.fps) [self.view addSubview:self.fps]; [self.view setNeedsLayout]; [self.view layoutIfNeeded]; + front_ios_display_refresh_start(self.game); + front_ios_present_hz_clamp(self.game); UIApplication.sharedApplication.idleTimerDisabled = YES; front_ios_boot(); } @@ -125,6 +137,83 @@ static void front_ios_power_watch(void) { // Frame-rate counter: the frames put on screen in the last second (nvn_mtl.m counts them for NVN and for the Vulkan layer alike; frames the display link merged away are not counted). // On when the IPA was built with it (the NSP script's SHOW_FPS / --show-fps → DMShowFPS in Info.plist); DM_SHOW_FPS=0 / 1 in Documents/env.txt overrides that without a rebuild. NULL when off uint64_t mtl_present_count(void); + +// iOS 16 has CADisplayLink even though it cannot use CAMetalDisplayLink. +// This link requests a screen cadence and measures its timestamps; it never +// obtains a drawable, submits a frame or changes the game's 60 Hz timebase. +@interface DmDisplayRefreshObserver : NSObject { + CADisplayLink *_link; + __weak UIView *_view; + int _requested, _supported, _preferred; + dm_display_probe _probe; +} +- (instancetype)initWithView:(UIView *)view requested:(int)requested; +- (void)configure; +- (void)tick:(CADisplayLink *)link; +- (void)setActive:(BOOL)active; +- (void)invalidate; +@end +@implementation DmDisplayRefreshObserver +- (instancetype)initWithView:(UIView *)view requested:(int)requested { + if ((self = [super init])) { + _view = view; _requested = requested; + _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)]; + _link.paused = YES; + [self configure]; + [_link addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes]; + } + return self; +} +- (void)configure { + UIScreen *screen = _view.window.screen ?: UIScreen.mainScreen; + _supported = (int)screen.maximumFramesPerSecond; + _preferred = dm_display_hz_select(_requested, _supported); + _link.preferredFrameRateRange = CAFrameRateRangeMake(MIN(60, _preferred), _preferred, _preferred); + dm_display_probe_reset(&_probe); + LOG("display refresh: requested=%d Hz supported_max=%d Hz preferred=%d Hz; screen cadence request only, game pacing unchanged", + _requested, _supported, _preferred); +} +- (void)setActive:(BOOL)active { + if (active) [self configure]; + dm_display_probe_reset(&_probe); + _link.paused = !active; +} +- (void)tick:(CADisplayLink *)link { + UIScreen *screen = _view.window.screen ?: UIScreen.mainScreen; + if ((int)screen.maximumFramesPerSecond != _supported) [self configure]; + dm_display_probe_result result; + if (dm_display_probe_tick(&_probe, link.timestamp, link.targetTimestamp, mtl_present_count(), &result)) + LOG("display cadence: requested=%d preferred=%d supported_max=%d Hz; measured_callback=%.2f Hz target_period=%.3f ms callback_period_min/max=%.3f/%.3f ms; actual_drawable=%.2f fps frames=%llu over=%.3f s; callback cadence is not rendered FPS", + _requested, _preferred, _supported, result.callback_hz, result.target_period_ms, + result.min_period_ms, result.max_period_ms, result.presented_fps, + (unsigned long long)result.presented_frames, result.seconds); +} +- (void)invalidate { [_link invalidate]; _link = nil; } +@end +static DmDisplayRefreshObserver *g_display_refresh; +static void front_ios_display_refresh_start(UIView *view) { + int requested = dm_display_hz_parse(getenv("DM_DISPLAY_HZ")); + if (requested < 0) { LOG("[RED] DM_DISPLAY_HZ expects 60 or 120; retaining system display policy"); return; } + if (!requested) return; + [g_display_refresh invalidate]; + g_display_refresh = [[DmDisplayRefreshObserver alloc] initWithView:view requested:requested]; + [g_display_refresh setActive:UIApplication.sharedApplication.applicationState == UIApplicationStateActive]; +} +static void front_ios_display_refresh_active(int active) { [g_display_refresh setActive:active != 0]; } +static void front_ios_present_hz_clamp(UIView *view) { + // This runs on the main thread before front_ios_boot starts the guest. The + // NVN pacer has not cached its explicit environment request at this point. + // Never turn the existing 60 Hz default into an implicit high-rate mode. + int requested = dm_display_hz_parse(getenv("DM_PRESENT_HZ")); + if (requested != 120) return; + UIScreen *screen = view.window.screen ?: UIScreen.mainScreen; + int supported = (int)screen.maximumFramesPerSecond; + int selected = dm_display_hz_select(requested, supported); + char value[16]; snprintf(value, sizeof value, "%d", selected); + setenv("DM_PRESENT_HZ", value, 1); + LOG("present refresh: explicit request=%d Hz supported_max=%d Hz selected=%d Hz; configured before guest startup", requested, supported, selected); +} + static UILabel *front_ios_fps_label(void) { const char *e = getenv("DM_SHOW_FPS"); if (!(e ? atoi(e) != 0 : [[NSBundle.mainBundle objectForInfoDictionaryKey:@"DMShowFPS"] boolValue])) return nil; @@ -169,6 +258,8 @@ void front_progress(const char *title, long done, long total) { }); } void *front_metal_layer(void) { return (__bridge void *)g_layer; } +int front_screen_max_fps(void) { return (int)UIScreen.mainScreen.maximumFramesPerSecond; } +void front_screen_match(int fps) { (void)fps; } // iOS chooses the display mode; the Metal layer is paced separately. void front_run(void (*guest)(void)) { guest(); } // Already on main.m's big-stack thread // Software keyboard: show a UIAlertController on the main thread, the calling thread waits for the result @@ -216,52 +307,197 @@ void front_save_failed(int err) { // Audio session: the frames RemoteIO asks for in each callback follow the I/O buffer duration, about 21 ms by default on iOS (1024 frames at 48 kHz). The game keeps only 4 blocks × 5 ms = 20 ms in flight, // so every callback came up 1.3 ms short and was zero-filled, about 47 gaps per second (crackling; reproducible on the Mac by forcing 1024-frame callbacks: 1999 underruns in 2000 callbacks). Ask for 5 ms and the system picks the nearest it supports; -// the category stays the default SoloAmbient (silent switch and interrupting other apps' audio behave as before). Set once at boot and again on return to the foreground (the system may reset it after an interruption) -static int g_session_off; // the last activation failed (another app or a call still held the session): tried again when the app becomes active +// SoloAmbient remains the default, including respect for Silent Mode. An inactive +// Stage Manager card releases its non-mixing session while graphics may continue. +static int g_session_off, g_audio_bg, g_audio_intr, g_audio_inactive, g_audio_lost; +static dm_audio_retry g_audio_retry; +static unsigned g_audio_route_logs; +static int g_audio_activation_logged, g_audio_error_seen; +static NSInteger g_audio_error_code; +static NSString *g_audio_error_domain; +static int g_audio_category_mode, g_audio_category_initialized; +static void front_ios_audio_apply(void); +static void front_ios_audio_retry_schedule(void); +static void front_ios_audio_deactivate(const char *why); +void dm_audio_diagnostics(const char *why); +void dm_audio_env_callback_set(void (*fn)(const char *, const char *)); +static int front_ios_audio_blocked(void) { return g_audio_bg || g_audio_intr || g_audio_inactive || g_audio_lost; } +static const char *front_ios_audio_category_name(void) { return g_audio_category_mode == DM_AUDIO_SESSION_PLAYBACK_MIX ? "playback_mix" : "solo"; } +static void front_ios_audio_route_log_impl(const char *why, int force) { + // Events are bounded; PCM summaries are produced off the real-time callback. + if (!force && g_audio_route_logs++ >= 64) return; + AVAudioSession *s = AVAudioSession.sharedInstance; + NSMutableArray *ports = [NSMutableArray array]; + for (AVAudioSessionPortDescription *p in s.currentRoute.outputs) + [ports addObject:[NSString stringWithFormat:@"%@:%@(%lu channels)", p.portType, p.portName, (unsigned long)p.channels.count]]; + LOG("audio session status (%s): requested=%s category=%s mode=%s options=%#lx outputVolume=%.3f route=%s rate=%.0f buffer_ms=%.3f outputLatency_ms=%.3f outputChannels=%ld background=%d inactive=%d interrupted=%d lost=%d session_off=%d retry=%u/%d epoch=%llu secondarySilenceHint=%d", + why, front_ios_audio_category_name(), s.category.UTF8String, s.mode.UTF8String, (unsigned long)s.categoryOptions, s.outputVolume, + ports.description.UTF8String, s.sampleRate, s.IOBufferDuration * 1000, s.outputLatency * 1000, (long)s.outputNumberOfChannels, + g_audio_bg, g_audio_inactive, g_audio_intr, g_audio_lost, g_session_off, g_audio_retry.attempts, g_audio_retry.pending, + (unsigned long long)g_audio_retry.generation, s.secondaryAudioShouldBeSilencedHint); + dm_audio_diagnostics(why); +} +static void front_ios_audio_route_log(const char *why) { front_ios_audio_route_log_impl(why, 0); } void front_ios_audio_session(void) { + if (!NSThread.isMainThread) { dispatch_async(dispatch_get_main_queue(), ^{ front_ios_audio_session(); }); return; } + if (front_ios_audio_blocked()) { g_session_off = 1; front_ios_audio_apply(); return; } + if (!g_audio_category_initialized) { + g_audio_category_initialized = 1; + int mode = dm_audio_session_category_parse(getenv("DM_AUDIO_SESSION_CATEGORY")); + if (mode >= 0) g_audio_category_mode = mode; + else LOG("[RED] invalid DM_AUDIO_SESSION_CATEGORY; preserving SoloAmbient default"); + } + unsigned attempt = g_audio_retry.attempts; + int report = !g_audio_activation_logged || attempt <= 3; AVAudioSession *s = AVAudioSession.sharedInstance; NSError *e = nil; - if (![s setCategory:AVAudioSessionCategorySoloAmbient error:&e]) LOG("[RED] audio session category could not be set: %s", e.localizedDescription.UTF8String); - [s setPreferredSampleRate:48000 error:nil]; - if (![s setPreferredIOBufferDuration:0.005 error:&e]) LOG("[RED] audio session I/O buffer duration could not be set: %s", e.localizedDescription.UTF8String); + AVAudioSessionCategory category = g_audio_category_mode == DM_AUDIO_SESSION_PLAYBACK_MIX ? AVAudioSessionCategoryPlayback : AVAudioSessionCategorySoloAmbient; + AVAudioSessionCategoryOptions options = g_audio_category_mode == DM_AUDIO_SESSION_PLAYBACK_MIX ? AVAudioSessionCategoryOptionMixWithOthers : 0; + if (![s setCategory:category mode:AVAudioSessionModeDefault options:options error:&e] && report) LOG("[RED] audio session category could not be set: %s", e.localizedDescription.UTF8String); + e = nil; + if (![s setPreferredSampleRate:48000 error:&e] && report) LOG("[RED] audio session preferred sample rate failed: %s", e.localizedDescription.UTF8String); + e = nil; + if (![s setPreferredIOBufferDuration:0.005 error:&e] && report) LOG("[RED] audio session I/O buffer duration could not be set: %s", e.localizedDescription.UTF8String); + e = nil; g_session_off = ![s setActive:YES error:&e]; - if (g_session_off) LOG("[RED] audio session activation failed: %s", e.localizedDescription.UTF8String); - LOG("audio session: sample rate %.0f Hz, I/O buffer %.2f ms (asked for 5 ms)", s.sampleRate, s.IOBufferDuration * 1000); + if (g_session_off) { + NSString *domain = e.domain ?: @""; + int changed = !g_audio_error_seen || g_audio_error_code != e.code || ![g_audio_error_domain isEqualToString:domain]; + report |= changed; + if (report) LOG("[RED] audio session activation failed: %s", e.localizedDescription.UTF8String); + g_audio_error_seen = 1; g_audio_error_code = e.code; g_audio_error_domain = domain; + } else { + report |= g_audio_error_seen; + g_audio_error_seen = 0; g_audio_error_domain = nil; + dm_audio_retry_reset(&g_audio_retry); // stale scheduled blocks cannot restart a later lifecycle epoch + } + front_ios_audio_apply(); + if (report) { + LOG("audio session: sample rate %.0f Hz, I/O buffer %.2f ms (asked for 5 ms)", s.sampleRate, s.IOBufferDuration * 1000); + front_ios_audio_route_log(g_session_off ? "activation failed" : "activated"); + } + g_audio_activation_logged = 1; + if (g_session_off) front_ios_audio_retry_schedule(); +} +static void front_ios_audio_retry_schedule(void) { + uint64_t token = 0; + unsigned milliseconds = dm_audio_retry_plan(&g_audio_retry, front_ios_audio_blocked(), g_session_off, &token); + if (!milliseconds) return; + if (g_audio_retry.attempts <= 4) + LOG("audio session: activation retry %u in %u ms (epoch %llu); later unchanged failures remain eligible at 8-second intervals", g_audio_retry.attempts, milliseconds, (unsigned long long)token); + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)milliseconds * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{ + if (!dm_audio_retry_fire(&g_audio_retry, token, front_ios_audio_blocked(), g_session_off)) return; + front_ios_audio_session(); + }); +} +static void front_ios_audio_deactivate(const char *why) { + dm_audio_retry_reset(&g_audio_retry); + g_session_off = 1; front_ios_audio_apply(); // stop our units before releasing the shared session + NSError *e = nil; + if (![AVAudioSession.sharedInstance setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&e]) + LOG("audio session deactivation (%s) returned code=%ld: %s", why, (long)e.code, e.localizedDescription.UTF8String); + front_ios_audio_route_log(why); +} +static void front_ios_audio_env_changed(const char *key, const char *value) { + if (!key || strcmp(key, "DM_AUDIO_SESSION_CATEGORY")) return; + int mode = dm_audio_session_category_parse(value); + if (mode < 0) { LOG("[RED] audio session category diagnostic rejected: %s", value ?: "(null)"); return; } + // Parse while CTL's line buffer is valid; capture only the enum across queues. + dispatch_async(dispatch_get_main_queue(), ^{ + g_audio_category_mode = mode; g_audio_category_initialized = 1; + dm_audio_retry_reset(&g_audio_retry); // cancel any previous policy's retry + if (front_ios_audio_blocked()) { + g_session_off = 1; front_ios_audio_apply(); + front_ios_audio_route_log_impl("category diagnostic deferred until active", 1); + return; + } + uint64_t before = dm_audio_render_calls(); + front_ios_audio_route_log_impl("category diagnostic before transition", 1); + front_ios_audio_deactivate("category diagnostic transition"); + front_ios_audio_session(); + front_ios_audio_route_log_impl("category diagnostic after transition", 1); + uint64_t epoch = g_audio_retry.generation; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 250 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{ + if (epoch != g_audio_retry.generation || mode != g_audio_category_mode || front_ios_audio_blocked()) return; + uint64_t after = dm_audio_render_calls(); + LOG("audio category diagnostic settled: mode=%s new_callbacks=%llu", front_ios_audio_category_name(), (unsigned long long)(after >= before ? after - before : 0)); + front_ios_audio_route_log_impl("category diagnostic settled", 1); + }); + }); } // Real audio output is suspended (the silent sink paces the game) while the app is in the background or another audio session has interrupted ours (Siri, an alarm, a call). // Without the interruption observer the AudioUnits stayed "started" while iOS no longer called them, and a game waiting for its buffers back lost its sound until restart. // All of this runs on the main queue, like the scene callbacks -static int g_audio_bg, g_audio_intr; -static void front_ios_audio_apply(void) { dm_audio_suspend(g_audio_bg || g_audio_intr); } +static void front_ios_audio_apply(void) { dm_audio_suspend(front_ios_audio_blocked() || g_session_off); } static void front_ios_audio_watch(void) { + static int installed; if (installed++) return; + dm_audio_env_callback_set(front_ios_audio_env_changed); NSNotificationCenter *nc = NSNotificationCenter.defaultCenter; [nc addObserverForName:AVAudioSessionInterruptionNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *n) { NSUInteger type = [n.userInfo[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue], reason = [n.userInfo[AVAudioSessionInterruptionReasonKey] unsignedIntegerValue]; - if (type == AVAudioSessionInterruptionTypeBegan) { LOG("audio session: interrupted (reason %lu)", (unsigned long)reason); g_audio_intr = 1; } + if (type == AVAudioSessionInterruptionTypeBegan) { + LOG("audio session: interrupted (reason %lu)", (unsigned long)reason); + g_audio_intr = 1; g_session_off = 1; dm_audio_retry_reset(&g_audio_retry); + } else { - LOG("audio session: interruption ended"); g_audio_intr = 0; - if (!g_audio_bg) front_ios_audio_session(); + LOG("audio session: interruption ended (options %#lx)", (unsigned long)[n.userInfo[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue]); + g_audio_intr = 0; g_session_off = 1; dm_audio_retry_reset(&g_audio_retry); + // Apple permits games without a media Play/Pause UI to resume here. + if (!front_ios_audio_blocked()) front_ios_audio_session(); } front_ios_audio_apply(); + front_ios_audio_route_log("interruption notification"); + }]; + [nc addObserverForName:AVAudioSessionRouteChangeNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *n) { + NSUInteger reason = [n.userInfo[AVAudioSessionRouteChangeReasonKey] unsignedIntegerValue]; + char why[64]; snprintf(why, sizeof why, "route changed reason=%lu", (unsigned long)reason); + front_ios_audio_route_log(why); + // Category changes can be generated by our own setup. Do not recursively + // reconfigure/reactivate on every route notification or reset its budget. + if (g_session_off) front_ios_audio_retry_schedule(); + }]; + [nc addObserverForName:AVAudioSessionMediaServicesWereLostNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *n) { + (void)n; g_audio_lost = 1; g_session_off = 1; dm_audio_retry_reset(&g_audio_retry); + front_ios_audio_apply(); front_ios_audio_route_log("media services lost"); }]; [nc addObserverForName:AVAudioSessionMediaServicesWereResetNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *n) { (void)n; LOG("audio session: the media services were reset; setting the session and the outputs up again"); - front_ios_audio_session(); dm_audio_reset(); + g_audio_lost = 0; g_session_off = 1; dm_audio_retry_reset(&g_audio_retry); + front_ios_audio_apply(); dm_audio_reset(); + if (!front_ios_audio_blocked()) front_ios_audio_session(); + front_ios_audio_route_log("media services reset"); }]; } // Scene lifecycle (called by SceneDelegate in main.m): 0 resign active, 1 enter background, 2 return to foreground, 3 become active. On entering background, freeze the guest clock and suspend after in-flight GPU work completes (submitting GPU work in background gets the process killed). // The screen stays on while the game is active: only touches reset the idle timer, so with a controller (or during a cutscene) the screen dimmed and locked mid-game void front_ios_focus_event(int ev) { - if (!g_layer) return; switch (ev) { - case 0: dm_gamepad_reset(); dm_focus_set(2); break; - case 1: UIApplication.sharedApplication.idleTimerDisabled = NO; dm_focus_set(3); g_audio_bg = 1; front_ios_audio_apply(); guest_clock_freeze_for(1, 1); dm_gfx_suspend(1); break; - case 2: dm_gfx_suspend(0); guest_clock_freeze_for(1, 0); front_ios_audio_session(); g_audio_bg = 0; front_ios_audio_apply(); dm_focus_set(2); break; + case 0: + front_ios_display_refresh_active(0); + g_audio_inactive = 1; front_ios_audio_deactivate("scene resigned active"); + dm_gamepad_set_active(0); + if (g_layer) dm_focus_set(2); break; + case 1: + front_ios_display_refresh_active(0); + g_audio_bg = 1; front_ios_audio_deactivate("scene entered background"); + dm_gamepad_set_active(0); + if (g_layer) { UIApplication.sharedApplication.idleTimerDisabled = NO; dm_focus_set(3); guest_clock_freeze_for(1, 1); dm_gfx_suspend(1); } break; + case 2: + g_audio_bg = 0; // still inactive until didBecomeActive; keep audio released + if (g_layer) { dm_gfx_suspend(0); guest_clock_freeze_for(1, 0); dm_focus_set(2); } break; case 3: // Becoming active again also ends an interruption whose "ended" never came (not every one sends it), and retries a session that would not activate on the way back - UIApplication.sharedApplication.idleTimerDisabled = YES; - if (g_audio_intr) { LOG("audio session: active again with no interruption-ended notice; resuming"); g_audio_intr = 0; g_session_off = 1; } - if (g_session_off) front_ios_audio_session(); - front_ios_audio_apply(); dm_focus_set(1); break; + { + front_ios_display_refresh_active(1); + int transitioned = g_audio_inactive || g_audio_bg || g_audio_intr; + g_audio_inactive = 0; g_audio_bg = 0; + dm_gamepad_set_active(1); + if (g_layer) UIApplication.sharedApplication.idleTimerDisabled = YES; + if (g_audio_intr) { LOG("audio session: active again with no interruption-ended notice; resuming"); g_audio_intr = 0; g_session_off = 1; } + if (transitioned) dm_audio_retry_reset(&g_audio_retry); + if (g_session_off && !g_audio_retry.pending && !g_audio_retry.attempts) front_ios_audio_session(); + front_ios_audio_apply(); if (g_layer) dm_focus_set(1); + front_ios_audio_route_log("scene became active"); + } break; } } diff --git a/dm/ios/Sources/main.m b/dm/ios/Sources/main.m index 1afea818de..48902becbf 100644 --- a/dm/ios/Sources/main.m +++ b/dm/ios/Sources/main.m @@ -68,6 +68,8 @@ static int boot_env(void) { NSString *log = [doc stringByAppendingPathComponent:@"log.txt"], *prev = [doc stringByAppendingPathComponent:@"log.prev.txt"]; [fm removeItemAtPath:prev error:nil]; [fm moveItemAtPath:log toPath:prev error:nil]; if (freopen(log.fileSystemRepresentation, "w", stderr)) setvbuf(stderr, NULL, _IOLBF, 0); + else NSLog(@"[RED] boot: cannot open Documents log (errno %d)", errno); + NSLog(@"boot: Documents writable=%d", [fm isWritableFileAtPath:doc]); // Environment switch files (for on-device diagnostics: a sideloaded app cannot be given environment variables from outside): one KEY=VALUE per line, # starts a comment; Documents/env.txt takes precedence over dm/env.txt in the bundle (put there by build_ipa.sh's DM_ENV_FILE) NSMutableArray *from_file = [NSMutableArray array], *from_launch = [NSMutableArray array]; for (char **e = environ; *e; e++) { // set at launch (Xcode, devicectl): before the env files, and the launcher keeps them @@ -127,11 +129,17 @@ void front_ios_boot(void) { // Called by front_ios.m once the window and Metal } int front_ios_boot_ok(void) { return g_boot_ok; } +@class SceneDelegate; @interface App : UIResponder @end @implementation App - (UISceneConfiguration *)application:(UIApplication *)a configurationForConnectingSceneSession:(UISceneSession *)s options:(UISceneConnectionOptions *)o { - return [[UISceneConfiguration alloc] initWithName:@"Default" sessionRole:s.role]; + // Returning a newly allocated configuration does not reliably inherit the + // delegate named by the Info.plist manifest on iOS 16. Bind it explicitly; + // otherwise UIKit waits for scene creation until the launch watchdog fires. + UISceneConfiguration *c = [[UISceneConfiguration alloc] initWithName:@"Default" sessionRole:s.role]; + c.delegateClass = NSClassFromString(@"SceneDelegate"); + return c; } @end diff --git a/dm/ios/project.yml b/dm/ios/project.yml index 4a1e3b6525..f0a27ae5bd 100644 --- a/dm/ios/project.yml +++ b/dm/ios/project.yml @@ -3,7 +3,7 @@ name: DmIOS options: bundleIdPrefix: ${DM_BUNDLE_ID} deploymentTarget: - iOS: "26.0" # Graphics replay uses Metal 4 (MTL4CommandQueue etc., from iOS 26) + iOS: "16.1" # Traditional Metal command encoders; the iPad target is iPadOS 16.1. configFiles: # Generated by build_ipa.sh: embedded-segment link flags + header paths and frameworks taken from dm/host/Makefile Release: Gen/dm.xcconfig Debug: Gen/dm.xcconfig @@ -13,7 +13,10 @@ targets: platform: iOS sources: - path: Sources - excludes: [Info.plist] # generated per project under Gen/, so games never share the generated plist + # SwiftUI's property-wrapper macro plugin cannot run in the iPad-hosted + # macOS build environment. The iOS 16 build starts directly with the + # generated Info.plist/env defaults instead. + excludes: [Info.plist, Launcher.swift] - path: Gen/embed.S - path: Gen/dm # Rewriter tables (*.fixups.tsv / gates / cuts / obs) + shader map / c1 list / warm-up list type: folder @@ -73,7 +76,7 @@ targets: TARGETED_DEVICE_FAMILY: "1,2" ENABLE_BITCODE: NO GCC_C_LANGUAGE_STANDARD: gnu11 - OTHER_CFLAGS: $(inherited) -mcpu=apple-a18 # The host assembly contains LSE atomic instructions, which the default baseline does not accept + OTHER_CFLAGS: $(inherited) -mcpu=apple-a10 # Keep the shipped binary on the pre-LSE baseline; newer devices run it unchanged. CLANG_ENABLE_OBJC_ARC: YES SWIFT_VERSION: "5.0" # Launcher.swift (SwiftUI) DEBUG_INFORMATION_FORMAT: dwarf diff --git a/dm/ios/tests/test_display_refresh.py b/dm/ios/tests/test_display_refresh.py new file mode 100644 index 0000000000..52d293a6b4 --- /dev/null +++ b/dm/ios/tests/test_display_refresh.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 +"""Exercise screen-request policy and independent timestamp/FPS measurements.""" +from pathlib import Path +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[3] + + +class DisplayRefresh(unittest.TestCase): + def test_policy_and_measured_cadence(self): + harness = r''' +#include +#include +#include "DmDisplayRefresh.h" +static void near(double a, double b) { assert(fabs(a-b) < 0.000001); } +static void window(dm_display_probe *probe, int callbacks, int presents_per_four_ticks, + double start, uint64_t base, double expected_callback, double expected_fps) { + dm_display_probe_result result; + assert(!dm_display_probe_tick(probe, start, start+1.0/callbacks, base, &result)); + for (int i=1; i<=callbacks*2; ++i) { + double timestamp=start+(double)i/callbacks; + uint64_t presented=base+(uint64_t)i*presents_per_four_ticks/4; + int ready=dm_display_probe_tick(probe,timestamp,timestamp+1.0/callbacks,presented,&result); + if (i +#include "DmFaceLayout.h" +static void check(dm_face_layout *layout, const int expected[4][2]) { + for (unsigned button = 0; button < 4; ++button) { + dm_face_offset p = dm_face_layout_offset(layout, button); + assert(p.x == expected[button][0] && p.y == expected[button][1]); + for (unsigned other = 0; other < button; ++other) { + dm_face_offset q = dm_face_layout_offset(layout, other); + assert(p.x != q.x || p.y != q.y); + } + } +} +int main(void) { + const int normal[4][2] = {{0,1}, {1,0}, {-1,0}, {0,-1}}; + const int skill[4][2] = {{1,0}, {0,1}, {0,-1}, {-1,0}}; + dm_face_layout layout = {0}; + check(&layout, normal); + // Showing a selector while any finger owns the diamond cannot move it, + // including a finger which currently presses no bits between two keys. + assert(!dm_face_layout_update(&layout, 1, 1)); + check(&layout, normal); + assert(dm_face_layout_update(&layout, 1, 0)); + check(&layout, skill); + assert(!dm_face_layout_update(&layout, 1, 0)); + // Closing while held also waits for release. Intermediate state changes + // do not queue a stale layout; the latest observed selector wins. + assert(!dm_face_layout_update(&layout, 0, 1)); + check(&layout, skill); + assert(!dm_face_layout_update(&layout, 1, 1)); + assert(!dm_face_layout_update(&layout, 1, 0)); + check(&layout, skill); + assert(dm_face_layout_update(&layout, 0, 0)); + check(&layout, normal); + assert(!dm_face_layout_update(&layout, 0, 0)); + return 0; +} +''' + with tempfile.TemporaryDirectory() as directory: + unit, executable = Path(directory) / 'layout.c', Path(directory) / 'layout' + unit.write_text(harness) + subprocess.run(['xcrun', 'clang', '-Wall', '-Werror', '-I', + str(ROOT / 'dm/ios/Sources'), str(unit), '-o', str(executable)], check=True) + subprocess.run([str(executable)], check=True) + + def test_shared_render_hit_test_geometry_and_native_inputs(self): + source = (ROOT / 'dm/ios/Sources/DmGamepad.m').read_text() + update = source[source.index('- (void)updateSkillFaceLayout {'):source.index('- (CGPoint)stickHome')] + self.assertIn('_faceOf.count != 0', update) + self.assertIn('dm_za_skill_layout_active()', update) + self.assertIn('NSThread.isMainThread', update) + self.assertIn('[self layoutIfNeeded]', update) + hit = source[source.index('- (uint64_t)faceAt:'):source.index('- (uint64_t)faceAt:') + 450] + self.assertIn('[self facePos:b]', hit) + layout = source[source.index('- (void)layoutSubviews {'):source.index('- (void)safeAreaInsetsDidChange')] + self.assertIn('[self facePos:b]', layout) + publish = source[source.index('static void publish('):source.index('// ---- Shaking')] + self.assertNotIn('dm_za_skill_', publish) + self.assertNotIn('dm_skill_buttons_apply', publish) + self.assertIn('g_touch_native = g_touch_face | g_touch_btn;', publish) + self.assertIn('uint64_t keyboard = g_key_btn;', publish) + host = (ROOT / 'dm/host/hle_za_timing.c').read_text() + self.assertIn('getenv("DM_ZA_SKILL_LAYOUT")', host) + self.assertNotIn('DM_ZA_SKILL_DIRECTIONS', host) + + +if __name__ == '__main__': + unittest.main() diff --git a/dm/ios/tests/test_keyboard_bindings.py b/dm/ios/tests/test_keyboard_bindings.py new file mode 100644 index 0000000000..d212771552 --- /dev/null +++ b/dm/ios/tests/test_keyboard_bindings.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python3 +"""Exercise the actual frontend keyboard function against the original bindings.""" +from pathlib import Path +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[3] + +class KeyboardBindings(unittest.TestCase): + def test_original_keys_and_native_publication(self): + source = (ROOT / "dm/ios/Sources/DmGamepad.m").read_text() + start = source.index("static uint64_t keyboard_button(") + end = source.index("\nstatic void read_key(", start) + code = source[start:end] + harness = """#import +#include +#include +#include +#include "dm_input.h" +""" + code + """ +int main(void) { + unsetenv("DM_KEY_ENTER_A"); + assert(keyboard_button(GCKeyCodeKeyJ) == DM_BTN_A); + assert(keyboard_button(GCKeyCodeKeyK) == DM_BTN_B); + assert(keyboard_button(GCKeyCodeKeyU) == DM_BTN_X); + assert(keyboard_button(GCKeyCodeKeyI) == DM_BTN_Y); + assert(keyboard_button(GCKeyCodeKeyL) == 0); + assert(keyboard_button(GCKeyCodeKeyW) == 0); + assert(keyboard_button(GCKeyCodeKeyQ) == DM_BTN_L); + assert(keyboard_button(GCKeyCodeKeyE) == DM_BTN_R); + assert(keyboard_button(GCKeyCodeKeyZ) == DM_BTN_ZL); + assert(keyboard_button(GCKeyCodeKeyC) == DM_BTN_ZR); + assert(keyboard_button(GCKeyCodeReturnOrEnter) == DM_BTN_PLUS); + setenv("DM_KEY_ENTER_A", "1", 1); + assert(keyboard_button(GCKeyCodeReturnOrEnter) == DM_BTN_A); + assert(keyboard_button(GCKeyCodeTab) == DM_BTN_MINUS); + assert(keyboard_button(GCKeyCodeLeftArrow) == DM_BTN_LEFT); + assert(keyboard_button(GCKeyCodeUpArrow) == DM_BTN_UP); + assert(keyboard_button(GCKeyCodeRightArrow) == DM_BTN_RIGHT); + assert(keyboard_button(GCKeyCodeDownArrow) == DM_BTN_DOWN); + return 0; +} +""" + with tempfile.TemporaryDirectory() as temp: + unit, exe = Path(temp)/"keyboard.m", Path(temp)/"keyboard" + unit.write_text(harness) + subprocess.run(["xcrun", "clang", "-x", "objective-c", "-I", str(ROOT/"dm/host"), str(unit), "-framework", "GameController", "-o", str(exe)], check=True) + subprocess.run([str(exe)], check=True) + publish = source[source.index("static void publish("):source.index("// ---- Shaking")] + self.assertNotIn("dm_skill_buttons_apply", publish) + self.assertNotIn("dm_za_skill_input_active", publish) + self.assertIn("g_touch_native = g_touch_face | g_touch_btn;", publish) + self.assertIn("uint64_t keyboard = g_key_btn;", publish) + self.assertIn("g_key_rx = g_key_ry = 0;", source) + + def test_touch_dpad_is_available_in_simple_and_full_layouts(self): + source = (ROOT / "dm/ios/Sources/DmGamepad.m").read_text() + self.assertIn("static BOOL fullOnly(int b) { return b == B_LS || b == B_RS; }", source) + began = source[source.index("- (void)touchesBegan:"):source.index("- (void)touchesMoved:")] + self.assertIn("kind = C_DPAD", began) + self.assertNotIn("if (_full) { CGFloat d = dist(p, self.dpadCenter)", began) + + def test_game_profiles_force_complete_touch_surface_beside_external_input(self): + source = (ROOT / "dm/ios/Sources/DmGamepad.m").read_text() + for title_id in ("0100F43008C44000", "0100152000022000"): + with self.subTest(title_id=title_id): + profile = (ROOT / f"dm/games/{title_id}/game.tsv").read_text() + environment = (ROOT / f"dm/games/{title_id}/ios.env").read_text() + self.assertIn("pad_layout\tfull", profile) + self.assertIn("DM_PAD_FORCE_FULL=1", environment) + self.assertIn("DM_PAD_KEEP_TOUCH=1", environment) + + self.assertIn('static BOOL pad_force_full(void)', source) + self.assertIn('if (pad_force_full()) l = 1;', source) + self.assertIn('int l = pad_force_full() ? 1', source) + self.assertIn('BOOL any = !keepTouch &&', source) + mouse = source[source.index("- (void)mouseMotionX:"):source.index("- (void)inputDevicesChanged")] + self.assertIn('if (!pad_keep_touch() &&', mouse) + + # The complete visible surface still publishes the original native + # Switch bits; no battle-specific face remapping is introduced. + self.assertIn("g_touch_native = g_touch_face | g_touch_btn;", source) + self.assertIn("{B_ZL, B_L, B_ZR, B_R, B_MINUS, B_PLUS, B_LS, B_RS}", source) + +if __name__ == "__main__": + unittest.main() diff --git a/dm/ios/tests/test_profile_env.py b/dm/ios/tests/test_profile_env.py new file mode 100644 index 0000000000..f723c5bfec --- /dev/null +++ b/dm/ios/tests/test_profile_env.py @@ -0,0 +1,93 @@ +import importlib.util +import tempfile +import unittest +from pathlib import Path + + +IOS = Path(__file__).resolve().parents[1] +ZA_PROFILE = IOS.parent / "games/0100F43008C44000" +SCRIPT = IOS / "tools/stage_profile_env.py" +spec = importlib.util.spec_from_file_location("stage_profile_env", SCRIPT) +profile_env = importlib.util.module_from_spec(spec) +assert spec.loader is not None +spec.loader.exec_module(profile_env) + + +class ProfileEnvironmentTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.root = Path(self.temp.name) + self.profile = self.root / "profile" + self.profile.mkdir() + self.output = self.root / "Gen/dm/env.txt" + + def tearDown(self): + self.temp.cleanup() + + def test_profile_default_is_staged_when_no_override_exists(self): + default = self.profile / "ios.env" + default.write_bytes(b"DM_PRESENT_HZ=120\n") + self.assertEqual(profile_env.stage(self.profile, self.output, {}), default) + self.assertEqual(self.output.read_bytes(), default.read_bytes()) + + def test_explicit_file_has_priority(self): + (self.profile / "ios.env").write_text("DM_PRESENT_HZ=120\n") + explicit = self.root / "diagnostic.env" + explicit.write_text("DM_PRESENT_HZ=60\n") + self.assertEqual( + profile_env.stage(self.profile, self.output, {"DM_ENV_FILE": str(explicit)}), + explicit, + ) + self.assertEqual(self.output.read_text(), "DM_PRESENT_HZ=60\n") + + def test_explicit_empty_value_disables_profile_default(self): + (self.profile / "ios.env").write_text("DM_PRESENT_HZ=120\n") + self.output.parent.mkdir(parents=True) + self.output.write_text("DM_PRESENT_HZ=30\n") + self.assertIsNone(profile_env.stage(self.profile, self.output, {"DM_ENV_FILE": ""})) + self.assertFalse(self.output.exists()) + + def test_missing_explicit_file_fails_closed(self): + with self.assertRaisesRegex(FileNotFoundError, "iOS environment file not found"): + profile_env.stage( + self.profile, + self.output, + {"DM_ENV_FILE": str(self.root / "missing.env")}, + ) + + def test_device_documents_keep_precedence_over_bundled_defaults(self): + source = (IOS / "Sources/main.m").read_text() + documents = 'for (NSString *ef in @[[doc stringByAppendingPathComponent:@"env.txt"]' + bundle = '[[res stringByAppendingPathComponent:@"dm"] stringByAppendingPathComponent:@"env.txt"]])' + self.assertLess(source.index(documents), source.index(bundle)) + self.assertIn("if (!getenv(k.UTF8String))", source) + + def test_za_profile_has_only_release_defaults(self): + values = [ + line + for line in (ZA_PROFILE / "ios.env").read_text().splitlines() + if line and not line.startswith("#") + ] + self.assertEqual( + values, + [ + "DM_PRESENT_INTERVAL=game", + "DM_ZA_SKILL_LAYOUT=1", + "DM_DISPLAY_HZ=120", + "DM_PRESENT_HZ=120", + "DM_ZA_FPS_MODE=dynamic60", + "DM_PAD_FORCE_FULL=1", + "DM_PAD_KEEP_TOUCH=1", + ], + ) + + def test_build_script_stages_the_selected_profile_environment(self): + source = (IOS / "tools/build_ipa.sh").read_text() + self.assertIn( + 'ENV_SOURCE=$(python3 tools/stage_profile_env.py "$GD" Gen/dm/env.txt)', + source, + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/dm/ios/tests/test_sampler_limit_sources.py b/dm/ios/tests/test_sampler_limit_sources.py new file mode 100644 index 0000000000..87f45ab9ba --- /dev/null +++ b/dm/ios/tests/test_sampler_limit_sources.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python3 +"""Regression tests for exact unused-sampler companions.""" +import copy +import hashlib +import importlib.util +import json +from pathlib import Path +import tempfile +import unittest + + +TOOL = Path(__file__).resolve().parents[1] / "tools/export_sampler_limit_sources.py" +SPEC = importlib.util.spec_from_file_location("sampler_limit", TOOL) +SAMPLER = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(SAMPLER) + + +def fixture(): + declarations = [] + bindings = [] + samples = [] + aid = 0 + for index in range(16): + texture = f"texture_{index}" + sampler = f"sampler_{index}" + declarations += [f"texture2d {texture} [[id({aid})]];", f"sampler {sampler} [[id({aid + 1})]];"] + bindings += [ + {"msl_name": texture, "msl_type": "texture2d", "arg_id": aid, "kind": "tex", "reg": index}, + {"msl_name": sampler, "msl_type": "sampler", "arg_id": aid + 1, "kind": "samp_tex", "reg": index}, + ] + samples.append(f"color += args.{texture}.sample(args.{sampler}, float2(0.5));") + aid += 2 + declarations += ["texture2d_array read_only [[id(32)]];", "sampler unused_read_sampler [[id(33)]];"] + bindings += [ + {"msl_name": "read_only", "msl_type": "texture2d_array", "arg_id": 32, "kind": "tex", "reg": 24}, + {"msl_name": "unused_read_sampler", "msl_type": "sampler", "arg_id": 33, "kind": "samp_tex", "reg": 24}, + ] + source = """#pragma clang diagnostic ignored \"-Wmissing-prototypes\" +#include +using namespace metal; +struct spvDescriptorSetBuffer0 { +%s +}; +fragment float4 main0(constant spvDescriptorSetBuffer0& args [[buffer(0)]]) { + float4 color = float4(0.0); + %s + color += args.read_only.read(uint2(0), 0); + return color; +} +""" % ("\n".join(declarations), "\n ".join(samples)) + meta = {"key": "1111111111111111_b", "chain": "test", "route": "B", "stage": "ps", "entry": "main0", "bindings": bindings} + return source, meta + + +class SamplerLimitProof(unittest.TestCase): + def test_removes_only_globally_unused_scalar_sampler(self): + source, meta = fixture() + result, proof = SAMPLER.lower(source, meta) + self.assertEqual(proof["original_sampler_count"], 17) + self.assertEqual(proof["remaining_sampler_count"], 16) + self.assertEqual(proof["removed"], [{"msl_name": "unused_read_sampler", "msl_type": "sampler", "arg_id": 33, + "kind": "samp_tex", "reg": 24}]) + self.assertNotIn("sampler unused_read_sampler [[id(33)]];", result) + self.assertIn("args.read_only.read(uint2(0), 0)", result) + for index in range(16): + self.assertIn(f"sampler sampler_{index} [[id({index * 2 + 1})]];", result) + self.assertIn(f"args.texture_{index}.sample(args.sampler_{index}", result) + + def test_identifier_in_any_helper_or_entry_is_not_unused(self): + source, meta = fixture() + source += "\nfloat helper(constant spvDescriptorSetBuffer0& args) { return args.unused_read_sampler.get_lod_min_clamp(); }\n" + with self.assertRaisesRegex(ValueError, "Not enough statically unused"): + SAMPLER.lower(source, meta) + + def test_all_seventeen_sampled_is_rejected(self): + source, meta = fixture() + source = source.replace("args.read_only.read(uint2(0), 0)", "args.read_only.sample(args.unused_read_sampler, float2(0.0), 0)") + with self.assertRaisesRegex(ValueError, "Not enough statically unused"): + SAMPLER.lower(source, meta) + + def test_reflection_or_declaration_disagreement_is_rejected(self): + source, meta = fixture() + changes = [] + changed = copy.deepcopy(meta) + changed["bindings"][-1]["arg_id"] = 34 + changes.append((source, changed, "argument ID mismatch")) + changed = copy.deepcopy(meta) + changed["bindings"][-1]["msl_name"] = "other_sampler" + changes.append((source, changed, "declarations and reflection")) + changed = copy.deepcopy(meta) + changed["bindings"][-1]["kind"] = "tex" + changes.append((source, changed, "kind mismatch")) + changed = copy.deepcopy(meta) + changed["bindings"][-1]["arg_id"] = changed["bindings"][-2]["arg_id"] + changes.append((source, changed, "Duplicate resource")) + for candidate_source, candidate_meta, reason in changes: + with self.subTest(reason=reason): + with self.assertRaisesRegex(ValueError, reason): + SAMPLER.lower(candidate_source, candidate_meta) + + def test_sampler_array_and_preprocessor_rejected(self): + source, meta = fixture() + array_meta = copy.deepcopy(meta) + array_meta["bindings"][-1]["msl_type"] = "array" + with self.assertRaisesRegex(ValueError, "Sampler arrays"): + SAMPLER.lower(source, array_meta) + for directive in ("#define unused_read_sampler hidden\n", "#if 1\n#endif\n"): + with self.subTest(directive=directive): + with self.assertRaisesRegex(ValueError, "Unsupported preprocessor"): + SAMPLER.lower(directive + source, meta) + + def test_comments_and_strings_do_not_make_sampler_live(self): + source, meta = fixture() + source = source.replace("float4 color", "// unused_read_sampler\n float4 color") + result, proof = SAMPLER.lower(source, meta) + self.assertNotIn("sampler unused_read_sampler", result) + self.assertEqual(proof["remaining_sampler_count"], 16) + + def test_export_binds_source_library_and_reflection_hashes(self): + source, meta = fixture() + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + cache = root / "cache/b-test" + libraries = root / "libraries/b-test" + output = root / "output/sampler-limit-v1" + cache.mkdir(parents=True) + libraries.mkdir(parents=True) + key = meta["key"] + source_bytes = source.encode() + reflection_bytes = json.dumps(meta, separators=(",", ":")).encode() + library_bytes = b"synthetic base Metal library" + (cache / (key + ".metal")).write_bytes(source_bytes) + (libraries / (key + ".json")).write_bytes(reflection_bytes) + (libraries / (key + ".metallib")).write_bytes(library_bytes) + + records = SAMPLER.export(cache, libraries, output, "test") + record = records[key] + self.assertTrue(record["supported"]) + self.assertEqual(record["source"], key + ".metal") + for field, data in (("original_source_sha256", source_bytes), ("reflection_sha256", reflection_bytes), + ("base_library_sha256", library_bytes), + ("source_sha256", (output / record["source"]).read_bytes())): + self.assertEqual(record[field], hashlib.sha256(data).hexdigest()) + manifest = json.loads((output / "manifest.json").read_text()) + self.assertEqual(manifest["kind"], "sampler-limit-v1") + self.assertEqual(manifest["limit"], 16) + self.assertEqual((libraries / (key + ".metallib")).read_bytes(), library_bytes) + + first = (output / record["source"]).read_bytes() + self.assertEqual(SAMPLER.export(cache, libraries, output, "test")[key]["source_sha256"], hashlib.sha256(first).hexdigest()) + self.assertEqual((output / record["source"]).read_bytes(), first) + + def test_unproven_export_is_recorded_but_not_emitted(self): + source, meta = fixture() + source = source.replace("args.read_only.read(uint2(0), 0)", "args.read_only.sample(args.unused_read_sampler, float2(0.0), 0)") + with tempfile.TemporaryDirectory() as temporary: + root = Path(temporary) + cache = root / "b-test" + libraries = root / "libraries/b-test" + output = root / "sampler-limit-v1" + cache.mkdir(parents=True) + libraries.mkdir(parents=True) + key = meta["key"] + (cache / (key + ".metal")).write_text(source) + (libraries / (key + ".json")).write_text(json.dumps(meta)) + (libraries / (key + ".metallib")).write_bytes(b"base") + record = SAMPLER.export(cache, libraries, output, "test")[key] + self.assertFalse(record["supported"]) + self.assertIn("Not enough statically unused", record["reason"]) + self.assertFalse((output / (key + ".metal")).exists()) + + +if __name__ == "__main__": + unittest.main() diff --git a/dm/ios/tests/test_shader_companion_validation.py b/dm/ios/tests/test_shader_companion_validation.py new file mode 100644 index 0000000000..fc46662923 --- /dev/null +++ b/dm/ios/tests/test_shader_companion_validation.py @@ -0,0 +1,191 @@ +#!/usr/bin/env python3 +"""Packaging preflight regressions for profile-required NVN companions.""" +import hashlib +import importlib.util +import json +from pathlib import Path +import tempfile +import unittest + + +IOS = Path(__file__).resolve().parents[1] +TOOL = IOS / "tools/validate_shader_companions.py" +SPEC = importlib.util.spec_from_file_location("validate_shader_companions", TOOL) +VALIDATOR = importlib.util.module_from_spec(SPEC) +assert SPEC.loader is not None +SPEC.loader.exec_module(VALIDATOR) + + +def digest(data): + return hashlib.sha256(data).hexdigest() + + +class ShaderCompanionValidationTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + root = Path(self.temp.name) + self.libraries = root / "b-test" + self.libraries.mkdir() + self.profile = root / "ios_companions.json" + self.keys = { + "nonpoint": "1111111111111111_b", + "layered": "2222222222222222_b", + "sampler": "3333333333333333_b", + "zero": "4444444444444444_b", + "atomic": "5555555555555555_b", + } + for key in self.keys.values(): + (self.libraries / (key + ".metallib")).write_bytes(("library-" + key).encode()) + (self.libraries / (key + ".json")).write_bytes(("reflection-" + key).encode()) + + self._manifest("nonpoint-v1", None, self.keys["nonpoint"], explicit_source=False) + self._manifest("layered-v1", "layered-slices-v1", self.keys["layered"], explicit_source=False) + self._manifest("sampler-limit-v1", "sampler-limit-v1", self.keys["sampler"], limit=16) + self._manifest("guarded-index-v1", "guarded-index-v1", self.keys["nonpoint"], + original_directory="nonpoint-v1") + self._manifest("zero-image-v1", "zero-image-clear-v1", self.keys["zero"]) + + atomic = self.libraries / "atomic-v1" + atomic.mkdir() + key = self.keys["atomic"] + source = b"bounded atomic source" + (atomic / (key + ".metal")).write_bytes(source) + (atomic / (key + ".json")).write_text(json.dumps({ + "schema": 1, "chain": "test", "kind": "bounded-image-atomics-v1", + "source_sha256": digest(source), + "library_sha256": digest((self.libraries / (key + ".metallib")).read_bytes()), + })) + + requirements = { + "schema": 1, + "manifests": [ + {"directory": "nonpoint-v1", "required_records": [self.keys["nonpoint"]]}, + {"directory": "layered-v1", "kind": "layered-slices-v1", + "required_records": [self.keys["layered"]]}, + {"directory": "sampler-limit-v1", "kind": "sampler-limit-v1", "limit": 16, + "required_records": [self.keys["sampler"]]}, + {"directory": "guarded-index-v1", "kind": "guarded-index-v1", + "original_directory": "nonpoint-v1", "required_records": [self.keys["nonpoint"]]}, + {"directory": "zero-image-v1", "kind": "zero-image-clear-v1", + "required_records": [self.keys["zero"]]}, + ], + "records": [{"directory": "atomic-v1", "kind": "bounded-image-atomics-v1", + "required_records": [self.keys["atomic"]]}], + } + self.profile.write_text(json.dumps(requirements)) + + def tearDown(self): + self.temp.cleanup() + + def _manifest(self, directory, kind, key, explicit_source=True, limit=None, original_directory=None): + where = self.libraries / directory + where.mkdir() + source = ("source-" + directory).encode() + name = key + ".metal" + (where / name).write_bytes(source) + record = { + "source_sha256": digest(source), + "base_library_sha256": digest((self.libraries / (key + ".metallib")).read_bytes()), + "reflection_sha256": digest((self.libraries / (key + ".json")).read_bytes()), + } + if explicit_source: + record["source"] = name + if original_directory: + record["original_source_sha256"] = digest( + (self.libraries / original_directory / name).read_bytes()) + manifest = {"schema": 1, "chain": "test", "shaders": {key: record}} + if kind is not None: + manifest["kind"] = kind + if limit is not None: + manifest["limit"] = limit + (where / "manifest.json").write_text(json.dumps(manifest)) + + def test_complete_profile_passes(self): + self.assertEqual(VALIDATOR.validate(self.profile, self.libraries, "test"), (5, 5, 1)) + + def test_missing_manifest_fails_closed(self): + (self.libraries / "layered-v1/manifest.json").unlink() + with self.assertRaisesRegex(VALIDATOR.CompanionError, "missing layered-v1 manifest"): + VALIDATOR.validate(self.profile, self.libraries, "test") + + def test_manifest_chain_kind_and_limit_are_exact(self): + path = self.libraries / "sampler-limit-v1/manifest.json" + for field, value, reason in (("chain", "other", "schema/chain"), + ("kind", "other", "manifest kind"), + ("limit", 17, "manifest limit")): + original = path.read_text() + manifest = json.loads(original) + manifest[field] = value + path.write_text(json.dumps(manifest)) + with self.subTest(field=field), self.assertRaisesRegex(VALIDATOR.CompanionError, reason): + VALIDATOR.validate(self.profile, self.libraries, "test") + path.write_text(original) + + def test_required_record_cannot_be_absent_or_unsupported(self): + path = self.libraries / "zero-image-v1/manifest.json" + original = json.loads(path.read_text()) + manifest = json.loads(path.read_text()) + manifest["shaders"] = {} + path.write_text(json.dumps(manifest)) + with self.assertRaises(VALIDATOR.CompanionError): + VALIDATOR.validate(self.profile, self.libraries, "test") + manifest = original + manifest["shaders"][self.keys["zero"]]["supported"] = False + path.write_text(json.dumps(manifest)) + with self.assertRaises(VALIDATOR.CompanionError): + VALIDATOR.validate(self.profile, self.libraries, "test") + + def test_every_emitted_source_and_hash_is_checked(self): + (self.libraries / "layered-v1" / (self.keys["layered"] + ".metal")).unlink() + with self.assertRaisesRegex(VALIDATOR.CompanionError, "missing or unreadable companion file"): + VALIDATOR.validate(self.profile, self.libraries, "test") + + def test_source_name_must_match_its_shader_record(self): + path = self.libraries / "zero-image-v1/manifest.json" + manifest = json.loads(path.read_text()) + record = manifest["shaders"][self.keys["zero"]] + original = self.libraries / "zero-image-v1" / record["source"] + other = self.libraries / "zero-image-v1/other.metal" + other.write_bytes(original.read_bytes()) + record["source"] = other.name + path.write_text(json.dumps(manifest)) + with self.assertRaisesRegex(VALIDATOR.CompanionError, "source name does not match"): + VALIDATOR.validate(self.profile, self.libraries, "test") + + def test_guarded_original_source_is_checked(self): + original = self.libraries / "nonpoint-v1" / (self.keys["nonpoint"] + ".metal") + original.write_bytes(b"changed") + with self.assertRaisesRegex(VALIDATOR.CompanionError, "source_sha256 mismatch"): + VALIDATOR.validate(self.profile, self.libraries, "test") + + def test_atomic_record_and_source_are_mandatory(self): + (self.libraries / "atomic-v1" / (self.keys["atomic"] + ".json")).unlink() + with self.assertRaisesRegex(VALIDATOR.CompanionError, "missing atomic-v1 record"): + VALIDATOR.validate(self.profile, self.libraries, "test") + + def test_za_profile_names_observed_a10x_requirements(self): + profile = IOS.parent / "games/0100F43008C44000" + self.assertIn("ios_companions\tios_companions.json", (profile / "game.tsv").read_text()) + requirements = json.loads((profile / "ios_companions.json").read_text()) + manifests = {item["directory"]: item for item in requirements["manifests"]} + self.assertEqual(set(manifests), {"nonpoint-v1", "layered-v1", "sampler-limit-v1", + "guarded-index-v1", "zero-image-v1"}) + self.assertEqual(set(manifests["layered-v1"]["required_records"]), { + "57246f9a04af8529_b", "d7d72bbbb0e32517_b", + "b29f08619f379a83_b", "5b931243ad28aeeb_b", + }) + self.assertIn("a6c06cda878a4fe0_b", manifests["sampler-limit-v1"]["required_records"]) + self.assertIn("ce7eacf81c6e4ffc_b", manifests["guarded-index-v1"]["required_records"]) + self.assertIn("5b107ddfd06551fc_b", manifests["zero-image-v1"]["required_records"]) + self.assertEqual(requirements["records"][0]["required_records"], ["2bd5037003980716_b"]) + + def test_build_preflight_runs_before_generation_and_packing(self): + source = (IOS / "tools/build_ipa.sh").read_text() + check = 'python3 tools/validate_shader_companions.py "$GD/$COMPANION_REQUIREMENTS"' + self.assertLess(source.index(check), source.index('t "generate Gen/')) + self.assertLess(source.index(check), source.index("python3 tools/pack_ipa.py")) + self.assertIn('die "profile companion requirements are missing:', source) + + +if __name__ == "__main__": + unittest.main() diff --git a/dm/ios/tests/test_za_readback_diagnostic.py b/dm/ios/tests/test_za_readback_diagnostic.py new file mode 100644 index 0000000000..db4f4e73b8 --- /dev/null +++ b/dm/ios/tests/test_za_readback_diagnostic.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +"""Keep production Z-A GPU readback diagnostics strictly opt-in.""" +from pathlib import Path +import unittest + + +ROOT = Path(__file__).resolve().parents[2] +SOURCE = ROOT / "gfx/nvn/nvn_mtl_legacy.inc" +DISPATCH = ROOT / "gfx/nvn/nvn_mtl.m" + + +def body(source, signature): + at = 0 + while True: + start = source.index(signature, at) + opening = source.index("{", start) + semicolon = source.find(";", start, opening) + if semicolon < 0: + break + at = semicolon + 1 + depth = 1 + for index in range(opening + 1, len(source)): + depth += (source[index] == "{") - (source[index] == "}") + if not depth: + return source[opening + 1:index] + raise AssertionError("unbalanced function") + + +class ZAReadbackDiagnosticTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.source = SOURCE.read_text() + + def test_only_exact_environment_value_one_enables_readback(self): + enabled = body(self.source, "static int za_diag_enabled(void)") + self.assertIn('getenv("DM_NVN_ZA_DIAG")', enabled) + self.assertIn('enabled = value && !strcmp(value, "1");', enabled) + self.assertNotIn("atoi", enabled) + self.assertNotIn("!= NULL", enabled) + + def test_every_diagnostic_entry_is_guarded_before_state_or_gpu_work(self): + for signature in ("static void za_diag_arm(", "static void za_diag_use(", + "static void za_diag_op(", "static void za_diag_pass_end(void)"): + with self.subTest(signature=signature): + self.assertTrue(body(self.source, signature).lstrip().startswith( + "if (!za_diag_enabled()) return;")) + + def test_default_path_reaches_no_readback_allocation(self): + end = body(self.source, "static void za_diag_pass_end(void)") + self.assertLess(end.index("if (!za_diag_enabled()) return;"), end.index("newBufferWithLength")) + self.assertNotIn("captured automatically", self.source) + profile = (ROOT / "games/0100F43008C44000/ios.env").read_text() + self.assertNotIn("DM_NVN_ZA_DIAG", profile) + + def test_host_tracks_inline_texture_implementation_dependencies(self): + makefile = (ROOT / "host/Makefile").read_text() + self.assertIn("../gfx/nvn/*.inc", makefile) + self.assertIn("../gfx/tex/*.inc", makefile) + + def test_ios_dispatches_to_the_legacy_renderer(self): + dispatch = DISPATCH.read_text() + self.assertIn("#if TARGET_OS_IPHONE", dispatch) + self.assertIn('#include "nvn_mtl_legacy.inc"', dispatch) + + +if __name__ == "__main__": + unittest.main() diff --git a/dm/ios/tests/test_zero_image_sources.py b/dm/ios/tests/test_zero_image_sources.py new file mode 100644 index 0000000000..b71dc55afb --- /dev/null +++ b/dm/ios/tests/test_zero_image_sources.py @@ -0,0 +1,129 @@ +#!/usr/bin/env python3 +"""Proof rejection and hash-binding tests using synthetic Metal source.""" +import copy +import hashlib +import importlib.util +import json +from pathlib import Path +import tempfile +import unittest + + +TOOL = Path(__file__).resolve().parents[1] / "tools/export_zero_image_sources.py" +SPEC = importlib.util.spec_from_file_location("zero_image", TOOL) +ZERO = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(ZERO) + +SOURCE = """#include +#include +using namespace metal; +struct _10 { +uint _m0; uint4 _m1[8]; float4 _m2; float4 _m3; int _m4; +float4 _m5[73]; int4 _m6; int _m7; +}; +struct Arguments { +constant _10* support [[id(0)]]; +texture2d_array target [[id(1)]]; +}; +fragment void clear_test(constant Arguments& args [[buffer(0)]], float4 pos [[position]]) { +float frag_x = pos.x; +float frag_y = pos.y; +float scale = (*args.support)._m5[0].x; +float scaled_x = frag_x / scale; +float scaled_y = frag_y / scale; +float rounded_x = trunc(scaled_x); +float rounded_y = trunc(scaled_y); +int coord_x = int(rounded_x); +int coord_y = int(rounded_y); +int3 pixel0 = int3(coord_x, coord_y, 0); +args.target.write(float4(0.0), uint2(pixel0.xy), uint(pixel0.z)); +int3 pixel2 = int3(coord_x, coord_y, 2); +args.target.write(float4(0.0), uint2(pixel2.xy), uint(pixel2.z)); +} +""" +META = {"key": "1111111111111111_b", "chain": "test", "stage": "ps", "entry": "clear_test", "msl_entry_kind": "fragment", + "raster": {"discard": 0, "sample_mask": 0, "colors": []}, "vertex_inputs": [], "drv_reads": {}, "drv_unfillable": [], + "bindings": [{"kind": "support", "msl_name": "support", "msl_type": "constant _10*", "arg_id": 0}, + {"kind": "img", "msl_name": "target", "msl_type": "texture2d_array", "arg_id": 1, "reg": 1}]} + + +class ZeroImageProof(unittest.TestCase): + def test_preserves_fragment_coordinates_and_atomic_word_zeroes(self): + result, proof = ZERO.lower(SOURCE, META) + self.assertEqual(proof["layers"], [0, 2]) + self.assertEqual(proof["parameter_slot"], 29) + self.assertEqual(proof["supported_pixel_bytes"], [4, 8, 16]) + self.assertIn("device atomic_uint* target [[id(1)]]", result) + self.assertIn("constant uint* dm_zero_layout [[buffer(29)]]", result) + self.assertIn("float scaled_x = frag_x / scale;", result) + self.assertEqual(result.count("dm_zero_store(args.target,"), 2) + self.assertNotIn(".write(", result) + self.assertIn("atomic_store_explicit(image + at + word, 0u, memory_order_relaxed)", result) + self.assertIn("ulong(layer) * image_words", result) + + def test_rejects_unproven_source_changes(self): + changes = [ + ("float4(0.0)", "float4(1.0)"), + ("trunc(scaled_x)", "unknown_function(scaled_x)"), + ("float frag_x = pos.x;", "float frag_x = args.target.read(uint2(0)).x;"), + ("float frag_x = pos.x;", "discard_fragment(); float frag_x = pos.x;"), + ("float frag_x = pos.x;", "atomic_store_explicit(hidden, 1u, memory_order_relaxed); float frag_x = pos.x;"), + ("float frag_x = pos.x;", "float frag_x = pos.x; frag_x += 1.0;"), + ("float frag_x = pos.x;", "if (pos.x > 0) { return; } float frag_x = pos.x;"), + ("float frag_x = pos.x;", "float frag_x = pos.x; return;"), + ("._m5[0].x", "._m5[0].y"), + ("int3(coord_x, coord_y, 2)", "int3(coord_y, coord_x, 2)"), + ("int3(coord_x, coord_y, 2)", "int3(coord_x, coord_y, -1)"), + ("[[buffer(0)]]", "[[buffer(29)]]"), + ("float frag_x = pos.x;", "float dm_zero_layout = pos.x;"), + ("int _m7;", "int _m7; device uint* hidden;"), + ("using namespace metal;", "using namespace metal;\n#define write ignore"), + ("fragment void clear_test", "float injected() { return 0; } fragment void clear_test"), + ] + for old, new in changes: + with self.subTest(change=new): + with self.assertRaises(ValueError): + ZERO.lower(SOURCE.replace(old, new, 1), META) + + def test_rejects_reflection_disagreement(self): + for path, value in [(('raster', 'discard'), 1), (('raster', 'sample_mask'), 1), + (('raster', 'colors'), [0]), (('bindings', 1, 'kind'), 'imga'), + (('bindings', 1, 'msl_type'), 'texture2d_array'), + (('bindings', 0, 'rw'), 1), (('entry',), 'other')]: + meta = copy.deepcopy(META) + cursor = meta + for item in path[:-1]: + cursor = cursor[item] + cursor[path[-1]] = value + with self.subTest(path=path): + with self.assertRaises(ValueError): + ZERO.lower(SOURCE, meta) + + def test_export_hash_binds_base_reflection_and_both_sources(self): + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + cache = root / "cache/b-test" + libraries = root / "libraries/b-test" + output = root / "output/zero-image-v1" + cache.mkdir(parents=True) + libraries.mkdir(parents=True) + key = META['key'] + original = SOURCE.encode() + reflection = json.dumps(META).encode() + base = b'synthetic base library, never modified' + (cache / (key + '.metal')).write_bytes(original) + (libraries / (key + '.json')).write_bytes(reflection) + (libraries / (key + '.metallib')).write_bytes(base) + records = ZERO.export(cache, libraries, output, 'test') + record = records[key] + for field, data in [('original_source_sha256', original), ('reflection_sha256', reflection), + ('base_library_sha256', base), ('source_sha256', (output / record['source']).read_bytes())]: + self.assertEqual(record[field], hashlib.sha256(data).hexdigest()) + self.assertEqual((libraries / (key + '.metallib')).read_bytes(), base) + self.assertEqual(json.loads((output / 'manifest.json').read_text())['kind'], 'zero-image-clear-v1') + (cache / (key + '.metal')).write_text(SOURCE.replace('float4(0.0)', 'float4(9.0)')) + self.assertEqual(ZERO.export(cache, libraries, output, 'test'), {}) + + +if __name__ == '__main__': + unittest.main() diff --git a/dm/ios/tools/build_ipa.sh b/dm/ios/tools/build_ipa.sh index e4fa8af109..37ba5f8590 100755 --- a/dm/ios/tools/build_ipa.sh +++ b/dm/ios/tools/build_ipa.sh @@ -14,6 +14,7 @@ # DM_SHOW_FPS=1 turns the frame-rate counter on (DMShowFPS in Info.plist; DM_SHOW_FPS in the app's Documents/env.txt overrides it on the phone); # DM_LANG= is the game language (DMLanguage in Info.plist, default en-US; DM_LANG in the app's Documents/env.txt overrides it on the phone); # both are also the launcher's defaults (dm/launcher.json from dm/games/settings.py; the player's choice there wins over them, Documents/env.txt over the choice); +# /ios.env supplies production iOS-only defaults when present; DM_ENV_FILE= replaces it, DM_ENV_FILE= disables it, and Documents/env.txt still has priority at runtime; # the IPA name, code modules, RomFS marker and shader tables come from the game profile set -u cd "$(dirname "$0")/.." || exit 1 @@ -67,7 +68,14 @@ else [ -n "$V" ] && ls "$MSL_IOS/b-$V" 2> /dev/null | grep -q '_b.metallib$' || IN=$(DM_GAME="$GAME" python3 "$DM/gfx/tools/dmsh_chain.py" --inputs) || die "dmsh_chain.py --inputs (game profile $GD)" grep -qxF "$(printf 'game\t%s' "$GAME")" "$MSL_IOS/b-$V/inputs.tsv" 2> /dev/null && grep -qxF "$(printf 'inputs\t%s' "$IN")" "$MSL_IOS/b-$V/inputs.tsv" || die "the iOS shader libraries $MSL_IOS/b-$V are not a full build from this profile's current tables (inputs $IN; their stamp: $([ -f "$MSL_IOS/b-$V/inputs.tsv" ] && grep -v '^#' "$MSL_IOS/b-$V/inputs.tsv" | tr '\n\t' ' ' || echo none)): build them again (IOSOUT=$MSL_IOS dmsh_build.py ...)" - head -1 "$GD/c1_full.tsv" 2> /dev/null | grep -q "^# chain b-$V inputs $IN " || die "$GD/c1_full.tsv is missing or not made from these libraries (chain $V, inputs $IN): a full dmsh_build.py run makes it and cb_extent.tsv"; fi + head -1 "$GD/c1_full.tsv" 2> /dev/null | grep -q "^# chain b-$V inputs $IN " || die "$GD/c1_full.tsv is missing or not made from these libraries (chain $V, inputs $IN): a full dmsh_build.py run makes it and cb_extent.tsv" + COMPANION_REQUIREMENTS=$(python3 "$DM/games/game.py" "$GAME" ios_companions 2> /dev/null || :) + if [ -n "$COMPANION_REQUIREMENTS" ]; then + [ -f "$GD/$COMPANION_REQUIREMENTS" ] || die "profile companion requirements are missing: $GD/$COMPANION_REQUIREMENTS" + python3 tools/validate_shader_companions.py "$GD/$COMPANION_REQUIREMENTS" "$MSL_IOS/b-$V" "$V" || + die "the profile's required iOS shader companions are incomplete in $MSL_IOS/b-$V" + fi +fi t "app: $DM_DISPLAY_NAME $DM_APP_VERSION ($DM_BUNDLE_ID), language $DM_LANG$([ $DM_SHOW_FPS = YES ] && echo ', frame-rate counter on')" t "generate Gen/ (host sources, embedded modules, tables)" @@ -101,8 +109,11 @@ L=$(GFXAPI="$GFXAPI" python3 "$DM/games/settings.py" "$GAME" "$PATCHED" "$VNACP" # The packed language (DMLanguage) is the Game language row's default, which settings.py makes the game's first language when it lacks DM_LANG: the screen and DM_LAUNCHER=0 then agree LD=$(python3 -c 'import json, sys; print(next((s["default"] for s in json.load(open(sys.argv[1]))["settings"] if s["env"] == "DM_LANG"), ""))' Gen/dm/launcher.json) || die "launcher settings (Gen/dm/launcher.json)" if [ -n "$LD" ] && [ "$LD" != "$DM_LANG" ]; then t "game language: the game has no $DM_LANG; packing $LD, its first"; export DM_LANG=$LD; fi -# Diagnostic build: DM_ENV_FILE=file → dm/env.txt in the bundle (main.m sets one environment variable per line at boot) -if [ -n "${DM_ENV_FILE:-}" ]; then cp "$DM_ENV_FILE" Gen/dm/env.txt || die "copy DM_ENV_FILE"; t "env: $(grep -v '^#' "$DM_ENV_FILE" | tr '\n' ' ')"; fi +# Bundle environment: an explicit DM_ENV_FILE (including an empty value) takes +# precedence over the profile's optional ios.env. Documents/env.txt is read +# first at runtime and therefore still overrides either bundled source. +ENV_SOURCE=$(python3 tools/stage_profile_env.py "$GD" Gen/dm/env.txt) || die "stage iOS environment" +if [ -n "$ENV_SOURCE" ]; then t "env ($ENV_SOURCE): $(grep -v '^#' "$ENV_SOURCE" | tr '\n' ' ')"; fi # The shader libraries go into the archive as DmIOS.app/msl/ (main.m sets DM_MSL_DIR to it) if [ "$GFXAPI" = vulkan ]; then MSLDIR=vk; elif [ "$GFXAPI" = gl ]; then MSLDIR=gl; else MSLDIR=b-$V; fi # Shared-font stand-ins: standard = macOS's Arial Unicode (absent on iOS), Nintendo extension = button icons generated by make_ext_font.py (needs fontTools; FONT_PY names a python that has it) diff --git a/dm/ios/tools/export_guarded_index_sources.py b/dm/ios/tools/export_guarded_index_sources.py new file mode 100644 index 0000000000..b679c4078f --- /dev/null +++ b/dm/ios/tools/export_guarded_index_sources.py @@ -0,0 +1,311 @@ +#!/usr/bin/env python3 +"""Export proven dead-index seed companions from verified non-point sources. + +The deliberately narrow proof recognizes a complete SPIRV-Cross coordinate-to- +constant-buffer index chain, independent of shader keys and temporary names. +When !guard is true both seeds are overwritten before use; otherwise every +arithmetic operation and memory read depending on them is disabled. All uses of +the seeds and intermediate indices must be inside that closed chain. Unknown +patterns produce no companion. This is not a general MSL optimizer. + +Game-derived inputs/outputs stay outside source control. Original sources, +libraries, reflection and the translation chain are never changed. Companions +are consumed only by the separately verified legacy non-point runtime path. +""" +import argparse +import bisect +import hashlib +import json +from pathlib import Path +import re +import tempfile + + +PROOF = "dead-guarded-index-seeds-v1" +KIND = "guarded-index-v1" +IDENT = r"[A-Za-z_]\w*" +TOKEN = re.compile(r"[A-Za-z_]\w*|0[xX][0-9a-fA-F]+|\d+|<<|>>|!=|==|&&|\|\||[^\s]") + +# Every branch uses the SAME boolean. The two loads are the only sinks of the +# index chain. Capture names, not numeric SPIR-V IDs or per-game constants. +CHAIN = r""" +int $mask = as_type((*$args.$binding).$array[#flag_index].$component) & #bit; +bool $guard = $mask != 0; +COPY_GUARD +if (!$guard) { + float $trunc_x = trunc($coord_x); + int $int_x = int($trunc_x); + $seed_x = $int_x; +} +int $x = $seed_x; +int $sum = $x; +if (!$guard) { + float $trunc_y = trunc($coord_y); + int $int_y = int($trunc_y); + $seed_y = $int_y; +} +int $y = $seed_y; +if (!$guard) { + int $twice_y = $y << #row_shift; + int $joined = $twice_y + $x; + $sum = $joined; +} +int $index = $sum; +int $scaled = $index; +if (!$guard) { + int $shifted = $index << #scale_shift; + $scaled = $shifted; +} +int $offset = $scaled; +if (!$guard) { + int $address = $offset + #base; + uint $word = uint(int(uint($address) >> uint(#word_shift))); + uint $vector = uint(int(uint(int($word)) >> uint(#vector_shift))); + int $channel = int($word) & #channel_mask; + float $value_x = (*$args.$binding).$array[int($vector)][$channel]; + int $next_word = int($word) + #next; + uint $next_vector = uint(int(uint($next_word) >> uint(#next_vector_shift))); + int $next_channel = $next_word & #next_channel_mask; + float $value_y = (*$args.$binding).$array[int($next_vector)][$next_channel]; + $output_x = $value_x; + $output_y = $value_y; +} +""" +DEPENDENT = { + "seed_x", "seed_y", "x", "sum", "y", "twice_y", "joined", "index", + "scaled", "shifted", "offset", "address", "word", "vector", "channel", + "next_word", "next_vector", "next_channel", +} +LOCALS = DEPENDENT | { + "mask", "guard", "trunc_x", "int_x", "trunc_y", "int_y", "value_x", "value_y", +} + + +def digest(data): + return hashlib.sha256(data).hexdigest() + + +def write_output(path, data): + # Replacing a directory entry preserves original inputs even if a previous + # output file was accidentally hard-linked or symlinked to an input. + with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as tmp: + temporary = Path(tmp.name) + try: + tmp.write(data) + tmp.close() + temporary.replace(path) + finally: + temporary.unlink(missing_ok=True) + + +def masked(source): + # Preserve offsets while removing comments. Macro expansion would invalidate + # a textual use-def proof; only ordinary includes are accepted. + clean = re.sub(r"//[^\n]*|/\*.*?\*/", lambda m: re.sub(r"[^\n]", " ", m[0]), source, flags=re.S) + directives = re.findall(r"^\s*#[^\n]*", clean, re.M) + if any(not re.fullmatch(r"\s*#\s*include\s*<(?:metal_stdlib|simd/simd\.h)>\s*", d) for d in directives): + raise ValueError("Only standard Metal includes are supported") + return clean + + +def functions(source): + clean = masked(source) + for match in re.finditer(r"\bvertex\s+" + IDENT + r"\s+(" + IDENT + r")\s*\(", clean): + opening, depth = match.end() - 1, 1 + at = opening + 1 + while at < len(clean) and depth: + depth += (clean[at] == "(") - (clean[at] == ")") + at += 1 + while at < len(clean) and clean[at].isspace(): + at += 1 + if depth or at == len(clean) or clean[at] != "{": + raise ValueError("Unsupported vertex function signature") + opening, depth = at, 1 + at += 1 + while at < len(clean) and depth: + depth += (clean[at] == "{") - (clean[at] == "}") + at += 1 + if depth: + raise ValueError("Unbalanced vertex function") + yield match[1], opening + 1, at - 1, clean[opening + 1:at - 1] + + +def pattern(copy_guard): + template = CHAIN.replace("COPY_GUARD", "$guard_copy = $guard;" if copy_guard else "") + seen, parts = set(), [] + for token in re.findall(r"\$\w+|#\w+|" + TOKEN.pattern, template): + if token[0] in "$#": + name = token[1:] + if name in seen: + parts.append("(?P=" + name + ")") + else: + seen.add(name) + parts.append("(?P<" + name + ">" + (IDENT if token[0] == "$" else r"(?:0|[1-9]\d*)") + ")") + else: + parts.append(re.escape(token)) + return re.compile(r"(? \( (" + IDENT + r") \)) ;") + + +def prove_body(body): + """Return byte-position edits only for completely closed seed-use chains.""" + tokens = list(TOKEN.finditer(body)) + canonical = " ".join(t[0] for t in tokens) + starts, at = [], 0 + scopes, stack = [], [] + for i, token in enumerate(tokens): + starts.append(at) + at += len(token[0]) + 1 + if token[0] == "}": + if not stack: + raise ValueError("Unbalanced body scope") + stack.pop() + scopes.append(tuple(stack)) + if token[0] == "{": + stack.append(i) + if stack or re.search(r"[\"']", body): + raise ValueError("Unsupported body syntax") + + def token_range(start, end): + return bisect.bisect_left(starts, start), bisect.bisect_right(starts, end - 1) + + seeds = {} + for seed in SEED.finditer(canonical): + seeds.setdefault(seed[1], []).append(seed) + edits, proven_seeds = {}, set() + for regex in PATTERNS: + for match in regex.finditer(canonical): + names = match.groupdict() + # Reject aliasing/shadowed roles. A different spelling or extra use + # needs its own proof, even if it happens to work for one draw. + locals_ = {names[k] for k in LOCALS} + if len(locals_) != len(LOCALS): + continue + if locals_ & {names[k] for k in ("args", "coord_x", "coord_y", "output_x", "output_y")}: + continue + if names.get("guard_copy") in locals_: + continue + seed_matches = [seeds.get(names[k], []) for k in ("seed_x", "seed_y")] + if any(len(s) != 1 or s[0].end() > match.start() for s in seed_matches): + continue + seed_matches = [s[0] for s in seed_matches] + region = token_range(*match.span()) + allowed = [region] + [token_range(*s.span()) for s in seed_matches] + # Do not drop observable volatile reads/writes or match the suffix + # of a qualified declaration. Seed operands must be plain local + # float values, as emitted by this translation pattern. + if any(a and tokens[a - 1][0] not in {";", "{", "}"} for a, _ in allowed): + continue + operands = [s[3] for s in seed_matches] + plain_float = re.compile(r"(?:^|(?<=[;{}]))\s*float (" + IDENT + r") = [^;{}]+ ;") + float_names = plain_float.findall(canonical) + if any(float_names.count(name) != 1 for name in operands): + continue + # Both definitions must dominate the chain's lexical scope. All + # reads/writes of every derived index must be inside these spans. + if any(scopes[region[0]][:len(scopes[a])] != scopes[a] for a, _ in allowed[1:]): + continue + dependent = {names[k] for k in DEPENDENT} + if any(s[3] in dependent for s in seed_matches): + continue + if any(t[0] in dependent and not any(a <= i < b for a, b in allowed) + for i, t in enumerate(tokens)): + continue + # Local variables participating in the proof may not be redeclared + # in a nested scope: identifier-only closure is then ambiguous. + declarations = re.findall(r"\b(?:int|uint|float|bool) (" + IDENT + r")\b", canonical) + if any(declarations.count(name) != 1 for name in locals_): + continue + for seed in seed_matches: + first, last = token_range(*seed.span(2)) + edits[(tokens[first].start(), tokens[last - 1].end())] = "0" + proven_seeds.add(seed[1]) + if not edits: + raise ValueError("No closed dead guarded-index seed chain") + if proven_seeds != set(seeds): + raise ValueError("A scalar integer bitcast seed lacks a closed proof") + return [(start, end, value) for (start, end), value in sorted(edits.items())] + + +def transform(source, meta, entries=None): + """Default to entry_z; preserve point/original and capture/TF functions. + + Explicit entry selection is available for offline proof tests. Each chosen + function needs its own complete proof; proofs never cross function scopes. + """ + reflected = {meta.get("entry"), meta.get("entry_z")} - {None} + entries = set(entries) if entries is not None else {meta.get("entry_z") or meta.get("entry")} + if meta.get("stage") != "vs" or not entries or not entries <= reflected: + raise ValueError("Expected reflected vertex entries") + if not re.search(r"\btrunc\b", source): + raise ValueError("No guarded coordinate conversion") + found, edits = set(), [] + for name, start, end, body in functions(source): + if name not in entries: + continue + if name in found: + raise ValueError("Duplicate vertex entry") + found.add(name) + # A partial proof in one entry is not borrowed by another entry. + edits.extend((start + a, start + b, value) for a, b, value in prove_body(body)) + if found != entries: + raise ValueError("Missing reflected vertex entry") + for start, end, value in reversed(sorted(edits)): + source = source[:start] + value + source[end:] + return source, len(edits) + + +def export(libraries: Path, output: Path, chain: str) -> int: + if libraries.name != "b-" + chain: + raise ValueError("Library directory must match the host chain") + base = libraries / "nonpoint-v1" + if output.resolve() in {libraries.resolve(), base.resolve()} or base.resolve() in output.resolve().parents: + raise ValueError("Output must not overwrite the original non-point sources") + manifest = json.loads((base / "manifest.json").read_bytes()) + if manifest.get("schema") != 1 or manifest.get("chain") != chain or not isinstance(manifest.get("shaders"), dict): + raise ValueError("Invalid non-point source manifest") + records, outputs = {}, {} + # Validate every base identity before writing anything, including records + # whose source pattern will ultimately be unsupported. + for key, record in sorted(manifest["shaders"].items()): + if not re.fullmatch(r"[A-Za-z0-9_]+", key): + raise ValueError("Invalid shader key") + original = (base / (key + ".metal")).read_bytes() + library = (libraries / (key + ".metallib")).read_bytes() + reflection = (libraries / (key + ".json")).read_bytes() + meta = json.loads(reflection) + if (record.get("source_sha256") != digest(original) or record.get("library_sha256") != digest(library) + or meta.get("chain") != chain or meta.get("key") != key or meta.get("stage") != "vs"): + raise ValueError("Non-point source, library or reflection identity mismatch: " + key) + try: + transformed, count = transform(original.decode("utf-8"), meta) + except ValueError: + continue + data = transformed.encode("utf-8") + name = key + ".metal" + outputs[name] = data + records[key] = {"source": name, "source_sha256": digest(data), "original_source_sha256": digest(original), + "base_library_sha256": digest(library), "reflection_sha256": digest(reflection), + "proof": PROOF, "replacement_count": count} + output.mkdir(parents=True, exist_ok=True) + for name, data in outputs.items(): + write_output(output / name, data) + write_output(output / "manifest.json", (json.dumps({"schema": 1, "chain": chain, "kind": KIND, + "shaders": records}, separators=(",", ":")) + "\n").encode()) + return len(records) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("libraries", type=Path, help="Chain-matched b-CHAIN containing verified nonpoint-v1") + parser.add_argument("output", type=Path, help="Independent guarded-index-v1 companion directory") + args = parser.parse_args() + header = Path(__file__).resolve().parents[2] / "gfx/nvn/dmsh_chain.h" + match = re.search(r'#define\s+DMSH_CHAIN\s+"([a-f0-9]+)"', header.read_text()) + if match is None: + raise ValueError(f"No DMSH_CHAIN in {header}") + chain = match.group(1) + print(f"Exported {export(args.libraries, args.output, chain)} proven guarded-index companions (chain {chain})") diff --git a/dm/ios/tools/export_layered_sources.py b/dm/ios/tools/export_layered_sources.py new file mode 100644 index 0000000000..47c5744e73 --- /dev/null +++ b/dm/ios/tools/export_layered_sources.py @@ -0,0 +1,160 @@ +#!/usr/bin/env python3 +"""Export conservatively proven per-slice shaders beside chain-matched libraries. + +Game-derived sources and outputs stay outside source control. The base shader +chain and libraries are unchanged. Unknown layer expressions remain unsupported. +""" +import argparse +import hashlib +import json +from pathlib import Path +import re + +SLOT = 30 +READ_ONLY = {"cbuf", "c1", "drvcb", "support", "tex", "tbuf", "samp", "sampt", "sampu", "texu", "samp_tex", "samp_ubo"} + + +def digest(data): + return hashlib.sha256(data).hexdigest() + + +def functions(source): + result = [] + for match in re.finditer(r"\b(vertex|fragment|kernel)\s+\w+\s+(\w+)\s*\(", source): + at = match.end() - 1 + depth = 1 + end = at + 1 + while depth and end < len(source): + depth += (source[end] == "(") - (source[end] == ")") + end += 1 + opening = source.find("{", end) + if opening < 0: + raise ValueError("Missing function body") + depth = 1 + close = opening + 1 + while depth and close < len(source): + depth += (source[close] == "{") - (source[close] == "}") + close += 1 + if depth: + raise ValueError("Unbalanced function body") + result.append((match.group(1), match.group(2), match.start(), end - 1, opening, close)) + return result + + +def pure(meta): + return all(binding.get("kind") in READ_ONLY and not binding.get("rw") and + "access::write" not in binding.get("msl_type", "") and + "access::read_write" not in binding.get("msl_type", "") + for binding in meta.get("bindings", [])) + + +def prove_layer(body): + if re.search(r"\b(if|for|while|switch|atomic|threadgroup)\b", body): + raise ValueError("Layer proof requires a straight-line, side-effect-free vertex entry") + layers = re.findall(r"out\.gl_Layer\s*=\s*([^;]+);", body) + if len(layers) != 1 or len(re.findall(r"return\s+out\s*;", body)) != 1: + raise ValueError("Expected one layer assignment and one return") + definitions = {} + for name, expression in re.findall(r"\b(_\d+)\s*=\s*([^;]+);", body): + if name in definitions: + raise ValueError("Mutable layer temporary") + definitions[name] = expression + checked = set() + + def visit(expression): + if "gl_VertexIndex" in expression or "gl_VertexID" in expression or "out." in expression: + raise ValueError("Layer varies with vertex ID") + for name in re.findall(r"\b[A-Za-z_]\w*\b", expression): + if name in {"uint", "int", "float", "as_type", "int32_t", "uint32_t", "gl_InstanceIndex", "gl_BaseInstance", "x", "y", "z", "w"}: + continue + if re.fullmatch(r"_\d+|_m\d+|spvDescriptorSet\d+|vp_c\d+", name): + continue + raise ValueError("Unproven layer dependency " + name) + for name in re.findall(r"\b_\d+\b", expression): + if name in checked: + continue + if name not in definitions: + raise ValueError("Unresolved layer dependency") + checked.add(name) + visit(definitions[name]) + # SPIRV-Cross arithmetic and uniform reads are the only accepted calls. + calls = set(re.findall(r"\b([A-Za-z_]\w*)\s*(?:<[^>]+>)?\s*\(", expression)) + if calls - {"as_type", "uint", "int", "float", "int32_t", "uint32_t"}: + raise ValueError("Unproven function in layer expression") + visit(layers[0]) + + +def lower(source, meta): + if not pure(meta) or re.search(r"\[\[\s*buffer\s*\(\s*30\s*\)\s*\]\]", source): + raise ValueError("Writable resources or occupied compatibility buffer slot") + stage = meta.get("stage") + if stage == "vs": + if meta.get("vertex_inputs") or "point_size" in source: + raise ValueError("Vertex inputs or point-size output require a separate proof") + entries = {meta.get("entry"), meta.get("entry_z")} - {None} + found = set() + for kind, name, start, signature_end, opening, end in reversed(functions(source)): + if kind != "vertex" or name not in entries: + source = source[:start] + source[end:] + continue + body = source[opening:end] + prove_layer(body) + found.add(name) + body = body.replace("out.gl_Layer", "dm_slice_layer") + body = body.replace("{", "{\n uint dm_slice_layer = 0;", 1) + body = re.sub(r"return\s+out\s*;", "if (dm_slice_layer != dm_selected_slice) out.gl_Position = float4(2.0, 2.0, 2.0, 1.0);\n return out;", body) + signature = source[start:signature_end] + ", constant uint& dm_selected_slice [[buffer(30)]])" + source = source[:start] + signature + "\n" + body + source[end:] + if found != entries: + raise ValueError("Missing vertex entry") + source, count = re.subn(r"\s*uint\s+gl_Layer\s*\[\[render_target_array_index\]\]\s*;", "", source) + if count != 1: + raise ValueError("Unknown vertex layer output structure") + elif stage == "ps": + source, count = re.subn(r"\buint\s+gl_Layer\s*\[\[render_target_array_index\]\]", "constant uint& gl_Layer [[buffer(30)]]", source) + if not count: + raise ValueError("Unknown fragment layer input") + else: + raise ValueError("Unsupported shader stage") + if "render_target_array_index" in source: + raise ValueError("Unlowered layer builtin") + return source + + +def export(cache, libraries, output, chain): + if cache.name != "b-" + chain or libraries.name != "b-" + chain: + raise ValueError("Source and library directories must match the host chain") + output.mkdir(parents=True, exist_ok=True) + records = {} + for path in sorted(libraries.glob("*_b.json")): + original = (cache / (path.stem + ".metal")).read_bytes() + if b"render_target_array_index" not in original: + continue + meta = json.loads(path.read_text()) + if meta.get("key") != path.stem or meta.get("chain") != chain: + raise ValueError("Mismatched shader reflection " + path.name) + record = {"stage": meta.get("stage"), "library_sha256": digest((libraries / (path.stem + ".metallib")).read_bytes()), + "reflection_sha256": digest(path.read_bytes()), "original_source_sha256": digest(original)} + try: + data = lower(original.decode(), meta).encode() + (output / (path.stem + ".metal")).write_bytes(data) + record.update(supported=True, proof="instance-layer-pure-v1", source_sha256=digest(data), slot=SLOT) + except ValueError as error: + record.update(supported=False, reason=str(error)) + records[path.stem] = record + (output / "manifest.json").write_text(json.dumps({"schema": 1, "chain": chain, "kind": "layered-slices-v1", "shaders": records}, separators=(",", ":")) + "\n") + return records + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + for name in ("cache", "libraries", "output"): + parser.add_argument(name, type=Path) + args = parser.parse_args() + header = Path(__file__).resolve().parents[2] / "gfx/nvn/dmsh_chain.h" + match = re.search(r'#define\s+DMSH_CHAIN\s+"([a-f0-9]+)"', header.read_text()) + if match is None: + raise ValueError(f"No DMSH_CHAIN in {header}") + chain = match.group(1) + records = export(args.cache, args.libraries, args.output, chain) + print(f"Layered sources: {sum(r['supported'] for r in records.values())} proven / {len(records)} total (chain {chain})") diff --git a/dm/ios/tools/export_legacy_atomics.py b/dm/ios/tools/export_legacy_atomics.py new file mode 100644 index 0000000000..98b43ea22f --- /dev/null +++ b/dm/ios/tools/export_legacy_atomics.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python3 +"""Export bounds-checked Metal 3 image-atomic companions from original MSL. + +Base chain libraries remain untouched. Output contains game-derived source and +must live with the user's game inputs, outside source control. +""" +import argparse +import hashlib +import json +from pathlib import Path +import re + + +HELPERS = r""" +// Legacy linear companions must preserve image bounds and array layers. +template +inline bool dm_image_atomic_valid(int2 c, texture2d t) { + return all(c >= int2(0)) && uint(c.x) < t.get_width() && uint(c.y) < t.get_height(); +} +template +inline bool dm_image_atomic_valid(int3 c, texture2d_array t) { + return all(c >= int3(0)) && uint(c.x) < t.get_width() && uint(c.y) < t.get_height() && uint(c.z) < t.get_array_size(); +} +template +inline ulong dm_image_atomic_index(int2 c, texture2d t) { + ulong a = spvLinearTextureAlignment / 4; + ulong pitch = (ulong(t.get_width()) + a - 1) & ~(a - 1); + return ulong(uint(c.y)) * pitch + uint(c.x); +} +template +inline ulong dm_image_atomic_index(int3 c, texture2d_array t) { + ulong a = spvLinearTextureAlignment / 4; + ulong pitch = (ulong(t.get_width()) + a - 1) & ~(a - 1); + return (ulong(uint(c.z)) * t.get_height() + uint(c.y)) * pitch + uint(c.x); +} +#define spvImage2DAtomicCoord(tc, tex) dm_image_atomic_index(tc, tex) +""" + + +def close_paren(source, start): + depth = 0 + for i in range(start, len(source)): + if source[i] == "(": + depth += 1 + elif source[i] == ")": + depth -= 1 + if not depth: + return i + raise ValueError("Unbalanced generated atomic call") + + +def split_args(source): + depth, start, args = 0, 0, [] + for i, c in enumerate(source): + if c in "([": + depth += 1 + elif c in ")]": + depth -= 1 + elif c == "," and not depth: + args.append(source[start:i].strip()) + start = i + 1 + args.append(source[start:].strip()) + return args + + +def transform(source): + macro = re.compile(r"^#define spvImage2DAtomicCoord[^\n]*$", re.M) + if len(macro.findall(source)) != 1: + raise ValueError("Expected one original image-atomic coordinate macro") + edits = [] + for call in re.finditer(r"\batomic_(?:fetch_\w+|exchange)_explicit\s*\(", source): + end = close_paren(source, call.end() - 1) + body = source[call.start():end + 1] + coord = re.search(r"\bspvImage2DAtomicCoord\s*\(", body) + if not coord: + continue + coord_end = close_paren(body, coord.end() - 1) + args = split_args(body[coord.end():coord_end]) + if len(args) != 2: + raise ValueError("Unexpected image-atomic coordinate arguments") + guard = f"dm_image_atomic_valid({args[0]}, {args[1]})" + edits.append((call.start(), end + 1, f"({guard} ? ({body}) : 0)")) + expected = len(re.findall(r"\bspvImage2DAtomicCoord\s*\(", source)) - 1 + if not edits or len(edits) != expected: + raise ValueError("An image-atomic coordinate use could not be guarded") + for start, end, replacement in reversed(edits): + source = source[:start] + replacement + source[end:] + return macro.sub(lambda _: HELPERS.strip(), source), len(edits) + + +def export(cache, libraries, output, chain): + if cache.name != f"b-{chain}" or libraries.name != f"b-{chain}": + raise ValueError("Source and library directories must match the host chain") + output.mkdir(parents=True, exist_ok=True) + count = 0 + for reflection in sorted(libraries.glob("*_b.json")): + meta = json.loads(reflection.read_text()) + if not any("_atomic" in b.get("msl_name", "") for b in meta.get("bindings", [])): + continue + if meta.get("chain") != chain or meta.get("key") != reflection.stem: + raise ValueError(f"Mismatched reflection: {reflection.name}") + original = (cache / (reflection.stem + ".metal")).read_bytes() + if b"spvImage2DAtomicCoord" not in original: + continue + variant, calls = transform(original.decode()) + data = variant.encode() + (output / (reflection.stem + ".metal")).write_bytes(data) + record = {"schema": 1, "chain": chain, "kind": "bounded-image-atomics-v1", "calls": calls, + "original_source_sha256": hashlib.sha256(original).hexdigest(), + "source_sha256": hashlib.sha256(data).hexdigest(), + "library_sha256": hashlib.sha256((libraries / (reflection.stem + ".metallib")).read_bytes()).hexdigest()} + (output / reflection.name).write_text(json.dumps(record) + "\n") + count += 1 + return count + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + for name in ("cache", "libraries", "output"): + parser.add_argument(name, type=Path) + args = parser.parse_args() + header = Path(__file__).resolve().parents[2] / "gfx/nvn/dmsh_chain.h" + match = re.search(r'#define\s+DMSH_CHAIN\s+"([a-f0-9]+)"', header.read_text()) + if match is None: + raise ValueError(f"No DMSH_CHAIN in {header}") + chain = match.group(1) + print(f"Exported {export(args.cache, args.libraries, args.output, chain)} bounded atomic variants (chain {chain})") diff --git a/dm/ios/tools/export_min_reduction.py b/dm/ios/tools/export_min_reduction.py new file mode 100644 index 0000000000..c203cf7f52 --- /dev/null +++ b/dm/ios/tools/export_min_reduction.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python3 +"""Export hash-gated MIN-reduction companions without changing base shaders. + +The two audited shaders each have one ordinary, integer-explicit-LOD sample of +slot 17. Runtime must additionally validate the recorded sampler/texture contract +before selecting a companion. Inputs and exported game-derived MSL stay outside +the repository. No compiled library, original source or reflection is replaced. +""" +import argparse +import hashlib +import json +from pathlib import Path +import re + +ROOT = Path(__file__).resolve().parents[3] +CHAIN = "43806fc88b5e" +KIND = "linear-min-reduction-v1" +AUDITED = { + "151607c7a92386cc_b": { + "stage": "ps", "prefix": "fp", + "original_source_sha256": "7c6e776f1cccd8ecf7310e5083664697e35de1ab85a9f21466cd307426de297f", + }, + "ec5ebd6d9902a9a1_b": { + "stage": "cs", "prefix": "cp", + "original_source_sha256": "bcd3d2c291a896ee1a22a828b7a461cc6501a33e835465df24d5c3cb71610435", + }, +} +SAMPLER = { + "reduction": 1, "minf": 3, "magf": 1, + "ws": 7, "wt": 7, "wr": 7, + "lod_min": 0, "lod_max": 1000, "lod_bias": 0, "aniso": 1, + "cmp_mode": 0, +} +HELPER_BEGIN = "// DM_MIN_REDUCTION_HELPER_BEGIN\n" +HELPER_END = "// DM_MIN_REDUCTION_HELPER_END\n" + + +def digest(data): + return hashlib.sha256(data).hexdigest() + + +def helper_source(): + directory = ROOT / "dm/gfx/nvn" + parts = [] + for name in ("nvn_reduction_footprint.h", "nvn_reduction_sample.metal.h"): + lines = (directory / name).read_text().splitlines() + lines = [line for line in lines if line != "#pragma once" and line != '#include "nvn_reduction_footprint.h"'] + parts.append("\n".join(lines).rstrip()) + return "\n\n".join(parts) + "\n" + + +def close_paren(source, start): + depth = 0 + for index in range(start, len(source)): + if source[index] == "(": + depth += 1 + elif source[index] == ")": + depth -= 1 + if not depth: + return index + raise ValueError("Unbalanced generated sample call") + + +def split_args(source): + depth, start, args = 0, 0, [] + for index, char in enumerate(source): + if char in "([{": + depth += 1 + elif char in ")]}": + depth -= 1 + elif char == "," and not depth: + args.append(source[start:index].strip()) + start = index + 1 + args.append(source[start:].strip()) + return args + + +def transform(source, key): + """Replace exactly one audited binding's sample; preserve the entry and ABI. + + Export enforces the original SHA before calling this structural transform. + Keeping this function independent permits entirely synthetic unit fixtures. + """ + prefix = AUDITED[key]["prefix"] + texture = f"spvDescriptorSet0.{prefix}_t_tcb_2A" + sampler = texture + "Smplr" + call = re.compile(re.escape(texture) + r"\.sample\s*\(") + matches = list(call.finditer(source)) + if len(matches) != 1 or HELPER_BEGIN in source: + raise ValueError("Expected exactly one unmodified slot-17 ordinary sample") + match = matches[0] + end = close_paren(source, match.end() - 1) + args = split_args(source[match.end():end]) + if len(args) != 3 or args[0] != sampler: + raise ValueError("Unexpected generated sampler/coordinate/LOD arguments") + lod = re.fullmatch(r"level\((.*)\)", args[2], re.S) + if not lod or not lod.group(1).strip(): + raise ValueError("Expected one explicit level() argument without offsets") + replacement = f"dm_reduction_sample_lmn_edge({texture}, {args[1]}, {lod.group(1)}, 0.0f, 1000.0f, false)" + variant = source[:match.start()] + replacement + source[end + 1:] + marker = "using namespace metal;" + if variant.count(marker) != 1: + raise ValueError("Expected one generated Metal namespace declaration") + position = variant.index(marker) + len(marker) + payload = helper_source() + injected = "\n" + HELPER_BEGIN + payload + HELPER_END + return variant[:position] + injected + variant[position:], payload + + +def validate_reflection(meta, key, chain): + profile = AUDITED[key] + if meta.get("chain") != chain or meta.get("key") != key or meta.get("stage") != profile["stage"] or meta.get("entry") != "main0": + raise ValueError(f"Mismatched reflection: {key}") + bindings = meta.get("bindings", []) + prefix = profile["prefix"] + for kind, name, msl_type in (("tex", f"{prefix}_t_tcb_2A", "texture2d"), + ("samp_tex", f"{prefix}_t_tcb_2ASmplr", "sampler")): + found = [row for row in bindings if row.get("kind") == kind and row.get("reg") == 17] + if len(found) != 1 or found[0].get("msl_name") != name or found[0].get("msl_type") != msl_type: + raise ValueError(f"Mismatched slot-17 reflection: {key} {kind}") + + +def export(cache, libraries, output, chain): + cache, libraries, output = Path(cache).resolve(), Path(libraries).resolve(), Path(output).resolve() + if chain != CHAIN or cache.name != f"b-{chain}" or libraries.name != f"b-{chain}": + raise ValueError("Source/library directories and chain must match the audited shader chain") + for path in (cache, libraries, output): + if path == ROOT or ROOT in path.parents: + raise ValueError("Game-derived inputs and exported sources must stay outside the repository") + if any(output == original or output in original.parents or original in output.parents for original in (cache, libraries)): + raise ValueError("Companion output must not overlap an original source/library directory") + pending = {} + for key, profile in AUDITED.items(): + meta = json.loads((libraries / (key + ".json")).read_text()) + validate_reflection(meta, key, chain) + original = (cache / (key + ".metal")).read_bytes() + if digest(original) != profile["original_source_sha256"]: + raise ValueError(f"Original source SHA256 differs from the disassembled/audited shader: {key}") + library = (libraries / (key + ".metallib")).read_bytes() + variant, payload = transform(original.decode("utf-8"), key) + data = variant.encode("utf-8") + record = { + "schema": 1, "chain": chain, "kind": KIND, "key": key, + "entry": "main0", "stage": profile["stage"], "reg": 17, "calls": 1, + "texture_dimension": "2d", "integer_explicit_lod": True, + "helper_file": "embedded", "helper_sha256": digest(payload.encode("utf-8")), + "original_source_sha256": digest(original), "source_sha256": digest(data), + "library_sha256": digest(library), "sampler": dict(SAMPLER), + } + pending[key + ".metal"] = data + pending[key + ".json"] = (json.dumps(record, sort_keys=True, separators=(",", ":")) + "\n").encode() + # Validate the entire pair before writing anything. Rerunning is idempotent; + # differing companions are refused rather than overwriting a checkpoint. + for name, data in pending.items(): + target = output / name + if target.exists() and target.read_bytes() != data: + raise ValueError(f"Existing companion differs; use a fresh output directory: {target.name}") + output.mkdir(parents=True, exist_ok=True) + for name, data in pending.items(): + (output / name).write_bytes(data) + return len(AUDITED) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + for name in ("cache", "libraries", "output"): + parser.add_argument(name, type=Path) + args = parser.parse_args() + header = ROOT / "dm/gfx/nvn/dmsh_chain.h" + match = re.search(r'#define\s+DMSH_CHAIN\s+"([a-f0-9]+)"', header.read_text()) + if match is None: + raise ValueError(f"No DMSH_CHAIN in {header}") + chain = match.group(1) + print(f"Exported {export(args.cache, args.libraries, args.output, chain)} audited MIN companions (chain {chain})") diff --git a/dm/ios/tools/export_nonpoint_sources.py b/dm/ios/tools/export_nonpoint_sources.py new file mode 100644 index 0000000000..f95c631cc5 --- /dev/null +++ b/dm/ios/tools/export_nonpoint_sources.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +"""Export unmodified, chain-matched vertex sources for legacy Metal variants. + +Inputs and outputs contain game-derived data and must stay outside source control. +The runtime checks both source and original library hashes before compiling a +non-point variant. This export does not change the base translation chain. +""" +import argparse +import hashlib +import json +from pathlib import Path +import re +import shutil + + +def export(cache: Path, libraries: Path, output: Path, chain: str) -> int: + if cache.name != f"b-{chain}" or libraries.name != f"b-{chain}": + raise ValueError("Source and library directories must match the host chain") + records = {} + output.mkdir(parents=True, exist_ok=True) + for reflection in sorted(libraries.glob("*_b.json")): + meta = json.loads(reflection.read_text()) + if meta.get("stage") != "vs": + continue + if meta.get("chain") != chain or meta.get("key") != reflection.stem: + raise ValueError(f"Mismatched reflection: {reflection.name}") + source = cache / (reflection.stem + ".metal") + data = source.read_bytes() + if b"[[point_size]]" not in data: + continue + library = libraries / (reflection.stem + ".metallib") + records[reflection.stem] = { + "source_sha256": hashlib.sha256(data).hexdigest(), + "library_sha256": hashlib.sha256(library.read_bytes()).hexdigest(), + } + shutil.copyfile(source, output / source.name) + manifest = {"schema": 1, "chain": chain, "shaders": records} + (output / "manifest.json").write_text(json.dumps(manifest, separators=(",", ":")) + "\n") + return len(records) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("cache", type=Path) + parser.add_argument("libraries", type=Path) + parser.add_argument("output", type=Path) + args = parser.parse_args() + header = Path(__file__).resolve().parents[2] / "gfx/nvn/dmsh_chain.h" + match = re.search(r'#define\s+DMSH_CHAIN\s+"([a-f0-9]+)"', header.read_text()) + if match is None: + raise ValueError(f"No DMSH_CHAIN in {header}") + chain = match.group(1) + count = export(args.cache, args.libraries, args.output, chain) + print(f"Exported {count} chain-matched non-point sources (chain {chain})") diff --git a/dm/ios/tools/export_sampler_limit_sources.py b/dm/ios/tools/export_sampler_limit_sources.py new file mode 100644 index 0000000000..e2fcb149a7 --- /dev/null +++ b/dm/ios/tools/export_sampler_limit_sources.py @@ -0,0 +1,259 @@ +#!/usr/bin/env python3 +"""Export exact, hash-bound Metal sources with unused scalar samplers removed. + +Metal Tier 1 argument buffers expose at most 16 indirect sampler resources to a +single function. SPIRV-Cross can still declare a sampler for a texture that is +only accessed with ``read()``. This exporter removes only sampler declarations +whose identifiers are absent everywhere else in the complete source. It does +not renumber argument IDs or rewrite texture operations. + +Game-derived sources and generated companions remain outside source control; +the base shader translation chain and metallibs are unchanged. +""" +import argparse +import hashlib +import json +from pathlib import Path +import re + + +LIMIT = 16 +PROOF = "unused-sampler-declaration-v1" +IDENT = r"[A-Za-z_]\w*" +SCALAR_DECL = re.compile( + rf"\bsampler\s+(?P{IDENT})\s*\[\[\s*id\s*\(\s*(?P\d+)\s*\)\s*\]\]\s*;" +) +ARRAY_SAMPLER = re.compile(r"array<\s*sampler\s*,\s*([1-9]\d*)\s*>") + + +def digest(data): + return hashlib.sha256(data).hexdigest() + + +def _masked_source(source): + """Blank comments and literals while preserving offsets and line breaks.""" + out = list(source) + index = 0 + state = "code" + while index < len(source): + char = source[index] + nxt = source[index + 1] if index + 1 < len(source) else "" + if state == "code": + if char == "/" and nxt == "/": + out[index] = out[index + 1] = " " + state = "line" + index += 2 + continue + if char == "/" and nxt == "*": + out[index] = out[index + 1] = " " + state = "block" + index += 2 + continue + if char == '"': + out[index] = " " + state = "string" + elif char == "'": + out[index] = " " + state = "char" + elif state == "line": + if char == "\n": + state = "code" + else: + out[index] = " " + elif state == "block": + if char == "*" and nxt == "/": + out[index] = out[index + 1] = " " + state = "code" + index += 2 + continue + if char != "\n": + out[index] = " " + elif state in {"string", "char"}: + if char == "\\" and index + 1 < len(source): + if char != "\n": + out[index] = " " + if source[index + 1] != "\n": + out[index + 1] = " " + index += 2 + continue + delimiter = '"' if state == "string" else "'" + if char == delimiter: + out[index] = " " + state = "code" + elif char != "\n": + out[index] = " " + index += 1 + if state in {"block", "string", "char"}: + raise ValueError("Unterminated comment or literal") + return "".join(out) + + +def _check_preprocessor(masked): + if "\\\n" in masked or "\\\r\n" in masked: + raise ValueError("Preprocessor line continuations are unsupported") + for line in masked.splitlines(): + match = re.match(r"\s*#\s*([A-Za-z_]\w*)", line) + if match and match.group(1) not in {"include", "pragma"}: + raise ValueError("Unsupported preprocessor directive #" + match.group(1)) + + +def _struct_body(masked): + match = re.search(r"\bstruct\s+spvDescriptorSetBuffer0\s*\{", masked) + if not match: + raise ValueError("Missing spvDescriptorSetBuffer0 argument structure") + opening = masked.find("{", match.start()) + depth = 1 + end = opening + 1 + while depth and end < len(masked): + depth += (masked[end] == "{") - (masked[end] == "}") + end += 1 + if depth: + raise ValueError("Unbalanced argument structure") + if re.search(r"\bstruct\s+spvDescriptorSetBuffer0\s*\{", masked[end:]): + raise ValueError("Duplicate argument structure") + return opening + 1, end - 1 + + +def sampler_weight(binding): + msl_type = binding.get("msl_type") + if msl_type == "sampler": + return 1 + if isinstance(msl_type, str): + match = ARRAY_SAMPLER.fullmatch(msl_type) + if match: + return int(match.group(1)) + return 0 + + +def sampler_count(meta): + return sum(sampler_weight(binding) for binding in meta.get("bindings", [])) + + +def lower(source, meta, limit=LIMIT): + if not isinstance(limit, int) or isinstance(limit, bool) or limit < 1: + raise ValueError("Invalid sampler limit") + bindings = meta.get("bindings") + if not isinstance(bindings, list): + raise ValueError("Missing reflection bindings") + original_count = sampler_count(meta) + if original_count <= limit: + raise ValueError("Shader does not exceed the sampler limit") + if any(sampler_weight(binding) > 1 for binding in bindings): + raise ValueError("Sampler arrays require a separate proof") + if any(binding.get("kind") in {"img", "imga"} for binding in bindings): + raise ValueError("Image companions cannot be composed with sampler pruning") + + names = set() + argument_ids = set() + scalar = [] + for binding in bindings: + name = binding.get("msl_name") + aid = binding.get("arg_id") + if not isinstance(name, str) or not re.fullmatch(IDENT, name): + raise ValueError("Invalid resource identifier") + if not isinstance(aid, int) or isinstance(aid, bool) or aid < 0: + raise ValueError("Invalid resource argument ID") + if name in names or aid in argument_ids: + raise ValueError("Duplicate resource name or argument ID") + names.add(name) + argument_ids.add(aid) + if sampler_weight(binding): + if not isinstance(binding.get("kind"), str) or not binding["kind"].startswith("samp"): + raise ValueError("Sampler reflection kind mismatch") + scalar.append(binding) + + masked = _masked_source(source) + _check_preprocessor(masked) + body_begin, body_end = _struct_body(masked) + declarations = {} + for match in SCALAR_DECL.finditer(masked, body_begin, body_end): + name = match.group("name") + if name in declarations: + raise ValueError("Duplicate scalar sampler declaration") + declarations[name] = match + if set(declarations) != {binding["msl_name"] for binding in scalar}: + raise ValueError("Sampler declarations and reflection disagree") + + candidates = [] + for binding in scalar: + match = declarations[binding["msl_name"]] + if int(match.group("aid")) != binding["arg_id"]: + raise ValueError("Sampler declaration argument ID mismatch") + without_declaration = masked[:match.start()] + " " * (match.end() - match.start()) + masked[match.end():] + if not re.search(rf"\b{re.escape(binding['msl_name'])}\b", without_declaration): + candidates.append((binding["arg_id"], match, binding)) + + needed = original_count - limit + if len(candidates) < needed: + raise ValueError("Not enough statically unused scalar samplers") + selected = sorted(candidates, key=lambda item: item[0])[:needed] + result = source + for _, match, _ in sorted(selected, key=lambda item: item[1].start(), reverse=True): + result = result[:match.start()] + result[match.end():] + removed = [] + for _, _, binding in selected: + record = {field: binding[field] for field in ("msl_name", "msl_type", "arg_id", "kind")} + if "reg" in binding: + record["reg"] = binding["reg"] + removed.append(record) + remaining = original_count - len(removed) + if remaining > limit or sampler_count({"bindings": [b for b in bindings if b not in [s[2] for s in selected]]}) != remaining: + raise ValueError("Internal sampler count mismatch") + return result, { + "proof": PROOF, + "limit": limit, + "original_sampler_count": original_count, + "remaining_sampler_count": remaining, + "removed": removed, + } + + +def export(cache, libraries, output, chain, limit=LIMIT): + if cache.name != "b-" + chain or libraries.name != "b-" + chain: + raise ValueError("Source and library directories must match the host chain") + output.mkdir(parents=True, exist_ok=True) + records = {} + for reflection in sorted(libraries.glob("*_b.json")): + meta = json.loads(reflection.read_text()) + if sampler_count(meta) <= limit: + continue + if meta.get("chain") != chain or meta.get("key") != reflection.stem or meta.get("route") != "B": + raise ValueError("Mismatched shader reflection " + reflection.name) + source_path = cache / (reflection.stem + ".metal") + library_path = libraries / (reflection.stem + ".metallib") + original = source_path.read_bytes() + record = { + "supported": False, + "original_sampler_count": sampler_count(meta), + "stage": meta.get("stage"), + "original_source_sha256": digest(original), + "base_library_sha256": digest(library_path.read_bytes()), + "reflection_sha256": digest(reflection.read_bytes()), + } + try: + companion, proof = lower(original.decode(), meta, limit) + data = companion.encode() + filename = reflection.stem + ".metal" + (output / filename).write_bytes(data) + record.update(proof, supported=True, source=filename, source_sha256=digest(data)) + except (UnicodeDecodeError, ValueError) as error: + record["reason"] = str(error) + records[reflection.stem] = record + manifest = {"schema": 1, "chain": chain, "kind": "sampler-limit-v1", "limit": limit, "shaders": records} + (output / "manifest.json").write_text(json.dumps(manifest, separators=(",", ":")) + "\n") + return records + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + for name in ("cache", "libraries", "output"): + parser.add_argument(name, type=Path) + args = parser.parse_args() + header = Path(__file__).resolve().parents[2] / "gfx/nvn/dmsh_chain.h" + match = re.search(r'#define\s+DMSH_CHAIN\s+"([a-f0-9]+)"', header.read_text()) + if match is None: + raise ValueError(f"No DMSH_CHAIN in {header}") + active_chain = match.group(1) + exported = export(args.cache, args.libraries, args.output, active_chain) + supported = sum(record.get("supported") is True for record in exported.values()) + print(f"Sampler-limit sources: {supported} proven / {len(exported)} over limit (chain {active_chain})") diff --git a/dm/ios/tools/export_zero_image_sources.py b/dm/ios/tools/export_zero_image_sources.py new file mode 100644 index 0000000000..1649fb8ef9 --- /dev/null +++ b/dm/ios/tools/export_zero_image_sources.py @@ -0,0 +1,211 @@ +#!/usr/bin/env python3 +"""Export tightly proven zero-image writes as atomic shadow-buffer writes. + +This companion preserves the original fragment coverage and coordinates. It does +not prove that a draw covers a rectangle or authorize clearing an entire image. +The host owns complete shadow initialization/copyback and validates its layout. +Inputs and generated shader data stay outside source control; the base shader +translation chain and libraries are unchanged. +""" +import argparse +import hashlib +import json +from pathlib import Path +import re + + +SLOT = 29 +PROOF = "fragment-zero-writes-v1" +IDENT = r"[A-Za-z_]\w*" + + +def digest(data): + return hashlib.sha256(data).hexdigest() + + +def uncomment(source): + return re.sub(r"//[^\n]*|/\*.*?\*/", " ", source, flags=re.S) + + +def prove(source, meta): + """Accept a small straight-line grammar, never arbitrary Metal expressions.""" + source = uncomment(source) + if re.search(r"\bdm_zero_(?:store|layout)\b", source): + raise ValueError("Reserved compatibility symbol is occupied") + if re.search(r"\[\[\s*buffer\s*\(\s*29\s*\)\s*\]\]", source): + raise ValueError("Compatibility buffer slot 29 is occupied") + if meta.get("stage") != "ps" or meta.get("msl_entry_kind") != "fragment": + raise ValueError("Only fragment entries are supported") + raster = meta.get("raster") + if not isinstance(raster, dict) or raster.get("discard") != 0 or raster.get("sample_mask") != 0 or raster.get("colors") != []: + raise ValueError("Fragment discard, sample mask or color outputs are unproven") + if meta.get("drv_unfillable") or meta.get("drv_reads") or meta.get("vertex_inputs"): + raise ValueError("Additional shader inputs are unproven") + bindings = meta.get("bindings", []) + if len(bindings) != 2: + raise ValueError("Exactly one support and one write-only image binding are required") + support = next((b for b in bindings if b.get("kind") == "support"), None) + image = next((b for b in bindings if b.get("kind") == "img"), None) + if not support or not image or support.get("rw") or image.get("rw"): + raise ValueError("Unexpected or writable resource binding") + if image.get("msl_type") != "texture2d_array": + raise ValueError("Image must be a write-only float 2D array") + if support.get("arg_id") != 0 or image.get("arg_id") != 1 or not isinstance(image.get("reg"), int) or isinstance(image.get("reg"), bool) or image["reg"] < 0: + raise ValueError("Unsupported resource argument layout") + for binding in bindings: + if not re.fullmatch(IDENT, binding.get("msl_name", "")): + raise ValueError("Invalid resource name") + entry = meta.get("entry", "") + if not re.fullmatch(IDENT, entry): + raise ValueError("Invalid entry name") + + # The fixed support structure is part of the current host ABI. Matching its + # entire declaration also rejects hidden device pointers and helper methods. + grammar = rf"""\A\s*\#include\s*\s*\#include\s*\s* + using\s+namespace\s+metal\s*;\s* + struct\s+(?P_\d+)\s*\{{\s* + uint\s+_m0\s*;\s*uint4\s+_m1\s*\[\s*8\s*\]\s*;\s* + float4\s+_m2\s*;\s*float4\s+_m3\s*;\s*int\s+_m4\s*;\s* + float4\s+_m5\s*\[\s*73\s*\]\s*;\s*int4\s+_m6\s*;\s*int\s+_m7\s*;\s*\}}\s*;\s* + struct\s+(?P{IDENT})\s*\{{\s* + constant\s+(?P=support_type)\s*\*\s*{re.escape(support['msl_name'])}\s*\[\[\s*id\s*\(\s*0\s*\)\s*\]\]\s*;\s* + (?Ptexture2d_array\s*<\s*float\s*,\s*access::write\s*>\s*{re.escape(image['msl_name'])})\s*\[\[\s*id\s*\(\s*1\s*\)\s*\]\]\s*;\s*\}}\s*;\s* + fragment\s+void\s+{re.escape(entry)}\s*\(\s* + constant\s+(?P=argument_type)\s*&\s*(?P{IDENT})\s*\[\[\s*buffer\s*\(\s*0\s*\)\s*\]\]\s*,\s* + float4\s+(?P{IDENT})\s*\[\[\s*position\s*\]\]\s* + (?P\))\s*\{{(?P[^{{}}]*)\}}\s*\Z""" + match = re.fullmatch(grammar, source, flags=re.X | re.S) + if not match or support.get("msl_type") != "constant " + match["support_type"] + "*": + raise ValueError("Source declarations, entry signature or support ABI are unproven") + values = {} + writes = [] + body = match["body"] + statements = body.split(";") + if statements[-1].strip(): + raise ValueError("Unterminated fragment statement") + scale_read = f"(*{match['arguments']}.{support['msl_name']})._m5[0].x" + image_expr = f"{match['arguments']}.{image['msl_name']}" + replacements = [] + cursor = 0 + for statement in statements[:-1]: + compact = re.sub(r"\s+", "", statement) + write = re.fullmatch(re.escape(image_expr) + rf"\.write\(float4\(0(?:\.0+)?\),uint2\(({IDENT})\.xy\),uint\(\1\.z\)\)", compact) + if write: + coordinate = values.get(write[1]) + if not coordinate or coordinate[0] != "pixel": + raise ValueError("Image write coordinates are unproven") + writes.append(coordinate[1]) + replacements.append((cursor, cursor + len(statement), f"\n dm_zero_store({image_expr}, uint2({write[1]}.xy), uint({write[1]}.z), dm_zero_layout)")) + else: + declaration = re.fullmatch(rf"\s*(float|int|int3)\s+({IDENT})\s*=\s*(.*?)\s*", statement, flags=re.S) + if not declaration or declaration[2] in values: + raise ValueError("Only immutable typed locals and proven zero writes are permitted") + kind, name, expression = declaration.groups() + expression = re.sub(r"\s+", "", expression) + value = None + if kind == "float": + for axis in "xy": + if expression == match["position"] + "." + axis: + value = ("fragment", axis) + if expression == scale_read: + value = ("scale",) + division = re.fullmatch(rf"({IDENT})/({IDENT})", expression) + if division and values.get(division[1], (None,))[0] == "fragment" and values.get(division[2]) == ("scale",): + value = ("scaled", values[division[1]][1]) + truncation = re.fullmatch(rf"trunc\(({IDENT})\)", expression) + if truncation and values.get(truncation[1], (None,))[0] == "scaled": + value = ("truncated", values[truncation[1]][1]) + elif kind == "int": + conversion = re.fullmatch(rf"int\(({IDENT})\)", expression) + if conversion and values.get(conversion[1], (None,))[0] == "truncated": + value = ("coordinate", values[conversion[1]][1]) + else: + vector = re.fullmatch(rf"int3\(({IDENT}),({IDENT}),(0|[1-9]\d*)\)", expression) + if vector and values.get(vector[1]) == ("coordinate", "x") and values.get(vector[2]) == ("coordinate", "y") and int(vector[3]) < 2**31: + value = ("pixel", int(vector[3])) + if value is None: + raise ValueError("Unproven local expression: " + name) + values[name] = value + cursor += len(statement) + 1 + if not writes: + raise ValueError("No proven zero image writes") + return source, match, replacements, sorted(set(writes)), image + + +HELPER = """// The host validates layout and supplies a fully initialized shadow buffer. +static inline void dm_zero_store(device atomic_uint* image, uint2 xy, uint layer, + constant uint* layout) { + uint width = layout[0], height = layout[1], layers = layout[2]; + uint words = layout[3], row_words = layout[4], image_words = layout[5]; + if (xy.x >= width || xy.y >= height || layer >= layers) return; + if (words != 1u && words != 2u && words != 4u) return; + ulong at = ulong(layer) * image_words + ulong(xy.y) * row_words + ulong(xy.x) * words; + for (uint word = 0; word < words; word++) + atomic_store_explicit(image + at + word, 0u, memory_order_relaxed); +} + +""" + + +def lower(source, meta): + source, match, replacements, layers, image = prove(source, meta) + body = match["body"] + for begin, end, replacement in reversed(replacements): + body = body[:begin] + replacement + body[end:] + source = source[:match.start("body")] + body + source[match.end("body"):] + position = match.start("signature_end") + source = source[:position] + f", constant uint* dm_zero_layout [[buffer({SLOT})]]" + source[position:] + begin, end = match.span("image_declaration") + source = source[:begin] + "device atomic_uint* " + image["msl_name"] + source[end:] + if "fragment void" in source: + position = source.index("fragment void") + else: + fragment = re.search(r"fragment\s+void", source) + if fragment is None: + raise ValueError("No fragment entry point") + position = fragment.start() + source = source[:position] + HELPER + source[position:] + return source, {"proof": PROOF, "parameter_slot": SLOT, "image_aid": image["arg_id"], "image_reg": image["reg"], "layers": layers, + "coordinate_proof": "trunc-fragcoord-over-support-scale-x-v1", "support_scale_byte_offset": 192, + "layout_words": ["width", "height", "layers", "words_per_pixel", "row_words", "image_words"], + "supported_pixel_bytes": [4, 8, 16], "preserves_fragment_coverage": True} + + +def export(cache, libraries, output, chain): + if cache.name != "b-" + chain or libraries.name != "b-" + chain: + raise ValueError("Source and library directories must match the host chain") + output.mkdir(parents=True, exist_ok=True) + records = {} + for reflection in sorted(libraries.glob("*_b.json")): + meta = json.loads(reflection.read_text()) + if meta.get("stage") != "ps" or not any(b.get("kind") == "img" for b in meta.get("bindings", [])): + continue + if meta.get("chain") != chain or meta.get("key") != reflection.stem: + raise ValueError("Mismatched shader reflection " + reflection.name) + original = (cache / (reflection.stem + ".metal")).read_bytes() + try: + companion, record = lower(original.decode(), meta) + except ValueError: + continue # Unsupported shaders have no authorization record. + data = companion.encode() + filename = reflection.stem + ".metal" + (output / filename).write_bytes(data) + record.update(source=filename, source_sha256=digest(data), base_library_sha256=digest((libraries / (reflection.stem + ".metallib")).read_bytes()), + reflection_sha256=digest(reflection.read_bytes()), original_source_sha256=digest(original)) + records[reflection.stem] = record + (output / "manifest.json").write_text(json.dumps({"schema": 1, "chain": chain, "kind": "zero-image-clear-v1", "shaders": records}, separators=(",", ":")) + "\n") + return records + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + for name in ("cache", "libraries", "output"): + parser.add_argument(name, type=Path) + args = parser.parse_args() + header = Path(__file__).resolve().parents[2] / "gfx/nvn/dmsh_chain.h" + match = re.search(r'#define\s+DMSH_CHAIN\s+"([a-f0-9]+)"', header.read_text()) + if match is None: + raise ValueError(f"No DMSH_CHAIN in {header}") + chain = match.group(1) + records = export(args.cache, args.libraries, args.output, chain) + print(f"Exported {len(records)} proven zero-image companions (chain {chain})") diff --git a/dm/ios/tools/gen_embed.py b/dm/ios/tools/gen_embed.py index a324f8ee2d..c83ed3a1d5 100644 --- a/dm/ios/tools/gen_embed.py +++ b/dm/ios/tools/gen_embed.py @@ -1,15 +1,10 @@ # Generate the embedded assembly: compile the rewritten module image into the executable, with code going into the single executable segment __TEXT only (Apple ITMS-90999: only one segment with execute permission is allowed). -# __TEXT,__dmx0 original text: [0, round16K(TEXT_END)) (includes part of the first ro page, read-only) -# __DMW,__img RW segment: [round16K(TEXT_END), PATCH_OFF), rest of ro + data + bss (relocations land only here) -# __TEXT,__dmx1 patch area: [PATCH_OFF, round16K(end of file)) +# __TEXT,__dmimg complete address layout: original text, a zero-filled hole for W, then the patch area +# __DMW,__img RW source: [round16K(TEXT_END), PATCH_OFF), rest of ro + data + bss (relocations land only here) +# At run time the loader leaves both code ranges in their signed __TEXT addresses and replaces only the zero-filled hole with a copy-on-write mapping of W. This never creates executable memory or a second executable segment. # Modules with the patch area before the base (negative PATCH_OFF, see patch_nso.py FAR): W is [round16K(TEXT_END), FILE_IMAGE_LEN), x1 is the patch area after FILE_IMAGE_LEN in the file; bss and the key page are not in the executable -# The three pieces are not adjacent in the executable; at run time the loader (loader.c iOS branch) reserves one contiguous address range and vm_remaps them back at their original relative positions (code pages share the signed pages, W is copy-on-write), -# so the run-time layout is byte-identical to the original image: PC-relative addressing between original code and data, relative jumps in the patch area, and the host's "base + original offset" references all need no change. -# The NRO modules the game loads at run time (nro.tsv beside the modules, dm/tools/nro_collect.py; Super Smash Bros. Ultimate: 98) are packed instead of getting sections of their own: a Mach-O -# file has at most 255 sections, and three per module would pass it. Their pieces follow each other, each 16K aligned, in __TEXT,__dm_nro_x0 / __DM_NRO_W,__img / __TEXT,__dm_nro_x1, and -# __DATA,__dm_nro_tab lists them: per module 32 bytes of name, then the offset and length of its x0, W and x1 pieces in their sections (u64 each); the loader maps each from there at start -# (loader.c nro_pieces). Rows named 'refused' (an online SDK the host never loads) have no module -# Usage: gen_embed.py ; link flags are printed to stdout (-segprot for the W segments) +# Modules with a negative PATCH_OFF retain the older three-piece representation for now. +# Usage: gen_embed.py ; link flags are printed to stdout (-segprot for the W segment) import os, sys out, dst, names = sys.argv[1], sys.argv[2], sys.argv[3:] PG = 0x4000 @@ -20,24 +15,46 @@ def pieces(n): # the module's (x0, W, x1) as (file start, end) ko, ks = int(hdr['KEY_PAGE_OFF'], 16), int(hdr['KEY_PAGE_SIZE'], 16) bin_ = os.path.abspath(f'{out}/{n}.patched.bin'); sz = os.path.getsize(bin_) a, end = r16(te), r16(sz) - if po < 0: # patch area before the base (patch_nso.py FAR): W ends at the end of data, the loader supplies zero pages for bss and the key page; in the file the patch area follows at FILE_IMAGE_LEN + if po < 0: # patch area before the base: W ends at the end of data and x1 follows at FILE_IMAGE_LEN fl = int(hdr['FILE_IMAGE_LEN'], 16) assert fl % PG == 0 and a <= fl and ko % PG == 0 and ks == PG and ko >= fl return bin_, sz, ((0, a), (a, fl), (fl, end)) assert po % PG == 0 and a <= ko and ko + ks <= po and ko % PG == 0 and ks == PG return bin_, sz, ((0, a), (a, po), (po, end)) -def place(asm, bin_, sz, lo, hi): # one piece, 16K aligned at both ends (vm_remap takes whole pages) +def place(asm, bin_, sz, lo, hi): # one piece, 16K aligned at both ends fill = min(hi, sz) - lo asm += [' .p2align 14', f' .incbin "{bin_}", {lo:#x}, {fill:#x}'] if hi - lo > fill: asm.append(f' .space {hi - lo - fill:#x}') asm.append(' .p2align 14') asm = ['// Generated by dm/ios/tools/gen_embed.py; do not edit'] -flags = [] +flags = ['-Wl,-segprot,__TEXT,rwx,rx'] for n in names: - bin_, sz, parts = pieces(n) - for sect, (lo, hi) in zip((f'__TEXT,__dm{n}x0', f'__DM{n}W,__img', f'__TEXT,__dm{n}x1'), parts): - assert all(len(x) <= 16 for x in sect.split(',')) - asm.append(f' .section {sect}'); place(asm, bin_, sz, lo, hi) + hdr = dict(l[2:].split() for l in open(f'{out}/{n}.fixups.tsv')) + te, po = int(hdr['TEXT_END'], 16), int(hdr['PATCH_OFF'], 16) + ko, ks = int(hdr['KEY_PAGE_OFF'], 16), int(hdr['KEY_PAGE_SIZE'], 16) + bin_ = os.path.abspath(f'{out}/{n}.patched.bin'); sz = os.path.getsize(bin_) + a, end = r16(te), r16(sz) + if po < 0: # patch area before the base (patch_nso.py FAR): keep the older remap representation + fl = int(hdr['FILE_IMAGE_LEN'], 16) + assert fl % PG == 0 and a <= fl and ko % PG == 0 and ks == PG and ko >= fl + parts = ((f'__TEXT,__dm{n}x0', 0, a), (f'__DM{n}W,__img', a, fl), (f'__TEXT,__dm{n}x1', fl, end)) + for sect, lo, hi in parts: + assert all(len(x) <= 16 for x in sect.split(',')[:2]) + fill = min(hi, sz) - lo + asm += [f' .section {sect}', ' .p2align 14', f' .incbin "{bin_}", {lo:#x}, {fill:#x}'] + if hi - lo > fill: asm.append(f' .space {hi - lo - fill:#x}') + asm.append(' .p2align 14') + else: + assert po % PG == 0 and a <= ko and ko + ks <= po and ko % PG == 0 and ks == PG + text_sect = f'__TEXT,__dm{n}img,regular,pure_instructions' + assert len(text_sect.split(',')[1]) <= 16 + patch_fill = min(end, sz) - po + asm += [f' .section {text_sect}', ' .p2align 14', + f' .incbin "{bin_}", 0x0, {a:#x}', f' .space {po - a:#x}', + f' .incbin "{bin_}", {po:#x}, {patch_fill:#x}'] + if end - po > patch_fill: asm.append(f' .space {end - po - patch_fill:#x}') + asm += [' .p2align 14', f' .section __DM{n}W,__img', ' .p2align 14', + f' .incbin "{bin_}", {a:#x}, {po - a:#x}', ' .p2align 14'] flags.append(f'-Wl,-segprot,__DM{n}W,rw,rw') tab = os.path.join(out, 'nro.tsv') nros = [ln.split('\t')[0] for ln in open(tab) if ln.strip() and not ln.startswith('#') and not ln.startswith('refused\t')] if os.path.exists(tab) else [] diff --git a/dm/ios/tools/ios_metal.py b/dm/ios/tools/ios_metal.py index f270b18a9a..67541176f5 100644 --- a/dm/ios/tools/ios_metal.py +++ b/dm/ios/tools/ios_metal.py @@ -4,8 +4,7 @@ # -fmetal-math-mode=relaxed: keep fast math's reassociation / contraction, but forbid assuming there are no NaN / Inf. The translated shaders reproduce Maxwell's NaN semantics with isnan selects (8337 / 12809 use them), # and the default fast mode lets each GPU's backend compiler treat them as always false; on the A20 Pro the opening course's odd frames had 727k NaN pixels in the main image (a few dozen with the same AIR on the M4 Pro), which became the black band / black-edged glow # -fpreserve-invariance: [[invariant]] position outputs only agree across shaders with this flag (as dmsh_build.py's MAC_METAL: a depth pre-pass followed by a less-or-equal G-buffer) -# -O1: -O2's switch folding leaves a control-flow graph Apple's GPU backend can only lower slowly into very large code (as dmsh_build.py's MAC_METAL) -METAL = ['xcrun', '-sdk', 'iphoneos', 'metal', '-c', '-O1', '-mios-version-min=18.0', '-fmetal-math-mode=relaxed', '-fpreserve-invariance'] +METAL = ['xcrun', '-sdk', 'iphoneos', 'metal', '-c', '-mios-version-min=16.1', '-fmetal-math-mode=relaxed', '-fpreserve-invariance'] METALLIB = ['xcrun', '-sdk', 'iphoneos', 'metallib'] RECORD = ' '.join(METAL) + '; ' + ' '.join(METALLIB) # Written into json chain.metal diff --git a/dm/ios/tools/set_text_maxprot.py b/dm/ios/tools/set_text_maxprot.py new file mode 100644 index 0000000000..757b5d3b89 --- /dev/null +++ b/dm/ios/tools/set_text_maxprot.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +"""Set an unsigned arm64 Mach-O executable's __TEXT maximum protection to RWX.""" +import struct +import sys + + +def main() -> None: + if len(sys.argv) != 2: + raise SystemExit(f"usage: {sys.argv[0]} ") + path = sys.argv[1] + with open(path, "r+b") as f: + header = f.read(32) + if len(header) != 32 or struct.unpack_from(" 0x7, initprot stays 0x5") + return + off += cmdsize + raise SystemExit(f"{path}: __TEXT segment not found") + + +if __name__ == "__main__": + main() diff --git a/dm/ios/tools/stage_profile_env.py b/dm/ios/tools/stage_profile_env.py new file mode 100644 index 0000000000..2da50fd904 --- /dev/null +++ b/dm/ios/tools/stage_profile_env.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +"""Stage the iOS bundle environment from an explicit file or game profile. + +DM_ENV_FILE has priority when it is present in the environment. An explicitly +empty value disables a profile default. Otherwise /ios.env is used +when it exists. The selected file is copied byte-for-byte to the requested +output and its path is printed; no selection prints an empty line. +""" +import os +import shutil +import sys +from collections.abc import Mapping +from pathlib import Path + + +def select(profile: Path, environ: Mapping[str, str]) -> Path | None: + if "DM_ENV_FILE" in environ: + value = environ["DM_ENV_FILE"] + return Path(value) if value else None + default = profile / "ios.env" + return default if default.is_file() else None + + +def stage(profile: Path, output: Path, environ: Mapping[str, str]) -> Path | None: + source = select(profile, environ) + if source is None: + output.unlink(missing_ok=True) + return None + if not source.is_file(): + raise FileNotFoundError(f"iOS environment file not found: {source}") + output.parent.mkdir(parents=True, exist_ok=True) + shutil.copyfile(source, output) + return source + + +def main() -> None: + if len(sys.argv) != 3: + raise SystemExit("usage: stage_profile_env.py ") + try: + source = stage(Path(sys.argv[1]), Path(sys.argv[2]), os.environ) + except OSError as error: + raise SystemExit(str(error)) from error + print(source or "") + + +if __name__ == "__main__": + main() diff --git a/dm/ios/tools/validate_shader_companions.py b/dm/ios/tools/validate_shader_companions.py new file mode 100644 index 0000000000..fc56f5a1d1 --- /dev/null +++ b/dm/ios/tools/validate_shader_companions.py @@ -0,0 +1,185 @@ +#!/usr/bin/env python3 +"""Fail closed when a profile's packaged NVN companions are incomplete.""" +import argparse +import hashlib +import json +from pathlib import Path +import re + + +KEY = re.compile(r"[0-9a-f]{16}_b") +HASH = re.compile(r"[0-9a-f]{64}") + + +class CompanionError(ValueError): + pass + + +def read_json(path, what): + try: + value = json.loads(path.read_text(encoding="utf-8")) + except FileNotFoundError: + raise CompanionError(f"missing {what}: {path}") from None + except (OSError, UnicodeDecodeError, json.JSONDecodeError) as error: + raise CompanionError(f"invalid {what} {path}: {error}") from None + if not isinstance(value, dict): + raise CompanionError(f"{what} must be a JSON object: {path}") + return value + + +def component(value, what): + if not isinstance(value, str) or not value or value != Path(value).name or value in (".", ".."): + raise CompanionError(f"invalid {what}: {value!r}") + return value + + +def shader_key(value, what): + if not isinstance(value, str) or not KEY.fullmatch(value): + raise CompanionError(f"invalid {what}: {value!r}") + return value + + +def sha256(path, cache): + path = path.resolve() + if path in cache: + return cache[path] + try: + with path.open("rb") as stream: + digest = hashlib.file_digest(stream, "sha256").hexdigest() + except OSError as error: + raise CompanionError(f"missing or unreadable companion file {path}: {error}") from None + cache[path] = digest + return digest + + +def verify_hash(record, field, path, cache, what): + expected = record.get(field) + if not isinstance(expected, str) or not HASH.fullmatch(expected): + raise CompanionError(f"{what} has no valid {field}") + actual = sha256(path, cache) + if actual != expected: + raise CompanionError(f"{what} {field} mismatch: {path}") + + +def fields(value, allowed, what): + if not isinstance(value, dict): + raise CompanionError(f"{what} must be an object") + extra = sorted(set(value) - set(allowed)) + if extra: + raise CompanionError(f"unknown {what} field(s): {', '.join(extra)}") + + +def required_keys(spec, what): + values = spec.get("required_records", []) + if not isinstance(values, list) or not values: + raise CompanionError(f"{what} must name at least one required record") + keys = [shader_key(value, f"{what} required record") for value in values] + if len(keys) != len(set(keys)): + raise CompanionError(f"{what} repeats a required record") + return keys + + +def validate_manifest(root, chain, spec, cache): + fields(spec, {"directory", "kind", "limit", "required_records", "original_directory"}, "manifest requirement") + directory = component(spec.get("directory"), "companion directory") + where = root / directory + manifest = read_json(where / "manifest.json", f"{directory} manifest") + if manifest.get("schema") != 1 or manifest.get("chain") != chain: + raise CompanionError(f"{directory} manifest schema/chain does not match b-{chain}") + if "kind" in spec and manifest.get("kind") != spec["kind"]: + raise CompanionError(f"{directory} manifest kind is not {spec['kind']!r}") + if "limit" in spec and manifest.get("limit") != spec["limit"]: + raise CompanionError(f"{directory} manifest limit is not {spec['limit']!r}") + records = manifest.get("shaders") + if not isinstance(records, dict) or not records: + raise CompanionError(f"{directory} manifest has no shader records") + + original_directory = spec.get("original_directory") + if original_directory is not None: + original_directory = component(original_directory, f"{directory} original directory") + active = set() + for key, record in records.items(): + shader_key(key, f"{directory} shader key") + if not isinstance(record, dict): + raise CompanionError(f"{directory} record {key} is not an object") + supported = record.get("supported", True) + if not isinstance(supported, bool): + raise CompanionError(f"{directory} record {key} has a non-boolean supported value") + if not supported: + continue + source = record.get("source", key + ".metal") + source = component(source, f"{directory} source for {key}") + if source != key + ".metal": + raise CompanionError(f"{directory} source name does not match record {key}: {source}") + verify_hash(record, "source_sha256", where / source, cache, f"{directory} record {key}") + library_hash = "base_library_sha256" if "base_library_sha256" in record else "library_sha256" + verify_hash(record, library_hash, root / (key + ".metallib"), cache, f"{directory} record {key}") + if "reflection_sha256" in record: + verify_hash(record, "reflection_sha256", root / (key + ".json"), cache, f"{directory} record {key}") + if original_directory is not None: + verify_hash(record, "original_source_sha256", root / original_directory / (key + ".metal"), cache, + f"{directory} record {key}") + active.add(key) + if not active: + raise CompanionError(f"{directory} manifest has no usable shader records") + for key in required_keys(spec, directory): + if key not in records: + raise CompanionError(f"{directory} is missing required record {key}") + if key not in active: + raise CompanionError(f"{directory} required record {key} is unsupported") + return len(active) + + +def validate_record_directory(root, chain, spec, cache): + fields(spec, {"directory", "kind", "required_records"}, "record requirement") + directory = component(spec.get("directory"), "record companion directory") + kind = spec.get("kind") + if not isinstance(kind, str) or not kind: + raise CompanionError(f"{directory} record requirement has no kind") + where = root / directory + count = 0 + for key in required_keys(spec, directory): + record = read_json(where / (key + ".json"), f"{directory} record {key}") + if record.get("schema") != 1 or record.get("chain") != chain or record.get("kind") != kind: + raise CompanionError(f"{directory} record {key} schema/chain/kind mismatch") + verify_hash(record, "source_sha256", where / (key + ".metal"), cache, f"{directory} record {key}") + verify_hash(record, "library_sha256", root / (key + ".metallib"), cache, f"{directory} record {key}") + count += 1 + return count + + +def validate(profile, libraries, chain): + profile, libraries = Path(profile), Path(libraries) + requirements = read_json(profile, "iOS companion requirements") + fields(requirements, {"schema", "manifests", "records"}, "requirements") + if requirements.get("schema") != 1: + raise CompanionError(f"unsupported requirements schema in {profile}") + if libraries.name != "b-" + chain or not libraries.is_dir(): + raise CompanionError(f"shader library directory must be an existing b-{chain}: {libraries}") + manifests, records = requirements.get("manifests"), requirements.get("records", []) + if not isinstance(manifests, list) or not manifests: + raise CompanionError("requirements must contain at least one manifest companion") + if not isinstance(records, list): + raise CompanionError("record companions must be a list") + cache = {} + shaders = sum(validate_manifest(libraries, chain, item, cache) for item in manifests) + standalone = sum(validate_record_directory(libraries, chain, item, cache) for item in records) + return len(manifests), shaders, standalone + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("profile", type=Path, help="profile ios_companions.json") + parser.add_argument("libraries", type=Path, help="packaged b-CHAIN directory") + parser.add_argument("chain", help="host DMSH_CHAIN") + args = parser.parse_args() + try: + manifests, shaders, standalone = validate(args.profile, args.libraries, args.chain) + except CompanionError as error: + parser.exit(1, f"[RED] shader companions: {error}\n") + print(f"shader companions: {manifests} manifests, {shaders} usable shader records, " + f"{standalone} standalone records (chain {args.chain})") + + +if __name__ == "__main__": + main() diff --git a/dm/third_party/bcdec/LICENSE b/dm/third_party/bcdec/LICENSE new file mode 100644 index 0000000000..78e53f197c --- /dev/null +++ b/dm/third_party/bcdec/LICENSE @@ -0,0 +1,52 @@ +This software is available under 2 licenses -- choose whichever you prefer. + +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License + +Copyright (c) 2022 Sergii Kudlai + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +------------------------------------------------------------------------------ +ALTERNATIVE B - The Unlicense + +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/dm/third_party/bcdec/README.md b/dm/third_party/bcdec/README.md new file mode 100644 index 0000000000..85fb9c7b89 --- /dev/null +++ b/dm/third_party/bcdec/README.md @@ -0,0 +1,2 @@ +Source: https://github.com/iOrange/bcdec commit 80859ed (version 0.985), unmodified. License in LICENSE (MIT or public domain, at your choice). +Use: on GPUs without BC compressed formats, the translation layer decompresses BC textures on the CPU (dm/gfx/tex/nvn_bcdec.c). diff --git a/dm/third_party/bcdec/bcdec.h b/dm/third_party/bcdec/bcdec.h new file mode 100644 index 0000000000..553c3ec5a8 --- /dev/null +++ b/dm/third_party/bcdec/bcdec.h @@ -0,0 +1,1512 @@ +/* bcdec.h - v0.985 + provides functions to decompress blocks of BC compressed images + written by Sergii "iOrange" Kudlai in 2022 + + This library does not allocate memory and is trying to use as less stack as possible + + This library was never optimized specifically for speed but for the overall size + it has zero external dependencies and is not using any runtime functions + + This library was writtem by humans and will never contain any LLM-generated code + + Supported BC formats: + BC1 (also known as DXT1) + it's "binary alpha" variant BC1A (DXT1A) + BC2 (also known as DXT3) + BC3 (also known as DXT5) + BC4 (also known as ATI1N) + BC5 (also known as ATI2N) + BC6H (HDR format) + BC7 + + BC1/BC2/BC3/BC7 are expected to decompress into 4*4 RGBA blocks 8bit per component (32bit pixel) + BC4/BC5 are expected to decompress into 4*4 R/RG blocks 8bit per component (8bit and 16bit pixel) + BC6H is expected to decompress into 4*4 RGB blocks of either 32bit float or 16bit "half" per + component (96bit or 48bit pixel) + + For more info, issues and suggestions please visit https://github.com/iOrange/bcdec + + Configuration: + #define BCDEC_BC4BC5_PRECISE: + enables more precise but slower BC4/BC5 decoding + signed/unsigned mode + + CREDITS: + Aras Pranckevicius (@aras-p) - BC1/BC3 decoders optimizations (up to 3x the speed) + - BC6H/BC7 bits pulling routines optimizations + - optimized BC6H by moving unquantize out of the loop + - Split BC6H decompression function into 'half' and + 'float' variants + + Michael Schmidt (@RunDevelopment) - Found better "magic" coefficients for integer interpolation + of reference colors in BC1 color block, that match with + the floating point interpolation. This also made it faster + than integer division by 3! + + bugfixes: + @linkmauve + @wkjarosz + + LICENSE: See end of file for license information. +*/ + +#ifndef BCDEC_HEADER_INCLUDED +#define BCDEC_HEADER_INCLUDED + +#define BCDEC_VERSION_MAJOR 0 +#define BCDEC_VERSION_MINOR 985 + +/* if BCDEC_STATIC causes problems, try defining BCDECDEF to 'inline' or 'static inline' */ +#ifndef BCDECDEF +#ifdef BCDEC_STATIC +#define BCDECDEF static +#else +#ifdef __cplusplus +#define BCDECDEF extern "C" +#else +#define BCDECDEF extern +#endif +#endif +#endif + +/* Used information sources: + https://docs.microsoft.com/en-us/windows/win32/direct3d10/d3d10-graphics-programming-guide-resources-block-compression + https://docs.microsoft.com/en-us/windows/win32/direct3d11/bc6h-format + https://docs.microsoft.com/en-us/windows/win32/direct3d11/bc7-format + https://docs.microsoft.com/en-us/windows/win32/direct3d11/bc7-format-mode-reference + + ! WARNING ! Khronos's BPTC partitions tables contain mistakes, do not use them! + https://www.khronos.org/registry/DataFormat/specs/1.1/dataformat.1.1.html#BPTC + + ! Use tables from here instead ! + https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_texture_compression_bptc.txt + + Leaving it here as it's a nice read + https://fgiesen.wordpress.com/2021/10/04/gpu-bcn-decoding/ + + Fast half to float function from here + https://gist.github.com/rygorous/2144712 +*/ + +#define BCDEC_BC1_BLOCK_SIZE 8 +#define BCDEC_BC2_BLOCK_SIZE 16 +#define BCDEC_BC3_BLOCK_SIZE 16 +#define BCDEC_BC4_BLOCK_SIZE 8 +#define BCDEC_BC5_BLOCK_SIZE 16 +#define BCDEC_BC6H_BLOCK_SIZE 16 +#define BCDEC_BC7_BLOCK_SIZE 16 + +#define BCDEC_BC1_COMPRESSED_SIZE(w, h) ((((w)>>2)*((h)>>2))*BCDEC_BC1_BLOCK_SIZE) +#define BCDEC_BC2_COMPRESSED_SIZE(w, h) ((((w)>>2)*((h)>>2))*BCDEC_BC2_BLOCK_SIZE) +#define BCDEC_BC3_COMPRESSED_SIZE(w, h) ((((w)>>2)*((h)>>2))*BCDEC_BC3_BLOCK_SIZE) +#define BCDEC_BC4_COMPRESSED_SIZE(w, h) ((((w)>>2)*((h)>>2))*BCDEC_BC4_BLOCK_SIZE) +#define BCDEC_BC5_COMPRESSED_SIZE(w, h) ((((w)>>2)*((h)>>2))*BCDEC_BC5_BLOCK_SIZE) +#define BCDEC_BC6H_COMPRESSED_SIZE(w, h) ((((w)>>2)*((h)>>2))*BCDEC_BC6H_BLOCK_SIZE) +#define BCDEC_BC7_COMPRESSED_SIZE(w, h) ((((w)>>2)*((h)>>2))*BCDEC_BC7_BLOCK_SIZE) + +BCDECDEF void bcdec_bc1(const void* compressedBlock, void* decompressedBlock, int destinationPitch); +BCDECDEF void bcdec_bc2(const void* compressedBlock, void* decompressedBlock, int destinationPitch); +BCDECDEF void bcdec_bc3(const void* compressedBlock, void* decompressedBlock, int destinationPitch); +#ifndef BCDEC_BC4BC5_PRECISE +BCDECDEF void bcdec_bc4(const void* compressedBlock, void* decompressedBlock, int destinationPitch); +BCDECDEF void bcdec_bc5(const void* compressedBlock, void* decompressedBlock, int destinationPitch); +#else +BCDECDEF void bcdec_bc4(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned); +BCDECDEF void bcdec_bc5(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned); +BCDECDEF void bcdec_bc4_float(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned); +BCDECDEF void bcdec_bc5_float(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned); +#endif +BCDECDEF void bcdec_bc6h_float(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned); +BCDECDEF void bcdec_bc6h_half(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned); +BCDECDEF void bcdec_bc7(const void* compressedBlock, void* decompressedBlock, int destinationPitch); + +#endif /* BCDEC_HEADER_INCLUDED */ + +#ifdef BCDEC_IMPLEMENTATION + +static unsigned short bcdec__getu16le(const void* s) { + unsigned short l = (unsigned short)((const unsigned char*)s)[0]; + unsigned short h = (unsigned short)((const unsigned char*)s)[1]; + return l | (h << 8); +} +static unsigned int bcdec__getu32le(const void* s) { + unsigned int l = bcdec__getu16le(s); + unsigned int h = bcdec__getu16le((const unsigned char*)s + 2); + return l | (h << 16); +} +static unsigned long long bcdec__getu64le(const void* s) { + unsigned long long l = bcdec__getu32le(s); + unsigned long long h = bcdec__getu32le((const unsigned char*)s + 4); + return l | (h << 32); +} + +static void bcdec__color_block(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int onlyOpaqueMode) { + unsigned short c0, c1; + unsigned int refColors[4]; /* 0xAABBGGRR */ + unsigned char* dstColors; + unsigned int colorIndices; + int i, j, idx; + unsigned int r0, g0, b0, r1, g1, b1, r, g, b; + + c0 = bcdec__getu16le(compressedBlock); + c1 = bcdec__getu16le((const unsigned char*)compressedBlock + 2); + + /* Unpack 565 ref colors */ + r0 = (c0 >> 11) & 0x1F; + g0 = (c0 >> 5) & 0x3F; + b0 = c0 & 0x1F; + + r1 = (c1 >> 11) & 0x1F; + g1 = (c1 >> 5) & 0x3F; + b1 = c1 & 0x1F; + + /* Expand 565 ref colors to 888 */ + r = (r0 * 527 + 23) >> 6; + g = (g0 * 259 + 33) >> 6; + b = (b0 * 527 + 23) >> 6; + refColors[0] = 0xFF000000 | (b << 16) | (g << 8) | r; + + r = (r1 * 527 + 23) >> 6; + g = (g1 * 259 + 33) >> 6; + b = (b1 * 527 + 23) >> 6; + refColors[1] = 0xFF000000 | (b << 16) | (g << 8) | r; + + if (c0 > c1 || onlyOpaqueMode) { /* Standard BC1 mode (also BC3 color block uses ONLY this mode) */ + /* color_2 = 2/3*color_0 + 1/3*color_1 + color_3 = 1/3*color_0 + 2/3*color_1 */ + r = ((2 * r0 + r1) * 351 + 61) >> 7; + g = ((2 * g0 + g1) * 2763 + 1039) >> 11; + b = ((2 * b0 + b1) * 351 + 61) >> 7; + refColors[2] = 0xFF000000 | (b << 16) | (g << 8) | r; + + r = ((r0 + r1 * 2) * 351 + 61) >> 7; + g = ((g0 + g1 * 2) * 2763 + 1039) >> 11; + b = ((b0 + b1 * 2) * 351 + 61) >> 7; + refColors[3] = 0xFF000000 | (b << 16) | (g << 8) | r; + } else { /* Quite rare BC1A mode */ + /* color_2 = 1/2*color_0 + 1/2*color_1; + color_3 = 0; */ + r = ((r0 + r1) * 1053 + 125) >> 8; + g = ((g0 + g1) * 4145 + 1019) >> 11; + b = ((b0 + b1) * 1053 + 125) >> 8; + refColors[2] = 0xFF000000 | (b << 16) | (g << 8) | r; + + refColors[3] = 0x00000000; + } + + colorIndices = bcdec__getu32le((const unsigned char*)compressedBlock + 4); + + /* Fill out the decompressed color block */ + dstColors = (unsigned char*)decompressedBlock; + for (i = 0; i < 4; ++i) { + for (j = 0; j < 4; ++j) { + idx = colorIndices & 0x03; + ((unsigned int*)dstColors)[j] = refColors[idx]; + colorIndices >>= 2; + } + + dstColors += destinationPitch; + } +} + +static void bcdec__sharp_alpha_block(const void* compressedBlock, void* decompressedBlock, int destinationPitch) { + unsigned short alpha; + const unsigned char* alphaPtr; + unsigned char* decompressed; + int i, j; + + alphaPtr = (const unsigned char*)compressedBlock; + decompressed = (unsigned char*)decompressedBlock; + + for (i = 0; i < 4; ++i) { + alpha = bcdec__getu16le(alphaPtr); + + for (j = 0; j < 4; ++j) { + decompressed[j * 4] = ((alpha >> (4 * j)) & 0x0F) * 17; + } + + alphaPtr += 2; + decompressed += destinationPitch; + } +} + +static void bcdec__smooth_alpha_block(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int pixelSize) { + unsigned char* decompressed; + unsigned char alpha[8]; + int i, j; + unsigned long long block, indices; + + block = bcdec__getu64le(compressedBlock); + decompressed = (unsigned char*)decompressedBlock; + + alpha[0] = block & 0xFF; + alpha[1] = (block >> 8) & 0xFF; + + if (alpha[0] > alpha[1]) { + /* 6 interpolated alpha values. */ + alpha[2] = (6 * alpha[0] + alpha[1]) / 7; /* 6/7*alpha_0 + 1/7*alpha_1 */ + alpha[3] = (5 * alpha[0] + 2 * alpha[1]) / 7; /* 5/7*alpha_0 + 2/7*alpha_1 */ + alpha[4] = (4 * alpha[0] + 3 * alpha[1]) / 7; /* 4/7*alpha_0 + 3/7*alpha_1 */ + alpha[5] = (3 * alpha[0] + 4 * alpha[1]) / 7; /* 3/7*alpha_0 + 4/7*alpha_1 */ + alpha[6] = (2 * alpha[0] + 5 * alpha[1]) / 7; /* 2/7*alpha_0 + 5/7*alpha_1 */ + alpha[7] = ( alpha[0] + 6 * alpha[1]) / 7; /* 1/7*alpha_0 + 6/7*alpha_1 */ + } + else { + /* 4 interpolated alpha values. */ + alpha[2] = (4 * alpha[0] + alpha[1]) / 5; /* 4/5*alpha_0 + 1/5*alpha_1 */ + alpha[3] = (3 * alpha[0] + 2 * alpha[1]) / 5; /* 3/5*alpha_0 + 2/5*alpha_1 */ + alpha[4] = (2 * alpha[0] + 3 * alpha[1]) / 5; /* 2/5*alpha_0 + 3/5*alpha_1 */ + alpha[5] = ( alpha[0] + 4 * alpha[1]) / 5; /* 1/5*alpha_0 + 4/5*alpha_1 */ + alpha[6] = 0x00; + alpha[7] = 0xFF; + } + + indices = block >> 16; + for (i = 0; i < 4; ++i) { + for (j = 0; j < 4; ++j) { + decompressed[j * pixelSize] = alpha[indices & 0x07]; + indices >>= 3; + } + + decompressed += destinationPitch; + } +} + +#ifdef BCDEC_BC4BC5_PRECISE +static void bcdec__bc4_block(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int pixelSize, int isSigned) { + signed char* sblock; + unsigned char* ublock; + int alpha[8]; + int i, j; + unsigned long long block, indices; + + static int aWeights4[4] = { 13107, 26215, 39321, 52429 }; + static int aWeights6[6] = { 9363, 18724, 28086, 37450, 46812, 56173 }; + + block = bcdec__getu64le(compressedBlock); + + if (isSigned) { + alpha[0] = (char)(block & 0xFF); + alpha[1] = (char)((block >> 8) & 0xFF); + if (alpha[0] < -127) alpha[0] = -127; /* -128 clamps to -127 */ + if (alpha[1] < -127) alpha[1] = -127; /* -128 clamps to -127 */ + } else { + alpha[0] = block & 0xFF; + alpha[1] = (block >> 8) & 0xFF; + } + + if (alpha[0] > alpha[1]) { + /* 6 interpolated alpha values. */ + alpha[2] = (aWeights6[5] * alpha[0] + aWeights6[0] * alpha[1] + 32768) >> 16; /* 6/7*alpha_0 + 1/7*alpha_1 */ + alpha[3] = (aWeights6[4] * alpha[0] + aWeights6[1] * alpha[1] + 32768) >> 16; /* 5/7*alpha_0 + 2/7*alpha_1 */ + alpha[4] = (aWeights6[3] * alpha[0] + aWeights6[2] * alpha[1] + 32768) >> 16; /* 4/7*alpha_0 + 3/7*alpha_1 */ + alpha[5] = (aWeights6[2] * alpha[0] + aWeights6[3] * alpha[1] + 32768) >> 16; /* 3/7*alpha_0 + 4/7*alpha_1 */ + alpha[6] = (aWeights6[1] * alpha[0] + aWeights6[4] * alpha[1] + 32768) >> 16; /* 2/7*alpha_0 + 5/7*alpha_1 */ + alpha[7] = (aWeights6[0] * alpha[0] + aWeights6[5] * alpha[1] + 32768) >> 16; /* 1/7*alpha_0 + 6/7*alpha_1 */ + } else { + /* 4 interpolated alpha values. */ + alpha[2] = (aWeights4[3] * alpha[0] + aWeights4[0] * alpha[1] + 32768) >> 16; /* 4/5*alpha_0 + 1/5*alpha_1 */ + alpha[3] = (aWeights4[2] * alpha[0] + aWeights4[1] * alpha[1] + 32768) >> 16; /* 3/5*alpha_0 + 2/5*alpha_1 */ + alpha[4] = (aWeights4[1] * alpha[0] + aWeights4[2] * alpha[1] + 32768) >> 16; /* 2/5*alpha_0 + 3/5*alpha_1 */ + alpha[5] = (aWeights4[0] * alpha[0] + aWeights4[3] * alpha[1] + 32768) >> 16; /* 1/5*alpha_0 + 4/5*alpha_1 */ + alpha[6] = isSigned ? -127 : 0; + alpha[7] = isSigned ? 127 : 255; + } + + indices = block >> 16; + if (isSigned) { + sblock = (signed char*)decompressedBlock; + for (i = 0; i < 4; ++i) { + for (j = 0; j < 4; ++j) { + sblock[j * pixelSize] = (signed char)alpha[indices & 0x07]; + indices >>= 3; + } + sblock += destinationPitch; + } + } else { + ublock = (unsigned char*)decompressedBlock; + for (i = 0; i < 4; ++i) { + for (j = 0; j < 4; ++j) { + ublock[j * pixelSize] = (unsigned char)alpha[indices & 0x07]; + indices >>= 3; + } + ublock += destinationPitch; + } + } +} + +static void bcdec__bc4_block_float(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int pixelSize, int isSigned) { + float* decompressed; + float alpha[8]; + int i, j; + unsigned long long block, indices; + + block = bcdec__getu64le(compressedBlock); + decompressed = (float*)decompressedBlock; + + if (isSigned) { + alpha[0] = (float)((char)(block & 0xFF)) / 127.0f; + alpha[1] = (float)((char)((block >> 8) & 0xFF)) / 127.0f; + if (alpha[0] < -1.0f) alpha[0] = -1.0f; /* -128 clamps to -127 */ + if (alpha[1] < -1.0f) alpha[1] = -1.0f; /* -128 clamps to -127 */ + } else { + alpha[0] = (float)(block & 0xFF) / 255.0f; + alpha[1] = (float)((block >> 8) & 0xFF) / 255.0f; + } + + if (alpha[0] > alpha[1]) { + /* 6 interpolated alpha values. */ + alpha[2] = (6.0f * alpha[0] + alpha[1]) / 7.0f; /* 6/7*alpha_0 + 1/7*alpha_1 */ + alpha[3] = (5.0f * alpha[0] + 2.0f * alpha[1]) / 7.0f; /* 5/7*alpha_0 + 2/7*alpha_1 */ + alpha[4] = (4.0f * alpha[0] + 3.0f * alpha[1]) / 7.0f; /* 4/7*alpha_0 + 3/7*alpha_1 */ + alpha[5] = (3.0f * alpha[0] + 4.0f * alpha[1]) / 7.0f; /* 3/7*alpha_0 + 4/7*alpha_1 */ + alpha[6] = (2.0f * alpha[0] + 5.0f * alpha[1]) / 7.0f; /* 2/7*alpha_0 + 5/7*alpha_1 */ + alpha[7] = ( alpha[0] + 6.0f * alpha[1]) / 7.0f; /* 1/7*alpha_0 + 6/7*alpha_1 */ + } else { + /* 4 interpolated alpha values. */ + alpha[2] = (4.0f * alpha[0] + alpha[1]) / 5.0f; /* 4/5*alpha_0 + 1/5*alpha_1 */ + alpha[3] = (3.0f * alpha[0] + 2.0f * alpha[1]) / 5.0f; /* 3/5*alpha_0 + 2/5*alpha_1 */ + alpha[4] = (2.0f * alpha[0] + 3.0f * alpha[1]) / 5.0f; /* 2/5*alpha_0 + 3/5*alpha_1 */ + alpha[5] = ( alpha[0] + 4.0f * alpha[1]) / 5.0f; /* 1/5*alpha_0 + 4/5*alpha_1 */ + alpha[6] = isSigned ? -1.0f : 0.0f; + alpha[7] = 1.0f; + } + + indices = block >> 16; + for (i = 0; i < 4; ++i) { + for (j = 0; j < 4; ++j) { + decompressed[j * pixelSize] = alpha[indices & 0x07]; + indices >>= 3; + } + decompressed += destinationPitch; + } +} +#endif /* BCDEC_BC4BC5_PRECISE */ + +typedef struct bcdec__bitstream { + unsigned long long low; + unsigned long long high; +} bcdec__bitstream_t; + +static int bcdec__bitstream_read_bits(bcdec__bitstream_t* bstream, int numBits) { + unsigned int mask = (1 << numBits) - 1; + /* Read the low N bits */ + unsigned int bits = (bstream->low & mask); + + bstream->low >>= numBits; + /* Put the low N bits of "high" into the high 64-N bits of "low". */ + bstream->low |= (bstream->high & mask) << (sizeof(bstream->high) * 8 - numBits); + bstream->high >>= numBits; + + return bits; +} + +static int bcdec__bitstream_read_bit(bcdec__bitstream_t* bstream) { + return bcdec__bitstream_read_bits(bstream, 1); +} + +/* reversed bits pulling, used in BC6H decoding + why ?? just why ??? */ +static int bcdec__bitstream_read_bits_r(bcdec__bitstream_t* bstream, int numBits) { + int bits = bcdec__bitstream_read_bits(bstream, numBits); + /* Reverse the bits. */ + int result = 0; + while (numBits--) { + result <<= 1; + result |= (bits & 1); + bits >>= 1; + } + return result; +} + + + +BCDECDEF void bcdec_bc1(const void* compressedBlock, void* decompressedBlock, int destinationPitch) { + bcdec__color_block(compressedBlock, decompressedBlock, destinationPitch, 0); +} + +BCDECDEF void bcdec_bc2(const void* compressedBlock, void* decompressedBlock, int destinationPitch) { + bcdec__color_block(((char*)compressedBlock) + 8, decompressedBlock, destinationPitch, 1); + bcdec__sharp_alpha_block(compressedBlock, ((char*)decompressedBlock) + 3, destinationPitch); +} + +BCDECDEF void bcdec_bc3(const void* compressedBlock, void* decompressedBlock, int destinationPitch) { + bcdec__color_block(((char*)compressedBlock) + 8, decompressedBlock, destinationPitch, 1); + bcdec__smooth_alpha_block(compressedBlock, ((char*)decompressedBlock) + 3, destinationPitch, 4); +} + +#ifndef BCDEC_BC4BC5_PRECISE +BCDECDEF void bcdec_bc4(const void* compressedBlock, void* decompressedBlock, int destinationPitch) { + bcdec__smooth_alpha_block(compressedBlock, decompressedBlock, destinationPitch, 1); +#else +BCDECDEF void bcdec_bc4(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned) { + bcdec__bc4_block(compressedBlock, decompressedBlock, destinationPitch, 1, isSigned); +#endif +} + +#ifndef BCDEC_BC4BC5_PRECISE +BCDECDEF void bcdec_bc5(const void* compressedBlock, void* decompressedBlock, int destinationPitch) { + bcdec__smooth_alpha_block(compressedBlock, decompressedBlock, destinationPitch, 2); + bcdec__smooth_alpha_block(((char*)compressedBlock) + 8, ((char*)decompressedBlock) + 1, destinationPitch, 2); +#else +BCDECDEF void bcdec_bc5(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned) { + bcdec__bc4_block(compressedBlock, decompressedBlock, destinationPitch, 2, isSigned); + bcdec__bc4_block(((char*)compressedBlock) + 8, ((char*)decompressedBlock) + 1, destinationPitch, 2, isSigned); +#endif +} + +#ifdef BCDEC_BC4BC5_PRECISE +BCDECDEF void bcdec_bc4_float(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned) { + bcdec__bc4_block_float(compressedBlock, decompressedBlock, destinationPitch, 1, isSigned); +} + +BCDECDEF void bcdec_bc5_float(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned) { + bcdec__bc4_block_float(compressedBlock, decompressedBlock, destinationPitch, 2, isSigned); + bcdec__bc4_block_float(((char*)compressedBlock) + 8, ((float*)decompressedBlock) + 1, destinationPitch, 2, isSigned); +} +#endif /* BCDEC_BC4BC5_PRECISE */ + +/* http://graphics.stanford.edu/~seander/bithacks.html#VariableSignExtend */ +static int bcdec__extend_sign(int val, int bits) { + return (val << (32 - bits)) >> (32 - bits); +} + +static int bcdec__transform_inverse(int val, int a0, int bits, int isSigned) { + /* If the precision of A0 is "p" bits, then the transform algorithm is: + B0 = (B0 + A0) & ((1 << p) - 1) */ + val = (val + a0) & ((1 << bits) - 1); + if (isSigned) { + val = bcdec__extend_sign(val, bits); + } + return val; +} + +/* pretty much copy-paste from documentation */ +static int bcdec__unquantize(int val, int bits, int isSigned) { + int unq, s = 0; + + if (!isSigned) { + if (bits >= 15) { + unq = val; + } else if (!val) { + unq = 0; + } else if (val == ((1 << bits) - 1)) { + unq = 0xFFFF; + } else { + unq = ((val << 16) + 0x8000) >> bits; + } + } else { + if (bits >= 16) { + unq = val; + } else { + if (val < 0) { + s = 1; + val = -val; + } + + if (val == 0) { + unq = 0; + } else if (val >= ((1 << (bits - 1)) - 1)) { + unq = 0x7FFF; + } else { + unq = ((val << 15) + 0x4000) >> (bits - 1); + } + + if (s) { + unq = -unq; + } + } + } + return unq; +} + +static int bcdec__interpolate(int a, int b, int* weights, int index) { + return (a * (64 - weights[index]) + b * weights[index] + 32) >> 6; +} + +static unsigned short bcdec__finish_unquantize(int val, int isSigned) { + int s; + + if (!isSigned) { + return (unsigned short)((val * 31) >> 6); /* scale the magnitude by 31 / 64 */ + } else { + val = (val < 0) ? -(((-val) * 31) >> 5) : (val * 31) >> 5; /* scale the magnitude by 31 / 32 */ + s = 0; + if (val < 0) { + s = 0x8000; + val = -val; + } + return (unsigned short)(s | val); + } +} + +/* modified half_to_float_fast4 from https://gist.github.com/rygorous/2144712 */ +static float bcdec__half_to_float_quick(unsigned short half) { + typedef union { + unsigned int u; + float f; + } FP32; + + static const FP32 magic = { 113 << 23 }; + static const unsigned int shifted_exp = 0x7c00 << 13; /* exponent mask after shift */ + FP32 o; + unsigned int exp; + + o.u = (half & 0x7fff) << 13; /* exponent/mantissa bits */ + exp = shifted_exp & o.u; /* just the exponent */ + o.u += (127 - 15) << 23; /* exponent adjust */ + + /* handle exponent special cases */ + if (exp == shifted_exp) { /* Inf/NaN? */ + o.u += (128 - 16) << 23; /* extra exp adjust */ + } else if (exp == 0) { /* Zero/Denormal? */ + o.u += 1 << 23; /* extra exp adjust */ + o.f -= magic.f; /* renormalize */ + } + + o.u |= (half & 0x8000) << 16; /* sign bit */ + return o.f; +} + +BCDECDEF void bcdec_bc6h_half(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned) { + static char actual_bits_count[4][14] = { + { 10, 7, 11, 11, 11, 9, 8, 8, 8, 6, 10, 11, 12, 16 }, /* W */ + { 5, 6, 5, 4, 4, 5, 6, 5, 5, 6, 10, 9, 8, 4 }, /* dR */ + { 5, 6, 4, 5, 4, 5, 5, 6, 5, 6, 10, 9, 8, 4 }, /* dG */ + { 5, 6, 4, 4, 5, 5, 5, 5, 6, 6, 10, 9, 8, 4 } /* dB */ + }; + + /* There are 32 possible partition sets for a two-region tile. + Each 4x4 block represents a single shape. + Here also every fix-up index has MSB bit set. */ + static unsigned char partition_sets[32][4][4] = { + { {128, 0, 1, 1}, {0, 0, 1, 1}, { 0, 0, 1, 1}, {0, 0, 1, 129} }, /* 0 */ + { {128, 0, 0, 1}, {0, 0, 0, 1}, { 0, 0, 0, 1}, {0, 0, 0, 129} }, /* 1 */ + { {128, 1, 1, 1}, {0, 1, 1, 1}, { 0, 1, 1, 1}, {0, 1, 1, 129} }, /* 2 */ + { {128, 0, 0, 1}, {0, 0, 1, 1}, { 0, 0, 1, 1}, {0, 1, 1, 129} }, /* 3 */ + { {128, 0, 0, 0}, {0, 0, 0, 1}, { 0, 0, 0, 1}, {0, 0, 1, 129} }, /* 4 */ + { {128, 0, 1, 1}, {0, 1, 1, 1}, { 0, 1, 1, 1}, {1, 1, 1, 129} }, /* 5 */ + { {128, 0, 0, 1}, {0, 0, 1, 1}, { 0, 1, 1, 1}, {1, 1, 1, 129} }, /* 6 */ + { {128, 0, 0, 0}, {0, 0, 0, 1}, { 0, 0, 1, 1}, {0, 1, 1, 129} }, /* 7 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 1}, {0, 0, 1, 129} }, /* 8 */ + { {128, 0, 1, 1}, {0, 1, 1, 1}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 9 */ + { {128, 0, 0, 0}, {0, 0, 0, 1}, { 0, 1, 1, 1}, {1, 1, 1, 129} }, /* 10 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 1}, {0, 1, 1, 129} }, /* 11 */ + { {128, 0, 0, 1}, {0, 1, 1, 1}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 12 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 13 */ + { {128, 0, 0, 0}, {1, 1, 1, 1}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 14 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 0}, {1, 1, 1, 129} }, /* 15 */ + { {128, 0, 0, 0}, {1, 0, 0, 0}, { 1, 1, 1, 0}, {1, 1, 1, 129} }, /* 16 */ + { {128, 1, 129, 1}, {0, 0, 0, 1}, { 0, 0, 0, 0}, {0, 0, 0, 0} }, /* 17 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, {129, 0, 0, 0}, {1, 1, 1, 0} }, /* 18 */ + { {128, 1, 129, 1}, {0, 0, 1, 1}, { 0, 0, 0, 1}, {0, 0, 0, 0} }, /* 19 */ + { {128, 0, 129, 1}, {0, 0, 0, 1}, { 0, 0, 0, 0}, {0, 0, 0, 0} }, /* 20 */ + { {128, 0, 0, 0}, {1, 0, 0, 0}, {129, 1, 0, 0}, {1, 1, 1, 0} }, /* 21 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, {129, 0, 0, 0}, {1, 1, 0, 0} }, /* 22 */ + { {128, 1, 1, 1}, {0, 0, 1, 1}, { 0, 0, 1, 1}, {0, 0, 0, 129} }, /* 23 */ + { {128, 0, 129, 1}, {0, 0, 0, 1}, { 0, 0, 0, 1}, {0, 0, 0, 0} }, /* 24 */ + { {128, 0, 0, 0}, {1, 0, 0, 0}, {129, 0, 0, 0}, {1, 1, 0, 0} }, /* 25 */ + { {128, 1, 129, 0}, {0, 1, 1, 0}, { 0, 1, 1, 0}, {0, 1, 1, 0} }, /* 26 */ + { {128, 0, 129, 1}, {0, 1, 1, 0}, { 0, 1, 1, 0}, {1, 1, 0, 0} }, /* 27 */ + { {128, 0, 0, 1}, {0, 1, 1, 1}, {129, 1, 1, 0}, {1, 0, 0, 0} }, /* 28 */ + { {128, 0, 0, 0}, {1, 1, 1, 1}, {129, 1, 1, 1}, {0, 0, 0, 0} }, /* 29 */ + { {128, 1, 129, 1}, {0, 0, 0, 1}, { 1, 0, 0, 0}, {1, 1, 1, 0} }, /* 30 */ + { {128, 0, 129, 1}, {1, 0, 0, 1}, { 1, 0, 0, 1}, {1, 1, 0, 0} } /* 31 */ + }; + + static int aWeight3[8] = { 0, 9, 18, 27, 37, 46, 55, 64 }; + static int aWeight4[16] = { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 }; + + bcdec__bitstream_t bstream; + int mode, partition, numPartitions, i, j, partitionSet, indexBits, index, ep_i, actualBits0Mode; + int r[4], g[4], b[4]; /* wxyz */ + unsigned short* decompressed; + int* weights; + + decompressed = (unsigned short*)decompressedBlock; + + bstream.low = bcdec__getu64le(compressedBlock); + bstream.high = bcdec__getu64le((const unsigned char*)compressedBlock + 8); + + r[0] = r[1] = r[2] = r[3] = 0; + g[0] = g[1] = g[2] = g[3] = 0; + b[0] = b[1] = b[2] = b[3] = 0; + + mode = bcdec__bitstream_read_bits(&bstream, 2); + if (mode > 1) { + mode |= (bcdec__bitstream_read_bits(&bstream, 3) << 2); + } + + /* modes >= 11 (10 in my code) are using 0 one, others will read it from the bitstream */ + partition = 0; + + switch (mode) { + /* mode 1 */ + case 0b00: { + /* Partitition indices: 46 bits + Partition: 5 bits + Color Endpoints: 75 bits (10.555, 10.555, 10.555) */ + g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */ + b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */ + r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */ + g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */ + b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */ + r[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* rx[4:0] */ + g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */ + g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */ + g[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* gx[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */ + g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */ + b[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* bx[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */ + b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */ + r[2] |= bcdec__bitstream_read_bits(&bstream, 5); /* ry[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */ + r[3] |= bcdec__bitstream_read_bits(&bstream, 5); /* rz[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */ + partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */ + mode = 0; + } break; + + /* mode 2 */ + case 0b01: { + /* Partitition indices: 46 bits + Partition: 5 bits + Color Endpoints: 75 bits (7666, 7666, 7666) */ + g[2] |= bcdec__bitstream_read_bit(&bstream) << 5; /* gy[5] */ + g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */ + g[3] |= bcdec__bitstream_read_bit(&bstream) << 5; /* gz[5] */ + r[0] |= bcdec__bitstream_read_bits(&bstream, 7); /* rw[6:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */ + b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */ + g[0] |= bcdec__bitstream_read_bits(&bstream, 7); /* gw[6:0] */ + b[2] |= bcdec__bitstream_read_bit(&bstream) << 5; /* by[5] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */ + g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */ + b[0] |= bcdec__bitstream_read_bits(&bstream, 7); /* bw[6:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 5; /* bz[5] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */ + r[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* rx[5:0] */ + g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */ + g[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* gx[5:0] */ + g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */ + b[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* bx[5:0] */ + b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */ + r[2] |= bcdec__bitstream_read_bits(&bstream, 6); /* ry[5:0] */ + r[3] |= bcdec__bitstream_read_bits(&bstream, 6); /* rz[5:0] */ + partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */ + mode = 1; + } break; + + /* mode 3 */ + case 0b00010: { + /* Partitition indices: 46 bits + Partition: 5 bits + Color Endpoints: 72 bits (11.555, 11.444, 11.444) */ + r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */ + g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */ + b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */ + r[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* rx[4:0] */ + r[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* rw[10] */ + g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */ + g[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* gx[3:0] */ + g[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* gw[10] */ + b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */ + g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */ + b[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* bx[3:0] */ + b[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* bw[10] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */ + b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */ + r[2] |= bcdec__bitstream_read_bits(&bstream, 5); /* ry[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */ + r[3] |= bcdec__bitstream_read_bits(&bstream, 5); /* rz[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */ + partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */ + mode = 2; + } break; + + /* mode 4 */ + case 0b00110: { + /* Partitition indices: 46 bits + Partition: 5 bits + Color Endpoints: 72 bits (11.444, 11.555, 11.444) */ + r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */ + g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */ + b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */ + r[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* rx[3:0] */ + r[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* rw[10] */ + g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */ + g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */ + g[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* gx[4:0] */ + g[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* gw[10] */ + g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */ + b[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* bx[3:0] */ + b[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* bw[10] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */ + b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */ + r[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* ry[3:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */ + r[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* rz[3:0] */ + g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */ + partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */ + mode = 3; + } break; + + /* mode 5 */ + case 0b01010: { + /* Partitition indices: 46 bits + Partition: 5 bits + Color Endpoints: 72 bits (11.444, 11.444, 11.555) */ + r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */ + g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */ + b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */ + r[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* rx[3:0] */ + r[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* rw[10] */ + b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */ + g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */ + g[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* gx[3:0] */ + g[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* gw[10] */ + b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */ + g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */ + b[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* bx[4:0] */ + b[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* bw[10] */ + b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */ + r[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* ry[3:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */ + r[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* rz[3:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */ + partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */ + mode = 4; + } break; + + /* mode 6 */ + case 0b01110: { + /* Partitition indices: 46 bits + Partition: 5 bits + Color Endpoints: 72 bits (9555, 9555, 9555) */ + r[0] |= bcdec__bitstream_read_bits(&bstream, 9); /* rw[8:0] */ + b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */ + g[0] |= bcdec__bitstream_read_bits(&bstream, 9); /* gw[8:0] */ + g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */ + b[0] |= bcdec__bitstream_read_bits(&bstream, 9); /* bw[8:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */ + r[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* rx[4:0] */ + g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */ + g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */ + g[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* gx[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */ + g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gx[3:0] */ + b[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* bx[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */ + b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */ + r[2] |= bcdec__bitstream_read_bits(&bstream, 5); /* ry[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */ + r[3] |= bcdec__bitstream_read_bits(&bstream, 5); /* rz[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */ + partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */ + mode = 5; + } break; + + /* mode 7 */ + case 0b10010: { + /* Partitition indices: 46 bits + Partition: 5 bits + Color Endpoints: 72 bits (8666, 8555, 8555) */ + r[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* rw[7:0] */ + g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */ + b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */ + g[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* gw[7:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */ + g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */ + b[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* bw[7:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */ + r[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* rx[5:0] */ + g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */ + g[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* gx[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */ + g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */ + b[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* bx[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */ + b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */ + r[2] |= bcdec__bitstream_read_bits(&bstream, 6); /* ry[5:0] */ + r[3] |= bcdec__bitstream_read_bits(&bstream, 6); /* rz[5:0] */ + partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */ + mode = 6; + } break; + + /* mode 8 */ + case 0b10110: { + /* Partitition indices: 46 bits + Partition: 5 bits + Color Endpoints: 72 bits (8555, 8666, 8555) */ + r[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* rw[7:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */ + b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */ + g[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* gw[7:0] */ + g[2] |= bcdec__bitstream_read_bit(&bstream) << 5; /* gy[5] */ + g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */ + b[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* bw[7:0] */ + g[3] |= bcdec__bitstream_read_bit(&bstream) << 5; /* gz[5] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */ + r[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* rx[4:0] */ + g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */ + g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */ + g[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* gx[5:0] */ + g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* zx[3:0] */ + b[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* bx[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */ + b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */ + r[2] |= bcdec__bitstream_read_bits(&bstream, 5); /* ry[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */ + r[3] |= bcdec__bitstream_read_bits(&bstream, 5); /* rz[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */ + partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */ + mode = 7; + } break; + + /* mode 9 */ + case 0b11010: { + /* Partitition indices: 46 bits + Partition: 5 bits + Color Endpoints: 72 bits (8555, 8555, 8666) */ + r[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* rw[7:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */ + b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */ + g[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* gw[7:0] */ + b[2] |= bcdec__bitstream_read_bit(&bstream) << 5; /* by[5] */ + g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */ + b[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* bw[7:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 5; /* bz[5] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */ + r[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* bw[4:0] */ + g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */ + g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */ + g[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* gx[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */ + g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */ + b[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* bx[5:0] */ + b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */ + r[2] |= bcdec__bitstream_read_bits(&bstream, 5); /* ry[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */ + r[3] |= bcdec__bitstream_read_bits(&bstream, 5); /* rz[4:0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */ + partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */ + mode = 8; + } break; + + /* mode 10 */ + case 0b11110: { + /* Partitition indices: 46 bits + Partition: 5 bits + Color Endpoints: 72 bits (6666, 6666, 6666) */ + r[0] |= bcdec__bitstream_read_bits(&bstream, 6); /* rw[5:0] */ + g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */ + b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */ + b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */ + g[0] |= bcdec__bitstream_read_bits(&bstream, 6); /* gw[5:0] */ + g[2] |= bcdec__bitstream_read_bit(&bstream) << 5; /* gy[5] */ + b[2] |= bcdec__bitstream_read_bit(&bstream) << 5; /* by[5] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */ + g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */ + b[0] |= bcdec__bitstream_read_bits(&bstream, 6); /* bw[5:0] */ + g[3] |= bcdec__bitstream_read_bit(&bstream) << 5; /* gz[5] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 5; /* bz[5] */ + b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */ + r[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* rx[5:0] */ + g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */ + g[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* gx[5:0] */ + g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */ + b[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* bx[5:0] */ + b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */ + r[2] |= bcdec__bitstream_read_bits(&bstream, 6); /* ry[5:0] */ + r[3] |= bcdec__bitstream_read_bits(&bstream, 6); /* rz[5:0] */ + partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */ + mode = 9; + } break; + + /* mode 11 */ + case 0b00011: { + /* Partitition indices: 63 bits + Partition: 0 bits + Color Endpoints: 60 bits (10.10, 10.10, 10.10) */ + r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */ + g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */ + b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */ + r[1] |= bcdec__bitstream_read_bits(&bstream, 10); /* rx[9:0] */ + g[1] |= bcdec__bitstream_read_bits(&bstream, 10); /* gx[9:0] */ + b[1] |= bcdec__bitstream_read_bits(&bstream, 10); /* bx[9:0] */ + mode = 10; + } break; + + /* mode 12 */ + case 0b00111: { + /* Partitition indices: 63 bits + Partition: 0 bits + Color Endpoints: 60 bits (11.9, 11.9, 11.9) */ + r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */ + g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */ + b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */ + r[1] |= bcdec__bitstream_read_bits(&bstream, 9); /* rx[8:0] */ + r[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* rw[10] */ + g[1] |= bcdec__bitstream_read_bits(&bstream, 9); /* gx[8:0] */ + g[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* gw[10] */ + b[1] |= bcdec__bitstream_read_bits(&bstream, 9); /* bx[8:0] */ + b[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* bw[10] */ + mode = 11; + } break; + + /* mode 13 */ + case 0b01011: { + /* Partitition indices: 63 bits + Partition: 0 bits + Color Endpoints: 60 bits (12.8, 12.8, 12.8) */ + r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */ + g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */ + b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */ + r[1] |= bcdec__bitstream_read_bits(&bstream, 8); /* rx[7:0] */ + r[0] |= bcdec__bitstream_read_bits_r(&bstream, 2) << 10;/* rx[10:11] */ + g[1] |= bcdec__bitstream_read_bits(&bstream, 8); /* gx[7:0] */ + g[0] |= bcdec__bitstream_read_bits_r(&bstream, 2) << 10;/* gx[10:11] */ + b[1] |= bcdec__bitstream_read_bits(&bstream, 8); /* bx[7:0] */ + b[0] |= bcdec__bitstream_read_bits_r(&bstream, 2) << 10;/* bx[10:11] */ + mode = 12; + } break; + + /* mode 14 */ + case 0b01111: { + /* Partitition indices: 63 bits + Partition: 0 bits + Color Endpoints: 60 bits (16.4, 16.4, 16.4) */ + r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */ + g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */ + b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */ + r[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* rx[3:0] */ + r[0] |= bcdec__bitstream_read_bits_r(&bstream, 6) << 10;/* rw[10:15] */ + g[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* gx[3:0] */ + g[0] |= bcdec__bitstream_read_bits_r(&bstream, 6) << 10;/* gw[10:15] */ + b[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* bx[3:0] */ + b[0] |= bcdec__bitstream_read_bits_r(&bstream, 6) << 10;/* bw[10:15] */ + mode = 13; + } break; + + default: { + /* Modes 10011, 10111, 11011, and 11111 (not shown) are reserved. + Do not use these in your encoder. If the hardware is passed blocks + with one of these modes specified, the resulting decompressed block + must contain all zeroes in all channels except for the alpha channel. */ + for (i = 0; i < 4; ++i) { + for (j = 0; j < 4; ++j) { + decompressed[j * 3 + 0] = 0; + decompressed[j * 3 + 1] = 0; + decompressed[j * 3 + 2] = 0; + } + decompressed += destinationPitch; + } + + return; + } + } + + numPartitions = (mode >= 10) ? 0 : 1; + + actualBits0Mode = actual_bits_count[0][mode]; + if (isSigned) { + r[0] = bcdec__extend_sign(r[0], actualBits0Mode); + g[0] = bcdec__extend_sign(g[0], actualBits0Mode); + b[0] = bcdec__extend_sign(b[0], actualBits0Mode); + } + + /* Mode 11 (like Mode 10) does not use delta compression, + and instead stores both color endpoints explicitly. */ + if ((mode != 9 && mode != 10) || isSigned) { + for (i = 1; i < (numPartitions + 1) * 2; ++i) { + r[i] = bcdec__extend_sign(r[i], actual_bits_count[1][mode]); + g[i] = bcdec__extend_sign(g[i], actual_bits_count[2][mode]); + b[i] = bcdec__extend_sign(b[i], actual_bits_count[3][mode]); + } + } + + if (mode != 9 && mode != 10) { + for (i = 1; i < (numPartitions + 1) * 2; ++i) { + r[i] = bcdec__transform_inverse(r[i], r[0], actualBits0Mode, isSigned); + g[i] = bcdec__transform_inverse(g[i], g[0], actualBits0Mode, isSigned); + b[i] = bcdec__transform_inverse(b[i], b[0], actualBits0Mode, isSigned); + } + } + + for (i = 0; i < (numPartitions + 1) * 2; ++i) { + r[i] = bcdec__unquantize(r[i], actualBits0Mode, isSigned); + g[i] = bcdec__unquantize(g[i], actualBits0Mode, isSigned); + b[i] = bcdec__unquantize(b[i], actualBits0Mode, isSigned); + } + + weights = (mode >= 10) ? aWeight4 : aWeight3; + for (i = 0; i < 4; ++i) { + for (j = 0; j < 4; ++j) { + partitionSet = (mode >= 10) ? ((i|j) ? 0 : 128) : partition_sets[partition][i][j]; + + indexBits = (mode >= 10) ? 4 : 3; + /* fix-up index is specified with one less bit */ + /* The fix-up index for subset 0 is always index 0 */ + if (partitionSet & 0x80) { + indexBits--; + } + partitionSet &= 0x01; + + index = bcdec__bitstream_read_bits(&bstream, indexBits); + + ep_i = partitionSet * 2; + decompressed[j * 3 + 0] = bcdec__finish_unquantize( + bcdec__interpolate(r[ep_i], r[ep_i+1], weights, index), isSigned); + decompressed[j * 3 + 1] = bcdec__finish_unquantize( + bcdec__interpolate(g[ep_i], g[ep_i+1], weights, index), isSigned); + decompressed[j * 3 + 2] = bcdec__finish_unquantize( + bcdec__interpolate(b[ep_i], b[ep_i+1], weights, index), isSigned); + } + + decompressed += destinationPitch; + } +} + +BCDECDEF void bcdec_bc6h_float(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned) { + unsigned short block[16*3]; + float* decompressed; + const unsigned short* b; + int i, j; + + bcdec_bc6h_half(compressedBlock, block, 4*3, isSigned); + b = block; + decompressed = (float*)decompressedBlock; + for (i = 0; i < 4; ++i) { + for (j = 0; j < 4; ++j) { + decompressed[j * 3 + 0] = bcdec__half_to_float_quick(*b++); + decompressed[j * 3 + 1] = bcdec__half_to_float_quick(*b++); + decompressed[j * 3 + 2] = bcdec__half_to_float_quick(*b++); + } + decompressed += destinationPitch; + } +} + +static void bcdec__swap_values(int* a, int* b) { + a[0] ^= b[0], b[0] ^= a[0], a[0] ^= b[0]; +} + +BCDECDEF void bcdec_bc7(const void* compressedBlock, void* decompressedBlock, int destinationPitch) { + static char actual_bits_count[2][8] = { + { 4, 6, 5, 7, 5, 7, 7, 5 }, /* RGBA */ + { 0, 0, 0, 0, 6, 8, 7, 5 }, /* Alpha */ + }; + + /* There are 64 possible partition sets for a two-region tile. + Each 4x4 block represents a single shape. + Here also every fix-up index has MSB bit set. */ + static unsigned char partition_sets[2][64][4][4] = { + { /* Partition table for 2-subset BPTC */ + { {128, 0, 1, 1}, {0, 0, 1, 1}, { 0, 0, 1, 1}, {0, 0, 1, 129} }, /* 0 */ + { {128, 0, 0, 1}, {0, 0, 0, 1}, { 0, 0, 0, 1}, {0, 0, 0, 129} }, /* 1 */ + { {128, 1, 1, 1}, {0, 1, 1, 1}, { 0, 1, 1, 1}, {0, 1, 1, 129} }, /* 2 */ + { {128, 0, 0, 1}, {0, 0, 1, 1}, { 0, 0, 1, 1}, {0, 1, 1, 129} }, /* 3 */ + { {128, 0, 0, 0}, {0, 0, 0, 1}, { 0, 0, 0, 1}, {0, 0, 1, 129} }, /* 4 */ + { {128, 0, 1, 1}, {0, 1, 1, 1}, { 0, 1, 1, 1}, {1, 1, 1, 129} }, /* 5 */ + { {128, 0, 0, 1}, {0, 0, 1, 1}, { 0, 1, 1, 1}, {1, 1, 1, 129} }, /* 6 */ + { {128, 0, 0, 0}, {0, 0, 0, 1}, { 0, 0, 1, 1}, {0, 1, 1, 129} }, /* 7 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 1}, {0, 0, 1, 129} }, /* 8 */ + { {128, 0, 1, 1}, {0, 1, 1, 1}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 9 */ + { {128, 0, 0, 0}, {0, 0, 0, 1}, { 0, 1, 1, 1}, {1, 1, 1, 129} }, /* 10 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 1}, {0, 1, 1, 129} }, /* 11 */ + { {128, 0, 0, 1}, {0, 1, 1, 1}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 12 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 13 */ + { {128, 0, 0, 0}, {1, 1, 1, 1}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 14 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 0}, {1, 1, 1, 129} }, /* 15 */ + { {128, 0, 0, 0}, {1, 0, 0, 0}, { 1, 1, 1, 0}, {1, 1, 1, 129} }, /* 16 */ + { {128, 1, 129, 1}, {0, 0, 0, 1}, { 0, 0, 0, 0}, {0, 0, 0, 0} }, /* 17 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, {129, 0, 0, 0}, {1, 1, 1, 0} }, /* 18 */ + { {128, 1, 129, 1}, {0, 0, 1, 1}, { 0, 0, 0, 1}, {0, 0, 0, 0} }, /* 19 */ + { {128, 0, 129, 1}, {0, 0, 0, 1}, { 0, 0, 0, 0}, {0, 0, 0, 0} }, /* 20 */ + { {128, 0, 0, 0}, {1, 0, 0, 0}, {129, 1, 0, 0}, {1, 1, 1, 0} }, /* 21 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, {129, 0, 0, 0}, {1, 1, 0, 0} }, /* 22 */ + { {128, 1, 1, 1}, {0, 0, 1, 1}, { 0, 0, 1, 1}, {0, 0, 0, 129} }, /* 23 */ + { {128, 0, 129, 1}, {0, 0, 0, 1}, { 0, 0, 0, 1}, {0, 0, 0, 0} }, /* 24 */ + { {128, 0, 0, 0}, {1, 0, 0, 0}, {129, 0, 0, 0}, {1, 1, 0, 0} }, /* 25 */ + { {128, 1, 129, 0}, {0, 1, 1, 0}, { 0, 1, 1, 0}, {0, 1, 1, 0} }, /* 26 */ + { {128, 0, 129, 1}, {0, 1, 1, 0}, { 0, 1, 1, 0}, {1, 1, 0, 0} }, /* 27 */ + { {128, 0, 0, 1}, {0, 1, 1, 1}, {129, 1, 1, 0}, {1, 0, 0, 0} }, /* 28 */ + { {128, 0, 0, 0}, {1, 1, 1, 1}, {129, 1, 1, 1}, {0, 0, 0, 0} }, /* 29 */ + { {128, 1, 129, 1}, {0, 0, 0, 1}, { 1, 0, 0, 0}, {1, 1, 1, 0} }, /* 30 */ + { {128, 0, 129, 1}, {1, 0, 0, 1}, { 1, 0, 0, 1}, {1, 1, 0, 0} }, /* 31 */ + { {128, 1, 0, 1}, {0, 1, 0, 1}, { 0, 1, 0, 1}, {0, 1, 0, 129} }, /* 32 */ + { {128, 0, 0, 0}, {1, 1, 1, 1}, { 0, 0, 0, 0}, {1, 1, 1, 129} }, /* 33 */ + { {128, 1, 0, 1}, {1, 0, 129, 0}, { 0, 1, 0, 1}, {1, 0, 1, 0} }, /* 34 */ + { {128, 0, 1, 1}, {0, 0, 1, 1}, {129, 1, 0, 0}, {1, 1, 0, 0} }, /* 35 */ + { {128, 0, 129, 1}, {1, 1, 0, 0}, { 0, 0, 1, 1}, {1, 1, 0, 0} }, /* 36 */ + { {128, 1, 0, 1}, {0, 1, 0, 1}, {129, 0, 1, 0}, {1, 0, 1, 0} }, /* 37 */ + { {128, 1, 1, 0}, {1, 0, 0, 1}, { 0, 1, 1, 0}, {1, 0, 0, 129} }, /* 38 */ + { {128, 1, 0, 1}, {1, 0, 1, 0}, { 1, 0, 1, 0}, {0, 1, 0, 129} }, /* 39 */ + { {128, 1, 129, 1}, {0, 0, 1, 1}, { 1, 1, 0, 0}, {1, 1, 1, 0} }, /* 40 */ + { {128, 0, 0, 1}, {0, 0, 1, 1}, {129, 1, 0, 0}, {1, 0, 0, 0} }, /* 41 */ + { {128, 0, 129, 1}, {0, 0, 1, 0}, { 0, 1, 0, 0}, {1, 1, 0, 0} }, /* 42 */ + { {128, 0, 129, 1}, {1, 0, 1, 1}, { 1, 1, 0, 1}, {1, 1, 0, 0} }, /* 43 */ + { {128, 1, 129, 0}, {1, 0, 0, 1}, { 1, 0, 0, 1}, {0, 1, 1, 0} }, /* 44 */ + { {128, 0, 1, 1}, {1, 1, 0, 0}, { 1, 1, 0, 0}, {0, 0, 1, 129} }, /* 45 */ + { {128, 1, 1, 0}, {0, 1, 1, 0}, { 1, 0, 0, 1}, {1, 0, 0, 129} }, /* 46 */ + { {128, 0, 0, 0}, {0, 1, 129, 0}, { 0, 1, 1, 0}, {0, 0, 0, 0} }, /* 47 */ + { {128, 1, 0, 0}, {1, 1, 129, 0}, { 0, 1, 0, 0}, {0, 0, 0, 0} }, /* 48 */ + { {128, 0, 129, 0}, {0, 1, 1, 1}, { 0, 0, 1, 0}, {0, 0, 0, 0} }, /* 49 */ + { {128, 0, 0, 0}, {0, 0, 129, 0}, { 0, 1, 1, 1}, {0, 0, 1, 0} }, /* 50 */ + { {128, 0, 0, 0}, {0, 1, 0, 0}, {129, 1, 1, 0}, {0, 1, 0, 0} }, /* 51 */ + { {128, 1, 1, 0}, {1, 1, 0, 0}, { 1, 0, 0, 1}, {0, 0, 1, 129} }, /* 52 */ + { {128, 0, 1, 1}, {0, 1, 1, 0}, { 1, 1, 0, 0}, {1, 0, 0, 129} }, /* 53 */ + { {128, 1, 129, 0}, {0, 0, 1, 1}, { 1, 0, 0, 1}, {1, 1, 0, 0} }, /* 54 */ + { {128, 0, 129, 1}, {1, 0, 0, 1}, { 1, 1, 0, 0}, {0, 1, 1, 0} }, /* 55 */ + { {128, 1, 1, 0}, {1, 1, 0, 0}, { 1, 1, 0, 0}, {1, 0, 0, 129} }, /* 56 */ + { {128, 1, 1, 0}, {0, 0, 1, 1}, { 0, 0, 1, 1}, {1, 0, 0, 129} }, /* 57 */ + { {128, 1, 1, 1}, {1, 1, 1, 0}, { 1, 0, 0, 0}, {0, 0, 0, 129} }, /* 58 */ + { {128, 0, 0, 1}, {1, 0, 0, 0}, { 1, 1, 1, 0}, {0, 1, 1, 129} }, /* 59 */ + { {128, 0, 0, 0}, {1, 1, 1, 1}, { 0, 0, 1, 1}, {0, 0, 1, 129} }, /* 60 */ + { {128, 0, 129, 1}, {0, 0, 1, 1}, { 1, 1, 1, 1}, {0, 0, 0, 0} }, /* 61 */ + { {128, 0, 129, 0}, {0, 0, 1, 0}, { 1, 1, 1, 0}, {1, 1, 1, 0} }, /* 62 */ + { {128, 1, 0, 0}, {0, 1, 0, 0}, { 0, 1, 1, 1}, {0, 1, 1, 129} } /* 63 */ + }, + { /* Partition table for 3-subset BPTC */ + { {128, 0, 1, 129}, {0, 0, 1, 1}, { 0, 2, 2, 1}, { 2, 2, 2, 130} }, /* 0 */ + { {128, 0, 0, 129}, {0, 0, 1, 1}, {130, 2, 1, 1}, { 2, 2, 2, 1} }, /* 1 */ + { {128, 0, 0, 0}, {2, 0, 0, 1}, {130, 2, 1, 1}, { 2, 2, 1, 129} }, /* 2 */ + { {128, 2, 2, 130}, {0, 0, 2, 2}, { 0, 0, 1, 1}, { 0, 1, 1, 129} }, /* 3 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, {129, 1, 2, 2}, { 1, 1, 2, 130} }, /* 4 */ + { {128, 0, 1, 129}, {0, 0, 1, 1}, { 0, 0, 2, 2}, { 0, 0, 2, 130} }, /* 5 */ + { {128, 0, 2, 130}, {0, 0, 2, 2}, { 1, 1, 1, 1}, { 1, 1, 1, 129} }, /* 6 */ + { {128, 0, 1, 1}, {0, 0, 1, 1}, {130, 2, 1, 1}, { 2, 2, 1, 129} }, /* 7 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, {129, 1, 1, 1}, { 2, 2, 2, 130} }, /* 8 */ + { {128, 0, 0, 0}, {1, 1, 1, 1}, {129, 1, 1, 1}, { 2, 2, 2, 130} }, /* 9 */ + { {128, 0, 0, 0}, {1, 1, 129, 1}, { 2, 2, 2, 2}, { 2, 2, 2, 130} }, /* 10 */ + { {128, 0, 1, 2}, {0, 0, 129, 2}, { 0, 0, 1, 2}, { 0, 0, 1, 130} }, /* 11 */ + { {128, 1, 1, 2}, {0, 1, 129, 2}, { 0, 1, 1, 2}, { 0, 1, 1, 130} }, /* 12 */ + { {128, 1, 2, 2}, {0, 129, 2, 2}, { 0, 1, 2, 2}, { 0, 1, 2, 130} }, /* 13 */ + { {128, 0, 1, 129}, {0, 1, 1, 2}, { 1, 1, 2, 2}, { 1, 2, 2, 130} }, /* 14 */ + { {128, 0, 1, 129}, {2, 0, 0, 1}, {130, 2, 0, 0}, { 2, 2, 2, 0} }, /* 15 */ + { {128, 0, 0, 129}, {0, 0, 1, 1}, { 0, 1, 1, 2}, { 1, 1, 2, 130} }, /* 16 */ + { {128, 1, 1, 129}, {0, 0, 1, 1}, {130, 0, 0, 1}, { 2, 2, 0, 0} }, /* 17 */ + { {128, 0, 0, 0}, {1, 1, 2, 2}, {129, 1, 2, 2}, { 1, 1, 2, 130} }, /* 18 */ + { {128, 0, 2, 130}, {0, 0, 2, 2}, { 0, 0, 2, 2}, { 1, 1, 1, 129} }, /* 19 */ + { {128, 1, 1, 129}, {0, 1, 1, 1}, { 0, 2, 2, 2}, { 0, 2, 2, 130} }, /* 20 */ + { {128, 0, 0, 129}, {0, 0, 0, 1}, {130, 2, 2, 1}, { 2, 2, 2, 1} }, /* 21 */ + { {128, 0, 0, 0}, {0, 0, 129, 1}, { 0, 1, 2, 2}, { 0, 1, 2, 130} }, /* 22 */ + { {128, 0, 0, 0}, {1, 1, 0, 0}, {130, 2, 129, 0}, { 2, 2, 1, 0} }, /* 23 */ + { {128, 1, 2, 130}, {0, 129, 2, 2}, { 0, 0, 1, 1}, { 0, 0, 0, 0} }, /* 24 */ + { {128, 0, 1, 2}, {0, 0, 1, 2}, {129, 1, 2, 2}, { 2, 2, 2, 130} }, /* 25 */ + { {128, 1, 1, 0}, {1, 2, 130, 1}, {129, 2, 2, 1}, { 0, 1, 1, 0} }, /* 26 */ + { {128, 0, 0, 0}, {0, 1, 129, 0}, { 1, 2, 130, 1}, { 1, 2, 2, 1} }, /* 27 */ + { {128, 0, 2, 2}, {1, 1, 0, 2}, {129, 1, 0, 2}, { 0, 0, 2, 130} }, /* 28 */ + { {128, 1, 1, 0}, {0, 129, 1, 0}, { 2, 0, 0, 2}, { 2, 2, 2, 130} }, /* 29 */ + { {128, 0, 1, 1}, {0, 1, 2, 2}, { 0, 1, 130, 2}, { 0, 0, 1, 129} }, /* 30 */ + { {128, 0, 0, 0}, {2, 0, 0, 0}, {130, 2, 1, 1}, { 2, 2, 2, 129} }, /* 31 */ + { {128, 0, 0, 0}, {0, 0, 0, 2}, {129, 1, 2, 2}, { 1, 2, 2, 130} }, /* 32 */ + { {128, 2, 2, 130}, {0, 0, 2, 2}, { 0, 0, 1, 2}, { 0, 0, 1, 129} }, /* 33 */ + { {128, 0, 1, 129}, {0, 0, 1, 2}, { 0, 0, 2, 2}, { 0, 2, 2, 130} }, /* 34 */ + { {128, 1, 2, 0}, {0, 129, 2, 0}, { 0, 1, 130, 0}, { 0, 1, 2, 0} }, /* 35 */ + { {128, 0, 0, 0}, {1, 1, 129, 1}, { 2, 2, 130, 2}, { 0, 0, 0, 0} }, /* 36 */ + { {128, 1, 2, 0}, {1, 2, 0, 1}, {130, 0, 129, 2}, { 0, 1, 2, 0} }, /* 37 */ + { {128, 1, 2, 0}, {2, 0, 1, 2}, {129, 130, 0, 1}, { 0, 1, 2, 0} }, /* 38 */ + { {128, 0, 1, 1}, {2, 2, 0, 0}, { 1, 1, 130, 2}, { 0, 0, 1, 129} }, /* 39 */ + { {128, 0, 1, 1}, {1, 1, 130, 2}, { 2, 2, 0, 0}, { 0, 0, 1, 129} }, /* 40 */ + { {128, 1, 0, 129}, {0, 1, 0, 1}, { 2, 2, 2, 2}, { 2, 2, 2, 130} }, /* 41 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, {130, 1, 2, 1}, { 2, 1, 2, 129} }, /* 42 */ + { {128, 0, 2, 2}, {1, 129, 2, 2}, { 0, 0, 2, 2}, { 1, 1, 2, 130} }, /* 43 */ + { {128, 0, 2, 130}, {0, 0, 1, 1}, { 0, 0, 2, 2}, { 0, 0, 1, 129} }, /* 44 */ + { {128, 2, 2, 0}, {1, 2, 130, 1}, { 0, 2, 2, 0}, { 1, 2, 2, 129} }, /* 45 */ + { {128, 1, 0, 1}, {2, 2, 130, 2}, { 2, 2, 2, 2}, { 0, 1, 0, 129} }, /* 46 */ + { {128, 0, 0, 0}, {2, 1, 2, 1}, {130, 1, 2, 1}, { 2, 1, 2, 129} }, /* 47 */ + { {128, 1, 0, 129}, {0, 1, 0, 1}, { 0, 1, 0, 1}, { 2, 2, 2, 130} }, /* 48 */ + { {128, 2, 2, 130}, {0, 1, 1, 1}, { 0, 2, 2, 2}, { 0, 1, 1, 129} }, /* 49 */ + { {128, 0, 0, 2}, {1, 129, 1, 2}, { 0, 0, 0, 2}, { 1, 1, 1, 130} }, /* 50 */ + { {128, 0, 0, 0}, {2, 129, 1, 2}, { 2, 1, 1, 2}, { 2, 1, 1, 130} }, /* 51 */ + { {128, 2, 2, 2}, {0, 129, 1, 1}, { 0, 1, 1, 1}, { 0, 2, 2, 130} }, /* 52 */ + { {128, 0, 0, 2}, {1, 1, 1, 2}, {129, 1, 1, 2}, { 0, 0, 0, 130} }, /* 53 */ + { {128, 1, 1, 0}, {0, 129, 1, 0}, { 0, 1, 1, 0}, { 2, 2, 2, 130} }, /* 54 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, { 2, 1, 129, 2}, { 2, 1, 1, 130} }, /* 55 */ + { {128, 1, 1, 0}, {0, 129, 1, 0}, { 2, 2, 2, 2}, { 2, 2, 2, 130} }, /* 56 */ + { {128, 0, 2, 2}, {0, 0, 1, 1}, { 0, 0, 129, 1}, { 0, 0, 2, 130} }, /* 57 */ + { {128, 0, 2, 2}, {1, 1, 2, 2}, {129, 1, 2, 2}, { 0, 0, 2, 130} }, /* 58 */ + { {128, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 0}, { 2, 129, 1, 130} }, /* 59 */ + { {128, 0, 0, 130}, {0, 0, 0, 1}, { 0, 0, 0, 2}, { 0, 0, 0, 129} }, /* 60 */ + { {128, 2, 2, 2}, {1, 2, 2, 2}, { 0, 2, 2, 2}, {129, 2, 2, 130} }, /* 61 */ + { {128, 1, 0, 129}, {2, 2, 2, 2}, { 2, 2, 2, 2}, { 2, 2, 2, 130} }, /* 62 */ + { {128, 1, 1, 129}, {2, 0, 1, 1}, {130, 2, 0, 1}, { 2, 2, 2, 0} } /* 63 */ + } + }; + + static int aWeight2[] = { 0, 21, 43, 64 }; + static int aWeight3[] = { 0, 9, 18, 27, 37, 46, 55, 64 }; + static int aWeight4[] = { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 }; + + static unsigned char sModeHasPBits = 0b11001011; + + bcdec__bitstream_t bstream; + int mode, partition, numPartitions, numEndpoints, i, j, k, rotation, partitionSet; + int indexSelectionBit, indexBits, indexBits2, index, index2; + int endpoints[6][4]; + char indices[4][4]; + int r, g, b, a; + int* weights, * weights2; + unsigned char* decompressed; + + decompressed = (unsigned char*)decompressedBlock; + + bstream.low = bcdec__getu64le(compressedBlock); + bstream.high = bcdec__getu64le((const unsigned char*)compressedBlock + 8); + + for (mode = 0; mode < 8 && (0 == bcdec__bitstream_read_bit(&bstream)); ++mode); + + /* unexpected mode, clear the block (transparent black) */ + if (mode >= 8) { + for (i = 0; i < 4; ++i) { + for (j = 0; j < 4; ++j) { + decompressed[j * 4 + 0] = 0; + decompressed[j * 4 + 1] = 0; + decompressed[j * 4 + 2] = 0; + decompressed[j * 4 + 3] = 0; + } + decompressed += destinationPitch; + } + + return; + } + + partition = 0; + numPartitions = 1; + rotation = 0; + indexSelectionBit = 0; + + if (mode == 0 || mode == 1 || mode == 2 || mode == 3 || mode == 7) { + numPartitions = (mode == 0 || mode == 2) ? 3 : 2; + partition = bcdec__bitstream_read_bits(&bstream, (mode == 0) ? 4 : 6); + } + + numEndpoints = numPartitions * 2; + + if (mode == 4 || mode == 5) { + rotation = bcdec__bitstream_read_bits(&bstream, 2); + + if (mode == 4) { + indexSelectionBit = bcdec__bitstream_read_bit(&bstream); + } + } + + /* Extract endpoints */ + /* RGB */ + for (i = 0; i < 3; ++i) { + for (j = 0; j < numEndpoints; ++j) { + endpoints[j][i] = bcdec__bitstream_read_bits(&bstream, actual_bits_count[0][mode]); + } + } + /* Alpha (if any) */ + if (actual_bits_count[1][mode] > 0) { + for (j = 0; j < numEndpoints; ++j) { + endpoints[j][3] = bcdec__bitstream_read_bits(&bstream, actual_bits_count[1][mode]); + } + } + + /* Fully decode endpoints */ + /* First handle modes that have P-bits */ + if (mode == 0 || mode == 1 || mode == 3 || mode == 6 || mode == 7) { + for (i = 0; i < numEndpoints; ++i) { + /* component-wise left-shift */ + for (j = 0; j < 4; ++j) { + endpoints[i][j] <<= 1; + } + } + + /* if P-bit is shared */ + if (mode == 1) { + i = bcdec__bitstream_read_bit(&bstream); + j = bcdec__bitstream_read_bit(&bstream); + + /* rgb component-wise insert pbits */ + for (k = 0; k < 3; ++k) { + endpoints[0][k] |= i; + endpoints[1][k] |= i; + endpoints[2][k] |= j; + endpoints[3][k] |= j; + } + } else if (sModeHasPBits & (1 << mode)) { + /* unique P-bit per endpoint */ + for (i = 0; i < numEndpoints; ++i) { + j = bcdec__bitstream_read_bit(&bstream); + for (k = 0; k < 4; ++k) { + endpoints[i][k] |= j; + } + } + } + } + + for (i = 0; i < numEndpoints; ++i) { + /* get color components precision including pbit */ + j = actual_bits_count[0][mode] + ((sModeHasPBits >> mode) & 1); + + for (k = 0; k < 3; ++k) { + /* left shift endpoint components so that their MSB lies in bit 7 */ + endpoints[i][k] = endpoints[i][k] << (8 - j); + /* Replicate each component's MSB into the LSBs revealed by the left-shift operation above */ + endpoints[i][k] = endpoints[i][k] | (endpoints[i][k] >> j); + } + + /* get alpha component precision including pbit */ + j = actual_bits_count[1][mode] + ((sModeHasPBits >> mode) & 1); + + /* left shift endpoint components so that their MSB lies in bit 7 */ + endpoints[i][3] = endpoints[i][3] << (8 - j); + /* Replicate each component's MSB into the LSBs revealed by the left-shift operation above */ + endpoints[i][3] = endpoints[i][3] | (endpoints[i][3] >> j); + } + + /* If this mode does not explicitly define the alpha component */ + /* set alpha equal to 1.0 */ + if (!actual_bits_count[1][mode]) { + for (j = 0; j < numEndpoints; ++j) { + endpoints[j][3] = 0xFF; + } + } + + /* Determine weights tables */ + indexBits = (mode == 0 || mode == 1) ? 3 : ((mode == 6) ? 4 : 2); + indexBits2 = (mode == 4) ? 3 : ((mode == 5) ? 2 : 0); + weights = (indexBits == 2) ? aWeight2 : ((indexBits == 3) ? aWeight3 : aWeight4); + weights2 = (indexBits2 == 2) ? aWeight2 : aWeight3; + + /* Quite inconvenient that indices aren't interleaved so we have to make 2 passes here */ + /* Pass #1: collecting color indices */ + for (i = 0; i < 4; ++i) { + for (j = 0; j < 4; ++j) { + partitionSet = (numPartitions == 1) ? ((i | j) ? 0 : 128) : partition_sets[numPartitions - 2][partition][i][j]; + + indexBits = (mode == 0 || mode == 1) ? 3 : ((mode == 6) ? 4 : 2); + /* fix-up index is specified with one less bit */ + /* The fix-up index for subset 0 is always index 0 */ + if (partitionSet & 0x80) { + indexBits--; + } + + indices[i][j] = bcdec__bitstream_read_bits(&bstream, indexBits); + } + } + + /* Pass #2: reading alpha indices (if any) and interpolating & rotating */ + for (i = 0; i < 4; ++i) { + for (j = 0; j < 4; ++j) { + partitionSet = (numPartitions == 1) ? ((i|j) ? 0 : 128) : partition_sets[numPartitions - 2][partition][i][j]; + partitionSet &= 0x03; + + index = indices[i][j]; + + if (!indexBits2) { + r = bcdec__interpolate(endpoints[partitionSet * 2][0], endpoints[partitionSet * 2 + 1][0], weights, index); + g = bcdec__interpolate(endpoints[partitionSet * 2][1], endpoints[partitionSet * 2 + 1][1], weights, index); + b = bcdec__interpolate(endpoints[partitionSet * 2][2], endpoints[partitionSet * 2 + 1][2], weights, index); + a = bcdec__interpolate(endpoints[partitionSet * 2][3], endpoints[partitionSet * 2 + 1][3], weights, index); + } else { + index2 = bcdec__bitstream_read_bits(&bstream, (i|j) ? indexBits2 : (indexBits2 - 1)); + /* The index value for interpolating color comes from the secondary index bits for the texel + if the mode has an index selection bit and its value is one, and from the primary index bits otherwise. + The alpha index comes from the secondary index bits if the block has a secondary index and + the block either doesn’t have an index selection bit or that bit is zero, and from the primary index bits otherwise. */ + if (!indexSelectionBit) { + r = bcdec__interpolate(endpoints[partitionSet * 2][0], endpoints[partitionSet * 2 + 1][0], weights, index); + g = bcdec__interpolate(endpoints[partitionSet * 2][1], endpoints[partitionSet * 2 + 1][1], weights, index); + b = bcdec__interpolate(endpoints[partitionSet * 2][2], endpoints[partitionSet * 2 + 1][2], weights, index); + a = bcdec__interpolate(endpoints[partitionSet * 2][3], endpoints[partitionSet * 2 + 1][3], weights2, index2); + } else { + r = bcdec__interpolate(endpoints[partitionSet * 2][0], endpoints[partitionSet * 2 + 1][0], weights2, index2); + g = bcdec__interpolate(endpoints[partitionSet * 2][1], endpoints[partitionSet * 2 + 1][1], weights2, index2); + b = bcdec__interpolate(endpoints[partitionSet * 2][2], endpoints[partitionSet * 2 + 1][2], weights2, index2); + a = bcdec__interpolate(endpoints[partitionSet * 2][3], endpoints[partitionSet * 2 + 1][3], weights, index); + } + } + + switch (rotation) { + case 1: { /* 01 – Block format is Scalar(R) Vector(AGB) - swap A and R */ + bcdec__swap_values(&a, &r); + } break; + case 2: { /* 10 – Block format is Scalar(G) Vector(RAB) - swap A and G */ + bcdec__swap_values(&a, &g); + } break; + case 3: { /* 11 - Block format is Scalar(B) Vector(RGA) - swap A and B */ + bcdec__swap_values(&a, &b); + } break; + } + + decompressed[j * 4 + 0] = r; + decompressed[j * 4 + 1] = g; + decompressed[j * 4 + 2] = b; + decompressed[j * 4 + 3] = a; + } + + decompressed += destinationPitch; + } +} + +#endif /* BCDEC_IMPLEMENTATION */ + +/* LICENSE: + +This software is available under 2 licenses -- choose whichever you prefer. + +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License + +Copyright (c) 2022 Sergii Kudlai + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +------------------------------------------------------------------------------ +ALTERNATIVE B - The Unlicense + +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to + +*/ diff --git a/dm/tools/tests/test_ios_atomic_export.py b/dm/tools/tests/test_ios_atomic_export.py new file mode 100644 index 0000000000..5f2d8f0366 --- /dev/null +++ b/dm/tools/tests/test_ios_atomic_export.py @@ -0,0 +1,55 @@ +"""Legacy image atomics must be guarded before evaluating their device access.""" +import hashlib +import importlib.util +import json +from pathlib import Path +import tempfile +import unittest + +TOOL = Path(__file__).resolve().parents[2] / "ios/tools/export_legacy_atomics.py" +SPEC = importlib.util.spec_from_file_location("atomic_export", TOOL) +assert SPEC is not None and SPEC.loader is not None +EXPORT = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(EXPORT) +SOURCE = """#define spvImage2DAtomicCoord(tc, tex) ((tex.get_width() * (tc).y) + (tc).x) +uint v = atomic_fetch_max_explicit((device atomic_uint*)&p[spvImage2DAtomicCoord(int3(f(x, y), y, layer), image)], uint(value), memory_order_relaxed); +uint unrelated = atomic_fetch_add_explicit(counter, 1u, memory_order_relaxed); +""" + + +class AtomicExportTests(unittest.TestCase): + def test_guards_nested_coordinates_and_preserves_unrelated_atomics(self): + result, count = EXPORT.transform(SOURCE) + self.assertEqual(count, 1) + self.assertIn("(dm_image_atomic_valid(int3(f(x, y), y, layer), image) ? (atomic_fetch_max_explicit", result) + self.assertIn("memory_order_relaxed)) : 0)", result) + self.assertIn("atomic_fetch_add_explicit(counter, 1u, memory_order_relaxed)", result) + self.assertIn("uint(c.z) < t.get_array_size()", result) + self.assertIn("ulong(uint(c.z)) * t.get_height()", result) + + def test_rejects_unguarded_or_malformed_coordinate_uses(self): + for bad in (SOURCE + "uint k = spvImage2DAtomicCoord(c, image);\n", + SOURCE.replace("int3(f(x, y), y, layer), image", "c, image, extra"), + SOURCE.replace("#define spvImage2DAtomicCoord", "#define unrelated")): + with self.subTest(source=bad), self.assertRaises(ValueError): + EXPORT.transform(bad) + + def test_manifest_binds_variant_to_original_source_library_and_chain(self): + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + cache, libs, out = root / "cache/b-abc", root / "ios/b-abc", root / "export" + cache.mkdir(parents=True); libs.mkdir(parents=True) + (cache / "a_b.metal").write_text(SOURCE) + (libs / "a_b.metallib").write_bytes(b"MTLB-original") + (libs / "a_b.json").write_text(json.dumps({"chain": "abc", "key": "a_b", "bindings": [{"msl_name": "image_atomic"}]})) + self.assertEqual(EXPORT.export(cache, libs, out, "abc"), 1) + record = json.loads((out / "a_b.json").read_text()) + self.assertEqual(record["original_source_sha256"], hashlib.sha256(SOURCE.encode()).hexdigest()) + self.assertEqual(record["source_sha256"], hashlib.sha256((out / "a_b.metal").read_bytes()).hexdigest()) + self.assertEqual(record["library_sha256"], hashlib.sha256(b"MTLB-original").hexdigest()) + with self.assertRaises(ValueError): + EXPORT.export(cache, libs, out, "def") + + +if __name__ == "__main__": + unittest.main() diff --git a/dm/tools/tests/test_ios_guarded_index_export.py b/dm/tools/tests/test_ios_guarded_index_export.py new file mode 100644 index 0000000000..6f4829d0f6 --- /dev/null +++ b/dm/tools/tests/test_ios_guarded_index_export.py @@ -0,0 +1,309 @@ +"""Dead seed proofs are structural, closed, and bound to verified base bytes. + +Fixtures below are synthetic toy functions, not extracted shader sources. +""" +import importlib.util +import json +from pathlib import Path +import re +import shutil +import subprocess +import tempfile +import unittest + + +TOOL = Path(__file__).resolve().parents[2] / "ios/tools/export_guarded_index_sources.py" +SPEC = importlib.util.spec_from_file_location("guarded_export", TOOL) +assert SPEC is not None and SPEC.loader is not None +EXPORT = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(EXPORT) +META = {"stage": "vs", "entry": "toy", "entry_z": "toy_z"} + +# A small uniform table stores coordinate pairs. A flag switches between those +# pairs and the unmodified input coordinates. The large bitcast seeds are dead +# on both paths, but their integer copies survive outside the guarded blocks. +REGION = """ + int mask = as_type((*args.constants).values[0].w) & 2; + bool skip = mask != 0; + copied_guard = skip; + if (!skip) { + float trunc_x = trunc(coord_x); + int int_x = int(trunc_x); + seed_x = int_x; + } + int x = seed_x; + int sum = x; + if (!skip) { + float trunc_y = trunc(coord_y); + int int_y = int(trunc_y); + seed_y = int_y; + } + int y = seed_y; + if (!skip) { + int twice_y = y << 1; + int joined = twice_y + x; + sum = joined; + } + int index = sum; + int scaled = index; + if (!skip) { + int shifted = index << 4; + scaled = shifted; + } + int offset = scaled; + if (!skip) { + int address = offset + 16; + uint word = uint(int(uint(address) >> uint(2))); + uint vector = uint(int(uint(int(word)) >> uint(2))); + int channel = int(word) & 3; + float value_x = (*args.constants).values[int(vector)][channel]; + int next_word = int(word) + 1; + uint next_vector = uint(int(uint(next_word) >> uint(2))); + int next_channel = next_word & 3; + float value_y = (*args.constants).values[int(next_vector)][next_channel]; + result_x = value_x; + result_y = value_y; + } +""" +NAMES = set(re.findall(r"\b(?:int|uint|float|bool) (\w+)\b", REGION)) | {"seed_x", "seed_y", "copied_guard"} + + +def region(prefix): + return re.sub(r"\b\w+\b", lambda m: prefix + "_" + m[0] if m[0] in NAMES else m[0], REGION) + + +def function(name): + return """ +vertex Output NAME(Input in, Args& args) { + Output out = {}; + float coord_x = in.x; + float coord_y = in.y; + float initial = in.z; + int a_seed_x = as_type(initial); + int a_seed_y = as_type(initial); + int b_seed_x = as_type(initial); + int b_seed_y = as_type(initial); + bool a_copied_guard; + bool b_copied_guard; + float result_x = coord_x; + float result_y = coord_y; + if (in.branch) { +REGION_A + } else { +REGION_B + } + out.x = result_x; + out.y = result_y; + return out; +} +""".replace("NAME", name).replace("REGION_A", region("a")).replace("REGION_B", region("b")) + + +SOURCE = "struct Output { float x, y; float point [[point_size]]; };\n" + function("toy") + function("toy_z") + function("toy_capture") + + +class GuardedIndexProofTests(unittest.TestCase): + def test_only_selected_entry_changes_and_names_are_not_shader_ids(self): + transformed, count = EXPORT.transform(SOURCE, META) + self.assertEqual(count, 4) + self.assertEqual(transformed[:transformed.index("vertex Output toy_z")], SOURCE[:SOURCE.index("vertex Output toy_z")]) + self.assertEqual(transformed[transformed.index("vertex Output toy_capture"):], SOURCE[SOURCE.index("vertex Output toy_capture"):]) + self.assertEqual(len(re.findall(r"int [ab]_seed_[xy] = 0;", transformed)), 4) + both, count = EXPORT.transform(SOURCE, META, {"toy", "toy_z"}) + self.assertEqual(count, 8) + self.assertIn("int a_seed_x = 0;", both) + + def test_each_entry_requires_its_own_complete_proof(self): + with self.assertRaises(ValueError): + EXPORT.transform(SOURCE.replace("toy_z(Input", "missing(Input"), META) + with self.assertRaises(ValueError): + EXPORT.transform(SOURCE, META, {"toy_capture"}) + bad = SOURCE.replace( + "int a_seed_x = as_type(initial);", "int a_seed_x = int(initial);", 2) + with self.assertRaises(ValueError): + EXPORT.transform(bad, META) + + def test_rejects_near_misses_not_just_wrong_shader_keys(self): + entry = function("toy_z") + changes = [ + ("a_seed_x = a_int_x;", "a_seed_x = a_seed_x;"), + ("if (!a_skip)", "if (a_skip)"), + ("a_seed_x = a_int_x;", "a_seed_y = a_int_x;"), + ("out.x = result_x;", "out.x = float(a_seed_x);"), + ("out.x = result_x;", "out.x = float(a_offset);"), + ("a_result_x = a_value_x;", "a_result_x = a_value_x; consume(a_word);"), + ("int a_x = a_seed_x;", "int a_x = a_seed_x; int a_offset = 7;"), + ("int a_seed_x = as_type(initial);", "volatile int a_seed_x = as_type(initial);"), + ("float initial = in.z;", "volatile float initial = in.z;"), + ] + # The value assignment is deliberately outside NAMES; use its actual + # spelling for the extra-call near miss. + changes[5] = ("result_x = a_value_x;", "result_x = a_value_x; consume(a_word);") + for before, after in changes: + with self.subTest(before=before): + self.assertIn(before, entry) + with self.assertRaises(ValueError): + EXPORT.transform(entry.replace(before, after, 1), META) + + def test_comments_do_not_count_as_uses_and_macros_fail_closed(self): + source = SOURCE.replace("int a_x = a_seed_x;", "// a_seed_x is not a live use\nint a_x = a_seed_x;") + self.assertEqual(EXPORT.transform(source, META)[1], 4) + for directive in ("#define a_seed_x another\n", '#include "unknown.h"\n', "#if 1\n"): + with self.assertRaises(ValueError): + EXPORT.transform(directive + SOURCE, META) + + def test_adjacent_float_seed_operands_each_receive_a_proof(self): + source = SOURCE.replace("float initial = in.z;", "float first_initial = in.z; float second_initial = in.y;") + source = source.replace("as_type(initial);", "as_type(first_initial);") + source = re.sub(r"(int [ab]_seed_y = as_type\()first_initial(\);)", r"\1second_initial\2", source) + self.assertEqual(EXPORT.transform(source, META)[1], 4) + + @unittest.skipUnless(shutil.which("c++"), "C++ compiler is needed for executed semantics comparison") + def test_executed_active_and_inactive_paths_preserve_outputs_and_memory_reads(self): + # Execute the actual before/after toy functions, including table bounds + # checks and an ordered read trace. Large/NaN seeds must never become + # active addresses. This does not claim anything about GPU speculation. + prefix = r""" +#include +#include +#include +#include +#define vertex +using uint = unsigned; +using std::trunc; +template T as_type(float x) { T r; std::memcpy(&r, &x, 4); return r; } +unsigned long long trace = 0; +struct Row { + float x, y, z, w; + float operator[](int i) const { + if (i < 0 || i >= 4) std::abort(); + trace = trace * 131 + unsigned(i + 1); + return i == 0 ? x : i == 1 ? y : i == 2 ? z : w; + } +}; +struct Rows { + Row rows[32]; + Row& operator[](int i) { + if (i < 0 || i >= 32) std::abort(); + trace = trace * 131 + unsigned(i + 7); + return rows[i]; + } +}; +struct Table { Rows values; }; +struct Args { Table* constants; }; +struct Input { float x, y, z; bool branch; }; +""" + harness = r""" +int main() { + Table table = {}; + for (int i = 0; i < 32; ++i) table.values.rows[i] = {float(i), float(i + 100), float(i + 200), float(i + 300)}; + Args args = {&table}; + float seeds[] = {-1.0f, 0.0f, 1.5f, std::nanf("")}; + for (int flags : {0, 2, 3, 65538}) { + std::memcpy(&table.values.rows[0].w, &flags, 4); + for (bool branch : {false, true}) for (float seed : seeds) + for (int x = 0; x < 3; ++x) for (int y = 0; y < 3; ++y) { + trace = 0; + Output out = toy_z({x + 0.75f, y + 0.25f, seed, branch}, args); + std::printf("%a %a %llu\n", double(out.x), double(out.y), trace); + } + } +} +""" + transformed, _ = EXPORT.transform(SOURCE, META) + with tempfile.TemporaryDirectory() as tmp: + outputs = [] + for i, source in enumerate((SOURCE, transformed)): + path = Path(tmp) / f"variant{i}.cpp" + path.write_text(prefix + source + harness) + binary = path.with_suffix("") + subprocess.run(["c++", "-std=c++17", "-Wno-unknown-attributes", "-include", "initializer_list", str(path), "-o", str(binary)], check=True, capture_output=True) + outputs.append(subprocess.run([str(binary)], check=True, capture_output=True).stdout) + self.assertEqual(outputs[0], outputs[1]) + self.assertEqual(len(outputs[0].splitlines()), 288) + + +class GuardedIndexExportTests(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + self.libs = Path(self.tmp.name) / "b-abc" + self.base = self.libs / "nonpoint-v1" + self.out = self.libs / "guarded-index-v1" + self.base.mkdir(parents=True) + self.source = SOURCE.encode() + self.library = b"MTLB-original" + self.meta = dict(META, chain="abc", key="0123_b") + self.reflection = (json.dumps(self.meta) + "\n").encode() + (self.base / "0123_b.metal").write_bytes(self.source) + (self.libs / "0123_b.metallib").write_bytes(self.library) + (self.libs / "0123_b.json").write_bytes(self.reflection) + self.manifest = {"schema": 1, "chain": "abc", "shaders": {"0123_b": { + "source_sha256": EXPORT.digest(self.source), "library_sha256": EXPORT.digest(self.library)}}} + self.write_manifest() + + def write_manifest(self): + (self.base / "manifest.json").write_text(json.dumps(self.manifest)) + + def test_manifest_binds_exact_input_and_output_bytes_and_preserves_originals(self): + originals = {p: p.read_bytes() for p in self.libs.rglob("*") if p.is_file()} + self.assertEqual(EXPORT.export(self.libs, self.out, "abc"), 1) + manifest = json.loads((self.out / "manifest.json").read_bytes()) + data = (self.out / "0123_b.metal").read_bytes() + self.assertEqual(manifest, {"schema": 1, "chain": "abc", "kind": "guarded-index-v1", "shaders": {"0123_b": { + "source": "0123_b.metal", "source_sha256": EXPORT.digest(data), + "original_source_sha256": EXPORT.digest(self.source), "base_library_sha256": EXPORT.digest(self.library), + "reflection_sha256": EXPORT.digest(self.reflection), "proof": "dead-guarded-index-seeds-v1", "replacement_count": 4}}}) + for path, original in originals.items(): + self.assertEqual(path.read_bytes(), original) + + def test_rejects_mixed_chain_source_library_and_reflection_before_writing(self): + for path in (self.base / "0123_b.metal", self.libs / "0123_b.metallib", self.libs / "0123_b.json"): + with self.subTest(path=path.name): + original = path.read_bytes() + path.write_bytes(original + b"tampered") + with self.assertRaises((ValueError, json.JSONDecodeError)): + EXPORT.export(self.libs, self.out, "abc") + self.assertFalse(self.out.exists()) + path.write_bytes(original) + self.meta["key"] = "other_b" + (self.libs / "0123_b.json").write_text(json.dumps(self.meta)) + with self.assertRaises(ValueError): + EXPORT.export(self.libs, self.out, "abc") + with self.assertRaises(ValueError): + EXPORT.export(self.libs, self.out, "wrong") + + def test_existing_linked_outputs_cannot_modify_originals(self): + self.out.mkdir() + (self.out / "0123_b.metal").hardlink_to(self.base / "0123_b.metal") + (self.out / "manifest.json").symlink_to(self.base / "manifest.json") + before = (self.base / "manifest.json").read_bytes() + self.assertEqual(EXPORT.export(self.libs, self.out, "abc"), 1) + self.assertEqual((self.base / "0123_b.metal").read_bytes(), self.source) + self.assertEqual((self.base / "manifest.json").read_bytes(), before) + + def test_rejects_bad_base_manifest_and_output_collision(self): + for field, value in (("schema", 2), ("chain", "wrong"), ("shaders", [])): + old = self.manifest[field] + self.manifest[field] = value + self.write_manifest() + with self.assertRaises(ValueError): + EXPORT.export(self.libs, self.out, "abc") + self.manifest[field] = old + self.write_manifest() + for output in (self.libs, self.base, self.base / "nested"): + with self.assertRaises(ValueError): + EXPORT.export(self.libs, output, "abc") + + def test_unsupported_source_has_no_record_or_companion(self): + unsupported = self.source.replace(b"a_seed_x = a_int_x;", b"a_seed_x = a_seed_x;") + (self.base / "0123_b.metal").write_bytes(unsupported) + self.manifest["shaders"]["0123_b"]["source_sha256"] = EXPORT.digest(unsupported) + self.write_manifest() + self.assertEqual(EXPORT.export(self.libs, self.out, "abc"), 0) + self.assertEqual(json.loads((self.out / "manifest.json").read_bytes())["shaders"], {}) + self.assertFalse((self.out / "0123_b.metal").exists()) + + +if __name__ == "__main__": + unittest.main() diff --git a/dm/tools/tests/test_ios_min_reduction_export.py b/dm/tools/tests/test_ios_min_reduction_export.py new file mode 100644 index 0000000000..6cd3bc6e6c --- /dev/null +++ b/dm/tools/tests/test_ios_min_reduction_export.py @@ -0,0 +1,116 @@ +"""MIN companions are structurally narrow, source-hash gated and reversible.""" +import hashlib +import importlib.util +import json +from pathlib import Path +import tempfile +import unittest +from unittest.mock import patch + +TOOL = Path(__file__).resolve().parents[2] / "ios/tools/export_min_reduction.py" +SPEC = importlib.util.spec_from_file_location("min_reduction_export", TOOL) +assert SPEC is not None and SPEC.loader is not None +EXPORT = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(EXPORT) + + +def source(prefix): + return f"""#include +using namespace metal; +// Synthetic test source; no game code or data. +float sample_fixture() {{ + float result = spvDescriptorSet0.{prefix}_t_tcb_2A.sample(spvDescriptorSet0.{prefix}_t_tcb_2ASmplr, select(float2(f(x, y), z), float2(0.0), isnan(float2(x, y))), level(integer_lod)).x; + return result + unrelated.sample(other_sampler, uv, level(0)).x; +}} +""" + + +class MinReductionExportTests(unittest.TestCase): + def test_only_unique_slot17_call_changes_and_helper_is_self_contained(self): + for key, profile in EXPORT.AUDITED.items(): + original = source(profile["prefix"]) + rewritten, payload = EXPORT.transform(original, key) + self.assertEqual(rewritten.count("// DM_MIN_REDUCTION_HELPER_BEGIN"), 1) + self.assertNotIn('#include "nvn_reduction_footprint.h"', rewritten) + self.assertIn("unrelated.sample(other_sampler, uv, level(0)).x", rewritten) + self.assertIn("level(integer_lod)", original) + self.assertIn("integer_lod, 0.0f, 1000.0f, false).x", rewritten) + injected = "\n" + EXPORT.HELPER_BEGIN + payload + EXPORT.HELPER_END + without_helper = rewritten.replace(injected, "", 1) + tex = f"spvDescriptorSet0.{profile['prefix']}_t_tcb_2A" + start = original.index(tex + ".sample(") + end = EXPORT.close_paren(original, start + len(tex + ".sample")) + expected = original[start:end + 1] + new_start = without_helper.index("dm_reduction_sample_lmn_edge(") + new_end = EXPORT.close_paren(without_helper, new_start + len("dm_reduction_sample_lmn_edge")) + restored = without_helper[:new_start] + expected + without_helper[new_end + 1:] + self.assertEqual(restored, original) + + def test_rejects_duplicate_implicit_offset_or_wrong_sampler_calls(self): + key = next(iter(EXPORT.AUDITED)) + original = source("fp") + for bad in (original + original, original.replace("level(integer_lod)", "bias(1)"), + original.replace("level(integer_lod)", "level(integer_lod), int2(1, 0)"), + original.replace("fp_t_tcb_2ASmplr", "differentSampler")): + with self.subTest(source=bad), self.assertRaises(ValueError): + EXPORT.transform(bad, key) + + def prepare(self, root): + cache, libs, out = root / f"cache/b-{EXPORT.CHAIN}", root / f"ios/b-{EXPORT.CHAIN}", root / "min-reduction-v1" + cache.mkdir(parents=True); libs.mkdir(parents=True) + audited = {} + for key, profile in EXPORT.AUDITED.items(): + data = source(profile["prefix"]).encode() + audited[key] = dict(profile, original_source_sha256=hashlib.sha256(data).hexdigest()) + (cache / (key + ".metal")).write_bytes(data) + (libs / (key + ".metallib")).write_bytes(b"MTLB-synthetic-original") + prefix = profile["prefix"] + meta = {"chain": EXPORT.CHAIN, "key": key, "stage": profile["stage"], "entry": "main0", "bindings": [ + {"kind": "tex", "reg": 17, "msl_name": f"{prefix}_t_tcb_2A", "msl_type": "texture2d"}, + {"kind": "samp_tex", "reg": 17, "msl_name": f"{prefix}_t_tcb_2ASmplr", "msl_type": "sampler"}, + ]} + (libs / (key + ".json")).write_text(json.dumps(meta)) + return cache, libs, out, audited + + def test_export_records_sha_contract_preserves_originals_and_is_idempotent(self): + with tempfile.TemporaryDirectory() as tmp: + cache, libs, out, audited = self.prepare(Path(tmp)) + originals = {p: p.read_bytes() for d in (cache, libs) for p in d.iterdir()} + with patch.object(EXPORT, "AUDITED", audited): + self.assertEqual(EXPORT.export(cache, libs, out, EXPORT.CHAIN), 2) + self.assertEqual(EXPORT.export(cache, libs, out, EXPORT.CHAIN), 2) + for key, profile in audited.items(): + record = json.loads((out / (key + ".json")).read_text()) + self.assertEqual(record["source_sha256"], hashlib.sha256((out / (key + ".metal")).read_bytes()).hexdigest()) + self.assertEqual(record["original_source_sha256"], profile["original_source_sha256"]) + self.assertEqual(record["library_sha256"], hashlib.sha256(b"MTLB-synthetic-original").hexdigest()) + self.assertEqual(record["helper_sha256"], hashlib.sha256(EXPORT.helper_source().encode()).hexdigest()) + self.assertEqual(record["sampler"], EXPORT.SAMPLER) + self.assertTrue(record["integer_explicit_lod"]) + self.assertEqual((record["entry"], record["reg"], record["calls"]), ("main0", 17, 1)) + for path, data in originals.items(): + self.assertEqual(path.read_bytes(), data) + + def test_fails_closed_on_original_sha_reflection_chain_or_existing_output(self): + with tempfile.TemporaryDirectory() as tmp: + cache, libs, out, audited = self.prepare(Path(tmp)) + with self.assertRaises(ValueError): + EXPORT.export(cache, libs, out, EXPORT.CHAIN) # synthetic source is not an audited original + self.assertFalse(out.exists()) + with patch.object(EXPORT, "AUDITED", audited): + with self.assertRaises(ValueError): EXPORT.export(cache, libs, out, "wrong-chain") + key = next(iter(audited)) + path = libs / (key + ".json") + correct = path.read_text(); bad = json.loads(correct); bad["bindings"][0]["reg"] = 18 + path.write_text(json.dumps(bad)) + with self.assertRaises(ValueError): EXPORT.export(cache, libs, out, EXPORT.CHAIN) + self.assertFalse(out.exists()); path.write_text(correct) + EXPORT.export(cache, libs, out, EXPORT.CHAIN) + (out / (key + ".metal")).write_text("changed companion") + with self.assertRaises(ValueError): EXPORT.export(cache, libs, out, EXPORT.CHAIN) + with self.assertRaises(ValueError): EXPORT.export(cache, libs, cache, EXPORT.CHAIN) + with self.assertRaises(ValueError): EXPORT.export(cache, libs, EXPORT.ROOT / "game-assets", EXPORT.CHAIN) + + +if __name__ == "__main__": + unittest.main() diff --git a/dm/tools/tests/test_ios_nonpoint_export.py b/dm/tools/tests/test_ios_nonpoint_export.py new file mode 100644 index 0000000000..84e64f18e4 --- /dev/null +++ b/dm/tools/tests/test_ios_nonpoint_export.py @@ -0,0 +1,62 @@ +"""Supplemental shader exports must preserve data and reject mixed identities.""" +import hashlib +import importlib.util +import json +from pathlib import Path +import tempfile +import unittest + +TOOL = Path(__file__).resolve().parents[2] / "ios/tools/export_nonpoint_sources.py" +SPEC = importlib.util.spec_from_file_location("nonpoint_export", TOOL) +assert SPEC is not None and SPEC.loader is not None +EXPORT = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(EXPORT) + + +class NonPointExportTests(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + root = Path(self.tmp.name) + self.cache, self.libs, self.out = root / "cache/b-abc", root / "ios/b-abc", root / "export" + self.cache.mkdir(parents=True) + self.libs.mkdir(parents=True) + self.source = b"float gl_PointSize [[point_size]];\n" + (self.cache / "a_b.metal").write_bytes(self.source) + (self.libs / "a_b.metallib").write_bytes(b"MTLB-original") + self.reflection = self.libs / "a_b.json" + self.meta = {"stage": "vs", "key": "a_b", "chain": "abc"} + self.reflection.write_text(json.dumps(self.meta)) + + def test_export_preserves_source_and_binds_both_hashes(self): + self.assertEqual(EXPORT.export(self.cache, self.libs, self.out, "abc"), 1) + manifest = json.loads((self.out / "manifest.json").read_text()) + self.assertEqual(manifest["chain"], "abc") + self.assertEqual((self.out / "a_b.metal").read_bytes(), self.source) + self.assertEqual(manifest["shaders"]["a_b"], { + "source_sha256": hashlib.sha256(self.source).hexdigest(), + "library_sha256": hashlib.sha256(b"MTLB-original").hexdigest(), + }) + + def test_rejects_wrong_chain_directory_and_reflection(self): + with self.assertRaises(ValueError): + EXPORT.export(self.cache, self.libs, self.out, "def") + self.meta["chain"] = "def" + self.reflection.write_text(json.dumps(self.meta)) + with self.assertRaises(ValueError): + EXPORT.export(self.cache, self.libs, self.out, "abc") + self.assertFalse((self.out / "manifest.json").exists()) + + def test_does_not_export_fragment_or_nonpoint_free_source(self): + self.meta["stage"] = "ps" + self.reflection.write_text(json.dumps(self.meta)) + self.assertEqual(EXPORT.export(self.cache, self.libs, self.out, "abc"), 0) + self.meta["stage"] = "vs" + self.reflection.write_text(json.dumps(self.meta)) + (self.cache / "a_b.metal").write_text("float4 position [[position]];\n") + self.assertEqual(EXPORT.export(self.cache, self.libs, self.out, "abc"), 0) + self.assertEqual(json.loads((self.out / "manifest.json").read_text())["shaders"], {}) + + +if __name__ == "__main__": + unittest.main() diff --git a/dm/tools/tests/test_za_astc_pack.py b/dm/tools/tests/test_za_astc_pack.py new file mode 100644 index 0000000000..0aa218a6c8 --- /dev/null +++ b/dm/tools/tests/test_za_astc_pack.py @@ -0,0 +1,425 @@ +"""Unit tests for the Z-A offline ASTC pack tool. + +These tests use only synthetic bytes and temporary files. They deliberately do +not require a RomFS, the guest Oodle module, or astcenc. +""" + +import hashlib +import importlib.util +import io +import json +import math +from pathlib import Path +import shutil +import struct +import tempfile +from types import SimpleNamespace +import unittest +from unittest import mock + + +TOOL = Path(__file__).resolve().parents[1] / "za_astc_pack.py" +SPEC = importlib.util.spec_from_file_location("za_astc_pack", TOOL) +assert SPEC is not None and SPEC.loader is not None +ZA = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(ZA) + + +def tga_header(kind, width, height, bits=32, descriptor=0x28): + return struct.pack( + " ASTC 4x4 patches for Z-A data.trpfs. + +The source archive is always opened read-only. ``census`` only writes JSON. +``prove`` runs one selected Trinity file through extract, BNTX conversion, +the game's Oodle encoder, an independent game-decoder round trip, and emits +an exact range-patch bundle. ``prepare`` extends the same operation to all +selected packs. ``apply`` verifies the source identity, clones (or explicitly +copies) it to a new path, and applies the exact ranges to that clone. +``relocate`` inserts copied replacement ONEPACK records immediately before the +archive's root FlatBuffer and emits an exact virtual pack layout, root-pointer, +root-index and descriptor edits; ``apply-relocated`` materializes that compact +bundle into a new, atomically published pair. Trinity requires both parallel +root vectors to remain sorted and each pack offset to be the aligned prefix sum +of descriptor sizes. A small read-redirect table therefore maps that virtual +layout to unchanged source packs or inserted replacement packs. The root must +remain at EOF in the virtual layout: appending packs after it makes Trinity +parse the appended payload as root-index data. + +Only BC7_UNORM and BC7_sRGB are eligible in the first version. BC1/BC4 would +grow from 8 to 16 bytes per block, BC5 needs a different no-BC destination +contract, and BC6H is HDR. A replacement is admitted only when its guest +Oodle stream fits the existing FlatBuffer byte-vector capacity. Relocated +packs instead append a new vector inside the copied pack, leaving every source +byte immutable. +""" + +from __future__ import annotations + +import argparse +import bisect +import collections +import ctypes as C +import errno +import hashlib +import json +import math +import mmap +import os +from pathlib import Path +import shutil +import struct +import subprocess +import tempfile +import time + + +REPO = Path(__file__).resolve().parents[2] +ONEPACK = b"ONEPACK\0" +REDIRECT_MAGIC = "DM_ROMFS_REDIRECTS_V1" +BC7_TO_ASTC = {0x2001: 0x2D01, 0x2006: 0x2D06} +FORMAT_NAMES = { + 0x1A01: "BC1_UNORM", + 0x1A06: "BC1_sRGB", + 0x1B01: "BC2_UNORM", + 0x1B06: "BC2_sRGB", + 0x1C01: "BC3_UNORM", + 0x1C06: "BC3_sRGB", + 0x1D01: "BC4_UNORM", + 0x1D02: "BC4_SNORM", + 0x1E01: "BC5_UNORM", + 0x1E02: "BC5_SNORM", + 0x1F05: "BC6H_UFLOAT", + 0x1F0A: "BC6H_SFLOAT", + 0x2001: "BC7_UNORM", + 0x2006: "BC7_sRGB", + 0x2D01: "ASTC4x4_UNORM", + 0x2D06: "ASTC4x4_sRGB", +} + + +class FormatError(ValueError): + """An archive or texture structure failed a defensive validation.""" + + +class QualityError(ValueError): + """A transcoded texture did not meet the requested quality floor.""" + + +def sha256_bytes(data: bytes | bytearray | memoryview) -> str: + return hashlib.sha256(data).hexdigest() + + +def sha256_file(path: Path) -> str: + digest = hashlib.sha256() + with path.open("rb") as stream: + while True: + block = stream.read(8 << 20) + if not block: + return digest.hexdigest() + digest.update(block) + + +def sha256_file_with_ranges(path: Path, patches: list[dict], append: bytes | Path = b"") -> str: + """Hash a virtual source with disjoint replacements and optional append.""" + digest = hashlib.sha256() + cursor = 0 + with path.open("rb") as stream: + for patch in sorted(patches, key=lambda row: row["offset"]): + offset = patch["offset"] + if offset < cursor: + raise ValueError("virtual hash ranges overlap") + left = offset - cursor + while left: + block = stream.read(min(left, 8 << 20)) + if not block: + raise EOFError("virtual hash range leaves source") + digest.update(block) + left -= len(block) + original = read_exact(stream, patch["bytes"]) + if sha256_bytes(original) != patch["source_sha256"]: + raise ValueError(f"source range differs at {offset:#x}") + replacement = bytes.fromhex(patch["replacement_hex"]) + if len(replacement) != patch["bytes"]: + raise ValueError("virtual replacement has a different size") + digest.update(replacement) + cursor = offset + patch["bytes"] + while True: + block = stream.read(8 << 20) + if not block: + break + digest.update(block) + if isinstance(append, Path): + with append.open("rb") as stream: + while True: + block = stream.read(8 << 20) + if not block: + break + digest.update(block) + else: + digest.update(append) + return digest.hexdigest() + + +def patched_bytes(source: bytes | bytearray | memoryview, base: int, + patches: list[dict], offset_key: str = "source_offset") -> bytes: + """Return one bounded source range with exact, non-overlapping edits.""" + result = bytearray(source) + previous_end = base + for patch in sorted(patches, key=lambda row: row[offset_key]): + offset = patch[offset_key] + size = patch["bytes"] + if offset < previous_end or offset < base or offset + size > base + len(result): + raise ValueError("inserted-archive edit range is invalid or overlaps") + relative = offset - base + original = bytes(result[relative:relative + size]) + if (original.hex() != patch["source_hex"] or + sha256_bytes(original) != patch["source_sha256"]): + raise ValueError(f"source range differs at {offset:#x}") + replacement = bytes.fromhex(patch["replacement_hex"]) + if len(replacement) != size or sha256_bytes(replacement) != patch["replacement_sha256"]: + raise ValueError(f"replacement range differs at {offset:#x}") + result[relative:relative + size] = replacement + previous_end = offset + size + return bytes(result) + + +def sha256_inserted_archive(path: Path, root_offset: int, append: Path, + header_edits: list[dict], final_root: bytes) -> str: + """Hash patched prefix + inserted packs + already-patched shifted root.""" + size = path.stat().st_size + if not 16 <= root_offset < size: + raise ValueError("archive root offset is outside the source") + digest = hashlib.sha256() + rows = sorted(header_edits, key=lambda row: row["source_offset"]) + cursor = 0 + with path.open("rb") as stream: + for row in rows: + offset, count = row["source_offset"], row["bytes"] + if offset < cursor or offset + count > root_offset: + raise ValueError("archive header edit is outside the prefix or overlaps") + left = offset - cursor + while left: + block = read_exact(stream, min(left, 8 << 20)) + digest.update(block); left -= len(block) + original = read_exact(stream, count) + if (original.hex() != row["source_hex"] or + sha256_bytes(original) != row["source_sha256"]): + raise ValueError(f"archive header source differs at {offset:#x}") + replacement = bytes.fromhex(row["replacement_hex"]) + if len(replacement) != count or sha256_bytes(replacement) != row["replacement_sha256"]: + raise ValueError(f"archive header replacement differs at {offset:#x}") + digest.update(replacement); cursor = offset + count + left = root_offset - cursor + while left: + block = read_exact(stream, min(left, 8 << 20)) + digest.update(block); left -= len(block) + with append.open("rb") as stream: + while True: + block = stream.read(8 << 20) + if not block: + break + digest.update(block) + digest.update(final_root) + return digest.hexdigest() + + +def align_up(value: int, alignment: int) -> int: + if alignment <= 0 or alignment & (alignment - 1): + raise ValueError("alignment must be a positive power of two") + return (value + alignment - 1) & -alignment + + +def virtual_pack_layout(packs: list[dict], relocated: list[dict], + source_root_offset: int, physical_root_offset: int, + root_bytes: int, physical_file_bytes: int) -> tuple[list[int], list[dict], int]: + """Build Trinity's aligned virtual pack sequence and its physical read map. + + The two root vectors are parallel and hash-sorted. Their order therefore + cannot change, while Trinity also requires the offset difference between + adjacent entries to equal the current descriptor size rounded to 16 bytes. + Only the virtual offsets move; each whole-pack read maps back to either the + immutable source range or the inserted replacement range. + """ + ordered = sorted(packs, key=lambda row: row["pack_index"]) + if not ordered or [row["pack_index"] for row in ordered] != list(range(len(ordered))): + raise FormatError("pack indices are not one complete ordered sequence") + replacements = {row["pack_index"]: row for row in relocated} + if len(replacements) != len(relocated): + raise FormatError("relocated pack table contains duplicate indices") + unknown = set(replacements) - {row["pack_index"] for row in ordered} + if unknown: + raise FormatError("relocated pack table contains an unknown index") + + # Refuse a source with a different physical invariant. The virtual map is + # safe only because every source pack occupies exactly its aligned span. + for number, pack in enumerate(ordered): + expected_end = pack["pack_offset"] + align_up(pack["pack_size"], 16) + actual_end = (ordered[number + 1]["pack_offset"] + if number + 1 < len(ordered) else source_root_offset) + if expected_end != actual_end: + raise FormatError(f"source pack {pack['pack_index']} is not a contiguous aligned span") + + virtual = ordered[0]["pack_offset"] + if virtual <= 0 or virtual > physical_file_bytes: + raise FormatError("archive prefix span is invalid") + offsets = [] + redirects = [{ + "kind": "header", "virtual_offset": 0, "virtual_bytes": virtual, + "physical_offset": 0, "physical_bytes": virtual, + }] + for pack in ordered: + replacement = replacements.get(pack["pack_index"]) + size = replacement["appended_bytes"] if replacement else pack["pack_size"] + physical = replacement["appended_offset"] if replacement else pack["pack_offset"] + span = align_up(size, 16) + if (physical < 0 or span <= 0 or physical > physical_file_bytes or + span > physical_file_bytes - physical): + raise FormatError(f"physical pack {pack['pack_index']} leaves the materialized archive") + offsets.append(virtual) + redirects.append({ + "pack_index": pack["pack_index"], + "virtual_offset": virtual, "virtual_bytes": span, + "physical_offset": physical, "physical_bytes": span, + }) + virtual += span + virtual_root_offset = virtual + if (physical_root_offset < 0 or root_bytes <= 0 or + physical_root_offset + root_bytes > physical_file_bytes): + raise FormatError("physical root leaves the materialized archive") + redirects.append({ + "kind": "root", "virtual_offset": virtual_root_offset, + "virtual_bytes": root_bytes, "physical_offset": physical_root_offset, + "physical_bytes": root_bytes, + }) + return offsets, redirects, virtual_root_offset + + +def redirect_table_bytes(rows: list[dict], virtual_file_bytes: int, + physical_file_bytes: int, + target: str = "arc/data.trpfs") -> bytes: + """Serialize the bounded whole-read virtual-to-physical mapping.""" + if (not rows or not target or any(c.isspace() for c in target) or + virtual_file_bytes <= 0 or physical_file_bytes <= 0): + raise FormatError("redirect table header is invalid") + lines = [f"{REDIRECT_MAGIC}\t{target}\t{len(rows)}\t{virtual_file_bytes}\t{physical_file_bytes}\n"] + previous_virtual_end = 0 + for row in rows: + values = tuple(row[key] for key in + ("virtual_offset", "virtual_bytes", "physical_offset", "physical_bytes")) + virtual_offset, virtual_bytes, physical_offset, physical_bytes = values + if (any(not isinstance(value, int) or value < 0 for value in values) or + virtual_bytes <= 0 or physical_bytes < virtual_bytes or + virtual_offset != previous_virtual_end or + virtual_offset + virtual_bytes > virtual_file_bytes or + physical_offset + physical_bytes > physical_file_bytes): + raise FormatError("redirect table range is invalid or overlaps") + previous_virtual_end = virtual_offset + virtual_bytes + lines.append("\t".join(str(value) for value in values) + "\n") + if previous_virtual_end != virtual_file_bytes: + raise FormatError("redirect table does not cover the virtual file") + return "".join(lines).encode("ascii") + + +def parse_redirect_table(data: bytes) -> dict: + """Parse and canonically reserialize one redirect table.""" + try: + lines = data.decode("ascii").splitlines(keepends=True) + header = lines[0].removesuffix("\n").split("\t") + if len(header) != 5 or header[0] != REDIRECT_MAGIC: + raise FormatError("redirect table magic or header differs") + target = header[1] + count, virtual_bytes, physical_bytes = map(int, header[2:]) + rows = [] + for line in lines[1:]: + fields = line.removesuffix("\n").split("\t") + if len(fields) != 4: + raise FormatError("redirect table row width differs") + values = tuple(map(int, fields)) + rows.append(dict(zip(("virtual_offset", "virtual_bytes", + "physical_offset", "physical_bytes"), values))) + except (IndexError, UnicodeDecodeError, ValueError) as exc: + raise FormatError("redirect table is malformed") from exc + if count != len(rows): + raise FormatError("redirect table record count differs") + if redirect_table_bytes(rows, virtual_bytes, physical_bytes, target) != data: + raise FormatError("redirect table is not canonical") + return {"target": target, "records": rows, + "virtual_file_bytes": virtual_bytes, + "physical_file_bytes": physical_bytes} + + +def fsync_directory(path: Path) -> None: + descriptor = os.open(path, os.O_RDONLY) + try: + os.fsync(descriptor) + finally: + os.close(descriptor) + + +def read_exact(stream, size: int) -> bytes: + result = bytearray() + while len(result) < size: + block = stream.read(size - len(result)) + if not block: + raise EOFError(f"stream ended after {len(result)} of {size} bytes") + result += block + return bytes(result) + + +def ensure_private_path(path: Path) -> None: + """Derived game data must not be created in the source repository.""" + resolved = path.resolve(strict=False) + try: + resolved.relative_to(REPO) + except ValueError: + return + raise ValueError(f"derived game data must be outside the source repository: {resolved}") + + +def fb_field(data, table: int, index: int) -> int | None: + if table < 4 or table + 4 > len(data): + raise FormatError("FlatBuffer table is out of bounds") + vtable = table - struct.unpack_from(" len(data): + raise FormatError("FlatBuffer vtable is out of bounds") + vsize = struct.unpack_from(" len(data): + raise FormatError("FlatBuffer vtable size is invalid") + if 4 + 2 * index >= vsize: + return None + offset = struct.unpack_from("= len(data): + raise FormatError("FlatBuffer field is out of bounds") + return field + + +def fb_vector(data, table: int, index: int) -> tuple[int, int]: + field = fb_field(data, table, index) + if field is None: + return 0, 0 + if field + 4 > len(data): + raise FormatError("FlatBuffer vector reference is truncated") + vector = field + struct.unpack_from(" len(data): + raise FormatError("FlatBuffer vector is out of bounds") + start, count = vector + 4, struct.unpack_from(" list[int]: + start, count = fb_vector(data, table, index) + if count > (len(data) - start) // 4: + raise FormatError("FlatBuffer reference vector is truncated") + result = [] + for item in range(count): + at = start + item * 4 + target = at + struct.unpack_from("= len(data): + raise FormatError("FlatBuffer reference is out of bounds") + result.append(target) + return result + + +def fb_root(data, at: int = 0) -> int: + if at < 0 or at + 4 > len(data): + raise FormatError("FlatBuffer root reference is truncated") + root = at + struct.unpack_from("= len(data): + raise FormatError("FlatBuffer root is out of bounds") + return root + + +def bntx_textures(data: bytes | bytearray) -> list[dict]: + """Return defensively validated BRTI metadata, including embedded BNTX.""" + rows = [] + search = 0 + while True: + base = data.find(b"BNTX\0\0\0\0", search) + if base < 0: + return rows + search = base + 8 + if base + 0x50 > len(data) or data[base + 12:base + 14] != b"\xff\xfe": + continue + size = struct.unpack_from(" len(data): + continue + count = struct.unpack_from(" 65536 or pointer_array + count * 8 > size: + continue + for texture_index in range(count): + brti = struct.unpack_from(" size or data[base + brti:base + brti + 4] != b"BRTI": + raise FormatError("invalid BRTI pointer") + q = base + brti + fmt = struct.unpack_from(" size: + raise FormatError("invalid BRTI mip pointer table") + offsets = list(struct.unpack_from("<" + "Q" * mips, data, base + mip_table)) + if (not offsets or offsets[0] + image_size > size or + any(x < offsets[0] or x >= offsets[0] + image_size for x in offsets)): + raise FormatError("invalid BRTI image bounds") + payload = data[base + offsets[0]:base + offsets[0] + image_size] + rows.append({ + "bntx_base": base, + "bntx_size": size, + "texture_index": texture_index, + "brti_offset": brti, + "format": fmt, + "format_name": FORMAT_NAMES.get(fmt, f"format_{fmt:08x}"), + "width": width, + "height": height, + "depth": depth, + "layers": layers, + "mips": mips, + "image_bytes": image_size, + "alignment": alignment, + "channels": channels, + "tile_mode": struct.unpack_from(" dict: + """Parse the immutable trpfd fields needed to name and relocate packs.""" + data = path.read_bytes() + root = fb_root(data) + hash_at, hash_count = fb_vector(data, root, 0) + hashes = struct.unpack_from("<" + "Q" * hash_count, data, hash_at) + strings = [] + for ref in fb_refs(data, root, 1): + length = struct.unpack_from(" len(data): + raise FormatError("descriptor pack name is truncated") + strings.append(data[ref + 4:ref + 4 + length].decode("utf-8", "replace")) + pack_of_hash = {} + for file_hash, info in zip(hashes, fb_refs(data, root, 2)): + field = fb_field(data, info, 0) + pack_index = struct.unpack_from("= len(strings): + raise FormatError("descriptor file info names an invalid pack") + pack_of_hash[file_hash] = pack_index + pack_infos = [] + for pack_index, info in enumerate(fb_refs(data, root, 3)): + size_field, count_field = fb_field(data, info, 0), fb_field(data, info, 1) + if size_field is None or count_field is None: + raise FormatError(f"descriptor pack {pack_index} is missing size/count") + pack_infos.append({ + "descriptor_index": pack_index, + "name": strings[pack_index], + "size": struct.unpack_from(" dict[int, str]: + return descriptor_catalog(path)["names_by_hash"] + + +def trpfs_records(data, names: dict[int, str], descriptor: dict | None = None) -> tuple[list[dict], list[dict]]: + if data[:8] != ONEPACK: + raise FormatError("archive does not start with ONEPACK\\0") + root_at = struct.unpack_from(" (len(data) - offset_at) // 8: + raise FormatError("pack offset vector is truncated") + packs, files = [], [] + for pack_index, pack_offset in enumerate(struct.unpack_from("<" + "Q" * pack_count, data, offset_at)): + pack_root = fb_root(data, pack_offset) + hash_at, hash_count = fb_vector(data, pack_root, 0) + hashes = struct.unpack_from("<" + "Q" * hash_count, data, hash_at) + tables = fb_refs(data, pack_root, 1) + if len(tables) != len(hashes): + raise FormatError(f"pack {pack_index} hash/file count mismatch") + pack_name = names.get(hashes[0], f"pack-{pack_index}") if hashes else f"pack-{pack_index}" + descriptor_index = None + pack_size = None + descriptor_size_field = None + if descriptor is not None and hashes: + descriptor_index = descriptor["pack_of_hash"].get(hashes[0]) + if descriptor_index is None: + raise FormatError(f"pack {pack_index} has no descriptor entry") + pack_info = descriptor["pack_infos"][descriptor_index] + pack_size = pack_info["size"] + descriptor_size_field = pack_info["size_field_offset"] + if pack_info["file_count"] != len(tables): + raise FormatError(f"pack {pack_index} descriptor file count mismatch") + if pack_offset + pack_size > len(data): + raise FormatError(f"pack {pack_index} descriptor size leaves the archive") + pack_common = { + "pack_index": pack_index, + "pack_offset": pack_offset, + "pack_offset_field": offset_at + pack_index * 8, + "pack_name": pack_name, + "file_count": len(tables), + "descriptor_index": descriptor_index, + "pack_size": pack_size, + "descriptor_size_field": descriptor_size_field, + } + packs.append(pack_common) + for file_index, (file_hash, table) in enumerate(zip(hashes, tables)): + compression_field = fb_field(data, table, 1) + level_field = fb_field(data, table, 2) + raw_field = fb_field(data, table, 3) + payload_field = fb_field(data, table, 4) + payload_at, stored_size = fb_vector(data, table, 4) + if payload_at + stored_size > len(data): + raise FormatError(f"pack {pack_index} file {file_index} payload is truncated") + files.append({ + "pack_index": pack_index, + **pack_common, + "file_index": file_index, + "file_hash": file_hash, + "file_table_offset": table, + "compression_field_offset": compression_field, + "raw_size_field_offset": raw_field, + "payload_field_offset": payload_field, + "compression": struct.unpack_from(" bytes: + if self.process.stdin is None or self.process.stdout is None: + raise RuntimeError("guest decoder pipes are unavailable") + try: + self.process.stdin.write(struct.pack(" None: + if self.process.stdin is not None and not self.process.stdin.closed: + self.process.stdin.close() + returncode = self.process.wait(timeout=30) + if self.process.stdout is not None: + self.process.stdout.close() + self.log.close() + if returncode: + raise RuntimeError(f"guest decoder exited with {returncode}; see {self.log_path}") + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc, traceback): + try: + self.close() + except Exception: + if exc is None: + raise + + +class GuestEncoder: + def __init__(self, host: Path, modules: Path, work: Path, log_path: Path, + compressor: int = 9, level: int = 7): + self.host, self.modules, self.work, self.log_path = host, modules, work, log_path + self.compressor, self.level = compressor, level + + def encode(self, raw: bytes, tag: str) -> bytes: + if not raw or len(raw) > 64 << 20: + raise ValueError("guest compression job accepts 1..64 MiB") + safe_tag = "".join(c if c.isalnum() or c in "-_" else "_" for c in tag)[:80] + job = self.work / f"compress-{safe_tag}.job" + output = Path(str(job) + ".out") + job.write_bytes(b"DMCJ" + struct.pack(" None: + sources = [ + REPO / "dm/gfx/tex/nvn_deswizzle.c", + REPO / "dm/gfx/nvn/nvn_tex.c", + REPO / "dm/gfx/tex/nvn_bcdec.c", + ] + subprocess.run(["clang", "-O2", "-dynamiclib", "-o", str(output), *(str(x) for x in sources)], + check=True) + + +def write_tga(path: Path, width: int, height: int, rgba: bytes) -> None: + if len(rgba) != width * height * 4: + raise ValueError("RGBA byte count does not match TGA dimensions") + bgra = bytearray(len(rgba)) + bgra[0::4], bgra[1::4], bgra[2::4], bgra[3::4] = rgba[2::4], rgba[1::4], rgba[0::4], rgba[3::4] + header = struct.pack(" tuple[int, int, bytes]: + data = path.read_bytes() + if len(data) < 18: + raise FormatError("ASTC decoder wrote a truncated TGA") + ident, cmap, kind = data[0], data[1], data[2] + width, height, bits, descriptor = struct.unpack_from(" len(data): + raise FormatError("ASTC decoder TGA pixels are truncated") + pixels = [data[p:p + bpp] for p in range(at, at + need, bpp)] + else: + total = width * height + while len(pixels) < total: + if at >= len(data): + raise FormatError("ASTC decoder TGA RLE is truncated") + packet = data[at] + at += 1 + count = (packet & 0x7F) + 1 + if packet & 0x80: + if at + bpp > len(data): + raise FormatError("ASTC decoder TGA RLE pixel is truncated") + pixel = data[at:at + bpp] + at += bpp + pixels.extend([pixel] * count) + else: + need = count * bpp + if at + need > len(data): + raise FormatError("ASTC decoder TGA raw packet is truncated") + pixels.extend(data[p:p + bpp] for p in range(at, at + need, bpp)) + at += need + if len(pixels) != total: + raise FormatError("ASTC decoder TGA RLE overruns its dimensions") + rows = [pixels[y * width:(y + 1) * width] for y in range(height)] + if not descriptor & 0x20: # bottom origin -> top origin + rows.reverse() + if descriptor & 0x10: # right origin -> left origin + rows = [list(reversed(row)) for row in rows] + rgba = bytearray(width * height * 4) + out = 0 + for row in rows: + for pixel in row: + rgba[out:out + 4] = bytes((pixel[2], pixel[1], pixel[0], pixel[3] if bpp == 4 else 255)) + out += 4 + return width, height, bytes(rgba) + + +def rgba_metrics(reference: bytes, decoded: bytes) -> dict: + if len(reference) != len(decoded) or len(reference) % 4: + raise ValueError("quality images have different byte counts") + sum_squared = 0 + maximum = 0 + for left, right in zip(reference, decoded): + delta = left - right + absolute = abs(delta) + maximum = max(maximum, absolute) + sum_squared += delta * delta + rmse = math.sqrt(sum_squared / len(reference)) + psnr = 999.0 if not rmse else 20 * math.log10(255 / rmse) + return {"psnr_db": psnr, "rmse_8bit": rmse, "maximum_error_8bit": maximum} + + +class BntxConverter: + def __init__(self, library: Path, encoder: Path, scratch: Path, cache: Path, + preset: str, jobs: int, minimum_psnr: float, maximum_rmse: float): + self.library_path, self.encoder = library, encoder + self.scratch, self.cache = scratch, cache + self.preset, self.jobs = preset, jobs + self.minimum_psnr, self.maximum_rmse = minimum_psnr, maximum_rmse + self.encoder_sha256 = sha256_file(encoder) + self.lib = C.CDLL(str(library.resolve())) + self.lib.nvn_tex_layout.argtypes = [C.POINTER(TextureDesc), C.POINTER(TextureLayout)] + self.lib.nvn_tex_level_offset.argtypes = [C.POINTER(TextureDesc), C.c_uint] + self.lib.nvn_tex_level_offset.restype = C.c_uint64 + self.lib.nvn_deswizzle.argtypes = [C.POINTER(TextureDesc), C.c_uint, C.c_uint, + C.c_void_p, C.c_void_p, C.c_size_t] + self.lib.nvn_deswizzle.restype = C.c_int + self.lib.nvn_swizzle.argtypes = [C.POINTER(TextureDesc), C.c_uint, C.c_uint, + C.c_void_p, C.c_size_t, C.c_void_p] + self.lib.nvn_swizzle.restype = C.c_int + self.lib.nvn_bc_decode.argtypes = [C.c_uint, C.c_void_p, C.c_size_t, C.c_uint, + C.c_uint, C.c_void_p, C.c_size_t] + self.lib.nvn_bc_decode.restype = C.c_int + cache.mkdir(parents=True, exist_ok=True) + + def convert_texture(self, data: bytes, texture: dict, label: str) -> tuple[bytes, dict]: + at = texture["bntx_base"] + texture["data_offset"] + original = data[at:at + texture["image_bytes"]] + recipe = { + "source_sha256": sha256_bytes(original), + "format": texture["format"], "width": texture["width"], "height": texture["height"], + "layers": texture["layers"], "mips": texture["mips"], + "block_height_log2": texture["block_height_log2"], + "encoder_sha256": self.encoder_sha256, "preset": self.preset, + "block": "4x4", "minimum_psnr_db": self.minimum_psnr, + "maximum_rmse_8bit": self.maximum_rmse, + } + key = sha256_bytes(json.dumps(recipe, sort_keys=True).encode()) + payload_cache, report_cache = self.cache / f"{key}.bin", self.cache / f"{key}.json" + if payload_cache.is_file() and report_cache.is_file(): + payload, report = payload_cache.read_bytes(), json.loads(report_cache.read_text()) + if len(payload) == len(original) and report.get("payload_sha256") == sha256_bytes(payload): + return payload, report + + if texture["format"] not in BC7_TO_ASTC or texture["depth"] != 1 or texture["tile_mode"] != 0: + raise FormatError(f"unsupported target geometry in {label}") + nvn_format = 0x4E if texture["format"] == 0x2006 else 0x4D + desc = TextureDesc(4 if texture["layers"] > 1 else 1, nvn_format, 0, 1, + texture["width"], texture["height"], texture["layers"], + texture["mips"], 0, texture["block_height_log2"] + 1) + layout = TextureLayout() + self.lib.nvn_tex_layout(C.byref(desc), C.byref(layout)) + if layout.size != len(original): + raise FormatError(f"NVN storage size mismatch in {label}: {layout.size} != {len(original)}") + offsets = [self.lib.nvn_tex_level_offset(C.byref(desc), level) + for level in range(texture["mips"])] + if offsets != texture["mip_offsets"]: + raise FormatError(f"NVN mip layout mismatch in {label}") + + storage = C.create_string_buffer(original) + result = C.create_string_buffer(original) + quality = [] + profile = "s" if texture["format"] == 0x2006 else "l" + for layer in range(texture["layers"]): + for level in range(texture["mips"]): + width = max(1, texture["width"] >> level) + height = max(1, texture["height"] >> level) + pitch = ((width + 3) // 4) * 16 + block_bytes = pitch * ((height + 3) // 4) + bc = C.create_string_buffer(block_bytes) + if self.lib.nvn_deswizzle(C.byref(desc), level, layer, storage, bc, pitch): + raise FormatError(f"NVN deswizzle failed in {label}") + pixels = C.create_string_buffer(width * height * 4) + if self.lib.nvn_bc_decode(nvn_format, bc, pitch, width, height, pixels, width * 4): + raise FormatError(f"BC7 decode failed in {label}") + rgba = pixels.raw[:width * height * 4] + stem = f"t{texture['texture_index']}-l{layer}-m{level}" + source_path = self.scratch / f"{stem}.tga" + astc_path = self.scratch / f"{stem}.astc" + decoded_path = self.scratch / f"{stem}-decoded.tga" + write_tga(source_path, width, height, rgba) + subprocess.run([str(self.encoder), f"-c{profile}", str(source_path), str(astc_path), + "4x4", f"-{self.preset}", "-j", str(self.jobs), "-silent"], + check=True, capture_output=True) + subprocess.run([str(self.encoder), f"-d{profile}", str(astc_path), str(decoded_path), + "-silent"], check=True, capture_output=True) + decoded_width, decoded_height, decoded = read_tga_rgba(decoded_path) + if (decoded_width, decoded_height) != (width, height): + raise FormatError(f"ASTC readback dimensions differ in {label}") + metrics = rgba_metrics(rgba, decoded) + metrics.update({"layer": layer, "mip": level}) + if metrics["psnr_db"] < self.minimum_psnr or metrics["rmse_8bit"] > self.maximum_rmse: + raise QualityError( + f"{label} layer={layer} mip={level} PSNR={metrics['psnr_db']:.2f} " + f"RMSE={metrics['rmse_8bit']:.3f}") + encoded = astc_path.read_bytes() + if encoded[:7] != b"\x13\xab\xa1\x5c\x04\x04\x01" or len(encoded) != 16 + block_bytes: + raise FormatError(f"ASTC encoder wrote an invalid 4x4 stream in {label}") + blocks = C.create_string_buffer(encoded[16:]) + if self.lib.nvn_swizzle(C.byref(desc), level, layer, blocks, pitch, result): + raise FormatError(f"NVN swizzle failed in {label}") + check = C.create_string_buffer(block_bytes) + if self.lib.nvn_deswizzle(C.byref(desc), level, layer, result, check, pitch): + raise FormatError(f"NVN ASTC readback failed in {label}") + if check.raw != encoded[16:]: + raise FormatError(f"NVN ASTC swizzle round trip differs in {label}") + quality.append(metrics) + source_path.unlink(missing_ok=True) + astc_path.unlink(missing_ok=True) + decoded_path.unlink(missing_ok=True) + payload = result.raw[:len(original)] + report = {"recipe": recipe, "payload_sha256": sha256_bytes(payload), + "minimum_psnr_db": min(x["psnr_db"] for x in quality), + "maximum_rmse_8bit": max(x["rmse_8bit"] for x in quality), + "subresources": quality} + payload_cache.with_suffix(".bin.partial").write_bytes(payload) + payload_cache.with_suffix(".bin.partial").replace(payload_cache) + report_cache.with_suffix(".json.partial").write_text(json.dumps(report, indent=2) + "\n") + report_cache.with_suffix(".json.partial").replace(report_cache) + return payload, report + + def convert_blob(self, raw: bytes, label: str) -> tuple[bytes, list[dict], list[dict]]: + result = bytearray(raw) + converted, retained = [], [] + for texture in bntx_textures(raw): + if texture["format"] not in BC7_TO_ASTC: + continue + try: + payload, quality = self.convert_texture(raw, texture, label) + except (FormatError, QualityError, subprocess.CalledProcessError) as exc: + retained.append({"texture": texture, "reason": str(exc)}) + continue + at = texture["bntx_base"] + texture["data_offset"] + result[at:at + len(payload)] = payload + format_at = texture["bntx_base"] + texture["brti_offset"] + 0x1C + struct.pack_into(" bytes: + start, size = record["payload_offset"], record["payload_capacity"] + payload = bytes(data[start:start + size]) + if record["compression"] == 3: + return decoder.decode(payload, record["raw_size"]) + # Trinity uses both an absent/default compression field and -1 for stored + # records. Size equality is the authoritative stored-file invariant used + # by the engine's existing shader scanner. + if size == record["raw_size"]: + return payload + raise FormatError( + f"unsupported compression {record['compression']} for pack {record['pack_index']} " + f"file {record['file_index']} ({size} -> {record['raw_size']})") + + +def bc7_file_selection(path: Path) -> set[tuple[int, int]]: + """Read pack/file identities from a census or append-relocated manifest.""" + document = json.loads(path.read_text()) + wanted = { + (row["pack_index"], row["file_index"]) + for row in document.get("rows", []) + if row.get("format") in BC7_TO_ASTC + } + if document.get("mode") in ("append-relocated-packs", + "insert-relocated-packs-before-root", + "virtual-pack-layout-before-root"): + for group in ("converted", "quality_retained"): + for row in document.get(group, []): + texture, record = row.get("texture", {}), row.get("record", {}) + if texture.get("format") in BC7_TO_ASTC: + wanted.add((record["pack_index"], record["file_index"])) + if not wanted: + raise ValueError(f"selection contains no BC7 records: {path}") + return wanted + + +def selected_records(records: list[dict], args) -> list[dict]: + selected = records + if getattr(args, "census", None) is not None: + wanted = bc7_file_selection(args.census) + selected = [x for x in selected if (x["pack_index"], x["file_index"]) in wanted] + print(f"BC7_SELECTED {len(wanted)} files from {args.census}", flush=True) + if getattr(args, "fslog", None) is not None: + touched = fslog_pack_indices(records, args.fslog, getattr(args, "fslog_before", None)) + selected = [x for x in selected if x["pack_index"] in touched] + print(f"FSLOG_SELECTED {len(touched)} packs from {args.fslog}", flush=True) + if getattr(args, "pack_index", None) is not None: + wanted = set(args.pack_index) + selected = [x for x in selected if x["pack_index"] in wanted] + if getattr(args, "file_index", None) is not None: + selected = [x for x in selected if x["file_index"] == args.file_index] + if getattr(args, "limit_packs", 0): + packs = sorted({x["pack_index"] for x in selected})[:args.limit_packs] + selected = [x for x in selected if x["pack_index"] in set(packs)] + return selected + + +def fslog_pack_indices(records: list[dict], path: Path, before: float | None = None) -> set[int]: + """Map DM_FSLOG data.trpfs read ranges back to descriptor-sized packs.""" + by_pack = {} + for record in records: + if record["pack_size"] is not None: + by_pack.setdefault(record["pack_index"], + (record["pack_offset"], record["pack_offset"] + record["pack_size"])) + intervals = sorted((start, end, pack_index) for pack_index, (start, end) in by_pack.items()) + starts = [row[0] for row in intervals] + touched = set() + for number, line in enumerate(path.read_text(errors="replace").splitlines(), 1): + fields = line.split("\t") + if len(fields) < 4 or not fields[1].replace("\\", "/").endswith("/arc/data.trpfs"): + continue + try: + seconds, offset, length = float(fields[0]), int(fields[2], 0), int(fields[3], 0) + except ValueError as exc: + raise FormatError(f"invalid DM_FSLOG row {number}") from exc + if before is not None and seconds > before: + continue + if offset < 0 or length <= 0: + continue + end = offset + length + at = max(0, bisect.bisect_right(starts, offset) - 1) + while at < len(intervals) and intervals[at][0] < end: + start, pack_end, pack_index = intervals[at] + if pack_end > offset: + touched.add(pack_index) + at += 1 + if not touched: + raise ValueError(f"DM_FSLOG contains no reads intersecting a Trinity pack: {path}") + return touched + + +def archive_and_records(args): + archive = args.archive.resolve() + descriptor = (args.descriptor or archive.with_suffix(".trpfd")).resolve() + if not archive.is_file() or not descriptor.is_file(): + raise FileNotFoundError("data.trpfs and data.trpfd are both required") + catalog = descriptor_catalog(descriptor) + source = archive.open("rb") + mapping = mmap.mmap(source.fileno(), 0, access=mmap.ACCESS_READ) + packs, records = trpfs_records(mapping, catalog["names_by_hash"], catalog) + return archive, descriptor, source, mapping, packs, records, catalog + + +def census(args) -> None: + ensure_private_path(args.output) + args.output.parent.mkdir(parents=True, exist_ok=True) + archive, descriptor, source, data, packs, records, catalog = archive_and_records(args) + records = selected_records(records, args) + work = args.output.with_suffix(args.output.suffix + ".partial-work") + if work.exists(): + raise FileExistsError(work) + work.mkdir() + formats = collections.Counter() + image_bytes = collections.Counter() + rows, errors = [], [] + started = time.time() + try: + with GuestDecoder(args.host.resolve(), args.modules.resolve(), work / "decoder.log") as decoder: + for number, record in enumerate(records, 1): + try: + raw = decode_record(data, record, decoder) + textures = bntx_textures(raw) + for texture in textures: + formats[texture["format_name"]] += 1 + image_bytes[texture["format_name"]] += texture["image_bytes"] + rows.append({**{k: record[k] for k in ("pack_index", "pack_offset", "pack_name", + "file_index", "file_hash", "compression", + "compression_level", "raw_size", + "payload_offset", "payload_capacity")}, **texture}) + except (FormatError, RuntimeError, struct.error) as exc: + errors.append({"record": record, "reason": str(exc)}) + if number % 100 == 0: + print(f"CENSUS {number}/{len(records)} files, {len(rows)} textures", flush=True) + result = { + "schema": 1, "mode": "read-only-census", "archive_name": archive.name, + "archive_bytes": archive.stat().st_size, "descriptor_name": descriptor.name, + "packs_total": len(packs), "files_selected": len(records), "texture_instances": len(rows), + "formats": {name: {"textures": formats[name], "image_bytes": image_bytes[name]} + for name in sorted(formats)}, + "errors": errors, "rows": rows, "elapsed_seconds": time.time() - started, + "source_archive_modified": False, + } + args.output.with_suffix(args.output.suffix + ".partial").write_text(json.dumps(result, indent=2) + "\n") + args.output.with_suffix(args.output.suffix + ".partial").replace(args.output) + print(json.dumps({k: v for k, v in result.items() if k not in ("rows", "errors")}, indent=2)) + print(f"CENSUS_OUTPUT {args.output}") + finally: + data.close() + source.close() + shutil.rmtree(work, ignore_errors=True) + + +def make_patch_bundle(args, all_selected: bool) -> None: + ensure_private_path(args.output) + ensure_private_path(args.cache) + if args.output.exists() or args.output.with_name(args.output.name + ".partial").exists(): + raise FileExistsError(f"refusing to overwrite output: {args.output}") + work = args.output.with_name(args.output.name + ".partial") + work.mkdir(parents=True) + (work / "patches").mkdir() + (work / "evidence").mkdir() + args.cache.mkdir(parents=True, exist_ok=True) + archive, descriptor, source, data, packs, records, catalog = archive_and_records(args) + records = selected_records(records, args) + if not all_selected and len(records) != 1: + raise ValueError("prove requires exactly one selected file; pass --pack-index and --file-index") + patch_rows, converted_rows, retained_rows, skipped_rows = [], [], [], [] + helper_tmp = tempfile.TemporaryDirectory(prefix="za-astc-helper-") + try: + library = args.library.resolve() if args.library else Path(helper_tmp.name) / "libza_texture_tools.dylib" + if not args.library: + build_texture_library(library) + scratch = Path(helper_tmp.name) / "scratch" + scratch.mkdir() + converter = BntxConverter(library, args.astcenc.resolve(), scratch, args.cache.resolve(), + args.preset, args.jobs, args.minimum_psnr, args.maximum_rmse) + encoder = GuestEncoder(args.host.resolve(), args.modules.resolve(), work, + work / "encoder.log", args.oodle_compressor, args.oodle_level) + with GuestDecoder(args.host.resolve(), args.modules.resolve(), work / "decoder.log") as decoder: + for number, record in enumerate(records, 1): + tag = f"p{record['pack_index']}-f{record['file_index']}" + try: + if record["compression"] != 3: + raise FormatError("stored/non-Oodle files cannot receive an Oodle payload in place") + raw = decode_record(data, record, decoder) + original_textures = bntx_textures(raw) + if not any(x["format"] in BC7_TO_ASTC for x in original_textures): + skipped_rows.append({"record": record, "reason": "no eligible BC7 texture"}) + continue + converted_raw, converted, retained = converter.convert_blob(raw, tag) + retained_rows.extend({"record": record, **row} for row in retained) + if converted_raw == raw: + skipped_rows.append({"record": record, "reason": "all eligible textures retained"}) + continue + compressed = encoder.encode(converted_raw, tag) + if len(compressed) > record["payload_capacity"]: + skipped_rows.append({"record": record, "reason": "recompressed payload exceeds vector capacity", + "compressed_bytes": len(compressed), + "capacity": record["payload_capacity"]}) + continue + verified = decoder.decode(compressed, len(converted_raw)) + if verified != converted_raw: + raise RuntimeError("independent guest decoder round trip differs") + after_textures = bntx_textures(verified) + converted_textures = bntx_textures(converted_raw) + if ([x["format"] for x in after_textures] != + [x["format"] for x in converted_textures]): + raise FormatError("post-compression BNTX format verifier failed") + patch_offset = record["payload_offset"] - 4 + capacity = record["payload_capacity"] + original_range = bytes(data[patch_offset:record["payload_offset"] + capacity]) + replacement = (struct.pack(" {len(compressed)} bytes, " + f"{len(converted)} textures", flush=True) + except (FormatError, RuntimeError, ValueError, struct.error, + subprocess.CalledProcessError) as exc: + skipped_rows.append({"record": record, "reason": str(exc)}) + if all_selected and number % 50 == 0: + print(f"PREPARE {number}/{len(records)} files, {len(patch_rows)} patches", flush=True) + # Patch ranges must be disjoint and wholly within the immutable source. + patch_rows.sort(key=lambda row: row["offset"]) + previous_end = 0 + for row in patch_rows: + if row["offset"] < previous_end or row["offset"] + row["bytes"] > len(data): + raise AssertionError("patch ranges overlap or leave the source archive") + previous_end = row["offset"] + row["bytes"] + manifest = { + "schema": 1, "game": "0100F43008C44000", "archive_name": archive.name, + "archive_bytes": archive.stat().st_size, "archive_sha256": sha256_file(archive), + "descriptor_name": descriptor.name, "policy": ( + "BC7_UNORM/BC7_sRGB to ASTC4x4 only; preserve storage layout; retain any texture " + "that fails geometry/quality; skip any Oodle stream larger than its original vector"), + "patches": patch_rows, "converted_texture_instances": len(converted_rows), + "quality_retained_texture_instances": len(retained_rows), + "skipped_files": skipped_rows, "converted": converted_rows, "quality_retained": retained_rows, + "source_archive_modified": False, "descriptor_modified": False, + "neighboring_pack_bytes_modified": False, "ready_to_apply_to_verified_clone": bool(patch_rows), + "astcenc_sha256": converter.encoder_sha256, + "oodle_compressor": args.oodle_compressor, "oodle_level": args.oodle_level, + "minimum_psnr_db": args.minimum_psnr, "maximum_rmse_8bit": args.maximum_rmse, + "astcenc_preset": args.preset, + } + (work / "manifest.json").write_text(json.dumps(manifest, indent=2) + "\n") + work.replace(args.output) + print(json.dumps({k: v for k, v in manifest.items() + if k not in ("patches", "converted", "quality_retained", "skipped_files")}, indent=2)) + print(f"PATCH_BUNDLE {args.output}") + except Exception: + (work / "FAILED").write_text("The partial directory is not an installable patch bundle.\n") + raise + finally: + helper_tmp.cleanup() + data.close() + source.close() + + +def relocated_pack_bytes(data, pack: dict, replacements: list[dict]) -> bytes: + """Copy one pack and point selected file tables at newly appended vectors.""" + if pack["pack_size"] is None or pack["descriptor_size_field"] is None: + raise FormatError("pack has no descriptor size metadata") + start, size = pack["pack_offset"], pack["pack_size"] + result = bytearray(data[start:start + size]) + if len(result) != size: + raise FormatError("source pack is truncated") + root = fb_root(result) + tables = fb_refs(result, root, 1) + if len(tables) != pack["file_count"]: + raise FormatError("relocated source pack file count differs") + seen = set() + for item in sorted(replacements, key=lambda row: row["record"]["file_index"]): + record = item["record"] + compressed = (item["compressed"] if "compressed" in item else + Path(item["compressed_file"]).read_bytes()) + file_index = record["file_index"] + if file_index in seen or file_index >= len(tables): + raise FormatError("relocated pack has a duplicate/invalid file index") + seen.add(file_index) + field_absolute = record["payload_field_offset"] + if field_absolute is None: + raise FormatError("relocated file has no payload-vector field") + field = field_absolute - start + if field < 0 or field + 4 > size or tables[file_index] + 4 > size: + raise FormatError("relocated file table leaves its source pack") + vector = align_up(len(result), 4) + result.extend(bytes(vector - len(result))) + relative = vector - field + if relative <= 0 or relative > 0xFFFFFFFF: + raise FormatError("relocated payload cannot be represented as a FlatBuffer uoffset") + result.extend(struct.pack(" len(result) or bytes(result[payload_at:payload_at + payload_size]) != compressed: + raise FormatError("relocated payload-vector verifier failed") + return bytes(result) + + +def make_relocated_bundle(args) -> None: + """Create replacement packs plus a virtual sequential archive mapping.""" + ensure_private_path(args.output) + ensure_private_path(args.cache) + work = args.output.with_name(args.output.name + ".partial") + if args.output.exists() or work.exists(): + raise FileExistsError(f"refusing to overwrite output: {args.output}") + args.output.parent.mkdir(parents=True, exist_ok=True) + work.mkdir() + (work / "evidence").mkdir() + (work / "streams").mkdir() + args.cache.mkdir(parents=True, exist_ok=True) + archive = descriptor = source = data = catalog = None + helper_tmp = tempfile.TemporaryDirectory(prefix="za-astc-helper-") + try: + archive, descriptor, source, data, packs, records, catalog = archive_and_records(args) + selected = selected_records(records, args) + if not selected: + raise ValueError("relocate selection contains no files") + proof_mode = len(selected) == 1 + pack_by_index = {pack["pack_index"]: pack for pack in packs} + library = args.library.resolve() if args.library else Path(helper_tmp.name) / "libza_texture_tools.dylib" + if not args.library: + build_texture_library(library) + scratch = Path(helper_tmp.name) / "scratch" + scratch.mkdir() + converter = BntxConverter(library, args.astcenc.resolve(), scratch, args.cache.resolve(), + args.preset, args.jobs, args.minimum_psnr, args.maximum_rmse) + encoder = GuestEncoder(args.host.resolve(), args.modules.resolve(), work, + work / "encoder.log", args.oodle_compressor, args.oodle_level) + prepared = collections.defaultdict(list) + converted_rows, retained_rows, skipped_rows = [], [], [] + with GuestDecoder(args.host.resolve(), args.modules.resolve(), work / "decoder.log") as decoder: + for number, record in enumerate(selected, 1): + tag = f"p{record['pack_index']}-f{record['file_index']}" + try: + if record["compression"] != 3: + raise FormatError("relocation currently accepts only existing Oodle records") + raw = decode_record(data, record, decoder) + original_textures = bntx_textures(raw) + if not any(x["format"] in BC7_TO_ASTC for x in original_textures): + skipped_rows.append({"record": record, "reason": "no eligible BC7 texture"}) + continue + converted_raw, converted, retained = converter.convert_blob(raw, tag) + retained_rows.extend({"record": record, **row} for row in retained) + if converted_raw == raw: + skipped_rows.append({"record": record, "reason": "all eligible textures retained"}) + continue + compressed = encoder.encode(converted_raw, tag) + verified = decoder.decode(compressed, len(converted_raw)) + if verified != converted_raw: + raise RuntimeError("independent guest decoder round trip differs") + expected = [x["format"] for x in bntx_textures(converted_raw)] + if [x["format"] for x in bntx_textures(verified)] != expected: + raise FormatError("post-compression BNTX format verifier failed") + stream_path = work / "streams" / f"{tag}.oodle" + stream_path.write_bytes(compressed) + item = {"record": record, "compressed_file": str(stream_path)} + prepared[record["pack_index"]].append(item) + converted_rows.extend({"record": record, **row} for row in converted) + if proof_mode: + (work / "evidence/original.raw").write_bytes(raw) + (work / "evidence/converted.raw").write_bytes(converted_raw) + (work / "evidence/recompressed.oodle").write_bytes(compressed) + print(f"RELOCATE_READY {tag}: {record['payload_capacity']} -> " + f"{len(compressed)} bytes, {len(converted)} textures", flush=True) + except (FormatError, RuntimeError, ValueError, struct.error, + subprocess.CalledProcessError) as exc: + skipped_rows.append({"record": record, "reason": str(exc)}) + if number % 50 == 0: + print(f"RELOCATE {number}/{len(selected)} files", flush=True) + + archive_edits, descriptor_edits, relocated = [], [], [] + archive_bytes = archive.stat().st_size + if data[:8] != ONEPACK: + raise FormatError("archive does not start with ONEPACK\\0") + root_offset = struct.unpack_from(" 64 << 20: + raise FormatError("archive root tail is unexpectedly large") + descriptor_result = bytearray(catalog["bytes"]) + append_name = "append.bin" + append_path = work / append_name + with append_path.open("wb") as append_stream: + for pack_index in sorted(prepared): + pack = pack_by_index[pack_index] + replacement_pack = relocated_pack_bytes(data, pack, prepared[pack_index]) + absolute = align_up(root_offset + append_stream.tell(), 16) + append_stream.write(bytes(absolute - (root_offset + append_stream.tell()))) + append_relative = append_stream.tell() + append_stream.write(replacement_pack) + replacement_span = align_up(len(replacement_pack), 16) + append_stream.write(bytes(replacement_span - len(replacement_pack))) + old_offset = bytes(data[pack["pack_offset_field"]:pack["pack_offset_field"] + 8]) + if old_offset != struct.pack(" str: + libc = C.CDLL(None, use_errno=True) + clone = getattr(libc, "clonefile", None) + if clone is not None: + clone.argtypes = [C.c_char_p, C.c_char_p, C.c_uint32] + clone.restype = C.c_int + if clone(os.fsencode(source), os.fsencode(destination), 0) == 0: + return "clonefile" + error = C.get_errno() + if error not in (errno.ENOTSUP, errno.EXDEV, errno.EINVAL): + raise OSError(error, os.strerror(error), str(destination)) + if not allow_copy: + raise OSError("APFS clonefile is unavailable; pass --allow-full-copy to permit a full copy") + shutil.copyfile(source, destination) + return "full-copy" + + +def apply_bundle(args) -> None: + bundle, source, destination = args.bundle.resolve(), args.archive.resolve(), args.output.resolve() + ensure_private_path(destination) + manifest = json.loads((bundle / "manifest.json").read_text()) + if destination.exists() or destination.with_name(destination.name + ".partial").exists(): + raise FileExistsError(f"refusing to overwrite output: {destination}") + if source.stat().st_size != manifest["archive_bytes"] or sha256_file(source) != manifest["archive_sha256"]: + raise ValueError("source archive identity does not match patch manifest") + patches = manifest["patches"] + with source.open("rb") as stream: + for row in patches: + stream.seek(row["offset"]) + expected = stream.read(row["bytes"]) + if sha256_bytes(expected) != row["source_sha256"]: + raise ValueError(f"source range differs at {row['offset']:#x}") + destination.parent.mkdir(parents=True, exist_ok=True) + partial = destination.with_name(destination.name + ".partial") + method = clone_or_copy(source, partial, args.allow_full_copy) + try: + with partial.open("r+b") as output: + for row in patches: + replacement = (bundle / row["patch"]).read_bytes() + if len(replacement) != row["bytes"] or sha256_bytes(replacement) != row["replacement_sha256"]: + raise ValueError(f"patch payload is invalid: {row['patch']}") + output.seek(row["offset"]) + output.write(replacement) + output.flush() + os.fsync(output.fileno()) + with partial.open("rb") as output: + for row in patches: + output.seek(row["offset"]) + if sha256_bytes(output.read(row["bytes"])) != row["replacement_sha256"]: + raise RuntimeError(f"written patch did not verify at {row['offset']:#x}") + partial.replace(destination) + except Exception: + partial.unlink(missing_ok=True) + raise + result = {"output": str(destination), "bytes": destination.stat().st_size, + "patches": len(patches), "creation_method": method, + "source_unchanged_sha256": sha256_file(source), + "output_sha256": sha256_file(destination)} + print(json.dumps(result, indent=2)) + + +def apply_relocated_bundle(args) -> None: + """Publish a verified trpfs/trpfd pair as one atomically renamed directory.""" + bundle = args.bundle.resolve() + source_archive = args.archive.resolve() + source_descriptor = args.descriptor.resolve() + output = args.output.resolve() + ensure_private_path(output) + manifest_bytes = (bundle / "manifest.json").read_bytes() + manifest = json.loads(manifest_bytes) + if (manifest.get("schema") != 4 or + manifest.get("mode") != "virtual-pack-layout-before-root" or + not manifest.get("ready_to_apply_to_new_output_directory")): + raise ValueError("bundle is not a ready virtual-layout relocated manifest") + if output.exists() or output.with_name(output.name + ".partial").exists(): + raise FileExistsError(f"refusing to overwrite output: {output}") + if (source_archive.stat().st_size != manifest["archive_bytes"] or + sha256_file(source_archive) != manifest["archive_sha256"]): + raise ValueError("source archive identity does not match relocated manifest") + source_descriptor_bytes = source_descriptor.read_bytes() + if (len(source_descriptor_bytes) != manifest["descriptor_bytes"] or + sha256_bytes(source_descriptor_bytes) != manifest["descriptor_sha256"]): + raise ValueError("source descriptor identity does not match relocated manifest") + append_path = bundle / manifest["append"]["file"] + if (append_path.stat().st_size != manifest["append"]["bytes"] or + sha256_file(append_path) != manifest["append"]["sha256"]): + raise ValueError("relocated append payload identity differs") + redirect_meta = manifest.get("redirects") + if (not isinstance(redirect_meta, dict) or + Path(str(redirect_meta.get("file", ""))).name != redirect_meta.get("file")): + raise ValueError("redirect table metadata is invalid") + redirect_path = bundle / redirect_meta["file"] + redirect_data = redirect_path.read_bytes() + if (len(redirect_data) != redirect_meta.get("bytes") or + sha256_bytes(redirect_data) != redirect_meta.get("sha256")): + raise ValueError("redirect table identity differs") + parsed_redirects = parse_redirect_table(redirect_data) + archive_bytes = manifest["archive_bytes"] + append_bytes = manifest["append"]["bytes"] + root_offset = manifest.get("archive_root_offset") + root_bytes = manifest.get("archive_root_bytes") + final_root_offset = manifest.get("final_archive_root_offset") + final_archive_bytes = manifest.get("final_archive_bytes") + virtual_root_offset = manifest.get("virtual_archive_root_offset") + virtual_archive_bytes = manifest.get("virtual_archive_bytes") + if (not isinstance(root_offset, int) or not isinstance(root_bytes, int) or + not isinstance(final_root_offset, int) or not isinstance(final_archive_bytes, int) or + not isinstance(virtual_root_offset, int) or not isinstance(virtual_archive_bytes, int) or + root_offset < 16 or root_bytes <= 0 or root_offset + root_bytes != archive_bytes or + final_root_offset != root_offset + append_bytes or + final_archive_bytes != archive_bytes + append_bytes or + virtual_root_offset + root_bytes != virtual_archive_bytes or + parsed_redirects["target"] != redirect_meta.get("target") or + len(parsed_redirects["records"]) != redirect_meta.get("records") or + parsed_redirects["virtual_file_bytes"] != virtual_archive_bytes or + parsed_redirects["physical_file_bytes"] != final_archive_bytes or + manifest["append"].get("output_offset") != root_offset): + raise ValueError("relocated archive layout is invalid") + with source_archive.open("rb") as stream: + stream.seek(root_offset) + source_root = read_exact(stream, root_bytes) + if sha256_bytes(source_root) != manifest.get("archive_root_sha256"): + raise ValueError("source archive root identity differs") + source_rows = manifest["archive_header_edits"] + manifest["archive_index_edits"] + previous_source_end = previous_output_end = -1 + for row in sorted(source_rows, key=lambda item: item["source_offset"]): + source_offset, output_offset, count = row["source_offset"], row["output_offset"], row["bytes"] + if (count <= 0 or source_offset < 0 or source_offset + count > archive_bytes or + source_offset < previous_source_end): + raise ValueError("source archive edit ranges overlap or leave the source") + previous_source_end = source_offset + count + with source_archive.open("rb") as stream: + stream.seek(source_offset); original = stream.read(count) + if (original.hex() != row["source_hex"] or + sha256_bytes(original) != row["source_sha256"]): + raise ValueError(f"source archive edit range differs at {source_offset:#x}") + replacement = bytes.fromhex(row["replacement_hex"]) + if len(replacement) != count or sha256_bytes(replacement) != row["replacement_sha256"]: + raise ValueError(f"archive replacement differs at {output_offset:#x}") + for row in manifest["archive_header_edits"]: + if row["source_offset"] >= root_offset or row["output_offset"] != row["source_offset"]: + raise ValueError("archive header edit is not inside the unchanged prefix") + for row in manifest["archive_index_edits"]: + if (row["source_offset"] < root_offset or + row["output_offset"] != row["source_offset"] + append_bytes): + raise ValueError("archive index edit does not follow the shifted root") + for row in sorted(source_rows, key=lambda item: item["output_offset"]): + output_offset, count = row["output_offset"], row["bytes"] + if output_offset < previous_output_end or output_offset + count > final_archive_bytes: + raise ValueError("output archive edit ranges overlap or leave the output") + previous_output_end = output_offset + count + for row in manifest["descriptor_size_edits"]: + original = source_descriptor_bytes[row["offset"]:row["offset"] + row["bytes"]] + if (original.hex() != row["source_hex"] or + sha256_bytes(original) != row["source_sha256"]): + raise ValueError(f"source descriptor edit range differs at {row['offset']:#x}") + + output.parent.mkdir(parents=True, exist_ok=True) + partial = output.with_name(output.name + ".partial") + partial.mkdir() + journal = partial / ".apply-journal.json" + published = False + + def write_journal(stage: str, **extra) -> None: + state = { + "schema": 1, "stage": stage, + "manifest_sha256": sha256_bytes(manifest_bytes), + "source_archive": str(source_archive), "source_descriptor": str(source_descriptor), + "rollback": "remove this unpublished partial directory; both sources are immutable", + **extra, + } + temporary = journal.with_suffix(".json.partial") + temporary.write_text(json.dumps(state, indent=2) + "\n") + with temporary.open("rb") as stream: + os.fsync(stream.fileno()) + temporary.replace(journal) + fsync_directory(partial) + + try: + write_journal("created") + output_archive = partial / manifest["archive_name"] + output_descriptor = partial / manifest["descriptor_name"] + output_redirects = partial / redirect_meta["file"] + creation_method = clone_or_copy(source_archive, output_archive, args.allow_full_copy) + shutil.copyfile(source_descriptor, output_descriptor) + shutil.copyfile(redirect_path, output_redirects) + with output_redirects.open("rb") as stream: + os.fsync(stream.fileno()) + write_journal("cloned", archive_creation_method=creation_method) + with output_archive.open("r+b") as stream, source_archive.open("rb") as source_stream: + stream.truncate(final_archive_bytes) + source_stream.seek(root_offset) + stream.seek(final_root_offset) + left = root_bytes + while left: + block = read_exact(source_stream, min(left, 8 << 20)) + stream.write(block); left -= len(block) + stream.seek(root_offset) + with append_path.open("rb") as append_stream: + shutil.copyfileobj(append_stream, stream, length=8 << 20) + for row in manifest["archive_header_edits"] + manifest["archive_index_edits"]: + replacement = bytes.fromhex(row["replacement_hex"]) + if len(replacement) != row["bytes"] or sha256_bytes(replacement) != row["replacement_sha256"]: + raise ValueError("archive replacement differs from manifest") + stream.seek(row["output_offset"]) + stream.write(replacement) + stream.flush() + os.fsync(stream.fileno()) + with output_descriptor.open("r+b") as stream: + for row in manifest["descriptor_size_edits"]: + replacement = bytes.fromhex(row["replacement_hex"]) + if len(replacement) != row["bytes"] or sha256_bytes(replacement) != row["replacement_sha256"]: + raise ValueError("descriptor replacement differs from manifest") + stream.seek(row["offset"]) + stream.write(replacement) + stream.flush() + os.fsync(stream.fileno()) + write_journal("written", archive_creation_method=creation_method) + with output_archive.open("rb") as stream: + stream.seek(root_offset) + if sha256_bytes(read_exact(stream, append_bytes)) != manifest["append"]["sha256"]: + raise RuntimeError("inserted append payload verification failed") + stream.seek(final_root_offset) + if sha256_bytes(read_exact(stream, root_bytes)) != manifest["final_archive_root_sha256"]: + raise RuntimeError("shifted archive root verification failed") + got_archive = sha256_file(output_archive) + got_descriptor = sha256_file(output_descriptor) + if (got_archive != manifest["final_archive_sha256"] or + got_descriptor != manifest["final_descriptor_sha256"] or + sha256_file(output_redirects) != redirect_meta["sha256"]): + raise RuntimeError("relocated output full-file verification failed") + marker = { + "schema": 1, "manifest_sha256": sha256_bytes(manifest_bytes), + "archive_sha256": got_archive, "descriptor_sha256": got_descriptor, + "redirects_sha256": sha256_bytes(redirect_data), + "relocated_packs": len(manifest["relocated_packs"]), + } + (partial / "READY.json").write_text(json.dumps(marker, indent=2) + "\n") + with (partial / "READY.json").open("rb") as stream: + os.fsync(stream.fileno()) + write_journal("verified", archive_creation_method=creation_method) + fsync_directory(partial) + partial.replace(output) + fsync_directory(output.parent) + published = True + finally: + if not published: + shutil.rmtree(partial, ignore_errors=True) + print(json.dumps({ + "output_directory": str(output), "archive_creation_method": creation_method, + "archive_bytes": manifest["final_archive_bytes"], + "archive_sha256": manifest["final_archive_sha256"], + "descriptor_sha256": manifest["final_descriptor_sha256"], + "redirects_sha256": redirect_meta["sha256"], + "virtual_archive_bytes": virtual_archive_bytes, + "relocated_packs": len(manifest["relocated_packs"]), "atomic_ready_marker": True, + "sources_unchanged": True, + }, indent=2)) + + +def common_archive_args(parser) -> None: + parser.add_argument("archive", type=Path, help="user-owned arc/data.trpfs (opened read-only)") + parser.add_argument("--descriptor", type=Path, help="arc/data.trpfd (default: beside archive)") + parser.add_argument("--host", type=Path, required=True, help="Mac dmhost built for Z-A") + parser.add_argument("--modules", type=Path, required=True, help="directory containing patched main/sdk modules") + parser.add_argument("--pack-index", type=int, action="append", help="select a pack index (repeatable)") + parser.add_argument("--file-index", type=int, help="select this file index within selected packs") + parser.add_argument("--limit-packs", type=int, default=0, help="process only the first N selected packs") + parser.add_argument("--fslog", type=Path, + help="select packs intersected by reads in a DM_FSLOG TSV") + parser.add_argument("--fslog-before", type=float, + help="with --fslog, ignore reads after this many seconds since boot") + parser.add_argument("--census", type=Path, + help="select BC7 pack/file identities from a census or relocated manifest") + + +def conversion_args(parser) -> None: + common_archive_args(parser) + parser.add_argument("output", type=Path, help="new patch-bundle directory outside the repository") + parser.add_argument("--astcenc", type=Path, required=True, help="Arm astcenc 5.x executable") + parser.add_argument("--library", type=Path, help="prebuilt texture helper dylib (otherwise compiled with clang)") + parser.add_argument("--cache", type=Path, required=True, help="private converted-texture cache outside the repository") + parser.add_argument("--preset", choices=("fast", "medium", "thorough", "verythorough", "exhaustive"), default="medium") + parser.add_argument("--jobs", type=int, default=2, help="astcenc worker threads") + parser.add_argument("--minimum-psnr", type=float, default=40.0) + parser.add_argument("--maximum-rmse", type=float, default=2.55) + parser.add_argument("--oodle-compressor", type=int, default=9, help="9 = Mermaid") + parser.add_argument("--oodle-level", type=int, default=7) + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + commands = parser.add_subparsers(dest="command", required=True) + census_parser = commands.add_parser("census", help="read-only Trinity/BNTX inventory") + common_archive_args(census_parser) + census_parser.add_argument("output", type=Path, help="JSON output outside the repository") + prove_parser = commands.add_parser("prove", help="one-file end-to-end proof and exact patch") + conversion_args(prove_parser) + prepare_parser = commands.add_parser("prepare", help="prepare patches for all selected records") + conversion_args(prepare_parser) + relocate_parser = commands.add_parser( + "relocate", help="append copied replacement packs and emit exact trpfs/trpfd index edits") + conversion_args(relocate_parser) + apply_parser = commands.add_parser("apply", help="apply a bundle to a verified new clone/copy") + apply_parser.add_argument("bundle", type=Path) + apply_parser.add_argument("archive", type=Path, help="immutable source data.trpfs") + apply_parser.add_argument("output", type=Path, help="new cloned/patched data.trpfs") + apply_parser.add_argument("--allow-full-copy", action="store_true", + help="permit a 4.75 GB copy if APFS clonefile is unavailable") + relocated_apply = commands.add_parser( + "apply-relocated", help="publish a relocated trpfs/trpfd pair in a new directory") + relocated_apply.add_argument("bundle", type=Path) + relocated_apply.add_argument("archive", type=Path, help="immutable source data.trpfs") + relocated_apply.add_argument("descriptor", type=Path, help="immutable source data.trpfd") + relocated_apply.add_argument("output", type=Path, help="new directory containing the verified pair") + relocated_apply.add_argument("--allow-full-copy", action="store_true", + help="permit a 4.75 GB copy if APFS clonefile is unavailable") + args = parser.parse_args() + if args.command == "census": + census(args) + elif args.command == "prove": + make_patch_bundle(args, all_selected=False) + elif args.command == "prepare": + make_patch_bundle(args, all_selected=True) + elif args.command == "relocate": + make_relocated_bundle(args) + elif args.command == "apply": + apply_bundle(args) + else: + apply_relocated_bundle(args) + + +if __name__ == "__main__": + main() diff --git a/dm/tvos/README.md b/dm/tvos/README.md new file mode 100644 index 0000000000..b2724f36b1 --- /dev/null +++ b/dm/tvos/README.md @@ -0,0 +1,168 @@ +# Native tvOS development target + +This target reuses the host and locally supplied rewritten modules from a +prepared iOS project. It has its own UIKit/Metal/GameController front end and +does not change the iPad touch controls. It accepts a Siri Remote, the system +Apple TV Remote on an iPhone/iPad, or an extended gamepad. + +## Remote controls + +With the game visible, swipe/hold the touch surface or use the directional ring +to move, click to press A, and press Back/Menu to press B. Play/Pause opens the +full controls panel. Choose Camera stick or D-pad to change the directional +input; Resume / Move restores the left stick. The current mode is shown along +the bottom and is remembered between launches. + +The panel provides ABXY, L/R/ZL/ZR, +/− and both stick-click buttons. A normal +button sends one short press. Hold L/R/ZL/ZR switches retain a shoulder button +while other controls are used; select the switch again or Release held buttons +to release it. Back or Play/Pause closes the panel. All inputs, including held +shoulders, release when the app loses focus. The TV/Home button retains its +system behavior. This Remote scheme covers the full gamepad through modes, +although a dual-stick gamepad is faster for simultaneous camera/movement. + +The iPhone/iPad Control Center Apple TV Remote uses the UIKit directional and +Select/Back/Play-Pause events. Connect it to the same Apple TV and use the +same controls panel; no extra controller app or web server is required. Its +directional input is digital. Physical touch-surface pan gestures also provide +proportional stick input. The frontend does not claim touch-surface pan support +from every phone Remote version. The production handler has portable state and +tvOS simulator UIKit/focus regression tests. Physical phone/Remote input still +requires verification on the target TV; installation alone does not prove it. + +`prepare_project.py` derives the tvOS layered icon and top-shelf assets from the +prepared iOS icon. The original artwork is fitted whole on a dark TV canvas; it +is not replaced by a generic icon or cropped through its title. + +## Static signed module layout + +The linker places each module in contiguous 16 KiB-aligned RX/RW/RX segments. +The middle segment includes the original read-only data, writable data, BSS and +key-page contents. Module-relative addresses and all original bytes are retained. +The loader only writes already-writable data and reduces the key page to read +protection. It does not make signed TEXT writable, remap executable code, or use +JIT. Native host TEXT, unwind information and Objective-C metadata keep their +normal linker layout. Multiple RX segments are intended for local development; +this is not an App Store packaging target. + +`TARGET_OS_TV` selects this layout in `dm/host/loader.c`. Other platforms keep +their existing loader. tvOS lacks `sigaltstack`, so crash diagnostics use the +ordinary signal stack there. + +## Build and validate + +The existing iOS project must have current `Gen/src`, `Gen/main`, `Gen/dm`, and +`Gen/embed.S` inputs. The module paths in its assembly must still exist. For +example, from the repository root: + +`prepare_project.py` deliberately removes `Gen/dm/env.txt` from the copied +inputs. That file comes from an iOS-only profile; tvOS settings belong in the +app container's writable `env.txt` or in tvOS frontend defaults. + +```sh +python3 dm/tvos/tools/prepare_project.py \ + --ios-project /path/to/ZA-xcode --output /path/to/ZA-TV-xcode \ + --team YOUR_TEAM --shader-archive-sha256 YOUR_RESOURCE_ARCHIVE_SHA256 +xcodebuild -project /path/to/ZA-TV-xcode/DmTV.xcodeproj -scheme DmTV \ + -configuration Release -sdk appletvos -derivedDataPath /path/to/ZA-TV-derived \ + CODE_SIGNING_ALLOWED=NO build +python3 dm/tvos/tools/verify_segments.py \ + /path/to/ZA-TV-derived/Build/Products/Release-appletvos/DmTV.app/DmTV \ + /path/to/ZA-TV-xcode/Gen +python3 dm/tvos/tests/test_resources.py +python3 dm/tvos/tests/test_romfs_patch.py +clang -std=c11 -I dm/host dm/tvos/tests/test_remote.c -o /tmp/test-tv-remote +/tmp/test-tv-remote +python3 dm/tvos/tests/test_remote_ui.py --output /path/to/remote-ui-qa +``` + +Normal Xcode development signing uses the configured team and paired Apple TV. +Only install when the user has authorized device deployment. The verifier checks +the native tvOS platform, nonoverlapping segment ranges, RX/RW protections, +module-relative addresses, alignment, and exact original input bytes. + +For a loading-time exit, set `DM_TV_MEMORY_DIAG=1` for that launch to log the +process footprint and remaining dirty-memory allowance every 250 ms, including +UIKit memory warnings. It is disabled by default. Metal's allocation total is +not the process footprint. The existing `DM_MEMSTAT=` tool +adds a one-second VM-class breakdown; `DM_MEMSTAT_DUMP=` also writes a +region snapshot. Capture the official termination signal or system report +before attributing an exit to memory pressure. + +`DM_MAP_HEADROOM_MB` is a diagnostic switch that makes a new +`MapPhysicalMemory` request return `ResultLimitReached` after the process's +available dirty-memory allowance falls below the selected reserve. Mapping only +changes protection on reserved virtual pages, so the map's full virtual length +is not subtracted from the allowance. It defaults to `0` (disabled) on every +platform: a living-room b18 experiment showed that refusing small runtime maps +at 640 MiB propagated the error into the game instead of freeing enough cache. + +## Resources and saves + +The thin app contains code, module tables and font resources. Copy the owned +RomFS directory into its app-data container at +`Library/Caches/nxnative/romfs`. Compile shader libraries for tvOS; +iOS or macOS metallibs are not substitutes. Put a USTAR/gzip archive with only +ordinary `msl/` files/directories at +`Library/Caches/nxnative/msl.tar.gz`. Its SHA-256 must match the +build's `DMShaderArchiveSHA256`. The app imports it on a background thread after +its window appears, verifies SHA-256 and gzip CRC, rejects links and unsafe paths, +and publishes the complete `msl` directory by rename. The input archive remains +on failure and is removed only after success. A previous resource directory is +retained as `.msl-before` during an update. + +A complete, independently verified direct directory transfer can publish +`msl/.archive-sha256` last, containing the build's archive SHA-256. A matching +marker makes startup use that directory while retaining a pending archive; +never publish the marker before all resource files have been verified. The +archive importer also writes this marker after successfully activating files. + +The same cache directory holds `env.txt`, `log.txt`, `save`, and `temp`. +On the tested tvOS 27.2 device, Foundation returned an Application Support path, +but opening a staged resource there failed with `EPERM`. Caches is readable and +writable by the app. CoreDevice listing a Support file is not evidence that the +application sandbox can read it. A resource bundle is also accepted. +`DM_MSL_DIR` and other explicit +environment options take precedence. Pipeline cache is disabled initially and +the original game present interval is retained. Saves currently live only in +the local cache, with no automatic backup or cross-device synchronization. +tvOS can purge cache data while the app is not running or storage is low, so +these files are not persistent saves and must be backed up separately. See +[Apple's archived local-storage guidance](https://developer.apple.com/library/archive/documentation/General/Conceptual/AppleTV_PG/index.html#//apple_ref/doc/uid/TP40015241-CH12-SW7). + +An offline relocated RomFS patch is opt-in. Put its verified schema-4 +`virtual-pack-layout-before-root` bundle in the writable cache and set +`DM_ROMFS_PATCH` in `env.txt` to that directory (a relative path is resolved +below `Library/Caches/nxnative`; an absolute path is accepted for development). +The app prepares it on a background thread before starting guest code. It +verifies the complete source archive, descriptor, source root tail, compact +payload, and `redirects.tsv` on first use, then creates an APFS clone overlay +with the physical layout `source prefix + inserted replacement-pack payload + +shifted root tail`. The header points to a virtual root, and the shifted root's +hash-sorted offset vector contains a 16-byte-aligned virtual prefix sum of all +descriptor sizes. The redirect table maps that complete virtual archive to +unchanged source packs, inserted replacements, and the shifted physical root. + +The materializer updates only the manifest's exact header, shifted-index, and +descriptor ranges, verifies the inserted payload, shifted root, redirect table, +and complete outputs, writes and syncs `READY.json` last, then publishes all +three files with one directory rename. Startup then sets `DM_ROMFS_OVERLAY` and +`DM_ROMFS_REDIRECTS` together. The file layer validates continuous virtual +coverage and physical bounds, supports partial and cross-row reads, and reports +the virtual size for `arc/data.trpfs`; malformed metadata or a short mapped read +fails closed. Earlier bundle schemas are rejected. The original RomFS stays +read-only and remains the rollback copy. A failed or interrupted preparation +never activates either environment variable and never starts the guest; the +next launch discards an unpublished `.partial` directory. Later launches use a +bounded READY check (sizes, replacement ranges, inserted-payload SHA-256, +final-root SHA-256, descriptor SHA-256, and redirect-table SHA-256) rather than +hashing the unchanged multi-gigabyte archive again. Without the explicit +environment variable this mechanism does nothing. + +An explicit launch environment `DM_TV_VALIDATE_LOADER=1` exercises the real +module loader, relocations, key pages, and the original guest `memcmp` code +without requiring game assets. A build/install or linker verification alone +does not establish this device test, gameplay, performance, or memory stability. +An `env.txt` setting also enables this diagnostic. Remove the variable and +terminate the diagnostic app process before a normal launch; an empty value +still enables it because the entry point checks whether the variable exists. diff --git a/dm/tvos/Sources/front_tv.m b/dm/tvos/Sources/front_tv.m new file mode 100644 index 0000000000..f595d5a3d7 --- /dev/null +++ b/dm/tvos/Sources/front_tv.m @@ -0,0 +1,294 @@ +// Apple TV front end: Metal, physical gamepads and UIKit Remote input. +#import +#import +#import +#import +#import +#include "dm.h" +#include "front.h" +#include "dm_input.h" +#include "hle.h" +#include "tv_remote.h" +#include + +void front_tv_start(void); +int front_tv_ready(void); +int front_tv_preparing(void); +static CAMetalLayer *tv_layer; +static UIViewController *tv_root; +static BOOL tv_active = YES; +static TVRemoteState remote_state; +static TVRemoteOutput physical_state; +static void input_publish(void) { + TVRemoteOutput r = tv_remote_output(&remote_state), p = physical_state; + if (!tv_active) { dm_pad_set(0, 0, 0, 0, 0); return; } + dm_pad_set(p.buttons | r.buttons, r.lx ?: p.lx, r.ly ?: p.ly, r.rx ?: p.rx, r.ry ?: p.ry); +} +static void controller_read(GCExtendedGamepad *p) { + if (!tv_active) { dm_pad_set(0, 0, 0, 0, 0); return; } + uint64_t b = 0; + // Nintendo's letters follow the physical positions used by the host's + // existing controllers: bottom B, right A, left Y, top X. + if (p.buttonA.pressed) b |= DM_BTN_B; + if (p.buttonB.pressed) b |= DM_BTN_A; + if (p.buttonX.pressed) b |= DM_BTN_Y; + if (p.buttonY.pressed) b |= DM_BTN_X; + if (p.leftShoulder.pressed) b |= DM_BTN_L; + if (p.rightShoulder.pressed) b |= DM_BTN_R; + if (p.leftTrigger.pressed) b |= DM_BTN_ZL; + if (p.rightTrigger.pressed) b |= DM_BTN_ZR; + if (p.leftThumbstickButton.pressed) b |= DM_BTN_LS; + if (p.rightThumbstickButton.pressed) b |= DM_BTN_RS; + if (p.buttonMenu.pressed) b |= DM_BTN_PLUS; + if (p.buttonOptions.pressed) b |= DM_BTN_MINUS; + if (p.dpad.left.pressed) b |= DM_BTN_LEFT; + if (p.dpad.right.pressed) b |= DM_BTN_RIGHT; + if (p.dpad.up.pressed) b |= DM_BTN_UP; + if (p.dpad.down.pressed) b |= DM_BTN_DOWN; + physical_state = (TVRemoteOutput){ b, p.leftThumbstick.xAxis.value * 32767, p.leftThumbstick.yAxis.value * 32767, + p.rightThumbstick.xAxis.value * 32767, p.rightThumbstick.yAxis.value * 32767 }; + input_publish(); +} +static void controllers_update(void) { + physical_state = (TVRemoteOutput){0}; input_publish(); + for (GCController *c in GCController.controllers) { + if (!c.extendedGamepad) continue; + c.extendedGamepad.valueChangedHandler = ^(GCExtendedGamepad *p, GCControllerElement *element) { + dispatch_async(dispatch_get_main_queue(), ^{ controller_read(p); }); + }; + controller_read(c.extendedGamepad); break; + } +} +void front_tv_focus(int active) { + tv_active = !!active; tv_remote_cancel(&remote_state, 1); + physical_state = (TVRemoteOutput){0}; input_publish(); dm_input_set(0, 0, 0, 0, 0); + [[NSNotificationCenter defaultCenter] postNotificationName:@"TVRemoteReset" object:nil]; + if (tv_layer) { dm_focus_set(active ? 1 : 3); guest_clock_freeze_for(1, !active); dm_gfx_suspend(!active); } + NSError *error = nil; + [AVAudioSession.sharedInstance setActive:!!active error:&error]; + if (active) controllers_update(); +} +@interface TVMetalView : UIView +@end +@implementation TVMetalView ++ (Class)layerClass { return CAMetalLayer.class; } +- (instancetype)initWithFrame:(CGRect)frame { + if ((self = [super initWithFrame:frame])) { + CAMetalLayer *l = (CAMetalLayer *)self.layer; + l.device = MTLCreateSystemDefaultDevice(); l.pixelFormat = MTLPixelFormatBGRA8Unorm; + l.framebufferOnly = NO; l.opaque = YES; + l.drawableSize = CGSizeMake(1920, 1080); + } return self; +} +@end +@interface TVRootVC : UIViewController +@property(strong, nonatomic) UILabel *preparationLabel; +@property(strong, nonatomic) UILabel *remoteHint; +@property(strong, nonatomic) UIView *remotePanel; +@property(strong, nonatomic) UIPanGestureRecognizer *remotePan; +@property(strong, nonatomic) NSMutableArray *remoteButtons; +@property(nonatomic) NSUInteger pulseSerial; +- (void)updateRemoteHint; +- (void)createGame; +@end +@implementation TVRootVC +- (void)viewDidLoad { + [super viewDidLoad]; self.view.backgroundColor = UIColor.blackColor; + if (!front_tv_ready()) { + UILabel *label = [[UILabel alloc] initWithFrame:CGRectInset(self.view.bounds, 70, 70)]; + label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + label.numberOfLines = 0; label.textColor = UIColor.whiteColor; label.textAlignment = NSTextAlignmentCenter; + label.text = front_tv_preparing() ? @"Preparing game files…" : @"Game resources have not been staged for Apple TV yet."; + self.preparationLabel = label; [self.view addSubview:label]; return; + } + [self createGame]; +} +- (void)createGame { + [self.preparationLabel removeFromSuperview]; self.preparationLabel = nil; + TVMetalView *view = [[TVMetalView alloc] initWithFrame:self.view.bounds]; + view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + [self.view addSubview:view]; tv_layer = (CAMetalLayer *)view.layer; + NSError *error = nil; AVAudioSession *audio = AVAudioSession.sharedInstance; + [audio setCategory:AVAudioSessionCategoryPlayback error:&error]; + [audio setPreferredSampleRate:48000 error:&error]; + [audio setPreferredIOBufferDuration:0.005 error:&error]; + [audio setActive:YES error:&error]; + [NSNotificationCenter.defaultCenter addObserverForName:GCControllerDidConnectNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *n) { controllers_update(); }]; + [NSNotificationCenter.defaultCenter addObserverForName:GCControllerDidDisconnectNotification object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *n) { controllers_update(); }]; + controllers_update(); UIApplication.sharedApplication.idleTimerDisabled = YES; + remote_state.mode = (int)[NSUserDefaults.standardUserDefaults integerForKey:@"TVRemoteMode"]; + if (remote_state.mode < TV_REMOTE_MOVE || remote_state.mode > TV_REMOTE_DPAD) remote_state.mode = TV_REMOTE_MOVE; + self.remotePan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(remotePanned:)]; + [self.view addGestureRecognizer:self.remotePan]; + self.remoteHint = [[UILabel alloc] initWithFrame:CGRectMake(50, self.view.bounds.size.height - 66, self.view.bounds.size.width - 100, 42)]; + self.remoteHint.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; + self.remoteHint.font = [UIFont systemFontOfSize:23 weight:UIFontWeightSemibold]; + self.remoteHint.textColor = UIColor.whiteColor; + self.remoteHint.backgroundColor = [UIColor colorWithWhite:0 alpha:0.70]; + self.remoteHint.textAlignment = NSTextAlignmentCenter; + self.remoteHint.accessibilityIdentifier = @"remote-hint"; + [self.view addSubview:self.remoteHint]; [self updateRemoteHint]; + [NSNotificationCenter.defaultCenter addObserverForName:@"TVRemoteReset" object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *n) { + self.pulseSerial++; [self updateRemoteHint]; + }]; + [self becomeFirstResponder]; +} +- (BOOL)canBecomeFirstResponder { return YES; } +- (NSArray> *)preferredFocusEnvironments { + return self.remotePanel && self.remoteButtons.count ? @[self.remoteButtons.firstObject] : @[]; +} +- (void)updateRemoteHint { + NSArray *modes = @[@"MOVE", @"CAMERA", @"D-PAD"]; + NSMutableArray *held = [NSMutableArray array]; + for (NSArray *entry in @[@[@"L", @(DM_BTN_L)], @[@"R", @(DM_BTN_R)], @[@"ZL", @(DM_BTN_ZL)], @[@"ZR", @(DM_BTN_ZR)]]) + if (remote_state.held & [entry[1] unsignedLongLongValue]) [held addObject:entry[0]]; + self.remoteHint.text = [NSString stringWithFormat:@"%@ | Click: A Back: B Play/Pause: all controls%@", modes[remote_state.mode], + held.count ? [@" | Holding: " stringByAppendingString:[held componentsJoinedByString:@" + "]] : @""]; +} +- (void)remotePanned:(UIPanGestureRecognizer *)pan { + if (self.remotePanel || self.presentedViewController || !tv_active) return; + CGPoint d = [pan translationInView:self.view]; + if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled || pan.state == UIGestureRecognizerStateFailed) + remote_state.x = remote_state.y = 0; + else { + remote_state.x = lrint(fmax(-1, fmin(1, d.x / 120.0)) * 32767); + remote_state.y = lrint(fmax(-1, fmin(1, -d.y / 120.0)) * 32767); + } + input_publish(); +} +- (void)hideRemotePanel { + [self.remotePanel removeFromSuperview]; self.remotePanel = nil; + self.remoteButtons = nil; self.remotePan.enabled = YES; + tv_remote_cancel(&remote_state, 0); self.pulseSerial++; input_publish(); + [self updateRemoteHint]; [self becomeFirstResponder]; [self setNeedsFocusUpdate]; +} +- (void)showRemotePanel { + if (self.remotePanel) { [self hideRemotePanel]; return; } + tv_remote_cancel(&remote_state, 0); self.pulseSerial++; input_publish(); + self.remotePan.enabled = NO; + UIView *panel = [[UIView alloc] initWithFrame:CGRectInset(self.view.bounds, 80, 70)]; + panel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + panel.backgroundColor = [UIColor colorWithWhite:0.04 alpha:0.96]; panel.layer.cornerRadius = 28; + panel.accessibilityIdentifier = @"remote-controls"; self.remotePanel = panel; + UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(30, 20, panel.bounds.size.width - 60, 100)]; + title.numberOfLines = 2; title.textAlignment = NSTextAlignmentCenter; title.textColor = UIColor.whiteColor; + title.font = [UIFont systemFontOfSize:28 weight:UIFontWeightSemibold]; + title.text = @"Remote controls\nChoose a stick mode or press a game button. Hold switches stay on until released."; + [panel addSubview:title]; self.remoteButtons = [NSMutableArray array]; + NSArray *labels = @[@"Resume / Move", @"Camera stick", @"D-pad", @"Release held buttons", + @"A", @"B", @"X", @"Y", @"L", @"R", @"ZL", @"ZR", @"+", @"−", @"L stick click", @"R stick click", + @"Hold L", @"Hold R", @"Hold ZL", @"Hold ZR"]; + CGFloat width = (panel.bounds.size.width - 180) / 4; + for (NSUInteger i = 0; i < labels.count; i++) { + UIButton *b = [UIButton buttonWithType:UIButtonTypeSystem]; + b.frame = CGRectMake(35 + (i % 4) * (width + 35), 150 + (i / 4) * ((panel.bounds.size.height - 175) / 5), width, (panel.bounds.size.height - 175) / 5 - 23); + b.tag = i; [b setTitle:labels[i] forState:UIControlStateNormal]; + b.titleLabel.font = [UIFont systemFontOfSize:26 weight:UIFontWeightSemibold]; + b.accessibilityIdentifier = [@"remote-" stringByAppendingString:labels[i]]; + [b addTarget:self action:@selector(remoteAction:) forControlEvents:UIControlEventPrimaryActionTriggered]; + [panel addSubview:b]; [self.remoteButtons addObject:b]; + } + [self.view addSubview:panel]; [self setNeedsFocusUpdate]; [self updateFocusIfNeeded]; + fprintf(stderr, "tvOS Remote: controls opened\n"); +} +- (void)remoteAction:(UIButton *)button { + NSInteger i = button.tag; + if (i < 3) { + tv_remote_mode(&remote_state, (int)i); + [NSUserDefaults.standardUserDefaults setInteger:i forKey:@"TVRemoteMode"]; + fprintf(stderr, "tvOS Remote: mode=%ld\n", (long)i); [self hideRemotePanel]; return; + } + if (i == 3) { tv_remote_cancel(&remote_state, 1); input_publish(); [self updateRemoteHint]; return; } + static const uint64_t buttons[] = { DM_BTN_A, DM_BTN_B, DM_BTN_X, DM_BTN_Y, DM_BTN_L, DM_BTN_R, DM_BTN_ZL, DM_BTN_ZR, + DM_BTN_PLUS, DM_BTN_MINUS, DM_BTN_LS, DM_BTN_RS, DM_BTN_L, DM_BTN_R, DM_BTN_ZL, DM_BTN_ZR }; + uint64_t bit = buttons[i - 4]; + if (i >= 16) { + remote_state.held ^= bit; input_publish(); [self updateRemoteHint]; + [button setTitle:[NSString stringWithFormat:@"%@ %@", remote_state.held & bit ? @"Release" : @"Hold", @[@"L", @"R", @"ZL", @"ZR"][i-16]] forState:UIControlStateNormal]; + return; + } + // The focused button produces one bounded pulse, never an accidental hold. + remote_state.pulse = bit; input_publish(); NSUInteger serial = ++self.pulseSerial; + fprintf(stderr, "tvOS Remote: pulse=0x%llx\n", (unsigned long long)bit); + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 180 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{ + if (self.pulseSerial != serial) return; + remote_state.pulse = 0; input_publish(); + }); +} +- (BOOL)remotePress:(UIPress *)press down:(BOOL)down { + if (!tv_active || self.presentedViewController || !self.remoteHint) return NO; + if (press.type == UIPressTypePlayPause) { if (down) [self showRemotePanel]; return YES; } + if (self.remotePanel) { + if (press.type == UIPressTypeMenu) { if (down) [self hideRemotePanel]; return YES; } + return NO; + } + uint64_t button = press.type == UIPressTypeSelect ? DM_BTN_A : press.type == UIPressTypeMenu ? DM_BTN_B : 0; + int direction = press.type == UIPressTypeLeftArrow ? TV_REMOTE_LEFT : press.type == UIPressTypeRightArrow ? TV_REMOTE_RIGHT : + press.type == UIPressTypeUpArrow ? TV_REMOTE_UP : press.type == UIPressTypeDownArrow ? TV_REMOTE_DOWN : 0; + if (!button && !direction) return NO; + if (down) { remote_state.pressed |= button; remote_state.directions |= direction; } + else { remote_state.pressed &= ~button; remote_state.directions &= ~direction; } + input_publish(); return YES; +} +- (void)pressesBegan:(NSSet *)presses withEvent:(UIPressesEvent *)event { + NSMutableSet *unhandled = [NSMutableSet set]; + for (UIPress *p in presses) if (![self remotePress:p down:YES]) [unhandled addObject:p]; + if (unhandled.count) [super pressesBegan:unhandled withEvent:event]; +} +- (void)pressesEnded:(NSSet *)presses withEvent:(UIPressesEvent *)event { + NSMutableSet *unhandled = [NSMutableSet set]; + for (UIPress *p in presses) if (![self remotePress:p down:NO]) [unhandled addObject:p]; + if (unhandled.count) [super pressesEnded:unhandled withEvent:event]; +} +- (void)pressesCancelled:(NSSet *)presses withEvent:(UIPressesEvent *)event { + tv_remote_cancel(&remote_state, 0); input_publish(); + [super pressesCancelled:presses withEvent:event]; +} +- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self becomeFirstResponder]; front_tv_start(); } +@end +UIViewController *front_tv_root(void) { tv_root = [TVRootVC new]; return tv_root; } +void front_tv_resources_ready(NSString *failure) { + TVRootVC *root = (TVRootVC *)tv_root; + if (failure) { + if (!root.preparationLabel) { + UILabel *label = [[UILabel alloc] initWithFrame:CGRectInset(root.view.bounds, 70, 70)]; + label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + label.numberOfLines = 0; label.textColor = UIColor.whiteColor; + label.backgroundColor = [UIColor colorWithWhite:0 alpha:0.82]; + label.textAlignment = NSTextAlignmentCenter; + root.preparationLabel = label; [root.view addSubview:label]; + } + root.preparationLabel.text = failure; return; + } + [root createGame]; front_tv_start(); +} +void *front_metal_layer(void) { return (__bridge void *)tv_layer; } +int front_screen_max_fps(void) { return 60; } +void front_screen_match(int fps) { (void)fps; } +void front_run(void (*guest)(void)) { guest(); } +int dm_thermal_now(void) { return (int)NSProcessInfo.processInfo.thermalState; } +int dm_low_power_now(void) { return 0; } +double dm_brightness_now(void) { return -1; } +void front_save_failed(int err) { + dispatch_async(dispatch_get_main_queue(), ^{ + UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Save failed" + message:[NSString stringWithFormat:@"Progress was not saved (error %d).", err] preferredStyle:UIAlertControllerStyleAlert]; + [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; + [tv_root presentViewController:alert animated:YES completion:nil]; + }); +} +int front_text_input(const char *title, const char *sub, const char *guide, const char *initial, int max_chars, char *out, int out_cap) { + __block int accepted = 0; __block NSString *text = nil; + dispatch_semaphore_t done = dispatch_semaphore_create(0); + dispatch_async(dispatch_get_main_queue(), ^{ + UIAlertController *alert = [UIAlertController alertControllerWithTitle:@(title ?: "") + message:@(sub ?: "") preferredStyle:UIAlertControllerStyleAlert]; + [alert addTextFieldWithConfigurationHandler:^(UITextField *f) { f.text = @(initial ?: ""); f.placeholder = @(guide ?: ""); }]; + [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *a) { text = alert.textFields.firstObject.text; accepted = 1; dispatch_semaphore_signal(done); }]]; + [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *a) { dispatch_semaphore_signal(done); }]]; + [tv_root presentViewController:alert animated:YES completion:nil]; + }); + dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER); + if (accepted) snprintf(out, out_cap, "%s", text.UTF8String ?: ""); + return accepted; +} diff --git a/dm/tvos/Sources/main_tv.m b/dm/tvos/Sources/main_tv.m new file mode 100644 index 0000000000..82f4220ab4 --- /dev/null +++ b/dm/tvos/Sources/main_tv.m @@ -0,0 +1,301 @@ +// tvOS entry point. The bundled game is immutable; writable cache and saves +// use Caches. tvOS local storage can be purged by the system. +#import +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "game_profile.h" +#include "dm.h" +#include "front.h" +#import "romfs_patch.h" + +UIViewController *front_tv_root(void); +void front_tv_focus(int active); +void front_tv_resources_ready(NSString *failure); +BOOL tv_unpack_shaders(NSString *writable, NSString *expectedSHA256, NSString **failure); +int dm_main(int argc, char **argv); +static char *host_argv[3]; +static int boot_ready; +static int resources_pending; +static int shaders_pending; +static int preparation_started; +static int host_started; +static NSString *boot_writable; +static NSString *boot_romfs; +static NSString *boot_patch_bundle; +static int early_boot_diag_enabled(void) { + const char *enabled = getenv("DM_TV_EARLY_BOOT_DIAG"); + return enabled && !strcmp(enabled, "1"); +} +// Opt-in process-limit diagnostics also run while the render thread is loading. +// Metal allocation totals alone do not identify the process's dirty-memory cap. +static void memory_diag_sample(const char *reason) { + task_vm_info_data_t vm = {0}; + mach_msg_type_number_t count = TASK_VM_INFO_COUNT; + kern_return_t result = task_info(mach_task_self(), TASK_VM_INFO, + (task_info_t)&vm, &count); + size_t available = os_proc_available_memory(); + fprintf(stderr, "tvOS memory: reason=%s pid=%d task_info=%d footprint=%llu resident=%llu virtual=%llu available=%zu footprint_plus_available=%llu\n", + reason, getpid(), result, (unsigned long long)vm.phys_footprint, + (unsigned long long)vm.resident_size, (unsigned long long)vm.virtual_size, + available, (unsigned long long)vm.phys_footprint + available); +} +static void *memory_diag_loop(void *unused) { + (void)unused; + for (;;) { memory_diag_sample("sample"); usleep(250000); } + return NULL; +} +static void memory_diag_start(void) { + const char *enabled = getenv("DM_TV_MEMORY_DIAG"); + if (!enabled || strcmp(enabled, "1")) return; + pthread_t thread; + if (!pthread_create(&thread, NULL, memory_diag_loop, NULL)) pthread_detach(thread); +} +static void *prepare_resources(void *unused) { + @autoreleasepool { + NSString *failure = nil; + BOOL ok = !shaders_pending || tv_unpack_shaders(boot_writable, + [NSBundle.mainBundle objectForInfoDictionaryKey:@"DMShaderArchiveSHA256"], &failure); + NSString *overlay = nil; + if (ok && boot_patch_bundle) { + NSString *game = [NSString stringWithFormat:@"%016llX", + (unsigned long long)DM_GAME_PROGRAM_ID]; + ok = tv_materialize_romfs_patch(boot_romfs, boot_writable, + boot_patch_bundle, game, &overlay, &failure); + } + if (ok) { + if (shaders_pending) + setenv("DM_MSL_DIR", [boot_writable stringByAppendingPathComponent:@"msl"].fileSystemRepresentation, 1); + if (overlay) { + setenv("DM_ROMFS_OVERLAY", overlay.fileSystemRepresentation, 1); + NSString *redirects = [overlay stringByAppendingPathComponent:@"redirects.tsv"]; + setenv("DM_ROMFS_REDIRECTS", redirects.fileSystemRepresentation, 1); + } + boot_ready = 1; resources_pending = 0; + dm_host_preload_start(2, host_argv); + } + if (!ok && !failure) failure = @"Game files could not be prepared."; + dispatch_async(dispatch_get_main_queue(), ^{ + if (!ok) preparation_started = 0; // a later foreground/start event may retry + front_tv_resources_ready(ok ? nil : failure); + }); + } + return NULL; +} +int front_tv_ready(void) { return boot_ready; } +int front_tv_preparing(void) { return resources_pending; } + +static void *validate_loader(void *unused) { + (void)unused; + void crash_install(void); crash_install(); + g_t0_ns = now_ns(); + static const char *modules[] = DM_GAME_MODULES; + load_modules(host_argv[1], modules, DM_GAME_NMOD); + int (*compare)(const void *, const void *, size_t) = guest_sym("memcmp"); + if (!compare || !module_of((const void *)compare) || + compare("same", "same", 4) || compare("a", "b", 1) >= 0 || compare("b", "a", 1) <= 0) + die("tvOS loader validation: original guest memcmp execution failed"); + fprintf(stderr, "tvOS loader validation PASS: signed guest execution, static RW data, key pages and relocation\n"); + NSLog(@"tvOS loader validation PASS: signed guest execution, static RW data, key pages and relocation"); + return NULL; +} + +static void *run_host(void *unused) { + (void)unused; + dm_main(2, host_argv); + return NULL; +} +void front_tv_start(void) { + if (resources_pending) { + if (!preparation_started) { + pthread_attr_t attr; + int result = pthread_attr_init(&attr); + int attrInitialized = result == 0; + if (!result) result = pthread_attr_setstacksize(&attr, 8u << 20); + pthread_t thread; + if (!result) result = pthread_create(&thread, &attr, prepare_resources, NULL); + if (attrInitialized) { + int destroyResult = pthread_attr_destroy(&attr); + if (destroyResult) fprintf(stderr, "[RED] tvOS resource thread attributes could not be destroyed: %d\n", destroyResult); + } + if (!result) { + preparation_started = 1; + pthread_detach(thread); + } else { + fprintf(stderr, "[RED] tvOS resource preparation thread creation failed: %d\n", result); + preparation_started = 0; + front_tv_resources_ready(@"Could not start game-file preparation. Reopen the app to retry."); + } + } + return; + } + if (!boot_ready || host_started) return; + pthread_attr_t attr; int result = pthread_attr_init(&attr); + int attrInitialized = result == 0; + if (!result) result = pthread_attr_setstacksize(&attr, 64u << 20); + pthread_t thread; + if (!result) result = pthread_create(&thread, &attr, run_host, NULL); + if (attrInitialized) { + int destroyResult = pthread_attr_destroy(&attr); + if (destroyResult) fprintf(stderr, "[RED] tvOS host thread attributes could not be destroyed: %d\n", destroyResult); + } + if (!result) { + host_started = 1; + pthread_detach(thread); + } else { + fprintf(stderr, "[RED] tvOS host thread creation failed: %d\n", result); + host_started = 0; + front_tv_resources_ready(@"Could not start the game. Reopen the app to retry."); + } +} + +static void boot(void) { + NSFileManager *fm = NSFileManager.defaultManager; + NSString *res = NSBundle.mainBundle.resourcePath; + NSString *foundation = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, + NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"nxnative"]; + NSString *staged = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/nxnative"]; + NSString *cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, + NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"nxnative"]; + // Use the platform's writable cache directory. Accept already staged + // support resources only when the application can actually read them. + BOOL stagedGame = [fm fileExistsAtPath:[[staged stringByAppendingPathComponent:@"romfs"] + stringByAppendingPathComponent:@DM_GAME_ROMFS_MARKER]]; + BOOL cacheGame = [fm fileExistsAtPath:[[cache stringByAppendingPathComponent:@"romfs"] + stringByAppendingPathComponent:@DM_GAME_ROMFS_MARKER]]; + NSString *writable = cacheGame || !stagedGame ? cache : staged; + if (early_boot_diag_enabled()) { + NSString *early = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/tv-early-boot.txt"]; + int diagnostic = open(early.fileSystemRepresentation, O_WRONLY | O_CREAT | O_APPEND, 0600); + if (diagnostic >= 0) { + NSString *marker = [[staged stringByAppendingPathComponent:@"romfs"] stringByAppendingPathComponent:@DM_GAME_ROMFS_MARKER]; + int source = open(marker.fileSystemRepresentation, O_RDONLY); + int sourceError = source >= 0 ? 0 : errno; + if (source >= 0) close(source); + dprintf(diagnostic, "Foundation support=%s home=%s staged_marker=%d support_open_errno=%d cache=%s cache_marker=%d selected=%s uid=%u executable=%s\n", + foundation.fileSystemRepresentation ?: "(nil)", NSHomeDirectory().fileSystemRepresentation, + stagedGame, sourceError, cache.fileSystemRepresentation, cacheGame, writable.fileSystemRepresentation, + getuid(), NSBundle.mainBundle.executablePath.fileSystemRepresentation); + close(diagnostic); + } + } + boot_writable = writable; + [fm createDirectoryAtPath:writable withIntermediateDirectories:YES attributes:nil error:nil]; + NSString *log = [writable stringByAppendingPathComponent:@"log.txt"]; + freopen(log.fileSystemRepresentation, "w", stderr); setvbuf(stderr, NULL, _IOLBF, 0); + fprintf(stderr, "tvOS boot: writable=%s\n", writable.fileSystemRepresentation); + for (NSString *folder in @[@"save", @"temp"]) [fm createDirectoryAtPath: + [writable stringByAppendingPathComponent:folder] withIntermediateDirectories:YES attributes:nil error:nil]; + for (NSString *ef in @[[writable stringByAppendingPathComponent:@"env.txt"], + [res stringByAppendingPathComponent:@"dm/env.txt"]]) { + NSString *txt = [NSString stringWithContentsOfFile:ef encoding:NSUTF8StringEncoding error:nil]; + for (NSString *line in [txt componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet]) { + NSString *s = [line stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet]; + NSRange eq = [s rangeOfString:@"="]; + if (!s.length || [s hasPrefix:@"#"] || eq.location == NSNotFound || !eq.location) continue; + setenv([s substringToIndex:eq.location].UTF8String, + [s substringFromIndex:eq.location + 1].UTF8String, 0); + } + } + NSString *romfs = [res stringByAppendingPathComponent:@"romfs"]; + NSString *external = [writable stringByAppendingPathComponent:@"romfs"]; + if ([fm fileExistsAtPath:[external stringByAppendingPathComponent:@DM_GAME_ROMFS_MARKER]]) romfs = external; + boot_romfs = romfs; + boot_ready = [fm fileExistsAtPath:[romfs stringByAppendingPathComponent:@DM_GAME_ROMFS_MARKER]]; + NSString *externalMSL = [writable stringByAppendingPathComponent:@"msl"]; + NSString *bundledMSL = [res stringByAppendingPathComponent:@"msl"]; + NSString *archiveSHA = [NSBundle.mainBundle objectForInfoDictionaryKey:@"DMShaderArchiveSHA256"]; + NSString *importedSHA = [[NSString stringWithContentsOfFile:[externalMSL stringByAppendingPathComponent:@".archive-sha256"] + encoding:NSUTF8StringEncoding error:nil] stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]; + BOOL externalDirectory = NO, bundledDirectory = NO; + [fm fileExistsAtPath:externalMSL isDirectory:&externalDirectory]; + [fm fileExistsAtPath:bundledMSL isDirectory:&bundledDirectory]; + BOOL imported = externalDirectory && archiveSHA.length == 64 && [importedSHA isEqualToString:archiveSHA]; + BOOL archiveAvailable = [fm fileExistsAtPath:[writable stringByAppendingPathComponent:@"msl.tar.gz"]]; + // Prefer a verified external import. Otherwise an available transfer must + // be imported before boot; with no transfer, only immutable bundled shaders + // are trusted. A stale writable directory is never selected by existence. + shaders_pending = boot_ready && !imported && (archiveAvailable || !bundledDirectory); + const char *patchSpec = getenv("DM_ROMFS_PATCH"); + if (patchSpec && patchSpec[0]) { + NSString *specified = @(patchSpec); + boot_patch_bundle = specified.isAbsolutePath ? specified : + [writable stringByAppendingPathComponent:specified]; + fprintf(stderr, "tvOS RomFS patch: explicitly enabled from %s\n", + boot_patch_bundle.fileSystemRepresentation); + } + resources_pending = boot_ready && (shaders_pending || boot_patch_bundle != nil); + if (imported) fprintf(stderr, "tvOS resources: verified external import marker matches archive\n"); + else if (archiveAvailable) fprintf(stderr, "tvOS resources: shader archive is pending verified import\n"); + else if (bundledDirectory) fprintf(stderr, "tvOS resources: using immutable bundled shaders\n"); + else if (boot_ready) fprintf(stderr, "[RED] tvOS resources: no verified external, transfer, or bundled shaders\n"); + setenv("DM_ROMFS", romfs.fileSystemRepresentation, 0); + setenv("DM_SAVE", [writable stringByAppendingPathComponent:@"save"].fileSystemRepresentation, 0); + setenv("DM_TEMP", [writable stringByAppendingPathComponent:@"temp"].fileSystemRepresentation, 0); + NSString *msl = imported ? externalMSL : bundledMSL; + setenv("DM_MSL_DIR", msl.fileSystemRepresentation, 0); + setenv("DM_NVN_SHMAP", [res stringByAppendingPathComponent:@"dm/shader_map.tsv"].fileSystemRepresentation, 0); + setenv("DM_FONT_DIR", [res stringByAppendingPathComponent:@"dm/font"].fileSystemRepresentation, 0); + setenv("DM_LANG", "zh-CN", 0); setenv("DM_PRESENT_INTERVAL", "game", 0); + setenv("DM_PIPELINE_CACHE", "off", 0); setenv("DM_NVN_MIN_REDUCTION", "1", 0); + setenv("DM_NVN_CPU_DECODE_CACHE", "0", 0); setenv("DM_SHOW_FPS", "0", 0); + chdir(writable.fileSystemRepresentation); + memory_diag_start(); + host_argv[0] = strdup("dmhost"); host_argv[1] = strdup([res stringByAppendingPathComponent:@"dm"].fileSystemRepresentation); + if (getenv("DM_TV_VALIDATE_LOADER")) { + boot_ready = 0; resources_pending = 0; + pthread_t thread; + int result = pthread_create(&thread, NULL, validate_loader, NULL); + if (!result) pthread_detach(thread); + else fprintf(stderr, "[RED] tvOS loader validation thread creation failed: %d\n", result); + } + else if (boot_ready && !resources_pending) dm_host_preload_start(2, host_argv); + if (resources_pending) boot_ready = 0; + else if (!boot_ready && !getenv("DM_TV_VALIDATE_LOADER")) + fprintf(stderr, "tvOS boot: game resources not staged yet\n"); +} + +@interface TVSceneDelegate : UIResponder +@property(strong, nonatomic) UIWindow *window; +@end +@implementation TVSceneDelegate +- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)options { + self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene]; + self.window.rootViewController = front_tv_root(); [self.window makeKeyAndVisible]; +} +- (void)sceneWillResignActive:(UIScene *)scene { front_tv_focus(0); } +- (void)sceneDidBecomeActive:(UIScene *)scene { front_tv_focus(1); } +@end +@interface TVApp : UIResponder +@end +@implementation TVApp +- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { + const char *enabled = getenv("DM_TV_MEMORY_DIAG"); + if (enabled && !strcmp(enabled, "1")) memory_diag_sample("UIKit-warning"); +} +- (UISceneConfiguration *)application:(UIApplication *)app configurationForConnectingSceneSession:(UISceneSession *)session options:(UISceneConnectionOptions *)options { + UISceneConfiguration *c = [[UISceneConfiguration alloc] initWithName:@"Default" sessionRole:session.role]; + c.delegateClass = TVSceneDelegate.class; return c; +} +@end +int main(int argc, char **argv) { + // Leave a libc-only marker before autorelease pools, Foundation path lookup, + // directory creation and UIKit. This separates startup from log-path faults. + if (early_boot_diag_enabled()) { + const char *home = getenv("HOME"); + dprintf(STDERR_FILENO, "tvOS main: before Foundation, pid=%d HOME=%s\n", getpid(), home ?: "(unset)"); + os_log(OS_LOG_DEFAULT, "DmTV main entered before Foundation"); + if (home) { + char path[PATH_MAX]; snprintf(path, sizeof path, "%s/tmp/tv-early-boot.txt", home); + int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600); + if (fd >= 0) { dprintf(fd, "before Foundation pid=%d HOME=%s\n", getpid(), home); close(fd); } + } + } + @autoreleasepool { boot(); return UIApplicationMain(argc, argv, nil, NSStringFromClass(TVApp.class)); } +} diff --git a/dm/tvos/Sources/resources_tv.m b/dm/tvos/Sources/resources_tv.m new file mode 100644 index 0000000000..91a1196dc8 --- /dev/null +++ b/dm/tvos/Sources/resources_tv.m @@ -0,0 +1,187 @@ +// Stream an owned shader archive into private staging, then publish it once +// gzip integrity and all tar entries have been verified. No shell is involved. +#import +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static BOOL read_exact(gzFile archive, void *buffer, size_t size) { + uint8_t *p = buffer; + while (size) { + int n = gzread(archive, p, (unsigned)MIN(size, (size_t)32768)); + if (n <= 0) return NO; + p += n; size -= (size_t)n; + } + return YES; +} +static BOOL octal(const char *p, size_t size, uint64_t *out) { + uint64_t value = 0; BOOL digits = NO; + while (size && (*p == ' ' || *p == '\0')) { p++; size--; } + while (size && *p >= '0' && *p <= '7') { + if (value > UINT64_MAX / 8) return NO; + value = value * 8 + (*p++ - '0'); size--; digits = YES; + } + while (size && (*p == ' ' || *p == '\0')) { p++; size--; } + *out = value; return !size && digits; +} +static NSString *field(const char *p, size_t size) { + return [[NSString alloc] initWithBytes:p length:strnlen(p, size) encoding:NSUTF8StringEncoding]; +} +static BOOL write_bytes(int fd, const void *p, size_t size) { + while (size) { + ssize_t n = write(fd, p, size); + if (n <= 0) return NO; + p = (const uint8_t *)p + n; size -= (size_t)n; + } + return YES; +} +static BOOL sync_directory(NSString *path) { + int fd = open(path.fileSystemRepresentation, O_RDONLY | O_DIRECTORY | O_NOFOLLOW); + if (fd < 0) return NO; + int result; + do result = fsync(fd); while (result && errno == EINTR); + close(fd); return result == 0; +} +// The digest is the commit marker for a completely extracted shader tree. +// Publish and sync it last; a failed marker must leave the transfer archive +// available for a clean retry. +static BOOL publish_marker(NSString *dest, NSString *digest) { +#if defined(DM_TV_RESOURCE_TESTS) + if (getenv("DM_TEST_TV_RESOURCE_MARKER_FAIL")) { errno = ENOSPC; return NO; } +#endif + NSString *marker = [dest stringByAppendingPathComponent:@".archive-sha256"]; + NSString *incoming = [marker stringByAppendingString:@".incoming"]; + int fd = open(incoming.fileSystemRepresentation, + O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0600); + NSData *bytes = [digest dataUsingEncoding:NSUTF8StringEncoding]; + BOOL ok = fd >= 0 && write_bytes(fd, bytes.bytes, bytes.length); + if (ok) { int result; do result = fsync(fd); while (result && errno == EINTR); ok = result == 0; } + if (fd >= 0 && close(fd)) ok = NO; + if (ok && rename(incoming.fileSystemRepresentation, marker.fileSystemRepresentation)) ok = NO; + if (ok) ok = sync_directory(dest); + if (!ok) unlink(incoming.fileSystemRepresentation); + return ok; +} +BOOL tv_unpack_shaders(NSString *writable, NSString *expectedSHA256, NSString **failure) { + NSFileManager *fm = NSFileManager.defaultManager; + NSString *archivePath = [writable stringByAppendingPathComponent:@"msl.tar.gz"]; + int input = open(archivePath.fileSystemRepresentation, O_RDONLY | O_NOFOLLOW); + CC_SHA256_CTX hash; CC_SHA256_Init(&hash); uint8_t digest[CC_SHA256_DIGEST_LENGTH], chunk[32768]; + ssize_t nread = -1; + if (input >= 0) { + while ((nread = read(input, chunk, sizeof chunk)) > 0) CC_SHA256_Update(&hash, chunk, (CC_LONG)nread); + close(input); + } + CC_SHA256_Final(digest, &hash); + char hex[CC_SHA256_DIGEST_LENGTH * 2 + 1]; + for (unsigned i = 0; i < sizeof digest; i++) snprintf(hex + i * 2, 3, "%02x", digest[i]); + if (nread < 0 || expectedSHA256.length != 64 || ![expectedSHA256 isEqualToString:@(hex)]) { + *failure = @"The game-file transfer is incomplete. Finish copying the archive and reopen the app."; + return NO; + } + fprintf(stderr, "tvOS resources: archive SHA-256 verified: %s\n", hex); + NSString *stage = [writable stringByAppendingPathComponent:@".msl-stage"]; + [fm removeItemAtPath:stage error:nil]; + if (![fm createDirectoryAtPath:stage withIntermediateDirectories:YES attributes:nil error:nil]) { + *failure = @"Could not prepare game files."; return NO; + } + gzFile archive = gzopen(archivePath.fileSystemRepresentation, "rb"); + int root = open(stage.fileSystemRepresentation, O_RDONLY | O_DIRECTORY | O_NOFOLLOW); + BOOL ok = archive && root >= 0, ended = NO; + unsigned entries = 0; uint64_t total = 0; uint8_t header[512], buffer[32768]; + while (ok) { + if (!read_exact(archive, header, sizeof header)) { ok = NO; break; } + BOOL zero = YES; for (unsigned i = 0; i < sizeof header; i++) if (header[i]) zero = NO; + if (zero) { ended = YES; break; } + uint64_t size, checksum; unsigned sum = 0; + for (unsigned i = 0; i < sizeof header; i++) sum += (i >= 148 && i < 156) ? ' ' : header[i]; + if (!octal((const char *)header + 124, 12, &size) || + !octal((const char *)header + 148, 8, &checksum) || checksum != sum || + memcmp(header + 257, "ustar", 5) || + (header[156] != '0' && header[156] != 0 && header[156] != '5') || + size > (8ull << 30) || total + size > (8ull << 30) || ++entries > 200000) { ok = NO; break; } + NSString *name = field((const char *)header, 100), *prefix = field((const char *)header + 345, 155); + if (!name || !prefix) { ok = NO; break; } + if (prefix.length) name = [prefix stringByAppendingFormat:@"/%@", name]; + if ([name hasSuffix:@"/"]) name = [name substringToIndex:name.length - 1]; + NSArray *components = [name componentsSeparatedByString:@"/"]; + if (![components.firstObject isEqualToString:@"msl"]) { ok = NO; break; } + for (NSString *part in components) + if (!part.length || [part isEqualToString:@"."] || [part isEqualToString:@".."] || + [part containsString:@"\\"] || [part lengthOfBytesUsingEncoding:NSUTF8StringEncoding] > 240) ok = NO; + if (!ok) break; + BOOL directory = header[156] == '5'; + if (directory && size) { ok = NO; break; } + int parent = dup(root); + for (NSUInteger i = 0; ok && i < components.count - 1; i++) { + const char *part = components[i].fileSystemRepresentation; + if (mkdirat(parent, part, 0700) && errno != EEXIST) { ok = NO; break; } + int next = openat(parent, part, O_RDONLY | O_DIRECTORY | O_NOFOLLOW); + close(parent); parent = next; if (next < 0) ok = NO; + } + NSString *leaf = components.lastObject; + if (ok && directory) { + if (mkdirat(parent, leaf.fileSystemRepresentation, 0700) && errno != EEXIST) ok = NO; + int dir = openat(parent, leaf.fileSystemRepresentation, O_RDONLY | O_DIRECTORY | O_NOFOLLOW); + if (dir < 0) ok = NO; else close(dir); + } else if (ok) { + NSString *partial = [leaf stringByAppendingString:@".incoming"]; + int file = openat(parent, partial.fileSystemRepresentation, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0600); + if (file < 0) ok = NO; + uint64_t remaining = size; + while (ok && remaining) { + size_t n = (size_t)MIN(remaining, sizeof buffer); + if (!read_exact(archive, buffer, n) || !write_bytes(file, buffer, n)) ok = NO; + remaining -= n; + } + if (file >= 0 && close(file)) ok = NO; + if (ok && renameat(parent, partial.fileSystemRepresentation, parent, leaf.fileSystemRepresentation)) ok = NO; + } + if (parent >= 0) close(parent); + if (!ok) break; + if (size % 512 && !read_exact(archive, buffer, (size_t)(512 - size % 512))) { ok = NO; break; } + total += size; + if (!(entries % 4000)) fprintf(stderr, "tvOS resources: extracted %u entries, %llu bytes\n", entries, total); + } + // Consume all tar padding so zlib verifies the final stream CRC. + if (ok && ended) { + int n; + while ((n = gzread(archive, buffer, sizeof buffer)) > 0) + for (int i = 0; i < n; i++) if (buffer[i]) ok = NO; + if (n < 0) ok = NO; + } + if (archive && gzclose(archive) != Z_OK) ok = NO; + if (root >= 0) close(root); + NSString *source = [stage stringByAppendingPathComponent:@"msl"]; + NSString *dest = [writable stringByAppendingPathComponent:@"msl"]; + NSString *backup = [writable stringByAppendingPathComponent:@".msl-before"]; + if (!ended || !ok || ![fm fileExistsAtPath:source]) { + *failure = @"Game files could not be prepared. Copy the resource archive again and reopen the app."; + return NO; // Keep the input archive for a complete transfer/retry. + } + if ([fm fileExistsAtPath:dest]) { + [fm removeItemAtPath:backup error:nil]; + if (![fm moveItemAtPath:dest toPath:backup error:nil]) { *failure = @"Could not update game files."; return NO; } + } + if (![fm moveItemAtPath:source toPath:dest error:nil]) { + if ([fm fileExistsAtPath:backup]) [fm moveItemAtPath:backup toPath:dest error:nil]; + *failure = @"Could not activate game files."; return NO; + } + if (!sync_directory(writable) || !publish_marker(dest, expectedSHA256)) { + [fm removeItemAtPath:dest error:nil]; + if ([fm fileExistsAtPath:backup]) [fm moveItemAtPath:backup toPath:dest error:nil]; + (void)sync_directory(writable); + *failure = @"Could not finish updating game files. The transfer archive was kept for retry."; + return NO; + } + [fm removeItemAtPath:archivePath error:nil]; [fm removeItemAtPath:stage error:nil]; + fprintf(stderr, "tvOS resources: complete, %u entries, %llu bytes; gzip integrity verified\n", entries, total); + return YES; +} diff --git a/dm/tvos/Sources/romfs_patch.h b/dm/tvos/Sources/romfs_patch.h new file mode 100644 index 0000000000..57e2c3327f --- /dev/null +++ b/dm/tvos/Sources/romfs_patch.h @@ -0,0 +1,12 @@ +#import + +// Materialize a schema-4 virtual-layout RomFS patch as an APFS clone overlay: +// source prefix + inserted replacement packs + shifted physical root tail, +// with a whole-file virtual-to-physical read map. Legacy layouts fail closed. +// The source RomFS is opened +// read-only and is never modified. The returned path is suitable for +// DM_ROMFS_OVERLAY. Callers opt in by supplying a bundle; there is deliberately +// no automatic bundle discovery here. +BOOL tv_materialize_romfs_patch(NSString *romfs, NSString *writable, + NSString *bundle, NSString *expectedGame, NSString **overlay, + NSString **failure); diff --git a/dm/tvos/Sources/romfs_patch.m b/dm/tvos/Sources/romfs_patch.m new file mode 100644 index 0000000000..3931bad2b5 --- /dev/null +++ b/dm/tvos/Sources/romfs_patch.m @@ -0,0 +1,517 @@ +// Publish a virtual-layout relocated RomFS patch without replacing the user's +// files. Trinity's hash-ordered pack offsets are rebuilt as aligned descriptor +// prefix sums; a verified sidecar maps the virtual file to unchanged source or +// inserted replacement ranges. The 4+ GiB archive is cloned on APFS, so only +// changed blocks and inserted packs consume new physical storage. READY is written +// last and the complete +// directory is activated by one rename. A crash before that rename leaves an +// ignored .partial directory which the next invocation removes. +#import "romfs_patch.h" +#include +#include +#include +#include +#include +#include +#include +#include + +static BOOL patch_fail(NSString **failure, NSString *message) { + fprintf(stderr, "[RED] tvOS RomFS patch: %s\n", message.UTF8String); + if (failure) *failure = message; + return NO; +} + +static BOOL regular_file(NSString *path, uint64_t expected, int *fdOut, + NSString **failure) { + int fd = open(path.fileSystemRepresentation, O_RDONLY | O_CLOEXEC | O_NOFOLLOW); + struct stat st; + if (fd < 0 || fstat(fd, &st) || !S_ISREG(st.st_mode) || + (uint64_t)st.st_size != expected) { + int e = errno; + if (fd >= 0) close(fd); + if (failure) patch_fail(failure, [NSString stringWithFormat: + @"File identity differs for %@ (expected %llu bytes, errno=%d).", + path.lastPathComponent, (unsigned long long)expected, e]); + return NO; + } + *fdOut = fd; + return YES; +} + +static NSString *digest_fd(int fd, off_t offset, uint64_t bytes) { + if (lseek(fd, offset, SEEK_SET) < 0) return nil; + (void)fcntl(fd, F_NOCACHE, 1); + CC_SHA256_CTX hash; CC_SHA256_Init(&hash); + const size_t bufferBytes = 1u << 20; + uint8_t *buffer = malloc(bufferBytes); + if (!buffer) return nil; + while (bytes) { + size_t want = (size_t)MIN(bytes, bufferBytes); + ssize_t n = read(fd, buffer, want); + if (n < 0 && errno == EINTR) continue; + if (n <= 0) { free(buffer); return nil; } + CC_SHA256_Update(&hash, buffer, (CC_LONG)n); bytes -= (uint64_t)n; + } + free(buffer); + uint8_t out[CC_SHA256_DIGEST_LENGTH]; char hex[65]; + CC_SHA256_Final(out, &hash); + for (unsigned i = 0; i < sizeof out; i++) snprintf(hex + i * 2, 3, "%02x", out[i]); + return @(hex); +} + +static NSData *read_limited(NSString *path, uint64_t limit, NSString **failure) { + int fd = open(path.fileSystemRepresentation, O_RDONLY | O_CLOEXEC | O_NOFOLLOW); + struct stat st; + if (fd < 0 || fstat(fd, &st) || !S_ISREG(st.st_mode) || st.st_size < 0 || + (uint64_t)st.st_size > limit) { + int e = errno; if (fd >= 0) close(fd); + if (failure) patch_fail(failure, [NSString stringWithFormat:@"Invalid patch file %@ (errno=%d).", + path.lastPathComponent, e]); + return nil; + } + NSMutableData *data = [NSMutableData dataWithLength:(NSUInteger)st.st_size]; + size_t at = 0; + while (at < data.length) { + ssize_t n = read(fd, (uint8_t *)data.mutableBytes + at, data.length - at); + if (n < 0 && errno == EINTR) continue; + if (n <= 0) { close(fd); if (failure) patch_fail(failure, @"Patch file ended early."); return nil; } + at += (size_t)n; + } + close(fd); return data; +} + +static NSString *digest_data(NSData *data) { + uint8_t out[CC_SHA256_DIGEST_LENGTH]; char hex[65]; + CC_SHA256(data.bytes, (CC_LONG)data.length, out); + for (unsigned i = 0; i < sizeof out; i++) snprintf(hex + i * 2, 3, "%02x", out[i]); + return @(hex); +} + +static NSNumber *unsigned_number(id value) { + if (![value isKindOfClass:NSNumber.class]) return nil; + const char *type = [value objCType]; + if (!strcmp(type, @encode(float)) || !strcmp(type, @encode(double)) || + !strcmp(type, @encode(BOOL)) || [value longLongValue] < 0) return nil; + return value; +} + +static NSData *hex_data(id value) { + if (![value isKindOfClass:NSString.class] || [value length] % 2) return nil; + NSMutableData *data = [NSMutableData dataWithLength:[value length] / 2]; + uint8_t *out = data.mutableBytes; + for (NSUInteger i = 0; i < data.length; i++) { + unichar a = [value characterAtIndex:i * 2], b = [value characterAtIndex:i * 2 + 1]; + int hi = a >= '0' && a <= '9' ? a - '0' : a >= 'a' && a <= 'f' ? a - 'a' + 10 : -1; + int lo = b >= '0' && b <= '9' ? b - '0' : b >= 'a' && b <= 'f' ? b - 'a' + 10 : -1; + if (hi < 0 || lo < 0) return nil; + out[i] = (uint8_t)((hi << 4) | lo); + } + return data; +} + +static BOOL exact_read(int fd, uint64_t offset, void *buffer, size_t bytes) { + size_t got = 0; + while (got < bytes) { + ssize_t n = pread(fd, (uint8_t *)buffer + got, bytes - got, (off_t)(offset + got)); + if (n < 0 && errno == EINTR) continue; + if (n <= 0) return NO; + got += (size_t)n; + } + return YES; +} + +static BOOL exact_write(int fd, uint64_t offset, const void *buffer, size_t bytes) { + size_t put = 0; + while (put < bytes) { + ssize_t n = pwrite(fd, (const uint8_t *)buffer + put, bytes - put, + (off_t)(offset + put)); + if (n < 0 && errno == EINTR) continue; + if (n <= 0) return NO; + put += (size_t)n; + } + return YES; +} + +static BOOL validate_edits(NSArray *rows, uint64_t fileBytes, NSString *offsetKey, + NSString **failure) { + if (![rows isKindOfClass:NSArray.class] || rows.count > 20000) + return patch_fail(failure, @"Patch edit table is invalid."); + for (id object in rows) { + if (![object isKindOfClass:NSDictionary.class]) + return patch_fail(failure, @"Patch edit entry is invalid."); + NSDictionary *row = object; + NSNumber *offsetNumber = unsigned_number(row[offsetKey]); + NSNumber *bytesNumber = unsigned_number(row[@"bytes"]); + NSData *source = hex_data(row[@"source_hex"]), *replacement = hex_data(row[@"replacement_hex"]); + if (!offsetNumber || !bytesNumber || !source || !replacement) return patch_fail(failure, @"Patch edit fields are invalid."); + uint64_t offset = offsetNumber.unsignedLongLongValue, bytes = bytesNumber.unsignedLongLongValue; + if (!bytes || bytes > (64u << 20) || source.length != bytes || replacement.length != bytes || + offset > fileBytes || bytes > fileBytes - offset) + return patch_fail(failure, @"Patch edit range is invalid."); + } + NSArray *ordered = [rows sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) { + uint64_t av = [a[offsetKey] unsignedLongLongValue], bv = [b[offsetKey] unsignedLongLongValue]; + return av < bv ? NSOrderedAscending : av > bv ? NSOrderedDescending : NSOrderedSame; + }]; + uint64_t previousEnd = 0; + for (NSDictionary *row in ordered) { + uint64_t offset = [row[offsetKey] unsignedLongLongValue]; + uint64_t bytes = [row[@"bytes"] unsignedLongLongValue]; + if (offset < previousEnd) + return patch_fail(failure, @"Patch edit ranges overlap."); + previousEnd = offset + bytes; + } + return YES; +} + +static BOOL verify_ranges(int fd, NSArray *rows, NSString *offsetKey, + BOOL replacement, NSString **failure) { + for (NSDictionary *row in rows) { + NSData *expected = hex_data(row[replacement ? @"replacement_hex" : @"source_hex"]); + uint64_t offset = [row[offsetKey] unsignedLongLongValue]; + NSMutableData *actual = [NSMutableData dataWithLength:expected.length]; + if (!actual || !exact_read(fd, offset, actual.mutableBytes, actual.length) || + memcmp(actual.bytes, expected.bytes, expected.length)) { + if (failure) patch_fail(failure, [NSString stringWithFormat: + @"Patch %@ bytes differ at offset 0x%llx.", replacement ? @"output" : @"source", + (unsigned long long)offset]); + return NO; + } + } + return YES; +} + +static BOOL apply_edits(int fd, NSArray *rows, NSString *offsetKey, NSString **failure) { + for (NSDictionary *row in rows) { + NSData *data = hex_data(row[@"replacement_hex"]); + if (!exact_write(fd, [row[offsetKey] unsignedLongLongValue], data.bytes, data.length)) + return patch_fail(failure, @"Could not write a patch edit."); + } + return YES; +} + +static BOOL fsync_path(NSString *path, BOOL directory) { + int flags = O_RDONLY | O_CLOEXEC | O_NOFOLLOW; + if (directory) flags |= O_DIRECTORY; + int fd = open(path.fileSystemRepresentation, flags); + if (fd < 0) return NO; + int result; do result = fsync(fd); while (result && errno == EINTR); + close(fd); return result == 0; +} + +static void test_exit(const char *stage) { + const char *wanted = getenv("DM_TEST_ROMFS_PATCH_EXIT"); + if (wanted && !strcmp(wanted, stage)) _exit(93); +} + +static BOOL ready_overlay(NSString *root, NSString *manifestDigest, + NSDictionary *manifest, NSString **failure) { + NSString *readyPath = [root stringByAppendingPathComponent:@"READY.json"]; + NSData *readyData = read_limited(readyPath, 1 << 20, NULL); + if (!readyData) return NO; + NSDictionary *ready = [NSJSONSerialization JSONObjectWithData:readyData options:0 error:nil]; + if (![ready isKindOfClass:NSDictionary.class] || + ![ready[@"manifest_sha256"] isEqual:manifestDigest]) return NO; + NSString *arc = [root stringByAppendingPathComponent:@"arc/data.trpfs"]; + NSString *desc = [root stringByAppendingPathComponent:@"arc/data.trpfd"]; + NSDictionary *append = nil, *redirects = manifest[@"redirects"]; + NSString *redirectPath = [root stringByAppendingPathComponent:redirects[@"file"] ?: @""]; + NSString *payloadDigest = nil, *rootDigest = nil, *descriptorDigest = nil, + *redirectDigest = nil; + int afd = -1, dfd = -1, rfd = -1; BOOL ok = NO; + uint64_t archiveBytes = [manifest[@"final_archive_bytes"] unsignedLongLongValue]; + uint64_t descriptorBytes = [manifest[@"final_descriptor_bytes"] unsignedLongLongValue]; + if (!regular_file(arc, archiveBytes, &afd, NULL) || + !regular_file(desc, descriptorBytes, &dfd, NULL) || + !regular_file(redirectPath, [redirects[@"bytes"] unsignedLongLongValue], &rfd, NULL)) goto out; + if (!verify_ranges(afd, manifest[@"archive_header_edits"], @"output_offset", YES, NULL) || + !verify_ranges(afd, manifest[@"archive_index_edits"], @"output_offset", YES, NULL) || + !verify_ranges(dfd, manifest[@"descriptor_size_edits"], @"offset", YES, NULL)) goto out; + append = manifest[@"append"]; + payloadDigest = digest_fd(afd, (off_t)[append[@"output_offset"] unsignedLongLongValue], + [append[@"bytes"] unsignedLongLongValue]); + rootDigest = digest_fd(afd, (off_t)[manifest[@"final_archive_root_offset"] unsignedLongLongValue], + [manifest[@"archive_root_bytes"] unsignedLongLongValue]); + descriptorDigest = digest_fd(dfd, 0, descriptorBytes); + redirectDigest = digest_fd(rfd, 0, [redirects[@"bytes"] unsignedLongLongValue]); + ok = [payloadDigest isEqual:append[@"sha256"]] && + [rootDigest isEqual:manifest[@"final_archive_root_sha256"]] && + [descriptorDigest isEqual:manifest[@"final_descriptor_sha256"]] && + [redirectDigest isEqual:redirects[@"sha256"]]; +out: + if (afd >= 0) close(afd); if (dfd >= 0) close(dfd); if (rfd >= 0) close(rfd); + if (!ok && failure) *failure = @"Existing RomFS patch overlay did not verify."; + return ok; +} + +BOOL tv_materialize_romfs_patch(NSString *romfs, NSString *writable, + NSString *bundle, NSString *expectedGame, NSString **overlay, + NSString **failure) { + if (overlay) *overlay = nil; if (failure) *failure = nil; + if (!romfs.length || !writable.length || !bundle.length || !expectedGame.length) + return patch_fail(failure, @"RomFS patch arguments are incomplete."); + + NSFileManager *fm = NSFileManager.defaultManager; + NSString *manifestPath = [bundle stringByAppendingPathComponent:@"manifest.json"]; + NSData *manifestData = read_limited(manifestPath, 128ull << 20, failure); + if (!manifestData) return NO; + NSString *manifestDigest = digest_data(manifestData); + NSError *jsonError = nil; + NSDictionary *manifest = [NSJSONSerialization JSONObjectWithData:manifestData options:0 error:&jsonError]; + NSDictionary *append = [manifest isKindOfClass:NSDictionary.class] ? manifest[@"append"] : nil; + NSDictionary *redirects = [manifest isKindOfClass:NSDictionary.class] ? manifest[@"redirects"] : nil; + if (![manifest isKindOfClass:NSDictionary.class] || [manifest[@"schema"] integerValue] != 4 || + ![manifest[@"mode"] isEqual:@"virtual-pack-layout-before-root"] || + ![manifest[@"game"] isEqual:expectedGame] || ![manifest[@"ready_to_apply_to_new_output_directory"] boolValue] || + ![manifest[@"archive_name"] isEqual:@"data.trpfs"] || + ![manifest[@"descriptor_name"] isEqual:@"data.trpfd"] || + ![append isKindOfClass:NSDictionary.class] || + ![redirects isKindOfClass:NSDictionary.class]) + return patch_fail(failure, @"RomFS patch manifest is incompatible with this game."); + + NSArray *headerEdits = manifest[@"archive_header_edits"]; + NSArray *archiveEdits = manifest[@"archive_index_edits"]; + NSArray *descriptorEdits = manifest[@"descriptor_size_edits"]; + NSNumber *archiveBytesNumber = unsigned_number(manifest[@"archive_bytes"]); + NSNumber *descriptorBytesNumber = unsigned_number(manifest[@"descriptor_bytes"]); + NSNumber *finalArchiveNumber = unsigned_number(manifest[@"final_archive_bytes"]); + NSNumber *finalDescriptorNumber = unsigned_number(manifest[@"final_descriptor_bytes"]); + NSNumber *appendBytesNumber = unsigned_number(append[@"bytes"]); + NSNumber *appendOffsetNumber = unsigned_number(append[@"output_offset"]); + NSNumber *rootOffsetNumber = unsigned_number(manifest[@"archive_root_offset"]); + NSNumber *rootBytesNumber = unsigned_number(manifest[@"archive_root_bytes"]); + NSNumber *finalRootOffsetNumber = unsigned_number(manifest[@"final_archive_root_offset"]); + NSNumber *virtualRootOffsetNumber = unsigned_number(manifest[@"virtual_archive_root_offset"]); + NSNumber *virtualArchiveNumber = unsigned_number(manifest[@"virtual_archive_bytes"]); + NSNumber *redirectBytesNumber = unsigned_number(redirects[@"bytes"]); + NSNumber *redirectRecordsNumber = unsigned_number(redirects[@"records"]); + NSNumber *redirectVirtualNumber = unsigned_number(redirects[@"virtual_file_bytes"]); + NSNumber *redirectPhysicalNumber = unsigned_number(redirects[@"physical_file_bytes"]); + NSString *appendName = append[@"file"], *redirectName = redirects[@"file"]; + if (!archiveBytesNumber || !descriptorBytesNumber || !finalArchiveNumber || + !finalDescriptorNumber || !appendBytesNumber || !appendOffsetNumber || + !rootOffsetNumber || !rootBytesNumber || !finalRootOffsetNumber || + !virtualRootOffsetNumber || !virtualArchiveNumber || !redirectBytesNumber || + !redirectRecordsNumber || !redirectVirtualNumber || !redirectPhysicalNumber || + ![appendName isKindOfClass:NSString.class] || !appendName.length || + [appendName containsString:@"/"] || [appendName isEqual:@"."] || [appendName isEqual:@".."] || + ![redirectName isKindOfClass:NSString.class] || + ![redirectName isEqual:@"redirects.tsv"] || + ![redirects[@"target"] isEqual:@"arc/data.trpfs"]) + return patch_fail(failure, @"RomFS patch sizes or payload names are invalid."); + uint64_t archiveBytes = archiveBytesNumber.unsignedLongLongValue; + uint64_t descriptorBytes = descriptorBytesNumber.unsignedLongLongValue; + uint64_t appendBytes = appendBytesNumber.unsignedLongLongValue; + uint64_t rootOffset = rootOffsetNumber.unsignedLongLongValue; + uint64_t rootBytes = rootBytesNumber.unsignedLongLongValue; + uint64_t finalRootOffset = finalRootOffsetNumber.unsignedLongLongValue; + uint64_t virtualRootOffset = virtualRootOffsetNumber.unsignedLongLongValue; + uint64_t virtualArchiveBytes = virtualArchiveNumber.unsignedLongLongValue; + if (archiveBytes > INT64_MAX || descriptorBytes > INT64_MAX || + appendBytes > (uint64_t)INT64_MAX - archiveBytes || + rootOffset < 16 || rootOffset >= archiveBytes || rootBytes > (64u << 20) || + rootBytes != archiveBytes - rootOffset || + appendOffsetNumber.unsignedLongLongValue != rootOffset || + finalRootOffset != rootOffset + appendBytes || + finalArchiveNumber.unsignedLongLongValue != archiveBytes + appendBytes || + finalRootOffset + rootBytes != finalArchiveNumber.unsignedLongLongValue || + virtualRootOffset + rootBytes != virtualArchiveBytes || + redirectBytesNumber.unsignedLongLongValue == 0 || + redirectBytesNumber.unsignedLongLongValue > (16u << 20) || + redirectRecordsNumber.unsignedLongLongValue == 0 || + redirectRecordsNumber.unsignedLongLongValue > 20000 || + redirectVirtualNumber.unsignedLongLongValue != virtualArchiveBytes || + redirectPhysicalNumber.unsignedLongLongValue != finalArchiveNumber.unsignedLongLongValue || + finalDescriptorNumber.unsignedLongLongValue != descriptorBytes || + headerEdits.count != 1 || archiveEdits.count != 1 || !descriptorEdits.count || + !validate_edits(headerEdits, archiveBytes, @"source_offset", failure) || + !validate_edits(headerEdits, archiveBytes + appendBytes, @"output_offset", failure) || + !validate_edits(archiveEdits, archiveBytes, @"source_offset", failure) || + !validate_edits(archiveEdits, archiveBytes + appendBytes, @"output_offset", failure) || + !validate_edits(descriptorEdits, descriptorBytes, @"offset", failure)) + return patch_fail(failure, @"RomFS patch layout or edit tables are invalid."); + NSDictionary *headerEdit = headerEdits.firstObject; + NSData *headerSource = hex_data(headerEdit[@"source_hex"]); + NSData *headerReplacement = hex_data(headerEdit[@"replacement_hex"]); + uint64_t encodedSourceRoot = 0, encodedFinalRoot = 0; + if (headerSource.length == 8) memcpy(&encodedSourceRoot, headerSource.bytes, 8); + if (headerReplacement.length == 8) memcpy(&encodedFinalRoot, headerReplacement.bytes, 8); + if ([headerEdit[@"source_offset"] unsignedLongLongValue] != 8 || + [headerEdit[@"output_offset"] unsignedLongLongValue] != 8 || + [headerEdit[@"bytes"] unsignedLongLongValue] != 8 || + encodedSourceRoot != rootOffset || encodedFinalRoot != virtualRootOffset) + return patch_fail(failure, @"RomFS root-pointer edit is invalid."); + NSDictionary *indexEdit = archiveEdits.firstObject; + uint64_t indexSourceOffset = [indexEdit[@"source_offset"] unsignedLongLongValue]; + uint64_t indexOutputOffset = [indexEdit[@"output_offset"] unsignedLongLongValue]; + uint64_t indexBytes = [indexEdit[@"bytes"] unsignedLongLongValue]; + NSNumber *packCountNumber = unsigned_number(indexEdit[@"pack_count"]); + if (indexSourceOffset < rootOffset || indexOutputOffset != indexSourceOffset + appendBytes || + !packCountNumber || !indexBytes || indexBytes % 8 || + packCountNumber.unsignedLongLongValue != indexBytes / 8) + return patch_fail(failure, @"RomFS shifted root-index edit is invalid."); + + NSString *parent = [writable stringByAppendingPathComponent:@"romfs-patch-overlays"]; + if (![fm createDirectoryAtPath:parent withIntermediateDirectories:YES attributes:nil error:&jsonError]) + return patch_fail(failure, @"Could not create the RomFS patch directory."); + NSString *lockPath = [parent stringByAppendingPathComponent:@"materialize.lock"]; + int lock = open(lockPath.fileSystemRepresentation, O_RDWR | O_CREAT | O_CLOEXEC | O_NOFOLLOW, 0600); + if (lock < 0 || flock(lock, LOCK_EX)) { if (lock >= 0) close(lock); return patch_fail(failure, @"Could not lock the RomFS patch directory."); } + + BOOL success = NO; + NSString *root = [parent stringByAppendingPathComponent:manifestDigest]; + NSString *partial = [root stringByAppendingString:@".partial"]; + NSString *arcDir = nil, *sourceArchive = nil, *sourceDescriptor = nil, + *appendPath = nil, *redirectPath = nil, *sourceADigest = nil, *sourceDDigest = nil, + *sourceRootDigest = nil, *appendDigest = nil, *redirectDigest = nil, + *outputArchive = nil, *outputDescriptor = nil, *outputRedirects = nil, + *finalADigest = nil, *finalDDigest = nil, + *finalRootDigest = nil, *readyIncoming = nil, + *readyPath = nil; + NSDictionary *ready = nil; + NSData *readyData = nil, *outputRedirectData = nil; + int sourceA = -1, sourceD = -1, appendFD = -1, redirectFD = -1, + outputA = -1, outputD = -1; + if (ready_overlay(root, manifestDigest, manifest, NULL)) { + fprintf(stderr, "tvOS RomFS patch: verified existing overlay %s\n", manifestDigest.UTF8String); + if (overlay) *overlay = root; success = YES; goto done; + } + [fm removeItemAtPath:partial error:nil]; + if ([fm fileExistsAtPath:root]) [fm removeItemAtPath:root error:nil]; + arcDir = [partial stringByAppendingPathComponent:@"arc"]; + if (![fm createDirectoryAtPath:arcDir withIntermediateDirectories:YES attributes:nil error:&jsonError]) { + patch_fail(failure, @"Could not create the RomFS patch staging directory."); goto done; + } + + sourceArchive = [romfs stringByAppendingPathComponent:@"arc/data.trpfs"]; + sourceDescriptor = [romfs stringByAppendingPathComponent:@"arc/data.trpfd"]; + appendPath = [bundle stringByAppendingPathComponent:appendName]; + redirectPath = [bundle stringByAppendingPathComponent:redirectName]; + if (!regular_file(sourceArchive, archiveBytes, &sourceA, failure) || + !regular_file(sourceDescriptor, descriptorBytes, &sourceD, failure) || + !regular_file(appendPath, appendBytes, &appendFD, failure) || + !regular_file(redirectPath, redirectBytesNumber.unsignedLongLongValue, + &redirectFD, failure)) goto close_sources; + if (!verify_ranges(sourceA, headerEdits, @"source_offset", NO, failure) || + !verify_ranges(sourceA, archiveEdits, @"source_offset", NO, failure) || + !verify_ranges(sourceD, descriptorEdits, @"offset", NO, failure)) goto close_sources; + sourceADigest = digest_fd(sourceA, 0, archiveBytes); + sourceDDigest = digest_fd(sourceD, 0, descriptorBytes); + sourceRootDigest = digest_fd(sourceA, (off_t)rootOffset, rootBytes); + appendDigest = digest_fd(appendFD, 0, appendBytes); + redirectDigest = digest_fd(redirectFD, 0, redirectBytesNumber.unsignedLongLongValue); + if (![sourceADigest isEqual:manifest[@"archive_sha256"]] || + ![sourceDDigest isEqual:manifest[@"descriptor_sha256"]] || + ![sourceRootDigest isEqual:manifest[@"archive_root_sha256"]] || + ![appendDigest isEqual:append[@"sha256"]] || + ![redirectDigest isEqual:redirects[@"sha256"]]) { + patch_fail(failure, @"RomFS source or append SHA-256 differs from the manifest."); goto close_sources; + } + + outputArchive = [arcDir stringByAppendingPathComponent:@"data.trpfs"]; + outputDescriptor = [arcDir stringByAppendingPathComponent:@"data.trpfd"]; + outputRedirects = [partial stringByAppendingPathComponent:redirectName]; + if (clonefile(sourceArchive.fileSystemRepresentation, outputArchive.fileSystemRepresentation, + CLONE_NOFOLLOW) || + clonefile(sourceDescriptor.fileSystemRepresentation, outputDescriptor.fileSystemRepresentation, + CLONE_NOFOLLOW) || + clonefile(redirectPath.fileSystemRepresentation, outputRedirects.fileSystemRepresentation, + CLONE_NOFOLLOW)) { + patch_fail(failure, [NSString stringWithFormat: + @"APFS cloning the RomFS failed (errno=%d); the original remains active.", errno]); goto close_sources; + } + test_exit("cloned"); + outputA = open(outputArchive.fileSystemRepresentation, O_RDWR | O_CLOEXEC | O_NOFOLLOW); + outputD = open(outputDescriptor.fileSystemRepresentation, O_RDWR | O_CLOEXEC | O_NOFOLLOW); + if (outputA < 0 || outputD < 0 || + ftruncate(outputA, (off_t)(archiveBytes + appendBytes))) { + patch_fail(failure, @"Could not extend the RomFS patch output."); + if (outputA >= 0) close(outputA); if (outputD >= 0) close(outputD); + outputA = outputD = -1; goto close_sources; + } + const size_t copyBufferBytes = 1u << 20; + uint8_t *copyBuffer = malloc(copyBufferBytes); + if (!copyBuffer) { + patch_fail(failure, @"Could not allocate the RomFS append buffer."); + close(outputA); close(outputD); outputA = outputD = -1; goto close_sources; + } + uint64_t copiedRoot = 0; + while (copiedRoot < rootBytes) { + size_t want = (size_t)MIN(rootBytes - copiedRoot, copyBufferBytes); + if (!exact_read(sourceA, rootOffset + copiedRoot, copyBuffer, want) || + !exact_write(outputA, finalRootOffset + copiedRoot, copyBuffer, want)) { + patch_fail(failure, @"Could not shift the RomFS root tail."); break; + } + copiedRoot += want; + } + uint64_t copiedAppend = 0; + while (copiedRoot == rootBytes && copiedAppend < appendBytes) { + size_t want = (size_t)MIN(appendBytes - copiedAppend, copyBufferBytes); + if (!exact_read(appendFD, copiedAppend, copyBuffer, want) || + !exact_write(outputA, rootOffset + copiedAppend, copyBuffer, want)) { + patch_fail(failure, @"Could not insert relocated RomFS packs before the root."); break; + } + copiedAppend += want; + } + free(copyBuffer); + if (copiedRoot != rootBytes || copiedAppend != appendBytes || + !apply_edits(outputA, headerEdits, @"output_offset", failure) || + !apply_edits(outputA, archiveEdits, @"output_offset", failure) || + !apply_edits(outputD, descriptorEdits, @"offset", failure) || + fsync(outputA) || fsync(outputD) || !fsync_path(outputRedirects, NO)) { + if (copiedRoot == rootBytes && copiedAppend == appendBytes && + (!failure || !*failure)) patch_fail(failure, @"Could not sync the RomFS patch output."); + close(outputA); close(outputD); outputA = outputD = -1; goto close_sources; + } + test_exit("written"); + if (!verify_ranges(outputA, headerEdits, @"output_offset", YES, failure) || + !verify_ranges(outputA, archiveEdits, @"output_offset", YES, failure) || + !verify_ranges(outputD, descriptorEdits, @"offset", YES, failure)) { + close(outputA); close(outputD); outputA = outputD = -1; goto close_sources; + } + appendDigest = digest_fd(outputA, (off_t)rootOffset, appendBytes); + finalRootDigest = digest_fd(outputA, (off_t)finalRootOffset, rootBytes); + finalADigest = digest_fd(outputA, 0, archiveBytes + appendBytes); + finalDDigest = digest_fd(outputD, 0, descriptorBytes); + close(outputA); close(outputD); outputA = outputD = -1; + outputRedirectData = read_limited(outputRedirects, 16u << 20, failure); + if (![appendDigest isEqual:append[@"sha256"]] || + ![finalRootDigest isEqual:manifest[@"final_archive_root_sha256"]] || + ![finalADigest isEqual:manifest[@"final_archive_sha256"]] || + ![finalDDigest isEqual:manifest[@"final_descriptor_sha256"]] || + !outputRedirectData || ![digest_data(outputRedirectData) isEqual:redirects[@"sha256"]]) { + patch_fail(failure, @"Materialized RomFS patch SHA-256 did not verify."); goto close_sources; + } + close(sourceA); sourceA = -1; close(sourceD); sourceD = -1; + close(appendFD); appendFD = -1; close(redirectFD); redirectFD = -1; + + ready = @{@"schema": @1, @"manifest_sha256": manifestDigest, + @"archive_sha256": finalADigest, @"descriptor_sha256": finalDDigest, + @"redirects_sha256": redirectDigest, + @"virtual_archive_bytes": @(virtualArchiveBytes)}; + readyData = [NSJSONSerialization dataWithJSONObject:ready options:NSJSONWritingPrettyPrinted error:&jsonError]; + readyIncoming = [partial stringByAppendingPathComponent:@"READY.json.incoming"]; + readyPath = [partial stringByAppendingPathComponent:@"READY.json"]; + if (!readyData || ![readyData writeToFile:readyIncoming options:NSDataWritingWithoutOverwriting error:&jsonError] || + !fsync_path(readyIncoming, NO) || rename(readyIncoming.fileSystemRepresentation, readyPath.fileSystemRepresentation) || + !fsync_path(arcDir, YES) || !fsync_path(partial, YES)) { + patch_fail(failure, @"Could not publish the RomFS patch READY marker."); goto done; + } + test_exit("ready"); + if (rename(partial.fileSystemRepresentation, root.fileSystemRepresentation) || + !fsync_path(parent, YES)) { + patch_fail(failure, @"Could not atomically publish the RomFS patch overlay."); goto done; + } + test_exit("published"); + fprintf(stderr, "tvOS RomFS patch: materialized and verified %s\n", manifestDigest.UTF8String); + if (overlay) *overlay = root; success = YES; goto done; + +close_sources: + if (sourceA >= 0) close(sourceA); if (sourceD >= 0) close(sourceD); + if (appendFD >= 0) close(appendFD); if (redirectFD >= 0) close(redirectFD); +done: + if (!success) [fm removeItemAtPath:partial error:nil]; + flock(lock, LOCK_UN); close(lock); + return success; +} diff --git a/dm/tvos/Sources/tv_remote.h b/dm/tvos/Sources/tv_remote.h new file mode 100644 index 0000000000..dbb9383890 --- /dev/null +++ b/dm/tvos/Sources/tv_remote.h @@ -0,0 +1,38 @@ +// Remote state is independent of UIKit so cancellation and mode transitions +// can be checked without running the guest or selecting a menu item. +#ifndef DM_TV_REMOTE_H +#define DM_TV_REMOTE_H +#include +#include "dm_input.h" +enum { TV_REMOTE_MOVE, TV_REMOTE_CAMERA, TV_REMOTE_DPAD }; +enum { TV_REMOTE_LEFT=1, TV_REMOTE_RIGHT=2, TV_REMOTE_UP=4, TV_REMOTE_DOWN=8 }; +typedef struct { + int mode, directions; + int16_t x, y; + uint64_t pressed, held, pulse; +} TVRemoteState; +typedef struct { uint64_t buttons; int16_t lx, ly, rx, ry; } TVRemoteOutput; +static inline void tv_remote_cancel(TVRemoteState *s, int release_held) { + s->directions = 0; s->x = s->y = 0; s->pressed = s->pulse = 0; + if (release_held) s->held = 0; +} +static inline void tv_remote_mode(TVRemoteState *s, int mode) { + tv_remote_cancel(s, 0); s->mode = mode; +} +static inline TVRemoteOutput tv_remote_output(const TVRemoteState *s) { + TVRemoteOutput o = { .buttons = s->pressed | s->held | s->pulse }; + int16_t x = s->directions & (TV_REMOTE_LEFT|TV_REMOTE_RIGHT) + ? ((!!(s->directions & TV_REMOTE_RIGHT) - !!(s->directions & TV_REMOTE_LEFT)) * 32767) : s->x; + int16_t y = s->directions & (TV_REMOTE_UP|TV_REMOTE_DOWN) + ? ((!!(s->directions & TV_REMOTE_UP) - !!(s->directions & TV_REMOTE_DOWN)) * 32767) : s->y; + if (s->mode == TV_REMOTE_MOVE) { o.lx=x; o.ly=y; } + else if (s->mode == TV_REMOTE_CAMERA) { o.rx=x; o.ry=y; } + else { + if (x < -8192) o.buttons |= DM_BTN_LEFT; + if (x > 8192) o.buttons |= DM_BTN_RIGHT; + if (y < -8192) o.buttons |= DM_BTN_DOWN; + if (y > 8192) o.buttons |= DM_BTN_UP; + } + return o; +} +#endif diff --git a/dm/tvos/tests/remote_harness/Mock.m b/dm/tvos/tests/remote_harness/Mock.m new file mode 100644 index 0000000000..72eb70d888 --- /dev/null +++ b/dm/tvos/tests/remote_harness/Mock.m @@ -0,0 +1,43 @@ +#import +#include +UIViewController *front_tv_root(void); +void front_tv_focus(int active); +static UILabel *stateLabel, *historyLabel; +static NSMutableArray *history; +void dm_pad_set(uint64_t b,int32_t lx,int32_t ly,int32_t rx,int32_t ry) { + stateLabel.text=[NSString stringWithFormat:@"%llu,%d,%d,%d,%d",b,lx,ly,rx,ry]; + if (b||lx||ly||rx||ry) { if (!history)history=[NSMutableArray new]; + [history addObject:stateLabel.text?:@""];historyLabel.text=[history componentsJoinedByString:@";"]; } +} +void dm_input_set(uint64_t b,int32_t lx,int32_t ly,int32_t rx,int32_t ry) {} +void dm_focus_set(int a) {} +void guest_clock_freeze_for(int r,int a) {} +void dm_gfx_suspend(int a) {} +void front_tv_start(void) {} +int front_tv_ready(void) { return 1; } +int front_tv_preparing(void) { return 0; } +@interface QAScene : UIResponder +@property(strong,nonatomic) UIWindow *window; +@end +@implementation QAScene +- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)options { + self.window=[[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene]; + self.window.rootViewController=front_tv_root();[self.window makeKeyAndVisible]; + stateLabel=[[UILabel alloc] initWithFrame:CGRectMake(100,100,1600,100)];stateLabel.textColor=UIColor.whiteColor; + stateLabel.accessibilityIdentifier=@"qa-pad-state";stateLabel.text=@"0,0,0,0,0"; + historyLabel=[[UILabel alloc] initWithFrame:CGRectMake(100,220,1600,100)];historyLabel.textColor=UIColor.whiteColor; + historyLabel.accessibilityIdentifier=@"qa-pad-history";historyLabel.text=@"none"; + [self.window.rootViewController.view addSubview:stateLabel];[self.window.rootViewController.view addSubview:historyLabel]; +} +- (void)sceneWillResignActive:(UIScene *)scene { front_tv_focus(0); } +- (void)sceneDidBecomeActive:(UIScene *)scene { front_tv_focus(1); } +@end +@interface QAApp : UIResponder +@end +@implementation QAApp +- (UISceneConfiguration *)application:(UIApplication *)app configurationForConnectingSceneSession:(UISceneSession *)session options:(UISceneConnectionOptions *)options { + UISceneConfiguration *c=[[UISceneConfiguration alloc] initWithName:@"Default" sessionRole:session.role]; + c.delegateClass=QAScene.class; return c; +} +@end +int main(int argc,char **argv) { @autoreleasepool { return UIApplicationMain(argc,argv,nil,@"QAApp"); } } diff --git a/dm/tvos/tests/remote_harness/RemoteUITests.m b/dm/tvos/tests/remote_harness/RemoteUITests.m new file mode 100644 index 0000000000..93d03a5900 --- /dev/null +++ b/dm/tvos/tests/remote_harness/RemoteUITests.m @@ -0,0 +1,46 @@ +#import +@interface RemoteUITests:XCTestCase +@end +@implementation RemoteUITests +- (void)testProductionRemoteUI { + self.continueAfterFailure=NO; + XCUIApplication *app=[[XCUIApplication alloc] initWithBundleIdentifier:@"org.nxnative.tvremote-harness"]; + [app launch]; + XCTAssertTrue([app.staticTexts[@"remote-hint"] waitForExistenceWithTimeout:10]); + [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonPlayPause]; + XCTAssertTrue([app.otherElements[@"remote-controls"] waitForExistenceWithTimeout:5]); + XCTAssertTrue(app.buttons[@"remote-Resume / Move"].hasFocus); + [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonRight]; + XCTAssertTrue(app.buttons[@"remote-Camera stick"].hasFocus); + [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonSelect]; + XCTAssertFalse(app.otherElements[@"remote-controls"].exists); + XCTAssertTrue([app.staticTexts[@"remote-hint"].label containsString:@"CAMERA"]); + [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonRight]; + XCTAssertTrue([app.staticTexts[@"qa-pad-history"].label containsString:@"0,0,0,32767,0"]); + XCTAssertEqualObjects(app.staticTexts[@"qa-pad-state"].label,@"0,0,0,0,0"); + [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonSelect]; + XCTAssertTrue([app.staticTexts[@"qa-pad-history"].label containsString:@"1,0,0,0,0"]); + [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonMenu]; + XCTAssertTrue([app.staticTexts[@"qa-pad-history"].label containsString:@"2,0,0,0,0"]); + [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonPlayPause]; + XCTAssertTrue([app.otherElements[@"remote-controls"] waitForExistenceWithTimeout:5]); + [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonSelect]; + XCTAssertTrue([app.staticTexts[@"remote-hint"].label containsString:@"MOVE"]); + [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonPlayPause]; + for(int i=0;i<4;i++) [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonDown]; + XCTAssertTrue(app.buttons[@"remote-Hold L"].hasFocus); + [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonSelect]; + XCTAssertTrue([app.staticTexts[@"remote-hint"].label containsString:@"Holding: L"]); + [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonMenu]; + XCTAssertFalse(app.otherElements[@"remote-controls"].exists); + XCTAssertEqualObjects(app.staticTexts[@"qa-pad-state"].label,@"64,0,0,0,0"); + [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonPlayPause]; + for(int i=0;i<3;i++) [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonRight]; + XCTAssertTrue(app.buttons[@"remote-Release held buttons"].hasFocus); + [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonSelect]; + XCTAssertEqualObjects(app.staticTexts[@"qa-pad-state"].label,@"0,0,0,0,0"); + XCTAttachment *a=[XCTAttachment attachmentWithScreenshot:XCUIScreen.mainScreen.screenshot];a.lifetime=XCTAttachmentLifetimeKeepAlways;[self addAttachment:a]; + [XCUIRemote.sharedRemote pressButton:XCUIRemoteButtonMenu]; + XCTAssertFalse(app.otherElements[@"remote-controls"].exists); +} +@end diff --git a/dm/tvos/tests/test_release_guards.py b/dm/tvos/tests/test_release_guards.py new file mode 100644 index 0000000000..598dfb81ed --- /dev/null +++ b/dm/tvos/tests/test_release_guards.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +"""Static/state helpers for tvOS fail-closed release behavior.""" +from importlib.util import module_from_spec, spec_from_file_location +from pathlib import Path +import tempfile + + +def main(): + tv = Path(__file__).resolve().parents[1] + main_source = (tv / 'Sources/main_tv.m').read_text() + resource_source = (tv / 'Sources/resources_tv.m').read_text() + front_source = (tv / 'Sources/front_tv.m').read_text() + prepare_path = tv / 'tools/prepare_project.py' + prepare_source = prepare_path.read_text() + + assert 'NSString *msl = imported ? externalMSL : bundledMSL;' in main_source + assert '!imported && (archiveAvailable || !bundledDirectory)' in main_source + assert 'if (![fm fileExistsAtPath:msl])' not in main_source + assert 'preparation_started = 0; // a later foreground/start event may retry' in main_source + assert 'host_started = 0;' in main_source + assert main_source.count('pthread_attr_setstacksize') >= 2 + assert 'if (!root.preparationLabel)' in front_source + + assert 'static BOOL publish_marker' in resource_source + marker = resource_source.index('publish_marker(dest, expectedSHA256)') + remove_archive = resource_source.index('[fm removeItemAtPath:archivePath') + assert marker < remove_archive + assert 'DM_TV_RESOURCE_TESTS' in resource_source + assert "p.add_argument('--shader-archive-sha256', required=True)" in prepare_source + + spec = spec_from_file_location('prepare_project', prepare_path) + assert spec is not None and spec.loader is not None + module = module_from_spec(spec); spec.loader.exec_module(module) + with tempfile.TemporaryDirectory(prefix='tv-prepare-links-') as temporary: + root = Path(temporary) + first = root / 'first'; second = root / 'second' + first.mkdir(); second.mkdir() + link = root / 'Sources' + link.symlink_to(root / 'missing', target_is_directory=True) + module.replace_symlink(link, first, directory=True) + assert link.resolve() == first.resolve() + link.unlink(); link.mkdir(); (link / 'stale').write_text('old') + module.replace_symlink(link, second, directory=True) + assert link.is_symlink() and link.resolve() == second.resolve() + print('tvOS release guards: PASS') + + +if __name__ == '__main__': + main() diff --git a/dm/tvos/tests/test_remote.c b/dm/tvos/tests/test_remote.c new file mode 100644 index 0000000000..710e9e8cb1 --- /dev/null +++ b/dm/tvos/tests/test_remote.c @@ -0,0 +1,20 @@ +#include +#include +#include "../Sources/tv_remote.h" +int main(void) { + TVRemoteState s = {0}; TVRemoteOutput o; + s.directions = TV_REMOTE_UP|TV_REMOTE_RIGHT; s.pressed = DM_BTN_A; + o=tv_remote_output(&s); assert(o.lx==32767 && o.ly==32767 && o.rx==0 && o.buttons==DM_BTN_A); + s.directions |= TV_REMOTE_LEFT; o=tv_remote_output(&s); assert(o.lx==0 && o.ly==32767); + s.held=DM_BTN_R; tv_remote_mode(&s, TV_REMOTE_CAMERA); + o=tv_remote_output(&s); assert(o.buttons==DM_BTN_R && !o.lx && !o.ly && !o.rx && !o.ry); + s.x=-20000; s.y=12000; s.pressed=DM_BTN_B; s.pulse=DM_BTN_X; + o=tv_remote_output(&s); assert(o.rx==-20000 && o.ry==12000 && !o.lx && !o.ly); + assert(o.buttons==(DM_BTN_R|DM_BTN_B|DM_BTN_X)); + tv_remote_mode(&s, TV_REMOTE_DPAD); s.x=-20000; s.y=12000; + o=tv_remote_output(&s); assert(o.buttons==(DM_BTN_R|DM_BTN_LEFT|DM_BTN_UP)); + assert(!o.lx && !o.ly && !o.rx && !o.ry); + tv_remote_cancel(&s, 1); o=tv_remote_output(&s); + assert(!o.buttons && !o.lx && !o.ly && !o.rx && !o.ry); + puts("Remote transitions, cancellation, simultaneous actions and stick/D-pad modes PASS"); +} diff --git a/dm/tvos/tests/test_remote_ui.py b/dm/tvos/tests/test_remote_ui.py new file mode 100644 index 0000000000..91e7803caf --- /dev/null +++ b/dm/tvos/tests/test_remote_ui.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python3 +"""Exercise the production Remote frontend in an isolated, owned TV simulator. + +The host functions are stubs; no guest game, resources, physical Apple TV or +existing simulator is modified. Requires Xcode with an available tvOS runtime +and xcodegen. The generated project and xcresult remain in --output. +""" +import argparse +import json +from pathlib import Path +import subprocess +import uuid + + +def run(args, **kwargs): + return subprocess.run(args, check=True, **kwargs) + + +def main(): + p = argparse.ArgumentParser(description=__doc__) + p.add_argument('--output', type=Path, required=True) + a = p.parse_args() + tv = Path(__file__).resolve().parents[1] + host = tv.parent / 'host' + out = a.output.resolve(); out.mkdir(parents=True, exist_ok=True) + fixture = out / 'fixture'; fixture.mkdir(exist_ok=True) + (fixture / 'game_profile.h').write_text('// No game-specific constants are needed by this frontend fixture.\n') + project = f'''name: TVRemoteHarness +settings: + CLANG_ENABLE_OBJC_ARC: YES + GENERATE_INFOPLIST_FILE: YES + HEADER_SEARCH_PATHS: '"{host}" "{fixture}"' +targets: + Harness: + type: application + platform: tvOS + deploymentTarget: '17.0' + sources: + - path: {tv}/tests/remote_harness/Mock.m + - path: {tv}/Sources/front_tv.m + info: + path: Info.plist + properties: + UIApplicationSceneManifest: + UIApplicationSupportsMultipleScenes: false + UISceneConfigurations: + UIWindowSceneSessionRoleApplication: + - UISceneConfigurationName: Default + UISceneDelegateClassName: QAScene + settings: + PRODUCT_BUNDLE_IDENTIFIER: org.nxnative.tvremote-harness + HarnessTests: + type: bundle.ui-testing + platform: tvOS + deploymentTarget: '17.0' + sources: + - path: {tv}/tests/remote_harness/RemoteUITests.m + dependencies: + - target: Harness + settings: + PRODUCT_BUNDLE_IDENTIFIER: org.nxnative.tvremote-harness-tests + TEST_TARGET_NAME: Harness +schemes: + TVRemoteHarness: + build: + targets: + Harness: all + HarnessTests: [test] + test: + targets: + - HarnessTests +''' + (out / 'project.yml').write_text(project) + run(['xcodegen', 'generate', '--spec', str(out / 'project.yml')]) + runtimes = json.loads(subprocess.check_output(['xcrun', 'simctl', 'list', 'runtimes', '-j']))['runtimes'] + choices = [r for r in runtimes if r.get('isAvailable') and '.tvOS-' in r['identifier']] + if not choices: + raise RuntimeError('No available tvOS simulator runtime') + runtime = max(choices, key=lambda r: tuple(map(int, r['version'].split('.'))))['identifier'] + owned = subprocess.check_output(['xcrun', 'simctl', 'create', 'ZA-Remote-QA-' + uuid.uuid4().hex[:8], + 'com.apple.CoreSimulator.SimDeviceType.Apple-TV-4K-3rd-generation-1080p', runtime], text=True).strip() + uuid.UUID(owned) + (out / 'owned-simulator.txt').write_text(owned + '\n') + try: + with (out / 'test.log').open('w') as log: + run(['xcodebuild', '-project', str(out / 'TVRemoteHarness.xcodeproj'), '-scheme', 'TVRemoteHarness', + '-destination', 'id=' + owned, '-derivedDataPath', str(out / 'Derived'), + '-resultBundlePath', str(out / 'Remote.xcresult'), 'CODE_SIGNING_ALLOWED=NO', 'test'], + stdout=log, stderr=subprocess.STDOUT, timeout=600) + finally: + # Only the simulator created above is eligible for cleanup. + subprocess.run(['xcrun', 'simctl', 'shutdown', owned], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + subprocess.run(['xcrun', 'simctl', 'delete', owned], check=True) + print('Production Remote UIKit input and focus checks PASS') + + +if __name__ == '__main__': + main() diff --git a/dm/tvos/tests/test_resources.py b/dm/tvos/tests/test_resources.py new file mode 100644 index 0000000000..601c0b042a --- /dev/null +++ b/dm/tvos/tests/test_resources.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python3 +"""Exercise the actual streaming importer with owned synthetic archives.""" +from pathlib import Path +import gzip +import hashlib +import io +import subprocess +import tarfile +import tempfile +import argparse +import os +import shutil + + +def archive(entries): + stream = io.BytesIO() + with tarfile.open(fileobj=stream, mode='w', format=tarfile.USTAR_FORMAT) as tar: + for name, payload, kind in entries: + info = tarfile.TarInfo(name); info.type = kind + info.size = len(payload) if kind == tarfile.REGTYPE else 0 + if kind == tarfile.SYMTYPE: info.linkname = '/tmp/forbidden' + tar.addfile(info, io.BytesIO(payload)) + return gzip.compress(stream.getvalue(), mtime=0) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--real-archive', type=Path) + args = parser.parse_args() + root = Path(__file__).resolve().parents[1] + with tempfile.TemporaryDirectory(prefix='nxnative-tv-import-') as temporary: + temp = Path(temporary) + runner = temp / 'runner.m' + runner.write_text('''#import +BOOL tv_unpack_shaders(NSString *, NSString *, NSString **); +int main(int argc, char **argv) { @autoreleasepool { + NSString *failure = nil; + BOOL ok = tv_unpack_shaders(@(argv[1]), @(argv[2]), &failure); + if (!ok) fprintf(stderr, "%s\\n", failure.UTF8String); + return ok ? 0 : 2; +} } +''') + exe = temp / 'importer' + subprocess.run(['xcrun', '--sdk', 'macosx', 'clang', '-fobjc-arc', + '-DDM_TV_RESOURCE_TESTS=1', + '-Wno-deprecated-declarations', '-framework', 'Foundation', '-lz', + str(root / 'Sources/resources_tv.m'), str(runner), '-o', str(exe)], check=True) + valid = archive([('msl', b'', tarfile.DIRTYPE), + ('msl/b-example/library.metallib', b'owned fixture', tarfile.REGTYPE), + ('msl/b-example/' + 'a' * 90 + '.json', b'{}', tarfile.REGTYPE)]) + corrupted = valid[:-8] + bytes([valid[-8] ^ 1]) + valid[-7:] + cases = [ + ('valid', valid, True, None), + ('update', valid, True, None), + ('wrong-sha', valid, False, '0' * 64), + ('bad-gzip-crc', corrupted, False, None), + ('traversal', archive([('msl/../escape', b'bad', tarfile.REGTYPE)]), False, None), + ('absolute', archive([('/msl/file', b'bad', tarfile.REGTYPE)]), False, None), + ('symlink', archive([('msl/link', b'', tarfile.SYMTYPE)]), False, None), + ] + for name, data, expected, override in cases: + folder = temp / name; folder.mkdir() + if name == 'update': + (folder / 'msl').mkdir(); (folder / 'msl/old').write_text('previous owned resource') + source = folder / 'msl.tar.gz'; source.write_bytes(data) + result = subprocess.run([str(exe), str(folder), override or hashlib.sha256(data).hexdigest()], capture_output=True, text=True) + assert (result.returncode == 0) == expected, (name, result.stderr) + if expected: + assert (folder / 'msl/b-example/library.metallib').read_bytes() == b'owned fixture' + assert (folder / 'msl/.archive-sha256').read_text() == hashlib.sha256(data).hexdigest() + assert not source.exists() + if name == 'update': assert (folder / '.msl-before/old').is_file() + else: + assert source.exists() and not (folder / 'msl').exists() + assert not (folder / 'escape').exists() + print(f'{name}: PASS') + folder = temp / 'marker-failure'; folder.mkdir() + (folder / 'msl').mkdir(); (folder / 'msl/old').write_text('previous owned resource') + source = folder / 'msl.tar.gz'; source.write_bytes(valid) + environment = dict(os.environ); environment['DM_TEST_TV_RESOURCE_MARKER_FAIL'] = '1' + result = subprocess.run([str(exe), str(folder), hashlib.sha256(valid).hexdigest()], + capture_output=True, text=True, env=environment) + assert result.returncode == 2, result.stderr + assert source.exists(), 'failed marker publication must keep the transfer archive' + assert (folder / 'msl/old').read_text() == 'previous owned resource' + assert not (folder / 'msl/b-example').exists() + assert not (folder / 'msl/.archive-sha256').exists() + print('marker-failure-rollback: PASS') + if args.real_archive: + folder = temp / 'real-archive'; folder.mkdir() + source = folder / 'msl.tar.gz' + try: os.link(args.real_archive, source) + except OSError: shutil.copyfile(args.real_archive, source) + with source.open('rb') as f: digest = hashlib.file_digest(f, 'sha256').hexdigest() + result = subprocess.run([str(exe), str(folder), digest], capture_output=True, text=True) + assert result.returncode == 0, result.stderr + assert not source.exists() and (folder / 'msl').is_dir() + assert (folder / 'msl/.archive-sha256').read_text() == digest + print(result.stderr.strip()) + print('complete owned shader archive: PASS') + + +if __name__ == '__main__': + main() diff --git a/dm/tvos/tests/test_romfs_patch.py b/dm/tvos/tests/test_romfs_patch.py new file mode 100644 index 0000000000..c423cad67e --- /dev/null +++ b/dm/tvos/tests/test_romfs_patch.py @@ -0,0 +1,271 @@ +#!/usr/bin/env python3 +"""Exercise the crash-safe APFS RomFS patch materializer with owned fixtures.""" +from pathlib import Path +import argparse +import hashlib +import json +import os +import subprocess +import tempfile + + +GAME = '0100F43008C44000' + + +def sha(data): + return hashlib.sha256(data).hexdigest() + + +def fixture(root): + romfs = root / 'romfs'; arc = romfs / 'arc'; arc.mkdir(parents=True) + bundle = root / 'bundle'; bundle.mkdir() + archive = bytearray((i * 37 + i // 251) & 255 for i in range(1_100_003)) + descriptor = bytearray((i * 19 + 7) & 255 for i in range(37_009)) + append = bytes((i * 11 + 3) & 255 for i in range(123_457)) + root_offset = 900_000 + archive[:8] = b'ONEPACK\0' + archive[8:16] = root_offset.to_bytes(8, 'little') + final_root_offset = root_offset + len(append) + virtual_root_offset = root_offset + virtual_archive_bytes = len(archive) + header_source = bytes(archive[8:16]) + header_replacement = virtual_root_offset.to_bytes(8, 'little') + header_edits = [{ + 'source_offset': 8, 'output_offset': 8, 'bytes': 8, + 'source_hex': header_source.hex(), + 'replacement_hex': header_replacement.hex(), + }] + index_offset = root_offset + 1 + archive[index_offset:index_offset + 16] = bytes(range(16)) + replacement = bytes(range(16, 32)) + source = bytes(archive[index_offset:index_offset + len(replacement)]) + archive_edits = [{'source_offset': index_offset, + 'output_offset': index_offset + len(append), + 'bytes': len(replacement), 'pack_count': 2, + 'source_hex': source.hex(), + 'replacement_hex': replacement.hex()}] + descriptor_edits = [] + for offset, replacement in ((17_777, b'PACKSIZE'), (31_001, b'NEWSIZE!')): + source = bytes(descriptor[offset:offset + len(replacement)]) + descriptor_edits.append({'offset': offset, 'bytes': len(replacement), + 'source_hex': source.hex(), + 'replacement_hex': replacement.hex()}) + final_root = bytearray(archive[root_offset:]) + final_descriptor = bytearray(descriptor) + for row in archive_edits: + data = bytes.fromhex(row['replacement_hex']); at = row['source_offset'] - root_offset + final_root[at:at + len(data)] = data + final_archive = bytearray(archive[:root_offset]) + final_archive[8:16] = header_replacement + final_archive.extend(append) + final_archive.extend(final_root) + for row in descriptor_edits: + data = bytes.fromhex(row['replacement_hex']); at = row['offset'] + final_descriptor[at:at + len(data)] = data + redirects = (f'DM_ROMFS_REDIRECTS_V1\tarc/data.trpfs\t2\t{virtual_archive_bytes}' + f'\t{len(final_archive)}\n' + f'0\t{virtual_root_offset}\t0\t{virtual_root_offset}\n' + f'{virtual_root_offset}\t{len(archive) - root_offset}\t{final_root_offset}' + f'\t{len(archive) - root_offset}\n').encode() + manifest = { + 'schema': 4, 'mode': 'virtual-pack-layout-before-root', 'game': GAME, + 'ready_to_apply_to_new_output_directory': True, + 'archive_name': 'data.trpfs', 'archive_bytes': len(archive), + 'archive_sha256': sha(archive), 'descriptor_name': 'data.trpfd', + 'descriptor_bytes': len(descriptor), 'descriptor_sha256': sha(descriptor), + 'archive_root_offset': root_offset, + 'archive_root_bytes': len(archive) - root_offset, + 'archive_root_sha256': sha(archive[root_offset:]), + 'final_archive_root_offset': final_root_offset, + 'virtual_archive_root_offset': virtual_root_offset, + 'virtual_archive_bytes': virtual_archive_bytes, + 'final_archive_root_sha256': sha(final_root), + 'archive_header_edits': header_edits, + 'archive_index_edits': archive_edits, + 'descriptor_size_edits': descriptor_edits, + 'append': {'file': 'append.bin', 'bytes': len(append), 'sha256': sha(append), + 'output_offset': root_offset}, + 'redirects': {'file': 'redirects.tsv', 'bytes': len(redirects), + 'sha256': sha(redirects), 'records': 2, + 'target': 'arc/data.trpfs', + 'virtual_file_bytes': virtual_archive_bytes, + 'physical_file_bytes': len(final_archive)}, + 'final_archive_bytes': len(final_archive), + 'final_archive_sha256': sha(final_archive), + 'final_descriptor_bytes': len(final_descriptor), + 'final_descriptor_sha256': sha(final_descriptor), + } + (arc / 'data.trpfs').write_bytes(archive) + (arc / 'data.trpfd').write_bytes(descriptor) + (bundle / 'append.bin').write_bytes(append) + (bundle / 'redirects.tsv').write_bytes(redirects) + (bundle / 'manifest.json').write_text(json.dumps(manifest, indent=2) + '\n') + return romfs, bundle, bytes(archive), bytes(descriptor), bytes(final_archive), bytes(final_descriptor) + + +def run(exe, romfs, writable, bundle, game=GAME, stage=None): + writable.mkdir(parents=True, exist_ok=True) + env = os.environ.copy() + if stage: env['DM_TEST_ROMFS_PATCH_EXIT'] = stage + return subprocess.run([str(exe), str(romfs), str(writable), str(bundle), game], + capture_output=True, text=True, env=env) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--real-romfs', type=Path) + parser.add_argument('--real-bundle', type=Path) + args = parser.parse_args() + if bool(args.real_romfs) != bool(args.real_bundle): + parser.error('--real-romfs and --real-bundle must be supplied together') + tv = Path(__file__).resolve().parents[1] + main_source = (tv / 'Sources/main_tv.m').read_text() + prepare_source = (tv / 'tools/prepare_project.py').read_text() + assert 'getenv("DM_ROMFS_PATCH")' in main_source + materialize_at = main_source.index('tv_materialize_romfs_patch(') + activate_at = main_source.index('setenv("DM_ROMFS_OVERLAY"') + preload_at = main_source.index('dm_host_preload_start(2, host_argv);', materialize_at) + assert materialize_at < activate_at < preload_at + assert 'sources:\n - path: Sources' in prepare_source + strip_ios_env_at = prepare_source.index("(replacement / 'env.txt').unlink(missing_ok=True)") + publish_inputs_at = prepare_source.index('replacement.rename(dest)') + assert strip_ios_env_at < publish_inputs_at + print('explicit opt-in, fail-closed startup wiring: PASS') + with tempfile.TemporaryDirectory(prefix='nxnative-romfs-patch-') as temporary: + temp = Path(temporary) + runner = temp / 'runner.m' + runner.write_text('''#import +#include +#import "romfs_patch.h" +struct job { char **argv; int result; }; +static void *materialize(void *opaque) { @autoreleasepool { + struct job *job = opaque; char **argv = job->argv; + NSString *overlay = nil, *failure = nil; + BOOL ok = tv_materialize_romfs_patch(@(argv[1]), @(argv[2]), @(argv[3]), + @(argv[4]), &overlay, &failure); + if (overlay) printf("OVERLAY=%s\\n", overlay.fileSystemRepresentation); + if (failure) fprintf(stderr, "FAILURE=%s\\n", failure.UTF8String); + job->result = ok ? 0 : 2; return NULL; +} } +int main(int argc, char **argv) { @autoreleasepool { + if (argc != 5) return 64; + struct job job = {.argv = argv, .result = 70}; + pthread_attr_t attr; pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, 512u << 10); + pthread_t thread; + if (pthread_create(&thread, &attr, materialize, &job)) return 71; + pthread_attr_destroy(&attr); + if (pthread_join(thread, NULL)) return 72; + return job.result; +} } +''') + exe = temp / 'materializer' + subprocess.run(['xcrun', '--sdk', 'macosx', 'clang', '-fobjc-arc', + '-Wno-deprecated-declarations', '-framework', 'Foundation', + '-I', str(tv / 'Sources'), str(tv / 'Sources/romfs_patch.m'), + str(runner), '-o', str(exe)], check=True) + romfs, bundle, source_a, source_d, final_a, final_d = fixture(temp / 'fixture') + + writable = temp / 'success' + result = run(exe, romfs, writable, bundle) + assert result.returncode == 0, (result.returncode, result.stdout, result.stderr) + overlay = Path(next(line.split('=', 1)[1] for line in result.stdout.splitlines() + if line.startswith('OVERLAY='))) + assert (overlay / 'arc/data.trpfs').read_bytes() == final_a + assert (overlay / 'arc/data.trpfd').read_bytes() == final_d + assert (overlay / 'redirects.tsv').read_bytes() == (bundle / 'redirects.tsv').read_bytes() + assert (overlay / 'READY.json').is_file() + assert not list((writable / 'romfs-patch-overlays').glob('*.partial')) + assert (romfs / 'arc/data.trpfs').read_bytes() == source_a + assert (romfs / 'arc/data.trpfd').read_bytes() == source_d + before = (overlay / 'READY.json').stat().st_mtime_ns + again = run(exe, romfs, writable, bundle) + assert again.returncode == 0 and 'verified existing overlay' in again.stderr + assert (overlay / 'READY.json').stat().st_mtime_ns == before + print('materialize + fast READY reuse: PASS') + + wrong_game = run(exe, romfs, temp / 'wrong-game', bundle, game='bad') + assert wrong_game.returncode == 2 and 'OVERLAY=' not in wrong_game.stdout + assert not list((temp / 'wrong-game').rglob('READY.json')) + print('wrong game fails closed without overlay: PASS') + + legacy_root = temp / 'legacy-schema'; legacy_romfs, legacy_bundle, *_ = fixture(legacy_root) + legacy_manifest_path = legacy_bundle / 'manifest.json' + legacy_manifest = json.loads(legacy_manifest_path.read_text()) + legacy_manifest['schema'] = 2 + legacy_manifest['mode'] = 'append-relocated-packs' + legacy_manifest_path.write_text(json.dumps(legacy_manifest, indent=2) + '\n') + legacy = run(exe, legacy_romfs, temp / 'legacy-writable', legacy_bundle) + assert legacy.returncode == 2 and 'OVERLAY=' not in legacy.stdout + assert 'incompatible with this game' in legacy.stderr + assert not list((temp / 'legacy-writable').rglob('READY.json')) + print('schema-2 append-after-EOF bundle fails closed: PASS') + + renamed_root = temp / 'renamed-redirect'; renamed_romfs, renamed_bundle, *_ = fixture(renamed_root) + renamed_manifest_path = renamed_bundle / 'manifest.json' + renamed_manifest = json.loads(renamed_manifest_path.read_text()) + (renamed_bundle / 'redirects.tsv').rename(renamed_bundle / 'other.tsv') + renamed_manifest['redirects']['file'] = 'other.tsv' + renamed_manifest_path.write_text(json.dumps(renamed_manifest, indent=2) + '\n') + renamed = run(exe, renamed_romfs, temp / 'renamed-redirect-writable', renamed_bundle) + assert renamed.returncode == 2 and 'OVERLAY=' not in renamed.stdout + assert 'payload names are invalid' in renamed.stderr + assert not list((temp / 'renamed-redirect-writable').rglob('READY.json')) + print('noncanonical redirect sidecar name fails closed: PASS') + + bad_root_hash = temp / 'bad-root-hash'; root_romfs, root_bundle, *_ = fixture(bad_root_hash) + root_manifest_path = root_bundle / 'manifest.json' + root_manifest = json.loads(root_manifest_path.read_text()) + root_manifest['archive_root_sha256'] = '0' * 64 + root_manifest_path.write_text(json.dumps(root_manifest, indent=2) + '\n') + root_failed = run(exe, root_romfs, temp / 'bad-root-writable', root_bundle) + assert root_failed.returncode == 2 and 'OVERLAY=' not in root_failed.stdout + assert 'source or append SHA-256 differs' in root_failed.stderr + assert not list((temp / 'bad-root-writable').rglob('READY.json')) + print('source-root hash mismatch fails closed: PASS') + + bad_root = temp / 'bad-source'; bad_romfs, bad_bundle, *_ = fixture(bad_root) + bad = bytearray((bad_romfs / 'arc/data.trpfs').read_bytes()); bad[200_000] ^= 1 + (bad_romfs / 'arc/data.trpfs').write_bytes(bad) + failed = run(exe, bad_romfs, temp / 'bad-writable', bad_bundle) + assert failed.returncode == 2 and 'OVERLAY=' not in failed.stdout + assert not list((temp / 'bad-writable').rglob('READY.json')) + assert (bad_romfs / 'arc/data.trpfs').read_bytes() == bad + print('source mismatch fails closed and preserves source: PASS') + + for stage in ('cloned', 'written', 'ready', 'published'): + interrupted = temp / ('interrupt-' + stage) + first = run(exe, romfs, interrupted, bundle, stage=stage) + assert first.returncode == 93, (stage, first.stderr) + recovered = run(exe, romfs, interrupted, bundle) + assert recovered.returncode == 0, (stage, recovered.stderr) + roots = [p for p in (interrupted / 'romfs-patch-overlays').iterdir() + if p.is_dir() and not p.name.endswith('.partial')] + assert len(roots) == 1 and (roots[0] / 'READY.json').is_file() + assert not list((interrupted / 'romfs-patch-overlays').glob('*.partial')) + print('interrupted clone/write/READY/publish recovery: PASS') + + concurrent = temp / 'concurrent'; concurrent.mkdir() + commands = [[str(exe), str(romfs), str(concurrent), str(bundle), GAME] for _ in range(3)] + processes = [subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + text=True) for command in commands] + results = [process.communicate(timeout=30) + (process.returncode,) for process in processes] + assert all(code == 0 for _, _, code in results), results + roots = [p for p in (concurrent / 'romfs-patch-overlays').iterdir() + if p.is_dir() and not p.name.endswith('.partial')] + assert len(roots) == 1 and (roots[0] / 'arc/data.trpfs').read_bytes() == final_a + print('concurrent materializers serialize to one overlay: PASS') + + if args.real_romfs: + real = run(exe, args.real_romfs.resolve(), temp / 'real-owned', + args.real_bundle.resolve()) + assert real.returncode == 0, (real.returncode, real.stdout, real.stderr) + real_overlay = Path(next(line.split('=', 1)[1] for line in real.stdout.splitlines() + if line.startswith('OVERLAY='))) + assert (real_overlay / 'READY.json').is_file() + print(real.stderr.strip()) + print('complete owned RomFS append-relocated bundle: PASS') + + +if __name__ == '__main__': + main() diff --git a/dm/tvos/tools/app_icon.py b/dm/tvos/tools/app_icon.py new file mode 100644 index 0000000000..c9cc48600b --- /dev/null +++ b/dm/tvos/tools/app_icon.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +"""Create a tvOS brand-asset catalog from the existing iOS icon artwork.""" +import argparse +import json +from pathlib import Path +import subprocess + +INFO = {"author": "xcode", "version": 1} + + +def write(path, value): + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps(value, indent=2) + "\n") + + +def create(source, output): + helper = Path(__file__).with_suffix('.swift') + renderer = output.parent / 'tv-icon-renderer' + subprocess.run(['swiftc', '-module-cache-path', str(output.parent / 'icon-module-cache'), str(helper), '-o', str(renderer)], check=True) + output.mkdir(parents=True, exist_ok=True) + write(output / 'Contents.json', {"info": INFO}) + brand = output / 'App Icon & Top Shelf Image.brandassets' + assets = [] + for name, width, height, role, scale in ( + ('App Icon', 400, 240, 'primary-app-icon', True), + ('App Icon - App Store', 1280, 768, 'primary-app-icon', False), + ('Top Shelf', 1920, 720, 'top-shelf-image', True), + ('Top Shelf Wide', 2320, 720, 'top-shelf-image-wide', True)): + stack = role == 'primary-app-icon' + filename = name + ('.imagestack' if stack else '.imageset') + asset = brand / filename + assets.append({'idiom': 'tv', 'size': f'{width}x{height}', 'role': role, 'filename': filename}) + layers = ['Front', 'Back'] if stack else ['Full'] + if stack: + write(asset / 'Contents.json', {'info': INFO, 'layers': [{'filename': x + '.imagestacklayer'} for x in layers]}) + for layer in layers: + imageset = asset / (layer + '.imagestacklayer') / 'Content.imageset' if stack else asset + if stack: + write(imageset.parent / 'Contents.json', {'info': INFO}) + images = [] + for multiplier in ([1, 2] if scale else [1]): + image_name = f'{layer.lower()}-{multiplier}x.png' + images.append({'idiom': 'tv', 'scale': f'{multiplier}x', 'filename': image_name}) + imageset.mkdir(parents=True, exist_ok=True) + subprocess.run([str(renderer), str(source), str(imageset / image_name), + str(width * multiplier), str(height * multiplier), layer.lower()], check=True) + write(imageset / 'Contents.json', {'info': INFO, 'images': images}) + write(brand / 'Contents.json', {'info': INFO, 'assets': assets}) + renderer.unlink() + + +if __name__ == '__main__': + p = argparse.ArgumentParser(description=__doc__) + p.add_argument('source', type=Path) + p.add_argument('output', type=Path) + a = p.parse_args() + create(a.source.resolve(), a.output.resolve()) diff --git a/dm/tvos/tools/app_icon.swift b/dm/tvos/tools/app_icon.swift new file mode 100644 index 0000000000..1bf982e655 --- /dev/null +++ b/dm/tvos/tools/app_icon.swift @@ -0,0 +1,27 @@ +// Reuse the supplied icon artwork without cropping its title or characters. +import AppKit +import Foundation + +let args = CommandLine.arguments +guard args.count == 6, let image = NSImage(contentsOfFile: args[1]), + let width = Int(args[3]), let height = Int(args[4]) else { fatalError("icon arguments") } +let size = NSSize(width: width, height: height) +let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: width, pixelsHigh: height, + bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, + colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)! +NSGraphicsContext.saveGraphicsState() +NSGraphicsContext.current = NSGraphicsContext(bitmapImageRep: rep) +let bounds = NSRect(origin: .zero, size: size) +NSColor.clear.setFill(); bounds.fill() +if args[5] != "front" { + NSColor(calibratedRed: 0.035, green: 0.08, blue: 0.16, alpha: 1).setFill() + bounds.fill() +} +if args[5] != "back" { + let side = CGFloat(height) + NSGraphicsContext.current?.imageInterpolation = .high + image.draw(in: NSRect(x: (CGFloat(width)-side)/2, y: 0, width: side, height: side), + from: .zero, operation: .sourceOver, fraction: 1) +} +NSGraphicsContext.restoreGraphicsState() +try rep.representation(using: .png, properties: [:])!.write(to: URL(fileURLWithPath: args[2])) diff --git a/dm/tvos/tools/prepare_project.py b/dm/tvos/tools/prepare_project.py new file mode 100644 index 0000000000..b4e4462bcc --- /dev/null +++ b/dm/tvos/tools/prepare_project.py @@ -0,0 +1,176 @@ +#!/usr/bin/env python3 +"""Prepare an independent tvOS Xcode project from existing iOS host inputs.""" +import argparse +from pathlib import Path +import shutil +import subprocess +import re + + +def replace_symlink(path: Path, target: Path, *, directory: bool = False) -> None: + """Replace regular, directory, valid, or broken prior output uniformly.""" + if path.is_symlink() or path.exists(): + if path.is_dir() and not path.is_symlink(): + shutil.rmtree(path) + else: + path.unlink() + path.symlink_to(target, target_is_directory=directory) + + +def main(): + p = argparse.ArgumentParser(description=__doc__) + p.add_argument('--ios-project', type=Path, required=True) + p.add_argument('--output', type=Path, required=True) + p.add_argument('--team', required=True) + p.add_argument('--bundle-id', default='org.nxnative.0100f43008c44000.tv') + p.add_argument('--shader-archive-sha256', required=True) + p.add_argument('--build-number', default='1') + a = p.parse_args() + if not re.fullmatch('[0-9a-f]{64}', a.shader_archive_sha256): + p.error('--shader-archive-sha256 must be a lowercase SHA-256 digest') + tv = Path(__file__).resolve().parents[1] + out = a.output.resolve() + if out == a.ios_project.resolve(): + p.error('--output must differ from the prepared iOS project') + out.mkdir(parents=True, exist_ok=True) + gen = out / 'Gen'; gen.mkdir(exist_ok=True) + iosgen = a.ios_project.resolve() / 'Gen' + for n in ('src', 'main', 'dm'): + dest = gen / n + # These are generated inputs. Refresh them together: retaining old + # fixup tables while embed.S points at a newer module mixes layouts. + replacement = gen / (n + '.incoming') + if replacement.is_symlink(): + replacement.unlink() + elif replacement.exists(): + shutil.rmtree(replacement) + shutil.copytree(iosgen / n, replacement, symlinks=True) + if n == 'dm': + # ios.env is a platform-specific production profile. A prepared + # tvOS project must use its own writable env.txt or frontend + # defaults, never silently inherit iPad refresh/timing settings. + (replacement / 'env.txt').unlink(missing_ok=True) + if dest.is_symlink(): + dest.unlink() + elif dest.exists(): + shutil.rmtree(dest) + replacement.rename(dest) + # Older prepared iOS projects have a frozen source-file list. The runtime + # BC-to-ASTC implementation may have been added to the checkout after that + # preparation, so add exactly that shared source when it is absent. Fresh + # iOS preparations already contain it through the host Makefile wildcard. + bc2astc = tv.parent / 'gfx' / 'tex' / 'nvn_bc2astc.c' + bc2astc_link = gen / 'src' / 'gfx__tex__nvn_bc2astc.c' + if bc2astc.is_file(): + replace_symlink(bc2astc_link, bc2astc) + # Let ld own segment addresses, fixup indices and signing metadata. The + # guest's own PC-relative references keep their original module offsets. + original = (iosgen / 'embed.S').read_text() + bins = {Path(s).name.removesuffix('.patched.bin'): Path(s) + for s in re.findall(r'\.incbin "([^"]+)"', original)} + asm, flags, address = [], [], 0x104000000 + for mod in ('main', 'sdk'): + hdr = dict(line[2:].split() for line in + (gen / 'dm' / f'{mod}.fixups.tsv').read_text().splitlines() + if line.startswith('# ')) + binary = bins[mod] + start = (int(hdr['TEXT_END'], 16) + 0x3fff) & ~0x3fff + patch = int(hdr['PATCH_OFF'], 16) + size = binary.stat().st_size; end = (size + 0x3fff) & ~0x3fff + if patch <= start or patch % 0x4000: + raise ValueError('tvOS static layout requires positive aligned PATCH_OFF') + for seg, section, lo, hi, prot in ( + (f'__DM{mod}0', f'__dm{mod}x0', 0, start, 'rx'), + (f'__DM{mod}D', '__data', start, patch, 'rw'), + (f'__DM{mod}1', f'__dm{mod}x1', patch, end, 'rx')): + attrs = ',regular,pure_instructions' if prot == 'rx' else ',regular' + fill = min(hi, size) - lo + asm.extend([f'.section {seg},{section}{attrs}', '.p2align 14', + f'.incbin "{binary}", {lo:#x}, {fill:#x}']) + if hi - lo > fill: + asm.append(f'.space {hi - lo - fill:#x}') + flags.extend([f'-Wl,-segprot,{seg},{prot},{prot}', + f'-Wl,-segaddr,{seg},{address:#x}']) + address += hi - lo + address += 0x4000 + (gen / 'embed.S').write_text('\n'.join(asm) + '\n') + config = [] + for line in (iosgen / 'dm.xcconfig').read_text().splitlines(): + if line.startswith(('CODE_SIGN_ENTITLEMENTS', 'DM_XCODE_OUTPUT_ROOT', 'OTHER_LDFLAGS', 'DM_APPICON')): + continue + config.append(line) + config.append('OTHER_LDFLAGS = $(inherited) $(DM_FRAMEWORKS) -lz ' + ' '.join(flags)) + (gen / 'dm-tv.xcconfig').write_text('\n'.join(config) + '\n') + from app_icon import create as create_icon + icon = iosgen / 'Assets.xcassets/AppIcon.appiconset/icon.png' + if not icon.is_file(): + raise ValueError('Prepared iOS icon artwork is missing') + create_icon(icon, gen / 'TVAssets.xcassets') + sources = out / 'Sources' + replace_symlink(sources, tv / 'Sources', directory=True) + (out / 'project.yml').write_text(f'''name: DmTV +configFiles: + Release: Gen/dm-tv.xcconfig + Debug: Gen/dm-tv.xcconfig +targets: + DmTV: + type: application + platform: tvOS + deploymentTarget: '17.0' + sources: + - path: Sources + - path: Gen/src + - path: Gen/main/main.c + compilerFlags: -Dmain=dm_main + - path: Gen/TVAssets.xcassets + - path: Gen/embed.S + - path: Gen/dm + type: folder + buildPhase: resources + info: + path: Gen/TV-Info.plist + properties: + CFBundleDisplayName: Pokémon Legends Z-A + CFBundleShortVersionString: $(MARKETING_VERSION) + CFBundleVersion: $(CURRENT_PROJECT_VERSION) + DMLanguage: zh-CN + DMShaderArchiveSHA256: '{a.shader_archive_sha256}' + GCSupportsControllerUserInteraction: true + GCSupportedGameControllers: + - ProfileName: ExtendedGamepad + - ProfileName: MicroGamepad + UIApplicationSceneManifest: + UIApplicationSupportsMultipleScenes: false + UISceneConfigurations: + UIWindowSceneSessionRoleApplication: + - UISceneConfigurationName: Default + UISceneDelegateClassName: TVSceneDelegate + settings: + PRODUCT_BUNDLE_IDENTIFIER: {a.bundle_id} + PRODUCT_NAME: DmTV + MARKETING_VERSION: '2.0.2' + CURRENT_PROJECT_VERSION: '{a.build_number}' + ASSETCATALOG_COMPILER_APPICON_NAME: App Icon & Top Shelf Image + DEVELOPMENT_TEAM: {a.team} + CODE_SIGN_STYLE: Automatic + ENABLE_USER_SCRIPT_SANDBOXING: NO + CLANG_ENABLE_OBJC_ARC: YES + GCC_C_LANGUAGE_STANDARD: gnu11 + OTHER_CFLAGS: $(inherited) -mcpu=apple-a15 + GCC_WARN_INHIBIT_ALL_WARNINGS: YES + ENABLE_BITCODE: NO + DEBUG_INFORMATION_FORMAT: dwarf +schemes: + DmTV: + build: + targets: + DmTV: all + run: + config: Release +''') + subprocess.run(['xcodegen', 'generate', '--spec', str(out / 'project.yml')], check=True, cwd=out) + print(out / 'DmTV.xcodeproj') + + +if __name__ == '__main__': + main() diff --git a/dm/tvos/tools/verify_segments.py b/dm/tvos/tools/verify_segments.py new file mode 100644 index 0000000000..6566b7d4ea --- /dev/null +++ b/dm/tvos/tools/verify_segments.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +"""Verify static guest offsets, protections and bytes in a linked tvOS app.""" +import argparse +from pathlib import Path +import re +import struct + + +def verify(executable, generated): + data = executable.read_bytes() + magic, cpu, subtype, kind, count, size, flags, reserved = struct.unpack_from('<8I', data) + assert magic == 0xFEEDFACF and cpu == 0x100000C and kind == 2 + sections, segment_ranges = {}, [] + off = 32; platform = None + for _ in range(count): + cmd, length = struct.unpack_from(' dm/ios/tools/build_ipa.sh` bakes `KEY=VALUE` lines into the app (`dm/env.txt`); `Documents/env.txt` overrides them without a rebuild. Every applied switch is logged at boot (`boot: env KEY=VALUE (source file)`). The app's working directory is `Documents`, so relative output paths (`DM_NVN_SHOT=3600:shot_%d.png`, `DM_NVN_TEXDUMP`) land where the Files app can reach them. +- **Switches**: a profile's optional `ios.env` supplies production iOS-only defaults. `DM_ENV_FILE= dm/ios/tools/build_ipa.sh` replaces it with diagnostic `KEY=VALUE` lines in the app (`dm/env.txt`), while `DM_ENV_FILE=` disables the profile file. `Documents/env.txt` overrides the bundled source without a rebuild. Every applied switch is logged at boot (`boot: env KEY=VALUE (source file)`). The app's working directory is `Documents`, so relative output paths (`DM_NVN_SHOT=3600:shot_%d.png`, `DM_NVN_TEXDUMP`) land where the Files app can reach them. - **Which build ran**: the first lines name the modules (`main: … patch_off …`), the shader map size (`shader map N entries`) and, for builds that have it, the build-ID gate line. Compare them with the build you sent before reading further; users sometimes send an old log. The save path (`fs mount save:`) shows the data folder, which changes when a sideloading tool gives a reinstall a new one. - **Reading the periodic lines**: `[longframe]` gives each long frame's submit / GPU / pipeline-compile / upload split; a burst of `new pipelines` in long frames after an install is first-use compilation (see "Prewarm lists" in [docs/development.md](../development.md)). In `display link up to tick N: …`, `empty ticks` (refreshes with no new frame) and `old frames dropped by merging` are totals since launch, not per window. Per frame, `DM_NVN_TSV` has the display-link tick that took it: `@link_target_ms` (same time base as `@pres_at_ms`, so their difference is how late the system showed the frame: 8.3 ms = one 120 Hz refresh), `@link_lead_ms` (the callback's lead before that target: about 50 ms in a window, 16 ms in fullscreen at frame latency 1), `@link_ticks` (callbacks since the previous frame's; above 1 = ticks without a new frame) and `@link_gap_ms` (the longest interval between them; 33 ms on a 60 Hz link = a refresh the link skipped). `on-screen up to frame N` windows give dropped frames (a frame on screen longer than 1.5 × its own hold, so a frame-step hold of 2 vsyncs at 33.3 ms is not dropped; discarded presents count too), p99 frame time and the physical footprint; `memory up to frame N` gives resident and Metal-allocated memory. Memory that steps up at level loads and stays flat in between is not a leak. - **Power**: `power up to frame N` covers the window since the previous one, for the whole process: CPU energy in watts and per frame (`task_energy` from `TASK_POWER_INFO_V2`, CPU only; compare per frame when one build holds the frame rate better), CPU time (100% = one core) and the share on performance cores, wakeups per second, the thermal state (0 nominal … 3 critical; changes are also logged as `front end: thermal state → n`), and the two guest threads that yielded most with how many of those yields the back-off turned into sleeps (`DM_YIELD_BACKOFF`, see [docs/development.md](../development.md)), and its spin wait passes with the last loop head. A thread yielding hundreds of thousands of times per window with few back-off sleeps is a busy spin. A busy thread that never yields and is not in the list may poll in a loop the spin wait rule does not take: sample it and look at its loop. `residency set up to frame N` gives residency-set commits and their time, what was added by source line of `nvn_mtl.m` (count, MB), removals, how many draw passes began with a full barrier or without one (`DM_NVN_BARRIER=game`), and segment commits: a source line adding gigabytes per window is allocation churn. It ends with render-target traffic per frame: attachment bytes loaded and stored, passes that load and store everything, and the stores `DM_NVN_UNWRITTEN_DC` saved (the same counters as the TSV's `@rt_load_mb` / `@rt_store_mb`). `threads up to frame N` gives guest-thread CPU time in ms per second by Switch priority band (≤ 44, the main thread's level and above; 45–59; ≥ 60) and the three busiest threads with their priority; it shows what `DM_QOS_MAP` would move. On the Mac the same lines appear; energy readings vary by tens of percent between identical runs, so compare runs interleaved and over the same scene. diff --git a/docs/development.md b/docs/development.md index 84ff8725c9..fd67caadbf 100644 --- a/docs/development.md +++ b/docs/development.md @@ -395,7 +395,7 @@ Notes on these steps: - `build_ipa.sh` takes the rewritten modules from `$W/build/patched_` and the RomFS image from `$W/data//RomFS.img` (legacy folders are also accepted); `PATCHED` and `ROMFS` override them. In unsigned IPA mode, `pack_ipa.py` packs the shader libraries, RomFS and add-on content beside the app Xcode built, as `DmIOS.app/msl/b-`, `romfs` and `aoc`. Prepared Xcode projects instead sync these resources into the app before signing. - It also bundles a standard font (macOS's Arial Unicode, `STD_FONT=` overrides it) and the generated `nintendo_ext.ttf`. - The icon is the `icon.jpg` that `nx_extract` saves next to `RomFS.img`; `DM_ICON=` points at another image. -- `DM_ENV_FILE=` bakes environment switches into the app, and `DM_DISPLAY_NAME=` renames it. +- A profile's optional `ios.env` supplies production iOS-only defaults and is bundled as `dm/env.txt`. `DM_ENV_FILE=` replaces it for a build, `DM_ENV_FILE=` disables it, and `Documents/env.txt` has runtime priority over either bundled source. `DM_DISPLAY_NAME=` renames the app. - `DM_LANG=` is the game language (`DMLanguage` in `Info.plist`, default `en-US`; `main.m` sets `DM_LANG` from it unless `Documents/env.txt` or the baked env file sets it). - `DM_METAL_CAPTURE=1` makes a build that can write GPU frame captures (`MetalCaptureEnabled` in `Info.plist`, for `DM_GPU_CAPTURE`, [GPU measurement](#gpu-measurement)). - `DM_SHOW_FPS=1` turns the frame-rate counter on (`DMShowFPS` in `Info.plist`, which `front_ios.m` reads; `DM_SHOW_FPS` in `Documents/env.txt` overrides it). It counts the frames put on screen each second (`mtl_present_count`), so frames the display link merges away are not counted. diff --git a/docs/env_switches.tsv b/docs/env_switches.tsv index 30b2d1d11c..29ae7a22e4 100644 --- a/docs/env_switches.tsv +++ b/docs/env_switches.tsv @@ -1,14 +1,17 @@ name class meaning DM_AB diagnostic `=` splits the run into phases of that many frames and alternates every switch set to `ab` (on in the odd phases); logs an `[ab]` line per phase with GPU and frame time and attachment traffic +DM_AIM_ZL player `1` adds a touch Aim button that latches ZL; either Command key remains the momentary keyboard ZL control DM_ALIAS_SIZE diagnostic `=` gives the guest an alias region of that size instead of the profile's (at least 256 MB, whole host pages; the heap and stacks stay as they are): `0x1000000000`, the Switch's 64 GB, holds what a game reserves unchanged; the NSP script's address region check sets it (Mac only: an iPhone has no room) DM_ALIAS_WINDOWS diagnostic `=` logs every 30 s how much of each window of that size in the alias region is mapped (windows aligned to their size in absolute addresses) DM_ALLOW_OTHER_METAL switch `1` lets `own_compilers.py` build the shader libraries with a Metal compiler other than the one in `dmsh_chain.lock` (it stops otherwise, GitHub issue #242); the NSP script sets it for `--allow-other-metal` DM_AOC_DIR path Folder of add-on content: one numbered subfolder per DLC index holding that DLC's RomFS, reported by `aoc` and mounted read-only; the app, the Mac launcher and the NSP script set it when the build has DLC DM_AOC_OWNED switch `0` reports no add-on content even when `DM_AOC_DIR` has some, for runs without the DLC +DM_APPICON path Xcode asset-catalog app-icon name written by `build_ipa.sh` and reused by generated iOS/tvOS project settings DM_APP_VERSION switch Overrides the IPA's version in `build_ipa.sh`; by default it is the packed game's display version from its control data (the update's when one is packed) DM_AUDIO switch `1` plays audio through Core Audio, `0` uses the silent sink; real output is the default on iOS, the silent sink on the Mac (the Mac launcher sets `1`) DM_AUDIO_DUMP diagnostic `=` writes every PCM block the game appends as is (16-bit, 48 kHz, channel count as in the OpenAudioOut log), to judge whether a sound played DM_AUDIO_MUTE switch `=1` runs the real Core Audio output path (callback pacing, background take-over) with the output silenced, for lifecycle tests on the Mac without noise +DM_AUDIO_SESSION_CATEGORY player `solo` uses the normal solo audio session; `playback_mix` requests playback that mixes with other audio, and live `DM_CTL` changes reconfigure it DM_AUDREN_TRACE diagnostic When set, the CPU audio renderer logs every update with the number of frames rendered since the previous one DM_BOOTPROF diagnostic When set, logs boot timing: the loader's breakdown (mapping, relocation, probes), Metal library loading, pipeline creation, texture uploads and the first present DM_BUNDLE_ID switch Overrides the iOS app's bundle ID in `build_ipa.sh` (default `org.nxnative.`) @@ -19,6 +22,7 @@ DM_DBG diagnostic When set, prints the guest's `svcOutputDebugString` text to st DM_DBG_AU_FRAMES diagnostic `=<frames>` asks the Mac's Core Audio output for that many frames per callback instead of about 5 ms (1024 reproduces the iOS default that caused crackling) DM_DETCLOCK switch `=<step ns>`: from the first present on, the guest clock advances a fixed step per present instead of following the wall clock, and the random source becomes a fixed-seed stream (replay and test runs) DM_DETCLOCK_PACE switch `=1` with `DM_DETCLOCK`: each frame takes at least one clock step of wall time, so background loading keeps up with the fixed clock (used by the screenshot gate) +DM_DISPLAY_HZ player On iOS, `60` or `120` requests that display-link refresh rate when the screen supports it; unset leaves the system policy unchanged DM_DISPLAY_NAME switch Overrides the iOS app's name in `build_ipa.sh` (default the game name in the base NSP's control data) DM_ENTITLEMENTS path `=<plist>` gives a prepared Xcode project another entitlements file instead of the Increased Memory Limit one, and the ad-hoc signature of the unsigned IPA's executable carries them; empty means no entitlements DM_ENV_FILE path `=<file>` makes `build_ipa.sh` bake its `KEY=VALUE` lines into the app as `dm/env.txt`, applied at boot; `Documents/env.txt` overrides them @@ -39,6 +43,7 @@ DM_GAME switch Selects the game profile (a title ID under `dm/games` or `DM_GAME DM_GAMES_DIR path Profile folders outside the repository, separated by `:`, searched after `dm/games`; the NSP script points it at a provisional or derived profile DM_GAMES_FIRST switch `=1` searches `DM_GAMES_DIR` before `dm/games`, so a provisional or derived profile wins over the game's own (the NSP script's `--provisional`) DM_GAME_ENV switch `KEY=VALUE` pairs, separated by spaces, that the game's own `getenv` returns, in place of the profile's `game_env` (`hle_os.c`); any other name goes to the SDK's empty environment +DM_GFXUSE path `=<file>` records a 30-frame legacy-renderer resource-use window after the profiled main-menu ribbon becomes stable DM_GFX_FIXTURE_OUT test `=<dir>`: graphics tests write their raw result textures and buffers there DM_GLSLC_CAPTURE path `=<dir>` saves every GLSLC output the game compiles at run time; `shader_split.py` reads it (default `<version dir>/glslc_capture`) DM_GL_CAPTURE path `=<dir>`: the GL layer writes every shader source the game compiles (`<key>.vert` / `<key>.frag`) for `gl_shaders.py`; the NSP script uses it to capture run-time shaders @@ -48,6 +53,7 @@ DM_GPU_CAPTURE diagnostic `=<frame>[:<frames>]` or `gpu:<ms>[:<after frame>]` wr DM_GPU_CAPTURE_DIR path Folder for `DM_GPU_CAPTURE`'s traces (default `gpu_captures/` in the working directory) DM_GPU_CAPTURE_KEEP diagnostic Before each GPU capture, prunes the capture folder's `dm_*.gputrace` entries so that the newest `KEEP - 1` stay (default 2) DM_GUEST_SPAWN_TEST diagnostic `=1` runs a self-test before `nnMain`: a host-started guest thread does SDK malloc / free and locks an `nn::os` mutex, and the result is logged; `=2` also keeps a resident thread +DM_HID_RECEIPT_LOG path `=<jsonl>` records received Npad state samples after the host has published them, for controller-input diagnostics DM_HITCH_DUMP diagnostic `=<ms>[:<s>]`: when no frame has been presented for that long (from s seconds on), logs every guest thread once per stall: wait, last SVC, pc, lr, frames and registers DM_HLECOUNT diagnostic `=<file>` (`%d` = process ID) writes at exit a TSV of how many times the guest called each HLE function, zero counts included DM_ICON path `=<image>` gives `build_ipa.sh` another app icon (default the `icon.jpg` beside the RomFS) @@ -55,11 +61,13 @@ DM_IMPORTS diagnostic When set, logs each import the loader binds to a host func DM_KBD switch `=<text>`: the system keyboard returns that text as OK without showing an input box; unset with no window, it returns cancel DM_KBD_PLAYER switch `own` makes the Mac keyboard player 1 on its own, with controllers from player 2 on (by default the first controller shares player 1 with the keyboard) DM_KEYS switch Scripted input: `<seconds or f<frame>>:<key>[+key][~hold][*count/interval],…` presses buttons, sticks and `Shake` at those times +DM_KEY_ENTER_A test `1` makes Return/Enter publish native A in the iOS keyboard mapping fixture; normal keyboard play leaves Enter unbound DM_LANG player Game language as a language code (default `en-US`; the iOS app sets the one the IPA was packed with) DM_LAUNCHER switch `0` skips the iOS settings screen or the Mac settings window and starts the game at once, with the saved choices (on the Mac also `--no-launcher`) DM_LINK_VTICK switch `0` keeps the system's display-link rate on 144 or 165 Hz Mac screens; by default the link counts only the refreshes nearest a steady 1/60 s clock DM_LOADHASH diagnostic `=1` logs an address-independent SHA-256 per module after loading DM_LONGFRAME diagnostic `0` turns off the long-frame log (on by default), a line for a frame over 20 ms or with GPU time over 16.7 ms that is clearly above recent frames +DM_MAP_HEADROOM_MB diagnostic `=<MiB>` overrides the reserved guest-map headroom so the old fixed-headroom behavior can be reproduced and measured DM_MEASURE_LOCK path `=<file>`: `scripts/regress.py`'s default `--lock`, the measuring lock `scripts/bench.py --lock` shares: held only while a game runs, and the harness's shader builds pause while another process holds it DM_MEMSTAT diagnostic `=<tsv>` writes one line per second of memory accounting: footprint, Metal allocation and resident and charged memory by region class (guest, code, malloc, gpu, …) DM_MEMSTAT_DUMP diagnostic `=<seconds>`: with `DM_MEMSTAT`, at that second writes every VM region of 1 MiB or more to `<tsv>.regions` @@ -72,53 +80,84 @@ DM_MOTION_AXES switch `=<map>` such as `x,-z,y`: maps Apple's controller axes to DM_MOTION_GAIN switch `=<gain>`: how fast the orientation is pulled toward the measured gravity, in rad/s per unit error (default 0.5; `0` integrates the gyro alone) DM_MOTION_HID switch On the Mac, `0` disables the passive Switch Pro Controller HID motion fallback (`motion_pro_mac.h`); enabled by default when exactly one physical Pro and one GameController Pro can use the same player slot; usable GameController motion takes priority DM_MOTION_LOG diagnostic `=1` logs player 1's mapped motion sample once a second per source (phone / GameController / controller HID; acceleration in G, gyro in revolutions/s), for checking the axes: flat and still reads acc 0 0 -1 +DM_MOUSE_LOOK_SCALE player Positive divisor for iOS pointer/trackpad camera deltas (default 1400; larger values reduce sensitivity) DM_MOVIE_TRACE diagnostic When set, the movie decoder logs every call with its thread and decoder (by default the first 12 calls, then every 2^k-th) DM_MSL_DIR path Parent folder of the shader libraries: NVN loads from `b-<chain version>/` beneath it, Vulkan from `vk/`, GL from `gl/`; the app and the Mac launcher set it DM_NICK switch The account nickname the game sees (default `Player`) +DM_NO_BC_ASTC switch Selects the no-BC-texture compatibility path on older Apple GPUs; unset applies it to every unsupported BC format DM_NRO switch `0` turns off loading of the NRO modules the game's NRR files register (on by default): every `nn::ro::LoadModule` then fails DM_NVN_ALLOCPROF diagnostic `=1` charges Metal allocation growth to each replayed op type and function, and logs command allocator sizes and each new texture (serial, size, format, storage) +DM_NVN_ALPHA_RED diagnostic `0` disables alpha-from-red render-target sampling; a comma-separated shader-key list replaces the profile list DM_NVN_API switch `=<major>.<minor>` overrides the NVN API version `nvnDeviceGetInteger` reports (by default decided by the SDK version or the profile's `device_api`: 55.15 for SDK 11 and later, 53.313 before) +DM_NVN_ARGUMENT_ENCODER switch `1` forces legacy Metal argument-encoder buffers and `0` forces the direct layout, overriding automatic device selection DM_NVN_BARRIER switch How draw passes are ordered: `full` (default) waits for all earlier GPU work, `game` only for the previous pass's fragment stage unless the game set a barrier, `ab` alternates with the `DM_AB` phases DM_NVN_BF_WANT path `=<file>`: appends the key of each fragment shader whose passes could not be merged for lack of a readable-tile version to that file, once per key DM_NVN_BLIT_ORDER diagnostic `0` drops the barriers between dependent operations in the copy encoder, which reproduces the fountain's vertical stripes in Mario Wonder +DM_NVN_BLOOM_Q switch `0` disables the legacy renderer's reduced-precision bloom optimization (enabled by default) +DM_NVN_BUFFER_OFFSET switch `0` disables caching of legacy Metal buffer-offset bindings so every offset is emitted again DM_NVN_CBDUMP diagnostic `=<dir>`: saves each draw's whole uniform blocks as `F<frame>_p<pass>_st<stage>_cb<slot>_<address>.bin`, and the source bytes of buffer-to-3D-texture copies, with `DM_NVN_PASSDUMP` DM_NVN_CBDUMP_KEY diagnostic `=<shader key>`: limits `DM_NVN_CBDUMP` to draws using that shader; for a vertex shader key also saves the draw's vertex streams and index buffer DM_NVN_CBLOG diagnostic `=1` logs the command memory the game adds to command buffers (`AddCommandMemory`: the first 60, then every 500th) and the out-of-memory callbacks DM_NVN_CLEARLOG diagnostic `=1` logs the first 40 colour clears drawn as a triangle (inside a pass, with a partial channel mask or a partial scissor) DM_NVN_CMDSTAT diagnostic `=1` logs every 300 frames the command recording usage: bytes and records per frame, command memory segment sizes, out-of-memory callbacks, host segments, count and bytes per command type +DM_NVN_COLOR_DIAG diagnostic `0` disables bounded colour-attachment metadata snapshots; otherwise the legacy renderer records representative bindings for black-screen diagnosis +DM_NVN_CPU_DECODE_CACHE switch `1` enables reuse of CPU-decoded compatibility textures and `0` disables it DM_NVN_CS_HOIST switch `0` makes a compute dispatch between the draws of a pass end it (on by default: the dispatch runs ahead of the pass when that changes nothing the draws before it read) +DM_NVN_CULL_NONE diagnostic `1` disables triangle culling in the legacy renderer to diagnose missing geometry DM_NVN_CUT switch `=<draws>`: the replay commits its command buffer every that many draws, between passes (default 64; `0` one command buffer per submission, except the cut after a buffer-to-texture copy) DM_NVN_DEFER switch `0` turns off the deferred pass merging of the TBDR optimizations (which run only with `DM_NVN_TBDR=1`) DM_NVN_DEFER_AB diagnostic `=1` keeps the deferred pass merging (with `DM_NVN_TBDR=1`) off until the second screenshot has been written, then turns it on, to compare the pictures with and without it +DM_NVN_DEFER_DIAG diagnostic When set, logs extra legacy deferred-pass decisions and retirement state DM_NVN_DEPTHOVR diagnostic `=<fragment shader key prefix>,…`: those draws compare depth with `ALWAYS`; with `DM_NVN_HILITE`, tells "the depth test rejects every fragment" from "nothing rasterizes" +DM_NVN_DEPTH_ALWAYS diagnostic `1` forces depth compare Always and disables stencil tests in the legacy renderer to diagnose fully rejected geometry +DM_NVN_DIAG_SPLIT_DRAW diagnostic Comma-separated shader-key prefixes whose indexed draws are split for legacy-renderer geometry diagnosis DM_NVN_DIRTYCHECK diagnostic `=1` fully hashes pool content on every use to find changes the dirty-page tracking missed DM_NVN_DISPATCH_OWN switch `=1` gives every compute dispatch a compute encoder of its own with full barriers, as before (by default dispatches share the copy encoder); `DM_NVN_PASSGPU` does the same DM_NVN_ENC_LABEL diagnostic `=1` labels every render encoder `p<pass in frame> <width>x<height> <first 8 digits of the first draw's fragment key> d<draws>` for Instruments traces DM_NVN_ENC_THREAD player `1` runs the replay's Metal encoding on a thread of its own while the submit thread goes on translating; on by default on iOS (`0` turns it off), off on macOS DM_NVN_FAST_WAKE switch `0` makes the game's `SyncWait`s wake on the commit feedback handler instead of the GPU's own signal of the fence (on by default) DM_NVN_FB_SNAP switch Which fragment shaders sampling a colour attachment of their own pass read a snapshot of it: `0` none, `all` every shader (slow), `<keys,…>` that list instead of the profile's `feedback_snap` +DM_NVN_FILL_C0 diagnostic `1` attaches a transient colour-0 target to sparse MRT passes to diagnose missing output zero DM_NVN_FLUSH_FILL switch `0` starts a new GPU texture in a CPU-writable pool as zeros; by default one whose storage the game flushed before its first use is filled from that storage once +DM_NVN_FORCE_VISIBLE diagnostic `1` enables the legacy force-visible draw-state diagnostic; it can also be changed through the runtime control channel +DM_NVN_FORMAT_SWIZZLE switch `0` disables the legacy format-swizzle compatibility rule (enabled by default for Mario Wonder) DM_NVN_FRAMES diagnostic `=<N>` prints the NVN call table frame by frame for the first N frames (default 3) DM_NVN_FUSE_CHECK diagnostic `=1` checks pass merging in the same frame: renders the merged passes again apart and logs the comparison of the attachments every time +DM_NVN_GEOMETRY_DIAG_FILE path Output file for bounded legacy geometry diagnostics selected by the corresponding draw probes DM_NVN_GPUPROF diagnostic `=<f>`, `<f1>-<f2>` or `every:<n>`: logs the GPU start and duration of every encoder of those frames (`[gpuprof]` lines from Metal 4 counter heap timestamps) DM_NVN_GPU_TS switch `segment` or `submission`: what timestamp reports record (GPU start / end of the segment holding each report, or one time for the whole submission), instead of the profile's `gpu_timestamps` DM_NVN_HILITE diagnostic `=<fragment shader key>,…`: those draws use a solid colour instead (magenta, green, cyan, yellow, blue, white in order), to tell wrong geometry from wrong shading +DM_NVN_HILITE_FULL diagnostic `1` makes shader highlighting write all available colour outputs instead of output 0 only DM_NVN_IXCLOG diagnostic `=<n>` logs the first n indirect draw and dispatch conversions (record to arguments) once the GPU has run them DM_NVN_KEYDUMP diagnostic `=<fs key>[:n[:every]]`: for up to n draws of that fragment shader (default 6), logs the pass dump binding lines plus vertex formats, vertex streams and uniform values; meant for devices DM_NVN_LAZY_CLEAR switch `0` makes clears with no draw after them open a pass of their own, as before; by default they wait to become the load action of the next pass on that target DM_NVN_LOADPROBE diagnostic `=1`, `2` or `3`: attachments whose load is skipped are cleared to a sentinel colour (`3` NaN; `1` NaN for float formats) and the sentinel pixels left at pass end are counted (should be 0) DM_NVN_LOG diagnostic `=1` logs the creation of NVN objects DM_NVN_MAGSCAN diagnostic `=<fs key>[:every[:phase]]`: device self-check that reads each pass's colour attachments every `every` frames (default 30) and logs `[mag]` lines for solid magenta 16×16 blocks or non-finite pixels +DM_NVN_MIN_REDUCTION switch `1` enables the audited minimum-reduction compatibility pipeline when its exact shader-chain and library hashes match +DM_NVN_NOFXAA diagnostic `1` skips the legacy FXAA full-screen draw and feeds its input to the later sharpening/upscale pass for visual A/B testing DM_NVN_PASSDUMP diagnostic `=<f>`, `<f1>-<f2>` or `every:<n>`: logs every render pass of those frames (attachments, clears, shader keys, bindings, draw state, presents) for the `tbdr_*.py` tools DM_NVN_PASSDUMP_ALL diagnostic `=1` makes `DM_NVN_PASSDUMP` record every draw and binding change, not only the first per pass and stage, including copies and inline uniform updates DM_NVN_PASSGPU diagnostic `=<file>` writes the GPU time of each render and compute pass from stage-boundary timestamp counters (off under Metal 4); also gives each dispatch its own encoder DM_NVN_PASSGPU_SERIAL diagnostic `=1` makes each encoder on the main command buffer wait until all earlier work has finished, so `DM_NVN_PASSGPU` times read each pass's exclusive time (totals read high) +DM_NVN_PIPELINE_BIND_CACHE switch `0` disables caching of legacy render-pipeline binds so every draw rebinds its pipeline +DM_NVN_PIXEL_HISTORY diagnostic `=u,v[;u,v…]` tracks up to three normalized pixels through legacy render passes to locate the pass that changed them DM_NVN_PREDICT switch `0` turns off the parallel precompile look-ahead: a pipeline miss compiles only the pipeline it needs DM_NVN_PROF diagnostic `=1` wraps the implemented NVN functions in a timing wrapper and collects the time each takes during recording DM_NVN_PROGTRACE diagnostic `=<vertex keys,...>` logs matching ProgramSetShaders calls and their guest backtraces +DM_NVN_PSO_PARALLEL switch `0` disables legacy background pipeline compilation while preserving the rest of the prediction path DM_NVN_REGLUM diagnostic `=x0,y0,x1,y1:<file>` (normalized coordinates): on every present records the present number, time, mean luminance and a grayscale thumbnail of that region, to check animation speed against frame rate +DM_NVN_RENDER_COND_ALWAYS_DRAW diagnostic `1` ignores conditional-render rejection in the legacy renderer; changeable through the runtime control channel DM_NVN_RT_RANGE switch `=1` (experimental, off by default; with `DM_NVN_TBDR=1`) keeps the pass open when the game sets the same colour and depth target again with a view that differs only in its level count DM_NVN_RT_RANGE_CHECK diagnostic With `DM_NVN_RT_RANGE=1` and `DM_NVN_PASSDUMP`, renders a candidate pass both ways in the same frame and logs and writes the comparison (`CHECK_RESULTS.tsv`, `[RED]` on a difference) +DM_NVN_RULER diagnostic `1` selects the legacy renderer's reference side of its visual equivalence A/B ruler +DM_NVN_RULER_NEG diagnostic `=<fragment-key prefix>` deliberately drops a matching draw on the tested ruler side as a negative control +DM_NVN_SAMPLE_TRACE diagnostic Comma-separated shader-key prefixes whose sampled-resource and binding metadata the legacy renderer traces +DM_NVN_SAMPLE_TRACE_BYTES diagnostic `1` includes bounded resource bytes in `DM_NVN_SAMPLE_TRACE` output +DM_NVN_SAMPLE_TRACE_CB diagnostic `=<register>:<hex offset>` adds one float4 constant-buffer value to `DM_NVN_SAMPLE_TRACE` +DM_NVN_SAMPLE_TRACE_ROOT_FRAMES diagnostic Frame or frame range that bounds root-resource sampling traces +DM_NVN_SAMPLE_TRACE_VERTEX diagnostic `1` includes vertex-stage arguments in `DM_NVN_SAMPLE_TRACE` +DM_NVN_SEPARATE_STENCIL switch `0` disables the legacy renderer's separate-stencil view handling (enabled by default) DM_NVN_SHDUMP diagnostic `=<dir>` saves programs missing from the shader map as `<key>.ctl` (control section) and `<key>.code`, to find the files that hold them DM_NVN_SHMAP path The shader map (`shader_map.tsv`); `c1_full.tsv` and `pso_manifest.tsv` are taken from the same folder. Default: the game profile's folder on the Mac; the iOS app sets it DM_NVN_SHOT diagnostic `=<frame>[,<frame>…]:<path>` saves the swapchain texture as an image at those frames (`%d` in the path = frame number) @@ -127,9 +166,13 @@ DM_NVN_SKIP_FS diagnostic `=<fragment key>[,…]` (up to 16) drops all draws of DM_NVN_SKIP_SMALL diagnostic `=<width>` drops all draws whose target width is at most that, to measure the cost of tiny render passes; the picture is wrong DM_NVN_SLICE_MERGE switch `0` keeps passes into other layers of one array or cube texture level apart (on by default: they run as one layered pass) DM_NVN_SNAPRAW diagnostic `=1` makes `DM_NVN_TEXDUMP` also save each target's raw bytes, with a text header line, since converting an HDR target to an 8-bit image truncates +DM_NVN_STATE_CACHE switch `0` disables legacy render-state command caching so unchanged state is emitted again +DM_NVN_STORAGE_ACCESS_DIAG diagnostic `adapter` or `metal` selects storage-image access diagnostics used by the compatibility tests DM_NVN_T2T_FROM diagnostic `=<frame>`: `DM_NVN_T2T_TRACE` records copies only from that frame on (default 0) DM_NVN_T2T_TRACE diagnostic `=<W>x<H>:<file>`: after each copy into an RGBA16F texture of that size, copies the result into a trace buffer on the GPU timeline; 240 entries are written to the file DM_NVN_TBDR switch `=1` turns on the replay's TBDR pass optimizations (store / load elision, pass merging, same-target continuation); off by default since bindless textures defeat their read inference +DM_NVN_TEST_FRZ test `1` stands in for the game's world-frozen status so the legacy freeze/extrapolation path can be tested +DM_NVN_TEST_FRZ_NEG test When set, keeps the normal UI-stencil draw during freeze testing as a negative control for exposed old pixels DM_NVN_TEXCPU diagnostic `=1` logs non-packaged (non-swizzled) textures in CPU-visible pools DM_NVN_TEXDUMP diagnostic `=<frame>:<path>`, `size=<W>x<H>:<path>` or `layers=<n>:<path>`: saves every colour render target of that frame as PNG (`%d` = serial × 100 + layer × 10 + level) DM_NVN_TEXINFO diagnostic `=<W>x<H>` logs storage and description of every texture of that size on first use, registration and initialization (pool, flags, packaged, non-zero bytes, swizzles) @@ -147,6 +190,8 @@ DM_NVN_UB_N diagnostic `=<frames>`: the length of each phase of the same-process DM_NVN_UB_PARTIAL switch `0`: a uniform block the deferred updates cover only in part ends the pass, as before; by default its shadow starts from memory and takes the submission's queued updates (on by default) DM_NVN_UDC_AB diagnostic `=1` starts with the DontCare store for unwritten attachments off and turns it on after the second screenshot, for an on/off picture comparison in one run DM_NVN_UNWRITTEN_DC switch `0` turns off storing attachments a render pass did not write as DontCare (on by default; part of the TBDR store lookahead), for equivalence comparisons +DM_NVN_UPLOAD_DIAG_FRAMES diagnostic `=<frame cap>:<label>` bounds upload diagnostic records and labels why collection stopped +DM_NVN_USE_DEDUP switch `0` disables legacy renderer resource-binding deduplication (enabled by default) DM_NVN_VBUF_CACHE switch `0` turns off the cache of resolved vertex stream addresses and vertex argument table writes across draws (on by default), for A/B runs DM_NVN_VCONV_CHECK diagnostic `=1` scans the indices again at every reuse of a cached vertex conversion range (`vconv_range`) and reports differences DM_NVN_VCONV_CPU switch `=1` converts integer vertex inputs fed formats of another class on the CPU, as before, instead of through the shader's shadow inputs (`<key>_bv`) @@ -158,17 +203,23 @@ DM_NVN_VSDUMP_KEY diagnostic Vertex key to capture, or `*` for all eligible vert DM_NVN_VSDUMP_MAX diagnostic Maximum post-vertex captures (default 1, or 256 for the wildcard key) DM_NVN_WAITS diagnostic `=1` turns on the wait ledger: every place a host or game thread waits on the CPU for the GPU or the encode thread, counted per frame DM_NVN_XV switch `0` turns off the cross-submission attachment validity ledger, which skips write-backs of colour attachments cleared before their next use (on by default, only with `DM_NVN_TBDR` on) +DM_NVN_ZA_DIAG diagnostic `1` enables a bounded Pokémon Z-A attachment readback diagnostic on the audited legacy renderer path DM_NVN_ZBLOCK diagnostic `=1` logs (`[zb]`) the all-zero blocks per level of each ASTC texture uploaded on the CPU (magenta when drawn); `=<folder>` also writes the raw storage of the first one per format and size DM_NVN_ZSKIP switch `0` turns off the zero-source skip, which drops a draw whose fragment shader is proven to write 0 over targets already 0 (on by default, only with `DM_NVN_TBDR` on) DM_OE_SCRIPT diagnostic Scripted applet state changes at given seconds, `key=value@seconds,…` with keys `focus`, `opmode` and `bg` (`bg=1` / `bg=0` run the whole background round trip), e.g. `bg=1@20,bg=0@25` +DM_OODLE_COMPRESS_JOB path `=<job file>` runs the game's Oodle compressor after module initialization, verifies a decode round trip and writes `<job>.out` DM_OODLE_HOST path The Mac host binary that `shader_split.py` runs in its Oodle tool mode to decode Trinity packs (`ONEPACK` files); set by the NSP script DM_OODLE_JOB path `=<job file>`: the host runs the game's own Oodle decoder on every record of the job file right before `nnMain`, writes `<job file>.out` and exits (Unreal shader archives) DM_OODLE_MODULES path The rewritten modules the Mac host's Oodle tool mode loads when `shader_split.py` decodes Trinity packs; set by the NSP script DM_OODLE_SERVE switch `=1`: the host answers decode requests on stdin / stdout with the game's own Oodle (or LZ5) decoder from the guest main thread before the SDK starts, until stdin ends DM_OPMODE player The operation mode the game sees: `0` handheld, `1` console (default: the profile's `op_mode`, console unless set); read once at startup +DM_PAD_FORCE_FULL player `1` always shows the complete iOS touch-controller overlay, including every D-pad, face, shoulder, stick and utility control +DM_PAD_KEEP_TOUCH player `1` keeps the complete touch controller visible and usable while a physical controller, keyboard or pointer is connected DM_PAD_MERGE switch `=1` on the Mac merges every controller into player 1, as before, instead of giving each controller a player of its own DM_PATCH_FRONT switch `=<bytes>`: the size of the patch area right before the module base for a long text (`BIG_TEXT`); set only by the rewriter's own rerun with the size it needs DM_PHONE_MOTION switch On iOS, whether the phone's own device motion drives player 1's six-axis sensor: `auto` (default) while no controller supplies fresh motion, `1` phone only, `0` never; each source keeps its own orientation +DM_PIPELINE_CACHE switch `off` disables the legacy on-disk render-pipeline cache; otherwise the cache is enabled +DM_PIPELINE_CACHE_DIR path Folder for the legacy render-pipeline cache, overriding its default per-game location DM_PREEMPT switch `0` / `1` overrides the profile's `core_preempt`: also check already-runnable higher-priority peers on the caller's core after every thread SVC DM_PRELOAD diagnostic Set: on the Mac, runs the host's early background loading (`dm_host_preload_start`) as the iOS app does, to exercise that path DM_PRESENT_FIFO switch `0` makes the display link take the newest queued frame each tick, as before; by default it shows frames in order, at most one tick behind @@ -223,13 +274,21 @@ DM_RESOURCE_SHADERS path iOS shader library folder, written to `Gen/resources.en DM_RES_CHECK diagnostic `=1` prints the first 40 textures or buffers a shader reaches through its argument block that are not in the residency set (textures attributed to their parent allocation) DM_ROMFS path The game's RomFS folder, mounted read-only as `rom` (default `$DM_ROOT/romfs`); the Mac launcher, the iOS app and the NSP script set it DM_ROMFS_OVERLAY path `=<dir>[:<dir>…]`: RomFS reads look in these folders first, in order, and fall back to `DM_ROMFS` (for a modified copy of some files) +DM_ROMFS_PATCH path On tvOS, patch specification staged beside the app resources and applied to the mounted RomFS before launch +DM_ROMFS_REDIRECTS path `=<tsv>` maps virtual RomFS archive byte ranges to disjoint backing files, used by the Z-A ASTC compatibility pack +DM_ROMFS_REDIRECTS_V1 test Version-one magic at the head of a `DM_ROMFS_REDIRECTS` manifest (listed because the source scanner treats quoted DM names uniformly) DM_ROOT path Workspace root (default `~/dm-work`): the host's and tools' default paths (RomFS, temporary storage, shader libraries under `build/cache/metal_shader_cache`, probe dumps) are under it DM_RT diagnostic `0` leaves the thread that acquires the window on its QoS only, with no Mach time-constraint policy declaring the frame deadline (on by default; for power A/B) +DM_RT_AB diagnostic `0` selects original rendering and `1` selects the experimental legacy extrapolation renderer, pinning the choice for the run +DM_RT_AB_TOGGLE diagnostic `=<seconds>` alternates `DM_RT_AB` rendering sides on that wall-clock interval for visual comparison DM_RT_COMP switch `=<ms>` fixes the computation the present thread's time-constraint policy declares; default `auto` re-declares it every 120 frames from that thread's per-frame CPU time (p90 and peak) +DM_RT_DBG diagnostic When set, emits bounded per-frame state for the legacy extrapolation/freeze renderer +DM_RT_TEST_NOPSO test When set, pretends the extrapolation pipeline cannot be built and verifies the run falls back to original rendering DM_RUMBLE player `0` turns game rumble off (on by default); launcher setting DM_RUMBLE_GAIN player `=0..3` scales the rumble strength (default 1, `0` is silent); launcher setting DM_RUMBLE_LOG diagnostic `=1` logs the game's vibration handle requests and, once a second, the values it sent, how many had no routed output and the peak amplitude DM_RUMBLE_MIN player `=0..0.9`: controller rumble levels below it stay silent and the rest is stretched over the full range (default 0; the phone is unaffected); for games whose quiet HD rumble texture keeps rotating motors running (Mario Kart 8 Deluxe: 0.45); launcher setting +DM_RUN_BUTTON player Selects the native button published by the iOS touch Run control; unset uses native B DM_SAVE path This run's save folder (must be set); the device save is `<DM_SAVE>_device`, and `pso_user.tsv` goes in its parent folder; the Mac launcher, the iOS app and the NSP script set it DM_SAVED_CHOICES switch `0` ignores the player's saved choices (launcher.<VAR> in the app's preferences) and leaves them saved; the Mac launcher and the iOS settings screen DM_SCHEME_NAME switch Set by `build_ipa.sh` itself for xcodegen's `project.yml`: the Xcode scheme name, `DmIOS`, or `<display name> (<title ID>)` for a project prepared with `--prepare-xcode` @@ -253,22 +312,30 @@ DM_TEST_BUILD test Build folder of `dm/host/tests` whose `t_svc_memory` `test_me DM_TEST_FS_FAIL diagnostic `=write:<n>[:<errno>]` makes the nth save write fail with that errno (default ENOSPC), which refuses the save transaction's commit DM_TEST_FS_FAILAT diagnostic `=<point>[:<count>][,…]` makes steps of the save transaction (`mountclone`, `reclone`, `guestname`, `renameback`) fail with ENOSPC DM_TEST_GEOMETRY test Path of the native `t_geometry` executable; `test_dmsh_geometry.py` runs its native geometry emitter checks only when it is set +DM_TEST_GHOST test `=px,py,gx,gy,gw,gh,cx,cy` enables the drag-follow image directly for Mac screenshot regression testing DM_TEST_KEYRACE diagnostic `=<n>` creates up to n (at most 512) pthread keys on a thread of its own while the host starts up DM_TEST_KILL_AT diagnostic `=write:<n>` exits the process (`_exit(9)`) at the nth save write, imitating a kill mid-write DM_TEST_LEAK_GPU diagnostic `=1` allocates and keeps a 1 MB shared Metal buffer every 100 ticks: a GPU memory leak on demand DM_TEST_LINK_STALL diagnostic `=<start s>,<duration s>`: counting from the first present, pauses the display link at that point for that long, to check the present fallback +DM_TEST_NO_BC test `1` forces the no-BC-texture path on hardware that normally supports BC, reproducing older-GPU compatibility behavior DM_TEST_NO_METAL4 diagnostic `=1` treats the GPU as one without Metal 4, as on a virtual Mac: the NVN layer starts only its classic part and drops every present DM_TEST_PRESENT_LAG diagnostic `=<every n>,<ms>[,<in a row>][;…]` sleeps that long before every nth present (and the ones after it in the run): a slow frame on demand +DM_TEST_ROMFS_PATCH_EXIT test Names a tvOS RomFS-patch stage at which the test harness exits to verify crash-safe recovery DM_TEST_SWKBD_HOLD_MS diagnostic `=<ms>` (with `DM_KBD`) keeps the system keyboard up that long before `DM_KBD`'s text is returned, as a player typing would DM_TEST_SWKBD_LAG_MS diagnostic `=<ms>` sleeps that long between the keyboard handing back its text and the game's thread getting the result, to check that auto-accept does not act early DM_TEST_TRANSFORM_FEEDBACK test Path to the native t_transform_feedback executable used by the Python feedback tests +DM_TEST_TV_RESOURCE_MARKER_FAIL test `1` injects an ENOSPC failure while writing the tvOS resource-ready marker DM_TEST_UP_LEAK diagnostic `=1` keeps the old upload staging block instead of retiring it, as before the fix, to measure the leak DM_TEST_UP_LOST diagnostic `=<batch index>` treats that batch of uploads as dropped, to exercise the re-upload path DM_THERMAL_CAP switch `0` turns off the thermal cap, which holds the frame step at 30 fps while iOS reports a serious or critical thermal state (on by default) DM_THR diagnostic `=1` logs each SVC (except `SleepThread`) with its first four arguments, and each `CreateThread` with entry, stack, priority and core DM_THRCPU diagnostic `=<tsv path>[,<seconds>]` writes each thread's CPU time (100% = one core) and the total every interval (default 10 s); on iOS a relative path is under `Documents` +DM_TOUCH_DIAG diagnostic `1` logs iOS virtual-controller ownership, press, release and layout decisions DM_TOUCH_HAPTICS player `0` turns off the light haptic tap when a touch control is pressed (on by default); launcher setting DM_TOUCH_LOG diagnostic `=1` logs each touch-screen read with touch points (sample, finger, position, attributes) and each gesture entry, up to 500 lines +DM_TV_EARLY_BOOT_DIAG diagnostic `1` enables early tvOS boot checkpoints before the normal host logger is available +DM_TV_MEMORY_DIAG diagnostic `1` logs tvOS memory snapshots during resource staging and host startup +DM_TV_VALIDATE_LOADER test When set, tvOS validates the prepared loader/resources and exits before starting the game DM_UDC_SELFTEST diagnostic `=1` runs a start-up check of DontCare load and store on private depth-stencil and RGBA8 textures (clear, Load + DontCare, read back) and logs one result line DM_UE_SHADERS path Folder of the Unreal shader code archives decoded by `uepak.py`, for `shader_split.py` (default `ue_shaders` beside the RomFS); needed when the RomFS has Unreal paks DM_VK_CAPTURE path `=<dir>` (Mac) writes every SPIR-V module the game creates to `<dir>/<key>.spv`; the NSP script's Vulkan shader capture stage sets it @@ -279,6 +346,11 @@ DM_WATCH_SAMPLE diagnostic `=<path>` (Mac): when the stall watchdog fires, sampl DM_WATCH_STALL diagnostic `=<s>`: when the frame loop makes no progress for that long (default 15 with `DM_WATCH`), logs a red line, dumps the threads and exits DM_WINDOW switch When set (to any value, `0` included) opens a Mac window with keyboard and controller input; unset runs headless with the guest on the current thread; the Mac launcher sets it DM_XCODE_DERIVED_DATA path Xcode derived data folder for `build_ipa.sh` (default `$W/build/ios/xcode-dd`); the NSP script points it into the game's build folder +DM_XCODE_OUTPUT_ROOT path Derived-data root shared by generated Xcode settings and resource staging so unsigned iOS/tvOS builds copy into the correct product DM_XCODE_PROJECT_DIR path Folder of the per-game Xcode project that `build_ipa.sh --prepare-xcode` makes (default `$W/xcode`); the NSP script sets it DM_XV_FILE path File where the attachment validity ledger keeps the keys it disabled after a violation, read at the next launch DM_YIELD_BACKOFF switch `0` turns off the yield back-off; `=<cap µs>[:<start µs>]` sets its sleep cap and the run of yields after which it starts (default 250:2000) +DM_ZA_FPS_MODE player Pokémon Z-A timing mode: `game`, `static60`, `adaptive60` or `dynamic60`; live changes require validated gates and `DM_ZA_TIMING=1` +DM_ZA_SKILL_DIAG diagnostic `1` logs audited Waza-layout visibility transitions used by the virtual face-button geometry +DM_ZA_SKILL_LAYOUT player `1` changes only the iOS virtual face-button geometry while the audited Z-A Waza layout is visible; native A/B/X/Y meanings do not change +DM_ZA_TIMING switch `1` enables audited Pokémon Z-A timing gates, diagnostics and controlled live `DM_ZA_FPS_MODE` changes diff --git a/docs/ios16-audio-recovery.md b/docs/ios16-audio-recovery.md new file mode 100644 index 0000000000..8a8597491c --- /dev/null +++ b/docs/ios16-audio-recovery.md @@ -0,0 +1,95 @@ +# iOS 16 audio-session diagnostics and recovery + +The game keeps the SoloAmbient category and therefore respects Silent Mode. Its +non-mixing audio session is released when its scene resigns active, including a +visible but inactive Stage Manager card, and on background entry. AudioUnits are +stopped before deactivation with NotifyOthersOnDeactivation. Graphics behavior is +unchanged for a merely inactive card; another running app or VM is not closed. + +Activation failure keeps real output suspended while the silent sink continues +buffer pacing. Main-queue activation retries back off through 1, 2, 4 and 8 seconds, +then continue at eight-second intervals only while active, foreground and not +interrupted. A long-held session therefore remains recoverable. Unchanged failure +messages are suppressed after the first few attempts; changed errors are reported. +Generation tokens cancel stale retries on lifecycle changes, interruptions, +successful activation and media-service resets. Repeated route notifications do +not reset the budget or recursively reconfigure the category. Foreground activity +starts a new recovery opportunity; a competing audio session is not forcibly ended. + +After a media reset, missing AudioUnits retain their real-output intent. A started +output retries recreation with backoff up to eight seconds; stopped and suspended +outputs are not started by that retry. Creation happens on a helper stack. Backend +ownership plus a non-blocking pull guard keeps an obsolete callback from becoming +a second queue consumer when the device is stopped or reset. + +Session events report category, mode, options, output volume, current output port +types/names, sample rate, I/O duration, latency, interruption and lifecycle state. +These detailed event snapshots are capped at 64 per run. The callback publishes +only a bounded PCM level summary after gain and diagnostic muting, sampling no more +than 4,096 PCM16 values. It performs no allocation, logging, locks or environment +lookup on the normal callback path. The watchdog reports peak, RMS and zero counts +after the first 100 callbacks and every 2,000 thereafter; raw PCM is not logged. +Nonzero PCM and advancing callbacks establish delivery to the AudioUnit, not +audibility at the speaker. Route selection and Silent Mode still need inspection. + +For a reversible Silent Mode comparison, `DM_AUDIO_SESSION_CATEGORY=playback_mix` +selects Playback with MixWithOthers. Unlike the default SoloAmbient category, +Playback ignores Silent Mode; the mixing option permits other apps' audio to +continue. `DM_AUDIO_SESSION_CATEGORY=solo`, an unset value or an empty startup +value retains the original category. Other values are rejected. This diagnostic +does not choose an output port, adjust system volume or terminate another app. + +The existing CTL interface can change the category without restarting the game: + +```text +env DM_AUDIO_SESSION_CATEGORY=playback_mix +env DM_AUDIO_SESSION_CATEGORY=solo +``` + +Only the exact category key reaches the platform-neutral registered observer. +The iOS observer parses the value before CTL's line buffer is reused, then +captures the enum onto the main queue. It stops its AudioUnits and deactivates +its own session before configuring the chosen category and reactivating. Inactive, +background, interrupted and media-services-lost states defer the change until +eligible; a category request cancels the preceding configuration's retry epoch. +The default category and all interruption/lifecycle eligibility rules remain in +effect. No additional framework dependency is introduced in the generic HID host. + +Explicit category commands produce route/PCM snapshots before and after the +transition, plus a guarded snapshot after 250 ms with the number of new callbacks. +These explicit diagnostic snapshots remain available after the normal 64-event +log budget is exhausted. A subsequent focus change, category change or recovery +epoch invalidates the delayed snapshot. Requested policy and actual category, +mode, options and route are logged separately so a failed configuration cannot be +mistaken for a successful comparison. + +These fix concrete recovery defects but do not establish the cause of a reported +system-wide mute. The initial device log had advancing callbacks and fed queues +without an activation failure. A later foreground snapshot after the recovery +changes showed Speaker output, two channels, volume about 0.7 and nonzero +post-gain PCM (peak 3,834; RMS 0.0386; zero samples 0/2,048), while the user still +reported silence. The category comparison distinguishes Silent Mode behavior +from a fault further downstream; it does not establish the cause in advance. + +Hardwareless regressions: + +```sh +clang -O1 -Wall -o /tmp/t_audio_session_retry dm/host/tests/t_audio_session_retry.c +/tmp/t_audio_session_retry +clang -O1 -Wall -I dm/host -I build/compile/profiles/0100F43008C44000 \ + -o /tmp/t_audio_recovery dm/host/tests/t_audio_recovery.c -framework AudioToolbox +/tmp/t_audio_recovery +``` + +These tests cover parser defaults and rejection, live CTL callback delivery, +concurrent observer registration and retry cancellation on category/focus +transitions. AudioUnit lifecycle tests use injected calls; no hardware test claims +speaker audibility or AVAudioSession category behavior. + +Apple references: [session activation](https://developer.apple.com/library/archive/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/ConfiguringanAudioSession/ConfiguringanAudioSession.html), +[interruptions](https://developer.apple.com/library/archive/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/HandlingAudioInterruptions/HandlingAudioInterruptions.html), +and [media-services reset recovery](https://developer.apple.com/library/archive/qa/qa1749/_index.html). +Category diagnostic references: [Playback](https://developer.apple.com/documentation/avfaudio/avaudiosession/category-swift.struct/playback) +and [MixWithOthers](https://developer.apple.com/documentation/avfaudio/avaudiosession/categoryoptions-swift.struct/mixwithothers). +Apple permits games without a media Play/Pause interface to resume after an +interruption without requiring ShouldResume; that behavior is retained. diff --git a/docs/ios16-compatibility.md b/docs/ios16-compatibility.md new file mode 100644 index 0000000000..2196ab77e3 --- /dev/null +++ b/docs/ios16-compatibility.md @@ -0,0 +1,96 @@ +# iPadOS 16 compatibility checkpoint + +This branch carries the cumulative traditional Metal backend port and iPadOS 16.1 build changes. It is based on upstream commit `ebe75c9d5d34486f714678f850f0e74bbab398ac`. It needs the user's own game inputs; none are part of this branch. + +## Rendering and memory + +The traditional Metal backend replaces newer Metal interfaces, explicitly declares resources reached through argument buffers, indexes buffer addresses, and decodes unsupported BC formats. Embedded guest code stays in the executable's original signed text mapping while its writable data hole is populated separately. `dm/ios/tools/set_text_maxprot.py` updates the unsigned executable's maximum text protection before external signing. + +GPU-written indirect records are sanitized on the GPU, in batches, without a mid-frame CPU wait. A draw's base instance is copied into its vertex driver constants at c0+0x644. Each command occurrence owns its constants; later draws cannot rewrite an earlier draw's SSBO table, and CPU initialization preserves the GPU-owned word even when encoding continues after a split commit. Returning to a direct draw invalidates the indirect vertex binding. + +Guest pool aliases are not fully represented by Metal resource object identity. Render, compute and blit replay passes therefore use an explicit queue-local fence. NVN Barrier ends an active pass and invalidates argument state. Without real barriers, the device hit GPU Hang in a single-texture-sample 7x4 postprocess pass, then rejected later submissions. + +Pipeline topology must match the actual draw. The former point-size workaround declared Point topology for triangle draws; Metal API validation rejects that combination. When an older compiler rejects an unused vertex point-size output for a non-point draw, the backend now compiles a separate vertex function whose point-size output is an unused user varying. It retains the original depth-range entry, argument layout, point function and transform-feedback function. At most two source compilations run concurrently. + +Combined depth/stencil allocations declare PixelFormatView usage before they become attachments. iOS 16 validation requires this for the stencil-only X32 view, including views selected implicitly by depth/stencil mode rather than an explicit format change. + +The non-point fallback needs supplemental copies of the user's original translated MSL. Before packing an IPA, export them into the iOS library folder; the normal IPA and Xcode resource staging already copy this nested directory: + +```sh +python3 dm/ios/tools/export_nonpoint_sources.py \ + "$CACHE/b-$CHAIN" "$IOS_LIBS/b-$CHAIN" \ + "$IOS_LIBS/b-$CHAIN/nonpoint-v1" +``` + +`CHAIN` must match `dm/gfx/nvn/dmsh_chain.h`. The export does not change the translation chain or base libraries. Its manifest records the source and original iOS library SHA-256 values, both verified before runtime compilation. These game-derived sources belong beside the user's game inputs, never in Git. + +Image-atomic companions also need image-coordinate bounds and array-layer addressing. The original linear companion calculated a raw pointer from projected coordinates without validating them, and its 2D macro ignored the layer of a 2D array. Shader Validation captured `atomic_fetch_max` past a 2,228,224-byte buffer immediately after entering the station. Enlarging the allocation cannot make arbitrary off-image coordinates safe. The legacy variant guards the operation before evaluating its pointer, returns zero for out-of-image coordinates, and includes the layer in the linear offset. The host copies every array plane into the private texture after the producer. Export these variants before packing as well: + +```sh +python3 dm/ios/tools/export_legacy_atomics.py \ + "$CACHE/b-$CHAIN" "$IOS_LIBS/b-$CHAIN" \ + "$IOS_LIBS/b-$CHAIN/atomic-v1" +``` + +The export records the original source hash, bounded source hash and original library hash. The backend requires a matching chain, variant kind, bounded source and base library before compiling the companion. It does not modify the base chain libraries. + +## Input and timing + +Keyboard movement uses WASD; arrow keys turn the camera. J/K/U/I map to A/B/X/Y. Virtual face-button positions are A bottom, B right, X left and Y top, with hit testing following the same logical positions. The touch overlay hides while a physical controller, keyboard or mouse is connected and returns after disconnection. No visible right stick was added. + +Mouse and trackpad camera input accepts GCMouse relative movement. UIKit hover and trackpad scroll gestures provide fallbacks, with recent raw input taking precedence over duplicate hover events. A single timer releases camera input after the last movement, including during touch tracking. + +The FPS overlay counts actual drawable presentations, independently of attempted presents used for screenshot scheduling. A missing drawable or discarded presentation no longer increases the displayed frame counter. + +For Pokemon Legends: Z-A, use `DM_PRESENT_INTERVAL=game`. The game sets interval 2; the former `display` override ignored it and is not a correct-speed 60 fps patch. This checkpoint follows the game's timing instead. + +## Validation and limits + +Local device package v48 initially passed 8,000 frames and camera rotation, but the longer run failed with GPU Hang at frame 65,655. That checkpoint was insufficient evidence of stability. Metal API validation then identified the invalid point/triangle topology combination; after correcting that, it identified the missing stencil-view usage flag. + +On 2026-10-25, v50 ran with both Metal API Validation and Metal GPU Validation enabled, loaded the station save, and continued for 5,160 frames without API assertions or command-buffer failures in log.txt. Fifty-seven non-point vertex variants compiled successfully. This was not a clean shader-validation result: Shader Validation writes to the system log by default and zero-fills invalid reads. Its reports were not yet being collected. Validation substantially reduces performance; its frame rate is not representative of normal play. + +Normal-speed v51 failed at frame 4,068 after horizontal rotation and upward camera input. The first fault was a 60x33 sampling pass; a later address fault and SubmissionsIgnored were consequences, not independent first causes. The API fixes are necessary but insufficient. Source review then found that raw sampler IDs had no submission-owned strong references when descriptor re-registration released the table's reference. V52 retains all encoded sampler states until GPU completion and records selected sampling bindings in memory, emitting them with the associated command buffer's error. V52 still failed during the same camera sequence, and disabling split submissions did not fix it. No sampler replacements were observed in that reproduction, so sampler retirement was not established as its cause. + +Enabling Shader Validation's abort-on-fault stopped the app immediately after loading the station and exposed an MTLGPUDebugBufferErrorLog, confirming that the zero-fill mode had hidden a buffer error. V53 reads the public MTLCommandBuffer.logs container on completion, including function debug locations, rather than relying on system-log forwarding. It captured invalid atomic_fetch_max writes from the image-atomic pass, including offsets 2,231,976 through 2,589,172 past a 2,228,224-byte companion buffer. V54 installs the guarded, array-aware companion described above. It synchronizes all three 480x270 array planes and passed more than 2,552 frames with API and shader validation, including camera rotation, without validation reports. A separate normal-speed run reached about 15,720 frames without a GPU error, then crashed with CPU SIGBUS in the first store of SDK memset (sdk+0x56e3ac). This is not a stability pass. + +The subsequent dirty-page lifetime audit found that protection could hold a range descriptor across concurrent pool retirement. Retirement restored RW and published live=0, then the old protection call could set the now-untracked memory read-only again. The deterministic t_dirty_retire_race control reproduced an EFAULT writing into the retired pool. V55 holds the pool lifecycle mutex across protection, preventing both retirement and slot reuse until the final mprotect has landed. The regression and existing shared-page/write-preparation tests pass. Terminal CPU fault diagnostics now include ESR, si_code, VM protection and matching live/retired dirty slots. The observed V54 CPU crash has not been conclusively tied to this race. + +V56 retains the lifetime fix and registers the optional fault diagnostic through a callback so crash.c still links independently. The terminal diagnostic was checked with an intentional read-only write fault; it reported WnR=1 and VM protection READ. A normal-speed iPad station run reached 31,500 frames (about 17.5 minutes), including 443 controller actions: title confirmation, 80 four-direction camera cycles, and a further 20 camera/movement cycles. Intermediate screenshots confirmed actual camera and character response. No CPU crashes, command-buffer errors or shader reports appeared; the final footprint was 2,363 MiB. This covers this station reproduction, not every subsequent scene. The automatic input queue was cleared and DM_CTL removed from the next-launch environment afterward. These fixes are included in this branch and still require upstream review. + +This is a limited real-device checkpoint, not a claim that every scene or other game is correct. Longer play, all scene assets, controller variants and regressions in other games still need testing. The broad backend port should be reviewed separately from the smaller input and synchronization fixes before upstream integration. + +Checks passed: complete unsigned arm64 iOS Release build targeting iOS 16.1; targeted cross-compilation of rendering and input units; 10 present-pacer checks; 12 shader-chain consistency tests using Python 3.15. Upstream's build script syntax requires Python 3.12 or newer. + +## A10X UI address fault checkpoint (2026-10-26) + +The older A10X device needs the Tier 1 argument encoder, baseline LL/SC host atomics, verified per-slice layered companions and buffer-backed fragment zero-write companions. Reaching its language screen did not establish stability: the original UI submission failed at frame 55, after which rejected submissions caused flashing. Splitting only the selected UI draws localized the fault to four draws using the same vertex/fragment pair and nonzero UV flags. + +An independent render probe reproduced the address fault with the captured constant, vertex, index and C1 bytes and freshly allocated resources. Changing only texture swizzle still failed. Setting only the vertex flags to zero completed with 54,720 nonclear pixels. Keeping the original flags and changing four dead index initializers to zero also completed all four previously failing draws, with readback coverage of 54,720, 6,480, 54,720 and 6,480 pixels. This supports a legacy compiler workaround for the inactive guarded-address path; the exact compiler instruction causing the fault has not been inspected. + +`export_guarded_index_sources.py` proves a complete same-guard coordinate/index/load chain with closed uses. When the loads are active, both seeds are overwritten before use. When inactive, no seed-dependent memory access is semantically executed. The exporter recognizes token structure rather than shader keys or temporary IDs, rejects unproven shapes, changes only the reflected `entry_z`, and retains the original point and capture/TF functions verbatim. Fourteen exporter/regression tests pass, including 288 executed active/inactive semantic cases. Export after the verified non-point sources: + +```sh +python3 dm/ios/tools/export_guarded_index_sources.py \ + "$IOS_LIBS/b-$CHAIN" "$IOS_LIBS/b-$CHAIN/guarded-index-v1" +``` + +The runtime uses these companions only below Apple4/Mac1, verifies the original source, transformed source, base library and reflection hashes, and retains the base function when no proof record exists. An invalid present record fails that non-point variant. The original shader chain and libraries remain unchanged. + +The installed v72 guarded-index host passed a 25-second native-pacing startup beyond frame 479 and a separate 32-second dynamic-pacing startup beyond frame 900 without a GPU error or abort. Screenshots at 16 and 24 seconds showed the complete language screen. With `DM_DISPLAY_HZ=120`, `DM_PRESENT_HZ=120` and `DM_ZA_FPS_MODE=dynamic60`, measured display callbacks were approximately 119.97 Hz while actual drawable presentations were approximately 31–34 fps on this device. The steady game-clock window ratios were 0.999390 and 0.999752. This is an uncapped request within the display's supported rate, not evidence of 120 rendered fps. + +A nonconfirming virtual ZL hold was observed in the game's actual GetNpad return, including press/release state changes. No language confirmation, character creation or battle input was automated. The game remains at language selection with clean requested settings. Nonzero fragment image writers and layered shaders without proofs still report unsupported, so these checks do not establish complete playability. + +## Tier 1 sampler-limit companion (2026-10-26) + +Metal Tier 1 argument buffers allow at most 16 indirect sampler resources in one function. SPIRV-Cross can nevertheless declare a sampler beside a texture that the shader accesses only with `read()`. On the A10X character profile screen, fragment shader `a6c06cda878a4fe0_b` declared 17 samplers and Metal rejected its pipeline with `Total number of indirect argument buffer resources exceeded for samplers (17/16)`. The geometry, hair, eyes and clothing still rendered, but the rejected transparent-material pass left the face dark. + +`export_sampler_limit_sources.py` scans the complete source, strips comments and literals for identifier analysis, and removes only scalar sampler declarations whose identifiers occur nowhere outside their exact argument-structure declarations. It never removes a texture, changes a texture operation or renumbers an argument ID. Sampler arrays, macros, writable image companions, reflection disagreement and shaders without enough unused samplers fail closed. Export the companions beside the chain-matched iOS libraries: + +```sh +python3 dm/ios/tools/export_sampler_limit_sources.py \ + "$CACHE/b-$CHAIN" "$IOS_LIBS/b-$CHAIN" \ + "$IOS_LIBS/b-$CHAIN/sampler-limit-v1" +``` + +The Tier 1 runtime verifies the chain, proof kind, source, base-library and reflection hashes, removes the same authorized binding from its private argument-encoder reflection, and compiles the companion with invariant preservation and fast math disabled. Tier 2 devices continue to load the original metallib. The profile-screen shader's unused sampler at argument ID 36 is reduced from 17 to 16 while all 32 `read()` calls through its texture remain unchanged. Eight focused exporter tests, the existing iOS compatibility tests, 21 related exporter tests, `git diff --check` and an unsigned A10 Release host build pass. This is source and package validation only; the unsigned build still requires user sideloading and real-device confirmation. diff --git a/docs/ios16-display-refresh.md b/docs/ios16-display-refresh.md new file mode 100644 index 0000000000..0419f2b0b0 --- /dev/null +++ b/docs/ios16-display-refresh.md @@ -0,0 +1,71 @@ +# iOS 16 automatic high refresh + +The Z-A iOS production package requests up to 120 Hz automatically, with no additional +settings screen or interaction. `DM_DISPLAY_HZ=120` requests callback cadence +through UIKit's `CADisplayLink`, available on this iOS 16 build. The preferred +rate is clamped to the window screen's supported maximum, up to 120 Hz. A screen +which supports only 60 Hz requests 60. Low Power Mode, thermal conditions and +system policy may still provide a lower callback cadence. The supported maximum +and requested/preferred rate are logged independently from measured results. +An unset `DM_DISPLAY_HZ` retains the existing system policy; an explicit 60 value +remains available for diagnostic comparisons through the environment file. + +This frontend link never acquires a drawable, submits Metal work, generates +frames or advances the guest clock. The existing classic presentation thread +continues to own `nextDrawable` and `presentAfterMinimumDuration:`. The combined +Z-A iOS package separately uses `DM_PRESENT_HZ=120`, `DM_PRESENT_INTERVAL=game` and +`DM_ZA_FPS_MODE=dynamic60`. The explicit present-Hz request is clamped against +the active screen on the main thread before the guest starts and before NVN +caches the setting. The ordinary 60 Hz default is not raised implicitly. + +Despite its historical name, `dynamic60` measures positive whole-frame active +time, including durations below 16.667 ms. It forces graphics code 0 (native +present interval 1), while publishing measured clock durations rather than a +fixed 60 FPS step. A 120 Hz present denominator then sets an 8.333 ms scheduling +floor on a supported screen. The iOS 16 classic path passes that period directly +to the drawable and its host acquire pacer; `mtl_fps_target()`'s 60 result is not +a scheduling cap on this path. Rendering remains limited by real CPU/GPU work. + +The higher present denominator must not be paired with unchanged native game +timing or the fixed `static60` mode: those combinations do not establish correct +game speed. The dynamic mode retains its exact-build/table ownership gates, +positive ordered duration entries, original disabled-channel handling, explicit +16.667 ms seed and 50 ms long-stall limit. It remains a measured-clock experiment, +with secondary-provider and gameplay coverage described in +[the timing notes](ios16-za-dynamic-timing.md). + +The iOS 16 backend cannot use its `CAMetalDisplayLink` branch, which first exists +on iOS 17; its classic `link_sync` disables `DM_PRESENT_LINK`. Setting +`DM_PRESENT_LINK=2` therefore does not enable a 120 Hz path in this build. + +Every two foreground seconds, `display cadence` logs measure intervals between +actual `CADisplayLink.timestamp` values, the average target period and callback +period range. The same window independently counts real drawable presentations +through the existing `mtl_present_count()` counter. For example, 120 callbacks +per second with 30 drawable presentations is reported as 120 Hz and 30 FPS. +The existing visible FPS counter continues to show actual drawable FPS. Callback +cadence is not a claim about newly rendered game frames or panel scan frequency. + +The link pauses and its measurement window resets when the scene loses focus or +enters the background. It reconfigures and starts a fresh window on activation; +background time is never averaged into the foreground callback rate. A changed +window-screen maximum also resets the measurement. + +The portable policy/probe tests cover 60-only hardware, both requested rates, +invalid configuration, independent 120 Hz/30 FPS and 60 Hz/60 FPS windows, pause, +counter reset and invalid timestamps. Run +`python3 dm/ios/tests/test_display_refresh.py`. + +The existing timing regression suite now also covers steady 60/120-rate active +time, 120-rate jitter, and the actual table-consuming clock fixture at a 120-rate +cadence. Every measured frame must conserve accumulated active time within 2 ms, +including the original one-frame provider latency. The 120-rate fixture consumed +time at ratio 0.999761111 and a latency-adjusted ratio 1.000041429 over 30 seconds; +no limited time was recorded. Run +`make -C dm/host/tests GAME=0100F43008C44000 za-timing`. + +An earlier M2 field sample had 17.02–25.47 ms median frame windows, approximately +39–59 FPS, which do not meet a 120 FPS renderer's 8.33 ms budget. +Actual callback cadence, drawable presentation intervals and game-clock speed +require separate device evidence; a 120 Hz preference establishes none of them +by itself. diff --git a/docs/ios16-min-reduction.md b/docs/ios16-min-reduction.md new file mode 100644 index 0000000000..e262a9bc43 --- /dev/null +++ b/docs/ios16-min-reduction.md @@ -0,0 +1,125 @@ +# Restricted software MIN reduction on iOS 16 + +The audited NVN header defines AVERAGE as 0, MIN as 1 and MAX as 2. The captured +depth-pyramid sampler has reduction 1, min filter 3 (linear, nearest mip), mag +filter 1 (linear), wrap 7/7/7 (clamp to edge), LOD 0..1000, bias 0, anisotropy 1 +and comparison disabled. An averaged pyramid cannot substitute for that MIN +reduction. A device readback demonstrated a mip-7 parent greater than a child in +mip 6, including a zero child: this violates the minimum reduction needed here. + +`export_min_reduction.py` exports two companions from exact audited originals in +chain `43806fc88b5e`. It changes only each source's unique ordinary slot-17 +explicit-LOD sample expression and inserts a self-contained helper after the +Metal namespace declaration. Entry `main0`, resource bindings and all other +source bytes are preserved. The original sources, reflection and compiled +libraries are never replaced. Output contains game-derived MSL and must remain +with the user's game inputs, outside source control. + +The FS (`151607c7a92386cc_b`) obtains its explicit LOD from an integer. The CS +(`ec5ebd6d9902a9a1_b`) uses `max(0, ceil(log2(extent + epsilon)) - 1)`, also an +integer. Runtime must check the exact sampler contract, a 2D depth view and the +source/library hashes before selecting either companion. A different sampler or +shader keeps the original function; source hashes are not permission to apply +this algorithm to unrelated bindings. + +At the selected view-relative mip, the helper computes `p = uv * size - 0.5`, +then takes the floor and fraction. Signed neighboring texel coordinates are +clamped before conversion to unsigned integers. At most four integer reads are +reduced component by component. A neighboring row/column whose interpolation +weight is zero is excluded: unconditional four-tap MIN incorrectly expands the +footprint at exact texel centers. Normalized UV is clamped before conversion to +handle outside or infinite coordinates safely; existing NaN-to-zero behavior is +retained. Dimensions and mip count come from the texture view rather than fixed +1920x1080 constants, including odd and one-texel mip dimensions. + +The standalone LOD prototype implements GL's nearest-mip half-tie convention, +`ceil(lod + 0.5) - 1`, after sampler clamps and before view-range clamps. The two +audited shaders have integer LODs, so fractional NVN tie behavior does not enter +this path. Fractional LOD and hardware interpolation-weight quantization need +their own NVN validation before broadening it. Nearest spatial filtering, +linear-between-mip filtering, anisotropy, implicit derivatives, repeat/border +addressing and comparison sampling are outside this companion contract. For +comparison sampling, each depth comparison must precede reduction; reducing raw +depth and then comparing is not generally equivalent. + +Each `<key>.json` record binds schema, chain, kind, key, stage, entry, slot, +sample count, integer-explicit-LOD and 2D metadata to these hashes: + +- `original_source_sha256`: pinned audited source bytes. +- `library_sha256`: the original bundled compiled library bytes. +- `source_sha256`: the complete self-contained rewritten companion bytes. +- `helper_sha256`: UTF-8 bytes strictly between the helper begin/end marker + lines, excluding both markers. `helper_file` is `embedded`. + +The original `.metal` need not be bundled when runtime checks its manifest SHA +against the audited constant. Runtime still checks actual original library and +rewritten source bytes, the metadata and actual bound sampler. A companion's +selected function must participate in both render and compute pipeline cache +identity; a pipeline made for the original function cannot be reused for it. + +Export to a new directory, then package its four files in +`<app shader chain directory>/min-reduction-v1/`: + +```sh +python3 dm/ios/tools/export_min_reduction.py \ + '<shader root>/cache/b-43806fc88b5e' \ + '<shader root>/ios/b-43806fc88b5e' \ + '<shader root>/min-reduction-v1/b-43806fc88b5e' +``` + +The exporter validates both originals before writing; it rejects changed source, +reflection, chain, an overlapping input/output directory, repository output or a +differing existing companion. An identical export may be rerun. + +Synthetic checks, containing no game assets: + +```sh +python3 -m unittest dm/tools/tests/test_ios_min_reduction_export.py -v +clang -O1 -Wall -Wextra -Werror -fsanitize=undefined,float-cast-overflow \ + dm/gfx/tests/t_reduction_footprint.c -o /tmp/t_reduction_footprint +/tmp/t_reduction_footprint +xcrun --sdk iphoneos metal -std=ios-metal2.4 -c \ + dm/gfx/tests/reduction_sample_fixture.metal -o /tmp/reduction_sample_fixture.air +``` + +CPU checks cover exact centers with smaller unused neighbors, one-axis zero +weights, MIN/MAX, four corners and the middle, outside/Inf/NaN coordinates, one +texel, odd dimensions, all 11 observed integer mip levels and clamp boundaries. +The synthetic Metal kernel supports GPU readback with the same queries. CPU and +compiler checks alone do not establish device GPU results; the complete +companion-path device verification is recorded below. + +Primary references: NVIDIA's [EXT_texture_filter_minmax specification](https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_filter_minmax.txt) +defines nonzero-weight participation and comparison order; Khronos's +[GLSL texture-query algorithm](https://registry.khronos.org/OpenGL/specs/gl/GLSLangSpec.4.60.html) +defines the nearest-mip half-tie convention. These establish the portable model; +the audited local NVN enum and captured binding establish which model is needed. + +## Device verification (2026-10-25) + +On iPadOS 16.1 / M2, both companions compiled on the device and both actual +variant pipelines were created successfully. At the same stationary saved +alley camera, the large black silhouette became the textured bridge, wall and +plants. Pixel readback confirmed that the formerly black point gained albedo, +normal and material data in the main G-buffer; its final RGB changed from +approximately (0.004, 0.008, 0.016) to (0.443, 0.439, 0.424). Across twelve +verifiable 2x2 parent/child comparisons in the depth pyramid, violations fell +from four to zero. Indirect arguments remained identical to the captured raw +GPU results, with no forced visibility or conditional-render override. + +This comparison changes both the reduction algorithm and the device compiler +path. The old precompiled fragment library also produced a depth-attachment +pipeline error, despite its iOS 16.1 target and a valid depth/stencil format. +A separate unchanged-source runtime compile has not isolated the contribution +of that binary compatibility issue. The demonstrated result applies to the +complete companion path, not to MIN emulation alone. Warm switching off does +not necessarily restore the defect in a stationary view: valid old pyramid +contents can remain when an ordinary mip pipeline fails. A cold launch is +needed for a meaningful ordinary-path baseline. + +A subsequent short run with both Metal API and GPU validation enabled reached +1,500 total frames, including about 300 frames of the restored field scene, +without reported GPU memory errors. This establishes neither exhaustive scene +coverage nor long-term stability; later scenes and performance still require +testing. Validation was then disabled and the companion opt-in persisted in +the device's ordinary startup environment. diff --git a/docs/ios16-storage-access.md b/docs/ios16-storage-access.md new file mode 100644 index 0000000000..1b52ce2d4d --- /dev/null +++ b/docs/ios16-storage-access.md @@ -0,0 +1,113 @@ +# Compiled storage-pointer access on the legacy Metal backend + +The runtime supplements storage-buffer write declarations from the actual +compiled function's argument-buffer reflection. It runs once when loading a +shader, after default function-constant specialization and the existing raw +argument-layout check. It only changes an in-memory `nvn_rbind.rw` from false to +true. Game shader source, library/reflection files, manifests, shader-chain +identity, parameter IDs, pipeline identity, barriers, and fences are untouched. + +Resource declarations also retain their complete access union for each replay +render/compute encoder. A pool buffer used as both a writable SBUF and a +read-only UBO keeps `Read|Write` regardless of declaration order. Turning off +`DM_NVN_USE_DEDUP` only changes redundant-call elimination; every emitted usage +still contains the complete union. An explicit creation epoch resets the set +for each new encoder, including when Objective-C reuses its address. The +512-slot/16-probe fast table has a retained overflow vector rather than evicting +or forgetting previous Write flags. Allocation failure stops before issuing an +incomplete GPU declaration. No unrelated texture gains Write, and existing +fence/barrier calls are unchanged. + +## Why supplement the existing metadata + +The bundled translator exports `BufferUsageFlags.Write = 1 << 0`, while the +current offline `reflect()` uses `flags & 2`. Actual write flag 1 therefore +becomes explicit `rw:false`; the runtime parser trusts that explicit value. +For the audited `ec5ebd6d9902a9a1_b` CS, original MSL writes/atomics prove these +four writable bindings: + +| Member | Argument ID | NVN storage slot | +| --- | ---: | ---: | +| `cp_s1` | 5 | 1 | +| `cp_s2` | 6 | 4 | +| `cp_s6` | 10 | 3 | +| `cp_s7` | 11 | 5 | + +The runtime enhancement contains no game key or fixed binding list. This is a +conservative resource-use correction. It does not establish that wrong metadata +caused a particular visual fault: this backend already explicitly orders normal +compute/render/blit encoders with a global fence, and compute storage-buffer +pending-write range tracking does not depend on the erroneous `rw` bit. + +## Public API and validation + +`MTLFunction.newArgumentEncoderWithBufferIndex:reflection:` returns the same +encoder plus `MTLArgument` reflection. This public overload and nested pointer +reflection are available from iOS 11. The overload is deprecated on iOS 16 but +remains available; warnings are suppressed only around these compatibility API +uses. If it fails to create an encoder, the existing nonreflection overload is +retried and the metadata remains unchanged. + +The outer argument must describe the requested buffer index and a struct. +Only resolved route-B `NVN_RB_SBUFP` storage pointers are eligible. Uniform, +driver, support, image, and nested argument-buffer pointers are excluded. +Members are matched by `MTLStructMember.argumentIndex`, never enumeration order +or `offset / 8`. The member must have pointer reflection and the exact byte +offset `aid * 8`, consistent with this backend's raw 8-byte argument-slot ABI; +its slot must fit the encoded storage. Duplicate or overlapping metadata IDs, +duplicate reflected IDs, unknown types/access, absent members, and unavailable +root reflection cannot promote a binding. + +`MTLPointerType.access` of ReadWrite or WriteOnly adds Write. Existing true flags +are never cleared, including when reflection is missing or says ReadOnly. +The outer argument's access is deliberately ignored: a constant/read-only +argument block can hold writable device pointers. Compiled declaration access +can be conservative, so all eight device pointers in the example CS may be +promoted even though only four writes were proven from source. + +## Diagnostics and verification + +`[storage-access]` records include shader key/stage, metadata kind/storage slot, +argument ID, reflected offset/access, before/after `rw`, promotion result, and +rejection reason. Access 0/1/2 means ReadOnly/ReadWrite/WriteOnly; -1 means no +usable reflected access. At most 128 ordinary records are emitted per process. +`DM_NVN_STORAGE_ACCESS_DIAG=<shader key prefix>` selects an additional independent +128-record budget; `1` selects all keys. Both counters saturate. The filter only +changes logging, not the promotion policy. + +Run: + +```sh +make -C dm/gfx/tests GAME=0100F43008C44000 storage-access +``` + +The pure policy test verifies shuffled sparse IDs, both write modes, original +true preservation, nonstorage exclusion, metadata overlap, pointer/offset/range +validation, duplicate and omitted members, and unavailable reflection. +The Objective-C adapter test uses synthetic reflection objects through the +actual adapter, proves outer-vs-pointee access handling, checks actual log fields +and the 128-record cap, and tests encoder creation fallback. Neither creates a +GPU device. + +The independent resource-use oracle checks both UBO/SBUF alias declaration +orders, separate read-only inputs, dedup-disabled unions, new-encoder epochs +and rollover, 2,048 colliding resource identities, capacity overflow, and initial +and growth allocation failures. Previous Write history survives every failure. + +`t_storage_access_metal.m` contains only synthetic MSL. With a Metal device it +checks real sparse-ID reflection, a separate contiguous route-B ABI entry, +promotion of actual output/atomic pointers, and separate/aliased GPU readbacks. +Without a Metal device it explicitly prints **SKIP**. On the current local +virtual machine it compiled but skipped execution: real MSL compilation, +reflection, and GPU readbacks have not been validated locally. + +For the device acceptance run, set the diagnostic prefix to the audited CS key. +Verify actual compiled reflection promotes at least IDs 5, 6, 10, and 11, with +valid offsets and access. Keep GPU validation enabled for the scene/camera run, +and verify the previously restored geometry and Hi-Z MIN constraints. An absent +promotion log is not evidence that a buffer is read-only. Do not broaden or +disable the existing fences based on these metadata changes. + +Public API references: [argument encoder](https://developer.apple.com/documentation/metal/mtlfunction/makeargumentencoder(bufferindex:reflection:)), +[struct member](https://developer.apple.com/documentation/metal/mtlstructmember), +[pointer access](https://developer.apple.com/documentation/metal/mtlpointertype/access). diff --git a/docs/ios16-touch-camera.md b/docs/ios16-touch-camera.md new file mode 100644 index 0000000000..e72337945d --- /dev/null +++ b/docs/ios16-touch-camera.md @@ -0,0 +1,181 @@ +# Touch camera after keyboard disconnection + +The input defect was in touch ownership, not trackpad gesture recognition. Z-A's +profile starts with the Simple layout (`DM_GAME_PAD_LAYOUT=0`); after removing a +keyboard, Simple returned on screen but only the Full/3D layout assigned empty +right-side touches to the camera. Simple instead delivered those drags to the +game's touchscreen. The trackpad recognizers already excluded direct fingers. + +Both visible control layouts now allow camera dragging in empty space in the +right half. The right stick remains invisible. Touch-down still checks locked +buttons, the layout selector, the face-button area, the D-pad and left stick +before claiming a camera touch. Its ownership then stays fixed until release or +cancellation. A camera finger crossing a face button does not press that skill; +a finger starting on ABXY remains a button finger. Left movement and right camera fingers keep their +separate existing stick slots and can operate simultaneously. + +Releasing or cancelling the camera finger clears its slot and recomputes neutral +right-stick axes without releasing other fingers. Focus loss immediately +publishes neutral touch, keyboard, mouse and physical-controller state, then +clears UIKit ownership on the main queue. A focus gate also ignores subsequent +global controller/mouse events while the scene is inactive or in the background; +becoming active clears stale state before new input is used. This matters when +the game remains visible beside an active Stage Manager VM. Input-device notifications read their +inventory and change overlay ownership on the main queue; an unavailable +`keyboardInput` no longer counts as a usable keyboard solely because a coalesced +keyboard object remains. Reconnecting physical controls hides the overlay and +releases old touch ownership. Existing raw mouse, hover and scroll camera input +is preserved. Tap layout still sends its blank-area fingers to the game. + +This changes the previously unused right-side blank area of Simple layout into +camera input. Touchscreen-focused games should use Tap layout for unrestricted +game touches. There is no new gesture recognizer and no visible right-stick UI. + +Compilation checks use the actual UIKit/GameController source at iOS 16.1: + +```sh +NX_COMPILE_GAME=0100F43008C44000 python3 scripts/check_ios17_compile.py DmGamepad.m +``` + +The existing owner-based touch handling supplies release/cancellation and +multi-touch behavior; no duplicate model test claims to validate UIKit delivery. +Device validation must cover keyboard detach/reconnect, holding left movement +while dragging the blank right half, a third ABXY/skill finger, dragging across +buttons without stealing them, releasing/cancelling one finger while keeping +another, focus loss and resumption, Tap passthrough, and preserved trackpad +motion with the overlay hidden. Cross-compilation does not establish those +on-device interaction results. + +## Persistent virtual mice on the new host + +On the physical M2 iPad, an independent GameController probe found usable mice +named `V-UC Automouse` and `V-Apple Internal Keyboard / Trackpad` after the user +detached physical inputs. A fresh game launch still received mouse connections, +so restarting the game did not restore the overlay. This is a real inventory +entry, not evidence of a connected physical keyboard cover. + +The input policy now lets a direct finger select touch controls while only +mice remain connected. Controllers and usable keyboards retain priority. Mouse +movement selects pointer input again once all direct fingers have released; +it cannot steal a held movement, camera or skill finger. Raw motion, hover and +scroll use that same policy. Inventory notifications reset ownership only when +the overlay's eligibility actually changes. Queued raw events carry a focus +epoch and cannot cross a focus reset into a resumed scene. + +`DM_TOUCH_DIAG=1` logs up to 256 actual UIKit ownership/axis transitions. It +records simultaneous left/right ownership and neutral release, and does not +replace a real two-finger acceptance test. The Mac-built v62 was installed on +the original M2 iPad without restarting SpringBoard or its running VM. Its +actual UIKit log captured simultaneous left/right owners with no face-button +bits, right-hand release with the left owner retained and right axes zero, full +release, and neutralization on focus loss. Longer scene, cancellation, keyboard +reconnection and GPU validation acceptance remain separate work. + +## Explicit run shortcut + +`DM_RUN_BUTTON=B` enables a labelled Run button above the left movement ring and +maps either keyboard Shift to native Npad B. This shortcut is independent of +the visible face letters and their skill-only positional conversion below. The option +is generic and off unless explicitly configured for a game. It sends the game's +ordinary button, preserving context-specific behavior (including B's skill +while targeting). It does not change game speed or synthesize B from stick tilt. + +Run touch ownership is fixed at touch-down like other locked buttons. Crossing +it with a camera or movement finger does not press it. Both Shift states are +tracked separately from face keys, so releasing one alias cannot release another held +alias; keyboard disconnection and focus reset clear them. Touch cancellation, +layout changes and focus reset clear the Run owner with all other controls. +Compilation and on-device acceptance of this addition must be recorded before +claiming it works on hardware. + +## Face-function restoration and accessible aiming (v65) + +The user requires A/B/X/Y to retain their original functions in menus and normal +play, with positional selection applied only when choosing a combat skill. +V64 incorrectly converted every touch/keyboard face publication globally. That +approach was rejected and removed in v65: the published face bits retain their +letters. The on-device menu/normal controls must not be treated as interchangeable +with skill positions. A skill-only solution is still under investigation; holding +ZL alone is not a sufficient proof of skill context (capture and menus exist). +The retired conversion fixture establishes only the helper arithmetic, not the +correct behavior requested by the user. + +The retired v65-v69 keyboard experiment used I/J/K/L as a positional diamond. +The current bindings are restored to J/K/U/I = A/B/X/Y. WASD moves, arrows and trackpad look, either Shift sends native Run B. +`DM_AIM_ZL=1` adds Aim beside Run. Tapping Aim latches ZL, tapping again releases +it. Either Command holds ZL until released; Z remains available. The view is +first responder and consumes Command combinations with game keys, including +Command-W. Cancelled Aim touches clear the latch. Focus, layout and input-device +resets clear held and latched input. Physical Command acceptance remains pending. + +On this install, get_app_info reported the old data container but the process +actually used another container, confirmed by a temporary boot NSLog. Inspect +the process's boot log rather than trusting metadata for incremental deployment. +The old committed save was preserved on the Mac, the initially empty new save +and work directories were renamed as backups, and the committed save was copied +into the actual process directory. Configuration/control paths must follow that +actual directory. Runtime resources and all three shader companion directories +remain in the existing app bundle. + +## Retired skill-input conversion (v69 validation build) + +The v69 validation build observed the actual Waza move layout's visibility, using +read-only OBS gates for main build `b1f12fd919eae86ab8a978317677e64b` only. Its +now-removed skill-direction switch enabled positional selection for new touch and +keyboard face presses while that layout was visible. A bottom selected the +original native B move slot, B right selected A, X left selected Y, and Y top +selected X. The game HUD and move assignments stayed in their original positions. +ZL alone was not the condition. This experiment was retired in v70. + +Each source binds a press at touch/key down until release, so a visibility or Aim +transition cannot change a held skill into a new confirm/cancel action. Focus +reset clears those bindings. Run and Shift remain explicit native B shortcuts; +physical controllers keep their existing native mapping. The layout destructor +clears the observed context. No game register, HID implementation, or normal +menu mapping is globally changed by these observations. + +The C acceptance fixture covers ordinary chords, four directions, entering and +leaving the selector with a held key, release, and independent touch/keyboard +sources. The full iOS Release build must pass; actual UIKit battle actions and +ordinary menu confirm/cancel still require device evidence. DM_CTL native-button +injection bypasses this frontend adaptation and cannot establish that evidence. + +## Native face-function restoration (v70) + +J/K/U/I again send native A/B/X/Y, exactly as before the positional keyboard +experiment. Touch A/B/X/Y also send their named native buttons in every context, +including battle. The frontend no longer calls the skill-input conversion helper; +changing a HUD layout must not change confirmation, cancellation or move bindings. +WASD, camera arrows, Run/Shift and Aim/Command retain their existing behavior. +Command-U is consumed alongside the other game combinations. + +The keyboard regression compiles the actual frontend mapping function against +GameController, exercises the original four keys, shoulder shortcuts and Enter's +explicit diagnostic override, and checks native publication without a selector +remap. Battle HUD layout work is independent of these input semantics. + +## Virtual battle geometry only (v71) + +The original game's skill HUD, move assignments and ROM resources stay unchanged. +J/K/U/I remain native A/B/X/Y in every context. The same is true of the named +touch buttons; no selector state is consulted when publishing native input bits. + +`DM_ZA_SKILL_LAYOUT=1` observes the exact-build Waza layout visibility and changes +only our virtual diamond's visible and hit-test geometry. Ordinary play keeps +A bottom, B right, X left and Y top. The visible skill selector uses A right, +B bottom, X top and Y left, matching the original game's move HUD. ZL alone +does not select this geometry. The old positional input-conversion option has +been removed from the current host. + +The existing main-run-loop timer observes geometry at 60 Hz. Drawing and hit +testing share `facePos:` and one production geometry helper. Every face-owned +finger freezes the diamond until release or cancellation, including a finger +which currently sits in a gap and presses no button. Pending visibility changes +use the latest selector state after release. Camera and movement ownership are +unchanged. The original keyboard regression and geometry/held-owner transition +tests pass; the Release iOS build passes. Actual battle interaction remains for +the user's verification. No battle-save fixture or autoplay is required. + +An experimental same-slot ROM layout candidate was prepared locally before the +user clarified the intended frontend-only geometry. It was retired without +installation; neither the local original archive nor device ROM was modified. diff --git a/docs/ios16-upload-diagnostics.md b/docs/ios16-upload-diagnostics.md new file mode 100644 index 0000000000..f5f36b00c8 --- /dev/null +++ b/docs/ios16-upload-diagnostics.md @@ -0,0 +1,29 @@ +# Packaged texture upload diagnostics + +`DM_NVN_UPLOAD_DIAG_FRAMES=4` captures the next four frames that reach the packaged upload path. It is disabled by default and may be enabled in a running `DM_CTL` session with `env DM_NVN_UPLOAD_DIAG_FRAMES=4`. The request is one-shot. Change its token to rearm, for example `4:second`; setting it to `0` stops it at the next upload frame. + +The diagnostic never skips an upload or changes a dirty stamp. It records both initial uploads and dirty reuploads, including serial, storage/pool range, dimensions, format, selected CPU/GPU path and source storage size. Each record reports upload encoding time separately from snapshot/hash overhead. Normal frame timings still include the real diagnostic overhead and must not be treated as a performance benchmark. + +Snapshots are bounded to 16 requested frames, 128 records, 32 serials, 32 MiB retained across serials and 8 MiB per texture. A replacement snapshot temporarily needs up to one additional texture allocation. Oversized or excess records report the applicable limit and leave uploads unchanged. Snapshots are freed when the session reaches its frame/record limit or its request changes. + +For a repeated serial with the same storage identity and size, the helper compares every source byte with the preceding snapshot, grouped by host pages. `exact=unchanged` is an exact `memcmp` result, not a hash match. The SHA256 digest attributes the captured bytes in a log without exposing their contents. Dirty generations are checked around the copy; a covered write reports `racy`, and an uncertain prior snapshot reports `prior-racy`. Such records cannot establish redundant uploads. `baseline` has no prior comparable snapshot. `changed` includes the number and first offset of differing pages, which can distinguish localized edits from broad source changes. + +This establishes whether successive captured source snapshots were equal. It does not prove what an earlier GPU upload contained, nor that the source stayed unchanged between captures. Use an unchanged result only alongside successful upload records and the timing/source identity to design a subsequent correctness-preserving optimization. + +## Publication ordering + +A packaged upload protects its source pages and takes an upload generation before deswizzling or encoding a copy. A CPU write can fault after those source bytes were read and before the upload returns. Its page generation then exceeds the upload stamp. Publishing `seen = nvn_dirty_now()` at the end incorrectly treats that new generation as already inspected: the next lookup can take `now == seen` and return the older texture without checking its dirty pages. + +Both initial and repeated uploads now publish `seen = upload_stamp`. A generation that advances during the upload must pass through the page check on the next lookup. A clean lookup may subsequently advance `seen`, because it actually inspected all writes through that generation. Lost-upload retries still run through the existing completion tracking and are unaffected. + +The graphics tests exercise the real dirty-page tracking, shared-page pool handling and write-preparation races. `t_upload_diag` separately checks disabled operation, exact equality/change classification, concurrent-write attribution and size/record/frame bounds. It does not claim an end-to-end Metal regression for the publication ordering; that requires the real `NvPkg` resolution and GPU upload path. + +## Exact CPU decode cache + +The v58 city trace captured two dirty 512x512 textures over four frames. All six comparable source snapshots were byte-identical. The BC7 sRGB texture (NVN format 0x4e, ten mips, 351,744 source bytes) spent about 4.0 to 4.1 ms per upload in CPU decoding/encoding. The BC5 texture used the GPU decoder and needed only 0.07 to 0.12 ms of CPU encoding. This supports testing a CPU decode cache; it does not support skipping every dirty upload. + +`DM_NVN_CPU_DECODE_CACHE=1` enables a bounded exact cache on CPU-BC reuploads. It is off by default pending device verification. The cache retains the compressed source snapshot and its decoded pixels, with a 32 MiB total limit, 32 LRU entries, an 8 MiB source limit and a 16 MiB decoded limit per entry. It compares every source byte, with no digest-based reuse. Storage identity, layout, dimensions, format, mip count and other descriptor fields must all match. A changed source is copied to owned storage and decoded from that same copy, so a guest write during the decode cannot make the baseline describe different input from the decoded pixels. The pre-comparison upload stamp remains the `seen` generation. + +Every dirty candidate still receives an ordinary upload into the ordinary target texture. A cache hit only replaces CPU decompression with a copy of cached decoded pixels into independent Metal staging storage. This preserves the behavior of image-capable textures and lost-upload retries; it never assumes the current GPU texture still holds unmodified CPU pixels. LRU eviction cannot alter an in-flight staging buffer. Failed CPU decodes are not admitted. Setting the flag to 0 releases the cached byte buffers on the next CPU-BC reupload. + +`[CPU-DECODE-CACHE]` reports hits, misses, evictions, retained bytes and comparison/source-copy costs. The upload diagnostic labels hits `cpu-bc-cached`; upload byte and reupload counters intentionally remain unchanged because the copies still execute. `t_cpu_decode_cache` uses the actual BC7 CPU decoder to check output parity, a write after snapshot capture, content/layout/pool changes, failed decodes, independent staging and byte/slot/size bounds. Device measurements must establish the actual frame-time improvement before enabling this by default. diff --git a/docs/ios16-za-dynamic-timing.md b/docs/ios16-za-dynamic-timing.md new file mode 100644 index 0000000000..9e8c049ad1 --- /dev/null +++ b/docs/ios16-za-dynamic-timing.md @@ -0,0 +1,150 @@ +# Z-A whole-frame dynamic timing experiment + +`DM_ZA_FPS_MODE=dynamic60` remains opt-in for the general host, while the Z-A +iOS production profile selects it explicitly. It requests a 60 Hz graphics +interval while advancing the game clocks by measured whole-frame active time. +It does not make a renderer that takes 22 ms render at 60 FPS. Hardware results +must separately establish frame rate, real game-clock speed, and scene stability. +Use `DM_PRESENT_INTERVAL=game`; do not confuse display frequency with game speed. + +## Why the earlier modes are insufficient + +For Z-A 2.0.2, the original clock-selection work interval at `main+0x1b296f0` +can be about 6 ms even when the complete frame takes 28 ms. `static60` and the +existing `adaptive60` therefore select a 16.667 ms game step and run slowly in +that scene. Neither mode is changed by this experiment. + +The independently implemented dynamic controller samples `guest_ns()` at +successive interval observations. This timebase excludes system background +suspension. The original controller still receives graphics code 0 in w20, while +w22 selects clock code 0 or 1 after the native max operation. Only that selected +entry of the shared duration table changes; the other entry is nominal. + +## Exact data and mapping gates + +The full original NSO build ID inspected offline is +`b1f12fd919eae86ab8a978317677e64bce443d1f000000000000000000000000`. +Runtime uses the loader's exact supported 16-byte build ID field +`b1f12fd919eae86ab8a978317677e64b`, both instruction-validated observation gates, +and exact original segment boundaries: text end `0x3163f70`, ro start +`0x3164000`, data start `0x3bc2000`. + +The naturally aligned table at `main+0x3393e20` must initially contain five +64-bit nanosecond values: + +| Code | Original duration | +| --- | ---: | +| 0 | 16,666,667 | +| 1 | 33,333,334 | +| 2 | 50,000,000 | +| 3 | 66,666,667 | +| 4 | 83,333,334 | + +Its full 40-byte range must be inside the original ro segment and a currently +readable, writable, nonexecutable Mach VM leaf mapping. No permission change is +attempted. Deterministic-clock mode is rejected because it does not provide +whole-frame active wall time. A failed prerequisite retains game timing. + +Original providers at `main+0x1b2a0e4`, `main+0x1b5407c`, and `main+0x29f4214` +read these table entries before passing nanoseconds to their existing clock +setters. The base clock's actual delta is observed at `main+0x1b292dc` before +the original load and conversion to engine seconds. Shared-table ratio/quality +callers also exist, so table ordering and positive values are retained. + +Only aligned entries 0 and 1 are atomically updated. Slots 2 through 4 are checked +but never written. Pair publication also preserves ordering between its two +stores. Every publication verifies the previous owned values; an unexpected +writer returns the mode to game. Returning to game/static60/adaptive60 restores +only entries that still equal this controller's owned values. A foreign value +is reported and left intact. Guest executable instructions, SDK code, code +caves, scratch BSS, and clock-provider pause flags are never changed. + +## Timing, jitter, and pause policy + +The accepted active frame range is `(0, 50]` ms. A quarter-weight integer EMA +smooths timing, but signed phase debt bounds the accumulated difference between +measured and issued time to 2 ms. Unlike a free-running EMA or integer-FPS +quantization, a persistent change in frame time cannot accumulate clock drift. +The emitted step can differ from one raw frame by at most 4 ms within this range. +Clock tier 1 is preferred below 46 FPS, subject to table-order bounds. There is no +integer FPS rounding. The first callback has no preceding full frame and issues +one explicitly counted nominal 16.667 ms seed. + +An active frame above 50 ms is limited to preserve ordering before the unchanged +native 50 ms tier. Its discarded active time is explicitly logged and included +in `limited`; it is not repaid as later acceleration. This policy intentionally +does not claim correct speed below 20 FPS or across real multi-second stalls. +Clock discontinuities get a separately counted seed. All such events are +visible, and a nonzero `limited` requires examination before a speed claim. + +A system suspension creates a large wall/active-time difference and resets only +the EMA. It preserves phase debt and uses the actual positive active delta on +resume, with no background catch-up. A zero active delta publishes nothing and +leaves native handling in place. Original disabled channels continue to receive +zero through their original setter. The native adaptive long-work guard above +one second executes after our observation point; reaching it explicitly returns +to game mode instead of silently changing its code-4 semantics. + +Reissuing `dynamic60` changes the diagnostic epoch without reseeding the frame +controller or changing its table. The observer's two-sample warmup does not +affect engine time. A real mode change can have one preceding-mode frame in +flight; original values are restored immediately and no zero table duration is +introduced. Switching back into dynamic60 starts a new explicitly counted seed. + +## Validation and device acceptance + +Run `make -C dm/host/tests GAME=0100F43008C44000 za-timing`. +Pure tests cover steady 37 FPS, positive/negative periodic jitter, asymmetric +jitter, boundary values, long stalls, a five-minute suspension, and resume with +or without a callback during the frozen period. Conservation checks hold after +every measured frame. + +The integration test uses actual Mach mappings and a model of the original +table lookup plus disabled-channel setter. It counts the deltas actually +consumed by that clock, independently of diagnostic counters. It resets the +diagnostic epoch every 23 frames over 3,600 frames. The time genuinely consumed +by the modeled provider remains within one pending frame plus 2 ms of active +time; after closing that explicit one-frame latency, error is within 2 ms +(latency-adjusted ratio 0.999979613 over 98.1 s). Additional cases +reject wrong build/layout/table, unaligned data, deterministic clocks, read-only +and executable mappings, exercise ownership conflicts and the native guard, +and confirm restoration when switching to existing modes. The preexisting +opt-in/register-preservation/live/concurrent-mode tests remain unchanged. + +For a device A/B, opt in at startup with `DM_ZA_TIMING=1`. Live changes can then +use `DM_ZA_FPS_MODE=dynamic60` and `game` through the existing control path. +Record at least three 300-frame windows of actual `ratio_active`, selected +codes, issued and measured totals, seeds, and limited time. The issued total is +not a substitute for the actual observed engine clock. Long-window phase error +includes a normal one-frame provider latency, so a few initial samples are not +a speed result. Confirm menus, cutscenes, disabled channels, suspension/resume, +movement, camera, and combat before claiming compatibility or stable 60 FPS. + +## Provenance + +The factual table, channel semantics, and original instruction addresses were +verified from this exact locally supplied original executable. The reference +author's RC3 observes full-frame system ticks and changes the same two duration +slots, but its code cave and SDK machine code were not copied or distributed. +The downloaded pack had no explicit reusable source license. This host policy, +integer phase-bound algorithm, validation, and tests are independently written. + +Reference author: [Fl4sh9174's original repository](https://github.com/Fl4sh9174/Switch-Emulator-Ultrawide-FPS-Mods). + +## Initial device measurement (2026-10-25) + +On M2 / iPadOS16.1 in the restored alley scene, 900 observed base-clock frames +advanced 19.758014 seconds over 19.765974 active seconds (ratio0.999597). The +controller reported no limited active time during this sample. This establishes +the observed base clock, not every secondary provider or animation clock. Actual +frame time varied: 300-frame windows had medians from17.02 to25.47ms, so this +was not stable 60 FPS and that diagnostic session was returned to `game` mode. +The production iOS profile later selected `dynamic60` to avoid a fixed 30 FPS +limit while retaining the measured near-unity game-clock ratio. This remains a +request for up to 60 FPS, not a claim that every scene renders at 60 FPS. + +The inspected native interval loop supplies one selected code to every enabled +channel of its two-channel provider; disabled channels get zero. The executable +integration fixture currently models one consuming channel. Independent callers +of the other two provider families have not all been proven to select that same +code, so full secondary-clock/gameplay coverage remains required. diff --git a/docs/za-astc-pack.md b/docs/za-astc-pack.md new file mode 100644 index 0000000000..fd035458af --- /dev/null +++ b/docs/za-astc-pack.md @@ -0,0 +1,266 @@ +# Pokémon Z-A offline ASTC pack preparation + +`dm/tools/za_astc_pack.py` prepares audited BC7-to-ASTC 4x4 changes for +Pokémon Legends: Z-A's Trinity archive. It is an offline host tool. It does not +install anything, launch the game, or operate an Apple device. It supports both +same-slot replacement and relocated replacement packs. The latter is the normal +path when an ASTC file's Oodle stream is larger than its original FlatBuffer +vector. + +The source `arc/data.trpfs` and `arc/data.trpfd` must come from the user's own +RomFS. Keep them and all derived game data outside this repository. + +## Safety model + +- `census`, `prove`, and `prepare` open the source archive read-only. +- Only BNTX `BC7_UNORM` and `BC7_sRGB` textures are eligible. BC7 and ASTC + 4x4 both use 16 bytes per 4x4 block, so the BNTX image storage layout and + image size can be preserved while the BRTI format changes to ASTC 4x4. +- BC1 and BC4 are not converted because ASTC would double their 8-byte block + storage. BC5 needs a different two-channel destination contract, and BC6H + is HDR. They remain unchanged. +- Every mip and layer is deswizzled through the repository's NVN layout code, + decoded, encoded by Arm astcenc, reswizzled, and deswizzled again. The tool + rejects a layout mismatch. It also decodes the ASTC result and records PSNR + and RMSE quality evidence. +- The converted file is compressed by the Oodle implementation linked into + the user's own game module. A separate persistent guest-decoder process must + reproduce the converted bytes exactly before a patch is admitted. +- A same-slot patch is emitted only when the new Oodle stream fits the + existing FlatBuffer byte-vector capacity. The vector length is updated and + unused tail bytes are zero-filled. An oversized stream is reported and + skipped; no neighboring pack bytes move. +- `relocate` never moves an existing source pack physically. It copies each + affected ONEPACK and appends new verified file vectors inside that copy. The + compact payload is inserted immediately before the physical archive root, + and the original root tail is shifted to the new physical end of file. + Trinity also requires every adjacent root offset difference to equal the + preceding descriptor size rounded to 16 bytes. The schema-4 manifest + therefore rebuilds all pack offsets as one virtual aligned prefix sum while + preserving their hash-sorted order, and emits `redirects.tsv` to map the + complete virtual archive to the cloned source packs, inserted replacements, + and shifted physical root. This admits oversized Oodle streams without + physically rewriting every later pack in the immutable 4.75 GB source. +- A manifest binds the complete source archive SHA-256, every original range, + every replacement range, and the astcenc executable. Treat a bundle with no + patches, or a partial directory containing `FAILED`, as non-installable. + +`DM_ROMFS_OVERLAY` selects complete physical files. `DM_ROMFS_REDIRECTS` adds +the byte-range translation required by a schema-4 virtual archive. A compact +bundle is therefore materialized as a physical archive, descriptor, and bound +redirect table before the guest starts. `apply-relocated` does this on a host: +it clones the base archive with APFS `clonefile`, extends the clone, copies the +source root tail to its shifted physical end-of-file position, streams +`append.bin` into the old root position, applies the virtual-root header and +shifted root-index edits, patches a copied descriptor, copies `redirects.tsv`, +verifies all three outputs, then atomically publishes a directory with +`READY.json`. Preserve the original archive. On a filesystem without cloning, +`--allow-full-copy` explicitly permits a full multi-gigabyte copy. + +The tvOS frontend has a separate opt-in materializer for the same schema-4 +`virtual-pack-layout-before-root` bundle. Earlier schemas are rejected. The +materializer is deliberately disabled unless a bundle path is configured. It +accepts a production bundle path relative to the app's writable container, or +an absolute path for development, validates the source identities and every +manifest-bound range, clones into a staging directory, writes `READY.json` +last, and atomically publishes the archive, descriptor, and redirect table as +one overlay. Only then does startup set both `DM_ROMFS_OVERLAY` and +`DM_ROMFS_REDIRECTS`. The file layer validates that the redirect rows cover +the virtual file continuously from byte zero to virtual EOF, translates +partial and cross-row reads, and reports the virtual size through file and +directory size queries. Invalid metadata, an out-of-bounds mapping, a gap, or +a short physical read fails closed. Removing the configured bundle/overlay +falls back to the immutable base. This repository workflow still does not +deploy it. + +## Prerequisites + +Build the Z-A host so the tool can use the game's Oodle implementation: + +```sh +make -C dm/host GAME=0100F43008C44000 dmhost +``` + +Supply the matching rewritten `main` and `sdk` modules through `--modules`. +Build or obtain an official Arm astcenc 5.x executable and pass its path with +`--astcenc`. The tool can compile its small NVN texture-layout helper with the +system `clang`; `--library` can instead select a prebuilt helper. + +The examples below use `/private/tmp` intentionally. Replace the RomFS and +module placeholders with local paths, but keep outputs and caches outside the +source tree. + +## Read-only census + +The census decodes every selected Trinity file and records BNTX texture +geometry, formats, image bytes, source offsets, and errors in JSON: + +```sh +python3 dm/tools/za_astc_pack.py census \ + /path/to/romfs/arc/data.trpfs \ + /private/tmp/za-texture-census.json \ + --descriptor /path/to/romfs/arc/data.trpfd \ + --host dm/host/dmhost \ + --modules /path/to/rewritten/modules +``` + +Use `--pack-index`, `--file-index`, or `--limit-packs` for a bounded audit. +Unsupported compression records appear in `errors`; they are not silently +treated as textures. + +An exact runtime route can be selected from `DM_FSLOG` reads. Pair the trace +with a completed census so only known BC7 file records in intersected packs are +decoded: + +```sh +python3 dm/tools/za_astc_pack.py census \ + /path/to/romfs/arc/data.trpfs \ + /private/tmp/za-texture-census.json \ + --descriptor /path/to/romfs/arc/data.trpfd \ + --host dm/host/dmhost \ + --modules /path/to/rewritten/modules \ + --fslog /path/to/route-fs.tsv +``` + +`--fslog-before SECONDS` can bound the selection to an earlier point. A read +crossing two pack ranges selects both; malformed rows and traces with no pack +intersection are rejected. + +## One-file proof + +`prove` requires exactly one selected file and saves the original raw file, +converted raw file, and recompressed Oodle stream as evidence when the patch +fits: + +```sh +python3 dm/tools/za_astc_pack.py prove \ + /path/to/romfs/arc/data.trpfs \ + /private/tmp/za-astc-proof \ + --descriptor /path/to/romfs/arc/data.trpfd \ + --host dm/host/dmhost \ + --modules /path/to/rewritten/modules \ + --pack-index 12777 --file-index 0 \ + --astcenc /path/to/astcenc \ + --cache /private/tmp/za-astc-cache +``` + +The default Oodle compressor is Mermaid (`9`) at level 7. The compressor is +selectable for controlled experiments, but a different compressor is not a +shortcut around verification: the resulting stream must fit and pass the +same exact guest-decoder round trip. Keep the compressor choice and quality +thresholds recorded with test results. + +The default quality floor is deliberately strict: every converted +subresource must meet 40 dB PSNR and at most 2.55 8-bit RMSE. Small mips and +high-frequency textures may be retained even when they are structurally +valid. Lower `--minimum-psnr` or raise `--maximum-rmse` only as an explicit, +reviewed visual-quality decision; the manifest records per-subresource +measurements. + +## Prepare more than one patch + +`prepare` applies the same conversion, quality, capacity, and exact-decode +gates to all selected records: + +```sh +python3 dm/tools/za_astc_pack.py prepare \ + /path/to/romfs/arc/data.trpfs \ + /private/tmp/za-astc-bundle \ + --descriptor /path/to/romfs/arc/data.trpfd \ + --host dm/host/dmhost \ + --modules /path/to/rewritten/modules \ + --astcenc /path/to/astcenc \ + --cache /private/tmp/za-astc-cache +``` + +Review `manifest.json`. In particular, inspect `patches`, `converted`, +`quality_retained`, and `skipped_files`; a completed command does not imply +that every BC7 texture was converted. + +## Prepare virtual-layout relocated packs + +Use `relocate` for a deployable compact bundle. This example limits work to +BC7 files from a prior census whose packs were read by a captured route: + +```sh +python3 dm/tools/za_astc_pack.py relocate \ + /path/to/romfs/arc/data.trpfs \ + /private/tmp/za-astc-route-bundle \ + --descriptor /path/to/romfs/arc/data.trpfd \ + --host dm/host/dmhost \ + --modules /path/to/rewritten/modules \ + --census /private/tmp/za-texture-census.json \ + --fslog /path/to/route-fs.tsv \ + --astcenc /path/to/astcenc \ + --cache /private/tmp/za-astc-cache \ + --minimum-psnr 35 --maximum-rmse 4 \ + --oodle-compressor 13 --oodle-level 7 +``` + +The compact runtime inputs are `manifest.json`, `append.bin`, and +`redirects.tsv`. Despite its historical filename, `append.bin` is inserted at +`archive_root_offset`, before the physical root tail; it is not appended after +EOF. A schema-4 manifest binds the source root offset and hash, the inserted +range, the shifted physical root offset and hash, the virtual root and EOF, +the complete output hashes, and the redirect-table identity and dimensions. +The first redirect row maps the unchanged header/prefix, one row maps each pack +to either its immutable source range or replacement range, and the final row +maps the virtual root to the shifted physical root. Rows cover the virtual file +without gaps. + +`data.relocated.trpfd` is a review/convenience artifact for host verification; +the materializer can derive the same descriptor from the manifest's exact +edits. The manifest records the complete canonical input and output hashes, +each old and new eight-byte field, every appended pack hash, quality evidence, +and both tool recipes. A trace-selected bundle only covers reads observed by +that trace; it is not evidence about later, unvisited scenes. + +## Apply to a new archive + +Apply a reviewed bundle to a distinct output path: + +```sh +python3 dm/tools/za_astc_pack.py apply \ + /private/tmp/za-astc-bundle \ + /path/to/romfs/arc/data.trpfs \ + /path/to/separate-overlay/arc/data.trpfs +``` + +Before writing, `apply` verifies the source size, full SHA-256, and every +source range. It creates `<output>.partial`, writes and `fsync`s the exact +manifest ranges, reads them back, and renames the verified result. It refuses +to overwrite an existing output or partial file. The immutable source is +hashed again in the final report. + +This workflow creates a host-side candidate for later testing. Device +installation and runtime validation are separate, explicit operations. + +## Apply and verify a relocated bundle on the host + +Publish a reviewed relocated bundle to a new directory: + +```sh +python3 dm/tools/za_astc_pack.py apply-relocated \ + /private/tmp/za-astc-route-bundle \ + /path/to/romfs/arc/data.trpfs \ + /path/to/romfs/arc/data.trpfd \ + /private/tmp/za-astc-route-materialized +``` + +The output contains `data.trpfs`, `data.trpfd`, `redirects.tsv`, an apply +journal, and `READY.json`. Its physical archive layout is `source prefix + +inserted replacement-pack payload + shifted root tail`, while its header and +root index describe the virtual sequential layout. The command refuses +pre-existing output, validates the complete source identities before cloning, +streams the payload instead of holding it in memory, `fsync`s all published +inputs, validates the inserted range, shifted root, redirect table, and +complete final hashes, and removes an unpublished partial directory on +failure. + +Do not run a normal mmap-based `census` directly on the materialized physical +archive: its root offsets are virtual by design. Verification must read through +the same redirect layer, and runtime traces refer to virtual offsets. A valid +redirect table continuously covers `[0, virtual_archive_bytes)`, stays within +`final_archive_bytes`, and maps the root row to +`final_archive_root_offset`. The host file layer enforces these boundaries and +fails closed instead of falling back to the same physical offset. diff --git a/scripts/build_ipa_from_nsp.py b/scripts/build_ipa_from_nsp.py index 8ad50e9fd0..c0609d73ae 100755 --- a/scripts/build_ipa_from_nsp.py +++ b/scripts/build_ipa_from_nsp.py @@ -29,7 +29,7 @@ # unless ALLOW_OTHER_METAL / --allow-other-metal. Issue #242 reported corruption with another compiler; the cause is still unconfirmed. # Python 3.12+ is required for bounded archive mappings inside images. The Python running this file needs lz4, capstone and fonttools (and backports.zstd before Python 3.14); also needed: Xcode, xcodegen, .NET SDK 10, zstd. SPIRV-Cross is the in-repo build. A first run takes 2 (Metroid Dread) to 10 minutes (Breath of the Wild: 5 of them the shader split), longer when shader libraries must be compiled; a repeat run with unchanged inputs takes about 20 s (Metroid Dread), mostly packing the IPA. Key values are never printed. # The console shows one line per stage ([k/n] stage, with a bar, done/total, percent, elapsed time and ETA where the work can be counted); the tools' own output goes only to logs/ (PROGRESS / --progress). --progress json writes newline-delimited JSON events to stdout. -import ast, collections, ctypes, getpass, hashlib, importlib.util, json, os, re, shlex, shutil, struct, subprocess, sys, tempfile, threading, time, types +import ast, collections, ctypes, getpass, hashlib, importlib.util, json, os, platform, re, shlex, shutil, struct, subprocess, sys, tempfile, threading, time, types from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "dm/tools")) import romfs as rfs @@ -1049,7 +1049,10 @@ def glslc_done(d): # capture files written whole: a GLSLC output (magic 0x1986 def apple_silicon(): # hw.optional.arm64 is 1 on Apple silicon (for a Python under Rosetta too); an Intel Mac has no such key - return subprocess.run(['sysctl', '-n', 'hw.optional.arm64'], capture_output=True, text=True).stdout.strip() == '1' + # Sandboxed macOS processes may be denied this sysctl even on Apple Silicon. + # platform.machine() remains a reliable fallback when Python itself is native. + probe = subprocess.run(['sysctl', '-n', 'hw.optional.arm64'], capture_output=True, text=True) + return probe.stdout.strip() == '1' or platform.machine() == 'arm64' HOST_DIRS = ('dm/host', 'dm/gfx', 'dm/gfx/nvn', 'dm/gfx/nvn/include', 'dm/gfx/tex', 'dm/gfx/vk', 'dm/gfx/gl') # the folders the host's compile line takes its sources and headers from (dm/host/Makefile SRC, GFX, GFX_H) diff --git a/scripts/check_ios17_compile.py b/scripts/check_ios17_compile.py new file mode 100755 index 0000000000..67b18a4a96 --- /dev/null +++ b/scripts/check_ios17_compile.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +"""Cross-compile host/iOS translation units for arm64 iOS 16.1, without private game inputs. + +Supports explicit unit filtering, zero-match detection (fails with code 2), +and automated multi-profile cross-compilation across all 7 game profiles. +""" +from pathlib import Path +import subprocess, json, concurrent.futures, sys, os + +ROOT = Path(__file__).resolve().parents[1] +DM = ROOT / 'dm' +BUILD_DIR = ROOT / 'build/compile' +BUILD_DIR.mkdir(parents=True, exist_ok=True) + +sdk = subprocess.check_output(['xcrun', '--sdk', 'iphoneos', '--show-sdk-path'], text=True).strip() + +# All directory includes inside dm/ excluding git/build +incs = [ + str(p) for p in DM.rglob('*') + if p.is_dir() and '.git' not in p.parts and ('Gen' not in p.parts) and p.name not in ('gen', 'bin', 'obj') +] + +# Discover all 7 registered game profiles +profiles = sorted(p.parent.name for p in (DM / 'games').glob('*/game.tsv')) +if not profiles: + sys.stderr.write("ERROR: No game profiles found in dm/games/*\n") + sys.exit(2) + +# Full repository source inventory across all relevant subdirectories +ALL_SOURCES = sorted(list(dict.fromkeys( + list((DM / 'ios/Sources').glob('*.[mc]')) + + list((DM / 'gfx/nvn').glob('*.[mc]')) + + list((DM / 'gfx/tex').glob('*.[mc]')) + + list((DM / 'host').glob('*.[mc]')) +))) +ALL_SOURCES = [p for p in ALL_SOURCES if p.name != 'front_mac.m'] + +# Check Gen/src if present (from local xcode gen) +gen_src = DM / 'ios/Gen/src' +if gen_src.exists(): + ALL_SOURCES = sorted(list(dict.fromkeys(ALL_SOURCES + list(gen_src.glob('*.[mc]'))))) + +def resolve_target_sources(argv): + """Resolve sources from command line arguments with strict non-zero exit on zero matches.""" + if not argv: + # Default target set: the 3 modified core units + default_names = ['front_ios.m', 'nvn_mtl.m', 'nvn_pipeline_cache.m'] + resolved = [] + for name in default_names: + hits = [p for p in ALL_SOURCES if p.name == name] + if not hits: + sys.stderr.write(f"ERROR: Default target '{name}' not found in source tree!\n") + sys.exit(2) + resolved.extend(hits) + return resolved + + if '--all-sources' in argv: + return ALL_SOURCES + + targets = [a for a in argv if not a.startswith('--')] + if not targets: + return resolve_target_sources([]) + + matched = [] + for target in targets: + hits = [p for p in ALL_SOURCES if target in p.name or target in str(p.relative_to(DM))] + if not hits: + sys.stderr.write(f"ERROR: Requested compile target '{target}' matched 0 source files in repository!\n") + sys.exit(2) + matched.extend(hits) + + return list(dict.fromkeys(matched)) + +def compile_profile(selected_profile, sources): + out_dir = BUILD_DIR / 'profiles' / selected_profile + out_dir.mkdir(parents=True, exist_ok=True) + + header_path = out_dir / 'game_profile.h' + subprocess.run(['python3', str(DM / 'games/game.py'), 'header', selected_profile, str(header_path)], check=True) + + base = [ + 'xcrun', '--sdk', 'iphoneos', 'clang', + '-target', 'arm64-apple-ios16.1', + '-isysroot', sdk, + '-mcpu=apple-m2', + '-O1', + '-Werror=unguarded-availability', + '-Werror=unguarded-availability-new', + '-Wno-deprecated-declarations', + '-I' + str(out_dir), + '-I' + str(DM / 'games' / selected_profile), + '-I' + str(DM / 'third_party/libvpx/config') + ] + ['-I' + i for i in incs] + + def run_one(p): + name = str(p.relative_to(DM)).replace('/', '__') + obj_file = out_dir / (name + '.o') + log_file = out_dir / (name + '.log') + cmd = base + (['-fobjc-arc'] if p.suffix == '.m' else []) + (['-Dmain=dm_main'] if p == DM / 'host/main.c' else []) + ['-c', str(p), '-o', str(obj_file)] + r = subprocess.run(cmd, capture_output=True, text=True) + log_file.write_text(r.stdout + r.stderr) + return { + 'source': str(p.relative_to(DM)), + 'exit': r.returncode, + 'log': str(log_file) + } + + with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool: + results = list(pool.map(run_one, sources)) + + report = { + 'profile': selected_profile, + 'translation_units': len(results), + 'failures': sum(r['exit'] != 0 for r in results), + 'results': results + } + (out_dir / 'report.json').write_text(json.dumps(report, indent=2)) + return report + +def main(): + run_all_profiles = '--all-profiles' in sys.argv or 'NX_COMPILE_ALL_PROFILES' in os.environ + filtered_argv = [a for a in sys.argv[1:] if a != '--all-profiles'] + + sources = resolve_target_sources(filtered_argv) + if not sources: + sys.stderr.write("ERROR: Resolved source list is empty!\n") + sys.exit(2) + + target_profiles = profiles if run_all_profiles else [os.environ.get('NX_COMPILE_GAME', profiles[0])] + + all_reports = [] + total_failures = 0 + total_units = 0 + + print(f"Cross-compiling {len(sources)} unit(s) across {len(target_profiles)} profile(s):") + for s in sources: + print(f" - {s.relative_to(DM)}") + + for p in target_profiles: + rep = compile_profile(p, sources) + all_reports.append(rep) + total_units += rep['translation_units'] + total_failures += rep['failures'] + status_str = "PASS" if rep['failures'] == 0 else f"FAIL ({rep['failures']} errors)" + print(f"Profile {p}: {rep['translation_units']} units -> {status_str}") + + summary = { + 'scope': 'All actual iOS/host sources cross-compiled for arm64 iOS17, not linked or device-run', + 'profiles_evaluated': target_profiles, + 'total_units_compiled': total_units, + 'total_failures': total_failures, + 'reports': all_reports + } + (BUILD_DIR / 'summary.json').write_text(json.dumps(summary, indent=2)) + + print(f"SUMMARY: {total_units} total unit compilations; {total_failures} failures.") + if total_failures != 0: + for rep in all_reports: + for r in rep['results']: + if r['exit'] != 0: + print(f"FAILED {rep['profile']} {r['source']}:\n{Path(r['log']).read_text()[-1000:]}") + sys.exit(1) + + sys.exit(0) + +if __name__ == '__main__': + main()