diff --git a/src/core/renderers/webgl/ShaderManager.js b/src/core/renderers/webgl/ShaderManager.js index d541c88..ca704d6 100644 --- a/src/core/renderers/webgl/ShaderManager.js +++ b/src/core/renderers/webgl/ShaderManager.js @@ -29,10 +29,16 @@ * @member {WebGLRenderingContext} */ this.gl = renderer.gl; + + this.shader = null; } bindShader(shader, dontSync) { + if(this.shader === shader)return; + + this.shader = shader; + let glShader = shader.glShaders[this.renderer.CONTEXT_UID] || this.generateShader(shader); this.renderer._bindGLShader(glShader); @@ -42,6 +48,92 @@ } } + setUniforms(uniforms) + { + const shader = this.shader; + const glShader = shader.glShaders[this.renderer.CONTEXT_UID]; + + + const uniformData = shader.uniformData; + const realUniforms = shader.realUniforms; + + // 0 is reserverd for the pixi texture so we start at 1! + let textureCount = 1; + let i = 0; + + for (i in uniforms) + { + uniformData[i].value = uniforms[i]; + } + + // TODO don't need to use the uniform + for (let i in uniformData) + { + if (uniformData[i].type === 'sampler2D' && uniformData[i].value !== 0) + { + if (uniformData[i].value.baseTexture) + { + glShader.uniforms[i] = this.renderer.bindTexture(uniformData[i].value, textureCount, true); + // console.log( glShader.uniforms[i]) + } + else + { + glShader.uniforms[i] = textureCount; + // TODO + // this is helpful as renderTargets can also be set. + // Although thinking about it, we could probably + // make the filter texture cache return a RenderTexture + // rather than a renderTarget + const gl = this.renderer.gl; + + gl.activeTexture(gl.TEXTURE0 + textureCount); + uniforms[i].texture.bind(); + } + + textureCount++; + } + else if (uniformData[i].type === 'mat3') + { + // check if its pixi matrix.. + if (uniformData[i].value.a !== undefined) + { + glShader.uniforms[i] = uniformData[i].value.toArray(true); + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else if (uniformData[i].type === 'vec2') + { + // check if its a point.. + if (uniformData[i].value.x !== undefined) + { + const val = glShader.uniforms[i] || new Float32Array(2); + + val[0] = uniformData[i].value.x; + val[1] = uniformData[i].value.y; + glShader.uniforms[i] = val; + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else if (uniformData[i].type === 'float') + { + if (glShader.uniforms.data[i].value !== uniformData[i].value) + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + } + generateShader(shader) { diff --git a/src/core/renderers/webgl/ShaderManager.js b/src/core/renderers/webgl/ShaderManager.js index d541c88..ca704d6 100644 --- a/src/core/renderers/webgl/ShaderManager.js +++ b/src/core/renderers/webgl/ShaderManager.js @@ -29,10 +29,16 @@ * @member {WebGLRenderingContext} */ this.gl = renderer.gl; + + this.shader = null; } bindShader(shader, dontSync) { + if(this.shader === shader)return; + + this.shader = shader; + let glShader = shader.glShaders[this.renderer.CONTEXT_UID] || this.generateShader(shader); this.renderer._bindGLShader(glShader); @@ -42,6 +48,92 @@ } } + setUniforms(uniforms) + { + const shader = this.shader; + const glShader = shader.glShaders[this.renderer.CONTEXT_UID]; + + + const uniformData = shader.uniformData; + const realUniforms = shader.realUniforms; + + // 0 is reserverd for the pixi texture so we start at 1! + let textureCount = 1; + let i = 0; + + for (i in uniforms) + { + uniformData[i].value = uniforms[i]; + } + + // TODO don't need to use the uniform + for (let i in uniformData) + { + if (uniformData[i].type === 'sampler2D' && uniformData[i].value !== 0) + { + if (uniformData[i].value.baseTexture) + { + glShader.uniforms[i] = this.renderer.bindTexture(uniformData[i].value, textureCount, true); + // console.log( glShader.uniforms[i]) + } + else + { + glShader.uniforms[i] = textureCount; + // TODO + // this is helpful as renderTargets can also be set. + // Although thinking about it, we could probably + // make the filter texture cache return a RenderTexture + // rather than a renderTarget + const gl = this.renderer.gl; + + gl.activeTexture(gl.TEXTURE0 + textureCount); + uniforms[i].texture.bind(); + } + + textureCount++; + } + else if (uniformData[i].type === 'mat3') + { + // check if its pixi matrix.. + if (uniformData[i].value.a !== undefined) + { + glShader.uniforms[i] = uniformData[i].value.toArray(true); + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else if (uniformData[i].type === 'vec2') + { + // check if its a point.. + if (uniformData[i].value.x !== undefined) + { + const val = glShader.uniforms[i] || new Float32Array(2); + + val[0] = uniformData[i].value.x; + val[1] = uniformData[i].value.y; + glShader.uniforms[i] = val; + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else if (uniformData[i].type === 'float') + { + if (glShader.uniforms.data[i].value !== uniformData[i].value) + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + } + generateShader(shader) { diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 046a45c..58e4db8 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -128,4 +128,5 @@ } -} \ No newline at end of file +} + diff --git a/src/core/renderers/webgl/ShaderManager.js b/src/core/renderers/webgl/ShaderManager.js index d541c88..ca704d6 100644 --- a/src/core/renderers/webgl/ShaderManager.js +++ b/src/core/renderers/webgl/ShaderManager.js @@ -29,10 +29,16 @@ * @member {WebGLRenderingContext} */ this.gl = renderer.gl; + + this.shader = null; } bindShader(shader, dontSync) { + if(this.shader === shader)return; + + this.shader = shader; + let glShader = shader.glShaders[this.renderer.CONTEXT_UID] || this.generateShader(shader); this.renderer._bindGLShader(glShader); @@ -42,6 +48,92 @@ } } + setUniforms(uniforms) + { + const shader = this.shader; + const glShader = shader.glShaders[this.renderer.CONTEXT_UID]; + + + const uniformData = shader.uniformData; + const realUniforms = shader.realUniforms; + + // 0 is reserverd for the pixi texture so we start at 1! + let textureCount = 1; + let i = 0; + + for (i in uniforms) + { + uniformData[i].value = uniforms[i]; + } + + // TODO don't need to use the uniform + for (let i in uniformData) + { + if (uniformData[i].type === 'sampler2D' && uniformData[i].value !== 0) + { + if (uniformData[i].value.baseTexture) + { + glShader.uniforms[i] = this.renderer.bindTexture(uniformData[i].value, textureCount, true); + // console.log( glShader.uniforms[i]) + } + else + { + glShader.uniforms[i] = textureCount; + // TODO + // this is helpful as renderTargets can also be set. + // Although thinking about it, we could probably + // make the filter texture cache return a RenderTexture + // rather than a renderTarget + const gl = this.renderer.gl; + + gl.activeTexture(gl.TEXTURE0 + textureCount); + uniforms[i].texture.bind(); + } + + textureCount++; + } + else if (uniformData[i].type === 'mat3') + { + // check if its pixi matrix.. + if (uniformData[i].value.a !== undefined) + { + glShader.uniforms[i] = uniformData[i].value.toArray(true); + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else if (uniformData[i].type === 'vec2') + { + // check if its a point.. + if (uniformData[i].value.x !== undefined) + { + const val = glShader.uniforms[i] || new Float32Array(2); + + val[0] = uniformData[i].value.x; + val[1] = uniformData[i].value.y; + glShader.uniforms[i] = val; + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else if (uniformData[i].type === 'float') + { + if (glShader.uniforms.data[i].value !== uniformData[i].value) + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + } + generateShader(shader) { diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 046a45c..58e4db8 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -128,4 +128,5 @@ } -} \ No newline at end of file +} + diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 6226886..c6e7721 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -448,6 +448,8 @@ this.shaderManager.bindShader(shader, dontSync); } + + /** * Changes the current shader to the one given in parameter * diff --git a/src/core/renderers/webgl/ShaderManager.js b/src/core/renderers/webgl/ShaderManager.js index d541c88..ca704d6 100644 --- a/src/core/renderers/webgl/ShaderManager.js +++ b/src/core/renderers/webgl/ShaderManager.js @@ -29,10 +29,16 @@ * @member {WebGLRenderingContext} */ this.gl = renderer.gl; + + this.shader = null; } bindShader(shader, dontSync) { + if(this.shader === shader)return; + + this.shader = shader; + let glShader = shader.glShaders[this.renderer.CONTEXT_UID] || this.generateShader(shader); this.renderer._bindGLShader(glShader); @@ -42,6 +48,92 @@ } } + setUniforms(uniforms) + { + const shader = this.shader; + const glShader = shader.glShaders[this.renderer.CONTEXT_UID]; + + + const uniformData = shader.uniformData; + const realUniforms = shader.realUniforms; + + // 0 is reserverd for the pixi texture so we start at 1! + let textureCount = 1; + let i = 0; + + for (i in uniforms) + { + uniformData[i].value = uniforms[i]; + } + + // TODO don't need to use the uniform + for (let i in uniformData) + { + if (uniformData[i].type === 'sampler2D' && uniformData[i].value !== 0) + { + if (uniformData[i].value.baseTexture) + { + glShader.uniforms[i] = this.renderer.bindTexture(uniformData[i].value, textureCount, true); + // console.log( glShader.uniforms[i]) + } + else + { + glShader.uniforms[i] = textureCount; + // TODO + // this is helpful as renderTargets can also be set. + // Although thinking about it, we could probably + // make the filter texture cache return a RenderTexture + // rather than a renderTarget + const gl = this.renderer.gl; + + gl.activeTexture(gl.TEXTURE0 + textureCount); + uniforms[i].texture.bind(); + } + + textureCount++; + } + else if (uniformData[i].type === 'mat3') + { + // check if its pixi matrix.. + if (uniformData[i].value.a !== undefined) + { + glShader.uniforms[i] = uniformData[i].value.toArray(true); + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else if (uniformData[i].type === 'vec2') + { + // check if its a point.. + if (uniformData[i].value.x !== undefined) + { + const val = glShader.uniforms[i] || new Float32Array(2); + + val[0] = uniformData[i].value.x; + val[1] = uniformData[i].value.y; + glShader.uniforms[i] = val; + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else if (uniformData[i].type === 'float') + { + if (glShader.uniforms.data[i].value !== uniformData[i].value) + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + } + generateShader(shader) { diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 046a45c..58e4db8 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -128,4 +128,5 @@ } -} \ No newline at end of file +} + diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 6226886..c6e7721 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -448,6 +448,8 @@ this.shaderManager.bindShader(shader, dontSync); } + + /** * Changes the current shader to the one given in parameter * diff --git a/src/core/renderers/webgl/managers/StateManager.js b/src/core/renderers/webgl/managers/StateManager.js index 36efb3b..9466b50 100755 --- a/src/core/renderers/webgl/managers/StateManager.js +++ b/src/core/renderers/webgl/managers/StateManager.js @@ -57,43 +57,6 @@ this.map[WINDING] = this.setFrontFace; this.checks = []; - -/* - this.p1 = true; - this.p2 = false; - this.p3 = false; - this.p4 = true; - this.p5 = true; - this.p5 = false; - - - // pack into bitfield.. - this.st = (true << 0) | (false << 1) | (true << 2); - this.st2 = (false << 0) | (false << 1) | (true << 2); - - this.st &= ~(1<<2); - - console.log(this.st); - - console.log("0 is " + !!(this.st & (1 << 0)) ); - console.log("1 is " + !!(this.st & (1 << 1)) ); - console.log("2 is " + !!(this.st & (1 << 2)) ); - - //if(this.st !== ) - let diff = this.st ^ this.st2; - let i = 0; - - // order from least to most common - while(diff) - { - if(diff & 1) - { - //skips least common.. - console.log(' diff is ' + i) - } - diff = diff >> 1; - i++; - }*/ } /** diff --git a/src/core/renderers/webgl/ShaderManager.js b/src/core/renderers/webgl/ShaderManager.js index d541c88..ca704d6 100644 --- a/src/core/renderers/webgl/ShaderManager.js +++ b/src/core/renderers/webgl/ShaderManager.js @@ -29,10 +29,16 @@ * @member {WebGLRenderingContext} */ this.gl = renderer.gl; + + this.shader = null; } bindShader(shader, dontSync) { + if(this.shader === shader)return; + + this.shader = shader; + let glShader = shader.glShaders[this.renderer.CONTEXT_UID] || this.generateShader(shader); this.renderer._bindGLShader(glShader); @@ -42,6 +48,92 @@ } } + setUniforms(uniforms) + { + const shader = this.shader; + const glShader = shader.glShaders[this.renderer.CONTEXT_UID]; + + + const uniformData = shader.uniformData; + const realUniforms = shader.realUniforms; + + // 0 is reserverd for the pixi texture so we start at 1! + let textureCount = 1; + let i = 0; + + for (i in uniforms) + { + uniformData[i].value = uniforms[i]; + } + + // TODO don't need to use the uniform + for (let i in uniformData) + { + if (uniformData[i].type === 'sampler2D' && uniformData[i].value !== 0) + { + if (uniformData[i].value.baseTexture) + { + glShader.uniforms[i] = this.renderer.bindTexture(uniformData[i].value, textureCount, true); + // console.log( glShader.uniforms[i]) + } + else + { + glShader.uniforms[i] = textureCount; + // TODO + // this is helpful as renderTargets can also be set. + // Although thinking about it, we could probably + // make the filter texture cache return a RenderTexture + // rather than a renderTarget + const gl = this.renderer.gl; + + gl.activeTexture(gl.TEXTURE0 + textureCount); + uniforms[i].texture.bind(); + } + + textureCount++; + } + else if (uniformData[i].type === 'mat3') + { + // check if its pixi matrix.. + if (uniformData[i].value.a !== undefined) + { + glShader.uniforms[i] = uniformData[i].value.toArray(true); + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else if (uniformData[i].type === 'vec2') + { + // check if its a point.. + if (uniformData[i].value.x !== undefined) + { + const val = glShader.uniforms[i] || new Float32Array(2); + + val[0] = uniformData[i].value.x; + val[1] = uniformData[i].value.y; + glShader.uniforms[i] = val; + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else if (uniformData[i].type === 'float') + { + if (glShader.uniforms.data[i].value !== uniformData[i].value) + { + glShader.uniforms[i] = uniformData[i].value; + } + } + else + { + glShader.uniforms[i] = uniformData[i].value; + } + } + } + generateShader(shader) { diff --git a/src/core/renderers/webgl/State.js b/src/core/renderers/webgl/State.js index 046a45c..58e4db8 100644 --- a/src/core/renderers/webgl/State.js +++ b/src/core/renderers/webgl/State.js @@ -128,4 +128,5 @@ } -} \ No newline at end of file +} + diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index 6226886..c6e7721 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -448,6 +448,8 @@ this.shaderManager.bindShader(shader, dontSync); } + + /** * Changes the current shader to the one given in parameter * diff --git a/src/core/renderers/webgl/managers/StateManager.js b/src/core/renderers/webgl/managers/StateManager.js index 36efb3b..9466b50 100755 --- a/src/core/renderers/webgl/managers/StateManager.js +++ b/src/core/renderers/webgl/managers/StateManager.js @@ -57,43 +57,6 @@ this.map[WINDING] = this.setFrontFace; this.checks = []; - -/* - this.p1 = true; - this.p2 = false; - this.p3 = false; - this.p4 = true; - this.p5 = true; - this.p5 = false; - - - // pack into bitfield.. - this.st = (true << 0) | (false << 1) | (true << 2); - this.st2 = (false << 0) | (false << 1) | (true << 2); - - this.st &= ~(1<<2); - - console.log(this.st); - - console.log("0 is " + !!(this.st & (1 << 0)) ); - console.log("1 is " + !!(this.st & (1 << 1)) ); - console.log("2 is " + !!(this.st & (1 << 2)) ); - - //if(this.st !== ) - let diff = this.st ^ this.st2; - let i = 0; - - // order from least to most common - while(diff) - { - if(diff & 1) - { - //skips least common.. - console.log(' diff is ' + i) - } - diff = diff >> 1; - i++; - }*/ } /** diff --git a/src/mesh/webgl/MeshRenderer.js b/src/mesh/webgl/MeshRenderer.js index 0e68959..64ae550 100644 --- a/src/mesh/webgl/MeshRenderer.js +++ b/src/mesh/webgl/MeshRenderer.js @@ -52,8 +52,10 @@ // this.renderer.state.setBlendMode(mesh.blendMode); // bind the shader.. - // TODO rename filter to shader - this.renderer.bindShader(mesh.shader); + this.renderer.shaderManager.bindShader(mesh.shader, true); + + // set unifomrs.. + this.renderer.shaderManager.setUniforms(mesh.uniforms); // sync uniforms.. this.renderer.state.setState(mesh.state);