mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-19 14:52:40 +08:00
perf: optimize instanced buffer batching (#269)
* perf: optimize instanced buffer batching * perf: reduce instanced buffer optimization code size * perf: reduce instanced buffer optimization code size * perf: reduce instanced buffer optimization code size
This commit is contained in:
@@ -28,8 +28,9 @@ import { UNIFORM_LIGHTMAP_TEXTURE_BINDING, UNIFORM_REFLECTION_PROBE_BLEND_CUBEMA
|
||||
UNIFORM_REFLECTION_PROBE_TEXTURE_BINDING, ENABLE_PROBE_BLEND,
|
||||
IRenderPass,
|
||||
getPassPool } from './define';
|
||||
import { BufferUsageBit, MemoryUsageBit, Device, Texture, InputAssembler, InputAssemblerInfo,
|
||||
import { BufferUsageBit, MemoryUsageBit, Device, InputAssembler, InputAssemblerInfo,
|
||||
Attribute, Buffer, BufferInfo, CommandBuffer, Shader, DescriptorSet } from '../gfx';
|
||||
import type { Texture } from '../gfx';
|
||||
import { RecyclePool } from '../core/memop';
|
||||
|
||||
export function instancingCompareFn (l: InstancedBuffer, r: InstancedBuffer): number {
|
||||
@@ -65,6 +66,7 @@ export class InstancedBuffer {
|
||||
public sortRender: IRenderPass;
|
||||
private declare _passPool: RecyclePool<IRenderPass>;
|
||||
private declare _device: Device;
|
||||
private readonly _instanceMap = new Map<string, IInstancedItem[]>();
|
||||
constructor (pass: Pass) {
|
||||
this._device = pass.device;
|
||||
this.pass = pass;
|
||||
@@ -81,6 +83,7 @@ export class InstancedBuffer {
|
||||
});
|
||||
this._passPool.reset();
|
||||
this.instances.length = 0;
|
||||
this._instanceMap.clear();
|
||||
}
|
||||
|
||||
public merge (subModel: SubModel, passIdx: number, shaderImplant: Shader | null = null): void {
|
||||
@@ -106,49 +109,55 @@ export class InstancedBuffer {
|
||||
this.sortRender.hash = hash;
|
||||
this.sortRender.shaderId = shader.typedID;
|
||||
this.sortRender.passIdx = passIdx;
|
||||
for (let i = 0; i < this.instances.length; ++i) {
|
||||
const instance = this.instances[i];
|
||||
if (instance.ia.indexBuffer?.objectID !== sourceIA.indexBuffer?.objectID || instance.count >= MAX_CAPACITY) { continue; }
|
||||
|
||||
// check same binding
|
||||
if (instance.lightingMap.objectID !== lightingMap.objectID) {
|
||||
continue;
|
||||
const key = `${sourceIA.indexBuffer?.objectID ?? 0}/${lightingMap.objectID}/${useReflectionProbeType}/`
|
||||
+ `${reflectionProbeCubemap.objectID}/${reflectionProbePlanarMap.objectID}/`
|
||||
+ `${ENABLE_PROBE_BLEND ? reflectionProbeBlendCubemap!.objectID : 0}/${stride}`;
|
||||
const mappedInstances = this._instanceMap.get(key);
|
||||
if (mappedInstances) {
|
||||
for (let i = 0; i < mappedInstances.length; ++i) {
|
||||
const instance = mappedInstances[i];
|
||||
if (instance.count >= MAX_CAPACITY) { continue; }
|
||||
this._appendInstance(instance, attrs.buffer, shader, descriptorSet);
|
||||
return;
|
||||
}
|
||||
|
||||
if (instance.useReflectionProbeType !== useReflectionProbeType) {
|
||||
continue;
|
||||
}
|
||||
if (instance.reflectionProbeCubemap.objectID !== reflectionProbeCubemap.objectID) {
|
||||
continue;
|
||||
}
|
||||
if (instance.reflectionProbePlanarMap.objectID !== reflectionProbePlanarMap.objectID) {
|
||||
continue;
|
||||
}
|
||||
if (ENABLE_PROBE_BLEND && instance.reflectionProbeBlendCubemap!.objectID !== reflectionProbeBlendCubemap!.objectID) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (instance.stride !== stride) {
|
||||
// we allow this considering both baked and non-baked
|
||||
// skinning models may be present in the same buffer
|
||||
continue;
|
||||
}
|
||||
if (instance.count >= instance.capacity) { // resize buffers
|
||||
instance.capacity <<= 1;
|
||||
const newSize = instance.stride * instance.capacity;
|
||||
const oldData = instance.data;
|
||||
instance.data = new Uint8Array(newSize);
|
||||
instance.data.set(oldData);
|
||||
instance.vb.resize(newSize);
|
||||
}
|
||||
instance.shader = shader;
|
||||
instance.descriptorSet = descriptorSet;
|
||||
instance.data.set(attrs.buffer, instance.stride * instance.count++);
|
||||
this.hasPendingModels = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a new instance
|
||||
this._createInstance(
|
||||
key,
|
||||
sourceIA,
|
||||
attrs.attributes,
|
||||
attrs.buffer,
|
||||
stride,
|
||||
shader,
|
||||
descriptorSet,
|
||||
);
|
||||
}
|
||||
|
||||
private _appendInstance (instance: IInstancedItem, data: Uint8Array, shader: Shader, descriptorSet: DescriptorSet): void {
|
||||
if (instance.count >= instance.capacity) { // resize buffers
|
||||
instance.capacity = Math.min(instance.capacity << 1, MAX_CAPACITY);
|
||||
const newSize = instance.stride * instance.capacity;
|
||||
const oldData = instance.data;
|
||||
instance.data = new Uint8Array(newSize);
|
||||
instance.data.set(oldData);
|
||||
instance.vb.resize(newSize);
|
||||
}
|
||||
instance.shader = shader;
|
||||
instance.descriptorSet = descriptorSet;
|
||||
instance.data.set(data, instance.stride * instance.count++);
|
||||
this.hasPendingModels = true;
|
||||
}
|
||||
|
||||
private _createInstance (
|
||||
key: string,
|
||||
sourceIA: InputAssembler,
|
||||
instancedAttributes: Attribute[],
|
||||
instanceData: Uint8Array,
|
||||
stride: number,
|
||||
shader: Shader,
|
||||
descriptorSet: DescriptorSet,
|
||||
): void {
|
||||
const vb = this._device.createBuffer(new BufferInfo(
|
||||
BufferUsageBit.VERTEX | BufferUsageBit.TRANSFER_DST,
|
||||
MemoryUsageBit.HOST | MemoryUsageBit.DEVICE,
|
||||
@@ -160,18 +169,33 @@ export class InstancedBuffer {
|
||||
const attributes = sourceIA.attributes.slice();
|
||||
const indexBuffer = sourceIA.indexBuffer;
|
||||
|
||||
for (let i = 0; i < attrs.attributes.length; i++) {
|
||||
const attr = attrs.attributes[i];
|
||||
for (let i = 0; i < instancedAttributes.length; i++) {
|
||||
const attr = instancedAttributes[i];
|
||||
const newAttr = new Attribute(attr.name, attr.format, attr.isNormalized, vertexBuffers.length, true);
|
||||
attributes.push(newAttr);
|
||||
}
|
||||
data.set(attrs.buffer);
|
||||
data.set(instanceData);
|
||||
|
||||
vertexBuffers.push(vb);
|
||||
const iaInfo = new InputAssemblerInfo(attributes, vertexBuffers, indexBuffer);
|
||||
const ia = this._device.createInputAssembler(iaInfo);
|
||||
// eslint-disable-next-line max-len
|
||||
this.instances.push({ count: 1, capacity: INITIAL_CAPACITY, vb, data, ia, stride, shader, descriptorSet, lightingMap, reflectionProbeCubemap, reflectionProbePlanarMap, useReflectionProbeType, reflectionProbeBlendCubemap });
|
||||
const instance = {
|
||||
count: 1,
|
||||
capacity: INITIAL_CAPACITY,
|
||||
vb,
|
||||
data,
|
||||
ia,
|
||||
stride,
|
||||
shader,
|
||||
descriptorSet,
|
||||
} as IInstancedItem;
|
||||
this.instances.push(instance);
|
||||
let mappedInstances = this._instanceMap.get(key);
|
||||
if (!mappedInstances) {
|
||||
mappedInstances = [];
|
||||
this._instanceMap.set(key, mappedInstances);
|
||||
}
|
||||
mappedInstances.push(instance);
|
||||
this.hasPendingModels = true;
|
||||
}
|
||||
|
||||
@@ -180,7 +204,7 @@ export class InstancedBuffer {
|
||||
const instance = this.instances[i];
|
||||
if (!instance.count) { continue; }
|
||||
instance.ia.instanceCount = instance.count;
|
||||
cmdBuff.updateBuffer(instance.vb, instance.data);
|
||||
cmdBuff.updateBuffer(instance.vb, instance.data.buffer as ArrayBuffer, instance.count * instance.stride);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user