Merge pull request #2525 from pandamicro/optimize

Sync world mat dirty to ensure mat dirty flag reliable
This commit is contained in:
pandamicro
2018-04-12 19:34:48 +08:00
committed by GitHub
13 changed files with 134 additions and 178 deletions
-7
View File
@@ -33,7 +33,6 @@ const EventListeners = require('./event/event-listeners');
const Obj = require('./platform/CCObject');
const renderer = require('./renderer');
const eventManager = require('./event-manager');
const transformSys = require('./systems/transform');
//----------------------------------------------------------------------------------------------------------------------
@@ -929,8 +928,6 @@ cc.Director.prototype = {
this._compScheduler.lateUpdatePhase(deltaTime);
this.emit(cc.Director.EVENT_AFTER_UPDATE);
transformSys.update();
}
// Render
@@ -939,7 +936,6 @@ cc.Director.prototype = {
// After draw
this.emit(cc.Director.EVENT_AFTER_DRAW);
transformSys.afterDraw();
this._totalFrames++;
@@ -967,8 +963,6 @@ cc.Director.prototype = {
this.emit(cc.Director.EVENT_AFTER_UPDATE);
// Destroy entities that have been removed recently
Obj._deferredDestroy();
// Update transform for all dirty nodes
transformSys.update();
}
// Render
@@ -977,7 +971,6 @@ cc.Director.prototype = {
// After draw
this.emit(cc.Director.EVENT_AFTER_DRAW);
transformSys.afterDraw();
eventManager.frameUpdateListeners();
this._totalFrames++;
+99 -28
View File
@@ -35,7 +35,6 @@ const eventManager = require('./event-manager');
const macro = require('./platform/CCMacro');
const misc = require('./utils/misc');
const Event = require('./event/event');
const transformSys = require('./systems/transform');
const hierarchyChain = require('./renderer/utils/hierarchy-chain');
const Flags = cc.Object.Flags;
@@ -56,6 +55,7 @@ var emptyFunc = function () {};
var _mat4_temp = math.mat4.create();
var _vec3_temp = math.vec3.create();
var _quat_temp = math.quat.create();
var _parents = [];
var _globalOrderOfArrival = 1;
/**
@@ -309,6 +309,16 @@ function _checkListeners (node, events) {
return true;
}
function _syncWorldDirty (children) {
for (let i = 0, l = children.length; i < l; i++) {
let child = children[i];
child._worldMatDirty = true;
if (child._children.length > 0) {
_syncWorldDirty(child._children);
}
}
}
/**
* !#en
* Class of all entities in Cocos Creator scenes.<br/>
@@ -422,7 +432,7 @@ var Node = cc.Class({
}
localPosition.x = value;
transformSys.setNodeDirty(this);
this.setLocalDirty();
// fast check event
var cache = this._hasListenerCache;
@@ -465,7 +475,7 @@ var Node = cc.Class({
}
localPosition.y = value;
transformSys.setNodeDirty(this);
this.setLocalDirty();
// fast check event
var cache = this._hasListenerCache;
@@ -495,7 +505,7 @@ var Node = cc.Class({
if (value !== localPosition.z) {
if (!CC_EDITOR || isFinite(value)) {
localPosition.z = value;
transformSys.setNodeDirty(this);
this.setLocalDirty();
}
else {
cc.error(ERR_INVALID_NUMBER, 'new z');
@@ -525,7 +535,7 @@ var Node = cc.Class({
this._rotationX = this._rotationY = value;
// Update quaternion from rotation
math.quat.fromEuler(this._quat, 0, 0, -value);
transformSys.setNodeDirty(this);
this.setLocalDirty();
var cache = this._hasListenerCache;
if (cache && cache[ROTATION_CHANGED]) {
@@ -558,7 +568,7 @@ var Node = cc.Class({
else {
math.quat.fromEuler(this._quat, value, this._rotationY, 0);
}
transformSys.setNodeDirty(this);
this.setLocalDirty();
var cache = this._hasListenerCache;
if (cache && cache[ROTATION_CHANGED]) {
@@ -591,7 +601,7 @@ var Node = cc.Class({
else {
math.quat.fromEuler(this._quat, this._rotationX, value, 0);
}
transformSys.setNodeDirty(this);
this.setLocalDirty();
var cache = this._hasListenerCache;
if (cache && cache[ROTATION_CHANGED]) {
@@ -626,7 +636,7 @@ var Node = cc.Class({
set (value) {
if (this._scale.x !== value) {
this._scale.x = value;
transformSys.setNodeDirty(this);
this.setLocalDirty();
var cache = this._hasListenerCache;
if (cache && cache[SCALE_CHANGED]) {
@@ -652,7 +662,7 @@ var Node = cc.Class({
set (value) {
if (this._scale.y !== value) {
this._scale.y = value;
transformSys.setNodeDirty(this);
this.setLocalDirty();
var cache = this._hasListenerCache;
if (cache && cache[SCALE_CHANGED]) {
@@ -677,7 +687,7 @@ var Node = cc.Class({
},
set (value) {
this._skewX = value;
transformSys.setNodeDirty(this);
this.setLocalDirty();
}
},
@@ -696,7 +706,7 @@ var Node = cc.Class({
},
set (value) {
this._skewY = value;
transformSys.setNodeDirty(this);
this.setLocalDirty();
}
},
@@ -911,6 +921,7 @@ var Node = cc.Class({
this._worldMatrix = mathPools.mat4.get();
this._localMatDirty = false;
this._worldMatDirty = false;
this.setLocalDirty();
this._cullingMask = 1 << this.groupIndex;
},
@@ -984,8 +995,6 @@ var Node = cc.Class({
var actionManager = ActionManagerExist ? cc.director.getActionManager() : null;
if (active) {
// activate
// Force update transform
transformSys.setNodeDirty(this);
// ActionManager & EventManager
actionManager && actionManager.resumeTarget(this);
eventManager.resumeTarget(this);
@@ -1291,7 +1300,7 @@ var Node = cc.Class({
testPt = cc.v2(point);
}
transformSys._updateWorldMatrix(this);
this._updateWorldMatrix();
math.mat4.invert(_mat4_temp, this._worldMatrix);
math.vec2.transformMat4(testPt, testPt, _mat4_temp);
testPt.x += this._anchorPoint.x * this._contentSize.width;
@@ -1541,7 +1550,7 @@ var Node = cc.Class({
else {
return cc.error(ERR_INVALID_NUMBER, 'y of new position');
}
transformSys.setNodeDirty(this);
this.setLocalDirty();
// fast check event
var cache = this._hasListenerCache;
@@ -1593,7 +1602,7 @@ var Node = cc.Class({
if (this._scale.x !== scaleX || this._scale.y !== scaleY) {
this._scale.x = scaleX;
this._scale.y = scaleY;
transformSys.setNodeDirty(this);
this.setLocalDirty();
var cache = this._hasListenerCache;
if (cache && cache[SCALE_CHANGED]) {
@@ -1731,7 +1740,7 @@ var Node = cc.Class({
locAnchorPoint.x = point;
locAnchorPoint.y = y;
}
transformSys.setNodeDirty(this);
this.setLocalDirty();
this.emit(ANCHOR_CHANGED);
},
@@ -1798,8 +1807,7 @@ var Node = cc.Class({
else {
math.vec3.copy(this._position, pos);
}
transformSys.setNodeDirty(this);
this.setLocalDirty();
// fast check event
var cache = this._hasListenerCache;
@@ -1911,6 +1919,69 @@ var Node = cc.Class({
}
},
_calculWorldMatrix () {
// Avoid as much function call as possible
if (this._localMatDirty) {
this._updateLocalMatrix();
}
// Assume parent world matrix is correct
if (this._parent) {
let parentMat = this._parent._worldMatrix;
math.mat4.mul(this._worldMatrix, parentMat, this._matrix);
}
else {
math.mat4.copy(this._worldMatrix, this._matrix);
}
this._worldMatDirty = false;
},
_updateWorldMatrix () {
let node = this;
let n = -1;
// Find root of world matrix changing
while (node) {
if (node._worldMatDirty) {
n++;
_parents[n] = node;
}
else {
break;
}
node = node._parent;
}
if (n >= 0) {
_parents.length = n + 1;
// Update world matrix of all changed parents
for (let i = n; i >= 0; i--) {
_parents[i]._calculWorldMatrix();
}
}
},
setLocalDirty () {
if (!this._localMatDirty) {
this._localMatDirty = true;
// Sync world mat dirty to sub tree
if (!this._worldMatDirty) {
this._worldMatDirty = true;
if (this._children.length > 0) {
_syncWorldDirty(this._children);
}
}
}
},
setWorldDirty () {
if (!this._worldMatDirty) {
this._worldMatDirty = true;
// Sync world mat dirty to sub tree
if (this._children.length > 0) {
_syncWorldDirty(this._children);
}
}
},
/**
* !#en
* Get the local transform matrix (4x4), based on parent node coordinates
@@ -1939,7 +2010,7 @@ var Node = cc.Class({
* node.getLocalMatrix(mat4);
*/
getWorldMatrix (out) {
transformSys._updateWorldMatrix(this);
this._updateWorldMatrix();
return math.mat4.copy(out, this._worldMatrix);
},
@@ -1953,7 +2024,7 @@ var Node = cc.Class({
* var newVec2 = node.convertToNodeSpace(cc.v2(100, 100));
*/
convertToNodeSpace (worldPoint) {
transformSys._updateWorldMatrix(this);
this._updateWorldMatrix();
math.mat4.invert(_mat4_temp, this._worldMatrix);
let out = new cc.Vec2();
math.vec2.transformMat4(out, worldPoint, _mat4_temp);
@@ -1972,7 +2043,7 @@ var Node = cc.Class({
* var newVec2 = node.convertToWorldSpace(cc.v2(100, 100));
*/
convertToWorldSpace (nodePoint) {
transformSys._updateWorldMatrix(this);
this._updateWorldMatrix();
let out = new cc.Vec2(
nodePoint.x - this._anchorPoint.x * this._contentSize.width,
nodePoint.y - this._anchorPoint.y * this._contentSize.height
@@ -1994,7 +2065,7 @@ var Node = cc.Class({
* var newVec2 = node.convertToNodeSpaceAR(cc.v2(100, 100));
*/
convertToNodeSpaceAR (worldPoint) {
transformSys._updateWorldMatrix(this);
this._updateWorldMatrix();
math.mat4.invert(_mat4_temp, this._worldMatrix);
let out = new cc.Vec2();
return math.vec2.transformMat4(out, worldPoint, _mat4_temp);
@@ -2014,7 +2085,7 @@ var Node = cc.Class({
* var newVec2 = node.convertToWorldSpaceAR(cc.v2(100, 100));
*/
convertToWorldSpaceAR (nodePoint) {
transformSys._updateWorldMatrix(this);
this._updateWorldMatrix();
let out = new cc.Vec2();
return math.vec2.transformMat4(out, nodePoint, this._worldMatrix);
},
@@ -2088,7 +2159,7 @@ var Node = cc.Class({
if (!out) {
out = AffineTrans.identity();
}
transformSys._updateWorldMatrix(this);
this._updateWorldMatrix();
var contentSize = this._contentSize;
_vec3_temp.x = -this._anchorPoint.x * contentSize.width;
@@ -2119,7 +2190,7 @@ var Node = cc.Class({
if (!out) {
out = AffineTrans.identity();
}
transformSys._updateWorldMatrix(this);
this._updateWorldMatrix();
return AffineTrans.fromMat4(out, this._matrix);
},
@@ -2162,7 +2233,7 @@ var Node = cc.Class({
if (!out) {
out = AffineTrans.identity();
}
transformSys._updateWorldMatrix(this);
this._updateWorldMatrix();
math.mat4.invert(_mat4_temp, this._worldMatrix);
return AffineTrans.fromMat4(out, _mat4_temp);
},
@@ -2231,7 +2302,7 @@ var Node = cc.Class({
*/
getBoundingBoxToWorld () {
if (this._parent) {
transformSys._updateWorldMatrix(this._parent);
this._parent._updateWorldMatrix();
return this._getBoundingBoxTo(this._parent._worldMatrix);
}
else {
+6 -7
View File
@@ -292,8 +292,7 @@ let Camera = cc.Class({
onEnable () {
this._matrixDirty = true;
cc.director.on(cc.Director.EVENT_AFTER_DRAW, this.afterDraw, this);
cc.director.on(cc.Director.EVENT_BEFORE_DRAW, this.beforeDraw, this);
if (game.renderType === game.RENDER_TYPE_WEBGL) {
renderer.scene.addCamera(this._camera);
}
@@ -301,8 +300,7 @@ let Camera = cc.Class({
},
onDisable () {
cc.director.off(cc.Director.EVENT_AFTER_DRAW, this.afterDraw, this);
cc.director.off(cc.Director.EVENT_BEFORE_DRAW, this.beforeDraw, this);
if (game.renderType === game.RENDER_TYPE_WEBGL) {
renderer.scene.removeCamera(this._camera);
}
@@ -434,15 +432,15 @@ let Camera = cc.Class({
// force update node world matrix
this.node.getWorldMatrix(_mat4_temp_1);
this.afterDraw();
this.beforeDraw();
renderer._walker.visit(root);
renderer._forward.renderCamera(this._camera, renderer.scene);
},
afterDraw: !CC_EDITOR && function () {
beforeDraw: !CC_EDITOR && function () {
let node = this.node;
if (!this._matrixDirty && !node._worldMatUpdated)
if (!this._matrixDirty && !node._worldMatDirty)
return;
let camera = this._camera;
@@ -456,6 +454,7 @@ let Camera = cc.Class({
height = targetTexture.height;
}
node._updateWorldMatrix();
_vec3_temp_1.x = node._worldMatrix.m12;
_vec3_temp_1.y = node._worldMatrix.m13;
_vec3_temp_1.z = 0;
@@ -29,7 +29,7 @@ const FillType = Sprite.FillType;
const simpleRenderUtil = require('./simple');
module.exports = {
update (sprite) {
update (sprite, batchData) {
let renderData = sprite._renderData;
let uvDirty = renderData.uvDirty,
vertDirty = renderData.vertDirty;
@@ -63,7 +63,7 @@ module.exports = {
if (vertDirty) {
this.updateVerts(sprite, fillStart, fillEnd);
}
if (vertDirty || sprite.node._worldMatUpdated) {
if (vertDirty || batchData.worldMatUpdated) {
this.updateWorldVerts(sprite);
}
},
@@ -67,13 +67,13 @@ let spriteAssembler = js.addon({
return util;
},
updateRenderData (sprite) {
updateRenderData (sprite, batchData) {
let datas = sprite.__allocedDatas;
if (!sprite.spriteFrame || !sprite.getMaterial() || !sprite._renderData) {
return datas;
}
sprite._assembler.update(sprite);
sprite._assembler.update(sprite, batchData);
return datas;
},
@@ -28,7 +28,7 @@ module.exports = {
return sprite.requestRenderData();
},
update (sprite) {
update (sprite, batchData) {
let renderData = sprite._renderData;
let frame = sprite.spriteFrame;
@@ -52,7 +52,7 @@ module.exports = {
if (vertDirty) {
this.updateVerts(sprite);
}
if (vertDirty || sprite.node._worldMatUpdated) {
if (vertDirty || batchData.worldMatUpdated) {
this.updateWorldVerts(sprite);
}
},
@@ -125,8 +125,8 @@ module.exports = {
updateWorldVerts (sprite) {
let node = sprite.node,
renderData = sprite._renderData,
data = renderData._data,
matrix = node._worldMatrix;
data = renderData._data;
let matrix = node._worldMatrix;
let a = matrix.m00, b = matrix.m01, c = matrix.m04, d = matrix.m05,
tx = matrix.m12, ty = matrix.m13;
@@ -29,7 +29,7 @@ const assembler = require('../assembler');
module.exports = js.addon({
useModel: false,
update (sprite) {
update (sprite, batchData) {
let renderData = sprite._renderData;
if (renderData.uvDirty) {
@@ -40,7 +40,7 @@ module.exports = js.addon({
if (vertDirty) {
this.updateVerts(sprite);
}
if (vertDirty || sprite.node._worldMatUpdated) {
if (vertDirty || batchData.worldMatUpdated) {
this.updateWorldVerts(sprite);
}
},
@@ -127,8 +127,9 @@ module.exports = js.addon({
updateWorldVerts (sprite) {
let node = sprite.node,
renderData = sprite._renderData,
data = renderData._data,
matrix = node._worldMatrix;
data = renderData._data;
let matrix = node._worldMatrix;
let a = matrix.m00, b = matrix.m01, c = matrix.m04, d = matrix.m05,
tx = matrix.m12, ty = matrix.m13;
@@ -154,8 +154,9 @@ module.exports = {
updateWorldVerts (sprite) {
let node = sprite.node,
renderData = sprite._renderData,
data = renderData._data,
matrix = node._worldMatrix;
data = renderData._data;
let matrix = node._worldMatrix;
let a = matrix.m00, b = matrix.m01, c = matrix.m04, d = matrix.m05,
tx = matrix.m12, ty = matrix.m13;
@@ -63,6 +63,10 @@ RenderComponentWalker.prototype = {
let ctx = this._device._ctx;
let cam = this._camera;
ctx.setTransform(cam.a, cam.b, cam.c, cam.d, cam.tx, cam.ty);
let node = comp.node;
if (node._worldMatDirty) {
node._updateWorldMatrix();
}
let drawCall = renderer.draw(ctx, comp);
this._device._stats.drawcalls += drawCall;
},
@@ -43,6 +43,7 @@ const MAX_INDICE_BYTES = MAX_INDICE * 2;
var _batchData = {
node: null,
worldMatUpdated: false,
vfmt: null,
material: null,
data: null,
@@ -124,6 +125,7 @@ RenderComponentWalker.prototype = {
// reset caches for handle render components
_batchData.node = null;
_batchData.worldMatUpdated = false;
_batchData.vfmt = null;
_batchData.material = null;
_batchData.data = null;
@@ -295,7 +297,6 @@ RenderComponentWalker.prototype = {
let group = this._curCameraNode ? this._curCameraNode.groupIndex : node.groupIndex;
node._cullingMask = 1 << group;
assembler = comp.constructor._assembler;
}
// Post handle
@@ -305,7 +306,12 @@ RenderComponentWalker.prototype = {
}
assembler = comp.constructor._postAssembler;
}
if (node._worldMatDirty) {
node._updateWorldMatrix();
_batchData.worldMatUpdated = true;
}
this.renderComp(comp, assembler);
_batchData.worldMatUpdated = false;
entry = entry.next;
}
-118
View File
@@ -1,118 +0,0 @@
/****************************************************************************
Copyright (c) 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.
****************************************************************************/
const renderEngine = require('../renderer/render-engine');
const math = renderEngine.math;
let transformSys = {
_dirtyNodes: [],
_updatedNodes: [],
_calculWorldMatrix (node) {
// Avoid as much function call as possible
if (node._localMatDirty) {
node._updateLocalMatrix();
}
// Assume parent world matrix is correct
if (node._parent) {
let parentMat = node._parent._worldMatrix;
math.mat4.mul(node._worldMatrix, parentMat, node._matrix);
}
else {
math.mat4.copy(node._worldMatrix, node._matrix);
}
node._worldMatDirty = false;
node._worldMatUpdated = true;
// For resetting _worldMatUpdated flag in afterDraw
this._updatedNodes.push(node);
let children = node._children;
for (let i = 0, len = children.length; i < len; ++i) {
let child = children[i];
this._calculWorldMatrix(child);
}
},
_updateWorldMatrix (node) {
let changedRoot = null;
while (node) {
if (node._localMatDirty || node._worldMatDirty) {
changedRoot = node;
}
node = node._parent;
}
if (changedRoot) {
this._calculWorldMatrix(changedRoot);
}
},
setNodeDirty (node) {
if (!node._localMatDirty) {
node._localMatDirty = true;
if (!this._dirtyNodes[node._level]) {
this._dirtyNodes[node._level] = [];
}
this._dirtyNodes[node._level].push(node);
}
},
setWorldDirty (node) {
if (!node._worldMatDirty) {
node._worldMatDirty = true;
if (!this._dirtyNodes[node._level]) {
this._dirtyNodes[node._level] = [];
}
this._dirtyNodes[node._level].push(node);
}
},
_updateLevel (nodes) {
let node;
for (let i = 0, n = nodes.length; i < n; i++) {
node = nodes[i];
if (node._localMatDirty || node._worldMatDirty) {
this._calculWorldMatrix(node);
}
}
nodes.length = 0;
},
update () {
let levels = this._dirtyNodes;
for (let i = 0, l = levels.length; i < l; i++) {
this._updateLevel(levels[i]);
}
},
afterDraw () {
for (let i = 0, l = this._updatedNodes.length; i < l; i++) {
this._updatedNodes[i]._worldMatUpdated = false;
}
this._updatedNodes.length = 0;
}
};
module.exports = transformSys;
+1 -2
View File
@@ -25,7 +25,6 @@
const renderEngine = require('../../cocos2d/core/renderer/render-engine');
const math = renderEngine.math;
const transformSys = require('../../cocos2d/core/systems/transform');
const BlendFactor = require('../../cocos2d/core/platform/CCMacro');
const BinaryOffset = dragonBones.BinaryOffset;
@@ -355,7 +354,7 @@ dragonBones.CCSlot = cc.Class({
let node = this._display.node;
math.mat4.copy(node._matrix, t);
node._localMatDirty = false;
transformSys.setWorldDirty(node);
node.setWorldDirty();
}
this._updateVertices();
+1 -1
View File
@@ -99,7 +99,7 @@ let armatureAssembler = js.addon({
let node = comp.node;
_z = node._position.z;
_worldMatrix = node._worldMatrix;
_a = _worldMatrix.m00; _b = _worldMatrix.m01; _c = _worldMatrix.m04; _d = _worldMatrix.m05;
_tx = _worldMatrix.m12; _ty = _worldMatrix.m13;