Skip to content

Commit

Permalink
Merge pull request #515 from Kitware/fix-gpuinfo
Browse files Browse the repository at this point in the history
fix(gpuInfo): better fallback logic
  • Loading branch information
floryst authored Nov 22, 2023
2 parents 8dc15e0 + 1e935d7 commit 4fb2e79
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/utils/errorReporting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export const init = (app: App<Element>) => {
dsn: VITE_SENTRY_DSN,
});

Sentry.setContext('gpu', getGPUInfo());
try {
Sentry.setContext('gpu', getGPUInfo());
} catch (err) {
Sentry.captureException(err);
}
};

const setEnabled = (enabled: boolean) => {
Expand Down
26 changes: 14 additions & 12 deletions src/utils/gpuInfo.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { Maybe } from '@/src/types';
function getContextFromOffscreenCanvas() {
if (typeof OffscreenCanvas === 'undefined') return null;
const canvas = new OffscreenCanvas(1, 1);
return canvas.getContext('webgl2') as WebGL2RenderingContext | null;
}

function getContextFromHTMLCanvas() {
if (typeof document === 'undefined') return null;
const canvas = document.createElement('canvas');
return canvas.getContext('webgl2') as WebGL2RenderingContext | null;
}

/**
* Retrieves the GPU renderer and vendor info.
* @returns
*/
export function getGPUInfo() {
let canvas: Maybe<OffscreenCanvas | HTMLCanvasElement> = null;
if (typeof OffscreenCanvas !== 'undefined') {
canvas = new OffscreenCanvas(1, 1);
} else if (typeof document !== 'undefined') {
canvas = document.createElement('canvas');
} else {
throw new Error('Cannot init a canvas');
}

const gl = canvas.getContext('webgl2') as WebGL2RenderingContext;
// try offscreencanvas to support usage in webworkers
const gl = getContextFromOffscreenCanvas() ?? getContextFromHTMLCanvas();
if (!gl) {
throw new Error('Cannot get a WebGL2 context');
throw new Error('Cannot get a webgl2 context');
}

const info = {
Expand Down

0 comments on commit 4fb2e79

Please sign in to comment.