Merge branch 'exposure_pass' into lightmath

# Conflicts:
#	src/gl/renderer/gl_renderbuffers.cpp
#	src/gl/renderer/gl_renderbuffers.h
#	src/gl/renderer/gl_renderer.h
#	src/gl/scene/gl_scene.cpp
This commit is contained in:
Magnus Norddahl 2016-09-18 16:22:44 +02:00
commit 38be2333d0
16 changed files with 327 additions and 34 deletions

View file

@ -76,7 +76,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)
{ {
@ -120,6 +123,7 @@ void FGLRenderer::RenderScreenQuad()
void FGLRenderer::PostProcessScene() void FGLRenderer::PostProcessScene()
{ {
mBuffers->BlitSceneToTexture(); mBuffers->BlitSceneToTexture();
UpdateCameraExposure();
BloomScene(); BloomScene();
TonemapScene(); TonemapScene();
ColormapScene(); ColormapScene();
@ -242,6 +246,78 @@ void FGLRenderer::AmbientOccludeScene()
FGLDebug::PopGroup(); FGLDebug::PopGroup();
} }
//-----------------------------------------------------------------------------
//
// 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
@ -257,6 +333,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;
@ -269,9 +346,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();
@ -356,7 +436,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

@ -75,6 +75,7 @@ FGLRenderBuffers::~FGLRenderBuffers()
ClearPipeline(); ClearPipeline();
ClearEyeBuffers(); ClearEyeBuffers();
ClearBloom(); ClearBloom();
ClearExposureLevels();
ClearAmbientOcclusion(); ClearAmbientOcclusion();
} }
@ -107,13 +108,16 @@ void FGLRenderBuffers::ClearBloom()
} }
} }
void FGLRenderBuffers::ClearAmbientOcclusion() void FGLRenderBuffers::ClearExposureLevels()
{ {
DeleteFrameBuffer(AmbientFB0); for (auto &level : ExposureLevels)
DeleteFrameBuffer(AmbientFB1); {
DeleteTexture(AmbientTexture0); DeleteTexture(level.Texture);
DeleteTexture(AmbientTexture1); DeleteFrameBuffer(level.Framebuffer);
DeleteTexture(AmbientRandomTexture); }
ExposureLevels.Clear();
DeleteTexture(ExposureTexture);
DeleteFrameBuffer(ExposureFB);
} }
void FGLRenderBuffers::ClearEyeBuffers() void FGLRenderBuffers::ClearEyeBuffers()
@ -128,6 +132,15 @@ void FGLRenderBuffers::ClearEyeBuffers()
mEyeFBs.Clear(); mEyeFBs.Clear();
} }
void FGLRenderBuffers::ClearAmbientOcclusion()
{
DeleteFrameBuffer(AmbientFB0);
DeleteFrameBuffer(AmbientFB1);
DeleteTexture(AmbientTexture0);
DeleteTexture(AmbientTexture1);
DeleteTexture(AmbientRandomTexture);
}
void FGLRenderBuffers::DeleteTexture(GLuint &handle) void FGLRenderBuffers::DeleteTexture(GLuint &handle)
{ {
if (handle != 0) if (handle != 0)
@ -198,6 +211,7 @@ bool FGLRenderBuffers::Setup(int width, int height, int sceneWidth, int sceneHei
if (mSceneWidth != sceneWidth || mSceneHeight != sceneHeight) if (mSceneWidth != sceneWidth || mSceneHeight != sceneHeight)
{ {
CreateBloom(sceneWidth, sceneHeight); CreateBloom(sceneWidth, sceneHeight);
CreateExposureLevels(sceneWidth, sceneHeight);
CreateAmbientOcclusion(sceneWidth, sceneHeight); CreateAmbientOcclusion(sceneWidth, sceneHeight);
mSceneWidth = sceneWidth; mSceneWidth = sceneWidth;
mSceneHeight = sceneHeight; mSceneHeight = sceneHeight;
@ -214,6 +228,7 @@ 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;
@ -338,6 +353,41 @@ void FGLRenderBuffers::CreateAmbientOcclusion(int width, int height)
AmbientRandomTexture = Create2DTexture("AmbientRandomTexture", GL_RGBA16_SNORM, 4, 4, randomValues); AmbientRandomTexture = Create2DTexture("AmbientRandomTexture", GL_RGBA16_SNORM, 4, 4, randomValues);
} }
//==========================================================================
//
// 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

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:
@ -41,6 +50,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;
// Ambient occlusion buffers // Ambient occlusion buffers
GLuint AmbientTexture0 = 0; GLuint AmbientTexture0 = 0;
GLuint AmbientTexture1 = 0; GLuint AmbientTexture1 = 0;
@ -63,12 +77,14 @@ private:
void ClearPipeline(); void ClearPipeline();
void ClearEyeBuffers(); void ClearEyeBuffers();
void ClearBloom(); void ClearBloom();
void ClearExposureLevels();
void ClearAmbientOcclusion(); void ClearAmbientOcclusion();
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 CreateAmbientOcclusion(int width, int height); void CreateExposureLevels(int width, int height);
void CreateEyeBuffers(int eye); void CreateEyeBuffers(int eye);
void CreateAmbientOcclusion(int width, int height);
GLuint Create2DTexture(const FString &name, GLuint format, int width, int height, const void *data = nullptr); GLuint Create2DTexture(const FString &name, GLuint format, int width, int height, const void *data = nullptr);
GLuint Create2DMultisampleTexture(const FString &name, GLuint format, int width, int height, int samples, bool fixedSampleLocations); GLuint Create2DMultisampleTexture(const FString &name, GLuint format, int width, int height, int samples, bool fixedSampleLocations);
GLuint CreateRenderBuffer(const FString &name, GLuint format, int width, int height); GLuint CreateRenderBuffer(const FString &name, GLuint format, int width, int height);

View file

@ -105,6 +105,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;
@ -128,6 +131,9 @@ void FGLRenderer::Initialize(int width, int height)
mSSAOCombineShader = new FSSAOCombineShader(); mSSAOCombineShader = new FSSAOCombineShader();
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();
@ -192,6 +198,9 @@ FGLRenderer::~FGLRenderer()
if (mSSAOCombineShader) delete mSSAOCombineShader; if (mSSAOCombineShader) delete mSSAOCombineShader;
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

@ -25,6 +25,9 @@ class FSSAOShader;
class FSSAOCombineShader; class FSSAOCombineShader;
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;
@ -100,6 +103,9 @@ public:
FSSAOCombineShader *mSSAOCombineShader; FSSAOCombineShader *mSSAOCombineShader;
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;
@ -126,7 +132,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];
@ -176,6 +181,7 @@ public:
void EndDrawScene(sector_t * viewsector); void EndDrawScene(sector_t * viewsector);
void PostProcessScene(); void PostProcessScene();
void AmbientOccludeScene(); void AmbientOccludeScene();
void UpdateCameraExposure();
void BloomScene(); void BloomScene();
void TonemapScene(); void TonemapScene();
void ColormapScene(); void ColormapScene();

View file

@ -427,7 +427,7 @@ void GLPortal::End(bool usestencil)
gl_RenderState.SetEffect(EFF_STENCIL); gl_RenderState.SetEffect(EFF_STENCIL);
gl_RenderState.EnableTexture(false); gl_RenderState.EnableTexture(false);
gl_RenderState.BlendFunc(GL_ONE, GL_ZERO); gl_RenderState.BlendFunc(GL_ONE, GL_ZERO);
gl_RenderState.BlendEquation(GL_ADD); gl_RenderState.BlendEquation(GL_FUNC_ADD);
gl_RenderState.Apply(); gl_RenderState.Apply();
DrawPortalStencil(); DrawPortalStencil();
gl_RenderState.SetEffect(EFF_NONE); gl_RenderState.SetEffect(EFF_NONE);

View file

@ -792,20 +792,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;

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);