From 02d1a3c1c3169784c18a6cd874ef94d099d8f444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Wernst=C3=A5l?= Date: Mon, 23 Jan 2023 14:12:14 +0100 Subject: [PATCH] feat(srgb): swap textures and framebuffers to SRGB --- kitty/gl.c | 2 +- kitty/glfw.c | 3 +++ kitty/shaders.c | 8 ++++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/kitty/gl.c b/kitty/gl.c index 21435bd09..2776c121c 100644 --- a/kitty/gl.c +++ b/kitty/gl.c @@ -71,7 +71,7 @@ update_surface_size(int w, int h, GLuint offscreen_texture_id) { glViewport(0, 0, w, h); if (offscreen_texture_id) { glBindTexture(GL_TEXTURE_2D, offscreen_texture_id); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); + glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); } } diff --git a/kitty/glfw.c b/kitty/glfw.c index 3c490c2b1..5cd6f85b9 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -11,6 +11,7 @@ #include "charsets.h" #include #include "glfw-wrapper.h" +#include "gl.h" #ifndef __APPLE__ #include "freetype_render_ui_text.h" #endif @@ -870,6 +871,8 @@ create_os_window(PyObject UNUSED *self, PyObject *args, PyObject *kw) { if (is_first_window) { gl_init(); } + // Will make the GPU automatically apply SRGB gamma curve on the resulting framebuffer + glEnable(GL_FRAMEBUFFER_SRGB); bool is_semi_transparent = glfwGetWindowAttrib(glfw_window, GLFW_TRANSPARENT_FRAMEBUFFER); // blank the window once so that there is no initial flash of color // changing, in case the background color is not black diff --git a/kitty/shaders.c b/kitty/shaders.c index 21a391bb1..0158333ef 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -102,7 +102,7 @@ realloc_sprite_texture(FONTS_DATA_HANDLE fg) { znum = z + 1; SpriteMap *sprite_map = (SpriteMap*)fg->sprite_map; width = xnum * sprite_map->cell_width; height = ynum * sprite_map->cell_height; - glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, width, height, znum); + glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_SRGB8_ALPHA8, width, height, znum); if (sprite_map->texture_id) { // need to re-alloc src_ynum = MAX(1, sprite_map->last_ynum); @@ -159,7 +159,7 @@ send_image_to_gpu(GLuint *tex_id, const void* data, GLsizei width, GLsizei heigh } glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, r); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, r); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, is_opaque ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, data); + glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA, width, height, 0, is_opaque ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, data); } // }}} @@ -612,7 +612,7 @@ render_a_bar(OSWindow *os_window, Screen *screen, const CellRenderData *crd, Win glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bar_width, bar_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bar->buf); + glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA, bar_width, bar_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bar->buf); set_cell_uniforms(1.f, false); bind_program(GRAPHICS_PROGRAM); send_graphics_data_to_gpu(1, os_window->gvao_idx, &data); @@ -793,7 +793,7 @@ draw_cells_interleaved_premult(ssize_t vao_idx, ssize_t gvao_idx, Screen *screen glGenFramebuffers(1, &os_window->offscreen_framebuffer); glGenTextures(1, &os_window->offscreen_texture_id); glBindTexture(GL_TEXTURE_2D, os_window->offscreen_texture_id); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, os_window->viewport_width, os_window->viewport_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); + glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA, os_window->viewport_width, os_window->viewport_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);