14 KiB
Phaser 3.80.0 Change Log
Return to the Change Log index.
WebGL Context Restore
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
emitsLOSE_WEBGL
andRESTORE_WEBGL
events.- Dynamic textures are cleared.
- WebGL objects are now enclosed in wrappers.
- You can test WebGL context restore with WebGL extensions.
- Supporting changes were made to the rendering system.
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
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 Object Wrappers
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:
- A property for the wrapped WebGL object, for use in rendering. This property should only be accessed within a call to WebGL; it may be changed within the wrapper.
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 theWebGLRenderer
.
The following wrappers are available in the Phaser.Renderer.WebGL.Wrappers
namespace:
WebGLAttribLocationWrapper
WebGLBufferWrapper
WebGLFramebufferWrapper
WebGLProgramWrapper
WebGLTextureWrapper
WebGLUniformLocationWrapper
Testing Context Restore
You 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);
Supporting Changes
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 fromWebGLTexture
toWebGLTextureWrapper
BatchChar
- Constructor:
texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
- Constructor:
Phaser.GameObjects.Shader
vertexBuffer
property type changed fromWebGLBuffer
toWebGLBufferWrapper
program
property type changed fromWebGLProgram
toWebGLProgramWrapper
framebuffer
property type changed fromWebGLFramebuffer
toWebGLFramebufferWrapper
glTexture
property type changed fromWebGLTexture
toWebGLTextureWrapper
#initUniforms
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
Phaser.GameObjects.TileSprite
fillPattern
property type changed from?(WebGLTexture|CanvasPattern)
to?(WebGLTextureWrapper|CanvasPattern)
Phaser.Renderer.WebGL.PipelineManager
- Added property
postPipelineInstances
of typePostFXPipeline[]
- Added method
removePostPipeline(pipeline: PostFXPipeline)
- Added method
restoreContext()
, used after context is restored
- Added property
Phaser.Renderer.WebGL.RenderTarget
framebuffer
property type changed fromWebGLFramebuffer
toWebGLFramebufferWrapper
texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
#unbind
method:- Return type changed from
WebGLFramebuffer
toWebGLFramebufferWrapper
- Return type changed from
Phaser.Renderer.WebGL.WebGLPipeline
vertexBuffer
property type changed fromWebGLBuffer
toWebGLBufferWrapper
activeBuffer
property type changed fromWebGLBuffer
toWebGLBufferWrapper
currentTexture
property type changed fromWebGLTexture
toWebGLTextureWrapper
activeTextures
property type changed fromWebGLTexture[]
toWebGLTextureWrapper[]
#setShader
method:vertexBuffer
parameter type changed fromWebGLBuffer
toWebGLBufferWrapper
#createBatch
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
#addTextureToBatch
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
#pushBatch
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
- Added method
restoreContext()
, used after context is restored #setVertexBuffer
method:buffer
parameter type changed fromWebGLBuffer
toWebGLBufferWrapper
#batchQuad
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
#batchTri
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
#drawFillRect
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
#setTexture2D
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
#bindTexture
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
Phaser.Renderer.WebGL.WebGLRenderer
- Added property
glBufferWrappers
of typeWebGLBufferWrapper[]
- Added property
glProgramWrappers
of typeWebGLProgramWrapper[]
- Added property
glTextureWrappers
of typeWebGLTextureWrapper[]
- Added property
glFramebufferWrappers
of typeWebGLFramebufferWrapper[]
- Added property
glAttribLocationWrappers
of typeWebGLAttribLocationWrapper[]
- Added property
glUniformLocationWrappers
of typeWebGLUniformLocationWrapper[]
- Added property
normalTexture
of typeWebGLTextureWrapper
currentFramebuffer
property type changed fromWebGLFramebuffer
toWebGLFramebufferWrapper
fboStack
property type changed fromWebGLFramebuffer[]
toWebGLFramebufferWrapper[]
currentProgram
property type changed fromWebGLProgram
toWebGLProgramWrapper
blankTexture
property type changed fromWebGLTexture
toWebGLTextureWrapper
whiteTexture
property type changed fromWebGLTexture
toWebGLTextureWrapper
- Added method
createTemporaryTextures()
, used when booting on MacOS #pushFramebuffer
method:framebuffer
parameter changed fromWebGLFramebuffer
toWebGLFramebufferWrapper
texture
parameter changed fromWebGLTexture
toWebGLTextureWrapper
#setFramebuffer
method:framebuffer
parameter changed fromWebGLFramebuffer
toWebGLFramebufferWrapper
texture
parameter changed fromWebGLTexture
toWebGLTextureWrapper
#popFramebuffer
method:- Return type changed from
WebGLFramebuffer
toWebGLFramebufferWrapper
- Return type changed from
#setProgram
method:program
parameter changed fromWebGLProgram
toWebGLFramebufferWrapper
#createTextureFromSource
method:- Return type changed from
WebGLTexture
toWebGLTextureWrapper
- Return type changed from
#createTexture2D
method:width
parameter made optionalheight
parameter made optional- Return type changed from
WebGLTexture
toWebGLTextureWrapper
#createFramebuffer
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
- Return type changed from
WebGLFramebuffer
toWebGLFramebufferWrapper
#createProgram
method:- Return type changed from
WebGLProgram
toWebGLProgramWrapper
- Return type changed from
#createVertexBuffer
method:- Return type changed from
WebGLBuffer
toWebGLBufferWrapper
- Return type changed from
- Added method
createAttribLocation
- Added method
createUniformLocation
#createIndexBuffer
method:- Return type changed from
WebGLBuffer
toWebGLBufferWrapper
- Return type changed from
#deleteTexture
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
#deleteFramebuffer
method:framebuffer
parameter type changed fromWebGLFramebuffer
toWebGLFramebufferWrapper
#deleteProgram
method:program
parameter type changed fromWebGLProgram
toWebGLProgramWrapper
- Added method
deleteAttribLocation
- Added method
deleteUniformLocation
#deleteBuffer
method:vertexBuffer
parameter type changed fromWebGLBuffer
toWebGLBufferWrapper
#snapshotFramebuffer
method:framebuffer
parameter changed fromWebGLFramebuffer
toWebGLFramebufferWrapper
#canvasToTexture
method:dstTexture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
- Return type changed from
WebGLTexture
toWebGLTextureWrapper
#createCanvasTexture
method:- Return type changed from
WebGLTexture
toWebGLTextureWrapper
- Return type changed from
#updateCanvasTexture
method:dstTexture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
- Return type changed from
WebGLTexture
toWebGLTextureWrapper
#createVideoTexture
method:- Return type changed from
WebGLTexture
toWebGLTextureWrapper
- Return type changed from
#updateCanvasTexture
method:dstTexture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
- Return type changed from
WebGLTexture
toWebGLTextureWrapper
- Added method
createUint8ArrayTexture
#setTextureFilter
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
- Added property
Phaser.Renderers.WebGL.WebGLShader
program
property type changed fromWebGLProgram
toWebGLProgramWrapper
- Added method
syncUniforms
for use during context recovery
Phaser.Renderers.WebGL.Pipelines.LightPipeline
- Removed property
defaultNormalMap
. There is now a default normal map atWebGLRenderer.normalTexture
, or the texture key'__NORMAL'
. currentNormalMap
property type changed fromWebGLTexture
toWebGLTextureWrapper
#setTexture2D
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
#isNewNormalMap
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
normalMap
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
#getNormalMap
method:- Return type changed from
WebGLTexture
toWebGLTextureWrapper
- Return type changed from
#batchTexture
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
- Removed property
Phaser.Renderers.WebGL.Pipelines.MultiPipeline
#batchTexture
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
Phaser.Renderers.WebGL.Pipelines.PreFXPipeline
quadVertexBuffer
property type changed fromWebGLBuffer
toWebGLBufferWrapper
#batchQuad
method:texture
parameter type changed fromWebGLTexture
toWebGLTextureWrapper
Phaser.Renderers.WebGL.Pipelines.DisplacementFXPipeline
glTexture
property type changed fromWebGLTexture
toWebGLTextureWrapper
WebGLPipelineAttribute
location
property changed fromnumber
to-1|WebGLAttribLocationWrapper
WebGLPipelineBatchEntry
texture
property changed fromWebGLTexture
toWebGLTextureWrapper
WebGLPipelineUniformsConfig
- Added property
setter
of type?function
for use in restoring context
- Added property
Phaser.Textures.DynamicTexture
#getWebGLTexture
method:- Return type changed from
WebGLTexture
toWebGLTextureWrapper
- Return type changed from
Phaser.Textures.Frame
glTexture
property type changed fromWebGLTexture
toWebGLTextureWrapper
- Property is now read-only
Phaser.Textures.Texture
- Constructor
source
parameter type options addedWebGLTextureWrapper
- Constructor
Phaser.Textures.TextureManager
- A texture with the key
'__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 fromWebGLTexture
toWebGLTextureWrapper
width
parameter removedheight
parameter removed
#getTextureKeys
now excludes'__NORMAL'
as well as'__DEFAULT'
,'__MISSING'
, and'__WHITE'
.- Added method
addUint8Array
for creating textures from raw colour data
- A texture with the key
Phaser.Textures.TextureSource
glTexture
property type changed fromWebGLTexture
toWebGLTextureWrapper
Phaser.Tilemaps.Tileset
glTexture
property type changed fromWebGLTexture
toWebGLTextureWrapper