[pr-fix][shaders] The toward-zero half conversion survives a driver that folds the round trip #347

Open
ntsthay wants to merge 1 commit from ntsthay/pkrtz-driver-fold into main AGit
First-time contributor

Not run in the game. A unit test shows the problem on the card.

Contributor role (optional)

  • Fluxer ID:

What it changes

f16_toward_zero converts to a half, converts it back, and steps the half once toward zero when the value it came back as is larger than the input. On f3840d7, whole_wave_sections_keep_rounding_mode_changes fails on an RTX 4080 under vkd3d-proton: left: 15360, right: 15359. The value 0x3F7FFFFF comes out as 0x3C00, rounded to nearest, when it should be 0x3BFF.

I dumped the SPIR-V that vkd3d-proton makes from this shader (VKD3D_SHADER_DUMP_PATH). It is a faithful translation: PackHalf2x16, UnpackHalf2x16, FAbs, FOrdGreaterThan, ISub and OpSelect. Then I replaced the shader through VKD3D_SHADER_OVERRIDE with versions that store the values in between. The packed half is 0x3C00, as it should be. The value that comes back has the bits of the input, 0x3F7FFFFF, which no half has. So the driver's compiler folds UnpackHalf2x16(PackHalf2x16(x)) into x, and the comparison is never true. With a mask of 0xFFFF on the half before it is unpacked, the same shader passes.

The change does that in the DXIL: legacyF16ToF32 now reads h & 0xFFFF, which is the same value. It changes nothing on a driver that does not fold the round trip.

This reaches v_cvt_pkrtz_f16_f32 and the round-toward-zero mode of round_half. With the fold, a value above 65504 becomes infinity instead of stopping at 65504.

The DXIL of every shader that goes through f16_toward_zero changes, so their pipelines are built again once.

How it was tested

  • Tested revision: 4e240f7, this branch on f3840d7.
  • Commands and results, under Proton on an RTX 4080:
    • whole_wave_sections_keep_rounding_mode_changes: passes; fails on f3840d7 with the message above.
    • The 16 tests matching rtz|pkrtz|toward_zero|round_mode|f16 and the 76 emit::lower:: and emit::bitcode:: tests without the card pass.
  • Gates 1 and 2 are clean. Gate 3 ran under Wine, not on Windows.
  • Gameplay: not run.
  • Shader wave sizes tested: the test's own.
  • Coverage gaps: no game run; not run on Windows or on another vendor.

Checklist

  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: none found
Not run in the game. A unit test shows the problem on the card. ## Contributor role (optional) - Fluxer ID: ## What it changes `f16_toward_zero` converts to a half, converts it back, and steps the half once toward zero when the value it came back as is larger than the input. On `f3840d7`, `whole_wave_sections_keep_rounding_mode_changes` fails on an RTX 4080 under vkd3d-proton: `left: 15360`, `right: 15359`. The value 0x3F7FFFFF comes out as 0x3C00, rounded to nearest, when it should be 0x3BFF. I dumped the SPIR-V that vkd3d-proton makes from this shader (`VKD3D_SHADER_DUMP_PATH`). It is a faithful translation: `PackHalf2x16`, `UnpackHalf2x16`, `FAbs`, `FOrdGreaterThan`, `ISub` and `OpSelect`. Then I replaced the shader through `VKD3D_SHADER_OVERRIDE` with versions that store the values in between. The packed half is 0x3C00, as it should be. The value that comes back has the bits of the input, 0x3F7FFFFF, which no half has. So the driver's compiler folds `UnpackHalf2x16(PackHalf2x16(x))` into `x`, and the comparison is never true. With a mask of 0xFFFF on the half before it is unpacked, the same shader passes. The change does that in the DXIL: `legacyF16ToF32` now reads `h & 0xFFFF`, which is the same value. It changes nothing on a driver that does not fold the round trip. This reaches `v_cvt_pkrtz_f16_f32` and the round-toward-zero mode of `round_half`. With the fold, a value above 65504 becomes infinity instead of stopping at 65504. The DXIL of every shader that goes through `f16_toward_zero` changes, so their pipelines are built again once. ## How it was tested - Tested revision: `4e240f7`, this branch on `f3840d7`. - Commands and results, under Proton on an RTX 4080: - `whole_wave_sections_keep_rounding_mode_changes`: passes; fails on `f3840d7` with the message above. - The 16 tests matching `rtz|pkrtz|toward_zero|round_mode|f16` and the 76 `emit::lower::` and `emit::bitcode::` tests without the card pass. - Gates 1 and 2 are clean. Gate 3 ran under Wine, not on Windows. - Gameplay: not run. - Shader wave sizes tested: the test's own. - Coverage gaps: no game run; not run on Windows or on another vendor. ## Checklist - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: none found
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/347/head:ntsthay/pkrtz-driver-fold'
git switch 'ntsthay/pkrtz-driver-fold'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'ntsthay/pkrtz-driver-fold'
git switch 'ntsthay/pkrtz-driver-fold'
git rebase 'main'
git switch 'main'
git merge --ff-only 'ntsthay/pkrtz-driver-fold'
git switch 'ntsthay/pkrtz-driver-fold'
git rebase 'main'
git switch 'main'
git merge --no-ff 'ntsthay/pkrtz-driver-fold'
git switch 'main'
git merge --squash 'ntsthay/pkrtz-driver-fold'
git switch 'main'
git merge --ff-only 'ntsthay/pkrtz-driver-fold'
git switch 'main'
git merge 'ntsthay/pkrtz-driver-fold'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
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.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!347
No description provided.