shader: pack v_cvt_pkrtz_f16_f32 with the native half conversion (GTA V) #1520

Manually merged
github merged 2 commits from gh-pr/1520/head into gh-pr/1520/base 2026-11-26 09:58:40 +00:00
Owner

@⁠AN0DIN on GitHub · 2026-10-15 07:18 UTC · original
AN0DIN:pr/shader/native-rtz-half-pack → main · +17 −76 · 3 files · 2 commits · merged 2026-10-16 00:56 UTC

Problem

GTA V (PPSA04263 01.010.002) uses v_cvt_pkrtz_f16_f32 in 613 of its 656 pixel shaders, about 16 times in
each G-buffer pixel shader. The recompiler emulates this round-toward-zero conversion with integer operations
(EmitF32ToF16RtzBits): about 20 to 30 operations per half. On an Apple M5 at 1080p this change saves 1.7 to
2.8 ms per frame (see Testing).

Change

  • EmitPackFloat2x16Rtz packs the pair with PackHalf2x16 and unpacks it again. Each half whose magnitude
    exceeds its input's then takes one unit off. Half signs and magnitudes are separate, so that is the next
    value toward zero. Zeros and NaNs are never adjusted.
  • The result has the bits of the previous code for every finite input and infinity, as long as the native
    conversion returns one of the two neighbours of the input. That holds for round-to-nearest, and for
    round-toward-zero conversions as #1204 describes on AMD, where the adjustment never triggers. A NaN stays a
    NaN, possibly with another payload.
  • EmitF32ToF16RtzBits and its helper EmitSubConstantMinusU32 are now unused and removed.

Testing

  • The new and old conversions were compared bit for bit:

    • CPU: 2 005 120 values (every exponent with edge mantissas, plus random values), 0 differences;
    • Apple M5 GPU through Metal: 400 020 values (edge cases including the subnormal case of
      CvtPkrtzF16F32SubnormalRoundsTowardZero, plus random values), 0 differences in both safe and fast math.
  • The existing GPU harness cases for this opcode need 64-bit atomics, which MoltenVK does not expose, so I could
    not run them.

  • macOS (Apple M5, 32 GB, macOS 26, x86_64 build under Rosetta 2, MoltenVK 1.4.2): the build passes, and the
    15 ctest tests that run on macOS pass, shader_cfg included.

  • In game: fixed script camera, 60 s at each of three places at night, 1080p, separate launches in the order
    A B B A. The build is my GTA V branch: upstream, the patches the game needs on macOS, and other performance
    changes, so absolute fps depend on it. A and B are the same binary, with the same logic as this change
    switched off or on at startup. Two launches of the same variant differ by 0.4 fps or less.

    • Sandy Shores: 18.6 -> 19.6 fps;
    • Paleto Bay: 19.8 -> 20.9 fps;
    • Downtown: 22.7 -> 23.6 fps.

    The images look the same to the eye.

  • Windows / Linux: not tested locally. The change applies there too; the result is the same as long as the
    driver's half conversion returns one of the two neighbours of the input. A driver that flushes f16 subnormals
    in packHalf2x16 would give a different result. CvtPkrtzF16F32SubnormalRoundsTowardZero in
    shader_recompiler_compute covers that case; I could not run it here, so a run on Windows or Linux would
    confirm it.

AI assistance

An AI coding assistant (Claude Code) helped me find the cost in the generated MSL, write the change and the
comparison, run the measurements and draft this description. I reviewed the change and this text.

