Added exposure pass calculating the bloom/tonemap exposure based on what the eye is seeing

This commit is contained in:
Magnus Norddahl 2016-09-18 15:57:22 +02:00 committed by Christoph Oelckers
parent 1e6b99cebd
commit f7b6b1433c
17 changed files with 358 additions and 40 deletions

View file

@ -75,7 +75,10 @@ CUSTOM_CVAR(Float, gl_bloom_amount, 1.4f, 0)
if (self < 0.1f) self = 0.1f; if (self < 0.1f) self = 0.1f;
} }
CVAR(Float, gl_exposure, 0.0f, 0) CVAR(Float, gl_exposure_scale, 0.75f, 0)
CVAR(Float, gl_exposure_min, 0.35f, 0)
CVAR(Float, gl_exposure_base, 0.35f, 0)
CVAR(Float, gl_exposure_speed, 0.05f, 0)
CUSTOM_CVAR(Int, gl_tonemap, 0, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) CUSTOM_CVAR(Int, gl_tonemap, 0, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
{ {
@ -106,6 +109,78 @@ void FGLRenderer::RenderScreenQuad()
GLRenderer->mVBO->RenderArray(GL_TRIANGLE_STRIP, FFlatVertexBuffer::PRESENT_INDEX, 4); GLRenderer->mVBO->RenderArray(GL_TRIANGLE_STRIP, FFlatVertexBuffer::PRESENT_INDEX, 4);
} }
//-----------------------------------------------------------------------------
//
// Extracts light average from the scene and updates the camera exposure texture
//
//-----------------------------------------------------------------------------
void FGLRenderer::UpdateCameraExposure()
{
if (!gl_bloom && gl_tonemap == 0)
return;
FGLDebug::PushGroup("UpdateCameraExposure");
FGLPostProcessState savedState;
savedState.SaveTextureBinding1();
// Extract light level from scene texture:
const auto &level0 = mBuffers->ExposureLevels[0];
glBindFramebuffer(GL_FRAMEBUFFER, level0.Framebuffer);
glViewport(0, 0, level0.Width, level0.Height);
mBuffers->BindCurrentTexture(0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
mExposureExtractShader->Bind();
mExposureExtractShader->SceneTexture.Set(0);
mExposureExtractShader->Scale.Set(mSceneViewport.width / (float)mScreenViewport.width, mSceneViewport.height / (float)mScreenViewport.height);
mExposureExtractShader->Offset.Set(mSceneViewport.left / (float)mScreenViewport.width, mSceneViewport.top / (float)mScreenViewport.height);
RenderScreenQuad();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Find the average value:
for (int i = 0; i + 1 < mBuffers->ExposureLevels.Size(); i++)
{
const auto &level = mBuffers->ExposureLevels[i];
const auto &next = mBuffers->ExposureLevels[i + 1];
glBindFramebuffer(GL_FRAMEBUFFER, next.Framebuffer);
glViewport(0, 0, next.Width, next.Height);
glBindTexture(GL_TEXTURE_2D, level.Texture);
mExposureAverageShader->Bind();
mExposureAverageShader->ExposureTexture.Set(0);
RenderScreenQuad();
}
// Combine average value with current camera exposure:
glBindFramebuffer(GL_FRAMEBUFFER, mBuffers->ExposureFB);
glViewport(0, 0, 1, 1);
if (!mBuffers->FirstExposureFrame)
{
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
else
{
mBuffers->FirstExposureFrame = false;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mBuffers->ExposureLevels.Last().Texture);
mExposureCombineShader->Bind();
mExposureCombineShader->ExposureTexture.Set(0);
mExposureCombineShader->ExposureBase.Set(gl_exposure_base);
mExposureCombineShader->ExposureMin.Set(gl_exposure_min);
mExposureCombineShader->ExposureScale.Set(gl_exposure_scale);
mExposureCombineShader->ExposureSpeed.Set(gl_exposure_speed);
RenderScreenQuad();
glViewport(mScreenViewport.left, mScreenViewport.top, mScreenViewport.width, mScreenViewport.height);
FGLDebug::PopGroup();
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// //
// Adds bloom contribution to scene texture // Adds bloom contribution to scene texture
@ -121,6 +196,7 @@ void FGLRenderer::BloomScene()
FGLDebug::PushGroup("BloomScene"); FGLDebug::PushGroup("BloomScene");
FGLPostProcessState savedState; FGLPostProcessState savedState;
savedState.SaveTextureBinding1();
const float blurAmount = gl_bloom_amount; const float blurAmount = gl_bloom_amount;
int sampleCount = gl_bloom_kernel_size; int sampleCount = gl_bloom_kernel_size;
@ -133,9 +209,12 @@ void FGLRenderer::BloomScene()
mBuffers->BindCurrentTexture(0); mBuffers->BindCurrentTexture(0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, mBuffers->ExposureTexture);
glActiveTexture(GL_TEXTURE0);
mBloomExtractShader->Bind(); mBloomExtractShader->Bind();
mBloomExtractShader->SceneTexture.Set(0); mBloomExtractShader->SceneTexture.Set(0);
mBloomExtractShader->Exposure.Set(mCameraExposure); mBloomExtractShader->ExposureTexture.Set(1);
mBloomExtractShader->Scale.Set(mSceneViewport.width / (float)mScreenViewport.width, mSceneViewport.height / (float)mScreenViewport.height); 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); mBloomExtractShader->Offset.Set(mSceneViewport.left / (float)mScreenViewport.width, mSceneViewport.top / (float)mScreenViewport.height);
RenderScreenQuad(); RenderScreenQuad();
@ -220,7 +299,12 @@ void FGLRenderer::TonemapScene()
} }
else else
{ {
mTonemapShader->Exposure.Set(mCameraExposure); savedState.SaveTextureBinding1();
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, mBuffers->ExposureTexture);
glActiveTexture(GL_TEXTURE0);
mTonemapShader->ExposureTexture.Set(1);
} }
RenderScreenQuad(); RenderScreenQuad();

