fix(libSceAgcDriver): quantize fallback D16_UNORM_S8_UINT depth clears, writes and bounds #17899

Merged
hamb3r merged 9 commits from upstream-pr/17899 into main 2026-11-25 05:15:38 +00:00
hamb3r commented 2026-11-09 21:18:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hamb3r on 2026-11-09T21:18:55Z.


What

On devices without VK_FORMAT_D16_UNORM_S8_UINT attachment support (such as NVIDIA GPUs), libSceAgcDriver backs 16-bit depth-stencil surfaces with VK_FORMAT_D24_UNORM_S8_UINT or VK_FORMAT_D32_SFLOAT_S8_UINT. Previously, clears, fragment depth values and dynamic depth bounds kept the wider host format's sub-16-bit precision, causing EQUAL / LESS / GREATER depth tests, stencil pass/depth-fail operations and depthBoundsTest to diverge from native 16-bit RDNA2 depth buffers whenever two values fell into the same 1 / 65535 step.

This PR matches native 16-bit unorm depth precision across all three paths on fallback surfaces:

  • Depth clears (DepthSurface): Round vkCmdClearDepthStencilImage clear values to the nearest 16-bit unorm step (rint(clamp(z, 0, 1) * 65535) * (1 / 65535)), and add a trivially-destructible thread_local fast-path cache to HostDepthFormat ahead of the cache mutex.
  • Fragment depth (Pipeline): When a draw tests or writes depth on a fallback D16_UNORM_S8_UINT target, rewrite the fragment shader's SPIR-V to quantize FragDepth stores (or inject FragDepth quantized from FragCoord.z) via RoundEven in [0, 1], strip EarlyFragmentTests / DepthLess / DepthGreater and add DepthReplacing; shaders with EarlyFragmentTests that also write memory throw rather than moving early tests past side effects. Stencil-only and depth-bounds-only draws (!depthTest && !depthWrite) leave the shader untouched. Pipeline and VK_EXT_graphics_pipeline_library keys include HostDepthFormat and QuantizesDepth16.
  • Dynamic depth bounds (Pipeline): Clamp and round minDepthBounds / maxDepthBounds to 16-bit unorm steps in Bind, setLibraryState and ContinueBackFaces (HostDepthBounds), matching the DB_DEPTH_BOUNDS_MIN / DB_DEPTH_BOUNDS_MAX rounding and [0, 1] clamping measured on RDNA2 D16_UNORM_S8_UINT hardware even with VK_EXT_depth_range_unrestricted enabled.
  • Debt & tests: Update the 16-bit depth with stencil entry in docs/dev/debt/libSceAgcDriver.md and add agc_driver_d16_stencil_precision covering bucket boundaries, 0.5-tie rounding, clears, interpolated depth, OpKill, explicit FragDepth, negativeOneToOne, sub-quantum and unrestricted depth bounds, sub-quantum and multi-step slope/constant depth bias, guest format controls and EarlyFragmentTests memory-write refusal.

Tested