**@⁠AN0DIN** on GitHub · 2026-10-15 07:18 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1520) `AN0DIN:pr/shader/native-rtz-half-pack` → `main` · +17 −76 · 3 files · 2 commits · merged 2026-10-16 00:56 UTC ### Problem GTA V (PPSA04263 01.010.002) uses `v_cvt_pkrtz_f16_f32` in 613 of its 656 pixel shaders, about 16 times in each G-buffer pixel shader. The recompiler emulates this round-toward-zero conversion with integer operations (`EmitF32ToF16RtzBits`): about 20 to 30 operations per half. On an Apple M5 at 1080p this change saves 1.7 to 2.8 ms per frame (see Testing). ### Change - `EmitPackFloat2x16Rtz` packs the pair with `PackHalf2x16` and unpacks it again. Each half whose magnitude exceeds its input's then takes one unit off. Half signs and magnitudes are separate, so that is the next value toward zero. Zeros and NaNs are never adjusted. - The result has the bits of the previous code for every finite input and infinity, as long as the native conversion returns one of the two neighbours of the input. That holds for round-to-nearest, and for round-toward-zero conversions as #1204 describes on AMD, where the adjustment never triggers. A NaN stays a NaN, possibly with another payload. - `EmitF32ToF16RtzBits` and its helper `EmitSubConstantMinusU32` are now unused and removed. ### Testing - The new and old conversions were compared bit for bit: - CPU: 2 005 120 values (every exponent with edge mantissas, plus random values), 0 differences; - Apple M5 GPU through Metal: 400 020 values (edge cases including the subnormal case of `CvtPkrtzF16F32SubnormalRoundsTowardZero`, plus random values), 0 differences in both safe and fast math. - The existing GPU harness cases for this opcode need 64-bit atomics, which MoltenVK does not expose, so I could not run them. - macOS (Apple M5, 32 GB, macOS 26, x86_64 build under Rosetta 2, MoltenVK 1.4.2): the build passes, and the 15 ctest tests that run on macOS pass, `shader_cfg` included. - In game: fixed script camera, 60 s at each of three places at night, 1080p, separate launches in the order A B B A. The build is my GTA V branch: upstream, the patches the game needs on macOS, and other performance changes, so absolute fps depend on it. A and B are the same binary, with the same logic as this change switched off or on at startup. Two launches of the same variant differ by 0.4 fps or less. - Sandy Shores: 18.6 -> 19.6 fps; - Paleto Bay: 19.8 -> 20.9 fps; - Downtown: 22.7 -> 23.6 fps. The images look the same to the eye. - Windows / Linux: not tested locally. The change applies there too; the result is the same as long as the driver's half conversion returns one of the two neighbours of the input. A driver that flushes f16 subnormals in `packHalf2x16` would give a different result. `CvtPkrtzF16F32SubnormalRoundsTowardZero` in `shader_recompiler_compute` covers that case; I could not run it here, so a run on Windows or Linux would confirm it. ### AI assistance An AI coding assistant (Claude Code) helped me find the cost in the generated MSL, write the change and the comparison, run the measurements and draft this description. I reviewed the change and this text.
The round-toward-zero pack was emulated with integer operations: about 20 to 30 operations
per half. GTA V uses it in 613 of its 656 pixel shaders, about 16 times in each G-buffer
pixel shader.

The pair is now packed with PackHalf2x16, unpacked again, and each half whose magnitude
exceeds its input's takes one unit off. Half signs and magnitudes are separate, so that is
the next value toward zero, whatever rounding the native conversion uses as long as it
returns one of the two neighbours. Same bits as before for every finite input and infinity;
a NaN stays a NaN. Checked against the old code on 2 million values on the CPU, and on an
Apple M5 GPU through Metal (400 000 values, subnormals included, safe and fast math).

GTA V on an Apple M5 (MoltenVK), 1080p, fixed script camera, three places, A B B A between
launches: -1.7 to -2.8 ms per frame (+4 to +6 %).

Scope: generic
Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:58:38 +00:00
github manually merged commit f3f0497038 into gh-pr/1520/base 2026-11-26 09:58:40 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-15 07:22 UTC · original

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 196549f4-6ce6-4407-9462-14950f18c037
📥 Commits

Reviewing files that changed from the base of the PR and between 9e41d8320a and 0cfa4e0a61.

📒 Files selected for processing: 3
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterAlu.cpp
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpp
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.h
💤 Files with no reviewable changes: 2
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.h
  • src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📝 Walkthrough

EmitPackFloat2x16Rtz now uses native pair packing and unpacking. It compares the rounded and input magnitudes for each half, then adjusts the packed bits when the rounded magnitude is greater. The separate F32-to-F16 conversion helper and its supporting subtraction helper were removed, along with their declarations.

Suggested reviewers: nmzik

Priority: ➖ Normal

