Skip to content

GPU: a half exactly halfway between two rounds to the even one, not up - #1220

Closed
BastienRodz wants to merge 1 commit into
Marvisak:mainfrom
BastienRodz:split/346-half-ties-even
Closed

BastienRodz wants to merge 1 commit into
Marvisak:mainfrom
BastienRodz:split/346-half-ties-even

Conversation

@BastienRodz

Copy link
Copy Markdown
Contributor

What's wrong

f32_to_half (crates/victory-gpu/src/primitive.rs), which packs the fourth corner of a rectangle-list draw into a half-float vertex attribute, rounds by adding 0x1000 before dropping the low 13 mantissa bits:

sign | (half + ((man + 0x1000) >> 13)) as u16

So a value exactly halfway between two halves always rounds up. For example, 1 + 2^-11 packs as 0x3c01 where round-to-nearest-even (IEEE 754, and the GPU's own conversion) gives 0x3c00. The corner computed on such a value lands one ulp off the other three.

@fuzeofficlal measured it in native draws on an RTX 5090 and on WARP: 10 of 16 reference pixels matched before, 16 of 16 after. They wrote the fix (#346 comment, their c256b97).

What the fix does

The retained bit decides halfway cases: (man + 0x0fff + retained) >> 13, where retained is bit 13 of the mantissa. A carry out of the mantissa still goes into the exponent, as before. The subnormal branch above already rounded halfway values to even and is unchanged.

Tests

primitive::tests::half_midpoints_round_to_even:

  • 1 + 2^-11 gives 0x3c00 (even);
  • 1 + 3·2^-11 gives 0x3c02 (even);
  • a value just past a midpoint still rounds up, to 0x3c01.

On main, the first assertion fails with left: 15361 (0x3c01), right: 15360 (0x3c00).

On GitHub's windows-latest with the upstream CI's commands (clippy -D warnings with and without --all-features, then cargo test --workspace --locked), everything passes; victory-gpu 51 passed, 0 failed: https://github.com/BastienRodz/VIctory/actions/runs/45437173565

Related

This is the CPU-path half of the rounding fix carried in #346, split out so it can land on its own. #346 keeps the GPU expander's copy of the same rounding. @SkillIssueXO checked the pair on an RTX 5070 Ti there: with only the old rounding put back, the byte-for-byte CPU/GPU test fails.

No game data used. AI-assisted: fix by @fuzeofficlal, split out and verified with Claude Code.

🤖 Generated with Claude Code

@Aniseto

Aniseto commented Nov 24, 2026

Copy link
Copy Markdown

Ran the new rounding test at 71bd075:

Environment: Windows 11 Pro build 26200 x64, Intel Core i5-12400F, 64 GB RAM, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only) driver 32.0.16.2124, Rust 1.98.1 (pinned by rust-toolchain.toml), MSVC 14.44.35207, DXC 1.9.2609.5, debug build.

cargo test -p victory-gpu half_midpoints
1 passed; 0 failed; 0 ignored      (exit 0)

cargo test -p victory-gpu primitive
5 passed; 0 failed; 0 ignored      (exit 0)

primitive::tests::half_midpoints_round_to_even passes and the rest of the module is unaffected. It is a unit-level test, so the adapter is not the variable; the datapoint is the pinned 1.98.1 toolchain. No test report on this PR yet.

Not tested: a title whose rasterizer actually hits the midpoint case (no game data here), RDNA2/RDNA4 and Intel GPUs.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash 1017 - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; no game data was used.

f32_to_half adds 0x1000 before dropping the low 13 mantissa bits, so a
value exactly halfway between two halves always rounds up: 1 + 2^-11
packs as 0x3c01 instead of 0x3c00. A rectangle's fourth corner computed
on such a value gets a half one ulp off. fuzeofficlal measured it in
native draws of the direct path on RTX 5090 and WARP (10 of 16 reference
pixels before, 16 of 16 after) and wrote the fix: the retained bit
decides halfway cases.

Co-Authored-By: fuzeofficlal <fuzeofficlal@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@BastienRodz
BastienRodz force-pushed the split/346-half-ties-even branch from 71bd075 to b4e4647 Compare November 24, 2026 16:47
@shortselow-byte

Copy link
Copy Markdown

I checked f32_to_half at head b4e4647 (same result at the earlier 71bd075) on every f32 bit pattern (2^32 values) against an independent reference (exact f64 arithmetic with round_ties_even):

@mercuryVM mercuryVM 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.

Tested b4e4647 on Windows 11, RTX 5070, MSVC:

  • cargo test -p victory-gpu on the head: the primitive tests pass, including the new midpoint test.
  • In one full run, frame::tests::waits_end_on_ticks_and_count_frames failed. That test is timing-based and this PR doesn't touch it, and it passed 3 out of 3 when rerun alone.
  • Merged into our fork: workspace tests, shader tests and clippy are all clean.

The rounding is right for normal halves. With r the retained bit, +0x0fff + r carries out of the low 13 bits exactly when they are above 0x1000, or equal to it with r = 1. That is round-half-to-even, and a carry into the exponent still saturates at 0x7c00 as before.

AI-assisted (Claude Code).

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
…two rounds to the even one, not up

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

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at b4e4647: cargo test -p victory-gpu passes, 51 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…itives, capture), verified together

