runtime: fix D16S8 fragment depth quantisation and pc_format fallback gating #1764

Manually merged
github merged 598 commits from github-bot/gh-1764 into main 2026-11-26 18:29:02 +00:00 AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 11:39 UTC · original
IlyassRS3:fix/depth16-quantise-fallback → main · +673 −16 · 5 files · 2 commits · merged 2026-11-26 18:23 UTC

Item ID: RT-DEPTH16-QUANTISE-AND-FALLBACK
Claim: #1608

Summary

Fixes three bugs in the Xbox D16_UNORM_S8_UINT (DXGI 118) depth-draw path introduced in db19bbd3:

  1. 256x depth shrinkage in depth24_fragment.h (levels == 65535.0f): Patch::quantise() scaled and clamped fragment depth by levels_ (65535.0f), but still hardcoded the 24-bit UNORM reconstruction constants (0xffffffu, 23 - e, e + 80). As a result, every fragment depth in (0, 1) on a D16S8 fallback pass was divided by 16,777,215 instead of 65,535 (e.g. 0.5 -> 0.001953, 0.9999 -> 0.003906, while 1.0 -> 1.0).

    • Added an exact, branch-free, division-free 16-bit UNORM code-to-float path when levels_ == 65535.0f (n = d << (15 - e), m = (n << 8) + ((n + 128) >> 8), scale = 2^(e - 39) via biased exponent e + 88), matching static_cast<float>(double(d) / 65535.0) bit-for-bit across all 65,536 codes without relying on SPIR-V OpFDiv's 2.5 ULP bound.
    • Primed all D16 constants in Patch::prime_quantise() before apply() so shader_writes_depth (at_return) never emits undeclared global constants.
    • Rejected unsupported levels values (anything other than 16777215.0f or 65535.0f) instead of silently feeding them into the 24-bit formula, and fixed -Wreorder on Patch::levels_.
  2. packed16_fallback bypassed pc_format() in d3d12_vulkan_device.cpp:7298: PipelineState::create_graphics() checked raw desc.DSVFormat == xbox_d16s8 instead of vulkan_format(desc.DSVFormat, true).format == VK_FORMAT_D16_UNORM_S8_UINT. On NVIDIA GPUs (default LEONIDA_CONSOLE_D16S8=d24, where pc_format(118) backs format 118 with native VK_FORMAT_D24_UNORM_S8_UINT), packed16_fallback still evaluated true — returning E_NOTIMPL on MSAA format-118 PSOs, disabling hardware depth bias, and patching single-sampled fragment shaders with 16-bit quantisation against a D24S8 target.

  3. D16S8 clear/fragment tie & NaN parity in vulkan_replay.h:4344: replaced std::round(std::clamp(depth, 0.0f, 1.0f) * 65535.0f) / 65535.0f with detail::quantize_depth16() (float multiply + round_half_even + NaN -> 0.0f + double divide) so ClearDepthStencilView matches Patch::quantise(levels = 65535.0f) bit-for-bit for EQUAL depth tests.

Testing

  • AI-assisted: yes
  • GPUs tested: none (macOS host, no Vulkan SDK / Windows SDK / game files). The Windows/Vulkan edits in runtime/shims/d3d12_vulkan_device.cpp and runtime/src/vulkan_replay.h could not be compiled or GPU-tested locally and rely on CI / Windows GPU verification.
  • Commands run:
    • clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=address,undefined runtime/test/depth24_fragment_test.cpp -o <tmp> && <tmp> — PASS (all 65536 D16 codes, ties, specials, bias/clamp, D24 parity, including structural SPIR-V validation and execution of depth-only, plain PS, and multi-return SV_Depth PS)
    • clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/depth24_fragment_test.cpp -o <tmp> && <tmp> — PASS
    • Mutation test against unpatched main:runtime/src/depth24_fragment.h — reproduced exact failure (D16 mismatch at d=1: got 33800001 (5.96046519e-08), want 37800080 (1.52590219e-05), exit 1)
    • python3 -I scripts/check_no_game_data.py runtime/src/depth24_fragment.h runtime/shims/d3d12_vulkan_device.cpp runtime/src/vulkan_replay.h runtime/test/depth24_fragment_test.cpp runtime/CMakeLists.txt — PASS
    • python3 -I tests/test_text_write_line_endings.py — PASS (3/3)