Windows 10 x64 with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (verified active; 0 missing-layer warnings and 0 VUID- validation errors across all runs):

  • agc_driver_d16_stencil_precision:
    • AMD Radeon(TM) Graphics (RDNA2, driver 26.10.2): 89/89 on native VK_FORMAT_D16_UNORM_S8_UINT and 85/85 on forced VK_FORMAT_D32_SFLOAT_S8_UINT fallback (1-to-1 match against native RDNA2 16-bit depth hardware).
    • NVIDIA GeForce RTX 3080 (driver 617.14): 91/91 on default VK_FORMAT_D24_UNORM_S8_UINT fallback and 87/87 on forced VK_FORMAT_D32_SFLOAT_S8_UINT fallback.
  • Regression & validation comparison (main vs branch, both GPUs):
    • agc_driver_depth_bias_per_face and agc_driver_depth_range_remap_draw: passed on both main and this branch with 0 VUID- validation errors.
  • tools/check_conventions.py --base origin/main: passed.
  • tools/pr_overlap.py --branch fix/agc-d16s8-depth-quantization: no open PR implements D16_UNORM_S8_UINT quantization.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 3080 (driver 617.14, Windows 10) and AMD Radeon(TM) Graphics (driver 26.10.2, Windows 10)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5231361852 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17899). Originally posted by [hamb3r](https://github.com/hamb3r) on 2026-11-09T21:18:55Z. --- ### What On devices without `VK_FORMAT_D16_UNORM_S8_UINT` attachment support (such as NVIDIA GPUs), `libSceAgcDriver` backs 16-bit depth-stencil surfaces with `VK_FORMAT_D24_UNORM_S8_UINT` or `VK_FORMAT_D32_SFLOAT_S8_UINT`. Previously, clears, fragment depth values and dynamic depth bounds kept the wider host format's sub-16-bit precision, causing `EQUAL` / `LESS` / `GREATER` depth tests, stencil pass/depth-fail operations and `depthBoundsTest` to diverge from native 16-bit RDNA2 depth buffers whenever two values fell into the same `1 / 65535` step. This PR matches native 16-bit unorm depth precision across all three paths on fallback surfaces: - **Depth clears (`DepthSurface`):** Round `vkCmdClearDepthStencilImage` clear values to the nearest 16-bit unorm step (`rint(clamp(z, 0, 1) * 65535) * (1 / 65535)`), and add a trivially-destructible `thread_local` fast-path cache to `HostDepthFormat` ahead of the cache mutex. - **Fragment depth (`Pipeline`):** When a draw tests or writes depth on a fallback `D16_UNORM_S8_UINT` target, rewrite the fragment shader's SPIR-V to quantize `FragDepth` stores (or inject `FragDepth` quantized from `FragCoord.z`) via `RoundEven` in `[0, 1]`, strip `EarlyFragmentTests` / `DepthLess` / `DepthGreater` and add `DepthReplacing`; shaders with `EarlyFragmentTests` that also write memory throw rather than moving early tests past side effects. Stencil-only and depth-bounds-only draws (`!depthTest && !depthWrite`) leave the shader untouched. Pipeline and `VK_EXT_graphics_pipeline_library` keys include `HostDepthFormat` and `QuantizesDepth16`. - **Dynamic depth bounds (`Pipeline`):** Clamp and round `minDepthBounds` / `maxDepthBounds` to 16-bit unorm steps in `Bind`, `setLibraryState` and `ContinueBackFaces` (`HostDepthBounds`), matching the `DB_DEPTH_BOUNDS_MIN` / `DB_DEPTH_BOUNDS_MAX` rounding and `[0, 1]` clamping measured on RDNA2 `D16_UNORM_S8_UINT` hardware even with `VK_EXT_depth_range_unrestricted` enabled. - **Debt & tests:** Update the `16-bit depth with stencil` entry in `docs/dev/debt/libSceAgcDriver.md` and add `agc_driver_d16_stencil_precision` covering bucket boundaries, `0.5`-tie rounding, clears, interpolated depth, `OpKill`, explicit `FragDepth`, `negativeOneToOne`, sub-quantum and unrestricted depth bounds, sub-quantum and multi-step slope/constant depth bias, guest format controls and `EarlyFragmentTests` memory-write refusal. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 10 x64 with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (verified active; 0 missing-layer warnings and 0 `VUID-` validation errors across all runs): - **`agc_driver_d16_stencil_precision`:** - **AMD Radeon(TM) Graphics (RDNA2, driver 26.10.2):** `89/89` on native `VK_FORMAT_D16_UNORM_S8_UINT` and `85/85` on forced `VK_FORMAT_D32_SFLOAT_S8_UINT` fallback (1-to-1 match against native RDNA2 16-bit depth hardware). - **NVIDIA GeForce RTX 3080 (driver 617.14):** `91/91` on default `VK_FORMAT_D24_UNORM_S8_UINT` fallback and `87/87` on forced `VK_FORMAT_D32_SFLOAT_S8_UINT` fallback. - **Regression & validation comparison (`main` vs branch, both GPUs):** - `agc_driver_depth_bias_per_face` and `agc_driver_depth_range_remap_draw`: passed on both `main` and this branch with `0` `VUID-` validation errors. - **`tools/check_conventions.py --base origin/main`:** passed. - **`tools/pr_overlap.py --branch fix/agc-d16s8-depth-quantization`:** no open PR implements `D16_UNORM_S8_UINT` quantization. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 3080 (driver 617.14, Windows 10) and AMD Radeon(TM) Graphics (driver 26.10.2, Windows 10) - [x] AI-assisted: yes
MarcZX10 commented 2026-11-20 15:22:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T15:22:04Z.

Review: COMMENTED


Reviewed — comments

Material implementation defects are not verified from the inspected code; the new execution test’s build integration was not confirmed.

What I ran

Reviewed head dbdf82b05ce034aa55dbfd4d5d07e16e63e54681 using the prepared diff and the pinned repo_read.py reader. No build or test commands were run. The GPU results in the PR description are author-reported.

Results

Check Result Note
Diff and targeted code review passed Core changes inspected
Build and runtime checks not run Not available in this review
Test registration not verified Registration files not inspected

Findings

Blocking

None verified.

Non-blocking

  • The SPIR-V rewrite is a substantial correctness-sensitive path. The added test exercises many cases, but this review did not run it or inspect all of its cases and build integration; please ensure it is registered and run in CI.

Not verified

Read: description.md, commits.md, manifest.json, diff.patch, README.md, targeted ranges in Pipeline.cpp and DepthSurface.cpp, and the relevant range in D16StencilPrecision.cpp as shown in the diff. All four changed files are included in the review bundle; full repository file texts were not read. Build registration, full test source, compilation, runtime behavior, validation output, and reported GPU results were not verified. Excluded files: none listed in the manifest.

To get this to a good state

Nothing is required. Optional: confirm the new execution test is registered and run it in CI.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5995039146 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17899#pullrequestreview-5995039146). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T15:22:04Z. **Review:** COMMENTED --- # Reviewed — comments Material implementation defects are not verified from the inspected code; the new execution test’s build integration was not confirmed. ## What I ran Reviewed head `dbdf82b05ce034aa55dbfd4d5d07e16e63e54681` using the prepared diff and the pinned `repo_read.py` reader. No build or test commands were run. The GPU results in the PR description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted code review | passed | Core changes inspected | | Build and runtime checks | not run | Not available in this review | | Test registration | not verified | Registration files not inspected | ## Findings ### Blocking None verified. ### Non-blocking - The SPIR-V rewrite is a substantial correctness-sensitive path. The added test exercises many cases, but this review did not run it or inspect all of its cases and build integration; please ensure it is registered and run in CI. ## Not verified Read: `description.md`, `commits.md`, `manifest.json`, `diff.patch`, `README.md`, targeted ranges in `Pipeline.cpp` and `DepthSurface.cpp`, and the relevant range in `D16StencilPrecision.cpp` as shown in the diff. All four changed files are included in the review bundle; full repository file texts were not read. Build registration, full test source, compilation, runtime behavior, validation output, and reported GPU results were not verified. Excluded files: none listed in the manifest. ## To get this to a good state Nothing is required. Optional: confirm the new execution test is registered and run it in CI.
MarcZX10 commented 2026-11-20 20:45:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T20:45:44Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the points below are optional.

What I ran

Reviewed head 5da49746a172d70d3e6aeb007ef40e2df1e1c638 using the supplied review artifacts and the pinned repository reader. No commands or tests were run.

Results

Check Result Note
Diff and targeted source review passed Four changed files read
Runtime tests and build not run Hardware results are author-reported

Findings

Blocking

None.

Non-blocking

  • The fragment SPIR-V rewrite contains custom instruction insertion and control-flow handling. Consider adding an offline SPIR-V validation test for shaders with multiple entry-point interfaces and early exits, alongside the reported GPU coverage.

Not verified

The runtime tests and validation runs described in the PR body were not run by this review. The listed changed files were all included and read: DepthSurface.cpp, Pipeline.cpp, D16StencilPrecision.cpp, and fix-agc-d16s8-depth-quantization.md. No excluded changed files were listed. The build and test registration were inspected in CMakeLists.txt but not executed.

To get this to a good state

Nothing is required. The SPIR-V validation test is optional.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5999131093 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17899#pullrequestreview-5999131093). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T20:45:44Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the points below are optional. ## What I ran Reviewed head `5da49746a172d70d3e6aeb007ef40e2df1e1c638` using the supplied review artifacts and the pinned repository reader. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted source review | passed | Four changed files read | | Runtime tests and build | not run | Hardware results are author-reported | ## Findings ### Blocking None. ### Non-blocking - The fragment SPIR-V rewrite contains custom instruction insertion and control-flow handling. Consider adding an offline SPIR-V validation test for shaders with multiple entry-point interfaces and early exits, alongside the reported GPU coverage. ## Not verified The runtime tests and validation runs described in the PR body were not run by this review. The listed changed files were all included and read: `DepthSurface.cpp`, `Pipeline.cpp`, `D16StencilPrecision.cpp`, and `fix-agc-d16s8-depth-quantization.md`. No excluded changed files were listed. The build and test registration were inspected in `CMakeLists.txt` but not executed. ## To get this to a good state Nothing is required. The SPIR-V validation test is optional.
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
boykopovar/AnyPS5!17899
No description provided.