From 44b019413c43c1fd814fd266caf652207af40fcf Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Sun, 1 May 2016 16:59:17 +0300 Subject: [PATCH] Implemented gamma correction in OS X native backend using shader effect --- src/gl/renderer/gl_renderstate.h | 1 + src/gl/shaders/gl_shader.cpp | 1 + src/posix/cocoa/i_video.mm | 30 +++++++------------ src/posix/cocoa/sdlglvideo.h | 3 +- wadsrc/static/shaders/glsl/gammacorrection.fp | 24 +++++++++++++++ 5 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 wadsrc/static/shaders/glsl/gammacorrection.fp diff --git a/src/gl/renderer/gl_renderstate.h b/src/gl/renderer/gl_renderstate.h index 829edeec0..cebbb9570 100644 --- a/src/gl/renderer/gl_renderstate.h +++ b/src/gl/renderer/gl_renderstate.h @@ -37,6 +37,7 @@ enum EEffect EFF_SPHEREMAP, EFF_BURN, EFF_STENCIL, + EFF_GAMMACORRECTION, MAX_EFFECTS }; diff --git a/src/gl/shaders/gl_shader.cpp b/src/gl/shaders/gl_shader.cpp index 395319734..551707398 100644 --- a/src/gl/shaders/gl_shader.cpp +++ b/src/gl/shaders/gl_shader.cpp @@ -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" }, { "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" }, + { "gammacorrection", "shaders/glsl/main.vp", "shaders/glsl/gammacorrection.fp", NULL, "#define SIMPLE\n" }, }; diff --git a/src/posix/cocoa/i_video.mm b/src/posix/cocoa/i_video.mm index 726733b05..fc415535f 100644 --- a/src/posix/cocoa/i_video.mm +++ b/src/posix/cocoa/i_video.mm @@ -63,6 +63,7 @@ #include "gl/renderer/gl_renderer.h" #include "gl/system/gl_framebuffer.h" #include "gl/system/gl_interface.h" +#include "gl/textures/gl_samplers.h" #include "gl/utility/gl_clock.h" #undef Class @@ -1156,15 +1157,12 @@ SDLGLFB::SDLGLFB(void*, const int width, const int height, int, int, const bool : DFrameBuffer(width, height) , m_lock(-1) , m_isUpdatePending(false) -, m_supportsGamma(true) -, m_gammaProgram("gamma_correction") , m_gammaTexture(GAMMA_TABLE_SIZE, 1, true) { } 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( reinterpret_cast(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) { + gl_RenderState.SetEffect(EFF_GAMMACORRECTION); gl_RenderState.SetTextureMode(TM_OPAQUE); gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f); 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; vbo->RenderCurrent(ptr, GL_TRIANGLE_STRIP); + + gl_RenderState.SetEffect(EFF_NONE); } 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); Bind(); - m_texture.CreateTexture(NULL, width, height, false, 0, 0); + m_texture.CreateTexture(NULL, width, height, 0, false, 0); m_texture.BindToFrameBuffer(); Unbind(); } @@ -1433,19 +1436,7 @@ CocoaOpenGLFrameBuffer::CocoaOpenGLFrameBuffer(void* hMonitor, int width, int he , m_renderTarget(width, height) { 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 m_renderTarget.Bind(); @@ -1515,7 +1506,6 @@ void CocoaOpenGLFrameBuffer::DrawRenderTarget() glViewport(rbOpts.shiftX, rbOpts.shiftY, rbOpts.width, rbOpts.height); - //m_gammaProgram.Bind(); BoundTextureDraw2D(Width, Height); glViewport(0, 0, Width, Height); @@ -1525,7 +1515,7 @@ void CocoaOpenGLFrameBuffer::DrawRenderTarget() void CocoaOpenGLFrameBuffer::SetSmoothPicture(const bool smooth) { FHardwareTexture& texture = m_renderTarget.GetColorTexture(); - texture.Bind(0, 0, 0); + texture.Bind(0, 0, false); BoundTextureSetFilter(GL_TEXTURE_2D, smooth ? GL_LINEAR : GL_NEAREST); } diff --git a/src/posix/cocoa/sdlglvideo.h b/src/posix/cocoa/sdlglvideo.h index 2199ef420..aac8992d9 100644 --- a/src/posix/cocoa/sdlglvideo.h +++ b/src/posix/cocoa/sdlglvideo.h @@ -68,9 +68,8 @@ protected: int m_lock; bool m_isUpdatePending; - bool m_supportsGamma; + static const bool m_supportsGamma = true; - FShader m_gammaProgram; FHardwareTexture m_gammaTexture; static const size_t GAMMA_TABLE_SIZE = 256; diff --git a/wadsrc/static/shaders/glsl/gammacorrection.fp b/wadsrc/static/shaders/glsl/gammacorrection.fp new file mode 100644 index 000000000..f8c2cf6a3 --- /dev/null +++ b/wadsrc/static/shaders/glsl/gammacorrection.fp @@ -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; +}