Skip to content

Commit 53c59a5

Browse files
committed
Avoid unnecessary vertexAttribPointer invocation for unified buffer
1 parent 412f1bd commit 53c59a5

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

Diff for: cocos2d/core/sprites/CCSpriteWebGLRenderCmd.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
//Sprite's WebGL render command
2626
(function() {
2727

28+
var _resetPointers = true;
29+
2830
cc.Sprite.WebGLRenderCmd = function (renderable) {
2931
cc.Node.WebGLRenderCmd.call(this, renderable);
3032
this._needDraw = true;
@@ -48,7 +50,8 @@
4850
// this._bufferOffset = 0;
4951
// this._quad = new cc.V3F_C4B_T2F_Quad(null, null, null, null, this._buffer, this._bufferOffset);
5052
// this._float32View = new Float32Array(this._buffer, this._bufferOffset, length / 4);
51-
// Init buffer
53+
// this._uint32View = new Uint32Array(this._buffer, this._bufferOffset, length / 4);
54+
// // Init buffer
5255
// var gl = cc._renderContext;
5356
// this._glBuffer = gl.createBuffer();
5457
// gl.bindBuffer(gl.ARRAY_BUFFER, this._glBuffer);
@@ -534,36 +537,37 @@
534537

535538
cc.glBlendFunc(node._blendFunc.src, node._blendFunc.dst);
536539
//optimize performance for javascript
537-
cc.glBindTexture2DN(0, locTexture); // = cc.glBindTexture2D(locTexture);
538-
cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
540+
cc.glBindTexture2DN(0, locTexture);
539541

540-
gl.bindBuffer(gl.ARRAY_BUFFER, this._buffer.vertexBuffer);
542+
var _bufferchanged = !gl.bindBuffer(gl.ARRAY_BUFFER, this._buffer.vertexBuffer);
541543
// if (this._bufferDirty) {
542544
// gl.bufferSubData(gl.ARRAY_BUFFER, this._bufferOffset, this._float32View);
543545
// this._bufferDirty = false;
544546
// }
545-
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 24, this._bufferOffset); //cc.VERTEX_ATTRIB_POSITION
546-
gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, true, 24, this._bufferOffset + 12); //cc.VERTEX_ATTRIB_COLOR
547-
gl.vertexAttribPointer(2, 2, gl.FLOAT, false, 24, this._bufferOffset + 16); //cc.VERTEX_ATTRIB_TEX_COORDS
548-
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
547+
if (_resetPointers || _bufferchanged) {
548+
cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
549+
gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 3, gl.FLOAT, false, 24, 0);
550+
gl.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, gl.UNSIGNED_BYTE, true, 24, 12);
551+
gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 24, 16);
552+
}
553+
gl.drawArrays(gl.TRIANGLE_STRIP, this._bufferOffset / (this.vertexBytesPerUnit/4), 4);
549554
}
550555
} else {
551556
program.use();
552557
program._setUniformForMVPMatrixWithMat4(this._stackMatrix);
553558

554559
cc.glBlendFunc(node._blendFunc.src, node._blendFunc.dst);
555-
cc.glBindTexture2D(null);
556-
557-
cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_COLOR);
558560

559561
gl.bindBuffer(gl.ARRAY_BUFFER, this._buffer.vertexBuffer);
560562
// if (this._bufferDirty) {
561563
// gl.bufferSubData(gl.ARRAY_BUFFER, this._bufferOffset, this._float32View);
562564
// this._bufferDirty = false;
563565
// }
564-
gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 3, gl.FLOAT, false, 24, this._bufferOffset);
565-
gl.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, gl.UNSIGNED_BYTE, true, 24, this._bufferOffset + 12);
566-
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
566+
cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_COLOR);
567+
gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 3, gl.FLOAT, false, 24, 0);
568+
gl.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, gl.UNSIGNED_BYTE, true, 24, 12);
569+
gl.drawArrays(gl.TRIANGLE_STRIP, this._bufferOffset / (this.vertexBytesPerUnit/4), 4);
570+
_resetPointers = true;
567571
}
568572
cc.g_NumberOfDraws++;
569573

Diff for: cocos2d/shaders/CCGLStateCache.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ if (cc.ENABLE_GL_STATE_CACHE) {
3939
cc._GLServerState = 0;
4040
if(cc.TEXTURE_ATLAS_USE_VAO)
4141
cc._uVAO = 0;
42+
43+
var _currBuffers = {};
44+
var _currBuffer;
45+
46+
WebGLRenderingContext.prototype.glBindBuffer = WebGLRenderingContext.prototype.bindBuffer;
47+
WebGLRenderingContext.prototype.bindBuffer = function (target, buffer) {
48+
if (_currBuffers[target] !== buffer) {
49+
_currBuffers[target] = buffer;
50+
this.glBindBuffer(target, buffer);
51+
}
52+
53+
if (!_currBuffer || _currBuffer !== buffer) {
54+
_currBuffer = buffer;
55+
return false;
56+
}
57+
else {
58+
return true;
59+
}
60+
};
4261
}
4362

4463
// GL State Cache functions
@@ -81,7 +100,7 @@ cc.glUseProgram = function (program) {
81100
if(!cc.ENABLE_GL_STATE_CACHE){
82101
cc.glUseProgram = function (program) {
83102
cc._renderContext.useProgram(program);
84-
}
103+
};
85104
}
86105

87106
/**
@@ -150,9 +169,9 @@ cc.glBlendFuncForParticle = function(sfactor, dfactor) {
150169
}
151170
};
152171

153-
if(!cc.ENABLE_GL_STATE_CACHE){
172+
if (!cc.ENABLE_GL_STATE_CACHE) {
154173
cc.glBlendFunc = cc.setBlending;
155-
};
174+
}
156175

157176
/**
158177
* Resets the blending mode back to the cached state in case you used glBlendFuncSeparate() or glBlendEquation().<br/>

0 commit comments

Comments
 (0)