Skip to content

Under Proton, mad() can be split into a multiply and an add, so wg_div / pd_div3 are 1 ULP off (upstream fix proposed) #150

Open

Description

@j0nsh1n

Under Proton, WrongGPU's exact arithmetic is not exact everywhere. The cause is in the D3D12-to-Vulkan translation, not in WrongGPU, and a fix is proposed upstream: HansKristian-Work/dxil-spirv#365. This issue records what we found, for reference.

What happens

dxil-spirv translates mad() (DXIL FMad) to a SPIR-V Fma. It marks the Fma NoContraction only when the FMad carries dx.precise. Without that mark, RADV's compiler (ACO) may split the Fma into a multiply and an add. In wg_div, mad(-bs, y, 1.0f) became s_mul_f32 + s_sub_f32, and the remainder steps became v_mul_f32 + v_sub_f32. AMD's Windows driver keeps every non-precise mad() fused (clean/dx12/README.md), and wg_div and pd_div3 depend on that.

Measured on an RX 9070 XT (RADV) under GE-Proton 11-11, with vkd3d-proton 5b1089c8 + dxil-spirv#362:

  • native_helpers color_divide against IEEE division (NumPy): 44,010 of 1,802,240 results 1 ULP off (51 of 110 divisors).
  • native_direct_test check: the direct and staged routes differ by 1 in post.history from frame 3 (reset frame, exposure mode 0, pre-exposure 1.375).

In game, a difference this size is not visible. It matters for bit-exact comparisons run under Proton.

Fixes

  1. Upstream, dxil-spirv#365: every FMad's Fma gets NoContraction. With it, color_divide has 0 wrong results and the two routes give the same bits on all 8 frames. A timing A/B of Model M's frame showed +0.18%, inside the noise. In Cyberpunk, RR's frame time did not change. It reaches players once a vkd3d-proton with it ships in Proton.
  2. Interim, no rebuild: vkd3d-proton's shader quirk. Create a text file with this line:
    0000000000000000-ffffffffffffffff FORCE_NOCONTRACT_MATH
    
    Then start the game with VKD3D_SHADER_QUIRKS=<path to that file> %command%. color_divide then also has 0 wrong results; Model M's frame A/B was +0.52%, inside the noise. Note that this applies to every shader in the game, not only WrongGPU's. I have not measured what it costs the game's own rendering.

Not a fix: precise

Marking the divisions' mad() calls precise would give dxil-spirv the dx.precise mark it needs. But build_dx12.py removes these marks on purpose, because AMD's Windows driver splits a precise mad(). Keeping them would trade the Proton problem for the same problem on Windows, so the fix belongs in the translation layer.

🤖 Generated with Claude Code

Activity

  1. j0nsh1n commented on Nov 14, 2026

    @j0nsh1n
    ContributorAuthor

    A correction to both fixes in this issue: please don't use either for now.

    In the game, the dxil-spirv change (HansKristian-Work/dxil-spirv#365) made Model M's upscaler take about 9 ms per frame instead of about 2.4 ms, according to OptiScaler's GPU timer. That was Cyberpunk 2077 on GE-Proton 11-11 with vkd3d-proton 5b1089c8. The "no cost" A/B above timed native_direct_test's kernels, not the ones the game runs. The FORCE_NOCONTRACT_MATH launch option applies the same change to every shader, so it should be expected to cost at least as much. I haven't measured it in the game.

    So for now the 1-ULP difference is the better trade-off: it isn't visible in the game. #365 is marked as a draft while I find out which kernels slow down and why.

    🤖 Generated with Claude Code

  2. j0nsh1n commented on Nov 14, 2026

    @j0nsh1n
    ContributorAuthor

    Retracting my previous comment: the dxil-spirv change does not slow Model M.

    Rerun in the game with a fresh start each time (Cyberpunk 2077, GE-Proton 11-11, RX 9070 XT, vkd3d-proton 5b1089c8): Model M's upscaler took 2.4 ms per frame without HansKristian-Work/dxil-spirv#365 and 2.39 ms with it, by OptiScaler's GPU timer. The 9 ms readings both came from one game session, where Ray Reconstruction and FSR 4 had run earlier. RADV's shader stats agree: the game's Model M kernels differ by under 1% with and without it.

    So fix 1 stands, at no measured cost, and #365 is ready for review again. I haven't measured the FORCE_NOCONTRACT_MATH launch option in the game. It covers every shader in the game, so the note above about its unknown cost for the game's own rendering still applies.

    🤖 Generated with Claude Code

  3. added
    area: linux
    area: runtime
    on Nov 14, 2026
  4. added this to the Later milestone on Nov 14, 2026
  5. phantoslayer21 commented on Nov 14, 2026

    @phantoslayer21
    Owner

    Thanks for recording this, and for the correction with the fresh-start timings. Labelled for Linux, milestone Later.

    The fix belongs upstream (HansKristian-Work/dxil-spirv#365). On Windows with AMD's driver, a non-precise mad() stays fused, so WrongGPU itself doesn't need to change. Once #365 lands, a small PR to the Linux notes saying which vkd3d-proton version gives bit-exact results would be welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: linuxarea: runtime

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions