diff --git a/cocos/asset/assets/effect-asset.ts b/cocos/asset/assets/effect-asset.ts index 4b58246482..0fc3843447 100644 --- a/cocos/asset/assets/effect-asset.ts +++ b/cocos/asset/assets/effect-asset.ts @@ -158,8 +158,8 @@ export declare namespace EffectAsset { export interface IShaderInfo { name: string; hash: number; - glsl4: { vert: string, frag: string }; - glsl3: { vert: string, frag: string }; + glsl4: { vert: string, frag: string, compute?: string }; + glsl3: { vert: string, frag: string, compute?: string }; glsl1: { vert: string, frag: string }; builtins: { globals: IBuiltinInfo, locals: IBuiltinInfo, statistics: Record }; defines: IDefineInfo[]; diff --git a/cocos/rendering/custom/define.ts b/cocos/rendering/custom/define.ts index 50bcc3ee8d..4bf4b42a3d 100644 --- a/cocos/rendering/custom/define.ts +++ b/cocos/rendering/custom/define.ts @@ -22,6 +22,7 @@ THE SOFTWARE. */ +import { EDITOR } from 'internal:constants'; import { BufferInfo, Buffer, BufferUsageBit, ClearFlagBit, Color, DescriptorSet, LoadOp, Format, Rect, Sampler, StoreOp, Texture, Viewport, MemoryUsageBit } from '../../gfx'; import { Camera, CSMLevel, DirectionalLight, Light, LightType, ReflectionProbe, ShadowType, SKYBOX_FLAG, SpotLight } from '../../render-scene/scene'; @@ -529,6 +530,9 @@ export function buildPostprocessPass (camera: Camera, export function buildForwardPass (camera: Camera, ppl: Pipeline, isOffScreen: boolean) { + if (EDITOR) { + ppl.setMacroInt('CC_PIPELINE_TYPE', 0); + } const cameraID = getCameraUniqueID(camera); const cameraName = `Camera${cameraID}`; const cameraInfo = buildShadowPasses(cameraName, camera, ppl); diff --git a/cocos/rendering/custom/executor.ts b/cocos/rendering/custom/executor.ts index 8bf8d0af77..fdcce0188e 100644 --- a/cocos/rendering/custom/executor.ts +++ b/cocos/rendering/custom/executor.ts @@ -29,7 +29,7 @@ */ /* eslint-disable max-len */ import { getPhaseID, InstancedBuffer, PipelineStateManager } from '..'; -import { assert, cclegacy } from '../../core'; +import { assert, cclegacy, RecyclePool } from '../../core'; import intersect from '../../core/geometry/intersect'; import { Sphere } from '../../core/geometry/sphere'; import { AccessFlagBit, Attribute, Buffer, BufferInfo, BufferUsageBit, BufferViewInfo, Color, ColorAttachment, CommandBuffer, DepthStencilAttachment, DescriptorSet, DescriptorSetInfo, Device, deviceManager, Format, Framebuffer, @@ -38,12 +38,12 @@ import { AccessFlagBit, Attribute, Buffer, BufferInfo, BufferUsageBit, BufferVie import { legacyCC } from '../../core/global-exports'; import { Vec3 } from '../../core/math/vec3'; import { Vec4 } from '../../core/math/vec4'; -import { BatchingSchemes } from '../../render-scene'; +import { BatchingSchemes, Pass } from '../../render-scene'; import { Camera } from '../../render-scene/scene/camera'; import { ShadowType } from '../../render-scene/scene/shadows'; import { Root } from '../../root'; import { BatchedBuffer } from '../batched-buffer'; -import { isEnableEffect, SetIndex, UBODeferredLight, UBOForwardLight, UBOLocal } from '../define'; +import { IRenderPass, isEnableEffect, SetIndex, UBODeferredLight, UBOForwardLight, UBOLocal } from '../define'; import { PipelineSceneData } from '../pipeline-scene-data'; import { PipelineInputAssemblerData } from '../render-pipeline'; import { LayoutGraphData, PipelineLayoutData, RenderPhaseData, RenderStageData } from './layout-graph'; @@ -157,11 +157,14 @@ class DeviceBuffer extends DeviceResource { } } +const _vec4Array = new Float32Array(4); class BlitDesc { + private _isUpdate = false; + private _isGatherLight = false; private _blit: Blit; private _screenQuad: PipelineInputAssemblerData | null = null; private _queue: DeviceRenderQueue | null = null; - private _stageDesc: DescriptorSet | null = null; + private _stageDesc: DescriptorSet | undefined; // If VOLUMETRIC_LIGHTING is turned on, it needs to be assigned private _lightVolumeBuffer: Buffer | null = null; private _lightMeterScale = 10000.0; @@ -179,73 +182,14 @@ class BlitDesc { * 创建四边形输入汇集器。 */ protected _createQuadInputAssembler (): PipelineInputAssemblerData { - // create vertex buffer - const inputAssemblerData = new PipelineInputAssemblerData(); - - const vbStride = Float32Array.BYTES_PER_ELEMENT * 4; - const vbSize = vbStride * 4; - const device = this._queue!.devicePass.context.device; - const quadVB = device.createBuffer(new BufferInfo( - BufferUsageBit.VERTEX | BufferUsageBit.TRANSFER_DST, - MemoryUsageBit.DEVICE | MemoryUsageBit.HOST, - vbSize, - vbStride, - )); - - if (!quadVB) { - return inputAssemblerData; - } - - // create index buffer - const ibStride = Uint8Array.BYTES_PER_ELEMENT; - const ibSize = ibStride * 6; - - const quadIB = device.createBuffer(new BufferInfo( - BufferUsageBit.INDEX | BufferUsageBit.TRANSFER_DST, - MemoryUsageBit.DEVICE, - ibSize, - ibStride, - )); - - if (!quadIB) { - return inputAssemblerData; - } - - const indices = new Uint8Array(6); - indices[0] = 0; indices[1] = 1; indices[2] = 2; - indices[3] = 1; indices[4] = 3; indices[5] = 2; - - quadIB.update(indices); - - // create input assembler - - const attributes = new Array(2); - attributes[0] = new Attribute('a_position', Format.RG32F); - attributes[1] = new Attribute('a_texCoord', Format.RG32F); - - const quadIA = device.createInputAssembler(new InputAssemblerInfo( - attributes, - [quadVB], - quadIB, - )); - - inputAssemblerData.quadIB = quadIB; - inputAssemblerData.quadVB = quadVB; - inputAssemblerData.quadIA = quadIA; - return inputAssemblerData; + const devicePass = this._queue!.devicePass; + return devicePass.context.blit.pipelineIAData; } createScreenQuad () { if (!this._screenQuad) { this._screenQuad = this._createQuadInputAssembler(); } } - private _updateScreenVB () { - const devicePass = this._queue!.devicePass; - const width = devicePass.context.width; - const height = devicePass.context.height; - const vb = devicePass.genQuadVertexData(SurfaceTransform.IDENTITY, new Rect(0, 0, width, height)); - this._screenQuad!.quadVB!.update(vb); - } private _gatherVolumeLights (camera: Camera) { if (!camera.scene) { return; } const context = this._queue!.devicePass.context; @@ -255,7 +199,6 @@ class BlitDesc { const sphereLights = camera.scene.sphereLights; const spotLights = camera.scene.spotLights; const _sphere = Sphere.create(0, 0, 0, 1); - const _vec4Array = new Float32Array(4); const exposure = camera.exposure; let idx = 0; @@ -340,45 +283,42 @@ class BlitDesc { cmdBuff.updateBuffer(this._lightVolumeBuffer!, this._lightBufferData); } update () { - this._updateScreenVB(); if (this.blit.sceneFlags & SceneFlags.VOLUMETRIC_LIGHTING - && this.blit.camera) { + && this.blit.camera && !this._isGatherLight) { this._gatherVolumeLights(this.blit.camera); + this._isGatherLight = true; + this._isUpdate = false; } - this._stageDesc!.update(); + if (!this._isUpdate) { + this._stageDesc!.update(); + this._isUpdate = true; + } + } + + reset () { + this._isUpdate = false; + this._isGatherLight = false; } createStageDescriptor () { - if (this._stageDesc) { - return; - } const blit = this.blit; const pass = blit.material!.passes[blit.passID]; - const device = this._queue!.devicePass.context.device; - this._stageDesc = device.createDescriptorSet(new DescriptorSetInfo(pass.localSetLayout)); + const context = this._queue!.devicePass.context; + const device = context.device; + this._stageDesc = context.blit.stageDescs.get(pass); + if (!this._stageDesc) { + this._stageDesc = device.createDescriptorSet(new DescriptorSetInfo(pass.localSetLayout)); + context.blit.stageDescs.set(pass, this._stageDesc); + } if (this.blit.sceneFlags & SceneFlags.VOLUMETRIC_LIGHTING) { - let totalSize = Float32Array.BYTES_PER_ELEMENT * 4 * 4 * UBODeferredLight.LIGHTS_PER_PASS; - totalSize = Math.ceil(totalSize / device.capabilities.uboOffsetAlignment) * device.capabilities.uboOffsetAlignment; - - this._lightVolumeBuffer = device.createBuffer(new BufferInfo( - BufferUsageBit.UNIFORM | BufferUsageBit.TRANSFER_DST, - MemoryUsageBit.HOST | MemoryUsageBit.DEVICE, - totalSize, - device.capabilities.uboOffsetAlignment, - )); - - const deferredLitsBufView = device.createBuffer(new BufferViewInfo(this._lightVolumeBuffer, 0, totalSize)); - this._lightBufferData = new Float32Array(totalSize / Float32Array.BYTES_PER_ELEMENT); + this._lightVolumeBuffer = context.blit.lightVolumeBuffer; + const deferredLitsBufView = context.blit.deferredLitsBufView; + this._lightBufferData = context.blit.lightBufferData; + this._lightBufferData.fill(0); const binding = isEnableEffect() ? getDescBindingFromName('CCForwardLight') : UBOForwardLight.BINDING; this._stageDesc.bindBuffer(UBOForwardLight.BINDING, deferredLitsBufView); } - const _localUBO = device.createBuffer(new BufferInfo( - BufferUsageBit.UNIFORM | BufferUsageBit.TRANSFER_DST, - MemoryUsageBit.DEVICE, - UBOLocal.SIZE, - UBOLocal.SIZE, - )); - this._stageDesc.bindBuffer(UBOLocal.BINDING, _localUBO); + this._stageDesc.bindBuffer(UBOLocal.BINDING, context.blit.emptyLocalUBO); } } @@ -386,30 +326,43 @@ class DeviceRenderQueue { private _preSceneTasks: DevicePreSceneTask[] = []; private _sceneTasks: DeviceSceneTask[] = []; private _postSceneTasks: DevicePostSceneTask[] = []; - private _devicePass: DeviceRenderPass; + private _devicePass: DeviceRenderPass | undefined; private _hint: QueueHint = QueueHint.NONE; private _phaseID: number = getPhaseID('default'); private _renderPhase: RenderPhaseData | null = null; + private _isUpdateUBO = false; + private _isUploadInstance = false; + private _isUploadBatched = false; protected _transversal: DeviceSceneTransversal | null = null; get phaseID (): number { return this._phaseID; } get renderPhase (): RenderPhaseData | null { return this._renderPhase; } set renderPhase (val) { this._renderPhase = val; } - private _sceneVisitor: WebSceneVisitor; + private _sceneVisitor; private _blitDesc: BlitDesc | null = null; private _queueId = -1; set queueId (val) { this._queueId = val; } get queueId () { return this._queueId; } - constructor (devicePass: DeviceRenderPass) { + set isUpdateUBO (update: boolean) { this._isUpdateUBO = update; } + get isUpdateUBO () { return this._isUpdateUBO; } + set isUploadInstance (value: boolean) { this._isUploadInstance = value; } + get isUploadInstance () { return this._isUploadInstance; } + set isUploadBatched (value: boolean) { this._isUploadBatched = value; } + get isUploadBatched () { return this._isUploadBatched; } + init (devicePass: DeviceRenderPass, queueHint: QueueHint, id: number) { + this.reset(); + this.queueHint = queueHint; + this.queueId = id; this._devicePass = devicePass; if (isEnableEffect()) this._phaseID = cclegacy.rendering.getPhaseID(devicePass.passID, 'default'); - this._sceneVisitor = new WebSceneVisitor(this._devicePass.context.commandBuffer, - this._devicePass.context.pipeline.pipelineSceneData); + if (!this._sceneVisitor) { + this._sceneVisitor = new WebSceneVisitor(devicePass.context.commandBuffer, + devicePass.context.pipeline.pipelineSceneData); + } } createBlitDesc (blit: Blit) { - if (this._blitDesc) { - return; + if (!this._blitDesc) { + this._blitDesc = new BlitDesc(blit, this); } - this._blitDesc = new BlitDesc(blit, this); this._blitDesc.createScreenQuad(); this._blitDesc.createStageDescriptor(); } @@ -417,17 +370,23 @@ class DeviceRenderQueue { if (!this._transversal) { this._transversal = new DeviceSceneTransversal(this, this.devicePass.context.pipelineSceneData, scene); } + this._transversal.graphScene = scene; this._preSceneTasks.push(this._transversal.preRenderPass(this._sceneVisitor)); this._sceneTasks.push(this._transversal.transverse(this._sceneVisitor)); } - clearTasks () { - this._sceneTasks.length = 0; + reset () { + this._postSceneTasks.length = this._preSceneTasks.length = this._sceneTasks.length = 0; + this._isUpdateUBO = false; + this._isUploadInstance = false; + this._isUploadBatched = false; + this._blitDesc?.reset(); } get blitDesc () { return this._blitDesc; } get sceneTasks () { return this._sceneTasks; } set queueHint (value: QueueHint) { this._hint = value; } get queueHint () { return this._hint; } - get devicePass () { return this._devicePass; } + get devicePass () { return this._devicePass!; } + get preSceneTasks () { return this._preSceneTasks; } preRecord () { for (const task of this._preSceneTasks) { task.start(); @@ -463,6 +422,21 @@ class SubmitInfo { public shadowMap: RenderShadowMapBatchedQueue | null = null; public additiveLight: RenderAdditiveLightQueue | null = null; public reflectionProbe: RenderReflectionProbeQueue | null = null; + private _isInit = false; + get isInit () { return this._isInit; } + set isInit (val: boolean) { this._isInit = val; } + reset () { + this._isInit = false; + this.instances.clear(); + this.renderInstanceQueue.length = 0; + this.batches.clear(); + this.opaqueList.length = 0; + this.transparentList.length = 0; + this.planarQueue = null; + this.shadowMap = null; + this.additiveLight = null; + this.reflectionProbe = null; + } } class RenderPassLayoutInfo { @@ -537,6 +511,8 @@ class RasterPassInfo { } } +const profilerViewport = new Viewport(); +const renderPassArea = new Rect(); class DeviceRenderPass { protected _renderPass: RenderPass; protected _framebuffer: Framebuffer; @@ -681,54 +657,6 @@ class DeviceRenderPass { const layoutData = layout.descriptorSets.get(UpdateFrequency.PER_PASS)!; return layoutData; } - genQuadVertexData (surfaceTransform: SurfaceTransform, renderArea: Rect): Float32Array { - const vbData = new Float32Array(4 * 4); - - const minX = renderArea.x / this._context.width; - const maxX = (renderArea.x + renderArea.width) / this._context.width; - let minY = renderArea.y / this._context.height; - let maxY = (renderArea.y + renderArea.height) / this._context.height; - if (this._context.root.device.capabilities.screenSpaceSignY > 0) { - const temp = maxY; - maxY = minY; - minY = temp; - } - let n = 0; - switch (surfaceTransform) { - case (SurfaceTransform.IDENTITY): - n = 0; - vbData[n++] = -1.0; vbData[n++] = -1.0; vbData[n++] = minX; vbData[n++] = maxY; - vbData[n++] = 1.0; vbData[n++] = -1.0; vbData[n++] = maxX; vbData[n++] = maxY; - vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = minX; vbData[n++] = minY; - vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = maxX; vbData[n++] = minY; - break; - case (SurfaceTransform.ROTATE_90): - n = 0; - vbData[n++] = -1.0; vbData[n++] = -1.0; vbData[n++] = maxX; vbData[n++] = maxY; - vbData[n++] = 1.0; vbData[n++] = -1.0; vbData[n++] = maxX; vbData[n++] = minY; - vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = minX; vbData[n++] = maxY; - vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = minX; vbData[n++] = minY; - break; - case (SurfaceTransform.ROTATE_180): - n = 0; - vbData[n++] = -1.0; vbData[n++] = -1.0; vbData[n++] = minX; vbData[n++] = minY; - vbData[n++] = 1.0; vbData[n++] = -1.0; vbData[n++] = maxX; vbData[n++] = minY; - vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = minX; vbData[n++] = maxY; - vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = maxX; vbData[n++] = maxY; - break; - case (SurfaceTransform.ROTATE_270): - n = 0; - vbData[n++] = -1.0; vbData[n++] = -1.0; vbData[n++] = minX; vbData[n++] = minY; - vbData[n++] = 1.0; vbData[n++] = -1.0; vbData[n++] = minX; vbData[n++] = maxY; - vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = maxX; vbData[n++] = minY; - vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = maxX; vbData[n++] = maxY; - break; - default: - break; - } - - return vbData; - } protected _applyViewport (frameTex: Texture) { this._viewport = null; @@ -757,10 +685,8 @@ class DeviceRenderPass { submodel.shaders[0], renderPass, ia); const descData = getDescriptorSetDataFromLayoutId(pass.passID)!; mergeSrcToTargetDesc(descData.descriptorSet, context.pipeline.descriptorSet, true); - const profilerViewport = new Viewport(); profilerViewport.width = rect.width; profilerViewport.height = rect.height; - cmdBuff.setViewport(profilerViewport); cmdBuff.setScissor(rect); cmdBuff.bindPipelineState(pso); @@ -775,10 +701,17 @@ class DeviceRenderPass { const tex = this.framebuffer.colorTextures[0]!; this._applyViewport(tex); const cmdBuff = this._context.commandBuffer; - const renderArea = this._viewport - ? new Rect(this._viewport.left, this._viewport.top, this._viewport.width, this._viewport.height) - : new Rect(0, 0, tex.width, tex.height); - cmdBuff.beginRenderPass(this.renderPass, this.framebuffer, renderArea, + if (this._viewport) { + renderPassArea.x = this._viewport.left; + renderPassArea.y = this._viewport.top; + renderPassArea.width = this._viewport.width; + renderPassArea.height = this._viewport.height; + } else { + renderPassArea.y = renderPassArea.x = 0; + renderPassArea.width = tex.width; + renderPassArea.height = tex.height; + } + cmdBuff.beginRenderPass(this.renderPass, this.framebuffer, renderPassArea, this.clearColor, this.clearDepth, this.clearStencil); cmdBuff.bindDescriptorSet(SetIndex.GLOBAL, this._context.pipeline.descriptorSet); @@ -786,7 +719,7 @@ class DeviceRenderPass { queue.record(); } if (this._rasterInfo.pass.showStatistics) { - this._showProfiler(renderArea); + this._showProfiler(renderPassArea); } cmdBuff.endRenderPass(); } @@ -860,21 +793,40 @@ class DeviceRenderPass { class DeviceSceneTransversal extends WebSceneTransversal { protected _currentQueue: DeviceRenderQueue; protected _graphScene: GraphScene; + protected _preSceneTask: DevicePreSceneTask | undefined; + protected _sceneTask: DeviceSceneTask | undefined; + protected _postSceneTask: DevicePostSceneTask | undefined; constructor (quque: DeviceRenderQueue, sceneData: PipelineSceneData, graphSceneData: GraphScene) { const camera = graphSceneData.scene ? graphSceneData.scene.camera : null; super(camera, sceneData, quque.devicePass.context.ubo); this._currentQueue = quque; this._graphScene = graphSceneData; } + set graphScene (graphScene: GraphScene) { + this._graphScene = graphScene; + this._camera = graphScene.scene ? graphScene.scene.camera : null; + if (this._camera) this._scene = this._camera.scene!; + } get graphScene () { return this._graphScene; } public preRenderPass (visitor: WebSceneVisitor): DevicePreSceneTask { - return new DevicePreSceneTask(this._currentQueue, this._graphScene, visitor); + if (!this._preSceneTask) { + this._preSceneTask = new DevicePreSceneTask(this._currentQueue, this._graphScene, visitor); + } + this._preSceneTask.apply(this._currentQueue, this.graphScene); + return this._preSceneTask; } public transverse (visitor: WebSceneVisitor): DeviceSceneTask { - return new DeviceSceneTask(this._currentQueue, this._graphScene, visitor); + if (!this._sceneTask) { + this._sceneTask = new DeviceSceneTask(this._currentQueue, this._graphScene, visitor); + } + this._sceneTask.apply(this._currentQueue, this.graphScene); + return this._sceneTask; } public postRenderPass (visitor: WebSceneVisitor): DevicePostSceneTask { - return new DevicePostSceneTask(this._sceneData, this._currentQueue.devicePass.context.ubo, this._camera, visitor); + if (!this._postSceneTask) { + this._postSceneTask = new DevicePostSceneTask(this._sceneData, this._currentQueue.devicePass.context.ubo, this._camera, visitor); + } + return this._postSceneTask; } } class GraphScene { @@ -882,6 +834,11 @@ class GraphScene { blit: Blit | null = null; dispatch: Dispatch | null = null; sceneID = -1; + init (scene, blit, sceneID) { + this.scene = scene; + this.blit = blit; + this.sceneID = sceneID; + } } class DevicePreSceneTask extends WebSceneTask { protected _currentQueue: DeviceRenderQueue; @@ -894,9 +851,20 @@ class DevicePreSceneTask extends WebSceneTask { graphScene.scene && graphScene.scene.camera ? graphScene.scene.camera : null, visitor); this._currentQueue = queue; this._graphScene = graphScene; - this._renderPass = this._currentQueue.devicePass.renderPass; + this._renderPass = queue.devicePass.renderPass; this._cmdBuff = queue.devicePass.context.commandBuffer; } + apply (queue: DeviceRenderQueue, graphScene: GraphScene) { + this._currentQueue = queue; + this._graphScene = graphScene; + this._renderPass = queue.devicePass.renderPass; + this._cmdBuff = queue.devicePass.context.commandBuffer; + const camera = graphScene.scene && graphScene.scene.camera ? graphScene.scene.camera : null; + if (camera) { + this._scene = camera.scene!; + this._camera = camera; + } + } get graphScene () { return this._graphScene; } public start () { @@ -913,11 +881,13 @@ class DevicePreSceneTask extends WebSceneTask { if (submitMap.has(this.camera)) { this._submitInfo = submitMap.get(this.camera)!; } else { - // culling - if (!this._isShadowMap() || (this._isShadowMap() && this.graphScene.scene!.light.level === 0)) super.start(); this._submitInfo = new SubmitInfo(); submitMap.set(this.camera, this._submitInfo); } + // culling + if ((!this._isShadowMap() || (this._isShadowMap() && this.graphScene.scene!.light.level === 0)) + && !this._submitInfo.isInit) { super.start(); this._submitInfo.isInit = true; } + // shadowmap if (this._isShadowMap()) { assert(this.graphScene.scene!.light.light); @@ -930,7 +900,8 @@ class DevicePreSceneTask extends WebSceneTask { } // reflection probe if (this.graphScene.scene!.flags & SceneFlags.REFLECTION_PROBE && !this._submitInfo.reflectionProbe) { - this._submitInfo.reflectionProbe = new RenderReflectionProbeQueue(this._currentQueue.devicePass.context.pipeline); + this._submitInfo.reflectionProbe = context.pools.addReflectionProbe(); + this._submitInfo.reflectionProbe.clear(); const probes = ReflectionProbeManager.probeManager.getProbes(); for (let i = 0; i < probes.length; i++) { if (probes[i].hasFrameBuffer(this._currentQueue.devicePass.framebuffer)) { @@ -947,6 +918,7 @@ class DevicePreSceneTask extends WebSceneTask { && !this._submitInfo.instances.size && !this._submitInfo.batches.size; if (isEmpty) { + context.pools.resetPassInfo(); for (const ro of this.sceneData.renderObjects) { const subModels = ro.model.subModels; for (const subModel of subModels) { @@ -971,13 +943,12 @@ class DevicePreSceneTask extends WebSceneTask { } this._instancedSort(); } - const pipeline = context.pipeline; if (sceneFlag & SceneFlags.DEFAULT_LIGHTING) { this._submitInfo.additiveLight = context.additiveLight; this._submitInfo.additiveLight.gatherLightPasses(this.camera, this._cmdBuff); } if (sceneFlag & SceneFlags.PLANAR_SHADOW) { - this._submitInfo.planarQueue = new PlanarShadowQueue(pipeline); + this._submitInfo.planarQueue = context.planarShadow; this._submitInfo.planarQueue.gatherShadowPasses(this.camera, this._cmdBuff); } if (sceneFlag & SceneFlags.OPAQUE_OBJECT) { this._submitInfo.opaqueList.sort(this._opaqueCompareFn); } @@ -1008,14 +979,15 @@ class DevicePreSceneTask extends WebSceneTask { const pass = subModel.passes[passIdx]; const shader = subModel.shaders[passIdx]; const currTransparent = pass.blendState.targets[0].blend; - const passId = this._currentQueue.devicePass.passID; + const devicePass = this._currentQueue.devicePass; + const passId = devicePass.passID; const phase = isEnableEffect() ? cclegacy.rendering.getPhaseID(passId, 'default') | cclegacy.rendering.getPhaseID(passId, 'planarShadow') : getPhaseID('default') | getPhaseID('planarShadow'); if (currTransparent !== isTransparent || !(pass.phaseID & (isTransparent ? phase : this._currentQueue.phaseID))) { return; } const hash = (0 << 30) | pass.priority << 16 | subModel.priority << 8 | passIdx; - const rp = new RenderInfo(); + const rp = devicePass.context.pools.addPassInfo(); rp.priority = ro.model.priority; rp.hash = hash; rp.depth = ro.depth || 0; @@ -1042,14 +1014,17 @@ class DevicePreSceneTask extends WebSceneTask { } private _uploadInstanceBuffers () { + if (this._currentQueue.isUploadInstance) return; const it = this._submitInfo!.instances.values(); let res = it.next(); while (!res.done) { if (res.value.hasPendingModels) res.value.uploadBuffers(this._cmdBuff); res = it.next(); } + this._currentQueue.isUploadInstance = true; } private _uploadBatchedBuffers () { + if (this._currentQueue.isUploadBatched) return; const it = this._submitInfo!.batches.values(); let res = it.next(); while (!res.done) { for (let b = 0; b < res.value.batches.length; ++b) { @@ -1063,6 +1038,7 @@ class DevicePreSceneTask extends WebSceneTask { } res = it.next(); } + this._currentQueue.isUploadBatched = true; } private _isShadowMap () { @@ -1099,6 +1075,7 @@ class DevicePreSceneTask extends WebSceneTask { } protected _updateUbo () { + if (this._currentQueue.isUpdateUBO) return; const devicePass = this._currentQueue.devicePass; const context = devicePass.context; const rasterId = devicePass.rasterPassInfo.id; @@ -1115,6 +1092,7 @@ class DevicePreSceneTask extends WebSceneTask { const descSetData = getDescriptorSetDataFromLayout(layoutName); mergeSrcToTargetDesc(descSetData!.descriptorSet, context.pipeline.descriptorSet, true); } + this._currentQueue.isUpdateUBO = true; } public submit () { @@ -1126,12 +1104,12 @@ class DevicePreSceneTask extends WebSceneTask { if (this._isShadowMap()) { return; } - this._uploadInstanceBuffers(); this._uploadBatchedBuffers(); } } +const sceneViewport = new Viewport(); class DeviceSceneTask extends WebSceneTask { protected _currentQueue: DeviceRenderQueue; protected _renderPass: RenderPass; @@ -1143,6 +1121,16 @@ class DeviceSceneTask extends WebSceneTask { this._renderPass = this._currentQueue.devicePass.renderPass; this._graphScene = graphScene; } + apply (queue: DeviceRenderQueue, graphScene: GraphScene) { + this._currentQueue = queue; + this._graphScene = graphScene; + this._renderPass = queue.devicePass.renderPass; + const camera = graphScene.scene && graphScene.scene.camera ? graphScene.scene.camera : null; + if (camera) { + this._scene = camera.scene!; + this._camera = camera; + } + } get graphScene () { return this._graphScene; } public start () {} protected _recordRenderList (isTransparent: boolean) { @@ -1342,7 +1330,11 @@ class DeviceSceneTask extends WebSceneTask { const area = this._isShadowMap() && graphScene.scene && lightInfo!.light ? getRenderArea(this.camera!, texture.width, texture.height, lightInfo!.light, lightInfo!.level) : getRenderArea(this.camera!, texture.width, texture.height); - this.visitor.setViewport(new Viewport(area.x, area.y, area.width, area.height)); + sceneViewport.left = area.x; + sceneViewport.top = area.y; + sceneViewport.width = area.width; + sceneViewport.height = area.height; + this.visitor.setViewport(sceneViewport); this.visitor.setScissor(area); } // Currently processing blit and camera first @@ -1389,6 +1381,222 @@ class DeviceSceneTask extends WebSceneTask { class DevicePostSceneTask extends WebSceneTask {} +class ExecutorPools { + constructor (context: ExecutorContext) { + this.deviceQueuePool = new RecyclePool(() => new DeviceRenderQueue(), 16); + this.graphScenePool = new RecyclePool(() => new GraphScene(), 16); + this.reflectionProbe = new RecyclePool(() => new RenderReflectionProbeQueue(context.pipeline), 8); + this.passPool = new RecyclePool(() => ({ + priority: 0, + hash: 0, + depth: 0, + shaderId: 0, + subModel: null!, + passIdx: 0, + }), 64); + } + addPassInfo () { + return this.passPool.add(); + } + resetPassInfo () { + this.passPool.reset(); + } + addDeviceQueue () { + return this.deviceQueuePool.add(); + } + addGraphScene () { + return this.graphScenePool.add(); + } + addReflectionProbe () { + return this.reflectionProbe.add(); + } + reset () { + this.deviceQueuePool.reset(); + this.graphScenePool.reset(); + this.reflectionProbe.reset(); + } + readonly deviceQueuePool: RecyclePool; + readonly graphScenePool: RecyclePool; + readonly reflectionProbe: RecyclePool; + readonly passPool: RecyclePool; +} + +const vbData = new Float32Array(4 * 4); +const quadRect = new Rect(); +class BlitInfo { + private _pipelineIAData: PipelineInputAssemblerData; + private _context: ExecutorContext; + private _width: number; + private _height: number; + private _lightVolumeBuffer!: Buffer; + private _lightBufferData!: Float32Array; + private _deferredLitsBufView!: Buffer; + private _localUBO!: Buffer; + private _stageDescs: Map = new Map(); + get pipelineIAData () { + return this._pipelineIAData; + } + get deferredLitsBufView () { + return this._deferredLitsBufView; + } + get lightVolumeBuffer () { + return this._lightVolumeBuffer; + } + get lightBufferData () { + return this._lightBufferData; + } + get stageDescs () { + return this._stageDescs; + } + get emptyLocalUBO () { + return this._localUBO; + } + constructor (context: ExecutorContext) { + this._context = context; + 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)); + this._pipelineIAData.quadVB!.update(vb); + this._createLightVolumes(); + this._localUBO = context.device.createBuffer(new BufferInfo( + BufferUsageBit.UNIFORM | BufferUsageBit.TRANSFER_DST, + MemoryUsageBit.DEVICE, + UBOLocal.SIZE, + UBOLocal.SIZE, + )); + } + + resize (width, height) { + if (width !== this._width || height !== this._height) { + quadRect.y = quadRect.x = 0; + quadRect.width = width; + quadRect.height = height; + const vb = this._genQuadVertexData(SurfaceTransform.IDENTITY, quadRect); + this._pipelineIAData.quadVB!.update(vb); + } + } + + private _createLightVolumes () { + const device = this._context.root.device; + let totalSize = Float32Array.BYTES_PER_ELEMENT * 4 * 4 * UBODeferredLight.LIGHTS_PER_PASS; + totalSize = Math.ceil(totalSize / device.capabilities.uboOffsetAlignment) * device.capabilities.uboOffsetAlignment; + + this._lightVolumeBuffer = device.createBuffer(new BufferInfo( + BufferUsageBit.UNIFORM | BufferUsageBit.TRANSFER_DST, + MemoryUsageBit.HOST | MemoryUsageBit.DEVICE, + totalSize, + device.capabilities.uboOffsetAlignment, + )); + + this._deferredLitsBufView = device.createBuffer(new BufferViewInfo(this._lightVolumeBuffer, 0, totalSize)); + this._lightBufferData = new Float32Array(totalSize / Float32Array.BYTES_PER_ELEMENT); + } + + private _genQuadVertexData (surfaceTransform: SurfaceTransform, renderArea: Rect): Float32Array { + const minX = renderArea.x / this._context.width; + const maxX = (renderArea.x + renderArea.width) / this._context.width; + let minY = renderArea.y / this._context.height; + let maxY = (renderArea.y + renderArea.height) / this._context.height; + if (this._context.root.device.capabilities.screenSpaceSignY > 0) { + const temp = maxY; + maxY = minY; + minY = temp; + } + let n = 0; + switch (surfaceTransform) { + case (SurfaceTransform.IDENTITY): + n = 0; + vbData[n++] = -1.0; vbData[n++] = -1.0; vbData[n++] = minX; vbData[n++] = maxY; + vbData[n++] = 1.0; vbData[n++] = -1.0; vbData[n++] = maxX; vbData[n++] = maxY; + vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = minX; vbData[n++] = minY; + vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = maxX; vbData[n++] = minY; + break; + case (SurfaceTransform.ROTATE_90): + n = 0; + vbData[n++] = -1.0; vbData[n++] = -1.0; vbData[n++] = maxX; vbData[n++] = maxY; + vbData[n++] = 1.0; vbData[n++] = -1.0; vbData[n++] = maxX; vbData[n++] = minY; + vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = minX; vbData[n++] = maxY; + vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = minX; vbData[n++] = minY; + break; + case (SurfaceTransform.ROTATE_180): + n = 0; + vbData[n++] = -1.0; vbData[n++] = -1.0; vbData[n++] = minX; vbData[n++] = minY; + vbData[n++] = 1.0; vbData[n++] = -1.0; vbData[n++] = maxX; vbData[n++] = minY; + vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = minX; vbData[n++] = maxY; + vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = maxX; vbData[n++] = maxY; + break; + case (SurfaceTransform.ROTATE_270): + n = 0; + vbData[n++] = -1.0; vbData[n++] = -1.0; vbData[n++] = minX; vbData[n++] = minY; + vbData[n++] = 1.0; vbData[n++] = -1.0; vbData[n++] = minX; vbData[n++] = maxY; + vbData[n++] = -1.0; vbData[n++] = 1.0; vbData[n++] = maxX; vbData[n++] = minY; + vbData[n++] = 1.0; vbData[n++] = 1.0; vbData[n++] = maxX; vbData[n++] = maxY; + break; + default: + break; + } + + return vbData; + } + private _createQuadInputAssembler (): PipelineInputAssemblerData { + // create vertex buffer + const inputAssemblerData = new PipelineInputAssemblerData(); + + const vbStride = Float32Array.BYTES_PER_ELEMENT * 4; + const vbSize = vbStride * 4; + const device = cclegacy.director.root.device; + const quadVB = device.createBuffer(new BufferInfo( + BufferUsageBit.VERTEX | BufferUsageBit.TRANSFER_DST, + MemoryUsageBit.DEVICE | MemoryUsageBit.HOST, + vbSize, + vbStride, + )); + + if (!quadVB) { + return inputAssemblerData; + } + + // create index buffer + const ibStride = Uint8Array.BYTES_PER_ELEMENT; + const ibSize = ibStride * 6; + + const quadIB = device.createBuffer(new BufferInfo( + BufferUsageBit.INDEX | BufferUsageBit.TRANSFER_DST, + MemoryUsageBit.DEVICE, + ibSize, + ibStride, + )); + + if (!quadIB) { + return inputAssemblerData; + } + + const indices = new Uint8Array(6); + indices[0] = 0; indices[1] = 1; indices[2] = 2; + indices[3] = 1; indices[4] = 3; indices[5] = 2; + + quadIB.update(indices); + + // create input assembler + + const attributes = new Array(2); + attributes[0] = new Attribute('a_position', Format.RG32F); + attributes[1] = new Attribute('a_texCoord', Format.RG32F); + + const quadIA = device.createInputAssembler(new InputAssemblerInfo( + attributes, + [quadVB], + quadIB, + )); + + inputAssemblerData.quadIB = quadIB; + inputAssemblerData.quadVB = quadVB; + inputAssemblerData.quadIA = quadIA; + return inputAssemblerData; + } +} + class ExecutorContext { constructor (pipeline: Pipeline, ubo: PipelineUBO, @@ -1411,6 +1619,20 @@ class ExecutorContext { this.height = height; this.additiveLight = new RenderAdditiveLightQueue(pipeline); this.shadowMapBatched = new RenderShadowMapBatchedQueue(pipeline); + this.planarShadow = new PlanarShadowQueue(pipeline); + this.pools = new ExecutorPools(this); + this.blit = new BlitInfo(this); + } + reset () { + this.pools.reset(); + for (const info of this.submitMap) { + info[1].reset(); + } + } + resize (width: number, height: number) { + this.width = width; + this.height = height; + this.blit.resize(width, height); } readonly device: Device; readonly pipeline: Pipeline; @@ -1422,12 +1644,15 @@ class ExecutorContext { readonly layoutGraph: LayoutGraphData; readonly root: Root; readonly ubo: PipelineUBO; - readonly width: number; - readonly height: number; readonly additiveLight: RenderAdditiveLightQueue; readonly shadowMapBatched: RenderShadowMapBatchedQueue; + readonly planarShadow: PlanarShadowQueue; readonly submitMap: Map = new Map(); + readonly pools: ExecutorPools; + readonly blit: BlitInfo; renderGraph: RenderGraph; + width: number; + height: number; } class ResourceVisitor implements ResourceGraphVisitor { private _context: ExecutorContext; @@ -1481,13 +1706,17 @@ export class Executor { ); } + resize (width: number, height: number) { + this._context.resize(width, height); + } + execute (rg: RenderGraph) { this._context.renderGraph = rg; - this._context.submitMap.clear(); + this._context.reset(); const cmdBuff = this._context.commandBuffer; cmdBuff.begin(); - const visitor = new RenderVisitor(this._context); - depthFirstSearch(visitor.graphView, visitor, visitor.colorMap); + if (!this._visitor) this._visitor = new RenderVisitor(this._context); + depthFirstSearch(this._visitor.graphView, this._visitor, this._visitor.colorMap); cmdBuff.end(); this._context.device.queue.submit([cmdBuff]); } @@ -1500,14 +1729,15 @@ export class Executor { this._context.deviceTextures.clear(); } readonly _context: ExecutorContext; + private _visitor: RenderVisitor | undefined; } class BaseRenderVisitor { public queueID = 0xFFFFFFFF; public sceneID = 0xFFFFFFFF; public passID = 0xFFFFFFFF; - public currPass; - public currQueue; + public currPass: DeviceRenderPass | undefined; + public currQueue: DeviceRenderQueue | undefined; public context: ExecutorContext; public rg: RenderGraph; constructor (context: ExecutorContext) { @@ -1559,7 +1789,7 @@ class PreRenderVisitor extends BaseRenderVisitor implements RenderGraphVisitor { const passHash = pass.versionName; this.currPass = devicePasses.get(passHash); const currRasterPass = this.currPass ? this.currPass.rasterPassInfo.pass : null; - if (!this.currPass || currRasterPass.version !== pass.version) { + if (!this.currPass || currRasterPass!.version !== pass.version) { this.currPass = new DeviceRenderPass(this.context, new RasterPassInfo(this.passID, pass)); devicePasses.set(passHash, this.currPass); } else { @@ -1574,32 +1804,30 @@ class PreRenderVisitor extends BaseRenderVisitor implements RenderGraphVisitor { raytrace (value: RaytracePass) {} queue (value: RenderQueue) { if (!this.rg.getValid(this.queueID)) return; - const deviceQueue = new DeviceRenderQueue(this.currPass); - deviceQueue.queueHint = value.hint; - deviceQueue.queueId = this.queueID; + const deviceQueue = this.context.pools.addDeviceQueue(); + deviceQueue.init(this.currPass!, value.hint, this.queueID); this.currQueue = deviceQueue; - this.currPass.addQueue(deviceQueue); + this.currPass!.addQueue(deviceQueue); const layoutName = this.rg.getLayout(this.queueID); if (layoutName) { const layoutGraph = this.context.layoutGraph; - if (this.currPass.renderLayout) { - const layoutId = layoutGraph.locateChild(this.currPass.renderLayout.layoutID, layoutName); + if (this.currPass!.renderLayout) { + const layoutId = layoutGraph.locateChild(this.currPass!.renderLayout.layoutID, layoutName); deviceQueue.renderPhase = layoutGraph.tryGetRenderPhase(layoutId); } } } scene (value: SceneData) { if (!this.rg.getValid(this.sceneID)) return; - const graphScene = new GraphScene(); - graphScene.scene = value; - graphScene.sceneID = this.sceneID; - this.currQueue.addSceneTask(graphScene); + const graphScene = this.context.pools.addGraphScene(); + graphScene.init(value, null, this.sceneID); + this.currQueue!.addSceneTask(graphScene); } blit (value: Blit) { if (!this.rg.getValid(this.sceneID)) return; - const graphScene = new GraphScene(); - graphScene.blit = value; - this.currQueue.addSceneTask(graphScene); + const graphScene = this.context.pools.addGraphScene(); + graphScene.init(null, value, -1); + this.currQueue!.addSceneTask(graphScene); } dispatch (value: Dispatch) {} } diff --git a/cocos/rendering/custom/pipeline.ts b/cocos/rendering/custom/pipeline.ts index f41b06377d..c9694afdff 100644 --- a/cocos/rendering/custom/pipeline.ts +++ b/cocos/rendering/custom/pipeline.ts @@ -100,20 +100,24 @@ export interface RasterQueueBuilder extends Setter { export interface RasterPassBuilder extends Setter { addRasterView (name: string, view: RasterView): void; addComputeView (name: string, view: ComputeView): void; - addQueue (hint: QueueHint): RasterQueueBuilder; - addQueue (/*QueueHint.NONE*/): RasterQueueBuilder; + addQueue (hint: QueueHint, layoutName: string): RasterQueueBuilder; + addQueue (hint: QueueHint/*, ''*/): RasterQueueBuilder; + addQueue (/*QueueHint.NONE, ''*/): RasterQueueBuilder; setViewport (viewport: Viewport): void; setVersion (name: string, version: number): void; showStatistics: boolean; } export interface ComputeQueueBuilder extends Setter { - addDispatch (shader: string, threadGroupCountX: number, threadGroupCountY: number, threadGroupCountZ: number): void; + addDispatch (threadGroupCountX: number, threadGroupCountY: number, threadGroupCountZ: number, material: Material, passID: number): void; + addDispatch (threadGroupCountX: number, threadGroupCountY: number, threadGroupCountZ: number, material: Material/*, 0*/): void; + addDispatch (threadGroupCountX: number, threadGroupCountY: number, threadGroupCountZ: number/*, null, 0*/): void; } export interface ComputePassBuilder extends Setter { addComputeView (name: string, view: ComputeView): void; - addQueue (): ComputeQueueBuilder; + addQueue (layoutName: string): ComputeQueueBuilder; + addQueue (/*''*/): ComputeQueueBuilder; } export interface MovePassBuilder extends RenderNode { diff --git a/cocos/rendering/custom/private.ts b/cocos/rendering/custom/private.ts index 74e285938f..5bd95de62a 100644 --- a/cocos/rendering/custom/private.ts +++ b/cocos/rendering/custom/private.ts @@ -29,7 +29,7 @@ */ /* eslint-disable max-len */ import { EffectAsset } from '../../asset/assets'; -import { DescriptorSetLayout, Device, PipelineLayout, Shader, ShaderInfo } from '../../gfx'; +import { DescriptorSetLayout, Device, PipelineLayout, PipelineState, Shader, ShaderInfo } from '../../gfx'; import { MacroRecord } from '../../render-scene/core/pass-utils'; import { IProgramInfo } from '../../render-scene/core/program-lib'; diff --git a/cocos/rendering/custom/render-graph.ts b/cocos/rendering/custom/render-graph.ts index 8def7abf44..9caac774d4 100644 --- a/cocos/rendering/custom/render-graph.ts +++ b/cocos/rendering/custom/render-graph.ts @@ -1072,10 +1072,12 @@ export class ClearView { } export class RenderQueue { - constructor (hint: QueueHint = QueueHint.RENDER_OPAQUE) { + constructor (hint: QueueHint = QueueHint.RENDER_OPAQUE, phaseID = 0xFFFFFFFF) { this.hint = hint; + this.phaseID = phaseID; } hint: QueueHint; + phaseID: number; } export class SceneData { @@ -1092,13 +1094,21 @@ export class SceneData { } export class Dispatch { - constructor (shader = '', threadGroupCountX = 0, threadGroupCountY = 0, threadGroupCountZ = 0) { - this.shader = shader; + constructor ( + material: Material | null, + passID: number, + threadGroupCountX: number, + threadGroupCountY: number, + threadGroupCountZ: number, + ) { + this.material = material; + this.passID = passID; this.threadGroupCountX = threadGroupCountX; this.threadGroupCountY = threadGroupCountY; this.threadGroupCountZ = threadGroupCountZ; } - shader: string; + /*refcount*/ material: Material | null; + passID: number; threadGroupCountX: number; threadGroupCountY: number; threadGroupCountZ: number; diff --git a/cocos/rendering/custom/web-pipeline.ts b/cocos/rendering/custom/web-pipeline.ts index c828a1faf4..c43b64b2b5 100644 --- a/cocos/rendering/custom/web-pipeline.ts +++ b/cocos/rendering/custom/web-pipeline.ts @@ -749,14 +749,17 @@ export class WebComputeQueueBuilder extends WebSetter implements ComputeQueueBui set name (name: string) { this._renderGraph.setName(this._vertID, name); } - addDispatch (shader: string, + addDispatch ( threadGroupCountX: number, threadGroupCountY: number, threadGroupCountZ: number, - name = 'Dispatch') { + material: Material | null = null, + passID = 0, + name = 'Dispatch', + ) { this._renderGraph.addVertex( RenderGraphValue.Dispatch, - new Dispatch(shader, threadGroupCountX, threadGroupCountY, threadGroupCountZ), + new Dispatch(material, passID, threadGroupCountX, threadGroupCountY, threadGroupCountZ), name, '', new RenderData(), false, this._vertID, ); } @@ -801,17 +804,6 @@ export class WebComputePassBuilder extends WebSetter implements ComputePassBuild ); return new WebComputeQueueBuilder(data, this._renderGraph, this._layoutGraph, queueID, queue, this._pipeline); } - addDispatch (shader: string, - threadGroupCountX: number, - threadGroupCountY: number, - threadGroupCountZ: number, - name = 'Dispatch') { - this._renderGraph.addVertex( - RenderGraphValue.Dispatch, - new Dispatch(shader, threadGroupCountX, threadGroupCountY, threadGroupCountZ), - name, '', new RenderData(), false, this._vertID, - ); - } private readonly _renderGraph: RenderGraph; private readonly _layoutGraph: LayoutGraphData; private readonly _vertID: number; @@ -1090,7 +1082,8 @@ export class WebPipeline implements Pipeline { // do nothing } beginSetup (): void { - this._renderGraph = new RenderGraph(); + if (!this._renderGraph) this._renderGraph = new RenderGraph(); + // this.renderGraph!.clear(); } endSetup (): void { this.compile(); @@ -1168,7 +1161,8 @@ export class WebPipeline implements Pipeline { // noop } endFrame () { - this._renderGraph = null; + // this._renderGraph = null; + this.renderGraph?.clear(); } compile () { @@ -1189,6 +1183,7 @@ export class WebPipeline implements Pipeline { this._executor = new Executor(this, this._pipelineUBO, this._device, this._resourceGraph, this.layoutGraph, this.width, this.height); } + this._executor.resize(this.width, this.height); this._executor.execute(this._renderGraph); } protected _applySize (cameras: Camera[]) { diff --git a/cocos/rendering/custom/web-scene.ts b/cocos/rendering/custom/web-scene.ts index 026c26e51d..13327c7100 100644 --- a/cocos/rendering/custom/web-scene.ts +++ b/cocos/rendering/custom/web-scene.ts @@ -151,8 +151,8 @@ export class WebSceneTask implements SceneTask { get visitor () { return this._visitor; } get dirLightFrustum () { return this._dirLightFrustum; } get sceneData (): PipelineSceneData { return this._sceneData; } - private _scene: RenderScene | null = null; - private _camera: Camera | null = null; + protected _scene: RenderScene | null = null; + protected _camera: Camera | null = null; private _visitor: SceneVisitor; private _sceneData: PipelineSceneData; private _dirLightFrustum = new Frustum(); diff --git a/cocos/rendering/define.ts b/cocos/rendering/define.ts index ef383001b0..b9959ef430 100644 --- a/cocos/rendering/define.ts +++ b/cocos/rendering/define.ts @@ -161,14 +161,14 @@ export enum SetIndex { } // parameters passed to GFX Device export const bindingMappingInfo = new BindingMappingInfo( - [GLOBAL_UBO_COUNT, 0, LOCAL_UBO_COUNT], // Uniform Buffer Counts - [GLOBAL_SAMPLER_COUNT, 0, LOCAL_SAMPLER_COUNT], // Combined Sampler Texture Counts - [0, 0, 0], // Sampler Counts - [0, 0, 0], // Texture Counts - [0, 0, 0], // Storage Buffer Counts - [0, 0, LOCAL_STORAGE_IMAGE_COUNT], // Storage Image Counts - [0, 0, 0], // Subpass Input Counts - [0, 2, 1], // Set Order Indices + [GLOBAL_UBO_COUNT, 0, LOCAL_UBO_COUNT, 0], // Uniform Buffer Counts + [GLOBAL_SAMPLER_COUNT, 0, LOCAL_SAMPLER_COUNT, 0], // Combined Sampler Texture Counts + [0, 0, 0, 0], // Sampler Counts + [0, 0, 0, 0], // Texture Counts + [0, 0, 0, 0], // Storage Buffer Counts + [0, 0, LOCAL_STORAGE_IMAGE_COUNT, 0], // Storage Image Counts + [0, 0, 0, 0], // Subpass Input Counts + [0, 2, 1, 3], // Set Order Indices ); /** diff --git a/native/cocos/bindings/auto/jsb_assets_auto.cpp b/native/cocos/bindings/auto/jsb_assets_auto.cpp index eb83881000..ec872bbdd7 100644 --- a/native/cocos/bindings/auto/jsb_assets_auto.cpp +++ b/native/cocos/bindings/auto/jsb_assets_auto.cpp @@ -16070,6 +16070,43 @@ static bool js_cc_IShaderSource_frag_get(se::State& s) } SE_BIND_PROP_GET(js_cc_IShaderSource_frag_get) +static bool js_cc_IShaderSource_compute_set(se::State& s) +{ + CC_UNUSED bool ok = true; + const auto& args = s.args(); + size_t argc = args.size(); + cc::IShaderSource *arg1 = (cc::IShaderSource *) NULL ; + + arg1 = SE_THIS_OBJECT(s); + if (nullptr == arg1) return true; + + ok &= sevalue_to_native(args[0], &arg1->compute, s.thisObject()); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + + + + return true; +} +SE_BIND_PROP_SET(js_cc_IShaderSource_compute_set) + +static bool js_cc_IShaderSource_compute_get(se::State& s) +{ + CC_UNUSED bool ok = true; + cc::IShaderSource *arg1 = (cc::IShaderSource *) NULL ; + + arg1 = SE_THIS_OBJECT(s); + if (nullptr == arg1) return true; + + ok &= nativevalue_to_se(arg1->compute, s.rval(), s.thisObject() /*ctx*/); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + SE_HOLD_RETURN_VALUE(arg1->compute, s.thisObject(), s.rval()); + + + + return true; +} +SE_BIND_PROP_GET(js_cc_IShaderSource_compute_get) + static bool js_new_cc_IShaderSource(se::State& s) // NOLINT(readability-identifier-naming) { CC_UNUSED bool ok = true; @@ -16117,6 +16154,12 @@ bool sevalue_to_native(const se::Value &from, cc::IShaderSource * to, se::Object } + json->getProperty("compute", &field, true); + if (!field.isNullOrUndefined()) { + ok &= sevalue_to_native(field, &(to->compute), ctx); + } + + return ok; } @@ -16127,6 +16170,7 @@ bool js_register_cc_IShaderSource(se::Object* obj) { cls->defineStaticProperty("__isJSB", se::Value(true), se::PropertyAttribute::READ_ONLY | se::PropertyAttribute::DONT_ENUM | se::PropertyAttribute::DONT_DELETE); cls->defineProperty("vert", _SE(js_cc_IShaderSource_vert_get), _SE(js_cc_IShaderSource_vert_set)); cls->defineProperty("frag", _SE(js_cc_IShaderSource_frag_get), _SE(js_cc_IShaderSource_frag_set)); + cls->defineProperty("compute", _SE(js_cc_IShaderSource_compute_get), _SE(js_cc_IShaderSource_compute_set)); diff --git a/native/cocos/bindings/auto/jsb_render_auto.cpp b/native/cocos/bindings/auto/jsb_render_auto.cpp index 7c977def91..a47579cbb8 100644 --- a/native/cocos/bindings/auto/jsb_render_auto.cpp +++ b/native/cocos/bindings/auto/jsb_render_auto.cpp @@ -1698,6 +1698,8 @@ static bool js_cc_render_RasterPassBuilder_addQueue__SWIG_0(se::State& s) const auto& args = s.args(); cc::render::RasterPassBuilder *arg1 = (cc::render::RasterPassBuilder *) NULL ; QueueHint arg2 ; + ccstd::string *arg3 = 0 ; + ccstd::string temp3 ; cc::render::RasterQueueBuilder *result = 0 ; arg1 = SE_THIS_OBJECT(s); @@ -1706,7 +1708,12 @@ static bool js_cc_render_RasterPassBuilder_addQueue__SWIG_0(se::State& s) ok &= sevalue_to_native(args[0], &arg2, s.thisObject()); SE_PRECONDITION2(ok, false, "Error processing arguments"); - result = (cc::render::RasterQueueBuilder *)(arg1)->addQueue(arg2); + + ok &= sevalue_to_native(args[1], &temp3, s.thisObject()); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + arg3 = &temp3; + + result = (cc::render::RasterQueueBuilder *)(arg1)->addQueue(arg2,(ccstd::string const &)*arg3); ok &= nativevalue_to_se(result, s.rval(), s.thisObject()); SE_PRECONDITION2(ok, false, "Error processing arguments"); @@ -1796,6 +1803,31 @@ static bool js_cc_render_RasterPassBuilder_addQueue__SWIG_1(se::State& s) return true; } +static bool js_cc_render_RasterPassBuilder_addQueue__SWIG_2(se::State& s) +{ + CC_UNUSED bool ok = true; + const auto& args = s.args(); + cc::render::RasterPassBuilder *arg1 = (cc::render::RasterPassBuilder *) NULL ; + QueueHint arg2 ; + cc::render::RasterQueueBuilder *result = 0 ; + + arg1 = SE_THIS_OBJECT(s); + if (nullptr == arg1) return true; + + ok &= sevalue_to_native(args[0], &arg2, s.thisObject()); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + + result = (cc::render::RasterQueueBuilder *)(arg1)->addQueue(arg2); + + ok &= nativevalue_to_se(result, s.rval(), s.thisObject()); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + SE_HOLD_RETURN_VALUE(result, s.thisObject(), s.rval()); + s.rval().toObject()->getPrivateObject()->tryAllowDestroyInGC(); + + + return true; +} + static bool js_cc_render_RasterPassBuilder_addQueue(se::State& s) { CC_UNUSED bool ok = true; @@ -1803,7 +1835,7 @@ static bool js_cc_render_RasterPassBuilder_addQueue(se::State& s) size_t argc = args.size(); - if (argc == 1) { + if (argc == 2) { ok = js_cc_render_RasterPassBuilder_addQueue__SWIG_0(s); if (ok) { return true; @@ -1815,6 +1847,12 @@ static bool js_cc_render_RasterPassBuilder_addQueue(se::State& s) return true; } } + if (argc == 1) { + ok = js_cc_render_RasterPassBuilder_addQueue__SWIG_2(s); + if (ok) { + return true; + } + } SE_REPORT_ERROR("wrong number of arguments: %d", (int)argc); return false; } @@ -1895,28 +1933,22 @@ se::Class* __jsb_cc_render_ComputeQueueBuilder_class = nullptr; se::Object* __jsb_cc_render_ComputeQueueBuilder_proto = nullptr; SE_DECLARE_FINALIZE_FUNC(js_delete_cc_render_ComputeQueueBuilder) -static bool js_cc_render_ComputeQueueBuilder_addDispatch(se::State& s) +static bool js_cc_render_ComputeQueueBuilder_addDispatch__SWIG_0(se::State& s) { CC_UNUSED bool ok = true; const auto& args = s.args(); - size_t argc = args.size(); cc::render::ComputeQueueBuilder *arg1 = (cc::render::ComputeQueueBuilder *) NULL ; - ccstd::string *arg2 = 0 ; + uint32_t arg2 ; uint32_t arg3 ; uint32_t arg4 ; - uint32_t arg5 ; - ccstd::string temp2 ; + Material *arg5 = (Material *) NULL ; + uint32_t arg6 ; - if(argc != 4) { - SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 4); - return false; - } arg1 = SE_THIS_OBJECT(s); if (nullptr == arg1) return true; - ok &= sevalue_to_native(args[0], &temp2, s.thisObject()); + ok &= sevalue_to_native(args[0], &arg2, s.thisObject()); SE_PRECONDITION2(ok, false, "Error processing arguments"); - arg2 = &temp2; ok &= sevalue_to_native(args[1], &arg3, s.thisObject()); @@ -1928,13 +1960,107 @@ static bool js_cc_render_ComputeQueueBuilder_addDispatch(se::State& s) ok &= sevalue_to_native(args[3], &arg5, s.thisObject()); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + + ok &= sevalue_to_native(args[4], &arg6, s.thisObject()); SE_PRECONDITION2(ok, false, "Error processing arguments"); - (arg1)->addDispatch((ccstd::string const &)*arg2,arg3,arg4,arg5); + (arg1)->addDispatch(arg2,arg3,arg4,arg5,arg6); return true; } + +static bool js_cc_render_ComputeQueueBuilder_addDispatch__SWIG_1(se::State& s) +{ + CC_UNUSED bool ok = true; + const auto& args = s.args(); + cc::render::ComputeQueueBuilder *arg1 = (cc::render::ComputeQueueBuilder *) NULL ; + uint32_t arg2 ; + uint32_t arg3 ; + uint32_t arg4 ; + + arg1 = SE_THIS_OBJECT(s); + if (nullptr == arg1) return true; + + ok &= sevalue_to_native(args[0], &arg2, s.thisObject()); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + + + ok &= sevalue_to_native(args[1], &arg3, s.thisObject()); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + + + ok &= sevalue_to_native(args[2], &arg4, s.thisObject()); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + + (arg1)->addDispatch(arg2,arg3,arg4); + + + return true; +} + +static bool js_cc_render_ComputeQueueBuilder_addDispatch__SWIG_2(se::State& s) +{ + CC_UNUSED bool ok = true; + const auto& args = s.args(); + cc::render::ComputeQueueBuilder *arg1 = (cc::render::ComputeQueueBuilder *) NULL ; + uint32_t arg2 ; + uint32_t arg3 ; + uint32_t arg4 ; + Material *arg5 = (Material *) NULL ; + + arg1 = SE_THIS_OBJECT(s); + if (nullptr == arg1) return true; + + ok &= sevalue_to_native(args[0], &arg2, s.thisObject()); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + + + ok &= sevalue_to_native(args[1], &arg3, s.thisObject()); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + + + ok &= sevalue_to_native(args[2], &arg4, s.thisObject()); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + + + ok &= sevalue_to_native(args[3], &arg5, s.thisObject()); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + (arg1)->addDispatch(arg2,arg3,arg4,arg5); + + + return true; +} + +static bool js_cc_render_ComputeQueueBuilder_addDispatch(se::State& s) +{ + CC_UNUSED bool ok = true; + const auto& args = s.args(); + size_t argc = args.size(); + + + if (argc == 5) { + ok = js_cc_render_ComputeQueueBuilder_addDispatch__SWIG_0(s); + if (ok) { + return true; + } + } + if (argc == 3) { + ok = js_cc_render_ComputeQueueBuilder_addDispatch__SWIG_1(s); + if (ok) { + return true; + } + } + if (argc == 4) { + ok = js_cc_render_ComputeQueueBuilder_addDispatch__SWIG_2(s); + if (ok) { + return true; + } + } + SE_REPORT_ERROR("wrong number of arguments: %d", (int)argc); + return false; +} SE_BIND_FUNC(js_cc_render_ComputeQueueBuilder_addDispatch) static bool js_delete_cc_render_ComputeQueueBuilder(se::State& s) @@ -2004,18 +2130,39 @@ static bool js_cc_render_ComputePassBuilder_addComputeView(se::State& s) } SE_BIND_FUNC(js_cc_render_ComputePassBuilder_addComputeView) -static bool js_cc_render_ComputePassBuilder_addQueue(se::State& s) +static bool js_cc_render_ComputePassBuilder_addQueue__SWIG_0(se::State& s) +{ + CC_UNUSED bool ok = true; + const auto& args = s.args(); + cc::render::ComputePassBuilder *arg1 = (cc::render::ComputePassBuilder *) NULL ; + ccstd::string *arg2 = 0 ; + ccstd::string temp2 ; + cc::render::ComputeQueueBuilder *result = 0 ; + + arg1 = SE_THIS_OBJECT(s); + if (nullptr == arg1) return true; + + ok &= sevalue_to_native(args[0], &temp2, s.thisObject()); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + arg2 = &temp2; + + result = (cc::render::ComputeQueueBuilder *)(arg1)->addQueue((ccstd::string const &)*arg2); + + ok &= nativevalue_to_se(result, s.rval(), s.thisObject()); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + SE_HOLD_RETURN_VALUE(result, s.thisObject(), s.rval()); + + + return true; +} + +static bool js_cc_render_ComputePassBuilder_addQueue__SWIG_1(se::State& s) { CC_UNUSED bool ok = true; const auto& args = s.args(); - size_t argc = args.size(); cc::render::ComputePassBuilder *arg1 = (cc::render::ComputePassBuilder *) NULL ; cc::render::ComputeQueueBuilder *result = 0 ; - if(argc != 0) { - SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 0); - return false; - } arg1 = SE_THIS_OBJECT(s); if (nullptr == arg1) return true; result = (cc::render::ComputeQueueBuilder *)(arg1)->addQueue(); @@ -2027,6 +2174,29 @@ static bool js_cc_render_ComputePassBuilder_addQueue(se::State& s) return true; } + +static bool js_cc_render_ComputePassBuilder_addQueue(se::State& s) +{ + CC_UNUSED bool ok = true; + const auto& args = s.args(); + size_t argc = args.size(); + + + if (argc == 1) { + ok = js_cc_render_ComputePassBuilder_addQueue__SWIG_0(s); + if (ok) { + return true; + } + } + if (argc == 0) { + ok = js_cc_render_ComputePassBuilder_addQueue__SWIG_1(s); + if (ok) { + return true; + } + } + SE_REPORT_ERROR("wrong number of arguments: %d", (int)argc); + return false; +} SE_BIND_FUNC(js_cc_render_ComputePassBuilder_addQueue) static bool js_delete_cc_render_ComputePassBuilder(se::State& s) diff --git a/native/cocos/core/assets/EffectAsset.h b/native/cocos/core/assets/EffectAsset.h index ac0bf92436..23c560867f 100644 --- a/native/cocos/core/assets/EffectAsset.h +++ b/native/cocos/core/assets/EffectAsset.h @@ -518,6 +518,7 @@ struct IDescriptorInfo { struct IShaderSource { ccstd::string vert; ccstd::string frag; + ccstd::optional compute; }; struct IShaderInfo { diff --git a/native/cocos/renderer/pipeline/Define.cpp b/native/cocos/renderer/pipeline/Define.cpp index 70319173c9..390ee790ee 100644 --- a/native/cocos/renderer/pipeline/Define.cpp +++ b/native/cocos/renderer/pipeline/Define.cpp @@ -42,14 +42,14 @@ uint32_t materialSet = static_cast(SetIndex::MATERIAL); uint32_t localSet = static_cast(SetIndex::LOCAL); gfx::BindingMappingInfo bindingMappingInfo = { - {globalUBOCount, 0, localUBOCount}, // Uniform Buffer Counts - {globalSamplerCount, 0, localSamplerCount}, // Combined Sampler Texture Counts - {0, 0, 0}, // Sampler Counts - {0, 0, 0}, // Texture Counts - {0, 0, 0}, // Storage Buffer Counts - {0, 0, localStorageImageCount}, // Storage Image Counts - {0, 0, 0}, // Subpass Input Counts - {0, 2, 1}, // Set Order Indices + {globalUBOCount, 0, localUBOCount, 0}, // Uniform Buffer Counts + {globalSamplerCount, 0, localSamplerCount, 0}, // Combined Sampler Texture Counts + {0, 0, 0, 0}, // Sampler Counts + {0, 0, 0, 0}, // Texture Counts + {0, 0, 0, 0}, // Storage Buffer Counts + {0, 0, localStorageImageCount, 0}, // Storage Image Counts + {0, 0, 0, 0}, // Subpass Input Counts + {0, 2, 1, 3}, // Set Order Indices }; DescriptorSetLayoutInfos globalDescriptorSetLayout; diff --git a/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp b/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp index 8ade6b5b74..8d2d21c40a 100644 --- a/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp +++ b/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp @@ -80,13 +80,13 @@ struct RenderGraphVisitorContext { PipelineRuntime* ppl = nullptr; ccstd::pmr::unordered_map< RenderGraph::vertex_descriptor, - gfx::DescriptorSet*>& renderGraphPerPassDescriptorSets; + gfx::DescriptorSet*>& renderGraphDescriptorSet; ccstd::pmr::unordered_map< RenderGraph::vertex_descriptor, gfx::DescriptorSet*>& profilerPerPassDescriptorSets; ccstd::pmr::unordered_map< RenderGraph::vertex_descriptor, - gfx::DescriptorSet*>& blitPerInstanceDescriptorSets; + gfx::DescriptorSet*>& perInstanceDescriptorSets; ProgramLibrary* programLib = nullptr; boost::container::pmr::memory_resource* scratch = nullptr; gfx::RenderPass* currentPass = nullptr; @@ -478,7 +478,7 @@ void uploadUniformBuffer( passSet->bindBuffer(bindID, buffer); } -gfx::DescriptorSet* initPerPassDescriptorSet( +gfx::DescriptorSet* initDescriptorSet( ResourceGraph& resg, gfx::Device* device, gfx::CommandBuffer* cmdBuff, @@ -491,7 +491,7 @@ gfx::DescriptorSet* initPerPassDescriptorSet( // update per pass resources const auto& data = set.descriptorSetLayoutData; - gfx::DescriptorSet* passSet = node.descriptorSetPool.allocateDescriptorSet(); + gfx::DescriptorSet* newSet = node.descriptorSetPool.allocateDescriptorSet(); for (const auto& block : data.descriptorBlocks) { CC_EXPECTS(block.descriptors.size() == block.capacity); auto bindID = block.offset; @@ -506,7 +506,7 @@ gfx::DescriptorSet* initPerPassDescriptorSet( CC_ENSURES(resource.bufferPool.bufferSize == resource.cpuBuffer.size()); // upload gfx buffer - uploadUniformBuffer(passSet, bindID, resource, cmdBuff); + uploadUniformBuffer(newSet, bindID, resource, cmdBuff); // increase slot // TODO(zhouzhenglong): here binding will be refactored in the future @@ -521,7 +521,7 @@ gfx::DescriptorSet* initPerPassDescriptorSet( CC_EXPECTS(false); break; case DescriptorTypeOrder::SAMPLER_TEXTURE: { - CC_EXPECTS(passSet); + CC_EXPECTS(newSet); for (const auto& d : block.descriptors) { CC_EXPECTS(d.count == 1); CC_EXPECTS(d.type >= gfx::Type::SAMPLER1D && @@ -532,12 +532,12 @@ gfx::DescriptorSet* initPerPassDescriptorSet( // render graph textures auto* texture = resg.getTexture(iter->second); CC_ENSURES(texture); - passSet->bindTexture(bindID, texture); + newSet->bindTexture(bindID, texture); } else { // user provided textures auto iter = user.textures.find(d.descriptorID.value); if (iter != user.textures.end()) { - passSet->bindTexture(bindID, iter->second.get()); + newSet->bindTexture(bindID, iter->second.get()); } else { // default textures gfx::TextureType type{}; @@ -563,7 +563,7 @@ gfx::DescriptorSet* initPerPassDescriptorSet( default: break; } - passSet->bindTexture(bindID, defaultResource.getTexture(type)); + newSet->bindTexture(bindID, defaultResource.getTexture(type)); } } bindID += d.count; @@ -575,11 +575,11 @@ gfx::DescriptorSet* initPerPassDescriptorSet( CC_EXPECTS(d.count == 1); auto iter = user.samplers.find(d.descriptorID.value); if (iter != user.samplers.end()) { - passSet->bindSampler(bindID, iter->second.get()); + newSet->bindSampler(bindID, iter->second.get()); } else { gfx::SamplerInfo info{}; auto* sampler = device->getSampler(info); - passSet->bindSampler(bindID, sampler); + newSet->bindSampler(bindID, sampler); } bindID += d.count; } @@ -609,9 +609,9 @@ gfx::DescriptorSet* initPerPassDescriptorSet( break; } } - passSet->update(); + newSet->update(); - return passSet; + return newSet; } gfx::DescriptorSet* updatePerPassDescriptorSet( @@ -799,13 +799,124 @@ void submitProfilerCommands( cmdBuff->draw(ia); } +PmrFlatMap +buildResourceIndex( + const ResourceGraph& resg, + const LayoutGraphData& lg, + const PmrTransparentMap>& computeViews, + boost::container::pmr::memory_resource* scratch) { + PmrFlatMap resourceIndex(scratch); + resourceIndex.reserve(computeViews.size() * 2); + for (const auto& [resName, computeViews] : computeViews) { + const auto resID = vertex(resName, resg); + for (const auto& computeView : computeViews) { + const auto& name = computeView.name; + CC_EXPECTS(!name.empty()); + const auto nameID = lg.attributeIndex.at(name); + resourceIndex.emplace(nameID, resID); + } + } + return resourceIndex; +} + +const PmrTransparentMap>& +getComputeViews(RenderGraph::vertex_descriptor passID, const RenderGraph& rg) { + if (holds(passID, rg)) { + return get(RasterTag{}, passID, rg).computeViews; + } + CC_EXPECTS(holds(passID, rg)); + return get(ComputeTag{}, passID, rg).computeViews; +} + struct RenderGraphUploadVisitor : boost::dfs_visitor<> { void updateAndCreatePerPassDescriptorSet(RenderGraph::vertex_descriptor vertID) const { auto* perPassSet = updateCameraUniformBufferAndDescriptorSet(ctx, vertID); if (perPassSet) { - ctx.renderGraphPerPassDescriptorSets[vertID] = perPassSet; + ctx.renderGraphDescriptorSet[vertID] = perPassSet; } } + void uploadBlitOrDispatchUniformBlokcs( + RenderGraph::vertex_descriptor vertID, + scene::Pass& pass) const { + pass.update(); + + // get shader + auto& shader = *pass.getShaderVariant(); + // update material ubo and descriptor set + // get or create program per-instance descriptor set + auto& node = ctx.context.layoutGraphResources.at(pass.getPhaseID()); + auto iter = node.programResources.find(std::string_view{shader.getName()}); + if (iter == node.programResources.end()) { + // make program resource + auto res = node.programResources.emplace( + std::piecewise_construct, + std::forward_as_tuple(shader.getName()), + std::forward_as_tuple()); + CC_ENSURES(res.second); + iter = res.first; + auto& instance = res.first->second; + + // make per-instance layout + IntrusivePtr instanceSetLayout = + &const_cast( + ctx.programLib->getLocalDescriptorSetLayout( + ctx.device, pass.getPhaseID(), shader.getName())); + + // init per-instance descriptor set pool + instance.descriptorSetPool.init(ctx.device, std::move(instanceSetLayout)); + + for (const auto& block : shader.getBlocks()) { + if (static_cast(block.set) != pipeline::SetIndex::LOCAL) { + continue; + } + const auto& name = block.name; + auto iter = ctx.lg.attributeIndex.find(std::string_view{name}); + CC_EXPECTS(iter != ctx.lg.attributeIndex.end()); + const auto attrID = iter->second; + auto sz = getUniformBlockSize(block.members); + const auto bDynamic = isDynamicUniformBlock(block.name); + instance.uniformBuffers[attrID].init(ctx.device, sz, bDynamic); + } + } + + // update per-instance buffer and descriptor set + const auto& programLib = *dynamic_cast(ctx.programLib); + const auto& data = programLib.localLayoutData; + auto& instance = iter->second; + auto* set = instance.descriptorSetPool.allocateDescriptorSet(); + CC_ENSURES(set); + for (const auto& block : shader.getBlocks()) { + if (static_cast(block.set) != pipeline::SetIndex::LOCAL) { + continue; + } + // find descriptor name ID + const auto& name = block.name; + auto iter = ctx.lg.attributeIndex.find(std::string_view{name}); + CC_EXPECTS(iter != ctx.lg.attributeIndex.end()); + const auto attrID = iter->second; + + // get uniformBuffer + auto& uniformBuffer = instance.uniformBuffers[attrID]; + CC_EXPECTS(uniformBuffer.cpuBuffer.size() == uniformBuffer.bufferPool.bufferSize); + + // fill cpu buffer + auto& cpuData = uniformBuffer.cpuBuffer; + if (false) { // NOLINT(readability-simplify-boolean-expr) + for (const auto& v : block.members) { + CC_LOG_INFO(v.name.c_str()); + } + } + + // create and upload buffer + auto* buffer = uniformBuffer.createFromCpuBuffer(); + + // set buffer descriptor + const auto binding = data.bindingMap.at(attrID); + set->bindBuffer(binding, buffer); + } + ctx.perInstanceDescriptorSets[vertID] = set; + } + void discover_vertex( RenderGraph::vertex_descriptor vertID, const boost::filtered_graph< @@ -813,7 +924,7 @@ struct RenderGraphUploadVisitor : boost::dfs_visitor<> { std::ignore = gv; CC_EXPECTS(ctx.currentPassLayoutID != LayoutGraphData::null_vertex()); - if (holds(vertID, ctx.g)) { + if (holds(vertID, ctx.g) || holds(vertID, ctx.g)) { const auto& pass = get(RasterTag{}, vertID, ctx.g); // render pass const auto& layoutName = get(RenderGraph::Layout, ctx.g, vertID); @@ -822,34 +933,62 @@ struct RenderGraphUploadVisitor : boost::dfs_visitor<> { // get layout auto& layout = get(LayoutGraphData::Layout, ctx.lg, layoutID); - // build pass resources - PmrFlatMap resourceIndex(ctx.scratch); - resourceIndex.reserve(pass.computeViews.size() * 2); - for (const auto& [resName, computeViews] : pass.computeViews) { - const auto resID = vertex(resName, ctx.resourceGraph); - for (const auto& computeView : computeViews) { - const auto& name = computeView.name; - CC_EXPECTS(!name.empty()); - const auto nameID = ctx.lg.attributeIndex.at(name); - resourceIndex.emplace(nameID, resID); - } - } - // update states auto iter = layout.descriptorSets.find(UpdateFrequency::PER_PASS); if (iter == layout.descriptorSets.end()) { return; } + + // build pass resources + const auto& resourceIndex = buildResourceIndex( + ctx.resourceGraph, ctx.lg, pass.computeViews, ctx.scratch); + + // populate set auto& set = iter->second; const auto& user = get(RenderGraph::Data, ctx.g, vertID); auto& node = ctx.context.layoutGraphResources.at(layoutID); - auto* perPassSet = initPerPassDescriptorSet( + auto* perPassSet = initDescriptorSet( ctx.resourceGraph, ctx.device, ctx.cmdBuff, *ctx.context.defaultResource, ctx.lg, resourceIndex, set, user, node); CC_ENSURES(perPassSet); - ctx.renderGraphPerPassDescriptorSets[vertID] = perPassSet; + ctx.renderGraphDescriptorSet[vertID] = perPassSet; + } else if (holds(vertID, ctx.g)) { + const auto& queue = get(QueueTag{}, vertID, ctx.g); + if (queue.phaseID == LayoutGraphData::null_vertex()) { + return; + } + + const auto layoutID = queue.phaseID; + + const auto passID = parent(vertID, ctx.g); + const auto& computeViews = getComputeViews(passID, ctx.g); + + // get layout + auto& layout = get(LayoutGraphData::Layout, ctx.lg, layoutID); + auto iter = layout.descriptorSets.find(UpdateFrequency::PER_PHASE); + if (iter == layout.descriptorSets.end()) { + return; + } + + // build pass resources + const auto& resourceIndex = buildResourceIndex( + ctx.resourceGraph, ctx.lg, computeViews, ctx.scratch); + + // populate set + auto& set = iter->second; + const auto& user = get(RenderGraph::Data, ctx.g, vertID); + auto& node = ctx.context.layoutGraphResources.at(layoutID); + + auto* perPhaseSet = initDescriptorSet( + ctx.resourceGraph, + ctx.device, ctx.cmdBuff, + *ctx.context.defaultResource, ctx.lg, + resourceIndex, set, user, node); + CC_ENSURES(perPhaseSet); + + ctx.renderGraphDescriptorSet[vertID] = perPhaseSet; } else if (holds(vertID, ctx.g)) { const auto& sceneData = get(SceneTag{}, vertID, ctx.g); if (sceneData.camera) { @@ -862,86 +1001,13 @@ struct RenderGraphUploadVisitor : boost::dfs_visitor<> { updateAndCreatePerPassDescriptorSet(vertID); ctx.currentProjMatrix = blit.camera->getMatProj(); } - // get pass auto& pass = *blit.material->getPasses()->at(static_cast(blit.passID)); - pass.update(); - - // get shader - auto& shader = *pass.getShaderVariant(); - // update material ubo and descriptor set - // get or create program per-instance descriptor set - auto& node = ctx.context.layoutGraphResources.at(pass.getPhaseID()); - auto iter = node.programResources.find(std::string_view{shader.getName()}); - if (iter == node.programResources.end()) { - // make program resource - auto res = node.programResources.emplace( - std::piecewise_construct, - std::forward_as_tuple(shader.getName()), - std::forward_as_tuple()); - CC_ENSURES(res.second); - iter = res.first; - auto& instance = res.first->second; - - // make per-instance layout - IntrusivePtr instanceSetLayout = - &const_cast( - ctx.programLib->getLocalDescriptorSetLayout( - ctx.device, pass.getPhaseID(), shader.getName())); - - // init per-instance descriptor set pool - instance.descriptorSetPool.init(ctx.device, std::move(instanceSetLayout)); - - for (const auto& block : shader.getBlocks()) { - if (static_cast(block.set) != pipeline::SetIndex::LOCAL) { - continue; - } - const auto& name = block.name; - auto iter = ctx.lg.attributeIndex.find(std::string_view{name}); - CC_EXPECTS(iter != ctx.lg.attributeIndex.end()); - const auto attrID = iter->second; - auto sz = getUniformBlockSize(block.members); - const auto bDynamic = isDynamicUniformBlock(block.name); - instance.uniformBuffers[attrID].init(ctx.device, sz, bDynamic); - } - } - - // update per-instance buffer and descriptor set - const auto& programLib = *dynamic_cast(ctx.programLib); - const auto& data = programLib.localLayoutData; - auto& instance = iter->second; - auto* set = instance.descriptorSetPool.allocateDescriptorSet(); - CC_ENSURES(set); - for (const auto& block : shader.getBlocks()) { - if (static_cast(block.set) != pipeline::SetIndex::LOCAL) { - continue; - } - // find descriptor name ID - const auto& name = block.name; - auto iter = ctx.lg.attributeIndex.find(std::string_view{name}); - CC_EXPECTS(iter != ctx.lg.attributeIndex.end()); - const auto attrID = iter->second; - - // get uniformBuffer - auto& uniformBuffer = instance.uniformBuffers[attrID]; - CC_EXPECTS(uniformBuffer.cpuBuffer.size() == uniformBuffer.bufferPool.bufferSize); - - // fill cpu buffer - auto& cpuData = uniformBuffer.cpuBuffer; - if (false) { // NOLINT(readability-simplify-boolean-expr) - for (const auto& v : block.members) { - CC_LOG_INFO(v.name.c_str()); - } - } - - // create and upload buffer - auto* buffer = uniformBuffer.createFromCpuBuffer(); - - // set buffer descriptor - const auto binding = data.bindingMap.at(attrID); - set->bindBuffer(binding, buffer); - } - ctx.blitPerInstanceDescriptorSets[vertID] = set; + uploadBlitOrDispatchUniformBlokcs(vertID, pass); + } else if (holds(vertID, ctx.g)) { + const auto& dispatch = get(DispatchTag{}, vertID, ctx.g); + auto& pass = *dispatch.material->getPasses()->at(static_cast(dispatch.passID)); + uploadBlitOrDispatchUniformBlokcs(vertID, pass); } } @@ -1015,14 +1081,24 @@ struct RenderGraphVisitor : boost::dfs_visitor<> { } } void tryBindPerPassDescriptorSet(RenderGraph::vertex_descriptor vertID) const { - auto iter = ctx.renderGraphPerPassDescriptorSets.find(vertID); - if (iter != ctx.renderGraphPerPassDescriptorSets.end()) { + auto iter = ctx.renderGraphDescriptorSet.find(vertID); + if (iter != ctx.renderGraphDescriptorSet.end()) { CC_ENSURES(iter->second); ctx.cmdBuff->bindDescriptorSet( static_cast(pipeline::SetIndex::GLOBAL), iter->second); } } + void tryBindPerPhaseDescriptorSet(RenderGraph::vertex_descriptor vertID) const { + auto iter = ctx.renderGraphDescriptorSet.find(vertID); + if (iter != ctx.renderGraphDescriptorSet.end()) { + CC_ENSURES(iter->second); + static_assert(static_cast(pipeline::SetIndex::COUNT) == 3); + ctx.cmdBuff->bindDescriptorSet( + static_cast(pipeline::SetIndex::COUNT), + iter->second); + } + } void begin(const RasterPass& pass, RenderGraph::vertex_descriptor vertID) const { // viewport auto vp = pass.viewport; @@ -1089,8 +1165,10 @@ struct RenderGraphVisitor : boost::dfs_visitor<> { // not implemented yet CC_EXPECTS(false); } - void begin(const RenderQueue& pass, RenderGraph::vertex_descriptor vertID) const { - // update uniform buffers and descriptor sets + void begin(const RenderQueue& queue, RenderGraph::vertex_descriptor vertID) const { + // PerPhase DescriptorSet + std::ignore = queue; + tryBindPerPhaseDescriptorSet(vertID); } void begin(const SceneData& sceneData, RenderGraph::vertex_descriptor sceneID) const { auto* camera = sceneData.camera; @@ -1145,7 +1223,7 @@ struct RenderGraphVisitor : boost::dfs_visitor<> { if (!pso) { return; } - auto* perInstanceSet = ctx.blitPerInstanceDescriptorSets.at(vertID); + auto* perInstanceSet = ctx.perInstanceDescriptorSets.at(vertID); // execution ctx.cmdBuff->bindPipelineState(pso); ctx.cmdBuff->bindDescriptorSet( @@ -1155,7 +1233,30 @@ struct RenderGraphVisitor : boost::dfs_visitor<> { ctx.cmdBuff->bindInputAssembler(ctx.context.fullscreenQuad.quadIA.get()); ctx.cmdBuff->draw(ctx.context.fullscreenQuad.quadIA.get()); } - void begin(const Dispatch& pass, RenderGraph::vertex_descriptor vertID) const { + void begin(const Dispatch& dispatch, RenderGraph::vertex_descriptor vertID) const { + auto& programLib = *ctx.programLib; + CC_EXPECTS(dispatch.material); + CC_EXPECTS(dispatch.material->getPasses()); + // get pass + auto& pass = *dispatch.material->getPasses()->at(static_cast(dispatch.passID)); + // get shader + auto& shader = *pass.getShaderVariant(); + // get pso + auto* pso = programLib.getComputePipelineState( + pass.getDevice(), pass.getPhaseID(), pass.getProgram(), pass.getDefines(), nullptr); + CC_EXPECTS(pso); + auto* perInstanceSet = ctx.perInstanceDescriptorSets.at(vertID); + // execution + ctx.cmdBuff->bindPipelineState(pso); + ctx.cmdBuff->bindDescriptorSet( + static_cast(pipeline::SetIndex::MATERIAL), pass.getDescriptorSet()); + ctx.cmdBuff->bindDescriptorSet( + static_cast(pipeline::SetIndex::LOCAL), perInstanceSet); + ctx.cmdBuff->dispatch(gfx::DispatchInfo{ + dispatch.threadGroupCountX, + dispatch.threadGroupCountY, + dispatch.threadGroupCountZ, + }); } void begin(const ccstd::pmr::vector& pass, RenderGraph::vertex_descriptor vertID) const { } @@ -1284,7 +1385,7 @@ struct RenderGraphVisitor : boost::dfs_visitor<> { auto& set = iter->second; auto& node = ctx.context.layoutGraphResources.at(pass->getPassID()); PmrFlatMap resourceIndex(ctx.scratch); - auto* perPassSet = initPerPassDescriptorSet( + auto* perPassSet = initDescriptorSet( ctx.resourceGraph, ctx.device, ctx.cmdBuff, *ctx.context.defaultResource, ctx.lg, @@ -1816,7 +1917,7 @@ void NativePipeline::executeRenderGraph(const RenderGraph& rg) { ccstd::pmr::unordered_map< RenderGraph::vertex_descriptor, gfx::DescriptorSet*> - renderGraphPerPassDescriptorSets(scratch); + renderGraphDescriptorSet(scratch); ccstd::pmr::unordered_map< RenderGraph::vertex_descriptor, @@ -1826,7 +1927,7 @@ void NativePipeline::executeRenderGraph(const RenderGraph& rg) { ccstd::pmr::unordered_map< RenderGraph::vertex_descriptor, gfx::DescriptorSet*> - blitPerInstanceDescriptorSets(scratch); + perInstanceDescriptorSets(scratch); // submit commands RenderGraphVisitorContext ctx{ @@ -1837,9 +1938,9 @@ void NativePipeline::executeRenderGraph(const RenderGraph& rg) { ppl.device, submit.primaryCommandBuffer, sceneQueues, &ppl, - renderGraphPerPassDescriptorSets, + renderGraphDescriptorSet, profilerPerPassDescriptorSets, - blitPerInstanceDescriptorSets, + perInstanceDescriptorSets, programLibrary, scratch}; diff --git a/native/cocos/renderer/pipeline/custom/NativePipelineTypes.h b/native/cocos/renderer/pipeline/custom/NativePipelineTypes.h index 53f750bf10..b1fb3b93fe 100644 --- a/native/cocos/renderer/pipeline/custom/NativePipelineTypes.h +++ b/native/cocos/renderer/pipeline/custom/NativePipelineTypes.h @@ -110,7 +110,7 @@ public: void addRasterView(const ccstd::string &name, const RasterView &view) override; void addComputeView(const ccstd::string &name, const ComputeView &view) override; - RasterQueueBuilder *addQueue(QueueHint hint) override; + RasterQueueBuilder *addQueue(QueueHint hint, const ccstd::string &layoutName) override; void setViewport(const gfx::Viewport &viewport) override; void setVersion(const ccstd::string &name, uint64_t version) override; bool getShowStatistics() const override; @@ -146,7 +146,7 @@ public: void setReadWriteTexture(const ccstd::string &name, gfx::Texture *texture) override; void setSampler(const ccstd::string &name, gfx::Sampler *sampler) override; - void addDispatch(const ccstd::string &shader, uint32_t threadGroupCountX, uint32_t threadGroupCountY, uint32_t threadGroupCountZ) override; + void addDispatch(uint32_t threadGroupCountX, uint32_t threadGroupCountY, uint32_t threadGroupCountZ, Material *material, uint32_t passID) override; RenderGraph* renderGraph{nullptr}; const LayoutGraphData* layoutGraph{nullptr}; @@ -178,7 +178,7 @@ public: void setSampler(const ccstd::string &name, gfx::Sampler *sampler) override; void addComputeView(const ccstd::string &name, const ComputeView &view) override; - ComputeQueueBuilder *addQueue() override; + ComputeQueueBuilder *addQueue(const ccstd::string &layoutName) override; RenderGraph* renderGraph{nullptr}; const LayoutGraphData* layoutGraph{nullptr}; @@ -531,6 +531,7 @@ public: const IProgramInfo &getProgramInfo(uint32_t phaseID, const ccstd::string &programName) const override; const gfx::ShaderInfo &getShaderInfo(uint32_t phaseID, const ccstd::string &programName) const override; ProgramProxy *getProgramVariant(gfx::Device *device, uint32_t phaseID, const ccstd::string &name, MacroRecord &defines, const ccstd::pmr::string *key) override; + gfx::PipelineState *getComputePipelineState(gfx::Device *device, uint32_t phaseID, const ccstd::string &name, MacroRecord &defines, const ccstd::pmr::string *key) override; const ccstd::vector &getBlockSizes(uint32_t phaseID, const ccstd::string &programName) const override; const Record &getHandleMap(uint32_t phaseID, const ccstd::string &programName) const override; uint32_t getProgramID(uint32_t phaseID, const ccstd::pmr::string &programName) override; @@ -638,11 +639,15 @@ public: NativeProgramProxy() = default; NativeProgramProxy(IntrusivePtr shaderIn) // NOLINT : shader(std::move(shaderIn)) {} + NativeProgramProxy(IntrusivePtr shaderIn, IntrusivePtr pipelineStateIn) + : shader(std::move(shaderIn)), + pipelineState(std::move(pipelineStateIn)) {} const ccstd::string &getName() const noexcept override; gfx::Shader *getShader() const noexcept override; IntrusivePtr shader; + IntrusivePtr pipelineState; }; class NativeRenderingModule final : public RenderingModule { diff --git a/native/cocos/renderer/pipeline/custom/NativeProgramLibrary.cpp b/native/cocos/renderer/pipeline/custom/NativeProgramLibrary.cpp index 2ef49ee96b..209701daa0 100644 --- a/native/cocos/renderer/pipeline/custom/NativeProgramLibrary.cpp +++ b/native/cocos/renderer/pipeline/custom/NativeProgramLibrary.cpp @@ -26,11 +26,11 @@ #include #include #include +#include #include #include #include #include -#include #include "LayoutGraphGraphs.h" #include "LayoutGraphUtils.h" #include "NativePipelineTypes.h" @@ -795,6 +795,7 @@ void makeShaderInfo( } } calculateFlattenedBinding(descriptorSets, fixedInstanceDescriptorSetLayout, shaderInfo); + shaderInfo.stages.reserve(2); shaderInfo.stages.emplace_back(gfx::ShaderStage{gfx::ShaderStageFlagBit::VERTEX, ""}); shaderInfo.stages.emplace_back(gfx::ShaderStage{gfx::ShaderStageFlagBit::FRAGMENT, ""}); } @@ -1352,10 +1353,6 @@ IntrusivePtr NativeProgramLibrary::getPipelineLayout( if (passSet) { info.setLayouts.emplace_back(passSet); } - auto *phaseSet = getDescriptorSetLayout(lg, passID, phaseID, UpdateFrequency::PER_PHASE); - if (phaseSet) { - info.setLayouts.emplace_back(phaseSet); - } auto *batchSet = getProgramDescriptorSetLayout( device, lg, phaseID, programName, UpdateFrequency::PER_BATCH); if (batchSet) { @@ -1366,6 +1363,10 @@ IntrusivePtr NativeProgramLibrary::getPipelineLayout( if (instanceSet) { info.setLayouts.emplace_back(instanceSet); } + auto *phaseSet = getDescriptorSetLayout(lg, passID, phaseID, UpdateFrequency::PER_PHASE); + if (phaseSet) { + info.setLayouts.emplace_back(phaseSet); + } programData.pipelineLayout = device->createPipelineLayout(info); return programData.pipelineLayout; } @@ -1475,8 +1476,17 @@ ProgramProxy *NativeProgramLibrary::getProgramVariant( } else { CC_LOG_ERROR("Invalid GFX API!"); } - info.shaderInfo.stages[0].source = prefix + src->vert; - info.shaderInfo.stages[1].source = prefix + src->frag; + + if (src->compute) { + info.shaderInfo.stages.clear(); + info.shaderInfo.stages.emplace_back( + gfx::ShaderStage{ + gfx::ShaderStageFlagBit::COMPUTE, + prefix + *src->compute}); + } else { + info.shaderInfo.stages[0].source = prefix + src->vert; + info.shaderInfo.stages[1].source = prefix + src->frag; + } // strip out the active attributes only, instancing depend on this info.shaderInfo.attributes = getActiveAttributes(programInfo, info.attributes, defines); @@ -1492,6 +1502,36 @@ ProgramProxy *NativeProgramLibrary::getProgramVariant( return res.first->second.get(); } +gfx::PipelineState* NativeProgramLibrary::getComputePipelineState( + gfx::Device *device, uint32_t phaseID, const ccstd::string &name, + MacroRecord &defines, const ccstd::pmr::string *key) { + auto *program = getProgramVariant(device, phaseID, name, defines, key); + auto *native = static_cast(program); + CC_EXPECTS(native->shader->getStages().size() == 1); + CC_EXPECTS(native->shader->getStages().at(0).stage == gfx::ShaderStageFlagBit::COMPUTE); + + if (native->pipelineState) { + return native->pipelineState; + } + + gfx::PipelineStateInfo info{ + native->shader.get(), + getPipelineLayout(device, phaseID, name), + nullptr, + gfx::InputState{}, + gfx::RasterizerState{}, + gfx::DepthStencilState{}, + gfx::BlendState{}, + gfx::PrimitiveMode::TRIANGLE_LIST, + gfx::DynamicStateFlagBit::NONE, + gfx::PipelineBindPoint::COMPUTE, + 0, + }; + native->pipelineState = device->createPipelineState(info); + CC_ENSURES(native->pipelineState); + return native->pipelineState.get(); +} + const ccstd::vector &NativeProgramLibrary::getBlockSizes( uint32_t phaseID, const ccstd::string &programName) const { CC_EXPECTS(phaseID != LayoutGraphData::null_vertex()); diff --git a/native/cocos/renderer/pipeline/custom/NativeRenderGraph.cpp b/native/cocos/renderer/pipeline/custom/NativeRenderGraph.cpp index 62373a0541..60bf7e2d75 100644 --- a/native/cocos/renderer/pipeline/custom/NativeRenderGraph.cpp +++ b/native/cocos/renderer/pipeline/custom/NativeRenderGraph.cpp @@ -32,6 +32,7 @@ #include "RenderGraphFwd.h" #include "RenderGraphGraphs.h" #include "RenderGraphTypes.h" +#include "RenderingModule.h" #include "cocos/math/Utils.h" #include "cocos/renderer/pipeline/Define.h" #include "cocos/renderer/pipeline/PipelineUBO.h" @@ -883,7 +884,16 @@ void NativeRasterQueueBuilder::setSampler(const ccstd::string &name, gfx::Sample addSampler(*layoutGraph, name, sampler, data); } -RasterQueueBuilder *NativeRasterPassBuilder::addQueue(QueueHint hint) { +RasterQueueBuilder *NativeRasterPassBuilder::addQueue( + QueueHint hint, const ccstd::string &layoutName) { + CC_EXPECTS(layoutID != LayoutGraphData::null_vertex()); + + auto phaseLayoutID = LayoutGraphData::null_vertex(); + if (!layoutName.empty()) { + phaseLayoutID = locate(layoutID, layoutName, *layoutGraph); + CC_ENSURES(phaseLayoutID != LayoutGraphData::null_vertex()); + } + std::string_view name = "Queue"; auto queueID = addVertex( QueueTag{}, @@ -891,7 +901,7 @@ RasterQueueBuilder *NativeRasterPassBuilder::addQueue(QueueHint hint) { std::forward_as_tuple(), std::forward_as_tuple(), std::forward_as_tuple(), - std::forward_as_tuple(hint), + std::forward_as_tuple(hint, phaseLayoutID), *renderGraph, passID); return new NativeRasterQueueBuilder(pipelineRuntime, renderGraph, queueID, layoutGraph); @@ -970,7 +980,9 @@ void NativeComputeQueueBuilder::setName(const ccstd::string &name) { get(RenderGraph::Name, *renderGraph, queueID) = std::string_view{name}; } -void NativeComputeQueueBuilder::addDispatch(const ccstd::string &shader, uint32_t threadGroupCountX, uint32_t threadGroupCountY, uint32_t threadGroupCountZ) { +void NativeComputeQueueBuilder::addDispatch( + uint32_t threadGroupCountX, uint32_t threadGroupCountY, uint32_t threadGroupCountZ, + Material *material, uint32_t passID) { std::string_view name("Dispatch"); addVertex( DispatchTag{}, @@ -979,7 +991,8 @@ void NativeComputeQueueBuilder::addDispatch(const ccstd::string &shader, uint32_ std::forward_as_tuple(), std::forward_as_tuple(), std::forward_as_tuple( - shader.c_str(), + material, + passID, threadGroupCountX, threadGroupCountY, threadGroupCountZ), @@ -1065,7 +1078,15 @@ void NativeComputePassBuilder::addComputeView(const ccstd::string &name, const C iter->second.emplace_back(view); } -ComputeQueueBuilder *NativeComputePassBuilder::addQueue() { +ComputeQueueBuilder *NativeComputePassBuilder::addQueue(const ccstd::string &layoutName) { + CC_EXPECTS(layoutID != LayoutGraphData::null_vertex()); + + auto phaseLayoutID = LayoutGraphData::null_vertex(); + if (!layoutName.empty()) { + phaseLayoutID = locate(layoutID, layoutName, *layoutGraph); + CC_ENSURES(phaseLayoutID != LayoutGraphData::null_vertex()); + } + std::string_view name("Queue"); auto queueID = addVertex( QueueTag{}, @@ -1073,7 +1094,7 @@ ComputeQueueBuilder *NativeComputePassBuilder::addQueue() { std::forward_as_tuple(), std::forward_as_tuple(), std::forward_as_tuple(), - std::forward_as_tuple(QueueHint::NONE), + std::forward_as_tuple(QueueHint::NONE, phaseLayoutID), *renderGraph, passID); return new NativeComputeQueueBuilder(renderGraph, queueID, layoutGraph); diff --git a/native/cocos/renderer/pipeline/custom/PrivateTypes.h b/native/cocos/renderer/pipeline/custom/PrivateTypes.h index d8c6b49372..2bea6dbc8a 100644 --- a/native/cocos/renderer/pipeline/custom/PrivateTypes.h +++ b/native/cocos/renderer/pipeline/custom/PrivateTypes.h @@ -69,6 +69,7 @@ public: virtual const IProgramInfo &getProgramInfo(uint32_t phaseID, const ccstd::string &programName) const = 0; virtual const gfx::ShaderInfo &getShaderInfo(uint32_t phaseID, const ccstd::string &programName) const = 0; virtual ProgramProxy *getProgramVariant(gfx::Device *device, uint32_t phaseID, const ccstd::string &name, MacroRecord &defines, const ccstd::pmr::string *key) = 0; + virtual gfx::PipelineState *getComputePipelineState(gfx::Device *device, uint32_t phaseID, const ccstd::string &name, MacroRecord &defines, const ccstd::pmr::string *key) = 0; virtual const ccstd::vector &getBlockSizes(uint32_t phaseID, const ccstd::string &programName) const = 0; virtual const Record &getHandleMap(uint32_t phaseID, const ccstd::string &programName) const = 0; virtual uint32_t getProgramID(uint32_t phaseID, const ccstd::pmr::string &programName) = 0; @@ -77,6 +78,9 @@ public: ProgramProxy *getProgramVariant(gfx::Device *device, uint32_t phaseID, const ccstd::string &name, MacroRecord &defines) { return getProgramVariant(device, phaseID, name, defines, nullptr); } + gfx::PipelineState *getComputePipelineState(gfx::Device *device, uint32_t phaseID, const ccstd::string &name, MacroRecord &defines) { + return getComputePipelineState(device, phaseID, name, defines, nullptr); + } }; } // namespace render diff --git a/native/cocos/renderer/pipeline/custom/RenderGraphTypes.cpp b/native/cocos/renderer/pipeline/custom/RenderGraphTypes.cpp index 76fa619ff4..4d4fca9a89 100644 --- a/native/cocos/renderer/pipeline/custom/RenderGraphTypes.cpp +++ b/native/cocos/renderer/pipeline/custom/RenderGraphTypes.cpp @@ -249,27 +249,6 @@ SceneData::SceneData(SceneData const& rhs, const allocator_type& alloc) flags(rhs.flags), scenes(rhs.scenes, alloc) {} -Dispatch::Dispatch(const allocator_type& alloc) noexcept -: shader(alloc) {} - -Dispatch::Dispatch(ccstd::pmr::string shaderIn, uint32_t threadGroupCountXIn, uint32_t threadGroupCountYIn, uint32_t threadGroupCountZIn, const allocator_type& alloc) noexcept // NOLINT -: shader(std::move(shaderIn), alloc), - threadGroupCountX(threadGroupCountXIn), - threadGroupCountY(threadGroupCountYIn), - threadGroupCountZ(threadGroupCountZIn) {} - -Dispatch::Dispatch(Dispatch&& rhs, const allocator_type& alloc) -: shader(std::move(rhs.shader), alloc), - threadGroupCountX(rhs.threadGroupCountX), - threadGroupCountY(rhs.threadGroupCountY), - threadGroupCountZ(rhs.threadGroupCountZ) {} - -Dispatch::Dispatch(Dispatch const& rhs, const allocator_type& alloc) -: shader(rhs.shader, alloc), - threadGroupCountX(rhs.threadGroupCountX), - threadGroupCountY(rhs.threadGroupCountY), - threadGroupCountZ(rhs.threadGroupCountZ) {} - PresentPass::PresentPass(const allocator_type& alloc) noexcept : presents(alloc) {} diff --git a/native/cocos/renderer/pipeline/custom/RenderGraphTypes.h b/native/cocos/renderer/pipeline/custom/RenderGraphTypes.h index 127ce2b017..b8c9c4ad58 100644 --- a/native/cocos/renderer/pipeline/custom/RenderGraphTypes.h +++ b/native/cocos/renderer/pipeline/custom/RenderGraphTypes.h @@ -612,10 +612,12 @@ struct ClearView { struct RenderQueue { RenderQueue() = default; - RenderQueue(QueueHint hintIn) noexcept // NOLINT - : hint(hintIn) {} + RenderQueue(QueueHint hintIn, uint32_t phaseIDIn) noexcept + : hint(hintIn), + phaseID(phaseIDIn) {} QueueHint hint{QueueHint::RENDER_OPAQUE}; + uint32_t phaseID{0xFFFFFFFF}; }; struct SceneData { @@ -642,22 +644,16 @@ struct SceneData { }; struct Dispatch { - using allocator_type = boost::container::pmr::polymorphic_allocator; - allocator_type get_allocator() const noexcept { // NOLINT - return {shader.get_allocator().resource()}; - } + Dispatch() = default; + Dispatch(IntrusivePtr materialIn, uint32_t passIDIn, uint32_t threadGroupCountXIn, uint32_t threadGroupCountYIn, uint32_t threadGroupCountZIn) noexcept // NOLINT + : material(std::move(materialIn)), + passID(passIDIn), + threadGroupCountX(threadGroupCountXIn), + threadGroupCountY(threadGroupCountYIn), + threadGroupCountZ(threadGroupCountZIn) {} - Dispatch(const allocator_type& alloc) noexcept; // NOLINT - Dispatch(ccstd::pmr::string shaderIn, uint32_t threadGroupCountXIn, uint32_t threadGroupCountYIn, uint32_t threadGroupCountZIn, const allocator_type& alloc) noexcept; - Dispatch(Dispatch&& rhs, const allocator_type& alloc); - Dispatch(Dispatch const& rhs, const allocator_type& alloc); - - Dispatch(Dispatch&& rhs) noexcept = default; - Dispatch(Dispatch const& rhs) = delete; - Dispatch& operator=(Dispatch&& rhs) = default; - Dispatch& operator=(Dispatch const& rhs) = default; - - ccstd::pmr::string shader; + IntrusivePtr material; + uint32_t passID{0}; uint32_t threadGroupCountX{0}; uint32_t threadGroupCountY{0}; uint32_t threadGroupCountZ{0}; diff --git a/native/cocos/renderer/pipeline/custom/RenderInterfaceTypes.h b/native/cocos/renderer/pipeline/custom/RenderInterfaceTypes.h index df572e3553..f6e002f1a4 100644 --- a/native/cocos/renderer/pipeline/custom/RenderInterfaceTypes.h +++ b/native/cocos/renderer/pipeline/custom/RenderInterfaceTypes.h @@ -170,13 +170,16 @@ public: virtual void addRasterView(const ccstd::string &name, const RasterView &view) = 0; virtual void addComputeView(const ccstd::string &name, const ComputeView &view) = 0; - virtual RasterQueueBuilder *addQueue(QueueHint hint) = 0; + virtual RasterQueueBuilder *addQueue(QueueHint hint, const ccstd::string &layoutName) = 0; virtual void setViewport(const gfx::Viewport &viewport) = 0; virtual void setVersion(const ccstd::string &name, uint64_t version) = 0; virtual bool getShowStatistics() const = 0; virtual void setShowStatistics(bool enable) = 0; RasterQueueBuilder *addQueue() { - return addQueue(QueueHint::NONE); + return addQueue(QueueHint::NONE, ""); + } + RasterQueueBuilder *addQueue(QueueHint hint) { + return addQueue(hint, ""); } }; @@ -184,7 +187,13 @@ class ComputeQueueBuilder : public Setter { public: ComputeQueueBuilder() noexcept = default; - virtual void addDispatch(const ccstd::string &shader, uint32_t threadGroupCountX, uint32_t threadGroupCountY, uint32_t threadGroupCountZ) = 0; + virtual void addDispatch(uint32_t threadGroupCountX, uint32_t threadGroupCountY, uint32_t threadGroupCountZ, Material *material, uint32_t passID) = 0; + void addDispatch(uint32_t threadGroupCountX, uint32_t threadGroupCountY, uint32_t threadGroupCountZ) { + addDispatch(threadGroupCountX, threadGroupCountY, threadGroupCountZ, nullptr, 0); + } + void addDispatch(uint32_t threadGroupCountX, uint32_t threadGroupCountY, uint32_t threadGroupCountZ, Material *material) { + addDispatch(threadGroupCountX, threadGroupCountY, threadGroupCountZ, material, 0); + } }; class ComputePassBuilder : public Setter { @@ -192,7 +201,10 @@ public: ComputePassBuilder() noexcept = default; virtual void addComputeView(const ccstd::string &name, const ComputeView &view) = 0; - virtual ComputeQueueBuilder *addQueue() = 0; + virtual ComputeQueueBuilder *addQueue(const ccstd::string &layoutName) = 0; + ComputeQueueBuilder *addQueue() { + return addQueue(""); + } }; class MovePassBuilder : public RenderNode {