From e069e93239d5834c0f2e35393092c21b2cb78750 Mon Sep 17 00:00:00 2001 From: minggo Date: Fri, 13 Apr 2018 18:41:53 +0800 Subject: [PATCH 1/4] add functions for native --- cocos2d/core/CCNode.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 147c2f80fe..b7e2d7bf6a 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -53,6 +53,7 @@ const ONE_DEGREE = Math.PI / 180; var ActionManagerExist = !!cc.ActionManager; var emptyFunc = function () {}; var _mat4_temp = math.mat4.create(); +var _typedArray_temp = new Float32Array(16); var _vec3_temp = math.vec3.create(); var _quat_temp = math.quat.create(); var _parents = []; @@ -919,6 +920,7 @@ var Node = cc.Class({ this._matrix = mathPools.mat4.get(); this._worldMatrix = mathPools.mat4.get(); + this._= new Float32Array(16); this._localMatDirty = false; this._worldMatDirty = false; this.setLocalDirty(); @@ -1877,6 +1879,31 @@ var Node = cc.Class({ return out; }, + _getWorldRT4Native() { + this.getWorldRT(_mat4_temp); + this._mat4ToArray(_typedArray_temp, _mat4_temp); + return _typedArray_temp; + }, + + _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; + }, + /** * !#en Set rotation by lookAt target point, normally used by Camera Node * !#zh 通过观察目标来设置 rotation,一般用于 Camera Node 上 @@ -2042,6 +2069,12 @@ var Node = cc.Class({ return math.mat4.copy(out, this._worldMatrix); }, + _getWorldMatrix4Native() { + this._updateWorldMatrix(); + this._mat4ToArray(_typedArray_temp, this._worldMatrix); + return _typedArray_temp; + }, + /** * !#en Converts a Point to node (local) space coordinates. The result is in Vec2. * !#zh 将一个点转换到节点 (局部) 坐标系。结果以 Vec2 为单位。 From 335da463c58bc5424601ec81aa0ca17c45f54a28 Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 16 Apr 2018 14:57:45 +0800 Subject: [PATCH 2/4] optimize performance --- jsb/gfx.js | 2 +- jsb/model.js | 6 +++--- jsb/renderer.js | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/jsb/gfx.js b/jsb/gfx.js index 37fb392ecc..233006ef1b 100644 --- a/jsb/gfx.js +++ b/jsb/gfx.js @@ -49,7 +49,7 @@ _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); }; cc.defineGetterSetter(_p, "count", _p.getCount); diff --git a/jsb/model.js b/jsb/model.js index cbf2698da5..5423ea5034 100644 --- a/jsb/model.js +++ b/jsb/model.js @@ -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() { diff --git a/jsb/renderer.js b/jsb/renderer.js index a640f35187..57cdcd17bc 100644 --- a/jsb/renderer.js +++ b/jsb/renderer.js @@ -93,6 +93,32 @@ cc.defineGetterSetter(_p, "_count", _p.getCount, _p.setCount); // Scene _p = renderer.Scene.prototype; +_p.addModel = function(model) { + // var args = []; + // args.push(model._dynamicIA); + // args.push(model._viewID); + // args.push(model._node); + + // var inputAssemberls = model._inputAssemblers; + // args.push(inputAssemberls.length); + // for (var i = 0, len = inputAssemberls.length; i < len; ++i) { + // args.push(inputAssemberls[i]) + // } + + // var effects = model._effects; + // args.push(effects.length); + // for (var i = 0, len = effects.length; i < len; ++i) { + // args.push(effects[i]._nativeObj); + // } + + // FIXME: should handle the situation that, the length input assemblers or effects is greater than 1 + // this.addModelNative.apply(this, args); + this.addModelNative(model._dynamicIA, + model._viewID, + model._node, + model._inputAssemblers[0], + model._effects[0]._nativeObj); +} _p.removeModel = function() {} From 8f5f927a9517573ab7456cfe6823c57ad4ccaf00 Mon Sep 17 00:00:00 2001 From: minggo Date: Wed, 18 Apr 2018 17:01:46 +0800 Subject: [PATCH 3/4] optimize performance --- jsb/config.js | 39 +++++++++++++++++++ jsb/effect.js | 1 + jsb/gfx.js | 2 + jsb/input-assembler.js | 26 +++++++++++++ jsb/renderer.js | 86 +++++++++++++++++++++--------------------- 5 files changed, 110 insertions(+), 44 deletions(-) create mode 100644 jsb/config.js create mode 100644 jsb/input-assembler.js diff --git a/jsb/config.js b/jsb/config.js new file mode 100644 index 0000000000..92ec7d144d --- /dev/null +++ b/jsb/config.js @@ -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; \ No newline at end of file diff --git a/jsb/effect.js b/jsb/effect.js index 0f75a9051e..9a57d898e3 100644 --- a/jsb/effect.js +++ b/jsb/effect.js @@ -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??? } diff --git a/jsb/gfx.js b/jsb/gfx.js index 233006ef1b..320c1a8652 100644 --- a/jsb/gfx.js +++ b/jsb/gfx.js @@ -50,12 +50,14 @@ _p._ctor = function(device, options) { _p = gfx.VertexBuffer.prototype; _p._ctor = function(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); diff --git a/jsb/input-assembler.js b/jsb/input-assembler.js new file mode 100644 index 0000000000..865178062e --- /dev/null +++ b/jsb/input-assembler.js @@ -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; \ No newline at end of file diff --git a/jsb/renderer.js b/jsb/renderer.js index 57cdcd17bc..adcc6beccf 100644 --- a/jsb/renderer.js +++ b/jsb/renderer.js @@ -58,20 +58,46 @@ 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 = model._node._getWorldRT4Native(); + 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; @@ -79,45 +105,17 @@ _p = renderer.ForwardRenderer.prototype; _p._ctor = function(device, builtin) { 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) { - // var args = []; - // args.push(model._dynamicIA); - // args.push(model._viewID); - // args.push(model._node); - - // var inputAssemberls = model._inputAssemblers; - // args.push(inputAssemberls.length); - // for (var i = 0, len = inputAssemberls.length; i < len; ++i) { - // args.push(inputAssemberls[i]) - // } - - // var effects = model._effects; - // args.push(effects.length); - // for (var i = 0, len = effects.length; i < len; ++i) { - // args.push(effects[i]._nativeObj); - // } - - // FIXME: should handle the situation that, the length input assemblers or effects is greater than 1 - // this.addModelNative.apply(this, args); - this.addModelNative(model._dynamicIA, - model._viewID, - model._node, - model._inputAssemblers[0], - model._effects[0]._nativeObj); + models.push(model); } _p.removeModel = function() {} From e08fcfa5a72b22cffe946cb586e22efbc6357aaf Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 19 Apr 2018 11:17:01 +0800 Subject: [PATCH 4/4] Improve jsb renderer implementation --- cocos2d/core/CCNode.js | 32 --------------------- jsb/renderer.js | 8 ++++-- jsb/utils.js | 65 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 35 deletions(-) create mode 100644 jsb/utils.js diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index f582a7e2ef..c1db785026 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -53,7 +53,6 @@ const ONE_DEGREE = Math.PI / 180; var ActionManagerExist = !!cc.ActionManager; var emptyFunc = function () {}; var _mat4_temp = math.mat4.create(); -var _typedArray_temp = new Float32Array(16); var _vec3_temp = math.vec3.create(); var _quat_temp = math.quat.create(); var _parents = []; @@ -1886,31 +1885,6 @@ var Node = cc.Class({ return out; }, - _getWorldRT4Native() { - this.getWorldRT(_mat4_temp); - this._mat4ToArray(_typedArray_temp, _mat4_temp); - return _typedArray_temp; - }, - - _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; - }, - /** * !#en Set rotation by lookAt target point, normally used by Camera Node * !#zh 通过观察目标来设置 rotation,一般用于 Camera Node 上 @@ -2094,12 +2068,6 @@ var Node = cc.Class({ return math.mat4.copy(out, this._worldMatrix); }, - _getWorldMatrix4Native() { - this._updateWorldMatrix(); - this._mat4ToArray(_typedArray_temp, this._worldMatrix); - return _typedArray_temp; - }, - /** * !#en Converts a Point to node (local) space coordinates. The result is in Vec2. * !#zh 将一个点转换到节点 (局部) 坐标系。结果以 Vec2 为单位。 diff --git a/jsb/renderer.js b/jsb/renderer.js index adcc6beccf..a0a566ce13 100644 --- a/jsb/renderer.js +++ b/jsb/renderer.js @@ -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; @@ -82,7 +84,7 @@ var fillModelData = function() { model = models[i]; modlesData[index++] = model._dynamicIA; modlesData[index++] = model._viewID; - worldMatrix = model._node._getWorldRT4Native(); + worldMatrix = utils.getWorldRTInAB(model._node); modlesData.set(worldMatrix, index); index += 16; @@ -103,7 +105,7 @@ 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(); diff --git a/jsb/utils.js b/jsb/utils.js new file mode 100644 index 0000000000..f1a4407a64 --- /dev/null +++ b/jsb/utils.js @@ -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 +} \ No newline at end of file