View file

@ -45,7 +45,7 @@ FGLPostProcessState::FGLPostProcessState()
{ {
glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTex); glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTex);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding); glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding[0]);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
if (gl.flags & RFL_SAMPLER_OBJECTS) if (gl.flags & RFL_SAMPLER_OBJECTS)
{ {
@ -75,6 +75,15 @@ FGLPostProcessState::FGLPostProcessState()
glDisable(GL_BLEND); glDisable(GL_BLEND);
} }
void FGLPostProcessState::SaveTextureBinding1()
{
glActiveTexture(GL_TEXTURE1);
glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding[1]);
glBindTexture(GL_TEXTURE_2D, 0);
textureBinding1Saved = true;
glActiveTexture(GL_TEXTURE0);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// //
// Restores state at the end of post processing // Restores state at the end of post processing
@ -108,6 +117,12 @@ FGLPostProcessState::~FGLPostProcessState()
glUseProgram(currentProgram); glUseProgram(currentProgram);
if (textureBinding1Saved)
{
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0);
}
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
if (gl.flags & RFL_SAMPLER_OBJECTS) if (gl.flags & RFL_SAMPLER_OBJECTS)
@ -115,6 +130,13 @@ FGLPostProcessState::~FGLPostProcessState()
glBindSampler(0, samplerBinding[0]); glBindSampler(0, samplerBinding[0]);
glBindSampler(1, samplerBinding[1]); glBindSampler(1, samplerBinding[1]);
} }
glBindTexture(GL_TEXTURE_2D, textureBinding); glBindTexture(GL_TEXTURE_2D, textureBinding[0]);
if (textureBinding1Saved)
{
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textureBinding[1]);
}
glActiveTexture(activeTex); glActiveTexture(activeTex);
} }

