fix(shader): pack D16 image reads toward zero and saturate 32-bit integers as RDNA2 does #22750

Merged
MichonGoddijn231849 merged 2 commits from upstream-pr/22750 into main 2026-11-26 16:50:58 +00:00
MichonGoddijn231849 commented 2026-11-18 06:56:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MichonGoddijn231849 on 2026-11-18T06:56:18Z.


What

D16 image reads packed their components the host's way, which differs from RDNA2. Floats went through packHalf2x16: round to nearest even, overflow to infinity, signaling NaNs quieted. Integers were truncated to their low 16 bits. RDNA2's texture unit does this instead:

  • 32-bit float texels (formats 22, 64, 74, 77): round toward zero, so 1 + 3·2⁻¹² gives 0x3c01, not 0x3c02, and finite values beyond 65504 become ±0x7bff, not infinity. Denormals truncate, so 0x337fffff gives 0.
  • 8- and 16-bit UNORM and SNORM texels: round to nearest even, as before. Measured over all 256 8-bit codes and 256 16-bit ones, loads and point samples. 16-bit float texels are exact either way.
  • NaN: sign, 0x7c00, and the top ten mantissa bits, or 1 if those are zero. So 0x7f800001 gives 0x7c01, 0x7f804001 gives 0x7c02, and a 16_FLOAT signaling NaN comes back unchanged.
  • 32-bit integers: UINT saturates to 0xffff and SINT to −32768…32767. 0x00010000 gives 0xffff, not 0.

Change (SpirvImageEmitter.cpp, the D16 branch shared by loads, samples and gathers):

  • float components are packed toward zero with the existing EmitPackFloat2x16Rtz (the v_cvt_pkrtz_f16_f32 lowering) when the new per-image specialization constant D16TowardZeroBase + index is 1, else to nearest with EmitPackHalf2x16 as before. DescriptorBindingBuilder sets the constant from the T# format for direct images that have a 16-bit data access (!depthBitsCompatible): 1 for 32_FLOAT, 32_32_FLOAT, 32_32_32_FLOAT and 32_32_32_32_FLOAT, 0 otherwise. Images read through a table and registration modules do not know the format, so they keep round-to-nearest. A NaN input then gets the hardware's NaN half;

  • integer components go through UMin(·, 0xffff) / SClamp(·, −32768, 32767) before the 16-bit mask. 8- and 16-bit formats never exceed that range, so they are unaffected.

  • test: new ImageD16Load.cpp loads 16 texels each of 32_UINT, 32_SINT, 32_FLOAT, 16_FLOAT, 8_UNORM and 8_SNORM with image_load … d16 (DMASK 0x1). The 8_UNORM row includes code 9, which reads 0x2885 (toward zero would give 0x2884). It checks every lane against the gfx1036 result and the three untouched sentinels. The texel and expected tables were run through tools/hw-oracle on gfx1036 as they stand in the file, and match.

  • debt: a new shader.md entry (fragment docs/dev/debt/changes/fix-d16-image-reads-round-toward-zero.md).

Source of the semantics: tools/hw-oracle on gfx1036 (AMD Windows driver 32.0.21045.16010) with image_load, point image_sample_lz and image_gather4 (D16) over the values above. The sample and gather use the same packing as the load: toward zero and saturating. A point sample of a NaN first sees the sampler's canonical 0xffc00000 and so returns 0xfe00. That NaN-texel difference is recorded in #22727 and is not changed here.

Tested

Windows 11 Pro 10.0.26200, validation layer 1.4.363, ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer:

NVIDIA GeForce RTX 5080, 610.74 AMD Radeon(TM) Graphics, gfx1036, 32.0.21045.16010
agc_driver_image_d16_load, this branch passed, 0 VUID- passed, 0 VUID-
the same test on main (fix reverted) fails: image_load d16 of 32_UINT texel 0x00010000: v10 is 0x00000000, RDNA 2 returns 0x0000ffff fails the same way
ctest -R 'agc_driver_(image|format_d16|d16|depth_storage|sampled_cache_budget|depth_plane_sampling|texture)|agc_shader_memory' (132 tests), this branch (incl. agc_driver_image_table, which exercises table images that must not get the constant) 129 passed; image_plane_address_on_line, image_sample_lod_clamp and image_sample_lod_clamp_shader_lane fail, as they do on main on this GPU 132 passed (2 skipped by the device)

agc_driver_graphics and the 63 ^agc_driver_graphics$|^agc_shader tests also pass on both GPUs. Every log shows Insert instance layer "VK_LAYER_KHRONOS_validation", and none shows was not found but was requested.

A differential run of the hardware oracle against agc_oracle_runner with this change found:

  • D16 loads of 32_FLOAT, 32_UINT, 32_SINT, 8_UNORM and 8_SNORM match on both GPUs, as do point samples of the UNORM and SNORM textures;
  • D16 samples and gathers match on AMD;
  • on NVIDIA, only a NaN texel differs, through the sampler's own NaN handling (#22727).

