Using uniform buffer for bloom threshold

This commit is contained in:
Olivier Prat 2018-07-06 11:13:42 +02:00
parent a97d4f655a
commit 1097e666d6
4 changed files with 44 additions and 22 deletions

View file

@ -21,13 +21,15 @@
#define BLOOM_BLUR_LEVEL_COUNT 3
BloomThreshold::BloomThreshold(unsigned int downsamplingFactor) :
_downsamplingFactor(downsamplingFactor) {
BloomThreshold::BloomThreshold(unsigned int downsamplingFactor) {
assert(downsamplingFactor > 0);
_parameters.edit()._sampleCount = downsamplingFactor;
}
void BloomThreshold::configure(const Config& config) {
_threshold = config.threshold;
if (_parameters.get()._threshold != config.threshold) {
_parameters.edit()._threshold = config.threshold;
}
}
void BloomThreshold::run(const render::RenderContextPointer& renderContext, const Inputs& inputs, Outputs& outputs) {
@ -43,10 +45,11 @@ void BloomThreshold::run(const render::RenderContextPointer& renderContext, cons
auto inputBuffer = inputFrameBuffer->getRenderBuffer(0);
auto bufferSize = gpu::Vec2u(inputBuffer->getDimensions());
const auto downSamplingFactor = _parameters.get()._sampleCount;
// Downsample resolution
bufferSize.x /= _downsamplingFactor;
bufferSize.y /= _downsamplingFactor;
bufferSize.x /= downSamplingFactor;
bufferSize.y /= downSamplingFactor;
if (!_outputBuffer || _outputBuffer->getSize() != bufferSize) {
auto colorTexture = gpu::TexturePointer(gpu::Texture::createRenderBuffer(inputBuffer->getTexelFormat(), bufferSize.x, bufferSize.y,
@ -54,10 +57,12 @@ void BloomThreshold::run(const render::RenderContextPointer& renderContext, cons
_outputBuffer = gpu::FramebufferPointer(gpu::Framebuffer::create("BloomThreshold"));
_outputBuffer->setRenderBuffer(0, colorTexture);
_parameters.edit()._deltaUV = { 1.0f / bufferSize.x, 1.0f / bufferSize.y };
}
static const int COLOR_MAP_SLOT = 0;
static const int THRESHOLD_SLOT = 1;
static const int PARAMETERS_SLOT = 1;
if (!_pipeline) {
auto vs = gpu::StandardShaderLib::getDrawTransformUnitQuadVS();
@ -66,7 +71,7 @@ void BloomThreshold::run(const render::RenderContextPointer& renderContext, cons
gpu::Shader::BindingSet slotBindings;
slotBindings.insert(gpu::Shader::Binding("colorMap", COLOR_MAP_SLOT));
slotBindings.insert(gpu::Shader::Binding("threshold", THRESHOLD_SLOT));
slotBindings.insert(gpu::Shader::Binding("parametersBuffer", PARAMETERS_SLOT));
gpu::Shader::makeProgram(*program, slotBindings);
gpu::StatePointer state = gpu::StatePointer(new gpu::State());
@ -86,7 +91,7 @@ void BloomThreshold::run(const render::RenderContextPointer& renderContext, cons
batch.setFramebuffer(_outputBuffer);
batch.setResourceTexture(COLOR_MAP_SLOT, inputBuffer);
batch._glUniform1f(THRESHOLD_SLOT, _threshold);
batch.setUniformBuffer(PARAMETERS_SLOT, _parameters);
batch.draw(gpu::TRIANGLE_STRIP, 4);
});

View file

@ -61,10 +61,11 @@ public:
private:
#include "BloomThreshold.shared.slh"
gpu::FramebufferPointer _outputBuffer;
gpu::PipelinePointer _pipeline;
float _threshold;
unsigned int _downsamplingFactor;
gpu::StructBuffer<Parameters> _parameters;
};

View file

@ -0,0 +1,18 @@
// glsl / C++ compatible source as interface for FadeEffect
#ifdef __cplusplus
# define BT_VEC2 glm::vec2
#else
# define BT_VEC2 vec2
#endif
struct Parameters
{
BT_VEC2 _deltaUV;
float _threshold;
int _sampleCount;
};
// <@if 1@>
// Trigger Scribe include
// <@endif@> <!def that !>
//

View file

@ -9,37 +9,35 @@
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
<@include BloomThreshold.shared.slh@>
uniform sampler2D colorMap;
uniform float threshold;
layout(std140) uniform parametersBuffer {
Parameters parameters;
};
in vec2 varTexCoord0;
out vec4 outFragColor;
#define DOWNSAMPLING_FACTOR 4
#define SAMPLE_COUNT (DOWNSAMPLING_FACTOR/2)
void main(void) {
vec2 deltaX = dFdx(varTexCoord0) / SAMPLE_COUNT;
vec2 deltaY = dFdy(varTexCoord0) / SAMPLE_COUNT;
vec2 startUv = varTexCoord0;
vec4 maskedColor = vec4(0,0,0,0);
for (int y=0 ; y<SAMPLE_COUNT ; y++) {
for (int y=0 ; y<parameters._sampleCount ; y++) {
vec2 uv = startUv;
for (int x=0 ; x<SAMPLE_COUNT ; x++) {
for (int x=0 ; x<parameters._sampleCount ; x++) {
vec4 color = texture(colorMap, uv);
float luminance = (color.r+color.g+color.b) / 3.0;
float mask = clamp((luminance-threshold)*0.25, 0, 1);
float mask = clamp((luminance-parameters._threshold)*0.25, 0, 1);
color *= mask;
maskedColor += color;
uv += deltaX;
uv.x += parameters._deltaUV.x;
}
startUv += deltaY;
startUv.y += parameters._deltaUV.y;
}
maskedColor /= SAMPLE_COUNT*SAMPLE_COUNT;
maskedColor /= parameters._sampleCount * parameters._sampleCount;
outFragColor = vec4(maskedColor.rgb, 1.0);
}