From 333560086de84cddacccff034afb887d76dc728b Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Mon, 2 May 2016 10:35:38 +0300 Subject: [PATCH] Calculate color values for gamma correction directly in shader Gamma table texture is no longer needed --- src/posix/cocoa/i_video.mm | 32 +++---------------- src/posix/cocoa/sdlglvideo.h | 6 ---- wadsrc/static/shaders/glsl/gammacorrection.fp | 30 +++++++++-------- 3 files changed, 21 insertions(+), 47 deletions(-) diff --git a/src/posix/cocoa/i_video.mm b/src/posix/cocoa/i_video.mm index fc415535f..069239b48 100644 --- a/src/posix/cocoa/i_video.mm +++ b/src/posix/cocoa/i_video.mm @@ -1150,19 +1150,14 @@ void CocoaFrameBuffer::Flip() // --------------------------------------------------------------------------- -static const uint32_t GAMMA_TABLE_ALPHA = 0xFF000000; - - SDLGLFB::SDLGLFB(void*, const int width, const int height, int, int, const bool fullscreen) : DFrameBuffer(width, height) , m_lock(-1) , m_isUpdatePending(false) -, m_gammaTexture(GAMMA_TABLE_SIZE, 1, true) { } SDLGLFB::SDLGLFB() -: m_gammaTexture(0, 0, false) { } @@ -1243,26 +1238,6 @@ void SDLGLFB::SwapBuffers() void SDLGLFB::SetGammaTable(WORD* table) { - const WORD* const red = &table[ 0]; - const WORD* const green = &table[256]; - const WORD* const blue = &table[512]; - - for (size_t i = 0; i < GAMMA_TABLE_SIZE; ++i) - { - // Convert 16 bits colors to 8 bits by dividing on 256 - - const uint32_t r = red[i] >> 8; - const uint32_t g = green[i] >> 8; - const uint32_t b = blue[i] >> 8; - - m_gammaTable[i] = GAMMA_TABLE_ALPHA + (b << 16) + (g << 8) + r; - } - - m_gammaTexture.CreateTexture( - reinterpret_cast(m_gammaTable), - GAMMA_TABLE_SIZE, 1, 1, false, 0); - - GLRenderer->mSamplerManager->Bind(1, CLAMP_NOFILTER, -1); } @@ -1278,12 +1253,15 @@ void BoundTextureSetFilter(const GLenum target, const GLint filter) glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); } +EXTERN_CVAR(Float, vid_brightness) +EXTERN_CVAR(Float, vid_contrast) + void BoundTextureDraw2D(const GLsizei width, const GLsizei height) { gl_RenderState.SetEffect(EFF_GAMMACORRECTION); gl_RenderState.SetTextureMode(TM_OPAQUE); gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f); - gl_RenderState.ResetColor(); + gl_RenderState.SetColor(Gamma, vid_contrast, vid_brightness); gl_RenderState.Apply(); static const float x = 0.f; @@ -1490,9 +1468,7 @@ void CocoaOpenGLFrameBuffer::GetScreenshotBuffer(const BYTE*& buffer, int& pitch void CocoaOpenGLFrameBuffer::DrawRenderTarget() { m_renderTarget.Unbind(); - m_renderTarget.GetColorTexture().Bind(0, 0, false); - m_gammaTexture.Bind(1, 0, false); if (rbOpts.dirty) { diff --git a/src/posix/cocoa/sdlglvideo.h b/src/posix/cocoa/sdlglvideo.h index aac8992d9..439b4d5ce 100644 --- a/src/posix/cocoa/sdlglvideo.h +++ b/src/posix/cocoa/sdlglvideo.h @@ -70,12 +70,6 @@ protected: static const bool m_supportsGamma = true; - FHardwareTexture m_gammaTexture; - - static const size_t GAMMA_TABLE_SIZE = 256; - uint32_t m_gammaTable[GAMMA_TABLE_SIZE]; - - SDLGLFB(); void InitializeState(); diff --git a/wadsrc/static/shaders/glsl/gammacorrection.fp b/wadsrc/static/shaders/glsl/gammacorrection.fp index f8c2cf6a3..1deb1a600 100644 --- a/wadsrc/static/shaders/glsl/gammacorrection.fp +++ b/wadsrc/static/shaders/glsl/gammacorrection.fp @@ -1,24 +1,28 @@ uniform sampler2D tex; -uniform sampler2D texture2; in vec4 vTexCoord; - -void applyGamma(int channel) -{ - vec4 color = texture(texture2, vec2(FragColor[channel], 0.0)); - FragColor[channel] = color[channel]; -} +in vec4 vColor; void main() { - FragColor = texture(tex, vTexCoord.st); + vec3 color = texture(tex, vTexCoord.st).rgb; // /* DEBUG */ if (vTexCoord.x > 0.5) { - applyGamma(0); - applyGamma(1); - applyGamma(2); + // Apply contrast + float contrast = clamp(vColor.y, 0.1, 3.0); + color = color.rgb * contrast - (contrast - 1.0) * 0.5; + + // Apply gamma + float gamma = clamp(vColor.x, 0.1, 4.0); + color = sign(color) * pow(abs(color), vec3(1.0 / gamma)); + + // Apply brightness + float brightness = clamp(vColor.z, -0.8, 0.8); + color += brightness * 0.5; + + color = clamp(color, 0.0, 1.0); } - - FragColor.a = 1.0; + + FragColor = vec4(color, 1.0); }