Merge pull request #2559 from minggo/optimize

Optimize performance
This commit is contained in:
pandamicro
2018-04-19 11:33:57 +08:00
committed by GitHub
7 changed files with 185 additions and 26 deletions
+39
View File
@@ -0,0 +1,39 @@
let _stageOffset = 0;
let _name2stageID = {};
var config = {
addStage: function (name) {
// already added
if (_name2stageID[name] !== undefined) {
return;
}
let stageID = 1 << _stageOffset;
_name2stageID[name] = stageID;
_stageOffset += 1;
renderer.addStage(name);
},
stageID: function (name) {
let id = _name2stageID[name];
if (id === undefined) {
return -1;
}
return id;
},
stageIDs: function (nameList) {
let key = 0;
for (let i = 0; i < nameList.length; ++i) {
let id = _name2stageID[nameList[i]];
if (id !== undefined) {
key |= id;
}
}
return key;
}
};
module.exports = config;
+1
View File
@@ -17,6 +17,7 @@ class Effect {
}
this._nativeObj = new renderer.EffectNative(techniqueObjs, properties, defines);
this._nativePtr = this._nativeObj.self();
// TODO: check if params is valid for current technique???
}
+3 -1
View File
@@ -49,13 +49,15 @@ _p._ctor = function(device, options) {
_p = gfx.VertexBuffer.prototype;
_p._ctor = function(device, format, usage, data, numVertices) {
this.init(device, format, usage, data, numVertices);
this.init(device, format._nativeObj, usage, data, numVertices);
this._nativePtr = this.self();
};
cc.defineGetterSetter(_p, "count", _p.getCount);
_p = gfx.IndexBuffer.prototype;
_p._ctor = function(device, format, usage, data, numIndices) {
this.init(device, format, usage, data, numIndices);
this._nativePtr = this.self();
};
cc.defineGetterSetter(_p, "count", _p.getCount);
+26
View File
@@ -0,0 +1,26 @@
class InputAssembler {
constructor(vb, ib, pt = gfx.PT_TRIANGLES) {
this._vertexBuffer = vb;
this._indexBuffer = ib;
this._primitiveType = pt;
this._start = 0;
this._count = -1;
// TODO: instancing data
// this._stream = 0;
}
getPrimitiveCount () {
if (this._count !== -1) {
return this._count;
}
if (this._indexBuffer) {
return this._indexBuffer.count;
}
return this._vertexBuffer.count;
}
}
module.exports = InputAssembler;
+3 -3
View File
@@ -50,9 +50,9 @@ class Model {
this._effects.push(effect);
//
let defs = Object.create(null);
effect.extractDefines(defs);
this._defines.push(defs);
// let defs = Object.create(null);
// effect.extractDefines(defs);
// this._defines.push(defs);
}
clearEffects() {
+48 -22
View File
@@ -22,7 +22,9 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
const utils = require('./utils');
// projection
renderer.PROJ_PERSPECTIVE = 0;
renderer.PROJ_ORTHO = 1;
@@ -58,41 +60,65 @@ renderer.PARAM_TEXTURE_CUBE = 14;
renderer.CLEAR_COLOR = 1;
renderer.CLEAR_DEPTH = 2;
renderer.CLEAR_STENCIL = 4;
//renderer.addStage = renderer.Config.addStage;
renderer.config = {
addStage: renderer.addStage,
// stageIDs: renderer.stageIDs,
stageIDs: renderer.stageIDs,
stageID: renderer.stageID
};
renderer.InputAssembler = require('./input-assembler');
renderer.config = require('./config');
renderer.Effect = require('./Effect');
renderer.Technique = require('./Technique');
renderer.Pass = require('./Pass');
renderer.Model = require('./Model');
var models = [];
var sizeOfModel = 23;
// length + 500 modles(8 for each model)
var modlesData = new Float64Array(1 + 500*sizeOfModel);
var fillModelData = function() {
if (models.length > 500)
modlesData = new FloatArray64(1 + models.length*sizeOfModel);
modlesData[0] = models.length;
var index = 1;
var model;
var worldMatrix;
var ia;
for (var i = 0, len = models.length; i < len; ++i) {
model = models[i];
modlesData[index++] = model._dynamicIA;
modlesData[index++] = model._viewID;
worldMatrix = utils.getWorldRTInAB(model._node);
modlesData.set(worldMatrix, index);
index += 16;
// ia
ia = model._inputAssemblers[0];
modlesData[index++] = ia._vertexBuffer._nativePtr;
modlesData[index++] = ia._indexBuffer._nativePtr;
modlesData[index++] = ia._start;
modlesData[index++] = ia._count;
// effect
modlesData[index++] = model._effects[0]._nativePtr;
}
}
// ForwardRenderer adapter
var _p;
_p = renderer.ForwardRenderer.prototype;
_p._ctor = function(device, builtin) {
this.init(device, builtin.programTemplates, builtin.defaultTexture, canvas.width, canvas.height);
this.init(device, builtin.programTemplates, builtin.defaultTexture, canvas.width, canvas.height);
};
_p.render = function(scene) {
fillModelData();
this.renderNative(scene, modlesData);
// InputAssembler adapter
_p = renderer.InputAssembler.prototype;
_p._ctor = function(vb, ib, pt = gfx.PT_TRIANGLES) {
this.init(vb, ib, pt);
};
cc.defineGetterSetter(_p, "_vertexBuffer", _p.getVertexBuffer, _p.setVertexBuffer);
cc.defineGetterSetter(_p, "_indexBuffer", _p.getIndexBuffer, _p.setIndexBuffer);
cc.defineGetterSetter(_p, "_primitiveType", _p.getPrimitiveType, _p.setPrimitiveType);
cc.defineGetterSetter(_p, "_start", _p.getStart, _p.setStart);
cc.defineGetterSetter(_p, "_count", _p.getCount, _p.setCount);
models.length = 0;
}
// Scene
// Scene
_p = renderer.Scene.prototype;
_p.addModel = function(model) {
models.push(model);
}
_p.removeModel = function() {}
+65
View File
@@ -0,0 +1,65 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
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.
****************************************************************************/
'use strict';
let _typedArray_temp = new Float32Array(16);
var _mat4_temp = cc.vmath.mat4.create();
function _mat4ToArray(typedArray, mat4) {
typedArray[0] = mat4.m00;
typedArray[1] = mat4.m01;
typedArray[2] = mat4.m02;
typedArray[3] = mat4.m03;
typedArray[4] = mat4.m04;
typedArray[5] = mat4.m05;
typedArray[6] = mat4.m06;
typedArray[7] = mat4.m07;
typedArray[8] = mat4.m08;
typedArray[9] = mat4.m09;
typedArray[10] = mat4.m10;
typedArray[11] = mat4.m11;
typedArray[12] = mat4.m12;
typedArray[13] = mat4.m13;
typedArray[14] = mat4.m14;
typedArray[15] = mat4.m15;
}
function getWorldRTInAB (node) {
node.getWorldRT(_mat4_temp);
_mat4ToArray(_typedArray_temp, _mat4_temp);
return _typedArray_temp;
}
function getWorldMatrixInAB (node) {
node._updateWorldMatrix();
_mat4ToArray(_typedArray_temp, node._worldMatrix);
return _typedArray_temp;
}
module.exports = {
getWorldRTInAB,
getWorldMatrixInAB
}