**@⁠IlyassRS3** on GitHub · 2026-11-25 11:39 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1764) `IlyassRS3:fix/depth16-quantise-fallback` → `main` · +673 −16 · 5 files · 2 commits · merged 2026-11-26 18:23 UTC Item ID: `RT-DEPTH16-QUANTISE-AND-FALLBACK` Claim: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 ### Summary Fixes three bugs in the Xbox `D16_UNORM_S8_UINT` (DXGI 118) depth-draw path introduced in `db19bbd3`: 1. **256x depth shrinkage in `depth24_fragment.h` (`levels == 65535.0f`)**: `Patch::quantise()` scaled and clamped fragment depth by `levels_` (`65535.0f`), but still hardcoded the 24-bit UNORM reconstruction constants (`0xffffffu`, `23 - e`, `e + 80`). As a result, every fragment depth in `(0, 1)` on a D16S8 fallback pass was divided by `16,777,215` instead of `65,535` (e.g. `0.5 -> 0.001953`, `0.9999 -> 0.003906`, while `1.0 -> 1.0`). - Added an exact, branch-free, division-free 16-bit UNORM code-to-float path when `levels_ == 65535.0f` (`n = d << (15 - e)`, `m = (n << 8) + ((n + 128) >> 8)`, `scale = 2^(e - 39)` via biased exponent `e + 88`), matching `static_cast<float>(double(d) / 65535.0)` bit-for-bit across all 65,536 codes without relying on SPIR-V `OpFDiv`'s 2.5 ULP bound. - Primed all D16 constants in `Patch::prime_quantise()` before `apply()` so `shader_writes_depth` (`at_return`) never emits undeclared global constants. - Rejected unsupported `levels` values (anything other than `16777215.0f` or `65535.0f`) instead of silently feeding them into the 24-bit formula, and fixed `-Wreorder` on `Patch::levels_`. 2. **`packed16_fallback` bypassed `pc_format()` in `d3d12_vulkan_device.cpp:7298`**: `PipelineState::create_graphics()` checked raw `desc.DSVFormat == xbox_d16s8` instead of `vulkan_format(desc.DSVFormat, true).format == VK_FORMAT_D16_UNORM_S8_UINT`. On NVIDIA GPUs (default `LEONIDA_CONSOLE_D16S8=d24`, where `pc_format(118)` backs format 118 with native `VK_FORMAT_D24_UNORM_S8_UINT`), `packed16_fallback` still evaluated true — returning `E_NOTIMPL` on MSAA format-118 PSOs, disabling hardware depth bias, and patching single-sampled fragment shaders with 16-bit quantisation against a D24S8 target. 3. **D16S8 clear/fragment tie & NaN parity in `vulkan_replay.h:4344`**: replaced `std::round(std::clamp(depth, 0.0f, 1.0f) * 65535.0f) / 65535.0f` with `detail::quantize_depth16()` (float multiply + `round_half_even` + `NaN -> 0.0f` + double divide) so `ClearDepthStencilView` matches `Patch::quantise(levels = 65535.0f)` bit-for-bit for `EQUAL` depth tests. ### Testing - **AI-assisted**: yes - **GPUs tested**: none (macOS host, no Vulkan SDK / Windows SDK / game files). The Windows/Vulkan edits in `runtime/shims/d3d12_vulkan_device.cpp` and `runtime/src/vulkan_replay.h` could not be compiled or GPU-tested locally and rely on CI / Windows GPU verification. - **Commands run**: - `clang++ -std=c++20 -Wall -Wextra -Werror -fsanitize=address,undefined runtime/test/depth24_fragment_test.cpp -o <tmp> && <tmp>` — PASS (`all 65536 D16 codes, ties, specials, bias/clamp, D24 parity`, including structural SPIR-V validation and execution of depth-only, plain PS, and multi-return `SV_Depth` PS) - `clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/depth24_fragment_test.cpp -o <tmp> && <tmp>` — PASS - Mutation test against unpatched `main:runtime/src/depth24_fragment.h` — reproduced exact failure (`D16 mismatch at d=1: got 33800001 (5.96046519e-08), want 37800080 (1.52590219e-05)`, exit 1) - `python3 -I scripts/check_no_game_data.py runtime/src/depth24_fragment.h runtime/shims/d3d12_vulkan_device.cpp runtime/src/vulkan_replay.h runtime/test/depth24_fragment_test.cpp runtime/CMakeLists.txt` — PASS - `python3 -I tests/test_text_write_line_endings.py` — PASS (3/3)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:04:07 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 11:39 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 11:39 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1764#pullrequestreview-6056925991) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-25 11:48 UTC · original

Native Windows 11 / RTX 4090 (NVIDIA 616.64), built with MSVC. The D16 shader change fixes a measurable output error on this card.

Synthetic fragment-depth readback Parent bcaa13dad PR 4645a3f62
D16 codes matching the float32 reference bit-for-bit 2 / 65,536 65,536 / 65,536
Output for code 32,768 (expected 0.5000076294) 0.0019531252 0.5000076294

I used a 256×256 full-screen draw with one D16 code per pixel. The input shader wrote float(code) / 65535.0 to gl_FragDepth. For each revision I ran its production depth24_patch_fragment with 65,535 levels and zero bias, then copied the final shader depth to an RGBA32F color attachment immediately before return. Both the original patched modules and the instrumented modules passed Vulkan 1.0 SPIR-V validation. This measures the GPU shader output before depth attachment storage.

All seven targeted CTests passed on the PR, including the fragment code sweep, D16S8 storage/surface tests and D24 draw regression. The D24 draw test skipped its six native-D24 bias-clamp cases on this setup. The GPU ramp uses a single sample; format-118 multisample pipeline creation is outside its coverage.

AI-assisted verification with OpenAI Codex. All inputs were synthetic.

**@⁠ecox982745** on GitHub · 2026-11-25 11:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1764#issuecomment-7161534271) Native Windows 11 / RTX 4090 (NVIDIA 616.64), built with MSVC. The D16 shader change fixes a measurable output error on this card. | Synthetic fragment-depth readback | Parent `bcaa13dad` | PR `4645a3f62` | | --- | ---: | ---: | | D16 codes matching the float32 reference bit-for-bit | 2 / 65,536 | 65,536 / 65,536 | | Output for code 32,768 (expected 0.5000076294) | 0.0019531252 | 0.5000076294 | I used a 256×256 full-screen draw with one D16 code per pixel. The input shader wrote `float(code) / 65535.0` to `gl_FragDepth`. For each revision I ran its production `depth24_patch_fragment` with 65,535 levels and zero bias, then copied the final shader depth to an RGBA32F color attachment immediately before return. Both the original patched modules and the instrumented modules passed Vulkan 1.0 SPIR-V validation. This measures the GPU shader output before depth attachment storage. All seven targeted CTests passed on the PR, including the fragment code sweep, D16S8 storage/surface tests and D24 draw regression. The D24 draw test skipped its six native-D24 bias-clamp cases on this setup. The GPU ramp uses a single sample; format-118 multisample pipeline creation is outside its coverage. AI-assisted verification with OpenAI Codex. All inputs were synthetic.
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-25 12:18 UTC · original

Verified the other two changes on native Windows 11, RTX 4090 (616.64). I built the parent bcaa13dad and PR 4645a3f62 with MSVC and ran identical synthetic fixtures against both.

4× MSAA with Xbox depth format 118. With default settings, the parent returned E_NOTIMPL (0x80004001) from the first CreateGraphicsPipelineState. The PR returned S_OK, drew and resolved, and matched all 4,096 expected color pixels. Of 189 edge pixels, 64 had partial sample coverage, and the one-sample write mask behaved as expected. To reproduce, use format 118 in the three D32 sites in runtime/test/d3d12_msaa_draw_test.cpp (pipeline, resource, and view). The original D32 fixture passed on both builds. With LEONIDA_CONSOLE_D16S8=native both builds still returned E_NOTIMPL for the format-118 multisample pipeline, as intended on this card.

Depth clear rounding. I used the six-layer xbox_depth_storage_test with LEONIDA_CONSOLE_D16S8=native and LEONIDA_TEST_D16S8_UAV=1. These are actual D16 depth words read back through the fixture’s UAV round trip.

Clear input in 16-bit code units (divide by 65535 before clearing) Parent PR
1024.5 1025 1024
1025.5 1026 1026
32768.5 32769 32768
65534.5 65535 65534

NaN read back as 0 on both builds. On the PR all 6,144 depth words and 6,144 stencil bytes matched, including the rectangle overwritten by the fixture. Both the default MSAA draw and the opt-in depth-clear test passed again in fresh processes.

AI-assisted verification with OpenAI Codex.

