diff --git a/cocos/core/memop/cached-array.ts b/cocos/core/memop/cached-array.ts index 2388d0bba8..5f7b7b4727 100644 --- a/cocos/core/memop/cached-array.ts +++ b/cocos/core/memop/cached-array.ts @@ -2,12 +2,12 @@ export class CachedArray { public array: T[]; public length: number = 0; + private cache: Array; private _compareFn; - private _capacity: number = 0; constructor (length: number, compareFn?: (a: T, b: T) => number) { this.array = new Array(length); - this._capacity = length; + this.cache = this.array; this.length = 0; if (compareFn !== undefined) { @@ -17,27 +17,7 @@ export class CachedArray { } } - public reserve (length: number) { - if (length > this._capacity) { - const temp = this.array; - this.array = new Array(length); - for (let i = 0; i < this._capacity; ++i) { - this.array[i] = temp[i]; - } - this._capacity = length; - } - } - public push (item: T) { - if (this.length > this._capacity) { - const temp = this.array; - this.array = new Array(this.length); - for (let i = 0; i < this._capacity; ++i) { - this.array[i] = temp[i]; - } - this._capacity = this.length; - } - this.array[this.length++] = item; } @@ -50,6 +30,7 @@ export class CachedArray { } public clear () { + this.cache.fill(null); this.length = 0; } @@ -59,30 +40,12 @@ export class CachedArray { } public concat (array: CachedArray) { - if (array.length + this.length > this._capacity) { - const temp = this.array; - this.array = new Array(array.length + this.length); - for (let i = 0; i < this._capacity; ++i) { - this.array[i] = temp[i]; - } - this._capacity = this.array.length; - } - for (let i = 0; i < array.length; ++i) { this.array[this.length++] = array.array[i]; } } public append (array: T[]) { - if (array.length + this.length > this._capacity) { - const temp = this.array; - this.array = new Array(array.length + this.length); - for (let i = 0; i < this._capacity; ++i) { - this.array[i] = temp[i]; - } - this._capacity = this.array.length; - } - for (const item of array) { this.array[this.length++] = item; } diff --git a/cocos/gfx/webgl/webgl-command-allocator.ts b/cocos/gfx/webgl/webgl-command-allocator.ts index 819f45ce97..fa5790db8d 100644 --- a/cocos/gfx/webgl/webgl-command-allocator.ts +++ b/cocos/gfx/webgl/webgl-command-allocator.ts @@ -27,11 +27,12 @@ export class WebGLGFXCommandPool { this._freeIdx = count - 1; } - /* */ + /* public alloc (clazz: new() => T): T { return new clazz(); } - /* * + */ + public alloc (clazz: new() => T): T { if (this._freeIdx < 0) { const size = this._frees.length * 2; @@ -50,18 +51,24 @@ export class WebGLGFXCommandPool { this._freeIdx += increase; } - const cmd = this._frees[this._freeIdx]; + const cmd = this._frees[this._freeIdx]!; this._frees[this._freeIdx--] = null; - return cmd!; + ++cmd.refCount; + return cmd; } - /* */ public free (cmd: T) { - this._freeCmds.push(cmd); + if (--cmd.refCount === 0) { + this._freeCmds.push(cmd); + } } public freeCmds (cmds: CachedArray) { - // this._freeCmds.concat(cmds); + for (let i = 0; i < cmds.length; ++i) { + if (--cmds.array[i].refCount === 0) { + this._freeCmds.push(cmds.array[i]); + } + } } public release () { diff --git a/cocos/gfx/webgl/webgl-command-buffer.ts b/cocos/gfx/webgl/webgl-command-buffer.ts index 2d3e6848ad..3f6d77f9e4 100644 --- a/cocos/gfx/webgl/webgl-command-buffer.ts +++ b/cocos/gfx/webgl/webgl-command-buffer.ts @@ -122,16 +122,14 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer { clearDepth: number, clearStencil: number) { const cmd = this._webGLAllocator!.beginRenderPassCmdPool.alloc(WebGLCmdBeginRenderPass); - if (cmd) { - cmd.gpuFramebuffer = ( framebuffer as WebGLGFXFramebuffer).gpuFramebuffer; - cmd.renderArea = renderArea; - cmd.clearColors = clearColors.slice(); - cmd.clearDepth = clearDepth; - cmd.clearStencil = clearStencil; - this.cmdPackage.beginRenderPassCmds.push(cmd); + cmd.gpuFramebuffer = ( framebuffer as WebGLGFXFramebuffer).gpuFramebuffer; + cmd.renderArea = renderArea; + cmd.clearColors = clearColors.slice(); + cmd.clearDepth = clearDepth; + cmd.clearStencil = clearStencil; + this.cmdPackage.beginRenderPassCmds.push(cmd); - this.cmdPackage.cmds.push(WebGLCmd.BEGIN_RENDER_PASS); - } + this.cmdPackage.cmds.push(WebGLCmd.BEGIN_RENDER_PASS); this._isInRenderPass = true; } @@ -409,11 +407,36 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer { for (let i = 0; i < count; ++i) { const webGLCmdBuff = cmdBuffs[i] as WebGLGFXCommandBuffer; - this.cmdPackage.beginRenderPassCmds.concat(webGLCmdBuff.cmdPackage.beginRenderPassCmds); - this.cmdPackage.bindStatesCmds.concat(webGLCmdBuff.cmdPackage.bindStatesCmds); - this.cmdPackage.drawCmds.concat(webGLCmdBuff.cmdPackage.drawCmds); - this.cmdPackage.updateBufferCmds.concat(webGLCmdBuff.cmdPackage.updateBufferCmds); - this.cmdPackage.copyBufferToTextureCmds.concat(webGLCmdBuff.cmdPackage.copyBufferToTextureCmds); + for (let c = 0; c < webGLCmdBuff.cmdPackage.beginRenderPassCmds.length; ++c) { + const cmd = webGLCmdBuff.cmdPackage.beginRenderPassCmds.array[c]; + ++cmd.refCount; + this.cmdPackage.beginRenderPassCmds.push(cmd); + } + + for (let c = 0; c < webGLCmdBuff.cmdPackage.bindStatesCmds.length; ++c) { + const cmd = webGLCmdBuff.cmdPackage.bindStatesCmds.array[c]; + ++cmd.refCount; + this.cmdPackage.bindStatesCmds.push(cmd); + } + + for (let c = 0; c < webGLCmdBuff.cmdPackage.drawCmds.length; ++c) { + const cmd = webGLCmdBuff.cmdPackage.drawCmds.array[c]; + ++cmd.refCount; + this.cmdPackage.drawCmds.push(cmd); + } + + for (let c = 0; c < webGLCmdBuff.cmdPackage.updateBufferCmds.length; ++c) { + const cmd = webGLCmdBuff.cmdPackage.updateBufferCmds.array[c]; + ++cmd.refCount; + this.cmdPackage.updateBufferCmds.push(cmd); + } + + for (let c = 0; c < webGLCmdBuff.cmdPackage.copyBufferToTextureCmds.length; ++c) { + const cmd = webGLCmdBuff.cmdPackage.copyBufferToTextureCmds.array[c]; + ++cmd.refCount; + this.cmdPackage.copyBufferToTextureCmds.push(cmd); + } + this.cmdPackage.cmds.concat(webGLCmdBuff.cmdPackage.cmds); } } diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index 3272a9361e..6d967bb34f 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -355,6 +355,7 @@ export enum WebGLCmd { export abstract class WebGLCmdObject { public cmdType: WebGLCmd; + public refCount: number = 0; constructor (type: WebGLCmd) { this.cmdType = type; @@ -378,8 +379,6 @@ export class WebGLCmdBeginRenderPass extends WebGLCmdObject { public clear () { this.gpuFramebuffer = null; this.clearColors = []; - this.clearDepth = 1.0; - this.clearStencil = 0; } } diff --git a/cocos/pipeline/render-queue.ts b/cocos/pipeline/render-queue.ts index cac06fa55b..7f81a66e5c 100644 --- a/cocos/pipeline/render-queue.ts +++ b/cocos/pipeline/render-queue.ts @@ -102,9 +102,6 @@ export class RenderQueue { const opaqueCount = this.opaques.length; const cmdBuffCount = opaqueCount + this.transparents.length; - - this.cmdBuffs.reserve(cmdBuffCount); - this.cmdBuffCount = cmdBuffCount; for (let i = 0; i < this.opaques.length; ++i) {