Skip to content

patches: decode CPU BC buffer-to-texture copies and reject unsupported BC readbacks - #446

Closed
romulof wants to merge 1 commit into
LNPANDY:mainfrom
romulof:fix/bc-cpu-decode-views-uploads
Closed

romulof wants to merge 1 commit into
LNPANDY:mainfrom
romulof:fix/bc-cpu-decode-views-uploads

Conversation

@romulof

@romulof romulof commented Nov 1, 2026 •

Copy link
Copy Markdown

Fix or feature

  • Fix
  • Feature

What and why

Follows up on review feedback from @thehobbit85 and @v0lumehi. Rebased onto main (v1.4.0-rc2, commit 757b2e8d).

Since merged #888 (commit 908e4e84, patch 0088-nvn-decode-bc-uploads-before-copying-into-unpack.patch) already landed the fix for CPU-decoded BC uploads and format views (tex_view_to_mtl), this PR is strictly scoped to the remaining copy/readback operations on GPUs without native BC support:

  1. copy_b2t (buffer to texture copies): When g_bc_cpu is active, copies from guest memory buffers carrying compressed BC blocks into unpacked Metal textures (RGBA8Unorm, R8Unorm, RG8Unorm, RGBA16Float) are decoded on the CPU into temporary frame arena memory via nvn_bc_decode before blitting, matching the unpacked texture format and byte stride.
    • GPU-to-CPU synchronization: Resolves pending GPU writes (pend_hits, sbw_hits, gw_live) on the source range before CPU decoding so that earlier GPU copies in the same submission are not read stale, preserving compressed guest memory.
  2. copy_t2b (texture to buffer copies): Reading from an unpacked Metal texture back into guest memory expecting compressed BC format cannot be re-encoded without a BC encoder. Logs an explicit [RED] warning (RED_ONCE(101)) and returns early without corrupting buffer memory or triggering GPU assertions.
  3. mtl_read_texels (CPU readbacks): CPU readbacks from the GPU for CPU-decoded BC textures log an explicit [RED] warning (RED_ONCE(102)) and return zeroed memory.

