Skip to content

xdxr: check the update source and bound the builder's reads (RT-XDXR-UPDATE-SOURCE-CHECK) - #2222

Merged
aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
DadaCap:xdxr-update-source-check
Nov 26, 2026
Merged

aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
DadaCap:xdxr-update-source-check

Conversation

@DadaCap

@DadaCap DadaCap commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #1608 (comment)

Depends on: none. It merges cleanly with current main, #2002, #1874 and #1418. It conflicts with #690 in runtime/src/xdxr_build.comp, runtime/src/xdxr_build.h and the generated runtime/src/xdxr_build.inc (both change the builder's modes and push constants); whichever lands second redoes those hunks and regenerates the .inc.

What and why

Two ways the XDXR builder (xdxr_build.comp) read memory it had no reason to touch:

  • An update source that is not a structure of this layout. D3D12 leaves an update from a structure the title never built undefined, and a title can pass one. The refit copied the source and then walked up its parent table and node pointers as they were, so a source of zeros, random bytes or another layout sent the walk (and its reads and writes) anywhere in the device's address space.
  • A vertex index past VertexCount. The builder read every vertex its index named. An index at or past the geometry's VertexCount (garbage in the index buffer, or VertexCount 0) read past the vertex buffer, possibly by gigabytes.

The fix

  • MODE_CHECK (new, one invocation): after the source is copied, it compares the header words build_header writes that the layout fixes (flags and version, element count, node, element and parent offsets, the root pointer) and the size against the layout, and writes the verdict to a scratch word.
  • An update now records both tails: the refit steps carry FLAG_IF_REFIT, a full build's steps (MODE_INIT and finish) carry FLAG_IF_BUILD, and each dispatch returns at once unless the verdict picks it (XdxrBuilder::refit_begin / update_finish). A source that fails the check is rebuilt from the update's geometry and nothing else of it is read.
  • The refit walk follows only pointers that name one of the layout's nodes (layout_node, parent_slot), so a source that passes the check but holds a garbage tree still ends and stays inside the structure.
  • vertex() returns NaN for an index at or past VertexCount, which makes its triangle inactive as a NaN vertex already did. XdxrTriangles::vertex_count carries the desc's VertexCount (vulkan_backend.h); the default bounds nothing, for callers with no desc. The push block grows by one word (static_assert 108 bytes).
  • docs/bvh.md states the bound; the comment in d3d12_vulkan_device.cpp that said such indices read past the buffer now says they are not read.
  • xdxr_build.inc regenerated with glslc --target-env=vulkan1.2 -O -mfmt=c (Vulkan SDK 1.4.363.1); main's .comp reproduces main's .inc byte for byte with the same command.

Tested

  • vk_xdxr_build_test (hand-made geometry, no game data):
    • blas_vertex_count: an unindexed geometry whose last triangle lies past VertexCount over finite vertices, an index far past the buffer, and VertexCount 0; the reference treats those triangles as inactive and xdxr_bvh.py check-build validates the result.
    • eight update-source cases, each on the large tree and the small-sort path: a source of zeros, of random bytes, the built structure with another element count in its header (these become a full build of the moved geometry and are validated like any build), and a valid header over a random tree (the refit must end; its result is not checked).
  • Built gta6rt and the XDXR/BVH test targets (Windows 11, MSVC, Ninja, RelWithDebInfo) and ran ctest -R "xdxr|bvh": 11 passed (xdxr_build_output, xdxr_descs, xdxr_builder_validation, xdxr_build, xdxr_build_check, d3d12_xdxr_rt, d3d12_xdxr_rt_check, xbox_bvh_builder, xbox_bvh_builder_console, xbox_bvh_builder_check, vk_xdxr_postbuild_readback), 1 skipped (bvh_builder, which skips in this configuration). Not run: the full ctest suite and the translator suite (no translator change).
  • python -I scripts/check_no_game_data.py on the changed files: clean. python -I tests/test_text_write_line_endings.py: OK.
  • Retail game runs on an RTX 5080 (driver 621.24, Windows 11): before, the device was lost in most runs that stayed in the world, at 2-8 minutes, with the BLAS update in flight; with the change, two 15-minute runs stayed in the world without a loss, and the kernel took the full-build path for invalid sources in both. Those runs used a local build with this change and other local work, including a diagnostic that is not part of this pull request. No log lines, addresses or captures are quoted here.

