From 06d20e13b80ec63d0377417830fa5458c99afbe1 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 29 Apr 2018 13:45:53 +0200 Subject: [PATCH] - cleaned up the gamma correction code. This had accumulated quite a bit of cruft by now and parts of it should be in non OpenGL code. --- src/gl/system/gl_framebuffer.cpp | 42 +++-------------------------- src/gl/system/gl_framebuffer.h | 5 +--- src/hwrenderer/utility/hw_cvars.cpp | 6 ++--- src/posix/cocoa/i_video.mm | 6 ++--- src/v_palette.cpp | 2 +- src/v_video.cpp | 32 +++++++++++++++++++--- src/v_video.h | 3 ++- 7 files changed, 43 insertions(+), 53 deletions(-) diff --git a/src/gl/system/gl_framebuffer.cpp b/src/gl/system/gl_framebuffer.cpp index 48e5c10ee4..2ebac1b76b 100644 --- a/src/gl/system/gl_framebuffer.cpp +++ b/src/gl/system/gl_framebuffer.cpp @@ -42,8 +42,6 @@ #include "gl_debug.h" #include "r_videoscale.h" -EXTERN_CVAR (Float, vid_brightness) -EXTERN_CVAR (Float, vid_contrast) EXTERN_CVAR (Bool, vid_vsync) CVAR(Bool, gl_aalines, false, CVAR_ARCHIVE) @@ -56,7 +54,7 @@ void gl_PrintStartupLog(); CUSTOM_CVAR(Int, vid_hwgamma, 2, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOINITCALL) { if (self < 0 || self > 2) self = 2; - if (GLRenderer != NULL && GLRenderer->framebuffer != NULL) GLRenderer->framebuffer->DoSetGamma(); + if (screen != nullptr) screen->SetGamma(); } //========================================================================== @@ -85,7 +83,7 @@ OpenGLFrameBuffer::OpenGLFrameBuffer(void *hMonitor, int width, int height, int InitializeState(); mDebug = std::make_shared(); mDebug->Update(); - DoSetGamma(); + SetGamma(); hwcaps = gl.flags; if (gl.legacyMode) hwcaps |= RFL_NO_SHADERS; } @@ -321,7 +319,7 @@ void OpenGLFrameBuffer::SetVSync(bool vsync) // //=========================================================================== -void OpenGLFrameBuffer::DoSetGamma() +void OpenGLFrameBuffer::SetGamma() { bool useHWGamma = m_supportsGamma && ((vid_hwgamma == 0) || (vid_hwgamma == 2 && IsFullscreen())); if (useHWGamma) @@ -329,21 +327,7 @@ void OpenGLFrameBuffer::DoSetGamma() uint16_t gammaTable[768]; // This formula is taken from Doomsday - float gamma = clamp(Gamma, 0.1f, 4.f); - float contrast = clamp(vid_contrast, 0.1f, 3.f); - float bright = clamp(vid_brightness, -0.8f, 0.8f); - - double invgamma = 1 / gamma; - double norm = pow(255., invgamma - 1); - - for (int i = 0; i < 256; i++) - { - double val = i * contrast - (contrast - 1) * 127; - val += bright * 128; - if(gamma != 1) val = pow(val, invgamma) / norm; - - gammaTable[i] = gammaTable[i + 256] = gammaTable[i + 512] = (uint16_t)clamp(val*256, 0, 0xffff); - } + BuildGammaTable(gammaTable); SetGammaTable(gammaTable); HWGammaActive = true; @@ -355,24 +339,6 @@ void OpenGLFrameBuffer::DoSetGamma() } } -bool OpenGLFrameBuffer::SetGamma(float gamma) -{ - DoSetGamma(); - return true; -} - -bool OpenGLFrameBuffer::SetBrightness(float bright) -{ - DoSetGamma(); - return true; -} - -bool OpenGLFrameBuffer::SetContrast(float contrast) -{ - DoSetGamma(); - return true; -} - //=========================================================================== // // diff --git a/src/gl/system/gl_framebuffer.h b/src/gl/system/gl_framebuffer.h index 0be4428f25..e82bcee033 100644 --- a/src/gl/system/gl_framebuffer.h +++ b/src/gl/system/gl_framebuffer.h @@ -23,10 +23,7 @@ public: void Update(); // Color correction - bool SetGamma (float gamma); - bool SetBrightness(float bright); - bool SetContrast(float contrast); - void DoSetGamma(); + void SetGamma(); void CleanForRestart() override; void UpdatePalette() override; diff --git a/src/hwrenderer/utility/hw_cvars.cpp b/src/hwrenderer/utility/hw_cvars.cpp index 305fbdda38..26c061de79 100644 --- a/src/hwrenderer/utility/hw_cvars.cpp +++ b/src/hwrenderer/utility/hw_cvars.cpp @@ -57,7 +57,7 @@ CUSTOM_CVAR (Float, vid_brightness, 0.f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) { if (screen != NULL) { - screen->SetGamma(Gamma); //Brightness (self); + screen->SetGamma(); } } @@ -65,7 +65,7 @@ CUSTOM_CVAR (Float, vid_contrast, 1.f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) { if (screen != NULL) { - screen->SetGamma(Gamma); //SetContrast (self); + screen->SetGamma(); } } @@ -73,7 +73,7 @@ CUSTOM_CVAR (Float, vid_saturation, 1.f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) { if (screen != NULL) { - screen->SetGamma(Gamma); + screen->SetGamma(); } } diff --git a/src/posix/cocoa/i_video.mm b/src/posix/cocoa/i_video.mm index 0b22ee6e7b..939e3c28b1 100644 --- a/src/posix/cocoa/i_video.mm +++ b/src/posix/cocoa/i_video.mm @@ -267,7 +267,7 @@ CUSTOM_CVAR(Float, rgamma, 1.0f, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) { if (NULL != screen) { - screen->SetGamma(Gamma); + screen->SetGamma(); } } @@ -275,7 +275,7 @@ CUSTOM_CVAR(Float, ggamma, 1.0f, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) { if (NULL != screen) { - screen->SetGamma(Gamma); + screen->SetGamma(); } } @@ -283,7 +283,7 @@ CUSTOM_CVAR(Float, bgamma, 1.0f, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) { if (NULL != screen) { - screen->SetGamma(Gamma); + screen->SetGamma(); } } diff --git a/src/v_palette.cpp b/src/v_palette.cpp index a836da1ede..814d7e7fa9 100644 --- a/src/v_palette.cpp +++ b/src/v_palette.cpp @@ -73,7 +73,7 @@ CUSTOM_CVAR (Float, Gamma, 1.f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) if (screen != NULL) { - screen->SetGamma (self); + screen->SetGamma (); } } diff --git a/src/v_video.cpp b/src/v_video.cpp index 5c0395331a..c0da989d5f 100644 --- a/src/v_video.cpp +++ b/src/v_video.cpp @@ -73,6 +73,8 @@ #include "i_time.h" EXTERN_CVAR(Bool, cl_capfps) +EXTERN_CVAR(Float, vid_brightness) +EXTERN_CVAR(Float, vid_contrast) CUSTOM_CVAR(Int, vid_maxfps, 200, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) { @@ -129,7 +131,6 @@ public: PalEntry *GetPalette() { DBGBREAK; return NULL; } void GetFlashedPalette(PalEntry palette[256]) { DBGBREAK; } void UpdatePalette() { DBGBREAK; } - bool SetGamma(float gamma) { Gamma = gamma; return true; } bool SetFlash(PalEntry rgb, int amount) { DBGBREAK; return false; } void GetFlash(PalEntry &rgb, int &amount) { DBGBREAK; } bool IsFullscreen() { DBGBREAK; return 0; } @@ -928,6 +929,31 @@ void DFrameBuffer::GameRestart() { } +//========================================================================== +// +// +// +//========================================================================== + +void DFrameBuffer::BuildGammaTable(uint16_t *gammaTable) +{ + float gamma = clamp(Gamma, 0.1f, 4.f); + float contrast = clamp(vid_contrast, 0.1f, 3.f); + float bright = clamp(vid_brightness, -0.8f, 0.8f); + + double invgamma = 1 / gamma; + double norm = pow(255., invgamma - 1); + + for (int i = 0; i < 256; i++) + { + double val = i * contrast - (contrast - 1) * 127; + val += bright * 128; + if (gamma != 1) val = pow(val, invgamma) / norm; + + gammaTable[i] = gammaTable[i + 256] = gammaTable[i + 512] = (uint16_t)clamp(val * 256, 0, 0xffff); + } +} + //========================================================================== // // DFrameBuffer :: GetCaps @@ -986,7 +1012,7 @@ bool V_DoModeSetup (int width, int height, int bits) } screen = buff; - screen->SetGamma (Gamma); + screen->SetGamma (); DisplayBits = bits; V_UpdateModeSize(screen->GetWidth(), screen->GetHeight()); @@ -1264,7 +1290,7 @@ void V_Init2() else Printf ("Resolution: %d x %d\n", SCREENWIDTH, SCREENHEIGHT); - screen->SetGamma (gamma); + screen->SetGamma (); FBaseCVar::ResetColors (); C_NewModeAdjust(); M_InitVideoModesMenu(); diff --git a/src/v_video.h b/src/v_video.h index 92f1dc37d7..f7b2f20c81 100644 --- a/src/v_video.h +++ b/src/v_video.h @@ -318,6 +318,7 @@ protected: template bool ParseDrawTextureTags(FTexture *img, double x, double y, uint32_t tag, T& tags, DrawParms *parms, bool fortext) const; void DrawTextCommon(FFont *font, int normalcolor, double x, double y, const char *string, DrawParms &parms); + void BuildGammaTable(uint16_t *gt); F2DDrawer m2DDrawer; int Width = 0; @@ -351,7 +352,7 @@ public: // Sets the gamma level. Returns false if the hardware does not support // gamma changing. (Always true for now, since palettes can always be // gamma adjusted.) - virtual bool SetGamma (float gamma) = 0; + virtual void SetGamma() {} // Sets a color flash. RGB is the color, and amount is 0-256, with 256 // being all flash and 0 being no flash. Returns false if the hardware