diff options
author | John Novak <john@johnovak.net> | 2022-04-25 09:01:04 +0300 |
---|---|---|
committer | John Novak <john@johnovak.net> | 2022-04-30 05:07:42 +0300 |
commit | 46c3448ed3c5c86fb7cb8a4bea8337322b5515e0 (patch) | |
tree | 50c53eed32f7c0cf0d6cc443cd375beb9504735d | |
parent | 08cce17356f6a2a893221f61be2326dbe5f7cabb (diff) |
Support gamma-correct interpolation in the 'sharp' OpenGL shaderjnovak/sharp-shader-gamma-fix
-rw-r--r-- | include/sdlmain.h | 1 | ||||
-rw-r--r-- | src/gui/sdlmain.cpp | 31 |
2 files changed, 29 insertions, 3 deletions
diff --git a/include/sdlmain.h b/include/sdlmain.h index 91d33822e..26de3e6bc 100644 --- a/include/sdlmain.h +++ b/include/sdlmain.h @@ -185,6 +185,7 @@ struct SDL_Block { bool pixel_buffer_object = false; bool npot_textures_supported = false; bool use_shader; + bool framebuffer_is_srgb_encoded; GLuint program_object; const char *shader_src; struct { diff --git a/src/gui/sdlmain.cpp b/src/gui/sdlmain.cpp index fafb5896a..875528c79 100644 --- a/src/gui/sdlmain.cpp +++ b/src/gui/sdlmain.cpp @@ -1388,10 +1388,18 @@ static bool LoadGLShaders(const char *src, GLuint *vertex, GLuint *fragment) { return ""; } -// "flexible" shaders properly handle window-resizing and NPOT textures +[[maybe_unused]] static bool is_sharp_shader() +{ + constexpr std::array<std::string_view, 2> sharp_shader_names{{ + "sharp", + "default", + }}; + return contains(sharp_shader_names, get_glshader_value()); +} + [[maybe_unused]] static bool is_shader_flexible() { - const std::array<std::string, 3> flexible_shader_names{{ + constexpr std::array<std::string_view, 3> flexible_shader_names{{ "sharp", "none", "default", @@ -1672,6 +1680,7 @@ dosurface: goto dosurface; } SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1); SetupWindowScaled(SCREEN_OPENGL, sdl.desktop.want_resizable_window); /* We may simply use SDL_BYTESPERPIXEL @@ -1857,10 +1866,26 @@ dosurface: assert(emptytex); memset(emptytex, 0, texture_area_bytes); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texsize_w, texsize_h, + + int is_framebuffer_srgb_capable; + SDL_GL_GetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, + &is_framebuffer_srgb_capable); + + sdl.opengl.framebuffer_is_srgb_encoded = is_sharp_shader() && is_framebuffer_srgb_capable > 0; + + if (is_sharp_shader() && !sdl.opengl.framebuffer_is_srgb_encoded) + LOG_WARNING("OPENGL: sRGB framebuffer not supported"); + + // Using GL_SRGB8_ALPHA8 because GL_SRGB8 doesn't work properly with Mesa drivers on certain integrated Intel GPUs + const auto texformat = sdl.opengl.framebuffer_is_srgb_encoded ? GL_SRGB8_ALPHA8 : GL_RGB8; + glTexImage2D(GL_TEXTURE_2D, 0, texformat, texsize_w, texsize_h, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, emptytex); delete[] emptytex; + if (sdl.opengl.framebuffer_is_srgb_encoded) { + glEnable(GL_FRAMEBUFFER_SRGB); + } + glClearColor (0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); |