diff --git a/src/gl/renderer/gl_postprocess.cpp b/src/gl/renderer/gl_postprocess.cpp index b6b658a41..1cdad9f9b 100644 --- a/src/gl/renderer/gl_postprocess.cpp +++ b/src/gl/renderer/gl_postprocess.cpp @@ -75,7 +75,10 @@ CUSTOM_CVAR(Float, gl_bloom_amount, 1.4f, 0) 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) { @@ -106,6 +109,78 @@ void FGLRenderer::RenderScreenQuad() 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 @@ -121,6 +196,7 @@ void FGLRenderer::BloomScene() FGLDebug::PushGroup("BloomScene"); FGLPostProcessState savedState; + savedState.SaveTextureBinding1(); const float blurAmount = gl_bloom_amount; int sampleCount = gl_bloom_kernel_size; @@ -133,9 +209,12 @@ void FGLRenderer::BloomScene() mBuffers->BindCurrentTexture(0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_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->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->Offset.Set(mSceneViewport.left / (float)mScreenViewport.width, mSceneViewport.top / (float)mScreenViewport.height); RenderScreenQuad(); @@ -220,7 +299,12 @@ void FGLRenderer::TonemapScene() } else { - mTonemapShader->Exposure.Set(mCameraExposure); + savedState.SaveTextureBinding1(); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, mBuffers->ExposureTexture); + glActiveTexture(GL_TEXTURE0); + + mTonemapShader->ExposureTexture.Set(1); } RenderScreenQuad(); diff --git a/src/gl/renderer/gl_postprocessstate.cpp b/src/gl/renderer/gl_postprocessstate.cpp index 9d995783d..57b7862f5 100644 --- a/src/gl/renderer/gl_postprocessstate.cpp +++ b/src/gl/renderer/gl_postprocessstate.cpp @@ -45,7 +45,7 @@ FGLPostProcessState::FGLPostProcessState() { glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTex); glActiveTexture(GL_TEXTURE0); - glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding); + glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding[0]); glBindTexture(GL_TEXTURE_2D, 0); if (gl.flags & RFL_SAMPLER_OBJECTS) { @@ -75,6 +75,15 @@ FGLPostProcessState::FGLPostProcessState() 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 @@ -108,6 +117,12 @@ FGLPostProcessState::~FGLPostProcessState() glUseProgram(currentProgram); + if (textureBinding1Saved) + { + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, 0); + } + glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, 0); if (gl.flags & RFL_SAMPLER_OBJECTS) @@ -115,6 +130,13 @@ FGLPostProcessState::~FGLPostProcessState() glBindSampler(0, samplerBinding[0]); 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); } diff --git a/src/gl/renderer/gl_postprocessstate.h b/src/gl/renderer/gl_postprocessstate.h index 4f2ca81a1..bf53aa7de 100644 --- a/src/gl/renderer/gl_postprocessstate.h +++ b/src/gl/renderer/gl_postprocessstate.h @@ -14,12 +14,14 @@ public: FGLPostProcessState(); ~FGLPostProcessState(); + void SaveTextureBinding1(); + private: FGLPostProcessState(const FGLPostProcessState &) = delete; FGLPostProcessState &operator=(const FGLPostProcessState &) = delete; GLint activeTex; - GLint textureBinding; + GLint textureBinding[2]; GLint samplerBinding[2]; GLboolean blendEnabled; GLboolean scissorEnabled; @@ -32,6 +34,7 @@ private: GLint blendSrcAlpha; GLint blendDestRgb; GLint blendDestAlpha; + bool textureBinding1Saved = false; }; #endif diff --git a/src/gl/renderer/gl_renderbuffers.cpp b/src/gl/renderer/gl_renderbuffers.cpp index 4383d706e..b2471e4b9 100644 --- a/src/gl/renderer/gl_renderbuffers.cpp +++ b/src/gl/renderer/gl_renderbuffers.cpp @@ -74,6 +74,7 @@ FGLRenderBuffers::~FGLRenderBuffers() ClearPipeline(); ClearEyeBuffers(); ClearBloom(); + ClearExposureLevels(); } 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() { 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 - if (mBloomWidth != sceneWidth || mBloomHeight != sceneHeight) + if (mSceneWidth != sceneWidth || mSceneHeight != sceneHeight) { CreateBloom(sceneWidth, sceneHeight); - mBloomWidth = sceneWidth; - mBloomHeight = sceneHeight; + CreateExposureLevels(sceneWidth, sceneHeight); + mSceneWidth = sceneWidth; + mSceneHeight = sceneHeight; } glBindTexture(GL_TEXTURE_2D, textureBinding); @@ -204,11 +218,12 @@ bool FGLRenderBuffers::Setup(int width, int height, int sceneWidth, int sceneHei ClearPipeline(); ClearEyeBuffers(); ClearBloom(); + ClearExposureLevels(); mWidth = 0; mHeight = 0; mSamples = 0; - mBloomWidth = 0; - mBloomHeight = 0; + mSceneWidth = 0; + mSceneHeight = 0; } 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 @@ -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; glGenTextures(1, &handle); glBindTexture(GL_TEXTURE_2D, handle); 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_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); diff --git a/src/gl/renderer/gl_renderbuffers.h b/src/gl/renderer/gl_renderbuffers.h index 08731e39f..4477718f4 100644 --- a/src/gl/renderer/gl_renderbuffers.h +++ b/src/gl/renderer/gl_renderbuffers.h @@ -14,6 +14,15 @@ public: GLuint Height = 0; }; +class FGLExposureTextureLevel +{ +public: + GLuint Texture = 0; + GLuint Framebuffer = 0; + GLuint Width = 0; + GLuint Height = 0; +}; + class FGLRenderBuffers { public: @@ -39,6 +48,11 @@ public: enum { NumBloomLevels = 4 }; FGLBloomTextureLevel BloomLevels[NumBloomLevels]; + TArray ExposureLevels; + GLuint ExposureTexture = 0; + GLuint ExposureFB = 0; + bool FirstExposureFrame = true; + static bool IsEnabled(); int GetWidth() const { return mWidth; } @@ -49,11 +63,13 @@ private: void ClearPipeline(); void ClearEyeBuffers(); void ClearBloom(); + void ClearExposureLevels(); void CreateScene(int width, int height, int samples); void CreatePipeline(int width, int height); void CreateBloom(int width, int height); + void CreateExposureLevels(int width, int height); 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 samples, int width, int height); GLuint CreateFrameBuffer(const FString &name, GLuint colorbuffer); @@ -69,8 +85,8 @@ private: int mHeight = 0; int mSamples = 0; int mMaxSamples = 0; - int mBloomWidth = 0; - int mBloomHeight = 0; + int mSceneWidth = 0; + int mSceneHeight = 0; static const int NumPipelineTextures = 2; int mCurrentPipelineTexture = 0; diff --git a/src/gl/renderer/gl_renderer.cpp b/src/gl/renderer/gl_renderer.cpp index 4b59ad1a9..ab0e658e7 100644 --- a/src/gl/renderer/gl_renderer.cpp +++ b/src/gl/renderer/gl_renderer.cpp @@ -104,6 +104,9 @@ FGLRenderer::FGLRenderer(OpenGLFrameBuffer *fb) mPresentShader = nullptr; mBloomExtractShader = nullptr; mBloomCombineShader = nullptr; + mExposureExtractShader = nullptr; + mExposureAverageShader = nullptr; + mExposureCombineShader = nullptr; mBlurShader = nullptr; mTonemapShader = nullptr; mTonemapPalette = nullptr; @@ -119,6 +122,9 @@ void FGLRenderer::Initialize(int width, int height) mBuffers = new FGLRenderBuffers(); mBloomExtractShader = new FBloomExtractShader(); mBloomCombineShader = new FBloomCombineShader(); + mExposureExtractShader = new FExposureExtractShader(); + mExposureAverageShader = new FExposureAverageShader(); + mExposureCombineShader = new FExposureCombineShader(); mBlurShader = new FBlurShader(); mTonemapShader = new FTonemapShader(); mColormapShader = new FColormapShader(); @@ -179,6 +185,9 @@ FGLRenderer::~FGLRenderer() if (mPresentShader) delete mPresentShader; if (mBloomExtractShader) delete mBloomExtractShader; if (mBloomCombineShader) delete mBloomCombineShader; + if (mExposureExtractShader) delete mExposureExtractShader; + if (mExposureAverageShader) delete mExposureAverageShader; + if (mExposureCombineShader) delete mExposureCombineShader; if (mBlurShader) delete mBlurShader; if (mTonemapShader) delete mTonemapShader; if (mTonemapPalette) delete mTonemapPalette; diff --git a/src/gl/renderer/gl_renderer.h b/src/gl/renderer/gl_renderer.h index 162fb6dcf..b95408521 100644 --- a/src/gl/renderer/gl_renderer.h +++ b/src/gl/renderer/gl_renderer.h @@ -21,6 +21,9 @@ class DPSprite; class FGLRenderBuffers; class FBloomExtractShader; class FBloomCombineShader; +class FExposureExtractShader; +class FExposureAverageShader; +class FExposureCombineShader; class FBlurShader; class FTonemapShader; class FColormapShader; @@ -92,6 +95,9 @@ public: FGLRenderBuffers *mBuffers; FBloomExtractShader *mBloomExtractShader; FBloomCombineShader *mBloomCombineShader; + FExposureExtractShader *mExposureExtractShader; + FExposureAverageShader *mExposureAverageShader; + FExposureCombineShader *mExposureCombineShader; FBlurShader *mBlurShader; FTonemapShader *mTonemapShader; FColormapShader *mColormapShader; @@ -118,7 +124,6 @@ public: GL_IRECT mSceneViewport; GL_IRECT mOutputLetterbox; bool mDrawingScene2D = false; - float mCameraExposure = 1.0f; float mSceneClearColor[3]; @@ -166,6 +171,7 @@ public: void SetFixedColormap (player_t *player); void WriteSavePic (player_t *player, FILE *file, int width, int height); void EndDrawScene(sector_t * viewsector); + void UpdateCameraExposure(); void BloomScene(); void TonemapScene(); void ColormapScene(); diff --git a/src/gl/scene/gl_scene.cpp b/src/gl/scene/gl_scene.cpp index cca452800..7c04ce77a 100644 --- a/src/gl/scene/gl_scene.cpp +++ b/src/gl/scene/gl_scene.cpp @@ -790,20 +790,6 @@ sector_t * FGLRenderer::RenderViewpoint (AActor * camera, GL_IRECT * bounds, flo 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. lviewsector = viewsector; @@ -839,6 +825,7 @@ sector_t * FGLRenderer::RenderViewpoint (AActor * camera, GL_IRECT * bounds, flo if (mainview && FGLRenderBuffers::IsEnabled()) { mBuffers->BlitSceneToTexture(); + UpdateCameraExposure(); BloomScene(); TonemapScene(); ColormapScene(); diff --git a/src/gl/shaders/gl_bloomshader.cpp b/src/gl/shaders/gl_bloomshader.cpp index a16c9ed1e..44253ae79 100644 --- a/src/gl/shaders/gl_bloomshader.cpp +++ b/src/gl/shaders/gl_bloomshader.cpp @@ -46,7 +46,7 @@ void FBloomExtractShader::Bind() mShader.Link("shaders/glsl/bloomextract"); mShader.SetAttribLocation(0, "PositionInProjection"); SceneTexture.Init(mShader, "SceneTexture"); - Exposure.Init(mShader, "ExposureAdjustment"); + ExposureTexture.Init(mShader, "ExposureTexture"); Scale.Init(mShader, "Scale"); Offset.Init(mShader, "Offset"); } diff --git a/src/gl/shaders/gl_bloomshader.h b/src/gl/shaders/gl_bloomshader.h index b20277a42..8b34bb479 100644 --- a/src/gl/shaders/gl_bloomshader.h +++ b/src/gl/shaders/gl_bloomshader.h @@ -9,7 +9,7 @@ public: void Bind(); FBufferedUniformSampler SceneTexture; - FBufferedUniform1f Exposure; + FBufferedUniformSampler ExposureTexture; FBufferedUniform2f Scale; FBufferedUniform2f Offset; diff --git a/src/gl/shaders/gl_tonemapshader.cpp b/src/gl/shaders/gl_tonemapshader.cpp index 144981b18..3db6db8ba 100644 --- a/src/gl/shaders/gl_tonemapshader.cpp +++ b/src/gl/shaders/gl_tonemapshader.cpp @@ -47,7 +47,7 @@ void FTonemapShader::Bind() shader.Link("shaders/glsl/tonemap"); shader.SetAttribLocation(0, "PositionInProjection"); SceneTexture.Init(shader, "InputTexture"); - Exposure.Init(shader, "ExposureAdjustment"); + ExposureTexture.Init(shader, "ExposureTexture"); PaletteLUT.Init(shader, "PaletteLUT"); } shader.Bind(); @@ -70,3 +70,51 @@ const char *FTonemapShader::GetDefines(int mode) 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(); +} \ No newline at end of file diff --git a/src/gl/shaders/gl_tonemapshader.h b/src/gl/shaders/gl_tonemapshader.h index 7ec24117b..b4cd102de 100644 --- a/src/gl/shaders/gl_tonemapshader.h +++ b/src/gl/shaders/gl_tonemapshader.h @@ -9,7 +9,7 @@ public: void Bind(); FBufferedUniformSampler SceneTexture; - FBufferedUniform1f Exposure; + FBufferedUniformSampler ExposureTexture; FBufferedUniformSampler PaletteLUT; static bool IsPaletteMode(); @@ -31,4 +31,43 @@ private: 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 \ No newline at end of file diff --git a/wadsrc/static/shaders/glsl/bloomextract.fp b/wadsrc/static/shaders/glsl/bloomextract.fp index bc94c3c0e..9c5aa0bea 100644 --- a/wadsrc/static/shaders/glsl/bloomextract.fp +++ b/wadsrc/static/shaders/glsl/bloomextract.fp @@ -3,12 +3,13 @@ in vec2 TexCoord; out vec4 FragColor; uniform sampler2D SceneTexture; -uniform float ExposureAdjustment; +uniform sampler2D ExposureTexture; uniform vec2 Scale; uniform vec2 Offset; void main() { + float exposureAdjustment = texture(ExposureTexture, vec2(0.5)).x; 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)); } diff --git a/wadsrc/static/shaders/glsl/exposureaverage.fp b/wadsrc/static/shaders/glsl/exposureaverage.fp new file mode 100644 index 000000000..41c0909d2 --- /dev/null +++ b/wadsrc/static/shaders/glsl/exposureaverage.fp @@ -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); +} diff --git a/wadsrc/static/shaders/glsl/exposurecombine.fp b/wadsrc/static/shaders/glsl/exposurecombine.fp new file mode 100644 index 000000000..f806f8f86 --- /dev/null +++ b/wadsrc/static/shaders/glsl/exposurecombine.fp @@ -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); +} diff --git a/wadsrc/static/shaders/glsl/exposureextract.fp b/wadsrc/static/shaders/glsl/exposureextract.fp new file mode 100644 index 000000000..f673bf029 --- /dev/null +++ b/wadsrc/static/shaders/glsl/exposureextract.fp @@ -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); +} diff --git a/wadsrc/static/shaders/glsl/tonemap.fp b/wadsrc/static/shaders/glsl/tonemap.fp index d6574b295..5cb7cf8bd 100644 --- a/wadsrc/static/shaders/glsl/tonemap.fp +++ b/wadsrc/static/shaders/glsl/tonemap.fp @@ -3,7 +3,7 @@ in vec2 TexCoord; out vec4 FragColor; uniform sampler2D InputTexture; -uniform float ExposureAdjustment; +uniform sampler2D ExposureTexture; vec3 Linear(vec3 c) { @@ -84,7 +84,8 @@ void main() { vec3 color = texture(InputTexture, TexCoord).rgb; #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 #endif FragColor = vec4(Tonemap(color), 1.0);