Calculate color values for gamma correction directly in shader

Gamma table texture is no longer needed
This commit is contained in:
alexey.lysiuk 2016-05-02 10:35:38 +03:00
parent 44b019413c
commit 333560086d
3 changed files with 21 additions and 47 deletions

View file

@ -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<unsigned char*>(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)
{

View file

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

View file

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