From 1b9011704d6d027ddf7ed6cb0335441309c1f696 Mon Sep 17 00:00:00 2001 From: GengineJS <476393671@qq.com> Date: Wed, 16 Oct 2024 09:34:27 +0800 Subject: [PATCH] Optimized execution code --- cocos/rendering/custom/executor.ts | 52 ++++++++++---------- cocos/rendering/custom/web-pipeline-types.ts | 49 +++++++----------- 2 files changed, 46 insertions(+), 55 deletions(-) diff --git a/cocos/rendering/custom/executor.ts b/cocos/rendering/custom/executor.ts index 3e643036033..d4685fc66b0 100644 --- a/cocos/rendering/custom/executor.ts +++ b/cocos/rendering/custom/executor.ts @@ -768,13 +768,7 @@ class DeviceRenderPass implements RecordingInterface { let depthTex: Texture | null = null; let swapchain: Swapchain | null = null; let framebuffer: Framebuffer | null = null; - for (const cv of rasterPass.computeViews) { - this._applyRenderLayout(cv); - } - // update the layout descriptorSet - if (this.renderLayout && this.renderLayout.descriptorSet) { - this.renderLayout.descriptorSet.update(); - } + this._processRenderLayout(rasterPass); for (const [resName, rasterV] of rasterPass.rasterViews) { let resTex = context.deviceTextures.get(resName); if (!resTex) { @@ -852,11 +846,11 @@ class DeviceRenderPass implements RecordingInterface { renderPassInfo.depthStencilAttachment = depthStencilAttachment; } this._renderPass = device.createRenderPass(renderPassInfo); - this._framebuffer = framebuffer || device.createFramebuffer(new FramebufferInfo( - this._renderPass, + this._createFramebuffer( + framebuffer, swapchain ? [swapchain.colorTexture] : colorTexs, swapchain ? swapchain.depthStencilTexture : depthTex, - )); + ); } get indexOfRD (): number { return this._idxOfRenderData; } get rasterID (): number { return this._rasterID; } @@ -994,6 +988,27 @@ class DeviceRenderPass implements RecordingInterface { postRecord (): void { // nothing to do } + + private _processRenderLayout (pass: RasterPass): void { + for (const cv of pass.computeViews) { + this._applyRenderLayout(cv); + } + // update the layout descriptorSet + if (this.renderLayout && this.renderLayout.descriptorSet) { + this.renderLayout.descriptorSet.update(); + } + } + + private _createFramebuffer (fbo: Framebuffer | null, cols: Texture[], depthTex: Texture | null): void { + if (!fbo && !cols.length) return; + if (this._framebuffer && fbo !== this._framebuffer) this._framebuffer.destroy(); + this._framebuffer = fbo || context.device.createFramebuffer(new FramebufferInfo( + this._renderPass, + cols, + depthTex, + )); + } + resetResource (id: number, pass: RasterPass): void { this._rasterID = id; this._rasterPass = pass; @@ -1006,13 +1021,7 @@ class DeviceRenderPass implements RecordingInterface { const currFramebuffer = this._framebuffer; const currFBDepthTex = currFramebuffer.depthStencilTexture; let depTexture = currFramebuffer ? currFBDepthTex : null; - for (const cv of pass.computeViews) { - this._applyRenderLayout(cv); - } - // update the layout descriptorSet - if (this.renderLayout && this.renderLayout.descriptorSet) { - this.renderLayout.descriptorSet.update(); - } + this._processRenderLayout(pass); const resGraph = context.resourceGraph; const currentWidth = currFramebuffer ? currFramebuffer.width : 0; @@ -1072,14 +1081,7 @@ class DeviceRenderPass implements RecordingInterface { } } } - if (!framebuffer && colTextures.length) { - this._framebuffer.destroy(); - this._framebuffer = context.device.createFramebuffer(new FramebufferInfo( - this._renderPass, - colTextures, - depTexture, - )); - } + this._createFramebuffer(framebuffer, colTextures, depTexture); } } diff --git a/cocos/rendering/custom/web-pipeline-types.ts b/cocos/rendering/custom/web-pipeline-types.ts index b3abc00d876..d338a0981fa 100644 --- a/cocos/rendering/custom/web-pipeline-types.ts +++ b/cocos/rendering/custom/web-pipeline-types.ts @@ -900,48 +900,37 @@ export class RenderDrawQueue { } export class RenderInstancingQueue { - passInstances: Map = new Map(); - instanceBuffers: Array = new Array(); - + /** + * @en A set of instanced buffer + * @zh Instance 合批缓存集合。 + */ + public queue = new Set(); empty (): boolean { - return this.passInstances.size === 0; + return this.queue.size === 0; } add (pass: Pass, subModel: SubModel, passID: number): void { - 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)!]; + const instancedBuffer = pass.getInstancedBuffer(); instancedBuffer.merge(subModel, passID); + this.queue.add(instancedBuffer); } clear (): void { - this.passInstances.clear(); - const instanceBuffers = this.instanceBuffers; - instanceBuffers.forEach((instance) => { - instance.clear(); - }); + const it = this.queue.values(); let res = it.next(); + while (!res.done) { + res.value.clear(); + res = it.next(); + } + this.queue.clear(); } sort (): void {} uploadBuffers (cmdBuffer: CommandBuffer): void { - for (const [pass, bufferID] of this.passInstances.entries()) { - const instanceBuffer = this.instanceBuffers[bufferID]; - if (instanceBuffer.hasPendingModels) { - instanceBuffer.uploadBuffers(cmdBuffer); - } + const it = this.queue.values(); let res = it.next(); + while (!res.done) { + if (res.value.hasPendingModels) res.value.uploadBuffers(cmdBuffer); + res = it.next(); } } @@ -952,7 +941,7 @@ export class RenderInstancingQueue { offset = 0, dynamicOffsets: number[] | null = null, ): void { - const renderQueue = this.instanceBuffers; + const renderQueue = this.queue; for (const instanceBuffer of renderQueue) { if (!instanceBuffer.hasPendingModels) { continue;