Skip to content

Decode BC textures on the CPU when the Metal device cannot sample them - #488

Closed
v0lumehi wants to merge 1 commit into
seedds:mainfrom
v0lumehi:bc-cpu-decode
Closed

v0lumehi wants to merge 1 commit into
seedds:mainfrom
v0lumehi:bc-cpu-decode

Conversation

@v0lumehi

@v0lumehi v0lumehi commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

What and why

On an Apple GPU that cannot sample BC ([MTLDevice supportsBCTextureCompression] is NO before the A19: A15 / A16, older iPhones and iPads) newTextureWithDescriptor: returns nil for every texture made in a BC pixel format, so each BC texture of a game is missing. This restores a CPU decode for those devices only. Part of the effort to let forks drop their larger patches (#388).

This was in the tree and was removed on purpose. 14fbcf9c ("Only GPUs with BC textures are supported: no-BC decompression paths removed") took out nvn_bcdec.c, dm/third_party/bcdec, a GPU kernel for BC1-5 and DM_TEST_NO_BC, because decoded textures do not fit in memory on those phones (Breath of the Wild: 3.9 GB instead of 2.8 GB). I am not trying to overturn that. What differs here:

  • The decision is the device's capability, detected at run time (nvn_bc_cpu_wanted): a device that samples BC (every supported one) takes exactly the code it takes now. No profile key, no build switch. DM_TEST_NO_BC=1 (the old switch name) takes the path on a Mac, for testing.
  • It is a best effort for unsupported devices. README's device requirements are unchanged. A game that needs more memory than the decoded textures leave still runs out of it, and the reference entry says so. Decoded size against the blocks: 2x for BC4 / BC5, 4x for BC2 / BC3 / BC7, 8x for BC1 / BC6H.
  • The GPU decoder for BC1-5 is not restored. It was written because CPU deswizzle plus decode took over 300 ms in one frame on an A16, so loading is slower than it was there. If you want it back, say so and it can follow as a second PR.

The downstream fork (NX2Apple) shipped this path in v0.4.5 for A15 / A16 users, behind a per-game profile key (tex_bc_cpu, on for Tears of the Kingdom). With run-time detection that key is not needed: a device without BC support has no other way to show a BC texture, and a device with it never reaches the path.

The change

  • dm/gfx/tex/nvn_bcdec.{c,h}: the decoded Metal format of each BC format, nvn_bc_decode for the blocks of one level and layer (BC1-BC7, the sRGB forms, BC4 / BC5 signed and unsigned, BC6H to RGBA16F; partial edge blocks write only the texels inside the level), and nvn_bc_cpu_wanted. Pure C. Decoding is dm/third_party/bcdec (0.985, unmodified, MIT or Unlicense, with its licence and a README; one row in dm/third_party/README.md).
  • nvn_mtl.m: mtl_init asks the device. tex_desc makes a BC texture RGBA8 / RGBA8 sRGB / R8 / RG8 / RGBA16F, also for a format-changing view (nvn_fmt_to_mtl_dec, so the sRGB reinterpretation of BC1 is an RGBA8 / RGBA8_sRGB view). pkg_upload_ (every CPU-written, packaged and sparse-pool texture) deswizzles on the CPU, decodes per level, array layer, cube face and depth slice into a staging buffer sized per texel, and blits it; the GPU deswizzle writes blocks, so it is skipped for these. copy_b2t into a BC texture is mirrored into the storage as before, or decoded from the buffer's memory when the CPU holds it (copy_b2t_bc). RGB_DXT1 keeps reading alpha 1 through its view swizzle.
  • Not covered, with a [RED] line instead of Metal validation aborts: a copy out of a BC texture (texture→buffer, ReadTexels of a texture the CPU did not write), a buffer-to-texture copy whose source the GPU writes, and a BC texture as a render target (the shadow of bcrt_attach copies blocks, which these textures no longer hold: the attachment is left out, as an unshadowed BC target is on any device).
  • Docs: reference entry "BC textures on a GPU that cannot sample them" in docs/reference/rendering.md (symptom index regenerated), dm/gfx/tex/README.md, docs/env_switches.tsv.

Checked

  • Tests: t_bcdec (make -C dm/gfx/tex/tests run, also asan; CPU only, added to the CI step "CPU texture layout and deswizzle tests" and so to the sanitizer job): 99 checks. BC1 against a decoder written without bcdec (all palette modes, sRGB the same bytes), hand-made BC2 / BC3 / BC4 (unsigned and signed, both modes) / BC5 / BC6H / BC7 blocks, the format map of all 16 BC formats and the rejection of their neighbours, a 10x6 image with padded source and destination pitches and untouched padding, a mip chain 9x5 down to 1x1, and nvn_bc_cpu_wanted for a device with and without BC (the mock of "no BC"). t_bc_cpu (make -C dm/gfx/tests GAME=010015100B514000 check_t_bc_cpu, Metal 4, so local like t_cpu_tex --metal): 15 checks, with DM_TEST_NO_BC=1 and Metal's validation layer on an M2 Max: a CPU-written 2D-array BC1 texture of 18x10, 2 layers, 3 levels is RGBA8 and every level and layer reads back as the decoded texels; a GPU-only texture filled by buffer-to-texture copies reads back the same; with the path off the same kind of texture stays BC1 and holds the blocks as written. It aborts in Metal's validation when tex_desc does not substitute the format. Existing: t_tex 5,334 checks, t_cpu_tex, t_tex_layout, t_cover.
  • make -C dm/host GAME=010015100B514000 builds; GAME=010015100B514000 python3 dm/ios/tools/compile_check.py: 179 files, 0 failed; lint_python.py: 0 new; dm/tools/tests/test_docs.py, test_env_switches.py, test_ci_toolchain.py pass.
  • Games run: none, and regress.py was not run. No game was started and nothing was installed on a device. On a device with BC the only changed code that runs is the mtl_init question and bc_decoded() returning 0.
  • Devices and OS: unit-tested and compile-checked only (M2 Max, macOS 27). Not run on an A15 / A16. The fork shipped it in v0.4.5 for A15 / A16 users, who are the ones to confirm it (and its memory use and load time) on a real game.
  • Measurements: none of the decode speed or memory on a device.

Docs

  • The finding is in its docs/reference/rendering.md entry, and python3 dm/tools/docs_symptoms.py was run (the symptom index)
  • No chain tool changed
  • No game files, keys or data extracted from a game in the change or the attachments

A device with supportsBCTextureCompression == NO (A15 / A16 and older iPhones and iPads) returns nil
for every texture made in a BC pixel format, so each BC texture of a game is missing. The device now
decides at start (nvn_bc_cpu_wanted): one that samples BC keeps the BC textures and the GPU deswizzle
unchanged; one that cannot makes a BC texture RGBA8 / RGBA8 sRGB / R8 / RG8 / RGBA16F and fills it with
texels decoded by bcdec from the deswizzled blocks (pkg_upload_, and buffer-to-texture copies in
copy_b2t). Views follow the substituted format; copies out of such a texture and BC render targets
are reported red. DM_TEST_NO_BC=1 takes the path on a Mac.

Tests: t_bcdec (CPU only, CI and ASan / UBSan), t_bc_cpu (Metal 4, local).
@seedds seedds closed this Oct 28, 2026
@seedds seedds reopened this Oct 28, 2026
@seedds

seedds commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Thank you for this, and for the care that went into it. Doing the check at run time is cleaner than the profile key, the write-up is honest about the history and the limits, and the tests are thorough: I ran t_bcdec here (99 checks, 0 failed, also clean under ASan / UBSan). Reading the diff, I agree that a device that samples BC goes through the same code as before.

Still, I'm going to close it. When I removed this path in 14fbcf9c I decided to support only GPUs that sample BC, and I'd like to stay with that for now. The cost isn't in run time. It's that every future texture path (copies, views, sparse textures, render targets, readbacks) would need to handle the decoded case, and I have no A15 / A16 to test it on. I'd rather not ship something I can't check and then can't fix when it breaks. That's a limit on my side, not a problem with your work.

In case it helps the NX2Apple fork, which keeps this path, here are a few things I noticed while reading (please correct me if I've misread any):

  • Which chips have BC: the comments and the new reference entry say supportsBCTextureCompression is NO before the A19. As far as I know the A17 Pro and later read BC natively (iPhone 15 Pro and newer), and Link's Awakening ran with its textures on an iPhone 15 Pro here. So the devices that need this are the A15 / A16 ones, including the iPhone 15 / 15 Plus.
  • BC textures as render targets: with BC, agl's BC4 block encoder (Breath of the Wild, Tears of the Kingdom, Wonder) goes through the uncompressed shadow (bcrt_attach). On the decoded path that attachment is dropped, so the encoded texture stays empty on those devices. It may be worth listing that under "not covered".
  • Vulkan and OpenGL layers: hle_vk.m and hle_gl.m still create BC textures unconditionally, so Hades, the two Galaxy games and Balatro would still lose them on an A15 / A16.
  • Copy ordering, unverified: for a packaged texture, a buffer-to-texture copy now relies on the next upload rather than an in-order GPU blit. If that upload can land ahead of draws earlier in the same submission, those draws would see the new content too early. I haven't confirmed it can happen.

Thanks again for taking the time on this, and for being so clear about what it is and isn't.

@seedds seedds closed this Oct 28, 2026
lucaszhongsj pushed a commit to lucaszhongsj/nxnative that referenced this pull request Nov 19, 2026
Upstream-Status: fork-only (declined seedds#488)
Reason: Support A15/A16 and older iOS GPUs without hardware BC texture compression including BC6H to RGB9E5
Issue: LNPANDY/NX2Apple#180
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