Skip to content

feat(kyty): serve BVH walks over several top-level trees natively - #337

Open
Raghav314 wants to merge 1 commit into
simnJS:Brainrotfrom
Raghav314:feat/kyty-rt-multiple-tlas
Open

Raghav314 wants to merge 1 commit into
simnJS:Brainrotfrom
Raghav314:feat/kyty-rt-multiple-tlas

Conversation

@Raghav314

@Raghav314 Raghav314 commented Nov 26, 2026 •

Copy link
Copy Markdown

What

Kyty serves a compute shader's BVH walks natively only when every walk reads the same top-level tree. A shader whose walks read two or three different trees kept the walks of the first tree and ran the others empty, so their rays missed.

  • Recompiler: NativeTraversalLayout keeps walks of other trees with the same instance-record layout and records each extra tree (user SGPR, header offset, header V#, root, layout) in ShaderInfo::ray_query_extra_sources, serialized with the shader.
  • SPIR-V: when a shader has more than one tree, each walk picks its host context at run time by matching the tree header address it read against the valid entries of the context table. Shaders with one tree emit the same code as before.
  • Host: a dispatch or draw prepares one context per tree. Each gets its own acceleration structure, instance-node range and context entry (128-byte stride), and CacheTlasMemory keeps every tree's guest memory mapped.
  • Walks the host query or the results rewrite answer stay on the first tree, whose context those paths read. A shader with extra trees synchronizes device-address memory conservatively instead of using the first tree's bounded reach. The readback ring grows by MaxContexts so one dispatch's contexts never wait on its own submission.
  • Limits come from the encoding, not from one title: up to 32 walk sites per shader (the site field and the site mask allow 32) and up to 32 trees. The emitter's per-site state is sized from MaxRayQuerySites; sites a shader does not use emit nothing.

Tested

Arch Linux (CachyOS), NVIDIA GeForce RTX 4090, driver 620.73.04.

Native coverage

kyty_rt_probe --subgroup 32 --oracle 33 --stats --all over the shader store of a PS5 open-world title (68 compute shaders with BVH walks, 550 walk sites):

base this PR
sites served natively 263 410
sites run empty 257 110
shaders fully native 18 of 68 42 of 68

The title's shaders use at most 7 walk sites and 3 trees each. The remaining empty sites are shaders whose instance records are read through a buffer descriptor built inside the shader, which this PR does not change.

Tests

kyty_shim_rt_*, kyty_shim_native_rt, kyty_shim_recompiler_* and kyty_shim_hw_oracle* (14 tests) pass on base and on this PR with the Vulkan validation layer inserted, 0 VUID- on both. kyty_shim_rt_corpus skips on both. kyty_shim_recompiler_valid_spirv passed 30 of 30 repeated runs.

New kyty_shim_rt_context_pick: two host TLASes (one instance each, custom index 11 and 22) and a context table holding tree A, tree B, a slot with valid = 0 whose header matches a third tree, and a second entry with tree A's header pointing at tree B. One 64-lane dispatch runs the emitter's tree pick and a ray query against the picked tree: walks on A return 11, walks on B return 22, the invalid slot and an unknown header miss. Mutants that ignore the valid word or let the last matching entry win fail it (16 failures each). Passes with the validation layer, 0 VUID-; skips without ray-query support.

In game

One attended session on the change (before the rebase onto current Brainrot; the rebase changed no survey result): host-traced RT dispatches every 10 s, no device loss, no Xid. It was a cold start, so most of the session was shader compilation.

Not measured: driver compile time of the shaders that now emit more walk sites (the stored modules from that session predate the rebase and the current replay tool cannot read them). Extra trees take their header from user data or the walk itself, not the flattened SRT, and start from the default instance capacity; those are speed limits, not correctness ones.

Checklist

  • Based on current Brainrot; 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 4090, driver 620.73.04, Arch Linux (CachyOS)
  • AI-assisted: yes

@github-actions

Copy link
Copy Markdown

The compute and draw paths can skip an extra source with an out-of-range SGPR pair, but the emitted shader still scans that context slot. Neither skip clears the slot, so a valid entry from an earlier dispatch or draw can be selected if its header matches. Invalidate skipped slots before the shader runs, or reject those sources before emitting the scan.

Please add a synthetic regression test that checks hit results from two distinct trees in one dispatch. The coverage counts show that more sites are marked native, but they don't establish that each walk selects the right tree.

@Raghav314
Raghav314 force-pushed the feat/kyty-rt-multiple-tlas branch from 6b7cd96 to 520f121 Compare November 26, 2026 20:26
@Raghav314
Raghav314 temporarily deployed to repository-maintenance November 26, 2026 20:26 — with GitHub Actions Inactive
@Raghav314

Copy link
Copy Markdown
Author

Both fixed in 520f121.

Skipped slots: when a dispatch or a draw skips an extra tree because its SGPR pair is out of range, Kyty now clears that context slot's valid word before the shader runs (RayTracing::InvalidateContext, a fill with barriers on both sides). A prepared slot is already reset by the walk dispatch, so these two skips were the only way a slot could stay stale.

Regression test: kyty_shim_rt_context_pick builds two host TLASes whose single instance carries custom index 11 or 22, and a context table with tree A, tree B, a slot with valid = 0 whose header matches a third tree, and a second entry with tree A's header that points at tree B. One 64-lane dispatch runs the emitter's tree pick (now PickContext, the same code the walk emits) and a ray query against the picked tree. Walks on A return 11, walks on B return 22, and the invalid slot and an unknown header miss. Mutants that ignore the valid word or let the last matching entry win both fail it (16 failures each). It passes with the validation layer inserted, 0 VUID-, and skips on devices without ray queries.

@github-actions

Copy link
Copy Markdown

The updated diff clears skipped extra slots in both compute and draw, and the reported two-TLAS test covers the requested hit results as well as invalid and duplicate entries.

One remaining issue: Serialize.h now reads ray_query_extra_sources unconditionally, with no format-version change in the diff. An older shader record has no vector at that position, so a reader that still accepts the old format can consume subsequent bytes as the vector length or fail at EOF. Please version the new field and default it to empty for older records, or bump the format version and explicitly reject or invalidate old records.

@Raghav314
Raghav314 force-pushed the feat/kyty-rt-multiple-tlas branch from 520f121 to e1dc335 Compare November 26, 2026 21:05
@Raghav314
Raghav314 temporarily deployed to repository-maintenance November 26, 2026 21:05 — with GitHub Actions Inactive
@Raghav314

Copy link
Copy Markdown
Author

Fixed in e1dc335.

The field is now versioned like relative_vgprs: Reader::shader_info_has_ray_query_extra_sources (default on) controls whether ray_query_extra_sources is read, and when it is off the vector is cleared, so a record from before this change decodes with no extra sources. kyty_spirv_match, the one reader that accepts older store layouts, now tries current, pre-extra-sources and pre-relative-VGPR layouts in that order. A new fixture store written without the field (no-extra-sources.shaders) matches. Dropping the pre-extra-sources layout from the list makes the test fail.

The runtime store never reaches this case: its stamp is a hash over the graphics/shader sources, Serialize.h included, so stores written by an older build are rejected at the header.

@github-actions

Copy link
Copy Markdown

The serialization concern is addressed in the updated diff: the reader clears the extra-source vector for older layouts, and kyty_spirv_match tries the pre-extra-sources layout. The new fixture covers that fallback.

One test portability issue remains: RtContextPick.cpp selects the first GPU with the required extensions and a compute queue, then checks the required features only after leaving the device loop. On a multi-GPU system, it can skip or fail even when a later GPU supports the test. Check the features inside the loop and continue to the next candidate when any are missing.

A compute shader whose walks read different top-level trees kept only
the walks of the first tree native and ran the others empty, so their
rays missed.

Keep walks of other trees that share the instance-record layout and
record each extra tree in the shader info. When a shader has more than
one tree, each walk picks its host context at run time by matching the
tree header it read against the valid context entries. The host
prepares one context, acceleration structure and instance-node range
per tree, and keeps every tree's guest memory mapped. Shaders with one
tree emit the same code as before.

Size the walk-site limit from the encoding: the site field and the site
mask allow 32, and the emitter's per-site state now follows
MaxRayQuerySites instead of a fixed 4.

Draws prepare every tree as dispatches do. Walks answered by the host
query or rewritten to results stay on the first tree, whose context
those paths read. A header never splits off twice with different
layouts, sites stay below MaxRayQuerySites, shaders with extra trees
synchronize device-address memory conservatively instead of using the
first tree's bounded reach, and the readback ring holds one dispatch's
contexts without waiting on its own submission.

A dispatch or draw that skips an extra tree because its SGPR pair is out
of range clears that context slot's valid word, so the walk never picks
a stale entry left by an earlier dispatch. The tree pick moves into
PickContext, and kyty_shim_rt_context_pick runs it with ray queries
against two host trees, an invalid slot and a repeated header.

The new shader-info field is versioned like relative_vgprs: a reader
flag (default on) decides whether it is read, and older records decode
with no extra sources. kyty_spirv_match also tries the layout from before
the field, and a fixture store without it covers that path.
@Raghav314
Raghav314 force-pushed the feat/kyty-rt-multiple-tlas branch from e1dc335 to 8bd4916 Compare November 26, 2026 21:06
@Raghav314

Copy link
Copy Markdown
Author

Fixed in 8bd4916. RtContextPick.cpp now checks ray query, acceleration structure, buffer device address and shaderInt64 support inside the device loop and moves on to the next candidate when any is missing, so a later GPU that supports the test is used. Still passes with the validation layer inserted (0 VUID-) and skips with 77 when no device qualifies.

@Raghav314
Raghav314 temporarily deployed to repository-maintenance November 26, 2026 21:07 — with GitHub Actions Inactive
@github-actions

Copy link
Copy Markdown

The updated diff addresses the device-selection issue: RtContextPick.cpp now checks the required features for each candidate before selecting it, so an unsupported GPU no longer prevents the test from finding a later compatible one. Thanks for confirming that the test still passes with validation enabled.

This branch was previously deployed

1 inactive deployment
repository-maintenance — 8bd4916f Deployed Nov 26, 2026 by Raghav314 via review #24
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.

1 participant