Add the logic for merging render passes (#18972)

This commit is contained in:
GengineJS
2025-09-02 18:53:05 +08:00
committed by GitHub
parent 1ba858437f
commit 2c05e20f55
3 changed files with 125 additions and 44 deletions
+99 -31
View File
@@ -43,13 +43,14 @@ import {
AttachmentType, LightInfo,
QueueHint, ResourceResidency, SceneFlags, UpdateFrequency,
} from './types';
import { Vec4, geometry, toRadian, cclegacy } from '../../core';
import { Vec4, geometry, toRadian, cclegacy, RecyclePool } from '../../core';
import { RenderWindow } from '../../render-scene/core/render-window';
import { RasterPass, RenderData, RenderGraph } from './render-graph';
import { WebPipeline } from './web-pipeline';
import { DescriptorSetData, LayoutGraphData } from './layout-graph';
import { AABB } from '../../core/geometry';
import { getUBOTypeCount } from './utils';
import { init } from '.';
const _rangedDirLightBoundingBox = new AABB(0.0, 0.0, 0.0, 0.5, 0.5, 0.5);
const _tmpBoundingBox = new AABB();
@@ -860,37 +861,104 @@ export function getSubpassOrPassID (sceneId: number, rg: RenderGraph, lg: Layout
return layoutId;
}
export function genHashValue (pass: RasterPass): void {
let hashCode = '';
for (const [name, raster] of pass.rasterViews) {
hashCode += hashCombineKey(name);
hashCode += hashCombineKey(raster.slotName);
hashCode += hashCombineKey(raster.accessType);
hashCode += hashCombineKey(raster.attachmentType);
hashCode += hashCombineKey(raster.loadOp);
hashCode += hashCombineKey(raster.storeOp);
hashCode += hashCombineKey(raster.clearFlags);
hashCode += hashCombineKey(raster.clearColor.x);
hashCode += hashCombineKey(raster.clearColor.y);
hashCode += hashCombineKey(raster.clearColor.z);
hashCode += hashCombineKey(raster.clearColor.w);
hashCode += hashCombineKey(raster.slotID);
hashCode += hashCombineKey(raster.shaderStageFlags);
export class RenderPassMergeInfo {
constructor (
public combineHash: number = 0,
public needBeginRP: boolean = true,
public needEndRP: boolean = true,
) {}
init (
combineHash: number,
needBeginRP: boolean = true,
needEndRP: boolean = true,
): void {
this.combineHash = combineHash;
this.needBeginRP = needBeginRP;
this.needEndRP = needEndRP;
}
for (const [name, computes] of pass.computeViews) {
hashCode += hashCombineKey(name);
for (const compute of computes) {
hashCode += hashCombineKey(compute.name);
hashCode += hashCombineKey(compute.accessType);
hashCode += hashCombineKey(compute.clearFlags);
hashCode += hashCombineKey(compute.clearValueType);
hashCode += hashCombineKey(compute.clearValue.x);
hashCode += hashCombineKey(compute.clearValue.y);
hashCode += hashCombineKey(compute.clearValue.z);
hashCode += hashCombineKey(compute.clearValue.w);
hashCode += hashCombineKey(compute.shaderStageFlags);
}
const passOrders: RasterPass[] = [];
const rpCombineMap: Map<RasterPass, number> = new Map();
export const rpMergeInfos: Map<RasterPass, RenderPassMergeInfo> = new Map();
const rpMergeInfoPool = new RecyclePool<RenderPassMergeInfo>((): RenderPassMergeInfo => new RenderPassMergeInfo(), 16);
export function resetPassMGState (): void {
rpMergeInfoPool.reset();
rpMergeInfos.clear();
rpCombineMap.clear();
passOrders.length = 0;
}
export function processPassMG (pass: RasterPass): void {
const currCHash = rpCombineMap.get(pass)!;
const currRPInfo = rpMergeInfoPool.add();
if (!rpMergeInfos.has(pass)) {
rpMergeInfos.set(pass, currRPInfo);
}
currRPInfo.init(currCHash);
const poLen = passOrders.length;
if (poLen !== 0) {
let isLoadOP = false;
for (const [_, raster] of pass.rasterViews) {
if (raster.loadOp === LoadOp.LOAD) {
isLoadOP = true;
break;
}
}
const prevPass = passOrders[poLen - 1];
if (isLoadOP && rpCombineMap.get(prevPass) === currCHash) {
const prevInfo = rpMergeInfos.get(prevPass)!;
prevInfo.needEndRP = false;
currRPInfo.needBeginRP = false;
}
}
hashCode += hashCombineKey(pass.showStatistics ? 1 : 0);
pass.hashValue = hashCombineStr(hashCode);
passOrders.push(pass);
}
export function genHashValue (pass: RasterPass): void {
const hashCodeParts: string[] = [];
const combineHashParts: string[] = [];
for (const [name, raster] of pass.rasterViews) {
const commonParts = [
hashCombineKey(name),
hashCombineKey(raster.slotName),
hashCombineKey(raster.accessType),
hashCombineKey(raster.attachmentType),
hashCombineKey(raster.storeOp),
hashCombineKey(raster.clearFlags),
hashCombineKey(raster.slotID),
hashCombineKey(raster.shaderStageFlags),
];
const extraParts = [
hashCombineKey(raster.loadOp),
hashCombineKey(raster.clearColor.x),
hashCombineKey(raster.clearColor.y),
hashCombineKey(raster.clearColor.z),
hashCombineKey(raster.clearColor.w),
];
const fullHash = commonParts.concat(extraParts).join('');
const combineHash = commonParts.join('');
hashCodeParts.push(fullHash);
combineHashParts.push(combineHash);
}
const subpassGraph = pass.subpassGraph;
const subpasses = subpassGraph._subpasses;
for (const subpass of subpasses) {
const resolvePairs = subpass.resolvePairs;
for (const resolve of resolvePairs) {
const commonParts = [
hashCombineKey(resolve.source),
hashCombineKey(resolve.target),
];
const combineHash = commonParts.join('');
combineHashParts.push(combineHash);
}
}
pass.hashValue = hashCombineStr(hashCodeParts.join(''));
rpCombineMap.set(pass, hashCombineStr(combineHashParts.join('')));
processPassMG(pass);
}
+24 -12
View File
@@ -137,6 +137,8 @@ import {
bool,
getDescriptorSetDataFromLayout,
getRenderArea,
RenderPassMergeInfo,
rpMergeInfos,
updateGlobalDescBinding,
} from './define';
import { LightResource, SceneCulling } from './scene-culling';
@@ -833,6 +835,7 @@ class DeviceRenderPass implements RecordingInterface {
const resTex = this._getOrCreateDeviceTex(resName);
if (!swapchain) swapchain = resTex.swapchain;
if (!framebuffer) framebuffer = resTex.framebuffer;
const isLoadAttachment = rasterV.loadOp === LoadOp.LOAD;
if (rasterV.attachmentType === AttachmentType.RENDER_TARGET) {
if (!resTex.swapchain && !resTex.framebuffer) colorTexs.push(resTex.texture!);
const colAtt = new ColorAttachment();
@@ -841,7 +844,7 @@ class DeviceRenderPass implements RecordingInterface {
colAtt.loadOp = rasterV.loadOp;
colAtt.storeOp = rasterV.storeOp;
colAtt.barrier = device.getGeneralBarrier(new GeneralBarrierInfo(
rasterV.loadOp === LoadOp.LOAD ? AccessFlagBit.COLOR_ATTACHMENT_WRITE : AccessFlagBit.NONE,
isLoadAttachment ? AccessFlagBit.COLOR_ATTACHMENT_WRITE : AccessFlagBit.NONE,
rasterV.storeOp === StoreOp.STORE ? AccessFlagBit.COLOR_ATTACHMENT_WRITE : AccessFlagBit.NONE,
));
const currCol = new Color();
@@ -855,7 +858,7 @@ class DeviceRenderPass implements RecordingInterface {
depAtt.sampleCount = resTex.description!.sampleCount;
depAtt.stencilLoadOp = rasterV.loadOp;
depAtt.barrier = device.getGeneralBarrier(new GeneralBarrierInfo(
rasterV.loadOp === LoadOp.LOAD ? AccessFlagBit.DEPTH_STENCIL_ATTACHMENT_WRITE : AccessFlagBit.NONE,
isLoadAttachment ? AccessFlagBit.DEPTH_STENCIL_ATTACHMENT_WRITE : AccessFlagBit.NONE,
rasterV.storeOp === StoreOp.STORE ? AccessFlagBit.DEPTH_STENCIL_ATTACHMENT_WRITE : AccessFlagBit.NONE,
));
if (!resTex.swapchain && !resTex.framebuffer) {
@@ -889,6 +892,7 @@ class DeviceRenderPass implements RecordingInterface {
swapchain ? swapchain.depthStencilTexture : depthTex,
);
}
get passMergeInfo (): RenderPassMergeInfo { return rpMergeInfos.get(this._rasterPass)!; }
get indexOfRD (): number { return this._idxOfRenderData; }
get rasterID (): number { return this._rasterID; }
get layoutName (): string { return this._layoutName; }
@@ -944,11 +948,23 @@ class DeviceRenderPass implements RecordingInterface {
}
}
bindGlobalDesc (): void {
const cmdBuff = context.commandBuffer;
if (context.passDescriptorSet) {
cmdBuff.bindDescriptorSet(
SetIndex.GLOBAL,
context.passDescriptorSet,
);
}
}
beginPass (): void {
if (!this.passMergeInfo.needBeginRP) {
this.bindGlobalDesc();
return;
}
const cmdBuff = context.commandBuffer;
const tex = this.framebuffer.colorTextures[0]!;
this._applyViewport(tex);
const cmdBuff = context.commandBuffer;
if (this._viewport) {
renderPassArea.x = this._viewport.left;
renderPassArea.y = this._viewport.top;
@@ -967,15 +983,11 @@ class DeviceRenderPass implements RecordingInterface {
this.clearDepth,
this.clearStencil,
);
if (context.passDescriptorSet) {
cmdBuff.bindDescriptorSet(
SetIndex.GLOBAL,
context.passDescriptorSet,
);
}
this.bindGlobalDesc();
}
endPass (): void {
if (!this.passMergeInfo.needEndRP) return;
const cmdBuff = context.commandBuffer;
cmdBuff.endRenderPass();
}
@@ -1191,8 +1203,6 @@ class DeviceRenderScene implements RecordingInterface {
this._currentQueue.createBlitDesc(this._blit);
this._currentQueue.blitDesc!.update();
}
context.lightResource.buildLightBuffer(context.commandBuffer);
context.lightResource.tryUpdateRenderSceneLocalDescriptorSet(context.culling);
}
postRecord (): void {
// nothing to do
@@ -1724,6 +1734,8 @@ export class Executor {
context.lightResource.buildLights(culling, context.pipelineSceneData.isHDR, context.pipelineSceneData.shadows);
this._removeDeviceResource();
cmdBuff.begin();
context.lightResource.buildLightBuffer(cmdBuff);
context.lightResource.tryUpdateRenderSceneLocalDescriptorSet(context.culling);
culling.uploadInstancing(cmdBuff);
if (!this._visitor) this._visitor = new RenderVisitor();
depthFirstSearch(this._visitor.graphView, this._visitor, this._visitor.colorMap);
+2 -1
View File
@@ -45,7 +45,7 @@ import { GeometryRenderer } from '../geometry-renderer';
import { Material } from '../../asset/assets';
import { decideProfilerCamera } from '../pipeline-funcs';
import { DebugViewCompositeType } from '../debug-view';
import { buildReflectionProbePass, genHashValue } from './define';
import { buildReflectionProbePass, genHashValue, resetPassMGState } from './define';
import { createGfxDescriptorSetsAndPipelines } from './layout-graph-utils';
import { Root } from '../../root';
import { Scene } from '../../scene-graph';
@@ -1644,6 +1644,7 @@ export class WebPipeline extends WebSetter implements BasicPipeline {
if (!this._renderGraph) {
throw new Error('RenderGraph cannot be built without being created');
}
resetPassMGState();
if (DEBUG) {
if (!this._compiler) {
this._compiler = new Compiler(this, this._renderGraph, this._resourceGraph, this._lg);