fix(agc): sample 5_6_5, 1_5_5_5 and 4_4_4_4 textures in AMD's channel order #2386

Merged
kr4y4 merged 2 commits from upstream-pr/2386 into main 2026-10-10 22:34:10 +00:00
kr4y4 commented 2026-10-09 17:09:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kr4y4 on 2026-10-09T17:09:02Z.


What

Textures in 5_6_5_UNORM (133), 1_5_5_5_UNORM (134) and 4_4_4_4_UNORM (136) are sampled with their channels in the wrong order.

On AMD hardware the first component of a packed format sits in the least significant bits. Mesa's gfx10_format_table.py reverses the size list of each name ("Keep the bit sizes in little-endian order"). So:

format X Y Z W
5_6_5 4:0 10:5 15:11 —
1_5_5_5 4:0 9:5 14:10 15
4_4_4_4 3:0 7:4 11:8 15:12

TextureFormat.cpp maps them to VK_FORMAT_R5G6B5_UNORM_PACK16, VK_FORMAT_A1R5G5B5_UNORM_PACK16 and VK_FORMAT_R4G4B4A4_UNORM_PACK16. Those Vulkan formats name the most significant field first: R5G6B5 has R in bits 11..15, A1R5G5B5 has R in bits 10..14, and R4G4B4A4 has R in bits 12..15 and A in bits 0..3 (Formats).

ViewComponents sends DST_SEL X straight to VK_COMPONENT_SWIZZLE_R. So a 5_6_5 texel 0x001f (X = 1) samples as (0, 0, 1, 1), and 1_5_5_5 swaps red and blue the same way. In 4_4_4_4 the order is fully reversed, so X comes back from the alpha nibble.

radv uses the same three Vulkan formats and compensates in the view swizzle: vk_format.c maps them to PIPE_FORMAT_B5G6R5_UNORM, B5G5R5A1_UNORM and A4B4G4R4_UNORM.

This adds TextureComponentChannel, which gives the Vulkan channel that holds a guest component: X, Y, Z, W read B, G, R, B for 133 (a DST_SEL W of 5_6_5 returns X, measured on gfx1035 in review), B, G, R, A for 134, and A, B, G, R for 136. ViewComponents composes it after DST_SEL, so every sampled view of these formats reads the bits the guest meant, with any swizzle. The VkFormats are unchanged, and no other format is affected.

Tests:

  • RunTextureFormatTests checks, for each component of the three formats, that the chosen Vulkan channel starts at the guest bit (low-bit table taken from the Vulkan format definitions).
  • agc_driver_image_packed16_load (new) fills a 256×1 texture of each format, with every component either all zeros or all ones. It then checks image_load of every texel and image_sample_lz of texel 0 on the GPU, with DST_SEL X Y Z W and Z Y X W.

Found by comparing AnyPS5's GFX10 format tables with Mesa's and the Vulkan format definitions. Two independent passes flagged the same three entries.

Tested

Windows 11 (26200), WinLibs GCC 15.2.0 r7, CI configure flags, main at d70b8998:

  • agc_driver_graphics and agc_driver_image_packed16_load pass with --repeat until-fail:3 on an RTX 4070 Laptop GPU and an Intel Raptor Lake-S iGPU.
  • With TextureComponentChannel returning the component unchanged, as on main, both fail:
    • format 133 component 0 is not read from bit 0
    • 5_6_5_UNORM image_load X Y Z W: texel 0 component 0 is 0x00000000, expected 0x3f800000 on the RTX 4070.
  • Full suite on the RTX 4070: 508/508 passed.

