image_bvh_intersect_ray: triangle nodes of type 2 and 3 use the vertices of type 0 #1917
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl#1917
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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:
Vertex
kis at byte12 * k, so v4 is at bytes 48 to 59. The id dword is at byte 60. The barycentric selectors are the bits at8 * typeof 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.rswrites 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
[[0, 1, 2], [1, 3, 2], [2, 3, 4], [2, 4, 0]], with vertexkat byte12 * k.@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
main20cf130: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 / @56the 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(thecore_bvh4.rpfnotes) says the same for the prebuilt BLASes: "triangle = pair: v0..v3, type 0 = (v0, v1, v2), type 1 = (v1, v3, v2); @52/@56 …".bvh::closestreads 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_BVH4is not set on this machine, so I counted no pointers myself).2. The fix is not free.
bvh_intersectis part ofPRELUDE_MEMinrecomp/emit.rs, whichemit.rspushes 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, thet1selects), 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_raybyte-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.
@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_intersectis inPRELUDE_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