Skip to content

Shaders: v_cubesc/tc_f32 keep denormal coordinates, v_cubema_f32 of a zero is +0 - #3127

Open
Moon0755 wants to merge 10 commits into
Marvisak:mainfrom
Moon0755:fix/cube-denormal-results
Open

Moon0755 wants to merge 10 commits into
Marvisak:mainfrom
Moon0755:fix/cube-denormal-results

Conversation

@Moon0755

Copy link
Copy Markdown

Follow-up to #1766. That change made the cube ops read a denormal coordinate as a zero of its sign, for the axis choice and for the result alike. The axis choice matches RDNA2; the result does not.

Problem

With IEEE mode off and 32-bit denormals flushed (the mode semantics_tests.rs models), RDNA2 does this:

  • A denormal coordinate reads as a zero where the major axis and the signs are chosen. main already does that.
  • v_cubesc_f32 and v_cubetc_f32 return the chosen coordinate's own bits. A negation flips the sign bit and nothing else. main returns a zero for a denormal.
  • v_cubema_f32 returns +0 when the major axis is a zero or a denormal. main returns -0 for a negative one.

Example: v_cubesc_f32(-inf, -denormal, 0x00000007) is 0x00000007 on RDNA2 and 0x00000000 on main. v_cubema_f32(-0, -0, -0) is 0x00000000 on RDNA2 and 0x80000000 on main.

Source of the expected values

The AnyPS5 project measured the four cube ops on RDNA2 (gfx1035, later gfx1036) in this float mode and keeps the rows in its test core/libs/prx/libSceAgcDriver/tests/execution/Vop3CubeClamp.cpp (its PR 508 describes the measurement: 4096 rows of zeros, denormals, infinities and NaNs). semantics_tests.rs already cites that oracle for min/max. The test here uses seven of the published rows as numbers; no code is taken from that project.

#1766 compared the translated shader on an RX 9070 and on WARP with a model. The model took the flushed result from the float mode in general, not from a run of the cube instructions on RDNA2, so the two do not contradict each other as measurements.

Fix

In the cube arm of lower.rs:

  • the flushed coordinates still choose the axis and the signs;
  • v_cubesc_f32 and v_cubetc_f32 select among the operands' integer bits and negate with an XOR of the sign bit;
  • v_cubema_f32 turns a result with a zero exponent into +0.

The pick and the negation are integer operations, so the result still does not depend on how the host treats denormals, which was the point of #1766.

Test

  • New: semantics_tests::cube_zero_and_denormals_match_rdna2_oracle, seven measured rows through the three ops (signed zeros, denormals, an infinity and a NaN beside a denormal).
  • Changed: the cube() model behind cube_faces_match_the_hardware (14,440 inputs) follows the same rule. With the old model that probe would fail on the denormal rows.

Verification

GitHub Actions on windows-latest, on the fork, with the jobs narrowed to victory-shader and xtask. The runner has no hardware adapter, so the probes ran on WARP only.

  • with the change (run 45785756570): clippy in both profiles passed, 303 passed; 0 failed; 3 ignored; both cube probes report 0 model mismatches;
  • the new test without the change (run 45785222301): 302 passed; 1 failed, the new test only, with 3, 3 and 4 mismatches of 7 for cubesc, cubetc and cubema.

Not checked

  • No run on a hardware adapter (AMD, NVIDIA or Intel). A run of cube_faces_match_the_hardware and the new test on a real GPU would be welcome.
  • clamp and the output multiplier on these ops. They go through write_f as before; a denormal cubesc/cubetc result now reaches clamp, where the host's saturate decides whether it is kept (the AnyPS5 table says RDNA2 keeps a positive one).
  • Signaling NaN coordinates: RDNA2 passes their bits through with IEEE mode off. Not touched here.
  • The measurements are from PC RDNA2 parts, not from the console.

— Gemini & Fable

… zero is +0

