mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
ForwardPipeline docking with FrameGraph (#3893)
* ForwardPipeline docking with FrameGraph * update * Fix the problem that ui does not display * Modify Rect writing
This commit is contained in:
@@ -45,6 +45,10 @@ namespace {
|
||||
(dst)[(offset) + 3] = (src).w;
|
||||
} // namespace
|
||||
|
||||
framegraph::StringHandle ForwardPipeline::fgStrHandleForwardColorTexture = framegraph::FrameGraph::stringToHandle("forwardColorTexture");
|
||||
framegraph::StringHandle ForwardPipeline::fgStrHandleForwardDepthTexture = framegraph::FrameGraph::stringToHandle("forwardDepthTexture");
|
||||
framegraph::StringHandle ForwardPipeline::fgStrHandleForwardPass = framegraph::FrameGraph::stringToHandle("forwardPass");
|
||||
|
||||
gfx::RenderPass *ForwardPipeline::getOrCreateRenderPass(gfx::ClearFlags clearFlags, gfx::Swapchain *swapchain) {
|
||||
if (_renderPasses.count(clearFlags)) {
|
||||
return _renderPasses[clearFlags];
|
||||
@@ -114,6 +118,27 @@ bool ForwardPipeline::activate(gfx::Swapchain *swapchain) {
|
||||
return true;
|
||||
}
|
||||
|
||||
gfx::Rect ForwardPipeline::getRenderArea(scene::Camera *camera, bool onScreen) {
|
||||
float w;
|
||||
float h;
|
||||
if (onScreen) {
|
||||
gfx::Swapchain *swapchain = camera->window->swapchain;
|
||||
w = static_cast<float>(swapchain && toNumber(swapchain->getSurfaceTransform()) % 2 ? camera->height : camera->width);
|
||||
h = static_cast<float>(swapchain && toNumber(swapchain->getSurfaceTransform()) % 2 ? camera->width : camera->height);
|
||||
} else {
|
||||
w = static_cast<float>(camera->width);
|
||||
h = static_cast<float>(camera->height);
|
||||
}
|
||||
|
||||
const auto &viewport = camera->viewPort;
|
||||
return gfx::Rect{
|
||||
static_cast<int>(viewport.x * w),
|
||||
static_cast<int>(viewport.y * h),
|
||||
static_cast<uint>(viewport.z * w * _pipelineSceneData->getSharedData()->shadingScale),
|
||||
static_cast<uint>(viewport.w * h * _pipelineSceneData->getSharedData()->shadingScale)
|
||||
};
|
||||
}
|
||||
|
||||
void ForwardPipeline::render(const vector<scene::Camera *> &cameras) {
|
||||
_commandBuffers[0]->begin();
|
||||
_pipelineUBO->updateGlobalUBO(cameras[0]);
|
||||
@@ -121,9 +146,12 @@ void ForwardPipeline::render(const vector<scene::Camera *> &cameras) {
|
||||
|
||||
for (auto *camera : cameras) {
|
||||
sceneCulling(this, camera);
|
||||
_fg.reset();
|
||||
for (auto *const flow : _flows) {
|
||||
flow->render(camera);
|
||||
}
|
||||
_fg.compile();
|
||||
_fg.execute();
|
||||
_pipelineUBO->incCameraUBOOffset();
|
||||
}
|
||||
|
||||
|
||||
@@ -27,9 +27,18 @@
|
||||
|
||||
#include <array>
|
||||
#include "../RenderPipeline.h"
|
||||
#include "frame-graph/FrameGraph.h"
|
||||
#include "frame-graph/Handle.h"
|
||||
|
||||
namespace cc {
|
||||
namespace pipeline {
|
||||
|
||||
enum class ForwaedInsertPoint {
|
||||
IP_FORWARD = 100,
|
||||
IP_INVALID
|
||||
};
|
||||
|
||||
|
||||
struct UBOGlobal;
|
||||
struct UBOCamera;
|
||||
struct UBOShadow;
|
||||
@@ -44,6 +53,7 @@ public:
|
||||
bool activate(gfx::Swapchain *swapchain) override;
|
||||
void render(const vector<scene::Camera *> &cameras) override;
|
||||
|
||||
framegraph::FrameGraph &getFrameGraph() { return _fg; }
|
||||
gfx::RenderPass *getOrCreateRenderPass(gfx::ClearFlags clearFlags, gfx::Swapchain *swapchain);
|
||||
|
||||
inline gfx::Buffer * getLightsUBO() const { return _lightsUBO; }
|
||||
@@ -51,6 +61,14 @@ public:
|
||||
inline const gfx::BufferList &getLightBuffers() const { return _lightBuffers; }
|
||||
inline const UintList & getLightIndexOffsets() const { return _lightIndexOffsets; }
|
||||
inline const UintList & getLightIndices() const { return _lightIndices; }
|
||||
inline uint getWidth() const { return _width; }
|
||||
inline uint getHeight() const { return _height; }
|
||||
gfx::Rect getRenderArea(scene::Camera *camera, bool onScreen);
|
||||
|
||||
static framegraph::StringHandle fgStrHandleForwardColorTexture;
|
||||
static framegraph::StringHandle fgStrHandleForwardDepthTexture;
|
||||
|
||||
static framegraph::StringHandle fgStrHandleForwardPass;
|
||||
|
||||
private:
|
||||
bool activeRenderer();
|
||||
@@ -62,6 +80,10 @@ private:
|
||||
UintList _lightIndexOffsets;
|
||||
UintList _lightIndices;
|
||||
unordered_map<gfx::ClearFlags, gfx::RenderPass *> _renderPasses;
|
||||
|
||||
uint _width = 0;
|
||||
uint _height = 0;
|
||||
framegraph::FrameGraph _fg;
|
||||
};
|
||||
|
||||
} // namespace pipeline
|
||||
|
||||
@@ -89,7 +89,7 @@ void ForwardStage::destroy() {
|
||||
RenderStage::destroy();
|
||||
}
|
||||
|
||||
void ForwardStage::render(scene::Camera *camera) {
|
||||
void ForwardStage::dispenseRenderObject2Queues() {
|
||||
_instancedQueue->clear();
|
||||
_batchedQueue->clear();
|
||||
auto * pipeline = static_cast<ForwardPipeline *>(_pipeline);
|
||||
@@ -135,27 +135,49 @@ void ForwardStage::render(scene::Camera *camera) {
|
||||
for (auto *queue : _renderQueues) {
|
||||
queue->sort();
|
||||
}
|
||||
}
|
||||
|
||||
auto *cmdBuff = pipeline->getCommandBuffers()[0];
|
||||
void ForwardStage::recordCommands(scene::Camera *camera, gfx::RenderPass *renderPass) {
|
||||
auto * cmdBuff = _pipeline->getCommandBuffers()[0];
|
||||
uint const globalOffsets[] = {_pipeline->getPipelineUBO()->getCurrentCameraUBOOffset()};
|
||||
cmdBuff->bindDescriptorSet(globalSet, _pipeline->getDescriptorSet(), static_cast<uint>(std::size(globalOffsets)), globalOffsets);
|
||||
if (!_pipeline->getPipelineSceneData()->getRenderObjects().empty()) {
|
||||
_renderQueues[0]->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
_instancedQueue->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
_batchedQueue->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
_additiveLightQueue->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
_planarShadowQueue->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
_renderQueues[1]->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
}
|
||||
_uiPhase->render(camera, renderPass);
|
||||
renderProfiler(renderPass, cmdBuff, _pipeline->getProfiler(), camera->window->swapchain);
|
||||
}
|
||||
|
||||
void ForwardStage::render(scene::Camera *camera) {
|
||||
struct RenderData {
|
||||
framegraph::TextureHandle backBuffer;
|
||||
framegraph::TextureHandle depth;
|
||||
};
|
||||
auto * pipeline = static_cast<ForwardPipeline *>(_pipeline);
|
||||
auto *const sceneData = _pipeline->getPipelineSceneData();
|
||||
auto *const sharedData = sceneData->getSharedData();
|
||||
|
||||
// render area is not oriented
|
||||
_renderArea = pipeline->getRenderArea(camera, false);
|
||||
// Command 'updateBuffer' must be recorded outside render passes, cannot put them in execute lambda
|
||||
dispenseRenderObject2Queues();
|
||||
auto *cmdBuff{ pipeline->getCommandBuffers()[0] };
|
||||
|
||||
_instancedQueue->uploadBuffers(cmdBuff);
|
||||
_batchedQueue->uploadBuffers(cmdBuff);
|
||||
_additiveLightQueue->gatherLightPasses(camera, cmdBuff);
|
||||
_planarShadowQueue->gatherShadowPasses(camera, cmdBuff);
|
||||
|
||||
// render area is not oriented
|
||||
bool flipWH = camera->window->swapchain && static_cast<uint>(camera->window->swapchain->getSurfaceTransform()) % 2;
|
||||
auto w = static_cast<float>(flipWH ? camera->height : camera->width);
|
||||
auto h = static_cast<float>(flipWH ? camera->width : camera->height);
|
||||
_renderArea.x = static_cast<int>(camera->viewPort.x * w);
|
||||
_renderArea.y = static_cast<int>(camera->viewPort.y * h);
|
||||
_renderArea.width = static_cast<uint>(camera->viewPort.z * w * sharedData->shadingScale);
|
||||
_renderArea.height = static_cast<uint>(camera->viewPort.w * h * sharedData->shadingScale);
|
||||
|
||||
if (hasFlag(static_cast<gfx::ClearFlags>(camera->clearFlag), gfx::ClearFlagBit::COLOR)) {
|
||||
auto forwardSetup = [&](framegraph::PassNodeBuilder &builder, RenderData &data) {
|
||||
if (hasFlag(static_cast<gfx::ClearFlags>(camera->clearFlag), gfx::ClearFlagBit::COLOR)) {
|
||||
if (sharedData->isHDR) {
|
||||
srgbToLinear(&_clearColors[0], camera->clearColor);
|
||||
auto scale = sharedData->fpScale / camera->exposure;
|
||||
auto scale{ sharedData->fpScale / camera->exposure };
|
||||
_clearColors[0].x *= scale;
|
||||
_clearColors[0].y *= scale;
|
||||
_clearColors[0].z *= scale;
|
||||
@@ -164,32 +186,66 @@ void ForwardStage::render(scene::Camera *camera) {
|
||||
_clearColors[0].y = camera->clearColor.y;
|
||||
_clearColors[0].z = camera->clearColor.z;
|
||||
}
|
||||
}
|
||||
}
|
||||
_clearColors[0].w = camera->clearColor.w;
|
||||
// color
|
||||
gfx::TextureInfo colorTexInfo {
|
||||
gfx::TextureType::TEX2D,
|
||||
gfx::TextureUsageBit::COLOR_ATTACHMENT | gfx::TextureUsageBit::TRANSFER_SRC,
|
||||
sharedData->isHDR ? gfx::Format::RGBA16F : gfx::Format::RGBA8,
|
||||
camera->window->getWidth(),
|
||||
camera->window->getHeight(),
|
||||
};
|
||||
framegraph::RenderTargetAttachment::Descriptor colorAttachmentInfo;
|
||||
colorAttachmentInfo.usage = framegraph::RenderTargetAttachment::Usage::COLOR;
|
||||
colorAttachmentInfo.clearColor = _clearColors[0];
|
||||
colorAttachmentInfo.loadOp = gfx::LoadOp::CLEAR;
|
||||
|
||||
_clearColors[0].w = camera->clearColor.w;
|
||||
auto clearFlags = static_cast<gfx::ClearFlagBit>(camera->clearFlag);
|
||||
if (!hasFlag(clearFlags, gfx::ClearFlagBit::COLOR)) {
|
||||
if (hasFlag(clearFlags, static_cast<gfx::ClearFlagBit>(skyboxFlag))) {
|
||||
colorAttachmentInfo.loadOp = gfx::LoadOp::DISCARD;
|
||||
} else {
|
||||
colorAttachmentInfo.loadOp = gfx::LoadOp::LOAD;
|
||||
}
|
||||
}
|
||||
colorAttachmentInfo.beginAccesses = {gfx::AccessType::TRANSFER_READ};
|
||||
colorAttachmentInfo.endAccesses = {gfx::AccessType::TRANSFER_READ};
|
||||
data.backBuffer = builder.create<framegraph::Texture>(ForwardPipeline::fgStrHandleForwardColorTexture, colorTexInfo);
|
||||
data.backBuffer = builder.write(data.backBuffer, colorAttachmentInfo);
|
||||
builder.writeToBlackboard(ForwardPipeline::fgStrHandleForwardColorTexture, data.backBuffer);
|
||||
// depth
|
||||
gfx::TextureInfo depthTexInfo {
|
||||
gfx::TextureType::TEX2D,
|
||||
gfx::TextureUsageBit::DEPTH_STENCIL_ATTACHMENT,
|
||||
gfx::Format::DEPTH_STENCIL,
|
||||
camera->window->getWidth(),
|
||||
camera->window->getHeight(),
|
||||
};
|
||||
|
||||
auto * framebuffer = camera->window->frameBuffer;
|
||||
const auto &colorTextures = framebuffer->getColorTextures();
|
||||
framegraph::RenderTargetAttachment::Descriptor depthAttachmentInfo;
|
||||
depthAttachmentInfo.usage = framegraph::RenderTargetAttachment::Usage::DEPTH_STENCIL;
|
||||
depthAttachmentInfo.loadOp = gfx::LoadOp::CLEAR;
|
||||
depthAttachmentInfo.clearDepth = camera->clearDepth;
|
||||
depthAttachmentInfo.clearStencil = camera->clearStencil;
|
||||
depthAttachmentInfo.endAccesses = {gfx::AccessType::DEPTH_STENCIL_ATTACHMENT_WRITE};
|
||||
|
||||
auto *renderPass = camera->window->swapchain
|
||||
? pipeline->getOrCreateRenderPass(static_cast<gfx::ClearFlagBit>(camera->clearFlag), camera->window->swapchain)
|
||||
: framebuffer->getRenderPass();
|
||||
data.depth = builder.create<framegraph::Texture>(ForwardPipeline::fgStrHandleForwardDepthTexture, depthTexInfo);
|
||||
data.depth = builder.write(data.depth, depthAttachmentInfo);
|
||||
builder.writeToBlackboard(ForwardPipeline::fgStrHandleForwardDepthTexture, data.depth);
|
||||
// viewport setup
|
||||
gfx::Viewport viewport{_renderArea.x, _renderArea.y, _renderArea.width, _renderArea.height, 0.F, 1.F};
|
||||
builder.setViewport(viewport, _renderArea);
|
||||
};
|
||||
|
||||
cmdBuff->beginRenderPass(renderPass, framebuffer, _renderArea, _clearColors, camera->clearDepth, camera->clearStencil);
|
||||
auto forwardExec = [this, camera](const RenderData & /*data*/, const framegraph::DevicePassResourceTable &table) {
|
||||
recordCommands(camera, table.getRenderPass());
|
||||
};
|
||||
|
||||
uint const globalOffsets[] = {_pipeline->getPipelineUBO()->getCurrentCameraUBOOffset()};
|
||||
cmdBuff->bindDescriptorSet(globalSet, _pipeline->getDescriptorSet(), static_cast<uint>(std::size(globalOffsets)), globalOffsets);
|
||||
// add pass
|
||||
pipeline->getFrameGraph().addPass<RenderData>(static_cast<uint>(ForwaedInsertPoint::IP_FORWARD), ForwardPipeline::fgStrHandleForwardPass, forwardSetup, forwardExec);
|
||||
pipeline->getFrameGraph().presentFromBlackboard(ForwardPipeline::fgStrHandleForwardColorTexture, camera->window->frameBuffer->getColorTextures()[0]);
|
||||
|
||||
_renderQueues[0]->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
_instancedQueue->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
_batchedQueue->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
_additiveLightQueue->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
_planarShadowQueue->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
_renderQueues[1]->recordCommandBuffer(_device, renderPass, cmdBuff);
|
||||
_uiPhase->render(camera, renderPass);
|
||||
renderProfiler(renderPass, cmdBuff, pipeline->getProfiler(), camera->window->swapchain);
|
||||
|
||||
cmdBuff->endRenderPass();
|
||||
}
|
||||
|
||||
} // namespace pipeline
|
||||
|
||||
@@ -51,6 +51,8 @@ public:
|
||||
void render(scene::Camera *camera) override;
|
||||
|
||||
private:
|
||||
void dispenseRenderObject2Queues();
|
||||
void recordCommands(scene::Camera *camera, gfx::RenderPass *renderPass);
|
||||
static RenderStageInfo initInfo;
|
||||
ForwardPipeline * _forwrdPipeline = nullptr;
|
||||
PlanarShadowQueue * _planarShadowQueue = nullptr;
|
||||
|
||||
Reference in New Issue
Block a user