image_bvh_intersect_ray: triangle nodes of type 2 and 3 use the vertices of type 0 #1917

Closed
opened 2026-11-26 05:09:31 +00:00 by github-bot · 2 comments
Owner

@⁠Melow-code on GitHub · 2026-11-24 23:04 UTC · original

What happens

A triangle node pointer has a type in its low 3 bits. The GFX10 triangle node holds five vertices, so types 0 to 3 name four triangles. XCL tests only two of them correctly:

  • crates/xcl/src/gpu/recomp/emit.rs, bvh_intersect: bool t1 = type == 1u; selects (v1, v3, v2) for type 1. Every other type uses (v0, v1, v2).
  • crates/xcl/src/gpu/bvh.rs, intersect_node: let v = if ty == 1 { ... } else { ... } does the same.

So a ray against a type 2 or type 3 triangle tests triangle 0 instead.

What the hardware does

AnyPS5 measured this on an RDNA2 GPU (gfx1036) in boykopovar/AnyPS5#24128:

type vertices
0 v0, v1, v2
1 v1, v3, v2
2 v2, v3, v4
3 v2, v4, v0

Vertex k is at byte 12 * k, so v4 is at bytes 48 to 59. The id dword is at byte 60. The barycentric selectors are the bits at 8 * type of that dword, as XCL already reads them.

The same measurement shows one more difference, on a miss in the barycentric return mode. The hardware returns an infinity with the sign of the denominator. It also returns the real denominator and the barycentric numerators. XCL returns (+inf, 1.0, 0, 0).

When it matters

It matters only if XboxBvhBuilder packs more than two triangles in a node. I did not check this. To check it, look at the box nodes that the builder records write. Count the child pointers with (pointer & 7) equal to 2 or 3.

The CPU builder in bvh.rs writes ids at bytes 48 to 59, which are the v4 slot. That is correct for its two-triangle nodes. A fix must keep that format for XCL-built nodes and use the five-vertex layout for title-built nodes.

Suggested fix

  1. Use one vertex table in both files: [[0, 1, 2], [1, 3, 2], [2, 3, 4], [2, 4, 0]], with vertex k at byte 12 * k.
  2. Add one GPU test per type, with a ray inside and a ray outside each triangle.
  3. Measure the miss return on hardware before you change it.
