From 369c8d87dc823e8c8909c2c4736714c994b3cb5c Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 3 Nov 2016 14:59:07 +0800 Subject: [PATCH 1/2] Remove overwrite in webgl context object --- cocos2d/core/CCDrawingPrimitivesWebGL.js | 36 +++--- cocos2d/core/CCGame.js | 2 +- cocos2d/core/graphics/graphics-webgl-cmd.js | 8 +- cocos2d/core/layers/CCLayerWebGLRenderCmd.js | 27 ++--- cocos2d/core/renderer/RendererWebGL.js | 22 ++-- cocos2d/core/textures/CCTextureAtlas.js | 22 ++-- cocos2d/effects/CCGrid.js | 35 +++--- cocos2d/motion-streak/CCSGMotionStreak.js | 7 +- .../CCSGMotionStreakWebGLRenderCmd.js | 19 ++-- .../CCSGParticleSystemWebGLRenderCmd.js | 22 ++-- .../CCProgressTimerWebGLRenderCmd.js | 16 +-- cocos2d/shaders/CCGLStateCache.js | 105 +++++++++--------- cocos2d/shaders/index.js | 4 +- cocos2d/shape-nodes/CCDrawNode.js | 9 +- .../armature/display/CCSkinWebGLRenderCmd.js | 10 +- 15 files changed, 174 insertions(+), 170 deletions(-) diff --git a/cocos2d/core/CCDrawingPrimitivesWebGL.js b/cocos2d/core/CCDrawingPrimitivesWebGL.js index eca726063a..6052378cf1 100644 --- a/cocos2d/core/CCDrawingPrimitivesWebGL.js +++ b/cocos2d/core/CCDrawingPrimitivesWebGL.js @@ -85,12 +85,12 @@ cc.DrawingPrimitiveWebGL = cc._Class.extend(/** @lends cc.DrawingPrimitiveWebGL# var glContext = this._renderContext; this._shader.use(); this._shader.setUniformForModelViewAndProjectionMatrixWithMat4(); - glContext.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); + cc.gl.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray); this._shader.setUniformLocationWith1f(this._pointSizeLocation, this._pointSize); var pointBuffer = glContext.createBuffer(); - glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); + cc.gl.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); glContext.bufferData(glContext.ARRAY_BUFFER, new Float32Array([point.x, point.y]), glContext.STATIC_DRAW); glContext.vertexAttribPointer(macro.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0); @@ -114,12 +114,12 @@ cc.DrawingPrimitiveWebGL = cc._Class.extend(/** @lends cc.DrawingPrimitiveWebGL# var glContext = this._renderContext; this._shader.use(); this._shader.setUniformForModelViewAndProjectionMatrixWithMat4(); - glContext.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); + cc.gl.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray); this._shader.setUniformLocationWith1f(this._pointSizeLocation, this._pointSize); var pointBuffer = glContext.createBuffer(); - glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); + cc.gl.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); glContext.bufferData(glContext.ARRAY_BUFFER, this._pointsToTypeArray(points), glContext.STATIC_DRAW); glContext.vertexAttribPointer(macro.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0); @@ -149,11 +149,11 @@ cc.DrawingPrimitiveWebGL = cc._Class.extend(/** @lends cc.DrawingPrimitiveWebGL# var glContext = this._renderContext; this._shader.use(); this._shader.setUniformForModelViewAndProjectionMatrixWithMat4(); - glContext.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); + cc.gl.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray); var pointBuffer = glContext.createBuffer(); - glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); + cc.gl.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); glContext.bufferData(glContext.ARRAY_BUFFER, this._pointsToTypeArray([origin, destination]), glContext.STATIC_DRAW); glContext.vertexAttribPointer(macro.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0); @@ -204,11 +204,11 @@ cc.DrawingPrimitiveWebGL = cc._Class.extend(/** @lends cc.DrawingPrimitiveWebGL# var glContext = this._renderContext; this._shader.use(); this._shader.setUniformForModelViewAndProjectionMatrixWithMat4(); - glContext.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); + cc.gl.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray); var pointBuffer = glContext.createBuffer(); - glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); + cc.gl.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); glContext.bufferData(glContext.ARRAY_BUFFER, this._pointsToTypeArray(vertices), glContext.STATIC_DRAW); glContext.vertexAttribPointer(macro.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0); @@ -235,11 +235,11 @@ cc.DrawingPrimitiveWebGL = cc._Class.extend(/** @lends cc.DrawingPrimitiveWebGL# var glContext = this._renderContext; this._shader.use(); this._shader.setUniformForModelViewAndProjectionMatrixWithMat4(); - glContext.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); + cc.gl.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray); var pointBuffer = glContext.createBuffer(); - glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); + cc.gl.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); glContext.bufferData(glContext.ARRAY_BUFFER, this._pointsToTypeArray(poli), glContext.STATIC_DRAW); glContext.vertexAttribPointer(macro.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0); glContext.drawArrays(glContext.TRIANGLE_FAN, 0, poli.length); @@ -283,11 +283,11 @@ cc.DrawingPrimitiveWebGL = cc._Class.extend(/** @lends cc.DrawingPrimitiveWebGL# var glContext = this._renderContext; this._shader.use(); this._shader.setUniformForModelViewAndProjectionMatrixWithMat4(); - glContext.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); + cc.gl.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray); var pointBuffer = glContext.createBuffer(); - glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); + cc.gl.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); glContext.bufferData(glContext.ARRAY_BUFFER, vertices, glContext.STATIC_DRAW); glContext.vertexAttribPointer(macro.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0); @@ -321,11 +321,11 @@ cc.DrawingPrimitiveWebGL = cc._Class.extend(/** @lends cc.DrawingPrimitiveWebGL# var glContext = this._renderContext; this._shader.use(); this._shader.setUniformForModelViewAndProjectionMatrixWithMat4(); - glContext.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); + cc.gl.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray); var pointBuffer = glContext.createBuffer(); - glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); + cc.gl.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); glContext.bufferData(glContext.ARRAY_BUFFER, vertices, glContext.STATIC_DRAW); glContext.vertexAttribPointer(macro.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0); @@ -360,11 +360,11 @@ cc.DrawingPrimitiveWebGL = cc._Class.extend(/** @lends cc.DrawingPrimitiveWebGL# var glContext = this._renderContext; this._shader.use(); this._shader.setUniformForModelViewAndProjectionMatrixWithMat4(); - glContext.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); + cc.gl.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray); var pointBuffer = glContext.createBuffer(); - glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); + cc.gl.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); glContext.bufferData(glContext.ARRAY_BUFFER, vertices, glContext.STATIC_DRAW); glContext.vertexAttribPointer(macro.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0); glContext.drawArrays(glContext.LINE_STRIP, 0, segments + 1); @@ -420,11 +420,11 @@ cc.DrawingPrimitiveWebGL = cc._Class.extend(/** @lends cc.DrawingPrimitiveWebGL# var glContext = this._renderContext; this._shader.use(); this._shader.setUniformForModelViewAndProjectionMatrixWithMat4(); - glContext.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); + cc.gl.enableVertexAttribArray(macro.VERTEX_ATTRIB_FLAG_POSITION); this._shader.setUniformLocationWith4fv(this._colorLocation, this._colorArray); var pointBuffer = glContext.createBuffer(); - glContext.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); + cc.gl.bindBuffer(glContext.ARRAY_BUFFER, pointBuffer); glContext.bufferData(glContext.ARRAY_BUFFER, vertices, glContext.STATIC_DRAW); glContext.vertexAttribPointer(macro.VERTEX_ATTRIB_POSITION, 2, glContext.FLOAT, false, 0, 0); glContext.drawArrays(glContext.LINE_STRIP, 0, segments + 1); diff --git a/cocos2d/core/CCGame.js b/cocos2d/core/CCGame.js index dcd501440d..dc78217b7d 100644 --- a/cocos2d/core/CCGame.js +++ b/cocos2d/core/CCGame.js @@ -646,7 +646,7 @@ var game = { var el = this.config[game.CONFIG_KEY.id], win = window, element = cc.$(el) || cc.$('#' + el), - localCanvas, localContainer, localConStyle; + localCanvas, localContainer; if (element.tagName === "CANVAS") { width = width || element.width; diff --git a/cocos2d/core/graphics/graphics-webgl-cmd.js b/cocos2d/core/graphics/graphics-webgl-cmd.js index 06ef569ff8..42fccaa179 100644 --- a/cocos2d/core/graphics/graphics-webgl-cmd.js +++ b/cocos2d/core/graphics/graphics-webgl-cmd.js @@ -310,10 +310,10 @@ Js.mixin(_p, { var vertsBuffer = this._vertsBuffer; if (!vertsBuffer || vertsBuffer.length === 0 || this._cmds.length === 0) return; - var gl = cc._renderContext; + var gl = cc._renderContext, ccgl = cc.gl; - gl.bindBuffer(gl.ARRAY_BUFFER, this._vertsVBO); - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesVBO); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._vertsVBO); + ccgl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesVBO); if (this._vertsDirty) { gl.bufferData(gl.ARRAY_BUFFER, vertsBuffer, gl.STREAM_DRAW); @@ -329,7 +329,7 @@ Js.mixin(_p, { cc.warn('Too many graphics vertices generated, only 65536 vertices support.'); } - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, VERTS_BYTE_LENGTH, 0); var shader = this._shader; diff --git a/cocos2d/core/layers/CCLayerWebGLRenderCmd.js b/cocos2d/core/layers/CCLayerWebGLRenderCmd.js index 3ac70da0a5..bfd7956339 100644 --- a/cocos2d/core/layers/CCLayerWebGLRenderCmd.js +++ b/cocos2d/core/layers/CCLayerWebGLRenderCmd.js @@ -82,6 +82,7 @@ proto.rendering = function (ctx) { var context = ctx || cc._renderContext; + var ccgl = cc.gl; var node = this._node; var wt = this._worldTransform, mat = this._matrix.mat; @@ -94,17 +95,17 @@ this._shaderProgram.use(); this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix); - context.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); - context.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); - cc.gl.blendFunc(node._blendFunc.src, node._blendFunc.dst); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); + ccgl.blendFunc(node._blendFunc.src, node._blendFunc.dst); // // Attributes // - context.bindBuffer(context.ARRAY_BUFFER, this._verticesFloat32Buffer); + ccgl.bindBuffer(context.ARRAY_BUFFER, this._verticesFloat32Buffer); context.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 3, context.FLOAT, false, 0, 0); - context.bindBuffer(context.ARRAY_BUFFER, this._colorsUint8Buffer); + ccgl.bindBuffer(context.ARRAY_BUFFER, this._colorsUint8Buffer); context.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_COLOR, 4, context.UNSIGNED_BYTE, true, 0, 0); context.drawArrays(context.TRIANGLE_STRIP, 0, this._squareVertices.length); @@ -144,13 +145,13 @@ proto._bindLayerVerticesBufferData = function(){ var glContext = cc._renderContext; - glContext.bindBuffer(glContext.ARRAY_BUFFER, this._verticesFloat32Buffer); + cc.gl.bindBuffer(glContext.ARRAY_BUFFER, this._verticesFloat32Buffer); glContext.bufferData(glContext.ARRAY_BUFFER, this._squareVerticesAB, glContext.DYNAMIC_DRAW); }; proto._bindLayerColorsBufferData = function(){ var glContext = cc._renderContext; - glContext.bindBuffer(glContext.ARRAY_BUFFER, this._colorsUint8Buffer); + cc.gl.bindBuffer(glContext.ARRAY_BUFFER, this._colorsUint8Buffer); glContext.bufferData(glContext.ARRAY_BUFFER, this._squareColorsAB, glContext.STATIC_DRAW); }; @@ -288,7 +289,7 @@ }; proto.rendering = function (ctx) { - var context = ctx || cc._renderContext, node = this._node; + var context = ctx || cc._renderContext, ccgl = cc.gl, node = this._node; //it is too expensive to use stencil to clip, so it use Scissor, //but it has a bug when layer rotated and layer's content size less than canvas's size. @@ -307,15 +308,15 @@ //draw gradient layer this._shaderProgram.use(); this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix); - context.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); - context.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); - cc.gl.blendFunc(node._blendFunc.src, node._blendFunc.dst); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); + ccgl.blendFunc(node._blendFunc.src, node._blendFunc.dst); // // Attributes // - context.bindBuffer(context.ARRAY_BUFFER, this._verticesFloat32Buffer); + ccgl.bindBuffer(context.ARRAY_BUFFER, this._verticesFloat32Buffer); context.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 3, context.FLOAT, false, 0, 0); - context.bindBuffer(context.ARRAY_BUFFER, this._colorsUint8Buffer); + ccgl.bindBuffer(context.ARRAY_BUFFER, this._colorsUint8Buffer); context.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_COLOR, 4, context.UNSIGNED_BYTE, true, 0, 0); context.drawArrays(context.TRIANGLE_STRIP, 0, this._squareVertices.length); diff --git a/cocos2d/core/renderer/RendererWebGL.js b/cocos2d/core/renderer/RendererWebGL.js index fced723d74..6ae1238bbd 100644 --- a/cocos2d/core/renderer/RendererWebGL.js +++ b/cocos2d/core/renderer/RendererWebGL.js @@ -64,9 +64,10 @@ var _batchedInfo = { // https://github.com/Talisca/cocos2d-html5/commit/de731f16414eb9bcaa20480006897ca6576d362c function updateBuffer (numQuads) { var gl = cc._renderContext; + var ccgl = cc.gl; // Update index buffer size if (_indexBuffer) { - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBuffer); + ccgl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBuffer); _indexData = new Uint16Array(numQuads * 6); var currentQuad = 0; for (var i = 0, len = numQuads * 6; i < len; i += 6) { @@ -88,7 +89,7 @@ function updateBuffer (numQuads) { _vertexDataF32 = new Float32Array(_vertexData); _vertexDataUI32 = new Uint32Array(_vertexData); // Init buffer data - gl.bindBuffer(gl.ARRAY_BUFFER, _vertexBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, _vertexBuffer); gl.bufferData(gl.ARRAY_BUFFER, _vertexDataF32, gl.DYNAMIC_DRAW); } // Downsize by 100 to avoid vertex data overflow @@ -372,6 +373,7 @@ cc.rendererWebGL = { } var gl = cc._renderContext; + var ccgl = cc.gl; var texture = _batchedInfo.texture; var shader = _batchedInfo.shader; var uploadAll = _batchingSize > _maxVertexSize * 0.5; @@ -381,10 +383,10 @@ cc.rendererWebGL = { shader._updateProjectionUniform(); } - cc.gl.blendFunc(_batchedInfo.blendSrc, _batchedInfo.blendDst); - cc.gl.bindTexture2DN(0, texture); // = cc.gl.bindTexture2D(texture); + ccgl.blendFunc(_batchedInfo.blendSrc, _batchedInfo.blendDst); + ccgl.bindTexture2DN(0, texture); // = ccgl.bindTexture2D(texture); - var _bufferchanged = !gl.bindBuffer(gl.ARRAY_BUFFER, _vertexBuffer); + var _bufferchanged = ccgl.bindBuffer(gl.ARRAY_BUFFER, _vertexBuffer); // upload the vertex data to the gl buffer if (uploadAll) { gl.bufferData(gl.ARRAY_BUFFER, _vertexDataF32, gl.DYNAMIC_DRAW); @@ -395,15 +397,15 @@ cc.rendererWebGL = { } if (_bufferchanged) { - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_TEX_COORDS); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_TEX_COORDS); gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 3, gl.FLOAT, false, 24, 0); gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_COLOR, 4, gl.UNSIGNED_BYTE, true, 24, 12); gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 24, 16); } - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBuffer); + ccgl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBuffer); if (!_prevIndexSize || !_pureQuad || _indexSize > _prevIndexSize) { if (uploadAll) { gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, _indexData, gl.DYNAMIC_DRAW); @@ -437,7 +439,7 @@ cc.rendererWebGL = { context = ctx || cc._renderContext; // Reset buffer for rendering - context.bindBuffer(context.ARRAY_BUFFER, null); + cc.gl.bindBuffer(context.ARRAY_BUFFER, null); for (i = 0, len = locCmds.length; i < len; ++i) { cmd = locCmds[i]; diff --git a/cocos2d/core/textures/CCTextureAtlas.js b/cocos2d/core/textures/CCTextureAtlas.js index dada06a179..5dca6666b4 100644 --- a/cocos2d/core/textures/CCTextureAtlas.js +++ b/cocos2d/core/textures/CCTextureAtlas.js @@ -639,12 +639,12 @@ if (cc._renderType === game.RENDER_TYPE_WEBGL) { _mapBuffers: function () { var _t = this; - var gl = cc._renderContext; + var gl = cc._renderContext, ccgl = cc.gl; - gl.bindBuffer(gl.ARRAY_BUFFER, _t._quadsWebBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, _t._quadsWebBuffer); gl.bufferData(gl.ARRAY_BUFFER, _t._quadsArrayBuffer, gl.DYNAMIC_DRAW); - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _t._buffersVBO[1]); + ccgl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _t._buffersVBO[1]); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, _t._indices, gl.STATIC_DRAW); //cc.checkGLErrorDebug(); @@ -660,19 +660,19 @@ if (cc._renderType === game.RENDER_TYPE_WEBGL) { if (0 === n || !_t.texture || !_t.texture.isLoaded()) return; - var gl = cc._renderContext; - cc.gl.bindTexture2D(_t.texture); + var gl = cc._renderContext, ccgl = cc.gl; + ccgl.bindTexture2D(_t.texture); // // Using VBO without VAO // //vertices - //gl.bindBuffer(gl.ARRAY_BUFFER, _t._buffersVBO[0]); + ccgl.bindBuffer(gl.ARRAY_BUFFER, _t._buffersVBO[0]); // XXX: update is done in draw... perhaps it should be done in a timer - gl.bindBuffer(gl.ARRAY_BUFFER, _t._quadsWebBuffer); - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_TEX_COORDS); + ccgl.bindBuffer(gl.ARRAY_BUFFER, _t._quadsWebBuffer); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_TEX_COORDS); if (_t.dirty){ gl.bufferData(gl.ARRAY_BUFFER, _t._quadsArrayBuffer, gl.DYNAMIC_DRAW); _t.dirty = false; @@ -682,7 +682,7 @@ if (cc._renderType === game.RENDER_TYPE_WEBGL) { gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_COLOR, 4, gl.UNSIGNED_BYTE, true, 24, 12); // colors gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 24, 16); // tex coords - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _t._buffersVBO[1]); + ccgl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _t._buffersVBO[1]); if (cc.macro.TEXTURE_ATLAS_USE_TRIANGLE_STRIP) gl.drawElements(gl.TRIANGLE_STRIP, n * 6, gl.UNSIGNED_SHORT, start * 6 * _t._indices.BYTES_PER_ELEMENT); diff --git a/cocos2d/effects/CCGrid.js b/cocos2d/effects/CCGrid.js index 2d390c3824..e59ad74028 100644 --- a/cocos2d/effects/CCGrid.js +++ b/cocos2d/effects/CCGrid.js @@ -451,7 +451,8 @@ cc.Grid3D = cc.GridBase.extend(/** @lends cc.Grid3D# */{ blit:function (target) { var n = this._gridSize.width * this._gridSize.height; - cc.gl.enableVertexAttribs(cc.macro.VERTEX_ATTRIB_FLAG_POSITION | cc.macro.VERTEX_ATTRIB_FLAG_TEX_COORDS); + var ccgl = cc.gl; + ccgl.enableVertexAttribs(cc.macro.VERTEX_ATTRIB_FLAG_POSITION | cc.macro.VERTEX_ATTRIB_FLAG_TEX_COORDS); this._shaderProgram.use(); //this._shaderProgram.setUniformsForBuiltins(); this._shaderProgram._setUniformForMVPMatrixWithMat4(target._renderCmd._stackMatrix); @@ -461,18 +462,18 @@ cc.Grid3D = cc.GridBase.extend(/** @lends cc.Grid3D# */{ // Attributes // // position - gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer); if (locDirty) gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW); gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 3, gl.FLOAT, false, 0, 0); // texCoords - gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer); if (locDirty) gl.bufferData(gl.ARRAY_BUFFER, this._texCoordinates, gl.DYNAMIC_DRAW); gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 0, 0); - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer); + ccgl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer); if (locDirty) gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indices, gl.STATIC_DRAW); gl.drawElements(gl.TRIANGLES, n * 6, gl.UNSIGNED_SHORT, 0); @@ -491,7 +492,7 @@ cc.Grid3D = cc.GridBase.extend(/** @lends cc.Grid3D# */{ }, calculateVertexPoints:function () { - var gl = cc._renderContext; + var gl = cc._renderContext, ccgl = cc.gl; var width = this._texture.getPixelWidth(); var height = this._texture.getPixelHeight(); @@ -558,11 +559,11 @@ cc.Grid3D = cc.GridBase.extend(/** @lends cc.Grid3D# */{ } this._originalVertices = new Float32Array(this._vertices); - gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer); gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW); - gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer); gl.bufferData(gl.ARRAY_BUFFER, this._texCoordinates, gl.DYNAMIC_DRAW); - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer); + ccgl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indices, gl.STATIC_DRAW); this._dirty = true; }, @@ -704,22 +705,22 @@ cc.TiledGrid3D = cc.GridBase.extend(/** @lends cc.TiledGrid3D# */{ // // Attributes // - var gl = cc._renderContext, locDirty = this._dirty; - cc.gl.enableVertexAttribs(cc.macro.VERTEX_ATTRIB_FLAG_POSITION | cc.macro.VERTEX_ATTRIB_FLAG_TEX_COORDS); + var gl = cc._renderContext, ccgl = cc.gl, locDirty = this._dirty; + ccgl.enableVertexAttribs(cc.macro.VERTEX_ATTRIB_FLAG_POSITION | cc.macro.VERTEX_ATTRIB_FLAG_TEX_COORDS); // position - gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer); if (locDirty) gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW); gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 3, gl.FLOAT, false, 0, this._vertices); // texCoords - gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer); if (locDirty) gl.bufferData(gl.ARRAY_BUFFER, this._texCoordinates, gl.DYNAMIC_DRAW); gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 0, this._texCoordinates); - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer); + ccgl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer); if (locDirty) gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indices, gl.STATIC_DRAW); gl.drawElements(gl.TRIANGLES, n * 6, gl.UNSIGNED_SHORT, 0); @@ -748,7 +749,7 @@ cc.TiledGrid3D = cc.GridBase.extend(/** @lends cc.TiledGrid3D# */{ this._texCoordinates = new Float32Array(numQuads * 8); this._indices = new Uint16Array(numQuads * 6); - var gl = cc._renderContext; + var gl = cc._renderContext, ccgl = cc.gl; if(this._verticesBuffer) gl.deleteBuffer(this._verticesBuffer); this._verticesBuffer = gl.createBuffer(); @@ -813,11 +814,11 @@ cc.TiledGrid3D = cc.GridBase.extend(/** @lends cc.TiledGrid3D# */{ } this._originalVertices = new Float32Array(this._vertices); - gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer); gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW); - gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer); gl.bufferData(gl.ARRAY_BUFFER, this._texCoordinates, gl.DYNAMIC_DRAW); - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer); + ccgl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indices, gl.DYNAMIC_DRAW); this._dirty = true; } diff --git a/cocos2d/motion-streak/CCSGMotionStreak.js b/cocos2d/motion-streak/CCSGMotionStreak.js index 2e0e8ec6b6..17ba37b06a 100644 --- a/cocos2d/motion-streak/CCSGMotionStreak.js +++ b/cocos2d/motion-streak/CCSGMotionStreak.js @@ -394,16 +394,17 @@ _ccsg.MotionStreak = _ccsg.Node.extend({ this._texCoords = new Float32Array(locMaxPoints * 4); this._colorPointer = new Uint8Array(locMaxPoints * 8); + var gl = cc._renderContext, ccgl = cc.gl; this._verticesBuffer = gl.createBuffer(); this._texCoordsBuffer = gl.createBuffer(); this._colorPointerBuffer = gl.createBuffer(); //bind buffer - gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer); gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW); - gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordsBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordsBuffer); gl.bufferData(gl.ARRAY_BUFFER, this._texCoords, gl.DYNAMIC_DRAW); - gl.bindBuffer(gl.ARRAY_BUFFER, this._colorPointerBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._colorPointerBuffer); gl.bufferData(gl.ARRAY_BUFFER, this._colorPointer, gl.DYNAMIC_DRAW); }, diff --git a/cocos2d/motion-streak/CCSGMotionStreakWebGLRenderCmd.js b/cocos2d/motion-streak/CCSGMotionStreakWebGLRenderCmd.js index fa12648aaa..6a8964c3bb 100644 --- a/cocos2d/motion-streak/CCSGMotionStreakWebGLRenderCmd.js +++ b/cocos2d/motion-streak/CCSGMotionStreakWebGLRenderCmd.js @@ -33,13 +33,14 @@ _ccsg.MotionStreak.WebGLRenderCmd = function(renderableObject){ _ccsg.MotionStreak.WebGLRenderCmd.prototype = Object.create(_ccsg.Node.WebGLRenderCmd.prototype); _ccsg.MotionStreak.WebGLRenderCmd.prototype.constructor = _ccsg.Sprite.WebGLRenderCmd; -_ccsg.MotionStreak.WebGLRenderCmd.prototype.rendering = function(ctx){ +_ccsg.MotionStreak.WebGLRenderCmd.prototype.rendering = function (ctx) { var node = this._node; if (node._nuPoints <= 1) return; if (node.texture && node.texture.isLoaded()) { ctx = ctx || cc._renderContext; + var ccgl = cc.gl; // update the color this._updateDisplayColor(); @@ -54,26 +55,26 @@ _ccsg.MotionStreak.WebGLRenderCmd.prototype.rendering = function(ctx){ this._shaderProgram.use(); this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix); - cc.gl.blendFunc(node._blendFunc.src, node._blendFunc.dst); + ccgl.blendFunc(node._blendFunc.src, node._blendFunc.dst); - cc.gl.bindTexture2DN(0, node.texture); + ccgl.bindTexture2DN(0, node.texture); - ctx.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); - ctx.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); - ctx.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_TEX_COORDS); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_TEX_COORDS); //position - ctx.bindBuffer(ctx.ARRAY_BUFFER, node._verticesBuffer); + ccgl.bindBuffer(ctx.ARRAY_BUFFER, node._verticesBuffer); ctx.bufferData(ctx.ARRAY_BUFFER, node._vertices, ctx.DYNAMIC_DRAW); ctx.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 2, ctx.FLOAT, false, 0, 0); //texcoords - ctx.bindBuffer(ctx.ARRAY_BUFFER, node._texCoordsBuffer); + ccgl.bindBuffer(ctx.ARRAY_BUFFER, node._texCoordsBuffer); ctx.bufferData(ctx.ARRAY_BUFFER, node._texCoords, ctx.DYNAMIC_DRAW); ctx.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_TEX_COORDS, 2, ctx.FLOAT, false, 0, 0); //colors - ctx.bindBuffer(ctx.ARRAY_BUFFER, node._colorPointerBuffer); + ccgl.bindBuffer(ctx.ARRAY_BUFFER, node._colorPointerBuffer); ctx.bufferData(ctx.ARRAY_BUFFER, node._colorPointer, ctx.DYNAMIC_DRAW); ctx.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_COLOR, 4, ctx.UNSIGNED_BYTE, true, 0, 0); diff --git a/cocos2d/particle/CCSGParticleSystemWebGLRenderCmd.js b/cocos2d/particle/CCSGParticleSystemWebGLRenderCmd.js index 6606d34567..85d8f7b4a0 100644 --- a/cocos2d/particle/CCSGParticleSystemWebGLRenderCmd.js +++ b/cocos2d/particle/CCSGParticleSystemWebGLRenderCmd.js @@ -186,7 +186,7 @@ proto.rendering = function (ctx) { if (!node._texture) return; - var gl = ctx || cc._renderContext; + var gl = ctx || cc._renderContext, ccgl = cc.gl; var wt = this._worldTransform, mat = this._matrix.mat; mat[0] = wt.a; @@ -199,22 +199,22 @@ proto.rendering = function (ctx) { this._shaderProgram.use(); this._shaderProgram._setUniformForMVPMatrixWithMat4(this._matrix); //; - cc.gl.bindTexture2DN(0, node._texture); - cc.gl.blendFuncForParticle(node._blendFunc.src, node._blendFunc.dst); + ccgl.bindTexture2DN(0, node._texture); + ccgl.blendFuncForParticle(node._blendFunc.src, node._blendFunc.dst); // // Using VBO without VAO // - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_TEX_COORDS); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_TEX_COORDS); - gl.bindBuffer(gl.ARRAY_BUFFER, this._buffersVBO[0]); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._buffersVBO[0]); gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 3, gl.FLOAT, false, 24, 0); // vertices gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_COLOR, 4, gl.UNSIGNED_BYTE, true, 24, 12); // colors gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 24, 16); // tex coords - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._buffersVBO[1]); + ccgl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._buffersVBO[1]); gl.drawElements(gl.TRIANGLES, node._particleIdx * 6, gl.UNSIGNED_SHORT, 0); }; @@ -333,11 +333,11 @@ proto._setupVBO = function(){ //gl.deleteBuffer(this._buffersVBO[0]); this._buffersVBO[0] = gl.createBuffer(); - gl.bindBuffer(gl.ARRAY_BUFFER, this._buffersVBO[0]); + cc.gl.bindBuffer(gl.ARRAY_BUFFER, this._buffersVBO[0]); gl.bufferData(gl.ARRAY_BUFFER, this._quadsArrayBuffer, gl.DYNAMIC_DRAW); this._buffersVBO[1] = gl.createBuffer(); - gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._buffersVBO[1]); + cc.gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._buffersVBO[1]); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indices, gl.STATIC_DRAW); //cc.checkGLErrorDebug(); @@ -370,7 +370,7 @@ proto._allocMemory = function(){ proto.postStep = function(){ var gl = cc._renderContext; - gl.bindBuffer(gl.ARRAY_BUFFER, this._buffersVBO[0]); + cc.gl.bindBuffer(gl.ARRAY_BUFFER, this._buffersVBO[0]); gl.bufferSubData(gl.ARRAY_BUFFER, 0, this._quadsArrayBuffer); }; diff --git a/cocos2d/progress-timer/CCProgressTimerWebGLRenderCmd.js b/cocos2d/progress-timer/CCProgressTimerWebGLRenderCmd.js index 0350f49778..c11916f03f 100644 --- a/cocos2d/progress-timer/CCProgressTimerWebGLRenderCmd.js +++ b/cocos2d/progress-timer/CCProgressTimerWebGLRenderCmd.js @@ -60,7 +60,7 @@ proto.rendering = function (ctx) { var node = this._node; - var context = ctx || cc._renderContext; + var context = ctx || cc._renderContext, ccgl = cc.gl; if (this._vertexDataCount === 0 || !node._sprite) return; @@ -68,13 +68,13 @@ this._shaderProgram._updateProjectionUniform(); var blendFunc = node._sprite._blendFunc; - cc.gl.blendFunc(blendFunc.src, blendFunc.dst); - cc.gl.bindTexture2D(node._sprite.texture); - context.bindBuffer(context.ARRAY_BUFFER, this._vertexWebGLBuffer); + ccgl.blendFunc(blendFunc.src, blendFunc.dst); + ccgl.bindTexture2D(node._sprite.texture); + ccgl.bindBuffer(context.ARRAY_BUFFER, this._vertexWebGLBuffer); - context.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); - context.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); - context.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_TEX_COORDS); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_TEX_COORDS); if (this._vertexDataDirty) { context.bufferSubData(context.ARRAY_BUFFER, 0, this._float32View); @@ -221,7 +221,7 @@ } // Init buffer data - gl.bindBuffer(gl.ARRAY_BUFFER, this._vertexWebGLBuffer); + cc.gl.bindBuffer(gl.ARRAY_BUFFER, this._vertexWebGLBuffer); gl.bufferData(gl.ARRAY_BUFFER, this._float32View, gl.DYNAMIC_DRAW); this._vertexDataCount = 0; diff --git a/cocos2d/shaders/CCGLStateCache.js b/cocos2d/shaders/CCGLStateCache.js index 2481fd1e86..080cbf868d 100644 --- a/cocos2d/shaders/CCGLStateCache.js +++ b/cocos2d/shaders/CCGLStateCache.js @@ -29,13 +29,22 @@ var _currentProjectionMatrix = -1; var macro = cc.macro; var ENABLE_GL_STATE_CACHE = macro.ENABLE_GL_STATE_CACHE; +// GL State Cache functions + +var ccgl = cc.gl = { + _vertexAttribPosition: false, + _vertexAttribColor: false, + _vertexAttribTexCoords: false +}; + var MAX_ACTIVETEXTURE = 0, _currentShaderProgram = 0, _currentBoundTexture = null, _blendingSource = 0, _blendingDest = 0, _GLServerState = 0, - _uVAO = 0; + _uVAO = 0, + _currBuffers = {}; if (ENABLE_GL_STATE_CACHE) { MAX_ACTIVETEXTURE = 16; _currentShaderProgram = -1; @@ -46,15 +55,9 @@ if (ENABLE_GL_STATE_CACHE) { if(macro.TEXTURE_ATLAS_USE_VAO) _uVAO = 0; - var _currBuffers = {}; - - // IE 10 WebGLRenderingContext does not exist - if (!window.WebGLRenderingContext) return; - - WebGLRenderingContext.prototype.glBindBuffer = WebGLRenderingContext.prototype.bindBuffer; - WebGLRenderingContext.prototype.bindBuffer = function (target, buffer) { + ccgl.bindBuffer = function (target, buffer) { if (_currBuffers[target] !== buffer) { - this.glBindBuffer(target, buffer); + cc._renderContext.bindBuffer(target, buffer); _currBuffers[target] = buffer; return false; } @@ -63,61 +66,55 @@ if (ENABLE_GL_STATE_CACHE) { } }; - WebGLRenderingContext.prototype.glEnableVertexAttribArray = WebGLRenderingContext.prototype.enableVertexAttribArray; - WebGLRenderingContext.prototype.enableVertexAttribArray = function (index) { + ccgl.enableVertexAttribArray = function (index) { if (index === macro.VERTEX_ATTRIB_FLAG_POSITION) { - if (!this._vertexAttribPosition) { - this.glEnableVertexAttribArray(index); - this._vertexAttribPosition = true; + if (!ccgl._vertexAttribPosition) { + cc._renderContext.enableVertexAttribArray(index); + ccgl._vertexAttribPosition = true; } } else if (index === macro.VERTEX_ATTRIB_FLAG_COLOR) { - if (!this._vertexAttribColor) { - this.glEnableVertexAttribArray(index); - this._vertexAttribColor = true; + if (!ccgl._vertexAttribColor) { + cc._renderContext.enableVertexAttribArray(index); + ccgl._vertexAttribColor = true; } } else if (index === macro.VERTEX_ATTRIB_FLAG_TEX_COORDS) { - if (!this._vertexAttribTexCoords) { - this.glEnableVertexAttribArray(index); - this._vertexAttribTexCoords = true; + if (!ccgl._vertexAttribTexCoords) { + cc._renderContext.enableVertexAttribArray(index); + ccgl._vertexAttribTexCoords = true; } } else { - this.glEnableVertexAttribArray(index); + cc._renderContext.enableVertexAttribArray(index); } }; - WebGLRenderingContext.prototype.glDisableVertexAttribArray = WebGLRenderingContext.prototype.disableVertexAttribArray; - WebGLRenderingContext.prototype.disableVertexAttribArray = function (index) { + ccgl.disableVertexAttribArray = function (index) { if (index === macro.VERTEX_ATTRIB_FLAG_COLOR) { - if (this._vertexAttribColor) { - this.glDisableVertexAttribArray(index); - this._vertexAttribColor = false; + if (ccgl._vertexAttribColor) { + cc._renderContext.disableVertexAttribArray(index); + ccgl._vertexAttribColor = false; } } else if (index === macro.VERTEX_ATTRIB_FLAG_TEX_COORDS) { - if (this._vertexAttribTexCoords) { - this.glDisableVertexAttribArray(index); - this._vertexAttribTexCoords = false; + if (ccgl._vertexAttribTexCoords) { + cc._renderContext.disableVertexAttribArray(index); + ccgl._vertexAttribTexCoords = false; } } else if (index !== 0) { - this.glDisableVertexAttribArray(index); + cc._renderContext.disableVertexAttribArray(index); } }; } -// GL State Cache functions - -cc.gl = {}; - /* * Invalidates the GL state cache.
* If cc.macro.ENABLE_GL_STATE_CACHE it will reset the GL state cache. * @function */ -cc.gl.invalidateStateCache = function () { +ccgl.invalidateStateCache = function () { cc.math.glFreeAll(); _currentProjectionMatrix = -1; if (ENABLE_GL_STATE_CACHE) { @@ -137,7 +134,7 @@ cc.gl.invalidateStateCache = function () { * @function * @param {WebGLProgram} program */ -cc.gl.useProgram = ENABLE_GL_STATE_CACHE ? function (program) { +ccgl.useProgram = ENABLE_GL_STATE_CACHE ? function (program) { if (program !== _currentShaderProgram) { _currentShaderProgram = program; cc._renderContext.useProgram(program); @@ -152,7 +149,7 @@ cc.gl.useProgram = ENABLE_GL_STATE_CACHE ? function (program) { * @function * @param {WebGLProgram} program */ -cc.gl.deleteProgram = function (program) { +ccgl.deleteProgram = function (program) { if (ENABLE_GL_STATE_CACHE) { if (program === _currentShaderProgram) _currentShaderProgram = -1; @@ -165,7 +162,7 @@ cc.gl.deleteProgram = function (program) { * @param {Number} sfactor * @param {Number} dfactor */ -cc.gl.setBlending = function (sfactor, dfactor) { +ccgl.setBlending = function (sfactor, dfactor) { var ctx = cc._renderContext; if ((sfactor === ctx.ONE) && (dfactor === ctx.ZERO)) { ctx.disable(ctx.BLEND); @@ -184,20 +181,20 @@ cc.gl.setBlending = function (sfactor, dfactor) { * @param {Number} sfactor * @param {Number} dfactor */ -cc.gl.blendFunc = ENABLE_GL_STATE_CACHE ? function (sfactor, dfactor) { +ccgl.blendFunc = ENABLE_GL_STATE_CACHE ? function (sfactor, dfactor) { if ((sfactor !== _blendingSource) || (dfactor !== _blendingDest)) { _blendingSource = sfactor; _blendingDest = dfactor; - cc.gl.setBlending(sfactor, dfactor); + ccgl.setBlending(sfactor, dfactor); } -} : cc.gl.setBlending; +} : ccgl.setBlending; /** * @function * @param {Number} sfactor * @param {Number} dfactor */ -cc.gl.blendFuncForParticle = function(sfactor, dfactor) { +ccgl.blendFuncForParticle = function(sfactor, dfactor) { if ((sfactor !== _blendingSource) || (dfactor !== _blendingDest)) { _blendingSource = sfactor; _blendingDest = dfactor; @@ -218,20 +215,20 @@ cc.gl.blendFuncForParticle = function(sfactor, dfactor) { * If cc.macro.ENABLE_GL_STATE_CACHE is disabled, it will just set the default blending mode using GL_FUNC_ADD. * @function */ -cc.gl.blendResetToCache = function () { +ccgl.blendResetToCache = function () { var ctx = cc._renderContext; ctx.blendEquation(ctx.FUNC_ADD); if (ENABLE_GL_STATE_CACHE) - cc.gl.setBlending(_blendingSource, _blendingDest); + ccgl.setBlending(_blendingSource, _blendingDest); else - cc.gl.setBlending(ctx.BLEND_SRC, ctx.BLEND_DST); + ccgl.setBlending(ctx.BLEND_SRC, ctx.BLEND_DST); }; /** * sets the projection matrix as dirty * @function */ -cc.gl.setProjectionMatrixDirty = function () { +ccgl.setProjectionMatrixDirty = function () { _currentProjectionMatrix = -1; }; @@ -241,8 +238,8 @@ cc.gl.setProjectionMatrixDirty = function () { * @function * @param {Texture2D} textureId */ -cc.gl.bindTexture2D = function (textureId) { - cc.gl.bindTexture2DN(0, textureId); +ccgl.bindTexture2D = function (textureId) { + ccgl.bindTexture2DN(0, textureId); }; /** @@ -252,7 +249,7 @@ cc.gl.bindTexture2D = function (textureId) { * @param {Number} textureUnit * @param {Texture2D} textureId */ -cc.gl.bindTexture2DN = ENABLE_GL_STATE_CACHE ? function (textureUnit, textureId) { +ccgl.bindTexture2DN = ENABLE_GL_STATE_CACHE ? function (textureUnit, textureId) { if (_currentBoundTexture[textureUnit] === textureId) return; _currentBoundTexture[textureUnit] = textureId; @@ -278,8 +275,8 @@ cc.gl.bindTexture2DN = ENABLE_GL_STATE_CACHE ? function (textureUnit, textureId) * @function * @param {Texture2D} textureId */ -cc.gl.deleteTexture2D = function (textureId) { - cc.gl.deleteTexture2DN(0, textureId); +ccgl.deleteTexture2D = function (textureId) { + ccgl.deleteTexture2DN(0, textureId); }; /** @@ -289,7 +286,7 @@ cc.gl.deleteTexture2D = function (textureId) { * @param {Number} textureUnit * @param {Texture2D} textureId */ -cc.gl.deleteTexture2DN = function (textureUnit, textureId) { +ccgl.deleteTexture2DN = function (textureUnit, textureId) { if (ENABLE_GL_STATE_CACHE) { if (textureId === _currentBoundTexture[ textureUnit ]) _currentBoundTexture[ textureUnit ] = -1; @@ -303,7 +300,7 @@ cc.gl.deleteTexture2DN = function (textureUnit, textureId) { * @function * @param {Number} vaoId */ -cc.gl.bindVAO = function (vaoId) { +ccgl.bindVAO = function (vaoId) { if (!macro.TEXTURE_ATLAS_USE_VAO) return; @@ -324,7 +321,7 @@ cc.gl.bindVAO = function (vaoId) { * @function * @param {Number} flags */ -cc.gl.enable = function (flags) { +ccgl.enable = function (flags) { if (ENABLE_GL_STATE_CACHE) { /*var enabled; diff --git a/cocos2d/shaders/index.js b/cocos2d/shaders/index.js index 49a1aeb233..109b3e24cd 100644 --- a/cocos2d/shaders/index.js +++ b/cocos2d/shaders/index.js @@ -23,7 +23,7 @@ THE SOFTWARE. ****************************************************************************/ +require('./CCGLStateCache'); require('./CCShaders'); require('./CCShaderCache'); -require('./CCGLProgram'); -require('./CCGLStateCache'); \ No newline at end of file +require('./CCGLProgram'); \ No newline at end of file diff --git a/cocos2d/shape-nodes/CCDrawNode.js b/cocos2d/shape-nodes/CCDrawNode.js index 2130e12826..5fe4c3ae39 100644 --- a/cocos2d/shape-nodes/CCDrawNode.js +++ b/cocos2d/shape-nodes/CCDrawNode.js @@ -589,17 +589,18 @@ cc.game.once(cc.game.EVENT_RENDERER_INITED, function () { _render:function () { var gl = cc._renderContext; + var ccgl = cc.gl; - gl.bindBuffer(gl.ARRAY_BUFFER, this._trianglesWebBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._trianglesWebBuffer); if (this._dirty) { gl.bufferData(gl.ARRAY_BUFFER, this._trianglesArrayBuffer, gl.STREAM_DRAW); this._dirty = false; } var triangleSize = cc.V2F_C4B_T2F.BYTES_PER_ELEMENT; - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); - gl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_TEX_COORDS); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_POSITION); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_COLOR); + ccgl.enableVertexAttribArray(cc.macro.VERTEX_ATTRIB_TEX_COORDS); // vertex gl.vertexAttribPointer(cc.macro.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, triangleSize, 0); diff --git a/extensions/cocostudio/armature/display/CCSkinWebGLRenderCmd.js b/extensions/cocostudio/armature/display/CCSkinWebGLRenderCmd.js index d6660ec766..fd277164cb 100644 --- a/extensions/cocostudio/armature/display/CCSkinWebGLRenderCmd.js +++ b/extensions/cocostudio/armature/display/CCSkinWebGLRenderCmd.js @@ -98,17 +98,17 @@ if (!node._textureLoaded) return; - var gl = ctx || cc._renderContext, locTexture = node._texture; + var gl = ctx || cc._renderContext, ccgl = cc.gl, locTexture = node._texture; if (locTexture && locTexture._textureLoaded) { this._shaderProgram.use(); this._shaderProgram.setUniformForModelViewAndProjectionMatrixWithMat4(); - cc.gl.blendFunc(node._blendFunc.src, node._blendFunc.dst); + ccgl.blendFunc(node._blendFunc.src, node._blendFunc.dst); //optimize performance for javascript - cc.gl.bindTexture2DN(0, locTexture); // = cc.gl.bindTexture2D(locTexture); - cc.gl.enableVertexAttribs(cc.macro.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX); + ccgl.bindTexture2DN(0, locTexture); // = ccgl.bindTexture2D(locTexture); + ccgl.enableVertexAttribs(cc.macro.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX); - gl.bindBuffer(gl.ARRAY_BUFFER, this._quadWebBuffer); + ccgl.bindBuffer(gl.ARRAY_BUFFER, this._quadWebBuffer); if (this._quadDirty) { gl.bufferData(gl.ARRAY_BUFFER, this._quad.arrayBuffer, gl.DYNAMIC_DRAW); this._quadDirty = false; From 8763920a3363fd49564c092bfdef51f9e7343543 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 3 Nov 2016 15:11:13 +0800 Subject: [PATCH 2/2] Improve cc.gl state cache functions --- cocos2d/core/renderer/RendererWebGL.js | 9 +- cocos2d/shaders/CCGLStateCache.js | 146 +++++++++++++------------ 2 files changed, 83 insertions(+), 72 deletions(-) diff --git a/cocos2d/core/renderer/RendererWebGL.js b/cocos2d/core/renderer/RendererWebGL.js index 6ae1238bbd..0b3f0eac8d 100644 --- a/cocos2d/core/renderer/RendererWebGL.js +++ b/cocos2d/core/renderer/RendererWebGL.js @@ -64,10 +64,9 @@ var _batchedInfo = { // https://github.com/Talisca/cocos2d-html5/commit/de731f16414eb9bcaa20480006897ca6576d362c function updateBuffer (numQuads) { var gl = cc._renderContext; - var ccgl = cc.gl; // Update index buffer size if (_indexBuffer) { - ccgl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBuffer); + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBuffer); _indexData = new Uint16Array(numQuads * 6); var currentQuad = 0; for (var i = 0, len = numQuads * 6; i < len; i += 6) { @@ -89,7 +88,7 @@ function updateBuffer (numQuads) { _vertexDataF32 = new Float32Array(_vertexData); _vertexDataUI32 = new Uint32Array(_vertexData); // Init buffer data - ccgl.bindBuffer(gl.ARRAY_BUFFER, _vertexBuffer); + gl.bindBuffer(gl.ARRAY_BUFFER, _vertexBuffer); gl.bufferData(gl.ARRAY_BUFFER, _vertexDataF32, gl.DYNAMIC_DRAW); } // Downsize by 100 to avoid vertex data overflow @@ -136,6 +135,8 @@ cc.rendererWebGL = { gl.disable(gl.CULL_FACE); gl.disable(gl.DEPTH_TEST); + cc.gl.initStateCache(gl); + this.mat4Identity = new cc.math.Matrix4(); this.mat4Identity.identity(); initQuadBuffer(BATCH_QUAD_COUNT); @@ -386,7 +387,7 @@ cc.rendererWebGL = { ccgl.blendFunc(_batchedInfo.blendSrc, _batchedInfo.blendDst); ccgl.bindTexture2DN(0, texture); // = ccgl.bindTexture2D(texture); - var _bufferchanged = ccgl.bindBuffer(gl.ARRAY_BUFFER, _vertexBuffer); + var _bufferchanged = !ccgl.bindBuffer(gl.ARRAY_BUFFER, _vertexBuffer); // upload the vertex data to the gl buffer if (uploadAll) { gl.bufferData(gl.ARRAY_BUFFER, _vertexDataF32, gl.DYNAMIC_DRAW); diff --git a/cocos2d/shaders/CCGLStateCache.js b/cocos2d/shaders/CCGLStateCache.js index 080cbf868d..0dbaea0c83 100644 --- a/cocos2d/shaders/CCGLStateCache.js +++ b/cocos2d/shaders/CCGLStateCache.js @@ -29,85 +29,95 @@ var _currentProjectionMatrix = -1; var macro = cc.macro; var ENABLE_GL_STATE_CACHE = macro.ENABLE_GL_STATE_CACHE; -// GL State Cache functions - -var ccgl = cc.gl = { - _vertexAttribPosition: false, - _vertexAttribColor: false, - _vertexAttribTexCoords: false -}; - var MAX_ACTIVETEXTURE = 0, _currentShaderProgram = 0, _currentBoundTexture = null, _blendingSource = 0, _blendingDest = 0, _GLServerState = 0, - _uVAO = 0, - _currBuffers = {}; -if (ENABLE_GL_STATE_CACHE) { - MAX_ACTIVETEXTURE = 16; - _currentShaderProgram = -1; - _currentBoundTexture = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]; - _blendingSource = -1; - _blendingDest = -1; - _GLServerState = 0; - if(macro.TEXTURE_ATLAS_USE_VAO) - _uVAO = 0; + _uVAO = 0; - ccgl.bindBuffer = function (target, buffer) { - if (_currBuffers[target] !== buffer) { - cc._renderContext.bindBuffer(target, buffer); - _currBuffers[target] = buffer; - return false; - } - else { - return true; - } - }; +// GL State Cache functions - ccgl.enableVertexAttribArray = function (index) { - if (index === macro.VERTEX_ATTRIB_FLAG_POSITION) { - if (!ccgl._vertexAttribPosition) { - cc._renderContext.enableVertexAttribArray(index); - ccgl._vertexAttribPosition = true; - } - } - else if (index === macro.VERTEX_ATTRIB_FLAG_COLOR) { - if (!ccgl._vertexAttribColor) { - cc._renderContext.enableVertexAttribArray(index); - ccgl._vertexAttribColor = true; - } - } - else if (index === macro.VERTEX_ATTRIB_FLAG_TEX_COORDS) { - if (!ccgl._vertexAttribTexCoords) { - cc._renderContext.enableVertexAttribArray(index); - ccgl._vertexAttribTexCoords = true; - } - } - else { - cc._renderContext.enableVertexAttribArray(index); - } - }; +var ccgl = cc.gl = { + _vertexAttribPosition: false, + _vertexAttribColor: false, + _vertexAttribTexCoords: false, - ccgl.disableVertexAttribArray = function (index) { - if (index === macro.VERTEX_ATTRIB_FLAG_COLOR) { - if (ccgl._vertexAttribColor) { + bindBuffer: null, + enableVertexAttribArray: null, + disableVertexAttribArray: null, + + initStateCache: ENABLE_GL_STATE_CACHE ? function () { + MAX_ACTIVETEXTURE = 16; + _currentShaderProgram = -1; + _currentBoundTexture = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]; + _blendingSource = -1; + _blendingDest = -1; + _GLServerState = 0; + if(macro.TEXTURE_ATLAS_USE_VAO) + _uVAO = 0; + + var _currBuffers = {}; + + this.bindBuffer = function (target, buffer) { + if (_currBuffers[target] !== buffer) { + cc._renderContext.bindBuffer(target, buffer); + _currBuffers[target] = buffer; + return false; + } + else { + return true; + } + }; + + this.enableVertexAttribArray = function (index) { + if (index === macro.VERTEX_ATTRIB_FLAG_POSITION) { + if (!this._vertexAttribPosition) { + cc._renderContext.enableVertexAttribArray(index); + this._vertexAttribPosition = true; + } + } + else if (index === macro.VERTEX_ATTRIB_FLAG_COLOR) { + if (!this._vertexAttribColor) { + cc._renderContext.enableVertexAttribArray(index); + this._vertexAttribColor = true; + } + } + else if (index === macro.VERTEX_ATTRIB_FLAG_TEX_COORDS) { + if (!this._vertexAttribTexCoords) { + cc._renderContext.enableVertexAttribArray(index); + this._vertexAttribTexCoords = true; + } + } + else { + cc._renderContext.enableVertexAttribArray(index); + } + }; + + this.disableVertexAttribArray = function (index) { + if (index === macro.VERTEX_ATTRIB_FLAG_COLOR) { + if (this._vertexAttribColor) { + cc._renderContext.disableVertexAttribArray(index); + this._vertexAttribColor = false; + } + } + else if (index === macro.VERTEX_ATTRIB_FLAG_TEX_COORDS) { + if (this._vertexAttribTexCoords) { + cc._renderContext.disableVertexAttribArray(index); + this._vertexAttribTexCoords = false; + } + } + else if (index !== 0) { cc._renderContext.disableVertexAttribArray(index); - ccgl._vertexAttribColor = false; } - } - else if (index === macro.VERTEX_ATTRIB_FLAG_TEX_COORDS) { - if (ccgl._vertexAttribTexCoords) { - cc._renderContext.disableVertexAttribArray(index); - ccgl._vertexAttribTexCoords = false; - } - } - else if (index !== 0) { - cc._renderContext.disableVertexAttribArray(index); - } - }; -} + }; + } : function (gl) { + this.bindBuffer = gl.bindBuffer.bind(gl); + this.enableVertexAttribArray = gl.enableVertexAttribArray.bind(gl); + this.disableVertexAttribArray = gl.disableVertexAttribArray.bind(gl); + } +}; /* * Invalidates the GL state cache.