Help wanted: real ray-tracing acceleration structures (BVH) #34

Open
opened 2026-11-27 03:14:46 +00:00 by github-bot · 3 comments
Owner

@⁠ThatRecompDev on GitHub · 2026-11-25 16:21 UTC · original

right now every ray-tracing pass is held back: our XboxBvhBuilder shim builds no acceleration structures by default, so the passes that trace rays (and the ones that only feed or shade them, like CS_RaytracingResolve and bvh_skinning) stay stand-ins. letting them run over unbuilt memory took the device down at world load, so holding them was the fix for now. the cost is that shadows and indirect light come from stand-in values instead of the real thing.

what would help:

  • building real acceleration structures in the console's layout, either on the CPU (--raytracing already has a start: sizes and BLAS) or on the GPU with the game's own 52 builder pipelines (--raytracing-gpu inventories them)
  • checking our image_bvh_intersect_ray emulation against a real build

background: docs/raytracing.md, docs/bvh-builder.md, docs/ray-intersection.md. happy to answer questions here.

**@⁠ThatRecompDev** on GitHub · 2026-11-25 16:21 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/issues/34) right now every ray-tracing pass is held back: our XboxBvhBuilder shim builds no acceleration structures by default, so the passes that trace rays (and the ones that only feed or shade them, like `CS_RaytracingResolve` and `bvh_skinning`) stay stand-ins. letting them run over unbuilt memory took the device down at world load, so holding them was the fix for now. the cost is that shadows and indirect light come from stand-in values instead of the real thing. what would help: - building real acceleration structures in the console's layout, either on the CPU (`--raytracing` already has a start: sizes and BLAS) or on the GPU with the game's own 52 builder pipelines (`--raytracing-gpu` inventories them) - checking our `image_bvh_intersect_ray` emulation against a real build background: `docs/raytracing.md`, `docs/bvh-builder.md`, `docs/ray-intersection.md`. happy to answer questions here.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 03:14:46 +00:00
Author
Owner

@⁠almostxxx27 on GitHub · 2026-11-28 08:23 UTC · original