View file

@ -14,12 +14,14 @@ public:
FGLPostProcessState(); FGLPostProcessState();
~FGLPostProcessState(); ~FGLPostProcessState();
void SaveTextureBinding1();
private: private:
FGLPostProcessState(const FGLPostProcessState &) = delete; FGLPostProcessState(const FGLPostProcessState &) = delete;
FGLPostProcessState &operator=(const FGLPostProcessState &) = delete; FGLPostProcessState &operator=(const FGLPostProcessState &) = delete;
GLint activeTex; GLint activeTex;
GLint textureBinding; GLint textureBinding[2];
GLint samplerBinding[2]; GLint samplerBinding[2];
GLboolean blendEnabled; GLboolean blendEnabled;
GLboolean scissorEnabled; GLboolean scissorEnabled;
@ -32,6 +34,7 @@ private:
GLint blendSrcAlpha; GLint blendSrcAlpha;
GLint blendDestRgb; GLint blendDestRgb;
GLint blendDestAlpha; GLint blendDestAlpha;
bool textureBinding1Saved = false;
}; };
#endif #endif

View file

@ -74,6 +74,7 @@ FGLRenderBuffers::~FGLRenderBuffers()
ClearPipeline(); ClearPipeline();
ClearEyeBuffers(); ClearEyeBuffers();
ClearBloom(); ClearBloom();
ClearExposureLevels();
} }
void FGLRenderBuffers::ClearScene() void FGLRenderBuffers::ClearScene()
@ -107,6 +108,18 @@ void FGLRenderBuffers::ClearBloom()
} }
} }
void FGLRenderBuffers::ClearExposureLevels()
{
for (auto &level : ExposureLevels)
{
DeleteTexture(level.Texture);
DeleteFrameBuffer(level.Framebuffer);
}
ExposureLevels.Clear();
DeleteTexture(ExposureTexture);
DeleteFrameBuffer(ExposureFB);
}
void FGLRenderBuffers::ClearEyeBuffers() void FGLRenderBuffers::ClearEyeBuffers()
{ {
for (auto handle : mEyeFBs) for (auto handle : mEyeFBs)
@ -186,11 +199,12 @@ bool FGLRenderBuffers::Setup(int width, int height, int sceneWidth, int sceneHei
} }
// Bloom bluring buffers need to match the scene to avoid bloom bleeding artifacts // Bloom bluring buffers need to match the scene to avoid bloom bleeding artifacts
if (mBloomWidth != sceneWidth || mBloomHeight != sceneHeight) if (mSceneWidth != sceneWidth || mSceneHeight != sceneHeight)
{ {
CreateBloom(sceneWidth, sceneHeight); CreateBloom(sceneWidth, sceneHeight);
mBloomWidth = sceneWidth; CreateExposureLevels(sceneWidth, sceneHeight);
mBloomHeight = sceneHeight; mSceneWidth = sceneWidth;
mSceneHeight = sceneHeight;
} }
glBindTexture(GL_TEXTURE_2D, textureBinding); glBindTexture(GL_TEXTURE_2D, textureBinding);
@ -204,11 +218,12 @@ bool FGLRenderBuffers::Setup(int width, int height, int sceneWidth, int sceneHei
ClearPipeline(); ClearPipeline();
ClearEyeBuffers(); ClearEyeBuffers();
ClearBloom(); ClearBloom();
ClearExposureLevels();
mWidth = 0; mWidth = 0;
mHeight = 0; mHeight = 0;
mSamples = 0; mSamples = 0;
mBloomWidth = 0; mSceneWidth = 0;
mBloomHeight = 0; mSceneHeight = 0;
} }
return !FailedCreate; return !FailedCreate;
@ -281,6 +296,41 @@ void FGLRenderBuffers::CreateBloom(int width, int height)
} }
} }
//==========================================================================
//
// Creates camera exposure level buffers
//
//==========================================================================
void FGLRenderBuffers::CreateExposureLevels(int width, int height)
{
ClearExposureLevels();
int i = 0;
do
{
width = MAX(width / 2, 1);
height = MAX(height / 2, 1);
FString textureName, fbName;
textureName.Format("Exposure.Texture%d", i);
fbName.Format("Exposure.Framebuffer%d", i);
i++;
FGLExposureTextureLevel level;
level.Width = width;
level.Height = height;
level.Texture = Create2DTexture(textureName, GL_R32F, level.Width, level.Height);
level.Framebuffer = CreateFrameBuffer(fbName, level.Texture);
ExposureLevels.Push(level);
} while (width > 1 || height > 1);
ExposureTexture = Create2DTexture("Exposure.CameraTexture", GL_R32F, 1, 1);
ExposureFB = CreateFrameBuffer("Exposure.CameraFB", ExposureTexture);
FirstExposureFrame = true;
}
//========================================================================== //==========================================================================
// //
// Creates eye buffers if needed // Creates eye buffers if needed
@ -316,14 +366,14 @@ void FGLRenderBuffers::CreateEyeBuffers(int eye)
// //
//========================================================================== //==========================================================================
GLuint FGLRenderBuffers::Create2DTexture(const FString &name, GLuint format, int width, int height) GLuint FGLRenderBuffers::Create2DTexture(const FString &name, GLuint format, int width, int height, const void *data)
{ {
GLuint type = (format == GL_RGBA16F) ? GL_FLOAT : GL_UNSIGNED_BYTE; GLuint type = (format == GL_RGBA16F || format == GL_R32F) ? GL_FLOAT : GL_UNSIGNED_BYTE;
GLuint handle = 0; GLuint handle = 0;
glGenTextures(1, &handle); glGenTextures(1, &handle);
glBindTexture(GL_TEXTURE_2D, handle); glBindTexture(GL_TEXTURE_2D, handle);
FGLDebug::LabelObject(GL_TEXTURE, handle, name); FGLDebug::LabelObject(GL_TEXTURE, handle, name);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, GL_RGBA, type, nullptr); glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format != GL_R32F ? GL_RGBA : GL_RED, type, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);

