fix(shader): intersect BVH triangle types 2 and 3 and return triangle ids and misses as RDNA2 does #24128

Merged
MichonGoddijn231849 merged 1 commit from upstream-pr/24128 into main 2026-11-27 13:41:08 +00:00
MichonGoddijn231849 commented 2026-11-20 07:04:22 +00:00 (Migrated from github.com)

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


What

image_bvh_intersect_ray and image_bvh64_intersect_ray were ported from AMD's GPURT software fallback (9bb94163ea, bf635fae68) and had not been compared with hardware. The gfx1036 hardware oracle shows three ways its triangle node test differs, all fixed in EmitImageBvhIntersectRay:

  • Node types 2 and 3 are triangles. gfx1036 intersects them as (v2, v3, v4) and (v2, v4, v0) of the five-vertex node (v4 at dwords 12 to 14, the id at dword 15), next to types 0 (v0, v1, v2) and 1 (v1, v3, v2). AnyPS5 returned four invalid dwords for them. The barycentric selectors come from the id's bits at 8 × type, as for types 0 and 1.
  • The triangle-id return is the node's id plus its type. Hardware returns id, id + 1, id + 2 and id + 3 for types 0 to 3, wrapping at 2^32, on hits and misses. AnyPS5 returned the id for every type.
  • A miss with barycentrics returned keeps the denominator. On a miss, gfx1036 returns t = +inf or −inf with the denominator's sign, the real denominator, and the barycentric numerators computed from it. AnyPS5 returned +inf over 1.0 and recomputed the third barycentric with 1.0. The triangle-id return of a miss (+inf, 1.0, id + type, 0) was already right.

The t / denominator of a hit and the hit/miss decision are unchanged.

Debt: a new entry records the rule, and that hits are f32 Möller–Trumbore whose last bits differ from the hardware's. A zero or NaN denominator and the PS5's gfx1013 are not measured.

Measurement: tools/hw-oracle on gfx1036 (AMD Windows driver 32.0.21045.16010), with the BVH in the oracle's buffer.

  • Descriptor: base >> 8 in dwords 0 and 1, size 0x3ff_ffffffff, type 8 in dword 3 bits 28 to 31 as RADV builds it, and the return mode in dword 3 bit 24.
  • Comparison: agc_oracle_runner running the same code on an NVIDIA GeForce RTX 5080 (610.74) and the gfx1036 iGPU through Vulkan.
  • Rays: aimed inside, outside and behind random triangles.
node type × return mode (gfx1036 iGPU, 4 rays each) main this branch
type 0, ids same structure, last bits differ same
type 1, ids id instead of id + 1 id + 1
types 2 and 3, ids all four dwords 0xffffffff hit/miss, id + type as hardware
type 0 or 1, barycentrics, miss +inf over 1.0 with a different third barycentric ±inf, real denominator and barycentrics
types 2 and 3, barycentrics 0xffffffff hit/miss and selected barycentrics as hardware

After the fix, all 32 rows (types 0 to 3, both return modes, seed 5, id 0x09060000) agree with hardware on hit or miss, the id, the infinity's sign and which barycentric is returned. The float bits match exactly on the 4 triangle-id misses. On the others they are within a few ULP: the denominator 0x413decd1 vs 0x413decd6, for example.

Tested

Windows 11 Pro 10.0.26200, MinGW Release build. ctest --test-dir build -R '^agc_driver_bvh|^ray_tracing_miss$' -V with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer, on main and on this branch. The GPUs were an NVIDIA GeForce RTX 5080 (610.74) and an AMD Radeon(TM) Graphics iGPU (gfx1036, 32.0.21045.16010).

main NVIDIA main AMD branch NVIDIA branch AMD
Result 2/2 2/2 2/2 2/2
Insert instance layer "VK_LAYER_KHRONOS_validation" 1 1 1 1
was not found but was requested 0 0 0 0
VUID- 0 0 0 0

ray_tracing_miss opens no Vulkan device, hence one layer insert for two tests.

