[PR #1764] [MERGED] runtime: fix D16S8 fragment depth quantisation and pc_format fallback gating #1776

Closed
opened 2026-11-26 21:18:55 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1764
Author: @IlyassRS3
Created: 11/25/2026
Status: ✅ Merged
Merged: 11/26/2026
Merged by: @aslan2142

Base: main ← Head: fix/depth16-quantise-fallback


📝 Commits (2)

  • 4645a3f runtime: fix D16S8 fragment depth quantisation and pc_format fallback gating
  • bbf861f Merge main into fix/depth16-quantise-fallback

📊 Changes

5 files changed (+673 additions, -16 deletions)

View changed files

📝 runtime/CMakeLists.txt (+4 -0)
📝 runtime/shims/d3d12_vulkan_device.cpp (+6 -3)
📝 runtime/src/depth24_fragment.h (+44 -12)
📝 runtime/src/vulkan_replay.h (+11 -1)
➕ runtime/test/depth24_fragment_test.cpp (+608 -0)

📄 Description

Item ID: RT-DEPTH16-QUANTISE-AND-FALLBACK
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7161138331

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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1764 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** ✅ Merged **Merged:** 11/26/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `fix/depth16-quantise-fallback` --- ### 📝 Commits (2) - [`4645a3f`](https://github.com/ubiquitarianism/leonida/commit/4645a3f62a7655ce69b930da70bdc1a858d1d0a1) runtime: fix D16S8 fragment depth quantisation and pc_format fallback gating - [`bbf861f`](https://github.com/ubiquitarianism/leonida/commit/bbf861fbb4ea51ca18cd6f372797111984b6f8aa) Merge main into fix/depth16-quantise-fallback ### 📊 Changes **5 files changed** (+673 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+4 -0) 📝 `runtime/shims/d3d12_vulkan_device.cpp` (+6 -3) 📝 `runtime/src/depth24_fragment.h` (+44 -12) 📝 `runtime/src/vulkan_replay.h` (+11 -1) ➕ `runtime/test/depth24_fragment_test.cpp` (+608 -0) </details> ### 📄 Description Item ID: `RT-DEPTH16-QUANTISE-AND-FALLBACK` Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7161138331 ### 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 21:18:55 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1776
No description provided.