View file

@ -14,6 +14,15 @@ public:
GLuint Height = 0; GLuint Height = 0;
}; };
class FGLExposureTextureLevel
{
public:
GLuint Texture = 0;
GLuint Framebuffer = 0;
GLuint Width = 0;
GLuint Height = 0;
};
class FGLRenderBuffers class FGLRenderBuffers
{ {
public: public:
@ -39,6 +48,11 @@ public:
enum { NumBloomLevels = 4 }; enum { NumBloomLevels = 4 };
FGLBloomTextureLevel BloomLevels[NumBloomLevels]; FGLBloomTextureLevel BloomLevels[NumBloomLevels];
TArray<FGLExposureTextureLevel> ExposureLevels;
GLuint ExposureTexture = 0;
GLuint ExposureFB = 0;
bool FirstExposureFrame = true;
static bool IsEnabled(); static bool IsEnabled();
int GetWidth() const { return mWidth; } int GetWidth() const { return mWidth; }
@ -49,11 +63,13 @@ private:
void ClearPipeline(); void ClearPipeline();
void ClearEyeBuffers(); void ClearEyeBuffers();
void ClearBloom(); void ClearBloom();
void ClearExposureLevels();
void CreateScene(int width, int height, int samples); void CreateScene(int width, int height, int samples);
void CreatePipeline(int width, int height); void CreatePipeline(int width, int height);
void CreateBloom(int width, int height); void CreateBloom(int width, int height);
void CreateExposureLevels(int width, int height);
void CreateEyeBuffers(int eye); void CreateEyeBuffers(int eye);
GLuint Create2DTexture(const FString &name, GLuint format, int width, int height); GLuint Create2DTexture(const FString &name, GLuint format, int width, int height, const void *data = nullptr);
GLuint CreateRenderBuffer(const FString &name, GLuint format, int width, int height); GLuint CreateRenderBuffer(const FString &name, GLuint format, int width, int height);
GLuint CreateRenderBuffer(const FString &name, GLuint format, int samples, int width, int height); GLuint CreateRenderBuffer(const FString &name, GLuint format, int samples, int width, int height);
GLuint CreateFrameBuffer(const FString &name, GLuint colorbuffer); GLuint CreateFrameBuffer(const FString &name, GLuint colorbuffer);
@ -69,8 +85,8 @@ private:
int mHeight = 0; int mHeight = 0;
int mSamples = 0; int mSamples = 0;
int mMaxSamples = 0; int mMaxSamples = 0;
int mBloomWidth = 0; int mSceneWidth = 0;
int mBloomHeight = 0; int mSceneHeight = 0;
static const int NumPipelineTextures = 2; static const int NumPipelineTextures = 2;
int mCurrentPipelineTexture = 0; int mCurrentPipelineTexture = 0;

View file

@ -104,6 +104,9 @@ FGLRenderer::FGLRenderer(OpenGLFrameBuffer *fb)
mPresentShader = nullptr; mPresentShader = nullptr;
mBloomExtractShader = nullptr; mBloomExtractShader = nullptr;
mBloomCombineShader = nullptr; mBloomCombineShader = nullptr;
mExposureExtractShader = nullptr;
mExposureAverageShader = nullptr;
mExposureCombineShader = nullptr;
mBlurShader = nullptr; mBlurShader = nullptr;
mTonemapShader = nullptr; mTonemapShader = nullptr;
mTonemapPalette = nullptr; mTonemapPalette = nullptr;
@ -119,6 +122,9 @@ void FGLRenderer::Initialize(int width, int height)
mBuffers = new FGLRenderBuffers(); mBuffers = new FGLRenderBuffers();
mBloomExtractShader = new FBloomExtractShader(); mBloomExtractShader = new FBloomExtractShader();
mBloomCombineShader = new FBloomCombineShader(); mBloomCombineShader = new FBloomCombineShader();
mExposureExtractShader = new FExposureExtractShader();
mExposureAverageShader = new FExposureAverageShader();
mExposureCombineShader = new FExposureCombineShader();
mBlurShader = new FBlurShader(); mBlurShader = new FBlurShader();
mTonemapShader = new FTonemapShader(); mTonemapShader = new FTonemapShader();
mColormapShader = new FColormapShader(); mColormapShader = new FColormapShader();
@ -179,6 +185,9 @@ FGLRenderer::~FGLRenderer()
if (mPresentShader) delete mPresentShader; if (mPresentShader) delete mPresentShader;
if (mBloomExtractShader) delete mBloomExtractShader; if (mBloomExtractShader) delete mBloomExtractShader;
if (mBloomCombineShader) delete mBloomCombineShader; if (mBloomCombineShader) delete mBloomCombineShader;
if (mExposureExtractShader) delete mExposureExtractShader;
if (mExposureAverageShader) delete mExposureAverageShader;
if (mExposureCombineShader) delete mExposureCombineShader;
if (mBlurShader) delete mBlurShader; if (mBlurShader) delete mBlurShader;
if (mTonemapShader) delete mTonemapShader; if (mTonemapShader) delete mTonemapShader;
if (mTonemapPalette) delete mTonemapPalette; if (mTonemapPalette) delete mTonemapPalette;

View file

@ -21,6 +21,9 @@ class DPSprite;
class FGLRenderBuffers; class FGLRenderBuffers;
class FBloomExtractShader; class FBloomExtractShader;
class FBloomCombineShader; class FBloomCombineShader;
class FExposureExtractShader;
class FExposureAverageShader;
class FExposureCombineShader;
class FBlurShader; class FBlurShader;
class FTonemapShader; class FTonemapShader;
class FColormapShader; class FColormapShader;
@ -92,6 +95,9 @@ public:
FGLRenderBuffers *mBuffers; FGLRenderBuffers *mBuffers;
FBloomExtractShader *mBloomExtractShader; FBloomExtractShader *mBloomExtractShader;
FBloomCombineShader *mBloomCombineShader; FBloomCombineShader *mBloomCombineShader;
FExposureExtractShader *mExposureExtractShader;
FExposureAverageShader *mExposureAverageShader;
FExposureCombineShader *mExposureCombineShader;
FBlurShader *mBlurShader; FBlurShader *mBlurShader;
FTonemapShader *mTonemapShader; FTonemapShader *mTonemapShader;
FColormapShader *mColormapShader; FColormapShader *mColormapShader;
@ -118,7 +124,6 @@ public:
GL_IRECT mSceneViewport; GL_IRECT mSceneViewport;
GL_IRECT mOutputLetterbox; GL_IRECT mOutputLetterbox;
bool mDrawingScene2D = false; bool mDrawingScene2D = false;
float mCameraExposure = 1.0f;
float mSceneClearColor[3]; float mSceneClearColor[3];
@ -166,6 +171,7 @@ public:
void SetFixedColormap (player_t *player); void SetFixedColormap (player_t *player);
void WriteSavePic (player_t *player, FILE *file, int width, int height); void WriteSavePic (player_t *player, FILE *file, int width, int height);
void EndDrawScene(sector_t * viewsector); void EndDrawScene(sector_t * viewsector);
void UpdateCameraExposure();
void BloomScene(); void BloomScene();
void TonemapScene(); void TonemapScene();
void ColormapScene(); void ColormapScene();

View file

@ -790,20 +790,6 @@ sector_t * FGLRenderer::RenderViewpoint (AActor * camera, GL_IRECT * bounds, flo
mViewActor=camera; mViewActor=camera;
} }
if (toscreen)
{
if (gl_exposure == 0.0f)
{
float light = viewsector->lightlevel / 255.0f;
float exposure = MAX(1.0f + (1.0f - light * light) * 0.9f, 0.5f);
mCameraExposure = mCameraExposure * 0.995f + exposure * 0.005f;
}
else
{
mCameraExposure = gl_exposure;
}
}
// 'viewsector' will not survive the rendering so it cannot be used anymore below. // 'viewsector' will not survive the rendering so it cannot be used anymore below.
lviewsector = viewsector; lviewsector = viewsector;
@ -839,6 +825,7 @@ sector_t * FGLRenderer::RenderViewpoint (AActor * camera, GL_IRECT * bounds, flo
if (mainview && FGLRenderBuffers::IsEnabled()) if (mainview && FGLRenderBuffers::IsEnabled())
{ {
mBuffers->BlitSceneToTexture(); mBuffers->BlitSceneToTexture();
UpdateCameraExposure();
BloomScene(); BloomScene();
TonemapScene(); TonemapScene();
ColormapScene(); ColormapScene();

View file

@ -46,7 +46,7 @@ void FBloomExtractShader::Bind()
mShader.Link("shaders/glsl/bloomextract"); mShader.Link("shaders/glsl/bloomextract");
mShader.SetAttribLocation(0, "PositionInProjection"); mShader.SetAttribLocation(0, "PositionInProjection");
SceneTexture.Init(mShader, "SceneTexture"); SceneTexture.Init(mShader, "SceneTexture");
Exposure.Init(mShader, "ExposureAdjustment"); ExposureTexture.Init(mShader, "ExposureTexture");
Scale.Init(mShader, "Scale"); Scale.Init(mShader, "Scale");
Offset.Init(mShader, "Offset"); Offset.Init(mShader, "Offset");
} }

View file

@ -9,7 +9,7 @@ public:
void Bind(); void Bind();
FBufferedUniformSampler SceneTexture; FBufferedUniformSampler SceneTexture;
FBufferedUniform1f Exposure; FBufferedUniformSampler ExposureTexture;
FBufferedUniform2f Scale; FBufferedUniform2f Scale;
FBufferedUniform2f Offset; FBufferedUniform2f Offset;

View file

@ -47,7 +47,7 @@ void FTonemapShader::Bind()
shader.Link("shaders/glsl/tonemap"); shader.Link("shaders/glsl/tonemap");
shader.SetAttribLocation(0, "PositionInProjection"); shader.SetAttribLocation(0, "PositionInProjection");
SceneTexture.Init(shader, "InputTexture"); SceneTexture.Init(shader, "InputTexture");
Exposure.Init(shader, "ExposureAdjustment"); ExposureTexture.Init(shader, "ExposureTexture");
PaletteLUT.Init(shader, "PaletteLUT"); PaletteLUT.Init(shader, "PaletteLUT");
} }
shader.Bind(); shader.Bind();
@ -70,3 +70,51 @@ const char *FTonemapShader::GetDefines(int mode)
case Palette: return "#define PALETTE\n"; case Palette: return "#define PALETTE\n";
} }
} }
void FExposureExtractShader::Bind()
{
if (!mShader)
{
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330);
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/exposureextract.fp", "", 330);
mShader.SetFragDataLocation(0, "FragColor");
mShader.Link("shaders/glsl/exposureextract");
mShader.SetAttribLocation(0, "PositionInProjection");
SceneTexture.Init(mShader, "SceneTexture");
Scale.Init(mShader, "Scale");
Offset.Init(mShader, "Offset");
}
mShader.Bind();
}
void FExposureAverageShader::Bind()
{
if (!mShader)
{
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 400);
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/exposureaverage.fp", "", 400);
mShader.SetFragDataLocation(0, "FragColor");
mShader.Link("shaders/glsl/exposureaverage");
mShader.SetAttribLocation(0, "PositionInProjection");
ExposureTexture.Init(mShader, "ExposureTexture");
}
mShader.Bind();
}
void FExposureCombineShader::Bind()
{
if (!mShader)
{
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/screenquad.vp", "", 330);
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/exposurecombine.fp", "", 330);
mShader.SetFragDataLocation(0, "FragColor");
mShader.Link("shaders/glsl/exposurecombine");
mShader.SetAttribLocation(0, "PositionInProjection");
ExposureTexture.Init(mShader, "ExposureTexture");
ExposureBase.Init(mShader, "ExposureBase");
ExposureMin.Init(mShader, "ExposureMin");
ExposureScale.Init(mShader, "ExposureScale");
ExposureSpeed.Init(mShader, "ExposureSpeed");
}
mShader.Bind();
}

