Skip to content

Commit d93fdea

Browse files
committed
Draft implementation of auto batch
1 parent 9363587 commit d93fdea

20 files changed

+1042
-769
lines changed

CCBoot.js

+3
Original file line numberDiff line numberDiff line change
@@ -2595,6 +2595,9 @@ cc.game = /** @lends cc.game# */{
25952595
cc.shaderCache._init();
25962596
cc._drawingUtil = new cc.DrawingPrimitiveWebGL(this._renderContext);
25972597
cc.textureCache._initializingRenderer();
2598+
// cc.glExt = {};
2599+
// cc.glExt.instanced_arrays = gl.getExtension("ANGLE_instanced_arrays");
2600+
// cc.glExt.element_uint = gl.getExtension("OES_element_index_uint");
25982601
} else {
25992602
cc.renderer = cc.rendererCanvas;
26002603
this._renderContext = cc._renderContext = new cc.CanvasContextWrapper(localCanvas.getContext("2d"));

cocos2d/core/CCDirectorWebGL.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ cc.game.addEventListener(cc.game.EVENT_RENDERER_INITED, function () {
8282
size.width - ox,
8383
-oy,
8484
size.height - oy,
85-
-1024, 1024);
85+
-1, 1);
8686
cc.kmGLMultMatrix(orthoMatrix);
8787
cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
8888
cc.kmGLLoadIdentity();

cocos2d/core/base-nodes/CCNodeCanvasRenderCmd.js

+38-10
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cc.CustomRenderCmd = function (target, func) {
3232
if (!this._callback)
3333
return;
3434
this._callback.call(this._target, ctx, scaleX, scaleY);
35-
}
35+
};
3636
};
3737

