changelog/v3/3.80/WebGLContextRestore.md
Return to the Change Log index.
Phaser now performs a WebGL context restore.
WebGL can lose context when the browser wants to reclaim resources. This happens most often when a browser tab is inactive. When it happens is beyond the control of web developers.
When context is lost, the game canvas goes blank. A lost context loses all the data that was sent to the WebGL system. It cannot do anything until the browser restores the context, usually when the user returns to their tab.
Phaser now restores this data, with a few exceptions, allowing your games to seamlessly resume. For the most part, your WebGL games should now simply recover when the browser restores the WebGL context.
Key concepts:
WebGLRenderer emits LOSE_WEBGL and RESTORE_WEBGL events.Phaser.Renderers.WebGL.WebGLRenderer now emits events that you can use to stay informed about context loss.
Phaser.Renderer.Events#LOSE_WEBGL is emitted when context is lost. This would be a good time to pause the game, or otherwise avoid making changes that the player cannot see.Phaser.Renderer.Events#RESTORE_WEBGL is emitted when context is restored. This would be a good time to restore dynamic textures, and start things moving again.Dynamic textures and render textures are stored on the GPU. When context is lost, the texture is erased. When context is restored, the texture is re-enabled, but it remains blank.
A render texture which is redrawn every frame will naturally redraw itself.
Textures which are drawn once, however, will stay blank. You should listen for the RESTORE_WEBGL event to know when to redraw them.
It is safe to draw while the context is lost, but nothing will be drawn. Snapshots will return blank.
WebGL objects are now wrapped. The wrapper holds the necessary information to automatically recreate the object when context is restored.
This is only relevant if you need deep access to the WebGL renderer.
Wrappers are created and managed by the WebGLRenderer. They have the following qualities:
createResource(), which will create a new WebGL object from stored information.destroy(), which will remove the object. Do not call this directly; it is managed by the WebGLRenderer.The following wrappers are available in the Phaser.Renderer.WebGL.Wrappers namespace:
WebGLAttribLocationWrapperWebGLBufferWrapperWebGLFramebufferWrapperWebGLProgramWrapperWebGLTextureWrapperWebGLUniformLocationWrapperYou can test context loss and restore with a built-in WebGL extension. The following code snippet should be useful for testing purposes. It shows how to get the built-in extension, and simulate losing and restoring the context after 1 and 2 seconds respectively.
const webGLLoseContextExtension = game.renderer.getExtension('WEBGL_lose_context');
setTimeout(function () {
webGLLoseContextExtension.loseContext();
setTimeout(function () {
webGLLoseContextExtension.restoreContext();
}, 1000)
}, 1000);
Several changes were made to the rendering system to support these improvements. In particular, many properties were changed to use wrappers instead of raw WebGL objects. The full list of changes is as follows:
Phaser.FX.Displacement
glTexture property type changed from WebGLTexture to WebGLTextureWrapperBatchChar
texture parameter type changed from WebGLTexture to WebGLTextureWrapperPhaser.GameObjects.Shader
vertexBuffer property type changed from WebGLBuffer to WebGLBufferWrapperprogram property type changed from WebGLProgram to WebGLProgramWrapperframebuffer property type changed from WebGLFramebuffer to WebGLFramebufferWrapperglTexture property type changed from WebGLTexture to WebGLTextureWrapper#initUniforms method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapperPhaser.GameObjects.TileSprite
fillPattern property type changed from ?(WebGLTexture|CanvasPattern) to ?(WebGLTextureWrapper|CanvasPattern)Phaser.Renderer.WebGL.PipelineManager
postPipelineInstances of type PostFXPipeline[]removePostPipeline(pipeline: PostFXPipeline)restoreContext(), used after context is restoredPhaser.Renderer.WebGL.RenderTarget
framebuffer property type changed from WebGLFramebuffer to WebGLFramebufferWrappertexture parameter type changed from WebGLTexture to WebGLTextureWrapper#unbind method:
WebGLFramebuffer to WebGLFramebufferWrapperPhaser.Renderer.WebGL.WebGLPipeline
vertexBuffer property type changed from WebGLBuffer to WebGLBufferWrapperactiveBuffer property type changed from WebGLBuffer to WebGLBufferWrappercurrentTexture property type changed from WebGLTexture to WebGLTextureWrapperactiveTextures property type changed from WebGLTexture[] to WebGLTextureWrapper[]#setShader method:
vertexBuffer parameter type changed from WebGLBuffer to WebGLBufferWrapper#createBatch method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapper#addTextureToBatch method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapper#pushBatch method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapperrestoreContext(), used after context is restored#setVertexBuffer method:
buffer parameter type changed from WebGLBuffer to WebGLBufferWrapper#batchQuad method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapper#batchTri method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapper#drawFillRect method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapper#setTexture2D method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapper#bindTexture method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapperPhaser.Renderer.WebGL.WebGLRenderer
glBufferWrappers of type WebGLBufferWrapper[]glProgramWrappers of type WebGLProgramWrapper[]glTextureWrappers of type WebGLTextureWrapper[]glFramebufferWrappers of type WebGLFramebufferWrapper[]glAttribLocationWrappers of type WebGLAttribLocationWrapper[]glUniformLocationWrappers of type WebGLUniformLocationWrapper[]normalTexture of type WebGLTextureWrappercurrentFramebuffer property type changed from WebGLFramebuffer to WebGLFramebufferWrapperfboStack property type changed from WebGLFramebuffer[] to WebGLFramebufferWrapper[]currentProgram property type changed from WebGLProgram to WebGLProgramWrapperblankTexture property type changed from WebGLTexture to WebGLTextureWrapperwhiteTexture property type changed from WebGLTexture to WebGLTextureWrappercreateTemporaryTextures(), used when booting on MacOS#pushFramebuffer method:
framebuffer parameter changed from WebGLFramebuffer to WebGLFramebufferWrappertexture parameter changed from WebGLTexture to WebGLTextureWrapper#setFramebuffer method:
framebuffer parameter changed from WebGLFramebuffer to WebGLFramebufferWrappertexture parameter changed from WebGLTexture to WebGLTextureWrapper#popFramebuffer method:
WebGLFramebuffer to WebGLFramebufferWrapper#setProgram method:
program parameter changed from WebGLProgram to WebGLFramebufferWrapper#createTextureFromSource method:
WebGLTexture to WebGLTextureWrapper#createTexture2D method:
width parameter made optionalheight parameter made optionalWebGLTexture to WebGLTextureWrapper#createFramebuffer method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapperWebGLFramebuffer to WebGLFramebufferWrapper#createProgram method:
WebGLProgram to WebGLProgramWrapper#createVertexBuffer method:
WebGLBuffer to WebGLBufferWrappercreateAttribLocationcreateUniformLocation#createIndexBuffer method:
WebGLBuffer to WebGLBufferWrapper#deleteTexture method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapper#deleteFramebuffer method:
framebuffer parameter type changed from WebGLFramebuffer to WebGLFramebufferWrapper#deleteProgram method:
program parameter type changed from WebGLProgram to WebGLProgramWrapperdeleteAttribLocationdeleteUniformLocation#deleteBuffer method:
vertexBuffer parameter type changed from WebGLBuffer to WebGLBufferWrapper#snapshotFramebuffer method:
framebuffer parameter changed from WebGLFramebuffer to WebGLFramebufferWrapper#canvasToTexture method:
dstTexture parameter type changed from WebGLTexture to WebGLTextureWrapperWebGLTexture to WebGLTextureWrapper#createCanvasTexture method:
WebGLTexture to WebGLTextureWrapper#updateCanvasTexture method:
dstTexture parameter type changed from WebGLTexture to WebGLTextureWrapperWebGLTexture to WebGLTextureWrapper#createVideoTexture method:
WebGLTexture to WebGLTextureWrapper#updateCanvasTexture method:
dstTexture parameter type changed from WebGLTexture to WebGLTextureWrapperWebGLTexture to WebGLTextureWrappercreateUint8ArrayTexture#setTextureFilter method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapperPhaser.Renderers.WebGL.WebGLShader
program property type changed from WebGLProgram to WebGLProgramWrappersyncUniforms for use during context recoveryPhaser.Renderers.WebGL.Pipelines.LightPipeline
defaultNormalMap. There is now a default normal map at WebGLRenderer.normalTexture, or the texture key '__NORMAL'.currentNormalMap property type changed from WebGLTexture to WebGLTextureWrapper#setTexture2D method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapper#isNewNormalMap method:
texture parameter type changed from WebGLTexture to WebGLTextureWrappernormalMap parameter type changed from WebGLTexture to WebGLTextureWrapper#getNormalMap method:
WebGLTexture to WebGLTextureWrapper#batchTexture method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapperPhaser.Renderers.WebGL.Pipelines.MultiPipeline
#batchTexture method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapperPhaser.Renderers.WebGL.Pipelines.PreFXPipeline
quadVertexBuffer property type changed from WebGLBuffer to WebGLBufferWrapper#batchQuad method:
texture parameter type changed from WebGLTexture to WebGLTextureWrapperPhaser.Renderers.WebGL.Pipelines.DisplacementFXPipeline
glTexture property type changed from WebGLTexture to WebGLTextureWrapperWebGLPipelineAttribute
location property changed from number to -1|WebGLAttribLocationWrapperWebGLPipelineBatchEntry
texture property changed from WebGLTexture to WebGLTextureWrapperWebGLPipelineUniformsConfig
setter of type ?function for use in restoring contextPhaser.Textures.DynamicTexture
#getWebGLTexture method:
WebGLTexture to WebGLTextureWrapperPhaser.Textures.Frame
glTexture property type changed from WebGLTexture to WebGLTextureWrapper
Phaser.Textures.Texture
source parameter type options added WebGLTextureWrapperPhaser.Textures.TextureManager
'__NORMAL' is created on boot if the WebGL renderer is being used. This is a 1x1 texture of colour #7f7fff, or a flat normal map.#addGLTexture method:
glTexture parameter type changed from WebGLTexture to WebGLTextureWrapperwidth parameter removedheight parameter removed#getTextureKeys now excludes '__NORMAL' as well as '__DEFAULT', '__MISSING', and '__WHITE'.addUint8Array for creating textures from raw colour dataPhaser.Textures.TextureSource
glTexture property type changed from WebGLTexture to WebGLTextureWrapperPhaser.Tilemaps.Tileset
glTexture property type changed from WebGLTexture to WebGLTextureWrapper