Skip to content

D3D-412: TLAS instances fold the BLAS leaf frame and store +64 in the console order - #4011

Open
wildheathen wants to merge 1 commit into
ItsNotRexed:mainfrom
wildheathen:fix/d3d412-leaf-frame
Open

wildheathen wants to merge 1 commit into
ItsNotRexed:mainfrom
wildheathen:fix/d3d412-leaf-frame

Conversation

@wildheathen

@wildheathen wildheathen commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D-412, claim (from the finding).

What and why

A console BLAS keeps its leaves and nodes in the frame of its compressionParams (docs/bvh.md §4a): (R * v - t) * s. Its sceneBounds stay in object space. The console's LoadInstances (in XboxBvhBuilder.dll, read from its DXIL) does two things whenever any of bits 24-31 is set:

  • it folds that frame into each instance's world-to-object matrix;
  • it ORs INST_SCALED_LOCAL_SPACE (0x40000000) into the root-pointer word.

A traversal then only applies the matrix at record +64.

The same 12 floats had a second difference: their order. The console does not store the world-to-object matrix as a row-major 3x4. It stores its translation first (+64, +68, +72), then the 3x3 row by row (+76, +88, +100). +0 keeps the D3D12 object-to-world 3x4 as given. Both Leonida builders and trace() used row-major 3x4 at +64, which is self-consistent but not what the game's RT shaders read: they would take (1, 0, 0) as the translation of an identity instance. This PR writes and reads the console order. Evidence (offsets and counts only):

  • LoadInstances' stores put (t0, t1, t2, r00) at +64, (r01, r02, r10, r11) at +80 and (r12, r20, r21, r22) at +96, traced back to the inverse it computes.

  • All 64 RT compute shaders in the shader archive (40 image_bvh_intersect_ray, 24 image_bvh64_intersect_ray) read the instance record the same way. Their instance branch loads +48..+63 and the 12 floats. It masks the +56 word with 0x3FFFFFFF and tests only its bit 31, never 0x40000000 and never the BLAS's +0x3C. In the 32-bit ones, row 0 of the transform uses +76/+80/+84 for x, y and z, and the identity-rotation path adds only +64/+68/+72. Both Leonida TLAS builders stored the plain inverse instead and never read the BLAS's +0x3C. That is invisible with their own BLAS, which are uncompressed, but every BLAS of a shipped *_bvh4 pack has a frame: all 1,237 of shared_minigame000_bvh4 and all 271,627 of core_bvh4 set bit 31. A TLAS over rehydrated console BLAS (RT-212, TEST-376) would therefore be traversed in the wrong space.

  • tools/xdxr_bvh.py:

    • leaf_frame / to_leaf_frame decode the word;
    • build_blas(..., compression=) builds a BLAS in a frame, as console BLAS are: leaves and nodes in the frame, sceneBounds in object space, the word at +60;
    • build_tlas folds (R * M^-1 - t) * s into +64 and sets bit 30. The identity flag needs a clear scale bit and rotation index 0, as in LoadInstances;
    • +64 is written (_pack_world_to_object) and read in trace() (_unpack_world_to_object) in the console order;
    • check_build takes a BLAS's object-space triangles (object_triangles) for the instances that place it.
  • runtime/src/xdxr_build.comp build_instance: the same composition, flags and +64 order. xdxr_build.inc is regenerated with the command in xdxr_build_spv.h (glslc from shaderc v2026.4; the unchanged source regenerates byte-identical).

  • runtime/test/vk_xdxr_build_test.cpp: two BLAS in a frame (scale plus translation; scale plus rotation index 21), built from triangles mapped into the frame and then given object-space bounds and the word, and a TLAS of three instances over them. xdxr_build_check traces it against brute force.

  • docs/bvh.md: §4a and the INSTANCE row say what the traversal sees, the +64 order included.

Tested

