From ba491ae9d51fa871bf4c1fe7d71dc388bec5153d Mon Sep 17 00:00:00 2001 From: zhuxuguang <247274249@qq.com> Date: Mon, 18 Feb 2019 20:54:50 +0800 Subject: [PATCH] resolve UI material issue --- cocos/3d/memop/pool.js | 45 --------- cocos/3d/memop/pool.ts | 60 ++++++++++++ cocos/3d/memop/recycle-pool.ts | 12 --- cocos/3d/ui/assembler/sprite/simple.ts | 2 +- cocos/3d/ui/components/sprite-component.ts | 6 +- cocos/renderer/scene/model.ts | 10 +- cocos/renderer/ui/ui-material.ts | 18 ++++ cocos/renderer/ui/ui.ts | 105 +++++++++++---------- 8 files changed, 143 insertions(+), 115 deletions(-) delete mode 100644 cocos/3d/memop/pool.js create mode 100644 cocos/3d/memop/pool.ts diff --git a/cocos/3d/memop/pool.js b/cocos/3d/memop/pool.js deleted file mode 100644 index 4db7e02a64..0000000000 --- a/cocos/3d/memop/pool.js +++ /dev/null @@ -1,45 +0,0 @@ -export default class Pool { - constructor(fn, size) { - this._fn = fn; - this._idx = size - 1; - this._frees = new Array(size); - - for (let i = 0; i < size; ++i) { - this._frees[i] = fn(); - } - } - - _expand(size) { - let old = this._frees; - this._frees = new Array(size); - - let len = size - old.length; - for (let i = 0; i < len; ++i) { - this._frees[i] = this._fn(); - } - - for (let i = len, j = 0; i < size; ++i, ++j) { - this._frees[i] = old[j]; - } - - this._idx += len; - } - - alloc() { - // create some more space (expand by 20%, minimum 1) - if (this._idx < 0) { - this._expand(Math.round(this._frees.length * 1.2) + 1); - } - - let ret = this._frees[this._idx]; - this._frees[this._idx] = null; - --this._idx; - - return ret; - } - - free(obj) { - ++this._idx; - this._frees[this._idx] = obj; - } -} \ No newline at end of file diff --git a/cocos/3d/memop/pool.ts b/cocos/3d/memop/pool.ts new file mode 100644 index 0000000000..f3813bbfa6 --- /dev/null +++ b/cocos/3d/memop/pool.ts @@ -0,0 +1,60 @@ +export default class Pool { + + private _fn: () => T; + private _idx: number; + private _frees: T[]; + + constructor (fn: () => T, size: number) { + this._fn = fn; + this._idx = size - 1; + this._frees = new Array(size); + + for (let i = 0; i < size; ++i) { + this._frees[i] = fn(); + } + } + + private _expand (size: number) { + const old = this._frees; + this._frees = new Array(size); + + const len = size - old.length; + for (let i = 0; i < len; ++i) { + this._frees[i] = this._fn(); + } + + for (let i = len, j = 0; i < size; ++i, ++j) { + this._frees[i] = old[j]; + } + + this._idx += len; + } + + public alloc (): T { + // create some more space (expand by 20%, minimum 1) + if (this._idx < 0) { + this._expand(Math.round(this._frees.length * 1.2) + 1); + } + + const ret = this._frees[this._idx]; + this._frees[this._idx] = null; + --this._idx; + + return ret; + } + + public free (obj: T) { + ++this._idx; + this._frees[this._idx] = obj; + } + + public clear (fn: (obj: T) => void) { + for (let i = 0; i <= this._idx; i++) { + if (fn) { + fn(this._frees[i]); + } + } + this._frees.splice(0); + this._idx = -1; + } +} diff --git a/cocos/3d/memop/recycle-pool.ts b/cocos/3d/memop/recycle-pool.ts index 67f8b5a89f..c74a3d3970 100644 --- a/cocos/3d/memop/recycle-pool.ts +++ b/cocos/3d/memop/recycle-pool.ts @@ -4,16 +4,13 @@ export default class RecyclePool { private _fn: () => T; private _count = 0; private _data: T[]; - private _objIdx: Map; constructor (fn: () => T, size: number) { this._fn = fn; this._data = new Array(size); - this._objIdx = new Map(); for (let i = 0; i < size; ++i) { this._data[i] = fn(); - this._objIdx.set(this._data[i], i); } } @@ -33,7 +30,6 @@ export default class RecyclePool { if (size > this._data.length) { for (let i = this._data.length; i < size; ++i) { this._data[i] = this._fn(); - this._objIdx.set(this._data[i], i); } } } @@ -55,17 +51,9 @@ export default class RecyclePool { const tmp = this._data[idx]; this._data[idx] = this._data[last]; this._data[last] = tmp; - this._objIdx.set(this._data[idx], idx); - this._objIdx.set(this._data[last], last); this._count -= 1; } - public remove (obj: T) { - if (this._objIdx.has(obj)) { - this.removeAt(this._objIdx.get(obj)!); - } - } - public sort (compare: (a: T, b: T) => number) { return sort(this._data, 0, this._count, compare); } diff --git a/cocos/3d/ui/assembler/sprite/simple.ts b/cocos/3d/ui/assembler/sprite/simple.ts index 283db1754c..3fc4d4f507 100644 --- a/cocos/3d/ui/assembler/sprite/simple.ts +++ b/cocos/3d/ui/assembler/sprite/simple.ts @@ -142,7 +142,7 @@ export const simple: IAssembler = { ibuf![indiceOffset++] = vertexId + 2; commitBuffer.meshBuffer = buffer; - commitBuffer.material = sprite.material!; + commitBuffer.material = sprite.sharedMaterial!; commitBuffer.texture = sprite.spriteFrame!; commitBuffer.priority = sprite.priority; renderer.addToQueue(commitBuffer); diff --git a/cocos/3d/ui/components/sprite-component.ts b/cocos/3d/ui/components/sprite-component.ts index 74536b7815..1ef6bf703d 100644 --- a/cocos/3d/ui/components/sprite-component.ts +++ b/cocos/3d/ui/components/sprite-component.ts @@ -400,7 +400,7 @@ export class SpriteComponent extends UIRenderComponent { } public updateAssembler (render: UI) { - if (!this._spriteFrame || !this._material) { + if (!this._spriteFrame || !this.sharedMaterial) { return; } super.updateAssembler(render); @@ -432,7 +432,7 @@ export class SpriteComponent extends UIRenderComponent { if (!this._renderData) { if (this._assembler && this._assembler.createData) { this._renderData = this._assembler.createData(this); - this._renderData.material = this._material; + this._renderData.material = this.sharedMaterial; this.markForUpdateRenderData(); } } @@ -440,7 +440,7 @@ export class SpriteComponent extends UIRenderComponent { private _activateMaterial () { const spriteFrame = this._spriteFrame; - const material = this._material; + const material = this.sharedMaterial; // WebGL if (cc.game.renderType !== cc.game.RENDER_TYPE_CANVAS) { // if (!material) { diff --git a/cocos/renderer/scene/model.ts b/cocos/renderer/scene/model.ts index 481def9e1c..96ea742157 100644 --- a/cocos/renderer/scene/model.ts +++ b/cocos/renderer/scene/model.ts @@ -2,7 +2,7 @@ import { Material } from '../../3d/assets/material'; import { IRenderingSubmesh } from '../../3d/assets/mesh'; import { aabb } from '../../3d/geom-utils'; -import { RecyclePool } from '../../3d/memop'; +import Pool from '../../3d/memop/pool'; import { Vec3 } from '../../core/value-types'; import { mat4 } from '../../core/vmath'; import { GFXBuffer } from '../../gfx/buffer'; @@ -17,7 +17,7 @@ import { SubModel } from './submodel'; const _temp_floatx16 = new Float32Array(16); const _temp_mat4 = mat4.create(); -const _subMeshPool = new RecyclePool(() => { +const _subMeshPool = new Pool(() => { return new SubModel(); }, 32); @@ -61,7 +61,7 @@ export class Model { public destroy () { for (const subModel of this._subModels) { subModel.destroy(); - _subMeshPool.remove(subModel); + _subMeshPool.free(subModel); } if (this._localUBO) { this._localUBO.destroy(); @@ -178,7 +178,7 @@ export class Model { public initSubModel (idx: number, subMeshData: IRenderingSubmesh, mat: Material) { if (this._subModels[idx] == null) { - this._subModels[idx] = _subMeshPool.add(); + this._subModels[idx] = _subMeshPool.alloc(); } else { const oldMat = this._subModels[idx].material; this._subModels[idx].destroy(); @@ -190,7 +190,7 @@ export class Model { public setSubModelMesh (idx: number, subMeshData: IRenderingSubmesh) { if (this._subModels[idx] == null) { - this._subModels[idx] = _subMeshPool.add(); + this._subModels[idx] = _subMeshPool.alloc(); } this._subModels[idx].subMeshData = subMeshData; } diff --git a/cocos/renderer/ui/ui-material.ts b/cocos/renderer/ui/ui-material.ts index 60cfbd776e..50ad083f9c 100644 --- a/cocos/renderer/ui/ui-material.ts +++ b/cocos/renderer/ui/ui-material.ts @@ -2,6 +2,7 @@ import { Material } from '../../3d/assets/material'; import { GFXBindingLayout } from '../../gfx/binding-layout'; import { GFXPipelineState } from '../../gfx/pipeline-state'; import { Pass } from '../core/pass'; +import Pool from '../../3d/memop/pool'; export interface IUIMaterialInfo { material: Material; @@ -19,8 +20,10 @@ export class UIMaterial { protected _material: Material | null = null; protected _pass: Pass | null = null; + private _psos: Pool | null; constructor () { + this._psos = null; } public initialize (info: IUIMaterialInfo): boolean { @@ -33,10 +36,25 @@ export class UIMaterial { this._pass = this._material.passes[0]; + this._psos = new Pool(() => this._pass!.createPipelineState()!, 1); + return true; } + public getPipelineState (): GFXPipelineState { + return this._psos!.alloc(); + } + + public revertPipelineState (pso: GFXPipelineState) { + this._psos!.free(pso); + } + public destroy () { this._material = null; + if (this._psos) { + this._psos.clear((obj: GFXPipelineState) => { + this._pass!.destroyPipelineState(obj); + }); + } } } diff --git a/cocos/renderer/ui/ui.ts b/cocos/renderer/ui/ui.ts index 8e1e8e49e3..bc0046079e 100644 --- a/cocos/renderer/ui/ui.ts +++ b/cocos/renderer/ui/ui.ts @@ -32,6 +32,7 @@ import { Node } from '../../scene-graph/node'; import { Camera, CameraProjection } from '../scene/camera'; import { RenderScene } from '../scene/render-scene'; import { IUIMaterialInfo, UIMaterial } from './ui-material'; +import Pool from '../../3d/memop/pool'; export class UBOUI { public static MAT_VIEW_PROJ_OFFSET: number = 0; @@ -71,19 +72,22 @@ export class UIDrawBatch { public pipelineState: GFXPipelineState | null = null; public bindingLayout: GFXBindingLayout | null = null; - public destroy () { + public destroy (ui: UI) { if (this.pipelineState) { - this.pipelineState.destroy(); + ui._getUIMaterial(this.material!).revertPipelineState(this.pipelineState); this.pipelineState = null; } if (this.bindingLayout) { - this.bindingLayout.destroy(); this.bindingLayout = null; } } - public clear () { + public clear (ui: UI) { + if (this.pipelineState) { + ui._getUIMaterial(this.material!).revertPipelineState(this.pipelineState); + this.pipelineState = null; + } this.camera = null; this.bufferBatch = null; this.material = null; @@ -116,7 +120,7 @@ export class UI { private _bufferBatchPool: RecyclePool = new RecyclePool(() => { return new UIBufferBatch(); }, 128); - private _drawBatchPool: RecyclePool = new RecyclePool(() => { + private _drawBatchPool: Pool = new Pool(() => { return new UIDrawBatch(); }, 128); private _device: GFXDevice; @@ -130,14 +134,14 @@ export class UI { private _vertF32Count = 0; private _bufferBatches: UIBufferBatch[] = []; private _uiMaterial: UIMaterial | null = null; - private _uiMaterials: UIMaterial[] = []; + private _uiMaterials: Map = new Map(); private _uboUI: UBOUI = new UBOUI(); private _uiUBO: GFXBuffer | null = null; private _batches: CachedArray; private _bufferInitData: IMeshBufferInitData | null = null; private _commitUIRenderDatas: IUIRenderData[] = []; private _commitUICanvas: IUICanvas[] = []; - private _sortChildList: RecyclePool = new RecyclePool(() => { + private _sortChildList: Pool = new Pool(() => { return []; }, 128); @@ -194,7 +198,7 @@ export class UI { type: GFXCommandBufferType.PRIMARY, }); - this._uiMaterial = this.createUIMaterial({ material: builtinResMgr.get('sprite-material')}); + // this._uiMaterial = this.createUIMaterial({ material: builtinResMgr.get('sprite-material') }); this._uiUBO = this._device.createBuffer({ usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, @@ -206,7 +210,7 @@ export class UI { } public destroy () { - this.destroyUIMaterials(); + this._destroyUIMaterials(); if (this._uiUBO) { this._uiUBO.destroy(); @@ -214,7 +218,7 @@ export class UI { } for (const batch of this._batches.array) { - batch.destroy(); + batch.destroy(this); } for (const buffBatch of this._bufferBatches) { @@ -224,6 +228,10 @@ export class UI { } this._bufferBatches.splice(0); + for (const uiMat of this._uiMaterials.values()) { + uiMat.destroy(); + } + if (this._cmdBuff) { this._cmdBuff.destroy(); this._cmdBuff = null; @@ -241,31 +249,29 @@ export class UI { // this._camera.update(); // } - public createUIMaterial (info: IUIMaterialInfo): UIMaterial | null { - const uiMtrl = new UIMaterial(); - if (uiMtrl.initialize(info)) { - this._uiMaterials.push(uiMtrl); - return uiMtrl; + public _getUIMaterial (mat: Material): UIMaterial { + if (this._uiMaterials.has(mat.hash)) { + return this._uiMaterials.get(mat.hash)!; } else { - return null; + const uiMat = new UIMaterial(); + uiMat.initialize({ material: mat }); + this._uiMaterials.set(mat.hash, uiMat); + return uiMat; } } - public destroyUIMaterial (uiMaterial: UIMaterial) { - for (let i = 0; i < this._uiMaterials.length; ++i) { - if (this._uiMaterials[i] === uiMaterial) { - uiMaterial.destroy(); - this._uiMaterials.splice(i); - return; - } + private _deleteUIMaterial (mat: Material) { + if (this._uiMaterials.has(mat.hash)) { + this._uiMaterials.get(mat.hash)!.destroy(); + this._uiMaterials.delete(mat.hash); } } - public destroyUIMaterials () { - for (const uiMaterial of this._uiMaterials) { - uiMaterial.destroy(); + private _destroyUIMaterials () { + for (const uiMat of this._uiMaterials.values()) { + uiMat.destroy(); } - this._uiMaterials = []; + this._uiMaterials.clear(); } public addScreen (comp) { @@ -311,8 +317,8 @@ export class UI { for (let i = 0; i < this._batches.length; ++i) { const batch = this._batches.array[i]; - batch.clear(); - this._drawBatchPool.remove(batch); + batch.clear(this); + this._drawBatchPool.free(batch); } this._batches.clear(); this._commitUICanvas = []; @@ -340,7 +346,6 @@ export class UI { public render () { - const material = this._uiMaterial!; if (this._batches.length) { const framebuffer = this._root.curWindow!.framebuffer; @@ -366,6 +371,7 @@ export class UI { // TODO: add screen adapter // update ubo + camera.matViewProj.m14 /= 1000; // hack:to be resolved later mat4.array(_mat4Array, camera.matViewProj); this._uboUI.view.set(_mat4Array, UBOUI.MAT_VIEW_PROJ_OFFSET); @@ -398,13 +404,13 @@ export class UI { } } - private _walk (node: Node, fn1, fn2, level = 0, isRenderComp = false) { + private _walk (node: Node, fn1, fn2, level = 0, isRenderComp = false) { let resortNodeList; const len = node.childrenCount; fn1(node); - if (len > 0){ + if (len > 0) { resortNodeList = this._defineNodeOrder(node); for (const comp of resortNodeList) { this._walk(comp, fn1, fn2, level); @@ -414,7 +420,7 @@ export class UI { // this._walk(child, fn1, fn2, level); // } - this._sortChildList.remove(resortNodeList); + this._sortChildList.free(resortNodeList); } fn2(node); @@ -422,13 +428,15 @@ export class UI { } private _defineNodeOrder (node: Node) { - let sortList: any[] = this._sortChildList.add(); + let sortList: any[] = this._sortChildList.alloc(); sortList = node.children.slice(); sortList.sort((a, b) => { const ca = a.getComponent(UIRenderComponent); const cb = b.getComponent(UIRenderComponent); - if (ca && cb) { return ca.priority - cb.priority; } + if (ca && cb) { + return ca.priority - cb.priority; + } else if (!ca) { return -Number.MAX_SAFE_INTEGER; } else { @@ -489,6 +497,7 @@ export class UI { private mergeBatches () { let curTexView: GFXTextureView | null = null; + let curMaterialHash: number = 0; let bufferBatchIdx = 0; let curBufferBatch: UIBufferBatch = this._bufferBatches[bufferBatchIdx++]; let firstIdx = 0; @@ -561,9 +570,10 @@ export class UI { curBufferBatch.vui16![idxCount + n] = vui16[n] + vertCount; } - if (curTexView !== uiRenderData.texture.getGFXTextureView()) { - if (curTexView){ - curDrawBatch = this._drawBatchPool.add(); + if (curMaterialHash !== uiRenderData.material.hash || + curTexView !== uiRenderData.texture.getGFXTextureView()) { + if (curTexView) { + curDrawBatch = this._drawBatchPool.alloc(); curDrawBatch.camera = uiCanvas.camera; curDrawBatch.bufferBatch = curBufferBatch; curDrawBatch.material = uiRenderData.material; @@ -572,15 +582,14 @@ export class UI { curDrawBatch.idxCount = idxCount - firstIdx; firstIdx = idxCount; - if (!curDrawBatch.pipelineState) { - curDrawBatch.pipelineState = this._uiMaterial!.pass.createPipelineState(); - curDrawBatch.bindingLayout = curDrawBatch.pipelineState!.pipelineLayout.layouts[0]; - } + curDrawBatch.pipelineState = this._getUIMaterial(uiRenderData.material).getPipelineState(); + curDrawBatch.bindingLayout = curDrawBatch.pipelineState!.pipelineLayout.layouts[0]; this._batches.push(curDrawBatch); } curTexView = uiRenderData.texture.getGFXTextureView(); + curMaterialHash = uiRenderData.material.hash; // firstIdx = idxCount; // idxCount = 0; @@ -589,10 +598,10 @@ export class UI { idxCount += vui16.length; vertCount += vCount; - isEnding = index === len; + isEnding = index === len; // get the last data - if (isEnding){ - curDrawBatch = this._drawBatchPool.add(); + if (isEnding) { + curDrawBatch = this._drawBatchPool.alloc(); curDrawBatch.camera = uiCanvas.camera; curDrawBatch.bufferBatch = curBufferBatch; curDrawBatch.material = uiRenderData.material; @@ -601,10 +610,8 @@ export class UI { curDrawBatch.idxCount = idxCount - firstIdx; firstIdx = idxCount; - if (!curDrawBatch.pipelineState) { - curDrawBatch.pipelineState = this._uiMaterial!.pass.createPipelineState(); - curDrawBatch.bindingLayout = curDrawBatch.pipelineState!.pipelineLayout.layouts[0]; - } + curDrawBatch.pipelineState = this._getUIMaterial(uiRenderData.material).getPipelineState(); + curDrawBatch.bindingLayout = curDrawBatch.pipelineState!.pipelineLayout.layouts[0]; this._batches.push(curDrawBatch); }