From 956ea89164d3f4d3cdb93e336968d631096e006c Mon Sep 17 00:00:00 2001 From: jerrywwl Date: Mon, 14 Jan 2019 20:42:16 +0800 Subject: [PATCH] add cached array, and optimize render queue with it --- cocos/3d/misc/utils.js | 69 ------------------------- cocos/core/memop/cached-array.ts | 47 +++++++++++++++++ cocos/gfx/command-buffer.ts | 2 +- cocos/gfx/webgl/webgl-command-buffer.ts | 15 +++--- cocos/pipeline/forward/forward-stage.ts | 8 +-- cocos/pipeline/render-queue.ts | 63 +++++++++++----------- 6 files changed, 88 insertions(+), 116 deletions(-) delete mode 100644 cocos/3d/misc/utils.js create mode 100644 cocos/core/memop/cached-array.ts diff --git a/cocos/3d/misc/utils.js b/cocos/3d/misc/utils.js deleted file mode 100644 index a6256437a4..0000000000 --- a/cocos/3d/misc/utils.js +++ /dev/null @@ -1,69 +0,0 @@ -import { renderer } from '../../renderer'; -import find from '../../scene-graph/find'; -import { Mesh, RenderingMesh } from '../assets/mesh'; -import gfx from '../../renderer/gfx/index'; -import { GFXPrimitiveMode } from '../../gfx/define'; - -/** - * - * @param {import("../assets/skeleton").default} skinning - */ -function createJointsTexture(skinning) { - const jointCount = skinning.joints.length; - - // Set jointsTexture. - // A squared texture with side length N(N > 1) multiples of 2 can store - // 2 ^ (2 * N - 2) matrices. - // We support most 1024 joints. - let size; - if (jointCount > 1024) { - throw "To many joints(more than 1024)."; - } else if (jointCount > 256) { - size = 64; - } else if (jointCount > 64) { - size = 32; - } else if (jointCount > 16) { - size = 16; - } else if (jointCount > 4) { - size = 8; - } else { - size = 4; - } - - return new gfx.Texture2D(cc.game._gfxDevice, { - width: size, - height: size, - format: gfx.TEXTURE_FMT_RGBA32F, - minFilter: gfx.FILTER_NEAREST, - magFilter: gfx.FILTER_NEAREST, - wrapS: gfx.WRAP_CLAMP, - wrapT: gfx.WRAP_CLAMP, - mipmap: false - }); -} - -function createMesh(context, data) { - let ia = renderer.createIA(context, data); - let meshAsset = new Mesh(); - const primitiveMode = data.primitiveMode === undefined ? GFXPrimitiveMode.TRIANGLE_LIST : data.primitiveMode; - meshAsset._renderingMesh = new RenderingMesh([{inputAssembler: ia, primitiveMode}], [], []); - meshAsset._minPosition = data.minPos; - meshAsset._maxPosition = data.maxPos; - - return meshAsset; -} - -/** - * @param {Uint8Array} buffer - */ -function toPPM(buffer, w, h) { - return `P3 ${w} ${h} 255\n${buffer.filter((e, i) => i % 4 < 3).toString()}\n`; -} - -export default { - createJointsTexture, - createMesh, - - find, - toPPM -}; diff --git a/cocos/core/memop/cached-array.ts b/cocos/core/memop/cached-array.ts new file mode 100644 index 0000000000..f63b7e3db5 --- /dev/null +++ b/cocos/core/memop/cached-array.ts @@ -0,0 +1,47 @@ +export class CachedArray { + + public data: Array; + public length: number = 0; + private _compareFn; + + constructor (count: number, compareFn?: (a: T, b: T) => number) { + this.data = new Array(count); + this.length = 0; + + if (compareFn !== undefined) { + this._compareFn = compareFn; + } else { + this._compareFn = (a: number, b: number) => a - b; + } + } + + 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]; + } + } + + this.data[this.length++] = item; + } + + public pop (): T | undefined { + return this.data[this.length--]; + } + + public get (idx: number): T | undefined { + return this.data[idx]; + } + + public clear () { + this.length = 0; + } + + public sort () { + this.data.fill(undefined, this.length, this.data.length); + this.data.sort(this._compareFn); + } +} diff --git a/cocos/gfx/command-buffer.ts b/cocos/gfx/command-buffer.ts index 28388d51fe..fd14d277d9 100644 --- a/cocos/gfx/command-buffer.ts +++ b/cocos/gfx/command-buffer.ts @@ -50,5 +50,5 @@ export abstract class GFXCommandBuffer extends GFXObject { public abstract draw (inputAssembler: GFXInputAssembler); public abstract updateBuffer (buffer: GFXBuffer, data: ArrayBuffer, offset?: number); public abstract copyBufferToTexture (srcBuff: GFXBuffer, dstTex: GFXTexture, dstLayout: GFXTextureLayout, regions: GFXBufferTextureCopy[]); - public abstract execute (cmdBuffs: GFXCommandBuffer[]); + public abstract execute (cmdBuffs: GFXCommandBuffer[], count: number); } diff --git a/cocos/gfx/webgl/webgl-command-buffer.ts b/cocos/gfx/webgl/webgl-command-buffer.ts index 1f64f68349..f15abbb38d 100644 --- a/cocos/gfx/webgl/webgl-command-buffer.ts +++ b/cocos/gfx/webgl/webgl-command-buffer.ts @@ -124,7 +124,6 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer { } public draw (inputAssembler: GFXInputAssembler) { - if (this._type === GFXCommandBufferType.PRIMARY && this._isInRenderPass || this._type === GFXCommandBufferType.SECONDARY) { if (this._isStateInvalied) { @@ -145,7 +144,8 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer { } public updateBuffer (buffer: GFXBuffer, data: GFXBufferSource, offset?: number, size?: number) { - if (!this._isInRenderPass) { + if (this._type === GFXCommandBufferType.PRIMARY && !this._isInRenderPass || + this._type === GFXCommandBufferType.SECONDARY) { const gpuBuffer = (buffer as WebGLGFXBuffer).gpuBuffer; if (gpuBuffer) { const cmd = (this._allocator as WebGLGFXCommandAllocator). @@ -183,7 +183,8 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer { dstLayout: GFXTextureLayout, regions: GFXBufferTextureCopy[]) { - if (!this._isInRenderPass) { + if (this._type === GFXCommandBufferType.PRIMARY && !this._isInRenderPass || + this._type === GFXCommandBufferType.SECONDARY) { const gpuBuffer = ( srcBuff as WebGLGFXBuffer).gpuBuffer; const gpuTexture = ( dstTex as WebGLGFXTexture).gpuTexture; if (gpuBuffer && gpuTexture) { @@ -205,10 +206,10 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer { } // tslint:disable: max-line-length - public execute (cmdBuffs: GFXCommandBuffer[]) { + public execute (cmdBuffs: GFXCommandBuffer[], count: number) { - for (const cmdBuff of cmdBuffs) { - const webGLCmdBuff = cmdBuff as WebGLGFXCommandBuffer; + 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); @@ -220,7 +221,7 @@ export class WebGLGFXCommandBuffer extends GFXCommandBuffer { } public get webGLDevice (): WebGLGFXDevice { - return this._device as WebGLGFXDevice; + return this._device as WebGLGFXDevice; } private bindStates () { diff --git a/cocos/pipeline/forward/forward-stage.ts b/cocos/pipeline/forward/forward-stage.ts index c0589212cd..f7b3644166 100644 --- a/cocos/pipeline/forward/forward-stage.ts +++ b/cocos/pipeline/forward/forward-stage.ts @@ -51,13 +51,7 @@ export class ForwardStage extends RenderStage { cmdBuff.beginRenderPass(this._framebuffer as GFXFramebuffer, this._renderArea, [camera.clearColor], camera.clearDepth, camera.clearStencil); - for (const item of queue.opaques) { - cmdBuff.execute([item.cmdBuff]); - } - - for (const item of queue.transparents) { - cmdBuff.execute([item.cmdBuff]); - } + cmdBuff.execute(queue.cmdBuffs, queue.cmdBuffCount); cmdBuff.endRenderPass(); cmdBuff.end(); diff --git a/cocos/pipeline/render-queue.ts b/cocos/pipeline/render-queue.ts index 458f3b5ba8..a194e70cb1 100644 --- a/cocos/pipeline/render-queue.ts +++ b/cocos/pipeline/render-queue.ts @@ -1,12 +1,10 @@ +import { CachedArray } from '../core/memop/cached-array'; import { vec3 } from '../core/vmath'; import { GFXCommandBuffer } from '../gfx/command-buffer'; import { Pass } from '../renderer/core/pass'; import { Camera } from '../renderer/scene/camera'; import { Model } from '../renderer/scene/model'; -const _pos: vec3 = vec3.create(); -const _cameraPos: vec3 = vec3.create(); - export class RenderItem { public hash: number; public model: Model; @@ -22,35 +20,40 @@ export class RenderItem { } export class RenderQueue { - - public opaques: RenderItem[] = []; - // public opaqueCmdBuffs: GFXCommandBuffer[] = []; - public transparents: RenderItem[] = []; - // public transparentCmdBuffs: GFXCommandBuffer[] = []; + public opaques: CachedArray; + public transparents: CachedArray; + public cmdBuffs: GFXCommandBuffer[] = []; + public cmdBuffCount: number = 0; private _buffer: ArrayBuffer; private _f32View: Float32Array; private _ui32View: Uint32Array; constructor () { + const compareFn = (a: RenderItem, b: RenderItem) => { + return a.hash - b.hash; + }; + + this.opaques = new CachedArray(64, compareFn); + this.transparents = new CachedArray(64, compareFn); + this._buffer = new ArrayBuffer(Float32Array.BYTES_PER_ELEMENT); this._f32View = new Float32Array(this._buffer); this._ui32View = new Uint32Array(this._buffer); } public clear () { - this.opaques = []; - this.transparents = []; + this.opaques.clear(); + this.transparents.clear(); + this.cmdBuffCount = 0; } public add (model: Model, camera: Camera) { - model.node.getPosition(_pos); - camera.node.getPosition(_cameraPos); - this._f32View[0] = vec3.distance(_cameraPos, _pos); + this._f32View[0] = vec3.distance(camera.node.getPosition(), model.node.getPosition()); let ui32Depth: number = this._ui32View[0]; const mask = -(ui32Depth >> 31) | 0x8000000000000000; ui32Depth = (ui32Depth ^ mask); - const ui32DepthInv = (ui32Depth ^ 0xffffffffffffffff); + const ui32DepthInv = (ui32Depth ^ 0x00000000ffffffff); for (let i = 0; i < model.passes.length; ++i) { const pass = model.passes[i]; @@ -58,10 +61,6 @@ export class RenderQueue { // update pass pass.update(); - const cmdBuff = model.commandBuffers[i]; - - model.node.getWorldPosition(); - const pso = pass.pipelineState; const isTransparent = pso.blendState.targets[0].blend; @@ -72,36 +71,36 @@ export class RenderQueue { // Opaque objects are sorted by depth front to back -> pass priority -> shader id. const hash = (0 << 61) | (i << 45) | (ui32Depth << 13) | pso.shader.id; - this.opaques.push({hash, model, pass, cmdBuff}); + this.opaques.push({hash, model, pass, cmdBuff: model.commandBuffers[i]}); } else { // Transparent objects are sorted by depth back to front -> pass priority -> shader id. const hash = (1 << 61) | (i << 45) | (ui32DepthInv << 13) | pso.shader.id; - this.transparents.push({hash, model, pass, cmdBuff}); + this.transparents.push({hash, model, pass, cmdBuff: model.commandBuffers[i]}); } } } public sort () { - this.opaques.sort((a: RenderItem, b: RenderItem) => { - return a.hash - b.hash; - }); + this.opaques.sort(); + this.transparents.sort(); - this.transparents.sort((a: RenderItem, b: RenderItem) => { - return a.hash - b.hash; - }); + const opaqueCount = this.opaques.length; + const cmdBuffCount = opaqueCount + this.transparents.length; + + if (cmdBuffCount > this.cmdBuffs.length) { + this.cmdBuffs = new Array(cmdBuffCount); + } + + this.cmdBuffCount = cmdBuffCount; - /* - this.opaqueCmdBuffs = new Array(this.opaques.length); for (let i = 0; i < this.opaques.length; ++i) { - this.opaqueCmdBuffs[i] = this.opaques[i].cmdBuff; + this.cmdBuffs[i] = (this.opaques.data[i] as RenderItem).cmdBuff; } - this.transparentCmdBuffs = new Array(this.transparents.length); for (let i = 0; i < this.transparents.length; ++i) { - this.transparentCmdBuffs[i] = this.transparents[i].cmdBuff; + this.cmdBuffs[opaqueCount + i] = (this.transparents.data[i] as RenderItem).cmdBuff; } - */ } }