Fix WebGPU crashes from disposed geometry in indirect rendering#1062
Merged
Conversation
The Three.js render would go dark after ~7 minutes of gameplay due to WebGPU failing to set the index buffer on GPURenderPassEncoder. Root cause: When GPUIndirectRenderer cloned geometry attributes, the cloned buffers weren't immediately marked as needing update. WebGPU would lazily create GPU buffers, potentially sharing them with the source geometry. When UnitRenderer disposed inactive instanced meshes after INACTIVE_MESH_CLEANUP_FRAMES, the shared GPU buffers would be invalidated, causing the cloned geometry to reference invalid buffers. Fix: 1. Mark cloned attributes and index buffers as needsUpdate=true in GPUIndirectRenderer.createIndirectMesh() to force fresh GPU buffer creation 2. Add per-frame validation in updateMeshVisibility() to catch disposed geometry before rendering 3. Call updateMeshVisibility() every frame in UnitRenderer after culling to ensure invalid meshes are hidden before the render pass https://claude.ai/code/session_01E36JDY27fQMf5EzB3KMZD4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes WebGPU crashes caused by disposed geometry being used in indirect rendering. The issue occurs when source geometries are disposed while cloned geometries still reference their buffers, causing "setIndexBuffer" errors and rendering crashes.
Key Changes
Force GPU buffer recreation for cloned attributes: Set
needsUpdate = trueon cloned geometry attributes and indices to ensure WebGPU creates fresh GPU buffers instead of lazily sharing buffers with the source geometry.Add pre-render validation: Introduced
updateMeshVisibility()call in the render loop (after culling, before rendering) to catch disposed geometry before it causes WebGPU crashes.Enhance disposed geometry detection: Improved validation logic in
updateMeshVisibility()to detect when index buffers have been disposed (by checking if the array becomes null) and mark meshes as invalid before rendering.Better error tracking: Added debug warnings when geometry is detected as invalid during visibility updates for easier troubleshooting.
Implementation Details
The root cause was that Three.js geometry cloning could create references to the same underlying GPU buffers as the source geometry. When the source geometry was disposed elsewhere, these buffers became invalid, but the cloned geometry still tried to use them. By explicitly setting
needsUpdate = trueon cloned attributes, we force WebGPU to allocate new buffers.The pre-render validation acts as a safety net to catch any remaining edge cases where disposed geometry slips through, preventing crashes and providing diagnostic information.
https://claude.ai/code/session_01E36JDY27fQMf5EzB3KMZD4