View file

@ -9,7 +9,7 @@ public:
void Bind(); void Bind();
FBufferedUniformSampler SceneTexture; FBufferedUniformSampler SceneTexture;
FBufferedUniform1f Exposure; FBufferedUniformSampler ExposureTexture;
FBufferedUniformSampler PaletteLUT; FBufferedUniformSampler PaletteLUT;
static bool IsPaletteMode(); static bool IsPaletteMode();
@ -31,4 +31,43 @@ private:
FShaderProgram mShader[NumTonemapModes]; FShaderProgram mShader[NumTonemapModes];
}; };
class FExposureExtractShader
{
public:
void Bind();
FBufferedUniformSampler SceneTexture;
FBufferedUniform2f Scale;
FBufferedUniform2f Offset;
private:
FShaderProgram mShader;
};
class FExposureAverageShader
{
public:
void Bind();
FBufferedUniformSampler ExposureTexture;
private:
FShaderProgram mShader;
};
class FExposureCombineShader
{
public:
void Bind();
FBufferedUniformSampler ExposureTexture;
FBufferedUniform1f ExposureBase;
FBufferedUniform1f ExposureMin;
FBufferedUniform1f ExposureScale;
FBufferedUniform1f ExposureSpeed;
private:
FShaderProgram mShader;
};
#endif #endif

