shader: pack v_cvt_pkrtz_f16_f32 with the native half conversion (GTA V) #1520
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1520
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1520/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 UTCProblem
GTA V (PPSA04263 01.010.002) uses
v_cvt_pkrtz_f16_f32in 613 of its 656 pixel shaders, about 16 times ineach 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 to2.8 ms per frame (see Testing).
Change
EmitPackFloat2x16Rtzpacks the pair withPackHalf2x16and unpacks it again. Each half whose magnitudeexceeds 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.
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.
EmitF32ToF16RtzBitsand its helperEmitSubConstantMinusU32are now unused and removed.Testing
The new and old conversions were compared bit for bit:
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_cfgincluded.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.
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
packHalf2x16would give a different result.CvtPkrtzF16F32SubnormalRoundsTowardZeroinshader_recompiler_computecovers that case; I could not run it here, so a run on Windows or Linux wouldconfirm 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.
@coderabbitai[bot] on GitHub · 2026-10-15 07:22 UTC · original
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
196549f4-6ce6-4407-9462-14950f18c037📥 Commits
Reviewing files that changed from the base of the PR and between
9e41d8320aand0cfa4e0a61.📒 Files selected for processing: 3
src/graphics/shader/recompiler/backend/spirv/spirvEmitterAlu.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.h💤 Files with no reviewable changes: 2
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:
nmzikPriority: ➖ Normal
Change: Refactor
Merge Risk: 🔵 Low · up to
0cfa4Some 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:
4 ·
1
v_cvt_pkrtz_f16_f32packing, with the GTA V context.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 helpto get the list of available commands.@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