agc_driver_bvh64_intersect_ray changes:

  • Node and cases: the triangle node gets v4 = (2, 0, 5). Two cases were added: type 2 (a miss) and type 3 (a hit at t = 10 / 2). The type 1 case now expects id + 1.
  • Barycentric pass: every case runs a second time with the barycentric return (dword 3 bit 24), in all four forms (image_bvh64_intersect_ray and image_bvh_intersect_ray, with and without A16) and both addressing modes.
  • Hardware check: all eight triangle results of the test's geometry (types 0 to 3, both return modes) were run on the gfx1036 hardware oracle first, and they match the test's expectations bit for bit.

Negative check: with main's SpirvImageEmitter.cpp, the test fails on both GPUs with image_bvh64_intersect_ray, address in the node pointer, triangle ids: thread 3 dword 2 is 11256099, expected 11256100.

tools/pr_overlap.py could not compare SpirvImageEmitter.cpp with 15 PRs that conflict with main. None of their diffs touches EmitImageBvhIntersectRay. Not tested on a PS5.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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 5080, driver 610.74, Windows 11 Pro 10.0.26200; AMD Radeon(TM) Graphics (RDNA2 iGPU, gfx1036), driver 32.0.21045.16010, Windows 11 Pro 10.0.26200
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5409269075 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24128). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-11-20T07:04:22Z. --- ### What `image_bvh_intersect_ray` and `image_bvh64_intersect_ray` were ported from AMD's GPURT software fallback (9bb94163ea, bf635fae68) and had not been compared with hardware. The gfx1036 hardware oracle shows three ways its triangle node test differs, all fixed in `EmitImageBvhIntersectRay`: - **Node types 2 and 3 are triangles.** gfx1036 intersects them as (v2, v3, v4) and (v2, v4, v0) of the five-vertex node (v4 at dwords 12 to 14, the id at dword 15), next to types 0 (v0, v1, v2) and 1 (v1, v3, v2). AnyPS5 returned four invalid dwords for them. The barycentric selectors come from the id's bits at 8 × type, as for types 0 and 1. - **The triangle-id return is the node's id plus its type.** Hardware returns id, id + 1, id + 2 and id + 3 for types 0 to 3, wrapping at 2^32, on hits and misses. AnyPS5 returned the id for every type. - **A miss with barycentrics returned keeps the denominator.** On a miss, gfx1036 returns t = +inf or −inf with the denominator's sign, the real denominator, and the barycentric numerators computed from it. AnyPS5 returned +inf over 1.0 and recomputed the third barycentric with 1.0. The triangle-id return of a miss (+inf, 1.0, id + type, 0) was already right. The `t / denominator` of a hit and the hit/miss decision are unchanged. **Debt:** a new entry records the rule, and that hits are f32 Möller–Trumbore whose last bits differ from the hardware's. A zero or NaN denominator and the PS5's gfx1013 are not measured. **Measurement:** `tools/hw-oracle` on gfx1036 (AMD Windows driver 32.0.21045.16010), with the BVH in the oracle's buffer. - **Descriptor:** base >> 8 in dwords 0 and 1, size 0x3ff_ffffffff, type 8 in dword 3 bits 28 to 31 as RADV builds it, and the return mode in dword 3 bit 24. - **Comparison:** `agc_oracle_runner` running the same code on an NVIDIA GeForce RTX 5080 (610.74) and the gfx1036 iGPU through Vulkan. - **Rays:** aimed inside, outside and behind random triangles. | node type × return mode (gfx1036 iGPU, 4 rays each) | `main` | this branch | | --- | --- | --- | | type 0, ids | same structure, last bits differ | same | | type 1, ids | id instead of id + 1 | id + 1 | | types 2 and 3, ids | all four dwords 0xffffffff | hit/miss, id + type as hardware | | type 0 or 1, barycentrics, miss | +inf over 1.0 with a different third barycentric | ±inf, real denominator and barycentrics | | types 2 and 3, barycentrics | 0xffffffff | hit/miss and selected barycentrics as hardware | After the fix, all 32 rows (types 0 to 3, both return modes, seed 5, id 0x09060000) agree with hardware on hit or miss, the id, the infinity's sign and which barycentric is returned. The float bits match exactly on the 4 triangle-id misses. On the others they are within a few ULP: the denominator 0x413decd1 vs 0x413decd6, for example. ### Tested Windows 11 Pro 10.0.26200, MinGW Release build. `ctest --test-dir build -R '^agc_driver_bvh|^ray_tracing_miss$' -V` with `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer`, on `main` and on this branch. The GPUs were an NVIDIA GeForce RTX 5080 (610.74) and an AMD Radeon(TM) Graphics iGPU (gfx1036, 32.0.21045.16010). | | `main` NVIDIA | `main` AMD | branch NVIDIA | branch AMD | | --- | --- | --- | --- | --- | | Result | 2/2 | 2/2 | 2/2 | 2/2 | | `Insert instance layer "VK_LAYER_KHRONOS_validation"` | 1 | 1 | 1 | 1 | | `was not found but was requested` | 0 | 0 | 0 | 0 | | `VUID-` | 0 | 0 | 0 | 0 | `ray_tracing_miss` opens no Vulkan device, hence one layer insert for two tests. `agc_driver_bvh64_intersect_ray` changes: - **Node and cases:** the triangle node gets v4 = (2, 0, 5). Two cases were added: type 2 (a miss) and type 3 (a hit at t = 10 / 2). The type 1 case now expects id + 1. - **Barycentric pass:** every case runs a second time with the barycentric return (dword 3 bit 24), in all four forms (`image_bvh64_intersect_ray` and `image_bvh_intersect_ray`, with and without A16) and both addressing modes. - **Hardware check:** all eight triangle results of the test's geometry (types 0 to 3, both return modes) were run on the gfx1036 hardware oracle first, and they match the test's expectations bit for bit. Negative check: with `main`'s `SpirvImageEmitter.cpp`, the test fails on both GPUs with `image_bvh64_intersect_ray, address in the node pointer, triangle ids: thread 3 dword 2 is 11256099, expected 11256100`. `tools/pr_overlap.py` could not compare `SpirvImageEmitter.cpp` with 15 PRs that conflict with `main`. None of their diffs touches `EmitImageBvhIntersectRay`. Not tested on a PS5. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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 5080, driver 610.74, Windows 11 Pro 10.0.26200; AMD Radeon(TM) Graphics (RDNA2 iGPU, gfx1036), driver 32.0.21045.16010, Windows 11 Pro 10.0.26200 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-20 07:26:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T07:26:31Z.

