Implemented gamma correction in OS X native backend using shader effect

This commit is contained in:
alexey.lysiuk 2016-05-01 16:59:17 +03:00
parent f2f3fa6d09
commit 44b019413c
5 changed files with 37 additions and 22 deletions

View file

@ -37,6 +37,7 @@ enum EEffect
EFF_SPHEREMAP, EFF_SPHEREMAP,
EFF_BURN, EFF_BURN,
EFF_STENCIL, EFF_STENCIL,
EFF_GAMMACORRECTION,
MAX_EFFECTS MAX_EFFECTS
}; };

View file

@ -433,6 +433,7 @@ static const FEffectShader effectshaders[]=
{ "spheremap", "shaders/glsl/main.vp", "shaders/glsl/main.fp", "shaders/glsl/func_normal.fp", "#define SPHEREMAP\n#define NO_ALPHATEST\n" }, { "spheremap", "shaders/glsl/main.vp", "shaders/glsl/main.fp", "shaders/glsl/func_normal.fp", "#define SPHEREMAP\n#define NO_ALPHATEST\n" },
{ "burn", "shaders/glsl/main.vp", "shaders/glsl/burn.fp", NULL, "#define SIMPLE\n#define NO_ALPHATEST\n" }, { "burn", "shaders/glsl/main.vp", "shaders/glsl/burn.fp", NULL, "#define SIMPLE\n#define NO_ALPHATEST\n" },
{ "stencil", "shaders/glsl/main.vp", "shaders/glsl/stencil.fp", NULL, "#define SIMPLE\n#define NO_ALPHATEST\n" }, { "stencil", "shaders/glsl/main.vp", "shaders/glsl/stencil.fp", NULL, "#define SIMPLE\n#define NO_ALPHATEST\n" },
{ "gammacorrection", "shaders/glsl/main.vp", "shaders/glsl/gammacorrection.fp", NULL, "#define SIMPLE\n" },
}; };

View file