GPUs tested: NVIDIA GeForce RTX 5080, driver 621.24, Windows 11 (the ctest entries and the game runs).

AI-assisted: yes (Claude Code, Opus 5.5)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator rule)

🤖 Generated with Claude Code

https://claude.ai/code/session_01Gt9hwRYcpySx4P4ncQ3wD5

…s by VertexCount

An update whose source is not a structure of this layout (one the title never
built, which D3D12 leaves undefined) made the refit follow its parent table
anywhere. MODE_CHECK now compares the copied header with the layout, and the
update records both tails: the refit runs when the check passes, a full build
when it fails. The refit walk follows only pointers to the layout's nodes.

A vertex index at or past the geometry's VertexCount is no longer read: its
triangle is inactive. XdxrTriangles carries vertex_count from the desc.

vk_xdxr_build_test: blas_vertex_count and eight update-source cases (zero,
random, another element count, garbage tree; large and small-sort).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gt9hwRYcpySx4P4ncQ3wD5
@djrolfes

Copy link
Copy Markdown
Contributor

Independent validation on Linux/Wine (NVIDIA RTX 3070 Ti, Vulkan 1.4, subgroup 32, Windows cross-build cherry-picked on top of a73a57e13 + local runtime test stack):

  • Offline Wine/Vulkan CTest (xdxr_build_output, xdxr_descs, xdxr_build, d3d12_xdxr_rt): 4/4 passed.
  • Controlled retail A/B on the same warm profile/cache:
    • Before #2222: repeatable VK_ERROR_DEVICE_LOST with READ_INVALID past the arena + NVIDIA Xid 31 / Xid 109 on an uncheckpointed AS build/update path.
    • With #2222 (77275065b): 0 fault addresses, 0 READ_INVALID, 0 Xid 31, 0 Xid 109 — the XDXR memory fault was completely eliminated. (The run later hit a separate zero-fault-address direct-queue timeout, unrelated to XDXR.)

AI-assisted validation.

@qhukz13

qhukz13 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

I reviewed 77275065 by reading the code against main a73a57e13. There is no glslc on this machine, so I did not regenerate the .inc, and my Windows PC is down today, so I have no GPU run. djrolfes's retail A/B above is the strong evidence that this is the fault seen at the world start.

The checks read right to me:

  • The gate is uniform. SCRATCH_REFIT (byte 28) is written by one invocation in MODE_CHECK, and a barrier follows. Every later dispatch then reads the same word, so a workgroup that uses shared memory leaves as a whole. Nothing in the fallback build touches that word: MODE_INIT writes bytes 0-20, the kept scan writes 24, and the sort arrays start at 64. So the verdict cannot flip halfway through the fallback.
  • The fallback matches a full build. It runs the same MODE_INIT as begin(). The geometry dispatches carry neither flag and run in both cases. They write the same element records either way, because an update pushes no different triangle or instance state.
  • layout_node bounds the walk. It checks the node type, the 64-byte boundary and rel / node_size() < pc.arg0. arg0 is l.nodes for MODE_PROPAGATE (base()). The ptr_unit < 2^26 guard keeps unit * 64 from wrapping. parent_slot keeps the parent-table read inside the body.

Two questions:

  1. What does an update cost now? update_finish records both tails every time. For a BLAS above 1,024 elements that is about 32 dispatches, each with a barrier, of which only the copy, check, clear, propagate and header did work before:

    • morton;
    • 8 × (count, scan, scatter);
    • karras and the 3 kept steps;
    • nodes, propagate and header.

    A dispatch the verdict skips still launches all its workgroups, 64 * tiles() for the radix steps. Each workgroup returns after one load, and the barriers serialize the queue. Skinned and animated BLASes are updated every frame, so on a small GPU this can add up. Did the retail run show any frame-time change? If it does matter, MODE_CHECK could write the group counts for vkCmdDispatchIndirect, so the skipped tail launches 0 groups.

  2. VertexCount as a hard bound. Before, an index past VertexCount was read, which the old comment called "as on the console". Now a descriptor with VertexCount 0, or one smaller than what the index buffer actually uses, silently loses those triangles from the BLAS. D3D12 calls that input invalid, but a console title only has to satisfy the console's builder. If the game's descriptors under-declare it, the raytraced shadows and reflections lose geometry with no message. Two things would make that visible:

    • a one-time log line with the triangles made inactive by the bound, per build or per run;
    • a count from djrolfes's retail run of how many triangles that was.

    If they turn out common, a clamp to the vertex buffer's size (from the resource the VA lands in) would keep both the fault fix and the geometry.

