From b84bfbc7ef188275f7fdabadf68d6016338d8f61 Mon Sep 17 00:00:00 2001 From: Ib Green <7025232+ibgreen@users.noreply.github.com> Date: Tue, 10 Dec 2024 09:36:47 -0500 Subject: [PATCH] fix(webgl): Fix webglAdapter.attach() (#2299) --- modules/webgl/src/adapter/webgl-adapter.ts | 13 ++++++--- modules/webgl/src/adapter/webgl-device.ts | 31 +++++++++++++--------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/modules/webgl/src/adapter/webgl-adapter.ts b/modules/webgl/src/adapter/webgl-adapter.ts index c0008819ed..443803b3ee 100644 --- a/modules/webgl/src/adapter/webgl-adapter.ts +++ b/modules/webgl/src/adapter/webgl-adapter.ts @@ -45,7 +45,8 @@ export class WebGLAdapter extends Adapter { /** * Get a device instance from a GL context - * Creates and instruments the device if not already created + * Creates a WebGLCanvasContext against the contexts canvas + * @note autoResize will be disabled, assuming that whoever created the external context will be handling resizes. * @param gl * @returns */ @@ -62,7 +63,13 @@ export class WebGLAdapter extends Adapter { if (!isWebGL(gl)) { throw new Error('Invalid WebGL2RenderingContext'); } - return new WebGLDevice({_handle: gl as WebGL2RenderingContext}); + + // We create a new device using the provided WebGL context and its canvas + // Assume that whoever created the external context will be handling resizes. + return new WebGLDevice({ + _handle: gl, + createCanvasContext: {canvas: gl.canvas, autoResize: false} + }); } async create(props: DeviceProps = {}): Promise { @@ -106,7 +113,7 @@ ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContex } /** Check if supplied parameter is a WebGL2RenderingContext */ -function isWebGL(gl: any): boolean { +function isWebGL(gl: any): gl is WebGL2RenderingContext { if (typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext) { return true; } diff --git a/modules/webgl/src/adapter/webgl-device.ts b/modules/webgl/src/adapter/webgl-device.ts index e174f758a2..5841fab23b 100644 --- a/modules/webgl/src/adapter/webgl-device.ts +++ b/modules/webgl/src/adapter/webgl-device.ts @@ -144,19 +144,24 @@ export class WebGLDevice extends Device { webglContextAttributes.powerPreference = props.powerPreference; } - const gl = createBrowserContext( - this.canvasContext.canvas, - { - onContextLost: (event: Event) => - this._resolveContextLost?.({ - reason: 'destroyed', - message: 'Entered sleep mode, or too many apps or browser tabs are using the GPU.' - }), - // eslint-disable-next-line no-console - onContextRestored: (event: Event) => console.log('WebGL context restored') - }, - webglContextAttributes - ); + // Check if we should attach to an externally created context or create a new context + const externalGLContext = this.props._handle as WebGL2RenderingContext | null; + + const gl = + externalGLContext || + createBrowserContext( + this.canvasContext.canvas, + { + onContextLost: (event: Event) => + this._resolveContextLost?.({ + reason: 'destroyed', + message: 'Entered sleep mode, or too many apps or browser tabs are using the GPU.' + }), + // eslint-disable-next-line no-console + onContextRestored: (event: Event) => console.log('WebGL context restored') + }, + webglContextAttributes + ); if (!gl) { throw new Error('WebGL context creation failed');