Skip to content

Batch: Only update the previous matrices texture when the matrices change. - #35057

Merged
Mugen87 merged 2 commits into
mrdoob:devfrom
HEOJUNFO:batch-previous-matrices-update
Oct 17, 2026
Merged

Mugen87 merged 2 commits into
mrdoob:devfrom
HEOJUNFO:batch-previous-matrices-update

Conversation

@HEOJUNFO

Copy link
Copy Markdown
Contributor

Fixed #35042.

Description

When a BatchedMesh is rendered with velocity (TRAA, motion blur), the OnAfterObjectUpdate callback in Batch.js copies _matricesTexture into previousMatricesTexture and sets needsUpdate after every draw, so the whole matrices texture is copied on the CPU and uploaded again each frame even when nothing moved.

This PR keeps the source texture and its version in the cached previous-matrices data and only copies when either has changed. The copy runs after the draw, so when the version is unchanged the previous texture already holds the current matrices and skipping is safe. The texture reference is part of the check so a different _matricesTexture is never treated as unchanged.

previousMatricesTexture is now also marked needsUpdate when it's created. Before, its first upload happened only after the first draw, so the first frame read an empty texture and produced velocity for every static instance.

Tested with a page that renders a 3-instance BatchedMesh into mrt( { output, velocity } ) for 9 frames, moving one instance on frames 2 and 5, with both the WebGPU and WebGL backends:

  • previousMatricesTexture uploads: 8 → 3 (creation + the frame after each move).
  • Velocity on the move frames is identical (same pixel count and sum), and zero on the other frames.
  • Frame 0 no longer reports velocity for static instances (192 px → 0).

npm run lint on the changed file and npm run test-unit pass. E2E for webgpu_mesh_batch, webgpu_shadowmap_array, webgpu_postprocessing_motion_blur, webgl_mesh_batch and webgl_batch_lod_bvh show no diff.

Unrelated, but found while testing: calling setInstanceCount() with a larger count after the first frame throws RangeError: offset is out of bounds in this callback, because getPreviousNode() returns the cached previousMatricesTexture sized for the old _matricesTexture. This PR doesn't change that behavior; I can open a separate issue/PR for it.

🤖 Generated with Claude Code

…ange.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

📦 Bundle size

Full ESM build including core, minified and gzipped.

Before After Diff
Core 392.32
102.83
392.32
102.83
+0 B
+0 B
WebGL 778.74
194.39
778.74
194.39
+0 B
+0 B
WebGPU 1102.95
296.89
1103.04
296.91
+86 B
+27 B
WebGPU Nodes 1100.90
296.54
1100.98
296.57
+86 B
+28 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 518.96
124.67
518.96
124.67
+0 B
+0 B
WebGPU 781.17
209.10
781.26
209.13
+86 B
+30 B
WebGPU Nodes 727.34
195.70
727.42
195.73
+86 B
+28 B

@Mugen87 Mugen87 added this to the r187 milestone Oct 17, 2026
@Mugen87
Mugen87 merged commit 7b20501 into mrdoob:dev Oct 17, 2026
10 checks passed
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.

WebGPU: BatchedMesh previousMatricesTexture optimization

2 participants