3838
cc.Node._dirtyFlags = {transformDirty: 1 << 0, visibleDirty: 1 << 1, colorDirty: 1 << 2, opacityDirty: 1 << 3, cacheDirty: 1 << 4,
@@ -56,6 +56,9 @@ cc.Node.RenderCmd = function(renderable){
5656
this._cascadeOpacityEnabledDirty = false;
5757

5858
this._curLevel = -1;
59+
60+
this._minZ = 0;
61+
this._maxZ = 0;
5962
};
6063

6164
cc.Node.RenderCmd.prototype = {
@@ -368,24 +371,50 @@ cc.Node.RenderCmd.prototype = {
368371
},
369372

370373
visitChildren: function(){
374+
var renderer = cc.renderer;
371375
var node = this._node;
372-
var i, children = node._children, child;
376+
var i, children = node._children, child, cmd;
373377
var len = children.length;
378+
var minZ = Number.MAX_VALUE;
379+
var maxZ = -Number.MAX_VALUE;
374380
if (len > 0) {
375381
node.sortAllChildren();
376382
// draw children zOrder < 0
377383
for (i = 0; i < len; i++) {
378384
child = children[i];
379-
if (child._localZOrder < 0)
380-
child._renderCmd.visit(this);
381-
else
385+
if (child._localZOrder < 0) {
386+
cmd = child._renderCmd;
387+
cmd.visit(this);
388+
// minZ = Math.min(minZ, child._minZ);
389+
// maxZ = Math.max(maxZ, child._maxZ);
390+
}
391+
else {
382392
break;
393+
}
383394
}
384-
cc.renderer.pushRenderCommand(this);
385-
for (; i < len; i++)
386-
children[i]._renderCmd.visit(this);
395+
396+
var z = renderer.assignedZ;
397+
node._vertexZ = z;
398+
renderer.assignedZ += renderer.assignedZStep;
399+
400+
// minZ = Math.min(minZ,z);
401+
// maxZ = Math.max(maxZ,z);
402+
403+
renderer.pushRenderCommand(this);
404+
for (; i < len; i++) {
405+
child = children[i];
406+
child._renderCmd.visit(this);
407+
// minZ = Math.min(minZ, child._minZ);
408+
// maxZ = Math.max(maxZ, child._maxZ);
409+
}
410+
411+
// node._minZ = minZ;
412+
// node._maxZ = maxZ;
387413
} else {
388-
cc.renderer.pushRenderCommand(this);
414+
node._vertexZ = renderer.assignedZ;
415+
renderer.assignedZ += renderer.assignedZStep;
416+
417+
renderer.pushRenderCommand(this);
389418
}
390419
this._dirtyFlag = 0;
391420
}
@@ -399,7 +428,6 @@ cc.Node.RenderCmd.prototype = {
399428
cc.Node.RenderCmd.call(this, renderable);
400429
this._cachedParent = null;
401430
this._cacheDirty = false;
402-
403431
};
404432

405433
var proto = cc.Node.CanvasRenderCmd.prototype = Object.create(cc.Node.RenderCmd.prototype);

cocos2d/core/base-nodes/CCNodeWebGLRenderCmd.js

+6-27
Original file line numberDiff line numberDiff line change
@@ -80,37 +80,16 @@
8080
t4x4Mat[5] = trans.d;
8181
t4x4Mat[13] = trans.ty;
8282

83-
// Update Z vertex manually
84-
t4x4Mat[14] = node._vertexZ;
85-
8683
//optimize performance for Javascript
8784
cc.kmMat4Multiply(stackMatrix, parentMatrix, t4x4);
8885

89-
// XXX: Expensive calls. Camera should be integrated into the cached affine matrix
90-
if (node._camera !== null && !(node.grid !== null && node.grid.isActive())) {
91-
var apx = this._anchorPointInPoints.x, apy = this._anchorPointInPoints.y;
92-
var translate = (apx !== 0.0 || apy !== 0.0);
93-
if (translate){
94-
if(!cc.SPRITEBATCHNODE_RENDER_SUBPIXEL) {
95-
apx = 0 | apx;
96-
apy = 0 | apy;
97-
}
98-
//cc.kmGLTranslatef(apx, apy, 0);
99-
var translation = cc.math.Matrix4.createByTranslation(apx, apy, 0, t4x4); //t4x4 as a temp matrix
100-
stackMatrix.multiply(translation);
101-
102-
node._camera._locateForRenderer(stackMatrix);
103-
104-
//cc.kmGLTranslatef(-apx, -apy, 0); optimize at here : kmGLTranslatef
105-
translation = cc.math.Matrix4.createByTranslation(-apx, -apy, 0, translation);
106-
stackMatrix.multiply(translation);
107-
t4x4.identity(); //reset t4x4;
108-
} else {
109-
node._camera._locateForRenderer(stackMatrix);
110-
}
111-
}
112-
if(!recursive || !node._children || node._children.length === 0)
86+
// Update Z depth
87+
t4x4Mat[14] = node._vertexZ;
88+
89+
if (!recursive || !node._children) {
11390
return;
91+
}
92+
11493
var i, len, locChildren = node._children;
11594
for(i = 0, len = locChildren.length; i< len; i++){
11695
locChildren[i]._renderCmd.transform(this, recursive);

cocos2d/core/platform/CCConfig.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ window["CocosEngine"] = cc.ENGINE_VERSION = "Cocos2d-JS v3.11";
5656
* @constant
5757
* @type {Number}
5858
*/
59-
cc.FIX_ARTIFACTS_BY_STRECHING_TEXEL = 0;
59+
cc.FIX_ARTIFACTS_BY_STRECHING_TEXEL = 1;
6060

6161
/**
6262
* Position of the FPS (Default: 0,0 (bottom-left corner))<br/>

cocos2d/core/platform/CCMacro.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,27 @@ cc.VERTEX_ATTRIB_TEX_COORDS = 2;
566566
* @constant
567567
* @type {Number}
568568
*/
569-
cc.VERTEX_ATTRIB_MAX = 3;
569+
cc.VERTEX_ATTRIB_MVMAT0 = 3;
570+
/**
571+
* @constant
572+
* @type {Number}
573+
*/
574+
cc.VERTEX_ATTRIB_MVMAT1 = 4;
575+
/**
576+
* @constant
577+
* @type {Number}
578+
*/
579+
cc.VERTEX_ATTRIB_MVMAT2 = 5;
580+
/**
581+
* @constant
582+
* @type {Number}
583+
*/
584+
cc.VERTEX_ATTRIB_MVMAT3 = 6;
585+
/**
586+
* @constant
587+
* @type {Number}
588+
*/
589+
cc.VERTEX_ATTRIB_MAX = 7;
570590

571591
//------------Uniforms------------------
572592
/**
@@ -626,6 +646,11 @@ cc.SHADER_POSITION_TEXTURECOLOR = "ShaderPositionTextureColor";
626646
* @type {String}
627647
*/
628648
cc.SHADER_POSITION_TEXTURECOLORALPHATEST = "ShaderPositionTextureColorAlphaTest";
649+
/**
650+
* @constant
651+
* @type {String}
652+
*/
653+
cc.SHADER_POSITION_TEXTURECOLORALPHATEST_BATCHED = "ShaderPositionTextureColorAlphaTestBatched";
629654
/**
630655
* @constant
631656
* @type {String}
@@ -720,6 +745,11 @@ cc.ATTRIBUTE_NAME_POSITION = "a_position";
720745
* @type {String}
721746
*/
722747
cc.ATTRIBUTE_NAME_TEX_COORD = "a_texCoord";
748+
/**
749+
* @constant
750+
* @type {String}
751+
*/
752+
cc.ATTRIBUTE_NAME_MVMAT = "a_mvMatrix";
723753

724754

725755
/**

0 commit comments

Comments
 (0)