Skip to content

vk: transcode BC3 and BC5_UNORM to ASTC 4x4 on BC-less GPUs - #579

Open
Propinbo wants to merge 1 commit into
mamcjr:mainfrom
Propinbo:feat/mali-bc-astc
Open

Propinbo wants to merge 1 commit into
mamcjr:mainfrom
Propinbo:feat/mali-bc-astc

Conversation

@Propinbo

@Propinbo Propinbo commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On GPUs without hardware BCn support (such as ARM Mali), textures.c previously transcoded only BC1 to ASTC 4x4 and decoded BC3 to RGBA8 (4x larger) and BC5_UNORM to R8G8_UNORM (2x larger).

This PR extends the BC-less CPU transcode path in runtime/nvn/bc1_astc.c and textures.c to transcode sampled non-RT BC3 and BC5_UNORM textures into ASTC 4x4 blocks (16 B/block), with zero changes to device.c or Adreno/Turnip (textureCompressionBC = 1) paths:

  1. BC3 -> ASTC 4x4 RGBA (VK_FORMAT_ASTC_4x4_UNORM_BLOCK / _SRGB_BLOCK, 1x size vs 4x RGBA8):
    • Analytical encoder preserving 4-colour RGB interpolation even when c0 <= c1, constant-alpha single-partition (0x53), constant-RGB, dual-plane RGB+alpha (0x442), and exact 0/255 alpha cutouts when a0 <= a1.
  2. BC5_UNORM -> ASTC 4x4 LA (CEM 4, VK_FORMAT_ASTC_4x4_UNORM_BLOCK, 1x size vs 2x RG8_UNORM):
    • Void-Extent encoding for constant blocks, single-varying-channel encoding, smooth dual-plane (0x452, delta <= 48), steep dual-plane (0x443) preserving exact 0/255 cutouts, and view-level VkComponentMapping expansion (IDENTITY expanded per slot before (R, G, B, A) -> (R, A, ZERO, ONE) in runtime/nvn/vulkan/bc_astc_policy.h).
  3. Strict exclusions & safety guards (textures.c, exec.c):
    • Never transcodes render targets (t->flags & 8), 3D volumes (t->target == 2), 1D / 1D-array textures (t->target == 0 || t->target == 3), MSAA textures (t->samples > 1), or BC5_SNORM.
    • Clears bc_vf, bc_astc_mode, and astc_* on create_image() R8G8B8A8_UNORM fallback.
    • Enforces vm == bc_astc_mode in view_format(), propagates bc_astc_mode in snapshot_image(), skips non-shadow attachment binds before creating unused cached VkImageView entries or marking gpu_owned in begin_pass(), and skips copy_buffer_to_texture / vkCmdBlitImage across ASTC / LA images.

Zero-Cost Isolation & Env Rollback

  • Adreno / Turnip (BC=1) and desktop: textureCompressionBC != 0 skips bc_target_ex() / bc_astc_cfg() (bc_vf = 0, bc_astc_mode = 0, zero extra vkGetPhysicalDeviceFormatProperties calls), and device.c has 0 lines modified.
  • Rollback toggles:
    • LARECOMP_BC_ASTC_EXT=0 — disable BC3 and BC5_UNORM -> ASTC 4x4
    • LARECOMP_BC3_ASTC=0|1, LARECOMP_BC5_ASTC=0|1 — per-format overrides (LARECOMP_BC1_ASTC=0 unchanged)

Tested vs. Untested GPU Matrix

GPU / Driver Status Notes
ARM Mali-G925-Immortalis MC12 (driver-id 9, v1.r49p1-03bet0) Tested Samsung SM-X730 (Android 16, API 36), BOTW 1.9.0, v0.5.1-beta (da30d45), PR commit 9a4e8a6d
Adreno + Mesa Turnip (BC=1) Logic/compile isolated (not run on device) Untouched by !format_supports(vf, need) (BC=1)
Maleoon / PanVK / other !BC && ASTC GPUs Not run on device Gated by !BC + ASTC_4x4 format feature checks; rollback via LARECOMP_BC_ASTC_EXT=0