@ -63,6 +63,7 @@
#include "gl/renderer/gl_renderer.h" #include "gl/renderer/gl_renderer.h"
#include "gl/system/gl_framebuffer.h" #include "gl/system/gl_framebuffer.h"
#include "gl/system/gl_interface.h" #include "gl/system/gl_interface.h"
#include "gl/textures/gl_samplers.h"
#include "gl/utility/gl_clock.h" #include "gl/utility/gl_clock.h"
#undef Class #undef Class
@ -1156,15 +1157,12 @@ SDLGLFB::SDLGLFB(void*, const int width, const int height, int, int, const bool
: DFrameBuffer(width, height) : DFrameBuffer(width, height)
, m_lock(-1) , m_lock(-1)
, m_isUpdatePending(false) , m_isUpdatePending(false)
, m_supportsGamma(true)
, m_gammaProgram("gamma_correction")
, m_gammaTexture(GAMMA_TABLE_SIZE, 1, true) , m_gammaTexture(GAMMA_TABLE_SIZE, 1, true)
{ {
} }
SDLGLFB::SDLGLFB() SDLGLFB::SDLGLFB()
: m_gammaProgram(nullptr) : m_gammaTexture(0, 0, false)
, m_gammaTexture(0, 0, false)
{ {
} }
@ -1262,7 +1260,9 @@ void SDLGLFB::SetGammaTable(WORD* table)
m_gammaTexture.CreateTexture( m_gammaTexture.CreateTexture(
reinterpret_cast<unsigned char*>(m_gammaTable), reinterpret_cast<unsigned char*>(m_gammaTable),
GAMMA_TABLE_SIZE, 1, false, 1, 0); GAMMA_TABLE_SIZE, 1, 1, false, 0);
GLRenderer->mSamplerManager->Bind(1, CLAMP_NOFILTER, -1);
} }
@ -1280,6 +1280,7 @@ void BoundTextureSetFilter(const GLenum target, const GLint filter)
void BoundTextureDraw2D(const GLsizei width, const GLsizei height) void BoundTextureDraw2D(const GLsizei width, const GLsizei height)
{ {
gl_RenderState.SetEffect(EFF_GAMMACORRECTION);
gl_RenderState.SetTextureMode(TM_OPAQUE); gl_RenderState.SetTextureMode(TM_OPAQUE);
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f); gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f);
gl_RenderState.ResetColor(); gl_RenderState.ResetColor();
@ -1304,6 +1305,8 @@ void BoundTextureDraw2D(const GLsizei width, const GLsizei height)
ptr->Set(x + w, y + h, 0, u2, v2); ++ptr; ptr->Set(x + w, y + h, 0, u2, v2); ++ptr;
vbo->RenderCurrent(ptr, GL_TRIANGLE_STRIP); vbo->RenderCurrent(ptr, GL_TRIANGLE_STRIP);
gl_RenderState.SetEffect(EFF_NONE);
} }
bool BoundTextureSaveAsPNG(const GLenum target, const char* const path) bool BoundTextureSaveAsPNG(const GLenum target, const char* const path)
@ -1387,7 +1390,7 @@ RenderTarget::RenderTarget(const GLsizei width, const GLsizei height)
glGenFramebuffersEXT(1, &m_ID); glGenFramebuffersEXT(1, &m_ID);
Bind(); Bind();
m_texture.CreateTexture(NULL, width, height, false, 0, 0); m_texture.CreateTexture(NULL, width, height, 0, false, 0);
m_texture.BindToFrameBuffer(); m_texture.BindToFrameBuffer();
Unbind(); Unbind();
} }
@ -1433,19 +1436,7 @@ CocoaOpenGLFrameBuffer::CocoaOpenGLFrameBuffer(void* hMonitor, int width, int he
, m_renderTarget(width, height) , m_renderTarget(width, height)
{ {
SetSmoothPicture(gl_smooth_rendered); SetSmoothPicture(gl_smooth_rendered);
/*
// Setup uniform samplers for gamma correction shader
m_gammaProgram.Load("GammaCorrection", "shaders/glsl/main.vp",
"shaders/glsl/gamma_correction.fp", NULL, "");
const GLuint program = m_gammaProgram.GetHandle();
glUseProgram(program);
glUniform1i(glGetUniformLocation(program, "backbuffer"), 0);
glUniform1i(glGetUniformLocation(program, "gammaTable"), 1);
glUseProgram(0);
*/
// Fill render target with black color // Fill render target with black color
m_renderTarget.Bind(); m_renderTarget.Bind();
@ -1515,7 +1506,6 @@ void CocoaOpenGLFrameBuffer::DrawRenderTarget()
glViewport(rbOpts.shiftX, rbOpts.shiftY, rbOpts.width, rbOpts.height); glViewport(rbOpts.shiftX, rbOpts.shiftY, rbOpts.width, rbOpts.height);
//m_gammaProgram.Bind();
BoundTextureDraw2D(Width, Height); BoundTextureDraw2D(Width, Height);
glViewport(0, 0, Width, Height); glViewport(0, 0, Width, Height);
@ -1525,7 +1515,7 @@ void CocoaOpenGLFrameBuffer::DrawRenderTarget()
void CocoaOpenGLFrameBuffer::SetSmoothPicture(const bool smooth) void CocoaOpenGLFrameBuffer::SetSmoothPicture(const bool smooth)
{ {
FHardwareTexture& texture = m_renderTarget.GetColorTexture(); FHardwareTexture& texture = m_renderTarget.GetColorTexture();
texture.Bind(0, 0, 0); texture.Bind(0, 0, false);
BoundTextureSetFilter(GL_TEXTURE_2D, smooth ? GL_LINEAR : GL_NEAREST); BoundTextureSetFilter(GL_TEXTURE_2D, smooth ? GL_LINEAR : GL_NEAREST);
} }

View file

@ -68,9 +68,8 @@ protected:
int m_lock; int m_lock;
bool m_isUpdatePending; bool m_isUpdatePending;
bool m_supportsGamma; static const bool m_supportsGamma = true;
FShader m_gammaProgram;
FHardwareTexture m_gammaTexture; FHardwareTexture m_gammaTexture;
static const size_t GAMMA_TABLE_SIZE = 256; static const size_t GAMMA_TABLE_SIZE = 256;

View file

@ -0,0 +1,24 @@
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];
}
void main()
{
FragColor = texture(tex, vTexCoord.st);
// /* DEBUG */ if (vTexCoord.x > 0.5)
{
applyGamma(0);
applyGamma(1);
applyGamma(2);
}
FragColor.a = 1.0;
}