Skip to content

Handle WebGL context loss, restoration, and disposal - #289

Open
arimxyer wants to merge 4 commits into
paper-design:mainfrom
arimxyer:fix/webgl-context-recovery
Open

Handle WebGL context loss, restoration, and disposal#289
arimxyer wants to merge 4 commits into
paper-design:mainfrom
arimxyer:fix/webgl-context-recovery

Conversation

@arimxyer

@arimxyer arimxyer commented Aug 26, 2026

Copy link
Copy Markdown

Summary

  • handle webglcontextlost for live ShaderMount instances by preventing the default, cancelling the render loop, and retaining logical frame, speed, visibility, sizing, and uniform state
  • rebuild every invalidated WebGL resource/state on webglcontextrestored: program, position buffer/attribute, uniform locations and values, textures, viewport, and resolution uniforms
  • make dispose() terminal and idempotent by removing lifecycle listeners, deleting owned resources, and calling WEBGL_lose_context.loseContext() without opting the disposed mount into restoration
  • add real Chromium coverage for forced context loss/restoration and repeated mount/dispose cycles

No 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_context is 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-lockfile
  • bun run --filter @paper-design/shaders type-check
  • bun run --filter @paper-design/shaders-react type-check
  • bun run clean && bun run build
  • bun test — 21 passed
  • bun run test:browser — 2 passed in Chromium
    • forces loss, proves the event is cancelled and animation stops, mutates frame/speed/uniform/texture/size/visibility while lost, then proves new program/buffer/texture identities, restored viewport and pixels, offscreen pause, and visible animation resume
    • retains 24 disposed WebGL contexts and proves every one is lost, none is restored or cancels its terminal loss event, stale disposal cannot detach the live remount, no canvases remain, no context-limit warning occurs, and a final live mount still animates
  • prettier --check ...
  • git diff --check

Note

Medium Risk
Changes core WebGL lifecycle and disposal in ShaderMount; behavior is complex but covered by new browser tests and fail-closed paths.

Overview
ShaderMount now survives browser-driven WebGL context loss: it cancels the default on webglcontextlost, pauses rendering, and keeps frame, speed, uniforms, and sizing in memory; on webglcontextrestored it 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_context is 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.

@vercel

vercel Bot commented Aug 26, 2026

Copy link
Copy Markdown

@arimxyer is attempting to deploy a commit to the Paper Design Team on Vercel.

A member of the Team first needs to authorize it.

Comment thread packages/shaders/src/shader-mount.ts Outdated
Comment thread packages/shaders/src/shader-mount.ts Outdated

@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 8c78a1f. Configure here.

Comment thread packages/shaders/src/shader-mount.ts Outdated
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.

1 participant