Skip to content

NVN: opt-in physical sharing for compressible render-image aliases - #111

Closed
Jpkovas wants to merge 1 commit into
seedds:mainfrom
Jpkovas:experiment/nvn-render-image-sharing
Closed

Jpkovas wants to merge 1 commit into
seedds:mainfrom
Jpkovas:experiment/nvn-render-image-sharing

Conversation

@Jpkovas

@Jpkovas Jpkovas commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Draft follow-up to #100. Dynamic-resolution render images with the same NVN pool/offset can otherwise keep separate Metal allocations for many slightly different descriptors. This opt-in experiment shares a placement heap for eligible compressible images instead of accumulating an independent allocation per resolution.

DM_NVN_ALIAS_HEAPS=1 enables it; the default remains off and no game profile changes. Only private, single-level/single-sample 2D COMPRESSIBLE images participate, with identical pool, offset, Metal format and power-of-two size class. Different descriptors remain separate Metal texture objects. Ordinary, linear, display, sparse and unmapped images retain their existing path. A mapped contiguous virtual pool is eligible because its existing MapVirtual implementation refuses a different backing base; true sparse page-table pools are excluded.

The parent heap is registered once for Metal4 residency, including indirect argument-buffer access. Pool finalization removes its heaps through deferred release. Creating a new alias does not issue an out-of-order zero fill over another alias's allocation; initialization comes from its next GPU writer. PixelFormatView is requested before allocation so later format-view usage does not create a standalone replacement. This is physical storage sharing, not CPU texture compression or swap.

Synthetic validation on this independent branch:

  • 128 simultaneously live resolution descriptors have a 4 MiB device-allocation delta in the measured Mac test.
  • Literal uploads/readbacks are checked after alternation and revisiting old descriptors, including indirect Metal4 reads with only the heap resident.
  • Production depth/stencil clears, pool/offset separation, actual NVN Initialize/MapVirtual/Finalize, an unmapped pool and a true sparse-pool exclusion are exercised.
  • All 659 checks pass independently with normal encoding and all 659 pass again with deferred encoding, on the public base named below.
make -C dm/gfx/tests GAME=0100DCA0064A6000 t_image_heaps
MTL_DEBUG_LAYER=1 dm/gfx/tests/t_image_heaps
DM_NVN_ENC_THREAD=1 MTL_DEBUG_LAYER=1 dm/gfx/tests/t_image_heaps

Local integration evidence is deliberately narrower than a general memory claim:

  • LM3 1.4.0, a combined iPhone build: the player passed the previously failing transition in Docked with a 60 FPS target. The observed window sampled about 2,970 MiB footprint / 2,393 MiB GPU allocation and roughly 60 FPS. This is not a controlled same-route percentage comparison or full-session stability proof.
  • A later app-switch/resume crash was traced separately to an AudioOut lifetime problem; sharing does not solve that.
  • ACNH 3.0.3, short silent Mac routes in Docked/30 and Docked/60: no fatal/GPU validation error in those bounded runs, but no memory reduction was demonstrated. The 60 FPS steady footprint was similar to the earlier candidate (~1.83 GiB) and GPU allocations were slightly higher (~935 vs ~915 MiB).

The 4 MiB synthetic result is not a predicted game footprint. Arbitrary overlapping offsets, cross-format aliases and sparse remapping are outside this key. Other titles, overlapping alias lifetimes and longer device sessions need review/testing before enabling this generally. The default-off draft is intentional.

This is distinct from #103's guest-page renewal and packaged-source reclaim. No game asset, compiled/translated game shader, private index stream, save, screenshot, raw device log or identity is included.

Public source base: d979c346b9a188d63ae16ec6f73e099a3c7f5a05. Each patch is independent of the other proposals in this batch.

