Skip to content

Commit

Permalink
Fix loseContext/restoreContext (#9051)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Karl <matt@mattkarl.com>
  • Loading branch information
yordan-kanchelov and bigtimebuddy committed Jan 11, 2023
1 parent 414b52e commit 16d1511
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 11 additions & 7 deletions packages/core/src/context/ContextSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ export class ContextSystem implements ISystem
this.gl = gl;
this.renderer.gl = gl;
this.renderer.CONTEXT_UID = CONTEXT_UID_COUNTER++;

// restore a context if it was previously lost
if (gl.isContextLost() && gl.getExtension('WEBGL_lose_context'))
{
gl.getExtension('WEBGL_lose_context').restoreContext();
}
}

/**
Expand Down Expand Up @@ -172,6 +166,7 @@ export class ContextSystem implements ISystem
const { gl } = this;

const common = {
loseContext: gl.getExtension('WEBGL_lose_context'),
anisotropicFiltering: gl.getExtension('EXT_texture_filter_anisotropic'),
floatTextureLinear: gl.getExtension('OES_texture_float_linear'),

Expand All @@ -190,7 +185,6 @@ export class ContextSystem implements ISystem
Object.assign(this.extensions, common, {
drawBuffers: gl.getExtension('WEBGL_draw_buffers'),
depthTexture: gl.getExtension('WEBGL_depth_texture'),
loseContext: gl.getExtension('WEBGL_lose_context'),
vertexArrayObject: gl.getExtension('OES_vertex_array_object')
|| gl.getExtension('MOZ_OES_vertex_array_object')
|| gl.getExtension('WEBKIT_OES_vertex_array_object'),
Expand All @@ -217,7 +211,17 @@ export class ContextSystem implements ISystem
*/
protected handleContextLost(event: WebGLContextEvent): void
{
// Prevent default to be able to restore the context
event.preventDefault();

// Restore the context after this event has exited
setTimeout(() =>
{
if (this.gl.isContextLost() && this.extensions.loseContext)
{
this.extensions.loseContext.restoreContext();
}
}, 0);
}

/** Handles a restored webgl context. */
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/framebuffer/FramebufferSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export class FramebufferSystem implements ISystem
/** Sets up the renderer context and necessary buffers. */
protected contextChange(): void
{
this.disposeAll(true);

const gl = this.gl = this.renderer.gl;

this.CONTEXT_UID = this.renderer.CONTEXT_UID;
Expand All @@ -53,8 +55,6 @@ export class FramebufferSystem implements ISystem
this.hasMRT = true;
this.writeDepthTexture = true;

this.disposeAll(true);

// webgl2
if (this.renderer.context.webGLVersion === 1)
{
Expand Down

0 comments on commit 16d1511

Please sign in to comment.