From 87df5e9be730840ede37e9005e62e430fc9b31d9 Mon Sep 17 00:00:00 2001 From: jerrywwl Date: Mon, 21 Jan 2019 21:26:40 +0800 Subject: [PATCH] fix command pool bug --- cocos/3d/framework/model-component.js | 1 - cocos/core/memop/cached-array.ts | 2 +- cocos/gfx/webgl/webgl-command-allocator.ts | 58 +++++++++++++--------- cocos/renderer/ui/ui.ts | 13 +++++ 4 files changed, 48 insertions(+), 26 deletions(-) diff --git a/cocos/3d/framework/model-component.js b/cocos/3d/framework/model-component.js index 2b2114a467..b3be42b7eb 100644 --- a/cocos/3d/framework/model-component.js +++ b/cocos/3d/framework/model-component.js @@ -174,7 +174,6 @@ export class ModelComponent extends RenderableComponent { } onDisable() { - this._getRenderScene().destroyModel(this._model); } onDestroy() { diff --git a/cocos/core/memop/cached-array.ts b/cocos/core/memop/cached-array.ts index de18948305..2388d0bba8 100644 --- a/cocos/core/memop/cached-array.ts +++ b/cocos/core/memop/cached-array.ts @@ -35,7 +35,7 @@ export class CachedArray { for (let i = 0; i < this._capacity; ++i) { this.array[i] = temp[i]; } - this._capacity = length; + this._capacity = this.length; } this.array[this.length++] = item; diff --git a/cocos/gfx/webgl/webgl-command-allocator.ts b/cocos/gfx/webgl/webgl-command-allocator.ts index 7138306483..ce05a34e62 100644 --- a/cocos/gfx/webgl/webgl-command-allocator.ts +++ b/cocos/gfx/webgl/webgl-command-allocator.ts @@ -11,41 +11,51 @@ import { export class WebGLGFXCommandPool { - private _frees: Array; - private _freeIdx: number = 0; + private _count = 0; + private _data: T[]; + private _objIdx: Map; constructor (clazz: new() => T, count: number) { - this._frees = new Array(count); + this._data = new Array(count); + this._objIdx = new Map(); + for (let i = 0; i < count; ++i) { - this._frees[i] = new clazz(); + this._data[i] = new clazz(); + this._objIdx.set(this._data[i], i); } - - this._freeIdx = count - 1; } - public alloc (clazz: new() => T): T | null { - - if (this._freeIdx < 0) { - const size = this._frees.length * 2; - const temp = this._frees; - this._frees = new Array(size); - - const increase = size - temp.length; - for (let i = 0; i < increase; ++i) { - this._frees[i] = new clazz(); + public alloc (clazz: new() => T): T { + if (this._count >= this._data.length) { + if (this._data.length * 2 > this._data.length) { + for (let i = this._data.length; i < this._data.length * 2; ++i) { + this._data[i] = new clazz(); + } } - - this._freeIdx += increase; } - const item = this._frees[this._freeIdx]; - this._frees[this._freeIdx--] = null; - - return item; + return this._data[this._count++]; } - public free (cmd: T) { - this._frees[++this._freeIdx] = cmd; + public freeAt (idx: number) { + if (idx >= this._count) { + return; + } + + const last = this._count - 1; + 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 free (obj: T) { + const idx = this._objIdx.get(obj); + if (idx !== undefined) { + this.freeAt(idx); + } } } diff --git a/cocos/renderer/ui/ui.ts b/cocos/renderer/ui/ui.ts index c2e80a3e0d..19c9e0b108 100644 --- a/cocos/renderer/ui/ui.ts +++ b/cocos/renderer/ui/ui.ts @@ -264,6 +264,19 @@ export class UI { } private render () { + /* + const framebuffer = this._root.curWindow!.framebuffer; + const cmdBuff = this._cmdBuff!; + + cmdBuff.begin(); + cmdBuff.beginRenderPass(framebuffer, this._renderArea, + [{r: 1, g: 1, b: 1, a: 1}], 1.0, 0); + + cmdBuff.end(); + + this._device.queue.submit([cmdBuff]); + */ + if (this._items.length) { const framebuffer = this._root.curWindow!.framebuffer; const cmdBuff = this._cmdBuff!;