Neither changes the fault fix itself.

AI-assisted (Claude Code).

@DadaCap

DadaCap commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the careful read, in particular the check that nothing in the fallback build touches the verdict word.

  1. Update cost. What I have: in a retail run with this change (RTX 5080, 15 min in the world), the XDXR build kernel came to about 5 ms of GPU time per world frame in all (roughly 50-100 builds per 10 s per queue at about 1 ms each, one 410 ms outlier), against about 1 s of direct-queue work per frame, so it did not show in the frame time there. I have no clean before/after: without the change those runs lost the device within minutes, so there is no matching baseline. On a smaller GPU the empty tail could matter more, and your suggestion is the right shape for it: MODE_CHECK writing the group counts for vkCmdDispatchIndirect so the unused tail launches 0 groups. I'd rather keep that as a follow-up than grow this pull request, unless a reviewer prefers it here.
  2. VertexCount as a hard bound. Fair: a title whose descriptors under-declare it would lose triangles from the BLAS with no message, and the rays would miss that geometry. I don't have the count yet. I'll measure it with a local diagnostic (a counter of triangles the bound made inactive, read back once per run) in a retail run and post the number here. If it turns out to be more than a stray index, clamping to the size of the resource the vertex VA lands in, as you suggest, keeps both the fault fix and the geometry, and I'll switch to that.

(Reply written with Claude Code.)

@DadaCap

DadaCap commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up on the VertexCount question: I counted it. A local diagnostic (not part of this pull request) has the build kernel count every triangle the bound makes inactive, with a counter read back without a stall. Retail run, RTX 5080, about 10 min with the world rendered (25 world frames), this change applied:

VertexCount bound: 0 triangles inactive in 0 builds (of 1,219,755 triangles built)

So in the part of the game reached so far, the title never declares fewer vertices than its index buffers use, and the bound drops no geometry from the BLASes; it only guards the out-of-range case. If a later scene turns out to under-declare, the clamp to the vertex resource's size you suggested is still the way to keep that geometry, but nothing here calls for it yet. No device loss in that run either.

(Reply written with Claude Code.)

@kawaii-shakal kawaii-shakal 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 by Claude Code (AI-assisted) on my behalf. Approving.

  • MODE_CHECK then a flagged MODE_INIT / update_finish is a sound way to survive an update whose source is not a
    structure of this layout. Both finishes are recorded and the GPU picks one, so the CPU never reads back. The extra
    dispatches early-exit.
  • vertex_count defaults to 0xffffffff, so callers without a desc keep today's behaviour. vulkan_backend.h passes
    the desc's value.
  • Push block 104 -> 108 bytes, matching the shader's new vertex_count word.
  • Checked at 2222's head on Linux with Mesa lavapipe (llvmpipe, LLVM 19.1.7): xdxr_build_output, xdxr_descs,
    xdxr_builder_validation, xdxr_build, xdxr_build_check (67 s) and vk_xdxr_postbuild_readback pass. I did not
    check that the new cases in vk_xdxr_build_test fail on main.

Generated by Claude Code

@kawaii-shakal kawaii-shakal 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 by Claude Code (AI-assisted) on my behalf. Approving.

  • MODE_CHECK then a flagged MODE_INIT / update_finish is a sound way to survive an update whose source is not a
    structure of this layout. Both finishes are recorded and the GPU picks one, so the CPU never reads back. The extra
    dispatches early-exit.
  • vertex_count defaults to 0xffffffff, so callers without a desc keep today's behaviour. vulkan_backend.h passes
    the desc's value.
  • Push block 104 -> 108 bytes, matching the shader's new vertex_count word.
  • Checked at 2222's head on Linux with Mesa lavapipe (llvmpipe, LLVM 19.1.7): xdxr_build_output, xdxr_descs,
    xdxr_builder_validation, xdxr_build, xdxr_build_check (67 s) and vk_xdxr_postbuild_readback pass. I did not
    check that the new cases in vk_xdxr_build_test fail on main.

Generated by Claude Code

@aslan2142

Copy link
Copy Markdown
Collaborator

