Skip to content

Commit

Permalink
Optimized execution code
Browse files Browse the repository at this point in the history
  • Loading branch information
GengineJS committed Oct 16, 2024
1 parent 0db84ff commit 1b90117
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 55 deletions.
52 changes: 27 additions & 25 deletions cocos/rendering/custom/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
49 changes: 19 additions & 30 deletions cocos/rendering/custom/web-pipeline-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,48 +900,37 @@ export class RenderDrawQueue {
}

export class RenderInstancingQueue {
passInstances: Map<Pass, number> = new Map<Pass, number>();
instanceBuffers: Array<InstancedBuffer> = new Array<InstancedBuffer>();

/**
* @en A set of instanced buffer
* @zh Instance 合批缓存集合。
*/
public queue = new Set<InstancedBuffer>();
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();
}
}

Expand All @@ -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;
Expand Down

0 comments on commit 1b90117

Please sign in to comment.