Skip to content

Commit df32063

Browse files
committed
Merge pull request #3268 from pandamicro/autobatch
Add GlobalVertexBuffer for Sprites to share
2 parents 7c3b0d8 + 08eb9a4 commit df32063

14 files changed

+449
-187
lines changed

Diff for: CCBoot.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,7 @@ var _initSys = function () {
18261826
}
18271827
}
18281828
catch (e) {}
1829+
tmpCanvas = null;
18291830
}
18301831

18311832
/**
@@ -1936,6 +1937,9 @@ var _initSys = function () {
19361937
};
19371938
_initSys();
19381939

1940+
_tmpCanvas1 = null;
1941+
_tmpCanvas2 = null;
1942+
19391943
//to make sure the cc.log, cc.warn, cc.error and cc.assert would not throw error before init by debugger mode.
19401944
cc.log = cc.warn = cc.error = cc.assert = function () {
19411945
};
@@ -2068,7 +2072,7 @@ cc.initEngine = function (config, cb) {
20682072

20692073
document.body ? _load(config) : cc._addEventListener(window, 'load', _windowLoaded, false);
20702074
_engineInitCalled = true;
2071-
}
2075+
};
20722076

20732077
})();
20742078
//+++++++++++++++++++++++++Engine initialization function end+++++++++++++++++++++++++++++
@@ -2592,12 +2596,10 @@ cc.game = /** @lends cc.game# */{
25922596
if (this._renderContext) {
25932597
cc.renderer = cc.rendererWebGL;
25942598
win.gl = this._renderContext; // global variable declared in CCMacro.js
2599+
cc.renderer.initQuadIndexBuffer();
25952600
cc.shaderCache._init();
25962601
cc._drawingUtil = new cc.DrawingPrimitiveWebGL(this._renderContext);
25972602
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");
26012603
} else {
26022604
cc.renderer = cc.rendererCanvas;
26032605
this._renderContext = cc._renderContext = new cc.CanvasContextWrapper(localCanvas.getContext("2d"));

Diff for: cocos2d/core/labelttf/CCLabelTTF.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
291291
if (a.r != null && a.g != null && a.b != null && a.a != null) {
292292
this._enableShadow(a, b, c);
293293
} else {
294-
this._enableShadowNoneColor(a, b, c, d)
294+
this._enableShadowNoneColor(a, b, c, d);
295295
}
296296
},
297297

Diff for: cocos2d/core/labelttf/CCLabelTTFCanvasRenderCmd.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
211211
context.setTransform(1, 0, 0, 1, locStatus.contextTransform.x, locStatus.contextTransform.y);
212212
var xOffset = locStatus.xOffset;
213213
var yOffsetArray = locStatus.OffsetYArray;
214-
this.drawLabels(context, xOffset, yOffsetArray)
214+
this.drawLabels(context, xOffset, yOffsetArray);
215215
};
216216

217217
proto._checkWarp = function (strArr, i, maxWidth) {
@@ -350,7 +350,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
350350
context.fillText(line, xOffset, yOffsetArray[i]);
351351
}
352352
cc.g_NumberOfDraws++;
353-
}
353+
};
354354
})();
355355

