Skip to content
Merged
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
102 changes: 76 additions & 26 deletions cocos/core/pipeline/custom/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,23 @@
*/
/* eslint-disable max-len */
import { getPhaseID, InstancedBuffer, PipelineStateManager } from '..';
import { Buffer, Color, ColorAttachment, CommandBuffer, DepthStencilAttachment, Device, Format, Framebuffer,
FramebufferInfo, PipelineState, Rect, RenderPass, RenderPassInfo, Swapchain, Texture, TextureInfo,
import { AccessFlagBit, Buffer, ClearFlagBit, Color, ColorAttachment, CommandBuffer, DepthStencilAttachment, Device, Format, Framebuffer,
FramebufferInfo, GeneralBarrierInfo, LoadOp, PipelineState, Rect, RenderPass, RenderPassInfo, StoreOp, Swapchain, Texture, TextureInfo,
TextureType, TextureUsageBit, Viewport } from '../../gfx';
import { legacyCC } from '../../global-exports';
import { assert } from '../../platform';
import { BatchingSchemes } from '../../renderer';
import { Camera } from '../../renderer/scene/camera';
import { Camera, SKYBOX_FLAG } from '../../renderer/scene/camera';
import { Root } from '../../root';
import { BatchedBuffer } from '../batched-buffer';
import { SetIndex } from '../define';
import { PipelineSceneData } from '../pipeline-scene-data';
import { Pipeline, SceneVisitor } from './pipeline';
import { AccessType, AttachmentType, Blit, ComputePass, CopyPass, Dispatch, ManagedResource, MovePass, PresentPass,
RasterPass, RaytracePass, RenderGraph, RenderGraphValue, RenderGraphVisitor, RenderQueue, RenderSwapchain, ResourceDesc,
RasterPass, RasterView, RaytracePass, RenderGraph, RenderGraphValue, RenderGraphVisitor, RenderQueue, RenderSwapchain, ResourceDesc,
ResourceGraph, ResourceGraphVisitor, ResourceTraits, SceneData } from './render-graph';
import { QueueHint, ResourceDimension, ResourceFlags } from './types';
import { PipelineUBO } from './ubos';
import { RenderInfo, RenderObject, WebSceneTask } from './web-scene';
import { WebSceneVisitor } from './web-scene-visitor';

Expand Down Expand Up @@ -142,6 +143,7 @@ class DeviceRenderQueue {
clearTasks () {
this._sceneTasks.length = 0;
}
get sceneTasks () { return this._sceneTasks; }
set queueHint (value: QueueHint) { this._hint = value; }
get queueHint () { return this._hint; }
get devicePass () { return this._devicePass; }
Expand All @@ -167,12 +169,18 @@ class DeviceRenderPass {
constructor (context: ExecutorContext, rasterPass: RasterPass) {
this._context = context;
this._rasterPass = rasterPass;
const device = context.device;
const depthStencilAttachment = new DepthStencilAttachment();
depthStencilAttachment.format = Format.DEPTH_STENCIL;
depthStencilAttachment.stencilStoreOp = StoreOp.DISCARD;
depthStencilAttachment.depthStoreOp = StoreOp.DISCARD;
depthStencilAttachment.barrier = device.getGeneralBarrier(new GeneralBarrierInfo(
AccessFlagBit.DEPTH_STENCIL_ATTACHMENT_WRITE,
AccessFlagBit.DEPTH_STENCIL_ATTACHMENT_WRITE,
));
const colors: ColorAttachment[] = [];
const colorTexs: Texture[] = [];
let depthTex: Texture | null = null;
const device = context.device;
let swapchain: Swapchain | null = null;
let framebuffer: Framebuffer | null = null;
for (const [resName, rasterV] of rasterPass.rasterViews) {
Expand All @@ -187,26 +195,41 @@ class DeviceRenderPass {
resTex = context.deviceTextures.get(resName)!;
if (!swapchain) swapchain = resTex.swapchain;
if (!framebuffer) framebuffer = resTex.framebuffer;
const clearFlag = rasterV.clearFlags & 0xffffffff;
switch (rasterV.attachmentType) {
case AttachmentType.RENDER_TARGET:
colorTexs.push(resTex.swapchain ? resTex.swapchain.colorTexture : resTex.texture!);
this._clearColor.push(rasterV.clearColor);
colors.push(new ColorAttachment(
resTex.description!.format,
resTex.description!.sampleCount,
rasterV.loadOp,
rasterV.storeOp,
));
{
if (!resTex.swapchain && !resTex.framebuffer) colorTexs.push(resTex.texture!);
const colorAttachment = new ColorAttachment();
colorAttachment.format = resTex.description!.format;
colorAttachment.sampleCount = resTex.description!.sampleCount;
if (!(clearFlag & ClearFlagBit.COLOR)) {
rasterV.clearColor.x = 0;
rasterV.clearColor.y = 0;
rasterV.clearColor.z = 0;
rasterV.clearColor.w = 1;
if (clearFlag & SKYBOX_FLAG) {
colorAttachment.loadOp = LoadOp.DISCARD;
} else {
colorAttachment.loadOp = LoadOp.LOAD;
colorAttachment.barrier = device.getGeneralBarrier(new GeneralBarrierInfo(
AccessFlagBit.COLOR_ATTACHMENT_WRITE,
AccessFlagBit.COLOR_ATTACHMENT_WRITE,
));
}
}
this._clearColor.push(rasterV.clearColor);
colors.push(colorAttachment);
}
break;
case AttachmentType.DEPTH_STENCIL:
depthTex = resTex.swapchain ? resTex.swapchain.depthStencilTexture : resTex.texture!;
if (!resTex.swapchain && !resTex.framebuffer) depthTex = resTex.texture!;
if ((clearFlag & ClearFlagBit.DEPTH_STENCIL) !== ClearFlagBit.DEPTH_STENCIL) {
if (!(clearFlag & ClearFlagBit.DEPTH)) depthStencilAttachment.depthLoadOp = LoadOp.LOAD;
if (!(clearFlag & ClearFlagBit.STENCIL)) depthStencilAttachment.stencilLoadOp = LoadOp.LOAD;
}
this._clearDepth = rasterV.clearColor.x;
this._clearStencil = rasterV.clearColor.y;
depthStencilAttachment.format = resTex.description!.format;
depthStencilAttachment.depthLoadOp = rasterV.loadOp;
depthStencilAttachment.depthStoreOp = rasterV.storeOp;
depthStencilAttachment.stencilLoadOp = rasterV.loadOp;
depthStencilAttachment.stencilStoreOp = rasterV.storeOp;
break;
default:
}
Expand All @@ -229,7 +252,9 @@ class DeviceRenderPass {
));
}
this._renderPass = device.createRenderPass(new RenderPassInfo(colors, depthStencilAttachment));
this._framebuffer = framebuffer || device.createFramebuffer(new FramebufferInfo(this._renderPass, colorTexs, depthTex));
this._framebuffer = framebuffer || device.createFramebuffer(new FramebufferInfo(this._renderPass,
swapchain ? [swapchain.colorTexture] : colorTexs,
swapchain ? swapchain.depthStencilTexture : depthTex));
}
get context () { return this._context; }
get renderPass () { return this._renderPass; }
Expand All @@ -243,7 +268,8 @@ class DeviceRenderPass {
// record common buffer
record () {
const cmdBuff = this._context.commandBuffer;
cmdBuff.beginRenderPass(this.renderPass, this.framebuffer, new Rect(),
const tex = this.framebuffer.colorTextures[0]!;
cmdBuff.beginRenderPass(this.renderPass, this.framebuffer, new Rect(0, 0, tex.width, tex.height),
this.clearColor, this.clearDepth, this.clearStencil);
cmdBuff.bindDescriptorSet(SetIndex.GLOBAL,
this._context.pipeline.globalDSManager.globalDescriptorSet);
Expand Down Expand Up @@ -441,6 +467,7 @@ class DeviceSceneTask extends WebSceneTask {

class ExecutorContext {
constructor (pipeline: Pipeline,
ubo: PipelineUBO,
device: Device,
resourceGraph: ResourceGraph,
renderGraph: RenderGraph) {
Expand All @@ -451,6 +478,7 @@ class ExecutorContext {
this.resourceGraph = resourceGraph;
this.renderGraph = renderGraph;
this.root = legacyCC.director.root;
this.ubo = ubo;
}
readonly device: Device;
readonly pipeline: Pipeline;
Expand All @@ -461,6 +489,7 @@ class ExecutorContext {
readonly deviceTextures: Map<string, DeviceTexture> = new Map<string, DeviceTexture>();
readonly renderGraph: RenderGraph;
readonly root: Root;
readonly ubo: PipelineUBO;
}

class ResourceVisitor implements ResourceGraphVisitor {
Expand Down Expand Up @@ -515,6 +544,19 @@ class PassVisitor implements RenderGraphVisitor {
rg.visitVertex(this, queueID);
}
}
let currCamera: Camera | null = null;
for (const queue of this._currPass.deviceQueues) {
for (const task of queue.sceneTasks) {
if (currCamera === task.camera) {
continue;
}
currCamera = task.camera;
const ubo = this._currPass.context.ubo;
ubo.updateGlobalUBO(task.camera.window);
ubo.updateCameraUBO(task.camera);
ubo.updateShadowUBO(task.camera);
}
}
this._currPass.record();
}
compute (pass: ComputePass) {
Expand Down Expand Up @@ -555,21 +597,26 @@ class PassVisitor implements RenderGraphVisitor {

export class Executor {
constructor (pipeline: Pipeline,
ubo: PipelineUBO,
device: Device,
resourceGraph: ResourceGraph) {
this._pipeline = pipeline;
this._device = device;
this._ubo = ubo;
this._commandBuffer = device.commandBuffer;
this._resourceGraph = resourceGraph;
}

execute (rg: RenderGraph) {
const context = new ExecutorContext(
this._pipeline,
this._ubo,
this._device,
this._resourceGraph,
rg,
);
const cmdBuff = context.commandBuffer;
cmdBuff.begin();
const passVisitor = new PassVisitor(context);
for (const vertID of rg.vertices()) {
if (rg.numParents(vertID) === 0) {
Expand All @@ -578,9 +625,12 @@ export class Executor {
rg.visitVertex(passVisitor, vertID);
}
}
}
private readonly _pipeline: Pipeline
private readonly _device: Device
private readonly _commandBuffer: CommandBuffer
private readonly _resourceGraph: ResourceGraph
cmdBuff.end();
context.device.queue.submit([cmdBuff]);
}
private readonly _pipeline: Pipeline;
private readonly _device: Device;
private readonly _commandBuffer: CommandBuffer;
private readonly _resourceGraph: ResourceGraph;
private readonly _ubo: PipelineUBO;
}
47 changes: 17 additions & 30 deletions cocos/core/pipeline/custom/web-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ export class WebPipeline extends Pipeline {
throw new Error('Cannot run without creating rendergraph');
}
if (!this._executor) {
this._executor = new Executor(this, this._device, this._resourceGraph);
this._executor = new Executor(this, this._pipelineUBO, this._device, this._resourceGraph);
}
this._executor.execute(this._renderGraph);
}
Expand Down Expand Up @@ -548,63 +548,49 @@ export class WebPipeline extends Pipeline {
if (cameras.length === 0) {
return;
}
cameras.forEach((camera) => {
if (!this._cameras.includes(camera)) {
this._cameras.push(camera);
}
});
// build graph
this.beginFrame();
for (let i = 0; i < cameras.length; i++) {
const camera = cameras[i];
if (camera.scene) {
this._pipelineUBO.updateGlobalUBO(camera.window);
this._pipelineUBO.updateCameraUBO(camera);
this._pipelineUBO.updateShadowUBO(camera);
const idx = this._cameras.indexOf(camera);
this._buildShadowPasses(this, this._validLights,
camera.scene.mainLight,
this._pipelineSceneData,
`Camera${i.toString()}`);
`Camera${idx}`);

const window = camera.window;
const width = Math.floor(window.width);
const height = Math.floor(window.height);
const forwardPassRTName = `dsForwardPassColorCamera${i}`;
const forwardPassDSName = `dsForwardPassDSCamera${i}`;
const forwardPassRTName = `dsForwardPassColorCamera${idx}`;
const forwardPassDSName = `dsForwardPassDSCamera${idx}`;
if (!this.resourceGraph.contains(forwardPassRTName)) {
this.addRenderTexture(forwardPassRTName, Format.RGBA8, width, height, camera.window);
this.addRenderTarget(forwardPassDSName, Format.DEPTH_STENCIL, width, height, ResourceResidency.MANAGED);
this.addDepthStencil(forwardPassDSName, Format.DEPTH_STENCIL, width, height, ResourceResidency.MANAGED);
}
const forwardPass = this.addRasterPass(width, height, '_', `CameraForwardPass${i.toString()}`);
const forwardPass = this.addRasterPass(width, height, '_', `CameraForwardPass${idx}`);
if (this.resourceGraph.contains(this._dsShadowMap)) {
forwardPass.addRasterView(this._dsShadowMap, new RasterView('_',
AccessType.READ, AttachmentType.RENDER_TARGET,
LoadOp.CLEAR, StoreOp.DISCARD,
ClearFlagBit.NONE,
new Color(0, 0, 0, 0)));
new Color(0, 0, 0, 1)));
}
const passView = new RasterView('_',
AccessType.WRITE, AttachmentType.RENDER_TARGET,
LoadOp.CLEAR, StoreOp.STORE,
ClearFlagBit.NONE,
new Color(0, 0, 0, 0));
if (!(camera.clearFlag & ClearFlagBit.COLOR)) {
if (camera.clearFlag & SKYBOX_FLAG) {
passView.loadOp = LoadOp.DISCARD;
} else {
passView.loadOp = LoadOp.LOAD;
passView.accessType = AccessType.READ_WRITE;
}
} else {
passView.clearColor.x = camera.clearColor.x;
passView.clearColor.y = camera.clearColor.y;
passView.clearColor.z = camera.clearColor.z;
passView.clearColor.w = camera.clearColor.w;
}
camera.clearFlag,
new Color(camera.clearColor.x, camera.clearColor.y, camera.clearColor.z, camera.clearColor.w));
const passDSView = new RasterView('_',
AccessType.WRITE, AttachmentType.DEPTH_STENCIL,
LoadOp.CLEAR, StoreOp.STORE,
ClearFlagBit.DEPTH_STENCIL,
camera.clearFlag,
new Color(1, 0, 0, 0));
if ((camera.clearFlag & ClearFlagBit.DEPTH_STENCIL) !== ClearFlagBit.DEPTH_STENCIL) {
if (!(camera.clearFlag & ClearFlagBit.DEPTH)) passDSView.loadOp = LoadOp.LOAD;
if (!(camera.clearFlag & ClearFlagBit.STENCIL)) passDSView.loadOp = LoadOp.LOAD;
}
forwardPass.addRasterView(forwardPassRTName, passView);
forwardPass.addRasterView(forwardPassDSName, passDSView);
forwardPass
Expand Down Expand Up @@ -682,6 +668,7 @@ export class WebPipeline extends Pipeline {
private _constantMacros = '';
private _profiler: Model | null = null;
private _pipelineUBO: PipelineUBO = new PipelineUBO();
private _cameras: Camera[] = [];

private readonly _layoutGraph: LayoutGraphData = new LayoutGraphData();
private readonly _resourceGraph: ResourceGraph = new ResourceGraph();
Expand Down