Merging.

  • Why: an update whose source is not a structure of this layout now becomes a full build instead of a refit through garbage pointers, and VertexCount bounds the vertex reads.
  • Tested on main 56bdd7e88 + this PR (77275065b), Mesa 26.3.1:
    • check_no_game_data.py is clean on the changed files.
    • xdxr_build.inc regenerates byte-identical with glslc.
    • xdxr_build and xdxr_build_check pass on llvmpipe, the integrated RDNA2 GPU and the RX 7900 XTX (RADV), with no kernel reset.
    • The new vertex-count and poisoned-source cases crash on plain main (llvmpipe only).
    • Windows suite under Wine on the integrated GPU: 418 of 482 pass, and no test went from pass to fail against main.
  • World runs on the RX 7900 XTX (RADV, Wine), from a build with this PR and three others merged locally:
    • With LEONIDA_RT_AS=off: 666 s with no fault, up to the title's own breakpoint. No regression, but this PR's code does not run with that setting.
    • With structures built: 1,417 builds were recorded and the world held for about six minutes. Then came read page faults on three pages, a ring timeout and a lost device, with every queue idle in the breadcrumbs.
    • So our run does not confirm the fix on RADV, and it does not show the builder as the cause either: a run with structures off ended in the same idle-queue timeout today. @djrolfes's A/B on this head is the evidence for the fix.
  • A separate gap seen in that run: 1,024 structure commands were not recorded because their destination or vertex address had no GPU copy. This PR does not cover that.

Thanks @DadaCap, @djrolfes for the A/B run and @qhukz13 for the review.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

@aslan2142
aslan2142 merged commit 935a2cd into ProjectLeonida:main Nov 26, 2026
2 of 5 checks passed
kawaii-shakal pushed a commit to kawaii-shakal/leonida that referenced this pull request Nov 26, 2026
Conflicts with ProjectLeonida#2222 (RT-XDXR-UPDATE-SOURCE-CHECK), resolved:
- Shader modes: this branch already uses 19-25 (serialize, deserialize and the
  pair modes), so main's MODE_CHECK = 19 becomes 26, in xdxr_build.comp and
  xdxr_build.h. Both sides' code is kept.
- Scratch: main's SCRATCH_REFIT was byte 28, which here is
  SCRATCH_LEAF_COUNT. An update that falls back to a full paired build would
  have overwritten the verdict with the leaf count and run both tails.
  SCRATCH_REFIT moves to byte 32, which nothing else uses.
- layout_node() (from ProjectLeonida#2222) bounded parent pointers by pc.nodes_off. A paired
  build places its nodes at nodes_off(), right after the kept leaves, so every
  parent was rejected and no box was propagated: all blas_paired_* cases of
  xdxr_build_check missed every ray. It now uses nodes_off(). Updates never
  pair (the layer pairs only builds without ALLOW_UPDATE), so refit_source_ok
  keeps pc.nodes_off.
- xdxr_build.inc regenerated with glslc (shaderc 2023.8, --target-env=vulkan1.2 -O -mfmt=c);
  spirv-val passes.

Tested: full ctest, 113 of 113, on Mesa lavapipe (llvmpipe, LLVM 19.1.7),
xdxr_build_check included.

Changes made by Claude Code (AI-assisted).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JPqXc6svM4pbBagTsVEYA3
kawaii-shakal pushed a commit to kawaii-shakal/leonida that referenced this pull request Nov 26, 2026
Conflicts with ProjectLeonida#2222 (RT-XDXR-UPDATE-SOURCE-CHECK), resolved:
- Shader modes: this branch uses 19 and 20 (serialize, deserialize), so
  main's MODE_CHECK = 19 becomes 26, in xdxr_build.comp and xdxr_build.h.
  21-25 stay free for the pair modes of ProjectLeonida#690, which builds on this branch, so
  the two branches number the modes alike. Both sides' code is kept.
- SCRATCH_REFIT moves from byte 28 to byte 32, as in ProjectLeonida#690, where byte 28 is
  the kept leaf count. Nothing here uses either byte.
- xdxr_build.inc regenerated with glslc (shaderc 2023.8, --target-env=vulkan1.2 -O -mfmt=c);
  spirv-val passes.

Tested: full ctest, 113 of 113, on Mesa lavapipe (llvmpipe, LLVM 19.1.7).

Changes made by Claude Code (AI-assisted).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JPqXc6svM4pbBagTsVEYA3
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.

5 participants