mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
resolve UI material issue
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
export default class Pool<T> {
|
||||
|
||||
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<T>(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;
|
||||
}
|
||||
}
|
||||
@@ -4,16 +4,13 @@ export default class RecyclePool<T = any> {
|
||||
private _fn: () => T;
|
||||
private _count = 0;
|
||||
private _data: T[];
|
||||
private _objIdx: Map<T, number>;
|
||||
|
||||
constructor (fn: () => T, size: number) {
|
||||
this._fn = fn;
|
||||
this._data = new Array(size);
|
||||
this._objIdx = new Map<T, number>();
|
||||
|
||||
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<T = any> {
|
||||
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<T = any> {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<GFXPipelineState> | 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+56
-49
@@ -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<UIBufferBatch> = new RecyclePool(() => {
|
||||
return new UIBufferBatch();
|
||||
}, 128);
|
||||
private _drawBatchPool: RecyclePool<UIDrawBatch> = new RecyclePool(() => {
|
||||
private _drawBatchPool: Pool<UIDrawBatch> = 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<number, UIMaterial> = new Map<number, UIMaterial>();
|
||||
private _uboUI: UBOUI = new UBOUI();
|
||||
private _uiUBO: GFXBuffer | null = null;
|
||||
private _batches: CachedArray<UIDrawBatch>;
|
||||
private _bufferInitData: IMeshBufferInitData | null = null;
|
||||
private _commitUIRenderDatas: IUIRenderData[] = [];
|
||||
private _commitUICanvas: IUICanvas[] = [];
|
||||
private _sortChildList: RecyclePool = new RecyclePool(() => {
|
||||
private _sortChildList: Pool<any[]> = new Pool(() => {
|
||||
return [];
|
||||
}, 128);
|
||||
|
||||
@@ -194,7 +198,7 @@ export class UI {
|
||||
type: GFXCommandBufferType.PRIMARY,
|
||||
});
|
||||
|
||||
this._uiMaterial = this.createUIMaterial({ material: builtinResMgr.get<Material>('sprite-material')});
|
||||
// this._uiMaterial = this.createUIMaterial({ material: builtinResMgr.get<Material>('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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user