Verification (SM-X730, Rebased onto v0.5.1-beta @ 9a4e8a6d)

  • Automated host & Bionic suites (all executed on 9a4e8a6d / SM-X730):
    • tools/test_soft_linear.py: PASS (23 + 3 + 203).
    • tools/test_vulkan_compat.py (16/16) & tools/test_vulkan_startup.py (33/33): PASS.
    • tools/test_bc1_astc.c (200k blocks/class WSL & aarch64 Bionic on SM-X730): PASS (0 illegal blocks, 0 exact 0/255 cutout mismatches, both K_ONECHAN channel orientations, test_odd_dims, test_policy_helpers).
    • tools/test_android_runtime.ps1 on SM-X730: 11/11 PASS, 0 mock sync hazards (includes 1T-vs-4T bit-exact bc3_to_astc & bc5_unorm_to_astc_la in test_bc_parallel, 0x4B shadow + LA swizzle in test_bc_shadow, and Case 10 in test_rt_load_upload testing fork() env-precedence, resolve_tex_format/bc_target_ex exclusions, view_format, and exec.c ASTC guards).
  • Real BOTW 1.9.0 texture error audit (plateau + zora-plaza + kakariko-day):
    • BC3: 115 textures (47 peak), 53.25 MB ASTC vs 212.89 MB RGBA8 ref, 3,489,781 blocks (const_a=69.2%, astc_err=0), 55.84M texels — RGB mean=0.387, p95=2, p99=3, max=4; Alpha all mean=0.718, p95=3, p99=15, max=44 (non-const mean=2.329, p95=11, p99=24, max=44).
    • BC5_UNORM: 252 textures (99 peak), 25.28 MB ASTC vs 101.08 MB RGBA8 ref (2x smaller than RG8), 1,657,053 blocks (steep=12.8%, astc_err=0), 26.51M texels — R mean=0.568, p95=3, p99=5, max=27; G mean=0.553, p95=3, p99=5, max=27; steep mean=1.737, p95=6, p99=12, max=27.
    • Excluded categories observed on BC1: BC1_SAMPLED=1111 (416 peak, 415.78 MB -> 831.57 MB ASTC vs 3325.90 MB RGBA8), BC1_RT=0, BC1_3D=0, BC3_EXCL=0, BC5U_EXCL=0.
  • Clean non-sync A/B (ext0: BC1=on BC3/5u=off vs ext1: BC1/3/5u=on) & 9a4e8a6d Release smoke (SM-X730):
    • Host VmRSS peak: 4043.6 MB -> 3863.9 MB (-179.7 MB); VMA steady-state gpu mem blocks settled at 1184 MB vs 1440 MB (1440 MB initial 256 MB block high-water mark).
    • 30 FPS perf parity preserved: plateau (30.00 / 29.64 low1 vs 30.00 / 29.42), zora-plaza (30.00 / 29.43 vs 30.00 / 29.28; on final 9a4e8a6d smoke: 30.00 FPS / 29.50 low1 / gpu p50 29.27 ms / rnd p50 16.76 ms / 1184 MB gpu mem / 3746.6 MB RSS), kakariko-day (29.96 / 25.69 vs 29.81 / 26.15).
  • Deterministic same-frame visual parity (2560x1440 match, bilinear, LARECOMP_SYNC_SHADERS=1):
    • hateno-dye-interior: aligned frame_00724 vs frame_00720 38.49 dB (mean_abs=0.695), shelf/cloth ROI 43.03 dB.
    • zora-plaza: aligned frame_00764 vs frame_00765 32.97 dB (mean_abs=2.959), pillar/arch ROI 34.71 dB.
    • riverside-stable-night: 25.40 dB (mean_abs=8.782).

@mamcjr

mamcjr commented Nov 27, 2026

Copy link
Copy Markdown
Owner

The texture memory saving is real and welcome (827 to 695 MB peak), since low-memory kills are one of the main Mali problems. Three things before merging:

  1. BC3 alpha precision. In the general case (bc1_astc.c:311) alpha gets 4 levels in dual-plane mode, where BC3 has 8, and the unit test accepts an alpha error up to 45/255 (tools/test_bc1_astc.c:161). The PR text only quotes the RGB error. On alpha-tested foliage that moves cutout edges, and on blended BC3 (smoke, decals, UI) it can band. Could you post the worst and mean alpha error measured on BOTW's real BC3 textures, and same-frame screenshots of foliage and of smoke or clouds, on this commit? (BC5 has a similar case on steep blocks, error 27, which shows in specular highlights on normal maps.)
  2. BC1 render targets. allow_astc = !(t->flags & 8) && t->target != 2 (textures.c:587) applies to BC1 too: BC1 textures flagged as render targets go back to RGBA8 (8x the memory), where main kept them as ASTC drawn through the BC shadow. It may well be the right call (ASTC has no colour-attachment usage), but it isn't mentioned: how many BC1 render-target textures does BOTW create, and how many MB?
  3. Decode mode. p50 went 30.29 to 30.95 ms with it alone, i.e. no gain. Please split it into its own PR, or keep it off by default until it shows one.

Add analytical BC3 -> ASTC 4x4 RGBA and BC5_UNORM -> ASTC 4x4 LA (CEM 4)
transcoders for GPUs without hardware BC support (such as ARM Mali).

- BC3 -> ASTC 4x4 keeps 16 B/block (4x smaller than RGBA8), preserves
  4-colour RGB mode regardless of endpoint order, and preserves exact
  0/255 alpha cutouts when a0 <= a1.
- BC5_UNORM -> ASTC 4x4 LA keeps 16 B/block (2x smaller than RG8), uses
  exact Void-Extent for constant blocks and CEM 4 L/A encoding with exact
  0/255 cutouts, and remaps sampled VkImageView swizzles (expanding
  IDENTITY per slot before mapping R,G,B,A -> R,A,ZERO,ONE).