View file

@ -3,12 +3,13 @@ in vec2 TexCoord;
out vec4 FragColor; out vec4 FragColor;
uniform sampler2D SceneTexture; uniform sampler2D SceneTexture;
uniform float ExposureAdjustment; uniform sampler2D ExposureTexture;
uniform vec2 Scale; uniform vec2 Scale;
uniform vec2 Offset; uniform vec2 Offset;
void main() void main()
{ {
float exposureAdjustment = texture(ExposureTexture, vec2(0.5)).x;
vec4 color = texture(SceneTexture, Offset + TexCoord * Scale); vec4 color = texture(SceneTexture, Offset + TexCoord * Scale);
FragColor = max(vec4(color.rgb * ExposureAdjustment - 1, 1), vec4(0)); FragColor = max(vec4((color.rgb + vec3(0.001)) * exposureAdjustment - 1, 1), vec4(0));
} }

View file

@ -0,0 +1,23 @@
in vec2 TexCoord;
out vec4 FragColor;
uniform sampler2D ExposureTexture;
void main()
{
#if __VERSION__ < 400
ivec2 size = textureSize(ExposureTexture, 0);
ivec2 tl = max(ivec2(TexCoord * vec2(size) - 0.5), ivec2(0));
ivec2 br = min(tl + ivec2(1), size - ivec2(1));
vec4 values = vec4(
texelFetch(ExposureTexture, tl, 0).x,
texelFetch(ExposureTexture, ivec2(tl.x, br.y), 0).x,
texelFetch(ExposureTexture, ivec2(br.x, tl.y), 0).x,
texelFetch(ExposureTexture, br, 0).x);
#else
vec4 values = textureGather(ExposureTexture, TexCoord);
#endif
FragColor = vec4((values.x + values.y + values.z + values.w) * 0.25, 0.0, 0.0, 1.0);
}

