From 588a64e277af5410c28cfc1948ddbb48d3ee678c Mon Sep 17 00:00:00 2001 From: hyde zhou Date: Tue, 7 Feb 2023 16:50:24 +0800 Subject: [PATCH] fix web-pipeline ui in post-processing (#14145) --- cocos/rendering/custom/builtin-pipelines.ts | 27 ++- cocos/rendering/custom/custom-pipeline.ts | 30 +++- cocos/rendering/custom/define.ts | 84 ++++++++- cocos/rendering/custom/pipeline.ts | 1 + cocos/rendering/custom/render-graph.ts | 1 + cocos/rendering/custom/utils.ts | 13 ++ cocos/rendering/custom/web-pipeline.ts | 6 + cocos/rendering/pipeline-funcs.ts | 3 - .../cocos/bindings/auto/jsb_render_auto.cpp | 42 +++++ .../renderer/gfx-metal/MTLPipelineState.mm | 12 +- .../gfx-validator/RenderPassValidator.cpp | 11 -- .../pipeline/custom/LayoutGraphUtils.cpp | 4 +- .../pipeline/custom/NativeExecutor.cpp | 168 +++++++++++++++--- .../pipeline/custom/NativePipelineTypes.cpp | 8 +- .../pipeline/custom/NativePipelineTypes.h | 5 +- .../pipeline/custom/NativeProgramLibrary.cpp | 8 + .../pipeline/custom/NativeRenderGraph.cpp | 23 ++- .../pipeline/custom/RenderGraphTypes.cpp | 6 +- .../pipeline/custom/RenderGraphTypes.h | 1 + .../pipeline/custom/RenderInterfaceTypes.h | 2 + native/tools/swig-config/renderer.i | 1 + 21 files changed, 393 insertions(+), 63 deletions(-) diff --git a/cocos/rendering/custom/builtin-pipelines.ts b/cocos/rendering/custom/builtin-pipelines.ts index 54a71b9eed..fdcf099193 100644 --- a/cocos/rendering/custom/builtin-pipelines.ts +++ b/cocos/rendering/custom/builtin-pipelines.ts @@ -23,9 +23,11 @@ */ import { EDITOR } from 'internal:constants'; -import { Camera } from '../../render-scene/scene'; +import { Camera, CameraUsage } from '../../render-scene/scene'; import { Pipeline, PipelineBuilder } from './pipeline'; -import { buildForwardPass, buildGBufferPass, buildLightingPass, buildPostprocessPass, buildReflectionProbePasss } from './define'; +import { buildForwardPass, buildGBufferPass, buildLightingPass, buildPostprocessPass, buildProfilerPass, + buildReflectionProbePasss, buildUIAndProfilerPass } from './define'; +import { isUICamera } from './utils'; export class ForwardPipelineBuilder implements PipelineBuilder { public setup (cameras: Camera[], ppl: Pipeline): void { @@ -50,16 +52,25 @@ export class DeferredPipelineBuilder implements PipelineBuilder { if (!camera.scene) { continue; } - if (EDITOR) { + const isGameView = camera.cameraUsage === CameraUsage.GAME + || camera.cameraUsage === CameraUsage.GAME_VIEW; + if (!isGameView) { buildForwardPass(camera, ppl, false); continue; } + if (!isUICamera(camera)) { // GBuffer Pass - const gBufferInfo = buildGBufferPass(camera, ppl); - // Lighting Pass - const lightInfo = buildLightingPass(camera, ppl, gBufferInfo); - // Postprocess - buildPostprocessPass(camera, ppl, lightInfo.rtName); + const gBufferInfo = buildGBufferPass(camera, ppl); + // Lighting Pass + const lightInfo = buildLightingPass(camera, ppl, gBufferInfo); + // Postprocess + buildPostprocessPass(camera, ppl, lightInfo.rtName); + // Profiler + buildProfilerPass(camera, ppl); + continue; + } + // render ui and profiler + buildUIAndProfilerPass(camera, ppl); } } } diff --git a/cocos/rendering/custom/custom-pipeline.ts b/cocos/rendering/custom/custom-pipeline.ts index 3563449efc..263a9993cf 100644 --- a/cocos/rendering/custom/custom-pipeline.ts +++ b/cocos/rendering/custom/custom-pipeline.ts @@ -24,8 +24,10 @@ import { Camera, CameraUsage } from '../../render-scene/scene'; import { buildFxaaPass, buildBloomPass as buildBloomPasses, buildForwardPass, - buildNativeDeferredPipeline, buildNativeForwardPass, buildPostprocessPass, AntiAliasing } from './define'; + buildNativeDeferredPipeline, buildNativeForwardPass, buildPostprocessPass, + AntiAliasing, buildUIAndProfilerPass, buildProfilerPass } from './define'; import { Pipeline, PipelineBuilder } from './pipeline'; +import { isUICamera } from './utils'; export class CustomPipelineBuilder implements PipelineBuilder { public setup (cameras: Camera[], ppl: Pipeline): void { @@ -36,17 +38,27 @@ export class CustomPipelineBuilder implements PipelineBuilder { } const isGameView = camera.cameraUsage === CameraUsage.GAME || camera.cameraUsage === CameraUsage.GAME_VIEW; - // forward pass - const forwardInfo = buildForwardPass(camera, ppl, isGameView); if (!isGameView) { + // forward pass + buildForwardPass(camera, ppl, isGameView); continue; } - // fxaa pass - const fxaaInfo = buildFxaaPass(camera, ppl, forwardInfo.rtName); - // bloom passes - const bloomInfo = buildBloomPasses(camera, ppl, fxaaInfo.rtName); - // Present Pass - buildPostprocessPass(camera, ppl, bloomInfo.rtName, AntiAliasing.NONE); + // TODO: There is currently no effective way to judge the ui camera. Let’s do this first. + if (!isUICamera(camera)) { + // forward pass + const forwardInfo = buildForwardPass(camera, ppl, isGameView); + // fxaa pass + const fxaaInfo = buildFxaaPass(camera, ppl, forwardInfo.rtName); + // bloom passes + const bloomInfo = buildBloomPasses(camera, ppl, fxaaInfo.rtName); + // Present Pass + buildPostprocessPass(camera, ppl, bloomInfo.rtName, AntiAliasing.NONE); + // Profiler + buildProfilerPass(camera, ppl); + continue; + } + // render ui and profiler + buildUIAndProfilerPass(camera, ppl); } } } diff --git a/cocos/rendering/custom/define.ts b/cocos/rendering/custom/define.ts index 62e8a05b17..20199468ac 100644 --- a/cocos/rendering/custom/define.ts +++ b/cocos/rendering/custom/define.ts @@ -519,7 +519,7 @@ export function buildPostprocessPass (camera: Camera, postInfo.postMaterial, 0, SceneFlags.NONE, ); postprocessPass.addQueue(QueueHint.RENDER_TRANSPARENT).addSceneOfCamera(camera, new LightInfo(), - SceneFlags.UI | SceneFlags.PROFILER); + SceneFlags.UI); return { rtName: postprocessPassRTName, dsName: postprocessPassDS }; } @@ -917,8 +917,8 @@ export function buildLightingPass (camera: Camera, ppl: Pipeline, gBuffer: GBuff camera, lightingInfo.deferredLightingMaterial, 0, SceneFlags.VOLUMETRIC_LIGHTING, ); - // lightingPass.addQueue(QueueHint.RENDER_TRANSPARENT).addSceneOfCamera(camera, new LightInfo(), - // SceneFlags.TRANSPARENT_OBJECT | SceneFlags.PLANAR_SHADOW | SceneFlags.GEOMETRY); + lightingPass.addQueue(QueueHint.RENDER_TRANSPARENT).addSceneOfCamera(camera, new LightInfo(), + SceneFlags.TRANSPARENT_OBJECT | SceneFlags.PLANAR_SHADOW | SceneFlags.GEOMETRY); return { rtName: deferredLightingPassRTName, dsName: deferredLightingPassDS }; } @@ -944,6 +944,84 @@ function getClearFlags (attachment: AttachmentType, clearFlag: ClearFlagBit, loa } } +export function buildProfilerPass (camera: Camera, + ppl: Pipeline) { + const cameraID = getCameraUniqueID(camera); + const cameraName = `Camera${cameraID}`; + const area = getRenderArea(camera, camera.window.width, camera.window.height); + const width = area.width; + const height = area.height; + + const dsProfilerPassRTName = `dsProfilerPassColor${cameraName}`; + const dsProfilerPassDSName = `dsProfilerPassDS${cameraName}`; + if (!ppl.containsResource(dsProfilerPassRTName)) { + ppl.addRenderTexture(dsProfilerPassRTName, Format.BGRA8, width, height, camera.window); + ppl.addDepthStencil(dsProfilerPassDSName, Format.DEPTH_STENCIL, width, height, ResourceResidency.MANAGED); + } + ppl.updateRenderWindow(dsProfilerPassRTName, camera.window); + ppl.updateDepthStencil(dsProfilerPassDSName, width, height); + const ProfilerPass = ppl.addRasterPass(width, height, 'default'); + ProfilerPass.name = `CameraProfilerPass${cameraID}`; + ProfilerPass.setViewport(new Viewport(area.x, area.y, width, height)); + const passView = new RasterView('_', + AccessType.WRITE, AttachmentType.RENDER_TARGET, + getLoadOpOfClearFlag(ClearFlagBit.DEPTH_STENCIL, AttachmentType.RENDER_TARGET), + StoreOp.STORE, + ClearFlagBit.DEPTH_STENCIL, + new Color(camera.clearColor.x, camera.clearColor.y, camera.clearColor.z, camera.clearColor.w)); + const passDSView = new RasterView('_', + AccessType.WRITE, AttachmentType.DEPTH_STENCIL, + getLoadOpOfClearFlag(ClearFlagBit.DEPTH_STENCIL, AttachmentType.DEPTH_STENCIL), + StoreOp.STORE, + ClearFlagBit.DEPTH_STENCIL, + new Color(camera.clearDepth, camera.clearStencil, 0, 0)); + ProfilerPass.addRasterView(dsProfilerPassRTName, passView); + ProfilerPass.addRasterView(dsProfilerPassDSName, passDSView); + const sceneFlags = SceneFlags.PROFILER; + ProfilerPass + .addQueue(QueueHint.RENDER_TRANSPARENT) + .addSceneOfCamera(camera, new LightInfo(), sceneFlags); +} + +export function buildUIAndProfilerPass (camera: Camera, + ppl: Pipeline) { + const cameraID = getCameraUniqueID(camera); + const cameraName = `Camera${cameraID}`; + const area = getRenderArea(camera, camera.window.width, camera.window.height); + const width = area.width; + const height = area.height; + + const dsUIAndProfilerPassRTName = `dsUIAndProfilerPassColor${cameraName}`; + const dsUIAndProfilerPassDSName = `dsUIAndProfilerPassDS${cameraName}`; + if (!ppl.containsResource(dsUIAndProfilerPassRTName)) { + ppl.addRenderTexture(dsUIAndProfilerPassRTName, Format.BGRA8, width, height, camera.window); + ppl.addDepthStencil(dsUIAndProfilerPassDSName, Format.DEPTH_STENCIL, width, height, ResourceResidency.MANAGED); + } + ppl.updateRenderWindow(dsUIAndProfilerPassRTName, camera.window); + ppl.updateDepthStencil(dsUIAndProfilerPassDSName, width, height); + const uIAndProfilerPass = ppl.addRasterPass(width, height, 'default'); + uIAndProfilerPass.name = `CameraUIAndProfilerPass${cameraID}`; + uIAndProfilerPass.setViewport(new Viewport(area.x, area.y, width, height)); + const passView = new RasterView('_', + AccessType.WRITE, AttachmentType.RENDER_TARGET, + getLoadOpOfClearFlag(camera.clearFlag, AttachmentType.RENDER_TARGET), + StoreOp.STORE, + camera.clearFlag, + new Color(camera.clearColor.x, camera.clearColor.y, camera.clearColor.z, camera.clearColor.w)); + const passDSView = new RasterView('_', + AccessType.WRITE, AttachmentType.DEPTH_STENCIL, + getLoadOpOfClearFlag(camera.clearFlag, AttachmentType.DEPTH_STENCIL), + StoreOp.STORE, + camera.clearFlag, + new Color(camera.clearDepth, camera.clearStencil, 0, 0)); + uIAndProfilerPass.addRasterView(dsUIAndProfilerPassRTName, passView); + uIAndProfilerPass.addRasterView(dsUIAndProfilerPassDSName, passDSView); + const sceneFlags = SceneFlags.UI | SceneFlags.PROFILER; + uIAndProfilerPass + .addQueue(QueueHint.RENDER_TRANSPARENT) + .addSceneOfCamera(camera, new LightInfo(), sceneFlags); +} + export function buildNativeForwardPass (camera: Camera, ppl: Pipeline) { const cameraID = getCameraUniqueID(camera); const cameraName = `Camera${cameraID}`; diff --git a/cocos/rendering/custom/pipeline.ts b/cocos/rendering/custom/pipeline.ts index 6db2bc53d2..f41b06377d 100644 --- a/cocos/rendering/custom/pipeline.ts +++ b/cocos/rendering/custom/pipeline.ts @@ -104,6 +104,7 @@ export interface RasterPassBuilder extends Setter { addQueue (/*QueueHint.NONE*/): RasterQueueBuilder; setViewport (viewport: Viewport): void; setVersion (name: string, version: number): void; + showStatistics: boolean; } export interface ComputeQueueBuilder extends Setter { diff --git a/cocos/rendering/custom/render-graph.ts b/cocos/rendering/custom/render-graph.ts index 1ea87b4be8..50fa37f77e 100644 --- a/cocos/rendering/custom/render-graph.ts +++ b/cocos/rendering/custom/render-graph.ts @@ -1026,6 +1026,7 @@ export class RasterPass { readonly viewport: Viewport = new Viewport(); versionName = ''; version = 0; + showStatistics = false; } export class ComputePass { diff --git a/cocos/rendering/custom/utils.ts b/cocos/rendering/custom/utils.ts index b69a79be4c..5489f4a602 100644 --- a/cocos/rendering/custom/utils.ts +++ b/cocos/rendering/custom/utils.ts @@ -25,6 +25,19 @@ // https://stackoverflow.com/questions/56714318/how-to-disable-multiple-rules-for-eslint-nextline?msclkid=5d4c2298ba7911eca34d0ab30591752e import { Type } from '../../gfx'; +import { Camera } from '../../render-scene/scene/camera'; + +export function isUICamera (camera: Camera): boolean { + const scene = camera.scene!; + const batches = scene.batches; + for (let i = 0; i < batches.length; i++) { + const batch = batches[i]; + if (camera.visibility & batch.visFlags) { + return true; + } + } + return false; +} // eslint-disable-next-line @typescript-eslint/no-unused-vars export function replacer (key: unknown, value: unknown) { diff --git a/cocos/rendering/custom/web-pipeline.ts b/cocos/rendering/custom/web-pipeline.ts index 4ab0637cbb..897eec3586 100644 --- a/cocos/rendering/custom/web-pipeline.ts +++ b/cocos/rendering/custom/web-pipeline.ts @@ -721,6 +721,12 @@ export class WebRasterPassBuilder extends WebSetter implements RasterPassBuilder setViewport (viewport: Viewport): void { this._pass.viewport.copy(viewport); } + get showStatistics (): boolean { + return this._pass.showStatistics; + } + set showStatistics (enable: boolean) { + this._pass.showStatistics = enable; + } private readonly _renderGraph: RenderGraph; private readonly _vertID: number; private readonly _layoutID: number; diff --git a/cocos/rendering/pipeline-funcs.ts b/cocos/rendering/pipeline-funcs.ts index f588a43718..e5c32a8ff9 100644 --- a/cocos/rendering/pipeline-funcs.ts +++ b/cocos/rendering/pipeline-funcs.ts @@ -77,9 +77,6 @@ export function decideProfilerCamera (cameras: Camera[]) { } export function renderProfiler (device: Device, renderPass: RenderPass, cmdBuff: CommandBuffer, profiler: Model | null, camera: Camera) { - if (isEnableEffect()) { - return; - } if (!profiler || !profiler.enabled) { return; } diff --git a/native/cocos/bindings/auto/jsb_render_auto.cpp b/native/cocos/bindings/auto/jsb_render_auto.cpp index c4fb2952f9..d881a6b1f1 100644 --- a/native/cocos/bindings/auto/jsb_render_auto.cpp +++ b/native/cocos/bindings/auto/jsb_render_auto.cpp @@ -106,6 +106,10 @@ using namespace cc::render; #define cc_render_RenderNode_name_set(self_, val_) self_->setName(val_) +#define cc_render_RasterPassBuilder_showStatistics_get(self_) self_->getShowStatistics() +#define cc_render_RasterPassBuilder_showStatistics_set(self_, val_) self_->setShowStatistics(val_) + + #define cc_render_SceneVisitor_pipelineSceneData_get(self_) self_->getPipelineSceneData() @@ -1814,6 +1818,43 @@ static bool js_cc_render_RasterPassBuilder_addQueue(se::State& s) } SE_BIND_FUNC(js_cc_render_RasterPassBuilder_addQueue) +static bool js_cc_render_RasterPassBuilder_showStatistics_set(se::State& s) +{ + CC_UNUSED bool ok = true; + const auto& args = s.args(); + size_t argc = args.size(); + cc::render::RasterPassBuilder *arg1 = (cc::render::RasterPassBuilder *) NULL ; + bool arg2 ; + + arg1 = SE_THIS_OBJECT(s); + if (nullptr == arg1) return true; + + ok &= sevalue_to_native(args[0], &arg2); + SE_PRECONDITION2(ok, false, "Error processing arguments"); + cc_render_RasterPassBuilder_showStatistics_set(arg1,arg2); + + + return true; +} +SE_BIND_PROP_SET(js_cc_render_RasterPassBuilder_showStatistics_set) + +static bool js_cc_render_RasterPassBuilder_showStatistics_get(se::State& s) +{ + CC_UNUSED bool ok = true; + cc::render::RasterPassBuilder *arg1 = (cc::render::RasterPassBuilder *) NULL ; + bool result; + + arg1 = SE_THIS_OBJECT(s); + if (nullptr == arg1) return true; + result = (bool)cc_render_RasterPassBuilder_showStatistics_get(arg1); + + ok &= nativevalue_to_se(result, s.rval(), s.thisObject()); + + + return true; +} +SE_BIND_PROP_GET(js_cc_render_RasterPassBuilder_showStatistics_get) + static bool js_delete_cc_render_RasterPassBuilder(se::State& s) { return true; @@ -1824,6 +1865,7 @@ bool js_register_cc_render_RasterPassBuilder(se::Object* obj) { auto* cls = se::Class::create("RasterPassBuilder", obj, __jsb_cc_render_Setter_proto, nullptr); cls->defineStaticProperty("__isJSB", se::Value(true), se::PropertyAttribute::READ_ONLY | se::PropertyAttribute::DONT_ENUM | se::PropertyAttribute::DONT_DELETE); + cls->defineProperty("showStatistics", _SE(js_cc_render_RasterPassBuilder_showStatistics_get), _SE(js_cc_render_RasterPassBuilder_showStatistics_set)); cls->defineFunction("addRasterView", _SE(js_cc_render_RasterPassBuilder_addRasterView)); cls->defineFunction("addComputeView", _SE(js_cc_render_RasterPassBuilder_addComputeView)); diff --git a/native/cocos/renderer/gfx-metal/MTLPipelineState.mm b/native/cocos/renderer/gfx-metal/MTLPipelineState.mm index 81af7360df..835a857142 100644 --- a/native/cocos/renderer/gfx-metal/MTLPipelineState.mm +++ b/native/cocos/renderer/gfx-metal/MTLPipelineState.mm @@ -145,6 +145,17 @@ bool CCMTLPipelineState::createMTLComputePipelineState() { } bool CCMTLPipelineState::createMTLDepthStencilState() { + const auto& colorAttachments = _renderPass->getColorAttachments(); + bool hasDS = std::any_of(colorAttachments.begin(), colorAttachments.end(), [](const ColorAttachment& attachemnt){ + return attachemnt.format == Format::DEPTH ||attachemnt.format == Format::DEPTH_STENCIL; + }); + hasDS |= _renderPass->getDepthStencilAttachment().format != Format::UNKNOWN; + + if(!hasDS) { + // default nil + return true; + } + MTLDepthStencilDescriptor *descriptor = [[MTLDepthStencilDescriptor alloc] init]; if (descriptor == nil) { CC_LOG_ERROR("CCMTLPipelineState: MTLDepthStencilDescriptor could not be allocated."); @@ -152,7 +163,6 @@ bool CCMTLPipelineState::createMTLDepthStencilState() { } descriptor.depthWriteEnabled = _depthStencilState.depthWrite != 0; - if (!_depthStencilState.depthTest) descriptor.depthCompareFunction = MTLCompareFunctionAlways; else diff --git a/native/cocos/renderer/gfx-validator/RenderPassValidator.cpp b/native/cocos/renderer/gfx-validator/RenderPassValidator.cpp index 439e518c96..48fcb8a7ac 100644 --- a/native/cocos/renderer/gfx-validator/RenderPassValidator.cpp +++ b/native/cocos/renderer/gfx-validator/RenderPassValidator.cpp @@ -52,17 +52,6 @@ void RenderPassValidator::doInit(const RenderPassInfo &info) { } } - for (auto &attachment : _colorAttachments) { - if (attachment.loadOp == LoadOp::LOAD && attachment.barrier->getInfo().prevAccesses == AccessFlagBit::NONE) { - // Attachment missing beginAccesses for LoadOp::LOAD. - CC_ABORT(); - } - } - if ((_depthStencilAttachment.depthLoadOp == LoadOp::LOAD || _depthStencilAttachment.stencilLoadOp == LoadOp::LOAD) && _depthStencilAttachment.barrier->getInfo().prevAccesses == AccessFlagBit::NONE) { - // Attachment missing beginAccesses for LoadOp::LOAD. - CC_ABORT(); - } - /////////// execute /////////// _actor->initialize(info); diff --git a/native/cocos/renderer/pipeline/custom/LayoutGraphUtils.cpp b/native/cocos/renderer/pipeline/custom/LayoutGraphUtils.cpp index ba6d31bffa..e76195f772 100644 --- a/native/cocos/renderer/pipeline/custom/LayoutGraphUtils.cpp +++ b/native/cocos/renderer/pipeline/custom/LayoutGraphUtils.cpp @@ -356,13 +356,15 @@ void generateConstantMacros( #define CC_DEVICE_CAN_BENEFIT_FROM_INPUT_ATTACHMENT %d #define CC_PLATFORM_ANDROID_AND_WEBGL 0 #define CC_ENABLE_WEBGL_HIGHP_STRUCT_VALUES 0 +#define CC_JOINT_UNIFORM_CAPACITY %d )", hasAnyFlags(device->getFormatFeatures(gfx::Format::RGBA32F), gfx::FormatFeature::RENDER_TARGET | gfx::FormatFeature::SAMPLED_TEXTURE), clusterEnabled ? 1 : 0, device->getCapabilities().maxVertexUniformVectors, device->getCapabilities().maxFragmentUniformVectors, - device->hasFeature(gfx::Feature::INPUT_ATTACHMENT_BENEFIT)); + device->hasFeature(gfx::Feature::INPUT_ATTACHMENT_BENEFIT), + pipeline::SkinningJointCapacity::jointUniformCapacity); } namespace { diff --git a/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp b/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp index 0434b5c917..948c9fa88b 100644 --- a/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp +++ b/native/cocos/renderer/pipeline/custom/NativeExecutor.cpp @@ -73,7 +73,7 @@ struct RenderGraphVisitorContext { const FrameGraphDispatcher::BarrierMap& barrierMap; const ccstd::pmr::vector& validPasses; gfx::Device* device = nullptr; - cc::gfx::CommandBuffer* cmdBuff = nullptr; + gfx::CommandBuffer* cmdBuff = nullptr; ccstd::pmr::unordered_map< const scene::RenderScene*, ccstd::pmr::unordered_map>& sceneQueues; @@ -82,6 +82,7 @@ struct RenderGraphVisitorContext { boost::container::pmr::memory_resource* scratch = nullptr; gfx::RenderPass* currentPass = nullptr; LayoutGraphData::vertex_descriptor currentPassLayoutID = LayoutGraphData::null_vertex(); + Mat4 currentProjMatrix{}; }; void clear(gfx::RenderPassInfo& info) { @@ -300,7 +301,7 @@ PersistentRenderPassAndFramebuffer& fetchOrCreateFramebuffer( return iter->second; } -gfx::BufferBarrierInfo getBufferBarrier(const cc::render::Barrier& barrier) { +gfx::BufferBarrierInfo getBufferBarrier(const render::Barrier& barrier) { gfx::MemoryUsage memUsage = gfx::MemoryUsage::DEVICE; const auto& beginUsage = get(barrier.beginStatus.usage); @@ -324,7 +325,7 @@ gfx::BufferBarrierInfo getBufferBarrier(const cc::render::Barrier& barrier) { std::pair getTextureBarrier( ResourceGraph& resg, ResourceGraph::vertex_descriptor resID, - const cc::render::Barrier& barrier) { + const render::Barrier& barrier) { gfx::Texture* texture = resg.getTexture(resID); CC_ENSURES(texture); @@ -553,7 +554,7 @@ void uploadUniformBuffer( void initPerPassDescriptorSet( ResourceGraph& resg, gfx::Device* device, - cc::gfx::CommandBuffer* cmdBuff, + gfx::CommandBuffer* cmdBuff, const gfx::DefaultResource& defaultResource, const LayoutGraphData& lg, const PmrFlatMap& resourceIndex, @@ -686,7 +687,7 @@ void initPerPassDescriptorSet( } void updatePerPassDescriptorSet( - cc::gfx::CommandBuffer* cmdBuff, + gfx::CommandBuffer* cmdBuff, const LayoutGraphData& lg, const DescriptorSetData& set, const RenderData& user, @@ -799,6 +800,91 @@ void updateCameraUniformBufferAndDescriptorSet( } } +void submitUICommands( + gfx::RenderPass* renderPass, + uint32_t layoutPassID, + scene::Camera* camera, + gfx::CommandBuffer* cmdBuff) { + const auto& batches = camera->getScene()->getBatches(); + for (auto* batch : batches) { + if (!(camera->getVisibility() & batch->getVisFlags())) { + continue; + } + const auto& passes = batch->getPasses(); + for (size_t i = 0; i < batch->getShaders().size(); ++i) { + const scene::Pass* pass = passes[i]; + if (pass->getPassID() != layoutPassID) { + continue; + } + auto* shader = batch->getShaders()[i]; + auto* inputAssembler = batch->getInputAssembler(); + auto* ds = batch->getDescriptorSet(); + auto* pso = pipeline::PipelineStateManager::getOrCreatePipelineState( + pass, shader, inputAssembler, renderPass); + cmdBuff->bindPipelineState(pso); + cmdBuff->bindDescriptorSet( + static_cast(pipeline::SetIndex::MATERIAL), + pass->getDescriptorSet()); + cmdBuff->bindInputAssembler(inputAssembler); + cmdBuff->bindDescriptorSet( + static_cast(pipeline::SetIndex::LOCAL), ds); + cmdBuff->draw(inputAssembler); + } + } +} + +void submitProfilerCommands( + RenderGraphVisitorContext& ctx, + const RasterPass& rasterPass) { + const auto* profiler = ctx.ppl->getProfiler(); + if (!profiler || !profiler->isEnabled()) { + return; + } + auto* renderPass = ctx.currentPass; + auto* cmdBuff = ctx.cmdBuff; + const auto& submodel = profiler->getSubModels()[0]; + auto* pass = submodel->getPass(0); + auto* ia = submodel->getInputAssembler(); + auto* pso = pipeline::PipelineStateManager::getOrCreatePipelineState( + pass, submodel->getShader(0), ia, renderPass); + + // current pass + RenderData user(ctx.scratch); + NativeSetter setter(ctx.lg, user); + setter.setMat4("cc_matProj", ctx.currentProjMatrix); + + // profiler pass + gfx::Viewport profilerViewport{}; + gfx::Rect profilerScissor{}; + profilerViewport.width = profilerScissor.width = rasterPass.width; + profilerViewport.height = profilerScissor.height = rasterPass.height; + cmdBuff->setViewport(profilerViewport); + cmdBuff->setScissor(profilerScissor); + + cmdBuff->bindPipelineState(pso); + { + auto& layout = get(LayoutGraphData::Layout, ctx.lg, pass->getPassID()); + auto iter = layout.descriptorSets.find(UpdateFrequency::PER_PASS); + if (iter != layout.descriptorSets.end()) { + auto& set = iter->second; + auto& node = ctx.context.layoutGraphResources.at(pass->getPassID()); + PmrFlatMap resourceIndex(ctx.scratch); + initPerPassDescriptorSet( + ctx.resourceGraph, + ctx.device, ctx.cmdBuff, + *ctx.context.defaultResource, ctx.lg, + resourceIndex, set, user, node); + } else { + CC_EXPECTS(false); + // TODO(zhouzhenglong): set descriptor set to empty + } + } + cmdBuff->bindDescriptorSet(static_cast(pipeline::SetIndex::MATERIAL), pass->getDescriptorSet()); + cmdBuff->bindDescriptorSet(static_cast(pipeline::SetIndex::LOCAL), submodel->getDescriptorSet()); + cmdBuff->bindInputAssembler(ia); + cmdBuff->draw(ia); +} + struct RenderGraphVisitor : boost::dfs_visitor<> { void submitBarriers(const std::vector& barriers) const { auto& resg = ctx.resourceGraph; @@ -970,6 +1056,7 @@ struct RenderGraphVisitor : boost::dfs_visitor<> { CC_EXPECTS(camera); if (camera) { // update camera data updateCameraUniformBufferAndDescriptorSet(ctx, sceneID); + ctx.currentProjMatrix = camera->getMatProj(); } const auto* scene = camera->getScene(); const auto& queues = ctx.sceneQueues.at(scene); @@ -996,6 +1083,10 @@ struct RenderGraphVisitor : boost::dfs_visitor<> { ctx.currentPass, ctx.cmdBuff); } } + if (any(sceneData.flags & SceneFlags::UI)) { + submitUICommands(ctx.currentPass, + ctx.currentPassLayoutID, camera, ctx.cmdBuff); + } } void begin(const Blit& blit, RenderGraph::vertex_descriptor vertID) const { const auto& programLib = *dynamic_cast(ctx.programLib); @@ -1003,6 +1094,7 @@ struct RenderGraphVisitor : boost::dfs_visitor<> { CC_EXPECTS(blit.material->getPasses()); if (blit.camera) { updateCameraUniformBufferAndDescriptorSet(ctx, vertID); + ctx.currentProjMatrix = blit.camera->getMatProj(); } // get pass auto& pass = *blit.material->getPasses()->at(static_cast(blit.passID)); @@ -1018,7 +1110,7 @@ struct RenderGraphVisitor : boost::dfs_visitor<> { pass.update(); // get or create program per-instance descriptor set - auto& node = ctx.context.layoutGraphResources.at(vertID); + 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 @@ -1104,9 +1196,10 @@ struct RenderGraphVisitor : boost::dfs_visitor<> { void begin(const gfx::Viewport& pass, RenderGraph::vertex_descriptor vertID) const { } void end(const RasterPass& pass) const { - std::ignore = pass; - auto* cmdBuff = ctx.cmdBuff; - cmdBuff->endRenderPass(); + if (pass.showStatistics) { + submitProfilerCommands(ctx, pass); + } + ctx.cmdBuff->endRenderPass(); ctx.currentPass = nullptr; ctx.currentPassLayoutID = LayoutGraphData::null_vertex(); } @@ -1371,20 +1464,22 @@ float computeSortingDepth(const scene::Camera& camera, const scene::Model& model float depth = 0; if (model.getNode()) { const auto* node = model.getTransform(); - cc::Vec3 position; - cc::Vec3::subtract(node->getWorldPosition(), camera.getPosition(), &position); + Vec3 position; + Vec3::subtract(node->getWorldPosition(), camera.getPosition(), &position); depth = position.dot(camera.getForward()); } return depth; } void addRenderObject( + LayoutGraphData::vertex_descriptor shadowCasterlayoutID, const scene::Camera& camera, const scene::Model& model, NativeRenderQueue& queue) { const bool bDrawTransparent = any(queue.sceneFlags & SceneFlags::TRANSPARENT_OBJECT); bool bDrawOpaqueOrCutout = any(queue.sceneFlags & (SceneFlags::OPAQUE_OBJECT | SceneFlags::CUTOUT_OBJECT)); if (!bDrawTransparent && !bDrawOpaqueOrCutout) { bDrawOpaqueOrCutout = true; } + const bool bDrawShadowCaster = any(queue.sceneFlags & SceneFlags::SHADOW_CASTER); const auto& subModels = model.getSubModels(); const auto subModelCount = subModels.size(); @@ -1396,6 +1491,7 @@ void addRenderObject( auto& pass = *passes[passIdx]; const bool bTransparent = isTransparent(pass); const bool bOpaqueOrCutout = !bTransparent; + const bool bShadowCaster = pass.getPhaseID() == shadowCasterlayoutID; if (!bDrawTransparent && bTransparent) { // skip transparent object @@ -1407,6 +1503,17 @@ void addRenderObject( continue; } + // skip irrelavent passes + if (queue.layoutPassID != pass.getPassID()) { + continue; + } + + // skip shadow caster + if (!bDrawShadowCaster && bShadowCaster) { + continue; + } + + // add object to queue if (pass.getBatchingScheme() == scene::BatchingSchemes::INSTANCING) { auto& instancedBuffer = *pass.getInstancedBuffer(); instancedBuffer.merge(subModel, passIdx); @@ -1428,6 +1535,7 @@ void addRenderObject( } void octreeCulling( + LayoutGraphData::vertex_descriptor shadowCasterlayoutID, const scene::Octree* octree, const scene::RenderScene* scene, const scene::Skybox* skyBox, @@ -1449,7 +1557,7 @@ void octreeCulling( } const auto visibility = camera.getVisibility(); if (isInstanceVisible(model, visibility) && isPointInstanceAndNotSkybox(model, skyBox)) { - addRenderObject(camera, model, queue); + addRenderObject(shadowCasterlayoutID, camera, model, queue); } } @@ -1463,11 +1571,12 @@ void octreeCulling( if (scene->isCulledByLod(&camera, &model)) { continue; } - addRenderObject(camera, model, queue); + addRenderObject(shadowCasterlayoutID, camera, model, queue); } } void frustumCulling( + LayoutGraphData::vertex_descriptor shadowCasterlayoutID, const scene::RenderScene* scene, const scene::Camera& camera, NativeRenderQueue& queue) { @@ -1495,12 +1604,12 @@ void frustumCulling( const auto* modelWorldBounds = model.getWorldBounds(); // object has no volume if (!modelWorldBounds) { - addRenderObject(camera, model, queue); + addRenderObject(shadowCasterlayoutID, camera, model, queue); continue; } // frustum culling if (modelWorldBounds->aabbFrustum(camera.getFrustum())) { - addRenderObject(camera, model, queue); + addRenderObject(shadowCasterlayoutID, camera, model, queue); } } } @@ -1508,6 +1617,7 @@ void frustumCulling( void mergeSceneFlags( const RenderGraph& rg, + const LayoutGraphData& lg, ccstd::pmr::unordered_map< const scene::RenderScene*, ccstd::pmr::unordered_map>& @@ -1516,10 +1626,21 @@ void mergeSceneFlags( if (!holds(vertID, rg)) { continue; } + const auto queueID = parent(vertID, rg); + CC_ENSURES(queueID != RenderGraph::null_vertex()); + const auto passID = parent(queueID, rg); + CC_ENSURES(passID != RenderGraph::null_vertex()); + const auto& layoutName = get(RenderGraph::Layout, rg, passID); + CC_ENSURES(!layoutName.empty()); + const auto layoutID = locate(LayoutGraphData::null_vertex(), layoutName, lg); + CC_ENSURES(layoutID != LayoutGraphData::null_vertex()); + const auto& sceneData = get(SceneTag{}, vertID, rg); const auto* scene = sceneData.camera->getScene(); if (scene) { - sceneQueues[scene][sceneData.camera].sceneFlags |= sceneData.flags; + auto& queue = sceneQueues[scene][sceneData.camera]; + queue.sceneFlags |= sceneData.flags; + queue.layoutPassID = layoutID; } } } @@ -1535,6 +1656,7 @@ void extendResourceLifetime(const NativeRenderQueue& queue, ResourceGroup& group } void buildRenderQueues( + LayoutGraphData::vertex_descriptor shadowCasterlayoutID, const pipeline::PipelineSceneData& sceneData, NativeRenderContext& context, ccstd::pmr::unordered_map< @@ -1569,9 +1691,9 @@ void buildRenderQueues( // culling if (octree && octree->isEnabled()) { - octreeCulling(octree, scene, skybox, *camera, queue); + octreeCulling(shadowCasterlayoutID, octree, scene, skybox, *camera, queue); } else { - frustumCulling(scene, *camera, queue); + frustumCulling(shadowCasterlayoutID, scene, *camera, queue); } queue.sort(); @@ -1622,8 +1744,14 @@ void NativePipeline::executeRenderGraph(const RenderGraph& rg) { ccstd::pmr::unordered_map> sceneQueues(scratch); { - mergeSceneFlags(rg, sceneQueues); - buildRenderQueues(*ppl.getPipelineSceneData(), ppl.nativeContext, sceneQueues); + mergeSceneFlags(rg, lg, sceneQueues); + const auto shadowCasterLayoutID = locate("/default/shadow-caster", lg); + CC_ENSURES(shadowCasterLayoutID != LayoutGraphData::null_vertex()); + buildRenderQueues( + shadowCasterLayoutID, + *ppl.getPipelineSceneData(), + ppl.nativeContext, + sceneQueues); } // Execute all valid passes diff --git a/native/cocos/renderer/pipeline/custom/NativePipelineTypes.cpp b/native/cocos/renderer/pipeline/custom/NativePipelineTypes.cpp index 9b5c8486bf..0edcb08f1a 100644 --- a/native/cocos/renderer/pipeline/custom/NativePipelineTypes.cpp +++ b/native/cocos/renderer/pipeline/custom/NativePipelineTypes.cpp @@ -82,19 +82,21 @@ NativeRenderQueue::NativeRenderQueue(const allocator_type& alloc) noexcept opaqueInstancingQueue(alloc), transparentInstancingQueue(alloc) {} -NativeRenderQueue::NativeRenderQueue(SceneFlags sceneFlagsIn, const allocator_type& alloc) noexcept +NativeRenderQueue::NativeRenderQueue(SceneFlags sceneFlagsIn, uint32_t layoutPassIDIn, const allocator_type& alloc) noexcept : opaqueQueue(alloc), transparentQueue(alloc), opaqueInstancingQueue(alloc), transparentInstancingQueue(alloc), - sceneFlags(sceneFlagsIn) {} + sceneFlags(sceneFlagsIn), + layoutPassID(layoutPassIDIn) {} NativeRenderQueue::NativeRenderQueue(NativeRenderQueue&& rhs, const allocator_type& alloc) : opaqueQueue(std::move(rhs.opaqueQueue), alloc), transparentQueue(std::move(rhs.transparentQueue), alloc), opaqueInstancingQueue(std::move(rhs.opaqueInstancingQueue), alloc), transparentInstancingQueue(std::move(rhs.transparentInstancingQueue), alloc), - sceneFlags(rhs.sceneFlags) {} + sceneFlags(rhs.sceneFlags), + layoutPassID(rhs.layoutPassID) {} DefaultSceneVisitor::DefaultSceneVisitor(const allocator_type& alloc) noexcept : name(alloc) {} diff --git a/native/cocos/renderer/pipeline/custom/NativePipelineTypes.h b/native/cocos/renderer/pipeline/custom/NativePipelineTypes.h index 9d7c1481f4..7de11f0d87 100644 --- a/native/cocos/renderer/pipeline/custom/NativePipelineTypes.h +++ b/native/cocos/renderer/pipeline/custom/NativePipelineTypes.h @@ -113,6 +113,8 @@ public: RasterQueueBuilder *addQueue(QueueHint hint) override; void setViewport(const gfx::Viewport &viewport) override; void setVersion(const ccstd::string &name, uint64_t version) override; + bool getShowStatistics() const override; + void setShowStatistics(bool enable) override; const PipelineRuntime* pipelineRuntime{nullptr}; RenderGraph* renderGraph{nullptr}; @@ -321,7 +323,7 @@ struct NativeRenderQueue { } NativeRenderQueue(const allocator_type& alloc) noexcept; // NOLINT - NativeRenderQueue(SceneFlags sceneFlagsIn, const allocator_type& alloc) noexcept; + NativeRenderQueue(SceneFlags sceneFlagsIn, uint32_t layoutPassIDIn, const allocator_type& alloc) noexcept; NativeRenderQueue(NativeRenderQueue&& rhs, const allocator_type& alloc); NativeRenderQueue(NativeRenderQueue&& rhs) noexcept = default; @@ -336,6 +338,7 @@ struct NativeRenderQueue { RenderInstancingQueue opaqueInstancingQueue; RenderInstancingQueue transparentInstancingQueue; SceneFlags sceneFlags{SceneFlags::NONE}; + uint32_t layoutPassID{0xFFFFFFFF}; }; class DefaultSceneVisitor final : public SceneVisitor { diff --git a/native/cocos/renderer/pipeline/custom/NativeProgramLibrary.cpp b/native/cocos/renderer/pipeline/custom/NativeProgramLibrary.cpp index 5d23c02db1..2ef49ee96b 100644 --- a/native/cocos/renderer/pipeline/custom/NativeProgramLibrary.cpp +++ b/native/cocos/renderer/pipeline/custom/NativeProgramLibrary.cpp @@ -1140,6 +1140,14 @@ void NativeProgramLibrary::init(gfx::Device *deviceIn) { } } + // update ubo + // tips: for compatibility with old version, when maxVertexUniformVectors is 128, maxJoints = 30 + uint maxJoints = (device->getCapabilities().maxVertexUniformVectors - 38) / 3; + maxJoints = maxJoints < 256 ? maxJoints : 256; + pipeline::SkinningJointCapacity::jointUniformCapacity = maxJoints; + pipeline::UBOSkinning::initLayout(maxJoints); + + // init layout graph auto &lg = layoutGraph; for (const auto v : makeRange(vertices(lg))) { auto &layout = get(LayoutGraphData::Layout, lg, v); diff --git a/native/cocos/renderer/pipeline/custom/NativeRenderGraph.cpp b/native/cocos/renderer/pipeline/custom/NativeRenderGraph.cpp index 88a85181d2..62373a0541 100644 --- a/native/cocos/renderer/pipeline/custom/NativeRenderGraph.cpp +++ b/native/cocos/renderer/pipeline/custom/NativeRenderGraph.cpp @@ -286,6 +286,16 @@ void NativeRasterPassBuilder::addComputeView(const ccstd::string &name, const Co iter->second.emplace_back(view); } +bool NativeRasterPassBuilder::getShowStatistics() const { + const auto &pass = get(RasterTag{}, passID, *renderGraph); + return pass.showStatistics; +} + +void NativeRasterPassBuilder::setShowStatistics(bool enable) { + auto &pass = get(RasterTag{}, passID, *renderGraph); + pass.showStatistics = enable; +} + ccstd::string NativeRasterQueueBuilder::getName() const { return std::string(get(RenderGraph::Name, *renderGraph, queueID)); } @@ -296,6 +306,17 @@ void NativeRasterQueueBuilder::setName(const ccstd::string &name) { namespace { +uint8_t getCombineSignY(gfx::Device *device) { + // 0: vk, 1: metal, 2: none, 3: gl-like + static int8_t combineSignY{-1}; + if (combineSignY < 0) { + const float screenSpaceSignY = device->getCapabilities().screenSpaceSignY * 0.5F + 0.5F; + const float clipSpaceSignY = device->getCapabilities().clipSpaceSignY * 0.5F + 0.5F; + combineSignY = static_cast(screenSpaceSignY) << 1 | static_cast(clipSpaceSignY); + } + return static_cast(combineSignY); +} + void setCameraUBOValues( const scene::Camera &camera, const pipeline::PipelineSceneData &cfg, @@ -315,7 +336,7 @@ void setCameraUBOValues( setter.setVec4("cc_cameraPos", Vec4(camera.getPosition().x, camera.getPosition().y, camera.getPosition().z, - pipeline::PipelineUBO::getCombineSignY())); + getCombineSignY(cc::gfx::Device::getInstance()))); setter.setVec4("cc_surfaceTransform", Vec4(static_cast(camera.getSurfaceTransform()), 0.0F, cosf(static_cast(mathutils::toRadian(skybox.getRotationAngle()))), diff --git a/native/cocos/renderer/pipeline/custom/RenderGraphTypes.cpp b/native/cocos/renderer/pipeline/custom/RenderGraphTypes.cpp index dfabba9653..0011c67643 100644 --- a/native/cocos/renderer/pipeline/custom/RenderGraphTypes.cpp +++ b/native/cocos/renderer/pipeline/custom/RenderGraphTypes.cpp @@ -170,7 +170,8 @@ RasterPass::RasterPass(RasterPass&& rhs, const allocator_type& alloc) height(rhs.height), viewport(rhs.viewport), versionName(std::move(rhs.versionName), alloc), - version(rhs.version) {} + version(rhs.version), + showStatistics(rhs.showStatistics) {} RasterPass::RasterPass(RasterPass const& rhs, const allocator_type& alloc) : rasterViews(rhs.rasterViews, alloc), @@ -180,7 +181,8 @@ RasterPass::RasterPass(RasterPass const& rhs, const allocator_type& alloc) height(rhs.height), viewport(rhs.viewport), versionName(rhs.versionName, alloc), - version(rhs.version) {} + version(rhs.version), + showStatistics(rhs.showStatistics) {} ComputePass::ComputePass(const allocator_type& alloc) noexcept : computeViews(alloc) {} diff --git a/native/cocos/renderer/pipeline/custom/RenderGraphTypes.h b/native/cocos/renderer/pipeline/custom/RenderGraphTypes.h index fc29eb43fc..fd60e7344a 100644 --- a/native/cocos/renderer/pipeline/custom/RenderGraphTypes.h +++ b/native/cocos/renderer/pipeline/custom/RenderGraphTypes.h @@ -472,6 +472,7 @@ struct RasterPass { gfx::Viewport viewport; ccstd::pmr::string versionName; uint64_t version{0}; + bool showStatistics{false}; }; inline bool operator==(const RasterPass& lhs, const RasterPass& rhs) noexcept { diff --git a/native/cocos/renderer/pipeline/custom/RenderInterfaceTypes.h b/native/cocos/renderer/pipeline/custom/RenderInterfaceTypes.h index 2989e295e3..df572e3553 100644 --- a/native/cocos/renderer/pipeline/custom/RenderInterfaceTypes.h +++ b/native/cocos/renderer/pipeline/custom/RenderInterfaceTypes.h @@ -173,6 +173,8 @@ public: virtual RasterQueueBuilder *addQueue(QueueHint hint) = 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); } diff --git a/native/tools/swig-config/renderer.i b/native/tools/swig-config/renderer.i index 3cfe67e37c..276e0554fb 100644 --- a/native/tools/swig-config/renderer.i +++ b/native/tools/swig-config/renderer.i @@ -98,6 +98,7 @@ using namespace cc::render; %attribute(cc::render::PipelineRuntime, cc::pipeline::GeometryRenderer*, geometryRenderer, getGeometryRenderer); %attribute(cc::render::PipelineRuntime, float, shadingScale, getShadingScale, setShadingScale); %attribute(cc::render::RenderNode, ccstd::string, name, getName, setName); +%attribute(cc::render::RasterPassBuilder, bool, showStatistics, getShowStatistics, setShowStatistics); %attribute(cc::render::SceneVisitor, cc::pipeline::PipelineSceneData*, pipelineSceneData, getPipelineSceneData); %attribute(cc::render::SceneTask, cc::render::TaskType, taskType, getTaskType);