Skip to content

Commit

Permalink
Optimize new pipeline execution code (#17722)
Browse files Browse the repository at this point in the history
  • Loading branch information
GengineJS authored Oct 16, 2024
1 parent f833b8a commit 86ce24b
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 185 deletions.
85 changes: 15 additions & 70 deletions cocos/rendering/custom/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
import { DEBUG } from 'internal:constants';
import { Buffer, Framebuffer, LoadOp, StoreOp, Texture, Viewport } from '../../gfx';
import { Buffer, Framebuffer, Texture, Viewport } from '../../gfx';
import { genHashValue } from './define';
import { VectorGraphColorMap } from './effect';
import { DefaultVisitor, depthFirstSearch, ReferenceGraphView } from './graph';
import { LayoutGraphData } from './layout-graph';
Expand All @@ -34,50 +34,7 @@ import {
RenderGraphValue,
} from './render-graph';
import { AccessType, ResourceResidency, SceneFlags } from './types';
import { hashCombineKey, hashCombineStr } from './define';

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);
}
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);
}
}
hashCode += hashCombineKey(pass.width);
hashCode += hashCombineKey(pass.height);
hashCode += hashCombineKey(pass.viewport.left);
hashCode += hashCombineKey(pass.viewport.top);
hashCode += hashCombineKey(pass.viewport.width);
hashCode += hashCombineKey(pass.viewport.height);
hashCode += hashCombineKey(pass.viewport.minDepth);
hashCode += hashCombineKey(pass.viewport.maxDepth);
hashCode += hashCombineKey(pass.showStatistics ? 1 : 0);
pass.hashValue = hashCombineStr(hashCode);
}
const readViews: Map<string, RasterView> = new Map();
class PassVisitor implements RenderGraphVisitor {
public queueID = 0xFFFFFFFF;
Expand Down Expand Up @@ -129,9 +86,6 @@ class PassVisitor implements RenderGraphVisitor {
}

private _useResourceInfo (input: string, raster: RasterView): void {
if (!DEBUG) {
return;
}
const resContext = this.context.resourceContext;
const useContext = resContext.get(input);
const resGraph = this.context.resourceGraph;
Expand Down Expand Up @@ -173,11 +127,6 @@ class PassVisitor implements RenderGraphVisitor {
private _fetchValidPass (): void {
const rg = this.context.renderGraph;
const resContext = this.context.resourceContext;
if (!DEBUG && rg.getValid(this.passID)) {
rg.setValid(this.queueID, true);
rg.setValid(this.sceneID, true);
return;
}
const outputId = this.resID;
const outputName = this.context.resourceGraph.vertexName(outputId);
readViews.clear();
Expand All @@ -187,9 +136,7 @@ class PassVisitor implements RenderGraphVisitor {
// find the pass
if (readName === outputName
&& raster.accessType !== AccessType.READ) {
if (DEBUG) {
this._useResourceInfo(readName, raster);
}
this._useResourceInfo(readName, raster);
rg.setValid(this.passID, true);
rg.setValid(this.queueID, true);
rg.setValid(this.sceneID, true);
Expand All @@ -199,7 +146,7 @@ class PassVisitor implements RenderGraphVisitor {
readViews.set(readName, raster);
}
}
if (DEBUG && validPass) return;
if (validPass) return;
if (rg.getValid(this.sceneID)) {
for (const [readName, raster] of pass.rasterViews) {
context.pipeline.resourceUses.push(readName);
Expand All @@ -215,19 +162,17 @@ class PassVisitor implements RenderGraphVisitor {
}
}
for (const [computeName, cViews] of pass.computeViews) {
if (DEBUG) {
let resUseContext = resContext.get(computeName);
if (!resUseContext) {
resUseContext = new ResourceUseContext();
resContext.set(computeName, resUseContext);
}
const computes = resUseContext.computes;
const currUseComputes = computes.get(this.passID);
if (currUseComputes) {
currUseComputes.push(cViews);
} else {
computes.set(this.passID, [cViews]);
}
let resUseContext = resContext.get(computeName);
if (!resUseContext) {
resUseContext = new ResourceUseContext();
resContext.set(computeName, resUseContext);
}
const computes = resUseContext.computes;
const currUseComputes = computes.get(this.passID);
if (currUseComputes) {
currUseComputes.push(cViews);
} else {
computes.set(this.passID, [cViews]);
}
resourceGraph = this.context.resourceGraph;
vertID = resourceGraph.find(computeName);
Expand Down
53 changes: 46 additions & 7 deletions cocos/rendering/custom/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
} from './types';
import { Vec4, geometry, toRadian, cclegacy } from '../../core';
import { RenderWindow } from '../../render-scene/core/render-window';
import { RenderData, RenderGraph } from './render-graph';
import { RasterPass, RenderData, RenderGraph } from './render-graph';
import { WebPipeline } from './web-pipeline';
import { DescriptorSetData, LayoutGraphData } from './layout-graph';
import { AABB } from '../../core/geometry';
Expand Down Expand Up @@ -617,15 +617,12 @@ function updateDefaultConstantBlock (blockId: number, sceneId: number, idxRD: nu
}

export function updatePerPassUBO (layout: string, sceneId: number, idxRD: number, user: RenderData): void {
const constantMap = user.constants;
const samplers = user.samplers;
const textures = user.textures;
const buffers = user.buffers;
const { constants, samplers, textures, buffers } = user;
const webPip = cclegacy.director.root.pipeline as WebPipeline;
const lg = webPip.layoutGraph;
const descriptorSetData = getDescriptorSetDataFromLayout(layout)!;
currBindBuffs.clear();
for (const [key, data] of constantMap) {
for (const [key, data] of constants) {
let constantBlock = constantBlockMap.get(key);
if (!constantBlock) {
const currMemKey = Array.from(lg.constantIndex).find(([_, v]) => v === key)![0];
Expand Down Expand Up @@ -683,7 +680,6 @@ export function updatePerPassUBO (layout: string, sceneId: number, idxRD: number
bindGlobalDesc(descriptorSet, bindId, value);
}
}
descriptorSet.update();
}

export function hashCombineKey (val): string {
Expand Down Expand Up @@ -866,3 +862,46 @@ 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);
}
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);
}
}
hashCode += hashCombineKey(pass.width);
hashCode += hashCombineKey(pass.height);
hashCode += hashCombineKey(pass.viewport.left);
hashCode += hashCombineKey(pass.viewport.top);
hashCode += hashCombineKey(pass.viewport.width);
hashCode += hashCombineKey(pass.viewport.height);
hashCode += hashCombineKey(pass.viewport.minDepth);
hashCode += hashCombineKey(pass.viewport.maxDepth);
hashCode += hashCombineKey(pass.showStatistics ? 1 : 0);
pass.hashValue = hashCombineStr(hashCode);
}
Loading

0 comments on commit 86ce24b

Please sign in to comment.