**@⁠ecox982745** on GitHub · 2026-11-25 12:18 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1764#issuecomment-7162202280) Verified the other two changes on native Windows 11, RTX 4090 (616.64). I built the parent `bcaa13dad` and PR `4645a3f62` with MSVC and ran identical synthetic fixtures against both. **4× MSAA with Xbox depth format 118.** With default settings, the parent returned `E_NOTIMPL` (0x80004001) from the first `CreateGraphicsPipelineState`. The PR returned `S_OK`, drew and resolved, and matched all 4,096 expected color pixels. Of 189 edge pixels, 64 had partial sample coverage, and the one-sample write mask behaved as expected. To reproduce, use format 118 in the three D32 sites in `runtime/test/d3d12_msaa_draw_test.cpp` (pipeline, resource, and view). The original D32 fixture passed on both builds. With `LEONIDA_CONSOLE_D16S8=native` both builds still returned `E_NOTIMPL` for the format-118 multisample pipeline, as intended on this card. **Depth clear rounding.** I used the six-layer `xbox_depth_storage_test` with `LEONIDA_CONSOLE_D16S8=native` and `LEONIDA_TEST_D16S8_UAV=1`. These are actual D16 depth words read back through the fixture’s UAV round trip. | Clear input in 16-bit code units (divide by 65535 before clearing) | Parent | PR | | --- | ---: | ---: | | 1024.5 | 1025 | 1024 | | 1025.5 | 1026 | 1026 | | 32768.5 | 32769 | 32768 | | 65534.5 | 65535 | 65534 | NaN read back as 0 on both builds. On the PR all 6,144 depth words and 6,144 stencil bytes matched, including the rectangle overwritten by the fixture. Both the default MSAA draw and the opt-in depth-clear test passed again in fresh processes. AI-assisted verification with OpenAI Codex.
Author
Owner

@⁠Timon360SB on GitHub · 2026-11-25 14:40 UTC · original

Independent verification — Turing and Blackwell

  • Base: bcaa13dad35f560748bd9b6293cd46504fe288c6
  • Head: 4645a3f62a7655ce69b930da70bdc1a858d1d0a1

Environment

GPU Architecture Windows 11 build
RTX 5070 Blackwell 26200
RTX 2060 SUPER Turing 28000

NVIDIA 621.14, Vulkan SDK 1.4.363.0, MSVC 19.44, Ninja RelWithDebInfo.

Three initial repetitions per configuration, followed by two additional sets of validated runs. Duplicate checkpoint results were excluded.

Results

Test Base Head
depth24_convert 6/24 6/24
d3d12_depth24_draw 24/24 24/24
d3d12_d24_support 24/24 24/24
xbox_d16s8_storage 6/24 6/24
xbox_d16s8_surface 24/24 24/24
d3d12_msaa_draw 24/24 24/24
d3d12_depth_stencil_uav 6/24 6/24
xbox_d16s8_refused_uav 24/24 24/24
Total 138/192 138/192

384 unique test executions: 276 passed, 108 failed, 0 CTest skips.

Findings

  • All tests passed with validation disabled.
  • The three failing tests reproduce VUID-vkCmdCopyBufferToImage-pRegions-07931 under validation on both revisions and GPUs.
  • depth24_convert additionally reports VUID-vkDestroyDevice-device-05137.
  • D16S8 UAV round trips returned all 6,144 depth words and 6,144 stencil bytes exactly, including validated runs.
  • Standard 4x MSAA tests matched all 4,096 pixels.
  • Native D24 depth-bias tests retain 12 known discrepancies and six internal depthBiasClamp skips per execution.

Conclusion

No new regressions observed in the selected tests on either architecture.

The new depth24_fragment test, dedicated D16 quantisation sweep and format-118 MSAA experiment were not included. The standard MSAA fixture uses D32_FLOAT.

Synthetic tests only. No game files used.
AI-assisted: yes.