pr_overlap.py --branch failed closed on 12 open PRs that share SpirvImageEmitter.cpp. None of their diffs touches the D16 packing.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 5080, driver 610.74, Windows 11 Pro 10.0.26200; AMD Radeon(TM) Graphics (RDNA2 iGPU, gfx1036), driver 32.0.21045.16010, Windows 11 Pro 10.0.26200
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5371404659 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22750). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-11-18T06:56:18Z. --- ### What D16 image reads packed their components the host's way, which differs from RDNA2. Floats went through `packHalf2x16`: round to nearest even, overflow to infinity, signaling NaNs quieted. Integers were truncated to their low 16 bits. RDNA2's texture unit does this instead: - **32-bit float texels (formats 22, 64, 74, 77):** round toward zero, so 1 + 3·2⁻¹² gives 0x3c01, not 0x3c02, and finite values beyond 65504 become ±0x7bff, not infinity. Denormals truncate, so 0x337fffff gives 0. - **8- and 16-bit UNORM and SNORM texels:** round to nearest even, as before. Measured over all 256 8-bit codes and 256 16-bit ones, loads and point samples. 16-bit float texels are exact either way. - **NaN:** sign, 0x7c00, and the top ten mantissa bits, or 1 if those are zero. So 0x7f800001 gives 0x7c01, 0x7f804001 gives 0x7c02, and a 16_FLOAT signaling NaN comes back unchanged. - **32-bit integers:** UINT saturates to 0xffff and SINT to −32768…32767. 0x00010000 gives 0xffff, not 0. **Change** (`SpirvImageEmitter.cpp`, the D16 branch shared by loads, samples and gathers): - float components are packed toward zero with the existing `EmitPackFloat2x16Rtz` (the `v_cvt_pkrtz_f16_f32` lowering) when the new per-image specialization constant `D16TowardZeroBase + index` is 1, else to nearest with `EmitPackHalf2x16` as before. `DescriptorBindingBuilder` sets the constant from the T# format for direct images that have a 16-bit data access (`!depthBitsCompatible`): 1 for 32_FLOAT, 32_32_FLOAT, 32_32_32_FLOAT and 32_32_32_32_FLOAT, 0 otherwise. Images read through a table and registration modules do not know the format, so they keep round-to-nearest. A NaN input then gets the hardware's NaN half; - integer components go through `UMin(·, 0xffff)` / `SClamp(·, −32768, 32767)` before the 16-bit mask. 8- and 16-bit formats never exceed that range, so they are unaffected. - **test:** new `ImageD16Load.cpp` loads 16 texels each of 32_UINT, 32_SINT, 32_FLOAT, 16_FLOAT, 8_UNORM and 8_SNORM with `image_load … d16` (DMASK 0x1). The 8_UNORM row includes code 9, which reads 0x2885 (toward zero would give 0x2884). It checks every lane against the gfx1036 result and the three untouched sentinels. The texel and expected tables were run through `tools/hw-oracle` on gfx1036 as they stand in the file, and match. - **debt:** a new `shader.md` entry (fragment `docs/dev/debt/changes/fix-d16-image-reads-round-toward-zero.md`). **Source of the semantics:** `tools/hw-oracle` on gfx1036 (AMD Windows driver 32.0.21045.16010) with `image_load`, point `image_sample_lz` and `image_gather4` (D16) over the values above. The sample and gather use the same packing as the load: toward zero and saturating. A point sample of a NaN first sees the sampler's canonical 0xffc00000 and so returns 0xfe00. That NaN-texel difference is recorded in #22727 and is not changed here. ### Tested Windows 11 Pro 10.0.26200, validation layer 1.4.363, `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `VK_LOADER_DEBUG=layer`: | | NVIDIA GeForce RTX 5080, 610.74 | AMD Radeon(TM) Graphics, gfx1036, 32.0.21045.16010 | | --- | --- | --- | | `agc_driver_image_d16_load`, this branch | passed, 0 `VUID-` | passed, 0 `VUID-` | | the same test on `main` (fix reverted) | fails: `image_load d16 of 32_UINT texel 0x00010000: v10 is 0x00000000, RDNA 2 returns 0x0000ffff` | fails the same way | | `ctest -R 'agc_driver_(image\|format_d16\|d16\|depth_storage\|sampled_cache_budget\|depth_plane_sampling\|texture)\|agc_shader_memory'` (132 tests), this branch (incl. `agc_driver_image_table`, which exercises table images that must not get the constant) | 129 passed; `image_plane_address_on_line`, `image_sample_lod_clamp` and `image_sample_lod_clamp_shader_lane` fail, as they do on `main` on this GPU | 132 passed (2 skipped by the device) | `agc_driver_graphics` and the 63 `^agc_driver_graphics$|^agc_shader` tests also pass on both GPUs. Every log shows `Insert instance layer "VK_LAYER_KHRONOS_validation"`, and none shows `was not found but was requested`. A differential run of the hardware oracle against `agc_oracle_runner` with this change found: - D16 loads of 32_FLOAT, 32_UINT, 32_SINT, 8_UNORM and 8_SNORM match on both GPUs, as do point samples of the UNORM and SNORM textures; - D16 samples and gathers match on AMD; - on NVIDIA, only a NaN texel differs, through the sampler's own NaN handling (#22727). `pr_overlap.py --branch` failed closed on 12 open PRs that share `SpirvImageEmitter.cpp`. None of their diffs touches the D16 packing. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 5080, driver 610.74, Windows 11 Pro 10.0.26200; AMD Radeon(TM) Graphics (RDNA2 iGPU, gfx1036), driver 32.0.21045.16010, Windows 11 Pro 10.0.26200 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-18 07:58:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-18T07:58:36Z.

