mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
fix command allocator bugs, and optimize cached array
This commit is contained in:
@@ -2,12 +2,12 @@ export class CachedArray<T> {
|
||||
|
||||
public array: T[];
|
||||
public length: number = 0;
|
||||
private cache: Array<T|null>;
|
||||
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<T> {
|
||||
}
|
||||
}
|
||||
|
||||
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<T> {
|
||||
}
|
||||
|
||||
public clear () {
|
||||
this.cache.fill(null);
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
@@ -59,30 +40,12 @@ export class CachedArray<T> {
|
||||
}
|
||||
|
||||
public concat (array: CachedArray<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 (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;
|
||||
}
|
||||
|
||||
@@ -27,11 +27,12 @@ export class WebGLGFXCommandPool<T extends WebGLCmdObject> {
|
||||
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<T extends WebGLCmdObject> {
|
||||
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<T>) {
|
||||
// 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 () {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user