356356
(function(){

Diff for: cocos2d/core/layers/CCLayerCanvasRenderCmd.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
for(var i = 0, len = children.length; i < len; i++)
9898
children[i]._renderCmd._setCachedParent(this);
9999

100-
if (!this._bakeSprite){
100+
if (!this._bakeSprite) {
101101
this._bakeSprite = new cc.BakeSprite();
102102
this._bakeSprite.setAnchorPoint(0,0);
103103
}

Diff for: cocos2d/core/platform/CCMacro.js

+6-21
Original file line numberDiff line numberDiff line change
@@ -562,26 +562,6 @@ cc.VERTEX_ATTRIB_COLOR = 1;
562562
* @type {Number}
563563
*/
564564
cc.VERTEX_ATTRIB_TEX_COORDS = 2;
565-
/**
566-
* @constant
567-
* @type {Number}
568-
*/
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;
585565
/**
586566
* @constant
587567
* @type {Number}
@@ -650,12 +630,17 @@ cc.SHADER_POSITION_TEXTURECOLORALPHATEST = "ShaderPositionTextureColorAlphaTest"
650630
* @constant
651631
* @type {String}
652632
*/
653-
cc.SHADER_POSITION_TEXTURECOLORALPHATEST_BATCHED = "ShaderPositionTextureColorAlphaTestBatched";
633+
cc.SHADER_SPRITE_POSITION_TEXTURECOLORALPHATEST = "ShaderSpritePositionTextureColorAlphaTest";
654634
/**
655635
* @constant
656636
* @type {String}
657637
*/
658638
cc.SHADER_POSITION_COLOR = "ShaderPositionColor";
639+
/**
640+
* @constant
641+
* @type {String}
642+
*/
643+
cc.SHADER_SPRITE_POSITION_COLOR = "ShaderSpritePositionColor";
659644
/**
660645
* @constant
661646
* @type {String}

Diff for: cocos2d/core/renderer/GlobalVertexBuffer.js

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/****************************************************************************
2+
Copyright (c) 2016 Chukong Technologies Inc.
3+
4+
http://www.cocos2d-x.org
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
****************************************************************************/
24+
25+
var GlobalVertexBuffer = (function () {
26+
27+
var VERTICES_SIZE = 888;
28+
29+
var GlobalVertexBuffer = function (gl) {
30+
// WebGL buffer
31+
this.gl = gl;
32+
this.vertexBuffer = gl.createBuffer();
33+
34+
this.size = VERTICES_SIZE;
35+
this.byteLength = VERTICES_SIZE * 4 * cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT;
36+
37+
// buffer data and views
38+
this.data = new ArrayBuffer(this.byteLength);
39+
this.dataArray = new Float32Array(this.data);
40+
41+
// Init buffer data
42+
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
43+
gl.bufferData(gl.ARRAY_BUFFER, this.byteLength, gl.DYNAMIC_DRAW);
44+
45+
this._dirty = false;
46+
this._spaces = {
47+
0: this.byteLength
48+
};
49+
};
50+
GlobalVertexBuffer.prototype = {
51+
constructor: GlobalVertexBuffer,
52+
53+
allocBuffer: function (offset, size) {
54+
var space = this._spaces[offset];
55+
if (space && space >= size) {
56+
// Remove the space
57+
delete this._spaces[offset];
58+
if (space > size) {
59+
var newOffset = offset + size;
60+
this._spaces[newOffset] = space - size;
61+
}
62+
return true;
63+
}
64+
else {
65+
return false;
66+
}
67+
},
68+
69+
requestBuffer: function (size) {
70+
var key, offset, available;
71+
for (key in this._spaces) {
72+
offset = parseInt(key);
73+
available = this._spaces[key];
74+
if (available >= size && this.allocBuffer(offset, size)) {
75+
return {
76+
buffer: this,
77+
offset: offset,
78+
size: size
79+
};
80+
}
81+
}
82+
return null;
83+
},
84+
85+
freeBuffer: function (offset, size) {
86+
var spaces = this._spaces;
87+
var i, key, end;
88+
// Merge with previous space
89+
for (key in spaces) {
90+
i = parseInt(key);
91+
if (i > offset) {
92+
break;
93+
}
94+
if (i + spaces[key] >= offset) {
95+
size = size + offset - i;
96+
offset = i;
97+
break;
98+
}
99+
}
100+
101+
end = offset + size;
102+
// Merge with next space
103+
if (this._spaces[end]) {
104+
size += this._spaces[end];
105+
delete this._spaces[end];
106+
}
107+
108+
this._spaces[offset] = size;
109+
},
110+
111+
setDirty: function () {
112+
this._dirty = true;
113+
},
114+
115+
update: function () {
116+
if (this._dirty) {
117+
this.gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
118+
// Note: Can memorize different dirty zones and update them separately, maybe faster
119+
this.gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.dataArray);
120+
this._dirty = false;
121+
}
122+
},
123+
124+
destroy: function () {
125+
this.gl.deleteBuffer(this.vertexBuffer);
126+
127+
this.data = null;
128+
this.positions = null;
129+
this.colors = null;
130+
this.texCoords = null;
131+
132+
this.vertexBuffer = null;
133+
}
134+
};
135+
136+
return GlobalVertexBuffer;
137+
138+
})();

0 commit comments

Comments
 (0)