Skip to content
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

Fix loseContext/restoreContext #8835

Merged
merged 2 commits into from Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions packages/core/src/context/ContextSystem.ts
Expand Up @@ -120,12 +120,6 @@ export class ContextSystem implements ISystem<ContextOptions>
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();
}
}

init(options: ContextOptions): void
Expand Down Expand Up @@ -240,6 +234,7 @@ export class ContextSystem implements ISystem<ContextOptions>
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 @@ -258,7 +253,6 @@ export class ContextSystem implements ISystem<ContextOptions>
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 @@ -285,7 +279,17 @@ export class ContextSystem implements ISystem<ContextOptions>
*/
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(() =>
bigtimebuddy marked this conversation as resolved.
Show resolved Hide resolved
{
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
Expand Up @@ -53,6 +53,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 @@ -61,8 +63,6 @@ export class FramebufferSystem implements ISystem
this.hasMRT = true;
this.writeDepthTexture = true;

this.disposeAll(true);

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