-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix(viewer): guard 3D viewer against null WebGL context (Sentry MONOREPO-EDITOR-59) #455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anton-pascal
wants to merge
3
commits into
main
Choose a base branch
from
fix/sentry-EDITOR-59
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
13 changes: 13 additions & 0 deletions
13
packages/viewer/src/components/viewer/unsupported-gpu-fallback.tsx
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| export function UnsupportedGpuViewerFallback() { | ||
| return ( | ||
| <div className="flex h-full min-h-64 w-full items-center justify-center bg-[#fafafa] p-6 text-center text-neutral-900"> | ||
| <div className="max-w-md rounded-2xl border border-neutral-200 bg-white p-6 shadow-sm"> | ||
| <h2 className="font-semibold text-lg">3D viewer unavailable</h2> | ||
| <p className="mt-2 text-neutral-600 text-sm"> | ||
| This browser or environment could not initialize WebGPU or WebGL, so Pascal cannot render | ||
| the 3D scene here. Try opening the editor in a browser with hardware acceleration enabled. | ||
| </p> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| // @ts-expect-error — bun:test is provided by the Bun runtime; viewer does not | ||
| // include Bun ambient types in its production declaration build. | ||
| import { describe, expect, mock, test } from 'bun:test' | ||
| import { UnsupportedGpuViewerFallback } from '../components/viewer/unsupported-gpu-fallback' | ||
| import { | ||
| initializeGpuRenderer, | ||
| type RendererBackendParameters, | ||
| type RendererCapabilityCanvas, | ||
| } from './renderer-capability' | ||
|
|
||
| function canvasWithContexts(contexts: Partial<Record<'webgl2', unknown>>) { | ||
| return { | ||
| getContext: (contextId: 'webgl2') => contexts[contextId] ?? null, | ||
| } satisfies RendererCapabilityCanvas | ||
| } | ||
|
|
||
| describe('GPU renderer capability and initialization', () => { | ||
| test('uses a working WebGPU device without requiring WebGL', async () => { | ||
| const device = {} | ||
| const createRenderer = mock(() => ({ init: async () => undefined })) | ||
|
|
||
| const result = await initializeGpuRenderer({ | ||
| createRenderer, | ||
| gpu: { | ||
| requestAdapter: async () => ({ | ||
| requestDevice: async () => device, | ||
| }), | ||
| }, | ||
| }) | ||
|
|
||
| expect(result.status).toBe('ready') | ||
| expect(createRenderer).toHaveBeenCalledWith({ device }) | ||
| }) | ||
|
|
||
| test('reports unsupported when neither WebGPU nor WebGL is available', async () => { | ||
| const createRenderer = mock(() => ({ init: async () => undefined })) | ||
|
|
||
| const result = await initializeGpuRenderer({ | ||
| createRenderer, | ||
| gpu: null, | ||
| probeCanvas: canvasWithContexts({}), | ||
| }) | ||
|
|
||
| expect(result.status).toBe('unsupported') | ||
| expect(createRenderer).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('falls back to WebGL when WebGPU cannot provide a device', async () => { | ||
| const webglContext = {} | ||
| const init = mock(async () => undefined) | ||
| const createRenderer = mock(() => ({ init })) | ||
|
|
||
| const result = await initializeGpuRenderer({ | ||
| createRenderer, | ||
| gpu: { | ||
| requestAdapter: async () => ({ | ||
| requestDevice: async () => { | ||
| throw new Error('device unavailable') | ||
| }, | ||
| }), | ||
| }, | ||
| probeCanvas: canvasWithContexts({ webgl2: webglContext }), | ||
| }) | ||
|
|
||
| expect(result.status).toBe('ready') | ||
| expect(createRenderer).toHaveBeenCalledWith({ forceWebGL: true }) | ||
| expect(init).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| test('falls back to WebGL when WebGPU renderer initialization fails', async () => { | ||
| const device = {} | ||
| const webglContext = {} | ||
| const displayGetContext = mock((_contextId: 'webgl2', attributes?: { antialias?: boolean }) => | ||
| attributes?.antialias ? webglContext : null, | ||
| ) | ||
| const webgpuDispose = mock(() => undefined) | ||
| const webglInit = mock(async () => undefined) | ||
| const parameters: RendererBackendParameters[] = [] | ||
|
|
||
| const result = await initializeGpuRenderer({ | ||
| createRenderer: (backendParameters) => { | ||
| parameters.push(backendParameters) | ||
| if (backendParameters.device) { | ||
| return { | ||
| dispose: webgpuDispose, | ||
| init: async () => { | ||
| throw new Error('WebGPU renderer init failed') | ||
| }, | ||
| } | ||
| } | ||
| return { | ||
| init: async () => { | ||
| if (!displayGetContext('webgl2', { antialias: true })) { | ||
| throw new Error('WebGL context unavailable') | ||
| } | ||
| await webglInit() | ||
| }, | ||
| } | ||
| }, | ||
| gpu: { | ||
| requestAdapter: async () => ({ | ||
| requestDevice: async () => device, | ||
| }), | ||
| }, | ||
| }) | ||
|
|
||
| expect(result.status).toBe('ready') | ||
| if (result.status === 'ready') expect(result.backend).toBe('webgl') | ||
| expect(parameters).toEqual([{ device }, { forceWebGL: true }]) | ||
| expect(displayGetContext).toHaveBeenCalledWith('webgl2', { antialias: true }) | ||
| expect(webgpuDispose).toHaveBeenCalledTimes(1) | ||
| expect(webglInit).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| test('isolates WebGL capability probing from the display canvas', async () => { | ||
| const probeContext = {} | ||
| const displayContext = {} | ||
| const probeGetContext = mock(() => probeContext) | ||
| const displayGetContext = mock((_contextId: 'webgl2', attributes?: { antialias?: boolean }) => | ||
| attributes?.antialias ? displayContext : null, | ||
| ) | ||
|
|
||
| const result = await initializeGpuRenderer({ | ||
| createRenderer: (backendParameters) => ({ | ||
| init: async () => { | ||
| expect(backendParameters).toEqual({ forceWebGL: true }) | ||
| expect(displayGetContext('webgl2', { antialias: true })).toBe(displayContext) | ||
| }, | ||
| }), | ||
| gpu: null, | ||
| probeCanvas: { getContext: probeGetContext }, | ||
| }) | ||
|
|
||
| expect(result.status).toBe('ready') | ||
| expect(probeGetContext).toHaveBeenCalledTimes(1) | ||
| expect(probeGetContext).toHaveBeenCalledWith('webgl2') | ||
| expect(displayGetContext).toHaveBeenCalledTimes(1) | ||
| expect(displayGetContext).toHaveBeenCalledWith('webgl2', { antialias: true }) | ||
| }) | ||
|
|
||
| test('times out a hung WebGPU adapter request and falls back to WebGL', async () => { | ||
| const createRenderer = mock(() => ({ init: async () => undefined })) | ||
|
|
||
| const result = await initializeGpuRenderer({ | ||
| createRenderer, | ||
| gpu: { | ||
| requestAdapter: () => new Promise<never>(() => undefined), | ||
| }, | ||
| probeCanvas: canvasWithContexts({ webgl2: {} }), | ||
| webgpuTimeoutMs: 10, | ||
| }) | ||
|
|
||
| expect(result.status).toBe('ready') | ||
| if (result.status === 'ready') expect(result.backend).toBe('webgl') | ||
| expect(createRenderer).toHaveBeenCalledWith({ forceWebGL: true }) | ||
| }) | ||
|
|
||
| test('reports unsupported after a hung WebGPU adapter times out without WebGL', async () => { | ||
| const result = await initializeGpuRenderer({ | ||
| createRenderer: () => ({ init: async () => undefined }), | ||
| gpu: { | ||
| requestAdapter: () => new Promise<never>(() => undefined), | ||
| }, | ||
| probeCanvas: canvasWithContexts({}), | ||
| webgpuTimeoutMs: 10, | ||
| }) | ||
|
|
||
| expect(result.status).toBe('unsupported') | ||
| expect(JSON.stringify(UnsupportedGpuViewerFallback())).toContain('3D viewer unavailable') | ||
| }) | ||
|
|
||
| test('times out hung WebGPU renderer initialization and falls back to WebGL', async () => { | ||
| const device = {} | ||
| const webgpuDispose = mock(() => undefined) | ||
| const parameters: RendererBackendParameters[] = [] | ||
|
|
||
| const result = await initializeGpuRenderer({ | ||
| createRenderer: (backendParameters) => { | ||
| parameters.push(backendParameters) | ||
| return backendParameters.device | ||
| ? { | ||
| dispose: webgpuDispose, | ||
| init: () => new Promise<never>(() => undefined), | ||
| } | ||
| : { init: async () => undefined } | ||
| }, | ||
| gpu: { | ||
| requestAdapter: async () => ({ requestDevice: async () => device }), | ||
| }, | ||
| webgpuTimeoutMs: 10, | ||
| }) | ||
|
|
||
| expect(result.status).toBe('ready') | ||
| if (result.status === 'ready') expect(result.backend).toBe('webgl') | ||
| expect(parameters).toEqual([{ device }, { forceWebGL: true }]) | ||
| expect(webgpuDispose).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| test('reports unsupported when WebGPU device and WebGL are unavailable', async () => { | ||
| const result = await initializeGpuRenderer({ | ||
| createRenderer: () => ({ init: async () => undefined }), | ||
| gpu: { | ||
| requestAdapter: async () => null, | ||
| }, | ||
| probeCanvas: canvasWithContexts({}), | ||
| }) | ||
|
|
||
| expect(result.status).toBe('unsupported') | ||
| }) | ||
|
|
||
| test('catches renderer initialization failure and selects the fallback UI', async () => { | ||
| const dispose = mock(() => undefined) | ||
|
|
||
| const result = await initializeGpuRenderer({ | ||
| createRenderer: () => ({ | ||
| dispose, | ||
| init: async () => { | ||
| throw new Error('getSupportedExtensions on null context') | ||
| }, | ||
| }), | ||
| gpu: null, | ||
| probeCanvas: canvasWithContexts({ webgl2: {} }), | ||
| }) | ||
|
|
||
| expect(result.status).toBe('unsupported') | ||
| expect(dispose).toHaveBeenCalledTimes(1) | ||
| expect(JSON.stringify(UnsupportedGpuViewerFallback())).toContain('3D viewer unavailable') | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.