Merges #124 #119 #515 #934 #484 #143 #1191 #278 #553 #153 #270 #1158 #1204
#1220 #488 #1146 #631 onto main (ffe310e), each head unchanged, no conflicts,
no changes of mine. cargo clippy --workspace --all-targets --locked -- -D
warnings, with and without --all-features, is clean and cargo test
--workspace --locked passes (372 passed, 8 ignored; main has 342 passed,
6 ignored).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
KainanP added a commit to KainanP/VIctory that referenced this pull request Nov 24, 2026
…) onto Marvisak#334 + integ/next

Taken instead of Marvisak#282 (same draws, Marvisak#255; only one can land): Marvisak#346 expands on
the GPU after the clamp, so vertex data the GPU wrote earlier in the
submission is seen, and its record grouping starts at each record's first
vertex. It has the stronger title evidence: the loading screen renders
(justlovett0, RTX 3080), the `prepare failed`/topology-17 drops go from 256
per frame to 0 with no device removal (neonbyte1, RTX 5090/Wine; Exorion1er,
RX 9070 XT/Proton; iurysf, RTX 4070), and every defect reported on it was
fixed by a later commit (scratch layout cee702e, record bounds 0aebe86,
count buffer 7ff1184, Wine compiler c256f83). Marvisak#282 expands on the CPU (does
not see GPU-written vertices, copies bound buffers whole, three times per
draw after 4d3d003) and its last head was only checked in short runs.

Conflicts:
1. crates/victory-gpu/src/indirect.rs, module doc: the stack's (Wine-safe
   clamp in structured buffers, root signature made beside it) kept, with
   Marvisak#346's sentence on the rectangle expansion added.
2. indirect.rs, `bound`: Marvisak#346 splits it into `clamp_uav` (clamp + UAV
   barrier) and `bound` (then a fixed UAV -> INDIRECT_ARGUMENT transition);
   Marvisak#334 (merged just before) replaced the fixed transitions with its buffer
   states (`bound` asks for BUFFER_WRITE, `execute` for BUFFER_READ). Kept
   Marvisak#346's split with Marvisak#334's tracking, the resolution justlovett0 and the
   author agreed on (Marvisak#346 comments 7121242441, 7121274527): `clamp_uav` takes
   the buffer, starts a command and asks for BUFFER_WRITE; `bound` is
   `clamp_uav` (the read state comes from `execute`), no fixed transition.
3. crates/victory-gpu/src/host.rs: the stack's `PooledUpload` and Marvisak#346's
   `RectScratch` added at the same place; both kept.

Non-textual fixes:
- rectangles.rs calls `clamp_uav(list, &args.buffer, ..)` (new parameter).
- rectangles.rs: the rewrite and expand passes bind the count's and the
  vertex buffers' host copies as root UAVs. With Marvisak#334 those copies' states
  are tracked, so they now ask for BUFFER_WRITE in the clamp's command
  (otherwise a copy a draw had moved to GENERIC_READ would be bound as a
  UAV in a read state). The draw's own `prepare` then asks for the read
  state as before. The list-owned scratch keeps Marvisak#346's own barriers.
- primitive.rs: Marvisak#346's half-float rounding commit (8c7ac43) is already here
  through Marvisak#1220 (in Marvisak#1418), so `half_midpoints_round_to_even` is not added
  twice: the PR adds 8 tests, 7 are new here.
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
nazmi added a commit to nazmi/VIctory that referenced this pull request Nov 24, 2026
Review workers over ~45 more PRs against the merged upstream tree.

Integrated: Marvisak#1394 (antisymmetric XUserCompare), Marvisak#1390 (A8_UNORM is not in
the R8 typeless family), Marvisak#1499 (deep 3D mips stay out of the thick tail,
plus a regression test the PR lacked), Marvisak#1366 (mesh stage I/O does not parse
VS inputs), Marvisak#1594 (the title's exit status reaches the shell), Marvisak#1299 (docs:
validator prerequisite), Marvisak#1515 (dialog commands before init), Marvisak#1424 (empty
submissions skip the lock), Marvisak#1144 (shader differential skips are reported),
Marvisak#1129 (64 MB pipeline-worker stacks), Marvisak#1564/Marvisak#1565/Marvisak#1566 (MakeResident,
Evict and SetStablePowerState succeed as the console's do), Marvisak#1250 (a zero
element count reads to the end of the buffer), Marvisak#1628 (texture-creation
failure logs its states).

Re-applied after the upstream merge dropped them: f32_to_half NaN and
ties-to-even (Marvisak#843), unique-temp library writes (Marvisak#1660),
VICTORY_PIPELINE_WORKERS (Marvisak#1728), VICTORY_MODE (Marvisak#1701).

Removed: the orphaned depth-bounds test file, whose implementation
(9a6d9e6) the merge dropped and which no module compiled.

Refused: Marvisak#1099 (writing 0 for an unpaired p1 interpolation computes wrong
values; two reviewers upstream and one here reached the same conclusion).
Duplicates: Marvisak#1217, Marvisak#529, Marvisak#531, Marvisak#590, Marvisak#1102, Marvisak#398, Marvisak#1150, Marvisak#1220.

Gate at efda2c5: fmt 0 differing, both Clippy gates 0 errors, policy 219
files, workspace 674 passed / 0 failed with the 10 documented pre-existing
GPU skips.
@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because another open pull request covers the same change.

See #1732. The same round-half-to-even formula as #1732, which also fixes NaN and has the fuller test (every half round-trips).

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
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.

6 participants