GPU: round depth-twin UNORM16 copies at the exact midpoint #3062

Open
andrewpozdnakov7-jpg wants to merge 1 commit from upstream-pr/3064 into main
andrewpozdnakov7-jpg commented 2026-11-26 05:58:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T05:58:43Z.


The depth-twin converter computes the nearest 16-bit UNORM code for a float depth. The intermediate value * 65535 + 0.5 can round up to an integer before the cast. For example, float bits 0x37000080 are just below the first midpoint and yield 1 instead of 0.

Scale by the exact power of two 65536 and split off the fractional part. Since 65535 * value = 65536 * value - value, the fraction and value tell us whether to adjust the integer part by one. The two half-step comparisons avoid rounding a boundary into the next code. The converter continues to compile as cs_5_1.

The same test executes the production converter on Windows app-local D3D12 WARP on main 63bcca43 and with the patch. It uses five adjacent floats around each of the 65,535 thresholds, all 65,536 widened UNORM codes, and five clamp controls. 32,768 of 393,216 mismatched on main, zero with the patch. The full GPU suite passed 247 tests with one ignored. Strict GPU Clippy passed with default and all features. Asset policy passed.

#1964 and #2969 independently change the adjacent HLSL and produce text conflicts in pairwise combinations. This branch is based directly on current main. If one lands first, the overlap is in the u16 converter and its neighboring functions or attributes.

Coordination claim

Please retain the Git author when reapplying this change. If you fold it into another commit, please add Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5504864003 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3064). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-26T05:58:43Z. --- The depth-twin converter computes the nearest 16-bit UNORM code for a float depth. The intermediate `value * 65535 + 0.5` can round up to an integer before the cast. For example, float bits `0x37000080` are just below the first midpoint and yield 1 instead of 0. Scale by the exact power of two 65536 and split off the fractional part. Since `65535 * value = 65536 * value - value`, the fraction and value tell us whether to adjust the integer part by one. The two half-step comparisons avoid rounding a boundary into the next code. The converter continues to compile as `cs_5_1`. The same test executes the production converter on Windows app-local D3D12 WARP on `main` `63bcca43` and with the patch. It uses five adjacent floats around each of the 65,535 thresholds, all 65,536 widened UNORM codes, and five clamp controls. **32,768 of 393,216 mismatched on main, zero with the patch.** The full GPU suite passed 247 tests with one ignored. Strict GPU Clippy passed with default and all features. Asset policy passed. #1964 and #2969 independently change the adjacent HLSL and produce text conflicts in pairwise combinations. This branch is based directly on current `main`. If one lands first, the overlap is in the `u16` converter and its neighboring functions or attributes. [Coordination claim](https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7182677309) Please retain the Git author when reapplying this change. If you fold it into another commit, please add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`.
Dukestar69 commented 2026-11-26 06:08:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T06:08:06Z.

Review: APPROVED


Tested locally merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP):

  • Diff review: Computing 65535 * value as scaled - value (scaled = value * 65536.0) and comparing precise half-step differences on frac(scaled) cleanly avoids float32 rounding error at UNORM16 midpoints.
  • cargo test -p victory-gpu twin::tests -- --test-threads=1 (RTX 5090 & WARP): 3 passed, 0 failed on both (including float_depth_rounds_at_each_u16_threshold verifying all 393,216 threshold/round-trip inputs with 0 mismatches).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6069303834 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3064#pullrequestreview-6069303834). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T06:08:06Z. **Review:** APPROVED --- Tested locally merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP): - Diff review: Computing `65535 * value` as `scaled - value` (`scaled = value * 65536.0`) and comparing `precise` half-step differences on `frac(scaled)` cleanly avoids float32 rounding error at UNORM16 midpoints. - `cargo test -p victory-gpu twin::tests -- --test-threads=1` (RTX 5090 & WARP): 3 passed, 0 failed on both (including `float_depth_rounds_at_each_u16_threshold` verifying all 393,216 threshold/round-trip inputs with 0 mismatches). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
fuzeofficlal commented 2026-11-26 06:09:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T06:09:11Z.

Review: APPROVED


Reviewed head f3c78f31 line-by-line (CI green):

  • The arithmetic is exact across [0, 1]: scaled = value * 65536.0 is an exact exponent shift, I = (uint)scaled and fraction = frac(scaled) are exact, and 65535 * value + 0.5 = I + (fraction - value + 0.5) with fraction - value + 0.5 in [-0.5, 1.5).
  • Marking fraction_minus_half = fraction - 0.5 and value_minus_half = value - 0.5 precise prevents contraction/reassociation; by Sterbenz's lemma both subtractions are exact on [0.25, 1] (and < -0.25 below 0.25, where the corresponding comparison is unambiguously false), handling 0.0, 1.0 (65536 - 1 = 65535), ±Inf, and NaN (saturate(NaN) = 0) cleanly.
  • float_depth_rounds_at_each_u16_threshold exhaustively tests 5 adjacent f32 ULPs around every single one of the 65,535 UNORM16 midpoints plus every round-tripped code and edge cases on GPU.

Clean and mathematically sound.

<!-- greyfinch-sync:Marvisak/VIctory:review:6069314383 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3064#pullrequestreview-6069314383). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T06:09:11Z. **Review:** APPROVED --- Reviewed head `f3c78f31` line-by-line (CI green): - The arithmetic is exact across `[0, 1]`: `scaled = value * 65536.0` is an exact exponent shift, `I = (uint)scaled` and `fraction = frac(scaled)` are exact, and `65535 * value + 0.5 = I + (fraction - value + 0.5)` with `fraction - value + 0.5` in `[-0.5, 1.5)`. - Marking `fraction_minus_half = fraction - 0.5` and `value_minus_half = value - 0.5` `precise` prevents contraction/reassociation; by Sterbenz's lemma both subtractions are exact on `[0.25, 1]` (and `< -0.25` below `0.25`, where the corresponding comparison is unambiguously false), handling `0.0`, `1.0` (`65536 - 1 = 65535`), `±Inf`, and `NaN` (`saturate(NaN) = 0`) cleanly. - `float_depth_rounds_at_each_u16_threshold` exhaustively tests 5 adjacent f32 ULPs around every single one of the 65,535 UNORM16 midpoints plus every round-tripped code and edge cases on GPU. Clean and mathematically sound.
kotekompotek commented 2026-11-26 06:34:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T06:34:06Z.

Review: APPROVED


Verified the math: scaled = v65536 is exact (power-of-two multiply), then round(65535v) = I + round(f-v) where f=frac(scaled) — the two comparisons implement round-half-up on f-v ∈ (-1,1) with correct tie behavior at both boundaries, and the precise qualifiers stop fusing that would reintroduce the rounding error. Exhaustive test: every midpoint neighborhood (5 adjacent f32s each), all 65536 codes, NaN/±Inf. Reference is f64 round-half-up.

<!-- greyfinch-sync:Marvisak/VIctory:review:6069549486 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3064#pullrequestreview-6069549486). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-26T06:34:06Z. **Review:** APPROVED --- Verified the math: scaled = v*65536 is exact (power-of-two multiply), then round(65535*v) = I + round(f-v) where f=frac(scaled) — the two comparisons implement round-half-up on f-v ∈ (-1,1) with correct tie behavior at both boundaries, and the precise qualifiers stop fusing that would reintroduce the rounding error. Exhaustive test: every midpoint neighborhood (5 adjacent f32s each), all 65536 codes, NaN/±Inf. Reference is f64 round-half-up.
Marvisak commented 2026-11-26 07:01:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T06:17:45Z.


Automated triage for the maintainers.

Merge candidate. The depth-twin converter's u16 (the HLSL SOURCE in crates/victory-gpu/src/twin.rs) is documented to give "a float back the nearest u". But value * 65535.0 + 0.5 rounds in f32, so values a few ULPs below a midpoint go up one code. The PR computes 65535·v = 65536·v − v with an exact power-of-two scale, and adjusts the integer part from the fraction. I checked the algebra: the adjustment is +1 iff frac − 0.5 ≥ v, −1 iff frac < v − 0.5. Both subtractions are exact, or keep the right sign, over the ranges where they decide the result, and v = 1 and NaN (saturated to 0) come out right. The new test compares against f64 rounding at every midpoint, plus the widened codes the forward conversion produces.

Notes: the gain is one code for depths within a few ULPs of a midpoint. The description says it conflicts textually with #1964 and #2969 in the same HLSL, so whichever lands second needs a rebase.

CI: check passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7184288312 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3064#issuecomment-7184288312). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T06:17:45Z. --- <!-- victory-triage v1 head=f3c78f3126aa69c7ede6d1bed2236cdfc3a970a4 verdict=merge-candidate at=2026-11-26T06:20:10Z --> Automated triage for the maintainers. **Merge candidate.** The depth-twin converter's `u16` (the HLSL `SOURCE` in `crates/victory-gpu/src/twin.rs`) is documented to give "a float back the nearest `u`". But `value * 65535.0 + 0.5` rounds in f32, so values a few ULPs below a midpoint go up one code. The PR computes `65535·v = 65536·v − v` with an exact power-of-two scale, and adjusts the integer part from the fraction. I checked the algebra: the adjustment is +1 iff `frac − 0.5 ≥ v`, −1 iff `frac < v − 0.5`. Both subtractions are exact, or keep the right sign, over the ranges where they decide the result, and `v = 1` and NaN (saturated to 0) come out right. The new test compares against f64 rounding at every midpoint, plus the widened codes the forward conversion produces. Notes: the gain is one code for depths within a few ULPs of a midpoint. The description says it conflicts textually with #1964 and #2969 in the same HLSL, so whichever lands second needs a rebase. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
oni-swr commented 2026-11-26 11:03:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-26T10:16:20Z.


Independent Windows WARP verification of f3c78f31 against its exact parent, current main (63bcca43). Coordination claim: https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7184310773. I confirmed Microsoft Basic Render Driver (DXGI software adapter, d3d10warp.dll 10.0.26100.9818) with VICTORY_D3D12_WARP=1 and diagnostics enabled. No physical GPU was used.

  • A synthetic UNORM16 conversion oracle checked the depth-twin u16 conversion. On the parent it found 32,768 mismatches. On the PR head it found 0.

AI-assisted with Claude Code (claude-sonnet-5-5), continuing a session started with Codex (gpt-6.2-sol). Synthetic data only.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7189140695 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3064#issuecomment-7189140695). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-26T10:16:20Z. --- <!-- oni-swr warp verification f3c78f3126aa69c7ede6d1bed2236cdfc3a970a4 --> Independent Windows WARP verification of `f3c78f31` against its exact parent, current `main` (`63bcca43`). Coordination claim: https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7184310773. I confirmed Microsoft Basic Render Driver (DXGI software adapter, `d3d10warp.dll` 10.0.26100.9818) with `VICTORY_D3D12_WARP=1` and diagnostics enabled. No physical GPU was used. - A synthetic UNORM16 conversion oracle checked the depth-twin `u16` conversion. On the parent it found 32,768 mismatches. On the PR head it found 0. AI-assisted with Claude Code (claude-sonnet-5-5), continuing a session started with Codex (gpt-6.2-sol). Synthetic data only.
neonbyte1 commented 2026-11-28 10:53:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by neonbyte1 on 2026-11-28T10:53:27Z.


A merge note for @andrewpozdnakov7-jpg and @arogan178 (#2919): these two PRs conflict without git noticing. #2919 removes both #define RS and [RootSignature(RS)] from the depth converter, because Wine's compiler builds it and the code makes the root signature beside the shader. #3064 keeps the [RootSignature(RS)] line. When both are merged, the attribute stays with no define. Wine's D3DCompile (vkd3d-shader) then rejects the converter with E5005: Identifier "RS" is not declared, and no depth converter exists: the log says no depth conversion: 16-bit depth written through unordered views does not reach the depth texture. In game on my fork's integration (RTX 5090, Wine 11.22 + vkd3d-proton), character close-ups get soft, skin-coloured blocks over the hair and back, and the PR's own float_depth_rounds_at_each_u16_threshold fails with depth converter.

The fix is to drop the attribute in whichever PR lands second and keep #3064's u16() as is: github.com/neonbyte1/VIctory@634bcce5 (adds a CPU test that the converter source compiles without it). With that on the same tree:

  • float_depth_rounds_at_each_u16_threshold passes under Wine/vkd3d-shader, with 0 of 393 216 values misrounded. The previous (x * 65535 + 0.5) rounding misrounds 32 768 of them, the first at 0x37000080 (expected 0, got 1). So #3064's rounding is worth having on this compiler too.
  • In game, the blocks are gone at frames 1700 and 2048.

One unrelated note: depth_typed_and_default_uavs_write_through_the_twin fails under Wine with 0x80004005 both with and without these changes.

AI-assisted (Claude); measured on my machine, no game data shared.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7238188638 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3064#issuecomment-7238188638). Originally posted by [neonbyte1](https://github.com/neonbyte1) on 2026-11-28T10:53:27Z. --- A merge note for @andrewpozdnakov7-jpg and @arogan178 (#2919): these two PRs conflict without git noticing. #2919 removes both `#define RS` and `[RootSignature(RS)]` from the depth converter, because Wine's compiler builds it and the code makes the root signature beside the shader. #3064 keeps the `[RootSignature(RS)]` line. When both are merged, the attribute stays with no define. Wine's `D3DCompile` (vkd3d-shader) then rejects the converter with `E5005: Identifier "RS" is not declared`, and no depth converter exists: the log says `no depth conversion: 16-bit depth written through unordered views does not reach the depth texture`. In game on my fork's integration (RTX 5090, Wine 11.22 + vkd3d-proton), character close-ups get soft, skin-coloured blocks over the hair and back, and the PR's own `float_depth_rounds_at_each_u16_threshold` fails with `depth converter`. The fix is to drop the attribute in whichever PR lands second and keep #3064's `u16()` as is: https://github.com/neonbyte1/VIctory/commit/634bcce5 (adds a CPU test that the converter source compiles without it). With that on the same tree: - `float_depth_rounds_at_each_u16_threshold` passes under Wine/vkd3d-shader, with **0 of 393 216** values misrounded. The previous `(x * 65535 + 0.5)` rounding misrounds 32 768 of them, the first at `0x37000080` (expected 0, got 1). So #3064's rounding is worth having on this compiler too. - In game, the blocks are gone at frames 1700 and 2048. One unrelated note: `depth_typed_and_default_uavs_write_through_the_twin` fails under Wine with 0x80004005 both with and without these changes. _AI-assisted (Claude); measured on my machine, no game data shared._
andrewpozdnakov7-jpg commented 2026-11-28 11:15:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T11:15:17Z.


#2919 now includes this rounding change at da0eca1, with the Git author preserved and the converter's root signature built separately. I verified all 393,216 inputs and the odd-width round trip on Windows WARP. The next step is maintainer review of the combined head. Exact-head verification

AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7238428989 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3064#issuecomment-7238428989). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-28T11:15:17Z. --- #2919 now includes this rounding change at `da0eca1`, with the Git author preserved and the converter's root signature built separately. I verified all 393,216 inputs and the odd-width round trip on Windows WARP. The next step is maintainer review of the combined head. [Exact-head verification](https://github.com/Marvisak/VIctory/pull/2919#issuecomment-7238428592) AI-assisted with OpenAI Codex.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3064:upstream-pr/3064'
git switch 'upstream-pr/3064'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3064'
git switch 'upstream-pr/3064'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3064'
git switch 'upstream-pr/3064'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3064'
git switch 'main'
git merge --squash 'upstream-pr/3064'
git switch 'main'
git merge --ff-only 'upstream-pr/3064'
git switch 'main'
git merge 'upstream-pr/3064'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!3062
No description provided.