diff --git a/cocos/rendering/custom/web-pipeline-types.ts b/cocos/rendering/custom/web-pipeline-types.ts index 026b6bd8a14..1ad44b1bbb2 100644 --- a/cocos/rendering/custom/web-pipeline-types.ts +++ b/cocos/rendering/custom/web-pipeline-types.ts @@ -901,37 +901,48 @@ export class RenderDrawQueue { } export class RenderInstancingQueue { - /** - * @en A set of instanced buffer - * @zh Instance 合批缓存集合。 - */ - public queue = new Set(); + passInstances: Map = new Map(); + instanceBuffers: Array = new Array(); + empty (): boolean { - return this.queue.size === 0; + return this.passInstances.size === 0; } add (pass: Pass, subModel: SubModel, passID: number): void { - const instancedBuffer = pass.getInstancedBuffer(); + const iter = this.passInstances.get(pass); + if (iter === undefined) { + const instanceBufferID = this.passInstances.size; + if (instanceBufferID >= this.instanceBuffers.length) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + this.instanceBuffers.push(new InstancedBuffer(pass)); + } + this.passInstances.set(pass, instanceBufferID); + + const instanceBuffer = this.instanceBuffers[instanceBufferID]; + instanceBuffer.pass = pass; + const instances = instanceBuffer.instances; + } + + const instancedBuffer = this.instanceBuffers[this.passInstances.get(pass)!]; instancedBuffer.merge(subModel, passID); - this.queue.add(instancedBuffer); } clear (): void { - const it = this.queue.values(); let res = it.next(); - while (!res.done) { - res.value.clear(); - res = it.next(); - } - this.queue.clear(); + this.passInstances.clear(); + const instanceBuffers = this.instanceBuffers; + instanceBuffers.forEach((instance) => { + instance.clear(); + }); } sort (): void {} uploadBuffers (cmdBuffer: CommandBuffer): void { - const it = this.queue.values(); let res = it.next(); - while (!res.done) { - if (res.value.hasPendingModels) res.value.uploadBuffers(cmdBuffer); - res = it.next(); + for (const [pass, bufferID] of this.passInstances.entries()) { + const instanceBuffer = this.instanceBuffers[bufferID]; + if (instanceBuffer.hasPendingModels) { + instanceBuffer.uploadBuffers(cmdBuffer); + } } } @@ -942,7 +953,7 @@ export class RenderInstancingQueue { offset = 0, dynamicOffsets: number[] | null = null, ): void { - const renderQueue = this.queue; + const renderQueue = this.instanceBuffers; for (const instanceBuffer of renderQueue) { if (!instanceBuffer.hasPendingModels) { continue;