seedds added a commit that referenced this pull request Oct 24, 2026
…ral, found on Luigi's Mansion 3): no blending on integer targets, primitive restart in fan and quad expansion, partial depth-stencil clears, every name of an upgraded texture, the SDK's AudioOut fields. nvn_mtl.m pso_desc (#105): an integer colour target took the game's blend enable, and Metal refuses such a pipeline with an assertion even without the API validation layer (checked on this Mac); Maxwell ignores blending on integer targets, so it is off for them. draw (#110): indexed triangle fans and quads expanded on the CPU read the restart index as a vertex and joined the polygons across it, and the extra triangles cleared parts of Luigi's Mansion 3's cached shadow depth; nvn_primitive.h ends a polygon at the restart index (compared before the base vertex), at most as many triangles as before. OP_CLEAR_DS (#108): a depth-stencil clear whose scissor does not cover the target, or whose stencil mask is partial, cleared the whole target before the first draw, and a partial stencil mask did so in a pass too; both are drawn as a triangle now (clear_ds_tri), full clears stay load actions. Logs on the T7 show who takes the new path: Tears of the Kingdom clears 8x8 tiles of a 64x64 depth target (each clear wiped the whole target), AI: THE SOMNIUM FILES clears 1280x720 of a 1920x1080 one. tex_upgrade (#107): a texture recreated to add a usage replaced only the asking serial and one sharing-table entry, so other serials on the same storage read the old copy; every entry in g_by_serial, g_ghost and g_alias now moves to the new texture in place, g_alias_set follows, and the old one is retired after the submissions that used it. hle_audio.c (#109, reworked): the host left the guest AudioOut zero, and middleware that checks its first word before Stop / Close skipped the close, freed its PCM and the host's consumer read freed memory (SIGBUS on resume in Luigi's Mansion 3). The PR wrote 1 and cleared it on close; the SDK's OpenAudioOut (SDK 8, 12, 18 and 20 checked) writes sample rate, channel count, sample format and state at +0 / +4 / +8 / +0xc, its getters load from there, and CloseAudioOut only sets the state to stopped, so the host now writes the same (au_info). New tests t_integer_blend (63 checks), t_primitive_restart (37), t_clear_ds (43), t_upgrade_alias (10) in the gfx run list, and an AudioOut fields and lifetime case in t_audio (12 guarded open / close / unmap / resume cycles, silent sink and real output). Checked: the four new tests pass under Metal validation; every other gfx test gives the same result as on main (t_fuse_check, t_reorder_chain, t_defer_chain, t_mip_extent, t_rt_range_ro and t_rt_range fail the same cases on both; t_refl finds no reflection samples in this checkout's shader cache); the host unit tests pass; on the Mac host for 90 s from boot, against main built the same way: Breath of the Wild over the title, 60 fps and the same red lines; AI: THE SOMNIUM FILES (shaders built by the NSP script on this chain), whose 1280x720 depth clears at the start of a pass now take the triangle path too (from pass 119), 60 fps, no red line, 5 long frames against 12. Not taken from this batch: #106 (the stencil-only view of a depth-stencil texture works without format-view usage; only the validation layer asks for it, and the PR would put that usage on every depth-stencil buffer), #111 (an opt-in draft with no memory saving shown), #104 (Mac app bundle)

Co-authored-by: João Karalkovas <[email protected]>
@seedds

seedds commented Oct 24, 2026

Copy link
Copy Markdown
Owner

Thank you for this, and for the careful write-up and the test. I applied it on current main (54b91a9; only the test lists in .gitignore and Makefile conflicted) and t_image_heaps passes 659/659 under Metal validation, with and without DM_NVN_ENC_THREAD=1, with the same 4 MiB delta you report. I'm closing it rather than merging, though, because as it stands it does not save memory in general and in some cases uses more:

  1. Heaps live until their pool is finalized. g_image_heaps loses entries only in image_heaps_drop_pool. On main, alias_take_over already frees an old render target when a new description takes over its storage (unless a live texture object still resolves to it). With the heaps, every (pool, offset, format, size class) a game ever uses keeps its allocation until the pool goes, and most NVN games keep one large pool for their whole run, so this grows monotonically.
  2. Power-of-two capacity. Measured with heapTextureSizeAndAlignWithDescriptor on an M-series Mac: 1920x1080 RGBA8 8.1 MiB → 8 MiB heap (no extra), 1920x1080 Depth32Float_Stencil8 10.3 → 16 MiB (+55%), 1600x900 RGBA16F 11.4 → 16 MiB (+40%). A game with one description per offset, which is the common case, pays this and gets nothing back; that fits the slightly higher GPU allocations you saw in Animal Crossing.
  3. PixelFormatView on every eligible render target. The host avoids that usage on purpose (see the comments around tex_upgrade and the format-view upgrade in nvn_mtl.m), because it turns off Apple's lossless compression. The same measurement shows it: with PixelFormatView the textures come out slightly smaller, i.e. without their compression metadata. That is a bandwidth and power cost on the iPhone for every render target, not only the aliased ones.
  4. No zero fill for a fresh heap. The host zero-fills new textures because new Switch memory reads 0; skipping it is right for a texture that reuses another alias's storage, but the first texture in a new heap now starts undefined.
  5. No game benefit shown yet. The Luigi's Mansion 3 run used a combined build without a same-route comparison, Animal Crossing was similar or slightly worse, and the 4 MiB figure is the synthetic case of 128 sizes kept alive at once.

If you want to take this further, these would make it a candidate to turn on by default: free a heap when its last texture goes (or fold the sharing into the existing g_alias / alias_take_over lifetime), exact rather than power-of-two sizing, a zero fill when the heap is new, no forced PixelFormatView (or a measured cost for it), and a measured saving in a game that really keeps several descriptions alive at one offset. Notes for such a change would go in docs/development.md rather than a new top-level doc.

@seedds seedds closed this Oct 24, 2026
Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
…eedds#109 and seedds#110 (general, found on Luigi's Mansion 3): no blending on integer targets, primitive restart in fan and quad expansion, partial depth-stencil clears, every name of an upgraded texture, the SDK's AudioOut fields. nvn_mtl.m pso_desc (seedds#105): an integer colour target took the game's blend enable, and Metal refuses such a pipeline with an assertion even without the API validation layer (checked on this Mac); Maxwell ignores blending on integer targets, so it is off for them. draw (seedds#110): indexed triangle fans and quads expanded on the CPU read the restart index as a vertex and joined the polygons across it, and the extra triangles cleared parts of Luigi's Mansion 3's cached shadow depth; nvn_primitive.h ends a polygon at the restart index (compared before the base vertex), at most as many triangles as before. OP_CLEAR_DS (seedds#108): a depth-stencil clear whose scissor does not cover the target, or whose stencil mask is partial, cleared the whole target before the first draw, and a partial stencil mask did so in a pass too; both are drawn as a triangle now (clear_ds_tri), full clears stay load actions. Logs on the T7 show who takes the new path: Tears of the Kingdom clears 8x8 tiles of a 64x64 depth target (each clear wiped the whole target), AI: THE SOMNIUM FILES clears 1280x720 of a 1920x1080 one. tex_upgrade (seedds#107): a texture recreated to add a usage replaced only the asking serial and one sharing-table entry, so other serials on the same storage read the old copy; every entry in g_by_serial, g_ghost and g_alias now moves to the new texture in place, g_alias_set follows, and the old one is retired after the submissions that used it. hle_audio.c (seedds#109, reworked): the host left the guest AudioOut zero, and middleware that checks its first word before Stop / Close skipped the close, freed its PCM and the host's consumer read freed memory (SIGBUS on resume in Luigi's Mansion 3). The PR wrote 1 and cleared it on close; the SDK's OpenAudioOut (SDK 8, 12, 18 and 20 checked) writes sample rate, channel count, sample format and state at +0 / +4 / +8 / +0xc, its getters load from there, and CloseAudioOut only sets the state to stopped, so the host now writes the same (au_info). New tests t_integer_blend (63 checks), t_primitive_restart (37), t_clear_ds (43), t_upgrade_alias (10) in the gfx run list, and an AudioOut fields and lifetime case in t_audio (12 guarded open / close / unmap / resume cycles, silent sink and real output). Checked: the four new tests pass under Metal validation; every other gfx test gives the same result as on main (t_fuse_check, t_reorder_chain, t_defer_chain, t_mip_extent, t_rt_range_ro and t_rt_range fail the same cases on both; t_refl finds no reflection samples in this checkout's shader cache); the host unit tests pass; on the Mac host for 90 s from boot, against main built the same way: Breath of the Wild over the title, 60 fps and the same red lines; AI: THE SOMNIUM FILES (shaders built by the NSP script on this chain), whose 1280x720 depth clears at the start of a pass now take the triangle path too (from pass 119), 60 fps, no red line, 5 long frames against 12. Not taken from this batch: seedds#106 (the stencil-only view of a depth-stencil texture works without format-view usage; only the validation layer asks for it, and the PR would put that usage on every depth-stencil buffer), seedds#111 (an opt-in draft with no memory saving shown), seedds#104 (Mac app bundle)

Co-authored-by: João Karalkovas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants