Skip to content

Commit 4fb2e79

Browse files
authored
Merge pull request #515 from Kitware/fix-gpuinfo
fix(gpuInfo): better fallback logic
2 parents 8dc15e0 + 1e935d7 commit 4fb2e79

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

src/utils/errorReporting.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export const init = (app: App<Element>) => {
1818
dsn: VITE_SENTRY_DSN,
1919
});
2020

21-
Sentry.setContext('gpu', getGPUInfo());
21+
try {
22+
Sentry.setContext('gpu', getGPUInfo());
23+
} catch (err) {
24+
Sentry.captureException(err);
25+
}
2226
};
2327

2428
const setEnabled = (enabled: boolean) => {

src/utils/gpuInfo.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
import { Maybe } from '@/src/types';
1+
function getContextFromOffscreenCanvas() {
2+
if (typeof OffscreenCanvas === 'undefined') return null;
3+
const canvas = new OffscreenCanvas(1, 1);
4+
return canvas.getContext('webgl2') as WebGL2RenderingContext | null;
5+
}
6+
7+
function getContextFromHTMLCanvas() {
8+
if (typeof document === 'undefined') return null;
9+
const canvas = document.createElement('canvas');
10+
return canvas.getContext('webgl2') as WebGL2RenderingContext | null;
11+
}
212

313
/**
414
* Retrieves the GPU renderer and vendor info.
515
* @returns
616
*/
717
export function getGPUInfo() {
8-
let canvas: Maybe<OffscreenCanvas | HTMLCanvasElement> = null;
9-
if (typeof OffscreenCanvas !== 'undefined') {
10-
canvas = new OffscreenCanvas(1, 1);
11-
} else if (typeof document !== 'undefined') {
12-
canvas = document.createElement('canvas');
13-
} else {
14-
throw new Error('Cannot init a canvas');
15-
}
16-
17-
const gl = canvas.getContext('webgl2') as WebGL2RenderingContext;
18+
// try offscreencanvas to support usage in webworkers
19+
const gl = getContextFromOffscreenCanvas() ?? getContextFromHTMLCanvas();
1820
if (!gl) {
19-
throw new Error('Cannot get a WebGL2 context');
21+
throw new Error('Cannot get a webgl2 context');
2022
}
2123

2224
const info = {

0 commit comments

Comments
 (0)