fix(agc): clamp an exported pixel depth to the viewport depth range #10348

Merged
oneandonlydean merged 9 commits from upstream-pr/10348 into main 2026-10-28 05:25:05 +00:00
oneandonlydean commented 2026-10-23 18:33:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-23T18:33:44Z.


What

A pixel shader that exports depth now has the exported value clamped to the viewport depth range before it reaches the depth attachment, as the DB does.

  • The pixel stage info carries PA_SC_VPORT_ZMIN_0 and ZMAX_0 when the shader exports depth (0 and 1 otherwise), and the recompiled shader NClamps the exported value to them before it stores gl_FragDepth. Only viewport 0 bounds the clamp.
  • With DB_RENDER_OVERRIDE.DISABLE_VIEWPORT_CLAMP set, the value is stored as exported. Non-finite or inverted bounds are rejected. DB_RENDER_OVERRIDE joins the draw key registers.
  • The bounds are part of the recompile cache key (only for depth-exporting shaders, so no other key changes), the draw key and its register check, and recompile requests.

The DB clamps an exported depth to ZMIN/ZMAX unless the override disables it. Vulkan only clamps with depthClamp, which also turns off clipping, so until now the exported value reached the depth attachment unclamped.

This writes recompile request version 49 and reads the depth bounds from version 49 on. Main is at version 48 (the barycentric built-ins flag from #9193); this branch keeps main's version-48 fields and adds the bounds after them. Version 46, 47 and 48 requests still decode, with default depth bounds.

Tested

Head d4a296d863 (main d13b3d7c55 merged in), compared with main d13b3d7c55, same machine, NVIDIA GeForce RTX 4090 (driver 610.57.04, Vulkan 1.4.341), Linux, Release build, ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (Khronos validation layer 1.4.363) and VK_LOADER_DEBUG=layer, so each log shows the loader inserting the layer. Tests selected: agc_driver_depth_export_clamp, agc_shader_memory, request_serializer, agc_driver_graphics, agc_masked_select and every test with depth in its name.

main d13b3d7c55 head d4a296d863
tests passed / failed / skipped 42 / 0 / 0 43 / 0 / 0
Insert instance layer "VK_LAYER_KHRONOS_validation" (loader lines) 44 45
was not found but was requested 0 0
VUID- lines 0 0

There are a couple more inserted-layer lines than tests because some tests create more than one Vulkan instance.

The extra passing test on the head is agc_driver_depth_export_clamp, which does not exist on main. It runs on the device.

  • A shader that exports 1.5 with a viewport depth range of [0.25, 0.75] leaves 0.75 in the depth buffer, and an export of -0.5 leaves 0.25. With the override set, the exported value is stored as it is.
  • With the emitter changed to always store the exported value unclamped, agc_driver_depth_export_clamp and agc_shader_memory fail, so the tests cover the clamp.
  • The pixel stage decode reads the bounds, passes the disable flag through, rejects bad bounds, and leaves a draw without depth export at 0 and 1. The bounds survive serialization and key the cache.

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
  • 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 4090, driver 610.57.04, Linux
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4987542445 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10348). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-23T18:33:44Z. --- ### What A pixel shader that exports depth now has the exported value clamped to the viewport depth range before it reaches the depth attachment, as the DB does. - The pixel stage info carries `PA_SC_VPORT_ZMIN_0` and `ZMAX_0` when the shader exports depth (0 and 1 otherwise), and the recompiled shader `NClamp`s the exported value to them before it stores `gl_FragDepth`. Only viewport 0 bounds the clamp. - With `DB_RENDER_OVERRIDE.DISABLE_VIEWPORT_CLAMP` set, the value is stored as exported. Non-finite or inverted bounds are rejected. `DB_RENDER_OVERRIDE` joins the draw key registers. - The bounds are part of the recompile cache key (only for depth-exporting shaders, so no other key changes), the draw key and its register check, and recompile requests. The DB clamps an exported depth to `ZMIN`/`ZMAX` unless the override disables it. Vulkan only clamps with `depthClamp`, which also turns off clipping, so until now the exported value reached the depth attachment unclamped. This writes recompile request version 49 and reads the depth bounds from version 49 on. Main is at version 48 (the barycentric built-ins flag from #9193); this branch keeps main's version-48 fields and adds the bounds after them. Version 46, 47 and 48 requests still decode, with default depth bounds. ### Tested Head `d4a296d863` (main `d13b3d7c55` merged in), compared with main `d13b3d7c55`, same machine, NVIDIA GeForce RTX 4090 (driver 610.57.04, Vulkan 1.4.341), Linux, Release build, `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (Khronos validation layer 1.4.363) and `VK_LOADER_DEBUG=layer`, so each log shows the loader inserting the layer. Tests selected: `agc_driver_depth_export_clamp`, `agc_shader_memory`, `request_serializer`, `agc_driver_graphics`, `agc_masked_select` and every test with `depth` in its name. | | main `d13b3d7c55` | head `d4a296d863` | |---|---|---| | tests passed / failed / skipped | 42 / 0 / 0 | 43 / 0 / 0 | | `Insert instance layer "VK_LAYER_KHRONOS_validation"` (loader lines) | 44 | 45 | | `was not found but was requested` | 0 | 0 | | `VUID-` lines | 0 | 0 | There are a couple more inserted-layer lines than tests because some tests create more than one Vulkan instance. The extra passing test on the head is `agc_driver_depth_export_clamp`, which does not exist on main. It runs on the device. - A shader that exports 1.5 with a viewport depth range of [0.25, 0.75] leaves 0.75 in the depth buffer, and an export of -0.5 leaves 0.25. With the override set, the exported value is stored as it is. - With the emitter changed to always store the exported value unclamped, `agc_driver_depth_export_clamp` and `agc_shader_memory` fail, so the tests cover the clamp. - The pixel stage decode reads the bounds, passes the disable flag through, rejects bad bounds, and leaves a draw without depth export at 0 and 1. The bounds survive serialization and key the cache. ### Checklist - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [x] No notes, investigation `.md` files or images added to the repository - [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 4090, driver 610.57.04, Linux - [x] AI-assisted: yes
MarcZX10 commented 2026-10-23 19:25:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T19:25:16Z.

