mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
add cached array, and optimize render queue with it
This commit is contained in:
@@ -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
|
||||
};
|
||||
@@ -0,0 +1,47 @@
|
||||
export class CachedArray<T> {
|
||||
|
||||
public data: Array<T|undefined>;
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 () {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<RenderItem>;
|
||||
public transparents: CachedArray<RenderItem>;
|
||||
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<GFXCommandBuffer>(cmdBuffCount);
|
||||
}
|
||||
|
||||
this.cmdBuffCount = cmdBuffCount;
|
||||
|
||||
/*
|
||||
this.opaqueCmdBuffs = new Array<GFXCommandBuffer>(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<GFXCommandBuffer>(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;
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user