fix(shader): mark the vertex position output invariant #2458

Merged
gjonn merged 1 commit from upstream-pr/2458 into main 2026-10-10 06:17:51 +00:00
gjonn commented 2026-10-09 21:54:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gjonn on 2026-10-09T21:54:08Z.


What

The recompiled vertex and mesh shaders now declare their position output Invariant.

A title (PPSA01317) draws a depth pre-pass (reverse Z, compare GREATER, depth writes on) and then draws the same geometry again in its G-buffer pass with compare GREATER_OR_EQUAL. The two passes use different vertex programs that compute the position the same way. On the GPU both give bit-identical depth. In Vulkan, without Invariant the driver is free to compile the same position arithmetic differently in each shader, for example contracting a multiply and an add into an FMA in one and not the other. The second pass's depth then came out slightly smaller and every fragment failed the test. The SPIR-V spec guarantees that an Invariant output gives the same result in different shaders for the same inputs and computation, which is what the hardware already does.

  • PerVertexType (SpirvEmitterHelpers.cpp): the gl_PerVertex block decorates member 0 (BuiltIn Position) Invariant as well. Every non-mesh stage that exports a position writes it through this block.
  • DefineMeshOutputs (SpirvModuleEmitter.cpp): the mesh shader's Position output variable is decorated Invariant.
  • The rect-list tessellation shaders (RectListShaders.cpp) are unchanged. Three corners are copied unchanged from the vertex shader, whose position is now Invariant. The fourth corner is a sum of the other three weighted by +1 or -1, so every product is exact and FMA contraction gives the same result.
  • Invariant may cost a little optimization on position math only. Since the shader disk cache key is derived from the recompiler sources, cached shaders are rebuilt automatically.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0: the new agc_position_invariance test recompiles a vertex shader and a mesh shader that export a position. It checks that every BuiltIn Position in the SPIR-V (the block member or the mesh output variable) is also Invariant. Both checks fail without this change.
  • ctest -R "agc|shader|spirv|recompil": 262 of 262 pass, among them agc_shader_memory, agc_shader_disk_cache, agc_spirv_module, agc_validate_program, agc_driver_graphics, agc_driver_prepared_graphics and agc_driver_mesh. The mesh and graphics tests draw on an RTX 5090 Laptop GPU with the decoration in place.
  • In PPSA01317 (RTX 5090 Laptop GPU), mid-frame dumps showed the G-buffer targets at 0% coverage, and 82% with the depth test forced off for those draws. With Invariant and the depth test on, they reached 42% after 1000 of the frame's draws, the normal progressive fill.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4806272117 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2458). Originally posted by [gjonn](https://github.com/gjonn) on 2026-10-09T21:54:08Z. --- ### What The recompiled vertex and mesh shaders now declare their position output `Invariant`. A title (PPSA01317) draws a depth pre-pass (reverse Z, compare GREATER, depth writes on) and then draws the same geometry again in its G-buffer pass with compare GREATER_OR_EQUAL. The two passes use different vertex programs that compute the position the same way. On the GPU both give bit-identical depth. In Vulkan, without `Invariant` the driver is free to compile the same position arithmetic differently in each shader, for example contracting a multiply and an add into an FMA in one and not the other. The second pass's depth then came out slightly smaller and every fragment failed the test. The SPIR-V spec guarantees that an `Invariant` output gives the same result in different shaders for the same inputs and computation, which is what the hardware already does. - `PerVertexType` (`SpirvEmitterHelpers.cpp`): the gl_PerVertex block decorates member 0 (`BuiltIn Position`) `Invariant` as well. Every non-mesh stage that exports a position writes it through this block. - `DefineMeshOutputs` (`SpirvModuleEmitter.cpp`): the mesh shader's Position output variable is decorated `Invariant`. - The rect-list tessellation shaders (`RectListShaders.cpp`) are unchanged. Three corners are copied unchanged from the vertex shader, whose position is now `Invariant`. The fourth corner is a sum of the other three weighted by +1 or -1, so every product is exact and FMA contraction gives the same result. - `Invariant` may cost a little optimization on position math only. Since the shader disk cache key is derived from the recompiler sources, cached shaders are rebuilt automatically. ### Tested - Windows 11, MinGW-w64 GCC 15.2.0: the new `agc_position_invariance` test recompiles a vertex shader and a mesh shader that export a position. It checks that every `BuiltIn Position` in the SPIR-V (the block member or the mesh output variable) is also `Invariant`. Both checks fail without this change. - `ctest -R "agc|shader|spirv|recompil"`: 262 of 262 pass, among them `agc_shader_memory`, `agc_shader_disk_cache`, `agc_spirv_module`, `agc_validate_program`, `agc_driver_graphics`, `agc_driver_prepared_graphics` and `agc_driver_mesh`. The mesh and graphics tests draw on an RTX 5090 Laptop GPU with the decoration in place. - In PPSA01317 (RTX 5090 Laptop GPU), mid-frame dumps showed the G-buffer targets at 0% coverage, and 82% with the depth test forced off for those draws. With `Invariant` and the depth test on, they reached 42% after 1000 of the frame's draws, the normal progressive fill. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
Kikoarl commented 2026-10-09 22:50:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-09T22:50:10Z.


Tested on Linux (CachyOS x86_64, Intel RPL-S iGPU / Mesa 26.3.0-devel / Vulkan 1.4.363, GCC 16.2.1) at 21f94288:

  • Build: Built cleanly in RAM (/tmp/anyps5_build) with zero warnings/errors.
  • Conventions: python3 tools/check_conventions.py --base origin/main --head HEAD -> PASS (0 errors).
  • Verification: Verified PositionInvariance.cpp and SPIR-V emitter output. Decorating vertex position output with Invariant prevents multi-pass Z-fighting across depth pre-passes.

LGTM!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090594375 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2458#issuecomment-6090594375). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-09T22:50:10Z. --- Tested on Linux (CachyOS x86_64, Intel RPL-S iGPU / Mesa 26.3.0-devel / Vulkan 1.4.363, GCC 16.2.1) at `21f94288`: - **Build**: Built cleanly in RAM (`/tmp/anyps5_build`) with zero warnings/errors. - **Conventions**: `python3 tools/check_conventions.py --base origin/main --head HEAD` -> **PASS (0 errors)**. - **Verification**: Verified `PositionInvariance.cpp` and SPIR-V emitter output. Decorating vertex position output with `Invariant` prevents multi-pass Z-fighting across depth pre-passes. LGTM!
adriadam10 commented 2026-10-10 06:17:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:17:48Z.


Checked on Linux: built the AGC/AgcDriver scope, agc_position_invariance passes and fails with the two emitter changes reverted; the rest of the scoped ctest is green. The decoration follows the SPIR-V Invariant guarantee, and the rect-list reasoning holds. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094576214 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2458#issuecomment-6094576214). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:17:48Z. --- Checked on Linux: built the AGC/AgcDriver scope, `agc_position_invariance` passes and fails with the two emitter changes reverted; the rest of the scoped ctest is green. The decoration follows the SPIR-V `Invariant` guarantee, and the rect-list reasoning holds. Merging.
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!2458
No description provided.