fix(webgl): cache support probe so it creates a context at most once#37
Merged
Conversation
`getWebglSupportedVersions` creates a real WebGL context (`getContext`) to test support, but only cached the result when called with the *default* id list (reference equality against the internal `WEBGL_CONTEXT_IDS`). Any caller passing its own array bypassed the cache and created a brand-new context on every call. On embedded TV boxes (Apollo/Sunrise, Chrome 38+) the live-context budget is tiny. Re-probing repeatedly blows that budget; the browser evicts the oldest context — the live render context — and the renderer then fails every `createTexture()`, spamming "Could not create WebGL Texture" each frame. The device console showed both "too many active WebGL contexts" and "INVALID_OPERATION: loseContext: context already lost" originating here. Changes: - Cache per distinct id list (Map keyed on the joined ids) so a probe context is created at most once per page, regardless of how the caller invokes it. - Release the probe context via WEBGL_lose_context, guarded by isContextLost() so we never call loseContext on a context the browser already evicted (the source of the "context already lost" console spam). Detection output is unchanged for any given argument — only the number of contexts created changes (now: one per arg list, ever). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
What
getWebglSupportedVersions(src/utils.ts) creates a real WebGL context (canvas.getContext) to detect support, but it only cached the result when called with the default id list (reference equality against the internalWEBGL_CONTEXT_IDS). Any caller passing its own array — which our app does — bypassed the cache and created a new WebGL context on every call.This PR:
Mapkeyed on the joined ids), so a probe context is created at most once per page, regardless of how the caller invokes it.WEBGL_lose_context, guarded byisContextLost()so we never callloseContext()on a context the browser already evicted.Why
Reported on Apollo/Sunrise TV boxes (Chrome 38+). The device console showed, all originating at this function:
Embedded TV SoCs cap the number of live WebGL contexts very low. Re-probing on every call blows that budget; the browser evicts the oldest context — the live render context — and the renderer then fails every
createTexture(), spammingCould not create WebGL TextureeachrequestAnimationFrame. A priorloseContext-only attempt didn't help (and produced thecontext already losterror) because the eviction happens at context creation time — the cure is to stop creating contexts repeatedly.Notes for reviewers
supportsWebGL/supportsWebGL2/supportsOnlyWebGL2results) is identical — only the number of contexts created changes (now: one per arg list, ever).detectPremultiplyAlphaHonored) is fixed separately insolid-tv/renderer: fix(webgl): release throwaway context in premultiply-alpha probe renderer#114Tests
New tests/webgl-support.test.ts (4 tests) asserts the behavioral guarantees:
loseContextis skipped when the context is already lostnpx vitest run→ full suite 134/134 pass;tsc --noEmitclean; prettier + eslint clean.🤖 Generated with Claude Code