mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
optimize command buffer with cached array
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
export class CachedArray<T> {
|
||||
|
||||
public data: Array<T|undefined>;
|
||||
public array: T[];
|
||||
public length: number = 0;
|
||||
private _compareFn;
|
||||
private _cache: Array<T|undefined>;
|
||||
|
||||
constructor (count: number, compareFn?: (a: T, b: T) => number) {
|
||||
this.data = new Array(count);
|
||||
constructor (length: number, compareFn?: (a: T, b: T) => number) {
|
||||
this.array = new Array(length);
|
||||
this._cache = this.array;
|
||||
this.length = 0;
|
||||
|
||||
if (compareFn !== undefined) {
|
||||
@@ -15,25 +17,34 @@ export class CachedArray<T> {
|
||||
}
|
||||
}
|
||||
|
||||
public push (item: T) {
|
||||
if (this.length >= this.data.length) {
|
||||
const size = this.length * 2;
|
||||
const temp = this.data;
|
||||
this.data = new Array(size);
|
||||
for (let i = 0; i < temp.length; ++i) {
|
||||
this.data[i] = temp[i];
|
||||
public reserve (length: number) {
|
||||
if (length > this.array.length) {
|
||||
this.array = new Array(length);
|
||||
for (let i = 0; i < this._cache.length; ++i) {
|
||||
this.array[i] = this._cache[i] as T;
|
||||
}
|
||||
this._cache = this.array;
|
||||
}
|
||||
}
|
||||
|
||||
public push (item: T) {
|
||||
if (this.length > this.array.length) {
|
||||
this.array = new Array(length);
|
||||
for (let i = 0; i < this._cache.length; ++i) {
|
||||
this.array[i] = this._cache[i] as T;
|
||||
}
|
||||
this._cache = this.array;
|
||||
}
|
||||
|
||||
this.data[this.length++] = item;
|
||||
this.array[this.length++] = item;
|
||||
}
|
||||
|
||||
public pop (): T | undefined {
|
||||
return this.data[this.length--];
|
||||
return this.array[this.length--];
|
||||
}
|
||||
|
||||
public get (idx: number): T | undefined {
|
||||
return this.data[idx];
|
||||
public get (idx: number): T {
|
||||
return this.array[idx];
|
||||
}
|
||||
|
||||
public clear () {
|
||||
@@ -41,7 +52,35 @@ export class CachedArray<T> {
|
||||
}
|
||||
|
||||
public sort () {
|
||||
this.data.fill(undefined, this.length, this.data.length);
|
||||
this.data.sort(this._compareFn);
|
||||
this._cache.fill(undefined, this.length, this.array.length);
|
||||
this._cache.sort(this._compareFn);
|
||||
}
|
||||
|
||||
public concat (array: CachedArray<T>) {
|
||||
if (array.length + this.length > this._cache.length) {
|
||||
this.array = new Array(length);
|
||||
for (let i = 0; i < this._cache.length; ++i) {
|
||||
this.array[i] = this._cache[i] as T;
|
||||
}
|
||||
this._cache = this.array;
|
||||
}
|
||||
|
||||
for (let i = 0; i < array.length; ++i) {
|
||||
this._cache[this.length++] = array.array[i];
|
||||
}
|
||||
}
|
||||
|
||||
public append (array: T[]) {
|
||||
if (array.length + this.length > this._cache.length) {
|
||||
this.array = new Array(length);
|
||||
for (let i = 0; i < this._cache.length; ++i) {
|
||||
this.array[i] = this._cache[i] as T;
|
||||
}
|
||||
this._cache = this.array;
|
||||
}
|
||||
|
||||
for (const item of array) {
|
||||
this._cache[this.length++] = item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
import { GFXBindingLayout } from '../binding-layout';
|
||||
import { GFXBuffer, GFXBufferSource } from '../buffer';
|
||||
import { GFXCommandBuffer, IGFXCommandBufferInfo } from '../command-buffer';
|
||||
import { GFXBufferTextureCopy, GFXBufferUsageBit, GFXStatus, GFXTextureLayout, IGFXColor, IGFXRect, GFXCommandBufferType } from '../define';
|
||||
import {
|
||||
GFXBufferTextureCopy,
|
||||
GFXBufferUsageBit,
|
||||
GFXCommandBufferType,
|
||||
GFXStatus,
|
||||
GFXTextureLayout,
|
||||
IGFXColor,
|
||||
IGFXRect,
|
||||
} from '../define';
|
||||
import { GFXDevice } from '../device';
|
||||
import { GFXFramebuffer } from '../framebuffer';
|
||||
import { GFXInputAssembler } from '../input-assembler';
|
||||
@@ -211,12 +219,12 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer {
|
||||
for (let i = 0; i < count; ++i) {
|
||||
const webGLCmdBuff = cmdBuffs[i] as WebGLGFXCommandBuffer;
|
||||
|
||||
this.cmdPackage.beginRenderPassCmds = this.cmdPackage.beginRenderPassCmds.concat(webGLCmdBuff.cmdPackage.beginRenderPassCmds);
|
||||
this.cmdPackage.bindStatesCmds = this.cmdPackage.bindStatesCmds.concat(webGLCmdBuff.cmdPackage.bindStatesCmds);
|
||||
this.cmdPackage.drawCmds = this.cmdPackage.drawCmds.concat(webGLCmdBuff.cmdPackage.drawCmds);
|
||||
this.cmdPackage.updateBufferCmds = this.cmdPackage.updateBufferCmds.concat(webGLCmdBuff.cmdPackage.updateBufferCmds);
|
||||
this.cmdPackage.copyBufferToTextureCmds = this.cmdPackage.copyBufferToTextureCmds.concat(webGLCmdBuff.cmdPackage.copyBufferToTextureCmds);
|
||||
this.cmdPackage.cmds = this.cmdPackage.cmds.concat(webGLCmdBuff.cmdPackage.cmds);
|
||||
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);
|
||||
this.cmdPackage.cmds.concat(webGLCmdBuff.cmdPackage.cmds);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import { WebGLAttrib, WebGLGPUBindingLayout, WebGLGPUBuffer, WebGLGPUFramebuffer
|
||||
WebGLGPUInput, WebGLGPUInputAssembler, WebGLGPUPipelineState, WebGLGPUShader,
|
||||
WebGLGPUTexture, WebGLGPUUniform, WebGLGPUUniformBlock, WebGLGPUUniformSampler } from './webgl-gpu-objects';
|
||||
import { IWebGLTexUnit } from './webgl-state-cache';
|
||||
import { CachedArray } from '../../core/memop/cached-array';
|
||||
|
||||
// tslint:disable: max-line-length
|
||||
|
||||
@@ -442,51 +443,51 @@ export class WebGLCmdCopyBufferToTexture extends WebGLCmdObject {
|
||||
}
|
||||
|
||||
export class WebGLCmdPackage {
|
||||
public cmds: WebGLCmd[] = [];
|
||||
public beginRenderPassCmds: WebGLCmdBeginRenderPass[] = [];
|
||||
public bindStatesCmds: WebGLCmdBindStates[] = [];
|
||||
public drawCmds: WebGLCmdDraw[] = [];
|
||||
public updateBufferCmds: WebGLCmdUpdateBuffer[] = [];
|
||||
public copyBufferToTextureCmds: WebGLCmdCopyBufferToTexture[] = [];
|
||||
public cmds: CachedArray<WebGLCmd> = new CachedArray(1);
|
||||
public beginRenderPassCmds: CachedArray<WebGLCmdBeginRenderPass> = new CachedArray(1);
|
||||
public bindStatesCmds: CachedArray<WebGLCmdBindStates> = new CachedArray(1);
|
||||
public drawCmds: CachedArray<WebGLCmdDraw> = new CachedArray(1);
|
||||
public updateBufferCmds: CachedArray<WebGLCmdUpdateBuffer> = new CachedArray(1);
|
||||
public copyBufferToTextureCmds: CachedArray<WebGLCmdCopyBufferToTexture> = new CachedArray(1);
|
||||
|
||||
public clearCmds (allocator: WebGLGFXCommandAllocator) {
|
||||
|
||||
if (this.beginRenderPassCmds.length) {
|
||||
for (const beginRenderPassCmd of this.beginRenderPassCmds) {
|
||||
allocator.beginRenderPassCmdPool.free(beginRenderPassCmd);
|
||||
for (let i = 0; i < this.beginRenderPassCmds.length; ++i) {
|
||||
allocator.beginRenderPassCmdPool.free(this.beginRenderPassCmds.array[i]);
|
||||
}
|
||||
this.beginRenderPassCmds = [];
|
||||
this.beginRenderPassCmds.clear();
|
||||
}
|
||||
|
||||
if (this.bindStatesCmds.length) {
|
||||
for (const bindStatesCmd of this.bindStatesCmds) {
|
||||
allocator.bindStatesCmdPool.free(bindStatesCmd);
|
||||
for (let i = 0; i < this.bindStatesCmds.length; ++i) {
|
||||
allocator.bindStatesCmdPool.free(this.bindStatesCmds.array[i]);
|
||||
}
|
||||
this.bindStatesCmds = [];
|
||||
this.bindStatesCmds.clear();
|
||||
}
|
||||
|
||||
if (this.drawCmds.length) {
|
||||
for (const drawCmd of this.drawCmds) {
|
||||
allocator.drawCmdPool.free(drawCmd);
|
||||
for (let i = 0; i < this.drawCmds.length; ++i) {
|
||||
allocator.drawCmdPool.free(this.drawCmds.array[i]);
|
||||
}
|
||||
this.drawCmds = [];
|
||||
this.drawCmds.clear();
|
||||
}
|
||||
|
||||
if (this.updateBufferCmds.length) {
|
||||
for (const updateBufferCmd of this.updateBufferCmds) {
|
||||
allocator.updateBufferCmdPool.free(updateBufferCmd);
|
||||
for (let i = 0; i < this.updateBufferCmds.length; ++i) {
|
||||
allocator.updateBufferCmdPool.free(this.updateBufferCmds.array[i]);
|
||||
}
|
||||
this.updateBufferCmds = [];
|
||||
this.updateBufferCmds.clear();
|
||||
}
|
||||
|
||||
if (this.copyBufferToTextureCmds.length) {
|
||||
for (const copyBufferToTextureCmd of this.copyBufferToTextureCmds) {
|
||||
allocator.copyBufferToTextureCmdPool.free(copyBufferToTextureCmd);
|
||||
for (let i = 0; i < this.copyBufferToTextureCmds.length; ++i) {
|
||||
allocator.copyBufferToTextureCmdPool.free(this.copyBufferToTextureCmds.array[i]);
|
||||
}
|
||||
this.copyBufferToTextureCmds = [];
|
||||
this.copyBufferToTextureCmds.clear();
|
||||
}
|
||||
|
||||
this.cmds = [];
|
||||
this.cmds.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1086,12 +1087,13 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web
|
||||
let gpuInputAssembler: WebGLGPUInputAssembler | null = null;
|
||||
let glPrimitive = WebGLRenderingContext.TRIANGLES;
|
||||
|
||||
for (const cmd of cmdPackage.cmds) {
|
||||
for (let i = 0; i < cmdPackage.cmds.length; ++i) {
|
||||
const cmd = cmdPackage.cmds.array[i];
|
||||
const cmdId = cmdIds[cmd]++;
|
||||
|
||||
switch (cmd) {
|
||||
case WebGLCmd.BEGIN_RENDER_PASS: {
|
||||
const cmd0 = cmdPackage.beginRenderPassCmds[cmdId];
|
||||
const cmd0 = cmdPackage.beginRenderPassCmds.array[cmdId];
|
||||
|
||||
let clears: GLbitfield = 0;
|
||||
|
||||
@@ -1263,7 +1265,7 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web
|
||||
}
|
||||
case WebGLCmd.BIND_STATES: {
|
||||
|
||||
const cmd2 = cmdPackage.bindStatesCmds[cmdId];
|
||||
const cmd2 = cmdPackage.bindStatesCmds.array[cmdId];
|
||||
if (cmd2.gpuPipelineState) {
|
||||
|
||||
glPrimitive = cmd2.gpuPipelineState.glPrimitive;
|
||||
@@ -1883,7 +1885,7 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web
|
||||
break;
|
||||
}
|
||||
case WebGLCmd.DRAW: {
|
||||
const cmd3: WebGLCmdDraw = cmdPackage.drawCmds[cmdId];
|
||||
const cmd3: WebGLCmdDraw = cmdPackage.drawCmds.array[cmdId];
|
||||
if (gpuInputAssembler && gpuShader) {
|
||||
if (!cmd3.isIndirect) {
|
||||
const drawInfo = cmd3.drawInfo as IGFXDrawInfo;
|
||||
@@ -1913,7 +1915,7 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web
|
||||
break;
|
||||
}
|
||||
case WebGLCmd.UPDATE_BUFFER: {
|
||||
const cmd4 = cmdPackage.updateBufferCmds[cmdId];
|
||||
const cmd4 = cmdPackage.updateBufferCmds.array[cmdId];
|
||||
WebGLCmdFuncUpdateBuffer(
|
||||
device,
|
||||
cmd4.gpuBuffer as WebGLGPUBuffer,
|
||||
@@ -1924,7 +1926,7 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web
|
||||
break;
|
||||
}
|
||||
case WebGLCmd.COPY_BUFFER_TO_TEXTURE: {
|
||||
const cmd5 = cmdPackage.copyBufferToTextureCmds[cmdId];
|
||||
const cmd5 = cmdPackage.copyBufferToTextureCmds.array[cmdId];
|
||||
WebGLCmdFuncCopyBufferToTexture(
|
||||
device,
|
||||
(cmd5.gpuBuffer as WebGLGPUBuffer).buffer as GFXBufferSource,
|
||||
|
||||
@@ -51,7 +51,7 @@ export class ForwardStage extends RenderStage {
|
||||
cmdBuff.beginRenderPass(this._framebuffer as GFXFramebuffer, this._renderArea,
|
||||
[camera.clearColor], camera.clearDepth, camera.clearStencil);
|
||||
|
||||
cmdBuff.execute(queue.cmdBuffs, queue.cmdBuffCount);
|
||||
cmdBuff.execute(queue.cmdBuffs.array, queue.cmdBuffCount);
|
||||
|
||||
cmdBuff.endRenderPass();
|
||||
cmdBuff.end();
|
||||
|
||||
@@ -22,7 +22,7 @@ export class RenderItem {
|
||||
export class RenderQueue {
|
||||
public opaques: CachedArray<RenderItem>;
|
||||
public transparents: CachedArray<RenderItem>;
|
||||
public cmdBuffs: GFXCommandBuffer[] = [];
|
||||
public cmdBuffs: CachedArray<GFXCommandBuffer>;
|
||||
public cmdBuffCount: number = 0;
|
||||
private _buffer: ArrayBuffer;
|
||||
private _f32View: Float32Array;
|
||||
@@ -35,6 +35,7 @@ export class RenderQueue {
|
||||
|
||||
this.opaques = new CachedArray(64, compareFn);
|
||||
this.transparents = new CachedArray(64, compareFn);
|
||||
this.cmdBuffs = new CachedArray(64);
|
||||
|
||||
this._buffer = new ArrayBuffer(Float32Array.BYTES_PER_ELEMENT);
|
||||
this._f32View = new Float32Array(this._buffer);
|
||||
@@ -89,18 +90,16 @@ export class RenderQueue {
|
||||
const opaqueCount = this.opaques.length;
|
||||
const cmdBuffCount = opaqueCount + this.transparents.length;
|
||||
|
||||
if (cmdBuffCount > this.cmdBuffs.length) {
|
||||
this.cmdBuffs = new Array<GFXCommandBuffer>(cmdBuffCount);
|
||||
}
|
||||
this.cmdBuffs.reserve(cmdBuffCount);
|
||||
|
||||
this.cmdBuffCount = cmdBuffCount;
|
||||
|
||||
for (let i = 0; i < this.opaques.length; ++i) {
|
||||
this.cmdBuffs[i] = (this.opaques.data[i] as RenderItem).cmdBuff;
|
||||
this.cmdBuffs.array[i] = this.opaques.array[i].cmdBuff;
|
||||
}
|
||||
|
||||
for (let i = 0; i < this.transparents.length; ++i) {
|
||||
this.cmdBuffs[opaqueCount + i] = (this.transparents.data[i] as RenderItem).cmdBuff;
|
||||
this.cmdBuffs.array[opaqueCount + i] = this.transparents.array[i].cmdBuff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user