feat(renderer): hide and isolate on the GPU from whole batches (#10264 S2)

Merged
LO
13d ago
+1612-1099

Part of #10264, stage S2 (hide and isolate from the batches). CPU-geometry release stays disabled; this PR frees no memory and makes no speed claim.

What changes

A colour batch whose entities are only partly hidden or isolated away is now drawn whole from its own GPU buffers, and the GPU discards the hidden entities per fragment. The renderer no longer cuts a sub-batch out of the CPU pieces for hide/isolate/storey isolation, for any model size. This removes the dependency the S0 audit recorded ("visible elements disappear" once the CPU copy is released).

  • One visibility table per device. entity-visibility-table.ts (S1's pick-visibility.ts, generalized) packs hide/isolate into a sorted id image with the isEntityVisible rules (hiding wins, empty isolation hides everything, null hides nothing). It is bound at group 3 binding 1, beside the section exclusions, so the colour pass, the shadow pass and the batch pick pass read the same table and the same WGSL entityVisible. S1's lane pick drops its private copy. The renderer uploads the image only when its visibility epoch moves (FrameEntityVisibility).
  • Colour pass. batch-visibility.ts classifies each batch once per visibility epoch (none / all / visible part). A partly visible batch draws through the normal whole-batch path (residency, routing, LOD) with MESH_FLAG_ENTITY_VISIBILITY in its uniform. fs_main resolves the full id from the vertex lane under the batch anchor (as the colour table and section exclusions do) and discards hidden entities. Everything derived from fs_main inherits the discard: ghost (X-Ray), the orthographic visibility replay. Partly visible opaque draws use the discard pipeline, not the discard-free twin. Fade and transparent routing come from the visible entities only.
  • Shadow pass. A partly visible batch casts its own buffers whole. The collector marks it visibilityFiltered; the shadow pass routes it through its fragment pipeline set (built on first need, as for clipping) whose fs_shadow_clip now also tests entityVisible. Stale clip flags are cleared when no clip is active.
  • Never silent, never hidden geometry. A partly visible batch that cannot be tested (the image exceeds the device's storage binding limit, or its ids span two 2^24 pages so no lane anchor exists, which scene buckets rule out by construction) is left out of colour and shadow and counted as not resident; it is never drawn with its hidden entities. The lane pick keeps S1's behaviour (throws, so the whole pick takes the CPU route).
  • Colour splits only. PartialBatchRequests.pushSplit keeps the X-Ray alpha split (#4129) and lens promotion split (#677) for partitionable batches, restricted to visible ids. Without pieces such a batch draws whole under the visibility test at the lowest alpha of its visible entities: too wide a fade, never missing or hidden geometry. Hide/isolate alone never splits. Stand-ins of an evicted parent (#9227) draw under the test.
  • Superseded code removed: the hide/isolate sub-batch request (pushVisible), classifyBatchVisibility, the shadow pass's partial sub-batches, the unused opaqueSubBatches list. renderer.ts shrinks by 76 lines; module-size budgets lowered for renderer.ts, shadow-pass.ts, main.wgsl.ts. No public export changes (API snapshot unchanged).

Design choices for the cases the stage plan named:

CaseBehaviour
Quantized / f32 flat batchesSame discard in fs_main; both vertex paths feed sectionEntityId. GPU-checked both layouts.
Textured meshesAlready drawn one per mesh and filtered without CPU geometry; flag cleared for them.
InstancedUnchanged per-instance hidden flag (colour, shadow, pick).
Colour-merged (per-vertex secondary ids)Partly visible batches test secondary lanes. Classification still reads primary entries, as before S2: fully visible primaries can leave a hidden secondary drawn; fully hidden primaries can drop a visible secondary. No in-tree producer emits foreign ids, but host-supplied meshes can. #10653 tracks complete coverage before such a producer or release of those host meshes.
Federation 1/NFull ids via the batch anchor, as the colour table. GPU-checked with a hidden entity in the second model (id offset 1,259,881).
Georeferenced / RTCNo change to transforms; GPU-checked on ISSUE_098 at ~5,000 km.
Transparent / X-RayTransparent and ghost pipelines carry the discard; X-Ray splits keep visible ids. GPU-checked X-Ray + hide.
Storey isolationRides isolatedIds; GPU-checked.
Hidden types (IfcSpace etc.)Not hide/isolate: useFilteredGeometry drops those meshes before upload and re-uploads from the store's CPU arrays on toggle. Unchanged here and documented as a remaining dependency before S4.
Device loss / resize / limitsTable is per device; a new device gets a fresh table and the cached image is re-uploaded on the first frame. Over-limit images refuse rather than truncate. No new pipelines; the shadow fragment set is the existing lazily built clip set. Device loss and resize have unit coverage only.

Evidence (real WebGPU, untimed)

tests/e2e/gpu-entity-visibility.e2e.spec.ts (opt-in GPU_PICK_PARITY=1, same config and harness as S1). Run on Pop under ~/build-workers/ifc-gpu-check, headed Chromium 1248 on a private Xvfb with the Vulkan/ANGLE flags, adapter nvidia / lovelace, on clean committed head 3cbdefc1 (main merged): 10/10 passed (4.6 min).

Oracle: the frame the old CPU path drew is the frame of the visible pieces alone. Each scenario captures the production colour frame (A, central 512×512 crop via the renderer's own colour readback), then removes every hidden flat piece from the scene and rebuilds those batches, re-applies the same visibility, and captures B. C is the same view before the filter (proves the comparison is not vacuous). "Strong" = a channel off by more than 48/255 (geometry present in one frame, absent in the other); the contract is ≤ 0.05 % strong pixels and ≤ 1 % faint ones. A pick grid (40×24) under the same visibility must never return a hidden entity and must agree with the CPU raycast.

Scenario (public fixture)Hidden flat idsA vs B: differing / strong / max ΔA vs C strongPicks compared
C20-Institute q: hide 2 + sun shadows + selected visible entity20 / 0 / 045,25752
C20-Institute q: isolate 36990 / 0 / 023,249104
C20-Institute q: storey isolation5401,011 / 0 / 2555,3373
C20-Institute q: X-Ray (ghostExceptIds) + hide 220 / 0 / 024,51752
C20-Institute q: section plane (down, 43 %) + hide 330 / 0 / 054,5745
C20-Institute f32: hide 2 + shadows + selection20 / 0 / 045,25452
C20-Institute f32: isolate 36990 / 0 / 023,248104
Federation Institute + duplex: hide in model 220 / 0 / 02,85756
ISSUE_098 (~5,000 km): isolate 311,1210 / 0 / 058,43028
Institute, hide + shadows, then scene.releaseGeometryData()—before vs after release: 0 differing—grid: no hidden pick

The storey case's 1,011 faint pixels (max Δ 25/255) sit on the façade glazing: the reference rebuilds the batches it removed pieces from, which reorders overlapping translucent draws. Nothing appears or vanishes (0 strong). After the release, isolating a different set still draws the isolated entities from their batches, and picks never return a hidden id.

The release test is evidence-only (it calls releaseGeometryData() from the spec); the product never releases. Screenshots A/B/C and diff images of every scenario are attached to the run and kept privately with the receipts.

S1 regression on the same head: tests/e2e/gpu-batch-pick.e2e.spec.ts 4/4 passed (3.8 min); the lane pick now reads the shared table.

Unit/integration (Node, no device): batch-visibility.test.ts, the renamed entity-visibility-table.test.ts (CPU mirror against isEntityVisible), shadow collector/pass tests (marked draw goes through the fragment set, entity.y set only for it, stale clip flags cleared), and renderer-render-paths.test.ts: hide, isolate and cross-batch isolate with the scene's CPU geometry released still draw and cast the visible batchmates from the batch's own buffers with no sub-batch (fails on the old path, where getOrCreatePartialBatch returned nothing); table over the limit leaves the partly visible batch out and reports it; an evicted partly visible batch is rebuilt through the queue, never as a subset; the table is uploaded once per visibility change.

Not covered / limitations

  • Reference purity on isolate. The reference cannot remove entities that also have instanced occurrences: removing one flags its occurrences hidden but drops it from the scene's instanced hidden set, and the perspective fast path then draws them again (a pre-existing deletion-path defect found while building the oracle, not touched here). The fully independent hide scenarios now assert mixedKept === 0. Those mixed ids (483 of 699 for isolate, 363 of 540 for storey, 3,151 of 11,121 on ISSUE_098) stay GPU-filtered in B as well; the remaining flat-only ids are a true visible-pieces reference.
  • Textured / instanced on screen. No scenario asserts a direct textured hit (textured meshes are unchanged and per-mesh); instanced occurrences keep their existing flag.
  • Hidden types stay a CPU re-upload route (see table). Selection/hover highlight still builds per-entity meshes from pieces (S3).
  • The colour readback covers the central 512×512 of a 1280×720 canvas.
  • Device loss and resize: unit coverage only.
  • No timing or memory claim; nothing was timed.

Validation

All checks below ran on pushed head e5ce300216227ca8e6fefad347a5b894fa32e969, which includes main at 40c0331f.

  • full viewer suite: 2943 files, 24742 tests, 0 failed, 362 skipped, on e5ce300216227ca8e6fefad347a5b894fa32e969. All four shards exited 0 in the same final run: 24380 passed. The installed Chromium executable was supplied explicitly.
  • Renderer: 2881 tests, 2879 passed, 0 failed, 2 skipped.
  • Root pnpm typecheck and viewer build: passed.
  • node scripts/pre-handoff.mjs --base 40c0331ffe704a63aa00e4f09d76d6c7c07f59f8: 14/14 gates passed.
  • Fleet: Pop, request auto-d1f4a17664214e61b2068a91811a042f, job 91101dad-5e3b-479a-b201-77a0cdd71c81, exit 0.
  • Real-GPU acceptance: 14/14 passed, 0 skipped, 0 flaky, Pop, headed Chromium 1248/private Xvfb under ifc-gpu-check. S1 picking 4/4 and S2 visibility 10/10.
  • Regression coverage includes delayed allocation OOM, no unconfirmed buffer bindings, equal-content failed-allocation suppression, recovery on changed/smaller visibility states, overlapping picks requiring different capacities, and teardown during the initial pick await.
  • Retained-scene GPU comparisons reject one-sided hits and require nonempty comparisons. Federation requires two distinct model-two targets. Hide/shadows (quantized and f32), X-Ray/hide, section/hide and federation/hide require independent flat-piece references (mixedKept === 0).
  • API surface snapshot unchanged; changeset is @ifc-lite/renderer patch.

Pre-existing handovers: instanced deletion #10639; host-supplied secondary lane classification #10653. Mixed isolate/storey screenshot-reference limitations remain documented above. CPU-geometry release remains disabled.