**@⁠Melow-code** on GitHub · 2026-11-24 23:04 UTC · [original](https://github.com/xcl-dev/xcl/issues/1917) ## What happens A triangle node pointer has a type in its low 3 bits. The GFX10 triangle node holds five vertices, so types 0 to 3 name four triangles. XCL tests only two of them correctly: - `crates/xcl/src/gpu/recomp/emit.rs`, `bvh_intersect`: `bool t1 = type == 1u;` selects (v1, v3, v2) for type 1. Every other type uses (v0, v1, v2). - `crates/xcl/src/gpu/bvh.rs`, `intersect_node`: `let v = if ty == 1 { ... } else { ... }` does the same. So a ray against a type 2 or type 3 triangle tests triangle 0 instead. ## What the hardware does AnyPS5 measured this on an RDNA2 GPU (gfx1036) in boykopovar/AnyPS5#24128: | type | vertices | |---|---| | 0 | v0, v1, v2 | | 1 | v1, v3, v2 | | 2 | v2, v3, v4 | | 3 | v2, v4, v0 | Vertex `k` is at byte `12 * k`, so v4 is at bytes 48 to 59. The id dword is at byte 60. The barycentric selectors are the bits at `8 * type` of that dword, as XCL already reads them. The same measurement shows one more difference, on a miss in the barycentric return mode. The hardware returns an infinity with the sign of the denominator. It also returns the real denominator and the barycentric numerators. XCL returns (+inf, 1.0, 0, 0). ## When it matters It matters only if XboxBvhBuilder packs more than two triangles in a node. I did not check this. To check it, look at the box nodes that the builder records write. Count the child pointers with `(pointer & 7)` equal to 2 or 3. The CPU builder in `bvh.rs` writes ids at bytes 48 to 59, which are the v4 slot. That is correct for its two-triangle nodes. A fix must keep that format for XCL-built nodes and use the five-vertex layout for title-built nodes. ## Suggested fix 1. Use one vertex table in both files: `[[0, 1, 2], [1, 3, 2], [2, 3, 4], [2, 4, 0]]`, with vertex `k` at byte `12 * k`. 2. Add one GPU test per type, with a ray inside and a ray outside each triangle. 3. Measure the miss return on hardware before you change it.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:09:31 +00:00
Author
Owner

@⁠b3IKK on GitHub · 2026-11-24 23:51 UTC · original

I looked at this to make the fix, and stopped before writing it. Two things from the tree, read at main 20cf130:

1. The repository already answers "does the builder pack more than two triangles in a node": not for this builder.

  • crates/xcl/src/gpu/bvh.rs, module header: the triangle node is the "AMD GPURT gfx10 layout (confirmed on the console builder's own output): a pair of triangles, v0..v3", with @48 = geometry index << 8 | triangle 1 present << 7 | opaque, @52 / @56 the primitive index of triangle 0 / 1, and "box nodes point at type 0 only; the game's shader tests type 1 next when bit 7 is set".
  • HANDOFF.md (the core_bvh4.rpf notes) says the same for the prebuilt BLASes: "triangle = pair: v0..v3, type 0 = (v0, v1, v2), type 1 = (v1, v3, v2); @⁠52/@56 …".
  • So in XboxBvhBuilder's nodes bytes 48–59 are the geometry and primitive words, not a fifth vertex, and no pointer of type 2 or 3 is made. bvh::closest reads those words at exactly these offsets.

That is from the notes of whoever measured the builder's output, not from a run of mine (XCL_SAMPLE_BVH4 is not set on this machine, so I counted no pointers myself).

2. The fix is not free. bvh_intersect is part of PRELUDE_MEM in recomp/emit.rs, which emit.rs pushes into the HLSL of every translated shader, and the DXIL cache is keyed by that HLSL (dxc::cache_key). Changing the function's text makes every cached DXIL of every title miss, including the ones people precompiled for #1269. The direct DXIL translator has its own copy (gpu/dxil/lower_image.rs, the t1 selects), so it is three places, not two.

The vertex table itself looks right to me: (v0, v1, v2), (v1, v3, v2), (v2, v3, v4), (v2, v4, v0) is a fan around v2 over the ring v0 → v1 → v3 → v4, and types 0 and 1 are what XCL has. I did not check the AnyPS5 measurement.

So my suggestion is to leave the emulation as it is until a title is seen whose nodes carry a type 2 or 3 pointer, and then to add the two cases in a way that keeps the HLSL of programs without image_bvh*_intersect_ray byte-identical. I have not changed anything, and I did not look at the miss return.

AI use: OpenCode, Claude Fable 5.2 read the code and wrote this comment.

**@⁠b3IKK** on GitHub · 2026-11-24 23:51 UTC · [original](https://github.com/xcl-dev/xcl/issues/1917#issuecomment-7147876023) I looked at this to make the fix, and stopped before writing it. Two things from the tree, read at `main` `20cf130`: **1. The repository already answers "does the builder pack more than two triangles in a node": not for this builder.** - `crates/xcl/src/gpu/bvh.rs`, module header: the triangle node is the "AMD GPURT gfx10 layout (confirmed on the console builder's own output): a pair of triangles, v0..v3", with `@48 = geometry index << 8 | triangle 1 present << 7 | opaque`, `@52 / @56` the primitive index of triangle 0 / 1, and "box nodes point at type 0 only; the game's shader tests type 1 next when bit 7 is set". - `HANDOFF.md` (the `core_bvh4.rpf` notes) says the same for the prebuilt BLASes: "triangle = **pair**: v0..v3, type 0 = (v0, v1, v2), type 1 = (v1, v3, v2); @⁠52/@56 …". - So in XboxBvhBuilder's nodes bytes 48–59 are the geometry and primitive words, not a fifth vertex, and no pointer of type 2 or 3 is made. `bvh::closest` reads those words at exactly these offsets. That is from the notes of whoever measured the builder's output, not from a run of mine (`XCL_SAMPLE_BVH4` is not set on this machine, so I counted no pointers myself). **2. The fix is not free.** `bvh_intersect` is part of `PRELUDE_MEM` in `recomp/emit.rs`, which `emit.rs` pushes into the HLSL of every translated shader, and the DXIL cache is keyed by that HLSL (`dxc::cache_key`). Changing the function's text makes every cached DXIL of every title miss, including the ones people precompiled for #1269. The direct DXIL translator has its own copy (`gpu/dxil/lower_image.rs`, the `t1` selects), so it is three places, not two. The vertex table itself looks right to me: (v0, v1, v2), (v1, v3, v2), (v2, v3, v4), (v2, v4, v0) is a fan around v2 over the ring v0 → v1 → v3 → v4, and types 0 and 1 are what XCL has. I did not check the AnyPS5 measurement. So my suggestion is to leave the emulation as it is until a title is seen whose nodes carry a type 2 or 3 pointer, and then to add the two cases in a way that keeps the HLSL of programs without `image_bvh*_intersect_ray` byte-identical. I have not changed anything, and I did not look at the miss return. AI use: OpenCode, Claude Fable 5.2 read the code and wrote this comment.
Author
Owner

@⁠Melow-code on GitHub · 2026-11-25 12:05 UTC · original

Good analysis, thanks. The builder's pair layout (types 0 and 1 only, confirmed by the GPURT gfx10 notes and HANDOFF.md) means no node carries a type 2 or 3 pointer today. The vertex table is correct for a quad fan; the gap is in the intersection code, which falls through to type 0's vertices for 2 and 3, but nothing reaches that path.

The cache invalidation cost is real: bvh_intersect is in PRELUDE_MEM, and changing its text blows every title's DXIL cache.

Agreed: leave this as a known gap and add the two cases only when a title or a future builder emits type 2 or 3. No change needed now.

🤖 Generated with Claude Code

**@⁠Melow-code** on GitHub · 2026-11-25 12:05 UTC · [original](https://github.com/xcl-dev/xcl/issues/1917#issuecomment-7161899916) Good analysis, thanks. The builder's pair layout (types 0 and 1 only, confirmed by the GPURT gfx10 notes and HANDOFF.md) means no node carries a type 2 or 3 pointer today. The vertex table is correct for a quad fan; the gap is in the intersection code, which falls through to type 0's vertices for 2 and 3, but nothing reaches that path. The cache invalidation cost is real: `bvh_intersect` is in `PRELUDE_MEM`, and changing its text blows every title's DXIL cache. Agreed: leave this as a known gap and add the two cases only when a title or a future builder emits type 2 or 3. No change needed now. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
This discussion has been locked. Commenting is limited to contributors.
No description provided.