Review: COMMENTED


Reviewed — no blocking findings

The changed D16 packing logic and its load test are consistent with the stated behavior; I did not run the tests or independently confirm the reported hardware results.

What I ran

Reviewed head dbd8b11ad392e81af9fdda24d10b41212f4ab670 using the prepared review bundle and the pinned repository reader. No executable commands or tests were run. I read the full new ImageD16Load.cpp, the changed ResultVector function and its RTZ helper, and the full debt entry. The shader source was otherwise not read.

Results

Check Result Note
Diff and targeted source review passed Changed logic and test inspected
Automated tests not run Hardware and CI results are author-reported
Negative control not run No local test execution

Negative control

Could not run a negative control because tests were not run.

Findings

Blocking

None.

Non-blocking

  • The added regression test exercises D16 loads. Since the same conversion path serves samples and gathers, consider adding automated coverage for those operations when practical.

Sources

Not verified

I did not run the build or tests, and did not independently verify the described NVIDIA or AMD hardware behavior. The rest of SpirvImageEmitter.cpp was not read. All three changed files were included in the review bundle; the test and debt entry were read in full, while only the changed shader function and the referenced conversion helper were read.

To get this to a good state

Nothing is required. Optional: add sample and gather regression coverage for D16 conversion.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5952711251 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22750#pullrequestreview-5952711251). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-18T07:58:36Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The changed D16 packing logic and its load test are consistent with the stated behavior; I did not run the tests or independently confirm the reported hardware results. ## What I ran Reviewed head `dbd8b11ad392e81af9fdda24d10b41212f4ab670` using the prepared review bundle and the pinned repository reader. No executable commands or tests were run. I read the full new `ImageD16Load.cpp`, the changed `ResultVector` function and its RTZ helper, and the full debt entry. The shader source was otherwise not read. ## Results | Check | Result | Note | | --- | --- | --- | | Diff and targeted source review | passed | Changed logic and test inspected | | Automated tests | not run | Hardware and CI results are author-reported | | Negative control | not run | No local test execution | ## Negative control Could not run a negative control because tests were not run. ## Findings ### Blocking None. ### Non-blocking - The added regression test exercises D16 loads. Since the same conversion path serves samples and gathers, consider adding automated coverage for those operations when practical. ## Sources - [Changed D16 packing logic at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/dbd8b11ad392e81af9fdda24d10b41212f4ab670/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp#L356-L381) - [Added D16 load test at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/dbd8b11ad392e81af9fdda24d10b41212f4ab670/core/libs/prx/libSceAgcDriver/tests/execution/ImageD16Load.cpp) - [Added technical debt entry at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/dbd8b11ad392e81af9fdda24d10b41212f4ab670/docs/dev/debt/changes/fix-d16-image-reads-round-toward-zero.md) ## Not verified I did not run the build or tests, and did not independently verify the described NVIDIA or AMD hardware behavior. The rest of `SpirvImageEmitter.cpp` was not read. All three changed files were included in the review bundle; the test and debt entry were read in full, while only the changed shader function and the referenced conversion helper were read. ## To get this to a good state Nothing is required. Optional: add sample and gather regression coverage for D16 conversion.
MarcZX10 commented 2026-11-18 16:40:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-18T16:40:32Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the focused code and test support the stated D16 packing behavior.

What I ran

Reviewed head 671392e910644307c003f2da81dc4095a34ab5fc. No commands or tests were run in this review; the hardware and test results in the description are author reported.

Results

Check Result Note
Changed code review passed All five changed files read
Tests in this review not run Hardware results are author reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests were run. Hardware behavior and the author reported CI/test results were not independently verified. All files listed as reviewed in manifest.json were read: ImageD16Load.cpp, DescriptorBindingBuilder.cpp, PipelineSpecialization.hpp, SpirvImageEmitter.cpp, and the D16 debt entry. No changed files were excluded.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5960989871 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22750#pullrequestreview-5960989871). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-18T16:40:32Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the focused code and test support the stated D16 packing behavior. ## What I ran Reviewed head `671392e910644307c003f2da81dc4095a34ab5fc`. No commands or tests were run in this review; the hardware and test results in the description are author reported. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | All five changed files read | | Tests in this review | not run | Hardware results are author reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No tests were run. Hardware behavior and the author reported CI/test results were not independently verified. All files listed as reviewed in `manifest.json` were read: `ImageD16Load.cpp`, `DescriptorBindingBuilder.cpp`, `PipelineSpecialization.hpp`, `SpirvImageEmitter.cpp`, and the D16 debt entry. No changed files were excluded. ## To get this to a good state Nothing is required.
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!22750
No description provided.