Update bloom (#3970)

* Update bloom

* Remove bloom pass for UI camera
This commit is contained in:
Kun Lin
2021-10-13 16:59:31 +08:00
committed by GitHub
parent 134914b53d
commit 528b9b0b99
6 changed files with 103 additions and 69 deletions
@@ -84,7 +84,6 @@ public:
inline PipelineUBO * getPipelineUBO() const { return _pipelineUBO; }
inline const String & getConstantMacros() const { return _constantMacros; }
inline gfx::Device * getDevice() const { return _device; }
inline bool getBloomEnable() const { return _bloomEnable; }
RenderStage * getRenderstageByName(const String &name) const;
gfx::Rect getRenderArea(scene::Camera *camera);
@@ -105,6 +104,9 @@ public:
inline bool getClusterEnabled() const { return _clusterEnabled; }
inline void setClusterEnabled(bool enable) { _clusterEnabled = enable; }
inline bool getBloomEnabled() const { return _bloomEnabled; }
inline void setBloomEnabled(bool enable) { _bloomEnabled = enable; }
protected:
static RenderPipeline *instance;
@@ -138,7 +140,7 @@ protected:
// use cluster culling or not
bool _clusterEnabled{false};
bool _bloomEnable{false};
bool _bloomEnabled{false};
};
} // namespace pipeline
@@ -40,25 +40,26 @@
#include "gfx-base/states/GFXSampler.h"
#include "pipeline/Define.h"
#include "scene/SubModel.h"
#include "scene/RenderScene.h"
namespace cc {
namespace pipeline {
namespace {
const String STAGE_NAME = "BloomStage";
const String BLOOM_STAGE_NAME = "BloomStage";
framegraph::StringHandle prefilterTexHandle = framegraph::FrameGraph::stringToHandle("prefilterTex");
framegraph::StringHandle downsampleTexHandles[BloomStage::MAX_SCALING_SAMPLE_PASS_NUM];
framegraph::StringHandle upsampleTexHandles[BloomStage::MAX_SCALING_SAMPLE_PASS_NUM];
framegraph::StringHandle downsampleTexHandles[MAX_BLOOM_FILTER_PASS_NUM];
framegraph::StringHandle upsampleTexHandles[MAX_BLOOM_FILTER_PASS_NUM];
framegraph::StringHandle prefilterPassHandle;
framegraph::StringHandle downsamplePassHandles[BloomStage::MAX_SCALING_SAMPLE_PASS_NUM];
framegraph::StringHandle upsamplePassHandles[BloomStage::MAX_SCALING_SAMPLE_PASS_NUM];
framegraph::StringHandle downsamplePassHandles[MAX_BLOOM_FILTER_PASS_NUM];
framegraph::StringHandle upsamplePassHandles[MAX_BLOOM_FILTER_PASS_NUM];
framegraph::StringHandle combinePassHandle;
void initStrHandle() {
prefilterPassHandle = framegraph::FrameGraph::stringToHandle("bloomPrefilterPass");
std::string tmp;
for (int i = 0; i < BloomStage::MAX_SCALING_SAMPLE_PASS_NUM; ++i) {
for (int i = 0; i < MAX_BLOOM_FILTER_PASS_NUM; ++i) {
tmp = std::string("bloomDownsamplePass") + std::to_string(i);
downsamplePassHandles[i] = framegraph::FrameGraph::stringToHandle(tmp.c_str());
@@ -77,7 +78,7 @@ void initStrHandle() {
} // namespace
RenderStageInfo BloomStage::initInfo = {
STAGE_NAME,
BLOOM_STAGE_NAME,
static_cast<uint>(DeferredStagePriority::BLOOM),
0,
{{true, RenderQueueSortMode::BACK_TO_FRONT, {"default"}}},
@@ -95,14 +96,15 @@ bool BloomStage::initialize(const RenderStageInfo &info) {
void BloomStage::activate(RenderPipeline *pipeline, RenderFlow *flow) {
RenderStage::activate(pipeline, flow);
if (!_pipeline->getBloomEnable()) return;
if (!_pipeline->getBloomEnabled()) return;
_phaseID = getPhaseID("default");
for (int i = 0; i < BloomStage::MAX_SCALING_SAMPLE_PASS_NUM; ++i) {
_prefilterUBO = _device->createBuffer({gfx::BufferUsage::UNIFORM, gfx::MemoryUsage::DEVICE | gfx::MemoryUsage::HOST, UBOBloom::SIZE});
_combineUBO = _device->createBuffer({gfx::BufferUsage::UNIFORM, gfx::MemoryUsage::DEVICE | gfx::MemoryUsage::HOST, UBOBloom::SIZE});
for (int i = 0; i < MAX_BLOOM_FILTER_PASS_NUM; ++i) {
_downsampleUBO[i] = _device->createBuffer({gfx::BufferUsage::UNIFORM, gfx::MemoryUsage::DEVICE | gfx::MemoryUsage::HOST, UBOBloom::SIZE});
_upsampleUBO[i] = _device->createBuffer({gfx::BufferUsage::UNIFORM, gfx::MemoryUsage::DEVICE | gfx::MemoryUsage::HOST, UBOBloom::SIZE});
_upsampleUBO[i] = _device->createBuffer({gfx::BufferUsage::UNIFORM, gfx::MemoryUsage::DEVICE | gfx::MemoryUsage::HOST, UBOBloom::SIZE});
}
gfx::SamplerInfo info{
@@ -117,27 +119,19 @@ void BloomStage::activate(RenderPipeline *pipeline, RenderFlow *flow) {
}
void BloomStage::destroy() {
for (int i = 0; i < BloomStage::MAX_SCALING_SAMPLE_PASS_NUM; ++i) {
CC_SAFE_DELETE(_prefilterUBO);
CC_SAFE_DELETE(_combineUBO);
for (int i = 0; i < MAX_BLOOM_FILTER_PASS_NUM; ++i) {
CC_SAFE_DELETE(_downsampleUBO[i]);
CC_SAFE_DELETE(_upsampleUBO[i]);
}
}
int calcScalingFilterPassNum() {
/*
* The down/upsample pass number should be calculated using the data passed by user
* through the camera, but for now we always return 2.
*/
return 2;
}
void BloomStage::render(scene::Camera *camera) {
auto *pipeline = _pipeline;
if (!pipeline->getBloomEnable()) return;
int scalingSampleNum = calcScalingFilterPassNum();
CC_ASSERT(pipeline != nullptr);
if (!pipeline->getBloomEnabled() || !camera->scene->getDrawBatch2Ds().empty()) return;
if (hasFlag(static_cast<gfx::ClearFlags>(camera->clearFlag), gfx::ClearFlagBit::COLOR)) {
_clearColors[0].x = camera->clearColor.x;
@@ -159,13 +153,21 @@ void BloomStage::render(scene::Camera *camera) {
framegraph::TextureHandle inputTexHandle;
framegraph::TextureHandle outputTexHandle;
gfx::Sampler * sampler;
gfx::Buffer * bloomUBO;
float textureSize[4];
};
auto *stage = static_cast<BloomStage *>(pipeline->getRenderstageByName(BLOOM_STAGE_NAME));
CC_ASSERT(stage != nullptr);
int iterations = stage->getIterations();
float intensity = stage->getIntensity();
float threshold = stage->getThreshold();
auto renderArea = pipeline->getRenderArea(camera);
renderArea.width >>= 1;
renderArea.height >>= 1;
float shadingScale{_pipeline->getPipelineSceneData()->getSharedData()->shadingScale};
auto prefilterSetup = [&](framegraph::PassNodeBuilder &builder, PrefilterRenderData &data) {
auto prefilterSetup = [&](framegraph::PassNodeBuilder &builder, PrefilterRenderData &data) {
data.sampler = _sampler;
// read lightingout as input
data.inputTexHandle = framegraph::TextureHandle(
@@ -199,10 +201,13 @@ void BloomStage::render(scene::Camera *camera) {
builder.writeToBlackboard(prefilterTexHandle, data.outputTexHandle);
builder.setViewport(pipeline->getViewport(camera), renderArea);
// Update threshold
data.bloomUBO = stage->getPrefilterUBO();
data.textureSize[2] = threshold;
};
auto prefilterExec = [this, camera](PrefilterRenderData const &data, const framegraph::DevicePassResourceTable &table) {
//auto * pipeline = static_cast<DeferredPipeline *>(RenderPipeline::getInstance());
auto * pipeline = _pipeline;
gfx::RenderPass *renderPass = table.getRenderPass();
@@ -219,8 +224,11 @@ void BloomStage::render(scene::Camera *camera) {
pass, shader, inputAssembler, renderPass);
CC_ASSERT(pso != nullptr);
pass->getDescriptorSet()->bindTexture(0, table.getRead(data.inputTexHandle));
pass->getDescriptorSet()->bindSampler(0, data.sampler);
data.bloomUBO->update(data.textureSize, sizeof(data.textureSize));
pass->getDescriptorSet()->bindBuffer(0, data.bloomUBO);
pass->getDescriptorSet()->bindTexture(1, table.getRead(data.inputTexHandle));
pass->getDescriptorSet()->bindSampler(1, data.sampler);
pass->getDescriptorSet()->update();
cmdBf->bindDescriptorSet(materialSet, pass->getDescriptorSet());
@@ -240,7 +248,7 @@ void BloomStage::render(scene::Camera *camera) {
int index;
};
// downsample pass
for (int i = 0; i < scalingSampleNum; ++i) {
for (int i = 0; i < iterations; ++i) {
renderArea.width >>= 1;
renderArea.height >>= 1;
@@ -276,8 +284,6 @@ void BloomStage::render(scene::Camera *camera) {
builder.setViewport(pipeline->getViewport(camera), renderArea);
// Update cc_textureSize
auto *stage = static_cast<BloomStage *>(pipeline->getRenderstageByName(STAGE_NAME));
CC_ASSERT(stage != nullptr);
data.bloomUBO = stage->getDownsampelUBO()[data.index];
data.textureSize[0] = static_cast<float>(static_cast<uint>(renderArea.width * shadingScale) << 1);
data.textureSize[1] = static_cast<float>(static_cast<uint>(renderArea.height * shadingScale) << 1);
@@ -292,7 +298,7 @@ void BloomStage::render(scene::Camera *camera) {
cmdBf->bindDescriptorSet(globalSet, pipeline->getDescriptorSet(), utils::toUint(globalOffsets.size()), globalOffsets.data());
auto *const sharedData = pipeline->getPipelineSceneData()->getSharedData();
scene::Pass * pass = sharedData->bloomDownsamplePass;
scene::Pass * pass = sharedData->bloomDownsamplePass[data.index];
gfx::Shader * shader = sharedData->bloomDownsamplePassShader;
auto rendeArea = pipeline->getRenderArea(camera);
gfx::InputAssembler *inputAssembler = pipeline->getIAByRenderArea(rendeArea);
@@ -317,7 +323,7 @@ void BloomStage::render(scene::Camera *camera) {
}
// upsample pass
for (int i = 0; i < scalingSampleNum; ++i) {
for (int i = 0; i < iterations; ++i) {
renderArea.width <<= 1;
renderArea.height <<= 1;
@@ -327,8 +333,8 @@ void BloomStage::render(scene::Camera *camera) {
// read from last downsample texture or last upsample texture
if (data.index == 0) {
data.inputTexHandle = builder.read(framegraph::TextureHandle(
builder.readFromBlackboard(downsampleTexHandles[scalingSampleNum - 1])));
builder.writeToBlackboard(downsampleTexHandles[scalingSampleNum - 1], data.inputTexHandle);
builder.readFromBlackboard(downsampleTexHandles[iterations - 1])));
builder.writeToBlackboard(downsampleTexHandles[iterations - 1], data.inputTexHandle);
} else {
data.inputTexHandle = builder.read(framegraph::TextureHandle(
builder.readFromBlackboard(upsampleTexHandles[data.index - 1])));
@@ -354,8 +360,6 @@ void BloomStage::render(scene::Camera *camera) {
builder.setViewport(pipeline->getViewport(camera), renderArea);
// Update cc_textureSize
auto *stage = static_cast<BloomStage *>(pipeline->getRenderstageByName(STAGE_NAME));
CC_ASSERT(stage != nullptr);
data.bloomUBO = stage->getUpsampleUBO()[data.index];
data.textureSize[0] = static_cast<float>(static_cast<uint>(renderArea.width * shadingScale) >> 1);
data.textureSize[1] = static_cast<float>(static_cast<uint>(renderArea.height * shadingScale) >> 1);
@@ -370,7 +374,7 @@ void BloomStage::render(scene::Camera *camera) {
cmdBf->bindDescriptorSet(globalSet, pipeline->getDescriptorSet(), utils::toUint(globalOffsets.size()), globalOffsets.data());
auto *const sharedData = pipeline->getPipelineSceneData()->getSharedData();
scene::Pass * pass = sharedData->bloomUpsamplePass;
scene::Pass * pass = sharedData->bloomUpsamplePass[data.index];
gfx::Shader * shader = sharedData->bloomUpsamplePassShader;
auto rendeArea = pipeline->getRenderArea(camera);
gfx::InputAssembler *inputAssembler = pipeline->getIAByRenderArea(rendeArea);
@@ -400,6 +404,8 @@ void BloomStage::render(scene::Camera *camera) {
framegraph::TextureHandle upsampleTexHandle;
framegraph::TextureHandle bloomOutTexHandle;
gfx::Sampler * sampler;
gfx::Buffer * bloomUBO;
float textureSize[4];
};
renderArea.width <<= 1;
@@ -414,8 +420,8 @@ void BloomStage::render(scene::Camera *camera) {
builder.writeToBlackboard(RenderPipeline::fgStrHandleOutColorTexture, data.lightingOutTexHandle);
data.upsampleTexHandle = builder.read(framegraph::TextureHandle(
builder.readFromBlackboard(upsampleTexHandles[scalingSampleNum - 1])));
builder.writeToBlackboard(upsampleTexHandles[scalingSampleNum - 1], data.upsampleTexHandle);
builder.readFromBlackboard(upsampleTexHandles[iterations - 1])));
builder.writeToBlackboard(upsampleTexHandles[iterations - 1], data.upsampleTexHandle);
// write to bloomOut texture
data.bloomOutTexHandle = framegraph::TextureHandle(
@@ -434,6 +440,10 @@ void BloomStage::render(scene::Camera *camera) {
builder.writeToBlackboard(RenderPipeline::fgStrHandleBloomOutTexture, data.bloomOutTexHandle);
builder.setViewport(pipeline->getViewport(camera), renderArea);
// Update intensity
data.bloomUBO = stage->getCombineUBO();
data.textureSize[3] = intensity;
};
auto combineExec = [this, camera](CombineRenderData const &data, const framegraph::DevicePassResourceTable &table) {
@@ -453,10 +463,13 @@ void BloomStage::render(scene::Camera *camera) {
pass, shader, inputAssembler, renderPass);
CC_ASSERT(pso != nullptr);
pass->getDescriptorSet()->bindTexture(0, table.getRead(data.lightingOutTexHandle));
pass->getDescriptorSet()->bindTexture(1, table.getRead(data.upsampleTexHandle));
pass->getDescriptorSet()->bindSampler(0, data.sampler);
data.bloomUBO->update(data.textureSize, sizeof(data.textureSize));
pass->getDescriptorSet()->bindBuffer(0, data.bloomUBO);
pass->getDescriptorSet()->bindTexture(1, table.getRead(data.lightingOutTexHandle));
pass->getDescriptorSet()->bindTexture(2, table.getRead(data.upsampleTexHandle));
pass->getDescriptorSet()->bindSampler(1, data.sampler);
pass->getDescriptorSet()->bindSampler(2, data.sampler);
pass->getDescriptorSet()->update();
cmdBf->bindDescriptorSet(materialSet, pass->getDescriptorSet());
@@ -29,6 +29,8 @@
#include "frame-graph/Handle.h"
#include "Enum.h"
#define MAX_BLOOM_FILTER_PASS_NUM 6
namespace cc {
namespace pipeline {
@@ -43,27 +45,42 @@ public:
BloomStage();
~BloomStage() override = default;
static const int MAX_SCALING_SAMPLE_PASS_NUM = 6;
static const RenderStageInfo &getInitializeInfo();
bool initialize(const RenderStageInfo &info) override;
void activate(RenderPipeline *pipeline, RenderFlow *flow) override;
void destroy() override;
void render(scene::Camera *camera) override;
gfx::Buffer * getPrefilterUBO() { return _prefilterUBO; }
auto & getDownsampelUBO() { return _downsampleUBO; }
auto & getUpsampleUBO() { return _upsampleUBO; }
gfx::Buffer * getCombineUBO() { return _combineUBO; }
gfx::Sampler *getSampler() const { return _sampler; }
inline float getThreshold() const { return _threshold; }
inline void setThreshold(float value) { _threshold = value; }
inline float getIntensity() const { return _intensity; }
inline void setIntensity(float value) { _intensity = value; }
inline int getIterations() const { return _iterations; }
inline void setIterations(int value) {
_iterations = std::max(1, std::min(value, MAX_BLOOM_FILTER_PASS_NUM));
}
private:
gfx::Rect _renderArea;
uint _phaseID = 0;
static RenderStageInfo initInfo;
gfx::Sampler * _sampler = nullptr;
std::array<gfx::Buffer *, MAX_SCALING_SAMPLE_PASS_NUM> _downsampleUBO{};
std::array<gfx::Buffer *, MAX_SCALING_SAMPLE_PASS_NUM> _upsampleUBO{};
framegraph::StringHandle _fgStrHandleBloomOut;
float _threshold = 1.0F;
float _intensity = 0.8F;
int _iterations = 2;
gfx::Sampler * _sampler = nullptr;
gfx::Buffer * _prefilterUBO = nullptr;
std::array<gfx::Buffer *, MAX_BLOOM_FILTER_PASS_NUM> _downsampleUBO{};
std::array<gfx::Buffer *, MAX_BLOOM_FILTER_PASS_NUM> _upsampleUBO{};
gfx::Buffer * _combineUBO = nullptr;
framegraph::StringHandle _fgStrHandleBloomOut;
};
} // namespace pipeline
} // namespace cc
@@ -113,7 +113,7 @@ void PostProcessStage::render(scene::Camera *camera) {
auto *pipeline = _pipeline;
float shadingScale{_pipeline->getPipelineSceneData()->getSharedData()->shadingScale};
auto postSetup = [&](framegraph::PassNodeBuilder &builder, RenderData &data) {
if (pipeline->getBloomEnable()) {
if (pipeline->getBloomEnabled()) {
data.outColorTex = framegraph::TextureHandle(builder.readFromBlackboard(RenderPipeline::fgStrHandleBloomOutTexture));
} else {
data.outColorTex = framegraph::TextureHandle(builder.readFromBlackboard(RenderPipeline::fgStrHandleOutColorTexture));
+19 -18
View File
@@ -25,6 +25,7 @@
#pragma once
#include <vector>
#include "math/Mat4.h"
#include "math/Vec2.h"
#include "math/Vec3.h"
@@ -107,24 +108,24 @@ struct Ambient {
};
struct PipelineSharedSceneData {
bool isHDR{true};
float shadingScale{0.0F};
Ambient * ambient{nullptr};
Shadow * shadow{nullptr};
Skybox * skybox{nullptr};
Fog * fog{nullptr};
Pass * deferredLightPass{nullptr};
gfx::Shader *deferredLightPassShader{nullptr};
Pass * bloomPrefilterPass{nullptr};
gfx::Shader *bloomPrefilterPassShader{nullptr};
Pass * bloomDownsamplePass{nullptr};
gfx::Shader *bloomDownsamplePassShader{nullptr};
Pass * bloomUpsamplePass{nullptr};
gfx::Shader *bloomUpsamplePassShader{nullptr};
Pass * bloomCombinePass{nullptr};
gfx::Shader *bloomCombinePassShader{nullptr};
Pass * pipelinePostPass{nullptr};
gfx::Shader *pipelinePostPassShader{nullptr};
bool isHDR{true};
float shadingScale{0.0F};
Ambient * ambient{nullptr};
Shadow * shadow{nullptr};
Skybox * skybox{nullptr};
Fog * fog{nullptr};
Pass * deferredLightPass{nullptr};
gfx::Shader * deferredLightPassShader{nullptr};
Pass * bloomPrefilterPass{nullptr};
gfx::Shader * bloomPrefilterPassShader{nullptr};
std::vector<Pass *> bloomDownsamplePass;
gfx::Shader * bloomDownsamplePassShader{nullptr};
std::vector<Pass *> bloomUpsamplePass;
gfx::Shader * bloomUpsamplePassShader{nullptr};
Pass * bloomCombinePass{nullptr};
gfx::Shader * bloomCombinePassShader{nullptr};
Pass * pipelinePostPass{nullptr};
gfx::Shader * pipelinePostPassShader{nullptr};
};
struct FlatBuffer {
+2 -1
View File
@@ -60,7 +60,8 @@ skip = ForwardPipeline::[getOrCreateRenderPass getLightsUBO getValidLights getLi
rename_functions =
getter_setter= RenderPipeline::[globalDSManager descriptorSet descriptorSetLayout constantMacros clusterEnabled]
getter_setter = RenderPipeline::[globalDSManager descriptorSet descriptorSetLayout constantMacros clusterEnabled bloomEnabled],
BloomStage::[threshold intensity iterations]
rename_classes =