Change: Refactor

Merge Risk: 🔵 Low · up to 0cfa4

Some Vulkan drivers may produce incorrect results for half-precision subnormal inputs. Confirm that behavior on affected platforms or accept the bounded compatibility risk before merging.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly identifies the main change: using native half conversion for v_cvt_pkrtz_f16_f32 packing, with the GTA V context.
Description check Passed The description directly explains the motivation, implementation, validation results, platform limitations, and performance impact of the changes.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-15 07:22 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1520#issuecomment-6186778405) <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/1520?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=3"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3" alt="Review in Change Stack →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- recent_review_start --> No actionable comments were generated in the recent review. 🎉 <details> <summary><strong>ℹ️ Recent review info</strong></summary> <dl> <dd> <details> <summary><strong>⚙️ Run configuration</strong></summary> <dl> <dd> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `196549f4-6ce6-4407-9462-14950f18c037` </dd> </dl> </details> <details> <summary><strong>📥 Commits</strong></summary> <dl> <dd> Reviewing files that changed from the base of the PR and between 9e41d8320a2774b303fbad344c2347e8d0292e83 and 0cfa4e0a6180288be7f646f90a662f12a6f229ca. </dd> </dl> </details> <details> <summary><strong>📒 Files selected for processing: 3</strong></summary> <dl> <dd> * `src/graphics/shader/recompiler/backend/spirv/spirvEmitterAlu.cpp` * `src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpp` * `src/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.h` </dd> </dl> </details> <details> <summary><strong>💤 Files with no reviewable changes: 2</strong></summary> <dl> <dd> * src/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.h * src/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cpp </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. </dd> </dl> </details> <!-- recent_review_end --> <!-- walkthrough_start --> <details> <summary><strong>📝 Walkthrough</strong></summary> <dl> <dd> EmitPackFloat2x16Rtz now uses native pair packing and unpacking. It compares the rounded and input magnitudes for each half, then adjusts the packed bits when the rounded magnitude is greater. The separate F32-to-F16 conversion helper and its supporting subtraction helper were removed, along with their declarations. **Suggested reviewers:** `nmzik` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ➖ Normal <!-- change_assessment_commit:"0cfa4e0a6180288be7f646f90a662f12a6f229ca" --> **Change:** Refactor <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong>🔵 Low</strong> · up to <code>0cfa4</code></summary> <dl> <dd> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **🔵 Low** · up to `0cfa4` --> <!-- final_review_risk_coverage:{"sourceCommitId":"0cfa4e0a6180288be7f646f90a662f12a6f229ca","coveredCommitId":"0cfa4e0a6180288be7f646f90a662f12a6f229ca","kind":"reviewed"} --> Some Vulkan drivers may produce incorrect results for half-precision subnormal inputs. Confirm that behavior on affected platforms or accept the bounded compatibility risk before merging. <!-- final_review_risk_end --> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_start --> <details> <summary><strong>Pre-merge checks: <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 4 · <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> 1</strong></summary> <dl> <dd> <strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> Failed checks: 1 warning</strong> | Check name | Status | Explanation | Resolution | | :---------------- | :----------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ![Warning](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-warning.svg) | Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 1 files. | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary><strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> Passed checks: 4 passed</strong></summary> <dl> <dd> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------- | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title clearly identifies the main change: using native half conversion for `v_cvt_pkrtz_f16_f32` packing, with the GTA V context. | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description directly explains the motivation, implementation, validation results, platform limitations, and performance impact of the changes. | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autofix</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1520)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary><strong>❤️ Share</strong></summary> <dl> <dd> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </dd> </dl> </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
Author
Owner

@⁠Nmzik on GitHub · 2026-10-16 01:32 UTC · original

for future me: this is not accurate we dont preserve subnormals (dedicated gpus seem to work fine though)
At least we now execute as GPU opcode natively

**@⁠Nmzik** on GitHub · 2026-10-16 01:32 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1520#issuecomment-6205986011) for future me: this is not accurate we dont preserve subnormals (dedicated gpus seem to work fine though) At least we now execute as GPU opcode natively
This discussion has been locked. Commenting is limited to contributors.
No description provided.