Windows 11, RTX 5080 (driver 621.24), MSVC 14.51, Python 3.14:

  • python -I tests/test_xdxr_bvh.py: 38 tests OK. test_world_to_object_is_translation_then_rows pins the +64 order with a rotated and shifted instance. It and the four existing assertions on +64 fail with the row-major order. The four new LeafFrame tests fail before the change (with build_tlas storing the plain inverse, the TLAS gives other hits than brute force, and the record has no bit 30); with the fold disabled again, all four fail.
  • vk_xdxr_build_test + xdxr_bvh.py check-build, before the shader change: tlas_leaf_frame 258 of 1,000 rays differ from brute force. After it, every case agrees with brute force, blas_leaf_frame, blas_leaf_frame_rotated and tlas_leaf_frame included (ctest runs it with 1,000 rays; see below).
  • The whole runtime built with MSVC, then ctest, rebased on main aa2988cc (xdxr_build.inc regenerated from the merged shader; the unchanged main shader regenerates its own .inc byte-identical with this glslc): 153 of 153 pass. d3d12_xdxr_rt_check traces the D3D12 layer's own TLAS, so the GPU builder and the walker agree on the new order. On the way, before the tolerance change below, 131 of 132 passed. The one failure was xdxr_build_check on the existing tlas case: the new cases shift the shared random ray sequence, and one new ray hits the same triangle at t = 1.62e-4, 2e-8 away from brute force. That is above rel_tol=1e-4, but it is the float32 inverse's absolute rounding, so the TLAS comparison gains abs_tol=1e-6. The same tlas.bin passes 1,000 rays with the old sequence. After the change, the six xdxr/bvh tests pass, xdxr_build_check included.
  • python -m pytest tests -q --tb=no --ignore=tests/semantic on this branch and on its parent f847748c: the same 41 failures in both (tools that need numpy/Pillow, absent here, and the duplicated docs rows), and 12 more passing on the branch.
  • The rotation: LoadInstances holds its 64 rotations as a 576-float table. All 64 equal _rotation(i) bit for bit, with the table read as columns. The shader applies them as R * v (column k of R times component k), to M^-1's columns and its translation, which is the (R * M^-1 - t) * s the builders write. Both builders also match brute force with rotation index 21.
  • Not verified:
    • a real BLAS with a non-zero rotation index: there is none in the shipped data. All 1,237 BLAS of shared_minigame000_bvh4 and all 271,627 of core_bvh4 set bit 31 (1,237 and 28,410 of them bit 30), and none uses a rotation;
    • that the +64 words are bit-identical to the console's, since the console composes in float32 dot4 steps and the CPU encoder in double, rounded once (the order is verified, the last bits are not);
    • the TLAS node bounds the console derives from the leaf-frame root box, where this PR maps sceneBounds;
    • running a game RT shader over a TLAS from these builders: the reading side is checked in the disassembly only.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md): synthetic scenes only. The DXIL reading and the pack counts were done locally, and only offsets and counts are quoted.
  • A new translator rule is in the contract table of docs/design.md: not applicable, the translator is not touched.

AI-assisted.

… console order

A console BLAS keeps its leaves and nodes in the frame of its compressionParams (docs/bvh.md 4a):
(R * v - t) * s, sceneBounds in object space. XboxBvhBuilder.dll's LoadInstances folds that frame into
each instance's world-to-object matrix whenever any of bits 24-31 is set, and sets
INST_SCALED_LOCAL_SPACE in the root-pointer word. Both Leonida TLAS builders stored the plain inverse
and never read the BLAS's +0x3C. Every BLAS of the shipped *_bvh4 packs has a frame.

The same 12 floats were also in the wrong order. The console stores the translation first (+64..+72)
and then the 3x3 row by row (+76, +88, +100), and the game's RT compute shaders read them that way.
Leonida wrote and read a row-major 3x4 there.

- tools/xdxr_bvh.py: leaf_frame / to_leaf_frame; build_blas(compression=) builds a BLAS in a frame;
  build_tlas folds the frame and sets bit 30 (the identity flag needs a clear scale bit and rotation 0);
  +64 is packed and read in trace() in the console order; check_build takes a BLAS's
  object_triangles for the instances that place it, and its TLAS t comparison gains abs_tol=1e-6.
- runtime/src/xdxr_build.comp build_instance: the same composition, flags and order;
  xdxr_build.inc regenerated with the command in xdxr_build_spv.h.
- runtime/test/vk_xdxr_build_test.cpp: two leaf-frame BLAS (scale and translation; scale and rotation
  21) and a TLAS over them, checked by xdxr_build_check.
- tests/test_xdxr_bvh.py: LeafFrame tests and the +64 order; docs/bvh.md: 4a and the INSTANCE row.
@whattheflop

Copy link
Copy Markdown
Contributor

Independent RTX 3080 / NVIDIA 616.64 verification of rebased 69ce3a44. All 11 positive synthetic builds matched brute force across 10,200 rays, with zero Vulkan validation errors. An independent transform and bounds oracle on the GPU and CPU TLAS records also matched 96 aimed hits and six misses per builder, including a mix of framed and plain BLAS.

I independently regenerated the shader after combining the GLSL with main's radix-offset change. The result exactly matches the published rebased blob f27cf245 and passes Vulkan 1.2 spirv-val. Full GPU and readback report. AI-assisted (Codex).

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the rebased head 69ce3a4. The added leaf-frame transform and TLAS instance layout are covered by the reported independent GPU and CPU-oracle checks, and the regenerated shader matches the documented source change. I found no distinct blocking issue.

@requiemsnkrp

Copy link
Copy Markdown