Review: CHANGES_REQUESTED


The reported RTX 4090 run covers the changed head, but this PR changes both the AGC driver and shader recompiler. CONTRIBUTING requires the same focused tests on main and this change, on the same machine and GPU, with VK_LAYER_KHRONOS_validation, ANYPS5_NO_SHADER_CACHE=1, and ANYPS5_REQUIRE_VULKAN=1, plus confirmation that the requested layer loaded and a comparison showing no new VUID- errors. Please add those baseline/change results (including the depth-export test and relevant serializer coverage) before merge. I could not dispatch the repository's manual build workflow from this account; GitHub returned HTTP 403 because workflow dispatch requires repository admin rights. My local configure also cannot reach the test build: this checkout has no SDL2 submodule sources and the host lacks XRandR development support, so I did not run the product tests.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5633417658 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10348#pullrequestreview-5633417658). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T19:25:16Z. **Review:** CHANGES_REQUESTED --- The reported RTX 4090 run covers the changed head, but this PR changes both the AGC driver and shader recompiler. CONTRIBUTING requires the same focused tests on `main` and this change, on the same machine and GPU, with `VK_LAYER_KHRONOS_validation`, `ANYPS5_NO_SHADER_CACHE=1`, and `ANYPS5_REQUIRE_VULKAN=1`, plus confirmation that the requested layer loaded and a comparison showing no new `VUID-` errors. Please add those baseline/change results (including the depth-export test and relevant serializer coverage) before merge. I could not dispatch the repository's manual build workflow from this account; GitHub returned HTTP 403 because workflow dispatch requires repository admin rights. My local configure also cannot reach the test build: this checkout has no SDL2 submodule sources and the host lacks XRandR development support, so I did not run the product tests.
adriadam10 commented 2026-10-24 00:47:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-24T00:47:36Z.

Review: DISMISSED


The change reads right: the exported value is NClamped to PA_SC_VPORT_ZMIN_0/ZMAX_0 unless DB_RENDER_OVERRIDE bit 16 is set, the bounds key the draw, the recompile cache (depth-exporting shaders only) and the serializer, and the new tests fail without the clamp. Two things block it:

  • It conflicts with main, which now writes request version 46 (#8564: targetMerge fields at the end of readPixelInfo/writePixelInfo). The bounds must be written and read after the targetMerge block, gated on version >= 47, and the version fixtures in RequestSerializerTests.cpp and ShaderMemory.cpp (base64 prefixes, the byte-stripping legacy fixtures, "future version") need redoing on top of 46. Conflicts are in RequestSerializer.cpp, ShaderMemory.cpp and RequestSerializerTests.cpp.
  • @MarcZX10's change request is still open: CONTRIBUTING needs the same focused tests (agc_driver_depth_export_clamp, agc_shader_memory, request_serializer, the depth tests) on main and on this branch, same machine and GPU, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, confirmation that the layer loaded, and a comparison showing no new VUID- errors. Please add those logs to Tested after rebasing.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5635900020 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10348#pullrequestreview-5635900020). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-24T00:47:36Z. **Review:** DISMISSED --- The change reads right: the exported value is `NClamp`ed to `PA_SC_VPORT_ZMIN_0/ZMAX_0` unless `DB_RENDER_OVERRIDE` bit 16 is set, the bounds key the draw, the recompile cache (depth-exporting shaders only) and the serializer, and the new tests fail without the clamp. Two things block it: - It conflicts with `main`, which now writes request version 46 (#8564: `targetMerge` fields at the end of `readPixelInfo`/`writePixelInfo`). The bounds must be written and read after the `targetMerge` block, gated on `version >= 47`, and the version fixtures in `RequestSerializerTests.cpp` and `ShaderMemory.cpp` (base64 prefixes, the byte-stripping legacy fixtures, "future version") need redoing on top of 46. Conflicts are in `RequestSerializer.cpp`, `ShaderMemory.cpp` and `RequestSerializerTests.cpp`. - @MarcZX10's change request is still open: CONTRIBUTING needs the same focused tests (`agc_driver_depth_export_clamp`, `agc_shader_memory`, `request_serializer`, the depth tests) on `main` and on this branch, same machine and GPU, with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, confirmation that the layer loaded, and a comparison showing no new `VUID-` errors. Please add those logs to `Tested` after rebasing.
adriadam10 commented 2026-10-26 16:20:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-26T16:20:40Z.

Review: DISMISSED


The rebase onto version 46 reads right: the bounds are written after the targetMerge block, read with version >= 47, and the fixtures were redone. @wachi35676's control run (main's emitter with the rest of this PR) fails agc_driver_depth_export_clamp and agc_shader_memory on NVIDIA and AMD, so the tests cover the clamp. Two things are left:

  • @MarcZX10's request is still open and nobody has supplied it: CONTRIBUTING needs the same focused tests (agc_driver_depth_export_clamp, agc_shader_memory, request_serializer, the depth tests) on main and on this head, same machine and GPU, with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, confirmation that the layer loaded (no was not found but was requested by env var VK_INSTANCE_LAYERS in either log), and a comparison showing no new VUID- errors. The Windows runs above don't use the layer. Please add those logs to Tested.
  • The description is out of date: it says main is at version 45 and that this writes 46/47 with other PRs holding 46. Main is at 46 now and this branch writes 47 after its fields. Please correct What and update Tested with the run on the current head.

Also, as wachi35676 noted, only PA_SC_VPORT_ZMIN_0/ZMAX_0 are read, so a draw that selects another viewport is clamped to viewport 0's range; please add a line for that in docs/dev/debt/ for the AGC driver.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5655328699 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10348#pullrequestreview-5655328699). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-26T16:20:40Z. **Review:** DISMISSED --- The rebase onto version 46 reads right: the bounds are written after the `targetMerge` block, read with `version >= 47`, and the fixtures were redone. @wachi35676's control run (main's emitter with the rest of this PR) fails `agc_driver_depth_export_clamp` and `agc_shader_memory` on NVIDIA and AMD, so the tests cover the clamp. Two things are left: - @MarcZX10's request is still open and nobody has supplied it: CONTRIBUTING needs the same focused tests (`agc_driver_depth_export_clamp`, `agc_shader_memory`, `request_serializer`, the depth tests) on `main` and on this head, same machine and GPU, with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, confirmation that the layer loaded (no `was not found but was requested by env var VK_INSTANCE_LAYERS` in either log), and a comparison showing no new `VUID-` errors. The Windows runs above don't use the layer. Please add those logs to `Tested`. - The description is out of date: it says main is at version 45 and that this writes 46/47 with other PRs holding 46. Main is at 46 now and this branch writes 47 after its fields. Please correct `What` and update `Tested` with the run on the current head. Also, as wachi35676 noted, only `PA_SC_VPORT_ZMIN_0`/`ZMAX_0` are read, so a draw that selects another viewport is clamped to viewport 0's range; please add a line for that in `docs/dev/debt/` for the AGC driver.
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!10348
No description provided.