- Excludes render targets (t->flags & 8) and 3D volumes (t->target == 2),
  tracks per-image bc_astc_mode across CopyTextureToTexture / snapshots /
  CopyBufferToTexture / non-shadow RT guards, and leaves Adreno / Turnip
  (BC=1) untouched with zero overhead.
- Adds LARECOMP_BC_ASTC_EXT, LARECOMP_BC3_ASTC, and LARECOMP_BC5_ASTC
  toggles.
@Propinbo Propinbo changed the title vk: transcode BC3 and BC5_UNORM to ASTC 4x4 and enable UNORM8 ASTC decode mode on Mali r49+ vk: transcode BC3 and BC5_UNORM to ASTC 4x4 on BC-less GPUs Nov 27, 2026
@Propinbo

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review! Rebased onto v0.5.1-beta (da30d45) and updated to 9a4e8a6d:

  1. VK_EXT_astc_decode_mode (UNORM8) removed completely

    • runtime/nvn/vulkan/device.c now has 0 diff against upstream/main, and LARECOMP_ASTC_DECODE_UNORM8 / UNORM8 decode-mode plumbing has been stripped cleanly.
  2. Real BOTW 1.9.0 texture error audit (SM-X730, plateau + zora-plaza + kakariko-day)
    Measured actual bcdec vs astc_decode_ldr error across every uploaded real texture (astc_err=0 on every block):

    Format Textures (Peak) ASTC Footprint (vs RGBA8 Ref) Blocks (const_a / steep) RGB Error (mean / p95 / p99 / max) Channel / Alpha Error (mean / p95 / p99 / max)
    BC3_ASTC 115 (47) 53.25 MB (212.89 MB ref, 4x save) 3,489,781 (69.2% const alpha) 0.387 / 2 / 3 / 4 All alpha (55.84M): 0.718 / 3 / 15 / 44; Non-const (17.21M): 2.329 / 11 / 24 / 44
    BC5U_ASTC 252 (99) 25.28 MB (101.08 MB ref; 2x vs RG8) 1,657,053 (12.8% steep) — R: 0.568 / 3 / 5 / 27, G: 0.553 / 3 / 5 / 27, Steep: 1.737 / 6 / 12 / 27
    BC1_SAMPLED 1111 (416) 831.57 MB (3325.90 MB ref) — — Excluded categories on BC1/BC3/BC5u: BC1_RT=0, BC1_3D=0, BC3_EXCL=0, BC5U_EXCL=0

    (Note: test_bc1_astc also confirms exact 0 / 255 alpha preservation (0 mismatches) when a0 <= a1.)

  3. Additional hardening & test coverage on 9a4e8a6d

    • Restricted allow_astc in textures.c to also exclude 1D (target=0), 1D-array (target=3), and MSAA (samples > 1), and cleared bc_vf/bc_astc_mode/astc_* on create_image() R8G8B8A8_UNORM fallback.
    • Moved begin_pass() non-shadow ASTC attachment skip before renderer_mark_gpu_owned() and vk_view() so skipped binds allocate zero unused cached views; fixed -U__ANDROID__ unused variable warning in snapshot_image().
    • Extended tests and verified all suites cleanly after the rebase on SM-X730: test_soft_linear.py (23+3+203), test_vulkan_compat.py (16/16), test_vulkan_startup.py (33/33), test_bc1_astc (both K_ONECHAN sides + zeroed Void-Extent), 11/11 test_android_runtime.ps1 (with bc3/bc5u 1T-vs-4T parity in test_bc_parallel, 0x4B in test_bc_shadow, and new Case 10 in test_rt_load_upload exercising fork() env precedence, resolve_tex_format/bc_target_ex, view_format, and exec.c ASTC guards), plus a signed Release APK smoke on zora-plaza (30.00 FPS / 29.50 1% low, gpu p50 29.27 ms, rnd p50 16.76 ms, gpu mem 1184 MB, VmRSS 3746.6 MB, [vk] BC -> ASTC 4x4: BC1=on BC3=on BC5u=on).
    • Clean 3-scene A/B (BC1 only vs BC1+BC3+BC5u): host VmRSS peak dropped 4043.6 -> 3863.9 MB (-179.7 MB) and steady-state VMA gpu mem blocks settled at 1184 MB vs 1440 MB while holding 30.00 FPS (plateau: 30.00/29.42, zora-plaza: 30.00/29.28, kakariko-day: 29.81/26.15).
  4. Deterministic same-frame visual parity (2560x1440 native, bilinear, LARECOMP_SYNC_SHADERS=1)

    • hateno-dye-interior (38.49 dB aligned, shelf/cloth crop 43.03 dB) • zora-plaza (32.97 dB aligned, arch crop 34.71 dB) • riverside-stable-night (25.40 dB):
    hateno-dye-interior (Shelf & Cloth 4x Crop + 8x Diff) zora-plaza (Carved Normal Floor 4x Crop + 8x Diff) plateau (Pine Foliage Cutout 4x Crop + 8x Diff)
    dye crop zora normal floor crop plateau pine cutout crop

Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 27, 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