From 3dcaf1937620d3682cae934a0b63f1a3ef3bcd30 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 11 Apr 2018 14:15:08 +0800 Subject: [PATCH 1/3] Visit render component with chain instead of walking scene graph --- cocos2d/core/CCNode.js | 10 +- cocos2d/core/components/CCRenderComponent.js | 15 ++ .../core/renderer/render-component-walker.js | 58 +++--- .../core/renderer/utils/hierarchy-chain.js | 177 ++++++++++++++++++ 4 files changed, 228 insertions(+), 32 deletions(-) create mode 100644 cocos2d/core/renderer/utils/hierarchy-chain.js diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index ef16dc9408..e690e0dd2f 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -36,6 +36,7 @@ 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; const Destroying = Flags.Destroying; @@ -1006,6 +1007,10 @@ var Node = cc.Class({ }, _onHierarchyChanged (oldParent) { + this._updateOrderOfArrival(); + if (this._parent && this._parent._delaySort) { + this._parent._delaySort(); + } this._onHierarchyChangedBase(oldParent); cc._widgetManager._nodesOrderDirty = true; }, @@ -1078,6 +1083,8 @@ var Node = cc.Class({ eventManager.pauseTarget(this); } + hierarchyChain.rebuild(this); + let children = this._children; for (let i = 0, len = children.length; i < len; i++) { children[i]._onBatchCreated(); @@ -2288,14 +2295,12 @@ var Node = cc.Class({ // invokes the parent setter child.parent = this; - child._updateOrderOfArrival(); if (zIndex !== undefined) { child.zIndex = zIndex; } if (name !== undefined) { child.name = name; } - this._delaySort(); }, /** @@ -2349,6 +2354,7 @@ var Node = cc.Class({ } _children[j + 1] = child; } + hierarchyChain.rebuild(this); this.emit(CHILD_REORDER); } cc.director.__fastOff(cc.Director.EVENT_AFTER_UPDATE, this.sortAllChildren, this); diff --git a/cocos2d/core/components/CCRenderComponent.js b/cocos2d/core/components/CCRenderComponent.js index 5ed1bd5cd3..0fef52fafe 100644 --- a/cocos2d/core/components/CCRenderComponent.js +++ b/cocos2d/core/components/CCRenderComponent.js @@ -26,6 +26,7 @@ const Component = require('./CCComponent'); const defaultVertexFormat = require('../renderer/vertex-format'); const renderEngine = require('../renderer/render-engine'); +const hierarchyChain = require('../renderer/utils/hierarchy-chain'); const RenderData = renderEngine.RenderData; /** @@ -51,14 +52,28 @@ var RenderComponent = cc.Class({ this.__allocedDatas = []; this._vertexFormat = defaultVertexFormat; this._toPostHandle = false; + // Render component chain entries + this._chain = { + comp: this, + next: null + }; + this._postChain = null; }, onEnable () { this.node._renderComponent = this; + // When parent children order dirty, its children's render component will be rechained in sortAllChildren + if (!this.node.parent._reorderChildDirty) { + hierarchyChain.rebuildSelf(this.node); + } }, onDisable () { this.node._renderComponent = null; + // When parent children order dirty, its children's render component will be rechained in sortAllChildren + if (!this.node.parent._reorderChildDirty) { + hierarchyChain.rebuildSelf(this.node); + } }, onDestroy () { diff --git a/cocos2d/core/renderer/render-component-walker.js b/cocos2d/core/renderer/render-component-walker.js index 47e99c9dfe..d0f243c759 100644 --- a/cocos2d/core/renderer/render-component-walker.js +++ b/cocos2d/core/renderer/render-component-walker.js @@ -27,6 +27,7 @@ const macro = require('../platform/CCMacro'); const renderEngine = require('./render-engine'); const defaultVertexFormat = require('./vertex-format'); const StencilManager = require('./stencil-manager'); +const hierarchyChain = require('./utils/hierarchy-chain'); const gfx = renderEngine.gfx; const RecyclePool = renderEngine.RecyclePool; const InputAssembler = renderEngine.InputAssembler; @@ -97,9 +98,6 @@ var RenderComponentWalker = function (device, renderScene) { this._sortKey = 0; this._curCameraNode = null; - - this._handleRender = this._handleRender.bind(this); - this._postHandleRender = this._postHandleRender.bind(this); }; RenderComponentWalker.prototype = { @@ -138,31 +136,6 @@ RenderComponentWalker.prototype = { this._stencilMgr.reset(); }, - _handleRender (node) { - if (node.groupIndex !== 0) { - this._curCameraNode = node; - } - - let group = this._curCameraNode ? this._curCameraNode.groupIndex : node.groupIndex; - node._cullingMask = 1 << group; - - let comp = node._renderComponent; - if (comp) { - this.renderComp(comp, comp.constructor._assembler); - } - }, - - _postHandleRender (node) { - let comp = node._renderComponent; - if (comp && comp.constructor._postAssembler) { - this.renderComp(comp, comp.constructor._postAssembler); - } - - if (this._curCameraNode === node) { - this._curCameraNode = null; - } - }, - _flush (batchData) { let vfmt = batchData.vfmt, material = batchData.material, @@ -308,8 +281,33 @@ RenderComponentWalker.prototype = { visit (scene) { this.reset(); - // Store all render components to _queue - scene.walk(this._handleRender, this._postHandleRender); + let entry = hierarchyChain.entry(scene); + while (entry) { + let comp = entry.comp; + + // Pre handle + if (!entry.post) { + let node = comp.node; + if (node.groupIndex !== 0) { + this._curCameraNode = node; + } + + let group = this._curCameraNode ? this._curCameraNode.groupIndex : node.groupIndex; + node._cullingMask = 1 << group; + + this.renderComp(comp, comp.constructor._assembler); + } + // Post handle + else { + this.renderComp(comp, comp.constructor._postAssembler); + + if (this._curCameraNode === node) { + this._curCameraNode = null; + } + } + + entry = entry.next; + } this._flush(_batchData); } diff --git a/cocos2d/core/renderer/utils/hierarchy-chain.js b/cocos2d/core/renderer/utils/hierarchy-chain.js new file mode 100644 index 0000000000..ab5da98603 --- /dev/null +++ b/cocos2d/core/renderer/utils/hierarchy-chain.js @@ -0,0 +1,177 @@ +/**************************************************************************** + Copyright (c) 2013-2016 Chukong Technologies Inc. + Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + + 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. + ****************************************************************************/ + +function findPrevious (node) { + let parent = node.parent; + // reach root scene + if (!parent) { + return null; + } + + // Find previous render component in siblings + let children = parent.children; + let index = children.indexOf(node); + let previous = null, comp = null; + for (let i = index - 1; i >= 0; i--) { + let sibling = children[i]; + previous = findLast(sibling); + if (previous) { + return previous; + } + } + + // Find previous render component in parent level + if (parent._renderComponent) { + return parent._renderComponent._chain; + } + else { + return findPrevious(parent); + } +} + +function findNext (node) { + let parent = node.parent; + // reach root scene + if (!parent) { + return null; + } + + // Find next render component in siblings + let children = parent.children; + let index = children.indexOf(node); + let next = null; + for (let i = index + 1; i < children.length; i++) { + let sibling = children[i]; + next = findFirst(sibling); + if (next) { + return next; + } + } + + let comp = parent._renderComponent; + if (comp && comp.constructor._postAssembler) { + if (!comp._postChain) { + comp._postChain = { + post: true, + comp: comp, + next: null + } + } + // If parent's component has post assembler, we need to chain it to the next component + return comp._postChain; + } + + // Find next render component in parent level + return findNext(parent); +} + +function findFirst (node) { + if (node._renderComponent) { + return node._renderComponent._chain; + } + + let children = node.children; + let first; + for (let i = 0; i < children.length; i++) { + let child = children[i]; + first = findFirst(child); + if (first) { + return first; + } + } + return null; +} + +function findLast (node) { + let comp = node._renderComponent; + if (comp && comp.constructor._postAssembler) { + if (!comp._postChain) { + comp._postChain = { + post: true, + comp: comp, + next: null + } + } + // If parent's component has post assembler, we need to chain it to the next component + return comp._postChain; + } + + let children = node.children; + let last; + for (let i = children.length - 1; i >= 0; i--) { + last = findLast(children[i]); + if (last) { + return last; + } + } + return comp ? comp._chain : null; +} + +module.exports = { + rebuildSelf (node) { + let previous = findPrevious(node); + let next = findNext(node); + let first = findFirst(node); + // If first not exist, then no render component inside the current node + if (first) { + if (previous) { + previous.next = first; + } + let last = findLast(node); + last.next = next; + } + else if (previous) { + previous.next = next; + } + }, + + rebuild (node) { + let previous = node._renderComponent ? node._renderComponent._chain : findPrevious(node); + // Rebuild for all children + let children = node.children; + let curr, first; + for (let i = 0; i < children.length; i++) { + curr = children[i]; + first = findFirst(curr); + // If first not exist, then no render component inside the current node, nothing to do + if (first) { + if (previous) { + // Rebuild chain for previous render component and the first render component in sub tree + previous.next = first; + } + // Set previous to the last render component in sub tree + previous = findLast(curr); + } + } + if (previous) { + // Link the last render component's next to the outside next render component + previous.next = findNext(node); + } + }, + + entry (scene) { + return findFirst(scene); + } +} \ No newline at end of file From 6d0872f1bf8e67728bc4c24f257ec8fca276b78f Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 11 Apr 2018 14:59:39 +0800 Subject: [PATCH 2/3] Small improvements --- cocos2d/core/CCNode.js | 2 +- cocos2d/core/renderer/utils/hierarchy-chain.js | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index e690e0dd2f..97ee498c25 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -1008,7 +1008,7 @@ var Node = cc.Class({ _onHierarchyChanged (oldParent) { this._updateOrderOfArrival(); - if (this._parent && this._parent._delaySort) { + if (this._parent) { this._parent._delaySort(); } this._onHierarchyChangedBase(oldParent); diff --git a/cocos2d/core/renderer/utils/hierarchy-chain.js b/cocos2d/core/renderer/utils/hierarchy-chain.js index ab5da98603..461b153dca 100644 --- a/cocos2d/core/renderer/utils/hierarchy-chain.js +++ b/cocos2d/core/renderer/utils/hierarchy-chain.js @@ -24,14 +24,14 @@ ****************************************************************************/ function findPrevious (node) { - let parent = node.parent; + let parent = node._parent; // reach root scene if (!parent) { return null; } // Find previous render component in siblings - let children = parent.children; + let children = parent._children; let index = children.indexOf(node); let previous = null, comp = null; for (let i = index - 1; i >= 0; i--) { @@ -52,14 +52,14 @@ function findPrevious (node) { } function findNext (node) { - let parent = node.parent; + let parent = node._parent; // reach root scene if (!parent) { return null; } // Find next render component in siblings - let children = parent.children; + let children = parent._children; let index = children.indexOf(node); let next = null; for (let i = index + 1; i < children.length; i++) { @@ -92,7 +92,7 @@ function findFirst (node) { return node._renderComponent._chain; } - let children = node.children; + let children = node._children; let first; for (let i = 0; i < children.length; i++) { let child = children[i]; @@ -118,7 +118,7 @@ function findLast (node) { return comp._postChain; } - let children = node.children; + let children = node._children; let last; for (let i = children.length - 1; i >= 0; i--) { last = findLast(children[i]); @@ -150,7 +150,7 @@ module.exports = { rebuild (node) { let previous = node._renderComponent ? node._renderComponent._chain : findPrevious(node); // Rebuild for all children - let children = node.children; + let children = node._children; let curr, first; for (let i = 0; i < children.length; i++) { curr = children[i]; From c5b586c5d7d8da298957576a7b023c22865339a7 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 11 Apr 2018 20:10:24 +0800 Subject: [PATCH 3/3] Fix old parent chain issue --- cocos2d/core/CCNode.js | 4 ++++ .../core/renderer/render-component-walker.js | 9 ++++---- .../core/renderer/utils/hierarchy-chain.js | 22 ++++++++++++++----- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 97ee498c25..0bae93e421 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -1007,6 +1007,10 @@ var Node = cc.Class({ }, _onHierarchyChanged (oldParent) { + if (oldParent) { + // Unchain from old parent + hierarchyChain.unchainSelf(); + } this._updateOrderOfArrival(); if (this._parent) { this._parent._delaySort(); diff --git a/cocos2d/core/renderer/render-component-walker.js b/cocos2d/core/renderer/render-component-walker.js index d0f243c759..d0e33750b3 100644 --- a/cocos2d/core/renderer/render-component-walker.js +++ b/cocos2d/core/renderer/render-component-walker.js @@ -284,10 +284,11 @@ RenderComponentWalker.prototype = { let entry = hierarchyChain.entry(scene); while (entry) { let comp = entry.comp; + let node = comp.node; + let assembler = null; // Pre handle if (!entry.post) { - let node = comp.node; if (node.groupIndex !== 0) { this._curCameraNode = node; } @@ -295,16 +296,16 @@ RenderComponentWalker.prototype = { let group = this._curCameraNode ? this._curCameraNode.groupIndex : node.groupIndex; node._cullingMask = 1 << group; - this.renderComp(comp, comp.constructor._assembler); + assembler = comp.constructor._assembler; } // Post handle else { - this.renderComp(comp, comp.constructor._postAssembler); - if (this._curCameraNode === node) { this._curCameraNode = null; } + assembler = comp.constructor._postAssembler; } + this.renderComp(comp, assembler); entry = entry.next; } diff --git a/cocos2d/core/renderer/utils/hierarchy-chain.js b/cocos2d/core/renderer/utils/hierarchy-chain.js index 461b153dca..38fe8401c4 100644 --- a/cocos2d/core/renderer/utils/hierarchy-chain.js +++ b/cocos2d/core/renderer/utils/hierarchy-chain.js @@ -130,20 +130,30 @@ function findLast (node) { } module.exports = { + unchainSelf (node) { + let last = findLast(node); + if (last) { + // Chain previous node to the next node + let previous = findPrevious(node); + if (previous) { + previous.next = last.next; + } + } + }, + rebuildSelf (node) { let previous = findPrevious(node); - let next = findNext(node); let first = findFirst(node); // If first not exist, then no render component inside the current node if (first) { + let last = findLast(node); if (previous) { + last.next = previous.next; previous.next = first; } - let last = findLast(node); - last.next = next; - } - else if (previous) { - previous.next = next; + else { + last.next = null; + } } },