View file

@ -0,0 +1,16 @@
in vec2 TexCoord;
out vec4 FragColor;
uniform sampler2D ExposureTexture;
uniform float ExposureBase;
uniform float ExposureMin;
uniform float ExposureScale;
uniform float ExposureSpeed;
void main()
{
float light = texture(ExposureTexture, TexCoord).x;
float exposureAdjustment = 1.0 / max(ExposureBase + light * ExposureScale, ExposureMin);
FragColor = vec4(exposureAdjustment, 0.0, 0.0, ExposureSpeed);
}

View file

@ -0,0 +1,13 @@
in vec2 TexCoord;
out vec4 FragColor;
uniform sampler2D SceneTexture;
uniform vec2 Scale;
uniform vec2 Offset;
void main()
{
vec4 color = texture(SceneTexture, Offset + TexCoord * Scale);
FragColor = vec4(max(max(color.r, color.g), color.b), 0.0, 0.0, 1.0);
}

View file

@ -3,7 +3,7 @@ in vec2 TexCoord;
out vec4 FragColor; out vec4 FragColor;
uniform sampler2D InputTexture; uniform sampler2D InputTexture;
uniform float ExposureAdjustment; uniform sampler2D ExposureTexture;
vec3 Linear(vec3 c) vec3 Linear(vec3 c)
{ {
@ -84,7 +84,8 @@ void main()
{ {
vec3 color = texture(InputTexture, TexCoord).rgb; vec3 color = texture(InputTexture, TexCoord).rgb;
#ifndef PALETTE #ifndef PALETTE
color = color * ExposureAdjustment; float exposureAdjustment = texture(ExposureTexture, vec2(0.5)).x;
color = color * exposureAdjustment;
color = Linear(color); // needed because gzdoom's scene texture is not linear at the moment color = Linear(color); // needed because gzdoom's scene texture is not linear at the moment
#endif #endif
FragColor = vec4(Tonemap(color), 1.0); FragColor = vec4(Tonemap(color), 1.0);