**@⁠Timon360SB** on GitHub · 2026-11-25 14:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1764#issuecomment-7165649275) ## Independent verification — Turing and Blackwell - **Base:** `bcaa13dad35f560748bd9b6293cd46504fe288c6` - **Head:** `4645a3f62a7655ce69b930da70bdc1a858d1d0a1` ### Environment | GPU | Architecture | Windows 11 build | |---|---|---| | RTX 5070 | Blackwell | 26200 | | RTX 2060 SUPER | Turing | 28000 | NVIDIA 621.14, Vulkan SDK 1.4.363.0, MSVC 19.44, Ninja RelWithDebInfo. Three initial repetitions per configuration, followed by two additional sets of validated runs. Duplicate checkpoint results were excluded. ### Results | Test | Base | Head | |---|---:|---:| | `depth24_convert` | 6/24 | 6/24 | | `d3d12_depth24_draw` | 24/24 | 24/24 | | `d3d12_d24_support` | 24/24 | 24/24 | | `xbox_d16s8_storage` | 6/24 | 6/24 | | `xbox_d16s8_surface` | 24/24 | 24/24 | | `d3d12_msaa_draw` | 24/24 | 24/24 | | `d3d12_depth_stencil_uav` | 6/24 | 6/24 | | `xbox_d16s8_refused_uav` | 24/24 | 24/24 | | **Total** | **138/192** | **138/192** | 384 unique test executions: 276 passed, 108 failed, 0 CTest skips. ### Findings - All tests passed with validation disabled. - The three failing tests reproduce `VUID-vkCmdCopyBufferToImage-pRegions-07931` under validation on both revisions and GPUs. - `depth24_convert` additionally reports `VUID-vkDestroyDevice-device-05137`. - D16S8 UAV round trips returned all 6,144 depth words and 6,144 stencil bytes exactly, including validated runs. - Standard 4x MSAA tests matched all 4,096 pixels. - Native D24 depth-bias tests retain 12 known discrepancies and six internal depthBiasClamp skips per execution. ### Conclusion No new regressions observed in the selected tests on either architecture. The new `depth24_fragment` test, dedicated D16 quantisation sweep and format-118 MSAA experiment were not included. The standard MSAA fixture uses D32_FLOAT. Synthetic tests only. No game files used. AI-assisted: yes.
github manually merged commit aa663c4c5d into main 2026-11-26 18:28:14 +00:00
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-26 18:23 UTC · original

Merging.

  • Why: on a GPU without a D16+S8 attachment format, the fragment depth patch rounded to 16 bits and then rebuilt the depth with the 24-bit constants, so every depth came out 256 times too small. The 16-bit path is now exact, the fallback check uses the PC format the runtime actually picked, and depth clears round half to even.
  • Maintainer commit bbf861fbb: a merge of main bdbc7e923 into the branch. It resolves one conflict in runtime/CMakeLists.txt, where main added three test entries at the place this branch adds depth24_fragment. Both sides are kept; nothing else was changed.
  • Tested on that merge:
    • Windows suite under Wine (clang-cl + xwin) on the integrated RDNA2 GPU (RADV, Mesa 26.3.1): 423 pass and 23 fail of 487. No test went from pass to fail against main, and depth24_fragment passes.
    • check_no_game_data.py is clean on the five changed files.
  • Not tested here: no world run. On our RX 7900 XTX (RADV) the console format is a real D16+S8 attachment, so the patched path does not run and a run could not show the fix. @⁠ecox982745's check of all 65,536 depth codes on an RTX 4090 and @⁠Timon360SB's runs on this head are the evidence for it.

Thanks @⁠IlyassRS3, and @⁠ecox982745 and @⁠Timon360SB for the tests.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

**@⁠aslan2142** on GitHub · 2026-11-26 18:23 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1764#issuecomment-7200406827) Merging. - **Why:** on a GPU without a D16+S8 attachment format, the fragment depth patch rounded to 16 bits and then rebuilt the depth with the 24-bit constants, so every depth came out 256 times too small. The 16-bit path is now exact, the fallback check uses the PC format the runtime actually picked, and depth clears round half to even. - **Maintainer commit `bbf861fbb`:** a merge of `main` `bdbc7e923` into the branch. It resolves one conflict in `runtime/CMakeLists.txt`, where `main` added three test entries at the place this branch adds `depth24_fragment`. Both sides are kept; nothing else was changed. - **Tested on that merge:** - Windows suite under Wine (clang-cl + xwin) on the integrated RDNA2 GPU (RADV, Mesa 26.3.1): 423 pass and 23 fail of 487. No test went from pass to fail against `main`, and `depth24_fragment` passes. - `check_no_game_data.py` is clean on the five changed files. - **Not tested here:** no world run. On our RX 7900 XTX (RADV) the console format is a real D16+S8 attachment, so the patched path does not run and a run could not show the fix. @⁠ecox982745's check of all 65,536 depth codes on an RTX 4090 and @⁠Timon360SB's runs on this head are the evidence for it. Thanks @⁠IlyassRS3, and @⁠ecox982745 and @⁠Timon360SB for the tests. AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)
This discussion has been locked. Commenting is limited to contributors.
No description provided.