Changes dropped per review

  • Upload & view fixes: Dropped (covered by merged patches: fix CPU BC texture uploads and views #888 / patch 0088).
  • Watchdog change: Dropped (frequency remains every 300 frames as in main).
  • front_ios_device_check bypass: Dropped (retained main check honoring dm_profile()->tex_bc_cpu via merged patch 0079).
  • Function pointer table / g_bc_cpu changes: Dropped (retained existing profile gate g_bc_cpu = !g_bc_gpu && dm_prof()->tex_bc_cpu).
  • RAM-saving setting (DM_BC6H_RGB9E5): Landed separately in patch 0117.

Test coverage

  • Extended dm/gfx/tests/t_bc_cpu.m (copies_and_readback):
    • Verified copy_b2t decodes BC1 compressed buffer into unpacked RGBA8 texels matching reference colors.
    • Verified GPU-to-BC copy ordering regression: mtl_sub_copy followed by copy_b2t properly resolves writes before decode and preserves compressed blue block in guest memory.
    • Verified copy_t2b logs RED_ONCE(101) and returns safely.
    • Verified mtl_read_texels logs RED_ONCE(102) and zeroes destination memory.
    • All 362 checks in t_bc_cpu pass cleanly.
  • Full validation suite passed cleanly.

@github-actions github-actions Bot added the patch-queue Changes the patch queue or the upstream pin label Nov 1, 2026
@romulof
romulof force-pushed the fix/bc-cpu-decode-views-uploads branch from 8bdc650 to d1b09f0 Compare November 1, 2026 19:43
@v0lumehi v0lumehi added this to the v1.1.2 milestone Nov 1, 2026
@romulof

romulof commented Nov 2, 2026 •

Copy link
Copy Markdown
Author

Folks, I'm updating this PR:

  1. Option to save RAM on non-BC devices:

Decodes BC6H HDR textures into RGB9E5Float (4 B/px) instead of RGBA16Float (8 B/px), halving their RAM footprint. This UI toggle (DM_BC6H_RGB9E5) is hidden if the device has native BC support.

  1. Always-enabled fallback:

Removed the per-game profile option (tex_bc_cpu); CPU BC decoding is now automatically active whenever the GPU lacks hardware BC support.

  1. Architecture & Performance:

At launch time, the host checks hardware capabilities and user settings, then binds specialized function pointers (g_pkg_upload, g_tex_desc, etc.) to one of 3 paths:

Path 1: Direct BC hardware uploads (if supported by GPU).
Path 2: CPU BC decode to standard Metal formats (RGBA16Float for BC6H).
Path 3: CPU BC decode with RAM optimization (RGB9E5Float for BC6H).

This guarantees zero per-texture branch overhead at runtime—all checks are resolved once at launch.

@romulof
romulof force-pushed the fix/bc-cpu-decode-views-uploads branch from d1b09f0 to 2fb9cb3 Compare November 2, 2026 14:12
@github-actions github-actions Bot added launcher The NX2Apple launcher app windows Windows (the Qt front end) labels Nov 2, 2026
@romulof romulof changed the title patches: handle views, non-sparse uploads, copies and attachments for CPU-decoded BC textures patches: always enable CPU BC decompression, dispatch BC paths via function pointers, and add RAM saving option Nov 2, 2026
@v0lumehi

v0lumehi commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the update and for the long explanation. Two things before I can review it:

  1. The PR ticks Fix and Feature. A fix is the crash on pre-A17 devices. The RAM saving setting (DM_BC6H_RGB9E5) is a feature, and by CONTRIBUTING.md it needs an issue in a milestone first. Could you split it into a fix PR and a feature PR?
  2. Engine changes go to seedds/nxnative first, as CONTRIBUTING.md and patches/README.md describe. Could you open the matching pull request there and put its number in the patch header (Upstream-Status: forwarded seedds/nxnative#N)? If you cannot, tell me and a maintainer sends it. The patch number is given out when the pull request merges, so you do not need to chase it.

@romulof
romulof marked this pull request as draft November 2, 2026 16:48
@romulof romulof closed this Nov 2, 2026
@romulof romulof reopened this Nov 2, 2026
@romulof
romulof force-pushed the fix/bc-cpu-decode-views-uploads branch from 2fb9cb3 to e4b0efb Compare November 2, 2026 18:15
@romulof romulof changed the title patches: always enable CPU BC decompression, dispatch BC paths via function pointers, and add RAM saving option patches: fix BC-compressed texture crash on pre-A17 devices Nov 2, 2026
@romulof
romulof marked this pull request as ready for review November 2, 2026 18:16
@romulof

romulof commented Nov 2, 2026

Copy link
Copy Markdown
Author

Thanks @v0lumehi! Here is the update addressing both review points:

1. Why this is fork-only and not on nxnative

In upstream pull request seedds/nxnative#488 ("Decode BC textures on the CPU when the Metal device cannot sample them"), upstream maintainer @seedds explicitly declined to merge and maintain CPU BC decompression in the upstream tree. Upstream's policy is to officially support only GPUs with native hardware BC sampling (A17 Pro+ and M-series); they do not have A15/A16 test devices and declined maintaining CPU fallback decoding across future texture changes (views, copies, attachments, uploads).

As documented in patches/README.md ("A feature upstream declines stays here as fork-only (declined seedds/nxnative#N), with the reason in Reason:"), this functionality is carried downstream in NX2Apple to keep pre-A17 devices (A15/A16) working. Both patches 0056 and 0057 have their headers set accordingly:

Upstream-Status: fork-only (declined seedds/nxnative#488)

matching the status of existing patch 0008-bc-cpu-decode.patch. Upstream PR #891 has also been closed.

2. Split PR: Pure Bug Fix

As requested, the PR has been split:

  • The DM_BC6H_RGB9E5 RAM saving option and all launcher UI/localization changes (frontend/Sources/NXCore/Localization.swift, frontend-qt/nspb_qt/localization.py, dm/games/settings.tsv, dm/ios/Sources/LauncherSettings.swift) have been completely removed from this PR. They are preserved on a separate feature branch (feat/bc6h-rgb9e5-ram-saving) and will be submitted as a separate feature PR once a milestone issue is created.
  • This PR now touches only patches/ and ticks strictly [x] Fix.
  • It fixes the SIGABRT crashes on pre-A17 devices (Metal texture view validation aborts on MTLPixelFormatInvalid, non-sparse uploads, copies, and attachment upgrades) and provides launch-time function-pointer dispatch with zero runtime branch overhead on supported hardware.

@v0lumehi

v0lumehi commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks @romulof. I accept the reason: upstream declined CPU BC decoding in seedds/nxnative#488, so this stays here as fork-only (declined seedds/nxnative#488), like patch 0008. The split is what I asked for: this PR now has only the fix, in patches/.

Two things on my side:

  • The PR conflicts with main now, because the patch numbers 0056 and 0057 are taken and the engine moves to a newer pin for v1.0.2. The numbers are given out when a PR merges, so you do not need to chase them. I look at the refresh after the bump and write here.
  • The RAM saving setting (DM_BC6H_RGB9E5) is a feature. It needs an issue in a milestone before a PR (see CONTRIBUTING.md). Open an issue with the form "Launcher problem or idea", This is: An idea, and describe the setting and where it shows (the launcher and the iPhone shell). I put it in a milestone when I have read it.

This PR stays in v1.1.2.

@romulof
romulof force-pushed the fix/bc-cpu-decode-views-uploads branch from e4b0efb to fe4b832 Compare November 2, 2026 20:53
@romulof romulof changed the title patches: fix BC-compressed texture crash on pre-A17 devices patches: fix BC-compressed texture crash and downsample BC HDR textures by default on pre-A17 devices Nov 2, 2026
@romulof
romulof force-pushed the fix/bc-cpu-decode-views-uploads branch from fe4b832 to e4b0efb Compare November 2, 2026 23:30
@romulof romulof changed the title patches: fix BC-compressed texture crash and downsample BC HDR textures by default on pre-A17 devices patches: handle views, non-sparse uploads, copies and attachments for CPU-decoded BC textures Nov 2, 2026
@v0lumehi v0lumehi modified the milestones: v1.2.1, v1.2.2 Nov 5, 2026

@v0lumehi v0lumehi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @romulof for the CPU BC decode work. Decoding BC views, uploads and copies on the CPU is a good way to get older iPhones past the crash, and the Upstream-Status: fork-only (declined seedds/nxnative#488) header is correct.

  1. Rebase on the current main. patches/series conflicts and the numbers 0056 and 0057 are taken. I renumber at merge.
  2. Patch 0057 moves the memory watchdog call from every 300 to every 60 frames for all games. That is unrelated to BC and not in the description. Please remove it or explain it.
  3. front_ios_device_check no longer stops pre-A17 devices at Play, for every game. That changes what players see on iPhone 14 and older. It needs an issue and a decision first. Please keep the check or open that issue.
  4. copy_t2b returns early and mtl_read_texels returns zeros for BC textures. Please log a RED line instead of silently giving zeros.
  5. With g_bc_cpu = !g_bc_gpu the !supportsBC && !g_bc_cpu check in mtl_init can never be true. Please remove it, and check that nothing else still uses the old tex_bc_cpu gate.
  6. Please add a result from a device after the fix, for example TotK past frame 153, with the log.
  7. The claim that function pointers remove per-texture branches does not match the many g_bc_cpu && nvn_bc_unpacked(...) branches that remain. Please correct the text.

Next: after the rebase and these points I review again. It fits v1.1.2 only if done soon, otherwise v1.1.3.

@v0lumehi v0lumehi added the enhancement New feature or request label Nov 5, 2026
@v0lumehi v0lumehi modified the milestones: v1.2.2, v1.2.3 Nov 5, 2026
@thehobbit85

Copy link
Copy Markdown
Contributor

The CPU-decoded BC upload/view portion is now covered by merged #888 (main commit 908e4e8), and the fully overlapping #776 is being closed as superseded.

This PR remains open because it also contains distinct work. Please drop the now-landed upload/view changes when refreshing the series and keep the remaining readback/copy behavior, device-gate decision and global watchdog change separate and justified, with the previously requested device validation. The new fix’s historical A15 evidence and synthetic current-pin GPU coverage do not establish those broader behaviors.

Landed patch: https://github.com/LNPANDY/NX2Apple/blob/908e4e848fd232304381fc19d4d847fcc10fc4c2/patches/0088-nvn-decode-bc-uploads-before-copying-into-unpack.patch

@romulof

romulof commented Nov 7, 2026 •

Copy link
Copy Markdown
Author

I was able to make TOTK run on an iPhone 14 Pro with:

  • This patch
  • Downsampling CPU-decoded BC HDR textures (I'm making another PR for that)
  • Turning off FXAA (another PR)

@romulof
romulof force-pushed the fix/bc-cpu-decode-views-uploads branch from e4b0efb to 115fef5 Compare November 7, 2026 14:33
@romulof romulof changed the title patches: handle views, non-sparse uploads, copies and attachments for CPU-decoded BC textures patches: decode CPU BC buffer-to-texture copies and reject unsupported BC readbacks Nov 7, 2026
@romulof

romulof commented Nov 7, 2026

Copy link
Copy Markdown
Author

Thanks @thehobbit85 and @v0lumehi for the guidance and reviews.

I have rebased the PR onto the current main (8dcaea44, on the new pin b2d46b86d03f) and addressed all review feedback:

  1. Uploads & Views dropped: Since merged patches: fix CPU BC texture uploads and views #888 landed 0088-nvn-decode-bc-uploads-before-copying-into-unpack.patch covering the BC upload/view paths, those changes have been removed from this PR.
  2. Watchdog reverted: Dropped the 60-frame watchdog change; hle_nvn.c remains at the original 300-frame frequency.
  3. Device check retained: Dropped the bypass of front_ios_device_check; the check is kept intact (and already honors dm_profile()->tex_bc_cpu via merged patch 0079).
  4. Copy & readback logging: Added explicit [RED] logging via RED_ONCE(101) for unsupported copy_t2b and RED_ONCE(102) for mtl_read_texels instead of silent early returns.
  5. Gating & dead checks: Cleaned up the tree so g_bc_cpu = !g_bc_gpu && dm_prof()->tex_bc_cpu is preserved without dead checks or premature function pointer tables.
  6. Test coverage: Extended dm/gfx/tests/t_bc_cpu.m (copies_and_readback) to verify BC1 copy_b2t decoding to expected texels, copy_t2b safe rejection, and mtl_read_texels zeroing. All 358 checks in t_bc_cpu pass cleanly.
  7. Series & Validation: The series applies cleanly as patch 0090 (will be renumbered on merge as needed). Pre-push validation passed all 873 Mac launcher tests and 385 Qt tests.

@v0lumehi v0lumehi modified the milestones: v1.2.4, v1.3.1 Nov 7, 2026

@thehobbit85 thehobbit85 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed revised head 115fef5c96b69e2fabc3f1314f225fd37dc9cb6e. The narrowed scope addresses the earlier watchdog, device-gate and already-landed upload/view points. The existing GPU test passes all 358 checks under Metal validation, but a new ordering regression blocks merging.

The CPU decode reads sb.contents immediately, even when an earlier GPU buffer copy in the same submission is still writing that source. With unchanged production code on an M2 Max, a BC1 source initially contains red, then mtl_sub_copy queues blue over that source before copy_b2t. After completion the source is blue (1f 00), but the copied texture is red (255,0,0,255). The additional test reports 360 checks, 1 failure, both normally and with DM_NVN_ENC_THREAD=1. Waiting for the source-copy submission before decode produces blue and passes 362 checks.

Please resolve the source range’s pending GPU writes before decoding on the CPU, preserve the compressed guest-memory representation, and add this GPU-copy-to-BC-copy regression. The existing pending-write checks around the guest mirror do not protect this new unconditional CPU read. The relevant production functions are unchanged at the newer engine pin, so refreshing the patch list alone will not fix it.

The reported iPhone 14 Pro result is useful context, but it includes two additional changes. The previously requested device log should identify the complete tested patch set. Patch numbering will be reconciled at merge time after the code is ready.

+ size_t urow = (size_t)c->r.width * (size_t)bc_bpp, uimg = urow * (size_t)c->r.height;
+ id<MTLBuffer> ab; size_t ao = arena_alloc(uimg * (size_t)nz, &ab);
+ for (int z = 0; z < nz; z++)
+ nvn_bc_decode(c->tex->d.format, (const uint8_t *)sb.contents + mo + (size_t)z * img, row, c->r.width, c->r.height, (uint8_t *)ab.contents + ao + (size_t)z * uimg, urow);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Wait for source GPU writes before CPU decoding. A preceding mtl_sub_copy in the same submission can leave this CPU read seeing old BC blocks. The real-GPU reproducer finishes with a blue compressed source but a red destination texture. Resolve the pending source writes first and add a regression for that ordered copy sequence.

@v0lumehi
v0lumehi dismissed thehobbit85’s stale review November 7, 2026 18:43

Dismissed: the reviewer is no longer a maintainer of this repository. The points of this review stay as comments for the author to judge.

@v0lumehi

v0lumehi commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

@romulof thanks. The patch conflicts with main in patches/series, and the number 0090 is taken, so please rebase onto main. I renumber at merge.

@thehobbit85 found a real bug on Nov 7: the CPU decode reads the source buffer before an earlier GPU copy into it has finished. Please wait for the pending GPU writes on the source range before decoding, keep the compressed guest-memory data, and add a regression test for a GPU copy followed by a BC copy. Please also list the complete patch set you tested on the iPhone 14 Pro, with the log.

@romulof
romulof force-pushed the fix/bc-cpu-decode-views-uploads branch from 115fef5 to ff17248 Compare November 11, 2026 12:22
@v0lumehi v0lumehi modified the milestones: v1.3.1, v1.4.0, v1.5.0 Nov 12, 2026
@v0lumehi

Copy link
Copy Markdown
Collaborator

Thanks for this. We no longer take submissions for the nxnative engine here. Engine fixes, game profiles and patches go to seedds/nxnative directly. Please open this change there as a pull request: https://github.com/seedds/nxnative (follow their CONTRIBUTING.md). Our CONTRIBUTING.md says the same now (#1433). I am closing this PR.

@v0lumehi v0lumehi closed this Nov 18, 2026
@romulof

romulof commented Nov 18, 2026

Copy link
Copy Markdown
Author

@v0lumehi Thanks for the note. Just to clarify why this cannot be submitted to seedds/nxnative: g_bc_cpu does not exist upstream.

Upstream nxnative strictly targets devices with hardware BC support (MTLDevice.supportsBCTextureCompression: Apple silicon Macs and A17 Pro+ iPhones) and explicitly declined CPU BC decoding in seedds/nxnative#488.

The CPU BC decompression subsystem is an NX2Apple fork-only feature, originally added in patch 0008 (issue #123, tagged Upstream-Status: fork-only (declined seedds/nxnative#488)) and extended by patches 0079, 0088 (#888), and 0117.

This PR is not a new engine feature—it is a bugfix for NX2Apple's own downstream 0008/0088 patches:

  1. In copy_b2t, buffer-to-texture copies carrying compressed BC blocks into unpacked Metal textures were blitting raw compressed data without decoding.
  2. The latest commit addresses @thehobbit85's review feedback on GPU-to-CPU ordering hazards (waiting for pending GPU writes on the source range before decode, preserving compressed guest memory, and passing all 362 checks in t_bc_cpu).

Since upstream does not carry g_bc_cpu, nvn_bc_unpacked, or nvn_bcdec.c, submitting this to seedds/nxnative would be rejected immediately. If NX2Apple plans to deprecate and remove the fork-only BC compatibility patches (0008, 0088, 0117) altogether, that's completely understandable; but if NX2Apple continues to maintain them for pre-A17 Pro iOS devices, this bugfix belongs alongside them in patches/.

Could you reconsider reopening this as a bugfix for existing fork-only code, or advise how you'd like bugfixes to NX2Apple-specific patches handled under the new policy?

v0lumehi added a commit that referenced this pull request Nov 18, 2026
patches: decode CPU BC buffer-to-texture copies and reject unsupported BC readbacks (#446)
@v0lumehi

Copy link
Copy Markdown
Collaborator

Thanks @romulof, you are right. The CPU BC decode only exists in this fork, so this fix cannot go upstream. I carried your patch in #1440, with you as its author. It is merged now as patches/0063. The wait for pending GPU writes and your regression test are in it.

958613554 pushed a commit to 958613554/NX2Apple that referenced this pull request Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request launcher The NX2Apple launcher app patch-queue Changes the patch queue or the upstream pin windows Windows (the Qt front end)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants