From a1d63f56eccfcab2aa9ff44e54041ced47c58b9d Mon Sep 17 00:00:00 2001 From: GengineJS <476393671@qq.com> Date: Tue, 12 Oct 2021 14:48:50 +0800 Subject: [PATCH] Pipeline custom zoom fbo size (#3964) * ForwardPipeline custom zoom fbo size * DeferredPipeline custom zoom fbo size --- .../renderer/pipeline/RenderPipeline.cpp | 17 ++++++-- .../cocos/renderer/pipeline/RenderPipeline.h | 1 + .../renderer/pipeline/common/BloomStage.cpp | 42 +++++++++---------- .../pipeline/common/PostProcessStage.cpp | 11 ++--- .../pipeline/deferred/GbufferStage.cpp | 19 +++++---- .../pipeline/deferred/LightingStage.cpp | 23 +++++----- .../pipeline/forward/ForwardStage.cpp | 13 +++--- 7 files changed, 67 insertions(+), 59 deletions(-) diff --git a/native/cocos/renderer/pipeline/RenderPipeline.cpp b/native/cocos/renderer/pipeline/RenderPipeline.cpp index 75bb2d3030..d72339bf78 100644 --- a/native/cocos/renderer/pipeline/RenderPipeline.cpp +++ b/native/cocos/renderer/pipeline/RenderPipeline.cpp @@ -229,10 +229,21 @@ void RenderPipeline::ensureEnoughSize(const vector &cameras) { } } +gfx::Viewport RenderPipeline::getViewport(scene::Camera *camera) { + auto scale{_pipelineSceneData->getSharedData()->shadingScale}; + const gfx::Rect &rect = getRenderArea(camera); + return { + static_cast(rect.x * scale), + static_cast(rect.y * scale), + static_cast(rect.width * scale), + static_cast(rect.height * scale) + }; +} + gfx::Rect RenderPipeline::getRenderArea(scene::Camera *camera) { - auto scale{_pipelineSceneData->getSharedData()->shadingScale}; - auto w{static_cast(camera->window->getWidth()) * scale}; - auto h{static_cast(camera->window->getHeight()) * scale}; + float shadingScale{_pipelineSceneData->getSharedData()->shadingScale}; + float w{static_cast(camera->window->getWidth()) * shadingScale}; + float h{static_cast(camera->window->getHeight()) * shadingScale}; return { static_cast(camera->viewPort.x * w), diff --git a/native/cocos/renderer/pipeline/RenderPipeline.h b/native/cocos/renderer/pipeline/RenderPipeline.h index 800b6b06ab..a14b8c1d88 100644 --- a/native/cocos/renderer/pipeline/RenderPipeline.h +++ b/native/cocos/renderer/pipeline/RenderPipeline.h @@ -88,6 +88,7 @@ public: RenderStage * getRenderstageByName(const String &name) const; gfx::Rect getRenderArea(scene::Camera *camera); + gfx::Viewport getViewport(scene::Camera *camera); void genQuadVertexData(const Vec4 &viewport, float *data); uint getWidth() const { return _width; } uint getHeight() const { return _height; } diff --git a/native/cocos/renderer/pipeline/common/BloomStage.cpp b/native/cocos/renderer/pipeline/common/BloomStage.cpp index dac3339748..a296d9d71f 100644 --- a/native/cocos/renderer/pipeline/common/BloomStage.cpp +++ b/native/cocos/renderer/pipeline/common/BloomStage.cpp @@ -164,7 +164,7 @@ void BloomStage::render(scene::Camera *camera) { auto renderArea = pipeline->getRenderArea(camera); renderArea.width >>= 1; renderArea.height >>= 1; - + float shadingScale{_pipeline->getPipelineSceneData()->getSharedData()->shadingScale}; auto prefilterSetup = [&](framegraph::PassNodeBuilder &builder, PrefilterRenderData &data) { data.sampler = _sampler; // read lightingout as input @@ -174,8 +174,8 @@ void BloomStage::render(scene::Camera *camera) { framegraph::Texture::Descriptor colorTexInfo; colorTexInfo.format = gfx::Format::RGBA16F; colorTexInfo.usage = gfx::TextureUsageBit::COLOR_ATTACHMENT | gfx::TextureUsageBit::SAMPLED; - colorTexInfo.width = pipeline->getWidth(); - colorTexInfo.height = pipeline->getHeight(); + colorTexInfo.width = static_cast(pipeline->getWidth() * shadingScale); + colorTexInfo.height = static_cast(pipeline->getHeight() * shadingScale); data.inputTexHandle = builder.create( RenderPipeline::fgStrHandleOutColorTexture, colorTexInfo); @@ -190,16 +190,15 @@ void BloomStage::render(scene::Camera *camera) { framegraph::Texture::Descriptor colorTexInfo; colorTexInfo.format = gfx::Format::RGBA16F; colorTexInfo.usage = gfx::TextureUsageBit::COLOR_ATTACHMENT | gfx::TextureUsageBit::SAMPLED; - colorTexInfo.width = renderArea.width; - colorTexInfo.height = renderArea.height; + colorTexInfo.width = static_cast(renderArea.width * shadingScale); + colorTexInfo.height = static_cast(renderArea.height * shadingScale); data.outputTexHandle = builder.create(prefilterTexHandle, colorTexInfo); } data.outputTexHandle = builder.write(data.outputTexHandle, colorAttachmentInfo); builder.writeToBlackboard(prefilterTexHandle, data.outputTexHandle); - gfx::Viewport viewport{renderArea.x, renderArea.y, renderArea.width, renderArea.height, 0.F, 1.F}; - builder.setViewport(viewport, renderArea); + builder.setViewport(pipeline->getViewport(camera), renderArea); }; auto prefilterExec = [this, camera](PrefilterRenderData const &data, const framegraph::DevicePassResourceTable &table) { @@ -266,23 +265,22 @@ void BloomStage::render(scene::Camera *camera) { framegraph::Texture::Descriptor colorTexInfo; colorTexInfo.format = gfx::Format::RGBA16F; colorTexInfo.usage = gfx::TextureUsageBit::COLOR_ATTACHMENT | gfx::TextureUsageBit::SAMPLED; - colorTexInfo.width = renderArea.width; - colorTexInfo.height = renderArea.height; + colorTexInfo.width = static_cast(renderArea.width * shadingScale); + colorTexInfo.height = static_cast(renderArea.height * shadingScale); data.outputTexHandle = builder.create(downsampleTexHandles[data.index], colorTexInfo); } data.outputTexHandle = builder.write(data.outputTexHandle, colorAttachmentInfo); builder.writeToBlackboard(downsampleTexHandles[data.index], data.outputTexHandle); - gfx::Viewport viewport{renderArea.x, renderArea.y, renderArea.width, renderArea.height, 0.F, 1.F}; - builder.setViewport(viewport, renderArea); + builder.setViewport(pipeline->getViewport(camera), renderArea); // Update cc_textureSize auto *stage = static_cast(pipeline->getRenderstageByName(STAGE_NAME)); CC_ASSERT(stage != nullptr); data.bloomUBO = stage->getDownsampelUBO()[data.index]; - data.textureSize[0] = static_cast(renderArea.width << 1); - data.textureSize[1] = static_cast(renderArea.height << 1); + data.textureSize[0] = static_cast(static_cast(renderArea.width * shadingScale) << 1); + data.textureSize[1] = static_cast(static_cast(renderArea.height * shadingScale) << 1); }; auto downsampleExec = [this, camera](ScalingSampleRenderData const &data, const framegraph::DevicePassResourceTable &table) { @@ -344,8 +342,8 @@ void BloomStage::render(scene::Camera *camera) { framegraph::Texture::Descriptor colorTexInfo; colorTexInfo.format = gfx::Format::RGBA16F; colorTexInfo.usage = gfx::TextureUsageBit::COLOR_ATTACHMENT | gfx::TextureUsageBit::SAMPLED; - colorTexInfo.width = renderArea.width; - colorTexInfo.height = renderArea.height; + colorTexInfo.width = static_cast(renderArea.width * shadingScale); + colorTexInfo.height = static_cast(renderArea.height * shadingScale); data.outputTexHandle = builder.create( upsampleTexHandles[data.index], colorTexInfo); @@ -353,15 +351,14 @@ void BloomStage::render(scene::Camera *camera) { data.outputTexHandle = builder.write(data.outputTexHandle, colorAttachmentInfo); builder.writeToBlackboard(upsampleTexHandles[data.index], data.outputTexHandle); - gfx::Viewport viewport{renderArea.x, renderArea.y, renderArea.width, renderArea.height, 0.F, 1.F}; - builder.setViewport(viewport, renderArea); + builder.setViewport(pipeline->getViewport(camera), renderArea); // Update cc_textureSize auto *stage = static_cast(pipeline->getRenderstageByName(STAGE_NAME)); CC_ASSERT(stage != nullptr); data.bloomUBO = stage->getUpsampleUBO()[data.index]; - data.textureSize[0] = static_cast(renderArea.width >> 1); - data.textureSize[1] = static_cast(renderArea.height >> 1); + data.textureSize[0] = static_cast(static_cast(renderArea.width * shadingScale) >> 1); + data.textureSize[1] = static_cast(static_cast(renderArea.height * shadingScale) >> 1); }; auto upsampleExec = [this, camera](ScalingSampleRenderData const &data, const framegraph::DevicePassResourceTable &table) { @@ -427,8 +424,8 @@ void BloomStage::render(scene::Camera *camera) { framegraph::Texture::Descriptor colorTexInfo; colorTexInfo.format = gfx::Format::RGBA16F; colorTexInfo.usage = gfx::TextureUsageBit::COLOR_ATTACHMENT | gfx::TextureUsageBit::SAMPLED; - colorTexInfo.width = renderArea.width; - colorTexInfo.height = renderArea.height; + colorTexInfo.width = static_cast(renderArea.width * shadingScale); + colorTexInfo.height = static_cast(renderArea.height * shadingScale); data.bloomOutTexHandle = builder.create( RenderPipeline::fgStrHandleBloomOutTexture, colorTexInfo); @@ -436,8 +433,7 @@ void BloomStage::render(scene::Camera *camera) { data.bloomOutTexHandle = builder.write(data.bloomOutTexHandle, colorAttachmentInfo); builder.writeToBlackboard(RenderPipeline::fgStrHandleBloomOutTexture, data.bloomOutTexHandle); - gfx::Viewport viewport{renderArea.x, renderArea.y, renderArea.width, renderArea.height, 0.F, 1.F}; - builder.setViewport(viewport, renderArea); + builder.setViewport(pipeline->getViewport(camera), renderArea); }; auto combineExec = [this, camera](CombineRenderData const &data, const framegraph::DevicePassResourceTable &table) { diff --git a/native/cocos/renderer/pipeline/common/PostProcessStage.cpp b/native/cocos/renderer/pipeline/common/PostProcessStage.cpp index 7678eb3893..c96c494c11 100644 --- a/native/cocos/renderer/pipeline/common/PostProcessStage.cpp +++ b/native/cocos/renderer/pipeline/common/PostProcessStage.cpp @@ -111,6 +111,7 @@ void PostProcessStage::render(scene::Camera *camera) { _clearColors[0].w = camera->clearColor.w; auto *pipeline = _pipeline; + float shadingScale{_pipeline->getPipelineSceneData()->getSharedData()->shadingScale}; auto postSetup = [&](framegraph::PassNodeBuilder &builder, RenderData &data) { if (pipeline->getBloomEnable()) { data.outColorTex = framegraph::TextureHandle(builder.readFromBlackboard(RenderPipeline::fgStrHandleBloomOutTexture)); @@ -122,8 +123,8 @@ void PostProcessStage::render(scene::Camera *camera) { framegraph::Texture::Descriptor colorTexInfo; colorTexInfo.format = gfx::Format::RGBA16F; colorTexInfo.usage = gfx::TextureUsageBit::COLOR_ATTACHMENT | gfx::TextureUsageBit::SAMPLED; - colorTexInfo.width = pipeline->getWidth(); - colorTexInfo.height = pipeline->getHeight(); + colorTexInfo.width = static_cast(pipeline->getWidth() * shadingScale); + colorTexInfo.height = static_cast(pipeline->getHeight() * shadingScale); data.outColorTex = builder.create(RenderPipeline::fgStrHandleOutColorTexture, colorTexInfo); } @@ -152,8 +153,8 @@ void PostProcessStage::render(scene::Camera *camera) { gfx::TextureType::TEX2D, gfx::TextureUsageBit::COLOR_ATTACHMENT, gfx::Format::RGBA8, - camera->window->getWidth(), - camera->window->getHeight(), + static_cast(camera->window->getWidth() * shadingScale), + static_cast(camera->window->getHeight() * shadingScale), }; data.backBuffer = builder.create(fgStrHandlePostProcessOutTexture, textureInfo); data.backBuffer = builder.write(data.backBuffer, colorAttachmentInfo); @@ -216,7 +217,7 @@ void PostProcessStage::render(scene::Camera *camera) { // add pass pipeline->getFrameGraph().addPass(static_cast(CommonInsertPoint::DIP_POSTPROCESS), RenderPipeline::fgStrHandlePostprocessPass, postSetup, postExec); - pipeline->getFrameGraph().presentFromBlackboard(fgStrHandlePostProcessOutTexture, camera->window->frameBuffer->getColorTextures()[0]); + pipeline->getFrameGraph().presentFromBlackboard(fgStrHandlePostProcessOutTexture, camera->window->frameBuffer->getColorTextures()[0], shadingScale == 1.F); } } // namespace pipeline diff --git a/native/cocos/renderer/pipeline/deferred/GbufferStage.cpp b/native/cocos/renderer/pipeline/deferred/GbufferStage.cpp index bf2232b769..eb395ceaf8 100644 --- a/native/cocos/renderer/pipeline/deferred/GbufferStage.cpp +++ b/native/cocos/renderer/pipeline/deferred/GbufferStage.cpp @@ -146,8 +146,9 @@ void GbufferStage::render(scene::Camera *camera) { framegraph::TextureHandle depth; }; - auto *pipeline = static_cast(_pipeline); - _renderArea = pipeline->getRenderArea(camera); + auto *pipeline = static_cast(_pipeline); + float shadingScale{_pipeline->getPipelineSceneData()->getSharedData()->shadingScale}; + _renderArea = pipeline->getRenderArea(camera); // render area is not oriented, copy buffer must be called outsize of RenderPass, it should not be called in execute lambda expression // If there are only transparent object, lighting pass is ignored, we should call getIAByRenderArea here @@ -161,15 +162,15 @@ void GbufferStage::render(scene::Camera *camera) { gfx::TextureType::TEX2D, gfx::TextureUsageBit::COLOR_ATTACHMENT | gfx::TextureUsageBit::INPUT_ATTACHMENT, gfx::Format::RGBA8, - pipeline->getWidth(), - pipeline->getHeight(), + static_cast(pipeline->getWidth() * shadingScale), + static_cast(pipeline->getHeight() * shadingScale), }; gfx::TextureInfo gbufferInfoFloat = { gfx::TextureType::TEX2D, gfx::TextureUsageBit::COLOR_ATTACHMENT | gfx::TextureUsageBit::INPUT_ATTACHMENT, gfx::Format::RGBA16F, - pipeline->getWidth(), - pipeline->getHeight(), + static_cast(pipeline->getWidth() * shadingScale), + static_cast(pipeline->getHeight() * shadingScale), }; for (int i = 0; i < DeferredPipeline::GBUFFER_COUNT - 1; ++i) { if (i != 0) { // positions & normals need more precision @@ -205,8 +206,8 @@ void GbufferStage::render(scene::Camera *camera) { gfx::TextureType::TEX2D, gfx::TextureUsageBit::DEPTH_STENCIL_ATTACHMENT, gfx::Format::DEPTH_STENCIL, - pipeline->getWidth(), - pipeline->getHeight(), + static_cast(pipeline->getWidth() * shadingScale), + static_cast(pipeline->getHeight() * shadingScale), }; data.depth = builder.create(DeferredPipeline::fgStrHandleOutDepthTexture, depthTexInfo); @@ -220,7 +221,7 @@ void GbufferStage::render(scene::Camera *camera) { builder.writeToBlackboard(DeferredPipeline::fgStrHandleOutDepthTexture, data.depth); // viewport setup - builder.setViewport(_renderArea); + builder.setViewport(pipeline->getViewport(camera), _renderArea); }; auto gbufferExec = [this](const RenderData & /*data*/, const framegraph::DevicePassResourceTable &table) { diff --git a/native/cocos/renderer/pipeline/deferred/LightingStage.cpp b/native/cocos/renderer/pipeline/deferred/LightingStage.cpp index aded853f2f..ef16e7705a 100644 --- a/native/cocos/renderer/pipeline/deferred/LightingStage.cpp +++ b/native/cocos/renderer/pipeline/deferred/LightingStage.cpp @@ -339,7 +339,7 @@ void LightingStage::fgLightingPass(scene::Camera *camera) { auto * pipeline = static_cast(_pipeline); gfx::Color clearColor = pipeline->getClearcolor(camera); - + float shadingScale{_pipeline->getPipelineSceneData()->getSharedData()->shadingScale}; auto lightingSetup = [&](framegraph::PassNodeBuilder &builder, RenderData &data) { builder.subpass(true); @@ -374,8 +374,8 @@ void LightingStage::fgLightingPass(scene::Camera *camera) { framegraph::Texture::Descriptor colorTexInfo; colorTexInfo.format = gfx::Format::RGBA16F; colorTexInfo.usage = gfx::TextureUsageBit::COLOR_ATTACHMENT | gfx::TextureUsageBit::SAMPLED; - colorTexInfo.width = pipeline->getWidth(); - colorTexInfo.height = pipeline->getHeight(); + colorTexInfo.width = static_cast(pipeline->getWidth() * shadingScale); + colorTexInfo.height = static_cast(pipeline->getHeight() * shadingScale); data.outputTex = builder.create(RenderPipeline::fgStrHandleOutColorTexture, colorTexInfo); framegraph::RenderTargetAttachment::Descriptor colorAttachmentInfo; @@ -386,9 +386,8 @@ void LightingStage::fgLightingPass(scene::Camera *camera) { colorAttachmentInfo.endAccesses = {gfx::AccessType::FRAGMENT_SHADER_READ_TEXTURE}; data.outputTex = builder.write(data.outputTex, colorAttachmentInfo); builder.writeToBlackboard(RenderPipeline::fgStrHandleOutColorTexture, data.outputTex); - // set render area - builder.setViewport(pipeline->getRenderArea(camera)); + builder.setViewport(pipeline->getViewport(camera), pipeline->getRenderArea(camera)); }; auto lightingExec = [this, camera](RenderData const &data, const framegraph::DevicePassResourceTable &table) { @@ -444,7 +443,7 @@ void LightingStage::fgTransparent(scene::Camera *camera) { auto * pipeline = static_cast(_pipeline); gfx::Color clearColor = pipeline->getClearcolor(camera); - + float shadingScale{_pipeline->getPipelineSceneData()->getSharedData()->shadingScale}; auto transparentSetup = [&](framegraph::PassNodeBuilder &builder, RenderData &data) { // write to lighting output framegraph::RenderTargetAttachment::Descriptor colorAttachmentInfo; @@ -459,8 +458,8 @@ void LightingStage::fgTransparent(scene::Camera *camera) { framegraph::Texture::Descriptor colorTexInfo; colorTexInfo.format = gfx::Format::RGBA16F; colorTexInfo.usage = gfx::TextureUsageBit::COLOR_ATTACHMENT | gfx::TextureUsageBit::SAMPLED; - colorTexInfo.width = pipeline->getWidth(); - colorTexInfo.height = pipeline->getHeight(); + colorTexInfo.width = static_cast(pipeline->getWidth() * shadingScale); + colorTexInfo.height = static_cast(pipeline->getHeight() * shadingScale); colorAttachmentInfo.loadOp = gfx::LoadOp::CLEAR; colorAttachmentInfo.clearColor = clearColor; @@ -483,8 +482,8 @@ void LightingStage::fgTransparent(scene::Camera *camera) { gfx::TextureType::TEX2D, gfx::TextureUsageBit::DEPTH_STENCIL_ATTACHMENT, gfx::Format::DEPTH_STENCIL, - pipeline->getWidth(), - pipeline->getHeight(), + static_cast(pipeline->getWidth() * shadingScale), + static_cast(pipeline->getHeight() * shadingScale), }; data.depth = builder.create(DeferredPipeline::fgStrHandleOutDepthTexture, depthTexInfo); } @@ -492,7 +491,7 @@ void LightingStage::fgTransparent(scene::Camera *camera) { builder.writeToBlackboard(DeferredPipeline::fgStrHandleOutDepthTexture, data.depth); // set render area - builder.setViewport(pipeline->getRenderArea(camera)); + builder.setViewport(pipeline->getViewport(camera), pipeline->getRenderArea(camera)); }; auto transparentExec = [this](RenderData const & /*data*/, const framegraph::DevicePassResourceTable &table) { @@ -775,7 +774,7 @@ void LightingStage::fgSsprPass(scene::Camera *camera) { data.depth = builder.write(framegraph::TextureHandle(builder.readFromBlackboard(DeferredPipeline::fgStrHandleOutDepthTexture)), depthAttachmentInfo); builder.writeToBlackboard(DeferredPipeline::fgStrHandleOutDepthTexture, data.depth); - builder.setViewport(pipeline->getRenderArea(camera)); + builder.setViewport(pipeline->getViewport(camera), pipeline->getRenderArea(camera)); }; auto renderExec = [this](DataRender const &data, const framegraph::DevicePassResourceTable &table) { diff --git a/native/cocos/renderer/pipeline/forward/ForwardStage.cpp b/native/cocos/renderer/pipeline/forward/ForwardStage.cpp index 33d9bf3711..2520059048 100644 --- a/native/cocos/renderer/pipeline/forward/ForwardStage.cpp +++ b/native/cocos/renderer/pipeline/forward/ForwardStage.cpp @@ -140,6 +140,7 @@ void ForwardStage::render(scene::Camera *camera) { auto *const sceneData = _pipeline->getPipelineSceneData(); auto *const sharedData = sceneData->getSharedData(); + float shadingScale{_pipeline->getPipelineSceneData()->getSharedData()->shadingScale}; _renderArea = pipeline->getRenderArea(camera); // Command 'updateBuffer' must be recorded outside render passes, cannot put them in execute lambda dispenseRenderObject2Queues(); @@ -150,7 +151,6 @@ void ForwardStage::render(scene::Camera *camera) { _batchedQueue->uploadBuffers(cmdBuff); _additiveLightQueue->gatherLightPasses(camera, cmdBuff); _planarShadowQueue->gatherShadowPasses(camera, cmdBuff); - auto forwardSetup = [&](framegraph::PassNodeBuilder &builder, RenderData &data) { if (hasFlag(static_cast(camera->clearFlag), gfx::ClearFlagBit::COLOR)) { if (sharedData->isHDR) { @@ -166,8 +166,8 @@ void ForwardStage::render(scene::Camera *camera) { framegraph::Texture::Descriptor colorTexInfo; colorTexInfo.format = sharedData->isHDR ? gfx::Format::RGBA16F : gfx::Format::RGBA8; colorTexInfo.usage = gfx::TextureUsageBit::COLOR_ATTACHMENT | gfx::TextureUsageBit::SAMPLED; - colorTexInfo.width = pipeline->getWidth(); - colorTexInfo.height = pipeline->getHeight(); + colorTexInfo.width = static_cast(pipeline->getWidth() * shadingScale); + colorTexInfo.height = static_cast(pipeline->getHeight() * shadingScale); data.outputTex = builder.create(RenderPipeline::fgStrHandleOutColorTexture, colorTexInfo); framegraph::RenderTargetAttachment::Descriptor colorAttachmentInfo; colorAttachmentInfo.usage = framegraph::RenderTargetAttachment::Usage::COLOR; @@ -191,8 +191,8 @@ void ForwardStage::render(scene::Camera *camera) { gfx::TextureType::TEX2D, gfx::TextureUsageBit::DEPTH_STENCIL_ATTACHMENT, gfx::Format::DEPTH_STENCIL, - camera->window->getWidth(), - camera->window->getHeight(), + static_cast(camera->window->getWidth() * shadingScale), + static_cast(camera->window->getHeight() * shadingScale), }; framegraph::RenderTargetAttachment::Descriptor depthAttachmentInfo; @@ -205,8 +205,7 @@ void ForwardStage::render(scene::Camera *camera) { data.depth = builder.create(RenderPipeline::fgStrHandleOutDepthTexture, depthTexInfo); data.depth = builder.write(data.depth, depthAttachmentInfo); builder.writeToBlackboard(RenderPipeline::fgStrHandleOutDepthTexture, data.depth); - - builder.setViewport(_renderArea); + builder.setViewport(pipeline->getViewport(camera), _renderArea); }; auto offset = _pipeline->getPipelineUBO()->getCurrentCameraUBOOffset();