Ray tracing: plan for real acceleration structures (issue #34)

Every ray-tracing pass is held today: the XboxBvhBuilder shim builds nothing by default, so the 64 programs that
trace rays, and the passes that only feed or shade them (CS_RaytracingResolve, bvh_skinning,
rtdirectionalshadows), stay stand-ins, and shadows and indirect light come from RtStandIns values. Letting the
passes run over unbuilt memory lost the device at world load (boots w63, w65b, w70e). This note is the plan for
building real structures in the console's layout and checking the image_bvh_intersect_ray emulation against them.
Background: raytracing.md, bvh-builder.md, ray-intersection.md.
Code: src/FiveStar.Loader/Shims/Bvh, src/FiveStar.Shaders/Bvh.

Where things stand

Piece State
Console sizes (the DLL's prebuild query) done, with --raytracing
Static BLASes (core_bvh4.rpf) the DLL's CPU rehydration into game memory, done
Runtime BLAS and TLAS CPU build at record time (BvhBatchBuilder, XdxrBlas, XdxrTlas), done; never seen in a boot
Traversal programs --rt-traversal lets the 64 run; no boot has shown they read anything (open: T# bits 47..40 are 0, game memory seen so far is above 1 TiB)
GPU builder all 52 programs translate and compile offline; --raytracing-gpu inventories only, nothing dispatches
Default everything held (RayTracingPrograms, BvhMiss, RtStandIns)

The shortest path to real shadows and indirect light is finishing and proving the CPU path; the GPU builder follows
for skinned geometry and cost.

Phase 0: one instrumented boot

A boot with --rt-traversal --d3d12-trace-records, with logging added so it answers, per structure:

  1. Every Build, Rehydrate and Copy: type, destination address, console size, structure size, written or skipped
    (and why), time spent. Today only the first skip of each kind is logged (OnceLog); add a per-sample counter line
    as the other subsystems have.
  2. The T# base each traversal program binds (from the trace records) against the set of addresses written: do they
    match, and are the pages in the address map?
  3. Whether the device survives the world load, and for how long.

Deliverable: a table in raytracing.md closing the "below 1 TiB" question. Everything after depends on it.

Phase 1: the CPU path safe enough to be the default

The world-load device loss came from traversing unbuilt memory. The invariant: a program never walks a structure the
loader did not write.

  1. Structure registry. BvhBuilderService keeps address → (type, size, generation, owner) for every BLAS and
    TLAS it wrote or rehydrated. InstanceDescs.Read takes root and bounds from whatever is at the BLAS address; a BLAS
    that was skipped puts garbage bounds and a garbage root into the TLAS. Change: an instance whose BLAS is not in the
    registry (or whose header is not version 6 with bit 24 clear) is left out, counted and logged.
  2. A runtime release gate instead of a compile-time hold. RayTracingPrograms.StandIns turns off only after the
    first TLAS has been written (as VIctory holds its pipelines until its builder has run). Until then the passes stay
    stand-ins and RtStandIns fills their outputs; after, each pass's stand-in stops as the pass runs. ServesRays
    (CS_RaytracingResolve, rtdirectionalshadows) releases with the same gate; bvh_skinning stays held until step 4.
  3. Updates and compaction. BuildOne ignores Flags bit 5 (PERFORM_UPDATE) and rebuilds, which is correct; check
    that the rebuilt size still fits and that Copy's COMPACT path sees the postbuild size written.
  4. Skinned geometry. bvh_skinning~CS_* writes its vertices on the GPU when the list runs; a CPU build at record
    time reads pre-skin memory. Phase 0 shows what those buffers hold at record time. Options, in order: (a) build from
    the previous frame's skinned output (one frame late, no crash); (b) defer the build to the list's fence and write
    before the next frame's trace list executes; (c) leave to the GPU builder (Phase 3). Until decided, skinned BLASes
    are built from whatever is readable and the registry marks them stale.
  5. Cost. Bvh4Tree is a median quarter-split on the game thread. Measure ms per batch in Phase 0; if it matters,
    move builds to a worker and block in ExecuteCommandLists for the list that first references the structure. SAH
    stays out of scope unless hit quality is visibly bad.
  6. Header gaps the docs list as unknown: bit 17 of dword 0 and the parent-pointer table (dword 14). Write both as
    rehydration writes them, so a shader that reads them sees sane values.

Acceptance: a boot survives 10 min or more with --rt-traversal; shadow masks and the screen-probe light come from
the traces (compared with the RtStandIns picture); the sample lines count no "destination not mapped" or
"larger than the console size" skips.

Phase 2: check image_bvh_intersect_ray against a real build

Three checks, cheapest first:

  1. A real rehydrated BLAS as a test fixture. --bvh-dump <dir> writes each rehydrated BLAS (bytes, source desc,
    vertex and index ranges) from game memory. A small one joins FiveStar.Shaders.Tests/Assembled beside the
    synthetic bvh_*.bin cases: the HLSL emulation on WARP against the C# reference (BvhReference) against brute
    force over the leaf triangles, with the ray set of BvhTests. This tests the emulation on the DLL's own node
    layout, not ours.
  2. The DLL as an oracle for our builder. The CPU rehydration takes a serialized header and packed leaves
    (raytracing.md). Synthesize them for an arbitrary triangle list, run ConsoleBvhModule.Rehydrate, and diff the
    result against XdxrBlas.Build of the same triangles: header dwords, node types, bounds, triangle-node fields.
    Anything the DLL writes that we do not (bit 17, the parent table, dword 15) shows here. A loader test; no boot.
  3. End to end in a boot. --capture-dispatch on shadowmask~CS_TraceShadowRays_* with the TLAS it bound (from
    the registry): replay the captured rays through the reference model and compare the hit mask the shader wrote.
    Disagreements beyond the last ULP point at a traversal-header field misread (instance node 0x00, 0x70, bit 30 of
    0x38) or an emulation bug.

Phase 3: the GPU builder (--raytracing-gpu beyond inventory)

The steps of bvh-builder.md, with concrete gates:

  1. Inventory boot first (possible today): which device slots the constructor uses besides 62, and whether it
    completes on our device.
  2. Real pipelines through slot 62: the 52 CS containers go through PipelineFactory like any console program
    (subdword loads and stores, atomic sub, fmin and fmax are in the translator). Log compile time; they take the
    normal cache, so it is paid once.
  3. Record the DLL's dispatches. Bind the build entry points back to the DLL only when all 52 pipelines are built;
    else the CPU path. Log every command-list slot the DLL calls during one batch (root signature, root constants and
    views, Dispatch, barriers, copies, UploadArena use) and make sure each has a shim.
  4. Quarantine mode (--raytracing-gpu quarantine): Dest and Scratch in the batched descs are rewritten to a
    loader-owned allocation, read back after the fence and compared with the CPU builder's result for the same desc
    (triangle count, bounds, root type, every reachable triangle node). Nothing reaches title memory until the
    comparison is clean for N batches.
  5. Ownership. One writer per structure: the GPU for batches the builder ran, the CPU for rehydration and for any
    batch the gate skipped. The registry of Phase 1 carries the owner.
  6. Skinned BLASes then come for free (built when the list runs), and bvh_skinning is released.

Phase 4: defaults and docs

  • --raytracing and the traversal on by default once Phase 1's acceptance holds over a few boot series;
    --no-raytracing restores today's stand-ins.
  • Update raytracing.md (close the Unknowns the phases answer), ray-intersection.md ("Without a built structure"
    becomes the fallback), loader.md's flags and the status page.

Order and size

Step Size Needs a boot
Phase 0 logging and boot small yes
Phase 1.1 to 1.2 registry and gate small to medium yes
Phase 2.2 DLL-as-oracle test small no
Phase 2.1 real-BLAS fixture small one dump
Phase 1.4 skinned decision medium yes
Phase 2.3 end-to-end capture medium yes
Phase 3 large, several weeks yes

Assumptions: the issue wants real geometry over a good-enough approximation, so the CPU path is finished and made
the default before the GPU builder, and the GPU builder's first job is skinned geometry rather than replacing the CPU
path. GPU-first still needs Phase 0 and Phase 1.1 to 1.2: the gate and the registry are what keep the device alive
either way.

**@⁠almostxxx27** on GitHub · 2026-11-28 08:23 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/issues/34#issuecomment-7236548047) # Ray tracing: plan for real acceleration structures (issue #34) Every ray-tracing pass is held today: the `XboxBvhBuilder` shim builds nothing by default, so the 64 programs that trace rays, and the passes that only feed or shade them (`CS_RaytracingResolve`, `bvh_skinning`, `rtdirectionalshadows`), stay stand-ins, and shadows and indirect light come from `RtStandIns` values. Letting the passes run over unbuilt memory lost the device at world load (boots w63, w65b, w70e). This note is the plan for building real structures in the console's layout and checking the `image_bvh_intersect_ray` emulation against them. Background: [raytracing.md](raytracing.md), [bvh-builder.md](bvh-builder.md), [ray-intersection.md](ray-intersection.md). Code: `src/FiveStar.Loader/Shims/Bvh`, `src/FiveStar.Shaders/Bvh`. ## Where things stand | Piece | State | |---|---| | Console sizes (the DLL's prebuild query) | done, with `--raytracing` | | Static BLASes (`core_bvh4.rpf`) | the DLL's CPU rehydration into game memory, done | | Runtime BLAS and TLAS | CPU build at record time (`BvhBatchBuilder`, `XdxrBlas`, `XdxrTlas`), done; never seen in a boot | | Traversal programs | `--rt-traversal` lets the 64 run; no boot has shown they read anything (open: T# bits 47..40 are 0, game memory seen so far is above 1 TiB) | | GPU builder | all 52 programs translate and compile offline; `--raytracing-gpu` inventories only, nothing dispatches | | Default | everything held (`RayTracingPrograms`, `BvhMiss`, `RtStandIns`) | The shortest path to real shadows and indirect light is finishing and proving the CPU path; the GPU builder follows for skinned geometry and cost. ## Phase 0: one instrumented boot A boot with `--rt-traversal --d3d12-trace-records`, with logging added so it answers, per structure: 1. Every `Build`, `Rehydrate` and `Copy`: type, destination address, console size, structure size, written or skipped (and why), time spent. Today only the first skip of each kind is logged (`OnceLog`); add a per-sample counter line as the other subsystems have. 2. The T# base each traversal program binds (from the trace records) against the set of addresses written: do they match, and are the pages in the address map? 3. Whether the device survives the world load, and for how long. Deliverable: a table in `raytracing.md` closing the "below 1 TiB" question. Everything after depends on it. ## Phase 1: the CPU path safe enough to be the default The world-load device loss came from traversing unbuilt memory. The invariant: a program never walks a structure the loader did not write. 1. **Structure registry.** `BvhBuilderService` keeps `address → (type, size, generation, owner)` for every BLAS and TLAS it wrote or rehydrated. `InstanceDescs.Read` takes root and bounds from whatever is at the BLAS address; a BLAS that was skipped puts garbage bounds and a garbage root into the TLAS. Change: an instance whose BLAS is not in the registry (or whose header is not version 6 with bit 24 clear) is left out, counted and logged. 2. **A runtime release gate instead of a compile-time hold.** `RayTracingPrograms.StandIns` turns off only after the first TLAS has been written (as VIctory holds its pipelines until its builder has run). Until then the passes stay stand-ins and `RtStandIns` fills their outputs; after, each pass's stand-in stops as the pass runs. `ServesRays` (`CS_RaytracingResolve`, `rtdirectionalshadows`) releases with the same gate; `bvh_skinning` stays held until step 4. 3. **Updates and compaction.** `BuildOne` ignores Flags bit 5 (PERFORM_UPDATE) and rebuilds, which is correct; check that the rebuilt size still fits and that `Copy`'s COMPACT path sees the postbuild size written. 4. **Skinned geometry.** `bvh_skinning~CS_*` writes its vertices on the GPU when the list runs; a CPU build at record time reads pre-skin memory. Phase 0 shows what those buffers hold at record time. Options, in order: (a) build from the previous frame's skinned output (one frame late, no crash); (b) defer the build to the list's fence and write before the next frame's trace list executes; (c) leave to the GPU builder (Phase 3). Until decided, skinned BLASes are built from whatever is readable and the registry marks them stale. 5. **Cost.** `Bvh4Tree` is a median quarter-split on the game thread. Measure ms per batch in Phase 0; if it matters, move builds to a worker and block in `ExecuteCommandLists` for the list that first references the structure. SAH stays out of scope unless hit quality is visibly bad. 6. **Header gaps** the docs list as unknown: bit 17 of dword 0 and the parent-pointer table (dword 14). Write both as rehydration writes them, so a shader that reads them sees sane values. Acceptance: a boot survives 10 min or more with `--rt-traversal`; shadow masks and the screen-probe light come from the traces (compared with the `RtStandIns` picture); the sample lines count no "destination not mapped" or "larger than the console size" skips. ## Phase 2: check `image_bvh_intersect_ray` against a real build Three checks, cheapest first: 1. **A real rehydrated BLAS as a test fixture.** `--bvh-dump <dir>` writes each rehydrated BLAS (bytes, source desc, vertex and index ranges) from game memory. A small one joins `FiveStar.Shaders.Tests/Assembled` beside the synthetic `bvh_*.bin` cases: the HLSL emulation on WARP against the C# reference (`BvhReference`) against brute force over the leaf triangles, with the ray set of `BvhTests`. This tests the emulation on the DLL's own node layout, not ours. 2. **The DLL as an oracle for our builder.** The CPU rehydration takes a serialized header and packed leaves (raytracing.md). Synthesize them for an arbitrary triangle list, run `ConsoleBvhModule.Rehydrate`, and diff the result against `XdxrBlas.Build` of the same triangles: header dwords, node types, bounds, triangle-node fields. Anything the DLL writes that we do not (bit 17, the parent table, dword 15) shows here. A loader test; no boot. 3. **End to end in a boot.** `--capture-dispatch` on `shadowmask~CS_TraceShadowRays_*` with the TLAS it bound (from the registry): replay the captured rays through the reference model and compare the hit mask the shader wrote. Disagreements beyond the last ULP point at a traversal-header field misread (instance node 0x00, 0x70, bit 30 of 0x38) or an emulation bug. ## Phase 3: the GPU builder (`--raytracing-gpu` beyond inventory) The steps of bvh-builder.md, with concrete gates: 1. **Inventory boot first** (possible today): which device slots the constructor uses besides 62, and whether it completes on our device. 2. **Real pipelines through slot 62**: the 52 CS containers go through `PipelineFactory` like any console program (subdword loads and stores, atomic sub, fmin and fmax are in the translator). Log compile time; they take the normal cache, so it is paid once. 3. **Record the DLL's dispatches.** Bind the build entry points back to the DLL only when all 52 pipelines are built; else the CPU path. Log every command-list slot the DLL calls during one batch (root signature, root constants and views, Dispatch, barriers, copies, `UploadArena` use) and make sure each has a shim. 4. **Quarantine mode** (`--raytracing-gpu quarantine`): Dest and Scratch in the batched descs are rewritten to a loader-owned allocation, read back after the fence and compared with the CPU builder's result for the same desc (triangle count, bounds, root type, every reachable triangle node). Nothing reaches title memory until the comparison is clean for N batches. 5. **Ownership.** One writer per structure: the GPU for batches the builder ran, the CPU for rehydration and for any batch the gate skipped. The registry of Phase 1 carries the owner. 6. Skinned BLASes then come for free (built when the list runs), and `bvh_skinning` is released. ## Phase 4: defaults and docs - `--raytracing` and the traversal on by default once Phase 1's acceptance holds over a few boot series; `--no-raytracing` restores today's stand-ins. - Update `raytracing.md` (close the Unknowns the phases answer), `ray-intersection.md` ("Without a built structure" becomes the fallback), `loader.md`'s flags and the status page. ## Order and size | Step | Size | Needs a boot | |---|---|---| | Phase 0 logging and boot | small | yes | | Phase 1.1 to 1.2 registry and gate | small to medium | yes | | Phase 2.2 DLL-as-oracle test | small | no | | Phase 2.1 real-BLAS fixture | small | one dump | | Phase 1.4 skinned decision | medium | yes | | Phase 2.3 end-to-end capture | medium | yes | | Phase 3 | large, several weeks | yes | Assumptions: the issue wants real geometry over a good-enough approximation, so the CPU path is finished and made the default before the GPU builder, and the GPU builder's first job is skinned geometry rather than replacing the CPU path. GPU-first still needs Phase 0 and Phase 1.1 to 1.2: the gate and the registry are what keep the device alive either way.
Author
Owner

@⁠ThatRecompDev on GitHub · 2026-11-28 09:49 UTC · original

thanks, this is a solid plan and most of the structure is right. a few things have moved, though. --raytracing on its own already lets the 64 programs run (and the bvh_, resolve and rtdirectionalshadows ones) and turns the node misses off. the world-load losses from w63, w65b and w70e date from before nodes in unmapped memory missed and before rehydration and the CPU builds, and testers now run --raytracing --rt-traversal for 15-25 minutes, so the 10 minute bar is basically met. the open question is whether the traces hit anything, i.e. the T# below 1 TiB.

the stand-ins changed last night too. a held pass now fills only the records it stores through, lamps get their shadow-tile table set to lit instead of 0xffffffff, and the screen-probe light comes from the scene's cached radiance, not a flat grey. --no-raytracing and --bvh-dump don't exist yet, fine as proposals.

what would be great as PRs: per-sample counters for builds, rehydrations and skips. a check in InstanceDescs that a BLAS header is really built (version 6, bit 24 clear, root type 4 or 5) before it goes into a TLAS, which is a real gap today since a zeroed header gives root 0. and the DLL-as-oracle test, as long as it skips when the DLL isn't there, since that's a game file. one caveat on the dump fixture: a BLAS dumped from the game can't go in the repo, so keep those local and build committed fixtures from synthetic triangles.

**@⁠ThatRecompDev** on GitHub · 2026-11-28 09:49 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/issues/34#issuecomment-7237486168) thanks, this is a solid plan and most of the structure is right. a few things have moved, though. --raytracing on its own already lets the 64 programs run (and the bvh_, resolve and rtdirectionalshadows ones) and turns the node misses off. the world-load losses from w63, w65b and w70e date from before nodes in unmapped memory missed and before rehydration and the CPU builds, and testers now run --raytracing --rt-traversal for 15-25 minutes, so the 10 minute bar is basically met. the open question is whether the traces hit anything, i.e. the T# below 1 TiB. the stand-ins changed last night too. a held pass now fills only the records it stores through, lamps get their shadow-tile table set to lit instead of 0xffffffff, and the screen-probe light comes from the scene's cached radiance, not a flat grey. --no-raytracing and --bvh-dump don't exist yet, fine as proposals. what would be great as PRs: per-sample counters for builds, rehydrations and skips. a check in InstanceDescs that a BLAS header is really built (version 6, bit 24 clear, root type 4 or 5) before it goes into a TLAS, which is a real gap today since a zeroed header gives root 0. and the DLL-as-oracle test, as long as it skips when the DLL isn't there, since that's a game file. one caveat on the dump fixture: a BLAS dumped from the game can't go in the repo, so keep those local and build committed fixtures from synthetic triangles.
Author
Owner

@⁠almostxxx27 on GitHub · 2026-11-28 19:51 UTC · original

#72

**@⁠almostxxx27** on GitHub · 2026-11-28 19:51 UTC · [original](https://github.com/ThatRecompDev/five-star-recomp/issues/34#issuecomment-7244852910) https://git.gtahub.st/github/ThatRecompDev--five-star-recomp/pulls/72
This discussion has been locked. Commenting is limited to contributors.
No description provided.