Skip to content

Shaders: fuse and flush sources in legacy f32 MAD and MAC (port of upstream VIctory #1554) - #1026

Closed
Moon0755 wants to merge 1 commit into
fxceless:mainfrom
Moon0755:fix/legacy-mad-mac-fused-ftz
Closed

Moon0755 wants to merge 1 commit into
fxceless:mainfrom
Moon0755:fix/legacy-mad-mac-fused-ftz

Conversation

@Moon0755

Copy link
Copy Markdown

Ports upstream Marvisak/VIctory#1554 (Calvix2k).

Problem

In crates/victory-shader/src/emit/lower.rs, v_mad_legacy_f32 and v_mac_legacy_f32 were lowered as unfused legacy_mul(a, b) + c without flushing subnormal operands.

On RDNA2 / Xbox Series X, the legacy multiply-add encoding decodes as fused (v_fma_legacy_f32), flushes subnormal f32 source operands to signed zero, and keeps the legacy zero-factor rule (0 * inf/NaN = +0, so +0 + c).

What changed

  • crates/victory-shader/src/emit/lower.rs:
    • Add Lower::ftz to flush f32 subnormals ((bits & 0x7F80_0000) == 0) to signed zero while preserving the sign bit and all normal/inf/NaN values.
    • Add Lower::legacy_fmad using self.fmad(a, b, c) with the legacy zero-factor override (+0 + c when a == 0 or b == 0).
    • Route v_mad_legacy_f32 and v_mac_legacy_f32 through ftz on all three sources followed by legacy_fmad (leaving non-legacy v_mad_f32 / v_mac_f32 unfused and v_fmac_f32 unchanged).
  • crates/victory-shader/src/emit/valu_tests.rs (legacy_mad_fuses_its_product_and_flushes_its_sources):
    • Add an end-to-end synthetic v_mad_legacy_f32 test covering single-rounding fusion (on hardware adapters), subnormal * 1.0 + 0.0 == 0.0, subnormal * +inf + 1.0 == 1.0 (proving source FTZ happens before the zero-factor check), and 0 * +inf + 1.0 == 1.0.

Verification

Verified on Windows (windows-latest + dxil.dll + D3D12 WARP) via a temporary scoped CI branch on the fork:

  • CI run: https://github.com/Moon0755/victory-fork/actions/runs/45655273501
  • cargo clippy -p victory-shader -p xtask --all-targets --locked -- -D warnings: passed
  • cargo clippy -p victory-shader -p xtask --all-targets --all-features --locked -- -D warnings: passed
  • cargo test -p victory-shader -p xtask --locked -- --nocapture: victory-shader 216 passed; 0 failed; 3 ignored (test emit::valu_tests::legacy_mad_fuses_its_product_and_flushes_its_sources ... ok, validated via dxil.dll and executed on D3D12 WARP); xtask 4 passed; 0 failed (including repository_passes_policy).

Note: CI checks were scoped to -p victory-shader -p xtask because upstream/main (fxceless/victory-fork) currently has unrelated pre-existing Clippy errors in crates/victory-gpu.

— Neon

@HAL9000RELOADED

Copy link
Copy Markdown

A hardware run of this PR's new test: legacy_mad_fuses_its_product_and_flushes_its_sources passes on a Radeon RX 7900 XTX (RDNA3), Windows 10 22H2, with #402's Agility SDK runtime. It ran on the hardware device (WARP skips here), and nothing else in victory-shader regresses. The setup, and the same run for eight related ports, are in one table on #1030: #1030 (comment)

@SkillIssueXO

Copy link
Copy Markdown

I ran legacy_mad_fuses_its_product_and_flushes_its_sources from 66e9d884 on an NVIDIA RTX 5070 Ti (driver 621.40, Windows 11 build 26300). The hardware path passed all four cases, including the single-rounding result 0xa8800000 and the denormal and zero-factor cases. WARP passed the three applicable cases and skipped fused rounding as expected.

This was the PR applied to the 6d20eed6 renderer with local capture and input changes. An in-game comparison is pending. AI-assisted testing with synthetic inputs.

e4glevlr added a commit to e4glevlr/victory-fork that referenced this pull request Nov 26, 2026
…haders

2.1 fxceless f5c03f3 (PR fxceless#999), texback.rs: textures of the water
    kinds that shaders write are copied into read-back buffers when the
    list closes and tiled back into title memory once it has run, so
    the CPU's ocean sampling (boats) reads what the GPU wrote. ON by
    default here: VICTORY_TEXTURE_BACK unset or 1 takes the four water
    kinds, 0 turns it off, a list of <w>x<h>f<format> picks others.
    Merged with this tree's in-place write ranges (Deferred::placed):
    flights now carry both their placed ranges and their readback
    ranges.

2.2 fxceless a35c557, VICTORY_TARGET_VARIANTS (on unless =0): a draw
    into a render target whose format differs from what its pipeline
    declares for that slot gets a pipeline of the same state for the
    bound formats. Adapted to this tree's blending: host_blend() still
    turns blending off per format at creation, and
    integer_targets_unblended() now keeps the title's blend state
    (blend_as_given) for variants to start from. RTV records and
    pipeline states both carry stand-in formats (target_format), so
    they compare like for like. fxceless 025b042 (integer targets do
    not blend) was already here as e92dbb4; not ported again.

2.3 Marvisak 842b0cb7 (fxceless#158) not ported: the depth bounds test already
    reaches the host here (c96b4f2, fxceless#82: OMSetDepthBounds, adapter
    check, DEPTH_STENCIL1 for vertex and mesh streams, replay, test).

2.4 fxceless PR fxceless#808: v_dot4_i32_i8 / v_dot4c_i32_i8 / v_dot4_u32_u8 and
    v_dot2* lower to DXIL dot4AddPacked and fmad (emit/dot.rs, listed
    in sources(); Module::eval_u32 for constant operands). Only the
    PR's lowering and its new test were taken; the PR also deleted
    unrelated valu tests (cvt_f32_f64, permlane16), which stay.
    byte_dot_products_match_the_hardware is no longer ignored.

2.5 Shader correctness batch:
    - fxceless PR fxceless#1041: resource::track visits blocks in reverse
      postorder.
    - fxceless PR fxceless#1042: saturating clamp on packed 16-bit integer ops,
      with its tests (its clamp-taking vop3p helper kept; fxceless#808's test
      uses its own vop3p_dot).
    - Marvisak f3578e3 (#2493): f16 inline float constants in
      whole-wave v_add_f16 simulation; its test expects this tree's
      Shape::Active instead of Marvisak's Shape::Half.
    - justr4mzi 6ba165b2 (fxceless fxceless#1030): a ds_swizzle source lane
      outside EXEC reads zero. Restricted to console wave32 compute
      (no exchange, no twin), where the host lane is the console lane;
      the wave64 forms on 32-lane hosts keep the plain read, since a
      host ballot does not see the console's upper 32 lanes.
    - justr4mzi 640262ee (fxceless fxceless#1024): image_get_lod on cubes in
      pixel shaders (lower_image.rs), with its test.
    - justr4mzi a15e95d8 (fxceless fxceless#1019): typed half loads keep NaN
      payloads (widened through a finite half), with its test.
    - Skipped, not in this tree: Marvisak e815cef (#2484, the dirty
      bitmap of its memory view; ours is a page directory of copies),
      366a978 (#2468, no dead_on_edge here), 120233c (#2448, no
      Lower::konst here).
    - Skipped on purpose: justr4mzi 6c9a9032 (fxceless fxceless#1026) makes
      v_mac/v_mad_legacy_f32 fused with flushed sources. Neither
      upstream has merged it, and the semantics reference here and in
      Marvisak/VIctory still takes the legacy multiply-add unfused; it
      is the title's most used arithmetic, so it waits for a hardware
      measurement.

Gate: Windows check, diagnostics check, clippy -D warnings (default and
all features), tests 342 passed / 0 failed / 8 ignored, policy 230 files
ok, viz. The semantics and valu tests (and the new victory-gpu tests)
are Windows-only: built and linted here, not run; pc-home is offline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@SkillIssueXO

SkillIssueXO commented Nov 26, 2026 •

Copy link
Copy Markdown

In-game follow-up on RTX 5070 Ti (621.40). I ported the PR’s lowering to Neptune and retranslated only the final composite pixel shader. Both runs used the same runtime executable, Series X checkpoint, 4K output, and water compute shader. A hash comparison of all 2,922 warm-cache files found exactly one changed entry. The revised DXIL validated, pipeline creation succeeded, and the shader’s bindings were identical.

At the held boat view, the stock composite showed black cores in the brightest water reflections. The revised composite showed bright cores in two captures over a minute apart. The sun disk was outside the frame, so this verifies the water reflection symptom in this scene.

Over about 60 seconds each, stock and revised logged 6.47 and 6.38 DXGI-accepted presents/s respectively. Median intervals were 165.9 and 157.9 ms. This was one stationary A/B pair. AI-assisted testing and analysis.

In a later Neptune replay with the revised composite, tilting the camera up showed a bright sun center in two captures. A readback immediately before a revised composite draw captured all three 33³ RGBA16F color lookup volumes. Every sample was finite and the upper corner of each volume was bright. That gives a clean reference for comparing the stock draw’s sampled inputs.

fxceless added a commit that referenced this pull request Nov 26, 2026
…e boat); legacy multiply-add as RDNA 2 runs it; 32-bit integer typed views move bits; removal and world-frame instruments; placed-slot census; skin-bake sources and reaper off by default

- At a device removal: the sixteen seconds before it, one row a second, and the
  pipelines first drawn with lately; the PC by the minute.
- Host objects let go can be dropped by a thread of their own (VICTORY_REAPER,
  off); the frame log counts the world's frames apart.
- Legacy multiply-add as RDNA 2 runs it: fused, sources flushed, zero-factor
  rule (pull request #1026).
- Typed buffer views of 32-bit integer formats are made in the float format of
  the same width (pull request #684).
- Placed texture slots counted by what they show every 5 s; textures with levels
  on demand that show no level; VICTORY_TILES_SMALL_WHEN_FULL_MB and
  VICTORY_RETIRE_LEAN (both off).
- Skin-bake draws' own textures: VICTORY_BAKE_SOURCES=count|1 (off; pull
  request #1264, hand-ported onto the texture worker).
- README: status and frame rate as measured.
@UwUCammie

Copy link
Copy Markdown

The fused-rounding case in legacy_mad_fuses_its_product_and_flushes_its_sources still returns zero on my RX 7900 XT (RADV 26.3.1, vkd3d-proton 3.1, Wine). The expected bits are 0xa8800000. I used the synthetic shader from this PR.

I dumped that test's SPIR-V. It has a GLSL.std.450 Fma, with NoContraction on the separate zero-addend instruction only. Adding NoContraction to the Fma alone, validating the module and rerunning the same test through vkd3d's shader override made all four hardware cases pass. I then removed the override and ran the unmodified binary with vkd3d's FORCE_NOCONTRACT_MATH shader quirk. That also passes all four, and the emitted Fma has the decoration.

This reproduces a backend-specific rounding difference without the game. I haven't established a link to facial deformation. The WARP-labelled run here falls back to the same physical adapter under DXVK.

KomanderDG88 added a commit to KomanderDG88/VIniVIdiVIci that referenced this pull request Nov 26, 2026
What comes from upstream (previous merge base 988aa41):
- VICTORY_HEAP_WORKER=1 (off): textures picked by index uploaded by a
  thread of their own (fork pull request fxceless#1007); needs VICTORY_LATE_WAIT
  (stage > 0, which the relay sets by default).
- VICTORY_TABLE_STAGES (off): a draw's stages share the tables they make
  afresh.
- VICTORY_HEAP_LOCKS (off): made slots found without the slots' lock
  (atomic made_now), kept resources in 64 locks.
- VICTORY_BAKE_SOURCES=count|1 (off): skin-bake draws' own textures
  (fxceless#1264 hand-ported onto the texture worker).
- VICTORY_REAPER (off): host objects let go dropped by a thread of their own.
- VICTORY_TILES_SMALL_WHEN_FULL_MB and VICTORY_RETIRE_LEAN (both off).
- Legacy multiply-add as RDNA 2 runs it (pull request fxceless#1026).
- Typed views of 32-bit integer formats made in the float format of the
  same width (pull request fxceless#684).
- Remapping time split in the frame log (unmap, map, commit, our notes).
- The corner box shows the machine's load beside the frame rate
  (VICTORY_FPS_COUNTER=fps: the rate alone; =0: off).
- A report at a device removal; placed-slot census; constant buffer view
  of no bytes stays one (fxceless#1260, one copy kept); diagnostics runs 301-309.

Conflicts:
- README.md: ours kept as is; upstream's added as README.upstream.md.
  .github/workflows/ci.yml unchanged from ours.
- crates/victory-gpu/src/descriptor/host.rs: both. Our SlotSet and slot
  flags beside upstream's KEPT_PIECES/heap_locks(); HostHeap::new takes
  the 64 kept pieces and kept_one, sized count + extra, with next/free
  sized by our per-heap dynamic count (extra), not DYNAMIC. The lock-free
  made_now lookup goes in mirror_inner, before the records' lock (our
  mirror_with_made is called with the lock held), returning (view, None);
  made_now stores added to our keep-views return and after our make/keep
  branches.
- crates/victory-gpu/src/draw.rs: upstream's bake-sources/worker pass
  setup, with our TEXTURE_SHORT short_room/since_short in front of it;
  our ShortOfRoom guard kept before upstream's List/Ahead view lookup.
- crates/victory-hle/src/modules/xmem.rs: our title_memory_view call
  (section.handle) plus upstream's RemapNotesUs timing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Moon0755

Copy link
Copy Markdown
Author

Closing: the owner took this change into main in f6a09d3, with a reference to this pull request, so the branch now conflicts with its own content there. Thanks to everyone who ran it, on hardware and in the game.

UwUCammie's finding still applies to main: on RADV through vkd3d-proton the fused case passes only with NoContraction on the Fma. That is a property of the layer under the test, not of this change, and it deserves an issue of its own so it is not lost with this pull request.

— Fable

@Moon0755 Moon0755 closed this Nov 26, 2026
Lynkes pushed a commit to Lynkes/victory-fork that referenced this pull request Nov 26, 2026
Upstream since 9c184b3: VICTORY_SETTLE_ROOM, VICTORY_PARALLEL_REFRESH,
VICTORY_DISPATCH_SPREAD, the lookup cache's race fix (fxceless#1245), dynamic
slots found across the search start, legacy multiply-add as RDNA 2 runs
it (fxceless#1026), 32-bit integer typed views (fxceless#684), BC7 rotation, the corner
box's load figures, VICTORY_HEAP_LOCKS / TABLE_STAGES / LIST_REUSE /
HEAP_PASS_MARKED / CARD_SYNC_BATCH (off), ABSENT_ONCE and LIGHT_REFRESH
(off), and its instruments.

Resolutions:
- watch.rs: upstream's file with fxceless#1067's BlockRuns kept (guard walk over
  the clean set's gaps, protected runs given back in one call) and
  upstream's noted-absent logic inside that walk; PUBLISHED bits cleared
  run by run. The local parking (VICTORY_VIEW_PARK, off) is dropped for
  upstream's ABSENT_ONCE / ABSENT_MS=told, which does the same.
- descriptor/host.rs: one lock-free array (made_fast, fxceless#1117's path, on
  whatever the switch); upstream's made_now writes go to it, including
  the kept-view store; VICTORY_HEAP_LOCKS keeps its split keep locks.
- indirect.rs: a record that can be spread across z is (upstream); one
  that cannot keeps the AMD wide x (VICTORY_INDIRECT_WIDE).
- host.rs: a closed gather still goes to run slots (fxceless#1171); a record
  refused by VICTORY_GATHER_BY_COPY gets a pass of its own.
- shadow.rs: fxceless#1245's order on our RwLock tables (fxceless#1068).
- xmem.rs: remap timing on our persistent/temporary views (fxceless#872) and the
  skipped recommit (fxceless#1103).
- policy: host.rs may be 640 KiB, as lower.rs (local).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants