Skip to content

runtime: Depth24 CPU conversions independent of the thread's rounding mode (RT-D24-CPU-ROUNDEVEN) - #1858

Open
thiefyzheng wants to merge 1 commit into
ProjectLeonida:mainfrom
thiefyzheng:fix/d24-cpu-roundeven
Open

thiefyzheng wants to merge 1 commit into
ProjectLeonida:mainfrom
thiefyzheng:fix/d24-cpu-roundeven

Conversation

@thiefyzheng

Copy link
Copy Markdown
Contributor

RT-D24-CPU-ROUNDEVEN

Claimed on #1608 (#1608 (comment)). Fixes @cheltyi's finding (#1608 (comment)): runtime/src/depth24.h's CPU-side Depth24 conversion followed the calling thread's rounding mode (float multiply + std::nearbyint), while depth24_convert.comp is roundEven of a correctly rounded product — so a thread with directed rounding (FE_DOWNWARD/FE_TOWARDZERO) quantized 0x1p-23f to 1 instead of 2 (~1 integer ULP off, beyond the documented 0.6 ULP tolerance), letting CPU clears (vulkan_replay.h depth24_clear_value) disagree with GPU uploads/copies. depth24_to_float had the same environment dependence.

What

  • depth24_detail::round_even: floor/fmod-based ties-to-even, exact in every rounding mode — no fesetround, no fenv dependence, holds under -ffast-math.
  • depth24_quantize: exact double(value) * 16777215.0 (24×24 bits fit a double), round to 24 significant bits via ldexp+round_even, then round to integer — the shader's two roundings.
  • depth24_to_float: the shader's integer formula ldexp(float(m+1), e-47), every step exact.
  • No existing helper fits (vulkan_replay.h's round_half_even rounds once and lives in a header that includes this one's users).
  • New runtime/test/depth24_cpu_test.cpp (ctest depth24_cpu, no GPU, no Vulkan loader; the clear_color_test pattern): runs both directions under FE_TONEAREST / FE_DOWNWARD / FE_UPWARD / FE_TOWARDZERO against a nearest-mode reference, with fixed cases (0x1p-23f -> 2, 0.5f -> 8388608 tie-to-even).

What was run

  • g++ -std=c++20 -O2 (also -frounding-math): 1,858,324 checks passed across all four rounding modes (all 2^24 codes round-trip in nearest and downward; every third float pattern in [0,1] quantized nearest-vs-towardzero); plus -ffast-math: 0 mismatches. Before the fix the old header fails immediately under FE_DOWNWARD (depth24_quantize(0x1p-23) = 1, expected 2).
  • MSVC on Windows (VS 2022 BuildTools, RelWithDebInfo, VS generator): depth24_cpu_test built and ctest -R depth24_cpu Passed (0.67 s).
  • Not run: the device-side tests (depth24_convert_test, d3d12_depth24_draw) — no Vulkan device here; the CPU/GPU cross-check on hardware would be the remaining confirmation, though both sides now implement the same documented formula.

Attribution

AI-assisted (Claude Sonnet 5.5 worker implementation, contributor verification incl. the winvm MSVC build, orchestration).

…ad's rounding mode

depth24_quantize multiplied in float and rounded with std::nearbyint, and depth24_to_float divided in double and
narrowed to float; all three follow the calling thread's rounding mode, while depth24_convert.comp is roundEven of a
correctly rounded product and integer arithmetic. Under FE_DOWNWARD or FE_TOWARDZERO 0x1p-23f quantised to 1, not 2,
so CPU clears (vulkan_replay.h depth24_clear_value) could disagree with GPU uploads and copies.

No fesetround (a library must not change the caller's environment, and fenv access can be compiled out). No existing
helper fits (vulkan_replay.h's round_half_even is in a header that includes this one's users and rounds only once), so
depth24_detail::round_even is floor/fmod based like it: exact in every mode. The float product is formed exactly in a
double (24 x 24 bits), rounded to 24 significant bits by scaling with ldexp and round_even, then rounded to the
integer, which is the shader's two roundings. depth24_to_float uses the shader's integer formula,
float(m + 1) * 2^(e-47), every step exact.

depth24_cpu_test (no GPU, no Vulkan loader; same pattern as clear_color_test) runs the conversions under
FE_TONEAREST, FE_DOWNWARD, FE_UPWARD and FE_TOWARDZERO and compares with a float multiply + nearbyint reference run
under the default mode, with fixed cases (0x1p-23f -> 2, 0.5f -> 8388608 tie to even).

AI-assisted. Checked with g++ -std=c++20 -O2 (also -frounding-math, -ffast-math); not built with MSVC.
@potatoe0100

Copy link
Copy Markdown
Contributor

Tested on Windows 11 (26300), NVIDIA GeForce RTX 4060, driver 621.14, VS 2022 RelWithDebInfo + Ninja, merged onto main 384abe5: depth24_cpu passes, and so do the device-side depth24_convert, d3d12_depth24_draw, d3d12_d24_support and clear_color. That covers the CPU/GPU cross-check you couldn't run, on NVIDIA at least.

I also ran an exhaustive check with MSVC /fp:strict. Every float in (0, 1) went through depth24_quantize under all four rounding modes and was compared with main's float multiply + nearbyint in round-to-nearest: 4,261,412,860 checks, 0 mismatches (main's header mismatches under the directed modes). All 2^24 codes through depth24_to_float give the nearest float to d / 16777215 and round-trip in every mode; main gets 16,777,214 of those wrong under the directed modes.

Reading the change: x + 0.5 in round_even is exact at both call sites (at most 24 integer and 24 fraction bits), and float(m + 1) is at most 2^24, so neither path depends on the rounding mode. Looks correct to me.

AI-assisted (Claude).

@thiefyzheng

Copy link
Copy Markdown
Contributor Author

@potatoe0100 — thank you; this closes every verification gap the PR body listed. Recording for whoever merges: on Windows/RTX 4060 (NVIDIA 621.14), the CPU test plus all device-side Depth24 tests pass (depth24_convert, d3d12_depth24_draw, d3d12_d24_support, clear_color), an exhaustive MSVC /fp:strict sweep over every float in (0,1) x all four rounding modes — 4,261,412,860 checks, 0 mismatches against the round-to-nearest reference (main's header mismatches under directed modes), all 2^24 depth24_to_float codes round-trip in every mode (main wrong on 16,777,214 of them), and a reading confirmation that neither path depends on the rounding environment (x + 0.5 exact at both call sites, float(m+1) at most 2^24).

The PR is now CPU-verified (g++ and MSVC), GPU-cross-checked on NVIDIA, and exhaustively swept — nothing left except merge. AI-assisted (orchestrator).

@Timon360SB

Copy link
Copy Markdown
Contributor

Independent verification — Turing and Blackwell

Tested the exact merge base and PR head on two NVIDIA architectures under native Windows.

  • Base: 98c2f7777276f83eb90b491a51721ef90a43ea73
  • Head: 06c3b4a6ac65d044faa7c99a10f46094cbfa9fc5

Environment

Configuration RTX 5070 RTX 2060 SUPER
Architecture Blackwell Turing
NVIDIA driver 621.14 621.14
Windows 11 build 26200 28000
Vulkan device API 1.4.357 1.4.357
Vulkan SDK 1.4.363.0 1.4.363.0
Khronos validation layer 1.4.363 1.4.363

MSVC 19.44.35230, Ninja Multi-Config, RelWithDebInfo.

Validated runs requested core validation, GPU-assisted validation and synchronization validation.

Results

Three initial repetitions per configuration, followed by two additional sets of validated runs.

Duplicate checkpoint results were excluded using execution timestamps and SHA-256 hashes of the original CTest logs.

Test Base Head
depth24_convert, validation off 6/6 6/6
depth24_convert, validation on 0/18 0/18
d3d12_depth24_draw 24/24 24/24
d3d12_d24_support 24/24 24/24
Total 54/72 54/72

Across 144 unique CTest executions: 108 passed, 36 failed.

Results were identical on both GPU architectures.

Depth24 conversion results

Every execution of depth24_convert, including those that subsequently failed under validation, reported:

  • All 16,777,216 depth words converted to the nearest float of d / 16777215.
  • All 16,777,216 depth words survived the float round trip.
  • Both infinities and all NaN payloads quantized correctly.
  • All 16,777,216 tested float patterns, including specials, ties and neighboring values, quantized like the reference.

Existing validation failure

depth24_convert failed under validation on both revisions and both GPUs.

The following diagnostics reproduced in every validated execution:

  • VUID-vkCmdCopyBufferToImage-pRegions-07931
  • line 559: value == 0
  • VUID-vkDestroyDevice-device-05137 — an undestroyed VkQueryPool.

The symptoms match those discussed in #1475. No isolated validation-layer configuration experiment was performed to establish the cause independently.

Depth24 rendering

d3d12_depth24_draw passed in every execution, including LESS/EQUAL comparisons, stored-depth checks and fragment-depth output.

Six internal depth-bias clamp cases per execution were skipped because depthBiasClamp was unavailable for native D24.

d3d12_d24_support also passed in every execution.

Conclusion

No new regressions were observed in the three selected CTest scenarios on either Turing or Blackwell.

The existing validation-only failure remains unchanged.

The new depth24_cpu test and explicit changes to the CPU floating-point rounding mode were not exercised in this run. Therefore, these results provide hardware regression coverage, not independent verification of rounding-mode invariance.

Synthetic tests only. No game files used.

AI-assisted: yes, test automation and result analysis.

@thiefyzheng

Copy link
Copy Markdown
Contributor Author

@Timon360SB — thank you for the two-architecture sweep. Recording for the merge decision: base and head are behaviorally identical on RTX 5070 (Blackwell) and RTX 2060 SUPER (Turing) across 144 deduplicated validated ctest runs — d3d12_depth24_draw and d3d12_d24_support green in every execution, and every depth24_convert run (validation off) converting all 16,777,216 words to the nearest float with full round-trip survival, identical on both archs. No new regressions; the depth24_convert failures under GPU-assisted validation are the known, already-documented class from #1475 (VUID-07931/05137), unchanged between base and head.

As you note, this is hardware regression coverage; the rounding-mode invariance itself stands on the CPU receipts (g++ ASan/UBSan corpus + MSVC build & ctest + your earlier /fp:strict sweep is potatoe0100's — the exhaustive cross-arch picture is now: CPU invariance proven, device parity and non-regression proven on NVIDIA across three architectures). Together with @potatoe0100's run, only merge remains. AI-assisted (orchestrator).

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.

3 participants