Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cocos/rendering/custom/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class PassVisitor implements RenderGraphVisitor {
}
}
if (validPass) return;
if (rg.getValid(this.sceneID)) {
if (rg.getValid(this.passID)) {
for (const [readName, raster] of pass.rasterViews) {
context.pipeline.resourceUses.push(readName);
}
Expand Down
28 changes: 22 additions & 6 deletions cocos/rendering/custom/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ export function AlignUp (value: number, alignment: number): number {
return (value + (alignment - 1)) & ~(alignment - 1);
}
const kLightMeterScale = 10000;
const _lightUboVec4 = new Float32Array(4);
export function SetLightUBO (
light: Light | null,
bHDR: boolean,
Expand All @@ -711,7 +712,7 @@ export function SetLightUBO (
offset: number,
elemSize: number,
): void {
const vec4Array = new Float32Array(4);
const vec4Array = _lightUboVec4;
let size = 0.0;
let range = 0.0;
let luminanceHDR = 0.0;
Expand Down Expand Up @@ -763,20 +764,28 @@ export function SetLightUBO (
buffer.set(vec4Array, index);

index = offset + UBOForwardLightEnum.LIGHT_SIZE_RANGE_ANGLE_OFFSET;
vec4Array.set([size, range, 0, 0]);
vec4Array[0] = size;
vec4Array[1] = range;
vec4Array[2] = 0;
vec4Array[3] = 0;
buffer.set(vec4Array, index);

index = offset + UBOForwardLightEnum.LIGHT_COLOR_OFFSET;
const color = light ? light.color : new Color();
if (light && light.useColorTemperature) {
const color = light.color;
const tempRGB = light.colorTemperatureRGB;
buffer[index++] = color.x * tempRGB.x;
buffer[index++] = color.y * tempRGB.y;
buffer[index++] = color.z * tempRGB.z;
} else {
} else if (light) {
const color = light.color;
buffer[index++] = color.x;
buffer[index++] = color.y;
buffer[index++] = color.z;
} else {
buffer[index++] = 0;
buffer[index++] = 0;
buffer[index++] = 0;
}

if (bHDR) {
Expand Down Expand Up @@ -879,7 +888,7 @@ export class RenderPassMergeInfo {
}

const passOrders: RasterPass[] = [];
const rpCombineMap: Map<RasterPass, number> = new Map();
export 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 {
Expand All @@ -888,6 +897,14 @@ export function resetPassMGState (): void {
rpCombineMap.clear();
passOrders.length = 0;
}
export function rebuildPassMGInfo (orderedPasses: Readonly<RasterPass[]>): void {
rpMergeInfoPool.reset();
rpMergeInfos.clear();
passOrders.length = 0;
for (const pass of orderedPasses) {
processPassMG(pass);
}
}
export function processPassMG (pass: RasterPass): void {
const currCHash = rpCombineMap.get(pass)!;
const currRPInfo = rpMergeInfoPool.add();
Expand Down Expand Up @@ -960,5 +977,4 @@ export function genHashValue (pass: RasterPass): void {

pass.hashValue = hashCombineStr(hashCodeParts.join(''));
rpCombineMap.set(pass, hashCombineStr(combineHashParts.join('')));
processPassMG(pass);
}
143 changes: 124 additions & 19 deletions cocos/rendering/custom/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@
bool,
getDescriptorSetDataFromLayout,
getRenderArea,
RenderPassMergeInfo,
rpMergeInfos,
rpCombineMap,
updateGlobalDescBinding,
} from './define';
import { LightResource, SceneCulling } from './scene-culling';
Expand Down Expand Up @@ -462,7 +461,7 @@

// Set the count of lights in cc_lightDir[0].w
const offset = fieldLen * 3 + 3;
this._lightBufferData.set([idx], offset);
this._lightBufferData[offset] = idx;
cmdBuff.updateBuffer(this._lightVolumeBuffer!, this._lightBufferData);
}

Expand Down Expand Up @@ -609,7 +608,15 @@
this.queueHint = renderQueue.hint;
const viewport = this._viewport = renderQueue.viewport;
if (viewport) {
this._scissor = new Rect(viewport.left, viewport.top, viewport.width, viewport.height);
if (!this._scissor) {
this._scissor = new Rect();
}
this._scissor.x = viewport.left;
this._scissor.y = viewport.top;
this._scissor.width = viewport.width;
this._scissor.height = viewport.height;
} else {
this._scissor = null;
}
this.queueId = id;
this._devicePass = devicePass;
Expand All @@ -619,6 +626,7 @@
if (!this._blitDesc) {
this._blitDesc = new BlitDesc(blit);
}
this._blitDesc.blit = blit;
this._blitDesc.createScreenQuad();
this._blitDesc.createStageDescriptor();
}
Expand All @@ -634,6 +642,8 @@
this._isUpdateUBO = false;
this._isUploadInstance = false;
this._isUploadBatched = false;
this._viewport = null;
this._scissor = null;
this._blitDesc?.reset();
}
get graphQueue (): RenderQueue { return this._graphQueue; }
Expand Down Expand Up @@ -753,6 +763,7 @@
const renderPassArea = new Rect();
const resourceVisitor = new ResourceVisitor();
const textureBlit = new TextureBlit();
const dispatchInfo = new DispatchInfo();
class DeviceRenderPass implements RecordingInterface {
protected _renderPass: RenderPass;
protected _framebuffer!: Framebuffer;
Expand Down Expand Up @@ -892,7 +903,8 @@
swapchain ? swapchain.depthStencilTexture : depthTex,
);
}
get passMergeInfo (): RenderPassMergeInfo { return rpMergeInfos.get(this._rasterPass)!; }
_needBeginRP = true;
_needEndRP = true;
get indexOfRD (): number { return this._idxOfRenderData; }
get rasterID (): number { return this._rasterID; }
get layoutName (): string { return this._layoutName; }
Expand Down Expand Up @@ -958,7 +970,7 @@
}
}
beginPass (): void {
if (!this.passMergeInfo.needBeginRP) {
if (!this._needBeginRP) {
this.bindGlobalDesc();
return;
}
Expand Down Expand Up @@ -987,7 +999,7 @@
}

endPass (): void {
if (!this.passMergeInfo.needEndRP) return;
if (!this._needEndRP) return;
const cmdBuff = context.commandBuffer;
cmdBuff.endRenderPass();
}
Expand Down Expand Up @@ -1466,7 +1478,11 @@
this._width = context.width;
this._height = context.height;
this._pipelineIAData = this._createQuadInputAssembler();
const vb = this._genQuadVertexData(SurfaceTransform.IDENTITY, new Rect(0, 0, context.width, context.height));
quadRect.x = 0;
quadRect.y = 0;
quadRect.width = context.width;
quadRect.height = context.height;
const vb = this._genQuadVertexData(SurfaceTransform.IDENTITY, quadRect);
this._pipelineIAData.quadVB!.update(vb);
this._createLightVolumes();
const size: number = UBOLocalEnum.SIZE;
Expand Down Expand Up @@ -1514,9 +1530,23 @@
if (this._context.root.device.capabilities.screenSpaceSignY > 0) {
[minY, maxY] = [maxY, minY];
}
const vbData = new Float32Array(16);
const fillVertices = (x1, y1, u1, v1, x2, y2, u2, v2, x3, y3, u3, v3, x4, y4, u4, v4): void => {
vbData.set([x1, y1, u1, v1, x2, y2, u2, v2, x3, y3, u3, v3, x4, y4, u4, v4]);
vbData[0] = x1;
vbData[1] = y1;
vbData[2] = u1;
vbData[3] = v1;
vbData[4] = x2;
vbData[5] = y2;
vbData[6] = u2;
vbData[7] = v2;
vbData[8] = x3;
vbData[9] = y3;
vbData[10] = u3;
vbData[11] = v3;
vbData[12] = x4;
vbData[13] = y4;
vbData[14] = u4;
vbData[15] = v4;
};
switch (surfaceTransform) {
case SurfaceTransform.IDENTITY:
Expand Down Expand Up @@ -1660,6 +1690,9 @@
}

export class Executor {
private _resourceUseSet: Set<string> = new Set<string>();
private _deletes: string[] = [];
private _deletesBuff: string[] = [];
constructor (
pipeline: BasicPipeline,
device: Device,
Expand All @@ -1686,12 +1719,19 @@
private _removeDeviceResource (): void {
const pipeline: any = context.pipeline;
const resourceUses = pipeline.resourceUses;
const deletes: string[] = [];
const resourceUseSet = this._resourceUseSet;
resourceUseSet.clear();
for (const name of resourceUses) {
resourceUseSet.add(name);

Check failure on line 1725 in cocos/rendering/custom/executor.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `string`

Check failure on line 1725 in cocos/rendering/custom/executor.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `string`
}

const deletes = this._deletes;
deletes.length = 0;
const deviceTexs = context.deviceTextures;
for (const [name, dTex] of deviceTexs) {
for (const [name] of deviceTexs) {
const resId = context.resourceGraph.vertex(name);
const trait = context.resourceGraph.getTraits(resId);
if (!resourceUses.includes(name)) {
if (!resourceUseSet.has(name)) {
switch (trait.residency) {
case ResourceResidency.MANAGED:
deletes.push(name);
Expand All @@ -1705,12 +1745,13 @@
deviceTexs.delete(name);
}

const deletesBuff: string[] = [];
const deletesBuff = this._deletesBuff;
deletesBuff.length = 0;
const deviceBuffs = context.deviceBuffers;
for (const [name, dBuff] of deviceBuffs) {
for (const [name] of deviceBuffs) {
const resId = context.resourceGraph.vertex(name);
const trait = context.resourceGraph.getTraits(resId);
if (!resourceUses.includes(name)) {
if (!resourceUseSet.has(name)) {
switch (trait.residency) {
case ResourceResidency.MANAGED:
deletesBuff.push(name);
Expand Down Expand Up @@ -1739,7 +1780,9 @@
context.lightResource.tryUpdateRenderSceneLocalDescriptorSet(context.culling);
culling.uploadInstancing(cmdBuff);
if (!this._visitor) this._visitor = new RenderVisitor();
this._visitor.reset();
depthFirstSearch(this._visitor.graphView, this._visitor, this._visitor.colorMap);
this._visitor.flush();
cmdBuff.end();
context.device.queue.submit([cmdBuff]);
}
Expand Down Expand Up @@ -1928,14 +1971,33 @@
const gx = value.threadGroupCountX;
const gy = value.threadGroupCountY;
const gz = value.threadGroupCountZ;
(cmdBuff as any).dispatch(new DispatchInfo(gx, gy, gz));
dispatchInfo.groupCountX = gx;
dispatchInfo.groupCountY = gy;
dispatchInfo.groupCountZ = gz;
dispatchInfo.indirectBuffer = null;
dispatchInfo.indirectOffset = 0;
(cmdBuff as any).dispatch(dispatchInfo);
}
}

class PostRenderVisitor extends BaseRenderVisitor implements RenderGraphVisitor {
private _pendingEndPass: DeviceRenderPass | null = null;
private _lastCombineHash: number = 0;
constructor () {
super();
}
reset (): void {
this._pendingEndPass = null;
this._lastCombineHash = 0;
}
flush (): void {
if (this._pendingEndPass) {
this._pendingEndPass._needEndRP = true;
this._pendingEndPass.endPass();
this._pendingEndPass.postRecord();
this._pendingEndPass = null;
}
}
clear (value: ClearView[]): void {
// do nothing
}
Expand All @@ -1949,8 +2011,43 @@
if (!currPass) return;
this.currPass = currPass;
context.passShowStatistics = pass.showStatistics;
this.currPass.record();
this.currPass.postRecord();

const currCHash = rpCombineMap.get(pass) ?? 0;
let canMerge = false;
if (this._pendingEndPass && currCHash === this._lastCombineHash) {
for (const [_, raster] of pass.rasterViews) {
if (raster.loadOp === LoadOp.LOAD) {
canMerge = true;
break;
}
}
}

if (this._pendingEndPass) {
if (canMerge) {
// Skip endRenderPass for previous pass (merge)
this._pendingEndPass._needEndRP = false;
this._pendingEndPass.endPass();
this._pendingEndPass.postRecord();
} else {
// End previous pass normally
this._pendingEndPass._needEndRP = true;
this._pendingEndPass.endPass();
this._pendingEndPass.postRecord();
}
}

// Begin and record queues for current pass
currPass._needBeginRP = !canMerge;
currPass._needEndRP = true;
currPass.beginPass();
for (const queue of currPass.deviceQueues.values()) {
queue.record();
}

// Defer endPass to see if next pass can merge
this._pendingEndPass = currPass;
this._lastCombineHash = currCHash;
}
rasterSubpass (value: RasterSubpass): void {
// do nothing
Expand Down Expand Up @@ -2000,6 +2097,14 @@
this._colorMap = new VectorGraphColorMap(context.renderGraph.nv());
}

reset (): void {
this._postVisitor.reset();
}

flush (): void {
this._postVisitor.flush();
}

get graphView (): ReferenceGraphView<RenderGraph> { return this._graphView; }
get colorMap (): VectorGraphColorMap { return this._colorMap; }
discoverVertex (u: number, gv: ReferenceGraphView<RenderGraph>): void {
Expand Down
3 changes: 2 additions & 1 deletion cocos/rendering/custom/scene-culling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,8 @@ export class LightResource {
// Already added
const existingLightID = this.lightIndex.get(light);
if (existingLightID !== undefined) {
return existingLightID;
return existingLightID * this.elementSize;
// return existingLightID;
}

if (!this.lightBuffer) {
Expand Down
Loading
Loading