Fix bloom shader missing its target

This commit is contained in:
Magnus Norddahl 2016-08-12 17:44:59 +02:00 committed by Christoph Oelckers
parent 4ecb77385d
commit 210fce1193
8 changed files with 26 additions and 8 deletions

View file

@ -143,6 +143,8 @@ void FGLRenderer::BloomScene()
mBloomExtractShader->Bind(); mBloomExtractShader->Bind();
mBloomExtractShader->SceneTexture.Set(0); mBloomExtractShader->SceneTexture.Set(0);
mBloomExtractShader->Exposure.Set(mCameraExposure); mBloomExtractShader->Exposure.Set(mCameraExposure);
mBloomExtractShader->Scale.Set(mSceneViewport.width / (float)mScreenViewport.width, mSceneViewport.height / (float)mScreenViewport.height);
mBloomExtractShader->Offset.Set(mSceneViewport.left / (float)mScreenViewport.width, mSceneViewport.top / (float)mScreenViewport.height);
RenderScreenQuad(); RenderScreenQuad();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@ -182,7 +184,7 @@ void FGLRenderer::BloomScene()
// Add bloom back to scene texture: // Add bloom back to scene texture:
mBuffers->BindCurrentFB(); mBuffers->BindCurrentFB();
glViewport(mScreenViewport.left, mScreenViewport.top, mScreenViewport.width, mScreenViewport.height); glViewport(mSceneViewport.left, mSceneViewport.top, mSceneViewport.width, mSceneViewport.height);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD); glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_ONE, GL_ONE); glBlendFunc(GL_ONE, GL_ONE);
@ -193,6 +195,7 @@ void FGLRenderer::BloomScene()
mBloomCombineShader->Bind(); mBloomCombineShader->Bind();
mBloomCombineShader->BloomTexture.Set(0); mBloomCombineShader->BloomTexture.Set(0);
RenderScreenQuad(); RenderScreenQuad();
glViewport(mScreenViewport.left, mScreenViewport.top, mScreenViewport.width, mScreenViewport.height);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View file

@ -139,7 +139,7 @@ void FGLRenderBuffers::DeleteFrameBuffer(GLuint &handle)
// //
//========================================================================== //==========================================================================
void FGLRenderBuffers::Setup(int width, int height) void FGLRenderBuffers::Setup(int width, int height, int sceneWidth, int sceneHeight)
{ {
if (!IsEnabled()) if (!IsEnabled())
return; return;
@ -151,16 +151,23 @@ void FGLRenderBuffers::Setup(int width, int height)
CreateScene(mWidth, mHeight, samples); CreateScene(mWidth, mHeight, samples);
mSamples = samples; mSamples = samples;
} }
else if (width > mWidth || height > mHeight) else if (width != mWidth || height != mHeight)
{ {
CreatePipeline(width, height); CreatePipeline(width, height);
CreateScene(width, height, samples); CreateScene(width, height, samples);
CreateBloom(width, height);
mWidth = width; mWidth = width;
mHeight = height; mHeight = height;
mSamples = samples; mSamples = samples;
} }
// Bloom bluring buffers need to match the scene to avoid bloom bleeding artifacts
if (mBloomWidth != sceneWidth || mBloomHeight != sceneHeight)
{
CreateBloom(sceneWidth, sceneHeight);
mBloomWidth = sceneWidth;
mBloomHeight = sceneHeight;
}
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0); glBindRenderbuffer(GL_RENDERBUFFER, 0);

View file

@ -20,7 +20,7 @@ public:
FGLRenderBuffers(); FGLRenderBuffers();
~FGLRenderBuffers(); ~FGLRenderBuffers();
void Setup(int width, int height); void Setup(int width, int height, int sceneWidth, int sceneHeight);
void BindSceneFB(); void BindSceneFB();
void BlitSceneToTexture(); void BlitSceneToTexture();
@ -64,6 +64,8 @@ private:
int mWidth = 0; int mWidth = 0;
int mHeight = 0; int mHeight = 0;
int mSamples = 0; int mSamples = 0;
int mBloomWidth = 0;
int mBloomHeight = 0;
static const int NumPipelineTextures = 2; static const int NumPipelineTextures = 2;
int mCurrentPipelineTexture = 0; int mCurrentPipelineTexture = 0;

View file

@ -286,7 +286,7 @@ void FGLRenderer::Begin2D()
{ {
if (FGLRenderBuffers::IsEnabled()) if (FGLRenderBuffers::IsEnabled())
{ {
mBuffers->Setup(framebuffer->GetWidth(), framebuffer->GetHeight()); mBuffers->Setup(mScreenViewport.width, mScreenViewport.height, mSceneViewport.width, mSceneViewport.height);
if (mDrawingScene2D) if (mDrawingScene2D)
mBuffers->BindSceneFB(); mBuffers->BindSceneFB();
else else

View file

@ -170,7 +170,7 @@ void FGLRenderer::Set3DViewport(bool mainview)
{ {
if (mainview && FGLRenderBuffers::IsEnabled()) if (mainview && FGLRenderBuffers::IsEnabled())
{ {
mBuffers->Setup(mScreenViewport.width, mScreenViewport.height); mBuffers->Setup(mScreenViewport.width, mScreenViewport.height, mSceneViewport.width, mSceneViewport.height);
mBuffers->BindSceneFB(); mBuffers->BindSceneFB();
} }

View file

@ -60,6 +60,8 @@ void FBloomExtractShader::Bind()
mShader.SetAttribLocation(0, "PositionInProjection"); mShader.SetAttribLocation(0, "PositionInProjection");
SceneTexture.Init(mShader, "SceneTexture"); SceneTexture.Init(mShader, "SceneTexture");
Exposure.Init(mShader, "ExposureAdjustment"); Exposure.Init(mShader, "ExposureAdjustment");
Scale.Init(mShader, "Scale");
Offset.Init(mShader, "Offset");
} }
mShader.Bind(); mShader.Bind();
} }

View file

@ -10,6 +10,8 @@ public:
FBufferedUniform1i SceneTexture; FBufferedUniform1i SceneTexture;
FBufferedUniform1f Exposure; FBufferedUniform1f Exposure;
FBufferedUniform2f Scale;
FBufferedUniform2f Offset;
private: private:
FShaderProgram mShader; FShaderProgram mShader;

View file

@ -4,9 +4,11 @@ out vec4 FragColor;
uniform sampler2D SceneTexture; uniform sampler2D SceneTexture;
uniform float ExposureAdjustment; uniform float ExposureAdjustment;
uniform vec2 Scale;
uniform vec2 Offset;
void main() void main()
{ {
vec4 color = texture(SceneTexture, TexCoord); vec4 color = texture(SceneTexture, Offset + TexCoord * Scale);
FragColor = max(vec4(color.rgb * ExposureAdjustment - 1, 1), vec4(0)); FragColor = max(vec4(color.rgb * ExposureAdjustment - 1, 1), vec4(0));
} }