Checklist

  • Based on current main; no other open PR implements the same thing
  • One topic per PR
  • No comments in code except technical debt
  • No silent stubs added
  • No notes, investigation files or images added to the repository
  • No new third-party code
  • Generic, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4803536253 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2386). Originally posted by [kr4y4](https://github.com/kr4y4) on 2026-10-09T17:09:02Z. --- ### What Textures in `5_6_5_UNORM` (133), `1_5_5_5_UNORM` (134) and `4_4_4_4_UNORM` (136) are sampled with their channels in the wrong order. On AMD hardware the first component of a packed format sits in the least significant bits. Mesa's `gfx10_format_table.py` reverses the size list of each name ("Keep the bit sizes in little-endian order"). So: | format | X | Y | Z | W | |---|---|---|---|---| | 5_6_5 | 4:0 | 10:5 | 15:11 | — | | 1_5_5_5 | 4:0 | 9:5 | 14:10 | 15 | | 4_4_4_4 | 3:0 | 7:4 | 11:8 | 15:12 | `TextureFormat.cpp` maps them to `VK_FORMAT_R5G6B5_UNORM_PACK16`, `VK_FORMAT_A1R5G5B5_UNORM_PACK16` and `VK_FORMAT_R4G4B4A4_UNORM_PACK16`. Those Vulkan formats name the most significant field first: R5G6B5 has R in bits 11..15, A1R5G5B5 has R in bits 10..14, and R4G4B4A4 has R in bits 12..15 and A in bits 0..3 (*Formats*). `ViewComponents` sends DST_SEL X straight to `VK_COMPONENT_SWIZZLE_R`. So a 5_6_5 texel `0x001f` (X = 1) samples as (0, 0, 1, 1), and 1_5_5_5 swaps red and blue the same way. In 4_4_4_4 the order is fully reversed, so X comes back from the alpha nibble. radv uses the same three Vulkan formats and compensates in the view swizzle: `vk_format.c` maps them to `PIPE_FORMAT_B5G6R5_UNORM`, `B5G5R5A1_UNORM` and `A4B4G4R4_UNORM`. This adds `TextureComponentChannel`, which gives the Vulkan channel that holds a guest component: X, Y, Z, W read B, G, R, B for 133 (a DST_SEL W of 5_6_5 returns X, measured on gfx1035 in review), B, G, R, A for 134, and A, B, G, R for 136. `ViewComponents` composes it after DST_SEL, so every sampled view of these formats reads the bits the guest meant, with any swizzle. The VkFormats are unchanged, and no other format is affected. Tests: - `RunTextureFormatTests` checks, for each component of the three formats, that the chosen Vulkan channel starts at the guest bit (low-bit table taken from the Vulkan format definitions). - `agc_driver_image_packed16_load` (new) fills a 256×1 texture of each format, with every component either all zeros or all ones. It then checks `image_load` of every texel and `image_sample_lz` of texel 0 on the GPU, with DST_SEL X Y Z W and Z Y X W. Found by comparing AnyPS5's GFX10 format tables with Mesa's and the Vulkan format definitions. Two independent passes flagged the same three entries. ### Tested Windows 11 (26200), WinLibs GCC 15.2.0 r7, CI configure flags, main at d70b8998: - `agc_driver_graphics` and `agc_driver_image_packed16_load` pass with `--repeat until-fail:3` on an RTX 4070 Laptop GPU and an Intel Raptor Lake-S iGPU. - With `TextureComponentChannel` returning the component unchanged, as on main, both fail: - `format 133 component 0 is not read from bit 0` - `5_6_5_UNORM image_load X Y Z W: texel 0 component 0 is 0x00000000, expected 0x3f800000` on the RTX 4070. - Full suite on the RTX 4070: 508/508 passed. ### Checklist - [x] Based on current `main`; no other open PR implements the same thing - [x] One topic per PR - [x] No comments in code except technical debt - [x] No silent stubs added - [x] No notes, investigation files or images added to the repository - [x] No new third-party code - [x] Generic, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Kikoarl commented 2026-10-09 17:44:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-09T17:44:06Z.


Linux run: Linux 7.2 (x86_64), Intel(R) Graphics (RPL-S) (Mesa 26.3.0-devel, Vulkan 1.4.363), GCC 16.2.1, Release, -DBUILD_TESTING=ON, #2386 (6fa4b70a) tested on current main (0fdda7be).

  • tests/agc_driver_graphics_tests: passes cleanly on Vulkan (Graphics validation tests passed), verifying the channel order updates for 5_6_5, 1_5_5_5 and 4_4_4_4 textures.
  • tools/check_conventions.py: passes cleanly.
  • No regressions observed across TextureFormat.cpp.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6086152702 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2386#issuecomment-6086152702). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-09T17:44:06Z. --- Linux run: Linux 7.2 (x86_64), Intel(R) Graphics (RPL-S) (Mesa 26.3.0-devel, Vulkan 1.4.363), GCC 16.2.1, Release, `-DBUILD_TESTING=ON`, #2386 (`6fa4b70a`) tested on current `main` (`0fdda7be`). - `tests/agc_driver_graphics_tests`: passes cleanly on Vulkan (`Graphics validation tests passed`), verifying the channel order updates for 5_6_5, 1_5_5_5 and 4_4_4_4 textures. - `tools/check_conventions.py`: passes cleanly. - No regressions observed across `TextureFormat.cpp`.
adriadam10 commented 2026-10-10 06:02:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:02:53Z.

Review: CHANGES_REQUESTED


Checked on RDNA2 (gfx1035) with the hardware oracle: image_load and image_sample_lz of 133/134/136, one bit set per row, DST_SEL X Y Z W and Z Y X W. X/Y/Z of all three formats and W of 1_5_5_5 and 4_4_4_4 come from the bits the PR says. Build, texture_format_lookup, agc_driver_graphics and agc_driver_image_packed16_load pass here, and the new execution test fails without the change.

One thing to fix before merging:

  • 5_6_5 with DST_SEL W: hardware doesn't return 1, it returns X (the low 5 bits). Texel 0x001f reads W = 1.0, 0x07e0 and 0xf800 read W = 0, and 0x1234 reads W = 0.645 = X, through both image_load and image_sample_lz. Another 3-channel format (30) does the same. The PR maps W to Vulkan's A, which is 1, and ImagePacked16Load.cpp:116 pins that (masks[component] == 0u ? One). So the test fixes a value the hardware doesn't produce. Either map W to the X channel for 133 (B in R5G6B5) and expect X in the test, or throw for DST_SEL W on 133, as converted 10_11_11 already does (TechnicalDebt, "Converted unorm images"). In both cases, add a TechnicalDebt line saying it was measured on gfx1035.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5477888091 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2386#pullrequestreview-5477888091). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:02:53Z. **Review:** CHANGES_REQUESTED --- Checked on RDNA2 (gfx1035) with the [hardware oracle](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/HW_ORACLE.md): `image_load` and `image_sample_lz` of 133/134/136, one bit set per row, DST_SEL X Y Z W and Z Y X W. X/Y/Z of all three formats and W of 1_5_5_5 and 4_4_4_4 come from the bits the PR says. Build, `texture_format_lookup`, `agc_driver_graphics` and `agc_driver_image_packed16_load` pass here, and the new execution test fails without the change. One thing to fix before merging: - 5_6_5 with DST_SEL W: hardware doesn't return 1, it returns X (the low 5 bits). Texel `0x001f` reads W = 1.0, `0x07e0` and `0xf800` read W = 0, and `0x1234` reads W = 0.645 = X, through both `image_load` and `image_sample_lz`. Another 3-channel format (30) does the same. The PR maps W to Vulkan's A, which is 1, and `ImagePacked16Load.cpp:116` pins that (`masks[component] == 0u ? One`). So the test fixes a value the hardware doesn't produce. Either map W to the X channel for 133 (`B` in R5G6B5) and expect X in the test, or throw for DST_SEL W on 133, as converted 10_11_11 already does (TechnicalDebt, "Converted unorm images"). In both cases, add a TechnicalDebt line saying it was measured on gfx1035.
klosixgaming-arch commented 2026-10-10 10:43:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-10T10:43:58Z.


Validated this on Windows — adds NVIDIA dGPU + an AMD iGPU to the RTX 4070 / Intel iGPU coverage in the PR description.

Setup: Windows 11 (10.0.26200), WinLibs GCC 15.2.0posix-14.0.0-ucrt-r7 (the CI Windows toolchain), Ninja, Release, BUILD_TESTING=ON, PR head 6fa4b70a.

Ran agc_driver_image_packed16_load and agc_driver_graphics on each device:

Test RTX 5090 RTX 4090 AMD Radeon (iGPU)
agc_driver_image_packed16_load ✅ ✅ ✅
agc_driver_graphics ✅ ✅ ✅

I also confirmed the new test actually exercises the bug (not a pass-through): with TextureComponentChannel returning the component unchanged — i.e. the main behavior — agc_driver_image_packed16_load fails on the 5090:

5_6_5_UNORM image_load X Y Z W: texel 0 component 0 is 0x00000000, expected 0x3f800000

…and passes again with the fix applied. So this is a positive confirmation on my hardware, not just "ran the PR's test and it passed".

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096680201 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2386#issuecomment-6096680201). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-10T10:43:58Z. --- Validated this on Windows — adds NVIDIA dGPU + an AMD iGPU to the RTX 4070 / Intel iGPU coverage in the PR description. Setup: Windows 11 (10.0.26200), WinLibs GCC `15.2.0posix-14.0.0-ucrt-r7` (the CI Windows toolchain), Ninja, Release, `BUILD_TESTING=ON`, PR head `6fa4b70a`. Ran `agc_driver_image_packed16_load` and `agc_driver_graphics` on each device: | Test | RTX 5090 | RTX 4090 | AMD Radeon (iGPU) | |---|---|---|---| | `agc_driver_image_packed16_load` | ✅ | ✅ | ✅ | | `agc_driver_graphics` | ✅ | ✅ | ✅ | I also confirmed the new test actually exercises the bug (not a pass-through): with `TextureComponentChannel` returning the component unchanged — i.e. the `main` behavior — `agc_driver_image_packed16_load` fails on the 5090: ``` 5_6_5_UNORM image_load X Y Z W: texel 0 component 0 is 0x00000000, expected 0x3f800000 ``` …and passes again with the fix applied. So this is a positive confirmation on my hardware, not just "ran the PR's test and it passed". Written with AI assistance.
kr4y4 commented 2026-10-10 15:22:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kr4y4 on 2026-10-10T15:22:08Z.


Thanks for measuring it. I went with the first option: for 5_6_5, W now selects X (B of R5G6B5), so DST_SEL W reads the low 5 bits as you measured.

  • TextureComponentChannel(133, A) returns VK_COMPONENT_SWIZZLE_B. 1_5_5_5 and 4_4_4_4 are unchanged.
  • RunTextureFormatTests expects bit 0 for W of 133. ImagePacked16Load now expects W = X for 5_6_5; that's the line you pointed at, so the test no longer pins 1.
  • TechnicalDebt has a new "Packed 16-bit textures" entry next to the converted unorm ones. It says the channel order, and W = X for 5_6_5, were measured on RDNA2 (gfx1035), not on the console.

Rebased on main de5e541d. agc_driver_graphics, agc_driver_image_packed16_load and texture_format_lookup pass with --repeat until-fail:3 on the RTX 4070 and the Intel iGPU. With W mapped back to A, both checks fail (format 133 component 3 is not read from bit 0, and 5_6_5_UNORM image_load X Y Z W: texel 1 component 3 is 0x3f800000, expected 0x00000000). check_conventions.py passes.

Not changed here: format 36 (B10G11R11_UFLOAT) is another 3-channel format with no remap, so its DST_SEL W still reads Vulkan's 1. I haven't measured it.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6099039762 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2386#issuecomment-6099039762). Originally posted by [kr4y4](https://github.com/kr4y4) on 2026-10-10T15:22:08Z. --- Thanks for measuring it. I went with the first option: for 5_6_5, W now selects X (`B` of R5G6B5), so DST_SEL W reads the low 5 bits as you measured. - `TextureComponentChannel(133, A)` returns `VK_COMPONENT_SWIZZLE_B`. 1_5_5_5 and 4_4_4_4 are unchanged. - `RunTextureFormatTests` expects bit 0 for W of 133. `ImagePacked16Load` now expects W = X for 5_6_5; that's the line you pointed at, so the test no longer pins 1. - TechnicalDebt has a new "Packed 16-bit textures" entry next to the converted unorm ones. It says the channel order, and W = X for 5_6_5, were measured on RDNA2 (gfx1035), not on the console. Rebased on main de5e541d. `agc_driver_graphics`, `agc_driver_image_packed16_load` and `texture_format_lookup` pass with `--repeat until-fail:3` on the RTX 4070 and the Intel iGPU. With W mapped back to A, both checks fail (`format 133 component 3 is not read from bit 0`, and `5_6_5_UNORM image_load X Y Z W: texel 1 component 3 is 0x3f800000, expected 0x00000000`). `check_conventions.py` passes. Not changed here: format 36 (B10G11R11_UFLOAT) is another 3-channel format with no remap, so its DST_SEL W still reads Vulkan's 1. I haven't measured it. Written with AI assistance.
adriadam10 commented 2026-10-10 22:34:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T22:34:06Z.

Review: APPROVED


Checked on current main: agc_driver_graphics, agc_driver_image_packed16_load and texture_format_lookup pass; agc_driver_image_packed16_load fails without the change. The 5_6_5 DST_SEL W = X now matches what we measured on gfx1035, and the TechnicalDebt entry says so. Windows/NVIDIA/Intel runs by @klosixgaming-arch and @Kikoarl cover what we can't run here. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5481050431 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2386#pullrequestreview-5481050431). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T22:34:06Z. **Review:** APPROVED --- Checked on current main: `agc_driver_graphics`, `agc_driver_image_packed16_load` and `texture_format_lookup` pass; `agc_driver_image_packed16_load` fails without the change. The 5_6_5 DST_SEL W = X now matches what we measured on gfx1035, and the TechnicalDebt entry says so. Windows/NVIDIA/Intel runs by @klosixgaming-arch and @Kikoarl cover what we can't run here. Merging.
orishamir commented 2026-10-10 23:30:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orishamir on 2026-10-10T23:30:31Z.


agc_driver_image_packed16_load fails on main in CI's linux job (lavapipe, llvmpipe LLVM 20.1.2) since this merged: runs 38092384980 at 06b489d9 and 38092669169 at a28a79c7 both end with 4_4_4_4_UNORM image_load X Y Z W: texel 0 component 0 is 0x00000000, expected 0x3f800000.

Cause, measured in a container with CI's packages (ubuntu 24.04, mesa-vulkan-drivers 25.2.8-0ubuntu0.24.04.4): that lavapipe reports no format features for VK_FORMAT_R4G4B4A4_UNORM_PACK16 (vulkaninfo omits it, while B4G4R4A4, A4R4G4B4 and A4B4G4R4_UNORM_PACK16 are listed with SAMPLED_IMAGE_BIT). ResolveTextureFormat maps 4_4_4_4 (136) to R4G4B4A4 regardless, the texture is created, and a copy of this test that prints instead of throwing shows every channel reading 0 for all 240 non-zero texels and all 32 sampled threads, for both swizzles, while 5_6_5 and 1_5_5_5 are exact in the same run. Deterministic: 10 of 10 runs fail. The same binary passes on Mesa 26.0.8 (llvmpipe LLVM 21.1.8), which lists R4G4B4A4 with full features, so it is a gap in the lavapipe CI gets, not in the channel order this PR fixed.

CI stays red on that runner until 4_4_4_4 is either skipped when the format reports no features or mapped to a format that driver supports; B4G4R4A4_UNORM_PACK16 (also a mandatory Vulkan 1.0 sampled format) is reported by both Mesa versions.

Thanks @krishnaraya4, the new test is what caught this.

AI-assisted: yes (Claude Code Fable 5.1 xhigh)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103332408 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2386#issuecomment-6103332408). Originally posted by [orishamir](https://github.com/orishamir) on 2026-10-10T23:30:31Z. --- `agc_driver_image_packed16_load` fails on `main` in CI's `linux` job (lavapipe, llvmpipe LLVM 20.1.2) since this merged: runs [38092384980](https://github.com/boykopovar/AnyPS5/actions/runs/38092384980) at 06b489d9 and [38092669169](https://github.com/boykopovar/AnyPS5/actions/runs/38092669169) at a28a79c7 both end with `4_4_4_4_UNORM image_load X Y Z W: texel 0 component 0 is 0x00000000, expected 0x3f800000`. Cause, measured in a container with CI's packages (ubuntu 24.04, `mesa-vulkan-drivers` 25.2.8-0ubuntu0.24.04.4): that lavapipe reports no format features for `VK_FORMAT_R4G4B4A4_UNORM_PACK16` (vulkaninfo omits it, while `B4G4R4A4`, `A4R4G4B4` and `A4B4G4R4_UNORM_PACK16` are listed with `SAMPLED_IMAGE_BIT`). `ResolveTextureFormat` maps 4_4_4_4 (136) to `R4G4B4A4` regardless, the texture is created, and a copy of this test that prints instead of throwing shows every channel reading 0 for all 240 non-zero texels and all 32 sampled threads, for both swizzles, while 5_6_5 and 1_5_5_5 are exact in the same run. Deterministic: 10 of 10 runs fail. The same binary passes on Mesa 26.0.8 (llvmpipe LLVM 21.1.8), which lists `R4G4B4A4` with full features, so it is a gap in the lavapipe CI gets, not in the channel order this PR fixed. CI stays red on that runner until 4_4_4_4 is either skipped when the format reports no features or mapped to a format that driver supports; `B4G4R4A4_UNORM_PACK16` (also a mandatory Vulkan 1.0 sampled format) is reported by both Mesa versions. Thanks @krishnaraya4, the new test is what caught this. AI-assisted: yes (Claude Code Fable 5.1 xhigh)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!2386
No description provided.