Skip to content

gpu(primitive): preserve NaN and use ties-to-even in FP16 rect expansion - #337

Open
Caasares wants to merge 1 commit into
fxceless:mainfrom
Caasares:fix/gpu-rect-fp16-nan-and-ties
Open

Caasares wants to merge 1 commit into
fxceless:mainfrom
Caasares:fix/gpu-rect-fp16-nan-and-ties

Conversation

@Caasares

Copy link
Copy Markdown

Fixes #245

Summary

Fixes two IEEE-754 FP16 conversion bugs in the GPU rectangle list CPU fallback (f32_to_half in crates/victory-gpu/src/primitive.rs):

  • Root cause:
    1. exp >= 31 caught raw_exp == 0xff inputs indiscriminately, turning NaN values into ±inf (sign | 0x7c00).
    2. Normal values used (man + 0x1000) >> 13, which always rounds exact halfway ties up instead of rounding ties to even (unlike the subnormal branch).
  • Fix:
    1. Check raw_exp == 0xff before normal/overflow handling: when man != 0, return a sign-preserving quiet NaN with a guaranteed nonzero mantissa (sign | 0x7e00 | ((man >> 13) & 0x03ff)); when man == 0, return sign | 0x7c00.
    2. For normal values, resolve ties using the retained mantissa LSB parity ((man >> 13) & 1) ((man + 0x0fff + ((man >> 13) & 1)) >> 13), preserving carry-out into the exponent and overflow to sign | 0x7c00.
  • Tests:
    • Added R16_FLOAT expand_rects regression tests for both #245 cases (normal even tie 1.0, 0.0, 2^-11 -> 0x3c00 and quiet NaN 0x7e00 / 0xfe00 preservation) and verified they failed before the fix and pass after.
    • Added direct f32_to_half edge-case tests covering odd tie rounding up (1.0 + 2^-10 + 2^-11 -> 0x3c02), non-ties, ±0.0, ±inf, finite overflow to ±inf, quiet/low-payload NaNs, and subnormal round-to-even ties.

Verification

  • rustfmt --edition 2024 --check crates/victory-gpu/src/primitive.rs — passed
  • cargo clippy --workspace --all-targets --locked -- -D warnings — passed (0 warnings)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings — passed (0 warnings)
  • cargo test --workspace --locked — 499 passed; 0 failed; 7 ignored across the workspace (victory-gpu: 113 passed; 0 failed; 1 ignored; xtask including repository_passes_policy: 4 passed; 0 failed)

Note: AI-assisted fix; contains no game files, keys, or assets.

@kaayzouee

Copy link
Copy Markdown

Duplicate of #315?

KomanderDG88 pushed a commit to KomanderDG88/VIniVIdiVIci that referenced this pull request Nov 26, 2026
…ICTORY_MERGE_SUBMISSIONS, off by default)

The thread that submits for the title pays fixed work per host submission
(the placed-heap descriptor pass, the write-watch asking, the pass over
copies in the card's memory, the refresh threads' joins, list close and
reset, a send and a signal, a flight for the relay), about 59 times a frame.
With VICTORY_MERGE_SUBMISSIONS=1 a title submission sent ahead
(VICTORY_LATE_WAIT / VICTORY_RELAY) is held back and joins the next ones; they
go to the card as one host submission, in the title's order, with a barrier
(UAV and aliasing barriers on no resource) in front of each submission after
the first.

They are sent at: a fence signal on any queue, a wait for a memory value
that a held submission may write (one met by a held bottom-of-pipe write
lets the part behind it join), a present, a synchronous submission of ours,
a settle, page mappings on a queue, a next submission that reads what held
shaders or flights in the air write, copies over what a held one brings to
the card, or runs code of ours on title memory, 256 lists or 32
submissions, and 500 us after the first was held (a thread of its own).
Counters per frame on the frame line: title submissions, host submissions,
sends by reason. Measured in AnyXboxX: preparation 48-52 ms a frame, 40-46
with =1.

=2 (fence signals carried behind the held submissions, set when they have
run) lost the device in AnyXboxX's runs: here it runs as =1, said in the
log, unless VICTORY_MERGE_SUBMISSIONS_UNSAFE_CARRY=1 asks for it too.

Off (the default) every path is as before. The decision logic is in
merge.rs with unit tests; two WARP tests cover the step summary and the
barrier prelude.

Port: from LooperSalty/AnyXboxX PR fxceless#337 (in fxceless#358), commit
f98cbc09f8e9c4016e4643df7b00ee0e814e4c18 with its docs follow-up
528b652e0f573f49e3196acb202e953f0bbfc020 folded in; applied by hand, as
the trees differ (different layout and history). merge.rs is the
original's but for the =2 gate and two field docs. The host side
(Merging, touches_of, merge_send/signal/fence_asked/wait, the timer
thread, join_barriers, Host::merge_offer/learn/send_held, the WARP tests)
is in a new module host/merging.rs instead of host.rs, which is near the
512 KiB limit; host.rs keeps the hooks (execute_late split into
send_late, submit/prelude take the joins, settle_now, execute,
execute_own, present, the frame line). Adapted to this tree's steps:
every fill counts as a read (blocked_by looks at constants too), a
Step::Host's title range counts as a read, Step::Run(..) as code, and
texture and buffer read-backs as what the shaders write; joins move past
the heap worker's lists sent in front. Also hooked: the signals of
joined parts (VICTORY_JOIN_PARTS) and Host::present (presents behind the
line, VICTORY_ASYNC_PRESENT). The original's fix of its own merge
(5ef1beca) is not needed here.

Port: onto the stack where `impl HostList` lives in host/host_list.rs;
HostList::prelude's hunks (the `join` argument, the prelude made when
joining, the join barriers first) moved there, the rest of host.rs as
in the original.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

GPU rectangle CPU fallback: FP16 conversion turns NaN into infinity and rounds ties up

2 participants