Handle WebGL context loss, restoration, and disposal - #289
Open
arimxyer wants to merge 4 commits into
Open
Conversation
|
@arimxyer is attempting to deploy a commit to the Paper Design Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 8c78a1f. Configure here.
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
webglcontextlostfor liveShaderMountinstances by preventing the default, cancelling the render loop, and retaining logical frame, speed, visibility, sizing, and uniform statewebglcontextrestored: program, position buffer/attribute, uniform locations and values, textures, viewport, and resolution uniformsdispose()terminal and idempotent by removing lifecycle listeners, deleting owned resources, and callingWEBGL_lose_context.loseContext()without opting the disposed mount into restorationNo public API changes are required.
Rationale
WebGL context restoration does not preserve WebGL objects or extension state, so resuming the existing render loop is insufficient; resources must be recreated from retained logical state. The WebGL context lifecycle specification defines the loss/restoration event contract, and
WEBGL_lose_contextis the recommended mechanism to programmatically destroy the underlying context and graphics resources.Issue #193 and PR #196 fixed the React context-attributes footgun and removed disposed canvases, but did not cover browser-driven context recovery or explicitly relinquish the underlying context. This PR keeps that behavior and adds the missing core lifecycle.
Tests
bun install --frozen-lockfilebun run --filter @paper-design/shaders type-checkbun run --filter @paper-design/shaders-react type-checkbun run clean && bun run buildbun test— 21 passedbun run test:browser— 2 passed in Chromiumprettier --check ...git diff --checkNote
Medium Risk
Changes core WebGL lifecycle and disposal in
ShaderMount; behavior is complex but covered by new browser tests and fail-closed paths.Overview
ShaderMountnow survives browser-driven WebGL context loss: it cancels the default onwebglcontextlost, pauses rendering, and keeps frame, speed, uniforms, and sizing in memory; onwebglcontextrestoredit rebuilds programs, buffers, textures, and uniform state and resumes animation without jumping time forward.Disposal is terminal and idempotent—listeners are removed first, owned GL objects are deleted, and
WEBGL_lose_contextis used so disposed mounts cannot opt into restoration (including edge cases where loss was already prevented).Construction and restoration fail closed: bad initial setup or failed rebuild disposes the mount and leaves no live canvas. Texture/uniform errors throw instead of silently logging.
Playwright Chromium tests (
test:browser) exercise forced loss/restore, failed rebuild paths, and repeated mount/dispose cycles; CI installs Chromium and runs them when packages, browser tests, or lockfile change.Reviewed by Cursor Bugbot for commit 9d1dcd0. Bugbot is set up for automated code reviews on this repo. Configure here.