Skip to content

Commit

Permalink
Add GlobalVertexBuffer for Sprites to share
Browse files Browse the repository at this point in the history
  • Loading branch information
pandamicro committed Apr 20, 2016
1 parent 5c252e6 commit 4c0cae6
Show file tree
Hide file tree
Showing 14 changed files with 422 additions and 179 deletions.
7 changes: 6 additions & 1 deletion CCBoot.js
Expand Up @@ -1826,6 +1826,7 @@ var _initSys = function () {
}
}
catch (e) {}
tmpCanvas = null;
}

/**
Expand Down Expand Up @@ -1936,6 +1937,9 @@ var _initSys = function () {
};
_initSys();

_tmpCanvas1 = null;
_tmpCanvas2 = null;

//to make sure the cc.log, cc.warn, cc.error and cc.assert would not throw error before init by debugger mode.
cc.log = cc.warn = cc.error = cc.assert = function () {
};
Expand Down Expand Up @@ -2068,7 +2072,7 @@ cc.initEngine = function (config, cb) {

document.body ? _load(config) : cc._addEventListener(window, 'load', _windowLoaded, false);
_engineInitCalled = true;
}
};

})();
//+++++++++++++++++++++++++Engine initialization function end+++++++++++++++++++++++++++++
Expand Down Expand Up @@ -2592,6 +2596,7 @@ cc.game = /** @lends cc.game# */{
if (this._renderContext) {
cc.renderer = cc.rendererWebGL;
win.gl = this._renderContext; // global variable declared in CCMacro.js
cc.renderer.initQuadIndexBuffer();
cc.shaderCache._init();
cc._drawingUtil = new cc.DrawingPrimitiveWebGL(this._renderContext);
cc.textureCache._initializingRenderer();
Expand Down
2 changes: 1 addition & 1 deletion cocos2d/core/labelttf/CCLabelTTF.js
Expand Up @@ -291,7 +291,7 @@ cc.LabelTTF = cc.Sprite.extend(/** @lends cc.LabelTTF# */{
if (a.r != null && a.g != null && a.b != null && a.a != null) {
this._enableShadow(a, b, c);
} else {
this._enableShadowNoneColor(a, b, c, d)
this._enableShadowNoneColor(a, b, c, d);
}
},

Expand Down
4 changes: 2 additions & 2 deletions cocos2d/core/labelttf/CCLabelTTFCanvasRenderCmd.js
Expand Up @@ -211,7 +211,7 @@ cc.LabelTTF._firsrEnglish = /^[a-zA-Z0-9ÄÖÜäöüßéèçàùêâîôû]/;
context.setTransform(1, 0, 0, 1, locStatus.contextTransform.x, locStatus.contextTransform.y);
var xOffset = locStatus.xOffset;
var yOffsetArray = locStatus.OffsetYArray;
this.drawLabels(context, xOffset, yOffsetArray)
this.drawLabels(context, xOffset, yOffsetArray);
};

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

(function(){
Expand Down
2 changes: 1 addition & 1 deletion cocos2d/core/layers/CCLayerCanvasRenderCmd.js
Expand Up @@ -97,7 +97,7 @@
for(var i = 0, len = children.length; i < len; i++)
children[i]._renderCmd._setCachedParent(this);

if (!this._bakeSprite){
if (!this._bakeSprite) {
this._bakeSprite = new cc.BakeSprite();
this._bakeSprite.setAnchorPoint(0,0);
}
Expand Down
27 changes: 6 additions & 21 deletions cocos2d/core/platform/CCMacro.js
Expand Up @@ -562,26 +562,6 @@ cc.VERTEX_ATTRIB_COLOR = 1;
* @type {Number}
*/
cc.VERTEX_ATTRIB_TEX_COORDS = 2;
/**
* @constant
* @type {Number}
*/
cc.VERTEX_ATTRIB_MVMAT0 = 3;
/**
* @constant
* @type {Number}
*/
cc.VERTEX_ATTRIB_MVMAT1 = 4;
/**
* @constant
* @type {Number}
*/
cc.VERTEX_ATTRIB_MVMAT2 = 5;
/**
* @constant
* @type {Number}
*/
cc.VERTEX_ATTRIB_MVMAT3 = 6;
/**
* @constant
* @type {Number}
Expand Down Expand Up @@ -650,12 +630,17 @@ cc.SHADER_POSITION_TEXTURECOLORALPHATEST = "ShaderPositionTextureColorAlphaTest"
* @constant
* @type {String}
*/
cc.SHADER_POSITION_TEXTURECOLORALPHATEST_BATCHED = "ShaderPositionTextureColorAlphaTestBatched";
cc.SHADER_SPRITE_POSITION_TEXTURECOLORALPHATEST = "ShaderSpritePositionTextureColorAlphaTest";
/**
* @constant
* @type {String}
*/
cc.SHADER_POSITION_COLOR = "ShaderPositionColor";
/**
* @constant
* @type {String}
*/
cc.SHADER_SPRITE_POSITION_COLOR = "ShaderSpritePositionColor";
/**
* @constant
* @type {String}
Expand Down
138 changes: 138 additions & 0 deletions cocos2d/core/renderer/GlobalVertexBuffer.js
@@ -0,0 +1,138 @@
/****************************************************************************
Copyright (c) 2016 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

var GlobalVertexBuffer = (function () {

var VERTICES_SIZE = 888;

var GlobalVertexBuffer = function (gl) {
// WebGL buffer
this.gl = gl;
this.vertexBuffer = gl.createBuffer();

this.size = VERTICES_SIZE;
this.byteLength = VERTICES_SIZE * 4 * cc.V3F_C4B_T2F_Quad.BYTES_PER_ELEMENT;

// buffer data and views
this.data = new ArrayBuffer(this.byteLength);
this.dataArray = new Float32Array(this.data);

// Init buffer data
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, this.byteLength, gl.DYNAMIC_DRAW);

this._dirty = false;
this._spaces = {
0: this.byteLength
};
};
GlobalVertexBuffer.prototype = {
constructor: GlobalVertexBuffer,

allocBuffer: function (offset, size) {
var space = this._spaces[offset];
if (space && space >= size) {
// Remove the space
delete this._spaces[offset];
if (space > size) {
var newOffset = offset + size;
this._spaces[newOffset] = space - size;
}
return true;
}
else {
return false;
}
},

requestBuffer: function (size) {
var key, offset, available;
for (key in this._spaces) {
offset = parseInt(key);
available = this._spaces[key];
if (available >= size && this.allocBuffer(offset, size)) {
return {
buffer: this,
offset: offset,
size: size
};
}
}
return null;
},

freeBuffer: function (offset, size) {
var spaces = this._spaces;
var i, key, end;
// Merge with previous space
for (key in spaces) {
i = parseInt(key);
if (i > offset) {
break;
}
if (i + spaces[key] >= offset) {
size = size + offset - i;
offset = i;
break;
}
}

end = offset + size;
// Merge with next space
if (this._spaces[end]) {
size += this._spaces[end];
delete this._spaces[end];
}

this._spaces[offset] = size;
},

setDirty: function () {
this._dirty = true;
},

update: function () {
if (this._dirty) {
this.gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
// Note: Can memorize different dirty zones and update them separately, maybe faster
this.gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.dataArray);
this._dirty = false;
}
},

destroy: function () {
this.gl.deleteBuffer(this.vertexBuffer);

this.data = null;
this.positions = null;
this.colors = null;
this.texCoords = null;

this.vertexBuffer = null;
}
};

return GlobalVertexBuffer;

})();

0 comments on commit 4c0cae6

Please sign in to comment.