From dc19a8002a66447850b86e0151eb4dac9c4f02f9 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 1 Apr 2026 23:08:30 -0600 Subject: [PATCH] [gpu] Add optional MSAA mode for small-size antialiasing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add hb_gpu_render_msaa() that evaluates 9 samples (3x3 grid at ±1/3 pixel) to reduce moiré at small sizes. GLSL only. The demo compiles two shader programs and toggles between them with the 'm' key, so the MSAA program's register pressure does not affect the fast single-sample path. Assisted-by: Claude --- src/hb-gpu-fragment-glsl.hh | 47 +++++++++++++++--------------- src/hb-gpu-fragment.glsl | 47 +++++++++++++++--------------- util/gpu/demo-glstate.cc | 55 ++++++++++++++++++++++++++++++------ util/gpu/demo-glstate.h | 3 ++ util/gpu/demo-renderer-gl.cc | 5 ++++ util/gpu/demo-renderer.h | 1 + util/gpu/demo-shader.cc | 20 +++++++++---- util/gpu/demo-shader.h | 2 +- util/gpu/demo-view.cc | 6 ++++ 9 files changed, 125 insertions(+), 61 deletions(-) diff --git a/src/hb-gpu-fragment-glsl.hh b/src/hb-gpu-fragment-glsl.hh index 2cde09302..423948917 100644 --- a/src/hb-gpu-fragment-glsl.hh +++ b/src/hb-gpu-fragment-glsl.hh @@ -157,8 +157,12 @@ static const char *hb_gpu_fragment_glsl = " return ivec2 (hCount, vCount);\n" "}\n" "\n" -"/* Single-sample coverage. */\n" -"float _hb_gpu_render_single (vec2 renderCoord, uint glyphLoc_)\n" +"/* Return coverage in [0, 1].\n" +" *\n" +" * renderCoord: em-space sample position\n" +" * glyphLoc: texel offset of glyph blob in atlas\n" +" */\n" +"float hb_gpu_render (vec2 renderCoord, uint glyphLoc_)\n" "{\n" " vec2 emsPerPixel = fwidth (renderCoord);\n" " vec2 pixelsPerEm = 1.0 / emsPerPixel;\n" @@ -275,34 +279,31 @@ static const char *hb_gpu_fragment_glsl = " return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt);\n" "}\n" "\n" -"/* Return coverage in [0, 1].\n" -" *\n" -" * At small sizes (ppem < 16), blends in 4 extra samples at\n" -" * ±1/3 pixel offsets to reduce moiré and aliasing.\n" +"/* Coverage with 4 extra samples at ±1/3 pixel offsets,\n" +" * blended with the center sample. Reduces moiré at small\n" +" * sizes. blend is in [0, 1]: 0 = center only, 1 = full MSAA.\n" " *\n" " * renderCoord: em-space sample position\n" " * glyphLoc: texel offset of glyph blob in atlas\n" +" * blend: MSAA blend factor\n" " */\n" -"float hb_gpu_render (vec2 renderCoord, uint glyphLoc_)\n" +"float hb_gpu_render_msaa (vec2 renderCoord, uint glyphLoc_,\n" +" float blend)\n" "{\n" -" float c = _hb_gpu_render_single (renderCoord, glyphLoc_);\n" +" float c = hb_gpu_render (renderCoord, glyphLoc_);\n" "\n" -" vec2 emsPerPixel = fwidth (renderCoord);\n" -" float ppem = 1.0 / max (emsPerPixel.x, emsPerPixel.y);\n" +" vec2 d = fwidth (renderCoord) * (1.0 / 3.0);\n" +" float msaa = (1.0 / 8.0) *\n" +" (hb_gpu_render (renderCoord + vec2 (-d.x, -d.y), glyphLoc_) +\n" +" hb_gpu_render (renderCoord + vec2 ( 0.0, -d.y), glyphLoc_) +\n" +" hb_gpu_render (renderCoord + vec2 ( d.x, -d.y), glyphLoc_) +\n" +" hb_gpu_render (renderCoord + vec2 (-d.x, 0.0), glyphLoc_) +\n" +" hb_gpu_render (renderCoord + vec2 ( d.x, 0.0), glyphLoc_) +\n" +" hb_gpu_render (renderCoord + vec2 (-d.x, d.y), glyphLoc_) +\n" +" hb_gpu_render (renderCoord + vec2 ( 0.0, d.y), glyphLoc_) +\n" +" hb_gpu_render (renderCoord + vec2 ( d.x, d.y), glyphLoc_));\n" "\n" -" if (ppem < 16.0)\n" -" {\n" -" vec2 d = emsPerPixel * (1.0 / 3.0);\n" -" float msaa = 0.25 *\n" -" (_hb_gpu_render_single (renderCoord + vec2 (-d.x, -d.y), glyphLoc_) +\n" -" _hb_gpu_render_single (renderCoord + vec2 ( d.x, -d.y), glyphLoc_) +\n" -" _hb_gpu_render_single (renderCoord + vec2 (-d.x, d.y), glyphLoc_) +\n" -" _hb_gpu_render_single (renderCoord + vec2 ( d.x, d.y), glyphLoc_));\n" -"\n" -" c = mix (c, msaa, smoothstep (16.0, 8.0, ppem));\n" -" }\n" -"\n" -" return c;\n" +" return mix (c, msaa, blend);\n" "}\n" "\n" "/* Stem darkening for small sizes.\n" diff --git a/src/hb-gpu-fragment.glsl b/src/hb-gpu-fragment.glsl index 5d2c70a37..e9a7933c6 100644 --- a/src/hb-gpu-fragment.glsl +++ b/src/hb-gpu-fragment.glsl @@ -156,8 +156,12 @@ ivec2 _hb_gpu_curve_counts (vec2 renderCoord, uint glyphLoc_) return ivec2 (hCount, vCount); } -/* Single-sample coverage. */ -float _hb_gpu_render_single (vec2 renderCoord, uint glyphLoc_) +/* Return coverage in [0, 1]. + * + * renderCoord: em-space sample position + * glyphLoc: texel offset of glyph blob in atlas + */ +float hb_gpu_render (vec2 renderCoord, uint glyphLoc_) { vec2 emsPerPixel = fwidth (renderCoord); vec2 pixelsPerEm = 1.0 / emsPerPixel; @@ -274,34 +278,31 @@ float _hb_gpu_render_single (vec2 renderCoord, uint glyphLoc_) return _hb_gpu_calc_coverage (xcov, ycov, xwgt, ywgt); } -/* Return coverage in [0, 1]. - * - * At small sizes (ppem < 16), blends in 4 extra samples at - * ±1/3 pixel offsets to reduce moiré and aliasing. +/* Coverage with 4 extra samples at ±1/3 pixel offsets, + * blended with the center sample. Reduces moiré at small + * sizes. blend is in [0, 1]: 0 = center only, 1 = full MSAA. * * renderCoord: em-space sample position * glyphLoc: texel offset of glyph blob in atlas + * blend: MSAA blend factor */ -float hb_gpu_render (vec2 renderCoord, uint glyphLoc_) +float hb_gpu_render_msaa (vec2 renderCoord, uint glyphLoc_, + float blend) { - float c = _hb_gpu_render_single (renderCoord, glyphLoc_); + float c = hb_gpu_render (renderCoord, glyphLoc_); - vec2 emsPerPixel = fwidth (renderCoord); - float ppem = 1.0 / max (emsPerPixel.x, emsPerPixel.y); + vec2 d = fwidth (renderCoord) * (1.0 / 3.0); + float msaa = (1.0 / 8.0) * + (hb_gpu_render (renderCoord + vec2 (-d.x, -d.y), glyphLoc_) + + hb_gpu_render (renderCoord + vec2 ( 0.0, -d.y), glyphLoc_) + + hb_gpu_render (renderCoord + vec2 ( d.x, -d.y), glyphLoc_) + + hb_gpu_render (renderCoord + vec2 (-d.x, 0.0), glyphLoc_) + + hb_gpu_render (renderCoord + vec2 ( d.x, 0.0), glyphLoc_) + + hb_gpu_render (renderCoord + vec2 (-d.x, d.y), glyphLoc_) + + hb_gpu_render (renderCoord + vec2 ( 0.0, d.y), glyphLoc_) + + hb_gpu_render (renderCoord + vec2 ( d.x, d.y), glyphLoc_)); - if (ppem < 16.0) - { - vec2 d = emsPerPixel * (1.0 / 3.0); - float msaa = 0.25 * - (_hb_gpu_render_single (renderCoord + vec2 (-d.x, -d.y), glyphLoc_) + - _hb_gpu_render_single (renderCoord + vec2 ( d.x, -d.y), glyphLoc_) + - _hb_gpu_render_single (renderCoord + vec2 (-d.x, d.y), glyphLoc_) + - _hb_gpu_render_single (renderCoord + vec2 ( d.x, d.y), glyphLoc_)); - - c = mix (c, msaa, smoothstep (16.0, 8.0, ppem)); - } - - return c; + return mix (c, msaa, blend); } /* Stem darkening for small sizes. diff --git a/util/gpu/demo-glstate.cc b/util/gpu/demo-glstate.cc index e81323e44..b4defbdc4 100644 --- a/util/gpu/demo-glstate.cc +++ b/util/gpu/demo-glstate.cc @@ -6,7 +6,14 @@ struct demo_glstate_t { GLuint program; + GLuint program_msaa; + bool msaa; demo_atlas_t *atlas; + /* Cached uniform values for program switching. */ + float gamma; + float fg[4]; + float debug; + float stem_darkening; }; demo_glstate_t * @@ -14,7 +21,9 @@ demo_glstate_create (void) { demo_glstate_t *st = (demo_glstate_t *) calloc (1, sizeof (demo_glstate_t)); - st->program = demo_shader_create_program (); + st->program = demo_shader_create_program (false); + st->program_msaa = demo_shader_create_program (true); + st->msaa = false; st->atlas = demo_atlas_create (1024 * 1024); return st; @@ -28,14 +37,40 @@ demo_glstate_destroy (demo_glstate_t *st) demo_atlas_destroy (st->atlas); glDeleteProgram (st->program); + glDeleteProgram (st->program_msaa); free (st); } +static GLuint +demo_glstate_active_program (demo_glstate_t *st) +{ + return st->msaa ? st->program_msaa : st->program; +} + +static void +demo_glstate_apply_uniforms (demo_glstate_t *st) +{ + GLuint p = demo_glstate_active_program (st); + demo_atlas_set_uniforms (st->atlas); + glUniform1f (glGetUniformLocation (p, "u_gamma"), st->gamma); + glUniform4fv (glGetUniformLocation (p, "u_foreground"), 1, st->fg); + glUniform1f (glGetUniformLocation (p, "u_debug"), st->debug); + glUniform1f (glGetUniformLocation (p, "u_stem_darkening"), st->stem_darkening); +} + +void +demo_glstate_set_msaa (demo_glstate_t *st, bool enabled) +{ + st->msaa = enabled; + glUseProgram (demo_glstate_active_program (st)); + demo_glstate_apply_uniforms (st); +} + void demo_glstate_setup (demo_glstate_t *st) { - glUseProgram (st->program); + glUseProgram (demo_glstate_active_program (st)); demo_atlas_set_uniforms (st->atlas); demo_glstate_set_gamma (st, 1.f); @@ -54,35 +89,39 @@ demo_glstate_get_atlas (demo_glstate_t *st) void demo_glstate_set_matrix (demo_glstate_t *st, float mat[16]) { - glUniformMatrix4fv (glGetUniformLocation (st->program, "u_matViewProjection"), 1, GL_FALSE, mat); + glUniformMatrix4fv (glGetUniformLocation (demo_glstate_active_program (st), "u_matViewProjection"), 1, GL_FALSE, mat); GLint viewport[4]; glGetIntegerv (GL_VIEWPORT, viewport); - glUniform2f (glGetUniformLocation (st->program, "u_viewport"), + glUniform2f (glGetUniformLocation (demo_glstate_active_program (st), "u_viewport"), (float) viewport[2], (float) viewport[3]); } void demo_glstate_set_gamma (demo_glstate_t *st, float gamma) { - glUniform1f (glGetUniformLocation (st->program, "u_gamma"), gamma); + st->gamma = gamma; + glUniform1f (glGetUniformLocation (demo_glstate_active_program (st), "u_gamma"), gamma); } void demo_glstate_set_foreground (demo_glstate_t *st, float r, float g, float b, float a) { - glUniform4f (glGetUniformLocation (st->program, "u_foreground"), r, g, b, a); + st->fg[0] = r; st->fg[1] = g; st->fg[2] = b; st->fg[3] = a; + glUniform4f (glGetUniformLocation (demo_glstate_active_program (st), "u_foreground"), r, g, b, a); } void demo_glstate_set_debug (demo_glstate_t *st, bool enabled) { - glUniform1f (glGetUniformLocation (st->program, "u_debug"), enabled ? 1.f : 0.f); + st->debug = enabled ? 1.f : 0.f; + glUniform1f (glGetUniformLocation (demo_glstate_active_program (st), "u_debug"), st->debug); } void demo_glstate_set_stem_darkening (demo_glstate_t *st, bool enabled) { - glUniform1f (glGetUniformLocation (st->program, "u_stem_darkening"), enabled ? 1.f : 0.f); + st->stem_darkening = enabled ? 1.f : 0.f; + glUniform1f (glGetUniformLocation (demo_glstate_active_program (st), "u_stem_darkening"), st->stem_darkening); } diff --git a/util/gpu/demo-glstate.h b/util/gpu/demo-glstate.h index 470dd8335..ccd911a19 100644 --- a/util/gpu/demo-glstate.h +++ b/util/gpu/demo-glstate.h @@ -42,5 +42,8 @@ demo_glstate_set_debug (demo_glstate_t *st, bool enabled); void demo_glstate_set_stem_darkening (demo_glstate_t *st, bool enabled); +void +demo_glstate_set_msaa (demo_glstate_t *st, bool enabled); + #endif /* DEMO_GLSTATE_H */ diff --git a/util/gpu/demo-renderer-gl.cc b/util/gpu/demo-renderer-gl.cc index eb8785f7a..777370da5 100644 --- a/util/gpu/demo-renderer-gl.cc +++ b/util/gpu/demo-renderer-gl.cc @@ -111,6 +111,11 @@ struct demo_renderer_gl_t : demo_renderer_t demo_glstate_set_stem_darkening (st, enabled); } + void set_msaa (bool enabled) override + { + demo_glstate_set_msaa (st, enabled); + } + bool set_srgb (bool enabled) override { #if defined(GL_FRAMEBUFFER_SRGB) diff --git a/util/gpu/demo-renderer.h b/util/gpu/demo-renderer.h index 3419dc4af..382fc4e05 100644 --- a/util/gpu/demo-renderer.h +++ b/util/gpu/demo-renderer.h @@ -27,6 +27,7 @@ struct demo_renderer_t virtual void set_background (float r, float g, float b, float a) = 0; virtual void set_debug (bool enabled) = 0; virtual void set_stem_darkening (bool enabled) { (void) enabled; } + virtual void set_msaa (bool enabled) { (void) enabled; } /* sRGB framebuffer control; returns false if unsupported. */ virtual bool set_srgb (bool enabled) = 0; diff --git a/util/gpu/demo-shader.cc b/util/gpu/demo-shader.cc index c9c051cbb..0d8462d89 100644 --- a/util/gpu/demo-shader.cc +++ b/util/gpu/demo-shader.cc @@ -46,7 +46,14 @@ out vec4 fragColor; void main () { + float ppem = 1.0 / max (fwidth (v_texcoord).x, fwidth (v_texcoord).y); + +#ifdef HB_GPU_DEMO_MSAA + float coverage = hb_gpu_render_msaa (v_texcoord, v_glyphLoc, + smoothstep (16.0, 8.0, ppem)); +#else float coverage = hb_gpu_render (v_texcoord, v_glyphLoc); +#endif /* Stem darkening / thinning at small sizes. * Light text on dark: stems get too fat → thin them (exponent > 1). @@ -55,7 +62,7 @@ void main () if (u_stem_darkening > 0.0) coverage = hb_gpu_darken (coverage, dot (u_foreground.rgb, vec3 (1.0 / 3.0)), - 1.0 / max (fwidth (v_texcoord).x, fwidth (v_texcoord).y)); + ppem); if (u_gamma != 1.0) coverage = pow (coverage, u_gamma); @@ -145,24 +152,25 @@ link_program (GLuint vertex_shader, } GLuint -demo_shader_create_program (void) +demo_shader_create_program (bool msaa) { GLuint vertex_shader, fragment_shader, program; #ifdef HB_GPU_ATLAS_2D - const GLchar *preamble = "#version 300 es\nprecision highp float;\nprecision highp int;\n#define HB_GPU_ATLAS_2D\n"; + const char *base = "#version 300 es\nprecision highp float;\nprecision highp int;\n#define HB_GPU_ATLAS_2D\n"; #else - const GLchar *preamble = "#version 330\n"; + const char *base = "#version 330\n"; #endif + const char *msaa_def = msaa ? "#define HB_GPU_DEMO_MSAA\n" : ""; - const GLchar *vert_sources[] = {preamble, + const GLchar *vert_sources[] = {base, hb_gpu_shader_vertex_source (HB_GPU_SHADER_LANG_GLSL), demo_vertex_glsl}; vertex_shader = compile_shader (GL_VERTEX_SHADER, ARRAY_LEN (vert_sources), vert_sources); - const GLchar *frag_sources[] = {preamble, + const GLchar *frag_sources[] = {base, msaa_def, hb_gpu_shader_fragment_source (HB_GPU_SHADER_LANG_GLSL), demo_fragment_glsl}; fragment_shader = compile_shader (GL_FRAGMENT_SHADER, diff --git a/util/gpu/demo-shader.h b/util/gpu/demo-shader.h index 57192ebb4..d5fbe1e09 100644 --- a/util/gpu/demo-shader.h +++ b/util/gpu/demo-shader.h @@ -75,7 +75,7 @@ demo_shader_add_glyph_vertices (const demo_point_t &p, GLuint -demo_shader_create_program (void); +demo_shader_create_program (bool msaa = false); #endif /* DEMO_SHADERS_H */ diff --git a/util/gpu/demo-view.cc b/util/gpu/demo-view.cc index 1f425beca..61a34f727 100644 --- a/util/gpu/demo-view.cc +++ b/util/gpu/demo-view.cc @@ -34,6 +34,7 @@ struct demo_view_t { bool dark_mode; bool debug; bool stem_darkening = true; + bool msaa; double screen_angle; /* 2D screen-space rotation from pinch */ enum { GAMMA_SRGB, GAMMA_2_2, GAMMA_NONE } gamma_mode; @@ -413,6 +414,11 @@ demo_view_key_func (demo_view_t *vu, int key, int scancode, int action, int mods vu->renderer->set_stem_darkening (vu->stem_darkening); LOGI ("Stem darkening: %s.\n", vu->stem_darkening ? "on" : "off"); break; + case GLFW_KEY_M: + vu->msaa = !vu->msaa; + vu->renderer->set_msaa (vu->msaa); + LOGI ("MSAA: %s.\n", vu->msaa ? "on" : "off"); + break; case GLFW_KEY_V: demo_view_toggle_vsync (vu); break;