Skip to content

Image atomics on buffer images: declared as buffers, and an unbound one gets a buffer placeholder and an atomic scratch of its own (general, from SHIN MEGAMI TENSEI V) - #263

Closed
Xanawer wants to merge 1 commit into
seedds:mainfrom
Xanawer:imga-buffer-images
Closed

Xanawer wants to merge 1 commit into
seedds:mainfrom
Xanawer:imga-buffer-images

Conversation

@Xanawer

@Xanawer Xanawer commented Oct 26, 2026

Copy link
Copy Markdown

Summary

Image atomics on a buffer image stop the host since the image-atomic admission (#178): the host's reflection declared a texture_buffer image as 2D, and the admission takes a bound image only of the declared type. This declares it a buffer, and gives an unbound buffer image a placeholder of its own type and an atomic scratch of its own.

Found with SHIN MEGAMI TENSEI V (#173), which needs it: Unreal's compute shader 05fe2caae79b6e53 stopped the Mac host about 60 s into a new game.

 dm/gfx/
 ├── nvn/
 │   ├── nvn_refl.m               # + a texture_buffer IMG is declared a buffer (as a sampled one, TEXD, already was)
 │   └── nvn_mtl.m                # + dummy_img_tbuf: an unbound buffer image's placeholder, one texel on a buffer of its own
 │                                # + dummy_atom_buf: an unbound IMGA's scratch (was dummy_buf, which unbound buffers read as zeros)
 └── tests/
+    ├── t_imga_tbuf.m            # Metal, API validation: declared type, bound buffer admitted, unbound placeholder and scratch
     └── test_imga_binding_source.py  # + the unbound IMGA does not take dummy_buf

What the admission compares, for a shader with texture_buffer<uint> img and device atomic_uint* img_atomic:

 imga_extent(bound texture t, binding b)
-  b->dtt = 2D (dummy_kind has no buffer type)    → t.textureType (buffer) ≠ 2D → FATAL
+  b->dtt = TextureBuffer                          → admitted, extent from the buffer texture
 unbound:
-  2D placeholder; atomics into dummy_buf (the zero buffer unbound buffers read)
+  1-texel buffer texture; atomics into dummy_atom_buf

Docs: a docs/reference/shaders.md entry, the image-atomic contract's unbound-IMG paragraph and its verification commands, and the symptom index.

Evidence

On 6062518, on an Apple M5 Mac:

make -C dm/host/tests GAME=<Wonder> run; tsan memory-tsan                                     OK
make -C dm/gfx/tests check_t_thermal thermal-tsan; t_perf_order --cpu; perf-order-tsan        OK
dm/gfx/tex/tests/t_tex; the CPU-only gfx tests CI runs                                         OK
python3 -m unittest dm/tools/tests/test_*.py dm/ios/tests/test_*.py dm/gfx/tests/test_*.py   218 tests OK
make -C dm/host GAME=<Wonder | Hades | Digimon> dmhost; every gfx test built                 OK
python3 dm/ios/tools/compile_check.py; build_smoke.py                                        133 files, 0 failed; OK
make -C dm/gfx/tests GAME=<Wonder> run   (52 commands)                                         52/52
make -C dm/gfx/tests GAME=<Legends Z-A> imga-layout imga-layout-sanitize check_t_imga_tbuf     OK, 11 checks

Not checked:

Merge Danger

Door: two-way

One commit, no save, cache or shader-chain change; a revert takes it back out.

Blast Radius: small

  • Every game with image atomics on a buffer image: the binding is declared a buffer, so a bound buffer texture is admitted instead of stopping the host. Shaders with 2D or 2D-array images are declared as before.
  • Unbound image atomics: they write a scratch of their own instead of the shared zero buffer, so unbound buffers keep reading zeros.

🤖 Generated with Claude Code

…fer IMG a buffer, and an unbound one gets a buffer placeholder and an atomic scratch of its own

Main's image-atomic admission (PR seedds#178) takes a bound image only of the shader's declared type (imga_extent). nvn_refl.m declared each IMG from its placeholder table, which has no buffer type, so a texture_buffer IMG came out 2D and every real buffer image failed the admission: "FATAL [RED] image atomics: unsupported or overflowing IMG extent" at the first such dispatch. SHIN MEGAMI TENSEI V (PR seedds#173) stopped there about 60 s into a new game, at Unreal's compute shader 05fe2caae79b6e53 (an atomic on a 448,800-byte buffer image).

- nvn_refl.m: a texture_buffer IMG is declared a buffer (MTLTextureTypeTextureBuffer), as a sampled texture_buffer (TEXD) already was.
- nvn_mtl.m: an unbound buffer image gets a one-texel buffer texture of its own (dummy_img_tbuf; dummy_img's cache is indexed by type and has no slot for it), its row aligned to the format's minimum linear alignment. An unbound IMGA gets a 256-byte atomic scratch (dummy_atom_buf) instead of dummy_buf, the zero buffer unbound buffers read, which the atomics through an unbound slot could write.
- t_imga_tbuf (Metal, API validation layer): the declared type, a bound buffer texture admitted with its extent, the unbound placeholder's type, and the scratch's capacity and that it is not dummy_buf; without the nvn_refl.m line 3 of its 11 checks fail. test_imga_binding_source.py: the unbound IMGA does not take dummy_buf.

Docs: a docs/reference/shaders.md entry, the contract's unbound-IMG paragraph and its verification commands, the symptom index.

Checked on 6062518:
- CI's steps: the host tests, tsan / memory-tsan, the thermal checks, t_perf_order and its tsan build, t_tex, the CPU-only gfx tests, the Python tests (218), the Mac host for Wonder, Hades and Digimon Story Cyber Sleuth, every gfx test compiled, the iOS compile check (133 files) and the unsigned iOS app;
- the gfx run list (52 commands) under Wonder's profile, imga-layout and imga-layout-sanitize, and the contract's commands (t_imga_tbuf among them) under Pokémon Legends: Z-A's profile;
- with SHIN MEGAMI TENSEI V's profile (PR seedds#173 on 6062518 plus this commit): a Mac boot from a new game to the first classroom at 60 fps (frame time p50 16.65 ms), 05fe2caae79b6e53 admitted and no abort;
- reviews: GPT (codex gpt-6-astra) and Opus.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Xanawer pushed a commit to Xanawer/nxnative-android that referenced this pull request Oct 26, 2026
…SDK 9.3.1): game profile plus general changes to the Unreal pak reader, NVN virtual pools and sparse textures, and on iOS 60 fps and a Screen setting

Plays on the iPhone 18 Pro Max (user, with a build from before main's image-atomic admission, PR seedds#178): the opening, the character naming and Jouin High School. With the 60 fps rows the phone's launcher offers Frame rate 30 / 60; its last logged session (60, Screen fit) ran its first 900 frames at frame time p50 16.7 ms. On the Mac the same route runs from a new game to the classroom at frame time p50 16.7 ms at 60 fps (33.3 at 30), at the game's own speed: the naming keyboard comes at the same wall time at either rate. On the Mac it needs the buffer-image fix of PR seedds#263: without it the image-atomic admission stops the host about 60 s into a new game.

- uepak.py / shader_split.py: legacy pak version 8 (UE 4.22 to 4.24: version 11's footer, five compression method names, Zlib here, read by Pak.read), deleted entries (flag 2) skipped, and the AES key from a key callback whose halves lie apart in main (callback_keys; kept in memory only, never printed). ue_legacy_jobs takes each pak's main for its key. 18,558 shaders split.
- hle_uemalloc.c: UE 4.23's FMallocBinned3 (74 pools of 512 MB, 37 GB of the engine's region) is replaced by the host allocator. Layout UE4_23 has 18 vtable slots and no TryMalloc / TryRealloc yet. The constructor call (main+0x1713920) is cut and the object passed at the OBS_UE_MALLOC gate (the profile's patches.py). patches.py also makes nninitStartup allocate its address region when -SwitchVirtualAddressSpaceSize= is given (it skipped the allocation and left the base 0).
- sdk_patches.py, SDK 9.3.1 sdk ad3fdfbd62a5: the node area cut and 32 MB alignment for regions of 1 GB and more, as DRAGON QUEST XI S's SDK 7.3.2 build. game_args -SwitchVirtualAddressSpaceSize=4064 in a 5 GB block.
- hle_nvn.c: sparse virtual pools. A virtual pool larger than the application pool, or every one with the new profile key virtual_pools sparse, gets an address range of its own at Initialize, and each MapVirtual request's pages are remapped into it (page table per pool, vp in pool_t). Unreal's NVN RHI gives each streaming texture a pool of its own, over a thousand by the opening, mapped 64 KB page by page from some twenty physical pools. nvnTextureGetSparseTileLayout and nvnTextureBuilderGetSparseTileLayout now fill the tiled level count (nvn.h NVNtextureSparseTileLayout fields). The unimplemented stub left a count past the texture's levels, and Unreal's out-of-memory abort followed at boot.
- nvn_tex.c: a sparse texture with 8- or 16-byte blocks and no packaged layout takes a block height of 8 GOBs, the standard 64 KB tile shape. The classroom's DXT5 lightmaps were scrambled in 4-texel columns with 16. New t_tex case.
- nvn_mtl.m / hle_nvn.c: a sampled sparse texture (tex_t sparse) is read only through the pool it was initialized in, which the host finds from the texture alone (vpool_tex: the live pool whose base is the texture's mem - off, made before the texture's serial), so a texture that outlives its pool, or a new pool at the same address, never reads another pool's pages; a texture whose pool the game finalized keeps its last upload. The level it is bound from comes from the mappings its bytes were gathered with (sparse_base). New test t_sparse_life. texraw reads a packaged texture at its sampled level. The naming cutscene came out all green, then white, from the pipeline-miss lookahead's scissor leak that main fixed for OCTOPATH TRAVELER II (pso_predict_rest restores R); its comment names this game too.
- hle_fpslock.c: rows for t.MaxFPS and rhi.SyncInterval (UE 4.23, both copies). The game's code sets t.MaxFPS 30 at run time and the Switch device profile sets rhi.syncinterval=2; either alone kept it at 30. The profile's fps_target 60 adds the launcher's Frame rate 30 / 60.
- iOS: a launcher setting Screen (DM_ASPECT fit / zoom / stretch, default fit, settings.tsv). front_ios.m keeps the game view 16:9 (its drawable follows its bounds, and present letterboxes any other ratio): zoom makes it larger than the screen, stretch widens it with a transform. Touches are mapped on the same rectangle (dm_game_rect, DmGamepad.m).

Profile: tex_flush_upload and tex_dirty_upload on (the colour grading LUT is written from the CPU; without them the picture is black), pad_layout full, depth_mode zero (the game sets near = 0), shader groups global / engine / game. Open: two of Unreal's global compute shaders (4f731ba19a5f2877, c073d1efaba9ac70) use the value their atomics return, which main's bounded Metal atomics do not translate; their dispatches are skipped (7 red lines), and the classroom comes out a little darker (mean luma 57.3 against 58.8 with their old translations put back). Docs: the game note, the games table (virtual_pools, version 8 paks, depth_mode), README (games gallery with a Mac picture of the classroom, the Screen setting, the DM_FPS_TARGET list), AGENTS.md's count (sixty-eight), docs/reference memory / nvn-objects / rendering / rewriter / shaders and the symptom index.

Checked on 6062518:
- CI's steps: the host tests for Wonder's profile and this one, tsan / memory-tsan, the thermal checks, t_perf_order and its tsan build, t_tex, the CPU-only gfx tests, the Python tests (217: dm/tools, dm/ios and dm/gfx tests, the docs check among them), the Mac host for Wonder, Hades, Digimon Story Cyber Sleuth and this profile, every gfx test compiled, the iOS compile check (133 files) and the unsigned iOS app for both profiles;
- the gfx run list (52 commands) passes under Wonder's profile, and under this one except t_readback, which aborts the same way on main under The Great Ace Attorney Chronicles' profile (tex_dirty_upload on); t_sparse_life (131 checks) under both profiles and ThreadSanitizer;
- with PR seedds#263 on top, a Mac boot from a new game through the naming to the classroom at 60 fps (frame time p50 16.65 ms): the naming cutscene and the classroom correct, only the two shaders' 7 red lines;
- on cbbb600, --prepare-xcode built the iOS project (18,558 shaders split, the chain tables match); main's image-atomic change has since moved the chain version (8393d85a4eae on 6062518), and the iOS project was not built again;
- reviews: GPT (codex gpt-6-astra) on the scissor fix, the 60 fps rows, the Screen setting, the merge with cbbb600 and, in four rounds, the sparse texture lifetime; Opus on the allocator swap, the scissor fix and the lifetime fix.

Rebased onto 82ab25b (main's present allocator fix, which shares no code with this change): the host builds for this profile, and t_sparse_life, t_present_alloc and the docs check pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Xanawer

Xanawer commented Oct 27, 2026

Copy link
Copy Markdown
Author

Closing: main's f8642c4 (image atomics as native Metal texture atomics) covers this. With it, SHIN MEGAMI TENSEI V's two global compute shaders that use their atomics' return values (4f731ba19a5f2877, c073d1efaba9ac70) build and run on main without this PR: no red line from boot to the classroom, and the classroom's brightness matches their old translations. #173 no longer depends on this PR.

@Xanawer Xanawer closed this Oct 27, 2026
ltrdpper pushed a commit to ltrdpper/BananaNative that referenced this pull request Nov 16, 2026
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