Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dm/games/0400486024DEC000/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ From an empty save: at about 45 s `A` (title), `A` (New Game), then the movie. F

## Found on this game

- [Reads past a uniform binding's size](../../../docs/reference/shaders.md#reads-past-a-uniform-bindings-size): resolved, long white streaks from the horses at Lon Lon Ranch and from Link's slingshot, and parts of the slingshot and of the ReDeads' faces flickering, with main's bound on (`26062f9e`). Sizes taken as 4-byte words fixed only the streaks, and no bound fixed both, so Switch 2 games are not bounded ([GitHub issue #2218](https://github.com/seedds/nxnative/issues/2218), [PR #2219](https://github.com/seedds/nxnative/pull/2219)). The bound also made the white clouds over the ground mist more likely ([GitHub issue #1839](https://github.com/seedds/nxnative/issues/1839)).
- [The 60 Hz link at 40 fps in fullscreen on a ProMotion screen](../../../docs/reference/frame-rate.md#the-60-hz-link-at-40-fps-in-fullscreen-on-a-promotion-screen): 40 fps in fullscreen on an M5 Pro MacBook Pro with the GPU at 10 ms of the 16.7, every present one 120 Hz refresh late and every other display-link tick skipped; the screen's fixed 60 Hz mode held 60.0 fps there and fullscreen now switches to it, with the 120 Hz link (59 fps) for screens with no such mode.
- [The shader package and the shader map come from different splits](../../../docs/reference/packing.md#the-shader-package-and-the-shader-map-come-from-different-splits): resolved, a black screen with sound in a build through NX2Apple ([issue #1841](https://github.com/seedds/nxnative/issues/1841)); the present pass was translated from a terrain shader's record. Records now carry their keys, and the shader build finds them by key.
- [Native mesh pipelines compiled at their first draw](../../../docs/reference/shaders.md#native-mesh-pipelines-compiled-at-their-first-draw): resolved. The foliage, shadows and enemies drawn with task and mesh shaders stalled at their first draw on a cold Metal cache (59.5 s of builds at the Kokiri Forest entry on an M5 Max, one frame of 42.9 s), as nothing recorded or kept their pipelines; `pso_manifest.tsv` now carries 164 `M` rows (Kokiri Forest, Hyrule Field, Castle Town, Hyrule Castle, and 41 from later play, Death Mountain Trail among it), and with the prepare step they are built before the first frame. Pipelines outside the list still stall once, then go into `pso_user.tsv`. With the first 123 alone, a cold first start (empty Metal cache and pipeline archive, M4 Pro) on a Death Mountain Trail save built 9 and 10 at their first draw in two runs, 2.7 and 4.2 s, frames of 1,775 and 1,657 ms; with all 164, none, worst frame 25.6 and 18.5 ms, the prepare step 29.6 and 29.3 s → 32.1 and 31.0 s.
Expand Down
8 changes: 6 additions & 2 deletions dm/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -10535,6 +10535,10 @@ static void cbs_report(const cbs_list *l, const char *where, uint64_t nth) { /
// translator used to compare every read with the size (GitHub issue #2066: a fifth of the GPU's time in Donkey Kong Bananza); a read at a computed offset is still compared in the
// shader (NVN_RB_CBSIZE). The bytes are the ones the slot would have pointed at: the deferred updates' when they cover the binding, a partly updated block's copy, else the memory.
// Paper Mario: The Thousand-Year Door binds 16 bytes to a slot read up to byte 0x930, and the GPU hung on the light count it read at 0x900 (GitHub issue #1860)
// A Switch 2 game (dm_sdk_ounce) gets no such bound: no zero padding, and every bound slot 64 KB in the size table, as before 26062f9e. The Switch 2 does not seem to stop a read at the
// size the game binds: with the bound, Ocarina of Time read zeros past its bindings and the vertices of its horses, of Link's slingshot and of the ReDeads stretched to the origin or
// flickered; taking the sizes as 4-byte words fixed the stretching but not the flicker, and only no bound fixed both (GitHub issue #2218)
static int ub_unbounded(void) { static int v = -1; if (v < 0) { v = dm_sdk_ounce(); if (v) MLOG("uniform reads are not bounded by the binding's size (a Switch 2 game)"); } return v; }
static uint64_t g_ub_short;
static uint64_t ub_padded(NvShader *sh, int st, int reg, const p_bind_buf *u, uint32_t need, const uint8_t *mem, __strong id<MTLBuffer> *buf) {
size_t off = arena_alloc(need, buf); uint8_t *d = (uint8_t *)g_cur_arena->cpu + off; uint64_t lo = u->addr, hi = lo + u->size, g; const uint8_t *c;
Expand All @@ -10555,7 +10559,7 @@ static uint64_t cbuf_slot(NvShader *sh, int nvn_stage, const nvn_rbind *b, __str
void *mb; uint64_t mo, gpu;
if (!u || !u->addr || !mtl_gpu_addr(u->addr, u->size ? u->size : 1, &mb, &mo, &gpu)) { // reads 0 (the all-zero bank), as an unbound texture slot does, instead of through a null pointer
RED_ONCE(16, "stage %d cb%d has no uniform buffer bound", nvn_stage, reg); *use = zero_bank(); return zero_bank_gpu(); }
if (__builtin_expect(u->size < 4u * b->fixw, 0)) // bound shorter than the shader reads it at fixed offsets: those reads give 0, as on Maxwell
if (__builtin_expect(u->size < 4u * b->fixw, 0) && !ub_unbounded()) // bound shorter than the shader reads it at fixed offsets: those reads give 0, as on Maxwell
return ub_padded(sh, nvn_stage, reg, u, 4u * b->fixw, (const uint8_t *)[(__bridge id<MTLBuffer>)mb contents] + mo, use);
*use = (__bridge id<MTLBuffer>)mb;
uint64_t ul = cb_len(sh, reg, u->size); // what the shader can read of it
Expand All @@ -10570,7 +10574,7 @@ static uint64_t cbuf_slot(NvShader *sh, int nvn_stage, const nvn_rbind *b, __str
static uint64_t cbsize_table(int nvn_stage, __strong id<MTLBuffer> *use) {
size_t off = arena_alloc(16 * ((DMSH_CBSIZE_SLOTS + 3) / 4), use); uint32_t *d = (uint32_t *)(void *)(g_cur_arena->cpu + off);
memset(d, 0, 16 * ((DMSH_CBSIZE_SLOTS + 3) / 4));
for (int r = 0; r < DMSH_CBSIZE_SLOTS; r++) { const p_bind_buf *u = &S.ubo[nvn_stage][r]; if (u->addr) d[r] = u->size < 65536 ? (uint32_t)u->size : 65536; }
for (int r = 0; r < DMSH_CBSIZE_SLOTS; r++) { const p_bind_buf *u = &S.ubo[nvn_stage][r]; if (u->addr) d[r] = u->size < 65536 && !ub_unbounded() ? (uint32_t)u->size : 65536; }
return g_cur_arena->gpu + off;
}
// Whether a block of sh can be patched when only what the uniform bindings point at changed: every binding that reads through a uniform buffer is a plain CBUF slot (no handles or
Expand Down
2 changes: 1 addition & 1 deletion docs/agents/symptoms.md
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
- `image atomics: unsupported or overflowing IMG extent` at the first dispatch, despite a valid four-byte counter buffer. → [Buffer image reflection keeps its texture type](../reference/shaders.md#buffer-image-reflection-keeps-its-texture-type)
- `[RED] image atomics on a texture Metal cannot run them on`, and whatever the shader computes through them is lost: its writes go to a sink. → [Image atomics on a 32-bit colour texture](../reference/shaders.md#image-atomics-on-a-32-bit-colour-texture)
- Metal shader validation reports invalid image-atomic stores even with an image-sized scratch buffer; without validation, GPU page faults can freeze rendering. → [Bounds of image atomics](../reference/shaders.md#bounds-of-image-atomics)
- the GPU hangs (Metal command-buffer timeouts, `GPU access revoked`, exit 70; on the Mac WindowServer can lose its watchdog check-in and the session restarts) or a draw reads garbage, where a shader reads a uniform slot further than the game bound it. No `[RED]` line. → [Reads past a uniform binding's size](../reference/shaders.md#reads-past-a-uniform-bindings-size)
- the GPU hangs (Metal command-buffer timeouts, `GPU access revoked`, exit 70; on the Mac WindowServer can lose its watchdog check-in and the session restarts) or a draw reads garbage, where a shader reads a uniform slot further than the game bound it. No `[RED]` line. With the bound, a Switch 2 game's models drew long white streaks from some vertices to the origin, or parts of them dropped out for a frame (Ocarina of Time's horses, Link's slingshot, the ReDeads' faces: GitHub issue #2218). → [Reads past a uniform binding's size](../reference/shaders.md#reads-past-a-uniform-bindings-size)
- on an M1 Pro (macOS 26), blue, green and white blocks for one or two frames on trees that dither-fade (OCTOPATH TRAVELER's S'warkii, walking behind a tree); a texture dump shows NaN pixels in the scene colour and holes in the normal G-buffer target, which depth of field spreads into blocks. Not seen on an iPhone or on newer Macs. On every GPU, a branch for each uniform word a shader reads. → [Bounded uniform reads written as select](../reference/shaders.md#bounded-uniform-reads-written-as-select)
- the NSP script stops with `captured only 0 run-time shaders` although the game reaches its frame loop with precompiled shaders. → [A GLSL language preamble is not a complete shader](../reference/shaders.md#a-glsl-language-preamble-is-not-a-complete-shader)
- shaders missing from the map that no game file holds. → [Built-in shaders compiled with the game's own GLSLC](../reference/shaders.md#built-in-shaders-compiled-with-the-games-own-glslc)
Expand Down
6 changes: 4 additions & 2 deletions docs/reference/shaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -1817,15 +1817,17 @@ Finding, keying and translating shaders (`dm/gfx/tools/shader_split.py`, `dmsh_b

### Reads past a uniform binding's size

- **Symptom:** the GPU hangs (Metal command-buffer timeouts, `GPU access revoked`, exit 70; on the Mac WindowServer can lose its watchdog check-in and the session restarts) or a draw reads garbage, where a shader reads a uniform slot further than the game bound it. No `[RED]` line.
- **Symptom:** the GPU hangs (Metal command-buffer timeouts, `GPU access revoked`, exit 70; on the Mac WindowServer can lose its watchdog check-in and the session restarts) or a draw reads garbage, where a shader reads a uniform slot further than the game bound it. No `[RED]` line. With the bound, a Switch 2 game's models drew long white streaks from some vertices to the origin, or parts of them dropped out for a frame (Ocarina of Time's horses, Link's slingshot, the ReDeads' faces: [GitHub issue #2218](https://github.com/seedds/nxnative/issues/2218)).
- **Cause:** Maxwell bounds every constant buffer read by the size the game bound (`nvnCommandBufferBindUniformBuffer`): past it a read gives 0, and games rely on that. The translator declared each slot as `float4 _m0[4096]` behind a plain pointer, and Metal has no bounds of its own, so such a read reached the guest memory after the binding. Paper Mario: The Thousand-Year Door's `forward.bfsha` variant `f1e0f08cc43e21e3` reads its light count at byte 0x900 of uniform slot 11 and loops over that many lights, filling a 19-entry local array and reading c14 at `0x440 + 0x30 i`; the game binds 2,384 bytes to the slot for some materials and 16 bytes for others (501 of 1,024 bindings sampled in Peach's invisibility-potion scene). On the 16-byte bindings the count was whatever lay past them.
- **Fix:** in the chain and the host, for every game (`TranslationFlags.MetalUniformBounds`, Metal only), in two parts, since a slot's bound size is one value for a draw:
- **Reads at fixed offsets, in the host, once a binding.** The translator leaves such a read as it is and reports how far they go in each game uniform slot (c[3] to c[16]): the bytes from the slot's start to the end of the highest word read so (`ResourceManager.NoteUniformRead`, `BufferDescriptor.FixedSize`, `fixed` in the translator's report and in the reflection's `cbuf` binding, `nvn_rbind.fixw`). When the game binds a slot shorter than that, the argument-buffer encode copies the binding into an arena block with zeros up to there and points the slot at it (`ub_padded` in `nvn_mtl.m`; the bytes are the ones the slot would have pointed at, the deferred updates' or a partly updated block's copy included), and logs the first such shaders with a running count (`uniform slot N of shader K (stage S) is bound with B bytes and read up to byte E at fixed offsets`). Every other binding costs one comparison on the CPU. The code generator refuses a fixed read past the reported extent, so the two cannot disagree silently.
- **Reads at computed offsets, in the shader.** The word's index is compared with the slot's size in words; out of bounds the read goes to row 0 and gives 0, so it reaches no memory past the binding (`TryGenerateBoundedUniformLoad` in `CodeGen/Spirv/Instructions.cs`). The sizes come from a table in a bank of its own, `DMSH_CBSIZE_BANK` (25), a u32 for each of the 14 slots in four `uint4` (binding kind `cbsize`, `NVN_RB_CBSIZE`), which the host fills from the current bindings at each argument-buffer encode, 0 for an unbound slot (whose reads already go to [the zero bank](rendering.md#an-unbound-uniform-slot-reads-the-zero-bank)), at most 64 KiB. Only a shader with such a read declares the table.

The driver segments (c0, c2), the shader's own c1, NVN2's push constants (c[24]) and compute's uniform pointers (`ubo_ptr`, global memory on Maxwell too) are not bounded. The host takes a uniform word for its own proofs only from inside the binding (`ubo_float` refuses a word past the size).

**Switch 2 games are not bounded** (`ub_unbounded` in `nvn_mtl.m`: the sdk module's spec is Ounce, `dm_sdk_ounce`). Their short bindings are not padded, and every bound slot is 64 KB in the size table, as before the bound. The Switch 2 does not seem to stop a read at the size the game binds. With the bound, Ocarina of Time read zeros past its bindings, and the vertices of its horses, of Link's slingshot and of the ReDeads stretched to the origin or flickered. Taking the sizes as 4-byte words fixed the stretching but not the flicker, and only no bound fixed both (GitHub issue #2218). The shaders' compare at computed offsets stays, and always passes.
- **History:** the first form (chain `3510ea767778`) compared every read in the shader, fixed offsets too (`(576u < (sizes[2].w >> 2u)) ? c14[144].x : 0.0`). spirv-cross wrote each as a conditional expression, a branch for every read, until [they were written as selects](#bounded-uniform-reads-written-as-select) (chain `eaa843a79149`). The branch form cost little where shaders read few uniform words (NieR:Automata's opening flight, GPU p50 +1.6%) and a fifth of the GPU's time in Donkey Kong Bananza, whose material shaders read many: 14.6 → 17.5-18.1 ms a frame on the Freezer Layer on an M4, which took the game from 55 to 30 fps ([GitHub issue #2066](https://github.com/seedds/nxnative/issues/2066), the reporter's measurement on their own build). Of the 8.3 million uniform reads in its 23,638 shaders, 95.5% are at fixed offsets, and 73% of the shaders have no other kind.
- **Found on:** [Paper Mario: The Thousand-Year Door](../../dm/games/0100ECD018EBE000/README.md#rendering) ([GitHub issue #1860](https://github.com/seedds/nxnative/issues/1860)): the reporter found the variant and the short bindings, and padding those bindings with zeros let Peach's scene through on an M3 Ultra Mac and an M4 iPad Pro. [Donkey Kong Bananza](../../dm/games/040074A01BF12000/README.md#found-on-this-game) ([GitHub issue #2066](https://github.com/seedds/nxnative/issues/2066)): the cost of comparing every read.
- **Found on:** [Paper Mario: The Thousand-Year Door](../../dm/games/0100ECD018EBE000/README.md#rendering) ([GitHub issue #1860](https://github.com/seedds/nxnative/issues/1860)): the reporter found the variant and the short bindings, and padding those bindings with zeros let Peach's scene through on an M3 Ultra Mac and an M4 iPad Pro. [Donkey Kong Bananza](../../dm/games/040074A01BF12000/README.md#found-on-this-game) ([GitHub issue #2066](https://github.com/seedds/nxnative/issues/2066)): the cost of comparing every read. [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md#found-on-this-game) ([GitHub issue #2218](https://github.com/seedds/nxnative/issues/2218), [PR #2219](https://github.com/seedds/nxnative/pull/2219)): stretched and flickering vertices with the bound, so Switch 2 games are not bounded.
- **Check:** `dm/tools/tests/test_dmsh_abi.py` `test_uniform_bounds_agree` (the translator's bank and slots are the header's); `dm/gfx/tests/test_dmsh_ampere_mem.py` `test_fixed_uniform_reads_are_bare_and_counted` (with `DMSH_TEST_XLATE`: two fixed reads of c[3] give no size table and `fixed` 28). On NieR:Automata's 1,225 shaders built with this change: all 148,022 reads at fixed offsets are bare, all 28,604 at computed offsets are compared (and `select_guards` writes each of them as a select, none refused), 455 shaders declare the size table, and each slot's `fixed` ends in the highest row its source reads at a constant index. Not run in a game yet (open): the host's padding has met no short binding, the potion scene's retest by the reporter of #1860 is open, and so is Bananza's frame time on the Freezer Layer. The first form's numbers, `bench.py` on NieR:Automata's opening flight (M6, two runs each, 60 s window): GPU time p50 6.43 → 6.53 ms (+1.6%), p95 9.12 → 9.70 ms (+6.4%), frame time unchanged at 60 fps. Changing this path changes the chain version: rebuild the translator, update the lock and rebuild the shaders.

### Bounded uniform reads written as select
Expand Down
Loading