From 21cf3ee8521db5731c893f45392b7b4885d04815 Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 12 Apr 2018 11:45:18 +0800 Subject: [PATCH 1/3] optimize performance --- jsb/effect.js | 12 ++++++ jsb/pass.js | 40 ++++++++++++++++++++ jsb/renderer.js | 96 +++++++++++++++++++++--------------------------- jsb/technique.js | 5 +++ 4 files changed, 99 insertions(+), 54 deletions(-) diff --git a/jsb/effect.js b/jsb/effect.js index fe51c68c09..0f75a9051e 100644 --- a/jsb/effect.js +++ b/jsb/effect.js @@ -9,6 +9,15 @@ class Effect { this._properties = properties; this._defines = defines; + var techniqueObjs = []; + var techniqueObj; + for (var i = 0, len = techniques.length; i < len; ++i) { + techniqueObj = techniques[i]._nativeObj; + techniqueObjs.push(techniqueObj); + } + + this._nativeObj = new renderer.EffectNative(techniqueObjs, properties, defines); + // TODO: check if params is valid for current technique??? } @@ -37,6 +46,7 @@ class Effect { setProperty(name, value) { // TODO: check if params is valid for current technique??? this._properties[name] = value; + this._nativeObj.setProperty(name, value); } getDefine(name) { @@ -60,6 +70,8 @@ class Effect { } } + this._nativeObj.setDefine(name, value); + console.warn(`Failed to set define ${name}, define not found.`); } diff --git a/jsb/pass.js b/jsb/pass.js index 5198f5e269..94a594d206 100644 --- a/jsb/pass.js +++ b/jsb/pass.js @@ -38,10 +38,14 @@ class Pass { this._stencilZFailOpBack = gfx.STENCIL_OP_KEEP; this._stencilZPassOpBack = gfx.STENCIL_OP_KEEP; this._stencilWriteMaskBack = 0xff; + + this._binary = new Uint32Array(25); + this._updateBinary(); } setCullMode(cullMode) { this._cullMode = cullMode; + this._binary[0] = cullMode; } setBlend( @@ -61,6 +65,8 @@ class Pass { this._blendSrcAlpha = blendSrcAlpha; this._blendDstAlpha = blendDstAlpha; this._blendColor = blendColor; + + this._updateBinary(); } setDepth( @@ -71,6 +77,8 @@ class Pass { this._depthTest = depthTest; this._depthWrite = depthWrite; this._depthFunc = depthFunc; + + this._updateBinary(); } setStencilFront( @@ -90,6 +98,8 @@ class Pass { this._stencilZFailOpFront = stencilZFailOp; this._stencilZPassOpFront = stencilZPassOp; this._stencilWriteMaskFront = stencilWriteMask; + + this._updateBinary(); } setStencilBack( @@ -109,6 +119,36 @@ class Pass { this._stencilZFailOpBack = stencilZFailOp; this._stencilZPassOpBack = stencilZPassOp; this._stencilWriteMaskBack = stencilWriteMask; + + this._updateBinary(); + } + + _updateBinary() { + this._binary[0] = this._cullMode; + this._binary[1] = this._blendEq; + this._binary[2] = this._blendSrc; + this._binary[3] = this._blendDst; + this._binary[4] = this._blendAlphaEq; + this._binary[5] = this._blendSrcAlpha; + this._binary[6] = this._blendDstAlpha; + this._binary[7] = this._blendColor; + this._binary[8] = this._depthTest; + this._binary[9] = this._depthWrite; + this._binary[10] = this._depthFunc; + this._binary[11] = this._stencilFuncFront; + this._binary[12] = this._stencilRefFront; + this._binary[13] = this._stencilMaskFront; + this._binary[14] = this._stencilFailOpFront; + this._binary[15] = this._stencilZFailOpFront; + this._binary[16] = this._stencilZPassOpFront; + this._binary[17] = this._stencilWriteMaskFront; + this._binary[18] = this._stencilFuncBack; + this._binary[19] = this._stencilRefBack; + this._binary[20] = this._stencilMaskBack; + this._binary[21] = this._stencilFailOpBack; + this._binary[22] = this._stencilZFailOpBack; + this._binary[23] = this._stencilZPassOpBack; + this._binary[24] = this._stencilWriteMaskBack; } } diff --git a/jsb/renderer.js b/jsb/renderer.js index 9cde55b6cf..c8b49e9b8f 100644 --- a/jsb/renderer.js +++ b/jsb/renderer.js @@ -94,62 +94,50 @@ cc.defineGetterSetter(_p, "_count", _p.getCount, _p.setCount); // Scene _p = renderer.Scene.prototype; // _p.addModel = function(model) { -// // var modelNative = new renderer.ModelNative(); -// // var effects = model._effects; -// // var effectNatives = []; -// // for (var i = 0, len = effects.length; i < len; ++i) { -// // var effect = effects[i]; -// // var techniques = effect._techniques; -// // var techniqueNatives = []; -// // for (var j = 0, len1 = techniques.length; j < len1; ++j) { -// // var technique = techniques[j]; -// // var passesNatives = []; -// // var passes = technique.passes; -// // for (var k =0, len2 = passes.length; k < len2; ++k) { -// // var pass = passes[k]; -// // var passNative = new renderer.PassNative(pass._programName); -// // passNative.setCullMode(pass._cullMode); -// // passNative.setBlend(pass._blendEq, -// // pass._blendSrc, -// // pass._blendDst, -// // pass._blendAlphaEq, -// // pass._blendSrcAlpha, -// // pass._blendDstAlpha, -// // pass._blendColor); -// // passNative.setDepth(pass._depthTest, pass._depthWrite, pass._depthFunc); -// // passNative.setStencilFront(pass._stencilFuncFront, -// // pass._stencilRefFront, -// // pass._stencilMaskFront, -// // pass._stencilFailOpFront, -// // pass._stencilZFailOpFront, -// // pass._stencilZPassOpFront, -// // pass._stencilWriteMaskFront); -// // passNative.setStencilBack(pass._stencilFuncBack, -// // pass._stencilRefBack, -// // pass._stencilMaskBack, -// // pass._stencilFailOpBack, -// // pass._stencilZFailOpBack, -// // pass._stencilZPassOpBack, -// // pass._stencilWriteMaskBack); -// // passesNatives.push(passNative); -// // } -// // var techniqueNative = new renderer.TechniqueNative(technique._stages, technique._parameters, passesNatives, technique._layer); -// // techniqueNatives.push(techniqueNative); -// // } -// // var effectNative = new renderer.EffectNative(techniqueNatives, effect._properties, effect._defines); -// // modelNative.addEffect(effectNative); -// // } +// var effects = model._effects; +// var elementsOfPass = 25; +// for (var i = 0, len = effects.length; i < len; ++i) { +// var effect = effects[i]; +// var techniques = effect._techniques; +// for (var j = 0, len1 = techniques.length; j < len1; ++j) { +// var technique = techniques[j]; +// var passesBinary = new Uint32Array(elementsOfPass * passes.length); +// var passes = technique.passes; +// Uin32Array passBinary[25]; +// for (var k =0, len2 = passes.length; k < len2; ++k) { +// var pass = passes[k]; +// var offset = elementsOfPass * k; +// // var passNative = new renderer.PassNative(pass._programName); +// // passNative.setCullMode(pass._cullMode); +// // passNative.setBlend(pass._blendEq, +// // pass._blendSrc, +// // pass._blendDst, +// // pass._blendAlphaEq, +// // pass._blendSrcAlpha, +// // pass._blendDstAlpha, +// // pass._blendColor); +// // passNative.setDepth(pass._depthTest, pass._depthWrite, pass._depthFunc); +// // passNative.setStencilFront(pass._stencilFuncFront, +// // pass._stencilRefFront, +// // pass._stencilMaskFront, +// // pass._stencilFailOpFront, +// // pass._stencilZFailOpFront, +// // pass._stencilZPassOpFront, +// // pass._stencilWriteMaskFront); +// // passNative.setStencilBack(pass._stencilFuncBack, +// // pass._stencilRefBack, +// // pass._stencilMaskBack, +// // pass._stencilFailOpBack, +// // pass._stencilZFailOpBack, +// // pass._stencilZPassOpBack, +// // pass._stencilWriteMaskBack); +// // passesNatives.push(passNative); +// } +// } -// // modelNative.setDynamicIA(model._dynamicIA); -// // modelNative.setViewId(model._viewID); -// // modelNative.setNode(model._node); +// } -// // var inputAssemblers = model._inputAssemblers; -// // for (var k = 0, len2 = inputAssemblers.length; k < len2; ++k) { -// // modelNative.addInputAssembler(inputAssemblers[k]); -// // } - -// // this.addModelNative(modelNative); + // this.addModelNative(model); // }; _p.removeModel = function() {} diff --git a/jsb/technique.js b/jsb/technique.js index 3bdbb42ba9..7a2672c0a8 100644 --- a/jsb/technique.js +++ b/jsb/technique.js @@ -19,11 +19,16 @@ class Technique { this._layer = layer; this._stages = stages; // TODO: this._version = 'webgl' or 'webgl2' // ???? + + this._nativeObj = new renderer.TechniqueNative(stages, parameters, passes, layer); + } setStages(stages) { this._stageIDs = config.stageIDs(stages); this._stages = stages; + + this._nativeObj.setStages(stages); } // get passes() { From 89e4e658ac1046e83a3d125842a997d42bc601be Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 12 Apr 2018 13:44:12 +0800 Subject: [PATCH 2/3] remove unneeded codes --- jsb/renderer.js | 47 ----------------------------------------------- 1 file changed, 47 deletions(-) diff --git a/jsb/renderer.js b/jsb/renderer.js index c8b49e9b8f..a640f35187 100644 --- a/jsb/renderer.js +++ b/jsb/renderer.js @@ -93,53 +93,6 @@ cc.defineGetterSetter(_p, "_count", _p.getCount, _p.setCount); // Scene _p = renderer.Scene.prototype; -// _p.addModel = function(model) { -// var effects = model._effects; -// var elementsOfPass = 25; -// for (var i = 0, len = effects.length; i < len; ++i) { -// var effect = effects[i]; -// var techniques = effect._techniques; -// for (var j = 0, len1 = techniques.length; j < len1; ++j) { -// var technique = techniques[j]; -// var passesBinary = new Uint32Array(elementsOfPass * passes.length); -// var passes = technique.passes; -// Uin32Array passBinary[25]; -// for (var k =0, len2 = passes.length; k < len2; ++k) { -// var pass = passes[k]; -// var offset = elementsOfPass * k; -// // var passNative = new renderer.PassNative(pass._programName); -// // passNative.setCullMode(pass._cullMode); -// // passNative.setBlend(pass._blendEq, -// // pass._blendSrc, -// // pass._blendDst, -// // pass._blendAlphaEq, -// // pass._blendSrcAlpha, -// // pass._blendDstAlpha, -// // pass._blendColor); -// // passNative.setDepth(pass._depthTest, pass._depthWrite, pass._depthFunc); -// // passNative.setStencilFront(pass._stencilFuncFront, -// // pass._stencilRefFront, -// // pass._stencilMaskFront, -// // pass._stencilFailOpFront, -// // pass._stencilZFailOpFront, -// // pass._stencilZPassOpFront, -// // pass._stencilWriteMaskFront); -// // passNative.setStencilBack(pass._stencilFuncBack, -// // pass._stencilRefBack, -// // pass._stencilMaskBack, -// // pass._stencilFailOpBack, -// // pass._stencilZFailOpBack, -// // pass._stencilZPassOpBack, -// // pass._stencilWriteMaskBack); -// // passesNatives.push(passNative); -// } -// } - -// } - - -// this.addModelNative(model); -// }; _p.removeModel = function() {} From ad986709d9813bebd6e732a104984177e7cf3d3f Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 12 Apr 2018 13:44:28 +0800 Subject: [PATCH 3/3] make codes more effect --- jsb/pass.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/jsb/pass.js b/jsb/pass.js index 94a594d206..8c77aae1c6 100644 --- a/jsb/pass.js +++ b/jsb/pass.js @@ -66,7 +66,13 @@ class Pass { this._blendDstAlpha = blendDstAlpha; this._blendColor = blendColor; - this._updateBinary(); + this._binary[1] = this._blendEq; + this._binary[2] = this._blendSrc; + this._binary[3] = this._blendDst; + this._binary[4] = this._blendAlphaEq; + this._binary[5] = this._blendSrcAlpha; + this._binary[6] = this._blendDstAlpha; + this._binary[7] = this._blendColor; } setDepth( @@ -78,7 +84,9 @@ class Pass { this._depthWrite = depthWrite; this._depthFunc = depthFunc; - this._updateBinary(); + this._binary[8] = this._depthTest; + this._binary[9] = this._depthWrite; + this._binary[10] = this._depthFunc; } setStencilFront( @@ -99,7 +107,13 @@ class Pass { this._stencilZPassOpFront = stencilZPassOp; this._stencilWriteMaskFront = stencilWriteMask; - this._updateBinary(); + this._binary[11] = this._stencilFuncFront; + this._binary[12] = this._stencilRefFront; + this._binary[13] = this._stencilMaskFront; + this._binary[14] = this._stencilFailOpFront; + this._binary[15] = this._stencilZFailOpFront; + this._binary[16] = this._stencilZPassOpFront; + this._binary[17] = this._stencilWriteMaskFront; } setStencilBack( @@ -120,7 +134,13 @@ class Pass { this._stencilZPassOpBack = stencilZPassOp; this._stencilWriteMaskBack = stencilWriteMask; - this._updateBinary(); + this._binary[18] = this._stencilFuncBack; + this._binary[19] = this._stencilRefBack; + this._binary[20] = this._stencilMaskBack; + this._binary[21] = this._stencilFailOpBack; + this._binary[22] = this._stencilZFailOpBack; + this._binary[23] = this._stencilZPassOpBack; + this._binary[24] = this._stencilWriteMaskBack; } _updateBinary() {