- 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.
This commit is contained in:
Christoph Oelckers 2018-04-29 13:45:53 +02:00
parent 9e6f3787c6
commit 06d20e13b8
7 changed files with 43 additions and 53 deletions

View file

@ -42,8 +42,6 @@
#include "gl_debug.h" #include "gl_debug.h"
#include "r_videoscale.h" #include "r_videoscale.h"
EXTERN_CVAR (Float, vid_brightness)
EXTERN_CVAR (Float, vid_contrast)
EXTERN_CVAR (Bool, vid_vsync) EXTERN_CVAR (Bool, vid_vsync)
CVAR(Bool, gl_aalines, false, CVAR_ARCHIVE) 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) CUSTOM_CVAR(Int, vid_hwgamma, 2, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOINITCALL)
{ {
if (self < 0 || self > 2) self = 2; 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(); InitializeState();
mDebug = std::make_shared<FGLDebug>(); mDebug = std::make_shared<FGLDebug>();
mDebug->Update(); mDebug->Update();
DoSetGamma(); SetGamma();
hwcaps = gl.flags; hwcaps = gl.flags;
if (gl.legacyMode) hwcaps |= RFL_NO_SHADERS; 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())); bool useHWGamma = m_supportsGamma && ((vid_hwgamma == 0) || (vid_hwgamma == 2 && IsFullscreen()));
if (useHWGamma) if (useHWGamma)
@ -329,21 +327,7 @@ void OpenGLFrameBuffer::DoSetGamma()
uint16_t gammaTable[768]; uint16_t gammaTable[768];
// This formula is taken from Doomsday // This formula is taken from Doomsday
float gamma = clamp<float>(Gamma, 0.1f, 4.f); BuildGammaTable(gammaTable);
float contrast = clamp<float>(vid_contrast, 0.1f, 3.f);
float bright = clamp<float>(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<double>(val*256, 0, 0xffff);
}
SetGammaTable(gammaTable); SetGammaTable(gammaTable);
HWGammaActive = true; 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;
}
//=========================================================================== //===========================================================================
// //
// //

View file

@ -23,10 +23,7 @@ public:
void Update(); void Update();
// Color correction // Color correction
bool SetGamma (float gamma); void SetGamma();
bool SetBrightness(float bright);
bool SetContrast(float contrast);
void DoSetGamma();
void CleanForRestart() override; void CleanForRestart() override;
void UpdatePalette() override; void UpdatePalette() override;

View file

@ -57,7 +57,7 @@ CUSTOM_CVAR (Float, vid_brightness, 0.f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
{ {
if (screen != NULL) 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) 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) if (screen != NULL)
{ {
screen->SetGamma(Gamma); screen->SetGamma();
} }
} }

View file

@ -267,7 +267,7 @@ CUSTOM_CVAR(Float, rgamma, 1.0f, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
{ {
if (NULL != screen) 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) 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) if (NULL != screen)
{ {
screen->SetGamma(Gamma); screen->SetGamma();
} }
} }

View file

@ -73,7 +73,7 @@ CUSTOM_CVAR (Float, Gamma, 1.f, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
if (screen != NULL) if (screen != NULL)
{ {
screen->SetGamma (self); screen->SetGamma ();
} }
} }

View file

@ -73,6 +73,8 @@
#include "i_time.h" #include "i_time.h"
EXTERN_CVAR(Bool, cl_capfps) 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) CUSTOM_CVAR(Int, vid_maxfps, 200, CVAR_ARCHIVE | CVAR_GLOBALCONFIG)
{ {
@ -129,7 +131,6 @@ public:
PalEntry *GetPalette() { DBGBREAK; return NULL; } PalEntry *GetPalette() { DBGBREAK; return NULL; }
void GetFlashedPalette(PalEntry palette[256]) { DBGBREAK; } void GetFlashedPalette(PalEntry palette[256]) { DBGBREAK; }
void UpdatePalette() { DBGBREAK; } void UpdatePalette() { DBGBREAK; }
bool SetGamma(float gamma) { Gamma = gamma; return true; }
bool SetFlash(PalEntry rgb, int amount) { DBGBREAK; return false; } bool SetFlash(PalEntry rgb, int amount) { DBGBREAK; return false; }
void GetFlash(PalEntry &rgb, int &amount) { DBGBREAK; } void GetFlash(PalEntry &rgb, int &amount) { DBGBREAK; }
bool IsFullscreen() { DBGBREAK; return 0; } bool IsFullscreen() { DBGBREAK; return 0; }
@ -928,6 +929,31 @@ void DFrameBuffer::GameRestart()
{ {
} }
//==========================================================================
//
//
//
//==========================================================================
void DFrameBuffer::BuildGammaTable(uint16_t *gammaTable)
{
float gamma = clamp<float>(Gamma, 0.1f, 4.f);
float contrast = clamp<float>(vid_contrast, 0.1f, 3.f);
float bright = clamp<float>(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<double>(val * 256, 0, 0xffff);
}
}
//========================================================================== //==========================================================================
// //
// DFrameBuffer :: GetCaps // DFrameBuffer :: GetCaps
@ -986,7 +1012,7 @@ bool V_DoModeSetup (int width, int height, int bits)
} }
screen = buff; screen = buff;
screen->SetGamma (Gamma); screen->SetGamma ();
DisplayBits = bits; DisplayBits = bits;
V_UpdateModeSize(screen->GetWidth(), screen->GetHeight()); V_UpdateModeSize(screen->GetWidth(), screen->GetHeight());
@ -1264,7 +1290,7 @@ void V_Init2()
else else
Printf ("Resolution: %d x %d\n", SCREENWIDTH, SCREENHEIGHT); Printf ("Resolution: %d x %d\n", SCREENWIDTH, SCREENHEIGHT);
screen->SetGamma (gamma); screen->SetGamma ();
FBaseCVar::ResetColors (); FBaseCVar::ResetColors ();
C_NewModeAdjust(); C_NewModeAdjust();
M_InitVideoModesMenu(); M_InitVideoModesMenu();

View file

@ -318,6 +318,7 @@ protected:
template<class T> template<class T>
bool ParseDrawTextureTags(FTexture *img, double x, double y, uint32_t tag, T& tags, DrawParms *parms, bool fortext) const; 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 DrawTextCommon(FFont *font, int normalcolor, double x, double y, const char *string, DrawParms &parms);
void BuildGammaTable(uint16_t *gt);
F2DDrawer m2DDrawer; F2DDrawer m2DDrawer;
int Width = 0; int Width = 0;
@ -351,7 +352,7 @@ public:
// Sets the gamma level. Returns false if the hardware does not support // Sets the gamma level. Returns false if the hardware does not support
// gamma changing. (Always true for now, since palettes can always be // gamma changing. (Always true for now, since palettes can always be
// gamma adjusted.) // 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 // 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 // being all flash and 0 being no flash. Returns false if the hardware