diff --git a/packages/core/src/framebuffer/FramebufferSystem.js b/packages/core/src/framebuffer/FramebufferSystem.js index 561dfdb..e4bdf16 100644 --- a/packages/core/src/framebuffer/FramebufferSystem.js +++ b/packages/core/src/framebuffer/FramebufferSystem.js @@ -2,6 +2,7 @@ import { Rectangle } from '@pixi/math'; import { ENV } from '@pixi/constants'; import { settings } from '../settings'; +import Framebuffer from './Framebuffer'; /** * System plugin to the renderer to manage framebuffers. @@ -25,6 +26,13 @@ * @readonly */ this.managedFramebuffers = []; + + /** + * Framebuffer value that shows that we don't know what is bound + * @member {Framebuffer} + * @readonly + */ + this.unknownFramebuffer = new Framebuffer(10, 10); } /** @@ -35,7 +43,7 @@ const gl = this.gl = this.renderer.gl; this.CONTEXT_UID = this.renderer.CONTEXT_UID; - this.current = null; + this.current = this.unknownFramebuffer; this.viewport = new Rectangle(); this.hasMRT = true; @@ -72,7 +80,7 @@ * Bind a framebuffer * * @param {PIXI.Framebuffer} framebuffer - * @param {PIXI.Rectangle} frame + * @param {PIXI.Rectangle} [frame] frame, default is framebuffer size */ bind(framebuffer, frame) { @@ -407,4 +415,15 @@ this.disposeFramebuffer(list[i], contextLost); } } + + /** + * resets framebuffer stored state, binds screen framebuffer + * + * should be called before renderTexture reset() + */ + reset() + { + this.current = this.unknownFramebuffer; + this.viewport = new Rectangle(); + } } diff --git a/packages/core/src/framebuffer/FramebufferSystem.js b/packages/core/src/framebuffer/FramebufferSystem.js index 561dfdb..e4bdf16 100644 --- a/packages/core/src/framebuffer/FramebufferSystem.js +++ b/packages/core/src/framebuffer/FramebufferSystem.js @@ -2,6 +2,7 @@ import { Rectangle } from '@pixi/math'; import { ENV } from '@pixi/constants'; import { settings } from '../settings'; +import Framebuffer from './Framebuffer'; /** * System plugin to the renderer to manage framebuffers. @@ -25,6 +26,13 @@ * @readonly */ this.managedFramebuffers = []; + + /** + * Framebuffer value that shows that we don't know what is bound + * @member {Framebuffer} + * @readonly + */ + this.unknownFramebuffer = new Framebuffer(10, 10); } /** @@ -35,7 +43,7 @@ const gl = this.gl = this.renderer.gl; this.CONTEXT_UID = this.renderer.CONTEXT_UID; - this.current = null; + this.current = this.unknownFramebuffer; this.viewport = new Rectangle(); this.hasMRT = true; @@ -72,7 +80,7 @@ * Bind a framebuffer * * @param {PIXI.Framebuffer} framebuffer - * @param {PIXI.Rectangle} frame + * @param {PIXI.Rectangle} [frame] frame, default is framebuffer size */ bind(framebuffer, frame) { @@ -407,4 +415,15 @@ this.disposeFramebuffer(list[i], contextLost); } } + + /** + * resets framebuffer stored state, binds screen framebuffer + * + * should be called before renderTexture reset() + */ + reset() + { + this.current = this.unknownFramebuffer; + this.viewport = new Rectangle(); + } } diff --git a/packages/core/src/renderTexture/RenderTextureSystem.js b/packages/core/src/renderTexture/RenderTextureSystem.js index b38476e..7902204 100644 --- a/packages/core/src/renderTexture/RenderTextureSystem.js +++ b/packages/core/src/renderTexture/RenderTextureSystem.js @@ -6,6 +6,8 @@ /** * System plugin to the renderer to manage render textures. * + * Should be added after FramebufferSystem + * * @class * @extends PIXI.System * @memberof PIXI.systems @@ -155,4 +157,12 @@ // resize the root only! this.bind(null); } + + /** + * Resets renderTexture state + */ + reset() + { + this.bind(null); + } } diff --git a/packages/core/src/framebuffer/FramebufferSystem.js b/packages/core/src/framebuffer/FramebufferSystem.js index 561dfdb..e4bdf16 100644 --- a/packages/core/src/framebuffer/FramebufferSystem.js +++ b/packages/core/src/framebuffer/FramebufferSystem.js @@ -2,6 +2,7 @@ import { Rectangle } from '@pixi/math'; import { ENV } from '@pixi/constants'; import { settings } from '../settings'; +import Framebuffer from './Framebuffer'; /** * System plugin to the renderer to manage framebuffers. @@ -25,6 +26,13 @@ * @readonly */ this.managedFramebuffers = []; + + /** + * Framebuffer value that shows that we don't know what is bound + * @member {Framebuffer} + * @readonly + */ + this.unknownFramebuffer = new Framebuffer(10, 10); } /** @@ -35,7 +43,7 @@ const gl = this.gl = this.renderer.gl; this.CONTEXT_UID = this.renderer.CONTEXT_UID; - this.current = null; + this.current = this.unknownFramebuffer; this.viewport = new Rectangle(); this.hasMRT = true; @@ -72,7 +80,7 @@ * Bind a framebuffer * * @param {PIXI.Framebuffer} framebuffer - * @param {PIXI.Rectangle} frame + * @param {PIXI.Rectangle} [frame] frame, default is framebuffer size */ bind(framebuffer, frame) { @@ -407,4 +415,15 @@ this.disposeFramebuffer(list[i], contextLost); } } + + /** + * resets framebuffer stored state, binds screen framebuffer + * + * should be called before renderTexture reset() + */ + reset() + { + this.current = this.unknownFramebuffer; + this.viewport = new Rectangle(); + } } diff --git a/packages/core/src/renderTexture/RenderTextureSystem.js b/packages/core/src/renderTexture/RenderTextureSystem.js index b38476e..7902204 100644 --- a/packages/core/src/renderTexture/RenderTextureSystem.js +++ b/packages/core/src/renderTexture/RenderTextureSystem.js @@ -6,6 +6,8 @@ /** * System plugin to the renderer to manage render textures. * + * Should be added after FramebufferSystem + * * @class * @extends PIXI.System * @memberof PIXI.systems @@ -155,4 +157,12 @@ // resize the root only! this.bind(null); } + + /** + * Resets renderTexture state + */ + reset() + { + this.bind(null); + } } diff --git a/packages/core/src/shader/ShaderSystem.js b/packages/core/src/shader/ShaderSystem.js index b9e7b92..e95450f 100644 --- a/packages/core/src/shader/ShaderSystem.js +++ b/packages/core/src/shader/ShaderSystem.js @@ -202,6 +202,15 @@ } /** + * Resets ShaderSystem state, does not affect WebGL state + */ + reset() + { + this.program = null; + this.shader = null; + } + + /** * Destroys this System and removes all its textures */ destroy() diff --git a/packages/core/src/framebuffer/FramebufferSystem.js b/packages/core/src/framebuffer/FramebufferSystem.js index 561dfdb..e4bdf16 100644 --- a/packages/core/src/framebuffer/FramebufferSystem.js +++ b/packages/core/src/framebuffer/FramebufferSystem.js @@ -2,6 +2,7 @@ import { Rectangle } from '@pixi/math'; import { ENV } from '@pixi/constants'; import { settings } from '../settings'; +import Framebuffer from './Framebuffer'; /** * System plugin to the renderer to manage framebuffers. @@ -25,6 +26,13 @@ * @readonly */ this.managedFramebuffers = []; + + /** + * Framebuffer value that shows that we don't know what is bound + * @member {Framebuffer} + * @readonly + */ + this.unknownFramebuffer = new Framebuffer(10, 10); } /** @@ -35,7 +43,7 @@ const gl = this.gl = this.renderer.gl; this.CONTEXT_UID = this.renderer.CONTEXT_UID; - this.current = null; + this.current = this.unknownFramebuffer; this.viewport = new Rectangle(); this.hasMRT = true; @@ -72,7 +80,7 @@ * Bind a framebuffer * * @param {PIXI.Framebuffer} framebuffer - * @param {PIXI.Rectangle} frame + * @param {PIXI.Rectangle} [frame] frame, default is framebuffer size */ bind(framebuffer, frame) { @@ -407,4 +415,15 @@ this.disposeFramebuffer(list[i], contextLost); } } + + /** + * resets framebuffer stored state, binds screen framebuffer + * + * should be called before renderTexture reset() + */ + reset() + { + this.current = this.unknownFramebuffer; + this.viewport = new Rectangle(); + } } diff --git a/packages/core/src/renderTexture/RenderTextureSystem.js b/packages/core/src/renderTexture/RenderTextureSystem.js index b38476e..7902204 100644 --- a/packages/core/src/renderTexture/RenderTextureSystem.js +++ b/packages/core/src/renderTexture/RenderTextureSystem.js @@ -6,6 +6,8 @@ /** * System plugin to the renderer to manage render textures. * + * Should be added after FramebufferSystem + * * @class * @extends PIXI.System * @memberof PIXI.systems @@ -155,4 +157,12 @@ // resize the root only! this.bind(null); } + + /** + * Resets renderTexture state + */ + reset() + { + this.bind(null); + } } diff --git a/packages/core/src/shader/ShaderSystem.js b/packages/core/src/shader/ShaderSystem.js index b9e7b92..e95450f 100644 --- a/packages/core/src/shader/ShaderSystem.js +++ b/packages/core/src/shader/ShaderSystem.js @@ -202,6 +202,15 @@ } /** + * Resets ShaderSystem state, does not affect WebGL state + */ + reset() + { + this.program = null; + this.shader = null; + } + + /** * Destroys this System and removes all its textures */ destroy() diff --git a/packages/core/src/state/StateSystem.js b/packages/core/src/state/StateSystem.js index 90c4ed6..c7e35d2 100755 --- a/packages/core/src/state/StateSystem.js +++ b/packages/core/src/state/StateSystem.js @@ -33,29 +33,6 @@ this.gl = null; /** - * Return from MAX_VERTEX_ATTRIBS - * @member {number} - * @readonly - */ - this.maxAttribs = null; - - /** - * Check we have vao - * @member {OES_vertex_array_object} - * @readonly - */ - this.nativeVaoExtension = null; - - /** - * Attribute state - * @member {object} - * @readonly - * @property {number[]} tempAttribState - * @property {number[]} attribState - */ - this.attribState = null; - - /** * State ID * @member {number} * @readonly @@ -119,20 +96,6 @@ { this.gl = gl; - this.maxAttribs = gl.getParameter(gl.MAX_VERTEX_ATTRIBS); - - // check we have vao.. - this.nativeVaoExtension = ( - gl.getExtension('OES_vertex_array_object') - || gl.getExtension('MOZ_OES_vertex_array_object') - || gl.getExtension('WEBKIT_OES_vertex_array_object') - ); - - this.attribState = { - tempAttribState: new Array(this.maxAttribs), - attribState: new Array(this.maxAttribs), - }; - this.blendModes = mapWebGLBlendModesToPixi(gl); this.setState(this.defaultState); @@ -181,6 +144,26 @@ } /** + * Sets the state, when previous state is unknown + * + * @param {*} state - The state to set + */ + forceState(state) + { + state = state || this.defaultState; + for (let i = 0; i < this.map.length; i++) + { + this.map[i].call(this, !!(state.data & (1 << i))); + } + for (let i = 0; i < this.checks.length; i++) + { + this.checks[i](this, state); + } + + this.stateId = state.data; + } + + /** * Enables or disabled blending. * * @param {boolean} value - Turn on or off webgl blending. @@ -280,52 +263,19 @@ this.gl.polygonOffset(value, scale); } - /** - * Disables all the vaos in use - * - */ - resetAttributes() - { - for (let i = 0; i < this.attribState.tempAttribState.length; i++) - { - this.attribState.tempAttribState[i] = 0; - } - - for (let i = 0; i < this.attribState.attribState.length; i++) - { - this.attribState.attribState[i] = 0; - } - - // im going to assume one is always active for performance reasons. - for (let i = 1; i < this.maxAttribs; i++) - { - this.gl.disableVertexAttribArray(i); - } - } - // used /** * Resets all the logic and disables the vaos */ reset() { - // unbind any VAO if they exist.. - if (this.nativeVaoExtension) - { - this.nativeVaoExtension.bindVertexArrayOES(null); - } - - // reset all attributes.. - this.resetAttributes(); - this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, false); + this.forceState(0); + this._blendEq = true; - + this.blendMode = -1; this.setBlendMode(0); - - // TODO? - // this.setState(this.defaultState); } /** diff --git a/packages/core/src/framebuffer/FramebufferSystem.js b/packages/core/src/framebuffer/FramebufferSystem.js index 561dfdb..e4bdf16 100644 --- a/packages/core/src/framebuffer/FramebufferSystem.js +++ b/packages/core/src/framebuffer/FramebufferSystem.js @@ -2,6 +2,7 @@ import { Rectangle } from '@pixi/math'; import { ENV } from '@pixi/constants'; import { settings } from '../settings'; +import Framebuffer from './Framebuffer'; /** * System plugin to the renderer to manage framebuffers. @@ -25,6 +26,13 @@ * @readonly */ this.managedFramebuffers = []; + + /** + * Framebuffer value that shows that we don't know what is bound + * @member {Framebuffer} + * @readonly + */ + this.unknownFramebuffer = new Framebuffer(10, 10); } /** @@ -35,7 +43,7 @@ const gl = this.gl = this.renderer.gl; this.CONTEXT_UID = this.renderer.CONTEXT_UID; - this.current = null; + this.current = this.unknownFramebuffer; this.viewport = new Rectangle(); this.hasMRT = true; @@ -72,7 +80,7 @@ * Bind a framebuffer * * @param {PIXI.Framebuffer} framebuffer - * @param {PIXI.Rectangle} frame + * @param {PIXI.Rectangle} [frame] frame, default is framebuffer size */ bind(framebuffer, frame) { @@ -407,4 +415,15 @@ this.disposeFramebuffer(list[i], contextLost); } } + + /** + * resets framebuffer stored state, binds screen framebuffer + * + * should be called before renderTexture reset() + */ + reset() + { + this.current = this.unknownFramebuffer; + this.viewport = new Rectangle(); + } } diff --git a/packages/core/src/renderTexture/RenderTextureSystem.js b/packages/core/src/renderTexture/RenderTextureSystem.js index b38476e..7902204 100644 --- a/packages/core/src/renderTexture/RenderTextureSystem.js +++ b/packages/core/src/renderTexture/RenderTextureSystem.js @@ -6,6 +6,8 @@ /** * System plugin to the renderer to manage render textures. * + * Should be added after FramebufferSystem + * * @class * @extends PIXI.System * @memberof PIXI.systems @@ -155,4 +157,12 @@ // resize the root only! this.bind(null); } + + /** + * Resets renderTexture state + */ + reset() + { + this.bind(null); + } } diff --git a/packages/core/src/shader/ShaderSystem.js b/packages/core/src/shader/ShaderSystem.js index b9e7b92..e95450f 100644 --- a/packages/core/src/shader/ShaderSystem.js +++ b/packages/core/src/shader/ShaderSystem.js @@ -202,6 +202,15 @@ } /** + * Resets ShaderSystem state, does not affect WebGL state + */ + reset() + { + this.program = null; + this.shader = null; + } + + /** * Destroys this System and removes all its textures */ destroy() diff --git a/packages/core/src/state/StateSystem.js b/packages/core/src/state/StateSystem.js index 90c4ed6..c7e35d2 100755 --- a/packages/core/src/state/StateSystem.js +++ b/packages/core/src/state/StateSystem.js @@ -33,29 +33,6 @@ this.gl = null; /** - * Return from MAX_VERTEX_ATTRIBS - * @member {number} - * @readonly - */ - this.maxAttribs = null; - - /** - * Check we have vao - * @member {OES_vertex_array_object} - * @readonly - */ - this.nativeVaoExtension = null; - - /** - * Attribute state - * @member {object} - * @readonly - * @property {number[]} tempAttribState - * @property {number[]} attribState - */ - this.attribState = null; - - /** * State ID * @member {number} * @readonly @@ -119,20 +96,6 @@ { this.gl = gl; - this.maxAttribs = gl.getParameter(gl.MAX_VERTEX_ATTRIBS); - - // check we have vao.. - this.nativeVaoExtension = ( - gl.getExtension('OES_vertex_array_object') - || gl.getExtension('MOZ_OES_vertex_array_object') - || gl.getExtension('WEBKIT_OES_vertex_array_object') - ); - - this.attribState = { - tempAttribState: new Array(this.maxAttribs), - attribState: new Array(this.maxAttribs), - }; - this.blendModes = mapWebGLBlendModesToPixi(gl); this.setState(this.defaultState); @@ -181,6 +144,26 @@ } /** + * Sets the state, when previous state is unknown + * + * @param {*} state - The state to set + */ + forceState(state) + { + state = state || this.defaultState; + for (let i = 0; i < this.map.length; i++) + { + this.map[i].call(this, !!(state.data & (1 << i))); + } + for (let i = 0; i < this.checks.length; i++) + { + this.checks[i](this, state); + } + + this.stateId = state.data; + } + + /** * Enables or disabled blending. * * @param {boolean} value - Turn on or off webgl blending. @@ -280,52 +263,19 @@ this.gl.polygonOffset(value, scale); } - /** - * Disables all the vaos in use - * - */ - resetAttributes() - { - for (let i = 0; i < this.attribState.tempAttribState.length; i++) - { - this.attribState.tempAttribState[i] = 0; - } - - for (let i = 0; i < this.attribState.attribState.length; i++) - { - this.attribState.attribState[i] = 0; - } - - // im going to assume one is always active for performance reasons. - for (let i = 1; i < this.maxAttribs; i++) - { - this.gl.disableVertexAttribArray(i); - } - } - // used /** * Resets all the logic and disables the vaos */ reset() { - // unbind any VAO if they exist.. - if (this.nativeVaoExtension) - { - this.nativeVaoExtension.bindVertexArrayOES(null); - } - - // reset all attributes.. - this.resetAttributes(); - this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, false); + this.forceState(0); + this._blendEq = true; - + this.blendMode = -1; this.setBlendMode(0); - - // TODO? - // this.setState(this.defaultState); } /** diff --git a/packages/core/src/textures/TextureSystem.js b/packages/core/src/textures/TextureSystem.js index 1feb756..109a76c 100644 --- a/packages/core/src/textures/TextureSystem.js +++ b/packages/core/src/textures/TextureSystem.js @@ -1,4 +1,5 @@ import System from '../System'; +import BaseTexture from './BaseTexture'; import GLTexture from './GLTexture'; import { removeItems } from '@pixi/utils'; import { WRAP_MODES } from '@pixi/constants'; @@ -39,6 +40,20 @@ * @readonly */ this.managedTextures = []; + + /** + * Did someone temper with textures state? We'll overwrite them when we need to unbind something. + * @member {boolean} + * @private + */ + this._unknownBoundTextures = false; + + /** + * BaseTexture value that shows that we don't know what is bound + * @member {PIXI.BaseTexture} + * @readonly + */ + this.unknownTexture = new BaseTexture(); } /** @@ -91,6 +106,8 @@ /** * Bind a texture to a specific location * + * If you want to unbind something, please use `unbind(texture)` instead of `bind(null, textureLocation)` + * * @param {PIXI.Texture|PIXI.BaseTexture} texture - Texture to bind * @param {number} [location=0] - Location to bind at */ @@ -141,16 +158,46 @@ } /** + * Resets texture location and bound textures + * + * Actual `bind(null, i)` calls will be performed at next `unbind()` call + */ + reset() + { + this._unknownBoundTextures = true; + this.currentLocation = -1; + + for (let i = 0; i < this.boundTextures.length; i++) + { + this.boundTextures[i] = this.unknownTexture; + } + } + + /** * Unbind a texture * @param {PIXI.Texture|PIXI.BaseTexture} texture - Texture to bind */ unbind(texture) { - const { gl } = this; + const { gl, boundTextures } = this; - for (let i = 0; i < this.boundTextures.length; i++) + if (this._unknownBoundTextures) { - if (this.boundTextures[i] === texture) + this._unknownBoundTextures = false; + // someone changed webGL state, + // we have to be sure that our texture does not appear in multi-texture renderer samplers + for (let i = 0; i < boundTextures.length; i++) + { + if (boundTextures[i] === this.unknownTexture) + { + this.bind(null, i); + } + } + } + + for (let i = 0; i < boundTextures.length; i++) + { + if (boundTextures[i] === texture) { if (this.currentLocation !== i) { @@ -159,7 +206,7 @@ } gl.bindTexture(gl.TEXTURE_2D, this.emptyTextures[texture.target].texture); - this.boundTextures[i] = null; + boundTextures[i] = null; } } }