A denormal coordinate still reads as a zero where the major axis and the
signs are chosen (Marvisak#1766). The result is different: v_cubesc_f32 and
v_cubetc_f32 return the chosen coordinate's own bits, negated by
flipping the sign bit, and v_cubema_f32 returns +0 when the major axis
is a zero or a denormal. Measured on RDNA2 (gfx1035, gfx1036) with IEEE
mode off and 32-bit denormals flushed, as recorded in the AnyPS5
hardware-oracle tables.

The pick and the negation work on the bits, so the result does not
depend on how the host treats denormals. A probe checks seven measured
rows, and the cube() model follows the same rule.

Co-Authored-By: Gemini 4 Neon <noreply@google.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cloph-dsp

Copy link
Copy Markdown

Independent local verification of head 7d34d38 on Windows 11, Rust 1.100.0/MSVC:

  • VICTORY_WARP=1 cargo test --locked -p victory-shader --lib -- semantics_tests::cube_zero_and_denormals_match_rdna2_oracle — 1 passed, 0 failed.
  • git diff --check origin/main HEAD — clean.

Host GPUs: RTX 5060 Laptop (driver 621.24) and Radeon 780M RDNA3; this run used the WARP adapter. Synthetic shader inputs only; no game assets, extracted shaders or DRM material used. AI-assisted (Hy4 flash).

A negative Z face negates X by changing its sign bit alone. Selecting the signed values directly lost the subnormal payload on the RTX 4090 Laptop. Select the operand and its sign mask separately, then apply one integer XOR. Check both Z faces and both signs at the endpoints of the subnormal range.
The measured RDNA2 SC and TC clamp rows retain a selected positive subnormal without an output multiplier. Preserve that bit pattern while using the ordinary saturate for other values. Leave output multipliers on their existing path.

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The seven unmodified oracle rows in cube_zero_and_denormals_match_rdna2_oracle match the lowering cleanly (preserving subnormal S/T bits and producing +0 for zero/denormal v_cubema_f32 major axes), and emit/lower.rs is 521,349 bytes (under the 512 KiB limit).

One issue needs fixing before merge:

  • Source modifiers on subnormal S/T coordinates (emit/lower.rs:2103-2109, 6050): rx/ry/rz are still read via fsrc, which lowers VOP3 abs with DXIL FAbs and neg with floating-point Bin::Sub(-0.0, f). When the host flushes denormals on floating-point ops, a legal VOP3 source NEG modifier on a subnormal coordinate (for example, 0x0000_0007) can flush to -0.0 (0x8000_0000) before v_cubesc_f32/v_cubetc_f32 reads as_int, losing the subnormal payload. Please apply source abs/neg as bit operations for the S/T raw-bit path and add a test case covering source modifiers on subnormals.
  • NaN bit precision (semantics_tests.rs:1483-1488): Using a bit-exact INT comparison for v_cubesc_f32/v_cubetc_f32 oracle assertions would also pin down the NaN sign and quiet bit (and verify sNaN preservation if covered).

Verified locally on Windows 11 x64 (RTX 5090 + WARP + dxil.dll, 7d34d385):

  • cargo run --locked -p xtask -- policy: passed (191 files ok)
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings (default and --all-features): clean
  • emit::lower::tests: 33 passed, 0 failed
  • cube_ -- --test-threads=1 (RTX 5090 and VICTORY_WARP=1): 3 passed, 0 failed on both (cube_gradients_pick_the_level, cube_faces_match_the_hardware, cube_zero_and_denormals_match_rdna2_oracle)

@passerbyyhx

Copy link
Copy Markdown

I ran the requested hardware probes on 7d34d385 on an RTX 4090 Laptop (native adapter verified by LUID). All seven RDNA2 reference rows passed. In the larger probe, CUBESC differed from the model on 336 of 14,440 inputs, all on the negative Z face with a nonzero subnormal X. The other three cube instructions matched on every input.

I opened Moon0755/VIctory#1 against your branch. Selecting the coordinate and sign mask separately eliminated all 336 mismatches. A second commit covers the no-multiplier CLAMP case you called out. Before it, both adapters flushed positive subnormal results on two of five measured SC rows and one TC row. All five cube tests now pass on both the RTX 4090 Laptop and WARP. The PR includes the reproducer and the three unrelated shader-suite failures that also reproduce on your base.

Shaders: preserve subnormal cube coordinates on negative Z and through clamp
@Moon0755

Copy link
Copy Markdown
Author

Thanks, this is the hardware run the PR was missing. I read Moon0755#1 and ran it through CI on the fork (run 45798031432, WARP only): 305 passed, 0 failed, the five cube tests among them. The five clamp rows match the published table. It is merged into this branch as 8731510, so the commits keep your authorship.

— Fable

@Moon0755

Copy link
Copy Markdown
Author

On the review above: agreed on both points. Source abs/neg on these ops will be applied to the bits on the SC/TC path, with a test on the modifier columns of the same published table, and the oracle test will compare bit for bit. I'll push it here after a CI run on the fork.

The branch now also carries Moon0755#1 (the negative-Z select and the clamp case, found on an RTX 4090), so the head is 8731510.

— Fable

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the updated head (87315104): the separate coordinate/sign selection on v_cubesc_f32 and the IULT(bits, 0x0080_0000) positive-subnormal clamp passthrough pass all five cube tests on both RTX 5090 and WARP, and emit/lower.rs stays under the 512 KiB limit (522,774 bytes).

Keeping Request Changes open for the two follow-up items noted in the thread:

  1. Apply VOP3 source ABS/NEG modifiers as bit operations on the v_cubesc_f32/v_cubetc_f32 raw-bit path so fsrc (Bin::Sub(-0.0, f) / FAbs) cannot flush subnormals before selection, with a modified-subnormal test case.
  2. Switch cube_zero_and_denormals_match_rdna2_oracle S/T checks from F32 to bit-exact INT assertions.

Verified locally on Windows 11 x64 (RTX 5090 + WARP + dxil.dll, 87315104):

  • cargo run --locked -p xtask -- policy: passed (191 files ok)
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings (default and --all-features): clean
  • emit::lower::tests: 33 passed, 0 failed
  • cube -- --test-threads=1 (RTX 5090 and VICTORY_WARP=1): 5 passed, 0 failed on both

@Moon0755

Copy link
Copy Markdown
Author

Both points from the review are in, head c09afc9.

  • Source abs/neg on v_cubesc_f32 / v_cubetc_f32 now act on the operand bits (AND with 0x7FFF_FFFF, then XOR with 0x8000_0000) on the raw-bit path. The axis and sign decisions are unchanged.
  • New test cube_srcmod_bit_paths_match_rdna2_oracle: rows 6, 7, 8 and 11 of the published table, columns 9 (|x|, -y, -z) and 10 (-x, -y, |z|), compared bit for bit.
  • cube_zero_and_denormals_match_rdna2_oracle compares S/T bit for bit as well; all rows pass, the NaN rows included.

CI on the fork, WARP only: without the change the new test fails (run 45800496753; WARP itself flushes the subnormal in the float abs/neg, so the mechanism shows there too), with it 306 pass (run 45805387961). Not run on a hardware adapter. lower.rs is 523,730 bytes.

— Devin & Fable

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified c09afc9a on RTX 5090 and WARP: both previous items are cleanly resolved — VOP3 source abs/neg are now applied bitwise on the raw SC/TC path, and cube_zero_and_denormals_match_rdna2_oracle uses bit-exact INT assertions for SC/TC.

Verified locally on Windows 11 x64 (RTX 5090 + WARP + dxil.dll, c09afc9a):

  • cargo run --locked -p xtask -- policy (191 files ok; emit/lower.rs = 523,730 bytes, 558 bytes below the 524,288-byte cap) and cargo clippy --workspace --all-targets -- -D warnings (default and --all-features): clean.
  • emit::lower::tests: 33 passed, 0 failed.
  • RTX 5090 and VICTORY_WARP=1 (--test-threads=1): all 6 cube tests passed and emit::semantics_tests 38 passed, 0 failed (1 ignored).

Two minor non-blocking nits:

  1. In cube_srcmod_bit_paths_match_rdna2_oracle (semantics_tests.rs:1514-1558), none of the 4 cases currently select a negative subnormal with abs enabled along the raw SC/TC path (sc_mods puts abs on x, which is only selected in row 4 where x is already +0x0000_0007; tc_mods puts abs on z, which is never selected because y is never major). Adding a row that selects a negative subnormal under abs (e.g., SC(|-denorm|, 0, +1) or TC(0, +1, |-denorm|)) would also lock down raw abs.
  2. rustfmt --edition 2024 --check flags formatting on newly added lines 1485, 1497, and 1513 in semantics_tests.rs.

@Moon0755

Copy link
Copy Markdown
Author

Both nits are in, head de77244, tests only.

  • A measured row for the case you named: column 13 of the published table, v_cubesc_f32 v23, v4, -v5, |v6| clamp, rows 6, 8 and 11. Row 8 selects a negative subnormal z under abs through the negative x axis; all three give 0x00000007. New test clamped_cube_abs_of_negative_denormal_matches_rdna2_oracle, bit for bit.
  • The three lines rustfmt flagged are formatted from the cargo fmt --check output in CI. Two older hunks in that file (now at 1931 and 3450) are not from this PR and are left alone.

CI on the fork, WARP only: 307 passed (run 45812258458).

— Devin & Fable

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on RTX 5090 & WARP against origin/main (63bcca4):

  • cargo test -p victory-shader --lib cube -- --test-threads=1: 7/7 passed on RTX 5090 and WARP (all 5 new cube denormal/zero/srcmod/clamp tests pass).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly preserves denormal coordinates in v_cubesc/v_cubetc_f32 and returns +0.0 for v_cubema_f32 of zero.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

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.

5 participants