Tested 69ce3a44 (merged onto main aa2988cc) on NVIDIA GeForce RTX 3060, driver 32.0.16.1714, Windows 11, MSVC 19.51, Vulkan SDK 1.4.363.0: pass.

  • With LEONIDA_VK_VALIDATION=1: xdxr_build passes (tlas_leaf_frame: 3 instances over two leaf-frame BLAS, 640 bytes). xdxr_build_check passes in 73.6 s with blas_leaf_frame: ... 0 validation errors, 0/1000 rays differ (950 hits), blas_leaf_frame_rotated: ... 0/1000 rays differ (56 hits), tlas_leaf_frame: 640 bytes, 0 validation errors, 0/1000 rays differ (184 hits), and tlas, tlas_refit, tlas_refit_clone at 0 differing.
  • d3d12_xdxr_rt and d3d12_xdxr_rt_check (d3d12_tlas: ... 0/1000 rays differ) and xdxr_descs pass; python -I tests/test_xdxr_bvh.py: Ran 42 tests ... OK.
  • Full ctest (-E "present|swapchain|frame_pacer", llvm-mc build): 100% tests passed, 0 tests failed out of 148 (9 skipped/disabled, same set as main: shader_library_corpus, bink_decode, bink_play_headless, fsr_dispatch, bvh_builder, bink_audio, gameinput, d3d12_rgb9e5_rtv, d3d12_first_light).
  • This PR and XDXR builds: ARRAY_OF_POINTERS instances and the 16-bit vertex formats #3944 both regenerate runtime/src/xdxr_build.inc and change xdxr_build.comp and tools/xdxr_bvh.py, so whichever merges second will need a rebase and a regenerated .inc.

AI-assisted (Claude). No game files.

@Parakeet-Wheat

Copy link
Copy Markdown
Contributor

Tested 69ce3a4 on Windows 11 26300, MSVC (VS 2022), Windows SDK 10.0.26100, RTX 3070 (610.88): pass. Full ctest is 153/153 with 0 build warnings, and tests/test_xdxr_bvh.py runs 42 OK. That agrees with @requiemsnkrp. With the SDK's glslc (shaderc v2026.4), xdxr_build.comp also compiles to a .inc byte-identical to the committed one, as @whattheflop found. Two things nobody has posted yet:

Negative control. With main's xdxr_build.inc back on this head and nothing else changed, xdxr_build_check fails on every TLAS case and nothing else:

case rays that differ from brute force
tlas 724 / 1000
tlas_refit 607 / 1000
tlas_refit_clone 622 / 1000
tlas_leaf_frame 223 / 1000
all 16 BLAS cases, blas_leaf_frame and blas_leaf_frame_rotated included 0

So the check catches a GPU builder that writes the old +64 order or skips the leaf-frame fold, and ties it to trace() on both changes.

The GLSL and Python rotation tables are the same. Your evidence compares _rotation(i) with LoadInstances' table. I also compared the GLSL leaf_rotation() with _rotation():

  • They have the same row layout (transpose(mat3(...)) written row by row) and the same Rz * (Ry * Rx) order.
  • The three GLSL literals are bit-identical to f32(cos(k*pi/8)) for k = 1, 2, 3 (0x3f6c835e, 0x3f3504f3, 0x3ec3ef15).

Only the last bit of a product can differ, since the GPU may fuse multiply-adds. With the PR's own blob, all 20 cases agree with brute force, so that last bit doesn't change a hit here.

AI-assisted (Claude Code).

@FaKL-Code

Copy link
Copy Markdown
Contributor

Tested 69ce3a4 (merged onto main aa2988c) on NVIDIA GeForce RTX 4060 Ti, driver 610.88 (32.0.16.1088), Windows 11 26200, MSVC 14.51.36231 (VS 2026), Vulkan SDK 1.4.363.0: pass

  • Merges cleanly; no new compiler warnings against main.
  • ctest: 144 of 144 pass (same 9 skips as main). Loader test passes; validation run: 56 tests clean.
  • ctest -R "xdxr|bvh" -V with LEONIDA_VK_VALIDATION=1: all 5 run pass (bvh_builder skips, as on main). In xdxr_build_check every case reports "0 validation errors, 0/1000 rays differ", the new blas_leaf_frame (950 hits), blas_leaf_frame_rotated (56 hits) and tlas_leaf_frame (184 hits) included. d3d12_xdxr_rt_check: d3d12_tlas 0/1000 differ, so the layer's own TLAS and the walker agree on the new +64 order here too.
  • python -I tests/test_xdxr_bvh.py: "Ran 42 tests ... OK".
  • xdxr_build.inc: the SDK's glslc here is also shaderc v2026.4. With the command in xdxr_build_spv.h, this PR's xdxr_build.comp regenerates the committed .inc byte-identical, and main's shader its own .inc byte-identical.
  • As a cross-check, I put main's xdxr_build.inc back into this branch and rebuilt vk_xdxr_build_test: xdxr_build_check fails, with tlas 724, tlas_leaf_frame 223, tlas_refit 607 and tlas_refit_clone 622 of 1000 rays differing from brute force; all BLAS cases still agree. That's the expected split: the old shader writes +64 row-major, and this branch's trace() reads it translation first. File restored afterwards.

AI-assisted (Claude).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants