perf(renderer): keep one raycast index across visibility changes (#9504)

Merged
BL
21d ago
+777-356

Closes #9504

What changes

  • RaycastFlatIndex collects the flat, textured and batched pieces once per pick-geometry revision and builds one BVH over all of them. Hide and isolate now filter each ray's candidates and rebuild nothing.
  • If a revision bump leaves the same pieces in the same order, the BVH is kept.
  • BVH moves to typed arrays with an in-place median split. It preserves its existing query/build methods and adds explicit buffer release.
  • New Renderer.isRaycastCacheWarm() reports whether the flat raycast index is warm. Ray-hit instance geometry can still materialize per ray.

Instanced occurrences stay per-ray, after AABB culling, as before.

Freshness

The cache key is getPickGeometryRevision() plus draw-list identity. meshDataMap.size is not enough, because fragments can be replaced under the same ids (#4556). A SceneContents adapter without a revision re-collects on every ray, which is today's behaviour.

The existing #4556 test wrote directly to the private meshDataMap, bypassing the revision. It now replaces the fragments through removeMeshesForEntities and addMeshData, and keeps the same assertions. The stub GPU buffers gained destroy() for that path. A new test covers replacement where only the revision moves.

Measurements

Base is this PR's main parent, measured in the same Node process on synthetic scenes.

Through RaycastEngine, 200k flat meshes:

BaseBranch
Cold ray648 ms290 ms
Warm ray223 ms2.1 ms
First ray after a hide change613 ms2 ms

BVH alone, 1.5M meshes:

BaseBranch
Build3749 ms937 ms
Retained210 MB63 MB
2000 rays51 ms25 ms

The 763 s figure in #9504 was measured on a real federation against 7.1.0, and is not re-measured here.

Verification

Run locally on Windows, where several gates cannot run; CI on this PR is the authoritative result.

  • packages/renderer: 2652 of 2655 pass with 2 skipped. The one failure, "the root entry does load them", compares module paths and does not exercise this code.
  • turbo typecheck: passes for the renderer and dependents; gaussian-splat-spike fails locally.
  • check-api-surface, check-module-size and check-source-text-assertions pass.
  • Mutation: dropping the revision check from the freshness test fails the adapter test.

Summary by CodeRabbit

  • New Features
    • Added a renderer status check to show whether the next scene raycast can reuse its cache.
    • Custom raycast adapters can now support textured meshes and respond to host-geometry eviction.
  • Performance
    • Raycasting reuses cached scene geometry across visibility changes while applying visibility when selecting results.
  • Bug Fixes
    • Raycast results now reflect geometry after placement changes and host eviction.
    • Improved ray intersection handling for near-parallel rays and bounds at large coordinates.
  • Documentation
    • Updated rendering and performance guides with cache-status details and raycast behavior.

Maintainer review fixes and validation

The contributor's implementation is preserved. Follow-up commits e4197975c8 and a933b1e342 handle nonzero subnormal slab directions, immediately release cached pieces and BVH buffers on host eviction or explicit geometry release, and provide an unsubscribe/disposal path for discarded raycast engines. The warmth guarantee covers only the flat index.

Exact final head a933b1e3424f1e828be7309b74d202a04d386446 passed on the fleet: 2,710 renderer tests with 2 skips, full root typecheck (136 tasks plus tail), and all available pre-handoff gates. The retained-free viewer artifact check skipped because this worker had no built viewer. Original request auto-616cd9b858f24aa983ac39b9038d55e1, PC job 1fd00419-a62b-4f5d-841b-ce3397917cfc, exit 0, source unchanged. This verifies the follow-up behavior; it does not independently remeasure the author's synthetic timings or the real federation result above. Fresh-head CI and review remain required before queueing.