diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index b4b02a0..1e8115b 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -177,6 +177,7 @@ */ AccessibilityManager.prototype.update = function() { + if(!this.renderer.renderingToScreen)return; // update children... this.updateAccessibleObjects(this.renderer._lastObjectRendered); diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index b4b02a0..1e8115b 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -177,6 +177,7 @@ */ AccessibilityManager.prototype.update = function() { + if(!this.renderer.renderingToScreen)return; // update children... this.updateAccessibleObjects(this.renderer._lastObjectRendered); diff --git a/src/core/graphics/webgl/GraphicsRenderer.js b/src/core/graphics/webgl/GraphicsRenderer.js index 282ba87..8253f1a 100644 --- a/src/core/graphics/webgl/GraphicsRenderer.js +++ b/src/core/graphics/webgl/GraphicsRenderer.js @@ -34,6 +34,9 @@ this.primitiveShader = null; this.gl = renderer.gl; + + // easy access! + this.CONTEXT_UID = 0; } GraphicsRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -51,6 +54,7 @@ GraphicsRenderer.prototype.onContextChange = function() { this.gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; this.primitiveShader = new PrimitiveShader(this.gl) }; @@ -81,12 +85,12 @@ var webGLData; - if (graphics.dirty || !graphics._webGL[gl.id]) + if (graphics.dirty || !graphics._webGL[this.CONTEXT_UID]) { this.updateGraphics(graphics); } - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // This could be speeded up for sure! var shader = this.primitiveShader; @@ -120,12 +124,12 @@ var gl = this.renderer.gl; // get the contexts graphics object - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // if the graphics object does not exist in the webGL context time to create it! if (!webGL) { - webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + webGL = graphics._webGL[this.CONTEXT_UID] = {lastIndex:0, data:[], gl:gl}; } diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index b4b02a0..1e8115b 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -177,6 +177,7 @@ */ AccessibilityManager.prototype.update = function() { + if(!this.renderer.renderingToScreen)return; // update children... this.updateAccessibleObjects(this.renderer._lastObjectRendered); diff --git a/src/core/graphics/webgl/GraphicsRenderer.js b/src/core/graphics/webgl/GraphicsRenderer.js index 282ba87..8253f1a 100644 --- a/src/core/graphics/webgl/GraphicsRenderer.js +++ b/src/core/graphics/webgl/GraphicsRenderer.js @@ -34,6 +34,9 @@ this.primitiveShader = null; this.gl = renderer.gl; + + // easy access! + this.CONTEXT_UID = 0; } GraphicsRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -51,6 +54,7 @@ GraphicsRenderer.prototype.onContextChange = function() { this.gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; this.primitiveShader = new PrimitiveShader(this.gl) }; @@ -81,12 +85,12 @@ var webGLData; - if (graphics.dirty || !graphics._webGL[gl.id]) + if (graphics.dirty || !graphics._webGL[this.CONTEXT_UID]) { this.updateGraphics(graphics); } - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // This could be speeded up for sure! var shader = this.primitiveShader; @@ -120,12 +124,12 @@ var gl = this.renderer.gl; // get the contexts graphics object - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // if the graphics object does not exist in the webGL context time to create it! if (!webGL) { - webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + webGL = graphics._webGL[this.CONTEXT_UID] = {lastIndex:0, data:[], gl:gl}; } diff --git a/src/core/particles/webgl/ParticleRenderer.js b/src/core/particles/webgl/ParticleRenderer.js index 0b0abd0..6e7b9c0 100644 --- a/src/core/particles/webgl/ParticleRenderer.js +++ b/src/core/particles/webgl/ParticleRenderer.js @@ -61,6 +61,8 @@ this.properties = null; this.tempMatrix = new math.Matrix(); + + this.CONTEXT_UID = 0; } ParticleRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -79,6 +81,8 @@ return; var gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; + // setup default shader this.shader = new ParticleShader(this.renderer.shaderManager); @@ -190,7 +194,7 @@ // make sure the texture is bound.. var baseTexture = children[0]._texture.baseTexture; - if (!baseTexture._glTextures[gl.id]) + if (!baseTexture._glTextures[this.CONTEXT_UID]) { // if the texture has not updated then lets not upload any static properties if(!this.renderer.updateTexture(baseTexture)) @@ -205,7 +209,7 @@ } else { - gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[gl.id]); + gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[this.CONTEXT_UID]); } // now lets upload and render the buffers.. diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index b4b02a0..1e8115b 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -177,6 +177,7 @@ */ AccessibilityManager.prototype.update = function() { + if(!this.renderer.renderingToScreen)return; // update children... this.updateAccessibleObjects(this.renderer._lastObjectRendered); diff --git a/src/core/graphics/webgl/GraphicsRenderer.js b/src/core/graphics/webgl/GraphicsRenderer.js index 282ba87..8253f1a 100644 --- a/src/core/graphics/webgl/GraphicsRenderer.js +++ b/src/core/graphics/webgl/GraphicsRenderer.js @@ -34,6 +34,9 @@ this.primitiveShader = null; this.gl = renderer.gl; + + // easy access! + this.CONTEXT_UID = 0; } GraphicsRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -51,6 +54,7 @@ GraphicsRenderer.prototype.onContextChange = function() { this.gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; this.primitiveShader = new PrimitiveShader(this.gl) }; @@ -81,12 +85,12 @@ var webGLData; - if (graphics.dirty || !graphics._webGL[gl.id]) + if (graphics.dirty || !graphics._webGL[this.CONTEXT_UID]) { this.updateGraphics(graphics); } - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // This could be speeded up for sure! var shader = this.primitiveShader; @@ -120,12 +124,12 @@ var gl = this.renderer.gl; // get the contexts graphics object - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // if the graphics object does not exist in the webGL context time to create it! if (!webGL) { - webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + webGL = graphics._webGL[this.CONTEXT_UID] = {lastIndex:0, data:[], gl:gl}; } diff --git a/src/core/particles/webgl/ParticleRenderer.js b/src/core/particles/webgl/ParticleRenderer.js index 0b0abd0..6e7b9c0 100644 --- a/src/core/particles/webgl/ParticleRenderer.js +++ b/src/core/particles/webgl/ParticleRenderer.js @@ -61,6 +61,8 @@ this.properties = null; this.tempMatrix = new math.Matrix(); + + this.CONTEXT_UID = 0; } ParticleRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -79,6 +81,8 @@ return; var gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; + // setup default shader this.shader = new ParticleShader(this.renderer.shaderManager); @@ -190,7 +194,7 @@ // make sure the texture is bound.. var baseTexture = children[0]._texture.baseTexture; - if (!baseTexture._glTextures[gl.id]) + if (!baseTexture._glTextures[this.CONTEXT_UID]) { // if the texture has not updated then lets not upload any static properties if(!this.renderer.updateTexture(baseTexture)) @@ -205,7 +209,7 @@ } else { - gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[gl.id]); + gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[this.CONTEXT_UID]); } // now lets upload and render the buffers.. diff --git a/src/core/renderers/webgl/RenderTextureManager.js b/src/core/renderers/webgl/RenderTextureManager.js index cbbbd4c..e922209 100644 --- a/src/core/renderers/webgl/RenderTextureManager.js +++ b/src/core/renderers/webgl/RenderTextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var RenderTextureManager = function(gl) +var RenderTextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -27,16 +28,15 @@ { texture = texture.baseTexture || texture; - var gl = this.gl; - var renderTarget = texture._glRenderTargets[gl.id]; + var renderTarget = texture._glRenderTargets[this.renderer.CONTEXT_UID]; if (!renderTarget) { - renderTarget = new RenderTarget(gl, texture.width, texture.height); + renderTarget = new RenderTarget(this.gl, texture.width, texture.height, texture.scaleMode, texture.resolution); - texture._glTextures[gl.id] = renderTarget.texture; - texture._glRenderTargets[gl.id] = renderTarget; + texture._glTextures[this.renderer.CONTEXT_UID] = renderTarget.texture; + texture._glRenderTargets[this.renderer.CONTEXT_UID] = renderTarget; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -60,17 +60,17 @@ texture = texture.baseTexture || texture; var gl = this.gl; - if (texture._glRenderTargets[gl.id]) + if (texture._glRenderTargets[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id] = null; - texture._glRenderTargets[gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID] = null; + texture._glRenderTargets[this.renderer.CONTEXT_UID].destroy(); //.destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -88,9 +88,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index b4b02a0..1e8115b 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -177,6 +177,7 @@ */ AccessibilityManager.prototype.update = function() { + if(!this.renderer.renderingToScreen)return; // update children... this.updateAccessibleObjects(this.renderer._lastObjectRendered); diff --git a/src/core/graphics/webgl/GraphicsRenderer.js b/src/core/graphics/webgl/GraphicsRenderer.js index 282ba87..8253f1a 100644 --- a/src/core/graphics/webgl/GraphicsRenderer.js +++ b/src/core/graphics/webgl/GraphicsRenderer.js @@ -34,6 +34,9 @@ this.primitiveShader = null; this.gl = renderer.gl; + + // easy access! + this.CONTEXT_UID = 0; } GraphicsRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -51,6 +54,7 @@ GraphicsRenderer.prototype.onContextChange = function() { this.gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; this.primitiveShader = new PrimitiveShader(this.gl) }; @@ -81,12 +85,12 @@ var webGLData; - if (graphics.dirty || !graphics._webGL[gl.id]) + if (graphics.dirty || !graphics._webGL[this.CONTEXT_UID]) { this.updateGraphics(graphics); } - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // This could be speeded up for sure! var shader = this.primitiveShader; @@ -120,12 +124,12 @@ var gl = this.renderer.gl; // get the contexts graphics object - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // if the graphics object does not exist in the webGL context time to create it! if (!webGL) { - webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + webGL = graphics._webGL[this.CONTEXT_UID] = {lastIndex:0, data:[], gl:gl}; } diff --git a/src/core/particles/webgl/ParticleRenderer.js b/src/core/particles/webgl/ParticleRenderer.js index 0b0abd0..6e7b9c0 100644 --- a/src/core/particles/webgl/ParticleRenderer.js +++ b/src/core/particles/webgl/ParticleRenderer.js @@ -61,6 +61,8 @@ this.properties = null; this.tempMatrix = new math.Matrix(); + + this.CONTEXT_UID = 0; } ParticleRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -79,6 +81,8 @@ return; var gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; + // setup default shader this.shader = new ParticleShader(this.renderer.shaderManager); @@ -190,7 +194,7 @@ // make sure the texture is bound.. var baseTexture = children[0]._texture.baseTexture; - if (!baseTexture._glTextures[gl.id]) + if (!baseTexture._glTextures[this.CONTEXT_UID]) { // if the texture has not updated then lets not upload any static properties if(!this.renderer.updateTexture(baseTexture)) @@ -205,7 +209,7 @@ } else { - gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[gl.id]); + gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[this.CONTEXT_UID]); } // now lets upload and render the buffers.. diff --git a/src/core/renderers/webgl/RenderTextureManager.js b/src/core/renderers/webgl/RenderTextureManager.js index cbbbd4c..e922209 100644 --- a/src/core/renderers/webgl/RenderTextureManager.js +++ b/src/core/renderers/webgl/RenderTextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var RenderTextureManager = function(gl) +var RenderTextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -27,16 +28,15 @@ { texture = texture.baseTexture || texture; - var gl = this.gl; - var renderTarget = texture._glRenderTargets[gl.id]; + var renderTarget = texture._glRenderTargets[this.renderer.CONTEXT_UID]; if (!renderTarget) { - renderTarget = new RenderTarget(gl, texture.width, texture.height); + renderTarget = new RenderTarget(this.gl, texture.width, texture.height, texture.scaleMode, texture.resolution); - texture._glTextures[gl.id] = renderTarget.texture; - texture._glRenderTargets[gl.id] = renderTarget; + texture._glTextures[this.renderer.CONTEXT_UID] = renderTarget.texture; + texture._glRenderTargets[this.renderer.CONTEXT_UID] = renderTarget; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -60,17 +60,17 @@ texture = texture.baseTexture || texture; var gl = this.gl; - if (texture._glRenderTargets[gl.id]) + if (texture._glRenderTargets[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id] = null; - texture._glRenderTargets[gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID] = null; + texture._glRenderTargets[this.renderer.CONTEXT_UID].destroy(); //.destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -88,9 +88,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/TextureManager.js b/src/core/renderers/webgl/TextureManager.js index a0f4492..eb78645 100644 --- a/src/core/renderers/webgl/TextureManager.js +++ b/src/core/renderers/webgl/TextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var TextureManager = function(gl) +var TextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer; + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -45,14 +46,13 @@ } - var gl = this.gl; - var glTexture = texture._glTextures[gl.id];//texture._glTextures[gl.id]; + var glTexture = texture._glTextures[this.renderer.CONTEXT_UID]; if (!glTexture) { - glTexture = new GLTexture(gl); + glTexture = new GLTexture(this.gl); glTexture.premultiplyAlpha = true; - texture._glTextures[gl.id] = glTexture; + texture._glTextures[this.renderer.CONTEXT_UID] = glTexture; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -93,14 +93,14 @@ return; } - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID].destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -118,9 +118,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index b4b02a0..1e8115b 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -177,6 +177,7 @@ */ AccessibilityManager.prototype.update = function() { + if(!this.renderer.renderingToScreen)return; // update children... this.updateAccessibleObjects(this.renderer._lastObjectRendered); diff --git a/src/core/graphics/webgl/GraphicsRenderer.js b/src/core/graphics/webgl/GraphicsRenderer.js index 282ba87..8253f1a 100644 --- a/src/core/graphics/webgl/GraphicsRenderer.js +++ b/src/core/graphics/webgl/GraphicsRenderer.js @@ -34,6 +34,9 @@ this.primitiveShader = null; this.gl = renderer.gl; + + // easy access! + this.CONTEXT_UID = 0; } GraphicsRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -51,6 +54,7 @@ GraphicsRenderer.prototype.onContextChange = function() { this.gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; this.primitiveShader = new PrimitiveShader(this.gl) }; @@ -81,12 +85,12 @@ var webGLData; - if (graphics.dirty || !graphics._webGL[gl.id]) + if (graphics.dirty || !graphics._webGL[this.CONTEXT_UID]) { this.updateGraphics(graphics); } - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // This could be speeded up for sure! var shader = this.primitiveShader; @@ -120,12 +124,12 @@ var gl = this.renderer.gl; // get the contexts graphics object - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // if the graphics object does not exist in the webGL context time to create it! if (!webGL) { - webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + webGL = graphics._webGL[this.CONTEXT_UID] = {lastIndex:0, data:[], gl:gl}; } diff --git a/src/core/particles/webgl/ParticleRenderer.js b/src/core/particles/webgl/ParticleRenderer.js index 0b0abd0..6e7b9c0 100644 --- a/src/core/particles/webgl/ParticleRenderer.js +++ b/src/core/particles/webgl/ParticleRenderer.js @@ -61,6 +61,8 @@ this.properties = null; this.tempMatrix = new math.Matrix(); + + this.CONTEXT_UID = 0; } ParticleRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -79,6 +81,8 @@ return; var gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; + // setup default shader this.shader = new ParticleShader(this.renderer.shaderManager); @@ -190,7 +194,7 @@ // make sure the texture is bound.. var baseTexture = children[0]._texture.baseTexture; - if (!baseTexture._glTextures[gl.id]) + if (!baseTexture._glTextures[this.CONTEXT_UID]) { // if the texture has not updated then lets not upload any static properties if(!this.renderer.updateTexture(baseTexture)) @@ -205,7 +209,7 @@ } else { - gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[gl.id]); + gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[this.CONTEXT_UID]); } // now lets upload and render the buffers.. diff --git a/src/core/renderers/webgl/RenderTextureManager.js b/src/core/renderers/webgl/RenderTextureManager.js index cbbbd4c..e922209 100644 --- a/src/core/renderers/webgl/RenderTextureManager.js +++ b/src/core/renderers/webgl/RenderTextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var RenderTextureManager = function(gl) +var RenderTextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -27,16 +28,15 @@ { texture = texture.baseTexture || texture; - var gl = this.gl; - var renderTarget = texture._glRenderTargets[gl.id]; + var renderTarget = texture._glRenderTargets[this.renderer.CONTEXT_UID]; if (!renderTarget) { - renderTarget = new RenderTarget(gl, texture.width, texture.height); + renderTarget = new RenderTarget(this.gl, texture.width, texture.height, texture.scaleMode, texture.resolution); - texture._glTextures[gl.id] = renderTarget.texture; - texture._glRenderTargets[gl.id] = renderTarget; + texture._glTextures[this.renderer.CONTEXT_UID] = renderTarget.texture; + texture._glRenderTargets[this.renderer.CONTEXT_UID] = renderTarget; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -60,17 +60,17 @@ texture = texture.baseTexture || texture; var gl = this.gl; - if (texture._glRenderTargets[gl.id]) + if (texture._glRenderTargets[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id] = null; - texture._glRenderTargets[gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID] = null; + texture._glRenderTargets[this.renderer.CONTEXT_UID].destroy(); //.destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -88,9 +88,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/TextureManager.js b/src/core/renderers/webgl/TextureManager.js index a0f4492..eb78645 100644 --- a/src/core/renderers/webgl/TextureManager.js +++ b/src/core/renderers/webgl/TextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var TextureManager = function(gl) +var TextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer; + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -45,14 +46,13 @@ } - var gl = this.gl; - var glTexture = texture._glTextures[gl.id];//texture._glTextures[gl.id]; + var glTexture = texture._glTextures[this.renderer.CONTEXT_UID]; if (!glTexture) { - glTexture = new GLTexture(gl); + glTexture = new GLTexture(this.gl); glTexture.premultiplyAlpha = true; - texture._glTextures[gl.id] = glTexture; + texture._glTextures[this.renderer.CONTEXT_UID] = glTexture; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -93,14 +93,14 @@ return; } - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID].destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -118,9 +118,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 6a0efa7..638d56f 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -103,6 +103,7 @@ this.gl.id = this.CONTEXT_UID = CONTEXT_UID++; this.state = new WebGLState(this.gl); + this.renderingToScreen = true; /** * Manages the filters. @@ -148,11 +149,9 @@ { var gl = this.gl; - - // create a texture manager... - this.textureManager = new TextureManager(gl); - this.renderTextureManager = new RenderTextureManager(gl); + this.textureManager = new TextureManager(this); + this.renderTextureManager = new RenderTextureManager(this); this.state.resetToDefault(); @@ -174,6 +173,9 @@ */ WebGLRenderer.prototype.render = function (displayObject, renderTexture, clear, transform, skipUpdateTransform) { + // can be handy to know! + this.renderingToScreen = !renderTexture; + this.emit('prerender'); // no point rendering if our context has been blown up! @@ -271,12 +273,12 @@ { var baseTexture = renderTexture.baseTexture; - if(!baseTexture._glRenderTargets[gl.id]) + if(!baseTexture._glRenderTargets[this.CONTEXT_UID]) { this.renderTextureManager.updateTexture(baseTexture); } - renderTarget = baseTexture._glRenderTargets[gl.id]; + renderTarget = baseTexture._glRenderTargets[this.CONTEXT_UID]; renderTarget.setFrame(renderTexture.frame); } @@ -350,7 +352,7 @@ //TODO - can we cache this texture too? this._activeTexture = texture; - if (!texture._glTextures[gl.id]) + if (!texture._glTextures[this.CONTEXT_UID]) { // this will also bind the texture.. this.textureManager.updateTexture(texture); @@ -358,7 +360,7 @@ else { // bind the current texture - texture._glTextures[gl.id].bind(); + texture._glTextures[this.CONTEXT_UID].bind(); } return this; diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index b4b02a0..1e8115b 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -177,6 +177,7 @@ */ AccessibilityManager.prototype.update = function() { + if(!this.renderer.renderingToScreen)return; // update children... this.updateAccessibleObjects(this.renderer._lastObjectRendered); diff --git a/src/core/graphics/webgl/GraphicsRenderer.js b/src/core/graphics/webgl/GraphicsRenderer.js index 282ba87..8253f1a 100644 --- a/src/core/graphics/webgl/GraphicsRenderer.js +++ b/src/core/graphics/webgl/GraphicsRenderer.js @@ -34,6 +34,9 @@ this.primitiveShader = null; this.gl = renderer.gl; + + // easy access! + this.CONTEXT_UID = 0; } GraphicsRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -51,6 +54,7 @@ GraphicsRenderer.prototype.onContextChange = function() { this.gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; this.primitiveShader = new PrimitiveShader(this.gl) }; @@ -81,12 +85,12 @@ var webGLData; - if (graphics.dirty || !graphics._webGL[gl.id]) + if (graphics.dirty || !graphics._webGL[this.CONTEXT_UID]) { this.updateGraphics(graphics); } - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // This could be speeded up for sure! var shader = this.primitiveShader; @@ -120,12 +124,12 @@ var gl = this.renderer.gl; // get the contexts graphics object - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // if the graphics object does not exist in the webGL context time to create it! if (!webGL) { - webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + webGL = graphics._webGL[this.CONTEXT_UID] = {lastIndex:0, data:[], gl:gl}; } diff --git a/src/core/particles/webgl/ParticleRenderer.js b/src/core/particles/webgl/ParticleRenderer.js index 0b0abd0..6e7b9c0 100644 --- a/src/core/particles/webgl/ParticleRenderer.js +++ b/src/core/particles/webgl/ParticleRenderer.js @@ -61,6 +61,8 @@ this.properties = null; this.tempMatrix = new math.Matrix(); + + this.CONTEXT_UID = 0; } ParticleRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -79,6 +81,8 @@ return; var gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; + // setup default shader this.shader = new ParticleShader(this.renderer.shaderManager); @@ -190,7 +194,7 @@ // make sure the texture is bound.. var baseTexture = children[0]._texture.baseTexture; - if (!baseTexture._glTextures[gl.id]) + if (!baseTexture._glTextures[this.CONTEXT_UID]) { // if the texture has not updated then lets not upload any static properties if(!this.renderer.updateTexture(baseTexture)) @@ -205,7 +209,7 @@ } else { - gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[gl.id]); + gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[this.CONTEXT_UID]); } // now lets upload and render the buffers.. diff --git a/src/core/renderers/webgl/RenderTextureManager.js b/src/core/renderers/webgl/RenderTextureManager.js index cbbbd4c..e922209 100644 --- a/src/core/renderers/webgl/RenderTextureManager.js +++ b/src/core/renderers/webgl/RenderTextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var RenderTextureManager = function(gl) +var RenderTextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -27,16 +28,15 @@ { texture = texture.baseTexture || texture; - var gl = this.gl; - var renderTarget = texture._glRenderTargets[gl.id]; + var renderTarget = texture._glRenderTargets[this.renderer.CONTEXT_UID]; if (!renderTarget) { - renderTarget = new RenderTarget(gl, texture.width, texture.height); + renderTarget = new RenderTarget(this.gl, texture.width, texture.height, texture.scaleMode, texture.resolution); - texture._glTextures[gl.id] = renderTarget.texture; - texture._glRenderTargets[gl.id] = renderTarget; + texture._glTextures[this.renderer.CONTEXT_UID] = renderTarget.texture; + texture._glRenderTargets[this.renderer.CONTEXT_UID] = renderTarget; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -60,17 +60,17 @@ texture = texture.baseTexture || texture; var gl = this.gl; - if (texture._glRenderTargets[gl.id]) + if (texture._glRenderTargets[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id] = null; - texture._glRenderTargets[gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID] = null; + texture._glRenderTargets[this.renderer.CONTEXT_UID].destroy(); //.destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -88,9 +88,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/TextureManager.js b/src/core/renderers/webgl/TextureManager.js index a0f4492..eb78645 100644 --- a/src/core/renderers/webgl/TextureManager.js +++ b/src/core/renderers/webgl/TextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var TextureManager = function(gl) +var TextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer; + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -45,14 +46,13 @@ } - var gl = this.gl; - var glTexture = texture._glTextures[gl.id];//texture._glTextures[gl.id]; + var glTexture = texture._glTextures[this.renderer.CONTEXT_UID]; if (!glTexture) { - glTexture = new GLTexture(gl); + glTexture = new GLTexture(this.gl); glTexture.premultiplyAlpha = true; - texture._glTextures[gl.id] = glTexture; + texture._glTextures[this.renderer.CONTEXT_UID] = glTexture; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -93,14 +93,14 @@ return; } - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID].destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -118,9 +118,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 6a0efa7..638d56f 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -103,6 +103,7 @@ this.gl.id = this.CONTEXT_UID = CONTEXT_UID++; this.state = new WebGLState(this.gl); + this.renderingToScreen = true; /** * Manages the filters. @@ -148,11 +149,9 @@ { var gl = this.gl; - - // create a texture manager... - this.textureManager = new TextureManager(gl); - this.renderTextureManager = new RenderTextureManager(gl); + this.textureManager = new TextureManager(this); + this.renderTextureManager = new RenderTextureManager(this); this.state.resetToDefault(); @@ -174,6 +173,9 @@ */ WebGLRenderer.prototype.render = function (displayObject, renderTexture, clear, transform, skipUpdateTransform) { + // can be handy to know! + this.renderingToScreen = !renderTexture; + this.emit('prerender'); // no point rendering if our context has been blown up! @@ -271,12 +273,12 @@ { var baseTexture = renderTexture.baseTexture; - if(!baseTexture._glRenderTargets[gl.id]) + if(!baseTexture._glRenderTargets[this.CONTEXT_UID]) { this.renderTextureManager.updateTexture(baseTexture); } - renderTarget = baseTexture._glRenderTargets[gl.id]; + renderTarget = baseTexture._glRenderTargets[this.CONTEXT_UID]; renderTarget.setFrame(renderTexture.frame); } @@ -350,7 +352,7 @@ //TODO - can we cache this texture too? this._activeTexture = texture; - if (!texture._glTextures[gl.id]) + if (!texture._glTextures[this.CONTEXT_UID]) { // this will also bind the texture.. this.textureManager.updateTexture(texture); @@ -358,7 +360,7 @@ else { // bind the current texture - texture._glTextures[gl.id].bind(); + texture._glTextures[this.CONTEXT_UID].bind(); } return this; diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.frag b/src/core/renderers/webgl/filters/FXAA/FXAA.frag deleted file mode 100755 index 2f3b3ae..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.frag +++ /dev/null @@ -1,127 +0,0 @@ -precision lowp float; - - -/** -Basic FXAA implementation based on the code on geeks3d.com with the -modification that the texture2DLod stuff was removed since it's -unsupported by WebGL. - --- - -From: -https://github.com/mitsuhiko/webgl-meincraft - -Copyright (c) 2011 by Armin Ronacher. - -Some rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following - disclaimer in the documentation and/or other materials provided - with the distribution. - - * The names of the contributors may not be used to endorse or - promote products derived from this software without specific - prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef FXAA_REDUCE_MIN - #define FXAA_REDUCE_MIN (1.0/ 128.0) -#endif -#ifndef FXAA_REDUCE_MUL - #define FXAA_REDUCE_MUL (1.0 / 8.0) -#endif -#ifndef FXAA_SPAN_MAX - #define FXAA_SPAN_MAX 8.0 -#endif - -//optimized version for mobile, where dependent -//texture reads can be a bottleneck -vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution, - vec2 v_rgbNW, vec2 v_rgbNE, - vec2 v_rgbSW, vec2 v_rgbSE, - vec2 v_rgbM) { - vec4 color; - mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y); - vec3 rgbNW = texture2D(tex, v_rgbNW).xyz; - vec3 rgbNE = texture2D(tex, v_rgbNE).xyz; - vec3 rgbSW = texture2D(tex, v_rgbSW).xyz; - vec3 rgbSE = texture2D(tex, v_rgbSE).xyz; - vec4 texColor = texture2D(tex, v_rgbM); - vec3 rgbM = texColor.xyz; - vec3 luma = vec3(0.299, 0.587, 0.114); - float lumaNW = dot(rgbNW, luma); - float lumaNE = dot(rgbNE, luma); - float lumaSW = dot(rgbSW, luma); - float lumaSE = dot(rgbSE, luma); - float lumaM = dot(rgbM, luma); - float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); - float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); - - mediump vec2 dir; - dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); - dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); - - float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * - (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); - - float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); - dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), - max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), - dir * rcpDirMin)) * inverseVP; - - vec3 rgbA = 0.5 * ( - texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz + - texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz); - vec3 rgbB = rgbA * 0.5 + 0.25 * ( - texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz + - texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz); - - float lumaB = dot(rgbB, luma); - if ((lumaB < lumaMin) || (lumaB > lumaMax)) - color = vec4(rgbA, texColor.a); - else - color = vec4(rgbB, texColor.a); - return color; -} - - -varying vec2 vTextureCoord; -varying vec4 vColor; -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - -uniform sampler2D uSampler; - - -void main(void){ - - gl_FragColor = fxaa(uSampler, vTextureCoord * vResolution, vResolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); - -} diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index b4b02a0..1e8115b 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -177,6 +177,7 @@ */ AccessibilityManager.prototype.update = function() { + if(!this.renderer.renderingToScreen)return; // update children... this.updateAccessibleObjects(this.renderer._lastObjectRendered); diff --git a/src/core/graphics/webgl/GraphicsRenderer.js b/src/core/graphics/webgl/GraphicsRenderer.js index 282ba87..8253f1a 100644 --- a/src/core/graphics/webgl/GraphicsRenderer.js +++ b/src/core/graphics/webgl/GraphicsRenderer.js @@ -34,6 +34,9 @@ this.primitiveShader = null; this.gl = renderer.gl; + + // easy access! + this.CONTEXT_UID = 0; } GraphicsRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -51,6 +54,7 @@ GraphicsRenderer.prototype.onContextChange = function() { this.gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; this.primitiveShader = new PrimitiveShader(this.gl) }; @@ -81,12 +85,12 @@ var webGLData; - if (graphics.dirty || !graphics._webGL[gl.id]) + if (graphics.dirty || !graphics._webGL[this.CONTEXT_UID]) { this.updateGraphics(graphics); } - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // This could be speeded up for sure! var shader = this.primitiveShader; @@ -120,12 +124,12 @@ var gl = this.renderer.gl; // get the contexts graphics object - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // if the graphics object does not exist in the webGL context time to create it! if (!webGL) { - webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + webGL = graphics._webGL[this.CONTEXT_UID] = {lastIndex:0, data:[], gl:gl}; } diff --git a/src/core/particles/webgl/ParticleRenderer.js b/src/core/particles/webgl/ParticleRenderer.js index 0b0abd0..6e7b9c0 100644 --- a/src/core/particles/webgl/ParticleRenderer.js +++ b/src/core/particles/webgl/ParticleRenderer.js @@ -61,6 +61,8 @@ this.properties = null; this.tempMatrix = new math.Matrix(); + + this.CONTEXT_UID = 0; } ParticleRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -79,6 +81,8 @@ return; var gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; + // setup default shader this.shader = new ParticleShader(this.renderer.shaderManager); @@ -190,7 +194,7 @@ // make sure the texture is bound.. var baseTexture = children[0]._texture.baseTexture; - if (!baseTexture._glTextures[gl.id]) + if (!baseTexture._glTextures[this.CONTEXT_UID]) { // if the texture has not updated then lets not upload any static properties if(!this.renderer.updateTexture(baseTexture)) @@ -205,7 +209,7 @@ } else { - gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[gl.id]); + gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[this.CONTEXT_UID]); } // now lets upload and render the buffers.. diff --git a/src/core/renderers/webgl/RenderTextureManager.js b/src/core/renderers/webgl/RenderTextureManager.js index cbbbd4c..e922209 100644 --- a/src/core/renderers/webgl/RenderTextureManager.js +++ b/src/core/renderers/webgl/RenderTextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var RenderTextureManager = function(gl) +var RenderTextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -27,16 +28,15 @@ { texture = texture.baseTexture || texture; - var gl = this.gl; - var renderTarget = texture._glRenderTargets[gl.id]; + var renderTarget = texture._glRenderTargets[this.renderer.CONTEXT_UID]; if (!renderTarget) { - renderTarget = new RenderTarget(gl, texture.width, texture.height); + renderTarget = new RenderTarget(this.gl, texture.width, texture.height, texture.scaleMode, texture.resolution); - texture._glTextures[gl.id] = renderTarget.texture; - texture._glRenderTargets[gl.id] = renderTarget; + texture._glTextures[this.renderer.CONTEXT_UID] = renderTarget.texture; + texture._glRenderTargets[this.renderer.CONTEXT_UID] = renderTarget; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -60,17 +60,17 @@ texture = texture.baseTexture || texture; var gl = this.gl; - if (texture._glRenderTargets[gl.id]) + if (texture._glRenderTargets[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id] = null; - texture._glRenderTargets[gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID] = null; + texture._glRenderTargets[this.renderer.CONTEXT_UID].destroy(); //.destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -88,9 +88,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/TextureManager.js b/src/core/renderers/webgl/TextureManager.js index a0f4492..eb78645 100644 --- a/src/core/renderers/webgl/TextureManager.js +++ b/src/core/renderers/webgl/TextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var TextureManager = function(gl) +var TextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer; + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -45,14 +46,13 @@ } - var gl = this.gl; - var glTexture = texture._glTextures[gl.id];//texture._glTextures[gl.id]; + var glTexture = texture._glTextures[this.renderer.CONTEXT_UID]; if (!glTexture) { - glTexture = new GLTexture(gl); + glTexture = new GLTexture(this.gl); glTexture.premultiplyAlpha = true; - texture._glTextures[gl.id] = glTexture; + texture._glTextures[this.renderer.CONTEXT_UID] = glTexture; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -93,14 +93,14 @@ return; } - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID].destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -118,9 +118,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 6a0efa7..638d56f 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -103,6 +103,7 @@ this.gl.id = this.CONTEXT_UID = CONTEXT_UID++; this.state = new WebGLState(this.gl); + this.renderingToScreen = true; /** * Manages the filters. @@ -148,11 +149,9 @@ { var gl = this.gl; - - // create a texture manager... - this.textureManager = new TextureManager(gl); - this.renderTextureManager = new RenderTextureManager(gl); + this.textureManager = new TextureManager(this); + this.renderTextureManager = new RenderTextureManager(this); this.state.resetToDefault(); @@ -174,6 +173,9 @@ */ WebGLRenderer.prototype.render = function (displayObject, renderTexture, clear, transform, skipUpdateTransform) { + // can be handy to know! + this.renderingToScreen = !renderTexture; + this.emit('prerender'); // no point rendering if our context has been blown up! @@ -271,12 +273,12 @@ { var baseTexture = renderTexture.baseTexture; - if(!baseTexture._glRenderTargets[gl.id]) + if(!baseTexture._glRenderTargets[this.CONTEXT_UID]) { this.renderTextureManager.updateTexture(baseTexture); } - renderTarget = baseTexture._glRenderTargets[gl.id]; + renderTarget = baseTexture._glRenderTargets[this.CONTEXT_UID]; renderTarget.setFrame(renderTexture.frame); } @@ -350,7 +352,7 @@ //TODO - can we cache this texture too? this._activeTexture = texture; - if (!texture._glTextures[gl.id]) + if (!texture._glTextures[this.CONTEXT_UID]) { // this will also bind the texture.. this.textureManager.updateTexture(texture); @@ -358,7 +360,7 @@ else { // bind the current texture - texture._glTextures[gl.id].bind(); + texture._glTextures[this.CONTEXT_UID].bind(); } return this; diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.frag b/src/core/renderers/webgl/filters/FXAA/FXAA.frag deleted file mode 100755 index 2f3b3ae..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.frag +++ /dev/null @@ -1,127 +0,0 @@ -precision lowp float; - - -/** -Basic FXAA implementation based on the code on geeks3d.com with the -modification that the texture2DLod stuff was removed since it's -unsupported by WebGL. - --- - -From: -https://github.com/mitsuhiko/webgl-meincraft - -Copyright (c) 2011 by Armin Ronacher. - -Some rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following - disclaimer in the documentation and/or other materials provided - with the distribution. - - * The names of the contributors may not be used to endorse or - promote products derived from this software without specific - prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef FXAA_REDUCE_MIN - #define FXAA_REDUCE_MIN (1.0/ 128.0) -#endif -#ifndef FXAA_REDUCE_MUL - #define FXAA_REDUCE_MUL (1.0 / 8.0) -#endif -#ifndef FXAA_SPAN_MAX - #define FXAA_SPAN_MAX 8.0 -#endif - -//optimized version for mobile, where dependent -//texture reads can be a bottleneck -vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution, - vec2 v_rgbNW, vec2 v_rgbNE, - vec2 v_rgbSW, vec2 v_rgbSE, - vec2 v_rgbM) { - vec4 color; - mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y); - vec3 rgbNW = texture2D(tex, v_rgbNW).xyz; - vec3 rgbNE = texture2D(tex, v_rgbNE).xyz; - vec3 rgbSW = texture2D(tex, v_rgbSW).xyz; - vec3 rgbSE = texture2D(tex, v_rgbSE).xyz; - vec4 texColor = texture2D(tex, v_rgbM); - vec3 rgbM = texColor.xyz; - vec3 luma = vec3(0.299, 0.587, 0.114); - float lumaNW = dot(rgbNW, luma); - float lumaNE = dot(rgbNE, luma); - float lumaSW = dot(rgbSW, luma); - float lumaSE = dot(rgbSE, luma); - float lumaM = dot(rgbM, luma); - float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); - float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); - - mediump vec2 dir; - dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); - dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); - - float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * - (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); - - float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); - dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), - max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), - dir * rcpDirMin)) * inverseVP; - - vec3 rgbA = 0.5 * ( - texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz + - texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz); - vec3 rgbB = rgbA * 0.5 + 0.25 * ( - texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz + - texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz); - - float lumaB = dot(rgbB, luma); - if ((lumaB < lumaMin) || (lumaB > lumaMax)) - color = vec4(rgbA, texColor.a); - else - color = vec4(rgbB, texColor.a); - return color; -} - - -varying vec2 vTextureCoord; -varying vec4 vColor; -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - -uniform sampler2D uSampler; - - -void main(void){ - - gl_FragColor = fxaa(uSampler, vTextureCoord * vResolution, vResolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); - -} diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.vert b/src/core/renderers/webgl/filters/FXAA/FXAA.vert deleted file mode 100755 index b0c1860..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.vert +++ /dev/null @@ -1,45 +0,0 @@ - -precision mediump float; - -attribute vec2 aVertexPosition; -attribute vec2 aTextureCoord; -attribute vec4 aColor; - -uniform mat3 projectionMatrix; -uniform vec2 resolution; - -varying vec2 vTextureCoord; -varying vec4 vColor; - -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - - -void texcoords(vec2 fragCoord, vec2 resolution, - out vec2 v_rgbNW, out vec2 v_rgbNE, - out vec2 v_rgbSW, out vec2 v_rgbSE, - out vec2 v_rgbM) { - vec2 inverseVP = 1.0 / resolution.xy; - v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP; - v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP; - v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP; - v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP; - v_rgbM = vec2(fragCoord * inverseVP); -} - -void main(void){ - gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0); - vTextureCoord = aTextureCoord; - vColor = vec4(aColor.rgb * aColor.a, aColor.a); - vResolution = resolution; - - //compute the texture coords and send them to varyings - texcoords(aTextureCoord * resolution, resolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); -} diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index b4b02a0..1e8115b 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -177,6 +177,7 @@ */ AccessibilityManager.prototype.update = function() { + if(!this.renderer.renderingToScreen)return; // update children... this.updateAccessibleObjects(this.renderer._lastObjectRendered); diff --git a/src/core/graphics/webgl/GraphicsRenderer.js b/src/core/graphics/webgl/GraphicsRenderer.js index 282ba87..8253f1a 100644 --- a/src/core/graphics/webgl/GraphicsRenderer.js +++ b/src/core/graphics/webgl/GraphicsRenderer.js @@ -34,6 +34,9 @@ this.primitiveShader = null; this.gl = renderer.gl; + + // easy access! + this.CONTEXT_UID = 0; } GraphicsRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -51,6 +54,7 @@ GraphicsRenderer.prototype.onContextChange = function() { this.gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; this.primitiveShader = new PrimitiveShader(this.gl) }; @@ -81,12 +85,12 @@ var webGLData; - if (graphics.dirty || !graphics._webGL[gl.id]) + if (graphics.dirty || !graphics._webGL[this.CONTEXT_UID]) { this.updateGraphics(graphics); } - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // This could be speeded up for sure! var shader = this.primitiveShader; @@ -120,12 +124,12 @@ var gl = this.renderer.gl; // get the contexts graphics object - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // if the graphics object does not exist in the webGL context time to create it! if (!webGL) { - webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + webGL = graphics._webGL[this.CONTEXT_UID] = {lastIndex:0, data:[], gl:gl}; } diff --git a/src/core/particles/webgl/ParticleRenderer.js b/src/core/particles/webgl/ParticleRenderer.js index 0b0abd0..6e7b9c0 100644 --- a/src/core/particles/webgl/ParticleRenderer.js +++ b/src/core/particles/webgl/ParticleRenderer.js @@ -61,6 +61,8 @@ this.properties = null; this.tempMatrix = new math.Matrix(); + + this.CONTEXT_UID = 0; } ParticleRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -79,6 +81,8 @@ return; var gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; + // setup default shader this.shader = new ParticleShader(this.renderer.shaderManager); @@ -190,7 +194,7 @@ // make sure the texture is bound.. var baseTexture = children[0]._texture.baseTexture; - if (!baseTexture._glTextures[gl.id]) + if (!baseTexture._glTextures[this.CONTEXT_UID]) { // if the texture has not updated then lets not upload any static properties if(!this.renderer.updateTexture(baseTexture)) @@ -205,7 +209,7 @@ } else { - gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[gl.id]); + gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[this.CONTEXT_UID]); } // now lets upload and render the buffers.. diff --git a/src/core/renderers/webgl/RenderTextureManager.js b/src/core/renderers/webgl/RenderTextureManager.js index cbbbd4c..e922209 100644 --- a/src/core/renderers/webgl/RenderTextureManager.js +++ b/src/core/renderers/webgl/RenderTextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var RenderTextureManager = function(gl) +var RenderTextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -27,16 +28,15 @@ { texture = texture.baseTexture || texture; - var gl = this.gl; - var renderTarget = texture._glRenderTargets[gl.id]; + var renderTarget = texture._glRenderTargets[this.renderer.CONTEXT_UID]; if (!renderTarget) { - renderTarget = new RenderTarget(gl, texture.width, texture.height); + renderTarget = new RenderTarget(this.gl, texture.width, texture.height, texture.scaleMode, texture.resolution); - texture._glTextures[gl.id] = renderTarget.texture; - texture._glRenderTargets[gl.id] = renderTarget; + texture._glTextures[this.renderer.CONTEXT_UID] = renderTarget.texture; + texture._glRenderTargets[this.renderer.CONTEXT_UID] = renderTarget; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -60,17 +60,17 @@ texture = texture.baseTexture || texture; var gl = this.gl; - if (texture._glRenderTargets[gl.id]) + if (texture._glRenderTargets[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id] = null; - texture._glRenderTargets[gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID] = null; + texture._glRenderTargets[this.renderer.CONTEXT_UID].destroy(); //.destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -88,9 +88,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/TextureManager.js b/src/core/renderers/webgl/TextureManager.js index a0f4492..eb78645 100644 --- a/src/core/renderers/webgl/TextureManager.js +++ b/src/core/renderers/webgl/TextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var TextureManager = function(gl) +var TextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer; + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -45,14 +46,13 @@ } - var gl = this.gl; - var glTexture = texture._glTextures[gl.id];//texture._glTextures[gl.id]; + var glTexture = texture._glTextures[this.renderer.CONTEXT_UID]; if (!glTexture) { - glTexture = new GLTexture(gl); + glTexture = new GLTexture(this.gl); glTexture.premultiplyAlpha = true; - texture._glTextures[gl.id] = glTexture; + texture._glTextures[this.renderer.CONTEXT_UID] = glTexture; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -93,14 +93,14 @@ return; } - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID].destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -118,9 +118,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 6a0efa7..638d56f 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -103,6 +103,7 @@ this.gl.id = this.CONTEXT_UID = CONTEXT_UID++; this.state = new WebGLState(this.gl); + this.renderingToScreen = true; /** * Manages the filters. @@ -148,11 +149,9 @@ { var gl = this.gl; - - // create a texture manager... - this.textureManager = new TextureManager(gl); - this.renderTextureManager = new RenderTextureManager(gl); + this.textureManager = new TextureManager(this); + this.renderTextureManager = new RenderTextureManager(this); this.state.resetToDefault(); @@ -174,6 +173,9 @@ */ WebGLRenderer.prototype.render = function (displayObject, renderTexture, clear, transform, skipUpdateTransform) { + // can be handy to know! + this.renderingToScreen = !renderTexture; + this.emit('prerender'); // no point rendering if our context has been blown up! @@ -271,12 +273,12 @@ { var baseTexture = renderTexture.baseTexture; - if(!baseTexture._glRenderTargets[gl.id]) + if(!baseTexture._glRenderTargets[this.CONTEXT_UID]) { this.renderTextureManager.updateTexture(baseTexture); } - renderTarget = baseTexture._glRenderTargets[gl.id]; + renderTarget = baseTexture._glRenderTargets[this.CONTEXT_UID]; renderTarget.setFrame(renderTexture.frame); } @@ -350,7 +352,7 @@ //TODO - can we cache this texture too? this._activeTexture = texture; - if (!texture._glTextures[gl.id]) + if (!texture._glTextures[this.CONTEXT_UID]) { // this will also bind the texture.. this.textureManager.updateTexture(texture); @@ -358,7 +360,7 @@ else { // bind the current texture - texture._glTextures[gl.id].bind(); + texture._glTextures[this.CONTEXT_UID].bind(); } return this; diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.frag b/src/core/renderers/webgl/filters/FXAA/FXAA.frag deleted file mode 100755 index 2f3b3ae..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.frag +++ /dev/null @@ -1,127 +0,0 @@ -precision lowp float; - - -/** -Basic FXAA implementation based on the code on geeks3d.com with the -modification that the texture2DLod stuff was removed since it's -unsupported by WebGL. - --- - -From: -https://github.com/mitsuhiko/webgl-meincraft - -Copyright (c) 2011 by Armin Ronacher. - -Some rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following - disclaimer in the documentation and/or other materials provided - with the distribution. - - * The names of the contributors may not be used to endorse or - promote products derived from this software without specific - prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef FXAA_REDUCE_MIN - #define FXAA_REDUCE_MIN (1.0/ 128.0) -#endif -#ifndef FXAA_REDUCE_MUL - #define FXAA_REDUCE_MUL (1.0 / 8.0) -#endif -#ifndef FXAA_SPAN_MAX - #define FXAA_SPAN_MAX 8.0 -#endif - -//optimized version for mobile, where dependent -//texture reads can be a bottleneck -vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution, - vec2 v_rgbNW, vec2 v_rgbNE, - vec2 v_rgbSW, vec2 v_rgbSE, - vec2 v_rgbM) { - vec4 color; - mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y); - vec3 rgbNW = texture2D(tex, v_rgbNW).xyz; - vec3 rgbNE = texture2D(tex, v_rgbNE).xyz; - vec3 rgbSW = texture2D(tex, v_rgbSW).xyz; - vec3 rgbSE = texture2D(tex, v_rgbSE).xyz; - vec4 texColor = texture2D(tex, v_rgbM); - vec3 rgbM = texColor.xyz; - vec3 luma = vec3(0.299, 0.587, 0.114); - float lumaNW = dot(rgbNW, luma); - float lumaNE = dot(rgbNE, luma); - float lumaSW = dot(rgbSW, luma); - float lumaSE = dot(rgbSE, luma); - float lumaM = dot(rgbM, luma); - float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); - float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); - - mediump vec2 dir; - dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); - dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); - - float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * - (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); - - float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); - dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), - max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), - dir * rcpDirMin)) * inverseVP; - - vec3 rgbA = 0.5 * ( - texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz + - texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz); - vec3 rgbB = rgbA * 0.5 + 0.25 * ( - texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz + - texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz); - - float lumaB = dot(rgbB, luma); - if ((lumaB < lumaMin) || (lumaB > lumaMax)) - color = vec4(rgbA, texColor.a); - else - color = vec4(rgbB, texColor.a); - return color; -} - - -varying vec2 vTextureCoord; -varying vec4 vColor; -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - -uniform sampler2D uSampler; - - -void main(void){ - - gl_FragColor = fxaa(uSampler, vTextureCoord * vResolution, vResolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); - -} diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.vert b/src/core/renderers/webgl/filters/FXAA/FXAA.vert deleted file mode 100755 index b0c1860..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.vert +++ /dev/null @@ -1,45 +0,0 @@ - -precision mediump float; - -attribute vec2 aVertexPosition; -attribute vec2 aTextureCoord; -attribute vec4 aColor; - -uniform mat3 projectionMatrix; -uniform vec2 resolution; - -varying vec2 vTextureCoord; -varying vec4 vColor; - -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - - -void texcoords(vec2 fragCoord, vec2 resolution, - out vec2 v_rgbNW, out vec2 v_rgbNE, - out vec2 v_rgbSW, out vec2 v_rgbSE, - out vec2 v_rgbM) { - vec2 inverseVP = 1.0 / resolution.xy; - v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP; - v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP; - v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP; - v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP; - v_rgbM = vec2(fragCoord * inverseVP); -} - -void main(void){ - gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0); - vTextureCoord = aTextureCoord; - vColor = vec4(aColor.rgb * aColor.a, aColor.a); - vResolution = resolution; - - //compute the texture coords and send them to varyings - texcoords(aTextureCoord * resolution, resolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); -} diff --git a/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js b/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js deleted file mode 100644 index 6d5646c..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js +++ /dev/null @@ -1,53 +0,0 @@ -var AbstractFilter = require('./AbstractFilter'); -// @see https://github.com/substack/brfs/issues/25 -var fs = require('fs'); - -/** - * - * Basic FXAA implementation based on the code on geeks3d.com with the - * modification that the texture2DLod stuff was removed since it's - * unsupported by WebGL. - * - * -- - * From: - * https://github.com/mitsuhiko/webgl-meincraft - * - * @class - * @extends PIXI.AbstractFilter - * @memberof PIXI - * - */ -function FXAAFilter() -{ - AbstractFilter.call(this, - // vertex shader - fs.readFileSync(__dirname + '/FXAA.vert', 'utf8'), - // fragment shader - fs.readFileSync(__dirname + '/FXAA.frag', 'utf8'), - // uniforms - { - resolution: { type: 'v2', value: { x: 1, y: 1 } } - } - ); - -} - -FXAAFilter.prototype = Object.create(AbstractFilter.prototype); -FXAAFilter.prototype.constructor = FXAAFilter; -module.exports = FXAAFilter; - -/** - * Applies the filter - * - * @param renderer {PIXI.WebGLRenderer} The renderer to retrieve the filter from - * @param input {PIXI.RenderTarget} - * @param output {PIXI.RenderTarget} - */ -FXAAFilter.prototype.applyFilter = function (renderer, input, output) -{ - var filterManager = renderer.filterManager; - - var shader = this.getShader( renderer ); - // draw the filter... - filterManager.applyFilter(shader, input, output); -}; diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index b4b02a0..1e8115b 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -177,6 +177,7 @@ */ AccessibilityManager.prototype.update = function() { + if(!this.renderer.renderingToScreen)return; // update children... this.updateAccessibleObjects(this.renderer._lastObjectRendered); diff --git a/src/core/graphics/webgl/GraphicsRenderer.js b/src/core/graphics/webgl/GraphicsRenderer.js index 282ba87..8253f1a 100644 --- a/src/core/graphics/webgl/GraphicsRenderer.js +++ b/src/core/graphics/webgl/GraphicsRenderer.js @@ -34,6 +34,9 @@ this.primitiveShader = null; this.gl = renderer.gl; + + // easy access! + this.CONTEXT_UID = 0; } GraphicsRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -51,6 +54,7 @@ GraphicsRenderer.prototype.onContextChange = function() { this.gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; this.primitiveShader = new PrimitiveShader(this.gl) }; @@ -81,12 +85,12 @@ var webGLData; - if (graphics.dirty || !graphics._webGL[gl.id]) + if (graphics.dirty || !graphics._webGL[this.CONTEXT_UID]) { this.updateGraphics(graphics); } - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // This could be speeded up for sure! var shader = this.primitiveShader; @@ -120,12 +124,12 @@ var gl = this.renderer.gl; // get the contexts graphics object - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // if the graphics object does not exist in the webGL context time to create it! if (!webGL) { - webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + webGL = graphics._webGL[this.CONTEXT_UID] = {lastIndex:0, data:[], gl:gl}; } diff --git a/src/core/particles/webgl/ParticleRenderer.js b/src/core/particles/webgl/ParticleRenderer.js index 0b0abd0..6e7b9c0 100644 --- a/src/core/particles/webgl/ParticleRenderer.js +++ b/src/core/particles/webgl/ParticleRenderer.js @@ -61,6 +61,8 @@ this.properties = null; this.tempMatrix = new math.Matrix(); + + this.CONTEXT_UID = 0; } ParticleRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -79,6 +81,8 @@ return; var gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; + // setup default shader this.shader = new ParticleShader(this.renderer.shaderManager); @@ -190,7 +194,7 @@ // make sure the texture is bound.. var baseTexture = children[0]._texture.baseTexture; - if (!baseTexture._glTextures[gl.id]) + if (!baseTexture._glTextures[this.CONTEXT_UID]) { // if the texture has not updated then lets not upload any static properties if(!this.renderer.updateTexture(baseTexture)) @@ -205,7 +209,7 @@ } else { - gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[gl.id]); + gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[this.CONTEXT_UID]); } // now lets upload and render the buffers.. diff --git a/src/core/renderers/webgl/RenderTextureManager.js b/src/core/renderers/webgl/RenderTextureManager.js index cbbbd4c..e922209 100644 --- a/src/core/renderers/webgl/RenderTextureManager.js +++ b/src/core/renderers/webgl/RenderTextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var RenderTextureManager = function(gl) +var RenderTextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -27,16 +28,15 @@ { texture = texture.baseTexture || texture; - var gl = this.gl; - var renderTarget = texture._glRenderTargets[gl.id]; + var renderTarget = texture._glRenderTargets[this.renderer.CONTEXT_UID]; if (!renderTarget) { - renderTarget = new RenderTarget(gl, texture.width, texture.height); + renderTarget = new RenderTarget(this.gl, texture.width, texture.height, texture.scaleMode, texture.resolution); - texture._glTextures[gl.id] = renderTarget.texture; - texture._glRenderTargets[gl.id] = renderTarget; + texture._glTextures[this.renderer.CONTEXT_UID] = renderTarget.texture; + texture._glRenderTargets[this.renderer.CONTEXT_UID] = renderTarget; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -60,17 +60,17 @@ texture = texture.baseTexture || texture; var gl = this.gl; - if (texture._glRenderTargets[gl.id]) + if (texture._glRenderTargets[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id] = null; - texture._glRenderTargets[gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID] = null; + texture._glRenderTargets[this.renderer.CONTEXT_UID].destroy(); //.destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -88,9 +88,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/TextureManager.js b/src/core/renderers/webgl/TextureManager.js index a0f4492..eb78645 100644 --- a/src/core/renderers/webgl/TextureManager.js +++ b/src/core/renderers/webgl/TextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var TextureManager = function(gl) +var TextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer; + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -45,14 +46,13 @@ } - var gl = this.gl; - var glTexture = texture._glTextures[gl.id];//texture._glTextures[gl.id]; + var glTexture = texture._glTextures[this.renderer.CONTEXT_UID]; if (!glTexture) { - glTexture = new GLTexture(gl); + glTexture = new GLTexture(this.gl); glTexture.premultiplyAlpha = true; - texture._glTextures[gl.id] = glTexture; + texture._glTextures[this.renderer.CONTEXT_UID] = glTexture; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -93,14 +93,14 @@ return; } - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID].destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -118,9 +118,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 6a0efa7..638d56f 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -103,6 +103,7 @@ this.gl.id = this.CONTEXT_UID = CONTEXT_UID++; this.state = new WebGLState(this.gl); + this.renderingToScreen = true; /** * Manages the filters. @@ -148,11 +149,9 @@ { var gl = this.gl; - - // create a texture manager... - this.textureManager = new TextureManager(gl); - this.renderTextureManager = new RenderTextureManager(gl); + this.textureManager = new TextureManager(this); + this.renderTextureManager = new RenderTextureManager(this); this.state.resetToDefault(); @@ -174,6 +173,9 @@ */ WebGLRenderer.prototype.render = function (displayObject, renderTexture, clear, transform, skipUpdateTransform) { + // can be handy to know! + this.renderingToScreen = !renderTexture; + this.emit('prerender'); // no point rendering if our context has been blown up! @@ -271,12 +273,12 @@ { var baseTexture = renderTexture.baseTexture; - if(!baseTexture._glRenderTargets[gl.id]) + if(!baseTexture._glRenderTargets[this.CONTEXT_UID]) { this.renderTextureManager.updateTexture(baseTexture); } - renderTarget = baseTexture._glRenderTargets[gl.id]; + renderTarget = baseTexture._glRenderTargets[this.CONTEXT_UID]; renderTarget.setFrame(renderTexture.frame); } @@ -350,7 +352,7 @@ //TODO - can we cache this texture too? this._activeTexture = texture; - if (!texture._glTextures[gl.id]) + if (!texture._glTextures[this.CONTEXT_UID]) { // this will also bind the texture.. this.textureManager.updateTexture(texture); @@ -358,7 +360,7 @@ else { // bind the current texture - texture._glTextures[gl.id].bind(); + texture._glTextures[this.CONTEXT_UID].bind(); } return this; diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.frag b/src/core/renderers/webgl/filters/FXAA/FXAA.frag deleted file mode 100755 index 2f3b3ae..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.frag +++ /dev/null @@ -1,127 +0,0 @@ -precision lowp float; - - -/** -Basic FXAA implementation based on the code on geeks3d.com with the -modification that the texture2DLod stuff was removed since it's -unsupported by WebGL. - --- - -From: -https://github.com/mitsuhiko/webgl-meincraft - -Copyright (c) 2011 by Armin Ronacher. - -Some rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following - disclaimer in the documentation and/or other materials provided - with the distribution. - - * The names of the contributors may not be used to endorse or - promote products derived from this software without specific - prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef FXAA_REDUCE_MIN - #define FXAA_REDUCE_MIN (1.0/ 128.0) -#endif -#ifndef FXAA_REDUCE_MUL - #define FXAA_REDUCE_MUL (1.0 / 8.0) -#endif -#ifndef FXAA_SPAN_MAX - #define FXAA_SPAN_MAX 8.0 -#endif - -//optimized version for mobile, where dependent -//texture reads can be a bottleneck -vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution, - vec2 v_rgbNW, vec2 v_rgbNE, - vec2 v_rgbSW, vec2 v_rgbSE, - vec2 v_rgbM) { - vec4 color; - mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y); - vec3 rgbNW = texture2D(tex, v_rgbNW).xyz; - vec3 rgbNE = texture2D(tex, v_rgbNE).xyz; - vec3 rgbSW = texture2D(tex, v_rgbSW).xyz; - vec3 rgbSE = texture2D(tex, v_rgbSE).xyz; - vec4 texColor = texture2D(tex, v_rgbM); - vec3 rgbM = texColor.xyz; - vec3 luma = vec3(0.299, 0.587, 0.114); - float lumaNW = dot(rgbNW, luma); - float lumaNE = dot(rgbNE, luma); - float lumaSW = dot(rgbSW, luma); - float lumaSE = dot(rgbSE, luma); - float lumaM = dot(rgbM, luma); - float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); - float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); - - mediump vec2 dir; - dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); - dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); - - float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * - (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); - - float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); - dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), - max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), - dir * rcpDirMin)) * inverseVP; - - vec3 rgbA = 0.5 * ( - texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz + - texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz); - vec3 rgbB = rgbA * 0.5 + 0.25 * ( - texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz + - texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz); - - float lumaB = dot(rgbB, luma); - if ((lumaB < lumaMin) || (lumaB > lumaMax)) - color = vec4(rgbA, texColor.a); - else - color = vec4(rgbB, texColor.a); - return color; -} - - -varying vec2 vTextureCoord; -varying vec4 vColor; -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - -uniform sampler2D uSampler; - - -void main(void){ - - gl_FragColor = fxaa(uSampler, vTextureCoord * vResolution, vResolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); - -} diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.vert b/src/core/renderers/webgl/filters/FXAA/FXAA.vert deleted file mode 100755 index b0c1860..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.vert +++ /dev/null @@ -1,45 +0,0 @@ - -precision mediump float; - -attribute vec2 aVertexPosition; -attribute vec2 aTextureCoord; -attribute vec4 aColor; - -uniform mat3 projectionMatrix; -uniform vec2 resolution; - -varying vec2 vTextureCoord; -varying vec4 vColor; - -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - - -void texcoords(vec2 fragCoord, vec2 resolution, - out vec2 v_rgbNW, out vec2 v_rgbNE, - out vec2 v_rgbSW, out vec2 v_rgbSE, - out vec2 v_rgbM) { - vec2 inverseVP = 1.0 / resolution.xy; - v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP; - v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP; - v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP; - v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP; - v_rgbM = vec2(fragCoord * inverseVP); -} - -void main(void){ - gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0); - vTextureCoord = aTextureCoord; - vColor = vec4(aColor.rgb * aColor.a, aColor.a); - vResolution = resolution; - - //compute the texture coords and send them to varyings - texcoords(aTextureCoord * resolution, resolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); -} diff --git a/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js b/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js deleted file mode 100644 index 6d5646c..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js +++ /dev/null @@ -1,53 +0,0 @@ -var AbstractFilter = require('./AbstractFilter'); -// @see https://github.com/substack/brfs/issues/25 -var fs = require('fs'); - -/** - * - * Basic FXAA implementation based on the code on geeks3d.com with the - * modification that the texture2DLod stuff was removed since it's - * unsupported by WebGL. - * - * -- - * From: - * https://github.com/mitsuhiko/webgl-meincraft - * - * @class - * @extends PIXI.AbstractFilter - * @memberof PIXI - * - */ -function FXAAFilter() -{ - AbstractFilter.call(this, - // vertex shader - fs.readFileSync(__dirname + '/FXAA.vert', 'utf8'), - // fragment shader - fs.readFileSync(__dirname + '/FXAA.frag', 'utf8'), - // uniforms - { - resolution: { type: 'v2', value: { x: 1, y: 1 } } - } - ); - -} - -FXAAFilter.prototype = Object.create(AbstractFilter.prototype); -FXAAFilter.prototype.constructor = FXAAFilter; -module.exports = FXAAFilter; - -/** - * Applies the filter - * - * @param renderer {PIXI.WebGLRenderer} The renderer to retrieve the filter from - * @param input {PIXI.RenderTarget} - * @param output {PIXI.RenderTarget} - */ -FXAAFilter.prototype.applyFilter = function (renderer, input, output) -{ - var filterManager = renderer.filterManager; - - var shader = this.getShader( renderer ); - // draw the filter... - filterManager.applyFilter(shader, input, output); -}; diff --git a/src/core/renderers/webgl/managers/FilterManager.js b/src/core/renderers/webgl/managers/FilterManager.js index 5fc6562..4f9f142 100644 --- a/src/core/renderers/webgl/managers/FilterManager.js +++ b/src/core/renderers/webgl/managers/FilterManager.js @@ -137,7 +137,7 @@ { var renderer = this.renderer; var lastState = this.stack[this.stackIndex-1]; - var shader = filter.glShaders[gl.id]; + var shader = filter.glShaders[renderer.CONTEXT_UID]; // cacheing.. if(!shader) @@ -148,12 +148,12 @@ if(!shader) { - shader = filter.glShaders[gl.id] = this.shaderCache[filter.glShaderKey] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); + shader = filter.glShaders[renderer.CONTEXT_UID] = this.shaderCache[filter.glShaderKey] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); } } else { - shader = filter.glShaders[gl.id] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); + shader = filter.glShaders[renderer.CONTEXT_UID] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); } //TODO - this only needs to be done once? diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index b4b02a0..1e8115b 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -177,6 +177,7 @@ */ AccessibilityManager.prototype.update = function() { + if(!this.renderer.renderingToScreen)return; // update children... this.updateAccessibleObjects(this.renderer._lastObjectRendered); diff --git a/src/core/graphics/webgl/GraphicsRenderer.js b/src/core/graphics/webgl/GraphicsRenderer.js index 282ba87..8253f1a 100644 --- a/src/core/graphics/webgl/GraphicsRenderer.js +++ b/src/core/graphics/webgl/GraphicsRenderer.js @@ -34,6 +34,9 @@ this.primitiveShader = null; this.gl = renderer.gl; + + // easy access! + this.CONTEXT_UID = 0; } GraphicsRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -51,6 +54,7 @@ GraphicsRenderer.prototype.onContextChange = function() { this.gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; this.primitiveShader = new PrimitiveShader(this.gl) }; @@ -81,12 +85,12 @@ var webGLData; - if (graphics.dirty || !graphics._webGL[gl.id]) + if (graphics.dirty || !graphics._webGL[this.CONTEXT_UID]) { this.updateGraphics(graphics); } - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // This could be speeded up for sure! var shader = this.primitiveShader; @@ -120,12 +124,12 @@ var gl = this.renderer.gl; // get the contexts graphics object - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // if the graphics object does not exist in the webGL context time to create it! if (!webGL) { - webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + webGL = graphics._webGL[this.CONTEXT_UID] = {lastIndex:0, data:[], gl:gl}; } diff --git a/src/core/particles/webgl/ParticleRenderer.js b/src/core/particles/webgl/ParticleRenderer.js index 0b0abd0..6e7b9c0 100644 --- a/src/core/particles/webgl/ParticleRenderer.js +++ b/src/core/particles/webgl/ParticleRenderer.js @@ -61,6 +61,8 @@ this.properties = null; this.tempMatrix = new math.Matrix(); + + this.CONTEXT_UID = 0; } ParticleRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -79,6 +81,8 @@ return; var gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; + // setup default shader this.shader = new ParticleShader(this.renderer.shaderManager); @@ -190,7 +194,7 @@ // make sure the texture is bound.. var baseTexture = children[0]._texture.baseTexture; - if (!baseTexture._glTextures[gl.id]) + if (!baseTexture._glTextures[this.CONTEXT_UID]) { // if the texture has not updated then lets not upload any static properties if(!this.renderer.updateTexture(baseTexture)) @@ -205,7 +209,7 @@ } else { - gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[gl.id]); + gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[this.CONTEXT_UID]); } // now lets upload and render the buffers.. diff --git a/src/core/renderers/webgl/RenderTextureManager.js b/src/core/renderers/webgl/RenderTextureManager.js index cbbbd4c..e922209 100644 --- a/src/core/renderers/webgl/RenderTextureManager.js +++ b/src/core/renderers/webgl/RenderTextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var RenderTextureManager = function(gl) +var RenderTextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -27,16 +28,15 @@ { texture = texture.baseTexture || texture; - var gl = this.gl; - var renderTarget = texture._glRenderTargets[gl.id]; + var renderTarget = texture._glRenderTargets[this.renderer.CONTEXT_UID]; if (!renderTarget) { - renderTarget = new RenderTarget(gl, texture.width, texture.height); + renderTarget = new RenderTarget(this.gl, texture.width, texture.height, texture.scaleMode, texture.resolution); - texture._glTextures[gl.id] = renderTarget.texture; - texture._glRenderTargets[gl.id] = renderTarget; + texture._glTextures[this.renderer.CONTEXT_UID] = renderTarget.texture; + texture._glRenderTargets[this.renderer.CONTEXT_UID] = renderTarget; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -60,17 +60,17 @@ texture = texture.baseTexture || texture; var gl = this.gl; - if (texture._glRenderTargets[gl.id]) + if (texture._glRenderTargets[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id] = null; - texture._glRenderTargets[gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID] = null; + texture._glRenderTargets[this.renderer.CONTEXT_UID].destroy(); //.destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -88,9 +88,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/TextureManager.js b/src/core/renderers/webgl/TextureManager.js index a0f4492..eb78645 100644 --- a/src/core/renderers/webgl/TextureManager.js +++ b/src/core/renderers/webgl/TextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var TextureManager = function(gl) +var TextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer; + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -45,14 +46,13 @@ } - var gl = this.gl; - var glTexture = texture._glTextures[gl.id];//texture._glTextures[gl.id]; + var glTexture = texture._glTextures[this.renderer.CONTEXT_UID]; if (!glTexture) { - glTexture = new GLTexture(gl); + glTexture = new GLTexture(this.gl); glTexture.premultiplyAlpha = true; - texture._glTextures[gl.id] = glTexture; + texture._glTextures[this.renderer.CONTEXT_UID] = glTexture; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -93,14 +93,14 @@ return; } - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID].destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -118,9 +118,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 6a0efa7..638d56f 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -103,6 +103,7 @@ this.gl.id = this.CONTEXT_UID = CONTEXT_UID++; this.state = new WebGLState(this.gl); + this.renderingToScreen = true; /** * Manages the filters. @@ -148,11 +149,9 @@ { var gl = this.gl; - - // create a texture manager... - this.textureManager = new TextureManager(gl); - this.renderTextureManager = new RenderTextureManager(gl); + this.textureManager = new TextureManager(this); + this.renderTextureManager = new RenderTextureManager(this); this.state.resetToDefault(); @@ -174,6 +173,9 @@ */ WebGLRenderer.prototype.render = function (displayObject, renderTexture, clear, transform, skipUpdateTransform) { + // can be handy to know! + this.renderingToScreen = !renderTexture; + this.emit('prerender'); // no point rendering if our context has been blown up! @@ -271,12 +273,12 @@ { var baseTexture = renderTexture.baseTexture; - if(!baseTexture._glRenderTargets[gl.id]) + if(!baseTexture._glRenderTargets[this.CONTEXT_UID]) { this.renderTextureManager.updateTexture(baseTexture); } - renderTarget = baseTexture._glRenderTargets[gl.id]; + renderTarget = baseTexture._glRenderTargets[this.CONTEXT_UID]; renderTarget.setFrame(renderTexture.frame); } @@ -350,7 +352,7 @@ //TODO - can we cache this texture too? this._activeTexture = texture; - if (!texture._glTextures[gl.id]) + if (!texture._glTextures[this.CONTEXT_UID]) { // this will also bind the texture.. this.textureManager.updateTexture(texture); @@ -358,7 +360,7 @@ else { // bind the current texture - texture._glTextures[gl.id].bind(); + texture._glTextures[this.CONTEXT_UID].bind(); } return this; diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.frag b/src/core/renderers/webgl/filters/FXAA/FXAA.frag deleted file mode 100755 index 2f3b3ae..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.frag +++ /dev/null @@ -1,127 +0,0 @@ -precision lowp float; - - -/** -Basic FXAA implementation based on the code on geeks3d.com with the -modification that the texture2DLod stuff was removed since it's -unsupported by WebGL. - --- - -From: -https://github.com/mitsuhiko/webgl-meincraft - -Copyright (c) 2011 by Armin Ronacher. - -Some rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following - disclaimer in the documentation and/or other materials provided - with the distribution. - - * The names of the contributors may not be used to endorse or - promote products derived from this software without specific - prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef FXAA_REDUCE_MIN - #define FXAA_REDUCE_MIN (1.0/ 128.0) -#endif -#ifndef FXAA_REDUCE_MUL - #define FXAA_REDUCE_MUL (1.0 / 8.0) -#endif -#ifndef FXAA_SPAN_MAX - #define FXAA_SPAN_MAX 8.0 -#endif - -//optimized version for mobile, where dependent -//texture reads can be a bottleneck -vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution, - vec2 v_rgbNW, vec2 v_rgbNE, - vec2 v_rgbSW, vec2 v_rgbSE, - vec2 v_rgbM) { - vec4 color; - mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y); - vec3 rgbNW = texture2D(tex, v_rgbNW).xyz; - vec3 rgbNE = texture2D(tex, v_rgbNE).xyz; - vec3 rgbSW = texture2D(tex, v_rgbSW).xyz; - vec3 rgbSE = texture2D(tex, v_rgbSE).xyz; - vec4 texColor = texture2D(tex, v_rgbM); - vec3 rgbM = texColor.xyz; - vec3 luma = vec3(0.299, 0.587, 0.114); - float lumaNW = dot(rgbNW, luma); - float lumaNE = dot(rgbNE, luma); - float lumaSW = dot(rgbSW, luma); - float lumaSE = dot(rgbSE, luma); - float lumaM = dot(rgbM, luma); - float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); - float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); - - mediump vec2 dir; - dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); - dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); - - float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * - (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); - - float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); - dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), - max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), - dir * rcpDirMin)) * inverseVP; - - vec3 rgbA = 0.5 * ( - texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz + - texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz); - vec3 rgbB = rgbA * 0.5 + 0.25 * ( - texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz + - texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz); - - float lumaB = dot(rgbB, luma); - if ((lumaB < lumaMin) || (lumaB > lumaMax)) - color = vec4(rgbA, texColor.a); - else - color = vec4(rgbB, texColor.a); - return color; -} - - -varying vec2 vTextureCoord; -varying vec4 vColor; -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - -uniform sampler2D uSampler; - - -void main(void){ - - gl_FragColor = fxaa(uSampler, vTextureCoord * vResolution, vResolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); - -} diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.vert b/src/core/renderers/webgl/filters/FXAA/FXAA.vert deleted file mode 100755 index b0c1860..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.vert +++ /dev/null @@ -1,45 +0,0 @@ - -precision mediump float; - -attribute vec2 aVertexPosition; -attribute vec2 aTextureCoord; -attribute vec4 aColor; - -uniform mat3 projectionMatrix; -uniform vec2 resolution; - -varying vec2 vTextureCoord; -varying vec4 vColor; - -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - - -void texcoords(vec2 fragCoord, vec2 resolution, - out vec2 v_rgbNW, out vec2 v_rgbNE, - out vec2 v_rgbSW, out vec2 v_rgbSE, - out vec2 v_rgbM) { - vec2 inverseVP = 1.0 / resolution.xy; - v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP; - v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP; - v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP; - v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP; - v_rgbM = vec2(fragCoord * inverseVP); -} - -void main(void){ - gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0); - vTextureCoord = aTextureCoord; - vColor = vec4(aColor.rgb * aColor.a, aColor.a); - vResolution = resolution; - - //compute the texture coords and send them to varyings - texcoords(aTextureCoord * resolution, resolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); -} diff --git a/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js b/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js deleted file mode 100644 index 6d5646c..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js +++ /dev/null @@ -1,53 +0,0 @@ -var AbstractFilter = require('./AbstractFilter'); -// @see https://github.com/substack/brfs/issues/25 -var fs = require('fs'); - -/** - * - * Basic FXAA implementation based on the code on geeks3d.com with the - * modification that the texture2DLod stuff was removed since it's - * unsupported by WebGL. - * - * -- - * From: - * https://github.com/mitsuhiko/webgl-meincraft - * - * @class - * @extends PIXI.AbstractFilter - * @memberof PIXI - * - */ -function FXAAFilter() -{ - AbstractFilter.call(this, - // vertex shader - fs.readFileSync(__dirname + '/FXAA.vert', 'utf8'), - // fragment shader - fs.readFileSync(__dirname + '/FXAA.frag', 'utf8'), - // uniforms - { - resolution: { type: 'v2', value: { x: 1, y: 1 } } - } - ); - -} - -FXAAFilter.prototype = Object.create(AbstractFilter.prototype); -FXAAFilter.prototype.constructor = FXAAFilter; -module.exports = FXAAFilter; - -/** - * Applies the filter - * - * @param renderer {PIXI.WebGLRenderer} The renderer to retrieve the filter from - * @param input {PIXI.RenderTarget} - * @param output {PIXI.RenderTarget} - */ -FXAAFilter.prototype.applyFilter = function (renderer, input, output) -{ - var filterManager = renderer.filterManager; - - var shader = this.getShader( renderer ); - // draw the filter... - filterManager.applyFilter(shader, input, output); -}; diff --git a/src/core/renderers/webgl/managers/FilterManager.js b/src/core/renderers/webgl/managers/FilterManager.js index 5fc6562..4f9f142 100644 --- a/src/core/renderers/webgl/managers/FilterManager.js +++ b/src/core/renderers/webgl/managers/FilterManager.js @@ -137,7 +137,7 @@ { var renderer = this.renderer; var lastState = this.stack[this.stackIndex-1]; - var shader = filter.glShaders[gl.id]; + var shader = filter.glShaders[renderer.CONTEXT_UID]; // cacheing.. if(!shader) @@ -148,12 +148,12 @@ if(!shader) { - shader = filter.glShaders[gl.id] = this.shaderCache[filter.glShaderKey] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); + shader = filter.glShaders[renderer.CONTEXT_UID] = this.shaderCache[filter.glShaderKey] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); } } else { - shader = filter.glShaders[gl.id] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); + shader = filter.glShaders[renderer.CONTEXT_UID] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); } //TODO - this only needs to be done once? diff --git a/src/filters/FXAA/FXAA.frag b/src/filters/FXAA/FXAA.frag new file mode 100755 index 0000000..2f3b3ae --- /dev/null +++ b/src/filters/FXAA/FXAA.frag @@ -0,0 +1,127 @@ +precision lowp float; + + +/** +Basic FXAA implementation based on the code on geeks3d.com with the +modification that the texture2DLod stuff was removed since it's +unsupported by WebGL. + +-- + +From: +https://github.com/mitsuhiko/webgl-meincraft + +Copyright (c) 2011 by Armin Ronacher. + +Some rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * The names of the contributors may not be used to endorse or + promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef FXAA_REDUCE_MIN + #define FXAA_REDUCE_MIN (1.0/ 128.0) +#endif +#ifndef FXAA_REDUCE_MUL + #define FXAA_REDUCE_MUL (1.0 / 8.0) +#endif +#ifndef FXAA_SPAN_MAX + #define FXAA_SPAN_MAX 8.0 +#endif + +//optimized version for mobile, where dependent +//texture reads can be a bottleneck +vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution, + vec2 v_rgbNW, vec2 v_rgbNE, + vec2 v_rgbSW, vec2 v_rgbSE, + vec2 v_rgbM) { + vec4 color; + mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y); + vec3 rgbNW = texture2D(tex, v_rgbNW).xyz; + vec3 rgbNE = texture2D(tex, v_rgbNE).xyz; + vec3 rgbSW = texture2D(tex, v_rgbSW).xyz; + vec3 rgbSE = texture2D(tex, v_rgbSE).xyz; + vec4 texColor = texture2D(tex, v_rgbM); + vec3 rgbM = texColor.xyz; + vec3 luma = vec3(0.299, 0.587, 0.114); + float lumaNW = dot(rgbNW, luma); + float lumaNE = dot(rgbNE, luma); + float lumaSW = dot(rgbSW, luma); + float lumaSE = dot(rgbSE, luma); + float lumaM = dot(rgbM, luma); + float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); + float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); + + mediump vec2 dir; + dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); + dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); + + float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * + (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); + + float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); + dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), + max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), + dir * rcpDirMin)) * inverseVP; + + vec3 rgbA = 0.5 * ( + texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz + + texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz); + vec3 rgbB = rgbA * 0.5 + 0.25 * ( + texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz + + texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz); + + float lumaB = dot(rgbB, luma); + if ((lumaB < lumaMin) || (lumaB > lumaMax)) + color = vec4(rgbA, texColor.a); + else + color = vec4(rgbB, texColor.a); + return color; +} + + +varying vec2 vTextureCoord; +varying vec4 vColor; +varying vec2 vResolution; + +//texcoords computed in vertex step +//to avoid dependent texture reads +varying vec2 v_rgbNW; +varying vec2 v_rgbNE; +varying vec2 v_rgbSW; +varying vec2 v_rgbSE; +varying vec2 v_rgbM; + +uniform sampler2D uSampler; + + +void main(void){ + + gl_FragColor = fxaa(uSampler, vTextureCoord * vResolution, vResolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); + +} diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index b4b02a0..1e8115b 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -177,6 +177,7 @@ */ AccessibilityManager.prototype.update = function() { + if(!this.renderer.renderingToScreen)return; // update children... this.updateAccessibleObjects(this.renderer._lastObjectRendered); diff --git a/src/core/graphics/webgl/GraphicsRenderer.js b/src/core/graphics/webgl/GraphicsRenderer.js index 282ba87..8253f1a 100644 --- a/src/core/graphics/webgl/GraphicsRenderer.js +++ b/src/core/graphics/webgl/GraphicsRenderer.js @@ -34,6 +34,9 @@ this.primitiveShader = null; this.gl = renderer.gl; + + // easy access! + this.CONTEXT_UID = 0; } GraphicsRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -51,6 +54,7 @@ GraphicsRenderer.prototype.onContextChange = function() { this.gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; this.primitiveShader = new PrimitiveShader(this.gl) }; @@ -81,12 +85,12 @@ var webGLData; - if (graphics.dirty || !graphics._webGL[gl.id]) + if (graphics.dirty || !graphics._webGL[this.CONTEXT_UID]) { this.updateGraphics(graphics); } - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // This could be speeded up for sure! var shader = this.primitiveShader; @@ -120,12 +124,12 @@ var gl = this.renderer.gl; // get the contexts graphics object - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // if the graphics object does not exist in the webGL context time to create it! if (!webGL) { - webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + webGL = graphics._webGL[this.CONTEXT_UID] = {lastIndex:0, data:[], gl:gl}; } diff --git a/src/core/particles/webgl/ParticleRenderer.js b/src/core/particles/webgl/ParticleRenderer.js index 0b0abd0..6e7b9c0 100644 --- a/src/core/particles/webgl/ParticleRenderer.js +++ b/src/core/particles/webgl/ParticleRenderer.js @@ -61,6 +61,8 @@ this.properties = null; this.tempMatrix = new math.Matrix(); + + this.CONTEXT_UID = 0; } ParticleRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -79,6 +81,8 @@ return; var gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; + // setup default shader this.shader = new ParticleShader(this.renderer.shaderManager); @@ -190,7 +194,7 @@ // make sure the texture is bound.. var baseTexture = children[0]._texture.baseTexture; - if (!baseTexture._glTextures[gl.id]) + if (!baseTexture._glTextures[this.CONTEXT_UID]) { // if the texture has not updated then lets not upload any static properties if(!this.renderer.updateTexture(baseTexture)) @@ -205,7 +209,7 @@ } else { - gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[gl.id]); + gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[this.CONTEXT_UID]); } // now lets upload and render the buffers.. diff --git a/src/core/renderers/webgl/RenderTextureManager.js b/src/core/renderers/webgl/RenderTextureManager.js index cbbbd4c..e922209 100644 --- a/src/core/renderers/webgl/RenderTextureManager.js +++ b/src/core/renderers/webgl/RenderTextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var RenderTextureManager = function(gl) +var RenderTextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -27,16 +28,15 @@ { texture = texture.baseTexture || texture; - var gl = this.gl; - var renderTarget = texture._glRenderTargets[gl.id]; + var renderTarget = texture._glRenderTargets[this.renderer.CONTEXT_UID]; if (!renderTarget) { - renderTarget = new RenderTarget(gl, texture.width, texture.height); + renderTarget = new RenderTarget(this.gl, texture.width, texture.height, texture.scaleMode, texture.resolution); - texture._glTextures[gl.id] = renderTarget.texture; - texture._glRenderTargets[gl.id] = renderTarget; + texture._glTextures[this.renderer.CONTEXT_UID] = renderTarget.texture; + texture._glRenderTargets[this.renderer.CONTEXT_UID] = renderTarget; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -60,17 +60,17 @@ texture = texture.baseTexture || texture; var gl = this.gl; - if (texture._glRenderTargets[gl.id]) + if (texture._glRenderTargets[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id] = null; - texture._glRenderTargets[gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID] = null; + texture._glRenderTargets[this.renderer.CONTEXT_UID].destroy(); //.destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -88,9 +88,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/TextureManager.js b/src/core/renderers/webgl/TextureManager.js index a0f4492..eb78645 100644 --- a/src/core/renderers/webgl/TextureManager.js +++ b/src/core/renderers/webgl/TextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var TextureManager = function(gl) +var TextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer; + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -45,14 +46,13 @@ } - var gl = this.gl; - var glTexture = texture._glTextures[gl.id];//texture._glTextures[gl.id]; + var glTexture = texture._glTextures[this.renderer.CONTEXT_UID]; if (!glTexture) { - glTexture = new GLTexture(gl); + glTexture = new GLTexture(this.gl); glTexture.premultiplyAlpha = true; - texture._glTextures[gl.id] = glTexture; + texture._glTextures[this.renderer.CONTEXT_UID] = glTexture; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -93,14 +93,14 @@ return; } - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID].destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -118,9 +118,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 6a0efa7..638d56f 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -103,6 +103,7 @@ this.gl.id = this.CONTEXT_UID = CONTEXT_UID++; this.state = new WebGLState(this.gl); + this.renderingToScreen = true; /** * Manages the filters. @@ -148,11 +149,9 @@ { var gl = this.gl; - - // create a texture manager... - this.textureManager = new TextureManager(gl); - this.renderTextureManager = new RenderTextureManager(gl); + this.textureManager = new TextureManager(this); + this.renderTextureManager = new RenderTextureManager(this); this.state.resetToDefault(); @@ -174,6 +173,9 @@ */ WebGLRenderer.prototype.render = function (displayObject, renderTexture, clear, transform, skipUpdateTransform) { + // can be handy to know! + this.renderingToScreen = !renderTexture; + this.emit('prerender'); // no point rendering if our context has been blown up! @@ -271,12 +273,12 @@ { var baseTexture = renderTexture.baseTexture; - if(!baseTexture._glRenderTargets[gl.id]) + if(!baseTexture._glRenderTargets[this.CONTEXT_UID]) { this.renderTextureManager.updateTexture(baseTexture); } - renderTarget = baseTexture._glRenderTargets[gl.id]; + renderTarget = baseTexture._glRenderTargets[this.CONTEXT_UID]; renderTarget.setFrame(renderTexture.frame); } @@ -350,7 +352,7 @@ //TODO - can we cache this texture too? this._activeTexture = texture; - if (!texture._glTextures[gl.id]) + if (!texture._glTextures[this.CONTEXT_UID]) { // this will also bind the texture.. this.textureManager.updateTexture(texture); @@ -358,7 +360,7 @@ else { // bind the current texture - texture._glTextures[gl.id].bind(); + texture._glTextures[this.CONTEXT_UID].bind(); } return this; diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.frag b/src/core/renderers/webgl/filters/FXAA/FXAA.frag deleted file mode 100755 index 2f3b3ae..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.frag +++ /dev/null @@ -1,127 +0,0 @@ -precision lowp float; - - -/** -Basic FXAA implementation based on the code on geeks3d.com with the -modification that the texture2DLod stuff was removed since it's -unsupported by WebGL. - --- - -From: -https://github.com/mitsuhiko/webgl-meincraft - -Copyright (c) 2011 by Armin Ronacher. - -Some rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following - disclaimer in the documentation and/or other materials provided - with the distribution. - - * The names of the contributors may not be used to endorse or - promote products derived from this software without specific - prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef FXAA_REDUCE_MIN - #define FXAA_REDUCE_MIN (1.0/ 128.0) -#endif -#ifndef FXAA_REDUCE_MUL - #define FXAA_REDUCE_MUL (1.0 / 8.0) -#endif -#ifndef FXAA_SPAN_MAX - #define FXAA_SPAN_MAX 8.0 -#endif - -//optimized version for mobile, where dependent -//texture reads can be a bottleneck -vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution, - vec2 v_rgbNW, vec2 v_rgbNE, - vec2 v_rgbSW, vec2 v_rgbSE, - vec2 v_rgbM) { - vec4 color; - mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y); - vec3 rgbNW = texture2D(tex, v_rgbNW).xyz; - vec3 rgbNE = texture2D(tex, v_rgbNE).xyz; - vec3 rgbSW = texture2D(tex, v_rgbSW).xyz; - vec3 rgbSE = texture2D(tex, v_rgbSE).xyz; - vec4 texColor = texture2D(tex, v_rgbM); - vec3 rgbM = texColor.xyz; - vec3 luma = vec3(0.299, 0.587, 0.114); - float lumaNW = dot(rgbNW, luma); - float lumaNE = dot(rgbNE, luma); - float lumaSW = dot(rgbSW, luma); - float lumaSE = dot(rgbSE, luma); - float lumaM = dot(rgbM, luma); - float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); - float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); - - mediump vec2 dir; - dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); - dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); - - float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * - (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); - - float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); - dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), - max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), - dir * rcpDirMin)) * inverseVP; - - vec3 rgbA = 0.5 * ( - texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz + - texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz); - vec3 rgbB = rgbA * 0.5 + 0.25 * ( - texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz + - texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz); - - float lumaB = dot(rgbB, luma); - if ((lumaB < lumaMin) || (lumaB > lumaMax)) - color = vec4(rgbA, texColor.a); - else - color = vec4(rgbB, texColor.a); - return color; -} - - -varying vec2 vTextureCoord; -varying vec4 vColor; -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - -uniform sampler2D uSampler; - - -void main(void){ - - gl_FragColor = fxaa(uSampler, vTextureCoord * vResolution, vResolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); - -} diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.vert b/src/core/renderers/webgl/filters/FXAA/FXAA.vert deleted file mode 100755 index b0c1860..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.vert +++ /dev/null @@ -1,45 +0,0 @@ - -precision mediump float; - -attribute vec2 aVertexPosition; -attribute vec2 aTextureCoord; -attribute vec4 aColor; - -uniform mat3 projectionMatrix; -uniform vec2 resolution; - -varying vec2 vTextureCoord; -varying vec4 vColor; - -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - - -void texcoords(vec2 fragCoord, vec2 resolution, - out vec2 v_rgbNW, out vec2 v_rgbNE, - out vec2 v_rgbSW, out vec2 v_rgbSE, - out vec2 v_rgbM) { - vec2 inverseVP = 1.0 / resolution.xy; - v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP; - v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP; - v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP; - v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP; - v_rgbM = vec2(fragCoord * inverseVP); -} - -void main(void){ - gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0); - vTextureCoord = aTextureCoord; - vColor = vec4(aColor.rgb * aColor.a, aColor.a); - vResolution = resolution; - - //compute the texture coords and send them to varyings - texcoords(aTextureCoord * resolution, resolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); -} diff --git a/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js b/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js deleted file mode 100644 index 6d5646c..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js +++ /dev/null @@ -1,53 +0,0 @@ -var AbstractFilter = require('./AbstractFilter'); -// @see https://github.com/substack/brfs/issues/25 -var fs = require('fs'); - -/** - * - * Basic FXAA implementation based on the code on geeks3d.com with the - * modification that the texture2DLod stuff was removed since it's - * unsupported by WebGL. - * - * -- - * From: - * https://github.com/mitsuhiko/webgl-meincraft - * - * @class - * @extends PIXI.AbstractFilter - * @memberof PIXI - * - */ -function FXAAFilter() -{ - AbstractFilter.call(this, - // vertex shader - fs.readFileSync(__dirname + '/FXAA.vert', 'utf8'), - // fragment shader - fs.readFileSync(__dirname + '/FXAA.frag', 'utf8'), - // uniforms - { - resolution: { type: 'v2', value: { x: 1, y: 1 } } - } - ); - -} - -FXAAFilter.prototype = Object.create(AbstractFilter.prototype); -FXAAFilter.prototype.constructor = FXAAFilter; -module.exports = FXAAFilter; - -/** - * Applies the filter - * - * @param renderer {PIXI.WebGLRenderer} The renderer to retrieve the filter from - * @param input {PIXI.RenderTarget} - * @param output {PIXI.RenderTarget} - */ -FXAAFilter.prototype.applyFilter = function (renderer, input, output) -{ - var filterManager = renderer.filterManager; - - var shader = this.getShader( renderer ); - // draw the filter... - filterManager.applyFilter(shader, input, output); -}; diff --git a/src/core/renderers/webgl/managers/FilterManager.js b/src/core/renderers/webgl/managers/FilterManager.js index 5fc6562..4f9f142 100644 --- a/src/core/renderers/webgl/managers/FilterManager.js +++ b/src/core/renderers/webgl/managers/FilterManager.js @@ -137,7 +137,7 @@ { var renderer = this.renderer; var lastState = this.stack[this.stackIndex-1]; - var shader = filter.glShaders[gl.id]; + var shader = filter.glShaders[renderer.CONTEXT_UID]; // cacheing.. if(!shader) @@ -148,12 +148,12 @@ if(!shader) { - shader = filter.glShaders[gl.id] = this.shaderCache[filter.glShaderKey] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); + shader = filter.glShaders[renderer.CONTEXT_UID] = this.shaderCache[filter.glShaderKey] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); } } else { - shader = filter.glShaders[gl.id] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); + shader = filter.glShaders[renderer.CONTEXT_UID] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); } //TODO - this only needs to be done once? diff --git a/src/filters/FXAA/FXAA.frag b/src/filters/FXAA/FXAA.frag new file mode 100755 index 0000000..2f3b3ae --- /dev/null +++ b/src/filters/FXAA/FXAA.frag @@ -0,0 +1,127 @@ +precision lowp float; + + +/** +Basic FXAA implementation based on the code on geeks3d.com with the +modification that the texture2DLod stuff was removed since it's +unsupported by WebGL. + +-- + +From: +https://github.com/mitsuhiko/webgl-meincraft + +Copyright (c) 2011 by Armin Ronacher. + +Some rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * The names of the contributors may not be used to endorse or + promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef FXAA_REDUCE_MIN + #define FXAA_REDUCE_MIN (1.0/ 128.0) +#endif +#ifndef FXAA_REDUCE_MUL + #define FXAA_REDUCE_MUL (1.0 / 8.0) +#endif +#ifndef FXAA_SPAN_MAX + #define FXAA_SPAN_MAX 8.0 +#endif + +//optimized version for mobile, where dependent +//texture reads can be a bottleneck +vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution, + vec2 v_rgbNW, vec2 v_rgbNE, + vec2 v_rgbSW, vec2 v_rgbSE, + vec2 v_rgbM) { + vec4 color; + mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y); + vec3 rgbNW = texture2D(tex, v_rgbNW).xyz; + vec3 rgbNE = texture2D(tex, v_rgbNE).xyz; + vec3 rgbSW = texture2D(tex, v_rgbSW).xyz; + vec3 rgbSE = texture2D(tex, v_rgbSE).xyz; + vec4 texColor = texture2D(tex, v_rgbM); + vec3 rgbM = texColor.xyz; + vec3 luma = vec3(0.299, 0.587, 0.114); + float lumaNW = dot(rgbNW, luma); + float lumaNE = dot(rgbNE, luma); + float lumaSW = dot(rgbSW, luma); + float lumaSE = dot(rgbSE, luma); + float lumaM = dot(rgbM, luma); + float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); + float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); + + mediump vec2 dir; + dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); + dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); + + float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * + (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); + + float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); + dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), + max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), + dir * rcpDirMin)) * inverseVP; + + vec3 rgbA = 0.5 * ( + texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz + + texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz); + vec3 rgbB = rgbA * 0.5 + 0.25 * ( + texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz + + texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz); + + float lumaB = dot(rgbB, luma); + if ((lumaB < lumaMin) || (lumaB > lumaMax)) + color = vec4(rgbA, texColor.a); + else + color = vec4(rgbB, texColor.a); + return color; +} + + +varying vec2 vTextureCoord; +varying vec4 vColor; +varying vec2 vResolution; + +//texcoords computed in vertex step +//to avoid dependent texture reads +varying vec2 v_rgbNW; +varying vec2 v_rgbNE; +varying vec2 v_rgbSW; +varying vec2 v_rgbSE; +varying vec2 v_rgbM; + +uniform sampler2D uSampler; + + +void main(void){ + + gl_FragColor = fxaa(uSampler, vTextureCoord * vResolution, vResolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); + +} diff --git a/src/filters/FXAA/FXAA.vert b/src/filters/FXAA/FXAA.vert new file mode 100755 index 0000000..b0c1860 --- /dev/null +++ b/src/filters/FXAA/FXAA.vert @@ -0,0 +1,45 @@ + +precision mediump float; + +attribute vec2 aVertexPosition; +attribute vec2 aTextureCoord; +attribute vec4 aColor; + +uniform mat3 projectionMatrix; +uniform vec2 resolution; + +varying vec2 vTextureCoord; +varying vec4 vColor; + +varying vec2 vResolution; + +//texcoords computed in vertex step +//to avoid dependent texture reads +varying vec2 v_rgbNW; +varying vec2 v_rgbNE; +varying vec2 v_rgbSW; +varying vec2 v_rgbSE; +varying vec2 v_rgbM; + + +void texcoords(vec2 fragCoord, vec2 resolution, + out vec2 v_rgbNW, out vec2 v_rgbNE, + out vec2 v_rgbSW, out vec2 v_rgbSE, + out vec2 v_rgbM) { + vec2 inverseVP = 1.0 / resolution.xy; + v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP; + v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP; + v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP; + v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP; + v_rgbM = vec2(fragCoord * inverseVP); +} + +void main(void){ + gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0); + vTextureCoord = aTextureCoord; + vColor = vec4(aColor.rgb * aColor.a, aColor.a); + vResolution = resolution; + + //compute the texture coords and send them to varyings + texcoords(aTextureCoord * resolution, resolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); +} diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index b4b02a0..1e8115b 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -177,6 +177,7 @@ */ AccessibilityManager.prototype.update = function() { + if(!this.renderer.renderingToScreen)return; // update children... this.updateAccessibleObjects(this.renderer._lastObjectRendered); diff --git a/src/core/graphics/webgl/GraphicsRenderer.js b/src/core/graphics/webgl/GraphicsRenderer.js index 282ba87..8253f1a 100644 --- a/src/core/graphics/webgl/GraphicsRenderer.js +++ b/src/core/graphics/webgl/GraphicsRenderer.js @@ -34,6 +34,9 @@ this.primitiveShader = null; this.gl = renderer.gl; + + // easy access! + this.CONTEXT_UID = 0; } GraphicsRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -51,6 +54,7 @@ GraphicsRenderer.prototype.onContextChange = function() { this.gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; this.primitiveShader = new PrimitiveShader(this.gl) }; @@ -81,12 +85,12 @@ var webGLData; - if (graphics.dirty || !graphics._webGL[gl.id]) + if (graphics.dirty || !graphics._webGL[this.CONTEXT_UID]) { this.updateGraphics(graphics); } - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // This could be speeded up for sure! var shader = this.primitiveShader; @@ -120,12 +124,12 @@ var gl = this.renderer.gl; // get the contexts graphics object - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // if the graphics object does not exist in the webGL context time to create it! if (!webGL) { - webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + webGL = graphics._webGL[this.CONTEXT_UID] = {lastIndex:0, data:[], gl:gl}; } diff --git a/src/core/particles/webgl/ParticleRenderer.js b/src/core/particles/webgl/ParticleRenderer.js index 0b0abd0..6e7b9c0 100644 --- a/src/core/particles/webgl/ParticleRenderer.js +++ b/src/core/particles/webgl/ParticleRenderer.js @@ -61,6 +61,8 @@ this.properties = null; this.tempMatrix = new math.Matrix(); + + this.CONTEXT_UID = 0; } ParticleRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -79,6 +81,8 @@ return; var gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; + // setup default shader this.shader = new ParticleShader(this.renderer.shaderManager); @@ -190,7 +194,7 @@ // make sure the texture is bound.. var baseTexture = children[0]._texture.baseTexture; - if (!baseTexture._glTextures[gl.id]) + if (!baseTexture._glTextures[this.CONTEXT_UID]) { // if the texture has not updated then lets not upload any static properties if(!this.renderer.updateTexture(baseTexture)) @@ -205,7 +209,7 @@ } else { - gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[gl.id]); + gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[this.CONTEXT_UID]); } // now lets upload and render the buffers.. diff --git a/src/core/renderers/webgl/RenderTextureManager.js b/src/core/renderers/webgl/RenderTextureManager.js index cbbbd4c..e922209 100644 --- a/src/core/renderers/webgl/RenderTextureManager.js +++ b/src/core/renderers/webgl/RenderTextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var RenderTextureManager = function(gl) +var RenderTextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -27,16 +28,15 @@ { texture = texture.baseTexture || texture; - var gl = this.gl; - var renderTarget = texture._glRenderTargets[gl.id]; + var renderTarget = texture._glRenderTargets[this.renderer.CONTEXT_UID]; if (!renderTarget) { - renderTarget = new RenderTarget(gl, texture.width, texture.height); + renderTarget = new RenderTarget(this.gl, texture.width, texture.height, texture.scaleMode, texture.resolution); - texture._glTextures[gl.id] = renderTarget.texture; - texture._glRenderTargets[gl.id] = renderTarget; + texture._glTextures[this.renderer.CONTEXT_UID] = renderTarget.texture; + texture._glRenderTargets[this.renderer.CONTEXT_UID] = renderTarget; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -60,17 +60,17 @@ texture = texture.baseTexture || texture; var gl = this.gl; - if (texture._glRenderTargets[gl.id]) + if (texture._glRenderTargets[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id] = null; - texture._glRenderTargets[gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID] = null; + texture._glRenderTargets[this.renderer.CONTEXT_UID].destroy(); //.destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -88,9 +88,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/TextureManager.js b/src/core/renderers/webgl/TextureManager.js index a0f4492..eb78645 100644 --- a/src/core/renderers/webgl/TextureManager.js +++ b/src/core/renderers/webgl/TextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var TextureManager = function(gl) +var TextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer; + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -45,14 +46,13 @@ } - var gl = this.gl; - var glTexture = texture._glTextures[gl.id];//texture._glTextures[gl.id]; + var glTexture = texture._glTextures[this.renderer.CONTEXT_UID]; if (!glTexture) { - glTexture = new GLTexture(gl); + glTexture = new GLTexture(this.gl); glTexture.premultiplyAlpha = true; - texture._glTextures[gl.id] = glTexture; + texture._glTextures[this.renderer.CONTEXT_UID] = glTexture; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -93,14 +93,14 @@ return; } - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID].destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -118,9 +118,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 6a0efa7..638d56f 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -103,6 +103,7 @@ this.gl.id = this.CONTEXT_UID = CONTEXT_UID++; this.state = new WebGLState(this.gl); + this.renderingToScreen = true; /** * Manages the filters. @@ -148,11 +149,9 @@ { var gl = this.gl; - - // create a texture manager... - this.textureManager = new TextureManager(gl); - this.renderTextureManager = new RenderTextureManager(gl); + this.textureManager = new TextureManager(this); + this.renderTextureManager = new RenderTextureManager(this); this.state.resetToDefault(); @@ -174,6 +173,9 @@ */ WebGLRenderer.prototype.render = function (displayObject, renderTexture, clear, transform, skipUpdateTransform) { + // can be handy to know! + this.renderingToScreen = !renderTexture; + this.emit('prerender'); // no point rendering if our context has been blown up! @@ -271,12 +273,12 @@ { var baseTexture = renderTexture.baseTexture; - if(!baseTexture._glRenderTargets[gl.id]) + if(!baseTexture._glRenderTargets[this.CONTEXT_UID]) { this.renderTextureManager.updateTexture(baseTexture); } - renderTarget = baseTexture._glRenderTargets[gl.id]; + renderTarget = baseTexture._glRenderTargets[this.CONTEXT_UID]; renderTarget.setFrame(renderTexture.frame); } @@ -350,7 +352,7 @@ //TODO - can we cache this texture too? this._activeTexture = texture; - if (!texture._glTextures[gl.id]) + if (!texture._glTextures[this.CONTEXT_UID]) { // this will also bind the texture.. this.textureManager.updateTexture(texture); @@ -358,7 +360,7 @@ else { // bind the current texture - texture._glTextures[gl.id].bind(); + texture._glTextures[this.CONTEXT_UID].bind(); } return this; diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.frag b/src/core/renderers/webgl/filters/FXAA/FXAA.frag deleted file mode 100755 index 2f3b3ae..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.frag +++ /dev/null @@ -1,127 +0,0 @@ -precision lowp float; - - -/** -Basic FXAA implementation based on the code on geeks3d.com with the -modification that the texture2DLod stuff was removed since it's -unsupported by WebGL. - --- - -From: -https://github.com/mitsuhiko/webgl-meincraft - -Copyright (c) 2011 by Armin Ronacher. - -Some rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following - disclaimer in the documentation and/or other materials provided - with the distribution. - - * The names of the contributors may not be used to endorse or - promote products derived from this software without specific - prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef FXAA_REDUCE_MIN - #define FXAA_REDUCE_MIN (1.0/ 128.0) -#endif -#ifndef FXAA_REDUCE_MUL - #define FXAA_REDUCE_MUL (1.0 / 8.0) -#endif -#ifndef FXAA_SPAN_MAX - #define FXAA_SPAN_MAX 8.0 -#endif - -//optimized version for mobile, where dependent -//texture reads can be a bottleneck -vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution, - vec2 v_rgbNW, vec2 v_rgbNE, - vec2 v_rgbSW, vec2 v_rgbSE, - vec2 v_rgbM) { - vec4 color; - mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y); - vec3 rgbNW = texture2D(tex, v_rgbNW).xyz; - vec3 rgbNE = texture2D(tex, v_rgbNE).xyz; - vec3 rgbSW = texture2D(tex, v_rgbSW).xyz; - vec3 rgbSE = texture2D(tex, v_rgbSE).xyz; - vec4 texColor = texture2D(tex, v_rgbM); - vec3 rgbM = texColor.xyz; - vec3 luma = vec3(0.299, 0.587, 0.114); - float lumaNW = dot(rgbNW, luma); - float lumaNE = dot(rgbNE, luma); - float lumaSW = dot(rgbSW, luma); - float lumaSE = dot(rgbSE, luma); - float lumaM = dot(rgbM, luma); - float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); - float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); - - mediump vec2 dir; - dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); - dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); - - float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * - (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); - - float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); - dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), - max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), - dir * rcpDirMin)) * inverseVP; - - vec3 rgbA = 0.5 * ( - texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz + - texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz); - vec3 rgbB = rgbA * 0.5 + 0.25 * ( - texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz + - texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz); - - float lumaB = dot(rgbB, luma); - if ((lumaB < lumaMin) || (lumaB > lumaMax)) - color = vec4(rgbA, texColor.a); - else - color = vec4(rgbB, texColor.a); - return color; -} - - -varying vec2 vTextureCoord; -varying vec4 vColor; -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - -uniform sampler2D uSampler; - - -void main(void){ - - gl_FragColor = fxaa(uSampler, vTextureCoord * vResolution, vResolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); - -} diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.vert b/src/core/renderers/webgl/filters/FXAA/FXAA.vert deleted file mode 100755 index b0c1860..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.vert +++ /dev/null @@ -1,45 +0,0 @@ - -precision mediump float; - -attribute vec2 aVertexPosition; -attribute vec2 aTextureCoord; -attribute vec4 aColor; - -uniform mat3 projectionMatrix; -uniform vec2 resolution; - -varying vec2 vTextureCoord; -varying vec4 vColor; - -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - - -void texcoords(vec2 fragCoord, vec2 resolution, - out vec2 v_rgbNW, out vec2 v_rgbNE, - out vec2 v_rgbSW, out vec2 v_rgbSE, - out vec2 v_rgbM) { - vec2 inverseVP = 1.0 / resolution.xy; - v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP; - v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP; - v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP; - v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP; - v_rgbM = vec2(fragCoord * inverseVP); -} - -void main(void){ - gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0); - vTextureCoord = aTextureCoord; - vColor = vec4(aColor.rgb * aColor.a, aColor.a); - vResolution = resolution; - - //compute the texture coords and send them to varyings - texcoords(aTextureCoord * resolution, resolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); -} diff --git a/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js b/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js deleted file mode 100644 index 6d5646c..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js +++ /dev/null @@ -1,53 +0,0 @@ -var AbstractFilter = require('./AbstractFilter'); -// @see https://github.com/substack/brfs/issues/25 -var fs = require('fs'); - -/** - * - * Basic FXAA implementation based on the code on geeks3d.com with the - * modification that the texture2DLod stuff was removed since it's - * unsupported by WebGL. - * - * -- - * From: - * https://github.com/mitsuhiko/webgl-meincraft - * - * @class - * @extends PIXI.AbstractFilter - * @memberof PIXI - * - */ -function FXAAFilter() -{ - AbstractFilter.call(this, - // vertex shader - fs.readFileSync(__dirname + '/FXAA.vert', 'utf8'), - // fragment shader - fs.readFileSync(__dirname + '/FXAA.frag', 'utf8'), - // uniforms - { - resolution: { type: 'v2', value: { x: 1, y: 1 } } - } - ); - -} - -FXAAFilter.prototype = Object.create(AbstractFilter.prototype); -FXAAFilter.prototype.constructor = FXAAFilter; -module.exports = FXAAFilter; - -/** - * Applies the filter - * - * @param renderer {PIXI.WebGLRenderer} The renderer to retrieve the filter from - * @param input {PIXI.RenderTarget} - * @param output {PIXI.RenderTarget} - */ -FXAAFilter.prototype.applyFilter = function (renderer, input, output) -{ - var filterManager = renderer.filterManager; - - var shader = this.getShader( renderer ); - // draw the filter... - filterManager.applyFilter(shader, input, output); -}; diff --git a/src/core/renderers/webgl/managers/FilterManager.js b/src/core/renderers/webgl/managers/FilterManager.js index 5fc6562..4f9f142 100644 --- a/src/core/renderers/webgl/managers/FilterManager.js +++ b/src/core/renderers/webgl/managers/FilterManager.js @@ -137,7 +137,7 @@ { var renderer = this.renderer; var lastState = this.stack[this.stackIndex-1]; - var shader = filter.glShaders[gl.id]; + var shader = filter.glShaders[renderer.CONTEXT_UID]; // cacheing.. if(!shader) @@ -148,12 +148,12 @@ if(!shader) { - shader = filter.glShaders[gl.id] = this.shaderCache[filter.glShaderKey] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); + shader = filter.glShaders[renderer.CONTEXT_UID] = this.shaderCache[filter.glShaderKey] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); } } else { - shader = filter.glShaders[gl.id] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); + shader = filter.glShaders[renderer.CONTEXT_UID] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); } //TODO - this only needs to be done once? diff --git a/src/filters/FXAA/FXAA.frag b/src/filters/FXAA/FXAA.frag new file mode 100755 index 0000000..2f3b3ae --- /dev/null +++ b/src/filters/FXAA/FXAA.frag @@ -0,0 +1,127 @@ +precision lowp float; + + +/** +Basic FXAA implementation based on the code on geeks3d.com with the +modification that the texture2DLod stuff was removed since it's +unsupported by WebGL. + +-- + +From: +https://github.com/mitsuhiko/webgl-meincraft + +Copyright (c) 2011 by Armin Ronacher. + +Some rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * The names of the contributors may not be used to endorse or + promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef FXAA_REDUCE_MIN + #define FXAA_REDUCE_MIN (1.0/ 128.0) +#endif +#ifndef FXAA_REDUCE_MUL + #define FXAA_REDUCE_MUL (1.0 / 8.0) +#endif +#ifndef FXAA_SPAN_MAX + #define FXAA_SPAN_MAX 8.0 +#endif + +//optimized version for mobile, where dependent +//texture reads can be a bottleneck +vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution, + vec2 v_rgbNW, vec2 v_rgbNE, + vec2 v_rgbSW, vec2 v_rgbSE, + vec2 v_rgbM) { + vec4 color; + mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y); + vec3 rgbNW = texture2D(tex, v_rgbNW).xyz; + vec3 rgbNE = texture2D(tex, v_rgbNE).xyz; + vec3 rgbSW = texture2D(tex, v_rgbSW).xyz; + vec3 rgbSE = texture2D(tex, v_rgbSE).xyz; + vec4 texColor = texture2D(tex, v_rgbM); + vec3 rgbM = texColor.xyz; + vec3 luma = vec3(0.299, 0.587, 0.114); + float lumaNW = dot(rgbNW, luma); + float lumaNE = dot(rgbNE, luma); + float lumaSW = dot(rgbSW, luma); + float lumaSE = dot(rgbSE, luma); + float lumaM = dot(rgbM, luma); + float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); + float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); + + mediump vec2 dir; + dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); + dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); + + float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * + (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); + + float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); + dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), + max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), + dir * rcpDirMin)) * inverseVP; + + vec3 rgbA = 0.5 * ( + texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz + + texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz); + vec3 rgbB = rgbA * 0.5 + 0.25 * ( + texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz + + texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz); + + float lumaB = dot(rgbB, luma); + if ((lumaB < lumaMin) || (lumaB > lumaMax)) + color = vec4(rgbA, texColor.a); + else + color = vec4(rgbB, texColor.a); + return color; +} + + +varying vec2 vTextureCoord; +varying vec4 vColor; +varying vec2 vResolution; + +//texcoords computed in vertex step +//to avoid dependent texture reads +varying vec2 v_rgbNW; +varying vec2 v_rgbNE; +varying vec2 v_rgbSW; +varying vec2 v_rgbSE; +varying vec2 v_rgbM; + +uniform sampler2D uSampler; + + +void main(void){ + + gl_FragColor = fxaa(uSampler, vTextureCoord * vResolution, vResolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); + +} diff --git a/src/filters/FXAA/FXAA.vert b/src/filters/FXAA/FXAA.vert new file mode 100755 index 0000000..b0c1860 --- /dev/null +++ b/src/filters/FXAA/FXAA.vert @@ -0,0 +1,45 @@ + +precision mediump float; + +attribute vec2 aVertexPosition; +attribute vec2 aTextureCoord; +attribute vec4 aColor; + +uniform mat3 projectionMatrix; +uniform vec2 resolution; + +varying vec2 vTextureCoord; +varying vec4 vColor; + +varying vec2 vResolution; + +//texcoords computed in vertex step +//to avoid dependent texture reads +varying vec2 v_rgbNW; +varying vec2 v_rgbNE; +varying vec2 v_rgbSW; +varying vec2 v_rgbSE; +varying vec2 v_rgbM; + + +void texcoords(vec2 fragCoord, vec2 resolution, + out vec2 v_rgbNW, out vec2 v_rgbNE, + out vec2 v_rgbSW, out vec2 v_rgbSE, + out vec2 v_rgbM) { + vec2 inverseVP = 1.0 / resolution.xy; + v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP; + v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP; + v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP; + v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP; + v_rgbM = vec2(fragCoord * inverseVP); +} + +void main(void){ + gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0); + vTextureCoord = aTextureCoord; + vColor = vec4(aColor.rgb * aColor.a, aColor.a); + vResolution = resolution; + + //compute the texture coords and send them to varyings + texcoords(aTextureCoord * resolution, resolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); +} diff --git a/src/filters/FXAA/FXAAFilter.js b/src/filters/FXAA/FXAAFilter.js new file mode 100644 index 0000000..6d5646c --- /dev/null +++ b/src/filters/FXAA/FXAAFilter.js @@ -0,0 +1,53 @@ +var AbstractFilter = require('./AbstractFilter'); +// @see https://github.com/substack/brfs/issues/25 +var fs = require('fs'); + +/** + * + * Basic FXAA implementation based on the code on geeks3d.com with the + * modification that the texture2DLod stuff was removed since it's + * unsupported by WebGL. + * + * -- + * From: + * https://github.com/mitsuhiko/webgl-meincraft + * + * @class + * @extends PIXI.AbstractFilter + * @memberof PIXI + * + */ +function FXAAFilter() +{ + AbstractFilter.call(this, + // vertex shader + fs.readFileSync(__dirname + '/FXAA.vert', 'utf8'), + // fragment shader + fs.readFileSync(__dirname + '/FXAA.frag', 'utf8'), + // uniforms + { + resolution: { type: 'v2', value: { x: 1, y: 1 } } + } + ); + +} + +FXAAFilter.prototype = Object.create(AbstractFilter.prototype); +FXAAFilter.prototype.constructor = FXAAFilter; +module.exports = FXAAFilter; + +/** + * Applies the filter + * + * @param renderer {PIXI.WebGLRenderer} The renderer to retrieve the filter from + * @param input {PIXI.RenderTarget} + * @param output {PIXI.RenderTarget} + */ +FXAAFilter.prototype.applyFilter = function (renderer, input, output) +{ + var filterManager = renderer.filterManager; + + var shader = this.getShader( renderer ); + // draw the filter... + filterManager.applyFilter(shader, input, output); +}; diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index b4b02a0..1e8115b 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -177,6 +177,7 @@ */ AccessibilityManager.prototype.update = function() { + if(!this.renderer.renderingToScreen)return; // update children... this.updateAccessibleObjects(this.renderer._lastObjectRendered); diff --git a/src/core/graphics/webgl/GraphicsRenderer.js b/src/core/graphics/webgl/GraphicsRenderer.js index 282ba87..8253f1a 100644 --- a/src/core/graphics/webgl/GraphicsRenderer.js +++ b/src/core/graphics/webgl/GraphicsRenderer.js @@ -34,6 +34,9 @@ this.primitiveShader = null; this.gl = renderer.gl; + + // easy access! + this.CONTEXT_UID = 0; } GraphicsRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -51,6 +54,7 @@ GraphicsRenderer.prototype.onContextChange = function() { this.gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; this.primitiveShader = new PrimitiveShader(this.gl) }; @@ -81,12 +85,12 @@ var webGLData; - if (graphics.dirty || !graphics._webGL[gl.id]) + if (graphics.dirty || !graphics._webGL[this.CONTEXT_UID]) { this.updateGraphics(graphics); } - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // This could be speeded up for sure! var shader = this.primitiveShader; @@ -120,12 +124,12 @@ var gl = this.renderer.gl; // get the contexts graphics object - var webGL = graphics._webGL[gl.id]; + var webGL = graphics._webGL[this.CONTEXT_UID]; // if the graphics object does not exist in the webGL context time to create it! if (!webGL) { - webGL = graphics._webGL[gl.id] = {lastIndex:0, data:[], gl:gl}; + webGL = graphics._webGL[this.CONTEXT_UID] = {lastIndex:0, data:[], gl:gl}; } diff --git a/src/core/particles/webgl/ParticleRenderer.js b/src/core/particles/webgl/ParticleRenderer.js index 0b0abd0..6e7b9c0 100644 --- a/src/core/particles/webgl/ParticleRenderer.js +++ b/src/core/particles/webgl/ParticleRenderer.js @@ -61,6 +61,8 @@ this.properties = null; this.tempMatrix = new math.Matrix(); + + this.CONTEXT_UID = 0; } ParticleRenderer.prototype = Object.create(ObjectRenderer.prototype); @@ -79,6 +81,8 @@ return; var gl = this.renderer.gl; + this.CONTEXT_UID = this.renderer.CONTEXT_UID; + // setup default shader this.shader = new ParticleShader(this.renderer.shaderManager); @@ -190,7 +194,7 @@ // make sure the texture is bound.. var baseTexture = children[0]._texture.baseTexture; - if (!baseTexture._glTextures[gl.id]) + if (!baseTexture._glTextures[this.CONTEXT_UID]) { // if the texture has not updated then lets not upload any static properties if(!this.renderer.updateTexture(baseTexture)) @@ -205,7 +209,7 @@ } else { - gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[gl.id]); + gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[this.CONTEXT_UID]); } // now lets upload and render the buffers.. diff --git a/src/core/renderers/webgl/RenderTextureManager.js b/src/core/renderers/webgl/RenderTextureManager.js index cbbbd4c..e922209 100644 --- a/src/core/renderers/webgl/RenderTextureManager.js +++ b/src/core/renderers/webgl/RenderTextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var RenderTextureManager = function(gl) +var RenderTextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -27,16 +28,15 @@ { texture = texture.baseTexture || texture; - var gl = this.gl; - var renderTarget = texture._glRenderTargets[gl.id]; + var renderTarget = texture._glRenderTargets[this.renderer.CONTEXT_UID]; if (!renderTarget) { - renderTarget = new RenderTarget(gl, texture.width, texture.height); + renderTarget = new RenderTarget(this.gl, texture.width, texture.height, texture.scaleMode, texture.resolution); - texture._glTextures[gl.id] = renderTarget.texture; - texture._glRenderTargets[gl.id] = renderTarget; + texture._glTextures[this.renderer.CONTEXT_UID] = renderTarget.texture; + texture._glRenderTargets[this.renderer.CONTEXT_UID] = renderTarget; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -60,17 +60,17 @@ texture = texture.baseTexture || texture; var gl = this.gl; - if (texture._glRenderTargets[gl.id]) + if (texture._glRenderTargets[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id] = null; - texture._glRenderTargets[gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID] = null; + texture._glRenderTargets[this.renderer.CONTEXT_UID].destroy(); //.destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -88,9 +88,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/TextureManager.js b/src/core/renderers/webgl/TextureManager.js index a0f4492..eb78645 100644 --- a/src/core/renderers/webgl/TextureManager.js +++ b/src/core/renderers/webgl/TextureManager.js @@ -10,9 +10,10 @@ * @param gl {WebGLRenderingContext} */ -var TextureManager = function(gl) +var TextureManager = function(renderer) { - this.gl = gl; + this.renderer = renderer; + this.gl = renderer.gl; // track textures in the renderer so we can no longer listen to them on destruction. this._managedTextures = []; @@ -45,14 +46,13 @@ } - var gl = this.gl; - var glTexture = texture._glTextures[gl.id];//texture._glTextures[gl.id]; + var glTexture = texture._glTextures[this.renderer.CONTEXT_UID]; if (!glTexture) { - glTexture = new GLTexture(gl); + glTexture = new GLTexture(this.gl); glTexture.premultiplyAlpha = true; - texture._glTextures[gl.id] = glTexture; + texture._glTextures[this.renderer.CONTEXT_UID] = glTexture; texture.on('update', this.updateTexture, this); texture.on('dispose', this.destroyTexture, this); @@ -93,14 +93,14 @@ return; } - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - texture._glTextures[this.gl.id].destroy(); + texture._glTextures[this.renderer.CONTEXT_UID].destroy(); texture.off('update', this.updateTexture, this); texture.off('dispose', this.destroyTexture, this); - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; if (!_skipRemove) { @@ -118,9 +118,9 @@ for (var i = 0; i < this._managedTextures.length; ++i) { var texture = this._managedTextures[i]; - if (texture._glTextures[this.gl.id]) + if (texture._glTextures[this.renderer.CONTEXT_UID]) { - delete texture._glTextures[this.gl.id]; + delete texture._glTextures[this.renderer.CONTEXT_UID]; } } } diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 6a0efa7..638d56f 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -103,6 +103,7 @@ this.gl.id = this.CONTEXT_UID = CONTEXT_UID++; this.state = new WebGLState(this.gl); + this.renderingToScreen = true; /** * Manages the filters. @@ -148,11 +149,9 @@ { var gl = this.gl; - - // create a texture manager... - this.textureManager = new TextureManager(gl); - this.renderTextureManager = new RenderTextureManager(gl); + this.textureManager = new TextureManager(this); + this.renderTextureManager = new RenderTextureManager(this); this.state.resetToDefault(); @@ -174,6 +173,9 @@ */ WebGLRenderer.prototype.render = function (displayObject, renderTexture, clear, transform, skipUpdateTransform) { + // can be handy to know! + this.renderingToScreen = !renderTexture; + this.emit('prerender'); // no point rendering if our context has been blown up! @@ -271,12 +273,12 @@ { var baseTexture = renderTexture.baseTexture; - if(!baseTexture._glRenderTargets[gl.id]) + if(!baseTexture._glRenderTargets[this.CONTEXT_UID]) { this.renderTextureManager.updateTexture(baseTexture); } - renderTarget = baseTexture._glRenderTargets[gl.id]; + renderTarget = baseTexture._glRenderTargets[this.CONTEXT_UID]; renderTarget.setFrame(renderTexture.frame); } @@ -350,7 +352,7 @@ //TODO - can we cache this texture too? this._activeTexture = texture; - if (!texture._glTextures[gl.id]) + if (!texture._glTextures[this.CONTEXT_UID]) { // this will also bind the texture.. this.textureManager.updateTexture(texture); @@ -358,7 +360,7 @@ else { // bind the current texture - texture._glTextures[gl.id].bind(); + texture._glTextures[this.CONTEXT_UID].bind(); } return this; diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.frag b/src/core/renderers/webgl/filters/FXAA/FXAA.frag deleted file mode 100755 index 2f3b3ae..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.frag +++ /dev/null @@ -1,127 +0,0 @@ -precision lowp float; - - -/** -Basic FXAA implementation based on the code on geeks3d.com with the -modification that the texture2DLod stuff was removed since it's -unsupported by WebGL. - --- - -From: -https://github.com/mitsuhiko/webgl-meincraft - -Copyright (c) 2011 by Armin Ronacher. - -Some rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following - disclaimer in the documentation and/or other materials provided - with the distribution. - - * The names of the contributors may not be used to endorse or - promote products derived from this software without specific - prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef FXAA_REDUCE_MIN - #define FXAA_REDUCE_MIN (1.0/ 128.0) -#endif -#ifndef FXAA_REDUCE_MUL - #define FXAA_REDUCE_MUL (1.0 / 8.0) -#endif -#ifndef FXAA_SPAN_MAX - #define FXAA_SPAN_MAX 8.0 -#endif - -//optimized version for mobile, where dependent -//texture reads can be a bottleneck -vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution, - vec2 v_rgbNW, vec2 v_rgbNE, - vec2 v_rgbSW, vec2 v_rgbSE, - vec2 v_rgbM) { - vec4 color; - mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y); - vec3 rgbNW = texture2D(tex, v_rgbNW).xyz; - vec3 rgbNE = texture2D(tex, v_rgbNE).xyz; - vec3 rgbSW = texture2D(tex, v_rgbSW).xyz; - vec3 rgbSE = texture2D(tex, v_rgbSE).xyz; - vec4 texColor = texture2D(tex, v_rgbM); - vec3 rgbM = texColor.xyz; - vec3 luma = vec3(0.299, 0.587, 0.114); - float lumaNW = dot(rgbNW, luma); - float lumaNE = dot(rgbNE, luma); - float lumaSW = dot(rgbSW, luma); - float lumaSE = dot(rgbSE, luma); - float lumaM = dot(rgbM, luma); - float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); - float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); - - mediump vec2 dir; - dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); - dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); - - float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * - (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); - - float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); - dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), - max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), - dir * rcpDirMin)) * inverseVP; - - vec3 rgbA = 0.5 * ( - texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz + - texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz); - vec3 rgbB = rgbA * 0.5 + 0.25 * ( - texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz + - texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz); - - float lumaB = dot(rgbB, luma); - if ((lumaB < lumaMin) || (lumaB > lumaMax)) - color = vec4(rgbA, texColor.a); - else - color = vec4(rgbB, texColor.a); - return color; -} - - -varying vec2 vTextureCoord; -varying vec4 vColor; -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - -uniform sampler2D uSampler; - - -void main(void){ - - gl_FragColor = fxaa(uSampler, vTextureCoord * vResolution, vResolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); - -} diff --git a/src/core/renderers/webgl/filters/FXAA/FXAA.vert b/src/core/renderers/webgl/filters/FXAA/FXAA.vert deleted file mode 100755 index b0c1860..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAA.vert +++ /dev/null @@ -1,45 +0,0 @@ - -precision mediump float; - -attribute vec2 aVertexPosition; -attribute vec2 aTextureCoord; -attribute vec4 aColor; - -uniform mat3 projectionMatrix; -uniform vec2 resolution; - -varying vec2 vTextureCoord; -varying vec4 vColor; - -varying vec2 vResolution; - -//texcoords computed in vertex step -//to avoid dependent texture reads -varying vec2 v_rgbNW; -varying vec2 v_rgbNE; -varying vec2 v_rgbSW; -varying vec2 v_rgbSE; -varying vec2 v_rgbM; - - -void texcoords(vec2 fragCoord, vec2 resolution, - out vec2 v_rgbNW, out vec2 v_rgbNE, - out vec2 v_rgbSW, out vec2 v_rgbSE, - out vec2 v_rgbM) { - vec2 inverseVP = 1.0 / resolution.xy; - v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP; - v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP; - v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP; - v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP; - v_rgbM = vec2(fragCoord * inverseVP); -} - -void main(void){ - gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0); - vTextureCoord = aTextureCoord; - vColor = vec4(aColor.rgb * aColor.a, aColor.a); - vResolution = resolution; - - //compute the texture coords and send them to varyings - texcoords(aTextureCoord * resolution, resolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); -} diff --git a/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js b/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js deleted file mode 100644 index 6d5646c..0000000 --- a/src/core/renderers/webgl/filters/FXAA/FXAAFilter.js +++ /dev/null @@ -1,53 +0,0 @@ -var AbstractFilter = require('./AbstractFilter'); -// @see https://github.com/substack/brfs/issues/25 -var fs = require('fs'); - -/** - * - * Basic FXAA implementation based on the code on geeks3d.com with the - * modification that the texture2DLod stuff was removed since it's - * unsupported by WebGL. - * - * -- - * From: - * https://github.com/mitsuhiko/webgl-meincraft - * - * @class - * @extends PIXI.AbstractFilter - * @memberof PIXI - * - */ -function FXAAFilter() -{ - AbstractFilter.call(this, - // vertex shader - fs.readFileSync(__dirname + '/FXAA.vert', 'utf8'), - // fragment shader - fs.readFileSync(__dirname + '/FXAA.frag', 'utf8'), - // uniforms - { - resolution: { type: 'v2', value: { x: 1, y: 1 } } - } - ); - -} - -FXAAFilter.prototype = Object.create(AbstractFilter.prototype); -FXAAFilter.prototype.constructor = FXAAFilter; -module.exports = FXAAFilter; - -/** - * Applies the filter - * - * @param renderer {PIXI.WebGLRenderer} The renderer to retrieve the filter from - * @param input {PIXI.RenderTarget} - * @param output {PIXI.RenderTarget} - */ -FXAAFilter.prototype.applyFilter = function (renderer, input, output) -{ - var filterManager = renderer.filterManager; - - var shader = this.getShader( renderer ); - // draw the filter... - filterManager.applyFilter(shader, input, output); -}; diff --git a/src/core/renderers/webgl/managers/FilterManager.js b/src/core/renderers/webgl/managers/FilterManager.js index 5fc6562..4f9f142 100644 --- a/src/core/renderers/webgl/managers/FilterManager.js +++ b/src/core/renderers/webgl/managers/FilterManager.js @@ -137,7 +137,7 @@ { var renderer = this.renderer; var lastState = this.stack[this.stackIndex-1]; - var shader = filter.glShaders[gl.id]; + var shader = filter.glShaders[renderer.CONTEXT_UID]; // cacheing.. if(!shader) @@ -148,12 +148,12 @@ if(!shader) { - shader = filter.glShaders[gl.id] = this.shaderCache[filter.glShaderKey] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); + shader = filter.glShaders[renderer.CONTEXT_UID] = this.shaderCache[filter.glShaderKey] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); } } else { - shader = filter.glShaders[gl.id] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); + shader = filter.glShaders[renderer.CONTEXT_UID] = new Shader(gl, filter.vertexSrc, filter.fragmentSrc); } //TODO - this only needs to be done once? diff --git a/src/filters/FXAA/FXAA.frag b/src/filters/FXAA/FXAA.frag new file mode 100755 index 0000000..2f3b3ae --- /dev/null +++ b/src/filters/FXAA/FXAA.frag @@ -0,0 +1,127 @@ +precision lowp float; + + +/** +Basic FXAA implementation based on the code on geeks3d.com with the +modification that the texture2DLod stuff was removed since it's +unsupported by WebGL. + +-- + +From: +https://github.com/mitsuhiko/webgl-meincraft + +Copyright (c) 2011 by Armin Ronacher. + +Some rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * The names of the contributors may not be used to endorse or + promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef FXAA_REDUCE_MIN + #define FXAA_REDUCE_MIN (1.0/ 128.0) +#endif +#ifndef FXAA_REDUCE_MUL + #define FXAA_REDUCE_MUL (1.0 / 8.0) +#endif +#ifndef FXAA_SPAN_MAX + #define FXAA_SPAN_MAX 8.0 +#endif + +//optimized version for mobile, where dependent +//texture reads can be a bottleneck +vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution, + vec2 v_rgbNW, vec2 v_rgbNE, + vec2 v_rgbSW, vec2 v_rgbSE, + vec2 v_rgbM) { + vec4 color; + mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y); + vec3 rgbNW = texture2D(tex, v_rgbNW).xyz; + vec3 rgbNE = texture2D(tex, v_rgbNE).xyz; + vec3 rgbSW = texture2D(tex, v_rgbSW).xyz; + vec3 rgbSE = texture2D(tex, v_rgbSE).xyz; + vec4 texColor = texture2D(tex, v_rgbM); + vec3 rgbM = texColor.xyz; + vec3 luma = vec3(0.299, 0.587, 0.114); + float lumaNW = dot(rgbNW, luma); + float lumaNE = dot(rgbNE, luma); + float lumaSW = dot(rgbSW, luma); + float lumaSE = dot(rgbSE, luma); + float lumaM = dot(rgbM, luma); + float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); + float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); + + mediump vec2 dir; + dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); + dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); + + float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * + (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); + + float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); + dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), + max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), + dir * rcpDirMin)) * inverseVP; + + vec3 rgbA = 0.5 * ( + texture2D(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz + + texture2D(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz); + vec3 rgbB = rgbA * 0.5 + 0.25 * ( + texture2D(tex, fragCoord * inverseVP + dir * -0.5).xyz + + texture2D(tex, fragCoord * inverseVP + dir * 0.5).xyz); + + float lumaB = dot(rgbB, luma); + if ((lumaB < lumaMin) || (lumaB > lumaMax)) + color = vec4(rgbA, texColor.a); + else + color = vec4(rgbB, texColor.a); + return color; +} + + +varying vec2 vTextureCoord; +varying vec4 vColor; +varying vec2 vResolution; + +//texcoords computed in vertex step +//to avoid dependent texture reads +varying vec2 v_rgbNW; +varying vec2 v_rgbNE; +varying vec2 v_rgbSW; +varying vec2 v_rgbSE; +varying vec2 v_rgbM; + +uniform sampler2D uSampler; + + +void main(void){ + + gl_FragColor = fxaa(uSampler, vTextureCoord * vResolution, vResolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); + +} diff --git a/src/filters/FXAA/FXAA.vert b/src/filters/FXAA/FXAA.vert new file mode 100755 index 0000000..b0c1860 --- /dev/null +++ b/src/filters/FXAA/FXAA.vert @@ -0,0 +1,45 @@ + +precision mediump float; + +attribute vec2 aVertexPosition; +attribute vec2 aTextureCoord; +attribute vec4 aColor; + +uniform mat3 projectionMatrix; +uniform vec2 resolution; + +varying vec2 vTextureCoord; +varying vec4 vColor; + +varying vec2 vResolution; + +//texcoords computed in vertex step +//to avoid dependent texture reads +varying vec2 v_rgbNW; +varying vec2 v_rgbNE; +varying vec2 v_rgbSW; +varying vec2 v_rgbSE; +varying vec2 v_rgbM; + + +void texcoords(vec2 fragCoord, vec2 resolution, + out vec2 v_rgbNW, out vec2 v_rgbNE, + out vec2 v_rgbSW, out vec2 v_rgbSE, + out vec2 v_rgbM) { + vec2 inverseVP = 1.0 / resolution.xy; + v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP; + v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP; + v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP; + v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP; + v_rgbM = vec2(fragCoord * inverseVP); +} + +void main(void){ + gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0); + vTextureCoord = aTextureCoord; + vColor = vec4(aColor.rgb * aColor.a, aColor.a); + vResolution = resolution; + + //compute the texture coords and send them to varyings + texcoords(aTextureCoord * resolution, resolution, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM); +} diff --git a/src/filters/FXAA/FXAAFilter.js b/src/filters/FXAA/FXAAFilter.js new file mode 100644 index 0000000..6d5646c --- /dev/null +++ b/src/filters/FXAA/FXAAFilter.js @@ -0,0 +1,53 @@ +var AbstractFilter = require('./AbstractFilter'); +// @see https://github.com/substack/brfs/issues/25 +var fs = require('fs'); + +/** + * + * Basic FXAA implementation based on the code on geeks3d.com with the + * modification that the texture2DLod stuff was removed since it's + * unsupported by WebGL. + * + * -- + * From: + * https://github.com/mitsuhiko/webgl-meincraft + * + * @class + * @extends PIXI.AbstractFilter + * @memberof PIXI + * + */ +function FXAAFilter() +{ + AbstractFilter.call(this, + // vertex shader + fs.readFileSync(__dirname + '/FXAA.vert', 'utf8'), + // fragment shader + fs.readFileSync(__dirname + '/FXAA.frag', 'utf8'), + // uniforms + { + resolution: { type: 'v2', value: { x: 1, y: 1 } } + } + ); + +} + +FXAAFilter.prototype = Object.create(AbstractFilter.prototype); +FXAAFilter.prototype.constructor = FXAAFilter; +module.exports = FXAAFilter; + +/** + * Applies the filter + * + * @param renderer {PIXI.WebGLRenderer} The renderer to retrieve the filter from + * @param input {PIXI.RenderTarget} + * @param output {PIXI.RenderTarget} + */ +FXAAFilter.prototype.applyFilter = function (renderer, input, output) +{ + var filterManager = renderer.filterManager; + + var shader = this.getShader( renderer ); + // draw the filter... + filterManager.applyFilter(shader, input, output); +}; diff --git a/src/mesh/webgl/MeshRenderer.js b/src/mesh/webgl/MeshRenderer.js index 0eade84..a49eecd 100644 --- a/src/mesh/webgl/MeshRenderer.js +++ b/src/mesh/webgl/MeshRenderer.js @@ -92,7 +92,7 @@ } else { - shader = shader.shaders[gl.id] || shader.getShader(renderer);// : shader; + shader = shader.shaders[renderer.CONTEXT_UID] || shader.getShader(renderer);// : shader; } this.renderer.shaderManager.setShader(shader); @@ -117,14 +117,14 @@ gl.activeTexture(gl.TEXTURE0); - if (!texture._glTextures[gl.id]) + if (!texture._glTextures[renderer.CONTEXT_UID]) { this.renderer.updateTexture(texture); } else { // bind the current texture - gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); + gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[renderer.CONTEXT_UID]); } gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, mesh._indexBuffer); @@ -145,14 +145,14 @@ gl.activeTexture(gl.TEXTURE0); - if (!texture._glTextures[gl.id]) + if (!texture._glTextures[renderer.CONTEXT_UID]) { this.renderer.updateTexture(texture); } else { // bind the current texture - gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]); + gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[renderer.CONTEXT_UID]); } // dont need to upload!