Review: APPROVED


Reviewed — looks good

The reviewed change is consistent with its stated goal; no blocking defects were identified in the changed files.

What I ran

  • Reviewed head ff9463e8bb223ab2cd140d22137409476844d655 using repo_read.py.
  • No build or test commands were run; the test results in the PR description are author-reported.

Results

Check Result Note
Changed code review passed All three changed files read
Build and tests not run Author-reported Vulkan results not independently verified

Findings

Blocking

None.

Non-blocking

None.

Not verified

  • Build and runtime behavior, including the reported hardware oracle comparisons, were not independently verified.
  • Read: core/libs/prx/libSceAgcDriver/tests/execution/Bvh64IntersectRay.cpp, core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp, and docs/dev/debt/changes/fix-bvh-triangle-types.md.
  • Excluded files: none; all changed files listed in the manifest were included and read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5988974162 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24128#pullrequestreview-5988974162). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T07:26:31Z. **Review:** APPROVED --- # Reviewed — looks good The reviewed change is consistent with its stated goal; no blocking defects were identified in the changed files. ## What I ran - Reviewed head `ff9463e8bb223ab2cd140d22137409476844d655` using `repo_read.py`. - No build or test commands were run; the test results in the PR description are author-reported. ## Results | Check | Result | Note | | --- | --- | --- | | Changed code review | passed | All three changed files read | | Build and tests | not run | Author-reported Vulkan results not independently verified | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified - Build and runtime behavior, including the reported hardware oracle comparisons, were not independently verified. - Read: `core/libs/prx/libSceAgcDriver/tests/execution/Bvh64IntersectRay.cpp`, `core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp`, and `docs/dev/debt/changes/fix-bvh-triangle-types.md`. - Excluded files: none; all changed files listed in the manifest were included and read. ## To get this to a good state Nothing is required.
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!24128
No description provided.