From 62a8a3a26cf0668afd618a7678cf37ed0da2763d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 13 Apr 2026 19:30:45 -0600 Subject: [PATCH] [util/gpu] Drop sRGB framebuffer mode; always gamma 2.2 The paint encoder bakes sRGB-encoded font palette values directly (quantizing 0..255 to 0..32767 without decoding), so the shader writes sRGB-ish values. With GL_FRAMEBUFFER_SRGB enabled the driver then applied linear->sRGB encoding on store, giving a double-encoded, washed-out result -- most visible on mid-tone gradients. The cleanest fix is to stop pretending we're doing linear-space compositing: drop the sRGB framebuffer mode entirely and always apply the classic 2.2 gamma exponent for coverage correction (flipped to 1/2.2 in dark mode to match the existing light/dark logic). Removed pieces: - demo_view_t::gamma_mode enum, demo_view_set_gamma_mode, demo_view_cycle_gamma, and the `g` keybinding + help line - demo_renderer_t::set_srgb virtual + every backend override - GLFW_SRGB_CAPABLE hint in gpu-output.hh - Metal swapchain MTLPixelFormatBGRA8Unorm_sRGB -> BGRA8Unorm Replaced with demo_view_update_gamma, called on setup() and after the dark-mode toggle. Shader side stays the same (only alpha gets the gamma curve, as before). Assisted-by: Claude Opus 4.6 (1M context) --- util/gpu-output.hh | 1 - util/gpu/demo-metal.mm | 12 +++------- util/gpu/demo-renderer-d3d11.cc | 1 - util/gpu/demo-renderer-gl.cc | 19 ---------------- util/gpu/demo-renderer.h | 3 --- util/gpu/demo-view.cc | 37 ++++++------------------------- util/gpu/web/hb-gpu-web-webgpu.cc | 2 -- 7 files changed, 10 insertions(+), 65 deletions(-) diff --git a/util/gpu-output.hh b/util/gpu-output.hh index 6e365f61c..4b7583575 100644 --- a/util/gpu-output.hh +++ b/util/gpu-output.hh @@ -171,7 +171,6 @@ struct gpu_output_t #ifdef __APPLE__ glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); #endif - glfwWindowHint (GLFW_SRGB_CAPABLE, GLFW_TRUE); window = glfwCreateWindow (WINDOW_W, WINDOW_H, "HarfBuzz GPU", NULL, NULL); if (!window) { diff --git a/util/gpu/demo-metal.mm b/util/gpu/demo-metal.mm index 64c50a729..d7ac83545 100644 --- a/util/gpu/demo-metal.mm +++ b/util/gpu/demo-metal.mm @@ -213,12 +213,6 @@ struct demo_renderer_metal_t : demo_renderer_t uniforms.stem_darkening = enabled ? 1.f : 0.f; } - bool set_srgb (bool enabled [[maybe_unused]]) override - { - /* Metal uses MTLPixelFormatBGRA8Unorm_sRGB; sRGB is always on. */ - return true; - } - void toggle_vsync (bool &vsync) override { vsync = !vsync; @@ -231,7 +225,7 @@ struct demo_renderer_metal_t : demo_renderer_t if (offscreenTexture && offscreen_w == width && offscreen_h == height) return; MTLTextureDescriptor *desc = - [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm_sRGB + [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm width:width height:height mipmapped:NO]; @@ -395,7 +389,7 @@ demo_renderer_create_metal (GLFWwindow *window) NSWindow *nswindow = glfwGetCocoaWindow (window); r->metalLayer = [CAMetalLayer layer]; r->metalLayer.device = r->device; - r->metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB; + r->metalLayer.pixelFormat = MTLPixelFormatBGRA8Unorm; nswindow.contentView.layer = r->metalLayer; nswindow.contentView.wantsLayer = YES; @@ -451,7 +445,7 @@ demo_renderer_create_metal (GLFWwindow *window) pipelineDesc.vertexFunction = vertexFunc; pipelineDesc.fragmentFunction = fragmentFunc; pipelineDesc.vertexDescriptor = vertexDesc; - pipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB; + pipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm; pipelineDesc.colorAttachments[0].blendingEnabled = YES; pipelineDesc.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha; pipelineDesc.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha; diff --git a/util/gpu/demo-renderer-d3d11.cc b/util/gpu/demo-renderer-d3d11.cc index e46f7b9b0..416b3ab8a 100644 --- a/util/gpu/demo-renderer-d3d11.cc +++ b/util/gpu/demo-renderer-d3d11.cc @@ -269,7 +269,6 @@ float4 ps_main (PSInput input) : SV_Target { void set_background (float r, float g, float b, float a) override { bg_r = r; bg_g = g; bg_b = b; bg_a = a; } void set_debug (bool e) override { debug_val = e ? 1.f : 0.f; } void set_stem_darkening (bool e) override { stem_val = e ? 1.f : 0.f; } - bool set_srgb (bool) override { return false; } void toggle_vsync (bool &v) override { v = !v; vsync_on = v; } void display (glyph_vertex_t *vertices, unsigned count, diff --git a/util/gpu/demo-renderer-gl.cc b/util/gpu/demo-renderer-gl.cc index 8baa7f83e..ff387bbda 100644 --- a/util/gpu/demo-renderer-gl.cc +++ b/util/gpu/demo-renderer-gl.cc @@ -111,25 +111,6 @@ struct demo_renderer_gl_t : demo_renderer_t demo_glstate_set_stem_darkening (st, enabled); } - bool set_srgb (bool enabled) override - { -#if defined(GL_FRAMEBUFFER_SRGB) - while (glGetError () != GL_NO_ERROR) - ; - if (enabled) - { - glEnable (GL_FRAMEBUFFER_SRGB); - if (glGetError () != GL_NO_ERROR) - return false; - } - else - glDisable (GL_FRAMEBUFFER_SRGB); - return true; -#else - return false; -#endif - } - void toggle_vsync (bool &vsync) override { vsync = !vsync; diff --git a/util/gpu/demo-renderer.h b/util/gpu/demo-renderer.h index 889457082..99e2d4ba1 100644 --- a/util/gpu/demo-renderer.h +++ b/util/gpu/demo-renderer.h @@ -28,9 +28,6 @@ struct demo_renderer_t virtual void set_debug (bool enabled) = 0; virtual void set_stem_darkening (bool enabled) { (void) enabled; } - /* sRGB framebuffer control; returns false if unsupported. */ - virtual bool set_srgb (bool enabled) = 0; - /* Vsync toggle. */ virtual void toggle_vsync (bool &vsync) = 0; diff --git a/util/gpu/demo-view.cc b/util/gpu/demo-view.cc index 3ac450aaa..d73197625 100644 --- a/util/gpu/demo-view.cc +++ b/util/gpu/demo-view.cc @@ -35,7 +35,6 @@ struct demo_view_t { bool debug; bool stem_darkening = true; double screen_angle; /* 2D screen-space rotation from pinch */ - enum { GAMMA_SRGB, GAMMA_2_2, GAMMA_NONE } gamma_mode; /* Mouse handling */ int buttons; @@ -238,31 +237,13 @@ demo_view_toggle_vsync (demo_view_t *vu) LOGI ("Setting vsync %s.\n", vu->vsync ? "on" : "off"); } +/* Gamma 2.2 coverage correction: the exponent flips in dark mode + * so thin strokes don't over-darken against the light-on-dark + * contrast. */ static void -demo_view_set_gamma_mode (demo_view_t *vu, int mode) +demo_view_update_gamma (demo_view_t *vu) { - vu->renderer->set_srgb (false); - - if (mode == demo_view_t::GAMMA_SRGB) - { - if (!vu->renderer->set_srgb (true)) - mode = demo_view_t::GAMMA_2_2; - } - - vu->gamma_mode = (decltype(vu->gamma_mode)) mode; - float gamma = mode == demo_view_t::GAMMA_2_2 ? (vu->dark_mode ? 1.f/2.2f : 2.2f) : 1.f; - vu->renderer->set_gamma (gamma); - - const char *names[] = {"sRGB", "gamma 2.2", "none"}; - LOGI ("Gamma correction: %s.\n", names[mode]); - vu->needs_redraw = true; -} - -static void -demo_view_cycle_gamma (demo_view_t *vu) -{ - int next = (vu->gamma_mode + 1) % 3; - demo_view_set_gamma_mode (vu, next); + vu->renderer->set_gamma (vu->dark_mode ? 1.f / 2.2f : 2.2f); } #define LIGHT_FG 0.f, 0.f, 0.f, 1.f @@ -285,7 +266,7 @@ demo_view_toggle_dark (demo_view_t *vu) vu->renderer->set_foreground (LIGHT_FG); vu->renderer->set_background (LIGHT_BG); } - demo_view_set_gamma_mode (vu, vu->gamma_mode); + demo_view_update_gamma (vu); vu->needs_redraw = true; } @@ -405,9 +386,6 @@ demo_view_key_func (demo_view_t *vu, int key, int scancode, int action, int mods case GLFW_KEY_SLASH: demo_view_print_help (vu); break; - case GLFW_KEY_G: - demo_view_cycle_gamma (vu); - break; case GLFW_KEY_B: demo_view_toggle_dark (vu); break; @@ -723,7 +701,6 @@ demo_view_print_help (demo_view_t *vu) LOGI (" Space Toggle animation\n"); LOGI (" f Toggle fullscreen\n"); LOGI (" b Toggle dark mode\n"); - LOGI (" g Cycle gamma (sRGB/2.2/none)\n"); LOGI (" s Toggle stem darkening\n"); LOGI (" d Toggle debug heatmap\n"); LOGI (" v Toggle vsync\n"); @@ -824,7 +801,7 @@ demo_view_setup (demo_view_t *vu) vu->renderer->setup (); vu->renderer->set_foreground (LIGHT_FG); vu->renderer->set_background (LIGHT_BG); - demo_view_set_gamma_mode (vu, demo_view_t::GAMMA_SRGB); + demo_view_update_gamma (vu); vu->stem_darkening = true; vu->renderer->set_stem_darkening (true); } diff --git a/util/gpu/web/hb-gpu-web-webgpu.cc b/util/gpu/web/hb-gpu-web-webgpu.cc index 634ea73a0..38af51c86 100644 --- a/util/gpu/web/hb-gpu-web-webgpu.cc +++ b/util/gpu/web/hb-gpu-web-webgpu.cc @@ -246,8 +246,6 @@ struct demo_renderer_webgpu_t : demo_renderer_t bool debug_mode = false; bool stem_mode = true; - bool set_srgb (bool enabled) override { return false; /* no sRGB framebuffer */ } - void toggle_vsync (bool &vsync) override { /* always vsync in browser */ } void display (glyph_vertex_t *vertices, unsigned int count,