Skip to content

Viewer CPU: one scene walk per frame, nested node batching (port of bottosson/performance) - #1232

Merged
wass08 merged 4 commits into
mainfrom
perf/bottosson-port
Nov 6, 2026
Merged

wass08 merged 4 commits into
mainfrom
perf/bottosson-port

Conversation

@wass08

@wass08 wass08 commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Port of @bottosson's bottosson/performance branch onto main, with two adjustments for what landed since (#1228). His two commits are kept as authored.

From bottosson/performance

  • One scene matrix walk per frame (lib/scene-matrices.ts). Three's per-render matrix update is replaced by one walk per frame that only rebuilds what changed. Subtrees no pass can draw (merged originals, a soloed level's shadow-only content) are set aside for the render.
  • Nested node batching. Cabinet modules, items on items, and wall- or ceiling-hosted items can batch now. Any edit, gesture, tint, rebuild or reveal on a host releases what it carries; a cooktop with lit burners stays out.
  • Cheaper shadow-bounds refresh, solo re-hide no longer runs every frame, and the fence system idles when no fence follows surfaces.
  • Fog without the cloud noise (the output is identical at fog elevations), and shared room-light level arrays, uploaded only when a level moves.

Port adjustments

Moving items (found while testing this PR, pre-existing on main)

Moving an item that carries another (a bedside with a lamp on top) hitched for seconds and could hop floors:

  • Placement stays on the selected level. Hovering another floor's furniture, slab, wall, ceiling or roof (past the building edge, through a window) re-parented the item to that floor mid-gesture.
  • No floating after leaving a host. The detach kept the surface move's building-space live transform, so a bedside left on a 4th-floor bed floated ~10 m up.
  • No rebuild storms. Daylight rooms no longer rebuild on furniture edits or child-list-only changes (a moved lamp still triggers a rebuild). The live stair/elevator opening systems ignore child lists, as the document reconcile already does. Node batching no longer cascades releases from levels, buildings or sites: re-parenting furniture used to release the whole floor's batches. This also fixes two history tests the port had broken.

Moving a bedside with an item on top across the screen (5-level condo): worst frame 2.3 s -> 0.5-0.85 s, no level hops, correct height.

Measured (M5 Max headless, High)

main this PR
Editor, 5-level house: frame CPU 14.5 ms 9.3 ms
rn9 baked overview 34 fps, 23.8 ms CPU 50 fps (cap), 10.3 ms
rn9 walkthrough 36 fps, 18 ms CPU 50 fps (cap), 6.9 ms
hav5 baked overview 12.6 ms CPU 5.6 ms

Draw counts are unchanged (culling intact), and before/after renders are identical.

Test plan

  • check-types, viewer (1348), core (7062), nodes (5230), editor (2856) tests, biome
  • Visual diff of baked renders before/after
  • Manual pass in the editor: cabinet doors and drawers, cooktop, dragging a cabinet run, editing hosts with items, undo

🤖 Generated with Claude Code


Note

High Risk
Touches core rendering (matrices, passes, shadows), batch membership, occlusion, and daylight shaders—regressions could affect visuals, culling, or lighting; manual/vis validation is important.

Overview
Scene and render path: Adds scene-matrices.ts so one walk per frame updates world matrices incrementally, temporarily hides subtrees nothing draws (merged originals, idle shadow-only), and wraps post-processing / key renders in renderWithSceneMatrices. LayerPassIndex exposes a layout revision for that walk; reveal-pose marks posed objects so they recompose every frame.

Nested node batching: BatchableConfig docs and batch candidate logic now follow a host chain to a level (not only direct level children). New helpers (hostChain, hostsBusy, addBatchableDescendants) propagate dirty releases, tints, and deferrals to carried batchable nodes (cabinet runs → modules, etc.). Cabinet and cabinet-module use a shared cabinetBatchable (cooktop burners stay excluded); shelf uses new staticBatchable. Container types (level/building/site) no longer cascade child-list churn to the whole floor.

Viewer CPU / lighting: Key-light shadow bounds skip double-counting nested registry roots and use expandByWorldBounds instead of per-object expandByObject matrix spam. Daylight rooms rebuild only on structural edits (roomInputsChanged + furniture-type filter + child-list-only skips), lamp feed keys include quantized world positions, and level height uniforms sit in a shared TSL group with updates only when Y/offset change. Fog uses horizonRadiance (no cloud noise / extra uniforms there).

Occlusion / visibility: Meshes temporarily hidden only for the optimized render count as “shown” via isSetAsideForRender so occlusion culling and merged outlines stay correct for batched originals.

Editor / core sync: Placement coordinator reparenting back to the floor publishes the floor live transform and gates surface hit handlers to the active level so drafts don’t jump floors mid-gesture. Elevator/stair opening sync passes ignoreChildLists into changedInputs. Fence system idles when no fence follows surfaces. LevelSystem re-applies shadow-only masks when layout revision moves after hidden rebuilds. Scene visibility idempotent re-hides track applied mask.

Tests cover roomInputsChanged and occlusion with set-aside occluders.

Reviewed by Cursor Bugbot for commit 29ffa64. Bugbot is set up for automated code reviews on this repo. Configure here.

bottosson and others added 3 commits November 6, 2026 16:41
- scene-matrices: one dirty-tracked matrix update per frame instead of three's
  full update in every pass; subtrees nothing in the frame draws (batched
  originals, a soloed level's shadow-only content) are hidden for the render
  only and skipped by the walk while the layout revision holds.
- Node batching reaches nodes nested under hosts (cabinet modules, items on
  items, wall/ceiling-hosted items); a host's edits, gestures and tint release
  what it carries. Cabinets, cabinet modules and shelves opt in.
- Solo re-hide, shadow bounds and fences no longer walk the scene every frame.

Waterfront 41-49 -> ~16 ms frame CPU, apartment 29 -> ~15 ms (dev build,
RTX 3060 Ti, High tier).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Item batches skip the per-pass depth sort (opaque; the prepass lays depth).
- Fog samples a cloud-free horizon radiance: the clouds fade out at the fog's
  elevation, so the two fbm calls per fragment and cloudTime (which moved every
  frame) drop out of every fogged material.
- The room light's level arrays move to a shared uniform group bumped only when
  a level moves: as per-object array uniforms they were re-uploaded for every
  object in every pass (~760 writeBuffer a frame in the apartment, now 1).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sort

- The occlusion culler (#1228) runs inside the render, where
  `renderWithSceneMatrices` hides subtrees nothing draws: the merged
  originals it rasterizes as walls and slabs. They count as shown again
  (`isSetAsideForRender`), as the merged outline already does; test added.
- Item batches keep three's front-to-back sort: `sortObjects = false` was
  justified by the depth pre-pass, which #1228 removed, and on a GPU-bound
  editor the overdraw costs more than the sort saves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit cc958c4. Configure here.

const overrides = useLiveNodeOverrides.getState()
if (overrides.get(nodeId as AnyNodeId) || useLiveTransforms.getState().get(nodeId)) return null
if (batchable.scope === 'wall' && node.parentId && overrides.get(node.parentId as AnyNodeId)) {
const liveTransforms = useLiveTransforms.getState()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant useLiveTransforms.getState() call before variable declaration

Low Severity

useLiveTransforms.getState() is called inline at the end of line 172 to check the node itself, and then called again on line 173 to store in liveTransforms for the hostsBusy check. The variable declared on line 173 could be hoisted above line 172 and reused in both places, avoiding the redundant state access.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit cc958c4. Configure here.

Moving an item (e.g. a bedside carrying a lamp) hitched for seconds and
could hop floors:

- Placement: hovering another floor's furniture, slab, wall, ceiling or
  roof (past the building edge, through a window) re-parented the item to
  that floor mid-gesture. With a level selected, the move tool now ignores
  surfaces on other levels; the floor cursor keeps following the selected
  one.
- Leaving a host back to the floor kept the surface move's live transform,
  a building-space cursor (the host's top, level height included), which
  the remounted item read level-local: a bedside left on a 4th-floor bed
  floated ~10 m up until the next floor move. The detach publishes the
  floor pose.
- Daylight rooms rebuilt (thousands of rays, ~1.6 s) on every scene edit;
  furniture edits and child-list-only changes no longer trigger it (a moved
  lamp still does, through its light's position).
- The live stair/elevator opening systems replanned on a level's child
  list changing; they now ignore child lists, as the document reconcile
  already does.
- Node batching cascaded releases from any dirty node, including a level
  whose child list changed: the whole floor's batches were released and
  rejoined. Levels, buildings and sites no longer cascade (also fixes two
  history tests that expected only a wall's own openings to release).

Measured on a 5-level condo, moving a bedside with an item on top across
the screen: worst frame 2.3 s -> 0.5-0.85 s, no level hops, correct height.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wass08
wass08 merged commit 5837e24 into main Nov 6, 2026
7 of 8 checks passed
@wass08
wass08 deleted the perf/bottosson-port branch November 6, 2026 23:42
@Aymericr Aymericr mentioned this pull request Nov 10, 2026
2 of 4 tasks
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.

2 participants